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