Coverage Report

Created: 2026-07-25 07:06

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
430
0
            MV this_best_mv;
431
0
            subpel_start_mv = get_mv_from_fullmv(&second_best_mv.as_fullmv);
432
0
            if (av1_is_subpelmv_in_range(&ms_params.mv_limits,
433
0
                                         subpel_start_mv)) {
434
0
              unsigned int sse;
435
0
              const int this_var = mv_search_params->find_fractional_mv_step(
436
0
                  xd, cm, &ms_params, subpel_start_mv, NULL, &this_best_mv,
437
0
                  &dis, &sse, fractional_ms_list);
438
439
              // If cpi->sf.mv_sf.disable_second_mv is 0 and both MVs are valid,
440
              // use actual rd cost to choose the better MV.
441
0
              if (!mv_sf->disable_second_mv && this_var != INT32_MAX) {
442
                // Calculate rd cost of first MV.
443
0
                mbmi->mv[0].as_mv = best_mv->as_mv;
444
0
                av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, &orig_dst,
445
0
                                              bsize, 0, 0);
446
0
                RD_STATS this_rd_stats;
447
0
                av1_init_rd_stats(&this_rd_stats);
448
0
                av1_estimate_txfm_yrd(cpi, x, &this_rd_stats, INT64_MAX, bsize,
449
0
                                      max_txsize_rect_lookup[bsize]);
450
0
                int this_mv_rate = av1_mv_bit_cost(
451
0
                    &best_mv->as_mv, &ref_mv, mv_costs->nmv_joint_cost,
452
0
                    mv_costs->mv_cost_stack, MV_COST_WEIGHT);
453
0
                const int64_t rd =
454
0
                    RDCOST(x->rdmult, this_mv_rate + this_rd_stats.rate,
455
0
                           this_rd_stats.dist);
456
457
                // Calculate rd cost of second MV.
458
0
                mbmi->mv[0].as_mv = this_best_mv;
459
0
                av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, &orig_dst,
460
0
                                              bsize, 0, 0);
461
0
                RD_STATS tmp_rd_stats;
462
0
                av1_init_rd_stats(&tmp_rd_stats);
463
0
                av1_estimate_txfm_yrd(cpi, x, &tmp_rd_stats, INT64_MAX, bsize,
464
0
                                      max_txsize_rect_lookup[bsize]);
465
0
                int tmp_mv_rate = av1_mv_bit_cost(
466
0
                    &this_best_mv, &ref_mv, mv_costs->nmv_joint_cost,
467
0
                    mv_costs->mv_cost_stack, MV_COST_WEIGHT);
468
0
                int64_t tmp_rd =
469
0
                    RDCOST(x->rdmult, tmp_rd_stats.rate + tmp_mv_rate,
470
0
                           tmp_rd_stats.dist);
471
0
                if (tmp_rd < rd) {
472
0
                  best_mv->as_mv = this_best_mv;
473
0
                  x->pred_sse[ref] = sse;
474
0
                }
475
0
              } else {
476
                // If cpi->sf.mv_sf.disable_second_mv = 1, use var to decide the
477
                // best MV.
478
0
                if (this_var < best_mv_var) {
479
0
                  best_mv->as_mv = this_best_mv;
480
0
                  x->pred_sse[ref] = sse;
481
0
                }
482
0
              }
483
0
            }
484
0
          }
485
0
        } else {
486
0
          mv_search_params->find_fractional_mv_step(
487
0
              xd, cm, &ms_params, subpel_start_mv, &best_mv_stats,
488
0
              &best_mv->as_mv, &dis, &x->pred_sse[ref], NULL);
489
0
        }
490
0
        break;
491
0
      case OBMC_CAUSAL:
492
0
        av1_find_best_obmc_sub_pixel_tree_up(
493
0
            xd, cm, &ms_params, subpel_start_mv, NULL, &best_mv->as_mv, &dis,
494
0
            &x->pred_sse[ref], NULL);
495
0
        break;
496
0
      default: assert(0 && "Invalid motion mode!\n");
497
0
    }
498
499
    // Terminate search with the current ref_idx based on subpel mv and rate
500
    // cost.
