mvref_common.c 25.9 KB
Newer Older
Jingning Han's avatar
Jingning Han committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Jingning Han's avatar
Jingning Han committed
3
 *
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
 */
Yaowu Xu's avatar
Yaowu Xu committed
11
#include "av1/common/mvref_common.h"
Jingning Han's avatar
Jingning Han committed
12

13
#if CONFIG_REF_MV
14 15
static uint8_t add_ref_mv_candidate(const MACROBLOCKD *xd,
                                    const MODE_INFO *const candidate_mi,
16 17 18 19 20 21 22 23 24 25 26 27 28 29
                                    const MB_MODE_INFO *const candidate,
                                    const MV_REFERENCE_FRAME rf[2],
                                    uint8_t *refmv_count,
                                    CANDIDATE_MV *ref_mv_stack,
                                    int len, int block, int col) {
  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]) {
        int_mv this_refmv =
            get_sub_block_mv(candidate_mi, ref, col, block);
30 31 32
        clamp_mv_ref(&this_refmv.as_mv,
                     xd->n8_w << 3, xd->n8_h << 3, xd);

33 34 35
        for (index = 0; index < *refmv_count; ++index)
          if (ref_mv_stack[index].this_mv.as_int == this_refmv.as_int)
            break;
36

37 38
        if (index < *refmv_count)
          ref_mv_stack[index].weight += 2 * len;
39

40 41 42
        // Add a new item to the list.
        if (index == *refmv_count) {
          ref_mv_stack[index].this_mv = this_refmv;
43 44
          ref_mv_stack[index].pred_mv =
              get_sub_block_pred_mv(candidate_mi, ref, col, block);
45 46 47 48 49 50
          ref_mv_stack[index].weight = 2 * len;
          ++(*refmv_count);

          if (candidate->mode == NEWMV)
            ++newmv_count;
        }
51

52 53 54 55
        if (candidate_mi->mbmi.sb_type < BLOCK_8X8 && block >= 0) {
          int alt_block = 3 - block;
          this_refmv =
              get_sub_block_mv(candidate_mi, ref, col, alt_block);
56 57 58
          clamp_mv_ref(&this_refmv.as_mv,
                       xd->n8_w << 3, xd->n8_h << 3, xd);

59 60 61 62 63 64 65 66 67 68
          for (index = 0; index < *refmv_count; ++index)
            if (ref_mv_stack[index].this_mv.as_int == this_refmv.as_int)
              break;

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

          // Add a new item to the list.
          if (index == *refmv_count) {
            ref_mv_stack[index].this_mv = this_refmv;
69 70
            ref_mv_stack[index].pred_mv =
                get_sub_block_pred_mv(candidate_mi, ref, col, alt_block);
71 72
            ref_mv_stack[index].weight = len;
            ++(*refmv_count);
73

74
            if (candidate->mode == NEWMV)
75
              ++newmv_count;
76 77 78
          }
        }
      }
79 80 81 82 83 84 85 86 87 88
    }
  } else {
    // compound reference frame
    if (candidate->ref_frame[0] == rf[0] &&
        candidate->ref_frame[1] == rf[1]) {
      int_mv this_refmv[2] = {
          get_sub_block_mv(candidate_mi, 0, col, block),
          get_sub_block_mv(candidate_mi, 1, col, block)
      };

89 90 91 92
      for (ref = 0; ref < 2; ++ref)
        clamp_mv_ref(&this_refmv[ref].as_mv,
                     xd->n8_w << 3, xd->n8_h << 3, xd);

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
      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;

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

      // 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];
        ref_mv_stack[index].weight = 2 * len;
        ++(*refmv_count);

        if (candidate->mode == NEWMV)
          ++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);

117 118 119 120
        for (ref = 0; ref < 2; ++ref)
          clamp_mv_ref(&this_refmv[ref].as_mv,
                       xd->n8_w << 3, xd->n8_h << 3, xd);

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        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;

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

        // 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];
          ref_mv_stack[index].weight = len;
          ++(*refmv_count);

          if (candidate->mode == NEWMV)
            ++newmv_count;
        }
      }
    }
  }
  return newmv_count;
}

