Coverage Report

Created: 2025-09-08 07:52

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