mvref_common.c 76.2 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Jingning Han's avatar
Jingning Han committed
10
11
 */

12
#include "av1/common/mvref_common.h"
Yue Chen's avatar
Yue Chen committed
13
14
15
#if CONFIG_WARPED_MOTION
#include "av1/common/warped_motion.h"
#endif  // CONFIG_WARPED_MOTION
Jingning Han's avatar
Jingning Han committed
16

17
18
19
20
21
22
static uint8_t add_ref_mv_candidate(const MODE_INFO *const candidate_mi,
                                    const MB_MODE_INFO *const candidate,
                                    const MV_REFERENCE_FRAME rf[2],
                                    uint8_t *refmv_count,
                                    CANDIDATE_MV *ref_mv_stack,
                                    const int use_hp, int len, int block,
RogerZhou's avatar
RogerZhou committed
23
24
25
26
27
28
                                    int col, int weight
#if CONFIG_AMVR
                                    ,
                                    int is_integer
#endif
                                    ) {
Jingning Han's avatar
Jingning Han committed
29
30
  int index = 0, ref;
  int newmv_count = 0;
31
32
33
34
35
#if CONFIG_CB4X4
  const int unify_bsize = 1;
#else
  const int unify_bsize = 0;
#endif
36
  assert(weight % 2 == 0);
Jingning Han's avatar
Jingning Han committed
37

Emil Keyder's avatar
Emil Keyder committed
38
  if (rf[1] == NONE_FRAME) {
Jingning Han's avatar
Jingning Han committed
39
40
41
    // single reference frame
    for (ref = 0; ref < 2; ++ref) {
      if (candidate->ref_frame[ref] == rf[0]) {
clang-format's avatar
clang-format committed
42
        int_mv this_refmv = get_sub_block_mv(candidate_mi, ref, col, block);
RogerZhou's avatar
RogerZhou committed
43
44
45
#if CONFIG_AMVR
        lower_mv_precision(&this_refmv.as_mv, use_hp, is_integer);
#else
46
        lower_mv_precision(&this_refmv.as_mv, use_hp);
RogerZhou's avatar
RogerZhou committed
47
#endif
48

Jingning Han's avatar
Jingning Han committed
49
        for (index = 0; index < *refmv_count; ++index)
clang-format's avatar
clang-format committed
50
          if (ref_mv_stack[index].this_mv.as_int == this_refmv.as_int) break;
51

52
        if (index < *refmv_count) ref_mv_stack[index].weight += weight * len;
53

Jingning Han's avatar
Jingning Han committed
54
55
56
        // Add a new item to the list.
        if (index == *refmv_count) {
          ref_mv_stack[index].this_mv = this_refmv;
57
58
          ref_mv_stack[index].pred_diff[0] = av1_get_pred_diff_ctx(
              get_sub_block_pred_mv(candidate_mi, ref, col, block), this_refmv);
59
          ref_mv_stack[index].weight = weight * len;
Jingning Han's avatar
Jingning Han committed
60
          ++(*refmv_count);
61

62
          if (candidate->mode == NEWMV) ++newmv_count;
Jingning Han's avatar
Jingning Han committed
63
64
        }

65
66
        if (candidate_mi->mbmi.sb_type < BLOCK_8X8 && block >= 0 &&
            !unify_bsize) {
Jingning Han's avatar
Jingning Han committed
67
          int alt_block = 3 - block;
clang-format's avatar
clang-format committed
68
          this_refmv = get_sub_block_mv(candidate_mi, ref, col, alt_block);
RogerZhou's avatar
RogerZhou committed
69
70
71
#if CONFIG_AMVR
          lower_mv_precision(&this_refmv.as_mv, use_hp, is_integer);
#else
72
          lower_mv_precision(&this_refmv.as_mv, use_hp);
RogerZhou's avatar
RogerZhou committed
73
#endif
74
          for (index = 0; index < *refmv_count; ++index)
clang-format's avatar
clang-format committed
75
            if (ref_mv_stack[index].this_mv.as_int == this_refmv.as_int) break;
76

Yaowu Xu's avatar
Yaowu Xu committed
77
          if (index < *refmv_count) ref_mv_stack[index].weight += len;
78
79
80
81

          // Add a new item to the list.
          if (index == *refmv_count) {
            ref_mv_stack[index].this_mv = this_refmv;
82
83
84
            ref_mv_stack[index].pred_diff[0] = av1_get_pred_diff_ctx(
                get_sub_block_pred_mv(candidate_mi, ref, col, alt_block),
                this_refmv);
Yaowu Xu's avatar
Yaowu Xu committed
85
            ref_mv_stack[index].weight = len;
86
            ++(*refmv_count);
87

88
            if (candidate->mode == NEWMV) ++newmv_count;
89
90
91
          }
        }
      }
Jingning Han's avatar
Jingning Han committed
92
93
94
    }
  } else {
    // compound reference frame
clang-format's avatar
clang-format committed
95
    if (candidate->ref_frame[0] == rf[0] && candidate->ref_frame[1] == rf[1]) {
96
      int_mv this_refmv[2];
Jingning Han's avatar
Jingning Han committed
97

98
99
      for (ref = 0; ref < 2; ++ref) {
        this_refmv[ref] = get_sub_block_mv(candidate_mi, ref, col, block);
RogerZhou's avatar
RogerZhou committed
100
101
102
103

#if CONFIG_AMVR
        lower_mv_precision(&this_refmv[ref].as_mv, use_hp, is_integer);
#else
104
        lower_mv_precision(&this_refmv[ref].as_mv, use_hp);
RogerZhou's avatar
RogerZhou committed
105
#endif
106
      }
107

Jingning Han's avatar
Jingning Han committed
108
109
110
111
112
      for (index = 0; index < *refmv_count; ++index)
        if ((ref_mv_stack[index].this_mv.as_int == this_refmv[0].as_int) &&
            (ref_mv_stack[index].comp_mv.as_int == this_refmv[1].as_int))
          break;

113
      if (index < *refmv_count) ref_mv_stack[index].weight += weight * len;
Jingning Han's avatar
Jingning Han committed
114
115
116
117
118

      // Add a new item to the list.
      if (index == *refmv_count) {
        ref_mv_stack[index].this_mv = this_refmv[0];
        ref_mv_stack[index].comp_mv = this_refmv[1];
119
120
121
122
        ref_mv_stack[index].pred_diff[0] = av1_get_pred_diff_ctx(
            get_sub_block_pred_mv(candidate_mi, 0, col, block), this_refmv[0]);
        ref_mv_stack[index].pred_diff[1] = av1_get_pred_diff_ctx(
            get_sub_block_pred_mv(candidate_mi, 1, col, block), this_refmv[1]);
123
        ref_mv_stack[index].weight = weight * len;
Jingning Han's avatar
Jingning Han committed
124
125
        ++(*refmv_count);

126
127
128
#if CONFIG_EXT_INTER
        if (candidate->mode == NEW_NEWMV)
#else
Jingning Han's avatar
Jingning Han committed
129
        if (candidate->mode == NEWMV)
130
#endif  // CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
131
132
133
          ++newmv_count;
      }

134
135
      if (candidate_mi->mbmi.sb_type < BLOCK_8X8 && block >= 0 &&
          !unify_bsize) {
Jingning Han's avatar
Jingning Han committed
136
137
138
139
        int alt_block = 3 - block;
        this_refmv[0] = get_sub_block_mv(candidate_mi, 0, col, alt_block);
        this_refmv[1] = get_sub_block_mv(candidate_mi, 1, col, alt_block);

RogerZhou's avatar
RogerZhou committed
140
141
142
143
        for (ref = 0; ref < 2; ++ref) {
#if CONFIG_AMVR
          lower_mv_precision(&this_refmv[ref].as_mv, use_hp, is_integer);
#else
144
          lower_mv_precision(&this_refmv[ref].as_mv, use_hp);
RogerZhou's avatar
RogerZhou committed
145
146
#endif
        }
Jingning Han's avatar
Jingning Han committed
147
148
149
150
151
        for (index = 0; index < *refmv_count; ++index)
          if (ref_mv_stack[index].this_mv.as_int == this_refmv[0].as_int &&
              ref_mv_stack[index].comp_mv.as_int == this_refmv[1].as_int)
            break;

Yaowu Xu's avatar
Yaowu Xu committed
152
        if (index < *refmv_count) ref_mv_stack[index].weight += len;
Jingning Han's avatar
Jingning Han committed
153
154
155
156
157

        // Add a new item to the list.
        if (index == *refmv_count) {
          ref_mv_stack[index].this_mv = this_refmv[0];
          ref_mv_stack[index].comp_mv = this_refmv[1];
158
159
160
161
162
163
          ref_mv_stack[index].pred_diff[0] = av1_get_pred_diff_ctx(
              get_sub_block_pred_mv(candidate_mi, 0, col, block),
              this_refmv[0]);
          ref_mv_stack[index].pred_diff[0] = av1_get_pred_diff_ctx(
              get_sub_block_pred_mv(candidate_mi, 1, col, block),
              this_refmv[1]);
Yaowu Xu's avatar
Yaowu Xu committed
164
          ref_mv_stack[index].weight = len;
Jingning Han's avatar
Jingning Han committed
165
166
          ++(*refmv_count);

167
168
169
#if CONFIG_EXT_INTER
          if (candidate->mode == NEW_NEWMV)
#else
Jingning Han's avatar
Jingning Han committed
170
          if (candidate->mode == NEWMV)
171
#endif  // CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
172
173
174
175
176
177
178
179
            ++newmv_count;
        }
      }
    }
  }
  return newmv_count;
}

