Coverage Report

Created: 2026-05-16 06:27

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
14.0M
void av1_get_mv_projection(MV *output, MV ref, int num, int den) {
28
14.0M
  den = AOMMIN(den, MAX_FRAME_DISTANCE);
29
14.0M
  num = num > 0 ? AOMMIN(num, MAX_FRAME_DISTANCE)
30
14.0M
                : AOMMAX(num, -MAX_FRAME_DISTANCE);
31
14.0M
  const int mv_row =
32
14.0M
      ROUND_POWER_OF_TWO_SIGNED(ref.row * num * div_mult[den], 14);
33
14.0M
  const int mv_col =
34
14.0M
      ROUND_POWER_OF_TWO_SIGNED(ref.col * num * div_mult[den], 14);
35
14.0M
  const int clamp_max = MV_UPP - 1;
36
14.0M
  const int clamp_min = MV_LOW + 1;
37
14.0M
  output->row = (int16_t)clamp(mv_row, clamp_min, clamp_max);
38
14.0M
  output->col = (int16_t)clamp(mv_col, clamp_min, clamp_max);
39
14.0M
}
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
4.72M
                        int x_mis, int y_mis) {
44
4.72M
  const int frame_mvs_stride = ROUND_POWER_OF_TWO(cm->mi_params.mi_cols, 1);
45
4.72M
  MV_REF *frame_mvs =
46
4.72M
      cm->cur_frame->mvs + (mi_row >> 1) * frame_mvs_stride + (mi_col >> 1);
47
4.72M
  x_mis = ROUND_POWER_OF_TWO(x_mis, 1);
48
4.72M
  y_mis = ROUND_POWER_OF_TWO(y_mis, 1);
49
4.72M
  int w, h;
50
51
15.0M
  for (h = 0; h < y_mis; h++) {
52
10.3M
    MV_REF *mv = frame_mvs;
53
64.1M
    for (w = 0; w < x_mis; w++) {
54
53.8M
      mv->ref_frame = NONE_FRAME;
55
53.8M
      mv->mv.as_int = 0;
56
57
161M
      for (int idx = 0; idx < 2; ++idx) {
58
107M
        MV_REFERENCE_FRAME ref_frame = mi->ref_frame[idx];
59
107M
        if (ref_frame > INTRA_FRAME) {
60
75.8M
          int8_t ref_idx = cm->ref_frame_side[ref_frame];
61
75.8M
          if (ref_idx) continue;
62
64.6M
          if ((abs(mi->mv[idx].as_mv.row) > REFMVS_LIMIT) ||
63
63.0M
              (abs(mi->mv[idx].as_mv.col) > REFMVS_LIMIT))
64
2.32M
            continue;
65
62.3M
          mv->ref_frame = ref_frame;
66
62.3M
          mv->mv.as_int = mi->mv[idx].as_int;
67
62.3M
        }
68
107M
      }
69
53.8M
      mv++;
70
53.8M
    }
71
10.3M
    frame_mvs += frame_mvs_stride;
72
10.3M
  }
73
4.72M
}
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
22.6M
    uint16_t weight) {
81
22.6M
  if (!is_inter_block(candidate)) return;
82
22.6M
  assert(weight % 2 == 0);
83
19.3M
  int index, ref;
84
85
19.3M
  if (rf[1] == NONE_FRAME) {
86
    // single reference frame
87
51.6M
    for (ref = 0; ref < 2; ++ref) {
88
34.3M
      if (candidate->ref_frame[ref] == rf[0]) {
89
14.7M
        const int is_gm_block =
90
14.7M
            is_global_mv_block(candidate, gm_params[rf[0]].wmtype);
91
14.7M
        const int_mv this_refmv =
92
14.7M
            is_gm_block ? gm_mv_candidates[0] : get_block_mv(candidate, ref);
93
29.8M
        for (index = 0; index < *refmv_count; ++index) {
94
21.7M
          if (ref_mv_stack[index].this_mv.as_int == this_refmv.as_int) {
95
6.62M
            ref_mv_weight[index] += weight;
96
6.62M
            break;
97
6.62M
          }
98
21.7M
        }
99
100
        // Add a new item to the list.
101
14.7M
        if (index == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
102
8.05M
          ref_mv_stack[index].this_mv = this_refmv;
103
8.05M
          ref_mv_weight[index] = weight;
104
8.05M
          ++(*refmv_count);
105
8.05M
        }
106
14.7M
        if (have_newmv_in_inter_mode(candidate->mode)) ++*newmv_count;
107
14.7M
        ++*ref_match_count;
108
14.7M
      }
109
34.3M
    }
110
17.2M
  } else {
111
    // compound reference frame
112
2.16M
    if (candidate->ref_frame[0] == rf[0] && candidate->ref_frame[1] == rf[1]) {
113
810k
      int_mv this_refmv[2];
114
115
2.43M
      for (ref = 0; ref < 2; ++ref) {
116
1.61M
        if (is_global_mv_block(candidate, gm_params[rf[ref]].wmtype))
117
33.8k
          this_refmv[ref] = gm_mv_candidates[ref];
118
1.58M
        else
119
1.58M
          this_refmv[ref] = get_block_mv(candidate, ref);
120
1.61M
      }
121
122
1.13M
      for (index = 0; index < *refmv_count; ++index) {
123
656k
        if ((ref_mv_stack[index].this_mv.as_int == this_refmv[0].as_int) &&
124
368k
            (ref_mv_stack[index].comp_mv.as_int == this_refmv[1].as_int)) {
125
327k
          ref_mv_weight[index] += weight;
126
327k
          break;
127
327k
        }
128
656k
      }
129
130
      // Add a new item to the list.
131
810k
      if (index == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
132
481k
        ref_mv_stack[index].this_mv = this_refmv[0];
133
481k
        ref_mv_stack[index].comp_mv = this_refmv[1];
134
481k
        ref_mv_weight[index] = weight;
135
481k
        ++(*refmv_count);
136
481k
      }
137
810k
      if (have_newmv_in_inter_mode(candidate->mode)) ++*newmv_count;
138
810k
      ++*ref_match_count;
139
810k
    }
140
2.16M
  }
141
19.3M
}
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
7.47M
                                 int *processed_rows) {
150
7.47M
  int end_mi = AOMMIN(xd->width, cm->mi_params.mi_cols - mi_col);
151
7.47M
  end_mi = AOMMIN(end_mi, mi_size_wide[BLOCK_64X64]);
152
7.47M
  const int width_8x8 = mi_size_wide[BLOCK_8X8];
153
7.47M
  const int width_16x16 = mi_size_wide[BLOCK_16X16];
154
7.47M
  int col_offset = 0;
155
  // TODO(jingning): Revisit this part after cb4x4 is stable.
156
7.47M
  if (abs(row_offset) > 1) {
157
4.14M
    col_offset = 1;
158
4.14M
    if ((mi_col & 0x01) && xd->width < width_8x8) --col_offset;
159
4.14M
  }
160
7.47M
  const int use_step_16 = (xd->width >= 16);
161
7.47M
  MB_MODE_INFO **const candidate_mi0 = xd->mi + row_offset * xd->mi_stride;
162
163
16.1M
  for (int i = 0; i < end_mi;) {
164
8.70M
    const MB_MODE_INFO *const candidate = candidate_mi0[col_offset + i];
165
8.70M
    const int candidate_bsize = candidate->bsize;
166
8.70M
    const int n4_w = mi_size_wide[candidate_bsize];
167
8.70M
    int len = AOMMIN(xd->width, n4_w);
168
8.70M
    if (use_step_16)
169
371k
      len = AOMMAX(width_16x16, len);
170
8.33M
    else if (abs(row_offset) > 1)
171
4.71M
      len = AOMMAX(len, width_8x8);
172
173
8.70M
    uint16_t weight = 2;
174
8.70M
    if (xd->width >= width_8x8 && xd->width <= n4_w) {
175
4.68M
      uint16_t inc = AOMMIN(-max_row_offset + row_offset + 1,
176
4.68M
                            mi_size_high[candidate_bsize]);
177
      // Obtain range used in weight calculation.
178
4.68M
      weight = AOMMAX(weight, inc);
179
      // Update processed rows.
180
4.68M
      *processed_rows = inc - row_offset - 1;
181
4.68M
    }
182
183
8.70M
    add_ref_mv_candidate(candidate, rf, refmv_count, ref_match_count,
184
8.70M
                         newmv_count, ref_mv_stack, ref_mv_weight,
185
8.70M
                         gm_mv_candidates, cm->global_motion, len * weight);
186
187
8.70M
    i += len;
188
8.70M
  }
189
7.47M
}
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
7.59M
                                 int *processed_cols) {
198
7.59M
  int end_mi = AOMMIN(xd->height, cm->mi_params.mi_rows - mi_row);
199
7.59M
  end_mi = AOMMIN(end_mi, mi_size_high[BLOCK_64X64]);
200
7.59M
  const int n8_h_8 = mi_size_high[BLOCK_8X8];
201
7.59M
  const int n8_h_16 = mi_size_high[BLOCK_16X16];
202
7.59M
  int i;
203
7.59M
  int row_offset = 0;
204
7.59M
  if (abs(col_offset) > 1) {
205
4.21M
    row_offset = 1;
206
4.21M
    if ((mi_row & 0x01) && xd->height < n8_h_8) --row_offset;
207
4.21M
  }
208
7.59M
  const int use_step_16 = (xd->height >= 16);
209
210
16.5M
  for (i = 0; i < end_mi;) {
211
8.91M
    const MB_MODE_INFO *const candidate =
212
8.91M
        xd->mi[(row_offset + i) * xd->mi_stride + col_offset];
213
8.91M
    const int candidate_bsize = candidate->bsize;
214
8.91M
    const int n4_h = mi_size_high[candidate_bsize];
215
8.91M
    int len = AOMMIN(xd->height, n4_h);
216
8.91M
    if (use_step_16)
217
386k
      len = AOMMAX(n8_h_16, len);
218
8.53M
    else if (abs(col_offset) > 1)
219
4.82M
      len = AOMMAX(len, n8_h_8);
220
221
8.91M
    int weight = 2;
222
8.91M
    if (xd->height >= n8_h_8 && xd->height <= n4_h) {
223
4.12M
      int inc = AOMMIN(-max_col_offset + col_offset + 1,
224
4.12M
                       mi_size_wide[candidate_bsize]);
225
      // Obtain range used in weight calculation.
226
4.12M
      weight = AOMMAX(weight, inc);
227
      // Update processed cols.
228
4.12M
      *processed_cols = inc - col_offset - 1;
229
4.12M
    }
230
231
8.91M
    add_ref_mv_candidate(candidate, rf, refmv_count, ref_match_count,
232
8.91M
                         newmv_count, ref_mv_stack, ref_mv_weight,
233
8.91M
                         gm_mv_candidates, cm->global_motion, len * weight);
234
235
8.91M
    i += len;
236
8.91M
  }
237
7.59M
}
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
5.39M
                                 uint8_t *refmv_count) {
247
5.39M
  const TileInfo *const tile = &xd->tile;
248
5.39M
  POSITION mi_pos;
249
250
5.39M
  mi_pos.row = row_offset;
251
5.39M
  mi_pos.col = col_offset;
252
253
5.39M
  if (is_inside(tile, mi_col, mi_row, &mi_pos)) {
254
5.04M
    const MB_MODE_INFO *const candidate =
255
5.04M
        xd->mi[mi_pos.row * xd->mi_stride + mi_pos.col];
256
5.04M
    const int len = mi_size_wide[BLOCK_8X8];
257
258
5.04M
    add_ref_mv_candidate(candidate, rf, refmv_count, ref_match_count,
259
5.04M
                         newmv_count, ref_mv_stack, ref_mv_weight,
260
5.04M
                         gm_mv_candidates, cm->global_motion, 2 * len);
261
5.04M
  }  // Analyze a single 8x8 block motion information.
262
5.39M
}
263
264
static int has_top_right(const AV1_COMMON *cm, const MACROBLOCKD *xd,
265
4.98M
                         int mi_row, int mi_col, int bs) {
266
4.98M
  const int sb_mi_size = mi_size_wide[cm->seq_params->sb_size];
267
4.98M
  const int mask_row = mi_row & (sb_mi_size - 1);
268
4.98M
  const int mask_col = mi_col & (sb_mi_size - 1);
269
270
4.98M
  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
4.75M
  int has_tr = !((mask_row & bs) && (mask_col & bs));
274
275
  // bs > 0 and bs is a power of 2
276
4.75M
  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
7.10M
  while (bs < sb_mi_size) {
282
6.73M
    if (mask_col & bs) {
283
3.06M
      if ((mask_col & (2 * bs)) && (mask_row & (2 * bs))) {
284
722k
        has_tr = 0;
285
722k
        break;
286
722k
      }
287
3.66M
    } else {
288
3.66M
      break;
289
3.66M
    }
290
2.34M
    bs <<= 1;
291
2.34M
  }
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
4.75M
  if (xd->width < xd->height) {
296
1.05M
    if (!xd->is_last_vertical_rect) has_tr = 1;
297
1.05M
  }
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
4.75M
  if (xd->width > xd->height) {
302
1.75M
    if (!xd->is_first_horizontal_rect) has_tr = 0;
303
1.75M
  }
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
4.75M
  if (xd->mi[0]->partition == PARTITION_VERT_A) {
309
153k
    if (xd->width == xd->height)
310
98.7k
      if (mask_row & bs) has_tr = 0;
311
153k
  }
312
313
4.75M
  return has_tr;
314
4.75M
}
315
316
static int check_sb_border(const int mi_row, const int mi_col,
317
4.96M
                           const int row_offset, const int col_offset) {
318
4.96M
  const int sb_mi_size = mi_size_wide[BLOCK_64X64];
319
4.96M
  const int row = mi_row & (sb_mi_size - 1);
320
4.96M
  const int col = mi_col & (sb_mi_size - 1);
321
322
4.96M
  if (row + row_offset < 0 || row + row_offset >= sb_mi_size ||
323
4.23M
      col + col_offset < 0 || col + col_offset >= sb_mi_size)
324
1.72M
    return 0;
325
326
3.23M
  return 1;
327
4.96M
}
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
13.5M
                          int16_t *mode_context) {
336
13.5M
  POSITION mi_pos;
337
13.5M
  mi_pos.row = (mi_row & 0x01) ? blk_row : blk_row + 1;
338
13.5M
  mi_pos.col = (mi_col & 0x01) ? blk_col : blk_col + 1;
339
340
13.5M
  if (!is_inside(&xd->tile, mi_col, mi_row, &mi_pos)) return 0;
341
342
13.4M
  const TPL_MV_REF *prev_frame_mvs =
343
13.4M
      cm->tpl_mvs +
344
13.4M
      ((mi_row + mi_pos.row) >> 1) * (cm->mi_params.mi_stride >> 1) +
345
13.4M
      ((mi_col + mi_pos.col) >> 1);
346
13.4M
  if (prev_frame_mvs->mfmv0.as_int == INVALID_MV) return 0;
347
348
979k
  MV_REFERENCE_FRAME rf[2];
349
979k
  av1_set_ref_frame(rf, ref_frame);
350
351
979k
  const uint16_t weight_unit = 1;  // mi_size_wide[BLOCK_8X8];
352
979k
  const int cur_frame_index = cm->cur_frame->order_hint;
353
979k
  const RefCntBuffer *const buf_0 = get_ref_frame_buf(cm, rf[0]);
354
979k
  const int frame0_index = buf_0->order_hint;
355
979k
  const int cur_offset_0 = get_relative_dist(&cm->seq_params->order_hint_info,
356
979k
                                             cur_frame_index, frame0_index);
357
979k
  int idx;
358
979k
  const int allow_high_precision_mv = cm->features.allow_high_precision_mv;
359
979k
  const int force_integer_mv = cm->features.cur_frame_force_integer_mv;
360
361
979k
  int_mv this_refmv;
362
979k
  av1_get_mv_projection(&this_refmv.as_mv, prev_frame_mvs->mfmv0.as_mv,
363
979k
                        cur_offset_0, prev_frame_mvs->ref_frame_offset);
364
979k
  lower_mv_precision(&this_refmv.as_mv, allow_high_precision_mv,
365
979k
                     force_integer_mv);
366
367
979k
  if (rf[1] == NONE_FRAME) {
368
463k
    if (blk_row == 0 && blk_col == 0) {
369
74.4k
      if (abs(this_refmv.as_mv.row - gm_mv_candidates[0].as_mv.row) >= 16 ||
370
61.4k
          abs(this_refmv.as_mv.col - gm_mv_candidates[0].as_mv.col) >= 16)
371
38.1k
        mode_context[ref_frame] |= (1 << GLOBALMV_OFFSET);
372
74.4k
    }
373
374
1.02M
    for (idx = 0; idx < *refmv_count; ++idx)
375
928k
      if (this_refmv.as_int == ref_mv_stack[idx].this_mv.as_int) break;
376
377
463k
    if (idx < *refmv_count) ref_mv_weight[idx] += 2 * weight_unit;
378
379
463k
    if (idx == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
380
95.2k
      ref_mv_stack[idx].this_mv.as_int = this_refmv.as_int;
381
95.2k
      ref_mv_weight[idx] = 2 * weight_unit;
382
95.2k
      ++(*refmv_count);
383
95.2k
    }
384
515k
  } else {
385
    // Process compound inter mode
386
515k
    const RefCntBuffer *const buf_1 = get_ref_frame_buf(cm, rf[1]);
387
515k
    const int frame1_index = buf_1->order_hint;
388
515k
    const int cur_offset_1 = get_relative_dist(&cm->seq_params->order_hint_info,
389
515k
                                               cur_frame_index, frame1_index);
390
515k
    int_mv comp_refmv;
391
515k
    av1_get_mv_projection(&comp_refmv.as_mv, prev_frame_mvs->mfmv0.as_mv,
392
515k
                          cur_offset_1, prev_frame_mvs->ref_frame_offset);
393
515k
    lower_mv_precision(&comp_refmv.as_mv, allow_high_precision_mv,
394
515k
                       force_integer_mv);
395
396
515k
    if (blk_row == 0 && blk_col == 0) {
397
43.0k
      if (abs(this_refmv.as_mv.row - gm_mv_candidates[0].as_mv.row) >= 16 ||
398
39.9k
          abs(this_refmv.as_mv.col - gm_mv_candidates[0].as_mv.col) >= 16 ||
399
25.5k
          abs(comp_refmv.as_mv.row - gm_mv_candidates[1].as_mv.row) >= 16 ||
400
24.7k
          abs(comp_refmv.as_mv.col - gm_mv_candidates[1].as_mv.col) >= 16)
401
19.9k
        mode_context[ref_frame] |= (1 << GLOBALMV_OFFSET);
402
43.0k
    }
403
404
905k
    for (idx = 0; idx < *refmv_count; ++idx) {
405
842k
      if (this_refmv.as_int == ref_mv_stack[idx].this_mv.as_int &&
406
461k
          comp_refmv.as_int == ref_mv_stack[idx].comp_mv.as_int)
407
453k
        break;
408
842k
    }
409
410
515k
    if (idx < *refmv_count) ref_mv_weight[idx] += 2 * weight_unit;
411
412
515k
    if (idx == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
413
64.3k
      ref_mv_stack[idx].this_mv.as_int = this_refmv.as_int;
414
64.3k
      ref_mv_stack[idx].comp_mv.as_int = comp_refmv.as_int;
415
64.3k
      ref_mv_weight[idx] = 2 * weight_unit;
416
64.3k
      ++(*refmv_count);
417
64.3k
    }
418
515k
  }
419
420
979k
  return 1;
421
13.4M
}
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
647k
    int ref_id_count[2], int_mv ref_diff[2][2], int ref_diff_count[2]) {
427
1.94M
  for (int rf_idx = 0; rf_idx < 2; ++rf_idx) {
428
1.29M
    MV_REFERENCE_FRAME can_rf = candidate->ref_frame[rf_idx];
429
430
3.88M
    for (int cmp_idx = 0; cmp_idx < 2; ++cmp_idx) {
431
2.58M
      if (can_rf == rf[cmp_idx] && ref_id_count[cmp_idx] < 2) {
432
749k
        ref_id[cmp_idx][ref_id_count[cmp_idx]] = candidate->mv[rf_idx];
433
749k
        ++ref_id_count[cmp_idx];
434
1.83M
      } else if (can_rf > INTRA_FRAME && ref_diff_count[cmp_idx] < 2) {
435
1.00M
        int_mv this_mv = candidate->mv[rf_idx];
436
1.00M
        if (cm->ref_frame_sign_bias[can_rf] !=
437
1.00M
            cm->ref_frame_sign_bias[rf[cmp_idx]]) {
438
167k
          this_mv.as_mv.row = -this_mv.as_mv.row;
439
167k
          this_mv.as_mv.col = -this_mv.as_mv.col;
440
167k
        }
441
1.00M
        ref_diff[cmp_idx][ref_diff_count[cmp_idx]] = this_mv;
442
1.00M
        ++ref_diff_count[cmp_idx];
443
1.00M
      }
444
2.58M
    }
445
1.29M
  }
446
647k
}
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
1.85M
    uint16_t ref_mv_weight[MAX_REF_MV_STACK_SIZE]) {
453
5.56M
  for (int rf_idx = 0; rf_idx < 2; ++rf_idx) {
454
3.70M
    if (candidate->ref_frame[rf_idx] > INTRA_FRAME) {
455
1.47M
      int_mv this_mv = candidate->mv[rf_idx];
456
1.47M
      if (cm->ref_frame_sign_bias[candidate->ref_frame[rf_idx]] !=
457
1.47M
          cm->ref_frame_sign_bias[ref_frame]) {
458
39.5k
        this_mv.as_mv.row = -this_mv.as_mv.row;
459
39.5k
        this_mv.as_mv.col = -this_mv.as_mv.col;
460
39.5k
      }
461
1.47M
      int stack_idx;
462
1.73M
      for (stack_idx = 0; stack_idx < *refmv_count; ++stack_idx) {
463
1.38M
        const int_mv stack_mv = ref_mv_stack[stack_idx].this_mv;
464
1.38M
        if (this_mv.as_int == stack_mv.as_int) break;
465
1.38M
      }
466
467
1.47M
      if (stack_idx == *refmv_count) {
468
351k
        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
351k
        ref_mv_weight[stack_idx] = 2;
473
351k
        ++(*refmv_count);
474
351k
      }
475
1.47M
    }
476
3.70M
  }
477
1.85M
}
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
3.49M
    int mi_row, int mi_col, int16_t *mode_context) {
486
3.49M
  const int bs = AOMMAX(xd->width, xd->height);
487
3.49M
  const int has_tr = has_top_right(cm, xd, mi_row, mi_col, bs);
488
3.49M
  MV_REFERENCE_FRAME rf[2];
489
490
3.49M
  const TileInfo *const tile = &xd->tile;
491
3.49M
  int max_row_offset = 0, max_col_offset = 0;
492
3.49M
  const int row_adj = (xd->height < mi_size_high[BLOCK_8X8]) && (mi_row & 0x01);
493
3.49M
  const int col_adj = (xd->width < mi_size_wide[BLOCK_8X8]) && (mi_col & 0x01);
494
3.49M
  int processed_rows = 0;
495
3.49M
  int processed_cols = 0;
496
497
3.49M
  av1_set_ref_frame(rf, ref_frame);
498
3.49M
  mode_context[ref_frame] = 0;
499
3.49M
  *refmv_count = 0;
500
501
  // Find valid maximum row/col offset.
502
3.49M
  if (xd->up_available) {
503
3.32M
    max_row_offset = -(MVREF_ROW_COLS << 1) + row_adj;
504
505
3.32M
    if (xd->height < mi_size_high[BLOCK_8X8])
506
808k
      max_row_offset = -(2 << 1) + row_adj;
507
508
3.32M
    max_row_offset = find_valid_row_offset(tile, mi_row, max_row_offset);
509
3.32M
  }
510
511
3.49M
  if (xd->left_available) {
512
3.38M
    max_col_offset = -(MVREF_ROW_COLS << 1) + col_adj;
513
514
3.38M
    if (xd->width < mi_size_wide[BLOCK_8X8])
515
622k
      max_col_offset = -(2 << 1) + col_adj;
516
517
3.38M
    max_col_offset = find_valid_col_offset(tile, mi_col, max_col_offset);
518
3.38M
  }
519
520
3.49M
  uint8_t col_match_count = 0;
521
3.49M
  uint8_t row_match_count = 0;
522
3.49M
  uint8_t newmv_count = 0;
523
524
  // Scan the first above row mode info. row_offset = -1;
525
3.49M
  if (abs(max_row_offset) >= 1)
526
3.32M
    scan_row_mbmi(cm, xd, mi_col, rf, -1, ref_mv_stack, ref_mv_weight,
527
3.32M
                  refmv_count, &row_match_count, &newmv_count, gm_mv_candidates,
528
3.32M
                  max_row_offset, &processed_rows);
529
  // Scan the first left column mode info. col_offset = -1;
530
3.49M
  if (abs(max_col_offset) >= 1)
531
3.38M
    scan_col_mbmi(cm, xd, mi_row, rf, -1, ref_mv_stack, ref_mv_weight,
532
3.38M
                  refmv_count, &col_match_count, &newmv_count, gm_mv_candidates,
533
3.38M
                  max_col_offset, &processed_cols);
534
  // Check top-right boundary
535
3.49M
  if (has_tr)
536
1.89M
    scan_blk_mbmi(cm, xd, mi_row, mi_col, rf, -1, xd->width, ref_mv_stack,
537
1.89M
                  ref_mv_weight, &row_match_count, &newmv_count,
538
1.89M
                  gm_mv_candidates, refmv_count);
539
540
3.49M
  const uint8_t nearest_match = (row_match_count > 0) + (col_match_count > 0);
541
3.49M
  const uint8_t nearest_refmv_count = *refmv_count;
542
543
  // TODO(yunqing): for comp_search, do it for all 3 cases.
544
8.59M
  for (int idx = 0; idx < nearest_refmv_count; ++idx)
545
5.10M
    ref_mv_weight[idx] += REF_CAT_LEVEL;
546
547
3.49M
  if (cm->features.allow_ref_frame_mvs) {
548
2.91M
    int is_available = 0;
549
2.91M
    const int voffset = AOMMAX(mi_size_high[BLOCK_8X8], xd->height);
550
2.91M
    const int hoffset = AOMMAX(mi_size_wide[BLOCK_8X8], xd->width);
551
2.91M
    const int blk_row_end = AOMMIN(xd->height, mi_size_high[BLOCK_64X64]);
552
2.91M
    const int blk_col_end = AOMMIN(xd->width, mi_size_wide[BLOCK_64X64]);
553
554
2.91M
    const int tpl_sample_pos[3][2] = {
555
2.91M
      { voffset, -2 },
556
2.91M
      { voffset, hoffset },
557
2.91M
      { voffset - 2, hoffset },
558
2.91M
    };
559
2.91M
    const int allow_extension = (xd->height >= mi_size_high[BLOCK_8X8]) &&
560
2.23M
                                (xd->height < mi_size_high[BLOCK_64X64]) &&
561
2.08M
                                (xd->width >= mi_size_wide[BLOCK_8X8]) &&
562
1.69M
                                (xd->width < mi_size_wide[BLOCK_64X64]);
563
564
2.91M
    const int step_h = (xd->height >= mi_size_high[BLOCK_64X64])
565
2.91M
                           ? mi_size_high[BLOCK_16X16]
566
2.91M
                           : mi_size_high[BLOCK_8X8];
567
2.91M
    const int step_w = (xd->width >= mi_size_wide[BLOCK_64X64])
568
2.91M
                           ? mi_size_wide[BLOCK_16X16]
569
2.91M
                           : mi_size_wide[BLOCK_8X8];
570
571
7.80M
    for (int blk_row = 0; blk_row < blk_row_end; blk_row += step_h) {
572
15.2M
      for (int blk_col = 0; blk_col < blk_col_end; blk_col += step_w) {
573
10.3M
        int ret = add_tpl_ref_mv(cm, xd, mi_row, mi_col, ref_frame, blk_row,
574
10.3M
                                 blk_col, gm_mv_candidates, refmv_count,
575
10.3M
                                 ref_mv_stack, ref_mv_weight, mode_context);
576
10.3M
        if (blk_row == 0 && blk_col == 0) is_available = ret;
577
10.3M
      }
578
4.89M
    }
579
580
2.91M
    if (is_available == 0) mode_context[ref_frame] |= (1 << GLOBALMV_OFFSET);
581
582
7.87M
    for (int i = 0; i < 3 && allow_extension; ++i) {
583
4.96M
      const int blk_row = tpl_sample_pos[i][0];
584
4.96M
      const int blk_col = tpl_sample_pos[i][1];
585
586
4.96M
      if (!check_sb_border(mi_row, mi_col, blk_row, blk_col)) continue;
587
3.23M
      add_tpl_ref_mv(cm, xd, mi_row, mi_col, ref_frame, blk_row, blk_col,
588
3.23M
                     gm_mv_candidates, refmv_count, ref_mv_stack, ref_mv_weight,
589
3.23M
                     mode_context);
590
3.23M
    }
591
2.91M
  }
592
593
3.49M
  uint8_t dummy_newmv_count = 0;
594
595
  // Scan the second outer area.
596
3.49M
  scan_blk_mbmi(cm, xd, mi_row, mi_col, rf, -1, -1, ref_mv_stack, ref_mv_weight,
597
3.49M
                &row_match_count, &dummy_newmv_count, gm_mv_candidates,
598
3.49M
                refmv_count);
599
600
10.4M
  for (int idx = 2; idx <= MVREF_ROW_COLS; ++idx) {
601
6.99M
    const int row_offset = -(idx << 1) + 1 + row_adj;
602
6.99M
    const int col_offset = -(idx << 1) + 1 + col_adj;
603
604
6.99M
    if (abs(row_offset) <= abs(max_row_offset) &&
605
5.77M
        abs(row_offset) > processed_rows)
606
4.14M
      scan_row_mbmi(cm, xd, mi_col, rf, row_offset, ref_mv_stack, ref_mv_weight,
607
4.14M
                    refmv_count, &row_match_count, &dummy_newmv_count,
608
4.14M
                    gm_mv_candidates, max_row_offset, &processed_rows);
609
610
6.99M
    if (abs(col_offset) <= abs(max_col_offset) &&
611
6.09M
        abs(col_offset) > processed_cols)
612
4.21M
      scan_col_mbmi(cm, xd, mi_row, rf, col_offset, ref_mv_stack, ref_mv_weight,
613
4.21M
                    refmv_count, &col_match_count, &dummy_newmv_count,
614
4.21M
                    gm_mv_candidates, max_col_offset, &processed_cols);
615
6.99M
  }
616
617
3.49M
  const uint8_t ref_match_count = (row_match_count > 0) + (col_match_count > 0);
618
619
3.49M
  switch (nearest_match) {
620
420k
    case 0:
621
420k
      if (ref_match_count >= 1) mode_context[ref_frame] |= 1;
622
420k
      if (ref_match_count == 1)
623
75.9k
        mode_context[ref_frame] |= (1 << REFMV_OFFSET);
624
344k
      else if (ref_match_count >= 2)
625
16.9k
        mode_context[ref_frame] |= (2 << REFMV_OFFSET);
626
420k
      break;
627
1.10M
    case 1:
628
1.10M
      mode_context[ref_frame] |= (newmv_count > 0) ? 2 : 3;
629
1.10M
      if (ref_match_count == 1)
630
659k
        mode_context[ref_frame] |= (3 << REFMV_OFFSET);
631
444k
      else if (ref_match_count >= 2)
632
444k
        mode_context[ref_frame] |= (4 << REFMV_OFFSET);
633
1.10M
      break;
634
1.97M
    case 2:
635
1.97M
    default:
636
1.97M
      if (newmv_count >= 1)
637
1.28M
        mode_context[ref_frame] |= 4;
638
687k
      else
639
687k
        mode_context[ref_frame] |= 5;
640
641
1.97M
      mode_context[ref_frame] |= (5 << REFMV_OFFSET);
642
1.97M
      break;
643
3.49M
  }
644
645
  // Rank the likelihood and assign nearest and near mvs.
646
3.49M
  int len = nearest_refmv_count;
647
7.29M
  while (len > 0) {
648
3.80M
    int nr_len = 0;
649
6.20M
    for (int idx = 1; idx < len; ++idx) {
650
2.40M
      if (ref_mv_weight[idx - 1] < ref_mv_weight[idx]) {
651
881k
        const CANDIDATE_MV tmp_mv = ref_mv_stack[idx - 1];
652
881k
        const uint16_t tmp_ref_mv_weight = ref_mv_weight[idx - 1];
653
881k
        ref_mv_stack[idx - 1] = ref_mv_stack[idx];
654
881k
        ref_mv_stack[idx] = tmp_mv;
655
881k
        ref_mv_weight[idx - 1] = ref_mv_weight[idx];
656
881k
        ref_mv_weight[idx] = tmp_ref_mv_weight;
657
881k
        nr_len = idx;
658
881k
      }
659
2.40M
    }
660
3.80M
    len = nr_len;
661
3.80M
  }
662
663
3.49M
  len = *refmv_count;
664
5.85M
  while (len > nearest_refmv_count) {
665
2.36M
    int nr_len = nearest_refmv_count;
666
4.30M
    for (int idx = nearest_refmv_count + 1; idx < len; ++idx) {
667
1.94M
      if (ref_mv_weight[idx - 1] < ref_mv_weight[idx]) {
668
561k
        const CANDIDATE_MV tmp_mv = ref_mv_stack[idx - 1];
669
561k
        const uint16_t tmp_ref_mv_weight = ref_mv_weight[idx - 1];
670
561k
        ref_mv_stack[idx - 1] = ref_mv_stack[idx];
671
561k
        ref_mv_stack[idx] = tmp_mv;
672
561k
        ref_mv_weight[idx - 1] = ref_mv_weight[idx];
673
561k
        ref_mv_weight[idx] = tmp_ref_mv_weight;
674
561k
        nr_len = idx;
675
561k
      }
676
1.94M
    }
677
2.36M
    len = nr_len;
678
2.36M
  }
679
680
3.49M
  int mi_width = AOMMIN(mi_size_wide[BLOCK_64X64], xd->width);
681
3.49M
  mi_width = AOMMIN(mi_width, cm->mi_params.mi_cols - mi_col);
682
3.49M
  int mi_height = AOMMIN(mi_size_high[BLOCK_64X64], xd->height);
683
3.49M
  mi_height = AOMMIN(mi_height, cm->mi_params.mi_rows - mi_row);
684
3.49M
  const int mi_size = AOMMIN(mi_width, mi_height);
685
3.49M
  if (rf[1] > NONE_FRAME) {
686
    // TODO(jingning, yunqing): Refactor and consolidate the compound and
687
    // single reference frame modes. Reduce unnecessary redundancy.
688
465k
    if (*refmv_count < MAX_MV_REF_CANDIDATES) {
689
353k
      int_mv ref_id[2][2], ref_diff[2][2];
690
353k
      int ref_id_count[2] = { 0 }, ref_diff_count[2] = { 0 };
691
692
662k
      for (int idx = 0; abs(max_row_offset) >= 1 && idx < mi_size;) {
693
308k
        const MB_MODE_INFO *const candidate = xd->mi[-xd->mi_stride + idx];
694
308k
        process_compound_ref_mv_candidate(
695
308k
            candidate, cm, rf, ref_id, ref_id_count, ref_diff, ref_diff_count);
696
308k
        idx += mi_size_wide[candidate->bsize];
697
308k
      }
698
699
692k
      for (int idx = 0; abs(max_col_offset) >= 1 && idx < mi_size;) {
700
338k
        const MB_MODE_INFO *const candidate = xd->mi[idx * xd->mi_stride - 1];
701
338k
        process_compound_ref_mv_candidate(
702
338k
            candidate, cm, rf, ref_id, ref_id_count, ref_diff, ref_diff_count);
703
338k
        idx += mi_size_high[candidate->bsize];
704
338k
      }
705
706
      // Build up the compound mv predictor
707
353k
      int_mv comp_list[MAX_MV_REF_CANDIDATES][2];
708
709
1.06M
      for (int idx = 0; idx < 2; ++idx) {
710
707k
        int comp_idx = 0;
711
707k
        for (int list_idx = 0;
712
1.45M
             list_idx < ref_id_count[idx] && comp_idx < MAX_MV_REF_CANDIDATES;
713
749k
             ++list_idx, ++comp_idx)
714
749k
          comp_list[comp_idx][idx] = ref_id[idx][list_idx];
715
707k
        for (int list_idx = 0;
716
1.22M
             list_idx < ref_diff_count[idx] && comp_idx < MAX_MV_REF_CANDIDATES;
717
707k
             ++list_idx, ++comp_idx)
718
517k
          comp_list[comp_idx][idx] = ref_diff[idx][list_idx];
719
855k
        for (; comp_idx < MAX_MV_REF_CANDIDATES; ++comp_idx)
720
147k
          comp_list[comp_idx][idx] = gm_mv_candidates[idx];
721
707k
      }
722
723
353k
      if (*refmv_count) {
724
224k
        assert(*refmv_count == 1);
725
224k
        if (comp_list[0][0].as_int == ref_mv_stack[0].this_mv.as_int &&
726
195k
            comp_list[0][1].as_int == ref_mv_stack[0].comp_mv.as_int) {
727
184k
          ref_mv_stack[*refmv_count].this_mv = comp_list[1][0];
728
184k
          ref_mv_stack[*refmv_count].comp_mv = comp_list[1][1];
729
184k
        } else {
730
40.0k
          ref_mv_stack[*refmv_count].this_mv = comp_list[0][0];
731
40.0k
          ref_mv_stack[*refmv_count].comp_mv = comp_list[0][1];
732
40.0k
        }
733
224k
        ref_mv_weight[*refmv_count] = 2;
734
224k
        ++*refmv_count;
735
224k
      } else {
736
388k
        for (int idx = 0; idx < MAX_MV_REF_CANDIDATES; ++idx) {
737
258k
          ref_mv_stack[*refmv_count].this_mv = comp_list[idx][0];
738
258k
          ref_mv_stack[*refmv_count].comp_mv = comp_list[idx][1];
739
258k
          ref_mv_weight[*refmv_count] = 2;
740
258k
          ++*refmv_count;
741
258k
        }
742
129k
      }
743
353k
    }
744
745
465k
    assert(*refmv_count >= 2);
746
747
1.49M
    for (int idx = 0; idx < *refmv_count; ++idx) {
748
1.02M
      clamp_mv_ref(&ref_mv_stack[idx].this_mv.as_mv, xd->width << MI_SIZE_LOG2,
749
1.02M
                   xd->height << MI_SIZE_LOG2, xd);
750
1.02M
      clamp_mv_ref(&ref_mv_stack[idx].comp_mv.as_mv, xd->width << MI_SIZE_LOG2,
751
1.02M
                   xd->height << MI_SIZE_LOG2, xd);
752
1.02M
    }
753
3.03M
  } else {
754
    // Handle single reference frame extension
755
4.00M
    for (int idx = 0; abs(max_row_offset) >= 1 && idx < mi_size &&
756
3.00M
                      *refmv_count < MAX_MV_REF_CANDIDATES;) {
757
978k
      const MB_MODE_INFO *const candidate = xd->mi[-xd->mi_stride + idx];
758
978k
      process_single_ref_mv_candidate(candidate, cm, ref_frame, refmv_count,
759
978k
                                      ref_mv_stack, ref_mv_weight);
760
978k
      idx += mi_size_wide[candidate->bsize];
761
978k
    }
762
763
3.90M
    for (int idx = 0; abs(max_col_offset) >= 1 && idx < mi_size &&
764
3.03M
                      *refmv_count < MAX_MV_REF_CANDIDATES;) {
765
876k
      const MB_MODE_INFO *const candidate = xd->mi[idx * xd->mi_stride - 1];
766
876k
      process_single_ref_mv_candidate(candidate, cm, ref_frame, refmv_count,
767
876k
                                      ref_mv_stack, ref_mv_weight);
768
876k
      idx += mi_size_high[candidate->bsize];
769
876k
    }
770
771
11.5M
    for (int idx = 0; idx < *refmv_count; ++idx) {
772
8.49M
      clamp_mv_ref(&ref_mv_stack[idx].this_mv.as_mv, xd->width << MI_SIZE_LOG2,
773
8.49M
                   xd->height << MI_SIZE_LOG2, xd);
774
8.49M
    }
775
776
3.03M
    if (mv_ref_list != NULL) {
777
3.86M
      for (int idx = *refmv_count; idx < MAX_MV_REF_CANDIDATES; ++idx)
778
833k
        mv_ref_list[idx].as_int = gm_mv_candidates[0].as_int;
779
780
8.26M
      for (int idx = 0; idx < AOMMIN(MAX_MV_REF_CANDIDATES, *refmv_count);
781
5.22M
           ++idx) {
782
5.22M
        mv_ref_list[idx].as_int = ref_mv_stack[idx].this_mv.as_int;
783
5.22M
      }
784
3.03M
    }
785
3.03M
  }
786
3.49M
}
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
3.49M
                      int_mv *global_mvs, int16_t *mode_context) {
795
3.49M
  const int mi_row = xd->mi_row;
796
3.49M
  const int mi_col = xd->mi_col;
797
3.49M
  int_mv gm_mv[2];
798
799
3.49M
  if (ref_frame == INTRA_FRAME) {
800
43.9k
    gm_mv[0].as_int = gm_mv[1].as_int = 0;
801
43.9k
    if (global_mvs != NULL) {
802
0
      global_mvs[ref_frame].as_int = INVALID_MV;
803
0
    }
804
3.45M
  } else {
805
3.45M
    const BLOCK_SIZE bsize = mi->bsize;
806
3.45M
    const int allow_high_precision_mv = cm->features.allow_high_precision_mv;
807
3.45M
    const int force_integer_mv = cm->features.cur_frame_force_integer_mv;
808
3.45M
    if (ref_frame < REF_FRAMES) {
809
2.98M
      gm_mv[0] = gm_get_motion_vector(&cm->global_motion[ref_frame],
810
2.98M
                                      allow_high_precision_mv, bsize, mi_col,
811
2.98M
                                      mi_row, force_integer_mv);
812
2.98M
      gm_mv[1].as_int = 0;
813
2.98M
      if (global_mvs != NULL) global_mvs[ref_frame] = gm_mv[0];
814
2.98M
    } else {
815
464k
      MV_REFERENCE_FRAME rf[2];
816
464k
      av1_set_ref_frame(rf, ref_frame);
817
464k
      gm_mv[0] = gm_get_motion_vector(&cm->global_motion[rf[0]],
818
464k
                                      allow_high_precision_mv, bsize, mi_col,
819
464k
                                      mi_row, force_integer_mv);
820
464k
      gm_mv[1] = gm_get_motion_vector(&cm->global_motion[rf[1]],
821
464k
                                      allow_high_precision_mv, bsize, mi_col,
822
464k
                                      mi_row, force_integer_mv);
823
464k
    }
824
3.45M
  }
825
826
3.49M
  setup_ref_mv_list(cm, xd, ref_frame, &ref_mv_count[ref_frame],
827
3.49M
                    ref_mv_stack[ref_frame], ref_mv_weight[ref_frame],
828
3.49M
                    mv_ref_list ? mv_ref_list[ref_frame] : NULL, gm_mv, mi_row,
829
3.49M
                    mi_col, mode_context);
830
3.49M
}
831
832
void av1_find_best_ref_mvs(int allow_hp, int_mv *mvlist, int_mv *nearest_mv,
833
2.57M
                           int_mv *near_mv, int is_integer) {
834
2.57M
  int i;
835
  // Make sure all the candidates are properly clamped etc
836
7.72M
  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
837
5.15M
    lower_mv_precision(&mvlist[i].as_mv, allow_hp, is_integer);
838
5.15M
  }
