Coverage Report

Created: 2025-12-03 07:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aom/av1/common/mvref_common.c
Line
Count
Source
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
25.9k
static AOM_INLINE void get_mv_projection(MV *output, MV ref, int num, int den) {
27
25.9k
  den = AOMMIN(den, MAX_FRAME_DISTANCE);
28
25.9k
  num = num > 0 ? AOMMIN(num, MAX_FRAME_DISTANCE)
29
25.9k
                : AOMMAX(num, -MAX_FRAME_DISTANCE);
30
25.9k
  const int mv_row =
31
25.9k
      ROUND_POWER_OF_TWO_SIGNED(ref.row * num * div_mult[den], 14);
32
25.9k
  const int mv_col =
33
25.9k
      ROUND_POWER_OF_TWO_SIGNED(ref.col * num * div_mult[den], 14);
34
25.9k
  const int clamp_max = MV_UPP - 1;
35
25.9k
  const int clamp_min = MV_LOW + 1;
36
25.9k
  output->row = (int16_t)clamp(mv_row, clamp_min, clamp_max);
37
25.9k
  output->col = (int16_t)clamp(mv_col, clamp_min, clamp_max);
38
25.9k
}
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
30.3k
                        int x_mis, int y_mis) {
43
30.3k
  const int frame_mvs_stride = ROUND_POWER_OF_TWO(cm->mi_params.mi_cols, 1);
44
30.3k
  MV_REF *frame_mvs =
45
30.3k
      cm->cur_frame->mvs + (mi_row >> 1) * frame_mvs_stride + (mi_col >> 1);
46
30.3k
  x_mis = ROUND_POWER_OF_TWO(x_mis, 1);
47
30.3k
  y_mis = ROUND_POWER_OF_TWO(y_mis, 1);
48
30.3k
  int w, h;
49
50
62.4k
  for (h = 0; h < y_mis; h++) {
51
32.0k
    MV_REF *mv = frame_mvs;
52
76.7k
    for (w = 0; w < x_mis; w++) {
53
44.7k
      mv->ref_frame = NONE_FRAME;
54
44.7k
      mv->mv.as_int = 0;
55
56
134k
      for (int idx = 0; idx < 2; ++idx) {
57
89.4k
        MV_REFERENCE_FRAME ref_frame = mi->ref_frame[idx];
58
89.4k
        if (ref_frame > INTRA_FRAME) {
59
54.3k
          int8_t ref_idx = cm->ref_frame_side[ref_frame];
60
54.3k
          if (ref_idx) continue;
61
32.5k
          if ((abs(mi->mv[idx].as_mv.row) > REFMVS_LIMIT) ||
62
32.4k
              (abs(mi->mv[idx].as_mv.col) > REFMVS_LIMIT))
63
125
            continue;
64
32.4k
          mv->ref_frame = ref_frame;
65
32.4k
          mv->mv.as_int = mi->mv[idx].as_int;
66
32.4k
        }
67
89.4k
      }
68
44.7k
      mv++;
69
44.7k
    }
70
32.0k
    frame_mvs += frame_mvs_stride;
71
32.0k
  }
72
30.3k
}
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
734k
    uint16_t weight) {
80
734k
  if (!is_inter_block(candidate)) return;
81
734k
  assert(weight % 2 == 0);
82
486k
  int index, ref;
83
84
486k
  if (rf[1] == NONE_FRAME) {
85
    // single reference frame
86
1.38M
    for (ref = 0; ref < 2; ++ref) {
87
924k
      if (candidate->ref_frame[ref] == rf[0]) {
88
443k
        const int is_gm_block =
89
443k
            is_global_mv_block(candidate, gm_params[rf[0]].wmtype);
90
443k
        const int_mv this_refmv =
91
443k
            is_gm_block ? gm_mv_candidates[0] : get_block_mv(candidate, ref);
92
1.07M
        for (index = 0; index < *refmv_count; ++index) {
93
745k
          if (ref_mv_stack[index].this_mv.as_int == this_refmv.as_int) {
94
117k
            ref_mv_weight[index] += weight;
95
117k
            break;
96
117k
          }
97
745k
        }
98
99
        // Add a new item to the list.
100
443k
        if (index == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
101
322k
          ref_mv_stack[index].this_mv = this_refmv;
102
322k
          ref_mv_weight[index] = weight;
103
322k
          ++(*refmv_count);
104
322k
        }
105
443k
        if (have_newmv_in_inter_mode(candidate->mode)) ++*newmv_count;
106
443k
        ++*ref_match_count;
107
443k
      }
108
924k
    }
109
462k
  } else {
110
    // compound reference frame
111
24.1k
    if (candidate->ref_frame[0] == rf[0] && candidate->ref_frame[1] == rf[1]) {
112
7.79k
      int_mv this_refmv[2];
113
114
23.3k
      for (ref = 0; ref < 2; ++ref) {
115
15.5k
        if (is_global_mv_block(candidate, gm_params[rf[ref]].wmtype))
116
1.35k
          this_refmv[ref] = gm_mv_candidates[ref];
117
14.2k
        else
118
14.2k
          this_refmv[ref] = get_block_mv(candidate, ref);
119
15.5k
      }
120
121
11.2k
      for (index = 0; index < *refmv_count; ++index) {
122
5.15k
        if ((ref_mv_stack[index].this_mv.as_int == this_refmv[0].as_int) &&
123
2.24k
            (ref_mv_stack[index].comp_mv.as_int == this_refmv[1].as_int)) {
124
1.66k
          ref_mv_weight[index] += weight;
125
1.66k
          break;
126
1.66k
        }
127
5.15k
      }
128
129
      // Add a new item to the list.
130
7.79k
      if (index == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
131
6.11k
        ref_mv_stack[index].this_mv = this_refmv[0];
132
6.11k
        ref_mv_stack[index].comp_mv = this_refmv[1];
133
6.11k
        ref_mv_weight[index] = weight;
134
6.11k
        ++(*refmv_count);
135
6.11k
      }
136
7.79k
      if (have_newmv_in_inter_mode(candidate->mode)) ++*newmv_count;
137
7.79k
      ++*ref_match_count;
138
7.79k
    }
139
24.1k
  }
140
486k
}
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
244k
    int *processed_rows) {
148
244k
  int end_mi = AOMMIN(xd->width, cm->mi_params.mi_cols - mi_col);
149
244k
  end_mi = AOMMIN(end_mi, mi_size_wide[BLOCK_64X64]);
150
244k
  const int width_8x8 = mi_size_wide[BLOCK_8X8];
151
244k
  const int width_16x16 = mi_size_wide[BLOCK_16X16];
152
244k
  int col_offset = 0;
153
  // TODO(jingning): Revisit this part after cb4x4 is stable.
154
244k
  if (abs(row_offset) > 1) {
155
130k
    col_offset = 1;
156
130k
    if ((mi_col & 0x01) && xd->width < width_8x8) --col_offset;
157
130k
  }
158
244k
  const int use_step_16 = (xd->width >= 16);
159
244k
  MB_MODE_INFO **const candidate_mi0 = xd->mi + row_offset * xd->mi_stride;
160
161
527k
  for (int i = 0; i < end_mi;) {
162
283k
    const MB_MODE_INFO *const candidate = candidate_mi0[col_offset + i];
163
283k
    const int candidate_bsize = candidate->bsize;
164
283k
    const int n4_w = mi_size_wide[candidate_bsize];
165
283k
    int len = AOMMIN(xd->width, n4_w);
166
283k
    if (use_step_16)
167
6.33k
      len = AOMMAX(width_16x16, len);
168
276k
    else if (abs(row_offset) > 1)
169
150k
      len = AOMMAX(len, width_8x8);
170
171
283k
    uint16_t weight = 2;
172
283k
    if (xd->width >= width_8x8 && xd->width <= n4_w) {
173
155k
      uint16_t inc = AOMMIN(-max_row_offset + row_offset + 1,
174
155k
                            mi_size_high[candidate_bsize]);
175
      // Obtain range used in weight calculation.
176
155k
      weight = AOMMAX(weight, inc);
177
      // Update processed rows.
178
155k
      *processed_rows = inc - row_offset - 1;
179
155k
    }
180
181
283k
    add_ref_mv_candidate(candidate, rf, refmv_count, ref_match_count,
182
283k
                         newmv_count, ref_mv_stack, ref_mv_weight,
183
283k
                         gm_mv_candidates, cm->global_motion, len * weight);
184
185
283k
    i += len;
186
283k
  }
187
244k
}
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
252k
    int *processed_cols) {
195
252k
  int end_mi = AOMMIN(xd->height, cm->mi_params.mi_rows - mi_row);
196
252k
  end_mi = AOMMIN(end_mi, mi_size_high[BLOCK_64X64]);
197
252k
  const int n8_h_8 = mi_size_high[BLOCK_8X8];
198
252k
  const int n8_h_16 = mi_size_high[BLOCK_16X16];
199
252k
  int i;
200
252k
  int row_offset = 0;
201
252k
  if (abs(col_offset) > 1) {
202
136k
    row_offset = 1;
203
136k
    if ((mi_row & 0x01) && xd->height < n8_h_8) --row_offset;
204
136k
  }
205
252k
  const int use_step_16 = (xd->height >= 16);
206
207
541k
  for (i = 0; i < end_mi;) {
208
288k
    const MB_MODE_INFO *const candidate =
209
288k
        xd->mi[(row_offset + i) * xd->mi_stride + col_offset];
210
288k
    const int candidate_bsize = candidate->bsize;
211
288k
    const int n4_h = mi_size_high[candidate_bsize];
212
288k
    int len = AOMMIN(xd->height, n4_h);
213
288k
    if (use_step_16)
214
7.02k
      len = AOMMAX(n8_h_16, len);
215
281k
    else if (abs(col_offset) > 1)
216
152k
      len = AOMMAX(len, n8_h_8);
217
218
288k
    int weight = 2;
219
288k
    if (xd->height >= n8_h_8 && xd->height <= n4_h) {
220
128k
      int inc = AOMMIN(-max_col_offset + col_offset + 1,
221
128k
                       mi_size_wide[candidate_bsize]);
222
      // Obtain range used in weight calculation.
223
128k
      weight = AOMMAX(weight, inc);
224
      // Update processed cols.
225
128k
      *processed_cols = inc - col_offset - 1;
226
128k
    }
227
228
288k
    add_ref_mv_candidate(candidate, rf, refmv_count, ref_match_count,
229
288k
                         newmv_count, ref_mv_stack, ref_mv_weight,
230
288k
                         gm_mv_candidates, cm->global_motion, len * weight);
231
232
288k
    i += len;
233
288k
  }
234
252k
}
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
222k
    uint8_t *refmv_count) {
242
222k
  const TileInfo *const tile = &xd->tile;
243
222k
  POSITION mi_pos;
244
245
222k
  mi_pos.row = row_offset;
246
222k
  mi_pos.col = col_offset;
247
248
222k
  if (is_inside(tile, mi_col, mi_row, &mi_pos)) {
249
162k
    const MB_MODE_INFO *const candidate =
250
162k
        xd->mi[mi_pos.row * xd->mi_stride + mi_pos.col];
251
162k
    const int len = mi_size_wide[BLOCK_8X8];
252
253
162k
    add_ref_mv_candidate(candidate, rf, refmv_count, ref_match_count,
254
162k
                         newmv_count, ref_mv_stack, ref_mv_weight,
255
162k
                         gm_mv_candidates, cm->global_motion, 2 * len);
256
162k
  }  // Analyze a single 8x8 block motion information.
257
222k
}
258
259
static int has_top_right(const AV1_COMMON *cm, const MACROBLOCKD *xd,
260
151k
                         int mi_row, int mi_col, int bs) {
261
151k
  const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size];
