PR #26376 opened by luoye-cpu
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26376
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26376.patch

libdav1d maps skip_frame to the dav1d settings once at init, so changing
skip_frame while decoding (for example a seek window that switches it per
packet) has no effect on the library. To make runtime skipping work, inspect
each packet with cbs_av1 in the wrapper before handing it to dav1d: when
skip_frame >= AVDISCARD_NONREF and every frame OBU in the packet is
non-reference (refresh_frame_flags == 0 && frame_type != AV1_FRAME_KEY, the
same predicate as the native av1dec NONREF check), drop the packet without
feeding dav1d, so the AU is never decoded.

show_existing_frame replays are never treated as skippable: cbs_av1 infers
refresh_frame_flags == 0 for non-KEY replays, but a replay produces no new
decode and is the only output of that content.

Conservative fallbacks feed the packet normally: packets carrying a sequence
header, packets without a frame OBU, superframes that mix reference and
non-reference frames, and packets that fail to parse.

The init-time mapping is kept for the whole-stream -skip_frame noref case;
this change adds the per-packet path required by windowed seeking. Adds
cbs_av1 to the libdav1d decoder dependencies (Makefile + configure select)
and FF_CODEC_CAP_INIT_CLEANUP so the cbs state is released on init failure.

Tested: manual verification on a layered SVT-AV1 stream - the window skip
count matches the non-reference fraction and the output matches the plain
seek; runtime-window effectiveness is covered by the fate-ffmpeg-gop_seek-
libdav1d case in PR-1; the existing av1 noref FATE runs through libdav1d.


>From ecd226a1456ed1b0052fcd9d921ca975d4d5e731 Mon Sep 17 00:00:00 2001
From: luoye-cpu <[email protected]>
Date: Fri, 27 Nov 2026 01:48:05 +0800
Subject: [PATCH] avcodec/libdav1d: honor runtime skip_frame for non-reference
 frame skipping

dav1d consumes skip_frame only when it is mapped onto
Dav1dSettings.decode_frame_type at init time; there is no knob to change
decode_frame_type afterwards, so a runtime window that rewrites
avctx->skip_frame per packet (as -gop_seek does) was silently ignored
and the window degraded to a full decode.

While the runtime skip_frame is AVDISCARD_NONREF or higher, inspect each
packet with the in-tree cbs_av1 parser before handing it to dav1d and
drop it when every frame header it carries satisfies the same predicate
as av1dec.c (refresh_frame_flags == 0 && frame_type != AV1_FRAME_KEY).
Per AV1 spec section 7.20 such a frame updates no reference slot, so
dropping the AU is a no-op for the reference chain; KEY frames stay
exempt because a delayed random access point may read
refresh_frame_flags as 0, and show_existing frames are exempted
explicitly: cbs_av1 infers refresh_frame_flags == 0 for them, yet the
replay is the frame's only output. Packets are always fed unchanged when
they carry a sequence header OBU, mixed frame headers, no frame OBU, no
PTS, or fail to parse.

A packet is dropped only when it carries complete, independently
submittable frame data: at least one AV1_OBU_TILE_GROUP or one
AV1_OBU_FRAME. A frame-header-only (or redundant-frame-header-only)
packet is never dropped even when the header hits the predicate, because
its tile group arrives in a later packet; temporal-delimiter-only and
tile-group-only packets are fed as well. The sequence header is kept in
the cbs context across packets, as in av1dec.c and av1_parser.c. The
whole-stream -skip_frame noref mapping at init time is untouched.

The wrapper now allocates cbs state in init and frees it in close, so
the decoder is marked FF_CODEC_CAP_INIT_CLEANUP to make sure close runs
when init fails partway. The cbs context is primed from extradata (same
AV1CodecConfigurationRecord offset as libdav1d_parse_extradata) and
re-primed after a flush, because ff_cbs_flush() drops the sequence
header state and a stream may not repeat it in-band; without priming,
a stream whose sequence header lives only in extradata would silently
degrade the window to a no-op.

The runtime window is covered at the API level by
fate-api-gopseek-libdav1d: it switches skip_frame to AVDISCARD_NONREF
while the decoder stays open on an IVF sample and asserts that the
decoded frame count drops without errors.

