Coverage Report

Created: 2025-07-23 08:18

/src/aom/av1/common/mvref_common.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2016, 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 <stdlib.h>
13
14
#include "av1/common/mvref_common.h"
15
#include "av1/common/warped_motion.h"
16
17
// Although we assign 32 bit integers, all the values are strictly under 14
18
// bits.
19
static int div_mult[32] = { 0,    16384, 8192, 5461, 4096, 3276, 2730, 2340,
20
                            2048, 1820,  1638, 1489, 1365, 1260, 1170, 1092,
21
                            1024, 963,   910,  862,  819,  780,  744,  712,
22
                            682,  655,   630,  606,  585,  564,  546,  528 };
23
24
// TODO(jingning): Consider the use of lookup table for (num / den)
25
// altogether.
26
872
static AOM_INLINE void get_mv_projection(MV *output, MV ref, int num, int den) {
27
872
  den = AOMMIN(den, MAX_FRAME_DISTANCE);
28
872
  num = num > 0 ? AOMMIN(num, MAX_FRAME_DISTANCE)
29
872
                : AOMMAX(num, -MAX_FRAME_DISTANCE);
30
872
  const int mv_row =
31
872
      ROUND_POWER_OF_TWO_SIGNED(ref.row * num * div_mult[den], 14);
32
872
  const int mv_col =
33
872
      ROUND_POWER_OF_TWO_SIGNED(ref.col * num * div_mult[den], 14);
34
872
  const int clamp_max = MV_UPP - 1;
35
872
  const int clamp_min = MV_LOW + 1;
36
872
  output->row = (int16_t)clamp(mv_row, clamp_min, clamp_max);
37
872
  output->col = (int16_t)clamp(mv_col, clamp_min, clamp_max);
38
872
}
39
40
void av1_copy_frame_mvs(const AV1_COMMON *const cm,
41
                        const MB_MODE_INFO *const mi, int mi_row, int mi_col,
42
4.96k
                        int x_mis, int y_mis) {
43
4.96k
  const int frame_mvs_stride = ROUND_POWER_OF_TWO(cm->mi_params.mi_cols, 1);
44
4.96k
  MV_REF *frame_mvs =
45
4.96k
      cm->cur_frame->mvs + (mi_row >> 1) * frame_mvs_stride + (mi_col >> 1);
46
4.96k
  x_mis = ROUND_POWER_OF_TWO(x_mis, 1);
47
4.96k
  y_mis = ROUND_POWER_OF_TWO(y_mis, 1);
48
4.96k
  int w, h;
49
50
9.92k
  for (h = 0; h < y_mis; h++) {
51
4.96k
    MV_REF *mv = frame_mvs;
52
9.92k
    for (w = 0; w < x_mis; w++) {
53
4.96k
      mv->ref_frame = NONE_FRAME;
54
4.96k
      mv->mv.as_int = 0;
55
56
14.8k
      for (int idx = 0; idx < 2; ++idx) {
57
9.92k
        MV_REFERENCE_FRAME ref_frame = mi->ref_frame[idx];
58
9.92k
        if (ref_frame > INTRA_FRAME) {
59
5.45k
          int8_t ref_idx = cm->ref_frame_side[ref_frame];
60
5.45k
          if (ref_idx) continue;
61
3.80k
          if ((abs(mi->mv[idx].as_mv.row) > REFMVS_LIMIT) ||
62
3.80k
              (abs(mi->mv[idx].as_mv.col) > REFMVS_LIMIT))
63
20
            continue;
64
3.78k
          mv->ref_frame = ref_frame;
65
3.78k
          mv->mv.as_int = mi->mv[idx].as_int;
66
3.78k
        }
67
9.92k
      }
68
4.96k
      mv++;
69
4.96k
    }
70
4.96k
    frame_mvs += frame_mvs_stride;
71
4.96k
  }
72
4.96k
}
73
74
static AOM_INLINE void add_ref_mv_candidate(
75
    const MB_MODE_INFO *const candidate, const MV_REFERENCE_FRAME rf[2],
76
    uint8_t *refmv_count, uint8_t *ref_match_count, uint8_t *newmv_count,
77
    CANDIDATE_MV *ref_mv_stack, uint16_t *ref_mv_weight,
78
    int_mv *gm_mv_candidates, const WarpedMotionParams *gm_params,
79
430k
    uint16_t weight) {
80
430k
  if (!is_inter_block(candidate)) return;
81
195k
  assert(weight % 2 == 0);
82
195k
  int index, ref;
83
84
195k
  if (rf[1] == NONE_FRAME) {
85
    // single reference frame
86
587k
    for (ref = 0; ref < 2; ++ref) {
87
391k
      if (candidate->ref_frame[ref] == rf[0]) {
88
193k
        const int is_gm_block =
89
193k
            is_global_mv_block(candidate, gm_params[rf[0]].wmtype);
90
193k
        const int_mv this_refmv =
91
193k
            is_gm_block ? gm_mv_candidates[0] : get_block_mv(candidate, ref);
92
407k
        for (index = 0; index < *refmv_count; ++index) {
93
261k
          if (ref_mv_stack[index].this_mv.as_int == this_refmv.as_int) {
94
47.5k
            ref_mv_weight[index] += weight;
95
47.5k
            break;
96
47.5k
          }
97
261k
        }
98
99
        // Add a new item to the list.
100
193k
        if (index == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
101
145k
          ref_mv_stack[index].this_mv = this_refmv;
102
145k
          ref_mv_weight[index] = weight;
103
145k
          ++(*refmv_count);
104
145k
        }
105
193k
        if (have_newmv_in_inter_mode(candidate->mode)) ++*newmv_count;
106
193k
        ++*ref_match_count;
107
193k
      }
108
391k
    }
109
195k
  } else {
110
    // compound reference frame
111
272
    if (candidate->ref_frame[0] == rf[0] && candidate->ref_frame[1] == rf[1]) {
112
80
      int_mv this_refmv[2];
113
114
240
      for (ref = 0; ref < 2; ++ref) {
115
160
        if (is_global_mv_block(candidate, gm_params[rf[ref]].wmtype))
116
23
          this_refmv[ref] = gm_mv_candidates[ref];
117
137
        else
118
137
          this_refmv[ref] = get_block_mv(candidate, ref);
119
160
      }
120
121
91
      for (index = 0; index < *refmv_count; ++index) {
122
18
        if ((ref_mv_stack[index].this_mv.as_int == this_refmv[0].as_int) &&
123
18
            (ref_mv_stack[index].comp_mv.as_int == this_refmv[1].as_int)) {
124
7
          ref_mv_weight[index] += weight;
125
7
          break;
126
7
        }
127
18
      }
128
129
      // Add a new item to the list.
130
80
      if (index == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
131
73
        ref_mv_stack[index].this_mv = this_refmv[0];
132
73
        ref_mv_stack[index].comp_mv = this_refmv[1];
133
73
        ref_mv_weight[index] = weight;
134
73
        ++(*refmv_count);
135
73
      }
136
80
      if (have_newmv_in_inter_mode(candidate->mode)) ++*newmv_count;
137
80
      ++*ref_match_count;
138
80
    }
139
272
  }
140
195k
}
141
142
static AOM_INLINE void scan_row_mbmi(
143
    const AV1_COMMON *cm, const MACROBLOCKD *xd, int mi_col,
144
    const MV_REFERENCE_FRAME rf[2], int row_offset, CANDIDATE_MV *ref_mv_stack,
145
    uint16_t *ref_mv_weight, uint8_t *refmv_count, uint8_t *ref_match_count,
146
    uint8_t *newmv_count, int_mv *gm_mv_candidates, int max_row_offset,
147
139k
    int *processed_rows) {
148
139k
  int end_mi = AOMMIN(xd->width, cm->mi_params.mi_cols - mi_col);
149
139k
  end_mi = AOMMIN(end_mi, mi_size_wide[BLOCK_64X64]);
150
139k
  const int width_8x8 = mi_size_wide[BLOCK_8X8];
151
139k
  const int width_16x16 = mi_size_wide[BLOCK_16X16];
152
139k
  int col_offset = 0;
153
  // TODO(jingning): Revisit this part after cb4x4 is stable.
154
139k
  if (abs(row_offset) > 1) {
155
75.5k
    col_offset = 1;
156
75.5k
    if ((mi_col & 0x01) && xd->width < width_8x8) --col_offset;
157
75.5k
  }
158
139k
  const int use_step_16 = (xd->width >= 16);
159
139k
  MB_MODE_INFO **const candidate_mi0 = xd->mi + row_offset * xd->mi_stride;
160
161
302k
  for (int i = 0; i < end_mi;) {
162
162k
    const MB_MODE_INFO *const candidate = candidate_mi0[col_offset + i];
163
162k
    const int candidate_bsize = candidate->bsize;
164
162k
    const int n4_w = mi_size_wide[candidate_bsize];
165
162k
    int len = AOMMIN(xd->width, n4_w);
166
162k
    if (use_step_16)
167
5.23k
      len = AOMMAX(width_16x16, len);
168
157k
    else if (abs(row_offset) > 1)
169
87.3k
      len = AOMMAX(len, width_8x8);
170
171
162k
    uint16_t weight = 2;
172
162k
    if (xd->width >= width_8x8 && xd->width <= n4_w) {
173
89.5k
      uint16_t inc = AOMMIN(-max_row_offset + row_offset + 1,
174
89.5k
                            mi_size_high[candidate_bsize]);
175
      // Obtain range used in weight calculation.
176
89.5k
      weight = AOMMAX(weight, inc);
177
      // Update processed rows.
178
89.5k
      *processed_rows = inc - row_offset - 1;
179
89.5k
    }
180
181
162k
    add_ref_mv_candidate(candidate, rf, refmv_count, ref_match_count,
182
162k
                         newmv_count, ref_mv_stack, ref_mv_weight,
183
162k
                         gm_mv_candidates, cm->global_motion, len * weight);
184
185
162k
    i += len;
186
162k
  }
187
139k
}
188
189
static AOM_INLINE void scan_col_mbmi(
190
    const AV1_COMMON *cm, const MACROBLOCKD *xd, int mi_row,
191
    const MV_REFERENCE_FRAME rf[2], int col_offset, CANDIDATE_MV *ref_mv_stack,
192
    uint16_t *ref_mv_weight, uint8_t *refmv_count, uint8_t *ref_match_count,
193
    uint8_t *newmv_count, int_mv *gm_mv_candidates, int max_col_offset,
194
145k
    int *processed_cols) {
195
145k
  int end_mi = AOMMIN(xd->height, cm->mi_params.mi_rows - mi_row);
196
145k
  end_mi = AOMMIN(end_mi, mi_size_high[BLOCK_64X64]);
197
145k
  const int n8_h_8 = mi_size_high[BLOCK_8X8];
198
145k
  const int n8_h_16 = mi_size_high[BLOCK_16X16];
199
145k
  int i;
200
145k
  int row_offset = 0;
201
145k
  if (abs(col_offset) > 1) {
202
79.8k
    row_offset = 1;
203
79.8k
    if ((mi_row & 0x01) && xd->height < n8_h_8) --row_offset;
204
79.8k
  }
205
145k
  const int use_step_16 = (xd->height >= 16);
206
207
315k
  for (i = 0; i < end_mi;) {
208
170k
    const MB_MODE_INFO *const candidate =
209
170k
        xd->mi[(row_offset + i) * xd->mi_stride + col_offset];
210
170k
    const int candidate_bsize = candidate->bsize;
211
170k
    const int n4_h = mi_size_high[candidate_bsize];
212
170k
    int len = AOMMIN(xd->height, n4_h);
213
170k
    if (use_step_16)
214
6.28k
      len = AOMMAX(n8_h_16, len);
215
163k
    else if (abs(col_offset) > 1)
216
90.6k
      len = AOMMAX(len, n8_h_8);
217
218
170k
    int weight = 2;
219
170k
    if (xd->height >= n8_h_8 && xd->height <= n4_h) {
220
77.6k
      int inc = AOMMIN(-max_col_offset + col_offset + 1,
221
77.6k
                       mi_size_wide[candidate_bsize]);
222
      // Obtain range used in weight calculation.
223
77.6k
      weight = AOMMAX(weight, inc);
224
      // Update processed cols.
225
77.6k
      *processed_cols = inc - col_offset - 1;
226
77.6k
    }
227
228
170k
    add_ref_mv_candidate(candidate, rf, refmv_count, ref_match_count,
229
170k
                         newmv_count, ref_mv_stack, ref_mv_weight,
230
170k
                         gm_mv_candidates, cm->global_motion, len * weight);
231
232
170k
    i += len;
233
170k
  }
234
145k
}
235
236
static AOM_INLINE void scan_blk_mbmi(
237
    const AV1_COMMON *cm, const MACROBLOCKD *xd, const int mi_row,
238
    const int mi_col, const MV_REFERENCE_FRAME rf[2], int row_offset,
239
    int col_offset, CANDIDATE_MV *ref_mv_stack, uint16_t *ref_mv_weight,
240
    uint8_t *ref_match_count, uint8_t *newmv_count, int_mv *gm_mv_candidates,
241
111k
    uint8_t *refmv_count) {
242
111k
  const TileInfo *const tile = &xd->tile;
243
111k
  POSITION mi_pos;
244
245
111k
  mi_pos.row = row_offset;
246
111k
  mi_pos.col = col_offset;
247
248
111k
  if (is_inside(tile, mi_col, mi_row, &mi_pos)) {
249
97.3k
    const MB_MODE_INFO *const candidate =
250
97.3k
        xd->mi[mi_pos.row * xd->mi_stride + mi_pos.col];
251
97.3k
    const int len = mi_size_wide[BLOCK_8X8];
252
253
97.3k
    add_ref_mv_candidate(candidate, rf, refmv_count, ref_match_count,
254
97.3k
                         newmv_count, ref_mv_stack, ref_mv_weight,
255
97.3k
                         gm_mv_candidates, cm->global_motion, 2 * len);
256
97.3k
  }  // Analyze a single 8x8 block motion information.
257
111k
}
258
259
static int has_top_right(const AV1_COMMON *cm, const MACROBLOCKD *xd,
260
72.0k
                         int mi_row, int mi_col, int bs) {
261
72.0k
  const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size];
