C++ FFMPEG not writing AVCC box information
I had the problem with empty AVCC boxes with my MP4 files too. It turned out I was setting CODEC_FLAG_GLOBAL_HEADER
flag on the AVCodecContext
instance after calling avcodec_open2
. The flag should be set before calling avcodec_open2
.
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.