Path: blob/master/thirdparty/libwebp/src/dec/vp8i_dec.h
21567 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()1718#include "src/dec/common_dec.h"19#include "src/dec/vp8_dec.h"20#include "src/dec/vp8li_dec.h"21#include "src/dec/webpi_dec.h"22#include "src/dsp/dsp.h"23#include "src/utils/bit_reader_utils.h"24#include "src/utils/random_utils.h"25#include "src/utils/thread_utils.h"26#include "src/webp/decode.h"27#include "src/webp/types.h"2829#ifdef __cplusplus30extern "C" {31#endif3233//------------------------------------------------------------------------------34// Various defines and enums3536// version numbers37#define DEC_MAJ_VERSION 138#define DEC_MIN_VERSION 639#define DEC_REV_VERSION 04041// YUV-cache parameters. Cache is 32-bytes wide (= one cacheline).42// Constraints are: We need to store one 16x16 block of luma samples (y),43// and two 8x8 chroma blocks (u/v). These are better be 16-bytes aligned,44// in order to be SIMD-friendly. We also need to store the top, left and45// top-left samples (from previously decoded blocks), along with four46// extra top-right samples for luma (intra4x4 prediction only).47// One possible layout is, using 32 * (17 + 9) bytes:48//49// .+------ <- only 1 pixel high50// .|yyyyt.51// .|yyyyt.52// .|yyyyt.53// .|yyyy..54// .+--.+-- <- only 1 pixel high55// .|uu.|vv56// .|uu.|vv57//58// Every character is a 4x4 block, with legend:59// '.' = unused60// 'y' = y-samples 'u' = u-samples 'v' = u-samples61// '|' = left sample, '-' = top sample, '+' = top-left sample62// 't' = extra top-right sample for 4x4 modes63#define YUV_SIZE (BPS * 17 + BPS * 9)64#define Y_OFF (BPS * 1 + 8)65#define U_OFF (Y_OFF + BPS * 16 + BPS)66#define V_OFF (U_OFF + 16)6768// minimal width under which lossy multi-threading is always disabled69#define MIN_WIDTH_FOR_THREADS 5127071//------------------------------------------------------------------------------72// Headers7374typedef struct {75uint8_t key_frame;76uint8_t profile;77uint8_t show;78uint32_t partition_length;79} VP8FrameHeader;8081typedef struct {82uint16_t width;83uint16_t height;84uint8_t xscale;85uint8_t yscale;86uint8_t colorspace; // 0 = YCbCr87uint8_t clamp_type;88} VP8PictureHeader;8990// segment features91typedef struct {92int use_segment;93int update_map; // whether to update the segment map or not94int absolute_delta; // absolute or delta values for quantizer and filter95int8_t quantizer[NUM_MB_SEGMENTS]; // quantization changes96int8_t filter_strength[NUM_MB_SEGMENTS]; // filter strength for segments97} VP8SegmentHeader;9899// probas associated to one of the contexts100typedef uint8_t VP8ProbaArray[NUM_PROBAS];101102typedef struct { // all the probas associated to one band103VP8ProbaArray probas[NUM_CTX];104} VP8BandProbas;105106// Struct collecting all frame-persistent probabilities.107typedef struct {108uint8_t segments[MB_FEATURE_TREE_PROBS];109// Type: 0:Intra16-AC 1:Intra16-DC 2:Chroma 3:Intra4110VP8BandProbas bands[NUM_TYPES][NUM_BANDS];111const VP8BandProbas* bands_ptr[NUM_TYPES][16 + 1];112} VP8Proba;113114// Filter parameters115typedef struct {116int simple; // 0=complex, 1=simple117int level; // [0..63]118int sharpness; // [0..7]119int use_lf_delta;120int ref_lf_delta[NUM_REF_LF_DELTAS];121int mode_lf_delta[NUM_MODE_LF_DELTAS];122} VP8FilterHeader;123124//------------------------------------------------------------------------------125// Informations about the macroblocks.126127typedef struct { // filter specs128uint8_t f_limit; // filter limit in [3..189], or 0 if no filtering129uint8_t f_ilevel; // inner limit in [1..63]130uint8_t f_inner; // do inner filtering?131uint8_t hev_thresh; // high edge variance threshold in [0..2]132} VP8FInfo;133134typedef struct { // Top/Left Contexts used for syntax-parsing135uint8_t nz; // non-zero AC/DC coeffs (4bit for luma + 4bit for chroma)136uint8_t nz_dc; // non-zero DC coeff (1bit)137} VP8MB;138139// Dequantization matrices140typedef int quant_t[2]; // [DC / AC]. Can be 'uint16_t[2]' too (~slower).141typedef struct {142quant_t y1_mat, y2_mat, uv_mat;143144int uv_quant; // U/V quantizer value145int dither; // dithering amplitude (0 = off, max=255)146} VP8QuantMatrix;147148// Data needed to reconstruct a macroblock149typedef struct {150int16_t coeffs[384]; // 384 coeffs = (16+4+4) * 4*4151uint8_t is_i4x4; // true if intra4x4152uint8_t imodes[16]; // one 16x16 mode (#0) or sixteen 4x4 modes153uint8_t uvmode; // chroma prediction mode154// bit-wise info about the content of each sub-4x4 blocks (in decoding order).155// Each of the 4x4 blocks for y/u/v is associated with a 2b code according to:156// code=0 -> no coefficient157// code=1 -> only DC158// code=2 -> first three coefficients are non-zero159// code=3 -> more than three coefficients are non-zero160// This allows to call specialized transform functions.161uint32_t non_zero_y;162uint32_t non_zero_uv;163uint8_t dither; // local dithering strength (deduced from non_zero*)164uint8_t skip;165uint8_t segment;166} VP8MBData;167168// Persistent information needed by the parallel processing169typedef struct {170int id; // cache row to process (in [0..2])171int mb_y; // macroblock position of the row172int filter_row; // true if row-filtering is needed173VP8FInfo* f_info; // filter strengths (swapped with dec->f_info)174VP8MBData* mb_data; // reconstruction data (swapped with dec->mb_data)175VP8Io io; // copy of the VP8Io to pass to put()176} VP8ThreadContext;177178// Saved top samples, per macroblock. Fits into a cache-line.179typedef struct {180uint8_t y[16], u[8], v[8];181} VP8TopSamples;182183//------------------------------------------------------------------------------184// VP8Decoder: the main opaque structure handed over to user185186struct VP8Decoder {187VP8StatusCode status;188int ready; // true if ready to decode a picture with VP8Decode()189const char* error_msg; // set when status is not OK.190191// Main data source192VP8BitReader br;193int incremental; // if true, incremental decoding is expected194195// headers196VP8FrameHeader frm_hdr;197VP8PictureHeader pic_hdr;198VP8FilterHeader filter_hdr;199VP8SegmentHeader segment_hdr;200201// Worker202WebPWorker worker;203int mt_method; // multi-thread method: 0=off, 1=[parse+recon][filter]204// 2=[parse][recon+filter]205int cache_id; // current cache row206int num_caches; // number of cached rows of 16 pixels (1, 2 or 3)207VP8ThreadContext thread_ctx; // Thread context208209// dimension, in macroblock units.210int mb_w, mb_h;211212// Macroblock to process/filter, depending on cropping and filter_type.213int tl_mb_x, tl_mb_y; // top-left MB that must be in-loop filtered214int br_mb_x, br_mb_y; // last bottom-right MB that must be decoded215216// number of partitions minus one.217uint32_t num_parts_minus_one;218// per-partition boolean decoders.219VP8BitReader parts[MAX_NUM_PARTITIONS];220221// Dithering strength, deduced from decoding options222int dither; // whether to use dithering or not223VP8Random dithering_rg; // random generator for dithering224225// dequantization (one set of DC/AC dequant factor per segment)226VP8QuantMatrix dqm[NUM_MB_SEGMENTS];227228// probabilities229VP8Proba proba;230int use_skip_proba;231uint8_t skip_p;232233// Boundary data cache and persistent buffers.234uint8_t* intra_t; // top intra modes values: 4 * mb_w235uint8_t intra_l[4]; // left intra modes values236237VP8TopSamples* yuv_t; // top y/u/v samples238239VP8MB* mb_info; // contextual macroblock info (mb_w + 1)240VP8FInfo* f_info; // filter strength info241uint8_t* yuv_b; // main block for Y/U/V (size = YUV_SIZE)242243uint8_t* cache_y; // macroblock row for storing unfiltered samples244uint8_t* cache_u;245uint8_t* cache_v;246int cache_y_stride;247int cache_uv_stride;248249// main memory chunk for the above data. Persistent.250void* mem;251size_t mem_size;252253// Per macroblock non-persistent infos.254int mb_x, mb_y; // current position, in macroblock units255VP8MBData* mb_data; // parsed reconstruction data256257// Filtering side-info258int filter_type; // 0=off, 1=simple, 2=complex259VP8FInfo fstrengths[NUM_MB_SEGMENTS][2]; // precalculated per-segment/type260261// Alpha262struct ALPHDecoder* alph_dec; // alpha-plane decoder object263const uint8_t* alpha_data; // compressed alpha data (if present)264size_t alpha_data_size;265int is_alpha_decoded; // true if alpha_data is decoded in alpha_plane266uint8_t* alpha_plane_mem; // memory allocated for alpha_plane267uint8_t* alpha_plane; // output. Persistent, contains the whole data.268const uint8_t* alpha_prev_line; // last decoded alpha row (or NULL)269int alpha_dithering; // derived from decoding options (0=off, 100=full)270};271272//------------------------------------------------------------------------------273// internal functions. Not public.274275// in vp8.c276int VP8SetError(VP8Decoder* const dec,277VP8StatusCode error, const char* const msg);278279// in tree.c280void VP8ResetProba(VP8Proba* const proba);281void VP8ParseProba(VP8BitReader* const br, VP8Decoder* const dec);282// parses one row of intra mode data in partition 0, returns !eof283int VP8ParseIntraModeRow(VP8BitReader* const br, VP8Decoder* const dec);284285// in quant.c286void VP8ParseQuant(VP8Decoder* const dec);287288// in frame.c289WEBP_NODISCARD int VP8InitFrame(VP8Decoder* const dec, VP8Io* const io);290// Call io->setup() and finish setting up scan parameters.291// After this call returns, one must always call VP8ExitCritical() with the292// same parameters. Both functions should be used in pair. Returns VP8_STATUS_OK293// if ok, otherwise sets and returns the error status on *dec.294VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io);295// Must always be called in pair with VP8EnterCritical().296// Returns false in case of error.297WEBP_NODISCARD int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io);298// Return the multi-threading method to use (0=off), depending299// on options and bitstream size. Only for lossy decoding.300int VP8GetThreadMethod(const WebPDecoderOptions* const options,301const WebPHeaderStructure* const headers,302int width, int height);303// Initialize dithering post-process if needed.304void VP8InitDithering(const WebPDecoderOptions* const options,305VP8Decoder* const dec);306// Process the last decoded row (filtering + output).307WEBP_NODISCARD int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io);308// To be called at the start of a new scanline, to initialize predictors.309void VP8InitScanline(VP8Decoder* const dec);310// Decode one macroblock. Returns false if there is not enough data.311WEBP_NODISCARD int VP8DecodeMB(VP8Decoder* const dec,312VP8BitReader* const token_br);313314// in alpha.c315const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,316const VP8Io* const io,317int row, int num_rows);318319//------------------------------------------------------------------------------320321#ifdef __cplusplus322} // extern "C"323#endif324325#endif // WEBP_DEC_VP8I_DEC_H_326327328