839
2.57M
  *nearest_mv = mvlist[0];
840
2.57M
  *near_mv = mvlist[1];
841
2.57M
}
842
843
159k
void av1_setup_frame_buf_refs(AV1_COMMON *cm) {
844
159k
  cm->cur_frame->order_hint = cm->current_frame.order_hint;
845
159k
  cm->cur_frame->display_order_hint = cm->current_frame.display_order_hint;
846
159k
  cm->cur_frame->pyramid_level = cm->current_frame.pyramid_level;
847
159k
  cm->cur_frame->base_qindex = cm->quant_params.base_qindex;
848
159k
  cm->cur_frame->filter_level[0] = -1;
849
159k
  cm->cur_frame->filter_level[1] = -1;
850
159k
  MV_REFERENCE_FRAME ref_frame;
851
1.27M
  for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
852
1.11M
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
853
1.11M
    if (buf != NULL) {
854
512k
      cm->cur_frame->ref_order_hints[ref_frame - LAST_FRAME] = buf->order_hint;
855
512k
      cm->cur_frame->ref_display_order_hint[ref_frame - LAST_FRAME] =
856
512k
          buf->display_order_hint;
857
512k
    }
858
1.11M
  }
859
159k
}
860
861
159k
void av1_setup_frame_sign_bias(AV1_COMMON *cm) {
862
159k
  MV_REFERENCE_FRAME ref_frame;
863
1.27M
  for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
864
1.11M
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
865
1.11M
    if (cm->seq_params->order_hint_info.enable_order_hint && buf != NULL) {
866
512k
      const int ref_order_hint = buf->order_hint;
867
512k
      cm->ref_frame_sign_bias[ref_frame] =
868
512k
          (get_relative_dist(&cm->seq_params->order_hint_info, ref_order_hint,
869
512k
                             (int)cm->current_frame.order_hint) <= 0)
870
512k
              ? 0
871
512k
              : 1;
872
602k
    } else {
873
602k
      cm->ref_frame_sign_bias[ref_frame] = 0;
874
602k
    }
875
1.11M
  }
