diff mbox series

[FFmpeg-devel,22/57] avcodec/me_cmp, motion_est: Move me_(pre_)?_cmp etc. to MotionEstContext

Message ID GV1P250MB0737577BFF3D9C12ED57691E8FC02@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM
State New
Headers show
Series [FFmpeg-devel,01/57] avcodec/vc1: Combine identical checks | expand

Commit Message

Andreas Rheinhardt June 12, 2024, 1:48 p.m. UTC
MECmpContext has several arrays of function pointers that
are not set by ff_me_cmp_init(), but that are set by users
to one of the other arrays via ff_set_cmp().

One of these other users is the motion estimation API.
It uses MECmpContext.(me_pre|me|me_sub|mb)_cmp. It is
basically the only user of these arrays.

This commit therefore moves these arrays to MotionEstContext;
this has the additional advantage of making motion_est.c
more independent from MpegEncContext.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/me_cmp.h              |  4 ---
 libavcodec/motion_est.c          | 42 +++++++++++-----------
 libavcodec/motion_est.h          |  7 +++-
 libavcodec/motion_est_template.c | 60 ++++++++++++++++----------------
 libavcodec/snowenc.c             |  6 ++--
 tests/checkasm/motion.c          |  4 ---
 6 files changed, 60 insertions(+), 63 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/me_cmp.h b/libavcodec/me_cmp.h