static uint8_t scan_row_mbmi(const AV1_COMMON *cm, const MACROBLOCKD *xd,
                             const int mi_row, const int mi_col, int block,
                             const MV_REFERENCE_FRAME rf[2],
                             int row_offset,
                             CANDIDATE_MV *ref_mv_stack,
                             uint8_t *refmv_count) {
  const TileInfo *const tile = &xd->tile;
  int i;
  uint8_t newmv_count = 0;

  for (i = 0; i < xd->n8_w && *refmv_count < MAX_REF_MV_STACK_SIZE;) {
    POSITION mi_pos;
    mi_pos.row = row_offset;
    mi_pos.col = i;

    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, &mi_pos)) {
      const MODE_INFO *const candidate_mi =
          xd->mi[mi_pos.row * xd->mi_stride + mi_pos.col];
      const MB_MODE_INFO *const candidate_mbmi = &candidate_mi->mbmi;
      const int len = AOMMIN(xd->n8_w,
                        num_8x8_blocks_wide_lookup[candidate_mbmi->sb_type]);
166
      newmv_count += add_ref_mv_candidate(xd, candidate_mi, candidate_mbmi, rf,
167 168
                                          refmv_count, ref_mv_stack, len,
                                          block, mi_pos.col);
169 170 171 172 173
      i += len;
    } else {
      ++i;
    }
  }
174
  return newmv_count;
175 176
}

177 178
static uint8_t scan_col_mbmi(const AV1_COMMON *cm, const MACROBLOCKD *xd,
                             const int mi_row, const int mi_col, int block,
179
                             const MV_REFERENCE_FRAME rf[2],
180 181 182
                             int col_offset,
                             CANDIDATE_MV *ref_mv_stack,
                             uint8_t *refmv_count) {
183 184
  const TileInfo *const tile = &xd->tile;
  int i;
185
  uint8_t newmv_count = 0;
186 187 188 189 190 191 192 193 194 195 196 197

  for (i = 0; i < xd->n8_h && *refmv_count < MAX_REF_MV_STACK_SIZE;) {
    POSITION mi_pos;
    mi_pos.row = i;
    mi_pos.col = col_offset;

    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, &mi_pos)) {
      const MODE_INFO *const candidate_mi =
          xd->mi[mi_pos.row * xd->mi_stride + mi_pos.col];
      const MB_MODE_INFO *const candidate_mbmi = &candidate_mi->mbmi;
      const int len = AOMMIN(xd->n8_h,
                        num_8x8_blocks_high_lookup[candidate_mbmi->sb_type]);
198
      newmv_count += add_ref_mv_candidate(xd, candidate_mi, candidate_mbmi, rf,
199 200
                                          refmv_count, ref_mv_stack, len,
                                          block, mi_pos.col);
201 202 203 204 205
      i += len;
    } else {
      ++i;
    }
  }
206
  return newmv_count;
207 208
}