876
159k
}
877
878
20.4M
#define MAX_OFFSET_WIDTH 64
879
21.3M
#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
12.6M
                              int blk_col, MV mv, int sign_bias) {
883
12.6M
  const int base_blk_row = (blk_row >> 3) << 3;
884
12.6M
  const int base_blk_col = (blk_col >> 3) << 3;
885
886
12.6M
  const int row_offset = (mv.row >= 0) ? (mv.row >> (4 + MI_SIZE_LOG2))
887
12.6M
                                       : -((-mv.row) >> (4 + MI_SIZE_LOG2));
888
889
12.6M
  const int col_offset = (mv.col >= 0) ? (mv.col >> (4 + MI_SIZE_LOG2))
890
12.6M
                                       : -((-mv.col) >> (4 + MI_SIZE_LOG2));
891
892
12.6M
  const int row =
893
12.6M
      (sign_bias == 1) ? blk_row - row_offset : blk_row + row_offset;
894
12.6M
  const int col =
895
12.6M
      (sign_bias == 1) ? blk_col - col_offset : blk_col + col_offset;
896
897
12.6M
  if (row < 0 || row >= (cm->mi_params.mi_rows >> 1) || col < 0 ||
898
11.0M
      col >= (cm->mi_params.mi_cols >> 1))
899
1.66M
    return 0;
900
901
10.9M
  if (row < base_blk_row - (MAX_OFFSET_HEIGHT >> 3) ||
902
10.4M
      row >= base_blk_row + 8 + (MAX_OFFSET_HEIGHT >> 3) ||
903
10.2M
      col < base_blk_col - (MAX_OFFSET_WIDTH >> 3) ||
904
10.1M
      col >= base_blk_col + 8 + (MAX_OFFSET_WIDTH >> 3))
905
757k
    return 0;
906
907
10.1M
  *mi_r = row;
908
10.1M
  *mi_c = col;
909
910
10.1M
  return 1;
911
10.9M
}
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
35.3k
                                   MV_REFERENCE_FRAME start_frame, int dir) {
921
35.3k
  TPL_MV_REF *tpl_mvs_base = cm->tpl_mvs;
922
35.3k
  int ref_offset[REF_FRAMES] = { 0 };
923
924
35.3k
  const RefCntBuffer *const start_frame_buf =
925
35.3k
      get_ref_frame_buf(cm, start_frame);
926
35.3k
  if (start_frame_buf == NULL) return 0;
927
928
35.3k
  if (start_frame_buf->frame_type == KEY_FRAME ||
929
16.8k
      start_frame_buf->frame_type == INTRA_ONLY_FRAME)
930
19.5k
    return 0;
931
932
15.7k
  if (start_frame_buf->mi_rows != cm->mi_params.mi_rows ||
933
15.7k
      start_frame_buf->mi_cols != cm->mi_params.mi_cols)
934
32
    return 0;
935
936
15.7k
  const int start_frame_order_hint = start_frame_buf->order_hint;
937
15.7k
  const unsigned int *const ref_order_hints =
938
15.7k
      &start_frame_buf->ref_order_hints[0];
939
15.7k
  const int cur_order_hint = cm->cur_frame->order_hint;
940
15.7k
  int start_to_current_frame_offset = get_relative_dist(
941
15.7k
      &cm->seq_params->order_hint_info, start_frame_order_hint, cur_order_hint);
942
943
126k
  for (MV_REFERENCE_FRAME rf = LAST_FRAME; rf <= INTER_REFS_PER_FRAME; ++rf) {
944
110k
    ref_offset[rf] = get_relative_dist(&cm->seq_params->order_hint_info,
945
110k
                                       start_frame_order_hint,
946
110k
                                       ref_order_hints[rf - LAST_FRAME]);
947
110k
  }
948
949
15.7k
  if (dir == 2) start_to_current_frame_offset = -start_to_current_frame_offset;
950
951
15.7k
  MV_REF *mv_ref_base = start_frame_buf->mvs;
952
15.7k
  const int mvs_rows = (cm->mi_params.mi_rows + 1) >> 1;
953
15.7k
  const int mvs_cols = (cm->mi_params.mi_cols + 1) >> 1;
954
955
467k
  for (int blk_row = 0; blk_row < mvs_rows; ++blk_row) {
956
22.4M
    for (int blk_col = 0; blk_col < mvs_cols; ++blk_col) {
957
21.9M
      MV_REF *mv_ref = &mv_ref_base[blk_row * mvs_cols + blk_col];
958
21.9M
      MV fwd_mv = mv_ref->mv.as_mv;
959
960
21.9M
      if (mv_ref->ref_frame > INTRA_FRAME) {
961
15.2M
        int_mv this_mv;
962
15.2M
        int mi_r, mi_c;
963
15.2M
        const int ref_frame_offset = ref_offset[mv_ref->ref_frame];
964
965
15.2M
        int pos_valid =
966
15.2M
            abs(ref_frame_offset) <= MAX_FRAME_DISTANCE &&
967
12.6M
            ref_frame_offset > 0 &&
968
12.6M
            abs(start_to_current_frame_offset) <= MAX_FRAME_DISTANCE;
969
970
15.2M
        if (pos_valid) {
971
12.6M
          av1_get_mv_projection(&this_mv.as_mv, fwd_mv,
972
12.6M
                                start_to_current_frame_offset,
973
12.6M
                                ref_frame_offset);
974
12.6M
          pos_valid = get_block_position(cm, &mi_r, &mi_c, blk_row, blk_col,
975
12.6M
                                         this_mv.as_mv, dir >> 1);
976
12.6M
        }
977
978
15.2M
        if (pos_valid) {
979
10.1M
          const int mi_offset = mi_r * (cm->mi_params.mi_stride >> 1) + mi_c;
980
981
10.1M
          tpl_mvs_base[mi_offset].mfmv0.as_mv.row = fwd_mv.row;
982
10.1M
          tpl_mvs_base[mi_offset].mfmv0.as_mv.col = fwd_mv.col;
983
10.1M
          tpl_mvs_base[mi_offset].ref_frame_offset = ref_frame_offset;
984
10.1M
        }
985
15.2M
      }
986
21.9M
    }
987
451k
  }
988
989
15.7k
  return 1;
990
15.7k
}
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
136k
void av1_calculate_ref_frame_side(AV1_COMMON *cm) {
995
136k
  const OrderHintInfo *const order_hint_info = &cm->seq_params->order_hint_info;
996
997
136k
  memset(cm->ref_frame_side, 0, sizeof(cm->ref_frame_side));
998
136k
  if (!order_hint_info->enable_order_hint) return;
999
1000
97.3k
  const int cur_order_hint = cm->cur_frame->order_hint;
1001
1002
779k
  for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
1003
681k
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
1004
681k
    int order_hint = 0;
1005
1006
681k
    if (buf != NULL) order_hint = buf->order_hint;
1007
1008
681k
    if (get_relative_dist(order_hint_info, order_hint, cur_order_hint) > 0)
1009
177k
      cm->ref_frame_side[ref_frame] = 1;
1010
504k
    else if (order_hint == cur_order_hint)
1011
136k
      cm->ref_frame_side[ref_frame] = -1;
1012
681k
  }