262
72.0k
  const int mask_row = mi_row & (sb_mi_size - 1);
263
72.0k
  const int mask_col = mi_col & (sb_mi_size - 1);
264
265
72.0k
  if (bs > mi_size_wide[BLOCK_64X64]) return 0;
266
267
  // In a split partition all apart from the bottom right has a top right
268
71.4k
  int has_tr = !((mask_row & bs) && (mask_col & bs));
269
270
  // bs > 0 and bs is a power of 2
271
71.4k
  assert(bs > 0 && !(bs & (bs - 1)));
272
273
  // For each 4x4 group of blocks, when the bottom right is decoded the blocks
274
  // to the right have not been decoded therefore the bottom right does
275
  // not have a top right
276
104k
  while (bs < sb_mi_size) {
277
100k
    if (mask_col & bs) {
278
42.5k
      if ((mask_col & (2 * bs)) && (mask_row & (2 * bs))) {
279
9.61k
        has_tr = 0;
280
9.61k
        break;
281
9.61k
      }
282
58.2k
    } else {
283
58.2k
      break;
284
58.2k
    }
285
32.9k
    bs <<= 1;
286
32.9k
  }
287
288
  // In a VERTICAL or VERTICAL_4 partition, all partition before the last one
289
  // always have a top right (as the block above will have been decoded).
290
71.4k
  if (xd->width < xd->height) {
291
14.8k
    if (!xd->is_last_vertical_rect) has_tr = 1;
292
14.8k
  }
293
294
  // In a HORIZONTAL or HORIZONTAL_4 partition, partitions after the first one
295
  // never have a top right (as the block to the right won't have been decoded).
296
71.4k
  if (xd->width > xd->height) {
297
23.5k
    if (!xd->is_first_horizontal_rect) has_tr = 0;
298
23.5k
  }
299
300
  // The bottom left square of a Vertical A (in the old format) does
301
  // not have a top right as it is decoded before the right hand
302
  // rectangle of the partition
303
71.4k
  if (xd->mi[0]->partition == PARTITION_VERT_A) {
304
2.58k
    if (xd->width == xd->height)
305
1.64k
      if (mask_row & bs) has_tr = 0;
306
2.58k
  }
307
308
71.4k
  return has_tr;
