Home | History | Annotate | Download | only in dec
      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 (at) gmail.com)
     13 
     14 #ifndef WEBP_DEC_VP8I_H_
     15 #define WEBP_DEC_VP8I_H_
     16 
     17 #include <string.h>     // for memcpy()
     18 #include "./vp8li.h"
     19 #include "../utils/bit_reader.h"
     20 #include "../utils/thread.h"
     21 #include "../dsp/dsp.h"
     22 
     23 #if defined(__cplusplus) || defined(c_plusplus)
     24 extern "C" {
     25 #endif
     26 
     27 //------------------------------------------------------------------------------
     28 // Various defines and enums
     29 
     30 // version numbers
     31 #define DEC_MAJ_VERSION 0
     32 #define DEC_MIN_VERSION 3
     33 #define DEC_REV_VERSION 1
     34 
     35 #define ONLY_KEYFRAME_CODE      // to remove any code related to P-Frames
     36 
     37 // intra prediction modes
     38 enum { B_DC_PRED = 0,   // 4x4 modes
     39        B_TM_PRED,
     40        B_VE_PRED,
     41        B_HE_PRED,
     42        B_RD_PRED,
     43        B_VR_PRED,
     44        B_LD_PRED,
     45        B_VL_PRED,
     46        B_HD_PRED,
     47        B_HU_PRED,
     48        NUM_BMODES = B_HU_PRED + 1 - B_DC_PRED,  // = 10
     49 
     50        // Luma16 or UV modes
     51        DC_PRED = B_DC_PRED, V_PRED = B_VE_PRED,
     52        H_PRED = B_HE_PRED, TM_PRED = B_TM_PRED,
     53        B_PRED = NUM_BMODES,   // refined I4x4 mode
     54 
     55        // special modes
     56        B_DC_PRED_NOTOP = 4,
     57        B_DC_PRED_NOLEFT = 5,
     58        B_DC_PRED_NOTOPLEFT = 6,
     59        NUM_B_DC_MODES = 7 };
     60 
     61 enum { MB_FEATURE_TREE_PROBS = 3,
     62        NUM_MB_SEGMENTS = 4,
     63        NUM_REF_LF_DELTAS = 4,
     64        NUM_MODE_LF_DELTAS = 4,    // I4x4, ZERO, *, SPLIT
     65        MAX_NUM_PARTITIONS = 8,
     66        // Probabilities
     67        NUM_TYPES = 4,
     68        NUM_BANDS = 8,
     69        NUM_CTX = 3,
     70        NUM_PROBAS = 11,
     71        NUM_MV_PROBAS = 19 };
     72 
     73 // YUV-cache parameters.
     74 // Constraints are: We need to store one 16x16 block of luma samples (y),
     75 // and two 8x8 chroma blocks (u/v). These are better be 16-bytes aligned,
     76 // in order to be SIMD-friendly. We also need to store the top, left and
     77 // top-left samples (from previously decoded blocks), along with four
     78 // extra top-right samples for luma (intra4x4 prediction only).
     79 // One possible layout is, using 32 * (17 + 9) bytes:
     80 //
     81 //   .+------   <- only 1 pixel high
     82 //   .|yyyyt.
     83 //   .|yyyyt.
     84 //   .|yyyyt.
     85 //   .|yyyy..
     86 //   .+--.+--   <- only 1 pixel high
     87 //   .|uu.|vv
     88 //   .|uu.|vv
     89 //
     90 // Every character is a 4x4 block, with legend:
     91 //  '.' = unused
     92 //  'y' = y-samples   'u' = u-samples     'v' = u-samples
     93 //  '|' = left sample,   '-' = top sample,    '+' = top-left sample
     94 //  't' = extra top-right sample for 4x4 modes
     95 // With this layout, BPS (=Bytes Per Scan-line) is one cacheline size.
     96 #define BPS       32    // this is the common stride used by yuv[]
     97 #define YUV_SIZE (BPS * 17 + BPS * 9)
     98 #define Y_SIZE   (BPS * 17)
     99 #define Y_OFF    (BPS * 1 + 8)
    100 #define U_OFF    (Y_OFF + BPS * 16 + BPS)
    101 #define V_OFF    (U_OFF + 16)
    102 
    103 //------------------------------------------------------------------------------
    104 // Headers
    105 
    106 typedef struct {
    107   uint8_t key_frame_;
    108   uint8_t profile_;
    109   uint8_t show_;
    110   uint32_t partition_length_;
    111 } VP8FrameHeader;
    112 
    113 typedef struct {
    114   uint16_t width_;
    115   uint16_t height_;
    116   uint8_t xscale_;
    117   uint8_t yscale_;
    118   uint8_t colorspace_;   // 0 = YCbCr
    119   uint8_t clamp_type_;
    120 } VP8PictureHeader;
    121 
    122 // segment features
    123 typedef struct {
    124   int use_segment_;
    125   int update_map_;        // whether to update the segment map or not
    126   int absolute_delta_;    // absolute or delta values for quantizer and filter
    127   int8_t quantizer_[NUM_MB_SEGMENTS];        // quantization changes
    128   int8_t filter_strength_[NUM_MB_SEGMENTS];  // filter strength for segments
    129 } VP8SegmentHeader;
    130 
    131 // Struct collecting all frame-persistent probabilities.
    132 typedef struct {
    133   uint8_t segments_[MB_FEATURE_TREE_PROBS];
    134   // Type: 0:Intra16-AC  1:Intra16-DC   2:Chroma   3:Intra4
    135   uint8_t coeffs_[NUM_TYPES][NUM_BANDS][NUM_CTX][NUM_PROBAS];
    136 #ifndef ONLY_KEYFRAME_CODE
    137   uint8_t ymode_[4], uvmode_[3];
    138   uint8_t mv_[2][NUM_MV_PROBAS];
    139 #endif
    140 } VP8Proba;
    141 
    142 // Filter parameters
    143 typedef struct {
    144   int simple_;                  // 0=complex, 1=simple
    145   int level_;                   // [0..63]
    146   int sharpness_;               // [0..7]
    147   int use_lf_delta_;
    148   int ref_lf_delta_[NUM_REF_LF_DELTAS];
    149   int mode_lf_delta_[NUM_MODE_LF_DELTAS];
    150 } VP8FilterHeader;
    151 
    152 //------------------------------------------------------------------------------
    153 // Informations about the macroblocks.
    154 
    155 typedef struct {  // filter specs
    156   unsigned int f_level_:6;      // filter strength: 0..63
    157   unsigned int f_ilevel_:6;     // inner limit: 1..63
    158   unsigned int f_inner_:1;      // do inner filtering?
    159 } VP8FInfo;
    160 
    161 typedef struct {  // used for syntax-parsing
    162   unsigned int nz_:24;       // non-zero AC/DC coeffs (24bit)
    163   unsigned int dc_nz_:1;     // non-zero DC coeffs
    164   unsigned int skip_:1;      // block type
    165 } VP8MB;
    166 
    167 // Dequantization matrices
    168 typedef int quant_t[2];      // [DC / AC].  Can be 'uint16_t[2]' too (~slower).
    169 typedef struct {
    170   quant_t y1_mat_, y2_mat_, uv_mat_;
    171 } VP8QuantMatrix;
    172 
    173 // Persistent information needed by the parallel processing
    174 typedef struct {
    175   int id_;            // cache row to process (in [0..2])
    176   int mb_y_;          // macroblock position of the row
    177   int filter_row_;    // true if row-filtering is needed
    178   VP8FInfo* f_info_;  // filter strengths
    179   VP8Io io_;          // copy of the VP8Io to pass to put()
    180 } VP8ThreadContext;
    181 
    182 //------------------------------------------------------------------------------
    183 // VP8Decoder: the main opaque structure handed over to user
    184 
    185 struct VP8Decoder {
    186   VP8StatusCode status_;
    187   int ready_;     // true if ready to decode a picture with VP8Decode()
    188   const char* error_msg_;  // set when status_ is not OK.
    189 
    190   // Main data source
    191   VP8BitReader br_;
    192 
    193   // headers
    194   VP8FrameHeader   frm_hdr_;
    195   VP8PictureHeader pic_hdr_;
    196   VP8FilterHeader  filter_hdr_;
    197   VP8SegmentHeader segment_hdr_;
    198 
    199   // Worker
    200   WebPWorker worker_;
    201   int use_threads_;    // use multi-thread
    202   int cache_id_;       // current cache row
    203   int num_caches_;     // number of cached rows of 16 pixels (1, 2 or 3)
    204   VP8ThreadContext thread_ctx_;  // Thread context
    205 
    206   // dimension, in macroblock units.
    207   int mb_w_, mb_h_;
    208 
    209   // Macroblock to process/filter, depending on cropping and filter_type.
    210   int tl_mb_x_, tl_mb_y_;  // top-left MB that must be in-loop filtered
    211   int br_mb_x_, br_mb_y_;  // last bottom-right MB that must be decoded
    212 
    213   // number of partitions.
    214   int num_parts_;
    215   // per-partition boolean decoders.
    216   VP8BitReader parts_[MAX_NUM_PARTITIONS];
    217 
    218   // buffer refresh flags
    219   //   bit 0: refresh Gold, bit 1: refresh Alt
    220   //   bit 2-3: copy to Gold, bit 4-5: copy to Alt
    221   //   bit 6: Gold sign bias, bit 7: Alt sign bias
    222   //   bit 8: refresh last frame
    223   uint32_t buffer_flags_;
    224 
    225   // dequantization (one set of DC/AC dequant factor per segment)
    226   VP8QuantMatrix dqm_[NUM_MB_SEGMENTS];
    227 
    228   // probabilities
    229   VP8Proba proba_;
    230   int use_skip_proba_;
    231   uint8_t skip_p_;
    232 #ifndef ONLY_KEYFRAME_CODE
    233   uint8_t intra_p_, last_p_, golden_p_;
    234   VP8Proba proba_saved_;
    235   int update_proba_;
    236 #endif
    237 
    238   // Boundary data cache and persistent buffers.
    239   uint8_t* intra_t_;     // top intra modes values: 4 * mb_w_
    240   uint8_t  intra_l_[4];  // left intra modes values
    241   uint8_t* y_t_;         // top luma samples: 16 * mb_w_
    242   uint8_t* u_t_, *v_t_;  // top u/v samples: 8 * mb_w_ each
    243 
    244   VP8MB* mb_info_;       // contextual macroblock info (mb_w_ + 1)
    245   VP8FInfo* f_info_;     // filter strength info
    246   uint8_t* yuv_b_;       // main block for Y/U/V (size = YUV_SIZE)
    247   int16_t* coeffs_;      // 384 coeffs = (16+8+8) * 4*4
    248 
    249   uint8_t* cache_y_;     // macroblock row for storing unfiltered samples
    250   uint8_t* cache_u_;
    251   uint8_t* cache_v_;
    252   int cache_y_stride_;
    253   int cache_uv_stride_;
    254 
    255   // main memory chunk for the above data. Persistent.
    256   void* mem_;
    257   size_t mem_size_;
    258 
    259   // Per macroblock non-persistent infos.
    260   int mb_x_, mb_y_;       // current position, in macroblock units
    261   uint8_t is_i4x4_;       // true if intra4x4
    262   uint8_t imodes_[16];    // one 16x16 mode (#0) or sixteen 4x4 modes
    263   uint8_t uvmode_;        // chroma prediction mode
    264   uint8_t segment_;       // block's segment
    265 
    266   // bit-wise info about the content of each sub-4x4 blocks: there are 16 bits
    267   // for luma (bits #0->#15), then 4 bits for chroma-u (#16->#19) and 4 bits for
    268   // chroma-v (#20->#23), each corresponding to one 4x4 block in decoding order.
    269   // If the bit is set, the 4x4 block contains some non-zero coefficients.
    270   uint32_t non_zero_;
    271   uint32_t non_zero_ac_;
    272 
    273   // Filtering side-info
    274   int filter_type_;                          // 0=off, 1=simple, 2=complex
    275   int filter_row_;                           // per-row flag
    276   VP8FInfo fstrengths_[NUM_MB_SEGMENTS][2];  // precalculated per-segment/type
    277 
    278   // extensions
    279   const uint8_t* alpha_data_;   // compressed alpha data (if present)
    280   size_t alpha_data_size_;
    281   int is_alpha_decoded_;  // true if alpha_data_ is decoded in alpha_plane_
    282   uint8_t* alpha_plane_;        // output. Persistent, contains the whole data.
    283 
    284   int layer_colorspace_;
    285   const uint8_t* layer_data_;   // compressed layer data (if present)
    286   size_t layer_data_size_;
    287 };
    288 
    289 //------------------------------------------------------------------------------
    290 // internal functions. Not public.
    291 
    292 // in vp8.c
    293 int VP8SetError(VP8Decoder* const dec,
    294                 VP8StatusCode error, const char* const msg);
    295 
    296 // in tree.c
    297 void VP8ResetProba(VP8Proba* const proba);
    298 void VP8ParseProba(VP8BitReader* const br, VP8Decoder* const dec);
    299 void VP8ParseIntraMode(VP8BitReader* const br,  VP8Decoder* const dec);
    300 
    301 // in quant.c
    302 void VP8ParseQuant(VP8Decoder* const dec);
    303 
    304 // in frame.c
    305 int VP8InitFrame(VP8Decoder* const dec, VP8Io* io);
    306 // Predict a block and add residual
    307 void VP8ReconstructBlock(VP8Decoder* const dec);
    308 // Call io->setup() and finish setting up scan parameters.
    309 // After this call returns, one must always call VP8ExitCritical() with the
    310 // same parameters. Both functions should be used in pair. Returns VP8_STATUS_OK
    311 // if ok, otherwise sets and returns the error status on *dec.
    312 VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io);
    313 // Must always be called in pair with VP8EnterCritical().
    314 // Returns false in case of error.
    315 int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io);
    316 // Process the last decoded row (filtering + output)
    317 int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io);
    318 // To be called at the start of a new scanline, to initialize predictors.
    319 void VP8InitScanline(VP8Decoder* const dec);
    320 // Decode one macroblock. Returns false if there is not enough data.
    321 int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br);
    322 
    323 // in alpha.c
    324 const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
    325                                       int row, int num_rows);
    326 
    327 // in layer.c
    328 int VP8DecodeLayer(VP8Decoder* const dec);
    329 
    330 //------------------------------------------------------------------------------
    331 
    332 #if defined(__cplusplus) || defined(c_plusplus)
    333 }    // extern "C"
    334 #endif
    335 
    336 #endif  /* WEBP_DEC_VP8I_H_ */
    337