1013
97.3k
}
1014
1015
14.2k
void av1_setup_motion_field(AV1_COMMON *cm) {
1016
14.2k
  const OrderHintInfo *const order_hint_info = &cm->seq_params->order_hint_info;
1017
1018
14.2k
  if (!order_hint_info->enable_order_hint) return;
1019
1020
14.2k
  TPL_MV_REF *tpl_mvs_base = cm->tpl_mvs;
1021
14.2k
  int size = ((cm->mi_params.mi_rows + MAX_MIB_SIZE) >> 1) *
1022
14.2k
             (cm->mi_params.mi_stride >> 1);
1023
48.9M
  for (int idx = 0; idx < size; ++idx) {
1024
48.9M
    tpl_mvs_base[idx].mfmv0.as_int = INVALID_MV;
1025
48.9M
    tpl_mvs_base[idx].ref_frame_offset = 0;
1026
48.9M
  }
1027
1028
14.2k
  const int cur_order_hint = cm->cur_frame->order_hint;
1029
14.2k
  const RefCntBuffer *ref_buf[INTER_REFS_PER_FRAME];
1030
14.2k
  int ref_order_hint[INTER_REFS_PER_FRAME];
1031
1032
113k
  for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
1033
99.4k
    const int ref_idx = ref_frame - LAST_FRAME;
1034
99.4k
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
1035
99.4k
    int order_hint = 0;
1036
1037
99.4k
    if (buf != NULL) order_hint = buf->order_hint;
1038
1039
99.4k
    ref_buf[ref_idx] = buf;
1040
99.4k
    ref_order_hint[ref_idx] = order_hint;
1041
99.4k
  }
