PR #26128 opened by walidbarakat URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26128 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26128.patch
# Summary of changes Speeds up seeking / keyframe-to-keyframe scrubbing with the QSV decoders. `avcodec_flush_buffers()` on `*_qsv` currently sets `initialized = 0`, so the next packet tears down the MFX decoder and redoes DecodeHeader, QueryIOSurf, hw_frames_ctx allocation and MFXVideoDECODE_Init. Players pay that on every seek. Two commits: 1. **lavc/qsvdec: drop pending async frames on flush** (bugfix) `qsv_decode_flush()` left entries in `async_fifo`; their `mfxSyncPoint`s were later passed to `SyncOperation()` on the re-created decoder and the associated `QSVFrame`s stayed pinned (`queued > 0`). New `qsv_clear_async()` drains the fifo, frees the sync points and resets the frame bookkeeping. 2. **lavc/qsvdec: use MFXVideoDECODE_Reset for flushing** Try `GetVideoParam` + `MFXVideoDECODE_Reset()` first; this keeps the session and surface pool alive and only resets decoding state. Falls back to the existing full reinit if the decoder was never initialized or Reset fails. Adds a Changelog entry. Testing (Arrow Lake, oneVPL 2.16, iHD 25.4.2, `--enable-libvpl`): - `ffmpeg -c:v h264_qsv -stream_loop 2 -i in.mp4 -f framecrc -` (stream_loop triggers `avcodec_flush_buffers()` between loops): fast path taken on both flushes, 360 frames, CRCs bit-identical to pre-patch output, identical across loops, pts/sizes match the software `h264` decoder. Same result with `-hwaccel qsv -hwaccel_output_format qsv` (video memory path). - flush + first frame after seek: ~15-20 ms -> ~2-3 ms. - valgrind `--leak-check=full` over 4 flush cycles: clean. - `fate-source`, `tools/patcheck`, `git diff --check`: clean. Not exercised on this hardware: the Reset-failure fallback (needs a mid-stream resolution change); the code path is the unchanged pre-patch re-init. >From 3a0bccfd7ca1684d9b292d89b95825c94ddc37d3 Mon Sep 17 00:00:00 2001 From: Walid <[email protected]> Date: Tue, 17 Nov 2026 12:01:33 +0100 Subject: [PATCH 1/2] lavc/qsvdec: drop pending async frames on flush qsv_decode_flush() only cleared the packet FIFO and forced a decoder reinit. Entries still sitting in async_fifo kept their mfxSyncPoint and their QSVFrame kept queued > 0, so after the reinit those stale sync points were handed to MFXVideoCORE_SyncOperation() on the new decoder instance and the frames stayed pinned until the next qsv_clear_unused_frames() happened to see them. Add qsv_clear_async() which drains async_fifo, frees the sync points, resets the queued/used bookkeeping on every work frame that the SDK is not holding locked, and clears zero_consume_run/reinit_flag so the decoder starts the next segment from a clean state. Signed-off-by: Walid <[email protected]> --- libavcodec/qsvdec.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c index 990e46ea1d7..097dae35cb3 100644 --- a/libavcodec/qsvdec.c +++ b/libavcodec/qsvdec.c @@ -572,6 +572,33 @@ static void qsv_clear_unused_frames(QSVContext *q) } } +static void qsv_clear_async(QSVContext *q) +{ + QSVAsyncFrame aframe; + QSVFrame *cur; + if (q->async_fifo) { + while (av_fifo_read(q->async_fifo, &aframe, 1) >= 0) { + if (aframe.frame && aframe.frame->queued > 0) + aframe.frame->queued--; + av_freep(&aframe.sync); + } + } + + cur = q->work_frames; + + while (cur) { + cur->queued = 0; + if (cur->used && !cur->surface.Data.Locked) { + cur->used = 0; + av_frame_unref(cur->frame); + } + cur = cur->next; + } + + q->zero_consume_run = 0; + q->reinit_flag = 0; +} + static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf) { QSVFrame *frame, **last; @@ -1181,6 +1208,7 @@ static void qsv_decode_flush(AVCodecContext *avctx) QSVDecContext *s = avctx->priv_data; qsv_clear_buffers(s); + qsv_clear_async(&s->qsv); s->qsv.orig_pix_fmt = AV_PIX_FMT_NONE; s->qsv.initialized = 0; -- 2.54.0 >From fe1c9f38093673d9d0cba928125cb55c16ec3ded Mon Sep 17 00:00:00 2001 From: Walid <[email protected]> Date: Tue, 17 Nov 2026 12:01:59 +0100 Subject: [PATCH 2/2] lavc/qsvdec: use MFXVideoDECODE_Reset for flushing Flushing the decoder previously set initialized = 0, which on the next packet tears down the MFX decoder and goes through DecodeHeader, QueryIOSurf, hw_frames_ctx allocation and MFXVideoDECODE_Init again. Players that seek keyframe-to-keyframe (scrubbing) pay that cost on every seek. Try MFXVideoDECODE_Reset() with the current parameters first; it keeps the session and the surface pool alive and only resets the decoding state. Fall back to the old full reinit if the decoder was never initialized or if GetVideoParam/Reset fail. Measured on Arrow Lake with oneVPL 2.16 / iHD 25.4.2, h264_qsv, 640x360 stream: flush+first frame drops from ~15-20 ms to ~2-3 ms. Output of a -stream_loop 2 decode is bit-identical to the pre-patch output in both system and video memory modes. Signed-off-by: Walid <[email protected]> --- Changelog | 1 + libavcodec/qsvdec.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Changelog b/Changelog index bce79b181f7..36def28606d 100644 --- a/Changelog +++ b/Changelog @@ -6,6 +6,7 @@ version <next>: - LCEVC raw demuxer and muxer - AVS+ (AVS1-P16, Broadcasting profile) support in the Chinese AVS decoder - PCM PAMF decoder +- QSV decoders flush via MFXVideoDECODE_Reset instead of a full reinit version 9.1: - ASTC image encoding and decoding via libastcenc, with .astc and KTX 1.0 muxers and demuxers diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c index 097dae35cb3..b0ef74947a7 100644 --- a/libavcodec/qsvdec.c +++ b/libavcodec/qsvdec.c @@ -1206,12 +1206,51 @@ static int qsv_decode_frame(AVCodecContext *avctx, AVFrame *frame, static void qsv_decode_flush(AVCodecContext *avctx) { QSVDecContext *s = avctx->priv_data; + mfxStatus sts; + mfxVideoParam param = { 0 }; + int reset_succeeded = 0; qsv_clear_buffers(s); qsv_clear_async(&s->qsv); - s->qsv.orig_pix_fmt = AV_PIX_FMT_NONE; - s->qsv.initialized = 0; + // MFXVideoDECODE_Reset keeps the session and surface pool alive, which is + // much cheaper than Close/DecodeHeader/Init on every seek. + if (s->qsv.session && s->qsv.initialized) { + av_log(avctx, AV_LOG_DEBUG, + "Attempting QSV decoder reset for fast flush\n"); + + sts = MFXVideoDECODE_GetVideoParam(s->qsv.session, ¶m); + + if (sts == MFX_ERR_NONE) { + // Restore critical parameters for reset + param.AsyncDepth = s->qsv.async_depth; + param.IOPattern = s->qsv.iopattern; + param.ExtParam = s->qsv.ext_buffers; + param.NumExtParam = s->qsv.nb_ext_buffers; + + sts = MFXVideoDECODE_Reset(s->qsv.session, ¶m); + if (sts >= 0) { + av_log(avctx, AV_LOG_DEBUG, + "QSV decoder reset successful (fast path)\n"); + s->qsv.frame_info = param.mfx.FrameInfo; + reset_succeeded = 1; + } else { + ff_qsv_print_warning(avctx, sts, + "Decoder reset failed, reinitializing"); + } + } else { + ff_qsv_print_warning(avctx, sts, + "GetVideoParam failed, reinitializing"); + } + } + + if (!reset_succeeded) { + // Fallback to full reinitialization + av_log(avctx, AV_LOG_DEBUG, + "Using full QSV decoder reinitialization\n"); + s->qsv.orig_pix_fmt = AV_PIX_FMT_NONE; + s->qsv.initialized = 0; + } } #define OFFSET(x) offsetof(QSVDecContext, x) -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
