cmp4h.264fmp4

about fmp4 encoding , how to fill the mdat box wit H264 frame?


I used C language to encode H264 frames (just have I/P frame) into fmp4 files. When I fill the mdat box, I don't know how to fill the frame data.
Is (I/P)frame data filled directly?
Do the first 4 bytes need to be filled with frame length?

I tried to use FFMPG to encode fmp4 file, and found that the data in mdat box was completely different from the original I/P frame data.
Is it coded again?
Can I directly fill the data of I/P frame into mdat box?


Solution

  • H.264 can be in different stream formats. One is called "Annex B" the other one is MP4. In "Annex B" your NAL units are prefix with start codes 00 00 00 01 or 00 00 01. In MP4 your NAL units are prefixed with the size in bytes.

    I assume your encoder emits "Annex B".

    1. Remove the start code (00) 00 00 01

    2. Prefix your NAL units with the size (typically 4 bytes)

    3. Filter out AUD/SPS/PPS NAL units from your stream

    4. Write you converted NAL units into the MDAT box

    5. Create an AVC Configuration Box ('avcC') based on your SPS, PPS and the length size

    6. Store your avcC box in moov->trak->mdia->minf->stbl->avc1->avcC

    7. While you are writing your samples into mdat - keep track of sizes, offsets and frame types to create the correct stts, stss, stsc, stsz and stco boxes.