Coverage Report

Created: 2022-08-24 06:17

/src/aom/av1/encoder/motion_search_facade.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2020, 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 "av1/common/reconinter.h"
13
14
#include "av1/encoder/encodemv.h"
15
#include "av1/encoder/encoder.h"
16
#include "av1/encoder/interp_search.h"
17
#include "av1/encoder/mcomp.h"
18
#include "av1/encoder/motion_search_facade.h"
19
#include "av1/encoder/partition_strategy.h"
20
#include "av1/encoder/reconinter_enc.h"
21
#include "av1/encoder/tpl_model.h"
22
#include "av1/encoder/tx_search.h"
23
24
0
#define RIGHT_SHIFT_MV(x) (((x) + 3 + ((x) >= 0)) >> 3)
25
26
typedef struct {
27
  FULLPEL_MV fmv;
28
  int weight;
29
} cand_mv_t;
30
31
0
static int compare_weight(const void *a, const void *b) {
32
0
  const int diff = ((cand_mv_t *)a)->weight - ((cand_mv_t *)b)->weight;
33
0
  if (diff < 0)
34
0
    return 1;
35
0
  else if (diff > 0)
36
0
    return -1;
37
0
  return 0;
38
0
}
39
40
// Allow more mesh searches for screen content type on the ARF.
41
0
static int use_fine_search_interval(const AV1_COMP *const cpi) {
42
0
  return cpi->is_screen_content_type &&
43
0
         cpi->ppi->gf_group.update_type[cpi->gf_frame_index] == ARF_UPDATE &&
44
0
         cpi->oxcf.speed <= 2;
45
0
}
46
47
// Iterate through the tpl and collect the mvs to be used as candidates
48
static INLINE void get_mv_candidate_from_tpl(const AV1_COMP *const cpi,
49
                                             const MACROBLOCK *x,
50
                                             BLOCK_SIZE bsize, int ref,
51
                                             cand_mv_t *cand, int *cand_count,
52
0
                                             int *total_cand_weight) {
53
0
  const SuperBlockEnc *sb_enc = &x->sb_enc;
54
0
  if (!sb_enc->tpl_data_count) {
55
0
    return;
56
0
  }
57
58
0
  const AV1_COMMON *cm = &cpi->common;
59
0
  const MACROBLOCKD *xd = &x->e_mbd;
60
0
  const int mi_row = xd->mi_row;
61
0
  const int mi_col = xd->mi_col;
62
63
0
  const BLOCK_SIZE tpl_bsize =
64
0
      convert_length_to_bsize(cpi->ppi->tpl_data.tpl_bsize_1d);
65
0
  const int tplw = mi_size_wide[tpl_bsize];
66
0
  const int tplh = mi_size_high[tpl_bsize];
67
0
  const int nw = mi_size_wide[bsize] / tplw;
68
0
  const int nh = mi_size_high[bsize] / tplh;
69
70
0
  if (nw >= 1 && nh >= 1) {
71
0
    const int of_h = mi_row % mi_size_high[cm->seq_params->sb_size];
72
0
    const int of_w = mi_col % mi_size_wide[cm->seq_params->sb_size];
73
0
    const int start = of_h / tplh * sb_enc->tpl_stride + of_w / tplw;
74
0
    int valid = 1;
75
76
    // Assign large weight to start_mv, so it is always tested.
77
0
    cand[0].weight = nw * nh;
78
79
0
    for (int k = 0; k < nh; k++) {
80
0
      for (int l = 0; l < nw; l++) {
81
0
        const int_mv mv =
82
0
            sb_enc
83
0
                ->tpl_mv[start + k * sb_enc->tpl_stride + l][ref - LAST_FRAME];
84
0
        if (mv.as_int == INVALID_MV) {
85
0
          valid = 0;
86
0
          break;
87
0
        }
88
89
0
        const FULLPEL_MV fmv = { GET_MV_RAWPEL(mv.as_mv.row),
90
0
                                 GET_MV_RAWPEL(mv.as_mv.col) };
91
0
        int unique = 1;
92
0
        for (int m = 0; m < *cand_count; m++) {
93
0
          if (RIGHT_SHIFT_MV(fmv.row) == RIGHT_SHIFT_MV(cand[m].fmv.row) &&
94
0
              RIGHT_SHIFT_MV(fmv.col) == RIGHT_SHIFT_MV(cand[m].fmv.col)) {
95
0
            unique = 0;
96
0
            cand[m].weight++;
97
0
            break;
98
0
          }
99
0
        }
100
101
0
        if (unique) {
102
0
          cand[*cand_count].fmv = fmv;
103
0
          cand[*cand_count].weight = 1;
104
0
          (*cand_count)++;
105
0
        }
106
0
      }
107
0
      if (!valid) break;
108
0
    }
109
110
0
    if (valid) {
111
0
      *total_cand_weight = 2 * nh * nw;
112
0
      if (*cand_count > 2)
113
0
        qsort(cand, *cand_count, sizeof(cand[0]), &compare_weight);
114
0
    }
115
0
  }
116
0
}
117
118
void av1_single_motion_search(const AV1_COMP *const cpi, MACROBLOCK *x,
119
                              BLOCK_SIZE bsize, int ref_idx, int *rate_mv,
120
                              int search_range, inter_mode_info *mode_info,
121
                              int_mv *best_mv,
122
0
                              struct HandleInterModeArgs *const args) {
123
0
  MACROBLOCKD *xd = &x->e_mbd;
124
0
  const AV1_COMMON *cm = &cpi->common;
125
0
  const MotionVectorSearchParams *mv_search_params = &cpi->mv_search_params;
126
0
  const int num_planes = av1_num_planes(cm);
127
0
  MB_MODE_INFO *mbmi = xd->mi[0];
128
0
  struct buf_2d backup_yv12[MAX_MB_PLANE] = { { 0, 0, 0, 0, 0 } };
129
0
  int bestsme = INT_MAX;
130
0
  const int ref = mbmi->ref_frame[ref_idx];
131
0
  const YV12_BUFFER_CONFIG *scaled_ref_frame =
132
0
      av1_get_scaled_ref_frame(cpi, ref);
133
0
  const int mi_row = xd->mi_row;
134
0
  const int mi_col = xd->mi_col;
135
0
  const MvCosts *mv_costs = x->mv_costs;
136
137
0
  if (scaled_ref_frame) {
138
    // Swap out the reference frame for a version that's been scaled to
139
    // match the resolution of the current frame, allowing the existing
140
    // full-pixel motion search code to be used without additional
141
    // modifications.
142
0
    for (int i = 0; i < num_planes; i++) {
143
0
      backup_yv12[i] = xd->plane[i].pre[ref_idx];
144
0
    }
145
0
    av1_setup_pre_planes(xd, ref_idx, scaled_ref_frame, mi_row, mi_col, NULL,
146
0
                         num_planes);
147
0
  }
148
149
  // Work out the size of the first step in the mv step search.
150
  // 0 here is maximum length first step. 1 is AOMMAX >> 1 etc.
151
0
  int step_param;
152
0
  if (cpi->sf.mv_sf.auto_mv_step_size && cm->show_frame) {
153
    // Take the weighted average of the step_params based on the last frame's
154
    // max mv magnitude and that based on the best ref mvs of the current
155
    // block for the given reference.
156
0
    step_param = (av1_init_search_range(x->max_mv_context[ref]) +
157
0
                  mv_search_params->mv_step_param) /
158
0
                 2;
159
0
  } else {
160
0
    step_param = mv_search_params->mv_step_param;
161
0
  }
162
163
0
  const MV ref_mv = av1_get_ref_mv(x, ref_idx).as_mv;
164
0
  FULLPEL_MV start_mv;
165
0
  if (mbmi->motion_mode != SIMPLE_TRANSLATION)
166
0
    start_mv = get_fullmv_from_mv(&mbmi->mv[0].as_mv);
167
0
  else
168
0
    start_mv = get_fullmv_from_mv(&ref_mv);
169
170
  // cand stores start_mv and all possible MVs in a SB.
171
0
  cand_mv_t cand[MAX_TPL_BLK_IN_SB * MAX_TPL_BLK_IN_SB + 1] = { { { 0, 0 },
172
0
                                                                  0 } };
173
0
  cand[0].fmv = start_mv;
174
0
  int cnt = 1;
175
0
  int total_weight = 0;
176
177
0
  if (!cpi->sf.mv_sf.full_pixel_search_level &&
178
0
      mbmi->motion_mode == SIMPLE_TRANSLATION) {
179
0
    get_mv_candidate_from_tpl(cpi, x, bsize, ref, cand, &cnt, &total_weight);
180
0
  }
181
182
  // Further reduce the search range.
183
0
  if (search_range < INT_MAX) {
184
0
    const search_site_config *search_site_cfg =
185
0
        &mv_search_params
186
0
             ->search_site_cfg[SS_CFG_SRC][cpi->sf.mv_sf.search_method];
187
    // Max step_param is search_site_cfg->num_search_steps.
188
0
    if (search_range < 1) {
189
0
      step_param = search_site_cfg->num_search_steps;
190
0
    } else {
191
0
      while (search_site_cfg->radius[search_site_cfg->num_search_steps -
192
0
                                     step_param - 1] > (search_range << 1) &&
193
0
             search_site_cfg->num_search_steps - step_param - 1 > 0)
194
0
        step_param++;
195
0
    }
196
0
  }
197
198
0
  int cost_list[5];
199
0
  int_mv second_best_mv;
200
0
  best_mv->as_int = second_best_mv.as_int = INVALID_MV;
201
202
  // Allow more mesh searches for screen content type on the ARF.
203
0
  const int fine_search_interval = use_fine_search_interval(cpi);
204
0
  const search_site_config *src_search_sites =
205
0
      mv_search_params->search_site_cfg[SS_CFG_SRC];
206
0
  FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
207
0
  av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize, &ref_mv,
208
0
                                     src_search_sites, fine_search_interval);