262
151k
  const int mask_row = mi_row & (sb_mi_size - 1);
263
151k
  const int mask_col = mi_col & (sb_mi_size - 1);
264
265
151k
  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
150k
  int has_tr = !((mask_row & bs) && (mask_col & bs));
269
270
  // bs > 0 and bs is a power of 2
271
150k
  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
218k
  while (bs < sb_mi_size) {
277
213k
    if (mask_col & bs) {
278
83.8k
      if ((mask_col & (2 * bs)) && (mask_row & (2 * bs))) {
279
16.3k
        has_tr = 0;
280
16.3k
        break;
281
16.3k
      }
282
129k
    } else {
283
129k
      break;
284
129k
    }
285
67.4k
    bs <<= 1;
286
67.4k
  }
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
150k
  if (xd->width < xd->height) {
291
26.4k
    if (!xd->is_last_vertical_rect) has_tr = 1;
292
26.4k
  }
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
150k
  if (xd->width > xd->height) {
297
53.3k
    if (!xd->is_first_horizontal_rect) has_tr = 0;
298
53.3k
  }
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
150k
  if (xd->mi[0]->partition == PARTITION_VERT_A) {
304
3.45k
    if (xd->width == xd->height)
305
2.23k
      if (mask_row & bs) has_tr = 0;
306
3.45k
  }
307
308
150k
  return has_tr;
