C++ FFMPEG not writing AVCC box information
c++, ffmpeg, h.264, mp4, video
Solution
Solved it. The data that was required was the SPS and PPS components of the AVCC codec. As the raw H264 stream was in annex b format, this was present at the start of every I-frame, in the NAL units starting `0x00 0x00 0x00 0x01 0x67` and `0x00 0x00 0x00 0x01 0x68`. So what was needed was to copy that information into the AVStream codec's extradata field:
codecContext = stream->codec;
...
// videoSeqHeader contains the PPS and SPS NAL unit data
codecContext->extradata = (uint8_t*)malloc( sizeof(uint8_t) * videoSeqHeader_.size() );
for( unsigned int index = 0; index < videoSeqHeader_.size(); index++ )
{
codecContext->extradata[index] = videoSeqHeader_[index];
}
codecContext->extradata_size = (int)videoSeqHeader_.size();
This resulted in the AVCC box being correctly populated.
Problem
I'm trying to encode raw H264 into an mp4 container using the FFMPEG API in C++. It all works fine, however the AVCC box is empty, and it returns the error: [iso file] Box "avcC" size 8 invalid If I then use the command line tool on the output file: ffmpeg -i output.mp4 -vcodec copy fixed.mp4 The output file works and AVCC is populated with the required information. I'm at a loss as to why this command line argument works but I'm unable to produce the same result using the API. What I do in the C++ code (also do things in between the function calls): ``` outputFormat_ = av_guess_format( "mp4", NULL, NULL ); //AV_CODEC_H264 formatContext_ = avformat_alloc_context(); formatContext_->oformat = outputFormat_; ... AVDictionary *opts = NULL; char tmpstr[50]; sprintf(tmpstr, "%i", muxRate * KILOBYTESTOBYTES); av_dict_set(&opts, "muxrate", tmpstr, 0); avformat_write_header( formatContext_, &opts); av_write_trailer(formatContext_); ``` The output of this is correct, except it's missing the AVCC information. Adding this is manually (and fixing the box lengths accordingly) lets me playback the video fine. Any idea why the API calls are not generating the AVCC info? For reference, here's the chars from the mp4 before the fix: .avc1.........................€.8.H...H..........................................ÿÿ....avcC....stts and after: avc1.........................€.8.H...H..........................................ÿÿ...!avcC.B€(ÿá..gB€(Ú.à.—•...hÎ<€....stts