Coverage Report

Created: 2026-01-20 07:37

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