309
150k
}
310
311
static int check_sb_border(const int mi_row, const int mi_col,
312
20.5k
                           const int row_offset, const int col_offset) {
313
20.5k
  const int sb_mi_size = mi_size_wide[BLOCK_64X64];
314
20.5k
  const int row = mi_row & (sb_mi_size - 1);
315
20.5k
  const int col = mi_col & (sb_mi_size - 1);
316
317
20.5k
  if (row + row_offset < 0 || row + row_offset >= sb_mi_size ||
318
20.5k
      col + col_offset < 0 || col + col_offset >= sb_mi_size)
319
3.89k
    return 0;
320
321
16.6k
  return 1;
322
20.5k
}
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
32.5k
                          int16_t *mode_context) {
331
32.5k
  POSITION mi_pos;
332
32.5k
  mi_pos.row = (mi_row & 0x01) ? blk_row : blk_row + 1;
333
32.5k
  mi_pos.col = (mi_col & 0x01) ? blk_col : blk_col + 1;
334
335
32.5k
  if (!is_inside(&xd->tile, mi_col, mi_row, &mi_pos)) return 0;
336
337
22.6k
  const TPL_MV_REF *prev_frame_mvs =
338
22.6k
      cm->tpl_mvs +
339
22.6k
      ((mi_row + mi_pos.row) >> 1) * (cm->mi_params.mi_stride >> 1) +
340
22.6k
      ((mi_col + mi_pos.col) >> 1);
341
22.6k
  if (prev_frame_mvs->mfmv0.as_int == INVALID_MV) return 0;
342
343
9.17k
  MV_REFERENCE_FRAME rf[2];
344
9.17k
  av1_set_ref_frame(rf, ref_frame);
345
346
9.17k
  const uint16_t weight_unit = 1;  // mi_size_wide[BLOCK_8X8];
347
9.17k
  const int cur_frame_index = cm->cur_frame->order_hint;
348
9.17k
  const RefCntBuffer *const buf_0 = get_ref_frame_buf(cm, rf[0]);
349
9.17k
  const int frame0_index = buf_0->order_hint;
350
9.17k
  const int cur_offset_0 = get_relative_dist(&cm->seq_params->order_hint_info,
351
9.17k
                                             cur_frame_index, frame0_index);
352
9.17k
  int idx;
353
9.17k
  const int allow_high_precision_mv = cm->features.allow_high_precision_mv;
354
9.17k
  const int force_integer_mv = cm->features.cur_frame_force_integer_mv;
355
356
9.17k
  int_mv this_refmv;
357
9.17k
  get_mv_projection(&this_refmv.as_mv, prev_frame_mvs->mfmv0.as_mv,
358
9.17k
                    cur_offset_0, prev_frame_mvs->ref_frame_offset);
359
9.17k
  lower_mv_precision(&this_refmv.as_mv, allow_high_precision_mv,
360
9.17k
                     force_integer_mv);
361
362
9.17k
  if (rf[1] == NONE_FRAME) {
363
4.39k
    if (blk_row == 0 && blk_col == 0) {
364
2.36k
      if (abs(this_refmv.as_mv.row - gm_mv_candidates[0].as_mv.row) >= 16 ||
365
1.61k
          abs(this_refmv.as_mv.col - gm_mv_candidates[0].as_mv.col) >= 16)
366
1.19k
        mode_context[ref_frame] |= (1 << GLOBALMV_OFFSET);
367
2.36k
    }
368
369
9.25k
    for (idx = 0; idx < *refmv_count; ++idx)
370
6.20k
      if (this_refmv.as_int == ref_mv_stack[idx].this_mv.as_int) break;
371
372
4.39k
    if (idx < *refmv_count) ref_mv_weight[idx] += 2 * weight_unit;
373
374
4.39k
    if (idx == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
375
3.04k
      ref_mv_stack[idx].this_mv.as_int = this_refmv.as_int;
376
3.04k
      ref_mv_weight[idx] = 2 * weight_unit;
377
3.04k
      ++(*refmv_count);
378
3.04k
    }
379
4.78k
  } else {
380
    // Process compound inter mode
381
4.78k
    const RefCntBuffer *const buf_1 = get_ref_frame_buf(cm, rf[1]);
382
4.78k
    const int frame1_index = buf_1->order_hint;
383
4.78k
    const int cur_offset_1 = get_relative_dist(&cm->seq_params->order_hint_info,
384
4.78k
                                               cur_frame_index, frame1_index);
385
4.78k
    int_mv comp_refmv;
386
4.78k
    get_mv_projection(&comp_refmv.as_mv, prev_frame_mvs->mfmv0.as_mv,
387
4.78k
                      cur_offset_1, prev_frame_mvs->ref_frame_offset);
388
4.78k
    lower_mv_precision(&comp_refmv.as_mv, allow_high_precision_mv,
389
4.78k
                       force_integer_mv);
390
391
4.78k
    if (blk_row == 0 && blk_col == 0) {
392
1.60k
      if (abs(this_refmv.as_mv.row - gm_mv_candidates[0].as_mv.row) >= 16 ||
393
1.13k
          abs(this_refmv.as_mv.col - gm_mv_candidates[0].as_mv.col) >= 16 ||
394
952
          abs(comp_refmv.as_mv.row - gm_mv_candidates[1].as_mv.row) >= 16 ||
395
835
          abs(comp_refmv.as_mv.col - gm_mv_candidates[1].as_mv.col) >= 16)
396
827
        mode_context[ref_frame] |= (1 << GLOBALMV_OFFSET);
397
1.60k
    }
398
399
9.01k
    for (idx = 0; idx < *refmv_count; ++idx) {
400
6.31k
      if (this_refmv.as_int == ref_mv_stack[idx].this_mv.as_int &&
401
2.48k
          comp_refmv.as_int == ref_mv_stack[idx].comp_mv.as_int)
402
2.08k
        break;
403
6.31k
    }
404
405
4.78k
    if (idx < *refmv_count) ref_mv_weight[idx] += 2 * weight_unit;
406
407
4.78k
    if (idx == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
408
2.69k
      ref_mv_stack[idx].this_mv.as_int = this_refmv.as_int;
409
2.69k
      ref_mv_stack[idx].comp_mv.as_int = comp_refmv.as_int;
410
2.69k
      ref_mv_weight[idx] = 2 * weight_unit;
411
2.69k
      ++(*refmv_count);
412
2.69k
    }
413
4.78k
  }
414
415
9.17k
  return 1;
416
22.6k
}
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
8.78k
    int ref_id_count[2], int_mv ref_diff[2][2], int ref_diff_count[2]) {
422
26.3k
  for (int rf_idx = 0; rf_idx < 2; ++rf_idx) {
423
17.5k
    MV_REFERENCE_FRAME can_rf = candidate->ref_frame[rf_idx];
424
425
52.6k
    for (int cmp_idx = 0; cmp_idx < 2; ++cmp_idx) {
426
35.1k
      if (can_rf == rf[cmp_idx] && ref_id_count[cmp_idx] < 2) {
427
9.48k
        ref_id[cmp_idx][ref_id_count[cmp_idx]] = candidate->mv[rf_idx];
428
9.48k
        ++ref_id_count[cmp_idx];
429
25.6k
      } else if (can_rf > INTRA_FRAME && ref_diff_count[cmp_idx] < 2) {
430
15.5k
        int_mv this_mv = candidate->mv[rf_idx];
431
15.5k
        if (cm->ref_frame_sign_bias[can_rf] !=
432
15.5k
            cm->ref_frame_sign_bias[rf[cmp_idx]]) {
433
454
          this_mv.as_mv.row = -this_mv.as_mv.row;
434
454
          this_mv.as_mv.col = -this_mv.as_mv.col;
435
454
        }
436
15.5k
        ref_diff[cmp_idx][ref_diff_count[cmp_idx]] = this_mv;
437
15.5k
        ++ref_diff_count[cmp_idx];
438
15.5k
      }
439
35.1k
    }
440
17.5k
  }
441
8.78k
}
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
67.2k
    uint16_t ref_mv_weight[MAX_REF_MV_STACK_SIZE]) {
448
201k
  for (int rf_idx = 0; rf_idx < 2; ++rf_idx) {
449
134k
    if (candidate->ref_frame[rf_idx] > INTRA_FRAME) {
450
21.6k
      int_mv this_mv = candidate->mv[rf_idx];
451
21.6k
      if (cm->ref_frame_sign_bias[candidate->ref_frame[rf_idx]] !=
452
21.6k
          cm->ref_frame_sign_bias[ref_frame]) {
453
401
        this_mv.as_mv.row = -this_mv.as_mv.row;
454
401
        this_mv.as_mv.col = -this_mv.as_mv.col;
455
401
      }
456
21.6k
      int stack_idx;
457
25.2k
      for (stack_idx = 0; stack_idx < *refmv_count; ++stack_idx) {
458
17.8k
        const int_mv stack_mv = ref_mv_stack[stack_idx].this_mv;
459
17.8k
        if (this_mv.as_int == stack_mv.as_int) break;
460
17.8k
      }
461
462
21.6k
      if (stack_idx == *refmv_count) {
463
7.33k
        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
7.33k
        ref_mv_weight[stack_idx] = 2;
468
7.33k
        ++(*refmv_count);
469
7.33k
      }
470
21.6k
    }
471
134k
  }
472
67.2k
}
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
137k
    int mi_row, int mi_col, int16_t *mode_context) {
481
137k
  const int bs = AOMMAX(xd->width, xd->height);
482
137k
  const int has_tr = has_top_right(cm, xd, mi_row, mi_col, bs);
483
137k
  MV_REFERENCE_FRAME rf[2];
484
485
137k
  const TileInfo *const tile = &xd->tile;
486
137k
  int max_row_offset = 0, max_col_offset = 0;
487
137k
  const int row_adj = (xd->height < mi_size_high[BLOCK_8X8]) && (mi_row & 0x01);
488
137k
  const int col_adj = (xd->width < mi_size_wide[BLOCK_8X8]) && (mi_col & 0x01);
489
137k
  int processed_rows = 0;
490
137k
  int processed_cols = 0;
491
492
137k
  av1_set_ref_frame(rf, ref_frame);
493
137k
  mode_context[ref_frame] = 0;
494
137k
  *refmv_count = 0;
495
496
  // Find valid maximum row/col offset.
497
137k
  if (xd->up_available) {
498
114k
    max_row_offset = -(MVREF_ROW_COLS << 1) + row_adj;
499
500
114k
    if (xd->height < mi_size_high[BLOCK_8X8])
501
35.7k
      max_row_offset = -(2 << 1) + row_adj;
502
503
114k
    max_row_offset = find_valid_row_offset(tile, mi_row, max_row_offset);
504
114k
  }
505
506
137k
  if (xd->left_available) {
507
116k
    max_col_offset = -(MVREF_ROW_COLS << 1) + col_adj;
508
509
116k
    if (xd->width < mi_size_wide[BLOCK_8X8])
510
25.5k
      max_col_offset = -(2 << 1) + col_adj;
511
512
116k
    max_col_offset = find_valid_col_offset(tile, mi_col, max_col_offset);
513
116k
  }
514
515
137k
  uint8_t col_match_count = 0;
516
137k
  uint8_t row_match_count = 0;
517
137k
  uint8_t newmv_count = 0;
518
519
  // Scan the first above row mode info. row_offset = -1;
520
137k
  if (abs(max_row_offset) >= 1)
521
114k
    scan_row_mbmi(cm, xd, mi_col, rf, -1, ref_mv_stack, ref_mv_weight,
522
114k
                  refmv_count, &row_match_count, &newmv_count, gm_mv_candidates,
523
114k
                  max_row_offset, &processed_rows);
524
  // Scan the first left column mode info. col_offset = -1;
525
137k
  if (abs(max_col_offset) >= 1)
526
116k
    scan_col_mbmi(cm, xd, mi_row, rf, -1, ref_mv_stack, ref_mv_weight,
527
116k
                  refmv_count, &col_match_count, &newmv_count, gm_mv_candidates,
528
116k
                  max_col_offset, &processed_cols);
529
  // Check top-right boundary
530
137k
  if (has_tr)
531
84.9k
    scan_blk_mbmi(cm, xd, mi_row, mi_col, rf, -1, xd->width, ref_mv_stack,
532
84.9k
                  ref_mv_weight, &row_match_count, &newmv_count,
533
84.9k
                  gm_mv_candidates, refmv_count);
534
535
137k
  const uint8_t nearest_match = (row_match_count > 0) + (col_match_count > 0);
536
137k
  const uint8_t nearest_refmv_count = *refmv_count;
537
538
  // TODO(yunqing): for comp_search, do it for all 3 cases.
539
310k
  for (int idx = 0; idx < nearest_refmv_count; ++idx)
540
172k
    ref_mv_weight[idx] += REF_CAT_LEVEL;
541
542
137k
  if (cm->features.allow_ref_frame_mvs) {
543
10.9k
    int is_available = 0;
544
10.9k
    const int voffset = AOMMAX(mi_size_high[BLOCK_8X8], xd->height);
545
10.9k
    const int hoffset = AOMMAX(mi_size_wide[BLOCK_8X8], xd->width);
546
10.9k
    const int blk_row_end = AOMMIN(xd->height, mi_size_high[BLOCK_64X64]);
547
10.9k
    const int blk_col_end = AOMMIN(xd->width, mi_size_wide[BLOCK_64X64]);
548
549
10.9k
    const int tpl_sample_pos[3][2] = {
550
10.9k
      { voffset, -2 },
551
10.9k
      { voffset, hoffset },
552
10.9k
      { voffset - 2, hoffset },
553
10.9k
    };
554
10.9k
    const int allow_extension = (xd->height >= mi_size_high[BLOCK_8X8]) &&
555
6.93k
                                (xd->height < mi_size_high[BLOCK_64X64]) &&
556
6.93k
                                (xd->width >= mi_size_wide[BLOCK_8X8]) &&
557
6.83k
                                (xd->width < mi_size_wide[BLOCK_64X64]);
558
559
10.9k
    const int step_h = (xd->height >= mi_size_high[BLOCK_64X64])
560
10.9k
                           ? mi_size_high[BLOCK_16X16]
561
10.9k
                           : mi_size_high[BLOCK_8X8];
562
10.9k
    const int step_w = (xd->width >= mi_size_wide[BLOCK_64X64])
563
10.9k
                           ? mi_size_wide[BLOCK_16X16]
564
10.9k
                           : mi_size_wide[BLOCK_8X8];
565
566
22.4k
    for (int blk_row = 0; blk_row < blk_row_end; blk_row += step_h) {
567
27.4k
      for (int blk_col = 0; blk_col < blk_col_end; blk_col += step_w) {
568
15.9k
        int ret = add_tpl_ref_mv(cm, xd, mi_row, mi_col, ref_frame, blk_row,
569
15.9k
                                 blk_col, gm_mv_candidates, refmv_count,
570
15.9k
                                 ref_mv_stack, ref_mv_weight, mode_context);
571
15.9k
        if (blk_row == 0 && blk_col == 0) is_available = ret;
572
15.9k
      }
573
11.5k
    }
574
575
10.9k
    if (is_available == 0) mode_context[ref_frame] |= (1 << GLOBALMV_OFFSET);
576
577
31.4k
    for (int i = 0; i < 3 && allow_extension; ++i) {
578
20.5k
      const int blk_row = tpl_sample_pos[i][0];
579
20.5k
      const int blk_col = tpl_sample_pos[i][1];
580
581
20.5k
      if (!check_sb_border(mi_row, mi_col, blk_row, blk_col)) continue;
582
16.6k
      add_tpl_ref_mv(cm, xd, mi_row, mi_col, ref_frame, blk_row, blk_col,
583
16.6k
                     gm_mv_candidates, refmv_count, ref_mv_stack, ref_mv_weight,
584
16.6k
                     mode_context);
585
16.6k
    }
586
10.9k
  }
587
588
137k
  uint8_t dummy_newmv_count = 0;
589
590
  // Scan the second outer area.
591
137k
  scan_blk_mbmi(cm, xd, mi_row, mi_col, rf, -1, -1, ref_mv_stack, ref_mv_weight,
592
137k
                &row_match_count, &dummy_newmv_count, gm_mv_candidates,
593
137k
                refmv_count);
594
595
412k
  for (int idx = 2; idx <= MVREF_ROW_COLS; ++idx) {
596
274k
    const int row_offset = -(idx << 1) + 1 + row_adj;
597
274k
    const int col_offset = -(idx << 1) + 1 + col_adj;
598
599
274k
    if (abs(row_offset) <= abs(max_row_offset) &&
600
170k
        abs(row_offset) > processed_rows)
601
130k
      scan_row_mbmi(cm, xd, mi_col, rf, row_offset, ref_mv_stack, ref_mv_weight,
602
130k
                    refmv_count, &row_match_count, &dummy_newmv_count,
603
130k
                    gm_mv_candidates, max_row_offset, &processed_rows);
604
605
274k
    if (abs(col_offset) <= abs(max_col_offset) &&
606
186k
        abs(col_offset) > processed_cols)
607
136k
      scan_col_mbmi(cm, xd, mi_row, rf, col_offset, ref_mv_stack, ref_mv_weight,
608
136k
                    refmv_count, &col_match_count, &dummy_newmv_count,
609
136k
                    gm_mv_candidates, max_col_offset, &processed_cols);
610
274k
  }
611
612
137k
  const uint8_t ref_match_count = (row_match_count > 0) + (col_match_count > 0);
613
614
137k
  switch (nearest_match) {
615
39.5k
    case 0:
616
39.5k
      if (ref_match_count >= 1) mode_context[ref_frame] |= 1;
617
39.5k
      if (ref_match_count == 1)
618
7.01k
        mode_context[ref_frame] |= (1 << REFMV_OFFSET);
619
32.5k
      else if (ref_match_count >= 2)
620
2.48k
        mode_context[ref_frame] |= (2 << REFMV_OFFSET);
621
39.5k
      break;
622
44.6k
    case 1:
623
44.6k
      mode_context[ref_frame] |= (newmv_count > 0) ? 2 : 3;
624
44.6k
      if (ref_match_count == 1)
625
28.0k
        mode_context[ref_frame] |= (3 << REFMV_OFFSET);
626
16.5k
      else if (ref_match_count >= 2)
627
16.5k
        mode_context[ref_frame] |= (4 << REFMV_OFFSET);
628
44.6k
      break;
629
53.2k
    case 2:
630
53.2k
    default:
631
53.2k
      if (newmv_count >= 1)
632
3.79k
        mode_context[ref_frame] |= 4;
633
49.4k
      else
634
49.4k
        mode_context[ref_frame] |= 5;
635
636
53.2k
      mode_context[ref_frame] |= (5 << REFMV_OFFSET);
637
53.2k
      break;
638
137k
  }
639
640
  // Rank the likelihood and assign nearest and near mvs.
641
137k
  int len = nearest_refmv_count;
642
262k
  while (len > 0) {
643
124k
    int nr_len = 0;
644
214k
    for (int idx = 1; idx < len; ++idx) {
645
89.9k
      if (ref_mv_weight[idx - 1] < ref_mv_weight[idx]) {
646
32.5k
        const CANDIDATE_MV tmp_mv = ref_mv_stack[idx - 1];
647
32.5k
        const uint16_t tmp_ref_mv_weight = ref_mv_weight[idx - 1];
648
32.5k
        ref_mv_stack[idx - 1] = ref_mv_stack[idx];
649
32.5k
        ref_mv_stack[idx] = tmp_mv;
650
32.5k
        ref_mv_weight[idx - 1] = ref_mv_weight[idx];
651
32.5k
        ref_mv_weight[idx] = tmp_ref_mv_weight;
652
32.5k
        nr_len = idx;
653
32.5k
      }
654
89.9k
    }
655
124k
    len = nr_len;
656
124k
  }
657
658
137k
  len = *refmv_count;
659
238k
  while (len > nearest_refmv_count) {
660
100k
    int nr_len = nearest_refmv_count;
661
193k
    for (int idx = nearest_refmv_count + 1; idx < len; ++idx) {
662
92.9k
      if (ref_mv_weight[idx - 1] < ref_mv_weight[idx]) {
663
24.6k
        const CANDIDATE_MV tmp_mv = ref_mv_stack[idx - 1];
664
24.6k
        const uint16_t tmp_ref_mv_weight = ref_mv_weight[idx - 1];
665
24.6k
        ref_mv_stack[idx - 1] = ref_mv_stack[idx];
666
24.6k
        ref_mv_stack[idx] = tmp_mv;
667
24.6k
        ref_mv_weight[idx - 1] = ref_mv_weight[idx];
668
24.6k
        ref_mv_weight[idx] = tmp_ref_mv_weight;
669
24.6k
        nr_len = idx;
670
24.6k
      }
671
92.9k
    }
672
100k
    len = nr_len;
673
100k
  }
674
675
137k
  int mi_width = AOMMIN(mi_size_wide[BLOCK_64X64], xd->width);
676
137k
  mi_width = AOMMIN(mi_width, cm->mi_params.mi_cols - mi_col);
677
137k
  int mi_height = AOMMIN(mi_size_high[BLOCK_64X64], xd->height);
678
137k
  mi_height = AOMMIN(mi_height, cm->mi_params.mi_rows - mi_row);
679
137k
  const int mi_size = AOMMIN(mi_width, mi_height);
680
137k
  if (rf[1] > NONE_FRAME) {
681
    // TODO(jingning, yunqing): Refactor and consolidate the compound and
682
    // single reference frame modes. Reduce unnecessary redundancy.
683
12.7k
    if (*refmv_count < MAX_MV_REF_CANDIDATES) {
684
10.6k
      int_mv ref_id[2][2], ref_diff[2][2];
685
10.6k
      int ref_id_count[2] = { 0 }, ref_diff_count[2] = { 0 };
686
687
14.7k
      for (int idx = 0; abs(max_row_offset) >= 1 && idx < mi_size;) {
688
4.07k
        const MB_MODE_INFO *const candidate = xd->mi[-xd->mi_stride + idx];
689
4.07k
        process_compound_ref_mv_candidate(
690
4.07k
            candidate, cm, rf, ref_id, ref_id_count, ref_diff, ref_diff_count);
691
4.07k
        idx += mi_size_wide[candidate->bsize];
692
4.07k
      }
693
694
15.3k
      for (int idx = 0; abs(max_col_offset) >= 1 && idx < mi_size;) {
695
4.71k
        const MB_MODE_INFO *const candidate = xd->mi[idx * xd->mi_stride - 1];
696
4.71k
        process_compound_ref_mv_candidate(
697
4.71k
            candidate, cm, rf, ref_id, ref_id_count, ref_diff, ref_diff_count);
698
4.71k
        idx += mi_size_high[candidate->bsize];
699
4.71k
      }
700
701
      // Build up the compound mv predictor
702
10.6k
      int_mv comp_list[MAX_MV_REF_CANDIDATES][2];
703
704
32.0k
      for (int idx = 0; idx < 2; ++idx) {
705
21.3k
        int comp_idx = 0;
706
21.3k
        for (int list_idx = 0;
707
30.8k
             list_idx < ref_id_count[idx] && comp_idx < MAX_MV_REF_CANDIDATES;
708
21.3k
             ++list_idx, ++comp_idx)
709
9.48k
          comp_list[comp_idx][idx] = ref_id[idx][list_idx];
710
21.3k
        for (int list_idx = 0;
711
33.6k
             list_idx < ref_diff_count[idx] && comp_idx < MAX_MV_REF_CANDIDATES;
712
21.3k
             ++list_idx, ++comp_idx)
713
12.2k
          comp_list[comp_idx][idx] = ref_diff[idx][list_idx];
714
42.3k
        for (; comp_idx < MAX_MV_REF_CANDIDATES; ++comp_idx)
715
20.9k
          comp_list[comp_idx][idx] = gm_mv_candidates[idx];
716
21.3k
      }
717
718
10.6k
      if (*refmv_count) {
719
3.23k
        assert(*refmv_count == 1);
720
3.23k
        if (comp_list[0][0].as_int == ref_mv_stack[0].this_mv.as_int &&
721
2.51k
            comp_list[0][1].as_int == ref_mv_stack[0].comp_mv.as_int) {
722
2.10k
          ref_mv_stack[*refmv_count].this_mv = comp_list[1][0];
723
2.10k
          ref_mv_stack[*refmv_count].comp_mv = comp_list[1][1];
724
2.10k
        } else {
725
1.13k
          ref_mv_stack[*refmv_count].this_mv = comp_list[0][0];
726
1.13k
          ref_mv_stack[*refmv_count].comp_mv = comp_list[0][1];
727
1.13k
        }
728
3.23k
        ref_mv_weight[*refmv_count] = 2;
729
3.23k
        ++*refmv_count;
730
7.44k
      } else {
731
22.3k
        for (int idx = 0; idx < MAX_MV_REF_CANDIDATES; ++idx) {
732
14.8k
          ref_mv_stack[*refmv_count].this_mv = comp_list[idx][0];
733
14.8k
          ref_mv_stack[*refmv_count].comp_mv = comp_list[idx][1];
734
14.8k
          ref_mv_weight[*refmv_count] = 2;
735
14.8k
          ++*refmv_count;
736
14.8k
        }
737
7.44k
      }
738
10.6k
    }
739
740
12.7k
    assert(*refmv_count >= 2);
741
742
39.6k
    for (int idx = 0; idx < *refmv_count; ++idx) {
743
26.9k
      clamp_mv_ref(&ref_mv_stack[idx].this_mv.as_mv, xd->width << MI_SIZE_LOG2,
744
26.9k
                   xd->height << MI_SIZE_LOG2, xd);
745
26.9k
      clamp_mv_ref(&ref_mv_stack[idx].comp_mv.as_mv, xd->width << MI_SIZE_LOG2,
746
26.9k
                   xd->height << MI_SIZE_LOG2, xd);
747
26.9k
    }
748
124k
  } else {
749
    // Handle single reference frame extension
750
156k
    for (int idx = 0; abs(max_row_offset) >= 1 && idx < mi_size &&
751
109k
                      *refmv_count < MAX_MV_REF_CANDIDATES;) {
752
32.1k
      const MB_MODE_INFO *const candidate = xd->mi[-xd->mi_stride + idx];
753
32.1k
      process_single_ref_mv_candidate(candidate, cm, ref_frame, refmv_count,
754
32.1k
                                      ref_mv_stack, ref_mv_weight);
755
32.1k
      idx += mi_size_wide[candidate->bsize];
756
32.1k
    }
757
758
159k
    for (int idx = 0; abs(max_col_offset) >= 1 && idx < mi_size &&
759
112k
                      *refmv_count < MAX_MV_REF_CANDIDATES;) {
760
35.0k
      const MB_MODE_INFO *const candidate = xd->mi[idx * xd->mi_stride - 1];
761
35.0k
      process_single_ref_mv_candidate(candidate, cm, ref_frame, refmv_count,
762
35.0k
                                      ref_mv_stack, ref_mv_weight);
763
35.0k
      idx += mi_size_high[candidate->bsize];
764
35.0k
    }
765
766
457k
    for (int idx = 0; idx < *refmv_count; ++idx) {
767
333k
      clamp_mv_ref(&ref_mv_stack[idx].this_mv.as_mv, xd->width << MI_SIZE_LOG2,
768
333k
                   xd->height << MI_SIZE_LOG2, xd);
769
333k
    }
770
771
124k
    if (mv_ref_list != NULL) {
772
184k
      for (int idx = *refmv_count; idx < MAX_MV_REF_CANDIDATES; ++idx)
773
60.1k
        mv_ref_list[idx].as_int = gm_mv_candidates[0].as_int;
774
775
314k
      for (int idx = 0; idx < AOMMIN(MAX_MV_REF_CANDIDATES, *refmv_count);
776
189k
           ++idx) {
777
189k
        mv_ref_list[idx].as_int = ref_mv_stack[idx].this_mv.as_int;
778
189k
      }
779
124k
    }
780
124k
  }
781
137k
}
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
137k
                      int_mv *global_mvs, int16_t *mode_context) {
790
137k
  const int mi_row = xd->mi_row;
791
137k
  const int mi_col = xd->mi_col;
792
137k
  int_mv gm_mv[2];
793
794
137k
  if (ref_frame == INTRA_FRAME) {
795
93.0k
    gm_mv[0].as_int = gm_mv[1].as_int = 0;
796
93.0k
    if (global_mvs != NULL) {
797
0
      global_mvs[ref_frame].as_int = INVALID_MV;
798
0
    }
799
93.0k
  } else {
800
44.3k
    const BLOCK_SIZE bsize = mi->bsize;
801
44.3k
    const int allow_high_precision_mv = cm->features.allow_high_precision_mv;
802
44.3k
    const int force_integer_mv = cm->features.cur_frame_force_integer_mv;
803
44.3k
    if (ref_frame < REF_FRAMES) {
804
31.6k
      gm_mv[0] = gm_get_motion_vector(&cm->global_motion[ref_frame],
805
31.6k
                                      allow_high_precision_mv, bsize, mi_col,
806
31.6k
                                      mi_row, force_integer_mv);
807
31.6k
      gm_mv[1].as_int = 0;
808
31.6k
      if (global_mvs != NULL) global_mvs[ref_frame] = gm_mv[0];
809
31.6k
    } else {
810
12.7k
      MV_REFERENCE_FRAME rf[2];
811
12.7k
      av1_set_ref_frame(rf, ref_frame);
812
12.7k
      gm_mv[0] = gm_get_motion_vector(&cm->global_motion[rf[0]],
813
12.7k
                                      allow_high_precision_mv, bsize, mi_col,
814
12.7k
                                      mi_row, force_integer_mv);
815
12.7k
      gm_mv[1] = gm_get_motion_vector(&cm->global_motion[rf[1]],
816
12.7k
                                      allow_high_precision_mv, bsize, mi_col,
817
12.7k
                                      mi_row, force_integer_mv);
818
12.7k
    }
819
44.3k
  }
820
821
137k
  setup_ref_mv_list(cm, xd, ref_frame, &ref_mv_count[ref_frame],
822
137k
                    ref_mv_stack[ref_frame], ref_mv_weight[ref_frame],
823
137k
                    mv_ref_list ? mv_ref_list[ref_frame] : NULL, gm_mv, mi_row,
824
137k
                    mi_col, mode_context);
825
137k
}
826
827
void av1_find_best_ref_mvs(int allow_hp, int_mv *mvlist, int_mv *nearest_mv,
828
115k
                           int_mv *near_mv, int is_integer) {
829
115k
  int i;
830
  // Make sure all the candidates are properly clamped etc
831
345k
  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
832
230k
    lower_mv_precision(&mvlist[i].as_mv, allow_hp, is_integer);
833
230k
  }
