mvref_common.c 40.3 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
#if CONFIG_REF_MV
18

clang-format's avatar
clang-format committed
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, int col) {
Jingning Han's avatar
Jingning Han committed
23
24
25
26
27
28
29
  int index = 0, ref;
  int newmv_count = 0;

  if (rf[1] == NONE) {
    // single reference frame
    for (ref = 0; ref < 2; ++ref) {
      if (candidate->ref_frame[ref] == rf[0]) {
clang-format's avatar
clang-format committed
30
        int_mv this_refmv = get_sub_block_mv(candidate_mi, ref, col, block);
31
32
        lower_mv_precision(&this_refmv.as_mv, use_hp);

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

Yaowu Xu's avatar
Yaowu Xu committed
36
        if (index < *refmv_count) ref_mv_stack[index].weight += 2 * len;
37

Jingning Han's avatar
Jingning Han committed
38
39
40
        // Add a new item to the list.
        if (index == *refmv_count) {
          ref_mv_stack[index].this_mv = this_refmv;
41
42
          ref_mv_stack[index].pred_diff[0] = av1_get_pred_diff_ctx(
              get_sub_block_pred_mv(candidate_mi, ref, col, block), this_refmv);
Yaowu Xu's avatar
Yaowu Xu committed
43
          ref_mv_stack[index].weight = 2 * len;
Jingning Han's avatar
Jingning Han committed
44
          ++(*refmv_count);
45

46
47
48
#if CONFIG_EXT_INTER
          if (candidate->mode == NEWMV || candidate->mode == NEWFROMNEARMV)
#else
Jingning Han's avatar
Jingning Han committed
49
          if (candidate->mode == NEWMV)
50
#endif  // CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
51
52
53
54
55
            ++newmv_count;
        }

        if (candidate_mi->mbmi.sb_type < BLOCK_8X8 && block >= 0) {
          int alt_block = 3 - block;
clang-format's avatar
clang-format committed
56
          this_refmv = get_sub_block_mv(candidate_mi, ref, col, alt_block);
57
58
          lower_mv_precision(&this_refmv.as_mv, use_hp);

59
          for (index = 0; index < *refmv_count; ++index)
clang-format's avatar
clang-format committed
60
            if (ref_mv_stack[index].this_mv.as_int == this_refmv.as_int) break;
61

Yaowu Xu's avatar
Yaowu Xu committed
62
          if (index < *refmv_count) ref_mv_stack[index].weight += len;
63
64
65
66

          // Add a new item to the list.
          if (index == *refmv_count) {
            ref_mv_stack[index].this_mv = this_refmv;
67
68
69
            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
70
            ref_mv_stack[index].weight = len;
71
            ++(*refmv_count);
72

73
#if CONFIG_EXT_INTER
clang-format's avatar
clang-format committed
74
            if (candidate->mode == NEWMV || candidate->mode == NEWFROMNEARMV)
75
#else
76
            if (candidate->mode == NEWMV)
77
#endif  // CONFIG_EXT_INTER
78
              ++newmv_count;
79
80
81
          }
        }
      }
Jingning Han's avatar
Jingning Han committed
82
83
84
    }
  } else {
    // compound reference frame
clang-format's avatar
clang-format committed
85
    if (candidate->ref_frame[0] == rf[0] && candidate->ref_frame[1] == rf[1]) {
86
      int_mv this_refmv[2];
Jingning Han's avatar
Jingning Han committed
87

88
89
      for (ref = 0; ref < 2; ++ref) {
        this_refmv[ref] = get_sub_block_mv(candidate_mi, ref, col, block);
90
        lower_mv_precision(&this_refmv[ref].as_mv, use_hp);
91
      }
92

Jingning Han's avatar
Jingning Han committed
93
94
95
96
97
      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
98
      if (index < *refmv_count) ref_mv_stack[index].weight += 2 * len;
Jingning Han's avatar
Jingning Han committed
99
100
101
102
103

      // 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];
104
105
106
107
        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]);
Yaowu Xu's avatar
Yaowu Xu committed
108
        ref_mv_stack[index].weight = 2 * len;
Jingning Han's avatar
Jingning Han committed
109
110
        ++(*refmv_count);

111
112
113
#if CONFIG_EXT_INTER
        if (candidate->mode == NEW_NEWMV)
#else
Jingning Han's avatar
Jingning Han committed
114
        if (candidate->mode == NEWMV)
115
#endif  // CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
116
117
118
119
120
121
122
123
          ++newmv_count;
      }

      if (candidate_mi->mbmi.sb_type < BLOCK_8X8 && block >= 0) {
        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);

124
125
126
        for (ref = 0; ref < 2; ++ref)
          lower_mv_precision(&this_refmv[ref].as_mv, use_hp);

Jingning Han's avatar
Jingning Han committed
127
128
129
130
131
        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
