vp8i_dec.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // VP8 decoder: internal header.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #ifndef WEBP_DEC_VP8I_DEC_H_
  14. #define WEBP_DEC_VP8I_DEC_H_
  15. #include <string.h> // for memcpy()
  16. #include "./common_dec.h"
  17. #include "./vp8li_dec.h"
  18. #include "../utils/bit_reader_utils.h"
  19. #include "../utils/random_utils.h"
  20. #include "../utils/thread_utils.h"
  21. #include "../dsp/dsp.h"
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. //------------------------------------------------------------------------------
  26. // Various defines and enums
  27. // version numbers
  28. #define DEC_MAJ_VERSION 1
  29. #define DEC_MIN_VERSION 2
  30. #define DEC_REV_VERSION 2
  31. // YUV-cache parameters. Cache is 32-bytes wide (= one cacheline).
  32. // Constraints are: We need to store one 16x16 block of luma samples (y),
  33. // and two 8x8 chroma blocks (u/v). These are better be 16-bytes aligned,
  34. // in order to be SIMD-friendly. We also need to store the top, left and
  35. // top-left samples (from previously decoded blocks), along with four
  36. // extra top-right samples for luma (intra4x4 prediction only).
  37. // One possible layout is, using 32 * (17 + 9) bytes:
  38. //
  39. // .+------ <- only 1 pixel high
  40. // .|yyyyt.
  41. // .|yyyyt.
  42. // .|yyyyt.
  43. // .|yyyy..
  44. // .+--.+-- <- only 1 pixel high
  45. // .|uu.|vv
  46. // .|uu.|vv
  47. //
  48. // Every character is a 4x4 block, with legend:
  49. // '.' = unused
  50. // 'y' = y-samples 'u' = u-samples 'v' = u-samples
  51. // '|' = left sample, '-' = top sample, '+' = top-left sample
  52. // 't' = extra top-right sample for 4x4 modes
  53. #define YUV_SIZE (BPS * 17 + BPS * 9)
  54. #define Y_OFF (BPS * 1 + 8)
  55. #define U_OFF (Y_OFF + BPS * 16 + BPS)
  56. #define V_OFF (U_OFF + 16)
  57. // minimal width under which lossy multi-threading is always disabled
  58. #define MIN_WIDTH_FOR_THREADS 512
  59. //------------------------------------------------------------------------------
  60. // Headers
  61. typedef struct {
  62. uint8_t key_frame_;
  63. uint8_t profile_;
  64. uint8_t show_;
  65. uint32_t partition_length_;
  66. } VP8FrameHeader;
  67. typedef struct {
  68. uint16_t width_;
  69. uint16_t height_;
  70. uint8_t xscale_;
  71. uint8_t yscale_;
  72. uint8_t colorspace_; // 0 = YCbCr
  73. uint8_t clamp_type_;
  74. } VP8PictureHeader;
  75. // segment features
  76. typedef struct {
  77. int use_segment_;
  78. int update_map_; // whether to update the segment map or not
  79. int absolute_delta_; // absolute or delta values for quantizer and filter
  80. int8_t quantizer_[NUM_MB_SEGMENTS]; // quantization changes
  81. int8_t filter_strength_[NUM_MB_SEGMENTS]; // filter strength for segments
  82. } VP8SegmentHeader;
  83. // probas associated to one of the contexts
  84. typedef uint8_t VP8ProbaArray[NUM_PROBAS];
  85. typedef struct { // all the probas associated to one band
  86. VP8ProbaArray probas_[NUM_CTX];
  87. } VP8BandProbas;
  88. // Struct collecting all frame-persistent probabilities.
  89. typedef struct {
  90. uint8_t segments_[MB_FEATURE_TREE_PROBS];
  91. // Type: 0:Intra16-AC 1:Intra16-DC 2:Chroma 3:Intra4
  92. VP8BandProbas bands_[NUM_TYPES][NUM_BANDS];
  93. const VP8BandProbas* bands_ptr_[NUM_TYPES][16 + 1];
  94. } VP8Proba;
  95. // Filter parameters
  96. typedef struct {
  97. int simple_; // 0=complex, 1=simple
  98. int level_; // [0..63]
  99. int sharpness_; // [0..7]
  100. int use_lf_delta_;
  101. int ref_lf_delta_[NUM_REF_LF_DELTAS];
  102. int mode_lf_delta_[NUM_MODE_LF_DELTAS];
  103. } VP8FilterHeader;
  104. //------------------------------------------------------------------------------
  105. // Informations about the macroblocks.
  106. typedef struct { // filter specs
  107. uint8_t f_limit_; // filter limit in [3..189], or 0 if no filtering
  108. uint8_t f_ilevel_; // inner limit in [1..63]
  109. uint8_t f_inner_; // do inner filtering?
  110. uint8_t hev_thresh_; // high edge variance threshold in [0..2]
  111. } VP8FInfo;
  112. typedef struct { // Top/Left Contexts used for syntax-parsing
  113. uint8_t nz_; // non-zero AC/DC coeffs (4bit for luma + 4bit for chroma)
  114. uint8_t nz_dc_; // non-zero DC coeff (1bit)
  115. } VP8MB;
  116. // Dequantization matrices
  117. typedef int quant_t[2]; // [DC / AC]. Can be 'uint16_t[2]' too (~slower).
  118. typedef struct {
  119. quant_t y1_mat_, y2_mat_, uv_mat_;
  120. int uv_quant_; // U/V quantizer value
  121. int dither_; // dithering amplitude (0 = off, max=255)
  122. } VP8QuantMatrix;
  123. // Data needed to reconstruct a macroblock
  124. typedef struct {
  125. int16_t coeffs_[384]; // 384 coeffs = (16+4+4) * 4*4
  126. uint8_t is_i4x4_; // true if intra4x4
  127. uint8_t imodes_[16]; // one 16x16 mode (#0) or sixteen 4x4 modes
  128. uint8_t uvmode_; // chroma prediction mode
  129. // bit-wise info about the content of each sub-4x4 blocks (in decoding order).
  130. // Each of the 4x4 blocks for y/u/v is associated with a 2b code according to:
  131. // code=0 -> no coefficient
  132. // code=1 -> only DC
  133. // code=2 -> first three coefficients are non-zero
  134. // code=3 -> more than three coefficients are non-zero
  135. // This allows to call specialized transform functions.
  136. uint32_t non_zero_y_;
  137. uint32_t non_zero_uv_;
  138. uint8_t dither_; // local dithering strength (deduced from non_zero_*)
  139. uint8_t skip_;
  140. uint8_t segment_;
  141. } VP8MBData;
  142. // Persistent information needed by the parallel processing
  143. typedef struct {
  144. int id_; // cache row to process (in [0..2])
  145. int mb_y_; // macroblock position of the row
  146. int filter_row_; // true if row-filtering is needed
  147. VP8FInfo* f_info_; // filter strengths (swapped with dec->f_info_)
  148. VP8MBData* mb_data_; // reconstruction data (swapped with dec->mb_data_)
  149. VP8Io io_; // copy of the VP8Io to pass to put()
  150. } VP8ThreadContext;
  151. // Saved top samples, per macroblock. Fits into a cache-line.
  152. typedef struct {
  153. uint8_t y[16], u[8], v[8];
  154. } VP8TopSamples;
  155. //------------------------------------------------------------------------------
  156. // VP8Decoder: the main opaque structure handed over to user
  157. struct VP8Decoder {
  158. VP8StatusCode status_;
  159. int ready_; // true if ready to decode a picture with VP8Decode()
  160. const char* error_msg_; // set when status_ is not OK.
  161. // Main data source
  162. VP8BitReader br_;
  163. // headers
  164. VP8FrameHeader frm_hdr_;
  165. VP8PictureHeader pic_hdr_;
  166. VP8FilterHeader filter_hdr_;
  167. VP8SegmentHeader segment_hdr_;
  168. // Worker
  169. WebPWorker worker_;
  170. int mt_method_; // multi-thread method: 0=off, 1=[parse+recon][filter]
  171. // 2=[parse][recon+filter]
  172. int cache_id_; // current cache row
  173. int num_caches_; // number of cached rows of 16 pixels (1, 2 or 3)
  174. VP8ThreadContext thread_ctx_; // Thread context
  175. // dimension, in macroblock units.
  176. int mb_w_, mb_h_;
  177. // Macroblock to process/filter, depending on cropping and filter_type.
  178. int tl_mb_x_, tl_mb_y_; // top-left MB that must be in-loop filtered
  179. int br_mb_x_, br_mb_y_; // last bottom-right MB that must be decoded
  180. // number of partitions minus one.
  181. uint32_t num_parts_minus_one_;
  182. // per-partition boolean decoders.
  183. VP8BitReader parts_[MAX_NUM_PARTITIONS];
  184. // Dithering strength, deduced from decoding options
  185. int dither_; // whether to use dithering or not
  186. VP8Random dithering_rg_; // random generator for dithering
  187. // dequantization (one set of DC/AC dequant factor per segment)
  188. VP8QuantMatrix dqm_[NUM_MB_SEGMENTS];
  189. // probabilities
  190. VP8Proba proba_;
  191. int use_skip_proba_;
  192. uint8_t skip_p_;
  193. // Boundary data cache and persistent buffers.
  194. uint8_t* intra_t_; // top intra modes values: 4 * mb_w_
  195. uint8_t intra_l_[4]; // left intra modes values
  196. VP8TopSamples* yuv_t_; // top y/u/v samples
  197. VP8MB* mb_info_; // contextual macroblock info (mb_w_ + 1)
  198. VP8FInfo* f_info_; // filter strength info
  199. uint8_t* yuv_b_; // main block for Y/U/V (size = YUV_SIZE)
  200. uint8_t* cache_y_; // macroblock row for storing unfiltered samples
  201. uint8_t* cache_u_;
  202. uint8_t* cache_v_;
  203. int cache_y_stride_;
  204. int cache_uv_stride_;
  205. // main memory chunk for the above data. Persistent.
  206. void* mem_;
  207. size_t mem_size_;
  208. // Per macroblock non-persistent infos.
  209. int mb_x_, mb_y_; // current position, in macroblock units
  210. VP8MBData* mb_data_; // parsed reconstruction data
  211. // Filtering side-info
  212. int filter_type_; // 0=off, 1=simple, 2=complex
  213. VP8FInfo fstrengths_[NUM_MB_SEGMENTS][2]; // precalculated per-segment/type
  214. // Alpha
  215. struct ALPHDecoder* alph_dec_; // alpha-plane decoder object
  216. const uint8_t* alpha_data_; // compressed alpha data (if present)
  217. size_t alpha_data_size_;
  218. int is_alpha_decoded_; // true if alpha_data_ is decoded in alpha_plane_
  219. uint8_t* alpha_plane_mem_; // memory allocated for alpha_plane_
  220. uint8_t* alpha_plane_; // output. Persistent, contains the whole data.
  221. const uint8_t* alpha_prev_line_; // last decoded alpha row (or NULL)
  222. int alpha_dithering_; // derived from decoding options (0=off, 100=full)
  223. };
  224. //------------------------------------------------------------------------------
  225. // internal functions. Not public.
  226. // in vp8.c
  227. int VP8SetError(VP8Decoder* const dec,
  228. VP8StatusCode error, const char* const msg);
  229. // in tree.c
  230. void VP8ResetProba(VP8Proba* const proba);
  231. void VP8ParseProba(VP8BitReader* const br, VP8Decoder* const dec);
  232. // parses one row of intra mode data in partition 0, returns !eof
  233. int VP8ParseIntraModeRow(VP8BitReader* const br, VP8Decoder* const dec);
  234. // in quant.c
  235. void VP8ParseQuant(VP8Decoder* const dec);
  236. // in frame.c
  237. int VP8InitFrame(VP8Decoder* const dec, VP8Io* const io);
  238. // Call io->setup() and finish setting up scan parameters.
  239. // After this call returns, one must always call VP8ExitCritical() with the
  240. // same parameters. Both functions should be used in pair. Returns VP8_STATUS_OK
  241. // if ok, otherwise sets and returns the error status on *dec.
  242. VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io);
  243. // Must always be called in pair with VP8EnterCritical().
  244. // Returns false in case of error.
  245. int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io);
  246. // Return the multi-threading method to use (0=off), depending
  247. // on options and bitstream size. Only for lossy decoding.
  248. int VP8GetThreadMethod(const WebPDecoderOptions* const options,
  249. const WebPHeaderStructure* const headers,
  250. int width, int height);
  251. // Initialize dithering post-process if needed.
  252. void VP8InitDithering(const WebPDecoderOptions* const options,
  253. VP8Decoder* const dec);
  254. // Process the last decoded row (filtering + output).
  255. int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io);
  256. // To be called at the start of a new scanline, to initialize predictors.
  257. void VP8InitScanline(VP8Decoder* const dec);
  258. // Decode one macroblock. Returns false if there is not enough data.
  259. int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br);
  260. // in alpha.c
  261. const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec,
  262. const VP8Io* const io,
  263. int row, int num_rows);
  264. //------------------------------------------------------------------------------
  265. #ifdef __cplusplus
  266. } // extern "C"
  267. #endif
  268. #endif // WEBP_DEC_VP8I_DEC_H_