1*b2055c35SXin Li // Copyright 2010 Google Inc. All Rights Reserved. 2*b2055c35SXin Li // 3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license 4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source 5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found 6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may 7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree. 8*b2055c35SXin Li // ----------------------------------------------------------------------------- 9*b2055c35SXin Li // 10*b2055c35SXin Li // VP8 decoder: internal header. 11*b2055c35SXin Li // 12*b2055c35SXin Li // Author: Skal ([email protected]) 13*b2055c35SXin Li 14*b2055c35SXin Li #ifndef WEBP_DEC_VP8I_DEC_H_ 15*b2055c35SXin Li #define WEBP_DEC_VP8I_DEC_H_ 16*b2055c35SXin Li 17*b2055c35SXin Li #include <string.h> // for memcpy() 18*b2055c35SXin Li #include "src/dec/common_dec.h" 19*b2055c35SXin Li #include "src/dec/vp8li_dec.h" 20*b2055c35SXin Li #include "src/utils/bit_reader_utils.h" 21*b2055c35SXin Li #include "src/utils/random_utils.h" 22*b2055c35SXin Li #include "src/utils/thread_utils.h" 23*b2055c35SXin Li #include "src/dsp/dsp.h" 24*b2055c35SXin Li #include "src/webp/types.h" 25*b2055c35SXin Li 26*b2055c35SXin Li #ifdef __cplusplus 27*b2055c35SXin Li extern "C" { 28*b2055c35SXin Li #endif 29*b2055c35SXin Li 30*b2055c35SXin Li //------------------------------------------------------------------------------ 31*b2055c35SXin Li // Various defines and enums 32*b2055c35SXin Li 33*b2055c35SXin Li // version numbers 34*b2055c35SXin Li #define DEC_MAJ_VERSION 1 35*b2055c35SXin Li #define DEC_MIN_VERSION 4 36*b2055c35SXin Li #define DEC_REV_VERSION 0 37*b2055c35SXin Li 38*b2055c35SXin Li // YUV-cache parameters. Cache is 32-bytes wide (= one cacheline). 39*b2055c35SXin Li // Constraints are: We need to store one 16x16 block of luma samples (y), 40*b2055c35SXin Li // and two 8x8 chroma blocks (u/v). These are better be 16-bytes aligned, 41*b2055c35SXin Li // in order to be SIMD-friendly. We also need to store the top, left and 42*b2055c35SXin Li // top-left samples (from previously decoded blocks), along with four 43*b2055c35SXin Li // extra top-right samples for luma (intra4x4 prediction only). 44*b2055c35SXin Li // One possible layout is, using 32 * (17 + 9) bytes: 45*b2055c35SXin Li // 46*b2055c35SXin Li // .+------ <- only 1 pixel high 47*b2055c35SXin Li // .|yyyyt. 48*b2055c35SXin Li // .|yyyyt. 49*b2055c35SXin Li // .|yyyyt. 50*b2055c35SXin Li // .|yyyy.. 51*b2055c35SXin Li // .+--.+-- <- only 1 pixel high 52*b2055c35SXin Li // .|uu.|vv 53*b2055c35SXin Li // .|uu.|vv 54*b2055c35SXin Li // 55*b2055c35SXin Li // Every character is a 4x4 block, with legend: 56*b2055c35SXin Li // '.' = unused 57*b2055c35SXin Li // 'y' = y-samples 'u' = u-samples 'v' = u-samples 58*b2055c35SXin Li // '|' = left sample, '-' = top sample, '+' = top-left sample 59*b2055c35SXin Li // 't' = extra top-right sample for 4x4 modes 60*b2055c35SXin Li #define YUV_SIZE (BPS * 17 + BPS * 9) 61*b2055c35SXin Li #define Y_OFF (BPS * 1 + 8) 62*b2055c35SXin Li #define U_OFF (Y_OFF + BPS * 16 + BPS) 63*b2055c35SXin Li #define V_OFF (U_OFF + 16) 64*b2055c35SXin Li 65*b2055c35SXin Li // minimal width under which lossy multi-threading is always disabled 66*b2055c35SXin Li #define MIN_WIDTH_FOR_THREADS 512 67*b2055c35SXin Li 68*b2055c35SXin Li //------------------------------------------------------------------------------ 69*b2055c35SXin Li // Headers 70*b2055c35SXin Li 71*b2055c35SXin Li typedef struct { 72*b2055c35SXin Li uint8_t key_frame_; 73*b2055c35SXin Li uint8_t profile_; 74*b2055c35SXin Li uint8_t show_; 75*b2055c35SXin Li uint32_t partition_length_; 76*b2055c35SXin Li } VP8FrameHeader; 77*b2055c35SXin Li 78*b2055c35SXin Li typedef struct { 79*b2055c35SXin Li uint16_t width_; 80*b2055c35SXin Li uint16_t height_; 81*b2055c35SXin Li uint8_t xscale_; 82*b2055c35SXin Li uint8_t yscale_; 83*b2055c35SXin Li uint8_t colorspace_; // 0 = YCbCr 84*b2055c35SXin Li uint8_t clamp_type_; 85*b2055c35SXin Li } VP8PictureHeader; 86*b2055c35SXin Li 87*b2055c35SXin Li // segment features 88*b2055c35SXin Li typedef struct { 89*b2055c35SXin Li int use_segment_; 90*b2055c35SXin Li int update_map_; // whether to update the segment map or not 91*b2055c35SXin Li int absolute_delta_; // absolute or delta values for quantizer and filter 92*b2055c35SXin Li int8_t quantizer_[NUM_MB_SEGMENTS]; // quantization changes 93*b2055c35SXin Li int8_t filter_strength_[NUM_MB_SEGMENTS]; // filter strength for segments 94*b2055c35SXin Li } VP8SegmentHeader; 95*b2055c35SXin Li 96*b2055c35SXin Li // probas associated to one of the contexts 97*b2055c35SXin Li typedef uint8_t VP8ProbaArray[NUM_PROBAS]; 98*b2055c35SXin Li 99*b2055c35SXin Li typedef struct { // all the probas associated to one band 100*b2055c35SXin Li VP8ProbaArray probas_[NUM_CTX]; 101*b2055c35SXin Li } VP8BandProbas; 102*b2055c35SXin Li 103*b2055c35SXin Li // Struct collecting all frame-persistent probabilities. 104*b2055c35SXin Li typedef struct { 105*b2055c35SXin Li uint8_t segments_[MB_FEATURE_TREE_PROBS]; 106*b2055c35SXin Li // Type: 0:Intra16-AC 1:Intra16-DC 2:Chroma 3:Intra4 107*b2055c35SXin Li VP8BandProbas bands_[NUM_TYPES][NUM_BANDS]; 108*b2055c35SXin Li const VP8BandProbas* bands_ptr_[NUM_TYPES][16 + 1]; 109*b2055c35SXin Li } VP8Proba; 110*b2055c35SXin Li 111*b2055c35SXin Li // Filter parameters 112*b2055c35SXin Li typedef struct { 113*b2055c35SXin Li int simple_; // 0=complex, 1=simple 114*b2055c35SXin Li int level_; // [0..63] 115*b2055c35SXin Li int sharpness_; // [0..7] 116*b2055c35SXin Li int use_lf_delta_; 117*b2055c35SXin Li int ref_lf_delta_[NUM_REF_LF_DELTAS]; 118*b2055c35SXin Li int mode_lf_delta_[NUM_MODE_LF_DELTAS]; 119*b2055c35SXin Li } VP8FilterHeader; 120*b2055c35SXin Li 121*b2055c35SXin Li //------------------------------------------------------------------------------ 122*b2055c35SXin Li // Informations about the macroblocks. 123*b2055c35SXin Li 124*b2055c35SXin Li typedef struct { // filter specs 125*b2055c35SXin Li uint8_t f_limit_; // filter limit in [3..189], or 0 if no filtering 126*b2055c35SXin Li uint8_t f_ilevel_; // inner limit in [1..63] 127*b2055c35SXin Li uint8_t f_inner_; // do inner filtering? 128*b2055c35SXin Li uint8_t hev_thresh_; // high edge variance threshold in [0..2] 129*b2055c35SXin Li } VP8FInfo; 130*b2055c35SXin Li 131*b2055c35SXin Li typedef struct { // Top/Left Contexts used for syntax-parsing 132*b2055c35SXin Li uint8_t nz_; // non-zero AC/DC coeffs (4bit for luma + 4bit for chroma) 133*b2055c35SXin Li uint8_t nz_dc_; // non-zero DC coeff (1bit) 134*b2055c35SXin Li } VP8MB; 135*b2055c35SXin Li 136*b2055c35SXin Li // Dequantization matrices 137*b2055c35SXin Li typedef int quant_t[2]; // [DC / AC]. Can be 'uint16_t[2]' too (~slower). 138*b2055c35SXin Li typedef struct { 139*b2055c35SXin Li quant_t y1_mat_, y2_mat_, uv_mat_; 140*b2055c35SXin Li 141*b2055c35SXin Li int uv_quant_; // U/V quantizer value 142*b2055c35SXin Li int dither_; // dithering amplitude (0 = off, max=255) 143*b2055c35SXin Li } VP8QuantMatrix; 144*b2055c35SXin Li 145*b2055c35SXin Li // Data needed to reconstruct a macroblock 146*b2055c35SXin Li typedef struct { 147*b2055c35SXin Li int16_t coeffs_[384]; // 384 coeffs = (16+4+4) * 4*4 148*b2055c35SXin Li uint8_t is_i4x4_; // true if intra4x4 149*b2055c35SXin Li uint8_t imodes_[16]; // one 16x16 mode (#0) or sixteen 4x4 modes 150*b2055c35SXin Li uint8_t uvmode_; // chroma prediction mode 151*b2055c35SXin Li // bit-wise info about the content of each sub-4x4 blocks (in decoding order). 152*b2055c35SXin Li // Each of the 4x4 blocks for y/u/v is associated with a 2b code according to: 153*b2055c35SXin Li // code=0 -> no coefficient 154*b2055c35SXin Li // code=1 -> only DC 155*b2055c35SXin Li // code=2 -> first three coefficients are non-zero 156*b2055c35SXin Li // code=3 -> more than three coefficients are non-zero 157*b2055c35SXin Li // This allows to call specialized transform functions. 158*b2055c35SXin Li uint32_t non_zero_y_; 159*b2055c35SXin Li uint32_t non_zero_uv_; 160*b2055c35SXin Li uint8_t dither_; // local dithering strength (deduced from non_zero_*) 161*b2055c35SXin Li uint8_t skip_; 162*b2055c35SXin Li uint8_t segment_; 163*b2055c35SXin Li } VP8MBData; 164*b2055c35SXin Li 165*b2055c35SXin Li // Persistent information needed by the parallel processing 166*b2055c35SXin Li typedef struct { 167*b2055c35SXin Li int id_; // cache row to process (in [0..2]) 168*b2055c35SXin Li int mb_y_; // macroblock position of the row 169*b2055c35SXin Li int filter_row_; // true if row-filtering is needed 170*b2055c35SXin Li VP8FInfo* f_info_; // filter strengths (swapped with dec->f_info_) 171*b2055c35SXin Li VP8MBData* mb_data_; // reconstruction data (swapped with dec->mb_data_) 172*b2055c35SXin Li VP8Io io_; // copy of the VP8Io to pass to put() 173*b2055c35SXin Li } VP8ThreadContext; 174*b2055c35SXin Li 175*b2055c35SXin Li // Saved top samples, per macroblock. Fits into a cache-line. 176*b2055c35SXin Li typedef struct { 177*b2055c35SXin Li uint8_t y[16], u[8], v[8]; 178*b2055c35SXin Li } VP8TopSamples; 179*b2055c35SXin Li 180*b2055c35SXin Li //------------------------------------------------------------------------------ 181*b2055c35SXin Li // VP8Decoder: the main opaque structure handed over to user 182*b2055c35SXin Li 183*b2055c35SXin Li struct VP8Decoder { 184*b2055c35SXin Li VP8StatusCode status_; 185*b2055c35SXin Li int ready_; // true if ready to decode a picture with VP8Decode() 186*b2055c35SXin Li const char* error_msg_; // set when status_ is not OK. 187*b2055c35SXin Li 188*b2055c35SXin Li // Main data source 189*b2055c35SXin Li VP8BitReader br_; 190*b2055c35SXin Li int incremental_; // if true, incremental decoding is expected 191*b2055c35SXin Li 192*b2055c35SXin Li // headers 193*b2055c35SXin Li VP8FrameHeader frm_hdr_; 194*b2055c35SXin Li VP8PictureHeader pic_hdr_; 195*b2055c35SXin Li VP8FilterHeader filter_hdr_; 196*b2055c35SXin Li VP8SegmentHeader segment_hdr_; 197*b2055c35SXin Li 198*b2055c35SXin Li // Worker 199*b2055c35SXin Li WebPWorker worker_; 200*b2055c35SXin Li int mt_method_; // multi-thread method: 0=off, 1=[parse+recon][filter] 201*b2055c35SXin Li // 2=[parse][recon+filter] 202*b2055c35SXin Li int cache_id_; // current cache row 203*b2055c35SXin Li int num_caches_; // number of cached rows of 16 pixels (1, 2 or 3) 204*b2055c35SXin Li VP8ThreadContext thread_ctx_; // Thread context 205*b2055c35SXin Li 206*b2055c35SXin Li // dimension, in macroblock units. 207*b2055c35SXin Li int mb_w_, mb_h_; 208*b2055c35SXin Li 209*b2055c35SXin Li // Macroblock to process/filter, depending on cropping and filter_type. 210*b2055c35SXin Li int tl_mb_x_, tl_mb_y_; // top-left MB that must be in-loop filtered 211*b2055c35SXin Li int br_mb_x_, br_mb_y_; // last bottom-right MB that must be decoded 212*b2055c35SXin Li 213*b2055c35SXin Li // number of partitions minus one. 214*b2055c35SXin Li uint32_t num_parts_minus_one_; 215*b2055c35SXin Li // per-partition boolean decoders. 216*b2055c35SXin Li VP8BitReader parts_[MAX_NUM_PARTITIONS]; 217*b2055c35SXin Li 218*b2055c35SXin Li // Dithering strength, deduced from decoding options 219*b2055c35SXin Li int dither_; // whether to use dithering or not 220*b2055c35SXin Li VP8Random dithering_rg_; // random generator for dithering 221*b2055c35SXin Li 222*b2055c35SXin Li // dequantization (one set of DC/AC dequant factor per segment) 223*b2055c35SXin Li VP8QuantMatrix dqm_[NUM_MB_SEGMENTS]; 224*b2055c35SXin Li 225*b2055c35SXin Li // probabilities 226*b2055c35SXin Li VP8Proba proba_; 227*b2055c35SXin Li int use_skip_proba_; 228*b2055c35SXin Li uint8_t skip_p_; 229*b2055c35SXin Li 230*b2055c35SXin Li // Boundary data cache and persistent buffers. 231*b2055c35SXin Li uint8_t* intra_t_; // top intra modes values: 4 * mb_w_ 232*b2055c35SXin Li uint8_t intra_l_[4]; // left intra modes values 233*b2055c35SXin Li 234*b2055c35SXin Li VP8TopSamples* yuv_t_; // top y/u/v samples 235*b2055c35SXin Li 236*b2055c35SXin Li VP8MB* mb_info_; // contextual macroblock info (mb_w_ + 1) 237*b2055c35SXin Li VP8FInfo* f_info_; // filter strength info 238*b2055c35SXin Li uint8_t* yuv_b_; // main block for Y/U/V (size = YUV_SIZE) 239*b2055c35SXin Li 240*b2055c35SXin Li uint8_t* cache_y_; // macroblock row for storing unfiltered samples 241*b2055c35SXin Li uint8_t* cache_u_; 242*b2055c35SXin Li uint8_t* cache_v_; 243*b2055c35SXin Li int cache_y_stride_; 244*b2055c35SXin Li int cache_uv_stride_; 245*b2055c35SXin Li 246*b2055c35SXin Li // main memory chunk for the above data. Persistent. 247*b2055c35SXin Li void* mem_; 248*b2055c35SXin Li size_t mem_size_; 249*b2055c35SXin Li 250*b2055c35SXin Li // Per macroblock non-persistent infos. 251*b2055c35SXin Li int mb_x_, mb_y_; // current position, in macroblock units 252*b2055c35SXin Li VP8MBData* mb_data_; // parsed reconstruction data 253*b2055c35SXin Li 254*b2055c35SXin Li // Filtering side-info 255*b2055c35SXin Li int filter_type_; // 0=off, 1=simple, 2=complex 256*b2055c35SXin Li VP8FInfo fstrengths_[NUM_MB_SEGMENTS][2]; // precalculated per-segment/type 257*b2055c35SXin Li 258*b2055c35SXin Li // Alpha 259*b2055c35SXin Li struct ALPHDecoder* alph_dec_; // alpha-plane decoder object 260*b2055c35SXin Li const uint8_t* alpha_data_; // compressed alpha data (if present) 261*b2055c35SXin Li size_t alpha_data_size_; 262*b2055c35SXin Li int is_alpha_decoded_; // true if alpha_data_ is decoded in alpha_plane_ 263*b2055c35SXin Li uint8_t* alpha_plane_mem_; // memory allocated for alpha_plane_ 264*b2055c35SXin Li uint8_t* alpha_plane_; // output. Persistent, contains the whole data. 265*b2055c35SXin Li const uint8_t* alpha_prev_line_; // last decoded alpha row (or NULL) 266*b2055c35SXin Li int alpha_dithering_; // derived from decoding options (0=off, 100=full) 267*b2055c35SXin Li }; 268*b2055c35SXin Li 269*b2055c35SXin Li //------------------------------------------------------------------------------ 270*b2055c35SXin Li // internal functions. Not public. 271*b2055c35SXin Li 272*b2055c35SXin Li // in vp8.c 273*b2055c35SXin Li int VP8SetError(VP8Decoder* const dec, 274*b2055c35SXin Li VP8StatusCode error, const char* const msg); 275*b2055c35SXin Li 276*b2055c35SXin Li // in tree.c 277*b2055c35SXin Li void VP8ResetProba(VP8Proba* const proba); 278*b2055c35SXin Li void VP8ParseProba(VP8BitReader* const br, VP8Decoder* const dec); 279*b2055c35SXin Li // parses one row of intra mode data in partition 0, returns !eof 280*b2055c35SXin Li int VP8ParseIntraModeRow(VP8BitReader* const br, VP8Decoder* const dec); 281*b2055c35SXin Li 282*b2055c35SXin Li // in quant.c 283*b2055c35SXin Li void VP8ParseQuant(VP8Decoder* const dec); 284*b2055c35SXin Li 285*b2055c35SXin Li // in frame.c 286*b2055c35SXin Li WEBP_NODISCARD int VP8InitFrame(VP8Decoder* const dec, VP8Io* const io); 287*b2055c35SXin Li // Call io->setup() and finish setting up scan parameters. 288*b2055c35SXin Li // After this call returns, one must always call VP8ExitCritical() with the 289*b2055c35SXin Li // same parameters. Both functions should be used in pair. Returns VP8_STATUS_OK 290*b2055c35SXin Li // if ok, otherwise sets and returns the error status on *dec. 291*b2055c35SXin Li VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io); 292*b2055c35SXin Li // Must always be called in pair with VP8EnterCritical(). 293*b2055c35SXin Li // Returns false in case of error. 294*b2055c35SXin Li WEBP_NODISCARD int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io); 295*b2055c35SXin Li // Return the multi-threading method to use (0=off), depending 296*b2055c35SXin Li // on options and bitstream size. Only for lossy decoding. 297*b2055c35SXin Li int VP8GetThreadMethod(const WebPDecoderOptions* const options, 298*b2055c35SXin Li const WebPHeaderStructure* const headers, 299*b2055c35SXin Li int width, int height); 300*b2055c35SXin Li // Initialize dithering post-process if needed. 301*b2055c35SXin Li void VP8InitDithering(const WebPDecoderOptions* const options, 302*b2055c35SXin Li VP8Decoder* const dec); 303*b2055c35SXin Li // Process the last decoded row (filtering + output). 304*b2055c35SXin Li WEBP_NODISCARD int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io); 305*b2055c35SXin Li // To be called at the start of a new scanline, to initialize predictors. 306*b2055c35SXin Li void VP8InitScanline(VP8Decoder* const dec); 307*b2055c35SXin Li // Decode one macroblock. Returns false if there is not enough data. 308*b2055c35SXin Li WEBP_NODISCARD int VP8DecodeMB(VP8Decoder* const dec, 309*b2055c35SXin Li VP8BitReader* const token_br); 310*b2055c35SXin Li 311*b2055c35SXin Li // in alpha.c 312*b2055c35SXin Li const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec, 313*b2055c35SXin Li const VP8Io* const io, 314*b2055c35SXin Li int row, int num_rows); 315*b2055c35SXin Li 316*b2055c35SXin Li //------------------------------------------------------------------------------ 317*b2055c35SXin Li 318*b2055c35SXin Li #ifdef __cplusplus 319*b2055c35SXin Li } // extern "C" 320*b2055c35SXin Li #endif 321*b2055c35SXin Li 322*b2055c35SXin Li #endif // WEBP_DEC_VP8I_DEC_H_ 323