blob: 8601b551c7da76c7ebf8fe8a682fcd7604ea704e [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <limits.h>
13#include <math.h>
14#include <stdio.h>
15
Tom Finegan60e653d2018-05-22 11:34:58 -070016#include "config/aom_config.h"
Tom Finegan44702c82018-05-22 13:00:39 -070017#include "config/aom_dsp_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070018
Yaowu Xuf883b422016-08-30 14:01:10 -070019#include "aom_dsp/aom_dsp_common.h"
20#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070021#include "aom_ports/mem.h"
22
Wan-Teh Changf2d15ee2020-03-10 09:24:43 -070023#include "av1/common/av1_common_int.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070024#include "av1/common/common.h"
chiyotsai2e42a662020-02-26 17:39:03 -080025#include "av1/common/filter.h"
Yunqing Wangff4fa062017-04-21 10:56:08 -070026#include "av1/common/mvref_common.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070027#include "av1/common/reconinter.h"
28
29#include "av1/encoder/encoder.h"
Angie Chiang09040922018-06-14 15:37:50 -070030#include "av1/encoder/encodemv.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070031#include "av1/encoder/mcomp.h"
32#include "av1/encoder/rdopt.h"
Urvang Joshi7500f802018-07-31 11:47:01 -070033#include "av1/encoder/reconinter_enc.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070034
chiyotsai94f4aca2020-03-20 12:54:47 -070035static INLINE void init_mv_cost_params(MV_COST_PARAMS *mv_cost_params,
chiyotsai9a06d182020-05-01 17:12:12 -070036 const MvCosts *mv_costs,
chiyotsaic95e3642020-04-10 13:17:06 -070037 const MV *ref_mv) {
chiyotsai94f4aca2020-03-20 12:54:47 -070038 mv_cost_params->ref_mv = ref_mv;
39 mv_cost_params->full_ref_mv = get_fullmv_from_mv(ref_mv);
chiyotsaic95e3642020-04-10 13:17:06 -070040 mv_cost_params->mv_cost_type = MV_COST_ENTROPY;
chiyotsai9a06d182020-05-01 17:12:12 -070041 mv_cost_params->error_per_bit = mv_costs->errorperbit;
42 mv_cost_params->sad_per_bit = mv_costs->sadperbit;
43 mv_cost_params->mvjcost = mv_costs->nmv_joint_cost;
44 mv_cost_params->mvcost[0] = mv_costs->mv_cost_stack[0];
45 mv_cost_params->mvcost[1] = mv_costs->mv_cost_stack[1];
chiyotsai94f4aca2020-03-20 12:54:47 -070046}
47
48static INLINE void init_ms_buffers(MSBuffers *ms_buffers, const MACROBLOCK *x) {
49 ms_buffers->ref = &x->e_mbd.plane[0].pre[0];
50 ms_buffers->src = &x->plane[0].src;
51
chiyotsai4d1b7d92020-04-07 13:56:22 -070052 av1_set_ms_compound_refs(ms_buffers, NULL, NULL, 0, 0);
chiyotsai94f4aca2020-03-20 12:54:47 -070053
chiyotsaid2b12212020-04-28 20:57:19 -070054 ms_buffers->wsrc = x->obmc_buffer.wsrc;
55 ms_buffers->obmc_mask = x->obmc_buffer.mask;
chiyotsai94f4aca2020-03-20 12:54:47 -070056}
chiyotsai9365bff2020-01-31 14:40:05 -080057
chiyotsaie4020f72020-07-13 15:46:47 -070058static AOM_INLINE SEARCH_METHODS
59get_faster_search_method(SEARCH_METHODS search_method) {
60 // Note on search method's accuracy:
61 // 1. NSTEP
62 // 2. DIAMOND
63 // 3. BIGDIA \approx SQUARE
64 // 4. HEX.
65 // 5. FAST_HEX \approx FAST_DIAMOND
66 switch (search_method) {
67 case NSTEP: return DIAMOND;
68 case DIAMOND: return BIGDIA;
69 case BIGDIA: return HEX;
70 case SQUARE: return HEX;
71 case HEX: return FAST_HEX;
72 case FAST_HEX: return FAST_HEX;
73 case FAST_DIAMOND: return FAST_DIAMOND;
venkat sanampudi14affd02020-07-23 07:41:05 +053074 case FAST_BIGDIA: return FAST_BIGDIA;
chiyotsaie4020f72020-07-13 15:46:47 -070075 default: assert(0 && "Invalid search method!"); return DIAMOND;
76 }
77}
78
venkat sanampudi14affd02020-07-23 07:41:05 +053079void av1_make_default_fullpel_ms_params(
80 FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const struct AV1_COMP *cpi,
81 const MACROBLOCK *x, BLOCK_SIZE bsize, const MV *ref_mv,
82 const search_site_config search_sites[NUM_SEARCH_METHODS],
chiyotsai7430da62020-07-27 14:06:26 -070083 int fine_search_interval) {
chiyotsaie4020f72020-07-13 15:46:47 -070084 const MV_SPEED_FEATURES *mv_sf = &cpi->sf.mv_sf;
85
chiyotsai2e42a662020-02-26 17:39:03 -080086 // High level params
87 ms_params->bsize = bsize;
88 ms_params->vfp = &cpi->fn_ptr[bsize];
89
chiyotsai94f4aca2020-03-20 12:54:47 -070090 init_ms_buffers(&ms_params->ms_buffers, x);
chiyotsai2e42a662020-02-26 17:39:03 -080091
chiyotsai7430da62020-07-27 14:06:26 -070092 SEARCH_METHODS search_method = mv_sf->search_method;
chiyotsaie4020f72020-07-13 15:46:47 -070093 if (mv_sf->use_bsize_dependent_search_method) {
94 const int min_dim = AOMMIN(block_size_wide[bsize], block_size_high[bsize]);
95 if (min_dim >= 32) {
chiyotsai7430da62020-07-27 14:06:26 -070096 search_method = get_faster_search_method(search_method);
chiyotsaie4020f72020-07-13 15:46:47 -070097 }
98 }
chiyotsai7430da62020-07-27 14:06:26 -070099
100 av1_set_mv_search_method(ms_params, search_sites, search_method);
chiyotsai2e42a662020-02-26 17:39:03 -0800101
chiyotsaic814afb2020-08-04 13:12:35 -0700102 const int use_downsampled_sad =
103 mv_sf->use_downsampled_sad && block_size_high[bsize] >= 16;
104 if (use_downsampled_sad) {
105 ms_params->sdf = ms_params->vfp->sdsf;
106 ms_params->sdx4df = ms_params->vfp->sdsx4df;
107 } else {
108 ms_params->sdf = ms_params->vfp->sdf;
109 ms_params->sdx4df = ms_params->vfp->sdx4df;
110 }
111
chiyotsaie4020f72020-07-13 15:46:47 -0700112 ms_params->mesh_patterns[0] = mv_sf->mesh_patterns;
113 ms_params->mesh_patterns[1] = mv_sf->intrabc_mesh_patterns;
114 ms_params->force_mesh_thresh = mv_sf->exhaustive_searches_thresh;
115 ms_params->prune_mesh_search = mv_sf->prune_mesh_search;
chiyotsai2e42a662020-02-26 17:39:03 -0800116 ms_params->run_mesh_search = 0;
Cheng Chencf16b022020-04-15 11:19:23 -0700117 ms_params->fine_search_interval = fine_search_interval;
chiyotsai2e42a662020-02-26 17:39:03 -0800118
119 ms_params->is_intra_mode = 0;
120
chiyotsaie4020f72020-07-13 15:46:47 -0700121 ms_params->fast_obmc_search = mv_sf->obmc_full_pixel_search_level;
chiyotsai2e42a662020-02-26 17:39:03 -0800122
123 ms_params->mv_limits = x->mv_limits;
124 av1_set_mv_search_range(&ms_params->mv_limits, ref_mv);
125
126 // Mvcost params
chiyotsai9a06d182020-05-01 17:12:12 -0700127 init_mv_cost_params(&ms_params->mv_cost_params, &x->mv_costs, ref_mv);
chiyotsai2e42a662020-02-26 17:39:03 -0800128}
129
chiyotsai083db972020-03-30 11:43:52 -0700130void av1_make_default_subpel_ms_params(SUBPEL_MOTION_SEARCH_PARAMS *ms_params,
131 const struct AV1_COMP *cpi,
132 const MACROBLOCK *x, BLOCK_SIZE bsize,
chiyotsai4d1b7d92020-04-07 13:56:22 -0700133 const MV *ref_mv, const int *cost_list) {
chiyotsai2aac3002020-02-13 17:02:01 -0800134 const AV1_COMMON *cm = &cpi->common;
135 // High level params
Urvang Joshib6409e92020-03-23 11:23:27 -0700136 ms_params->allow_hp = cm->features.allow_high_precision_mv;
chiyotsai2aac3002020-02-13 17:02:01 -0800137 ms_params->forced_stop = cpi->sf.mv_sf.subpel_force_stop;
138 ms_params->iters_per_step = cpi->sf.mv_sf.subpel_iters_per_step;
139 ms_params->cost_list = cond_cost_list_const(cpi, cost_list);
chiyotsai2aac3002020-02-13 17:02:01 -0800140
chiyotsai5aa70752020-03-26 10:13:10 -0700141 av1_set_subpel_mv_search_range(&ms_params->mv_limits, &x->mv_limits, ref_mv);
142
chiyotsai2aac3002020-02-13 17:02:01 -0800143 // Mvcost params
chiyotsai9a06d182020-05-01 17:12:12 -0700144 init_mv_cost_params(&ms_params->mv_cost_params, &x->mv_costs, ref_mv);
chiyotsai2aac3002020-02-13 17:02:01 -0800145
146 // Subpel variance params
147 ms_params->var_params.vfp = &cpi->fn_ptr[bsize];
148 ms_params->var_params.subpel_search_type =
149 cpi->sf.mv_sf.use_accurate_subpel_search;
chiyotsai2aac3002020-02-13 17:02:01 -0800150 ms_params->var_params.w = block_size_wide[bsize];
151 ms_params->var_params.h = block_size_high[bsize];
chiyotsai5aa70752020-03-26 10:13:10 -0700152
153 // Ref and src buffers
154 MSBuffers *ms_buffers = &ms_params->var_params.ms_buffers;
155 init_ms_buffers(ms_buffers, x);
chiyotsai2aac3002020-02-13 17:02:01 -0800156}
157
chiyotsai5aa70752020-03-26 10:13:10 -0700158static INLINE int get_offset_from_fullmv(const FULLPEL_MV *mv, int stride) {
chiyotsai87bb8052020-02-12 16:56:33 -0800159 return mv->row * stride + mv->col;
160}
161
chiyotsai5aa70752020-03-26 10:13:10 -0700162static INLINE const uint8_t *get_buf_from_fullmv(const struct buf_2d *buf,
163 const FULLPEL_MV *mv) {
164 return &buf->buf[get_offset_from_fullmv(mv, buf->stride)];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700165}
166
chiyotsai2ad959b2020-02-12 14:29:32 -0800167void av1_set_mv_search_range(FullMvLimits *mv_limits, const MV *mv) {
chiyotsaie46cff72020-02-05 15:03:34 -0800168 int col_min =
169 GET_MV_RAWPEL(mv->col) - MAX_FULL_PEL_VAL + (mv->col & 7 ? 1 : 0);
170 int row_min =
171 GET_MV_RAWPEL(mv->row) - MAX_FULL_PEL_VAL + (mv->row & 7 ? 1 : 0);
172 int col_max = GET_MV_RAWPEL(mv->col) + MAX_FULL_PEL_VAL;
173 int row_max = GET_MV_RAWPEL(mv->row) + MAX_FULL_PEL_VAL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700174
chiyotsaie46cff72020-02-05 15:03:34 -0800175 col_min = AOMMAX(col_min, GET_MV_RAWPEL(MV_LOW) + 1);
176 row_min = AOMMAX(row_min, GET_MV_RAWPEL(MV_LOW) + 1);
177 col_max = AOMMIN(col_max, GET_MV_RAWPEL(MV_UPP) - 1);
178 row_max = AOMMIN(row_max, GET_MV_RAWPEL(MV_UPP) - 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179
180 // Get intersection of UMV window and valid MV window to reduce # of checks
181 // in diamond search.
Alex Converse0fa0f422017-04-24 12:51:14 -0700182 if (mv_limits->col_min < col_min) mv_limits->col_min = col_min;
183 if (mv_limits->col_max > col_max) mv_limits->col_max = col_max;
184 if (mv_limits->row_min < row_min) mv_limits->row_min = row_min;
185 if (mv_limits->row_max > row_max) mv_limits->row_max = row_max;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186}
187
Yaowu Xuf883b422016-08-30 14:01:10 -0700188int av1_init_search_range(int size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189 int sr = 0;
190 // Minimum search size no matter what the passed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -0700191 size = AOMMAX(16, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700192
193 while ((size << sr) < MAX_FULL_PEL_VAL) sr++;
194
Yaowu Xuf883b422016-08-30 14:01:10 -0700195 sr = AOMMIN(sr, MAX_MVSEARCH_STEPS - 2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700196 return sr;
197}
198
chiyotsai9f664e32020-02-19 14:17:31 -0800199// ============================================================================
200// Cost of motion vectors
201// ============================================================================
chiyotsai94f4aca2020-03-20 12:54:47 -0700202// TODO(any): Adaptively adjust the regularization strength based on image size
203// and motion activity instead of using hard-coded values. It seems like we
204// roughly half the lambda for each increase in resolution
205// These are multiplier used to perform regularization in motion compensation
206// when x->mv_cost_type is set to MV_COST_L1.
207// LOWRES
208#define SSE_LAMBDA_LOWRES 2 // Used by mv_cost_err_fn
209#define SAD_LAMBDA_LOWRES 32 // Used by mvsad_err_cost during full pixel search
210// MIDRES
211#define SSE_LAMBDA_MIDRES 0 // Used by mv_cost_err_fn
212#define SAD_LAMBDA_MIDRES 15 // Used by mvsad_err_cost during full pixel search
213// HDRES
214#define SSE_LAMBDA_HDRES 1 // Used by mv_cost_err_fn
215#define SAD_LAMBDA_HDRES 8 // Used by mvsad_err_cost during full pixel search
chiyotsai9f664e32020-02-19 14:17:31 -0800216
chiyotsai9365bff2020-01-31 14:40:05 -0800217// Returns the rate of encoding the current motion vector based on the
chiyotsaifcffdc62020-02-04 15:17:05 -0800218// joint_cost and comp_cost. joint_costs covers the cost of transmitting
219// JOINT_MV, and comp_cost covers the cost of transmitting the actual motion
chiyotsai9365bff2020-01-31 14:40:05 -0800220// vector.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700221static INLINE int mv_cost(const MV *mv, const int *joint_cost,
chiyotsai2aac3002020-02-13 17:02:01 -0800222 const int *const comp_cost[2]) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700223 return joint_cost[av1_get_mv_joint(mv)] + comp_cost[0][mv->row] +
Yaowu Xuc27fc142016-08-22 16:08:15 -0700224 comp_cost[1][mv->col];
225}
226
chiyotsai2aac3002020-02-13 17:02:01 -0800227#define CONVERT_TO_CONST_MVCOST(ptr) ((const int *const *)(ptr))
chiyotsaifcffdc62020-02-04 15:17:05 -0800228// Returns the cost of encoding the motion vector diff := *mv - *ref. The cost
229// is defined as the rate required to encode diff * weight, rounded to the
230// nearest 2 ** 7.
chiyotsai9365bff2020-01-31 14:40:05 -0800231// This is NOT used during motion compensation.
chiyotsaie46cff72020-02-05 15:03:34 -0800232int av1_mv_bit_cost(const MV *mv, const MV *ref_mv, const int *mvjcost,
Yaowu Xuf883b422016-08-30 14:01:10 -0700233 int *mvcost[2], int weight) {
chiyotsaie46cff72020-02-05 15:03:34 -0800234 const MV diff = { mv->row - ref_mv->row, mv->col - ref_mv->col };
chiyotsai2aac3002020-02-13 17:02:01 -0800235 return ROUND_POWER_OF_TWO(
236 mv_cost(&diff, mvjcost, CONVERT_TO_CONST_MVCOST(mvcost)) * weight, 7);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700237}
238
chiyotsai9365bff2020-01-31 14:40:05 -0800239// Returns the cost of using the current mv during the motion search. This is
240// used when var is used as the error metric.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700241#define PIXEL_TRANSFORM_ERROR_SCALE 4
chiyotsai2e42a662020-02-26 17:39:03 -0800242static INLINE int mv_err_cost(const MV *mv, const MV *ref_mv,
243 const int *mvjcost, const int *const mvcost[2],
244 int error_per_bit, MV_COST_TYPE mv_cost_type) {
chiyotsaie46cff72020-02-05 15:03:34 -0800245 const MV diff = { mv->row - ref_mv->row, mv->col - ref_mv->col };
chiyotsai9365bff2020-01-31 14:40:05 -0800246 const MV abs_diff = { abs(diff.row), abs(diff.col) };
247
248 switch (mv_cost_type) {
249 case MV_COST_ENTROPY:
250 if (mvcost) {
251 return (int)ROUND_POWER_OF_TWO_64(
252 (int64_t)mv_cost(&diff, mvjcost, mvcost) * error_per_bit,
253 RDDIV_BITS + AV1_PROB_COST_SHIFT - RD_EPB_SHIFT +
254 PIXEL_TRANSFORM_ERROR_SCALE);
255 }
256 return 0;
257 case MV_COST_L1_LOWRES:
258 return (SSE_LAMBDA_LOWRES * (abs_diff.row + abs_diff.col)) >> 3;
259 case MV_COST_L1_MIDRES:
260 return (SSE_LAMBDA_MIDRES * (abs_diff.row + abs_diff.col)) >> 3;
261 case MV_COST_L1_HDRES:
262 return (SSE_LAMBDA_HDRES * (abs_diff.row + abs_diff.col)) >> 3;
263 case MV_COST_NONE: return 0;
264 default: assert(0 && "Invalid rd_cost_type"); return 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700265 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700266}
267
chiyotsaia742e7d2020-02-14 17:27:58 -0800268static INLINE int mv_err_cost_(const MV *mv,
269 const MV_COST_PARAMS *mv_cost_params) {
270 return mv_err_cost(mv, mv_cost_params->ref_mv, mv_cost_params->mvjcost,
271 mv_cost_params->mvcost, mv_cost_params->error_per_bit,
272 mv_cost_params->mv_cost_type);
273}
274
chiyotsai9365bff2020-01-31 14:40:05 -0800275// Returns the cost of using the current mv during the motion search. This is
276// only used during full pixel motion search when sad is used as the error
chiyotsai2e42a662020-02-26 17:39:03 -0800277// metric
278static INLINE int mvsad_err_cost(const FULLPEL_MV *mv, const FULLPEL_MV *ref_mv,
279 const int *mvjcost, const int *const mvcost[2],
280 int sad_per_bit, MV_COST_TYPE mv_cost_type) {
chiyotsaie46cff72020-02-05 15:03:34 -0800281 const MV diff = { GET_MV_SUBPEL(mv->row - ref_mv->row),
282 GET_MV_SUBPEL(mv->col - ref_mv->col) };
chiyotsai2aac3002020-02-13 17:02:01 -0800283
chiyotsai9365bff2020-01-31 14:40:05 -0800284 switch (mv_cost_type) {
285 case MV_COST_ENTROPY:
286 return ROUND_POWER_OF_TWO(
chiyotsai2e42a662020-02-26 17:39:03 -0800287 (unsigned)mv_cost(&diff, mvjcost, CONVERT_TO_CONST_MVCOST(mvcost)) *
chiyotsai9365bff2020-01-31 14:40:05 -0800288 sad_per_bit,
289 AV1_PROB_COST_SHIFT);
290 case MV_COST_L1_LOWRES:
291 return (SAD_LAMBDA_LOWRES * (abs(diff.row) + abs(diff.col))) >> 3;
292 case MV_COST_L1_MIDRES:
293 return (SAD_LAMBDA_MIDRES * (abs(diff.row) + abs(diff.col))) >> 3;
294 case MV_COST_L1_HDRES:
295 return (SAD_LAMBDA_HDRES * (abs(diff.row) + abs(diff.col))) >> 3;
296 case MV_COST_NONE: return 0;
297 default: assert(0 && "Invalid rd_cost_type"); return 0;
298 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700299}
300
chiyotsai2e42a662020-02-26 17:39:03 -0800301static INLINE int mvsad_err_cost_(const FULLPEL_MV *mv,
302 const MV_COST_PARAMS *mv_cost_params) {
303 return mvsad_err_cost(mv, &mv_cost_params->full_ref_mv,
304 mv_cost_params->mvjcost, mv_cost_params->mvcost,
305 mv_cost_params->sad_per_bit,
306 mv_cost_params->mv_cost_type);
307}
308
chiyotsai9f664e32020-02-19 14:17:31 -0800309// =============================================================================
310// Fullpixel Motion Search: Translational
311// =============================================================================
312#define MAX_PATTERN_SCALES 11
313#define MAX_PATTERN_CANDIDATES 8 // max number of candidates per scale
314#define PATTERN_CANDIDATES_REF 3 // number of refinement candidates
315
Yaowu Xuf883b422016-08-30 14:01:10 -0700316void av1_init_dsmotion_compensation(search_site_config *cfg, int stride) {
Cheng Chen66190d92020-04-21 12:58:43 -0700317 int num_search_steps = 0;
Jingning Han4d780b02019-11-22 09:48:14 -0800318 int stage_index = MAX_MVSEARCH_STEPS - 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700319
Cheng Chen66190d92020-04-21 12:58:43 -0700320 cfg->site[stage_index][0].mv.col = cfg->site[stage_index][0].mv.row = 0;
321 cfg->site[stage_index][0].offset = 0;
chiyotsai836b69b2019-04-09 13:41:24 -0700322 cfg->stride = stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700323
Jingning Han183b2a82019-12-18 16:03:41 -0800324 for (int radius = MAX_FIRST_STEP; radius > 0; radius /= 2) {
325 int num_search_pts = 8;
326
Cheng Chen66190d92020-04-21 12:58:43 -0700327 const FULLPEL_MV search_site_mvs[13] = {
Jingning Han183b2a82019-12-18 16:03:41 -0800328 { 0, 0 }, { -radius, 0 }, { radius, 0 },
329 { 0, -radius }, { 0, radius }, { -radius, -radius },
330 { radius, radius }, { -radius, radius }, { radius, -radius },
Jingning Han4d780b02019-11-22 09:48:14 -0800331 };
Jingning Han183b2a82019-12-18 16:03:41 -0800332
Yaowu Xuc27fc142016-08-22 16:08:15 -0700333 int i;
Jingning Han183b2a82019-12-18 16:03:41 -0800334 for (i = 0; i <= num_search_pts; ++i) {
Cheng Chen66190d92020-04-21 12:58:43 -0700335 search_site *const site = &cfg->site[stage_index][i];
336 site->mv = search_site_mvs[i];
337 site->offset = get_offset_from_fullmv(&site->mv, stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700338 }
Jingning Han183b2a82019-12-18 16:03:41 -0800339 cfg->searches_per_step[stage_index] = num_search_pts;
340 cfg->radius[stage_index] = radius;
Jingning Han4d780b02019-11-22 09:48:14 -0800341 --stage_index;
Cheng Chen66190d92020-04-21 12:58:43 -0700342 ++num_search_steps;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700343 }
Cheng Chen66190d92020-04-21 12:58:43 -0700344 cfg->num_search_steps = num_search_steps;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345}
346
Jingning Han969a8d42019-12-16 19:40:14 -0800347void av1_init_motion_fpf(search_site_config *cfg, int stride) {
Cheng Chen66190d92020-04-21 12:58:43 -0700348 int num_search_steps = 0;
Jingning Han4d780b02019-11-22 09:48:14 -0800349 int stage_index = MAX_MVSEARCH_STEPS - 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700350
Cheng Chen66190d92020-04-21 12:58:43 -0700351 cfg->site[stage_index][0].mv.col = cfg->site[stage_index][0].mv.row = 0;
352 cfg->site[stage_index][0].offset = 0;
chiyotsai836b69b2019-04-09 13:41:24 -0700353 cfg->stride = stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700354
Jingning Hanb7dad712019-11-22 15:11:56 -0800355 for (int radius = MAX_FIRST_STEP; radius > 0; radius /= 2) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700356 // Generate offsets for 8 search sites per step.
Jingning Hanb7dad712019-11-22 15:11:56 -0800357 int tan_radius = AOMMAX((int)(0.41 * radius), 1);
358 int num_search_pts = 12;
359 if (radius == 1) num_search_pts = 8;
360
Cheng Chen66190d92020-04-21 12:58:43 -0700361 const FULLPEL_MV search_site_mvs[13] = {
Jingning Hanb7dad712019-11-22 15:11:56 -0800362 { 0, 0 },
363 { -radius, 0 },
364 { radius, 0 },
365 { 0, -radius },
366 { 0, radius },
367 { -radius, -tan_radius },
368 { radius, tan_radius },
369 { -tan_radius, radius },
370 { tan_radius, -radius },
371 { -radius, tan_radius },
372 { radius, -tan_radius },
373 { tan_radius, radius },
374 { -tan_radius, -radius },
375 };
Jingning Han4d780b02019-11-22 09:48:14 -0800376
Yaowu Xuc27fc142016-08-22 16:08:15 -0700377 int i;
Jingning Hanb7dad712019-11-22 15:11:56 -0800378 for (i = 0; i <= num_search_pts; ++i) {
Cheng Chen66190d92020-04-21 12:58:43 -0700379 search_site *const site = &cfg->site[stage_index][i];
380 site->mv = search_site_mvs[i];
381 site->offset = get_offset_from_fullmv(&site->mv, stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700382 }
Jingning Hanb7dad712019-11-22 15:11:56 -0800383 cfg->searches_per_step[stage_index] = num_search_pts;
Jingning Han969a8d42019-12-16 19:40:14 -0800384 cfg->radius[stage_index] = radius;
Jingning Han4d780b02019-11-22 09:48:14 -0800385 --stage_index;
Cheng Chen66190d92020-04-21 12:58:43 -0700386 ++num_search_steps;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700387 }
Cheng Chen66190d92020-04-21 12:58:43 -0700388 cfg->num_search_steps = num_search_steps;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700389}
390
venkat sanampudi14affd02020-07-23 07:41:05 +0530391// Search site initialization for NSTEP search method.
392void av1_init_motion_compensation_nstep(search_site_config *cfg, int stride) {
Cheng Chen66190d92020-04-21 12:58:43 -0700393 int num_search_steps = 0;
Jingning Han969a8d42019-12-16 19:40:14 -0800394 int stage_index = 0;
395 cfg->stride = stride;
396 int radius = 1;
Jingning Hana1e7dfe2020-01-03 15:36:27 -0800397 for (stage_index = 0; stage_index < 15; ++stage_index) {
Jingning Han969a8d42019-12-16 19:40:14 -0800398 int tan_radius = AOMMAX((int)(0.41 * radius), 1);
399 int num_search_pts = 12;
Jingning Hana73154a2020-01-30 17:01:33 -0800400 if (radius <= 5) {
401 tan_radius = radius;
402 num_search_pts = 8;
403 }
Cheng Chen66190d92020-04-21 12:58:43 -0700404 const FULLPEL_MV search_site_mvs[13] = {
Jingning Han969a8d42019-12-16 19:40:14 -0800405 { 0, 0 },
406 { -radius, 0 },
407 { radius, 0 },
408 { 0, -radius },
409 { 0, radius },
410 { -radius, -tan_radius },
411 { radius, tan_radius },
412 { -tan_radius, radius },
413 { tan_radius, -radius },
414 { -radius, tan_radius },
415 { radius, -tan_radius },
416 { tan_radius, radius },
417 { -tan_radius, -radius },
418 };
419
420 for (int i = 0; i <= num_search_pts; ++i) {
Cheng Chen66190d92020-04-21 12:58:43 -0700421 search_site *const site = &cfg->site[stage_index][i];
422 site->mv = search_site_mvs[i];
423 site->offset = get_offset_from_fullmv(&site->mv, stride);
Jingning Han969a8d42019-12-16 19:40:14 -0800424 }
425 cfg->searches_per_step[stage_index] = num_search_pts;
426 cfg->radius[stage_index] = radius;
Cheng Chen66190d92020-04-21 12:58:43 -0700427 ++num_search_steps;
Jingning Hana1e7dfe2020-01-03 15:36:27 -0800428 if (stage_index < 12)
429 radius = (int)AOMMAX((radius * 1.5 + 0.5), radius + 1);
Jingning Han969a8d42019-12-16 19:40:14 -0800430 }
Cheng Chen66190d92020-04-21 12:58:43 -0700431 cfg->num_search_steps = num_search_steps;
Jingning Han969a8d42019-12-16 19:40:14 -0800432}
433
venkat sanampudi14affd02020-07-23 07:41:05 +0530434// Search site initialization for BIGDIA / FAST_BIGDIA / FAST_DIAMOND
435// search methods.
venkat sanampudidc5371c2020-07-10 09:36:01 +0530436void av1_init_motion_compensation_bigdia(search_site_config *cfg, int stride) {
437 cfg->stride = stride;
438 // First scale has 4-closest points, the rest have 8 points in diamond
439 // shape at increasing scales
440 static const int bigdia_num_candidates[MAX_PATTERN_SCALES] = {
441 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
442 };
443
444 // BIGDIA search method candidates.
445 // Note that the largest candidate step at each scale is 2^scale
446 /* clang-format off */
447 static const FULLPEL_MV
448 site_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
449 { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, 0 }, { 0, 0 },
450 { 0, 0 }, { 0, 0 } },
451 { { -1, -1 }, { 0, -2 }, { 1, -1 }, { 2, 0 }, { 1, 1 }, { 0, 2 },
452 { -1, 1 }, { -2, 0 } },
453 { { -2, -2 }, { 0, -4 }, { 2, -2 }, { 4, 0 }, { 2, 2 }, { 0, 4 },
454 { -2, 2 }, { -4, 0 } },
455 { { -4, -4 }, { 0, -8 }, { 4, -4 }, { 8, 0 }, { 4, 4 }, { 0, 8 },
456 { -4, 4 }, { -8, 0 } },
457 { { -8, -8 }, { 0, -16 }, { 8, -8 }, { 16, 0 }, { 8, 8 }, { 0, 16 },
458 { -8, 8 }, { -16, 0 } },
459 { { -16, -16 }, { 0, -32 }, { 16, -16 }, { 32, 0 }, { 16, 16 },
460 { 0, 32 }, { -16, 16 }, { -32, 0 } },
461 { { -32, -32 }, { 0, -64 }, { 32, -32 }, { 64, 0 }, { 32, 32 },
462 { 0, 64 }, { -32, 32 }, { -64, 0 } },
463 { { -64, -64 }, { 0, -128 }, { 64, -64 }, { 128, 0 }, { 64, 64 },
464 { 0, 128 }, { -64, 64 }, { -128, 0 } },
465 { { -128, -128 }, { 0, -256 }, { 128, -128 }, { 256, 0 },
466 { 128, 128 }, { 0, 256 }, { -128, 128 }, { -256, 0 } },
467 { { -256, -256 }, { 0, -512 }, { 256, -256 }, { 512, 0 },
468 { 256, 256 }, { 0, 512 }, { -256, 256 }, { -512, 0 } },
469 { { -512, -512 }, { 0, -1024 }, { 512, -512 }, { 1024, 0 },
470 { 512, 512 }, { 0, 1024 }, { -512, 512 }, { -1024, 0 } },
471 };
472
473 /* clang-format on */
venkat sanampudi14affd02020-07-23 07:41:05 +0530474 int radius = 1;
venkat sanampudidc5371c2020-07-10 09:36:01 +0530475 for (int i = 0; i < MAX_PATTERN_SCALES; ++i) {
476 cfg->searches_per_step[i] = bigdia_num_candidates[i];
venkat sanampudi14affd02020-07-23 07:41:05 +0530477 cfg->radius[i] = radius;
venkat sanampudidc5371c2020-07-10 09:36:01 +0530478 for (int j = 0; j < MAX_PATTERN_CANDIDATES; ++j) {
479 search_site *const site = &cfg->site[i][j];
480 site->mv = site_candidates[i][j];
481 site->offset = get_offset_from_fullmv(&site->mv, stride);
482 }
venkat sanampudi14affd02020-07-23 07:41:05 +0530483 radius *= 2;
venkat sanampudidc5371c2020-07-10 09:36:01 +0530484 }
venkat sanampudi14affd02020-07-23 07:41:05 +0530485 cfg->num_search_steps = MAX_PATTERN_SCALES;
486}
487
488// Search site initialization for SQUARE search method.
489void av1_init_motion_compensation_square(search_site_config *cfg, int stride) {
490 cfg->stride = stride;
491 // All scales have 8 closest points in square shape.
492 static const int square_num_candidates[MAX_PATTERN_SCALES] = {
493 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
494 };
495
496 // Square search method candidates.
497 // Note that the largest candidate step at each scale is 2^scale.
498 /* clang-format off */
499 static const FULLPEL_MV
500 square_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
501 { { -1, -1 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
502 { -1, 1 }, { -1, 0 } },
503 { { -2, -2 }, { 0, -2 }, { 2, -2 }, { 2, 0 }, { 2, 2 }, { 0, 2 },
504 { -2, 2 }, { -2, 0 } },
505 { { -4, -4 }, { 0, -4 }, { 4, -4 }, { 4, 0 }, { 4, 4 }, { 0, 4 },
506 { -4, 4 }, { -4, 0 } },
507 { { -8, -8 }, { 0, -8 }, { 8, -8 }, { 8, 0 }, { 8, 8 }, { 0, 8 },
508 { -8, 8 }, { -8, 0 } },
509 { { -16, -16 }, { 0, -16 }, { 16, -16 }, { 16, 0 }, { 16, 16 },
510 { 0, 16 }, { -16, 16 }, { -16, 0 } },
511 { { -32, -32 }, { 0, -32 }, { 32, -32 }, { 32, 0 }, { 32, 32 },
512 { 0, 32 }, { -32, 32 }, { -32, 0 } },
513 { { -64, -64 }, { 0, -64 }, { 64, -64 }, { 64, 0 }, { 64, 64 },
514 { 0, 64 }, { -64, 64 }, { -64, 0 } },
515 { { -128, -128 }, { 0, -128 }, { 128, -128 }, { 128, 0 },
516 { 128, 128 }, { 0, 128 }, { -128, 128 }, { -128, 0 } },
517 { { -256, -256 }, { 0, -256 }, { 256, -256 }, { 256, 0 },
518 { 256, 256 }, { 0, 256 }, { -256, 256 }, { -256, 0 } },
519 { { -512, -512 }, { 0, -512 }, { 512, -512 }, { 512, 0 },
520 { 512, 512 }, { 0, 512 }, { -512, 512 }, { -512, 0 } },
521 { { -1024, -1024 }, { 0, -1024 }, { 1024, -1024 }, { 1024, 0 },
522 { 1024, 1024 }, { 0, 1024 }, { -1024, 1024 }, { -1024, 0 } },
523 };
524
525 /* clang-format on */
526 int radius = 1;
527 for (int i = 0; i < MAX_PATTERN_SCALES; ++i) {
528 cfg->searches_per_step[i] = square_num_candidates[i];
529 cfg->radius[i] = radius;
530 for (int j = 0; j < MAX_PATTERN_CANDIDATES; ++j) {
531 search_site *const site = &cfg->site[i][j];
532 site->mv = square_candidates[i][j];
533 site->offset = get_offset_from_fullmv(&site->mv, stride);
534 }
535 radius *= 2;
536 }
537 cfg->num_search_steps = MAX_PATTERN_SCALES;
538}
539
540// Search site initialization for HEX / FAST_HEX search methods.
541void av1_init_motion_compensation_hex(search_site_config *cfg, int stride) {
542 cfg->stride = stride;
543 // First scale has 8-closest points, the rest have 6 points in hex shape
544 // at increasing scales.
545 static const int hex_num_candidates[MAX_PATTERN_SCALES] = { 8, 6, 6, 6, 6, 6,
546 6, 6, 6, 6, 6 };
547 // Note that the largest candidate step at each scale is 2^scale.
548 /* clang-format off */
549 static const FULLPEL_MV
550 hex_candidates[MAX_PATTERN_SCALES][MAX_PATTERN_CANDIDATES] = {
551 { { -1, -1 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 },
552 { -1, 1 }, { -1, 0 } },
553 { { -1, -2 }, { 1, -2 }, { 2, 0 }, { 1, 2 }, { -1, 2 }, { -2, 0 } },
554 { { -2, -4 }, { 2, -4 }, { 4, 0 }, { 2, 4 }, { -2, 4 }, { -4, 0 } },
555 { { -4, -8 }, { 4, -8 }, { 8, 0 }, { 4, 8 }, { -4, 8 }, { -8, 0 } },
556 { { -8, -16 }, { 8, -16 }, { 16, 0 }, { 8, 16 },
557 { -8, 16 }, { -16, 0 } },
558 { { -16, -32 }, { 16, -32 }, { 32, 0 }, { 16, 32 }, { -16, 32 },
559 { -32, 0 } },
560 { { -32, -64 }, { 32, -64 }, { 64, 0 }, { 32, 64 }, { -32, 64 },
561 { -64, 0 } },
562 { { -64, -128 }, { 64, -128 }, { 128, 0 }, { 64, 128 },
563 { -64, 128 }, { -128, 0 } },
564 { { -128, -256 }, { 128, -256 }, { 256, 0 }, { 128, 256 },
565 { -128, 256 }, { -256, 0 } },
566 { { -256, -512 }, { 256, -512 }, { 512, 0 }, { 256, 512 },
567 { -256, 512 }, { -512, 0 } },
568 { { -512, -1024 }, { 512, -1024 }, { 1024, 0 }, { 512, 1024 },
569 { -512, 1024 }, { -1024, 0 } },
570 };
571
572 /* clang-format on */
573 int radius = 1;
574 for (int i = 0; i < MAX_PATTERN_SCALES; ++i) {
575 cfg->searches_per_step[i] = hex_num_candidates[i];
576 cfg->radius[i] = radius;
577 for (int j = 0; j < hex_num_candidates[i]; ++j) {
578 search_site *const site = &cfg->site[i][j];
579 site->mv = hex_candidates[i][j];
580 site->offset = get_offset_from_fullmv(&site->mv, stride);
581 }
582 radius *= 2;
583 }
584 cfg->num_search_steps = MAX_PATTERN_SCALES;
venkat sanampudidc5371c2020-07-10 09:36:01 +0530585}
586
chiyotsai9f664e32020-02-19 14:17:31 -0800587// Checks whether the mv is within range of the mv_limits
chiyotsai2ad959b2020-02-12 14:29:32 -0800588static INLINE int check_bounds(const FullMvLimits *mv_limits, int row, int col,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700589 int range) {
Alex Converse0fa0f422017-04-24 12:51:14 -0700590 return ((row - range) >= mv_limits->row_min) &
591 ((row + range) <= mv_limits->row_max) &
592 ((col - range) >= mv_limits->col_min) &
593 ((col + range) <= mv_limits->col_max);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700594}
595
chiyotsai2e42a662020-02-26 17:39:03 -0800596static INLINE int get_mvpred_var_cost(
597 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV *this_mv) {
598 const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
599 const MV sub_this_mv = get_mv_from_fullmv(this_mv);
chiyotsai94f4aca2020-03-20 12:54:47 -0700600 const struct buf_2d *const src = ms_params->ms_buffers.src;
601 const struct buf_2d *const ref = ms_params->ms_buffers.ref;
chiyotsai2e42a662020-02-26 17:39:03 -0800602 const uint8_t *src_buf = src->buf;
603 const int src_stride = src->stride;
604 const int ref_stride = ref->stride;
605
606 unsigned unused;
607 int bestsme;
608
chiyotsai5aa70752020-03-26 10:13:10 -0700609 bestsme = vfp->vf(src_buf, src_stride, get_buf_from_fullmv(ref, this_mv),
chiyotsai2e42a662020-02-26 17:39:03 -0800610 ref_stride, &unused);
611
612 bestsme += mv_err_cost_(&sub_this_mv, &ms_params->mv_cost_params);
613
614 return bestsme;
615}
616
617static INLINE int get_mvpred_sad(const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
618 const struct buf_2d *const src,
619 const uint8_t *const ref_address,
620 const int ref_stride) {
chiyotsai2e42a662020-02-26 17:39:03 -0800621 const uint8_t *src_buf = src->buf;
622 const int src_stride = src->stride;
623
chiyotsaic814afb2020-08-04 13:12:35 -0700624 return ms_params->sdf(src_buf, src_stride, ref_address, ref_stride);
chiyotsai2e42a662020-02-26 17:39:03 -0800625}
626
chiyotsai94f4aca2020-03-20 12:54:47 -0700627static INLINE int get_mvpred_compound_var_cost(
628 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV *this_mv) {
629 const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
630 const struct buf_2d *const src = ms_params->ms_buffers.src;
631 const struct buf_2d *const ref = ms_params->ms_buffers.ref;
632 const uint8_t *src_buf = src->buf;
633 const int src_stride = src->stride;
634 const int ref_stride = ref->stride;
635
636 const uint8_t *mask = ms_params->ms_buffers.mask;
637 const uint8_t *second_pred = ms_params->ms_buffers.second_pred;
638 const int mask_stride = ms_params->ms_buffers.mask_stride;
639 const int invert_mask = ms_params->ms_buffers.inv_mask;
640 unsigned unused;
641 int bestsme;
642
643 if (mask) {
Jingning Han142ade22020-10-10 00:32:34 -0700644 bestsme = vfp->msvf(get_buf_from_fullmv(ref, this_mv), ref_stride, 0, 0,
645 src_buf, src_stride, second_pred, mask, mask_stride,
646 invert_mask, &unused);
chiyotsai94f4aca2020-03-20 12:54:47 -0700647 } else if (second_pred) {
chiyotsai5aa70752020-03-26 10:13:10 -0700648 bestsme = vfp->svaf(get_buf_from_fullmv(ref, this_mv), ref_stride, 0, 0,
chiyotsai94f4aca2020-03-20 12:54:47 -0700649 src_buf, src_stride, &unused, second_pred);
650 } else {
chiyotsai5aa70752020-03-26 10:13:10 -0700651 bestsme = vfp->vf(src_buf, src_stride, get_buf_from_fullmv(ref, this_mv),
chiyotsai94f4aca2020-03-20 12:54:47 -0700652 ref_stride, &unused);
653 }
654
655 const MV sub_this_mv = get_mv_from_fullmv(this_mv);
656 bestsme += mv_err_cost_(&sub_this_mv, &ms_params->mv_cost_params);
657
658 return bestsme;
659}
660
661static INLINE int get_mvpred_compound_sad(
662 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
663 const struct buf_2d *const src, const uint8_t *const ref_address,
664 const int ref_stride) {
665 const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
666 const uint8_t *src_buf = src->buf;
667 const int src_stride = src->stride;
668
669 const uint8_t *mask = ms_params->ms_buffers.mask;
670 const uint8_t *second_pred = ms_params->ms_buffers.second_pred;
671 const int mask_stride = ms_params->ms_buffers.mask_stride;
672 const int invert_mask = ms_params->ms_buffers.inv_mask;
673
674 if (mask) {
675 return vfp->msdf(src_buf, src_stride, ref_address, ref_stride, second_pred,
676 mask, mask_stride, invert_mask);
677 } else if (second_pred) {
678 return vfp->sdaf(src_buf, src_stride, ref_address, ref_stride, second_pred);
679 } else {
chiyotsaic814afb2020-08-04 13:12:35 -0700680 return ms_params->sdf(src_buf, src_stride, ref_address, ref_stride);
chiyotsai94f4aca2020-03-20 12:54:47 -0700681 }
682}
683
chiyotsai9f664e32020-02-19 14:17:31 -0800684// Calculates and returns a sad+mvcost list around an integer best pel during
685// fullpixel motion search. The resulting list can be used to speed up subpel
686// motion search later.
chiyotsai85eecf92020-02-25 17:56:59 -0800687#define USE_SAD_COSTLIST 1
688
689// calc_int_cost_list uses var to populate the costlist, which is more accurate
690// than sad but slightly slower.
chiyotsai2e42a662020-02-26 17:39:03 -0800691static AOM_FORCE_INLINE void calc_int_cost_list(
692 const FULLPEL_MV best_mv, const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
693 int *cost_list) {
chiyotsai6edca112020-02-25 14:31:18 -0800694 static const FULLPEL_MV neighbors[4] = {
695 { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }
696 };
chiyotsai2e42a662020-02-26 17:39:03 -0800697 const int br = best_mv.row;
698 const int bc = best_mv.col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700699
chiyotsai2e42a662020-02-26 17:39:03 -0800700 cost_list[0] = get_mvpred_var_cost(ms_params, &best_mv);
701
702 if (check_bounds(&ms_params->mv_limits, br, bc, 1)) {
chiyotsai4e29a452020-01-27 14:55:52 -0800703 for (int i = 0; i < 4; i++) {
chiyotsaie46cff72020-02-05 15:03:34 -0800704 const FULLPEL_MV neighbor_mv = { br + neighbors[i].row,
705 bc + neighbors[i].col };
chiyotsai2e42a662020-02-26 17:39:03 -0800706 cost_list[i + 1] = get_mvpred_var_cost(ms_params, &neighbor_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700707 }
708 } else {
chiyotsai4e29a452020-01-27 14:55:52 -0800709 for (int i = 0; i < 4; i++) {
chiyotsaie46cff72020-02-05 15:03:34 -0800710 const FULLPEL_MV neighbor_mv = { br + neighbors[i].row,
711 bc + neighbors[i].col };
chiyotsai2e42a662020-02-26 17:39:03 -0800712 if (!av1_is_fullmv_in_range(&ms_params->mv_limits, neighbor_mv)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700713 cost_list[i + 1] = INT_MAX;
chiyotsai4e29a452020-01-27 14:55:52 -0800714 } else {
chiyotsai2e42a662020-02-26 17:39:03 -0800715 cost_list[i + 1] = get_mvpred_var_cost(ms_params, &neighbor_mv);
chiyotsai4e29a452020-01-27 14:55:52 -0800716 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700717 }
718 }
719}
720
chiyotsai2e42a662020-02-26 17:39:03 -0800721// calc_int_sad_list uses sad to populate the costlist, which is less accurate
chiyotsai85eecf92020-02-25 17:56:59 -0800722// than var but faster.
chiyotsai2e42a662020-02-26 17:39:03 -0800723static AOM_FORCE_INLINE void calc_int_sad_list(
724 const FULLPEL_MV best_mv, const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
725 int *cost_list, int costlist_has_sad) {
chiyotsai6edca112020-02-25 14:31:18 -0800726 static const FULLPEL_MV neighbors[4] = {
727 { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }
728 };
chiyotsai94f4aca2020-03-20 12:54:47 -0700729 const struct buf_2d *const src = ms_params->ms_buffers.src;
730 const struct buf_2d *const ref = ms_params->ms_buffers.ref;
chiyotsai2e42a662020-02-26 17:39:03 -0800731 const int ref_stride = ref->stride;
732 const int br = best_mv.row;
733 const int bc = best_mv.col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700734
chiyotsai2e42a662020-02-26 17:39:03 -0800735 assert(av1_is_fullmv_in_range(&ms_params->mv_limits, best_mv));
chiyotsai85eecf92020-02-25 17:56:59 -0800736
737 // Refresh the costlist it does not contain valid sad
738 if (!costlist_has_sad) {
chiyotsai5aa70752020-03-26 10:13:10 -0700739 cost_list[0] = get_mvpred_sad(
740 ms_params, src, get_buf_from_fullmv(ref, &best_mv), ref_stride);
chiyotsai6edca112020-02-25 14:31:18 -0800741
chiyotsai2e42a662020-02-26 17:39:03 -0800742 if (check_bounds(&ms_params->mv_limits, br, bc, 1)) {
chiyotsai4e29a452020-01-27 14:55:52 -0800743 for (int i = 0; i < 4; i++) {
chiyotsaie46cff72020-02-05 15:03:34 -0800744 const FULLPEL_MV this_mv = { br + neighbors[i].row,
745 bc + neighbors[i].col };
chiyotsai2e42a662020-02-26 17:39:03 -0800746 cost_list[i + 1] = get_mvpred_sad(
chiyotsai5aa70752020-03-26 10:13:10 -0700747 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700748 }
749 } else {
chiyotsai4e29a452020-01-27 14:55:52 -0800750 for (int i = 0; i < 4; i++) {
chiyotsaie46cff72020-02-05 15:03:34 -0800751 const FULLPEL_MV this_mv = { br + neighbors[i].row,
752 bc + neighbors[i].col };
chiyotsai2e42a662020-02-26 17:39:03 -0800753 if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700754 cost_list[i + 1] = INT_MAX;
chiyotsai6edca112020-02-25 14:31:18 -0800755 } else {
chiyotsai2e42a662020-02-26 17:39:03 -0800756 cost_list[i + 1] = get_mvpred_sad(
chiyotsai5aa70752020-03-26 10:13:10 -0700757 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
chiyotsai6edca112020-02-25 14:31:18 -0800758 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700759 }
760 }
chiyotsai85eecf92020-02-25 17:56:59 -0800761 }
chiyotsai6edca112020-02-25 14:31:18 -0800762
chiyotsai2e42a662020-02-26 17:39:03 -0800763 const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
764 cost_list[0] += mvsad_err_cost_(&best_mv, mv_cost_params);
chiyotsai85eecf92020-02-25 17:56:59 -0800765
766 for (int idx = 0; idx < 4; idx++) {
767 if (cost_list[idx + 1] != INT_MAX) {
768 const FULLPEL_MV this_mv = { br + neighbors[idx].row,
769 bc + neighbors[idx].col };
chiyotsai2e42a662020-02-26 17:39:03 -0800770 cost_list[idx + 1] += mvsad_err_cost_(&this_mv, mv_cost_params);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700771 }
772 }
773}
774
Cheng Chen5483f602020-04-16 15:29:56 -0700775// Computes motion vector cost and adds to the sad cost.
776// Then updates the best sad and motion vectors.
777// Inputs:
778// this_sad: the sad to be evaluated.
779// mv: the current motion vector.
780// mv_cost_params: a structure containing information to compute mv cost.
781// best_sad: the current best sad.
782// raw_best_sad (optional): the current best sad without calculating mv cost.
783// best_mv: the current best motion vector.
784// second_best_mv (optional): the second best motion vector up to now.
785// Modifies:
786// best_sad, raw_best_sad, best_mv, second_best_mv
787// If the current sad is lower than the current best sad.
788// Returns:
789// Whether the input sad (mv) is better than the current best.
790static int update_mvs_and_sad(const unsigned int this_sad, const FULLPEL_MV *mv,
791 const MV_COST_PARAMS *mv_cost_params,
792 unsigned int *best_sad,
793 unsigned int *raw_best_sad, FULLPEL_MV *best_mv,
794 FULLPEL_MV *second_best_mv) {
795 if (this_sad >= *best_sad) return 0;
796
797 // Add the motion vector cost.
798 const unsigned int sad = this_sad + mvsad_err_cost_(mv, mv_cost_params);
799 if (sad < *best_sad) {
800 if (raw_best_sad) *raw_best_sad = this_sad;
801 *best_sad = sad;
802 if (second_best_mv) *second_best_mv = *best_mv;
803 *best_mv = *mv;
804 return 1;
chiyotsai9f664e32020-02-19 14:17:31 -0800805 }
Cheng Chen5483f602020-04-16 15:29:56 -0700806 return 0;
807}
chiyotsai9f664e32020-02-19 14:17:31 -0800808
venkat sanampudidc5371c2020-07-10 09:36:01 +0530809// Calculate sad4 and update the bestmv information
810// in FAST_DIAMOND search method.
811static void calc_sad4_update_bestmv(
812 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
813 const MV_COST_PARAMS *mv_cost_params, FULLPEL_MV *best_mv,
814 FULLPEL_MV *temp_best_mv, unsigned int *bestsad, unsigned int *raw_bestsad,
venkat sanampudi14affd02020-07-23 07:41:05 +0530815 int search_step, int *best_site, int cand_start) {
venkat sanampudidc5371c2020-07-10 09:36:01 +0530816 const struct buf_2d *const src = ms_params->ms_buffers.src;
817 const struct buf_2d *const ref = ms_params->ms_buffers.ref;
818 const search_site *site = ms_params->search_sites->site[search_step];
819
venkat sanampudidc5371c2020-07-10 09:36:01 +0530820 unsigned char const *block_offset[4];
821 unsigned int sads[4];
822 const uint8_t *best_address;
823 const uint8_t *src_buf = src->buf;
824 const int src_stride = src->stride;
825 best_address = get_buf_from_fullmv(ref, temp_best_mv);
826 // Loop over number of candidates.
venkat sanampudi14affd02020-07-23 07:41:05 +0530827 for (int j = 0; j < 4; j++)
828 block_offset[j] = site[cand_start + j].offset + best_address;
venkat sanampudidc5371c2020-07-10 09:36:01 +0530829
chiyotsaic814afb2020-08-04 13:12:35 -0700830 // 4-point sad calculation.
831 ms_params->sdx4df(src_buf, src_stride, block_offset, ref->stride, sads);
venkat sanampudidc5371c2020-07-10 09:36:01 +0530832
venkat sanampudi14affd02020-07-23 07:41:05 +0530833 for (int j = 0; j < 4; j++) {
834 const FULLPEL_MV this_mv = {
835 temp_best_mv->row + site[cand_start + j].mv.row,
836 temp_best_mv->col + site[cand_start + j].mv.col
837 };
838 const int found_better_mv = update_mvs_and_sad(
839 sads[j], &this_mv, mv_cost_params, bestsad, raw_bestsad, best_mv,
840 /*second_best_mv=*/NULL);
841 if (found_better_mv) *best_site = cand_start + j;
venkat sanampudidc5371c2020-07-10 09:36:01 +0530842 }
843}
844
845// Calculate sad and update the bestmv information
846// in FAST_DIAMOND search method.
847static void calc_sad_update_bestmv(
848 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
849 const MV_COST_PARAMS *mv_cost_params, FULLPEL_MV *best_mv,
850 FULLPEL_MV *temp_best_mv, unsigned int *bestsad, unsigned int *raw_bestsad,
venkat sanampudi14affd02020-07-23 07:41:05 +0530851 int search_step, int *best_site, const int num_candidates, int cand_start) {
venkat sanampudidc5371c2020-07-10 09:36:01 +0530852 const struct buf_2d *const src = ms_params->ms_buffers.src;
853 const struct buf_2d *const ref = ms_params->ms_buffers.ref;
854 const search_site *site = ms_params->search_sites->site[search_step];
855 // Loop over number of candidates.
venkat sanampudi14affd02020-07-23 07:41:05 +0530856 for (int i = cand_start; i < num_candidates; i++) {
venkat sanampudidc5371c2020-07-10 09:36:01 +0530857 const FULLPEL_MV this_mv = { temp_best_mv->row + site[i].mv.row,
858 temp_best_mv->col + site[i].mv.col };
859 if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) continue;
860 int thissad = get_mvpred_sad(
861 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref->stride);
862 const int found_better_mv = update_mvs_and_sad(
863 thissad, &this_mv, mv_cost_params, bestsad, raw_bestsad, best_mv,
864 /*second_best_mv=*/NULL);
865 if (found_better_mv) *best_site = i;
866 }
867}
868
Yaowu Xuc27fc142016-08-22 16:08:15 -0700869// Generic pattern search function that searches over multiple scales.
870// Each scale can have a different number of candidates and shape of
871// candidates as indicated in the num_candidates and candidates arrays
872// passed into this function
venkat sanampudi14affd02020-07-23 07:41:05 +0530873static int pattern_search(FULLPEL_MV start_mv,
874 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
875 int search_step, const int do_init_search,
876 int *cost_list, FULLPEL_MV *best_mv) {
Cheng Chenbb983ba2020-04-21 12:17:53 -0700877 static const int search_steps[MAX_MVSEARCH_STEPS] = {
Yunqing Wange25fdbb2018-03-16 16:36:32 -0700878 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700879 };
880 int i, s, t;
chiyotsai2e42a662020-02-26 17:39:03 -0800881
chiyotsai94f4aca2020-03-20 12:54:47 -0700882 const struct buf_2d *const src = ms_params->ms_buffers.src;
883 const struct buf_2d *const ref = ms_params->ms_buffers.ref;
venkat sanampudi14affd02020-07-23 07:41:05 +0530884 const search_site_config *search_sites = ms_params->search_sites;
885 const int *num_candidates = search_sites->searches_per_step;
chiyotsai2e42a662020-02-26 17:39:03 -0800886 const int ref_stride = ref->stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700887 const int last_is_4 = num_candidates[0] == 4;
888 int br, bc;
Cheng Chen5483f602020-04-16 15:29:56 -0700889 unsigned int bestsad = UINT_MAX, raw_bestsad = UINT_MAX;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700890 int thissad;
891 int k = -1;
chiyotsai2e42a662020-02-26 17:39:03 -0800892 const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
chiyotsaie4020f72020-07-13 15:46:47 -0700893 search_step = AOMMIN(search_step, MAX_MVSEARCH_STEPS - 1);
894 assert(search_step >= 0);
Cheng Chenbb983ba2020-04-21 12:17:53 -0700895 int best_init_s = search_steps[search_step];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700896 // adjust ref_mv to make sure it is within MV range
chiyotsai2e42a662020-02-26 17:39:03 -0800897 clamp_fullmv(&start_mv, &ms_params->mv_limits);
chiyotsai74ee04a2020-02-21 16:58:02 -0800898 br = start_mv.row;
899 bc = start_mv.col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700900 if (cost_list != NULL) {
901 cost_list[0] = cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] =
902 INT_MAX;
903 }
chiyotsai85eecf92020-02-25 17:56:59 -0800904 int costlist_has_sad = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700905
906 // Work out the start point for the search
chiyotsai5aa70752020-03-26 10:13:10 -0700907 raw_bestsad = get_mvpred_sad(ms_params, src,
908 get_buf_from_fullmv(ref, &start_mv), ref_stride);
chiyotsai2e42a662020-02-26 17:39:03 -0800909 bestsad = raw_bestsad + mvsad_err_cost_(&start_mv, mv_cost_params);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700910
chiyotsaie46cff72020-02-05 15:03:34 -0800911 // Search all possible scales up to the search param around the center point
Yaowu Xuc27fc142016-08-22 16:08:15 -0700912 // pick the scale of the point that is best as the starting scale of
913 // further steps around it.
914 if (do_init_search) {
915 s = best_init_s;
916 best_init_s = -1;
917 for (t = 0; t <= s; ++t) {
918 int best_site = -1;
venkat sanampudi14affd02020-07-23 07:41:05 +0530919 FULLPEL_MV temp_best_mv;
920 temp_best_mv.row = br;
921 temp_best_mv.col = bc;
chiyotsai2e42a662020-02-26 17:39:03 -0800922 if (check_bounds(&ms_params->mv_limits, br, bc, 1 << t)) {
venkat sanampudi14affd02020-07-23 07:41:05 +0530923 // Call 4-point sad for multiples of 4 candidates.
924 const int no_of_4_cand_loops = num_candidates[t] >> 2;
925 for (i = 0; i < no_of_4_cand_loops; i++) {
926 calc_sad4_update_bestmv(ms_params, mv_cost_params, best_mv,
927 &temp_best_mv, &bestsad, &raw_bestsad, t,
928 &best_site, i * 4);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700929 }
venkat sanampudi14affd02020-07-23 07:41:05 +0530930 // Rest of the candidates
931 const int remaining_cand = num_candidates[t] % 4;
932 calc_sad_update_bestmv(ms_params, mv_cost_params, best_mv,
933 &temp_best_mv, &bestsad, &raw_bestsad, t,
934 &best_site, remaining_cand,
935 no_of_4_cand_loops * 4);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700936 } else {
venkat sanampudi14affd02020-07-23 07:41:05 +0530937 calc_sad_update_bestmv(ms_params, mv_cost_params, best_mv,
938 &temp_best_mv, &bestsad, &raw_bestsad, t,
939 &best_site, num_candidates[t], 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700940 }
941 if (best_site == -1) {
942 continue;
943 } else {
944 best_init_s = t;
945 k = best_site;
946 }
947 }
948 if (best_init_s != -1) {
venkat sanampudi14affd02020-07-23 07:41:05 +0530949 br += search_sites->site[best_init_s][k].mv.row;
950 bc += search_sites->site[best_init_s][k].mv.col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700951 }
952 }
953
954 // If the center point is still the best, just skip this and move to
955 // the refinement step.
956 if (best_init_s != -1) {
957 const int last_s = (last_is_4 && cost_list != NULL);
958 int best_site = -1;
959 s = best_init_s;
960
961 for (; s >= last_s; s--) {
962 // No need to search all points the 1st time if initial search was used
963 if (!do_init_search || s != best_init_s) {
venkat sanampudi14affd02020-07-23 07:41:05 +0530964 FULLPEL_MV temp_best_mv;
965 temp_best_mv.row = br;
966 temp_best_mv.col = bc;
chiyotsai2e42a662020-02-26 17:39:03 -0800967 if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) {
venkat sanampudi14affd02020-07-23 07:41:05 +0530968 // Call 4-point sad for multiples of 4 candidates.
969 const int no_of_4_cand_loops = num_candidates[s] >> 2;
970 for (i = 0; i < no_of_4_cand_loops; i++) {
971 calc_sad4_update_bestmv(ms_params, mv_cost_params, best_mv,
972 &temp_best_mv, &bestsad, &raw_bestsad, s,
973 &best_site, i * 4);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700974 }
venkat sanampudi14affd02020-07-23 07:41:05 +0530975 // Rest of the candidates
976 const int remaining_cand = num_candidates[s] % 4;
977 calc_sad_update_bestmv(ms_params, mv_cost_params, best_mv,
978 &temp_best_mv, &bestsad, &raw_bestsad, s,
979 &best_site, remaining_cand,
980 no_of_4_cand_loops * 4);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700981 } else {
venkat sanampudi14affd02020-07-23 07:41:05 +0530982 calc_sad_update_bestmv(ms_params, mv_cost_params, best_mv,
983 &temp_best_mv, &bestsad, &raw_bestsad, s,
984 &best_site, num_candidates[s], 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700985 }
986
987 if (best_site == -1) {
988 continue;
989 } else {
venkat sanampudi14affd02020-07-23 07:41:05 +0530990 br += search_sites->site[s][best_site].mv.row;
991 bc += search_sites->site[s][best_site].mv.col;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700992 k = best_site;
993 }
994 }
995
996 do {
997 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
998 best_site = -1;
999 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1000 next_chkpts_indices[1] = k;
1001 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1002
chiyotsai2e42a662020-02-26 17:39:03 -08001003 if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001004 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
chiyotsaie46cff72020-02-05 15:03:34 -08001005 const FULLPEL_MV this_mv = {
venkat sanampudi14affd02020-07-23 07:41:05 +05301006 br + search_sites->site[s][next_chkpts_indices[i]].mv.row,
1007 bc + search_sites->site[s][next_chkpts_indices[i]].mv.col
Yaowu Xuc27fc142016-08-22 16:08:15 -07001008 };
chiyotsai2e42a662020-02-26 17:39:03 -08001009 thissad = get_mvpred_sad(
chiyotsai5aa70752020-03-26 10:13:10 -07001010 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
Cheng Chen5483f602020-04-16 15:29:56 -07001011 const int found_better_mv =
1012 update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad,
1013 &raw_bestsad, best_mv,
1014 /*second_best_mv=*/NULL);
1015 if (found_better_mv) best_site = i;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001016 }
1017 } else {
1018 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
chiyotsaie46cff72020-02-05 15:03:34 -08001019 const FULLPEL_MV this_mv = {
venkat sanampudi14affd02020-07-23 07:41:05 +05301020 br + search_sites->site[s][next_chkpts_indices[i]].mv.row,
1021 bc + search_sites->site[s][next_chkpts_indices[i]].mv.col
Yaowu Xuc27fc142016-08-22 16:08:15 -07001022 };
chiyotsai2e42a662020-02-26 17:39:03 -08001023 if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv))
1024 continue;
1025 thissad = get_mvpred_sad(
chiyotsai5aa70752020-03-26 10:13:10 -07001026 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
Cheng Chen5483f602020-04-16 15:29:56 -07001027 const int found_better_mv =
1028 update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad,
1029 &raw_bestsad, best_mv,
1030 /*second_best_mv=*/NULL);
1031 if (found_better_mv) best_site = i;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001032 }
1033 }
1034
1035 if (best_site != -1) {
1036 k = next_chkpts_indices[best_site];
venkat sanampudi14affd02020-07-23 07:41:05 +05301037 br += search_sites->site[s][k].mv.row;
1038 bc += search_sites->site[s][k].mv.col;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001039 }
1040 } while (best_site != -1);
1041 }
1042
1043 // Note: If we enter the if below, then cost_list must be non-NULL.
1044 if (s == 0) {
chiyotsai6edca112020-02-25 14:31:18 -08001045 cost_list[0] = raw_bestsad;
chiyotsai85eecf92020-02-25 17:56:59 -08001046 costlist_has_sad = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001047 if (!do_init_search || s != best_init_s) {
chiyotsai2e42a662020-02-26 17:39:03 -08001048 if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001049 for (i = 0; i < num_candidates[s]; i++) {
venkat sanampudi14affd02020-07-23 07:41:05 +05301050 const FULLPEL_MV this_mv = { br + search_sites->site[s][i].mv.row,
1051 bc + search_sites->site[s][i].mv.col };
chiyotsai2e42a662020-02-26 17:39:03 -08001052 cost_list[i + 1] = thissad = get_mvpred_sad(
chiyotsai5aa70752020-03-26 10:13:10 -07001053 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
Cheng Chen5483f602020-04-16 15:29:56 -07001054 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;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001059 }
1060 } else {
1061 for (i = 0; i < num_candidates[s]; i++) {
venkat sanampudi14affd02020-07-23 07:41:05 +05301062 const FULLPEL_MV this_mv = { br + search_sites->site[s][i].mv.row,
1063 bc + search_sites->site[s][i].mv.col };
chiyotsai2e42a662020-02-26 17:39:03 -08001064 if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv))
1065 continue;
1066 cost_list[i + 1] = thissad = get_mvpred_sad(
chiyotsai5aa70752020-03-26 10:13:10 -07001067 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
Cheng Chen5483f602020-04-16 15:29:56 -07001068 const int found_better_mv =
1069 update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad,
1070 &raw_bestsad, best_mv,
1071 /*second_best_mv=*/NULL);
1072 if (found_better_mv) best_site = i;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001073 }
1074 }
1075
1076 if (best_site != -1) {
venkat sanampudi14affd02020-07-23 07:41:05 +05301077 br += search_sites->site[s][best_site].mv.row;
1078 bc += search_sites->site[s][best_site].mv.col;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001079 k = best_site;
1080 }
1081 }
1082 while (best_site != -1) {
1083 int next_chkpts_indices[PATTERN_CANDIDATES_REF];
1084 best_site = -1;
1085 next_chkpts_indices[0] = (k == 0) ? num_candidates[s] - 1 : k - 1;
1086 next_chkpts_indices[1] = k;
1087 next_chkpts_indices[2] = (k == num_candidates[s] - 1) ? 0 : k + 1;
1088 cost_list[1] = cost_list[2] = cost_list[3] = cost_list[4] = INT_MAX;
1089 cost_list[((k + 2) % 4) + 1] = cost_list[0];
chiyotsai6edca112020-02-25 14:31:18 -08001090 cost_list[0] = raw_bestsad;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001091
chiyotsai2e42a662020-02-26 17:39:03 -08001092 if (check_bounds(&ms_params->mv_limits, br, bc, 1 << s)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001093 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
chiyotsaie46cff72020-02-05 15:03:34 -08001094 const FULLPEL_MV this_mv = {
venkat sanampudi14affd02020-07-23 07:41:05 +05301095 br + search_sites->site[s][next_chkpts_indices[i]].mv.row,
1096 bc + search_sites->site[s][next_chkpts_indices[i]].mv.col
Yaowu Xuc27fc142016-08-22 16:08:15 -07001097 };
chiyotsai2e42a662020-02-26 17:39:03 -08001098 cost_list[next_chkpts_indices[i] + 1] = thissad = get_mvpred_sad(
chiyotsai5aa70752020-03-26 10:13:10 -07001099 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
Cheng Chen5483f602020-04-16 15:29:56 -07001100 const int found_better_mv =
1101 update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad,
1102 &raw_bestsad, best_mv,
1103 /*second_best_mv=*/NULL);
1104 if (found_better_mv) best_site = i;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001105 }
1106 } else {
1107 for (i = 0; i < PATTERN_CANDIDATES_REF; i++) {
chiyotsaie46cff72020-02-05 15:03:34 -08001108 const FULLPEL_MV this_mv = {
venkat sanampudi14affd02020-07-23 07:41:05 +05301109 br + search_sites->site[s][next_chkpts_indices[i]].mv.row,
1110 bc + search_sites->site[s][next_chkpts_indices[i]].mv.col
Yaowu Xuc27fc142016-08-22 16:08:15 -07001111 };
chiyotsai2e42a662020-02-26 17:39:03 -08001112 if (!av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001113 cost_list[next_chkpts_indices[i] + 1] = INT_MAX;
1114 continue;
1115 }
chiyotsai2e42a662020-02-26 17:39:03 -08001116 cost_list[next_chkpts_indices[i] + 1] = thissad = get_mvpred_sad(
chiyotsai5aa70752020-03-26 10:13:10 -07001117 ms_params, src, get_buf_from_fullmv(ref, &this_mv), ref_stride);
Cheng Chen5483f602020-04-16 15:29:56 -07001118 const int found_better_mv =
1119 update_mvs_and_sad(thissad, &this_mv, mv_cost_params, &bestsad,
1120 &raw_bestsad, best_mv,
1121 /*second_best_mv=*/NULL);
1122 if (found_better_mv) best_site = i;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001123 }
1124 }
1125
1126 if (best_site != -1) {
1127 k = next_chkpts_indices[best_site];
venkat sanampudi14affd02020-07-23 07:41:05 +05301128 br += search_sites->site[s][k].mv.row;
1129 bc += search_sites->site[s][k].mv.col;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001130 }
1131 }
1132 }
1133 }
1134
chiyotsai8bbdd412020-03-03 14:57:23 -08001135 best_mv->row = br;
1136 best_mv->col = bc;
1137
Yaowu Xuc27fc142016-08-22 16:08:15 -07001138 // Returns the one-away integer pel cost/sad around the best as follows:
1139 // cost_list[0]: cost/sad at the best integer pel
1140 // cost_list[1]: cost/sad at delta {0, -1} (left) from the best integer pel
1141 // cost_list[2]: cost/sad at delta { 1, 0} (bottom) from the best integer pel
1142 // cost_list[3]: cost/sad at delta { 0, 1} (right) from the best integer pel
1143 // cost_list[4]: cost/sad at delta {-1, 0} (top) from the best integer pel
1144 if (cost_list) {
chiyotsai85eecf92020-02-25 17:56:59 -08001145 if (USE_SAD_COSTLIST) {
chiyotsai2e42a662020-02-26 17:39:03 -08001146 calc_int_sad_list(*best_mv, ms_params, cost_list, costlist_has_sad);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001147 } else {
chiyotsai2e42a662020-02-26 17:39:03 -08001148 calc_int_cost_list(*best_mv, ms_params, cost_list);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001149 }
1150 }
chiyotsai2e42a662020-02-26 17:39:03 -08001151 best_mv->row = br;
1152 best_mv->col = bc;
chiyotsai2cbd6402020-02-26 14:49:00 -08001153
chiyotsai2e42a662020-02-26 17:39:03 -08001154 const int var_cost = get_mvpred_var_cost(ms_params, best_mv);
chiyotsai2cbd6402020-02-26 14:49:00 -08001155 return var_cost;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001156}
David Barkerc155e012017-05-11 13:54:54 +01001157
chiyotsaie46cff72020-02-05 15:03:34 -08001158// For the following foo_search, the input arguments are:
chiyotsaie46cff72020-02-05 15:03:34 -08001159// start_mv: where we are starting our motion search
Cheng Chenbb983ba2020-04-21 12:17:53 -07001160// ms_params: a collection of motion search parameters
1161// search_step: how many steps to skip in our motion search. For example,
chiyotsaie46cff72020-02-05 15:03:34 -08001162// a value 3 suggests that 3 search steps have already taken place prior to
1163// this function call, so we jump directly to step 4 of the search process
chiyotsaie46cff72020-02-05 15:03:34 -08001164// do_init_search: if on, do an initial search of all possible scales around the
1165// start_mv, and then pick the best scale.
1166// cond_list: used to hold the cost around the best full mv so we can use it to
1167// speed up subpel search later.
Cheng Chenbb983ba2020-04-21 12:17:53 -07001168// best_mv: the best mv found in the motion search
chiyotsai2e42a662020-02-26 17:39:03 -08001169static int hex_search(const FULLPEL_MV start_mv,
1170 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
Cheng Chenbb983ba2020-04-21 12:17:53 -07001171 const int search_step, const int do_init_search,
chiyotsai2e42a662020-02-26 17:39:03 -08001172 int *cost_list, FULLPEL_MV *best_mv) {
Cheng Chenbb983ba2020-04-21 12:17:53 -07001173 return pattern_search(start_mv, ms_params, search_step, do_init_search,
venkat sanampudi14affd02020-07-23 07:41:05 +05301174 cost_list, best_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001175}
1176
chiyotsai2e42a662020-02-26 17:39:03 -08001177static int bigdia_search(const FULLPEL_MV start_mv,
1178 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
Cheng Chenbb983ba2020-04-21 12:17:53 -07001179 const int search_step, const int do_init_search,
chiyotsai2e42a662020-02-26 17:39:03 -08001180 int *cost_list, FULLPEL_MV *best_mv) {
Cheng Chenbb983ba2020-04-21 12:17:53 -07001181 return pattern_search(start_mv, ms_params, search_step, do_init_search,
venkat sanampudi14affd02020-07-23 07:41:05 +05301182 cost_list, best_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001183}
1184
chiyotsai2e42a662020-02-26 17:39:03 -08001185static int square_search(const FULLPEL_MV start_mv,
1186 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
Cheng Chenbb983ba2020-04-21 12:17:53 -07001187 const int search_step, const int do_init_search,
chiyotsai2e42a662020-02-26 17:39:03 -08001188 int *cost_list, FULLPEL_MV *best_mv) {
Cheng Chenbb983ba2020-04-21 12:17:53 -07001189 return pattern_search(start_mv, ms_params, search_step, do_init_search,
venkat sanampudi14affd02020-07-23 07:41:05 +05301190 cost_list, best_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001191}
1192
chiyotsai2e42a662020-02-26 17:39:03 -08001193static int fast_hex_search(const FULLPEL_MV start_mv,
1194 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
Cheng Chenbb983ba2020-04-21 12:17:53 -07001195 const int search_step, const int do_init_search,
chiyotsai2e42a662020-02-26 17:39:03 -08001196 int *cost_list, FULLPEL_MV *best_mv) {
1197 return hex_search(start_mv, ms_params,
Cheng Chenbb983ba2020-04-21 12:17:53 -07001198 AOMMAX(MAX_MVSEARCH_STEPS - 2, search_step), do_init_search,
1199 cost_list, best_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001200}
1201
chiyotsai2e42a662020-02-26 17:39:03 -08001202static int fast_dia_search(const FULLPEL_MV start_mv,
1203 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
Cheng Chenbb983ba2020-04-21 12:17:53 -07001204 const int search_step, const int do_init_search,
chiyotsai2e42a662020-02-26 17:39:03 -08001205 int *cost_list, FULLPEL_MV *best_mv) {
venkat sanampudiaeada122020-07-17 13:14:42 +05301206 return bigdia_search(start_mv, ms_params,
1207 AOMMAX(MAX_MVSEARCH_STEPS - 2, search_step),
1208 do_init_search, cost_list, best_mv);
1209}
1210
1211static int fast_bigdia_search(const FULLPEL_MV start_mv,
1212 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1213 const int search_step, const int do_init_search,
1214 int *cost_list, FULLPEL_MV *best_mv) {
venkat sanampudi14affd02020-07-23 07:41:05 +05301215 return bigdia_search(start_mv, ms_params,
1216 AOMMAX(MAX_MVSEARCH_STEPS - 3, search_step),
1217 do_init_search, cost_list, best_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001218}
1219
chiyotsai2e42a662020-02-26 17:39:03 -08001220static int diamond_search_sad(FULLPEL_MV start_mv,
1221 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
Cheng Chenbb983ba2020-04-21 12:17:53 -07001222 const int search_step, int *num00,
chiyotsai2e42a662020-02-26 17:39:03 -08001223 FULLPEL_MV *best_mv, FULLPEL_MV *second_best_mv) {
chiyotsai94f4aca2020-03-20 12:54:47 -07001224 const struct buf_2d *const src = ms_params->ms_buffers.src;
1225 const struct buf_2d *const ref = ms_params->ms_buffers.ref;
chiyotsai2e42a662020-02-26 17:39:03 -08001226
1227 const int ref_stride = ref->stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001228 const uint8_t *best_address;
1229
chiyotsai94f4aca2020-03-20 12:54:47 -07001230 const uint8_t *mask = ms_params->ms_buffers.mask;
1231 const uint8_t *second_pred = ms_params->ms_buffers.second_pred;
chiyotsai2e42a662020-02-26 17:39:03 -08001232 const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
1233
1234 const search_site_config *cfg = ms_params->search_sites;
1235
Yaowu Xuc27fc142016-08-22 16:08:15 -07001236 unsigned int bestsad = INT_MAX;
1237 int best_site = 0;
Jingning Han4d780b02019-11-22 09:48:14 -08001238 int is_off_center = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001239
chiyotsai2e42a662020-02-26 17:39:03 -08001240 clamp_fullmv(&start_mv, &ms_params->mv_limits);
chiyotsai74ee04a2020-02-21 16:58:02 -08001241
Cheng Chenbb983ba2020-04-21 12:17:53 -07001242 // search_step determines the length of the initial step and hence the number
Yaowu Xuc27fc142016-08-22 16:08:15 -07001243 // of iterations.
Cheng Chen66190d92020-04-21 12:58:43 -07001244 const int tot_steps = cfg->num_search_steps - search_step;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001245
Yaowu Xuc27fc142016-08-22 16:08:15 -07001246 *num00 = 0;
chiyotsai74ee04a2020-02-21 16:58:02 -08001247 *best_mv = start_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001248
Yaowu Xuc27fc142016-08-22 16:08:15 -07001249 // Check the starting position
chiyotsai5aa70752020-03-26 10:13:10 -07001250 best_address = get_buf_from_fullmv(ref, &start_mv);
chiyotsai2e42a662020-02-26 17:39:03 -08001251 bestsad = get_mvpred_compound_sad(ms_params, src, best_address, ref_stride);
1252 bestsad += mvsad_err_cost_(best_mv, &ms_params->mv_cost_params);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001253
Jingning Hana1e7dfe2020-01-03 15:36:27 -08001254 int next_step_size = tot_steps > 2 ? cfg->radius[tot_steps - 2] : 1;
Jingning Han8c368032019-12-03 14:56:47 -08001255 for (int step = tot_steps - 1; step >= 0; --step) {
Cheng Chen66190d92020-04-21 12:58:43 -07001256 const search_site *site = cfg->site[step];
Jingning Han4d780b02019-11-22 09:48:14 -08001257 best_site = 0;
Jingning Hana1e7dfe2020-01-03 15:36:27 -08001258 if (step > 0) next_step_size = cfg->radius[step - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07001259
Ruiling Songddafbe22019-12-09 09:00:07 +08001260 int all_in = 1, j;
1261 // Trap illegal vectors
Cheng Chen66190d92020-04-21 12:58:43 -07001262 all_in &= best_mv->row + site[1].mv.row >= ms_params->mv_limits.row_min;
1263 all_in &= best_mv->row + site[2].mv.row <= ms_params->mv_limits.row_max;
1264 all_in &= best_mv->col + site[3].mv.col >= ms_params->mv_limits.col_min;
1265 all_in &= best_mv->col + site[4].mv.col <= ms_params->mv_limits.col_max;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001266
Ruiling Songddafbe22019-12-09 09:00:07 +08001267 // TODO(anyone): Implement 4 points search for msdf&sdaf
1268 if (all_in && !mask && !second_pred) {
chiyotsai2e42a662020-02-26 17:39:03 -08001269 const uint8_t *src_buf = src->buf;
1270 const int src_stride = src->stride;
Ruiling Songddafbe22019-12-09 09:00:07 +08001271 for (int idx = 1; idx <= cfg->searches_per_step[step]; idx += 4) {
1272 unsigned char const *block_offset[4];
1273 unsigned int sads[4];
Jingning Hanaaa34672019-11-25 14:29:26 -08001274
Ruiling Songddafbe22019-12-09 09:00:07 +08001275 for (j = 0; j < 4; j++)
Cheng Chen66190d92020-04-21 12:58:43 -07001276 block_offset[j] = site[idx + j].offset + best_address;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001277
chiyotsaic814afb2020-08-04 13:12:35 -07001278 ms_params->sdx4df(src_buf, src_stride, block_offset, ref_stride, sads);
Ruiling Songddafbe22019-12-09 09:00:07 +08001279 for (j = 0; j < 4; j++) {
1280 if (sads[j] < bestsad) {
Cheng Chen66190d92020-04-21 12:58:43 -07001281 const FULLPEL_MV this_mv = { best_mv->row + site[idx + j].mv.row,
1282 best_mv->col + site[idx + j].mv.col };
Ruiling Songddafbe22019-12-09 09:00:07 +08001283 unsigned int thissad =
chiyotsai2e42a662020-02-26 17:39:03 -08001284 sads[j] + mvsad_err_cost_(&this_mv, mv_cost_params);
Ruiling Songddafbe22019-12-09 09:00:07 +08001285 if (thissad < bestsad) {
1286 bestsad = thissad;
1287 best_site = idx + j;
1288 }
1289 }
1290 }
1291 }
1292 } else {
1293 for (int idx = 1; idx <= cfg->searches_per_step[step]; idx++) {
Cheng Chen66190d92020-04-21 12:58:43 -07001294 const FULLPEL_MV this_mv = { best_mv->row + site[idx].mv.row,
1295 best_mv->col + site[idx].mv.col };
Ruiling Songddafbe22019-12-09 09:00:07 +08001296
chiyotsai2e42a662020-02-26 17:39:03 -08001297 if (av1_is_fullmv_in_range(&ms_params->mv_limits, this_mv)) {
Cheng Chen66190d92020-04-21 12:58:43 -07001298 const uint8_t *const check_here = site[idx].offset + best_address;
Ruiling Songddafbe22019-12-09 09:00:07 +08001299 unsigned int thissad;
1300
chiyotsai2e42a662020-02-26 17:39:03 -08001301 thissad =
1302 get_mvpred_compound_sad(ms_params, src, check_here, ref_stride);
Ruiling Songddafbe22019-12-09 09:00:07 +08001303
Yaowu Xuc27fc142016-08-22 16:08:15 -07001304 if (thissad < bestsad) {
chiyotsai2e42a662020-02-26 17:39:03 -08001305 thissad += mvsad_err_cost_(&this_mv, mv_cost_params);
Ruiling Songddafbe22019-12-09 09:00:07 +08001306 if (thissad < bestsad) {
1307 bestsad = thissad;
1308 best_site = idx;
1309 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001310 }
1311 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001312 }
1313 }
Jingning Han4d780b02019-11-22 09:48:14 -08001314
1315 if (best_site != 0) {
chiyotsai8bbdd412020-03-03 14:57:23 -08001316 if (second_best_mv) {
1317 *second_best_mv = *best_mv;
1318 }
Cheng Chen66190d92020-04-21 12:58:43 -07001319 best_mv->row += site[best_site].mv.row;
1320 best_mv->col += site[best_site].mv.col;
1321 best_address += site[best_site].offset;
Jingning Han4d780b02019-11-22 09:48:14 -08001322 is_off_center = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001323 }
Jingning Han4d780b02019-11-22 09:48:14 -08001324
Jingning Hanb7dad712019-11-22 15:11:56 -08001325 if (is_off_center == 0) (*num00)++;
Jingning Hana1e7dfe2020-01-03 15:36:27 -08001326
1327 if (best_site == 0) {
1328 while (next_step_size == cfg->radius[step] && step > 2) {
1329 ++(*num00);
1330 --step;
1331 next_step_size = cfg->radius[step - 1];
1332 }
1333 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001334 }
Jingning Han4d780b02019-11-22 09:48:14 -08001335
Yaowu Xuc27fc142016-08-22 16:08:15 -07001336 return bestsad;
1337}
1338
Yaowu Xuc27fc142016-08-22 16:08:15 -07001339/* do_refine: If last step (1-away) of n-step search doesn't pick the center
1340 point as the best match, we will do a final 1-away diamond
1341 refining search */
chiyotsai2e42a662020-02-26 17:39:03 -08001342static int full_pixel_diamond(const FULLPEL_MV start_mv,
1343 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1344 const int step_param, int *cost_list,
chiyotsai8bbdd412020-03-03 14:57:23 -08001345 FULLPEL_MV *best_mv, FULLPEL_MV *second_best_mv) {
chiyotsai2e42a662020-02-26 17:39:03 -08001346 const search_site_config *cfg = ms_params->search_sites;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001347 int thissme, n, num00 = 0;
chiyotsai2e42a662020-02-26 17:39:03 -08001348 int bestsme = diamond_search_sad(start_mv, ms_params, step_param, &n, best_mv,
1349 second_best_mv);
Jingning Han5e95d282019-12-19 12:04:17 -08001350
1351 if (bestsme < INT_MAX) {
chiyotsai2e42a662020-02-26 17:39:03 -08001352 bestsme = get_mvpred_compound_var_cost(ms_params, best_mv);
Jingning Han5e95d282019-12-19 12:04:17 -08001353 }
1354
Yaowu Xuc27fc142016-08-22 16:08:15 -07001355 // If there won't be more n-step search, check to see if refining search is
1356 // needed.
Cheng Chen66190d92020-04-21 12:58:43 -07001357 const int further_steps = cfg->num_search_steps - 1 - step_param;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001358 while (n < further_steps) {
1359 ++n;
1360
1361 if (num00) {
1362 num00--;
1363 } else {
chiyotsai8bbdd412020-03-03 14:57:23 -08001364 // TODO(chiyotsai@google.com): There is another bug here where the second
1365 // best mv gets incorrectly overwritten. Fix it later.
chiyotsai2e42a662020-02-26 17:39:03 -08001366 FULLPEL_MV tmp_best_mv;
1367 thissme = diamond_search_sad(start_mv, ms_params, step_param + n, &num00,
1368 &tmp_best_mv, second_best_mv);
Jingning Han5e95d282019-12-19 12:04:17 -08001369
1370 if (thissme < INT_MAX) {
chiyotsai2e42a662020-02-26 17:39:03 -08001371 thissme = get_mvpred_compound_var_cost(ms_params, &tmp_best_mv);
Jingning Han5e95d282019-12-19 12:04:17 -08001372 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001373
Yaowu Xuc27fc142016-08-22 16:08:15 -07001374 if (thissme < bestsme) {
1375 bestsme = thissme;
chiyotsai8bbdd412020-03-03 14:57:23 -08001376 *best_mv = tmp_best_mv;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001377 }
1378 }
1379 }
1380
Yaowu Xuc27fc142016-08-22 16:08:15 -07001381 // Return cost list.
1382 if (cost_list) {
chiyotsai85eecf92020-02-25 17:56:59 -08001383 if (USE_SAD_COSTLIST) {
1384 const int costlist_has_sad = 0;
chiyotsai2e42a662020-02-26 17:39:03 -08001385 calc_int_sad_list(*best_mv, ms_params, cost_list, costlist_has_sad);
chiyotsai85eecf92020-02-25 17:56:59 -08001386 } else {
chiyotsai2e42a662020-02-26 17:39:03 -08001387 calc_int_cost_list(*best_mv, ms_params, cost_list);
chiyotsai85eecf92020-02-25 17:56:59 -08001388 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001389 }
1390 return bestsme;
1391}
1392
chiyotsai2e42a662020-02-26 17:39:03 -08001393// Exhaustive motion search around a given centre position with a given
1394// step size.
1395static int exhaustive_mesh_search(FULLPEL_MV start_mv,
1396 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1397 const int range, const int step,
1398 FULLPEL_MV *best_mv,
1399 FULLPEL_MV *second_best_mv) {
chiyotsai2e42a662020-02-26 17:39:03 -08001400 const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
chiyotsai94f4aca2020-03-20 12:54:47 -07001401 const struct buf_2d *const src = ms_params->ms_buffers.src;
1402 const struct buf_2d *const ref = ms_params->ms_buffers.ref;
chiyotsai2e42a662020-02-26 17:39:03 -08001403 const int ref_stride = ref->stride;
1404 unsigned int best_sad = INT_MAX;
1405 int r, c, i;
1406 int start_col, end_col, start_row, end_row;
Cheng Chen5483f602020-04-16 15:29:56 -07001407 const int col_step = (step > 1) ? step : 4;
chiyotsai2e42a662020-02-26 17:39:03 -08001408
1409 assert(step >= 1);
1410
1411 clamp_fullmv(&start_mv, &ms_params->mv_limits);
1412 *best_mv = start_mv;
chiyotsai5aa70752020-03-26 10:13:10 -07001413 best_sad = get_mvpred_sad(ms_params, src, get_buf_from_fullmv(ref, &start_mv),
chiyotsai2e42a662020-02-26 17:39:03 -08001414 ref_stride);
1415 best_sad += mvsad_err_cost_(&start_mv, mv_cost_params);
1416 start_row = AOMMAX(-range, ms_params->mv_limits.row_min - start_mv.row);
1417 start_col = AOMMAX(-range, ms_params->mv_limits.col_min - start_mv.col);
1418 end_row = AOMMIN(range, ms_params->mv_limits.row_max - start_mv.row);
1419 end_col = AOMMIN(range, ms_params->mv_limits.col_max - start_mv.col);
1420
1421 for (r = start_row; r <= end_row; r += step) {
1422 for (c = start_col; c <= end_col; c += col_step) {
1423 // Step > 1 means we are not checking every location in this pass.
1424 if (step > 1) {
1425 const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c };
1426 unsigned int sad = get_mvpred_sad(
chiyotsai5aa70752020-03-26 10:13:10 -07001427 ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride);
Cheng Chen5483f602020-04-16 15:29:56 -07001428 update_mvs_and_sad(sad, &mv, mv_cost_params, &best_sad,
1429 /*raw_best_sad=*/NULL, best_mv, second_best_mv);
chiyotsai2e42a662020-02-26 17:39:03 -08001430 } else {
1431 // 4 sads in a single call if we are checking every location
1432 if (c + 3 <= end_col) {
1433 unsigned int sads[4];
1434 const uint8_t *addrs[4];
1435 for (i = 0; i < 4; ++i) {
1436 const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i };
chiyotsai5aa70752020-03-26 10:13:10 -07001437 addrs[i] = get_buf_from_fullmv(ref, &mv);
chiyotsai2e42a662020-02-26 17:39:03 -08001438 }
chiyotsaic814afb2020-08-04 13:12:35 -07001439
1440 ms_params->sdx4df(src->buf, src->stride, addrs, ref_stride, sads);
chiyotsai2e42a662020-02-26 17:39:03 -08001441
1442 for (i = 0; i < 4; ++i) {
1443 if (sads[i] < best_sad) {
1444 const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i };
Cheng Chen5483f602020-04-16 15:29:56 -07001445 update_mvs_and_sad(sads[i], &mv, mv_cost_params, &best_sad,
1446 /*raw_best_sad=*/NULL, best_mv,
1447 second_best_mv);
chiyotsai2e42a662020-02-26 17:39:03 -08001448 }
1449 }
1450 } else {
1451 for (i = 0; i < end_col - c; ++i) {
1452 const FULLPEL_MV mv = { start_mv.row + r, start_mv.col + c + i };
1453 unsigned int sad = get_mvpred_sad(
chiyotsai5aa70752020-03-26 10:13:10 -07001454 ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride);
Cheng Chen5483f602020-04-16 15:29:56 -07001455 update_mvs_and_sad(sad, &mv, mv_cost_params, &best_sad,
1456 /*raw_best_sad=*/NULL, best_mv, second_best_mv);
chiyotsai2e42a662020-02-26 17:39:03 -08001457 }
1458 }
1459 }
1460 }
1461 }
1462
1463 return best_sad;
1464}
1465
Yaowu Xuc27fc142016-08-22 16:08:15 -07001466// Runs an limited range exhaustive mesh search using a pattern set
1467// according to the encode speed profile.
chiyotsai2e42a662020-02-26 17:39:03 -08001468static int full_pixel_exhaustive(const FULLPEL_MV start_mv,
1469 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1470 const struct MESH_PATTERN *const mesh_patterns,
1471 int *cost_list, FULLPEL_MV *best_mv,
1472 FULLPEL_MV *second_best_mv) {
1473 const int kMinRange = 7;
1474 const int kMaxRange = 256;
1475 const int kMinInterval = 1;
1476
Yaowu Xuc27fc142016-08-22 16:08:15 -07001477 int bestsme;
1478 int i;
Ranjit Kumar Tulabandu5e1f7012019-10-18 19:01:46 +05301479 int interval = mesh_patterns[0].interval;
1480 int range = mesh_patterns[0].range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001481 int baseline_interval_divisor;
1482
chiyotsai74ee04a2020-02-21 16:58:02 -08001483 *best_mv = start_mv;
chiyotsaie46cff72020-02-05 15:03:34 -08001484
Yaowu Xuc27fc142016-08-22 16:08:15 -07001485 // Trap illegal values for interval and range for this function.
chiyotsai2e42a662020-02-26 17:39:03 -08001486 if ((range < kMinRange) || (range > kMaxRange) || (interval < kMinInterval) ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07001487 (interval > range))
1488 return INT_MAX;
1489
1490 baseline_interval_divisor = range / interval;
1491
1492 // Check size of proposed first range against magnitude of the centre
1493 // value used as a starting point.
chiyotsaie46cff72020-02-05 15:03:34 -08001494 range = AOMMAX(range, (5 * AOMMAX(abs(best_mv->row), abs(best_mv->col))) / 4);
chiyotsai2e42a662020-02-26 17:39:03 -08001495 range = AOMMIN(range, kMaxRange);
Yaowu Xuf883b422016-08-30 14:01:10 -07001496 interval = AOMMAX(interval, range / baseline_interval_divisor);
Cheng Chencf16b022020-04-15 11:19:23 -07001497 // Use a small search step/interval for certain kind of clips.
1498 // For example, screen content clips with a lot of texts.
1499 // Large interval could lead to a false matching position, and it can't find
1500 // the best global candidate in following iterations due to reduced search
1501 // range. The solution here is to use a small search iterval in the beginning
1502 // and thus reduces the chance of missing the best candidate.
1503 if (ms_params->fine_search_interval) {
1504 interval = AOMMIN(interval, 4);
1505 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001506
1507 // initial search
chiyotsai2e42a662020-02-26 17:39:03 -08001508 bestsme = exhaustive_mesh_search(*best_mv, ms_params, range, interval,
1509 best_mv, second_best_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001510
chiyotsai2e42a662020-02-26 17:39:03 -08001511 if ((interval > kMinInterval) && (range > kMinRange)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001512 // Progressive searches with range and step size decreasing each time
1513 // till we reach a step size of 1. Then break out.
1514 for (i = 1; i < MAX_MESH_STEP; ++i) {
1515 // First pass with coarser step and longer range
chiyotsai2e42a662020-02-26 17:39:03 -08001516 bestsme = exhaustive_mesh_search(
1517 *best_mv, ms_params, mesh_patterns[i].range,
1518 mesh_patterns[i].interval, best_mv, second_best_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001519
Ranjit Kumar Tulabandu5e1f7012019-10-18 19:01:46 +05301520 if (mesh_patterns[i].interval == 1) break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001521 }
1522 }
1523
chiyotsai2e42a662020-02-26 17:39:03 -08001524 if (bestsme < INT_MAX) {
1525 bestsme = get_mvpred_var_cost(ms_params, best_mv);
1526 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001527
1528 // Return cost list.
1529 if (cost_list) {
chiyotsai85eecf92020-02-25 17:56:59 -08001530 if (USE_SAD_COSTLIST) {
1531 const int costlist_has_sad = 0;
chiyotsai2e42a662020-02-26 17:39:03 -08001532 calc_int_sad_list(*best_mv, ms_params, cost_list, costlist_has_sad);
chiyotsai85eecf92020-02-25 17:56:59 -08001533 } else {
chiyotsai2e42a662020-02-26 17:39:03 -08001534 calc_int_cost_list(*best_mv, ms_params, cost_list);
chiyotsai85eecf92020-02-25 17:56:59 -08001535 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001536 }
1537 return bestsme;
1538}
1539
Yaowu Xuc27fc142016-08-22 16:08:15 -07001540// This function is called when we do joint motion search in comp_inter_inter
David Barkerf19f35f2017-05-22 16:33:22 +01001541// mode, or when searching for one component of an ext-inter compound mode.
chiyotsai30a5bdf2020-04-06 14:28:59 -07001542int av1_refining_search_8p_c(const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1543 const FULLPEL_MV start_mv, FULLPEL_MV *best_mv) {
Deepa K Gd4febfb2018-08-14 11:33:51 +05301544 static const search_neighbors neighbors[8] = {
1545 { { -1, 0 }, -1 * SEARCH_GRID_STRIDE_8P + 0 },
1546 { { 0, -1 }, 0 * SEARCH_GRID_STRIDE_8P - 1 },
1547 { { 0, 1 }, 0 * SEARCH_GRID_STRIDE_8P + 1 },
1548 { { 1, 0 }, 1 * SEARCH_GRID_STRIDE_8P + 0 },
1549 { { -1, -1 }, -1 * SEARCH_GRID_STRIDE_8P - 1 },
1550 { { 1, -1 }, 1 * SEARCH_GRID_STRIDE_8P - 1 },
1551 { { -1, 1 }, -1 * SEARCH_GRID_STRIDE_8P + 1 },
1552 { { 1, 1 }, 1 * SEARCH_GRID_STRIDE_8P + 1 }
1553 };
chiyotsai30a5bdf2020-04-06 14:28:59 -07001554
Hien Ho830b8972019-04-04 15:51:14 -07001555 uint8_t do_refine_search_grid[SEARCH_GRID_STRIDE_8P *
1556 SEARCH_GRID_STRIDE_8P] = { 0 };
Deepa K Gd4febfb2018-08-14 11:33:51 +05301557 int grid_center = SEARCH_GRID_CENTER_8P;
1558 int grid_coord = grid_center;
Yunqing Wang37f86a32017-04-20 15:53:00 -07001559
chiyotsai30a5bdf2020-04-06 14:28:59 -07001560 const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
1561 const FullMvLimits *mv_limits = &ms_params->mv_limits;
1562 const MSBuffers *ms_buffers = &ms_params->ms_buffers;
1563 const struct buf_2d *src = ms_buffers->src;
1564 const struct buf_2d *ref = ms_buffers->ref;
1565 const int ref_stride = ref->stride;
1566
1567 *best_mv = start_mv;
1568 clamp_fullmv(best_mv, mv_limits);
1569
1570 unsigned int best_sad = get_mvpred_compound_sad(
1571 ms_params, src, get_buf_from_fullmv(ref, best_mv), ref_stride);
1572 best_sad += mvsad_err_cost_(best_mv, mv_cost_params);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001573
Deepa K Gd4febfb2018-08-14 11:33:51 +05301574 do_refine_search_grid[grid_coord] = 1;
1575
chiyotsai30a5bdf2020-04-06 14:28:59 -07001576 for (int i = 0; i < SEARCH_RANGE_8P; ++i) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001577 int best_site = -1;
1578
chiyotsai30a5bdf2020-04-06 14:28:59 -07001579 for (int j = 0; j < 8; ++j) {
Deepa K Gd4febfb2018-08-14 11:33:51 +05301580 grid_coord = grid_center + neighbors[j].coord_offset;
1581 if (do_refine_search_grid[grid_coord] == 1) {
1582 continue;
1583 }
chiyotsaie46cff72020-02-05 15:03:34 -08001584 const FULLPEL_MV mv = { best_mv->row + neighbors[j].coord.row,
1585 best_mv->col + neighbors[j].coord.col };
Yaowu Xuc27fc142016-08-22 16:08:15 -07001586
Deepa K Gd4febfb2018-08-14 11:33:51 +05301587 do_refine_search_grid[grid_coord] = 1;
chiyotsai30a5bdf2020-04-06 14:28:59 -07001588 if (av1_is_fullmv_in_range(mv_limits, mv)) {
David Barkerc155e012017-05-11 13:54:54 +01001589 unsigned int sad;
chiyotsai30a5bdf2020-04-06 14:28:59 -07001590 sad = get_mvpred_compound_sad(
1591 ms_params, src, get_buf_from_fullmv(ref, &mv), ref_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001592 if (sad < best_sad) {
chiyotsai30a5bdf2020-04-06 14:28:59 -07001593 sad += mvsad_err_cost_(&mv, mv_cost_params);
chiyotsai2e42a662020-02-26 17:39:03 -08001594
Yaowu Xuc27fc142016-08-22 16:08:15 -07001595 if (sad < best_sad) {
1596 best_sad = sad;
1597 best_site = j;
1598 }
1599 }
1600 }
1601 }
1602
1603 if (best_site == -1) {
1604 break;
1605 } else {
Deepa K Gd4febfb2018-08-14 11:33:51 +05301606 best_mv->row += neighbors[best_site].coord.row;
1607 best_mv->col += neighbors[best_site].coord.col;
1608 grid_center += neighbors[best_site].coord_offset;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001609 }
1610 }
1611 return best_sad;
1612}
1613
chiyotsai2e42a662020-02-26 17:39:03 -08001614int av1_full_pixel_search(const FULLPEL_MV start_mv,
1615 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1616 const int step_param, int *cost_list,
chiyotsai8bbdd412020-03-03 14:57:23 -08001617 FULLPEL_MV *best_mv, FULLPEL_MV *second_best_mv) {
chiyotsai2e42a662020-02-26 17:39:03 -08001618 const BLOCK_SIZE bsize = ms_params->bsize;
chiyotsai2e42a662020-02-26 17:39:03 -08001619 const SEARCH_METHODS search_method = ms_params->search_method;
1620
1621 const int is_intra_mode = ms_params->is_intra_mode;
1622 int run_mesh_search = ms_params->run_mesh_search;
1623
1624 int var = 0;
chiyotsaie4d0d852020-04-07 15:21:09 -07001625 MARK_MV_INVALID(best_mv);
1626 if (second_best_mv) {
1627 MARK_MV_INVALID(second_best_mv);
1628 }
chiyotsai2e42a662020-02-26 17:39:03 -08001629
Debargha Mukherjee57fc1302020-10-31 03:42:09 +00001630 assert(ms_params->ms_buffers.second_pred == NULL &&
1631 ms_params->ms_buffers.mask == NULL &&
1632 "av1_full_pixel_search does not support compound pred");
1633
Yaowu Xuc27fc142016-08-22 16:08:15 -07001634 if (cost_list) {
1635 cost_list[0] = INT_MAX;
1636 cost_list[1] = INT_MAX;
1637 cost_list[2] = INT_MAX;
1638 cost_list[3] = INT_MAX;
1639 cost_list[4] = INT_MAX;
1640 }
1641
chiyotsai2e42a662020-02-26 17:39:03 -08001642 switch (search_method) {
venkat sanampudiaeada122020-07-17 13:14:42 +05301643 case FAST_BIGDIA:
1644 var = fast_bigdia_search(start_mv, ms_params, step_param, 0, cost_list,
1645 best_mv);
1646 break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001647 case FAST_DIAMOND:
chiyotsai2e42a662020-02-26 17:39:03 -08001648 var = fast_dia_search(start_mv, ms_params, step_param, 0, cost_list,
1649 best_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001650 break;
1651 case FAST_HEX:
chiyotsai2e42a662020-02-26 17:39:03 -08001652 var = fast_hex_search(start_mv, ms_params, step_param, 0, cost_list,
1653 best_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001654 break;
1655 case HEX:
chiyotsai2e42a662020-02-26 17:39:03 -08001656 var = hex_search(start_mv, ms_params, step_param, 1, cost_list, best_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001657 break;
1658 case SQUARE:
chiyotsai2e42a662020-02-26 17:39:03 -08001659 var =
1660 square_search(start_mv, ms_params, step_param, 1, cost_list, best_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001661 break;
1662 case BIGDIA:
chiyotsai2e42a662020-02-26 17:39:03 -08001663 var =
1664 bigdia_search(start_mv, ms_params, step_param, 1, cost_list, best_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001665 break;
1666 case NSTEP:
Jingning Han183b2a82019-12-18 16:03:41 -08001667 case DIAMOND:
chiyotsai2e42a662020-02-26 17:39:03 -08001668 var = full_pixel_diamond(start_mv, ms_params, step_param, cost_list,
1669 best_mv, second_best_mv);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001670 break;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001671 default: assert(0 && "Invalid search method.");
1672 }
1673
Hui Su410ca3b2018-08-07 11:57:57 -07001674 // Should we allow a follow on exhaustive search?
chiyotsai2e42a662020-02-26 17:39:03 -08001675 if (!run_mesh_search && search_method == NSTEP) {
Cheng Chencf16b022020-04-15 11:19:23 -07001676 int exhaustive_thr = ms_params->force_mesh_thresh;
1677 exhaustive_thr >>=
Hui Su31e26172019-10-10 09:59:25 -07001678 10 - (mi_size_wide_log2[bsize] + mi_size_high_log2[bsize]);
1679 // Threshold variance for an exhaustive full search.
Cheng Chencf16b022020-04-15 11:19:23 -07001680 if (var > exhaustive_thr) run_mesh_search = 1;
Hui Su410ca3b2018-08-07 11:57:57 -07001681 }
1682
Yunqing Wangefc5ecf2020-02-07 18:04:53 -08001683 // TODO(yunqing): the following is used to reduce mesh search in temporal
1684 // filtering. Can extend it to intrabc.
chiyotsai2e42a662020-02-26 17:39:03 -08001685 if (!is_intra_mode && ms_params->prune_mesh_search) {
chiyotsai8bbdd412020-03-03 14:57:23 -08001686 const int full_pel_mv_diff = AOMMAX(abs(start_mv.row - best_mv->row),
1687 abs(start_mv.col - best_mv->col));
Yunqing Wangefc5ecf2020-02-07 18:04:53 -08001688 if (full_pel_mv_diff <= 4) {
1689 run_mesh_search = 0;
1690 }
1691 }
1692
chiyotsaic814afb2020-08-04 13:12:35 -07001693 if (ms_params->sdf != ms_params->vfp->sdf) {
1694 // If we are skipping rows when we perform the motion search, we need to
1695 // check the quality of skipping. If it's bad, then we run mesh search with
1696 // skip row features off.
1697 // TODO(chiyotsai@google.com): Handle the case where we have a vertical
1698 // offset of 1 before we hit this statement to avoid having to redo
1699 // motion search.
1700 const struct buf_2d *src = ms_params->ms_buffers.src;
1701 const struct buf_2d *ref = ms_params->ms_buffers.ref;
1702 const int src_stride = src->stride;
1703 const int ref_stride = ref->stride;
1704
1705 const uint8_t *src_address = src->buf;
1706 const uint8_t *best_address = get_buf_from_fullmv(ref, best_mv);
1707 const int sad =
1708 ms_params->vfp->sdf(src_address, src_stride, best_address, ref_stride);
1709 const int skip_sad =
1710 ms_params->vfp->sdsf(src_address, src_stride, best_address, ref_stride);
1711 // We will keep the result of skipping rows if it's good enough. Here, good
1712 // enough means the error is less than 1 per pixel.
1713 const int kSADThresh =
1714 1 << (mi_size_wide_log2[bsize] + mi_size_high_log2[bsize]);
1715 if (sad > kSADThresh && abs(skip_sad - sad) * 10 >= AOMMAX(sad, 1) * 9) {
1716 // There is a large discrepancy between skipping and not skipping, so we
1717 // need to redo the motion search.
1718 FULLPEL_MOTION_SEARCH_PARAMS new_ms_params = *ms_params;
1719 new_ms_params.sdf = new_ms_params.vfp->sdf;
1720 new_ms_params.sdx4df = new_ms_params.vfp->sdx4df;
1721
1722 return av1_full_pixel_search(start_mv, &new_ms_params, step_param,
1723 cost_list, best_mv, second_best_mv);
1724 }
1725 }
1726
Hui Su410ca3b2018-08-07 11:57:57 -07001727 if (run_mesh_search) {
1728 int var_ex;
chiyotsaie46cff72020-02-05 15:03:34 -08001729 FULLPEL_MV tmp_mv_ex;
Ranjit Kumar Tulabandu5e1f7012019-10-18 19:01:46 +05301730 // Pick the mesh pattern for exhaustive search based on the toolset (intraBC
1731 // or non-intraBC)
chiyotsai8bbdd412020-03-03 14:57:23 -08001732 // TODO(chiyotsai@google.com): There is a bug here where the second best mv
1733 // gets overwritten without actually comparing the rdcost.
chiyotsai2e42a662020-02-26 17:39:03 -08001734 const MESH_PATTERN *const mesh_patterns =
1735 ms_params->mesh_patterns[is_intra_mode];
1736 // TODO(chiyotsai@google.com): the second best mv is not set correctly by
1737 // full_pixel_exhaustive, which can incorrectly override it.
1738 var_ex = full_pixel_exhaustive(*best_mv, ms_params, mesh_patterns,
1739 cost_list, &tmp_mv_ex, second_best_mv);
1740 if (var_ex < var) {
1741 var = var_ex;
chiyotsai8bbdd412020-03-03 14:57:23 -08001742 *best_mv = tmp_mv_ex;
Hui Su410ca3b2018-08-07 11:57:57 -07001743 }
1744 }
1745
chiyotsai2e42a662020-02-26 17:39:03 -08001746 return var;
chiyotsai8bbdd412020-03-03 14:57:23 -08001747}
Hui Su2d5fd742018-02-21 18:10:37 -08001748
chiyotsai82f36c92020-04-09 16:18:02 -07001749int av1_intrabc_hash_search(const AV1_COMP *cpi, const MACROBLOCKD *xd,
1750 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
1751 IntraBCHashInfo *intrabc_hash_info,
1752 FULLPEL_MV *best_mv) {
1753 if (!av1_use_hash_me(cpi)) return INT_MAX;
RogerZhoucc5d35d2017-08-07 22:20:15 -07001754
chiyotsai82f36c92020-04-09 16:18:02 -07001755 const BLOCK_SIZE bsize = ms_params->bsize;
chiyotsai8bbdd412020-03-03 14:57:23 -08001756 const int block_width = block_size_wide[bsize];
chiyotsai82f36c92020-04-09 16:18:02 -07001757 const int block_height = block_size_high[bsize];
1758
1759 if (block_width != block_height) return INT_MAX;
1760
1761 const FullMvLimits *mv_limits = &ms_params->mv_limits;
1762 const MSBuffers *ms_buffer = &ms_params->ms_buffers;
1763
1764 const uint8_t *src = ms_buffer->src->buf;
1765 const int src_stride = ms_buffer->src->stride;
1766
1767 const int mi_row = xd->mi_row;
1768 const int mi_col = xd->mi_col;
chiyotsai8bbdd412020-03-03 14:57:23 -08001769 const int x_pos = mi_col * MI_SIZE;
1770 const int y_pos = mi_row * MI_SIZE;
RogerZhoucc5d35d2017-08-07 22:20:15 -07001771
chiyotsai82f36c92020-04-09 16:18:02 -07001772 uint32_t hash_value1, hash_value2;
1773 int best_hash_cost = INT_MAX;
RogerZhoucc5d35d2017-08-07 22:20:15 -07001774
chiyotsai82f36c92020-04-09 16:18:02 -07001775 // for the hashMap
1776 hash_table *ref_frame_hash = &intrabc_hash_info->intrabc_hash_table;
RogerZhoucc5d35d2017-08-07 22:20:15 -07001777
chiyotsai82f36c92020-04-09 16:18:02 -07001778 av1_get_block_hash_value(intrabc_hash_info, src, src_stride, block_width,
1779 &hash_value1, &hash_value2, is_cur_buf_hbd(xd));
chiyotsai8bbdd412020-03-03 14:57:23 -08001780
chiyotsai82f36c92020-04-09 16:18:02 -07001781 const int count = av1_hash_table_count(ref_frame_hash, hash_value1);
1782 if (count <= 1) {
1783 return INT_MAX;
1784 }
chiyotsai8bbdd412020-03-03 14:57:23 -08001785
chiyotsai82f36c92020-04-09 16:18:02 -07001786 Iterator iterator = av1_hash_get_first_iterator(ref_frame_hash, hash_value1);
1787 for (int i = 0; i < count; i++, aom_iterator_increment(&iterator)) {
1788 block_hash ref_block_hash = *(block_hash *)(aom_iterator_get(&iterator));
1789 if (hash_value2 == ref_block_hash.hash_value2) {
1790 // Make sure the prediction is from valid area.
1791 const MV dv = { GET_MV_SUBPEL(ref_block_hash.y - y_pos),
1792 GET_MV_SUBPEL(ref_block_hash.x - x_pos) };
1793 if (!av1_is_dv_valid(dv, &cpi->common, xd, mi_row, mi_col, bsize,
1794 cpi->common.seq_params.mib_size_log2))
1795 continue;
chiyotsai8bbdd412020-03-03 14:57:23 -08001796
chiyotsai82f36c92020-04-09 16:18:02 -07001797 FULLPEL_MV hash_mv;
1798 hash_mv.col = ref_block_hash.x - x_pos;
1799 hash_mv.row = ref_block_hash.y - y_pos;
1800 if (!av1_is_fullmv_in_range(mv_limits, hash_mv)) continue;
1801 const int refCost = get_mvpred_var_cost(ms_params, &hash_mv);
1802 if (refCost < best_hash_cost) {
1803 best_hash_cost = refCost;
1804 *best_mv = hash_mv;
RogerZhoucc5d35d2017-08-07 22:20:15 -07001805 }
1806 }
chiyotsai8bbdd412020-03-03 14:57:23 -08001807 }
chiyotsai82f36c92020-04-09 16:18:02 -07001808
1809 return best_hash_cost;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001810}
1811
chiyotsai9f664e32020-02-19 14:17:31 -08001812static int vector_match(int16_t *ref, int16_t *src, int bwl) {
1813 int best_sad = INT_MAX;
1814 int this_sad;
1815 int d;
1816 int center, offset = 0;
1817 int bw = 4 << bwl; // redundant variable, to be changed in the experiments.
1818 for (d = 0; d <= bw; d += 16) {
1819 this_sad = aom_vector_var(&ref[d], src, bwl);
1820 if (this_sad < best_sad) {
1821 best_sad = this_sad;
1822 offset = d;
1823 }
1824 }
1825 center = offset;
1826
1827 for (d = -8; d <= 8; d += 16) {
1828 int this_pos = offset + d;
1829 // check limit
1830 if (this_pos < 0 || this_pos > bw) continue;
1831 this_sad = aom_vector_var(&ref[this_pos], src, bwl);
1832 if (this_sad < best_sad) {
1833 best_sad = this_sad;
1834 center = this_pos;
1835 }
1836 }
1837 offset = center;
1838
1839 for (d = -4; d <= 4; d += 8) {
1840 int this_pos = offset + d;
1841 // check limit
1842 if (this_pos < 0 || this_pos > bw) continue;
1843 this_sad = aom_vector_var(&ref[this_pos], src, bwl);
1844 if (this_sad < best_sad) {
1845 best_sad = this_sad;
1846 center = this_pos;
1847 }
1848 }
1849 offset = center;
1850
1851 for (d = -2; d <= 2; d += 4) {
1852 int this_pos = offset + d;
1853 // check limit
1854 if (this_pos < 0 || this_pos > bw) continue;
1855 this_sad = aom_vector_var(&ref[this_pos], src, bwl);
1856 if (this_sad < best_sad) {
1857 best_sad = this_sad;
1858 center = this_pos;
1859 }
1860 }
1861 offset = center;
1862
1863 for (d = -1; d <= 1; d += 2) {
1864 int this_pos = offset + d;
1865 // check limit
1866 if (this_pos < 0 || this_pos > bw) continue;
1867 this_sad = aom_vector_var(&ref[this_pos], src, bwl);
1868 if (this_sad < best_sad) {
1869 best_sad = this_sad;
1870 center = this_pos;
1871 }
1872 }
1873
1874 return (center - (bw >> 1));
1875}
1876
1877// A special fast version of motion search used in rt mode
1878unsigned int av1_int_pro_motion_estimation(const AV1_COMP *cpi, MACROBLOCK *x,
1879 BLOCK_SIZE bsize, int mi_row,
1880 int mi_col, const MV *ref_mv) {
1881 MACROBLOCKD *xd = &x->e_mbd;
1882 MB_MODE_INFO *mi = xd->mi[0];
1883 struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } };
1884 DECLARE_ALIGNED(16, int16_t, hbuf[256]);
1885 DECLARE_ALIGNED(16, int16_t, vbuf[256]);
1886 DECLARE_ALIGNED(16, int16_t, src_hbuf[128]);
1887 DECLARE_ALIGNED(16, int16_t, src_vbuf[128]);
1888 int idx;
1889 const int bw = 4 << mi_size_wide_log2[bsize];
1890 const int bh = 4 << mi_size_high_log2[bsize];
1891 const int search_width = bw << 1;
1892 const int search_height = bh << 1;
1893 const int src_stride = x->plane[0].src.stride;
1894 const int ref_stride = xd->plane[0].pre[0].stride;
1895 uint8_t const *ref_buf, *src_buf;
1896 int_mv *best_int_mv = &xd->mi[0]->mv[0];
1897 unsigned int best_sad, tmp_sad, this_sad[4];
1898 const int norm_factor = 3 + (bw >> 5);
1899 const YV12_BUFFER_CONFIG *scaled_ref_frame =
1900 av1_get_scaled_ref_frame(cpi, mi->ref_frame[0]);
1901 static const MV search_pos[4] = {
1902 { -1, 0 },
1903 { 0, -1 },
1904 { 0, 1 },
1905 { 1, 0 },
1906 };
1907
1908 if (scaled_ref_frame) {
1909 int i;
1910 // Swap out the reference frame for a version that's been scaled to
1911 // match the resolution of the current frame, allowing the existing
1912 // motion search code to be used without additional modifications.
1913 for (i = 0; i < MAX_MB_PLANE; i++) backup_yv12[i] = xd->plane[i].pre[0];
1914 av1_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL,
1915 MAX_MB_PLANE);
1916 }
1917
1918 if (xd->bd != 8) {
1919 unsigned int sad;
1920 best_int_mv->as_fullmv = kZeroFullMv;
1921 sad = cpi->fn_ptr[bsize].sdf(x->plane[0].src.buf, src_stride,
1922 xd->plane[0].pre[0].buf, ref_stride);
1923
1924 if (scaled_ref_frame) {
1925 int i;
1926 for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i];
1927 }
1928 return sad;
1929 }
1930
1931 // Set up prediction 1-D reference set
1932 ref_buf = xd->plane[0].pre[0].buf - (bw >> 1);
1933 for (idx = 0; idx < search_width; idx += 16) {
1934 aom_int_pro_row(&hbuf[idx], ref_buf, ref_stride, bh);
1935 ref_buf += 16;
1936 }
1937
1938 ref_buf = xd->plane[0].pre[0].buf - (bh >> 1) * ref_stride;
1939 for (idx = 0; idx < search_height; ++idx) {
1940 vbuf[idx] = aom_int_pro_col(ref_buf, bw) >> norm_factor;
1941 ref_buf += ref_stride;
1942 }
1943
1944 // Set up src 1-D reference set
1945 for (idx = 0; idx < bw; idx += 16) {
1946 src_buf = x->plane[0].src.buf + idx;
1947 aom_int_pro_row(&src_hbuf[idx], src_buf, src_stride, bh);
1948 }
1949
1950 src_buf = x->plane[0].src.buf;
1951 for (idx = 0; idx < bh; ++idx) {
1952 src_vbuf[idx] = aom_int_pro_col(src_buf, bw) >> norm_factor;
1953 src_buf += src_stride;
1954 }
1955
1956 // Find the best match per 1-D search
1957 best_int_mv->as_fullmv.col =
1958 vector_match(hbuf, src_hbuf, mi_size_wide_log2[bsize]);
1959 best_int_mv->as_fullmv.row =
1960 vector_match(vbuf, src_vbuf, mi_size_high_log2[bsize]);
1961
1962 FULLPEL_MV this_mv = best_int_mv->as_fullmv;
1963 src_buf = x->plane[0].src.buf;
chiyotsai5aa70752020-03-26 10:13:10 -07001964 ref_buf = get_buf_from_fullmv(&xd->plane[0].pre[0], &this_mv);
chiyotsai9f664e32020-02-19 14:17:31 -08001965 best_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride, ref_buf, ref_stride);
1966
1967 {
1968 const uint8_t *const pos[4] = {
1969 ref_buf - ref_stride,
1970 ref_buf - 1,
1971 ref_buf + 1,
1972 ref_buf + ref_stride,
1973 };
1974
1975 cpi->fn_ptr[bsize].sdx4df(src_buf, src_stride, pos, ref_stride, this_sad);
1976 }
1977
1978 for (idx = 0; idx < 4; ++idx) {
1979 if (this_sad[idx] < best_sad) {
1980 best_sad = this_sad[idx];
1981 best_int_mv->as_fullmv.row = search_pos[idx].row + this_mv.row;
1982 best_int_mv->as_fullmv.col = search_pos[idx].col + this_mv.col;
1983 }
1984 }
1985
1986 if (this_sad[0] < this_sad[3])
1987 this_mv.row -= 1;
1988 else
1989 this_mv.row += 1;
1990
1991 if (this_sad[1] < this_sad[2])
1992 this_mv.col -= 1;
1993 else
1994 this_mv.col += 1;
1995
chiyotsai5aa70752020-03-26 10:13:10 -07001996 ref_buf = get_buf_from_fullmv(&xd->plane[0].pre[0], &this_mv);
chiyotsai9f664e32020-02-19 14:17:31 -08001997
1998 tmp_sad = cpi->fn_ptr[bsize].sdf(src_buf, src_stride, ref_buf, ref_stride);
1999 if (best_sad > tmp_sad) {
2000 best_int_mv->as_fullmv = this_mv;
2001 best_sad = tmp_sad;
2002 }
2003
2004 convert_fullmv_to_mv(best_int_mv);
2005
2006 SubpelMvLimits subpel_mv_limits;
2007 av1_set_subpel_mv_search_range(&subpel_mv_limits, &x->mv_limits, ref_mv);
2008 clamp_mv(&best_int_mv->as_mv, &subpel_mv_limits);
2009
2010 if (scaled_ref_frame) {
2011 int i;
2012 for (i = 0; i < MAX_MB_PLANE; i++) xd->plane[i].pre[0] = backup_yv12[i];
2013 }
2014
2015 return best_sad;
2016}
2017
2018// =============================================================================
chiyotsai2e42a662020-02-26 17:39:03 -08002019// Fullpixel Motion Search: OBMC
2020// =============================================================================
chiyotsai94f4aca2020-03-20 12:54:47 -07002021static INLINE int get_obmc_mvpred_var(
2022 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV *this_mv) {
2023 const aom_variance_fn_ptr_t *vfp = ms_params->vfp;
2024 const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
2025 const MSBuffers *ms_buffers = &ms_params->ms_buffers;
2026 const int32_t *wsrc = ms_buffers->wsrc;
2027 const int32_t *mask = ms_buffers->obmc_mask;
2028 const struct buf_2d *ref_buf = ms_buffers->ref;
2029
2030 const MV mv = get_mv_from_fullmv(this_mv);
chiyotsai2e42a662020-02-26 17:39:03 -08002031 unsigned int unused;
2032
chiyotsai5aa70752020-03-26 10:13:10 -07002033 return vfp->ovf(get_buf_from_fullmv(ref_buf, this_mv), ref_buf->stride, wsrc,
chiyotsai2e42a662020-02-26 17:39:03 -08002034 mask, &unused) +
chiyotsai94f4aca2020-03-20 12:54:47 -07002035 mv_err_cost_(&mv, mv_cost_params);
chiyotsai2e42a662020-02-26 17:39:03 -08002036}
2037
chiyotsai94f4aca2020-03-20 12:54:47 -07002038static int obmc_refining_search_sad(
2039 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, FULLPEL_MV *best_mv) {
2040 const aom_variance_fn_ptr_t *fn_ptr = ms_params->vfp;
2041 const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
2042 const MSBuffers *ms_buffers = &ms_params->ms_buffers;
2043 const int32_t *wsrc = ms_buffers->wsrc;
2044 const int32_t *mask = ms_buffers->obmc_mask;
2045 const struct buf_2d *ref_buf = ms_buffers->ref;
2046 const FULLPEL_MV neighbors[4] = { { -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, 0 } };
2047 const int kSearchRange = 8;
chiyotsai2e42a662020-02-26 17:39:03 -08002048
chiyotsai5aa70752020-03-26 10:13:10 -07002049 unsigned int best_sad = fn_ptr->osdf(get_buf_from_fullmv(ref_buf, best_mv),
chiyotsai94f4aca2020-03-20 12:54:47 -07002050 ref_buf->stride, wsrc, mask) +
2051 mvsad_err_cost_(best_mv, mv_cost_params);
chiyotsai2e42a662020-02-26 17:39:03 -08002052
chiyotsai94f4aca2020-03-20 12:54:47 -07002053 for (int i = 0; i < kSearchRange; i++) {
chiyotsai2e42a662020-02-26 17:39:03 -08002054 int best_site = -1;
2055
chiyotsai94f4aca2020-03-20 12:54:47 -07002056 for (int j = 0; j < 4; j++) {
chiyotsai2e42a662020-02-26 17:39:03 -08002057 const FULLPEL_MV mv = { best_mv->row + neighbors[j].row,
2058 best_mv->col + neighbors[j].col };
chiyotsai94f4aca2020-03-20 12:54:47 -07002059 if (av1_is_fullmv_in_range(&ms_params->mv_limits, mv)) {
chiyotsai5aa70752020-03-26 10:13:10 -07002060 unsigned int sad = fn_ptr->osdf(get_buf_from_fullmv(ref_buf, &mv),
chiyotsai94f4aca2020-03-20 12:54:47 -07002061 ref_buf->stride, wsrc, mask);
chiyotsai2e42a662020-02-26 17:39:03 -08002062 if (sad < best_sad) {
chiyotsai94f4aca2020-03-20 12:54:47 -07002063 sad += mvsad_err_cost_(&mv, mv_cost_params);
chiyotsai2e42a662020-02-26 17:39:03 -08002064
2065 if (sad < best_sad) {
2066 best_sad = sad;
2067 best_site = j;
2068 }
2069 }
2070 }
2071 }
2072
2073 if (best_site == -1) {
2074 break;
2075 } else {
2076 best_mv->row += neighbors[best_site].row;
2077 best_mv->col += neighbors[best_site].col;
2078 }
2079 }
2080 return best_sad;
2081}
2082
2083static int obmc_diamond_search_sad(
chiyotsai94f4aca2020-03-20 12:54:47 -07002084 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, FULLPEL_MV start_mv,
Cheng Chenbb983ba2020-04-21 12:17:53 -07002085 FULLPEL_MV *best_mv, int search_step, int *num00) {
chiyotsai94f4aca2020-03-20 12:54:47 -07002086 const aom_variance_fn_ptr_t *fn_ptr = ms_params->vfp;
2087 const search_site_config *cfg = ms_params->search_sites;
2088 const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
2089 const MSBuffers *ms_buffers = &ms_params->ms_buffers;
2090 const int32_t *wsrc = ms_buffers->wsrc;
2091 const int32_t *mask = ms_buffers->obmc_mask;
2092 const struct buf_2d *const ref_buf = ms_buffers->ref;
Cheng Chenbb983ba2020-04-21 12:17:53 -07002093 // search_step determines the length of the initial step and hence the number
chiyotsai2e42a662020-02-26 17:39:03 -08002094 // of iterations
2095 // 0 = initial step (MAX_FIRST_STEP) pel : 1 = (MAX_FIRST_STEP/2) pel, 2 =
2096 // (MAX_FIRST_STEP/4) pel... etc.
2097
Cheng Chenbb983ba2020-04-21 12:17:53 -07002098 const int tot_steps = MAX_MVSEARCH_STEPS - 1 - search_step;
chiyotsai94f4aca2020-03-20 12:54:47 -07002099 const uint8_t *best_address, *init_ref;
chiyotsai2e42a662020-02-26 17:39:03 -08002100 int best_sad = INT_MAX;
2101 int best_site = 0;
2102 int step;
2103
chiyotsai94f4aca2020-03-20 12:54:47 -07002104 clamp_fullmv(&start_mv, &ms_params->mv_limits);
chiyotsai5aa70752020-03-26 10:13:10 -07002105 best_address = init_ref = get_buf_from_fullmv(ref_buf, &start_mv);
chiyotsai2e42a662020-02-26 17:39:03 -08002106 *num00 = 0;
2107 *best_mv = start_mv;
2108
2109 // Check the starting position
chiyotsai94f4aca2020-03-20 12:54:47 -07002110 best_sad = fn_ptr->osdf(best_address, ref_buf->stride, wsrc, mask) +
2111 mvsad_err_cost_(best_mv, mv_cost_params);
chiyotsai2e42a662020-02-26 17:39:03 -08002112
2113 for (step = tot_steps; step >= 0; --step) {
Cheng Chen66190d92020-04-21 12:58:43 -07002114 const search_site *const site = cfg->site[step];
chiyotsai2e42a662020-02-26 17:39:03 -08002115 best_site = 0;
2116 for (int idx = 1; idx <= cfg->searches_per_step[step]; ++idx) {
Cheng Chen66190d92020-04-21 12:58:43 -07002117 const FULLPEL_MV mv = { best_mv->row + site[idx].mv.row,
2118 best_mv->col + site[idx].mv.col };
chiyotsai94f4aca2020-03-20 12:54:47 -07002119 if (av1_is_fullmv_in_range(&ms_params->mv_limits, mv)) {
Cheng Chen66190d92020-04-21 12:58:43 -07002120 int sad = fn_ptr->osdf(best_address + site[idx].offset, ref_buf->stride,
chiyotsai2e42a662020-02-26 17:39:03 -08002121 wsrc, mask);
2122 if (sad < best_sad) {
chiyotsai94f4aca2020-03-20 12:54:47 -07002123 sad += mvsad_err_cost_(&mv, mv_cost_params);
chiyotsai2e42a662020-02-26 17:39:03 -08002124
2125 if (sad < best_sad) {
2126 best_sad = sad;
2127 best_site = idx;
2128 }
2129 }
2130 }
2131 }
2132
2133 if (best_site != 0) {
Cheng Chen66190d92020-04-21 12:58:43 -07002134 best_mv->row += site[best_site].mv.row;
2135 best_mv->col += site[best_site].mv.col;
2136 best_address += site[best_site].offset;
chiyotsai94f4aca2020-03-20 12:54:47 -07002137 } else if (best_address == init_ref) {
chiyotsai2e42a662020-02-26 17:39:03 -08002138 (*num00)++;
2139 }
2140 }
2141 return best_sad;
2142}
2143
chiyotsai94f4aca2020-03-20 12:54:47 -07002144static int obmc_full_pixel_diamond(
2145 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params, const FULLPEL_MV start_mv,
2146 int step_param, int do_refine, FULLPEL_MV *best_mv) {
2147 const search_site_config *cfg = ms_params->search_sites;
chiyotsai2e42a662020-02-26 17:39:03 -08002148 FULLPEL_MV tmp_mv;
2149 int thissme, n, num00 = 0;
chiyotsai94f4aca2020-03-20 12:54:47 -07002150 int bestsme =
2151 obmc_diamond_search_sad(ms_params, start_mv, &tmp_mv, step_param, &n);
2152 if (bestsme < INT_MAX) bestsme = get_obmc_mvpred_var(ms_params, &tmp_mv);
chiyotsai2e42a662020-02-26 17:39:03 -08002153 *best_mv = tmp_mv;
2154
2155 // If there won't be more n-step search, check to see if refining search is
2156 // needed.
Cheng Chen66190d92020-04-21 12:58:43 -07002157 const int further_steps = cfg->num_search_steps - 1 - step_param;
chiyotsai2e42a662020-02-26 17:39:03 -08002158 if (n > further_steps) do_refine = 0;
2159
2160 while (n < further_steps) {
2161 ++n;
2162
2163 if (num00) {
2164 num00--;
2165 } else {
chiyotsai94f4aca2020-03-20 12:54:47 -07002166 thissme = obmc_diamond_search_sad(ms_params, start_mv, &tmp_mv,
2167 step_param + n, &num00);
2168 if (thissme < INT_MAX) thissme = get_obmc_mvpred_var(ms_params, &tmp_mv);
chiyotsai2e42a662020-02-26 17:39:03 -08002169
2170 // check to see if refining search is needed.
2171 if (num00 > further_steps - n) do_refine = 0;
2172
2173 if (thissme < bestsme) {
2174 bestsme = thissme;
2175 *best_mv = tmp_mv;
2176 }
2177 }
2178 }
2179
2180 // final 1-away diamond refining search
2181 if (do_refine) {
chiyotsai2e42a662020-02-26 17:39:03 -08002182 tmp_mv = *best_mv;
chiyotsai94f4aca2020-03-20 12:54:47 -07002183 thissme = obmc_refining_search_sad(ms_params, &tmp_mv);
2184 if (thissme < INT_MAX) thissme = get_obmc_mvpred_var(ms_params, &tmp_mv);
chiyotsai2e42a662020-02-26 17:39:03 -08002185 if (thissme < bestsme) {
2186 bestsme = thissme;
2187 *best_mv = tmp_mv;
2188 }
2189 }
2190 return bestsme;
2191}
2192
chiyotsai94f4aca2020-03-20 12:54:47 -07002193int av1_obmc_full_pixel_search(const FULLPEL_MV start_mv,
chiyotsai2e42a662020-02-26 17:39:03 -08002194 const FULLPEL_MOTION_SEARCH_PARAMS *ms_params,
2195 const int step_param, FULLPEL_MV *best_mv) {
chiyotsai2e42a662020-02-26 17:39:03 -08002196 if (!ms_params->fast_obmc_search) {
chiyotsai2e42a662020-02-26 17:39:03 -08002197 const int do_refine = 1;
chiyotsai94f4aca2020-03-20 12:54:47 -07002198 const int bestsme = obmc_full_pixel_diamond(ms_params, start_mv, step_param,
2199 do_refine, best_mv);
chiyotsai2e42a662020-02-26 17:39:03 -08002200 return bestsme;
2201 } else {
chiyotsai2e42a662020-02-26 17:39:03 -08002202 *best_mv = start_mv;
chiyotsai94f4aca2020-03-20 12:54:47 -07002203 clamp_fullmv(best_mv, &ms_params->mv_limits);
2204 int thissme = obmc_refining_search_sad(ms_params, best_mv);
2205 if (thissme < INT_MAX) thissme = get_obmc_mvpred_var(ms_params, best_mv);
chiyotsai2e42a662020-02-26 17:39:03 -08002206 return thissme;
2207 }
2208}
2209
2210// =============================================================================
chiyotsai9f664e32020-02-19 14:17:31 -08002211// Subpixel Motion Search: Translational
2212// =============================================================================
2213#define INIT_SUBPEL_STEP_SIZE (4)
2214/*
2215 * To avoid the penalty for crossing cache-line read, preload the reference
2216 * area in a small buffer, which is aligned to make sure there won't be crossing
2217 * cache-line read while reading from this buffer. This reduced the cpu
2218 * cycles spent on reading ref data in sub-pixel filter functions.
2219 * TODO: Currently, since sub-pixel search range here is -3 ~ 3, copy 22 rows x
2220 * 32 cols area that is enough for 16x16 macroblock. Later, for SPLITMV, we
2221 * could reduce the area.
2222 */
2223
2224// Returns the subpel offset used by various subpel variance functions [m]sv[a]f
chiyotsai5aa70752020-03-26 10:13:10 -07002225static INLINE int get_subpel_part(int x) { return x & 7; }
chiyotsai9f664e32020-02-19 14:17:31 -08002226
2227// Gets the address of the ref buffer at subpel location (r, c), rounded to the
2228// nearest fullpel precision toward - \infty
chiyotsai5aa70752020-03-26 10:13:10 -07002229
2230static INLINE const uint8_t *get_buf_from_mv(const struct buf_2d *buf,
2231 const MV mv) {
2232 const int offset = (mv.row >> 3) * buf->stride + (mv.col >> 3);
2233 return &buf->buf[offset];
chiyotsai9f664e32020-02-19 14:17:31 -08002234}
2235
2236// Estimates the variance of prediction residue using bilinear filter for fast
2237// search.
2238static INLINE int estimated_pref_error(
chiyotsai5aa70752020-03-26 10:13:10 -07002239 const MV *this_mv, const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2240 unsigned int *sse) {
chiyotsai9f664e32020-02-19 14:17:31 -08002241 const aom_variance_fn_ptr_t *vfp = var_params->vfp;
chiyotsai5aa70752020-03-26 10:13:10 -07002242
2243 const MSBuffers *ms_buffers = &var_params->ms_buffers;
2244 const uint8_t *src = ms_buffers->src->buf;
2245 const uint8_t *ref = get_buf_from_mv(ms_buffers->ref, *this_mv);
2246 const int src_stride = ms_buffers->src->stride;
2247 const int ref_stride = ms_buffers->ref->stride;
2248 const uint8_t *second_pred = ms_buffers->second_pred;
2249 const uint8_t *mask = ms_buffers->mask;
2250 const int mask_stride = ms_buffers->mask_stride;
2251 const int invert_mask = ms_buffers->inv_mask;
2252
2253 const int subpel_x_q3 = get_subpel_part(this_mv->col);
2254 const int subpel_y_q3 = get_subpel_part(this_mv->row);
chiyotsai9f664e32020-02-19 14:17:31 -08002255
2256 if (second_pred == NULL) {
chiyotsai5aa70752020-03-26 10:13:10 -07002257 return vfp->svf(ref, ref_stride, subpel_x_q3, subpel_y_q3, src, src_stride,
2258 sse);
chiyotsai9f664e32020-02-19 14:17:31 -08002259 } else if (mask) {
chiyotsai5aa70752020-03-26 10:13:10 -07002260 return vfp->msvf(ref, ref_stride, subpel_x_q3, subpel_y_q3, src, src_stride,
2261 second_pred, mask, mask_stride, invert_mask, sse);
chiyotsai9f664e32020-02-19 14:17:31 -08002262 } else {
chiyotsai5aa70752020-03-26 10:13:10 -07002263 return vfp->svaf(ref, ref_stride, subpel_x_q3, subpel_y_q3, src, src_stride,
2264 sse, second_pred);
chiyotsai9f664e32020-02-19 14:17:31 -08002265 }
2266}
2267
2268// Calculates the variance of prediction residue.
2269static int upsampled_pref_error(MACROBLOCKD *xd, const AV1_COMMON *cm,
chiyotsai5aa70752020-03-26 10:13:10 -07002270 const MV *this_mv,
chiyotsai9f664e32020-02-19 14:17:31 -08002271 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2272 unsigned int *sse) {
2273 const aom_variance_fn_ptr_t *vfp = var_params->vfp;
2274 const SUBPEL_SEARCH_TYPE subpel_search_type = var_params->subpel_search_type;
chiyotsai5aa70752020-03-26 10:13:10 -07002275
2276 const MSBuffers *ms_buffers = &var_params->ms_buffers;
2277 const uint8_t *src = ms_buffers->src->buf;
2278 const uint8_t *ref = get_buf_from_mv(ms_buffers->ref, *this_mv);
2279 const int src_stride = ms_buffers->src->stride;
2280 const int ref_stride = ms_buffers->ref->stride;
2281 const uint8_t *second_pred = ms_buffers->second_pred;
2282 const uint8_t *mask = ms_buffers->mask;
2283 const int mask_stride = ms_buffers->mask_stride;
2284 const int invert_mask = ms_buffers->inv_mask;
chiyotsai9f664e32020-02-19 14:17:31 -08002285 const int w = var_params->w;
2286 const int h = var_params->h;
2287
2288 const int mi_row = xd->mi_row;
2289 const int mi_col = xd->mi_col;
chiyotsai5aa70752020-03-26 10:13:10 -07002290 const int subpel_x_q3 = get_subpel_part(this_mv->col);
2291 const int subpel_y_q3 = get_subpel_part(this_mv->row);
chiyotsaia7e2cf82020-02-19 16:13:29 -08002292
2293 unsigned int besterr;
Ryan Lei2a676372020-09-28 16:58:52 -07002294
chiyotsai9f664e32020-02-19 14:17:31 -08002295 if (is_cur_buf_hbd(xd)) {
2296 DECLARE_ALIGNED(16, uint16_t, pred16[MAX_SB_SQUARE]);
2297 uint8_t *pred8 = CONVERT_TO_BYTEPTR(pred16);
2298 if (second_pred != NULL) {
2299 if (mask) {
2300 aom_highbd_comp_mask_upsampled_pred(
2301 xd, cm, mi_row, mi_col, this_mv, pred8, second_pred, w, h,
2302 subpel_x_q3, subpel_y_q3, ref, ref_stride, mask, mask_stride,
2303 invert_mask, xd->bd, subpel_search_type);
2304 } else {
2305 aom_highbd_comp_avg_upsampled_pred(
2306 xd, cm, mi_row, mi_col, this_mv, pred8, second_pred, w, h,
2307 subpel_x_q3, subpel_y_q3, ref, ref_stride, xd->bd,
2308 subpel_search_type);
2309 }
2310 } else {
2311 aom_highbd_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred8, w, h,
2312 subpel_x_q3, subpel_y_q3, ref, ref_stride,
2313 xd->bd, subpel_search_type);
2314 }
2315 besterr = vfp->vf(pred8, w, src, src_stride, sse);
2316 } else {
2317 DECLARE_ALIGNED(16, uint8_t, pred[MAX_SB_SQUARE]);
2318 if (second_pred != NULL) {
2319 if (mask) {
2320 aom_comp_mask_upsampled_pred(
2321 xd, cm, mi_row, mi_col, this_mv, pred, second_pred, w, h,
2322 subpel_x_q3, subpel_y_q3, ref, ref_stride, mask, mask_stride,
2323 invert_mask, subpel_search_type);
2324 } else {
2325 aom_comp_avg_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred,
2326 second_pred, w, h, subpel_x_q3, subpel_y_q3,
2327 ref, ref_stride, subpel_search_type);
2328 }
2329 } else {
2330 aom_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred, w, h,
2331 subpel_x_q3, subpel_y_q3, ref, ref_stride,
2332 subpel_search_type);
2333 }
2334
2335 besterr = vfp->vf(pred, w, src, src_stride, sse);
2336 }
chiyotsai9f664e32020-02-19 14:17:31 -08002337
chiyotsai9f664e32020-02-19 14:17:31 -08002338 return besterr;
2339}
2340
2341// Estimates whether this_mv is better than best_mv. This function incorporates
2342// both prediction error and residue into account. It is suffixed "fast" because
2343// it uses bilinear filter to estimate the prediction.
2344static INLINE unsigned int check_better_fast(
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302345 MACROBLOCKD *xd, const AV1_COMMON *cm, const MV *this_mv, MV *best_mv,
2346 const SubpelMvLimits *mv_limits, const SUBPEL_SEARCH_VAR_PARAMS *var_params,
chiyotsai9f664e32020-02-19 14:17:31 -08002347 const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302348 unsigned int *sse1, int *distortion, int *has_better_mv, int is_scaled) {
chiyotsai9f664e32020-02-19 14:17:31 -08002349 unsigned int cost;
2350 if (av1_is_subpelmv_in_range(mv_limits, *this_mv)) {
2351 unsigned int sse;
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302352 int thismse;
2353 if (is_scaled) {
2354 thismse = upsampled_pref_error(xd, cm, this_mv, var_params, &sse);
2355 } else {
2356 thismse = estimated_pref_error(this_mv, var_params, &sse);
2357 }
chiyotsai9f664e32020-02-19 14:17:31 -08002358 cost = mv_err_cost_(this_mv, mv_cost_params);
2359 cost += thismse;
2360
2361 if (cost < *besterr) {
2362 *besterr = cost;
2363 *best_mv = *this_mv;
2364 *distortion = thismse;
2365 *sse1 = sse;
2366 *has_better_mv |= 1;
2367 }
2368 } else {
2369 cost = INT_MAX;
2370 }
2371 return cost;
2372}
2373
2374// Checks whether this_mv is better than best_mv. This function incorporates
2375// both prediction error and residue into account.
2376static AOM_FORCE_INLINE unsigned int check_better(
2377 MACROBLOCKD *xd, const AV1_COMMON *cm, const MV *this_mv, MV *best_mv,
chiyotsai5aa70752020-03-26 10:13:10 -07002378 const SubpelMvLimits *mv_limits, const SUBPEL_SEARCH_VAR_PARAMS *var_params,
chiyotsai9f664e32020-02-19 14:17:31 -08002379 const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
2380 unsigned int *sse1, int *distortion, int *is_better) {
2381 unsigned int cost;
2382 if (av1_is_subpelmv_in_range(mv_limits, *this_mv)) {
2383 unsigned int sse;
2384 int thismse;
chiyotsai5aa70752020-03-26 10:13:10 -07002385 thismse = upsampled_pref_error(xd, cm, this_mv, var_params, &sse);
chiyotsai9f664e32020-02-19 14:17:31 -08002386 cost = mv_err_cost_(this_mv, mv_cost_params);
2387 cost += thismse;
2388 if (cost < *besterr) {
2389 *besterr = cost;
2390 *best_mv = *this_mv;
2391 *distortion = thismse;
2392 *sse1 = sse;
2393 *is_better |= 1;
2394 }
2395 } else {
2396 cost = INT_MAX;
2397 }
2398 return cost;
2399}
2400
chiyotsaic1df6a02020-03-19 13:21:39 -07002401static INLINE MV get_best_diag_step(int step_size, unsigned int left_cost,
2402 unsigned int right_cost,
2403 unsigned int up_cost,
2404 unsigned int down_cost) {
2405 const MV diag_step = { up_cost <= down_cost ? -step_size : step_size,
2406 left_cost <= right_cost ? -step_size : step_size };
2407
2408 return diag_step;
2409}
2410
chiyotsai9f664e32020-02-19 14:17:31 -08002411// Searches the four cardinal direction for a better mv, then follows up with a
2412// search in the best quadrant. This uses bilinear filter to speed up the
2413// calculation.
chiyotsaic1df6a02020-03-19 13:21:39 -07002414static AOM_FORCE_INLINE MV first_level_check_fast(
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302415 MACROBLOCKD *xd, const AV1_COMMON *cm, const MV this_mv, MV *best_mv,
2416 int hstep, const SubpelMvLimits *mv_limits,
chiyotsai5aa70752020-03-26 10:13:10 -07002417 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
chiyotsai9f664e32020-02-19 14:17:31 -08002418 const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302419 unsigned int *sse1, int *distortion, int is_scaled) {
chiyotsai9f664e32020-02-19 14:17:31 -08002420 // Check the four cardinal directions
chiyotsaic1df6a02020-03-19 13:21:39 -07002421 const MV left_mv = { this_mv.row, this_mv.col - hstep };
chiyotsai9f664e32020-02-19 14:17:31 -08002422 int dummy = 0;
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302423 const unsigned int left = check_better_fast(
2424 xd, cm, &left_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr,
2425 sse1, distortion, &dummy, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002426
chiyotsaic1df6a02020-03-19 13:21:39 -07002427 const MV right_mv = { this_mv.row, this_mv.col + hstep };
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302428 const unsigned int right = check_better_fast(
2429 xd, cm, &right_mv, best_mv, mv_limits, var_params, mv_cost_params,
2430 besterr, sse1, distortion, &dummy, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002431
chiyotsaic1df6a02020-03-19 13:21:39 -07002432 const MV top_mv = { this_mv.row - hstep, this_mv.col };
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302433 const unsigned int up = check_better_fast(
2434 xd, cm, &top_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr,
2435 sse1, distortion, &dummy, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002436
chiyotsaic1df6a02020-03-19 13:21:39 -07002437 const MV bottom_mv = { this_mv.row + hstep, this_mv.col };
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302438 const unsigned int down = check_better_fast(
2439 xd, cm, &bottom_mv, best_mv, mv_limits, var_params, mv_cost_params,
2440 besterr, sse1, distortion, &dummy, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002441
chiyotsaic1df6a02020-03-19 13:21:39 -07002442 const MV diag_step = get_best_diag_step(hstep, left, right, up, down);
2443 const MV diag_mv = { this_mv.row + diag_step.row,
2444 this_mv.col + diag_step.col };
2445
chiyotsai9f664e32020-02-19 14:17:31 -08002446 // Check the diagonal direction with the best mv
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302447 check_better_fast(xd, cm, &diag_mv, best_mv, mv_limits, var_params,
2448 mv_cost_params, besterr, sse1, distortion, &dummy,
2449 is_scaled);
chiyotsaic1df6a02020-03-19 13:21:39 -07002450
2451 return diag_step;
chiyotsai9f664e32020-02-19 14:17:31 -08002452}
2453
2454// Performs a following up search after first_level_check_fast is called. This
2455// performs two extra chess pattern searches in the best quadrant.
2456static AOM_FORCE_INLINE void second_level_check_fast(
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302457 MACROBLOCKD *xd, const AV1_COMMON *cm, const MV this_mv, const MV diag_step,
2458 MV *best_mv, int hstep, const SubpelMvLimits *mv_limits,
2459 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
chiyotsai9f664e32020-02-19 14:17:31 -08002460 const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302461 unsigned int *sse1, int *distortion, int is_scaled) {
chiyotsaic1df6a02020-03-19 13:21:39 -07002462 assert(diag_step.row == hstep || diag_step.row == -hstep);
2463 assert(diag_step.col == hstep || diag_step.col == -hstep);
2464 const int tr = this_mv.row;
2465 const int tc = this_mv.col;
chiyotsai9f664e32020-02-19 14:17:31 -08002466 const int br = best_mv->row;
2467 const int bc = best_mv->col;
2468 int dummy = 0;
2469 if (tr != br && tc != bc) {
chiyotsaic1df6a02020-03-19 13:21:39 -07002470 assert(diag_step.col == bc - tc);
2471 assert(diag_step.row == br - tr);
2472 const MV chess_mv_1 = { br, bc + diag_step.col };
2473 const MV chess_mv_2 = { br + diag_step.row, bc };
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302474 check_better_fast(xd, cm, &chess_mv_1, best_mv, mv_limits, var_params,
2475 mv_cost_params, besterr, sse1, distortion, &dummy,
2476 is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002477
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302478 check_better_fast(xd, cm, &chess_mv_2, best_mv, mv_limits, var_params,
2479 mv_cost_params, besterr, sse1, distortion, &dummy,
2480 is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002481 } else if (tr == br && tc != bc) {
chiyotsaic1df6a02020-03-19 13:21:39 -07002482 assert(diag_step.col == bc - tc);
2483 // Continue searching in the best direction
2484 const MV bottom_long_mv = { br + hstep, bc + diag_step.col };
2485 const MV top_long_mv = { br - hstep, bc + diag_step.col };
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302486 check_better_fast(xd, cm, &bottom_long_mv, best_mv, mv_limits, var_params,
2487 mv_cost_params, besterr, sse1, distortion, &dummy,
2488 is_scaled);
2489 check_better_fast(xd, cm, &top_long_mv, best_mv, mv_limits, var_params,
2490 mv_cost_params, besterr, sse1, distortion, &dummy,
2491 is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002492
chiyotsaic1df6a02020-03-19 13:21:39 -07002493 // Search in the direction opposite of the best quadrant
2494 const MV rev_mv = { br - diag_step.row, bc };
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302495 check_better_fast(xd, cm, &rev_mv, best_mv, mv_limits, var_params,
2496 mv_cost_params, besterr, sse1, distortion, &dummy,
2497 is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002498 } else if (tr != br && tc == bc) {
chiyotsaic1df6a02020-03-19 13:21:39 -07002499 assert(diag_step.row == br - tr);
2500 // Continue searching in the best direction
2501 const MV right_long_mv = { br + diag_step.row, bc + hstep };
2502 const MV left_long_mv = { br + diag_step.row, bc - hstep };
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302503 check_better_fast(xd, cm, &right_long_mv, best_mv, mv_limits, var_params,
2504 mv_cost_params, besterr, sse1, distortion, &dummy,
2505 is_scaled);
2506 check_better_fast(xd, cm, &left_long_mv, best_mv, mv_limits, var_params,
2507 mv_cost_params, besterr, sse1, distortion, &dummy,
2508 is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002509
chiyotsaic1df6a02020-03-19 13:21:39 -07002510 // Search in the direction opposite of the best quadrant
2511 const MV rev_mv = { br, bc - diag_step.col };
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302512 check_better_fast(xd, cm, &rev_mv, best_mv, mv_limits, var_params,
2513 mv_cost_params, besterr, sse1, distortion, &dummy,
2514 is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002515 }
2516}
2517
2518// Combines first level check and second level check when applicable. This first
2519// searches the four cardinal directions, and perform several
2520// diagonal/chess-pattern searches in the best quadrant.
2521static AOM_FORCE_INLINE void two_level_checks_fast(
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302522 MACROBLOCKD *xd, const AV1_COMMON *cm, const MV this_mv, MV *best_mv,
2523 int hstep, const SubpelMvLimits *mv_limits,
chiyotsai5aa70752020-03-26 10:13:10 -07002524 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
chiyotsai9f664e32020-02-19 14:17:31 -08002525 const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302526 unsigned int *sse1, int *distortion, int iters, int is_scaled) {
2527 const MV diag_step = first_level_check_fast(
2528 xd, cm, this_mv, best_mv, hstep, mv_limits, var_params, mv_cost_params,
2529 besterr, sse1, distortion, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002530 if (iters > 1) {
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302531 second_level_check_fast(xd, cm, this_mv, diag_step, best_mv, hstep,
2532 mv_limits, var_params, mv_cost_params, besterr,
2533 sse1, distortion, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002534 }
2535}
2536
chiyotsai5aa70752020-03-26 10:13:10 -07002537static AOM_FORCE_INLINE MV
2538first_level_check(MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV this_mv,
2539 MV *best_mv, const int hstep, const SubpelMvLimits *mv_limits,
2540 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2541 const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
2542 unsigned int *sse1, int *distortion) {
chiyotsai981eb5f2020-03-19 14:26:17 -07002543 int dummy = 0;
2544 const MV left_mv = { this_mv.row, this_mv.col - hstep };
2545 const MV right_mv = { this_mv.row, this_mv.col + hstep };
2546 const MV top_mv = { this_mv.row - hstep, this_mv.col };
2547 const MV bottom_mv = { this_mv.row + hstep, this_mv.col };
chiyotsai9f664e32020-02-19 14:17:31 -08002548
chiyotsai5aa70752020-03-26 10:13:10 -07002549 const unsigned int left =
2550 check_better(xd, cm, &left_mv, best_mv, mv_limits, var_params,
2551 mv_cost_params, besterr, sse1, distortion, &dummy);
2552 const unsigned int right =
2553 check_better(xd, cm, &right_mv, best_mv, mv_limits, var_params,
2554 mv_cost_params, besterr, sse1, distortion, &dummy);
2555 const unsigned int up =
2556 check_better(xd, cm, &top_mv, best_mv, mv_limits, var_params,
2557 mv_cost_params, besterr, sse1, distortion, &dummy);
2558 const unsigned int down =
2559 check_better(xd, cm, &bottom_mv, best_mv, mv_limits, var_params,
2560 mv_cost_params, besterr, sse1, distortion, &dummy);
chiyotsai981eb5f2020-03-19 14:26:17 -07002561
2562 const MV diag_step = get_best_diag_step(hstep, left, right, up, down);
2563 const MV diag_mv = { this_mv.row + diag_step.row,
2564 this_mv.col + diag_step.col };
2565
2566 // Check the diagonal direction with the best mv
chiyotsai5aa70752020-03-26 10:13:10 -07002567 check_better(xd, cm, &diag_mv, best_mv, mv_limits, var_params, mv_cost_params,
2568 besterr, sse1, distortion, &dummy);
chiyotsai981eb5f2020-03-19 14:26:17 -07002569
2570 return diag_step;
2571}
2572
2573// A newer version of second level check that gives better quality.
2574// TODO(chiyotsai@google.com): evaluate this on subpel_search_types different
2575// from av1_find_best_sub_pixel_tree
2576static AOM_FORCE_INLINE void second_level_check_v2(
2577 MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV this_mv, MV diag_step,
chiyotsai5aa70752020-03-26 10:13:10 -07002578 MV *best_mv, const SubpelMvLimits *mv_limits,
chiyotsai981eb5f2020-03-19 14:26:17 -07002579 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2580 const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302581 unsigned int *sse1, int *distortion, int is_scaled) {
chiyotsai981eb5f2020-03-19 14:26:17 -07002582 assert(best_mv->row == this_mv.row + diag_step.row ||
2583 best_mv->col == this_mv.col + diag_step.col);
2584 if (CHECK_MV_EQUAL(this_mv, *best_mv)) {
2585 return;
2586 } else if (this_mv.row == best_mv->row) {
2587 // Search away from diagonal step since diagonal search did not provide any
2588 // improvement
2589 diag_step.row *= -1;
2590 } else if (this_mv.col == best_mv->col) {
2591 diag_step.col *= -1;
chiyotsai9f664e32020-02-19 14:17:31 -08002592 }
2593
chiyotsai981eb5f2020-03-19 14:26:17 -07002594 const MV row_bias_mv = { best_mv->row + diag_step.row, best_mv->col };
2595 const MV col_bias_mv = { best_mv->row, best_mv->col + diag_step.col };
2596 const MV diag_bias_mv = { best_mv->row + diag_step.row,
2597 best_mv->col + diag_step.col };
chiyotsai9f664e32020-02-19 14:17:31 -08002598 int has_better_mv = 0;
2599
2600 if (var_params->subpel_search_type != USE_2_TAPS_ORIG) {
chiyotsai5aa70752020-03-26 10:13:10 -07002601 check_better(xd, cm, &row_bias_mv, best_mv, mv_limits, var_params,
2602 mv_cost_params, besterr, sse1, distortion, &has_better_mv);
2603 check_better(xd, cm, &col_bias_mv, best_mv, mv_limits, var_params,
2604 mv_cost_params, besterr, sse1, distortion, &has_better_mv);
chiyotsai9f664e32020-02-19 14:17:31 -08002605
2606 // Do an additional search if the second iteration gives a better mv
2607 if (has_better_mv) {
chiyotsai5aa70752020-03-26 10:13:10 -07002608 check_better(xd, cm, &diag_bias_mv, best_mv, mv_limits, var_params,
2609 mv_cost_params, besterr, sse1, distortion, &has_better_mv);
chiyotsai9f664e32020-02-19 14:17:31 -08002610 }
2611 } else {
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302612 check_better_fast(xd, cm, &row_bias_mv, best_mv, mv_limits, var_params,
2613 mv_cost_params, besterr, sse1, distortion, &has_better_mv,
2614 is_scaled);
2615 check_better_fast(xd, cm, &col_bias_mv, best_mv, mv_limits, var_params,
2616 mv_cost_params, besterr, sse1, distortion, &has_better_mv,
2617 is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002618
2619 // Do an additional search if the second iteration gives a better mv
2620 if (has_better_mv) {
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302621 check_better_fast(xd, cm, &diag_bias_mv, best_mv, mv_limits, var_params,
chiyotsai5aa70752020-03-26 10:13:10 -07002622 mv_cost_params, besterr, sse1, distortion,
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302623 &has_better_mv, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002624 }
2625 }
2626}
2627
2628// Gets the error at the beginning when the mv has fullpel precision
2629static unsigned int setup_center_error(
chiyotsai5aa70752020-03-26 10:13:10 -07002630 const MACROBLOCKD *xd, const MV *bestmv,
chiyotsai9f664e32020-02-19 14:17:31 -08002631 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2632 const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion) {
2633 const aom_variance_fn_ptr_t *vfp = var_params->vfp;
chiyotsai9f664e32020-02-19 14:17:31 -08002634 const int w = var_params->w;
2635 const int h = var_params->h;
2636
chiyotsai5aa70752020-03-26 10:13:10 -07002637 const MSBuffers *ms_buffers = &var_params->ms_buffers;
2638 const uint8_t *src = ms_buffers->src->buf;
2639 const uint8_t *y = get_buf_from_mv(ms_buffers->ref, *bestmv);
2640 const int src_stride = ms_buffers->src->stride;
2641 const int y_stride = ms_buffers->ref->stride;
2642 const uint8_t *second_pred = ms_buffers->second_pred;
2643 const uint8_t *mask = ms_buffers->mask;
2644 const int mask_stride = ms_buffers->mask_stride;
2645 const int invert_mask = ms_buffers->inv_mask;
2646
chiyotsai9f664e32020-02-19 14:17:31 -08002647 unsigned int besterr;
chiyotsai9f664e32020-02-19 14:17:31 -08002648
2649 if (second_pred != NULL) {
chiyotsai9f664e32020-02-19 14:17:31 -08002650 if (is_cur_buf_hbd(xd)) {
2651 DECLARE_ALIGNED(16, uint16_t, comp_pred16[MAX_SB_SQUARE]);
2652 uint8_t *comp_pred = CONVERT_TO_BYTEPTR(comp_pred16);
2653 if (mask) {
2654 aom_highbd_comp_mask_pred(comp_pred, second_pred, w, h, y, y_stride,
2655 mask, mask_stride, invert_mask);
2656 } else {
2657 aom_highbd_comp_avg_pred(comp_pred, second_pred, w, h, y, y_stride);
2658 }
2659 besterr = vfp->vf(comp_pred, w, src, src_stride, sse1);
2660 } else {
2661 DECLARE_ALIGNED(16, uint8_t, comp_pred[MAX_SB_SQUARE]);
2662 if (mask) {
2663 aom_comp_mask_pred(comp_pred, second_pred, w, h, y, y_stride, mask,
2664 mask_stride, invert_mask);
2665 } else {
2666 aom_comp_avg_pred(comp_pred, second_pred, w, h, y, y_stride);
2667 }
2668 besterr = vfp->vf(comp_pred, w, src, src_stride, sse1);
2669 }
chiyotsai9f664e32020-02-19 14:17:31 -08002670 } else {
2671 besterr = vfp->vf(y, y_stride, src, src_stride, sse1);
2672 }
2673 *distortion = besterr;
2674 besterr += mv_err_cost_(bestmv, mv_cost_params);
2675 return besterr;
2676}
2677
2678// Gets the error at the beginning when the mv has fullpel precision
2679static unsigned int upsampled_setup_center_error(
2680 MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV *bestmv,
chiyotsai5aa70752020-03-26 10:13:10 -07002681 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
chiyotsai9f664e32020-02-19 14:17:31 -08002682 const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion) {
chiyotsai5aa70752020-03-26 10:13:10 -07002683 unsigned int besterr = upsampled_pref_error(xd, cm, bestmv, var_params, sse1);
chiyotsai9f664e32020-02-19 14:17:31 -08002684 *distortion = besterr;
2685 besterr += mv_err_cost_(bestmv, mv_cost_params);
2686 return besterr;
2687}
2688
2689static INLINE int divide_and_round(int n, int d) {
2690 return ((n < 0) ^ (d < 0)) ? ((n - d / 2) / d) : ((n + d / 2) / d);
2691}
2692
2693static INLINE int is_cost_list_wellbehaved(const int *cost_list) {
2694 return cost_list[0] < cost_list[1] && cost_list[0] < cost_list[2] &&
2695 cost_list[0] < cost_list[3] && cost_list[0] < cost_list[4];
2696}
2697
2698// Returns surface minima estimate at given precision in 1/2^n bits.
2699// Assume a model for the cost surface: S = A(x - x0)^2 + B(y - y0)^2 + C
2700// For a given set of costs S0, S1, S2, S3, S4 at points
2701// (y, x) = (0, 0), (0, -1), (1, 0), (0, 1) and (-1, 0) respectively,
2702// the solution for the location of the minima (x0, y0) is given by:
2703// x0 = 1/2 (S1 - S3)/(S1 + S3 - 2*S0),
2704// y0 = 1/2 (S4 - S2)/(S4 + S2 - 2*S0).
2705// The code below is an integerized version of that.
2706static AOM_INLINE void get_cost_surf_min(const int *cost_list, int *ir, int *ic,
2707 int bits) {
2708 *ic = divide_and_round((cost_list[1] - cost_list[3]) * (1 << (bits - 1)),
2709 (cost_list[1] - 2 * cost_list[0] + cost_list[3]));
2710 *ir = divide_and_round((cost_list[4] - cost_list[2]) * (1 << (bits - 1)),
2711 (cost_list[4] - 2 * cost_list[0] + cost_list[2]));
2712}
2713
chiyotsai083db972020-03-30 11:43:52 -07002714// Checks the list of mvs searched in the last iteration and see if we are
2715// repeating it. If so, return 1. Otherwise we update the last_mv_search_list
2716// with current_mv and return 0.
2717static INLINE int check_repeated_mv_and_update(int_mv *last_mv_search_list,
2718 const MV current_mv, int iter) {
2719 if (last_mv_search_list) {
2720 if (CHECK_MV_EQUAL(last_mv_search_list[iter].as_mv, current_mv)) {
2721 return 1;
2722 }
2723
2724 last_mv_search_list[iter].as_mv = current_mv;
2725 }
2726 return 0;
2727}
2728
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302729static AOM_INLINE int setup_center_error_facade(
2730 MACROBLOCKD *xd, const AV1_COMMON *cm, const MV *bestmv,
2731 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
2732 const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion,
2733 int is_scaled) {
2734 if (is_scaled) {
2735 return upsampled_setup_center_error(xd, cm, bestmv, var_params,
2736 mv_cost_params, sse1, distortion);
2737 } else {
2738 return setup_center_error(xd, bestmv, var_params, mv_cost_params, sse1,
2739 distortion);
2740 }
2741}
2742
chiyotsai9f664e32020-02-19 14:17:31 -08002743int av1_find_best_sub_pixel_tree_pruned_evenmore(
chiyotsai5aa70752020-03-26 10:13:10 -07002744 MACROBLOCKD *xd, const AV1_COMMON *const cm,
2745 const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, MV start_mv, MV *bestmv,
2746 int *distortion, unsigned int *sse1, int_mv *last_mv_search_list) {
2747 (void)cm;
chiyotsai9f664e32020-02-19 14:17:31 -08002748 const int allow_hp = ms_params->allow_hp;
2749 const int forced_stop = ms_params->forced_stop;
2750 const int iters_per_step = ms_params->iters_per_step;
2751 const int *cost_list = ms_params->cost_list;
chiyotsai5aa70752020-03-26 10:13:10 -07002752 const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
chiyotsai9f664e32020-02-19 14:17:31 -08002753 const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
2754 const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params;
2755
chiyotsai083db972020-03-30 11:43:52 -07002756 // The iteration we are current searching for. Iter 0 corresponds to fullpel
2757 // mv, iter 1 to half pel, and so on
2758 int iter = 0;
2759 int hstep = INIT_SUBPEL_STEP_SIZE; // Step size, initialized to 4/8=1/2 pel
chiyotsai9f664e32020-02-19 14:17:31 -08002760 unsigned int besterr = INT_MAX;
chiyotsai5aa70752020-03-26 10:13:10 -07002761 *bestmv = start_mv;
leolzhao3ab59842021-05-11 10:07:48 -07002762#if CONFIG_SDP
2763 const struct scale_factors *const sf =
2764 is_intrabc_block(xd->mi[0], xd->tree_type)
2765#else
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302766 const struct scale_factors *const sf = is_intrabc_block(xd->mi[0])
leolzhao3ab59842021-05-11 10:07:48 -07002767#endif
2768 ? &cm->sf_identity
2769 : xd->block_ref_scale_factors[0];
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302770 const int is_scaled = av1_is_scaled(sf);
2771 besterr = setup_center_error_facade(
2772 xd, cm, bestmv, var_params, mv_cost_params, sse1, distortion, is_scaled);
2773
Yunqing Wang8e94c582020-04-15 17:24:47 -07002774 // If forced_stop is FULL_PEL, return.
2775 if (forced_stop == FULL_PEL) return besterr;
chiyotsai9f664e32020-02-19 14:17:31 -08002776
chiyotsai083db972020-03-30 11:43:52 -07002777 if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2778 return INT_MAX;
2779 }
2780 iter++;
2781
chiyotsai9f664e32020-02-19 14:17:31 -08002782 if (cost_list && cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
2783 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
2784 cost_list[4] != INT_MAX && is_cost_list_wellbehaved(cost_list)) {
2785 int ir, ic;
2786 int dummy = 0;
2787 get_cost_surf_min(cost_list, &ir, &ic, 2);
2788 if (ir != 0 || ic != 0) {
2789 const MV this_mv = { start_mv.row + 2 * ir, start_mv.col + 2 * ic };
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302790 check_better_fast(xd, cm, &this_mv, bestmv, mv_limits, var_params,
2791 mv_cost_params, &besterr, sse1, distortion, &dummy,
2792 is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002793 }
2794 } else {
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302795 two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits,
2796 var_params, mv_cost_params, &besterr, sse1,
2797 distortion, iters_per_step, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002798
2799 // Each subsequent iteration checks at least one point in common with
2800 // the last iteration could be 2 ( if diag selected) 1/4 pel
Yunqing Wang8e94c582020-04-15 17:24:47 -07002801 if (forced_stop < HALF_PEL) {
chiyotsai083db972020-03-30 11:43:52 -07002802 if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2803 return INT_MAX;
2804 }
2805 iter++;
2806
chiyotsai9f664e32020-02-19 14:17:31 -08002807 hstep >>= 1;
2808 start_mv = *bestmv;
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302809 two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits,
2810 var_params, mv_cost_params, &besterr, sse1,
2811 distortion, iters_per_step, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002812 }
2813 }
2814
2815 if (allow_hp && forced_stop == EIGHTH_PEL) {
chiyotsai083db972020-03-30 11:43:52 -07002816 if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2817 return INT_MAX;
2818 }
2819 iter++;
2820
chiyotsai9f664e32020-02-19 14:17:31 -08002821 hstep >>= 1;
2822 start_mv = *bestmv;
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302823 two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits,
2824 var_params, mv_cost_params, &besterr, sse1,
2825 distortion, iters_per_step, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002826 }
2827
2828 return besterr;
2829}
2830
2831int av1_find_best_sub_pixel_tree_pruned_more(
chiyotsai5aa70752020-03-26 10:13:10 -07002832 MACROBLOCKD *xd, const AV1_COMMON *const cm,
2833 const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, MV start_mv, MV *bestmv,
2834 int *distortion, unsigned int *sse1, int_mv *last_mv_search_list) {
2835 (void)cm;
chiyotsai9f664e32020-02-19 14:17:31 -08002836 const int allow_hp = ms_params->allow_hp;
2837 const int forced_stop = ms_params->forced_stop;
2838 const int iters_per_step = ms_params->iters_per_step;
2839 const int *cost_list = ms_params->cost_list;
chiyotsai5aa70752020-03-26 10:13:10 -07002840 const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
chiyotsai9f664e32020-02-19 14:17:31 -08002841 const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
2842 const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params;
2843
chiyotsai083db972020-03-30 11:43:52 -07002844 // The iteration we are current searching for. Iter 0 corresponds to fullpel
2845 // mv, iter 1 to half pel, and so on
2846 int iter = 0;
2847 int hstep = INIT_SUBPEL_STEP_SIZE; // Step size, initialized to 4/8=1/2 pel
chiyotsai9f664e32020-02-19 14:17:31 -08002848 unsigned int besterr = INT_MAX;
chiyotsai5aa70752020-03-26 10:13:10 -07002849 *bestmv = start_mv;
leolzhao3ab59842021-05-11 10:07:48 -07002850#if CONFIG_SDP
2851 const struct scale_factors *const sf =
2852 is_intrabc_block(xd->mi[0], xd->tree_type)
2853#else
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302854 const struct scale_factors *const sf = is_intrabc_block(xd->mi[0])
leolzhao3ab59842021-05-11 10:07:48 -07002855#endif
2856 ? &cm->sf_identity
2857 : xd->block_ref_scale_factors[0];
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302858 const int is_scaled = av1_is_scaled(sf);
2859 besterr = setup_center_error_facade(
2860 xd, cm, bestmv, var_params, mv_cost_params, sse1, distortion, is_scaled);
2861
Yunqing Wang8e94c582020-04-15 17:24:47 -07002862 // If forced_stop is FULL_PEL, return.
2863 if (forced_stop == FULL_PEL) return besterr;
chiyotsai083db972020-03-30 11:43:52 -07002864
2865 if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2866 return INT_MAX;
2867 }
2868 iter++;
2869
chiyotsai9f664e32020-02-19 14:17:31 -08002870 if (cost_list && cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
2871 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
2872 cost_list[4] != INT_MAX && is_cost_list_wellbehaved(cost_list)) {
2873 int ir, ic;
2874 get_cost_surf_min(cost_list, &ir, &ic, 1);
2875 if (ir != 0 || ic != 0) {
2876 const MV this_mv = { start_mv.row + ir * hstep,
2877 start_mv.col + ic * hstep };
2878 int dummy = 0;
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302879 check_better_fast(xd, cm, &this_mv, bestmv, mv_limits, var_params,
2880 mv_cost_params, &besterr, sse1, distortion, &dummy,
2881 is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002882 }
2883 } else {
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302884 two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits,
2885 var_params, mv_cost_params, &besterr, sse1,
2886 distortion, iters_per_step, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002887 }
2888
2889 // Each subsequent iteration checks at least one point in common with
2890 // the last iteration could be 2 ( if diag selected) 1/4 pel
Yunqing Wang8e94c582020-04-15 17:24:47 -07002891 if (forced_stop < HALF_PEL) {
chiyotsai083db972020-03-30 11:43:52 -07002892 if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2893 return INT_MAX;
2894 }
2895 iter++;
2896
chiyotsai9f664e32020-02-19 14:17:31 -08002897 hstep >>= 1;
2898 start_mv = *bestmv;
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302899 two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits,
2900 var_params, mv_cost_params, &besterr, sse1,
2901 distortion, iters_per_step, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002902 }
2903
2904 if (allow_hp && forced_stop == EIGHTH_PEL) {
chiyotsai083db972020-03-30 11:43:52 -07002905 if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2906 return INT_MAX;
2907 }
2908 iter++;
2909
chiyotsai9f664e32020-02-19 14:17:31 -08002910 hstep >>= 1;
2911 start_mv = *bestmv;
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302912 two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits,
2913 var_params, mv_cost_params, &besterr, sse1,
2914 distortion, iters_per_step, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002915 }
2916
2917 return besterr;
2918}
2919
2920int av1_find_best_sub_pixel_tree_pruned(
chiyotsai5aa70752020-03-26 10:13:10 -07002921 MACROBLOCKD *xd, const AV1_COMMON *const cm,
2922 const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, MV start_mv, MV *bestmv,
2923 int *distortion, unsigned int *sse1, int_mv *last_mv_search_list) {
2924 (void)cm;
chiyotsai9f664e32020-02-19 14:17:31 -08002925 const int allow_hp = ms_params->allow_hp;
2926 const int forced_stop = ms_params->forced_stop;
2927 const int iters_per_step = ms_params->iters_per_step;
2928 const int *cost_list = ms_params->cost_list;
chiyotsai5aa70752020-03-26 10:13:10 -07002929 const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
chiyotsai9f664e32020-02-19 14:17:31 -08002930 const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
2931 const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params;
2932
chiyotsai083db972020-03-30 11:43:52 -07002933 // The iteration we are current searching for. Iter 0 corresponds to fullpel
2934 // mv, iter 1 to half pel, and so on
2935 int iter = 0;
2936 int hstep = INIT_SUBPEL_STEP_SIZE; // Step size, initialized to 4/8=1/2 pel
chiyotsai9f664e32020-02-19 14:17:31 -08002937 unsigned int besterr = INT_MAX;
chiyotsai5aa70752020-03-26 10:13:10 -07002938 *bestmv = start_mv;
leolzhao3ab59842021-05-11 10:07:48 -07002939#if CONFIG_SDP
2940 const struct scale_factors *const sf =
2941 is_intrabc_block(xd->mi[0], xd->tree_type)
2942#else
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302943 const struct scale_factors *const sf = is_intrabc_block(xd->mi[0])
leolzhao3ab59842021-05-11 10:07:48 -07002944#endif
2945 ? &cm->sf_identity
2946 : xd->block_ref_scale_factors[0];
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302947 const int is_scaled = av1_is_scaled(sf);
2948 besterr = setup_center_error_facade(
2949 xd, cm, bestmv, var_params, mv_cost_params, sse1, distortion, is_scaled);
2950
Yunqing Wang8e94c582020-04-15 17:24:47 -07002951 // If forced_stop is FULL_PEL, return.
2952 if (forced_stop == FULL_PEL) return besterr;
2953
chiyotsai083db972020-03-30 11:43:52 -07002954 if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
2955 return INT_MAX;
2956 }
2957 iter++;
2958
chiyotsai9f664e32020-02-19 14:17:31 -08002959 if (cost_list && cost_list[0] != INT_MAX && cost_list[1] != INT_MAX &&
2960 cost_list[2] != INT_MAX && cost_list[3] != INT_MAX &&
2961 cost_list[4] != INT_MAX) {
2962 const unsigned int whichdir = (cost_list[1] < cost_list[3] ? 0 : 1) +
2963 (cost_list[2] < cost_list[4] ? 0 : 2);
2964
2965 const MV left_mv = { start_mv.row, start_mv.col - hstep };
2966 const MV right_mv = { start_mv.row, start_mv.col + hstep };
2967 const MV bottom_mv = { start_mv.row + hstep, start_mv.col };
2968 const MV top_mv = { start_mv.row - hstep, start_mv.col };
2969
2970 const MV bottom_left_mv = { start_mv.row + hstep, start_mv.col - hstep };
2971 const MV bottom_right_mv = { start_mv.row + hstep, start_mv.col + hstep };
2972 const MV top_left_mv = { start_mv.row - hstep, start_mv.col - hstep };
2973 const MV top_right_mv = { start_mv.row - hstep, start_mv.col + hstep };
2974
2975 int dummy = 0;
2976
2977 switch (whichdir) {
2978 case 0: // bottom left quadrant
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302979 check_better_fast(xd, cm, &left_mv, bestmv, mv_limits, var_params,
2980 mv_cost_params, &besterr, sse1, distortion, &dummy,
2981 is_scaled);
2982 check_better_fast(xd, cm, &bottom_mv, bestmv, mv_limits, var_params,
2983 mv_cost_params, &besterr, sse1, distortion, &dummy,
2984 is_scaled);
2985 check_better_fast(xd, cm, &bottom_left_mv, bestmv, mv_limits,
2986 var_params, mv_cost_params, &besterr, sse1,
2987 distortion, &dummy, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002988 break;
2989 case 1: // bottom right quadrant
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05302990 check_better_fast(xd, cm, &right_mv, bestmv, mv_limits, var_params,
2991 mv_cost_params, &besterr, sse1, distortion, &dummy,
2992 is_scaled);
2993 check_better_fast(xd, cm, &bottom_mv, bestmv, mv_limits, var_params,
2994 mv_cost_params, &besterr, sse1, distortion, &dummy,
2995 is_scaled);
2996 check_better_fast(xd, cm, &bottom_right_mv, bestmv, mv_limits,
2997 var_params, mv_cost_params, &besterr, sse1,
2998 distortion, &dummy, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08002999 break;
3000 case 2: // top left quadrant
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05303001 check_better_fast(xd, cm, &left_mv, bestmv, mv_limits, var_params,
3002 mv_cost_params, &besterr, sse1, distortion, &dummy,
3003 is_scaled);
3004 check_better_fast(xd, cm, &top_mv, bestmv, mv_limits, var_params,
3005 mv_cost_params, &besterr, sse1, distortion, &dummy,
3006 is_scaled);
3007 check_better_fast(xd, cm, &top_left_mv, bestmv, mv_limits, var_params,
3008 mv_cost_params, &besterr, sse1, distortion, &dummy,
3009 is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08003010 break;
3011 case 3: // top right quadrant
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05303012 check_better_fast(xd, cm, &right_mv, bestmv, mv_limits, var_params,
3013 mv_cost_params, &besterr, sse1, distortion, &dummy,
3014 is_scaled);
3015 check_better_fast(xd, cm, &top_mv, bestmv, mv_limits, var_params,
3016 mv_cost_params, &besterr, sse1, distortion, &dummy,
3017 is_scaled);
3018 check_better_fast(xd, cm, &top_right_mv, bestmv, mv_limits, var_params,
3019 mv_cost_params, &besterr, sse1, distortion, &dummy,
3020 is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08003021 break;
3022 }
3023 } else {
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05303024 two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits,
3025 var_params, mv_cost_params, &besterr, sse1,
3026 distortion, iters_per_step, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08003027 }
3028
3029 // Each subsequent iteration checks at least one point in common with
3030 // the last iteration could be 2 ( if diag selected) 1/4 pel
Yunqing Wang8e94c582020-04-15 17:24:47 -07003031 if (forced_stop < HALF_PEL) {
chiyotsai083db972020-03-30 11:43:52 -07003032 if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
3033 return INT_MAX;
3034 }
3035 iter++;
3036
chiyotsai9f664e32020-02-19 14:17:31 -08003037 hstep >>= 1;
3038 start_mv = *bestmv;
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05303039 two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits,
3040 var_params, mv_cost_params, &besterr, sse1,
3041 distortion, iters_per_step, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08003042 }
3043
3044 if (allow_hp && forced_stop == EIGHTH_PEL) {
chiyotsai083db972020-03-30 11:43:52 -07003045 if (check_repeated_mv_and_update(last_mv_search_list, *bestmv, iter)) {
3046 return INT_MAX;
3047 }
3048 iter++;
3049
chiyotsai9f664e32020-02-19 14:17:31 -08003050 hstep >>= 1;
3051 start_mv = *bestmv;
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05303052 two_level_checks_fast(xd, cm, start_mv, bestmv, hstep, mv_limits,
3053 var_params, mv_cost_params, &besterr, sse1,
3054 distortion, iters_per_step, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08003055 }
3056
3057 return besterr;
3058}
3059
chiyotsai5aa70752020-03-26 10:13:10 -07003060int av1_find_best_sub_pixel_tree(MACROBLOCKD *xd, const AV1_COMMON *const cm,
chiyotsai9f664e32020-02-19 14:17:31 -08003061 const SUBPEL_MOTION_SEARCH_PARAMS *ms_params,
Yaowu Xuf8b0e9b2020-03-31 13:14:38 -07003062 MV start_mv, MV *bestmv, int *distortion,
chiyotsai5aa70752020-03-26 10:13:10 -07003063 unsigned int *sse1,
3064 int_mv *last_mv_search_list) {
chiyotsai9f664e32020-02-19 14:17:31 -08003065 const int allow_hp = ms_params->allow_hp;
3066 const int forced_stop = ms_params->forced_stop;
3067 const int iters_per_step = ms_params->iters_per_step;
chiyotsai9f664e32020-02-19 14:17:31 -08003068 const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
3069 const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params;
chiyotsai9f664e32020-02-19 14:17:31 -08003070 const SUBPEL_SEARCH_TYPE subpel_search_type =
3071 ms_params->var_params.subpel_search_type;
chiyotsai5aa70752020-03-26 10:13:10 -07003072 const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
chiyotsai9f664e32020-02-19 14:17:31 -08003073
chiyotsai083db972020-03-30 11:43:52 -07003074 // How many steps to take. A round of 0 means fullpel search only, 1 means
3075 // half-pel, and so on.
chiyotsai5aa70752020-03-26 10:13:10 -07003076 const int round = AOMMIN(FULL_PEL - forced_stop, 3 - !allow_hp);
chiyotsai083db972020-03-30 11:43:52 -07003077 int hstep = INIT_SUBPEL_STEP_SIZE; // Step size, initialized to 4/8=1/2 pel
chiyotsai5aa70752020-03-26 10:13:10 -07003078
chiyotsai9f664e32020-02-19 14:17:31 -08003079 unsigned int besterr = INT_MAX;
3080
chiyotsai5aa70752020-03-26 10:13:10 -07003081 *bestmv = start_mv;
leolzhao3ab59842021-05-11 10:07:48 -07003082#if CONFIG_SDP
3083 const struct scale_factors *const sf =
3084 is_intrabc_block(xd->mi[0], xd->tree_type)
3085#else
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05303086 const struct scale_factors *const sf = is_intrabc_block(xd->mi[0])
leolzhao3ab59842021-05-11 10:07:48 -07003087#endif
3088 ? &cm->sf_identity
3089 : xd->block_ref_scale_factors[0];
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05303090 const int is_scaled = av1_is_scaled(sf);
3091
chiyotsai9f664e32020-02-19 14:17:31 -08003092 if (subpel_search_type != USE_2_TAPS_ORIG) {
chiyotsai5aa70752020-03-26 10:13:10 -07003093 besterr = upsampled_setup_center_error(xd, cm, bestmv, var_params,
3094 mv_cost_params, sse1, distortion);
chiyotsai9f664e32020-02-19 14:17:31 -08003095 } else {
chiyotsai5aa70752020-03-26 10:13:10 -07003096 besterr = setup_center_error(xd, bestmv, var_params, mv_cost_params, sse1,
3097 distortion);
chiyotsai9f664e32020-02-19 14:17:31 -08003098 }
3099
Yunqing Wang8e94c582020-04-15 17:24:47 -07003100 // If forced_stop is FULL_PEL, return.
3101 if (!round) return besterr;
3102
chiyotsai5aa70752020-03-26 10:13:10 -07003103 for (int iter = 0; iter < round; ++iter) {
chiyotsai981eb5f2020-03-19 14:26:17 -07003104 MV iter_center_mv = *bestmv;
chiyotsai083db972020-03-30 11:43:52 -07003105 if (check_repeated_mv_and_update(last_mv_search_list, iter_center_mv,
3106 iter)) {
3107 return INT_MAX;
chiyotsai5aa70752020-03-26 10:13:10 -07003108 }
chiyotsai9f664e32020-02-19 14:17:31 -08003109
chiyotsai981eb5f2020-03-19 14:26:17 -07003110 MV diag_step;
3111 if (subpel_search_type != USE_2_TAPS_ORIG) {
3112 diag_step = first_level_check(xd, cm, iter_center_mv, bestmv, hstep,
chiyotsai5aa70752020-03-26 10:13:10 -07003113 mv_limits, var_params, mv_cost_params,
3114 &besterr, sse1, distortion);
chiyotsai981eb5f2020-03-19 14:26:17 -07003115 } else {
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05303116 diag_step = first_level_check_fast(xd, cm, iter_center_mv, bestmv, hstep,
chiyotsai5aa70752020-03-26 10:13:10 -07003117 mv_limits, var_params, mv_cost_params,
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05303118 &besterr, sse1, distortion, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08003119 }
3120
3121 // Check diagonal sub-pixel position
chiyotsai981eb5f2020-03-19 14:26:17 -07003122 if (!CHECK_MV_EQUAL(iter_center_mv, *bestmv) && iters_per_step > 1) {
3123 second_level_check_v2(xd, cm, iter_center_mv, diag_step, bestmv,
chiyotsai5aa70752020-03-26 10:13:10 -07003124 mv_limits, var_params, mv_cost_params, &besterr,
Mufaddal Chakera25bb7d02020-06-18 12:53:03 +05303125 sse1, distortion, is_scaled);
chiyotsai9f664e32020-02-19 14:17:31 -08003126 }
3127
chiyotsai9f664e32020-02-19 14:17:31 -08003128 hstep >>= 1;
3129 }
3130
chiyotsai9f664e32020-02-19 14:17:31 -08003131 return besterr;
3132}
3133
Yunqing Wangff4fa062017-04-21 10:56:08 -07003134// Note(yunqingwang): The following 2 functions are only used in the motion
3135// vector unit test, which return extreme motion vectors allowed by the MV
3136// limits.
chiyotsai9f664e32020-02-19 14:17:31 -08003137// Returns the maximum MV.
chiyotsai5aa70752020-03-26 10:13:10 -07003138int av1_return_max_sub_pixel_mv(MACROBLOCKD *xd, const AV1_COMMON *const cm,
chiyotsai2aac3002020-02-13 17:02:01 -08003139 const SUBPEL_MOTION_SEARCH_PARAMS *ms_params,
Yaowu Xuf8b0e9b2020-03-31 13:14:38 -07003140 MV start_mv, MV *bestmv, int *distortion,
chiyotsai5aa70752020-03-26 10:13:10 -07003141 unsigned int *sse1,
3142 int_mv *last_mv_search_list) {
3143 (void)xd;
Urvang Joshi52b62992018-02-02 14:43:07 -08003144 (void)cm;
chiyotsai5aa70752020-03-26 10:13:10 -07003145 (void)start_mv;
chiyotsaia742e7d2020-02-14 17:27:58 -08003146 (void)sse1;
3147 (void)distortion;
chiyotsai5aa70752020-03-26 10:13:10 -07003148 (void)last_mv_search_list;
chiyotsaia742e7d2020-02-14 17:27:58 -08003149
3150 const int allow_hp = ms_params->allow_hp;
chiyotsai5aa70752020-03-26 10:13:10 -07003151 const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
chiyotsaia742e7d2020-02-14 17:27:58 -08003152
chiyotsai5aa70752020-03-26 10:13:10 -07003153 bestmv->row = mv_limits->row_max;
3154 bestmv->col = mv_limits->col_max;
chiyotsaia742e7d2020-02-14 17:27:58 -08003155
3156 unsigned int besterr = 0;
3157
Yaowu Xu45295c32018-03-29 12:06:10 -07003158 // In the sub-pel motion search, if hp is not used, then the last bit of mv
3159 // has to be 0.
RogerZhou3b635242017-09-19 10:06:46 -07003160 lower_mv_precision(bestmv, allow_hp, 0);
Yunqing Wangff4fa062017-04-21 10:56:08 -07003161 return besterr;
3162}
chiyotsai9f664e32020-02-19 14:17:31 -08003163
chiyotsaia7e2cf82020-02-19 16:13:29 -08003164// Returns the minimum MV.
chiyotsai5aa70752020-03-26 10:13:10 -07003165int av1_return_min_sub_pixel_mv(MACROBLOCKD *xd, const AV1_COMMON *const cm,
chiyotsai2aac3002020-02-13 17:02:01 -08003166 const SUBPEL_MOTION_SEARCH_PARAMS *ms_params,
Yaowu Xuf8b0e9b2020-03-31 13:14:38 -07003167 MV start_mv, MV *bestmv, int *distortion,
chiyotsai5aa70752020-03-26 10:13:10 -07003168 unsigned int *sse1,
3169 int_mv *last_mv_search_list) {
3170 (void)xd;
Urvang Joshi52b62992018-02-02 14:43:07 -08003171 (void)cm;
chiyotsai5aa70752020-03-26 10:13:10 -07003172 (void)start_mv;
chiyotsaia742e7d2020-02-14 17:27:58 -08003173 (void)sse1;
3174 (void)distortion;
chiyotsai5aa70752020-03-26 10:13:10 -07003175 (void)last_mv_search_list;
chiyotsaia742e7d2020-02-14 17:27:58 -08003176
3177 const int allow_hp = ms_params->allow_hp;
chiyotsai5aa70752020-03-26 10:13:10 -07003178 const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
chiyotsaia742e7d2020-02-14 17:27:58 -08003179
chiyotsai5aa70752020-03-26 10:13:10 -07003180 bestmv->row = mv_limits->row_min;
3181 bestmv->col = mv_limits->col_min;
chiyotsaia742e7d2020-02-14 17:27:58 -08003182
3183 unsigned int besterr = 0;
Yaowu Xu45295c32018-03-29 12:06:10 -07003184 // In the sub-pel motion search, if hp is not used, then the last bit of mv
3185 // has to be 0.
RogerZhou3b635242017-09-19 10:06:46 -07003186 lower_mv_precision(bestmv, allow_hp, 0);
Yunqing Wangff4fa062017-04-21 10:56:08 -07003187 return besterr;
3188}
chiyotsai19a58ee2019-03-18 18:01:05 -07003189
chiyotsai74da72b2020-04-07 13:45:40 -07003190// Computes the cost of the current predictor by going through the whole
3191// av1_enc_build_inter_predictor pipeline. This is mainly used by warped mv
3192// during motion_mode_rd. We are going through the whole
3193// av1_enc_build_inter_predictor because we might have changed the interpolation
3194// filter, etc before motion_mode_rd is called.
3195static INLINE unsigned int compute_motion_cost(
3196 MACROBLOCKD *xd, const AV1_COMMON *const cm,
3197 const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, BLOCK_SIZE bsize,
3198 const MV *this_mv) {
3199 unsigned int mse;
3200 unsigned int sse;
3201 const int mi_row = xd->mi_row;
3202 const int mi_col = xd->mi_col;
3203
3204 av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
3205 AOM_PLANE_Y, AOM_PLANE_Y);
3206
3207 const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params;
3208 const MSBuffers *ms_buffers = &var_params->ms_buffers;
3209
3210 const uint8_t *const src = ms_buffers->src->buf;
3211 const int src_stride = ms_buffers->src->stride;
3212 const uint8_t *const dst = xd->plane[0].dst.buf;
3213 const int dst_stride = xd->plane[0].dst.stride;
3214 const aom_variance_fn_ptr_t *vfp = ms_params->var_params.vfp;
3215
3216 mse = vfp->vf(dst, dst_stride, src, src_stride, &sse);
3217 mse += mv_err_cost_(this_mv, &ms_params->mv_cost_params);
3218 return mse;
3219}
3220
chiyotsaia7e2cf82020-02-19 16:13:29 -08003221// Refines MV in a small range
chiyotsai74da72b2020-04-07 13:45:40 -07003222unsigned int av1_refine_warped_mv(MACROBLOCKD *xd, const AV1_COMMON *const cm,
3223 const SUBPEL_MOTION_SEARCH_PARAMS *ms_params,
3224 BLOCK_SIZE bsize, const int *pts0,
3225 const int *pts_inref0, int total_samples) {
chiyotsai19a58ee2019-03-18 18:01:05 -07003226 MB_MODE_INFO *mbmi = xd->mi[0];
chiyotsai74da72b2020-04-07 13:45:40 -07003227 static const MV neighbors[8] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 },
3228 { 0, -2 }, { 2, 0 }, { 0, 2 }, { -2, 0 } };
3229 MV *best_mv = &mbmi->mv[0].as_mv;
3230
chiyotsai9f664e32020-02-19 14:17:31 -08003231 WarpedMotionParams best_wm_params = mbmi->wm_params;
3232 int best_num_proj_ref = mbmi->num_proj_ref;
3233 unsigned int bestmse;
chiyotsai74da72b2020-04-07 13:45:40 -07003234 const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
chiyotsai19a58ee2019-03-18 18:01:05 -07003235
chiyotsai74da72b2020-04-07 13:45:40 -07003236 const int start = ms_params->allow_hp ? 0 : 4;
chiyotsai9f664e32020-02-19 14:17:31 -08003237
3238 // Calculate the center position's error
chiyotsai74da72b2020-04-07 13:45:40 -07003239 assert(av1_is_subpelmv_in_range(mv_limits, *best_mv));
3240 bestmse = compute_motion_cost(xd, cm, ms_params, bsize, best_mv);
chiyotsai9f664e32020-02-19 14:17:31 -08003241
3242 // MV search
chiyotsai74da72b2020-04-07 13:45:40 -07003243 int pts[SAMPLES_ARRAY_SIZE], pts_inref[SAMPLES_ARRAY_SIZE];
chiyotsai9f664e32020-02-19 14:17:31 -08003244 const int mi_row = xd->mi_row;
3245 const int mi_col = xd->mi_col;
chiyotsai74da72b2020-04-07 13:45:40 -07003246 for (int ite = 0; ite < 2; ++ite) {
chiyotsai9f664e32020-02-19 14:17:31 -08003247 int best_idx = -1;
chiyotsai9f664e32020-02-19 14:17:31 -08003248
chiyotsai74da72b2020-04-07 13:45:40 -07003249 for (int idx = start; idx < start + 4; ++idx) {
chiyotsai9f664e32020-02-19 14:17:31 -08003250 unsigned int thismse;
3251
chiyotsai74da72b2020-04-07 13:45:40 -07003252 MV this_mv = { best_mv->row + neighbors[idx].row,
3253 best_mv->col + neighbors[idx].col };
3254 if (av1_is_subpelmv_in_range(mv_limits, this_mv)) {
chiyotsai9f664e32020-02-19 14:17:31 -08003255 memcpy(pts, pts0, total_samples * 2 * sizeof(*pts0));
3256 memcpy(pts_inref, pts_inref0, total_samples * 2 * sizeof(*pts_inref0));
3257 if (total_samples > 1)
3258 mbmi->num_proj_ref =
3259 av1_selectSamples(&this_mv, pts, pts_inref, total_samples, bsize);
3260
chiyotsai74da72b2020-04-07 13:45:40 -07003261 if (!av1_find_projection(mbmi->num_proj_ref, pts, pts_inref, bsize,
3262 this_mv.row, this_mv.col, &mbmi->wm_params,
3263 mi_row, mi_col)) {
3264 thismse = compute_motion_cost(xd, cm, ms_params, bsize, &this_mv);
chiyotsai9f664e32020-02-19 14:17:31 -08003265
3266 if (thismse < bestmse) {
3267 best_idx = idx;
3268 best_wm_params = mbmi->wm_params;
3269 best_num_proj_ref = mbmi->num_proj_ref;
3270 bestmse = thismse;
3271 }
3272 }
3273 }
3274 }
3275
3276 if (best_idx == -1) break;
3277
3278 if (best_idx >= 0) {
chiyotsai74da72b2020-04-07 13:45:40 -07003279 best_mv->row += neighbors[best_idx].row;
3280 best_mv->col += neighbors[best_idx].col;
chiyotsai9f664e32020-02-19 14:17:31 -08003281 }
chiyotsai19a58ee2019-03-18 18:01:05 -07003282 }
3283
chiyotsai9f664e32020-02-19 14:17:31 -08003284 mbmi->wm_params = best_wm_params;
3285 mbmi->num_proj_ref = best_num_proj_ref;
3286 return bestmse;
chiyotsai19a58ee2019-03-18 18:01:05 -07003287}
chiyotsai9f664e32020-02-19 14:17:31 -08003288// =============================================================================
3289// Subpixel Motion Search: OBMC
3290// =============================================================================
chiyotsaia7e2cf82020-02-19 16:13:29 -08003291// Estimates the variance of prediction residue
3292static INLINE int estimate_obmc_pref_error(
chiyotsai5aa70752020-03-26 10:13:10 -07003293 const MV *this_mv, const SUBPEL_SEARCH_VAR_PARAMS *var_params,
3294 unsigned int *sse) {
chiyotsaia7e2cf82020-02-19 16:13:29 -08003295 const aom_variance_fn_ptr_t *vfp = var_params->vfp;
chiyotsai9f664e32020-02-19 14:17:31 -08003296
chiyotsai5aa70752020-03-26 10:13:10 -07003297 const MSBuffers *ms_buffers = &var_params->ms_buffers;
3298 const int32_t *src = 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 return vfp->osvf(ref, ref_stride, subpel_x_q3, subpel_y_q3, src, mask, sse);
chiyotsai9f664e32020-02-19 14:17:31 -08003307}
3308
chiyotsaia7e2cf82020-02-19 16:13:29 -08003309// Calculates the variance of prediction residue
3310static int upsampled_obmc_pref_error(MACROBLOCKD *xd, const AV1_COMMON *cm,
chiyotsai5aa70752020-03-26 10:13:10 -07003311 const MV *this_mv,
chiyotsaia7e2cf82020-02-19 16:13:29 -08003312 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
3313 unsigned int *sse) {
3314 const aom_variance_fn_ptr_t *vfp = var_params->vfp;
3315 const SUBPEL_SEARCH_TYPE subpel_search_type = var_params->subpel_search_type;
3316 const int w = var_params->w;
3317 const int h = var_params->h;
chiyotsai9f664e32020-02-19 14:17:31 -08003318
chiyotsai5aa70752020-03-26 10:13:10 -07003319 const MSBuffers *ms_buffers = &var_params->ms_buffers;
3320 const int32_t *wsrc = ms_buffers->wsrc;
3321 const int32_t *mask = ms_buffers->obmc_mask;
3322 const uint8_t *ref = get_buf_from_mv(ms_buffers->ref, *this_mv);
3323 const int ref_stride = ms_buffers->ref->stride;
3324
3325 const int subpel_x_q3 = get_subpel_part(this_mv->col);
3326 const int subpel_y_q3 = get_subpel_part(this_mv->row);
3327
chiyotsai9f664e32020-02-19 14:17:31 -08003328 const int mi_row = xd->mi_row;
3329 const int mi_col = xd->mi_col;
chiyotsaia7e2cf82020-02-19 16:13:29 -08003330
3331 unsigned int besterr;
chiyotsai9f664e32020-02-19 14:17:31 -08003332 DECLARE_ALIGNED(16, uint8_t, pred[2 * MAX_SB_SQUARE]);
chiyotsai9f664e32020-02-19 14:17:31 -08003333 if (is_cur_buf_hbd(xd)) {
3334 uint8_t *pred8 = CONVERT_TO_BYTEPTR(pred);
chiyotsaia7e2cf82020-02-19 16:13:29 -08003335 aom_highbd_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred8, w, h,
3336 subpel_x_q3, subpel_y_q3, ref, ref_stride, xd->bd,
3337 subpel_search_type);
chiyotsai9f664e32020-02-19 14:17:31 -08003338 besterr = vfp->ovf(pred8, w, wsrc, mask, sse);
3339 } else {
chiyotsaia7e2cf82020-02-19 16:13:29 -08003340 aom_upsampled_pred(xd, cm, mi_row, mi_col, this_mv, pred, w, h, subpel_x_q3,
3341 subpel_y_q3, ref, ref_stride, subpel_search_type);
chiyotsai9f664e32020-02-19 14:17:31 -08003342
3343 besterr = vfp->ovf(pred, w, wsrc, mask, sse);
3344 }
chiyotsai9f664e32020-02-19 14:17:31 -08003345
chiyotsai9f664e32020-02-19 14:17:31 -08003346 return besterr;
3347}
chiyotsai9f664e32020-02-19 14:17:31 -08003348
chiyotsaia7e2cf82020-02-19 16:13:29 -08003349static unsigned int setup_obmc_center_error(
chiyotsai5aa70752020-03-26 10:13:10 -07003350 const MV *this_mv, const SUBPEL_SEARCH_VAR_PARAMS *var_params,
chiyotsaia7e2cf82020-02-19 16:13:29 -08003351 const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion) {
chiyotsai5aa70752020-03-26 10:13:10 -07003352 // TODO(chiyotsai@google.com): There might be a bug here where we didn't use
3353 // get_buf_from_mv(ref, *this_mv).
3354 const MSBuffers *ms_buffers = &var_params->ms_buffers;
3355 const int32_t *wsrc = ms_buffers->wsrc;
3356 const int32_t *mask = ms_buffers->obmc_mask;
3357 const uint8_t *ref = ms_buffers->ref->buf;
3358 const int ref_stride = ms_buffers->ref->stride;
chiyotsaia7e2cf82020-02-19 16:13:29 -08003359 unsigned int besterr =
3360 var_params->vfp->ovf(ref, ref_stride, wsrc, mask, sse1);
3361 *distortion = besterr;
3362 besterr += mv_err_cost_(this_mv, mv_cost_params);
3363 return besterr;
3364}
chiyotsai9f664e32020-02-19 14:17:31 -08003365
3366static unsigned int upsampled_setup_obmc_center_error(
chiyotsaia7e2cf82020-02-19 16:13:29 -08003367 MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV *this_mv,
chiyotsai5aa70752020-03-26 10:13:10 -07003368 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
chiyotsaia7e2cf82020-02-19 16:13:29 -08003369 const MV_COST_PARAMS *mv_cost_params, unsigned int *sse1, int *distortion) {
chiyotsai5aa70752020-03-26 10:13:10 -07003370 unsigned int besterr =
3371 upsampled_obmc_pref_error(xd, cm, this_mv, var_params, sse1);
chiyotsai9f664e32020-02-19 14:17:31 -08003372 *distortion = besterr;
chiyotsaia7e2cf82020-02-19 16:13:29 -08003373 besterr += mv_err_cost_(this_mv, mv_cost_params);
chiyotsai9f664e32020-02-19 14:17:31 -08003374 return besterr;
3375}
3376
chiyotsaia7e2cf82020-02-19 16:13:29 -08003377// Estimates the variance of prediction residue
3378// TODO(chiyotsai@google.com): the cost does does not match the cost in
3379// mv_cost_. Investigate this later.
3380static INLINE int estimate_obmc_mvcost(const MV *this_mv,
3381 const MV_COST_PARAMS *mv_cost_params) {
3382 const MV *ref_mv = mv_cost_params->ref_mv;
3383 const int *mvjcost = mv_cost_params->mvjcost;
3384 const int *const *mvcost = mv_cost_params->mvcost;
3385 const int error_per_bit = mv_cost_params->error_per_bit;
3386 const MV_COST_TYPE mv_cost_type = mv_cost_params->mv_cost_type;
3387 const MV diff_mv = { GET_MV_SUBPEL(this_mv->row - ref_mv->row),
3388 GET_MV_SUBPEL(this_mv->col - ref_mv->col) };
3389
3390 switch (mv_cost_type) {
3391 case MV_COST_ENTROPY:
3392 return (unsigned)((mv_cost(&diff_mv, mvjcost,
3393 CONVERT_TO_CONST_MVCOST(mvcost)) *
3394 error_per_bit +
3395 4096) >>
3396 13);
3397 case MV_COST_NONE: return 0;
3398 default:
3399 assert(0 && "L1 norm is not tuned for estimated obmc mvcost");
3400 return 0;
3401 }
3402}
3403
3404// Estimates whether this_mv is better than best_mv. This function incorporates
3405// both prediction error and residue into account.
3406static INLINE unsigned int obmc_check_better_fast(
3407 const MV *this_mv, MV *best_mv, const SubpelMvLimits *mv_limits,
chiyotsai5aa70752020-03-26 10:13:10 -07003408 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
chiyotsaia7e2cf82020-02-19 16:13:29 -08003409 const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
3410 unsigned int *sse1, int *distortion, int *has_better_mv) {
3411 unsigned int cost;
3412 if (av1_is_subpelmv_in_range(mv_limits, *this_mv)) {
3413 unsigned int sse;
chiyotsai5aa70752020-03-26 10:13:10 -07003414 const int thismse = estimate_obmc_pref_error(this_mv, var_params, &sse);
chiyotsaia7e2cf82020-02-19 16:13:29 -08003415
3416 cost = estimate_obmc_mvcost(this_mv, mv_cost_params);
3417 cost += thismse;
3418
3419 if (cost < *besterr) {
3420 *besterr = cost;
3421 *best_mv = *this_mv;
3422 *distortion = thismse;
3423 *sse1 = sse;
3424 *has_better_mv |= 1;
3425 }
3426 } else {
3427 cost = INT_MAX;
3428 }
3429 return cost;
3430}
3431
3432// Estimates whether this_mv is better than best_mv. This function incorporates
3433// both prediction error and residue into account.
3434static INLINE unsigned int obmc_check_better(
3435 MACROBLOCKD *xd, const AV1_COMMON *cm, const MV *this_mv, MV *best_mv,
chiyotsai5aa70752020-03-26 10:13:10 -07003436 const SubpelMvLimits *mv_limits, const SUBPEL_SEARCH_VAR_PARAMS *var_params,
chiyotsaia7e2cf82020-02-19 16:13:29 -08003437 const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
3438 unsigned int *sse1, int *distortion, int *has_better_mv) {
3439 unsigned int cost;
3440 if (av1_is_subpelmv_in_range(mv_limits, *this_mv)) {
3441 unsigned int sse;
chiyotsai5aa70752020-03-26 10:13:10 -07003442 const int thismse =
3443 upsampled_obmc_pref_error(xd, cm, this_mv, var_params, &sse);
chiyotsaia7e2cf82020-02-19 16:13:29 -08003444 cost = mv_err_cost_(this_mv, mv_cost_params);
3445
3446 cost += thismse;
3447
3448 if (cost < *besterr) {
3449 *besterr = cost;
3450 *best_mv = *this_mv;
3451 *distortion = thismse;
3452 *sse1 = sse;
3453 *has_better_mv |= 1;
3454 }
3455 } else {
3456 cost = INT_MAX;
3457 }
3458 return cost;
3459}
3460
chiyotsai981eb5f2020-03-19 14:26:17 -07003461static AOM_FORCE_INLINE MV obmc_first_level_check(
3462 MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV this_mv, MV *best_mv,
chiyotsai5aa70752020-03-26 10:13:10 -07003463 const int hstep, const SubpelMvLimits *mv_limits,
chiyotsai981eb5f2020-03-19 14:26:17 -07003464 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
3465 const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
3466 unsigned int *sse1, int *distortion) {
3467 int dummy = 0;
3468 const MV left_mv = { this_mv.row, this_mv.col - hstep };
3469 const MV right_mv = { this_mv.row, this_mv.col + hstep };
3470 const MV top_mv = { this_mv.row - hstep, this_mv.col };
3471 const MV bottom_mv = { this_mv.row + hstep, this_mv.col };
3472
3473 if (var_params->subpel_search_type != USE_2_TAPS_ORIG) {
chiyotsai5aa70752020-03-26 10:13:10 -07003474 const unsigned int left =
3475 obmc_check_better(xd, cm, &left_mv, best_mv, mv_limits, var_params,
3476 mv_cost_params, besterr, sse1, distortion, &dummy);
3477 const unsigned int right =
3478 obmc_check_better(xd, cm, &right_mv, best_mv, mv_limits, var_params,
3479 mv_cost_params, besterr, sse1, distortion, &dummy);
3480 const unsigned int up =
3481 obmc_check_better(xd, cm, &top_mv, best_mv, mv_limits, var_params,
3482 mv_cost_params, besterr, sse1, distortion, &dummy);
3483 const unsigned int down =
3484 obmc_check_better(xd, cm, &bottom_mv, best_mv, mv_limits, var_params,
3485 mv_cost_params, besterr, sse1, distortion, &dummy);
chiyotsai981eb5f2020-03-19 14:26:17 -07003486
3487 const MV diag_step = get_best_diag_step(hstep, left, right, up, down);
3488 const MV diag_mv = { this_mv.row + diag_step.row,
3489 this_mv.col + diag_step.col };
3490
3491 // Check the diagonal direction with the best mv
chiyotsai5aa70752020-03-26 10:13:10 -07003492 obmc_check_better(xd, cm, &diag_mv, best_mv, mv_limits, var_params,
3493 mv_cost_params, besterr, sse1, distortion, &dummy);
chiyotsai981eb5f2020-03-19 14:26:17 -07003494
3495 return diag_step;
3496 } else {
3497 const unsigned int left = obmc_check_better_fast(
chiyotsai5aa70752020-03-26 10:13:10 -07003498 &left_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr, sse1,
3499 distortion, &dummy);
chiyotsai981eb5f2020-03-19 14:26:17 -07003500 const unsigned int right = obmc_check_better_fast(
chiyotsai5aa70752020-03-26 10:13:10 -07003501 &right_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr,
3502 sse1, distortion, &dummy);
chiyotsai981eb5f2020-03-19 14:26:17 -07003503
3504 const unsigned int up = obmc_check_better_fast(
chiyotsai5aa70752020-03-26 10:13:10 -07003505 &top_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr, sse1,
3506 distortion, &dummy);
chiyotsai981eb5f2020-03-19 14:26:17 -07003507
3508 const unsigned int down = obmc_check_better_fast(
chiyotsai5aa70752020-03-26 10:13:10 -07003509 &bottom_mv, best_mv, mv_limits, var_params, mv_cost_params, besterr,
3510 sse1, distortion, &dummy);
chiyotsai981eb5f2020-03-19 14:26:17 -07003511
3512 const MV diag_step = get_best_diag_step(hstep, left, right, up, down);
3513 const MV diag_mv = { this_mv.row + diag_step.row,
3514 this_mv.col + diag_step.col };
3515
3516 // Check the diagonal direction with the best mv
chiyotsai5aa70752020-03-26 10:13:10 -07003517 obmc_check_better_fast(&diag_mv, best_mv, mv_limits, var_params,
3518 mv_cost_params, besterr, sse1, distortion, &dummy);
chiyotsai981eb5f2020-03-19 14:26:17 -07003519
3520 return diag_step;
3521 }
3522}
3523
chiyotsaia7e2cf82020-02-19 16:13:29 -08003524// A newer version of second level check for obmc that gives better quality.
3525static AOM_FORCE_INLINE void obmc_second_level_check_v2(
chiyotsai981eb5f2020-03-19 14:26:17 -07003526 MACROBLOCKD *xd, const AV1_COMMON *const cm, const MV this_mv, MV diag_step,
chiyotsai5aa70752020-03-26 10:13:10 -07003527 MV *best_mv, const SubpelMvLimits *mv_limits,
chiyotsaia7e2cf82020-02-19 16:13:29 -08003528 const SUBPEL_SEARCH_VAR_PARAMS *var_params,
3529 const MV_COST_PARAMS *mv_cost_params, unsigned int *besterr,
3530 unsigned int *sse1, int *distortion) {
chiyotsai981eb5f2020-03-19 14:26:17 -07003531 assert(best_mv->row == this_mv.row + diag_step.row ||
3532 best_mv->col == this_mv.col + diag_step.col);
3533 if (CHECK_MV_EQUAL(this_mv, *best_mv)) {
3534 return;
3535 } else if (this_mv.row == best_mv->row) {
3536 // Search away from diagonal step since diagonal search did not provide any
3537 // improvement
3538 diag_step.row *= -1;
3539 } else if (this_mv.col == best_mv->col) {
3540 diag_step.col *= -1;
chiyotsaia7e2cf82020-02-19 16:13:29 -08003541 }
3542
chiyotsai981eb5f2020-03-19 14:26:17 -07003543 const MV row_bias_mv = { best_mv->row + diag_step.row, best_mv->col };
3544 const MV col_bias_mv = { best_mv->row, best_mv->col + diag_step.col };
3545 const MV diag_bias_mv = { best_mv->row + diag_step.row,
3546 best_mv->col + diag_step.col };
chiyotsaia7e2cf82020-02-19 16:13:29 -08003547 int has_better_mv = 0;
3548
3549 if (var_params->subpel_search_type != USE_2_TAPS_ORIG) {
chiyotsai5aa70752020-03-26 10:13:10 -07003550 obmc_check_better(xd, cm, &row_bias_mv, best_mv, mv_limits, var_params,
3551 mv_cost_params, besterr, sse1, distortion,
3552 &has_better_mv);
3553 obmc_check_better(xd, cm, &col_bias_mv, best_mv, mv_limits, var_params,
3554 mv_cost_params, besterr, sse1, distortion,
3555 &has_better_mv);
chiyotsaia7e2cf82020-02-19 16:13:29 -08003556
3557 // Do an additional search if the second iteration gives a better mv
3558 if (has_better_mv) {
chiyotsai5aa70752020-03-26 10:13:10 -07003559 obmc_check_better(xd, cm, &diag_bias_mv, best_mv, mv_limits, var_params,
3560 mv_cost_params, besterr, sse1, distortion,
3561 &has_better_mv);
chiyotsaia7e2cf82020-02-19 16:13:29 -08003562 }
3563 } else {
chiyotsai5aa70752020-03-26 10:13:10 -07003564 obmc_check_better_fast(&row_bias_mv, best_mv, mv_limits, var_params,
3565 mv_cost_params, besterr, sse1, distortion,
3566 &has_better_mv);
3567 obmc_check_better_fast(&col_bias_mv, best_mv, mv_limits, var_params,
3568 mv_cost_params, besterr, sse1, distortion,
3569 &has_better_mv);
chiyotsaia7e2cf82020-02-19 16:13:29 -08003570
3571 // Do an additional search if the second iteration gives a better mv
3572 if (has_better_mv) {
chiyotsai5aa70752020-03-26 10:13:10 -07003573 obmc_check_better_fast(&diag_bias_mv, best_mv, mv_limits, var_params,
3574 mv_cost_params, besterr, sse1, distortion,
3575 &has_better_mv);
chiyotsaia7e2cf82020-02-19 16:13:29 -08003576 }
3577 }
3578}
3579
chiyotsai9f664e32020-02-19 14:17:31 -08003580int av1_find_best_obmc_sub_pixel_tree_up(
chiyotsai5aa70752020-03-26 10:13:10 -07003581 MACROBLOCKD *xd, const AV1_COMMON *const cm,
Yaowu Xuf8b0e9b2020-03-31 13:14:38 -07003582 const SUBPEL_MOTION_SEARCH_PARAMS *ms_params, MV start_mv, MV *bestmv,
chiyotsai5aa70752020-03-26 10:13:10 -07003583 int *distortion, unsigned int *sse1, int_mv *last_mv_search_list) {
3584 (void)last_mv_search_list;
chiyotsaia7e2cf82020-02-19 16:13:29 -08003585 const int allow_hp = ms_params->allow_hp;
chiyotsai5aa70752020-03-26 10:13:10 -07003586 const int forced_stop = ms_params->forced_stop;
chiyotsaia7e2cf82020-02-19 16:13:29 -08003587 const int iters_per_step = ms_params->iters_per_step;
chiyotsaia7e2cf82020-02-19 16:13:29 -08003588 const MV_COST_PARAMS *mv_cost_params = &ms_params->mv_cost_params;
3589 const SUBPEL_SEARCH_VAR_PARAMS *var_params = &ms_params->var_params;
chiyotsaia7e2cf82020-02-19 16:13:29 -08003590 const SUBPEL_SEARCH_TYPE subpel_search_type =
3591 ms_params->var_params.subpel_search_type;
chiyotsai5aa70752020-03-26 10:13:10 -07003592 const SubpelMvLimits *mv_limits = &ms_params->mv_limits;
chiyotsai9f664e32020-02-19 14:17:31 -08003593
chiyotsaia7e2cf82020-02-19 16:13:29 -08003594 int hstep = INIT_SUBPEL_STEP_SIZE;
chiyotsai5aa70752020-03-26 10:13:10 -07003595 const int round = AOMMIN(FULL_PEL - forced_stop, 3 - !allow_hp);
chiyotsai9f664e32020-02-19 14:17:31 -08003596
chiyotsaia7e2cf82020-02-19 16:13:29 -08003597 unsigned int besterr = INT_MAX;
chiyotsai5aa70752020-03-26 10:13:10 -07003598 *bestmv = start_mv;
chiyotsaia7e2cf82020-02-19 16:13:29 -08003599
chiyotsai9f664e32020-02-19 14:17:31 -08003600 if (subpel_search_type != USE_2_TAPS_ORIG)
3601 besterr = upsampled_setup_obmc_center_error(
chiyotsai5aa70752020-03-26 10:13:10 -07003602 xd, cm, bestmv, var_params, mv_cost_params, sse1, distortion);
chiyotsai9f664e32020-02-19 14:17:31 -08003603 else
chiyotsai5aa70752020-03-26 10:13:10 -07003604 besterr = setup_obmc_center_error(bestmv, var_params, mv_cost_params, sse1,
3605 distortion);
chiyotsai9f664e32020-02-19 14:17:31 -08003606
chiyotsaia7e2cf82020-02-19 16:13:29 -08003607 for (int iter = 0; iter < round; ++iter) {
chiyotsai981eb5f2020-03-19 14:26:17 -07003608 MV iter_center_mv = *bestmv;
chiyotsai5aa70752020-03-26 10:13:10 -07003609 MV diag_step = obmc_first_level_check(xd, cm, iter_center_mv, bestmv, hstep,
3610 mv_limits, var_params, mv_cost_params,
3611 &besterr, sse1, distortion);
chiyotsaia7e2cf82020-02-19 16:13:29 -08003612
chiyotsai981eb5f2020-03-19 14:26:17 -07003613 if (!CHECK_MV_EQUAL(iter_center_mv, *bestmv) && iters_per_step > 1) {
3614 obmc_second_level_check_v2(xd, cm, iter_center_mv, diag_step, bestmv,
chiyotsai5aa70752020-03-26 10:13:10 -07003615 mv_limits, var_params, mv_cost_params,
chiyotsai981eb5f2020-03-19 14:26:17 -07003616 &besterr, sse1, distortion);
chiyotsai9f664e32020-02-19 14:17:31 -08003617 }
chiyotsai9f664e32020-02-19 14:17:31 -08003618 hstep >>= 1;
chiyotsai9f664e32020-02-19 14:17:31 -08003619 }
3620
chiyotsai9f664e32020-02-19 14:17:31 -08003621 return besterr;
3622}
3623
chiyotsai9f664e32020-02-19 14:17:31 -08003624// =============================================================================
3625// Public cost function: mv_cost + pred error
3626// =============================================================================
chiyotsaic95e3642020-04-10 13:17:06 -07003627int av1_get_mvpred_sse(const MV_COST_PARAMS *mv_cost_params,
3628 const FULLPEL_MV best_mv,
3629 const aom_variance_fn_ptr_t *vfp,
3630 const struct buf_2d *src, const struct buf_2d *pre) {
3631 const MV mv = get_mv_from_fullmv(&best_mv);
chiyotsai9f664e32020-02-19 14:17:31 -08003632 unsigned int sse, var;
3633
chiyotsaic95e3642020-04-10 13:17:06 -07003634 var = vfp->vf(src->buf, src->stride, get_buf_from_fullmv(pre, &best_mv),
3635 pre->stride, &sse);
chiyotsai9f664e32020-02-19 14:17:31 -08003636 (void)var;
3637
chiyotsaic95e3642020-04-10 13:17:06 -07003638 return sse + mv_err_cost_(&mv, mv_cost_params);
chiyotsai9f664e32020-02-19 14:17:31 -08003639}
3640
chiyotsai30a5bdf2020-04-06 14:28:59 -07003641static INLINE int get_mvpred_av_var(const MV_COST_PARAMS *mv_cost_params,
3642 const FULLPEL_MV best_mv,
3643 const uint8_t *second_pred,
3644 const aom_variance_fn_ptr_t *vfp,
3645 const struct buf_2d *src,
3646 const struct buf_2d *pre) {
chiyotsai30a5bdf2020-04-06 14:28:59 -07003647 const MV mv = get_mv_from_fullmv(&best_mv);
chiyotsai9f664e32020-02-19 14:17:31 -08003648 unsigned int unused;
3649
chiyotsaic95e3642020-04-10 13:17:06 -07003650 return vfp->svaf(get_buf_from_fullmv(pre, &best_mv), pre->stride, 0, 0,
3651 src->buf, src->stride, &unused, second_pred) +
chiyotsai30a5bdf2020-04-06 14:28:59 -07003652 mv_err_cost_(&mv, mv_cost_params);
chiyotsai9f664e32020-02-19 14:17:31 -08003653}
3654
chiyotsai30a5bdf2020-04-06 14:28:59 -07003655static INLINE int get_mvpred_mask_var(
3656 const MV_COST_PARAMS *mv_cost_params, const FULLPEL_MV best_mv,
3657 const uint8_t *second_pred, const uint8_t *mask, int mask_stride,
3658 int invert_mask, const aom_variance_fn_ptr_t *vfp, const struct buf_2d *src,
3659 const struct buf_2d *pre) {
chiyotsai30a5bdf2020-04-06 14:28:59 -07003660 const MV mv = get_mv_from_fullmv(&best_mv);
chiyotsai9f664e32020-02-19 14:17:31 -08003661 unsigned int unused;
3662
Jingning Han142ade22020-10-10 00:32:34 -07003663 return vfp->msvf(get_buf_from_fullmv(pre, &best_mv), pre->stride, 0, 0,
3664 src->buf, src->stride, second_pred, mask, mask_stride,
3665 invert_mask, &unused) +
chiyotsai30a5bdf2020-04-06 14:28:59 -07003666 mv_err_cost_(&mv, mv_cost_params);
3667}
3668
3669int av1_get_mvpred_compound_var(const MV_COST_PARAMS *mv_cost_params,
3670 const FULLPEL_MV best_mv,
3671 const uint8_t *second_pred, const uint8_t *mask,
3672 int mask_stride, int invert_mask,
3673 const aom_variance_fn_ptr_t *vfp,
3674 const struct buf_2d *src,
3675 const struct buf_2d *pre) {
3676 if (mask) {
3677 return get_mvpred_mask_var(mv_cost_params, best_mv, second_pred, mask,
3678 mask_stride, invert_mask, vfp, src, pre);
3679 } else {
3680 return get_mvpred_av_var(mv_cost_params, best_mv, second_pred, vfp, src,
3681 pre);
3682 }
chiyotsai9f664e32020-02-19 14:17:31 -08003683}