1233d2500723e5594f3e7c70896ffeeef32b9c950ywan
2233d2500723e5594f3e7c70896ffeeef32b9c950ywan/*
3233d2500723e5594f3e7c70896ffeeef32b9c950ywan *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
4233d2500723e5594f3e7c70896ffeeef32b9c950ywan *
5233d2500723e5594f3e7c70896ffeeef32b9c950ywan *  Use of this source code is governed by a BSD-style license
6233d2500723e5594f3e7c70896ffeeef32b9c950ywan *  that can be found in the LICENSE file in the root of the source
7233d2500723e5594f3e7c70896ffeeef32b9c950ywan *  tree. An additional intellectual property rights grant can be found
8233d2500723e5594f3e7c70896ffeeef32b9c950ywan *  in the file PATENTS.  All contributing project authors may
9233d2500723e5594f3e7c70896ffeeef32b9c950ywan *  be found in the AUTHORS file in the root of the source tree.
10233d2500723e5594f3e7c70896ffeeef32b9c950ywan */
11233d2500723e5594f3e7c70896ffeeef32b9c950ywan
12233d2500723e5594f3e7c70896ffeeef32b9c950ywan#include "vp9/common/vp9_mvref_common.h"
13233d2500723e5594f3e7c70896ffeeef32b9c950ywan
14233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define MVREF_NEIGHBOURS 8
15233d2500723e5594f3e7c70896ffeeef32b9c950ywan
16233d2500723e5594f3e7c70896ffeeef32b9c950ywantypedef struct position {
17233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int row;
18233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int col;
19233d2500723e5594f3e7c70896ffeeef32b9c950ywan} POSITION;
20233d2500723e5594f3e7c70896ffeeef32b9c950ywan
21233d2500723e5594f3e7c70896ffeeef32b9c950ywantypedef enum {
22233d2500723e5594f3e7c70896ffeeef32b9c950ywan  BOTH_ZERO = 0,
23233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ZERO_PLUS_PREDICTED = 1,
24233d2500723e5594f3e7c70896ffeeef32b9c950ywan  BOTH_PREDICTED = 2,
25233d2500723e5594f3e7c70896ffeeef32b9c950ywan  NEW_PLUS_NON_INTRA = 3,
26233d2500723e5594f3e7c70896ffeeef32b9c950ywan  BOTH_NEW = 4,
27233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INTRA_PLUS_NON_INTRA = 5,
28233d2500723e5594f3e7c70896ffeeef32b9c950ywan  BOTH_INTRA = 6,
29233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INVALID_CASE = 9
30233d2500723e5594f3e7c70896ffeeef32b9c950ywan} motion_vector_context;
31233d2500723e5594f3e7c70896ffeeef32b9c950ywan
32233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This is used to figure out a context for the ref blocks. The code flattens
33233d2500723e5594f3e7c70896ffeeef32b9c950ywan// an array that would have 3 possible counts (0, 1 & 2) for 3 choices by
34233d2500723e5594f3e7c70896ffeeef32b9c950ywan// adding 9 for each intra block, 3 for each zero mv and 1 for each new
35233d2500723e5594f3e7c70896ffeeef32b9c950ywan// motion vector. This single number is then converted into a context
36233d2500723e5594f3e7c70896ffeeef32b9c950ywan// with a single lookup ( counter_to_context ).
37233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const int mode_2_counter[MB_MODE_COUNT] = {
38233d2500723e5594f3e7c70896ffeeef32b9c950ywan  9,  // DC_PRED
39233d2500723e5594f3e7c70896ffeeef32b9c950ywan  9,  // V_PRED
40233d2500723e5594f3e7c70896ffeeef32b9c950ywan  9,  // H_PRED
41233d2500723e5594f3e7c70896ffeeef32b9c950ywan  9,  // D45_PRED
42233d2500723e5594f3e7c70896ffeeef32b9c950ywan  9,  // D135_PRED
43233d2500723e5594f3e7c70896ffeeef32b9c950ywan  9,  // D117_PRED
44233d2500723e5594f3e7c70896ffeeef32b9c950ywan  9,  // D153_PRED
45233d2500723e5594f3e7c70896ffeeef32b9c950ywan  9,  // D207_PRED
46233d2500723e5594f3e7c70896ffeeef32b9c950ywan  9,  // D63_PRED
47233d2500723e5594f3e7c70896ffeeef32b9c950ywan  9,  // TM_PRED
48233d2500723e5594f3e7c70896ffeeef32b9c950ywan  0,  // NEARESTMV
49233d2500723e5594f3e7c70896ffeeef32b9c950ywan  0,  // NEARMV
50233d2500723e5594f3e7c70896ffeeef32b9c950ywan  3,  // ZEROMV
51233d2500723e5594f3e7c70896ffeeef32b9c950ywan  1,  // NEWMV
52233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
53233d2500723e5594f3e7c70896ffeeef32b9c950ywan
54233d2500723e5594f3e7c70896ffeeef32b9c950ywan// There are 3^3 different combinations of 3 counts that can be either 0,1 or
55233d2500723e5594f3e7c70896ffeeef32b9c950ywan// 2. However the actual count can never be greater than 2 so the highest
56233d2500723e5594f3e7c70896ffeeef32b9c950ywan// counter we need is 18. 9 is an invalid counter that's never used.
57233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const int counter_to_context[19] = {
58233d2500723e5594f3e7c70896ffeeef32b9c950ywan  BOTH_PREDICTED,  // 0
59233d2500723e5594f3e7c70896ffeeef32b9c950ywan  NEW_PLUS_NON_INTRA,  // 1
60233d2500723e5594f3e7c70896ffeeef32b9c950ywan  BOTH_NEW,  // 2
61233d2500723e5594f3e7c70896ffeeef32b9c950ywan  ZERO_PLUS_PREDICTED,  // 3
62233d2500723e5594f3e7c70896ffeeef32b9c950ywan  NEW_PLUS_NON_INTRA,  // 4
63233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INVALID_CASE,  // 5
64233d2500723e5594f3e7c70896ffeeef32b9c950ywan  BOTH_ZERO,  // 6
65233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INVALID_CASE,  // 7
66233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INVALID_CASE,  // 8
67233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INTRA_PLUS_NON_INTRA,  // 9
68233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INTRA_PLUS_NON_INTRA,  // 10
69233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INVALID_CASE,  // 11
70233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INTRA_PLUS_NON_INTRA,  // 12
71233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INVALID_CASE,  // 13
72233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INVALID_CASE,  // 14
73233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INVALID_CASE,  // 15
74233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INVALID_CASE,  // 16
75233d2500723e5594f3e7c70896ffeeef32b9c950ywan  INVALID_CASE,  // 17
76233d2500723e5594f3e7c70896ffeeef32b9c950ywan  BOTH_INTRA  // 18
77233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
78233d2500723e5594f3e7c70896ffeeef32b9c950ywan
79233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const POSITION mv_ref_blocks[BLOCK_SIZES][MVREF_NEIGHBOURS] = {
80233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // 4X4
81233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
82233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // 4X8
83233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
84233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // 8X4
85233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
86233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // 8X8
87233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
88233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // 8X16
89233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {{0, -1}, {-1, 0}, {1, -1}, {-1, -1}, {0, -2}, {-2, 0}, {-2, -1}, {-1, -2}},
90233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // 16X8
91233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {{-1, 0}, {0, -1}, {-1, 1}, {-1, -1}, {-2, 0}, {0, -2}, {-1, -2}, {-2, -1}},
92233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // 16X16
93233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {{-1, 0}, {0, -1}, {-1, 1}, {1, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
94233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // 16X32
95233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {{0, -1}, {-1, 0}, {2, -1}, {-1, -1}, {-1, 1}, {0, -3}, {-3, 0}, {-3, -3}},
96233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // 32X16
97233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {{-1, 0}, {0, -1}, {-1, 2}, {-1, -1}, {1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
98233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // 32X32
99233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {{-1, 1}, {1, -1}, {-1, 2}, {2, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
100233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // 32X64
101233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {{0, -1}, {-1, 0}, {4, -1}, {-1, 2}, {-1, -1}, {0, -3}, {-3, 0}, {2, -1}},
102233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // 64X32
103233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {{-1, 0}, {0, -1}, {-1, 4}, {2, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-1, 2}},
104233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // 64X64
105233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {{-1, 3}, {3, -1}, {-1, 4}, {4, -1}, {-1, -1}, {-1, 0}, {0, -1}, {-1, 6}}
106233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
107233d2500723e5594f3e7c70896ffeeef32b9c950ywan
108233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic const int idx_n_column_to_subblock[4][2] = {
109233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {1, 2},
110233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {1, 3},
111233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {3, 2},
112233d2500723e5594f3e7c70896ffeeef32b9c950ywan  {3, 3}
113233d2500723e5594f3e7c70896ffeeef32b9c950ywan};
114233d2500723e5594f3e7c70896ffeeef32b9c950ywan
115233d2500723e5594f3e7c70896ffeeef32b9c950ywan// clamp_mv_ref
116233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define MV_BORDER (16 << 3)  // Allow 16 pels in 1/8th pel units
117233d2500723e5594f3e7c70896ffeeef32b9c950ywan
118233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void clamp_mv_ref(MV *mv, const MACROBLOCKD *xd) {
119233d2500723e5594f3e7c70896ffeeef32b9c950ywan  clamp_mv(mv, xd->mb_to_left_edge - MV_BORDER,
120233d2500723e5594f3e7c70896ffeeef32b9c950ywan               xd->mb_to_right_edge + MV_BORDER,
121233d2500723e5594f3e7c70896ffeeef32b9c950ywan               xd->mb_to_top_edge - MV_BORDER,
122233d2500723e5594f3e7c70896ffeeef32b9c950ywan               xd->mb_to_bottom_edge + MV_BORDER);
123233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
124233d2500723e5594f3e7c70896ffeeef32b9c950ywan
125233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This function returns either the appropriate sub block or block's mv
126233d2500723e5594f3e7c70896ffeeef32b9c950ywan// on whether the block_size < 8x8 and we have check_sub_blocks set.
127233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic INLINE int_mv get_sub_block_mv(const MODE_INFO *candidate, int which_mv,
128233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                      int search_col, int block_idx) {
129233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return block_idx >= 0 && candidate->mbmi.sb_type < BLOCK_8X8
130233d2500723e5594f3e7c70896ffeeef32b9c950ywan          ? candidate->bmi[idx_n_column_to_subblock[block_idx][search_col == 0]]
131233d2500723e5594f3e7c70896ffeeef32b9c950ywan              .as_mv[which_mv]
132233d2500723e5594f3e7c70896ffeeef32b9c950ywan          : candidate->mbmi.mv[which_mv];
133233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
134233d2500723e5594f3e7c70896ffeeef32b9c950ywan
135233d2500723e5594f3e7c70896ffeeef32b9c950ywan
136233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Performs mv sign inversion if indicated by the reference frame combination.
137233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic INLINE int_mv scale_mv(const MB_MODE_INFO *mbmi, int ref,
138233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              const MV_REFERENCE_FRAME this_ref_frame,
139233d2500723e5594f3e7c70896ffeeef32b9c950ywan                              const int *ref_sign_bias) {
140233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int_mv mv = mbmi->mv[ref];
141233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (ref_sign_bias[mbmi->ref_frame[ref]] != ref_sign_bias[this_ref_frame]) {
142233d2500723e5594f3e7c70896ffeeef32b9c950ywan    mv.as_mv.row *= -1;
143233d2500723e5594f3e7c70896ffeeef32b9c950ywan    mv.as_mv.col *= -1;
144233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
145233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return mv;
146233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
147233d2500723e5594f3e7c70896ffeeef32b9c950ywan
148233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This macro is used to add a motion vector mv_ref list if it isn't
149233d2500723e5594f3e7c70896ffeeef32b9c950ywan// already in the list.  If it's the second motion vector it will also
150233d2500723e5594f3e7c70896ffeeef32b9c950ywan// skip all additional processing and jump to done!
151233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define ADD_MV_REF_LIST(mv) \
152233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do { \
153233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (refmv_count) { \
154233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if ((mv).as_int != mv_ref_list[0].as_int) { \
155233d2500723e5594f3e7c70896ffeeef32b9c950ywan        mv_ref_list[refmv_count] = (mv); \
156233d2500723e5594f3e7c70896ffeeef32b9c950ywan        goto Done; \
157233d2500723e5594f3e7c70896ffeeef32b9c950ywan      } \
158233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } else { \
159233d2500723e5594f3e7c70896ffeeef32b9c950ywan      mv_ref_list[refmv_count++] = (mv); \
160233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } \
161233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (0)
162233d2500723e5594f3e7c70896ffeeef32b9c950ywan
163233d2500723e5594f3e7c70896ffeeef32b9c950ywan// If either reference frame is different, not INTRA, and they
164233d2500723e5594f3e7c70896ffeeef32b9c950ywan// are different from each other scale and add the mv to our list.
165233d2500723e5594f3e7c70896ffeeef32b9c950ywan#define IF_DIFF_REF_FRAME_ADD_MV(mbmi) \
166233d2500723e5594f3e7c70896ffeeef32b9c950ywan  do { \
167233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (is_inter_block(mbmi)) { \
168233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if ((mbmi)->ref_frame[0] != ref_frame) \
169233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ADD_MV_REF_LIST(scale_mv((mbmi), 0, ref_frame, ref_sign_bias)); \
170233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (has_second_ref(mbmi) && \
171233d2500723e5594f3e7c70896ffeeef32b9c950ywan          (mbmi)->ref_frame[1] != ref_frame && \
172233d2500723e5594f3e7c70896ffeeef32b9c950ywan          (mbmi)->mv[1].as_int != (mbmi)->mv[0].as_int) \
173233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ADD_MV_REF_LIST(scale_mv((mbmi), 1, ref_frame, ref_sign_bias)); \
174233d2500723e5594f3e7c70896ffeeef32b9c950ywan    } \
175233d2500723e5594f3e7c70896ffeeef32b9c950ywan  } while (0)
176233d2500723e5594f3e7c70896ffeeef32b9c950ywan
177233d2500723e5594f3e7c70896ffeeef32b9c950ywan
178233d2500723e5594f3e7c70896ffeeef32b9c950ywan// Checks that the given mi_row, mi_col and search point
179233d2500723e5594f3e7c70896ffeeef32b9c950ywan// are inside the borders of the tile.
180233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic INLINE int is_inside(const TileInfo *const tile,
181233d2500723e5594f3e7c70896ffeeef32b9c950ywan                            int mi_col, int mi_row, int mi_rows,
182233d2500723e5594f3e7c70896ffeeef32b9c950ywan                            const POSITION *mi_pos) {
183233d2500723e5594f3e7c70896ffeeef32b9c950ywan  return !(mi_row + mi_pos->row < 0 ||
184233d2500723e5594f3e7c70896ffeeef32b9c950ywan           mi_col + mi_pos->col < tile->mi_col_start ||
185233d2500723e5594f3e7c70896ffeeef32b9c950ywan           mi_row + mi_pos->row >= mi_rows ||
186233d2500723e5594f3e7c70896ffeeef32b9c950ywan           mi_col + mi_pos->col >= tile->mi_col_end);
187233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
188233d2500723e5594f3e7c70896ffeeef32b9c950ywan
189233d2500723e5594f3e7c70896ffeeef32b9c950ywan// This function searches the neighbourhood of a given MB/SB
190233d2500723e5594f3e7c70896ffeeef32b9c950ywan// to try and find candidate reference vectors.
191233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void find_mv_refs_idx(const VP9_COMMON *cm, const MACROBLOCKD *xd,
192233d2500723e5594f3e7c70896ffeeef32b9c950ywan                             const TileInfo *const tile,
193233d2500723e5594f3e7c70896ffeeef32b9c950ywan                             MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
194233d2500723e5594f3e7c70896ffeeef32b9c950ywan                             int_mv *mv_ref_list,
195233d2500723e5594f3e7c70896ffeeef32b9c950ywan                             int block, int mi_row, int mi_col) {
196233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int *ref_sign_bias = cm->ref_frame_sign_bias;
197233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int i, refmv_count = 0;
198233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const MODE_INFO *prev_mi = cm->coding_use_prev_mi && cm->prev_mi
199233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ? cm->prev_mi_grid_visible[mi_row * xd->mi_stride + mi_col]
200233d2500723e5594f3e7c70896ffeeef32b9c950ywan        : NULL;
201233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const MB_MODE_INFO *const prev_mbmi = prev_mi ? &prev_mi->mbmi : NULL;
202233d2500723e5594f3e7c70896ffeeef32b9c950ywan
203233d2500723e5594f3e7c70896ffeeef32b9c950ywan
204233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const POSITION *const mv_ref_search = mv_ref_blocks[mi->mbmi.sb_type];
205233d2500723e5594f3e7c70896ffeeef32b9c950ywan
206233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int different_ref_found = 0;
207233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int context_counter = 0;
208233d2500723e5594f3e7c70896ffeeef32b9c950ywan
209233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Blank the reference vector list
210233d2500723e5594f3e7c70896ffeeef32b9c950ywan  vpx_memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
211233d2500723e5594f3e7c70896ffeeef32b9c950ywan
212233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // The nearest 2 blocks are treated differently
213233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // if the size < 8x8 we get the mv from the bmi substructure,
214233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // and we also need to keep a mode count.
215233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (i = 0; i < 2; ++i) {
216233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const POSITION *const mv_ref = &mv_ref_search[i];
217233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
218233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const MODE_INFO *const candidate_mi = xd->mi[mv_ref->col + mv_ref->row *
219233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                   xd->mi_stride];
220233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const MB_MODE_INFO *const candidate = &candidate_mi->mbmi;
221233d2500723e5594f3e7c70896ffeeef32b9c950ywan      // Keep counts for entropy encoding.
222233d2500723e5594f3e7c70896ffeeef32b9c950ywan      context_counter += mode_2_counter[candidate->mode];
223233d2500723e5594f3e7c70896ffeeef32b9c950ywan      different_ref_found = 1;
224233d2500723e5594f3e7c70896ffeeef32b9c950ywan
225233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (candidate->ref_frame[0] == ref_frame)
226233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0, mv_ref->col, block));
227233d2500723e5594f3e7c70896ffeeef32b9c950ywan      else if (candidate->ref_frame[1] == ref_frame)
228233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 1, mv_ref->col, block));
229233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
230233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
231233d2500723e5594f3e7c70896ffeeef32b9c950ywan
232233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Check the rest of the neighbors in much the same way
233233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // as before except we don't need to keep track of sub blocks or
234233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // mode counts.
235233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (; i < MVREF_NEIGHBOURS; ++i) {
236233d2500723e5594f3e7c70896ffeeef32b9c950ywan    const POSITION *const mv_ref = &mv_ref_search[i];
237233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
238233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row *
239233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                                    xd->mi_stride]->mbmi;
240233d2500723e5594f3e7c70896ffeeef32b9c950ywan      different_ref_found = 1;
241233d2500723e5594f3e7c70896ffeeef32b9c950ywan
242233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (candidate->ref_frame[0] == ref_frame)
243233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ADD_MV_REF_LIST(candidate->mv[0]);
244233d2500723e5594f3e7c70896ffeeef32b9c950ywan      else if (candidate->ref_frame[1] == ref_frame)
245233d2500723e5594f3e7c70896ffeeef32b9c950ywan        ADD_MV_REF_LIST(candidate->mv[1]);
246233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
247233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
248233d2500723e5594f3e7c70896ffeeef32b9c950ywan
249233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Check the last frame's mode and mv info.
250233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (prev_mbmi) {
251233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (prev_mbmi->ref_frame[0] == ref_frame)
252233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ADD_MV_REF_LIST(prev_mbmi->mv[0]);
253233d2500723e5594f3e7c70896ffeeef32b9c950ywan    else if (prev_mbmi->ref_frame[1] == ref_frame)
254233d2500723e5594f3e7c70896ffeeef32b9c950ywan      ADD_MV_REF_LIST(prev_mbmi->mv[1]);
255233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
256233d2500723e5594f3e7c70896ffeeef32b9c950ywan
257233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Since we couldn't find 2 mvs from the same reference frame
258233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // go back through the neighbors and find motion vectors from
259233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // different reference frames.
260233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (different_ref_found) {
261233d2500723e5594f3e7c70896ffeeef32b9c950ywan    for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
262233d2500723e5594f3e7c70896ffeeef32b9c950ywan      const POSITION *mv_ref = &mv_ref_search[i];
263233d2500723e5594f3e7c70896ffeeef32b9c950ywan      if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
264233d2500723e5594f3e7c70896ffeeef32b9c950ywan        const MB_MODE_INFO *const candidate = &xd->mi[mv_ref->col + mv_ref->row
265233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                              * xd->mi_stride]->mbmi;
266233d2500723e5594f3e7c70896ffeeef32b9c950ywan
267233d2500723e5594f3e7c70896ffeeef32b9c950ywan        // If the candidate is INTRA we don't want to consider its mv.
268233d2500723e5594f3e7c70896ffeeef32b9c950ywan        IF_DIFF_REF_FRAME_ADD_MV(candidate);
269233d2500723e5594f3e7c70896ffeeef32b9c950ywan      }
270233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
271233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
272233d2500723e5594f3e7c70896ffeeef32b9c950ywan
273233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Since we still don't have a candidate we'll try the last frame.
274233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (prev_mbmi)
275233d2500723e5594f3e7c70896ffeeef32b9c950ywan    IF_DIFF_REF_FRAME_ADD_MV(prev_mbmi);
276233d2500723e5594f3e7c70896ffeeef32b9c950ywan
277233d2500723e5594f3e7c70896ffeeef32b9c950ywan Done:
278233d2500723e5594f3e7c70896ffeeef32b9c950ywan
279233d2500723e5594f3e7c70896ffeeef32b9c950ywan  mi->mbmi.mode_context[ref_frame] = counter_to_context[context_counter];
280233d2500723e5594f3e7c70896ffeeef32b9c950ywan
281233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Clamp vectors
282233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
283233d2500723e5594f3e7c70896ffeeef32b9c950ywan    clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
284233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
285233d2500723e5594f3e7c70896ffeeef32b9c950ywan
286233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
287233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                    const TileInfo *const tile,
288233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                    MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
289233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                    int_mv *mv_ref_list,
290233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                    int mi_row, int mi_col) {
291233d2500723e5594f3e7c70896ffeeef32b9c950ywan  find_mv_refs_idx(cm, xd, tile, mi, ref_frame, mv_ref_list, -1,
292233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   mi_row, mi_col);
293233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
294233d2500723e5594f3e7c70896ffeeef32b9c950ywan
295233d2500723e5594f3e7c70896ffeeef32b9c950ywanstatic void lower_mv_precision(MV *mv, int allow_hp) {
296233d2500723e5594f3e7c70896ffeeef32b9c950ywan  const int use_hp = allow_hp && vp9_use_mv_hp(mv);
297233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (!use_hp) {
298233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (mv->row & 1)
299233d2500723e5594f3e7c70896ffeeef32b9c950ywan      mv->row += (mv->row > 0 ? -1 : 1);
300233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if (mv->col & 1)
301233d2500723e5594f3e7c70896ffeeef32b9c950ywan      mv->col += (mv->col > 0 ? -1 : 1);
302233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
303233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
304233d2500723e5594f3e7c70896ffeeef32b9c950ywan
305233d2500723e5594f3e7c70896ffeeef32b9c950ywan
306233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp,
307233d2500723e5594f3e7c70896ffeeef32b9c950ywan                           int_mv *mvlist, int_mv *nearest, int_mv *near) {
308233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int i;
309233d2500723e5594f3e7c70896ffeeef32b9c950ywan  // Make sure all the candidates are properly clamped etc
310233d2500723e5594f3e7c70896ffeeef32b9c950ywan  for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
311233d2500723e5594f3e7c70896ffeeef32b9c950ywan    lower_mv_precision(&mvlist[i].as_mv, allow_hp);
312233d2500723e5594f3e7c70896ffeeef32b9c950ywan    clamp_mv2(&mvlist[i].as_mv, xd);
313233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
314233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *nearest = mvlist[0];
315233d2500723e5594f3e7c70896ffeeef32b9c950ywan  *near = mvlist[1];
316233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
317233d2500723e5594f3e7c70896ffeeef32b9c950ywan
318233d2500723e5594f3e7c70896ffeeef32b9c950ywanvoid vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd,
319233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   const TileInfo *const tile,
320233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   int block, int ref, int mi_row, int mi_col,
321233d2500723e5594f3e7c70896ffeeef32b9c950ywan                                   int_mv *nearest, int_mv *near) {
322233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int_mv mv_list[MAX_MV_REF_CANDIDATES];
323233d2500723e5594f3e7c70896ffeeef32b9c950ywan  MODE_INFO *const mi = xd->mi[0];
324233d2500723e5594f3e7c70896ffeeef32b9c950ywan  b_mode_info *bmi = mi->bmi;
325233d2500723e5594f3e7c70896ffeeef32b9c950ywan  int n;
326233d2500723e5594f3e7c70896ffeeef32b9c950ywan
327233d2500723e5594f3e7c70896ffeeef32b9c950ywan  assert(MAX_MV_REF_CANDIDATES == 2);
328233d2500723e5594f3e7c70896ffeeef32b9c950ywan
329233d2500723e5594f3e7c70896ffeeef32b9c950ywan  find_mv_refs_idx(cm, xd, tile, mi, mi->mbmi.ref_frame[ref], mv_list, block,
330233d2500723e5594f3e7c70896ffeeef32b9c950ywan                   mi_row, mi_col);
331233d2500723e5594f3e7c70896ffeeef32b9c950ywan
332233d2500723e5594f3e7c70896ffeeef32b9c950ywan  near->as_int = 0;
333233d2500723e5594f3e7c70896ffeeef32b9c950ywan  switch (block) {
334233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case 0:
335233d2500723e5594f3e7c70896ffeeef32b9c950ywan      nearest->as_int = mv_list[0].as_int;
336233d2500723e5594f3e7c70896ffeeef32b9c950ywan      near->as_int = mv_list[1].as_int;
337233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
338233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case 1:
339233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case 2:
340233d2500723e5594f3e7c70896ffeeef32b9c950ywan      nearest->as_int = bmi[0].as_mv[ref].as_int;
341233d2500723e5594f3e7c70896ffeeef32b9c950ywan      for (n = 0; n < MAX_MV_REF_CANDIDATES; ++n)
342233d2500723e5594f3e7c70896ffeeef32b9c950ywan        if (nearest->as_int != mv_list[n].as_int) {
343233d2500723e5594f3e7c70896ffeeef32b9c950ywan          near->as_int = mv_list[n].as_int;
344233d2500723e5594f3e7c70896ffeeef32b9c950ywan          break;
345233d2500723e5594f3e7c70896ffeeef32b9c950ywan        }
346233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
347233d2500723e5594f3e7c70896ffeeef32b9c950ywan    case 3: {
348233d2500723e5594f3e7c70896ffeeef32b9c950ywan      int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
349233d2500723e5594f3e7c70896ffeeef32b9c950ywan      candidates[0] = bmi[1].as_mv[ref];
350233d2500723e5594f3e7c70896ffeeef32b9c950ywan      candidates[1] = bmi[0].as_mv[ref];
351233d2500723e5594f3e7c70896ffeeef32b9c950ywan      candidates[2] = mv_list[0];
352233d2500723e5594f3e7c70896ffeeef32b9c950ywan      candidates[3] = mv_list[1];
353233d2500723e5594f3e7c70896ffeeef32b9c950ywan
354233d2500723e5594f3e7c70896ffeeef32b9c950ywan      nearest->as_int = bmi[2].as_mv[ref].as_int;
355233d2500723e5594f3e7c70896ffeeef32b9c950ywan      for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n)
356233d2500723e5594f3e7c70896ffeeef32b9c950ywan        if (nearest->as_int != candidates[n].as_int) {
357233d2500723e5594f3e7c70896ffeeef32b9c950ywan          near->as_int = candidates[n].as_int;
358233d2500723e5594f3e7c70896ffeeef32b9c950ywan          break;
359233d2500723e5594f3e7c70896ffeeef32b9c950ywan        }
360233d2500723e5594f3e7c70896ffeeef32b9c950ywan      break;
361233d2500723e5594f3e7c70896ffeeef32b9c950ywan    }
362233d2500723e5594f3e7c70896ffeeef32b9c950ywan    default:
363233d2500723e5594f3e7c70896ffeeef32b9c950ywan      assert("Invalid block index.");
364233d2500723e5594f3e7c70896ffeeef32b9c950ywan  }
365233d2500723e5594f3e7c70896ffeeef32b9c950ywan}
366