Yaowu Xu's avatar
Yaowu Xu committed
180
static uint8_t scan_row_mbmi(const AV1_COMMON *cm, const MACROBLOCKD *xd,
Yunqing Wang's avatar
Yunqing Wang committed
181
                             const int mi_col, int block,
clang-format's avatar
clang-format committed
182
                             const MV_REFERENCE_FRAME rf[2], int row_offset,
183
184
                             CANDIDATE_MV *ref_mv_stack, uint8_t *refmv_count,
                             int max_row_offset, int *processed_rows) {
Yunqing Wang's avatar
Yunqing Wang committed
185
  const int end_mi = AOMMIN(xd->n8_w, cm->mi_cols - mi_col);
186
187
  const int n8_w_8 = mi_size_wide[BLOCK_8X8];
  const int n8_w_16 = mi_size_wide[BLOCK_16X16];
Jingning Han's avatar
Jingning Han committed
188
189
  int i;
  uint8_t newmv_count = 0;
190
  int col_offset = 0;
191
#if CONFIG_CB4X4
192
  const int shift = 0;
193
  // TODO(jingning): Revisit this part after cb4x4 is stable.
194
195
  if (abs(row_offset) > 1) {
    col_offset = 1;
196
    if (mi_col & 0x01 && xd->n8_w < n8_w_8) --col_offset;
197
  }
Yunqing Wang's avatar
Yunqing Wang committed
198
  const int use_step_16 = (xd->n8_w >= 16);
199
#else
200
  const int shift = 1;
Yunqing Wang's avatar
Yunqing Wang committed
201
  const int use_step_16 = (xd->n8_w >= 8);
202
#endif
Yunqing Wang's avatar
Yunqing Wang committed
203
  MODE_INFO **const candidate_mi0 = xd->mi + row_offset * xd->mi_stride;
Jingning Han's avatar
Jingning Han committed
204

Yunqing Wang's avatar
Yunqing Wang committed
205
206
207
  for (i = 0; i < end_mi && *refmv_count < MAX_REF_MV_STACK_SIZE;) {
    const MODE_INFO *const candidate_mi = candidate_mi0[col_offset + i];
    const MB_MODE_INFO *const candidate = &candidate_mi->mbmi;
208
209
210
    const int candidate_bsize = candidate->sb_type;
    const int n8_w = mi_size_wide[candidate_bsize];
    int len = AOMMIN(xd->n8_w, n8_w);
Yunqing Wang's avatar
Yunqing Wang committed
211
212
213
214
    if (use_step_16)
      len = AOMMAX(n8_w_16, len);
    else if (abs(row_offset) > 1)
      len = AOMMAX(len, n8_w_8);
215
216
217
218
219
220
221
222
223
224
225

    int weight = 2;
    if (xd->n8_w >= n8_w_8 && xd->n8_w <= n8_w) {
      int inc = AOMMIN(-max_row_offset + row_offset + 1,
                       mi_size_high[candidate_bsize]);
      // Obtain range used in weight calculation.
      weight = AOMMAX(weight, (inc << shift));
      // Update processed rows.
      *processed_rows = inc - row_offset - 1;
    }

RogerZhou's avatar
RogerZhou committed
226
227
228
229
230
231
#if CONFIG_AMVR
    newmv_count += add_ref_mv_candidate(
        candidate_mi, candidate, rf, refmv_count, ref_mv_stack,
        cm->allow_high_precision_mv, len, block, col_offset + i, weight,
        cm->cur_frame_mv_precision_level);
#else
Yunqing Wang's avatar
Yunqing Wang committed
232
233
    newmv_count += add_ref_mv_candidate(
        candidate_mi, candidate, rf, refmv_count, ref_mv_stack,
234
        cm->allow_high_precision_mv, len, block, col_offset + i, weight);
RogerZhou's avatar
RogerZhou committed
235
#endif
Yunqing Wang's avatar
Yunqing Wang committed
236
    i += len;
237
  }
238
239

  return newmv_count;
240
241
}

