From 05f52331882ac51b589bde5bb710d51219453a5f Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Fri, 28 Feb 2025 18:56:15 +0100
Subject: [PATCH 02/77] avcodec/mpegvideoenc: Add MPVMainEncContext

This is in preparation for moving all the elements from
MpegEncContext that are only used by the main encoder thread
to MPVMainEncContext.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/flvenc.c        | 2 +-
 libavcodec/h261enc.c       | 4 ++--
 libavcodec/ituh263enc.c    | 4 ++--
 libavcodec/mjpegenc.c      | 2 +-
 libavcodec/mpeg12enc.c     | 4 ++--
 libavcodec/mpeg4videoenc.c | 2 +-
 libavcodec/mpegvideoenc.h  | 4 ++++
 libavcodec/msmpeg4enc.c    | 2 +-
 libavcodec/msmpeg4enc.h    | 4 ++--
 libavcodec/rv10enc.c       | 2 +-
 libavcodec/rv20enc.c       | 2 +-
 libavcodec/speedhqenc.c    | 2 +-
 libavcodec/wmv2enc.c       | 4 ++--
 13 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/libavcodec/flvenc.c b/libavcodec/flvenc.c
index b6f9d1c6db..0cd20f026d 100644
--- a/libavcodec/flvenc.c
+++ b/libavcodec/flvenc.c
@@ -89,7 +89,7 @@ const FFCodec ff_flv_encoder = {
     .p.id           = AV_CODEC_ID_FLV1,
     .p.priv_class   = &ff_mpv_enc_class,
     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
-    .priv_data_size = sizeof(MpegEncContext),
+    .priv_data_size = sizeof(MPVMainEncContext),
     .init           = ff_mpv_encode_init,
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = ff_mpv_encode_end,
diff --git a/libavcodec/h261enc.c b/libavcodec/h261enc.c
index 36436ee60f..264efb0aa3 100644
--- a/libavcodec/h261enc.c
+++ b/libavcodec/h261enc.c
@@ -55,7 +55,7 @@ static uint8_t uni_h261_rl_len_last[64 * 128];
 static uint8_t h261_mv_codes[64][2];
 
 typedef struct H261EncContext {
-    MpegEncContext s;
+    MPVMainEncContext s;
 
     H261Context common;
 
@@ -167,7 +167,7 @@ static inline int get_cbp(MpegEncContext *s, int16_t block[6][64])
  */
 static void h261_encode_block(H261EncContext *h, int16_t *block, int n)
 {
-    MpegEncContext *const s = &h->s;
+    MpegEncContext *const s = &h->s.s;
     int level, run, i, j, last_index, last_non_zero;
 
     if (s->mb_intra) {
diff --git a/libavcodec/ituh263enc.c b/libavcodec/ituh263enc.c
index a81bfb7c79..825b398455 100644
--- a/libavcodec/ituh263enc.c
+++ b/libavcodec/ituh263enc.c
@@ -908,7 +908,7 @@ const FFCodec ff_h263_encoder = {
     .p.priv_class   = &h263_class,
     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
-    .priv_data_size = sizeof(MpegEncContext),
+    .priv_data_size = sizeof(MPVMainEncContext),
     .init           = ff_mpv_encode_init,
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = ff_mpv_encode_end,
@@ -941,7 +941,7 @@ const FFCodec ff_h263p_encoder = {
     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS |
                       AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
-    .priv_data_size = sizeof(MpegEncContext),
+    .priv_data_size = sizeof(MPVMainEncContext),
     .init           = ff_mpv_encode_init,
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = ff_mpv_encode_end,
diff --git a/libavcodec/mjpegenc.c b/libavcodec/mjpegenc.c
index 30f2c9900d..0a2d6eebd3 100644
--- a/libavcodec/mjpegenc.c
+++ b/libavcodec/mjpegenc.c
@@ -64,7 +64,7 @@ typedef struct MJpegHuffmanCode {
  * MpegEncContext is followed by a MjpegContext; the other threads
  * can access this shared context via MpegEncContext.mjpeg. */
 typedef struct MJPEGEncContext {
-    MpegEncContext mpeg;
+    MPVMainEncContext mpeg;
     MJpegContext   mjpeg;
 } MJPEGEncContext;
 
diff --git a/libavcodec/mpeg12enc.c b/libavcodec/mpeg12enc.c
index 1563cefd23..bb61b4f4fe 100644
--- a/libavcodec/mpeg12enc.c
+++ b/libavcodec/mpeg12enc.c
@@ -72,7 +72,7 @@ static uint32_t mpeg1_lum_dc_uni[512];
 static uint32_t mpeg1_chr_dc_uni[512];
 
 typedef struct MPEG12EncContext {
-    MpegEncContext mpeg;
+    MPVMainEncContext mpeg;
     AVRational frame_rate_ext;
     unsigned frame_rate_index;
 
@@ -201,7 +201,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
         }
     }
 
-    if (mpeg12->mpeg.q_scale_type == 1) {
+    if (mpeg12->mpeg.s.q_scale_type == 1) {
         if (avctx->qmax > 28) {
             av_log(avctx, AV_LOG_ERROR,
                    "non linear quant only supports qmax <= 28 currently\n");
diff --git a/libavcodec/mpeg4videoenc.c b/libavcodec/mpeg4videoenc.c
index 3b312915b4..11872f29eb 100644
--- a/libavcodec/mpeg4videoenc.c
+++ b/libavcodec/mpeg4videoenc.c
@@ -1395,7 +1395,7 @@ const FFCodec ff_mpeg4_encoder = {
     CODEC_LONG_NAME("MPEG-4 part 2"),
     .p.type         = AVMEDIA_TYPE_VIDEO,
     .p.id           = AV_CODEC_ID_MPEG4,
-    .priv_data_size = sizeof(MpegEncContext),
+    .priv_data_size = sizeof(MPVMainEncContext),
     .init           = encode_init,
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = ff_mpv_encode_end,
diff --git a/libavcodec/mpegvideoenc.h b/libavcodec/mpegvideoenc.h
index 6dd382a3c6..f7aad17650 100644
--- a/libavcodec/mpegvideoenc.h
+++ b/libavcodec/mpegvideoenc.h
@@ -33,6 +33,10 @@
 #include "libavutil/opt.h"
 #include "mpegvideo.h"
 
+typedef struct MPVMainEncContext {
+    MpegEncContext s;  ///< The main slicecontext
+} MPVMainEncContext;
+
 #define MAX_FCODE        7
 #define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))
 #define INPLACE_OFFSET 16
diff --git a/libavcodec/msmpeg4enc.c b/libavcodec/msmpeg4enc.c
index 8310e0a578..98b72e4d58 100644
--- a/libavcodec/msmpeg4enc.c
+++ b/libavcodec/msmpeg4enc.c
@@ -151,7 +151,7 @@ av_cold void ff_msmpeg4_encode_init(MpegEncContext *s)
 
 static void find_best_tables(MSMPEG4EncContext *ms)
 {
-    MpegEncContext *const s = &ms->s;
+    MpegEncContext *const s = &ms->m.s;
     int i;
     int best        = 0, best_size        = INT_MAX;
     int chroma_best = 0, best_chroma_size = INT_MAX;
diff --git a/libavcodec/msmpeg4enc.h b/libavcodec/msmpeg4enc.h
index da9a45b589..72992176de 100644
--- a/libavcodec/msmpeg4enc.h
+++ b/libavcodec/msmpeg4enc.h
@@ -22,12 +22,12 @@
 #ifndef AVCODEC_MSMPEG4ENC_H
 #define AVCODEC_MSMPEG4ENC_H
 
-#include "mpegvideo.h"
+#include "mpegvideoenc.h"
 #include "put_bits.h"
 #include "rl.h"
 
 typedef struct MSMPEG4EncContext {
-    MpegEncContext s;
+    MPVMainEncContext m;
 
     /** [mb_intra][isChroma][level][run][last] */
     unsigned ac_stats[2][2][MAX_LEVEL + 1][MAX_RUN + 1][2];
diff --git a/libavcodec/rv10enc.c b/libavcodec/rv10enc.c
index 53463dd068..d55fa7c2b0 100644
--- a/libavcodec/rv10enc.c
+++ b/libavcodec/rv10enc.c
@@ -72,7 +72,7 @@ const FFCodec ff_rv10_encoder = {
     .p.id           = AV_CODEC_ID_RV10,
     .p.priv_class   = &ff_mpv_enc_class,
     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
-    .priv_data_size = sizeof(MpegEncContext),
+    .priv_data_size = sizeof(MPVMainEncContext),
     .init           = ff_mpv_encode_init,
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = ff_mpv_encode_end,
diff --git a/libavcodec/rv20enc.c b/libavcodec/rv20enc.c
index cf27ff04ea..cacda6fdda 100644
--- a/libavcodec/rv20enc.c
+++ b/libavcodec/rv20enc.c
@@ -69,7 +69,7 @@ const FFCodec ff_rv20_encoder = {
     .p.id           = AV_CODEC_ID_RV20,
     .p.priv_class   = &ff_mpv_enc_class,
     .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
-    .priv_data_size = sizeof(MpegEncContext),
+    .priv_data_size = sizeof(MPVMainEncContext),
     .init           = ff_mpv_encode_init,
     FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
     .close          = ff_mpv_encode_end,
diff --git a/libavcodec/speedhqenc.c b/libavcodec/speedhqenc.c
index 536ddc0bf1..a0baa869cc 100644
--- a/libavcodec/speedhqenc.c
+++ b/libavcodec/speedhqenc.c
@@ -59,7 +59,7 @@ static uint32_t speedhq_chr_dc_uni[512];
 static uint8_t uni_speedhq_ac_vlc_len[64 * 64 * 2];
 
 typedef struct SpeedHQEncContext {
-    MpegEncContext m;
+    MPVMainEncContext m;
 
     int slice_start;
 } SpeedHQEncContext;
diff --git a/libavcodec/wmv2enc.c b/libavcodec/wmv2enc.c
index 825ab91a05..33569a6d39 100644
--- a/libavcodec/wmv2enc.c
+++ b/libavcodec/wmv2enc.c
@@ -49,7 +49,7 @@ typedef struct WMV2EncContext {
 
 static int encode_ext_header(WMV2EncContext *w)
 {
-    MpegEncContext *const s = &w->msmpeg4.s;
+    MpegEncContext *const s = &w->msmpeg4.m.s;
     PutBitContext pb;
     int code;
 
@@ -76,7 +76,7 @@ static int encode_ext_header(WMV2EncContext *w)
 static av_cold int wmv2_encode_init(AVCodecContext *avctx)
 {
     WMV2EncContext *const w = avctx->priv_data;
-    MpegEncContext *const s = &w->msmpeg4.s;
+    MpegEncContext *const s = &w->msmpeg4.m.s;
 
     s->private_ctx = &w->common;
     if (ff_mpv_encode_init(avctx) < 0)
-- 
2.45.2