Coverage Report

Created: 2022-08-24 06:15

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