Signed-off-by: luoye-cpu <[email protected]>
---
 configure                             |   2 +-
 libavcodec/Makefile                   |   2 +-
 libavcodec/libdav1d.c                 | 188 ++++++++++++++++++-
 tests/api/Makefile                    |   4 +
 tests/api/api-gopseek-libdav1d-test.c | 250 ++++++++++++++++++++++++++
 tests/fate/api.mak                    |   8 +
 6 files changed, 451 insertions(+), 3 deletions(-)
 create mode 100644 tests/api/api-gopseek-libdav1d-test.c

diff --git a/configure b/configure
index eed7a76aa92..e12a6c4ac44 100755
--- a/configure
+++ b/configure
@@ -3818,7 +3818,7 @@ libastcenc_decoder_deps="libastcenc"
 libcodec2_decoder_deps="libcodec2"
 libcodec2_encoder_deps="libcodec2"
 libdav1d_decoder_deps="libdav1d"
-libdav1d_decoder_select="itut_t35"
+libdav1d_decoder_select="cbs_av1 itut_t35"
 libdavs2_decoder_deps="libdavs2"
 libdavs2_decoder_select="avs2_parser"
 libfdk_aac_decoder_deps="libfdk_aac"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index c3da8412c60..69578977bc2 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1193,7 +1193,7 @@ OBJS-$(CONFIG_LIBASTCENC_ENCODER)         += libastcenc.o
 OBJS-$(CONFIG_LIBASTCENC_DECODER)         += libastcdec.o
 OBJS-$(CONFIG_LIBCODEC2_DECODER)          += libcodec2.o
 OBJS-$(CONFIG_LIBCODEC2_ENCODER)          += libcodec2.o
-OBJS-$(CONFIG_LIBDAV1D_DECODER)           += libdav1d.o av1_parse.o
+OBJS-$(CONFIG_LIBDAV1D_DECODER)           += libdav1d.o av1_parse.o cbs_av1.o
 OBJS-$(CONFIG_LIBDAVS2_DECODER)           += libdavs2.o
 OBJS-$(CONFIG_LIBFDK_AAC_DECODER)         += libfdk-aacdec.o
 OBJS-$(CONFIG_LIBFDK_AAC_ENCODER)         += libfdk-aacenc.o
diff --git a/libavcodec/libdav1d.c b/libavcodec/libdav1d.c
index 0f713efac2a..ea079158b50 100644
--- a/libavcodec/libdav1d.c
+++ b/libavcodec/libdav1d.c
@@ -27,12 +27,15 @@
 #include "libavutil/hdr_dynamic_metadata.h"
 #include "libavutil/mastering_display_metadata.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/macros.h"
 #include "libavutil/opt.h"
 
 #include "atsc_a53.h"
 #include "av1_parse.h"
 #include "avcodec.h"
 #include "bytestream.h"
+#include "cbs.h"
+#include "cbs_av1.h"
 #include "codec_internal.h"
 #include "decode.h"
 #include "dovi_rpu.h"
@@ -54,6 +57,12 @@ typedef struct Libdav1dContext {
     int apply_grain;
     int operating_point;
     int all_layers;
+
+    /* Runtime skip_frame window (-gop_seek) support; see
+       libdav1d_probe_skippable(). */
+    CodedBitstreamContext *cbc;
+    CodedBitstreamFragment frag;
+    int need_reprime;
 } Libdav1dContext;
 
 static const enum AVPixelFormat pix_fmt[][3] = {
@@ -199,6 +208,44 @@ static av_cold int libdav1d_parse_extradata(AVCodecContext 
*c)
     return 0;
 }
 
