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