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

# Summary of changes

This series adds Common Encryption `cbcs` (AES-CBC with pattern encryption, 
ISO/IEC 23001-7) support to the MOV muxer and demuxer. `cbcs` is the scheme 
used by HLS SAMPLE-AES and Apple FairPlay-style content.

Demuxer (`libavformat/mov.c`):

- Parse both the ISO/IEC 23001-7 `tenc` layout and the legacy FFmpeg field 
order.
- Decrypt `cbcs` samples correctly: apply the crypt/skip pattern to every whole 
16-byte block, including a truncated final crypt run, leaving only the trailing 
partial block clear (per ISO/IEC 23001-7, 10.4.2).
- Treat a zero pattern (`crypt_byte_block = skip_byte_block = 0`) as 
full-sample CBC, like `cbc1`, instead of rejecting it.
- Validate 16-byte alignment of CBC protected data rather than silently 
dropping a trailing partial block.

Muxer (`libavformat/movenc.c`, `libavformat/movenccenc.c`):

- Add `encryption_scheme=cbcs` and `encryption_crypt_byte_block` / 
`encryption_skip_byte_block` options.
- Encrypt samples with AES-CBC using the requested pattern (default 1:9 for 
video, 1:0 for other tracks).
- Write a spec-conformant version-1 `tenc` box carrying the crypt/skip block 
counts.

Tests (`tests/fate/mov.mak` and reference files):

- Add round-trip FATE tests that remux AV1, H.264, and HEVC samples to 
fragmented MP4 with `cbcs`, then demux and decrypt, verifying the decrypted 
content matches the source.

No new FATE test samples are required; the tests reuse samples already present 
in the fate-suite.


>From ca251548bd61c9c615d2ff0143eea417b33f038a Mon Sep 17 00:00:00 2001
From: Steven Liu <[email protected]>
Date: Tue, 10 Nov 2026 15:04:35 +0800
Subject: [PATCH 01/10] avformat/mov: use a local variable for the default tenc
 encryption info

---
 libavformat/mov.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/libavformat/mov.c b/libavformat/mov.c
