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