209
210
0
  switch (mbmi->motion_mode) {
211
0
    case SIMPLE_TRANSLATION: {
212
      // Perform a search with the top 2 candidates
213
0
      int sum_weight = 0;
214
0
      for (int m = 0; m < AOMMIN(2, cnt); m++) {
215
0
        FULLPEL_MV smv = cand[m].fmv;
216
0
        FULLPEL_MV this_best_mv, this_second_best_mv;
217
218
0
        int thissme = av1_full_pixel_search(
219
0
            smv, &full_ms_params, step_param, cond_cost_list(cpi, cost_list),
220
0
            &this_best_mv, &this_second_best_mv);
221
222
0
        if (thissme < bestsme) {
223
0
          bestsme = thissme;
224
0
          best_mv->as_fullmv = this_best_mv;
225
0
          second_best_mv.as_fullmv = this_second_best_mv;
226
0
        }
227
228
0
        sum_weight += cand[m].weight;
229
0
        if (4 * sum_weight > 3 * total_weight) break;
230
0
      }
231
0
    } break;
232
0
    case OBMC_CAUSAL:
233
0
      bestsme = av1_obmc_full_pixel_search(start_mv, &full_ms_params,
234
0
                                           step_param, &best_mv->as_fullmv);
235
0
      break;
236
0
    default: assert(0 && "Invalid motion mode!\n");
237
0
  }
238
239
0
  if (scaled_ref_frame) {
240
    // Swap back the original buffers for subpel motion search.
241
0
    for (int i = 0; i < num_planes; i++) {
242
0
      xd->plane[i].pre[ref_idx] = backup_yv12[i];
243
0
    }
244
0
  }
245
246
  // Terminate search with the current ref_idx based on fullpel mv, rate cost,
247
  // and other know cost.