+/**
+ * Seed the cbs_av1 persistent state from extradata, using the same
+ * AV1CodecConfigurationRecord offset as libdav1d_parse_extradata().
+ *
+ * Needed because the probe parses frame headers with cbs_av1: for streams
+ * whose sequence header only lives in extradata, ff_cbs_read_packet() would
+ * otherwise find no sequence header and fail to parse the frame headers,
+ * which silently disables the runtime window.  Errors are deliberately
+ * ignored - the probe then simply declines to drop anything.
+ */
+static void libdav1d_prime_cbs(AVCodecContext *c)
+{
+    Libdav1dContext *dav1d = c->priv_data;
+    size_t offset = 0;
+
+    if (!c->extradata || c->extradata_size <= 0)
+        return;
+
+    if (c->extradata[0] & 0x80) {
+        if (c->extradata_size < 4)
+            return;
+        offset = 4;
+    }
+
+    ff_cbs_read(dav1d->cbc, &dav1d->frag, NULL,
+                c->extradata + offset, c->extradata_size - offset);
+    ff_cbs_fragment_reset(&dav1d->frag);
+}
+
+static const CodedBitstreamUnitType libdav1d_probe_unit_types[] = {
+    AV1_OBU_TEMPORAL_DELIMITER,
+    AV1_OBU_SEQUENCE_HEADER,
+    AV1_OBU_FRAME_HEADER,
+    AV1_OBU_TILE_GROUP,
+    AV1_OBU_FRAME,
+    AV1_OBU_REDUNDANT_FRAME_HEADER,
+};
+
 static av_cold int libdav1d_init(AVCodecContext *c)
 {
     Libdav1dContext *dav1d = c->priv_data;
@@ -242,6 +289,15 @@ static av_cold int libdav1d_init(AVCodecContext *c)
         s.decode_frame_type = DAV1D_DECODEFRAMETYPE_REFERENCE;
 #endif
 
+    /* cbs_av1 is used to read frame headers for the runtime skip_frame
+       window below; see libdav1d_probe_skippable(). */
+    res = ff_cbs_init(&dav1d->cbc, AV_CODEC_ID_AV1, c);
+    if (res < 0)
+        return res;
+    dav1d->cbc->decompose_unit_types    = libdav1d_probe_unit_types;
+    dav1d->cbc->nb_decompose_unit_types = 
FF_ARRAY_ELEMS(libdav1d_probe_unit_types);
+    libdav1d_prime_cbs(c);
+
     res = libdav1d_parse_extradata(c);
     if (res < 0)
         return res;
@@ -272,6 +328,10 @@ static void libdav1d_flush(AVCodecContext *c)
     Libdav1dContext *dav1d = c->priv_data;
 
     dav1d_data_unref(&dav1d->data);
+    ff_cbs_flush(dav1d->cbc);
+    /* ff_cbs_flush() drops the sequence header state; packets may not repeat
+     * it, so restore it from extradata before the next probe. */
+    dav1d->need_reprime = 1;
     dav1d_flush(dav1d->c);
 }
 
@@ -287,6 +347,121 @@ static void libdav1d_user_data_free(const uint8_t *data, 
void *opaque) {
     av_packet_free(&pkt);
 }
 
