PR #26377 opened by luoye-cpu URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26377 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26377.patch
Add a -gop_seek option to ffplay. After a seek, packets before the target are submitted with skip_frame=AVDISCARD_NONREF so non-reference frames before the target are not decoded; packets at and after the target are decoded normally. The window is armed on the initial -ss seek (after the video decoder is set up and before the first packet is enqueued) and on interactive seeks, and is reset on stream switch and at EOF. The user's own skip_frame value is captured on the first windowed packet and restored when the window closes. The window state is accessed through SDL atomics (ffplay's existing primitive). Unlike ffmpeg's accurate seeking, ffplay has no trim backstop, so this is a pure decode-work saving rather than a byte-exactness guarantee. Because ffplay seeks with the audio pre-roll, a seek may land before its target and the window then also skips non-reference frames ahead of the requested position that would otherwise be displayed. The window is bound to a seek generation: packets fetched before a seek flush carry the old queue serial and can neither join nor close a newer window. The 64-bit target is published under a mutex, so a re-arm while the window is active is race-free, and a failed seek clears any previous window. No automated test: ffplay has no FATE coverage for any feature; verified by compilation and smoke runs (-gop_seek -ss 2 -autoexit on a 4K HEVC stream with a -v verbose arm/skip trace). >From da2639fa0a21c666910a5485b51945f6b6e66e91 Mon Sep 17 00:00:00 2001 From: luoye-cpu <[email protected]> Date: Fri, 27 Nov 2026 01:48:06 +0800 Subject: [PATCH] ffplay: add GOP-aware seek support Add -gop_seek, which arms a decoding window for video seeks: video packets before the seek target are submitted with AVDISCARD_NONREF and the user's skip_frame value, captured when the window opens, is restored at the first packet at or after the target. Unlike the ffmpeg CLI option this is a decode-work saving aid only, since ffplay has no accurate-seek trimming and displays whatever the decoder outputs. The target is rescaled to the video stream timebase with av_rescale_q_rnd(..., AV_ROUND_DOWN), matching the trim start of the ffmpeg CLI. The startup -ss seek bypasses the seek_req path, so read_thread arms that window right after opening the streams; regular seeks arm it when the seek request is handled, after the queue flush. The active flag is an SDL_atomic_t, but the target is 64-bit while SDL_atomic_t is only 32-bit, so {active, target, serial} is published and read under a mutex; the target is written before the flag is raised as well, so video_thread never observes an armed window with a stale or torn target. The window is bound to the video packet queue serial at arm time: packets fetched before the flush that precedes a seek carry an older serial and neither use the new target nor close the new window, so a re-arm on a new seek generation is safe. The window closes on flush, on byte-position seeks (no timestamp target), when the video stream is reopened, and on a failed seek, so a stale target is not kept for the packets that follow. Signed-off-by: luoye-cpu <[email protected]> --- doc/ffplay.texi | 14 +++++ fftools/ffplay.c | 157 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 166 insertions(+), 5 deletions(-) diff --git a/doc/ffplay.texi b/doc/ffplay.texi index 2a426f953ec..e1564c82174 100644 --- a/doc/ffplay.texi +++ b/doc/ffplay.texi @@ -58,6 +58,20 @@ see @ref{time duration syntax,,the Time duration section in the ffmpeg-utils(1) Seek by bytes. @item -seek_interval Set custom interval, in seconds, for seeking using left/right keys. Default is 10 seconds. +@item -gop_seek +Enable a GOP-aware decoding window for video seeks: video packets before the +requested seek target are submitted to the decoder with non-reference frame +skipping (equivalent to @code{-skip_frame noref}), and normal decoding resumes +at the first packet at or after the target. This saves decoding work while the +player is catching up to the seek position. +Note that @command{ffplay} has no accurate seek trimming, so this option is +purely a decoding efficiency aid: frames are still not guaranteed to match an +accurate seek. Because @command{ffplay} seeks with the audio pre-roll, a seek +may land before its target and the window then also skips non-reference frames +ahead of the requested position that would otherwise be displayed. +Decoders that act on @option{skip_frame} during decoding benefit fully +(H.264 and HEVC); for AV1 it depends on the selected decoder, and decoders +without support make the option a no-op. @item -nodisp Disable graphical display. @item -noborder diff --git a/fftools/ffplay.c b/fftools/ffplay.c index 0bb1d55cca1..396c4112875 100644 --- a/fftools/ffplay.c +++ b/fftools/ffplay.c @@ -233,6 +233,25 @@ typedef struct VideoState { int seek_flags; int64_t seek_pos; int64_t seek_rel; + /* GOP-aware seek window (-gop_seek): non-reference video packets before + * gop_seek_target_pts (video stream timebase) are submitted with + * AVDISCARD_NONREF and the window closes at the first packet at/after the + * target. ffplay has no accurate_seek trim backstop, so unlike the ffmpeg + * CLI option this is purely a decode-work saving aid */ + int gop_seek; + int64_t gop_seek_target_pts; + SDL_atomic_t gop_seek_active; + /* videoq serial current at arm time: packets the decoder fetched before + * the flush that precedes a seek carry an older serial and must neither + * join nor close the window armed for the new serial */ + int gop_seek_serial; + /* user skip_frame captured when the window opens, restored on close */ + int gop_seek_skip_user; + int gop_seek_skip_saved; + /* serializes arm/close against the per-packet read of the triple + * {active, target, serial}: the target is 64-bit while SDL_atomic_t is + * only 32-bit, so the atomic alone cannot publish it tear-free */ + SDL_mutex *gop_seek_lock; int read_pause_return; AVFormatContext *ic; int realtime; @@ -349,6 +368,7 @@ static int subtitle_disable; static const char* wanted_stream_spec[AVMEDIA_TYPE_NB] = {0}; static int seek_by_bytes = -1; static float seek_interval = 10; +static int gop_seek; static int display_disable; static int borderless; static int alwaysontop; @@ -636,7 +656,61 @@ static int decoder_init(Decoder *d, AVCodecContext *avctx, PacketQueue *queue, S return 0; } -static int decoder_decode_frame(Decoder *d, AVFrame *frame, AVSubtitle *sub) { +/* The caller must hold gop_seek_lock. avctx == NULL means there is no decoder + * to restore right now (seek failure, stream switch): only the active flag is + * dropped and the skip_frame restore is deferred to the video thread, which + * runs it on the next packet of the armed serial or on decoder EOF. */ +static void gop_seek_window_close_locked(VideoState *is, AVCodecContext *avctx) +{ + if (is->gop_seek_skip_saved && avctx) { + avctx->skip_frame = is->gop_seek_skip_user; + is->gop_seek_skip_saved = 0; + } + SDL_AtomicSet(&is->gop_seek_active, 0); + /* A deferred restore (avctx == NULL) happens on the video thread, which + * only acts on packets of the armed serial; re-point the serial at the + * queue's current one so those packets still match after a flush. */ + is->gop_seek_serial = is->videoq.serial; +} + +static void gop_seek_window_close(VideoState *is, AVCodecContext *avctx) +{ + SDL_LockMutex(is->gop_seek_lock); + gop_seek_window_close_locked(is, avctx); + SDL_UnlockMutex(is->gop_seek_lock); +} + +/* Drop the window without restoring skip_frame: only used when the decoder it + * was captured from is going away (stream switch), so there is nothing left to + * restore. */ +static void gop_seek_window_reset(VideoState *is) +{ + SDL_LockMutex(is->gop_seek_lock); + is->gop_seek_skip_saved = 0; + SDL_AtomicSet(&is->gop_seek_active, 0); + SDL_UnlockMutex(is->gop_seek_lock); +} + +/* Arm the window for the current videoq serial. The target and the serial are + * published under the lock together with the active flag: SDL atomics are full + * barriers, but the target is 64-bit and SDL_atomic_t is only 32-bit, so the + * pair cannot be published tear-free through the atomic alone. Callers run on + * the read thread after the packet_queue_flush() that precedes a seek (or + * before the first enqueue on the startup path), so the captured serial is the + * one the seek's packets will carry. The lock is never held across a blocking + * packet queue operation. */ +static void gop_seek_window_arm(VideoState *is, int64_t target_pts) +{ + SDL_LockMutex(is->gop_seek_lock); + is->gop_seek_target_pts = target_pts; + is->gop_seek_serial = is->videoq.serial; + SDL_AtomicSet(&is->gop_seek_active, 1); + SDL_UnlockMutex(is->gop_seek_lock); + av_log(NULL, AV_LOG_VERBOSE, "gop_seek: window armed, target pts %"PRId64"\n", + target_pts); +} + +static int decoder_decode_frame(VideoState *is, Decoder *d, AVFrame *frame, AVSubtitle *sub) { int ret = AVERROR(EAGAIN); for (;;) { @@ -674,6 +748,8 @@ static int decoder_decode_frame(Decoder *d, AVFrame *frame, AVSubtitle *sub) { if (ret == AVERROR_EOF) { d->finished = d->pkt_serial; avcodec_flush_buffers(d->avctx); + if (d->avctx->codec_type == AVMEDIA_TYPE_VIDEO) + gop_seek_window_close(is, d->avctx); return 0; } if (ret >= 0) @@ -725,6 +801,43 @@ static int decoder_decode_frame(Decoder *d, AVFrame *frame, AVSubtitle *sub) { fd->pkt_pos = d->pkt->pos; } + if (d->avctx->codec_type == AVMEDIA_TYPE_VIDEO) { + int active, serial; + int64_t target; + + /* per-packet skip_frame like fftools/ffmpeg_dec.c packet_decode: + * skip non-reference packets before the seek target, restore the + * user's value at the first packet at/after it. ffplay displays + * everything the decoder outputs, so the window only saves + * decoding work and provides no output parity guarantee. + * The user value is captured lazily because a startup -ss window + * is armed before the decoder is opened. + * The {active, target, serial} triple is read consistently under + * gop_seek_lock, and only packets carrying the armed serial take + * part: a packet fetched before the flush of a new seek must not + * use the new target nor close the new window. */ + SDL_LockMutex(is->gop_seek_lock); + active = SDL_AtomicGet(&is->gop_seek_active); + target = is->gop_seek_target_pts; + serial = is->gop_seek_serial; + if (active && d->pkt_serial == serial) { + if (!is->gop_seek_skip_saved) { + is->gop_seek_skip_user = d->avctx->skip_frame; + is->gop_seek_skip_saved = 1; + av_log(NULL, AV_LOG_VERBOSE, + "gop_seek: first packet pts %"PRId64", target %"PRId64"\n", + d->pkt->pts, target); + } + if (d->pkt->pts != AV_NOPTS_VALUE && d->pkt->pts < target) + d->avctx->skip_frame = AVDISCARD_NONREF; + else + gop_seek_window_close_locked(is, d->avctx); + } else if (!active && is->gop_seek_skip_saved && + d->pkt_serial == serial) + gop_seek_window_close_locked(is, d->avctx); + SDL_UnlockMutex(is->gop_seek_lock); + } + if (avcodec_send_packet(d->avctx, d->pkt) == AVERROR(EAGAIN)) { av_log(d->avctx, AV_LOG_ERROR, "Receive_frame and send_packet both returned EAGAIN, which is an API violation.\n"); d->packet_pending = 1; @@ -1481,6 +1594,7 @@ static void stream_close(VideoState *is) frame_queue_destroy(&is->sampq); frame_queue_destroy(&is->subpq); SDL_DestroyCond(is->continue_read_thread); + SDL_DestroyMutex(is->gop_seek_lock); sws_freeContext(is->sub_convert_ctx); av_free(is->filename); av_free(is->title); @@ -1997,7 +2111,7 @@ static int get_video_frame(VideoState *is, AVFrame *frame) { int got_picture; - if ((got_picture = decoder_decode_frame(&is->viddec, frame, NULL)) < 0) + if ((got_picture = decoder_decode_frame(is, &is->viddec, frame, NULL)) < 0) return -1; if (got_picture) { @@ -2308,7 +2422,7 @@ static int audio_thread(void *arg) return AVERROR(ENOMEM); do { - if ((got_frame = decoder_decode_frame(&is->auddec, frame, NULL)) < 0) + if ((got_frame = decoder_decode_frame(is, &is->auddec, frame, NULL)) < 0) goto the_end; if (got_frame) { @@ -2512,7 +2626,7 @@ static int subtitle_thread(void *arg) if (!(sp = frame_queue_peek_writable(&is->subpq))) return 0; - if ((got_subtitle = decoder_decode_frame(&is->subdec, NULL, &sp->sub)) < 0) + if ((got_subtitle = decoder_decode_frame(is, &is->subdec, NULL, &sp->sub)) < 0) break; pts = 0; @@ -3082,6 +3196,9 @@ static int stream_component_open(VideoState *is, int stream_index) case AVMEDIA_TYPE_VIDEO: is->video_stream = stream_index; is->video_st = ic->streams[stream_index]; + /* a stream switch invalidates any pending GOP-aware seek window: the + * decoder that captured the user skip_frame is gone, so reset */ + gop_seek_window_reset(is); if (ici->streams[stream_index]->group) { if ((ret = init_lcevc_graph(ic, stream_index)) < 0) @@ -3241,6 +3358,7 @@ static int read_thread(void *arg) AVDictionary *format_options = NULL; int scan_all_pmts_set = 0; int64_t pkt_ts; + int64_t initial_seek_target = AV_NOPTS_VALUE; if (!wait_mutex) { av_log(NULL, AV_LOG_FATAL, "SDL_CreateMutex(): %s\n", SDL_GetError()); @@ -3347,7 +3465,8 @@ static int read_thread(void *arg) if (ret < 0) { av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n", is->filename, (double)timestamp / AV_TIME_BASE); - } + } else + initial_seek_target = timestamp; } is->realtime = is_realtime(ic); @@ -3466,6 +3585,13 @@ static int read_thread(void *arg) if (is->show_mode == SHOW_MODE_NONE) is->show_mode = ret >= 0 ? SHOW_MODE_VIDEO : SHOW_MODE_RDFT; + /* The startup -ss seek above bypassed the seek_req path, so arm its window + * here: the video decoder thread is running but videoq is still empty, and + * only this thread enqueues the first packet from now on. */ + if (is->gop_seek && initial_seek_target != AV_NOPTS_VALUE && is->video_stream >= 0) + gop_seek_window_arm(is, av_rescale_q_rnd(initial_seek_target, AV_TIME_BASE_Q, + is->video_st->time_base, AV_ROUND_DOWN)); + if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) { stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]); } @@ -3516,6 +3642,12 @@ static int read_thread(void *arg) if (ret < 0) { av_log(NULL, AV_LOG_ERROR, "%s: error while seeking\n", is->ic->url); + /* the seek failed: drop any window still armed for the previous + * position, or the next packets would keep using a stale target. + * avctx is NULL because only the video thread owns the decoder; + * it restores its skip_frame on the next packet or on EOF */ + if (is->gop_seek) + gop_seek_window_close(is, NULL); } else { if (is->audio_stream >= 0) packet_queue_flush(&is->audioq); @@ -3534,6 +3666,15 @@ static int read_thread(void *arg) } else { set_clock(&is->extclk, seek_target / (double)AV_TIME_BASE, 0); } + /* arm the GOP-aware decode-saving window for this seek; a + * byte-position seek has no timestamp target and closes it */ + if (is->gop_seek) { + if ((is->seek_flags & AVSEEK_FLAG_BYTE) || !is->video_st) + gop_seek_window_close(is, NULL); + else + gop_seek_window_arm(is, av_rescale_q_rnd(seek_target, AV_TIME_BASE_Q, + is->video_st->time_base, AV_ROUND_DOWN)); + } } is->seek_req = 0; is->queue_attachments_req = 1; @@ -3696,6 +3837,7 @@ static VideoState *stream_open(const char *filename, is->seek_by_bytes = seek_by_bytes; is->infinite_buffer = infinite_buffer; is->loop = loop; + is->gop_seek = gop_seek; is->ytop = 0; is->xleft = 0; @@ -3716,6 +3858,10 @@ static VideoState *stream_open(const char *filename, av_log(NULL, AV_LOG_FATAL, "SDL_CreateCond(): %s\n", SDL_GetError()); goto fail; } + if (!(is->gop_seek_lock = SDL_CreateMutex())) { + av_log(NULL, AV_LOG_FATAL, "SDL_CreateMutex(): %s\n", SDL_GetError()); + goto fail; + } init_clock(&is->vidclk, &is->videoq.serial); init_clock(&is->audclk, &is->audioq.serial); @@ -4467,6 +4613,7 @@ static const OptionDef options[] = { { "t", OPT_TYPE_TIME, 0, { &duration }, "play \"duration\" seconds of audio/video", "duration" }, { "bytes", OPT_TYPE_INT, 0, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" }, { "seek_interval", OPT_TYPE_FLOAT, 0, { &seek_interval }, "set seek interval for left/right keys, in seconds", "seconds" }, + { "gop_seek", OPT_TYPE_BOOL, 0, { &gop_seek }, "skip decoding non-reference frames before seek targets", "" }, { "nodisp", OPT_TYPE_BOOL, 0, { &display_disable }, "disable graphical display" }, { "noborder", OPT_TYPE_BOOL, 0, { &borderless }, "borderless window" }, { "alwaysontop", OPT_TYPE_BOOL, 0, { &alwaysontop }, "window always on top" }, -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
