4

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.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
doğan
  • 339
  • 1
  • 4
  • 15

2 Answers2

6

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);
}
doğan
  • 339
  • 1
  • 4
  • 15
  • Could you say where in that function you added it? Perhaps as a patch file. – Ryan Nov 25 '15 at 22:16
  • 1
    I tired it before the `av_packet_rescale_ts` method. The code finishes but the resulting file has only audio... – Setheron Jan 22 '16 at 23:06
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.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90