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