501
0
    if (cpi->sf.inter_sf.skip_newmv_in_drl >= 1 && args != NULL &&
502
0
        mbmi->motion_mode == SIMPLE_TRANSLATION &&
503
0
        best_mv->as_int != INVALID_MV) {
504
0
      const int ref_mv_idx = mbmi->ref_mv_idx;
505
0
      best_mv_rate =
506
0
          av1_mv_bit_cost(&best_mv->as_mv, &ref_mv, mv_costs->nmv_joint_cost,
507
0
                          mv_costs->mv_cost_stack, MV_COST_WEIGHT);
508
0
      mv_rate_calculated = 1;
509
510
0
      for (int prev_ref_idx = 0; prev_ref_idx < ref_mv_idx; ++prev_ref_idx) {
511
0
        if (!args->single_newmv_valid[prev_ref_idx][ref]) continue;
512
        // Check if the motion vectors are the same.
513
0
        if (best_mv->as_int == args->single_newmv[prev_ref_idx][ref].as_int) {
514
          // Skip this evaluation if the previous one is skipped.
515
0
          if (mode_info[prev_ref_idx].skip) {
516
0
            mode_info[ref_mv_idx].skip = 1;
517
0
            break;
518
0
          }
519
          // Compare the rate cost that we current know.
520
0
          const int prev_rate_cost =
521
0
              args->single_newmv_rate[prev_ref_idx][ref] +
522
0
              mode_info[prev_ref_idx].drl_cost;
523
0
          const int this_rate_cost =
524
0
              best_mv_rate + mode_info[ref_mv_idx].drl_cost;
525
526
0
          if (prev_rate_cost <= this_rate_cost) {
527
            // If the current rate_cost is worse than the previous rate_cost,
528
            // then we terminate the search for this ref_mv_idx.
529
0
            mode_info[ref_mv_idx].skip = 1;
530
0
            break;
531
0
          }
532
0
        }
533
0
      }
534
0
    }
535
0
  }
536
537
0
  if (mv_rate_calculated) {
538
0
    *rate_mv = best_mv_rate;
539
0
  } else {
540
0
    *rate_mv =
541
0
        av1_mv_bit_cost(&best_mv->as_mv, &ref_mv, mv_costs->nmv_joint_cost,
542
0
                        mv_costs->mv_cost_stack, MV_COST_WEIGHT);
543
0
  }
