Commit 3b92ec55a8 for ffmpeg

commit 3b92ec55a83e08e71aea363b9d63bd898e6f6f24
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date:   Sat Nov 21 09:51:55 2026 -0500

    avformat/demux: move the end trim to a function

    Take the decoded length of the packet as a parameter, so it can be
    computed from another source.

diff --git a/libavformat/demux.c b/libavformat/demux.c
index c593e7b317..b5755f18ce 100644
--- a/libavformat/demux.c
+++ b/libavformat/demux.c
@@ -1193,6 +1193,36 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
         pkt->flags |= AV_PKT_FLAG_KEY;
 }

+static int64_t ts_to_samples(AVStream *st, int64_t ts)
+{
+    return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
+}
+
+/**
+ * Samples of a packet past the end trim of the stream.
+ *
+ * @param min_duration known decoded length of the packet, in samples; the
+ *                     packet duration is used when it is longer
+ */
+static int end_trim_padding(AVStream *st, const AVPacket *pkt, int64_t min_duration)
+{
+    const FFStream *sti = ffstream(st);
+    int64_t pts = pkt->pts, duration, sample, end_sample;
+
+    if (!sti->first_discard_sample || pts == AV_NOPTS_VALUE)
+        return 0;
+
+    duration = FFMAX(min_duration, ts_to_samples(st, pkt->duration));
+    if (duration <= 0)
+        return 0;
+
+    sample     = ts_to_samples(st, pts - (is_relative(pts) ? RELATIVE_TS_BASE : 0));
+    end_sample = av_sat_add64(sample, duration);
+    if (end_sample < sti->first_discard_sample || sample >= sti->last_discard_sample)
+        return 0;
+    return FFMIN(end_sample - sti->first_discard_sample, duration);
+}
+
 /**
  * Parse a packet, add all split parts to parse_queue.
  *
@@ -1370,11 +1400,6 @@ fail:
     return ret;
 }

-static int64_t ts_to_samples(AVStream *st, int64_t ts)
-{
-    return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
-}
-
 static int codec_close(AVFormatContext *s, FFStream *sti)
 {
     AVCodecContext *avctx_new = NULL;
@@ -1572,16 +1597,7 @@ static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
     if (ret >= 0) {
         AVStream *const st  = s->streams[pkt->stream_index];
         FFStream *const sti = ffstream(st);
-        int discard_padding = 0;
-        if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
-            int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
-            int64_t sample = ts_to_samples(st, pts);
-            int64_t duration = FFMAX(st->codecpar->frame_size, ts_to_samples(st, pkt->duration));
-            int64_t end_sample = av_sat_add64(sample, duration);
-            if (duration > 0 && end_sample >= sti->first_discard_sample &&
-                sample < sti->last_discard_sample)
-                discard_padding = FFMIN(end_sample - sti->first_discard_sample, duration);
-        }
+        int discard_padding = end_trim_padding(st, pkt, st->codecpar->frame_size);
         if (sti->seek_discard) {
             const AVCodecParameters *par = st->codecpar;