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

Resolve writes an additional index entry at the end of clip-wrapped
JPEG 2000 essence. Its StreamOffset equals the essence KLV value length.
Using that entry to size the final packet currently logs a partial-file
error before retrying with the same endpoint.

Allow the final VBE index entry to resolve to the exact BodySID endpoint
when sizing a clip-wrapped packet. Resolve the exclusive index end
directly when sizing packets and keep invalid offsets on the error path.
Seek lookups still require an offset inside the essence.

Verified with three Resolve OP-Atom files and three OP1a controls:
90 unchanged decoded frames each. An offset one byte past the essence
still logs an error, and actual truncation still marks a packet corrupt.
All 17 enabled fate-mxf tests pass.



>From 7f09ba2af587007fb1a3eb63bf197d94904f951c Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Wed, 18 Nov 2026 20:45:25 -0800
Subject: [PATCH] avformat/mxfdec: handle clip-wrapped essence end offsets

Resolve writes an additional index entry at the end of clip-wrapped
JPEG 2000 essence. Its StreamOffset equals the essence KLV value length.
Using that entry to size the final packet currently logs a partial-file
error before retrying with the same endpoint.

Allow the final VBE index entry to resolve to the exact BodySID endpoint
when sizing a clip-wrapped packet. Resolve the exclusive index end
directly when sizing packets and keep invalid offsets on the error path.
Seek lookups still require an offset inside the essence.

Verified with three Resolve OP-Atom files and three OP1a controls:
90 unchanged decoded frames each. An offset one byte past the essence
still logs an error, and actual truncation still marks a packet corrupt.
All 17 enabled fate-mxf tests pass.
---
 libavformat/mxfdec.c | 56 ++++++++++++++++++++++++++++++++------------
 1 file changed, 41 insertions(+), 15 deletions(-)

diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c
index a47a01812f1..157a2bd7a79 100644
--- a/libavformat/mxfdec.c
+++ b/libavformat/mxfdec.c
@@ -1868,10 +1868,12 @@ static int mxf_get_sorted_table_segments(MXFContext 
*mxf, int *nb_sorted_segment
     return 0;
 }
 
+static int64_t mxf_essence_container_end(MXFContext *mxf, int body_sid);
+
 /**
  * Computes the absolute file offset of the given essence container offset
  */
