PR #26375 opened by luoye-cpu URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26375 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26375.patch
Add an input option that, when seeking with -ss in accurate mode, submits video packets before the seek target with AVCodecContext.skip_frame set to AVDISCARD_NONREF, so non-reference frames before the target are not decoded at all; packets at and after the target are submitted with the user's skip_frame value. The target is computed from the same value the trim filter uses, and trim stays as the fallback, so the output is identical to a plain accurate seek while redundant decode work before the target is removed. Applies to video streams only. Works with every decoder that runs its own non-reference skip check before submitting a frame: the native H.264, HEVC and AV1 decoders, including when they use hardware acceleration (their skip check runs before the hwaccel submission). Hardware-only wrapper decoders do not consume skip_frame and keep the window disabled with a verbose message; decoders without skip_frame support make the option a no-op. Related work, not overlapping: this is application-level orchestration on top of the existing seek/flush/skip_frame mechanisms. It does not touch the container or generic-search layers (#26313, #24508) and does not depend on AVSEEK_FLAG_PREROLL (#25842). Equivalent application-level windows already exist in the field (mpv's hr-seek framedrop and VLC's hurry-up mode both switch skip_frame to AVDISCARD_NONREF before the seek target); this patch brings the same idea to FFmpeg's own tools, which have no such window today. Limitations: - The identical output assumes the container supplies real presentation timestamps. For streams whose packet timestamps are merely a linear decode-order sequence (some remuxed raw streams), the first frame after the seek point may differ by one frame; the same is observable with -skip_frame alone, without this option. - The window does not change random-access point selection: streams with open-GOP random access points keep the demuxer and decoder behavior, and a packet carrying several access units is treated as a whole. FATE: two cases on fate-suite samples - fate-ffmpeg-gop_seek (H.264, parity against the default accurate seek plus a decode-count assertion via -debug_ts) and fate-ffmpeg-gop_seek-hevc (same assertions on HEVC). The decode-count assertion makes the test fail if the window is a no-op. >From 5bc4a521fcf510f985ab206b9cea5c7786801ba1 Mon Sep 17 00:00:00 2001 From: luoye-cpu <[email protected]> Date: Fri, 27 Nov 2026 01:48:05 +0800 Subject: [PATCH 1/2] fftools: add a -gop_seek option for GOP-aware accurate seeking With -ss and accurate seeking enabled, -gop_seek submits video packets before the seek target with AVCodecContext.skip_frame set to AVDISCARD_NONREF, so non-reference frames before the target are not decoded at all; packets at and after the target are submitted with the user's skip_frame value, captured when the decoder is opened. The target is computed from the same source as the trim filter start, and the trim filter stays in place, so the output is byte-identical to the same run without -gop_seek while redundant decode work before the target is removed. The pre-existing trim-start expression is extracted verbatim into demux_trim_start_us() so the trim filter start and the window target cannot drift apart. The window applies to video streams only and resets at flush packets. Hardware decoding is supported when the decoder runs its skip_frame check before submitting frames to the hwaccel (the native H.264, HEVC and AV1 decoders); hardware-only decoders are rejected through AV_CODEC_CAP_HARDWARE and keep the window disabled, as skip_frame never reaches them. Unsupported decoders degrade to the regular accurate-seek path. The window is applied per packet, so a packet carrying several access units is treated as a whole, and it does not change random-access point selection: streams with open-GOP random access points keep the demuxer and decoder behavior. Signed-off-by: luoye-cpu <[email protected]> --- doc/ffmpeg.texi | 21 +++++++++++++++++ fftools/ffmpeg.h | 9 +++++++ fftools/ffmpeg_dec.c | 53 ++++++++++++++++++++++++++++++++++++++++++ fftools/ffmpeg_demux.c | 47 ++++++++++++++++++++++++++++++------- fftools/ffmpeg_opt.c | 3 +++ 5 files changed, 125 insertions(+), 8 deletions(-) diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi index bff7e31c7c4..b823d92dbcd 100644 --- a/doc/ffmpeg.texi +++ b/doc/ffmpeg.texi @@ -2845,6 +2845,27 @@ This option enables or disables accurate seeking in input files with the transcoding. Use @option{-noaccurate_seek} to disable it, which may be useful e.g. when copying some streams and transcoding the others. +@item -gop_seek (@emph{input}) +Enable a GOP-aware decoding window when seeking with @option{-ss} while +transcoding (@option{-accurate_seek} enabled, the default). Video packets +before the seek target are submitted to the decoder with non-reference frame +skipping, avoiding decoding of frames that accurate seeking would discard +anyway; the decoded output is identical to a seek without this option. +Applies to video streams only. 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. +With hardware decoding, decoders that run their @option{skip_frame} check +before submitting frames to the hwaccel benefit (the native H.264, HEVC and +AV1 decoders); the window is not enabled for hardware-only decoders. +The window does not change random-access point selection: streams with +open-GOP random access points keep the demuxer and decoder behavior, and a +packet carrying several access units is treated as a whole. +The identical output assumes the container supplies real presentation +timestamps; for streams whose packet timestamps are merely a linear +decode-order sequence, the first frame after the seek point may differ by +one frame (the same is observable with @option{skip_frame} alone, without +this option). + @item -seek_timestamp (@emph{input}) This option enables or disables seeking by timestamp in input files with the @option{-ss} option. It is disabled by default. If enabled, the argument diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index fbf44bf65a0..70da9e60f27 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -155,6 +155,7 @@ typedef struct OptionsContext { float readrate_catchup; double readrate_initial_burst; int accurate_seek; + int gop_seek; int thread_queue_size; int input_sync_ref; int find_stream_info; @@ -446,6 +447,11 @@ typedef struct DecoderOpts { AVRational time_base; + /* GOP-aware seek window (-gop_seek); gop_seek_pts is the seek target + * in time_base units, valid only when gop_seek is set */ + int gop_seek; + int64_t gop_seek_pts; + // Either forced (when DECODER_FLAG_FRAMERATE_FORCED is set) or // estimated (otherwise) video framerate. AVRational framerate; @@ -524,6 +530,9 @@ typedef struct InputFile { /* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */ int64_t start_time; + /* enable the GOP-aware seek window (-gop_seek) for this input */ + int gop_seek; + /* streams that ffmpeg is aware of; * there may be extra streams in ctx that are not mapped to an InputStream * if new streams appear dynamically during demuxing */ diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c index 1c9ef14f25f..11f6637bd12 100644 --- a/fftools/ffmpeg_dec.c +++ b/fftools/ffmpeg_dec.c @@ -50,6 +50,16 @@ typedef struct DecoderPriv { AVRational framerate_in; + /* GOP-aware seek window (-gop_seek): non-reference packets arriving + * before gop_seek_pts (stream timebase) are submitted with + * AVDISCARD_NONREF; the window closes at the first packet at/after the + * target and is re-armed when the decoder is flushed */ + int gop_seek; + int gop_seek_active; + int64_t gop_seek_pts; + /* user/default skip_frame value, restored when the window closes */ + int gop_seek_skip_user; + // a combination of DECODER_FLAG_*, provided to dec_open() int flags; int apply_cropping; @@ -709,6 +719,18 @@ static int packet_decode(DecoderPriv *dp, AVPacket *pkt, AVFrame *frame) pkt->dts = AV_NOPTS_VALUE; } + /* -gop_seek: skip non-reference packets before the seek target; frames + * still decoded inside the window are dropped by the trim filter, so the + * output matches an accurate seek without the option */ + if (pkt && dp->gop_seek_active) { + if (pkt->pts != AV_NOPTS_VALUE && pkt->pts < dp->gop_seek_pts) + dec->skip_frame = AVDISCARD_NONREF; + else { + dec->skip_frame = dp->gop_seek_skip_user; + dp->gop_seek_active = 0; + } + } + if (pkt) { FrameData *fd = packet_data(pkt); if (!fd) @@ -974,6 +996,7 @@ static int decoder_thread(void *arg) } avcodec_flush_buffers(dp->dec_ctx); + dp->gop_seek_active = dp->gop_seek; } else if (ret < 0) { av_log(dp, AV_LOG_ERROR, "Error processing packet in decoder: %s\n", av_err2str(ret)); @@ -1541,6 +1564,18 @@ static int hw_device_setup_for_decode(DecoderPriv *dp, return 0; } +/* Decoders that run their own skip_frame check before a frame is submitted + * to a hwaccel keep the -gop_seek window active alongside hardware decoding: + * this is every non-hardware-only decoder (the native H.264, HEVC and AV1 + * ones, and the native MPEG-2/VC-1/VP8 variants whose skip checks also run + * before the hwaccel start_frame call). Hardware-only wrapper decoders + * (h264_cuvid, ...) do not consume skip_frame; they carry + * AV_CODEC_CAP_HARDWARE and keep the window disabled with a verbose message. */ +static int gop_seek_hwaccel_supported(const AVCodec *codec) +{ + return !(codec->capabilities & AV_CODEC_CAP_HARDWARE); +} + static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts, const DecoderOpts *o, AVFrame *param_out) { @@ -1553,6 +1588,10 @@ static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts, dp->dec.type = codec->type; dp->framerate_in = o->framerate; + dp->gop_seek = o->gop_seek && o->gop_seek_pts != AV_NOPTS_VALUE; + dp->gop_seek_pts = o->gop_seek_pts; + dp->gop_seek_active = dp->gop_seek; + dp->hwaccel_id = o->hwaccel_id; dp->hwaccel_device_type = o->hwaccel_device_type; dp->hwaccel_output_format = o->hwaccel_output_format; @@ -1640,6 +1679,20 @@ static int dec_open(DecoderPriv *dp, AVDictionary **dec_opts, dp->dec_ctx->extra_hw_frames = extra_frames; } + /* save the user/default skip_frame so the window can restore it instead + * of clobbering an explicit -skip_frame setting */ + dp->gop_seek_skip_user = dp->dec_ctx->skip_frame; + + /* hardware gate: the window stays active only for decoders whose skip + * check provably runs before the hwaccel submission; see + * gop_seek_hwaccel_supported() */ + if (dp->gop_seek && !gop_seek_hwaccel_supported(codec)) { + av_log(dp, AV_LOG_VERBOSE, + "gop_seek is disabled for this hardware decoder, ignoring\n"); + dp->gop_seek = 0; + dp->gop_seek_active = 0; + } + if (dp->dec_ctx->subtitle_header) { /* ASS code assumes this buffer is null terminated so add extra byte. */ dp->dec.subtitle_header = av_mallocz(dp->dec_ctx->subtitle_header_size + 1); diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c index 5409dcdc6c0..592f3e74678 100644 --- a/fftools/ffmpeg_demux.c +++ b/fftools/ffmpeg_demux.c @@ -1054,6 +1054,24 @@ void ifile_close(InputFile **pf) av_freep(pf); } +/* Timestamp used by accurate seeking: the trim filter drops everything before + * it (see ist_filter_add()) and the -gop_seek decoder window treats it as its + * target (see ist_use()). AV_NOPTS_VALUE when unknown, i.e. -ss is missing or + * accurate seeking is disabled. */ +static int64_t demux_trim_start_us(const Demuxer *d) +{ + int64_t tsoffset = 0; + + if (copy_ts) { + tsoffset = d->f.start_time == AV_NOPTS_VALUE ? 0 : d->f.start_time; + if (!start_at_zero && d->f.ctx->start_time != AV_NOPTS_VALUE) + tsoffset += d->f.ctx->start_time; + } + + return ((d->f.start_time == AV_NOPTS_VALUE) || !d->accurate_seek) ? + AV_NOPTS_VALUE : tsoffset; +} + int ist_use(InputStream *ist, int decoding_needed, const ViewSpecifier *vs, SchedulerNode *src) { @@ -1176,6 +1194,25 @@ int ist_use(InputStream *ist, int decoding_needed, ds->dec_opts.log_parent = ist; + /* -gop_seek: arm the decoder-side seek window for video streams when + * the seek target is known, rescaling it to the stream timebase for + * the per-packet comparison done by the decoder */ + if (d->f.gop_seek) { + int64_t trim_start_us = demux_trim_start_us(d); + + if (ist->par->codec_type != AVMEDIA_TYPE_VIDEO) + av_log(ist, AV_LOG_VERBOSE, + "gop_seek applies to video streams only, ignoring\n"); + else if (trim_start_us == AV_NOPTS_VALUE) + av_log(ist, AV_LOG_VERBOSE, + "gop_seek requires -ss with accurate seeking, ignoring\n"); + else { + ds->dec_opts.gop_seek = 1; + ds->dec_opts.gop_seek_pts = av_rescale_q(trim_start_us, AV_TIME_BASE_Q, + ds->dec_opts.time_base); + } + } + ds->decoded_params = av_frame_alloc(); if (!ds->decoded_params) return AVERROR(ENOMEM); @@ -1213,7 +1250,6 @@ int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple, { Demuxer *d = demuxer_from_ifile(ist->file); DemuxStream *ds = ds_from_ist(ist); - int64_t tsoffset = 0; int ret; ret = ist_use(ist, is_simple ? DECODING_FOR_OST : DECODING_FOR_FILTER, @@ -1285,13 +1321,7 @@ int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple, if (ret < 0) return ret; - if (copy_ts) { - tsoffset = d->f.start_time == AV_NOPTS_VALUE ? 0 : d->f.start_time; - if (!start_at_zero && d->f.ctx->start_time != AV_NOPTS_VALUE) - tsoffset += d->f.ctx->start_time; - } - opts->trim_start_us = ((d->f.start_time == AV_NOPTS_VALUE) || !d->accurate_seek) ? - AV_NOPTS_VALUE : tsoffset; + opts->trim_start_us = demux_trim_start_us(d); opts->trim_end_us = d->recording_time; opts->name = av_strdup(ds->dec_name); @@ -2571,6 +2601,7 @@ int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch) f->input_ts_offset = o->input_ts_offset; f->ts_offset = o->input_ts_offset - (copy_ts ? (start_at_zero && ic->start_time != AV_NOPTS_VALUE ? ic->start_time : 0) : timestamp); d->accurate_seek = o->accurate_seek; + f->gop_seek = o->gop_seek; d->loop = o->loop; d->nb_streams_warn = ic->nb_streams; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 2012b5ac559..26267dd16d3 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1648,6 +1648,9 @@ const OptionDef options[] = { { "accurate_seek", OPT_TYPE_BOOL, OPT_OFFSET | OPT_EXPERT | OPT_INPUT, { .off = OFFSET(accurate_seek) }, "enable/disable accurate seeking with -ss" }, + { "gop_seek", OPT_TYPE_BOOL, OPT_OFFSET | OPT_EXPERT | OPT_INPUT, + { .off = OFFSET(gop_seek) }, + "enable GOP-aware frame-exact seeking with -ss" }, { "isync", OPT_TYPE_INT, OPT_OFFSET | OPT_EXPERT | OPT_INPUT, { .off = OFFSET(input_sync_ref) }, "Indicate the input index for sync reference", "sync ref" }, -- 2.54.1 >From fa5b5bde3a13c4e10e1a1cc05ec13c833fd87d6b Mon Sep 17 00:00:00 2001 From: luoye-cpu <[email protected]> Date: Fri, 27 Nov 2026 01:48:05 +0800 Subject: [PATCH 2/2] tests/fate: add gop_seek decode-window cases Add two fate-ffmpeg-gop_seek cases covering the decoder paths the option is expected to work on: the default H.264 seek and the native HEVC decoder. Each case compares the gop_seek output against the same seek without the option, so the trim filter must keep the default path byte-exact, then counts the "decoder ->" lines in the debug_ts output of both runs. The decode-count check is the real proof that the window takes effect: parity alone would also hold for a no-op window, while a reduced count fails if the window stops skipping frames, and the output comparison catches a frame the reference chain needed being skipped. Signed-off-by: luoye-cpu <[email protected]> --- tests/fate/ffmpeg.mak | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak index 96252410a61..24b95be129b 100644 --- a/tests/fate/ffmpeg.mak +++ b/tests/fate/ffmpeg.mak @@ -573,3 +573,41 @@ fate-ffmpeg-mjpeg-444-qns: CMD = framecrc -cpuflags 0 -f lavfi -i "testsrc2=s=32 # alpha merging fate-ffmpeg-heif-merge-alpha: CMD = framecrc -auto_conversion_filters -i $(TARGET_SAMPLES)/heif/alpha.heic FATE_SAMPLES_FFMPEG-$(call FRAMECRC, MOV, HEVC, HEVC_PARSER SCALE_FILTER) += fate-ffmpeg-heif-merge-alpha +# -gop_seek must not change what a mid-GOP -ss seek decodes: the trim filter +# keeps the default path byte-exact. The debug_ts decoder line count proves the +# window actually skips frames instead of being a no-op. +FATE_SAMPLES_FFMPEG-$(call FRAMECRC, MOV, H264,) += fate-ffmpeg-gop_seek +fate-ffmpeg-gop_seek: CMP = null +fate-ffmpeg-gop_seek: CMD = \ + crcbase=tests/data/fate/ffmpeg-gop_seek-base.crc; \ + crcgop=tests/data/fate/ffmpeg-gop_seek-gopseek.crc; \ + dbgbase=tests/data/fate/ffmpeg-gop_seek-dbgbase.log; \ + dbggop=tests/data/fate/ffmpeg-gop_seek-dbggop.log; \ + cleanfiles="$$cleanfiles $$crcbase $$crcgop $$dbgbase $$dbggop"; \ + ffmpeg -ss 1.5 -i $(TARGET_SAMPLES)/h264/interlaced_crop.mp4 -f framecrc - > $$crcbase && \ + ffmpeg -ss 1.5 -gop_seek -i $(TARGET_SAMPLES)/h264/interlaced_crop.mp4 -f framecrc - > $$crcgop && \ + ffmpeg -debug_ts -ss 1.5 -i $(TARGET_SAMPLES)/h264/interlaced_crop.mp4 -f null - 2> $$dbgbase && \ + ffmpeg -debug_ts -ss 1.5 -gop_seek -i $(TARGET_SAMPLES)/h264/interlaced_crop.mp4 -f null - 2> $$dbggop && \ + { cmp -s $$crcbase $$crcgop || { echo "-gop_seek output differs from the default seek"; false; }; } && \ + { nb_decoded=`grep -c "decoder ->" $$dbgbase`; nb_gop=`grep -c "decoder ->" $$dbggop`; \ + test $$nb_gop -lt $$nb_decoded || { echo "-gop_seek did not reduce decode work"; false; }; } + +# HEVC window with the software decoder: dv84.mov carries trailing non-reference +# RASL_N pictures (an N-type, i.e. AVDISCARD_NONREF) inside its open-GOP +# structure. A seek that skips them must leave the default output byte-exact +# while decoding fewer frames than the plain accurate seek. +FATE_SAMPLES_FFMPEG-$(call FRAMECRC, MOV, HEVC, HEVC_PARSER) += fate-ffmpeg-gop_seek-hevc +fate-ffmpeg-gop_seek-hevc: CMP = null +fate-ffmpeg-gop_seek-hevc: CMD = \ + crcbase=tests/data/fate/ffmpeg-gop_seek-hevc-base.crc; \ + crcgop=tests/data/fate/ffmpeg-gop_seek-hevc-gopseek.crc; \ + dbgbase=tests/data/fate/ffmpeg-gop_seek-hevc-dbgbase.log; \ + dbggop=tests/data/fate/ffmpeg-gop_seek-hevc-dbggop.log; \ + cleanfiles="$$cleanfiles $$crcbase $$crcgop $$dbgbase $$dbggop"; \ + ffmpeg -ss 1.08 -i $(TARGET_SAMPLES)/hevc/dv84.mov -map 0:v:0 -f framecrc - > $$crcbase && \ + ffmpeg -ss 1.08 -gop_seek -i $(TARGET_SAMPLES)/hevc/dv84.mov -map 0:v:0 -f framecrc - > $$crcgop && \ + ffmpeg -debug_ts -ss 1.08 -i $(TARGET_SAMPLES)/hevc/dv84.mov -map 0:v:0 -f null - 2> $$dbgbase && \ + ffmpeg -debug_ts -ss 1.08 -gop_seek -i $(TARGET_SAMPLES)/hevc/dv84.mov -map 0:v:0 -f null - 2> $$dbggop && \ + { cmp -s $$crcbase $$crcgop || { echo "-gop_seek output differs from the default seek"; false; }; } && \ + { nb_decoded=`grep -c "decoder ->" $$dbgbase`; nb_gop=`grep -c "decoder ->" $$dbggop`; \ + test $$nb_gop -lt $$nb_decoded || { echo "-gop_seek did not reduce decode work"; false; }; } -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