+/*
+ * Runtime skip_frame window support.
+ *
+ * skip_frame reaches libdav1d at two layers:
+ *
+ * - libdav1d_init() maps a static AVDiscard setting onto
+ *   Dav1dSettings.decode_frame_type once at init time, and dav1d then
+ *   performs the skipping internally.  This is the whole-stream
+ *   "-skip_frame noref" behaviour and is left untouched here.
+ *
+ * - A runtime window (fftools' -gop_seek rewrites avctx->skip_frame
+ *   for every packet before submission) cannot be mapped onto dav1d:
+ *   Dav1dSettings is consumed by dav1d_open() and dav1d offers no knob
+ *   to change decode_frame_type afterwards, so runtime changes were
+ *   silently ignored and the window degraded to a full decode.
+ *
+ * This probe closes that gap at the wrapper layer.  While the runtime
+ * skip_frame is AVDISCARD_NONREF or higher, each packet is read with
+ * the in-tree cbs_av1 parser (as av1dec.c and av1_parser.c do) before
+ * being handed to dav1d, and the packet is dropped without ever
+ * reaching dav1d when every frame header it carries hits the predicate
+ * shared with av1dec.c:
+ *
+ *     refresh_frame_flags == 0 && frame_type != AV1_FRAME_KEY &&
+ *     !show_existing_frame
+ *
+ * Per AV1 spec section 7.20, a frame with refresh_frame_flags == 0
+ * updates no reference slot, so later frames can never depend on its
+ * decoded output; dropping the AU is therefore a true decode saving
+ * (dav1d never parses it) and a no-op for the reference chain.  KEY
+ * frames are kept because a delayed RAP (show_frame == 0) may read
+ * refresh_frame_flags as 0 (section 7.6.2).  show_existing_frame
+ * replays are kept as well: cbs_av1 infers refresh_frame_flags == 0
+ * for a non-KEY replay (section 7.20), but the replay carries no tile
+ * data to save and is the frame's only output, so dropping it would
+ * lose displayed frames for free.  The sequence header is kept in the
+ * cbs context across packets, as in av1dec.c and av1_parser.c; nothing
+ * else about the decision is cached.
+ *
+ * The packet is fed to dav1d unchanged on any parse failure, when it
+ * carries no frame OBU, when its frame OBUs do not unanimously hit the
+ * predicate (mixed or multi-frame packets stay untouched), when it
+ * carries a sequence header OBU (dropping it would strand dav1d with
+ * stale sequence parameters), or when it has no PTS.
+ *
+ * A packet is only dropped when it itself carries a complete,
+ * independently submittable frame: at least one AV1_OBU_TILE_GROUP or a
+ * complete AV1_OBU_FRAME.  A packet holding only a frame header (or only
+ * a redundant frame header) is fed normally even when the header hits
+ * the predicate - its tile group arrives in a later packet, so dropping
+ * the header would hand dav1d tile data without the header describing
+ * it.  Temporal-delimiter-only and tile-group-only packets are fed as
+ * well (conservative).  Whole-AU inputs (IVF/MP4, where one packet is
+ * FH+TG or a single OBU_FRAME) still get the full optimisation.
+ *
+ * The cbs context is primed from extradata at init and re-primed after a
+ * flush (see libdav1d_prime_cbs()): a stream whose sequence header lives
+ * only in extradata would otherwise have no sequence header for the frame
+ * header parse, silently turning the window into a no-op.
+ */
+static int libdav1d_probe_skippable(AVCodecContext *c, const AVPacket *pkt)
+{
+    Libdav1dContext *dav1d = c->priv_data;
+    CodedBitstreamFragment *frag = &dav1d->frag;
+    int nb_frames = 0, nb_skippable = 0, has_seq = 0, has_frame_data = 0;
+    int ret;
+
+    if (pkt->pts == AV_NOPTS_VALUE)
+        return 0;
+
+    if (dav1d->need_reprime) {
+        libdav1d_prime_cbs(c);
+        dav1d->need_reprime = 0;
+    }
+
+    ret = ff_cbs_read_packet(dav1d->cbc, frag, pkt);
+    if (ret >= 0) {
+        for (int i = 0; i < frag->nb_units; i++) {
+            const CodedBitstreamUnit *unit = &frag->units[i];
+            const AV1RawOBU *obu = unit->content;
+            const AV1RawFrameHeader *frame;
+
+            if (unit->type == AV1_OBU_TILE_GROUP ||
+                unit->type == AV1_OBU_FRAME)
+                has_frame_data = 1;
+
+            if (unit->type == AV1_OBU_SEQUENCE_HEADER)
+                has_seq = 1;
+
+            if (unit->type == AV1_OBU_FRAME)
+                frame = &obu->obu.frame.header;
+            else if (unit->type == AV1_OBU_FRAME_HEADER ||
+                     unit->type == AV1_OBU_REDUNDANT_FRAME_HEADER)
+                frame = &obu->obu.frame_header;
+            else
+                continue;
+
+            nb_frames++;
+            /* Same predicate as av1dec.c's AVDISCARD_NONREF check, plus
+             * show_existing_frame: cbs_av1 infers refresh_frame_flags == 0
+             * for a non-KEY replay, yet a replay decodes no tile data and
+             * is the frame's only output. */
+            if (!frame->show_existing_frame &&
+                !frame->refresh_frame_flags &&
+                frame->frame_type != AV1_FRAME_KEY)
+                nb_skippable++;
+        }
+    }
+
+    ff_cbs_fragment_reset(frag);
+
+    return nb_frames > 0 && nb_frames == nb_skippable && !has_seq &&
+           has_frame_data;
+}
+
 static int libdav1d_receive_frame_internal(AVCodecContext *c, Dav1dPicture *p)
 {
     Libdav1dContext *dav1d = c->priv_data;
@@ -306,6 +481,14 @@ static int libdav1d_receive_frame_internal(AVCodecContext 
*c, Dav1dPicture *p)
         }
 
         if (pkt->size) {
+            if (c->skip_frame >= AVDISCARD_NONREF &&
+                libdav1d_probe_skippable(c, pkt)) {
+                av_log(c, AV_LOG_DEBUG, "Skipping non-reference frame "
+                       "(runtime skip_frame window)\n");
+                av_packet_free(&pkt);
+                return 1;
+            }
+
             res = dav1d_data_wrap(data, pkt->data, pkt->size,
                                   libdav1d_data_free, pkt->buf);
             if (res < 0) {
@@ -570,6 +753,8 @@ static av_cold int libdav1d_close(AVCodecContext *c)
     av_buffer_pool_uninit(&dav1d->pool);
     ff_dovi_ctx_unref(&dav1d->dovi);
     dav1d_data_unref(&dav1d->data);
+    ff_cbs_fragment_free(&dav1d->frag);
+    ff_cbs_close(&dav1d->cbc);
     dav1d_close(&dav1d->c);
 
     return 0;
@@ -614,7 +799,8 @@ const FFCodec ff_libdav1d_decoder = {
     FF_CODEC_RECEIVE_FRAME_CB(libdav1d_receive_frame),
     .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS,
     .caps_internal  = FF_CODEC_CAP_SETS_FRAME_PROPS |
-                      FF_CODEC_CAP_AUTO_THREADS,
+                      FF_CODEC_CAP_AUTO_THREADS |
+                      FF_CODEC_CAP_INIT_CLEANUP,
     .p.priv_class   = &libdav1d_class,
     .p.wrapper_name = "libdav1d",
 };
diff --git a/tests/api/Makefile b/tests/api/Makefile
index 75df91492d9..a25dd3ce8d0 100644
--- a/tests/api/Makefile
+++ b/tests/api/Makefile
@@ -8,6 +8,10 @@ APITESTPROGS-$(CONFIG_MOV_MUXER)        += api-movenc
 APITESTPROGS-$(CONFIG_MOVTEXT_ENCODER)  += api-movtext
 APITESTPROGS-$(call ALLYES, HEVC_MP4TOANNEXB_BSF HEVC_DECODER) += 
api-hevc-mp4toannexb
 APITESTPROGS-yes += api-seek api-dump-stream-meta
+# Decodes an IVF sample through the libdav1d wrapper, so the demuxer gate is
+# IVF (not AV1) - the same sample and container as the FATE case in
+# tests/fate/api.mak.
+APITESTPROGS-$(call ALLYES, IVF_DEMUXER LIBDAV1D_DECODER FILE_PROTOCOL) += 
api-gopseek-libdav1d
 APITESTPROGS-$(call DEMDEC, H263, H263) += api-band
 APITESTPROGS-$(HAVE_THREADS) += api-threadmessage
 APITESTPROGS-$(CONFIG_AVCODEC) += api-enc-info
diff --git a/tests/api/api-gopseek-libdav1d-test.c 
b/tests/api/api-gopseek-libdav1d-test.c
new file mode 100644
index 00000000000..74d2e917c78
--- /dev/null
+++ b/tests/api/api-gopseek-libdav1d-test.c
@@ -0,0 +1,250 @@
+/*
+ * Copyright (c) 2026 FFmpeg developers
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/**
+ * Runtime skip_frame window test for the libdav1d wrapper.
+ *
+ * Pass A decodes the whole stream with skip_frame == AVDISCARD_NONE and
+ * checksums every output frame.  Pass B decodes the same stream but switches
+ * avctx->skip_frame to AVDISCARD_NONREF after the first few frames, emulating
+ * an external caller (fftools' -gop_seek) that changes skip_frame while the
+ * decoder is already open.  That runtime change is exactly what the wrapper
+ * probe has to honour; a wrapper that only maps skip_frame onto the decoder at
+ * init time would decode every frame in pass B as well.
+ *
+ * Asserted: pass B decodes strictly fewer frames than pass A and neither pass
+ * reports a decode error.  Pixel identity across the passes is deliberately
+ * not asserted - the skipped frames are legitimately absent from pass B.
+ */
+
+#include <inttypes.h>
+
+#include "libavutil/adler32.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
+#include "libavcodec/avcodec.h"
+#include "libavformat/avformat.h"
+
+/* Switch to AVDISCARD_NONREF once this many frames have been output. */
+#define WINDOW_AFTER_FRAME 2
+
+static int drain_frames(AVCodecContext *dec, AVFrame *frame,
+                        uint8_t **buf, int *buf_size,
+                        int *nb_decoded, uint32_t *crc,
+                        int window_after, int *window_open)
+{
+    int ret;
+
+    for (;;) {
+        int need, written;
+
+        ret = avcodec_receive_frame(dec, frame);
+        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
+            return 0;
+        if (ret < 0) {
+            av_log(dec, AV_LOG_ERROR, "Error decoding frame\n");
+            return ret;
+        }
+
+        need = av_image_get_buffer_size(frame->format, frame->width,
+                                        frame->height, 1);
+        if (need < 0) {
+            av_frame_unref(frame);
+            return need;
+        }
+        if (need > *buf_size) {
+            av_freep(buf);
+            *buf = av_malloc(need);
+            if (!*buf) {
+                av_frame_unref(frame);
+                av_log(dec, AV_LOG_ERROR, "Cannot allocate frame buffer\n");
+                return AVERROR(ENOMEM);
+            }
+            *buf_size = need;
+        }
+
+        written = av_image_copy_to_buffer(*buf, need,
+                                          (const uint8_t * const *)frame->data,
+                                          (const int *)frame->linesize,
+                                          frame->format, frame->width,
+                                          frame->height, 1);
+        if (written < 0) {
+            av_frame_unref(frame);
+            av_log(dec, AV_LOG_ERROR, "Cannot copy image to buffer\n");
+            return written;
+        }
+        *crc = av_adler32_update(*crc, *buf, written);
+        (*nb_decoded)++;
+
+        if (window_after >= 0 && !*window_open &&
+            *nb_decoded > window_after) {
+            av_log(dec, AV_LOG_VERBOSE, "Opening skip_frame window\n");
+            dec->skip_frame = AVDISCARD_NONREF;
+            *window_open = 1;
+        }
+
+        av_frame_unref(frame);
+    }
+}
+
+static int decode_pass(const char *filename, int window_after,
+                       int *nb_decoded, uint32_t *checksum)
+{
+    AVFormatContext *fmt_ctx = NULL;
+    AVCodecContext *dec = NULL;
+    const AVCodec *codec = NULL;
+    AVPacket *pkt = NULL;
+    AVFrame *frame = NULL;
+    uint8_t *buf = NULL;
+    int buf_size = 0;
+    int video_stream;
+    int window_open = 0;
+    int ret;
+
+    *nb_decoded = 0;
+    *checksum = 0;
+
+    ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
+        goto end;
+    }
+
+    ret = avformat_find_stream_info(fmt_ctx, NULL);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot get stream info\n");
+        goto end;
+    }
+
+    video_stream = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1,
+                                       &codec, 0);
+    if (video_stream < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot find video stream\n");
+        ret = video_stream;
+        goto end;
+    }
+
+    dec = avcodec_alloc_context3(codec);
+    if (!dec) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot allocate decoder context\n");
+        ret = AVERROR(ENOMEM);
+        goto end;
+    }
+
+    ret = avcodec_parameters_to_context(dec,
+                                        
fmt_ctx->streams[video_stream]->codecpar);
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot copy decoder parameters\n");
+        goto end;
+    }
+
+    /* Open with no skipping, so pass B really exercises a runtime change. */
+    dec->skip_frame = AVDISCARD_NONE;
+
+    ret = avcodec_open2(dec, codec, NULL);
+    if (ret < 0) {
+        av_log(dec, AV_LOG_ERROR, "Cannot open decoder\n");
+        goto end;
+    }
+
+    pkt = av_packet_alloc();
+    frame = av_frame_alloc();
+    if (!pkt || !frame) {
+        av_log(NULL, AV_LOG_ERROR, "Cannot allocate packet/frame\n");
+        ret = AVERROR(ENOMEM);
+        goto end;
+    }
+
+    while ((ret = av_read_frame(fmt_ctx, pkt)) >= 0) {
+        if (pkt->stream_index != video_stream) {
+            av_packet_unref(pkt);
+            continue;
+        }
+
+        ret = avcodec_send_packet(dec, pkt);
+        av_packet_unref(pkt);
+        if (ret < 0) {
+            av_log(dec, AV_LOG_ERROR, "Error submitting a packet\n");
+            goto end;
+        }
+
+        ret = drain_frames(dec, frame, &buf, &buf_size, nb_decoded, checksum,
+                           window_after, &window_open);
+        if (ret < 0)
+            goto end;
+    }
+    ret = AVERROR_EOF;
+
+    ret = avcodec_send_packet(dec, NULL);
+    if (ret < 0 && ret != AVERROR_EOF) {
+        av_log(dec, AV_LOG_ERROR, "Error submitting drain packet\n");
+        goto end;
+    }
+    ret = drain_frames(dec, frame, &buf, &buf_size, nb_decoded, checksum,
+                       window_after, &window_open);
+
+end:
+    av_freep(&buf);
+    av_packet_free(&pkt);
+    av_frame_free(&frame);
+    avcodec_free_context(&dec);
+    avformat_close_input(&fmt_ctx);
+    return ret < 0 ? ret : 0;
+}
+
+int main(int argc, char **argv)
+{
+    int nb_all, nb_noref;
+    uint32_t crc_all, crc_noref;
+    int ret;
+
+    if (argc < 2) {
+        av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file>\n", argv[0]);
+        return 1;
+    }
+
+    ret = decode_pass(argv[1], -1, &nb_all, &crc_all);
+    if (ret < 0)
+        return 1;
+
+    ret = decode_pass(argv[1], WINDOW_AFTER_FRAME, &nb_noref, &crc_noref);
+    if (ret < 0)
+        return 1;
+
+    printf("all frames=%d crc=%08"PRIx32"\n", nb_all, crc_all);
+    printf("noref frames=%d crc=%08"PRIx32"\n", nb_noref, crc_noref);
+
+    if (nb_all <= WINDOW_AFTER_FRAME) {
+        av_log(NULL, AV_LOG_ERROR,
+               "Input too short for a runtime window test (%d frames)\n",
+               nb_all);
+        return 1;
+    }
+    if (nb_noref >= nb_all) {
+        av_log(NULL, AV_LOG_ERROR,
+               "Runtime skip_frame window did not reduce decodes (%d >= %d)\n",
+               nb_noref, nb_all);
+        return 1;
+    }
+
+    return 0;
+}
diff --git a/tests/fate/api.mak b/tests/fate/api.mak
index 69df95b90d0..b3aa9a9ffd9 100644
--- a/tests/fate/api.mak
+++ b/tests/fate/api.mak
@@ -58,6 +58,14 @@ fate-lavf-flv: CMP =
 fate-api-seek: CMD = run $(APITESTSDIR)/api-seek-test$(EXESUF) 
