From 0cc58c515276ccb0c6c6fbf1d5828dc4f72b510a Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Sat, 1 Mar 2025 02:04:41 +0100
Subject: [PATCH 06/77] avcodec/mpegvideo: Move frame_skip_* fields to
 MPVMainEncContext

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mpegvideo.h     |  7 -------
 libavcodec/mpegvideo_enc.c | 31 +++++++++++++++++--------------
 libavcodec/mpegvideoenc.h  | 15 +++++++++++----
 3 files changed, 28 insertions(+), 25 deletions(-)

diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index 0bb12a343e..2cc6dbf534 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -510,13 +510,6 @@ typedef struct MpegEncContext {
 
     int error_rate;
 
-    /* frame skip options for encoding */
-    int frame_skip_threshold;
-    int frame_skip_factor;
-    int frame_skip_exp;
-    int frame_skip_cmp;
-    me_cmp_func frame_skip_cmp_fn;
-
     int scenechange_threshold;
     int noise_reduction;
 
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index d2bd6f4a18..1b7a4c1236 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -307,8 +307,9 @@ av_cold void ff_dct_encode_init(MpegEncContext *s)
         s->dct_quantize  = dct_quantize_trellis_c;
 }
 
-static av_cold int me_cmp_init(MpegEncContext *s, AVCodecContext *avctx)
+static av_cold int me_cmp_init(MPVMainEncContext *const m, AVCodecContext *avctx)
 {
+    MpegEncContext *const s = &m->s;
     MECmpContext mecc;
     me_cmp_func me_cmp[6];
     int ret;
@@ -317,10 +318,10 @@ static av_cold int me_cmp_init(MpegEncContext *s, AVCodecContext *avctx)
     ret = ff_me_init(&s->me, avctx, &mecc, 1);
     if (ret < 0)
         return ret;
-    ret = ff_set_cmp(&mecc, me_cmp, s->frame_skip_cmp, 1);
+    ret = ff_set_cmp(&mecc, me_cmp, m->frame_skip_cmp, 1);
     if (ret < 0)
         return ret;
-    s->frame_skip_cmp_fn = me_cmp[1];
+    m->frame_skip_cmp_fn = me_cmp[1];
     if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
         ret = ff_set_cmp(&mecc, me_cmp, avctx->ildct_cmp, 1);
         if (ret < 0)
@@ -898,7 +899,7 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
                                 s->alternate_scan);
 
     if (avctx->flags & AV_CODEC_FLAG_PSNR || avctx->mb_decision == FF_MB_DECISION_RD ||
-        s->frame_skip_threshold || s->frame_skip_factor) {
+        m->frame_skip_threshold || m->frame_skip_factor) {
         s->frame_reconstruction_bitfield = (1 << AV_PICTURE_TYPE_I) |
                                            (1 << AV_PICTURE_TYPE_P) |
                                            (1 << AV_PICTURE_TYPE_B);
@@ -922,7 +923,7 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
     ff_fdctdsp_init(&s->fdsp, avctx);
     ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
     ff_pixblockdsp_init(&s->pdsp, avctx);
-    ret = me_cmp_init(s, avctx);
+    ret = me_cmp_init(m, avctx);
     if (ret < 0)
         return ret;
 
@@ -1402,8 +1403,10 @@ fail:
     return ret;
 }
 
