From 2b38ab06b0fc8a778b10842eda2b37cd173d8639 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Tue, 18 Mar 2025 16:29:07 +0100
Subject: [PATCH 58/77] avcodec/mpegvideo_enc: Call ff_mpv_common_init() later

Namely after the main slice context has already been initialized,
so that this initialized state is directly copied to the newly
created slice contexts without relying on it being copied
by ff_update_duplicate_context(). This is in preparation for further
commits.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegvideo_enc.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 9f44c37b3d..f9b8d96c83 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -1011,9 +1011,6 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
 
     /* init */
     ff_mpv_idct_init(s);
-    if ((ret = ff_mpv_common_init(s)) < 0)
-        return ret;
-
     ff_fdctdsp_init(&s->fdsp, avctx);
     ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
     ff_pixblockdsp_init(&s->pdsp, avctx);
@@ -1030,10 +1027,6 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
     if (ret < 0)
         return ret;
 
-    ret = init_buffers(m, avctx);
-    if (ret < 0)
-        return ret;
-
     ff_dct_encode_init(s);
 
     if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
@@ -1047,13 +1040,6 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
         s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
     }
 
-    if (s->slice_context_count > 1) {
-        s->rtp_mode = 1;
-
-        if (avctx->codec_id == AV_CODEC_ID_H263P)
-            s->h263_slice_structured = 1;
-    }
-
     if (CONFIG_H263_ENCODER && s->out_format == FMT_H263) {
         ff_h263_encode_init(m);
 #if CONFIG_MSMPEG4ENC
@@ -1062,6 +1048,23 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
 #endif
     }
 
+    ret = ff_mpv_common_init(s);
+    if (ret < 0)
+        return ret;
+
+    if (s->slice_context_count > 1) {
+        for (int i = 0; i < s->slice_context_count; ++i) {
+            s->thread_context[i]->rtp_mode = 1;
+
+            if (avctx->codec_id == AV_CODEC_ID_H263P)
+                s->thread_context[i]->h263_slice_structured = 1;
+        }
+    }
+
+    ret = init_buffers(m, avctx);
+    if (ret < 0)
+        return ret;
+
     ret = ff_rate_control_init(m);
     if (ret < 0)
         return ret;
-- 
2.45.2