Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / common / vp9_blockd.h
1 /*
2  *  Copyright (c) 2010 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
11
12 #ifndef VP9_COMMON_VP9_BLOCKD_H_
13 #define VP9_COMMON_VP9_BLOCKD_H_
14
15 #include "./vpx_config.h"
16
17 #include "vpx_ports/mem.h"
18 #include "vpx_scale/yv12config.h"
19
20 #include "vp9/common/vp9_common.h"
21 #include "vp9/common/vp9_common_data.h"
22 #include "vp9/common/vp9_enums.h"
23 #include "vp9/common/vp9_filter.h"
24 #include "vp9/common/vp9_mv.h"
25 #include "vp9/common/vp9_scale.h"
26 #include "vp9/common/vp9_seg_common.h"
27
28 #define BLOCK_SIZE_GROUPS 4
29 #define MBSKIP_CONTEXTS 3
30 #define INTER_MODE_CONTEXTS 7
31
32 /* Segment Feature Masks */
33 #define MAX_MV_REF_CANDIDATES 2
34
35 #define INTRA_INTER_CONTEXTS 4
36 #define COMP_INTER_CONTEXTS 5
37 #define REF_CONTEXTS 5
38
39 typedef enum {
40   PLANE_TYPE_Y  = 0,
41   PLANE_TYPE_UV = 1,
42   PLANE_TYPES
43 } PLANE_TYPE;
44
45 typedef char ENTROPY_CONTEXT;
46
47 typedef char PARTITION_CONTEXT;
48
49 static INLINE int combine_entropy_contexts(ENTROPY_CONTEXT a,
50                                            ENTROPY_CONTEXT b) {
51   return (a != 0) + (b != 0);
52 }
53
54 typedef enum {
55   KEY_FRAME = 0,
56   INTER_FRAME = 1,
57   FRAME_TYPES,
58 } FRAME_TYPE;
59
60 typedef enum {
61   DC_PRED,         // Average of above and left pixels
62   V_PRED,          // Vertical
63   H_PRED,          // Horizontal
64   D45_PRED,        // Directional 45  deg = round(arctan(1/1) * 180/pi)
65   D135_PRED,       // Directional 135 deg = 180 - 45
66   D117_PRED,       // Directional 117 deg = 180 - 63
67   D153_PRED,       // Directional 153 deg = 180 - 27
68   D207_PRED,       // Directional 207 deg = 180 + 27
69   D63_PRED,        // Directional 63  deg = round(arctan(2/1) * 180/pi)
70   TM_PRED,         // True-motion
71   NEARESTMV,
72   NEARMV,
73   ZEROMV,
74   NEWMV,
75   MB_MODE_COUNT
76 } MB_PREDICTION_MODE;
77
78 static INLINE int is_inter_mode(MB_PREDICTION_MODE mode) {
79   return mode >= NEARESTMV && mode <= NEWMV;
80 }
81
82 #define INTRA_MODES (TM_PRED + 1)
83
84 #define INTER_MODES (1 + NEWMV - NEARESTMV)
85
86 #define INTER_OFFSET(mode) ((mode) - NEARESTMV)
87
88
89 /* For keyframes, intra block modes are predicted by the (already decoded)
90    modes for the Y blocks to the left and above us; for interframes, there
91    is a single probability table. */
92
93 typedef struct {
94   MB_PREDICTION_MODE as_mode;
95   int_mv as_mv[2];  // first, second inter predictor motion vectors
96 } b_mode_info;
97
98 typedef enum {
99   NONE = -1,
100   INTRA_FRAME = 0,
101   LAST_FRAME = 1,
102   GOLDEN_FRAME = 2,
103   ALTREF_FRAME = 3,
104   MAX_REF_FRAMES = 4
105 } MV_REFERENCE_FRAME;
106
107 static INLINE int b_width_log2(BLOCK_SIZE sb_type) {
108   return b_width_log2_lookup[sb_type];
109 }
110 static INLINE int b_height_log2(BLOCK_SIZE sb_type) {
111   return b_height_log2_lookup[sb_type];
112 }
113
114 static INLINE int mi_width_log2(BLOCK_SIZE sb_type) {
115   return mi_width_log2_lookup[sb_type];
116 }
117
118 static INLINE int mi_height_log2(BLOCK_SIZE sb_type) {
119   return mi_height_log2_lookup[sb_type];
120 }
121
122 // This structure now relates to 8x8 block regions.
123 typedef struct {
124   MB_PREDICTION_MODE mode, uv_mode;
125   MV_REFERENCE_FRAME ref_frame[2];
126   TX_SIZE tx_size;
127   int_mv mv[2];                // for each reference frame used
128   int_mv ref_mvs[MAX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
129   int_mv best_mv[2];
130
131   uint8_t mode_context[MAX_REF_FRAMES];
132
133   unsigned char skip_coeff;    // 0=need to decode coeffs, 1=no coefficients
134   unsigned char segment_id;    // Segment id for this block.
135
136   // Flags used for prediction status of various bit-stream signals
137   unsigned char seg_id_predicted;
138
139   INTERPOLATION_TYPE interp_filter;
140
141   BLOCK_SIZE sb_type;
142 } MB_MODE_INFO;
143
144 typedef struct {
145   MB_MODE_INFO mbmi;
146   b_mode_info bmi[4];
147 } MODE_INFO;
148
149 static INLINE int is_inter_block(const MB_MODE_INFO *mbmi) {
150   return mbmi->ref_frame[0] > INTRA_FRAME;
151 }
152
153 static INLINE int has_second_ref(const MB_MODE_INFO *mbmi) {
154   return mbmi->ref_frame[1] > INTRA_FRAME;
155 }
156
157 static MB_PREDICTION_MODE left_block_mode(const MODE_INFO *cur_mi,
158                                           const MODE_INFO *left_mi, int b) {
159   if (b == 0 || b == 2) {
160     if (!left_mi || is_inter_block(&left_mi->mbmi))
161       return DC_PRED;
162
163     return left_mi->mbmi.sb_type < BLOCK_8X8 ? left_mi->bmi[b + 1].as_mode
164                                              : left_mi->mbmi.mode;
165   } else {
166     assert(b == 1 || b == 3);
167     return cur_mi->bmi[b - 1].as_mode;
168   }
169 }
170
171 static MB_PREDICTION_MODE above_block_mode(const MODE_INFO *cur_mi,
172                                            const MODE_INFO *above_mi, int b) {
173   if (b == 0 || b == 1) {
174     if (!above_mi || is_inter_block(&above_mi->mbmi))
175       return DC_PRED;
176
177     return above_mi->mbmi.sb_type < BLOCK_8X8 ? above_mi->bmi[b + 2].as_mode
178                                               : above_mi->mbmi.mode;
179   } else {
180     assert(b == 2 || b == 3);
181     return cur_mi->bmi[b - 2].as_mode;
182   }
183 }
184
185 enum mv_precision {
186   MV_PRECISION_Q3,
187   MV_PRECISION_Q4
188 };
189
190 #if CONFIG_ALPHA
191 enum { MAX_MB_PLANE = 4 };
192 #else
193 enum { MAX_MB_PLANE = 3 };
194 #endif
195
196 struct buf_2d {
197   uint8_t *buf;
198   int stride;
199 };
200
201 struct macroblockd_plane {
202   int16_t *dqcoeff;
203   PLANE_TYPE plane_type;
204   int subsampling_x;
205   int subsampling_y;
206   struct buf_2d dst;
207   struct buf_2d pre[2];
208   int16_t *dequant;
209   ENTROPY_CONTEXT *above_context;
210   ENTROPY_CONTEXT *left_context;
211 };
212
213 #define BLOCK_OFFSET(x, i) ((x) + (i) * 16)
214
215 typedef struct macroblockd {
216   struct macroblockd_plane plane[MAX_MB_PLANE];
217
218   const struct scale_factors *scale_factors[2];
219
220   MODE_INFO *last_mi;
221   int mode_info_stride;
222
223   // A NULL indicates that the 8x8 is not part of the image
224   MODE_INFO **mi_8x8;
225   MODE_INFO **prev_mi_8x8;
226   MODE_INFO *mi_stream;
227
228   int up_available;
229   int left_available;
230
231   /* Distance of MB away from frame edges */
232   int mb_to_left_edge;
233   int mb_to_right_edge;
234   int mb_to_top_edge;
235   int mb_to_bottom_edge;
236
237   /* pointers to reference frames */
238   const YV12_BUFFER_CONFIG *ref_buf[2];
239
240   /* pointer to current frame */
241   const YV12_BUFFER_CONFIG *cur_buf;
242
243   int lossless;
244   /* Inverse transform function pointers. */
245   void (*itxm_add)(const int16_t *input, uint8_t *dest, int stride, int eob);
246
247   struct subpix_fn_table  subpix;
248
249   int corrupted;
250
251   /* Y,U,V,(A) */
252   ENTROPY_CONTEXT *above_context[MAX_MB_PLANE];
253   ENTROPY_CONTEXT left_context[MAX_MB_PLANE][16];
254
255   PARTITION_CONTEXT *above_seg_context;
256   PARTITION_CONTEXT left_seg_context[8];
257 } MACROBLOCKD;
258
259
260
261 static BLOCK_SIZE get_subsize(BLOCK_SIZE bsize, PARTITION_TYPE partition) {
262   const BLOCK_SIZE subsize = subsize_lookup[partition][bsize];
263   assert(subsize < BLOCK_SIZES);
264   return subsize;
265 }
266
267 extern const TX_TYPE mode2txfm_map[MB_MODE_COUNT];
268
269 static INLINE TX_TYPE get_tx_type_4x4(PLANE_TYPE plane_type,
270                                       const MACROBLOCKD *xd, int ib) {
271   const MODE_INFO *const mi = xd->mi_8x8[0];
272   const MB_MODE_INFO *const mbmi = &mi->mbmi;
273
274   if (plane_type != PLANE_TYPE_Y || xd->lossless || is_inter_block(mbmi))
275     return DCT_DCT;
276
277   return mode2txfm_map[mbmi->sb_type < BLOCK_8X8 ? mi->bmi[ib].as_mode
278                                                  : mbmi->mode];
279 }
280
281 static INLINE TX_TYPE get_tx_type_8x8(PLANE_TYPE plane_type,
282                                       const MACROBLOCKD *xd) {
283   return plane_type == PLANE_TYPE_Y ? mode2txfm_map[xd->mi_8x8[0]->mbmi.mode]
284                                     : DCT_DCT;
285 }
286
287 static INLINE TX_TYPE get_tx_type_16x16(PLANE_TYPE plane_type,
288                                         const MACROBLOCKD *xd) {
289   return plane_type == PLANE_TYPE_Y ? mode2txfm_map[xd->mi_8x8[0]->mbmi.mode]
290                                     : DCT_DCT;
291 }
292
293 static void setup_block_dptrs(MACROBLOCKD *xd, int ss_x, int ss_y) {
294   int i;
295
296   for (i = 0; i < MAX_MB_PLANE; i++) {
297     xd->plane[i].plane_type = i ? PLANE_TYPE_UV : PLANE_TYPE_Y;
298     xd->plane[i].subsampling_x = i ? ss_x : 0;
299     xd->plane[i].subsampling_y = i ? ss_y : 0;
300   }
301 #if CONFIG_ALPHA
302   // TODO(jkoleszar): Using the Y w/h for now
303   xd->plane[3].plane_type = PLANE_TYPE_Y;
304   xd->plane[3].subsampling_x = 0;
305   xd->plane[3].subsampling_y = 0;
306 #endif
307 }
308
309 static TX_SIZE get_uv_tx_size_impl(TX_SIZE y_tx_size, BLOCK_SIZE bsize) {
310   if (bsize < BLOCK_8X8) {
311     return TX_4X4;
312   } else {
313     // TODO(dkovalev): Assuming YUV420 (ss_x == 1, ss_y == 1)
314     const BLOCK_SIZE plane_bsize = ss_size_lookup[bsize][1][1];
315     return MIN(y_tx_size, max_txsize_lookup[plane_bsize]);
316   }
317 }
318
319 static TX_SIZE get_uv_tx_size(const MB_MODE_INFO *mbmi) {
320   return get_uv_tx_size_impl(mbmi->tx_size, mbmi->sb_type);
321 }
322
323 static BLOCK_SIZE get_plane_block_size(BLOCK_SIZE bsize,
324                                        const struct macroblockd_plane *pd) {
325   BLOCK_SIZE bs = ss_size_lookup[bsize][pd->subsampling_x][pd->subsampling_y];
326   assert(bs < BLOCK_SIZES);
327   return bs;
328 }
329
330 typedef void (*foreach_transformed_block_visitor)(int plane, int block,
331                                                   BLOCK_SIZE plane_bsize,
332                                                   TX_SIZE tx_size,
333                                                   void *arg);
334
335 static INLINE void foreach_transformed_block_in_plane(
336     const MACROBLOCKD *const xd, BLOCK_SIZE bsize, int plane,
337     foreach_transformed_block_visitor visit, void *arg) {
338   const struct macroblockd_plane *const pd = &xd->plane[plane];
339   const MB_MODE_INFO* mbmi = &xd->mi_8x8[0]->mbmi;
340   // block and transform sizes, in number of 4x4 blocks log 2 ("*_b")
341   // 4x4=0, 8x8=2, 16x16=4, 32x32=6, 64x64=8
342   // transform size varies per plane, look it up in a common way.
343   const TX_SIZE tx_size = plane ? get_uv_tx_size(mbmi)
344                                 : mbmi->tx_size;
345   const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
346   const int num_4x4_w = num_4x4_blocks_wide_lookup[plane_bsize];
347   const int num_4x4_h = num_4x4_blocks_high_lookup[plane_bsize];
348   const int step = 1 << (tx_size << 1);
349   int i;
350
351   // If mb_to_right_edge is < 0 we are in a situation in which
352   // the current block size extends into the UMV and we won't
353   // visit the sub blocks that are wholly within the UMV.
354   if (xd->mb_to_right_edge < 0 || xd->mb_to_bottom_edge < 0) {
355     int r, c;
356
357     int max_blocks_wide = num_4x4_w;
358     int max_blocks_high = num_4x4_h;
359
360     // xd->mb_to_right_edge is in units of pixels * 8.  This converts
361     // it to 4x4 block sizes.
362     if (xd->mb_to_right_edge < 0)
363       max_blocks_wide += (xd->mb_to_right_edge >> (5 + pd->subsampling_x));
364
365     if (xd->mb_to_bottom_edge < 0)
366       max_blocks_high += (xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
367
368     i = 0;
369     // Unlike the normal case - in here we have to keep track of the
370     // row and column of the blocks we use so that we know if we are in
371     // the unrestricted motion border.
372     for (r = 0; r < num_4x4_h; r += (1 << tx_size)) {
373       for (c = 0; c < num_4x4_w; c += (1 << tx_size)) {
374         if (r < max_blocks_high && c < max_blocks_wide)
375           visit(plane, i, plane_bsize, tx_size, arg);
376         i += step;
377       }
378     }
379   } else {
380     for (i = 0; i < num_4x4_w * num_4x4_h; i += step)
381       visit(plane, i, plane_bsize, tx_size, arg);
382   }
383 }
384
385 static INLINE void foreach_transformed_block(
386     const MACROBLOCKD* const xd, BLOCK_SIZE bsize,
387     foreach_transformed_block_visitor visit, void *arg) {
388   int plane;
389
390   for (plane = 0; plane < MAX_MB_PLANE; plane++)
391     foreach_transformed_block_in_plane(xd, bsize, plane, visit, arg);
392 }
393
394 static INLINE void foreach_transformed_block_uv(
395     const MACROBLOCKD* const xd, BLOCK_SIZE bsize,
396     foreach_transformed_block_visitor visit, void *arg) {
397   int plane;
398
399   for (plane = 1; plane < MAX_MB_PLANE; plane++)
400     foreach_transformed_block_in_plane(xd, bsize, plane, visit, arg);
401 }
402
403 static void txfrm_block_to_raster_xy(BLOCK_SIZE plane_bsize,
404                                      TX_SIZE tx_size, int block,
405                                      int *x, int *y) {
406   const int bwl = b_width_log2(plane_bsize);
407   const int tx_cols_log2 = bwl - tx_size;
408   const int tx_cols = 1 << tx_cols_log2;
409   const int raster_mb = block >> (tx_size << 1);
410   *x = (raster_mb & (tx_cols - 1)) << tx_size;
411   *y = (raster_mb >> tx_cols_log2) << tx_size;
412 }
413
414 static void set_contexts(const MACROBLOCKD *xd, struct macroblockd_plane *pd,
415                          BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
416                          int has_eob, int aoff, int loff) {
417   ENTROPY_CONTEXT *const a = pd->above_context + aoff;
418   ENTROPY_CONTEXT *const l = pd->left_context + loff;
419   const int tx_size_in_blocks = 1 << tx_size;
420
421   // above
422   if (has_eob && xd->mb_to_right_edge < 0) {
423     int i;
424     const int blocks_wide = num_4x4_blocks_wide_lookup[plane_bsize] +
425                             (xd->mb_to_right_edge >> (5 + pd->subsampling_x));
426     int above_contexts = tx_size_in_blocks;
427     if (above_contexts + aoff > blocks_wide)
428       above_contexts = blocks_wide - aoff;
429
430     for (i = 0; i < above_contexts; ++i)
431       a[i] = has_eob;
432     for (i = above_contexts; i < tx_size_in_blocks; ++i)
433       a[i] = 0;
434   } else {
435     vpx_memset(a, has_eob, sizeof(ENTROPY_CONTEXT) * tx_size_in_blocks);
436   }
437
438   // left
439   if (has_eob && xd->mb_to_bottom_edge < 0) {
440     int i;
441     const int blocks_high = num_4x4_blocks_high_lookup[plane_bsize] +
442                             (xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
443     int left_contexts = tx_size_in_blocks;
444     if (left_contexts + loff > blocks_high)
445       left_contexts = blocks_high - loff;
446
447     for (i = 0; i < left_contexts; ++i)
448       l[i] = has_eob;
449     for (i = left_contexts; i < tx_size_in_blocks; ++i)
450       l[i] = 0;
451   } else {
452     vpx_memset(l, has_eob, sizeof(ENTROPY_CONTEXT) * tx_size_in_blocks);
453   }
454 }
455
456 static int get_tx_eob(const struct segmentation *seg, int segment_id,
457                       TX_SIZE tx_size) {
458   const int eob_max = 16 << (tx_size << 1);
459   return vp9_segfeature_active(seg, segment_id, SEG_LVL_SKIP) ? 0 : eob_max;
460 }
461
462 #endif  // VP9_COMMON_VP9_BLOCKD_H_