Yaowu Xu's avatar
Yaowu Xu committed
242
static uint8_t scan_col_mbmi(const AV1_COMMON *cm, const MACROBLOCKD *xd,
Yunqing Wang's avatar
Yunqing Wang committed
243
                             const int mi_row, int block,
clang-format's avatar
clang-format committed
244
                             const MV_REFERENCE_FRAME rf[2], int col_offset,
245
246
                             CANDIDATE_MV *ref_mv_stack, uint8_t *refmv_count,
                             int max_col_offset, int *processed_cols) {
Yunqing Wang's avatar
Yunqing Wang committed
247
  const int end_mi = AOMMIN(xd->n8_h, cm->mi_rows - mi_row);
248
249
  const int n8_h_8 = mi_size_high[BLOCK_8X8];
  const int n8_h_16 = mi_size_high[BLOCK_16X16];
250
  int i;
251
  uint8_t newmv_count = 0;
252
  int row_offset = 0;
253
#if CONFIG_CB4X4
254
  const int shift = 0;
255
256
  if (abs(col_offset) > 1) {
    row_offset = 1;
257
    if (mi_row & 0x01 && xd->n8_h < n8_h_8) --row_offset;
258
  }
Yunqing Wang's avatar
Yunqing Wang committed
259
  const int use_step_16 = (xd->n8_h >= 16);
260
#else
261
  const int shift = 1;
Yunqing Wang's avatar
Yunqing Wang committed
262
  const int use_step_16 = (xd->n8_h >= 8);
263
#endif
264

Yunqing Wang's avatar
Yunqing Wang committed
265
266
267
268
  for (i = 0; i < end_mi && *refmv_count < MAX_REF_MV_STACK_SIZE;) {
    const MODE_INFO *const candidate_mi =
        xd->mi[(row_offset + i) * xd->mi_stride + col_offset];
    const MB_MODE_INFO *const candidate = &candidate_mi->mbmi;
269
270
271
    const int candidate_bsize = candidate->sb_type;
    const int n8_h = mi_size_high[candidate_bsize];
    int len = AOMMIN(xd->n8_h, n8_h);
Yunqing Wang's avatar
Yunqing Wang committed
272
273
274
275
    if (use_step_16)
      len = AOMMAX(n8_h_16, len);
    else if (abs(col_offset) > 1)
      len = AOMMAX(len, n8_h_8);
276
277
278
279
280
281
282
283
284
285
286

    int weight = 2;
    if (xd->n8_h >= n8_h_8 && xd->n8_h <= n8_h) {
      int inc = AOMMIN(-max_col_offset + col_offset + 1,
                       mi_size_wide[candidate_bsize]);
      // Obtain range used in weight calculation.
      weight = AOMMAX(weight, (inc << shift));
      // Update processed cols.
      *processed_cols = inc - col_offset - 1;
    }

RogerZhou's avatar
RogerZhou committed
287
288
289
290
291
292
#if CONFIG_AMVR
    newmv_count += add_ref_mv_candidate(
        candidate_mi, candidate, rf, refmv_count, ref_mv_stack,
        cm->allow_high_precision_mv, len, block, col_offset, weight,
        cm->cur_frame_mv_precision_level);
#else
Yunqing Wang's avatar
Yunqing Wang committed
293
294
    newmv_count += add_ref_mv_candidate(
        candidate_mi, candidate, rf, refmv_count, ref_mv_stack,
295
        cm->allow_high_precision_mv, len, block, col_offset, weight);
RogerZhou's avatar
RogerZhou committed
296
#endif
Yunqing Wang's avatar
Yunqing Wang committed
297
    i += len;
298
  }
299
300

  return newmv_count;
301
302
}

Yaowu Xu's avatar
Yaowu Xu committed
303
static uint8_t scan_blk_mbmi(const AV1_COMMON *cm, const MACROBLOCKD *xd,
304
                             const int mi_row, const int mi_col, int block,
clang-format's avatar
clang-format committed
305
306
                             const MV_REFERENCE_FRAME rf[2], int row_offset,
                             int col_offset, CANDIDATE_MV *ref_mv_stack,
307
                             uint8_t *refmv_count) {
308
309
  const TileInfo *const tile = &xd->tile;
  POSITION mi_pos;
310
  uint8_t newmv_count = 0;
311
312
313
314

  mi_pos.row = row_offset;
  mi_pos.col = col_offset;

315
  if (is_inside(tile, mi_col, mi_row, cm->mi_rows, cm, &mi_pos) &&
316
      *refmv_count < MAX_REF_MV_STACK_SIZE) {
317
318
319
    const MODE_INFO *const candidate_mi =
        xd->mi[mi_pos.row * xd->mi_stride + mi_pos.col];
    const MB_MODE_INFO *const candidate = &candidate_mi->mbmi;
320
    const int len = mi_size_wide[BLOCK_8X8];
321

RogerZhou's avatar
RogerZhou committed
322
323
324
325
326
327
#if CONFIG_AMVR
    newmv_count += add_ref_mv_candidate(
        candidate_mi, candidate, rf, refmv_count, ref_mv_stack,
        cm->allow_high_precision_mv, len, block, mi_pos.col, 2,
        cm->cur_frame_mv_precision_level);
#else
clang-format's avatar
clang-format committed
328
329
    newmv_count += add_ref_mv_candidate(
        candidate_mi, candidate, rf, refmv_count, ref_mv_stack,
330
        cm->allow_high_precision_mv, len, block, mi_pos.col, 2);
RogerZhou's avatar
RogerZhou committed
331
#endif
332
  }  // Analyze a single 8x8 block motion information.
Yaowu Xu's avatar
Yaowu Xu committed
333

334
  return newmv_count;
335
336
}

337
338
339
340
341
static int has_top_right(const AV1_COMMON *cm, const MACROBLOCKD *xd,
                         int mi_row, int mi_col, int bs) {
  const int sb_mi_size = mi_size_wide[cm->sb_size];
  const int mask_row = mi_row & (sb_mi_size - 1);
  const int mask_col = mi_col & (sb_mi_size - 1);
342

343
  // In a split partition all apart from the bottom right has a top right
344
  int has_tr = !((mask_row & bs) && (mask_col & bs));
345

346
347
  // bs > 0 and bs is a power of 2
  assert(bs > 0 && !(bs & (bs - 1)));
348

349
350
351
  // For each 4x4 group of blocks, when the bottom right is decoded the blocks
  // to the right have not been decoded therefore the bottom right does
  // not have a top right
352
  while (bs < sb_mi_size) {
353
354
    if (mask_col & bs) {
      if ((mask_col & (2 * bs)) && (mask_row & (2 * bs))) {
355
        has_tr = 0;
356
357
358
359
360
361
362
        break;
      }
    } else {
      break;
    }
    bs <<= 1;
  }
363

364
  // The left hand of two vertical rectangles always has a top right (as the
365
  // block above will have been decoded)
366
  if (xd->n8_w < xd->n8_h)
clang-format's avatar
clang-format committed
367
    if (!xd->is_sec_rect) has_tr = 1;
368

369
370
  // The bottom of two horizontal rectangles never has a top right (as the block
  // to the right won't have been decoded)
371
  if (xd->n8_w > xd->n8_h)
clang-format's avatar
clang-format committed
372
    if (xd->is_sec_rect) has_tr = 0;
373

374
375
376
377
#if CONFIG_EXT_PARTITION_TYPES
  // The bottom left square of a Vertical A does not have a top right as it is
  // decoded before the right hand rectangle of the partition
  if (xd->mi[0]->mbmi.partition == PARTITION_VERT_A)
378
    if ((mask_row & bs) && !(mask_col & bs)) has_tr = 0;
379
380
#endif  // CONFIG_EXT_PARTITION_TYPES

381
382
383
  return has_tr;
}