544
0
}
545
546
int av1_joint_motion_search(const AV1_COMP *cpi, MACROBLOCK *x,
547
                            BLOCK_SIZE bsize, int_mv *cur_mv,
548
                            const uint8_t *mask, int mask_stride, int *rate_mv,
549
0
                            int allow_second_mv, int joint_me_num_refine_iter) {
550
0
  const AV1_COMMON *const cm = &cpi->common;
551
0
  const int num_planes = av1_num_planes(cm);
552
0
  const int pw = block_size_wide[bsize];
553
0
  const int ph = block_size_high[bsize];
554
0
  const int plane = 0;
555
0
  MACROBLOCKD *xd = &x->e_mbd;
556
0
  MB_MODE_INFO *mbmi = xd->mi[0];
557
  // This function should only ever be called for compound modes
558
0
  assert(has_second_ref(mbmi));
559
0
  const int_mv init_mv[2] = { cur_mv[0], cur_mv[1] };
560
0
  const int refs[2] = { mbmi->ref_frame[0], mbmi->ref_frame[1] };
561
0
  const MvCosts *mv_costs = x->mv_costs;
562
0
  int_mv ref_mv[2];
563
0
  int ite, ref;
564
565
  // Get the prediction block from the 'other' reference frame.
566
0
  const int_interpfilters interp_filters =
567
0
      av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
568
569
0
  InterPredParams inter_pred_params;
570
0
  const int mi_row = xd->mi_row;
571
0
  const int mi_col = xd->mi_col;
572
573
  // Do joint motion search in compound mode to get more accurate mv.
574
0
  struct buf_2d backup_yv12[2][MAX_MB_PLANE];
575
0
  int last_besterr[2] = { INT_MAX, INT_MAX };
576
0
  const YV12_BUFFER_CONFIG *const scaled_ref_frame[2] = {
577
0
    av1_get_scaled_ref_frame(cpi, refs[0]),
578
0
    av1_get_scaled_ref_frame(cpi, refs[1])
579
0
  };
580
581
  // Prediction buffer from second frame.
582
0
  DECLARE_ALIGNED(16, uint8_t, second_pred16[MAX_SB_SQUARE * sizeof(uint16_t)]);
583
0
  uint8_t *second_pred = get_buf_by_bd(xd, second_pred16);
584
585
0
  int_mv best_mv, second_best_mv;
586
587
  // Allow joint search multiple times iteratively for each reference frame
588
  // and break out of the search loop if it couldn't find a better mv.
589
0
  for (ite = 0; ite < (2 * joint_me_num_refine_iter); ite++) {
590
0
    struct buf_2d ref_yv12[2];
591
0
    int bestsme = INT_MAX;
592
0
    int id = ite % 2;  // Even iterations search in the first reference frame,
593
                       // odd iterations search in the second. The predictor
594
                       // found for the 'other' reference frame is factored in.
595
0
    if (ite >= 2 && cur_mv[!id].as_int == init_mv[!id].as_int) {
596
0
      if (cur_mv[id].as_int == init_mv[id].as_int) {
597
0
        break;
598
0
      } else {
599
0
        int_mv cur_int_mv, init_int_mv;
600
0
        cur_int_mv.as_mv.col = cur_mv[id].as_mv.col >> 3;
601
0
        cur_int_mv.as_mv.row = cur_mv[id].as_mv.row >> 3;
602
0
        init_int_mv.as_mv.row = init_mv[id].as_mv.row >> 3;
603
0
        init_int_mv.as_mv.col = init_mv[id].as_mv.col >> 3;
604
0
        if (cur_int_mv.as_int == init_int_mv.as_int) {
605
0
          break;
606
0
        }
607
0
      }
608
0
    }
609
0
    for (ref = 0; ref < 2; ++ref) {
610
0
      ref_mv[ref] = av1_get_ref_mv(x, ref);
611
      // Swap out the reference frame for a version that's been scaled to
612
      // match the resolution of the current frame, allowing the existing
613
      // motion search code to be used without additional modifications.
614
0
      if (scaled_ref_frame[ref]) {
615
0
        int i;
616
0
        for (i = 0; i < num_planes; i++)
617
0
          backup_yv12[ref][i] = xd->plane[i].pre[ref];
618
0
        av1_setup_pre_planes(xd, ref, scaled_ref_frame[ref], mi_row, mi_col,
619
0
                             NULL, num_planes);
620
0
      }
621
0
    }
622
623
0
    assert(IMPLIES(scaled_ref_frame[0] != NULL,
624
0
                   cm->width == scaled_ref_frame[0]->y_crop_width &&
625
0
                       cm->height == scaled_ref_frame[0]->y_crop_height));
626
0
    assert(IMPLIES(scaled_ref_frame[1] != NULL,
627
0
                   cm->width == scaled_ref_frame[1]->y_crop_width &&
628
0
                       cm->height == scaled_ref_frame[1]->y_crop_height));
629
630
    // Initialize based on (possibly scaled) prediction buffers.
631
0
    ref_yv12[0] = xd->plane[plane].pre[0];
632
0
    ref_yv12[1] = xd->plane[plane].pre[1];
633
634
0
    av1_init_inter_params(&inter_pred_params, pw, ph, mi_row * MI_SIZE,
635
0
                          mi_col * MI_SIZE, 0, 0, xd->bd, is_cur_buf_hbd(xd), 0,
636
0
                          &cm->sf_identity, &ref_yv12[!id], interp_filters);
637
0
    inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
638
639
    // Since we have scaled the reference frames to match the size of the
640
    // current frame we must use a unit scaling factor during mode selection.
641
0
    av1_enc_build_one_inter_predictor(second_pred, pw, &cur_mv[!id].as_mv,
642
0
                                      &inter_pred_params);
643
644
    // Do full-pixel compound motion search on the current reference frame.
645
0
    if (id) xd->plane[plane].pre[0] = ref_yv12[id];
646
647
    // Make motion search params
648
0
    FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
649
0
    FULLPEL_MV_STATS best_mv_stats;
650
0
    const MV_SPEED_FEATURES *mv_sf = &cpi->sf.mv_sf;
651
0
    const SEARCH_METHODS search_method =
652
0
        av1_get_default_mv_search_method(x, mv_sf, bsize);
653
0
    const search_site_config *src_search_sites =
654
0
        av1_get_search_site_config(cpi, x, search_method);
655
    // Use the mv result from the single mode as mv predictor.
656
0
    const FULLPEL_MV start_fullmv = get_fullmv_from_mv(&cur_mv[id].as_mv);
657
0
    av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize,
658
0
                                       &ref_mv[id].as_mv, start_fullmv,
659
0
                                       src_search_sites, search_method,
660
0
                                       /*fine_search_interval=*/0);
661
662
0
    av1_set_ms_compound_refs(&full_ms_params.ms_buffers, second_pred, mask,
663
0
                             mask_stride, id);
664
665
    // Small-range full-pixel motion search.
666
0
    if (!mv_sf->disable_extensive_joint_motion_search &&
667
0
        mbmi->interinter_comp.type != COMPOUND_WEDGE) {
668
0
      bestsme = av1_full_pixel_search(start_fullmv, &full_ms_params, 5, NULL,
669
0
                                      &best_mv.as_fullmv, &best_mv_stats,
670
0
                                      &second_best_mv.as_fullmv);
671
0
    } else {
672
0
      bestsme = av1_refining_search_8p_c(&full_ms_params, start_fullmv,
673
0
                                         &best_mv.as_fullmv);
674
0
      second_best_mv = best_mv;
675
0
    }
676
677
0
    const int try_second = second_best_mv.as_int != INVALID_MV &&
678
0
                           second_best_mv.as_int != best_mv.as_int &&
679
0
                           allow_second_mv;
680
681
    // Restore the pointer to the first (possibly scaled) prediction buffer.
682
0
    if (id) xd->plane[plane].pre[0] = ref_yv12[0];
683
684
0
    for (ref = 0; ref < 2; ++ref) {
685
0
      if (scaled_ref_frame[ref]) {
686
        // Swap back the original buffers for subpel motion search.
687
0
        for (int i = 0; i < num_planes; i++) {
688
0
          xd->plane[i].pre[ref] = backup_yv12[ref][i];
689
0
        }
690
        // Re-initialize based on unscaled prediction buffers.
691
0
        ref_yv12[ref] = xd->plane[plane].pre[ref];
692
0
      }
693
0
    }
694
695
    // Do sub-pixel compound motion search on the current reference frame.
696
0
    if (id) xd->plane[plane].pre[0] = ref_yv12[id];
697
698
0
    if (cpi->common.features.cur_frame_force_integer_mv) {
699
0
      convert_fullmv_to_mv(&best_mv);
700
0
    }
701
0
    if (bestsme < INT_MAX &&
702
0
        cpi->common.features.cur_frame_force_integer_mv == 0) {
703
0
      int dis; /* TODO: use dis in distortion calculation later. */
704
0
      unsigned int sse;
705
0
      SUBPEL_MOTION_SEARCH_PARAMS ms_params;
706
0
      av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize,
707
0
                                        &ref_mv[id].as_mv, NULL);
708
0
      av1_set_ms_compound_refs(&ms_params.var_params.ms_buffers, second_pred,
709
0
                               mask, mask_stride, id);
710
0
      ms_params.forced_stop = EIGHTH_PEL;
711
0
      MV start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
712
0
      assert(av1_is_subpelmv_in_range(&ms_params.mv_limits, start_mv));
713
0
      bestsme = cpi->mv_search_params.find_fractional_mv_step(
714
0
          xd, cm, &ms_params, start_mv, NULL, &best_mv.as_mv, &dis, &sse, NULL);
715
716
0
      if (try_second) {
717
0
        MV this_best_mv;
718
0
        MV subpel_start_mv = get_mv_from_fullmv(&second_best_mv.as_fullmv);
719
0
        if (av1_is_subpelmv_in_range(&ms_params.mv_limits, subpel_start_mv)) {
720
0
          const int thissme = cpi->mv_search_params.find_fractional_mv_step(
721
0
              xd, cm, &ms_params, subpel_start_mv, NULL, &this_best_mv, &dis,
722
0
              &sse, NULL);
723
0
          if (thissme < bestsme) {
724
0
            best_mv.as_mv = this_best_mv;
725
0
            bestsme = thissme;
726
0
          }
727
0
        }
728
0
      }
729
0
    }
