Coverage Report

Created: 2025-06-22 08:04

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