PR #26213 opened by Pu Cheng (GeekQAQ)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26213
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26213.patch

The decoder keeps the previous picture in its own buffer and decides
whether a frame is the first one of a stream (palette set, intra,
keyframe) by avctx->frame_num, which is not reset by
avcodec_flush_buffers(). After a flush and a switch to another stream
(or a seek back to the start), the first frame is thus marked as a
non-keyframe P frame, and pixels the new stream does not overwrite keep
the previous stream's picture, where a newly opened decoder has zeroes.

Add a flush callback that clears the buffer and makes the next frame
be treated as the first frame of a stream.

Signed-off-by: Pu Cheng <[email protected]>



>From 535e26dba2bc7282c07ed5f2999a76d64c3a170f Mon Sep 17 00:00:00 2001
From: Pu Cheng <[email protected]>
Date: Fri, 20 Nov 2026 14:08:21 +0800
Subject: [PATCH] avcodec/bfi: add a flush callback

The decoder keeps the previous picture in its own buffer and decides
whether a frame is the first one of a stream (palette set, intra,
keyframe) by avctx->frame_num, which is not reset by
avcodec_flush_buffers(). After a flush and a switch to another stream
(or a seek back to the start), the first frame is thus marked as a
non-keyframe P frame, and pixels the new stream does not overwrite keep
the previous stream's picture, where a newly opened decoder has zeroes.

Add a flush callback that clears the buffer and makes the next frame
be treated as the first frame of a stream.

Signed-off-by: Pu Cheng <[email protected]>
---
 libavcodec/bfi.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/libavcodec/bfi.c b/libavcodec/bfi.c
index 1d2d3a72045..7977be9183f 100644
--- a/libavcodec/bfi.c
+++ b/libavcodec/bfi.c
@@ -35,6 +35,7 @@
 typedef struct BFIContext {
     uint8_t *dst;
     uint32_t pal[256];
+    int next_is_first;  ///< the next frame starts a stream: set the palette, 
mark it as a keyframe
 } BFIContext;
 
 static av_cold int bfi_decode_init(AVCodecContext *avctx)
@@ -44,9 +45,17 @@ static av_cold int bfi_decode_init(AVCodecContext *avctx)
     bfi->dst        = av_mallocz(avctx->width * avctx->height);
     if (!bfi->dst)
         return AVERROR(ENOMEM);
+    bfi->next_is_first = 1;
     return 0;
 }
 
+static av_cold void bfi_decode_flush(AVCodecContext *avctx)
+{
+    BFIContext *bfi = avctx->priv_data;
+    memset(bfi->dst, 0, avctx->width * avctx->height);
+    bfi->next_is_first = 1;
+}
+
 static int bfi_decode_frame(AVCodecContext *avctx, AVFrame *frame,
                             int *got_frame, AVPacket *avpkt)
 {
@@ -65,7 +74,7 @@ static int bfi_decode_frame(AVCodecContext *avctx, AVFrame 
*frame,
     bytestream2_init(&g, avpkt->data, buf_size);
 
     /* Set frame parameters and palette, if necessary */
-    if (!avctx->frame_num) {
+    if (bfi->next_is_first) {
         frame->pict_type = AV_PICTURE_TYPE_I;
         frame->flags |= AV_FRAME_FLAG_KEY;
         /* Setting the palette */
@@ -160,6 +169,7 @@ static int bfi_decode_frame(AVCodecContext *avctx, AVFrame 
*frame,
         src += avctx->width;
         dst += frame->linesize[0];
     }
+    bfi->next_is_first = 0;
     *got_frame = 1;
 
     return buf_size;
@@ -180,6 +190,7 @@ const FFCodec ff_bfi_decoder = {
     .priv_data_size = sizeof(BFIContext),
     .init           = bfi_decode_init,
     .close          = bfi_decode_close,
+    .flush          = bfi_decode_flush,
     FF_CODEC_DECODE_CB(bfi_decode_frame),
     .p.capabilities = AV_CODEC_CAP_DR1,
 };
-- 
2.54.1

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

Reply via email to