From cb00bce3c7de503711cf83183ee8e8c44e48f168 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Sat, 29 Jan 2022 05:25:25 +0100
Subject: [PATCH 55/77] avcodec/mpegvideo: Move last-pic information to
 MPVMainEncContext

last_pict_type, last_non_b_pict_type and last_lambda_for
are only used by the encoder's main thread.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegvideo.h     |  3 ---
 libavcodec/mpegvideo_enc.c | 16 ++++++++--------
 libavcodec/mpegvideoenc.h  |  3 +++
 libavcodec/msmpeg4enc.c    |  4 ++--
 libavcodec/ratecontrol.c   |  4 ++--
 libavcodec/snowenc.c       |  2 +-
 6 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index 02894ce68f..c16fb1c703 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -185,10 +185,7 @@ typedef struct MpegEncContext {
     int adaptive_quant;         ///< use adaptive quantization
     int dquant;                 ///< qscale difference to prev qscale
     int pict_type;              ///< AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
-    int last_pict_type; //FIXME removes
-    int last_non_b_pict_type;   ///< used for MPEG-4 gmc B-frames & ratecontrol
     int droppable;
-    int last_lambda_for[5];     ///< last lambda for a specific pict type
     int skipdct;                ///< skip dct and code zero residual
 
     /* motion compensation */
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index b32b8fe768..9f44c37b3d 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -1515,9 +1515,9 @@ static int estimate_best_b_count(MPVMainEncContext *const m)
         return AVERROR(ENOMEM);
 
     //emms_c();
-    p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
+    p_lambda = m->last_lambda_for[AV_PICTURE_TYPE_P];
     //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
-    b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
+    b_lambda = m->last_lambda_for[AV_PICTURE_TYPE_B];
     if (!b_lambda) // FIXME we should do this somewhere else
         b_lambda = p_lambda;
     lambda2  = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
@@ -1871,10 +1871,10 @@ static void frame_end(MPVMainEncContext *const m)
 
     emms_c();
 
-    s->last_pict_type                 = s->pict_type;
-    s->last_lambda_for [s->pict_type] = s->cur_pic.ptr->f->quality;
+    m->last_pict_type                = s->pict_type;
+    m->last_lambda_for[s->pict_type] = s->cur_pic.ptr->f->quality;
     if (s->pict_type!= AV_PICTURE_TYPE_B)
-        s->last_non_b_pict_type = s->pict_type;
+        m->last_non_b_pict_type = s->pict_type;
 }
 
 static void update_noise_reduction(MPVMainEncContext *const m)
@@ -3702,9 +3702,9 @@ static int encode_picture(MPVMainEncContext *const m, const AVPacket *pkt)
         ff_get_2pass_fcode(m);
     } else if (!(s->avctx->flags & AV_CODEC_FLAG_QSCALE)) {
         if(s->pict_type==AV_PICTURE_TYPE_B)
-            s->lambda= s->last_lambda_for[s->pict_type];
+            s->lambda = m->last_lambda_for[s->pict_type];
         else
-            s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
+            s->lambda = m->last_lambda_for[m->last_non_b_pict_type];
         update_qscale(m);
     }
 
@@ -3735,7 +3735,7 @@ static int encode_picture(MPVMainEncContext *const m, const AVPacket *pkt)
         s->lambda  = (s->lambda  * s->me_penalty_compensation + 128) >> 8;
         s->lambda2 = (s->lambda2 * (int64_t) s->me_penalty_compensation + 128) >> 8;
         if (s->pict_type != AV_PICTURE_TYPE_B) {
-            if ((s->me_pre && s->last_non_b_pict_type == AV_PICTURE_TYPE_I) ||
+            if ((s->me_pre && m->last_non_b_pict_type == AV_PICTURE_TYPE_I) ||
                 s->me_pre == 2) {
                 s->avctx->execute(s->avctx, pre_estimate_motion_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
             }
diff --git a/libavcodec/mpegvideoenc.h b/libavcodec/mpegvideoenc.h
index d101c92cbb..7054881c05 100644
--- a/libavcodec/mpegvideoenc.h
+++ b/libavcodec/mpegvideoenc.h
@@ -96,6 +96,9 @@ typedef struct MPVMainEncContext {
     int stuffing_bits;             ///< bits used for stuffing
     int next_lambda;               ///< next lambda used for retrying to encode a frame
     int fixed_qscale;              ///< fixed qscale if non zero
+    int last_lambda_for[5];        ///< last lambda for a specific pict type
+    int last_pict_type;            //FIXME removes
+    int last_non_b_pict_type;      ///< used for MPEG-4 gmc B-frames & ratecontrol
     RateControlContext rc_context; ///< contains stuff only accessed in ratecontrol.c
 
     int64_t mb_var_sum;            ///< sum of MB variance for current frame
diff --git a/libavcodec/msmpeg4enc.c b/libavcodec/msmpeg4enc.c
index 78197756ae..bba0493f01 100644
--- a/libavcodec/msmpeg4enc.c
+++ b/libavcodec/msmpeg4enc.c
@@ -203,8 +203,8 @@ static void find_best_tables(MSMPEG4EncContext *ms)
     ms->rl_table_index        =        best;
     ms->rl_chroma_table_index = chroma_best;
 
-    if(s->pict_type != s->last_non_b_pict_type){
-        ms->rl_table_index = 2;
+    if (s->pict_type != ms->m.last_non_b_pict_type) {
+        ms->rl_table_index= 2;
         if(s->pict_type==AV_PICTURE_TYPE_I)
             ms->rl_chroma_table_index = 1;
         else
diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c
index 0d2bf8fb30..b131f61b70 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -940,10 +940,10 @@ float ff_rate_estimate_qscale(MPVMainEncContext *const m, int dry_run)
     /* update predictors */
     if (picture_number > 2 && !dry_run) {
         const int64_t last_var =
-            s->last_pict_type == AV_PICTURE_TYPE_I ? rcc->last_mb_var_sum
+            m->last_pict_type == AV_PICTURE_TYPE_I ? rcc->last_mb_var_sum
                                                    : rcc->last_mc_mb_var_sum;
         av_assert1(m->frame_bits >= m->stuffing_bits);
-        update_predictor(&rcc->pred[s->last_pict_type],
+        update_predictor(&rcc->pred[m->last_pict_type],
                          rcc->last_qscale,
                          sqrt(last_var),
                          m->frame_bits - m->stuffing_bits);
diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
index b4a329158f..e6cf2a290c 100644
--- a/libavcodec/snowenc.c
+++ b/libavcodec/snowenc.c
@@ -2058,7 +2058,7 @@ redo_frame:
     }
     if(avctx->flags&AV_CODEC_FLAG_PASS1)
         ff_write_pass1_stats(&enc->m);
-    mpv->last_pict_type = mpv->pict_type;
+    enc->m.last_pict_type = mpv->pict_type;
 
     emms_c();
 
-- 
2.45.2