Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / common / vp9_mvref_common.c
1
2 /*
3  *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
4  *
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.
10  */
11
12 #include "vp9/common/vp9_mvref_common.h"
13
14 #define MVREF_NEIGHBOURS 8
15
16 typedef struct position {
17   int row;
18   int col;
19 } POSITION;
20
21 typedef enum {
22   BOTH_ZERO = 0,
23   ZERO_PLUS_PREDICTED = 1,
24   BOTH_PREDICTED = 2,
25   NEW_PLUS_NON_INTRA = 3,
26   BOTH_NEW = 4,
27   INTRA_PLUS_NON_INTRA = 5,
28   BOTH_INTRA = 6,
29   INVALID_CASE = 9
30 } motion_vector_context;
31
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] = {
38   9,  // DC_PRED
39   9,  // V_PRED
40   9,  // H_PRED
41   9,  // D45_PRED
42   9,  // D135_PRED
43   9,  // D117_PRED
44   9,  // D153_PRED
45   9,  // D207_PRED
46   9,  // D63_PRED
47   9,  // TM_PRED
48   0,  // NEARESTMV
49   0,  // NEARMV
50   3,  // ZEROMV
51   1,  // NEWMV
52 };
53
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] = {
58   BOTH_PREDICTED,  // 0
59   NEW_PLUS_NON_INTRA,  // 1
60   BOTH_NEW,  // 2
61   ZERO_PLUS_PREDICTED,  // 3
62   NEW_PLUS_NON_INTRA,  // 4
63   INVALID_CASE,  // 5
64   BOTH_ZERO,  // 6
65   INVALID_CASE,  // 7
66   INVALID_CASE,  // 8
67   INTRA_PLUS_NON_INTRA,  // 9
68   INTRA_PLUS_NON_INTRA,  // 10
69   INVALID_CASE,  // 11
70   INTRA_PLUS_NON_INTRA,  // 12
71   INVALID_CASE,  // 13
72   INVALID_CASE,  // 14
73   INVALID_CASE,  // 15
74   INVALID_CASE,  // 16
75   INVALID_CASE,  // 17
76   BOTH_INTRA  // 18
77 };
78
79 static const POSITION mv_ref_blocks[BLOCK_SIZES][MVREF_NEIGHBOURS] = {
80   // 4X4
81   {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
82   // 4X8
83   {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
84   // 8X4
85   {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
86   // 8X8
87   {{-1, 0}, {0, -1}, {-1, -1}, {-2, 0}, {0, -2}, {-2, -1}, {-1, -2}, {-2, -2}},
88   // 8X16
89   {{0, -1}, {-1, 0}, {1, -1}, {-1, -1}, {0, -2}, {-2, 0}, {-2, -1}, {-1, -2}},
90   // 16X8
91   {{-1, 0}, {0, -1}, {-1, 1}, {-1, -1}, {-2, 0}, {0, -2}, {-1, -2}, {-2, -1}},
92   // 16X16
93   {{-1, 0}, {0, -1}, {-1, 1}, {1, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
94   // 16X32
95   {{0, -1}, {-1, 0}, {2, -1}, {-1, -1}, {-1, 1}, {0, -3}, {-3, 0}, {-3, -3}},
96   // 32X16
97   {{-1, 0}, {0, -1}, {-1, 2}, {-1, -1}, {1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
98   // 32X32
99   {{-1, 1}, {1, -1}, {-1, 2}, {2, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-3, -3}},
100   // 32X64
101   {{0, -1}, {-1, 0}, {4, -1}, {-1, 2}, {-1, -1}, {0, -3}, {-3, 0}, {2, -1}},
102   // 64X32
103   {{-1, 0}, {0, -1}, {-1, 4}, {2, -1}, {-1, -1}, {-3, 0}, {0, -3}, {-1, 2}},
104   // 64X64
105   {{-1, 3}, {3, -1}, {-1, 4}, {4, -1}, {-1, -1}, {-1, 0}, {0, -1}, {-1, 6}}
106 };
107
108 static const int idx_n_column_to_subblock[4][2] = {
109   {1, 2},
110   {1, 3},
111   {3, 2},
112   {3, 3}
113 };
114
115 // clamp_mv_ref
116 #define MV_BORDER (16 << 3)  // Allow 16 pels in 1/8th pel units
117
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);
123 }
124
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]]
131               .as_mv[which_mv]
132           : candidate->mbmi.mv[which_mv];
133 }
134
135
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]) {
142     mv.as_mv.row *= -1;
143     mv.as_mv.col *= -1;
144   }
145   return mv;
146 }
147
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) \
152   do { \
153     if (refmv_count) { \
154       if ((MV).as_int != mv_ref_list[0].as_int) { \
155         mv_ref_list[refmv_count] = (MV); \
156         goto Done; \
157       } \
158     } else { \
159       mv_ref_list[refmv_count++] = (MV); \
160     } \
161   } while (0)
162
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) \
166   do { \
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)); \
173   } while (0)
174
175
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);
185 }
186
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,
193                              int_mv *mv_ref_list,
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;
202
203   // Blank the reference vector list
204   vpx_memset(mv_ref_list, 0, sizeof(*mv_ref_list) * MAX_MV_REF_CANDIDATES);
205
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];
217
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;
223       } else {
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;
229       }
230     }
231   }
232
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
235   // mode counts.
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 +
240                                             mv_ref->row
241                                             * xd->mode_info_stride]->mbmi;
242
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;
246       } else {
247         if (candidate->ref_frame[1] == ref_frame)
248           ADD_MV_REF_LIST(candidate->mv[1]);
249         different_ref_found = 1;
250       }
251     }
252   }
253
254   // Check the last frame's mode and mv info.
255   if (prev_mbmi) {
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]);
260   }
261
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 +
270                                                           mv_ref->row
271                                               * xd->mode_info_stride]->mbmi;
272
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);
276       }
277     }
278   }
279
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);
283
284  Done:
285
286   mi->mbmi.mode_context[ref_frame] = counter_to_context[context_counter];
287
288   // Clamp vectors
289   for (i = 0; i < MAX_MV_REF_CANDIDATES; ++i)
290     clamp_mv_ref(&mv_ref_list[i].as_mv, xd);
291 }
292
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,
297                                     int_mv *mv_ref_list,
298                                     int mi_row, int mi_col) {
299   find_mv_refs_idx(cm, xd, tile, mi, prev_mi, ref_frame, mv_ref_list, -1,
300                    mi_row, mi_col);
301 }
302
303 static void lower_mv_precision(MV *mv, int allow_hp) {
304   const int use_hp = allow_hp && vp9_use_mv_hp(mv);
305   if (!use_hp) {
306     if (mv->row & 1)
307       mv->row += (mv->row > 0 ? -1 : 1);
308     if (mv->col & 1)
309       mv->col += (mv->col > 0 ? -1 : 1);
310   }
311 }
312
313
314 void vp9_find_best_ref_mvs(MACROBLOCKD *xd, int allow_hp,
315                            int_mv *mvlist, int_mv *nearest, int_mv *near) {
316   int i;
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);
321   }
322   *nearest = mvlist[0];
323   *near = mvlist[1];
324 }
325
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;
333   int n;
334
335   assert(MAX_MV_REF_CANDIDATES == 2);
336
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);
339
340   near->as_int = 0;
341   switch (block) {
342     case 0:
343       nearest->as_int = mv_list[0].as_int;
344       near->as_int = mv_list[1].as_int;
345       break;
346     case 1:
347     case 2:
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;
352           break;
353         }
354       break;
355     case 3: {
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];
361
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;
366           break;
367         }
368       break;
369     }
370     default:
371       assert("Invalid block index.");
372   }
373 }