From dd3a973dd2480a77f04673ff95ff4e8c2a7526c5 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Sun, 16 Mar 2025 14:52:36 +0100
Subject: [PATCH 54/77] avcodec/mpegvideo: Support custom slice context sizes

This is in preparation for adding a special slice context for
the encoders and moving all the encoder-specific fields to it.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegvideo.c | 3 ++-
 libavcodec/mpegvideo.h | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c
index 8055e6c0e2..126fefa1be 100644
--- a/libavcodec/mpegvideo.c
+++ b/libavcodec/mpegvideo.c
@@ -375,6 +375,7 @@ static av_cold int init_duplicate_context(MpegEncContext *s)
 av_cold int ff_mpv_init_duplicate_contexts(MpegEncContext *s)
 {
     int nb_slices = s->slice_context_count, ret;
+    size_t slice_size = s->slice_ctx_size ? s->slice_ctx_size : sizeof(*s);
 
     s->parent = s;
 
@@ -382,7 +383,7 @@ av_cold int ff_mpv_init_duplicate_contexts(MpegEncContext *s)
      * fields allocated in init_duplicate_context are NULL after
      * copying. This prevents double-frees upon allocation error. */
     for (int i = 1; i < nb_slices; i++) {
-        s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
+        s->thread_context[i] = av_memdup(s, slice_size);
         if (!s->thread_context[i])
             return AVERROR(ENOMEM);
         if ((ret = init_duplicate_context(s->thread_context[i])) < 0)
diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index 69efc81096..02894ce68f 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -469,6 +469,9 @@ typedef struct MpegEncContext {
      * a frame size change */
     int context_reinit;
 
+    /// If set, ff_mpv_common_init() will allocate slice contexts of this size
+    unsigned slice_ctx_size;
+
     ERContext er;
 
     int error_rate;
-- 
2.45.2