834
115k
  *nearest_mv = mvlist[0];
835
115k
  *near_mv = mvlist[1];
836
115k
}
837
838
55.7k
void av1_setup_frame_buf_refs(AV1_COMMON *cm) {
839
55.7k
  cm->cur_frame->order_hint = cm->current_frame.order_hint;
840
55.7k
  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
55.7k
  MV_REFERENCE_FRAME ref_frame;
845
446k
  for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
846
390k
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
847
390k
    if (buf != NULL) {
848
91.4k
      cm->cur_frame->ref_order_hints[ref_frame - LAST_FRAME] = buf->order_hint;
849
91.4k
      cm->cur_frame->ref_display_order_hint[ref_frame - LAST_FRAME] =
850
91.4k
          buf->display_order_hint;
851
91.4k
    }
852
390k
  }
853
55.7k
}
854
855
55.7k
void av1_setup_frame_sign_bias(AV1_COMMON *cm) {
856
55.7k
  MV_REFERENCE_FRAME ref_frame;
857
446k
  for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
858
390k
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
859
390k
    if (cm->seq_params->order_hint_info.enable_order_hint && buf != NULL) {
860
56.9k
      const int ref_order_hint = buf->order_hint;
861
56.9k
      cm->ref_frame_sign_bias[ref_frame] =
862
56.9k
          (get_relative_dist(&cm->seq_params->order_hint_info, ref_order_hint,
863
56.9k
                             (int)cm->current_frame.order_hint) <= 0)
864
56.9k
              ? 0
865
56.9k
              : 1;
866
333k
    } else {
867
333k
      cm->ref_frame_sign_bias[ref_frame] = 0;
868
333k
    }
869
390k
  }
