AAC encoder delay

Referencing this document:

https://developer.apple.com/library/content/documentation/QuickTime/QTFF/QTFFAppenG/QTFFAppenG.html


Hi,


I'm looking to determine of there are any existing Apple or third party encoders which use the method 'Track Structures to Represent Encoder Delay Explicitly' for audio priming / handling encoder delay in AAC in a .mov.


Looking at Quicktime 7 (very old I know), Quicktime X and Compressor 4.3.2, they all use the historical solution of encoding a file for the decoder to assume a delay of 2112 samples. So far this is the only method I can see Apple uses in any of their tools despite the description of the track structure (modern) method in their document above.


Does anyone know if there are any Apple or 3rd party encoders which can produce a .mov that strictly follows the modern' method outlined in the above link, known as 'Using Track Structures to Represent Encoder Delay Explicitly'?


Alternativly, can anyone confirm that Quicktime / AVFoundation based decoders will *always* assume 2112 samples of encoder delay no matter the track structure?


Many thanks

Mark

Answered by galad87 in 241434022

The next version of HandBrake will use an explicit encoder delay (you can already download a nightly build or compile the source code yourself).

When using AVFoundation's AVAssetWriter, it will add an edit list for your AAC track (provided your CMSampleBuffers have the correct data attached so AVF knows the delay).

The keys for this are kCMSampleBufferAttachmentKey_TrimDurationAtStart and kCMSampleBufferAttachmentKey_TrimDurationAtEnd.


Otherwise, as far as I know, QuickTime/AVF assume 2112 samples of delay.

Many thanks Valerian. Thats very useful. Are you aware of any encoding software on macOS that actaully uses this explicit encoder delay method? That seems to be the way that many other encoders and decoders have gone with regards to creating or expecting explicit encoder delay, but I cant find any QT/AVF based encoders which actually produce a file that does this.


Many thanks

Mark

Accepted Answer

The next version of HandBrake will use an explicit encoder delay (you can already download a nightly build or compile the source code yourself).

Thanks galad87. Thats actually very interesting.


The reason for my initial post is because I've been using ffmpeg and found it is not currently producing a .mov with aac which decodes in sync in Quicktime players.


Looking at the latest Handbrake nightly, it does have the Sample to Group atom in the output file, but it actually suffers the exact same issue as ffmpeg. Audio is out of sync (by about 1 frame) when decoded in Quicktime players.


It just seems that Quicktime always falls back on the historical 2112 method, no matter what the file tries to tell it.


I'm finding it hard to pass on a fix for ffmpeg because despite following the recomended sample group atom method, no one seems to actually be able to get it to work.


Have you any further insight into why this might be happening?


Many thanks

Mark

HandBrake right now doesn't apply an inplicit delay to file with no explicit encoder delay (because for example every AAC encoder uses a different delay), so it will add a delay if you reencode a file with no explicit encoder delay. Did you try to encode for example some pcm audio?

By the way I already reported an issue in Final Cut Pro X, but QuickTime seemed to work in my limited testing.

Thanks. Actually, for me I've only been inputting files with uncompressed PCM audio and encoding to AAC. So its happening on the first conversion from PCM. The file when played in a Quicktime decoder (such as QT7, QTX, Avid or Switch Player) has incorrect sync. The same file plays fine in VLC.


Does that make sense?

Can you share the test file you are using?

Anyway, there's enough info to open a new radar 😐 either the documentation is wrong or no one actually tested this feature in AVFoundation.

Final Cut Pro, Compressor, Adobe Premiere ... though all will probably create a file with a delay of 2112.

what flavor of aac? ffmpeg has an internal aac encoder and you can build fdk-aac

Here are two files which should be pretty useful to test with. I've been using these to check the sync after encoding using the ffmpeg internal AAC encoder and then playing back in Quicktime. They have an audio pip on the first frame (00fr) and an audio pip on the last frame (23fr). They are Avid DNx115 files.


2 second clip (30mb)

https://www.dropbox.com/s/k9dpuwmgnzc6zl6/SyncTest_2sec_24p.mov?dl=0

1 minute clip (890mb)

https://www.dropbox.com/s/bht8b8hnuwryycc/SyncTest_1min_24p.mov?dl=0


If you see and hear what I do, then you won't hear the pip on first frame and you will hear the last audio pip one frame early. If you convert the AAC file back to PCM with a Quicktime app, you will see the same result on the conversion. Play the AAC file in VLC and it is in sync.

Hi, this is with the internal ffmpeg aac encoder, but have also tested with the audiotoolbox aac encoder in ffmpeg (aac_at) with same results.

build the libfdk-acc, i think latest version of ffmpeg removed this lib but an older version around 2.3 should still build


the source code states that it has delay but only supports 2 formatss


git.libav.org/?p=libav.git;a=blob;f=libavcodec/libfdk-aacenc.c


416 AVCodec ff_libfdk_aac_encoder = {

417 .name = "libfdk_aac",

418 .long_name = NULL_IF_CONFIG_SMALL("Fraunhofer FDK AAC"),

419 .type = AVMEDIA_TYPE_AUDIO,

420 .id = AV_CODEC_ID_AAC,

421 .priv_data_size = sizeof(AACContext),

422 .init = aac_encode_init,

423 .encode2 = aac_encode_frame,

424 .close = aac_encode_close,

425 .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY,

426 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,

427 AV_SAMPLE_FMT_NONE },

428 .priv_class = &aac_enc_class,

