PR #26305 opened by joszamama
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26305
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26305.patch

# Summary of changes

For WAVE_FORMAT_EXTENSIBLE, `parse_waveformatex()` replaces 
`bits_per_coded_sample` (wBitsPerSample, the container size) with 
wValidBitsPerSample, and the PCM codec is then chosen from the valid bits. When 
the valid bits are fewer than the container bits, samples are read with the 
wrong size: 24 valid bits in a 32-bit container become `pcm_f24le` (integer PCM 
decoded as float, through the check for Audition "24.0 float" files in 
`wavdec.c`), and 20 valid bits in a 32-bit container become `pcm_s24le` with 
3-byte samples and a wrong sample count.

With this change the container size is kept when the valid bits are fewer, and 
the valid bits are exported as `bits_per_raw_sample`. Files whose valid bits 
equal the container size, and plain WAVE_FORMAT_PCM files, are not affected.

Fixes #26304

**Testing**

- Two FATE tests, `fate-wav-pcm-s32le-valid-bits-24` and `-20`, write a 
pcm_s32le WAV, set wValidBitsPerSample to 24 or 20 with 
`run_with_patched_temp`, and probe the stream. Both fail without the change and 
pass with it.
- Full `make fate` with the sample suite on macOS arm64 (configure 
`--disable-autodetect`): 5995 tests pass on master 329157f, and 5997 pass with 
the patch.
- Rechecked on master 4dfdbc9: the patch applies cleanly, and the two new tests 
pass with it (the 24-bit one fails without it: `pcm_f24le`/`flt` instead of 
`pcm_s32le`/`s32`).
- The three 76 to 80 byte files from the issue (24, 20 and 32 valid bits in 32) 
are now all `pcm_s32le` with 2, 2 and 3 frames and decode to the stored values.
- No new FATE samples are needed: the tests generate their input from 
`tests/data/asynth-44100-2.wav`.


>From d7b36ca7ca441a45b537cb4fc1262dd9fe514393 Mon Sep 17 00:00:00 2001
From: joszamama <[email protected]>
Date: Mon, 23 Nov 2026 19:20:47 +0000
Subject: [PATCH] avformat/riffdec: keep the container size for
 WAVE_FORMAT_EXTENSIBLE PCM

wValidBitsPerSample replaced wBitsPerSample as bits_per_coded_sample, so
the PCM codec was chosen from the valid bits. A 32-bit container with 24
valid bits was decoded as pcm_f24le, and one with 20 valid bits as
pcm_s24le with 3-byte samples and a wrong sample count.

Keep wBitsPerSample when the valid bits are fewer, and export them as
bits_per_raw_sample.

Fixes #26304
---
 libavformat/riffdec.c                      |  6 ++++--
 tests/fate/wav.mak                         | 25 +++++++++++++++++++---
 tests/ref/fate/wav-pcm-s32le-valid-bits-20 |  6 ++++++
 tests/ref/fate/wav-pcm-s32le-valid-bits-24 |  6 ++++++
 4 files changed, 38 insertions(+), 5 deletions(-)
 create mode 100644 tests/ref/fate/wav-pcm-s32le-valid-bits-20
 create mode 100644 tests/ref/fate/wav-pcm-s32le-valid-bits-24

