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