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