730
731
    // Restore the pointer to the first prediction buffer.
732
0
    if (id) xd->plane[plane].pre[0] = ref_yv12[0];
733
0
    if (bestsme < last_besterr[id]) {
734
0
      cur_mv[id] = best_mv;
735
0
      last_besterr[id] = bestsme;
736
0
    } else {
737
0
      break;
738
0
    }
739
0
  }
740
741
0
  *rate_mv = 0;
742
743
0
  for (ref = 0; ref < 2; ++ref) {
744
0
    const int_mv curr_ref_mv = av1_get_ref_mv(x, ref);
745
0
    *rate_mv += av1_mv_bit_cost(&cur_mv[ref].as_mv, &curr_ref_mv.as_mv,
746
0
                                mv_costs->nmv_joint_cost,
747
0
                                mv_costs->mv_cost_stack, MV_COST_WEIGHT);
748
0
  }
749
750
0
  return AOMMIN(last_besterr[0], last_besterr[1]);
751
0
}
752
753
// Search for the best mv for one component of a compound,
754
// given that the other component is fixed.
755
int av1_compound_single_motion_search(const AV1_COMP *cpi, MACROBLOCK *x,
756
                                      BLOCK_SIZE bsize, MV *this_mv,
757
                                      const uint8_t *second_pred,
758
                                      const uint8_t *mask, int mask_stride,
759
0
                                      int *rate_mv, int ref_idx) {
760
0
  const AV1_COMMON *const cm = &cpi->common;
761
0
  const int num_planes = av1_num_planes(cm);
762
0
  MACROBLOCKD *xd = &x->e_mbd;
763
0
  MB_MODE_INFO *mbmi = xd->mi[0];
764
0
  const int ref = mbmi->ref_frame[ref_idx];
765
0
  const int_mv ref_mv = av1_get_ref_mv(x, ref_idx);
766
0
  struct macroblockd_plane *const pd = &xd->plane[0];
767
0
  const MvCosts *mv_costs = x->mv_costs;
768
769
0
  struct buf_2d backup_yv12[MAX_MB_PLANE];
770
0
  const YV12_BUFFER_CONFIG *const scaled_ref_frame =
771
0
      av1_get_scaled_ref_frame(cpi, ref);
772
773
  // Check that this is either an interinter or an interintra block
774
0
  assert(has_second_ref(mbmi) || (ref_idx == 0 && is_interintra_mode(mbmi)));
775
776
  // Store the first prediction buffer.
777
0
  struct buf_2d orig_yv12;
778
0
  if (ref_idx) {
779
0
    orig_yv12 = pd->pre[0];
780
0
    pd->pre[0] = pd->pre[ref_idx];
781
0
  }
782
783
0
  if (scaled_ref_frame) {
784
    // Swap out the reference frame for a version that's been scaled to
785
    // match the resolution of the current frame, allowing the existing
786
    // full-pixel motion search code to be used without additional
787
    // modifications.
788
0
    for (int i = 0; i < num_planes; i++) {
789
0
      backup_yv12[i] = xd->plane[i].pre[ref_idx];
790
0
    }
791
0
    const int mi_row = xd->mi_row;
792
0
    const int mi_col = xd->mi_col;
793
    // The index below needs to be 0 instead of ref_idx since we assume the
794
    // 0th slot to be used for subsequent searches. Note that the ref_idx
795
    // reference buffer has been copied to the 0th slot in the code above.
796
    // Now we need to swap the reference frame for the 0th slot.
797
0
    av1_setup_pre_planes(xd, 0, scaled_ref_frame, mi_row, mi_col, NULL,
798
0
                         num_planes);
799
0
  }
800
801
0
  int bestsme = INT_MAX;
802
0
  int_mv best_mv;
803
804
  // Make motion search params
805
0
  FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
806
0
  FULLPEL_MV_STATS best_mv_stats;
807
0
  const SEARCH_METHODS search_method =
808
0
      av1_get_default_mv_search_method(x, &cpi->sf.mv_sf, bsize);
809
0
  const search_site_config *src_search_sites =
810
0
      av1_get_search_site_config(cpi, x, search_method);
811
  // Use the mv result from the single mode as mv predictor.
812
0
  const FULLPEL_MV start_fullmv = get_fullmv_from_mv(this_mv);
813
0
  av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize,
814
0
                                     &ref_mv.as_mv, start_fullmv,
815
0
                                     src_search_sites, search_method,
816
0
                                     /*fine_search_interval=*/0);
