Blame src/dec/vp8i_dec.h

Packit 9c6abc
// Copyright 2010 Google Inc. All Rights Reserved.
Packit 9c6abc
//
Packit 9c6abc
// Use of this source code is governed by a BSD-style license
Packit 9c6abc
// that can be found in the COPYING file in the root of the source
Packit 9c6abc
// tree. An additional intellectual property rights grant can be found
Packit 9c6abc
// in the file PATENTS. All contributing project authors may
Packit 9c6abc
// be found in the AUTHORS file in the root of the source tree.
Packit 9c6abc
// -----------------------------------------------------------------------------
Packit 9c6abc
//
Packit 9c6abc
// VP8 decoder: internal header.
Packit 9c6abc
//
Packit 9c6abc
// Author: Skal (pascal.massimino@gmail.com)
Packit 9c6abc
Packit 9c6abc
#ifndef WEBP_DEC_VP8I_DEC_H_
Packit 9c6abc
#define WEBP_DEC_VP8I_DEC_H_
Packit 9c6abc
Packit 9c6abc
#include <string.h>     // for memcpy()
Packit 9c6abc
#include "src/dec/common_dec.h"
Packit 9c6abc
#include "src/dec/vp8li_dec.h"
Packit 9c6abc
#include "src/utils/bit_reader_utils.h"
Packit 9c6abc
#include "src/utils/random_utils.h"
Packit 9c6abc
#include "src/utils/thread_utils.h"
Packit 9c6abc
#include "src/dsp/dsp.h"
Packit 9c6abc
Packit 9c6abc
#ifdef __cplusplus
Packit 9c6abc
extern "C" {
Packit 9c6abc
#endif
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
// Various defines and enums
Packit 9c6abc
Packit 9c6abc
// version numbers
Packit 9c6abc
#define DEC_MAJ_VERSION 1
Packit 9c6abc
#define DEC_MIN_VERSION 0
Packit 9c6abc
#define DEC_REV_VERSION 0
Packit 9c6abc
Packit 9c6abc
// YUV-cache parameters. Cache is 32-bytes wide (= one cacheline).
Packit 9c6abc
// Constraints are: We need to store one 16x16 block of luma samples (y),
Packit 9c6abc
// and two 8x8 chroma blocks (u/v). These are better be 16-bytes aligned,
Packit 9c6abc
// in order to be SIMD-friendly. We also need to store the top, left and
Packit 9c6abc
// top-left samples (from previously decoded blocks), along with four
Packit 9c6abc
// extra top-right samples for luma (intra4x4 prediction only).
Packit 9c6abc
// One possible layout is, using 32 * (17 + 9) bytes:
Packit 9c6abc
//
Packit 9c6abc
//   .+------   <- only 1 pixel high
Packit 9c6abc
//   .|yyyyt.
Packit 9c6abc
//   .|yyyyt.
Packit 9c6abc
//   .|yyyyt.
Packit 9c6abc
//   .|yyyy..
Packit 9c6abc
//   .+--.+--   <- only 1 pixel high
Packit 9c6abc
//   .|uu.|vv
Packit 9c6abc
//   .|uu.|vv
Packit 9c6abc
//
Packit 9c6abc
// Every character is a 4x4 block, with legend:
Packit 9c6abc
//  '.' = unused
Packit 9c6abc
//  'y' = y-samples   'u' = u-samples     'v' = u-samples
Packit 9c6abc
//  '|' = left sample,   '-' = top sample,    '+' = top-left sample
Packit 9c6abc
//  't' = extra top-right sample for 4x4 modes
Packit 9c6abc
#define YUV_SIZE (BPS * 17 + BPS * 9)
Packit 9c6abc
#define Y_OFF    (BPS * 1 + 8)
Packit 9c6abc
#define U_OFF    (Y_OFF + BPS * 16 + BPS)
Packit 9c6abc
#define V_OFF    (U_OFF + 16)
Packit 9c6abc
Packit 9c6abc
// minimal width under which lossy multi-threading is always disabled
Packit 9c6abc
#define MIN_WIDTH_FOR_THREADS 512
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
// Headers
Packit 9c6abc
Packit 9c6abc
typedef struct {
Packit 9c6abc
  uint8_t key_frame_;
Packit 9c6abc
  uint8_t profile_;
Packit 9c6abc
  uint8_t show_;
Packit 9c6abc
  uint32_t partition_length_;
Packit 9c6abc
} VP8FrameHeader;
Packit 9c6abc
Packit 9c6abc
typedef struct {
Packit 9c6abc
  uint16_t width_;
Packit 9c6abc
  uint16_t height_;
Packit 9c6abc
  uint8_t xscale_;
Packit 9c6abc
  uint8_t yscale_;
Packit 9c6abc
  uint8_t colorspace_;   // 0 = YCbCr
Packit 9c6abc
  uint8_t clamp_type_;
Packit 9c6abc
} VP8PictureHeader;
Packit 9c6abc
Packit 9c6abc
// segment features
Packit 9c6abc
typedef struct {
Packit 9c6abc
  int use_segment_;
Packit 9c6abc
  int update_map_;        // whether to update the segment map or not
Packit 9c6abc
  int absolute_delta_;    // absolute or delta values for quantizer and filter
Packit 9c6abc
  int8_t quantizer_[NUM_MB_SEGMENTS];        // quantization changes
Packit 9c6abc
  int8_t filter_strength_[NUM_MB_SEGMENTS];  // filter strength for segments
Packit 9c6abc
} VP8SegmentHeader;
Packit 9c6abc
Packit 9c6abc
// probas associated to one of the contexts
Packit 9c6abc
typedef uint8_t VP8ProbaArray[NUM_PROBAS];
Packit 9c6abc
Packit 9c6abc
typedef struct {   // all the probas associated to one band
Packit 9c6abc
  VP8ProbaArray probas_[NUM_CTX];
Packit 9c6abc
} VP8BandProbas;
Packit 9c6abc
Packit 9c6abc
// Struct collecting all frame-persistent probabilities.
Packit 9c6abc
typedef struct {
Packit 9c6abc
  uint8_t segments_[MB_FEATURE_TREE_PROBS];
Packit 9c6abc
  // Type: 0:Intra16-AC  1:Intra16-DC   2:Chroma   3:Intra4
Packit 9c6abc
  VP8BandProbas bands_[NUM_TYPES][NUM_BANDS];
Packit 9c6abc
  const VP8BandProbas* bands_ptr_[NUM_TYPES][16 + 1];
Packit 9c6abc
} VP8Proba;
Packit 9c6abc
Packit 9c6abc
// Filter parameters
Packit 9c6abc
typedef struct {
Packit 9c6abc
  int simple_;                  // 0=complex, 1=simple
Packit 9c6abc
  int level_;                   // [0..63]
Packit 9c6abc
  int sharpness_;               // [0..7]
Packit 9c6abc
  int use_lf_delta_;
Packit 9c6abc
  int ref_lf_delta_[NUM_REF_LF_DELTAS];
Packit 9c6abc
  int mode_lf_delta_[NUM_MODE_LF_DELTAS];
Packit 9c6abc
} VP8FilterHeader;
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
// Informations about the macroblocks.
Packit 9c6abc
Packit 9c6abc
typedef struct {  // filter specs
Packit 9c6abc
  uint8_t f_limit_;      // filter limit in [3..189], or 0 if no filtering
Packit 9c6abc
  uint8_t f_ilevel_;     // inner limit in [1..63]
Packit 9c6abc
  uint8_t f_inner_;      // do inner filtering?
Packit 9c6abc
  uint8_t hev_thresh_;   // high edge variance threshold in [0..2]
Packit 9c6abc
} VP8FInfo;
Packit 9c6abc
Packit 9c6abc
typedef struct {  // Top/Left Contexts used for syntax-parsing
Packit 9c6abc
  uint8_t nz_;        // non-zero AC/DC coeffs (4bit for luma + 4bit for chroma)
Packit 9c6abc
  uint8_t nz_dc_;     // non-zero DC coeff (1bit)
Packit 9c6abc
} VP8MB;
Packit 9c6abc
Packit 9c6abc
// Dequantization matrices
Packit 9c6abc
typedef int quant_t[2];      // [DC / AC].  Can be 'uint16_t[2]' too (~slower).
Packit 9c6abc
typedef struct {
Packit 9c6abc
  quant_t y1_mat_, y2_mat_, uv_mat_;
Packit 9c6abc
Packit 9c6abc
  int uv_quant_;   // U/V quantizer value
Packit 9c6abc
  int dither_;     // dithering amplitude (0 = off, max=255)
Packit 9c6abc
} VP8QuantMatrix;
Packit 9c6abc
Packit 9c6abc
// Data needed to reconstruct a macroblock
Packit 9c6abc
typedef struct {
Packit 9c6abc
  int16_t coeffs_[384];   // 384 coeffs = (16+4+4) * 4*4
Packit 9c6abc
  uint8_t is_i4x4_;       // true if intra4x4
Packit 9c6abc
  uint8_t imodes_[16];    // one 16x16 mode (#0) or sixteen 4x4 modes
Packit 9c6abc
  uint8_t uvmode_;        // chroma prediction mode
Packit 9c6abc
  // bit-wise info about the content of each sub-4x4 blocks (in decoding order).
Packit 9c6abc
  // Each of the 4x4 blocks for y/u/v is associated with a 2b code according to:
Packit 9c6abc
  //   code=0 -> no coefficient
Packit 9c6abc
  //   code=1 -> only DC
Packit 9c6abc
  //   code=2 -> first three coefficients are non-zero
Packit 9c6abc
  //   code=3 -> more than three coefficients are non-zero
Packit 9c6abc
  // This allows to call specialized transform functions.
Packit 9c6abc
  uint32_t non_zero_y_;
Packit 9c6abc
  uint32_t non_zero_uv_;
Packit 9c6abc
  uint8_t dither_;      // local dithering strength (deduced from non_zero_*)
Packit 9c6abc
  uint8_t skip_;
Packit 9c6abc
  uint8_t segment_;
Packit 9c6abc
} VP8MBData;
Packit 9c6abc
Packit 9c6abc
// Persistent information needed by the parallel processing
Packit 9c6abc
typedef struct {
Packit 9c6abc
  int id_;              // cache row to process (in [0..2])
Packit 9c6abc
  int mb_y_;            // macroblock position of the row
Packit 9c6abc
  int filter_row_;      // true if row-filtering is needed
Packit 9c6abc
  VP8FInfo* f_info_;    // filter strengths (swapped with dec->f_info_)
Packit 9c6abc
  VP8MBData* mb_data_;  // reconstruction data (swapped with dec->mb_data_)
Packit 9c6abc
  VP8Io io_;            // copy of the VP8Io to pass to put()
Packit 9c6abc
} VP8ThreadContext;
Packit 9c6abc
Packit 9c6abc
// Saved top samples, per macroblock. Fits into a cache-line.
Packit 9c6abc
typedef struct {
Packit 9c6abc
  uint8_t y[16], u[8], v[8];
Packit 9c6abc
} VP8TopSamples;
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
// VP8Decoder: the main opaque structure handed over to user
Packit 9c6abc
Packit 9c6abc
struct VP8Decoder {
Packit 9c6abc
  VP8StatusCode status_;
Packit 9c6abc
  int ready_;     // true if ready to decode a picture with VP8Decode()
Packit 9c6abc
  const char* error_msg_;  // set when status_ is not OK.
Packit 9c6abc
Packit 9c6abc
  // Main data source
Packit 9c6abc
  VP8BitReader br_;
Packit 9c6abc
Packit 9c6abc
  // headers
Packit 9c6abc
  VP8FrameHeader   frm_hdr_;
Packit 9c6abc
  VP8PictureHeader pic_hdr_;
Packit 9c6abc
  VP8FilterHeader  filter_hdr_;
Packit 9c6abc
  VP8SegmentHeader segment_hdr_;
Packit 9c6abc
Packit 9c6abc
  // Worker
Packit 9c6abc
  WebPWorker worker_;
Packit 9c6abc
  int mt_method_;      // multi-thread method: 0=off, 1=[parse+recon][filter]
Packit 9c6abc
                       // 2=[parse][recon+filter]
Packit 9c6abc
  int cache_id_;       // current cache row
Packit 9c6abc
  int num_caches_;     // number of cached rows of 16 pixels (1, 2 or 3)
Packit 9c6abc
  VP8ThreadContext thread_ctx_;  // Thread context
Packit 9c6abc
Packit 9c6abc
  // dimension, in macroblock units.
Packit 9c6abc
  int mb_w_, mb_h_;
Packit 9c6abc
Packit 9c6abc
  // Macroblock to process/filter, depending on cropping and filter_type.
Packit 9c6abc
  int tl_mb_x_, tl_mb_y_;  // top-left MB that must be in-loop filtered
Packit 9c6abc
  int br_mb_x_, br_mb_y_;  // last bottom-right MB that must be decoded
Packit 9c6abc
Packit 9c6abc
  // number of partitions minus one.
Packit 9c6abc
  uint32_t num_parts_minus_one_;
Packit 9c6abc
  // per-partition boolean decoders.
Packit 9c6abc
  VP8BitReader parts_[MAX_NUM_PARTITIONS];
Packit 9c6abc
Packit 9c6abc
  // Dithering strength, deduced from decoding options
Packit 9c6abc
  int dither_;                // whether to use dithering or not
Packit 9c6abc
  VP8Random dithering_rg_;    // random generator for dithering
Packit 9c6abc
Packit 9c6abc
  // dequantization (one set of DC/AC dequant factor per segment)
Packit 9c6abc
  VP8QuantMatrix dqm_[NUM_MB_SEGMENTS];
Packit 9c6abc
Packit 9c6abc
  // probabilities
Packit 9c6abc
  VP8Proba proba_;
Packit 9c6abc
  int use_skip_proba_;
Packit 9c6abc
  uint8_t skip_p_;
Packit 9c6abc
Packit 9c6abc
  // Boundary data cache and persistent buffers.
Packit 9c6abc
  uint8_t* intra_t_;      // top intra modes values: 4 * mb_w_
Packit 9c6abc
  uint8_t  intra_l_[4];   // left intra modes values
Packit 9c6abc
Packit 9c6abc
  VP8TopSamples* yuv_t_;  // top y/u/v samples
Packit 9c6abc
Packit 9c6abc
  VP8MB* mb_info_;        // contextual macroblock info (mb_w_ + 1)
Packit 9c6abc
  VP8FInfo* f_info_;      // filter strength info
Packit 9c6abc
  uint8_t* yuv_b_;        // main block for Y/U/V (size = YUV_SIZE)
Packit 9c6abc
Packit 9c6abc
  uint8_t* cache_y_;      // macroblock row for storing unfiltered samples
Packit 9c6abc
  uint8_t* cache_u_;
Packit 9c6abc
  uint8_t* cache_v_;
Packit 9c6abc
  int cache_y_stride_;
Packit 9c6abc
  int cache_uv_stride_;
Packit 9c6abc
Packit 9c6abc
  // main memory chunk for the above data. Persistent.
Packit 9c6abc
  void* mem_;
Packit 9c6abc
  size_t mem_size_;
Packit 9c6abc
Packit 9c6abc
  // Per macroblock non-persistent infos.
Packit 9c6abc
  int mb_x_, mb_y_;       // current position, in macroblock units
Packit 9c6abc
  VP8MBData* mb_data_;    // parsed reconstruction data
Packit 9c6abc
Packit 9c6abc
  // Filtering side-info
Packit 9c6abc
  int filter_type_;                          // 0=off, 1=simple, 2=complex
Packit 9c6abc
  VP8FInfo fstrengths_[NUM_MB_SEGMENTS][2];  // precalculated per-segment/type
Packit 9c6abc
Packit 9c6abc
  // Alpha
Packit 9c6abc
  struct ALPHDecoder* alph_dec_;  // alpha-plane decoder object
Packit 9c6abc
  const uint8_t* alpha_data_;     // compressed alpha data (if present)
Packit 9c6abc
  size_t alpha_data_size_;
Packit 9c6abc
  int is_alpha_decoded_;      // true if alpha_data_ is decoded in alpha_plane_
Packit 9c6abc
  uint8_t* alpha_plane_mem_;  // memory allocated for alpha_plane_
Packit 9c6abc
  uint8_t* alpha_plane_;      // output. Persistent, contains the whole data.
Packit 9c6abc
  const uint8_t* alpha_prev_line_;  // last decoded alpha row (or NULL)
Packit 9c6abc
  int alpha_dithering_;       // derived from decoding options (0=off, 100=full)
Packit 9c6abc
};
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
// internal functions. Not public.
Packit 9c6abc
Packit 9c6abc
// in vp8.c
Packit 9c6abc
int VP8SetError(VP8Decoder* const dec,
Packit 9c6abc
                VP8StatusCode error, const char* const msg);
Packit 9c6abc
Packit 9c6abc
// in tree.c
Packit 9c6abc
void VP8ResetProba(VP8Proba* const proba);
Packit 9c6abc
void VP8ParseProba(VP8BitReader* const br, VP8Decoder* const dec);
Packit 9c6abc
// parses one row of intra mode data in partition 0, returns !eof
Packit 9c6abc
int VP8ParseIntraModeRow(VP8BitReader* const br, VP8Decoder* const dec);
Packit 9c6abc
Packit 9c6abc
// in quant.c
Packit 9c6abc
void VP8ParseQuant(VP8Decoder* const dec);
Packit 9c6abc
Packit 9c6abc
// in frame.c
Packit 9c6abc
int VP8InitFrame(VP8Decoder* const dec, VP8Io* const io);
Packit 9c6abc
// Call io->setup() and finish setting up scan parameters.
Packit 9c6abc
// After this call returns, one must always call VP8ExitCritical() with the
Packit 9c6abc
// same parameters. Both functions should be used in pair. Returns VP8_STATUS_OK
Packit 9c6abc
// if ok, otherwise sets and returns the error status on *dec.
Packit 9c6abc
VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io);
Packit 9c6abc
// Must always be called in pair with VP8EnterCritical().
Packit 9c6abc
// Returns false in case of error.
Packit 9c6abc
int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io);
Packit 9c6abc
// Return the multi-threading method to use (0=off), depending
Packit 9c6abc
// on options and bitstream size. Only for lossy decoding.
Packit 9c6abc
int VP8GetThreadMethod(const WebPDecoderOptions* const options,
Packit 9c6abc
                       const WebPHeaderStructure* const headers,
Packit 9c6abc
                       int width, int height);
Packit 9c6abc
// Initialize dithering post-process if needed.
Packit 9c6abc
void VP8InitDithering(const WebPDecoderOptions* const options,
Packit 9c6abc
                      VP8Decoder* const dec);
Packit 9c6abc
// Process the last decoded row (filtering + output).
Packit 9c6abc
int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io);
Packit 9c6abc
// To be called at the start of a new scanline, to initialize predictors.
Packit 9c6abc
void VP8InitScanline(VP8Decoder* const dec);
Packit 9c6abc
// Decode one macroblock. Returns false if there is not enough data.
Packit 9c6abc
int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br);
Packit 9c6abc
Packit 9c6abc
// in alpha.c
Packit 9c6abc
const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
Packit 9c6abc
                                      const VP8Io* const io,
Packit 9c6abc
                                      int row, int num_rows);
Packit 9c6abc
Packit 9c6abc
//------------------------------------------------------------------------------
Packit 9c6abc
Packit 9c6abc
#ifdef __cplusplus
Packit 9c6abc
}    // extern "C"
Packit 9c6abc
#endif
Packit 9c6abc
Packit 9c6abc
#endif  /* WEBP_DEC_VP8I_DEC_H_ */