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

Since cf0244aa38 (issue #23417), enabling edit lists with `-movflags cmaf` 
(e.g. via `+delay_moov`, or `-use_editlist 1`) disables negative CTS offsets 
for all tracks, even when `negative_cts_offsets` is explicitly set. That is 
correct for the case reported there: a CMAF video track shall not use both 
signed composition offsets and an edit list (ISO/IEC 23000-19, 9.2.5).

It however leaves no way to write video tracks with version 1 `trun` boxes and 
no edit list, which is the form 9.2.5 requires outside of CMAF track files and 
that the `cmf2` brand mandates (7.7.2, 7.7.3), while still writing the offset 
edit list needed for audio priming (10.2.6, 10.3.3.2).

### Changes

- **allow negative CTS offsets with edit lists in CMAF output**: when 
`negative_cts_offsets` is explicitly set together with `cmaf`, keep the signed 
offsets and don't write an edit list for the video tracks; the other tracks 
keep theirs. If such a video track doesn't start at pts 0, a warning is 
printed, as this can't be signaled without an edit list.
- **signal the cmf2 brand**: write `cmf2` next to `cmfc` when CMAF output uses 
negative CTS offsets, as 7.2 recommends listing all the structural brands a 
track conforms to.

The behavior of plain `-movflags cmaf`, with or without edit lists, and of an 
explicit `-use_editlist 1`, is unchanged, apart from the additional compatible 
brand when negative CTS offsets are already in use.

### Result

`-movflags cmaf+delay_moov+negative_cts_offsets+frag_keyframe`, H.264 with 
B-frames + AAC:

| | before | after |
|---|---|---|
| video `trun` | version 0, unsigned offsets | version 1, signed offsets, 
earliest pts of each fragment == `baseMediaDecodeTime` (7.5.17) |
| video edit list | `media_time` = reorder delay | none |
| audio edit list | single entry, `media_time` = priming | same |
| brands | `cmfc` | `cmfc`, `cmf2` |

### Testing

- New `cmaf-negative-cts-elst` case in `fate-api-movenc`. The `cmf2` brand 
grows the `ftyp` by 4 bytes, which changes the `api-movenc` and 
`mov-mp4-fragmented-ttml-stpp` references.
- Full `make fate` passes locally, except tests whose samples are missing from 
my local sample set.
- Apple's `avmediainfo` reads the output with video starting at 0 and the audio 
priming trimmed, and `mediastreamvalidator` 2.0.284 reports no media issues on 
single-track files referenced by byte-range HLS playlists.

The clause numbers are from the ISO/IEC 23000-19 4th edition CD text (WG 03 
N1847).

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



From 28f1af224bd94f95b05ec4a2856362da0ef708ba Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Tue, 10 Nov 2026 23:19:25 -0800
Subject: [PATCH 1/2] avformat/movenc: allow negative CTS offsets with edit
 lists in CMAF output

Since cf0244aa38, enabling edit lists with the cmaf flag disables negative
CTS offsets for all the tracks, even when the negative_cts_offsets flag was
explicitly set, as a CMAF video track shall not use both signed composition
offsets and an edit list (ISO/IEC 23000-19, 9.2.5).

This made it impossible to use version 1 trun boxes for the video tracks,
as required outside of CMAF track files and by the cmf2 brand, while still
writing the offset edit list needed to signal audio priming.

When negative_cts_offsets is explicitly set together with cmaf, keep the
signed offsets, and don't write an edit list for the video tracks instead.
The default behavior is unchanged.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
 doc/muxers.texi             |  7 +++++++
 libavformat/movenc.c        | 23 +++++++++++++++++++++--
 tests/api/api-movenc-test.c | 10 ++++++++++
 tests/ref/fate/api-movenc   |  5 +++++
 4 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/doc/muxers.texi b/doc/muxers.texi
index a2489760fcd..3b3bc18a13d 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -491,6 +491,13 @@ Set various muxing switches. The following flags can be 
used:
 write CMAF (Common Media Application Format) compatible fragmented
 MP4 output
 
+This implicitly sets @samp{negative_cts_offsets}, unless edit lists are
+written (see @option{use_editlist}), in which case all the tracks get an
+edit list and unsigned CTS offsets are used, which CMAF only allows in
+CMAF track files. Setting @samp{negative_cts_offsets} explicitly together
+with this flag keeps the negative CTS offsets and writes edit lists only
+for the non-video tracks, e.g. to signal audio priming.
+
 @item dash
 write DASH (Dynamic Adaptive Streaming over HTTP) compatible fragmented
 MP4 output
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index ea71acde42a..d6fe7491540 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -4542,7 +4542,20 @@ static int mov_write_trak_tag(AVFormatContext *s, 
AVIOContext *pb, MOVMuxContext
     av_assert2(mov->use_editlist >= 0);
 
     if (track->start_dts != AV_NOPTS_VALUE) {
-        if (mov->use_editlist)
+        /* A CMAF video track shall not use both signed composition offsets
+         * and an edit list (ISO/IEC 23000-19, 9.2.5). The signed offsets
+         * already make the first sample start at its decode time. */
+        if (mov->use_editlist && mov->flags & FF_MOV_FLAG_CMAF &&
+            mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS &&
+            track->par->codec_type == AVMEDIA_TYPE_VIDEO) {
+            if (track->start_cts != AV_NOPTS_VALUE &&
+                track->start_dts + track->start_cts)
+                av_log(mov->fc, AV_LOG_WARNING,
+                       "Track %d starts at pts %"PRId64", which can't be "
+                       "signaled without an edit list in a CMAF video track "
+                       "using negative CTS offsets\n",
+                       track->track_id, track->start_dts + track->start_cts);
+        } else if (mov->use_editlist)
             mov_write_edts_tag(pb, mov, track);  // PSP Movies and several 
other cases require edts box
         else if ((track->entry && track->cluster[0].dts) || track->mode == 
MODE_PSP || is_clcp_track(track))
             av_log(mov->fc, AV_LOG_WARNING,
@@ -8335,6 +8348,7 @@ static int mov_init_iamf_track(AVFormatContext *s)
 static int mov_init(AVFormatContext *s)
 {
     MOVMuxContext *mov = s->priv_data;
+    int negative_cts_offsets = mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS;
     int has_iamf = 0;
     int i, ret;
 
@@ -8420,7 +8434,12 @@ static int mov_init(AVFormatContext *s)
         !(mov->flags & FF_MOV_FLAG_DELAY_MOOV) && mov->use_editlist)
         av_log(s, AV_LOG_WARNING, "No meaningful edit list will be written 
when using empty_moov without delay_moov\n");
 
-    if (mov->flags & FF_MOV_FLAG_CMAF && mov->use_editlist)
+    /* CMAF video tracks either use signed composition offsets or an edit
+     * list, never both. When edit lists are enabled, default to writing
+     * them for all the tracks, with unsigned offsets (CMAF track file).
+     * If negative CTS offsets were explicitly requested, keep them and
+     * don't write an edit list for the video tracks instead. */
+    if (mov->flags & FF_MOV_FLAG_CMAF && mov->use_editlist && 
!negative_cts_offsets)
         mov->flags &= ~FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS;
 
     if (!mov->use_editlist && s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO 
&&
diff --git a/tests/api/api-movenc-test.c b/tests/api/api-movenc-test.c
index 39494748fd3..3ee4e83fbe3 100644
--- a/tests/api/api-movenc-test.c
+++ b/tests/api/api-movenc-test.c
@@ -526,6 +526,16 @@ int main(int argc, char **argv)
     finish();
     close_out();
 
+    // Test writing CMAF with both edit lists and negative CTS offsets;
+    // the video track uses the signed offsets and has no edit list,
+    // while the audio track has an edit list for its preroll.
+    init_out("cmaf-negative-cts-elst");
+    av_dict_set(&opts, "movflags", 
"+cmaf+frag_keyframe+delay_moov+negative_cts_offsets", 0);
+    init(1, 1);
+    mux_gops(2);
+    finish();
+    close_out();
+
     // Test writing a file with one track lacking packets, with delay_moov.
     skip_write_audio = 1;
     init_out("delay-moov-empty-track");
diff --git a/tests/ref/fate/api-movenc b/tests/ref/fate/api-movenc
index 20996b89101..0638c653d50 100644
--- a/tests/ref/fate/api-movenc
+++ b/tests/ref/fate/api-movenc
@@ -53,6 +53,11 @@ write_data len 916, time -33333, type sync atom moof
 write_data len 908, time 966667, type sync atom moof
 write_data len 148, time nopts, type trailer atom -
 b3e114f4c1f8d090ab4411b1845e89f8 3203 delay-moov-elst
+write_data len 1187, time nopts, type header atom ftyp
+write_data len 908, time 0, type sync atom moof
+write_data len 900, time 1000000, type sync atom moof
+write_data len 148, time nopts, type trailer atom -
+1282090463af3213f43b3acdba23df94 3143 cmaf-negative-cts-elst
 write_data len 1195, time nopts, type header atom ftyp
 write_data len 836, time 0, type sync atom moof
 write_data len 67, time nopts, type trailer atom -
-- 
2.54.0


From 253eede4744d13654740b5aa2f0f53bc247692d9 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Tue, 10 Nov 2026 23:21:23 -0800
Subject: [PATCH 2/2] avformat/movenc: signal the cmf2 brand in CMAF output
 using negative CTS offsets

ISO/IEC 23000-19, 7.2 recommends listing all the structural CMAF brands
a track conforms to. On top of the cmfc constraints, cmf2 requires video
tracks to use version 1 trun boxes and to not have an edit list, and the
sample flags, durations and sizes to be set in the tfhd or trun boxes
(7.7), all of which are fulfilled when negative CTS offsets are used.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
 libavformat/movenc.c                        |  7 ++++-
 tests/ref/fate/api-movenc                   |  8 +++---
 tests/ref/fate/mov-mp4-fragmented-ttml-stpp | 30 ++++++++++-----------
 3 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index d6fe7491540..d9c89dcf12e 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -6477,8 +6477,13 @@ static int mov_write_ftyp_tag(AVIOContext *pb, 
AVFormatContext *s)
         // brand, if not already the major brand. This is compatible with 
users that
         // don't understand tfdt.
         if (mov->mode == MODE_MP4) {
-            if (mov->flags & FF_MOV_FLAG_CMAF)
+            if (mov->flags & FF_MOV_FLAG_CMAF) {
                 ffio_wfourcc(pb, "cmfc");
+                // cmf2 additionally requires video tracks to use version 1
+                // trun boxes and to not have an edit list.
+                if (mov->flags & FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS)
+                    ffio_wfourcc(pb, "cmf2");
+            }
             if (mov->flags & FF_MOV_FLAG_FRAGMENT && !(mov->flags & 
FF_MOV_FLAG_NEGATIVE_CTS_OFFSETS))
                 ffio_wfourcc(pb, "iso6");
             if (has_av1)
diff --git a/tests/ref/fate/api-movenc b/tests/ref/fate/api-movenc
index 0638c653d50..7dcc533e965 100644
--- a/tests/ref/fate/api-movenc
+++ b/tests/ref/fate/api-movenc
@@ -53,11 +53,11 @@ write_data len 916, time -33333, type sync atom moof
 write_data len 908, time 966667, type sync atom moof
 write_data len 148, time nopts, type trailer atom -
 b3e114f4c1f8d090ab4411b1845e89f8 3203 delay-moov-elst
-write_data len 1187, time nopts, type header atom ftyp
+write_data len 1191, time nopts, type header atom ftyp
 write_data len 908, time 0, type sync atom moof
 write_data len 900, time 1000000, type sync atom moof
 write_data len 148, time nopts, type trailer atom -
-1282090463af3213f43b3acdba23df94 3143 cmaf-negative-cts-elst
+72905d37240956dfb856d5e1f4aca4cb 3147 cmaf-negative-cts-elst
 write_data len 1195, time nopts, type header atom ftyp
 write_data len 836, time 0, type sync atom moof
 write_data len 67, time nopts, type trailer atom -
@@ -160,11 +160,11 @@ write_data len 900, time 0, type sync atom moof
 write_data len 908, time 1000000, type sync atom moof
 write_data len 148, time nopts, type trailer atom -
 47ea28db2ccd18d9c3b080a64e5420de 3115 empty-moov-neg-cts
-write_data len 28, time nopts, type header atom ftyp
+write_data len 32, time nopts, type header atom ftyp
 write_data len 1123, time nopts, type header atom -
 write_data len 70, time 0, type boundary atom emsg
 write_data len 1832, time 0, type sync atom moof
 write_data len 70, time 2000000, type boundary atom emsg
 write_data len 1840, time 2000000, type sync atom moof
 write_data len 148, time nopts, type trailer atom -
-43f9d447aa759ec494e20d9c8d149488 5111 emsg
+782d5eb4c4bccaf442cf3a928b510409 5115 emsg
diff --git a/tests/ref/fate/mov-mp4-fragmented-ttml-stpp 
b/tests/ref/fate/mov-mp4-fragmented-ttml-stpp
index f1316833206..95f9d5bbfd7 100644
--- a/tests/ref/fate/mov-mp4-fragmented-ttml-stpp
+++ b/tests/ref/fate/mov-mp4-fragmented-ttml-stpp
@@ -1,5 +1,5 @@
-aceb3be7068511fe52ccb953fede8ecd 
*tests/data/fate/mov-mp4-fragmented-ttml-stpp.mp4
-183872 tests/data/fate/mov-mp4-fragmented-ttml-stpp.mp4
+2696266b1506f9537b59ac4aa490e596 
*tests/data/fate/mov-mp4-fragmented-ttml-stpp.mp4
+183876 tests/data/fate/mov-mp4-fragmented-ttml-stpp.mp4
 <?xml version="1.0" encoding="utf-8"?>
 <tt
   xmlns="http://www.w3.org/ns/ttml";
@@ -415,16 +415,16 @@ aceb3be7068511fe52ccb953fede8ecd 
*tests/data/fate/mov-mp4-fragmented-ttml-stpp.m
     </div>
   </body>
 </tt>
-packet,subtitle,1,0,0.000000,0,0.000000,2002,2.002000,1172,14302,K__,CRC32:bde7afa1
-packet,subtitle,1,2002,2.002000,2002,2.002000,2002,2.002000,1015,28648,K__,CRC32:bd3bd4bc
-packet,subtitle,1,4004,4.004000,4004,4.004000,2002,2.002000,1305,42837,K__,CRC32:c8ff5017
-packet,subtitle,1,6006,6.006000,6006,6.006000,2002,2.002000,1111,57316,K__,CRC32:f0b10da6
-packet,subtitle,1,8008,8.008000,8008,8.008000,2002,2.002000,927,71601,K__,CRC32:55a7bb1b
-packet,subtitle,1,10010,10.010000,10010,10.010000,2002,2.002000,1194,85702,K__,CRC32:03dc4753
-packet,subtitle,1,12012,12.012000,12012,12.012000,2002,2.002000,874,100070,K__,CRC32:c7a092d4
-packet,subtitle,1,14014,14.014000,14014,14.014000,2002,2.002000,1154,114118,K__,CRC32:b476fe9c
-packet,subtitle,1,16016,16.016000,16016,16.016000,2002,2.002000,1240,128446,K__,CRC32:912e694c
-packet,subtitle,1,18018,18.018000,18018,18.018000,2002,2.002000,960,142860,K__,CRC32:bba73912
-packet,subtitle,1,20020,20.020000,20020,20.020000,2002,2.002000,1251,156994,K__,CRC32:4c7b87d8
-packet,subtitle,1,22022,22.022000,22022,22.022000,2002,2.002000,1285,171419,K__,CRC32:c0368927
-packet,subtitle,1,24024,24.024000,24024,24.024000,501,0.501000,1676,181630,K__,CRC32:812a103f
+packet,subtitle,1,0,0.000000,0,0.000000,2002,2.002000,1172,14306,K__,CRC32:bde7afa1
+packet,subtitle,1,2002,2.002000,2002,2.002000,2002,2.002000,1015,28652,K__,CRC32:bd3bd4bc
+packet,subtitle,1,4004,4.004000,4004,4.004000,2002,2.002000,1305,42841,K__,CRC32:c8ff5017
+packet,subtitle,1,6006,6.006000,6006,6.006000,2002,2.002000,1111,57320,K__,CRC32:f0b10da6
+packet,subtitle,1,8008,8.008000,8008,8.008000,2002,2.002000,927,71605,K__,CRC32:55a7bb1b
+packet,subtitle,1,10010,10.010000,10010,10.010000,2002,2.002000,1194,85706,K__,CRC32:03dc4753
+packet,subtitle,1,12012,12.012000,12012,12.012000,2002,2.002000,874,100074,K__,CRC32:c7a092d4
+packet,subtitle,1,14014,14.014000,14014,14.014000,2002,2.002000,1154,114122,K__,CRC32:b476fe9c
+packet,subtitle,1,16016,16.016000,16016,16.016000,2002,2.002000,1240,128450,K__,CRC32:912e694c
+packet,subtitle,1,18018,18.018000,18018,18.018000,2002,2.002000,960,142864,K__,CRC32:bba73912
+packet,subtitle,1,20020,20.020000,20020,20.020000,2002,2.002000,1251,156998,K__,CRC32:4c7b87d8
+packet,subtitle,1,22022,22.022000,22022,22.022000,2002,2.002000,1285,171423,K__,CRC32:c0368927
+packet,subtitle,1,24024,24.024000,24024,24.024000,501,0.501000,1676,181634,K__,CRC32:812a103f
-- 
2.54.0

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

Reply via email to