209 210
static uint8_t scan_blk_mbmi(const AV1_COMMON *cm, const MACROBLOCKD *xd,
                             const int mi_row, const int mi_col, int block,
211
                             const MV_REFERENCE_FRAME rf[2],
212 213 214
                             int row_offset, int col_offset,
                             CANDIDATE_MV *ref_mv_stack,
                             uint8_t *refmv_count) {
215 216
  const TileInfo *const tile = &xd->tile;
  POSITION mi_pos;
217
  uint8_t newmv_count = 0;
218 219 220 221 222 223 224 225 226 227

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

  if (is_inside(tile, mi_col, mi_row, cm->mi_rows, &mi_pos) &&
      *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_mbmi = &candidate_mi->mbmi;
    const int len = 1;
228
    newmv_count += add_ref_mv_candidate(xd, candidate_mi, candidate_mbmi, rf,
229 230
                                        refmv_count, ref_mv_stack, len,
                                        block, mi_pos.col);
231
  }  // Analyze a single 8x8 block motion information.
232
  return newmv_count;
233 234
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
static int has_top_right(const MACROBLOCKD *xd,
                         int mi_row, int mi_col, int bs) {
  int has_tr = !((mi_row & bs) & (bs * 2 - 1)) ||
               !((mi_col & bs) & (bs * 2 - 1));

  // Filter out partial right-most boundaries
  if ((mi_col & bs) & (bs * 2 - 1)) {
    if (((mi_col & (2 * bs)) & (bs * 4 - 1)) &&
        ((mi_row & (2 * bs)) & (bs * 4 - 1)))
      has_tr = 0;
  }

  if (has_tr)
    if (((mi_col + xd->n8_w) & 0x07) == 0)
      if ((mi_row & 0x07) > 0)
        has_tr = 0;

252 253
  if (xd->n8_w < xd->n8_h)
    if (!xd->is_sec_rect)
254 255
      has_tr = 1;

256 257
  if (xd->n8_w > xd->n8_h)
    if (xd->is_sec_rect)
258
      has_tr = 0;
259

260 261 262
  return has_tr;
}

263
static void handle_sec_rect_block(const MB_MODE_INFO * const candidate,
264
                                  uint8_t refmv_count,
265 266 267 268 269 270 271
                                  CANDIDATE_MV *ref_mv_stack,
                                  MV_REFERENCE_FRAME ref_frame,
                                  int16_t *mode_context) {
  int rf, idx;

  for (rf = 0; rf < 2; ++rf) {
    if (candidate->ref_frame[rf] == ref_frame) {
272
      const int list_range = AOMMIN(refmv_count, MAX_MV_REF_CANDIDATES);
273 274 275 276 277 278
      const int_mv pred_mv = candidate->mv[rf];
      for (idx = 0; idx < list_range; ++idx)
        if (pred_mv.as_int == ref_mv_stack[idx].this_mv.as_int)
          break;

      if (idx < list_range) {
279
        if (idx == 0)
280
          mode_context[ref_frame] |= (1 << SKIP_NEARESTMV_OFFSET);
281
        else if (idx == 1)
282 283 284 285 286 287
          mode_context[ref_frame] |= (1 << SKIP_NEARMV_OFFSET);
      }
    }
  }
}

288 289
static void setup_ref_mv_list(const AV1_COMMON *cm, const MACROBLOCKD *xd,
                              MV_REFERENCE_FRAME ref_frame,
290 291
                              uint8_t *refmv_count,
                              CANDIDATE_MV *ref_mv_stack,
292 293
                              int_mv *mv_ref_list,
                              int block, int mi_row, int mi_col,
294
                              int16_t *mode_context) {
295
  int idx, nearest_refmv_count = 0;
296
  uint8_t newmv_count = 0;
297 298
  CANDIDATE_MV tmp_mv;
  int len, nr_len;
299 300 301 302

  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;

303 304
  const int bs = AOMMAX(xd->n8_w, xd->n8_h);
  const int has_tr = has_top_right(xd, mi_row, mi_col, bs);
305
  MV_REFERENCE_FRAME rf[2];
306

307
  av1_set_ref_frame(rf, ref_frame);
308
  mode_context[ref_frame] = 0;
309
  *refmv_count = 0;
310 311

  // Scan the first above row mode info.
312
  newmv_count += scan_row_mbmi(cm, xd, mi_row, mi_col, block, rf,
313
                               -1, ref_mv_stack, refmv_count);
314
  // Scan the first left column mode info.
315
  newmv_count += scan_col_mbmi(cm, xd, mi_row, mi_col, block, rf,
316
                               -1, ref_mv_stack, refmv_count);
317

318 319
  // Check top-right boundary
  if (has_tr)
320
    newmv_count += scan_blk_mbmi(cm, xd, mi_row, mi_col, block, rf,
321
                                 -1, 1, ref_mv_stack, refmv_count);
322

323
  nearest_refmv_count = *refmv_count;
324

325 326 327
  for (idx = 0; idx < nearest_refmv_count; ++idx)
    ref_mv_stack[idx].weight += REF_CAT_LEVEL;

328 329
  if (prev_frame_mvs_base && cm->show_frame && cm->last_show_frame &&
      rf[1] == NONE) {
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    int ref;
    int blk_row, blk_col;
    int coll_blk_count = 0;

    for (blk_row = 0; blk_row < xd->n8_h; ++blk_row) {
      for (blk_col = 0; blk_col < xd->n8_w; ++blk_col) {
        const MV_REF *prev_frame_mvs =
            prev_frame_mvs_base + blk_row * cm->mi_cols + blk_col;

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

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

        for (ref = 0; ref < 2; ++ref) {
          if (prev_frame_mvs->ref_frame[ref] == ref_frame) {
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
            for (idx = 0; idx < *refmv_count; ++idx)
              if (prev_frame_mvs->mv[ref].as_int ==
                  ref_mv_stack[idx].this_mv.as_int)
                break;

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

            if (idx == *refmv_count &&
                *refmv_count < MAX_REF_MV_STACK_SIZE) {
              ref_mv_stack[idx].this_mv.as_int = prev_frame_mvs->mv[ref].as_int;
              ref_mv_stack[idx].weight = 2;
              ++(*refmv_count);

              if (abs(ref_mv_stack[idx].this_mv.as_mv.row) >= 8 ||
                  abs(ref_mv_stack[idx].this_mv.as_mv.col) >= 8)
                mode_context[ref_frame] |= (1 << ZEROMV_OFFSET);
            }

367 368 369 370 371 372 373
            ++coll_blk_count;
          }
        }
      }
    }
    if (coll_blk_count == 0)
      mode_context[ref_frame] |= (1 << ZEROMV_OFFSET);
374 375
  } else {
    mode_context[ref_frame] |= (1 << ZEROMV_OFFSET);
376 377
  }

378 379
  // Scan the second outer area.
  for (idx = 2; idx <= 4; ++idx) {
380
    scan_row_mbmi(cm, xd, mi_row, mi_col, block, rf,
381
                  -idx, ref_mv_stack, refmv_count);
382
    scan_col_mbmi(cm, xd, mi_row, mi_col, block, rf,
383
                  -idx, ref_mv_stack, refmv_count);
384 385
  }

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
  switch (nearest_refmv_count) {
    case 0:
      mode_context[ref_frame] |= 0;
      if (*refmv_count >= 1)
        mode_context[ref_frame] |= 1;

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

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
  // 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;
  }

434
  len = *refmv_count;
435 436 437 438 439 440 441 442 443 444 445 446 447
  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;
  }

448 449 450 451
  if (xd->is_sec_rect) {
    if (xd->n8_w < xd->n8_h) {
      const MODE_INFO *const candidate_mi = xd->mi[-1];
      const MB_MODE_INFO *const candidate_mbmi = &candidate_mi->mbmi;
452
      handle_sec_rect_block(candidate_mbmi, nearest_refmv_count, ref_mv_stack,
453 454 455 456 457 458
                            ref_frame, mode_context);
    }

    if (xd->n8_w > xd->n8_h) {
      const MODE_INFO *const candidate_mi = xd->mi[-xd->mi_stride];
      const MB_MODE_INFO *const candidate_mbmi = &candidate_mi->mbmi;
459
      handle_sec_rect_block(candidate_mbmi, nearest_refmv_count, ref_mv_stack,
460 461 462 463
                            ref_frame, mode_context);
    }
  }

464 465 466 467 468 469 470 471 472 473 474 475 476
  if (rf[1] > NONE) {
    for (idx = 0; idx < *refmv_count; ++idx) {
      clamp_mv_ref(&ref_mv_stack[idx].this_mv.as_mv,
                   xd->n8_w << 3 , xd->n8_h << 3, xd);
      clamp_mv_ref(&ref_mv_stack[idx].comp_mv.as_mv,
                   xd->n8_w << 3 , xd->n8_h << 3, xd);
    }
  } else {
    for (idx = 0; idx < AOMMIN(MAX_MV_REF_CANDIDATES, *refmv_count); ++idx) {
      mv_ref_list[idx].as_int = ref_mv_stack[idx].this_mv.as_int;
      clamp_mv_ref(&mv_ref_list[idx].as_mv,
                   xd->n8_w << 3, xd->n8_h << 3, xd);
    }
477 478 479 480
  }
}
#endif