817
818
0
  av1_set_ms_compound_refs(&full_ms_params.ms_buffers, second_pred, mask,
819
0
                           mask_stride, ref_idx);
820
821
  // Small-range full-pixel motion search.
822
0
  bestsme = av1_full_pixel_search(start_fullmv, &full_ms_params, 5, NULL,
823
0
                                  &best_mv.as_fullmv, &best_mv_stats, NULL);
824
825
0
  if (scaled_ref_frame) {
826
    // Swap back the original buffers for subpel motion search for the 0th slot.
827
0
    for (int i = 0; i < num_planes; i++) {
828
0
      xd->plane[i].pre[0] = backup_yv12[i];
829
0
    }
830
0
  }
831
832
0
  if (cpi->common.features.cur_frame_force_integer_mv) {
833
0
    convert_fullmv_to_mv(&best_mv);
834
0
  }
835
0
  const int use_fractional_mv =
836
0
      bestsme < INT_MAX && cpi->common.features.cur_frame_force_integer_mv == 0;
837
0
  if (use_fractional_mv) {
838
0
    int dis; /* TODO: use dis in distortion calculation later. */
839
0
    unsigned int sse;
840
0
    SUBPEL_MOTION_SEARCH_PARAMS ms_params;
841
0
    av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv.as_mv,
842
0
                                      NULL);
843
0
    av1_set_ms_compound_refs(&ms_params.var_params.ms_buffers, second_pred,
844
0
                             mask, mask_stride, ref_idx);
845
0
    ms_params.forced_stop = EIGHTH_PEL;
846
0
    MV start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
847
0
    assert(av1_is_subpelmv_in_range(&ms_params.mv_limits, start_mv));
848
0
    bestsme = cpi->mv_search_params.find_fractional_mv_step(
849
0
        xd, cm, &ms_params, start_mv, &best_mv_stats, &best_mv.as_mv, &dis,
850
0
        &sse, NULL);
851
0
  }
852
853
  // Restore the pointer to the first unscaled prediction buffer.
854
0
  if (ref_idx) pd->pre[0] = orig_yv12;
