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