PR #26288 opened by Baptiste Coudurier (bcoudurier)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26288
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26288.patch

An SPS with a signalled reorder limit never learns a depth, so after a flush it 
now starts from that limit instead of a depth inherited from a previous stream.

Depends on https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26035 (first commit). 
Reported by Forgejo_Fairy there.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



From db2195c754a3d62c2ce6b45928f9a6cdbaacf53c Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Fri, 13 Nov 2026 14:02:23 -0800
Subject: [PATCH 1/2] avcodec/h264dec: reset has_b_frames when a new SPS
 follows a flush

The learned reorder depth delayed output after a stream switch. Known SPSs
keep their depth, or the highest seen without a signalled limit, so replays
do not drop pictures; this can delay a return to a low-delay stream.
The largest evicted depth is the floor for unknown SPSs.

Fixes: #25321

Signed-off-by: Baptiste Coudurier <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 libavcodec/h264_slice.c         |  58 ++++++++-
 libavcodec/h264dec.c            |   5 +-
 libavcodec/h264dec.h            |  12 ++
 tests/api/Makefile              |   1 +
 tests/api/api-h264-flush-test.c | 223 ++++++++++++++++++++++++++++++++
 tests/fate/api.mak              |   8 ++
 tests/ref/fate/api-h264-flush-1 |  15 +++
 tests/ref/fate/api-h264-flush-4 |  15 +++
 8 files changed, 334 insertions(+), 3 deletions(-)
 create mode 100644 tests/api/api-h264-flush-test.c
 create mode 100644 tests/ref/fate/api-h264-flush-1
 create mode 100644 tests/ref/fate/api-h264-flush-4

diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
index b5953007bac..8a6dfd97662 100644
--- a/libavcodec/h264_slice.c
+++ b/libavcodec/h264_slice.c
@@ -28,6 +28,7 @@
 #include "config_components.h"
 
 #include "libavutil/avassert.h"
+#include "libavutil/crc.h"
 #include "libavutil/mem.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/timecode.h"
@@ -402,6 +403,12 @@ int ff_h264_update_thread_context(AVCodecContext *dst,
     h->mb_aff_frame         = h1->mb_aff_frame;
     h->droppable            = h1->droppable;
 
+    memcpy(h->sps_depth, h1->sps_depth, sizeof(h->sps_depth));
+    h->nb_sps_depth         = h1->nb_sps_depth;
+    h->sps_depth_evicted    = h1->sps_depth_evicted;
+    h->sps_depth_max        = h1->sps_depth_max;
+    h->sps_crc              = h1->sps_crc;
+
     for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
         ret = ff_h264_replace_picture(&h->DPB[i], &h1->DPB[i]);
         if (ret < 0)
@@ -1071,6 +1078,25 @@ static enum AVPixelFormat non_j_pixfmt(enum 
AVPixelFormat a)
     }
 }
 