855
856
0
  if (bestsme < INT_MAX) *this_mv = best_mv.as_mv;
857
858
0
  *rate_mv = 0;
859
860
0
  *rate_mv += av1_mv_bit_cost(this_mv, &ref_mv.as_mv, mv_costs->nmv_joint_cost,
861
0
                              mv_costs->mv_cost_stack, MV_COST_WEIGHT);
862
0
  return bestsme;
863
0
}
864
865
static inline void build_second_inter_pred(const AV1_COMP *cpi, MACROBLOCK *x,
866
                                           BLOCK_SIZE bsize, const MV *other_mv,
867
0
                                           int ref_idx, uint8_t *second_pred) {
868
0
  const AV1_COMMON *const cm = &cpi->common;
869
0
  const int pw = block_size_wide[bsize];
870
0
  const int ph = block_size_high[bsize];
871
0
  MACROBLOCKD *xd = &x->e_mbd;
872
0
  MB_MODE_INFO *mbmi = xd->mi[0];
873
0
  struct macroblockd_plane *const pd = &xd->plane[0];
874
0
  const int mi_row = xd->mi_row;
875
0
  const int mi_col = xd->mi_col;
876
0
  const int p_col = ((mi_col * MI_SIZE) >> pd->subsampling_x);
877
0
  const int p_row = ((mi_row * MI_SIZE) >> pd->subsampling_y);
878
879
  // This function should only ever be called for compound modes
880
0
  assert(has_second_ref(mbmi));
881
882
0
  const int plane = 0;
883
0
  struct buf_2d ref_yv12 = xd->plane[plane].pre[!ref_idx];
884
885
0
  struct scale_factors sf;
886
0
  av1_setup_scale_factors_for_frame(&sf, ref_yv12.width, ref_yv12.height,
887
0
                                    cm->width, cm->height);
888
889
0
  InterPredParams inter_pred_params;
890
891
0
  av1_init_inter_params(&inter_pred_params, pw, ph, p_row, p_col,
892
0
                        pd->subsampling_x, pd->subsampling_y, xd->bd,
893
0
                        is_cur_buf_hbd(xd), 0, &sf, &ref_yv12,
894
0
                        mbmi->interp_filters);
895
0
  inter_pred_params.conv_params = get_conv_params(0, plane, xd->bd);
896
897
  // Get the prediction block from the 'other' reference frame.
898
0
  av1_enc_build_one_inter_predictor(second_pred, pw, other_mv,
899
0
                                    &inter_pred_params);
900
0
}
901
902
// Wrapper for av1_compound_single_motion_search, for the common case
903
// where the second prediction is also an inter mode.
904
static int compound_single_motion_search_interinter(
905
    const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize, int_mv *cur_mv,
906
0
    const uint8_t *mask, int mask_stride, int *rate_mv, int ref_idx) {
907
0
  MACROBLOCKD *xd = &x->e_mbd;
908
  // This function should only ever be called for compound modes
909
0
  assert(has_second_ref(xd->mi[0]));
910
911
  // Prediction buffer from second frame.
912
0
  DECLARE_ALIGNED(16, uint16_t, second_pred_alloc_16[MAX_SB_SQUARE]);
913
0
  uint8_t *second_pred;
914
0
  if (is_cur_buf_hbd(xd))
915
0
    second_pred = CONVERT_TO_BYTEPTR(second_pred_alloc_16);
916
0
  else
917
0
    second_pred = (uint8_t *)second_pred_alloc_16;
918
919
0
  MV *this_mv = &cur_mv[ref_idx].as_mv;
920
0
  const MV *other_mv = &cur_mv[!ref_idx].as_mv;
921
0
  build_second_inter_pred(cpi, x, bsize, other_mv, ref_idx, second_pred);
922
0
  return av1_compound_single_motion_search(cpi, x, bsize, this_mv, second_pred,
923
0
                                           mask, mask_stride, rate_mv, ref_idx);
924
0
}
925
926
static inline void do_masked_motion_search_indexed(
927
    const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv,
928
    const INTERINTER_COMPOUND_DATA *const comp_data, BLOCK_SIZE bsize,
929
0
    int_mv *tmp_mv, int *rate_mv, int which) {
930
  // NOTE: which values: 0 - 0 only, 1 - 1 only, 2 - both
931
0
  MACROBLOCKD *xd = &x->e_mbd;
932
0
  MB_MODE_INFO *mbmi = xd->mi[0];
933
0
  BLOCK_SIZE sb_type = mbmi->bsize;
934
0
  const uint8_t *mask;
935
0
  const int mask_stride = block_size_wide[bsize];
936
937
0
  mask = av1_get_compound_type_mask(comp_data, sb_type);
938
939
0
  tmp_mv[0].as_int = cur_mv[0].as_int;
940
0
  tmp_mv[1].as_int = cur_mv[1].as_int;
941
0
  if (which == 0 || which == 1) {
942
0
    compound_single_motion_search_interinter(cpi, x, bsize, tmp_mv, mask,
943
0
                                             mask_stride, rate_mv, which);
944
0
  } else if (which == 2) {
945
0
    const int joint_me_num_refine_iter =
946
0
        cpi->sf.inter_sf.enable_fast_compound_mode_search == 2
947
0
            ? REDUCED_JOINT_ME_REFINE_ITER
948
0
            : NUM_JOINT_ME_REFINE_ITER;
949
0
    av1_joint_motion_search(cpi, x, bsize, tmp_mv, mask, mask_stride, rate_mv,
950
0
                            !cpi->sf.mv_sf.disable_second_mv,
951
0
                            joint_me_num_refine_iter);
952
0
  }
953
0
}
954
955
int av1_interinter_compound_motion_search(const AV1_COMP *const cpi,
956
                                          MACROBLOCK *x,
957
                                          const int_mv *const cur_mv,
958
                                          const BLOCK_SIZE bsize,
959
0
                                          const PREDICTION_MODE this_mode) {
960
0
  MACROBLOCKD *const xd = &x->e_mbd;
961
0
  MB_MODE_INFO *const mbmi = xd->mi[0];
962
0
  int_mv tmp_mv[2];
963
0
  int tmp_rate_mv = 0;
964
  // TODO(jingning): The average compound mode has proper SAD and variance
965
  // functions implemented, and is triggerd by setting the mask pointer as
966
  // Null. Need to further implement those for frame distance weighted mode.
967
0
  mbmi->interinter_comp.seg_mask =
968
0
      mbmi->interinter_comp.type == COMPOUND_AVERAGE ? NULL : xd->seg_mask;
969
0
  const INTERINTER_COMPOUND_DATA *compound_data = &mbmi->interinter_comp;
970
971
0
  if (this_mode == NEW_NEWMV) {
972
0
    do_masked_motion_search_indexed(cpi, x, cur_mv, compound_data, bsize,
973
0
                                    tmp_mv, &tmp_rate_mv, 2);
974
0
    mbmi->mv[0].as_int = tmp_mv[0].as_int;
975
0
    mbmi->mv[1].as_int = tmp_mv[1].as_int;
976
0
  } else if (this_mode >= NEAREST_NEWMV && this_mode <= NEW_NEARMV) {
977
    // which = 1 if this_mode == NEAREST_NEWMV || this_mode == NEAR_NEWMV
978
    // which = 0 if this_mode == NEW_NEARESTMV || this_mode == NEW_NEARMV
979
0
    int which = (NEWMV == compound_ref1_mode(this_mode));
980
0
    do_masked_motion_search_indexed(cpi, x, cur_mv, compound_data, bsize,
981
0
                                    tmp_mv, &tmp_rate_mv, which);
982
0
    mbmi->mv[which].as_int = tmp_mv[which].as_int;
983
0
  }
984
0
  return tmp_rate_mv;
985
0
}
986
987
int_mv av1_simple_motion_search_sse_var(AV1_COMP *const cpi, MACROBLOCK *x,
988
                                        int mi_row, int mi_col,
989
                                        BLOCK_SIZE bsize, int ref,
990
                                        FULLPEL_MV start_mv, int num_planes,
991
                                        int use_subpixel, unsigned int *sse,
992
0
                                        unsigned int *var) {
993
0
  assert(num_planes == 1 &&
994
0
         "Currently simple_motion_search only supports luma plane");
995
0
  assert(!frame_is_intra_only(&cpi->common) &&
996
0
         "Simple motion search only enabled for non-key frames");
997
0
  AV1_COMMON *const cm = &cpi->common;
998
0
  MACROBLOCKD *xd = &x->e_mbd;
999
1000
0
  set_offsets_for_motion_search(cpi, x, mi_row, mi_col, bsize);
1001
1002
0
  MB_MODE_INFO *mbmi = xd->mi[0];
1003
0
  mbmi->bsize = bsize;
1004
0
  mbmi->ref_frame[0] = ref;
1005
0
  mbmi->ref_frame[1] = NONE_FRAME;
1006
0
  mbmi->motion_mode = SIMPLE_TRANSLATION;
1007
0
  mbmi->interp_filters = av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
1008
1009
0
  const YV12_BUFFER_CONFIG *yv12 = get_ref_frame_yv12_buf(cm, ref);
1010
0
  const YV12_BUFFER_CONFIG *scaled_ref_frame =
1011
0
      av1_get_scaled_ref_frame(cpi, ref);
1012
0
  struct buf_2d backup_yv12;
1013
  // ref_mv is used to calculate the cost of the motion vector
1014
0
  const MV ref_mv = kZeroMv;
1015
0
  const int step_param =
1016
0
      AOMMIN(cpi->mv_search_params.mv_step_param +
1017
0
                 cpi->sf.part_sf.simple_motion_search_reduce_search_steps,
1018
0
             MAX_MVSEARCH_STEPS - 2);
1019
0
  int cost_list[5];
1020
0
  const int ref_idx = 0;
1021
0
  int bestsme;
1022
0
  int_mv best_mv;
1023
0
  FULLPEL_MV_STATS best_mv_stats;
1024
1025
0
  av1_setup_pre_planes(xd, ref_idx, yv12, mi_row, mi_col,
1026
0
                       get_ref_scale_factors(cm, ref), num_planes);
1027
0
  set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
1028
0
  if (scaled_ref_frame) {
1029
0
    backup_yv12 = xd->plane[AOM_PLANE_Y].pre[ref_idx];
1030
0
    av1_setup_pre_planes(xd, ref_idx, scaled_ref_frame, mi_row, mi_col, NULL,
1031
0
                         num_planes);
1032
0
  }
1033
1034
  // Allow more mesh searches for screen content type on the ARF.
1035
0
  const int fine_search_interval = use_fine_search_interval(cpi);
1036
0
  FULLPEL_MOTION_SEARCH_PARAMS full_ms_params;
1037
0
  const MV_SPEED_FEATURES *mv_sf = &cpi->sf.mv_sf;
1038
0
  const SEARCH_METHODS search_method =
1039
0
      av1_get_default_mv_search_method(x, mv_sf, bsize);
1040
0
  const search_site_config *src_search_sites =
1041
0
      av1_get_search_site_config(cpi, x, search_method);
1042
0
  av1_make_default_fullpel_ms_params(&full_ms_params, cpi, x, bsize, &ref_mv,
1043
0
                                     start_mv, src_search_sites, search_method,
1044
0
                                     fine_search_interval);
1045
1046
0
  bestsme = av1_full_pixel_search(start_mv, &full_ms_params, step_param,
1047
0
                                  cond_cost_list(cpi, cost_list),
1048
0
                                  &best_mv.as_fullmv, &best_mv_stats, NULL);
1049
1050
0
  const int use_subpel_search =
1051
0
      bestsme < INT_MAX && !cpi->common.features.cur_frame_force_integer_mv &&
1052
0
      use_subpixel &&
1053
0
      (cpi->sf.mv_sf.simple_motion_subpel_force_stop != FULL_PEL);
1054
0
  if (scaled_ref_frame) {
1055
0
    xd->plane[AOM_PLANE_Y].pre[ref_idx] = backup_yv12;
1056
0
  }
1057
0
  if (use_subpel_search) {
1058
0
    int not_used = 0;
1059
1060
0
    SUBPEL_MOTION_SEARCH_PARAMS ms_params;
1061
0
    av1_make_default_subpel_ms_params(&ms_params, cpi, x, bsize, &ref_mv,
1062
0
                                      cost_list);
1063
    // TODO(yunqing): integrate this into av1_make_default_subpel_ms_params().
1064
0
    ms_params.forced_stop = mv_sf->simple_motion_subpel_force_stop;
1065
1066
0
    MV subpel_start_mv = get_mv_from_fullmv(&best_mv.as_fullmv);
1067
0
    assert(av1_is_subpelmv_in_range(&ms_params.mv_limits, subpel_start_mv));
1068
1069
0
    cpi->mv_search_params.find_fractional_mv_step(
1070
0
        xd, cm, &ms_params, subpel_start_mv, &best_mv_stats, &best_mv.as_mv,
1071
0
        &not_used, &x->pred_sse[ref], NULL);
1072
1073
0
    mbmi->mv[0] = best_mv;
1074
1075
    // Get a copy of the prediction output
1076
0
    av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, NULL, bsize,
1077
0
                                  AOM_PLANE_Y, AOM_PLANE_Y);
1078
0
    *var = cpi->ppi->fn_ptr[bsize].vf(
1079
0
        x->plane[0].src.buf, x->plane[0].src.stride, xd->plane[0].dst.buf,
1080
0
        xd->plane[0].dst.stride, sse);
1081
0
  } else {
1082
    // Manually convert from units of pixel to 1/8-pixels if we are not doing
1083
    // subpel search
1084
0
    convert_fullmv_to_mv(&best_mv);
1085
0
    *var = best_mv_stats.distortion;
1086
0
    *sse = best_mv_stats.sse;
1087
0
  }
1088
1089
0
  return best_mv;
1090
0
}