384
#if CONFIG_MFMV
385
386
387
388
389
390
391
392
393
static int check_sb_border(const AV1_COMMON *cm, const int mi_row,
                           const int mi_col, const int row_offset,
                           const int col_offset) {
  const int sb_mi_size = mi_size_wide[cm->sb_size];
  const int row = mi_row & (sb_mi_size - 1);
  const int col = mi_col & (sb_mi_size - 1);

  if (row + row_offset < 0 || row + row_offset >= sb_mi_size ||
      col + col_offset < 0 || col + col_offset >= sb_mi_size)
394
395
396
397
398
    return 0;

  return 1;
}

399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
static int add_tpl_ref_mv(const AV1_COMMON *cm,
                          const MV_REF *prev_frame_mvs_base,
                          const MACROBLOCKD *xd, int mi_row, int mi_col,
                          MV_REFERENCE_FRAME ref_frame, int blk_row,
                          int blk_col, uint8_t *refmv_count,
                          CANDIDATE_MV *ref_mv_stack, int16_t *mode_context) {
  (void)prev_frame_mvs_base;
  POSITION mi_pos;
  int idx;
  int coll_blk_count = 0;
  const int weight_unit = 1;  // mi_size_wide[BLOCK_8X8];

#if CONFIG_MV_COMPRESS
  mi_pos.row = (mi_row & 0x01) ? blk_row : blk_row + 1;
  mi_pos.col = (mi_col & 0x01) ? blk_col : blk_col + 1;
#else
  mi_pos.row = blk_row;
  mi_pos.col = blk_col;
#endif

  if (!is_inside(&xd->tile, mi_col, mi_row, cm->mi_rows, cm, &mi_pos))
    return coll_blk_count;

Jingning Han's avatar
Jingning Han committed
422
423
424
425
  const TPL_MV_REF *prev_frame_mvs = cm->cur_frame->tpl_mvs +
                                     (mi_row + mi_pos.row) * cm->mi_stride +
                                     (mi_col + mi_pos.col);

426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
  MV_REFERENCE_FRAME rf[2];
  av1_set_ref_frame(rf, ref_frame);

  if (rf[1] == NONE_FRAME) {
    for (int i = 0; i < MFMV_STACK_SIZE; ++i) {
      if (prev_frame_mvs->mfmv[ref_frame - LAST_FRAME][i].as_int !=
          INVALID_MV) {
        int_mv this_refmv = prev_frame_mvs->mfmv[ref_frame - LAST_FRAME][i];
        lower_mv_precision(&this_refmv.as_mv, cm->allow_high_precision_mv);

        if (abs(this_refmv.as_mv.row) >= 16 || abs(this_refmv.as_mv.col) >= 16)
          mode_context[ref_frame] |= (1 << ZEROMV_OFFSET);

        for (idx = 0; idx < *refmv_count; ++idx)
          if (abs(this_refmv.as_mv.row - ref_mv_stack[idx].this_mv.as_mv.row) <
                  4 &&
              abs(this_refmv.as_mv.col - ref_mv_stack[idx].this_mv.as_mv.col) <
                  4)
            break;

        if (idx < *refmv_count) ref_mv_stack[idx].weight += 2 * weight_unit;

        if (idx == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
          ref_mv_stack[idx].this_mv.as_int = this_refmv.as_int;
          // TODO(jingning): Hard coded context number. Need to make it better
          // sense.
          ref_mv_stack[idx].pred_diff[0] = 1;
          ref_mv_stack[idx].weight = 2 * weight_unit;
          ++(*refmv_count);
        }

        ++coll_blk_count;
      }
    }
  } else {
    // Process compound inter mode
    for (int i = 0; i < MFMV_STACK_SIZE; ++i) {
      if (prev_frame_mvs->mfmv[rf[0] - LAST_FRAME][i].as_int != INVALID_MV &&
          prev_frame_mvs->mfmv[rf[1] - LAST_FRAME][i].as_int != INVALID_MV) {
        int_mv this_refmv = prev_frame_mvs->mfmv[rf[0] - LAST_FRAME][i];
        int_mv comp_refmv = prev_frame_mvs->mfmv[rf[1] - LAST_FRAME][i];
        lower_mv_precision(&this_refmv.as_mv, cm->allow_high_precision_mv);
        lower_mv_precision(&comp_refmv.as_mv, cm->allow_high_precision_mv);

        if (abs(this_refmv.as_mv.row) >= 16 ||
            abs(this_refmv.as_mv.col) >= 16 ||
            abs(comp_refmv.as_mv.row) >= 16 || abs(comp_refmv.as_mv.col) >= 16)
          mode_context[ref_frame] |= (1 << ZEROMV_OFFSET);

        for (idx = 0; idx < *refmv_count; ++idx)
          if (abs(this_refmv.as_mv.row - ref_mv_stack[idx].this_mv.as_mv.row) <
                  4 &&
              abs(this_refmv.as_mv.col - ref_mv_stack[idx].this_mv.as_mv.col) <
                  4 &&
              abs(comp_refmv.as_mv.row - ref_mv_stack[idx].comp_mv.as_mv.row) <
                  4 &&
              abs(comp_refmv.as_mv.col - ref_mv_stack[idx].comp_mv.as_mv.col) <
                  4)
            break;

        if (idx < *refmv_count) ref_mv_stack[idx].weight += 2 * weight_unit;

        if (idx == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
          ref_mv_stack[idx].this_mv.as_int = this_refmv.as_int;
          ref_mv_stack[idx].comp_mv.as_int = comp_refmv.as_int;
          // TODO(jingning): Hard coded context number. Need to make it better
          // sense.
          ref_mv_stack[idx].pred_diff[0] = 1;
          ref_mv_stack[idx].pred_diff[1] = 1;
          ref_mv_stack[idx].weight = 2 * weight_unit;
          ++(*refmv_count);
        }

        ++coll_blk_count;
      }
    }
  }

  return coll_blk_count;
}
#else
507
508
509
510
511
512
513
514
515
516
517
static int add_col_ref_mv(const AV1_COMMON *cm,
                          const MV_REF *prev_frame_mvs_base,
                          const MACROBLOCKD *xd, int mi_row, int mi_col,
                          MV_REFERENCE_FRAME ref_frame, int blk_row,
                          int blk_col, uint8_t *refmv_count,
                          CANDIDATE_MV *ref_mv_stack, int16_t *mode_context) {
  const MV_REF *prev_frame_mvs =
      prev_frame_mvs_base + blk_row * cm->mi_cols + blk_col;
  POSITION mi_pos;
  int ref, idx;
  int coll_blk_count = 0;
518
  const int weight_unit = mi_size_wide[BLOCK_8X8];
519

520
521
522
523
#if CONFIG_MV_COMPRESS
  mi_pos.row = (mi_row & 0x01) ? blk_row : blk_row + 1;
  mi_pos.col = (mi_col & 0x01) ? blk_col : blk_col + 1;
#else
524
525
  mi_pos.row = blk_row;
  mi_pos.col = blk_col;
526
#endif
527

528
  if (!is_inside(&xd->tile, mi_col, mi_row, cm->mi_rows, cm, &mi_pos))
529
    return coll_blk_count;
530
531
532
  for (ref = 0; ref < 2; ++ref) {
    if (prev_frame_mvs->ref_frame[ref] == ref_frame) {
      int_mv this_refmv = prev_frame_mvs->mv[ref];
RogerZhou's avatar
RogerZhou committed
533
534
535
536
#if CONFIG_AMVR
      lower_mv_precision(&this_refmv.as_mv, cm->allow_high_precision_mv,
                         cm->cur_frame_mv_precision_level);
#else
537
      lower_mv_precision(&this_refmv.as_mv, cm->allow_high_precision_mv);
RogerZhou's avatar
RogerZhou committed
538
#endif
539
540
541
542
543
544
      if (abs(this_refmv.as_mv.row) >= 16 || abs(this_refmv.as_mv.col) >= 16)
        mode_context[ref_frame] |= (1 << ZEROMV_OFFSET);

      for (idx = 0; idx < *refmv_count; ++idx)
        if (this_refmv.as_int == ref_mv_stack[idx].this_mv.as_int) break;

545
      if (idx < *refmv_count) ref_mv_stack[idx].weight += 2 * weight_unit;
546
547
548

      if (idx == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
        ref_mv_stack[idx].this_mv.as_int = this_refmv.as_int;
549
550
        ref_mv_stack[idx].pred_diff[0] =
            av1_get_pred_diff_ctx(prev_frame_mvs->pred_mv[ref], this_refmv);
551
        ref_mv_stack[idx].weight = 2 * weight_unit;
552
553
554
555
556
557
558
559
560
        ++(*refmv_count);
      }

      ++coll_blk_count;
    }
  }

  return coll_blk_count;
}
561
#endif
562