132
        if (index < *refmv_count) ref_mv_stack[index].weight += len;
Jingning Han's avatar
Jingning Han committed
133
134
135
136
137

        // 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];
138
139
140
141
142
143
          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
144
          ref_mv_stack[index].weight = len;
Jingning Han's avatar
Jingning Han committed
145
146
          ++(*refmv_count);

147
148
149
#if CONFIG_EXT_INTER
          if (candidate->mode == NEW_NEWMV)
#else
Jingning Han's avatar
Jingning Han committed
150
          if (candidate->mode == NEWMV)
151
#endif  // CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
152
153
154
155
156
157
158
159
            ++newmv_count;
        }
      }
    }
  }
  return newmv_count;
}

Yaowu Xu's avatar
Yaowu Xu committed
160
static uint8_t scan_row_mbmi(const AV1_COMMON *cm, const MACROBLOCKD *xd,
Jingning Han's avatar
Jingning Han committed
161
                             const int mi_row, const int mi_col, int block,
clang-format's avatar
clang-format committed
162
163
                             const MV_REFERENCE_FRAME rf[2], int row_offset,
                             CANDIDATE_MV *ref_mv_stack, uint8_t *refmv_count) {
Jingning Han's avatar
Jingning Han committed
164
165
166
  const TileInfo *const tile = &xd->tile;
  int i;
  uint8_t newmv_count = 0;
167
168
169
170
171
  const int mi_offset = mi_size_wide[BLOCK_8X8];
#if CONFIG_CB4X4
  // TODO(jingning): Revisit this part after cb4x4 is stable.
  row_offset *= 2;
#endif
Jingning Han's avatar
Jingning Han committed
172
173
174

  for (i = 0; i < xd->n8_w && *refmv_count < MAX_REF_MV_STACK_SIZE;) {
    POSITION mi_pos;
175
176
177
#if CONFIG_CB4X4
    const int use_step_16 = (xd->n8_w >= 16);
#else
178
    const int use_step_16 = (xd->n8_w >= 8);
179
#endif
180

Jingning Han's avatar
Jingning Han committed
181
182
    mi_pos.row = row_offset;
    mi_pos.col = i;
Geza Lore's avatar
Geza Lore committed
183
    if (is_inside(tile, mi_col, mi_row, &mi_pos)) {
Jingning Han's avatar
Jingning Han committed
184
185
186
      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;
187
      int len = AOMMIN(xd->n8_w, mi_size_wide[candidate->sb_type]);
188
      if (use_step_16) len = AOMMAX(2, len);
clang-format's avatar
clang-format committed
189
190
191
      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);
192
193
      i += len;
    } else {
194
      if (use_step_16)
195
        i += (mi_offset << 1);
196
      else
197
        i += mi_offset;
198
199
    }
  }
200
201

  return newmv_count;
202
203
}

Yaowu Xu's avatar
Yaowu Xu committed
204
static uint8_t scan_col_mbmi(const AV1_COMMON *cm, const MACROBLOCKD *xd,
205
                             const int mi_row, const int mi_col, int block,
clang-format's avatar
clang-format committed
206
207
                             const MV_REFERENCE_FRAME rf[2], int col_offset,
                             CANDIDATE_MV *ref_mv_stack, uint8_t *refmv_count) {
208
209
  const TileInfo *const tile = &xd->tile;
  int i;
210
  uint8_t newmv_count = 0;
211
212
213
214
  const int mi_offset = mi_size_wide[BLOCK_8X8];
#if CONFIG_CB4X4
  col_offset *= 2;
#endif
215
216
217

  for (i = 0; i < xd->n8_h && *refmv_count < MAX_REF_MV_STACK_SIZE;) {
    POSITION mi_pos;
218
219
220
#if CONFIG_CB4X4
    const int use_step_16 = (xd->n8_h >= 16);
#else
221
    const int use_step_16 = (xd->n8_h >= 8);
222
#endif
223

224
225
    mi_pos.row = i;
    mi_pos.col = col_offset;
Geza Lore's avatar
Geza Lore committed
226
    if (is_inside(tile, mi_col, mi_row, &mi_pos)) {
227
228
229
      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;
230
      int len = AOMMIN(xd->n8_h, mi_size_high[candidate->sb_type]);
231
      if (use_step_16) len = AOMMAX(2, len);
clang-format's avatar
clang-format committed
232
233
234
      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);
235
236
      i += len;
    } else {
237
      if (use_step_16)
238
        i += (mi_offset << 1);
239
      else
240
        i += mi_offset;
241
242
    }
  }
243
244

  return newmv_count;
245
246
}