309
71.4k
}
310
311
static int check_sb_border(const int mi_row, const int mi_col,
312
2.53k
                           const int row_offset, const int col_offset) {
313
2.53k
  const int sb_mi_size = mi_size_wide[BLOCK_64X64];
314
2.53k
  const int row = mi_row & (sb_mi_size - 1);
315
2.53k
  const int col = mi_col & (sb_mi_size - 1);
316
317
2.53k
  if (row + row_offset < 0 || row + row_offset >= sb_mi_size ||
318
2.53k
      col + col_offset < 0 || col + col_offset >= sb_mi_size)
319
845
    return 0;
320
321
1.69k
  return 1;
322
2.53k
}
323
324
static int add_tpl_ref_mv(const AV1_COMMON *cm, const MACROBLOCKD *xd,
325
                          int mi_row, int mi_col, MV_REFERENCE_FRAME ref_frame,
326
                          int blk_row, int blk_col, int_mv *gm_mv_candidates,
327
                          uint8_t *const refmv_count,
328
                          CANDIDATE_MV ref_mv_stack[MAX_REF_MV_STACK_SIZE],
329
                          uint16_t ref_mv_weight[MAX_REF_MV_STACK_SIZE],
330
3.87k
                          int16_t *mode_context) {
331
3.87k
  POSITION mi_pos;
332
3.87k
  mi_pos.row = (mi_row & 0x01) ? blk_row : blk_row + 1;
333
3.87k
  mi_pos.col = (mi_col & 0x01) ? blk_col : blk_col + 1;
334
335
3.87k
  if (!is_inside(&xd->tile, mi_col, mi_row, &mi_pos)) return 0;
336
337
2.18k
  const TPL_MV_REF *prev_frame_mvs =
338
2.18k
      cm->tpl_mvs +
339
2.18k
      ((mi_row + mi_pos.row) >> 1) * (cm->mi_params.mi_stride >> 1) +
340
2.18k
      ((mi_col + mi_pos.col) >> 1);
341
2.18k
  if (prev_frame_mvs->mfmv0.as_int == INVALID_MV) return 0;
342
343
418
  MV_REFERENCE_FRAME rf[2];
344
418
  av1_set_ref_frame(rf, ref_frame);
345
346
418
  const uint16_t weight_unit = 1;  // mi_size_wide[BLOCK_8X8];
347
418
  const int cur_frame_index = cm->cur_frame->order_hint;
348
418
  const RefCntBuffer *const buf_0 = get_ref_frame_buf(cm, rf[0]);
349
418
  const int frame0_index = buf_0->order_hint;
350
418
  const int cur_offset_0 = get_relative_dist(&cm->seq_params->order_hint_info,
351
418
                                             cur_frame_index, frame0_index);
352
418
  int idx;
353
418
  const int allow_high_precision_mv = cm->features.allow_high_precision_mv;
354
418
  const int force_integer_mv = cm->features.cur_frame_force_integer_mv;
355
356
418
  int_mv this_refmv;
357
418
  get_mv_projection(&this_refmv.as_mv, prev_frame_mvs->mfmv0.as_mv,
358
418
                    cur_offset_0, prev_frame_mvs->ref_frame_offset);
359
418
  lower_mv_precision(&this_refmv.as_mv, allow_high_precision_mv,
360
418
                     force_integer_mv);
361
362
418
  if (rf[1] == NONE_FRAME) {
363
356
    if (blk_row == 0 && blk_col == 0) {
364
356
      if (abs(this_refmv.as_mv.row - gm_mv_candidates[0].as_mv.row) >= 16 ||
365
356
          abs(this_refmv.as_mv.col - gm_mv_candidates[0].as_mv.col) >= 16)
366
86
        mode_context[ref_frame] |= (1 << GLOBALMV_OFFSET);
367
356
    }
368
369
523
    for (idx = 0; idx < *refmv_count; ++idx)
370
219
      if (this_refmv.as_int == ref_mv_stack[idx].this_mv.as_int) break;
371
372
356
    if (idx < *refmv_count) ref_mv_weight[idx] += 2 * weight_unit;
373
374
356
    if (idx == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
375
304
      ref_mv_stack[idx].this_mv.as_int = this_refmv.as_int;
376
304
      ref_mv_weight[idx] = 2 * weight_unit;
377
304
      ++(*refmv_count);
378
304
    }
379
356
  } else {
380
    // Process compound inter mode
381
62
    const RefCntBuffer *const buf_1 = get_ref_frame_buf(cm, rf[1]);
382
62
    const int frame1_index = buf_1->order_hint;
383
62
    const int cur_offset_1 = get_relative_dist(&cm->seq_params->order_hint_info,
384
62
                                               cur_frame_index, frame1_index);
385
62
    int_mv comp_refmv;
386
62
    get_mv_projection(&comp_refmv.as_mv, prev_frame_mvs->mfmv0.as_mv,
387
62
                      cur_offset_1, prev_frame_mvs->ref_frame_offset);
388
62
    lower_mv_precision(&comp_refmv.as_mv, allow_high_precision_mv,
389
62
                       force_integer_mv);
390
391
62
    if (blk_row == 0 && blk_col == 0) {
392
62
      if (abs(this_refmv.as_mv.row - gm_mv_candidates[0].as_mv.row) >= 16 ||
393
62
          abs(this_refmv.as_mv.col - gm_mv_candidates[0].as_mv.col) >= 16 ||
394
62
          abs(comp_refmv.as_mv.row - gm_mv_candidates[1].as_mv.row) >= 16 ||
395
62
          abs(comp_refmv.as_mv.col - gm_mv_candidates[1].as_mv.col) >= 16)
396
34
        mode_context[ref_frame] |= (1 << GLOBALMV_OFFSET);
397
62
    }
398
399
62
    for (idx = 0; idx < *refmv_count; ++idx) {
400
0
      if (this_refmv.as_int == ref_mv_stack[idx].this_mv.as_int &&
401
0
          comp_refmv.as_int == ref_mv_stack[idx].comp_mv.as_int)
402
0
        break;
403
0
    }
404
405
62
    if (idx < *refmv_count) ref_mv_weight[idx] += 2 * weight_unit;
406
407
62
    if (idx == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
408
62
      ref_mv_stack[idx].this_mv.as_int = this_refmv.as_int;
409
62
      ref_mv_stack[idx].comp_mv.as_int = comp_refmv.as_int;
410
62
      ref_mv_weight[idx] = 2 * weight_unit;
411
62
      ++(*refmv_count);
412
62
    }
413
62
  }
414
415
418
  return 1;
416
2.18k
}
417
418
static AOM_INLINE void process_compound_ref_mv_candidate(
419
    const MB_MODE_INFO *const candidate, const AV1_COMMON *const cm,
420
    const MV_REFERENCE_FRAME *const rf, int_mv ref_id[2][2],
421
168
    int ref_id_count[2], int_mv ref_diff[2][2], int ref_diff_count[2]) {
422
504
  for (int rf_idx = 0; rf_idx < 2; ++rf_idx) {
423
336
    MV_REFERENCE_FRAME can_rf = candidate->ref_frame[rf_idx];
424
425
1.00k
    for (int cmp_idx = 0; cmp_idx < 2; ++cmp_idx) {
426
672
      if (can_rf == rf[cmp_idx] && ref_id_count[cmp_idx] < 2) {
427
207
        ref_id[cmp_idx][ref_id_count[cmp_idx]] = candidate->mv[rf_idx];
428
207
        ++ref_id_count[cmp_idx];
429
465
      } else if (can_rf > INTRA_FRAME && ref_diff_count[cmp_idx] < 2) {
430
275
        int_mv this_mv = candidate->mv[rf_idx];
431
275
        if (cm->ref_frame_sign_bias[can_rf] !=
432
275
            cm->ref_frame_sign_bias[rf[cmp_idx]]) {
433
0
          this_mv.as_mv.row = -this_mv.as_mv.row;
434
0
          this_mv.as_mv.col = -this_mv.as_mv.col;
435
0
        }
436
275
        ref_diff[cmp_idx][ref_diff_count[cmp_idx]] = this_mv;
437
275
        ++ref_diff_count[cmp_idx];
438
275
      }
439
672
    }
440
336
  }
441
168
}
442
443
static AOM_INLINE void process_single_ref_mv_candidate(
444
    const MB_MODE_INFO *const candidate, const AV1_COMMON *const cm,
445
    MV_REFERENCE_FRAME ref_frame, uint8_t *const refmv_count,
446
    CANDIDATE_MV ref_mv_stack[MAX_REF_MV_STACK_SIZE],
447
54.6k
    uint16_t ref_mv_weight[MAX_REF_MV_STACK_SIZE]) {
448
163k
  for (int rf_idx = 0; rf_idx < 2; ++rf_idx) {
449
109k
    if (candidate->ref_frame[rf_idx] > INTRA_FRAME) {
450
3.39k
      int_mv this_mv = candidate->mv[rf_idx];
451
3.39k
      if (cm->ref_frame_sign_bias[candidate->ref_frame[rf_idx]] !=
452
3.39k
          cm->ref_frame_sign_bias[ref_frame]) {
453
131
        this_mv.as_mv.row = -this_mv.as_mv.row;
454
131
        this_mv.as_mv.col = -this_mv.as_mv.col;
455
131
      }
456
3.39k
      int stack_idx;
457
3.84k
      for (stack_idx = 0; stack_idx < *refmv_count; ++stack_idx) {
458
2.76k
        const int_mv stack_mv = ref_mv_stack[stack_idx].this_mv;
459
2.76k
        if (this_mv.as_int == stack_mv.as_int) break;
460
2.76k
      }
461
462
3.39k
      if (stack_idx == *refmv_count) {
463
1.08k
        ref_mv_stack[stack_idx].this_mv = this_mv;
464
465
        // TODO(jingning): Set an arbitrary small number here. The weight
466
        // doesn't matter as long as it is properly initialized.
467
1.08k
        ref_mv_weight[stack_idx] = 2;
468
1.08k
        ++(*refmv_count);
469
1.08k
      }
470
3.39k
    }
471
109k
  }
472
54.6k
}
473
474
static AOM_INLINE void setup_ref_mv_list(
475
    const AV1_COMMON *cm, const MACROBLOCKD *xd, MV_REFERENCE_FRAME ref_frame,
476
    uint8_t *const refmv_count,
477
    CANDIDATE_MV ref_mv_stack[MAX_REF_MV_STACK_SIZE],
478
    uint16_t ref_mv_weight[MAX_REF_MV_STACK_SIZE],
479
    int_mv mv_ref_list[MAX_MV_REF_CANDIDATES], int_mv *gm_mv_candidates,
480
70.7k
    int mi_row, int mi_col, int16_t *mode_context) {
481
70.7k
  const int bs = AOMMAX(xd->width, xd->height);
482
70.7k
  const int has_tr = has_top_right(cm, xd, mi_row, mi_col, bs);
483
70.7k
  MV_REFERENCE_FRAME rf[2];
484
485
70.7k
  const TileInfo *const tile = &xd->tile;
486
70.7k
  int max_row_offset = 0, max_col_offset = 0;
487
70.7k
  const int row_adj = (xd->height < mi_size_high[BLOCK_8X8]) && (mi_row & 0x01);
488
70.7k
  const int col_adj = (xd->width < mi_size_wide[BLOCK_8X8]) && (mi_col & 0x01);
489
70.7k
  int processed_rows = 0;
490
70.7k
  int processed_cols = 0;
491
492
70.7k
  av1_set_ref_frame(rf, ref_frame);
493
70.7k
  mode_context[ref_frame] = 0;
494
70.7k
  *refmv_count = 0;
495
496
  // Find valid maximum row/col offset.
497
70.7k
  if (xd->up_available) {
498
64.1k
    max_row_offset = -(MVREF_ROW_COLS << 1) + row_adj;
499
500
64.1k
    if (xd->height < mi_size_high[BLOCK_8X8])
501
17.5k
      max_row_offset = -(2 << 1) + row_adj;
502
503
64.1k
    max_row_offset = find_valid_row_offset(tile, mi_row, max_row_offset);
504
64.1k
  }
505
506
70.7k
  if (xd->left_available) {
507
66.0k
    max_col_offset = -(MVREF_ROW_COLS << 1) + col_adj;
508
509
66.0k
    if (xd->width < mi_size_wide[BLOCK_8X8])
510
13.1k
      max_col_offset = -(2 << 1) + col_adj;
511
512
66.0k
    max_col_offset = find_valid_col_offset(tile, mi_col, max_col_offset);
513
66.0k
  }
514
515
70.7k
  uint8_t col_match_count = 0;
516
70.7k
  uint8_t row_match_count = 0;
517
70.7k
  uint8_t newmv_count = 0;
518
519
  // Scan the first above row mode info. row_offset = -1;
520
70.7k
  if (abs(max_row_offset) >= 1)
521
64.1k
    scan_row_mbmi(cm, xd, mi_col, rf, -1, ref_mv_stack, ref_mv_weight,
522
64.1k
                  refmv_count, &row_match_count, &newmv_count, gm_mv_candidates,
523
64.1k
                  max_row_offset, &processed_rows);
524
  // Scan the first left column mode info. col_offset = -1;
525
70.7k
  if (abs(max_col_offset) >= 1)
526
66.0k
    scan_col_mbmi(cm, xd, mi_row, rf, -1, ref_mv_stack, ref_mv_weight,
527
66.0k
                  refmv_count, &col_match_count, &newmv_count, gm_mv_candidates,
528
66.0k
                  max_col_offset, &processed_cols);
529
  // Check top-right boundary
530
70.7k
  if (has_tr)
531
41.2k
    scan_blk_mbmi(cm, xd, mi_row, mi_col, rf, -1, xd->width, ref_mv_stack,
532
41.2k
                  ref_mv_weight, &row_match_count, &newmv_count,
533
41.2k
                  gm_mv_candidates, refmv_count);
534
535
70.7k
  const uint8_t nearest_match = (row_match_count > 0) + (col_match_count > 0);
536
70.7k
  const uint8_t nearest_refmv_count = *refmv_count;
537
538
  // TODO(yunqing): for comp_search, do it for all 3 cases.
539
144k
  for (int idx = 0; idx < nearest_refmv_count; ++idx)
540
74.0k
    ref_mv_weight[idx] += REF_CAT_LEVEL;
541
542
70.7k
  if (cm->features.allow_ref_frame_mvs) {
543
2.18k
    int is_available = 0;
544
2.18k
    const int voffset = AOMMAX(mi_size_high[BLOCK_8X8], xd->height);
545
2.18k
    const int hoffset = AOMMAX(mi_size_wide[BLOCK_8X8], xd->width);
546
2.18k
    const int blk_row_end = AOMMIN(xd->height, mi_size_high[BLOCK_64X64]);
547
2.18k
    const int blk_col_end = AOMMIN(xd->width, mi_size_wide[BLOCK_64X64]);
548
549
2.18k
    const int tpl_sample_pos[3][2] = {
550
2.18k
      { voffset, -2 },
551
2.18k
      { voffset, hoffset },
552
2.18k
      { voffset - 2, hoffset },
553
2.18k
    };
554
2.18k
    const int allow_extension = (xd->height >= mi_size_high[BLOCK_8X8]) &&
555
2.18k
                                (xd->height < mi_size_high[BLOCK_64X64]) &&
556
2.18k
                                (xd->width >= mi_size_wide[BLOCK_8X8]) &&
557
2.18k
                                (xd->width < mi_size_wide[BLOCK_64X64]);
558
559
2.18k
    const int step_h = (xd->height >= mi_size_high[BLOCK_64X64])
560
2.18k
                           ? mi_size_high[BLOCK_16X16]
561
2.18k
                           : mi_size_high[BLOCK_8X8];
562
2.18k
    const int step_w = (xd->width >= mi_size_wide[BLOCK_64X64])
563
2.18k
                           ? mi_size_wide[BLOCK_16X16]
564
2.18k
                           : mi_size_wide[BLOCK_8X8];
565
566
4.37k
    for (int blk_row = 0; blk_row < blk_row_end; blk_row += step_h) {
567
4.37k
      for (int blk_col = 0; blk_col < blk_col_end; blk_col += step_w) {
568
2.18k
        int ret = add_tpl_ref_mv(cm, xd, mi_row, mi_col, ref_frame, blk_row,
569
2.18k
                                 blk_col, gm_mv_candidates, refmv_count,
570
2.18k
                                 ref_mv_stack, ref_mv_weight, mode_context);
571
2.18k
        if (blk_row == 0 && blk_col == 0) is_available = ret;
572
2.18k
      }
573
2.18k
    }
574
575
2.18k
    if (is_available == 0) mode_context[ref_frame] |= (1 << GLOBALMV_OFFSET);
576
577
4.72k
    for (int i = 0; i < 3 && allow_extension; ++i) {
578
2.53k
      const int blk_row = tpl_sample_pos[i][0];
579
2.53k
      const int blk_col = tpl_sample_pos[i][1];
580
581
2.53k
      if (!check_sb_border(mi_row, mi_col, blk_row, blk_col)) continue;
582
1.69k
      add_tpl_ref_mv(cm, xd, mi_row, mi_col, ref_frame, blk_row, blk_col,
583
1.69k
                     gm_mv_candidates, refmv_count, ref_mv_stack, ref_mv_weight,
584
1.69k
                     mode_context);
585
1.69k
    }
586
2.18k
  }
587
588
70.7k
  uint8_t dummy_newmv_count = 0;
589
590
  // Scan the second outer area.
591
70.7k
  scan_blk_mbmi(cm, xd, mi_row, mi_col, rf, -1, -1, ref_mv_stack, ref_mv_weight,
592
70.7k
                &row_match_count, &dummy_newmv_count, gm_mv_candidates,
593
70.7k
                refmv_count);
594
595
212k
  for (int idx = 2; idx <= MVREF_ROW_COLS; ++idx) {
596
141k
    const int row_offset = -(idx << 1) + 1 + row_adj;
597
141k
    const int col_offset = -(idx << 1) + 1 + col_adj;
598
599
141k
    if (abs(row_offset) <= abs(max_row_offset) &&
600
141k
        abs(row_offset) > processed_rows)
601
75.5k
      scan_row_mbmi(cm, xd, mi_col, rf, row_offset, ref_mv_stack, ref_mv_weight,
602
75.5k
                    refmv_count, &row_match_count, &dummy_newmv_count,
603
75.5k
                    gm_mv_candidates, max_row_offset, &processed_rows);
604
605
141k
    if (abs(col_offset) <= abs(max_col_offset) &&
606
141k
        abs(col_offset) > processed_cols)
607
79.8k
      scan_col_mbmi(cm, xd, mi_row, rf, col_offset, ref_mv_stack, ref_mv_weight,
608
79.8k
                    refmv_count, &col_match_count, &dummy_newmv_count,
609
79.8k
                    gm_mv_candidates, max_col_offset, &processed_cols);
610
141k
  }
611
612
70.7k
  const uint8_t ref_match_count = (row_match_count > 0) + (col_match_count > 0);
613
614
70.7k
  switch (nearest_match) {
615
24.1k
    case 0:
616
24.1k
      if (ref_match_count >= 1) mode_context[ref_frame] |= 1;
617
24.1k
      if (ref_match_count == 1)
618
6.55k
        mode_context[ref_frame] |= (1 << REFMV_OFFSET);
619
17.5k
      else if (ref_match_count >= 2)
620
2.26k
        mode_context[ref_frame] |= (2 << REFMV_OFFSET);
621
24.1k
      break;
622
27.0k
    case 1:
623
27.0k
      mode_context[ref_frame] |= (newmv_count > 0) ? 2 : 3;
624
27.0k
      if (ref_match_count == 1)
625
14.3k
        mode_context[ref_frame] |= (3 << REFMV_OFFSET);
626
12.7k
      else if (ref_match_count >= 2)
627
12.7k
        mode_context[ref_frame] |= (4 << REFMV_OFFSET);
628
27.0k
      break;
629
19.4k
    case 2:
630
19.4k
    default:
631
19.4k
      if (newmv_count >= 1)
632
296
        mode_context[ref_frame] |= 4;
633
19.1k
      else
634
19.1k
        mode_context[ref_frame] |= 5;
635
636
19.4k
      mode_context[ref_frame] |= (5 << REFMV_OFFSET);
637
19.4k
      break;
638
70.7k
  }
639
640
  // Rank the likelihood and assign nearest and near mvs.
641
70.7k
  int len = nearest_refmv_count;
642
126k
  while (len > 0) {
643
56.2k
    int nr_len = 0;
644
87.6k
    for (int idx = 1; idx < len; ++idx) {
645
31.4k
      if (ref_mv_weight[idx - 1] < ref_mv_weight[idx]) {
646
11.1k
        const CANDIDATE_MV tmp_mv = ref_mv_stack[idx - 1];
647
11.1k
        const uint16_t tmp_ref_mv_weight = ref_mv_weight[idx - 1];
648
11.1k
        ref_mv_stack[idx - 1] = ref_mv_stack[idx];
649
11.1k
        ref_mv_stack[idx] = tmp_mv;
650
11.1k
        ref_mv_weight[idx - 1] = ref_mv_weight[idx];
651
11.1k
        ref_mv_weight[idx] = tmp_ref_mv_weight;
652
11.1k
        nr_len = idx;
653
11.1k
      }
654
31.4k
    }
655
56.2k
    len = nr_len;
656
56.2k
  }
657
658
70.7k
  len = *refmv_count;
659
118k
  while (len > nearest_refmv_count) {
660
48.2k
    int nr_len = nearest_refmv_count;
661
82.5k
    for (int idx = nearest_refmv_count + 1; idx < len; ++idx) {
662
34.2k
      if (ref_mv_weight[idx - 1] < ref_mv_weight[idx]) {
663
8.58k
        const CANDIDATE_MV tmp_mv = ref_mv_stack[idx - 1];
664
8.58k
        const uint16_t tmp_ref_mv_weight = ref_mv_weight[idx - 1];
665
8.58k
        ref_mv_stack[idx - 1] = ref_mv_stack[idx];
666
8.58k
        ref_mv_stack[idx] = tmp_mv;
667
8.58k
        ref_mv_weight[idx - 1] = ref_mv_weight[idx];
668
8.58k
        ref_mv_weight[idx] = tmp_ref_mv_weight;
669
8.58k
        nr_len = idx;
670
8.58k
      }
671
34.2k
    }
672
48.2k
    len = nr_len;
673
48.2k
  }
674
675
70.7k
  int mi_width = AOMMIN(mi_size_wide[BLOCK_64X64], xd->width);
676
70.7k
  mi_width = AOMMIN(mi_width, cm->mi_params.mi_cols - mi_col);
677
70.7k
  int mi_height = AOMMIN(mi_size_high[BLOCK_64X64], xd->height);
678
70.7k
  mi_height = AOMMIN(mi_height, cm->mi_params.mi_rows - mi_row);
679
70.7k
  const int mi_size = AOMMIN(mi_width, mi_height);
680
70.7k
  if (rf[1] > NONE_FRAME) {
681
    // TODO(jingning, yunqing): Refactor and consolidate the compound and
682
    // single reference frame modes. Reduce unnecessary redundancy.
683
1.17k
    if (*refmv_count < MAX_MV_REF_CANDIDATES) {
684
1.16k
      int_mv ref_id[2][2], ref_diff[2][2];
685
1.16k
      int ref_id_count[2] = { 0 }, ref_diff_count[2] = { 0 };
686
687
1.16k
      for (int idx = 0; abs(max_row_offset) >= 1 && idx < mi_size;) {
688
0
        const MB_MODE_INFO *const candidate = xd->mi[-xd->mi_stride + idx];
689
0
        process_compound_ref_mv_candidate(
690
0
            candidate, cm, rf, ref_id, ref_id_count, ref_diff, ref_diff_count);
691
0
        idx += mi_size_wide[candidate->bsize];
692
0
      }
693
694
1.33k
      for (int idx = 0; abs(max_col_offset) >= 1 && idx < mi_size;) {
695
168
        const MB_MODE_INFO *const candidate = xd->mi[idx * xd->mi_stride - 1];
696
168
        process_compound_ref_mv_candidate(
697
168
            candidate, cm, rf, ref_id, ref_id_count, ref_diff, ref_diff_count);
698
168
        idx += mi_size_high[candidate->bsize];
699
168
      }
700
701
      // Build up the compound mv predictor
702
1.16k
      int_mv comp_list[MAX_MV_REF_CANDIDATES][2];
703
704
3.48k
      for (int idx = 0; idx < 2; ++idx) {
705
2.32k
        int comp_idx = 0;
706
2.32k
        for (int list_idx = 0;
707
2.53k
             list_idx < ref_id_count[idx] && comp_idx < MAX_MV_REF_CANDIDATES;
708
2.32k
             ++list_idx, ++comp_idx)
709
207
          comp_list[comp_idx][idx] = ref_id[idx][list_idx];
710
2.32k
        for (int list_idx = 0;
711
2.59k
             list_idx < ref_diff_count[idx] && comp_idx < MAX_MV_REF_CANDIDATES;
712
2.32k
             ++list_idx, ++comp_idx)
713
275
          comp_list[comp_idx][idx] = ref_diff[idx][list_idx];
714
6.49k
        for (; comp_idx < MAX_MV_REF_CANDIDATES; ++comp_idx)
715
4.16k
          comp_list[comp_idx][idx] = gm_mv_candidates[idx];
716
2.32k
      }
717
718
1.16k
      if (*refmv_count) {
719
113
        assert(*refmv_count == 1);
720
113
        if (comp_list[0][0].as_int == ref_mv_stack[0].this_mv.as_int &&
721
113
            comp_list[0][1].as_int == ref_mv_stack[0].comp_mv.as_int) {
722
48
          ref_mv_stack[*refmv_count].this_mv = comp_list[1][0];
723
48
          ref_mv_stack[*refmv_count].comp_mv = comp_list[1][1];
724
65
        } else {
725
65
          ref_mv_stack[*refmv_count].this_mv = comp_list[0][0];
726
65
          ref_mv_stack[*refmv_count].comp_mv = comp_list[0][1];
727
65
        }
728
113
        ref_mv_weight[*refmv_count] = 2;
729
113
        ++*refmv_count;
730
1.04k
      } else {
731
3.14k
        for (int idx = 0; idx < MAX_MV_REF_CANDIDATES; ++idx) {
732
2.09k
          ref_mv_stack[*refmv_count].this_mv = comp_list[idx][0];
733
2.09k
          ref_mv_stack[*refmv_count].comp_mv = comp_list[idx][1];
734
2.09k
          ref_mv_weight[*refmv_count] = 2;
735
2.09k
          ++*refmv_count;
736
2.09k
        }
737
1.04k
      }
738
1.16k
    }
739
740
1.17k
    assert(*refmv_count >= 2);
741
742
3.51k
    for (int idx = 0; idx < *refmv_count; ++idx) {
743
2.34k
      clamp_mv_ref(&ref_mv_stack[idx].this_mv.as_mv, xd->width << MI_SIZE_LOG2,
744
2.34k
                   xd->height << MI_SIZE_LOG2, xd);
745
2.34k
      clamp_mv_ref(&ref_mv_stack[idx].comp_mv.as_mv, xd->width << MI_SIZE_LOG2,
746
2.34k
                   xd->height << MI_SIZE_LOG2, xd);
747
2.34k
    }
748
69.5k
  } else {
749
    // Handle single reference frame extension
750
95.5k
    for (int idx = 0; abs(max_row_offset) >= 1 && idx < mi_size &&
751
95.5k
                      *refmv_count < MAX_MV_REF_CANDIDATES;) {
752
26.0k
      const MB_MODE_INFO *const candidate = xd->mi[-xd->mi_stride + idx];
753
26.0k
      process_single_ref_mv_candidate(candidate, cm, ref_frame, refmv_count,
754
26.0k
                                      ref_mv_stack, ref_mv_weight);
755
26.0k
      idx += mi_size_wide[candidate->bsize];
756
26.0k
    }
757
758
98.1k
    for (int idx = 0; abs(max_col_offset) >= 1 && idx < mi_size &&
759
98.1k
                      *refmv_count < MAX_MV_REF_CANDIDATES;) {
760
28.5k
      const MB_MODE_INFO *const candidate = xd->mi[idx * xd->mi_stride - 1];
761
28.5k
      process_single_ref_mv_candidate(candidate, cm, ref_frame, refmv_count,
762
28.5k
                                      ref_mv_stack, ref_mv_weight);
763
28.5k
      idx += mi_size_high[candidate->bsize];
764
28.5k
    }
765
766
216k
    for (int idx = 0; idx < *refmv_count; ++idx) {
767
146k
      clamp_mv_ref(&ref_mv_stack[idx].this_mv.as_mv, xd->width << MI_SIZE_LOG2,
768
146k
                   xd->height << MI_SIZE_LOG2, xd);
769
146k
    }
770
771
69.5k
    if (mv_ref_list != NULL) {
772
112k
      for (int idx = *refmv_count; idx < MAX_MV_REF_CANDIDATES; ++idx)
773
42.9k
        mv_ref_list[idx].as_int = gm_mv_candidates[0].as_int;
774
775
165k
      for (int idx = 0; idx < AOMMIN(MAX_MV_REF_CANDIDATES, *refmv_count);
776
96.1k
           ++idx) {
777
96.1k
        mv_ref_list[idx].as_int = ref_mv_stack[idx].this_mv.as_int;
778
96.1k
      }
779
69.5k
    }
780
69.5k
  }
781
70.7k
}
782
783
void av1_find_mv_refs(const AV1_COMMON *cm, const MACROBLOCKD *xd,
784
                      MB_MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
785
                      uint8_t ref_mv_count[MODE_CTX_REF_FRAMES],
786
                      CANDIDATE_MV ref_mv_stack[][MAX_REF_MV_STACK_SIZE],
787
                      uint16_t ref_mv_weight[][MAX_REF_MV_STACK_SIZE],
788
                      int_mv mv_ref_list[][MAX_MV_REF_CANDIDATES],
789
70.7k
                      int_mv *global_mvs, int16_t *mode_context) {
790
70.7k
  const int mi_row = xd->mi_row;
791
70.7k
  const int mi_col = xd->mi_col;
792
70.7k
  int_mv gm_mv[2];
793
794
70.7k
  if (ref_frame == INTRA_FRAME) {
795
63.5k
    gm_mv[0].as_int = gm_mv[1].as_int = 0;
796
63.5k
    if (global_mvs != NULL) {
797
0
      global_mvs[ref_frame].as_int = INVALID_MV;
798
0
    }
799
63.5k
  } else {
800
7.19k
    const BLOCK_SIZE bsize = mi->bsize;
801
7.19k
    const int allow_high_precision_mv = cm->features.allow_high_precision_mv;
802
7.19k
    const int force_integer_mv = cm->features.cur_frame_force_integer_mv;
803
7.19k
    if (ref_frame < REF_FRAMES) {
804
6.02k
      gm_mv[0] = gm_get_motion_vector(&cm->global_motion[ref_frame],
805
6.02k
                                      allow_high_precision_mv, bsize, mi_col,
806
6.02k
                                      mi_row, force_integer_mv);
807
6.02k
      gm_mv[1].as_int = 0;
808
6.02k
      if (global_mvs != NULL) global_mvs[ref_frame] = gm_mv[0];
809
6.02k
    } else {
810
1.17k
      MV_REFERENCE_FRAME rf[2];
811
1.17k
      av1_set_ref_frame(rf, ref_frame);
812
1.17k
      gm_mv[0] = gm_get_motion_vector(&cm->global_motion[rf[0]],
813
1.17k
                                      allow_high_precision_mv, bsize, mi_col,
814
1.17k
                                      mi_row, force_integer_mv);
815
1.17k
      gm_mv[1] = gm_get_motion_vector(&cm->global_motion[rf[1]],
816
1.17k
                                      allow_high_precision_mv, bsize, mi_col,
817
1.17k
                                      mi_row, force_integer_mv);
818
1.17k
    }
819
7.19k
  }
820
821
70.7k
  setup_ref_mv_list(cm, xd, ref_frame, &ref_mv_count[ref_frame],
822
70.7k
                    ref_mv_stack[ref_frame], ref_mv_weight[ref_frame],
823
70.7k
                    mv_ref_list ? mv_ref_list[ref_frame] : NULL, gm_mv, mi_row,
824
70.7k
                    mi_col, mode_context);
825
70.7k
}
826
827
void av1_find_best_ref_mvs(int allow_hp, int_mv *mvlist, int_mv *nearest_mv,
828
67.6k
                           int_mv *near_mv, int is_integer) {
829
67.6k
  int i;
830
  // Make sure all the candidates are properly clamped etc
831
203k
  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
832
135k
    lower_mv_precision(&mvlist[i].as_mv, allow_hp, is_integer);
833
135k
  }
