如何使用avcodec_decode_audio4解码AAC?

use*_*461 5 ffmpeg decoder libavcodec libav libavformat

我将代码 avcodec_decode_audio3 更改为 avcodec_decode_audio4 并添加了帧处理。但现在我不能再解码 AAC 帧了。

为什么 avcodec_decode_audio4 返回-22无效参数)?按照下面的回答,这和AVContext中需要设置的参数有关系吗?

我不得不使用 avcodec_decode_audio4 因为我更新了我的 ffmpeg 然后出现以下错误:

[NULL @ 0xb14f020] Custom get_buffer() for use withavcodec_decode_audio3() detected. 
         Overriding with avcodec_default_get_buffer
[NULL @ 0xb14f020] Please port your application to avcodec_decode_audio4()
Run Code Online (Sandbox Code Playgroud)

根据avcodec_decode_audio4() 中的缓冲区错误,这是一个回归,除了回到 ffmpeg < 0.8 之外,还有其他解决方案吗?

使用 avcodec_decode_audio4 的解码器:

AVCodec *codec;
AVCodecContext *avCtx;
AVFrame * decoded_frame = NULL;
uint8_t *outbuf = static_cast<uint8_t *>(malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE));
AVPacket avPacket;

main(){
    av_register_all();
    codec = avcodec_find_decoder(CODEC_ID_AAC);

    //set parameters
    avCtx = avcodec_alloc_context3(codec);
    avCtx->channels = 1;
    avCtx->sample_rate = 44100;
    avCtx->bit_rate=16;

    if (avcodec_open2(avCtx, codec, NULL) < 0) printf("Could not open codec\n");

    av_init_packet(&avPacket);

    //Main decoder loop
    while(1) 
        my_frame_decoder();
    return 0;
}

void my_frame_decoder() {
    //get data
    ...
    avPacket.size = numBytes;
    avPacket.data = inputBytes;
    int len;

    while (avPacket.size > 0) {

    int got_frame = 0;
    if (!decoded_frame) {
        if (!(decoded_frame = avcodec_alloc_frame())) {
            printf("out of memory");
            return;
        }
    } else {
        avcodec_get_frame_defaults(decoded_frame);
    }

    //-------------------->> returns always -22
    len = avcodec_decode_audio4(avCtx, decoded_frame, &got_frame, &avPacket); 

    //do something with the decoded frame
    ...
    avPacket.size -= len;
    avPacket.data += len;
    }

    return;
}
Run Code Online (Sandbox Code Playgroud)

小智 2

经过几个小时的搜索,我发现必须由初始化的dec_ctx文件avcodec_decode_audio4打开dec_codecav_find_best_stream()

\n\n
1\xc2\xb0 av_find_best_stream(in_fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1,\n                &dec_codec, 0);<br>\n2\xc2\xb0 dec_ctx = m_in_aud_strm->codec;<br>\n3\xc2\xb0 av_opt_set_int(dec_ctx, "refcounted_frames", 1, 0);<br>\n4\xc2\xb0 avcodec_open2(dec_ctx, dec_codec, NULL)<br>\n.\n.\n.\n5\xc2\xb0 avcodec_decode_audio4(dec_ctx, pFrame, &got_frame, &pkt);\n
Run Code Online (Sandbox Code Playgroud)\n