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

Three pre-existing MOV/CENC issues found while reviewing 
https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26095:

- movenc: in a shared moof, only the first track's CENC entries were flushed, 
so later tracks repeated stale IVs in senc.
- mov: encrypted audio kept the stsd depth because frma resolves the codec 
later; IMA WAV opened with 16 bits.
- movenc: a delay_moov empty moov counted buffered CBR audio in stts.

Each commit has a FATE test.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From 3c95d2995ec6bcb4126c813ca380f1571ce4cc08 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sun, 22 Nov 2026 22:34:38 -0800
Subject: [PATCH 1/3] avformat/movenc: drop CENC entries of every track in a
 shared moof

Only the first track was flushed, so later tracks repeated stale IVs in senc.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/movenc.c                  |  5 ++---
 tests/fate/mov.mak                    |  7 +++++++
 tests/ref/fate/mov-adpcm-ms-cenc-frag | 30 +++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 3 deletions(-)
 create mode 100644 tests/ref/fate/mov-adpcm-ms-cenc-frag

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index e0057dba3bf..c04670e1982 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -6949,6 +6949,8 @@ static int mov_finish_fragment(MOVMuxContext *mov, 
MOVTrack *track,
                track->cluster, track->entry * sizeof(*track->cluster));
         track->entry_written += track->entry;
     }
+    if (track->cenc.aes_ctr)
+        ff_mov_cenc_flush(&track->cenc);
     track->entry = 0;
     track->entries_flushed = 0;
     track->end_reliable = 0;
@@ -7138,9 +7140,6 @@ static int mov_flush_fragment(AVFormatContext *s, int 
force)
             mov_write_moof_tag(s->pb, mov, moof_tracks, mdat_size);
             mov->fragments++;
 