834
67.6k
  *nearest_mv = mvlist[0];
835
67.6k
  *near_mv = mvlist[1];
836
67.6k
}
837
838
32.5k
void av1_setup_frame_buf_refs(AV1_COMMON *cm) {
839
32.5k
  cm->cur_frame->order_hint = cm->current_frame.order_hint;
840
32.5k
  cm->cur_frame->display_order_hint = cm->current_frame.display_order_hint;
841
#if CONFIG_FRAME_PARALLEL_ENCODE
842
  cm->cur_frame->pyramid_level = cm->current_frame.pyramid_level;
843
#endif  // CONFIG_FRAME_PARALLEL_ENCODE
844
32.5k
  MV_REFERENCE_FRAME ref_frame;
845
260k
  for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
846
227k
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
847
227k
    if (buf != NULL) {
848
27.0k
      cm->cur_frame->ref_order_hints[ref_frame - LAST_FRAME] = buf->order_hint;
849
27.0k
      cm->cur_frame->ref_display_order_hint[ref_frame - LAST_FRAME] =
850
27.0k
          buf->display_order_hint;
851
27.0k
    }
852
227k
  }
853
32.5k
}
854
855
32.5k
void av1_setup_frame_sign_bias(AV1_COMMON *cm) {
856
32.5k
  MV_REFERENCE_FRAME ref_frame;
857
260k
  for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
858
227k
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
859
227k
    if (cm->seq_params->order_hint_info.enable_order_hint && buf != NULL) {
860
23.1k
      const int ref_order_hint = buf->order_hint;
861
23.1k
      cm->ref_frame_sign_bias[ref_frame] =
862
23.1k
          (get_relative_dist(&cm->seq_params->order_hint_info, ref_order_hint,
863
23.1k
                             (int)cm->current_frame.order_hint) <= 0)
864
23.1k
              ? 0
865
23.1k
              : 1;
866
204k
    } else {
867
204k
      cm->ref_frame_sign_bias[ref_frame] = 0;
868
204k
    }
869
227k
  }
