New comit of SDL2
[supertux.git] / src / SDL2 / external / libwebp-0.3.0 / src / dec / vp8l.c
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
3 // This code is licensed under the same terms as WebM:
4 //  Software License Agreement:  http://www.webmproject.org/license/software/
5 //  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
6 // -----------------------------------------------------------------------------
7 //
8 // main entry for the decoder
9 //
10 // Authors: Vikas Arora (vikaas.arora@gmail.com)
11 //          Jyrki Alakuijala (jyrki@google.com)
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "./vp8li.h"
16 #include "../dsp/lossless.h"
17 #include "../dsp/yuv.h"
18 #include "../utils/huffman.h"
19 #include "../utils/utils.h"
20
21 #if defined(__cplusplus) || defined(c_plusplus)
22 extern "C" {
23 #endif
24
25 #define NUM_ARGB_CACHE_ROWS          16
26
27 static const int kCodeLengthLiterals = 16;
28 static const int kCodeLengthRepeatCode = 16;
29 static const int kCodeLengthExtraBits[3] = { 2, 3, 7 };
30 static const int kCodeLengthRepeatOffsets[3] = { 3, 3, 11 };
31
32 // -----------------------------------------------------------------------------
33 //  Five Huffman codes are used at each meta code:
34 //  1. green + length prefix codes + color cache codes,
35 //  2. alpha,
36 //  3. red,
37 //  4. blue, and,
38 //  5. distance prefix codes.
39 typedef enum {
40   GREEN = 0,
41   RED   = 1,
42   BLUE  = 2,
43   ALPHA = 3,
44   DIST  = 4
45 } HuffIndex;
46
47 static const uint16_t kAlphabetSize[HUFFMAN_CODES_PER_META_CODE] = {
48   NUM_LITERAL_CODES + NUM_LENGTH_CODES,
49   NUM_LITERAL_CODES, NUM_LITERAL_CODES, NUM_LITERAL_CODES,
50   NUM_DISTANCE_CODES
51 };
52
53
54 #define NUM_CODE_LENGTH_CODES       19
55 static const uint8_t kCodeLengthCodeOrder[NUM_CODE_LENGTH_CODES] = {
56   17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
57 };
58
59 #define CODE_TO_PLANE_CODES        120
60 static const uint8_t code_to_plane_lut[CODE_TO_PLANE_CODES] = {
61   0x18, 0x07, 0x17, 0x19, 0x28, 0x06, 0x27, 0x29, 0x16, 0x1a,
62   0x26, 0x2a, 0x38, 0x05, 0x37, 0x39, 0x15, 0x1b, 0x36, 0x3a,
63   0x25, 0x2b, 0x48, 0x04, 0x47, 0x49, 0x14, 0x1c, 0x35, 0x3b,
64   0x46, 0x4a, 0x24, 0x2c, 0x58, 0x45, 0x4b, 0x34, 0x3c, 0x03,
65   0x57, 0x59, 0x13, 0x1d, 0x56, 0x5a, 0x23, 0x2d, 0x44, 0x4c,
66   0x55, 0x5b, 0x33, 0x3d, 0x68, 0x02, 0x67, 0x69, 0x12, 0x1e,
67   0x66, 0x6a, 0x22, 0x2e, 0x54, 0x5c, 0x43, 0x4d, 0x65, 0x6b,
68   0x32, 0x3e, 0x78, 0x01, 0x77, 0x79, 0x53, 0x5d, 0x11, 0x1f,
69   0x64, 0x6c, 0x42, 0x4e, 0x76, 0x7a, 0x21, 0x2f, 0x75, 0x7b,
70   0x31, 0x3f, 0x63, 0x6d, 0x52, 0x5e, 0x00, 0x74, 0x7c, 0x41,
71   0x4f, 0x10, 0x20, 0x62, 0x6e, 0x30, 0x73, 0x7d, 0x51, 0x5f,
72   0x40, 0x72, 0x7e, 0x61, 0x6f, 0x50, 0x71, 0x7f, 0x60, 0x70
73 };
74
75 static int DecodeImageStream(int xsize, int ysize,
76                              int is_level0,
77                              VP8LDecoder* const dec,
78                              uint32_t** const decoded_data);
79
80 //------------------------------------------------------------------------------
81
82 int VP8LCheckSignature(const uint8_t* const data, size_t size) {
83   return (size >= 1) && (data[0] == VP8L_MAGIC_BYTE);
84 }
85
86 static int ReadImageInfo(VP8LBitReader* const br,
87                          int* const width, int* const height,
88                          int* const has_alpha) {
89   const uint8_t signature = VP8LReadBits(br, 8);
90   if (!VP8LCheckSignature(&signature, 1)) {
91     return 0;
92   }
93   *width = VP8LReadBits(br, VP8L_IMAGE_SIZE_BITS) + 1;
94   *height = VP8LReadBits(br, VP8L_IMAGE_SIZE_BITS) + 1;
95   *has_alpha = VP8LReadBits(br, 1);
96   VP8LReadBits(br, VP8L_VERSION_BITS);  // Read/ignore the version number.
97   return 1;
98 }
99
100 int VP8LGetInfo(const uint8_t* data, size_t data_size,
101                 int* const width, int* const height, int* const has_alpha) {
102   if (data == NULL || data_size < VP8L_FRAME_HEADER_SIZE) {
103     return 0;         // not enough data
104   } else {
105     int w, h, a;
106     VP8LBitReader br;
107     VP8LInitBitReader(&br, data, data_size);
108     if (!ReadImageInfo(&br, &w, &h, &a)) {
109       return 0;
110     }
111     if (width != NULL) *width = w;
112     if (height != NULL) *height = h;
113     if (has_alpha != NULL) *has_alpha = a;
114     return 1;
115   }
116 }
117
118 //------------------------------------------------------------------------------
119
120 static WEBP_INLINE int GetCopyDistance(int distance_symbol,
121                                        VP8LBitReader* const br) {
122   int extra_bits, offset;
123   if (distance_symbol < 4) {
124     return distance_symbol + 1;
125   }
126   extra_bits = (distance_symbol - 2) >> 1;
127   offset = (2 + (distance_symbol & 1)) << extra_bits;
128   return offset + VP8LReadBits(br, extra_bits) + 1;
129 }
130
131 static WEBP_INLINE int GetCopyLength(int length_symbol,
132                                      VP8LBitReader* const br) {
133   // Length and distance prefixes are encoded the same way.
134   return GetCopyDistance(length_symbol, br);
135 }
136
137 static WEBP_INLINE int PlaneCodeToDistance(int xsize, int plane_code) {
138   if (plane_code > CODE_TO_PLANE_CODES) {
139     return plane_code - CODE_TO_PLANE_CODES;
140   } else {
141     const int dist_code = code_to_plane_lut[plane_code - 1];
142     const int yoffset = dist_code >> 4;
143     const int xoffset = 8 - (dist_code & 0xf);
144     const int dist = yoffset * xsize + xoffset;
145     return (dist >= 1) ? dist : 1;
146   }
147 }
148
149 //------------------------------------------------------------------------------
150 // Decodes the next Huffman code from bit-stream.
151 // FillBitWindow(br) needs to be called at minimum every second call
152 // to ReadSymbol, in order to pre-fetch enough bits.
153 static WEBP_INLINE int ReadSymbol(const HuffmanTree* tree,
154                                   VP8LBitReader* const br) {
155   const HuffmanTreeNode* node = tree->root_;
156   int num_bits = 0;
157   uint32_t bits = VP8LPrefetchBits(br);
158   assert(node != NULL);
159   while (!HuffmanTreeNodeIsLeaf(node)) {
160     node = HuffmanTreeNextNode(node, bits & 1);
161     bits >>= 1;
162     ++num_bits;
163   }
164   VP8LDiscardBits(br, num_bits);
165   return node->symbol_;
166 }
167
168 static int ReadHuffmanCodeLengths(
169     VP8LDecoder* const dec, const int* const code_length_code_lengths,
170     int num_symbols, int* const code_lengths) {
171   int ok = 0;
172   VP8LBitReader* const br = &dec->br_;
173   int symbol;
174   int max_symbol;
175   int prev_code_len = DEFAULT_CODE_LENGTH;
176   HuffmanTree tree;
177
178   if (!HuffmanTreeBuildImplicit(&tree, code_length_code_lengths,
179                                 NUM_CODE_LENGTH_CODES)) {
180     dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
181     return 0;
182   }
183
184   if (VP8LReadBits(br, 1)) {    // use length
185     const int length_nbits = 2 + 2 * VP8LReadBits(br, 3);
186     max_symbol = 2 + VP8LReadBits(br, length_nbits);
187     if (max_symbol > num_symbols) {
188       dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
189       goto End;
190     }
191   } else {
192     max_symbol = num_symbols;
193   }
194
195   symbol = 0;
196   while (symbol < num_symbols) {
197     int code_len;
198     if (max_symbol-- == 0) break;
199     VP8LFillBitWindow(br);
200     code_len = ReadSymbol(&tree, br);
201     if (code_len < kCodeLengthLiterals) {
202       code_lengths[symbol++] = code_len;
203       if (code_len != 0) prev_code_len = code_len;
204     } else {
205       const int use_prev = (code_len == kCodeLengthRepeatCode);
206       const int slot = code_len - kCodeLengthLiterals;
207       const int extra_bits = kCodeLengthExtraBits[slot];
208       const int repeat_offset = kCodeLengthRepeatOffsets[slot];
209       int repeat = VP8LReadBits(br, extra_bits) + repeat_offset;
210       if (symbol + repeat > num_symbols) {
211         dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
212         goto End;
213       } else {
214         const int length = use_prev ? prev_code_len : 0;
215         while (repeat-- > 0) code_lengths[symbol++] = length;
216       }
217     }
218   }
219   ok = 1;
220
221  End:
222   HuffmanTreeRelease(&tree);
223   return ok;
224 }
225
226 static int ReadHuffmanCode(int alphabet_size, VP8LDecoder* const dec,
227                            HuffmanTree* const tree) {
228   int ok = 0;
229   VP8LBitReader* const br = &dec->br_;
230   const int simple_code = VP8LReadBits(br, 1);
231
232   if (simple_code) {  // Read symbols, codes & code lengths directly.
233     int symbols[2];
234     int codes[2];
235     int code_lengths[2];
236     const int num_symbols = VP8LReadBits(br, 1) + 1;
237     const int first_symbol_len_code = VP8LReadBits(br, 1);
238     // The first code is either 1 bit or 8 bit code.
239     symbols[0] = VP8LReadBits(br, (first_symbol_len_code == 0) ? 1 : 8);
240     codes[0] = 0;
241     code_lengths[0] = num_symbols - 1;
242     // The second code (if present), is always 8 bit long.
243     if (num_symbols == 2) {
244       symbols[1] = VP8LReadBits(br, 8);
245       codes[1] = 1;
246       code_lengths[1] = num_symbols - 1;
247     }
248     ok = HuffmanTreeBuildExplicit(tree, code_lengths, codes, symbols,
249                                   alphabet_size, num_symbols);
250   } else {  // Decode Huffman-coded code lengths.
251     int* code_lengths = NULL;
252     int i;
253     int code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
254     const int num_codes = VP8LReadBits(br, 4) + 4;
255     if (num_codes > NUM_CODE_LENGTH_CODES) {
256       dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
257       return 0;
258     }
259
260     code_lengths =
261         (int*)WebPSafeCalloc((uint64_t)alphabet_size, sizeof(*code_lengths));
262     if (code_lengths == NULL) {
263       dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
264       return 0;
265     }
266
267     for (i = 0; i < num_codes; ++i) {
268       code_length_code_lengths[kCodeLengthCodeOrder[i]] = VP8LReadBits(br, 3);
269     }
270     ok = ReadHuffmanCodeLengths(dec, code_length_code_lengths, alphabet_size,
271                                 code_lengths);
272     if (ok) {
273       ok = HuffmanTreeBuildImplicit(tree, code_lengths, alphabet_size);
274     }
275     free(code_lengths);
276   }
277   ok = ok && !br->error_;
278   if (!ok) {
279     dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
280     return 0;
281   }
282   return 1;
283 }
284
285 static void DeleteHtreeGroups(HTreeGroup* htree_groups, int num_htree_groups) {
286   if (htree_groups != NULL) {
287     int i, j;
288     for (i = 0; i < num_htree_groups; ++i) {
289       HuffmanTree* const htrees = htree_groups[i].htrees_;
290       for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
291         HuffmanTreeRelease(&htrees[j]);
292       }
293     }
294     free(htree_groups);
295   }
296 }
297
298 static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
299                             int color_cache_bits, int allow_recursion) {
300   int i, j;
301   VP8LBitReader* const br = &dec->br_;
302   VP8LMetadata* const hdr = &dec->hdr_;
303   uint32_t* huffman_image = NULL;
304   HTreeGroup* htree_groups = NULL;
305   int num_htree_groups = 1;
306
307   if (allow_recursion && VP8LReadBits(br, 1)) {
308     // use meta Huffman codes.
309     const int huffman_precision = VP8LReadBits(br, 3) + 2;
310     const int huffman_xsize = VP8LSubSampleSize(xsize, huffman_precision);
311     const int huffman_ysize = VP8LSubSampleSize(ysize, huffman_precision);
312     const int huffman_pixs = huffman_xsize * huffman_ysize;
313     if (!DecodeImageStream(huffman_xsize, huffman_ysize, 0, dec,
314                            &huffman_image)) {
315       dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
316       goto Error;
317     }
318     hdr->huffman_subsample_bits_ = huffman_precision;
319     for (i = 0; i < huffman_pixs; ++i) {
320       // The huffman data is stored in red and green bytes.
321       const int group = (huffman_image[i] >> 8) & 0xffff;
322       huffman_image[i] = group;
323       if (group >= num_htree_groups) {
324         num_htree_groups = group + 1;
325       }
326     }
327   }
328
329   if (br->error_) goto Error;
330
331   assert(num_htree_groups <= 0x10000);
332   htree_groups =
333       (HTreeGroup*)WebPSafeCalloc((uint64_t)num_htree_groups,
334                                   sizeof(*htree_groups));
335   if (htree_groups == NULL) {
336     dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
337     goto Error;
338   }
339
340   for (i = 0; i < num_htree_groups; ++i) {
341     HuffmanTree* const htrees = htree_groups[i].htrees_;
342     for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
343       int alphabet_size = kAlphabetSize[j];
344       if (j == 0 && color_cache_bits > 0) {
345         alphabet_size += 1 << color_cache_bits;
346       }
347       if (!ReadHuffmanCode(alphabet_size, dec, htrees + j)) goto Error;
348     }
349   }
350
351   // All OK. Finalize pointers and return.
352   hdr->huffman_image_ = huffman_image;
353   hdr->num_htree_groups_ = num_htree_groups;
354   hdr->htree_groups_ = htree_groups;
355   return 1;
356
357  Error:
358   free(huffman_image);
359   DeleteHtreeGroups(htree_groups, num_htree_groups);
360   return 0;
361 }
362
363 //------------------------------------------------------------------------------
364 // Scaling.
365
366 static int AllocateAndInitRescaler(VP8LDecoder* const dec, VP8Io* const io) {
367   const int num_channels = 4;
368   const int in_width = io->mb_w;
369   const int out_width = io->scaled_width;
370   const int in_height = io->mb_h;
371   const int out_height = io->scaled_height;
372   const uint64_t work_size = 2 * num_channels * (uint64_t)out_width;
373   int32_t* work;        // Rescaler work area.
374   const uint64_t scaled_data_size = num_channels * (uint64_t)out_width;
375   uint32_t* scaled_data;  // Temporary storage for scaled BGRA data.
376   const uint64_t memory_size = sizeof(*dec->rescaler) +
377                                work_size * sizeof(*work) +
378                                scaled_data_size * sizeof(*scaled_data);
379   uint8_t* memory = (uint8_t*)WebPSafeCalloc(memory_size, sizeof(*memory));
380   if (memory == NULL) {
381     dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
382     return 0;
383   }
384   assert(dec->rescaler_memory == NULL);
385   dec->rescaler_memory = memory;
386
387   dec->rescaler = (WebPRescaler*)memory;
388   memory += sizeof(*dec->rescaler);
389   work = (int32_t*)memory;
390   memory += work_size * sizeof(*work);
391   scaled_data = (uint32_t*)memory;
392
393   WebPRescalerInit(dec->rescaler, in_width, in_height, (uint8_t*)scaled_data,
394                    out_width, out_height, 0, num_channels,
395                    in_width, out_width, in_height, out_height, work);
396   return 1;
397 }
398
399 //------------------------------------------------------------------------------
400 // Export to ARGB
401
402 // We have special "export" function since we need to convert from BGRA
403 static int Export(WebPRescaler* const rescaler, WEBP_CSP_MODE colorspace,
404                   int rgba_stride, uint8_t* const rgba) {
405   const uint32_t* const src = (const uint32_t*)rescaler->dst;
406   const int dst_width = rescaler->dst_width;
407   int num_lines_out = 0;
408   while (WebPRescalerHasPendingOutput(rescaler)) {
409     uint8_t* const dst = rgba + num_lines_out * rgba_stride;
410     WebPRescalerExportRow(rescaler);
411     VP8LConvertFromBGRA(src, dst_width, colorspace, dst);
412     ++num_lines_out;
413   }
414   return num_lines_out;
415 }
416
417 // Emit scaled rows.
418 static int EmitRescaledRows(const VP8LDecoder* const dec,
419                             const uint32_t* const data, int in_stride, int mb_h,
420                             uint8_t* const out, int out_stride) {
421   const WEBP_CSP_MODE colorspace = dec->output_->colorspace;
422   const uint8_t* const in = (const uint8_t*)data;
423   int num_lines_in = 0;
424   int num_lines_out = 0;
425   while (num_lines_in < mb_h) {
426     const uint8_t* const row_in = in + num_lines_in * in_stride;
427     uint8_t* const row_out = out + num_lines_out * out_stride;
428     num_lines_in += WebPRescalerImport(dec->rescaler, mb_h - num_lines_in,
429                                        row_in, in_stride);
430     num_lines_out += Export(dec->rescaler, colorspace, out_stride, row_out);
431   }
432   return num_lines_out;
433 }
434
435 // Emit rows without any scaling.
436 static int EmitRows(WEBP_CSP_MODE colorspace,
437                     const uint32_t* const data, int in_stride,
438                     int mb_w, int mb_h,
439                     uint8_t* const out, int out_stride) {
440   int lines = mb_h;
441   const uint8_t* row_in = (const uint8_t*)data;
442   uint8_t* row_out = out;
443   while (lines-- > 0) {
444     VP8LConvertFromBGRA((const uint32_t*)row_in, mb_w, colorspace, row_out);
445     row_in += in_stride;
446     row_out += out_stride;
447   }
448   return mb_h;  // Num rows out == num rows in.
449 }
450
451 //------------------------------------------------------------------------------
452 // Export to YUVA
453
454 static void ConvertToYUVA(const uint32_t* const src, int width, int y_pos,
455                           const WebPDecBuffer* const output) {
456   const WebPYUVABuffer* const buf = &output->u.YUVA;
457   // first, the luma plane
458   {
459     int i;
460     uint8_t* const y = buf->y + y_pos * buf->y_stride;
461     for (i = 0; i < width; ++i) {
462       const uint32_t p = src[i];
463       y[i] = VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >> 0) & 0xff);
464     }
465   }
466
467   // then U/V planes
468   {
469     uint8_t* const u = buf->u + (y_pos >> 1) * buf->u_stride;
470     uint8_t* const v = buf->v + (y_pos >> 1) * buf->v_stride;
471     const int uv_width = width >> 1;
472     int i;
473     for (i = 0; i < uv_width; ++i) {
474       const uint32_t v0 = src[2 * i + 0];
475       const uint32_t v1 = src[2 * i + 1];
476       // VP8RGBToU/V expects four accumulated pixels. Hence we need to
477       // scale r/g/b value by a factor 2. We just shift v0/v1 one bit less.
478       const int r = ((v0 >> 15) & 0x1fe) + ((v1 >> 15) & 0x1fe);
479       const int g = ((v0 >>  7) & 0x1fe) + ((v1 >>  7) & 0x1fe);
480       const int b = ((v0 <<  1) & 0x1fe) + ((v1 <<  1) & 0x1fe);
481       if (!(y_pos & 1)) {  // even lines: store values
482         u[i] = VP8RGBToU(r, g, b);
483         v[i] = VP8RGBToV(r, g, b);
484       } else {             // odd lines: average with previous values
485         const int tmp_u = VP8RGBToU(r, g, b);
486         const int tmp_v = VP8RGBToV(r, g, b);
487         // Approximated average-of-four. But it's an acceptable diff.
488         u[i] = (u[i] + tmp_u + 1) >> 1;
489         v[i] = (v[i] + tmp_v + 1) >> 1;
490       }
491     }
492     if (width & 1) {       // last pixel
493       const uint32_t v0 = src[2 * i + 0];
494       const int r = (v0 >> 14) & 0x3fc;
495       const int g = (v0 >>  6) & 0x3fc;
496       const int b = (v0 <<  2) & 0x3fc;
497       if (!(y_pos & 1)) {  // even lines
498         u[i] = VP8RGBToU(r, g, b);
499         v[i] = VP8RGBToV(r, g, b);
500       } else {             // odd lines (note: we could just skip this)
501         const int tmp_u = VP8RGBToU(r, g, b);
502         const int tmp_v = VP8RGBToV(r, g, b);
503         u[i] = (u[i] + tmp_u + 1) >> 1;
504         v[i] = (v[i] + tmp_v + 1) >> 1;
505       }
506     }
507   }
508   // Lastly, store alpha if needed.
509   if (buf->a != NULL) {
510     int i;
511     uint8_t* const a = buf->a + y_pos * buf->a_stride;
512     for (i = 0; i < width; ++i) a[i] = (src[i] >> 24);
513   }
514 }
515
516 static int ExportYUVA(const VP8LDecoder* const dec, int y_pos) {
517   WebPRescaler* const rescaler = dec->rescaler;
518   const uint32_t* const src = (const uint32_t*)rescaler->dst;
519   const int dst_width = rescaler->dst_width;
520   int num_lines_out = 0;
521   while (WebPRescalerHasPendingOutput(rescaler)) {
522     WebPRescalerExportRow(rescaler);
523     ConvertToYUVA(src, dst_width, y_pos, dec->output_);
524     ++y_pos;
525     ++num_lines_out;
526   }
527   return num_lines_out;
528 }
529
530 static int EmitRescaledRowsYUVA(const VP8LDecoder* const dec,
531                                 const uint32_t* const data,
532                                 int in_stride, int mb_h) {
533   const uint8_t* const in = (const uint8_t*)data;
534   int num_lines_in = 0;
535   int y_pos = dec->last_out_row_;
536   while (num_lines_in < mb_h) {
537     const uint8_t* const row_in = in + num_lines_in * in_stride;
538     num_lines_in += WebPRescalerImport(dec->rescaler, mb_h - num_lines_in,
539                                        row_in, in_stride);
540     y_pos += ExportYUVA(dec, y_pos);
541   }
542   return y_pos;
543 }
544
545 static int EmitRowsYUVA(const VP8LDecoder* const dec,
546                         const uint32_t* const data, int in_stride,
547                         int mb_w, int num_rows) {
548   int y_pos = dec->last_out_row_;
549   const uint8_t* row_in = (const uint8_t*)data;
550   while (num_rows-- > 0) {
551     ConvertToYUVA((const uint32_t*)row_in, mb_w, y_pos, dec->output_);
552     row_in += in_stride;
553     ++y_pos;
554   }
555   return y_pos;
556 }
557
558 //------------------------------------------------------------------------------
559 // Cropping.
560
561 // Sets io->mb_y, io->mb_h & io->mb_w according to start row, end row and
562 // crop options. Also updates the input data pointer, so that it points to the
563 // start of the cropped window.
564 // Note that 'pixel_stride' is in units of 'uint32_t' (and not 'bytes).
565 // Returns true if the crop window is not empty.
566 static int SetCropWindow(VP8Io* const io, int y_start, int y_end,
567                          const uint32_t** const in_data, int pixel_stride) {
568   assert(y_start < y_end);
569   assert(io->crop_left < io->crop_right);
570   if (y_end > io->crop_bottom) {
571     y_end = io->crop_bottom;  // make sure we don't overflow on last row.
572   }
573   if (y_start < io->crop_top) {
574     const int delta = io->crop_top - y_start;
575     y_start = io->crop_top;
576     *in_data += pixel_stride * delta;
577   }
578   if (y_start >= y_end) return 0;  // Crop window is empty.
579
580   *in_data += io->crop_left;
581
582   io->mb_y = y_start - io->crop_top;
583   io->mb_w = io->crop_right - io->crop_left;
584   io->mb_h = y_end - y_start;
585   return 1;  // Non-empty crop window.
586 }
587
588 //------------------------------------------------------------------------------
589
590 static WEBP_INLINE int GetMetaIndex(
591     const uint32_t* const image, int xsize, int bits, int x, int y) {
592   if (bits == 0) return 0;
593   return image[xsize * (y >> bits) + (x >> bits)];
594 }
595
596 static WEBP_INLINE HTreeGroup* GetHtreeGroupForPos(VP8LMetadata* const hdr,
597                                                    int x, int y) {
598   const int meta_index = GetMetaIndex(hdr->huffman_image_, hdr->huffman_xsize_,
599                                       hdr->huffman_subsample_bits_, x, y);
600   assert(meta_index < hdr->num_htree_groups_);
601   return hdr->htree_groups_ + meta_index;
602 }
603
604 //------------------------------------------------------------------------------
605 // Main loop, with custom row-processing function
606
607 typedef void (*ProcessRowsFunc)(VP8LDecoder* const dec, int row);
608
609 static void ApplyInverseTransforms(VP8LDecoder* const dec, int num_rows,
610                                    const uint32_t* const rows) {
611   int n = dec->next_transform_;
612   const int cache_pixs = dec->width_ * num_rows;
613   const int start_row = dec->last_row_;
614   const int end_row = start_row + num_rows;
615   const uint32_t* rows_in = rows;
616   uint32_t* const rows_out = dec->argb_cache_;
617
618   // Inverse transforms.
619   // TODO: most transforms only need to operate on the cropped region only.
620   memcpy(rows_out, rows_in, cache_pixs * sizeof(*rows_out));
621   while (n-- > 0) {
622     VP8LTransform* const transform = &dec->transforms_[n];
623     VP8LInverseTransform(transform, start_row, end_row, rows_in, rows_out);
624     rows_in = rows_out;
625   }
626 }
627
628 // Special method for paletted alpha data.
629 static void ApplyInverseTransformsAlpha(VP8LDecoder* const dec, int num_rows,
630                                         const uint8_t* const rows) {
631   const int start_row = dec->last_row_;
632   const int end_row = start_row + num_rows;
633   const uint8_t* rows_in = rows;
634   uint8_t* rows_out = (uint8_t*)dec->io_->opaque + dec->io_->width * start_row;
635   VP8LTransform* const transform = &dec->transforms_[0];
636   assert(dec->next_transform_ == 1);
637   assert(transform->type_ == COLOR_INDEXING_TRANSFORM);
638   VP8LColorIndexInverseTransformAlpha(transform, start_row, end_row, rows_in,
639                                       rows_out);
640 }
641
642 // Processes (transforms, scales & color-converts) the rows decoded after the
643 // last call.
644 static void ProcessRows(VP8LDecoder* const dec, int row) {
645   const uint32_t* const rows = dec->pixels_ + dec->width_ * dec->last_row_;
646   const int num_rows = row - dec->last_row_;
647
648   if (num_rows <= 0) return;  // Nothing to be done.
649   ApplyInverseTransforms(dec, num_rows, rows);
650
651   // Emit output.
652   {
653     VP8Io* const io = dec->io_;
654     const uint32_t* rows_data = dec->argb_cache_;
655     if (!SetCropWindow(io, dec->last_row_, row, &rows_data, io->width)) {
656       // Nothing to output (this time).
657     } else {
658       const WebPDecBuffer* const output = dec->output_;
659       const int in_stride = io->width * sizeof(*rows_data);
660       if (output->colorspace < MODE_YUV) {  // convert to RGBA
661         const WebPRGBABuffer* const buf = &output->u.RGBA;
662         uint8_t* const rgba = buf->rgba + dec->last_out_row_ * buf->stride;
663         const int num_rows_out = io->use_scaling ?
664             EmitRescaledRows(dec, rows_data, in_stride, io->mb_h,
665                              rgba, buf->stride) :
666             EmitRows(output->colorspace, rows_data, in_stride,
667                      io->mb_w, io->mb_h, rgba, buf->stride);
668         // Update 'last_out_row_'.
669         dec->last_out_row_ += num_rows_out;
670       } else {                              // convert to YUVA
671         dec->last_out_row_ = io->use_scaling ?
672             EmitRescaledRowsYUVA(dec, rows_data, in_stride, io->mb_h) :
673             EmitRowsYUVA(dec, rows_data, in_stride, io->mb_w, io->mb_h);
674       }
675       assert(dec->last_out_row_ <= output->height);
676     }
677   }
678
679   // Update 'last_row_'.
680   dec->last_row_ = row;
681   assert(dec->last_row_ <= dec->height_);
682 }
683
684 #define DECODE_DATA_FUNC(FUNC_NAME, TYPE, STORE_PIXEL)                         \
685 static int FUNC_NAME(VP8LDecoder* const dec, TYPE* const data, int width,      \
686                      int height, ProcessRowsFunc process_func) {               \
687   int ok = 1;                                                                  \
688   int col = 0, row = 0;                                                        \
689   VP8LBitReader* const br = &dec->br_;                                         \
690   VP8LMetadata* const hdr = &dec->hdr_;                                        \
691   HTreeGroup* htree_group = hdr->htree_groups_;                                \
692   TYPE* src = data;                                                            \
693   TYPE* last_cached = data;                                                    \
694   TYPE* const src_end = data + width * height;                                 \
695   const int len_code_limit = NUM_LITERAL_CODES + NUM_LENGTH_CODES;             \
696   const int color_cache_limit = len_code_limit + hdr->color_cache_size_;       \
697   VP8LColorCache* const color_cache =                                          \
698       (hdr->color_cache_size_ > 0) ? &hdr->color_cache_ : NULL;                \
699   const int mask = hdr->huffman_mask_;                                         \
700   assert(htree_group != NULL);                                                 \
701   while (!br->eos_ && src < src_end) {                                         \
702     int code;                                                                  \
703     /* Only update when changing tile. Note we could use this test:        */  \
704     /* if "((((prev_col ^ col) | prev_row ^ row)) > mask)" -> tile changed */  \
705     /* but that's actually slower and needs storing the previous col/row.  */  \
706     if ((col & mask) == 0) {                                                   \
707       htree_group = GetHtreeGroupForPos(hdr, col, row);                        \
708     }                                                                          \
709     VP8LFillBitWindow(br);                                                     \
710     code = ReadSymbol(&htree_group->htrees_[GREEN], br);                       \
711     if (code < NUM_LITERAL_CODES) {  /* Literal*/                              \
712       int red, green, blue, alpha;                                             \
713       red = ReadSymbol(&htree_group->htrees_[RED], br);                        \
714       green = code;                                                            \
715       VP8LFillBitWindow(br);                                                   \
716       blue = ReadSymbol(&htree_group->htrees_[BLUE], br);                      \
717       alpha = ReadSymbol(&htree_group->htrees_[ALPHA], br);                    \
718       *src = STORE_PIXEL(alpha, red, green, blue);                             \
719     AdvanceByOne:                                                              \
720       ++src;                                                                   \
721       ++col;                                                                   \
722       if (col >= width) {                                                      \
723         col = 0;                                                               \
724         ++row;                                                                 \
725         if ((process_func != NULL) && (row % NUM_ARGB_CACHE_ROWS == 0)) {      \
726           process_func(dec, row);                                              \
727         }                                                                      \
728         if (color_cache != NULL) {                                             \
729           while (last_cached < src) {                                          \
730             VP8LColorCacheInsert(color_cache, *last_cached++);                 \
731           }                                                                    \
732         }                                                                      \
733       }                                                                        \
734     } else if (code < len_code_limit) {  /* Backward reference */              \
735       int dist_code, dist;                                                     \
736       const int length_sym = code - NUM_LITERAL_CODES;                         \
737       const int length = GetCopyLength(length_sym, br);                        \
738       const int dist_symbol = ReadSymbol(&htree_group->htrees_[DIST], br);     \
739       VP8LFillBitWindow(br);                                                   \
740       dist_code = GetCopyDistance(dist_symbol, br);                            \
741       dist = PlaneCodeToDistance(width, dist_code);                            \
742       if (src - data < dist || src_end - src < length) {                       \
743         ok = 0;                                                                \
744         goto End;                                                              \
745       }                                                                        \
746       {                                                                        \
747         int i;                                                                 \
748         for (i = 0; i < length; ++i) src[i] = src[i - dist];                   \
749         src += length;                                                         \
750       }                                                                        \
751       col += length;                                                           \
752       while (col >= width) {                                                   \
753         col -= width;                                                          \
754         ++row;                                                                 \
755         if ((process_func != NULL) && (row % NUM_ARGB_CACHE_ROWS == 0)) {      \
756           process_func(dec, row);                                              \
757         }                                                                      \
758       }                                                                        \
759       if (src < src_end) {                                                     \
760         htree_group = GetHtreeGroupForPos(hdr, col, row);                      \
761         if (color_cache != NULL) {                                             \
762           while (last_cached < src) {                                          \
763             VP8LColorCacheInsert(color_cache, *last_cached++);                 \
764           }                                                                    \
765         }                                                                      \
766       }                                                                        \
767     } else if (code < color_cache_limit) {  /* Color cache */                  \
768       const int key = code - len_code_limit;                                   \
769       assert(color_cache != NULL);                                             \
770       while (last_cached < src) {                                              \
771         VP8LColorCacheInsert(color_cache, *last_cached++);                     \
772       }                                                                        \
773       *src = VP8LColorCacheLookup(color_cache, key);                           \
774       goto AdvanceByOne;                                                       \
775     } else {  /* Not reached */                                                \
776       ok = 0;                                                                  \
777       goto End;                                                                \
778     }                                                                          \
779     ok = !br->error_;                                                          \
780     if (!ok) goto End;                                                         \
781   }                                                                            \
782   /* Process the remaining rows corresponding to last row-block. */            \
783   if (process_func != NULL) process_func(dec, row);                            \
784 End:                                                                           \
785   if (br->error_ || !ok || (br->eos_ && src < src_end)) {                      \
786     ok = 0;                                                                    \
787     dec->status_ =                                                             \
788         (!br->eos_) ? VP8_STATUS_BITSTREAM_ERROR : VP8_STATUS_SUSPENDED;       \
789   } else if (src == src_end) {                                                 \
790     dec->state_ = READ_DATA;                                                   \
791   }                                                                            \
792   return ok;                                                                   \
793 }
794
795 static WEBP_INLINE uint32_t GetARGBPixel(int alpha, int red, int green,
796                                          int blue) {
797   return (alpha << 24) | (red << 16) | (green << 8) | blue;
798 }
799
800 static WEBP_INLINE uint8_t GetAlphaPixel(int alpha, int red, int green,
801                                          int blue) {
802   (void)alpha;
803   (void)red;
804   (void)blue;
805   return green;  // Alpha value is stored in green channel.
806 }
807
808 DECODE_DATA_FUNC(DecodeImageData, uint32_t, GetARGBPixel)
809 DECODE_DATA_FUNC(DecodeAlphaData, uint8_t, GetAlphaPixel)
810
811 #undef DECODE_DATA_FUNC
812
813 // -----------------------------------------------------------------------------
814 // VP8LTransform
815
816 static void ClearTransform(VP8LTransform* const transform) {
817   free(transform->data_);
818   transform->data_ = NULL;
819 }
820
821 // For security reason, we need to remap the color map to span
822 // the total possible bundled values, and not just the num_colors.
823 static int ExpandColorMap(int num_colors, VP8LTransform* const transform) {
824   int i;
825   const int final_num_colors = 1 << (8 >> transform->bits_);
826   uint32_t* const new_color_map =
827       (uint32_t*)WebPSafeMalloc((uint64_t)final_num_colors,
828                                 sizeof(*new_color_map));
829   if (new_color_map == NULL) {
830     return 0;
831   } else {
832     uint8_t* const data = (uint8_t*)transform->data_;
833     uint8_t* const new_data = (uint8_t*)new_color_map;
834     new_color_map[0] = transform->data_[0];
835     for (i = 4; i < 4 * num_colors; ++i) {
836       // Equivalent to AddPixelEq(), on a byte-basis.
837       new_data[i] = (data[i] + new_data[i - 4]) & 0xff;
838     }
839     for (; i < 4 * final_num_colors; ++i)
840       new_data[i] = 0;  // black tail.
841     free(transform->data_);
842     transform->data_ = new_color_map;
843   }
844   return 1;
845 }
846
847 static int ReadTransform(int* const xsize, int const* ysize,
848                          VP8LDecoder* const dec) {
849   int ok = 1;
850   VP8LBitReader* const br = &dec->br_;
851   VP8LTransform* transform = &dec->transforms_[dec->next_transform_];
852   const VP8LImageTransformType type =
853       (VP8LImageTransformType)VP8LReadBits(br, 2);
854
855   // Each transform type can only be present once in the stream.
856   if (dec->transforms_seen_ & (1U << type)) {
857     return 0;  // Already there, let's not accept the second same transform.
858   }
859   dec->transforms_seen_ |= (1U << type);
860
861   transform->type_ = type;
862   transform->xsize_ = *xsize;
863   transform->ysize_ = *ysize;
864   transform->data_ = NULL;
865   ++dec->next_transform_;
866   assert(dec->next_transform_ <= NUM_TRANSFORMS);
867
868   switch (type) {
869     case PREDICTOR_TRANSFORM:
870     case CROSS_COLOR_TRANSFORM:
871       transform->bits_ = VP8LReadBits(br, 3) + 2;
872       ok = DecodeImageStream(VP8LSubSampleSize(transform->xsize_,
873                                                transform->bits_),
874                              VP8LSubSampleSize(transform->ysize_,
875                                                transform->bits_),
876                              0, dec, &transform->data_);
877       break;
878     case COLOR_INDEXING_TRANSFORM: {
879        const int num_colors = VP8LReadBits(br, 8) + 1;
880        const int bits = (num_colors > 16) ? 0
881                       : (num_colors > 4) ? 1
882                       : (num_colors > 2) ? 2
883                       : 3;
884        *xsize = VP8LSubSampleSize(transform->xsize_, bits);
885        transform->bits_ = bits;
886        ok = DecodeImageStream(num_colors, 1, 0, dec, &transform->data_);
887        ok = ok && ExpandColorMap(num_colors, transform);
888       break;
889     }
890     case SUBTRACT_GREEN:
891       break;
892     default:
893       assert(0);    // can't happen
894       break;
895   }
896
897   return ok;
898 }
899
900 // -----------------------------------------------------------------------------
901 // VP8LMetadata
902
903 static void InitMetadata(VP8LMetadata* const hdr) {
904   assert(hdr);
905   memset(hdr, 0, sizeof(*hdr));
906 }
907
908 static void ClearMetadata(VP8LMetadata* const hdr) {
909   assert(hdr);
910
911   free(hdr->huffman_image_);
912   DeleteHtreeGroups(hdr->htree_groups_, hdr->num_htree_groups_);
913   VP8LColorCacheClear(&hdr->color_cache_);
914   InitMetadata(hdr);
915 }
916
917 // -----------------------------------------------------------------------------
918 // VP8LDecoder
919
920 VP8LDecoder* VP8LNew(void) {
921   VP8LDecoder* const dec = (VP8LDecoder*)calloc(1, sizeof(*dec));
922   if (dec == NULL) return NULL;
923   dec->status_ = VP8_STATUS_OK;
924   dec->action_ = READ_DIM;
925   dec->state_ = READ_DIM;
926   return dec;
927 }
928
929 void VP8LClear(VP8LDecoder* const dec) {
930   int i;
931   if (dec == NULL) return;
932   ClearMetadata(&dec->hdr_);
933
934   free(dec->pixels_);
935   dec->pixels_ = NULL;
936   for (i = 0; i < dec->next_transform_; ++i) {
937     ClearTransform(&dec->transforms_[i]);
938   }
939   dec->next_transform_ = 0;
940   dec->transforms_seen_ = 0;
941
942   free(dec->rescaler_memory);
943   dec->rescaler_memory = NULL;
944
945   dec->output_ = NULL;   // leave no trace behind
946 }
947
948 void VP8LDelete(VP8LDecoder* const dec) {
949   if (dec != NULL) {
950     VP8LClear(dec);
951     free(dec);
952   }
953 }
954
955 static void UpdateDecoder(VP8LDecoder* const dec, int width, int height) {
956   VP8LMetadata* const hdr = &dec->hdr_;
957   const int num_bits = hdr->huffman_subsample_bits_;
958   dec->width_ = width;
959   dec->height_ = height;
960
961   hdr->huffman_xsize_ = VP8LSubSampleSize(width, num_bits);
962   hdr->huffman_mask_ = (num_bits == 0) ? ~0 : (1 << num_bits) - 1;
963 }
964
965 static int DecodeImageStream(int xsize, int ysize,
966                              int is_level0,
967                              VP8LDecoder* const dec,
968                              uint32_t** const decoded_data) {
969   int ok = 1;
970   int transform_xsize = xsize;
971   int transform_ysize = ysize;
972   VP8LBitReader* const br = &dec->br_;
973   VP8LMetadata* const hdr = &dec->hdr_;
974   uint32_t* data = NULL;
975   int color_cache_bits = 0;
976
977   // Read the transforms (may recurse).
978   if (is_level0) {
979     while (ok && VP8LReadBits(br, 1)) {
980       ok = ReadTransform(&transform_xsize, &transform_ysize, dec);
981     }
982   }
983
984   // Color cache
985   if (ok && VP8LReadBits(br, 1)) {
986     color_cache_bits = VP8LReadBits(br, 4);
987     ok = (color_cache_bits >= 1 && color_cache_bits <= MAX_CACHE_BITS);
988     if (!ok) {
989       dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
990       goto End;
991     }
992   }
993
994   // Read the Huffman codes (may recurse).
995   ok = ok && ReadHuffmanCodes(dec, transform_xsize, transform_ysize,
996                               color_cache_bits, is_level0);
997   if (!ok) {
998     dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
999     goto End;
1000   }
1001
1002   // Finish setting up the color-cache
1003   if (color_cache_bits > 0) {
1004     hdr->color_cache_size_ = 1 << color_cache_bits;
1005     if (!VP8LColorCacheInit(&hdr->color_cache_, color_cache_bits)) {
1006       dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
1007       ok = 0;
1008       goto End;
1009     }
1010   } else {
1011     hdr->color_cache_size_ = 0;
1012   }
1013   UpdateDecoder(dec, transform_xsize, transform_ysize);
1014
1015   if (is_level0) {   // level 0 complete
1016     dec->state_ = READ_HDR;
1017     goto End;
1018   }
1019
1020   {
1021     const uint64_t total_size = (uint64_t)transform_xsize * transform_ysize;
1022     data = (uint32_t*)WebPSafeMalloc(total_size, sizeof(*data));
1023     if (data == NULL) {
1024       dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
1025       ok = 0;
1026       goto End;
1027     }
1028   }
1029
1030   // Use the Huffman trees to decode the LZ77 encoded data.
1031   ok = DecodeImageData(dec, data, transform_xsize, transform_ysize, NULL);
1032   ok = ok && !br->error_;
1033
1034  End:
1035
1036   if (!ok) {
1037     free(data);
1038     ClearMetadata(hdr);
1039     // If not enough data (br.eos_) resulted in BIT_STREAM_ERROR, update the
1040     // status appropriately.
1041     if (dec->status_ == VP8_STATUS_BITSTREAM_ERROR && dec->br_.eos_) {
1042       dec->status_ = VP8_STATUS_SUSPENDED;
1043     }
1044   } else {
1045     if (decoded_data != NULL) {
1046       *decoded_data = data;
1047     } else {
1048       // We allocate image data in this function only for transforms. At level 0
1049       // (that is: not the transforms), we shouldn't have allocated anything.
1050       assert(data == NULL);
1051       assert(is_level0);
1052     }
1053     if (!is_level0) ClearMetadata(hdr);  // Clean up temporary data behind.
1054   }
1055   return ok;
1056 }
1057
1058 //------------------------------------------------------------------------------
1059 // Allocate internal buffers dec->pixels_ and dec->argb_cache_.
1060 static int AllocateInternalBuffers(VP8LDecoder* const dec, int final_width,
1061                                    size_t bytes_per_pixel) {
1062   const int argb_cache_needed = (bytes_per_pixel == sizeof(uint32_t));
1063   const uint64_t num_pixels = (uint64_t)dec->width_ * dec->height_;
1064   // Scratch buffer corresponding to top-prediction row for transforming the
1065   // first row in the row-blocks. Not needed for paletted alpha.
1066   const uint64_t cache_top_pixels =
1067       argb_cache_needed ? (uint16_t)final_width : 0ULL;
1068   // Scratch buffer for temporary BGRA storage. Not needed for paletted alpha.
1069   const uint64_t cache_pixels =
1070       argb_cache_needed ? (uint64_t)final_width * NUM_ARGB_CACHE_ROWS : 0ULL;
1071   const uint64_t total_num_pixels =
1072       num_pixels + cache_top_pixels + cache_pixels;
1073
1074   assert(dec->width_ <= final_width);
1075   dec->pixels_ = (uint32_t*)WebPSafeMalloc(total_num_pixels, bytes_per_pixel);
1076   if (dec->pixels_ == NULL) {
1077     dec->argb_cache_ = NULL;    // for sanity check
1078     dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
1079     return 0;
1080   }
1081   dec->argb_cache_ =
1082       argb_cache_needed ? dec->pixels_ + num_pixels + cache_top_pixels : NULL;
1083   return 1;
1084 }
1085
1086 //------------------------------------------------------------------------------
1087
1088 // Special row-processing that only stores the alpha data.
1089 static void ExtractAlphaRows(VP8LDecoder* const dec, int row) {
1090   const int num_rows = row - dec->last_row_;
1091   const uint32_t* const in = dec->pixels_ + dec->width_ * dec->last_row_;
1092
1093   if (num_rows <= 0) return;  // Nothing to be done.
1094   ApplyInverseTransforms(dec, num_rows, in);
1095
1096   // Extract alpha (which is stored in the green plane).
1097   {
1098     const int width = dec->io_->width;      // the final width (!= dec->width_)
1099     const int cache_pixs = width * num_rows;
1100     uint8_t* const dst = (uint8_t*)dec->io_->opaque + width * dec->last_row_;
1101     const uint32_t* const src = dec->argb_cache_;
1102     int i;
1103     for (i = 0; i < cache_pixs; ++i) dst[i] = (src[i] >> 8) & 0xff;
1104   }
1105   dec->last_row_ = dec->last_out_row_ = row;
1106 }
1107
1108 // Row-processing for the special case when alpha data contains only one
1109 // transform: color indexing.
1110 static void ExtractPalettedAlphaRows(VP8LDecoder* const dec, int row) {
1111   const int num_rows = row - dec->last_row_;
1112   const uint8_t* const in =
1113       (uint8_t*)dec->pixels_ + dec->width_ * dec->last_row_;
1114   if (num_rows <= 0) return;  // Nothing to be done.
1115   ApplyInverseTransformsAlpha(dec, num_rows, in);
1116   dec->last_row_ = dec->last_out_row_ = row;
1117 }
1118
1119 int VP8LDecodeAlphaImageStream(int width, int height, const uint8_t* const data,
1120                                size_t data_size, uint8_t* const output) {
1121   VP8Io io;
1122   int ok = 0;
1123   VP8LDecoder* const dec = VP8LNew();
1124   size_t bytes_per_pixel = sizeof(uint32_t);  // Default: BGRA mode.
1125   if (dec == NULL) return 0;
1126
1127   dec->width_ = width;
1128   dec->height_ = height;
1129   dec->io_ = &io;
1130
1131   VP8InitIo(&io);
1132   WebPInitCustomIo(NULL, &io);    // Just a sanity Init. io won't be used.
1133   io.opaque = output;
1134   io.width = width;
1135   io.height = height;
1136
1137   dec->status_ = VP8_STATUS_OK;
1138   VP8LInitBitReader(&dec->br_, data, data_size);
1139
1140   dec->action_ = READ_HDR;
1141   if (!DecodeImageStream(width, height, 1, dec, NULL)) goto Err;
1142
1143   // Special case: if alpha data contains only the color indexing transform
1144   // (a frequent case), we will use DecodeAlphaData() method that only needs
1145   // allocation of 1 byte per pixel (alpha channel).
1146   if (dec->next_transform_ == 1 &&
1147       dec->transforms_[0].type_ == COLOR_INDEXING_TRANSFORM) {
1148     bytes_per_pixel = sizeof(uint8_t);
1149   }
1150
1151   // Allocate internal buffers (note that dec->width_ may have changed here).
1152   if (!AllocateInternalBuffers(dec, width, bytes_per_pixel)) goto Err;
1153
1154   // Decode (with special row processing).
1155   dec->action_ = READ_DATA;
1156   ok = (bytes_per_pixel == sizeof(uint8_t)) ?
1157       DecodeAlphaData(dec, (uint8_t*)dec->pixels_, dec->width_, dec->height_,
1158                       ExtractPalettedAlphaRows) :
1159       DecodeImageData(dec, dec->pixels_, dec->width_, dec->height_,
1160                       ExtractAlphaRows);
1161
1162  Err:
1163   VP8LDelete(dec);
1164   return ok;
1165 }
1166
1167 //------------------------------------------------------------------------------
1168
1169 int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io) {
1170   int width, height, has_alpha;
1171
1172   if (dec == NULL) return 0;
1173   if (io == NULL) {
1174     dec->status_ = VP8_STATUS_INVALID_PARAM;
1175     return 0;
1176   }
1177
1178   dec->io_ = io;
1179   dec->status_ = VP8_STATUS_OK;
1180   VP8LInitBitReader(&dec->br_, io->data, io->data_size);
1181   if (!ReadImageInfo(&dec->br_, &width, &height, &has_alpha)) {
1182     dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
1183     goto Error;
1184   }
1185   dec->state_ = READ_DIM;
1186   io->width = width;
1187   io->height = height;
1188
1189   dec->action_ = READ_HDR;
1190   if (!DecodeImageStream(width, height, 1, dec, NULL)) goto Error;
1191   return 1;
1192
1193  Error:
1194   VP8LClear(dec);
1195   assert(dec->status_ != VP8_STATUS_OK);
1196   return 0;
1197 }
1198
1199 int VP8LDecodeImage(VP8LDecoder* const dec) {
1200   const size_t bytes_per_pixel = sizeof(uint32_t);
1201   VP8Io* io = NULL;
1202   WebPDecParams* params = NULL;
1203
1204   // Sanity checks.
1205   if (dec == NULL) return 0;
1206
1207   io = dec->io_;
1208   assert(io != NULL);
1209   params = (WebPDecParams*)io->opaque;
1210   assert(params != NULL);
1211   dec->output_ = params->output;
1212   assert(dec->output_ != NULL);
1213
1214   // Initialization.
1215   if (!WebPIoInitFromOptions(params->options, io, MODE_BGRA)) {
1216     dec->status_ = VP8_STATUS_INVALID_PARAM;
1217     goto Err;
1218   }
1219
1220   if (!AllocateInternalBuffers(dec, io->width, bytes_per_pixel)) goto Err;
1221
1222   if (io->use_scaling && !AllocateAndInitRescaler(dec, io)) goto Err;
1223
1224   // Decode.
1225   dec->action_ = READ_DATA;
1226   if (!DecodeImageData(dec, dec->pixels_, dec->width_, dec->height_,
1227                        ProcessRows)) {
1228     goto Err;
1229   }
1230
1231   // Cleanup.
1232   params->last_y = dec->last_out_row_;
1233   VP8LClear(dec);
1234   return 1;
1235
1236  Err:
1237   VP8LClear(dec);
1238   assert(dec->status_ != VP8_STATUS_OK);
1239   return 0;
1240 }
1241
1242 //------------------------------------------------------------------------------
1243
1244 #if defined(__cplusplus) || defined(c_plusplus)
1245 }    // extern "C"
1246 #endif