Home | History | Annotate | Download | only in dec
      1 // Copyright 2010 Google Inc. All Rights Reserved.
      2 //
      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 // -----------------------------------------------------------------------------
      7 //
      8 // VP8 decoder: internal header.
      9 //
     10 // Author: Skal (pascal.massimino (at) gmail.com)
     11 
     12 #ifndef WEBP_DEC_VP8I_H_
     13 #define WEBP_DEC_VP8I_H_
     14 
     15 #include <string.h>     // for memcpy()
     16 #include "./vp8li.h"
     17 #include "../utils/bit_reader.h"
     18 #include "../utils/thread.h"
     19 #include "../dsp/dsp.h"
     20 
     21 #if defined(__cplusplus) || defined(c_plusplus)
     22 extern "C" {
     23 #endif
     24 
     25 //------------------------------------------------------------------------------
     26 // Various defines and enums
     27 
     28 // version numbers
     29 #define DEC_MAJ_VERSION 0
     30 #define DEC_MIN_VERSION 2
     31 #define DEC_REV_VERSION 1
     32 
     33 #define ONLY_KEYFRAME_CODE      // to remove any code related to P-Frames
     34 
     35 // intra prediction modes
     36 enum { B_DC_PRED = 0,   // 4x4 modes
     37        B_TM_PRED,
     38        B_VE_PRED,
     39        B_HE_PRED,
     40        B_RD_PRED,
     41        B_VR_PRED,
     42        B_LD_PRED,
     43        B_VL_PRED,
     44        B_HD_PRED,
     45        B_HU_PRED,
     46        NUM_BMODES = B_HU_PRED + 1 - B_DC_PRED,  // = 10
     47 
     48        // Luma16 or UV modes
     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
     52 
     53        // special modes
     54        B_DC_PRED_NOTOP = 4,
     55        B_DC_PRED_NOLEFT = 5,
     56        B_DC_PRED_NOTOPLEFT = 6,
     57        NUM_B_DC_MODES = 7 };
     58 
     59 enum { MB_FEATURE_TREE_PROBS = 3,
     60        NUM_MB_SEGMENTS = 4,
     61        NUM_REF_LF_DELTAS = 4,
     62        NUM_MODE_LF_DELTAS = 4,    // I4x4, ZERO, *, SPLIT
     63        MAX_NUM_PARTITIONS = 8,
     64        // Probabilities
     65        NUM_TYPES = 4,
     66        NUM_BANDS = 8,
     67        NUM_CTX = 3,
     68        NUM_PROBAS = 11,
     69        NUM_MV_PROBAS = 19 };
     70 
     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:
     78 //
     79 //   .+------   <- only 1 pixel high
     80 //   .|yyyyt.
     81 //   .|yyyyt.
     82 //   .|yyyyt.
     83 //   .|yyyy..
     84 //   .+--.+--   <- only 1 pixel high
     85 //   .|uu.|vv
     86 //   .|uu.|vv
     87 //
     88 // Every character is a 4x4 block, with legend:
     89 //  '.' = unused
     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)
    100 
    101 //------------------------------------------------------------------------------
    102 // Headers
    103 
    104 typedef struct {
    105   uint8_t key_frame_;
    106   uint8_t profile_;
    107   uint8_t show_;
    108   uint32_t partition_length_;
    109 } VP8FrameHeader;
    110 
    111 typedef struct {
    112   uint16_t width_;
    113   uint16_t height_;
    114   uint8_t xscale_;
    115   uint8_t yscale_;
    116   uint8_t colorspace_;   // 0 = YCbCr
    117   uint8_t clamp_type_;
    118 } VP8PictureHeader;
    119 
    120 // segment features
    121 typedef struct {
    122   int use_segment_;
    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
    127 } VP8SegmentHeader;
    128 
    129 // Struct collecting all frame-persistent probabilities.
    130 typedef struct {
    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];
    137 #endif
    138 } VP8Proba;
    139 
    140 // Filter parameters
    141 typedef struct {
    142   int simple_;                  // 0=complex, 1=simple
    143   int level_;                   // [0..63]
    144   int sharpness_;               // [0..7]
    145   int use_lf_delta_;
    146   int ref_lf_delta_[NUM_REF_LF_DELTAS];
    147   int mode_lf_delta_[NUM_MODE_LF_DELTAS];
    148 } VP8FilterHeader;
    149 
    150 //------------------------------------------------------------------------------
    151 // Informations about the macroblocks.
    152 
    153 typedef struct {  // filter specs
    154   unsigned int f_level_:6;      // filter strength: 0..63
    155   unsigned int f_ilevel_:6;     // inner limit: 1..63
    156   unsigned int f_inner_:1;      // do inner filtering?
    157 } VP8FInfo;
    158 
    159 typedef struct {  // used for syntax-parsing
    160   unsigned int nz_:24;       // non-zero AC/DC coeffs (24bit)
    161   unsigned int dc_nz_:1;     // non-zero DC coeffs
    162   unsigned int skip_:1;      // block type
    163 } VP8MB;
    164 
    165 // Dequantization matrices
    166 typedef int quant_t[2];      // [DC / AC].  Can be 'uint16_t[2]' too (~slower).
    167 typedef struct {
    168   quant_t y1_mat_, y2_mat_, uv_mat_;
    169 } VP8QuantMatrix;
    170 
    171 // Persistent information needed by the parallel processing
    172 typedef struct {
    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()
    178 } VP8ThreadContext;
    179 
    180 //------------------------------------------------------------------------------
    181 // VP8Decoder: the main opaque structure handed over to user
    182 
    183 struct VP8Decoder {
    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.
    187 
    188   // Main data source
    189   VP8BitReader br_;
    190 
    191   // headers
    192   VP8FrameHeader   frm_hdr_;
    193   VP8PictureHeader pic_hdr_;
    194   VP8FilterHeader  filter_hdr_;
    195   VP8SegmentHeader segment_hdr_;
    196 
    197   // Worker
    198   WebPWorker worker_;
    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
    203 
    204   // dimension, in macroblock units.
    205   int mb_w_, mb_h_;
    206 
    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
    210 
    211   // number of partitions.
    212   int num_parts_;
    213   // per-partition boolean decoders.
    214   VP8BitReader parts_[MAX_NUM_PARTITIONS];
    215 
    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_;
    222 
    223   // dequantization (one set of DC/AC dequant factor per segment)
    224   VP8QuantMatrix dqm_[NUM_MB_SEGMENTS];
    225 
    226   // probabilities
    227   VP8Proba proba_;
    228   int use_skip_proba_;
    229   uint8_t skip_p_;
    230 #ifndef ONLY_KEYFRAME_CODE
    231   uint8_t intra_p_, last_p_, golden_p_;
    232   VP8Proba proba_saved_;
    233   int update_proba_;
    234 #endif
    235 
    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
    241 
    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
    246 
    247   uint8_t* cache_y_;     // macroblock row for storing unfiltered samples
    248   uint8_t* cache_u_;
    249   uint8_t* cache_v_;
    250   int cache_y_stride_;
    251   int cache_uv_stride_;
    252 
    253   // main memory chunk for the above data. Persistent.
    254   void* mem_;
    255   size_t mem_size_;
    256 
    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
    263 
    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.
    268   uint32_t non_zero_;
    269   uint32_t non_zero_ac_;
    270 
    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
    275 
    276   // extensions
    277   const uint8_t* alpha_data_;   // compressed alpha data (if present)
    278   size_t alpha_data_size_;
    279   uint8_t* alpha_plane_;        // output. Persistent, contains the whole data.
    280 
    281   int layer_colorspace_;
    282   const uint8_t* layer_data_;   // compressed layer data (if present)
    283   size_t layer_data_size_;
    284 };
    285 
    286 //------------------------------------------------------------------------------
    287 // internal functions. Not public.
    288 
    289 // in vp8.c
    290 int VP8SetError(VP8Decoder* const dec,
    291                 VP8StatusCode error, const char* const msg);
    292 
    293 // in tree.c
    294 void VP8ResetProba(VP8Proba* const proba);
    295 void VP8ParseProba(VP8BitReader* const br, VP8Decoder* const dec);
    296 void VP8ParseIntraMode(VP8BitReader* const br,  VP8Decoder* const dec);
    297 
    298 // in quant.c
    299 void VP8ParseQuant(VP8Decoder* const dec);
    300 
    301 // in frame.c
    302 int VP8InitFrame(VP8Decoder* const dec, VP8Io* io);
    303 // Predict a block and add residual
    304 void VP8ReconstructBlock(VP8Decoder* const dec);
    305 // Call io->setup() and finish setting up scan parameters.
    306 // After this call returns, one must always call VP8ExitCritical() with the
    307 // same parameters. Both functions should be used in pair. Returns VP8_STATUS_OK
    308 // if ok, otherwise sets and returns the error status on *dec.
    309 VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io);
    310 // Must always be called in pair with VP8EnterCritical().
    311 // Returns false in case of error.
    312 int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io);
    313 // Process the last decoded row (filtering + output)
    314 int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io);
    315 // To be called at the start of a new scanline, to initialize predictors.
    316 void VP8InitScanline(VP8Decoder* const dec);
    317 // Decode one macroblock. Returns false if there is not enough data.
    318 int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br);
    319 
    320 // in alpha.c
    321 const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
    322                                       int row, int num_rows);
    323 
    324 // in layer.c
    325 int VP8DecodeLayer(VP8Decoder* const dec);
    326 
    327 //------------------------------------------------------------------------------
    328 
    329 #if defined(__cplusplus) || defined(c_plusplus)
    330 }    // extern "C"
    331 #endif
    332 
    333 #endif  /* WEBP_DEC_VP8I_H_ */
    334