Commit 5e50833d5d for ffmpeg

commit 5e50833d5d2597497c384465c901858a36d5faa3
Author: Romain Beauxis <romain.beauxis@gmail.com>
Date:   Wed Dec 2 07:28:19 2026 -0600

    avformat/seek: tell the demuxer that a seek has AVSEEK_FLAG_PREROLL

    The flag is not passed to the demuxer, so that a demuxer seeking
    through another one does not move the target back twice.

    Record it in FFFormatContext with the requested timestamp and the lower
    bound of the seek in its own time base, for a demuxer that lands on an
    earlier keyframe for a caller that decodes.

diff --git a/libavformat/internal.h b/libavformat/internal.h
index 3c95eb5026..a2fbe31ced 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -130,6 +130,22 @@ typedef struct FFFormatContext {
      * scoped to this context.
      */
     struct CurlLoop *curl_loop;
+
+    /**
+     * Set while a seek with AVSEEK_FLAG_PREROLL runs, for a demuxer to land
+     * on a keyframe from which the pictures are whole at the target. The
+     * flag itself is not passed to the demuxer.
+     */
+    int seek_has_preroll_flag;
+    /**
+     * The requested timestamp of that seek, before the pre-roll moves it
+     * back, and its lower bound, INT64_MIN when it has none, in the time
+     * base of the seek. The first whole picture decoded from the keyframe
+     * must come between them.
+     */
+    int64_t seek_ts;
+    int64_t seek_min_ts;
+    AVRational seek_time_base;
 } FFFormatContext;

 static av_always_inline FFFormatContext *ffformatcontext(AVFormatContext *s)
diff --git a/libavformat/seek.c b/libavformat/seek.c
index 0e9b3b15c4..c2f3e3ece2 100644
--- a/libavformat/seek.c
+++ b/libavformat/seek.c
@@ -714,7 +714,11 @@ static int64_t preroll_target(const AVFormatContext *s, int stream_index,
 int av_seek_frame(AVFormatContext *s, int stream_index,
                   int64_t timestamp, int flags)
 {
+    FFFormatContext *const si = ffformatcontext(s);
     int preroll_flag = flags & AVSEEK_FLAG_PREROLL;
+    int seek_has_preroll_flag = si->seek_has_preroll_flag;
+    int64_t seek_ts = si->seek_ts, seek_min_ts = si->seek_min_ts;
+    AVRational seek_time_base = si->seek_time_base;
     int64_t target = timestamp;
     int ret;

@@ -732,9 +736,17 @@ int av_seek_frame(AVFormatContext *s, int stream_index,
     }

     flags &= ~AVSEEK_FLAG_PREROLL;
-    if (preroll_flag && !(flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME)))
+    if (preroll_flag && !(flags & (AVSEEK_FLAG_BYTE | AVSEEK_FLAG_FRAME))) {
         target = preroll_target(s, stream_index, INT64_MIN, timestamp);
-
+        // Keep what avformat_seek_file() set when the seek comes from it.
+        if (!si->seek_has_preroll_flag) {
+            si->seek_has_preroll_flag = 1;
+            si->seek_ts        = timestamp;
+            si->seek_min_ts    = INT64_MIN;
+            si->seek_time_base = stream_index < 0 ? AV_TIME_BASE_Q :
+                                 s->streams[stream_index]->time_base;
+        }
+    }
     ret = seek_frame_internal(s, stream_index, target, flags);
     // Without a known start, the earlier target may come before the first
     // packet.
@@ -742,6 +754,10 @@ int av_seek_frame(AVFormatContext *s, int stream_index,
         target = timestamp;
         ret = seek_frame_internal(s, stream_index, timestamp, flags);
     }
+    si->seek_has_preroll_flag = seek_has_preroll_flag;
+    si->seek_ts        = seek_ts;
+    si->seek_min_ts    = seek_min_ts;
+    si->seek_time_base = seek_time_base;
     if (ret >= 0 && preroll_flag)
         request_seek_discard(s, stream_index, target, flags | preroll_flag);

@@ -789,6 +805,7 @@ static int seek_file(AVFormatContext *s, int stream_index, int64_t min_ts,
 int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts,
                        int64_t ts, int64_t max_ts, int flags)
 {
+    FFFormatContext *const si = ffformatcontext(s);
     int64_t target = ts, target_max = max_ts;
     int preroll_flag;
     int ret;
@@ -817,8 +834,13 @@ FF_ENABLE_DEPRECATION_WARNINGS
         // An open upper bound stays open.
         if (max_ts != INT64_MAX)
             target_max = av_sat_sub64(max_ts, av_sat_sub64(ts, target));
-    }

+        si->seek_has_preroll_flag = 1;
+        si->seek_ts        = ts;
+        si->seek_min_ts    = min_ts;
+        si->seek_time_base = stream_index < 0 ? AV_TIME_BASE_Q :
+                             s->streams[stream_index]->time_base;
+    }
     ret = seek_file(s, stream_index, min_ts, target, target_max, flags);
     // Without a known start, the earlier target may come before the first
     // packet.
@@ -826,6 +848,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
         target = ts;
         ret = seek_file(s, stream_index, min_ts, ts, max_ts, flags);
     }
+    si->seek_has_preroll_flag = 0;
     if (ret >= 0)
         request_seek_discard(s, stream_index, target, flags | preroll_flag);
     return ret;