1042
1043
14.2k
  int ref_stamp = MFMV_STACK_SIZE - 1;
1044
1045
14.2k
  if (ref_buf[LAST_FRAME - LAST_FRAME] != NULL) {
1046
14.2k
    const int alt_of_lst_order_hint =
1047
14.2k
        ref_buf[LAST_FRAME - LAST_FRAME]
1048
14.2k
            ->ref_order_hints[ALTREF_FRAME - LAST_FRAME];
1049
1050
14.2k
    const int is_lst_overlay =
1051
14.2k
        (alt_of_lst_order_hint == ref_order_hint[GOLDEN_FRAME - LAST_FRAME]);
1052
14.2k
    if (!is_lst_overlay) motion_field_projection(cm, LAST_FRAME, 2);
1053
14.2k
    --ref_stamp;
1054
14.2k
  }
1055
1056
14.2k
  if (get_relative_dist(order_hint_info,
1057
14.2k
                        ref_order_hint[BWDREF_FRAME - LAST_FRAME],
1058
14.2k
                        cur_order_hint) > 0) {
1059
2.94k
    if (motion_field_projection(cm, BWDREF_FRAME, 0)) --ref_stamp;
1060
2.94k
  }
1061
1062
14.2k
  if (get_relative_dist(order_hint_info,
1063
14.2k
                        ref_order_hint[ALTREF2_FRAME - LAST_FRAME],
1064
14.2k
                        cur_order_hint) > 0) {
1065
4.29k
    if (motion_field_projection(cm, ALTREF2_FRAME, 0)) --ref_stamp;
1066
4.29k
  }
