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

At 30000/1001, PCM elements alternate 1602/1601 samples, so a constant element 
delta (and the CBR EditUnitByteCount) is wrong on some edit units. Shorter 
elements are now padded with KLV fill to a constant size, ST 377-1 11.1.4.

Depends on https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26151; only the last 
commit is new.

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



From 9650a1ddcf37a936ed574063f8d7b93f443656be Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Fri, 13 Nov 2026 16:22:33 -0800
Subject: [PATCH 1/6] avformat/mxfenc: do not index a system item in OPAtom

OPAtom has no system item, write a single delta entry at offset 0.

Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mxfenc.c                    | 25 +++++++++++++++++--------
 tests/ref/fate/mxf-opatom-user-comments |  2 +-
 tests/ref/lavf/mxf_opatom               |  2 +-
 tests/ref/lavf/mxf_opatom_audio         |  2 +-
 4 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index 66528d5e358..26325dfffc9 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -2011,6 +2011,12 @@ static unsigned klv_fill_size(uint64_t size)
         return pad & (KAG_SIZE-1);
 }
 
+static int mxf_index_delta_count(AVFormatContext *s)
+{
+    // one delta entry per element, plus the system item except in OPAtom
+    return s->nb_streams + !IS_OPATOM(s);
+}
+
 static int mxf_check_frame_offset(AVFormatContext *s, int offset)
 {
     if (offset >= INT8_MIN && offset <= INT8_MAX)
@@ -2081,13 +2087,15 @@ static int 
mxf_write_index_table_segment(AVFormatContext *s)
     avio_w8(pb, !mxf->edit_unit_byte_count); // only one slice for CBR
 
     // delta entry array
-    mxf_write_local_tag(s, 8 + (s->nb_streams+1)*6, 0x3F09);
-    avio_wb32(pb, s->nb_streams+1); // num of entries
+    mxf_write_local_tag(s, 8 + mxf_index_delta_count(s)*6, 0x3F09);
+    avio_wb32(pb, mxf_index_delta_count(s)); // num of entries
     avio_wb32(pb, 6);               // size of one entry
-    // write system item delta entry
-    avio_w8(pb, 0);
-    avio_w8(pb, 0); // slice entry
-    avio_wb32(pb, 0); // element delta
+    if (!IS_OPATOM(s)) {
+        // write system item delta entry
+        avio_w8(pb, 0);
+        avio_w8(pb, 0); // slice entry
+        avio_wb32(pb, 0); // element delta
+    }
     // write each stream delta entry
     for (i = 0; i < s->nb_streams; i++) {
         AVStream *st = s->streams[i];
@@ -2100,7 +2108,8 @@ static int mxf_write_index_table_segment(AVFormatContext 
*s)
             avio_wb32(pb, sc->slice_offset);
         } else if (i == 0) { // video track
             avio_w8(pb, 0); // slice number
-            avio_wb32(pb, KAG_SIZE); // system item size including klv fill
+            // system item size including klv fill, no system item in OPAtom
+            avio_wb32(pb, IS_OPATOM(s) ? 0 : KAG_SIZE);
         } else { // audio or data track
             if (!audio_frame_size) {
                 audio_frame_size = sc->frame_size;
@@ -2207,7 +2216,7 @@ static int mxf_write_partition(AVFormatContext *s, int 
bodysid,
     int err;
 
     if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
-        index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
+        index_byte_count = 85 + 12+mxf_index_delta_count(s)*6 +
             12+mxf->edit_units_count*15;
     else if (mxf->edit_unit_byte_count && indexsid)
         index_byte_count = 80;
diff --git a/tests/ref/fate/mxf-opatom-user-comments 
b/tests/ref/fate/mxf-opatom-user-comments
index 14a1cee1f26..66afabf2e24 100644
--- a/tests/ref/fate/mxf-opatom-user-comments
+++ b/tests/ref/fate/mxf-opatom-user-comments
@@ -1 +1 @@
-d40b64e492133c74faa07e605eb65b2f
+b3191be3ec3f84e5d756f86a51cc2b20
diff --git a/tests/ref/lavf/mxf_opatom b/tests/ref/lavf/mxf_opatom
index fa722418139..4e6cc51470f 100644
--- a/tests/ref/lavf/mxf_opatom
+++ b/tests/ref/lavf/mxf_opatom
@@ -1,3 +1,3 @@
-6418766ca9b8b23fae74a80d66f26f3e *tests/data/lavf/lavf.mxf_opatom
+491ec08f32738efd10ba2671746fae4d *tests/data/lavf/lavf.mxf_opatom
 4717625 tests/data/lavf/lavf.mxf_opatom
 tests/data/lavf/lavf.mxf_opatom CRC=0xb13ba2f8
diff --git a/tests/ref/lavf/mxf_opatom_audio b/tests/ref/lavf/mxf_opatom_audio
index 0e034991c00..58343a7a51f 100644
--- a/tests/ref/lavf/mxf_opatom_audio
+++ b/tests/ref/lavf/mxf_opatom_audio
@@ -1,3 +1,3 @@
-633b6d373009416f214edcf456208580 *tests/data/lavf/lavf.mxf_opatom_audio
+36bdc33f00f5014d1c276cd5438e9cf1 *tests/data/lavf/lavf.mxf_opatom_audio
 102969 tests/data/lavf/lavf.mxf_opatom_audio
 tests/data/lavf/lavf.mxf_opatom_audio CRC=0xd155c6ff
-- 
2.54.1


From abec2509bb424db5f344bade76ef7dbbeee3deb0 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Fri, 13 Nov 2026 16:23:07 -0800
Subject: [PATCH 2/6] avformat/mxfenc: include the KLV key and length in the
 element delta

Element deltas were missing the 20 byte key and length of the
preceding elements, accumulate the real element strides.

Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mxfenc.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index 26325dfffc9..ccb891646fd 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -2036,7 +2036,7 @@ static int mxf_write_index_table_segment(AVFormatContext 
*s)
     int i, j, temporal_reordering = 0;
     int key_index = mxf->last_key_index;
     int prev_non_b_picture = 0;
-    int audio_frame_size = 0;
+    unsigned element_delta = 0;
     int64_t pos;
     int err;
 
@@ -2111,12 +2111,11 @@ static int 
mxf_write_index_table_segment(AVFormatContext *s)
             // system item size including klv fill, no system item in OPAtom
             avio_wb32(pb, IS_OPATOM(s) ? 0 : KAG_SIZE);
         } else { // audio or data track
-            if (!audio_frame_size) {
-                audio_frame_size = sc->frame_size;
-                audio_frame_size += klv_fill_size(audio_frame_size);
-            }
             avio_w8(pb, 1);
-            avio_wb32(pb, (i-1)*audio_frame_size); // element delta
+            avio_wb32(pb, element_delta);
+            // each element is KAG aligned and starts with its 16 byte key and 
4 byte length
+            element_delta += 16 + 4 + sc->frame_size;
+            element_delta += klv_fill_size(element_delta);
         }
     }
 
-- 
2.54.1


From 8d80caabf1ddf39f46ae22a4affec9f6f5357dcd Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Fri, 13 Nov 2026 16:24:41 -0800
Subject: [PATCH 3/6] avformat/mxfenc: split index tables larger than 64 KiB

The IndexEntryArray 2 byte local length overflowed past 4368 entries,
segment the index table instead, as ST 377-1 11.2 recommends,
with every segment KAG aligned.

Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mxfenc.c                     | 127 ++++++++++++++---------
 tests/fate/mxf.mak                       |   7 ++
 tests/ref/fate/mxf-opatom-index-segments |   1 +
 3 files changed, 84 insertions(+), 51 deletions(-)
 create mode 100644 tests/ref/fate/mxf-opatom-index-segments

diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index ccb891646fd..803a724d1ee 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -2011,6 +2011,10 @@ static unsigned klv_fill_size(uint64_t size)
         return pad & (KAG_SIZE-1);
 }
 
+#define MXF_INDEX_ENTRY_SIZE 15
+// largest IndexEntryArray fitting a 2 byte local set length
+#define MXF_INDEX_MAX_ENTRIES ((UINT16_MAX - 8) / MXF_INDEX_ENTRY_SIZE)
+
 static int mxf_index_delta_count(AVFormatContext *s)
 {
     // one delta entry per element, plus the system item except in OPAtom
@@ -2029,30 +2033,28 @@ static int mxf_check_frame_offset(AVFormatContext *s, 
int offset)
     return AVERROR_PATCHWELCOME;
 }
 
-static int mxf_write_index_table_segment(AVFormatContext *s)
+// Fixed local tags and DeltaEntryArray, excluding the KLV key and length.
+static unsigned mxf_index_header_size(AVFormatContext *s)
+{
+    return 85 + 12 + 6 * mxf_index_delta_count(s);
+}
+
+static void mxf_write_index_table_segment_header(AVFormatContext *s, int64_t 
start, int duration)
 {
     MXFContext *mxf = s->priv_data;
     AVIOContext *pb = s->pb;
-    int i, j, temporal_reordering = 0;
-    int key_index = mxf->last_key_index;
-    int prev_non_b_picture = 0;
     unsigned element_delta = 0;
-    int64_t pos;
-    int err;
-
-    av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
-
-    if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
-        return 0;
+    unsigned size = mxf_index_header_size(s);
+    int i;
 
+    if (duration)
+        size += 12 + duration * MXF_INDEX_ENTRY_SIZE;
     avio_write(pb, index_table_segment_key, 16);
-
-    klv_encode_ber4_length(pb, 0);
-    pos = avio_tell(pb);
+    klv_encode_ber4_length(pb, size);
 
     // instance id
     mxf_write_local_tag(s, 16, 0x3C0A);
-    mxf_write_uuid(pb, IndexTableSegment, mxf->last_indexed_edit_unit);
+    mxf_write_uuid(pb, IndexTableSegment, start);
 
     // index edit rate
     mxf_write_local_tag(s, 8, 0x3F0B);
@@ -2061,14 +2063,11 @@ static int 
mxf_write_index_table_segment(AVFormatContext *s)
 
     // index start position
     mxf_write_local_tag(s, 8, 0x3F0C);
-    avio_wb64(pb, mxf->last_indexed_edit_unit);
+    avio_wb64(pb, start);
 
-    // index duration
+    // index duration, 0 if the index table covers whole container
     mxf_write_local_tag(s, 8, 0x3F0D);
-    if (mxf->edit_unit_byte_count)
-        avio_wb64(pb, 0); // index table covers whole container
-    else
-        avio_wb64(pb, mxf->edit_units_count);
+    avio_wb64(pb, duration);
 
     // edit unit byte count
     mxf_write_local_tag(s, 4, 0x3F05);
@@ -2101,8 +2100,6 @@ static int mxf_write_index_table_segment(AVFormatContext 
*s)
         AVStream *st = s->streams[i];
         MXFStreamContext *sc = st->priv_data;
         avio_w8(pb, sc->temporal_reordering);
-        if (sc->temporal_reordering)
-            temporal_reordering = 1;
         if (mxf->edit_unit_byte_count) {
             avio_w8(pb, 0); // slice number
             avio_wb32(pb, sc->slice_offset);
@@ -2118,23 +2115,61 @@ static int 
mxf_write_index_table_segment(AVFormatContext *s)
             element_delta += klv_fill_size(element_delta);
         }
     }
+}
 
