1 // Copyright 2012 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 // Lossless decoder: internal header.
11 //
12 // Author: Skal (pascal.massimino@gmail.com)
13 //         Vikas Arora(vikaas.arora@gmail.com)
14 
15 #ifndef WEBP_DEC_VP8LI_DEC_H_
16 #define WEBP_DEC_VP8LI_DEC_H_
17 
18 #include <string.h>     // for memcpy()
19 #include "src/dec/webpi_dec.h"
20 #include "src/utils/bit_reader_utils.h"
21 #include "src/utils/color_cache_utils.h"
22 #include "src/utils/huffman_utils.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 typedef enum {
29   READ_DATA = 0,
30   READ_HDR = 1,
31   READ_DIM = 2
32 } VP8LDecodeState;
33 
34 typedef struct VP8LTransform VP8LTransform;
35 struct VP8LTransform {
36   VP8LImageTransformType type_;   // transform type.
37   int                    bits_;   // subsampling bits defining transform window.
38   int                    xsize_;  // transform window X index.
39   int                    ysize_;  // transform window Y index.
40   uint32_t*              data_;   // transform data.
41 };
42 
43 typedef struct {
44   int             color_cache_size_;
45   VP8LColorCache  color_cache_;
46   VP8LColorCache  saved_color_cache_;  // for incremental
47 
48   int             huffman_mask_;
49   int             huffman_subsample_bits_;
50   int             huffman_xsize_;
51   uint32_t*       huffman_image_;
52   int             num_htree_groups_;
53   HTreeGroup*     htree_groups_;
54   HuffmanCode*    huffman_tables_;
55 } VP8LMetadata;
56 
57 typedef struct VP8LDecoder VP8LDecoder;
58 struct VP8LDecoder {
59   VP8StatusCode    status_;
60   VP8LDecodeState  state_;
61   VP8Io*           io_;
62 
63   const WebPDecBuffer* output_;    // shortcut to io->opaque->output
64 
65   uint32_t*        pixels_;        // Internal data: either uint8_t* for alpha
66                                    // or uint32_t* for BGRA.
67   uint32_t*        argb_cache_;    // Scratch buffer for temporary BGRA storage.
68 
69   VP8LBitReader    br_;
70   int              incremental_;   // if true, incremental decoding is expected
71   VP8LBitReader    saved_br_;      // note: could be local variables too
72   int              saved_last_pixel_;
73 
74   int              width_;
75   int              height_;
76   int              last_row_;      // last input row decoded so far.
77   int              last_pixel_;    // last pixel decoded so far. However, it may
78                                    // not be transformed, scaled and
79                                    // color-converted yet.
80   int              last_out_row_;  // last row output so far.
81 
82   VP8LMetadata     hdr_;
83 
84   int              next_transform_;
85   VP8LTransform    transforms_[NUM_TRANSFORMS];
86   // or'd bitset storing the transforms types.
87   uint32_t         transforms_seen_;
88 
89   uint8_t*         rescaler_memory;  // Working memory for rescaling work.
90   WebPRescaler*    rescaler;         // Common rescaler for all channels.
91 };
92 
93 //------------------------------------------------------------------------------
94 // internal functions. Not public.
95 
96 struct ALPHDecoder;  // Defined in dec/alphai.h.
97 
98 // in vp8l.c
99 
100 // Decodes image header for alpha data stored using lossless compression.
101 // Returns false in case of error.
102 int VP8LDecodeAlphaHeader(struct ALPHDecoder* const alph_dec,
103                           const uint8_t* const data, size_t data_size);
104 
105 // Decodes *at least* 'last_row' rows of alpha. If some of the initial rows are
106 // already decoded in previous call(s), it will resume decoding from where it
107 // was paused.
108 // Returns false in case of bitstream error.
109 int VP8LDecodeAlphaImageStream(struct ALPHDecoder* const alph_dec,
110                                int last_row);
111 
112 // Allocates and initialize a new lossless decoder instance.
113 VP8LDecoder* VP8LNew(void);
114 
115 // Decodes the image header. Returns false in case of error.
116 int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io);
117 
118 // Decodes an image. It's required to decode the lossless header before calling
119 // this function. Returns false in case of error, with updated dec->status_.
120 int VP8LDecodeImage(VP8LDecoder* const dec);
121 
122 // Resets the decoder in its initial state, reclaiming memory.
123 // Preserves the dec->status_ value.
124 void VP8LClear(VP8LDecoder* const dec);
125 
126 // Clears and deallocate a lossless decoder instance.
127 void VP8LDelete(VP8LDecoder* const dec);
128 
129 //------------------------------------------------------------------------------
130 
131 #ifdef __cplusplus
132 }    // extern "C"
133 #endif
134 
135 #endif  // WEBP_DEC_VP8LI_DEC_H_
136