PR #26095 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26095
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26095.patch

Copying Microsoft ADPCM or Microsoft IMA ADPCM from WAV to MOV fails with 
`input packet contains no samples` when the input does not set 
`codecpar->frame_size`. WAV demuxing can also group several compressed blocks 
into one packet, so using a single block's frame size undercounts that packet.

Derive the missing samples-per-block value from the codec and WAVE block 
alignment for the QuickTime version 1 sound description. Derive each packet's 
coded sample count from its byte size when writing the sample tables. This 
keeps the sound description and sample tables consistent with the compressed 
block structure and preserves the complete stream during remuxing.

The QuickTime sound description defines samples per packet and bytes per frame; 
the WAVE ADPCM format defines the block alignment and samples per block. A 
demuxer packet containing multiple blocks must contribute all of their decoded 
samples to the MOV sample tables.

Validation:
- Added generated WAV-to-MOV remux regressions for both codecs, each using five 
blocks so the input includes a multi-block packet and a final single-block 
packet.
- Both tests check the resulting MOV hash, complete decoded PCM checksums, 
codec parameters, and media duration (5060 samples for MS ADPCM; 5085 for IMA 
ADPCM).
- Both targeted FATE tests pass.
- Verified stereo 48 kHz remuxes preserve all compressed packets and decoded 
PCM: 238 MS ADPCM blocks / 240856 frames and 236 IMA ADPCM blocks / 240012 
frames.



>From ce37aaa048b868a4acfdfca53ccabd112f9393d8 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sun, 15 Nov 2026 23:42:20 -0800
Subject: [PATCH] avformat/movenc: derive MS ADPCM sample counts from block
 sizes

WAV streams need not set codecpar frame_size and may group several ADPCM blocks 
in one packet. Derive samples per block for the version 1 sound description and 
count all coded samples in each packet when constructing the sample tables.

This keeps the QuickTime sound description consistent with the WAVE block 
alignment and permits stream copying Microsoft ADPCM and IMA ADPCM from WAV to 
MOV. Add generated multi-block remux regressions for both codecs.
---
 libavformat/movenc.c                   |  5 ++++-
 tests/fate/mov.mak                     | 14 ++++++++++++++
 tests/ref/fate/mov-adpcm-ima-wav-remux | 18 ++++++++++++++++++
 tests/ref/fate/mov-adpcm-ms-remux      | 18 ++++++++++++++++++
 4 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 tests/ref/fate/mov-adpcm-ima-wav-remux
 create mode 100644 tests/ref/fate/mov-adpcm-ms-remux

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 27e1dd77188..967a0006c68 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -7217,7 +7217,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
         }
     } else if (par->codec_id == AV_CODEC_ID_ADPCM_MS ||
                par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
-        samples_in_chunk = trk->par->frame_size;
+        samples_in_chunk = av_get_audio_frame_duration2(par, size);
     } else if (trk->sample_size)
         samples_in_chunk = size / trk->sample_size;
     else
@@ -8805,6 +8805,9 @@ static int mov_init(AVFormatContext *s)
                     return AVERROR(EINVAL);
                 }
                 track->sample_size = st->codecpar->block_align;
+                if (!st->codecpar->frame_size)
+                    st->codecpar->frame_size =
+                        av_get_audio_frame_duration2(st->codecpar, 
st->codecpar->block_align);
             }else if (st->codecpar->frame_size > 1){ /* assume compressed 
audio */
                 track->audio_vbr = 1;
             }else{
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 4abecc02314..650f6d2bbf2 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -635,6 +635,20 @@ fate-mov-chpl-utf8: tests/data/chpl-utf8.ffmeta
 FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, COLOR_FILTER MPEG4_ENCODER 
FFMETADATA_DEMUXER TGP_MUXER MOV_DEMUXER FILE_PROTOCOL) += fate-mov-chpl-utf8
 fate-mov-chpl-utf8: CMD = run_with_temp "$(FFMPEG) -nostdin -v error 
-filter_complex color=c=black:s=2x2:r=1 -f ffmetadata -i 
$(TARGET_PATH)/tests/data/chpl-utf8.ffmeta -map_chapters 0 -frames:v 1 -c:v 
mpeg4 -threads 1 -flags +bitexact -fflags +bitexact -f 3gp -y" 
"ffprobe$(PROGSSUF)$(EXESUF) -loglevel error -bitexact -show_chapters" 3gp
 
