FFMPEG Malformed AAC bitstream detected : use the audio bitstream filter 'aac_adtstoasc' to fix it error
Asked Answered
C

2

5

I am working with ffmpeg transcoding.c example. When i set video encoder codec to AV_CODEC_ID_H264 and audio encoder codec to AV_CODEC_ID_AAC I got following error.

enter image description here

How can i fix this problem.

Cutwork answered 15/10, 2015 at 11:31 Comment(3)
I would look for av_bitstream_filter_init and av_bitstream_filter_filter in the source files.Nunatak
what is the name of the AAC codec doing the encoding? is it libfaac?Guardado
Why do WIndows users tend to make screenshots of console outputs instead of copying and pasting the text?Quark
C
7

First of all, thanks for the answers.

Solution of the my problem is AVBitStreamFilterContext*. I added following lines into the "encode_write_frame" method and it is ok.

if(ifmt_ctx->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO && ifmt_ctx->streams[stream_index]->codec->codec_id == AV_CODEC_ID_H264){
    AVBitStreamFilterContext* h264BitstreamFilterContext = av_bitstream_filter_init("h264_mp4toannexb");
    av_bitstream_filter_filter(h264BitstreamFilterContext, ofmt_ctx->streams[stream_index]->codec, NULL, &enc_pkt.data, &enc_pkt.size, enc_pkt.data, enc_pkt.size, 0);
} else if(ifmt_ctx->streams[stream_index]->codec->codec_id == AV_CODEC_ID_AAC) {        
    AVBitStreamFilterContext* aacBitstreamFilterContext = av_bitstream_filter_init("aac_adtstoasc");
    av_bitstream_filter_filter(aacBitstreamFilterContext, ofmt_ctx->streams[stream_index]->codec, NULL, &enc_pkt.data, &enc_pkt.size, enc_pkt.data, enc_pkt.size, 0);
}
Cutwork answered 16/10, 2015 at 9:48 Comment(2)
Could you say where in that function you added it? Perhaps as a patch file.Fresh
I tired it before the av_packet_rescale_ts method. The code finishes but the resulting file has only audio...Abott
G
4

Your aac audio frames still have adts headers when provided to a unit of processing which is expecting raw aac. Adts header allow you to dump AAC data directly to a file, call it foo.aac and open it with other program.

In this case the unit is the MP4 container code. MP4 container requires AV_CODEC_FLAG_GLOBAL_HEADER, which means all stream should contains stream data only, and other data is provided by setting AVCodecContext.extradata. Because MP4 has its own way of transporting metainformation (here transport info), writing that transport prefix before each frame will make the data unreadable.

Are you sure you have the following lines and that the flag CODEC_FLAG_GLOBAL_HEADER is set?

//ofmt_ctx is AVFormatContext
//enc_ctx is the AVCodecContext of the current stream 
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
    enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;

Without them the encoder may add the metadatas in the data which is sent to the container. For AAC it is the ADTS header, for H264 it is SPS and PPS data.

Guardado answered 15/10, 2015 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.