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