Yaowu Xu's avatar
Yaowu Xu committed
247
static uint8_t scan_blk_mbmi(const AV1_COMMON *cm, const MACROBLOCKD *xd,
248
                             const int mi_row, const int mi_col, int block,
clang-format's avatar
clang-format committed
249
250
                             const MV_REFERENCE_FRAME rf[2], int row_offset,
                             int col_offset, CANDIDATE_MV *ref_mv_stack,
251
                             uint8_t *refmv_count) {
252
253
  const TileInfo *const tile = &xd->tile;
  POSITION mi_pos;
254
  uint8_t newmv_count = 0;
255
256
257
258

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

Geza Lore's avatar
Geza Lore committed
259
  if (is_inside(tile, mi_col, mi_row, &mi_pos) &&
260
261
262
263
      *refmv_count < MAX_REF_MV_STACK_SIZE) {
    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;
264
    const int len = mi_size_wide[BLOCK_8X8];
265

clang-format's avatar
clang-format committed
266
267
268
    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);
269
  }  // Analyze a single 8x8 block motion information.
Yaowu Xu's avatar
Yaowu Xu committed
270

271
  return newmv_count;
272
273
}

clang-format's avatar
clang-format committed
274
275
static int has_top_right(const MACROBLOCKD *xd, int mi_row, int mi_col,
                         int bs) {
276
277
278
  const int mask_row = mi_row & MAX_MIB_MASK;
  const int mask_col = mi_col & MAX_MIB_MASK;

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

282
283
  // bs > 0 and bs is a power of 2
  assert(bs > 0 && !(bs & (bs - 1)));
284

285
286
287
  // 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
288
  while (bs < MAX_MIB_SIZE) {
289
290
    if (mask_col & bs) {
      if ((mask_col & (2 * bs)) && (mask_row & (2 * bs))) {
291
        has_tr = 0;
292
293
294
295
296
297
298
        break;
      }
    } else {
      break;
    }
    bs <<= 1;
  }
299

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

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

310
311
312
313
#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)
314
    if ((mask_row & bs) && !(mask_col & bs)) has_tr = 0;
315
316
#endif  // CONFIG_EXT_PARTITION_TYPES

317
318
319
  return has_tr;
}

320
321
322
323
324
325
326
327
328
329
330
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;
331
  const int weight_unit = mi_size_wide[BLOCK_8X8];
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

  mi_pos.row = blk_row;
  mi_pos.col = blk_col;

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

  for (ref = 0; ref < 2; ++ref) {
    if (prev_frame_mvs->ref_frame[ref] == ref_frame) {
      int_mv this_refmv = prev_frame_mvs->mv[ref];
      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 (this_refmv.as_int == ref_mv_stack[idx].this_mv.as_int) break;

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

      if (idx == *refmv_count && *refmv_count < MAX_REF_MV_STACK_SIZE) {
        ref_mv_stack[idx].this_mv.as_int = this_refmv.as_int;
353
354
        ref_mv_stack[idx].pred_diff[0] =
            av1_get_pred_diff_ctx(prev_frame_mvs->pred_mv[ref], this_refmv);
355
        ref_mv_stack[idx].weight = 2 * weight_unit;
356
357
358
359
360
361
362
363
364
365
        ++(*refmv_count);
      }

      ++coll_blk_count;
    }
  }

  return coll_blk_count;
}

Yaowu Xu's avatar
Yaowu Xu committed
366
static void setup_ref_mv_list(const AV1_COMMON *cm, const MACROBLOCKD *xd,
367
                              MV_REFERENCE_FRAME ref_frame,
clang-format's avatar
clang-format committed
368
369
370
                              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) {
371
  int idx, nearest_refmv_count = 0;
372
  uint8_t newmv_count = 0;
373
374
375
  CANDIDATE_MV tmp_mv;
  int len, nr_len;

clang-format's avatar
clang-format committed
376
377
378
379
  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;
380

381
382
  const int bs = AOMMAX(xd->n8_w, xd->n8_h);
  const int has_tr = has_top_right(xd, mi_row, mi_col, bs);
Jingning Han's avatar
Jingning Han committed
383
384
  MV_REFERENCE_FRAME rf[2];

385
  av1_set_ref_frame(rf, ref_frame);
386
  mode_context[ref_frame] = 0;
387
  *refmv_count = 0;
388
389

  // Scan the first above row mode info.
390
391
  newmv_count += scan_row_mbmi(cm, xd, mi_row, mi_col, block, rf, -1,
                               ref_mv_stack, refmv_count);
392
  // Scan the first left column mode info.
clang-format's avatar
clang-format committed
393
394
  newmv_count += scan_col_mbmi(cm, xd, mi_row, mi_col, block, rf, -1,
                               ref_mv_stack, refmv_count);
395

396
397
  // Check top-right boundary
  if (has_tr)
398
399
    newmv_count += scan_blk_mbmi(cm, xd, mi_row, mi_col, block, rf, -1,
                                 xd->n8_w, ref_mv_stack, refmv_count);
400

401
  nearest_refmv_count = *refmv_count;
402

403
  for (idx = 0; idx < nearest_refmv_count; ++idx)
404
405
    ref_mv_stack[idx].weight += REF_CAT_LEVEL;

clang-format's avatar
clang-format committed
406
407
  if (prev_frame_mvs_base && cm->show_frame && cm->last_show_frame &&
      rf[1] == NONE) {
408
    int blk_row, blk_col;
409
    int coll_blk_count = 0;
410
    const int mi_step = mi_size_wide[BLOCK_16X16];
411

412
413
    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) {
414
415
416
        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);
417
418
      }
    }
