PR #26204 opened by Romain Beauxis (toots) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26204 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26204.patch
Since c31a781412, the audio frame queue spreads an encoder delay longer than a packet over several leading packets. The mp3 muxer only read the first one: libmp3lame at 22050 Hz sends 1105 samples as 576 + 529, and the LAME header got 47 instead of 576. This sums them and subtracts the decoder delay once when writing the Xing header. The new test fate-gapless-mp3-split-delay-remux stream-copies a libmp3lame NUT sample to mp3. It needs a new FATE sample, gapless/lame-split-delay.nut (848 bytes), to be uploaded before merge. It was generated with: `ffmpeg -f lavfi -i sine=r=22050:d=0.2 -ac 1 -c:a libmp3lame -b:a 16k -fflags +bitexact -flags:a +bitexact -syncpoints timestamped -strict experimental -f nut lame-split-delay.nut` ```fate-samples gapless/lame-split-delay.nut. ``` >From a8599f4d999b409a2230158fed87cc034a4d21fa Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Thu, 19 Nov 2026 08:15:58 -0600 Subject: [PATCH 1/2] tests/fate/gapless: add mp3 remux test with split encoder delay At 22050 Hz, libmp3lame spreads its 1105 samples of delay over the skip samples of the first two 576 sample packets. The reference records the current behavior: only the first share reaches the LAME header. --- tests/fate/gapless.mak | 5 +++++ tests/ref/fate/gapless-mp3-split-delay-remux | 3 +++ 2 files changed, 8 insertions(+) create mode 100644 tests/ref/fate/gapless-mp3-split-delay-remux diff --git a/tests/fate/gapless.mak b/tests/fate/gapless.mak index 708c98eb2e3..1c4a05006a6 100644 --- a/tests/fate/gapless.mak +++ b/tests/fate/gapless.mak @@ -15,6 +15,11 @@ fate-gapless-mp3-itunes: CMD = gapless $(TARGET_SAMPLES)/gapless/gapless-itunes. FATE_GAPLESSINFO_PROBE-$(call ALLYES, MP3_DEMUXER FILE_PROTOCOL) += fate-gapless-mp3-itunes-side-data fate-gapless-mp3-itunes-side-data: CMD = ffprobe_demux $(TARGET_SAMPLES)/gapless/gapless-itunes.mp3 -show_entries packet=pts:packet_side_data:stream=codec_name,start_pts,start_time,duration_ts,duration:format=start_time,duration +# libmp3lame at 22050 Hz spreads its 1105 samples of delay over the first two +# 576 sample packets. +FATE_GAPLESSENC_PROBE-$(call ALLYES, NUT_DEMUXER MP3_MUXER MP3_DEMUXER NULL_MUXER) += fate-gapless-mp3-split-delay-remux +fate-gapless-mp3-split-delay-remux: CMD = transcode nut $(TARGET_SAMPLES)/gapless/lame-split-delay.nut mp3 "-c copy" "-c copy" "-of compact -show_entries stream=start_pts,duration_ts" "" "" "" null + FATE_GAPLESS-$(call DEMDEC, MP3, MP3, ARESAMPLE_FILTER WAV_MUXER) += fate-audiomatch-square-mp3 fate-audiomatch-square-mp3: CMD = audio_match $(TARGET_SAMPLES)/audiomatch/square3.mp3 $(SAMPLES)/audiomatch/square3.wav diff --git a/tests/ref/fate/gapless-mp3-split-delay-remux b/tests/ref/fate/gapless-mp3-split-delay-remux new file mode 100644 index 00000000000..35fcf92dfb4 --- /dev/null +++ b/tests/ref/fate/gapless-mp3-split-delay-remux @@ -0,0 +1,3 @@ +36dc8ba5708648784f951045e9b76c4c *tests/data/fate/gapless-mp3-split-delay-remux.mp3 +725 tests/data/fate/gapless-mp3-split-delay-remux.mp3 +stream|start_pts=368640|duration_ts=3160960 -- 2.54.1 >From 96ceb30b948bad4c56e776c5d0f2f77ee831d419 Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Thu, 19 Nov 2026 08:16:06 -0600 Subject: [PATCH 2/2] avformat/mp3enc: sum encoder delay spread over several packets Since c31a781412 the audio frame queue spreads the encoder delay over the skip samples of the leading packets. Only the first one was read, so a libmp3lame encode at 22050 Hz wrote a delay of 47 instead of 576. Sum them and subtract the decoder delay when writing the Xing header. --- libavformat/mp3enc.c | 14 ++++++++++---- tests/ref/fate/gapless-mp3-split-delay-remux | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c index 4ea54f72c3b..ba704d5f3c7 100644 --- a/libavformat/mp3enc.c +++ b/libavformat/mp3enc.c @@ -129,7 +129,8 @@ typedef struct MP3Context { MPADecodeHeader2 *hdr; MPADecodeHeader2 *xing_header; MPADecodeHeader2 *first_frame; - int delay; + int64_t delay; + int delay_done; int64_t padding; /* index of the audio stream */ @@ -389,12 +390,16 @@ static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt) AV_PKT_DATA_SKIP_SAMPLES, &side_data_size); if (side_data && side_data_size >= 10) { + uint32_t skip_samples = AV_RL32(side_data); uint32_t discard_padding = AV_RL32(side_data + 4); - /* Padding longer than a frame is spread over several packets. */ + /* Delay and padding longer than a frame are spread over + * several packets. */ + if (!mp3->delay_done) + mp3->delay += skip_samples; + mp3->delay_done |= !skip_samples; mp3->padding = discard_padding ? mp3->padding + discard_padding : 0; - if (!mp3->delay) - mp3->delay = FFMAX((int64_t)AV_RL32(side_data) - 528 - 1, 0); } else { + mp3->delay_done = 1; mp3->padding = 0; } } @@ -509,6 +514,7 @@ static void mp3_update_xing(AVFormatContext *s) } /* write encoder delay/padding */ + mp3->delay = FFMAX(mp3->delay - 528 - 1, 0); if (mp3->padding) mp3->padding += 528 + 1; if (mp3->delay >= 1 << 12) { diff --git a/tests/ref/fate/gapless-mp3-split-delay-remux b/tests/ref/fate/gapless-mp3-split-delay-remux index 35fcf92dfb4..8400c99d4b3 100644 --- a/tests/ref/fate/gapless-mp3-split-delay-remux +++ b/tests/ref/fate/gapless-mp3-split-delay-remux @@ -1,3 +1,3 @@ -36dc8ba5708648784f951045e9b76c4c *tests/data/fate/gapless-mp3-split-delay-remux.mp3 +3765088ef73f39b647914c10d2ede355 *tests/data/fate/gapless-mp3-split-delay-remux.mp3 725 tests/data/fate/gapless-mp3-split-delay-remux.mp3 -stream|start_pts=368640|duration_ts=3160960 +stream|start_pts=707200|duration_ts=2822400 -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
