Commit 65f2df28eb for ffmpeg

commit 65f2df28eb00edb25e0843131c25e9b51e523b7e
Author: Wesley Maa <wesley.maa@gmail.com>
Date:   Sun Nov 8 10:31:38 2026 -0500

    avcodec/audiotoolboxenc: fix initial_padding for HE-AAC and HE-AACv2

    kAudioConverterPrimeInfo reports leadingFrames in core (AAC-LC) sample
    frames. With SBR (HE-AAC, HE-AACv2) the decoded output runs at twice
    the core rate (ISO/IEC 14496-3, SBR tool, subclause 4.6.18), and the
    encoder's frame_size (mFramesPerPacket) is already reported at the
    output rate (2048). initial_padding is in output-rate samples, so it
    was set to half the actual priming.

    The converter reports leadingFrames = 2112 for every profile and
    sample rate tested (LC and HE at 16-48 kHz, HEv2 at 32-48 kHz); the
    true delay at the output rate is 4224 for HE/HEv2. Apple's own HE-AAC
    files (afconvert -d aach) agree: they store a 2112-sample priming in
    iTunSMPB with a 24 kHz media timescale for 48 kHz audio, i.e. 4224
    output samples.

    Before this patch, an HE-AAC MP4 written with -c:a aac_at -profile:a 4
    had an edit list starting at 2112, and the decoded audio was 2112
    samples late compared to the source when decoded with afconvert or
    AVAssetReader (measured by cross-correlation at 32, 44.1 and 48 kHz).
    With the patch the edit list starts at 4224 and the offset is 0
    samples, matching afconvert's own HE-AAC output. AAC-LC is unchanged.

    Signed-off-by: Wesley Maa <wesley.maa@gmail.com>

diff --git a/libavcodec/audiotoolboxenc.c b/libavcodec/audiotoolboxenc.c
index f588751f62..a006d5faa2 100644
--- a/libavcodec/audiotoolboxenc.c
+++ b/libavcodec/audiotoolboxenc.c
@@ -113,6 +113,10 @@ static int ffat_update_ctx(AVCodecContext *avctx)
                                    kAudioConverterPrimeInfo,
                                    &size, &prime_info)) {
         avctx->initial_padding = prime_info.leadingFrames;
+        if (avctx->codec_id == AV_CODEC_ID_AAC &&
+            (avctx->profile == AV_PROFILE_AAC_HE ||
+             avctx->profile == AV_PROFILE_AAC_HE_V2))
+            avctx->initial_padding *= 2;
     }

     size = sizeof(out_format);