xref: /aosp_15_r20/external/webp/src/dec/vp8li_dec.h (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Copyright 2012 Google Inc. All Rights Reserved.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license
4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source
5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found
6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may
7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree.
8*b2055c35SXin Li // -----------------------------------------------------------------------------
9*b2055c35SXin Li //
10*b2055c35SXin Li // Lossless decoder: internal header.
11*b2055c35SXin Li //
12*b2055c35SXin Li // Author: Skal ([email protected])
13*b2055c35SXin Li //         Vikas Arora([email protected])
14*b2055c35SXin Li 
15*b2055c35SXin Li #ifndef WEBP_DEC_VP8LI_DEC_H_
16*b2055c35SXin Li #define WEBP_DEC_VP8LI_DEC_H_
17*b2055c35SXin Li 
18*b2055c35SXin Li #include <string.h>     // for memcpy()
19*b2055c35SXin Li #include "src/dec/webpi_dec.h"
20*b2055c35SXin Li #include "src/utils/bit_reader_utils.h"
21*b2055c35SXin Li #include "src/utils/color_cache_utils.h"
22*b2055c35SXin Li #include "src/utils/huffman_utils.h"
23*b2055c35SXin Li #include "src/webp/types.h"
24*b2055c35SXin Li 
25*b2055c35SXin Li #ifdef __cplusplus
26*b2055c35SXin Li extern "C" {
27*b2055c35SXin Li #endif
28*b2055c35SXin Li 
29*b2055c35SXin Li typedef enum {
30*b2055c35SXin Li   READ_DATA = 0,
31*b2055c35SXin Li   READ_HDR = 1,
32*b2055c35SXin Li   READ_DIM = 2
33*b2055c35SXin Li } VP8LDecodeState;
34*b2055c35SXin Li 
35*b2055c35SXin Li typedef struct VP8LTransform VP8LTransform;
36*b2055c35SXin Li struct VP8LTransform {
37*b2055c35SXin Li   VP8LImageTransformType type_;   // transform type.
38*b2055c35SXin Li   int                    bits_;   // subsampling bits defining transform window.
39*b2055c35SXin Li   int                    xsize_;  // transform window X index.
40*b2055c35SXin Li   int                    ysize_;  // transform window Y index.
41*b2055c35SXin Li   uint32_t*              data_;   // transform data.
42*b2055c35SXin Li };
43*b2055c35SXin Li 
44*b2055c35SXin Li typedef struct {
45*b2055c35SXin Li   int             color_cache_size_;
46*b2055c35SXin Li   VP8LColorCache  color_cache_;
47*b2055c35SXin Li   VP8LColorCache  saved_color_cache_;  // for incremental
48*b2055c35SXin Li 
49*b2055c35SXin Li   int             huffman_mask_;
50*b2055c35SXin Li   int             huffman_subsample_bits_;
51*b2055c35SXin Li   int             huffman_xsize_;
52*b2055c35SXin Li   uint32_t*       huffman_image_;
53*b2055c35SXin Li   int             num_htree_groups_;
54*b2055c35SXin Li   HTreeGroup*     htree_groups_;
55*b2055c35SXin Li   HuffmanTables   huffman_tables_;
56*b2055c35SXin Li } VP8LMetadata;
57*b2055c35SXin Li 
58*b2055c35SXin Li typedef struct VP8LDecoder VP8LDecoder;
59*b2055c35SXin Li struct VP8LDecoder {
60*b2055c35SXin Li   VP8StatusCode    status_;
61*b2055c35SXin Li   VP8LDecodeState  state_;
62*b2055c35SXin Li   VP8Io*           io_;
63*b2055c35SXin Li 
64*b2055c35SXin Li   const WebPDecBuffer* output_;    // shortcut to io->opaque->output
65*b2055c35SXin Li 
66*b2055c35SXin Li   uint32_t*        pixels_;        // Internal data: either uint8_t* for alpha
67*b2055c35SXin Li                                    // or uint32_t* for BGRA.
68*b2055c35SXin Li   uint32_t*        argb_cache_;    // Scratch buffer for temporary BGRA storage.
69*b2055c35SXin Li 
70*b2055c35SXin Li   VP8LBitReader    br_;
71*b2055c35SXin Li   int              incremental_;   // if true, incremental decoding is expected
72*b2055c35SXin Li   VP8LBitReader    saved_br_;      // note: could be local variables too
73*b2055c35SXin Li   int              saved_last_pixel_;
74*b2055c35SXin Li 
75*b2055c35SXin Li   int              width_;
76*b2055c35SXin Li   int              height_;
77*b2055c35SXin Li   int              last_row_;      // last input row decoded so far.
78*b2055c35SXin Li   int              last_pixel_;    // last pixel decoded so far. However, it may
79*b2055c35SXin Li                                    // not be transformed, scaled and
80*b2055c35SXin Li                                    // color-converted yet.
81*b2055c35SXin Li   int              last_out_row_;  // last row output so far.
82*b2055c35SXin Li 
83*b2055c35SXin Li   VP8LMetadata     hdr_;
84*b2055c35SXin Li 
85*b2055c35SXin Li   int              next_transform_;
86*b2055c35SXin Li   VP8LTransform    transforms_[NUM_TRANSFORMS];
87*b2055c35SXin Li   // or'd bitset storing the transforms types.
88*b2055c35SXin Li   uint32_t         transforms_seen_;
89*b2055c35SXin Li 
90*b2055c35SXin Li   uint8_t*         rescaler_memory;  // Working memory for rescaling work.
91*b2055c35SXin Li   WebPRescaler*    rescaler;         // Common rescaler for all channels.
92*b2055c35SXin Li };
93*b2055c35SXin Li 
94*b2055c35SXin Li //------------------------------------------------------------------------------
95*b2055c35SXin Li // internal functions. Not public.
96*b2055c35SXin Li 
97*b2055c35SXin Li struct ALPHDecoder;  // Defined in dec/alphai.h.
98*b2055c35SXin Li 
99*b2055c35SXin Li // in vp8l.c
100*b2055c35SXin Li 
101*b2055c35SXin Li // Decodes image header for alpha data stored using lossless compression.
102*b2055c35SXin Li // Returns false in case of error.
103*b2055c35SXin Li WEBP_NODISCARD int VP8LDecodeAlphaHeader(struct ALPHDecoder* const alph_dec,
104*b2055c35SXin Li                                          const uint8_t* const data,
105*b2055c35SXin Li                                          size_t data_size);
106*b2055c35SXin Li 
107*b2055c35SXin Li // Decodes *at least* 'last_row' rows of alpha. If some of the initial rows are
108*b2055c35SXin Li // already decoded in previous call(s), it will resume decoding from where it
109*b2055c35SXin Li // was paused.
110*b2055c35SXin Li // Returns false in case of bitstream error.
111*b2055c35SXin Li WEBP_NODISCARD int VP8LDecodeAlphaImageStream(
112*b2055c35SXin Li     struct ALPHDecoder* const alph_dec, int last_row);
113*b2055c35SXin Li 
114*b2055c35SXin Li // Allocates and initialize a new lossless decoder instance.
115*b2055c35SXin Li WEBP_NODISCARD VP8LDecoder* VP8LNew(void);
116*b2055c35SXin Li 
117*b2055c35SXin Li // Decodes the image header. Returns false in case of error.
118*b2055c35SXin Li WEBP_NODISCARD int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io);
119*b2055c35SXin Li 
120*b2055c35SXin Li // Decodes an image. It's required to decode the lossless header before calling
121*b2055c35SXin Li // this function. Returns false in case of error, with updated dec->status_.
122*b2055c35SXin Li WEBP_NODISCARD int VP8LDecodeImage(VP8LDecoder* const dec);
123*b2055c35SXin Li 
124*b2055c35SXin Li // Resets the decoder in its initial state, reclaiming memory.
125*b2055c35SXin Li // Preserves the dec->status_ value.
126*b2055c35SXin Li void VP8LClear(VP8LDecoder* const dec);
127*b2055c35SXin Li 
128*b2055c35SXin Li // Clears and deallocate a lossless decoder instance.
129*b2055c35SXin Li void VP8LDelete(VP8LDecoder* const dec);
130*b2055c35SXin Li 
131*b2055c35SXin Li // Helper function for reading the different Huffman codes and storing them in
132*b2055c35SXin Li // 'huffman_tables' and 'htree_groups'.
133*b2055c35SXin Li // If mapping is NULL 'num_htree_groups_max' must equal 'num_htree_groups'.
134*b2055c35SXin Li // If it is not NULL, it maps 'num_htree_groups_max' indices to the
135*b2055c35SXin Li // 'num_htree_groups' groups. If 'num_htree_groups_max' > 'num_htree_groups',
136*b2055c35SXin Li // some of those indices map to -1. This is used for non-balanced codes to
137*b2055c35SXin Li // limit memory usage.
138*b2055c35SXin Li WEBP_NODISCARD int ReadHuffmanCodesHelper(
139*b2055c35SXin Li     int color_cache_bits, int num_htree_groups, int num_htree_groups_max,
140*b2055c35SXin Li     const int* const mapping, VP8LDecoder* const dec,
141*b2055c35SXin Li     HuffmanTables* const huffman_tables, HTreeGroup** const htree_groups);
142*b2055c35SXin Li 
143*b2055c35SXin Li //------------------------------------------------------------------------------
144*b2055c35SXin Li 
145*b2055c35SXin Li #ifdef __cplusplus
146*b2055c35SXin Li }    // extern "C"
147*b2055c35SXin Li #endif
148*b2055c35SXin Li 
149*b2055c35SXin Li #endif  // WEBP_DEC_VP8LI_DEC_H_
150