419
420
    if (coll_blk_count == 0) mode_context[ref_frame] |= (1 << ZEROMV_OFFSET);
  } else {
421
    mode_context[ref_frame] |= (1 << ZEROMV_OFFSET);
422
  }
423

424
  // Scan the second outer area.
Deng's avatar
Deng committed
425
426
427
428
429
430
431
432
433
434
  scan_blk_mbmi(cm, xd, mi_row, mi_col, block, rf, -1, -1, ref_mv_stack,
                refmv_count);
  for (idx = 2; idx <= 3; ++idx) {
    scan_row_mbmi(cm, xd, mi_row, mi_col, block, rf, -idx, ref_mv_stack,
                  refmv_count);
    scan_col_mbmi(cm, xd, mi_row, mi_col, block, rf, -idx, ref_mv_stack,
                  refmv_count);
  }
  scan_col_mbmi(cm, xd, mi_row, mi_col, block, rf, -4, ref_mv_stack,
                refmv_count);
435

436
437
438
  switch (nearest_refmv_count) {
    case 0:
      mode_context[ref_frame] |= 0;
clang-format's avatar
clang-format committed
439
      if (*refmv_count >= 1) mode_context[ref_frame] |= 1;
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

      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;
  }

468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
  // 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;
  }

483
  len = *refmv_count;
484
485
486
487
488
489
490
491
492
493
494
495
496
  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;
  }

497
498
  if (rf[1] > NONE) {
    for (idx = 0; idx < *refmv_count; ++idx) {
499
500
501
502
      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);
503
504
    }
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
505
    for (idx = 0; idx < AOMMIN(MAX_MV_REF_CANDIDATES, *refmv_count); ++idx) {
506
      mv_ref_list[idx].as_int = ref_mv_stack[idx].this_mv.as_int;
507
508
      clamp_mv_ref(&mv_ref_list[idx].as_mv, xd->n8_w << MI_SIZE_LOG2,
                   xd->n8_h << MI_SIZE_LOG2, xd);
509
    }
510
511
512
513
  }
}
#endif

Jingning Han's avatar
Jingning Han committed
514
515
// This function searches the neighbourhood of a given MB/SB
// to try and find candidate reference vectors.
Yaowu Xu's avatar
Yaowu Xu committed
516
static void find_mv_refs_idx(const AV1_COMMON *cm, const MACROBLOCKD *xd,
Jingning Han's avatar
Jingning Han committed
517
                             MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
clang-format's avatar
clang-format committed
518
519
                             int_mv *mv_ref_list, int block, int mi_row,
                             int mi_col, find_mv_refs_sync sync,
520
521
                             void *const data, int16_t *mode_context,
                             int_mv zeromv) {
Jingning Han's avatar
Jingning Han committed
522
523
  const int *ref_sign_bias = cm->ref_frame_sign_bias;
  int i, refmv_count = 0;
Yaowu Xu's avatar
Yaowu Xu committed
524
#if !CONFIG_REF_MV
Jingning Han's avatar
Jingning Han committed
525
  const POSITION *const mv_ref_search = mv_ref_blocks[mi->mbmi.sb_type];
Deng's avatar
Deng committed
526
#endif
Jingning Han's avatar
Jingning Han committed
527
528
  int different_ref_found = 0;
  int context_counter = 0;
clang-format's avatar
clang-format committed
529
530
531
532
  const MV_REF *const prev_frame_mvs =
      cm->use_prev_frame_mvs
          ? cm->prev_frame->mvs + mi_row * cm->mi_cols + mi_col
          : NULL;
Jingning Han's avatar
Jingning Han committed
533
  const TileInfo *const tile = &xd->tile;
534
535
536
  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)];
Yaowu Xu's avatar
Yaowu Xu committed
537
#if CONFIG_REF_MV
Deng's avatar
Deng committed
538
  POSITION mv_ref_search[MVREF_NEIGHBOURS];
539
540
  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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  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;
  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;
  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;
559
560
561
562
563
564
565
566

#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
#endif  // CONFIG_REF_MV
Jingning Han's avatar
Jingning Han committed
567
568
569
570
571
572

  // 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];
