• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <limits.h>
13 #include <math.h>
14 #include <stdio.h>
15 
16 #include "config/aom_config.h"
17 #include "config/aom_dsp_rtcd.h"
18 
19 #include "aom_dsp/aom_dsp_common.h"
20 #include "aom_mem/aom_mem.h"
21 #include "aom_ports/mem.h"
22 
23 #include "av1/common/av1_common_int.h"
24 #include "av1/common/common.h"
25 #include "av1/common/filter.h"
26 #include "av1/common/mvref_common.h"
27 #include "av1/common/reconinter.h"
28 
29 #include "av1/encoder/encoder.h"
30 #include "av1/encoder/encodemv.h"
31 #include "av1/encoder/mcomp.h"
32 #include "av1/encoder/rdopt.h"
33 #include "av1/encoder/reconinter_enc.h"
34 
init_mv_cost_params(MV_COST_PARAMS * mv_cost_params,const MACROBLOCK * x,const MV * ref_mv)35 static INLINE void init_mv_cost_params(MV_COST_PARAMS *mv_cost_params,
36                                        const MACROBLOCK *x, const MV *ref_mv) {
37   mv_cost_params->ref_mv = ref_mv;
38   mv_cost_params->full_ref_mv = get_fullmv_from_mv(ref_mv);
39   mv_cost_params->error_per_bit = x->errorperbit;
40   mv_cost_params->sad_per_bit = x->sadperbit;
41   mv_cost_params->mvjcost = x->nmv_vec_cost;
42   mv_cost_params->mvcost[0] = x->mv_cost_stack[0];
43   mv_cost_params->mvcost[1] = x->mv_cost_stack[1];
44   mv_cost_params->mv_cost_type = x->mv_cost_type;
45 }
46 
init_ms_buffers(MSBuffers * ms_buffers,const MACROBLOCK * x)47 static INLINE void init_ms_buffers(MSBuffers *ms_buffers, const MACROBLOCK *x) {
48   ms_buffers->ref = &x->e_mbd.plane[0].pre[0];
49   ms_buffers->src = &x->plane[0].src;
50 
51   av1_set_ms_compound_refs(ms_buffers, NULL, NULL, 0, 0);
52 
53   ms_buffers->wsrc = x->wsrc_buf;
54   ms_buffers->obmc_mask = x->mask_buf;
55 }
56 
av1_make_default_fullpel_ms_params(FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const struct AV1_COMP * cpi,const MACROBLOCK * x,BLOCK_SIZE bsize,const MV * ref_mv,const search_site_config * search_sites)57 void av1_make_default_fullpel_ms_params(
58     FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const struct AV1_COMP *cpi,
59     const MACROBLOCK *x, BLOCK_SIZE bsize, const MV *ref_mv,
60     const search_site_config *search_sites) {
61   // High level params
62   ms_params->bsize = bsize;
63   ms_params->vfp = &cpi->fn_ptr[bsize];
64 
65   init_ms_buffers(&ms_params->ms_buffers, x);
66 
67   ms_params->search_method = cpi->sf.mv_sf.search_method;
68   ms_params->search_sites = search_sites;
69 
70   ms_params->mesh_patterns[0] = cpi->sf.mv_sf.mesh_patterns;
71   ms_params->mesh_patterns[1] = cpi->sf.mv_sf.intrabc_mesh_patterns;
72   ms_params->force_mesh_thresh = cpi->sf.mv_sf.exhaustive_searches_thresh;
73   ms_params->prune_mesh_search = cpi->sf.mv_sf.prune_mesh_search;
74   ms_params->run_mesh_search = 0;
75 
76   ms_params->is_intra_mode = 0;
77 
78   ms_params->fast_obmc_search = cpi->sf.mv_sf.obmc_full_pixel_search_level;
79 
80   ms_params->mv_limits = x->mv_limits;
81   av1_set_mv_search_range(&ms_params->mv_limits, ref_mv);
82 
83   // Mvcost params
84   init_mv_cost_params(&ms_params->mv_cost_params, x, ref_mv);
85 }
86 
av1_make_default_subpel_ms_params(SUBPEL_MOTION_SEARCH_PARAMS * ms_params,const struct AV1_COMP * cpi,const MACROBLOCK * x,BLOCK_SIZE bsize,const MV * ref_mv,const int * cost_list)87 void av1_make_default_subpel_ms_params(SUBPEL_MOTION_SEARCH_PARAMS *ms_params,
88                                        const struct AV1_COMP *cpi,
89                                        const MACROBLOCK *x, BLOCK_SIZE bsize,
90                                        const MV *ref_mv, const int *cost_list) {
91   const AV1_COMMON *cm = &cpi->common;
92   // High level params
93   ms_params->allow_hp = cm->features.allow_high_precision_mv;
94   ms_params->forced_stop = cpi->sf.mv_sf.subpel_force_stop;
95   ms_params->iters_per_step = cpi->sf.mv_sf.subpel_iters_per_step;
96   ms_params->cost_list = cond_cost_list_const(cpi, cost_list);
97 
98   av1_set_subpel_mv_search_range(&ms_params->mv_limits, &x->mv_limits, ref_mv);
99 
100   // Mvcost params
101   init_mv_cost_params(&ms_params->mv_cost_params, x, ref_mv);
102 
103   // Subpel variance params
104   ms_params->var_params.vfp = &cpi->fn_ptr[bsize];
105   ms_params->var_params.subpel_search_type =
106       cpi->sf.mv_sf.use_accurate_subpel_search;
107   ms_params->var_params.w = block_size_wide[bsize];
108   ms_params->var_params.h = block_size_high[bsize];
109 
110   // Ref and src buffers
111   MSBuffers *ms_buffers = &ms_params->var_params.ms_buffers;
112   init_ms_buffers(ms_buffers, x);
113 }
114 
get_offset_from_fullmv(const FULLPEL_MV * mv,int stride)115 static INLINE int get_offset_from_fullmv(const FULLPEL_MV *mv, int stride) {
116   return mv->row * stride + mv->col;
117 }
118 
get_buf_from_fullmv(const struct buf_2d * buf,const FULLPEL_MV * mv)119 static INLINE const uint8_t *get_buf_from_fullmv(const struct buf_2d *buf,
120                                                  const FULLPEL_MV *mv) {
121   return &buf->buf[get_offset_from_fullmv(mv, buf->stride)];
122 }
123 
av1_set_mv_search_range(FullMvLimits * mv_limits,const MV * mv)124 void av1_set_mv_search_range(FullMvLimits *mv_limits, const MV *mv) {
125   int col_min =
126       GET_MV_RAWPEL(mv->col) - MAX_FULL_PEL_VAL + (mv->col & 7 ? 1 : 0);
127   int row_min =
128       GET_MV_RAWPEL(mv->row) - MAX_FULL_PEL_VAL + (mv->row & 7 ? 1 : 0);
129   int col_max = GET_MV_RAWPEL(mv->col) + MAX_FULL_PEL_VAL;
130   int row_max = GET_MV_RAWPEL(mv->row) + MAX_FULL_PEL_VAL;
131 
132   col_min = AOMMAX(col_min, GET_MV_RAWPEL(MV_LOW) + 1);
133   row_min = AOMMAX(row_min, GET_MV_RAWPEL(MV_LOW) + 1);
134   col_max = AOMMIN(col_max, GET_MV_RAWPEL(MV_UPP) - 1);
135   row_max = AOMMIN(row_max, GET_MV_RAWPEL(MV_UPP) - 1);
136 
137   // Get intersection of UMV window and valid MV window to reduce # of checks
138   // in diamond search.
139   if (mv_limits->col_min < col_min) mv_limits->col_min = col_min;
140   if (mv_limits->col_max > col_max) mv_limits->col_max = col_max;
141   if (mv_limits->row_min < row_min) mv_limits->row_min = row_min;
142   if (mv_limits->row_max > row_max) mv_limits->row_max = row_max;
143 }
144 
av1_init_search_range(int size)145 int av1_init_search_range(int size) {
146   int sr = 0;
147   // Minimum search size no matter what the passed in value.
148   size = AOMMAX(16, size);
149 
150   while ((size << sr) < MAX_FULL_PEL_VAL) sr++;
151 
152   sr = AOMMIN(sr, MAX_MVSEARCH_STEPS - 2);
153   return sr;
154 }
155 
156 // ============================================================================
157 //  Cost of motion vectors
158 // ============================================================================
159 // TODO(any): Adaptively adjust the regularization strength based on image size
160 // and motion activity instead of using hard-coded values. It seems like we
161 // roughly half the lambda for each increase in resolution
162 // These are multiplier used to perform regularization in motion compensation
163 // when x->mv_cost_type is set to MV_COST_L1.
164 // LOWRES
165 #define SSE_LAMBDA_LOWRES 2   // Used by mv_cost_err_fn
166 #define SAD_LAMBDA_LOWRES 32  // Used by mvsad_err_cost during full pixel search
167 // MIDRES
168 #define SSE_LAMBDA_MIDRES 0   // Used by mv_cost_err_fn
169 #define SAD_LAMBDA_MIDRES 15  // Used by mvsad_err_cost during full pixel search
170 // HDRES
171 #define SSE_LAMBDA_HDRES 1  // Used by mv_cost_err_fn
172 #define SAD_LAMBDA_HDRES 8  // Used by mvsad_err_cost during full pixel search
173 
174 // Returns the rate of encoding the current motion vector based on the
175 // joint_cost and comp_cost. joint_costs covers the cost of transmitting
176 // JOINT_MV, and comp_cost covers the cost of transmitting the actual motion
177 // vector.
mv_cost(const MV * mv,const int * joint_cost,const int * const comp_cost[2])178 static INLINE int mv_cost(const MV *mv, const int *joint_cost,
179                           const int *const comp_cost[2]) {
180   return joint_cost[av1_get_mv_joint(mv)] + comp_cost[0][mv->row] +
181          comp_cost[1][mv->col];
182 }
183 
184 #define CONVERT_TO_CONST_MVCOST(ptr) ((const int *const *)(ptr))
185 // Returns the cost of encoding the motion vector diff := *mv - *ref. The cost
186 // is defined as the rate required to encode diff * weight, rounded to the
187 // nearest 2 ** 7.
188 // This is NOT used during motion compensation.
av1_mv_bit_cost(const MV * mv,const MV * ref_mv,const int * mvjcost,int * mvcost[2],int weight)189 int av1_mv_bit_cost(const MV *mv, const MV *ref_mv, const int *mvjcost,
190                     int *mvcost[2], int weight) {
191   const MV diff = { mv->row - ref_mv->row, mv->col - ref_mv->col };
192   return ROUND_POWER_OF_TWO(
193       mv_cost(&diff, mvjcost, CONVERT_TO_CONST_MVCOST(mvcost)) * weight, 7);
194 }
195 
196 // Returns the cost of using the current mv during the motion search. This is
197 // used when var is used as the error metric.
198 #define PIXEL_TRANSFORM_ERROR_SCALE 4
mv_err_cost(const MV * mv,const MV * ref_mv,const int * mvjcost,const int * const mvcost[2],int error_per_bit,MV_COST_TYPE mv_cost_type)199 static INLINE int mv_err_cost(const MV *mv, const MV *ref_mv,
200                               const int *mvjcost, const int *const mvcost[2],
201                               int error_per_bit, MV_COST_TYPE mv_cost_type) {
202   const MV diff = { mv->row - ref_mv->row, mv->col - ref_mv->col };
203   const MV abs_diff = { abs(diff.row), abs(diff.col) };
204 
205   switch (mv_cost_type) {
206     case MV_COST_ENTROPY:
207       if (mvcost) {
208         return (int)ROUND_POWER_OF_TWO_64(
209             (int64_t)mv_cost(&diff, mvjcost, mvcost) * error_per_bit,
210             RDDIV_BITS + AV1_PROB_COST_SHIFT - RD_EPB_SHIFT +
211                 PIXEL_TRANSFORM_ERROR_SCALE);
212       }
213       return 0;
214     case MV_COST_L1_LOWRES:
215       return (SSE_LAMBDA_LOWRES * (abs_diff.row + abs_diff.col)) >> 3;
216     case MV_COST_L1_MIDRES:
217       return (SSE_LAMBDA_MIDRES * (abs_diff.row + abs_diff.col)) >> 3;
218     case MV_COST_L1_HDRES:
219       return (SSE_LAMBDA_HDRES * (abs_diff.row + abs_diff.col)) >> 3;
220     case MV_COST_NONE: return 0;
221     default: assert(0 && "Invalid rd_cost_type"); return 0;
222   }
223 }
224 
mv_err_cost_(const MV * mv,const MV_COST_PARAMS * mv_cost_params)225 static INLINE int mv_err_cost_(const MV *mv,
226                                const MV_COST_PARAMS *mv_cost_params) {
227   return mv_err_cost(mv, mv_cost_params->ref_mv, mv_cost_params->mvjcost,
228                      mv_cost_params->mvcost, mv_cost_params->error_per_bit,
229                      mv_cost_params->mv_cost_type);
230 }
231 
232 // Returns the cost of using the current mv during the motion search. This is
233 // only used during full pixel motion search when sad is used as the error
234 // metric
mvsad_err_cost(const FULLPEL_MV * mv,const FULLPEL_MV * ref_mv,const int * mvjcost,const int * const mvcost[2],int sad_per_bit,MV_COST_TYPE mv_cost_type)235 static INLINE int mvsad_err_cost(const FULLPEL_MV *mv, const FULLPEL_MV *ref_mv,
236                                  const int *mvjcost, const int *const mvcost[2],
237                                  int sad_per_bit, MV_COST_TYPE mv_cost_type) {
238   const MV diff = { GET_MV_SUBPEL(mv->row - ref_mv->row),
239                     GET_MV_SUBPEL(mv->col - ref_mv->col) };
240 
241   switch (mv_cost_type) {
242     case MV_COST_ENTROPY:
243       return ROUND_POWER_OF_TWO(
244           (unsigned)mv_cost(&diff, mvjcost, CONVERT_TO_CONST_MVCOST(mvcost)) *
245               sad_per_bit,
246           AV1_PROB_COST_SHIFT);
247     case MV_COST_L1_LOWRES:
248       return (SAD_LAMBDA_LOWRES * (abs(diff.row) + abs(diff.col))) >> 3;
249     case MV_COST_L1_MIDRES:
250       return (SAD_LAMBDA_MIDRES * (abs(diff.row) + abs(diff.col))) >> 3;
251     case MV_COST_L1_HDRES:
252       return (SAD_LAMBDA_HDRES * (abs(diff.row) + abs(diff.col))) >> 3;
253     case MV_COST_NONE: return 0;
254     default: assert(0 && "Invalid rd_cost_type"); return 0;
255   }
256 }
257 
mvsad_err_cost_(const FULLPEL_MV * mv,const MV_COST_PARAMS * mv_cost_params)258 static INLINE int mvsad_err_cost_(const FULLPEL_MV *mv,
259                                   const MV_COST_PARAMS *mv_cost_params) {
260   return mvsad_err_cost(mv, &mv_cost_params->full_ref_mv,
261                         mv_cost_params->mvjcost, mv_cost_params->mvcost,
262                         mv_cost_params->sad_per_bit,
263                         mv_cost_params->mv_cost_type);
264 }
265 
266 // =============================================================================
267 //  Fullpixel Motion Search: Translational
268 // =============================================================================
269 #define MAX_PATTERN_SCALES 11
270 #define MAX_PATTERN_CANDIDATES 8  // max number of candidates per scale
271 #define PATTERN_CANDIDATES_REF 3  // number of refinement candidates
272 
av1_init_dsmotion_compensation(search_site_config * cfg,int stride)273 void av1_init_dsmotion_compensation(search_site_config *cfg, int stride) {
274   int ss_count = 0;
275   int stage_index = MAX_MVSEARCH_STEPS - 1;
276 
277   cfg->ss[stage_index][0].mv.col = cfg->ss[stage_index][0].mv.row = 0;
278   cfg->ss[stage_index][0].offset = 0;
279   cfg->stride = stride;
280 
281   for (int radius = MAX_FIRST_STEP; radius > 0; radius /= 2) {
282     int num_search_pts = 8;
283 
284     const FULLPEL_MV ss_mvs[13] = {
285       { 0, 0 },           { -radius, 0 },      { radius, 0 },
286       { 0, -radius },     { 0, radius },       { -radius, -radius },
287       { radius, radius }, { -radius, radius }, { radius, -radius },
288     };
289 
290     int i;
291     for (i = 0; i <= num_search_pts; ++i) {
292       search_site *const ss = &cfg->ss[stage_index][i];
293       ss->mv = ss_mvs[i];
294       ss->offset = get_offset_from_fullmv(&ss->mv, stride);
295     }
296     cfg->searches_per_step[stage_index] = num_search_pts;
297     cfg->radius[stage_index] = radius;
298     --stage_index;
299     ++ss_count;
300   }
301   cfg->ss_count = ss_count;
302 }
303 
av1_init_motion_fpf(search_site_config * cfg,int stride)304 void av1_init_motion_fpf(search_site_config *cfg, int stride) {
305   int ss_count = 0;
306   int stage_index = MAX_MVSEARCH_STEPS - 1;
307 
308   cfg->ss[stage_index][0].mv.col = cfg->ss[stage_index][0].mv.row = 0;
309   cfg->ss[stage_index][0].offset = 0;
310   cfg->stride = stride;
311 
312   for (int radius = MAX_FIRST_STEP; radius > 0; radius /= 2) {
313     // Generate offsets for 8 search sites per step.
314     int tan_radius = AOMMAX((int)(0.41 * radius), 1);
315     int num_search_pts = 12;
316     if (radius == 1) num_search_pts = 8;
317 
318     const FULLPEL_MV ss_mvs[13] = {
319       { 0, 0 },
320       { -radius, 0 },
321       { radius, 0 },
322       { 0, -radius },
323       { 0, radius },
324       { -radius, -tan_radius },
325       { radius, tan_radius },
326       { -tan_radius, radius },
327       { tan_radius, -radius },
328       { -radius, tan_radius },
329       { radius, -tan_radius },
330       { tan_radius, radius },
331       { -tan_radius, -radius },
332     };
333 
334     int i;
335     for (i = 0; i <= num_search_pts; ++i) {
336       search_site *const ss = &cfg->ss[stage_index][i];
337       ss->mv = ss_mvs[i];
338       ss->offset = get_offset_from_fullmv(&ss->mv, stride);
339     }
340     cfg->searches_per_step[stage_index] = num_search_pts;
341     cfg->radius[stage_index] = radius;
342     --stage_index;
343     ++ss_count;
344   }
345   cfg->ss_count = ss_count;
346 }
347 
av1_init3smotion_compensation(search_site_config * cfg,int stride)348 void av1_init3smotion_compensation(search_site_config *cfg, int stride) {
349   int ss_count = 0;
350   int stage_index = 0;
351   cfg->stride = stride;
352   int radius = 1;
353   for (stage_index = 0; stage_index < 15; ++stage_index) {
354     int tan_radius = AOMMAX((int)(0.41 * radius), 1);
355     int num_search_pts = 12;
356     if (radius <= 5) {
357       tan_radius = radius;
358       num_search_pts = 8;
359     }
360     const FULLPEL_MV ss_mvs[13] = {
361       { 0, 0 },
362       { -radius, 0 },
363       { radius, 0 },
364       { 0, -radius },
365       { 0, radius },
366       { -radius, -tan_radius },
367       { radius, tan_radius },
368       { -tan_radius, radius },
369       { tan_radius, -radius },
370       { -radius, tan_radius },
371       { radius, -tan_radius },
372       { tan_radius, radius },
373       { -tan_radius, -radius },
374     };
375 
376     for (int i = 0; i <= num_search_pts; ++i) {
377       search_site *const ss = &cfg->ss[stage_index][i];
378       ss->mv = ss_mvs[i];
379       ss->offset = get_offset_from_fullmv(&ss->mv, stride);
380     }
381     cfg->searches_per_step[stage_index] = num_search_pts;
382     cfg->radius[stage_index] = radius;
383     ++ss_count;
384     if (stage_index < 12)
385       radius = (int)AOMMAX((radius * 1.5 + 0.5), radius + 1);
386   }
387   cfg->ss_count = ss_count;
388 }
389 
390 // Checks whether the mv is within range of the mv_limits
check_bounds(const FullMvLimits * mv_limits,int row,int col,int range)391 static INLINE int check_bounds(const FullMvLimits *mv_limits, int row, int col,
392                                int range) {
393   return ((row - range) >= mv_limits->row_min) &
394          ((row + range) <= mv_limits->row_max) &
395          ((col - range) >= mv_limits->col_min) &
396          ((col + range) <= mv_limits->col_max);
397 }
398 
get_mvpred_var_cost(const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const FULLPEL_MV * this_mv)399 static INLINE int get_mvpred_var_cost(
400     const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV *this_mv) {
401   const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
402   const MV sub_this_mv = get_mv_from_fullmv(this_mv);
403   const struct buf_2d *const src = ms_params->ms_buffers.src;
404   const struct buf_2d *const ref = ms_params->ms_buffers.ref;
405   const uint8_t *src_buf = src->buf;
406   const int src_stride = src->stride;
407   const int ref_stride = ref->stride;
408 
409   unsigned unused;
410   int bestsme;
411 
412   bestsme = vfp->vf(src_buf, src_stride, get_buf_from_fullmv(ref, this_mv),
413                     ref_stride, &unused);
414 
415   bestsme += mv_err_cost_(&sub_this_mv, &ms_params->mv_cost_params);
416 
417   return bestsme;
418 }
419 
get_mvpred_sad(const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const struct buf_2d * const src,const uint8_t * const ref_address,const int ref_stride)420 static INLINE int get_mvpred_sad(const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
421                                  const struct buf_2d *const src,
422                                  const uint8_t *const ref_address,
423                                  const int ref_stride) {
424   const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
425   const uint8_t *src_buf = src->buf;
426   const int src_stride = src->stride;
427 
428   return vfp->sdf(src_buf, src_stride, ref_address, ref_stride);
429 }
430 
get_mvpred_compound_var_cost(const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const FULLPEL_MV * this_mv)431 static INLINE int get_mvpred_compound_var_cost(
432     const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV *this_mv) {
433   const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
434   const struct buf_2d *const src = ms_params->ms_buffers.src;
435   const struct buf_2d *const ref = ms_params->ms_buffers.ref;
436   const uint8_t *src_buf = src->buf;
437   const int src_stride = src->stride;
438   const int ref_stride = ref->stride;
439 
440   const uint8_t *mask = ms_params->ms_buffers.mask;
441   const uint8_t *second_pred = ms_params->ms_buffers.second_pred;
442   const int mask_stride = ms_params->ms_buffers.mask_stride;
443   const int invert_mask = ms_params->ms_buffers.inv_mask;
444   unsigned unused;
445   int bestsme;
446 
447   if (mask) {
448     bestsme = vfp->msvf(src_buf, src_stride, 0, 0,
449                         get_buf_from_fullmv(ref, this_mv), ref_stride,
450                         second_pred, mask, mask_stride, invert_mask, &unused);
451   } else if (second_pred) {
452     bestsme = vfp->svaf(get_buf_from_fullmv(ref, this_mv), ref_stride, 0, 0,
453                         src_buf, src_stride, &unused, second_pred);
454   } else {
455     bestsme = vfp->vf(src_buf, src_stride, get_buf_from_fullmv(ref, this_mv),
456                       ref_stride, &unused);
457   }
458 
459   const MV sub_this_mv = get_mv_from_fullmv(this_mv);
460   bestsme += mv_err_cost_(&sub_this_mv, &ms_params->mv_cost_params);
461 
462   return bestsme;
463 }
464 
get_mvpred_compound_sad(const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const struct buf_2d * const src,const uint8_t * const ref_address,const int ref_stride)465 static INLINE int get_mvpred_compound_sad(
466     const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
467     const struct buf_2d *const src, const uint8_t *const ref_address,
468     const int ref_stride) {
469   const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
470   const uint8_t *src_buf = src->buf;
471   const int src_stride = src->stride;
472 
473   const uint8_t *mask = ms_params->ms_buffers.mask;
474   const uint8_t *second_pred = ms_params->ms_buffers.second_pred;
475   const int mask_stride = ms_params->ms_buffers.mask_stride;
476   const int invert_mask = ms_params->ms_buffers.inv_mask;
477 
478   if (mask) {
479     return vfp->msdf(src_buf, src_stride, ref_address, ref_stride, second_pred,
480                      mask, mask_stride, invert_mask);
481   } else if (second_pred) {
482     return vfp->sdaf(src_buf, src_stride, ref_address, ref_stride, second_pred);
483   } else {
484     return vfp->sdf(src_buf, src_stride, ref_address, ref_stride);
485   }
486 }
487 
488 // Calculates and returns a sad+mvcost list around an integer best pel during
489 // fullpixel motion search. The resulting list can be used to speed up subpel
490 // motion search later.
491 #define USE_SAD_COSTLIST 1
492 
493 // calc_int_cost_list uses var to populate the costlist, which is more accurate
494 // than sad but slightly slower.
calc_int_cost_list(const FULLPEL_MV best_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,int * cost_list)495 static AOM_FORCE_INLINE void calc_int_cost_list(
496     const FULLPEL_MV best_mv, const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
497     int *cost_list) {
498   static const FULLPEL_MV neighbors[4] = {
499     { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }
500   };
501   const int br = best_mv.row;
502   const int bc = best_mv.col;
503 
504   cost_list[0] = get_mvpred_var_cost(ms_params, &best_mv);
505 
506   if (check_bounds(&ms_params->mv_limits, br, bc, 1)) {
507     for (int i = 0; i < 4; i++) {
508       const FULLPEL_MV neighbor_mv = { br + neighbors[i].row,
509                                        bc + neighbors[i].col };
510       cost_list[i + 1] = get_mvpred_var_cost(ms_params, &neighbor_mv);
511     }
512   } else {
513     for (int i = 0; i < 4; i++) {
514       const FULLPEL_MV neighbor_mv = { br + neighbors[i].row,
515                                        bc + neighbors[i].col };
516       if (!av1_is_fullmv_in_range(&ms_params->mv_limits, neighbor_mv)) {
517         cost_list[i + 1] = INT_MAX;
518       } else {
519         cost_list[i + 1] = get_mvpred_var_cost(ms_params, &neighbor_mv);
520       }
521     }
522   }
523 }
524 
525 // calc_int_sad_list uses sad to populate the costlist, which is less accurate
526 // than var but faster.
calc_int_sad_list(const FULLPEL_MV best_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,int * cost_list,int costlist_has_sad)527 static AOM_FORCE_INLINE void calc_int_sad_list(
528     const FULLPEL_MV best_mv, const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
529     int *cost_list, int costlist_has_sad) {
530   static const FULLPEL_MV neighbors[4] = {
531     { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }
532   };
533   const struct buf_2d *const src = ms_params->ms_buffers.src;
534   const struct buf_2d *const ref = ms_params->ms_buffers.ref;
535   const int ref_stride = ref->stride;
536   const int br = best_mv.row;
537   const int bc = best_mv.col;
538 
539   assert(av1_is_fullmv_in_range(&ms_params->mv_limits, best_mv));
540 
541   // Refresh the costlist it does not contain valid sad
542   if (!costlist_has_sad) {
543     cost_list[0] = get_mvpred_sad(
544         ms_params, src, get_buf_from_fullmv(ref, &best_mv), ref_stride);
545 
546     if (check_bounds(&ms_params->mv_limits, br, bc, 1)) {
547       for (int i = 0; i < 4; i++) {
548         const FULLPEL_MV this_mv = { br + neighbors[i].row,
549                                      bc + neighbors[i].col };
550         cost_list[i + 1] = get_mvpred_sad(
551             ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
552       }
553     } else {
554       for (int i = 0; i < 4; i++) {
555         const FULLPEL_MV this_mv = { br + neighbors[i].row,
556                                      bc + neighbors[i].col };
557         if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) {
558           cost_list[i + 1] = INT_MAX;
559         } else {
560           cost_list[i + 1] = get_mvpred_sad(
561               ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
562         }
563       }
564     }
565   }
566 
567   const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
568   cost_list[0] += mvsad_err_cost_(&best_mv, mv_cost_params);
569 
570   for (int idx = 0; idx < 4; idx++) {
571     if (cost_list[idx + 1] != INT_MAX) {
572       const FULLPEL_MV this_mv = { br + neighbors[idx].row,
573                                    bc + neighbors[idx].col };
574       cost_list[idx + 1] += mvsad_err_cost_(&this_mv, mv_cost_params);
575     }
576   }
577 }
578 
579 #define CHECK_BETTER                                                      \
580   if (thissad < bestsad) {                                                \
581     int tmp_thissad = thissad;                                            \
582     if (use_mvcost) thissad += mvsad_err_cost_(&this_mv, mv_cost_params); \
583     if (thissad < bestsad) {                                              \
584       raw_bestsad = tmp_thissad;                                          \
585       bestsad = thissad;                                                  \
586       best_site = i;                                                      \
587     }                                                                     \
588   }
589 
590 // Generic pattern search function that searches over multiple scales.
591 // Each scale can have a different number of candidates and shape of
592 // candidates as indicated in the num_candidates and candidates arrays
593 // passed into this function
pattern_search(FULLPEL_MV start_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const int search_param,const int do_init_search,const int num_candidates[MAX_PATTERN_SCALES],const MV candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES],int * cost_list,FULLPEL_MV * best_mv)594 static int pattern_search(
595     FULLPEL_MV start_mv, const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
596     const int search_param, const int do_init_search,
597     const int num_candidates[MAX_PATTERN_SCALES],
598     const MV candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES],
599     int *cost_list, FULLPEL_MV *best_mv) {
600   static const int search_param_to_steps[MAX_MVSEARCH_STEPS] = {
601     10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
602   };
603   int i, s, t;
604 
605   const struct buf_2d *const src = ms_params->ms_buffers.src;
606   const struct buf_2d *const ref = ms_params->ms_buffers.ref;
607   const int ref_stride = ref->stride;
608   const int last_is_4 = num_candidates[0] == 4;
609   int br, bc;
610   int bestsad = INT_MAX, raw_bestsad = INT_MAX;
611   int thissad;
612   int k = -1;
613   const int use_mvcost = ms_params->mv_cost_params.mv_cost_type != MV_COST_NONE;
614   const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
615   assert(search_param < MAX_MVSEARCH_STEPS);
616   int best_init_s = search_param_to_steps[search_param];
617   // adjust ref_mv to make sure it is within MV range
618   clamp_fullmv(&start_mv, &ms_params->mv_limits);
619   br = start_mv.row;
620   bc = start_mv.col;
621   if (cost_list != NULL) {
622     cost_list[0] = cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] =
623         INT_MAX;
624   }
625   int costlist_has_sad = 0;
626 
627   // Work out the start point for the search
628   raw_bestsad = get_mvpred_sad(ms_params, src,
629                                get_buf_from_fullmv(ref, &start_mv), ref_stride);
630   bestsad = raw_bestsad + mvsad_err_cost_(&start_mv, mv_cost_params);
631 
632   // Search all possible scales up to the search param around the center point
633   // pick the scale of the point that is best as the starting scale of
634   // further steps around it.
635   if (do_init_search) {
636     s = best_init_s;
637     best_init_s = -1;
638     for (t = 0; t <= s; ++t) {
639       int best_site = -1;
640       if (check_bounds(&ms_params->mv_limits, br, bc, 1 << t)) {
641         for (i = 0; i < num_candidates[t]; i++) {
642           const FULLPEL_MV this_mv = { br + candidates[t][i].row,
643                                        bc + candidates[t][i].col };
644           thissad = get_mvpred_sad(
645               ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
646           CHECK_BETTER
647         }
648       } else {
649         for (i = 0; i < num_candidates[t]; i++) {
650           const FULLPEL_MV this_mv = { br + candidates[t][i].row,
651                                        bc + candidates[t][i].col };
652           if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) continue;
653           thissad = get_mvpred_sad(
654               ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
655           CHECK_BETTER
656         }
657       }
658       if (best_site == -1) {
659         continue;
660       } else {
661         best_init_s = t;
662         k = best_site;
663       }
664     }
665     if (best_init_s != -1) {
666       br += candidates[best_init_s][k].row;
667       bc += candidates[best_init_s][k].col;
668     }
669   }
670 
671   // If the center point is still the best, just skip this and move to
672   // the refinement step.
673   if (best_init_s != -1) {
674     const int last_s = (last_is_4 && cost_list != NULL);
675     int best_site = -1;
676     s = best_init_s;
677 
678     for (; s >= last_s; s--) {
679       // No need to search all points the 1st time if initial search was used
680       if (!do_init_search || s != best_init_s) {
681         if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) {
682           for (i = 0; i < num_candidates[s]; i++) {
683             const FULLPEL_MV this_mv = { br + candidates[s][i].row,
684                                          bc + candidates[s][i].col };
685             thissad = get_mvpred_sad(
686                 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
687             CHECK_BETTER
688           }
689         } else {
690           for (i = 0; i < num_candidates[s]; i++) {
691             const FULLPEL_MV this_mv = { br + candidates[s][i].row,
692                                          bc + candidates[s][i].col };
693             if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv))
694               continue;
695             thissad = get_mvpred_sad(
696                 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
697             CHECK_BETTER
698           }
699         }
700 
701         if (best_site == -1) {
702           continue;
703         } else {
704           br += candidates[s][best_site].row;
705           bc += candidates[s][best_site].col;
706           k = best_site;
707         }
708       }
709 
710       do {
711         int next_chkpts_indices[PATTERN_CANDIDATES_REF];
712         best_site = -1;
713         next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
714         next_chkpts_indices[1] = k;
715         next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
716 
717         if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) {
718           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
719             const FULLPEL_MV this_mv = {
720               br + candidates[s][next_chkpts_indices[i]].row,
721               bc + candidates[s][next_chkpts_indices[i]].col
722             };
723             thissad = get_mvpred_sad(
724                 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
725             CHECK_BETTER
726           }
727         } else {
728           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
729             const FULLPEL_MV this_mv = {
730               br + candidates[s][next_chkpts_indices[i]].row,
731               bc + candidates[s][next_chkpts_indices[i]].col
732             };
733             if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv))
734               continue;
735             thissad = get_mvpred_sad(
736                 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
737             CHECK_BETTER
738           }
739         }
740 
741         if (best_site != -1) {
742           k = next_chkpts_indices[best_site];
743           br += candidates[s][k].row;
744           bc += candidates[s][k].col;
745         }
746       } while (best_site != -1);
747     }
748 
749     // Note: If we enter the if below, then cost_list must be non-NULL.
750     if (s == 0) {
751       cost_list[0] = raw_bestsad;
752       costlist_has_sad = 1;
753       if (!do_init_search || s != best_init_s) {
754         if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) {
755           for (i = 0; i < num_candidates[s]; i++) {
756             const FULLPEL_MV this_mv = { br + candidates[s][i].row,
757                                          bc + candidates[s][i].col };
758             cost_list[i + 1] = thissad = get_mvpred_sad(
759                 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
760             CHECK_BETTER
761           }
762         } else {
763           for (i = 0; i < num_candidates[s]; i++) {
764             const FULLPEL_MV this_mv = { br + candidates[s][i].row,
765                                          bc + candidates[s][i].col };
766             if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv))
767               continue;
768             cost_list[i + 1] = thissad = get_mvpred_sad(
769                 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
770             CHECK_BETTER
771           }
772         }
773 
774         if (best_site != -1) {
775           br += candidates[s][best_site].row;
776           bc += candidates[s][best_site].col;
777           k = best_site;
778         }
779       }
780       while (best_site != -1) {
781         int next_chkpts_indices[PATTERN_CANDIDATES_REF];
782         best_site = -1;
783         next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
784         next_chkpts_indices[1] = k;
785         next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
786         cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] = INT_MAX;
787         cost_list[((k + 2) % 4) + 1] = cost_list[0];
788         cost_list[0] = raw_bestsad;
789 
790         if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) {
791           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
792             const FULLPEL_MV this_mv = {
793               br + candidates[s][next_chkpts_indices[i]].row,
794               bc + candidates[s][next_chkpts_indices[i]].col
795             };
796             cost_list[next_chkpts_indices[i] + 1] = thissad = get_mvpred_sad(
797                 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
798             CHECK_BETTER
799           }
800         } else {
801           for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
802             const FULLPEL_MV this_mv = {
803               br + candidates[s][next_chkpts_indices[i]].row,
804               bc + candidates[s][next_chkpts_indices[i]].col
805             };
806             if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) {
807               cost_list[next_chkpts_indices[i] + 1] = INT_MAX;
808               continue;
809             }
810             cost_list[next_chkpts_indices[i] + 1] = thissad = get_mvpred_sad(
811                 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
812             CHECK_BETTER
813           }
814         }
815 
816         if (best_site != -1) {
817           k = next_chkpts_indices[best_site];
818           br += candidates[s][k].row;
819           bc += candidates[s][k].col;
820         }
821       }
822     }
823   }
824 
825   best_mv->row = br;
826   best_mv->col = bc;
827 
828   // Returns the one-away integer pel cost/sad around the best as follows:
829   // cost_list[0]: cost/sad at the best integer pel
830   // cost_list[1]: cost/sad at delta {0, -1} (left)   from the best integer pel
831   // cost_list[2]: cost/sad at delta { 1, 0} (bottom) from the best integer pel
832   // cost_list[3]: cost/sad at delta { 0, 1} (right)  from the best integer pel
833   // cost_list[4]: cost/sad at delta {-1, 0} (top)    from the best integer pel
834   if (cost_list) {
835     if (USE_SAD_COSTLIST) {
836       calc_int_sad_list(*best_mv, ms_params, cost_list, costlist_has_sad);
837     } else {
838       calc_int_cost_list(*best_mv, ms_params, cost_list);
839     }
840   }
841   best_mv->row = br;
842   best_mv->col = bc;
843 
844   const int var_cost = get_mvpred_var_cost(ms_params, best_mv);
845   return var_cost;
846 }
847 #undef CHECK_BETTER
848 
849 // For the following foo_search, the input arguments are:
850 // x: The struct used to hold a bunch of random configs.
851 // start_mv: where we are starting our motion search
852 // search_param: how many steps to skip in our motion search. For example,
853 //   a value 3 suggests that 3 search steps have already taken place prior to
854 //   this function call, so we jump directly to step 4 of the search process
855 // sad_per_bit: a multiplier used to convert rate to sad cost
856 // do_init_search: if on, do an initial search of all possible scales around the
857 //   start_mv, and then pick the best scale.
858 // cond_list: used to hold the cost around the best full mv so we can use it to
859 //   speed up subpel search later.
860 // vfp: a function pointer to the simd function so we can compute the cost
861 //   efficiently
862 // ref_mv: the reference mv used to compute the mv cost
hex_search(const FULLPEL_MV start_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const int search_param,const int do_init_search,int * cost_list,FULLPEL_MV * best_mv)863 static int hex_search(const FULLPEL_MV start_mv,
864                       const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
865                       const int search_param, const int do_init_search,
866                       int *cost_list, FULLPEL_MV *best_mv) {
867   // First scale has 8-closest points, the rest have 6 points in hex shape
868   // at increasing scales
869   static const int hex_num_candidates[MAX_PATTERN_SCALES] = { 8, 6, 6, 6, 6, 6,
870                                                               6, 6, 6, 6, 6 };
871   // Note that the largest candidate step at each scale is 2^scale
872   /* clang-format off */
873   static const MV hex_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
874     { { -1, -1 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 },
875       { -1, 0 } },
876     { { -1, -2 }, { 1, -2 }, { 2, 0 }, { 1, 2 }, { -1, 2 }, { -2, 0 } },
877     { { -2, -4 }, { 2, -4 }, { 4, 0 }, { 2, 4 }, { -2, 4 }, { -4, 0 } },
878     { { -4, -8 }, { 4, -8 }, { 8, 0 }, { 4, 8 }, { -4, 8 }, { -8, 0 } },
879     { { -8, -16 }, { 8, -16 }, { 16, 0 }, { 8, 16 }, { -8, 16 }, { -16, 0 } },
880     { { -16, -32 }, { 16, -32 }, { 32, 0 }, { 16, 32 }, { -16, 32 },
881       { -32, 0 } },
882     { { -32, -64 }, { 32, -64 }, { 64, 0 }, { 32, 64 }, { -32, 64 },
883       { -64, 0 } },
884     { { -64, -128 }, { 64, -128 }, { 128, 0 }, { 64, 128 }, { -64, 128 },
885       { -128, 0 } },
886     { { -128, -256 }, { 128, -256 }, { 256, 0 }, { 128, 256 }, { -128, 256 },
887       { -256, 0 } },
888     { { -256, -512 }, { 256, -512 }, { 512, 0 }, { 256, 512 }, { -256, 512 },
889       { -512, 0 } },
890     { { -512, -1024 }, { 512, -1024 }, { 1024, 0 }, { 512, 1024 },
891       { -512, 1024 }, { -1024, 0 } },
892   };
893   /* clang-format on */
894   return pattern_search(start_mv, ms_params, search_param, do_init_search,
895                         hex_num_candidates, hex_candidates, cost_list, best_mv);
896 }
897 
bigdia_search(const FULLPEL_MV start_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const int search_param,const int do_init_search,int * cost_list,FULLPEL_MV * best_mv)898 static int bigdia_search(const FULLPEL_MV start_mv,
899                          const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
900                          const int search_param, const int do_init_search,
901                          int *cost_list, FULLPEL_MV *best_mv) {
902   // First scale has 4-closest points, the rest have 8 points in diamond
903   // shape at increasing scales
904   static const int bigdia_num_candidates[MAX_PATTERN_SCALES] = {
905     4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
906   };
907   // Note that the largest candidate step at each scale is 2^scale
908   /* clang-format off */
909   static const MV
910       bigdia_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
911         { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 } },
912         { { -1, -1 }, { 0, -2 }, { 1, -1 }, { 2, 0 }, { 1, 1 }, { 0, 2 },
913           { -1, 1 }, { -2, 0 } },
914         { { -2, -2 }, { 0, -4 }, { 2, -2 }, { 4, 0 }, { 2, 2 }, { 0, 4 },
915           { -2, 2 }, { -4, 0 } },
916         { { -4, -4 }, { 0, -8 }, { 4, -4 }, { 8, 0 }, { 4, 4 }, { 0, 8 },
917           { -4, 4 }, { -8, 0 } },
918         { { -8, -8 }, { 0, -16 }, { 8, -8 }, { 16, 0 }, { 8, 8 }, { 0, 16 },
919           { -8, 8 }, { -16, 0 } },
920         { { -16, -16 }, { 0, -32 }, { 16, -16 }, { 32, 0 }, { 16, 16 },
921           { 0, 32 }, { -16, 16 }, { -32, 0 } },
922         { { -32, -32 }, { 0, -64 }, { 32, -32 }, { 64, 0 }, { 32, 32 },
923           { 0, 64 }, { -32, 32 }, { -64, 0 } },
924         { { -64, -64 }, { 0, -128 }, { 64, -64 }, { 128, 0 }, { 64, 64 },
925           { 0, 128 }, { -64, 64 }, { -128, 0 } },
926         { { -128, -128 }, { 0, -256 }, { 128, -128 }, { 256, 0 }, { 128, 128 },
927           { 0, 256 }, { -128, 128 }, { -256, 0 } },
928         { { -256, -256 }, { 0, -512 }, { 256, -256 }, { 512, 0 }, { 256, 256 },
929           { 0, 512 }, { -256, 256 }, { -512, 0 } },
930         { { -512, -512 }, { 0, -1024 }, { 512, -512 }, { 1024, 0 },
931           { 512, 512 }, { 0, 1024 }, { -512, 512 }, { -1024, 0 } },
932       };
933   /* clang-format on */
934   return pattern_search(start_mv, ms_params, search_param, do_init_search,
935                         bigdia_num_candidates, bigdia_candidates, cost_list,
936                         best_mv);
937 }
938 
square_search(const FULLPEL_MV start_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const int search_param,const int do_init_search,int * cost_list,FULLPEL_MV * best_mv)939 static int square_search(const FULLPEL_MV start_mv,
940                          const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
941                          const int search_param, const int do_init_search,
942                          int *cost_list, FULLPEL_MV *best_mv) {
943   // All scales have 8 closest points in square shape
944   static const int square_num_candidates[MAX_PATTERN_SCALES] = {
945     8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
946   };
947   // Note that the largest candidate step at each scale is 2^scale
948   /* clang-format off */
949   static const MV
950       square_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
951         { { -1, -1 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
952           { -1, 1 }, { -1, 0 } },
953         { { -2, -2 }, { 0, -2 }, { 2, -2 }, { 2, 0 }, { 2, 2 }, { 0, 2 },
954           { -2, 2 }, { -2, 0 } },
955         { { -4, -4 }, { 0, -4 }, { 4, -4 }, { 4, 0 }, { 4, 4 }, { 0, 4 },
956           { -4, 4 }, { -4, 0 } },
957         { { -8, -8 }, { 0, -8 }, { 8, -8 }, { 8, 0 }, { 8, 8 }, { 0, 8 },
958           { -8, 8 }, { -8, 0 } },
959         { { -16, -16 }, { 0, -16 }, { 16, -16 }, { 16, 0 }, { 16, 16 },
960           { 0, 16 }, { -16, 16 }, { -16, 0 } },
961         { { -32, -32 }, { 0, -32 }, { 32, -32 }, { 32, 0 }, { 32, 32 },
962           { 0, 32 }, { -32, 32 }, { -32, 0 } },
963         { { -64, -64 }, { 0, -64 }, { 64, -64 }, { 64, 0 }, { 64, 64 },
964           { 0, 64 }, { -64, 64 }, { -64, 0 } },
965         { { -128, -128 }, { 0, -128 }, { 128, -128 }, { 128, 0 }, { 128, 128 },
966           { 0, 128 }, { -128, 128 }, { -128, 0 } },
967         { { -256, -256 }, { 0, -256 }, { 256, -256 }, { 256, 0 }, { 256, 256 },
968           { 0, 256 }, { -256, 256 }, { -256, 0 } },
969         { { -512, -512 }, { 0, -512 }, { 512, -512 }, { 512, 0 }, { 512, 512 },
970           { 0, 512 }, { -512, 512 }, { -512, 0 } },
971         { { -1024, -1024 }, { 0, -1024 }, { 1024, -1024 }, { 1024, 0 },
972           { 1024, 1024 }, { 0, 1024 }, { -1024, 1024 }, { -1024, 0 } },
973       };
974   /* clang-format on */
975   return pattern_search(start_mv, ms_params, search_param, do_init_search,
976                         square_num_candidates, square_candidates, cost_list,
977                         best_mv);
978 }
979 
fast_hex_search(const FULLPEL_MV start_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const int search_param,const int do_init_search,int * cost_list,FULLPEL_MV * best_mv)980 static int fast_hex_search(const FULLPEL_MV start_mv,
981                            const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
982                            const int search_param, const int do_init_search,
983                            int *cost_list, FULLPEL_MV *best_mv) {
984   return hex_search(start_mv, ms_params,
985                     AOMMAX(MAX_MVSEARCH_STEPS - 2, search_param),
986                     do_init_search, cost_list, best_mv);
987 }
988 
fast_dia_search(const FULLPEL_MV start_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const int search_param,const int do_init_search,int * cost_list,FULLPEL_MV * best_mv)989 static int fast_dia_search(const FULLPEL_MV start_mv,
990                            const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
991                            const int search_param, const int do_init_search,
992                            int *cost_list, FULLPEL_MV *best_mv) {
993   return bigdia_search(start_mv, ms_params,
994                        AOMMAX(MAX_MVSEARCH_STEPS - 2, search_param),
995                        do_init_search, cost_list, best_mv);
996 }
997 
diamond_search_sad(FULLPEL_MV start_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const int search_param,int * num00,FULLPEL_MV * best_mv,FULLPEL_MV * second_best_mv)998 static int diamond_search_sad(FULLPEL_MV start_mv,
999                               const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1000                               const int search_param, int *num00,
1001                               FULLPEL_MV *best_mv, FULLPEL_MV *second_best_mv) {
1002   const struct buf_2d *const src = ms_params->ms_buffers.src;
1003   const struct buf_2d *const ref = ms_params->ms_buffers.ref;
1004 
1005   const int ref_stride = ref->stride;
1006   const uint8_t *best_address;
1007 
1008   const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
1009   const uint8_t *mask = ms_params->ms_buffers.mask;
1010   const uint8_t *second_pred = ms_params->ms_buffers.second_pred;
1011   const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
1012 
1013   const search_site_config *cfg = ms_params->search_sites;
1014 
1015   unsigned int bestsad = INT_MAX;
1016   int best_site = 0;
1017   int is_off_center = 0;
1018 
1019   clamp_fullmv(&start_mv, &ms_params->mv_limits);
1020 
1021   // search_param determines the length of the initial step and hence the number
1022   // of iterations.
1023   const int tot_steps = cfg->ss_count - search_param;
1024 
1025   *num00 = 0;
1026   *best_mv = start_mv;
1027 
1028   // Check the starting position
1029   best_address = get_buf_from_fullmv(ref, &start_mv);
1030   bestsad = get_mvpred_compound_sad(ms_params, src, best_address, ref_stride);
1031   bestsad += mvsad_err_cost_(best_mv, &ms_params->mv_cost_params);
1032 
1033   int next_step_size = tot_steps > 2 ? cfg->radius[tot_steps - 2] : 1;
1034   for (int step = tot_steps - 1; step >= 0; --step) {
1035     const search_site *ss = cfg->ss[step];
1036     best_site = 0;
1037     if (step > 0) next_step_size = cfg->radius[step - 1];
1038 
1039     int all_in = 1, j;
1040     // Trap illegal vectors
1041     all_in &= best_mv->row + ss[1].mv.row >= ms_params->mv_limits.row_min;
1042     all_in &= best_mv->row + ss[2].mv.row <= ms_params->mv_limits.row_max;
1043     all_in &= best_mv->col + ss[3].mv.col >= ms_params->mv_limits.col_min;
1044     all_in &= best_mv->col + ss[4].mv.col <= ms_params->mv_limits.col_max;
1045 
1046     // TODO(anyone): Implement 4 points search for msdf&sdaf
1047     if (all_in && !mask && !second_pred) {
1048       const uint8_t *src_buf = src->buf;
1049       const int src_stride = src->stride;
1050       for (int idx = 1; idx <= cfg->searches_per_step[step]; idx += 4) {
1051         unsigned char const *block_offset[4];
1052         unsigned int sads[4];
1053 
1054         for (j = 0; j < 4; j++)
1055           block_offset[j] = ss[idx + j].offset + best_address;
1056 
1057         vfp->sdx4df(src_buf, src_stride, block_offset, ref_stride, sads);
1058         for (j = 0; j < 4; j++) {
1059           if (sads[j] < bestsad) {
1060             const FULLPEL_MV this_mv = { best_mv->row + ss[idx + j].mv.row,
1061                                          best_mv->col + ss[idx + j].mv.col };
1062             unsigned int thissad =
1063                 sads[j] + mvsad_err_cost_(&this_mv, mv_cost_params);
1064             if (thissad < bestsad) {
1065               bestsad = thissad;
1066               best_site = idx + j;
1067             }
1068           }
1069         }
1070       }
1071     } else {
1072       for (int idx = 1; idx <= cfg->searches_per_step[step]; idx++) {
1073         const FULLPEL_MV this_mv = { best_mv->row + ss[idx].mv.row,
1074                                      best_mv->col + ss[idx].mv.col };
1075 
1076         if (av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) {
1077           const uint8_t *const check_here = ss[idx].offset + best_address;
1078           unsigned int thissad;
1079 
1080           thissad =
1081               get_mvpred_compound_sad(ms_params, src, check_here, ref_stride);
1082 
1083           if (thissad < bestsad) {
1084             thissad += mvsad_err_cost_(&this_mv, mv_cost_params);
1085             if (thissad < bestsad) {
1086               bestsad = thissad;
1087               best_site = idx;
1088             }
1089           }
1090         }
1091       }
1092     }
1093 
1094     if (best_site != 0) {
1095       if (second_best_mv) {
1096         *second_best_mv = *best_mv;
1097       }
1098       best_mv->row += ss[best_site].mv.row;
1099       best_mv->col += ss[best_site].mv.col;
1100       best_address += ss[best_site].offset;
1101       is_off_center = 1;
1102     }
1103 
1104     if (is_off_center == 0) (*num00)++;
1105 
1106     if (best_site == 0) {
1107       while (next_step_size == cfg->radius[step] && step > 2) {
1108         ++(*num00);
1109         --step;
1110         next_step_size = cfg->radius[step - 1];
1111       }
1112     }
1113   }
1114 
1115   return bestsad;
1116 }
1117 
1118 /* do_refine: If last step (1-away) of n-step search doesn't pick the center
1119               point as the best match, we will do a final 1-away diamond
1120               refining search  */
full_pixel_diamond(const FULLPEL_MV start_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const int step_param,int * cost_list,FULLPEL_MV * best_mv,FULLPEL_MV * second_best_mv)1121 static int full_pixel_diamond(const FULLPEL_MV start_mv,
1122                               const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1123                               const int step_param, int *cost_list,
1124                               FULLPEL_MV *best_mv, FULLPEL_MV *second_best_mv) {
1125   const search_site_config *cfg = ms_params->search_sites;
1126   int thissme, n, num00 = 0;
1127   int bestsme = diamond_search_sad(start_mv, ms_params, step_param, &n, best_mv,
1128                                    second_best_mv);
1129 
1130   if (bestsme < INT_MAX) {
1131     bestsme = get_mvpred_compound_var_cost(ms_params, best_mv);
1132   }
1133 
1134   // If there won't be more n-step search, check to see if refining search is
1135   // needed.
1136   const int further_steps = cfg->ss_count - 1 - step_param;
1137   while (n < further_steps) {
1138     ++n;
1139 
1140     if (num00) {
1141       num00--;
1142     } else {
1143       // TODO(chiyotsai@google.com): There is another bug here where the second
1144       // best mv gets incorrectly overwritten. Fix it later.
1145       FULLPEL_MV tmp_best_mv;
1146       thissme = diamond_search_sad(start_mv, ms_params, step_param + n, &num00,
1147                                    &tmp_best_mv, second_best_mv);
1148 
1149       if (thissme < INT_MAX) {
1150         thissme = get_mvpred_compound_var_cost(ms_params, &tmp_best_mv);
1151       }
1152 
1153       if (thissme < bestsme) {
1154         bestsme = thissme;
1155         *best_mv = tmp_best_mv;
1156       }
1157     }
1158   }
1159 
1160   // Return cost list.
1161   if (cost_list) {
1162     if (USE_SAD_COSTLIST) {
1163       const int costlist_has_sad = 0;
1164       calc_int_sad_list(*best_mv, ms_params, cost_list, costlist_has_sad);
1165     } else {
1166       calc_int_cost_list(*best_mv, ms_params, cost_list);
1167     }
1168   }
1169   return bestsme;
1170 }
1171 
1172 // Exhaustive motion search around a given centre position with a given
1173 // step size.
exhaustive_mesh_search(FULLPEL_MV start_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const int range,const int step,FULLPEL_MV * best_mv,FULLPEL_MV * second_best_mv)1174 static int exhaustive_mesh_search(FULLPEL_MV start_mv,
1175                                   const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1176                                   const int range, const int step,
1177                                   FULLPEL_MV *best_mv,
1178                                   FULLPEL_MV *second_best_mv) {
1179   const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
1180   const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
1181   const struct buf_2d *const src = ms_params->ms_buffers.src;
1182   const struct buf_2d *const ref = ms_params->ms_buffers.ref;
1183   const int ref_stride = ref->stride;
1184   unsigned int best_sad = INT_MAX;
1185   int r, c, i;
1186   int start_col, end_col, start_row, end_row;
1187   int col_step = (step > 1) ? step : 4;
1188 
1189   assert(step >= 1);
1190 
1191   clamp_fullmv(&start_mv, &ms_params->mv_limits);
1192   *best_mv = start_mv;
1193   best_sad = get_mvpred_sad(ms_params, src, get_buf_from_fullmv(ref, &start_mv),
1194                             ref_stride);
1195   best_sad += mvsad_err_cost_(&start_mv, mv_cost_params);
1196   start_row = AOMMAX(-range, ms_params->mv_limits.row_min - start_mv.row);
1197   start_col = AOMMAX(-range, ms_params->mv_limits.col_min - start_mv.col);
1198   end_row = AOMMIN(range, ms_params->mv_limits.row_max - start_mv.row);
1199   end_col = AOMMIN(range, ms_params->mv_limits.col_max - start_mv.col);
1200 
1201   for (r = start_row; r <= end_row; r += step) {
1202     for (c = start_col; c <= end_col; c += col_step) {
1203       // Step > 1 means we are not checking every location in this pass.
1204       if (step > 1) {
1205         const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c };
1206         unsigned int sad = get_mvpred_sad(
1207             ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride);
1208         if (sad < best_sad) {
1209           sad += mvsad_err_cost_(&mv, mv_cost_params);
1210           if (sad < best_sad) {
1211             best_sad = sad;
1212             if (second_best_mv) {
1213               *second_best_mv = *best_mv;
1214             }
1215             *best_mv = mv;
1216           }
1217         }
1218       } else {
1219         // 4 sads in a single call if we are checking every location
1220         if (c + 3 <= end_col) {
1221           unsigned int sads[4];
1222           const uint8_t *addrs[4];
1223           for (i = 0; i < 4; ++i) {
1224             const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i };
1225             addrs[i] = get_buf_from_fullmv(ref, &mv);
1226           }
1227           vfp->sdx4df(src->buf, src->stride, addrs, ref_stride, sads);
1228 
1229           for (i = 0; i < 4; ++i) {
1230             if (sads[i] < best_sad) {
1231               const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i };
1232               const unsigned int sad =
1233                   sads[i] + mvsad_err_cost_(&mv, mv_cost_params);
1234               if (sad < best_sad) {
1235                 best_sad = sad;
1236                 if (second_best_mv) {
1237                   *second_best_mv = *best_mv;
1238                 }
1239                 *best_mv = mv;
1240               }
1241             }
1242           }
1243         } else {
1244           for (i = 0; i < end_col - c; ++i) {
1245             const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i };
1246             unsigned int sad = get_mvpred_sad(
1247                 ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride);
1248             if (sad < best_sad) {
1249               sad += mvsad_err_cost_(&mv, mv_cost_params);
1250               if (sad < best_sad) {
1251                 best_sad = sad;
1252                 if (second_best_mv) {
1253                   *second_best_mv = *best_mv;
1254                 }
1255                 *best_mv = mv;
1256               }
1257             }
1258           }
1259         }
1260       }
1261     }
1262   }
1263 
1264   return best_sad;
1265 }
1266 
1267 // Runs an limited range exhaustive mesh search using a pattern set
1268 // according to the encode speed profile.
full_pixel_exhaustive(const FULLPEL_MV start_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const struct MESH_PATTERN * const mesh_patterns,int * cost_list,FULLPEL_MV * best_mv,FULLPEL_MV * second_best_mv)1269 static int full_pixel_exhaustive(const FULLPEL_MV start_mv,
1270                                  const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1271                                  const struct MESH_PATTERN *const mesh_patterns,
1272                                  int *cost_list, FULLPEL_MV *best_mv,
1273                                  FULLPEL_MV *second_best_mv) {
1274   const int kMinRange = 7;
1275   const int kMaxRange = 256;
1276   const int kMinInterval = 1;
1277 
1278   int bestsme;
1279   int i;
1280   int interval = mesh_patterns[0].interval;
1281   int range = mesh_patterns[0].range;
1282   int baseline_interval_divisor;
1283 
1284   *best_mv = start_mv;
1285 
1286   // Trap illegal values for interval and range for this function.
1287   if ((range < kMinRange) || (range > kMaxRange) || (interval < kMinInterval) ||
1288       (interval > range))
1289     return INT_MAX;
1290 
1291   baseline_interval_divisor = range / interval;
1292 
1293   // Check size of proposed first range against magnitude of the centre
1294   // value used as a starting point.
1295   range = AOMMAX(range, (5 * AOMMAX(abs(best_mv->row), abs(best_mv->col))) / 4);
1296   range = AOMMIN(range, kMaxRange);
1297   interval = AOMMAX(interval, range / baseline_interval_divisor);
1298 
1299   // initial search
1300   bestsme = exhaustive_mesh_search(*best_mv, ms_params, range, interval,
1301                                    best_mv, second_best_mv);
1302 
1303   if ((interval > kMinInterval) && (range > kMinRange)) {
1304     // Progressive searches with range and step size decreasing each time
1305     // till we reach a step size of 1. Then break out.
1306     for (i = 1; i < MAX_MESH_STEP; ++i) {
1307       // First pass with coarser step and longer range
1308       bestsme = exhaustive_mesh_search(
1309           *best_mv, ms_params, mesh_patterns[i].range,
1310           mesh_patterns[i].interval, best_mv, second_best_mv);
1311 
1312       if (mesh_patterns[i].interval == 1) break;
1313     }
1314   }
1315 
1316   if (bestsme < INT_MAX) {
1317     bestsme = get_mvpred_var_cost(ms_params, best_mv);
1318   }
1319 
1320   // Return cost list.
1321   if (cost_list) {
1322     if (USE_SAD_COSTLIST) {
1323       const int costlist_has_sad = 0;
1324       calc_int_sad_list(*best_mv, ms_params, cost_list, costlist_has_sad);
1325     } else {
1326       calc_int_cost_list(*best_mv, ms_params, cost_list);
1327     }
1328   }
1329   return bestsme;
1330 }
1331 
1332 // This function is called when we do joint motion search in comp_inter_inter
1333 // mode, or when searching for one component of an ext-inter compound mode.
av1_refining_search_8p_c(const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const FULLPEL_MV start_mv,FULLPEL_MV * best_mv)1334 int av1_refining_search_8p_c(const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1335                              const FULLPEL_MV start_mv, FULLPEL_MV *best_mv) {
1336   static const search_neighbors neighbors[8] = {
1337     { { -1, 0 }, -1 * SEARCH_GRID_STRIDE_8P + 0 },
1338     { { 0, -1 }, 0 * SEARCH_GRID_STRIDE_8P - 1 },
1339     { { 0, 1 }, 0 * SEARCH_GRID_STRIDE_8P + 1 },
1340     { { 1, 0 }, 1 * SEARCH_GRID_STRIDE_8P + 0 },
1341     { { -1, -1 }, -1 * SEARCH_GRID_STRIDE_8P - 1 },
1342     { { 1, -1 }, 1 * SEARCH_GRID_STRIDE_8P - 1 },
1343     { { -1, 1 }, -1 * SEARCH_GRID_STRIDE_8P + 1 },
1344     { { 1, 1 }, 1 * SEARCH_GRID_STRIDE_8P + 1 }
1345   };
1346 
1347   uint8_t do_refine_search_grid[SEARCH_GRID_STRIDE_8P *
1348                                 SEARCH_GRID_STRIDE_8P] = { 0 };
1349   int grid_center = SEARCH_GRID_CENTER_8P;
1350   int grid_coord = grid_center;
1351 
1352   const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
1353   const FullMvLimits *mv_limits = &ms_params->mv_limits;
1354   const MSBuffers *ms_buffers = &ms_params->ms_buffers;
1355   const struct buf_2d *src = ms_buffers->src;
1356   const struct buf_2d *ref = ms_buffers->ref;
1357   const int ref_stride = ref->stride;
1358 
1359   *best_mv = start_mv;
1360   clamp_fullmv(best_mv, mv_limits);
1361 
1362   unsigned int best_sad = get_mvpred_compound_sad(
1363       ms_params, src, get_buf_from_fullmv(ref, best_mv), ref_stride);
1364   best_sad += mvsad_err_cost_(best_mv, mv_cost_params);
1365 
1366   do_refine_search_grid[grid_coord] = 1;
1367 
1368   for (int i = 0; i < SEARCH_RANGE_8P; ++i) {
1369     int best_site = -1;
1370 
1371     for (int j = 0; j < 8; ++j) {
1372       grid_coord = grid_center + neighbors[j].coord_offset;
1373       if (do_refine_search_grid[grid_coord] == 1) {
1374         continue;
1375       }
1376       const FULLPEL_MV mv = { best_mv->row + neighbors[j].coord.row,
1377                               best_mv->col + neighbors[j].coord.col };
1378 
1379       do_refine_search_grid[grid_coord] = 1;
1380       if (av1_is_fullmv_in_range(mv_limits, mv)) {
1381         unsigned int sad;
1382         sad = get_mvpred_compound_sad(
1383             ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride);
1384         if (sad < best_sad) {
1385           sad += mvsad_err_cost_(&mv, mv_cost_params);
1386 
1387           if (sad < best_sad) {
1388             best_sad = sad;
1389             best_site = j;
1390           }
1391         }
1392       }
1393     }
1394 
1395     if (best_site == -1) {
1396       break;
1397     } else {
1398       best_mv->row += neighbors[best_site].coord.row;
1399       best_mv->col += neighbors[best_site].coord.col;
1400       grid_center += neighbors[best_site].coord_offset;
1401     }
1402   }
1403   return best_sad;
1404 }
1405 
av1_full_pixel_search(const FULLPEL_MV start_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const int step_param,int * cost_list,FULLPEL_MV * best_mv,FULLPEL_MV * second_best_mv)1406 int av1_full_pixel_search(const FULLPEL_MV start_mv,
1407                           const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1408                           const int step_param, int *cost_list,
1409                           FULLPEL_MV *best_mv, FULLPEL_MV *second_best_mv) {
1410   const BLOCK_SIZE bsize = ms_params->bsize;
1411   const SEARCH_METHODS search_method = ms_params->search_method;
1412 
1413   const int is_intra_mode = ms_params->is_intra_mode;
1414   int run_mesh_search = ms_params->run_mesh_search;
1415 
1416   int var = 0;
1417   MARK_MV_INVALID(best_mv);
1418   if (second_best_mv) {
1419     MARK_MV_INVALID(second_best_mv);
1420   }
1421 
1422   assert(ms_params->ms_buffers.second_pred == NULL &&
1423          ms_params->ms_buffers.mask == NULL &&
1424          "av1_full_pixel_search does not support compound pred");
1425 
1426   if (cost_list) {
1427     cost_list[0] = INT_MAX;
1428     cost_list[1] = INT_MAX;
1429     cost_list[2] = INT_MAX;
1430     cost_list[3] = INT_MAX;
1431     cost_list[4] = INT_MAX;
1432   }
1433 
1434   switch (search_method) {
1435     case FAST_DIAMOND:
1436       var = fast_dia_search(start_mv, ms_params, step_param, 0, cost_list,
1437                             best_mv);
1438       break;
1439     case FAST_HEX:
1440       var = fast_hex_search(start_mv, ms_params, step_param, 0, cost_list,
1441                             best_mv);
1442       break;
1443     case HEX:
1444       var = hex_search(start_mv, ms_params, step_param, 1, cost_list, best_mv);
1445       break;
1446     case SQUARE:
1447       var =
1448           square_search(start_mv, ms_params, step_param, 1, cost_list, best_mv);
1449       break;
1450     case BIGDIA:
1451       var =
1452           bigdia_search(start_mv, ms_params, step_param, 1, cost_list, best_mv);
1453       break;
1454     case NSTEP:
1455     case DIAMOND:
1456       var = full_pixel_diamond(start_mv, ms_params, step_param, cost_list,
1457                                best_mv, second_best_mv);
1458       break;
1459     default: assert(0 && "Invalid search method.");
1460   }
1461 
1462   // Should we allow a follow on exhaustive search?
1463   if (!run_mesh_search && search_method == NSTEP) {
1464     int exhuastive_thr = ms_params->force_mesh_thresh;
1465     exhuastive_thr >>=
1466         10 - (mi_size_wide_log2[bsize] + mi_size_high_log2[bsize]);
1467     // Threshold variance for an exhaustive full search.
1468     if (var > exhuastive_thr) run_mesh_search = 1;
1469   }
1470 
1471   // TODO(yunqing): the following is used to reduce mesh search in temporal
1472   // filtering. Can extend it to intrabc.
1473   if (!is_intra_mode && ms_params->prune_mesh_search) {
1474     const int full_pel_mv_diff = AOMMAX(abs(start_mv.row - best_mv->row),
1475                                         abs(start_mv.col - best_mv->col));
1476     if (full_pel_mv_diff <= 4) {
1477       run_mesh_search = 0;
1478     }
1479   }
1480 
1481   if (run_mesh_search) {
1482     int var_ex;
1483     FULLPEL_MV tmp_mv_ex;
1484     // Pick the mesh pattern for exhaustive search based on the toolset (intraBC
1485     // or non-intraBC)
1486     // TODO(chiyotsai@google.com):  There is a bug here where the second best mv
1487     // gets overwritten without actually comparing the rdcost.
1488     const MESH_PATTERN *const mesh_patterns =
1489         ms_params->mesh_patterns[is_intra_mode];
1490     // TODO(chiyotsai@google.com): the second best mv is not set correctly by
1491     // full_pixel_exhaustive, which can incorrectly override it.
1492     var_ex = full_pixel_exhaustive(*best_mv, ms_params, mesh_patterns,
1493                                    cost_list, &tmp_mv_ex, second_best_mv);
1494     if (var_ex < var) {
1495       var = var_ex;
1496       *best_mv = tmp_mv_ex;
1497     }
1498   }
1499 
1500   return var;
1501 }
1502 
av1_intrabc_hash_search(const AV1_COMP * cpi,const MACROBLOCKD * xd,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,IntraBCHashInfo * intrabc_hash_info,FULLPEL_MV * best_mv)1503 int av1_intrabc_hash_search(const AV1_COMP *cpi, const MACROBLOCKD *xd,
1504                             const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1505                             IntraBCHashInfo *intrabc_hash_info,
1506                             FULLPEL_MV *best_mv) {
1507   if (!av1_use_hash_me(cpi)) return INT_MAX;
1508 
1509   const BLOCK_SIZE bsize = ms_params->bsize;
1510   const int block_width = block_size_wide[bsize];
1511   const int block_height = block_size_high[bsize];
1512 
1513   if (block_width != block_height) return INT_MAX;
1514 
1515   const FullMvLimits *mv_limits = &ms_params->mv_limits;
1516   const MSBuffers *ms_buffer = &ms_params->ms_buffers;
1517 
1518   const uint8_t *src = ms_buffer->src->buf;
1519   const int src_stride = ms_buffer->src->stride;
1520 
1521   const int mi_row = xd->mi_row;
1522   const int mi_col = xd->mi_col;
1523   const int x_pos = mi_col * MI_SIZE;
1524   const int y_pos = mi_row * MI_SIZE;
1525 
1526   uint32_t hash_value1, hash_value2;
1527   int best_hash_cost = INT_MAX;
1528 
1529   // for the hashMap
1530   hash_table *ref_frame_hash = &intrabc_hash_info->intrabc_hash_table;
1531 
1532   av1_get_block_hash_value(intrabc_hash_info, src, src_stride, block_width,
1533                            &hash_value1, &hash_value2, is_cur_buf_hbd(xd));
1534 
1535   const int count = av1_hash_table_count(ref_frame_hash, hash_value1);
1536   if (count <= 1) {
1537     return INT_MAX;
1538   }
1539 
1540   Iterator iterator = av1_hash_get_first_iterator(ref_frame_hash, hash_value1);
1541   for (int i = 0; i < count; i++, aom_iterator_increment(&iterator)) {
1542     block_hash ref_block_hash = *(block_hash *)(aom_iterator_get(&iterator));
1543     if (hash_value2 == ref_block_hash.hash_value2) {
1544       // Make sure the prediction is from valid area.
1545       const MV dv = { GET_MV_SUBPEL(ref_block_hash.y - y_pos),
1546                       GET_MV_SUBPEL(ref_block_hash.x - x_pos) };
1547       if (!av1_is_dv_valid(dv, &cpi->common, xd, mi_row, mi_col, bsize,
1548                            cpi->common.seq_params.mib_size_log2))
1549         continue;
1550 
1551       FULLPEL_MV hash_mv;
1552       hash_mv.col = ref_block_hash.x - x_pos;
1553       hash_mv.row = ref_block_hash.y - y_pos;
1554       if (!av1_is_fullmv_in_range(mv_limits, hash_mv)) continue;
1555       const int refCost = get_mvpred_var_cost(ms_params, &hash_mv);
1556       if (refCost < best_hash_cost) {
1557         best_hash_cost = refCost;
1558         *best_mv = hash_mv;
1559       }
1560     }
1561   }
1562 
1563   return best_hash_cost;
1564 }
1565 
vector_match(int16_t * ref,int16_t * src,int bwl)1566 static int vector_match(int16_t *ref, int16_t *src, int bwl) {
1567   int best_sad = INT_MAX;
1568   int this_sad;
1569   int d;
1570   int center, offset = 0;
1571   int bw = 4 << bwl;  // redundant variable, to be changed in the experiments.
1572   for (d = 0; d <= bw; d += 16) {
1573     this_sad = aom_vector_var(&ref[d], src, bwl);
1574     if (this_sad < best_sad) {
1575       best_sad = this_sad;
1576       offset = d;
1577     }
1578   }
1579   center = offset;
1580 
1581   for (d = -8; d <= 8; d += 16) {
1582     int this_pos = offset + d;
1583     // check limit
1584     if (this_pos < 0 || this_pos > bw) continue;
1585     this_sad = aom_vector_var(&ref[this_pos], src, bwl);
1586     if (this_sad < best_sad) {
1587       best_sad = this_sad;
1588       center = this_pos;
1589     }
1590   }
1591   offset = center;
1592 
1593   for (d = -4; d <= 4; d += 8) {
1594     int this_pos = offset + d;
1595     // check limit
1596     if (this_pos < 0 || this_pos > bw) continue;
1597     this_sad = aom_vector_var(&ref[this_pos], src, bwl);
1598     if (this_sad < best_sad) {
1599       best_sad = this_sad;
1600       center = this_pos;
1601     }
1602   }
1603   offset = center;
1604 
1605   for (d = -2; d <= 2; d += 4) {
1606     int this_pos = offset + d;
1607     // check limit
1608     if (this_pos < 0 || this_pos > bw) continue;
1609     this_sad = aom_vector_var(&ref[this_pos], src, bwl);
1610     if (this_sad < best_sad) {
1611       best_sad = this_sad;
1612       center = this_pos;
1613     }
1614   }
1615   offset = center;
1616 
1617   for (d = -1; d <= 1; d += 2) {
1618     int this_pos = offset + d;
1619     // check limit
1620     if (this_pos < 0 || this_pos > bw) continue;
1621     this_sad = aom_vector_var(&ref[this_pos], src, bwl);
1622     if (this_sad < best_sad) {
1623       best_sad = this_sad;
1624       center = this_pos;
1625     }
1626   }
1627 
1628   return (center - (bw >> 1));
1629 }
1630 
1631 // A special fast version of motion search used in rt mode
av1_int_pro_motion_estimation(const AV1_COMP * cpi,MACROBLOCK * x,BLOCK_SIZE bsize,int mi_row,int mi_col,const MV * ref_mv)1632 unsigned int av1_int_pro_motion_estimation(const AV1_COMP *cpi, MACROBLOCK *x,
1633                                            BLOCK_SIZE bsize, int mi_row,
1634                                            int mi_col, const MV *ref_mv) {
1635   MACROBLOCKD *xd = &x->e_mbd;
1636   MB_MODE_INFO *mi = xd->mi[0];
1637   struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } };
1638   DECLARE_ALIGNED(16, int16_t, hbuf[256]);
1639   DECLARE_ALIGNED(16, int16_t, vbuf[256]);
1640   DECLARE_ALIGNED(16, int16_t, src_hbuf[128]);
1641   DECLARE_ALIGNED(16, int16_t, src_vbuf[128]);
1642   int idx;
1643   const int bw = 4 << mi_size_wide_log2[bsize];
1644   const int bh = 4 << mi_size_high_log2[bsize];
1645   const int search_width = bw << 1;
1646   const int search_height = bh << 1;
1647   const int src_stride = x->plane[0].src.stride;
1648   const int ref_stride = xd->plane[0].pre[0].stride;
1649   uint8_t const *ref_buf, *src_buf;
1650   int_mv *best_int_mv = &xd->mi[0]->mv[0];
1651   unsigned int best_sad, tmp_sad, this_sad[4];
1652   const int norm_factor = 3 + (bw >> 5);
1653   const YV12_BUFFER_CONFIG *scaled_ref_frame =
1654       av1_get_scaled_ref_frame(cpi, mi->ref_frame[0]);
1655   static const MV search_pos[4] = {
1656     { -1, 0 },
1657     { 0, -1 },
1658     { 0, 1 },
1659     { 1, 0 },
1660   };
1661 
1662   if (scaled_ref_frame) {
1663     int i;
1664     // Swap out the reference frame for a version that's been scaled to
1665     // match the resolution of the current frame, allowing the existing
1666     // motion search code to be used without additional modifications.
1667     for (i = 0; i < MAX_MB_PLANE; i++) backup_yv12[i] = xd->plane[i].pre[0];
1668     av1_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL,
1669                          MAX_MB_PLANE);
1670   }
1671 
1672   if (xd->bd != 8) {
1673     unsigned int sad;
1674     best_int_mv->as_fullmv = kZeroFullMv;
1675     sad = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf, src_stride,
1676                                  xd->plane[0].pre[0].buf, ref_stride);
1677 
1678     if (scaled_ref_frame) {
1679       int i;
1680       for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i];
1681     }
1682     return sad;
1683   }
1684 
1685   // Set up prediction 1-D reference set
1686   ref_buf = xd->plane[0].pre[0].buf - (bw >> 1);
1687   for (idx = 0; idx < search_width; idx += 16) {
1688     aom_int_pro_row(&hbuf[idx], ref_buf, ref_stride, bh);
1689     ref_buf += 16;
1690   }
1691 
1692   ref_buf = xd->plane[0].pre[0].buf - (bh >> 1) * ref_stride;
1693   for (idx = 0; idx < search_height; ++idx) {
1694     vbuf[idx] = aom_int_pro_col(ref_buf, bw) >> norm_factor;
1695     ref_buf += ref_stride;
1696   }
1697 
1698   // Set up src 1-D reference set
1699   for (idx = 0; idx < bw; idx += 16) {
1700     src_buf = x->plane[0].src.buf + idx;
1701     aom_int_pro_row(&src_hbuf[idx], src_buf, src_stride, bh);
1702   }
1703 
1704   src_buf = x->plane[0].src.buf;
1705   for (idx = 0; idx < bh; ++idx) {
1706     src_vbuf[idx] = aom_int_pro_col(src_buf, bw) >> norm_factor;
1707     src_buf += src_stride;
1708   }
1709 
1710   // Find the best match per 1-D search
1711   best_int_mv->as_fullmv.col =
1712       vector_match(hbuf, src_hbuf, mi_size_wide_log2[bsize]);
1713   best_int_mv->as_fullmv.row =
1714       vector_match(vbuf, src_vbuf, mi_size_high_log2[bsize]);
1715 
1716   FULLPEL_MV this_mv = best_int_mv->as_fullmv;
1717   src_buf = x->plane[0].src.buf;
1718   ref_buf = get_buf_from_fullmv(&xd->plane[0].pre[0], &this_mv);
1719   best_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride, ref_buf, ref_stride);
1720 
1721   {
1722     const uint8_t *const pos[4] = {
1723       ref_buf - ref_stride,
1724       ref_buf - 1,
1725       ref_buf + 1,
1726       ref_buf + ref_stride,
1727     };
1728 
1729     cpi->fn_ptr[bsize].sdx4df(src_buf, src_stride, pos, ref_stride, this_sad);
1730   }
1731 
1732   for (idx = 0; idx < 4; ++idx) {
1733     if (this_sad[idx] < best_sad) {
1734       best_sad = this_sad[idx];
1735       best_int_mv->as_fullmv.row = search_pos[idx].row + this_mv.row;
1736       best_int_mv->as_fullmv.col = search_pos[idx].col + this_mv.col;
1737     }
1738   }
1739 
1740   if (this_sad[0] < this_sad[3])
1741     this_mv.row -= 1;
1742   else
1743     this_mv.row += 1;
1744 
1745   if (this_sad[1] < this_sad[2])
1746     this_mv.col -= 1;
1747   else
1748     this_mv.col += 1;
1749 
1750   ref_buf = get_buf_from_fullmv(&xd->plane[0].pre[0], &this_mv);
1751 
1752   tmp_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride, ref_buf, ref_stride);
1753   if (best_sad > tmp_sad) {
1754     best_int_mv->as_fullmv = this_mv;
1755     best_sad = tmp_sad;
1756   }
1757 
1758   convert_fullmv_to_mv(best_int_mv);
1759 
1760   SubpelMvLimits subpel_mv_limits;
1761   av1_set_subpel_mv_search_range(&subpel_mv_limits, &x->mv_limits, ref_mv);
1762   clamp_mv(&best_int_mv->as_mv, &subpel_mv_limits);
1763 
1764   if (scaled_ref_frame) {
1765     int i;
1766     for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i];
1767   }
1768 
1769   return best_sad;
1770 }
1771 
1772 // =============================================================================
1773 //  Fullpixel Motion Search: OBMC
1774 // =============================================================================
get_obmc_mvpred_var(const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const FULLPEL_MV * this_mv)1775 static INLINE int get_obmc_mvpred_var(
1776     const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV *this_mv) {
1777   const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
1778   const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
1779   const MSBuffers *ms_buffers = &ms_params->ms_buffers;
1780   const int32_t *wsrc = ms_buffers->wsrc;
1781   const int32_t *mask = ms_buffers->obmc_mask;
1782   const struct buf_2d *ref_buf = ms_buffers->ref;
1783 
1784   const MV mv = get_mv_from_fullmv(this_mv);
1785   unsigned int unused;
1786 
1787   return vfp->ovf(get_buf_from_fullmv(ref_buf, this_mv), ref_buf->stride, wsrc,
1788                   mask, &unused) +
1789          mv_err_cost_(&mv, mv_cost_params);
1790 }
1791 
obmc_refining_search_sad(const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,FULLPEL_MV * best_mv)1792 static int obmc_refining_search_sad(
1793     const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, FULLPEL_MV *best_mv) {
1794   const aom_variance_fn_ptr_t *fn_ptr = ms_params->vfp;
1795   const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
1796   const MSBuffers *ms_buffers = &ms_params->ms_buffers;
1797   const int32_t *wsrc = ms_buffers->wsrc;
1798   const int32_t *mask = ms_buffers->obmc_mask;
1799   const struct buf_2d *ref_buf = ms_buffers->ref;
1800   const FULLPEL_MV neighbors[4] = { { -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, 0 } };
1801   const int kSearchRange = 8;
1802 
1803   unsigned int best_sad = fn_ptr->osdf(get_buf_from_fullmv(ref_buf, best_mv),
1804                                        ref_buf->stride, wsrc, mask) +
1805                           mvsad_err_cost_(best_mv, mv_cost_params);
1806 
1807   for (int i = 0; i < kSearchRange; i++) {
1808     int best_site = -1;
1809 
1810     for (int j = 0; j < 4; j++) {
1811       const FULLPEL_MV mv = { best_mv->row + neighbors[j].row,
1812                               best_mv->col + neighbors[j].col };
1813       if (av1_is_fullmv_in_range(&ms_params->mv_limits, mv)) {
1814         unsigned int sad = fn_ptr->osdf(get_buf_from_fullmv(ref_buf, &mv),
1815                                         ref_buf->stride, wsrc, mask);
1816         if (sad < best_sad) {
1817           sad += mvsad_err_cost_(&mv, mv_cost_params);
1818 
1819           if (sad < best_sad) {
1820             best_sad = sad;
1821             best_site = j;
1822           }
1823         }
1824       }
1825     }
1826 
1827     if (best_site == -1) {
1828       break;
1829     } else {
1830       best_mv->row += neighbors[best_site].row;
1831       best_mv->col += neighbors[best_site].col;
1832     }
1833   }
1834   return best_sad;
1835 }
1836 
obmc_diamond_search_sad(const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,FULLPEL_MV start_mv,FULLPEL_MV * best_mv,int search_param,int * num00)1837 static int obmc_diamond_search_sad(
1838     const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, FULLPEL_MV start_mv,
1839     FULLPEL_MV *best_mv, int search_param, int *num00) {
1840   const aom_variance_fn_ptr_t *fn_ptr = ms_params->vfp;
1841   const search_site_config *cfg = ms_params->search_sites;
1842   const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
1843   const MSBuffers *ms_buffers = &ms_params->ms_buffers;
1844   const int32_t *wsrc = ms_buffers->wsrc;
1845   const int32_t *mask = ms_buffers->obmc_mask;
1846   const struct buf_2d *const ref_buf = ms_buffers->ref;
1847   // search_param determines the length of the initial step and hence the number
1848   // of iterations
1849   // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 =
1850   // (MAX_FIRST_STEP/4) pel... etc.
1851 
1852   const int tot_steps = MAX_MVSEARCH_STEPS - 1 - search_param;
1853   const uint8_t *best_address, *init_ref;
1854   int best_sad = INT_MAX;
1855   int best_site = 0;
1856   int step;
1857 
1858   clamp_fullmv(&start_mv, &ms_params->mv_limits);
1859   best_address = init_ref = get_buf_from_fullmv(ref_buf, &start_mv);
1860   *num00 = 0;
1861   *best_mv = start_mv;
1862 
1863   // Check the starting position
1864   best_sad = fn_ptr->osdf(best_address, ref_buf->stride, wsrc, mask) +
1865              mvsad_err_cost_(best_mv, mv_cost_params);
1866 
1867   for (step = tot_steps; step >= 0; --step) {
1868     const search_site *const ss = cfg->ss[step];
1869     best_site = 0;
1870     for (int idx = 1; idx <= cfg->searches_per_step[step]; ++idx) {
1871       const FULLPEL_MV mv = { best_mv->row + ss[idx].mv.row,
1872                               best_mv->col + ss[idx].mv.col };
1873       if (av1_is_fullmv_in_range(&ms_params->mv_limits, mv)) {
1874         int sad = fn_ptr->osdf(best_address + ss[idx].offset, ref_buf->stride,
1875                                wsrc, mask);
1876         if (sad < best_sad) {
1877           sad += mvsad_err_cost_(&mv, mv_cost_params);
1878 
1879           if (sad < best_sad) {
1880             best_sad = sad;
1881             best_site = idx;
1882           }
1883         }
1884       }
1885     }
1886 
1887     if (best_site != 0) {
1888       best_mv->row += ss[best_site].mv.row;
1889       best_mv->col += ss[best_site].mv.col;
1890       best_address += ss[best_site].offset;
1891     } else if (best_address == init_ref) {
1892       (*num00)++;
1893     }
1894   }
1895   return best_sad;
1896 }
1897 
obmc_full_pixel_diamond(const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const FULLPEL_MV start_mv,int step_param,int do_refine,FULLPEL_MV * best_mv)1898 static int obmc_full_pixel_diamond(
1899     const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV start_mv,
1900     int step_param, int do_refine, FULLPEL_MV *best_mv) {
1901   const search_site_config *cfg = ms_params->search_sites;
1902   FULLPEL_MV tmp_mv;
1903   int thissme, n, num00 = 0;
1904   int bestsme =
1905       obmc_diamond_search_sad(ms_params, start_mv, &tmp_mv, step_param, &n);
1906   if (bestsme < INT_MAX) bestsme = get_obmc_mvpred_var(ms_params, &tmp_mv);
1907   *best_mv = tmp_mv;
1908 
1909   // If there won't be more n-step search, check to see if refining search is
1910   // needed.
1911   const int further_steps = cfg->ss_count - 1 - step_param;
1912   if (n > further_steps) do_refine = 0;
1913 
1914   while (n < further_steps) {
1915     ++n;
1916 
1917     if (num00) {
1918       num00--;
1919     } else {
1920       thissme = obmc_diamond_search_sad(ms_params, start_mv, &tmp_mv,
1921                                         step_param + n, &num00);
1922       if (thissme < INT_MAX) thissme = get_obmc_mvpred_var(ms_params, &tmp_mv);
1923 
1924       // check to see if refining search is needed.
1925       if (num00 > further_steps - n) do_refine = 0;
1926 
1927       if (thissme < bestsme) {
1928         bestsme = thissme;
1929         *best_mv = tmp_mv;
1930       }
1931     }
1932   }
1933 
1934   // final 1-away diamond refining search
1935   if (do_refine) {
1936     tmp_mv = *best_mv;
1937     thissme = obmc_refining_search_sad(ms_params, &tmp_mv);
1938     if (thissme < INT_MAX) thissme = get_obmc_mvpred_var(ms_params, &tmp_mv);
1939     if (thissme < bestsme) {
1940       bestsme = thissme;
1941       *best_mv = tmp_mv;
1942     }
1943   }
1944   return bestsme;
1945 }
1946 
av1_obmc_full_pixel_search(const FULLPEL_MV start_mv,const FULLPEL_MOTION_SEARCH_PARAMS * ms_params,const int step_param,FULLPEL_MV * best_mv)1947 int av1_obmc_full_pixel_search(const FULLPEL_MV start_mv,
1948                                const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1949                                const int step_param, FULLPEL_MV *best_mv) {
1950   if (!ms_params->fast_obmc_search) {
1951     const int do_refine = 1;
1952     const int bestsme = obmc_full_pixel_diamond(ms_params, start_mv, step_param,
1953                                                 do_refine, best_mv);
1954     return bestsme;
1955   } else {
1956     *best_mv = start_mv;
1957     clamp_fullmv(best_mv, &ms_params->mv_limits);
1958     int thissme = obmc_refining_search_sad(ms_params, best_mv);
1959     if (thissme < INT_MAX) thissme = get_obmc_mvpred_var(ms_params, best_mv);
1960     return thissme;
1961   }
1962 }
1963 
1964 // =============================================================================
1965 //  Subpixel Motion Search: Translational
1966 // =============================================================================
1967 #define INIT_SUBPEL_STEP_SIZE (4)
1968 /*
1969  * To avoid the penalty for crossing cache-line read, preload the reference
1970  * area in a small buffer, which is aligned to make sure there won't be crossing
1971  * cache-line read while reading from this buffer. This reduced the cpu
1972  * cycles spent on reading ref data in sub-pixel filter functions.
1973  * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x
1974  * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we
1975  * could reduce the area.
1976  */
1977 
1978 // Returns the subpel offset used by various subpel variance functions [m]sv[a]f
get_subpel_part(int x)1979 static INLINE int get_subpel_part(int x) { return x & 7; }
1980 
1981 // Gets the address of the ref buffer at subpel location (r, c), rounded to the
1982 // nearest fullpel precision toward - \infty
1983 
get_buf_from_mv(const struct buf_2d * buf,const MV mv)1984 static INLINE const uint8_t *get_buf_from_mv(const struct buf_2d *buf,
1985                                              const MV mv) {
1986   const int offset = (mv.row >> 3) * buf->stride + (mv.col >> 3);
1987   return &buf->buf[offset];
1988 }
1989 
1990 // Estimates the variance of prediction residue using bilinear filter for fast
1991 // search.
estimated_pref_error(const MV * this_mv,const SUBPEL_SEARCH_VAR_PARAMS * var_params,unsigned int * sse)1992 static INLINE int estimated_pref_error(
1993     const MV *this_mv, const SUBPEL_SEARCH_VAR_PARAMS *var_params,
1994     unsigned int *sse) {
1995   const aom_variance_fn_ptr_t *vfp = var_params->vfp;
1996 
1997   const MSBuffers *ms_buffers = &var_params->ms_buffers;
1998   const uint8_t *src = ms_buffers->src->buf;
1999   const uint8_t *ref = get_buf_from_mv(ms_buffers->ref, *this_mv);
2000   const int src_stride = ms_buffers->src->stride;
2001   const int ref_stride = ms_buffers->ref->stride;
2002   const uint8_t *second_pred = ms_buffers->second_pred;
2003   const uint8_t *mask = ms_buffers->mask;
2004   const int mask_stride = ms_buffers->mask_stride;
2005   const int invert_mask = ms_buffers->inv_mask;
2006 
2007   const int subpel_x_q3 = get_subpel_part(this_mv->col);
2008   const int subpel_y_q3 = get_subpel_part(this_mv->row);
2009 
2010   if (second_pred == NULL) {
2011     return vfp->svf(ref, ref_stride, subpel_x_q3, subpel_y_q3, src, src_stride,
2012                     sse);
2013   } else if (mask) {
2014     return vfp->msvf(ref, ref_stride, subpel_x_q3, subpel_y_q3, src, src_stride,
2015                      second_pred, mask, mask_stride, invert_mask, sse);
2016   } else {
2017     return vfp->svaf(ref, ref_stride, subpel_x_q3, subpel_y_q3, src, src_stride,
2018                      sse, second_pred);
2019   }
2020 }
2021 
2022 // Calculates the variance of prediction residue.
upsampled_pref_error(MACROBLOCKD * xd,const AV1_COMMON * cm,const MV * this_mv,const SUBPEL_SEARCH_VAR_PARAMS * var_params,unsigned int * sse)2023 static int upsampled_pref_error(MACROBLOCKD *xd, const AV1_COMMON *cm,
2024                                 const MV *this_mv,
2025                                 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2026                                 unsigned int *sse) {
2027   const aom_variance_fn_ptr_t *vfp = var_params->vfp;
2028   const SUBPEL_SEARCH_TYPE subpel_search_type = var_params->subpel_search_type;
2029 
2030   const MSBuffers *ms_buffers = &var_params->ms_buffers;
2031   const uint8_t *src = ms_buffers->src->buf;
2032   const uint8_t *ref = get_buf_from_mv(ms_buffers->ref, *this_mv);
2033   const int src_stride = ms_buffers->src->stride;
2034   const int ref_stride = ms_buffers->ref->stride;
2035   const uint8_t *second_pred = ms_buffers->second_pred;
2036   const uint8_t *mask = ms_buffers->mask;
2037   const int mask_stride = ms_buffers->mask_stride;
2038   const int invert_mask = ms_buffers->inv_mask;
2039   const int w = var_params->w;
2040   const int h = var_params->h;
2041 
2042   const int mi_row = xd->mi_row;
2043   const int mi_col = xd->mi_col;
2044   const int subpel_x_q3 = get_subpel_part(this_mv->col);
2045   const int subpel_y_q3 = get_subpel_part(this_mv->row);
2046 
2047   unsigned int besterr;
2048 #if CONFIG_AV1_HIGHBITDEPTH
2049   if (is_cur_buf_hbd(xd)) {
2050     DECLARE_ALIGNED(16, uint16_t, pred16[MAX_SB_SQUARE]);
2051     uint8_t *pred8 = CONVERT_TO_BYTEPTR(pred16);
2052     if (second_pred != NULL) {
2053       if (mask) {
2054         aom_highbd_comp_mask_upsampled_pred(
2055             xd, cm, mi_row, mi_col, this_mv, pred8, second_pred, w, h,
2056             subpel_x_q3, subpel_y_q3, ref, ref_stride, mask, mask_stride,
2057             invert_mask, xd->bd, subpel_search_type);
2058       } else {
2059         aom_highbd_comp_avg_upsampled_pred(
2060             xd, cm, mi_row, mi_col, this_mv, pred8, second_pred, w, h,
2061             subpel_x_q3, subpel_y_q3, ref, ref_stride, xd->bd,
2062             subpel_search_type);
2063       }
2064     } else {
2065       aom_highbd_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred8, w, h,
2066                                 subpel_x_q3, subpel_y_q3, ref, ref_stride,
2067                                 xd->bd, subpel_search_type);
2068     }
2069     besterr = vfp->vf(pred8, w, src, src_stride, sse);
2070   } else {
2071     DECLARE_ALIGNED(16, uint8_t, pred[MAX_SB_SQUARE]);
2072     if (second_pred != NULL) {
2073       if (mask) {
2074         aom_comp_mask_upsampled_pred(
2075             xd, cm, mi_row, mi_col, this_mv, pred, second_pred, w, h,
2076             subpel_x_q3, subpel_y_q3, ref, ref_stride, mask, mask_stride,
2077             invert_mask, subpel_search_type);
2078       } else {
2079         aom_comp_avg_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred,
2080                                     second_pred, w, h, subpel_x_q3, subpel_y_q3,
2081                                     ref, ref_stride, subpel_search_type);
2082       }
2083     } else {
2084       aom_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred, w, h,
2085                          subpel_x_q3, subpel_y_q3, ref, ref_stride,
2086                          subpel_search_type);
2087     }
2088 
2089     besterr = vfp->vf(pred, w, src, src_stride, sse);
2090   }
2091 #else
2092   DECLARE_ALIGNED(16, uint8_t, pred[MAX_SB_SQUARE]);
2093   if (second_pred != NULL) {
2094     if (mask) {
2095       aom_comp_mask_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred,
2096                                    second_pred, w, h, subpel_x_q3, subpel_y_q3,
2097                                    ref, ref_stride, mask, mask_stride,
2098                                    invert_mask, subpel_search_type);
2099     } else {
2100       aom_comp_avg_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred,
2101                                   second_pred, w, h, subpel_x_q3, subpel_y_q3,
2102                                   ref, ref_stride, subpel_search_type);
2103     }
2104   } else {
2105     aom_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred, w, h, subpel_x_q3,
2106                        subpel_y_q3, ref, ref_stride, subpel_search_type);
2107   }
2108 
2109   besterr = vfp->vf(pred, w, src, src_stride, sse);
2110 #endif
2111   return besterr;
2112 }
2113 
2114 // Estimates whether this_mv is better than best_mv. This function incorporates
2115 // both prediction error and residue into account. It is suffixed "fast" because
2116 // it uses bilinear filter to estimate the prediction.
check_better_fast(const MV * this_mv,MV * best_mv,const SubpelMvLimits * mv_limits,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * besterr,unsigned int * sse1,int * distortion,int * has_better_mv)2117 static INLINE unsigned int check_better_fast(
2118     const MV *this_mv, MV *best_mv, const SubpelMvLimits *mv_limits,
2119     const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2120     const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
2121     unsigned int *sse1, int *distortion, int *has_better_mv) {
2122   unsigned int cost;
2123   if (av1_is_subpelmv_in_range(mv_limits, *this_mv)) {
2124     unsigned int sse;
2125     int thismse = estimated_pref_error(this_mv, var_params, &sse);
2126     cost = mv_err_cost_(this_mv, mv_cost_params);
2127     cost += thismse;
2128 
2129     if (cost < *besterr) {
2130       *besterr = cost;
2131       *best_mv = *this_mv;
2132       *distortion = thismse;
2133       *sse1 = sse;
2134       *has_better_mv |= 1;
2135     }
2136   } else {
2137     cost = INT_MAX;
2138   }
2139   return cost;
2140 }
2141 
2142 // Checks whether this_mv is better than best_mv. This function incorporates
2143 // both prediction error and residue into account.
check_better(MACROBLOCKD * xd,const AV1_COMMON * cm,const MV * this_mv,MV * best_mv,const SubpelMvLimits * mv_limits,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * besterr,unsigned int * sse1,int * distortion,int * is_better)2144 static AOM_FORCE_INLINE unsigned int check_better(
2145     MACROBLOCKD *xd, const AV1_COMMON *cm, const MV *this_mv, MV *best_mv,
2146     const SubpelMvLimits *mv_limits, const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2147     const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
2148     unsigned int *sse1, int *distortion, int *is_better) {
2149   unsigned int cost;
2150   if (av1_is_subpelmv_in_range(mv_limits, *this_mv)) {
2151     unsigned int sse;
2152     int thismse;
2153     thismse = upsampled_pref_error(xd, cm, this_mv, var_params, &sse);
2154     cost = mv_err_cost_(this_mv, mv_cost_params);
2155     cost += thismse;
2156     if (cost < *besterr) {
2157       *besterr = cost;
2158       *best_mv = *this_mv;
2159       *distortion = thismse;
2160       *sse1 = sse;
2161       *is_better |= 1;
2162     }
2163   } else {
2164     cost = INT_MAX;
2165   }
2166   return cost;
2167 }
2168 
get_best_diag_step(int step_size,unsigned int left_cost,unsigned int right_cost,unsigned int up_cost,unsigned int down_cost)2169 static INLINE MV get_best_diag_step(int step_size, unsigned int left_cost,
2170                                     unsigned int right_cost,
2171                                     unsigned int up_cost,
2172                                     unsigned int down_cost) {
2173   const MV diag_step = { up_cost <= down_cost ? -step_size : step_size,
2174                          left_cost <= right_cost ? -step_size : step_size };
2175 
2176   return diag_step;
2177 }
2178 
2179 // Searches the four cardinal direction for a better mv, then follows up with a
2180 // search in the best quadrant. This uses bilinear filter to speed up the
2181 // calculation.
first_level_check_fast(const MV this_mv,MV * best_mv,int hstep,const SubpelMvLimits * mv_limits,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * besterr,unsigned int * sse1,int * distortion)2182 static AOM_FORCE_INLINE MV first_level_check_fast(
2183     const MV this_mv, MV *best_mv, int hstep, const SubpelMvLimits *mv_limits,
2184     const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2185     const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
2186     unsigned int *sse1, int *distortion) {
2187   // Check the four cardinal directions
2188   const MV left_mv = { this_mv.row, this_mv.col - hstep };
2189   int dummy = 0;
2190   const unsigned int left =
2191       check_better_fast(&left_mv, best_mv, mv_limits, var_params,
2192                         mv_cost_params, besterr, sse1, distortion, &dummy);
2193 
2194   const MV right_mv = { this_mv.row, this_mv.col + hstep };
2195   const unsigned int right =
2196       check_better_fast(&right_mv, best_mv, mv_limits, var_params,
2197                         mv_cost_params, besterr, sse1, distortion, &dummy);
2198 
2199   const MV top_mv = { this_mv.row - hstep, this_mv.col };
2200   const unsigned int up =
2201       check_better_fast(&top_mv, best_mv, mv_limits, var_params, mv_cost_params,
2202                         besterr, sse1, distortion, &dummy);
2203 
2204   const MV bottom_mv = { this_mv.row + hstep, this_mv.col };
2205   const unsigned int down =
2206       check_better_fast(&bottom_mv, best_mv, mv_limits, var_params,
2207                         mv_cost_params, besterr, sse1, distortion, &dummy);
2208 
2209   const MV diag_step = get_best_diag_step(hstep, left, right, up, down);
2210   const MV diag_mv = { this_mv.row + diag_step.row,
2211                        this_mv.col + diag_step.col };
2212 
2213   // Check the diagonal direction with the best mv
2214   check_better_fast(&diag_mv, best_mv, mv_limits, var_params, mv_cost_params,
2215                     besterr, sse1, distortion, &dummy);
2216 
2217   return diag_step;
2218 }
2219 
2220 // Performs a following up search after first_level_check_fast is called. This
2221 // performs two extra chess pattern searches in the best quadrant.
second_level_check_fast(const MV this_mv,const MV diag_step,MV * best_mv,int hstep,const SubpelMvLimits * mv_limits,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * besterr,unsigned int * sse1,int * distortion)2222 static AOM_FORCE_INLINE void second_level_check_fast(
2223     const MV this_mv, const MV diag_step, MV *best_mv, int hstep,
2224     const SubpelMvLimits *mv_limits, const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2225     const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
2226     unsigned int *sse1, int *distortion) {
2227   assert(diag_step.row == hstep || diag_step.row == -hstep);
2228   assert(diag_step.col == hstep || diag_step.col == -hstep);
2229   const int tr = this_mv.row;
2230   const int tc = this_mv.col;
2231   const int br = best_mv->row;
2232   const int bc = best_mv->col;
2233   int dummy = 0;
2234   if (tr != br && tc != bc) {
2235     assert(diag_step.col == bc - tc);
2236     assert(diag_step.row == br - tr);
2237     const MV chess_mv_1 = { br, bc + diag_step.col };
2238     const MV chess_mv_2 = { br + diag_step.row, bc };
2239     check_better_fast(&chess_mv_1, best_mv, mv_limits, var_params,
2240                       mv_cost_params, besterr, sse1, distortion, &dummy);
2241 
2242     check_better_fast(&chess_mv_2, best_mv, mv_limits, var_params,
2243                       mv_cost_params, besterr, sse1, distortion, &dummy);
2244   } else if (tr == br && tc != bc) {
2245     assert(diag_step.col == bc - tc);
2246     // Continue searching in the best direction
2247     const MV bottom_long_mv = { br + hstep, bc + diag_step.col };
2248     const MV top_long_mv = { br - hstep, bc + diag_step.col };
2249     check_better_fast(&bottom_long_mv, best_mv, mv_limits, var_params,
2250                       mv_cost_params, besterr, sse1, distortion, &dummy);
2251     check_better_fast(&top_long_mv, best_mv, mv_limits, var_params,
2252                       mv_cost_params, besterr, sse1, distortion, &dummy);
2253 
2254     // Search in the direction opposite of the best quadrant
2255     const MV rev_mv = { br - diag_step.row, bc };
2256     check_better_fast(&rev_mv, best_mv, mv_limits, var_params, mv_cost_params,
2257                       besterr, sse1, distortion, &dummy);
2258   } else if (tr != br && tc == bc) {
2259     assert(diag_step.row == br - tr);
2260     // Continue searching in the best direction
2261     const MV right_long_mv = { br + diag_step.row, bc + hstep };
2262     const MV left_long_mv = { br + diag_step.row, bc - hstep };
2263     check_better_fast(&right_long_mv, best_mv, mv_limits, var_params,
2264                       mv_cost_params, besterr, sse1, distortion, &dummy);
2265     check_better_fast(&left_long_mv, best_mv, mv_limits, var_params,
2266                       mv_cost_params, besterr, sse1, distortion, &dummy);
2267 
2268     // Search in the direction opposite of the best quadrant
2269     const MV rev_mv = { br, bc - diag_step.col };
2270     check_better_fast(&rev_mv, best_mv, mv_limits, var_params, mv_cost_params,
2271                       besterr, sse1, distortion, &dummy);
2272   }
2273 }
2274 
2275 // Combines first level check and second level check when applicable. This first
2276 // searches the four cardinal directions, and perform several
2277 // diagonal/chess-pattern searches in the best quadrant.
two_level_checks_fast(const MV this_mv,MV * best_mv,int hstep,const SubpelMvLimits * mv_limits,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * besterr,unsigned int * sse1,int * distortion,int iters)2278 static AOM_FORCE_INLINE void two_level_checks_fast(
2279     const MV this_mv, MV *best_mv, int hstep, const SubpelMvLimits *mv_limits,
2280     const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2281     const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
2282     unsigned int *sse1, int *distortion, int iters) {
2283   const MV diag_step =
2284       first_level_check_fast(this_mv, best_mv, hstep, mv_limits, var_params,
2285                              mv_cost_params, besterr, sse1, distortion);
2286   if (iters > 1) {
2287     second_level_check_fast(this_mv, diag_step, best_mv, hstep, mv_limits,
2288                             var_params, mv_cost_params, besterr, sse1,
2289                             distortion);
2290   }
2291 }
2292 
2293 static AOM_FORCE_INLINE MV
first_level_check(MACROBLOCKD * xd,const AV1_COMMON * const cm,const MV this_mv,MV * best_mv,const int hstep,const SubpelMvLimits * mv_limits,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * besterr,unsigned int * sse1,int * distortion)2294 first_level_check(MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV this_mv,
2295                   MV *best_mv, const int hstep, const SubpelMvLimits *mv_limits,
2296                   const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2297                   const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
2298                   unsigned int *sse1, int *distortion) {
2299   int dummy = 0;
2300   const MV left_mv = { this_mv.row, this_mv.col - hstep };
2301   const MV right_mv = { this_mv.row, this_mv.col + hstep };
2302   const MV top_mv = { this_mv.row - hstep, this_mv.col };
2303   const MV bottom_mv = { this_mv.row + hstep, this_mv.col };
2304 
2305   const unsigned int left =
2306       check_better(xd, cm, &left_mv, best_mv, mv_limits, var_params,
2307                    mv_cost_params, besterr, sse1, distortion, &dummy);
2308   const unsigned int right =
2309       check_better(xd, cm, &right_mv, best_mv, mv_limits, var_params,
2310                    mv_cost_params, besterr, sse1, distortion, &dummy);
2311   const unsigned int up =
2312       check_better(xd, cm, &top_mv, best_mv, mv_limits, var_params,
2313                    mv_cost_params, besterr, sse1, distortion, &dummy);
2314   const unsigned int down =
2315       check_better(xd, cm, &bottom_mv, best_mv, mv_limits, var_params,
2316                    mv_cost_params, besterr, sse1, distortion, &dummy);
2317 
2318   const MV diag_step = get_best_diag_step(hstep, left, right, up, down);
2319   const MV diag_mv = { this_mv.row + diag_step.row,
2320                        this_mv.col + diag_step.col };
2321 
2322   // Check the diagonal direction with the best mv
2323   check_better(xd, cm, &diag_mv, best_mv, mv_limits, var_params, mv_cost_params,
2324                besterr, sse1, distortion, &dummy);
2325 
2326   return diag_step;
2327 }
2328 
2329 // A newer version of second level check that gives better quality.
2330 // TODO(chiyotsai@google.com): evaluate this on subpel_search_types different
2331 // from av1_find_best_sub_pixel_tree
second_level_check_v2(MACROBLOCKD * xd,const AV1_COMMON * const cm,const MV this_mv,MV diag_step,MV * best_mv,const SubpelMvLimits * mv_limits,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * besterr,unsigned int * sse1,int * distortion)2332 static AOM_FORCE_INLINE void second_level_check_v2(
2333     MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV this_mv, MV diag_step,
2334     MV *best_mv, const SubpelMvLimits *mv_limits,
2335     const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2336     const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
2337     unsigned int *sse1, int *distortion) {
2338   assert(best_mv->row == this_mv.row + diag_step.row ||
2339          best_mv->col == this_mv.col + diag_step.col);
2340   if (CHECK_MV_EQUAL(this_mv, *best_mv)) {
2341     return;
2342   } else if (this_mv.row == best_mv->row) {
2343     // Search away from diagonal step since diagonal search did not provide any
2344     // improvement
2345     diag_step.row *= -1;
2346   } else if (this_mv.col == best_mv->col) {
2347     diag_step.col *= -1;
2348   }
2349 
2350   const MV row_bias_mv = { best_mv->row + diag_step.row, best_mv->col };
2351   const MV col_bias_mv = { best_mv->row, best_mv->col + diag_step.col };
2352   const MV diag_bias_mv = { best_mv->row + diag_step.row,
2353                             best_mv->col + diag_step.col };
2354   int has_better_mv = 0;
2355 
2356   if (var_params->subpel_search_type != USE_2_TAPS_ORIG) {
2357     check_better(xd, cm, &row_bias_mv, best_mv, mv_limits, var_params,
2358                  mv_cost_params, besterr, sse1, distortion, &has_better_mv);
2359     check_better(xd, cm, &col_bias_mv, best_mv, mv_limits, var_params,
2360                  mv_cost_params, besterr, sse1, distortion, &has_better_mv);
2361 
2362     // Do an additional search if the second iteration gives a better mv
2363     if (has_better_mv) {
2364       check_better(xd, cm, &diag_bias_mv, best_mv, mv_limits, var_params,
2365                    mv_cost_params, besterr, sse1, distortion, &has_better_mv);
2366     }
2367   } else {
2368     check_better_fast(&row_bias_mv, best_mv, mv_limits, var_params,
2369                       mv_cost_params, besterr, sse1, distortion,
2370                       &has_better_mv);
2371     check_better_fast(&col_bias_mv, best_mv, mv_limits, var_params,
2372                       mv_cost_params, besterr, sse1, distortion,
2373                       &has_better_mv);
2374 
2375     // Do an additional search if the second iteration gives a better mv
2376     if (has_better_mv) {
2377       check_better_fast(&diag_bias_mv, best_mv, mv_limits, var_params,
2378                         mv_cost_params, besterr, sse1, distortion,
2379                         &has_better_mv);
2380     }
2381   }
2382 }
2383 
2384 // Gets the error at the beginning when the mv has fullpel precision
setup_center_error(const MACROBLOCKD * xd,const MV * bestmv,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * sse1,int * distortion)2385 static unsigned int setup_center_error(
2386     const MACROBLOCKD *xd, const MV *bestmv,
2387     const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2388     const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion) {
2389   const aom_variance_fn_ptr_t *vfp = var_params->vfp;
2390   const int w = var_params->w;
2391   const int h = var_params->h;
2392 
2393   const MSBuffers *ms_buffers = &var_params->ms_buffers;
2394   const uint8_t *src = ms_buffers->src->buf;
2395   const uint8_t *y = get_buf_from_mv(ms_buffers->ref, *bestmv);
2396   const int src_stride = ms_buffers->src->stride;
2397   const int y_stride = ms_buffers->ref->stride;
2398   const uint8_t *second_pred = ms_buffers->second_pred;
2399   const uint8_t *mask = ms_buffers->mask;
2400   const int mask_stride = ms_buffers->mask_stride;
2401   const int invert_mask = ms_buffers->inv_mask;
2402 
2403   unsigned int besterr;
2404 
2405   if (second_pred != NULL) {
2406 #if CONFIG_AV1_HIGHBITDEPTH
2407     if (is_cur_buf_hbd(xd)) {
2408       DECLARE_ALIGNED(16, uint16_t, comp_pred16[MAX_SB_SQUARE]);
2409       uint8_t *comp_pred = CONVERT_TO_BYTEPTR(comp_pred16);
2410       if (mask) {
2411         aom_highbd_comp_mask_pred(comp_pred, second_pred, w, h, y, y_stride,
2412                                   mask, mask_stride, invert_mask);
2413       } else {
2414         aom_highbd_comp_avg_pred(comp_pred, second_pred, w, h, y, y_stride);
2415       }
2416       besterr = vfp->vf(comp_pred, w, src, src_stride, sse1);
2417     } else {
2418       DECLARE_ALIGNED(16, uint8_t, comp_pred[MAX_SB_SQUARE]);
2419       if (mask) {
2420         aom_comp_mask_pred(comp_pred, second_pred, w, h, y, y_stride, mask,
2421                            mask_stride, invert_mask);
2422       } else {
2423         aom_comp_avg_pred(comp_pred, second_pred, w, h, y, y_stride);
2424       }
2425       besterr = vfp->vf(comp_pred, w, src, src_stride, sse1);
2426     }
2427 #else
2428     (void)xd;
2429     DECLARE_ALIGNED(16, uint8_t, comp_pred[MAX_SB_SQUARE]);
2430     if (mask) {
2431       aom_comp_mask_pred(comp_pred, second_pred, w, h, y, y_stride, mask,
2432                          mask_stride, invert_mask);
2433     } else {
2434       aom_comp_avg_pred(comp_pred, second_pred, w, h, y, y_stride);
2435     }
2436     besterr = vfp->vf(comp_pred, w, src, src_stride, sse1);
2437 #endif
2438   } else {
2439     besterr = vfp->vf(y, y_stride, src, src_stride, sse1);
2440   }
2441   *distortion = besterr;
2442   besterr += mv_err_cost_(bestmv, mv_cost_params);
2443   return besterr;
2444 }
2445 
2446 // Gets the error at the beginning when the mv has fullpel precision
upsampled_setup_center_error(MACROBLOCKD * xd,const AV1_COMMON * const cm,const MV * bestmv,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * sse1,int * distortion)2447 static unsigned int upsampled_setup_center_error(
2448     MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV *bestmv,
2449     const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2450     const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion) {
2451   unsigned int besterr = upsampled_pref_error(xd, cm, bestmv, var_params, sse1);
2452   *distortion = besterr;
2453   besterr += mv_err_cost_(bestmv, mv_cost_params);
2454   return besterr;
2455 }
2456 
divide_and_round(int n,int d)2457 static INLINE int divide_and_round(int n, int d) {
2458   return ((n < 0) ^ (d < 0)) ? ((n - d / 2) / d) : ((n + d / 2) / d);
2459 }
2460 
is_cost_list_wellbehaved(const int * cost_list)2461 static INLINE int is_cost_list_wellbehaved(const int *cost_list) {
2462   return cost_list[0] < cost_list[1] && cost_list[0] < cost_list[2] &&
2463          cost_list[0] < cost_list[3] && cost_list[0] < cost_list[4];
2464 }
2465 
2466 // Returns surface minima estimate at given precision in 1/2^n bits.
2467 // Assume a model for the cost surface: S = A(x - x0)^2 + B(y - y0)^2 + C
2468 // For a given set of costs S0, S1, S2, S3, S4 at points
2469 // (y, x) = (0, 0), (0, -1), (1, 0), (0, 1) and (-1, 0) respectively,
2470 // the solution for the location of the minima (x0, y0) is given by:
2471 // x0 = 1/2 (S1 - S3)/(S1 + S3 - 2*S0),
2472 // y0 = 1/2 (S4 - S2)/(S4 + S2 - 2*S0).
2473 // The code below is an integerized version of that.
get_cost_surf_min(const int * cost_list,int * ir,int * ic,int bits)2474 static AOM_INLINE void get_cost_surf_min(const int *cost_list, int *ir, int *ic,
2475                                          int bits) {
2476   *ic = divide_and_round((cost_list[1] - cost_list[3]) * (1 << (bits - 1)),
2477                          (cost_list[1] - 2 * cost_list[0] + cost_list[3]));
2478   *ir = divide_and_round((cost_list[4] - cost_list[2]) * (1 << (bits - 1)),
2479                          (cost_list[4] - 2 * cost_list[0] + cost_list[2]));
2480 }
2481 
2482 // Checks the list of mvs searched in the last iteration and see if we are
2483 // repeating it. If so, return 1. Otherwise we update the last_mv_search_list
2484 // with current_mv and return 0.
check_repeated_mv_and_update(int_mv * last_mv_search_list,const MV current_mv,int iter)2485 static INLINE int check_repeated_mv_and_update(int_mv *last_mv_search_list,
2486                                                const MV current_mv, int iter) {
2487   if (last_mv_search_list) {
2488     if (CHECK_MV_EQUAL(last_mv_search_list[iter].as_mv, current_mv)) {
2489       return 1;
2490     }
2491 
2492     last_mv_search_list[iter].as_mv = current_mv;
2493   }
2494   return 0;
2495 }
2496 
av1_find_best_sub_pixel_tree_pruned_evenmore(MACROBLOCKD * xd,const AV1_COMMON * const cm,const SUBPEL_MOTION_SEARCH_PARAMS * ms_params,MV start_mv,MV * bestmv,int * distortion,unsigned int * sse1,int_mv * last_mv_search_list)2497 int av1_find_best_sub_pixel_tree_pruned_evenmore(
2498     MACROBLOCKD *xd, const AV1_COMMON *const cm,
2499     const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, MV start_mv, MV *bestmv,
2500     int *distortion, unsigned int *sse1, int_mv *last_mv_search_list) {
2501   (void)cm;
2502   const int allow_hp = ms_params->allow_hp;
2503   const int forced_stop = ms_params->forced_stop;
2504   const int iters_per_step = ms_params->iters_per_step;
2505   const int *cost_list = ms_params->cost_list;
2506   const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
2507   const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
2508   const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params;
2509 
2510   // The iteration we are current searching for. Iter 0 corresponds to fullpel
2511   // mv, iter 1 to half pel, and so on
2512   int iter = 0;
2513   int hstep = INIT_SUBPEL_STEP_SIZE;  // Step size, initialized to 4/8=1/2 pel
2514   unsigned int besterr = INT_MAX;
2515   *bestmv = start_mv;
2516 
2517   besterr = setup_center_error(xd, bestmv, var_params, mv_cost_params, sse1,
2518                                distortion);
2519 
2520   if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2521     return INT_MAX;
2522   }
2523   iter++;
2524 
2525   if (cost_list && cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
2526       cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
2527       cost_list[4] != INT_MAX && is_cost_list_wellbehaved(cost_list)) {
2528     int ir, ic;
2529     int dummy = 0;
2530     get_cost_surf_min(cost_list, &ir, &ic, 2);
2531     if (ir != 0 || ic != 0) {
2532       const MV this_mv = { start_mv.row + 2 * ir, start_mv.col + 2 * ic };
2533       check_better_fast(&this_mv, bestmv, mv_limits, var_params, mv_cost_params,
2534                         &besterr, sse1, distortion, &dummy);
2535     }
2536   } else {
2537     two_level_checks_fast(start_mv, bestmv, hstep, mv_limits, var_params,
2538                           mv_cost_params, &besterr, sse1, distortion,
2539                           iters_per_step);
2540 
2541     // Each subsequent iteration checks at least one point in common with
2542     // the last iteration could be 2 ( if diag selected) 1/4 pel
2543     if (forced_stop != HALF_PEL) {
2544       if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2545         return INT_MAX;
2546       }
2547       iter++;
2548 
2549       hstep >>= 1;
2550       start_mv = *bestmv;
2551       two_level_checks_fast(start_mv, bestmv, hstep, mv_limits, var_params,
2552                             mv_cost_params, &besterr, sse1, distortion,
2553                             iters_per_step);
2554     }
2555   }
2556 
2557   if (allow_hp && forced_stop == EIGHTH_PEL) {
2558     if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2559       return INT_MAX;
2560     }
2561     iter++;
2562 
2563     hstep >>= 1;
2564     start_mv = *bestmv;
2565     two_level_checks_fast(start_mv, bestmv, hstep, mv_limits, var_params,
2566                           mv_cost_params, &besterr, sse1, distortion,
2567                           iters_per_step);
2568   }
2569 
2570   return besterr;
2571 }
2572 
av1_find_best_sub_pixel_tree_pruned_more(MACROBLOCKD * xd,const AV1_COMMON * const cm,const SUBPEL_MOTION_SEARCH_PARAMS * ms_params,MV start_mv,MV * bestmv,int * distortion,unsigned int * sse1,int_mv * last_mv_search_list)2573 int av1_find_best_sub_pixel_tree_pruned_more(
2574     MACROBLOCKD *xd, const AV1_COMMON *const cm,
2575     const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, MV start_mv, MV *bestmv,
2576     int *distortion, unsigned int *sse1, int_mv *last_mv_search_list) {
2577   (void)cm;
2578   const int allow_hp = ms_params->allow_hp;
2579   const int forced_stop = ms_params->forced_stop;
2580   const int iters_per_step = ms_params->iters_per_step;
2581   const int *cost_list = ms_params->cost_list;
2582   const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
2583   const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
2584   const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params;
2585 
2586   // The iteration we are current searching for. Iter 0 corresponds to fullpel
2587   // mv, iter 1 to half pel, and so on
2588   int iter = 0;
2589   int hstep = INIT_SUBPEL_STEP_SIZE;  // Step size, initialized to 4/8=1/2 pel
2590   unsigned int besterr = INT_MAX;
2591   *bestmv = start_mv;
2592 
2593   besterr = setup_center_error(xd, bestmv, var_params, mv_cost_params, sse1,
2594                                distortion);
2595 
2596   if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2597     return INT_MAX;
2598   }
2599   iter++;
2600 
2601   if (cost_list && cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
2602       cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
2603       cost_list[4] != INT_MAX && is_cost_list_wellbehaved(cost_list)) {
2604     int ir, ic;
2605     get_cost_surf_min(cost_list, &ir, &ic, 1);
2606     if (ir != 0 || ic != 0) {
2607       const MV this_mv = { start_mv.row + ir * hstep,
2608                            start_mv.col + ic * hstep };
2609       int dummy = 0;
2610       check_better_fast(&this_mv, bestmv, mv_limits, var_params, mv_cost_params,
2611                         &besterr, sse1, distortion, &dummy);
2612     }
2613   } else {
2614     two_level_checks_fast(start_mv, bestmv, hstep, mv_limits, var_params,
2615                           mv_cost_params, &besterr, sse1, distortion,
2616                           iters_per_step);
2617   }
2618 
2619   // Each subsequent iteration checks at least one point in common with
2620   // the last iteration could be 2 ( if diag selected) 1/4 pel
2621   if (forced_stop != HALF_PEL) {
2622     if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2623       return INT_MAX;
2624     }
2625     iter++;
2626 
2627     hstep >>= 1;
2628     start_mv = *bestmv;
2629     two_level_checks_fast(start_mv, bestmv, hstep, mv_limits, var_params,
2630                           mv_cost_params, &besterr, sse1, distortion,
2631                           iters_per_step);
2632   }
2633 
2634   if (allow_hp && forced_stop == EIGHTH_PEL) {
2635     if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2636       return INT_MAX;
2637     }
2638     iter++;
2639 
2640     hstep >>= 1;
2641     start_mv = *bestmv;
2642     two_level_checks_fast(start_mv, bestmv, hstep, mv_limits, var_params,
2643                           mv_cost_params, &besterr, sse1, distortion,
2644                           iters_per_step);
2645   }
2646 
2647   return besterr;
2648 }
2649 
av1_find_best_sub_pixel_tree_pruned(MACROBLOCKD * xd,const AV1_COMMON * const cm,const SUBPEL_MOTION_SEARCH_PARAMS * ms_params,MV start_mv,MV * bestmv,int * distortion,unsigned int * sse1,int_mv * last_mv_search_list)2650 int av1_find_best_sub_pixel_tree_pruned(
2651     MACROBLOCKD *xd, const AV1_COMMON *const cm,
2652     const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, MV start_mv, MV *bestmv,
2653     int *distortion, unsigned int *sse1, int_mv *last_mv_search_list) {
2654   (void)cm;
2655   const int allow_hp = ms_params->allow_hp;
2656   const int forced_stop = ms_params->forced_stop;
2657   const int iters_per_step = ms_params->iters_per_step;
2658   const int *cost_list = ms_params->cost_list;
2659   const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
2660   const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
2661   const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params;
2662 
2663   // The iteration we are current searching for. Iter 0 corresponds to fullpel
2664   // mv, iter 1 to half pel, and so on
2665   int iter = 0;
2666   int hstep = INIT_SUBPEL_STEP_SIZE;  // Step size, initialized to 4/8=1/2 pel
2667   unsigned int besterr = INT_MAX;
2668   *bestmv = start_mv;
2669 
2670   besterr = setup_center_error(xd, bestmv, var_params, mv_cost_params, sse1,
2671                                distortion);
2672   if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2673     return INT_MAX;
2674   }
2675   iter++;
2676 
2677   if (cost_list && cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
2678       cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
2679       cost_list[4] != INT_MAX) {
2680     const unsigned int whichdir = (cost_list[1] < cost_list[3] ? 0 : 1) +
2681                                   (cost_list[2] < cost_list[4] ? 0 : 2);
2682 
2683     const MV left_mv = { start_mv.row, start_mv.col - hstep };
2684     const MV right_mv = { start_mv.row, start_mv.col + hstep };
2685     const MV bottom_mv = { start_mv.row + hstep, start_mv.col };
2686     const MV top_mv = { start_mv.row - hstep, start_mv.col };
2687 
2688     const MV bottom_left_mv = { start_mv.row + hstep, start_mv.col - hstep };
2689     const MV bottom_right_mv = { start_mv.row + hstep, start_mv.col + hstep };
2690     const MV top_left_mv = { start_mv.row - hstep, start_mv.col - hstep };
2691     const MV top_right_mv = { start_mv.row - hstep, start_mv.col + hstep };
2692 
2693     int dummy = 0;
2694 
2695     switch (whichdir) {
2696       case 0:  // bottom left quadrant
2697         check_better_fast(&left_mv, bestmv, mv_limits, var_params,
2698                           mv_cost_params, &besterr, sse1, distortion, &dummy);
2699         check_better_fast(&bottom_mv, bestmv, mv_limits, var_params,
2700                           mv_cost_params, &besterr, sse1, distortion, &dummy);
2701         check_better_fast(&bottom_left_mv, bestmv, mv_limits, var_params,
2702                           mv_cost_params, &besterr, sse1, distortion, &dummy);
2703         break;
2704       case 1:  // bottom right quadrant
2705         check_better_fast(&right_mv, bestmv, mv_limits, var_params,
2706                           mv_cost_params, &besterr, sse1, distortion, &dummy);
2707         check_better_fast(&bottom_mv, bestmv, mv_limits, var_params,
2708                           mv_cost_params, &besterr, sse1, distortion, &dummy);
2709         check_better_fast(&bottom_right_mv, bestmv, mv_limits, var_params,
2710                           mv_cost_params, &besterr, sse1, distortion, &dummy);
2711         break;
2712       case 2:  // top left quadrant
2713         check_better_fast(&left_mv, bestmv, mv_limits, var_params,
2714                           mv_cost_params, &besterr, sse1, distortion, &dummy);
2715         check_better_fast(&top_mv, bestmv, mv_limits, var_params,
2716                           mv_cost_params, &besterr, sse1, distortion, &dummy);
2717         check_better_fast(&top_left_mv, bestmv, mv_limits, var_params,
2718                           mv_cost_params, &besterr, sse1, distortion, &dummy);
2719         break;
2720       case 3:  // top right quadrant
2721         check_better_fast(&right_mv, bestmv, mv_limits, var_params,
2722                           mv_cost_params, &besterr, sse1, distortion, &dummy);
2723         check_better_fast(&top_mv, bestmv, mv_limits, var_params,
2724                           mv_cost_params, &besterr, sse1, distortion, &dummy);
2725         check_better_fast(&top_right_mv, bestmv, mv_limits, var_params,
2726                           mv_cost_params, &besterr, sse1, distortion, &dummy);
2727         break;
2728     }
2729   } else {
2730     two_level_checks_fast(start_mv, bestmv, hstep, mv_limits, var_params,
2731                           mv_cost_params, &besterr, sse1, distortion,
2732                           iters_per_step);
2733   }
2734 
2735   // Each subsequent iteration checks at least one point in common with
2736   // the last iteration could be 2 ( if diag selected) 1/4 pel
2737   if (forced_stop != HALF_PEL) {
2738     if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2739       return INT_MAX;
2740     }
2741     iter++;
2742 
2743     hstep >>= 1;
2744     start_mv = *bestmv;
2745     two_level_checks_fast(start_mv, bestmv, hstep, mv_limits, var_params,
2746                           mv_cost_params, &besterr, sse1, distortion,
2747                           iters_per_step);
2748   }
2749 
2750   if (allow_hp && forced_stop == EIGHTH_PEL) {
2751     if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2752       return INT_MAX;
2753     }
2754     iter++;
2755 
2756     hstep >>= 1;
2757     start_mv = *bestmv;
2758     two_level_checks_fast(start_mv, bestmv, hstep, mv_limits, var_params,
2759                           mv_cost_params, &besterr, sse1, distortion,
2760                           iters_per_step);
2761   }
2762 
2763   return besterr;
2764 }
2765 
av1_find_best_sub_pixel_tree(MACROBLOCKD * xd,const AV1_COMMON * const cm,const SUBPEL_MOTION_SEARCH_PARAMS * ms_params,MV start_mv,MV * bestmv,int * distortion,unsigned int * sse1,int_mv * last_mv_search_list)2766 int av1_find_best_sub_pixel_tree(MACROBLOCKD *xd, const AV1_COMMON *const cm,
2767                                  const SUBPEL_MOTION_SEARCH_PARAMS *ms_params,
2768                                  MV start_mv, MV *bestmv, int *distortion,
2769                                  unsigned int *sse1,
2770                                  int_mv *last_mv_search_list) {
2771   const int allow_hp = ms_params->allow_hp;
2772   const int forced_stop = ms_params->forced_stop;
2773   const int iters_per_step = ms_params->iters_per_step;
2774   const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
2775   const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params;
2776   const SUBPEL_SEARCH_TYPE subpel_search_type =
2777       ms_params->var_params.subpel_search_type;
2778   const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
2779 
2780   // How many steps to take. A round of 0 means fullpel search only, 1 means
2781   // half-pel, and so on.
2782   const int round = AOMMIN(FULL_PEL - forced_stop, 3 - !allow_hp);
2783   int hstep = INIT_SUBPEL_STEP_SIZE;  // Step size, initialized to 4/8=1/2 pel
2784 
2785   unsigned int besterr = INT_MAX;
2786 
2787   *bestmv = start_mv;
2788 
2789   if (subpel_search_type != USE_2_TAPS_ORIG) {
2790     besterr = upsampled_setup_center_error(xd, cm, bestmv, var_params,
2791                                            mv_cost_params, sse1, distortion);
2792   } else {
2793     besterr = setup_center_error(xd, bestmv, var_params, mv_cost_params, sse1,
2794                                  distortion);
2795   }
2796 
2797   for (int iter = 0; iter < round; ++iter) {
2798     MV iter_center_mv = *bestmv;
2799     if (check_repeated_mv_and_update(last_mv_search_list, iter_center_mv,
2800                                      iter)) {
2801       return INT_MAX;
2802     }
2803 
2804     MV diag_step;
2805     if (subpel_search_type != USE_2_TAPS_ORIG) {
2806       diag_step = first_level_check(xd, cm, iter_center_mv, bestmv, hstep,
2807                                     mv_limits, var_params, mv_cost_params,
2808                                     &besterr, sse1, distortion);
2809     } else {
2810       diag_step = first_level_check_fast(iter_center_mv, bestmv, hstep,
2811                                          mv_limits, var_params, mv_cost_params,
2812                                          &besterr, sse1, distortion);
2813     }
2814 
2815     // Check diagonal sub-pixel position
2816     if (!CHECK_MV_EQUAL(iter_center_mv, *bestmv) && iters_per_step > 1) {
2817       second_level_check_v2(xd, cm, iter_center_mv, diag_step, bestmv,
2818                             mv_limits, var_params, mv_cost_params, &besterr,
2819                             sse1, distortion);
2820     }
2821 
2822     hstep >>= 1;
2823   }
2824 
2825   return besterr;
2826 }
2827 
2828 // Note(yunqingwang): The following 2 functions are only used in the motion
2829 // vector unit test, which return extreme motion vectors allowed by the MV
2830 // limits.
2831 // Returns the maximum MV.
av1_return_max_sub_pixel_mv(MACROBLOCKD * xd,const AV1_COMMON * const cm,const SUBPEL_MOTION_SEARCH_PARAMS * ms_params,MV start_mv,MV * bestmv,int * distortion,unsigned int * sse1,int_mv * last_mv_search_list)2832 int av1_return_max_sub_pixel_mv(MACROBLOCKD *xd, const AV1_COMMON *const cm,
2833                                 const SUBPEL_MOTION_SEARCH_PARAMS *ms_params,
2834                                 MV start_mv, MV *bestmv, int *distortion,
2835                                 unsigned int *sse1,
2836                                 int_mv *last_mv_search_list) {
2837   (void)xd;
2838   (void)cm;
2839   (void)start_mv;
2840   (void)sse1;
2841   (void)distortion;
2842   (void)last_mv_search_list;
2843 
2844   const int allow_hp = ms_params->allow_hp;
2845   const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
2846 
2847   bestmv->row = mv_limits->row_max;
2848   bestmv->col = mv_limits->col_max;
2849 
2850   unsigned int besterr = 0;
2851 
2852   // In the sub-pel motion search, if hp is not used, then the last bit of mv
2853   // has to be 0.
2854   lower_mv_precision(bestmv, allow_hp, 0);
2855   return besterr;
2856 }
2857 
2858 // Returns the minimum MV.
av1_return_min_sub_pixel_mv(MACROBLOCKD * xd,const AV1_COMMON * const cm,const SUBPEL_MOTION_SEARCH_PARAMS * ms_params,MV start_mv,MV * bestmv,int * distortion,unsigned int * sse1,int_mv * last_mv_search_list)2859 int av1_return_min_sub_pixel_mv(MACROBLOCKD *xd, const AV1_COMMON *const cm,
2860                                 const SUBPEL_MOTION_SEARCH_PARAMS *ms_params,
2861                                 MV start_mv, MV *bestmv, int *distortion,
2862                                 unsigned int *sse1,
2863                                 int_mv *last_mv_search_list) {
2864   (void)xd;
2865   (void)cm;
2866   (void)start_mv;
2867   (void)sse1;
2868   (void)distortion;
2869   (void)last_mv_search_list;
2870 
2871   const int allow_hp = ms_params->allow_hp;
2872   const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
2873 
2874   bestmv->row = mv_limits->row_min;
2875   bestmv->col = mv_limits->col_min;
2876 
2877   unsigned int besterr = 0;
2878   // In the sub-pel motion search, if hp is not used, then the last bit of mv
2879   // has to be 0.
2880   lower_mv_precision(bestmv, allow_hp, 0);
2881   return besterr;
2882 }
2883 
2884 // Computes the cost of the current predictor by going through the whole
2885 // av1_enc_build_inter_predictor pipeline. This is mainly used by warped mv
2886 // during motion_mode_rd. We are going through the whole
2887 // av1_enc_build_inter_predictor because we might have changed the interpolation
2888 // filter, etc before motion_mode_rd is called.
compute_motion_cost(MACROBLOCKD * xd,const AV1_COMMON * const cm,const SUBPEL_MOTION_SEARCH_PARAMS * ms_params,BLOCK_SIZE bsize,const MV * this_mv)2889 static INLINE unsigned int compute_motion_cost(
2890     MACROBLOCKD *xd, const AV1_COMMON *const cm,
2891     const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, BLOCK_SIZE bsize,
2892     const MV *this_mv) {
2893   unsigned int mse;
2894   unsigned int sse;
2895   const int mi_row = xd->mi_row;
2896   const int mi_col = xd->mi_col;
2897 
2898   av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
2899                                 AOM_PLANE_Y, AOM_PLANE_Y);
2900 
2901   const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params;
2902   const MSBuffers *ms_buffers = &var_params->ms_buffers;
2903 
2904   const uint8_t *const src = ms_buffers->src->buf;
2905   const int src_stride = ms_buffers->src->stride;
2906   const uint8_t *const dst = xd->plane[0].dst.buf;
2907   const int dst_stride = xd->plane[0].dst.stride;
2908   const aom_variance_fn_ptr_t *vfp = ms_params->var_params.vfp;
2909 
2910   mse = vfp->vf(dst, dst_stride, src, src_stride, &sse);
2911   mse += mv_err_cost_(this_mv, &ms_params->mv_cost_params);
2912   return mse;
2913 }
2914 
2915 // Refines MV in a small range
av1_refine_warped_mv(MACROBLOCKD * xd,const AV1_COMMON * const cm,const SUBPEL_MOTION_SEARCH_PARAMS * ms_params,BLOCK_SIZE bsize,const int * pts0,const int * pts_inref0,int total_samples)2916 unsigned int av1_refine_warped_mv(MACROBLOCKD *xd, const AV1_COMMON *const cm,
2917                                   const SUBPEL_MOTION_SEARCH_PARAMS *ms_params,
2918                                   BLOCK_SIZE bsize, const int *pts0,
2919                                   const int *pts_inref0, int total_samples) {
2920   MB_MODE_INFO *mbmi = xd->mi[0];
2921   static const MV neighbors[8] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 },
2922                                    { 0, -2 }, { 2, 0 }, { 0, 2 }, { -2, 0 } };
2923   MV *best_mv = &mbmi->mv[0].as_mv;
2924 
2925   WarpedMotionParams best_wm_params = mbmi->wm_params;
2926   int best_num_proj_ref = mbmi->num_proj_ref;
2927   unsigned int bestmse;
2928   const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
2929 
2930   const int start = ms_params->allow_hp ? 0 : 4;
2931 
2932   // Calculate the center position's error
2933   assert(av1_is_subpelmv_in_range(mv_limits, *best_mv));
2934   bestmse = compute_motion_cost(xd, cm, ms_params, bsize, best_mv);
2935 
2936   // MV search
2937   int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
2938   const int mi_row = xd->mi_row;
2939   const int mi_col = xd->mi_col;
2940   for (int ite = 0; ite < 2; ++ite) {
2941     int best_idx = -1;
2942 
2943     for (int idx = start; idx < start + 4; ++idx) {
2944       unsigned int thismse;
2945 
2946       MV this_mv = { best_mv->row + neighbors[idx].row,
2947                      best_mv->col + neighbors[idx].col };
2948       if (av1_is_subpelmv_in_range(mv_limits, this_mv)) {
2949         memcpy(pts, pts0, total_samples * 2 * sizeof(*pts0));
2950         memcpy(pts_inref, pts_inref0, total_samples * 2 * sizeof(*pts_inref0));
2951         if (total_samples > 1)
2952           mbmi->num_proj_ref =
2953               av1_selectSamples(&this_mv, pts, pts_inref, total_samples, bsize);
2954 
2955         if (!av1_find_projection(mbmi->num_proj_ref, pts, pts_inref, bsize,
2956                                  this_mv.row, this_mv.col, &mbmi->wm_params,
2957                                  mi_row, mi_col)) {
2958           thismse = compute_motion_cost(xd, cm, ms_params, bsize, &this_mv);
2959 
2960           if (thismse < bestmse) {
2961             best_idx = idx;
2962             best_wm_params = mbmi->wm_params;
2963             best_num_proj_ref = mbmi->num_proj_ref;
2964             bestmse = thismse;
2965           }
2966         }
2967       }
2968     }
2969 
2970     if (best_idx == -1) break;
2971 
2972     if (best_idx >= 0) {
2973       best_mv->row += neighbors[best_idx].row;
2974       best_mv->col += neighbors[best_idx].col;
2975     }
2976   }
2977 
2978   mbmi->wm_params = best_wm_params;
2979   mbmi->num_proj_ref = best_num_proj_ref;
2980   return bestmse;
2981 }
2982 // =============================================================================
2983 //  Subpixel Motion Search: OBMC
2984 // =============================================================================
2985 // Estimates the variance of prediction residue
estimate_obmc_pref_error(const MV * this_mv,const SUBPEL_SEARCH_VAR_PARAMS * var_params,unsigned int * sse)2986 static INLINE int estimate_obmc_pref_error(
2987     const MV *this_mv, const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2988     unsigned int *sse) {
2989   const aom_variance_fn_ptr_t *vfp = var_params->vfp;
2990 
2991   const MSBuffers *ms_buffers = &var_params->ms_buffers;
2992   const int32_t *src = ms_buffers->wsrc;
2993   const int32_t *mask = ms_buffers->obmc_mask;
2994   const uint8_t *ref = get_buf_from_mv(ms_buffers->ref, *this_mv);
2995   const int ref_stride = ms_buffers->ref->stride;
2996 
2997   const int subpel_x_q3 = get_subpel_part(this_mv->col);
2998   const int subpel_y_q3 = get_subpel_part(this_mv->row);
2999 
3000   return vfp->osvf(ref, ref_stride, subpel_x_q3, subpel_y_q3, src, mask, sse);
3001 }
3002 
3003 // Calculates the variance of prediction residue
upsampled_obmc_pref_error(MACROBLOCKD * xd,const AV1_COMMON * cm,const MV * this_mv,const SUBPEL_SEARCH_VAR_PARAMS * var_params,unsigned int * sse)3004 static int upsampled_obmc_pref_error(MACROBLOCKD *xd, const AV1_COMMON *cm,
3005                                      const MV *this_mv,
3006                                      const SUBPEL_SEARCH_VAR_PARAMS *var_params,
3007                                      unsigned int *sse) {
3008   const aom_variance_fn_ptr_t *vfp = var_params->vfp;
3009   const SUBPEL_SEARCH_TYPE subpel_search_type = var_params->subpel_search_type;
3010   const int w = var_params->w;
3011   const int h = var_params->h;
3012 
3013   const MSBuffers *ms_buffers = &var_params->ms_buffers;
3014   const int32_t *wsrc = ms_buffers->wsrc;
3015   const int32_t *mask = ms_buffers->obmc_mask;
3016   const uint8_t *ref = get_buf_from_mv(ms_buffers->ref, *this_mv);
3017   const int ref_stride = ms_buffers->ref->stride;
3018 
3019   const int subpel_x_q3 = get_subpel_part(this_mv->col);
3020   const int subpel_y_q3 = get_subpel_part(this_mv->row);
3021 
3022   const int mi_row = xd->mi_row;
3023   const int mi_col = xd->mi_col;
3024 
3025   unsigned int besterr;
3026   DECLARE_ALIGNED(16, uint8_t, pred[2 * MAX_SB_SQUARE]);
3027 #if CONFIG_AV1_HIGHBITDEPTH
3028   if (is_cur_buf_hbd(xd)) {
3029     uint8_t *pred8 = CONVERT_TO_BYTEPTR(pred);
3030     aom_highbd_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred8, w, h,
3031                               subpel_x_q3, subpel_y_q3, ref, ref_stride, xd->bd,
3032                               subpel_search_type);
3033     besterr = vfp->ovf(pred8, w, wsrc, mask, sse);
3034   } else {
3035     aom_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred, w, h, subpel_x_q3,
3036                        subpel_y_q3, ref, ref_stride, subpel_search_type);
3037 
3038     besterr = vfp->ovf(pred, w, wsrc, mask, sse);
3039   }
3040 #else
3041   aom_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred, w, h, subpel_x_q3,
3042                      subpel_y_q3, ref, ref_stride, subpel_search_type);
3043 
3044   besterr = vfp->ovf(pred, w, wsrc, mask, sse);
3045 #endif
3046   return besterr;
3047 }
3048 
setup_obmc_center_error(const MV * this_mv,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * sse1,int * distortion)3049 static unsigned int setup_obmc_center_error(
3050     const MV *this_mv, const SUBPEL_SEARCH_VAR_PARAMS *var_params,
3051     const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion) {
3052   // TODO(chiyotsai@google.com): There might be a bug here where we didn't use
3053   // get_buf_from_mv(ref, *this_mv).
3054   const MSBuffers *ms_buffers = &var_params->ms_buffers;
3055   const int32_t *wsrc = ms_buffers->wsrc;
3056   const int32_t *mask = ms_buffers->obmc_mask;
3057   const uint8_t *ref = ms_buffers->ref->buf;
3058   const int ref_stride = ms_buffers->ref->stride;
3059   unsigned int besterr =
3060       var_params->vfp->ovf(ref, ref_stride, wsrc, mask, sse1);
3061   *distortion = besterr;
3062   besterr += mv_err_cost_(this_mv, mv_cost_params);
3063   return besterr;
3064 }
3065 
upsampled_setup_obmc_center_error(MACROBLOCKD * xd,const AV1_COMMON * const cm,const MV * this_mv,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * sse1,int * distortion)3066 static unsigned int upsampled_setup_obmc_center_error(
3067     MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV *this_mv,
3068     const SUBPEL_SEARCH_VAR_PARAMS *var_params,
3069     const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion) {
3070   unsigned int besterr =
3071       upsampled_obmc_pref_error(xd, cm, this_mv, var_params, sse1);
3072   *distortion = besterr;
3073   besterr += mv_err_cost_(this_mv, mv_cost_params);
3074   return besterr;
3075 }
3076 
3077 // Estimates the variance of prediction residue
3078 // TODO(chiyotsai@google.com): the cost does does not match the cost in
3079 // mv_cost_. Investigate this later.
estimate_obmc_mvcost(const MV * this_mv,const MV_COST_PARAMS * mv_cost_params)3080 static INLINE int estimate_obmc_mvcost(const MV *this_mv,
3081                                        const MV_COST_PARAMS *mv_cost_params) {
3082   const MV *ref_mv = mv_cost_params->ref_mv;
3083   const int *mvjcost = mv_cost_params->mvjcost;
3084   const int *const *mvcost = mv_cost_params->mvcost;
3085   const int error_per_bit = mv_cost_params->error_per_bit;
3086   const MV_COST_TYPE mv_cost_type = mv_cost_params->mv_cost_type;
3087   const MV diff_mv = { GET_MV_SUBPEL(this_mv->row - ref_mv->row),
3088                        GET_MV_SUBPEL(this_mv->col - ref_mv->col) };
3089 
3090   switch (mv_cost_type) {
3091     case MV_COST_ENTROPY:
3092       return (unsigned)((mv_cost(&diff_mv, mvjcost,
3093                                  CONVERT_TO_CONST_MVCOST(mvcost)) *
3094                              error_per_bit +
3095                          4096) >>
3096                         13);
3097     case MV_COST_NONE: return 0;
3098     default:
3099       assert(0 && "L1 norm is not tuned for estimated obmc mvcost");
3100       return 0;
3101   }
3102 }
3103 
3104 // Estimates whether this_mv is better than best_mv. This function incorporates
3105 // both prediction error and residue into account.
obmc_check_better_fast(const MV * this_mv,MV * best_mv,const SubpelMvLimits * mv_limits,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * besterr,unsigned int * sse1,int * distortion,int * has_better_mv)3106 static INLINE unsigned int obmc_check_better_fast(
3107     const MV *this_mv, MV *best_mv, const SubpelMvLimits *mv_limits,
3108     const SUBPEL_SEARCH_VAR_PARAMS *var_params,
3109     const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
3110     unsigned int *sse1, int *distortion, int *has_better_mv) {
3111   unsigned int cost;
3112   if (av1_is_subpelmv_in_range(mv_limits, *this_mv)) {
3113     unsigned int sse;
3114     const int thismse = estimate_obmc_pref_error(this_mv, var_params, &sse);
3115 
3116     cost = estimate_obmc_mvcost(this_mv, mv_cost_params);
3117     cost += thismse;
3118 
3119     if (cost < *besterr) {
3120       *besterr = cost;
3121       *best_mv = *this_mv;
3122       *distortion = thismse;
3123       *sse1 = sse;
3124       *has_better_mv |= 1;
3125     }
3126   } else {
3127     cost = INT_MAX;
3128   }
3129   return cost;
3130 }
3131 
3132 // Estimates whether this_mv is better than best_mv. This function incorporates
3133 // both prediction error and residue into account.
obmc_check_better(MACROBLOCKD * xd,const AV1_COMMON * cm,const MV * this_mv,MV * best_mv,const SubpelMvLimits * mv_limits,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * besterr,unsigned int * sse1,int * distortion,int * has_better_mv)3134 static INLINE unsigned int obmc_check_better(
3135     MACROBLOCKD *xd, const AV1_COMMON *cm, const MV *this_mv, MV *best_mv,
3136     const SubpelMvLimits *mv_limits, const SUBPEL_SEARCH_VAR_PARAMS *var_params,
3137     const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
3138     unsigned int *sse1, int *distortion, int *has_better_mv) {
3139   unsigned int cost;
3140   if (av1_is_subpelmv_in_range(mv_limits, *this_mv)) {
3141     unsigned int sse;
3142     const int thismse =
3143         upsampled_obmc_pref_error(xd, cm, this_mv, var_params, &sse);
3144     cost = mv_err_cost_(this_mv, mv_cost_params);
3145 
3146     cost += thismse;
3147 
3148     if (cost < *besterr) {
3149       *besterr = cost;
3150       *best_mv = *this_mv;
3151       *distortion = thismse;
3152       *sse1 = sse;
3153       *has_better_mv |= 1;
3154     }
3155   } else {
3156     cost = INT_MAX;
3157   }
3158   return cost;
3159 }
3160 
obmc_first_level_check(MACROBLOCKD * xd,const AV1_COMMON * const cm,const MV this_mv,MV * best_mv,const int hstep,const SubpelMvLimits * mv_limits,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * besterr,unsigned int * sse1,int * distortion)3161 static AOM_FORCE_INLINE MV obmc_first_level_check(
3162     MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV this_mv, MV *best_mv,
3163     const int hstep, const SubpelMvLimits *mv_limits,
3164     const SUBPEL_SEARCH_VAR_PARAMS *var_params,
3165     const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
3166     unsigned int *sse1, int *distortion) {
3167   int dummy = 0;
3168   const MV left_mv = { this_mv.row, this_mv.col - hstep };
3169   const MV right_mv = { this_mv.row, this_mv.col + hstep };
3170   const MV top_mv = { this_mv.row - hstep, this_mv.col };
3171   const MV bottom_mv = { this_mv.row + hstep, this_mv.col };
3172 
3173   if (var_params->subpel_search_type != USE_2_TAPS_ORIG) {
3174     const unsigned int left =
3175         obmc_check_better(xd, cm, &left_mv, best_mv, mv_limits, var_params,
3176                           mv_cost_params, besterr, sse1, distortion, &dummy);
3177     const unsigned int right =
3178         obmc_check_better(xd, cm, &right_mv, best_mv, mv_limits, var_params,
3179                           mv_cost_params, besterr, sse1, distortion, &dummy);
3180     const unsigned int up =
3181         obmc_check_better(xd, cm, &top_mv, best_mv, mv_limits, var_params,
3182                           mv_cost_params, besterr, sse1, distortion, &dummy);
3183     const unsigned int down =
3184         obmc_check_better(xd, cm, &bottom_mv, best_mv, mv_limits, var_params,
3185                           mv_cost_params, besterr, sse1, distortion, &dummy);
3186 
3187     const MV diag_step = get_best_diag_step(hstep, left, right, up, down);
3188     const MV diag_mv = { this_mv.row + diag_step.row,
3189                          this_mv.col + diag_step.col };
3190 
3191     // Check the diagonal direction with the best mv
3192     obmc_check_better(xd, cm, &diag_mv, best_mv, mv_limits, var_params,
3193                       mv_cost_params, besterr, sse1, distortion, &dummy);
3194 
3195     return diag_step;
3196   } else {
3197     const unsigned int left = obmc_check_better_fast(
3198         &left_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr, sse1,
3199         distortion, &dummy);
3200     const unsigned int right = obmc_check_better_fast(
3201         &right_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr,
3202         sse1, distortion, &dummy);
3203 
3204     const unsigned int up = obmc_check_better_fast(
3205         &top_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr, sse1,
3206         distortion, &dummy);
3207 
3208     const unsigned int down = obmc_check_better_fast(
3209         &bottom_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr,
3210         sse1, distortion, &dummy);
3211 
3212     const MV diag_step = get_best_diag_step(hstep, left, right, up, down);
3213     const MV diag_mv = { this_mv.row + diag_step.row,
3214                          this_mv.col + diag_step.col };
3215 
3216     // Check the diagonal direction with the best mv
3217     obmc_check_better_fast(&diag_mv, best_mv, mv_limits, var_params,
3218                            mv_cost_params, besterr, sse1, distortion, &dummy);
3219 
3220     return diag_step;
3221   }
3222 }
3223 
3224 // A newer version of second level check for obmc that gives better quality.
obmc_second_level_check_v2(MACROBLOCKD * xd,const AV1_COMMON * const cm,const MV this_mv,MV diag_step,MV * best_mv,const SubpelMvLimits * mv_limits,const SUBPEL_SEARCH_VAR_PARAMS * var_params,const MV_COST_PARAMS * mv_cost_params,unsigned int * besterr,unsigned int * sse1,int * distortion)3225 static AOM_FORCE_INLINE void obmc_second_level_check_v2(
3226     MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV this_mv, MV diag_step,
3227     MV *best_mv, const SubpelMvLimits *mv_limits,
3228     const SUBPEL_SEARCH_VAR_PARAMS *var_params,
3229     const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
3230     unsigned int *sse1, int *distortion) {
3231   assert(best_mv->row == this_mv.row + diag_step.row ||
3232          best_mv->col == this_mv.col + diag_step.col);
3233   if (CHECK_MV_EQUAL(this_mv, *best_mv)) {
3234     return;
3235   } else if (this_mv.row == best_mv->row) {
3236     // Search away from diagonal step since diagonal search did not provide any
3237     // improvement
3238     diag_step.row *= -1;
3239   } else if (this_mv.col == best_mv->col) {
3240     diag_step.col *= -1;
3241   }
3242 
3243   const MV row_bias_mv = { best_mv->row + diag_step.row, best_mv->col };
3244   const MV col_bias_mv = { best_mv->row, best_mv->col + diag_step.col };
3245   const MV diag_bias_mv = { best_mv->row + diag_step.row,
3246                             best_mv->col + diag_step.col };
3247   int has_better_mv = 0;
3248 
3249   if (var_params->subpel_search_type != USE_2_TAPS_ORIG) {
3250     obmc_check_better(xd, cm, &row_bias_mv, best_mv, mv_limits, var_params,
3251                       mv_cost_params, besterr, sse1, distortion,
3252                       &has_better_mv);
3253     obmc_check_better(xd, cm, &col_bias_mv, best_mv, mv_limits, var_params,
3254                       mv_cost_params, besterr, sse1, distortion,
3255                       &has_better_mv);
3256 
3257     // Do an additional search if the second iteration gives a better mv
3258     if (has_better_mv) {
3259       obmc_check_better(xd, cm, &diag_bias_mv, best_mv, mv_limits, var_params,
3260                         mv_cost_params, besterr, sse1, distortion,
3261                         &has_better_mv);
3262     }
3263   } else {
3264     obmc_check_better_fast(&row_bias_mv, best_mv, mv_limits, var_params,
3265                            mv_cost_params, besterr, sse1, distortion,
3266                            &has_better_mv);
3267     obmc_check_better_fast(&col_bias_mv, best_mv, mv_limits, var_params,
3268                            mv_cost_params, besterr, sse1, distortion,
3269                            &has_better_mv);
3270 
3271     // Do an additional search if the second iteration gives a better mv
3272     if (has_better_mv) {
3273       obmc_check_better_fast(&diag_bias_mv, best_mv, mv_limits, var_params,
3274                              mv_cost_params, besterr, sse1, distortion,
3275                              &has_better_mv);
3276     }
3277   }
3278 }
3279 
av1_find_best_obmc_sub_pixel_tree_up(MACROBLOCKD * xd,const AV1_COMMON * const cm,const SUBPEL_MOTION_SEARCH_PARAMS * ms_params,MV start_mv,MV * bestmv,int * distortion,unsigned int * sse1,int_mv * last_mv_search_list)3280 int av1_find_best_obmc_sub_pixel_tree_up(
3281     MACROBLOCKD *xd, const AV1_COMMON *const cm,
3282     const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, MV start_mv, MV *bestmv,
3283     int *distortion, unsigned int *sse1, int_mv *last_mv_search_list) {
3284   (void)last_mv_search_list;
3285   const int allow_hp = ms_params->allow_hp;
3286   const int forced_stop = ms_params->forced_stop;
3287   const int iters_per_step = ms_params->iters_per_step;
3288   const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
3289   const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params;
3290   const SUBPEL_SEARCH_TYPE subpel_search_type =
3291       ms_params->var_params.subpel_search_type;
3292   const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
3293 
3294   int hstep = INIT_SUBPEL_STEP_SIZE;
3295   const int round = AOMMIN(FULL_PEL - forced_stop, 3 - !allow_hp);
3296 
3297   unsigned int besterr = INT_MAX;
3298   *bestmv = start_mv;
3299 
3300   if (subpel_search_type != USE_2_TAPS_ORIG)
3301     besterr = upsampled_setup_obmc_center_error(
3302         xd, cm, bestmv, var_params, mv_cost_params, sse1, distortion);
3303   else
3304     besterr = setup_obmc_center_error(bestmv, var_params, mv_cost_params, sse1,
3305                                       distortion);
3306 
3307   for (int iter = 0; iter < round; ++iter) {
3308     MV iter_center_mv = *bestmv;
3309     MV diag_step = obmc_first_level_check(xd, cm, iter_center_mv, bestmv, hstep,
3310                                           mv_limits, var_params, mv_cost_params,
3311                                           &besterr, sse1, distortion);
3312 
3313     if (!CHECK_MV_EQUAL(iter_center_mv, *bestmv) && iters_per_step > 1) {
3314       obmc_second_level_check_v2(xd, cm, iter_center_mv, diag_step, bestmv,
3315                                  mv_limits, var_params, mv_cost_params,
3316                                  &besterr, sse1, distortion);
3317     }
3318     hstep >>= 1;
3319   }
3320 
3321   return besterr;
3322 }
3323 
3324 // =============================================================================
3325 //  Public cost function: mv_cost + pred error
3326 // =============================================================================
av1_get_mvpred_sse(const MACROBLOCK * x,const FULLPEL_MV * best_mv,const MV * ref_mv,const aom_variance_fn_ptr_t * vfp)3327 int av1_get_mvpred_sse(const MACROBLOCK *x, const FULLPEL_MV *best_mv,
3328                        const MV *ref_mv, const aom_variance_fn_ptr_t *vfp) {
3329   const MACROBLOCKD *const xd = &x->e_mbd;
3330   const struct buf_2d *const what = &x->plane[0].src;
3331   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
3332   const MV mv = get_mv_from_fullmv(best_mv);
3333   const MV_COST_TYPE mv_cost_type = x->mv_cost_type;
3334   unsigned int sse, var;
3335 
3336   var = vfp->vf(what->buf, what->stride, get_buf_from_fullmv(in_what, best_mv),
3337                 in_what->stride, &sse);
3338   (void)var;
3339 
3340   return sse + mv_err_cost(&mv, ref_mv, x->nmv_vec_cost,
3341                            CONVERT_TO_CONST_MVCOST(x->mv_cost_stack),
3342                            x->errorperbit, mv_cost_type);
3343 }
3344 
get_mvpred_av_var(const MV_COST_PARAMS * mv_cost_params,const FULLPEL_MV best_mv,const uint8_t * second_pred,const aom_variance_fn_ptr_t * vfp,const struct buf_2d * src,const struct buf_2d * pre)3345 static INLINE int get_mvpred_av_var(const MV_COST_PARAMS *mv_cost_params,
3346                                     const FULLPEL_MV best_mv,
3347                                     const uint8_t *second_pred,
3348                                     const aom_variance_fn_ptr_t *vfp,
3349                                     const struct buf_2d *src,
3350                                     const struct buf_2d *pre) {
3351   const struct buf_2d *const what = src;
3352   const struct buf_2d *const in_what = pre;
3353   const MV mv = get_mv_from_fullmv(&best_mv);
3354   unsigned int unused;
3355 
3356   return vfp->svaf(get_buf_from_fullmv(in_what, &best_mv), in_what->stride, 0,
3357                    0, what->buf, what->stride, &unused, second_pred) +
3358          mv_err_cost_(&mv, mv_cost_params);
3359 }
3360 
get_mvpred_mask_var(const MV_COST_PARAMS * mv_cost_params,const FULLPEL_MV best_mv,const uint8_t * second_pred,const uint8_t * mask,int mask_stride,int invert_mask,const aom_variance_fn_ptr_t * vfp,const struct buf_2d * src,const struct buf_2d * pre)3361 static INLINE int get_mvpred_mask_var(
3362     const MV_COST_PARAMS *mv_cost_params, const FULLPEL_MV best_mv,
3363     const uint8_t *second_pred, const uint8_t *mask, int mask_stride,
3364     int invert_mask, const aom_variance_fn_ptr_t *vfp, const struct buf_2d *src,
3365     const struct buf_2d *pre) {
3366   const struct buf_2d *const what = src;
3367   const struct buf_2d *const in_what = pre;
3368   const MV mv = get_mv_from_fullmv(&best_mv);
3369   unsigned int unused;
3370 
3371   return vfp->msvf(what->buf, what->stride, 0, 0,
3372                    get_buf_from_fullmv(in_what, &best_mv), in_what->stride,
3373                    second_pred, mask, mask_stride, invert_mask, &unused) +
3374          mv_err_cost_(&mv, mv_cost_params);
3375 }
3376 
av1_get_mvpred_compound_var(const MV_COST_PARAMS * mv_cost_params,const FULLPEL_MV best_mv,const uint8_t * second_pred,const uint8_t * mask,int mask_stride,int invert_mask,const aom_variance_fn_ptr_t * vfp,const struct buf_2d * src,const struct buf_2d * pre)3377 int av1_get_mvpred_compound_var(const MV_COST_PARAMS *mv_cost_params,
3378                                 const FULLPEL_MV best_mv,
3379                                 const uint8_t *second_pred, const uint8_t *mask,
3380                                 int mask_stride, int invert_mask,
3381                                 const aom_variance_fn_ptr_t *vfp,
3382                                 const struct buf_2d *src,
3383                                 const struct buf_2d *pre) {
3384   if (mask) {
3385     return get_mvpred_mask_var(mv_cost_params, best_mv, second_pred, mask,
3386                                mask_stride, invert_mask, vfp, src, pre);
3387   } else {
3388     return get_mvpred_av_var(mv_cost_params, best_mv, second_pred, vfp, src,
3389                              pre);
3390   }
3391 }
3392