248
0
  if (cpi->sf.inter_sf.skip_newmv_in_drl >= 2 &&
249
0
      mbmi->motion_mode == SIMPLE_TRANSLATION &&
250
0
      best_mv->as_int != INVALID_MV) {
251
0
    int_mv this_mv;
252
0
    this_mv.as_mv = get_mv_from_fullmv(&best_mv->as_fullmv);
253
0
    const int ref_mv_idx = mbmi->ref_mv_idx;
254
0
    const int this_mv_rate =
255
0
        av1_mv_bit_cost(&this_mv.as_mv, &ref_mv, mv_costs->nmv_joint_cost,
256
0
                        mv_costs->mv_cost_stack, MV_COST_WEIGHT);
257
0
    mode_info[ref_mv_idx].full_search_mv.as_int = this_mv.as_int;
258
0
    mode_info[ref_mv_idx].full_mv_rate = this_mv_rate;
259
0
    mode_info[ref_mv_idx].full_mv_bestsme = bestsme;
260
261
0
    for (int prev_ref_idx = 0; prev_ref_idx < ref_mv_idx; ++prev_ref_idx) {
262
      // Check if the motion search result same as previous results
263
0
      if (this_mv.as_int == mode_info[prev_ref_idx].full_search_mv.as_int) {
264
        // Compare the rate cost
265
0
        const int prev_rate_cost = mode_info[prev_ref_idx].full_mv_rate +
266
0
                                   mode_info[prev_ref_idx].drl_cost;
267
0
        const int this_rate_cost =
268
0
            this_mv_rate + mode_info[ref_mv_idx].drl_cost;
269
270
0
        if (prev_rate_cost <= this_rate_cost) {
271
          // If the current rate_cost is worse than the previous rate_cost, then
272
          // we terminate the search. Since av1_single_motion_search is only
273
          // called by handle_new_mv in SIMPLE_TRANSLATION mode, we set the
274
          // best_mv to INVALID mv to signal that we wish to terminate search
275
          // for the current mode.
276
0
          best_mv->as_int = INVALID_MV;
277
0
          return;
278
0
        }
279
0
      }
280
281
      // Terminate the evaluation of current ref_mv_idx based on bestsme and
282
      // drl_cost.
283
0
      const int psme = mode_info[prev_ref_idx].full_mv_bestsme;
284
0
      if (psme == INT_MAX) continue;
285
0
      const int thr =
286
0
          cpi->sf.inter_sf.skip_newmv_in_drl == 3 ? (psme + (psme >> 2)) : psme;
287
0
      if (cpi->sf.inter_sf.skip_newmv_in_drl >= 3 &&
288
0
          mode_info[ref_mv_idx].full_mv_bestsme > thr &&
289
0
          mode_info[prev_ref_idx].drl_cost < mode_info[ref_mv_idx].drl_cost) {
290
0
        best_mv->as_int = INVALID_MV;
291
0
        return;
292
0
      }
293
0
    }
294
0
  }
295
296
0
  if (cpi->common.features.cur_frame_force_integer_mv) {
297
0
    convert_fullmv_to_mv(best_mv);
298
0
  }
299
300
0
  const int use_fractional_mv =
301
0
      bestsme < INT_MAX && cpi->common.features.cur_frame_force_integer_mv == 0;
302
0
  int best_mv_rate = 0;
303
0
  int mv_rate_calculated = 0;
304
0
  if (use_fractional_mv) {
305
0
    int_mv fractional_ms_list[3];
306
0
    av1_set_fractional_mv(fractional_ms_list);
307
0
    int dis; /* TODO: use dis in distortion calculation later. */
308
309
0
    SUBPEL_MOTION_SEARCH_PARAMS ms_params;
310
0
    av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv,
311
0
                                      cost_list);
312
0
    MV subpel_start_mv = get_mv_from_fullmv(&best_mv->as_fullmv);
313
314
0
    switch (mbmi->motion_mode) {
315
0
      case SIMPLE_TRANSLATION:
316
0
        if (cpi->sf.mv_sf.use_accurate_subpel_search) {
317
0
          const int try_second = second_best_mv.as_int != INVALID_MV &&
318
0
                                 second_best_mv.as_int != best_mv->as_int &&
319
0
                                 (cpi->sf.mv_sf.disable_second_mv <= 1);
320
0
          const int best_mv_var = mv_search_params->find_fractional_mv_step(
321
0
              xd, cm, &ms_params, subpel_start_mv, &best_mv->as_mv, &dis,
322
0
              &x->pred_sse[ref], fractional_ms_list);
323
324
0
          if (try_second) {
325
0
            struct macroblockd_plane *p = xd->plane;
326
0
            const BUFFER_SET orig_dst = {
327
0
              { p[0].dst.buf, p[1].dst.buf, p[2].dst.buf },
328
0
              { p[0].dst.stride, p[1].dst.stride, p[2].dst.stride },
329
0
            };
330
0
            int64_t rd = INT64_MAX;
331
0
            if (!cpi->sf.mv_sf.disable_second_mv) {
332
              // Calculate actual rd cost.
333
0
              mbmi->mv[0].as_mv = best_mv->as_mv;
334
0
              av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, &orig_dst,
335
0
                                            bsize, 0, 0);
336
0
              av1_subtract_plane(x, bsize, 0);
337
0
              RD_STATS this_rd_stats;
338
0
              av1_init_rd_stats(&this_rd_stats);
339
0
              av1_estimate_txfm_yrd(cpi, x, &this_rd_stats, INT64_MAX, bsize,
340
0
                                    max_txsize_rect_lookup[bsize]);
341
0
              int this_mv_rate = av1_mv_bit_cost(
342
0
                  &best_mv->as_mv, &ref_mv, mv_costs->nmv_joint_cost,
343
0
                  mv_costs->mv_cost_stack, MV_COST_WEIGHT);
344
0
              rd = RDCOST(x->rdmult, this_mv_rate + this_rd_stats.rate,
345
0
                          this_rd_stats.dist);
346
0
            }
347
348
0
            MV this_best_mv;
349
0
            subpel_start_mv = get_mv_from_fullmv(&second_best_mv.as_fullmv);
350
0
            if (av1_is_subpelmv_in_range(&ms_params.mv_limits,
351
0
                                         subpel_start_mv)) {
352
0
              unsigned int sse;
353
0
              const int this_var = mv_search_params->find_fractional_mv_step(
354
0
                  xd, cm, &ms_params, subpel_start_mv, &this_best_mv, &dis,
355
0
                  &sse, fractional_ms_list);
356
357
0
              if (!cpi->sf.mv_sf.disable_second_mv) {
358
                // If cpi->sf.mv_sf.disable_second_mv is 0, use actual rd cost
359
                // to choose the better MV.
360
0
                mbmi->mv[0].as_mv = this_best_mv;
361
0
                av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, &orig_dst,
362
0
                                              bsize, 0, 0);
363
0
                av1_subtract_plane(x, bsize, 0);
364
0
                RD_STATS tmp_rd_stats;
365
0
                av1_init_rd_stats(&tmp_rd_stats);
366
0
                av1_estimate_txfm_yrd(cpi, x, &tmp_rd_stats, INT64_MAX, bsize,
367
0
                                      max_txsize_rect_lookup[bsize]);
368
0
                int tmp_mv_rate = av1_mv_bit_cost(
369
0
                    &this_best_mv, &ref_mv, mv_costs->nmv_joint_cost,
370
0
                    mv_costs->mv_cost_stack, MV_COST_WEIGHT);
371
0
                int64_t tmp_rd =
372
0
                    RDCOST(x->rdmult, tmp_rd_stats.rate + tmp_mv_rate,
373
0
                           tmp_rd_stats.dist);
374
0
                if (tmp_rd < rd) {
375
0
                  best_mv->as_mv = this_best_mv;
376
0
                  x->pred_sse[ref] = sse;
377
0
                }
378
0
              } else {
379
                // If cpi->sf.mv_sf.disable_second_mv = 1, use var to decide the
380
                // best MV.
381
0
                if (this_var < best_mv_var) {
382
0
                  best_mv->as_mv = this_best_mv;
383
0
                  x->pred_sse[ref] = sse;
384
0
                }
385
0
              }
386
0
            }
387
0
          }