Geza Lore's avatar
Geza Lore committed
573
    if (is_inside(tile, mi_col, mi_row, mv_ref)) {
clang-format's avatar
clang-format committed
574
575
      const MODE_INFO *const candidate_mi =
          xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
Jingning Han's avatar
Jingning Han committed
576
577
578
579
580
581
582
      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),
583
                        refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
584
585
      else if (candidate->ref_frame[1] == ref_frame)
        ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 1, mv_ref->col, block),
586
                        refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
587
588
589
590
591
592
593
594
    }
  }

  // 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];
Geza Lore's avatar
Geza Lore committed
595
    if (is_inside(tile, mi_col, mi_row, mv_ref)) {
clang-format's avatar
clang-format committed
596
597
      const MB_MODE_INFO *const candidate =
          &xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride]->mbmi;
Yaowu Xu's avatar
Yaowu Xu committed
598
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
599
      if (candidate == NULL) continue;
600
601
      if ((mi_row % MAX_MIB_SIZE) + mv_ref->row >= MAX_MIB_SIZE ||
          (mi_col % MAX_MIB_SIZE) + mv_ref->col >= MAX_MIB_SIZE)
Deng's avatar
Deng committed
602
603
        continue;
#endif
Jingning Han's avatar
Jingning Han committed
604
605
606
      different_ref_found = 1;

      if (candidate->ref_frame[0] == ref_frame)
clang-format's avatar
clang-format committed
607
608
        ADD_MV_REF_LIST(candidate->mv[0], refmv_count, mv_ref_list, bw, bh, xd,
                        Done);
Jingning Han's avatar
Jingning Han committed
609
      else if (candidate->ref_frame[1] == ref_frame)
clang-format's avatar
clang-format committed
610
611
        ADD_MV_REF_LIST(candidate->mv[1], refmv_count, mv_ref_list, bw, bh, xd,
                        Done);
Jingning Han's avatar
Jingning Han committed
612
613
614
    }
  }

clang-format's avatar
clang-format committed
615
616
617
618
// 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
619
#if defined(_WIN32) && !HAVE_PTHREAD_H
clang-format's avatar
clang-format committed
620
621
622
  if (cm->frame_parallel_decode && sync != NULL) {
    sync(data, mi_row);
  }
Jingning Han's avatar
Jingning Han committed
623
624
625
626
627
628
629
630
631
632
#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
633
634
      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
635
    } else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
clang-format's avatar
clang-format committed
636
637
      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
638
639
640
641
642
643
644
645
646
    }
  }

  // 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];
Geza Lore's avatar
Geza Lore committed
647
      if (is_inside(tile, mi_col, mi_row, mv_ref)) {
clang-format's avatar
clang-format committed
648
649
        const MB_MODE_INFO *const candidate =
            &xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride]->mbmi;
Yaowu Xu's avatar
Yaowu Xu committed
650
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
651
        if (candidate == NULL) continue;
652
653
        if ((mi_row % MAX_MIB_SIZE) + mv_ref->row >= MAX_MIB_SIZE ||
            (mi_col % MAX_MIB_SIZE) + mv_ref->col >= MAX_MIB_SIZE)
Deng's avatar
Deng committed
654
655
          continue;
#endif
Jingning Han's avatar
Jingning Han committed
656
657
658

        // 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,
659
                                 refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
660
661
662
663
664
665
666
667
668
669
670
671
672
673
      }
    }
  }

  // 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;
      }
674
      ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
675
676
677
    }

    if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
678
        prev_frame_mvs->ref_frame[1] != ref_frame) {
Jingning Han's avatar
Jingning Han committed
679
680
681
682
683
684
      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;
      }
685
      ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
686
687
688
    }
  }

689
690
691
Done:
  if (mode_context)
    mode_context[ref_frame] = counter_to_context[context_counter];
692
  for (i = refmv_count; i < MAX_MV_REF_CANDIDATES; ++i)
693
    mv_ref_list[i].as_int = zeromv.as_int;
Jingning Han's avatar
Jingning Han committed
694
695
}

