Coverage Report

Created: 2022-08-24 06:17

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