-    if (!mxf->edit_unit_byte_count) {
+static void mxf_write_klv_fill(AVFormatContext *s)
+{
+    unsigned pad = klv_fill_size(avio_tell(s->pb));
+    if (pad) {
+        avio_write(s->pb, klv_fill_key, 16);
+        pad -= 16 + 4;
+        klv_encode_ber4_length(s->pb, pad);
+        ffio_fill(s->pb, 0, pad);
+        av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
+    }
+}
+
+static int mxf_write_index_table_segment(AVFormatContext *s)
+{
+    MXFContext *mxf = s->priv_data;
+    AVIOContext *pb = s->pb;
+    int i, j;
+    int key_index = mxf->last_key_index;
+    int prev_non_b_picture = 0;
+    int err;
+
+    av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
+
+    if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
+        return 0;
+
+    if (mxf->edit_unit_byte_count) {
+        mxf_write_index_table_segment_header(s, mxf->last_indexed_edit_unit, 
0);
+    } else {
         MXFStreamContext *sc = s->streams[0]->priv_data;
-        mxf_write_local_tag(s, 8 + mxf->edit_units_count*15, 0x3F0A);
-        avio_wb32(pb, mxf->edit_units_count);  // num of entries
-        avio_wb32(pb, 15);  // size of one entry
 
         for (i = 0; i < mxf->edit_units_count; i++) {
             int temporal_offset = 0;
 
+            // split the index table so that each segment fits 2 byte local 
set lengths
+            if (!(i % MXF_INDEX_MAX_ENTRIES)) {
+                int nb_entries = FFMIN(mxf->edit_units_count - i, 
MXF_INDEX_MAX_ENTRIES);
+                if (i)
+                    mxf_write_klv_fill(s);
+                mxf_write_index_table_segment_header(s, 
mxf->last_indexed_edit_unit + i,
+                                                     nb_entries);
+                mxf_write_local_tag(s, 8 + nb_entries*MXF_INDEX_ENTRY_SIZE, 
0x3F0A);
+                avio_wb32(pb, nb_entries);           // num of entries
+                avio_wb32(pb, MXF_INDEX_ENTRY_SIZE); // size of one entry
+            }
+
             if (!(mxf->index_entries[i].flags & 0x33)) { // I-frame
                 sc->max_gop = FFMAX(sc->max_gop, i - mxf->last_key_index);
                 mxf->last_key_index = key_index;
                 key_index = i;
             }
 
-            if (temporal_reordering) {
+            if (sc->temporal_reordering) {
                 int pic_num_in_gop = i - key_index;
                 if (pic_num_in_gop != mxf->index_entries[i].temporal_ref) {
                     for (j = key_index; j < mxf->edit_units_count; j++) {
@@ -2186,23 +2221,9 @@ static int mxf_write_index_table_segment(AVFormatContext 
*s)
         mxf->edit_units_count = 0;
     }
 
-    mxf_update_klv_size(pb, pos);
-
     return 0;
 }
 
-static void mxf_write_klv_fill(AVFormatContext *s)
-{
-    unsigned pad = klv_fill_size(avio_tell(s->pb));
-    if (pad) {
-        avio_write(s->pb, klv_fill_key, 16);
-        pad -= 16 + 4;
-        klv_encode_ber4_length(s->pb, pad);
-        ffio_fill(s->pb, 0, pad);
-        av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
-    }
-}
-
 static int mxf_write_partition(AVFormatContext *s, int bodysid,
                                 int indexsid,
                                 const uint8_t *key, int write_metadata)
@@ -2210,20 +2231,24 @@ static int mxf_write_partition(AVFormatContext *s, int 
bodysid,
     MXFContext *mxf = s->priv_data;
     AVIOContext *pb = s->pb;
     int64_t header_byte_count_offset;
-    unsigned index_byte_count = 0;
+    uint64_t index_byte_count = 0;
     uint64_t partition_offset = avio_tell(pb);
     int err;
 
-    if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
-        index_byte_count = 85 + 12+mxf_index_delta_count(s)*6 +
-            12+mxf->edit_units_count*15;
-    else if (mxf->edit_unit_byte_count && indexsid)
-        index_byte_count = 80;
+    if (!mxf->edit_unit_byte_count && mxf->edit_units_count) {
+        unsigned nb_segments = 1 + (mxf->edit_units_count - 1) / 
MXF_INDEX_MAX_ENTRIES;
+        // key and ber4 length, fixed sets, delta and index entry arrays per 
segment
+        unsigned segment_header_size = 16 + 4 + mxf_index_header_size(s) + 12;
+        unsigned full_segment_size = segment_header_size + 
MXF_INDEX_MAX_ENTRIES * MXF_INDEX_ENTRY_SIZE;
+        // segments are KAG aligned, the last one by the fill below
+        index_byte_count = (uint64_t)nb_segments * segment_header_size +
+                           (uint64_t)mxf->edit_units_count * 
MXF_INDEX_ENTRY_SIZE +
+                           (uint64_t)(nb_segments - 1) * 
klv_fill_size(full_segment_size);
+    } else if (mxf->edit_unit_byte_count && indexsid)
+        index_byte_count = 16 + 4 + 80;
 
-    if (index_byte_count) {
-        index_byte_count += 16 + 4; // add encoded ber4 length
+    if (index_byte_count)
         index_byte_count += klv_fill_size(index_byte_count);
-    }
 
     if (key && !memcmp(key, body_partition_key, 16)) {
         if ((err = av_reallocp_array(&mxf->body_partition_offset, 
mxf->body_partitions_count + 1,
diff --git a/tests/fate/mxf.mak b/tests/fate/mxf.mak
index 615b24c1b6a..d448b532582 100644
--- a/tests/fate/mxf.mak
+++ b/tests/fate/mxf.mak
@@ -85,3 +85,10 @@ FATE_SAMPLES_FFMPEG_FFPROBE += $(FATE_MXF_FFMPEG_FFPROBE-yes)
 FATE_SAMPLES_FFPROBE += $(FATE_MXF_PROBE-yes)
 
 fate-mxf: $(FATE_MXF-yes) $(FATE_MXF_PROBE-yes) $(FATE_MXF_FFMPEG_FFPROBE-yes)
+
+# Cross the 64 KiB IndexEntryArray limit, so that the index table is split.
+FATE_MXF_INDEX-$(call ENCMUX, MPEG2VIDEO, MXF_OPATOM, COLOR_FILTER 
FILE_PROTOCOL) += fate-mxf-opatom-index-segments
+fate-mxf-opatom-index-segments: CMD = md5 -filter_complex color=s=64x64:r=25 
-frames:v 6000 -c:v mpeg2video -g 12 -bf 2 -threads 1 -dct fastint -flags 
+bitexact -fflags +bitexact -f mxf_opatom
+
+FATE_FFMPEG += $(FATE_MXF_INDEX-yes)
+fate-mxf: $(FATE_MXF_INDEX-yes)
diff --git a/tests/ref/fate/mxf-opatom-index-segments 
b/tests/ref/fate/mxf-opatom-index-segments
new file mode 100644
index 00000000000..c58e910b937
--- /dev/null
+++ b/tests/ref/fate/mxf-opatom-index-segments
@@ -0,0 +1 @@
+8eccc2a849a76650c763118c4281f095
-- 
2.54.1


From ecbd0549a6df702979efa8b164534c8288bbb1c5 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Fri, 13 Nov 2026 16:38:36 -0800
Subject: [PATCH 4/6] avformat/mxfenc: compute the CBE IndexByteCount

It was hardcoded and too small with many delta entries.

Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mxfenc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index 803a724d1ee..b5c8bbb2925 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -2245,7 +2245,7 @@ static int mxf_write_partition(AVFormatContext *s, int 
bodysid,
                            (uint64_t)mxf->edit_units_count * 
MXF_INDEX_ENTRY_SIZE +
                            (uint64_t)(nb_segments - 1) * 
klv_fill_size(full_segment_size);
     } else if (mxf->edit_unit_byte_count && indexsid)
-        index_byte_count = 16 + 4 + 80;
+        index_byte_count = 16 + 4 + mxf_index_header_size(s);
 
     if (index_byte_count)
         index_byte_count += klv_fill_size(index_byte_count);
-- 
2.54.1


From 18bdb37b9fb2743408a65cc59b7b3a55639cb498 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Fri, 13 Nov 2026 17:34:34 -0800
Subject: [PATCH 5/6] avformat/mxfenc: do not write index slices when the
 picture is the last element

There should be no slices then, ST 377-1 11.1.4.

Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mxfenc.c                     | 45 +++++++++++++++---------
 tests/ref/fate/mxf-opatom-index-segments |  2 +-
 tests/ref/fate/mxf-opatom-user-comments  |  2 +-
 tests/ref/fate/time_base                 |  2 +-
 tests/ref/lavf/mxf_ffv1                  |  4 +--
 5 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index b5c8bbb2925..7c2407bafad 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -471,7 +471,6 @@ typedef struct MXFContext {
     MXFIndexEntry *index_entries;
     unsigned edit_units_count;
     uint64_t timestamp;   ///< timestamp, as 
year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
-    uint8_t slice_count;  ///< index slice count minus 1 (1 if no audio, 0 
otherwise)
     int last_indexed_edit_unit;
     uint64_t *body_partition_offset;
     unsigned body_partitions_count;
@@ -2011,9 +2010,26 @@ static unsigned klv_fill_size(uint64_t size)
         return pad & (KAG_SIZE-1);
 }
 
-#define MXF_INDEX_ENTRY_SIZE 15
+/**
+ * Number of slices minus 1. Elements following the VBE picture are in slice 1,
+ * there should be no slices if the picture is the last element, ST 377-1 
11.1.4.
+ */
+static int mxf_index_slice_count(AVFormatContext *s)
+{
+    MXFContext *mxf = s->priv_data;
+    return !mxf->edit_unit_byte_count && s->nb_streams > 1;
+}
+
+static int mxf_index_entry_size(AVFormatContext *s)
+{
+    return 11 + 4 * mxf_index_slice_count(s);
+}
+
 // largest IndexEntryArray fitting a 2 byte local set length
-#define MXF_INDEX_MAX_ENTRIES ((UINT16_MAX - 8) / MXF_INDEX_ENTRY_SIZE)
+static int mxf_index_max_entries(AVFormatContext *s)
+{
+    return (UINT16_MAX - 8) / mxf_index_entry_size(s);
+}
 
 static int mxf_index_delta_count(AVFormatContext *s)
 {
@@ -2048,7 +2064,7 @@ static void 
mxf_write_index_table_segment_header(AVFormatContext *s, int64_t sta
     int i;
 
     if (duration)
-        size += 12 + duration * MXF_INDEX_ENTRY_SIZE;
+        size += 12 + duration * mxf_index_entry_size(s);
     avio_write(pb, index_table_segment_key, 16);
     klv_encode_ber4_length(pb, size);
 
@@ -2083,7 +2099,7 @@ static void 
mxf_write_index_table_segment_header(AVFormatContext *s, int64_t sta
 
     // real slice count - 1
     mxf_write_local_tag(s, 1, 0x3F08);
-    avio_w8(pb, !mxf->edit_unit_byte_count); // only one slice for CBR
+    avio_w8(pb, mxf_index_slice_count(s));
 
     // delta entry array
     mxf_write_local_tag(s, 8 + mxf_index_delta_count(s)*6, 0x3F09);
@@ -2152,15 +2168,15 @@ static int 
mxf_write_index_table_segment(AVFormatContext *s)
             int temporal_offset = 0;
 
             // split the index table so that each segment fits 2 byte local 
set lengths
-            if (!(i % MXF_INDEX_MAX_ENTRIES)) {
-                int nb_entries = FFMIN(mxf->edit_units_count - i, 
MXF_INDEX_MAX_ENTRIES);
+            if (!(i % mxf_index_max_entries(s))) {
+                int nb_entries = FFMIN(mxf->edit_units_count - i, 
mxf_index_max_entries(s));
                 if (i)
                     mxf_write_klv_fill(s);
                 mxf_write_index_table_segment_header(s, 
mxf->last_indexed_edit_unit + i,
                                                      nb_entries);
-                mxf_write_local_tag(s, 8 + nb_entries*MXF_INDEX_ENTRY_SIZE, 
0x3F0A);
+                mxf_write_local_tag(s, 8 + nb_entries*mxf_index_entry_size(s), 
0x3F0A);
                 avio_wb32(pb, nb_entries);           // num of entries
-                avio_wb32(pb, MXF_INDEX_ENTRY_SIZE); // size of one entry
+                avio_wb32(pb, mxf_index_entry_size(s)); // size of one entry
             }
 
             if (!(mxf->index_entries[i].flags & 0x33)) { // I-frame
@@ -2210,10 +2226,8 @@ static int mxf_write_index_table_segment(AVFormatContext 
*s)
             avio_w8(pb, mxf->index_entries[i].flags);
             // stream offset
             avio_wb64(pb, mxf->index_entries[i].offset);
-            if (s->nb_streams > 1)
+            if (mxf_index_slice_count(s))
                 avio_wb32(pb, mxf->index_entries[i].slice_offset);
-            else
-                avio_wb32(pb, 0);
         }
 
         mxf->last_key_index = key_index - mxf->edit_units_count;
@@ -2236,13 +2250,13 @@ static int mxf_write_partition(AVFormatContext *s, int 
bodysid,
     int err;
 
     if (!mxf->edit_unit_byte_count && mxf->edit_units_count) {
-        unsigned nb_segments = 1 + (mxf->edit_units_count - 1) / 
MXF_INDEX_MAX_ENTRIES;
+        unsigned nb_segments = 1 + (mxf->edit_units_count - 1) / 
mxf_index_max_entries(s);
         // key and ber4 length, fixed sets, delta and index entry arrays per 
segment
         unsigned segment_header_size = 16 + 4 + mxf_index_header_size(s) + 12;
-        unsigned full_segment_size = segment_header_size + 
MXF_INDEX_MAX_ENTRIES * MXF_INDEX_ENTRY_SIZE;
+        unsigned full_segment_size = segment_header_size + 
mxf_index_max_entries(s) * mxf_index_entry_size(s);
         // segments are KAG aligned, the last one by the fill below
         index_byte_count = (uint64_t)nb_segments * segment_header_size +
-                           (uint64_t)mxf->edit_units_count * 
MXF_INDEX_ENTRY_SIZE +
+                           (uint64_t)mxf->edit_units_count * 
mxf_index_entry_size(s) +
                            (uint64_t)(nb_segments - 1) * 
klv_fill_size(full_segment_size);
     } else if (mxf->edit_unit_byte_count && indexsid)
         index_byte_count = 16 + 4 + mxf_index_header_size(s);
@@ -3121,7 +3135,6 @@ static int mxf_init(AVFormatContext *s)
                 mxf->edit_unit_byte_count = 
(av_get_bits_per_sample(st->codecpar->codec_id) * 
st->codecpar->ch_layout.nb_channels) >> 3;
                 sc->index = INDEX_WAV;
             } else {
-                mxf->slice_count = 1;
                 sc->frame_size = st->codecpar->ch_layout.nb_channels *
                                  av_rescale_rnd(st->codecpar->sample_rate, 
mxf->time_base.num, mxf->time_base.den, AV_ROUND_UP) *
                                  
av_get_bits_per_sample(st->codecpar->codec_id) / 8;
diff --git a/tests/ref/fate/mxf-opatom-index-segments 
b/tests/ref/fate/mxf-opatom-index-segments
index c58e910b937..68b7036306f 100644
--- a/tests/ref/fate/mxf-opatom-index-segments
+++ b/tests/ref/fate/mxf-opatom-index-segments
@@ -1 +1 @@
-8eccc2a849a76650c763118c4281f095
+c1c66a39c4d903d52b171b052899ff24
diff --git a/tests/ref/fate/mxf-opatom-user-comments 
b/tests/ref/fate/mxf-opatom-user-comments
index 66afabf2e24..b60aae9afd9 100644
--- a/tests/ref/fate/mxf-opatom-user-comments
+++ b/tests/ref/fate/mxf-opatom-user-comments
@@ -1 +1 @@
-b3191be3ec3f84e5d756f86a51cc2b20
+8df1c62368ab861010a0e378c12c154c
diff --git a/tests/ref/fate/time_base b/tests/ref/fate/time_base
index ae96232e60c..04129b300f4 100644
--- a/tests/ref/fate/time_base
+++ b/tests/ref/fate/time_base
@@ -1 +1 @@
-69ffc45e19ab070bc3e964d7b718fe53
+479107911a1a90d03422cf5a7660a23d
diff --git a/tests/ref/lavf/mxf_ffv1 b/tests/ref/lavf/mxf_ffv1
index 4ae93b2e892..20bf8115856 100644
--- a/tests/ref/lavf/mxf_ffv1
+++ b/tests/ref/lavf/mxf_ffv1
@@ -1,3 +1,3 @@
-67638015788bb7dfcc1c16183967b513 *tests/data/lavf/lavf.mxf_ffv1
-5587001 tests/data/lavf/lavf.mxf_ffv1
+bb73cbf1750bebd278633e4ac3752a48 *tests/data/lavf/lavf.mxf_ffv1
+5586489 tests/data/lavf/lavf.mxf_ffv1
 tests/data/lavf/lavf.mxf_ffv1 CRC=0x02354cdc
-- 
2.54.1


From 71a21bea474658c1e36a3937e716a4fa2c8ff8f6 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sat, 21 Nov 2026 11:39:15 -0800
Subject: [PATCH 6/6] avformat/mxfenc: pad PCM elements to a constant size

At rates like 30000/1001 the PCM elements are one sample shorter on some
edit units, which broke the element deltas and edit unit byte count.

Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavformat/mxfenc.c                 |  57 +++++++++--
 tests/fate/mxf.mak                   |   9 ++
 tests/ref/fate/mxf-pcm-element-delta | 144 +++++++++++++++++++++++++++
 3 files changed, 200 insertions(+), 10 deletions(-)
 create mode 100644 tests/ref/fate/mxf-pcm-element-delta

diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index 7c2407bafad..d006c375af7 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -115,6 +115,7 @@ typedef struct MXFStreamContext {
     int video_bit_rate;
     int slice_offset;
     int frame_size;          ///< frame size in bytes
+    int element_size;        ///< KAG aligned PCM element size, shorter 
elements are padded to it
     int seq_closed_gop;      ///< all gops in sequence are closed, used in 
mpeg-2 descriptor
     int max_gop;             ///< maximum gop size, used by mpeg-2 descriptor
     int b_picture_count;     ///< maximum number of consecutive b pictures, 
used in mpeg-2 descriptor
@@ -2010,6 +2011,14 @@ static unsigned klv_fill_size(uint64_t size)
         return pad & (KAG_SIZE-1);
 }
 
+// size of a KAG aligned element, including its 16 byte key and 4 byte length
+static unsigned mxf_element_size(const MXFStreamContext *sc)
+{
+    if (sc->element_size)
+        return sc->element_size;
+    return 16 + 4 + sc->frame_size + klv_fill_size(16 + 4 + sc->frame_size);
+}
+
 /**
  * Number of slices minus 1. Elements following the VBE picture are in slice 1,
  * there should be no slices if the picture is the last element, ST 377-1 
11.1.4.
@@ -2126,21 +2135,24 @@ static void 
mxf_write_index_table_segment_header(AVFormatContext *s, int64_t sta
         } else { // audio or data track
             avio_w8(pb, 1);
             avio_wb32(pb, element_delta);
-            // each element is KAG aligned and starts with its 16 byte key and 
4 byte length
-            element_delta += 16 + 4 + sc->frame_size;
-            element_delta += klv_fill_size(element_delta);
+            element_delta += mxf_element_size(sc);
         }
     }
 }
 
+static void mxf_write_fill(AVIOContext *pb, unsigned size)
+{
+    avio_write(pb, klv_fill_key, 16);
+    size -= 16 + 4;
+    klv_encode_ber4_length(pb, size);
+    ffio_fill(pb, 0, size);
+}
+
 static void mxf_write_klv_fill(AVFormatContext *s)
 {
     unsigned pad = klv_fill_size(avio_tell(s->pb));
     if (pad) {
-        avio_write(s->pb, klv_fill_key, 16);
-        pad -= 16 + 4;
-        klv_encode_ber4_length(s->pb, pad);
-        ffio_fill(s->pb, 0, pad);
+        mxf_write_fill(s->pb, pad);
         av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
     }
 }
@@ -3138,6 +3150,14 @@ static int mxf_init(AVFormatContext *s)
                 sc->frame_size = st->codecpar->ch_layout.nb_channels *
                                  av_rescale_rnd(st->codecpar->sample_rate, 
mxf->time_base.num, mxf->time_base.den, AV_ROUND_UP) *
                                  
av_get_bits_per_sample(st->codecpar->codec_id) / 8;
+                // Elements are one sample shorter on some edit units at rates 
like 30000/1001.
+                // They are padded with KLV fill to the size of the longest, 
so that the
+                // element deltas and edit unit byte count are constant, ST 
377-1 11.1.4.
+                sc->element_size = 16 + 4 + sc->frame_size;
+                sc->element_size += klv_fill_size(sc->element_size);
+                if (sc->element_size == 16 + 4 + sc->frame_size &&
+                    (int64_t)st->codecpar->sample_rate * mxf->time_base.num % 
mxf->time_base.den)
+                    sc->element_size += KAG_SIZE; // room for the fill of 
shorter elements
             }
             snprintf(bsf_arg, sizeof(bsf_arg), "r=%d/%d", mxf->tc.rate.num, 
mxf->tc.rate.den);
             ret = ff_stream_add_bitstream_filter(st, "pcm_rechunk", bsf_arg);
@@ -3380,8 +3400,7 @@ static void 
mxf_compute_edit_unit_byte_count(AVFormatContext *s)
         AVStream *st = s->streams[i];
         MXFStreamContext *sc = st->priv_data;
         sc->slice_offset = mxf->edit_unit_byte_count;
-        mxf->edit_unit_byte_count += 16 + 4 + sc->frame_size;
-        mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
+        mxf->edit_unit_byte_count += mxf_element_size(sc);
     }
 }
 
@@ -3392,7 +3411,7 @@ static int mxf_write_packet(AVFormatContext *s, AVPacket 
*pkt)
     AVStream *st = s->streams[pkt->stream_index];
     MXFStreamContext *sc = st->priv_data;
     MXFIndexEntry ie = {0};
-    int err;
+    int err, pad = -1;
 
     if (!mxf->header_written && pkt->stream_index != 0 &&
         !IS_OPATOM(s)) {
@@ -3507,10 +3526,28 @@ static int mxf_write_packet(AVFormatContext *s, 
AVPacket *pkt)
             mxf->body_offset - 
mxf->index_entries[mxf->edit_units_count-1].offset;
     }
 
+    // pad PCM to a constant size when another element of the edit unit follows
+    // or with a constant edit unit byte count
+    if (sc->element_size &&
+        (mxf->edit_unit_byte_count || st->index < s->nb_streams - 1)) {
+        pad = sc->element_size - (16 + 4 + pkt->size);
+        if (pkt->size > sc->frame_size || (pad && pad < 20)) {
+            av_log(s, AV_LOG_ERROR, "track %d: packet size %d does not match 
element size %d\n",
+                   st->index, pkt->size, sc->element_size);
+            return AVERROR(EINVAL);
+        }
+    }
+
     mxf_write_klv_fill(s);
     avio_write(pb, sc->track_essence_element_key, 16); // write key
     if (IS_D10(s) && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
         mxf_write_d10_audio_packet(s, st, pkt);
+    } else if (pad >= 0) {
+        klv_encode_ber4_length(pb, pkt->size);
+        avio_write(pb, pkt->data, pkt->size);
+        if (pad)
+            mxf_write_fill(pb, pad);
+        mxf->body_offset += sc->element_size;
     } else {
         klv_encode_ber4_length(pb, pkt->size); // write length
         avio_write(pb, pkt->data, pkt->size);
diff --git a/tests/fate/mxf.mak b/tests/fate/mxf.mak
index d448b532582..c13dbc30312 100644
--- a/tests/fate/mxf.mak
+++ b/tests/fate/mxf.mak
@@ -92,3 +92,12 @@ fate-mxf-opatom-index-segments: CMD = md5 -filter_complex 
color=s=64x64:r=25 -fr
 
 FATE_FFMPEG += $(FATE_MXF_INDEX-yes)
 fate-mxf: $(FATE_MXF_INDEX-yes)
+
+# PCM elements alternate 1602 and 1601 samples at 30000/1001, the shorter ones 
are
+# padded with KLV fill so that the element delta of the next track is constant.
+FATE_MXF_INDEX_FFPROBE-$(call REMUX, MXF, LAVFI_INDEV TESTSRC2_FILTER 
ANULLSRC_FILTER AFORMAT_FILTER MPEG2VIDEO_ENCODER PCM_S24LE_ENCODER \
+                                          PCM_RECHUNK_BSF 
WRAPPED_AVFRAME_DECODER MPEGVIDEO_PARSER EXTRACT_EXTRADATA_BSF) += 
fate-mxf-pcm-element-delta
+fate-mxf-pcm-element-delta: CMD = transcode lavfi 
"testsrc2=s=64x64:r=30000/1001:d=0.2[out0];anullsrc=r=48000:cl=5.0:d=0.2,aformat=s32[out1];anullsrc=r=48000:cl=5.0:d=0.2,aformat=s32[out2]"
 mxf "-map 0 -c:v mpeg2video -threads 1 -dct fastint -c:a pcm_s24le" "-map 0 -c 
copy" "-show_entries packet=stream_index,pts,pos,size"
+
+FATE_FFMPEG_FFPROBE += $(FATE_MXF_INDEX_FFPROBE-yes)
+fate-mxf: $(FATE_MXF_INDEX_FFPROBE-yes)
diff --git a/tests/ref/fate/mxf-pcm-element-delta 
b/tests/ref/fate/mxf-pcm-element-delta
new file mode 100644
index 00000000000..0e6301828c3
--- /dev/null
+++ b/tests/ref/fate/mxf-pcm-element-delta
@@ -0,0 +1,144 @@
+82c7b4dbbd21556368fcee672b1f21f3 *tests/data/fate/mxf-pcm-element-delta.mxf
+316985 tests/data/fate/mxf-pcm-element-delta.mxf
+#extradata 0:       22, 0x41660557
+#tb 0: 1001/30000
+#media_type 0: video
+#codec_id 0: mpeg2video
+#dimensions 0: 64x64
+#sar 0: 1/1
+#tb 1: 1/48000
+#media_type 1: audio
+#codec_id 1: pcm_s24le
+#sample_rate 1: 48000
+#channel_layout_name 1: 5.0
+#tb 2: 1/48000
+#media_type 2: audio
+#codec_id 2: pcm_s24le
+#sample_rate 2: 48000
+#channel_layout_name 2: 5.0
+0,         -1,          0,        1,     2748, 0x4b60e874
+0,          0,          1,        1,     1604, 0x999e6cb6, F=0x0
+1,          0,          0,     1602,    24030, 0x00000000
+2,          0,          0,     1602,    24030, 0x00000000
+0,          1,          2,        1,     1253, 0x75a8e24b, F=0x0
+1,       1602,       1602,     1601,    24015, 0x00000000
+2,       1602,       1602,     1601,    24015, 0x00000000
+1,       3203,       3203,     1602,    24030, 0x00000000
+2,       3203,       3203,     1602,    24030, 0x00000000
+0,          2,          3,        1,      876, 0xd1dc44db, F=0x0
+0,          3,          4,        1,     1693, 0xf513615d, F=0x0
+1,       4805,       4805,     1601,    24015, 0x00000000
+2,       4805,       4805,     1601,    24015, 0x00000000
+1,       6406,       6406,     1602,    24030, 0x00000000
+2,       6406,       6406,     1602,    24030, 0x00000000
+0,          4,          5,        1,     1259, 0x5e95de9a, F=0x0
+1,       8008,       8008,     1602,    24030, 0x00000000
+2,       8008,       8008,     1602,    24030, 0x00000000
+[PACKET]
+stream_index=0
+pts=0
+size=2748
+pos=8192
+[/PACKET]
+[PACKET]
+stream_index=1
+pts=0
+size=24030
+pos=11264
+[/PACKET]
+[PACKET]
+stream_index=2
+pts=0
+size=24030
+pos=35840
+[/PACKET]
+[PACKET]
+stream_index=0
+pts=1
+size=1604
+pos=60928
+[/PACKET]
+[PACKET]
+stream_index=1
+pts=1602
+size=24015
+pos=62976
+[/PACKET]
+[PACKET]
+stream_index=2
+pts=1602
+size=24015
+pos=87552
+[/PACKET]
+[PACKET]
+stream_index=0
+pts=2
+size=1253
+pos=112128
+[/PACKET]
+[PACKET]
+stream_index=1
+pts=3203
+size=24030
+pos=113664
+[/PACKET]
+[PACKET]
+stream_index=2
+pts=3203
+size=24030
+pos=138240
+[/PACKET]
+[PACKET]
+stream_index=0
+pts=3
+size=876
+pos=163328
+[/PACKET]
+[PACKET]
+stream_index=1
+pts=4805
+size=24015
+pos=164352
+[/PACKET]
+[PACKET]
+stream_index=2
+pts=4805
+size=24015
+pos=188928
+[/PACKET]
+[PACKET]
+stream_index=0
+pts=4
+size=1693
+pos=213504
+[/PACKET]
+[PACKET]
+stream_index=1
+pts=6406
+size=24030
+pos=215552
+[/PACKET]
+[PACKET]
+stream_index=2
+pts=6406
+size=24030
+pos=240128
+[/PACKET]
+[PACKET]
+stream_index=0
+pts=5
+size=1259
+pos=265216
+[/PACKET]
+[PACKET]
+stream_index=1
+pts=8008
+size=24030
+pos=266752
+[/PACKET]
+[PACKET]
+stream_index=2
+pts=8008
+size=24030
+pos=291328
+[/PACKET]
-- 
2.54.1

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

Reply via email to