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