1// Copyright 2010 Google Inc.
2//
3// This code is licensed under the same terms as WebM:
4//  Software License Agreement:  http://www.webmproject.org/license/software/
5//  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
6// -----------------------------------------------------------------------------
7//
8// VP8 decoder: internal header.
9//
10// Author: Skal (pascal.massimino@gmail.com)
11
12#ifndef WEBP_DEC_VP8I_H_
13#define WEBP_DEC_VP8I_H_
14
15#include <string.h>     // for memcpy()
16#include "bits.h"
17
18#if defined(__cplusplus) || defined(c_plusplus)
19extern "C" {
20#endif
21
22//-----------------------------------------------------------------------------
23// Various defines and enums
24
25#define ONLY_KEYFRAME_CODE      // to remove any code related to P-Frames
26
27// intra prediction modes
28enum { B_DC_PRED = 0,   // 4x4 modes
29       B_TM_PRED,
30       B_VE_PRED,
31       B_HE_PRED,
32       B_RD_PRED,
33       B_VR_PRED,
34       B_LD_PRED,
35       B_VL_PRED,
36       B_HD_PRED,
37       B_HU_PRED,
38       NUM_BMODES = B_HU_PRED + 1 - B_DC_PRED,  // = 10
39
40       // Luma16 or UV modes
41       DC_PRED = B_DC_PRED, V_PRED = B_VE_PRED,
42       H_PRED = B_HE_PRED, TM_PRED = B_TM_PRED,
43       B_PRED = NUM_BMODES,   // refined I4x4 mode
44
45       // special modes
46       B_DC_PRED_NOTOP = 4,
47       B_DC_PRED_NOLEFT = 5,
48       B_DC_PRED_NOTOPLEFT = 6 };
49
50enum { MB_FEATURE_TREE_PROBS = 3,
51       NUM_MB_SEGMENTS = 4,
52       NUM_REF_LF_DELTAS = 4,
53       NUM_MODE_LF_DELTAS = 4,    // I4x4, ZERO, *, SPLIT
54       MAX_NUM_PARTITIONS = 8,
55       // Probabilities
56       NUM_TYPES = 4,
57       NUM_BANDS = 8,
58       NUM_CTX = 3,
59       NUM_PROBAS = 11,
60       NUM_MV_PROBAS = 19 };
61
62// YUV-cache parameters.
63// Constraints are: We need to store one 16x16 block of luma samples (y),
64// and two 8x8 chroma blocks (u/v). These are better be 16-bytes aligned,
65// in order to be SIMD-friendly. We also need to store the top, left and
66// top-left samples (from previously decoded blocks), along with four
67// extra top-right samples for luma (intra4x4 prediction only).
68// One possible layout is, using 32 * (17 + 9) bytes:
69//
70//   .+------   <- only 1 pixel high
71//   .|yyyyt.
72//   .|yyyyt.
73//   .|yyyyt.
74//   .|yyyy..
75//   .+--.+--   <- only 1 pixel high
76//   .|uu.|vv
77//   .|uu.|vv
78//
79// Every character is a 4x4 block, with legend:
80//  '.' = unused
81//  'y' = y-samples   'u' = u-samples     'v' = u-samples
82//  '|' = left sample,   '-' = top sample,    '+' = top-left sample
83//  't' = extra top-right sample for 4x4 modes
84// With this layout, BPS (=Bytes Per Scan-line) is one cacheline size.
85#define BPS       32    // this is the common stride used by yuv[]
86#define YUV_SIZE (BPS * 17 + BPS * 9)
87#define Y_SIZE   (BPS * 17)
88#define Y_OFF    (BPS * 1 + 8)
89#define U_OFF    (Y_OFF + BPS * 16 + BPS)
90#define V_OFF    (U_OFF + 16)
91
92//-----------------------------------------------------------------------------
93// Headers
94
95typedef struct {
96  uint8_t key_frame_;
97  uint8_t profile_;
98  uint8_t show_;
99  uint32_t partition_length_;
100} VP8FrameHeader;
101
102typedef struct {
103  uint16_t width_;
104  uint16_t height_;
105  uint8_t xscale_;
106  uint8_t yscale_;
107  uint8_t colorspace_;   // 0 = YCbCr
108  uint8_t clamp_type_;
109} VP8PictureHeader;
110
111// segment features
112typedef struct {
113  int use_segment_;
114  int update_map_;        // whether to update the segment map or not
115  int absolute_delta_;    // absolute or delta values for quantizer and filter
116  int8_t quantizer_[NUM_MB_SEGMENTS];        // quantization changes
117  int8_t filter_strength_[NUM_MB_SEGMENTS];  // filter strength for segments
118} VP8SegmentHeader;
119
120// Struct collecting all frame-persistent probabilities.
121typedef struct {
122  uint8_t segments_[MB_FEATURE_TREE_PROBS];
123  // Type: 0:Intra16-AC  1:Intra16-DC   2:Chroma   3:Intra4
124  uint8_t coeffs_[NUM_TYPES][NUM_BANDS][NUM_CTX][NUM_PROBAS];
125#ifndef ONLY_KEYFRAME_CODE
126  uint8_t ymode_[4], uvmode_[3];
127  uint8_t mv_[2][NUM_MV_PROBAS];
128#endif
129} VP8Proba;
130
131// Filter parameters
132typedef struct {
133  int simple_;                  // 0=complex, 1=simple
134  int level_;                   // [0..63]
135  int sharpness_;               // [0..7]
136  int use_lf_delta_;
137  int ref_lf_delta_[NUM_REF_LF_DELTAS];
138  int mode_lf_delta_[NUM_MODE_LF_DELTAS];
139} VP8FilterHeader;
140
141//-----------------------------------------------------------------------------
142// Informations about the macroblocks.
143
144typedef struct {
145  // block type
146  uint8_t skip_:1;
147  // filter specs
148  uint8_t f_level_:6;      // filter strength: 0..63
149  uint8_t f_ilevel_:6;     // inner limit: 1..63
150  uint8_t f_inner_:1;      // do inner filtering?
151  // cbp
152  uint8_t nz_;        // non-zero AC/DC coeffs
153  uint8_t dc_nz_;     // non-zero DC coeffs
154} VP8MB;
155
156// Dequantization matrices
157typedef struct {
158  uint16_t y1_mat_[2], y2_mat_[2], uv_mat_[2];    // [DC / AC]
159} VP8QuantMatrix;
160
161//-----------------------------------------------------------------------------
162// VP8Decoder: the main opaque structure handed over to user
163
164struct VP8Decoder {
165  VP8StatusCode status_;
166  int ready_;     // true if ready to decode a picture with VP8Decode()
167  const char* error_msg_;  // set when status_ is not OK.
168
169  // Main data source
170  VP8BitReader br_;
171
172  // headers
173  VP8FrameHeader   frm_hdr_;
174  VP8PictureHeader pic_hdr_;
175  VP8FilterHeader  filter_hdr_;
176  VP8SegmentHeader segment_hdr_;
177
178  // dimension, in macroblock units.
179  int mb_w_, mb_h_;
180
181  // number of partitions.
182  int num_parts_;
183  // per-partition boolean decoders.
184  VP8BitReader parts_[MAX_NUM_PARTITIONS];
185
186  // buffer refresh flags
187  //   bit 0: refresh Gold, bit 1: refresh Alt
188  //   bit 2-3: copy to Gold, bit 4-5: copy to Alt
189  //   bit 6: Gold sign bias, bit 7: Alt sign bias
190  //   bit 8: refresh last frame
191  uint32_t buffer_flags_;
192
193  // dequantization (one set of DC/AC dequant factor per segment)
194  VP8QuantMatrix dqm_[NUM_MB_SEGMENTS];
195
196  // probabilities
197  VP8Proba proba_;
198  int use_skip_proba_;
199  uint8_t skip_p_;
200#ifndef ONLY_KEYFRAME_CODE
201  uint8_t intra_p_, last_p_, golden_p_;
202  VP8Proba proba_saved_;
203  int update_proba_;
204#endif
205
206  // Boundary data cache and persistent buffers.
207  uint8_t* intra_t_;     // top intra modes values: 4 * mb_w_
208  uint8_t  intra_l_[4];  // left intra modes values
209  uint8_t *y_t_;         // top luma samples: 16 * mb_w_
210  uint8_t *u_t_, *v_t_;  // top u/v samples: 8 * mb_w_ each
211
212  VP8MB* mb_info_;       // contextual macroblock infos (mb_w_ + 1)
213  uint8_t* yuv_b_;       // main block for Y/U/V (size = YUV_SIZE)
214  int16_t* coeffs_;      // 384 coeffs = (16+8+8) * 4*4
215
216  uint8_t* cache_y_;     // macroblock row for storing unfiltered samples
217  uint8_t* cache_u_;
218  uint8_t* cache_v_;
219  int cache_y_stride_;
220  int cache_uv_stride_;
221
222  // main memory chunk for the above data. Persistent.
223  void* mem_;
224  int mem_size_;
225
226  // Per macroblock non-persistent infos.
227  int mb_x_, mb_y_;       // current position, in macroblock units
228  uint8_t is_i4x4_;       // true if intra4x4
229  uint8_t imodes_[16];    // one 16x16 mode (#0) or sixteen 4x4 modes
230  uint8_t uvmode_;        // chroma prediction mode
231  uint8_t segment_;       // block's segment
232
233  // bit-wise info about the content of each sub-4x4 blocks: there are 16 bits
234  // for luma (bits #0->#15), then 4 bits for chroma-u (#16->#19) and 4 bits for
235  // chroma-v (#20->#23), each corresponding to one 4x4 block in decoding order.
236  // If the bit is set, the 4x4 block contains some non-zero coefficients.
237  uint32_t non_zero_;
238  uint32_t non_zero_ac_;
239
240  // Filtering side-info
241  int filter_type_;                       // 0=off, 1=simple, 2=complex
242  uint8_t filter_levels_[NUM_MB_SEGMENTS];  // precalculated per-segment
243};
244
245//-----------------------------------------------------------------------------
246// internal functions. Not public.
247
248// in vp8.c
249int VP8SetError(VP8Decoder* const dec,
250                VP8StatusCode error, const char * const msg);
251
252// in tree.c
253void VP8ResetProba(VP8Proba* const proba);
254void VP8ParseProba(VP8BitReader* const br, VP8Decoder* const dec);
255void VP8ParseIntraMode(VP8BitReader* const br,  VP8Decoder* const dec);
256
257// in quant.c
258void VP8ParseQuant(VP8Decoder* const dec);
259
260// in frame.c
261int VP8InitFrame(VP8Decoder* const dec, VP8Io* io);
262// Predict a block and add residual
263void VP8ReconstructBlock(VP8Decoder* const dec);
264// Store a block, along with filtering params
265void VP8StoreBlock(VP8Decoder* const dec);
266// Finalize and transmit a complete row. Return false in case of user-abort.
267int VP8FinishRow(VP8Decoder* const dec, VP8Io* io);
268
269// in dsp.c
270typedef void (*VP8Idct)(const int16_t* coeffs, uint8_t* dst);
271extern VP8Idct VP8Transform;
272extern VP8Idct VP8TransformUV;
273extern VP8Idct VP8TransformDC;
274extern VP8Idct VP8TransformDCUV;
275extern void (*VP8TransformWHT)(const int16_t* in, int16_t* out);
276
277// *dst is the destination block, with stride BPS. Boundary samples are
278// assumed accessible when needed.
279typedef void (*VP8PredFunc)(uint8_t *dst);
280extern VP8PredFunc VP8PredLuma16[7];
281extern VP8PredFunc VP8PredChroma8[7];
282extern VP8PredFunc VP8PredLuma4[11];
283
284void VP8DspInit();        // must be called before anything using the above
285void VP8DspInitTables();  // needs to be called no matter what.
286
287// simple filter (only for luma)
288typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh);
289extern VP8SimpleFilterFunc VP8SimpleVFilter16;
290extern VP8SimpleFilterFunc VP8SimpleHFilter16;
291extern VP8SimpleFilterFunc VP8SimpleVFilter16i;  // filter 3 inner edges
292extern VP8SimpleFilterFunc VP8SimpleHFilter16i;
293
294// regular filter (on both macroblock edges and inner edges)
295typedef void (*VP8LumaFilterFunc)(uint8_t* luma, int stride,
296                                  int thresh, int ithresh, int hev_t);
297typedef void (*VP8ChromaFilterFunc)(uint8_t* u, uint8_t* v, int stride,
298                                    int thresh, int ithresh, int hev_t);
299// on outter edge
300extern VP8LumaFilterFunc VP8VFilter16;
301extern VP8LumaFilterFunc VP8HFilter16;
302extern VP8ChromaFilterFunc VP8VFilter8;
303extern VP8ChromaFilterFunc VP8HFilter8;
304
305// on inner edge
306extern VP8LumaFilterFunc VP8VFilter16i;   // filtering 3 inner edges altogether
307extern VP8LumaFilterFunc VP8HFilter16i;
308extern VP8ChromaFilterFunc VP8VFilter8i;  // filtering u and v altogether
309extern VP8ChromaFilterFunc VP8HFilter8i;
310
311//-----------------------------------------------------------------------------
312
313#if defined(__cplusplus) || defined(c_plusplus)
314}    // extern "C"
315#endif
316
317#endif  // WEBP_DEC_VP8I_H_
318