Coverage Report

Created: 2025-12-31 07:53

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