388
0
        } else {
389
0
          mv_search_params->find_fractional_mv_step(
390
0
              xd, cm, &ms_params, subpel_start_mv, &best_mv->as_mv, &dis,
391
0
              &x->pred_sse[ref], NULL);
392
0
        }
393
0
        break;
394
0
      case OBMC_CAUSAL:
395
0
        av1_find_best_obmc_sub_pixel_tree_up(xd, cm, &ms_params,
396
0
                                             subpel_start_mv, &best_mv->as_mv,
397
0
                                             &dis, &x->pred_sse[ref], NULL);
398
0
        break;
399
0
      default: assert(0 && "Invalid motion mode!\n");
400
0
    }
401
402
    // Terminate search with the current ref_idx based on subpel mv and rate
403
    // cost.
404
0
    if (cpi->sf.inter_sf.skip_newmv_in_drl >= 1 && args != NULL &&
405
0
        mbmi->motion_mode == SIMPLE_TRANSLATION &&
406
0
        best_mv->as_int != INVALID_MV) {
407
0
      const int ref_mv_idx = mbmi->ref_mv_idx;
408
0
      best_mv_rate =
409
0
          av1_mv_bit_cost(&best_mv->as_mv, &ref_mv, mv_costs->nmv_joint_cost,
410
0
                          mv_costs->mv_cost_stack, MV_COST_WEIGHT);
411
0
      mv_rate_calculated = 1;
412
413
0
      for (int prev_ref_idx = 0; prev_ref_idx < ref_mv_idx; ++prev_ref_idx) {
414
0
        if (!args->single_newmv_valid[prev_ref_idx][ref]) continue;
415
        // Check if the motion vectors are the same.
416
0
        if (best_mv->as_int == args->single_newmv[prev_ref_idx][ref].as_int) {
417
          // Skip this evaluation if the previous one is skipped.
418
0
          if (mode_info[prev_ref_idx].skip) {
419
0
            mode_info[ref_mv_idx].skip = 1;
420
0
            break;
421
0
          }
422
          // Compare the rate cost that we current know.
423
0
          const int prev_rate_cost =
424
0
              args->single_newmv_rate[prev_ref_idx][ref] +
425
0
              mode_info[prev_ref_idx].drl_cost;
426
0
          const int this_rate_cost =
427
0
              best_mv_rate + mode_info[ref_mv_idx].drl_cost;
428
429
0
          if (prev_rate_cost <= this_rate_cost) {
430
            // If the current rate_cost is worse than the previous rate_cost,
431
            // then we terminate the search for this ref_mv_idx.
432
0
            mode_info[ref_mv_idx].skip = 1;
433
0
            break;
434
0
          }
435
0
        }
436
0
      }
437
0
    }
438
0
  }
439
440
0
  if (mv_rate_calculated) {
441
0
    *rate_mv = best_mv_rate;
442
0
  } else {
443
0
    *rate_mv =
444
0
        av1_mv_bit_cost(&best_mv->as_mv, &ref_mv, mv_costs->nmv_joint_cost,
445
0
                        mv_costs->mv_cost_stack, MV_COST_WEIGHT);
446
0
  }