Yue Chen's avatar
Yue Chen committed
696
697
#if CONFIG_EXT_INTER
// This function keeps a mode count for a given MB/SB
Yaowu Xu's avatar
Yaowu Xu committed
698
699
700
701
void av1_update_mv_context(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
702
  int i, refmv_count = 0;
Yaowu Xu's avatar
Yaowu Xu committed
703
#if !CONFIG_REF_MV
Yue Chen's avatar
Yue Chen committed
704
  const POSITION *const mv_ref_search = mv_ref_blocks[mi->mbmi.sb_type];
705
#endif
Yue Chen's avatar
Yue Chen committed
706
707
708
709
  int context_counter = 0;
  const int bw = num_8x8_blocks_wide_lookup[mi->mbmi.sb_type] << 3;
  const int bh = num_8x8_blocks_high_lookup[mi->mbmi.sb_type] << 3;
  const TileInfo *const tile = &xd->tile;
Yaowu Xu's avatar
Yaowu Xu committed
710
#if CONFIG_REF_MV
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
  POSITION mv_ref_search[MVREF_NEIGHBOURS];
  const int num_8x8_blocks_wide = bw >> 3;
  const int num_8x8_blocks_high = bh >> 3;
  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;
  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;
  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;
#endif
Yue Chen's avatar
Yue Chen committed
733
734
735
736
737
738
739
740

  // Blank the reference vector list
  memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);

  // The nearest 2 blocks are examined only.
  // If the size < 8x8, we get the mv from the bmi substructure;
  for (i = 0; i < 2; ++i) {
    const POSITION *const mv_ref = &mv_ref_search[i];
Geza Lore's avatar
Geza Lore committed
741
    if (is_inside(tile, mi_col, mi_row, mv_ref)) {
Yue Chen's avatar
Yue Chen committed
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
      const MODE_INFO *const candidate_mi =
          xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
      const MB_MODE_INFO *const candidate = &candidate_mi->mbmi;

      // Keep counts for entropy encoding.
      context_counter += mode_2_counter[candidate->mode];

      if (candidate->ref_frame[0] == ref_frame) {
        ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, block),
                        refmv_count, mv_ref_list, bw, bh, xd, Done);
      } else if (candidate->ref_frame[1] == ref_frame) {
        ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 1, mv_ref->col, block),
                        refmv_count, mv_ref_list, bw, bh, xd, Done);
      }
    }
  }

clang-format's avatar
clang-format committed
759
Done:
Yue Chen's avatar
Yue Chen committed
760
761
762
763
764
765

  if (mode_context)
    mode_context[ref_frame] = counter_to_context[context_counter];
}
#endif  // CONFIG_EXT_INTER

Yaowu Xu's avatar
Yaowu Xu committed
766
767
void av1_find_mv_refs(const AV1_COMMON *cm, const MACROBLOCKD *xd,
                      MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
768
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
769
                      uint8_t *ref_mv_count, CANDIDATE_MV *ref_mv_stack,
770
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
771
                      int16_t *compound_mode_context,
772
#endif  // CONFIG_EXT_INTER
773
#endif
Yaowu Xu's avatar
Yaowu Xu committed
774
775
776
                      int_mv *mv_ref_list, int mi_row, int mi_col,
                      find_mv_refs_sync sync, void *const data,
                      int16_t *mode_context) {
777
  int_mv zeromv[2];
778
#if CONFIG_REF_MV
779
780
781
782
  int idx, all_zero = 1;
#endif
#if CONFIG_GLOBAL_MOTION
  MV_REFERENCE_FRAME rf[2];
783
#endif
Yue Chen's avatar
Yue Chen committed
784
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
785
  av1_update_mv_context(xd, mi, ref_frame, mv_ref_list, -1, mi_row, mi_col,
786
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
787
                        compound_mode_context);
788
#else
Yaowu Xu's avatar
Yaowu Xu committed
789
                        mode_context);
790
#endif  // CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
791
#endif  // CONFIG_EXT_INTER
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806

#if CONFIG_GLOBAL_MOTION
  av1_set_ref_frame(rf, ref_frame);
  zeromv[0].as_int = gm_get_motion_vector(&cm->global_motion[rf[0]],
                                          cm->allow_high_precision_mv)
                         .as_int;
  zeromv[1].as_int = (rf[1] != NONE)
                         ? gm_get_motion_vector(&cm->global_motion[rf[1]],
                                                cm->allow_high_precision_mv)
                               .as_int
                         : 0;
#else
  zeromv[0].as_int = zeromv[1].as_int = 0;
#endif

Yaowu Xu's avatar
Yaowu Xu committed
807
808
#if CONFIG_REF_MV
  if (ref_frame <= ALTREF_FRAME)
809
#endif  // CONFIG_REF_MV
810
811
    find_mv_refs_idx(cm, xd, mi, ref_frame, mv_ref_list, -1, mi_row, mi_col,
                     sync, data, mode_context, zeromv[0]);
812
813

#if CONFIG_REF_MV
clang-format's avatar
clang-format committed
814
815
  setup_ref_mv_list(cm, xd, ref_frame, ref_mv_count, ref_mv_stack, mv_ref_list,
                    -1, mi_row, mi_col, mode_context);
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
  /* Note: If global motion is enabled, then we want to set the ALL_ZERO flag
     iff all of the MVs we could generate with NEARMV/NEARESTMV are equivalent
     to the global motion vector.
     Note: For the following to work properly, the encoder can't throw away
     any global motion models after calling this function, even if they are
     unused. Instead we rely on the recode loop: If any non-IDENTITY model
     is unused, the whole frame will be re-encoded without it.
     The problem is that, otherwise, we can end up in the following situation:
     * Encoder has a global motion model with nonzero translational part,
       and all candidate MVs are zero. So the ALL_ZERO flag is unset.
     * Encoder throws away global motion because it is never used.
     * Decoder sees that there is no global motion and all candidate MVs are
       zero, so sets the ALL_ZERO flag.
     * This leads to an encode/decode mismatch.
  */
