2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * 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.
10 #ifndef VP9_COMMON_VP9_MVREF_COMMON_H_
11 #define VP9_COMMON_VP9_MVREF_COMMON_H_
13 #include "vp9/common/vp9_onyxc_int.h"
14 #include "vp9/common/vp9_blockd.h"
20 #define LEFT_TOP_MARGIN ((VP9_ENC_BORDER_IN_PIXELS - VP9_INTERP_EXTEND) << 3)
21 #define RIGHT_BOTTOM_MARGIN ((VP9_ENC_BORDER_IN_PIXELS -\
22 VP9_INTERP_EXTEND) << 3)
24 #define MVREF_NEIGHBOURS 8
26 typedef struct position {
33 ZERO_PLUS_PREDICTED = 1,
35 NEW_PLUS_NON_INTRA = 3,
37 INTRA_PLUS_NON_INTRA = 5,
40 } motion_vector_context;
42 // This is used to figure out a context for the ref blocks. The code flattens
43 // an array that would have 3 possible counts (0, 1 & 2) for 3 choices by
44 // adding 9 for each intra block, 3 for each zero mv and 1 for each new
45 // motion vector. This single number is then converted into a context
46 // with a single lookup ( counter_to_context ).
47 static const int mode_2_counter[MB_MODE_COUNT] = {
64 // There are 3^3 different combinations of 3 counts that can be either 0,1 or
65 // 2. However the actual count can never be greater than 2 so the highest
66 // counter we need is 18. 9 is an invalid counter that's never used.
67 static const int counter_to_context[19] = {
69 NEW_PLUS_NON_INTRA, // 1
71 ZERO_PLUS_PREDICTED, // 3
72 NEW_PLUS_NON_INTRA, // 4
77 INTRA_PLUS_NON_INTRA, // 9
78 INTRA_PLUS_NON_INTRA, // 10
80 INTRA_PLUS_NON_INTRA, // 12
89 static const POSITION mv_ref_blocks[BLOCK_SIZES][MVREF_NEIGHBOURS] = {
91 {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
93 {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
95 {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
97 {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
99 {{0, -1}, {-1, 0}, {1, -1}, {-1, -1}, {0, -2}, {-2, 0}, {-2, -1}, {-1, -2}},
101 {{-1, 0}, {0, -1}, {-1, 1}, {-1, -1}, {-2, 0}, {0, -2}, {-1, -2}, {-2, -1}},
103 {{-1, 0}, {0, -1}, {-1, 1}, {1, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
105 {{0, -1}, {-1, 0}, {2, -1}, {-1, -1}, {-1, 1}, {0, -3}, {-3, 0}, {-3, -3}},
107 {{-1, 0}, {0, -1}, {-1, 2}, {-1, -1}, {1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
109 {{-1, 1}, {1, -1}, {-1, 2}, {2, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
111 {{0, -1}, {-1, 0}, {4, -1}, {-1, 2}, {-1, -1}, {0, -3}, {-3, 0}, {2, -1}},
113 {{-1, 0}, {0, -1}, {-1, 4}, {2, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-1, 2}},
115 {{-1, 3}, {3, -1}, {-1, 4}, {4, -1}, {-1, -1}, {-1, 0}, {0, -1}, {-1, 6}}
118 static const int idx_n_column_to_subblock[4][2] = {
126 #define MV_BORDER (16 << 3) // Allow 16 pels in 1/8th pel units
128 static void clamp_mv_ref(MV *mv, const MACROBLOCKD *xd) {
129 clamp_mv(mv, xd->mb_to_left_edge - MV_BORDER,
130 xd->mb_to_right_edge + MV_BORDER,
131 xd->mb_to_top_edge - MV_BORDER,
132 xd->mb_to_bottom_edge + MV_BORDER);
135 // This function returns either the appropriate sub block or block's mv
136 // on whether the block_size < 8x8 and we have check_sub_blocks set.
137 static INLINE int_mv get_sub_block_mv(const MODE_INFO *candidate, int which_mv,
138 int search_col, int block_idx) {
139 return block_idx >= 0 && candidate->mbmi.sb_type < BLOCK_8X8
140 ? candidate->bmi[idx_n_column_to_subblock[block_idx][search_col == 0]]
142 : candidate->mbmi.mv[which_mv];
146 // Performs mv sign inversion if indicated by the reference frame combination.
147 static INLINE int_mv scale_mv(const MB_MODE_INFO *mbmi, int ref,
148 const MV_REFERENCE_FRAME this_ref_frame,
149 const int *ref_sign_bias) {
150 int_mv mv = mbmi->mv[ref];
151 if (ref_sign_bias[mbmi->ref_frame[ref]] != ref_sign_bias[this_ref_frame]) {
158 // This macro is used to add a motion vector mv_ref list if it isn't
159 // already in the list. If it's the second motion vector it will also
160 // skip all additional processing and jump to done!
161 #define ADD_MV_REF_LIST(mv) \
164 if ((mv).as_int != mv_ref_list[0].as_int) { \
165 mv_ref_list[refmv_count] = (mv); \
169 mv_ref_list[refmv_count++] = (mv); \
173 // If either reference frame is different, not INTRA, and they
174 // are different from each other scale and add the mv to our list.
175 #define IF_DIFF_REF_FRAME_ADD_MV(mbmi) \
177 if (is_inter_block(mbmi)) { \
178 if ((mbmi)->ref_frame[0] != ref_frame) \
179 ADD_MV_REF_LIST(scale_mv((mbmi), 0, ref_frame, ref_sign_bias)); \
180 if (has_second_ref(mbmi) && \
181 (mbmi)->ref_frame[1] != ref_frame && \
182 (mbmi)->mv[1].as_int != (mbmi)->mv[0].as_int) \
183 ADD_MV_REF_LIST(scale_mv((mbmi), 1, ref_frame, ref_sign_bias)); \
188 // Checks that the given mi_row, mi_col and search point
189 // are inside the borders of the tile.
190 static INLINE int is_inside(const TileInfo *const tile,
191 int mi_col, int mi_row, int mi_rows,
192 const POSITION *mi_pos) {
193 return !(mi_row + mi_pos->row < 0 ||
194 mi_col + mi_pos->col < tile->mi_col_start ||
195 mi_row + mi_pos->row >= mi_rows ||
196 mi_col + mi_pos->col >= tile->mi_col_end);
199 // TODO(jingning): this mv clamping function should be block size dependent.
200 static INLINE void clamp_mv2(MV *mv, const MACROBLOCKD *xd) {
201 clamp_mv(mv, xd->mb_to_left_edge - LEFT_TOP_MARGIN,
202 xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN,
203 xd->mb_to_top_edge - LEFT_TOP_MARGIN,
204 xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN);
207 void vp9_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
208 const TileInfo *const tile,
209 MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
210 int_mv *mv_ref_list, int mi_row, int mi_col);
212 // check a list of motion vectors by sad score using a number rows of pixels
213 // above and a number cols of pixels in the left to select the one with best
214 // score to use as ref motion vector
215 void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp,
216 int_mv *mvlist, int_mv *nearest, int_mv *near);
218 void vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd,
219 const TileInfo *const tile,
220 int block, int ref, int mi_row, int mi_col,
221 int_mv *nearest, int_mv *near);
227 #endif // VP9_COMMON_VP9_MVREF_COMMON_H_