870
55.7k
}
871
872
19.2k
#define MAX_OFFSET_WIDTH 64
873
19.2k
#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
11.9k
                              int blk_col, MV mv, int sign_bias) {
877
11.9k
  const int base_blk_row = (blk_row >> 3) << 3;
878
11.9k
  const int base_blk_col = (blk_col >> 3) << 3;
879
880
11.9k
  const int row_offset = (mv.row >= 0) ? (mv.row >> (4 + MI_SIZE_LOG2))
881
11.9k
                                       : -((-mv.row) >> (4 + MI_SIZE_LOG2));
882
883
11.9k
  const int col_offset = (mv.col >= 0) ? (mv.col >> (4 + MI_SIZE_LOG2))
884
11.9k
                                       : -((-mv.col) >> (4 + MI_SIZE_LOG2));
885
886
11.9k
  const int row =
887
11.9k
      (sign_bias == 1) ? blk_row - row_offset : blk_row + row_offset;
888
11.9k
  const int col =
889
11.9k
      (sign_bias == 1) ? blk_col - col_offset : blk_col + col_offset;
890
891
11.9k
  if (row < 0 || row >= (cm->mi_params.mi_rows >> 1) || col < 0 ||
892
10.4k
      col >= (cm->mi_params.mi_cols >> 1))
893
2.34k
    return 0;
894
895
9.60k
  if (row < base_blk_row - (MAX_OFFSET_HEIGHT >> 3) ||
896
9.60k
      row >= base_blk_row + 8 + (MAX_OFFSET_HEIGHT >> 3) ||
897
9.60k
      col < base_blk_col - (MAX_OFFSET_WIDTH >> 3) ||
898
9.60k
      col >= base_blk_col + 8 + (MAX_OFFSET_WIDTH >> 3))
899
0
    return 0;
900
901
9.60k
  *mi_r = row;
902
9.60k
  *mi_c = col;
903
904
9.60k
  return 1;
905
9.60k
}
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
8.78k
                                   MV_REFERENCE_FRAME start_frame, int dir) {
915
8.78k
  TPL_MV_REF *tpl_mvs_base = cm->tpl_mvs;
916
8.78k
  int ref_offset[REF_FRAMES] = { 0 };
917
918
8.78k
  const RefCntBuffer *const start_frame_buf =
919
8.78k
      get_ref_frame_buf(cm, start_frame);
920
8.78k
  if (start_frame_buf == NULL) return 0;
921
922
8.78k
  if (start_frame_buf->frame_type == KEY_FRAME ||
923
5.55k
      start_frame_buf->frame_type == INTRA_ONLY_FRAME)
924
3.26k
    return 0;
925
926
5.52k
  if (start_frame_buf->mi_rows != cm->mi_params.mi_rows ||
927
5.49k
      start_frame_buf->mi_cols != cm->mi_params.mi_cols)
928
71
    return 0;
929
930
5.44k
  const int start_frame_order_hint = start_frame_buf->order_hint;
931
5.44k
  const unsigned int *const ref_order_hints =
932
5.44k
      &start_frame_buf->ref_order_hints[0];
933
5.44k
  const int cur_order_hint = cm->cur_frame->order_hint;
934
5.44k
  int start_to_current_frame_offset = get_relative_dist(
935
5.44k
      &cm->seq_params->order_hint_info, start_frame_order_hint, cur_order_hint);
936
937
43.5k
  for (MV_REFERENCE_FRAME rf = LAST_FRAME; rf <= INTER_REFS_PER_FRAME; ++rf) {
938
38.1k
    ref_offset[rf] = get_relative_dist(&cm->seq_params->order_hint_info,
939
38.1k
                                       start_frame_order_hint,
940
38.1k
                                       ref_order_hints[rf - LAST_FRAME]);
941
38.1k
  }
942
943
5.44k
  if (dir == 2) start_to_current_frame_offset = -start_to_current_frame_offset;
944
945
5.44k
  MV_REF *mv_ref_base = start_frame_buf->mvs;
946
5.44k
  const int mvs_rows = (cm->mi_params.mi_rows + 1) >> 1;
947
5.44k
  const int mvs_cols = (cm->mi_params.mi_cols + 1) >> 1;
948
949
13.3k
  for (int blk_row = 0; blk_row < mvs_rows; ++blk_row) {
950
25.3k
    for (int blk_col = 0; blk_col < mvs_cols; ++blk_col) {
951
17.5k
      MV_REF *mv_ref = &mv_ref_base[blk_row * mvs_cols + blk_col];
952
17.5k
      MV fwd_mv = mv_ref->mv.as_mv;
953
954
17.5k
      if (mv_ref->ref_frame > INTRA_FRAME) {
955
14.6k
        int_mv this_mv;
956
14.6k
        int mi_r, mi_c;
957
14.6k
        const int ref_frame_offset = ref_offset[mv_ref->ref_frame];
958
959
14.6k
        int pos_valid =
960
14.6k
            abs(ref_frame_offset) <= MAX_FRAME_DISTANCE &&
961
12.4k
            ref_frame_offset > 0 &&
962
12.2k
            abs(start_to_current_frame_offset) <= MAX_FRAME_DISTANCE;
963
964
14.6k
        if (pos_valid) {
965
11.9k
          get_mv_projection(&this_mv.as_mv, fwd_mv,
966
11.9k
                            start_to_current_frame_offset, ref_frame_offset);
967
11.9k
          pos_valid = get_block_position(cm, &mi_r, &mi_c, blk_row, blk_col,
968
11.9k
                                         this_mv.as_mv, dir >> 1);
969
11.9k
        }
970
971
14.6k
        if (pos_valid) {
972
9.60k
          const int mi_offset = mi_r * (cm->mi_params.mi_stride >> 1) + mi_c;
973
974
9.60k
          tpl_mvs_base[mi_offset].mfmv0.as_mv.row = fwd_mv.row;
975
9.60k
          tpl_mvs_base[mi_offset].mfmv0.as_mv.col = fwd_mv.col;
976
9.60k
          tpl_mvs_base[mi_offset].ref_frame_offset = ref_frame_offset;
977
9.60k
        }
978
14.6k
      }
979
17.5k
    }
980
7.85k
  }
981
982
5.44k
  return 1;
983
5.52k
}
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
55.2k
void av1_calculate_ref_frame_side(AV1_COMMON *cm) {
988
55.2k
  const OrderHintInfo *const order_hint_info = &cm->seq_params->order_hint_info;
989
990
55.2k
  memset(cm->ref_frame_side, 0, sizeof(cm->ref_frame_side));
991
55.2k
  if (!order_hint_info->enable_order_hint) return;
992
993
17.2k
  const int cur_order_hint = cm->cur_frame->order_hint;
994
995
138k
  for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
996
120k
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
997
120k
    int order_hint = 0;
998
999
120k
    if (buf != NULL) order_hint = buf->order_hint;
1000
1001
120k
    if (get_relative_dist(order_hint_info, order_hint, cur_order_hint) > 0)
1002
50.8k
      cm->ref_frame_side[ref_frame] = 1;
1003
70.0k
    else if (order_hint == cur_order_hint)
1004
20.4k
      cm->ref_frame_side[ref_frame] = -1;
1005
120k
  }