447
0
}
448
449
int av1_joint_motion_search(const AV1_COMP *cpi, MACROBLOCK *x,
450
                            BLOCK_SIZE bsize, int_mv *cur_mv,
451
                            const uint8_t *mask, int mask_stride, int *rate_mv,
452
0
                            int allow_second_mv) {
453
0
  const AV1_COMMON *const cm = &cpi->common;
454
0
  const int num_planes = av1_num_planes(cm);
455
0
  const int pw = block_size_wide[bsize];
456
0
  const int ph = block_size_high[bsize];
457
0
  const int plane = 0;
458
0
  MACROBLOCKD *xd = &x->e_mbd;
459
0
  MB_MODE_INFO *mbmi = xd->mi[0];
460
  // This function should only ever be called for compound modes
461
0
  assert(has_second_ref(mbmi));
462
0
  const int_mv init_mv[2] = { cur_mv[0], cur_mv[1] };
463
0
  const int refs[2] = { mbmi->ref_frame[0], mbmi->ref_frame[1] };
464
0
  const MvCosts *mv_costs = x->mv_costs;
465
0
  int_mv ref_mv[2];
466
0
  int ite, ref;
467
468
  // Get the prediction block from the 'other' reference frame.
469
0
  const int_interpfilters interp_filters =
470
0
      av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
471
472
0
  InterPredParams inter_pred_params;
473
0
  const int mi_row = xd->mi_row;
474
0
  const int mi_col = xd->mi_col;
475
476
  // Do joint motion search in compound mode to get more accurate mv.
477
0
  struct buf_2d backup_yv12[2][MAX_MB_PLANE];
478
0
  int last_besterr[2] = { INT_MAX, INT_MAX };
479
0
  const YV12_BUFFER_CONFIG *const scaled_ref_frame[2] = {
480
0
    av1_get_scaled_ref_frame(cpi, refs[0]),
481
0
    av1_get_scaled_ref_frame(cpi, refs[1])
482
0
  };
483
484
  // Prediction buffer from second frame.
485
0
  DECLARE_ALIGNED(16, uint8_t, second_pred16[MAX_SB_SQUARE * sizeof(uint16_t)]);
486
0
  uint8_t *second_pred = get_buf_by_bd(xd, second_pred16);
487
488
0
  int_mv best_mv, second_best_mv;
489
490
  // Allow joint search multiple times iteratively for each reference frame
491
  // and break out of the search loop if it couldn't find a better mv.
492
0
  for (ite = 0; ite < 4; ite++) {
493
0
    struct buf_2d ref_yv12[2];
494
0
    int bestsme = INT_MAX;
495
0
    int id = ite % 2;  // Even iterations search in the first reference frame,
496
                       // odd iterations search in the second. The predictor
497
                       // found for the 'other' reference frame is factored in.
498
0
    if (ite >= 2 && cur_mv[!id].as_int == init_mv[!id].as_int) {
499
0
      if (cur_mv[id].as_int == init_mv[id].as_int) {
500
0
        break;
501
0
      } else {
502
0
        int_mv cur_int_mv, init_int_mv;
503
0
        cur_int_mv.as_mv.col = cur_mv[id].as_mv.col >> 3;
504
0
        cur_int_mv.as_mv.row = cur_mv[id].as_mv.row >> 3;
505
0
        init_int_mv.as_mv.row = init_mv[id].as_mv.row >> 3;
506
0
        init_int_mv.as_mv.col = init_mv[id].as_mv.col >> 3;
507
0
        if (cur_int_mv.as_int == init_int_mv.as_int) {
508
0
          break;
509
0
        }
510
0
      }
511
0
    }
512
0
    for (ref = 0; ref < 2; ++ref) {
513
0
      ref_mv[ref] = av1_get_ref_mv(x, ref);
514
      // Swap out the reference frame for a version that's been scaled to
515
      // match the resolution of the current frame, allowing the existing
516
      // motion search code to be used without additional modifications.
517
0
      if (scaled_ref_frame[ref]) {
518
0
        int i;
519
0
        for (i = 0; i < num_planes; i++)
520
0
          backup_yv12[ref][i] = xd->plane[i].pre[ref];
521
0
        av1_setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col,
522
0
                             NULL, num_planes);
523
0
      }
524
0
    }
525
526
0
    assert(IMPLIES(scaled_ref_frame[0] != NULL,
527
0
                   cm->width == scaled_ref_frame[0]->y_crop_width &&
528
0
                       cm->height == scaled_ref_frame[0]->y_crop_height));
529
0
    assert(IMPLIES(scaled_ref_frame[1] != NULL,
530
0
                   cm->width == scaled_ref_frame[1]->y_crop_width &&
531
0
                       cm->height == scaled_ref_frame[1]->y_crop_height));
532
533
    // Initialize based on (possibly scaled) prediction buffers.
534
0
    ref_yv12[0] = xd->plane[plane].pre[0];
535
0
    ref_yv12[1] = xd->plane[plane].pre[1];
536
537
0
    av1_init_inter_params(&inter_pred_params, pw, ph, mi_row * MI_SIZE,
538
0
                          mi_col * MI_SIZE, 0, 0, xd->bd, is_cur_buf_hbd(xd), 0,
539
0
                          &cm->sf_identity, &ref_yv12[!id], interp_filters);
540
0
    inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
541
542
    // Since we have scaled the reference frames to match the size of the
543
    // current frame we must use a unit scaling factor during mode selection.
544
0
    av1_enc_build_one_inter_predictor(second_pred, pw, &cur_mv[!id].as_mv,
545
0
                                      &inter_pred_params);
546
547
    // Do full-pixel compound motion search on the current reference frame.
548
0
    if (id) xd->plane[plane].pre[0] = ref_yv12[id];
549
550
    // Make motion search params
551
0
    FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
552
0
    const search_site_config *src_search_sites =
553
0
        cpi->mv_search_params.search_site_cfg[SS_CFG_SRC];
554
0
    av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize,
555
0
                                       &ref_mv[id].as_mv, src_search_sites,
556
0
                                       /*fine_search_interval=*/0);
557
558
0
    av1_set_ms_compound_refs(&full_ms_params.ms_buffers, second_pred, mask,
559
0
                             mask_stride, id);
560
561
    // Use the mv result from the single mode as mv predictor.
562
0
    const FULLPEL_MV start_fullmv = get_fullmv_from_mv(&cur_mv[id].as_mv);
563
564
    // Small-range full-pixel motion search.
565
0
    if (!cpi->sf.mv_sf.disable_extensive_joint_motion_search &&
566
0
        mbmi->interinter_comp.type != COMPOUND_WEDGE) {
567
0
      bestsme =
568
0
          av1_full_pixel_search(start_fullmv, &full_ms_params, 5, NULL,
569
0
                                &best_mv.as_fullmv, &second_best_mv.as_fullmv);
570
0
    } else {
571
0
      bestsme = av1_refining_search_8p_c(&full_ms_params, start_fullmv,
572
0
                                         &best_mv.as_fullmv);
573
0
      second_best_mv = best_mv;
574
0
    }
575
576
0
    const int try_second = second_best_mv.as_int != INVALID_MV &&
577
0
                           second_best_mv.as_int != best_mv.as_int &&
578
0
                           allow_second_mv;
579
580
    // Restore the pointer to the first (possibly scaled) prediction buffer.
581
0
    if (id) xd->plane[plane].pre[0] = ref_yv12[0];
582
583
0
    for (ref = 0; ref < 2; ++ref) {
584
0
      if (scaled_ref_frame[ref]) {
585
        // Swap back the original buffers for subpel motion search.
586
0
        for (int i = 0; i < num_planes; i++) {
587
0
          xd->plane[i].pre[ref] = backup_yv12[ref][i];
588
0
        }
589
        // Re-initialize based on unscaled prediction buffers.
590
0
        ref_yv12[ref] = xd->plane[plane].pre[ref];
591
0
      }
592
0
    }
593
594
    // Do sub-pixel compound motion search on the current reference frame.
595
0
    if (id) xd->plane[plane].pre[0] = ref_yv12[id];
596
597
0
    if (cpi->common.features.cur_frame_force_integer_mv) {
598
0
      convert_fullmv_to_mv(&best_mv);
599
0
    }
600
0
    if (bestsme < INT_MAX &&
601
0
        cpi->common.features.cur_frame_force_integer_mv == 0) {
602
0
      int dis; /* TODO: use dis in distortion calculation later. */
603
0
      unsigned int sse;
604
0
      SUBPEL_MOTION_SEARCH_PARAMS ms_params;
605
0
      av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize,
606
0
                                        &ref_mv[id].as_mv, NULL);
607
0
      av1_set_ms_compound_refs(&ms_params.var_params.ms_buffers, second_pred,
608
0
                               mask, mask_stride, id);
609
0
      ms_params.forced_stop = EIGHTH_PEL;
610
0
      MV start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
611
0
      bestsme = cpi->mv_search_params.find_fractional_mv_step(
612
0
          xd, cm, &ms_params, start_mv, &best_mv.as_mv, &dis, &sse, NULL);