$(TARGET_PATH)/tests/data/lavf/lavf.flv 0 720
 fate-api-seek: CMP = null
 
+# Runtime skip_frame window in the libdav1d wrapper: the sample interleaves
+# non-reference inter frames, so switching skip_frame to AVDISCARD_NONREF while
+# the decoder is open must reduce the number of decoded frames without errors.
+FATE_API_SAMPLES_LIBAVFORMAT-$(call ALLYES, IVF_DEMUXER LIBDAV1D_DECODER 
FILE_PROTOCOL) += fate-api-gopseek-libdav1d
+fate-api-gopseek-libdav1d: $(APITESTSDIR)/api-gopseek-libdav1d-test$(EXESUF)
+fate-api-gopseek-libdav1d: CMD = run 
$(APITESTSDIR)/api-gopseek-libdav1d-test$(EXESUF) 
$(TARGET_SAMPLES)/av1/av1-1-b8-22-svc-L1T2.ivf
+fate-api-gopseek-libdav1d: CMP = null
+
 FATE_API_LIBAVFORMAT-$(call ALLYES, MP4_MUXER ISMV_MUXER) += fate-api-movenc
 fate-api-movenc: $(APITESTSDIR)/api-movenc-test$(EXESUF)
 fate-api-movenc: CMD = run $(APITESTSDIR)/api-movenc-test$(EXESUF)
-- 
2.54.1

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

Reply via email to