1006
17.2k
}
1007
1008
3.49k
void av1_setup_motion_field(AV1_COMMON *cm) {
1009
3.49k
  const OrderHintInfo *const order_hint_info = &cm->seq_params->order_hint_info;
1010
1011
3.49k
  if (!order_hint_info->enable_order_hint) return;
1012
1013
3.49k
  TPL_MV_REF *tpl_mvs_base = cm->tpl_mvs;
1014
3.49k
  int size = ((cm->mi_params.mi_rows + MAX_MIB_SIZE) >> 1) *
1015
3.49k
             (cm->mi_params.mi_stride >> 1);
1016
988k
  for (int idx = 0; idx < size; ++idx) {
1017
985k
    tpl_mvs_base[idx].mfmv0.as_int = INVALID_MV;
1018
985k
    tpl_mvs_base[idx].ref_frame_offset = 0;
1019
985k
  }
1020
1021
3.49k
  const int cur_order_hint = cm->cur_frame->order_hint;
1022
3.49k
  const RefCntBuffer *ref_buf[INTER_REFS_PER_FRAME];
1023
3.49k
  int ref_order_hint[INTER_REFS_PER_FRAME];
1024
1025
27.9k
  for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
1026
24.4k
    const int ref_idx = ref_frame - LAST_FRAME;
1027
24.4k
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
1028
24.4k
    int order_hint = 0;
1029
1030
24.4k
    if (buf != NULL) order_hint = buf->order_hint;
1031
1032
24.4k
    ref_buf[ref_idx] = buf;
1033
24.4k
    ref_order_hint[ref_idx] = order_hint;
1034
24.4k
  }
1035
1036
3.49k
  int ref_stamp = MFMV_STACK_SIZE - 1;
1037
1038
3.49k
  if (ref_buf[LAST_FRAME - LAST_FRAME] != NULL) {
1039
3.49k
    const int alt_of_lst_order_hint =
1040
3.49k
        ref_buf[LAST_FRAME - LAST_FRAME]
1041
3.49k
            ->ref_order_hints[ALTREF_FRAME - LAST_FRAME];
1042
1043
3.49k
    const int is_lst_overlay =
1044
3.49k
        (alt_of_lst_order_hint == ref_order_hint[GOLDEN_FRAME - LAST_FRAME]);
1045
3.49k
    if (!is_lst_overlay) motion_field_projection(cm, LAST_FRAME, 2);
1046
3.49k
    --ref_stamp;
1047
3.49k
  }
1048
1049
3.49k
  if (get_relative_dist(order_hint_info,
1050
3.49k
                        ref_order_hint[BWDREF_FRAME - LAST_FRAME],
1051
3.49k
                        cur_order_hint) > 0) {
1052
1.37k
    if (motion_field_projection(cm, BWDREF_FRAME, 0)) --ref_stamp;
1053
1.37k
  }
1054
1055
3.49k
  if (get_relative_dist(order_hint_info,
1056
3.49k
                        ref_order_hint[ALTREF2_FRAME - LAST_FRAME],
1057
3.49k
                        cur_order_hint) > 0) {
1058
1.30k
    if (motion_field_projection(cm, ALTREF2_FRAME, 0)) --ref_stamp;
1059
1.30k
  }
1060
1061
3.49k
  if (get_relative_dist(order_hint_info,
1062
3.49k
                        ref_order_hint[ALTREF_FRAME - LAST_FRAME],
1063
3.49k
                        cur_order_hint) > 0 &&
1064
1.12k
      ref_stamp >= 0)
1065
674
    if (motion_field_projection(cm, ALTREF_FRAME, 0)) --ref_stamp;
1066
1067
3.49k
  if (ref_stamp >= 0) motion_field_projection(cm, LAST2_FRAME, 2);