1067
1068
14.2k
  if (get_relative_dist(order_hint_info,
1069
14.2k
                        ref_order_hint[ALTREF_FRAME - LAST_FRAME],
1070
14.2k
                        cur_order_hint) > 0 &&
1071
4.28k
      ref_stamp >= 0)
1072
3.79k
    if (motion_field_projection(cm, ALTREF_FRAME, 0)) --ref_stamp;
1073
1074
14.2k
  if (ref_stamp >= 0) motion_field_projection(cm, LAST2_FRAME, 2);
1075
14.2k
}
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
3.84M
                                  int col_offset, int sign_c) {
1080
3.84M
  const int bw = block_size_wide[mbmi->bsize];
1081
3.84M
  const int bh = block_size_high[mbmi->bsize];
1082
3.84M
  const int x = col_offset * MI_SIZE + sign_c * bw / 2 - 1;
1083
3.84M
  const int y = row_offset * MI_SIZE + sign_r * bh / 2 - 1;
1084
1085
3.84M
  pts[0] = GET_MV_SUBPEL(x);
1086
3.84M
  pts[1] = GET_MV_SUBPEL(y);
1087
3.84M
  pts_inref[0] = pts[0] + mbmi->mv[0].as_mv.col;
1088
3.84M
  pts_inref[1] = pts[1] + mbmi->mv[0].as_mv.row;
1089
3.84M
}
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
239k
                          BLOCK_SIZE bsize) {
1094
239k
  const int bw = block_size_wide[bsize];
1095
239k
  const int bh = block_size_high[bsize];
1096
239k
  const int thresh = clamp(AOMMAX(bw, bh), 16, 112);
1097
239k
  uint8_t ret = 0;
1098
239k
  assert(len <= LEAST_SQUARES_SAMPLES_MAX);
1099
1100
  // Only keep the samples with MV differences within threshold.
1101
979k
  for (int i = 0; i < len; ++i) {
1102
740k
    const int diff = abs(pts_inref[2 * i] - pts[2 * i] - mv->col) +
1103
740k
                     abs(pts_inref[2 * i + 1] - pts[2 * i + 1] - mv->row);
1104
740k
    if (diff > thresh) continue;
1105
440k
    if (ret != i) {
1106
76.2k
      memcpy(pts + 2 * ret, pts + 2 * i, 2 * sizeof(pts[0]));
1107
76.2k
      memcpy(pts_inref + 2 * ret, pts_inref + 2 * i, 2 * sizeof(pts_inref[0]));
1108
76.2k
    }
1109
440k
    ++ret;
1110
440k
  }
1111
  // Keep at least 1 sample.
1112
239k
  return AOMMAX(ret, 1);
1113
239k
}
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
1.74M
                        int *pts_inref) {
1120
1.74M
  const MB_MODE_INFO *const mbmi0 = xd->mi[0];
1121
1.74M
  const int ref_frame = mbmi0->ref_frame[0];
1122
1.74M
  const int up_available = xd->up_available;
1123
1.74M
  const int left_available = xd->left_available;
1124
1.74M
  uint8_t np = 0;
1125
1.74M
  int do_tl = 1;
1126
1.74M
  int do_tr = 1;
1127
1.74M
  const int mi_stride = xd->mi_stride;
1128
1.74M
  const int mi_row = xd->mi_row;
1129
1.74M
  const int mi_col = xd->mi_col;
1130
1131
  // scan the nearest above rows
1132
1.74M
  if (up_available) {
1133
1.67M
    const int mi_row_offset = -1;
1134
1.67M
    const MB_MODE_INFO *mbmi = xd->mi[mi_row_offset * mi_stride];
1135
1.67M
    uint8_t superblock_width = mi_size_wide[mbmi->bsize];
1136
1137
1.67M
    if (xd->width <= superblock_width) {
1138
      // Handle "current block width <= above block width" case.
1139
1.41M
      const int col_offset = -mi_col % superblock_width;
1140
1141
1.41M
      if (col_offset < 0) do_tl = 0;
1142
1.41M
      if (col_offset + superblock_width > xd->width) do_tr = 0;
1143
1144
1.41M
      if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) {
1145
916k
        record_samples(mbmi, pts, pts_inref, 0, -1, col_offset, 1);
1146
916k
        pts += 2;
1147
916k
        pts_inref += 2;
1148
916k
        if (++np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX;
1149
916k
      }
1150
1.41M
    } else {
1151
      // Handle "current block width > above block width" case.
1152
924k
      for (int i = 0; i < AOMMIN(xd->width, cm->mi_params.mi_cols - mi_col);
1153
664k
           i += superblock_width) {
1154
664k
        mbmi = xd->mi[i + mi_row_offset * mi_stride];
1155
664k
        superblock_width = mi_size_wide[mbmi->bsize];
1156
1157
664k
        if (mbmi->ref_frame[0] == ref_frame &&
1158
470k
            mbmi->ref_frame[1] == NONE_FRAME) {
1159
430k
          record_samples(mbmi, pts, pts_inref, 0, -1, i, 1);
1160
430k
          pts += 2;
1161
430k
          pts_inref += 2;
1162
430k
          if (++np >= LEAST_SQUARES_SAMPLES_MAX)
1163
1.18k
            return LEAST_SQUARES_SAMPLES_MAX;
1164
430k
        }
1165
664k
      }
1166
260k
    }
1167
1.67M
  }
1168
1.74M
  assert(np <= LEAST_SQUARES_SAMPLES_MAX);
1169
1170
  // scan the nearest left columns
1171
1.74M
  if (left_available) {
1172
1.70M
    const int mi_col_offset = -1;
1173
1.70M
    const MB_MODE_INFO *mbmi = xd->mi[mi_col_offset];
1174
1.70M
    uint8_t superblock_height = mi_size_high[mbmi->bsize];
1175
1176
1.70M
    if (xd->height <= superblock_height) {
1177
      // Handle "current block height <= above block height" case.
1178
1.40M
      const int row_offset = -mi_row % superblock_height;
1179
1180
1.40M
      if (row_offset < 0) do_tl = 0;
1181
1182
1.40M
      if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) {
1183
889k
        record_samples(mbmi, pts, pts_inref, row_offset, 1, 0, -1);
1184
889k
        pts += 2;
1185
889k
        pts_inref += 2;
1186
889k
        np++;
1187
889k
        if (np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX;
1188
889k
      }
1189
1.40M
    } else {
1190
      // Handle "current block height > above block height" case.
1191
1.05M
      for (int i = 0; i < AOMMIN(xd->height, cm->mi_params.mi_rows - mi_row);
1192
757k
           i += superblock_height) {
1193
757k
        mbmi = xd->mi[mi_col_offset + i * mi_stride];
1194
757k
        superblock_height = mi_size_high[mbmi->bsize];
1195
1196
757k
        if (mbmi->ref_frame[0] == ref_frame &&
1197
546k
            mbmi->ref_frame[1] == NONE_FRAME) {
1198
506k
          record_samples(mbmi, pts, pts_inref, i, 1, 0, -1);
1199
506k
          pts += 2;
1200
506k
          pts_inref += 2;
1201
506k
          if (++np >= LEAST_SQUARES_SAMPLES_MAX)
1202
4.45k
            return LEAST_SQUARES_SAMPLES_MAX;
1203
506k
        }
1204
757k
      }
1205
299k
    }
1206
1.70M
  }
1207
1.74M
  assert(np <= LEAST_SQUARES_SAMPLES_MAX);
1208
1209
  // Top-left block
1210
1.74M
  if (do_tl && left_available && up_available) {
1211
1.12M
    const int mi_row_offset = -1;
1212
1.12M
    const int mi_col_offset = -1;
1213
1.12M
    MB_MODE_INFO *mbmi = xd->mi[mi_col_offset + mi_row_offset * mi_stride];
1214
1215
1.12M
    if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) {
1216
696k
      record_samples(mbmi, pts, pts_inref, 0, -1, 0, -1);
1217
696k
      pts += 2;
1218
696k
      pts_inref += 2;
1219
696k
      if (++np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX;
1220
696k
    }
1221
1.12M
  }
1222
1.74M
  assert(np <= LEAST_SQUARES_SAMPLES_MAX);
1223
1224
  // Top-right block
1225
1.73M
  if (do_tr &&
1226
1.48M
      has_top_right(cm, xd, mi_row, mi_col, AOMMAX(xd->width, xd->height))) {
1227
754k
    const POSITION trb_pos = { -1, xd->width };
1228
754k
    const TileInfo *const tile = &xd->tile;
1229
754k
    if (is_inside(tile, mi_col, mi_row, &trb_pos)) {
1230
697k
      const int mi_row_offset = -1;
1231
697k
      const int mi_col_offset = xd->width;
1232
697k
      const MB_MODE_INFO *mbmi =
1233
697k
          xd->mi[mi_col_offset + mi_row_offset * mi_stride];
1234
1235
697k
      if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE_FRAME) {
1236
410k
        record_samples(mbmi, pts, pts_inref, 0, -1, xd->width, 1);
1237
410k
        if (++np >= LEAST_SQUARES_SAMPLES_MAX) return LEAST_SQUARES_SAMPLES_MAX;
1238
410k
      }
1239
697k
    }
1240
754k
  }
