PR #26234 opened by WyattBlue URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26234 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26234.patch
Since c31a781412 the audio frame queue writes each packet's skip as FFMIN(output_delay, removed_samples), so a start delay longer than one frame is spread over several packets. The muxer only read the first packet's value. At 24 kHz and below a frame is 576 samples while libmp3lame's initial_padding is 1105 (576 + 528 + 1). The first packet reports 576, and the Xing/LAME header got a delay of 47 instead of 576, leaving 529 samples of encoder priming in the decoded output. At 32 kHz and above a frame is 1152 samples, longer than the delay, so those rates were not affected. Sum the skip over the leading packets, as is already done for trailing padding since 23fccd657c, and work out the delay when the Xing header is updated. Only the leading run of packets counts now: a skip showing up later in the stream no longer sets the delay when the first one was too short to. # Summary of changes Briefly describe what this PR does and why. <!-- If this PR requires new FATE test samples, attach them to the PR and list their target paths below (relative to the fate-suite root). Attached filenames must match the sample's filename: ```fate-samples # e.g. vorbis/new-sample.ogg ``` --> >From e8d937a369eb0435bd097a660aa1ee27eca1ca79 Mon Sep 17 00:00:00 2001 From: WyattBlue <[email protected]> Date: Fri, 20 Nov 2026 10:39:52 -0500 Subject: [PATCH] avformat/mp3enc: sum the start delay spanning several packets Since c31a781412 the audio frame queue writes each packet's skip as FFMIN(output_delay, removed_samples), so a start delay longer than one frame is spread over several packets. The muxer only read the first packet's value. At 24 kHz and below a frame is 576 samples while libmp3lame's initial_padding is 1105 (576 + 528 + 1). The first packet reports 576, and the Xing/LAME header got a delay of 47 instead of 576, leaving 529 samples of encoder priming in the decoded output. At 32 kHz and above a frame is 1152 samples, longer than the delay, so those rates were not affected. Sum the skip over the leading packets, as is already done for trailing padding since 23fccd657c, and work out the delay when the Xing header is updated. Only the leading run of packets counts now: a skip showing up later in the stream no longer sets the delay when the first one was too short to. --- libavformat/mp3enc.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c index 4ea54f72c3b..89409b4f9ff 100644 --- a/libavformat/mp3enc.c +++ b/libavformat/mp3enc.c @@ -129,8 +129,9 @@ typedef struct MP3Context { MPADecodeHeader2 *hdr; MPADecodeHeader2 *xing_header; MPADecodeHeader2 *first_frame; - int delay; int64_t padding; + int64_t skip; /* start delay as the packets report it, summed */ + int skip_done; /* a packet without any start delay has been seen */ /* index of the audio stream */ int audio_stream_idx; @@ -389,13 +390,22 @@ 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. */ mp3->padding = discard_padding ? mp3->padding + discard_padding : 0; - if (!mp3->delay) - mp3->delay = FFMAX((int64_t)AV_RL32(side_data) - 528 - 1, 0); + /* A start delay longer than a frame is spread over several + * packets too, as LAME's is at 24 kHz and below, where a + * frame is 576 samples. */ + if (!mp3->skip_done) { + if (skip_samples) + mp3->skip += skip_samples; + else + mp3->skip_done = 1; + } } else { mp3->padding = 0; + mp3->skip_done = 1; } } } @@ -456,6 +466,7 @@ static void mp3_update_xing(AVFormatContext *s) uint16_t tag_crc; uint8_t *toc; int i; + int64_t delay; int64_t old_pos = avio_tell(s->pb); /* replace "Xing" identification string with "Info" for CBR files. */ @@ -509,17 +520,18 @@ static void mp3_update_xing(AVFormatContext *s) } /* write encoder delay/padding */ + delay = FFMAX(mp3->skip - 528 - 1, 0); if (mp3->padding) mp3->padding += 528 + 1; - if (mp3->delay >= 1 << 12) { - mp3->delay = (1 << 12) - 1; + if (delay >= 1 << 12) { + delay = (1 << 12) - 1; av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n"); } if (mp3->padding >= 1 << 12) { mp3->padding = (1 << 12) - 1; av_log(s, AV_LOG_WARNING, "Too many samples of trailing padding.\n"); } - AV_WB24(mp3->xing_frame + mp3->xing_offset + 141, (mp3->delay << 12) + mp3->padding); + AV_WB24(mp3->xing_frame + mp3->xing_offset + 141, (delay << 12) + mp3->padding); AV_WB32(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 8, mp3->audio_size); AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 4, mp3->audio_crc); -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
