From c0215651a2b84f8f5faf56ebab4b86643b571402 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Tue, 25 Mar 2025 03:23:13 +0100
Subject: [PATCH 07/11] avcodec/motion_est: Put map, me_map into
 MotionEstContext

They have a fixed size and given that nowadays
MotionEstContext is no longer in any decoder's private context,
it is not wasteful to just put it into there.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/motion_est.h    |  5 +++--
 libavcodec/mpegvideo_enc.c | 15 ++-------------
 libavcodec/snowenc.c       |  5 +----
 libavcodec/svq1enc.c       |  5 +----
 4 files changed, 7 insertions(+), 23 deletions(-)

diff --git a/libavcodec/motion_est.h b/libavcodec/motion_est.h
index fe3d61fafb..89a2f41b75 100644
--- a/libavcodec/motion_est.h
+++ b/libavcodec/motion_est.h
@@ -55,8 +55,6 @@ typedef struct MotionEstContext {
     uint8_t *scratchpad;            /**< data area for the ME algo, so that
                                      * the ME does not need to malloc/free. */
     uint8_t *temp;
-    uint32_t *map;                  ///< map to avoid duplicate evaluations
-    uint32_t *score_map;            ///< map to store the scores
     unsigned map_generation;
     int pre_penalty_factor;
     int penalty_factor;             /**< an estimate of the bits required to
@@ -104,6 +102,9 @@ typedef struct MotionEstContext {
                              int *mx_ptr, int *my_ptr, int dmin,
                              int src_index, int ref_index,
                              int size, int h);
+
+    uint32_t map[ME_MAP_SIZE];      ///< map to avoid duplicate evaluations
+    uint32_t score_map[ME_MAP_SIZE];///< map to store the scores
 } MotionEstContext;
 
 /**
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index febe50930b..3d67df8d73 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -451,13 +451,12 @@ static av_cold int init_buffers(MPVMainEncContext *const m, AVCodecContext *avct
 #else
         ALIGN = 128,
 #endif
-        ME_MAP_ALLOC_SIZE = FFALIGN(2 * ME_MAP_SIZE * sizeof(*s->me.map), ALIGN),
         DCT_ERROR_SIZE    = FFALIGN(2 * sizeof(*s->dct_error_sum), ALIGN),
     };
-    static_assert(FFMAX(ME_MAP_ALLOC_SIZE, DCT_ERROR_SIZE) * MAX_THREADS + ALIGN - 1 <= SIZE_MAX,
+    static_assert(DCT_ERROR_SIZE * MAX_THREADS + ALIGN - 1 <= SIZE_MAX,
                   "Need checks for potential overflow.");
     unsigned nb_slices = s->c.slice_context_count, mv_table_size, mb_array_size;
-    char *dct_error = NULL, *me_map;
+    char *dct_error = NULL;
     int has_b_frames = !!m->max_b_frames, nb_mv_tables = 1 + 5 * has_b_frames;
     int16_t (*mv_table)[2];
 
@@ -470,11 +469,6 @@ static av_cold int init_buffers(MPVMainEncContext *const m, AVCodecContext *avct
         m->dct_error_sum_base = dct_error;
         dct_error += FFALIGN((uintptr_t)dct_error, ALIGN) - (uintptr_t)dct_error;
     }
-    me_map = av_mallocz(ALIGN - 1 + nb_slices * ME_MAP_ALLOC_SIZE);
-    if (!me_map)
-        return AVERROR(ENOMEM);
-    m->me_map_base = me_map;
-    me_map += FFALIGN((uintptr_t)me_map, ALIGN) - (uintptr_t)me_map;
 
     /* Allocate MB type table */
     mb_array_size = s->c.mb_stride * s->c.mb_height;
@@ -514,10 +508,6 @@ static av_cold int init_buffers(MPVMainEncContext *const m, AVCodecContext *avct
         s2->mb_mean      = (uint8_t*)(s2->mb_var + mb_array_size);
         s2->lambda_table = s->lambda_table;
 
-        s2->me.map       = (uint32_t*)me_map;
-        s2->me.score_map = s2->me.map + ME_MAP_SIZE;
-        me_map          += ME_MAP_ALLOC_SIZE;
-
         s2->p_mv_table            = tmp_mv_table;
         if (has_b_frames) {
             s2->b_forw_mv_table       = tmp_mv_table += mv_table_size;
@@ -1132,7 +1122,6 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
     av_freep(&m->mv_table_base);
     av_freep(&s->p_field_select_table[0]);
     av_freep(&m->dct_error_sum_base);
-    av_freep(&m->me_map_base);
 
     av_freep(&s->mb_type);
     av_freep(&s->lambda_table);
diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
index a15960e4a3..7818c79fa6 100644
--- a/libavcodec/snowenc.c
+++ b/libavcodec/snowenc.c
@@ -235,10 +235,8 @@ static av_cold int encode_init(AVCodecContext *avctx)
     mpv->me.temp      =
     mpv->me.scratchpad = av_calloc(avctx->width + 64, 2*16*2*sizeof(uint8_t));
     mpv->c.sc.obmc_scratchpad = av_mallocz(MB_SIZE*MB_SIZE*12*sizeof(uint32_t));
-    mpv->me.map       = av_mallocz(2 * ME_MAP_SIZE * sizeof(*mpv->me.map));
-    if (!mpv->me.scratchpad || !mpv->me.map || !mpv->c.sc.obmc_scratchpad)
+    if (!mpv->me.scratchpad || !mpv->c.sc.obmc_scratchpad)
         return AVERROR(ENOMEM);
-    mpv->me.score_map = mpv->me.map + ME_MAP_SIZE;
 
     mpv->me.mv_penalty = ff_h263_get_mv_penalty();
 
@@ -2093,7 +2091,6 @@ static av_cold int encode_end(AVCodecContext *avctx)
 
     enc->m.s.me.temp = NULL;
     av_freep(&enc->m.s.me.scratchpad);
-    av_freep(&enc->m.s.me.map);
     av_freep(&enc->m.s.c.sc.obmc_scratchpad);
 
     av_freep(&avctx->stats_out);
diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c
index 9d8b0d496f..0a92c5607c 100644
--- a/libavcodec/svq1enc.c
+++ b/libavcodec/svq1enc.c
@@ -540,7 +540,6 @@ static av_cold int svq1_encode_end(AVCodecContext *avctx)
                                       avctx->frame_num));
 
     av_freep(&s->m.me.scratchpad);
-    av_freep(&s->m.me.map);
     av_freep(&s->mb_type);
     av_freep(&s->dummy);
     av_freep(&s->scratchbuf);
@@ -620,13 +619,11 @@ static av_cold int svq1_encode_init(AVCodecContext *avctx)
                                         s->y_block_height * sizeof(int16_t));
     s->dummy               = av_mallocz((s->y_block_width + 1) *
                                         s->y_block_height * sizeof(int32_t));
-    s->m.me.map            = av_mallocz(2 * ME_MAP_SIZE * sizeof(*s->m.me.map));
     s->m.new_pic       = av_frame_alloc();
 
-    if (!s->m.me.scratchpad || !s->m.me.map ||
+    if (!s->m.me.scratchpad ||
         !s->mb_type || !s->dummy || !s->m.new_pic)
         return AVERROR(ENOMEM);
-    s->m.me.score_map = s->m.me.map + ME_MAP_SIZE;
 
     ff_svq1enc_init(&s->svq1encdsp);
 
-- 
2.45.2