+# WAV demuxing can group several ADPCM blocks in a packet and does not
+# populate codecpar->frame_size. Exercise both the sound description and
+# the sample table when copying those packets to MOV.
+tests/data/mov-adpcm_ms.wav tests/data/mov-adpcm_ima_wav.wav: TAG = GEN
+tests/data/mov-adpcm_ms.wav tests/data/mov-adpcm_ima_wav.wav: 
ffmpeg$(PROGSSUF)$(EXESUF) tests/data/asynth-44100-2.wav | tests/data
+       $(M)$(TARGET_EXEC) $(TARGET_PATH)/$< -nostdin -v error -fflags 
+bitexact -i $(TARGET_PATH)/tests/data/asynth-44100-2.wav -t 0.1 -c:a 
$(@:tests/data/mov-%.wav=%) -flags +bitexact -y $(TARGET_PATH)/$@
+
+FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, ADPCM_MS_ENCODER ADPCM_MS_DECODER 
PCM_S16LE_DECODER PCM_S16LE_ENCODER WAV_MUXER WAV_DEMUXER MOV_MUXER MOV_DEMUXER 
FRAMECRC_MUXER FILE_PROTOCOL PIPE_PROTOCOL) += fate-mov-adpcm-ms-remux
+FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, ADPCM_IMA_WAV_ENCODER 
ADPCM_IMA_WAV_DECODER PCM_S16LE_DECODER PCM_S16LE_ENCODER ARESAMPLE_FILTER 
WAV_MUXER WAV_DEMUXER MOV_MUXER MOV_DEMUXER FRAMECRC_MUXER FILE_PROTOCOL 
PIPE_PROTOCOL) += fate-mov-adpcm-ima-wav-remux
+fate-mov-adpcm-ms-remux: tests/data/mov-adpcm_ms.wav
+fate-mov-adpcm-ms-remux: CMD = transcode wav 
$(TARGET_PATH)/tests/data/mov-adpcm_ms.wav mov "-c:a copy" "-c:a pcm_s16le" 
"-show_entries stream=codec_name,sample_rate,channels,duration_ts"
+fate-mov-adpcm-ima-wav-remux: tests/data/mov-adpcm_ima_wav.wav
+fate-mov-adpcm-ima-wav-remux: CMD = transcode wav 
$(TARGET_PATH)/tests/data/mov-adpcm_ima_wav.wav mov "-c:a copy" "-af aresample 
-c:a pcm_s16le" "-show_entries 
stream=codec_name,sample_rate,channels,duration_ts"
+
 FATE_FFMPEG += $(FATE_MOV_FFMPEG-yes)
 FATE_FFMPEG_FFPROBE += $(FATE_MOV_FFMPEG_FFPROBE-yes)
 
diff --git a/tests/ref/fate/mov-adpcm-ima-wav-remux 
b/tests/ref/fate/mov-adpcm-ima-wav-remux
new file mode 100644
index 00000000000..21c52329076
--- /dev/null
+++ b/tests/ref/fate/mov-adpcm-ima-wav-remux
@@ -0,0 +1,18 @@
+f3c07da24649de920fdd30fe357f20be *tests/data/fate/mov-adpcm-ima-wav-remux.mov
+5821 tests/data/fate/mov-adpcm-ima-wav-remux.mov
+#tb 0: 1/44100
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 44100
+#channel_layout_name 0: stereo
+0,          0,          0,     1017,     4068, 0x7152d92f
+0,       1017,       1017,     1017,     4068, 0xd171d92b
+0,       2034,       2034,     1017,     4068, 0x0f32f797
+0,       3051,       3051,     1017,     4068, 0xd5bddacf
+0,       4068,       4068,     1017,     4068, 0x97ef1783
+[STREAM]
+codec_name=adpcm_ima_wav
+sample_rate=44100
+channels=2
+duration_ts=5085
+[/STREAM]
diff --git a/tests/ref/fate/mov-adpcm-ms-remux 
b/tests/ref/fate/mov-adpcm-ms-remux
new file mode 100644
index 00000000000..34bb3b9f8f0
--- /dev/null
+++ b/tests/ref/fate/mov-adpcm-ms-remux
@@ -0,0 +1,18 @@
+daed568724f1595495bc94d95d5eaa36 *tests/data/fate/mov-adpcm-ms-remux.mov
+5851 tests/data/fate/mov-adpcm-ms-remux.mov
+#tb 0: 1/44100
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 44100
+#channel_layout_name 0: stereo
+0,          0,          0,     1012,     4048, 0x2859017e
+0,       1012,       1012,     1012,     4048, 0x4524f707
+0,       2024,       2024,     1012,     4048, 0xedcee0dd
+0,       3036,       3036,     1012,     4048, 0x1ce0cf87
+0,       4048,       4048,     1012,     4048, 0x1411c3b9
+[STREAM]
+codec_name=adpcm_ms
+sample_rate=44100
+channels=2
+duration_ts=5060
+[/STREAM]
-- 
2.54.0

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

Reply via email to