1241
1.73M
  assert(np <= LEAST_SQUARES_SAMPLES_MAX);
1242
1243
1.73M
  return np;
1244
1.73M
}
1245
1246
142k
void av1_setup_skip_mode_allowed(AV1_COMMON *cm) {
1247
142k
  const OrderHintInfo *const order_hint_info = &cm->seq_params->order_hint_info;
1248
142k
  SkipModeInfo *const skip_mode_info = &cm->current_frame.skip_mode_info;
1249
1250
142k
  skip_mode_info->skip_mode_allowed = 0;
1251
142k
  skip_mode_info->ref_frame_idx_0 = INVALID_IDX;
1252
142k
  skip_mode_info->ref_frame_idx_1 = INVALID_IDX;
1253
1254
142k
  if (!order_hint_info->enable_order_hint || frame_is_intra_only(cm) ||
1255
42.9k
      cm->current_frame.reference_mode == SINGLE_REFERENCE)
1256
115k
    return;
1257
1258
26.8k
  const int cur_order_hint = cm->current_frame.order_hint;
1259
26.8k
  int ref_order_hints[2] = { -1, INT_MAX };
1260
26.8k
  int ref_idx[2] = { INVALID_IDX, INVALID_IDX };
1261
1262
  // Identify the nearest forward and backward references.
1263
215k
  for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
1264
188k
    const RefCntBuffer *const buf = get_ref_frame_buf(cm, LAST_FRAME + i);
1265
188k
    if (buf == NULL) continue;
1266
1267
188k
    const int ref_order_hint = buf->order_hint;
1268
188k
    if (get_relative_dist(order_hint_info, ref_order_hint, cur_order_hint) <
1269
188k
        0) {
1270
      // Forward reference
1271
129k
      if (ref_order_hints[0] == -1 ||
1272
103k
          get_relative_dist(order_hint_info, ref_order_hint,
1273
103k
                            ref_order_hints[0]) > 0) {
1274
43.2k
        ref_order_hints[0] = ref_order_hint;
1275
43.2k
        ref_idx[0] = i;
1276
43.2k
      }
1277
129k
    } else if (get_relative_dist(order_hint_info, ref_order_hint,
1278
58.5k
                                 cur_order_hint) > 0) {
1279
      // Backward reference
1280
19.8k
      if (ref_order_hints[1] == INT_MAX ||
1281
11.8k
          get_relative_dist(order_hint_info, ref_order_hint,
1282
11.8k
                            ref_order_hints[1]) < 0) {
1283
8.90k
        ref_order_hints[1] = ref_order_hint;
1284
8.90k
        ref_idx[1] = i;
1285
8.90k
      }
1286
19.8k
    }
1287
188k
  }
1288
1289
26.8k
  if (ref_idx[0] != INVALID_IDX && ref_idx[1] != INVALID_IDX) {
1290
    // == Bi-directional prediction ==
1291
7.51k
    skip_mode_info->skip_mode_allowed = 1;
1292
7.51k
    skip_mode_info->ref_frame_idx_0 = AOMMIN(ref_idx[0], ref_idx[1]);
1293
7.51k
    skip_mode_info->ref_frame_idx_1 = AOMMAX(ref_idx[0], ref_idx[1]);
1294
19.3k
  } else if (ref_idx[0] != INVALID_IDX && ref_idx[1] == INVALID_IDX) {
1295
    // == Forward prediction only ==
1296
    // Identify the second nearest forward reference.
1297
18.8k
    ref_order_hints[1] = -1;
1298
150k
    for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
1299
131k
      const RefCntBuffer *const buf = get_ref_frame_buf(cm, LAST_FRAME + i);
1300
131k
      if (buf == NULL) continue;
1301
1302
131k
      const int ref_order_hint = buf->order_hint;
1303
131k
      if ((ref_order_hints[0] != -1 &&
1304
131k
           get_relative_dist(order_hint_info, ref_order_hint,
1305
131k
                             ref_order_hints[0]) < 0) &&
1306
41.7k
          (ref_order_hints[1] == -1 ||
1307
24.3k
           get_relative_dist(order_hint_info, ref_order_hint,
1308
24.3k
                             ref_order_hints[1]) > 0)) {
1309
        // Second closest forward reference
1310
20.2k
        ref_order_hints[1] = ref_order_hint;
1311
20.2k
        ref_idx[1] = i;
1312
20.2k
      }
1313
131k
    }
1314
18.8k
    if (ref_order_hints[1] != -1) {
1315
17.3k
      skip_mode_info->skip_mode_allowed = 1;
1316
17.3k
      skip_mode_info->ref_frame_idx_0 = AOMMIN(ref_idx[0], ref_idx[1]);
1317
17.3k
      skip_mode_info->ref_frame_idx_1 = AOMMAX(ref_idx[0], ref_idx[1]);
1318
17.3k
    }
1319
18.8k
  }
