Coverage Report

Created: 2026-11-12 08:20

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