429 .defaults = aac_encode_defaults,

430 .profiles = profiles,

431 .supported_samplerates = aac_sample_rates,

432 .channel_layouts = aac_channel_layout,

433 };

Hi galad,


I've posted a longer response with some test files, but its still being moderated, I guess because it contains links to the test files. Anyway, hopefully that is posted soon.


Thanks

Mark

Hi iPad_dev, thanks for your message. There are a number of features which mean I need to stay on the latest ffmpeg, but also I'm not sure this will help since I think its the muxer that is having the issue creating a file which Quicktime will respond too. Having said that, have you tested this AAC codec and found it does something different in this regard? Many thanks, Mark

Hi Valerian, I dont have FCP or Premiere, but in my tests Compressor did not create a sample group atom.

My bad. Sorry, they all use the edit list atom (unless it changed recently) !


I don't remember encountering any file with the sgpd atom ...

Seems that support for this in FFmpeg is quite recent as well (4 months) so this might explain to lack of files having that.

You can encode with FFmpeg (or whatever other library) and mux with AVAssetWriter. That way you're sure QuickTime will read in sync.

i personally don't like to use the AAC codec, performance and quality wise, they're very lacking. opus or vorbis iss what i use with lossy but i regularly build my own custom version ffmpeg with a lot of fixes.


i agree with others, whatever you're using to mux is the issue


converted a flac to default aac encoder, quicktime had no issues playing or a delay.

ffmpeg started on 2017-07-10 at 13:05:35
Report written to logs/log_ffmpeg170710.log
Input #0, flac, from /Volumes/Apps_Media/Music/Genesis/Genesis_Land_Of_Confusion.flac:
  Duration: 00:04:46.23, start: 0.000000, bitrate: 975 kb/s
    Stream #0:0: Audio: flac, 44100 Hz, stereo, s16
Stream mapping:
  Stream #0:0 -> #0:0 (flac (native) -> aac (native))
Press [q] to stop, [?] for help
Output #0, adts, to 'test.aac':
  Metadata:
    encoder         : Lavf57.75.100
    Stream #0:0: Audio: aac (LC), 44100 Hz, stereo, fltp (16 bit), 128 kb/s
    Metadata:
      encoder         : Lavc57.100.103 aac
size=    4625kB time=00:04:46.23 bitrate= 132.4kbits/s speed=49.8x 
video:0kB audio:4541kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.855953%
[aac @ 0x7fedde836c00] Qavg: 952.935

Hi iPad_dev, would you be able to test with one of the two files I posted above. This issue is regarding video / audio sync, so you are certainky not going to notice it unless you have picture. Many thanks, Mark

The thing I noticed with the edit list is that its not counteracting the Quicktime 2112 assumption. In the case of the latest ffmpeg, when encoding using the Audiotoolbox aac codec, the edit list atom has a value set at 2112, but the Quicktime decoder is applying that and also applying an additonal assumed 2112, so the combined delay removal means a loss of 2112 samples overall. At least thats my reading of it. Very crudely, if this edit list is edited with Atom Inspector to change the 2112 to 0, then the file plays in sync (there are issues with the remaining samples not being properly trimmed, but thats a whole other problem!).


Aon on its own the edit list atom does not appear to be enough to force Quicktime to ony read its value. This is where the sample group atoms come in I think, but as said before, Quicktime appears to not be responding to these correctly either.

In QuickTime X it plays the first beep, but if I seek back to the begin and play again it cuts it a bit. Weird. If I export the file in QuickTime it removes the edit list and sample group atom, but it plays the same as before (first time in sync, second time out of sync), so it seems a bug unrelated to the sample group atom.

You should send a bug report to Apple. I'll try to send one too if I have time.

Thats an intersting route. How would I employ the AVAssetWriter step? I'm OK with the ffmpeg steps to produce the streams, but I'm not a developer unfortunatly so wouldn't be able to write my own mux application.

Thats odd, I've never had the first beep play in any Quicktime player, 7 or X or 3rd party. So you encoded the test file I sent with ffmpeg using built in aac codec (or Audiotoolbox aac) and you heard the first beep in QT X? Thats so weird.


I don't think there is a sample group atom at all is there? Latest snapshot build of ffmpeg does not produce a mov with a sample group atom for me. What build are you using? Do you def see a sample group atom for the sound track?


I'd love to post a bug report to Apple, but I'm still a little unsure how to describe the issue beyond saying the file is not being played back in sync. You've been very kind with your time, but could I ask one more favour and ask how you would describe this to Apple so I can make a report which goes to the right place.


Many thanks

Mark

I encoded with the latest HandBrake master branch code, that adds both the edit list and the sample group atom.

There is no way a FFmpeg encoded file is going to work correctly if it doesn't include the sample group atom.

Thanks galad, when I tried it a week or so ago, I could not get a file with the sample group atom, but as you say its working now.


Altghough the file does decode in sync in QTX (although trimming end is not working, so clips run overlong and produce black frame at end), it still sufferes the same issue as before in QT7, so it does seem that that player won't support the 'modern' Apple delay method no matter what. Shame as its a much more useful player than QTX in my opinion.


It seems many thrid party QT players are based on that same underlying tech too - Switch player also does not read the sample group atoms.


Is this all to do with the move from QTKit to AV Foundation? QTKit can't understand sample group delay signaling, whereas AV Foundation can?


Ultimatly, we need ffmpeg dev's to add the sample group code anyway.