+static int *find_sps_depth(H264Context *h, uint32_t crc, int add)
+{
+    const unsigned size = FF_ARRAY_ELEMS(h->sps_depth);
+    unsigned i;
+
+    for (i = 0; i < FFMIN(h->nb_sps_depth, size); i++)
+        if (h->sps_depth[i].crc == crc)
+            return &h->sps_depth[i].has_b_frames;
+    if (!add)
+        return NULL;
+    i = h->nb_sps_depth++ % size;
+    if (h->nb_sps_depth > size)
+        h->sps_depth_evicted = FFMAX(h->sps_depth_evicted, 
h->sps_depth[i].has_b_frames);
+    if (h->nb_sps_depth == 2 * size)
+        h->nb_sps_depth = size;
+    h->sps_depth[i].crc = crc;
+    return &h->sps_depth[i].has_b_frames;
+}
+
 static int h264_init_ps(H264Context *h, const H264SliceContext *sl, int 
first_slice)
 {
     const SPS *sps;
@@ -1080,7 +1106,37 @@ static int h264_init_ps(H264Context *h, const 
H264SliceContext *sl, int first_sl
         av_refstruct_replace(&h->ps.pps, h->ps.pps_list[sl->pps_id]);
 
     if (h->ps.sps != h->ps.pps->sps) {
-        h->ps.sps = h->ps.pps->sps;
+        uint32_t crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE_LE), 0,
+                              h->ps.pps->sps->data, h->ps.pps->sps->data_size);
+        /* read before saving the outgoing SPS, which may evict this entry */
+        int *saved = find_sps_depth(h, crc, 0);
+        int depth  = h->ps.sps && h->sps_crc == crc ? h->avctx->has_b_frames :
+                     saved ? *saved : FFMAX(h->has_b_frames_from_caller,
+                                            h->sps_depth_evicted);
+
+        if (h->ps.sps) {
+            *find_sps_depth(h, h->sps_crc, 1) = h->avctx->has_b_frames;
+            h->sps_depth_max = FFMAX(h->sps_depth_max, h->avctx->has_b_frames);
+        }
+
+        h->ps.sps  = h->ps.pps->sps;
+        h->sps_crc = crc;
+
+        /* After a flush, a new SPS (another stream) starts from the caller's
+         * depth, as in a fresh decoder, or from the largest evicted one, as it
+         * may have been evicted. An SPS used before (a seek) gets back its
+         * depth, as relearning it drops pictures, but a learned one is not
+         * lowered, as it may come from a partial decode. Use the highest
+         * saved depth for known SPSs without a signalled limit: an intervening
+         * SPS may already have reset the current depth. Nor is the depth
+         * lowered while pictures may still be queued. */
+        if (saved && !h->ps.sps->bitstream_restriction_flag)
+            depth = FFMAX(depth, h->sps_depth_max);
+        if (!h->context_initialized && !h->delayed_pic[0] &&
+            (!saved || h->ps.sps->bitstream_restriction_flag))
+            h->avctx->has_b_frames = depth;
+        else
+            h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, depth);
 
         if (h->mb_width  != h->ps.sps->mb_width ||
             h->mb_height != h->ps.sps->mb_height ||
diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
index fea2e2a6167..8186d9cb7ca 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
@@ -291,8 +291,9 @@ static int h264_init_context(AVCodecContext *avctx, 
H264Context *h)
     h->avctx                 = avctx;
     h->cur_chroma_format_idc = -1;
 
-    h->width_from_caller     = avctx->width;
-    h->height_from_caller    = avctx->height;
+    h->width_from_caller        = avctx->width;
+    h->height_from_caller       = avctx->height;
+    h->has_b_frames_from_caller = avctx->has_b_frames;
 
     h->workaround_bugs       = avctx->workaround_bugs;
     h->flags                 = avctx->flags;
diff --git a/libavcodec/h264dec.h b/libavcodec/h264dec.h
index 7bce897021d..0ca897d1bcb 100644
--- a/libavcodec/h264dec.h
+++ b/libavcodec/h264dec.h
@@ -569,6 +569,18 @@ typedef struct H264Context {
      * cropping */
     int width_from_caller;
     int height_from_caller;
+    int has_b_frames_from_caller;
+
+    /* reorder depth last used with recently active SPSs, by SPS CRC,
+     * restored when one of them is activated again after a flush */
+    struct {
+        uint32_t crc;
+        int has_b_frames;
+    } sps_depth[MAX_SPS_COUNT];
+    unsigned nb_sps_depth;  ///< number of entries added, oldest replaced first
+    int sps_depth_evicted;  ///< largest depth of the replaced entries
+    int sps_depth_max;      ///< largest depth saved over the decoder lifetime
+    uint32_t sps_crc;       ///< CRC of ps.sps
 
     int enable_er;
     ERContext er;
diff --git a/tests/api/Makefile b/tests/api/Makefile
index c411e82e5f1..ff7f8da648d 100644
--- a/tests/api/Makefile
+++ b/tests/api/Makefile
@@ -2,6 +2,7 @@ APITESTPROGS-$(call ALLYES, AC3_ENCODER AC3_DECODER 
EAC3_DECODER AC3_FIXED_DECOD
 APITESTPROGS-$(call ALLYES, LIBASTCENC_ENCODER LIBASTCENC_DECODER) += 
api-astc-profile
 APITESTPROGS-$(call ENCDEC, FLAC, FLAC) += api-flac
 APITESTPROGS-$(call DEMDEC, H264, H264) += api-h264
+APITESTPROGS-$(call DEMDEC, H264, H264, H264_PARSER H264_METADATA_BSF) += 
api-h264-flush
 APITESTPROGS-$(call DEMDEC, H264, H264) += api-h264-slice
 APITESTPROGS-$(CONFIG_MOV_MUXER)        += api-movenc
 APITESTPROGS-$(CONFIG_MOVTEXT_ENCODER)  += api-movtext
diff --git a/tests/api/api-h264-flush-test.c b/tests/api/api-h264-flush-test.c
new file mode 100644
index 00000000000..0b8179be224
--- /dev/null
+++ b/tests/api/api-h264-flush-test.c
@@ -0,0 +1,223 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "libavcodec/avcodec.h"
+#include "libavcodec/bsf.h"
+#include "libavformat/avformat.h"
+#include "libavutil/adler32.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/mem.h"
+#include "libavutil/opt.h"
+
+typedef struct Output {
+    int packets;
+    int frames;
+    int first_packet;
+    AVAdler checksum;
+} Output;
+
+static void check(int ret)
+{
+    if (ret < 0) {
+        fprintf(stderr, "%s\n", av_err2str(ret));
+        exit(1);
+    }
+}
+
+static AVCodecContext *open_decoder(int threads)
+{
+    const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264);
+    AVCodecContext *ctx = avcodec_alloc_context3(codec);
+
+    if (!ctx)
+        exit(1);
+    ctx->thread_count = threads;
+    ctx->thread_type = FF_THREAD_FRAME;
+    check(avcodec_open2(ctx, codec, NULL));
+    return ctx;
+}
+
+static void receive(AVCodecContext *ctx, int packet, Output *out)
+{
+    AVFrame *frame = av_frame_alloc();
+    int ret;
+
+    if (!frame)
+        exit(1);
+    while ((ret = avcodec_receive_frame(ctx, frame)) >= 0) {
+        int size = av_image_get_buffer_size(frame->format, frame->width,
+                                           frame->height, 1);
+        uint8_t *data;
+
+        check(size);
+        data = av_malloc(size);
+        if (!data)
+            exit(1);
+        check(av_image_copy_to_buffer(data, size,
+                                     (const uint8_t *const *)frame->data,
+                                     frame->linesize, frame->format,
+                                     frame->width, frame->height, 1));
+        out->checksum = av_adler32_update(out->checksum, data, size);
+        av_free(data);
+        if (!out->frames++)
+            out->first_packet = packet;
+    }
+    if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
+        check(ret);
+    av_frame_free(&frame);
+}
+
+/* Send max_packets packets (all if 0), without draining.
+ * Changing only SAR makes distinct SPSs without changing the decoded pixels. 
*/
+static void feed(AVCodecContext *ctx, const char *path, int sar,
+                 int max_packets, Output *out)
+{
+    AVFormatContext *fmt = NULL;
+    AVBSFContext *bsf = NULL;
+    AVPacket *pkt = av_packet_alloc();
+    int packets = 0, ret = 0;
+
+    if (!pkt)
+        exit(1);
+    check(avformat_open_input(&fmt, path, av_find_input_format("h264"), NULL));
+    if (sar) {
+        char value[32];
+
+        check(av_bsf_alloc(av_bsf_get_by_name("h264_metadata"), &bsf));
+        check(avcodec_parameters_copy(bsf->par_in, fmt->streams[0]->codecpar));
+        bsf->time_base_in = fmt->streams[0]->time_base;
+        snprintf(value, sizeof(value), "1/%d", sar);
+        check(av_opt_set(bsf->priv_data, "sample_aspect_ratio", value, 0));
+        check(av_bsf_init(bsf));
+    }
+    while ((!max_packets || packets < max_packets) &&
+           (ret = av_read_frame(fmt, pkt)) >= 0) {
+        if (bsf) {
+            check(av_bsf_send_packet(bsf, pkt));
+            check(av_bsf_receive_packet(bsf, pkt));
+        }
+        check(avcodec_send_packet(ctx, pkt));
+        packets++;
+        receive(ctx, ++out->packets, out);
+        av_packet_unref(pkt);
+    }
+    if (ret != AVERROR_EOF || (max_packets && packets < max_packets))
+        check(ret);
+    av_packet_free(&pkt);
+    av_bsf_free(&bsf);
+    avformat_close_input(&fmt);
+}
+
+static void drain(AVCodecContext *ctx, Output *out, const char *label)
+{
+    check(avcodec_send_packet(ctx, NULL));
+    receive(ctx, out->packets + 1, out);
+    if (label)
+        printf("%s: first packet %d, %d frames, checksum %08" PRIx32 "\n",
+               label, out->first_packet, out->frames, out->checksum);
+}
+
+static void decode(AVCodecContext *ctx, const char *path, int sar, const char 
*label)
+{
+    Output out = { .checksum = 1 };
+
+    feed(ctx, path, sar, 0, &out);
+    drain(ctx, &out, label);
+}
+
+/* BA1 then the reference stream, without a flush in between */
+static void decode_concat(AVCodecContext *ctx, const char *ba1, int sar,
+                          const char *reference, const char *label)
+{
+    Output out = { .checksum = 1 };
+
+    feed(ctx, ba1, sar, 0, &out);
+    feed(ctx, reference, 0, 0, &out);
+    drain(ctx, &out, label);
+}
+
+int main(int argc, char **argv)
+{
+    const int histories[] = { 32, 33, 70 };
+    Output partial = { .checksum = 1 };
+    AVCodecContext *ctx;
+    int threads;
+
+    if (argc != 5)
+        return 1;
+    threads = atoi(argv[1]);
+    av_log_set_level(AV_LOG_ERROR);
+
+    ctx = open_decoder(threads);
+    decode(ctx, argv[2], 0, NULL);
+    avcodec_flush_buffers(ctx);
+    decode(ctx, argv[2], 0, "same-SPS replay");
+    avcodec_free_context(&ctx);
+
+    ctx = open_decoder(threads);
+    decode(ctx, argv[3], 0, "fresh decoder");
+    avcodec_free_context(&ctx);
+
+    ctx = open_decoder(threads);
+    decode(ctx, argv[4], 0, NULL);
+    avcodec_flush_buffers(ctx);
+    decode(ctx, argv[3], 0, "after stream switch");
+    avcodec_free_context(&ctx);
+
+    /* A partial decode must not lower the depth of an SPS used before. */
+    ctx = open_decoder(threads);
+    feed(ctx, argv[2], 0, 8, &partial);
+    avcodec_flush_buffers(ctx);
+    decode(ctx, argv[2], 7, NULL);
+    avcodec_flush_buffers(ctx);
+    decode(ctx, argv[3], 0, "intervening stream switch");
+    avcodec_flush_buffers(ctx);
+    decode(ctx, argv[2], 0, "replay after partial decode");
+    avcodec_free_context(&ctx);
+
+    /* An SPS used before gets back its depth at a later SPS change too. */
+    ctx = open_decoder(threads);
+    decode_concat(ctx, argv[3], 0, argv[2], NULL);
+    avcodec_flush_buffers(ctx);
+    decode_concat(ctx, argv[3], 5, argv[2], "new SPS then known SPS");
+    avcodec_free_context(&ctx);
+
+    for (int i = 0; i < FF_ARRAY_ELEMS(histories); i++) {
+        int variants = histories[i];
+
+        ctx = open_decoder(threads);
+        decode(ctx, argv[2], 0, NULL);
+        for (int sar = 1; sar <= variants; sar++) {
+            Output history = { .checksum = 1 };
+
+            avcodec_flush_buffers(ctx);
+            feed(ctx, argv[3], sar, 1, &history);
+        }
+        printf("%d SPS variants:\n", variants);
+        avcodec_flush_buffers(ctx);
+        decode(ctx, argv[3], 99, "  new SPS");
+        avcodec_flush_buffers(ctx);
+        decode(ctx, argv[2], 0, "  replay");
+        avcodec_free_context(&ctx);
+    }
+    return 0;
+}
diff --git a/tests/fate/api.mak b/tests/fate/api.mak
index 1659c7979c0..1278576e645 100644
--- a/tests/fate/api.mak
+++ b/tests/fate/api.mak
@@ -39,6 +39,14 @@ FATE_API_SAMPLES_LIBAVFORMAT-$(call DEMDEC, H264, H264, 
H264_PARSER) += fate-api
 fate-api-h264: $(APITESTSDIR)/api-h264-test$(EXESUF)
 fate-api-h264: CMD = run $(APITESTSDIR)/api-h264-test$(EXESUF) 
$(TARGET_SAMPLES)/h264-conformance/SVA_NL2_E.264
 
+FATE_API_SAMPLES_LIBAVFORMAT-$(call DEMDEC, H264, H264, H264_PARSER 
H264_METADATA_BSF) += fate-api-h264-flush-1
+ifeq ($(HAVE_THREADS),yes)
+FATE_API_SAMPLES_LIBAVFORMAT-$(call DEMDEC, H264, H264, H264_PARSER 
H264_METADATA_BSF) += fate-api-h264-flush-4
+endif
+fate-api-h264-flush-1 fate-api-h264-flush-4: 
$(APITESTSDIR)/api-h264-flush-test$(EXESUF)
+fate-api-h264-flush-1: CMD = run $(APITESTSDIR)/api-h264-flush-test$(EXESUF) 1 
$(TARGET_SAMPLES)/h264/reference-b-reorder.h264 
$(TARGET_SAMPLES)/h264-conformance/BA1_Sony_D.jsv 
$(TARGET_SAMPLES)/h264-conformance/CABA3_SVA_B.264
+fate-api-h264-flush-4: CMD = run $(APITESTSDIR)/api-h264-flush-test$(EXESUF) 4 
$(TARGET_SAMPLES)/h264/reference-b-reorder.h264 
$(TARGET_SAMPLES)/h264-conformance/BA1_Sony_D.jsv 
$(TARGET_SAMPLES)/h264-conformance/CABA3_SVA_B.264
+
 FATE_API_SAMPLES_LIBAVFORMAT-$(call DEMDEC, H264, H264) += fate-api-h264-slice
 fate-api-h264-slice: $(APITESTSDIR)/api-h264-slice-test$(EXESUF)
 fate-api-h264-slice: CMD = run $(APITESTSDIR)/api-h264-slice-test$(EXESUF) 2 
$(TARGET_SAMPLES)/h264/crew_cif.nal
diff --git a/tests/ref/fate/api-h264-flush-1 b/tests/ref/fate/api-h264-flush-1
new file mode 100644
index 00000000000..0d76c1bdeb4
--- /dev/null
+++ b/tests/ref/fate/api-h264-flush-1
@@ -0,0 +1,15 @@
+same-SPS replay: first packet 3, 21 frames, checksum b2004ca5
+fresh decoder: first packet 1, 17 frames, checksum 54156989
+after stream switch: first packet 1, 17 frames, checksum 54156989
+intervening stream switch: first packet 1, 17 frames, checksum 54156989
+replay after partial decode: first packet 3, 21 frames, checksum b2004ca5
+new SPS then known SPS: first packet 1, 38 frames, checksum cda5b62d
+32 SPS variants:
+  new SPS: first packet 1, 17 frames, checksum 54156989
+  replay: first packet 3, 21 frames, checksum b2004ca5
+33 SPS variants:
+  new SPS: first packet 3, 17 frames, checksum 54156989
+  replay: first packet 3, 21 frames, checksum b2004ca5
+70 SPS variants:
+  new SPS: first packet 3, 17 frames, checksum 54156989
+  replay: first packet 3, 21 frames, checksum b2004ca5
diff --git a/tests/ref/fate/api-h264-flush-4 b/tests/ref/fate/api-h264-flush-4
new file mode 100644
index 00000000000..e20ed470629
--- /dev/null
+++ b/tests/ref/fate/api-h264-flush-4
@@ -0,0 +1,15 @@
+same-SPS replay: first packet 6, 21 frames, checksum b2004ca5
+fresh decoder: first packet 4, 17 frames, checksum 54156989
+after stream switch: first packet 4, 17 frames, checksum 54156989
+intervening stream switch: first packet 4, 17 frames, checksum 54156989
+replay after partial decode: first packet 6, 21 frames, checksum b2004ca5
+new SPS then known SPS: first packet 4, 38 frames, checksum cda5b62d
+32 SPS variants:
+  new SPS: first packet 4, 17 frames, checksum 54156989
+  replay: first packet 6, 21 frames, checksum b2004ca5
+33 SPS variants:
+  new SPS: first packet 6, 17 frames, checksum 54156989
+  replay: first packet 6, 21 frames, checksum b2004ca5
+70 SPS variants:
+  new SPS: first packet 6, 17 frames, checksum 54156989
+  replay: first packet 6, 21 frames, checksum b2004ca5
-- 
2.54.1


From 97cc9fadf7035b3a7f5b969b17cab85dcfbd2487 Mon Sep 17 00:00:00 2001
From: Baptiste Coudurier <[email protected]>
Date: Sun, 22 Nov 2026 22:34:26 -0800
Subject: [PATCH 2/2] avcodec/h264_slice: start an SPS with a reorder limit
 from that limit

No depth is learned with a signalled limit, so a saved one came from a
previous stream and delayed output when the SPS returned after a flush.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavcodec/h264_slice.c         |  5 +++++
 tests/api/api-h264-flush-test.c | 22 ++++++++++++++++------
 tests/fate/api.mak              |  4 ++--
 tests/ref/fate/api-h264-flush-1 |  1 +
 tests/ref/fate/api-h264-flush-4 |  1 +
 5 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
index 8a6dfd97662..76edb01571d 100644
--- a/libavcodec/h264_slice.c
+++ b/libavcodec/h264_slice.c
@@ -1132,6 +1132,11 @@ static int h264_init_ps(H264Context *h, const 
H264SliceContext *sl, int first_sl
          * lowered while pictures may still be queued. */
         if (saved && !h->ps.sps->bitstream_restriction_flag)
             depth = FFMAX(depth, h->sps_depth_max);
+        /* No depth is learned with a signalled limit, a saved one is from
+         * another SPS: start from the limit, as in a fresh decoder. */
+        if (h->ps.sps->bitstream_restriction_flag)
+            depth = FFMAX(h->has_b_frames_from_caller,
+                          h->ps.sps->num_reorder_frames);
         if (!h->context_initialized && !h->delayed_pic[0] &&
             (!saved || h->ps.sps->bitstream_restriction_flag))
             h->avctx->has_b_frames = depth;
diff --git a/tests/api/api-h264-flush-test.c b/tests/api/api-h264-flush-test.c
index 0b8179be224..d6b13843bfe 100644
--- a/tests/api/api-h264-flush-test.c
+++ b/tests/api/api-h264-flush-test.c
@@ -144,14 +144,14 @@ static void decode(AVCodecContext *ctx, const char *path, 
int sar, const char *l
     drain(ctx, &out, label);
 }
 
-/* BA1 then the reference stream, without a flush in between */
-static void decode_concat(AVCodecContext *ctx, const char *ba1, int sar,
-                          const char *reference, const char *label)
+/* Two streams without a flush in between */
+static void decode_concat(AVCodecContext *ctx, const char *first, int sar,
+                          const char *second, const char *label)
 {
     Output out = { .checksum = 1 };
 
-    feed(ctx, ba1, sar, 0, &out);
-    feed(ctx, reference, 0, 0, &out);
+    feed(ctx, first, sar, 0, &out);
+    feed(ctx, second, 0, 0, &out);
     drain(ctx, &out, label);
 }
 
@@ -162,7 +162,7 @@ int main(int argc, char **argv)
     AVCodecContext *ctx;
     int threads;
 
-    if (argc != 5)
+    if (argc != 6)
         return 1;
     threads = atoi(argv[1]);
     av_log_set_level(AV_LOG_ERROR);
@@ -201,6 +201,16 @@ int main(int argc, char **argv)
     decode_concat(ctx, argv[3], 5, argv[2], "new SPS then known SPS");
     avcodec_free_context(&ctx);
 
+    /* An SPS with a signalled limit does not keep a depth inherited
+     * from a previous stream. */
+    ctx = open_decoder(threads);
+    decode_concat(ctx, argv[2], 0, argv[5], NULL);
+    avcodec_flush_buffers(ctx);
+    decode(ctx, argv[3], 0, NULL);
+    avcodec_flush_buffers(ctx);
+    decode(ctx, argv[5], 0, "known SPS with reorder limit");
+    avcodec_free_context(&ctx);
+
     for (int i = 0; i < FF_ARRAY_ELEMS(histories); i++) {
         int variants = histories[i];
 
diff --git a/tests/fate/api.mak b/tests/fate/api.mak
index 1278576e645..c3bb2c63571 100644
--- a/tests/fate/api.mak
+++ b/tests/fate/api.mak
@@ -44,8 +44,8 @@ ifeq ($(HAVE_THREADS),yes)
 FATE_API_SAMPLES_LIBAVFORMAT-$(call DEMDEC, H264, H264, H264_PARSER 
H264_METADATA_BSF) += fate-api-h264-flush-4
 endif
 fate-api-h264-flush-1 fate-api-h264-flush-4: 
$(APITESTSDIR)/api-h264-flush-test$(EXESUF)
-fate-api-h264-flush-1: CMD = run $(APITESTSDIR)/api-h264-flush-test$(EXESUF) 1 
$(TARGET_SAMPLES)/h264/reference-b-reorder.h264 
$(TARGET_SAMPLES)/h264-conformance/BA1_Sony_D.jsv 
$(TARGET_SAMPLES)/h264-conformance/CABA3_SVA_B.264
-fate-api-h264-flush-4: CMD = run $(APITESTSDIR)/api-h264-flush-test$(EXESUF) 4 
$(TARGET_SAMPLES)/h264/reference-b-reorder.h264 
$(TARGET_SAMPLES)/h264-conformance/BA1_Sony_D.jsv 
$(TARGET_SAMPLES)/h264-conformance/CABA3_SVA_B.264
+fate-api-h264-flush-1: CMD = run $(APITESTSDIR)/api-h264-flush-test$(EXESUF) 1 
$(TARGET_SAMPLES)/h264/reference-b-reorder.h264 
$(TARGET_SAMPLES)/h264-conformance/BA1_Sony_D.jsv 
$(TARGET_SAMPLES)/h264-conformance/CABA3_SVA_B.264 
$(TARGET_SAMPLES)/h264/crew_cif.nal
+fate-api-h264-flush-4: CMD = run $(APITESTSDIR)/api-h264-flush-test$(EXESUF) 4 
$(TARGET_SAMPLES)/h264/reference-b-reorder.h264 
$(TARGET_SAMPLES)/h264-conformance/BA1_Sony_D.jsv 
$(TARGET_SAMPLES)/h264-conformance/CABA3_SVA_B.264 
$(TARGET_SAMPLES)/h264/crew_cif.nal
 
 FATE_API_SAMPLES_LIBAVFORMAT-$(call DEMDEC, H264, H264) += fate-api-h264-slice
 fate-api-h264-slice: $(APITESTSDIR)/api-h264-slice-test$(EXESUF)
diff --git a/tests/ref/fate/api-h264-flush-1 b/tests/ref/fate/api-h264-flush-1
index 0d76c1bdeb4..7bf0b06bf66 100644
--- a/tests/ref/fate/api-h264-flush-1
+++ b/tests/ref/fate/api-h264-flush-1
@@ -4,6 +4,7 @@ after stream switch: first packet 1, 17 frames, checksum 
54156989
 intervening stream switch: first packet 1, 17 frames, checksum 54156989
 replay after partial decode: first packet 3, 21 frames, checksum b2004ca5
 new SPS then known SPS: first packet 1, 38 frames, checksum cda5b62d
+known SPS with reorder limit: first packet 1, 300 frames, checksum 2a5f83a6
 32 SPS variants:
   new SPS: first packet 1, 17 frames, checksum 54156989
   replay: first packet 3, 21 frames, checksum b2004ca5
diff --git a/tests/ref/fate/api-h264-flush-4 b/tests/ref/fate/api-h264-flush-4
index e20ed470629..0ffb2da8ef8 100644
--- a/tests/ref/fate/api-h264-flush-4
+++ b/tests/ref/fate/api-h264-flush-4
@@ -4,6 +4,7 @@ after stream switch: first packet 4, 17 frames, checksum 
54156989
 intervening stream switch: first packet 4, 17 frames, checksum 54156989
 replay after partial decode: first packet 6, 21 frames, checksum b2004ca5
 new SPS then known SPS: first packet 4, 38 frames, checksum cda5b62d
+known SPS with reorder limit: first packet 4, 300 frames, checksum 2a5f83a6
 32 SPS variants:
   new SPS: first packet 4, 17 frames, checksum 54156989
   replay: first packet 6, 21 frames, checksum b2004ca5
-- 
2.54.1

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

Reply via email to