1068
3.49k
}
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
8.89k
                                  int col_offset, int sign_c) {
1073
8.89k
  const int bw = block_size_wide[mbmi->bsize];
1074
8.89k
  const int bh = block_size_high[mbmi->bsize];
1075
8.89k
  const int x = col_offset * MI_SIZE + sign_c * bw / 2 - 1;
1076
8.89k
  const int y = row_offset * MI_SIZE + sign_r * bh / 2 - 1;
1077
1078
8.89k
  pts[0] = GET_MV_SUBPEL(x);
1079
8.89k
  pts[1] = GET_MV_SUBPEL(y);
1080
8.89k
  pts_inref[0] = pts[0] + mbmi->mv[0].as_mv.col;
1081
8.89k
  pts_inref[1] = pts[1] + mbmi->mv[0].as_mv.row;
1082
8.89k
}
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
454
                          BLOCK_SIZE bsize) {
1087
454
  const int bw = block_size_wide[bsize];
1088
454
  const int bh = block_size_high[bsize];
1089
454
  const int thresh = clamp(AOMMAX(bw, bh), 16, 112);
1090
454
  uint8_t ret = 0;
1091
454
  assert(len <= LEAST_SQUARES_SAMPLES_MAX);
1092
1093
  // Only keep the samples with MV differences within threshold.
1094
1.49k
  for (int i = 0; i < len; ++i) {
1095
1.03k
    const int diff = abs(pts_inref[2 * i] - pts[2 * i] - mv->col) +
1096
1.03k
                     abs(pts_inref[2 * i + 1] - pts[2 * i + 1] - mv->row);
1097
1.03k
    if (diff > thresh) continue;
1098
429
    if (ret != i) {
1099
41
      memcpy(pts + 2 * ret, pts + 2 * i, 2 * sizeof(pts[0]));
1100
41
      memcpy(pts_inref + 2 * ret, pts_inref + 2 * i, 2 * sizeof(pts_inref[0]));
1101
41
    }
1102
429
    ++ret;
1103
429
  }
1104
  // Keep at least 1 sample.
1105
454
  return AOMMAX(ret, 1);
1106
454
}
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
14.7k
                        int *pts_inref) {
1113
14.7k
  const MB_MODE_INFO *const mbmi0 = xd->mi[0];
1114
14.7k
  const int ref_frame = mbmi0->ref_frame[0];
1115
14.7k
  const int up_available = xd->up_available;
1116
14.7k
  const int left_available = xd->left_available;
1117
14.7k
  uint8_t np = 0;
1118
14.7k
  int do_tl = 1;
1119
14.7k
  int do_tr = 1;
1120
14.7k
  const int mi_stride = xd->mi_stride;
1121
14.7k
  const int mi_row = xd->mi_row;
1122
14.7k
  const int mi_col = xd->mi_col;
1123
1124
  // scan the nearest above rows
1125
14.7k
  if (up_available) {
1126
7.26k
    const int mi_row_offset = -1;
1127
7.26k
    const MB_MODE_INFO *mbmi = xd->mi[mi_row_offset * mi_stride];
1128
7.26k
    uint8_t superblock_width = mi_size_wide[mbmi->bsize];
1129
1130
7.26k
    if (xd->width <= superblock_width) {
1131
      // Handle "current block width <= above block width" case.
1132
6.45k
      const int col_offset = -mi_col % superblock_width;
1133
1134
6.45k
      if (col_offset < 0) do_tl = 0;
1135
6.45k
      if (col_offset + superblock_width > xd->width) do_tr = 0;
1136
1137
6.45k
      if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) {
1138
3.75k
        record_samples(mbmi, pts, pts_inref, 0, -1, col_offset, 1);
1139
3.75k
        pts += 2;
1140
3.75k
        pts_inref += 2;
1141
3.75k
        if (++np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX;
1142
3.75k
      }
1143
6.45k
    } else {
1144
      // Handle "current block width > above block width" case.
1145
2.46k
      for (int i = 0; i < AOMMIN(xd->width, cm->mi_params.mi_cols - mi_col);
1146
1.65k
           i += superblock_width) {
1147
1.65k
        mbmi = xd->mi[i + mi_row_offset * mi_stride];
1148
1.65k
        superblock_width = mi_size_wide[mbmi->bsize];
1149
1150
1.65k
        if (mbmi->ref_frame[0] == ref_frame &&
1151
882
            mbmi->ref_frame[1] == NONE_FRAME) {
1152
782
          record_samples(mbmi, pts, pts_inref, 0, -1, i, 1);
1153
782
          pts += 2;
1154
782
          pts_inref += 2;
1155
782
          if (++np >= LEAST_SQUARES_SAMPLES_MAX)
1156
0
            return LEAST_SQUARES_SAMPLES_MAX;
1157
782
        }
1158
1.65k
      }
1159
814
    }
1160
7.26k
  }
1161
14.7k
  assert(np <= LEAST_SQUARES_SAMPLES_MAX);
1162
1163
  // scan the nearest left columns
1164
14.7k
  if (left_available) {
1165
6.40k
    const int mi_col_offset = -1;
1166
6.40k
    const MB_MODE_INFO *mbmi = xd->mi[mi_col_offset];
1167
6.40k
    uint8_t superblock_height = mi_size_high[mbmi->bsize];
1168
1169
6.40k
    if (xd->height <= superblock_height) {
1170
      // Handle "current block height <= above block height" case.
1171
5.74k
      const int row_offset = -mi_row % superblock_height;
1172
1173
5.74k
      if (row_offset < 0) do_tl = 0;
1174
1175
5.74k
      if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) {
1176
2.24k
        record_samples(mbmi, pts, pts_inref, row_offset, 1, 0, -1);
1177
2.24k
        pts += 2;
1178
2.24k
        pts_inref += 2;
1179
2.24k
        np++;
1180
2.24k
        if (np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX;
1181
2.24k
      }
1182
5.74k
    } else {
1183
      // Handle "current block height > above block height" case.
1184
1.99k
      for (int i = 0; i < AOMMIN(xd->height, cm->mi_params.mi_rows - mi_row);
1185
1.34k
           i += superblock_height) {
1186
1.34k
        mbmi = xd->mi[mi_col_offset + i * mi_stride];
1187
1.34k
        superblock_height = mi_size_high[mbmi->bsize];
1188
1189
1.34k
        if (mbmi->ref_frame[0] == ref_frame &&
1190
750
            mbmi->ref_frame[1] == NONE_FRAME) {
1191
723
          record_samples(mbmi, pts, pts_inref, i, 1, 0, -1);
1192
723
          pts += 2;
1193
723
          pts_inref += 2;
1194
723
          if (++np >= LEAST_SQUARES_SAMPLES_MAX)
1195
0
            return LEAST_SQUARES_SAMPLES_MAX;
1196
723
        }
1197
1.34k
      }
1198
658
    }
1199
6.40k
  }
1200
14.7k
  assert(np <= LEAST_SQUARES_SAMPLES_MAX);
1201
1202
  // Top-left block
1203
14.7k
  if (do_tl && left_available && up_available) {
1204
2.33k
    const int mi_row_offset = -1;
1205
2.33k
    const int mi_col_offset = -1;
1206
2.33k
    MB_MODE_INFO *mbmi = xd->mi[mi_col_offset + mi_row_offset * mi_stride];
1207
1208
2.33k
    if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) {
1209
944
      record_samples(mbmi, pts, pts_inref, 0, -1, 0, -1);
1210
944
      pts += 2;
1211
944
      pts_inref += 2;
1212
944
      if (++np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX;
1213
944
    }
1214
2.33k
  }
1215
14.7k
  assert(np <= LEAST_SQUARES_SAMPLES_MAX);
1216
1217
  // Top-right block
1218
14.7k
  if (do_tr &&
1219
13.6k
      has_top_right(cm, xd, mi_row, mi_col, AOMMAX(xd->width, xd->height))) {
1220
10.2k
    const POSITION trb_pos = { -1, xd->width };
1221
10.2k
    const TileInfo *const tile = &xd->tile;
1222
10.2k
    if (is_inside(tile, mi_col, mi_row, &trb_pos)) {
1223
1.18k
      const int mi_row_offset = -1;
1224
1.18k
      const int mi_col_offset = xd->width;
1225
1.18k
      const MB_MODE_INFO *mbmi =
1226
1.18k
          xd->mi[mi_col_offset + mi_row_offset * mi_stride];
1227
1228
1.18k
      if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) {
1229
446
        record_samples(mbmi, pts, pts_inref, 0, -1, xd->width, 1);
1230
446
        if (++np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX;
1231
446
      }
1232
1.18k
    }
1233
10.2k
  }
1234
14.7k
  assert(np <= LEAST_SQUARES_SAMPLES_MAX);
1235
1236
14.7k
  return np;
1237
14.7k
}
1238
1239
55.5k
void av1_setup_skip_mode_allowed(AV1_COMMON *cm) {
1240
55.5k
  const OrderHintInfo *const order_hint_info = &cm->seq_params->order_hint_info;
1241
55.5k
  SkipModeInfo *const skip_mode_info = &cm->current_frame.skip_mode_info;
1242
1243
55.5k
  skip_mode_info->skip_mode_allowed = 0;
1244
55.5k
  skip_mode_info->ref_frame_idx_0 = INVALID_IDX;
1245
55.5k
  skip_mode_info->ref_frame_idx_1 = INVALID_IDX;
1246
1247
55.5k
  if (!order_hint_info->enable_order_hint || frame_is_intra_only(cm) ||
1248
8.11k
      cm->current_frame.reference_mode == SINGLE_REFERENCE)
1249
49.7k
    return;
1250
1251
5.82k
  const int cur_order_hint = cm->current_frame.order_hint;
1252
5.82k
  int ref_order_hints[2] = { -1, INT_MAX };
1253
5.82k
  int ref_idx[2] = { INVALID_IDX, INVALID_IDX };
1254
1255
  // Identify the nearest forward and backward references.
1256
46.5k
  for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
1257
40.7k
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, LAST_FRAME + i);
1258
40.7k
    if (buf == NULL) continue;
1259
1260
40.7k
    const int ref_order_hint = buf->order_hint;
1261
40.7k
    if (get_relative_dist(order_hint_info, ref_order_hint, cur_order_hint) <
1262
40.7k
        0) {
1263
      // Forward reference
1264
24.2k
      if (ref_order_hints[0] == -1 ||
1265
19.7k
          get_relative_dist(order_hint_info, ref_order_hint,
1266
19.7k
                            ref_order_hints[0]) > 0) {
1267
5.52k
        ref_order_hints[0] = ref_order_hint;
1268
5.52k
        ref_idx[0] = i;
1269
5.52k
      }
1270
24.2k
    } else if (get_relative_dist(order_hint_info, ref_order_hint,
1271
16.5k
                                 cur_order_hint) > 0) {
1272
      // Backward reference
1273
13.6k
      if (ref_order_hints[1] == INT_MAX ||
1274
10.6k
          get_relative_dist(order_hint_info, ref_order_hint,
1275
10.6k
                            ref_order_hints[1]) < 0) {
1276
3.41k
        ref_order_hints[1] = ref_order_hint;
1277
3.41k
        ref_idx[1] = i;
1278
3.41k
      }
1279
13.6k
    }
1280
40.7k
  }
1281
1282
5.82k
  if (ref_idx[0] != INVALID_IDX && ref_idx[1] != INVALID_IDX) {
1283
    // == Bi-directional prediction ==
1284
1.64k
    skip_mode_info->skip_mode_allowed = 1;
1285
1.64k
    skip_mode_info->ref_frame_idx_0 = AOMMIN(ref_idx[0], ref_idx[1]);
1286
1.64k
    skip_mode_info->ref_frame_idx_1 = AOMMAX(ref_idx[0], ref_idx[1]);
1287
4.17k
  } else if (ref_idx[0] != INVALID_IDX && ref_idx[1] == INVALID_IDX) {
1288
    // == Forward prediction only ==
1289
    // Identify the second nearest forward reference.
1290
2.82k
    ref_order_hints[1] = -1;
1291
22.5k
    for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
1292
19.7k
      const RefCntBuffer *const buf = get_ref_frame_buf(cm, LAST_FRAME + i);
1293
19.7k
      if (buf == NULL) continue;
1294
1295
19.7k
      const int ref_order_hint = buf->order_hint;
1296
19.7k
      if ((ref_order_hints[0] != -1 &&
1297
19.7k
           get_relative_dist(order_hint_info, ref_order_hint,
1298
19.7k
                             ref_order_hints[0]) < 0) &&
1299
2.74k
          (ref_order_hints[1] == -1 ||
1300
1.94k
           get_relative_dist(order_hint_info, ref_order_hint,
1301
1.94k
                             ref_order_hints[1]) > 0)) {
1302
        // Second closest forward reference
1303
1.06k
        ref_order_hints[1] = ref_order_hint;
1304
1.06k
        ref_idx[1] = i;
1305
1.06k
      }
1306
19.7k
    }
1307
2.82k
    if (ref_order_hints[1] != -1) {
1308
801
      skip_mode_info->skip_mode_allowed = 1;
1309
801
      skip_mode_info->ref_frame_idx_0 = AOMMIN(ref_idx[0], ref_idx[1]);
1310
801
      skip_mode_info->ref_frame_idx_1 = AOMMAX(ref_idx[0], ref_idx[1]);
1311
801
    }
1312
2.82k
  }
