[FFmpeg-devel] Implement muxing AAC in a CAFF container

Lewis Fox lrflew.coll at gmail.com
Sat Nov 10 05:32:54 EET 2018

I was messing around with the CAFF support, and noticed that muxing AAC had been noted as unsupported for a long time. I looked into it, and realized that by using code from movenc.c, this could be fairly trivially added.

I tested this implementation by playing the resulting CAFF files in Quicktime and seeing that they do play correctly. I also used afconvert and compared the resulting files. There were some minor differences (mostly with the packet table being at the end), but there are no major differences that would indicate a problem with the output of FFMPEG.

I originally planned on putting a shared function for writing the elementary stream descriptor in mov_esds.c, along side the function that reads the elementary stream descriptor. However, the implementation in movenc.c used the MOVTrack class, which wouldn't easily be usable by the CAFF encoder. I ended up copying the function into cafenc.c and reworking it to take an AVStream instead. (I also simplified it by assuming the stream is AAC)

The only part of this that isn't quite complete is setting the bitrates in the magic cookie. The buffersize appears to be a constant 6144 in all the tests I did using afconvert. Based on afconvert, the avgbitrate is the target bitrate if using CBR, ABR, or VBR-Constrained and 0 for VBR. This implementation appears to set this field correctly when using the built-in AAC encoder, though it doesn't neccesarily set this value correctly when using the copy codec type. The maxbitrate should be the "max rate in any 1 second window", but I couldn't figure out how to correctly set it. For this patch, I just set it to the same value as avgbitrate (as it doesn't appear to be critical to get it accurate), but I would welcome any suggestion on how to do this correctly. Setting it correctly might require waiting for the end of the stream to be written, but it should be fine to put the magic cookie after the data (though it's hard to test, as Quicktime will play the file even if the magic cookie is mising).

I also included a small, related change in this commit. Put simply, the CAFF magic cookie doesn't contain the version+flags field that the MPEG ESDS section has, but it was still trying to be read by ff_mov_read_esds. I moved that field to mov_read_esds, which fixes reading the magic cookie when demuxing CAFF files.

More information about the ffmpeg-devel mailing list