PR #26225 opened by Pu Cheng (GeekQAQ)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26225
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26225.patch

After the byte stream is repositioned and avformat_flush() is called,
the NUT demuxer parses the bytes at the new position as frame codes,
and when that fails searches for a startcode only after the syncpoint
it had seen before the discontinuity. Resynchronize from the current
position on flush, as read_seek() does after seeking: look for the next
startcode, reset last_resync_pos and skip until the next keyframe.

Signed-off-by: Pu Cheng <[email protected]>



>From 694a72da492d48f1f1f71eaaa8153b48e50d2231 Mon Sep 17 00:00:00 2001
From: Pu Cheng <[email protected]>
Date: Fri, 20 Nov 2026 14:34:00 +0800
Subject: [PATCH 1/3] avformat/demux: add a read_flush callback for
 avformat_flush()

avformat_flush() only calls ff_read_frame_flush(), which drops the
generic packet buffers, parser state and timestamps. Demuxers keep their
own read state, and there is no way for them to drop it, although the
documentation says that avformat_flush() "should also work with NUT,
Ogg and in a limited way AVI for example".

Add an optional FFInputFormat.read_flush callback and call it from
avformat_flush().

Signed-off-by: Pu Cheng <[email protected]>
---
 libavformat/demux.h | 6 ++++++
 libavformat/seek.c  | 2 ++
 2 files changed, 8 insertions(+)

diff --git a/libavformat/demux.h b/libavformat/demux.h
index 37d08fb2ca9..cfa0810f97f 100644
--- a/libavformat/demux.h
+++ b/libavformat/demux.h
@@ -190,6 +190,12 @@ typedef struct FFInputFormat {
      * @see avdevice_list_devices() for more details.
      */
     int (*get_device_list)(struct AVFormatContext *s, struct AVDeviceInfoList 
*device_list);
+
+    /**
+     * Discard the demuxer's private read state after a discontinuity in the
+     * byte stream (avformat_flush()). May be NULL.
+     */
+    int (*read_flush)(struct AVFormatContext *s);
 } FFInputFormat;
 
 static inline const FFInputFormat *ffifmt(const AVInputFormat *fmt)
diff --git a/libavformat/seek.c b/libavformat/seek.c
index 0e9b3b15c42..b8feedb7dd8 100644
--- a/libavformat/seek.c
+++ b/libavformat/seek.c
@@ -866,6 +866,8 @@ void ff_read_frame_flush(AVFormatContext *s)
 int avformat_flush(AVFormatContext *s)
 {
     ff_read_frame_flush(s);
+    if (s->iformat && ffifmt(s->iformat)->read_flush)
+        return ffifmt(s->iformat)->read_flush(s);
     return 0;
 }
 
-- 
2.54.1


>From cd96a959a4d7c4a2c133bb1b305666a4e6d3d51c Mon Sep 17 00:00:00 2001
From: Pu Cheng <[email protected]>
Date: Fri, 20 Nov 2026 14:34:00 +0800
Subject: [PATCH 2/3] avformat/ogg/demuxer: reset the page state on
 avformat_flush()

After the byte stream is repositioned and avformat_flush() is called,
the Ogg demuxer first returns the remaining packets of the page it had
buffered before the discontinuity, and only then reads from the new
position. Use ogg_reset() as read_flush, as the seek paths already do.

Signed-off-by: Pu Cheng <[email protected]>
---
 libavformat/ogg/demuxer.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libavformat/ogg/demuxer.c b/libavformat/ogg/demuxer.c
index 015f0e1b9c8..a8a81fdef87 100644
--- a/libavformat/ogg/demuxer.c
+++ b/libavformat/ogg/demuxer.c
@@ -1005,5 +1005,6 @@ const FFInputFormat ff_ogg_demuxer = {
     .read_packet    = ogg_read_packet,
     .read_close     = ogg_read_close,
     .read_seek      = ogg_read_seek,
+    .read_flush     = ogg_reset,
     .read_timestamp = ogg_read_timestamp,
 };
-- 
2.54.1


>From dbbaa45cdee83ac82469636465c788e8bc51c649 Mon Sep 17 00:00:00 2001
From: Pu Cheng <[email protected]>
Date: Fri, 20 Nov 2026 14:34:00 +0800
Subject: [PATCH 3/3] avformat/nutdec: resynchronize on avformat_flush()

After the byte stream is repositioned and avformat_flush() is called,
the NUT demuxer parses the bytes at the new position as frame codes,
and when that fails searches for a startcode only after the syncpoint
it had seen before the discontinuity. Resynchronize from the current
position on flush, as read_seek() does after seeking: look for the next
startcode, reset last_resync_pos and skip until the next keyframe.

Signed-off-by: Pu Cheng <[email protected]>
---
 libavformat/nutdec.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/libavformat/nutdec.c b/libavformat/nutdec.c
index ffb0a2a040e..312bd660614 100644
--- a/libavformat/nutdec.c
+++ b/libavformat/nutdec.c
@@ -1305,6 +1305,23 @@ static int read_seek(AVFormatContext *s, int 
stream_index,
     return 0;
 }
 
+static int nut_read_flush(AVFormatContext *s)
+{
+    NUTContext *nut = s->priv_data;
+    AVIOContext *bc = s->pb;
+    int i;
+
+    /* as after read_seek(): resynchronize from the current byte position */
+    nut->next_startcode = find_any_startcode(bc, -1);
+    if (nut->next_startcode)
+        nut->last_syncpoint_pos = avio_tell(bc) - 8;
+    for (i = 0; i < s->nb_streams; i++)
+        nut->stream[i].skip_until_key_frame = 1;
+    nut->last_resync_pos = 0;
+
+    return 0;
+}
+
 const FFInputFormat ff_nut_demuxer = {
     .p.name         = "nut",
     .p.long_name    = NULL_IF_CONFIG_SMALL("NUT"),
@@ -1318,4 +1335,5 @@ const FFInputFormat ff_nut_demuxer = {
     .read_packet    = nut_read_packet,
     .read_close     = nut_read_close,
     .read_seek      = read_seek,
+    .read_flush     = nut_read_flush,
 };
-- 
2.54.1

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

Reply via email to