-static int mxf_absolute_bodysid_offset(MXFContext *mxf, int body_sid, int64_t 
offset, int64_t *offset_out, MXFPartition **partition_out)
+static int mxf_absolute_bodysid_offset(MXFContext *mxf, int body_sid, int64_t 
offset, int64_t *offset_out, MXFPartition **partition_out, int allow_end)
 {
     MXFPartition *last_p = NULL;
     int a, b, m, m0;
@@ -1897,11 +1899,17 @@ static int mxf_absolute_bodysid_offset(MXFContext *mxf, 
int body_sid, int64_t of
     if (a >= 0)
         last_p = &mxf->partitions[a];
 
-    if (last_p && (!last_p->essence_length || last_p->essence_length > (offset 
- last_p->body_offset))) {
-        *offset_out = last_p->essence_offset + (offset - last_p->body_offset);
-        if (partition_out)
-            *partition_out = last_p;
-        return 0;
+    if (last_p) {
+        int64_t relative_offset = offset - last_p->body_offset;
+
+        if (!last_p->essence_length || relative_offset < 
last_p->essence_length ||
+            (allow_end && relative_offset == last_p->essence_length &&
+             last_p->essence_offset + relative_offset == 
mxf_essence_container_end(mxf, body_sid))) {
+            *offset_out = last_p->essence_offset + relative_offset;
+            if (partition_out)
+                *partition_out = last_p;
+            return 0;
+        }
     }
 
     av_log(mxf->fc, AV_LOG_ERROR,
@@ -1931,8 +1939,15 @@ static int64_t mxf_essence_container_end(MXFContext 
*mxf, int body_sid)
     return 0;
 }
 
+enum MXFOffsetUse {
+    MXF_OFFSET_SEARCH,
+    MXF_OFFSET_SEEK,
+    MXF_OFFSET_PACKET_END,
+    MXF_OFFSET_CLIP_PACKET_END,
+};
+
 /* EditUnit -> absolute offset */
-static int mxf_edit_unit_absolute_offset(MXFContext *mxf, MXFIndexTable 
*index_table, int64_t edit_unit, AVRational edit_rate, int64_t *edit_unit_out, 
int64_t *offset_out, MXFPartition **partition_out, int nag)
+static int mxf_edit_unit_absolute_offset(MXFContext *mxf, MXFIndexTable 
*index_table, int64_t edit_unit, AVRational edit_rate, int64_t *edit_unit_out, 
int64_t *offset_out, MXFPartition **partition_out, enum MXFOffsetUse use)
 {
     int i = 0, dir = 0;
     int64_t index_duration, index_end;
@@ -1954,6 +1969,15 @@ static int mxf_edit_unit_absolute_offset(MXFContext 
*mxf, MXFIndexTable *index_t
     if (edit_unit < 0)
         return AVERROR_PATCHWELCOME;
 
+    if (edit_unit == index_end &&
+        (use == MXF_OFFSET_PACKET_END || use == MXF_OFFSET_CLIP_PACKET_END)) {
+        *offset_out = mxf_essence_container_end(mxf, index_table->body_sid);
+        if (*offset_out > 0)
+            return 0;
+        av_log(mxf->fc, AV_LOG_ERROR, "unable to compute the size of the last 
packet\n");
+        return AVERROR_INVALIDDATA;
+    }
+
     // guess which table segment this edit unit is in
     // saturation is fine since it's just a guess
     // if the guess is wrong we revert to a linear search
@@ -1997,7 +2021,11 @@ static int mxf_edit_unit_absolute_offset(MXFContext 
*mxf, MXFIndexTable *index_t
             if (edit_unit_out)
                 *edit_unit_out = av_rescale_q(edit_unit, edit_rate, 
s->index_edit_rate);
 
-            return mxf_absolute_bodysid_offset(mxf, index_table->body_sid, 
offset_temp, offset_out, partition_out);
+            /* Some clip-wrapped VBE indexes include the end of the last
+             * packet as an extra entry. Accept it only when sizing a packet. 
*/
+            return mxf_absolute_bodysid_offset(mxf, index_table->body_sid, 
offset_temp, offset_out, partition_out,
+                                               use == 
MXF_OFFSET_CLIP_PACKET_END && !s->edit_unit_byte_count &&
+                                               s == last_segment && edit_unit 
== index_end - 1);
         } else if (dir == 0) {
             // scan backwards if the segment is earlier than the current 
IndexStartPosition
             // else scan forwards
@@ -2009,7 +2037,7 @@ static int mxf_edit_unit_absolute_offset(MXFContext *mxf, 
MXFIndexTable *index_t
         }
     }
 
-    if (nag)
+    if (use == MXF_OFFSET_SEEK)
         av_log(mxf->fc, AV_LOG_ERROR, "failed to map EditUnit %"PRId64" in 
IndexSID %i to an offset\n", edit_unit, index_table->index_sid);
 
     return AVERROR_INVALIDDATA;
@@ -3948,7 +3976,7 @@ static int mxf_get_next_track_edit_unit(MXFContext *mxf, 
MXFTrack *track, int64_
     b = track->original_duration;
     while (b - 1 > a) {
         m = (a + (uint64_t)b) >> 1;
-        if (mxf_edit_unit_absolute_offset(mxf, t, m, track->edit_rate, NULL, 
&offset, NULL, 0) < 0)
+        if (mxf_edit_unit_absolute_offset(mxf, t, m, track->edit_rate, NULL, 
&offset, NULL, MXF_OFFSET_SEARCH) < 0)
             return -1;
         if (offset < current_offset)
             a = m;
@@ -4003,11 +4031,9 @@ static int64_t mxf_set_current_edit_unit(MXFContext 
*mxf, AVStream *st, int64_t
     if (!t || track->wrapping == UnknownWrapped || edit_unit > INT64_MAX - 
track->edit_units_per_packet)
         return -1;
 
-    if (mxf_edit_unit_absolute_offset(mxf, t, edit_unit + 
track->edit_units_per_packet, track->edit_rate, NULL, &next_ofs, NULL, 0) < 0 &&
-        (next_ofs = mxf_essence_container_end(mxf, t->body_sid)) <= 0) {
-        av_log(mxf->fc, AV_LOG_ERROR, "unable to compute the size of the last 
packet\n");
+    if (mxf_edit_unit_absolute_offset(mxf, t, edit_unit + 
track->edit_units_per_packet, track->edit_rate, NULL, &next_ofs, NULL,
+                                      track->wrapping == ClipWrapped ? 
MXF_OFFSET_CLIP_PACKET_END : MXF_OFFSET_PACKET_END) < 0)
         return -1;
-    }
 
     /* check if the next edit unit offset (next_ofs) starts ahead of 
current_offset */
     if (next_ofs > current_offset)
@@ -4351,7 +4377,7 @@ static int mxf_read_seek(AVFormatContext *s, int 
stream_index, int64_t sample_ti
         if (source_track->wrapping == UnknownWrapped)
             av_log(mxf->fc, AV_LOG_WARNING, "attempted seek in an 
UnknownWrapped essence\n");
 
-        if ((ret = mxf_edit_unit_absolute_offset(mxf, t, sample_time, 
source_track->edit_rate, &sample_time, &seekpos, &partition, 1)) < 0)
+        if ((ret = mxf_edit_unit_absolute_offset(mxf, t, sample_time, 
source_track->edit_rate, &sample_time, &seekpos, &partition, MXF_OFFSET_SEEK)) 
< 0)
             return ret;
 
         avpriv_update_cur_dts(s, st, sample_time);
-- 
2.54.1

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

Reply via email to