index 14d19bd142..67e3816829 100644
--- a/libavcodec/me_cmp.h
+++ b/libavcodec/me_cmp.h
@@ -70,10 +70,6 @@  typedef struct MECmpContext {
     me_cmp_func dct_max[6];
     me_cmp_func dct264_sad[6];
 
-    me_cmp_func me_pre_cmp[6];
-    me_cmp_func me_cmp[6];
-    me_cmp_func me_sub_cmp[6];
-    me_cmp_func mb_cmp[6];
     me_cmp_func ildct_cmp[6]; // only width 16 used
     me_cmp_func frame_skip_cmp[6]; // only width 8 used
 
diff --git a/libavcodec/motion_est.c b/libavcodec/motion_est.c
index ee28a4a445..b29d0c6d96 100644
--- a/libavcodec/motion_est.c
+++ b/libavcodec/motion_est.c
@@ -305,7 +305,7 @@  static int zero_cmp(MpegEncContext *s, const uint8_t *a, const uint8_t *b,
 static void zero_hpel(uint8_t *a, const uint8_t *b, ptrdiff_t stride, int h){
 }
 
-av_cold int ff_me_init(MotionEstContext *c, AVCodecContext *avctx, MECmpContext *mecc)
+av_cold int ff_me_init(MotionEstContext *c, AVCodecContext *avctx, const MECmpContext *mecc)
 {
     int cache_size = FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
     int dia_size   = FFMAX(FFABS(avctx->dia_size) & 255, FFABS(avctx->pre_dia_size) & 255);
@@ -324,10 +324,10 @@  av_cold int ff_me_init(MotionEstContext *c, AVCodecContext *avctx, MECmpContext
     if (cache_size < 2 * dia_size)
         av_log(avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
 
-    ret  = ff_set_cmp(mecc, mecc->me_pre_cmp, avctx->me_pre_cmp);
-    ret |= ff_set_cmp(mecc, mecc->me_cmp,     avctx->me_cmp);
-    ret |= ff_set_cmp(mecc, mecc->me_sub_cmp, avctx->me_sub_cmp);
-    ret |= ff_set_cmp(mecc, mecc->mb_cmp,     avctx->mb_cmp);
+    ret  = ff_set_cmp(mecc, c->me_pre_cmp, avctx->me_pre_cmp);
+    ret |= ff_set_cmp(mecc, c->me_cmp,     avctx->me_cmp);
+    ret |= ff_set_cmp(mecc, c->me_sub_cmp, avctx->me_sub_cmp);
+    ret |= ff_set_cmp(mecc, c->mb_cmp,     avctx->mb_cmp);
     if (ret < 0)
         return ret;
 
@@ -354,10 +354,10 @@  av_cold int ff_me_init(MotionEstContext *c, AVCodecContext *avctx, MECmpContext
      * not have yet, and even if we had, the motion estimation code
      * does not expect it. */
     if (avctx->codec_id != AV_CODEC_ID_SNOW) {
-        if ((avctx->me_cmp & FF_CMP_CHROMA) /* && !s->mecc.me_cmp[2] */)
-            mecc->me_cmp[2] = zero_cmp;
-        if ((avctx->me_sub_cmp & FF_CMP_CHROMA) && !mecc->me_sub_cmp[2])
-            mecc->me_sub_cmp[2] = zero_cmp;
+        if ((avctx->me_cmp & FF_CMP_CHROMA) /* && !c->me_cmp[2] */)
+            c->me_cmp[2] = zero_cmp;
+        if ((avctx->me_sub_cmp & FF_CMP_CHROMA) && !c->me_sub_cmp[2])
+            c->me_sub_cmp[2] = zero_cmp;
     }
 
     return 0;
@@ -649,7 +649,7 @@  static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
 
         dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
 
-        if (s->mecc.me_sub_cmp[0] != s->mecc.mb_cmp[0]) {
+        if (c->me_sub_cmp[0] != c->mb_cmp[0]) {
             int dxy;
             const int offset= ((block&1) + (block>>1)*stride)*8;
             uint8_t *dest_y = c->scratchpad + offset;
@@ -691,11 +691,11 @@  static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
     if(same)
         return INT_MAX;
 
-    if (s->mecc.me_sub_cmp[0] != s->mecc.mb_cmp[0]) {
-        dmin_sum += s->mecc.mb_cmp[0](s,
-                                      s->new_pic->data[0] +
-                                      s->mb_x * 16 + s->mb_y * 16 * stride,
-                                      c->scratchpad, stride, 16);
+    if (c->me_sub_cmp[0] != c->mb_cmp[0]) {
+        dmin_sum += c->mb_cmp[0](s,
+                                 s->new_pic->data[0] +
+                                 s->mb_x * 16 + s->mb_y * 16 * stride,
+                                 c->scratchpad, stride, 16);
     }
 
     if(c->avctx->mb_cmp&FF_CMP_CHROMA){
@@ -717,8 +717,8 @@  static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
             s->hdsp.put_pixels_tab       [1][dxy](c->scratchpad + 8, s->last_pic.data[2] + offset, s->uvlinesize, 8);
         }
 
-        dmin_sum += s->mecc.mb_cmp[1](s, s->new_pic->data[1] + s->mb_x * 8 + s->mb_y * 8 * s->uvlinesize, c->scratchpad,     s->uvlinesize, 8);
-        dmin_sum += s->mecc.mb_cmp[1](s, s->new_pic->data[2] + s->mb_x * 8 + s->mb_y * 8 * s->uvlinesize, c->scratchpad + 8, s->uvlinesize, 8);
+        dmin_sum += c->mb_cmp[1](s, s->new_pic->data[1] + s->mb_x * 8 + s->mb_y * 8 * s->uvlinesize, c->scratchpad,     s->uvlinesize, 8);
+        dmin_sum += c->mb_cmp[1](s, s->new_pic->data[2] + s->mb_x * 8 + s->mb_y * 8 * s->uvlinesize, c->scratchpad + 8, s->uvlinesize, 8);
     }
 
     c->pred_x= mx;
@@ -814,7 +814,7 @@  static int interlaced_search(MpegEncContext *s, int ref_index,
             mv_table[xy][0]= mx_i;
             mv_table[xy][1]= my_i;
 
-            if (s->mecc.me_sub_cmp[0] != s->mecc.mb_cmp[0]) {
+            if (c->me_sub_cmp[0] != c->mb_cmp[0]) {
                 int dxy;
 
                 //FIXME chroma ME
@@ -826,7 +826,7 @@  static int interlaced_search(MpegEncContext *s, int ref_index,
                 }else{
                     s->hdsp.put_pixels_tab       [size][dxy](c->scratchpad, ref    , stride, h);
                 }
-                dmin = s->mecc.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
+                dmin = c->mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
                 dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
             }else
                 dmin+= c->mb_penalty_factor; //field_select bits
@@ -1049,7 +1049,7 @@  void ff_estimate_p_frame_motion(MpegEncContext * s,
                 *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
             }
 
-            intra_score= s->mecc.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
+            intra_score= c->mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
         }
         intra_score += c->mb_penalty_factor*16 + s->intra_penalty;
 
@@ -1236,7 +1236,7 @@  static inline int check_bidir_mv(MpegEncContext * s,
 
     fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
            +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
-           + s->mecc.mb_cmp[size](s, src_data[0], dest_y, stride, h); // FIXME new_pic
+           + c->mb_cmp[size](s, src_data[0], dest_y, stride, h); // FIXME new_pic
 
     if(c->avctx->mb_cmp&FF_CMP_CHROMA){
     }
diff --git a/libavcodec/motion_est.h b/libavcodec/motion_est.h
index feea9a473b..5547aeb8bc 100644
--- a/libavcodec/motion_est.h
+++ b/libavcodec/motion_est.h
@@ -84,6 +84,11 @@  typedef struct MotionEstContext {
     int64_t mb_var_sum_temp;
     int scene_change_score;
 
+    me_cmp_func me_pre_cmp[6];
+    me_cmp_func me_cmp[6];
+    me_cmp_func me_sub_cmp[6];
+    me_cmp_func mb_cmp[6];
+
     op_pixels_func(*hpel_put)[4];
     op_pixels_func(*hpel_avg)[4];
     qpel_mc_func(*qpel_put)[16];
@@ -110,7 +115,7 @@  static inline int ff_h263_round_chroma(int x)
  * Performs one-time initialization of the MotionEstContext.
  */
 int ff_me_init(MotionEstContext *c, struct AVCodecContext *avctx,
-               struct MECmpContext *mecc);
+               const struct MECmpContext *mecc);
 
 void ff_me_init_pic(struct MpegEncContext *s);
 
diff --git a/libavcodec/motion_est_template.c b/libavcodec/motion_est_template.c
index 1888697db7..b2701aa32e 100644
--- a/libavcodec/motion_est_template.c
+++ b/libavcodec/motion_est_template.c
@@ -64,8 +64,8 @@  static int hpel_motion_search(MpegEncContext * s,
 
  //FIXME factorize
 
-    cmp_sub        = s->mecc.me_sub_cmp[size];
-    chroma_cmp_sub = s->mecc.me_sub_cmp[size + 1];
+    cmp_sub        = c->me_sub_cmp[size];
+    chroma_cmp_sub = c->me_sub_cmp[size + 1];
 
     if(c->skip){ //FIXME move out of hpel?
         *mx_ptr = 0;
@@ -178,8 +178,8 @@  static inline int get_mb_score(MpegEncContext *s, int mx, int my,
 
  //FIXME factorize
 
-    cmp_sub        = s->mecc.mb_cmp[size];
-    chroma_cmp_sub = s->mecc.mb_cmp[size + 1];
+    cmp_sub        = c->mb_cmp[size];
+    chroma_cmp_sub = c->mb_cmp[size + 1];
 
     d= cmp(s, mx>>(qpel+1), my>>(qpel+1), mx&mask, my&mask, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
     //FIXME check cbp before adding penalty for (0,0) vector
@@ -222,12 +222,12 @@  static int qpel_motion_search(MpegEncContext * s,
     LOAD_COMMON
     int flags= c->sub_flags;
 
-    cmpf        = s->mecc.me_cmp[size];
-    chroma_cmpf = s->mecc.me_cmp[size + 1]; // FIXME: factorize
+    cmpf        = c->me_cmp[size];
+    chroma_cmpf = c->me_cmp[size + 1]; // FIXME: factorize
  //FIXME factorize
 
-    cmp_sub        = s->mecc.me_sub_cmp[size];
-    chroma_cmp_sub = s->mecc.me_sub_cmp[size + 1];
+    cmp_sub        = c->me_sub_cmp[size];
+    chroma_cmp_sub = c->me_sub_cmp[size + 1];
 
     if(c->skip){ //FIXME somehow move up (benchmark)
         *mx_ptr = 0;
@@ -424,8 +424,8 @@  static av_always_inline int small_diamond_search(MpegEncContext * s, int *best,
     LOAD_COMMON2
     unsigned map_generation = c->map_generation;
 
-    cmpf        = s->mecc.me_cmp[size];
-    chroma_cmpf = s->mecc.me_cmp[size + 1];
+    cmpf        = c->me_cmp[size];
+    chroma_cmpf = c->me_cmp[size + 1];
 
     { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
         const unsigned key = ((unsigned)best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
@@ -465,8 +465,8 @@  static int funny_diamond_search(MpegEncContext * s, int *best, int dmin,
     LOAD_COMMON2
     unsigned map_generation = c->map_generation;
 
-    cmpf        = s->mecc.me_cmp[size];
-    chroma_cmpf = s->mecc.me_cmp[size + 1];
+    cmpf        = c->me_cmp[size];
+    chroma_cmpf = c->me_cmp[size + 1];
 
     for(dia_size=1; dia_size<=4; dia_size++){
         int dir;
@@ -508,8 +508,8 @@  static int hex_search(MpegEncContext * s, int *best, int dmin,
     int x,y,d;
     const int dec= dia_size & (dia_size-1);
 
-    cmpf        = s->mecc.me_cmp[size];
-    chroma_cmpf = s->mecc.me_cmp[size + 1];
+    cmpf        = c->me_cmp[size];
+    chroma_cmpf = c->me_cmp[size + 1];
 
     for(;dia_size; dia_size= dec ? dia_size-1 : dia_size>>1){
         do{
@@ -545,8 +545,8 @@  static int l2s_dia_search(MpegEncContext * s, int *best, int dmin,
     static const int hex[8][2]={{-2, 0}, {-1,-1}, { 0,-2}, { 1,-1},
                                 { 2, 0}, { 1, 1}, { 0, 2}, {-1, 1}};
 
-    cmpf        = s->mecc.me_cmp[size];
-    chroma_cmpf = s->mecc.me_cmp[size + 1];
+    cmpf        = c->me_cmp[size];
+    chroma_cmpf = c->me_cmp[size + 1];
 
     for(; dia_size; dia_size= dec ? dia_size-1 : dia_size>>1){
         do{
@@ -584,8 +584,8 @@  static int umh_search(MpegEncContext * s, int *best, int dmin,
                                  {-2, 3}, { 0, 4}, { 2, 3},
                                  {-2,-3}, { 0,-4}, { 2,-3},};
 
-    cmpf        = s->mecc.me_cmp[size];
-    chroma_cmpf = s->mecc.me_cmp[size + 1];
+    cmpf        = c->me_cmp[size];
+    chroma_cmpf = c->me_cmp[size + 1];
 
     x= best[0];
     y= best[1];
@@ -627,8 +627,8 @@  static int full_search(MpegEncContext * s, int *best, int dmin,
     int x,y, d;
     const int dia_size= c->dia_size&0xFF;
 
-    cmpf        = s->mecc.me_cmp[size];
-    chroma_cmpf = s->mecc.me_cmp[size + 1];
+    cmpf        = c->me_cmp[size];
+    chroma_cmpf = c->me_cmp[size + 1];
 
     for(y=FFMAX(-dia_size, ymin); y<=FFMIN(dia_size,ymax); y++){
         for(x=FFMAX(-dia_size, xmin); x<=FFMIN(dia_size,xmax); x++){
@@ -693,8 +693,8 @@  static int sab_diamond_search(MpegEncContext * s, int *best, int dmin,
 
     av_assert1(minima_count <= MAX_SAB_SIZE);
 
-    cmpf        = s->mecc.me_cmp[size];
-    chroma_cmpf = s->mecc.me_cmp[size + 1];
+    cmpf        = c->me_cmp[size];
+    chroma_cmpf = c->me_cmp[size + 1];
 
     /*Note j<MAX_SAB_SIZE is needed if MAX_SAB_SIZE < ME_MAP_SIZE as j can
       become larger due to MVs overflowing their ME_MAP_MV_BITS bits space in map
@@ -779,8 +779,8 @@  static int var_diamond_search(MpegEncContext * s, int *best, int dmin,
     LOAD_COMMON2
     unsigned map_generation = c->map_generation;
 
-    cmpf        = s->mecc.me_cmp[size];
-    chroma_cmpf = s->mecc.me_cmp[size + 1];
+    cmpf        = c->me_cmp[size];
+    chroma_cmpf = c->me_cmp[size + 1];
 
     for(dia_size=1; dia_size<=c->dia_size; dia_size++){
         int dir, start, end;
@@ -880,12 +880,12 @@  static av_always_inline int epzs_motion_search_internal(MpegEncContext * s, int
 
     if(c->pre_pass){
         penalty_factor= c->pre_penalty_factor;
-        cmpf           = s->mecc.me_pre_cmp[size];
-        chroma_cmpf    = s->mecc.me_pre_cmp[size + 1];
+        cmpf           = c->me_pre_cmp[size];
+        chroma_cmpf    = c->me_pre_cmp[size + 1];
     }else{
         penalty_factor= c->penalty_factor;
-        cmpf           = s->mecc.me_cmp[size];
-        chroma_cmpf    = s->mecc.me_cmp[size + 1];
+        cmpf           = c->me_cmp[size];
+        chroma_cmpf    = c->me_cmp[size + 1];
     }
 
     map_generation= update_map_generation(c);
@@ -1008,8 +1008,8 @@  static int epzs_motion_search2(MpegEncContext * s,
     int flags= c->flags;
     LOAD_COMMON2
 
-    cmpf        = s->mecc.me_cmp[size];
-    chroma_cmpf = s->mecc.me_cmp[size + 1];
+    cmpf        = c->me_cmp[size];
+    chroma_cmpf = c->me_cmp[size + 1];
 
     map_generation= update_map_generation(c);
 
diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
index f3c78cfb21..9d64b1efe2 100644
--- a/libavcodec/snowenc.c
+++ b/libavcodec/snowenc.c
@@ -832,12 +832,12 @@  static int get_block_rd(SnowEncContext *enc, int mb_x, int mb_y,
             distortion = 0;
             for(i=0; i<4; i++){
                 int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;
-                distortion += enc->mecc.me_cmp[0](&enc->m, src + off, dst + off, ref_stride, 16);
+                distortion += enc->m.me.me_cmp[0](&enc->m, src + off, dst + off, ref_stride, 16);
             }
         }
     }else{
         av_assert2(block_w==8);
-        distortion = enc->mecc.me_cmp[0](&enc->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
+        distortion = enc->m.me.me_cmp[0](&enc->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);
     }
 
     if(plane_index==0){
@@ -903,7 +903,7 @@  static int get_4block_rd(SnowEncContext *enc, int mb_x, int mb_y, int plane_inde
         }
 
         av_assert1(block_w== 8 || block_w==16);
-        distortion += enc->mecc.me_cmp[block_w==8](&enc->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_h);
+        distortion += enc->m.me.me_cmp[block_w==8](&enc->m, src + x + y*ref_stride, dst + x + y*ref_stride, ref_stride, block_h);
     }
 
     if(plane_index==0){
diff --git a/tests/checkasm/motion.c b/tests/checkasm/motion.c
index e7a36bbbda..2bf49afc77 100644
--- a/tests/checkasm/motion.c
+++ b/tests/checkasm/motion.c
@@ -94,10 +94,6 @@  static void test_motion(const char *name, me_cmp_func test_func)
     XX(vsad)                                                                   \
     XX(vsse)                                                                   \
     XX(nsse)                                                                   \
-    XX(me_pre_cmp)                                                             \
-    XX(me_cmp)                                                                 \
-    XX(me_sub_cmp)                                                             \
-    XX(mb_cmp)                                                                 \
     XX(ildct_cmp)                                                              \
     XX(frame_skip_cmp)                                                         \
     XX(median_sad)