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

With lowres, chroma motion compensation near the picture edge reads
bytes of the edge emulation buffer that the current call did not write,
so the result depends on what the buffer held before. ff_mpeg_flush()
keeps the buffer, so after avcodec_flush_buffers() and a switch to
another stream, chroma of edge macroblocks with motion vectors pointing
outside the picture picks up data the previous stream left in it, where
a newly opened decoder reads freshly allocated memory.

Free the scratch buffers of all slice contexts on flush, so the next
frame allocates them anew. The read of unwritten bytes itself is not
changed by this.

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



>From 50be79ab13f4a0f65aa0dee20b15376d8ecdc457 Mon Sep 17 00:00:00 2001
From: Pu Cheng <[email protected]>
Date: Fri, 20 Nov 2026 14:34:00 +0800
Subject: [PATCH] avcodec/mpegvideo_dec: free the scratch buffers in
 ff_mpeg_flush()

With lowres, chroma motion compensation near the picture edge reads
bytes of the edge emulation buffer that the current call did not write,
so the result depends on what the buffer held before. ff_mpeg_flush()
keeps the buffer, so after avcodec_flush_buffers() and a switch to
another stream, chroma of edge macroblocks with motion vectors pointing
outside the picture picks up data the previous stream left in it, where
a newly opened decoder reads freshly allocated memory.

Free the scratch buffers of all slice contexts on flush, so the next
frame allocates them anew. The read of unwritten bytes itself is not
changed by this.

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

diff --git a/libavcodec/mpegvideo_dec.c b/libavcodec/mpegvideo_dec.c
index 6018e532df6..57d8a3d32d7 100644
--- a/libavcodec/mpegvideo_dec.c
+++ b/libavcodec/mpegvideo_dec.c
@@ -28,6 +28,7 @@
 #include "libavutil/emms.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/internal.h"
+#include "libavutil/mem.h"
 #include "libavutil/video_enc_params.h"
 
 #include "avcodec.h"
@@ -466,6 +467,18 @@ av_cold void ff_mpeg_flush(AVCodecContext *avctx)
     s->mb_x = s->mb_y = 0;
 
     s->pp_time = 0;
+
+    /* Free the scratch buffers (edge emulation etc.), so that the next
+     * frame reallocates them as a newly opened decoder does. */
+    for (int i = 0; i < s->slice_context_count; i++) {
+        MpegEncContext *const t = s->thread_context[i];
+        if (!t)
+            continue;
+        av_freep(&t->sc.edge_emu_buffer);
+        av_freep(&t->sc.scratchpad_buf);
+        t->sc.obmc_scratchpad = NULL;
+        t->sc.linesize        = 0;
+    }
 }
 
 static inline int hpel_motion_lowres(MpegEncContext *s,
-- 
2.54.1

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

Reply via email to