Yaowu Xu's avatar
Yaowu Xu committed
563
static void setup_ref_mv_list(const AV1_COMMON *cm, const MACROBLOCKD *xd,
564
                              MV_REFERENCE_FRAME ref_frame,
clang-format's avatar
clang-format committed
565
566
567
                              uint8_t *refmv_count, CANDIDATE_MV *ref_mv_stack,
                              int_mv *mv_ref_list, int block, int mi_row,
                              int mi_col, int16_t *mode_context) {
568
  int idx, nearest_refmv_count = 0;
569
  uint8_t newmv_count = 0;
570
571
572
  CANDIDATE_MV tmp_mv;
  int len, nr_len;

573
574
575
576
577
578
579
#if CONFIG_MV_COMPRESS
  const MV_REF *const prev_frame_mvs_base =
      cm->use_prev_frame_mvs
          ? cm->prev_frame->mvs + (((mi_row >> 1) << 1) + 1) * cm->mi_cols +
                ((mi_col >> 1) << 1) + 1
          : NULL;
#else
clang-format's avatar
clang-format committed
580
581
582
583
  const MV_REF *const prev_frame_mvs_base =
      cm->use_prev_frame_mvs
          ? cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col
          : NULL;
584
#endif
585

586
  const int bs = AOMMAX(xd->n8_w, xd->n8_h);
587
  const int has_tr = has_top_right(cm, xd, mi_row, mi_col, bs);
Jingning Han's avatar
Jingning Han committed
588
589
  MV_REFERENCE_FRAME rf[2];

Yunqing Wang's avatar
Yunqing Wang committed
590
  const TileInfo *const tile = &xd->tile;
591
  int max_row_offset = 0, max_col_offset = 0;
Yunqing Wang's avatar
Yunqing Wang committed
592
#if CONFIG_CB4X4
593
594
  const int row_adj = (xd->n8_h < mi_size_high[BLOCK_8X8]) && (mi_row & 0x01);
  const int col_adj = (xd->n8_w < mi_size_wide[BLOCK_8X8]) && (mi_col & 0x01);
Yunqing Wang's avatar
Yunqing Wang committed
595
#endif
596
597
  int processed_rows = 0;
  int processed_cols = 0;
Yunqing Wang's avatar
Yunqing Wang committed
598
599
  int row_offset, col_offset;

600
  av1_set_ref_frame(rf, ref_frame);
601
  mode_context[ref_frame] = 0;
602
  *refmv_count = 0;
603

604
605
606
607
608
609
610
  // Find valid maximum row/col offset.
  if (xd->up_available) {
#if CONFIG_CB4X4
    max_row_offset = -(MVREF_ROWS << 1) + row_adj;
#else
    max_row_offset = -MVREF_ROWS;
#endif
Yunqing Wang's avatar
Yunqing Wang committed
611
612
613
    max_row_offset =
        find_valid_row_offset(tile, mi_row, cm->mi_rows, cm, max_row_offset);
  }
614
615
616
617
618
619
620

  if (xd->left_available) {
#if CONFIG_CB4X4
    max_col_offset = -(MVREF_COLS << 1) + col_adj;
#else
    max_col_offset = -MVREF_COLS;
#endif
Yunqing Wang's avatar
Yunqing Wang committed
621
622
    max_col_offset = find_valid_col_offset(tile, mi_col, max_col_offset);
  }
623

624
625
626
627
628
629
630
631
  // Scan the first above row mode info. row_offset = -1;
  if (abs(max_row_offset) >= 1)
    newmv_count += scan_row_mbmi(cm, xd, mi_col, block, rf, -1, ref_mv_stack,
                                 refmv_count, max_row_offset, &processed_rows);
  // Scan the first left column mode info. col_offset = -1;
  if (abs(max_col_offset) >= 1)
    newmv_count += scan_col_mbmi(cm, xd, mi_row, block, rf, -1, ref_mv_stack,
                                 refmv_count, max_col_offset, &processed_cols);
632
633
  // Check top-right boundary
  if (has_tr)
634
635
    newmv_count += scan_blk_mbmi(cm, xd, mi_row, mi_col, block, rf, -1,
                                 xd->n8_w, ref_mv_stack, refmv_count);
636

637
  nearest_refmv_count = *refmv_count;
638

639
  for (idx = 0; idx < nearest_refmv_count; ++idx)
640
    ref_mv_stack[idx].weight += REF_CAT_LEVEL;
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665

#if CONFIG_MFMV
  int blk_row, blk_col;
  int coll_blk_count = 0;

  int tpl_sample_pos[9][2] = {
    { -1, xd->n8_w }, { 0, xd->n8_w },  { xd->n8_h, xd->n8_w },
    { xd->n8_h, 0 },  { xd->n8_h, -1 }, { xd->n8_h, -2 },
    { -2, xd->n8_w }, { xd->n8_h, 2 },  { 1, xd->n8_w + 2 },
  };
  int i;

  for (blk_row = 0; blk_row < xd->n8_h; ++blk_row) {
    for (blk_col = 0; blk_col < xd->n8_w; ++blk_col) {
      coll_blk_count += add_tpl_ref_mv(cm, prev_frame_mvs_base, xd, mi_row,
                                       mi_col, ref_frame, blk_row, blk_col,
                                       refmv_count, ref_mv_stack, mode_context);
    }
  }

  if (coll_blk_count == 0) mode_context[ref_frame] |= (1 << ZEROMV_OFFSET);

  for (i = 0; i < 9; ++i) {
    blk_row = tpl_sample_pos[i][0];
    blk_col = tpl_sample_pos[i][1];
666

667
    if (!check_sb_border(cm, mi_row, mi_col, blk_row, blk_col)) continue;
668

669
670
671
672
673
674
    coll_blk_count += add_tpl_ref_mv(cm, prev_frame_mvs_base, xd, mi_row,
                                     mi_col, ref_frame, blk_row, blk_col,
                                     refmv_count, ref_mv_stack, mode_context);
  }

#else
675
#if CONFIG_TEMPMV_SIGNALING
676
  if (cm->use_prev_frame_mvs && rf[1] == NONE_FRAME)
677
#else
clang-format's avatar
clang-format committed
678
  if (prev_frame_mvs_base && cm->show_frame && cm->last_show_frame &&
679
      rf[1] == NONE_FRAME)
680
#endif
681
  {
682
    int blk_row, blk_col;
683
    int coll_blk_count = 0;
684
685
686
687
688
#if CONFIG_CB4X4
    const int mi_step = (xd->n8_w == 1 || xd->n8_h == 1)
                            ? mi_size_wide[BLOCK_8X8]
                            : mi_size_wide[BLOCK_16X16];
#else
689
    const int mi_step = mi_size_wide[BLOCK_16X16];
690
#endif
691
692
693
694
695
696
697
698
699
700

#if CONFIG_TPL_MV
    int tpl_sample_pos[5][2] = { { -1, xd->n8_w },
                                 { 0, xd->n8_w },
                                 { xd->n8_h, xd->n8_w },
                                 { xd->n8_h, 0 },
                                 { xd->n8_h, -1 } };
    int i;
#endif

701
702
    for (blk_row = 0; blk_row < xd->n8_h; blk_row += mi_step) {
      for (blk_col = 0; blk_col < xd->n8_w; blk_col += mi_step) {
703
704
705
        coll_blk_count += add_col_ref_mv(
            cm, prev_frame_mvs_base, xd, mi_row, mi_col, ref_frame, blk_row,
            blk_col, refmv_count, ref_mv_stack, mode_context);
706
707
      }
    }
708
709
710
711
712
713
714
715
716
717
718

#if CONFIG_TPL_MV
    for (i = 0; i < 5; ++i) {
      blk_row = tpl_sample_pos[i][0];
      blk_col = tpl_sample_pos[i][1];
      coll_blk_count += add_col_ref_mv(cm, prev_frame_mvs_base, xd, mi_row,
                                       mi_col, ref_frame, blk_row, blk_col,
                                       refmv_count, ref_mv_stack, mode_context);
    }
#endif

719
720
    if (coll_blk_count == 0) mode_context[ref_frame] |= (1 << ZEROMV_OFFSET);
  } else {
721
    mode_context[ref_frame] |= (1 << ZEROMV_OFFSET);
722
  }
723
#endif
724

725
  // Scan the second outer area.
Deng's avatar
Deng committed
726
727
  scan_blk_mbmi(cm, xd, mi_row, mi_col, block, rf, -1, -1, ref_mv_stack,
                refmv_count);
Yunqing Wang's avatar
Yunqing Wang committed
728
729
  for (idx = 2; idx <= MVREF_ROWS; ++idx) {
#if CONFIG_CB4X4
730
731
    row_offset = -(idx << 1) + 1 + row_adj;
    col_offset = -(idx << 1) + 1 + col_adj;
Yunqing Wang's avatar
Yunqing Wang committed
732
#else
733
734
    row_offset = -idx;
    col_offset = -idx;
Yunqing Wang's avatar
Yunqing Wang committed
735
736
#endif

737
738
739
740
741
742
743
744
745
    if (abs(row_offset) <= abs(max_row_offset) &&
        abs(row_offset) > processed_rows)
      scan_row_mbmi(cm, xd, mi_col, block, rf, row_offset, ref_mv_stack,
                    refmv_count, max_row_offset, &processed_rows);

    if (abs(col_offset) <= abs(max_col_offset) &&
        abs(col_offset) > processed_cols)
      scan_col_mbmi(cm, xd, mi_row, block, rf, col_offset, ref_mv_stack,
                    refmv_count, max_col_offset, &processed_cols);
Yunqing Wang's avatar
Yunqing Wang committed
746
747
748
  }

#if CONFIG_CB4X4
749
  col_offset = -(MVREF_COLS << 1) + 1 + col_adj;
Yunqing Wang's avatar
Yunqing Wang committed
750
#else
751
  col_offset = -MVREF_COLS;
Yunqing Wang's avatar
Yunqing Wang committed
752
#endif
753
754
755
756
  if (abs(col_offset) <= abs(max_col_offset) &&
      abs(col_offset) > processed_cols)
    scan_col_mbmi(cm, xd, mi_row, block, rf, col_offset, ref_mv_stack,
                  refmv_count, max_col_offset, &processed_cols);
757

758
759
760
  switch (nearest_refmv_count) {
    case 0:
      mode_context[ref_frame] |= 0;
clang-format's avatar
clang-format committed
761
      if (*refmv_count >= 1) mode_context[ref_frame] |= 1;
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789

      if (*refmv_count == 1)
        mode_context[ref_frame] |= (1 << REFMV_OFFSET);
      else if (*refmv_count >= 2)
        mode_context[ref_frame] |= (2 << REFMV_OFFSET);
      break;
    case 1:
      mode_context[ref_frame] |= (newmv_count > 0) ? 2 : 3;

      if (*refmv_count == 1)
        mode_context[ref_frame] |= (3 << REFMV_OFFSET);
      else if (*refmv_count >= 2)
        mode_context[ref_frame] |= (4 << REFMV_OFFSET);
      break;

    case 2:
    default:
      if (newmv_count >= 2)
        mode_context[ref_frame] |= 4;
      else if (newmv_count == 1)
        mode_context[ref_frame] |= 5;
      else
        mode_context[ref_frame] |= 6;

      mode_context[ref_frame] |= (5 << REFMV_OFFSET);
      break;
  }

790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
  // Rank the likelihood and assign nearest and near mvs.
  len = nearest_refmv_count;
  while (len > 0) {
    nr_len = 0;
    for (idx = 1; idx < len; ++idx) {
      if (ref_mv_stack[idx - 1].weight < ref_mv_stack[idx].weight) {
        tmp_mv = ref_mv_stack[idx - 1];
        ref_mv_stack[idx - 1] = ref_mv_stack[idx];
        ref_mv_stack[idx] = tmp_mv;
        nr_len = idx;
      }
    }
    len = nr_len;
  }

805
  len = *refmv_count;
806
807
808
809
810
811
812
813
814
815
816
817
818
  while (len > nearest_refmv_count) {
    nr_len = nearest_refmv_count;
    for (idx = nearest_refmv_count + 1; idx < len; ++idx) {
      if (ref_mv_stack[idx - 1].weight < ref_mv_stack[idx].weight) {
        tmp_mv = ref_mv_stack[idx - 1];
        ref_mv_stack[idx - 1] = ref_mv_stack[idx];
        ref_mv_stack[idx] = tmp_mv;
        nr_len = idx;
      }
    }
    len = nr_len;
  }

Emil Keyder's avatar
Emil Keyder committed
819
  if (rf[1] > NONE_FRAME) {
820
    for (idx = 0; idx < *refmv_count; ++idx) {
821
822
823
824
      clamp_mv_ref(&ref_mv_stack[idx].this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
                   xd->n8_h << MI_SIZE_LOG2, xd);
      clamp_mv_ref(&ref_mv_stack[idx].comp_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
                   xd->n8_h << MI_SIZE_LOG2, xd);
825
826
    }
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
827
    for (idx = 0; idx < AOMMIN(MAX_MV_REF_CANDIDATES, *refmv_count); ++idx) {
828
      mv_ref_list[idx].as_int = ref_mv_stack[idx].this_mv.as_int;
829
830
      clamp_mv_ref(&mv_ref_list[idx].as_mv, xd->n8_w << MI_SIZE_LOG2,
                   xd->n8_h << MI_SIZE_LOG2, xd);
831
    }
832
833
834
  }
}