1313
5.82k
}
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
34.5k
static int compare_ref_frame_info(const void *arg_a, const void *arg_b) {
1324
34.5k
  const REF_FRAME_INFO *info_a = (REF_FRAME_INFO *)arg_a;
1325
34.5k
  const REF_FRAME_INFO *info_b = (REF_FRAME_INFO *)arg_b;
1326
1327
34.5k
  const int sort_idx_diff = info_a->sort_idx - info_b->sort_idx;
1328
34.5k
  if (sort_idx_diff != 0) return sort_idx_diff;
1329
14.1k
  return info_a->map_idx - info_b->map_idx;
1330
34.5k
}
1331
1332
static AOM_INLINE void set_ref_frame_info(int *remapped_ref_idx, int frame_idx,
1333
16.7k
                                          REF_FRAME_INFO *ref_info) {
1334
16.7k
  assert(frame_idx >= 0 && frame_idx < INTER_REFS_PER_FRAME);
1335
1336
16.7k
  remapped_ref_idx[frame_idx] = ref_info->map_idx;
1337
16.7k
}
1338
1339
void av1_set_frame_refs(AV1_COMMON *const cm, int *remapped_ref_idx,
1340
2.41k
                        int lst_map_idx, int gld_map_idx) {
1341
2.41k
  int lst_frame_sort_idx = -1;
1342
2.41k
  int gld_frame_sort_idx = -1;
1343
1344
2.41k
  assert(cm->seq_params->order_hint_info.enable_order_hint);
1345
2.41k
  assert(cm->seq_params->order_hint_info.order_hint_bits_minus_1 >= 0);
1346
2.41k
  const int cur_order_hint = (int)cm->current_frame.order_hint;
1347
2.41k
  const int cur_frame_sort_idx =
1348
2.41k
      1 << cm->seq_params->order_hint_info.order_hint_bits_minus_1;
1349
1350
2.41k
  REF_FRAME_INFO ref_frame_info[REF_FRAMES];
1351
2.41k
  int ref_flag_list[INTER_REFS_PER_FRAME] = { 0, 0, 0, 0, 0, 0, 0 };
1352
1353
21.7k
  for (int i = 0; i < REF_FRAMES; ++i) {
1354
19.3k
    const int map_idx = i;
1355
1356
19.3k
    ref_frame_info[i].map_idx = map_idx;
1357
19.3k
    ref_frame_info[i].sort_idx = -1;
1358
1359
19.3k
    RefCntBuffer *const buf = cm->ref_frame_map[map_idx];
1360
19.3k
    ref_frame_info[i].buf = buf;
1361
1362
19.3k
    if (buf == NULL) continue;
1363
    // If this assertion fails, there is a reference leak.
1364
19.3k
    assert(buf->ref_count > 0);
1365
1366
17.1k
    const int offset = (int)buf->order_hint;
1367
17.1k
    ref_frame_info[i].sort_idx =
1368
17.1k
        (offset == -1) ? -1
1369
17.1k
                       : cur_frame_sort_idx +
1370
17.1k
                             get_relative_dist(&cm->seq_params->order_hint_info,
1371
17.1k
                                               offset, cur_order_hint);
1372
17.1k
    assert(ref_frame_info[i].sort_idx >= -1);
1373
1374
17.1k
    if (map_idx == lst_map_idx) lst_frame_sort_idx = ref_frame_info[i].sort_idx;
1375
17.1k
    if (map_idx == gld_map_idx) gld_frame_sort_idx = ref_frame_info[i].sort_idx;
1376
17.1k
  }
1377
1378
  // Confirm both LAST_FRAME and GOLDEN_FRAME are valid forward reference
1379
  // frames.
1380
2.41k
  if (lst_frame_sort_idx == -1 || lst_frame_sort_idx >= cur_frame_sort_idx) {
1381
21
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
1382
21
                       "Inter frame requests a look-ahead frame as LAST");
1383
21
  }
1384
2.41k
  if (gld_frame_sort_idx == -1 || gld_frame_sort_idx >= cur_frame_sort_idx) {
1385
6
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
1386
6
                       "Inter frame requests a look-ahead frame as GOLDEN");
1387
6
  }
1388
1389
  // Sort ref frames based on their frame_offset values.
1390
2.41k
  qsort(ref_frame_info, REF_FRAMES, sizeof(REF_FRAME_INFO),
1391
2.41k
        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
2.41k
  int fwd_start_idx = 0, fwd_end_idx = REF_FRAMES - 1;
1397
1398
18.8k
  for (int i = 0; i < REF_FRAMES; i++) {
1399
17.4k
    if (ref_frame_info[i].sort_idx == -1) {
1400
2.18k
      fwd_start_idx++;
1401
2.18k
      continue;
1402
2.18k
    }
1403
1404
15.2k
    if (ref_frame_info[i].sort_idx >= cur_frame_sort_idx) {
1405
1.03k
      fwd_end_idx = i - 1;
1406
1.03k
      break;
1407
1.03k
    }
1408
15.2k
  }
1409
1410
2.41k
  int bwd_start_idx = fwd_end_idx + 1;
1411
2.41k
  int bwd_end_idx = REF_FRAMES - 1;
1412
1413
  // === Backward Reference Frames ===
1414
1415
  // == ALTREF_FRAME ==
1416
2.41k
  if (bwd_start_idx <= bwd_end_idx) {
1417
1.03k
    set_ref_frame_info(remapped_ref_idx, ALTREF_FRAME - LAST_FRAME,
1418
1.03k
                       &ref_frame_info[bwd_end_idx]);
1419
1.03k
    ref_flag_list[ALTREF_FRAME - LAST_FRAME] = 1;
1420
1.03k
    bwd_end_idx--;
1421
1.03k
  }
1422
1423
  // == BWDREF_FRAME ==
1424
2.41k
  if (bwd_start_idx <= bwd_end_idx) {
1425
791
    set_ref_frame_info(remapped_ref_idx, BWDREF_FRAME - LAST_FRAME,
1426
791
                       &ref_frame_info[bwd_start_idx]);
1427
791
    ref_flag_list[BWDREF_FRAME - LAST_FRAME] = 1;
1428
791
    bwd_start_idx++;
1429
791
  }
1430
1431
  // == ALTREF2_FRAME ==
1432
2.41k
  if (bwd_start_idx <= bwd_end_idx) {
1433
480
    set_ref_frame_info(remapped_ref_idx, ALTREF2_FRAME - LAST_FRAME,
1434
480
                       &ref_frame_info[bwd_start_idx]);
1435
480
    ref_flag_list[ALTREF2_FRAME - LAST_FRAME] = 1;
1436
480
  }
1437
1438
  // === Forward Reference Frames ===
1439
1440
16.6k
  for (int i = fwd_start_idx; i <= fwd_end_idx; ++i) {
1441
    // == LAST_FRAME ==
1442
14.2k
    if (ref_frame_info[i].map_idx == lst_map_idx) {
1443
2.39k
      set_ref_frame_info(remapped_ref_idx, LAST_FRAME - LAST_FRAME,
1444
2.39k
                         &ref_frame_info[i]);
1445
2.39k
      ref_flag_list[LAST_FRAME - LAST_FRAME] = 1;
1446
2.39k
    }
1447
1448
    // == GOLDEN_FRAME ==
1449
14.2k
    if (ref_frame_info[i].map_idx == gld_map_idx) {
1450
2.39k
      set_ref_frame_info(remapped_ref_idx, GOLDEN_FRAME - LAST_FRAME,
1451
2.39k
                         &ref_frame_info[i]);
1452
2.39k
      ref_flag_list[GOLDEN_FRAME - LAST_FRAME] = 1;
1453
2.39k
    }
1454
14.2k
  }
1455
1456
2.41k
  assert(ref_flag_list[LAST_FRAME - LAST_FRAME] == 1 &&
1457
2.41k
         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
2.39k
  static const MV_REFERENCE_FRAME ref_frame_list[INTER_REFS_PER_FRAME - 2] = {
1467
2.39k
    LAST2_FRAME, LAST3_FRAME, BWDREF_FRAME, ALTREF2_FRAME, ALTREF_FRAME
1468
2.39k
  };
1469
1470
2.39k
  int ref_idx;
1471
12.5k
  for (ref_idx = 0; ref_idx < (INTER_REFS_PER_FRAME - 2); ref_idx++) {
1472
10.6k
    const MV_REFERENCE_FRAME ref_frame = ref_frame_list[ref_idx];
1473
1474
10.6k
    if (ref_flag_list[ref_frame - LAST_FRAME] == 1) continue;
1475
1476
12.8k
    while (fwd_start_idx <= fwd_end_idx &&
1477
12.3k
           (ref_frame_info[fwd_end_idx].map_idx == lst_map_idx ||
1478
10.2k
            ref_frame_info[fwd_end_idx].map_idx == gld_map_idx)) {
1479
3.79k
      fwd_end_idx--;
1480
3.79k
    }
1481
9.04k
    if (fwd_start_idx > fwd_end_idx) break;
1482
1483
8.52k
    set_ref_frame_info(remapped_ref_idx, ref_frame - LAST_FRAME,
1484
8.52k
                       &ref_frame_info[fwd_end_idx]);
1485
8.52k
    ref_flag_list[ref_frame - LAST_FRAME] = 1;
1486
1487
8.52k
    fwd_end_idx--;
1488
8.52k
  }
1489
1490
  // Assign all the remaining frame(s), if any, to the earliest reference
1491
  // frame.
1492
4.17k
  for (; ref_idx < (INTER_REFS_PER_FRAME - 2); ref_idx++) {
1493
1.78k
    const MV_REFERENCE_FRAME ref_frame = ref_frame_list[ref_idx];
1494
1.78k
    if (ref_flag_list[ref_frame - LAST_FRAME] == 1) continue;
1495
1.13k
    set_ref_frame_info(remapped_ref_idx, ref_frame - LAST_FRAME,
1496
1.13k
                       &ref_frame_info[fwd_start_idx]);
1497
1.13k
    ref_flag_list[ref_frame - LAST_FRAME] = 1;
1498
1.13k
  }
1499
1500
19.1k
  for (int i = 0; i < INTER_REFS_PER_FRAME; i++) {
1501
    assert(ref_flag_list[i] == 1);
1502
16.7k
  }
1503
2.39k
}