Jingning Han's avatar
Jingning Han committed
481 482
// This function searches the neighbourhood of a given MB/SB
// to try and find candidate reference vectors.
483
static void find_mv_refs_idx(const AV1_COMMON *cm, const MACROBLOCKD *xd,
Jingning Han's avatar
Jingning Han committed
484
                             MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
clang-format's avatar
clang-format committed
485 486
                             int_mv *mv_ref_list, int block, int mi_row,
                             int mi_col, find_mv_refs_sync sync,
487
                             void *const data, int16_t *mode_context) {
Jingning Han's avatar
Jingning Han committed
488 489 490 491 492
  const int *ref_sign_bias = cm->ref_frame_sign_bias;
  int i, refmv_count = 0;
  const POSITION *const mv_ref_search = mv_ref_blocks[mi->mbmi.sb_type];
  int different_ref_found = 0;
  int context_counter = 0;
clang-format's avatar
clang-format committed
493 494 495 496
  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
497
  const TileInfo *const tile = &xd->tile;
498 499
  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;
Jingning Han's avatar
Jingning Han committed
500

501
#if !CONFIG_MISC_FIXES
Jingning Han's avatar
Jingning Han committed
502 503
  // Blank the reference vector list
  memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
504
#endif
Jingning Han's avatar
Jingning Han committed
505 506 507 508 509 510 511

  // 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];
    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
clang-format's avatar
clang-format committed
512 513
      const MODE_INFO *const candidate_mi =
          xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride];
