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