Path: blob/master/thirdparty/libwebp/src/dec/vp8i_dec.h
9912 views
// Copyright 2010 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// VP8 decoder: internal header.10//11// Author: Skal ([email protected])1213#ifndef WEBP_DEC_VP8I_DEC_H_14#define WEBP_DEC_VP8I_DEC_H_1516#include <string.h> // for memcpy()17#include "src/dec/common_dec.h"18#include "src/dec/vp8li_dec.h"19#include "src/utils/bit_reader_utils.h"20#include "src/utils/random_utils.h"21#include "src/utils/thread_utils.h"22#include "src/dsp/dsp.h"23#include "src/webp/types.h"2425#ifdef __cplusplus26extern "C" {27#endif2829//------------------------------------------------------------------------------30// Various defines and enums3132// version numbers33#define DEC_MAJ_VERSION 134#define DEC_MIN_VERSION 535#define DEC_REV_VERSION 03637// YUV-cache parameters. Cache is 32-bytes wide (= one cacheline).38// Constraints are: We need to store one 16x16 block of luma samples (y),39// and two 8x8 chroma blocks (u/v). These are better be 16-bytes aligned,40// in order to be SIMD-friendly. We also need to store the top, left and41// top-left samples (from previously decoded blocks), along with four42// extra top-right samples for luma (intra4x4 prediction only).43// One possible layout is, using 32 * (17 + 9) bytes:44//45// .+------ <- only 1 pixel high46// .|yyyyt.47// .|yyyyt.48// .|yyyyt.49// .|yyyy..50// .+--.+-- <- only 1 pixel high51// .|uu.|vv52// .|uu.|vv53//54// Every character is a 4x4 block, with legend:55// '.' = unused56// 'y' = y-samples 'u' = u-samples 'v' = u-samples57// '|' = left sample, '-' = top sample, '+' = top-left sample58// 't' = extra top-right sample for 4x4 modes59#define YUV_SIZE (BPS * 17 + BPS * 9)60#define Y_OFF (BPS * 1 + 8)61#define U_OFF (Y_OFF + BPS * 16 + BPS)62#define V_OFF (U_OFF + 16)6364// minimal width under which lossy multi-threading is always disabled65#define MIN_WIDTH_FOR_THREADS 5126667//------------------------------------------------------------------------------68// Headers6970typedef struct {71uint8_t key_frame_;72uint8_t profile_;73uint8_t show_;74uint32_t partition_length_;75} VP8FrameHeader;7677typedef struct {78uint16_t width_;79uint16_t height_;80uint8_t xscale_;81uint8_t yscale_;82uint8_t colorspace_; // 0 = YCbCr83uint8_t clamp_type_;84} VP8PictureHeader;8586// segment features87typedef struct {88int use_segment_;89int update_map_; // whether to update the segment map or not90int absolute_delta_; // absolute or delta values for quantizer and filter91int8_t quantizer_[NUM_MB_SEGMENTS]; // quantization changes92int8_t filter_strength_[NUM_MB_SEGMENTS]; // filter strength for segments93} VP8SegmentHeader;9495// probas associated to one of the contexts96typedef uint8_t VP8ProbaArray[NUM_PROBAS];9798typedef struct { // all the probas associated to one band99VP8ProbaArray probas_[NUM_CTX];100} VP8BandProbas;101102// Struct collecting all frame-persistent probabilities.103typedef struct {104uint8_t segments_[MB_FEATURE_TREE_PROBS];105// Type: 0:Intra16-AC 1:Intra16-DC 2:Chroma 3:Intra4106VP8BandProbas bands_[NUM_TYPES][NUM_BANDS];107const VP8BandProbas* bands_ptr_[NUM_TYPES][16 + 1];108} VP8Proba;109110// Filter parameters111typedef struct {112int simple_; // 0=complex, 1=simple113int level_; // [0..63]114int sharpness_; // [0..7]115int use_lf_delta_;116int ref_lf_delta_[NUM_REF_LF_DELTAS];117int mode_lf_delta_[NUM_MODE_LF_DELTAS];118} VP8FilterHeader;119120//------------------------------------------------------------------------------121// Informations about the macroblocks.122123typedef struct { // filter specs124uint8_t f_limit_; // filter limit in [3..189], or 0 if no filtering125uint8_t f_ilevel_; // inner limit in [1..63]126uint8_t f_inner_; // do inner filtering?127uint8_t hev_thresh_; // high edge variance threshold in [0..2]128} VP8FInfo;129130typedef struct { // Top/Left Contexts used for syntax-parsing131uint8_t nz_; // non-zero AC/DC coeffs (4bit for luma + 4bit for chroma)132uint8_t nz_dc_; // non-zero DC coeff (1bit)133} VP8MB;134135// Dequantization matrices136typedef int quant_t[2]; // [DC / AC]. Can be 'uint16_t[2]' too (~slower).137typedef struct {138quant_t y1_mat_, y2_mat_, uv_mat_;139140int uv_quant_; // U/V quantizer value141int dither_; // dithering amplitude (0 = off, max=255)142} VP8QuantMatrix;143144// Data needed to reconstruct a macroblock145typedef struct {146int16_t coeffs_[384]; // 384 coeffs = (16+4+4) * 4*4147uint8_t is_i4x4_; // true if intra4x4148uint8_t imodes_[16]; // one 16x16 mode (#0) or sixteen 4x4 modes149uint8_t uvmode_; // chroma prediction mode150// bit-wise info about the content of each sub-4x4 blocks (in decoding order).151// Each of the 4x4 blocks for y/u/v is associated with a 2b code according to:152// code=0 -> no coefficient153// code=1 -> only DC154// code=2 -> first three coefficients are non-zero155// code=3 -> more than three coefficients are non-zero156// This allows to call specialized transform functions.157uint32_t non_zero_y_;158uint32_t non_zero_uv_;159uint8_t dither_; // local dithering strength (deduced from non_zero_*)160uint8_t skip_;161uint8_t segment_;162} VP8MBData;163164// Persistent information needed by the parallel processing165typedef struct {166int id_; // cache row to process (in [0..2])167int mb_y_; // macroblock position of the row168int filter_row_; // true if row-filtering is needed169VP8FInfo* f_info_; // filter strengths (swapped with dec->f_info_)170VP8MBData* mb_data_; // reconstruction data (swapped with dec->mb_data_)171VP8Io io_; // copy of the VP8Io to pass to put()172} VP8ThreadContext;173174// Saved top samples, per macroblock. Fits into a cache-line.175typedef struct {176uint8_t y[16], u[8], v[8];177} VP8TopSamples;178179//------------------------------------------------------------------------------180// VP8Decoder: the main opaque structure handed over to user181182struct VP8Decoder {183VP8StatusCode status_;184int ready_; // true if ready to decode a picture with VP8Decode()185const char* error_msg_; // set when status_ is not OK.186187// Main data source188VP8BitReader br_;189int incremental_; // if true, incremental decoding is expected190191// headers192VP8FrameHeader frm_hdr_;193VP8PictureHeader pic_hdr_;194VP8FilterHeader filter_hdr_;195VP8SegmentHeader segment_hdr_;196197// Worker198WebPWorker worker_;199int mt_method_; // multi-thread method: 0=off, 1=[parse+recon][filter]200// 2=[parse][recon+filter]201int cache_id_; // current cache row202int num_caches_; // number of cached rows of 16 pixels (1, 2 or 3)203VP8ThreadContext thread_ctx_; // Thread context204205// dimension, in macroblock units.206int mb_w_, mb_h_;207208// Macroblock to process/filter, depending on cropping and filter_type.209int tl_mb_x_, tl_mb_y_; // top-left MB that must be in-loop filtered210int br_mb_x_, br_mb_y_; // last bottom-right MB that must be decoded211212// number of partitions minus one.213uint32_t num_parts_minus_one_;214// per-partition boolean decoders.215VP8BitReader parts_[MAX_NUM_PARTITIONS];216217// Dithering strength, deduced from decoding options218int dither_; // whether to use dithering or not219VP8Random dithering_rg_; // random generator for dithering220221// dequantization (one set of DC/AC dequant factor per segment)222VP8QuantMatrix dqm_[NUM_MB_SEGMENTS];223224// probabilities225VP8Proba proba_;226int use_skip_proba_;227uint8_t skip_p_;228229// Boundary data cache and persistent buffers.230uint8_t* intra_t_; // top intra modes values: 4 * mb_w_231uint8_t intra_l_[4]; // left intra modes values232233VP8TopSamples* yuv_t_; // top y/u/v samples234235VP8MB* mb_info_; // contextual macroblock info (mb_w_ + 1)236VP8FInfo* f_info_; // filter strength info237uint8_t* yuv_b_; // main block for Y/U/V (size = YUV_SIZE)238239uint8_t* cache_y_; // macroblock row for storing unfiltered samples240uint8_t* cache_u_;241uint8_t* cache_v_;242int cache_y_stride_;243int cache_uv_stride_;244245// main memory chunk for the above data. Persistent.246void* mem_;247size_t mem_size_;248249// Per macroblock non-persistent infos.250int mb_x_, mb_y_; // current position, in macroblock units251VP8MBData* mb_data_; // parsed reconstruction data252253// Filtering side-info254int filter_type_; // 0=off, 1=simple, 2=complex255VP8FInfo fstrengths_[NUM_MB_SEGMENTS][2]; // precalculated per-segment/type256257// Alpha258struct ALPHDecoder* alph_dec_; // alpha-plane decoder object259const uint8_t* alpha_data_; // compressed alpha data (if present)260size_t alpha_data_size_;261int is_alpha_decoded_; // true if alpha_data_ is decoded in alpha_plane_262uint8_t* alpha_plane_mem_; // memory allocated for alpha_plane_263uint8_t* alpha_plane_; // output. Persistent, contains the whole data.264const uint8_t* alpha_prev_line_; // last decoded alpha row (or NULL)265int alpha_dithering_; // derived from decoding options (0=off, 100=full)266};267268//------------------------------------------------------------------------------269// internal functions. Not public.270271// in vp8.c272int VP8SetError(VP8Decoder* const dec,273VP8StatusCode error, const char* const msg);274275// in tree.c276void VP8ResetProba(VP8Proba* const proba);277void VP8ParseProba(VP8BitReader* const br, VP8Decoder* const dec);278// parses one row of intra mode data in partition 0, returns !eof279int VP8ParseIntraModeRow(VP8BitReader* const br, VP8Decoder* const dec);280281// in quant.c282void VP8ParseQuant(VP8Decoder* const dec);283284// in frame.c285WEBP_NODISCARD int VP8InitFrame(VP8Decoder* const dec, VP8Io* const io);286// Call io->setup() and finish setting up scan parameters.287// After this call returns, one must always call VP8ExitCritical() with the288// same parameters. Both functions should be used in pair. Returns VP8_STATUS_OK289// if ok, otherwise sets and returns the error status on *dec.290VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io);291// Must always be called in pair with VP8EnterCritical().292// Returns false in case of error.293WEBP_NODISCARD int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io);294// Return the multi-threading method to use (0=off), depending295// on options and bitstream size. Only for lossy decoding.296int VP8GetThreadMethod(const WebPDecoderOptions* const options,297const WebPHeaderStructure* const headers,298int width, int height);299// Initialize dithering post-process if needed.300void VP8InitDithering(const WebPDecoderOptions* const options,301VP8Decoder* const dec);302// Process the last decoded row (filtering + output).303WEBP_NODISCARD int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io);304// To be called at the start of a new scanline, to initialize predictors.305void VP8InitScanline(VP8Decoder* const dec);306// Decode one macroblock. Returns false if there is not enough data.307WEBP_NODISCARD int VP8DecodeMB(VP8Decoder* const dec,308VP8BitReader* const token_br);309310// in alpha.c311const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,312const VP8Io* const io,313int row, int num_rows);314315//------------------------------------------------------------------------------316317#ifdef __cplusplus318} // extern "C"319#endif320321#endif // WEBP_DEC_VP8I_DEC_H_322323324