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