870
32.5k
}
871
872
654
#define MAX_OFFSET_WIDTH 64
873
654
#define MAX_OFFSET_HEIGHT 0
874
875
static int get_block_position(AV1_COMMON *cm, int *mi_r, int *mi_c, int blk_row,
876
392
                              int blk_col, MV mv, int sign_bias) {
877
392
  const int base_blk_row = (blk_row >> 3) << 3;
878
392
  const int base_blk_col = (blk_col >> 3) << 3;
879
880
392
  const int row_offset = (mv.row >= 0) ? (mv.row >> (4 + MI_SIZE_LOG2))
881
392
                                       : -((-mv.row) >> (4 + MI_SIZE_LOG2));
882
883
392
  const int col_offset = (mv.col >= 0) ? (mv.col >> (4 + MI_SIZE_LOG2))
884
392
                                       : -((-mv.col) >> (4 + MI_SIZE_LOG2));
885
886
392
  const int row =
887
392
      (sign_bias == 1) ? blk_row - row_offset : blk_row + row_offset;
888
392
  const int col =
889
392
      (sign_bias == 1) ? blk_col - col_offset : blk_col + col_offset;
890
891
392
  if (row < 0 || row >= (cm->mi_params.mi_rows >> 1) || col < 0 ||
892
392
      col >= (cm->mi_params.mi_cols >> 1))
893
65
    return 0;
894
895
327
  if (row < base_blk_row - (MAX_OFFSET_HEIGHT >> 3) ||
896
327
      row >= base_blk_row + 8 + (MAX_OFFSET_HEIGHT >> 3) ||
897
327
      col < base_blk_col - (MAX_OFFSET_WIDTH >> 3) ||
898
327
      col >= base_blk_col + 8 + (MAX_OFFSET_WIDTH >> 3))
899
0
    return 0;
900
901
327
  *mi_r = row;
902
327
  *mi_c = col;
903
904
327
  return 1;
905
327
}
906
907
// Note: motion_filed_projection finds motion vectors of current frame's
908
// reference frame, and projects them to current frame. To make it clear,
909
// let's call current frame's reference frame as start frame.
910
// Call Start frame's reference frames as reference frames.
911
// Call ref_offset as frame distances between start frame and its reference
912
// frames.
913
static int motion_field_projection(AV1_COMMON *cm,
914
3.03k
                                   MV_REFERENCE_FRAME start_frame, int dir) {
915
3.03k
  TPL_MV_REF *tpl_mvs_base = cm->tpl_mvs;
916
3.03k
  int ref_offset[REF_FRAMES] = { 0 };
917
918
3.03k
  const RefCntBuffer *const start_frame_buf =
919
3.03k
      get_ref_frame_buf(cm, start_frame);
920
3.03k
  if (start_frame_buf == NULL) return 0;
921
922
3.03k
  if (start_frame_buf->frame_type == KEY_FRAME ||
923
3.03k
      start_frame_buf->frame_type == INTRA_ONLY_FRAME)
924
1.07k
    return 0;
925
926
1.95k
  if (start_frame_buf->mi_rows != cm->mi_params.mi_rows ||
927
1.95k
      start_frame_buf->mi_cols != cm->mi_params.mi_cols)
928
0
    return 0;
929
930
1.95k
  const int start_frame_order_hint = start_frame_buf->order_hint;
931
1.95k
  const unsigned int *const ref_order_hints =
932
1.95k
      &start_frame_buf->ref_order_hints[0];
933
1.95k
  const int cur_order_hint = cm->cur_frame->order_hint;
934
1.95k
  int start_to_current_frame_offset = get_relative_dist(
935
1.95k
      &cm->seq_params->order_hint_info, start_frame_order_hint, cur_order_hint);
936
937
15.6k
  for (MV_REFERENCE_FRAME rf = LAST_FRAME; rf <= INTER_REFS_PER_FRAME; ++rf) {
938
13.6k
    ref_offset[rf] = get_relative_dist(&cm->seq_params->order_hint_info,
939
13.6k
                                       start_frame_order_hint,
940
13.6k
                                       ref_order_hints[rf - LAST_FRAME]);
941
13.6k
  }
942
943
1.95k
  if (dir == 2) start_to_current_frame_offset = -start_to_current_frame_offset;
944
945
1.95k
  MV_REF *mv_ref_base = start_frame_buf->mvs;
946
1.95k
  const int mvs_rows = (cm->mi_params.mi_rows + 1) >> 1;
947
1.95k
  const int mvs_cols = (cm->mi_params.mi_cols + 1) >> 1;
948
949
3.91k
  for (int blk_row = 0; blk_row < mvs_rows; ++blk_row) {
950
3.91k
    for (int blk_col = 0; blk_col < mvs_cols; ++blk_col) {
951
1.95k
      MV_REF *mv_ref = &mv_ref_base[blk_row * mvs_cols + blk_col];
952
1.95k
      MV fwd_mv = mv_ref->mv.as_mv;
953
954
1.95k
      if (mv_ref->ref_frame > INTRA_FRAME) {
955
1.83k
        int_mv this_mv;
956
1.83k
        int mi_r, mi_c;
957
1.83k
        const int ref_frame_offset = ref_offset[mv_ref->ref_frame];
958
959
1.83k
        int pos_valid =
960
1.83k
            abs(ref_frame_offset) <= MAX_FRAME_DISTANCE &&
961
1.83k
            ref_frame_offset > 0 &&
962
1.83k
            abs(start_to_current_frame_offset) <= MAX_FRAME_DISTANCE;
963
964
1.83k
        if (pos_valid) {
965
392
          get_mv_projection(&this_mv.as_mv, fwd_mv,
966
392
                            start_to_current_frame_offset, ref_frame_offset);
967
392
          pos_valid = get_block_position(cm, &mi_r, &mi_c, blk_row, blk_col,
968
392
                                         this_mv.as_mv, dir >> 1);
969
392
        }
970
971
1.83k
        if (pos_valid) {
972
327
          const int mi_offset = mi_r * (cm->mi_params.mi_stride >> 1) + mi_c;
973
974
327
          tpl_mvs_base[mi_offset].mfmv0.as_mv.row = fwd_mv.row;
975
327
          tpl_mvs_base[mi_offset].mfmv0.as_mv.col = fwd_mv.col;
976
327
          tpl_mvs_base[mi_offset].ref_frame_offset = ref_frame_offset;
977
327
        }
978
1.83k
      }
979
1.95k
    }
980
1.95k
  }
981
982
1.95k
  return 1;
983
1.95k
}
984
985
// cm->ref_frame_side is calculated here, and will be used in
986
// av1_copy_frame_mvs() to affect how mvs are copied.
987
32.1k
void av1_calculate_ref_frame_side(AV1_COMMON *cm) {
988
32.1k
  const OrderHintInfo *const order_hint_info = &cm->seq_params->order_hint_info;
989
990
32.1k
  memset(cm->ref_frame_side, 0, sizeof(cm->ref_frame_side));
991
32.1k
  if (!order_hint_info->enable_order_hint) return;
992
993
6.31k
  const int cur_order_hint = cm->cur_frame->order_hint;
994
995
50.4k
  for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
996
44.1k
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
997
44.1k
    int order_hint = 0;
998
999
44.1k
    if (buf != NULL) order_hint = buf->order_hint;
1000
1001
44.1k
    if (get_relative_dist(order_hint_info, order_hint, cur_order_hint) > 0)
1002
17.4k
      cm->ref_frame_side[ref_frame] = 1;
1003
26.7k
    else if (order_hint == cur_order_hint)
1004
2.46k
      cm->ref_frame_side[ref_frame] = -1;
1005
44.1k
  }
