vp9_findnearmv.c 2.67 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9
10
 */

11
#include "vp9/common/vp9_findnearmv.h"
Ronald S. Bultje's avatar
Ronald S. Bultje committed
12
#include "vp9/common/vp9_mvref_common.h"
John Koleszar's avatar
John Koleszar committed
13

Dmitry Kovalev's avatar
Dmitry Kovalev committed
14
15
static void lower_mv_precision(MV *mv, int allow_hp) {
  const int use_hp = allow_hp && vp9_use_mv_hp(mv);
16
  if (!use_hp) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
17
18
19
20
    if (mv->row & 1)
      mv->row += (mv->row > 0 ? -1 : 1);
    if (mv->col & 1)
      mv->col += (mv->col > 0 ? -1 : 1);
21
  }
22
23
24
}


25
26
void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp,
                           int_mv *mvlist, int_mv *nearest, int_mv *near) {
Yaowu Xu's avatar
Yaowu Xu committed
27
28
29
  int i;
  // Make sure all the candidates are properly clamped etc
  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
30
    lower_mv_precision(&mvlist[i].as_mv, allow_hp);
31
    clamp_mv2(&mvlist[i].as_mv, xd);
Yaowu Xu's avatar
Yaowu Xu committed
32
33
34
35
  }
  *nearest = mvlist[0];
  *near = mvlist[1];
}
Ronald S. Bultje's avatar
Ronald S. Bultje committed
36
37

void vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd,
James Zern's avatar
James Zern committed
38
                                   const TileInfo *const tile,
39
                                   int_mv *dst_nearest, int_mv *dst_near,
40
41
                                   int block_idx, int ref_idx,
                                   int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
42
  int_mv mv_list[MAX_MV_REF_CANDIDATES];
43
  MODE_INFO *const mi = xd->mi_8x8[0];
44
45
  b_mode_info *bmi = mi->bmi;
  int n;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
46
47

  assert(ref_idx == 0 || ref_idx == 1);
48
  assert(MAX_MV_REF_CANDIDATES == 2);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
49

James Zern's avatar
James Zern committed
50
  vp9_find_mv_refs_idx(cm, xd, tile, mi, xd->last_mi,
51
52
                       mi->mbmi.ref_frame[ref_idx],
                       mv_list, block_idx, mi_row, mi_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
53

54
  dst_near->as_int = 0;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
55
  if (block_idx == 0) {
56
57
    dst_nearest->as_int = mv_list[0].as_int;
    dst_near->as_int = mv_list[1].as_int;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
58
  } else if (block_idx == 1 || block_idx == 2) {
59
60
61
62
63
64
    dst_nearest->as_int = bmi[0].as_mv[ref_idx].as_int;
    for (n = 0; n < MAX_MV_REF_CANDIDATES; ++n)
      if (dst_nearest->as_int != mv_list[n].as_int) {
        dst_near->as_int = mv_list[n].as_int;
        break;
      }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
65
  } else {
Yaowu Xu's avatar
Yaowu Xu committed
66
67
68
69
70
71
    int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
    candidates[0] = bmi[1].as_mv[ref_idx];
    candidates[1] = bmi[0].as_mv[ref_idx];
    candidates[2] = mv_list[0];
    candidates[3] = mv_list[1];

Ronald S. Bultje's avatar
Ronald S. Bultje committed
72
    assert(block_idx == 3);
73
74
75
76
77
78
79
    dst_nearest->as_int = bmi[2].as_mv[ref_idx].as_int;
    for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n) {
      if (dst_nearest->as_int != candidates[n].as_int) {
        dst_near->as_int = candidates[n].as_int;
        break;
      }
    }
Ronald S. Bultje's avatar
Ronald S. Bultje committed
80
81
  }
}