613
614
0
      if (try_second) {
615
0
        MV this_best_mv;
616
0
        MV subpel_start_mv = get_mv_from_fullmv(&second_best_mv.as_fullmv);
617
0
        if (av1_is_subpelmv_in_range(&ms_params.mv_limits, subpel_start_mv)) {
618
0
          const int thissme = cpi->mv_search_params.find_fractional_mv_step(
619
0
              xd, cm, &ms_params, subpel_start_mv, &this_best_mv, &dis, &sse,
620
0
              NULL);
621
0
          if (thissme < bestsme) {
622
0
            best_mv.as_mv = this_best_mv;
623
0
            bestsme = thissme;
624
0
          }
625
0
        }
626
0
      }
627
0
    }
628
629
    // Restore the pointer to the first prediction buffer.
630
0
    if (id) xd->plane[plane].pre[0] = ref_yv12[0];
631
0
    if (bestsme < last_besterr[id]) {
632
0
      cur_mv[id] = best_mv;
633
0
      last_besterr[id] = bestsme;
634
0
    } else {
635
0
      break;
636
0
    }
637
0
  }
638
639
0
  *rate_mv = 0;
640
641
0
  for (ref = 0; ref < 2; ++ref) {
642
0
    const int_mv curr_ref_mv = av1_get_ref_mv(x, ref);
643
0
    *rate_mv += av1_mv_bit_cost(&cur_mv[ref].as_mv, &curr_ref_mv.as_mv,
644
0
                                mv_costs->nmv_joint_cost,
645
0
                                mv_costs->mv_cost_stack, MV_COST_WEIGHT);
646
0
  }
647
648
0
  return AOMMIN(last_besterr[0], last_besterr[1]);
649
0
}
650
651
// Search for the best mv for one component of a compound,
652
// given that the other component is fixed.
653
int av1_compound_single_motion_search(const AV1_COMP *cpi, MACROBLOCK *x,
654
                                      BLOCK_SIZE bsize, MV *this_mv,
655
                                      const uint8_t *second_pred,
656
                                      const uint8_t *mask, int mask_stride,
657
0
                                      int *rate_mv, int ref_idx) {
658
0
  const AV1_COMMON *const cm = &cpi->common;
659
0
  const int num_planes = av1_num_planes(cm);
660
0
  MACROBLOCKD *xd = &x->e_mbd;
661
0
  MB_MODE_INFO *mbmi = xd->mi[0];
662
0
  const int ref = mbmi->ref_frame[ref_idx];
663
0
  const int_mv ref_mv = av1_get_ref_mv(x, ref_idx);
664
0
  struct macroblockd_plane *const pd = &xd->plane[0];
665
0
  const MvCosts *mv_costs = x->mv_costs;
666
667
0
  struct buf_2d backup_yv12[MAX_MB_PLANE];
668
0
  const YV12_BUFFER_CONFIG *const scaled_ref_frame =
669
0
      av1_get_scaled_ref_frame(cpi, ref);
670
671
  // Check that this is either an interinter or an interintra block
672
0
  assert(has_second_ref(mbmi) || (ref_idx == 0 && is_interintra_mode(mbmi)));
673
674
  // Store the first prediction buffer.
675
0
  struct buf_2d orig_yv12;
676
0
  if (ref_idx) {
677
0
    orig_yv12 = pd->pre[0];
678
0
    pd->pre[0] = pd->pre[ref_idx];
679
0
  }
680
681
0
  if (scaled_ref_frame) {
682
    // Swap out the reference frame for a version that's been scaled to
683
    // match the resolution of the current frame, allowing the existing
684
    // full-pixel motion search code to be used without additional
685
    // modifications.
686
0
    for (int i = 0; i < num_planes; i++) {
687
0
      backup_yv12[i] = xd->plane[i].pre[ref_idx];
688
0
    }
689
0
    const int mi_row = xd->mi_row;
690
0
    const int mi_col = xd->mi_col;
691
0
    av1_setup_pre_planes(xd, ref_idx, scaled_ref_frame, mi_row, mi_col, NULL,
692
0
                         num_planes);
693
0
  }
694
695
0
  int bestsme = INT_MAX;
696
0
  int_mv best_mv;
697
698
  // Make motion search params
699
0
  FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
700
0
  const search_site_config *src_search_sites =
701
0
      cpi->mv_search_params.search_site_cfg[SS_CFG_SRC];
702
0
  av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize,
703
0
                                     &ref_mv.as_mv, src_search_sites,
704
0
                                     /*fine_search_interval=*/0);
705
706
0
  av1_set_ms_compound_refs(&full_ms_params.ms_buffers, second_pred, mask,
707
0
                           mask_stride, ref_idx);
708
709
  // Use the mv result from the single mode as mv predictor.
710
0
  const FULLPEL_MV start_fullmv = get_fullmv_from_mv(this_mv);
711
712
  // Small-range full-pixel motion search.
713
0
  bestsme = av1_full_pixel_search(start_fullmv, &full_ms_params, 5, NULL,
714
0
                                  &best_mv.as_fullmv, NULL);
715
716
0
  if (scaled_ref_frame) {
717
    // Swap back the original buffers for subpel motion search.
718
0
    for (int i = 0; i < num_planes; i++) {
719
0
      xd->plane[i].pre[ref_idx] = backup_yv12[i];
720
0
    }
721
0
  }
722
723
0
  if (cpi->common.features.cur_frame_force_integer_mv) {
724
0
    convert_fullmv_to_mv(&best_mv);
725
0
  }
726
0
  const int use_fractional_mv =
727
0
      bestsme < INT_MAX && cpi->common.features.cur_frame_force_integer_mv == 0;
728
0
  if (use_fractional_mv) {
729
0
    int dis; /* TODO: use dis in distortion calculation later. */
730
0
    unsigned int sse;
731
0
    SUBPEL_MOTION_SEARCH_PARAMS ms_params;
732
0
    av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv.as_mv,
733
0
                                      NULL);
734
0
    av1_set_ms_compound_refs(&ms_params.var_params.ms_buffers, second_pred,
735
0
                             mask, mask_stride, ref_idx);
736
0
    ms_params.forced_stop = EIGHTH_PEL;
737
0
    MV start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
738
0
    bestsme = cpi->mv_search_params.find_fractional_mv_step(
739
0
        xd, cm, &ms_params, start_mv, &best_mv.as_mv, &dis, &sse, NULL);
740
0
  }
741
742
  // Restore the pointer to the first unscaled prediction buffer.
743
0
  if (ref_idx) pd->pre[0] = orig_yv12;
744
745
0
  if (bestsme < INT_MAX) *this_mv = best_mv.as_mv;
746
747
0
  *rate_mv = 0;
