Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / common / vp9_mvref_common.h
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 VP9_COMMON_VP9_MVREF_COMMON_H_
11 #define 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 ((VP9_ENC_BORDER_IN_PIXELS -\
22                                 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}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
92   // 4X8
93   {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
94   // 8X4
95   {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
96   // 8X8
97   {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
98   // 8X16
99   {{0, -1}, {-1, 0}, {1, -1}, {-1, -1}, {0, -2}, {-2, 0}, {-2, -1}, {-1, -2}},
100   // 16X8
101   {{-1, 0}, {0, -1}, {-1, 1}, {-1, -1}, {-2, 0}, {0, -2}, {-1, -2}, {-2, -1}},
102   // 16X16
103   {{-1, 0}, {0, -1}, {-1, 1}, {1, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
104   // 16X32
105   {{0, -1}, {-1, 0}, {2, -1}, {-1, -1}, {-1, 1}, {0, -3}, {-3, 0}, {-3, -3}},
106   // 32X16
107   {{-1, 0}, {0, -1}, {-1, 2}, {-1, -1}, {1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
108   // 32X32
109   {{-1, 1}, {1, -1}, {-1, 2}, {2, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
110   // 32X64
111   {{0, -1}, {-1, 0}, {4, -1}, {-1, 2}, {-1, -1}, {0, -3}, {-3, 0}, {2, -1}},
112   // 64X32
113   {{-1, 0}, {0, -1}, {-1, 4}, {2, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-1, 2}},
114   // 64X64
115   {{-1, 3}, {3, -1}, {-1, 4}, {4, -1}, {-1, -1}, {-1, 0}, {0, -1}, {-1, 6}}
116 };
117
118 static const int idx_n_column_to_subblock[4][2] = {
119   {1, 2},
120   {1, 3},
121   {3, 2},
122   {3, 3}
123 };
124
125 // clamp_mv_ref
126 #define MV_BORDER (16 << 3)  // Allow 16 pels in 1/8th pel units
127
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);
133 }
134
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]]
141               .as_mv[which_mv]
142           : candidate->mbmi.mv[which_mv];
143 }
144
145
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]) {
152     mv.as_mv.row *= -1;
153     mv.as_mv.col *= -1;
154   }
155   return mv;
156 }
157
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) \
162   do { \
163     if (refmv_count) { \
164       if ((mv).as_int != mv_ref_list[0].as_int) { \
165         mv_ref_list[refmv_count] = (mv); \
166         goto Done; \
167       } \
168     } else { \
169       mv_ref_list[refmv_count++] = (mv); \
170     } \
171   } while (0)
172
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) \
176   do { \
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)); \
184     } \
185   } while (0)
186
187
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);
197 }
198
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);
205 }
206
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);
211
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);
217
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);
222
223 #ifdef __cplusplus
224 }  // extern "C"
225 #endif
226
227 #endif  // VP9_COMMON_VP9_MVREF_COMMON_H_