1006
6.31k
}
1007
1008
1.27k
void av1_setup_motion_field(AV1_COMMON *cm) {
1009
1.27k
  const OrderHintInfo *const order_hint_info = &cm->seq_params->order_hint_info;
1010
1011
1.27k
  if (!order_hint_info->enable_order_hint) return;
1012
1013
1.27k
  TPL_MV_REF *tpl_mvs_base = cm->tpl_mvs;
1014
1.27k
  int size = ((cm->mi_params.mi_rows + MAX_MIB_SIZE) >> 1) *
1015
1.27k
             (cm->mi_params.mi_stride >> 1);
1016
347k
  for (int idx = 0; idx < size; ++idx) {
1017
346k
    tpl_mvs_base[idx].mfmv0.as_int = INVALID_MV;
1018
346k
    tpl_mvs_base[idx].ref_frame_offset = 0;
1019
346k
  }
1020
1021
1.27k
  const int cur_order_hint = cm->cur_frame->order_hint;
1022
1.27k
  const RefCntBuffer *ref_buf[INTER_REFS_PER_FRAME];
1023
1.27k
  int ref_order_hint[INTER_REFS_PER_FRAME];
1024
1025
10.1k
  for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
1026
8.91k
    const int ref_idx = ref_frame - LAST_FRAME;
1027
8.91k
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
1028
8.91k
    int order_hint = 0;
1029
1030
8.91k
    if (buf != NULL) order_hint = buf->order_hint;
1031
1032
8.91k
    ref_buf[ref_idx] = buf;
1033
8.91k
    ref_order_hint[ref_idx] = order_hint;
1034
8.91k
  }
1035
1036
1.27k
  int ref_stamp = MFMV_STACK_SIZE - 1;
1037
1038
1.27k
  if (ref_buf[LAST_FRAME - LAST_FRAME] != NULL) {
1039
1.27k
    const int alt_of_lst_order_hint =
1040
1.27k
        ref_buf[LAST_FRAME - LAST_FRAME]
1041
1.27k
            ->ref_order_hints[ALTREF_FRAME - LAST_FRAME];
1042
1043
1.27k
    const int is_lst_overlay =
1044
1.27k
        (alt_of_lst_order_hint == ref_order_hint[GOLDEN_FRAME - LAST_FRAME]);
1045
1.27k
    if (!is_lst_overlay) motion_field_projection(cm, LAST_FRAME, 2);
1046
1.27k
    --ref_stamp;
1047
1.27k
  }
1048
1049
1.27k
  if (get_relative_dist(order_hint_info,
1050
1.27k
                        ref_order_hint[BWDREF_FRAME - LAST_FRAME],
1051
1.27k
                        cur_order_hint) > 0) {
1052
324
    if (motion_field_projection(cm, BWDREF_FRAME, 0)) --ref_stamp;
1053
324
  }
1054
1055
1.27k
  if (get_relative_dist(order_hint_info,
1056
1.27k
                        ref_order_hint[ALTREF2_FRAME - LAST_FRAME],
1057
1.27k
                        cur_order_hint) > 0) {
1058
300
    if (motion_field_projection(cm, ALTREF2_FRAME, 0)) --ref_stamp;
1059
300
  }
1060
1061
1.27k
  if (get_relative_dist(order_hint_info,
1062
1.27k
                        ref_order_hint[ALTREF_FRAME - LAST_FRAME],
1063
1.27k
                        cur_order_hint) > 0 &&
1064
1.27k
      ref_stamp >= 0)
1065
171
    if (motion_field_projection(cm, ALTREF_FRAME, 0)) --ref_stamp;
1066
1067
1.27k
  if (ref_stamp >= 0) motion_field_projection(cm, LAST2_FRAME, 2);
