• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3  *
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.
9  */
10 #ifndef VPX_VP9_COMMON_VP9_MVREF_COMMON_H_
11 #define VPX_VP9_COMMON_VP9_MVREF_COMMON_H_
12 
13 #include "vp9/common/vp9_onyxc_int.h"
14 #include "vp9/common/vp9_blockd.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #define LEFT_TOP_MARGIN ((VP9_ENC_BORDER_IN_PIXELS - VP9_INTERP_EXTEND) << 3)
21 #define RIGHT_BOTTOM_MARGIN \
22   ((VP9_ENC_BORDER_IN_PIXELS - VP9_INTERP_EXTEND) << 3)
23 
24 #define MVREF_NEIGHBOURS 8
25 
26 typedef struct position {
27   int row;
28   int col;
29 } POSITION;
30 
31 typedef enum {
32   BOTH_ZERO = 0,
33   ZERO_PLUS_PREDICTED = 1,
34   BOTH_PREDICTED = 2,
35   NEW_PLUS_NON_INTRA = 3,
36   BOTH_NEW = 4,
37   INTRA_PLUS_NON_INTRA = 5,
38   BOTH_INTRA = 6,
39   INVALID_CASE = 9
40 } motion_vector_context;
41 
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] = {
48   9,  // DC_PRED
49   9,  // V_PRED
50   9,  // H_PRED
51   9,  // D45_PRED
52   9,  // D135_PRED
53   9,  // D117_PRED
54   9,  // D153_PRED
55   9,  // D207_PRED
56   9,  // D63_PRED
57   9,  // TM_PRED
58   0,  // NEARESTMV
59   0,  // NEARMV
60   3,  // ZEROMV
61   1,  // NEWMV
62 };
63 
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] = {
68   BOTH_PREDICTED,        // 0
69   NEW_PLUS_NON_INTRA,    // 1
70   BOTH_NEW,              // 2
71   ZERO_PLUS_PREDICTED,   // 3
72   NEW_PLUS_NON_INTRA,    // 4
73   INVALID_CASE,          // 5
74   BOTH_ZERO,             // 6
75   INVALID_CASE,          // 7
76   INVALID_CASE,          // 8
77   INTRA_PLUS_NON_INTRA,  // 9
78   INTRA_PLUS_NON_INTRA,  // 10
79   INVALID_CASE,          // 11
80   INTRA_PLUS_NON_INTRA,  // 12
81   INVALID_CASE,          // 13
82   INVALID_CASE,          // 14
83   INVALID_CASE,          // 15
84   INVALID_CASE,          // 16
85   INVALID_CASE,          // 17
86   BOTH_INTRA             // 18
87 };
88 
89 static const POSITION mv_ref_blocks[BLOCK_SIZES][MVREF_NEIGHBOURS] = {
90   // 4X4
91   { { -1, 0 },
92     { 0, -1 },
93     { -1, -1 },
94     { -2, 0 },
95     { 0, -2 },
96     { -2, -1 },
97     { -1, -2 },
98     { -2, -2 } },
99   // 4X8
100   { { -1, 0 },
101     { 0, -1 },
102     { -1, -1 },
103     { -2, 0 },
104     { 0, -2 },
105     { -2, -1 },
106     { -1, -2 },
107     { -2, -2 } },
108   // 8X4
109   { { -1, 0 },
110     { 0, -1 },
111     { -1, -1 },
112     { -2, 0 },
113     { 0, -2 },
114     { -2, -1 },
115     { -1, -2 },
116     { -2, -2 } },
117   // 8X8
118   { { -1, 0 },
119     { 0, -1 },
120     { -1, -1 },
121     { -2, 0 },
122     { 0, -2 },
123     { -2, -1 },
124     { -1, -2 },
125     { -2, -2 } },
126   // 8X16
127   { { 0, -1 },
128     { -1, 0 },
129     { 1, -1 },
130     { -1, -1 },
131     { 0, -2 },
132     { -2, 0 },
133     { -2, -1 },
134     { -1, -2 } },
135   // 16X8
136   { { -1, 0 },
137     { 0, -1 },
138     { -1, 1 },
139     { -1, -1 },
140     { -2, 0 },
141     { 0, -2 },
142     { -1, -2 },
143     { -2, -1 } },
144   // 16X16
145   { { -1, 0 },
146     { 0, -1 },
147     { -1, 1 },
148     { 1, -1 },
149     { -1, -1 },
150     { -3, 0 },
151     { 0, -3 },
152     { -3, -3 } },
153   // 16X32
154   { { 0, -1 },
155     { -1, 0 },
156     { 2, -1 },
157     { -1, -1 },
158     { -1, 1 },
159     { 0, -3 },
160     { -3, 0 },
161     { -3, -3 } },
162   // 32X16
163   { { -1, 0 },
164     { 0, -1 },
165     { -1, 2 },
166     { -1, -1 },
167     { 1, -1 },
168     { -3, 0 },
169     { 0, -3 },
170     { -3, -3 } },
171   // 32X32
172   { { -1, 1 },
173     { 1, -1 },
174     { -1, 2 },
175     { 2, -1 },
176     { -1, -1 },
177     { -3, 0 },
178     { 0, -3 },
179     { -3, -3 } },
180   // 32X64
181   { { 0, -1 },
182     { -1, 0 },
183     { 4, -1 },
184     { -1, 2 },
185     { -1, -1 },
186     { 0, -3 },
187     { -3, 0 },
188     { 2, -1 } },
189   // 64X32
190   { { -1, 0 },
191     { 0, -1 },
192     { -1, 4 },
193     { 2, -1 },
194     { -1, -1 },
195     { -3, 0 },
196     { 0, -3 },
197     { -1, 2 } },
198   // 64X64
199   { { -1, 3 },
200     { 3, -1 },
201     { -1, 4 },
202     { 4, -1 },
203     { -1, -1 },
204     { -1, 0 },
205     { 0, -1 },
206     { -1, 6 } }
207 };
208 
209 static const int idx_n_column_to_subblock[4][2] = {
210   { 1, 2 }, { 1, 3 }, { 3, 2 }, { 3, 3 }
211 };
212 
213 // clamp_mv_ref
214 #define MV_BORDER (16 << 3)  // Allow 16 pels in 1/8th pel units
215 
clamp_mv_ref(MV * mv,const MACROBLOCKD * xd)216 static INLINE void clamp_mv_ref(MV *mv, const MACROBLOCKD *xd) {
217   clamp_mv(mv, xd->mb_to_left_edge - MV_BORDER,
218            xd->mb_to_right_edge + MV_BORDER, xd->mb_to_top_edge - MV_BORDER,
219            xd->mb_to_bottom_edge + MV_BORDER);
220 }
221 
222 // This function returns either the appropriate sub block or block's mv
223 // on whether the block_size < 8x8 and we have check_sub_blocks set.
get_sub_block_mv(const MODE_INFO * candidate,int which_mv,int search_col,int block_idx)224 static INLINE int_mv get_sub_block_mv(const MODE_INFO *candidate, int which_mv,
225                                       int search_col, int block_idx) {
226   return block_idx >= 0 && candidate->sb_type < BLOCK_8X8
227              ? candidate
228                    ->bmi[idx_n_column_to_subblock[block_idx][search_col == 0]]
229                    .as_mv[which_mv]
230              : candidate->mv[which_mv];
231 }
232 
233 // Performs mv sign inversion if indicated by the reference frame combination.
scale_mv(const MODE_INFO * mi,int ref,const MV_REFERENCE_FRAME this_ref_frame,const int * ref_sign_bias)234 static INLINE int_mv scale_mv(const MODE_INFO *mi, int ref,
235                               const MV_REFERENCE_FRAME this_ref_frame,
236                               const int *ref_sign_bias) {
237   int_mv mv = mi->mv[ref];
238   if (ref_sign_bias[mi->ref_frame[ref]] != ref_sign_bias[this_ref_frame]) {
239     mv.as_mv.row *= -1;
240     mv.as_mv.col *= -1;
241   }
242   return mv;
243 }
244 
245 // This macro is used to add a motion vector mv_ref list if it isn't
246 // already in the list.  If it's the second motion vector it will also
247 // skip all additional processing and jump to Done!
248 #define ADD_MV_REF_LIST(mv, refmv_count, mv_ref_list, Done) \
249   do {                                                      \
250     if (refmv_count) {                                      \
251       if ((mv).as_int != (mv_ref_list)[0].as_int) {         \
252         (mv_ref_list)[(refmv_count)] = (mv);                \
253         goto Done;                                          \
254       }                                                     \
255     } else {                                                \
256       (mv_ref_list)[(refmv_count)++] = (mv);                \
257     }                                                       \
258   } while (0)
259 
260 // If either reference frame is different, not INTRA, and they
261 // are different from each other scale and add the mv to our list.
262 #define IF_DIFF_REF_FRAME_ADD_MV(mbmi, ref_frame, ref_sign_bias, refmv_count, \
263                                  mv_ref_list, Done)                           \
264   do {                                                                        \
265     if (is_inter_block(mbmi)) {                                               \
266       if ((mbmi)->ref_frame[0] != (ref_frame))                                \
267         ADD_MV_REF_LIST(scale_mv((mbmi), 0, ref_frame, ref_sign_bias),        \
268                         refmv_count, mv_ref_list, Done);                      \
269       if (has_second_ref(mbmi) && (mbmi)->ref_frame[1] != (ref_frame) &&      \
270           (mbmi)->mv[1].as_int != (mbmi)->mv[0].as_int)                       \
271         ADD_MV_REF_LIST(scale_mv((mbmi), 1, ref_frame, ref_sign_bias),        \
272                         refmv_count, mv_ref_list, Done);                      \
273     }                                                                         \
274   } while (0)
275 
276 // Checks that the given mi_row, mi_col and search point
277 // are inside the borders of the tile.
is_inside(const TileInfo * const tile,int mi_col,int mi_row,int mi_rows,const POSITION * mi_pos)278 static INLINE int is_inside(const TileInfo *const tile, int mi_col, int mi_row,
279                             int mi_rows, const POSITION *mi_pos) {
280   return !(mi_row + mi_pos->row < 0 ||
281            mi_col + mi_pos->col < tile->mi_col_start ||
282            mi_row + mi_pos->row >= mi_rows ||
283            mi_col + mi_pos->col >= tile->mi_col_end);
284 }
285 
286 // TODO(jingning): this mv clamping function should be block size dependent.
clamp_mv2(MV * mv,const MACROBLOCKD * xd)287 static INLINE void clamp_mv2(MV *mv, const MACROBLOCKD *xd) {
288   clamp_mv(mv, xd->mb_to_left_edge - LEFT_TOP_MARGIN,
289            xd->mb_to_right_edge + RIGHT_BOTTOM_MARGIN,
290            xd->mb_to_top_edge - LEFT_TOP_MARGIN,
291            xd->mb_to_bottom_edge + RIGHT_BOTTOM_MARGIN);
292 }
293 
lower_mv_precision(MV * mv,int allow_hp)294 static INLINE void lower_mv_precision(MV *mv, int allow_hp) {
295   const int use_hp = allow_hp && use_mv_hp(mv);
296   if (!use_hp) {
297     if (mv->row & 1) mv->row += (mv->row > 0 ? -1 : 1);
298     if (mv->col & 1) mv->col += (mv->col > 0 ? -1 : 1);
299   }
300 }
301 
302 typedef void (*find_mv_refs_sync)(void *const data, int mi_row);
303 void vp9_find_mv_refs(const VP9_COMMON *cm, const MACROBLOCKD *xd,
304                       MODE_INFO *mi, MV_REFERENCE_FRAME ref_frame,
305                       int_mv *mv_ref_list, int mi_row, int mi_col,
306                       uint8_t *mode_context);
307 
308 // check a list of motion vectors by sad score using a number rows of pixels
309 // above and a number cols of pixels in the left to select the one with best
310 // score to use as ref motion vector
311 void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp, int_mv *mvlist,
312                            int_mv *nearest_mv, int_mv *near_mv);
313 
314 void vp9_append_sub8x8_mvs_for_idx(VP9_COMMON *cm, MACROBLOCKD *xd, int block,
315                                    int ref, int mi_row, int mi_col,
316                                    int_mv *nearest_mv, int_mv *near_mv,
317                                    uint8_t *mode_context);
318 
319 #ifdef __cplusplus
320 }  // extern "C"
321 #endif
322 
323 #endif  // VPX_VP9_COMMON_VP9_MVREF_COMMON_H_
324