Path: blob/master/3rdparty/libwebp/src/dec/vp8i_dec.h
16358 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"2324#ifdef __cplusplus25extern "C" {26#endif2728//------------------------------------------------------------------------------29// Various defines and enums3031// version numbers32#define DEC_MAJ_VERSION 133#define DEC_MIN_VERSION 034#define DEC_REV_VERSION 03536// YUV-cache parameters. Cache is 32-bytes wide (= one cacheline).37// Constraints are: We need to store one 16x16 block of luma samples (y),38// and two 8x8 chroma blocks (u/v). These are better be 16-bytes aligned,39// in order to be SIMD-friendly. We also need to store the top, left and40// top-left samples (from previously decoded blocks), along with four41// extra top-right samples for luma (intra4x4 prediction only).42// One possible layout is, using 32 * (17 + 9) bytes:43//44// .+------ <- only 1 pixel high45// .|yyyyt.46// .|yyyyt.47// .|yyyyt.48// .|yyyy..49// .+--.+-- <- only 1 pixel high50// .|uu.|vv51// .|uu.|vv52//53// Every character is a 4x4 block, with legend:54// '.' = unused55// 'y' = y-samples 'u' = u-samples 'v' = u-samples56// '|' = left sample, '-' = top sample, '+' = top-left sample57// 't' = extra top-right sample for 4x4 modes58#define YUV_SIZE (BPS * 17 + BPS * 9)59#define Y_OFF (BPS * 1 + 8)60#define U_OFF (Y_OFF + BPS * 16 + BPS)61#define V_OFF (U_OFF + 16)6263// minimal width under which lossy multi-threading is always disabled64#define MIN_WIDTH_FOR_THREADS 5126566//------------------------------------------------------------------------------67// Headers6869typedef struct {70uint8_t key_frame_;71uint8_t profile_;72uint8_t show_;73uint32_t partition_length_;74} VP8FrameHeader;7576typedef struct {77uint16_t width_;78uint16_t height_;79uint8_t xscale_;80uint8_t yscale_;81uint8_t colorspace_; // 0 = YCbCr82uint8_t clamp_type_;83} VP8PictureHeader;8485// segment features86typedef struct {87int use_segment_;88int update_map_; // whether to update the segment map or not89int absolute_delta_; // absolute or delta values for quantizer and filter90int8_t quantizer_[NUM_MB_SEGMENTS]; // quantization changes91int8_t filter_strength_[NUM_MB_SEGMENTS]; // filter strength for segments92} VP8SegmentHeader;9394// probas associated to one of the contexts95typedef uint8_t VP8ProbaArray[NUM_PROBAS];9697typedef struct { // all the probas associated to one band98VP8ProbaArray probas_[NUM_CTX];99} VP8BandProbas;100101// Struct collecting all frame-persistent probabilities.102typedef struct {103uint8_t segments_[MB_FEATURE_TREE_PROBS];104// Type: 0:Intra16-AC 1:Intra16-DC 2:Chroma 3:Intra4105VP8BandProbas bands_[NUM_TYPES][NUM_BANDS];106const VP8BandProbas* bands_ptr_[NUM_TYPES][16 + 1];107} VP8Proba;108109// Filter parameters110typedef struct {111int simple_; // 0=complex, 1=simple112int level_; // [0..63]113int sharpness_; // [0..7]114int use_lf_delta_;115int ref_lf_delta_[NUM_REF_LF_DELTAS];116int mode_lf_delta_[NUM_MODE_LF_DELTAS];117} VP8FilterHeader;118119//------------------------------------------------------------------------------120// Informations about the macroblocks.121122typedef struct { // filter specs123uint8_t f_limit_; // filter limit in [3..189], or 0 if no filtering124uint8_t f_ilevel_; // inner limit in [1..63]125uint8_t f_inner_; // do inner filtering?126uint8_t hev_thresh_; // high edge variance threshold in [0..2]127} VP8FInfo;128129typedef struct { // Top/Left Contexts used for syntax-parsing130uint8_t nz_; // non-zero AC/DC coeffs (4bit for luma + 4bit for chroma)131uint8_t nz_dc_; // non-zero DC coeff (1bit)132} VP8MB;133134// Dequantization matrices135typedef int quant_t[2]; // [DC / AC]. Can be 'uint16_t[2]' too (~slower).136typedef struct {137quant_t y1_mat_, y2_mat_, uv_mat_;138139int uv_quant_; // U/V quantizer value140int dither_; // dithering amplitude (0 = off, max=255)141} VP8QuantMatrix;142143// Data needed to reconstruct a macroblock144typedef struct {145int16_t coeffs_[384]; // 384 coeffs = (16+4+4) * 4*4146uint8_t is_i4x4_; // true if intra4x4147uint8_t imodes_[16]; // one 16x16 mode (#0) or sixteen 4x4 modes148uint8_t uvmode_; // chroma prediction mode149// bit-wise info about the content of each sub-4x4 blocks (in decoding order).150// Each of the 4x4 blocks for y/u/v is associated with a 2b code according to:151// code=0 -> no coefficient152// code=1 -> only DC153// code=2 -> first three coefficients are non-zero154// code=3 -> more than three coefficients are non-zero155// This allows to call specialized transform functions.156uint32_t non_zero_y_;157uint32_t non_zero_uv_;158uint8_t dither_; // local dithering strength (deduced from non_zero_*)159uint8_t skip_;160uint8_t segment_;161} VP8MBData;162163// Persistent information needed by the parallel processing164typedef struct {165int id_; // cache row to process (in [0..2])166int mb_y_; // macroblock position of the row167int filter_row_; // true if row-filtering is needed168VP8FInfo* f_info_; // filter strengths (swapped with dec->f_info_)169VP8MBData* mb_data_; // reconstruction data (swapped with dec->mb_data_)170VP8Io io_; // copy of the VP8Io to pass to put()171} VP8ThreadContext;172173// Saved top samples, per macroblock. Fits into a cache-line.174typedef struct {175uint8_t y[16], u[8], v[8];176} VP8TopSamples;177178//------------------------------------------------------------------------------179// VP8Decoder: the main opaque structure handed over to user180181struct VP8Decoder {182VP8StatusCode status_;183int ready_; // true if ready to decode a picture with VP8Decode()184const char* error_msg_; // set when status_ is not OK.185186// Main data source187VP8BitReader br_;188189// headers190VP8FrameHeader frm_hdr_;191VP8PictureHeader pic_hdr_;192VP8FilterHeader filter_hdr_;193VP8SegmentHeader segment_hdr_;194195// Worker196WebPWorker worker_;197int mt_method_; // multi-thread method: 0=off, 1=[parse+recon][filter]198// 2=[parse][recon+filter]199int cache_id_; // current cache row200int num_caches_; // number of cached rows of 16 pixels (1, 2 or 3)201VP8ThreadContext thread_ctx_; // Thread context202203// dimension, in macroblock units.204int mb_w_, mb_h_;205206// Macroblock to process/filter, depending on cropping and filter_type.207int tl_mb_x_, tl_mb_y_; // top-left MB that must be in-loop filtered208int br_mb_x_, br_mb_y_; // last bottom-right MB that must be decoded209210// number of partitions minus one.211uint32_t num_parts_minus_one_;212// per-partition boolean decoders.213VP8BitReader parts_[MAX_NUM_PARTITIONS];214215// Dithering strength, deduced from decoding options216int dither_; // whether to use dithering or not217VP8Random dithering_rg_; // random generator for dithering218219// dequantization (one set of DC/AC dequant factor per segment)220VP8QuantMatrix dqm_[NUM_MB_SEGMENTS];221222// probabilities223VP8Proba proba_;224int use_skip_proba_;225uint8_t skip_p_;226227// Boundary data cache and persistent buffers.228uint8_t* intra_t_; // top intra modes values: 4 * mb_w_229uint8_t intra_l_[4]; // left intra modes values230231VP8TopSamples* yuv_t_; // top y/u/v samples232233VP8MB* mb_info_; // contextual macroblock info (mb_w_ + 1)234VP8FInfo* f_info_; // filter strength info235uint8_t* yuv_b_; // main block for Y/U/V (size = YUV_SIZE)236237uint8_t* cache_y_; // macroblock row for storing unfiltered samples238uint8_t* cache_u_;239uint8_t* cache_v_;240int cache_y_stride_;241int cache_uv_stride_;242243// main memory chunk for the above data. Persistent.244void* mem_;245size_t mem_size_;246247// Per macroblock non-persistent infos.248int mb_x_, mb_y_; // current position, in macroblock units249VP8MBData* mb_data_; // parsed reconstruction data250251// Filtering side-info252int filter_type_; // 0=off, 1=simple, 2=complex253VP8FInfo fstrengths_[NUM_MB_SEGMENTS][2]; // precalculated per-segment/type254255// Alpha256struct ALPHDecoder* alph_dec_; // alpha-plane decoder object257const uint8_t* alpha_data_; // compressed alpha data (if present)258size_t alpha_data_size_;259int is_alpha_decoded_; // true if alpha_data_ is decoded in alpha_plane_260uint8_t* alpha_plane_mem_; // memory allocated for alpha_plane_261uint8_t* alpha_plane_; // output. Persistent, contains the whole data.262const uint8_t* alpha_prev_line_; // last decoded alpha row (or NULL)263int alpha_dithering_; // derived from decoding options (0=off, 100=full)264};265266//------------------------------------------------------------------------------267// internal functions. Not public.268269// in vp8.c270int VP8SetError(VP8Decoder* const dec,271VP8StatusCode error, const char* const msg);272273// in tree.c274void VP8ResetProba(VP8Proba* const proba);275void VP8ParseProba(VP8BitReader* const br, VP8Decoder* const dec);276// parses one row of intra mode data in partition 0, returns !eof277int VP8ParseIntraModeRow(VP8BitReader* const br, VP8Decoder* const dec);278279// in quant.c280void VP8ParseQuant(VP8Decoder* const dec);281282// in frame.c283int VP8InitFrame(VP8Decoder* const dec, VP8Io* const io);284// Call io->setup() and finish setting up scan parameters.285// After this call returns, one must always call VP8ExitCritical() with the286// same parameters. Both functions should be used in pair. Returns VP8_STATUS_OK287// if ok, otherwise sets and returns the error status on *dec.288VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io);289// Must always be called in pair with VP8EnterCritical().290// Returns false in case of error.291int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io);292// Return the multi-threading method to use (0=off), depending293// on options and bitstream size. Only for lossy decoding.294int VP8GetThreadMethod(const WebPDecoderOptions* const options,295const WebPHeaderStructure* const headers,296int width, int height);297// Initialize dithering post-process if needed.298void VP8InitDithering(const WebPDecoderOptions* const options,299VP8Decoder* const dec);300// Process the last decoded row (filtering + output).301int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io);302// To be called at the start of a new scanline, to initialize predictors.303void VP8InitScanline(VP8Decoder* const dec);304// Decode one macroblock. Returns false if there is not enough data.305int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br);306307// in alpha.c308const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,309const VP8Io* const io,310int row, int num_rows);311312//------------------------------------------------------------------------------313314#ifdef __cplusplus315} // extern "C"316#endif317318#endif /* WEBP_DEC_VP8I_DEC_H_ */319320321