Jingning Han's avatar
Jingning Han committed
835
836
// This function searches the neighbourhood of a given MB/SB
// to try and find candidate reference vectors.
Yaowu Xu's avatar
Yaowu Xu committed
837
static void find_mv_refs_idx(const AV1_COMMON *cm, const MACROBLOCKD *xd,
Jingning Han's avatar
Jingning Han committed
838
                             MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
clang-format's avatar
clang-format committed
839
840
                             int_mv *mv_ref_list, int block, int mi_row,
                             int mi_col, find_mv_refs_sync sync,
841
842
                             void *const data, int16_t *mode_context,
                             int_mv zeromv) {
Jingning Han's avatar
Jingning Han committed
843
  const int *ref_sign_bias = cm->ref_frame_sign_bias;
844
  const int sb_mi_size = mi_size_wide[cm->sb_size];
Jingning Han's avatar
Jingning Han committed
845
846
847
  int i, refmv_count = 0;
  int different_ref_found = 0;
  int context_counter = 0;
848
849
850
851
852
853
854
855
856
857
858
859
860
861
#if CONFIG_MV_COMPRESS
  const TileInfo *const tile_ = &xd->tile;
  int mi_row_end = tile_->mi_row_end;
  int mi_col_end = tile_->mi_col_end;
  const MV_REF *const prev_frame_mvs =
      cm->use_prev_frame_mvs
          ? cm->prev_frame->mvs +
                AOMMIN(((mi_row >> 1) << 1) + 1 + (((xd->n8_h - 1) >> 1) << 1),
                       mi_row_end - 1) *
                    cm->mi_cols +
                AOMMIN(((mi_col >> 1) << 1) + 1 + (((xd->n8_w - 1) >> 1) << 1),
                       mi_col_end - 1)
          : NULL;
#else
clang-format's avatar
clang-format committed
862
863
864
865
  const MV_REF *const prev_frame_mvs =
      cm->use_prev_frame_mvs
          ? cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col
          : NULL;
866
867
868
#endif
#if CONFIG_INTRABC
  assert(IMPLIES(ref_frame == INTRA_FRAME, cm->use_prev_frame_mvs == 0));
869
#endif
Jingning Han's avatar
Jingning Han committed
870
  const TileInfo *const tile = &xd->tile;
871
872
873
  const BLOCK_SIZE bsize = mi->mbmi.sb_type;
  const int bw = block_size_wide[AOMMAX(bsize, BLOCK_8X8)];
  const int bh = block_size_high[AOMMAX(bsize, BLOCK_8X8)];
Deng's avatar
Deng committed
874
  POSITION mv_ref_search[MVREF_NEIGHBOURS];
875
876
  const int num_8x8_blocks_wide = num_8x8_blocks_wide_lookup[bsize];
  const int num_8x8_blocks_high = num_8x8_blocks_high_lookup[bsize];
Deng's avatar
Deng committed
877
878
879
880
881
882
883
884
885
886
  mv_ref_search[0].row = num_8x8_blocks_high - 1;
  mv_ref_search[0].col = -1;
  mv_ref_search[1].row = -1;
  mv_ref_search[1].col = num_8x8_blocks_wide - 1;
  mv_ref_search[2].row = -1;
  mv_ref_search[2].col = (num_8x8_blocks_wide - 1) >> 1;
  mv_ref_search[3].row = (num_8x8_blocks_high - 1) >> 1;
  mv_ref_search[3].col = -1;
  mv_ref_search[4].row = -1;
  mv_ref_search[4].col = -1;
887
888
889
890
891
892
893
894
895
896
897
898
899
#if CONFIG_EXT_PARTITION_TYPES
  if (num_8x8_blocks_wide == num_8x8_blocks_high) {
    mv_ref_search[5].row = -1;
    mv_ref_search[5].col = 0;
    mv_ref_search[6].row = 0;
    mv_ref_search[6].col = -1;
  } else {
    mv_ref_search[5].row = -1;
    mv_ref_search[5].col = num_8x8_blocks_wide;
    mv_ref_search[6].row = num_8x8_blocks_high;
    mv_ref_search[6].col = -1;
  }
#else
Deng's avatar
Deng committed
900
901
902
903
  mv_ref_search[5].row = -1;
  mv_ref_search[5].col = num_8x8_blocks_wide;
  mv_ref_search[6].row = num_8x8_blocks_high;
  mv_ref_search[6].col = -1;
904
#endif  // CONFIG_EXT_PARTITION_TYPES
Deng's avatar
Deng committed
905
906
907
908
  mv_ref_search[7].row = -1;
  mv_ref_search[7].col = -3;
  mv_ref_search[8].row = num_8x8_blocks_high - 1;
  mv_ref_search[8].col = -3;
909
910
911
912
913
914
915

#if CONFIG_CB4X4
  for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
    mv_ref_search[i].row *= 2;
    mv_ref_search[i].col *= 2;
  }
