3 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
5 * Use of this source code is governed by a BSD-style license
6 * that can be found in the LICENSE file in the root of the source
7 * tree. An additional intellectual property rights grant can be found
8 * in the file PATENTS. All contributing project authors may
9 * be found in the AUTHORS file in the root of the source tree.
12 #include "vp9/common/vp9_mvref_common.h"
14 #define MVREF_NEIGHBOURS 8
16 typedef struct position {
23 ZERO_PLUS_PREDICTED = 1,
25 NEW_PLUS_NON_INTRA = 3,
27 INTRA_PLUS_NON_INTRA = 5,
30 } motion_vector_context;
32 // This is used to figure out a context for the ref blocks. The code flattens
33 // an array that would have 3 possible counts (0, 1 & 2) for 3 choices by
34 // adding 9 for each intra block, 3 for each zero mv and 1 for each new
35 // motion vector. This single number is then converted into a context
36 // with a single lookup ( counter_to_context ).
37 static const int mode_2_counter[MB_MODE_COUNT] = {
54 // There are 3^3 different combinations of 3 counts that can be either 0,1 or
55 // 2. However the actual count can never be greater than 2 so the highest
56 // counter we need is 18. 9 is an invalid counter that's never used.
57 static const int counter_to_context[19] = {
59 NEW_PLUS_NON_INTRA, // 1
61 ZERO_PLUS_PREDICTED, // 3
62 NEW_PLUS_NON_INTRA, // 4
67 INTRA_PLUS_NON_INTRA, // 9
68 INTRA_PLUS_NON_INTRA, // 10
70 INTRA_PLUS_NON_INTRA, // 12
79 static const POSITION mv_ref_blocks[BLOCK_SIZES][MVREF_NEIGHBOURS] = {
81 {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
83 {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
85 {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
87 {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
89 {{0, -1}, {-1, 0}, {1, -1}, {-1, -1}, {0, -2}, {-2, 0}, {-2, -1}, {-1, -2}},
91 {{-1, 0}, {0, -1}, {-1, 1}, {-1, -1}, {-2, 0}, {0, -2}, {-1, -2}, {-2, -1}},
93 {{-1, 0}, {0, -1}, {-1, 1}, {1, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
95 {{0, -1}, {-1, 0}, {2, -1}, {-1, -1}, {-1, 1}, {0, -3}, {-3, 0}, {-3, -3}},
97 {{-1, 0}, {0, -1}, {-1, 2}, {-1, -1}, {1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
99 {{-1, 1}, {1, -1}, {-1, 2}, {2, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
101 {{0, -1}, {-1, 0}, {4, -1}, {-1, 2}, {-1, -1}, {0, -3}, {-3, 0}, {2, -1}},
103 {{-1, 0}, {0, -1}, {-1, 4}, {2, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-1, 2}},
105 {{-1, 3}, {3, -1}, {-1, 4}, {4, -1}, {-1, -1}, {-1, 0}, {0, -1}, {-1, 6}}
108 static const int idx_n_column_to_subblock[4][2] = {
116 #define MV_BORDER (16 << 3) // Allow 16 pels in 1/8th pel units
118 static void clamp_mv_ref(MV *mv, const MACROBLOCKD *xd) {
119 clamp_mv(mv, xd->mb_to_left_edge - MV_BORDER,
120 xd->mb_to_right_edge + MV_BORDER,
121 xd->mb_to_top_edge - MV_BORDER,
122 xd->mb_to_bottom_edge + MV_BORDER);
125 // This function returns either the appropriate sub block or block's mv
126 // on whether the block_size < 8x8 and we have check_sub_blocks set.
127 static INLINE int_mv get_sub_block_mv(const MODE_INFO *candidate, int which_mv,
128 int search_col, int block_idx) {
129 return block_idx >= 0 && candidate->mbmi.sb_type < BLOCK_8X8
130 ? candidate->bmi[idx_n_column_to_subblock[block_idx][search_col == 0]]
132 : candidate->mbmi.mv[which_mv];
136 // Performs mv sign inversion if indicated by the reference frame combination.
137 static INLINE int_mv scale_mv(const MB_MODE_INFO *mbmi, int ref,
138 const MV_REFERENCE_FRAME this_ref_frame,
139 const int *ref_sign_bias) {
140 int_mv mv = mbmi->mv[ref];
141 if (ref_sign_bias[mbmi->ref_frame[ref]] != ref_sign_bias[this_ref_frame]) {
148 // This macro is used to add a motion vector mv_ref list if it isn't
149 // already in the list. If it's the second motion vector it will also
150 // skip all additional processing and jump to done!
151 #define ADD_MV_REF_LIST(MV) \
154 if ((MV).as_int != mv_ref_list[0].as_int) { \
155 mv_ref_list[refmv_count] = (MV); \
159 mv_ref_list[refmv_count++] = (MV); \
163 // If either reference frame is different, not INTRA, and they
164 // are different from each other scale and add the mv to our list.
165 #define IF_DIFF_REF_FRAME_ADD_MV(CANDIDATE) \
167 if ((CANDIDATE)->ref_frame[0] != ref_frame) \
168 ADD_MV_REF_LIST(scale_mv((CANDIDATE), 0, ref_frame, ref_sign_bias)); \
169 if ((CANDIDATE)->ref_frame[1] != ref_frame && \
170 has_second_ref(CANDIDATE) && \
171 (CANDIDATE)->mv[1].as_int != (CANDIDATE)->mv[0].as_int) \
172 ADD_MV_REF_LIST(scale_mv((CANDIDATE), 1, ref_frame, ref_sign_bias)); \
176 // Checks that the given mi_row, mi_col and search point
177 // are inside the borders of the tile.
178 static INLINE int is_inside(const TileInfo *const tile,
179 int mi_col, int mi_row, int mi_rows,
180 const POSITION *mi_pos) {
181 return !(mi_row + mi_pos->row < 0 ||
182 mi_col + mi_pos->col < tile->mi_col_start ||
183 mi_row + mi_pos->row >= mi_rows ||
184 mi_col + mi_pos->col >= tile->mi_col_end);
187 // This function searches the neighbourhood of a given MB/SB
188 // to try and find candidate reference vectors.
189 static void find_mv_refs_idx(const VP9_COMMON *cm, const MACROBLOCKD *xd,
190 const TileInfo *const tile,
191 MODE_INFO *mi, const MODE_INFO *prev_mi,
192 MV_REFERENCE_FRAME ref_frame,
194 int block_idx, int mi_row, int mi_col) {
195 const int *ref_sign_bias = cm->ref_frame_sign_bias;
196 int i, refmv_count = 0;
197 const POSITION *const mv_ref_search = mv_ref_blocks[mi->mbmi.sb_type];
198 const MB_MODE_INFO *const prev_mbmi = cm->coding_use_prev_mi && prev_mi ?
199 &prev_mi->mbmi : NULL;
200 int different_ref_found = 0;
201 int context_counter = 0;
203 // Blank the reference vector list
204 vpx_memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
206 // The nearest 2 blocks are treated differently
207 // if the size < 8x8 we get the mv from the bmi substructure,
208 // and we also need to keep a mode count.
209 for (i = 0; i < 2; ++i) {
210 const POSITION *const mv_ref = &mv_ref_search[i];
211 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
212 const MODE_INFO *const candidate_mi = xd->mi_8x8[mv_ref->col + mv_ref->row
213 * xd->mode_info_stride];
214 const MB_MODE_INFO *const candidate = &candidate_mi->mbmi;
215 // Keep counts for entropy encoding.
216 context_counter += mode_2_counter[candidate->mode];
218 // Check if the candidate comes from the same reference frame.
219 if (candidate->ref_frame[0] == ref_frame) {
220 ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 0,
221 mv_ref->col, block_idx));
222 different_ref_found = candidate->ref_frame[1] != ref_frame;
224 if (candidate->ref_frame[1] == ref_frame)
225 // Add second motion vector if it has the same ref_frame.
226 ADD_MV_REF_LIST(get_sub_block_mv(candidate_mi, 1,
227 mv_ref->col, block_idx));
228 different_ref_found = 1;
233 // Check the rest of the neighbors in much the same way
234 // as before except we don't need to keep track of sub blocks or
236 for (; i < MVREF_NEIGHBOURS; ++i) {
237 const POSITION *const mv_ref = &mv_ref_search[i];
238 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
239 const MB_MODE_INFO *const candidate = &xd->mi_8x8[mv_ref->col +
241 * xd->mode_info_stride]->mbmi;
243 if (candidate->ref_frame[0] == ref_frame) {
244 ADD_MV_REF_LIST(candidate->mv[0]);
245 different_ref_found = candidate->ref_frame[1] != ref_frame;
247 if (candidate->ref_frame[1] == ref_frame)
248 ADD_MV_REF_LIST(candidate->mv[1]);
249 different_ref_found = 1;
254 // Check the last frame's mode and mv info.
256 if (prev_mbmi->ref_frame[0] == ref_frame)
257 ADD_MV_REF_LIST(prev_mbmi->mv[0]);
258 else if (prev_mbmi->ref_frame[1] == ref_frame)
259 ADD_MV_REF_LIST(prev_mbmi->mv[1]);
262 // Since we couldn't find 2 mvs from the same reference frame
263 // go back through the neighbors and find motion vectors from
264 // different reference frames.
265 if (different_ref_found) {
266 for (i = 0; i < MVREF_NEIGHBOURS; ++i) {
267 const POSITION *mv_ref = &mv_ref_search[i];
268 if (is_inside(tile, mi_col, mi_row, cm->mi_rows, mv_ref)) {
269 const MB_MODE_INFO *const candidate = &xd->mi_8x8[mv_ref->col +
271 * xd->mode_info_stride]->mbmi;
273 // If the candidate is INTRA we don't want to consider its mv.
274 if (is_inter_block(candidate))
275 IF_DIFF_REF_FRAME_ADD_MV(candidate);
280 // Since we still don't have a candidate we'll try the last frame.
281 if (prev_mbmi && is_inter_block(prev_mbmi))
282 IF_DIFF_REF_FRAME_ADD_MV(prev_mbmi);
286 mi->mbmi.mode_context[ref_frame] = counter_to_context[context_counter];
289 for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
290 clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
293 void vp9_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
294 const TileInfo *const tile,
295 MODE_INFO *mi, const MODE_INFO *prev_mi,
296 MV_REFERENCE_FRAME ref_frame,
298 int mi_row, int mi_col) {
299 find_mv_refs_idx(cm, xd, tile, mi, prev_mi, ref_frame, mv_ref_list, -1,
303 static void lower_mv_precision(MV *mv, int allow_hp) {
304 const int use_hp = allow_hp && vp9_use_mv_hp(mv);
307 mv->row += (mv->row > 0 ? -1 : 1);
309 mv->col += (mv->col > 0 ? -1 : 1);
314 void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp,
315 int_mv *mvlist, int_mv *nearest, int_mv *near) {
317 // Make sure all the candidates are properly clamped etc
318 for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i) {
319 lower_mv_precision(&mvlist[i].as_mv, allow_hp);
320 clamp_mv2(&mvlist[i].as_mv, xd);
322 *nearest = mvlist[0];
326 void vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd,
327 const TileInfo *const tile,
328 int block, int ref, int mi_row, int mi_col,
329 int_mv *nearest, int_mv *near) {
330 int_mv mv_list[MAX_MV_REF_CANDIDATES];
331 MODE_INFO *const mi = xd->mi_8x8[0];
332 b_mode_info *bmi = mi->bmi;
335 assert(MAX_MV_REF_CANDIDATES == 2);
337 find_mv_refs_idx(cm, xd, tile, mi, xd->last_mi, mi->mbmi.ref_frame[ref],
338 mv_list, block, mi_row, mi_col);
343 nearest->as_int = mv_list[0].as_int;
344 near->as_int = mv_list[1].as_int;
348 nearest->as_int = bmi[0].as_mv[ref].as_int;
349 for (n = 0; n < MAX_MV_REF_CANDIDATES; ++n)
350 if (nearest->as_int != mv_list[n].as_int) {
351 near->as_int = mv_list[n].as_int;
356 int_mv candidates[2 + MAX_MV_REF_CANDIDATES];
357 candidates[0] = bmi[1].as_mv[ref];
358 candidates[1] = bmi[0].as_mv[ref];
359 candidates[2] = mv_list[0];
360 candidates[3] = mv_list[1];
362 nearest->as_int = bmi[2].as_mv[ref].as_int;
363 for (n = 0; n < 2 + MAX_MV_REF_CANDIDATES; ++n)
364 if (nearest->as_int != candidates[n].as_int) {
365 near->as_int = candidates[n].as_int;
371 assert("Invalid block index.");