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