Jingning Han's avatar
Jingning Han committed
514 515 516 517 518 519 520
      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),
521
                        refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
522 523
      else if (candidate->ref_frame[1] == ref_frame)
        ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 1, mv_ref->col, block),
524
                        refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
525 526 527 528 529 530 531 532 533
    }
  }

  // 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];
    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
clang-format's avatar
clang-format committed
534 535
      const MB_MODE_INFO *const candidate =
          &xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride]->mbmi;
Jingning Han's avatar
Jingning Han committed
536 537 538
      different_ref_found = 1;

      if (candidate->ref_frame[0] == ref_frame)
clang-format's avatar
clang-format committed
539 540
        ADD_MV_REF_LIST(candidate->mv[0], refmv_count, mv_ref_list, bw, bh, xd,
                        Done);
Jingning Han's avatar
Jingning Han committed
541
      else if (candidate->ref_frame[1] == ref_frame)
clang-format's avatar
clang-format committed
542 543
        ADD_MV_REF_LIST(candidate->mv[1], refmv_count, mv_ref_list, bw, bh, xd,
                        Done);
Jingning Han's avatar
Jingning Han committed
544 545 546
    }
  }

clang-format's avatar
clang-format committed
547 548 549 550
// 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
551
#if defined(_WIN32) && !HAVE_PTHREAD_H
clang-format's avatar
clang-format committed
552 553 554
  if (cm->frame_parallel_decode && sync != NULL) {
    sync(data, mi_row);
  }
Jingning Han's avatar
Jingning Han committed
555 556 557 558 559 560 561 562 563 564
#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
565 566
      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
567
    } else if (prev_frame_mvs->ref_frame[1] == ref_frame) {
clang-format's avatar
clang-format committed
568 569
      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
570 571 572 573 574 575 576 577 578 579
    }
  }

  // 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];
      if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
clang-format's avatar
clang-format committed
580 581
        const MB_MODE_INFO *const candidate =
            &xd->mi[mv_ref->col + mv_ref->row * xd->mi_stride]->mbmi;
Jingning Han's avatar
Jingning Han committed
582 583 584

        // 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,
585
                                 refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599
      }
    }
  }

  // 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;
      }
600
      ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
601 602 603
    }

    if (prev_frame_mvs->ref_frame[1] > INTRA_FRAME &&
604 605 606 607
#if !CONFIG_MISC_FIXES
        prev_frame_mvs->mv[1].as_int != prev_frame_mvs->mv[0].as_int &&
#endif
        prev_frame_mvs->ref_frame[1] != ref_frame) {
Jingning Han's avatar
Jingning Han committed
608 609 610 611 612 613
      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;
      }
614
      ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, bw, bh, xd, Done);
Jingning Han's avatar
Jingning Han committed
615 616 617
    }
  }

clang-format's avatar
clang-format committed
618
Done:
619 620
  if (mode_context)
    mode_context[ref_frame] = counter_to_context[context_counter];
Jingning Han's avatar
Jingning Han committed
621

622 623
#if CONFIG_MISC_FIXES
  for (i = refmv_count; i < MAX_MV_REF_CANDIDATES; ++i)
clang-format's avatar
clang-format committed
624
    mv_ref_list[i].as_int = 0;
625
#else
Jingning Han's avatar
Jingning Han committed
626 627
  // Clamp vectors
  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
628
    clamp_mv_ref(&mv_ref_list[i].as_mv, bw, bh, xd);
629
#endif
Jingning Han's avatar
Jingning Han committed
630 631
}