1320
26.8k
}
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
403k
static int compare_ref_frame_info(const void *arg_a, const void *arg_b) {
1331
403k
  const REF_FRAME_INFO *info_a = (REF_FRAME_INFO *)arg_a;
1332
403k
  const REF_FRAME_INFO *info_b = (REF_FRAME_INFO *)arg_b;
1333
1334
403k
  const int sort_idx_diff = info_a->sort_idx - info_b->sort_idx;
1335
403k
  if (sort_idx_diff != 0) return sort_idx_diff;
1336
140k
  return info_a->map_idx - info_b->map_idx;
1337
403k
}
1338
1339
static inline void set_ref_frame_info(int *remapped_ref_idx, int frame_idx,
1340
182k
                                      REF_FRAME_INFO *ref_info) {
1341
182k
  assert(frame_idx >= 0 && frame_idx < INTER_REFS_PER_FRAME);
1342
1343
182k
  remapped_ref_idx[frame_idx] = ref_info->map_idx;
1344
182k
}
1345
1346
void av1_set_frame_refs(AV1_COMMON *const cm, int *remapped_ref_idx,
1347
27.0k
                        int lst_map_idx, int gld_map_idx) {
1348
27.0k
  int lst_frame_sort_idx = -1;
1349
27.0k
  int gld_frame_sort_idx = -1;
1350
1351
27.0k
  assert(cm->seq_params->order_hint_info.enable_order_hint);
1352
27.0k
  assert(cm->seq_params->order_hint_info.order_hint_bits_minus_1 >= 0);
1353
27.0k
  const int cur_order_hint = (int)cm->current_frame.order_hint;
1354
27.0k
  const int cur_frame_sort_idx =
1355
27.0k
      1 << cm->seq_params->order_hint_info.order_hint_bits_minus_1;
1356
1357
27.0k
  REF_FRAME_INFO ref_frame_info[REF_FRAMES];
1358
27.0k
  int ref_flag_list[INTER_REFS_PER_FRAME] = { 0, 0, 0, 0, 0, 0, 0 };
1359
1360
243k
  for (int i = 0; i < REF_FRAMES; ++i) {
1361
216k
    const int map_idx = i;
1362
1363
216k
    ref_frame_info[i].map_idx = map_idx;
1364
216k
    ref_frame_info[i].sort_idx = -1;
1365
1366
216k
    RefCntBuffer *const buf = cm->ref_frame_map[map_idx];
1367
216k
    ref_frame_info[i].buf = buf;
1368
1369
216k
    if (buf == NULL) continue;
1370
    // If this assertion fails, there is a reference leak.
1371
216k
    assert(buf->ref_count > 0);
1372
1373
199k
    const int offset = (int)buf->order_hint;
1374
199k
    ref_frame_info[i].sort_idx =
1375
199k
        (offset == -1) ? -1
1376
199k
                       : cur_frame_sort_idx +
1377
199k
                             get_relative_dist(&cm->seq_params->order_hint_info,
1378
199k
                                               offset, cur_order_hint);
1379
199k
    assert(ref_frame_info[i].sort_idx >= -1);
1380
1381
199k
    if (map_idx == lst_map_idx) lst_frame_sort_idx = ref_frame_info[i].sort_idx;
1382
199k
    if (map_idx == gld_map_idx) gld_frame_sort_idx = ref_frame_info[i].sort_idx;
1383
199k
  }
1384
1385
  // Confirm both LAST_FRAME and GOLDEN_FRAME are valid forward reference
1386
  // frames.
1387
27.0k
  if (lst_frame_sort_idx == -1 || lst_frame_sort_idx >= cur_frame_sort_idx) {
1388
794
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
1389
794
                       "Inter frame requests a look-ahead frame as LAST");
1390
794
  }
1391
27.0k
  if (gld_frame_sort_idx == -1 || gld_frame_sort_idx >= cur_frame_sort_idx) {
1392
172
    aom_internal_error(cm->error, AOM_CODEC_CORRUPT_FRAME,
1393
172
                       "Inter frame requests a look-ahead frame as GOLDEN");
1394
172
  }
1395
1396
  // Sort ref frames based on their frame_offset values.
1397
27.0k
  qsort(ref_frame_info, REF_FRAMES, sizeof(REF_FRAME_INFO),
1398
27.0k
        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
27.0k
  int fwd_start_idx = 0, fwd_end_idx = REF_FRAMES - 1;
1404
1405
181k
  for (int i = 0; i < REF_FRAMES; i++) {
1406
175k
    if (ref_frame_info[i].sort_idx == -1) {
1407
17.3k
      fwd_start_idx++;
1408
17.3k
      continue;
1409
17.3k
    }
1410
1411
158k
    if (ref_frame_info[i].sort_idx >= cur_frame_sort_idx) {
1412
21.4k
      fwd_end_idx = i - 1;
1413
21.4k
      break;
1414
21.4k
    }
1415
158k
  }
1416
1417
27.0k
  int bwd_start_idx = fwd_end_idx + 1;
1418
27.0k
  int bwd_end_idx = REF_FRAMES - 1;
1419
1420
  // === Backward Reference Frames ===
1421
1422
  // == ALTREF_FRAME ==
1423
27.0k
  if (bwd_start_idx <= bwd_end_idx) {
1424
21.4k
    set_ref_frame_info(remapped_ref_idx, ALTREF_FRAME - LAST_FRAME,
1425
21.4k
                       &ref_frame_info[bwd_end_idx]);
1426
21.4k
    ref_flag_list[ALTREF_FRAME - LAST_FRAME] = 1;
1427
21.4k
    bwd_end_idx--;
1428
21.4k
  }
1429
1430
  // == BWDREF_FRAME ==
1431
27.0k
  if (bwd_start_idx <= bwd_end_idx) {
1432
16.8k
    set_ref_frame_info(remapped_ref_idx, BWDREF_FRAME - LAST_FRAME,
1433
16.8k
                       &ref_frame_info[bwd_start_idx]);
1434
16.8k
    ref_flag_list[BWDREF_FRAME - LAST_FRAME] = 1;
1435
16.8k
    bwd_start_idx++;
1436
16.8k
  }
1437
1438
  // == ALTREF2_FRAME ==
1439
27.0k
  if (bwd_start_idx <= bwd_end_idx) {
1440
9.89k
    set_ref_frame_info(remapped_ref_idx, ALTREF2_FRAME - LAST_FRAME,
1441
9.89k
                       &ref_frame_info[bwd_start_idx]);
1442
9.89k
    ref_flag_list[ALTREF2_FRAME - LAST_FRAME] = 1;
1443
9.89k
  }
1444
1445
  // === Forward Reference Frames ===
1446
1447
164k
  for (int i = fwd_start_idx; i <= fwd_end_idx; ++i) {
1448
    // == LAST_FRAME ==
1449
137k
    if (ref_frame_info[i].map_idx == lst_map_idx) {
1450
26.1k
      set_ref_frame_info(remapped_ref_idx, LAST_FRAME - LAST_FRAME,
1451
26.1k
                         &ref_frame_info[i]);
1452
26.1k
      ref_flag_list[LAST_FRAME - LAST_FRAME] = 1;
1453
26.1k
    }
1454
1455
    // == GOLDEN_FRAME ==
1456
137k
    if (ref_frame_info[i].map_idx == gld_map_idx) {
1457
26.1k
      set_ref_frame_info(remapped_ref_idx, GOLDEN_FRAME - LAST_FRAME,
1458
26.1k
                         &ref_frame_info[i]);
1459
26.1k
      ref_flag_list[GOLDEN_FRAME - LAST_FRAME] = 1;
1460
26.1k
    }
1461
137k
  }
1462
1463
27.0k
  assert(ref_flag_list[LAST_FRAME - LAST_FRAME] == 1 &&
1464
27.0k
         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
26.1k
  static const MV_REFERENCE_FRAME ref_frame_list[INTER_REFS_PER_FRAME - 2] = {
1474
26.1k
    LAST2_FRAME, LAST3_FRAME, BWDREF_FRAME, ALTREF2_FRAME, ALTREF_FRAME
1475
26.1k
  };
1476
1477
26.1k
  int ref_idx;
1478
130k
  for (ref_idx = 0; ref_idx < (INTER_REFS_PER_FRAME - 2); ref_idx++) {
1479
112k
    const MV_REFERENCE_FRAME ref_frame = ref_frame_list[ref_idx];
1480
1481
112k
    if (ref_flag_list[ref_frame - LAST_FRAME] == 1) continue;
1482
1483
113k
    while (fwd_start_idx <= fwd_end_idx &&
1484
105k
           (ref_frame_info[fwd_end_idx].map_idx == lst_map_idx ||
1485
88.5k
            ref_frame_info[fwd_end_idx].map_idx == gld_map_idx)) {
1486
35.7k
      fwd_end_idx--;
1487
35.7k
    }
1488
77.7k
    if (fwd_start_idx > fwd_end_idx) break;
1489
1490
70.1k
    set_ref_frame_info(remapped_ref_idx, ref_frame - LAST_FRAME,
1491
70.1k
                       &ref_frame_info[fwd_end_idx]);
1492
70.1k
    ref_flag_list[ref_frame - LAST_FRAME] = 1;
1493
1494
70.1k
    fwd_end_idx--;
1495
70.1k
  }
1496
1497
  // Assign all the remaining frame(s), if any, to the earliest reference
1498
  // frame.
1499
52.2k
  for (; ref_idx < (INTER_REFS_PER_FRAME - 2); ref_idx++) {
1500
26.1k
    const MV_REFERENCE_FRAME ref_frame = ref_frame_list[ref_idx];
1501
26.1k
    if (ref_flag_list[ref_frame - LAST_FRAME] == 1) continue;
1502
12.3k
    set_ref_frame_info(remapped_ref_idx, ref_frame - LAST_FRAME,
1503
12.3k
                       &ref_frame_info[fwd_start_idx]);
1504
12.3k
    ref_flag_list[ref_frame - LAST_FRAME] = 1;
1505
12.3k
  }
1506
1507
208k
  for (int i = 0; i < INTER_REFS_PER_FRAME; i++) {
1508
    assert(ref_flag_list[i] == 1);
1509
182k
  }
1510
26.1k
}