-            if (track->cenc.aes_ctr)
-                ff_mov_cenc_flush(&track->cenc);
-
             avio_wb32(s->pb, mdat_size + 8);
             ffio_wfourcc(s->pb, "mdat");
             mdat_start = avio_tell(s->pb);
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 3af99182794..a90f54d8ee6 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -269,6 +269,13 @@ $(eval $(call 
FATE_MOV_CENC_TEST,h264_iso,H264,matroska,MATROSKA,h264/dts_5frame
 $(eval $(call 
FATE_MOV_CENC_TEST,hevc,HEVC,hevc,HEVC,hevc-conformance/ipcm_A_NEC_3.bit))
 $(eval $(call 
FATE_MOV_CENC_TEST,hevc_iso,HEVC,mov,MOV,hevc/extradata-reload-multi-stsd.mov))
 
+# Two encrypted tracks sharing each moof.
+FATE_MOV_FFMPEG-$(call ALLYES, ADPCM_MS_ENCODER ADPCM_MS_DECODER 
PCM_S16LE_DECODER PCM_S16LE_ENCODER WAV_DEMUXER MOV_MUXER MOV_DEMUXER 
FRAMECRC_MUXER FILE_PROTOCOL PIPE_PROTOCOL) += fate-mov-adpcm-ms-cenc-frag
+fate-mov-adpcm-ms-cenc-frag: tests/data/asynth-44100-2.wav
+fate-mov-adpcm-ms-cenc-frag: CMD = transcode wav 
$(TARGET_PATH)/tests/data/asynth-44100-2.wav mov \
+  "-map 0:a -map 0:a -t 0.2 -c:a adpcm_ms -movflags +empty_moov -frag_duration 
25000 -encryption_scheme cenc-aes-ctr -encryption_key 
00112233445566778899aabbccddeeff -encryption_kid 
00112233445566778899aabbccddeeff" \
+  "-map 0:a -c:a pcm_s16le" "" "" "-decryption_key 
00112233445566778899aabbccddeeff"
+
 # avif muxing - still image with 1 item.
 FATE_MOV_FFMPEG_SAMPLES-$(call REMUX, AVIF OBU, AV1_DECODER AV1_PARSER) \
                            += fate-mov-avif-mux-still-image-1-item
diff --git a/tests/ref/fate/mov-adpcm-ms-cenc-frag 
b/tests/ref/fate/mov-adpcm-ms-cenc-frag
new file mode 100644
index 00000000000..e7ece6bed62
--- /dev/null
+++ b/tests/ref/fate/mov-adpcm-ms-cenc-frag
@@ -0,0 +1,30 @@
+0a1a82cdda694c97ae40a74124333942 *tests/data/fate/mov-adpcm-ms-cenc-frag.mov
+21790 tests/data/fate/mov-adpcm-ms-cenc-frag.mov
+#tb 0: 1/44100
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 44100
+#channel_layout_name 0: stereo
+#tb 1: 1/44100
+#media_type 1: audio
+#codec_id 1: pcm_s16le
+#sample_rate 1: 44100
+#channel_layout_name 1: stereo
+0,          0,          0,     1012,     4048, 0x2859017e
+1,          0,          0,     1012,     4048, 0x2859017e
+0,       1012,       1012,     1012,     4048, 0x4524f707
+1,       1012,       1012,     1012,     4048, 0x4524f707
+0,       2024,       2024,     1012,     4048, 0xedcee0dd
+1,       2024,       2024,     1012,     4048, 0xedcee0dd
+0,       3036,       3036,     1012,     4048, 0x1ce0cf87
+1,       3036,       3036,     1012,     4048, 0x1ce0cf87
+0,       4048,       4048,     1012,     4048, 0x51abd7d1
+1,       4048,       4048,     1012,     4048, 0x51abd7d1
+0,       5060,       5060,     1012,     4048, 0x0b88de79
+1,       5060,       5060,     1012,     4048, 0x0b88de79
+0,       6072,       6072,     1012,     4048, 0x3c33cf85
+1,       6072,       6072,     1012,     4048, 0x3c33cf85
+0,       7084,       7084,     1012,     4048, 0x9735ccf9
+1,       7084,       7084,     1012,     4048, 0x9735ccf9
+0,       8096,       8096,     1012,     4048, 0x3565dc87
+1,       8096,       8096,     1012,     4048, 0x3565dc87
-- 
2.54.1


From 6f9097756a370dcef49a0c614e41b43feb8630c3 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sun, 22 Nov 2026 22:35:42 -0800
Subject: [PATCH 2/3] avformat/mov: set the coded depth of encrypted audio from
 frma

The stsd depth was kept, so encrypted IMA WAV opened with 16 bits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mov.c                          |  6 ++++++
 tests/fate/mov.mak                         |  6 ++++++
 tests/ref/fate/mov-adpcm-ima-wav-cenc-frag | 16 ++++++++++++++++
 3 files changed, 28 insertions(+)
 create mode 100644 tests/ref/fate/mov-adpcm-ima-wav-cenc-frag

diff --git a/libavformat/mov.c b/libavformat/mov.c
index f2fb4c77a26..f795d15c2f0 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -8060,6 +8060,12 @@ static int mov_read_frma(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
 
         st->codecpar->codec_id = id;
         sc->format = format;
+        if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
+            /* the stsd depth was read before the codec was known */
+            int bits_per_sample = av_get_bits_per_sample(id);
+            if (bits_per_sample)
+                st->codecpar->bits_per_coded_sample = bits_per_sample;
+        }
         break;
 
     default:
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index a90f54d8ee6..2af1f1d3f4f 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -276,6 +276,12 @@ fate-mov-adpcm-ms-cenc-frag: CMD = transcode wav 
$(TARGET_PATH)/tests/data/asynt
   "-map 0:a -map 0:a -t 0.2 -c:a adpcm_ms -movflags +empty_moov -frag_duration 
25000 -encryption_scheme cenc-aes-ctr -encryption_key 
00112233445566778899aabbccddeeff -encryption_kid 
00112233445566778899aabbccddeeff" \
   "-map 0:a -c:a pcm_s16le" "" "" "-decryption_key 
00112233445566778899aabbccddeeff"
 
+FATE_MOV_FFMPEG-$(call ALLYES, ADPCM_IMA_WAV_ENCODER ADPCM_IMA_WAV_DECODER 
PCM_S16LE_DECODER PCM_S16LE_ENCODER ARESAMPLE_FILTER WAV_DEMUXER MOV_MUXER 
MOV_DEMUXER FRAMECRC_MUXER FILE_PROTOCOL PIPE_PROTOCOL) += 
fate-mov-adpcm-ima-wav-cenc-frag
+fate-mov-adpcm-ima-wav-cenc-frag: tests/data/asynth-44100-2.wav
+fate-mov-adpcm-ima-wav-cenc-frag: CMD = transcode wav 
$(TARGET_PATH)/tests/data/asynth-44100-2.wav mov \
+  "-t 0.2 -af aresample -c:a adpcm_ima_wav -movflags +empty_moov 
-frag_duration 25000 -encryption_scheme cenc-aes-ctr -encryption_key 
00112233445566778899aabbccddeeff -encryption_kid 
00112233445566778899aabbccddeeff" \
+  "-af aresample -c:a pcm_s16le" "" "" "-decryption_key 
00112233445566778899aabbccddeeff"
+
 # avif muxing - still image with 1 item.
 FATE_MOV_FFMPEG_SAMPLES-$(call REMUX, AVIF OBU, AV1_DECODER AV1_PARSER) \
                            += fate-mov-avif-mux-still-image-1-item
diff --git a/tests/ref/fate/mov-adpcm-ima-wav-cenc-frag 
b/tests/ref/fate/mov-adpcm-ima-wav-cenc-frag
new file mode 100644
index 00000000000..18b728eee34
--- /dev/null
+++ b/tests/ref/fate/mov-adpcm-ima-wav-cenc-frag
@@ -0,0 +1,16 @@
+f43ef45d8bcab2055260e7c8bc76d754 
*tests/data/fate/mov-adpcm-ima-wav-cenc-frag.mov
+11029 tests/data/fate/mov-adpcm-ima-wav-cenc-frag.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, 0x044df5c3
+0,       5085,       5085,     1017,     4068, 0x5bf8ee03
+0,       6102,       6102,     1017,     4068, 0xb82df4f3
+0,       7119,       7119,     1017,     4068, 0x2f10c875
+0,       8136,       8136,     1017,     4068, 0x79b62458
-- 
2.54.1


From cf7ac99b7dca4a73f539404a0e33d6e5874449ab Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sun, 22 Nov 2026 22:36:51 -0800
Subject: [PATCH 3/3] avformat/movenc: do not count buffered audio in a delayed
 empty moov

stts counted the CBR audio samples buffered by delay_moov.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/movenc.c              |  4 +++-
 tests/fate/mov.mak                |  6 ++++++
 tests/ref/fate/mov-pcm-delay-moov | 12 ++++++++++++
 3 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 tests/ref/fate/mov-pcm-delay-moov

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index c04670e1982..7c77afb7845 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -4655,12 +4655,13 @@ static int mov_write_trak_tag(AVFormatContext *s, 
AVIOContext *pb, MOVMuxContext
     int64_t pos = avio_tell(pb);
     int entry_backup = track->entry;
     int chunk_backup = track->chunkCount;
+    long sample_count_backup = track->sample_count;
     int ret;
 
     /* If we want to have an empty moov, but some samples already have been
      * buffered (delay_moov), pretend that no samples have been written yet. */
     if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV)
-        track->chunkCount = track->entry = 0;
+        track->chunkCount = track->entry = track->sample_count = 0;
 
     avio_wb32(pb, 0); /* size */
     ffio_wfourcc(pb, "trak");
@@ -4702,6 +4703,7 @@ static int mov_write_trak_tag(AVFormatContext *s, 
AVIOContext *pb, MOVMuxContext
     mov_write_track_udta_tag(pb, mov, st);
     track->entry = entry_backup;
     track->chunkCount = chunk_backup;
+    track->sample_count = sample_count_backup;
     return update_size(pb, pos);
 }
 
diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 2af1f1d3f4f..0c0c73324a6 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -282,6 +282,12 @@ fate-mov-adpcm-ima-wav-cenc-frag: CMD = transcode wav 
$(TARGET_PATH)/tests/data/
   "-t 0.2 -af aresample -c:a adpcm_ima_wav -movflags +empty_moov 
-frag_duration 25000 -encryption_scheme cenc-aes-ctr -encryption_key 
00112233445566778899aabbccddeeff -encryption_kid 
00112233445566778899aabbccddeeff" \
   "-af aresample -c:a pcm_s16le" "" "" "-decryption_key 
00112233445566778899aabbccddeeff"
 
+# The empty moov written by delay_moov must not count the buffered audio.
+FATE_MOV_FFMPEG_FFPROBE-$(call ALLYES, PCM_S16LE_DECODER PCM_S16LE_ENCODER 
WAV_DEMUXER MOV_MUXER MOV_DEMUXER FRAMECRC_MUXER FILE_PROTOCOL PIPE_PROTOCOL) 
+= fate-mov-pcm-delay-moov
+fate-mov-pcm-delay-moov: tests/data/asynth-44100-2.wav
+fate-mov-pcm-delay-moov: CMD = transcode wav 
$(TARGET_PATH)/tests/data/asynth-44100-2.wav mov \
+  "-t 0.05 -c:a pcm_s16le -movflags +delay_moov -frag_duration 25000" "-c:a 
copy" "-show_entries stream=duration_ts,nb_frames"
+
 # avif muxing - still image with 1 item.
 FATE_MOV_FFMPEG_SAMPLES-$(call REMUX, AVIF OBU, AV1_DECODER AV1_PARSER) \
                            += fate-mov-avif-mux-still-image-1-item
diff --git a/tests/ref/fate/mov-pcm-delay-moov 
b/tests/ref/fate/mov-pcm-delay-moov
new file mode 100644
index 00000000000..ea82cf3b0fc
--- /dev/null
+++ b/tests/ref/fate/mov-pcm-delay-moov
@@ -0,0 +1,12 @@
+8560b9a8020f9da1b4d4f0a45ab84f3b *tests/data/fate/mov-pcm-delay-moov.mov
+9664 tests/data/fate/mov-pcm-delay-moov.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,     2205,     8820, 0xc3813291
+[STREAM]
+duration_ts=2205
+nb_frames=N/A
+[/STREAM]
-- 
2.54.1

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

Reply via email to