index 0c2f592e7f3..718b9615b51 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -8525,6 +8525,7 @@ static int mov_read_tenc(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
 {
     AVStream *st;
     MOVStreamContext *sc;
+    AVEncryptionInfo *info;
     unsigned int version, pattern, is_protected, iv_size;
 
     if (c->fc->nb_streams < 1)
@@ -8543,6 +8544,7 @@ static int mov_read_tenc(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
             return AVERROR(ENOMEM);
         }
     }
+    info = sc->cenc.default_encrypted_sample;
 
     if (atom.size < 20)
         return AVERROR_INVALIDDATA;
@@ -8554,8 +8556,8 @@ static int mov_read_tenc(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
     pattern = avio_r8(pb);
 
     if (version > 0) {
-        sc->cenc.default_encrypted_sample->crypt_byte_block = pattern >> 4;
-        sc->cenc.default_encrypted_sample->skip_byte_block = pattern & 0xf;
+        info->crypt_byte_block = pattern >> 4;
+        info->skip_byte_block = pattern & 0xf;
     }
 
     is_protected = avio_r8(pb);
@@ -8571,7 +8573,7 @@ static int mov_read_tenc(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
         return AVERROR_INVALIDDATA;
     }
     sc->cenc.per_sample_iv_size = iv_size;
-    if (avio_read(pb, sc->cenc.default_encrypted_sample->key_id, 16) != 16) {
+    if (avio_read(pb, info->key_id, 16) != 16) {
         av_log(c->fc, AV_LOG_ERROR, "failed to read the default key ID\n");
         return AVERROR_INVALIDDATA;
     }
@@ -8583,7 +8585,7 @@ static int mov_read_tenc(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
             return AVERROR_INVALIDDATA;
         }
 
-        if (avio_read(pb, sc->cenc.default_encrypted_sample->iv, iv_size) != 
iv_size) {
+        if (avio_read(pb, info->iv, iv_size) != iv_size) {
             av_log(c->fc, AV_LOG_ERROR, "failed to read the default IV\n");
             return AVERROR_INVALIDDATA;
         }
-- 
2.54.0


>From 8019c5d2f9b7c764239b021b52c1bdb49fb13872 Mon Sep 17 00:00:00 2001
From: Steven Liu <[email protected]>
Date: Tue, 10 Nov 2026 15:02:59 +0800
Subject: [PATCH 02/10] avformat/mov: fix tenc parsing for ISO/IEC 23001-7
 layout

The TrackEncryptionBox ('tenc') parser only understood the legacy FFmpeg
field order and rejected files written with the ISO/IEC 23001-7 layout,
which is what the cbcs scheme uses. Accept both layouts so existing
files keep working while spec-conformant files can be parsed.
---
 libavformat/mov.c | 61 +++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 51 insertions(+), 10 deletions(-)

diff --git a/libavformat/mov.c b/libavformat/mov.c
index 718b9615b51..7e4a2583e1c 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -8527,6 +8527,8 @@ static int mov_read_tenc(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
     MOVStreamContext *sc;
     AVEncryptionInfo *info;
     unsigned int version, pattern, is_protected, iv_size;
+    int legacy_layout;
+    int bytes_read;
 
     if (c->fc->nb_streams < 1)
         return 0;
@@ -8546,7 +8548,7 @@ static int mov_read_tenc(MOVContext *c, AVIOContext *pb, 
MOVAtom atom)
     }
     info = sc->cenc.default_encrypted_sample;
 
-    if (atom.size < 20)
+    if (atom.size < 23)
         return AVERROR_INVALIDDATA;
 
     version = avio_r8(pb); /* version */
@@ -8554,20 +8556,34 @@ static int mov_read_tenc(MOVContext *c, AVIOContext 
*pb, MOVAtom atom)
 
     avio_r8(pb); /* reserved */
     pattern = avio_r8(pb);
+    is_protected = avio_r8(pb);
 
-    if (version > 0) {
-        info->crypt_byte_block = pattern >> 4;
-        info->skip_byte_block = pattern & 0xf;
+    /* ISO/IEC 23001-7 layout: reserved, default_IsProtected,
+     * default_Per_Sample_IV_Size, default_KID, [constant IV],
+     * default_Crypt_Byte_Block, default_Skip_Byte_Block. */
+    if (pattern <= 1 &&
+        (is_protected == 0 || is_protected == 8 || is_protected == 16)) {
+        legacy_layout = 0;
+        iv_size = is_protected;
+        is_protected = pattern;
+    } else {
+        /* Legacy FFmpeg layout: reserved, pattern,
+         * default_IsProtected, default_Per_Sample_IV_Size,
+         * default_KID, with a nibble-packed pattern byte. */
+        legacy_layout = 1;
+        if (version > 0) {
+            info->crypt_byte_block = pattern >> 4;
+            info->skip_byte_block = pattern & 0xf;
+        }
+        iv_size = avio_r8(pb);
     }
 
-    is_protected = avio_r8(pb);
     if (is_protected && !sc->cenc.encryption_index) {
         // The whole stream should be by-default encrypted.
         sc->cenc.encryption_index = av_mallocz(sizeof(MOVEncryptionIndex));
         if (!sc->cenc.encryption_index)
             return AVERROR(ENOMEM);
     }
-    iv_size = avio_r8(pb);
     if (iv_size != 0 && iv_size != 8 && iv_size != 16) {
         av_log(c->fc, AV_LOG_ERROR, "invalid per-sample IV size value\n");
         return AVERROR_INVALIDDATA;
@@ -8578,17 +8594,42 @@ static int mov_read_tenc(MOVContext *c, AVIOContext 
*pb, MOVAtom atom)
         return AVERROR_INVALIDDATA;
     }
 
-    if (is_protected && !sc->cenc.per_sample_iv_size) {
-        iv_size = avio_r8(pb);
-        if (iv_size != 8 && iv_size != 16) {
+    if (legacy_layout) {
+        if (is_protected && !sc->cenc.per_sample_iv_size) {
+            iv_size = avio_r8(pb);
+            if (iv_size != 8 && iv_size != 16) {
+                av_log(c->fc, AV_LOG_ERROR, "invalid default_constant_IV_size 
in tenc atom\n");
+                return AVERROR_INVALIDDATA;
+            }
+
+            if (avio_read(pb, info->iv, iv_size) != iv_size) {
+                av_log(c->fc, AV_LOG_ERROR, "failed to read the default IV\n");
+                return AVERROR_INVALIDDATA;
+            }
+        }
+        return 0;
+    }
+
+    bytes_read = 4 + 1 + 1 + 1 + 16;
+
+    if (is_protected && !iv_size) {
+        int constant_iv_size = avio_r8(pb);
+        bytes_read += 1;
+        if (constant_iv_size != 8 && constant_iv_size != 16) {
             av_log(c->fc, AV_LOG_ERROR, "invalid default_constant_IV_size in 
tenc atom\n");
             return AVERROR_INVALIDDATA;
         }
 
-        if (avio_read(pb, info->iv, iv_size) != iv_size) {
+        if (avio_read(pb, info->iv, constant_iv_size) != constant_iv_size) {
             av_log(c->fc, AV_LOG_ERROR, "failed to read the default IV\n");
             return AVERROR_INVALIDDATA;
         }
+        bytes_read += constant_iv_size;
+    }
+
+    if (bytes_read + 2 <= atom.size) {
+        info->crypt_byte_block = avio_r8(pb);
+        info->skip_byte_block = avio_r8(pb);
     }
 
     return 0;
-- 
2.54.0


>From c95ba3d34fdd1fc125e3b51c2f6777c72ff766e8 Mon Sep 17 00:00:00 2001
From: Steven Liu <[email protected]>
Date: Thu, 12 Nov 2026 10:08:15 +0800
Subject: [PATCH 03/10] avformat/mov: check sample size alignment before
 full-sample CBC decryption

cbc1 and cbcs use AES-CBC, so the whole-sample protected data passed to
av_aes_crypt() must be a multiple of the 16-byte block size.  The
decryption paths currently compute size/16 and silently drop a trailing
partial block; reject such samples instead.
---
 libavformat/mov.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/libavformat/mov.c b/libavformat/mov.c
index 7e4a2583e1c..ce9cc4293e2 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -8801,6 +8801,10 @@ static int cbc1_scheme_decrypt(MOVContext *c, 
MOVStreamContext *sc, AVEncryption
     /* whole-block full sample encryption */
     if (!sample->subsample_count) {
         /* decrypt the whole packet */
+        if (size % 16) {
+            av_log(c->fc, AV_LOG_ERROR, "sample size is not a multiple of 
16\n");
+            return AVERROR_INVALIDDATA;
+        }
         av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1);
         return 0;
     }
@@ -8937,6 +8941,10 @@ static int cbcs_scheme_decrypt(MOVContext *c, 
MOVStreamContext *sc, AVEncryption
     if (!sample->subsample_count) {
         /* decrypt the whole packet */
         memcpy(iv, sample->iv, 16);
+        if (size % 16) {
+            av_log(c->fc, AV_LOG_ERROR, "sample size is not a multiple of 
16\n");
+            return AVERROR_INVALIDDATA;
+        }
         av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1);
         return 0;
     } else if (!sample->crypt_byte_block && !sample->skip_byte_block) {
-- 
2.54.0


>From 71f6250d7ffe7a67d79167f72def406231dfcbe0 Mon Sep 17 00:00:00 2001
From: Steven Liu <[email protected]>
Date: Thu, 12 Nov 2026 10:08:59 +0800
Subject: [PATCH 04/10] avformat/mov: fix cbcs decryption for whole and
 subsampled samples

Apply the crypt/skip pattern when decrypting a whole-sample cbcs track,
where the pattern was previously ignored for samples without subsample
information.

Also decrypt subsampled cbcs samples without a pattern
(crypt_byte_block = skip_byte_block = 0) as full CBC blocks, like the
cbc1 scheme does, instead of rejecting them.

Apply the pattern to every whole 16-byte block, including a truncated
final crypt run; only the trailing partial block is left clear
(ISO/IEC 23001-7, 10.4.2).
---
 libavformat/mov.c | 55 ++++++++++++++++++++++++++++++++---------------
 1 file changed, 38 insertions(+), 17 deletions(-)

diff --git a/libavformat/mov.c b/libavformat/mov.c
index ce9cc4293e2..4e3039c01fe 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -8914,7 +8914,7 @@ static int cens_scheme_decrypt(MOVContext *c, 
MOVStreamContext *sc, AVEncryption
 
 static int cbcs_scheme_decrypt(MOVContext *c, MOVStreamContext *sc, 
AVEncryptionInfo *sample, uint8_t *input, int size)
 {
-    int i, ret, rem_bytes;
+    int i, ret, rem_bytes, crypt_blocks;
     uint8_t iv[16];
     uint8_t *data;
     uint8_t decryption_key[16];
@@ -8939,17 +8939,30 @@ static int cbcs_scheme_decrypt(MOVContext *c, 
MOVStreamContext *sc, AVEncryption
 
     /* whole-block full sample encryption */
     if (!sample->subsample_count) {
-        /* decrypt the whole packet */
         memcpy(iv, sample->iv, 16);
-        if (size % 16) {
-            av_log(c->fc, AV_LOG_ERROR, "sample size is not a multiple of 
16\n");
-            return AVERROR_INVALIDDATA;
+        if (!sample->crypt_byte_block && !sample->skip_byte_block) {
+            /* decrypt the whole packet */
+            if (size % 16) {
+                av_log(c->fc, AV_LOG_ERROR, "sample size is not a multiple of 
16\n");
+                return AVERROR_INVALIDDATA;
+            }
+            av_aes_crypt(sc->cenc.aes_ctx, input, input, size / 16, iv, 1);
+        } else {
+            /* apply the crypt/skip pattern to the whole sample */
+            data = input;
+            rem_bytes = size;
+            while (rem_bytes >= 16) {
+                crypt_blocks = FFMIN(sample->crypt_byte_block, rem_bytes / 16);
+
+                av_aes_crypt(sc->cenc.aes_ctx, data, data, crypt_blocks, iv, 
1);
+                data += 16 * crypt_blocks;
+                rem_bytes -= 16 * crypt_blocks;
+
+                data += FFMIN(16 * sample->skip_byte_block, rem_bytes);
+                rem_bytes -= FFMIN(16 * sample->skip_byte_block, rem_bytes);
+            }
         }
-        av_aes_crypt(sc->cenc.aes_ctx, input, input, size/16, iv, 1);
         return 0;
-    } else if (!sample->crypt_byte_block && !sample->skip_byte_block) {
-        av_log(c->fc, AV_LOG_ERROR, "pattern encryption is not present in 
'cbcs' scheme\n");
-        return AVERROR_INVALIDDATA;
     }
 
     for (i = 0; i < sample->subsample_count; i++) {
@@ -8966,15 +8979,23 @@ static int cbcs_scheme_decrypt(MOVContext *c, 
MOVStreamContext *sc, AVEncryption
         memcpy(iv, sample->iv, 16);
         data = input;
         rem_bytes = sample->subsamples[i].bytes_of_protected_data;
-        while (rem_bytes > 0) {
-            if (rem_bytes < 16*sample->crypt_byte_block) {
-                break;
+        if (!sample->crypt_byte_block && !sample->skip_byte_block) {
+            if (rem_bytes % 16) {
+                av_log(c->fc, AV_LOG_ERROR, "subsample BytesOfProtectedData is 
not a multiple of 16\n");
+                return AVERROR_INVALIDDATA;
+            }
+            av_aes_crypt(sc->cenc.aes_ctx, data, data, rem_bytes / 16, iv, 1);
+        } else {
+            while (rem_bytes >= 16) {
+                crypt_blocks = FFMIN(sample->crypt_byte_block, rem_bytes / 16);
+
+                av_aes_crypt(sc->cenc.aes_ctx, data, data, crypt_blocks, iv, 
1);
+                data += 16 * crypt_blocks;
+                rem_bytes -= 16 * crypt_blocks;
+
+                data += FFMIN(16 * sample->skip_byte_block, rem_bytes);
+                rem_bytes -= FFMIN(16 * sample->skip_byte_block, rem_bytes);
             }
-            av_aes_crypt(sc->cenc.aes_ctx, data, data, 
sample->crypt_byte_block, iv, 1);
-            data += 16*sample->crypt_byte_block;
-            rem_bytes -= 16*sample->crypt_byte_block;
-            data += FFMIN(16*sample->skip_byte_block, rem_bytes);
-            rem_bytes -= FFMIN(16*sample->skip_byte_block, rem_bytes);
         }
         input += sample->subsamples[i].bytes_of_protected_data;
         size -= sample->subsamples[i].bytes_of_protected_data;
-- 
2.54.0


>From eb1609247f8472b668647ea5ae2bbe945da0482b Mon Sep 17 00:00:00 2001
From: Steven Liu <[email protected]>
Date: Wed, 4 Nov 2026 20:58:11 +0800
Subject: [PATCH 05/10] avformat/movenc: add cbcs encryption support

Add support for the Common Encryption 'cbcs' scheme (AES-CBC with a
crypt/skip pattern), which is used by HLS SAMPLE-AES.

Samples are encrypted with AES-CBC, applying a 1:9 pattern for video
tracks and whole-block full-sample encryption for other tracks, and
the muxer writes a spec-conformant 'tenc' box with 16-byte per-sample
IVs. The existing cenc-aes-ctr output is left unchanged to preserve the
FFmpeg tenc layout, and the pattern can be overridden with the new
encryption_crypt_byte_block / encryption_skip_byte_block options.
---
 doc/muxers.texi          |  12 +++-
 libavformat/movenc.c     |  83 ++++++++++++++++-------
 libavformat/movenc.h     |   3 +
 libavformat/movenccenc.c | 140 ++++++++++++++++++++++++++++++++-------
 libavformat/movenccenc.h |  12 +++-
 5 files changed, 200 insertions(+), 50 deletions(-)

diff --git a/doc/muxers.texi b/doc/muxers.texi
index a2489760fcd..0666e44e28a 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -448,7 +448,17 @@ set the media encryption key identifier in hexadecimal 
format
 
 @item encryption_scheme @var{scheme}
 configure the encryption scheme, allowed values are @samp{none}, and
-@samp{cenc-aes-ctr}
+@samp{cenc-aes-ctr}, and @samp{cbcs}
+
+@item encryption_crypt_byte_block @var{blocks}
+Number of encrypted 16-byte blocks in a pattern used with @samp{cbcs}.
+@code{0} means automatic: @code{1} for video tracks and whole-block
+full-sample encryption for other tracks.
+
+@item encryption_skip_byte_block @var{blocks}
+Number of clear 16-byte blocks in a pattern used with @samp{cbcs}.
+@code{0} means automatic: @code{9} for video tracks and @code{0} for
+other tracks.
 
 @item frag_duration @var{duration}
 Create fragments that are @var{duration} microseconds long.
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index debe1acb7e0..8ba01349eab 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -81,7 +81,9 @@ static const AVOption options[] = {
     { "empty_hdlr_name", "write zero-length name string in hdlr atoms within 
mdia and minf atoms", offsetof(MOVMuxContext, empty_hdlr_name), 
AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
     { "encryption_key", "The media encryption key (hex)", 
offsetof(MOVMuxContext, encryption_key), AV_OPT_TYPE_BINARY, .flags = 
AV_OPT_FLAG_ENCODING_PARAM },
     { "encryption_kid", "The media encryption key identifier (hex)", 
offsetof(MOVMuxContext, encryption_kid), AV_OPT_TYPE_BINARY, .flags = 
AV_OPT_FLAG_ENCODING_PARAM },
-    { "encryption_scheme",    "Configures the encryption scheme, allowed 
values are none, cenc-aes-ctr", offsetof(MOVMuxContext, encryption_scheme_str), 
  AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
+    { "encryption_scheme",    "Configures the encryption scheme, allowed 
values are none, cenc-aes-ctr, cbcs", offsetof(MOVMuxContext, 
encryption_scheme_str),   AV_OPT_TYPE_STRING, {.str = NULL}, .flags = 
AV_OPT_FLAG_ENCODING_PARAM },
+    { "encryption_crypt_byte_block", "Count of encrypted 16-byte blocks in a 
pattern (cbcs), 0 means auto", offsetof(MOVMuxContext, 
encryption_crypt_byte_block), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, 
AV_OPT_FLAG_ENCODING_PARAM },
+    { "encryption_skip_byte_block", "Count of clear 16-byte blocks in a 
pattern (cbcs), 0 means auto", offsetof(MOVMuxContext, 
encryption_skip_byte_block), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, 
AV_OPT_FLAG_ENCODING_PARAM },
     { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, 
max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, 
AV_OPT_FLAG_ENCODING_PARAM},
     { "frag_interleave", "Interleave samples within fragments (max number of 
consecutive samples, lower is tighter interleaving, but with more overhead)", 
offsetof(MOVMuxContext, frag_interleave), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 
INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
     { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, 
max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, 
AV_OPT_FLAG_ENCODING_PARAM},
@@ -3511,7 +3513,7 @@ static int mov_write_stbl_tag(AVFormatContext *s, 
AVIOContext *pb, MOVMuxContext
     mov_write_stsc_tag(pb, track);
     mov_write_stsz_tag(pb, track);
     mov_write_stco_tag(pb, track);
-    if (track->cenc.aes_ctr && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
+    if (track->cenc.scheme != MOV_ENC_NONE && !(mov->flags & 
FF_MOV_FLAG_FRAGMENT)) {
         ff_mov_cenc_write_stbl_atoms(&track->cenc, pb, 0);
     }
     if (track->par->codec_id == AV_CODEC_ID_OPUS || track->par->codec_id == 
AV_CODEC_ID_AAC) {
@@ -6019,7 +6021,7 @@ static int mov_write_traf_tag(AVIOContext *pb, 
MOVMuxContext *mov,
         }
     }
 
-    if (track->cenc.aes_ctr && (mov->flags & FF_MOV_FLAG_FRAGMENT))
+    if (track->cenc.scheme != MOV_ENC_NONE && (mov->flags & 
FF_MOV_FLAG_FRAGMENT))
         ff_mov_cenc_write_stbl_atoms(&track->cenc, pb, moof_offset);
 
     return update_size(pb, pos);
@@ -6042,7 +6044,7 @@ static int mov_write_moof_tag_internal(AVIOContext *pb, 
MOVMuxContext *mov,
             continue;
         if (!track->entry)
             continue;
-        if (track->cenc.aes_ctr && (mov->flags & FF_MOV_FLAG_FRAGMENT))
+        if (track->cenc.scheme != MOV_ENC_NONE && (mov->flags & 
FF_MOV_FLAG_FRAGMENT))
             mov_write_pssh_tag(pb, track->st);
         mov_write_traf_tag(pb, mov, track, pos, moof_size);
     }
@@ -6988,7 +6990,7 @@ 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)
+            if (track->cenc.scheme != MOV_ENC_NONE)
                 ff_mov_cenc_flush(&track->cenc);
 
             avio_wb32(s->pb, mdat_size + 8);
@@ -7275,7 +7277,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
                 return ret;
             avio_write(pb, reformatted_data, size);
         } else {
-            if (trk->cenc.aes_ctr) {
+            if (trk->cenc.scheme != MOV_ENC_NONE) {
                 size = ff_mov_cenc_avc_parse_nal_units(&trk->cenc, pb, 
pkt->data, size);
                 if (size < 0) {
                     ret = size;
@@ -7296,7 +7298,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
                 return ret;
             avio_write(pb, reformatted_data, size);
         } else {
-            if (trk->cenc.aes_ctr) {
+            if (trk->cenc.scheme != MOV_ENC_NONE) {
                 size = ff_mov_cenc_avc_parse_nal_units(&trk->cenc, pb, 
pkt->data, size);
                 if (size < 0) {
                     ret = size;
@@ -7327,7 +7329,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
                 return ret;
             avio_write(pb, reformatted_data, size);
         } else {
-            if (trk->cenc.aes_ctr) {
+            if (trk->cenc.scheme != MOV_ENC_NONE) {
                 size = ff_mov_cenc_avc_parse_nal_units(&trk->cenc, pb, 
pkt->data, size);
                 if (size < 0) {
                     ret = size;
@@ -7337,7 +7339,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
                 size = ff_nal_parse_units(pb, pkt->data, pkt->size);
             }
         }
-    } else if (par->codec_id == AV_CODEC_ID_AV1 && !trk->cenc.aes_ctr) {
+    } else if (par->codec_id == AV_CODEC_ID_AV1 && trk->cenc.scheme == 
MOV_ENC_NONE) {
         if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
             ret = ff_av1_filter_obus_buf(pkt->data, &reformatted_data,
                                          &size, &offset);
@@ -7382,7 +7384,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
 
         avio_write(s->pb, pkt->data, pkt->size);
     } else {
-        if (trk->cenc.aes_ctr) {
+        if (trk->cenc.scheme != MOV_ENC_NONE) {
             uint8_t *extradata = trk->extradata[trk->last_stsd_index];
             int extradata_size = trk->extradata_size[trk->last_stsd_index];
             if (par->codec_id == AV_CODEC_ID_H264 && extradata_size > 4) {
@@ -8591,16 +8593,15 @@ static int mov_init(AVFormatContext *s)
     if (mov->encryption_scheme_str != NULL && 
strcmp(mov->encryption_scheme_str, "none") != 0) {
         if (strcmp(mov->encryption_scheme_str, "cenc-aes-ctr") == 0) {
             mov->encryption_scheme = MOV_ENC_CENC_AES_CTR;
-
-            if (mov->encryption_key_len != AES_CTR_KEY_SIZE) {
-                av_log(s, AV_LOG_ERROR, "Invalid encryption key len %d 
expected %d\n",
-                    mov->encryption_key_len, AES_CTR_KEY_SIZE);
-                return AVERROR(EINVAL);
-            }
-
-            if (mov->encryption_kid_len != CENC_KID_SIZE) {
-                av_log(s, AV_LOG_ERROR, "Invalid encryption kid len %d 
expected %d\n",
-                    mov->encryption_kid_len, CENC_KID_SIZE);
+        } else if (!av_strcasecmp(mov->encryption_scheme_str, "cbcs")) {
+            mov->encryption_scheme = MOV_ENC_CBCS;
+            if (mov->encryption_crypt_byte_block > 255 ||
+                mov->encryption_skip_byte_block > 255 ||
+                (!mov->encryption_crypt_byte_block &&
+                 mov->encryption_skip_byte_block > 0)) {
+                av_log(s, AV_LOG_ERROR, "Invalid encryption pattern %d:%d for 
cbcs\n",
+                       mov->encryption_crypt_byte_block,
+                       mov->encryption_skip_byte_block);
                 return AVERROR(EINVAL);
             }
         } else {
@@ -8608,6 +8609,18 @@ static int mov_init(AVFormatContext *s)
                 mov->encryption_scheme_str);
             return AVERROR(EINVAL);
         }
+
+        if (mov->encryption_key_len != AES_CTR_KEY_SIZE) {
+            av_log(s, AV_LOG_ERROR, "Invalid encryption key len %d expected 
%d\n",
+                mov->encryption_key_len, AES_CTR_KEY_SIZE);
+            return AVERROR(EINVAL);
+        }
+
+        if (mov->encryption_kid_len != CENC_KID_SIZE) {
+            av_log(s, AV_LOG_ERROR, "Invalid encryption kid len %d expected 
%d\n",
+                mov->encryption_kid_len, CENC_KID_SIZE);
+            return AVERROR(EINVAL);
+        }
     }
 
 #if CONFIG_IAMFENC
@@ -8833,11 +8846,33 @@ static int mov_init(AVFormatContext *s)
 
         avpriv_set_pts_info(st, 64, 1, track->timescale);
 
-        if (mov->encryption_scheme == MOV_ENC_CENC_AES_CTR) {
+        if (mov->encryption_scheme == MOV_ENC_CENC_AES_CTR ||
+            mov->encryption_scheme == MOV_ENC_CBCS) {
+            int use_subsamples = track->par->codec_id == AV_CODEC_ID_H264 ||
+                                 track->par->codec_id == AV_CODEC_ID_HEVC ||
+                                 track->par->codec_id == AV_CODEC_ID_VVC ||
+                                 track->par->codec_id == AV_CODEC_ID_AV1;
+            int crypt_byte_block = mov->encryption_crypt_byte_block;
+            int skip_byte_block  = mov->encryption_skip_byte_block;
+
+            if (mov->encryption_scheme == MOV_ENC_CBCS &&
+                !crypt_byte_block && !skip_byte_block) {
+                /* CENC default pattern: 1:9 for video, whole-block
+                 * full-sample encryption for other tracks. */
+                if (track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
+                    crypt_byte_block = 1;
+                    skip_byte_block  = 9;
+                } else {
+                    crypt_byte_block = 1;
+                    skip_byte_block  = 0;
+                }
+            }
+
             ret = ff_mov_cenc_init(&track->cenc, mov->encryption_key,
-                (track->par->codec_id == AV_CODEC_ID_H264 || 
track->par->codec_id == AV_CODEC_ID_HEVC ||
-                 track->par->codec_id == AV_CODEC_ID_VVC || 
track->par->codec_id == AV_CODEC_ID_AV1),
-                 track->par->codec_id, s->flags & AVFMT_FLAG_BITEXACT);
+                                   use_subsamples, track->par->codec_id,
+                                   s->flags & AVFMT_FLAG_BITEXACT,
+                                   mov->encryption_scheme,
+                                   crypt_byte_block, skip_byte_block);
             if (ret)
                 return ret;
         }
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index c375659c93e..eb1f886e330 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -199,6 +199,7 @@ typedef struct MOVTrack {
 typedef enum {
     MOV_ENC_NONE = 0,
     MOV_ENC_CENC_AES_CTR,
+    MOV_ENC_CBCS,
 } MOVEncryptionScheme;
 
 typedef enum {
@@ -260,6 +261,8 @@ typedef struct MOVMuxContext {
     int encryption_key_len;
     uint8_t *encryption_kid;
     int encryption_kid_len;
+    int encryption_crypt_byte_block;
+    int encryption_skip_byte_block;
 
     int use_stream_ids_as_track_ids;
     int track_ids_ok;
diff --git a/libavformat/movenccenc.c b/libavformat/movenccenc.c
index 4808b597739..79f0aef0550 100644
--- a/libavformat/movenccenc.c
+++ b/libavformat/movenccenc.c
@@ -22,9 +22,11 @@
 #include "libavcodec/av1_parse.h"
 #include "libavcodec/bytestream.h"
 #include "libavformat/cbs_av1.h"
+#include "libavutil/aes.h"
 #include "libavutil/attributes.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mem.h"
+#include "libavutil/random_seed.h"
 #include "avio_internal.h"
 #include "movenc.h"
 #include "avc.h"
@@ -89,6 +91,45 @@ static int auxiliary_info_add_subsample(MOVMuxCencContext* 
ctx,
     return 0;
 }
 
+/**
+ * Encrypt the input buffer with AES-CBC using the CENC pattern
+ * (crypt_byte_block encrypted 16-byte blocks followed by skip_byte_block
+ * clear 16-byte blocks, applied independently to each encrypted run).
+ */
+static void mov_cenc_write_encrypted_cbc(MOVMuxCencContext* ctx, AVIOContext 
*pb,
+                                         const uint8_t *buf_in, int size)
+{
+    uint8_t iv[16];
+    uint8_t block[16];
+    int crypt_blocks = ctx->crypt_byte_block;
+    int skip_blocks  = ctx->skip_byte_block;
+    int pattern_len  = crypt_blocks + skip_blocks;
+    int pos = 0;
+
+    if (pattern_len <= 0) {
+        /* whole-block full-sample encryption */
+        crypt_blocks = 1;
+        skip_blocks  = 0;
+        pattern_len  = 1;
+    }
+
+    memcpy(iv, ctx->cbc_iv, sizeof(iv));
+
+    while (pos + 16 <= size) {
+        int block_index = (pos / 16) % pattern_len;
+
+        if (block_index < crypt_blocks) {
+            av_aes_crypt(ctx->aes_cbc, block, buf_in + pos, 1, iv, 0);
+            avio_write(pb, block, 16);
+        } else
+            avio_write(pb, buf_in + pos, 16);
+        pos += 16;
+    }
+
+    if (pos < size)
+        avio_write(pb, buf_in + pos, size - pos);
+}
+
 /**
  * Encrypt the input buffer and write using avio_write
  */
@@ -100,6 +141,11 @@ static void mov_cenc_write_encrypted(MOVMuxCencContext* 
ctx, AVIOContext *pb,
     int size_left = size;
     int cur_size;
 
+    if (ctx->scheme == MOV_ENC_CBCS) {
+        mov_cenc_write_encrypted_cbc(ctx, pb, buf_in, size);
+        return;
+    }
+
     while (size_left > 0) {
         cur_size = FFMIN(size_left, sizeof(chunk));
         av_aes_ctr_crypt(ctx->aes_ctr, chunk, cur_pos, cur_size);
@@ -115,9 +161,11 @@ static void mov_cenc_write_encrypted(MOVMuxCencContext* 
ctx, AVIOContext *pb,
 static int mov_cenc_start_packet(MOVMuxCencContext* ctx)
 {
     int ret;
+    const uint8_t *iv = ctx->scheme == MOV_ENC_CBCS ? ctx->cbc_iv
+                                                     : 
av_aes_ctr_get_iv(ctx->aes_ctr);
 
     /* write the iv */
-    ret = auxiliary_info_write(ctx, av_aes_ctr_get_iv(ctx->aes_ctr), 
AES_CTR_IV_SIZE);
+    ret = auxiliary_info_write(ctx, iv, ctx->iv_size);
     if (ret) {
         return ret;
     }
@@ -137,6 +185,17 @@ static int mov_cenc_start_packet(MOVMuxCencContext* ctx)
     return 0;
 }
 
+static void mov_cenc_increment_iv(MOVMuxCencContext* ctx)
+{
+    if (ctx->scheme == MOV_ENC_CBCS) {
+        for (int i = 15; i >= 0; i--) {
+            if (++ctx->cbc_iv[i])
+                break;
+        }
+    } else
+        av_aes_ctr_increment_iv(ctx->aes_ctr);
+}
+
 /**
  * Finalize a packet
  */
@@ -144,7 +203,7 @@ static int mov_cenc_end_packet(MOVMuxCencContext* ctx)
 {
     size_t new_alloc_size;
 
-    av_aes_ctr_increment_iv(ctx->aes_ctr);
+    mov_cenc_increment_iv(ctx);
 
     if (!ctx->use_subsamples) {
         ctx->auxiliary_info_entries++;
@@ -161,7 +220,7 @@ static int mov_cenc_end_packet(MOVMuxCencContext* ctx)
         ctx->auxiliary_info_sizes_alloc_size = new_alloc_size;
     }
     ctx->auxiliary_info_sizes[ctx->auxiliary_info_entries] =
-        AES_CTR_IV_SIZE + ctx->auxiliary_info_size - 
ctx->auxiliary_info_subsample_start;
+        ctx->iv_size + ctx->auxiliary_info_size - 
ctx->auxiliary_info_subsample_start;
     ctx->auxiliary_info_entries++;
 
     /* update the subsample count*/
@@ -531,7 +590,7 @@ static int mov_cenc_write_saiz_tag(MOVMuxCencContext* ctx, 
AVIOContext *pb)
     avio_wb32(pb, 0); /* size */
     ffio_wfourcc(pb, "saiz");
     avio_wb32(pb, 0); /* version & flags */
-    avio_w8(pb, ctx->use_subsamples ? 0 : AES_CTR_IV_SIZE);    /* default 
size*/
+    avio_w8(pb, ctx->use_subsamples ? 0 : ctx->iv_size);    /* default size*/
     avio_wb32(pb, ctx->auxiliary_info_entries); /* entry count */
     if (ctx->use_subsamples) {
         avio_write(pb, ctx->auxiliary_info_sizes, ctx->auxiliary_info_entries);
@@ -549,18 +608,33 @@ void ff_mov_cenc_write_stbl_atoms(MOVMuxCencContext* ctx, 
AVIOContext *pb,
     mov_cenc_write_saiz_tag(ctx, pb);
 }
 
-static int mov_cenc_write_schi_tag(AVIOContext *pb, uint8_t* kid)
+static int mov_cenc_write_schi_tag(MOVMuxCencContext *ctx, AVIOContext *pb, 
uint8_t* kid)
 {
     int64_t pos = avio_tell(pb);
     avio_wb32(pb, 0);     /* size */
     ffio_wfourcc(pb, "schi");
 
-    avio_wb32(pb, 32);    /* size */
-    ffio_wfourcc(pb, "tenc");
-    avio_wb32(pb, 0);     /* version & flags */
-    avio_wb24(pb, 1);     /* is encrypted */
-    avio_w8(pb, AES_CTR_IV_SIZE); /* iv size */
-    avio_write(pb, kid, CENC_KID_SIZE);
+    if (ctx->scheme == MOV_ENC_CBCS) {
+        /* ISO/IEC 23001-7 TrackEncryptionBox ('tenc') */
+        int64_t tenc_pos = avio_tell(pb);
+        avio_wb32(pb, 0);          /* size, patched below */
+        ffio_wfourcc(pb, "tenc");
+        avio_wb32(pb, 0x01000000); /* version 1, flags 0 */
+        avio_w8(pb, 0);            /* reserved */
+        avio_w8(pb, 1);            /* default_IsProtected */
+        avio_w8(pb, ctx->iv_size); /* default_Per_Sample_IV_Size */
+        avio_write(pb, kid, CENC_KID_SIZE);
+        avio_w8(pb, ctx->crypt_byte_block); /* default_Crypt_Byte_Block */
+        avio_w8(pb, ctx->skip_byte_block);  /* default_Skip_Byte_Block */
+        update_size(pb, tenc_pos);
+    } else {
+        avio_wb32(pb, 32);    /* size */
+        ffio_wfourcc(pb, "tenc");
+        avio_wb32(pb, 0);     /* version & flags */
+        avio_wb24(pb, 1);     /* is encrypted */
+        avio_w8(pb, AES_CTR_IV_SIZE); /* iv size */
+        avio_write(pb, kid, CENC_KID_SIZE);
+    }
 
     return update_size(pb, pos);
 }
@@ -568,6 +642,7 @@ static int mov_cenc_write_schi_tag(AVIOContext *pb, 
uint8_t* kid)
 int ff_mov_cenc_write_sinf_tag(MOVTrack* track, AVIOContext *pb, uint8_t* kid)
 {
     int64_t pos = avio_tell(pb);
+    const char *scheme = track->cenc.scheme == MOV_ENC_CBCS ? "cbcs" : "cenc";
     avio_wb32(pb, 0); /* size */
     ffio_wfourcc(pb, "sinf");
 
@@ -580,11 +655,11 @@ int ff_mov_cenc_write_sinf_tag(MOVTrack* track, 
AVIOContext *pb, uint8_t* kid)
     avio_wb32(pb, 20);    /* size */
     ffio_wfourcc(pb, "schm");
     avio_wb32(pb, 0); /* version & flags */
-    ffio_wfourcc(pb, "cenc");    /* scheme type*/
+    ffio_wfourcc(pb, scheme);    /* scheme type*/
     avio_wb32(pb, 0x10000); /* scheme version */
 
     /* schi */
-    mov_cenc_write_schi_tag(pb, kid);
+    mov_cenc_write_schi_tag(&track->cenc, pb, kid);
 
     return update_size(pb, pos);
 }
@@ -598,22 +673,38 @@ static const CodedBitstreamUnitType 
decompose_unit_types[] = {
 };
 
 int ff_mov_cenc_init(MOVMuxCencContext* ctx, uint8_t* encryption_key,
-                     int use_subsamples, enum AVCodecID codec_id, int bitexact)
+                     int use_subsamples, enum AVCodecID codec_id, int bitexact,
+                     int scheme, int crypt_byte_block, int skip_byte_block)
 {
     int ret;
 
-    ctx->aes_ctr = av_aes_ctr_alloc();
-    if (!ctx->aes_ctr) {
-        return AVERROR(ENOMEM);
-    }
+    ctx->scheme            = scheme;
+    ctx->iv_size           = scheme == MOV_ENC_CBCS ? 16 : AES_CTR_IV_SIZE;
+    ctx->crypt_byte_block  = crypt_byte_block;
+    ctx->skip_byte_block   = skip_byte_block;
 
-    ret = av_aes_ctr_init(ctx->aes_ctr, encryption_key);
-    if (ret != 0) {
-        return ret;
-    }
+    if (scheme == MOV_ENC_CBCS) {
+        ctx->aes_cbc = av_aes_alloc();
+        if (!ctx->aes_cbc)
+            return AVERROR(ENOMEM);
 
-    if (!bitexact) {
-        av_aes_ctr_set_random_iv(ctx->aes_ctr);
+        ret = av_aes_init(ctx->aes_cbc, encryption_key, 128, 0);
+        if (ret)
+            return ret;
+
+        if (!bitexact)
+            av_random_bytes(ctx->cbc_iv, sizeof(ctx->cbc_iv));
+    } else {
+        ctx->aes_ctr = av_aes_ctr_alloc();
+        if (!ctx->aes_ctr)
+            return AVERROR(ENOMEM);
+
+        ret = av_aes_ctr_init(ctx->aes_ctr, encryption_key);
+        if (ret)
+            return ret;
+
+        if (!bitexact)
+            av_aes_ctr_set_random_iv(ctx->aes_ctr);
     }
 
     ctx->use_subsamples = use_subsamples;
@@ -639,6 +730,7 @@ void ff_mov_cenc_flush(MOVMuxCencContext* ctx)
 void ff_mov_cenc_free(MOVMuxCencContext* ctx)
 {
     av_aes_ctr_free(ctx->aes_ctr);
+    av_freep(&ctx->aes_cbc);
     av_freep(&ctx->auxiliary_info);
     av_freep(&ctx->auxiliary_info_sizes);
 
diff --git a/libavformat/movenccenc.h b/libavformat/movenccenc.h
index f03e2fe6aa8..b25a0d0cc6c 100644
--- a/libavformat/movenccenc.h
+++ b/libavformat/movenccenc.h
@@ -38,7 +38,13 @@ struct MOVMuxCencAV1TGInfo {
 };
 
 typedef struct {
+    int scheme;
     struct AVAESCTR* aes_ctr;
+    struct AVAES* aes_cbc;
+    uint8_t cbc_iv[16];
+    int iv_size;
+    int crypt_byte_block;
+    int skip_byte_block;
     uint8_t* auxiliary_info;
     size_t auxiliary_info_size;
     size_t auxiliary_info_alloc_size;
@@ -64,9 +70,13 @@ typedef struct {
  * Initialize a CENC context
  * @param key encryption key, must have a length of AES_CTR_KEY_SIZE
  * @param use_subsamples when enabled parts of a packet can be encrypted, 
otherwise the whole packet is encrypted
+ * @param scheme the encryption scheme (MOV_ENC_CENC_AES_CTR or MOV_ENC_CBCS)
+ * @param crypt_byte_block number of encrypted 16-byte blocks in a pattern 
(cbcs)
+ * @param skip_byte_block number of clear 16-byte blocks in a pattern (cbcs)
  */
 int ff_mov_cenc_init(MOVMuxCencContext* ctx, uint8_t* encryption_key, int 
use_subsamples,
-                     enum AVCodecID codec_id, int bitexact);
+                     enum AVCodecID codec_id, int bitexact,
+                     int scheme, int crypt_byte_block, int skip_byte_block);
 
 /**
  * Free a CENC context
-- 
2.54.0


>From 61f5cbcc864aef01cc0db61ebdbfeb3f84dfeae4 Mon Sep 17 00:00:00 2001
From: Steven Liu <[email protected]>
Date: Thu, 12 Nov 2026 12:07:44 +0800
Subject: [PATCH 06/10] tests/fate: add AV1 cbcs encryption round-trip test

Remux an AV1 sample to fragmented MP4 with the cbcs scheme, then
demux and decrypt it to confirm the decrypted content matches the
source.
---
 tests/fate/mov.mak          |  9 +++++++++
 tests/ref/fate/mov-av1-cbcs | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
 create mode 100644 tests/ref/fate/mov-av1-cbcs

diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 7aabe980d3d..d5c26be1105 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -261,6 +261,15 @@ $(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))
 
+define FATE_MOV_CBCS_TEST
+FATE_MOV_FFMPEG_SAMPLES-$(call REMUX, MP4 $(4), MOV_DEMUXER $(2)_PARSER 
EXTRACT_EXTRADATA_BSF) += fate-mov-$(1)-cbcs
+fate-mov-$(1)-cbcs: CMD = transcode $(3) $(TARGET_SAMPLES)/$(5) mp4 \
+    "-c:v copy -encryption_scheme cbcs -encryption_key 
00112233445566778899aabbccddeeff -encryption_kid 
00112233445566778899aabbccddeeff" \
+    "-c:v copy" "" "" "-decryption_key 00112233445566778899aabbccddeeff"
+endef
+
+$(eval $(call FATE_MOV_CBCS_TEST,av1,AV1,ivf,IVF,av1/non_uniform_tiling.ivf))
+
 # 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-av1-cbcs b/tests/ref/fate/mov-av1-cbcs
new file mode 100644
index 00000000000..5d06fcc88c4
--- /dev/null
+++ b/tests/ref/fate/mov-av1-cbcs
@@ -0,0 +1,32 @@
+771b1c57b6ad83424416b2991a742c9e *tests/data/fate/mov-av1-cbcs.mp4
+39559 tests/data/fate/mov-av1-cbcs.mp4
+#extradata 0:       17, 0x197103ea
+#tb 0: 1/12288
+#media_type 0: video
+#codec_id 0: av1
+#dimensions 0: 720x300
+#sar 0: 1/1
+0,          0,          0,      512,    17049, 0x6d66f097
+0,        512,        512,      512,     8365, 0x9c48024d, F=0x0
+0,       1024,       1024,      512,       77, 0x88251d4e, F=0x0
+0,       1536,       1536,      512,      232, 0x3c31706f, F=0x0
+0,       2048,       2048,      512,      245, 0x8195753f, F=0x0
+0,       2560,       2560,      512,      271, 0x134f86cc, F=0x0
+0,       3072,       3072,      512,      246, 0x04b777cf, F=0x0
+0,       3584,       3584,      512,      143, 0x6cbd43a3, F=0x0
+0,       4096,       4096,      512,      215, 0x88d66733, F=0x0
+0,       4608,       4608,      512,       83, 0xb2d22362, F=0x0
+0,       5120,       5120,      512,       50, 0x75d211e2, F=0x0
+0,       5632,       5632,      512,     5087, 0x505ec5f3, F=0x0
+0,       6144,       6144,      512,      242, 0x70fe6f2c, F=0x0
+0,       6656,       6656,      512,      366, 0xcb0caedc, F=0x0
+0,       7168,       7168,      512,      644, 0xf72e3623, F=0x0
+0,       7680,       7680,      512,      668, 0x0cff4537, F=0x0
+0,       8192,       8192,      512,      239, 0x364c77a4, F=0x0
+0,       8704,       8704,      512,      400, 0x7e3bbaef, F=0x0
+0,       9216,       9216,      512,      404, 0x175cb5db, F=0x0
+0,       9728,       9728,      512,      277, 0x113185c5, F=0x0
+0,      10240,      10240,      512,       46, 0x0c860cfa, F=0x0
+0,      10752,      10752,      512,      659, 0x501b392a, F=0x0
+0,      11264,      11264,      512,      478, 0x0503e8ec, F=0x0
+0,      11776,      11776,      512,     1132, 0x952a1543, F=0x0
-- 
2.54.0


>From cbcd8fc71a973a1feb52cf507a3959f5745ee863 Mon Sep 17 00:00:00 2001
From: Steven Liu <[email protected]>
Date: Thu, 12 Nov 2026 12:07:50 +0800
Subject: [PATCH 07/10] tests/fate: add H.264 cbcs encryption round-trip test

Remux an H.264 sample to fragmented MP4 with the cbcs scheme, then
demux and decrypt it to confirm the decrypted content matches the
source.
---
 tests/fate/mov.mak           |  1 +
 tests/ref/fate/mov-h264-cbcs | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 tests/ref/fate/mov-h264-cbcs

diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index d5c26be1105..d48e90aed50 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -269,6 +269,7 @@ fate-mov-$(1)-cbcs: CMD = transcode $(3) 
$(TARGET_SAMPLES)/$(5) mp4 \
 endef
 
 $(eval $(call FATE_MOV_CBCS_TEST,av1,AV1,ivf,IVF,av1/non_uniform_tiling.ivf))
+$(eval $(call 
FATE_MOV_CBCS_TEST,h264,H264,h264,H264,h264-conformance/CAMACI3_Sony_C.jsv))
 
 # avif muxing - still image with 1 item.
 FATE_MOV_FFMPEG_SAMPLES-$(call REMUX, AVIF OBU, AV1_DECODER AV1_PARSER) \
diff --git a/tests/ref/fate/mov-h264-cbcs b/tests/ref/fate/mov-h264-cbcs
new file mode 100644
index 00000000000..1eb4f779579
--- /dev/null
+++ b/tests/ref/fate/mov-h264-cbcs
@@ -0,0 +1,25 @@
+81a6499ca3d1be987b6c35eda04c20f5 *tests/data/fate/mov-h264-cbcs.mp4
+73133 tests/data/fate/mov-h264-cbcs.mp4
+#extradata 0:       25, 0x5c9d073a
+#tb 0: 1/1200000
+#media_type 0: video
+#codec_id 0: h264
+#dimensions 0: 192x128
+#sar 0: 0/1
+0,     -96000,     -96000,    48000,     5183, 0x9bc6204f, F=0x5
+0,     -48000,     -48000,    48000,     4528, 0x9e28ce6e, F=0x4
+0,          0,          0,    48000,     4660, 0x1ff41c2a, F=0x0
+0,      48000,      48000,    48000,     3740, 0x8cec5062, F=0x0
+0,      96000,      96000,    48000,     3492, 0x8471c705, F=0x0
+0,     144000,     144000,    48000,     4793, 0xab1b7855, F=0x0
+0,     192000,     192000,    48000,     3351, 0xad2783f0, F=0x0
+0,     240000,     240000,    48000,     3396, 0xe0908da6, F=0x0
+0,     288000,     288000,    48000,     4846, 0x03a36085, F=0x0
+0,     336000,     336000,    48000,     3870, 0xdd6fa409, F=0x0
+0,     384000,     384000,    48000,     4022, 0x2a85ee8e, F=0x0
+0,     432000,     432000,    48000,     5641, 0x60cfe0ca, F=0x0
+0,     480000,     480000,    48000,     4221, 0x97733c38, F=0x0
+0,     528000,     528000,    48000,     4068, 0xfc65fe01, F=0x0
+0,     576000,     576000,    48000,     3971, 0x1b7aa573, F=0x0
+0,     624000,     624000,    48000,     3964, 0x4abab2ed, F=0x0
+0,     672000,     672000,    48000,     3867, 0xf12f9612, F=0x0
-- 
2.54.0


>From 2bfb40eb29a29a7563407018913f24581df9392d Mon Sep 17 00:00:00 2001
From: Steven Liu <[email protected]>
Date: Thu, 12 Nov 2026 12:07:55 +0800
Subject: [PATCH 08/10] tests/fate: add H.264 (Matroska) cbcs encryption
 round-trip test

Remux an H.264 sample from Matroska to fragmented MP4 with the cbcs
scheme, then demux and decrypt it to confirm the decrypted content
matches the source.
---
 tests/fate/mov.mak               |  1 +
 tests/ref/fate/mov-h264_iso-cbcs | 13 +++++++++++++
 2 files changed, 14 insertions(+)
 create mode 100644 tests/ref/fate/mov-h264_iso-cbcs

diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index d48e90aed50..1ff3a3e2c63 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -270,6 +270,7 @@ endef
 
 $(eval $(call FATE_MOV_CBCS_TEST,av1,AV1,ivf,IVF,av1/non_uniform_tiling.ivf))
 $(eval $(call 
FATE_MOV_CBCS_TEST,h264,H264,h264,H264,h264-conformance/CAMACI3_Sony_C.jsv))
+$(eval $(call 
FATE_MOV_CBCS_TEST,h264_iso,H264,matroska,MATROSKA,h264/dts_5frames.mkv))
 
 # avif muxing - still image with 1 item.
 FATE_MOV_FFMPEG_SAMPLES-$(call REMUX, AVIF OBU, AV1_DECODER AV1_PARSER) \
diff --git a/tests/ref/fate/mov-h264_iso-cbcs b/tests/ref/fate/mov-h264_iso-cbcs
new file mode 100644
index 00000000000..d267935ecfc
--- /dev/null
+++ b/tests/ref/fate/mov-h264_iso-cbcs
@@ -0,0 +1,13 @@
+2ec131ad412d6f02564111874dcd35f4 *tests/data/fate/mov-h264_iso-cbcs.mp4
+15583 tests/data/fate/mov-h264_iso-cbcs.mp4
+#extradata 0:       40, 0xcd870951
+#tb 0: 1/16000
+#media_type 0: video
+#codec_id 0: h264
+#dimensions 0: 512x256
+#sar 0: 1/1
+0,          0,          0,     2672,    14428, 0x1c626b86
+0,       2672,       2672,     2656,       11, 0x08f901f1, F=0x0
+0,       5328,       5328,     2672,       11, 0x09990211, F=0x0
+0,       8000,       8000,     2672,       11, 0x0a390231, F=0x0
+0,      10672,      10672,     2672,       11, 0x0ad90251, F=0x0
-- 
2.54.0


>From 818ddcb412c23c6d318185fcef7a2818e04c7a80 Mon Sep 17 00:00:00 2001
From: Steven Liu <[email protected]>
Date: Thu, 12 Nov 2026 12:08:01 +0800
Subject: [PATCH 09/10] tests/fate: add HEVC cbcs encryption round-trip test

Remux an HEVC sample to fragmented MP4 with the cbcs scheme, then
demux and decrypt it to confirm the decrypted content matches the
source.
---
 tests/fate/mov.mak           | 1 +
 tests/ref/fate/mov-hevc-cbcs | 9 +++++++++
 2 files changed, 10 insertions(+)
 create mode 100644 tests/ref/fate/mov-hevc-cbcs

diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index 1ff3a3e2c63..d00752ad6d8 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -271,6 +271,7 @@ endef
 $(eval $(call FATE_MOV_CBCS_TEST,av1,AV1,ivf,IVF,av1/non_uniform_tiling.ivf))
 $(eval $(call 
FATE_MOV_CBCS_TEST,h264,H264,h264,H264,h264-conformance/CAMACI3_Sony_C.jsv))
 $(eval $(call 
FATE_MOV_CBCS_TEST,h264_iso,H264,matroska,MATROSKA,h264/dts_5frames.mkv))
+$(eval $(call 
FATE_MOV_CBCS_TEST,hevc,HEVC,hevc,HEVC,hevc-conformance/ipcm_A_NEC_3.bit))
 
 # avif muxing - still image with 1 item.
 FATE_MOV_FFMPEG_SAMPLES-$(call REMUX, AVIF OBU, AV1_DECODER AV1_PARSER) \
diff --git a/tests/ref/fate/mov-hevc-cbcs b/tests/ref/fate/mov-hevc-cbcs
new file mode 100644
index 00000000000..73636bc3396
--- /dev/null
+++ b/tests/ref/fate/mov-hevc-cbcs
@@ -0,0 +1,9 @@
+3c3a59717232d251be1dec83f108ee1b *tests/data/fate/mov-hevc-cbcs.mp4
+6175 tests/data/fate/mov-hevc-cbcs.mp4
+#extradata 0:      101, 0x8d351483
+#tb 0: 1/1200000
+#media_type 0: video
+#codec_id 0: hevc
+#dimensions 0: 416x240
+#sar 0: 0/1
+0,          0,          0,    48000,     5155, 0x18ffdb5c
-- 
2.54.0


>From 44f243a351c6bca504701b2f873defbb47c55d73 Mon Sep 17 00:00:00 2001
From: Steven Liu <[email protected]>
Date: Thu, 12 Nov 2026 12:08:07 +0800
Subject: [PATCH 10/10] tests/fate: add HEVC (ISO-BMFF) cbcs encryption
 round-trip test

Remux an HEVC sample from MOV to fragmented MP4 with the cbcs scheme,
then demux and decrypt it to confirm the decrypted content matches the
source.
---
 tests/fate/mov.mak               |  1 +
 tests/ref/fate/mov-hevc_iso-cbcs | 10 ++++++++++
 2 files changed, 11 insertions(+)
 create mode 100644 tests/ref/fate/mov-hevc_iso-cbcs

diff --git a/tests/fate/mov.mak b/tests/fate/mov.mak
index d00752ad6d8..265ad95375b 100644
--- a/tests/fate/mov.mak
+++ b/tests/fate/mov.mak
@@ -272,6 +272,7 @@ $(eval $(call 
FATE_MOV_CBCS_TEST,av1,AV1,ivf,IVF,av1/non_uniform_tiling.ivf))
 $(eval $(call 
FATE_MOV_CBCS_TEST,h264,H264,h264,H264,h264-conformance/CAMACI3_Sony_C.jsv))
 $(eval $(call 
FATE_MOV_CBCS_TEST,h264_iso,H264,matroska,MATROSKA,h264/dts_5frames.mkv))
 $(eval $(call 
FATE_MOV_CBCS_TEST,hevc,HEVC,hevc,HEVC,hevc-conformance/ipcm_A_NEC_3.bit))
+$(eval $(call 
FATE_MOV_CBCS_TEST,hevc_iso,HEVC,mov,MOV,hevc/extradata-reload-multi-stsd.mov))
 
 # avif muxing - still image with 1 item.
 FATE_MOV_FFMPEG_SAMPLES-$(call REMUX, AVIF OBU, AV1_DECODER AV1_PARSER) \
diff --git a/tests/ref/fate/mov-hevc_iso-cbcs b/tests/ref/fate/mov-hevc_iso-cbcs
new file mode 100644
index 00000000000..fbc8d867440
--- /dev/null
+++ b/tests/ref/fate/mov-hevc_iso-cbcs
@@ -0,0 +1,10 @@
+99d0fc1cf98459635ba1533e3bb9692e *tests/data/fate/mov-hevc_iso-cbcs.mp4
+5582 tests/data/fate/mov-hevc_iso-cbcs.mp4
+#extradata 0:      110, 0xb3a41478
+#tb 0: 1/12800
+#media_type 0: video
+#codec_id 0: hevc
+#dimensions 0: 128x128
+#sar 0: 1/1
+0,          0,          0,      512,     2108, 0x57c38f64
+0,        512,        512,      512,       31, 0xabe10d25, F=0x0
-- 
2.54.0

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

Reply via email to