#endif  // CONFIG_CB4X4
Jingning Han's avatar
Jingning Han committed
916
917
918
919
920
921

  // The nearest 2 blocks are treated differently
  // if the size < 8x8 we get the mv from the bmi substructure,
  // and we also need to keep a mode count.
  for (i = 0; i < 2; ++i) {
    const POSITION *const mv_ref = &mv_ref_search[i];
922
    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, cm, mv_ref)) {
clang-format's avatar
clang-format committed
923
924
      const MODE_INFO *const candidate_mi =
          xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
Jingning Han's avatar
Jingning Han committed
925
926
927
928
929
930
931
      const MB_MODE_INFO *const candidate = &candidate_mi->mbmi;
      // Keep counts for entropy encoding.
      context_counter += mode_2_counter[candidate->mode];
      different_ref_found = 1;

      if (candidate->ref_frame[0] == ref_frame)
        ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, block),
932
                        refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
933
934
      else if (candidate->ref_frame[1] == ref_frame)
        ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 1, mv_ref->col, block),
935
                        refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
936
937
938
939
940
941
942
943
    }
  }

  // Check the rest of the neighbors in much the same way
  // as before except we don't need to keep track of sub blocks or
  // mode counts.
  for (; i < MVREF_NEIGHBOURS; ++i) {
    const POSITION *const mv_ref = &mv_ref_search[i];
944
    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, cm, mv_ref)) {
clang-format's avatar
clang-format committed
945
      const MB_MODE_INFO *const candidate =
Yaowu Xu's avatar
Yaowu Xu committed
946
947
948
          !xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride]
              ? NULL
              : &xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride]->mbmi;