Yaowu Xu's avatar
Yaowu Xu committed
831
832
  if (*ref_mv_count >= 2) {
    for (idx = 0; idx < AOMMIN(3, *ref_mv_count); ++idx) {
833
      if (ref_mv_stack[idx].this_mv.as_int != zeromv[0].as_int) all_zero = 0;
Yaowu Xu's avatar
Yaowu Xu committed
834
      if (ref_frame > ALTREF_FRAME)
835
        if (ref_mv_stack[idx].comp_mv.as_int != zeromv[1].as_int) all_zero = 0;
Yaowu Xu's avatar
Yaowu Xu committed
836
837
838
    }
  } else if (ref_frame <= ALTREF_FRAME) {
    for (idx = 0; idx < MAX_MV_REF_CANDIDATES; ++idx)
839
      if (mv_ref_list[idx].as_int != zeromv[0].as_int) all_zero = 0;
Yaowu Xu's avatar
Yaowu Xu committed
840
  }
841

clang-format's avatar
clang-format committed
842
  if (all_zero) mode_context[ref_frame] |= (1 << ALL_ZERO_FLAG_OFFSET);
843
#endif
Jingning Han's avatar
Jingning Han committed
844
845
}

Yaowu Xu's avatar
Yaowu Xu committed
846
847
void av1_find_best_ref_mvs(int allow_hp, int_mv *mvlist, int_mv *nearest_mv,
                           int_mv *near_mv) {
Jingning Han's avatar
Jingning Han committed
848
849
850
851
852
853
854
855
856
  int i;
  // Make sure all the candidates are properly clamped etc
  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
    lower_mv_precision(&mvlist[i].as_mv, allow_hp);
  }
  *nearest_mv = mvlist[0];
  *near_mv = mvlist[1];
}

857
858
void av1_append_sub8x8_mvs_for_idx(const AV1_COMMON *cm, MACROBLOCKD *xd,
                                   int block, int ref, int mi_row, int mi_col,
859
#if CONFIG_REF_MV
Yaowu Xu's avatar
Yaowu Xu committed
860
861
                                   CANDIDATE_MV *ref_mv_stack,
                                   uint8_t *ref_mv_count,
862
#endif
Yue Chen's avatar
Yue Chen committed
863
#if CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
864
                                   int_mv *mv_list,
Yue Chen's avatar
Yue Chen committed
865
#endif  // CONFIG_EXT_INTER
Yaowu Xu's avatar
Yaowu Xu committed
866
                                   int_mv *nearest_mv, int_mv *near_mv) {
Yue Chen's avatar
Yue Chen committed
867
#if !CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
868
  int_mv mv_list[MAX_MV_REF_CANDIDATES];
Yue Chen's avatar
Yue Chen committed
869
#endif  // !CONFIG_EXT_INTER
Jingning Han's avatar
Jingning Han committed
870
871
872
  MODE_INFO *const mi = xd->mi[0];
  b_mode_info *bmi = mi->bmi;
  int n;
873
  int_mv zeromv;
874
875
#if CONFIG_REF_MV
  CANDIDATE_MV tmp_mv;
876
  uint8_t idx;
877
  uint8_t above_count = 0, left_count = 0;
Jingning Han's avatar
Jingning Han committed
878
  MV_REFERENCE_FRAME rf[2] = { mi->mbmi.ref_frame[ref], NONE };
879
  *ref_mv_count = 0;
880
#endif
Jingning Han's avatar
Jingning Han committed
881
882
883

  assert(MAX_MV_REF_CANDIDATES == 2);

884
885
886
887
888
889
890
#if CONFIG_GLOBAL_MOTION
  zeromv.as_int =
      gm_get_motion_vector(&cm->global_motion[ref], cm->allow_high_precision_mv)
          .as_int;
#else
  zeromv.as_int = 0;
#endif
clang-format's avatar
clang-format committed
891
  find_mv_refs_idx(cm, xd, mi, mi->mbmi.ref_frame[ref], mv_list, block, mi_row,
892
                   mi_col, NULL, NULL, NULL, zeromv);
Jingning Han's avatar
Jingning Han committed
893

894
#if CONFIG_REF_MV
clang-format's avatar
clang-format committed
895
896
  scan_blk_mbmi(cm, xd, mi_row, mi_col, block, rf, -1, 0, ref_mv_stack,
                ref_mv_count);
897
  above_count = *ref_mv_count;
898

clang-format's avatar
clang-format committed
899
900
  scan_blk_mbmi(cm, xd, mi_row, mi_col, block, rf, 0, -1, ref_mv_stack,
                ref_mv_count);
901
  left_count = *ref_mv_count - above_count;
902
903
904
905
906
907
908

  if (above_count > 1 && left_count > 0) {
    tmp_mv = ref_mv_stack[1];
    ref_mv_stack[1] = ref_mv_stack[above_count];
    ref_mv_stack[above_count] = tmp_mv;
  }

909
  for (idx = 0; idx < *ref_mv_count; ++idx)
910
911
    clamp_mv_ref(&ref_mv_stack[idx].this_mv.as_mv, xd->n8_w << MI_SIZE_LOG2,
                 xd->n8_h << MI_SIZE_LOG2, xd);
912

Yaowu Xu's avatar
Yaowu Xu committed
913
  for (idx = 0; idx < AOMMIN(MAX_MV_REF_CANDIDATES, *ref_mv_count); ++idx)
914
    mv_list[idx].as_int = ref_mv_stack[idx].this_mv.as_int;
915
916
#endif

Jingning Han's avatar
Jingning Han committed
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
  near_mv->as_int = 0;
  switch (block) {
    case 0:
      nearest_mv->as_int = mv_list[0].as_int;
      near_mv->as_int = mv_list[1].as_int;
      break;
    case 1:
    case 2:
      nearest_mv->as_int = bmi[0].as_mv[ref].as_int;
      for (n = 0; n < MAX_MV_REF_CANDIDATES; ++n)
        if (nearest_mv->as_int != mv_list[n].as_int) {
          near_mv->as_int = mv_list[n].as_int;
          break;
        }
      break;
    case 3: {
      int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
      candidates[0] = bmi[1].as_mv[ref];
      candidates[1] = bmi[0].as_mv[ref];
      candidates[2] = mv_list[0];
      candidates[3] = mv_list[1];

      nearest_mv->as_int = bmi[2].as_mv[ref].as_int;
      for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n)
        if (nearest_mv->as_int != candidates[n].as_int) {
          near_mv->as_int = candidates[n].as_int;
          break;
        }
      break;
    }
clang-format's avatar
clang-format committed
947
    default: assert(0 && "Invalid block index.");
Jingning Han's avatar
Jingning Han committed
948
949
  }
}
Yue Chen's avatar
Yue Chen committed
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976

