/src/libwebp/src/dec/vp8l_dec.c
Line | Count | Source (jump to first uncovered line) |
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 | | // main entry for the decoder |
11 | | // |
12 | | // Authors: Vikas Arora (vikaas.arora@gmail.com) |
13 | | // Jyrki Alakuijala (jyrki@google.com) |
14 | | |
15 | | #include <assert.h> |
16 | | #include <stdlib.h> |
17 | | |
18 | | #include "src/dec/alphai_dec.h" |
19 | | #include "src/dec/vp8li_dec.h" |
20 | | #include "src/dsp/dsp.h" |
21 | | #include "src/dsp/lossless.h" |
22 | | #include "src/dsp/lossless_common.h" |
23 | | #include "src/utils/huffman_utils.h" |
24 | | #include "src/utils/utils.h" |
25 | | #include "src/webp/format_constants.h" |
26 | | |
27 | 0 | #define NUM_ARGB_CACHE_ROWS 16 |
28 | | |
29 | | static const int kCodeLengthLiterals = 16; |
30 | | static const int kCodeLengthRepeatCode = 16; |
31 | | static const uint8_t kCodeLengthExtraBits[3] = { 2, 3, 7 }; |
32 | | static const uint8_t kCodeLengthRepeatOffsets[3] = { 3, 3, 11 }; |
33 | | |
34 | | // ----------------------------------------------------------------------------- |
35 | | // Five Huffman codes are used at each meta code: |
36 | | // 1. green + length prefix codes + color cache codes, |
37 | | // 2. alpha, |
38 | | // 3. red, |
39 | | // 4. blue, and, |
40 | | // 5. distance prefix codes. |
41 | | typedef enum { |
42 | | GREEN = 0, |
43 | | RED = 1, |
44 | | BLUE = 2, |
45 | | ALPHA = 3, |
46 | | DIST = 4 |
47 | | } HuffIndex; |
48 | | |
49 | | static const uint16_t kAlphabetSize[HUFFMAN_CODES_PER_META_CODE] = { |
50 | | NUM_LITERAL_CODES + NUM_LENGTH_CODES, |
51 | | NUM_LITERAL_CODES, NUM_LITERAL_CODES, NUM_LITERAL_CODES, |
52 | | NUM_DISTANCE_CODES |
53 | | }; |
54 | | |
55 | | static const uint8_t kLiteralMap[HUFFMAN_CODES_PER_META_CODE] = { |
56 | | 0, 1, 1, 1, 0 |
57 | | }; |
58 | | |
59 | 0 | #define NUM_CODE_LENGTH_CODES 19 |
60 | | static const uint8_t kCodeLengthCodeOrder[NUM_CODE_LENGTH_CODES] = { |
61 | | 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 |
62 | | }; |
63 | | |
64 | 0 | #define CODE_TO_PLANE_CODES 120 |
65 | | static const uint8_t kCodeToPlane[CODE_TO_PLANE_CODES] = { |
66 | | 0x18, 0x07, 0x17, 0x19, 0x28, 0x06, 0x27, 0x29, 0x16, 0x1a, |
67 | | 0x26, 0x2a, 0x38, 0x05, 0x37, 0x39, 0x15, 0x1b, 0x36, 0x3a, |
68 | | 0x25, 0x2b, 0x48, 0x04, 0x47, 0x49, 0x14, 0x1c, 0x35, 0x3b, |
69 | | 0x46, 0x4a, 0x24, 0x2c, 0x58, 0x45, 0x4b, 0x34, 0x3c, 0x03, |
70 | | 0x57, 0x59, 0x13, 0x1d, 0x56, 0x5a, 0x23, 0x2d, 0x44, 0x4c, |
71 | | 0x55, 0x5b, 0x33, 0x3d, 0x68, 0x02, 0x67, 0x69, 0x12, 0x1e, |
72 | | 0x66, 0x6a, 0x22, 0x2e, 0x54, 0x5c, 0x43, 0x4d, 0x65, 0x6b, |
73 | | 0x32, 0x3e, 0x78, 0x01, 0x77, 0x79, 0x53, 0x5d, 0x11, 0x1f, |
74 | | 0x64, 0x6c, 0x42, 0x4e, 0x76, 0x7a, 0x21, 0x2f, 0x75, 0x7b, |
75 | | 0x31, 0x3f, 0x63, 0x6d, 0x52, 0x5e, 0x00, 0x74, 0x7c, 0x41, |
76 | | 0x4f, 0x10, 0x20, 0x62, 0x6e, 0x30, 0x73, 0x7d, 0x51, 0x5f, |
77 | | 0x40, 0x72, 0x7e, 0x61, 0x6f, 0x50, 0x71, 0x7f, 0x60, 0x70 |
78 | | }; |
79 | | |
80 | | // Memory needed for lookup tables of one Huffman tree group. Red, blue, alpha |
81 | | // and distance alphabets are constant (256 for red, blue and alpha, 40 for |
82 | | // distance) and lookup table sizes for them in worst case are 630 and 410 |
83 | | // respectively. Size of green alphabet depends on color cache size and is equal |
84 | | // to 256 (green component values) + 24 (length prefix values) |
85 | | // + color_cache_size (between 0 and 2048). |
86 | | // All values computed for 8-bit first level lookup with Mark Adler's tool: |
87 | | // https://github.com/madler/zlib/blob/v1.2.5/examples/enough.c |
88 | | #define FIXED_TABLE_SIZE (630 * 3 + 410) |
89 | | static const uint16_t kTableSize[12] = { |
90 | | FIXED_TABLE_SIZE + 654, |
91 | | FIXED_TABLE_SIZE + 656, |
92 | | FIXED_TABLE_SIZE + 658, |
93 | | FIXED_TABLE_SIZE + 662, |
94 | | FIXED_TABLE_SIZE + 670, |
95 | | FIXED_TABLE_SIZE + 686, |
96 | | FIXED_TABLE_SIZE + 718, |
97 | | FIXED_TABLE_SIZE + 782, |
98 | | FIXED_TABLE_SIZE + 912, |
99 | | FIXED_TABLE_SIZE + 1168, |
100 | | FIXED_TABLE_SIZE + 1680, |
101 | | FIXED_TABLE_SIZE + 2704 |
102 | | }; |
103 | | |
104 | 0 | static int VP8LSetError(VP8LDecoder* const dec, VP8StatusCode error) { |
105 | | // The oldest error reported takes precedence over the new one. |
106 | 0 | if (dec->status_ == VP8_STATUS_OK || dec->status_ == VP8_STATUS_SUSPENDED) { |
107 | 0 | dec->status_ = error; |
108 | 0 | } |
109 | 0 | return 0; |
110 | 0 | } |
111 | | |
112 | | static int DecodeImageStream(int xsize, int ysize, |
113 | | int is_level0, |
114 | | VP8LDecoder* const dec, |
115 | | uint32_t** const decoded_data); |
116 | | |
117 | | //------------------------------------------------------------------------------ |
118 | | |
119 | 0 | int VP8LCheckSignature(const uint8_t* const data, size_t size) { |
120 | 0 | return (size >= VP8L_FRAME_HEADER_SIZE && |
121 | 0 | data[0] == VP8L_MAGIC_BYTE && |
122 | 0 | (data[4] >> 5) == 0); // version |
123 | 0 | } |
124 | | |
125 | | static int ReadImageInfo(VP8LBitReader* const br, |
126 | | int* const width, int* const height, |
127 | 0 | int* const has_alpha) { |
128 | 0 | if (VP8LReadBits(br, 8) != VP8L_MAGIC_BYTE) return 0; |
129 | 0 | *width = VP8LReadBits(br, VP8L_IMAGE_SIZE_BITS) + 1; |
130 | 0 | *height = VP8LReadBits(br, VP8L_IMAGE_SIZE_BITS) + 1; |
131 | 0 | *has_alpha = VP8LReadBits(br, 1); |
132 | 0 | if (VP8LReadBits(br, VP8L_VERSION_BITS) != 0) return 0; |
133 | 0 | return !br->eos_; |
134 | 0 | } |
135 | | |
136 | | int VP8LGetInfo(const uint8_t* data, size_t data_size, |
137 | 0 | int* const width, int* const height, int* const has_alpha) { |
138 | 0 | if (data == NULL || data_size < VP8L_FRAME_HEADER_SIZE) { |
139 | 0 | return 0; // not enough data |
140 | 0 | } else if (!VP8LCheckSignature(data, data_size)) { |
141 | 0 | return 0; // bad signature |
142 | 0 | } else { |
143 | 0 | int w, h, a; |
144 | 0 | VP8LBitReader br; |
145 | 0 | VP8LInitBitReader(&br, data, data_size); |
146 | 0 | if (!ReadImageInfo(&br, &w, &h, &a)) { |
147 | 0 | return 0; |
148 | 0 | } |
149 | 0 | if (width != NULL) *width = w; |
150 | 0 | if (height != NULL) *height = h; |
151 | 0 | if (has_alpha != NULL) *has_alpha = a; |
152 | 0 | return 1; |
153 | 0 | } |
154 | 0 | } |
155 | | |
156 | | //------------------------------------------------------------------------------ |
157 | | |
158 | | static WEBP_INLINE int GetCopyDistance(int distance_symbol, |
159 | 0 | VP8LBitReader* const br) { |
160 | 0 | int extra_bits, offset; |
161 | 0 | if (distance_symbol < 4) { |
162 | 0 | return distance_symbol + 1; |
163 | 0 | } |
164 | 0 | extra_bits = (distance_symbol - 2) >> 1; |
165 | 0 | offset = (2 + (distance_symbol & 1)) << extra_bits; |
166 | 0 | return offset + VP8LReadBits(br, extra_bits) + 1; |
167 | 0 | } |
168 | | |
169 | | static WEBP_INLINE int GetCopyLength(int length_symbol, |
170 | 0 | VP8LBitReader* const br) { |
171 | | // Length and distance prefixes are encoded the same way. |
172 | 0 | return GetCopyDistance(length_symbol, br); |
173 | 0 | } |
174 | | |
175 | 0 | static WEBP_INLINE int PlaneCodeToDistance(int xsize, int plane_code) { |
176 | 0 | if (plane_code > CODE_TO_PLANE_CODES) { |
177 | 0 | return plane_code - CODE_TO_PLANE_CODES; |
178 | 0 | } else { |
179 | 0 | const int dist_code = kCodeToPlane[plane_code - 1]; |
180 | 0 | const int yoffset = dist_code >> 4; |
181 | 0 | const int xoffset = 8 - (dist_code & 0xf); |
182 | 0 | const int dist = yoffset * xsize + xoffset; |
183 | 0 | return (dist >= 1) ? dist : 1; // dist<1 can happen if xsize is very small |
184 | 0 | } |
185 | 0 | } |
186 | | |
187 | | //------------------------------------------------------------------------------ |
188 | | // Decodes the next Huffman code from bit-stream. |
189 | | // VP8LFillBitWindow(br) needs to be called at minimum every second call |
190 | | // to ReadSymbol, in order to pre-fetch enough bits. |
191 | | static WEBP_INLINE int ReadSymbol(const HuffmanCode* table, |
192 | 0 | VP8LBitReader* const br) { |
193 | 0 | int nbits; |
194 | 0 | uint32_t val = VP8LPrefetchBits(br); |
195 | 0 | table += val & HUFFMAN_TABLE_MASK; |
196 | 0 | nbits = table->bits - HUFFMAN_TABLE_BITS; |
197 | 0 | if (nbits > 0) { |
198 | 0 | VP8LSetBitPos(br, br->bit_pos_ + HUFFMAN_TABLE_BITS); |
199 | 0 | val = VP8LPrefetchBits(br); |
200 | 0 | table += table->value; |
201 | 0 | table += val & ((1 << nbits) - 1); |
202 | 0 | } |
203 | 0 | VP8LSetBitPos(br, br->bit_pos_ + table->bits); |
204 | 0 | return table->value; |
205 | 0 | } |
206 | | |
207 | | // Reads packed symbol depending on GREEN channel |
208 | 0 | #define BITS_SPECIAL_MARKER 0x100 // something large enough (and a bit-mask) |
209 | 0 | #define PACKED_NON_LITERAL_CODE 0 // must be < NUM_LITERAL_CODES |
210 | | static WEBP_INLINE int ReadPackedSymbols(const HTreeGroup* group, |
211 | | VP8LBitReader* const br, |
212 | 0 | uint32_t* const dst) { |
213 | 0 | const uint32_t val = VP8LPrefetchBits(br) & (HUFFMAN_PACKED_TABLE_SIZE - 1); |
214 | 0 | const HuffmanCode32 code = group->packed_table[val]; |
215 | 0 | assert(group->use_packed_table); |
216 | 0 | if (code.bits < BITS_SPECIAL_MARKER) { |
217 | 0 | VP8LSetBitPos(br, br->bit_pos_ + code.bits); |
218 | 0 | *dst = code.value; |
219 | 0 | return PACKED_NON_LITERAL_CODE; |
220 | 0 | } else { |
221 | 0 | VP8LSetBitPos(br, br->bit_pos_ + code.bits - BITS_SPECIAL_MARKER); |
222 | 0 | assert(code.value >= NUM_LITERAL_CODES); |
223 | 0 | return code.value; |
224 | 0 | } |
225 | 0 | } |
226 | | |
227 | | static int AccumulateHCode(HuffmanCode hcode, int shift, |
228 | 0 | HuffmanCode32* const huff) { |
229 | 0 | huff->bits += hcode.bits; |
230 | 0 | huff->value |= (uint32_t)hcode.value << shift; |
231 | 0 | assert(huff->bits <= HUFFMAN_TABLE_BITS); |
232 | 0 | return hcode.bits; |
233 | 0 | } |
234 | | |
235 | 0 | static void BuildPackedTable(HTreeGroup* const htree_group) { |
236 | 0 | uint32_t code; |
237 | 0 | for (code = 0; code < HUFFMAN_PACKED_TABLE_SIZE; ++code) { |
238 | 0 | uint32_t bits = code; |
239 | 0 | HuffmanCode32* const huff = &htree_group->packed_table[bits]; |
240 | 0 | HuffmanCode hcode = htree_group->htrees[GREEN][bits]; |
241 | 0 | if (hcode.value >= NUM_LITERAL_CODES) { |
242 | 0 | huff->bits = hcode.bits + BITS_SPECIAL_MARKER; |
243 | 0 | huff->value = hcode.value; |
244 | 0 | } else { |
245 | 0 | huff->bits = 0; |
246 | 0 | huff->value = 0; |
247 | 0 | bits >>= AccumulateHCode(hcode, 8, huff); |
248 | 0 | bits >>= AccumulateHCode(htree_group->htrees[RED][bits], 16, huff); |
249 | 0 | bits >>= AccumulateHCode(htree_group->htrees[BLUE][bits], 0, huff); |
250 | 0 | bits >>= AccumulateHCode(htree_group->htrees[ALPHA][bits], 24, huff); |
251 | 0 | (void)bits; |
252 | 0 | } |
253 | 0 | } |
254 | 0 | } |
255 | | |
256 | | static int ReadHuffmanCodeLengths( |
257 | | VP8LDecoder* const dec, const int* const code_length_code_lengths, |
258 | 0 | int num_symbols, int* const code_lengths) { |
259 | 0 | int ok = 0; |
260 | 0 | VP8LBitReader* const br = &dec->br_; |
261 | 0 | int symbol; |
262 | 0 | int max_symbol; |
263 | 0 | int prev_code_len = DEFAULT_CODE_LENGTH; |
264 | 0 | HuffmanTables tables; |
265 | |
|
266 | 0 | if (!VP8LHuffmanTablesAllocate(1 << LENGTHS_TABLE_BITS, &tables) || |
267 | 0 | !VP8LBuildHuffmanTable(&tables, LENGTHS_TABLE_BITS, |
268 | 0 | code_length_code_lengths, NUM_CODE_LENGTH_CODES)) { |
269 | 0 | goto End; |
270 | 0 | } |
271 | | |
272 | 0 | if (VP8LReadBits(br, 1)) { // use length |
273 | 0 | const int length_nbits = 2 + 2 * VP8LReadBits(br, 3); |
274 | 0 | max_symbol = 2 + VP8LReadBits(br, length_nbits); |
275 | 0 | if (max_symbol > num_symbols) { |
276 | 0 | goto End; |
277 | 0 | } |
278 | 0 | } else { |
279 | 0 | max_symbol = num_symbols; |
280 | 0 | } |
281 | | |
282 | 0 | symbol = 0; |
283 | 0 | while (symbol < num_symbols) { |
284 | 0 | const HuffmanCode* p; |
285 | 0 | int code_len; |
286 | 0 | if (max_symbol-- == 0) break; |
287 | 0 | VP8LFillBitWindow(br); |
288 | 0 | p = &tables.curr_segment->start[VP8LPrefetchBits(br) & LENGTHS_TABLE_MASK]; |
289 | 0 | VP8LSetBitPos(br, br->bit_pos_ + p->bits); |
290 | 0 | code_len = p->value; |
291 | 0 | if (code_len < kCodeLengthLiterals) { |
292 | 0 | code_lengths[symbol++] = code_len; |
293 | 0 | if (code_len != 0) prev_code_len = code_len; |
294 | 0 | } else { |
295 | 0 | const int use_prev = (code_len == kCodeLengthRepeatCode); |
296 | 0 | const int slot = code_len - kCodeLengthLiterals; |
297 | 0 | const int extra_bits = kCodeLengthExtraBits[slot]; |
298 | 0 | const int repeat_offset = kCodeLengthRepeatOffsets[slot]; |
299 | 0 | int repeat = VP8LReadBits(br, extra_bits) + repeat_offset; |
300 | 0 | if (symbol + repeat > num_symbols) { |
301 | 0 | goto End; |
302 | 0 | } else { |
303 | 0 | const int length = use_prev ? prev_code_len : 0; |
304 | 0 | while (repeat-- > 0) code_lengths[symbol++] = length; |
305 | 0 | } |
306 | 0 | } |
307 | 0 | } |
308 | 0 | ok = 1; |
309 | |
|
310 | 0 | End: |
311 | 0 | VP8LHuffmanTablesDeallocate(&tables); |
312 | 0 | if (!ok) return VP8LSetError(dec, VP8_STATUS_BITSTREAM_ERROR); |
313 | 0 | return ok; |
314 | 0 | } |
315 | | |
316 | | // 'code_lengths' is pre-allocated temporary buffer, used for creating Huffman |
317 | | // tree. |
318 | | static int ReadHuffmanCode(int alphabet_size, VP8LDecoder* const dec, |
319 | | int* const code_lengths, |
320 | 0 | HuffmanTables* const table) { |
321 | 0 | int ok = 0; |
322 | 0 | int size = 0; |
323 | 0 | VP8LBitReader* const br = &dec->br_; |
324 | 0 | const int simple_code = VP8LReadBits(br, 1); |
325 | |
|
326 | 0 | memset(code_lengths, 0, alphabet_size * sizeof(*code_lengths)); |
327 | |
|
328 | 0 | if (simple_code) { // Read symbols, codes & code lengths directly. |
329 | 0 | const int num_symbols = VP8LReadBits(br, 1) + 1; |
330 | 0 | const int first_symbol_len_code = VP8LReadBits(br, 1); |
331 | | // The first code is either 1 bit or 8 bit code. |
332 | 0 | int symbol = VP8LReadBits(br, (first_symbol_len_code == 0) ? 1 : 8); |
333 | 0 | code_lengths[symbol] = 1; |
334 | | // The second code (if present), is always 8 bits long. |
335 | 0 | if (num_symbols == 2) { |
336 | 0 | symbol = VP8LReadBits(br, 8); |
337 | 0 | code_lengths[symbol] = 1; |
338 | 0 | } |
339 | 0 | ok = 1; |
340 | 0 | } else { // Decode Huffman-coded code lengths. |
341 | 0 | int i; |
342 | 0 | int code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 }; |
343 | 0 | const int num_codes = VP8LReadBits(br, 4) + 4; |
344 | 0 | assert(num_codes <= NUM_CODE_LENGTH_CODES); |
345 | | |
346 | 0 | for (i = 0; i < num_codes; ++i) { |
347 | 0 | code_length_code_lengths[kCodeLengthCodeOrder[i]] = VP8LReadBits(br, 3); |
348 | 0 | } |
349 | 0 | ok = ReadHuffmanCodeLengths(dec, code_length_code_lengths, alphabet_size, |
350 | 0 | code_lengths); |
351 | 0 | } |
352 | | |
353 | 0 | ok = ok && !br->eos_; |
354 | 0 | if (ok) { |
355 | 0 | size = VP8LBuildHuffmanTable(table, HUFFMAN_TABLE_BITS, |
356 | 0 | code_lengths, alphabet_size); |
357 | 0 | } |
358 | 0 | if (!ok || size == 0) { |
359 | 0 | return VP8LSetError(dec, VP8_STATUS_BITSTREAM_ERROR); |
360 | 0 | } |
361 | 0 | return size; |
362 | 0 | } |
363 | | |
364 | | static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize, |
365 | 0 | int color_cache_bits, int allow_recursion) { |
366 | 0 | int i; |
367 | 0 | VP8LBitReader* const br = &dec->br_; |
368 | 0 | VP8LMetadata* const hdr = &dec->hdr_; |
369 | 0 | uint32_t* huffman_image = NULL; |
370 | 0 | HTreeGroup* htree_groups = NULL; |
371 | 0 | HuffmanTables* huffman_tables = &hdr->huffman_tables_; |
372 | 0 | int num_htree_groups = 1; |
373 | 0 | int num_htree_groups_max = 1; |
374 | 0 | int* mapping = NULL; |
375 | 0 | int ok = 0; |
376 | | |
377 | | // Check the table has been 0 initialized (through InitMetadata). |
378 | 0 | assert(huffman_tables->root.start == NULL); |
379 | 0 | assert(huffman_tables->curr_segment == NULL); |
380 | | |
381 | 0 | if (allow_recursion && VP8LReadBits(br, 1)) { |
382 | | // use meta Huffman codes. |
383 | 0 | const int huffman_precision = |
384 | 0 | MIN_HUFFMAN_BITS + VP8LReadBits(br, NUM_HUFFMAN_BITS); |
385 | 0 | const int huffman_xsize = VP8LSubSampleSize(xsize, huffman_precision); |
386 | 0 | const int huffman_ysize = VP8LSubSampleSize(ysize, huffman_precision); |
387 | 0 | const int huffman_pixs = huffman_xsize * huffman_ysize; |
388 | 0 | if (!DecodeImageStream(huffman_xsize, huffman_ysize, /*is_level0=*/0, dec, |
389 | 0 | &huffman_image)) { |
390 | 0 | goto Error; |
391 | 0 | } |
392 | 0 | hdr->huffman_subsample_bits_ = huffman_precision; |
393 | 0 | for (i = 0; i < huffman_pixs; ++i) { |
394 | | // The huffman data is stored in red and green bytes. |
395 | 0 | const int group = (huffman_image[i] >> 8) & 0xffff; |
396 | 0 | huffman_image[i] = group; |
397 | 0 | if (group >= num_htree_groups_max) { |
398 | 0 | num_htree_groups_max = group + 1; |
399 | 0 | } |
400 | 0 | } |
401 | | // Check the validity of num_htree_groups_max. If it seems too big, use a |
402 | | // smaller value for later. This will prevent big memory allocations to end |
403 | | // up with a bad bitstream anyway. |
404 | | // The value of 1000 is totally arbitrary. We know that num_htree_groups_max |
405 | | // is smaller than (1 << 16) and should be smaller than the number of pixels |
406 | | // (though the format allows it to be bigger). |
407 | 0 | if (num_htree_groups_max > 1000 || num_htree_groups_max > xsize * ysize) { |
408 | | // Create a mapping from the used indices to the minimal set of used |
409 | | // values [0, num_htree_groups) |
410 | 0 | mapping = (int*)WebPSafeMalloc(num_htree_groups_max, sizeof(*mapping)); |
411 | 0 | if (mapping == NULL) { |
412 | 0 | VP8LSetError(dec, VP8_STATUS_OUT_OF_MEMORY); |
413 | 0 | goto Error; |
414 | 0 | } |
415 | | // -1 means a value is unmapped, and therefore unused in the Huffman |
416 | | // image. |
417 | 0 | memset(mapping, 0xff, num_htree_groups_max * sizeof(*mapping)); |
418 | 0 | for (num_htree_groups = 0, i = 0; i < huffman_pixs; ++i) { |
419 | | // Get the current mapping for the group and remap the Huffman image. |
420 | 0 | int* const mapped_group = &mapping[huffman_image[i]]; |
421 | 0 | if (*mapped_group == -1) *mapped_group = num_htree_groups++; |
422 | 0 | huffman_image[i] = *mapped_group; |
423 | 0 | } |
424 | 0 | } else { |
425 | 0 | num_htree_groups = num_htree_groups_max; |
426 | 0 | } |
427 | 0 | } |
428 | | |
429 | 0 | if (br->eos_) goto Error; |
430 | | |
431 | 0 | if (!ReadHuffmanCodesHelper(color_cache_bits, num_htree_groups, |
432 | 0 | num_htree_groups_max, mapping, dec, |
433 | 0 | huffman_tables, &htree_groups)) { |
434 | 0 | goto Error; |
435 | 0 | } |
436 | 0 | ok = 1; |
437 | | |
438 | | // All OK. Finalize pointers. |
439 | 0 | hdr->huffman_image_ = huffman_image; |
440 | 0 | hdr->num_htree_groups_ = num_htree_groups; |
441 | 0 | hdr->htree_groups_ = htree_groups; |
442 | |
|
443 | 0 | Error: |
444 | 0 | WebPSafeFree(mapping); |
445 | 0 | if (!ok) { |
446 | 0 | WebPSafeFree(huffman_image); |
447 | 0 | VP8LHuffmanTablesDeallocate(huffman_tables); |
448 | 0 | VP8LHtreeGroupsFree(htree_groups); |
449 | 0 | } |
450 | 0 | return ok; |
451 | 0 | } |
452 | | |
453 | | int ReadHuffmanCodesHelper(int color_cache_bits, int num_htree_groups, |
454 | | int num_htree_groups_max, const int* const mapping, |
455 | | VP8LDecoder* const dec, |
456 | | HuffmanTables* const huffman_tables, |
457 | 0 | HTreeGroup** const htree_groups) { |
458 | 0 | int i, j, ok = 0; |
459 | 0 | const int max_alphabet_size = |
460 | 0 | kAlphabetSize[0] + ((color_cache_bits > 0) ? 1 << color_cache_bits : 0); |
461 | 0 | const int table_size = kTableSize[color_cache_bits]; |
462 | 0 | int* code_lengths = NULL; |
463 | |
|
464 | 0 | if ((mapping == NULL && num_htree_groups != num_htree_groups_max) || |
465 | 0 | num_htree_groups > num_htree_groups_max) { |
466 | 0 | goto Error; |
467 | 0 | } |
468 | | |
469 | 0 | code_lengths = |
470 | 0 | (int*)WebPSafeCalloc((uint64_t)max_alphabet_size, sizeof(*code_lengths)); |
471 | 0 | *htree_groups = VP8LHtreeGroupsNew(num_htree_groups); |
472 | |
|
473 | 0 | if (*htree_groups == NULL || code_lengths == NULL || |
474 | 0 | !VP8LHuffmanTablesAllocate(num_htree_groups * table_size, |
475 | 0 | huffman_tables)) { |
476 | 0 | VP8LSetError(dec, VP8_STATUS_OUT_OF_MEMORY); |
477 | 0 | goto Error; |
478 | 0 | } |
479 | | |
480 | 0 | for (i = 0; i < num_htree_groups_max; ++i) { |
481 | | // If the index "i" is unused in the Huffman image, just make sure the |
482 | | // coefficients are valid but do not store them. |
483 | 0 | if (mapping != NULL && mapping[i] == -1) { |
484 | 0 | for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) { |
485 | 0 | int alphabet_size = kAlphabetSize[j]; |
486 | 0 | if (j == 0 && color_cache_bits > 0) { |
487 | 0 | alphabet_size += (1 << color_cache_bits); |
488 | 0 | } |
489 | | // Passing in NULL so that nothing gets filled. |
490 | 0 | if (!ReadHuffmanCode(alphabet_size, dec, code_lengths, NULL)) { |
491 | 0 | goto Error; |
492 | 0 | } |
493 | 0 | } |
494 | 0 | } else { |
495 | 0 | HTreeGroup* const htree_group = |
496 | 0 | &(*htree_groups)[(mapping == NULL) ? i : mapping[i]]; |
497 | 0 | HuffmanCode** const htrees = htree_group->htrees; |
498 | 0 | int size; |
499 | 0 | int total_size = 0; |
500 | 0 | int is_trivial_literal = 1; |
501 | 0 | int max_bits = 0; |
502 | 0 | for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) { |
503 | 0 | int alphabet_size = kAlphabetSize[j]; |
504 | 0 | if (j == 0 && color_cache_bits > 0) { |
505 | 0 | alphabet_size += (1 << color_cache_bits); |
506 | 0 | } |
507 | 0 | size = |
508 | 0 | ReadHuffmanCode(alphabet_size, dec, code_lengths, huffman_tables); |
509 | 0 | htrees[j] = huffman_tables->curr_segment->curr_table; |
510 | 0 | if (size == 0) { |
511 | 0 | goto Error; |
512 | 0 | } |
513 | 0 | if (is_trivial_literal && kLiteralMap[j] == 1) { |
514 | 0 | is_trivial_literal = (htrees[j]->bits == 0); |
515 | 0 | } |
516 | 0 | total_size += htrees[j]->bits; |
517 | 0 | huffman_tables->curr_segment->curr_table += size; |
518 | 0 | if (j <= ALPHA) { |
519 | 0 | int local_max_bits = code_lengths[0]; |
520 | 0 | int k; |
521 | 0 | for (k = 1; k < alphabet_size; ++k) { |
522 | 0 | if (code_lengths[k] > local_max_bits) { |
523 | 0 | local_max_bits = code_lengths[k]; |
524 | 0 | } |
525 | 0 | } |
526 | 0 | max_bits += local_max_bits; |
527 | 0 | } |
528 | 0 | } |
529 | 0 | htree_group->is_trivial_literal = is_trivial_literal; |
530 | 0 | htree_group->is_trivial_code = 0; |
531 | 0 | if (is_trivial_literal) { |
532 | 0 | const int red = htrees[RED][0].value; |
533 | 0 | const int blue = htrees[BLUE][0].value; |
534 | 0 | const int alpha = htrees[ALPHA][0].value; |
535 | 0 | htree_group->literal_arb = ((uint32_t)alpha << 24) | (red << 16) | blue; |
536 | 0 | if (total_size == 0 && htrees[GREEN][0].value < NUM_LITERAL_CODES) { |
537 | 0 | htree_group->is_trivial_code = 1; |
538 | 0 | htree_group->literal_arb |= htrees[GREEN][0].value << 8; |
539 | 0 | } |
540 | 0 | } |
541 | 0 | htree_group->use_packed_table = |
542 | 0 | !htree_group->is_trivial_code && (max_bits < HUFFMAN_PACKED_BITS); |
543 | 0 | if (htree_group->use_packed_table) BuildPackedTable(htree_group); |
544 | 0 | } |
545 | 0 | } |
546 | 0 | ok = 1; |
547 | |
|
548 | 0 | Error: |
549 | 0 | WebPSafeFree(code_lengths); |
550 | 0 | if (!ok) { |
551 | 0 | VP8LHuffmanTablesDeallocate(huffman_tables); |
552 | 0 | VP8LHtreeGroupsFree(*htree_groups); |
553 | 0 | *htree_groups = NULL; |
554 | 0 | } |
555 | 0 | return ok; |
556 | 0 | } |
557 | | |
558 | | //------------------------------------------------------------------------------ |
559 | | // Scaling. |
560 | | |
561 | | #if !defined(WEBP_REDUCE_SIZE) |
562 | 0 | static int AllocateAndInitRescaler(VP8LDecoder* const dec, VP8Io* const io) { |
563 | 0 | const int num_channels = 4; |
564 | 0 | const int in_width = io->mb_w; |
565 | 0 | const int out_width = io->scaled_width; |
566 | 0 | const int in_height = io->mb_h; |
567 | 0 | const int out_height = io->scaled_height; |
568 | 0 | const uint64_t work_size = 2 * num_channels * (uint64_t)out_width; |
569 | 0 | rescaler_t* work; // Rescaler work area. |
570 | 0 | const uint64_t scaled_data_size = (uint64_t)out_width; |
571 | 0 | uint32_t* scaled_data; // Temporary storage for scaled BGRA data. |
572 | 0 | const uint64_t memory_size = sizeof(*dec->rescaler) + |
573 | 0 | work_size * sizeof(*work) + |
574 | 0 | scaled_data_size * sizeof(*scaled_data); |
575 | 0 | uint8_t* memory = (uint8_t*)WebPSafeMalloc(memory_size, sizeof(*memory)); |
576 | 0 | if (memory == NULL) { |
577 | 0 | return VP8LSetError(dec, VP8_STATUS_OUT_OF_MEMORY); |
578 | 0 | } |
579 | 0 | assert(dec->rescaler_memory == NULL); |
580 | 0 | dec->rescaler_memory = memory; |
581 | |
|
582 | 0 | dec->rescaler = (WebPRescaler*)memory; |
583 | 0 | memory += sizeof(*dec->rescaler); |
584 | 0 | work = (rescaler_t*)memory; |
585 | 0 | memory += work_size * sizeof(*work); |
586 | 0 | scaled_data = (uint32_t*)memory; |
587 | |
|
588 | 0 | if (!WebPRescalerInit(dec->rescaler, in_width, in_height, |
589 | 0 | (uint8_t*)scaled_data, out_width, out_height, |
590 | 0 | 0, num_channels, work)) { |
591 | 0 | return 0; |
592 | 0 | } |
593 | 0 | return 1; |
594 | 0 | } |
595 | | #endif // WEBP_REDUCE_SIZE |
596 | | |
597 | | //------------------------------------------------------------------------------ |
598 | | // Export to ARGB |
599 | | |
600 | | #if !defined(WEBP_REDUCE_SIZE) |
601 | | |
602 | | // We have special "export" function since we need to convert from BGRA |
603 | | static int Export(WebPRescaler* const rescaler, WEBP_CSP_MODE colorspace, |
604 | 0 | int rgba_stride, uint8_t* const rgba) { |
605 | 0 | uint32_t* const src = (uint32_t*)rescaler->dst; |
606 | 0 | uint8_t* dst = rgba; |
607 | 0 | const int dst_width = rescaler->dst_width; |
608 | 0 | int num_lines_out = 0; |
609 | 0 | while (WebPRescalerHasPendingOutput(rescaler)) { |
610 | 0 | WebPRescalerExportRow(rescaler); |
611 | 0 | WebPMultARGBRow(src, dst_width, 1); |
612 | 0 | VP8LConvertFromBGRA(src, dst_width, colorspace, dst); |
613 | 0 | dst += rgba_stride; |
614 | 0 | ++num_lines_out; |
615 | 0 | } |
616 | 0 | return num_lines_out; |
617 | 0 | } |
618 | | |
619 | | // Emit scaled rows. |
620 | | static int EmitRescaledRowsRGBA(const VP8LDecoder* const dec, |
621 | | uint8_t* in, int in_stride, int mb_h, |
622 | 0 | uint8_t* const out, int out_stride) { |
623 | 0 | const WEBP_CSP_MODE colorspace = dec->output_->colorspace; |
624 | 0 | int num_lines_in = 0; |
625 | 0 | int num_lines_out = 0; |
626 | 0 | while (num_lines_in < mb_h) { |
627 | 0 | uint8_t* const row_in = in + (uint64_t)num_lines_in * in_stride; |
628 | 0 | uint8_t* const row_out = out + (uint64_t)num_lines_out * out_stride; |
629 | 0 | const int lines_left = mb_h - num_lines_in; |
630 | 0 | const int needed_lines = WebPRescaleNeededLines(dec->rescaler, lines_left); |
631 | 0 | int lines_imported; |
632 | 0 | assert(needed_lines > 0 && needed_lines <= lines_left); |
633 | 0 | WebPMultARGBRows(row_in, in_stride, |
634 | 0 | dec->rescaler->src_width, needed_lines, 0); |
635 | 0 | lines_imported = |
636 | 0 | WebPRescalerImport(dec->rescaler, lines_left, row_in, in_stride); |
637 | 0 | assert(lines_imported == needed_lines); |
638 | 0 | num_lines_in += lines_imported; |
639 | 0 | num_lines_out += Export(dec->rescaler, colorspace, out_stride, row_out); |
640 | 0 | } |
641 | 0 | return num_lines_out; |
642 | 0 | } |
643 | | |
644 | | #endif // WEBP_REDUCE_SIZE |
645 | | |
646 | | // Emit rows without any scaling. |
647 | | static int EmitRows(WEBP_CSP_MODE colorspace, |
648 | | const uint8_t* row_in, int in_stride, |
649 | | int mb_w, int mb_h, |
650 | 0 | uint8_t* const out, int out_stride) { |
651 | 0 | int lines = mb_h; |
652 | 0 | uint8_t* row_out = out; |
653 | 0 | while (lines-- > 0) { |
654 | 0 | VP8LConvertFromBGRA((const uint32_t*)row_in, mb_w, colorspace, row_out); |
655 | 0 | row_in += in_stride; |
656 | 0 | row_out += out_stride; |
657 | 0 | } |
658 | 0 | return mb_h; // Num rows out == num rows in. |
659 | 0 | } |
660 | | |
661 | | //------------------------------------------------------------------------------ |
662 | | // Export to YUVA |
663 | | |
664 | | static void ConvertToYUVA(const uint32_t* const src, int width, int y_pos, |
665 | 0 | const WebPDecBuffer* const output) { |
666 | 0 | const WebPYUVABuffer* const buf = &output->u.YUVA; |
667 | | |
668 | | // first, the luma plane |
669 | 0 | WebPConvertARGBToY(src, buf->y + y_pos * buf->y_stride, width); |
670 | | |
671 | | // then U/V planes |
672 | 0 | { |
673 | 0 | uint8_t* const u = buf->u + (y_pos >> 1) * buf->u_stride; |
674 | 0 | uint8_t* const v = buf->v + (y_pos >> 1) * buf->v_stride; |
675 | | // even lines: store values |
676 | | // odd lines: average with previous values |
677 | 0 | WebPConvertARGBToUV(src, u, v, width, !(y_pos & 1)); |
678 | 0 | } |
679 | | // Lastly, store alpha if needed. |
680 | 0 | if (buf->a != NULL) { |
681 | 0 | uint8_t* const a = buf->a + y_pos * buf->a_stride; |
682 | | #if defined(WORDS_BIGENDIAN) |
683 | | WebPExtractAlpha((uint8_t*)src + 0, 0, width, 1, a, 0); |
684 | | #else |
685 | 0 | WebPExtractAlpha((uint8_t*)src + 3, 0, width, 1, a, 0); |
686 | 0 | #endif |
687 | 0 | } |
688 | 0 | } |
689 | | |
690 | 0 | static int ExportYUVA(const VP8LDecoder* const dec, int y_pos) { |
691 | 0 | WebPRescaler* const rescaler = dec->rescaler; |
692 | 0 | uint32_t* const src = (uint32_t*)rescaler->dst; |
693 | 0 | const int dst_width = rescaler->dst_width; |
694 | 0 | int num_lines_out = 0; |
695 | 0 | while (WebPRescalerHasPendingOutput(rescaler)) { |
696 | 0 | WebPRescalerExportRow(rescaler); |
697 | 0 | WebPMultARGBRow(src, dst_width, 1); |
698 | 0 | ConvertToYUVA(src, dst_width, y_pos, dec->output_); |
699 | 0 | ++y_pos; |
700 | 0 | ++num_lines_out; |
701 | 0 | } |
702 | 0 | return num_lines_out; |
703 | 0 | } |
704 | | |
705 | | static int EmitRescaledRowsYUVA(const VP8LDecoder* const dec, |
706 | 0 | uint8_t* in, int in_stride, int mb_h) { |
707 | 0 | int num_lines_in = 0; |
708 | 0 | int y_pos = dec->last_out_row_; |
709 | 0 | while (num_lines_in < mb_h) { |
710 | 0 | const int lines_left = mb_h - num_lines_in; |
711 | 0 | const int needed_lines = WebPRescaleNeededLines(dec->rescaler, lines_left); |
712 | 0 | int lines_imported; |
713 | 0 | WebPMultARGBRows(in, in_stride, dec->rescaler->src_width, needed_lines, 0); |
714 | 0 | lines_imported = |
715 | 0 | WebPRescalerImport(dec->rescaler, lines_left, in, in_stride); |
716 | 0 | assert(lines_imported == needed_lines); |
717 | 0 | num_lines_in += lines_imported; |
718 | 0 | in += needed_lines * in_stride; |
719 | 0 | y_pos += ExportYUVA(dec, y_pos); |
720 | 0 | } |
721 | 0 | return y_pos; |
722 | 0 | } |
723 | | |
724 | | static int EmitRowsYUVA(const VP8LDecoder* const dec, |
725 | | const uint8_t* in, int in_stride, |
726 | 0 | int mb_w, int num_rows) { |
727 | 0 | int y_pos = dec->last_out_row_; |
728 | 0 | while (num_rows-- > 0) { |
729 | 0 | ConvertToYUVA((const uint32_t*)in, mb_w, y_pos, dec->output_); |
730 | 0 | in += in_stride; |
731 | 0 | ++y_pos; |
732 | 0 | } |
733 | 0 | return y_pos; |
734 | 0 | } |
735 | | |
736 | | //------------------------------------------------------------------------------ |
737 | | // Cropping. |
738 | | |
739 | | // Sets io->mb_y, io->mb_h & io->mb_w according to start row, end row and |
740 | | // crop options. Also updates the input data pointer, so that it points to the |
741 | | // start of the cropped window. Note that pixels are in ARGB format even if |
742 | | // 'in_data' is uint8_t*. |
743 | | // Returns true if the crop window is not empty. |
744 | | static int SetCropWindow(VP8Io* const io, int y_start, int y_end, |
745 | 0 | uint8_t** const in_data, int pixel_stride) { |
746 | 0 | assert(y_start < y_end); |
747 | 0 | assert(io->crop_left < io->crop_right); |
748 | 0 | if (y_end > io->crop_bottom) { |
749 | 0 | y_end = io->crop_bottom; // make sure we don't overflow on last row. |
750 | 0 | } |
751 | 0 | if (y_start < io->crop_top) { |
752 | 0 | const int delta = io->crop_top - y_start; |
753 | 0 | y_start = io->crop_top; |
754 | 0 | *in_data += delta * pixel_stride; |
755 | 0 | } |
756 | 0 | if (y_start >= y_end) return 0; // Crop window is empty. |
757 | | |
758 | 0 | *in_data += io->crop_left * sizeof(uint32_t); |
759 | |
|
760 | 0 | io->mb_y = y_start - io->crop_top; |
761 | 0 | io->mb_w = io->crop_right - io->crop_left; |
762 | 0 | io->mb_h = y_end - y_start; |
763 | 0 | return 1; // Non-empty crop window. |
764 | 0 | } |
765 | | |
766 | | //------------------------------------------------------------------------------ |
767 | | |
768 | | static WEBP_INLINE int GetMetaIndex( |
769 | 0 | const uint32_t* const image, int xsize, int bits, int x, int y) { |
770 | 0 | if (bits == 0) return 0; |
771 | 0 | return image[xsize * (y >> bits) + (x >> bits)]; |
772 | 0 | } |
773 | | |
774 | | static WEBP_INLINE HTreeGroup* GetHtreeGroupForPos(VP8LMetadata* const hdr, |
775 | 0 | int x, int y) { |
776 | 0 | const int meta_index = GetMetaIndex(hdr->huffman_image_, hdr->huffman_xsize_, |
777 | 0 | hdr->huffman_subsample_bits_, x, y); |
778 | 0 | assert(meta_index < hdr->num_htree_groups_); |
779 | 0 | return hdr->htree_groups_ + meta_index; |
780 | 0 | } |
781 | | |
782 | | //------------------------------------------------------------------------------ |
783 | | // Main loop, with custom row-processing function |
784 | | |
785 | | typedef void (*ProcessRowsFunc)(VP8LDecoder* const dec, int row); |
786 | | |
787 | | static void ApplyInverseTransforms(VP8LDecoder* const dec, |
788 | | int start_row, int num_rows, |
789 | 0 | const uint32_t* const rows) { |
790 | 0 | int n = dec->next_transform_; |
791 | 0 | const int cache_pixs = dec->width_ * num_rows; |
792 | 0 | const int end_row = start_row + num_rows; |
793 | 0 | const uint32_t* rows_in = rows; |
794 | 0 | uint32_t* const rows_out = dec->argb_cache_; |
795 | | |
796 | | // Inverse transforms. |
797 | 0 | while (n-- > 0) { |
798 | 0 | VP8LTransform* const transform = &dec->transforms_[n]; |
799 | 0 | VP8LInverseTransform(transform, start_row, end_row, rows_in, rows_out); |
800 | 0 | rows_in = rows_out; |
801 | 0 | } |
802 | 0 | if (rows_in != rows_out) { |
803 | | // No transform called, hence just copy. |
804 | 0 | memcpy(rows_out, rows_in, cache_pixs * sizeof(*rows_out)); |
805 | 0 | } |
806 | 0 | } |
807 | | |
808 | | // Processes (transforms, scales & color-converts) the rows decoded after the |
809 | | // last call. |
810 | 0 | static void ProcessRows(VP8LDecoder* const dec, int row) { |
811 | 0 | const uint32_t* const rows = dec->pixels_ + dec->width_ * dec->last_row_; |
812 | 0 | const int num_rows = row - dec->last_row_; |
813 | |
|
814 | 0 | assert(row <= dec->io_->crop_bottom); |
815 | | // We can't process more than NUM_ARGB_CACHE_ROWS at a time (that's the size |
816 | | // of argb_cache_), but we currently don't need more than that. |
817 | 0 | assert(num_rows <= NUM_ARGB_CACHE_ROWS); |
818 | 0 | if (num_rows > 0) { // Emit output. |
819 | 0 | VP8Io* const io = dec->io_; |
820 | 0 | uint8_t* rows_data = (uint8_t*)dec->argb_cache_; |
821 | 0 | const int in_stride = io->width * sizeof(uint32_t); // in unit of RGBA |
822 | 0 | ApplyInverseTransforms(dec, dec->last_row_, num_rows, rows); |
823 | 0 | if (!SetCropWindow(io, dec->last_row_, row, &rows_data, in_stride)) { |
824 | | // Nothing to output (this time). |
825 | 0 | } else { |
826 | 0 | const WebPDecBuffer* const output = dec->output_; |
827 | 0 | if (WebPIsRGBMode(output->colorspace)) { // convert to RGBA |
828 | 0 | const WebPRGBABuffer* const buf = &output->u.RGBA; |
829 | 0 | uint8_t* const rgba = |
830 | 0 | buf->rgba + (int64_t)dec->last_out_row_ * buf->stride; |
831 | 0 | const int num_rows_out = |
832 | 0 | #if !defined(WEBP_REDUCE_SIZE) |
833 | 0 | io->use_scaling ? |
834 | 0 | EmitRescaledRowsRGBA(dec, rows_data, in_stride, io->mb_h, |
835 | 0 | rgba, buf->stride) : |
836 | 0 | #endif // WEBP_REDUCE_SIZE |
837 | 0 | EmitRows(output->colorspace, rows_data, in_stride, |
838 | 0 | io->mb_w, io->mb_h, rgba, buf->stride); |
839 | | // Update 'last_out_row_'. |
840 | 0 | dec->last_out_row_ += num_rows_out; |
841 | 0 | } else { // convert to YUVA |
842 | 0 | dec->last_out_row_ = io->use_scaling ? |
843 | 0 | EmitRescaledRowsYUVA(dec, rows_data, in_stride, io->mb_h) : |
844 | 0 | EmitRowsYUVA(dec, rows_data, in_stride, io->mb_w, io->mb_h); |
845 | 0 | } |
846 | 0 | assert(dec->last_out_row_ <= output->height); |
847 | 0 | } |
848 | 0 | } |
849 | | |
850 | | // Update 'last_row_'. |
851 | 0 | dec->last_row_ = row; |
852 | 0 | assert(dec->last_row_ <= dec->height_); |
853 | 0 | } |
854 | | |
855 | | // Row-processing for the special case when alpha data contains only one |
856 | | // transform (color indexing), and trivial non-green literals. |
857 | 0 | static int Is8bOptimizable(const VP8LMetadata* const hdr) { |
858 | 0 | int i; |
859 | 0 | if (hdr->color_cache_size_ > 0) return 0; |
860 | | // When the Huffman tree contains only one symbol, we can skip the |
861 | | // call to ReadSymbol() for red/blue/alpha channels. |
862 | 0 | for (i = 0; i < hdr->num_htree_groups_; ++i) { |
863 | 0 | HuffmanCode** const htrees = hdr->htree_groups_[i].htrees; |
864 | 0 | if (htrees[RED][0].bits > 0) return 0; |
865 | 0 | if (htrees[BLUE][0].bits > 0) return 0; |
866 | 0 | if (htrees[ALPHA][0].bits > 0) return 0; |
867 | 0 | } |
868 | 0 | return 1; |
869 | 0 | } |
870 | | |
871 | | static void AlphaApplyFilter(ALPHDecoder* const alph_dec, |
872 | | int first_row, int last_row, |
873 | 0 | uint8_t* out, int stride) { |
874 | 0 | if (alph_dec->filter_ != WEBP_FILTER_NONE) { |
875 | 0 | int y; |
876 | 0 | const uint8_t* prev_line = alph_dec->prev_line_; |
877 | 0 | assert(WebPUnfilters[alph_dec->filter_] != NULL); |
878 | 0 | for (y = first_row; y < last_row; ++y) { |
879 | 0 | WebPUnfilters[alph_dec->filter_](prev_line, out, out, stride); |
880 | 0 | prev_line = out; |
881 | 0 | out += stride; |
882 | 0 | } |
883 | 0 | alph_dec->prev_line_ = prev_line; |
884 | 0 | } |
885 | 0 | } |
886 | | |
887 | 0 | static void ExtractPalettedAlphaRows(VP8LDecoder* const dec, int last_row) { |
888 | | // For vertical and gradient filtering, we need to decode the part above the |
889 | | // crop_top row, in order to have the correct spatial predictors. |
890 | 0 | ALPHDecoder* const alph_dec = (ALPHDecoder*)dec->io_->opaque; |
891 | 0 | const int top_row = |
892 | 0 | (alph_dec->filter_ == WEBP_FILTER_NONE || |
893 | 0 | alph_dec->filter_ == WEBP_FILTER_HORIZONTAL) ? dec->io_->crop_top |
894 | 0 | : dec->last_row_; |
895 | 0 | const int first_row = (dec->last_row_ < top_row) ? top_row : dec->last_row_; |
896 | 0 | assert(last_row <= dec->io_->crop_bottom); |
897 | 0 | if (last_row > first_row) { |
898 | | // Special method for paletted alpha data. We only process the cropped area. |
899 | 0 | const int width = dec->io_->width; |
900 | 0 | uint8_t* out = alph_dec->output_ + width * first_row; |
901 | 0 | const uint8_t* const in = |
902 | 0 | (uint8_t*)dec->pixels_ + dec->width_ * first_row; |
903 | 0 | VP8LTransform* const transform = &dec->transforms_[0]; |
904 | 0 | assert(dec->next_transform_ == 1); |
905 | 0 | assert(transform->type_ == COLOR_INDEXING_TRANSFORM); |
906 | 0 | VP8LColorIndexInverseTransformAlpha(transform, first_row, last_row, |
907 | 0 | in, out); |
908 | 0 | AlphaApplyFilter(alph_dec, first_row, last_row, out, width); |
909 | 0 | } |
910 | 0 | dec->last_row_ = dec->last_out_row_ = last_row; |
911 | 0 | } |
912 | | |
913 | | //------------------------------------------------------------------------------ |
914 | | // Helper functions for fast pattern copy (8b and 32b) |
915 | | |
916 | | // cyclic rotation of pattern word |
917 | 0 | static WEBP_INLINE uint32_t Rotate8b(uint32_t V) { |
918 | | #if defined(WORDS_BIGENDIAN) |
919 | | return ((V & 0xff000000u) >> 24) | (V << 8); |
920 | | #else |
921 | 0 | return ((V & 0xffu) << 24) | (V >> 8); |
922 | 0 | #endif |
923 | 0 | } |
924 | | |
925 | | // copy 1, 2 or 4-bytes pattern |
926 | | static WEBP_INLINE void CopySmallPattern8b(const uint8_t* src, uint8_t* dst, |
927 | 0 | int length, uint32_t pattern) { |
928 | 0 | int i; |
929 | | // align 'dst' to 4-bytes boundary. Adjust the pattern along the way. |
930 | 0 | while ((uintptr_t)dst & 3) { |
931 | 0 | *dst++ = *src++; |
932 | 0 | pattern = Rotate8b(pattern); |
933 | 0 | --length; |
934 | 0 | } |
935 | | // Copy the pattern 4 bytes at a time. |
936 | 0 | for (i = 0; i < (length >> 2); ++i) { |
937 | 0 | ((uint32_t*)dst)[i] = pattern; |
938 | 0 | } |
939 | | // Finish with left-overs. 'pattern' is still correctly positioned, |
940 | | // so no Rotate8b() call is needed. |
941 | 0 | for (i <<= 2; i < length; ++i) { |
942 | 0 | dst[i] = src[i]; |
943 | 0 | } |
944 | 0 | } |
945 | | |
946 | 0 | static WEBP_INLINE void CopyBlock8b(uint8_t* const dst, int dist, int length) { |
947 | 0 | const uint8_t* src = dst - dist; |
948 | 0 | if (length >= 8) { |
949 | 0 | uint32_t pattern = 0; |
950 | 0 | switch (dist) { |
951 | 0 | case 1: |
952 | 0 | pattern = src[0]; |
953 | | #if defined(__arm__) || defined(_M_ARM) // arm doesn't like multiply that much |
954 | | pattern |= pattern << 8; |
955 | | pattern |= pattern << 16; |
956 | | #elif defined(WEBP_USE_MIPS_DSP_R2) |
957 | | __asm__ volatile ("replv.qb %0, %0" : "+r"(pattern)); |
958 | | #else |
959 | 0 | pattern = 0x01010101u * pattern; |
960 | 0 | #endif |
961 | 0 | break; |
962 | 0 | case 2: |
963 | 0 | #if !defined(WORDS_BIGENDIAN) |
964 | 0 | memcpy(&pattern, src, sizeof(uint16_t)); |
965 | | #else |
966 | | pattern = ((uint32_t)src[0] << 8) | src[1]; |
967 | | #endif |
968 | | #if defined(__arm__) || defined(_M_ARM) |
969 | | pattern |= pattern << 16; |
970 | | #elif defined(WEBP_USE_MIPS_DSP_R2) |
971 | | __asm__ volatile ("replv.ph %0, %0" : "+r"(pattern)); |
972 | | #else |
973 | 0 | pattern = 0x00010001u * pattern; |
974 | 0 | #endif |
975 | 0 | break; |
976 | 0 | case 4: |
977 | 0 | memcpy(&pattern, src, sizeof(uint32_t)); |
978 | 0 | break; |
979 | 0 | default: |
980 | 0 | goto Copy; |
981 | 0 | } |
982 | 0 | CopySmallPattern8b(src, dst, length, pattern); |
983 | 0 | return; |
984 | 0 | } |
985 | 0 | Copy: |
986 | 0 | if (dist >= length) { // no overlap -> use memcpy() |
987 | 0 | memcpy(dst, src, length * sizeof(*dst)); |
988 | 0 | } else { |
989 | 0 | int i; |
990 | 0 | for (i = 0; i < length; ++i) dst[i] = src[i]; |
991 | 0 | } |
992 | 0 | } |
993 | | |
994 | | // copy pattern of 1 or 2 uint32_t's |
995 | | static WEBP_INLINE void CopySmallPattern32b(const uint32_t* src, |
996 | | uint32_t* dst, |
997 | 0 | int length, uint64_t pattern) { |
998 | 0 | int i; |
999 | 0 | if ((uintptr_t)dst & 4) { // Align 'dst' to 8-bytes boundary. |
1000 | 0 | *dst++ = *src++; |
1001 | 0 | pattern = (pattern >> 32) | (pattern << 32); |
1002 | 0 | --length; |
1003 | 0 | } |
1004 | 0 | assert(0 == ((uintptr_t)dst & 7)); |
1005 | 0 | for (i = 0; i < (length >> 1); ++i) { |
1006 | 0 | ((uint64_t*)dst)[i] = pattern; // Copy the pattern 8 bytes at a time. |
1007 | 0 | } |
1008 | 0 | if (length & 1) { // Finish with left-over. |
1009 | 0 | dst[i << 1] = src[i << 1]; |
1010 | 0 | } |
1011 | 0 | } |
1012 | | |
1013 | | static WEBP_INLINE void CopyBlock32b(uint32_t* const dst, |
1014 | 0 | int dist, int length) { |
1015 | 0 | const uint32_t* const src = dst - dist; |
1016 | 0 | if (dist <= 2 && length >= 4 && ((uintptr_t)dst & 3) == 0) { |
1017 | 0 | uint64_t pattern; |
1018 | 0 | if (dist == 1) { |
1019 | 0 | pattern = (uint64_t)src[0]; |
1020 | 0 | pattern |= pattern << 32; |
1021 | 0 | } else { |
1022 | 0 | memcpy(&pattern, src, sizeof(pattern)); |
1023 | 0 | } |
1024 | 0 | CopySmallPattern32b(src, dst, length, pattern); |
1025 | 0 | } else if (dist >= length) { // no overlap |
1026 | 0 | memcpy(dst, src, length * sizeof(*dst)); |
1027 | 0 | } else { |
1028 | 0 | int i; |
1029 | 0 | for (i = 0; i < length; ++i) dst[i] = src[i]; |
1030 | 0 | } |
1031 | 0 | } |
1032 | | |
1033 | | //------------------------------------------------------------------------------ |
1034 | | |
1035 | | static int DecodeAlphaData(VP8LDecoder* const dec, uint8_t* const data, |
1036 | 0 | int width, int height, int last_row) { |
1037 | 0 | int ok = 1; |
1038 | 0 | int row = dec->last_pixel_ / width; |
1039 | 0 | int col = dec->last_pixel_ % width; |
1040 | 0 | VP8LBitReader* const br = &dec->br_; |
1041 | 0 | VP8LMetadata* const hdr = &dec->hdr_; |
1042 | 0 | int pos = dec->last_pixel_; // current position |
1043 | 0 | const int end = width * height; // End of data |
1044 | 0 | const int last = width * last_row; // Last pixel to decode |
1045 | 0 | const int len_code_limit = NUM_LITERAL_CODES + NUM_LENGTH_CODES; |
1046 | 0 | const int mask = hdr->huffman_mask_; |
1047 | 0 | const HTreeGroup* htree_group = |
1048 | 0 | (pos < last) ? GetHtreeGroupForPos(hdr, col, row) : NULL; |
1049 | 0 | assert(pos <= end); |
1050 | 0 | assert(last_row <= height); |
1051 | 0 | assert(Is8bOptimizable(hdr)); |
1052 | | |
1053 | 0 | while (!br->eos_ && pos < last) { |
1054 | 0 | int code; |
1055 | | // Only update when changing tile. |
1056 | 0 | if ((col & mask) == 0) { |
1057 | 0 | htree_group = GetHtreeGroupForPos(hdr, col, row); |
1058 | 0 | } |
1059 | 0 | assert(htree_group != NULL); |
1060 | 0 | VP8LFillBitWindow(br); |
1061 | 0 | code = ReadSymbol(htree_group->htrees[GREEN], br); |
1062 | 0 | if (code < NUM_LITERAL_CODES) { // Literal |
1063 | 0 | data[pos] = code; |
1064 | 0 | ++pos; |
1065 | 0 | ++col; |
1066 | 0 | if (col >= width) { |
1067 | 0 | col = 0; |
1068 | 0 | ++row; |
1069 | 0 | if (row <= last_row && (row % NUM_ARGB_CACHE_ROWS == 0)) { |
1070 | 0 | ExtractPalettedAlphaRows(dec, row); |
1071 | 0 | } |
1072 | 0 | } |
1073 | 0 | } else if (code < len_code_limit) { // Backward reference |
1074 | 0 | int dist_code, dist; |
1075 | 0 | const int length_sym = code - NUM_LITERAL_CODES; |
1076 | 0 | const int length = GetCopyLength(length_sym, br); |
1077 | 0 | const int dist_symbol = ReadSymbol(htree_group->htrees[DIST], br); |
1078 | 0 | VP8LFillBitWindow(br); |
1079 | 0 | dist_code = GetCopyDistance(dist_symbol, br); |
1080 | 0 | dist = PlaneCodeToDistance(width, dist_code); |
1081 | 0 | if (pos >= dist && end - pos >= length) { |
1082 | 0 | CopyBlock8b(data + pos, dist, length); |
1083 | 0 | } else { |
1084 | 0 | ok = 0; |
1085 | 0 | goto End; |
1086 | 0 | } |
1087 | 0 | pos += length; |
1088 | 0 | col += length; |
1089 | 0 | while (col >= width) { |
1090 | 0 | col -= width; |
1091 | 0 | ++row; |
1092 | 0 | if (row <= last_row && (row % NUM_ARGB_CACHE_ROWS == 0)) { |
1093 | 0 | ExtractPalettedAlphaRows(dec, row); |
1094 | 0 | } |
1095 | 0 | } |
1096 | 0 | if (pos < last && (col & mask)) { |
1097 | 0 | htree_group = GetHtreeGroupForPos(hdr, col, row); |
1098 | 0 | } |
1099 | 0 | } else { // Not reached |
1100 | 0 | ok = 0; |
1101 | 0 | goto End; |
1102 | 0 | } |
1103 | 0 | br->eos_ = VP8LIsEndOfStream(br); |
1104 | 0 | } |
1105 | | // Process the remaining rows corresponding to last row-block. |
1106 | 0 | ExtractPalettedAlphaRows(dec, row > last_row ? last_row : row); |
1107 | |
|
1108 | 0 | End: |
1109 | 0 | br->eos_ = VP8LIsEndOfStream(br); |
1110 | 0 | if (!ok || (br->eos_ && pos < end)) { |
1111 | 0 | return VP8LSetError( |
1112 | 0 | dec, br->eos_ ? VP8_STATUS_SUSPENDED : VP8_STATUS_BITSTREAM_ERROR); |
1113 | 0 | } |
1114 | 0 | dec->last_pixel_ = pos; |
1115 | 0 | return ok; |
1116 | 0 | } |
1117 | | |
1118 | 0 | static void SaveState(VP8LDecoder* const dec, int last_pixel) { |
1119 | 0 | assert(dec->incremental_); |
1120 | 0 | dec->saved_br_ = dec->br_; |
1121 | 0 | dec->saved_last_pixel_ = last_pixel; |
1122 | 0 | if (dec->hdr_.color_cache_size_ > 0) { |
1123 | 0 | VP8LColorCacheCopy(&dec->hdr_.color_cache_, &dec->hdr_.saved_color_cache_); |
1124 | 0 | } |
1125 | 0 | } |
1126 | | |
1127 | 0 | static void RestoreState(VP8LDecoder* const dec) { |
1128 | 0 | assert(dec->br_.eos_); |
1129 | 0 | dec->status_ = VP8_STATUS_SUSPENDED; |
1130 | 0 | dec->br_ = dec->saved_br_; |
1131 | 0 | dec->last_pixel_ = dec->saved_last_pixel_; |
1132 | 0 | if (dec->hdr_.color_cache_size_ > 0) { |
1133 | 0 | VP8LColorCacheCopy(&dec->hdr_.saved_color_cache_, &dec->hdr_.color_cache_); |
1134 | 0 | } |
1135 | 0 | } |
1136 | | |
1137 | 0 | #define SYNC_EVERY_N_ROWS 8 // minimum number of rows between check-points |
1138 | | static int DecodeImageData(VP8LDecoder* const dec, uint32_t* const data, |
1139 | | int width, int height, int last_row, |
1140 | 0 | ProcessRowsFunc process_func) { |
1141 | 0 | int row = dec->last_pixel_ / width; |
1142 | 0 | int col = dec->last_pixel_ % width; |
1143 | 0 | VP8LBitReader* const br = &dec->br_; |
1144 | 0 | VP8LMetadata* const hdr = &dec->hdr_; |
1145 | 0 | uint32_t* src = data + dec->last_pixel_; |
1146 | 0 | uint32_t* last_cached = src; |
1147 | 0 | uint32_t* const src_end = data + width * height; // End of data |
1148 | 0 | uint32_t* const src_last = data + width * last_row; // Last pixel to decode |
1149 | 0 | const int len_code_limit = NUM_LITERAL_CODES + NUM_LENGTH_CODES; |
1150 | 0 | const int color_cache_limit = len_code_limit + hdr->color_cache_size_; |
1151 | 0 | int next_sync_row = dec->incremental_ ? row : 1 << 24; |
1152 | 0 | VP8LColorCache* const color_cache = |
1153 | 0 | (hdr->color_cache_size_ > 0) ? &hdr->color_cache_ : NULL; |
1154 | 0 | const int mask = hdr->huffman_mask_; |
1155 | 0 | const HTreeGroup* htree_group = |
1156 | 0 | (src < src_last) ? GetHtreeGroupForPos(hdr, col, row) : NULL; |
1157 | 0 | assert(dec->last_row_ < last_row); |
1158 | 0 | assert(src_last <= src_end); |
1159 | | |
1160 | 0 | while (src < src_last) { |
1161 | 0 | int code; |
1162 | 0 | if (row >= next_sync_row) { |
1163 | 0 | SaveState(dec, (int)(src - data)); |
1164 | 0 | next_sync_row = row + SYNC_EVERY_N_ROWS; |
1165 | 0 | } |
1166 | | // Only update when changing tile. Note we could use this test: |
1167 | | // if "((((prev_col ^ col) | prev_row ^ row)) > mask)" -> tile changed |
1168 | | // but that's actually slower and needs storing the previous col/row. |
1169 | 0 | if ((col & mask) == 0) { |
1170 | 0 | htree_group = GetHtreeGroupForPos(hdr, col, row); |
1171 | 0 | } |
1172 | 0 | assert(htree_group != NULL); |
1173 | 0 | if (htree_group->is_trivial_code) { |
1174 | 0 | *src = htree_group->literal_arb; |
1175 | 0 | goto AdvanceByOne; |
1176 | 0 | } |
1177 | 0 | VP8LFillBitWindow(br); |
1178 | 0 | if (htree_group->use_packed_table) { |
1179 | 0 | code = ReadPackedSymbols(htree_group, br, src); |
1180 | 0 | if (VP8LIsEndOfStream(br)) break; |
1181 | 0 | if (code == PACKED_NON_LITERAL_CODE) goto AdvanceByOne; |
1182 | 0 | } else { |
1183 | 0 | code = ReadSymbol(htree_group->htrees[GREEN], br); |
1184 | 0 | } |
1185 | 0 | if (VP8LIsEndOfStream(br)) break; |
1186 | 0 | if (code < NUM_LITERAL_CODES) { // Literal |
1187 | 0 | if (htree_group->is_trivial_literal) { |
1188 | 0 | *src = htree_group->literal_arb | (code << 8); |
1189 | 0 | } else { |
1190 | 0 | int red, blue, alpha; |
1191 | 0 | red = ReadSymbol(htree_group->htrees[RED], br); |
1192 | 0 | VP8LFillBitWindow(br); |
1193 | 0 | blue = ReadSymbol(htree_group->htrees[BLUE], br); |
1194 | 0 | alpha = ReadSymbol(htree_group->htrees[ALPHA], br); |
1195 | 0 | if (VP8LIsEndOfStream(br)) break; |
1196 | 0 | *src = ((uint32_t)alpha << 24) | (red << 16) | (code << 8) | blue; |
1197 | 0 | } |
1198 | 0 | AdvanceByOne: |
1199 | 0 | ++src; |
1200 | 0 | ++col; |
1201 | 0 | if (col >= width) { |
1202 | 0 | col = 0; |
1203 | 0 | ++row; |
1204 | 0 | if (process_func != NULL) { |
1205 | 0 | if (row <= last_row && (row % NUM_ARGB_CACHE_ROWS == 0)) { |
1206 | 0 | process_func(dec, row); |
1207 | 0 | } |
1208 | 0 | } |
1209 | 0 | if (color_cache != NULL) { |
1210 | 0 | while (last_cached < src) { |
1211 | 0 | VP8LColorCacheInsert(color_cache, *last_cached++); |
1212 | 0 | } |
1213 | 0 | } |
1214 | 0 | } |
1215 | 0 | } else if (code < len_code_limit) { // Backward reference |
1216 | 0 | int dist_code, dist; |
1217 | 0 | const int length_sym = code - NUM_LITERAL_CODES; |
1218 | 0 | const int length = GetCopyLength(length_sym, br); |
1219 | 0 | const int dist_symbol = ReadSymbol(htree_group->htrees[DIST], br); |
1220 | 0 | VP8LFillBitWindow(br); |
1221 | 0 | dist_code = GetCopyDistance(dist_symbol, br); |
1222 | 0 | dist = PlaneCodeToDistance(width, dist_code); |
1223 | |
|
1224 | 0 | if (VP8LIsEndOfStream(br)) break; |
1225 | 0 | if (src - data < (ptrdiff_t)dist || src_end - src < (ptrdiff_t)length) { |
1226 | 0 | goto Error; |
1227 | 0 | } else { |
1228 | 0 | CopyBlock32b(src, dist, length); |
1229 | 0 | } |
1230 | 0 | src += length; |
1231 | 0 | col += length; |
1232 | 0 | while (col >= width) { |
1233 | 0 | col -= width; |
1234 | 0 | ++row; |
1235 | 0 | if (process_func != NULL) { |
1236 | 0 | if (row <= last_row && (row % NUM_ARGB_CACHE_ROWS == 0)) { |
1237 | 0 | process_func(dec, row); |
1238 | 0 | } |
1239 | 0 | } |
1240 | 0 | } |
1241 | | // Because of the check done above (before 'src' was incremented by |
1242 | | // 'length'), the following holds true. |
1243 | 0 | assert(src <= src_end); |
1244 | 0 | if (col & mask) htree_group = GetHtreeGroupForPos(hdr, col, row); |
1245 | 0 | if (color_cache != NULL) { |
1246 | 0 | while (last_cached < src) { |
1247 | 0 | VP8LColorCacheInsert(color_cache, *last_cached++); |
1248 | 0 | } |
1249 | 0 | } |
1250 | 0 | } else if (code < color_cache_limit) { // Color cache |
1251 | 0 | const int key = code - len_code_limit; |
1252 | 0 | assert(color_cache != NULL); |
1253 | 0 | while (last_cached < src) { |
1254 | 0 | VP8LColorCacheInsert(color_cache, *last_cached++); |
1255 | 0 | } |
1256 | 0 | *src = VP8LColorCacheLookup(color_cache, key); |
1257 | 0 | goto AdvanceByOne; |
1258 | 0 | } else { // Not reached |
1259 | 0 | goto Error; |
1260 | 0 | } |
1261 | 0 | } |
1262 | | |
1263 | 0 | br->eos_ = VP8LIsEndOfStream(br); |
1264 | | // In incremental decoding: |
1265 | | // br->eos_ && src < src_last: if 'br' reached the end of the buffer and |
1266 | | // 'src_last' has not been reached yet, there is not enough data. 'dec' has to |
1267 | | // be reset until there is more data. |
1268 | | // !br->eos_ && src < src_last: this cannot happen as either the buffer is |
1269 | | // fully read, either enough has been read to reach 'src_last'. |
1270 | | // src >= src_last: 'src_last' is reached, all is fine. 'src' can actually go |
1271 | | // beyond 'src_last' in case the image is cropped and an LZ77 goes further. |
1272 | | // The buffer might have been enough or there is some left. 'br->eos_' does |
1273 | | // not matter. |
1274 | 0 | assert(!dec->incremental_ || (br->eos_ && src < src_last) || src >= src_last); |
1275 | 0 | if (dec->incremental_ && br->eos_ && src < src_last) { |
1276 | 0 | RestoreState(dec); |
1277 | 0 | } else if ((dec->incremental_ && src >= src_last) || !br->eos_) { |
1278 | | // Process the remaining rows corresponding to last row-block. |
1279 | 0 | if (process_func != NULL) { |
1280 | 0 | process_func(dec, row > last_row ? last_row : row); |
1281 | 0 | } |
1282 | 0 | dec->status_ = VP8_STATUS_OK; |
1283 | 0 | dec->last_pixel_ = (int)(src - data); // end-of-scan marker |
1284 | 0 | } else { |
1285 | | // if not incremental, and we are past the end of buffer (eos_=1), then this |
1286 | | // is a real bitstream error. |
1287 | 0 | goto Error; |
1288 | 0 | } |
1289 | 0 | return 1; |
1290 | | |
1291 | 0 | Error: |
1292 | 0 | return VP8LSetError(dec, VP8_STATUS_BITSTREAM_ERROR); |
1293 | 0 | } |
1294 | | |
1295 | | // ----------------------------------------------------------------------------- |
1296 | | // VP8LTransform |
1297 | | |
1298 | 0 | static void ClearTransform(VP8LTransform* const transform) { |
1299 | 0 | WebPSafeFree(transform->data_); |
1300 | 0 | transform->data_ = NULL; |
1301 | 0 | } |
1302 | | |
1303 | | // For security reason, we need to remap the color map to span |
1304 | | // the total possible bundled values, and not just the num_colors. |
1305 | 0 | static int ExpandColorMap(int num_colors, VP8LTransform* const transform) { |
1306 | 0 | int i; |
1307 | 0 | const int final_num_colors = 1 << (8 >> transform->bits_); |
1308 | 0 | uint32_t* const new_color_map = |
1309 | 0 | (uint32_t*)WebPSafeMalloc((uint64_t)final_num_colors, |
1310 | 0 | sizeof(*new_color_map)); |
1311 | 0 | if (new_color_map == NULL) { |
1312 | 0 | return 0; |
1313 | 0 | } else { |
1314 | 0 | uint8_t* const data = (uint8_t*)transform->data_; |
1315 | 0 | uint8_t* const new_data = (uint8_t*)new_color_map; |
1316 | 0 | new_color_map[0] = transform->data_[0]; |
1317 | 0 | for (i = 4; i < 4 * num_colors; ++i) { |
1318 | | // Equivalent to VP8LAddPixels(), on a byte-basis. |
1319 | 0 | new_data[i] = (data[i] + new_data[i - 4]) & 0xff; |
1320 | 0 | } |
1321 | 0 | for (; i < 4 * final_num_colors; ++i) { |
1322 | 0 | new_data[i] = 0; // black tail. |
1323 | 0 | } |
1324 | 0 | WebPSafeFree(transform->data_); |
1325 | 0 | transform->data_ = new_color_map; |
1326 | 0 | } |
1327 | 0 | return 1; |
1328 | 0 | } |
1329 | | |
1330 | | static int ReadTransform(int* const xsize, int const* ysize, |
1331 | 0 | VP8LDecoder* const dec) { |
1332 | 0 | int ok = 1; |
1333 | 0 | VP8LBitReader* const br = &dec->br_; |
1334 | 0 | VP8LTransform* transform = &dec->transforms_[dec->next_transform_]; |
1335 | 0 | const VP8LImageTransformType type = |
1336 | 0 | (VP8LImageTransformType)VP8LReadBits(br, 2); |
1337 | | |
1338 | | // Each transform type can only be present once in the stream. |
1339 | 0 | if (dec->transforms_seen_ & (1U << type)) { |
1340 | 0 | return 0; // Already there, let's not accept the second same transform. |
1341 | 0 | } |
1342 | 0 | dec->transforms_seen_ |= (1U << type); |
1343 | |
|
1344 | 0 | transform->type_ = type; |
1345 | 0 | transform->xsize_ = *xsize; |
1346 | 0 | transform->ysize_ = *ysize; |
1347 | 0 | transform->data_ = NULL; |
1348 | 0 | ++dec->next_transform_; |
1349 | 0 | assert(dec->next_transform_ <= NUM_TRANSFORMS); |
1350 | | |
1351 | 0 | switch (type) { |
1352 | 0 | case PREDICTOR_TRANSFORM: |
1353 | 0 | case CROSS_COLOR_TRANSFORM: |
1354 | 0 | transform->bits_ = |
1355 | 0 | MIN_TRANSFORM_BITS + VP8LReadBits(br, NUM_TRANSFORM_BITS); |
1356 | 0 | ok = DecodeImageStream(VP8LSubSampleSize(transform->xsize_, |
1357 | 0 | transform->bits_), |
1358 | 0 | VP8LSubSampleSize(transform->ysize_, |
1359 | 0 | transform->bits_), |
1360 | 0 | /*is_level0=*/0, dec, &transform->data_); |
1361 | 0 | break; |
1362 | 0 | case COLOR_INDEXING_TRANSFORM: { |
1363 | 0 | const int num_colors = VP8LReadBits(br, 8) + 1; |
1364 | 0 | const int bits = (num_colors > 16) ? 0 |
1365 | 0 | : (num_colors > 4) ? 1 |
1366 | 0 | : (num_colors > 2) ? 2 |
1367 | 0 | : 3; |
1368 | 0 | *xsize = VP8LSubSampleSize(transform->xsize_, bits); |
1369 | 0 | transform->bits_ = bits; |
1370 | 0 | ok = DecodeImageStream(num_colors, /*ysize=*/1, /*is_level0=*/0, dec, |
1371 | 0 | &transform->data_); |
1372 | 0 | if (ok && !ExpandColorMap(num_colors, transform)) { |
1373 | 0 | return VP8LSetError(dec, VP8_STATUS_OUT_OF_MEMORY); |
1374 | 0 | } |
1375 | 0 | break; |
1376 | 0 | } |
1377 | 0 | case SUBTRACT_GREEN_TRANSFORM: |
1378 | 0 | break; |
1379 | 0 | default: |
1380 | 0 | assert(0); // can't happen |
1381 | 0 | break; |
1382 | 0 | } |
1383 | | |
1384 | 0 | return ok; |
1385 | 0 | } |
1386 | | |
1387 | | // ----------------------------------------------------------------------------- |
1388 | | // VP8LMetadata |
1389 | | |
1390 | 0 | static void InitMetadata(VP8LMetadata* const hdr) { |
1391 | 0 | assert(hdr != NULL); |
1392 | 0 | memset(hdr, 0, sizeof(*hdr)); |
1393 | 0 | } |
1394 | | |
1395 | 0 | static void ClearMetadata(VP8LMetadata* const hdr) { |
1396 | 0 | assert(hdr != NULL); |
1397 | | |
1398 | 0 | WebPSafeFree(hdr->huffman_image_); |
1399 | 0 | VP8LHuffmanTablesDeallocate(&hdr->huffman_tables_); |
1400 | 0 | VP8LHtreeGroupsFree(hdr->htree_groups_); |
1401 | 0 | VP8LColorCacheClear(&hdr->color_cache_); |
1402 | 0 | VP8LColorCacheClear(&hdr->saved_color_cache_); |
1403 | 0 | InitMetadata(hdr); |
1404 | 0 | } |
1405 | | |
1406 | | // ----------------------------------------------------------------------------- |
1407 | | // VP8LDecoder |
1408 | | |
1409 | 0 | VP8LDecoder* VP8LNew(void) { |
1410 | 0 | VP8LDecoder* const dec = (VP8LDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec)); |
1411 | 0 | if (dec == NULL) return NULL; |
1412 | 0 | dec->status_ = VP8_STATUS_OK; |
1413 | 0 | dec->state_ = READ_DIM; |
1414 | |
|
1415 | 0 | VP8LDspInit(); // Init critical function pointers. |
1416 | |
|
1417 | 0 | return dec; |
1418 | 0 | } |
1419 | | |
1420 | | // Resets the decoder in its initial state, reclaiming memory. |
1421 | | // Preserves the dec->status_ value. |
1422 | 0 | static void VP8LClear(VP8LDecoder* const dec) { |
1423 | 0 | int i; |
1424 | 0 | if (dec == NULL) return; |
1425 | 0 | ClearMetadata(&dec->hdr_); |
1426 | |
|
1427 | 0 | WebPSafeFree(dec->pixels_); |
1428 | 0 | dec->pixels_ = NULL; |
1429 | 0 | for (i = 0; i < dec->next_transform_; ++i) { |
1430 | 0 | ClearTransform(&dec->transforms_[i]); |
1431 | 0 | } |
1432 | 0 | dec->next_transform_ = 0; |
1433 | 0 | dec->transforms_seen_ = 0; |
1434 | |
|
1435 | 0 | WebPSafeFree(dec->rescaler_memory); |
1436 | 0 | dec->rescaler_memory = NULL; |
1437 | |
|
1438 | 0 | dec->output_ = NULL; // leave no trace behind |
1439 | 0 | } |
1440 | | |
1441 | 0 | void VP8LDelete(VP8LDecoder* const dec) { |
1442 | 0 | if (dec != NULL) { |
1443 | 0 | VP8LClear(dec); |
1444 | 0 | WebPSafeFree(dec); |
1445 | 0 | } |
1446 | 0 | } |
1447 | | |
1448 | 0 | static void UpdateDecoder(VP8LDecoder* const dec, int width, int height) { |
1449 | 0 | VP8LMetadata* const hdr = &dec->hdr_; |
1450 | 0 | const int num_bits = hdr->huffman_subsample_bits_; |
1451 | 0 | dec->width_ = width; |
1452 | 0 | dec->height_ = height; |
1453 | |
|
1454 | 0 | hdr->huffman_xsize_ = VP8LSubSampleSize(width, num_bits); |
1455 | 0 | hdr->huffman_mask_ = (num_bits == 0) ? ~0 : (1 << num_bits) - 1; |
1456 | 0 | } |
1457 | | |
1458 | | static int DecodeImageStream(int xsize, int ysize, |
1459 | | int is_level0, |
1460 | | VP8LDecoder* const dec, |
1461 | 0 | uint32_t** const decoded_data) { |
1462 | 0 | int ok = 1; |
1463 | 0 | int transform_xsize = xsize; |
1464 | 0 | int transform_ysize = ysize; |
1465 | 0 | VP8LBitReader* const br = &dec->br_; |
1466 | 0 | VP8LMetadata* const hdr = &dec->hdr_; |
1467 | 0 | uint32_t* data = NULL; |
1468 | 0 | int color_cache_bits = 0; |
1469 | | |
1470 | | // Read the transforms (may recurse). |
1471 | 0 | if (is_level0) { |
1472 | 0 | while (ok && VP8LReadBits(br, 1)) { |
1473 | 0 | ok = ReadTransform(&transform_xsize, &transform_ysize, dec); |
1474 | 0 | } |
1475 | 0 | } |
1476 | | |
1477 | | // Color cache |
1478 | 0 | if (ok && VP8LReadBits(br, 1)) { |
1479 | 0 | color_cache_bits = VP8LReadBits(br, 4); |
1480 | 0 | ok = (color_cache_bits >= 1 && color_cache_bits <= MAX_CACHE_BITS); |
1481 | 0 | if (!ok) { |
1482 | 0 | VP8LSetError(dec, VP8_STATUS_BITSTREAM_ERROR); |
1483 | 0 | goto End; |
1484 | 0 | } |
1485 | 0 | } |
1486 | | |
1487 | | // Read the Huffman codes (may recurse). |
1488 | 0 | ok = ok && ReadHuffmanCodes(dec, transform_xsize, transform_ysize, |
1489 | 0 | color_cache_bits, is_level0); |
1490 | 0 | if (!ok) { |
1491 | 0 | VP8LSetError(dec, VP8_STATUS_BITSTREAM_ERROR); |
1492 | 0 | goto End; |
1493 | 0 | } |
1494 | | |
1495 | | // Finish setting up the color-cache |
1496 | 0 | if (color_cache_bits > 0) { |
1497 | 0 | hdr->color_cache_size_ = 1 << color_cache_bits; |
1498 | 0 | if (!VP8LColorCacheInit(&hdr->color_cache_, color_cache_bits)) { |
1499 | 0 | ok = VP8LSetError(dec, VP8_STATUS_OUT_OF_MEMORY); |
1500 | 0 | goto End; |
1501 | 0 | } |
1502 | 0 | } else { |
1503 | 0 | hdr->color_cache_size_ = 0; |
1504 | 0 | } |
1505 | 0 | UpdateDecoder(dec, transform_xsize, transform_ysize); |
1506 | |
|
1507 | 0 | if (is_level0) { // level 0 complete |
1508 | 0 | dec->state_ = READ_HDR; |
1509 | 0 | goto End; |
1510 | 0 | } |
1511 | | |
1512 | 0 | { |
1513 | 0 | const uint64_t total_size = (uint64_t)transform_xsize * transform_ysize; |
1514 | 0 | data = (uint32_t*)WebPSafeMalloc(total_size, sizeof(*data)); |
1515 | 0 | if (data == NULL) { |
1516 | 0 | ok = VP8LSetError(dec, VP8_STATUS_OUT_OF_MEMORY); |
1517 | 0 | goto End; |
1518 | 0 | } |
1519 | 0 | } |
1520 | | |
1521 | | // Use the Huffman trees to decode the LZ77 encoded data. |
1522 | 0 | ok = DecodeImageData(dec, data, transform_xsize, transform_ysize, |
1523 | 0 | transform_ysize, NULL); |
1524 | 0 | ok = ok && !br->eos_; |
1525 | |
|
1526 | 0 | End: |
1527 | 0 | if (!ok) { |
1528 | 0 | WebPSafeFree(data); |
1529 | 0 | ClearMetadata(hdr); |
1530 | 0 | } else { |
1531 | 0 | if (decoded_data != NULL) { |
1532 | 0 | *decoded_data = data; |
1533 | 0 | } else { |
1534 | | // We allocate image data in this function only for transforms. At level 0 |
1535 | | // (that is: not the transforms), we shouldn't have allocated anything. |
1536 | 0 | assert(data == NULL); |
1537 | 0 | assert(is_level0); |
1538 | 0 | } |
1539 | 0 | dec->last_pixel_ = 0; // Reset for future DECODE_DATA_FUNC() calls. |
1540 | 0 | if (!is_level0) ClearMetadata(hdr); // Clean up temporary data behind. |
1541 | 0 | } |
1542 | 0 | return ok; |
1543 | 0 | } |
1544 | | |
1545 | | //------------------------------------------------------------------------------ |
1546 | | // Allocate internal buffers dec->pixels_ and dec->argb_cache_. |
1547 | 0 | static int AllocateInternalBuffers32b(VP8LDecoder* const dec, int final_width) { |
1548 | 0 | const uint64_t num_pixels = (uint64_t)dec->width_ * dec->height_; |
1549 | | // Scratch buffer corresponding to top-prediction row for transforming the |
1550 | | // first row in the row-blocks. Not needed for paletted alpha. |
1551 | 0 | const uint64_t cache_top_pixels = (uint16_t)final_width; |
1552 | | // Scratch buffer for temporary BGRA storage. Not needed for paletted alpha. |
1553 | 0 | const uint64_t cache_pixels = (uint64_t)final_width * NUM_ARGB_CACHE_ROWS; |
1554 | 0 | const uint64_t total_num_pixels = |
1555 | 0 | num_pixels + cache_top_pixels + cache_pixels; |
1556 | |
|
1557 | 0 | assert(dec->width_ <= final_width); |
1558 | 0 | dec->pixels_ = (uint32_t*)WebPSafeMalloc(total_num_pixels, sizeof(uint32_t)); |
1559 | 0 | if (dec->pixels_ == NULL) { |
1560 | 0 | dec->argb_cache_ = NULL; // for soundness |
1561 | 0 | return VP8LSetError(dec, VP8_STATUS_OUT_OF_MEMORY); |
1562 | 0 | } |
1563 | 0 | dec->argb_cache_ = dec->pixels_ + num_pixels + cache_top_pixels; |
1564 | 0 | return 1; |
1565 | 0 | } |
1566 | | |
1567 | 0 | static int AllocateInternalBuffers8b(VP8LDecoder* const dec) { |
1568 | 0 | const uint64_t total_num_pixels = (uint64_t)dec->width_ * dec->height_; |
1569 | 0 | dec->argb_cache_ = NULL; // for soundness |
1570 | 0 | dec->pixels_ = (uint32_t*)WebPSafeMalloc(total_num_pixels, sizeof(uint8_t)); |
1571 | 0 | if (dec->pixels_ == NULL) { |
1572 | 0 | return VP8LSetError(dec, VP8_STATUS_OUT_OF_MEMORY); |
1573 | 0 | } |
1574 | 0 | return 1; |
1575 | 0 | } |
1576 | | |
1577 | | //------------------------------------------------------------------------------ |
1578 | | |
1579 | | // Special row-processing that only stores the alpha data. |
1580 | 0 | static void ExtractAlphaRows(VP8LDecoder* const dec, int last_row) { |
1581 | 0 | int cur_row = dec->last_row_; |
1582 | 0 | int num_rows = last_row - cur_row; |
1583 | 0 | const uint32_t* in = dec->pixels_ + dec->width_ * cur_row; |
1584 | |
|
1585 | 0 | assert(last_row <= dec->io_->crop_bottom); |
1586 | 0 | while (num_rows > 0) { |
1587 | 0 | const int num_rows_to_process = |
1588 | 0 | (num_rows > NUM_ARGB_CACHE_ROWS) ? NUM_ARGB_CACHE_ROWS : num_rows; |
1589 | | // Extract alpha (which is stored in the green plane). |
1590 | 0 | ALPHDecoder* const alph_dec = (ALPHDecoder*)dec->io_->opaque; |
1591 | 0 | uint8_t* const output = alph_dec->output_; |
1592 | 0 | const int width = dec->io_->width; // the final width (!= dec->width_) |
1593 | 0 | const int cache_pixs = width * num_rows_to_process; |
1594 | 0 | uint8_t* const dst = output + width * cur_row; |
1595 | 0 | const uint32_t* const src = dec->argb_cache_; |
1596 | 0 | ApplyInverseTransforms(dec, cur_row, num_rows_to_process, in); |
1597 | 0 | WebPExtractGreen(src, dst, cache_pixs); |
1598 | 0 | AlphaApplyFilter(alph_dec, |
1599 | 0 | cur_row, cur_row + num_rows_to_process, dst, width); |
1600 | 0 | num_rows -= num_rows_to_process; |
1601 | 0 | in += num_rows_to_process * dec->width_; |
1602 | 0 | cur_row += num_rows_to_process; |
1603 | 0 | } |
1604 | 0 | assert(cur_row == last_row); |
1605 | 0 | dec->last_row_ = dec->last_out_row_ = last_row; |
1606 | 0 | } |
1607 | | |
1608 | | int VP8LDecodeAlphaHeader(ALPHDecoder* const alph_dec, |
1609 | 0 | const uint8_t* const data, size_t data_size) { |
1610 | 0 | int ok = 0; |
1611 | 0 | VP8LDecoder* dec = VP8LNew(); |
1612 | |
|
1613 | 0 | if (dec == NULL) return 0; |
1614 | | |
1615 | 0 | assert(alph_dec != NULL); |
1616 | | |
1617 | 0 | dec->width_ = alph_dec->width_; |
1618 | 0 | dec->height_ = alph_dec->height_; |
1619 | 0 | dec->io_ = &alph_dec->io_; |
1620 | 0 | dec->io_->opaque = alph_dec; |
1621 | 0 | dec->io_->width = alph_dec->width_; |
1622 | 0 | dec->io_->height = alph_dec->height_; |
1623 | |
|
1624 | 0 | dec->status_ = VP8_STATUS_OK; |
1625 | 0 | VP8LInitBitReader(&dec->br_, data, data_size); |
1626 | |
|
1627 | 0 | if (!DecodeImageStream(alph_dec->width_, alph_dec->height_, /*is_level0=*/1, |
1628 | 0 | dec, /*decoded_data=*/NULL)) { |
1629 | 0 | goto Err; |
1630 | 0 | } |
1631 | | |
1632 | | // Special case: if alpha data uses only the color indexing transform and |
1633 | | // doesn't use color cache (a frequent case), we will use DecodeAlphaData() |
1634 | | // method that only needs allocation of 1 byte per pixel (alpha channel). |
1635 | 0 | if (dec->next_transform_ == 1 && |
1636 | 0 | dec->transforms_[0].type_ == COLOR_INDEXING_TRANSFORM && |
1637 | 0 | Is8bOptimizable(&dec->hdr_)) { |
1638 | 0 | alph_dec->use_8b_decode_ = 1; |
1639 | 0 | ok = AllocateInternalBuffers8b(dec); |
1640 | 0 | } else { |
1641 | | // Allocate internal buffers (note that dec->width_ may have changed here). |
1642 | 0 | alph_dec->use_8b_decode_ = 0; |
1643 | 0 | ok = AllocateInternalBuffers32b(dec, alph_dec->width_); |
1644 | 0 | } |
1645 | |
|
1646 | 0 | if (!ok) goto Err; |
1647 | | |
1648 | | // Only set here, once we are sure it is valid (to avoid thread races). |
1649 | 0 | alph_dec->vp8l_dec_ = dec; |
1650 | 0 | return 1; |
1651 | | |
1652 | 0 | Err: |
1653 | 0 | VP8LDelete(dec); |
1654 | 0 | return 0; |
1655 | 0 | } |
1656 | | |
1657 | 0 | int VP8LDecodeAlphaImageStream(ALPHDecoder* const alph_dec, int last_row) { |
1658 | 0 | VP8LDecoder* const dec = alph_dec->vp8l_dec_; |
1659 | 0 | assert(dec != NULL); |
1660 | 0 | assert(last_row <= dec->height_); |
1661 | | |
1662 | 0 | if (dec->last_row_ >= last_row) { |
1663 | 0 | return 1; // done |
1664 | 0 | } |
1665 | | |
1666 | 0 | if (!alph_dec->use_8b_decode_) WebPInitAlphaProcessing(); |
1667 | | |
1668 | | // Decode (with special row processing). |
1669 | 0 | return alph_dec->use_8b_decode_ ? |
1670 | 0 | DecodeAlphaData(dec, (uint8_t*)dec->pixels_, dec->width_, dec->height_, |
1671 | 0 | last_row) : |
1672 | 0 | DecodeImageData(dec, dec->pixels_, dec->width_, dec->height_, |
1673 | 0 | last_row, ExtractAlphaRows); |
1674 | 0 | } |
1675 | | |
1676 | | //------------------------------------------------------------------------------ |
1677 | | |
1678 | 0 | int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io) { |
1679 | 0 | int width, height, has_alpha; |
1680 | |
|
1681 | 0 | if (dec == NULL) return 0; |
1682 | 0 | if (io == NULL) { |
1683 | 0 | return VP8LSetError(dec, VP8_STATUS_INVALID_PARAM); |
1684 | 0 | } |
1685 | | |
1686 | 0 | dec->io_ = io; |
1687 | 0 | dec->status_ = VP8_STATUS_OK; |
1688 | 0 | VP8LInitBitReader(&dec->br_, io->data, io->data_size); |
1689 | 0 | if (!ReadImageInfo(&dec->br_, &width, &height, &has_alpha)) { |
1690 | 0 | VP8LSetError(dec, VP8_STATUS_BITSTREAM_ERROR); |
1691 | 0 | goto Error; |
1692 | 0 | } |
1693 | 0 | dec->state_ = READ_DIM; |
1694 | 0 | io->width = width; |
1695 | 0 | io->height = height; |
1696 | |
|
1697 | 0 | if (!DecodeImageStream(width, height, /*is_level0=*/1, dec, |
1698 | | /*decoded_data=*/NULL)) { |
1699 | 0 | goto Error; |
1700 | 0 | } |
1701 | 0 | return 1; |
1702 | | |
1703 | 0 | Error: |
1704 | 0 | VP8LClear(dec); |
1705 | 0 | assert(dec->status_ != VP8_STATUS_OK); |
1706 | 0 | return 0; |
1707 | 0 | } |
1708 | | |
1709 | 0 | int VP8LDecodeImage(VP8LDecoder* const dec) { |
1710 | 0 | VP8Io* io = NULL; |
1711 | 0 | WebPDecParams* params = NULL; |
1712 | |
|
1713 | 0 | if (dec == NULL) return 0; |
1714 | | |
1715 | 0 | assert(dec->hdr_.huffman_tables_.root.start != NULL); |
1716 | 0 | assert(dec->hdr_.htree_groups_ != NULL); |
1717 | 0 | assert(dec->hdr_.num_htree_groups_ > 0); |
1718 | | |
1719 | 0 | io = dec->io_; |
1720 | 0 | assert(io != NULL); |
1721 | 0 | params = (WebPDecParams*)io->opaque; |
1722 | 0 | assert(params != NULL); |
1723 | | |
1724 | | // Initialization. |
1725 | 0 | if (dec->state_ != READ_DATA) { |
1726 | 0 | dec->output_ = params->output; |
1727 | 0 | assert(dec->output_ != NULL); |
1728 | | |
1729 | 0 | if (!WebPIoInitFromOptions(params->options, io, MODE_BGRA)) { |
1730 | 0 | VP8LSetError(dec, VP8_STATUS_INVALID_PARAM); |
1731 | 0 | goto Err; |
1732 | 0 | } |
1733 | | |
1734 | 0 | if (!AllocateInternalBuffers32b(dec, io->width)) goto Err; |
1735 | | |
1736 | 0 | #if !defined(WEBP_REDUCE_SIZE) |
1737 | 0 | if (io->use_scaling && !AllocateAndInitRescaler(dec, io)) goto Err; |
1738 | | #else |
1739 | | if (io->use_scaling) { |
1740 | | VP8LSetError(dec, VP8_STATUS_INVALID_PARAM); |
1741 | | goto Err; |
1742 | | } |
1743 | | #endif |
1744 | 0 | if (io->use_scaling || WebPIsPremultipliedMode(dec->output_->colorspace)) { |
1745 | | // need the alpha-multiply functions for premultiplied output or rescaling |
1746 | 0 | WebPInitAlphaProcessing(); |
1747 | 0 | } |
1748 | |
|
1749 | 0 | if (!WebPIsRGBMode(dec->output_->colorspace)) { |
1750 | 0 | WebPInitConvertARGBToYUV(); |
1751 | 0 | if (dec->output_->u.YUVA.a != NULL) WebPInitAlphaProcessing(); |
1752 | 0 | } |
1753 | 0 | if (dec->incremental_) { |
1754 | 0 | if (dec->hdr_.color_cache_size_ > 0 && |
1755 | 0 | dec->hdr_.saved_color_cache_.colors_ == NULL) { |
1756 | 0 | if (!VP8LColorCacheInit(&dec->hdr_.saved_color_cache_, |
1757 | 0 | dec->hdr_.color_cache_.hash_bits_)) { |
1758 | 0 | VP8LSetError(dec, VP8_STATUS_OUT_OF_MEMORY); |
1759 | 0 | goto Err; |
1760 | 0 | } |
1761 | 0 | } |
1762 | 0 | } |
1763 | 0 | dec->state_ = READ_DATA; |
1764 | 0 | } |
1765 | | |
1766 | | // Decode. |
1767 | 0 | if (!DecodeImageData(dec, dec->pixels_, dec->width_, dec->height_, |
1768 | 0 | io->crop_bottom, ProcessRows)) { |
1769 | 0 | goto Err; |
1770 | 0 | } |
1771 | | |
1772 | 0 | params->last_y = dec->last_out_row_; |
1773 | 0 | return 1; |
1774 | | |
1775 | 0 | Err: |
1776 | 0 | VP8LClear(dec); |
1777 | 0 | assert(dec->status_ != VP8_STATUS_OK); |
1778 | 0 | return 0; |
1779 | 0 | } |
1780 | | |
1781 | | //------------------------------------------------------------------------------ |