Yaowu Xu's avatar
Yaowu Xu committed
949
      if (candidate == NULL) continue;
950
951
      if ((mi_row & (sb_mi_size - 1)) + mv_ref->row >= sb_mi_size ||
          (mi_col & (sb_mi_size - 1)) + mv_ref->col >= sb_mi_size)
Deng's avatar
Deng committed
952
        continue;
Jingning Han's avatar
Jingning Han committed
953
954
955
      different_ref_found = 1;

      if (candidate->ref_frame[0] == ref_frame)
clang-format's avatar
clang-format committed
956
957
        ADD_MV_REF_LIST(candidate->mv[0], refmv_count, mv_ref_list, bw, bh, xd,
                        Done);
Jingning Han's avatar
Jingning Han committed
958
      else if (candidate->ref_frame[1] == ref_frame)
clang-format's avatar
clang-format committed
959
960
        ADD_MV_REF_LIST(candidate->mv[1], refmv_count, mv_ref_list, bw, bh, xd,
                        Done);
Jingning Han's avatar
Jingning Han committed
961
962
963
    }
  }

clang-format's avatar
clang-format committed
964
965
966
967
// TODO(hkuang): Remove this sync after fixing pthread_cond_broadcast
// on windows platform. The sync here is unncessary if use_perv_frame_mvs
// is 0. But after removing it, there will be hang in the unit test on windows
// due to several threads waiting for a thread's signal.
Jingning Han's avatar
Jingning Han committed
968
#if defined(_WIN32) && !HAVE_PTHREAD_H
clang-format's avatar
clang-format committed
969
970
971
  if (cm->frame_parallel_decode && sync != NULL) {
    sync(data, mi_row);
  }
Jingning Han's avatar
Jingning Han committed
972
973
974
975
976
977
978
979
980
981
#endif

  // Check the last frame's mode and mv info.
  if (cm->use_prev_frame_mvs) {
    // Synchronize here for frame parallel decode if sync function is provided.
    if (cm->frame_parallel_decode && sync != NULL) {
      sync(data, mi_row);
    }

    if (prev_frame_mvs->ref_frame[0] == ref_frame) {
clang-format's avatar
clang-format committed
982
983
      ADD_MV_REF_LIST(prev_frame_mvs->mv[0], refmv_count, mv_ref_list, bw, bh,
                      xd, Done);
Jingning Han's avatar
Jingning Han committed
984
    } else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
clang-format's avatar
clang-format committed
985
986
      ADD_MV_REF_LIST(prev_frame_mvs->mv[1], refmv_count, mv_ref_list, bw, bh,
                      xd, Done);
Jingning Han's avatar
Jingning Han committed
987
988
989
990
991
992
993
994
995
    }
  }

  // Since we couldn't find 2 mvs from the same reference frame
  // go back through the neighbors and find motion vectors from
  // different reference frames.
  if (different_ref_found) {
    for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
      const POSITION *mv_ref = &mv_ref_search[i];
996
      if (is_inside(tile, mi_col, mi_row, cm->mi_rows, cm, mv_ref)) {
clang-format's avatar
clang-format committed
997
        const MB_MODE_INFO *const candidate =
Yaowu Xu's avatar
Yaowu Xu committed
998
999
1000
            !xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride]
                ? NULL
                : &xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride]->mbmi;
Yaowu Xu's avatar
Yaowu Xu committed
1001
        if (candidate == NULL) continue;
1002
1003
        if ((mi_row & (sb_mi_size - 1)) + mv_ref->row >= sb_mi_size ||
            (mi_col & (sb_mi_size - 1)) + mv_ref->col >= sb_mi_size)
Deng's avatar
Deng committed
1004
          continue;
Jingning Han's avatar
Jingning Han committed
1005
1006
1007

        // If the candidate is INTRA we don't want to consider its mv.
        IF_DIFF_REF_FRAME_ADD_MV(candidate, ref_frame, ref_sign_bias,
1008
                                 refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
      }
    }
  }

  // Since we still don't have a candidate we'll try the last frame.
  if (cm->use_prev_frame_mvs) {
    if (prev_frame_mvs->ref_frame[0] != ref_frame &&
        prev_frame_mvs->ref_frame[0] > INTRA_FRAME) {
      int_mv mv = prev_frame_mvs->mv[0];
      if (ref_sign_bias[prev_frame_mvs->ref_frame[0]] !=
          ref_sign_bias[ref_frame]) {
        mv.as_mv.row *= -1;
        mv.as_mv.col *= -1;
      }
1023
      ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
1024
1025
1026
    }

    if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
1027
        prev_frame_mvs->ref_frame[1] != ref_frame) {
Jingning Han's avatar
Jingning Han committed
1028
1029
1030
1031
1032
1033
      int_mv mv = prev_frame_mvs->mv[1];
      if (ref_sign_bias[prev_frame_mvs->ref_frame[1]] !=
          ref_sign_bias[ref_frame]) {
        mv.as_mv.row *= -1;
        mv.as_mv.col *= -1;
      }
1034
      ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
1035
1036
1037
    }
  }

1038
1039
1040
Done:
  if (mode_context)
    mode_context[ref_frame] = counter_to_context[context_counter];
1041
  for (i = refmv_count; i < MAX_MV_REF_CANDIDATES; ++i)
1042
    mv_ref_list[i].as_int = zeromv.as_int;
Jingning Han's avatar
Jingning Han committed
1043
1044
}

Yue Chen's avatar
Yue Chen committed
1045
1046
#if CONFIG_EXT_INTER
// This function keeps a mode count for a given MB/SB
1047
1048
1049
1050
void av1_update_mv_context(const AV1_COMMON *cm, const MACROBLOCKD *xd,
                           MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
                           int_mv *mv_ref_list, int block, int mi_row,
                           int mi_col, int16_t *mode_context) {
Yue Chen's avatar
Yue Chen committed
1051
1052
  int i, refmv_count = 0;
  int context_counter = 0;
1053
1054
  const int bw = block_size_wide[mi->mbmi.sb_type];
  const int bh = block_size_high[mi->mbmi.sb_type];
Yue Chen's avatar
Yue Chen committed
1055
  const TileInfo *const tile = &xd->tile;
1056
  POSITION mv_ref_search[2];
1057
1058
  const int num_8x8_blocks_wide = mi_size_wide[mi->mbmi.sb_type];
  const int num_8x8_blocks_high = mi_size_high[mi->mbmi.sb_type];
1059

1060
1061
1062
1063
  mv_ref_search[0].row = num_8x8_blocks_high - 1;
  mv_ref_search[0].col = -1;
  mv_ref_search[1].row = -1;
  mv_ref_search[1].col = num_8x8_blocks_wide - 1;
Yue Chen's avatar
Yue Chen committed
1064
1065
1066
1067
1068
1069
1070
1071