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

The default get_buffer2() takes buffers from a pool owned by the codec
context. Newly allocated pool buffers are zeroed, but recycled ones are
handed out as they are, and avcodec_flush_buffers() does not touch the
pool. Decoders such as hevc, mpeg2video, mpeg4, msrle, qtrle, vp8 and
h264 leave the parts of a damaged frame that they cannot decode
unwritten, so after a flush and a switch to another stream those parts
show pixels of the previous stream, where a newly opened decoder outputs
zeroes.

Unref the pool on flush. Frames still held by the caller keep their
buffers; the next get_buffer2() call creates a new pool.

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



>From 9f7e22adc3bcc4a190db0932af1de247d0588e4a Mon Sep 17 00:00:00 2001
From: Pu Cheng <[email protected]>
Date: Fri, 20 Nov 2026 14:08:21 +0800
Subject: [PATCH] avcodec/decode: drop the frame buffer pool in
 ff_decode_flush_buffers()

The default get_buffer2() takes buffers from a pool owned by the codec
context. Newly allocated pool buffers are zeroed, but recycled ones are
handed out as they are, and avcodec_flush_buffers() does not touch the
pool. Decoders such as hevc, mpeg2video, mpeg4, msrle, qtrle, vp8 and
h264 leave the parts of a damaged frame that they cannot decode
unwritten, so after a flush and a switch to another stream those parts
show pixels of the previous stream, where a newly opened decoder outputs
zeroes.

Unref the pool on flush. Frames still held by the caller keep their
buffers; the next get_buffer2() call creates a new pool.

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

diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 2696f8f916b..c3bd598cadd 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -2416,6 +2416,10 @@ av_cold void ff_decode_flush_buffers(AVCodecContext 
*avctx)
     av_packet_unref(avci->last_pkt_props);
     av_packet_unref(avci->in_pkt);
 
+    /* Drop the frame pool, so that buffers recycled from the previous stream
+     * are not handed out again after the flush. */
+    av_refstruct_unref(&avci->pool);
+
     dc->pts_correction_num_faulty_pts =
     dc->pts_correction_num_faulty_dts = 0;
     dc->pts_correction_last_pts =
-- 
2.54.1

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

Reply via email to