748
749
0
  *rate_mv += av1_mv_bit_cost(this_mv, &ref_mv.as_mv, mv_costs->nmv_joint_cost,
750
0
                              mv_costs->mv_cost_stack, MV_COST_WEIGHT);
751
0
  return bestsme;
752
0
}
753
754
static AOM_INLINE void build_second_inter_pred(const AV1_COMP *cpi,
755
                                               MACROBLOCK *x, BLOCK_SIZE bsize,
756
                                               const MV *other_mv, int ref_idx,
757
0
                                               uint8_t *second_pred) {
758
0
  const AV1_COMMON *const cm = &cpi->common;
759
0
  const int pw = block_size_wide[bsize];
760
0
  const int ph = block_size_high[bsize];
761
0
  MACROBLOCKD *xd = &x->e_mbd;
762
0
  MB_MODE_INFO *mbmi = xd->mi[0];
763
0
  struct macroblockd_plane *const pd = &xd->plane[0];
764
0
  const int mi_row = xd->mi_row;
765
0
  const int mi_col = xd->mi_col;
766
0
  const int p_col = ((mi_col * MI_SIZE) >> pd->subsampling_x);
767
0
  const int p_row = ((mi_row * MI_SIZE) >> pd->subsampling_y);
768
769
  // This function should only ever be called for compound modes
770
0
  assert(has_second_ref(mbmi));
771
772
0
  const int plane = 0;
773
0
  struct buf_2d ref_yv12 = xd->plane[plane].pre[!ref_idx];
774
775
0
  struct scale_factors sf;
776
0
  av1_setup_scale_factors_for_frame(&sf, ref_yv12.width, ref_yv12.height,
777
0
                                    cm->width, cm->height);
778
779
0
  InterPredParams inter_pred_params;
780
781
0
  av1_init_inter_params(&inter_pred_params, pw, ph, p_row, p_col,
782
0
                        pd->subsampling_x, pd->subsampling_y, xd->bd,
783
0
                        is_cur_buf_hbd(xd), 0, &sf, &ref_yv12,
784
0
                        mbmi->interp_filters);
785
0
  inter_pred_params.conv_params = get_conv_params(0, plane, xd->bd);
786
787
  // Get the prediction block from the 'other' reference frame.
788
0
  av1_enc_build_one_inter_predictor(second_pred, pw, other_mv,
789
0
                                    &inter_pred_params);
790
0
}
791
792
// Wrapper for av1_compound_single_motion_search, for the common case
793
// where the second prediction is also an inter mode.
794
int av1_compound_single_motion_search_interinter(
795
    const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int_mv *cur_mv,
796
0
    const uint8_t *mask, int mask_stride, int *rate_mv, int ref_idx) {
797
0
  MACROBLOCKD *xd = &x->e_mbd;
798
  // This function should only ever be called for compound modes
799
0
  assert(has_second_ref(xd->mi[0]));
800
801
  // Prediction buffer from second frame.
802
0
  DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[MAX_SB_SQUARE]);
803
0
  uint8_t *second_pred;
804
0
  if (is_cur_buf_hbd(xd))
805
0
    second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16);
806
0
  else
807
0
    second_pred = (uint8_t *)second_pred_alloc_16;
808
809
0
  MV *this_mv = &cur_mv[ref_idx].as_mv;
810
0
  const MV *other_mv = &cur_mv[!ref_idx].as_mv;
811
0
  build_second_inter_pred(cpi, x, bsize, other_mv, ref_idx, second_pred);
812
0
  return av1_compound_single_motion_search(cpi, x, bsize, this_mv, second_pred,
813
0
                                           mask, mask_stride, rate_mv, ref_idx);
814
0
}
815
816
static AOM_INLINE void do_masked_motion_search_indexed(
817
    const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv,
818
    const INTERINTER_COMPOUND_DATA *const comp_data, BLOCK_SIZE bsize,
819
0
    int_mv *tmp_mv, int *rate_mv, int which) {
820
  // NOTE: which values: 0 - 0 only, 1 - 1 only, 2 - both
821
0
  MACROBLOCKD *xd = &x->e_mbd;
822
0
  MB_MODE_INFO *mbmi = xd->mi[0];
823
0
  BLOCK_SIZE sb_type = mbmi->bsize;
824
0
  const uint8_t *mask;
825
0
  const int mask_stride = block_size_wide[bsize];
826
827
0
  mask = av1_get_compound_type_mask(comp_data, sb_type);
828
829
0
  tmp_mv[0].as_int = cur_mv[0].as_int;
830
0
  tmp_mv[1].as_int = cur_mv[1].as_int;
831
0
  if (which == 0 || which == 1) {
832
0
    av1_compound_single_motion_search_interinter(cpi, x, bsize, tmp_mv, mask,
833
0
                                                 mask_stride, rate_mv, which);
834
0
  } else if (which == 2) {
835
0
    av1_joint_motion_search(cpi, x, bsize, tmp_mv, mask, mask_stride, rate_mv,
836
0
                            !cpi->sf.mv_sf.disable_second_mv);
837
0
  }
838
0
}
839
840
int av1_interinter_compound_motion_search(const AV1_COMP *const cpi,
841
                                          MACROBLOCK *x,
842
                                          const int_mv *const cur_mv,
843
                                          const BLOCK_SIZE bsize,
844
0
                                          const PREDICTION_MODE this_mode) {
845
0
  MACROBLOCKD *const xd = &x->e_mbd;
846
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
847
0
  int_mv tmp_mv[2];
848
0
  int tmp_rate_mv = 0;
849
  // TODO(jingning): The average compound mode has proper SAD and variance
850
  // functions implemented, and is triggerd by setting the mask pointer as
851
  // Null. Need to further implement those for frame distance weighted mode.
852
0
  mbmi->interinter_comp.seg_mask =
853
0
      mbmi->interinter_comp.type == COMPOUND_AVERAGE ? NULL : xd->seg_mask;
854
0
  const INTERINTER_COMPOUND_DATA *compound_data = &mbmi->interinter_comp;
855
856
0
  if (this_mode == NEW_NEWMV) {
857
0
    do_masked_motion_search_indexed(cpi, x, cur_mv, compound_data, bsize,
858
0
                                    tmp_mv, &tmp_rate_mv, 2);
859
0
    mbmi->mv[0].as_int = tmp_mv[0].as_int;
860
0
    mbmi->mv[1].as_int = tmp_mv[1].as_int;
861
0
  } else if (this_mode >= NEAREST_NEWMV && this_mode <= NEW_NEARMV) {
862
    // which = 1 if this_mode == NEAREST_NEWMV || this_mode == NEAR_NEWMV
863
    // which = 0 if this_mode == NEW_NEARESTMV || this_mode == NEW_NEARMV
864
0
    int which = (NEWMV == compound_ref1_mode(this_mode));
865
0
    do_masked_motion_search_indexed(cpi, x, cur_mv, compound_data, bsize,
866
0
                                    tmp_mv, &tmp_rate_mv, which);
867
0
    mbmi->mv[which].as_int = tmp_mv[which].as_int;
868
0
  }
869
0
  return tmp_rate_mv;
870
0
}
871
872
int_mv av1_simple_motion_search(AV1_COMP *const cpi, MACROBLOCK *x, int mi_row,
873
                                int mi_col, BLOCK_SIZE bsize, int ref,
874
                                FULLPEL_MV start_mv, int num_planes,
875
0
                                int use_subpixel) {
876
0
  assert(num_planes == 1 &&
877
0
         "Currently simple_motion_search only supports luma plane");
878
0
  assert(!frame_is_intra_only(&cpi->common) &&
879
0
         "Simple motion search only enabled for non-key frames");
880
0
  AV1_COMMON *const cm = &cpi->common;
881
0
  MACROBLOCKD *xd = &x->e_mbd;
882
883
0
  set_offsets_for_motion_search(cpi, x, mi_row, mi_col, bsize);
884
885
0
  MB_MODE_INFO *mbmi = xd->mi[0];
886
0
  mbmi->bsize = bsize;
887
0
  mbmi->ref_frame[0] = ref;
888
0
  mbmi->ref_frame[1] = NONE_FRAME;
889
0
  mbmi->motion_mode = SIMPLE_TRANSLATION;
890
0
  mbmi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
891
892
0
  const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, ref);