diff --git a/libavformat/riffdec.c b/libavformat/riffdec.c
index 229ffdc4567..c360ea2b471 100644
--- a/libavformat/riffdec.c
+++ b/libavformat/riffdec.c
@@ -65,8 +65,10 @@ static void parse_waveformatex(void *logctx, AVIOContext 
*pb, AVCodecParameters
     int bps;
     uint64_t mask;
 
-    bps = big_endian ? avio_rb16(pb) :  avio_rl16(pb);
-    if (bps)
+    bps = big_endian ? avio_rb16(pb) :  avio_rl16(pb); /* wValidBitsPerSample 
*/
+    if (bps && bps < par->bits_per_coded_sample)
+        par->bits_per_raw_sample = bps; /* keep the container size */
+    else if (bps)
         par->bits_per_coded_sample = bps;
 
     mask = big_endian ? avio_rb32(pb) : avio_rl32(pb); /* dwChannelMask */
diff --git a/tests/fate/wav.mak b/tests/fate/wav.mak
index e2167edc85e..267acf4ef72 100644
--- a/tests/fate/wav.mak
+++ b/tests/fate/wav.mak
@@ -105,7 +105,26 @@ 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"
 
-FATE-yes            += $(FATE_WAV_ENCINFO-yes)
-FATE_FFMPEG_FFPROBE += $(FATE_WAV_BITRATE-yes)
+# WAVE_FORMAT_EXTENSIBLE with wValidBitsPerSample below wBitsPerSample (32):
+# the codec must follow the container size, not the valid bits.
+WAV_VALID_BITS_PROBE = ffprobe$(PROGSSUF)$(EXESUF) -v 0 -bitexact \
+    -show_entries stream=codec_name,sample_fmt,bits_per_sample,duration_ts 
-print_format default=nk=1
 
-fate-wav: $(FATE_WAV_ENCINFO-yes) $(FATE_WAV_BITRATE-yes)
+FATE_WAV_VALID_BITS-$(call ALLYES, PCM_S16LE_DECODER PCM_S32LE_ENCODER 
PCM_S32LE_DECODER WAV_MUXER WAV_DEMUXER FILE_PROTOCOL SWRESAMPLE) += 
fate-wav-pcm-s32le-valid-bits-24
+fate-wav-pcm-s32le-valid-bits-24: tests/data/asynth-44100-2.wav
+fate-wav-pcm-s32le-valid-bits-24: CMD = run_with_patched_temp \
+    "$(FFMPEG) -nostdin -loglevel quiet \
+    -i $(TARGET_PATH)/tests/data/asynth-44100-2.wav -c:a pcm_s32le -fflags 
+bitexact -y" \
+    "$(WAV_VALID_BITS_PROBE)" wav 38 "\0030\0000"
+
+FATE_WAV_VALID_BITS-$(call ALLYES, PCM_S16LE_DECODER PCM_S32LE_ENCODER 
PCM_S32LE_DECODER WAV_MUXER WAV_DEMUXER FILE_PROTOCOL SWRESAMPLE) += 
fate-wav-pcm-s32le-valid-bits-20
+fate-wav-pcm-s32le-valid-bits-20: tests/data/asynth-44100-2.wav
+fate-wav-pcm-s32le-valid-bits-20: CMD = run_with_patched_temp \
+    "$(FFMPEG) -nostdin -loglevel quiet \
+    -i $(TARGET_PATH)/tests/data/asynth-44100-2.wav -c:a pcm_s32le -fflags 
+bitexact -y" \
+    "$(WAV_VALID_BITS_PROBE)" wav 38 "\0024\0000"
+
+FATE-yes            += $(FATE_WAV_ENCINFO-yes)
+FATE_FFMPEG_FFPROBE += $(FATE_WAV_BITRATE-yes) $(FATE_WAV_VALID_BITS-yes)
+
+fate-wav: $(FATE_WAV_ENCINFO-yes) $(FATE_WAV_BITRATE-yes) 
$(FATE_WAV_VALID_BITS-yes)
diff --git a/tests/ref/fate/wav-pcm-s32le-valid-bits-20 
b/tests/ref/fate/wav-pcm-s32le-valid-bits-20
new file mode 100644
index 00000000000..b98124dfcf2
--- /dev/null
+++ b/tests/ref/fate/wav-pcm-s32le-valid-bits-20
@@ -0,0 +1,6 @@
+[STREAM]
+pcm_s32le
+s32
+32
+264600
+[/STREAM]
diff --git a/tests/ref/fate/wav-pcm-s32le-valid-bits-24 
b/tests/ref/fate/wav-pcm-s32le-valid-bits-24
new file mode 100644
index 00000000000..b98124dfcf2
--- /dev/null
+++ b/tests/ref/fate/wav-pcm-s32le-valid-bits-24
@@ -0,0 +1,6 @@
+[STREAM]
+pcm_s32le
+s32
+32
+264600
+[/STREAM]
-- 
2.54.1

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

Reply via email to