1068
1.27k
}
1069
1070
static INLINE void record_samples(const MB_MODE_INFO *mbmi, int *pts,
1071
                                  int *pts_inref, int row_offset, int sign_r,
1072
107
                                  int col_offset, int sign_c) {
1073
107
  const int bw = block_size_wide[mbmi->bsize];
1074
107
  const int bh = block_size_high[mbmi->bsize];
1075
107
  const int x = col_offset * MI_SIZE + sign_c * bw / 2 - 1;
1076
107
  const int y = row_offset * MI_SIZE + sign_r * bh / 2 - 1;
1077
1078
107
  pts[0] = GET_MV_SUBPEL(x);
1079
107
  pts[1] = GET_MV_SUBPEL(y);
1080
107
  pts_inref[0] = pts[0] + mbmi->mv[0].as_mv.col;
1081
107
  pts_inref[1] = pts[1] + mbmi->mv[0].as_mv.row;
1082
107
}
1083
1084
// Select samples according to the motion vector difference.
1085
uint8_t av1_selectSamples(MV *mv, int *pts, int *pts_inref, int len,
1086
0
                          BLOCK_SIZE bsize) {
1087
0
  const int bw = block_size_wide[bsize];
1088
0
  const int bh = block_size_high[bsize];
1089
0
  const int thresh = clamp(AOMMAX(bw, bh), 16, 112);
1090
0
  uint8_t ret = 0;
1091
0
  assert(len <= LEAST_SQUARES_SAMPLES_MAX);
1092
1093
  // Only keep the samples with MV differences within threshold.
1094
0
  for (int i = 0; i < len; ++i) {
1095
0
    const int diff = abs(pts_inref[2 * i] - pts[2 * i] - mv->col) +
1096
0
                     abs(pts_inref[2 * i + 1] - pts[2 * i + 1] - mv->row);
1097
0
    if (diff > thresh) continue;
1098
0
    if (ret != i) {
1099
0
      memcpy(pts + 2 * ret, pts + 2 * i, 2 * sizeof(pts[0]));
1100
0
      memcpy(pts_inref + 2 * ret, pts_inref + 2 * i, 2 * sizeof(pts_inref[0]));
1101
0
    }
1102
0
    ++ret;
1103
0
  }
1104
  // Keep at least 1 sample.
1105
0
  return AOMMAX(ret, 1);
1106
0
}
1107
1108
// Note: Samples returned are at 1/8-pel precision
1109
// Sample are the neighbor block center point's coordinates relative to the
1110
// left-top pixel of current block.
1111
uint8_t av1_findSamples(const AV1_COMMON *cm, MACROBLOCKD *xd, int *pts,
1112
1.29k
                        int *pts_inref) {
1113
1.29k
  const MB_MODE_INFO *const mbmi0 = xd->mi[0];
1114
1.29k
  const int ref_frame = mbmi0->ref_frame[0];
1115
1.29k
  const int up_available = xd->up_available;
1116
1.29k
  const int left_available = xd->left_available;
1117
1.29k
  uint8_t np = 0;
1118
1.29k
  int do_tl = 1;
1119
1.29k
  int do_tr = 1;
1120
1.29k
  const int mi_stride = xd->mi_stride;
1121
1.29k
  const int mi_row = xd->mi_row;
1122
1.29k
  const int mi_col = xd->mi_col;
1123
1124
  // scan the nearest above rows
1125
1.29k
  if (up_available) {
1126
0
    const int mi_row_offset = -1;
1127
0
    const MB_MODE_INFO *mbmi = xd->mi[mi_row_offset * mi_stride];
1128
0
    uint8_t superblock_width = mi_size_wide[mbmi->bsize];
1129
1130
0
    if (xd->width <= superblock_width) {
1131
      // Handle "current block width <= above block width" case.
1132
0
      const int col_offset = -mi_col % superblock_width;
1133
1134
0
      if (col_offset < 0) do_tl = 0;
1135
0
      if (col_offset + superblock_width > xd->width) do_tr = 0;
1136
1137
0
      if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) {
1138
0
        record_samples(mbmi, pts, pts_inref, 0, -1, col_offset, 1);
1139
0
        pts += 2;
1140
0
        pts_inref += 2;
1141
0
        if (++np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX;
1142
0
      }
1143
0
    } else {
1144
      // Handle "current block width > above block width" case.
1145
0
      for (int i = 0; i < AOMMIN(xd->width, cm->mi_params.mi_cols - mi_col);
1146
0
           i += superblock_width) {
1147
0
        mbmi = xd->mi[i + mi_row_offset * mi_stride];
1148
0
        superblock_width = mi_size_wide[mbmi->bsize];
1149
1150
0
        if (mbmi->ref_frame[0] == ref_frame &&
1151
0
            mbmi->ref_frame[1] == NONE_FRAME) {
1152
0
          record_samples(mbmi, pts, pts_inref, 0, -1, i, 1);
1153
0
          pts += 2;
1154
0
          pts_inref += 2;
1155
0
          if (++np >= LEAST_SQUARES_SAMPLES_MAX)
1156
0
            return LEAST_SQUARES_SAMPLES_MAX;
1157
0
        }
1158
0
      }
1159
0
    }
1160
0
  }
1161
1.29k
  assert(np <= LEAST_SQUARES_SAMPLES_MAX);
1162
1163
  // scan the nearest left columns
1164
1.29k
  if (left_available) {
1165
132
    const int mi_col_offset = -1;
1166
132
    const MB_MODE_INFO *mbmi = xd->mi[mi_col_offset];
1167
132
    uint8_t superblock_height = mi_size_high[mbmi->bsize];
1168
1169
132
    if (xd->height <= superblock_height) {
1170
      // Handle "current block height <= above block height" case.
1171
87
      const int row_offset = -mi_row % superblock_height;
1172
1173
87
      if (row_offset < 0) do_tl = 0;
1174
1175
87
      if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) {
1176
54
        record_samples(mbmi, pts, pts_inref, row_offset, 1, 0, -1);
1177
54
        pts += 2;
1178
54
        pts_inref += 2;
1179
54
        np++;
1180
54
        if (np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX;
1181
54
      }
1182
87
    } else {
1183
      // Handle "current block height > above block height" case.
1184
135
      for (int i = 0; i < AOMMIN(xd->height, cm->mi_params.mi_rows - mi_row);
1185
90
           i += superblock_height) {
1186
90
        mbmi = xd->mi[mi_col_offset + i * mi_stride];
1187
90
        superblock_height = mi_size_high[mbmi->bsize];
1188
1189
90
        if (mbmi->ref_frame[0] == ref_frame &&
1190
90
            mbmi->ref_frame[1] == NONE_FRAME) {
1191
53
          record_samples(mbmi, pts, pts_inref, i, 1, 0, -1);
1192
53
          pts += 2;
1193
53
          pts_inref += 2;
1194
53
          if (++np >= LEAST_SQUARES_SAMPLES_MAX)
1195
0
            return LEAST_SQUARES_SAMPLES_MAX;
1196
53
        }
1197
90
      }
1198
45
    }
1199
132
  }
1200
1.29k
  assert(np <= LEAST_SQUARES_SAMPLES_MAX);
1201
1202
  // Top-left block
1203
1.29k
  if (do_tl && left_available && up_available) {
1204
0
    const int mi_row_offset = -1;
1205
0
    const int mi_col_offset = -1;
1206
0
    MB_MODE_INFO *mbmi = xd->mi[mi_col_offset + mi_row_offset * mi_stride];
1207
1208
0
    if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) {
1209
0
      record_samples(mbmi, pts, pts_inref, 0, -1, 0, -1);
1210
0
      pts += 2;
1211
0
      pts_inref += 2;
1212
0
      if (++np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX;
1213
0
    }
1214
0
  }
1215
1.29k
  assert(np <= LEAST_SQUARES_SAMPLES_MAX);
1216
1217
  // Top-right block
1218
1.29k
  if (do_tr &&
1219
1.29k
      has_top_right(cm, xd, mi_row, mi_col, AOMMAX(xd->width, xd->height))) {
1220
1.29k
    const POSITION trb_pos = { -1, xd->width };
1221
1.29k
    const TileInfo *const tile = &xd->tile;
1222
1.29k
    if (is_inside(tile, mi_col, mi_row, &trb_pos)) {
1223
0
      const int mi_row_offset = -1;
1224
0
      const int mi_col_offset = xd->width;
1225
0
      const MB_MODE_INFO *mbmi =
1226
0
          xd->mi[mi_col_offset + mi_row_offset * mi_stride];
1227
1228
0
      if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) {
1229
0
        record_samples(mbmi, pts, pts_inref, 0, -1, xd->width, 1);
1230
0
        if (++np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX;
1231
0
      }
1232
0
    }
1233
1.29k
  }
1234
1.29k
  assert(np <= LEAST_SQUARES_SAMPLES_MAX);
1235
1236
1.29k
  return np;
1237
1.29k
}
1238
1239
32.3k
void av1_setup_skip_mode_allowed(AV1_COMMON *cm) {
1240
32.3k
  const OrderHintInfo *const order_hint_info = &cm->seq_params->order_hint_info;
1241
32.3k
  SkipModeInfo *const skip_mode_info = &cm->current_frame.skip_mode_info;
1242
1243
32.3k
  skip_mode_info->skip_mode_allowed = 0;
1244
32.3k
  skip_mode_info->ref_frame_idx_0 = INVALID_IDX;
1245
32.3k
  skip_mode_info->ref_frame_idx_1 = INVALID_IDX;
1246
1247
32.3k
  if (!order_hint_info->enable_order_hint || frame_is_intra_only(cm) ||
1248
32.3k
      cm->current_frame.reference_mode == SINGLE_REFERENCE)
1249
29.9k
    return;
1250
1251
2.39k
  const int cur_order_hint = cm->current_frame.order_hint;
1252
2.39k
  int ref_order_hints[2] = { -1, INT_MAX };
1253
2.39k
  int ref_idx[2] = { INVALID_IDX, INVALID_IDX };
1254
1255
  // Identify the nearest forward and backward references.
1256
19.1k
  for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
1257
16.7k
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, LAST_FRAME + i);
1258
16.7k
    if (buf == NULL) continue;
1259
1260
16.7k
    const int ref_order_hint = buf->order_hint;
1261
16.7k
    if (get_relative_dist(order_hint_info, ref_order_hint, cur_order_hint) <
1262
16.7k
        0) {
1263
      // Forward reference
1264
11.5k
      if (ref_order_hints[0] == -1 ||
1265
11.5k
          get_relative_dist(order_hint_info, ref_order_hint,
1266
9.60k
                            ref_order_hints[0]) > 0) {
1267
2.28k
        ref_order_hints[0] = ref_order_hint;
1268
2.28k
        ref_idx[0] = i;
1269
2.28k
      }
1270
11.5k
    } else if (get_relative_dist(order_hint_info, ref_order_hint,
1271
5.21k
                                 cur_order_hint) > 0) {
1272
      // Backward reference
1273
4.55k
      if (ref_order_hints[1] == INT_MAX ||
1274
4.55k
          get_relative_dist(order_hint_info, ref_order_hint,
1275
3.31k
                            ref_order_hints[1]) < 0) {
1276
1.29k
        ref_order_hints[1] = ref_order_hint;
1277
1.29k
        ref_idx[1] = i;
1278
1.29k
      }
1279
4.55k
    }
1280
16.7k
  }
1281
1282
2.39k
  if (ref_idx[0] != INVALID_IDX && ref_idx[1] != INVALID_IDX) {
1283
    // == Bi-directional prediction ==
1284
806
    skip_mode_info->skip_mode_allowed = 1;
1285
806
    skip_mode_info->ref_frame_idx_0 = AOMMIN(ref_idx[0], ref_idx[1]);
1286
806
    skip_mode_info->ref_frame_idx_1 = AOMMAX(ref_idx[0], ref_idx[1]);
1287
1.59k
  } else if (ref_idx[0] != INVALID_IDX && ref_idx[1] == INVALID_IDX) {
1288
    // == Forward prediction only ==
1289
    // Identify the second nearest forward reference.
1290
1.16k
    ref_order_hints[1] = -1;
1291
9.30k
    for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
1292
8.14k
      const RefCntBuffer *const buf = get_ref_frame_buf(cm, LAST_FRAME + i);
1293
8.14k
      if (buf == NULL) continue;
1294
1295
8.14k
      const int ref_order_hint = buf->order_hint;
1296
8.14k
      if ((ref_order_hints[0] != -1 &&
1297
8.14k
           get_relative_dist(order_hint_info, ref_order_hint,
1298
8.14k
                             ref_order_hints[0]) < 0) &&
1299
8.14k
          (ref_order_hints[1] == -1 ||
1300
629
           get_relative_dist(order_hint_info, ref_order_hint,
1301
479
                             ref_order_hints[1]) > 0)) {
1302
        // Second closest forward reference
1303
260
        ref_order_hints[1] = ref_order_hint;
1304
260
        ref_idx[1] = i;
1305
260
      }
1306
8.14k
    }
1307
1.16k
    if (ref_order_hints[1] != -1) {
1308
150
      skip_mode_info->skip_mode_allowed = 1;
1309
150
      skip_mode_info->ref_frame_idx_0 = AOMMIN(ref_idx[0], ref_idx[1]);
1310
150
      skip_mode_info->ref_frame_idx_1 = AOMMAX(ref_idx[0], ref_idx[1]);
1311
150
    }
1312
1.16k
  }
