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