vp8i_dec.h (11804B)
1 // Copyright 2010 Google Inc. All Rights Reserved. 2 // 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 // ----------------------------------------------------------------------------- 9 // 10 // VP8 decoder: internal header. 11 // 12 // Author: Skal (pascal.massimino@gmail.com) 13 14 #ifndef WEBP_DEC_VP8I_DEC_H_ 15 #define WEBP_DEC_VP8I_DEC_H_ 16 17 #include <string.h> // for memcpy() 18 19 #include "src/dec/common_dec.h" 20 #include "src/dec/vp8_dec.h" 21 #include "src/dec/vp8li_dec.h" 22 #include "src/dec/webpi_dec.h" 23 #include "src/dsp/dsp.h" 24 #include "src/utils/bit_reader_utils.h" 25 #include "src/utils/random_utils.h" 26 #include "src/utils/thread_utils.h" 27 #include "src/webp/decode.h" 28 #include "src/webp/types.h" 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 //------------------------------------------------------------------------------ 35 // Various defines and enums 36 37 // version numbers 38 #define DEC_MAJ_VERSION 1 39 #define DEC_MIN_VERSION 6 40 #define DEC_REV_VERSION 0 41 42 // YUV-cache parameters. Cache is 32-bytes wide (= one cacheline). 43 // Constraints are: We need to store one 16x16 block of luma samples (y), 44 // and two 8x8 chroma blocks (u/v). These are better be 16-bytes aligned, 45 // in order to be SIMD-friendly. We also need to store the top, left and 46 // top-left samples (from previously decoded blocks), along with four 47 // extra top-right samples for luma (intra4x4 prediction only). 48 // One possible layout is, using 32 * (17 + 9) bytes: 49 // 50 // .+------ <- only 1 pixel high 51 // .|yyyyt. 52 // .|yyyyt. 53 // .|yyyyt. 54 // .|yyyy.. 55 // .+--.+-- <- only 1 pixel high 56 // .|uu.|vv 57 // .|uu.|vv 58 // 59 // Every character is a 4x4 block, with legend: 60 // '.' = unused 61 // 'y' = y-samples 'u' = u-samples 'v' = u-samples 62 // '|' = left sample, '-' = top sample, '+' = top-left sample 63 // 't' = extra top-right sample for 4x4 modes 64 #define YUV_SIZE (BPS * 17 + BPS * 9) 65 #define Y_OFF (BPS * 1 + 8) 66 #define U_OFF (Y_OFF + BPS * 16 + BPS) 67 #define V_OFF (U_OFF + 16) 68 69 // minimal width under which lossy multi-threading is always disabled 70 #define MIN_WIDTH_FOR_THREADS 512 71 72 //------------------------------------------------------------------------------ 73 // Headers 74 75 typedef struct { 76 uint8_t key_frame; 77 uint8_t profile; 78 uint8_t show; 79 uint32_t partition_length; 80 } VP8FrameHeader; 81 82 typedef struct { 83 uint16_t width; 84 uint16_t height; 85 uint8_t xscale; 86 uint8_t yscale; 87 uint8_t colorspace; // 0 = YCbCr 88 uint8_t clamp_type; 89 } VP8PictureHeader; 90 91 // segment features 92 typedef struct { 93 int use_segment; 94 int update_map; // whether to update the segment map or not 95 int absolute_delta; // absolute or delta values for quantizer and filter 96 int8_t quantizer[NUM_MB_SEGMENTS]; // quantization changes 97 int8_t filter_strength[NUM_MB_SEGMENTS]; // filter strength for segments 98 } VP8SegmentHeader; 99 100 // probas associated to one of the contexts 101 typedef uint8_t VP8ProbaArray[NUM_PROBAS]; 102 103 typedef struct { // all the probas associated to one band 104 VP8ProbaArray probas[NUM_CTX]; 105 } VP8BandProbas; 106 107 // Struct collecting all frame-persistent probabilities. 108 typedef struct { 109 uint8_t segments[MB_FEATURE_TREE_PROBS]; 110 // Type: 0:Intra16-AC 1:Intra16-DC 2:Chroma 3:Intra4 111 VP8BandProbas bands[NUM_TYPES][NUM_BANDS]; 112 const VP8BandProbas* bands_ptr[NUM_TYPES][16 + 1]; 113 } VP8Proba; 114 115 // Filter parameters 116 typedef struct { 117 int simple; // 0=complex, 1=simple 118 int level; // [0..63] 119 int sharpness; // [0..7] 120 int use_lf_delta; 121 int ref_lf_delta[NUM_REF_LF_DELTAS]; 122 int mode_lf_delta[NUM_MODE_LF_DELTAS]; 123 } VP8FilterHeader; 124 125 //------------------------------------------------------------------------------ 126 // Informations about the macroblocks. 127 128 typedef struct { // filter specs 129 uint8_t f_limit; // filter limit in [3..189], or 0 if no filtering 130 uint8_t f_ilevel; // inner limit in [1..63] 131 uint8_t f_inner; // do inner filtering? 132 uint8_t hev_thresh; // high edge variance threshold in [0..2] 133 } VP8FInfo; 134 135 typedef struct { // Top/Left Contexts used for syntax-parsing 136 uint8_t nz; // non-zero AC/DC coeffs (4bit for luma + 4bit for chroma) 137 uint8_t nz_dc; // non-zero DC coeff (1bit) 138 } VP8MB; 139 140 // Dequantization matrices 141 typedef int quant_t[2]; // [DC / AC]. Can be 'uint16_t[2]' too (~slower). 142 typedef struct { 143 quant_t y1_mat, y2_mat, uv_mat; 144 145 int uv_quant; // U/V quantizer value 146 int dither; // dithering amplitude (0 = off, max=255) 147 } VP8QuantMatrix; 148 149 // Data needed to reconstruct a macroblock 150 typedef struct { 151 int16_t coeffs[384]; // 384 coeffs = (16+4+4) * 4*4 152 uint8_t is_i4x4; // true if intra4x4 153 uint8_t imodes[16]; // one 16x16 mode (#0) or sixteen 4x4 modes 154 uint8_t uvmode; // chroma prediction mode 155 // bit-wise info about the content of each sub-4x4 blocks (in decoding order). 156 // Each of the 4x4 blocks for y/u/v is associated with a 2b code according to: 157 // code=0 -> no coefficient 158 // code=1 -> only DC 159 // code=2 -> first three coefficients are non-zero 160 // code=3 -> more than three coefficients are non-zero 161 // This allows to call specialized transform functions. 162 uint32_t non_zero_y; 163 uint32_t non_zero_uv; 164 uint8_t dither; // local dithering strength (deduced from non_zero*) 165 uint8_t skip; 166 uint8_t segment; 167 } VP8MBData; 168 169 // Persistent information needed by the parallel processing 170 typedef struct { 171 int id; // cache row to process (in [0..2]) 172 int mb_y; // macroblock position of the row 173 int filter_row; // true if row-filtering is needed 174 VP8FInfo* f_info; // filter strengths (swapped with dec->f_info) 175 VP8MBData* mb_data; // reconstruction data (swapped with dec->mb_data) 176 VP8Io io; // copy of the VP8Io to pass to put() 177 } VP8ThreadContext; 178 179 // Saved top samples, per macroblock. Fits into a cache-line. 180 typedef struct { 181 uint8_t y[16], u[8], v[8]; 182 } VP8TopSamples; 183 184 //------------------------------------------------------------------------------ 185 // VP8Decoder: the main opaque structure handed over to user 186 187 struct VP8Decoder { 188 VP8StatusCode status; 189 int ready; // true if ready to decode a picture with VP8Decode() 190 const char* error_msg; // set when status is not OK. 191 192 // Main data source 193 VP8BitReader br; 194 int incremental; // if true, incremental decoding is expected 195 196 // headers 197 VP8FrameHeader frm_hdr; 198 VP8PictureHeader pic_hdr; 199 VP8FilterHeader filter_hdr; 200 VP8SegmentHeader segment_hdr; 201 202 // Worker 203 WebPWorker worker; 204 int mt_method; // multi-thread method: 0=off, 1=[parse+recon][filter] 205 // 2=[parse][recon+filter] 206 int cache_id; // current cache row 207 int num_caches; // number of cached rows of 16 pixels (1, 2 or 3) 208 VP8ThreadContext thread_ctx; // Thread context 209 210 // dimension, in macroblock units. 211 int mb_w, mb_h; 212 213 // Macroblock to process/filter, depending on cropping and filter_type. 214 int tl_mb_x, tl_mb_y; // top-left MB that must be in-loop filtered 215 int br_mb_x, br_mb_y; // last bottom-right MB that must be decoded 216 217 // number of partitions minus one. 218 uint32_t num_parts_minus_one; 219 // per-partition boolean decoders. 220 VP8BitReader parts[MAX_NUM_PARTITIONS]; 221 222 // Dithering strength, deduced from decoding options 223 int dither; // whether to use dithering or not 224 VP8Random dithering_rg; // random generator for dithering 225 226 // dequantization (one set of DC/AC dequant factor per segment) 227 VP8QuantMatrix dqm[NUM_MB_SEGMENTS]; 228 229 // probabilities 230 VP8Proba proba; 231 int use_skip_proba; 232 uint8_t skip_p; 233 234 // Boundary data cache and persistent buffers. 235 uint8_t* intra_t; // top intra modes values: 4 * mb_w 236 uint8_t intra_l[4]; // left intra modes values 237 238 VP8TopSamples* yuv_t; // top y/u/v samples 239 240 VP8MB* mb_info; // contextual macroblock info (mb_w + 1) 241 VP8FInfo* f_info; // filter strength info 242 uint8_t* yuv_b; // main block for Y/U/V (size = YUV_SIZE) 243 244 uint8_t* cache_y; // macroblock row for storing unfiltered samples 245 uint8_t* cache_u; 246 uint8_t* cache_v; 247 int cache_y_stride; 248 int cache_uv_stride; 249 250 // main memory chunk for the above data. Persistent. 251 void* mem; 252 size_t mem_size; 253 254 // Per macroblock non-persistent infos. 255 int mb_x, mb_y; // current position, in macroblock units 256 VP8MBData* mb_data; // parsed reconstruction data 257 258 // Filtering side-info 259 int filter_type; // 0=off, 1=simple, 2=complex 260 VP8FInfo fstrengths[NUM_MB_SEGMENTS][2]; // precalculated per-segment/type 261 262 // Alpha 263 struct ALPHDecoder* alph_dec; // alpha-plane decoder object 264 const uint8_t* alpha_data; // compressed alpha data (if present) 265 size_t alpha_data_size; 266 int is_alpha_decoded; // true if alpha_data is decoded in alpha_plane 267 uint8_t* alpha_plane_mem; // memory allocated for alpha_plane 268 uint8_t* alpha_plane; // output. Persistent, contains the whole data. 269 const uint8_t* alpha_prev_line; // last decoded alpha row (or NULL) 270 int alpha_dithering; // derived from decoding options (0=off, 100=full) 271 }; 272 273 //------------------------------------------------------------------------------ 274 // internal functions. Not public. 275 276 // in vp8.c 277 int VP8SetError(VP8Decoder* const dec, 278 VP8StatusCode error, const char* const msg); 279 280 // in tree.c 281 void VP8ResetProba(VP8Proba* const proba); 282 void VP8ParseProba(VP8BitReader* const br, VP8Decoder* const dec); 283 // parses one row of intra mode data in partition 0, returns !eof 284 int VP8ParseIntraModeRow(VP8BitReader* const br, VP8Decoder* const dec); 285 286 // in quant.c 287 void VP8ParseQuant(VP8Decoder* const dec); 288 289 // in frame.c 290 WEBP_NODISCARD int VP8InitFrame(VP8Decoder* const dec, VP8Io* const io); 291 // Call io->setup() and finish setting up scan parameters. 292 // After this call returns, one must always call VP8ExitCritical() with the 293 // same parameters. Both functions should be used in pair. Returns VP8_STATUS_OK 294 // if ok, otherwise sets and returns the error status on *dec. 295 VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io); 296 // Must always be called in pair with VP8EnterCritical(). 297 // Returns false in case of error. 298 WEBP_NODISCARD int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io); 299 // Return the multi-threading method to use (0=off), depending 300 // on options and bitstream size. Only for lossy decoding. 301 int VP8GetThreadMethod(const WebPDecoderOptions* const options, 302 const WebPHeaderStructure* const headers, 303 int width, int height); 304 // Initialize dithering post-process if needed. 305 void VP8InitDithering(const WebPDecoderOptions* const options, 306 VP8Decoder* const dec); 307 // Process the last decoded row (filtering + output). 308 WEBP_NODISCARD int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io); 309 // To be called at the start of a new scanline, to initialize predictors. 310 void VP8InitScanline(VP8Decoder* const dec); 311 // Decode one macroblock. Returns false if there is not enough data. 312 WEBP_NODISCARD int VP8DecodeMB(VP8Decoder* const dec, 313 VP8BitReader* const token_br); 314 315 // in alpha.c 316 const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec, 317 const VP8Io* const io, 318 int row, int num_rows); 319 320 //------------------------------------------------------------------------------ 321 322 #ifdef __cplusplus 323 } // extern "C" 324 #endif 325 326 #endif // WEBP_DEC_VP8I_DEC_H_