893
0
  const YV12_BUFFER_CONFIG *scaled_ref_frame =
894
0
      av1_get_scaled_ref_frame(cpi, ref);
895
0
  struct buf_2d backup_yv12;
896
  // ref_mv is used to calculate the cost of the motion vector
897
0
  const MV ref_mv = kZeroMv;
898
0
  const int step_param =
899
0
      AOMMIN(cpi->mv_search_params.mv_step_param +
900
0
                 cpi->sf.part_sf.simple_motion_search_reduce_search_steps,
901
0
             MAX_MVSEARCH_STEPS - 2);
902
0
  const search_site_config *src_search_sites =
903
0
      cpi->mv_search_params.search_site_cfg[SS_CFG_SRC];
904
0
  int cost_list[5];
905
0
  const int ref_idx = 0;
906
0
  int var;
907
0
  int_mv best_mv;
908
909
0
  av1_setup_pre_planes(xd, ref_idx, yv12, mi_row, mi_col,
910
0
                       get_ref_scale_factors(cm, ref), num_planes);
911
0
  set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
912
0
  if (scaled_ref_frame) {
913
0
    backup_yv12 = xd->plane[AOM_PLANE_Y].pre[ref_idx];
914
0
    av1_setup_pre_planes(xd, ref_idx, scaled_ref_frame, mi_row, mi_col, NULL,
915
0
                         num_planes);
916
0
  }
917
918
  // Allow more mesh searches for screen content type on the ARF.
919
0
  const int fine_search_interval = use_fine_search_interval(cpi);
920
0
  FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
921
0
  av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize, &ref_mv,
922
0
                                     src_search_sites, fine_search_interval);
923
924
0
  var = av1_full_pixel_search(start_mv, &full_ms_params, step_param,
925
0
                              cond_cost_list(cpi, cost_list),
926
0
                              &best_mv.as_fullmv, NULL);
927
928
0
  const int use_subpel_search =
929
0
      var < INT_MAX && !cpi->common.features.cur_frame_force_integer_mv &&
930
0
      use_subpixel;
931
0
  if (scaled_ref_frame) {
932
0
    xd->plane[AOM_PLANE_Y].pre[ref_idx] = backup_yv12;
933
0
  }
934
0
  if (use_subpel_search) {
935
0
    int not_used = 0;
936
937
0
    SUBPEL_MOTION_SEARCH_PARAMS ms_params;
938
0
    av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv,
939
0
                                      cost_list);
940
    // TODO(yunqing): integrate this into av1_make_default_subpel_ms_params().
941
0
    ms_params.forced_stop = cpi->sf.mv_sf.simple_motion_subpel_force_stop;
942
943
0
    MV subpel_start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
944
945
0
    cpi->mv_search_params.find_fractional_mv_step(
946
0
        xd, cm, &ms_params, subpel_start_mv, &best_mv.as_mv, &not_used,
947
0
        &x->pred_sse[ref], NULL);
948
0
  } else {
949
    // Manually convert from units of pixel to 1/8-pixels if we are not doing
950
    // subpel search
951
0
    convert_fullmv_to_mv(&best_mv);
952
0
  }
953
954
0
  mbmi->mv[0] = best_mv;
955
956
  // Get a copy of the prediction output
957
0
  av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
958
0
                                AOM_PLANE_Y, AOM_PLANE_Y);
959
960
0
  if (scaled_ref_frame) {
961
0
    xd->plane[AOM_PLANE_Y].pre[ref_idx] = backup_yv12;
962
0
  }
963
964
0
  return best_mv;
965
0
}
966
967
int_mv av1_simple_motion_sse_var(AV1_COMP *cpi, MACROBLOCK *x, int mi_row,
968
                                 int mi_col, BLOCK_SIZE bsize,
969
                                 const FULLPEL_MV start_mv, int use_subpixel,
970
0
                                 unsigned int *sse, unsigned int *var) {
971
0
  MACROBLOCKD *xd = &x->e_mbd;
972
0
  const MV_REFERENCE_FRAME ref =
973
0
      cpi->rc.is_src_frame_alt_ref ? ALTREF_FRAME : LAST_FRAME;
974
975
0
  int_mv best_mv = av1_simple_motion_search(cpi, x, mi_row, mi_col, bsize, ref,
976
0
                                            start_mv, 1, use_subpixel);
977
978
0
  const uint8_t *src = x->plane[0].src.buf;
979
0
  const int src_stride = x->plane[0].src.stride;
980
0
  const uint8_t *dst = xd->plane[0].dst.buf;
981
0
  const int dst_stride = xd->plane[0].dst.stride;
982
983
0
  *var = cpi->ppi->fn_ptr[bsize].vf(src, src_stride, dst, dst_stride, sse);
984
985
0
  return best_mv;
986
0
}