【发布时间】:2011-07-19 01:32:22
【问题描述】:
我正在使用为 Android NDK 编译的 FFmpeg 编写一个 Android 播放器。
我可以通过 FFmpeg 打开文件并写下:
av_register_all();
char* str = (*env) -> GetStringUTFChars(env, argv, 0);
__android_log_print(ANDROID_LOG_INFO, "HelloNDK!", str, str);
if (av_open_input_file (&pFormatCtx, str, NULL, 0, NULL) != 0)
return -2; // Couldn't open file
// Retrieve stream information
if (av_find_stream_info(pFormatCtx) < 0)
return -3; // Couldn't find stream information
// Dump information about file onto standard error
dump_format(pFormatCtx, 0, argv, 0);
// Find the first video stream
videoStream =- 1;
audioStream =- 1;
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO && audioStream <0) {
audioStream = i;
}
}
if (audioStream == -1)
return -5;
aCodecCtx = pFormatCtx->streams[audioStream]->codec;
// Set audio settings from codec info
aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
if (!aCodec) {
fprintf (stderr, "Unsupported codec! \n");
return -1;
}
avcodec_open (aCodecCtx, aCodec);
我现在如何解码音频并将其返回给 Java 程序?也许有人可以给我一个例子
编辑: 使用 avcodec_decode_audio3 时 获取以下内容: 03-22 07:54:00.988: 信息/调试 (31): * ** * ** * ** * * * ** * ** * 03-22 07:54:00.988: INFO / DEBUG (31): 构建指纹: 'generic / sdk / generic /: 2.2/FRF91/43546: eng / test-keys' 03-22 07:54:00.988: 信息/调试 (31): pid: 435, tid: 435>>> org.libsdl.app
我可以使用 avcodec_decode_audio2 吗? 我最近几天达到了以下代码:
AVFormatContext * pFormatCtx;
int i, videoStream, audioStream;
AVCodecContext * pCodecCtx;
AVCodec * pCodec;
AVFrame * pFrame;
AVPacket packet;
int frameFinished;
float aspect_ratio;
AVCodecContext * aCodecCtx;
AVCodec * aCodec;
AVCodecContext * c = NULL;
int out_size, len;
int16_t * audio_buf;
uint8_t * outbuf;
uint8_t inbuf [AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
av_register_all ();
char * str = (* env) -> GetStringUTFChars (env, argv, 0);
if (av_open_input_file (& pFormatCtx, str, NULL, 0, NULL)! = 0)
return -150;
if (av_find_stream_info (pFormatCtx) nb_streams; i + +) {
if (pFormatCtx-> streams [i] -> codec-> codec_type == CODEC_TYPE_VIDEO & & videoStream streams [i] -> codec-> codec_type == CODEC_TYPE_AUDIO & & audioStream streams [audioStream] -> codec;
aCodec = avcodec_find_decoder (aCodecCtx-> codec_id);
if (! aCodec) {
fprintf (stderr, "Unsupported codec! \ n");
return -45;
}
avcodec_open (aCodecCtx, aCodec);
c = avcodec_alloc_context ();
packet_queue_init (& audioq);
while (av_read_frame (pFormatCtx, & packet)> = 0) {
if (packet.stream_index == videoStream) {
} Else if (packet.stream_index == audioStream) {
packet_queue_put (& audioq, & packet);
int len1, data_size;
data_size = 417;
len1 = avcodec_decode_audio2 (aCodecCtx, (int16_t *) audio_buf, & data_size,
packet.data, packet.size);
return packet.size;
} Else {
av_free_packet (& packet);
}
}
return 0;
在这种情况下,当 avcodec_decode_audio2 我得到-1。我做错了什么?
注意: 当我开车时 int data_size = 417;然后DEBUG没有出现并且函数返回-1,但是当我驱动a时:int data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE * 2;然后 vyskakievaet 在你的函数中调试,所以在我自己的函数中!怎么解决?
编辑2: 我的新代码:
JNIEXPORT jint JNICALL Java_org_libsdl_app_SDLActivity_main( JNIEnv* env, jobject obj, int argc, jstring argv, jbyteArray array) {
AVFormatContext *pFormatCtx;
int i, videoStream, audioStream;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame;
AVPacket packet;
int frameFinished;
float aspect_ratio;
AVCodecContext *aCodecCtx;
AVCodec *aCodec;
SDL_Overlay *bmp;
SDL_Surface *screen;
SDL_Rect rect;
SDL_Event event;
SDL_AudioSpec wanted_spec, spec;
AVCodecContext *c= NULL;
int out_size, len;
int16_t *audio_buf;
uint8_t *outbuf;
uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
char *pAudioBuffer = (char *) av_malloc (AVCODEC_MAX_AUDIO_FRAME_SIZE * 2);
av_register_all();
char *str = (*env)->GetStringUTFChars(env, argv, 0);
if(av_open_input_file(&pFormatCtx, str, NULL, 0, NULL)!=0)
return -150; // Couldn't open file
if(av_find_stream_info(pFormatCtx)nb_streams; i++) {
if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO &&
videoStream streams[i]->codec->codec_type==CODEC_TYPE_AUDIO &&
audioStream streams[audioStream]->codec;
aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
if(!aCodec) {
fprintf(stderr, "Unsupported codec!\n");
return -45;
}
avcodec_open(aCodecCtx, aCodec);
c=avcodec_alloc_context();
packet_queue_init(&audioq);
while (av_read_frame(pFormatCtx, &packet)>= 0) {
if (aCodecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
int data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE * 2;
int size=packet.size;
while(size > 0) {
int len = avcodec_decode_audio3(aCodecCtx, (int16_t *) pAudioBuffer, &data_size, &packet);
jbyte *bytes = (*env)->GetByteArrayElements(env, array, NULL);
memcpy(bytes, (int16_t *) pAudioBuffer, size);
(*env)->ReleaseByteArrayElements(env, array, bytes, 0);
size = packet.size-len;
}
}
}
return 5;
}
【问题讨论】:
-
您能解释一下为什么在 memcpy 中使用“size”而不是 len 吗?据我了解,我们从“pAudioBuffer”复制到长度为“size”的“bytes”。但是 size 是 avpacket 的大小,而 pAudioBuffer 已经解码数据,其长度为“len”。那是对的吗? (我不是 C 专业的 - 正在努力学习)
标签: android audio ffmpeg decode