632
void av1_find_mv_refs(const AV1_COMMON *cm, const MACROBLOCKD *xd,
633 634 635 636 637 638 639
                      MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
#if CONFIG_REF_MV
                      uint8_t *ref_mv_count,
                      CANDIDATE_MV *ref_mv_stack,
#endif
                      int_mv *mv_ref_list, int mi_row, int mi_col,
                      find_mv_refs_sync sync, void *const data,
640 641 642 643
                      int16_t *mode_context) {
#if CONFIG_REF_MV
  int idx, all_zero = 1;
#endif
clang-format's avatar
clang-format committed
644 645
  find_mv_refs_idx(cm, xd, mi, ref_frame, mv_ref_list, -1, mi_row, mi_col, sync,
                   data, mode_context);
646 647

#if CONFIG_REF_MV
648 649 650
  setup_ref_mv_list(cm, xd, ref_frame,
                    ref_mv_count, ref_mv_stack,
                    mv_ref_list, -1,
651
                    mi_row, mi_col, mode_context);
652

653 654 655 656 657 658 659 660 661 662
  if (*ref_mv_count >= 2) {
    for (idx = 0; idx < AOMMIN(3, *ref_mv_count); ++idx) {
      if (ref_mv_stack[idx].this_mv.as_int != 0)
        all_zero = 0;
    }
  } else {
    for (idx = 0; idx < MAX_MV_REF_CANDIDATES; ++idx)
      if (mv_ref_list[idx].as_int != 0)
        all_zero = 0;
  }
663 664 665

  if (all_zero)
    mode_context[ref_frame] |= (1 << ALL_ZERO_FLAG_OFFSET);
666
#endif
Jingning Han's avatar
Jingning Han committed
667 668
}

669
void av1_find_best_ref_mvs(int allow_hp, int_mv *mvlist, int_mv *nearest_mv,
clang-format's avatar
clang-format committed
670
                            int_mv *near_mv) {
Jingning Han's avatar
Jingning Han committed
671 672 673 674 675 676 677 678 679
  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];
}

680
void av1_append_sub8x8_mvs_for_idx(AV1_COMMON *cm, MACROBLOCKD *xd, int block,
681 682
                                   int ref, int mi_row, int mi_col,
                                   int_mv *nearest_mv, int_mv *near_mv) {
Jingning Han's avatar
Jingning Han committed
683 684 685 686 687
  int_mv mv_list[MAX_MV_REF_CANDIDATES];
  MODE_INFO *const mi = xd->mi[0];
  b_mode_info *bmi = mi->bmi;
  int n;

688 689 690 691 692
#if CONFIG_REF_MV
  CANDIDATE_MV ref_mv_stack[MAX_REF_MV_STACK_SIZE];
  CANDIDATE_MV tmp_mv;
  uint8_t ref_mv_count = 0, idx;
  uint8_t above_count = 0, left_count = 0;
693
  MV_REFERENCE_FRAME rf[2] = { mi->mbmi.ref_frame[ref], NONE };
694 695
#endif

Jingning Han's avatar
Jingning Han committed
696 697
  assert(MAX_MV_REF_CANDIDATES == 2);

clang-format's avatar
clang-format committed
698
  find_mv_refs_idx(cm, xd, mi, mi->mbmi.ref_frame[ref], mv_list, block, mi_row,
699 700 701
                   mi_col, NULL, NULL, NULL);

#if CONFIG_REF_MV
702
  scan_blk_mbmi(cm, xd, mi_row, mi_col, block, rf,
703 704 705
                -1, 0, ref_mv_stack, &ref_mv_count);
  above_count = ref_mv_count;

706
  scan_blk_mbmi(cm, xd, mi_row, mi_col, block, rf,
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
                0, -1, ref_mv_stack, &ref_mv_count);
  left_count = ref_mv_count - above_count;

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

  for (idx = 0; idx < AOMMIN(MAX_MV_REF_CANDIDATES, ref_mv_count); ++idx) {
    mv_list[idx].as_int = ref_mv_stack[idx].this_mv.as_int;
    clamp_mv_ref(&mv_list[idx].as_mv,
                 xd->n8_w << 3, xd->n8_h << 3, xd);
  }
#endif
Jingning Han's avatar
Jingning Han committed
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752

  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
753
    default: assert(0 && "Invalid block index.");
Jingning Han's avatar
Jingning Han committed
754 755
  }
}