Coverage Report

Created: 2026-04-01 07:49

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