#if CONFIG_WARPED_MOTION
int findSamples(const AV1_COMMON *cm, MACROBLOCKD *xd, int mi_row, int mi_col,
                double *pts, double *pts_inref) {
  MB_MODE_INFO *const mbmi0 = &(xd->mi[0]->mbmi);
  int ref_frame = mbmi0->ref_frame[0];
  int up_available = xd->up_available;
  int left_available = xd->left_available;
  int i, mi_step, np = 0;
  int mvasint[100];
  int mvnumber = 0;
  int global_offset_c = mi_col * 8;
  int global_offset_r = mi_row * 8;
  int samples_per_neighbor = 4;

  // scan the above row
  if (up_available) {
    for (i = 0; i < AOMMIN(xd->n8_w, cm->mi_cols - mi_col); i += mi_step) {
      int mi_row_offset = -1;
      int mi_col_offset = i;

      MODE_INFO *mi = xd->mi[mi_col_offset + mi_row_offset * xd->mi_stride];
      MB_MODE_INFO *mbmi = &mi->mbmi;

      mi_step = AOMMIN(xd->n8_w, num_8x8_blocks_wide_lookup[mbmi->sb_type]);

      if (mbmi->ref_frame[0] == ref_frame && mbmi->ref_frame[1] == NONE) {
977
978
        int bw = block_size_wide[mbmi->sb_type];
        int bh = block_size_high[mbmi->sb_type];
Yue Chen's avatar
Yue Chen committed
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
        int mv_row = mbmi->mv[0].as_mv.row;
        int mv_col = mbmi->mv[0].as_mv.col;
        int cr_offset = -AOMMAX(bh, 8) / 2 - 1;
        int cc_offset = i * 8 + AOMMAX(bw, 8) / 2 - 1;
        int j;
        int pixelperblock = samples_per_neighbor;

        mvasint[mvnumber] = mbmi->mv[0].as_int;
        mvnumber++;

        for (j = 0; j < pixelperblock; j++) {
          int r_offset = j / 2;
          int c_offset = j % 2;

          pts[0] = (double)(cc_offset + c_offset + global_offset_c);
          pts[1] = (double)(cr_offset + r_offset + global_offset_r);

          if (mbmi->motion_mode == WARPED_CAUSAL) {
            int ipts[2], ipts_inref[2];
            ipts[0] = cc_offset + c_offset + global_offset_c;
            ipts[1] = cr_offset + r_offset + global_offset_r;

For faster browsing, not all history is shown. View entire blame