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