7cbdfce528b7c2f2411cc0ceb5b91c09ebdc039a
[platform/framework/web/crosswalk.git] / src / third_party / libvpx / source / libvpx / vp9 / encoder / vp9_block.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 #ifndef VP9_ENCODER_VP9_BLOCK_H_
12 #define VP9_ENCODER_VP9_BLOCK_H_
13
14 #include "vp9/common/vp9_onyx.h"
15 #include "vp9/common/vp9_entropymv.h"
16 #include "vp9/common/vp9_entropy.h"
17 #include "vpx_ports/mem.h"
18 #include "vp9/common/vp9_onyxc_int.h"
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 // motion search site
25 typedef struct {
26   MV mv;
27   int offset;
28 } search_site;
29
30 // Structure to hold snapshot of coding context during the mode picking process
31 typedef struct {
32   MODE_INFO mic;
33   uint8_t *zcoeff_blk;
34   int16_t *coeff[MAX_MB_PLANE][3];
35   int16_t *qcoeff[MAX_MB_PLANE][3];
36   int16_t *dqcoeff[MAX_MB_PLANE][3];
37   uint16_t *eobs[MAX_MB_PLANE][3];
38
39   // dual buffer pointers, 0: in use, 1: best in store
40   int16_t *coeff_pbuf[MAX_MB_PLANE][3];
41   int16_t *qcoeff_pbuf[MAX_MB_PLANE][3];
42   int16_t *dqcoeff_pbuf[MAX_MB_PLANE][3];
43   uint16_t *eobs_pbuf[MAX_MB_PLANE][3];
44
45   int is_coded;
46   int num_4x4_blk;
47   int skip;
48   int_mv best_ref_mv[2];
49   int_mv ref_mvs[MAX_REF_FRAMES][MAX_MV_REF_CANDIDATES];
50   int rate;
51   int distortion;
52   int64_t intra_error;
53   int best_mode_index;
54   int rddiv;
55   int rdmult;
56   int hybrid_pred_diff;
57   int comp_pred_diff;
58   int single_pred_diff;
59   int64_t tx_rd_diff[TX_MODES];
60   int64_t best_filter_diff[SWITCHABLE_FILTER_CONTEXTS];
61
62   // motion vector cache for adaptive motion search control in partition
63   // search loop
64   int_mv pred_mv[MAX_REF_FRAMES];
65   INTERP_FILTER pred_interp_filter;
66
67   // Bit flag for each mode whether it has high error in comparison to others.
68   unsigned int modes_with_high_error;
69 } PICK_MODE_CONTEXT;
70
71 struct macroblock_plane {
72   DECLARE_ALIGNED(16, int16_t, src_diff[64 * 64]);
73   int16_t *qcoeff;
74   int16_t *coeff;
75   uint16_t *eobs;
76   struct buf_2d src;
77
78   // Quantizer setings
79   int16_t *quant;
80   int16_t *quant_shift;
81   int16_t *zbin;
82   int16_t *round;
83
84   // Zbin Over Quant value
85   int16_t zbin_extra;
86 };
87
88 /* The [2] dimension is for whether we skip the EOB node (i.e. if previous
89  * coefficient in this block was zero) or not. */
90 typedef unsigned int vp9_coeff_cost[PLANE_TYPES][REF_TYPES][COEF_BANDS][2]
91                                    [COEFF_CONTEXTS][ENTROPY_TOKENS];
92
93 typedef struct macroblock MACROBLOCK;
94 struct macroblock {
95   struct macroblock_plane plane[MAX_MB_PLANE];
96
97   MACROBLOCKD e_mbd;
98   int skip_block;
99   int select_txfm_size;
100   int skip_recode;
101   int skip_optimize;
102   int q_index;
103
104   search_site *ss;
105   int ss_count;
106   int searches_per_step;
107
108   int errorperbit;
109   int sadperbit16;
110   int sadperbit4;
111   int rddiv;
112   int rdmult;
113   unsigned int mb_energy;
114   unsigned int *mb_activity_ptr;
115   int *mb_norm_activity_ptr;
116   signed int act_zbin_adj;
117
118   int mv_best_ref_index[MAX_REF_FRAMES];
119   unsigned int max_mv_context[MAX_REF_FRAMES];
120   unsigned int source_variance;
121   unsigned int pred_sse[MAX_REF_FRAMES];
122   int pred_mv_sad[MAX_REF_FRAMES];
123   int mode_sad[MAX_REF_FRAMES][INTER_MODES + 1];
124
125   int nmvjointcost[MV_JOINTS];
126   int nmvcosts[2][MV_VALS];
127   int *nmvcost[2];
128   int nmvcosts_hp[2][MV_VALS];
129   int *nmvcost_hp[2];
130   int **mvcost;
131
132   int nmvjointsadcost[MV_JOINTS];
133   int nmvsadcosts[2][MV_VALS];
134   int *nmvsadcost[2];
135   int nmvsadcosts_hp[2][MV_VALS];
136   int *nmvsadcost_hp[2];
137   int **mvsadcost;
138
139   int mbmode_cost[MB_MODE_COUNT];
140   unsigned inter_mode_cost[INTER_MODE_CONTEXTS][INTER_MODES];
141   int intra_uv_mode_cost[2][MB_MODE_COUNT];
142   int y_mode_costs[INTRA_MODES][INTRA_MODES][INTRA_MODES];
143   int switchable_interp_costs[SWITCHABLE_FILTER_CONTEXTS][SWITCHABLE_FILTERS];
144
145   unsigned char sb_index;   // index of 32x32 block inside the 64x64 block
146   unsigned char mb_index;   // index of 16x16 block inside the 32x32 block
147   unsigned char b_index;    // index of 8x8 block inside the 16x16 block
148   unsigned char ab_index;   // index of 4x4 block inside the 8x8 block
149
150   // These define limits to motion vector components to prevent them
151   // from extending outside the UMV borders
152   int mv_col_min;
153   int mv_col_max;
154   int mv_row_min;
155   int mv_row_max;
156
157   uint8_t zcoeff_blk[TX_SIZES][256];
158   int skip;
159
160   int encode_breakout;
161
162   unsigned char *active_ptr;
163
164   // note that token_costs is the cost when eob node is skipped
165   vp9_coeff_cost token_costs[TX_SIZES];
166   DECLARE_ALIGNED(16, uint8_t, token_cache[1024]);
167
168   int optimize;
169
170   // indicate if it is in the rd search loop or encoding process
171   int use_lp32x32fdct;
172   int skip_encode;
173
174   // Used to store sub partition's choices.
175   int_mv pred_mv[MAX_REF_FRAMES];
176
177   // TODO(jingning): Need to refactor the structure arrays that buffers the
178   // coding mode decisions of each partition type.
179   PICK_MODE_CONTEXT ab4x4_context[4][4][4];
180   PICK_MODE_CONTEXT sb8x4_context[4][4][4];
181   PICK_MODE_CONTEXT sb4x8_context[4][4][4];
182   PICK_MODE_CONTEXT sb8x8_context[4][4][4];
183   PICK_MODE_CONTEXT sb8x16_context[4][4][2];
184   PICK_MODE_CONTEXT sb16x8_context[4][4][2];
185   PICK_MODE_CONTEXT mb_context[4][4];
186   PICK_MODE_CONTEXT sb32x16_context[4][2];
187   PICK_MODE_CONTEXT sb16x32_context[4][2];
188   // when 4 MBs share coding parameters:
189   PICK_MODE_CONTEXT sb32_context[4];
190   PICK_MODE_CONTEXT sb32x64_context[2];
191   PICK_MODE_CONTEXT sb64x32_context[2];
192   PICK_MODE_CONTEXT sb64_context;
193   int partition_cost[PARTITION_CONTEXTS][PARTITION_TYPES];
194
195   BLOCK_SIZE b_partitioning[4][4][4];
196   BLOCK_SIZE mb_partitioning[4][4];
197   BLOCK_SIZE sb_partitioning[4];
198   BLOCK_SIZE sb64_partitioning;
199
200   void (*fwd_txm4x4)(const int16_t *input, int16_t *output, int stride);
201 };
202
203 // TODO(jingning): the variables used here are little complicated. need further
204 // refactoring on organizing the temporary buffers, when recursive
205 // partition down to 4x4 block size is enabled.
206 static PICK_MODE_CONTEXT *get_block_context(MACROBLOCK *x, BLOCK_SIZE bsize) {
207   switch (bsize) {
208     case BLOCK_64X64:
209       return &x->sb64_context;
210     case BLOCK_64X32:
211       return &x->sb64x32_context[x->sb_index];
212     case BLOCK_32X64:
213       return &x->sb32x64_context[x->sb_index];
214     case BLOCK_32X32:
215       return &x->sb32_context[x->sb_index];
216     case BLOCK_32X16:
217       return &x->sb32x16_context[x->sb_index][x->mb_index];
218     case BLOCK_16X32:
219       return &x->sb16x32_context[x->sb_index][x->mb_index];
220     case BLOCK_16X16:
221       return &x->mb_context[x->sb_index][x->mb_index];
222     case BLOCK_16X8:
223       return &x->sb16x8_context[x->sb_index][x->mb_index][x->b_index];
224     case BLOCK_8X16:
225       return &x->sb8x16_context[x->sb_index][x->mb_index][x->b_index];
226     case BLOCK_8X8:
227       return &x->sb8x8_context[x->sb_index][x->mb_index][x->b_index];
228     case BLOCK_8X4:
229       return &x->sb8x4_context[x->sb_index][x->mb_index][x->b_index];
230     case BLOCK_4X8:
231       return &x->sb4x8_context[x->sb_index][x->mb_index][x->b_index];
232     case BLOCK_4X4:
233       return &x->ab4x4_context[x->sb_index][x->mb_index][x->b_index];
234     default:
235       assert(0);
236       return NULL;
237   }
238 }
239
240 #ifdef __cplusplus
241 }  // extern "C"
242 #endif
243
244 #endif  // VP9_ENCODER_VP9_BLOCK_H_