1 // Copyright 2010 Google Inc. All Rights Reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
10 // VP8 decoder: internal header.
12 // Author: Skal (pascal.massimino@gmail.com)
14 #ifndef WEBP_DEC_VP8I_H_
15 #define WEBP_DEC_VP8I_H_
17 #include <string.h> // for memcpy()
19 #include "../utils/bit_reader.h"
20 #include "../utils/random.h"
21 #include "../utils/thread.h"
22 #include "../dsp/dsp.h"
28 //------------------------------------------------------------------------------
29 // Various defines and enums
32 #define DEC_MAJ_VERSION 0
33 #define DEC_MIN_VERSION 4
34 #define DEC_REV_VERSION 0
36 // intra prediction modes
37 enum { B_DC_PRED
= 0, // 4x4 modes
47 NUM_BMODES
= B_HU_PRED
+ 1 - B_DC_PRED
, // = 10
50 DC_PRED
= B_DC_PRED
, V_PRED
= B_VE_PRED
,
51 H_PRED
= B_HE_PRED
, TM_PRED
= B_TM_PRED
,
52 B_PRED
= NUM_BMODES
, // refined I4x4 mode
57 B_DC_PRED_NOTOPLEFT
= 6,
60 enum { MB_FEATURE_TREE_PROBS
= 3,
62 NUM_REF_LF_DELTAS
= 4,
63 NUM_MODE_LF_DELTAS
= 4, // I4x4, ZERO, *, SPLIT
64 MAX_NUM_PARTITIONS
= 8,
72 // YUV-cache parameters.
73 // Constraints are: We need to store one 16x16 block of luma samples (y),
74 // and two 8x8 chroma blocks (u/v). These are better be 16-bytes aligned,
75 // in order to be SIMD-friendly. We also need to store the top, left and
76 // top-left samples (from previously decoded blocks), along with four
77 // extra top-right samples for luma (intra4x4 prediction only).
78 // One possible layout is, using 32 * (17 + 9) bytes:
80 // .+------ <- only 1 pixel high
85 // .+--.+-- <- only 1 pixel high
89 // Every character is a 4x4 block, with legend:
91 // 'y' = y-samples 'u' = u-samples 'v' = u-samples
92 // '|' = left sample, '-' = top sample, '+' = top-left sample
93 // 't' = extra top-right sample for 4x4 modes
94 // With this layout, BPS (=Bytes Per Scan-line) is one cacheline size.
95 #define BPS 32 // this is the common stride used by yuv[]
96 #define YUV_SIZE (BPS * 17 + BPS * 9)
97 #define Y_SIZE (BPS * 17)
98 #define Y_OFF (BPS * 1 + 8)
99 #define U_OFF (Y_OFF + BPS * 16 + BPS)
100 #define V_OFF (U_OFF + 16)
102 // minimal width under which lossy multi-threading is always disabled
103 #define MIN_WIDTH_FOR_THREADS 512
105 //------------------------------------------------------------------------------
112 uint32_t partition_length_
;
120 uint8_t colorspace_
; // 0 = YCbCr
127 int update_map_
; // whether to update the segment map or not
128 int absolute_delta_
; // absolute or delta values for quantizer and filter
129 int8_t quantizer_
[NUM_MB_SEGMENTS
]; // quantization changes
130 int8_t filter_strength_
[NUM_MB_SEGMENTS
]; // filter strength for segments
134 // probas associated to one of the contexts
135 typedef uint8_t VP8ProbaArray
[NUM_PROBAS
];
137 typedef struct { // all the probas associated to one band
138 VP8ProbaArray probas_
[NUM_CTX
];
141 // Struct collecting all frame-persistent probabilities.
143 uint8_t segments_
[MB_FEATURE_TREE_PROBS
];
144 // Type: 0:Intra16-AC 1:Intra16-DC 2:Chroma 3:Intra4
145 VP8BandProbas bands_
[NUM_TYPES
][NUM_BANDS
];
150 int simple_
; // 0=complex, 1=simple
151 int level_
; // [0..63]
152 int sharpness_
; // [0..7]
154 int ref_lf_delta_
[NUM_REF_LF_DELTAS
];
155 int mode_lf_delta_
[NUM_MODE_LF_DELTAS
];
158 //------------------------------------------------------------------------------
159 // Informations about the macroblocks.
161 typedef struct { // filter specs
162 uint8_t f_limit_
; // filter limit in [3..189], or 0 if no filtering
163 uint8_t f_ilevel_
; // inner limit in [1..63]
164 uint8_t f_inner_
; // do inner filtering?
165 uint8_t hev_thresh_
; // high edge variance threshold in [0..2]
168 typedef struct { // Top/Left Contexts used for syntax-parsing
169 uint8_t nz_
; // non-zero AC/DC coeffs (4bit for luma + 4bit for chroma)
170 uint8_t nz_dc_
; // non-zero DC coeff (1bit)
173 // Dequantization matrices
174 typedef int quant_t
[2]; // [DC / AC]. Can be 'uint16_t[2]' too (~slower).
176 quant_t y1_mat_
, y2_mat_
, uv_mat_
;
178 int uv_quant_
; // U/V quantizer value
179 int dither_
; // dithering amplitude (0 = off, max=255)
182 // Data needed to reconstruct a macroblock
184 int16_t coeffs_
[384]; // 384 coeffs = (16+4+4) * 4*4
185 uint8_t is_i4x4_
; // true if intra4x4
186 uint8_t imodes_
[16]; // one 16x16 mode (#0) or sixteen 4x4 modes
187 uint8_t uvmode_
; // chroma prediction mode
188 // bit-wise info about the content of each sub-4x4 blocks (in decoding order).
189 // Each of the 4x4 blocks for y/u/v is associated with a 2b code according to:
190 // code=0 -> no coefficient
192 // code=2 -> first three coefficients are non-zero
193 // code=3 -> more than three coefficients are non-zero
194 // This allows to call specialized transform functions.
195 uint32_t non_zero_y_
;
196 uint32_t non_zero_uv_
;
197 uint8_t dither_
; // local dithering strength (deduced from non_zero_*)
200 // Persistent information needed by the parallel processing
202 int id_
; // cache row to process (in [0..2])
203 int mb_y_
; // macroblock position of the row
204 int filter_row_
; // true if row-filtering is needed
205 VP8FInfo
* f_info_
; // filter strengths (swapped with dec->f_info_)
206 VP8MBData
* mb_data_
; // reconstruction data (swapped with dec->mb_data_)
207 VP8Io io_
; // copy of the VP8Io to pass to put()
210 // Saved top samples, per macroblock. Fits into a cache-line.
212 uint8_t y
[16], u
[8], v
[8];
215 //------------------------------------------------------------------------------
216 // VP8Decoder: the main opaque structure handed over to user
219 VP8StatusCode status_
;
220 int ready_
; // true if ready to decode a picture with VP8Decode()
221 const char* error_msg_
; // set when status_ is not OK.
227 VP8FrameHeader frm_hdr_
;
228 VP8PictureHeader pic_hdr_
;
229 VP8FilterHeader filter_hdr_
;
230 VP8SegmentHeader segment_hdr_
;
234 int mt_method_
; // multi-thread method: 0=off, 1=[parse+recon][filter]
235 // 2=[parse][recon+filter]
236 int cache_id_
; // current cache row
237 int num_caches_
; // number of cached rows of 16 pixels (1, 2 or 3)
238 VP8ThreadContext thread_ctx_
; // Thread context
240 // dimension, in macroblock units.
243 // Macroblock to process/filter, depending on cropping and filter_type.
244 int tl_mb_x_
, tl_mb_y_
; // top-left MB that must be in-loop filtered
245 int br_mb_x_
, br_mb_y_
; // last bottom-right MB that must be decoded
247 // number of partitions.
249 // per-partition boolean decoders.
250 VP8BitReader parts_
[MAX_NUM_PARTITIONS
];
252 // Dithering strength, deduced from decoding options
253 int dither_
; // whether to use dithering or not
254 VP8Random dithering_rg_
; // random generator for dithering
256 // dequantization (one set of DC/AC dequant factor per segment)
257 VP8QuantMatrix dqm_
[NUM_MB_SEGMENTS
];
264 // Boundary data cache and persistent buffers.
265 uint8_t* intra_t_
; // top intra modes values: 4 * mb_w_
266 uint8_t intra_l_
[4]; // left intra modes values
268 uint8_t segment_
; // segment of the currently parsed block
269 VP8TopSamples
* yuv_t_
; // top y/u/v samples
271 VP8MB
* mb_info_
; // contextual macroblock info (mb_w_ + 1)
272 VP8FInfo
* f_info_
; // filter strength info
273 uint8_t* yuv_b_
; // main block for Y/U/V (size = YUV_SIZE)
275 uint8_t* cache_y_
; // macroblock row for storing unfiltered samples
279 int cache_uv_stride_
;
281 // main memory chunk for the above data. Persistent.
285 // Per macroblock non-persistent infos.
286 int mb_x_
, mb_y_
; // current position, in macroblock units
287 VP8MBData
* mb_data_
; // parsed reconstruction data
289 // Filtering side-info
290 int filter_type_
; // 0=off, 1=simple, 2=complex
291 VP8FInfo fstrengths_
[NUM_MB_SEGMENTS
][2]; // precalculated per-segment/type
294 struct ALPHDecoder
* alph_dec_
; // alpha-plane decoder object
295 const uint8_t* alpha_data_
; // compressed alpha data (if present)
296 size_t alpha_data_size_
;
297 int is_alpha_decoded_
; // true if alpha_data_ is decoded in alpha_plane_
298 uint8_t* alpha_plane_
; // output. Persistent, contains the whole data.
301 int layer_colorspace_
;
302 const uint8_t* layer_data_
; // compressed layer data (if present)
303 size_t layer_data_size_
;
306 //------------------------------------------------------------------------------
307 // internal functions. Not public.
310 int VP8SetError(VP8Decoder
* const dec
,
311 VP8StatusCode error
, const char* const msg
);
314 void VP8ResetProba(VP8Proba
* const proba
);
315 void VP8ParseProba(VP8BitReader
* const br
, VP8Decoder
* const dec
);
316 void VP8ParseIntraMode(VP8BitReader
* const br
, VP8Decoder
* const dec
);
319 void VP8ParseQuant(VP8Decoder
* const dec
);
322 int VP8InitFrame(VP8Decoder
* const dec
, VP8Io
* io
);
323 // Call io->setup() and finish setting up scan parameters.
324 // After this call returns, one must always call VP8ExitCritical() with the
325 // same parameters. Both functions should be used in pair. Returns VP8_STATUS_OK
326 // if ok, otherwise sets and returns the error status on *dec.
327 VP8StatusCode
VP8EnterCritical(VP8Decoder
* const dec
, VP8Io
* const io
);
328 // Must always be called in pair with VP8EnterCritical().
329 // Returns false in case of error.
330 int VP8ExitCritical(VP8Decoder
* const dec
, VP8Io
* const io
);
331 // Return the multi-threading method to use (0=off), depending
332 // on options and bitstream size. Only for lossy decoding.
333 int VP8GetThreadMethod(const WebPDecoderOptions
* const options
,
334 const WebPHeaderStructure
* const headers
,
335 int width
, int height
);
336 // Initialize dithering post-process if needed.
337 void VP8InitDithering(const WebPDecoderOptions
* const options
,
338 VP8Decoder
* const dec
);
339 // Process the last decoded row (filtering + output).
340 int VP8ProcessRow(VP8Decoder
* const dec
, VP8Io
* const io
);
341 // To be called at the start of a new scanline, to initialize predictors.
342 void VP8InitScanline(VP8Decoder
* const dec
);
343 // Decode one macroblock. Returns false if there is not enough data.
344 int VP8DecodeMB(VP8Decoder
* const dec
, VP8BitReader
* const token_br
);
347 const uint8_t* VP8DecompressAlphaRows(VP8Decoder
* const dec
,
348 int row
, int num_rows
);
351 int VP8DecodeLayer(VP8Decoder
* const dec
);
353 //------------------------------------------------------------------------------
359 #endif /* WEBP_DEC_VP8I_H_ */