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