PR #26209 opened by Romain Beauxis (toots) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26209 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26209.patch
## Background The trim of an MP3 stream is the number of decoded samples to drop at its start and end. The decoder adds a delay of 528 + 1 samples of its own (LAME's `libmp3lame/encoder.h`). The LAME tag stores the encoder delay and padding without the decoder delay. iTunSMPB also stores its priming and remainder without the decoder delay. Core Audio (`afconvert -f WAVE -d LEI16`) decodes `gapless/gapless-itunes.mp3` (priming 528, remainder 2154) to the 418950 samples of its iTunSMPB sample count. That output starts 528 + 529 samples into the untrimmed decode and ends 2154 - 529 samples before its end. shine's encoder delay is 528 samples, so its decoded output lags its input by 1057, as measured by cross-correlating a decode against its input. ## Current behaviour - mp3dec applies the iTunSMPB priming and remainder without the decoder delay. iTunes files start 529 samples early and lose their last 529 samples. - libshine declares no encoder delay, so its output is trimmed by 529 samples instead of 1057. ## Changes - mp3dec adds the decoder delay to the iTunSMPB priming and to the start of the end discard, as it already does for the LAME tag. - The decoder delay becomes a named constant, `MP3_DECODER_DELAY`, used by libmp3lame, mp3dec and mp3enc. This is cosmetic only. - libshine declares an encoder delay of 528 samples. With this series, FFmpeg's decode of `gapless/gapless-itunes.mp3` matches Core Audio's over its whole length, to within 1 LSB. ## Tests The tests come first and record the current behaviour, so that each fix shows in its reference changes. - `fate-gapless-mp3-itunes-remux` stream-copies the iTunes sample to MP3 and probes the result. The start goes from 529 to 1057 samples, and the duration from 418949 to 418950, the iTunSMPB sample count. The existing `gapless-mp3-itunes*` and `preroll-mp3-itunes*` references change the same way: the skip goes from 528 to 1057, and the end discard from 1002 to 473. - `fate-gaplessenc-pcm-to-mp3-libshine` encodes `$(AREF)` with libshine and probes the start only, since the encoded data and the end of the stream depend on the shine version. The start goes from 529 to 1057 samples. The test only runs where libshine is enabled. The iTunSMPB fix applies to release/9.1 as well, which has the parsing commit a1d17fdd28. ## Sources - iTunSMPB: Core Audio's decode of `fate-suite/gapless/gapless-itunes.mp3` with `afconvert`, compared with FFmpeg's. - Decoder delay: LAME's `libmp3lame/encoder.h`, https://sourceforge.net/p/lame/svn/HEAD/tree/trunk/lame/libmp3lame/encoder.h >From 42cea76fa373cf6bbdc906aa7ab7c91d7203d07c Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Thu, 19 Nov 2026 22:09:44 -0600 Subject: [PATCH 1/5] tests/fate/gapless: add iTunes mp3 remux test The reference records the current behavior: the remuxed file starts 529 samples into the decoded output, while Core Audio starts the original at 1057. --- tests/fate/gapless.mak | 5 +++++ tests/ref/fate/gapless-mp3-itunes-remux | 3 +++ 2 files changed, 8 insertions(+) create mode 100644 tests/ref/fate/gapless-mp3-itunes-remux diff --git a/tests/fate/gapless.mak b/tests/fate/gapless.mak index 708c98eb2e3..bdeed916a26 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 +# The iTunSMPB priming and padding of this sample have to survive a remux, +# which stores them in the LAME tag. +FATE_GAPLESSENC_PROBE-$(call ALLYES, MP3_DEMUXER MP3_MUXER NULL_MUXER) += fate-gapless-mp3-itunes-remux +fate-gapless-mp3-itunes-remux: CMD = transcode mp3 $(TARGET_SAMPLES)/gapless/gapless-itunes.mp3 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-itunes-remux b/tests/ref/fate/gapless-mp3-itunes-remux new file mode 100644 index 00000000000..ed52801ef6d --- /dev/null +++ b/tests/ref/fate/gapless-mp3-itunes-remux @@ -0,0 +1,3 @@ +4e886ae5b38188d171a61c19deabac0a *tests/data/fate/gapless-mp3-itunes-remux.mp3 +192081 tests/data/fate/gapless-mp3-itunes-remux.mp3 +stream|start_pts=169280|duration_ts=134063680 -- 2.54.1 >From 12d6fe1ee54df24f34143ad70c41d8a699176d2e Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Thu, 19 Nov 2026 22:10:34 -0600 Subject: [PATCH 2/5] tests/fate/gapless: add libshine mp3 trim test The reference records the current behavior: libshine declares no encoder delay, so its 1057 samples of delay read back as 529. --- tests/fate/gapless.mak | 6 ++++++ tests/ref/fate/gaplessenc-pcm-to-mp3-libshine | 1 + 2 files changed, 7 insertions(+) create mode 100644 tests/ref/fate/gaplessenc-pcm-to-mp3-libshine diff --git a/tests/fate/gapless.mak b/tests/fate/gapless.mak index bdeed916a26..7efd67cb136 100644 --- a/tests/fate/gapless.mak +++ b/tests/fate/gapless.mak @@ -20,6 +20,12 @@ fate-gapless-mp3-itunes-side-data: CMD = ffprobe_demux $(TARGET_SAMPLES)/gapless FATE_GAPLESSENC_PROBE-$(call ALLYES, MP3_DEMUXER MP3_MUXER NULL_MUXER) += fate-gapless-mp3-itunes-remux fate-gapless-mp3-itunes-remux: CMD = transcode mp3 $(TARGET_SAMPLES)/gapless/gapless-itunes.mp3 mp3 "-c copy" "-c copy" "-of compact -show_entries stream=start_pts,duration_ts" "" "" "" null +# Only the start is checked, as the encoded data and the end of the stream +# depend on the shine version. +FATE_GAPLESSENC_PROBE-$(call ALLYES, LIBSHINE_ENCODER MP3_MUXER MP3_DEMUXER PCM_S16LE_DEMUXER PCM_S16LE_DECODER ARESAMPLE_FILTER) += fate-gaplessenc-pcm-to-mp3-libshine +fate-gaplessenc-pcm-to-mp3-libshine: $(AREF) +fate-gaplessenc-pcm-to-mp3-libshine: CMD = enc_external $(AREF) mp3 "-af aresample -c:a libshine" "-of compact -show_entries stream=start_pts" + 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/gaplessenc-pcm-to-mp3-libshine b/tests/ref/fate/gaplessenc-pcm-to-mp3-libshine new file mode 100644 index 00000000000..c6273b99451 --- /dev/null +++ b/tests/ref/fate/gaplessenc-pcm-to-mp3-libshine @@ -0,0 +1 @@ +stream|start_pts=169280 -- 2.54.1 >From 3d89ff5351027cea61d403862044a684778b1733 Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Thu, 19 Nov 2026 22:12:31 -0600 Subject: [PATCH 3/5] avformat/mp3dec: add the decoder delay to the iTunSMPB priming Like the LAME tag, iTunSMPB stores the encoder delay and padding without the 528 + 1 samples of decoder delay. Core Audio decodes gapless/gapless-itunes.mp3 (priming 528) to the 418950 samples of its sample count starting 1057 samples in, and FFmpeg's output now matches Core Audio's to 1 LSB over the whole file. It used to start 529 samples early and cut the last 529. --- libavformat/mp3dec.c | 8 +++++--- tests/ref/fate/gapless-mp3-itunes | 10 +++++----- tests/ref/fate/gapless-mp3-itunes-remux | 4 ++-- tests/ref/fate/gapless-mp3-itunes-side-data | 8 ++++---- tests/ref/fate/preroll-mp3-itunes-declared | 2 +- tests/ref/fate/preroll-mp3-itunes-decode | 13 ++++++------- tests/ref/fate/preroll-mp3-itunes-decode-noaccurate | 12 ++++++------ 7 files changed, 29 insertions(+), 28 deletions(-) diff --git a/libavformat/mp3dec.c b/libavformat/mp3dec.c index 12d0811ba45..f7f3991087f 100644 --- a/libavformat/mp3dec.c +++ b/libavformat/mp3dec.c @@ -371,9 +371,11 @@ static void mp3_parse_itunes_smpb(AVFormatContext *s, AVStream *st, priming + samples + remainder != mp3->frames * (int64_t)spf) return; - /* Contrary to the LAME tag, the priming count covers the decoder delay. */ - sti->start_skip_samples = priming; - sti->first_discard_sample = priming + samples; + /* As in the LAME tag, priming and remainder exclude the 528 + 1 samples of + * decoder delay: Core Audio (afconvert) outputs the sample count starting + * that far past the priming. */ + sti->start_skip_samples = priming + 528 + 1; + sti->first_discard_sample = priming + 528 + 1 + samples; sti->last_discard_sample = priming + samples + remainder; st->duration = av_rescale_q(samples, (AVRational){ 1, c->sample_rate }, diff --git a/tests/ref/fate/gapless-mp3-itunes b/tests/ref/fate/gapless-mp3-itunes index 5f768064fe1..7b54f83e7d1 100644 --- a/tests/ref/fate/gapless-mp3-itunes +++ b/tests/ref/fate/gapless-mp3-itunes @@ -1,5 +1,5 @@ -defc98cd62163e01f501b6221158af47 *tests/data/fate/gapless-mp3-itunes.out-1 -81cf1a5d04e513c4f17fffa6c0427665 -b9c05e4d68c1b435611e798aff2aeac0 *tests/data/fate/gapless-mp3-itunes.out-2 -81cf1a5d04e513c4f17fffa6c0427665 -749b5b5b9ff0cc42e6a776639c2c9675 *tests/data/fate/gapless-mp3-itunes.out-3 +8408f2c327bb3638dec1c4960b415325 *tests/data/fate/gapless-mp3-itunes.out-1 +b9457926804b147dfac408c7c378fe2c +9441edbe1d4bd5daff13e86bfd880f0a *tests/data/fate/gapless-mp3-itunes.out-2 +b9457926804b147dfac408c7c378fe2c +54fd7cf57d333c5fe76f71896e38f748 *tests/data/fate/gapless-mp3-itunes.out-3 diff --git a/tests/ref/fate/gapless-mp3-itunes-remux b/tests/ref/fate/gapless-mp3-itunes-remux index ed52801ef6d..e2cac0192bb 100644 --- a/tests/ref/fate/gapless-mp3-itunes-remux +++ b/tests/ref/fate/gapless-mp3-itunes-remux @@ -1,3 +1,3 @@ -4e886ae5b38188d171a61c19deabac0a *tests/data/fate/gapless-mp3-itunes-remux.mp3 +ef52f48db87bab99e0151f7f6f18de50 *tests/data/fate/gapless-mp3-itunes-remux.mp3 192081 tests/data/fate/gapless-mp3-itunes-remux.mp3 -stream|start_pts=169280|duration_ts=134063680 +stream|start_pts=338240|duration_ts=134064000 diff --git a/tests/ref/fate/gapless-mp3-itunes-side-data b/tests/ref/fate/gapless-mp3-itunes-side-data index b76dc567cd4..958ec2048d8 100644 --- a/tests/ref/fate/gapless-mp3-itunes-side-data +++ b/tests/ref/fate/gapless-mp3-itunes-side-data @@ -1,4 +1,4 @@ -packet|pts=0|side_datum/skip_samples:side_data_type=Skip Samples|side_datum/skip_samples:skip_samples=528|side_datum/skip_samples:discard_padding=0|side_datum/skip_samples:skip_reason=0|side_datum/skip_samples:discard_reason=0 +packet|pts=0|side_datum/skip_samples:side_data_type=Skip Samples|side_datum/skip_samples:skip_samples=1057|side_datum/skip_samples:discard_padding=0|side_datum/skip_samples:skip_reason=0|side_datum/skip_samples:discard_reason=0 packet|pts=368640 packet|pts=737280 packet|pts=1105920 @@ -362,7 +362,7 @@ packet|pts=132710400 packet|pts=133079040 packet|pts=133447680 packet|pts=133816320 -packet|pts=134184960|side_datum/skip_samples:side_data_type=Skip Samples|side_datum/skip_samples:skip_samples=0|side_datum/skip_samples:discard_padding=1002|side_datum/skip_samples:skip_reason=0|side_datum/skip_samples:discard_reason=0 +packet|pts=134184960|side_datum/skip_samples:side_data_type=Skip Samples|side_datum/skip_samples:skip_samples=0|side_datum/skip_samples:discard_padding=473|side_datum/skip_samples:skip_reason=0|side_datum/skip_samples:discard_reason=0 packet|pts=134553600|side_datum/skip_samples:side_data_type=Skip Samples|side_datum/skip_samples:skip_samples=0|side_datum/skip_samples:discard_padding=1152|side_datum/skip_samples:skip_reason=0|side_datum/skip_samples:discard_reason=0 -stream|codec_name=mp3|start_pts=168960|start_time=0.011973|duration_ts=134064000|duration=9.500000|disposition:default=0|disposition:dub=0|disposition:original=0|disposition:comment=0|disposition:lyrics=0|disposition:karaoke=0|disposition:forced=0|disposition:hearing_impaired=0|disposition:visual_impaired=0|disposition:clean_effects=0|disposition:attached_pic=0|disposition:timed_thumbnails=0|disposition:non_diegetic=0|disposition:captions=0|disposition:descriptions=0|disposition:metadata=0|disposition:dependent=0|disposition:still_image=0|disposition:multilayer=0 -format|start_time=0.011973|duration=9.500000|tag:title=7rk|tag:comment-iTunPGAP-eng=0|tag:encoded_by=iTunes 12.7.0.166|tag:comment-iTunNORM-eng= 00000362 000004C0 0000308F 00003CC5 00000DAC 00000DAC 00007D14 00007AC9 000007C1 0000175E|tag:comment-iTunSMPB-eng= 00000000 00000210 0000086A 0000000000066486 00000000 0002DA9D 00000000 00000000 00000000 00000000 00000000 00000000 +stream|codec_name=mp3|start_pts=338240|start_time=0.023968|duration_ts=134064000|duration=9.500000|disposition:default=0|disposition:dub=0|disposition:original=0|disposition:comment=0|disposition:lyrics=0|disposition:karaoke=0|disposition:forced=0|disposition:hearing_impaired=0|disposition:visual_impaired=0|disposition:clean_effects=0|disposition:attached_pic=0|disposition:timed_thumbnails=0|disposition:non_diegetic=0|disposition:captions=0|disposition:descriptions=0|disposition:metadata=0|disposition:dependent=0|disposition:still_image=0|disposition:multilayer=0 +format|start_time=0.023968|duration=9.500000|tag:title=7rk|tag:comment-iTunPGAP-eng=0|tag:encoded_by=iTunes 12.7.0.166|tag:comment-iTunNORM-eng= 00000362 000004C0 0000308F 00003CC5 00000DAC 00000DAC 00007D14 00007AC9 000007C1 0000175E|tag:comment-iTunSMPB-eng= 00000000 00000210 0000086A 0000000000066486 00000000 0002DA9D 00000000 00000000 00000000 00000000 00000000 00000000 diff --git a/tests/ref/fate/preroll-mp3-itunes-declared b/tests/ref/fate/preroll-mp3-itunes-declared index e111f6c9fb9..4093c04fe03 100644 --- a/tests/ref/fate/preroll-mp3-itunes-declared +++ b/tests/ref/fate/preroll-mp3-itunes-declared @@ -1 +1 @@ -stream|codec_name=mp3|sample_rate=44100|initial_padding=528|seek_preroll=0 +stream|codec_name=mp3|sample_rate=44100|initial_padding=1057|seek_preroll=0 diff --git a/tests/ref/fate/preroll-mp3-itunes-decode b/tests/ref/fate/preroll-mp3-itunes-decode index 7c29cc513da..875ed2cd2a7 100644 --- a/tests/ref/fate/preroll-mp3-itunes-decode +++ b/tests/ref/fate/preroll-mp3-itunes-decode @@ -3,10 +3,9 @@ #codec_id 0: pcm_s16le #sample_rate 0: 44100 #channel_layout_name 0: stereo -0, 0, 0, 291, 1164, 0x00000000 -0, 291, 291, 1152, 4608, 0x00000000 -0, 1443, 1443, 1152, 4608, 0x00000000 -0, 2595, 2595, 1152, 4608, 0x00000000 -0, 3747, 3747, 1152, 4608, 0x00000000 -0, 4899, 4899, 1152, 4608, 0x00000000 -0, 6051, 6051, 564, 2256, 0x00000000 +0, 0, 0, 914, 3656, 0x00000000 +0, 914, 914, 1152, 4608, 0x00000000 +0, 2066, 2066, 1152, 4608, 0x00000000 +0, 3218, 3218, 1152, 4608, 0x00000000 +0, 4370, 4370, 1152, 4608, 0x00000000 +0, 5522, 5522, 1093, 4372, 0x00000000 diff --git a/tests/ref/fate/preroll-mp3-itunes-decode-noaccurate b/tests/ref/fate/preroll-mp3-itunes-decode-noaccurate index 960cd66525b..e9207503bd0 100644 --- a/tests/ref/fate/preroll-mp3-itunes-decode-noaccurate +++ b/tests/ref/fate/preroll-mp3-itunes-decode-noaccurate @@ -3,9 +3,9 @@ #codec_id 0: pcm_s16le #sample_rate 0: 44100 #channel_layout_name 0: stereo -0, -861, -861, 1152, 4608, 0x00000000 -0, 291, 291, 1152, 4608, 0x00000000 -0, 1443, 1443, 1152, 4608, 0x00000000 -0, 2595, 2595, 1152, 4608, 0x00000000 -0, 3747, 3747, 1152, 4608, 0x00000000 -0, 4899, 4899, 855, 3420, 0x00000000 +0, -238, -238, 1152, 4608, 0x00000000 +0, 914, 914, 1152, 4608, 0x00000000 +0, 2066, 2066, 1152, 4608, 0x00000000 +0, 3218, 3218, 1152, 4608, 0x00000000 +0, 4370, 4370, 1152, 4608, 0x00000000 +0, 5522, 5522, 855, 3420, 0x00000000 -- 2.54.1 >From 7c10407235358ede4127c4d933e2816e617810f3 Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Thu, 19 Nov 2026 22:13:34 -0600 Subject: [PATCH 4/5] avcodec/mpegaudio: name the layer III decoder delay The LAME tag and iTunSMPB conversions each spelled it out as 528 + 1. --- libavcodec/libmp3lame.c | 2 +- libavcodec/mpegaudio.h | 6 ++++++ libavformat/mp3dec.c | 14 +++++++------- libavformat/mp3enc.c | 4 ++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c index b84cfa8b4d8..defa2c8bafb 100644 --- a/libavcodec/libmp3lame.c +++ b/libavcodec/libmp3lame.c @@ -157,7 +157,7 @@ static av_cold int mp3lame_encode_init(AVCodecContext *avctx) } /* get encoder delay */ - avctx->initial_padding = lame_get_encoder_delay(s->gfp) + 528 + 1; + avctx->initial_padding = lame_get_encoder_delay(s->gfp) + MP3_DECODER_DELAY; ff_af_queue_init(avctx, &s->afq); avctx->frame_size = lame_get_framesize(s->gfp); diff --git a/libavcodec/mpegaudio.h b/libavcodec/mpegaudio.h index 74590a8e8b7..cd9fa4809a3 100644 --- a/libavcodec/mpegaudio.h +++ b/libavcodec/mpegaudio.h @@ -39,6 +39,12 @@ /* max compressed frame size */ #define MPA_MAX_CODED_FRAME_SIZE 1792 +/** + * Delay of the layer III synthesis filterbank, in samples. The LAME tag and + * iTunSMPB store the encoder delay and padding without it. + */ +#define MP3_DECODER_DELAY (528 + 1) + #define MPA_MAX_CHANNELS 2 #define SBLIMIT 32 /* number of subbands */ diff --git a/libavformat/mp3dec.c b/libavformat/mp3dec.c index f7f3991087f..7ab6353f95a 100644 --- a/libavformat/mp3dec.c +++ b/libavformat/mp3dec.c @@ -264,9 +264,9 @@ static void mp3_parse_info_tag(AVFormatContext *s, AVStream *st, mp3->start_pad = v>>12; mp3-> end_pad = v&4095; - sti->start_skip_samples = mp3->start_pad + 528 + 1; + sti->start_skip_samples = mp3->start_pad + MP3_DECODER_DELAY; if (mp3->frames) { - sti->first_discard_sample = -mp3->end_pad + 528 + 1 + mp3->frames * (int64_t)spf; + sti->first_discard_sample = -mp3->end_pad + MP3_DECODER_DELAY + mp3->frames * (int64_t)spf; sti->last_discard_sample = mp3->frames * (int64_t)spf; } av_log(s, AV_LOG_DEBUG, "pad %d %d\n", mp3->start_pad, mp3-> end_pad); @@ -371,11 +371,11 @@ static void mp3_parse_itunes_smpb(AVFormatContext *s, AVStream *st, priming + samples + remainder != mp3->frames * (int64_t)spf) return; - /* As in the LAME tag, priming and remainder exclude the 528 + 1 samples of - * decoder delay: Core Audio (afconvert) outputs the sample count starting - * that far past the priming. */ - sti->start_skip_samples = priming + 528 + 1; - sti->first_discard_sample = priming + 528 + 1 + samples; + /* As in the LAME tag, priming and remainder exclude the decoder delay: + * Core Audio (afconvert) outputs the sample count starting that far past + * the priming. */ + sti->start_skip_samples = priming + MP3_DECODER_DELAY; + sti->first_discard_sample = priming + MP3_DECODER_DELAY + samples; sti->last_discard_sample = priming + samples + remainder; st->duration = av_rescale_q(samples, (AVRational){ 1, c->sample_rate }, diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c index 4ea54f72c3b..6a60e325c4b 100644 --- a/libavformat/mp3enc.c +++ b/libavformat/mp3enc.c @@ -393,7 +393,7 @@ static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt) /* Padding longer than a frame is spread over several packets. */ mp3->padding = discard_padding ? mp3->padding + discard_padding : 0; if (!mp3->delay) - mp3->delay = FFMAX((int64_t)AV_RL32(side_data) - 528 - 1, 0); + mp3->delay = FFMAX((int64_t)AV_RL32(side_data) - MP3_DECODER_DELAY, 0); } else { mp3->padding = 0; } @@ -510,7 +510,7 @@ static void mp3_update_xing(AVFormatContext *s) /* write encoder delay/padding */ if (mp3->padding) - mp3->padding += 528 + 1; + mp3->padding += MP3_DECODER_DELAY; if (mp3->delay >= 1 << 12) { mp3->delay = (1 << 12) - 1; av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n"); -- 2.54.1 >From dfbbb3ece7d460cfa590f610f184f10e124f88f2 Mon Sep 17 00:00:00 2001 From: Romain Beauxis <[email protected]> Date: Thu, 19 Nov 2026 22:14:40 -0600 Subject: [PATCH 5/5] avcodec/libshine: declare the encoder delay shine starts from a zeroed filterbank and pads nothing, so its delay is that of its analysis filterbank: 528 samples, polyphase 240 and MDCT 288 as LAME's libmp3lame/encoder.h breaks it down. With the decoder's, the decoded output lags the input by 1057 samples, which matches the lag measured by cross-correlating a decode with its input, and which was reported as 529. --- libavcodec/libshine.c | 9 ++++++++- tests/ref/fate/gaplessenc-pcm-to-mp3-libshine | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/libavcodec/libshine.c b/libavcodec/libshine.c index e7edc5b03f1..efd5665f57e 100644 --- a/libavcodec/libshine.c +++ b/libavcodec/libshine.c @@ -32,6 +32,12 @@ #define BUFFER_SIZE (4096 * 20) +/** + * Delay of the analysis filterbank, polyphase (240) and MDCT (288), as broken + * down in LAME's libmp3lame/encoder.h. shine adds no padding of its own. + */ +#define SHINE_ENCODER_DELAY 528 + typedef struct SHINEContext { shine_config_t config; shine_t shine; @@ -57,7 +63,8 @@ static av_cold int libshine_encode_init(AVCodecContext *avctx) s->shine = shine_initialise(&s->config); if (!s->shine) return AVERROR(ENOMEM); - avctx->frame_size = shine_samples_per_pass(s->shine); + avctx->frame_size = shine_samples_per_pass(s->shine); + avctx->initial_padding = SHINE_ENCODER_DELAY + MP3_DECODER_DELAY; ff_af_queue_init(avctx, &s->afq); return 0; } diff --git a/tests/ref/fate/gaplessenc-pcm-to-mp3-libshine b/tests/ref/fate/gaplessenc-pcm-to-mp3-libshine index c6273b99451..4873f593b79 100644 --- a/tests/ref/fate/gaplessenc-pcm-to-mp3-libshine +++ b/tests/ref/fate/gaplessenc-pcm-to-mp3-libshine @@ -1 +1 @@ -stream|start_pts=169280 +stream|start_pts=338240 -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
