1 // Copyright 2010 Google Inc. All Rights Reserved.
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 // -----------------------------------------------------------------------------
8 // VP8 decoder: internal header.
10 // Author: Skal (pascal.massimino@gmail.com)
12 #ifndef WEBP_DEC_VP8I_H_
13 #define WEBP_DEC_VP8I_H_
15 #include <string.h> // for memcpy()
17 #include "../utils/bit_reader.h"
18 #include "../utils/thread.h"
19 #include "../dsp/dsp.h"
21 #if defined(__cplusplus) || defined(c_plusplus)
25 //------------------------------------------------------------------------------
26 // Various defines and enums
29 #define DEC_MAJ_VERSION 0
30 #define DEC_MIN_VERSION 3
31 #define DEC_REV_VERSION 0
33 #define ONLY_KEYFRAME_CODE // to remove any code related to P-Frames
35 // intra prediction modes
36 enum { B_DC_PRED = 0, // 4x4 modes
46 NUM_BMODES = B_HU_PRED + 1 - B_DC_PRED, // = 10
49 DC_PRED = B_DC_PRED, V_PRED = B_VE_PRED,
50 H_PRED = B_HE_PRED, TM_PRED = B_TM_PRED,
51 B_PRED = NUM_BMODES, // refined I4x4 mode
56 B_DC_PRED_NOTOPLEFT = 6,
59 enum { MB_FEATURE_TREE_PROBS = 3,
61 NUM_REF_LF_DELTAS = 4,
62 NUM_MODE_LF_DELTAS = 4, // I4x4, ZERO, *, SPLIT
63 MAX_NUM_PARTITIONS = 8,
71 // YUV-cache parameters.
72 // Constraints are: We need to store one 16x16 block of luma samples (y),
73 // and two 8x8 chroma blocks (u/v). These are better be 16-bytes aligned,
74 // in order to be SIMD-friendly. We also need to store the top, left and
75 // top-left samples (from previously decoded blocks), along with four
76 // extra top-right samples for luma (intra4x4 prediction only).
77 // One possible layout is, using 32 * (17 + 9) bytes:
79 // .+------ <- only 1 pixel high
84 // .+--.+-- <- only 1 pixel high
88 // Every character is a 4x4 block, with legend:
90 // 'y' = y-samples 'u' = u-samples 'v' = u-samples
91 // '|' = left sample, '-' = top sample, '+' = top-left sample
92 // 't' = extra top-right sample for 4x4 modes
93 // With this layout, BPS (=Bytes Per Scan-line) is one cacheline size.
94 #define BPS 32 // this is the common stride used by yuv[]
95 #define YUV_SIZE (BPS * 17 + BPS * 9)
96 #define Y_SIZE (BPS * 17)
97 #define Y_OFF (BPS * 1 + 8)
98 #define U_OFF (Y_OFF + BPS * 16 + BPS)
99 #define V_OFF (U_OFF + 16)
101 //------------------------------------------------------------------------------
108 uint32_t partition_length_;
116 uint8_t colorspace_; // 0 = YCbCr
123 int update_map_; // whether to update the segment map or not
124 int absolute_delta_; // absolute or delta values for quantizer and filter
125 int8_t quantizer_[NUM_MB_SEGMENTS]; // quantization changes
126 int8_t filter_strength_[NUM_MB_SEGMENTS]; // filter strength for segments
129 // Struct collecting all frame-persistent probabilities.
131 uint8_t segments_[MB_FEATURE_TREE_PROBS];
132 // Type: 0:Intra16-AC 1:Intra16-DC 2:Chroma 3:Intra4
133 uint8_t coeffs_[NUM_TYPES][NUM_BANDS][NUM_CTX][NUM_PROBAS];
134 #ifndef ONLY_KEYFRAME_CODE
135 uint8_t ymode_[4], uvmode_[3];
136 uint8_t mv_[2][NUM_MV_PROBAS];
142 int simple_; // 0=complex, 1=simple
143 int level_; // [0..63]
144 int sharpness_; // [0..7]
146 int ref_lf_delta_[NUM_REF_LF_DELTAS];
147 int mode_lf_delta_[NUM_MODE_LF_DELTAS];
150 //------------------------------------------------------------------------------
151 // Informations about the macroblocks.
153 typedef struct { // filter specs
154 uint8_t f_level_; // filter strength: 0..63
155 uint8_t f_ilevel_; // inner limit: 1..63
156 uint8_t f_inner_; // do inner filtering?
157 uint8_t pad_; // mostly needed for struct aligning on ARM
160 typedef struct { // Top/Left Contexts used for syntax-parsing
161 uint8_t nz_; // non-zero AC/DC coeffs (4bit for luma + 4bit for chroma)
162 uint8_t nz_dc_; // non-zero DC coeff (1bit)
165 // Dequantization matrices
166 typedef int quant_t[2]; // [DC / AC]. Can be 'uint16_t[2]' too (~slower).
168 quant_t y1_mat_, y2_mat_, uv_mat_;
171 // Persistent information needed by the parallel processing
173 int id_; // cache row to process (in [0..2])
174 int mb_y_; // macroblock position of the row
175 int filter_row_; // true if row-filtering is needed
176 VP8FInfo* f_info_; // filter strengths
177 VP8Io io_; // copy of the VP8Io to pass to put()
180 //------------------------------------------------------------------------------
181 // VP8Decoder: the main opaque structure handed over to user
184 VP8StatusCode status_;
185 int ready_; // true if ready to decode a picture with VP8Decode()
186 const char* error_msg_; // set when status_ is not OK.
192 VP8FrameHeader frm_hdr_;
193 VP8PictureHeader pic_hdr_;
194 VP8FilterHeader filter_hdr_;
195 VP8SegmentHeader segment_hdr_;
199 int use_threads_; // use multi-thread
200 int cache_id_; // current cache row
201 int num_caches_; // number of cached rows of 16 pixels (1, 2 or 3)
202 VP8ThreadContext thread_ctx_; // Thread context
204 // dimension, in macroblock units.
207 // Macroblock to process/filter, depending on cropping and filter_type.
208 int tl_mb_x_, tl_mb_y_; // top-left MB that must be in-loop filtered
209 int br_mb_x_, br_mb_y_; // last bottom-right MB that must be decoded
211 // number of partitions.
213 // per-partition boolean decoders.
214 VP8BitReader parts_[MAX_NUM_PARTITIONS];
216 // buffer refresh flags
217 // bit 0: refresh Gold, bit 1: refresh Alt
218 // bit 2-3: copy to Gold, bit 4-5: copy to Alt
219 // bit 6: Gold sign bias, bit 7: Alt sign bias
220 // bit 8: refresh last frame
221 uint32_t buffer_flags_;
223 // dequantization (one set of DC/AC dequant factor per segment)
224 VP8QuantMatrix dqm_[NUM_MB_SEGMENTS];
230 #ifndef ONLY_KEYFRAME_CODE
231 uint8_t intra_p_, last_p_, golden_p_;
232 VP8Proba proba_saved_;
236 // Boundary data cache and persistent buffers.
237 uint8_t* intra_t_; // top intra modes values: 4 * mb_w_
238 uint8_t intra_l_[4]; // left intra modes values
239 uint8_t* y_t_; // top luma samples: 16 * mb_w_
240 uint8_t* u_t_, *v_t_; // top u/v samples: 8 * mb_w_ each
242 VP8MB* mb_info_; // contextual macroblock info (mb_w_ + 1)
243 VP8FInfo* f_info_; // filter strength info
244 uint8_t* yuv_b_; // main block for Y/U/V (size = YUV_SIZE)
245 int16_t* coeffs_; // 384 coeffs = (16+8+8) * 4*4
247 uint8_t* cache_y_; // macroblock row for storing unfiltered samples
251 int cache_uv_stride_;
253 // main memory chunk for the above data. Persistent.
257 // Per macroblock non-persistent infos.
258 int mb_x_, mb_y_; // current position, in macroblock units
259 uint8_t is_i4x4_; // true if intra4x4
260 uint8_t imodes_[16]; // one 16x16 mode (#0) or sixteen 4x4 modes
261 uint8_t uvmode_; // chroma prediction mode
262 uint8_t segment_; // block's segment
264 // bit-wise info about the content of each sub-4x4 blocks: there are 16 bits
265 // for luma (bits #0->#15), then 4 bits for chroma-u (#16->#19) and 4 bits for
266 // chroma-v (#20->#23), each corresponding to one 4x4 block in decoding order.
267 // If the bit is set, the 4x4 block contains some non-zero coefficients.
269 uint32_t non_zero_ac_;
271 // Filtering side-info
272 int filter_type_; // 0=off, 1=simple, 2=complex
273 int filter_row_; // per-row flag
274 VP8FInfo fstrengths_[NUM_MB_SEGMENTS][2]; // precalculated per-segment/type
277 const uint8_t* alpha_data_; // compressed alpha data (if present)
278 size_t alpha_data_size_;
279 int is_alpha_decoded_; // true if alpha_data_ is decoded in alpha_plane_
280 uint8_t* alpha_plane_; // output. Persistent, contains the whole data.
282 int layer_colorspace_;
283 const uint8_t* layer_data_; // compressed layer data (if present)
284 size_t layer_data_size_;
287 //------------------------------------------------------------------------------
288 // internal functions. Not public.
291 int VP8SetError(VP8Decoder* const dec,
292 VP8StatusCode error, const char* const msg);
295 void VP8ResetProba(VP8Proba* const proba);
296 void VP8ParseProba(VP8BitReader* const br, VP8Decoder* const dec);
297 void VP8ParseIntraMode(VP8BitReader* const br, VP8Decoder* const dec);
300 void VP8ParseQuant(VP8Decoder* const dec);
303 int VP8InitFrame(VP8Decoder* const dec, VP8Io* io);
304 // Predict a block and add residual
305 void VP8ReconstructBlock(const VP8Decoder* const dec);
306 // Call io->setup() and finish setting up scan parameters.
307 // After this call returns, one must always call VP8ExitCritical() with the
308 // same parameters. Both functions should be used in pair. Returns VP8_STATUS_OK
309 // if ok, otherwise sets and returns the error status on *dec.
310 VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io);
311 // Must always be called in pair with VP8EnterCritical().
312 // Returns false in case of error.
313 int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io);
314 // Process the last decoded row (filtering + output)
315 int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io);
316 // To be called at the start of a new scanline, to initialize predictors.
317 void VP8InitScanline(VP8Decoder* const dec);
318 // Decode one macroblock. Returns false if there is not enough data.
319 int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br);
322 const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
323 int row, int num_rows);
326 int VP8DecodeLayer(VP8Decoder* const dec);
328 //------------------------------------------------------------------------------
330 #if defined(__cplusplus) || defined(c_plusplus)
334 #endif /* WEBP_DEC_VP8I_H_ */