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

ISO/IEC 14496-12 defines media sample timing in stts and trun and the mapping 
to the presentation timeline in elst. When a finite edit ends inside an audio 
sample, report the packet duration up to the edit boundary and signal the 
remaining decoded samples as discard padding.

For fragmented AAC with a shortened run duration, preserve that duration and 
signal the difference from the coded frame size as discard padding.



>From 77e24c09a8d05508e8a45a59fd3585f5abdb2340 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sat, 14 Nov 2026 00:02:36 -0800
Subject: [PATCH] avformat/mov: account for audio samples ending at edit
 boundaries

ISO/IEC 14496-12 defines media sample timing in stts and trun and the mapping 
to the presentation timeline in elst. When a finite edit ends inside an audio 
sample, report the packet duration up to the edit boundary and signal the 
remaining decoded samples as discard padding.

For fragmented AAC with a shortened run duration, preserve that duration and 
signal the difference from the coded frame size as discard padding.
---
 libavformat/isom.h |  3 +++
 libavformat/mov.c  | 61 ++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 59 insertions(+), 5 deletions(-)

diff --git a/libavformat/isom.h b/libavformat/isom.h
index 6230218e37f..7858aad44f3 100644
--- a/libavformat/isom.h
+++ b/libavformat/isom.h
@@ -175,6 +175,8 @@ typedef struct MOVFragmentIndex {
 typedef struct MOVIndexRange {
     int64_t start;
     int64_t end;
+    unsigned int last_duration; ///< packet duration when the edit ends inside 
a sample
+    unsigned int end_padding; ///< media ticks trimmed from the last sample
 } MOVIndexRange;
 
 typedef struct MOVStreamContext {
@@ -215,6 +217,7 @@ typedef struct MOVStreamContext {
     unsigned int keyframe_count;
     int *keyframes;
     int time_scale;
+    int64_t fragment_edit_end; ///< finite edit end on the presentation 
timeline
     int64_t time_offset;  ///< time offset of the edit list entries
     int64_t min_corrected_pts;  ///< minimum Composition time shown by the 
edits excluding empty edits.
     int current_sample;
diff --git a/libavformat/mov.c b/libavformat/mov.c
index ca33cb2285b..9a03cdcd3fd 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -4575,7 +4575,7 @@ static void mov_fix_index(MOVContext *mov, AVStream *st)
     int64_t max_entries = nb_old * 16LL + (1 << 16);
 
     // allocate the index ranges array
-    msc->index_ranges = av_malloc_array(msc->elst_count + 1,
+    msc->index_ranges = av_calloc(msc->elst_count + 1,
                                         sizeof(msc->index_ranges[0]));
     if (!msc->index_ranges) {
         av_log(mov->fc, AV_LOG_ERROR, "Cannot allocate index ranges buffer\n");
@@ -4690,6 +4690,8 @@ static void mov_fix_index(MOVContext *mov, AVStream *st)
             curr_ctts = 0;
 
             if (tts_data_old && tts_index_old < tts_count_old) {
+                if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
+                    frame_duration = tts_data_old[tts_index_old].duration;
                 curr_ctts = tts_data_old[tts_index_old].offset;
                 av_log(mov->fc, AV_LOG_TRACE, "stts: %"PRId64" ctts: 
%"PRId64", tts_index: %"PRId64", tts_count: %"PRId64"\n",
                        curr_cts, curr_ctts, tts_index_old, tts_count_old);
@@ -4778,13 +4780,22 @@ static void mov_fix_index(MOVContext *mov, AVStream *st)
             }
 
             // Update the index ranges array
-            if (!current_index_range || index != current_index_range->end) {
+            if (!current_index_range || current_index_range->end_padding || 
index != current_index_range->end) {
                 current_index_range = current_index_range ? 
current_index_range + 1
                                                           : msc->index_ranges;
                 current_index_range->start = index;
             }
             current_index_range->end = index + 1;
 
+            if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && 
st->codecpar->sample_rate > 0 &&
+                !(flags & AVINDEX_DISCARD_FRAME)) {
+                int64_t remaining = 
av_sat_sub64(av_sat_add64(edit_list_media_time, edit_list_duration), curr_cts);
+                if (remaining > 0 && remaining < frame_duration) {
+                    current_index_range->last_duration = remaining;
+                    current_index_range->end_padding = frame_duration - 
remaining;
+                }
+            }
+
             // Only start incrementing DTS in frame_duration amounts, when we 
encounter a frame in edit list.
             if (edit_list_start_encountered > 0) {
                 edit_list_dts_counter = edit_list_dts_counter + frame_duration;
@@ -5052,6 +5063,14 @@ static void mov_build_index(MOVContext *mov, AVStream 
*st)
                 current_dts = -av_clip64(sc->time_offset, -INT64_MAX, 
INT64_MAX);
         }
 
+        if (!multiple_edits && edit_start_index < sc->elst_count &&
+            sc->elst_data[edit_start_index].rate == 1 && mov->time_scale > 0) {
+            int64_t media_time, duration;
+            if (get_edit_list_entry(mov, sc, edit_start_index, &media_time, 
&duration, mov->time_scale) &&
+                duration > 0)
+                sc->fragment_edit_end = av_sat_add64(empty_duration, duration);
+        }
+
         if (!multiple_edits && !mov->advanced_editlist &&
             st->codecpar->codec_id == AV_CODEC_ID_AAC && start_time > 0)
             st->codecpar->initial_padding = av_rescale_q(start_time, 
st->time_base,
@@ -12782,10 +12801,11 @@ static int get_eia608_packet(AVIOContext *pb, 
AVPacket *pkt, int src_size)
 }
 
 static int mov_finalize_packet(AVFormatContext *s, AVStream *st, AVIndexEntry 
*sample,
-                                int64_t current_index, AVPacket *pkt)
+                                int64_t current_index, const MOVIndexRange 
*range, AVPacket *pkt)
 {
     MOVContext *mov = s->priv_data;
     MOVStreamContext *sc = st->priv_data;
+    int64_t end_padding = 0;
 
     pkt->stream_index = sc->ffindex;
     pkt->dts = sample->timestamp;
@@ -12825,6 +12845,35 @@ static int mov_finalize_packet(AVFormatContext *s, 
AVStream *st, AVIndexEntry *s
             ffstream(st)->last_discard_sample  = total;
     }
 
+    if (range && range->end_padding && current_index + 1 == range->end) {
+        pkt->duration = range->last_duration;
+        end_padding = av_rescale_q(range->end_padding, st->time_base,
+                                   (AVRational){ 1, st->codecpar->sample_rate 
});
+    } else if (mov->fragment.found_tfhd && st->codecpar->codec_type == 
AVMEDIA_TYPE_AUDIO &&
+               st->codecpar->sample_rate > 0 && pkt->duration > 0) {
+        AVRational sample_time = { 1, st->codecpar->sample_rate };
+        int64_t coded_samples = av_rescale_q(pkt->duration, st->time_base, 
sample_time);
+
+        if (sc->fragment_edit_end && pkt->pts >= sc->fragment_edit_end)
+            pkt->flags |= AV_PKT_FLAG_DISCARD;
+        else {
+            if (sc->fragment_edit_end)
+                pkt->duration = FFMIN(pkt->duration, 
av_sat_sub64(sc->fragment_edit_end, pkt->pts));
+            /* A shortened AAC run duration still describes a full coded 
frame. */
+            if (st->codecpar->codec_id == AV_CODEC_ID_AAC)
+                coded_samples = FFMAX(coded_samples, st->codecpar->frame_size);
+            end_padding = coded_samples -
+                          av_rescale_q(pkt->duration, st->time_base, 
sample_time);
+        }
+    }
+    if (end_padding > 0) {
+        uint8_t *sd = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 
10);
+        if (!sd)
+            return AVERROR(ENOMEM);
+        memset(sd, 0, 10);
+        AV_WL32(sd + 4, end_padding);
+    }
+
     if (sc->tts_data && sc->tts_index < sc->tts_count) {
         /* update tts context */
         sc->tts_sample++;
@@ -12873,6 +12922,7 @@ static int mov_read_packet(AVFormatContext *s, AVPacket 
*pkt)
     AVStream *st = NULL;
     FFStream *avsti = NULL;
     int64_t current_index;
+    const MOVIndexRange *range;
     int ret;
     int i;
     mov->fc = s;
@@ -12922,6 +12972,7 @@ static int mov_read_packet(AVFormatContext *s, AVPacket 
*pkt)
     sc = st->priv_data;
     /* must be done just before reading, to avoid infinite loop on sample */
     current_index = sc->current_index;
+    range = sc->current_index_range;
     mov_current_sample_inc(sc);
 
     if (mov->next_root_atom) {
@@ -12953,7 +13004,7 @@ static int mov_read_packet(AVFormatContext *s, AVPacket 
*pkt)
         else if (sc->iamf) {
             int64_t pts, dts, pos, duration;
             int flags, size = sample->size;
-            ret = mov_finalize_packet(s, st, sample, current_index, pkt);
+            ret = mov_finalize_packet(s, st, sample, current_index, range, 
pkt);
             pts = pkt->pts; dts = pkt->dts;
             pos = pkt->pos; flags = pkt->flags;
             duration = pkt->duration;
@@ -13028,7 +13079,7 @@ static int mov_read_packet(AVFormatContext *s, AVPacket 
*pkt)
         }
     }
 
-    ret = mov_finalize_packet(s, st, sample, current_index, pkt);
+    ret = mov_finalize_packet(s, st, sample, current_index, range, pkt);
     if (ret < 0)
         return ret;
 
-- 
2.54.0

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

Reply via email to