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

After `avcodec_flush_buffers()`, the h264 decoder kept the reorder depth 
(`has_b_frames`) learned from the previous stream, so a following stream 
without reordering was output one frame late.

When a different SPS is activated after a flush, `has_b_frames` now goes back 
to the value the caller opened the decoder with (e.g. `video_delay`). Seeking 
within the same stream activates the same SPS, so the learned depth is kept and 
the picture loss from #24613 does not come back.

Tested with the program from the issue and the FATE samples, single-threaded 
and with 4 frame threads:
- CABA3_SVA_B.264, flush, BA1_Sony_D.jsv: first picture after the same packet 
as a fresh decoder (master: one packet later)
- reference-b-reorder.h264, flush, same file: 21 pictures, as on master
- decoder opened with has_b_frames=2, CABA3_SVA_B.264, flush, 
reference-b-reorder.h264: 21 pictures, as a fresh decoder
- fate-h264, fate-api, fate-seek pass

Fixes #25321

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



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

The reorder depth learned from the previous stream delayed output by a frame.

Fixes: #25321

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Baptiste Coudurier <[email protected]>
---
 libavcodec/h264_slice.c | 3 +++
 libavcodec/h264dec.c    | 5 +++--
 libavcodec/h264dec.h    | 1 +
 3 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
index b5953007bac..036dd2a2509 100644
--- a/libavcodec/h264_slice.c
+++ b/libavcodec/h264_slice.c
@@ -1080,6 +1080,9 @@ 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) {
+        /* Discard the previous SPS's reorder depth when switching after a 
flush. */
+        if (!h->context_initialized)
+            h->avctx->has_b_frames = h->has_b_frames_from_caller;
         h->ps.sps = h->ps.pps->sps;
 
         if (h->mb_width  != h->ps.sps->mb_width ||
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..540a8a66471 100644
--- a/libavcodec/h264dec.h
+++ b/libavcodec/h264dec.h
@@ -569,6 +569,7 @@ typedef struct H264Context {
      * cropping */
     int width_from_caller;
     int height_from_caller;
+    int has_b_frames_from_caller;
 
     int enable_er;
     ERContext er;
-- 
2.54.0

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

Reply via email to