PR #26388 opened by bonkbeats URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26388 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26388.patch
# Summary of changes The WAV demuxer throws away a correct `fact` sample count for MP3, MP2 and the other codecs stored with `wBitsPerSample = 0`, and falls back to estimating the duration from the bitrate. The sanity check in `wav_read_header()` rejects the sample count when `(data_size << 3) / sample_count / channels > bits_per_coded_sample + 1`. With `bits_per_coded_sample` 0 the bound is 1, so with integer division every file at 2 or more bits per sample is rejected: anything above ~88 kbps per channel at 44.1 kHz, including every 128 kbps MP3. riffenc writes `bps = 0` for these codecs itself (`riffenc.c`, ATRAC3 / G.723.1 / G.728 / MP2 / MP3 / GSM_MS), as does the MPEGLAYER3WAVEFORMAT spec. History: the check was added in 47c84c0bf7 with a bound of 8 bytes per sample (Ticket 3708), and 13d605e090 tightened it to `bits_per_coded_sample` (for Ticket 4577, G.729). This patch keeps the original 8 bytes per sample bound when `bits_per_coded_sample` is 0. Codecs with a known bits per sample, and the G.729 special case, are unchanged. I ran into this while stream-copying TTS output (128 kbps MP3) into WAV: every probe printed `ignoring wrong sample_count` and `Estimating duration from bitrate, this may be inaccurate`. ### Reproduction ``` ffmpeg -f lavfi -i "anoisesrc=r=44100:d=32:a=0.9:c=white" -f lavfi -i "sine=f=440:r=44100:d=8" \ -filter_complex "[1]volume=0.02[q];[0][q]concat=n=2:v=0:a=1" -ac 1 -c:a libmp3lame -q:a 0 vbr.wav ffprobe vbr.wav ``` ### Testing | File | fact chunk | Before | After | Decoded length | |---|---|---|---|---| | VBR MP3 (libmp3lame `-q:a 0`, 2.44 bits/sample) | 40.025 s | **33.589 s** + both warnings | 40.025 s | 40.04 s | | CBR 128k MP3, stream-copied | 1.071 s | 1.071 s + both warnings | 1.071 s, no warnings | 1.07 s | | MP2 384k | 10.008 s | 10.008 s + both warnings | 10.008 s, no warnings | 10.00 s | | MP2 384k, `nAvgBytesPerSec` halved (new FATE test) | 6.008 s | **12.016 s** | 6.008 s | 6.00 s | | VBR MP3 with fact patched to 1000 (garbage) | — | rejected | still rejected | — | | PCM, ADPCM MS, G.723.1, MP2 64k | — | unchanged | unchanged | — | "Before" is the same code in a release build (the check is unchanged since 2015). New test: `fate-wav-mp2-sample-count`. It encodes MP2 at 384k from `asynth-44100-2.wav`, halves `nAvgBytesPerSec`, and checks the duration. Needs no samples. Without the wavdec.c change the new test fails (`529920` / `12.016327` instead of `264960` / `6.008163`). FATE: `make fate` (without the sample suite) passes, 3287 tests, and `make fate-wav` passes with the new test (Windows 11 / MSYS2 UCRT64 / gcc 16.2). ### Notes - Sample-based tests with bps 0 codecs in WAV (ATRAC3, GSM_MS `gsm/ciao.wav`, TrueSpeech, Siren) haven't been run yet. By my arithmetic they're all below 2 bits per sample, so they shouldn't be affected. <!-- 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 22b65107413aa26ec89180497220838b51ba8c10 Mon Sep 17 00:00:00 2001 From: prateek rao <[email protected]> Date: Fri, 27 Nov 2026 06:12:14 +0530 Subject: [PATCH] avformat/wavdec: keep the sample count when bits_per_coded_sample is 0 The sample count sanity check rejects the fact chunk when the data works out to more than bits_per_coded_sample + 1 bits per sample. MP3, MP2 and a few other codecs are stored with wBitsPerSample 0, so any such file at 2 or more bits per sample, e.g. every 128 kbps MP3, loses its correct sample count and the duration falls back to an estimate from the bitrate. That estimate is wrong whenever the bitrate is: a 40.0 s VBR MP3 written to WAV by libmp3lame (-q:a 0) is reported as 33.6 s, and an MP2 file with a wrong nAvgBytesPerSec gets a duration off by the same factor. The check started with a bound of 8 bytes per sample in 47c84c0bf7 and was tightened to bits_per_coded_sample in 13d605e090. Keep the original bound when bits_per_coded_sample is 0, and add a test. Signed-off-by: prateek rao <[email protected]> --- libavformat/wavdec.c | 5 ++++- tests/fate/wav.mak | 11 +++++++++++ tests/ref/fate/wav-mp2-sample-count | 7 +++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/ref/fate/wav-mp2-sample-count diff --git a/libavformat/wavdec.c b/libavformat/wavdec.c index d1f061d65b8..0f70ce9d1f2 100644 --- a/libavformat/wavdec.c +++ b/libavformat/wavdec.c @@ -646,8 +646,11 @@ break_loop: sample_count /= st->codecpar->ch_layout.nb_channels; } + /* bits_per_coded_sample is 0 for compressed codecs like MP3, so only + * reject sample counts implying more than 8 bytes per sample for them */ if (data_size > 0 && sample_count && st->codecpar->ch_layout.nb_channels && - (data_size << 3) / sample_count / st->codecpar->ch_layout.nb_channels > st->codecpar->bits_per_coded_sample + 1) { + (data_size << 3) / sample_count / st->codecpar->ch_layout.nb_channels > + (st->codecpar->bits_per_coded_sample > 0 ? st->codecpar->bits_per_coded_sample + 1 : 64)) { av_log(s, AV_LOG_WARNING, "ignoring wrong sample_count %"PRId64"\n", sample_count); sample_count = 0; } diff --git a/tests/fate/wav.mak b/tests/fate/wav.mak index e2167edc85e..d3363526593 100644 --- a/tests/fate/wav.mak +++ b/tests/fate/wav.mak @@ -105,6 +105,17 @@ fate-wav-pcm-mulaw-bad-blockalign: CMD = run_with_patched_temp \ -i $(TARGET_PATH)/tests/data/asynth-44100-2.wav -c:a pcm_mulaw -fflags +bitexact -y" \ "$(WAV_BITRATE_PROBE)" wav 32 "\0001\0000" +# MP2 (stored with wBitsPerSample 0) with nAvgBytesPerSec halved: the duration +# must come from the fact chunk sample count, not from the byte rate. +FATE_WAV_BITRATE-$(call ALLYES, MP2_ENCODER PCM_S16LE_DECODER WAV_MUXER WAV_DEMUXER FILE_PROTOCOL) += fate-wav-mp2-sample-count +fate-wav-mp2-sample-count: tests/data/asynth-44100-2.wav +fate-wav-mp2-sample-count: CMD = run_with_patched_temp \ + "$(FFMPEG) -nostdin -loglevel quiet \ + -i $(TARGET_PATH)/tests/data/asynth-44100-2.wav -c:a mp2 -b:a 384k -fflags +bitexact -y" \ + "ffprobe$(PROGSSUF)$(EXESUF) -v 0 -bitexact \ + -show_entries stream=codec_name,duration_ts:format=duration -print_format default=nk=1" \ + wav 28 "\0300\0135\0000\0000" + FATE-yes += $(FATE_WAV_ENCINFO-yes) FATE_FFMPEG_FFPROBE += $(FATE_WAV_BITRATE-yes) diff --git a/tests/ref/fate/wav-mp2-sample-count b/tests/ref/fate/wav-mp2-sample-count new file mode 100644 index 00000000000..66a539d3121 --- /dev/null +++ b/tests/ref/fate/wav-mp2-sample-count @@ -0,0 +1,7 @@ +[STREAM] +mp2 +264960 +[/STREAM] +[FORMAT] +6.008163 +[/FORMAT] -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
