Coverage Report

Created: 2026-04-01 07:49

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