1313
2.39k
}
1314
1315
typedef struct {
1316
  int map_idx;        // frame map index
1317
  RefCntBuffer *buf;  // frame buffer
1318
  int sort_idx;       // index based on the offset to be used for sorting
1319
} REF_FRAME_INFO;
1320
1321
// Compares the sort_idx fields. If they are equal, then compares the map_idx
1322
// fields to break the tie. This ensures a stable sort.
1323
19.3k
static int compare_ref_frame_info(const void *arg_a, const void *arg_b) {
1324
19.3k
  const REF_FRAME_INFO *info_a = (REF_FRAME_INFO *)arg_a;
1325
19.3k
  const REF_FRAME_INFO *info_b = (REF_FRAME_INFO *)arg_b;
1326
1327
19.3k
  const int sort_idx_diff = info_a->sort_idx - info_b->sort_idx;
1328
19.3k
  if (sort_idx_diff != 0) return sort_idx_diff;
1329
5.15k
  return info_a->map_idx - info_b->map_idx;
1330
19.3k
}
1331
1332
static AOM_INLINE void set_ref_frame_info(int *remapped_ref_idx, int frame_idx,
1333
8.83k
                                          REF_FRAME_INFO *ref_info) {
1334
8.83k
  assert(frame_idx >= 0 && frame_idx < INTER_REFS_PER_FRAME);
1335
1336
8.83k
  remapped_ref_idx[frame_idx] = ref_info->map_idx;
1337
8.83k
}
1338
1339
void av1_set_frame_refs(AV1_COMMON *const cm, int *remapped_ref_idx,
1340
1.28k
                        int lst_map_idx, int gld_map_idx) {
1341
1.28k
  int lst_frame_sort_idx = -1;
1342
1.28k
  int gld_frame_sort_idx = -1;
1343
1344
1.28k
  assert(cm->seq_params->order_hint_info.enable_order_hint);
1345
1.28k
  assert(cm->seq_params->order_hint_info.order_hint_bits_minus_1 >= 0);
1346
1.28k
  const int cur_order_hint = (int)cm->current_frame.order_hint;
1347
1.28k
  const int cur_frame_sort_idx =
1348
1.28k
      1 << cm->seq_params->order_hint_info.order_hint_bits_minus_1;
1349
1350
1.28k
  REF_FRAME_INFO ref_frame_info[REF_FRAMES];
1351
1.28k
  int ref_flag_list[INTER_REFS_PER_FRAME] = { 0, 0, 0, 0, 0, 0, 0 };
1352
1353
11.5k
  for (int i = 0; i < REF_FRAMES; ++i) {
1354
10.2k
    const int map_idx = i;
1355
1356
10.2k
    ref_frame_info[i].map_idx = map_idx;
1357
10.2k
    ref_frame_info[i].sort_idx = -1;
1358
1359
10.2k
    RefCntBuffer *const buf = cm->ref_frame_map[map_idx];
1360
10.2k
    ref_frame_info[i].buf = buf;
1361
1362
10.2k
    if (buf == NULL) continue;
1363
    // If this assertion fails, there is a reference leak.
1364
9.50k
    assert(buf->ref_count > 0);
1365
1366
9.50k
    const int offset = (int)buf->order_hint;
1367
9.50k
    ref_frame_info[i].sort_idx =
1368
9.50k
        (offset == -1) ? -1
1369
9.50k
                       : cur_frame_sort_idx +
1370
9.50k
                             get_relative_dist(&cm->seq_params->order_hint_info,
1371
9.50k
                                               offset, cur_order_hint);
1372
9.50k
    assert(ref_frame_info[i].sort_idx >= -1);
1373
1374
9.50k
    if (map_idx == lst_map_idx) lst_frame_sort_idx = ref_frame_info[i].sort_idx;
1375
9.50k
    if (map_idx == gld_map_idx) gld_frame_sort_idx = ref_frame_info[i].sort_idx;
1376
9.50k
  }
1377
1378
  // Confirm both LAST_FRAME and GOLDEN_FRAME are valid forward reference
1379
  // frames.
1380
1.28k
  if (lst_frame_sort_idx == -1 || lst_frame_sort_idx >= cur_frame_sort_idx) {
1381
14
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
1382
14
                       "Inter frame requests a look-ahead frame as LAST");
1383
14
  }
1384
1.28k
  if (gld_frame_sort_idx == -1 || gld_frame_sort_idx >= cur_frame_sort_idx) {
1385
4
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
1386
4
                       "Inter frame requests a look-ahead frame as GOLDEN");
1387
4
  }
1388
1389
  // Sort ref frames based on their frame_offset values.
1390
1.28k
  qsort(ref_frame_info, REF_FRAMES, sizeof(REF_FRAME_INFO),
1391
1.28k
        compare_ref_frame_info);
1392
1393
  // Identify forward and backward reference frames.
1394
  // Forward  reference: offset < order_hint
1395
  // Backward reference: offset >= order_hint
1396
1.28k
  int fwd_start_idx = 0, fwd_end_idx = REF_FRAMES - 1;
1397
1398
9.79k
  for (int i = 0; i < REF_FRAMES; i++) {
1399
9.27k
    if (ref_frame_info[i].sort_idx == -1) {
1400
732
      fwd_start_idx++;
1401
732
      continue;
1402
732
    }
1403
1404
8.54k
    if (ref_frame_info[i].sort_idx >= cur_frame_sort_idx) {
1405
766
      fwd_end_idx = i - 1;
1406
766
      break;
1407
766
    }
1408
8.54k
  }
1409
1410
1.28k
  int bwd_start_idx = fwd_end_idx + 1;
1411
1.28k
  int bwd_end_idx = REF_FRAMES - 1;
1412
1413
  // === Backward Reference Frames ===
1414
1415
  // == ALTREF_FRAME ==
1416
1.28k
  if (bwd_start_idx <= bwd_end_idx) {
1417
766
    set_ref_frame_info(remapped_ref_idx, ALTREF_FRAME - LAST_FRAME,
1418
766
                       &ref_frame_info[bwd_end_idx]);
1419
766
    ref_flag_list[ALTREF_FRAME - LAST_FRAME] = 1;
1420
766
    bwd_end_idx--;
1421
766
  }
1422
1423
  // == BWDREF_FRAME ==
1424
1.28k
  if (bwd_start_idx <= bwd_end_idx) {
1425
609
    set_ref_frame_info(remapped_ref_idx, BWDREF_FRAME - LAST_FRAME,
1426
609
                       &ref_frame_info[bwd_start_idx]);
1427
609
    ref_flag_list[BWDREF_FRAME - LAST_FRAME] = 1;
1428
609
    bwd_start_idx++;
1429
609
  }
1430
1431
  // == ALTREF2_FRAME ==
1432
1.28k
  if (bwd_start_idx <= bwd_end_idx) {
1433
127
    set_ref_frame_info(remapped_ref_idx, ALTREF2_FRAME - LAST_FRAME,
1434
127
                       &ref_frame_info[bwd_start_idx]);
1435
127
    ref_flag_list[ALTREF2_FRAME - LAST_FRAME] = 1;
1436
127
  }
1437
1438
  // === Forward Reference Frames ===
1439
1440
9.05k
  for (int i = fwd_start_idx; i <= fwd_end_idx; ++i) {
1441
    // == LAST_FRAME ==
1442
7.77k
    if (ref_frame_info[i].map_idx == lst_map_idx) {
1443
1.26k
      set_ref_frame_info(remapped_ref_idx, LAST_FRAME - LAST_FRAME,
1444
1.26k
                         &ref_frame_info[i]);
1445
1.26k
      ref_flag_list[LAST_FRAME - LAST_FRAME] = 1;
1446
1.26k
    }
1447
1448
    // == GOLDEN_FRAME ==
1449
7.77k
    if (ref_frame_info[i].map_idx == gld_map_idx) {
1450
1.26k
      set_ref_frame_info(remapped_ref_idx, GOLDEN_FRAME - LAST_FRAME,
1451
1.26k
                         &ref_frame_info[i]);
1452
1.26k
      ref_flag_list[GOLDEN_FRAME - LAST_FRAME] = 1;
1453
1.26k
    }
1454
7.77k
  }
1455
1456
1.28k
  assert(ref_flag_list[LAST_FRAME - LAST_FRAME] == 1 &&
1457
1.28k
         ref_flag_list[GOLDEN_FRAME - LAST_FRAME] == 1);
1458
1459
  // == LAST2_FRAME ==
1460
  // == LAST3_FRAME ==
1461
  // == BWDREF_FRAME ==
1462
  // == ALTREF2_FRAME ==
1463
  // == ALTREF_FRAME ==
1464
1465
  // Set up the reference frames in the anti-chronological order.
1466
1.26k
  static const MV_REFERENCE_FRAME ref_frame_list[INTER_REFS_PER_FRAME - 2] = {
1467
1.26k
    LAST2_FRAME, LAST3_FRAME, BWDREF_FRAME, ALTREF2_FRAME, ALTREF_FRAME
1468
1.26k
  };
1469
1470
1.26k
  int ref_idx;
1471
7.06k
  for (ref_idx = 0; ref_idx < (INTER_REFS_PER_FRAME - 2); ref_idx++) {
1472
5.98k
    const MV_REFERENCE_FRAME ref_frame = ref_frame_list[ref_idx];
1473
1474
5.98k
    if (ref_flag_list[ref_frame - LAST_FRAME] == 1) continue;
1475
1476
6.51k
    while (fwd_start_idx <= fwd_end_idx &&
1477
6.51k
           (ref_frame_info[fwd_end_idx].map_idx == lst_map_idx ||
1478
6.33k
            ref_frame_info[fwd_end_idx].map_idx == gld_map_idx)) {
1479
1.99k
      fwd_end_idx--;
1480
1.99k
    }
1481
4.52k
    if (fwd_start_idx > fwd_end_idx) break;
1482
1483
4.34k
    set_ref_frame_info(remapped_ref_idx, ref_frame - LAST_FRAME,
1484
4.34k
                       &ref_frame_info[fwd_end_idx]);
1485
4.34k
    ref_flag_list[ref_frame - LAST_FRAME] = 1;
1486
1487
4.34k
    fwd_end_idx--;
1488
4.34k
  }
1489
1490
  // Assign all the remaining frame(s), if any, to the earliest reference
1491
  // frame.
1492
1.76k
  for (; ref_idx < (INTER_REFS_PER_FRAME - 2); ref_idx++) {
1493
507
    const MV_REFERENCE_FRAME ref_frame = ref_frame_list[ref_idx];
1494
507
    if (ref_flag_list[ref_frame - LAST_FRAME] == 1) continue;
1495
461
    set_ref_frame_info(remapped_ref_idx, ref_frame - LAST_FRAME,
1496
461
                       &ref_frame_info[fwd_start_idx]);
1497
461
    ref_flag_list[ref_frame - LAST_FRAME] = 1;
1498
461
  }
1499
1500
10.0k
  for (int i = 0; i < INTER_REFS_PER_FRAME; i++) {
1501
8.83k
    assert(ref_flag_list[i] == 1);
1502
8.83k
  }
1503
1.26k
}