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 VP8_COMMON_BLOCKD_H_
12 #define VP8_COMMON_BLOCKD_H_
13 
14 void vpx_log(const char *format, ...);
15 
16 #include "vpx_config.h"
17 #include "vpx_scale/yv12config.h"
18 #include "mv.h"
19 #include "treecoder.h"
20 #include "vpx_ports/mem.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /*#define DCPRED 1*/
27 #define DCPREDSIMTHRESH 0
28 #define DCPREDCNTTHRESH 3
29 
30 #define MB_FEATURE_TREE_PROBS 3
31 #define MAX_MB_SEGMENTS 4
32 
33 #define MAX_REF_LF_DELTAS 4
34 #define MAX_MODE_LF_DELTAS 4
35 
36 /* Segment Feature Masks */
37 #define SEGMENT_DELTADATA 0
38 #define SEGMENT_ABSDATA 1
39 
40 typedef struct { int r, c; } POS;
41 
42 #define PLANE_TYPE_Y_NO_DC 0
43 #define PLANE_TYPE_Y2 1
44 #define PLANE_TYPE_UV 2
45 #define PLANE_TYPE_Y_WITH_DC 3
46 
47 typedef char ENTROPY_CONTEXT;
48 typedef struct {
49   ENTROPY_CONTEXT y1[4];
50   ENTROPY_CONTEXT u[2];
51   ENTROPY_CONTEXT v[2];
52   ENTROPY_CONTEXT y2;
53 } ENTROPY_CONTEXT_PLANES;
54 
55 extern const unsigned char vp8_block2left[25];
56 extern const unsigned char vp8_block2above[25];
57 
58 #define VP8_COMBINEENTROPYCONTEXTS(Dest, A, B) Dest = (A) + (B);
59 
60 typedef enum { KEY_FRAME = 0, INTER_FRAME = 1 } FRAME_TYPE;
61 
62 typedef enum {
63   DC_PRED, /* average of above and left pixels */
64   V_PRED,  /* vertical prediction */
65   H_PRED,  /* horizontal prediction */
66   TM_PRED, /* Truemotion prediction */
67   B_PRED,  /* block based prediction, each block has its own prediction mode */
68 
69   NEARESTMV,
70   NEARMV,
71   ZEROMV,
72   NEWMV,
73   SPLITMV,
74 
75   MB_MODE_COUNT
76 } MB_PREDICTION_MODE;
77 
78 /* Macroblock level features */
79 typedef enum {
80   MB_LVL_ALT_Q = 0,  /* Use alternate Quantizer .... */
81   MB_LVL_ALT_LF = 1, /* Use alternate loop filter value... */
82   MB_LVL_MAX = 2     /* Number of MB level features supported */
83 
84 } MB_LVL_FEATURES;
85 
86 /* Segment Feature Masks */
87 #define SEGMENT_ALTQ 0x01
88 #define SEGMENT_ALT_LF 0x02
89 
90 #define VP8_YMODES (B_PRED + 1)
91 #define VP8_UV_MODES (TM_PRED + 1)
92 
93 #define VP8_MVREFS (1 + SPLITMV - NEARESTMV)
94 
95 typedef enum {
96   B_DC_PRED, /* average of above and left pixels */
97   B_TM_PRED,
98 
99   B_VE_PRED, /* vertical prediction */
100   B_HE_PRED, /* horizontal prediction */
101 
102   B_LD_PRED,
103   B_RD_PRED,
104 
105   B_VR_PRED,
106   B_VL_PRED,
107   B_HD_PRED,
108   B_HU_PRED,
109 
110   LEFT4X4,
111   ABOVE4X4,
112   ZERO4X4,
113   NEW4X4,
114 
115   B_MODE_COUNT
116 } B_PREDICTION_MODE;
117 
118 #define VP8_BINTRAMODES (B_HU_PRED + 1) /* 10 */
119 #define VP8_SUBMVREFS (1 + NEW4X4 - LEFT4X4)
120 
121 /* For keyframes, intra block modes are predicted by the (already decoded)
122    modes for the Y blocks to the left and above us; for interframes, there
123    is a single probability table. */
124 
125 union b_mode_info {
126   B_PREDICTION_MODE as_mode;
127   int_mv mv;
128 };
129 
130 typedef enum {
131   INTRA_FRAME = 0,
132   LAST_FRAME = 1,
133   GOLDEN_FRAME = 2,
134   ALTREF_FRAME = 3,
135   MAX_REF_FRAMES = 4
136 } MV_REFERENCE_FRAME;
137 
138 typedef struct {
139   uint8_t mode, uv_mode;
140   uint8_t ref_frame;
141   uint8_t is_4x4;
142   int_mv mv;
143 
144   uint8_t partitioning;
145   /* does this mb has coefficients at all, 1=no coefficients, 0=need decode
146      tokens */
147   uint8_t mb_skip_coeff;
148   uint8_t need_to_clamp_mvs;
149   /* Which set of segmentation parameters should be used for this MB */
150   uint8_t segment_id;
151 } MB_MODE_INFO;
152 
153 typedef struct modeinfo {
154   MB_MODE_INFO mbmi;
155   union b_mode_info bmi[16];
156 } MODE_INFO;
157 
158 #if CONFIG_MULTI_RES_ENCODING
159 /* The mb-level information needed to be stored for higher-resolution encoder */
160 typedef struct {
161   MB_PREDICTION_MODE mode;
162   MV_REFERENCE_FRAME ref_frame;
163   int_mv mv;
164   int dissim; /* dissimilarity level of the macroblock */
165 } LOWER_RES_MB_INFO;
166 
167 /* The frame-level information needed to be stored for higher-resolution
168  *  encoder */
169 typedef struct {
170   FRAME_TYPE frame_type;
171   int is_frame_dropped;
172   // If frame is dropped due to overshoot after encode_frame. This triggers a
173   // drop and resets rate control with Q forced to max for following frame.
174   // The check for this dropping due to overshoot is only done on lowest stream,
175   // and if set will force drop on all spatial streams for that current frame.
176   int is_frame_dropped_overshoot_maxqp;
177   // The frame rate for the lowest resolution.
178   double low_res_framerate;
179   /* The frame number of each reference frames */
180   unsigned int low_res_ref_frames[MAX_REF_FRAMES];
181   // The video frame counter value for the key frame, for lowest resolution.
182   unsigned int key_frame_counter_value;
183   LOWER_RES_MB_INFO *mb_info;
184 } LOWER_RES_FRAME_INFO;
185 #endif
186 
187 typedef struct blockd {
188   short *qcoeff;
189   short *dqcoeff;
190   unsigned char *predictor;
191   short *dequant;
192 
193   int offset;
194   char *eob;
195 
196   union b_mode_info bmi;
197 } BLOCKD;
198 
199 typedef void (*vp8_subpix_fn_t)(unsigned char *src, int src_pitch, int xofst,
200                                 int yofst, unsigned char *dst, int dst_pitch);
201 
202 typedef struct macroblockd {
203   DECLARE_ALIGNED(16, unsigned char, predictor[384]);
204   DECLARE_ALIGNED(16, short, qcoeff[400]);
205   DECLARE_ALIGNED(16, short, dqcoeff[400]);
206   DECLARE_ALIGNED(16, char, eobs[25]);
207 
208   DECLARE_ALIGNED(16, short, dequant_y1[16]);
209   DECLARE_ALIGNED(16, short, dequant_y1_dc[16]);
210   DECLARE_ALIGNED(16, short, dequant_y2[16]);
211   DECLARE_ALIGNED(16, short, dequant_uv[16]);
212 
213   /* 16 Y blocks, 4 U, 4 V, 1 DC 2nd order block, each with 16 entries. */
214   BLOCKD block[25];
215   int fullpixel_mask;
216 
217   YV12_BUFFER_CONFIG pre; /* Filtered copy of previous frame reconstruction */
218   YV12_BUFFER_CONFIG dst;
219 
220   MODE_INFO *mode_info_context;
221   int mode_info_stride;
222 
223   FRAME_TYPE frame_type;
224 
225   int up_available;
226   int left_available;
227 
228   unsigned char *recon_above[3];
229   unsigned char *recon_left[3];
230   int recon_left_stride[2];
231 
232   /* Y,U,V,Y2 */
233   ENTROPY_CONTEXT_PLANES *above_context;
234   ENTROPY_CONTEXT_PLANES *left_context;
235 
236   /* 0 indicates segmentation at MB level is not enabled. Otherwise the
237    * individual bits indicate which features are active. */
238   unsigned char segmentation_enabled;
239 
240   /* 0 (do not update) 1 (update) the macroblock segmentation map. */
241   unsigned char update_mb_segmentation_map;
242 
243   /* 0 (do not update) 1 (update) the macroblock segmentation feature data. */
244   unsigned char update_mb_segmentation_data;
245 
246   /* 0 (do not update) 1 (update) the macroblock segmentation feature data. */
247   unsigned char mb_segement_abs_delta;
248 
249   /* Per frame flags that define which MB level features (such as quantizer or
250    * loop filter level) */
251   /* are enabled and when enabled the proabilities used to decode the per MB
252    * flags in MB_MODE_INFO */
253   /* Probability Tree used to code Segment number */
254   vp8_prob mb_segment_tree_probs[MB_FEATURE_TREE_PROBS];
255   /* Segment parameters */
256   signed char segment_feature_data[MB_LVL_MAX][MAX_MB_SEGMENTS];
257 
258   /* mode_based Loop filter adjustment */
259   unsigned char mode_ref_lf_delta_enabled;
260   unsigned char mode_ref_lf_delta_update;
261 
262   /* Delta values have the range +/- MAX_LOOP_FILTER */
263   signed char
264       last_ref_lf_deltas[MAX_REF_LF_DELTAS];    /* 0 = Intra, Last, GF, ARF */
265   signed char ref_lf_deltas[MAX_REF_LF_DELTAS]; /* 0 = Intra, Last, GF, ARF */
266   /* 0 = BPRED, ZERO_MV, MV, SPLIT */
267   signed char last_mode_lf_deltas[MAX_MODE_LF_DELTAS];
268   signed char
269       mode_lf_deltas[MAX_MODE_LF_DELTAS]; /* 0 = BPRED, ZERO_MV, MV, SPLIT */
270 
271   /* Distance of MB away from frame edges */
272   int mb_to_left_edge;
273   int mb_to_right_edge;
274   int mb_to_top_edge;
275   int mb_to_bottom_edge;
276 
277   vp8_subpix_fn_t subpixel_predict;
278   vp8_subpix_fn_t subpixel_predict8x4;
279   vp8_subpix_fn_t subpixel_predict8x8;
280   vp8_subpix_fn_t subpixel_predict16x16;
281 
282   void *current_bc;
283 
284   int corrupted;
285 
286 #if ARCH_X86 || ARCH_X86_64
287   /* This is an intermediate buffer currently used in sub-pixel motion search
288    * to keep a copy of the reference area. This buffer can be used for other
289    * purpose.
290    */
291   DECLARE_ALIGNED(32, unsigned char, y_buf[22 * 32]);
292 #endif
293 } MACROBLOCKD;
294 
295 extern void vp8_build_block_doffsets(MACROBLOCKD *x);
296 extern void vp8_setup_block_dptrs(MACROBLOCKD *x);
297 
298 #ifdef __cplusplus
299 }  // extern "C"
300 #endif
301 
302 #endif  // VP8_COMMON_BLOCKD_H_
303