-static int skip_check(MpegEncContext *s, const MPVPicture *p, const MPVPicture *ref)
+static int skip_check(MPVMainEncContext *const m,
+                      const MPVPicture *p, const MPVPicture *ref)
 {
+    MpegEncContext *const s = &m->s;
     int x, y, plane;
     int score = 0;
     int64_t score64 = 0;
@@ -1416,9 +1419,9 @@ static int skip_check(MpegEncContext *s, const MPVPicture *p, const MPVPicture *
                 int off = p->shared ? 0 : 16;
                 const uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
                 const uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
-                int v = s->frame_skip_cmp_fn(s, dptr, rptr, stride, 8);
+                int v = m->frame_skip_cmp_fn(s, dptr, rptr, stride, 8);
 
-                switch (FFABS(s->frame_skip_exp)) {
+                switch (FFABS(m->frame_skip_exp)) {
                 case 0: score    =  FFMAX(score, v);          break;
                 case 1: score   += FFABS(v);                  break;
                 case 2: score64 += v * (int64_t)v;                       break;
@@ -1432,13 +1435,13 @@ static int skip_check(MpegEncContext *s, const MPVPicture *p, const MPVPicture *
 
     if (score)
         score64 = score;
-    if (s->frame_skip_exp < 0)
+    if (m->frame_skip_exp < 0)
         score64 = pow(score64 / (double)(s->mb_width * s->mb_height),
-                      -1.0/s->frame_skip_exp);
+                      -1.0/m->frame_skip_exp);
 
-    if (score64 < s->frame_skip_threshold)
+    if (score64 < m->frame_skip_threshold)
         return 1;
-    if (score64 < ((s->frame_skip_factor * (int64_t) s->lambda) >> 8))
+    if (score64 < ((m->frame_skip_factor * (int64_t) s->lambda) >> 8))
         return 1;
     return 0;
 }
@@ -1626,10 +1629,10 @@ static int set_bframe_chain_length(MPVMainEncContext *const m)
         return 0;
 
     /* set next picture type & ordering */
-    if (s->frame_skip_threshold || s->frame_skip_factor) {
+    if (m->frame_skip_threshold || m->frame_skip_factor) {
         if (s->picture_in_gop_number < s->gop_size &&
             s->next_pic.ptr &&
-            skip_check(s, s->input_picture[0], s->next_pic.ptr)) {
+            skip_check(m, s->input_picture[0], s->next_pic.ptr)) {
             // FIXME check that the gop check above is +-1 correct
             av_refstruct_unref(&s->input_picture[0]);
 
diff --git a/libavcodec/mpegvideoenc.h b/libavcodec/mpegvideoenc.h
index c1d2821038..9f0d63c31e 100644
--- a/libavcodec/mpegvideoenc.h
+++ b/libavcodec/mpegvideoenc.h
@@ -45,6 +45,13 @@ typedef struct MPVMainEncContext {
     int b_sensitivity;
     int brd_scale;
 
+    /* frame skip options */
+    int frame_skip_threshold;
+    int frame_skip_factor;
+    int frame_skip_exp;
+    int frame_skip_cmp;
+    me_cmp_func frame_skip_cmp_fn;
+
     /* bit rate control */
     int64_t total_bits;
     int frame_bits;                ///< bits used for the current frame
@@ -134,10 +141,10 @@ FF_MPV_OPT_CMP_FUNC, \
 {"border_mask", "increase the quantizer for macroblocks close to borders", FF_MPV_OFFSET(border_masking), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS},    \
 {"lmin", "minimum Lagrange factor (VBR)",                           FF_MPV_OFFSET(lmin), AV_OPT_TYPE_INT, {.i64 =  2*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS },            \
 {"lmax", "maximum Lagrange factor (VBR)",                           FF_MPV_OFFSET(lmax), AV_OPT_TYPE_INT, {.i64 = 31*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS },            \
-{"skip_threshold", "Frame skip threshold",                          FF_MPV_OFFSET(frame_skip_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
-{"skip_factor", "Frame skip factor",                                FF_MPV_OFFSET(frame_skip_factor), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
-{"skip_exp", "Frame skip exponent",                                 FF_MPV_OFFSET(frame_skip_exp), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
-{"skip_cmp", "Frame skip compare function",                         FF_MPV_OFFSET(frame_skip_cmp), AV_OPT_TYPE_INT, {.i64 = FF_CMP_DCTMAX }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
+{"skip_threshold", "Frame skip threshold",                          FF_MPV_MAIN_OFFSET(frame_skip_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
+{"skip_factor", "Frame skip factor",                                FF_MPV_MAIN_OFFSET(frame_skip_factor), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
+{"skip_exp", "Frame skip exponent",                                 FF_MPV_MAIN_OFFSET(frame_skip_exp), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
+{"skip_cmp", "Frame skip compare function",                         FF_MPV_MAIN_OFFSET(frame_skip_cmp), AV_OPT_TYPE_INT, {.i64 = FF_CMP_DCTMAX }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
 {"sc_threshold", "Scene change threshold",                          FF_MPV_OFFSET(scenechange_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
 {"noise_reduction", "Noise reduction",                              FF_MPV_OFFSET(noise_reduction), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
 {"ps", "RTP payload size in bytes",                             FF_MPV_OFFSET(rtp_payload_size), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
-- 
2.45.2