PR #25868 opened by Wesley Maa (WT-MM)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25868
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25868.patch

`kAudioConverterPrimeInfo` reports `leadingFrames` at the AAC core rate, but
`initial_padding` is in output samples, and with SBR (HE-AAC, HE-AACv2) the
output runs at twice the core rate. `aac_at` therefore signalled 2112 samples
of priming instead of 4224, and the decoded audio started 44 ms late at
48 kHz. Apple's own HE-AAC encoder signals 4224.

## Reproducing

    $ ffmpeg -f lavfi -i sine=440:d=5 -ar 48000 -ac 2 src.wav
    $ ffmpeg -i src.wav -c:a aac_at -profile:a 4 ffmpeg.m4a
    $ afconvert -f m4af -d aach src.wav apple.m4a

    $ ffprobe -v trace ffmpeg.m4a 2>&1 | grep 'time='
    duration=243264 time=2112 rate=1.000000    # before
    duration=241152 time=4224 rate=1.000000    # after

    $ ffprobe -v trace apple.m4a 2>&1 | grep 'Parsed iTunSMPB'
    Parsed iTunSMPB: priming 4224, remainder 1536 samples 240000

No FATE test, since `aac_at` is only available on Apple platforms. Tested on
macOS 26 at 32, 44.1 and 48 kHz; AAC-LC output is unchanged.



>From 21f0b5b875dc0e92b0e900c2569890bf8d474355 Mon Sep 17 00:00:00 2001
From: Wesley Maa <[email protected]>
Date: Sun, 8 Nov 2026 10:31:38 -0500
Subject: [PATCH] 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 <[email protected]>
---
 libavcodec/audiotoolboxenc.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libavcodec/audiotoolboxenc.c b/libavcodec/audiotoolboxenc.c
index f588751f621..a006d5faa2d 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);
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to