1 // Copyright 2012 Google Inc. All Rights Reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
10 // main entry for the decoder
12 // Authors: Vikas Arora (vikaas.arora@gmail.com)
13 // Jyrki Alakuijala (jyrki@google.com)
18 #include "../dsp/lossless.h"
19 #include "../dsp/yuv.h"
20 #include "../utils/huffman.h"
21 #include "../utils/utils.h"
23 #if defined(__cplusplus) || defined(c_plusplus)
27 #define NUM_ARGB_CACHE_ROWS 16
29 static const int kCodeLengthLiterals = 16;
30 static const int kCodeLengthRepeatCode = 16;
31 static const int kCodeLengthExtraBits[3] = { 2, 3, 7 };
32 static const int kCodeLengthRepeatOffsets[3] = { 3, 3, 11 };
34 // -----------------------------------------------------------------------------
35 // Five Huffman codes are used at each meta code:
36 // 1. green + length prefix codes + color cache codes,
40 // 5. distance prefix codes.
49 static const uint16_t kAlphabetSize[HUFFMAN_CODES_PER_META_CODE] = {
50 NUM_LITERAL_CODES + NUM_LENGTH_CODES,
51 NUM_LITERAL_CODES, NUM_LITERAL_CODES, NUM_LITERAL_CODES,
56 #define NUM_CODE_LENGTH_CODES 19
57 static const uint8_t kCodeLengthCodeOrder[NUM_CODE_LENGTH_CODES] = {
58 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
61 #define CODE_TO_PLANE_CODES 120
62 static const uint8_t code_to_plane_lut[CODE_TO_PLANE_CODES] = {
63 0x18, 0x07, 0x17, 0x19, 0x28, 0x06, 0x27, 0x29, 0x16, 0x1a,
64 0x26, 0x2a, 0x38, 0x05, 0x37, 0x39, 0x15, 0x1b, 0x36, 0x3a,
65 0x25, 0x2b, 0x48, 0x04, 0x47, 0x49, 0x14, 0x1c, 0x35, 0x3b,
66 0x46, 0x4a, 0x24, 0x2c, 0x58, 0x45, 0x4b, 0x34, 0x3c, 0x03,
67 0x57, 0x59, 0x13, 0x1d, 0x56, 0x5a, 0x23, 0x2d, 0x44, 0x4c,
68 0x55, 0x5b, 0x33, 0x3d, 0x68, 0x02, 0x67, 0x69, 0x12, 0x1e,
69 0x66, 0x6a, 0x22, 0x2e, 0x54, 0x5c, 0x43, 0x4d, 0x65, 0x6b,
70 0x32, 0x3e, 0x78, 0x01, 0x77, 0x79, 0x53, 0x5d, 0x11, 0x1f,
71 0x64, 0x6c, 0x42, 0x4e, 0x76, 0x7a, 0x21, 0x2f, 0x75, 0x7b,
72 0x31, 0x3f, 0x63, 0x6d, 0x52, 0x5e, 0x00, 0x74, 0x7c, 0x41,
73 0x4f, 0x10, 0x20, 0x62, 0x6e, 0x30, 0x73, 0x7d, 0x51, 0x5f,
74 0x40, 0x72, 0x7e, 0x61, 0x6f, 0x50, 0x71, 0x7f, 0x60, 0x70
77 static int DecodeImageStream(int xsize, int ysize,
79 VP8LDecoder* const dec,
80 uint32_t** const decoded_data);
82 //------------------------------------------------------------------------------
84 int VP8LCheckSignature(const uint8_t* const data, size_t size) {
85 return (size >= VP8L_FRAME_HEADER_SIZE &&
86 data[0] == VP8L_MAGIC_BYTE &&
87 (data[4] >> 5) == 0); // version
90 static int ReadImageInfo(VP8LBitReader* const br,
91 int* const width, int* const height,
92 int* const has_alpha) {
93 if (VP8LReadBits(br, 8) != VP8L_MAGIC_BYTE) return 0;
94 *width = VP8LReadBits(br, VP8L_IMAGE_SIZE_BITS) + 1;
95 *height = VP8LReadBits(br, VP8L_IMAGE_SIZE_BITS) + 1;
96 *has_alpha = VP8LReadBits(br, 1);
97 if (VP8LReadBits(br, VP8L_VERSION_BITS) != 0) return 0;
101 int VP8LGetInfo(const uint8_t* data, size_t data_size,
102 int* const width, int* const height, int* const has_alpha) {
103 if (data == NULL || data_size < VP8L_FRAME_HEADER_SIZE) {
104 return 0; // not enough data
105 } else if (!VP8LCheckSignature(data, data_size)) {
106 return 0; // bad signature
110 VP8LInitBitReader(&br, data, data_size);
111 if (!ReadImageInfo(&br, &w, &h, &a)) {
114 if (width != NULL) *width = w;
115 if (height != NULL) *height = h;
116 if (has_alpha != NULL) *has_alpha = a;
121 //------------------------------------------------------------------------------
123 static WEBP_INLINE int GetCopyDistance(int distance_symbol,
124 VP8LBitReader* const br) {
125 int extra_bits, offset;
126 if (distance_symbol < 4) {
127 return distance_symbol + 1;
129 extra_bits = (distance_symbol - 2) >> 1;
130 offset = (2 + (distance_symbol & 1)) << extra_bits;
131 return offset + VP8LReadBits(br, extra_bits) + 1;
134 static WEBP_INLINE int GetCopyLength(int length_symbol,
135 VP8LBitReader* const br) {
136 // Length and distance prefixes are encoded the same way.
137 return GetCopyDistance(length_symbol, br);
140 static WEBP_INLINE int PlaneCodeToDistance(int xsize, int plane_code) {
141 if (plane_code > CODE_TO_PLANE_CODES) {
142 return plane_code - CODE_TO_PLANE_CODES;
144 const int dist_code = code_to_plane_lut[plane_code - 1];
145 const int yoffset = dist_code >> 4;
146 const int xoffset = 8 - (dist_code & 0xf);
147 const int dist = yoffset * xsize + xoffset;
148 return (dist >= 1) ? dist : 1;
152 //------------------------------------------------------------------------------
153 // Decodes the next Huffman code from bit-stream.
154 // FillBitWindow(br) needs to be called at minimum every second call
155 // to ReadSymbol, in order to pre-fetch enough bits.
156 static WEBP_INLINE int ReadSymbol(const HuffmanTree* tree,
157 VP8LBitReader* const br) {
158 const HuffmanTreeNode* node = tree->root_;
160 uint32_t bits = VP8LPrefetchBits(br);
161 assert(node != NULL);
162 while (!HuffmanTreeNodeIsLeaf(node)) {
163 node = HuffmanTreeNextNode(node, bits & 1);
167 VP8LDiscardBits(br, num_bits);
168 return node->symbol_;
171 static int ReadHuffmanCodeLengths(
172 VP8LDecoder* const dec, const int* const code_length_code_lengths,
173 int num_symbols, int* const code_lengths) {
175 VP8LBitReader* const br = &dec->br_;
178 int prev_code_len = DEFAULT_CODE_LENGTH;
181 if (!HuffmanTreeBuildImplicit(&tree, code_length_code_lengths,
182 NUM_CODE_LENGTH_CODES)) {
183 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
187 if (VP8LReadBits(br, 1)) { // use length
188 const int length_nbits = 2 + 2 * VP8LReadBits(br, 3);
189 max_symbol = 2 + VP8LReadBits(br, length_nbits);
190 if (max_symbol > num_symbols) {
191 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
195 max_symbol = num_symbols;
199 while (symbol < num_symbols) {
201 if (max_symbol-- == 0) break;
202 VP8LFillBitWindow(br);
203 code_len = ReadSymbol(&tree, br);
204 if (code_len < kCodeLengthLiterals) {
205 code_lengths[symbol++] = code_len;
206 if (code_len != 0) prev_code_len = code_len;
208 const int use_prev = (code_len == kCodeLengthRepeatCode);
209 const int slot = code_len - kCodeLengthLiterals;
210 const int extra_bits = kCodeLengthExtraBits[slot];
211 const int repeat_offset = kCodeLengthRepeatOffsets[slot];
212 int repeat = VP8LReadBits(br, extra_bits) + repeat_offset;
213 if (symbol + repeat > num_symbols) {
214 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
217 const int length = use_prev ? prev_code_len : 0;
218 while (repeat-- > 0) code_lengths[symbol++] = length;
225 HuffmanTreeRelease(&tree);
229 static int ReadHuffmanCode(int alphabet_size, VP8LDecoder* const dec,
230 HuffmanTree* const tree) {
232 VP8LBitReader* const br = &dec->br_;
233 const int simple_code = VP8LReadBits(br, 1);
235 if (simple_code) { // Read symbols, codes & code lengths directly.
239 const int num_symbols = VP8LReadBits(br, 1) + 1;
240 const int first_symbol_len_code = VP8LReadBits(br, 1);
241 // The first code is either 1 bit or 8 bit code.
242 symbols[0] = VP8LReadBits(br, (first_symbol_len_code == 0) ? 1 : 8);
244 code_lengths[0] = num_symbols - 1;
245 // The second code (if present), is always 8 bit long.
246 if (num_symbols == 2) {
247 symbols[1] = VP8LReadBits(br, 8);
249 code_lengths[1] = num_symbols - 1;
251 ok = HuffmanTreeBuildExplicit(tree, code_lengths, codes, symbols,
252 alphabet_size, num_symbols);
253 } else { // Decode Huffman-coded code lengths.
254 int* code_lengths = NULL;
256 int code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
257 const int num_codes = VP8LReadBits(br, 4) + 4;
258 if (num_codes > NUM_CODE_LENGTH_CODES) {
259 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
264 (int*)WebPSafeCalloc((uint64_t)alphabet_size, sizeof(*code_lengths));
265 if (code_lengths == NULL) {
266 dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
270 for (i = 0; i < num_codes; ++i) {
271 code_length_code_lengths[kCodeLengthCodeOrder[i]] = VP8LReadBits(br, 3);
273 ok = ReadHuffmanCodeLengths(dec, code_length_code_lengths, alphabet_size,
276 ok = HuffmanTreeBuildImplicit(tree, code_lengths, alphabet_size);
280 ok = ok && !br->error_;
282 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
288 static void DeleteHtreeGroups(HTreeGroup* htree_groups, int num_htree_groups) {
289 if (htree_groups != NULL) {
291 for (i = 0; i < num_htree_groups; ++i) {
292 HuffmanTree* const htrees = htree_groups[i].htrees_;
293 for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
294 HuffmanTreeRelease(&htrees[j]);
301 static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
302 int color_cache_bits, int allow_recursion) {
304 VP8LBitReader* const br = &dec->br_;
305 VP8LMetadata* const hdr = &dec->hdr_;
306 uint32_t* huffman_image = NULL;
307 HTreeGroup* htree_groups = NULL;
308 int num_htree_groups = 1;
310 if (allow_recursion && VP8LReadBits(br, 1)) {
311 // use meta Huffman codes.
312 const int huffman_precision = VP8LReadBits(br, 3) + 2;
313 const int huffman_xsize = VP8LSubSampleSize(xsize, huffman_precision);
314 const int huffman_ysize = VP8LSubSampleSize(ysize, huffman_precision);
315 const int huffman_pixs = huffman_xsize * huffman_ysize;
316 if (!DecodeImageStream(huffman_xsize, huffman_ysize, 0, dec,
318 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
321 hdr->huffman_subsample_bits_ = huffman_precision;
322 for (i = 0; i < huffman_pixs; ++i) {
323 // The huffman data is stored in red and green bytes.
324 const int group = (huffman_image[i] >> 8) & 0xffff;
325 huffman_image[i] = group;
326 if (group >= num_htree_groups) {
327 num_htree_groups = group + 1;
332 if (br->error_) goto Error;
334 assert(num_htree_groups <= 0x10000);
336 (HTreeGroup*)WebPSafeCalloc((uint64_t)num_htree_groups,
337 sizeof(*htree_groups));
338 if (htree_groups == NULL) {
339 dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
343 for (i = 0; i < num_htree_groups; ++i) {
344 HuffmanTree* const htrees = htree_groups[i].htrees_;
345 for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
346 int alphabet_size = kAlphabetSize[j];
347 if (j == 0 && color_cache_bits > 0) {
348 alphabet_size += 1 << color_cache_bits;
350 if (!ReadHuffmanCode(alphabet_size, dec, htrees + j)) goto Error;
354 // All OK. Finalize pointers and return.
355 hdr->huffman_image_ = huffman_image;
356 hdr->num_htree_groups_ = num_htree_groups;
357 hdr->htree_groups_ = htree_groups;
362 DeleteHtreeGroups(htree_groups, num_htree_groups);
366 //------------------------------------------------------------------------------
369 static int AllocateAndInitRescaler(VP8LDecoder* const dec, VP8Io* const io) {
370 const int num_channels = 4;
371 const int in_width = io->mb_w;
372 const int out_width = io->scaled_width;
373 const int in_height = io->mb_h;
374 const int out_height = io->scaled_height;
375 const uint64_t work_size = 2 * num_channels * (uint64_t)out_width;
376 int32_t* work; // Rescaler work area.
377 const uint64_t scaled_data_size = num_channels * (uint64_t)out_width;
378 uint32_t* scaled_data; // Temporary storage for scaled BGRA data.
379 const uint64_t memory_size = sizeof(*dec->rescaler) +
380 work_size * sizeof(*work) +
381 scaled_data_size * sizeof(*scaled_data);
382 uint8_t* memory = (uint8_t*)WebPSafeCalloc(memory_size, sizeof(*memory));
383 if (memory == NULL) {
384 dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
387 assert(dec->rescaler_memory == NULL);
388 dec->rescaler_memory = memory;
390 dec->rescaler = (WebPRescaler*)memory;
391 memory += sizeof(*dec->rescaler);
392 work = (int32_t*)memory;
393 memory += work_size * sizeof(*work);
394 scaled_data = (uint32_t*)memory;
396 WebPRescalerInit(dec->rescaler, in_width, in_height, (uint8_t*)scaled_data,
397 out_width, out_height, 0, num_channels,
398 in_width, out_width, in_height, out_height, work);
402 //------------------------------------------------------------------------------
405 // We have special "export" function since we need to convert from BGRA
406 static int Export(WebPRescaler* const rescaler, WEBP_CSP_MODE colorspace,
407 int rgba_stride, uint8_t* const rgba) {
408 const uint32_t* const src = (const uint32_t*)rescaler->dst;
409 const int dst_width = rescaler->dst_width;
410 int num_lines_out = 0;
411 while (WebPRescalerHasPendingOutput(rescaler)) {
412 uint8_t* const dst = rgba + num_lines_out * rgba_stride;
413 WebPRescalerExportRow(rescaler);
414 VP8LConvertFromBGRA(src, dst_width, colorspace, dst);
417 return num_lines_out;
421 static int EmitRescaledRows(const VP8LDecoder* const dec,
422 const uint32_t* const data, int in_stride, int mb_h,
423 uint8_t* const out, int out_stride) {
424 const WEBP_CSP_MODE colorspace = dec->output_->colorspace;
425 const uint8_t* const in = (const uint8_t*)data;
426 int num_lines_in = 0;
427 int num_lines_out = 0;
428 while (num_lines_in < mb_h) {
429 const uint8_t* const row_in = in + num_lines_in * in_stride;
430 uint8_t* const row_out = out + num_lines_out * out_stride;
431 num_lines_in += WebPRescalerImport(dec->rescaler, mb_h - num_lines_in,
433 num_lines_out += Export(dec->rescaler, colorspace, out_stride, row_out);
435 return num_lines_out;
438 // Emit rows without any scaling.
439 static int EmitRows(WEBP_CSP_MODE colorspace,
440 const uint32_t* const data, int in_stride,
442 uint8_t* const out, int out_stride) {
444 const uint8_t* row_in = (const uint8_t*)data;
445 uint8_t* row_out = out;
446 while (lines-- > 0) {
447 VP8LConvertFromBGRA((const uint32_t*)row_in, mb_w, colorspace, row_out);
449 row_out += out_stride;
451 return mb_h; // Num rows out == num rows in.
454 //------------------------------------------------------------------------------
457 static void ConvertToYUVA(const uint32_t* const src, int width, int y_pos,
458 const WebPDecBuffer* const output) {
459 const WebPYUVABuffer* const buf = &output->u.YUVA;
460 // first, the luma plane
463 uint8_t* const y = buf->y + y_pos * buf->y_stride;
464 for (i = 0; i < width; ++i) {
465 const uint32_t p = src[i];
466 y[i] = VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >> 0) & 0xff);
472 uint8_t* const u = buf->u + (y_pos >> 1) * buf->u_stride;
473 uint8_t* const v = buf->v + (y_pos >> 1) * buf->v_stride;
474 const int uv_width = width >> 1;
476 for (i = 0; i < uv_width; ++i) {
477 const uint32_t v0 = src[2 * i + 0];
478 const uint32_t v1 = src[2 * i + 1];
479 // VP8RGBToU/V expects four accumulated pixels. Hence we need to
480 // scale r/g/b value by a factor 2. We just shift v0/v1 one bit less.
481 const int r = ((v0 >> 15) & 0x1fe) + ((v1 >> 15) & 0x1fe);
482 const int g = ((v0 >> 7) & 0x1fe) + ((v1 >> 7) & 0x1fe);
483 const int b = ((v0 << 1) & 0x1fe) + ((v1 << 1) & 0x1fe);
484 if (!(y_pos & 1)) { // even lines: store values
485 u[i] = VP8RGBToU(r, g, b);
486 v[i] = VP8RGBToV(r, g, b);
487 } else { // odd lines: average with previous values
488 const int tmp_u = VP8RGBToU(r, g, b);
489 const int tmp_v = VP8RGBToV(r, g, b);
490 // Approximated average-of-four. But it's an acceptable diff.
491 u[i] = (u[i] + tmp_u + 1) >> 1;
492 v[i] = (v[i] + tmp_v + 1) >> 1;
495 if (width & 1) { // last pixel
496 const uint32_t v0 = src[2 * i + 0];
497 const int r = (v0 >> 14) & 0x3fc;
498 const int g = (v0 >> 6) & 0x3fc;
499 const int b = (v0 << 2) & 0x3fc;
500 if (!(y_pos & 1)) { // even lines
501 u[i] = VP8RGBToU(r, g, b);
502 v[i] = VP8RGBToV(r, g, b);
503 } else { // odd lines (note: we could just skip this)
504 const int tmp_u = VP8RGBToU(r, g, b);
505 const int tmp_v = VP8RGBToV(r, g, b);
506 u[i] = (u[i] + tmp_u + 1) >> 1;
507 v[i] = (v[i] + tmp_v + 1) >> 1;
511 // Lastly, store alpha if needed.
512 if (buf->a != NULL) {
514 uint8_t* const a = buf->a + y_pos * buf->a_stride;
515 for (i = 0; i < width; ++i) a[i] = (src[i] >> 24);
519 static int ExportYUVA(const VP8LDecoder* const dec, int y_pos) {
520 WebPRescaler* const rescaler = dec->rescaler;
521 const uint32_t* const src = (const uint32_t*)rescaler->dst;
522 const int dst_width = rescaler->dst_width;
523 int num_lines_out = 0;
524 while (WebPRescalerHasPendingOutput(rescaler)) {
525 WebPRescalerExportRow(rescaler);
526 ConvertToYUVA(src, dst_width, y_pos, dec->output_);
530 return num_lines_out;
533 static int EmitRescaledRowsYUVA(const VP8LDecoder* const dec,
534 const uint32_t* const data,
535 int in_stride, int mb_h) {
536 const uint8_t* const in = (const uint8_t*)data;
537 int num_lines_in = 0;
538 int y_pos = dec->last_out_row_;
539 while (num_lines_in < mb_h) {
540 const uint8_t* const row_in = in + num_lines_in * in_stride;
541 num_lines_in += WebPRescalerImport(dec->rescaler, mb_h - num_lines_in,
543 y_pos += ExportYUVA(dec, y_pos);
548 static int EmitRowsYUVA(const VP8LDecoder* const dec,
549 const uint32_t* const data, int in_stride,
550 int mb_w, int num_rows) {
551 int y_pos = dec->last_out_row_;
552 const uint8_t* row_in = (const uint8_t*)data;
553 while (num_rows-- > 0) {
554 ConvertToYUVA((const uint32_t*)row_in, mb_w, y_pos, dec->output_);
561 //------------------------------------------------------------------------------
564 // Sets io->mb_y, io->mb_h & io->mb_w according to start row, end row and
565 // crop options. Also updates the input data pointer, so that it points to the
566 // start of the cropped window.
567 // Note that 'pixel_stride' is in units of 'uint32_t' (and not 'bytes).
568 // Returns true if the crop window is not empty.
569 static int SetCropWindow(VP8Io* const io, int y_start, int y_end,
570 const uint32_t** const in_data, int pixel_stride) {
571 assert(y_start < y_end);
572 assert(io->crop_left < io->crop_right);
573 if (y_end > io->crop_bottom) {
574 y_end = io->crop_bottom; // make sure we don't overflow on last row.
576 if (y_start < io->crop_top) {
577 const int delta = io->crop_top - y_start;
578 y_start = io->crop_top;
579 *in_data += pixel_stride * delta;
581 if (y_start >= y_end) return 0; // Crop window is empty.
583 *in_data += io->crop_left;
585 io->mb_y = y_start - io->crop_top;
586 io->mb_w = io->crop_right - io->crop_left;
587 io->mb_h = y_end - y_start;
588 return 1; // Non-empty crop window.
591 //------------------------------------------------------------------------------
593 static WEBP_INLINE int GetMetaIndex(
594 const uint32_t* const image, int xsize, int bits, int x, int y) {
595 if (bits == 0) return 0;
596 return image[xsize * (y >> bits) + (x >> bits)];
599 static WEBP_INLINE HTreeGroup* GetHtreeGroupForPos(VP8LMetadata* const hdr,
601 const int meta_index = GetMetaIndex(hdr->huffman_image_, hdr->huffman_xsize_,
602 hdr->huffman_subsample_bits_, x, y);
603 assert(meta_index < hdr->num_htree_groups_);
604 return hdr->htree_groups_ + meta_index;
607 //------------------------------------------------------------------------------
608 // Main loop, with custom row-processing function
610 typedef void (*ProcessRowsFunc)(VP8LDecoder* const dec, int row);
612 static void ApplyInverseTransforms(VP8LDecoder* const dec, int num_rows,
613 const uint32_t* const rows) {
614 int n = dec->next_transform_;
615 const int cache_pixs = dec->width_ * num_rows;
616 const int start_row = dec->last_row_;
617 const int end_row = start_row + num_rows;
618 const uint32_t* rows_in = rows;
619 uint32_t* const rows_out = dec->argb_cache_;
621 // Inverse transforms.
622 // TODO: most transforms only need to operate on the cropped region only.
623 memcpy(rows_out, rows_in, cache_pixs * sizeof(*rows_out));
625 VP8LTransform* const transform = &dec->transforms_[n];
626 VP8LInverseTransform(transform, start_row, end_row, rows_in, rows_out);
631 // Special method for paletted alpha data.
632 static void ApplyInverseTransformsAlpha(VP8LDecoder* const dec, int num_rows,
633 const uint8_t* const rows) {
634 const int start_row = dec->last_row_;
635 const int end_row = start_row + num_rows;
636 const uint8_t* rows_in = rows;
637 uint8_t* rows_out = (uint8_t*)dec->io_->opaque + dec->io_->width * start_row;
638 VP8LTransform* const transform = &dec->transforms_[0];
639 assert(dec->next_transform_ == 1);
640 assert(transform->type_ == COLOR_INDEXING_TRANSFORM);
641 VP8LColorIndexInverseTransformAlpha(transform, start_row, end_row, rows_in,
645 // Processes (transforms, scales & color-converts) the rows decoded after the
647 static void ProcessRows(VP8LDecoder* const dec, int row) {
648 const uint32_t* const rows = dec->pixels_ + dec->width_ * dec->last_row_;
649 const int num_rows = row - dec->last_row_;
651 if (num_rows <= 0) return; // Nothing to be done.
652 ApplyInverseTransforms(dec, num_rows, rows);
656 VP8Io* const io = dec->io_;
657 const uint32_t* rows_data = dec->argb_cache_;
658 if (!SetCropWindow(io, dec->last_row_, row, &rows_data, io->width)) {
659 // Nothing to output (this time).
661 const WebPDecBuffer* const output = dec->output_;
662 const int in_stride = io->width * sizeof(*rows_data);
663 if (output->colorspace < MODE_YUV) { // convert to RGBA
664 const WebPRGBABuffer* const buf = &output->u.RGBA;
665 uint8_t* const rgba = buf->rgba + dec->last_out_row_ * buf->stride;
666 const int num_rows_out = io->use_scaling ?
667 EmitRescaledRows(dec, rows_data, in_stride, io->mb_h,
669 EmitRows(output->colorspace, rows_data, in_stride,
670 io->mb_w, io->mb_h, rgba, buf->stride);
671 // Update 'last_out_row_'.
672 dec->last_out_row_ += num_rows_out;
673 } else { // convert to YUVA
674 dec->last_out_row_ = io->use_scaling ?
675 EmitRescaledRowsYUVA(dec, rows_data, in_stride, io->mb_h) :
676 EmitRowsYUVA(dec, rows_data, in_stride, io->mb_w, io->mb_h);
678 assert(dec->last_out_row_ <= output->height);
682 // Update 'last_row_'.
683 dec->last_row_ = row;
684 assert(dec->last_row_ <= dec->height_);
687 #define DECODE_DATA_FUNC(FUNC_NAME, TYPE, STORE_PIXEL) \
688 static int FUNC_NAME(VP8LDecoder* const dec, TYPE* const data, int width, \
689 int height, ProcessRowsFunc process_func) { \
691 int col = 0, row = 0; \
692 VP8LBitReader* const br = &dec->br_; \
693 VP8LMetadata* const hdr = &dec->hdr_; \
694 HTreeGroup* htree_group = hdr->htree_groups_; \
696 TYPE* last_cached = data; \
697 TYPE* const src_end = data + width * height; \
698 const int len_code_limit = NUM_LITERAL_CODES + NUM_LENGTH_CODES; \
699 const int color_cache_limit = len_code_limit + hdr->color_cache_size_; \
700 VP8LColorCache* const color_cache = \
701 (hdr->color_cache_size_ > 0) ? &hdr->color_cache_ : NULL; \
702 const int mask = hdr->huffman_mask_; \
703 assert(htree_group != NULL); \
704 while (!br->eos_ && src < src_end) { \
706 /* Only update when changing tile. Note we could use this test: */ \
707 /* if "((((prev_col ^ col) | prev_row ^ row)) > mask)" -> tile changed */ \
708 /* but that's actually slower and needs storing the previous col/row. */ \
709 if ((col & mask) == 0) { \
710 htree_group = GetHtreeGroupForPos(hdr, col, row); \
712 VP8LFillBitWindow(br); \
713 code = ReadSymbol(&htree_group->htrees_[GREEN], br); \
714 if (code < NUM_LITERAL_CODES) { /* Literal*/ \
715 int red, green, blue, alpha; \
716 red = ReadSymbol(&htree_group->htrees_[RED], br); \
718 VP8LFillBitWindow(br); \
719 blue = ReadSymbol(&htree_group->htrees_[BLUE], br); \
720 alpha = ReadSymbol(&htree_group->htrees_[ALPHA], br); \
721 *src = STORE_PIXEL(alpha, red, green, blue); \
725 if (col >= width) { \
728 if ((process_func != NULL) && (row % NUM_ARGB_CACHE_ROWS == 0)) { \
729 process_func(dec, row); \
731 if (color_cache != NULL) { \
732 while (last_cached < src) { \
733 VP8LColorCacheInsert(color_cache, *last_cached++); \
737 } else if (code < len_code_limit) { /* Backward reference */ \
738 int dist_code, dist; \
739 const int length_sym = code - NUM_LITERAL_CODES; \
740 const int length = GetCopyLength(length_sym, br); \
741 const int dist_symbol = ReadSymbol(&htree_group->htrees_[DIST], br); \
742 VP8LFillBitWindow(br); \
743 dist_code = GetCopyDistance(dist_symbol, br); \
744 dist = PlaneCodeToDistance(width, dist_code); \
745 if (src - data < dist || src_end - src < length) { \
751 for (i = 0; i < length; ++i) src[i] = src[i - dist]; \
755 while (col >= width) { \
758 if ((process_func != NULL) && (row % NUM_ARGB_CACHE_ROWS == 0)) { \
759 process_func(dec, row); \
762 if (src < src_end) { \
763 htree_group = GetHtreeGroupForPos(hdr, col, row); \
764 if (color_cache != NULL) { \
765 while (last_cached < src) { \
766 VP8LColorCacheInsert(color_cache, *last_cached++); \
770 } else if (code < color_cache_limit) { /* Color cache */ \
771 const int key = code - len_code_limit; \
772 assert(color_cache != NULL); \
773 while (last_cached < src) { \
774 VP8LColorCacheInsert(color_cache, *last_cached++); \
776 *src = VP8LColorCacheLookup(color_cache, key); \
778 } else { /* Not reached */ \
785 /* Process the remaining rows corresponding to last row-block. */ \
786 if (process_func != NULL) process_func(dec, row); \
788 if (br->error_ || !ok || (br->eos_ && src < src_end)) { \
791 (!br->eos_) ? VP8_STATUS_BITSTREAM_ERROR : VP8_STATUS_SUSPENDED; \
792 } else if (src == src_end) { \
793 dec->state_ = READ_DATA; \
798 static WEBP_INLINE uint32_t GetARGBPixel(int alpha, int red, int green,
800 return (alpha << 24) | (red << 16) | (green << 8) | blue;
803 static WEBP_INLINE uint8_t GetAlphaPixel(int alpha, int red, int green,
808 return green; // Alpha value is stored in green channel.
811 DECODE_DATA_FUNC(DecodeImageData, uint32_t, GetARGBPixel)
812 DECODE_DATA_FUNC(DecodeAlphaData, uint8_t, GetAlphaPixel)
814 #undef DECODE_DATA_FUNC
816 // -----------------------------------------------------------------------------
819 static void ClearTransform(VP8LTransform* const transform) {
820 free(transform->data_);
821 transform->data_ = NULL;
824 // For security reason, we need to remap the color map to span
825 // the total possible bundled values, and not just the num_colors.
826 static int ExpandColorMap(int num_colors, VP8LTransform* const transform) {
828 const int final_num_colors = 1 << (8 >> transform->bits_);
829 uint32_t* const new_color_map =
830 (uint32_t*)WebPSafeMalloc((uint64_t)final_num_colors,
831 sizeof(*new_color_map));
832 if (new_color_map == NULL) {
835 uint8_t* const data = (uint8_t*)transform->data_;
836 uint8_t* const new_data = (uint8_t*)new_color_map;
837 new_color_map[0] = transform->data_[0];
838 for (i = 4; i < 4 * num_colors; ++i) {
839 // Equivalent to AddPixelEq(), on a byte-basis.
840 new_data[i] = (data[i] + new_data[i - 4]) & 0xff;
842 for (; i < 4 * final_num_colors; ++i)
843 new_data[i] = 0; // black tail.
844 free(transform->data_);
845 transform->data_ = new_color_map;
850 static int ReadTransform(int* const xsize, int const* ysize,
851 VP8LDecoder* const dec) {
853 VP8LBitReader* const br = &dec->br_;
854 VP8LTransform* transform = &dec->transforms_[dec->next_transform_];
855 const VP8LImageTransformType type =
856 (VP8LImageTransformType)VP8LReadBits(br, 2);
858 // Each transform type can only be present once in the stream.
859 if (dec->transforms_seen_ & (1U << type)) {
860 return 0; // Already there, let's not accept the second same transform.
862 dec->transforms_seen_ |= (1U << type);
864 transform->type_ = type;
865 transform->xsize_ = *xsize;
866 transform->ysize_ = *ysize;
867 transform->data_ = NULL;
868 ++dec->next_transform_;
869 assert(dec->next_transform_ <= NUM_TRANSFORMS);
872 case PREDICTOR_TRANSFORM:
873 case CROSS_COLOR_TRANSFORM:
874 transform->bits_ = VP8LReadBits(br, 3) + 2;
875 ok = DecodeImageStream(VP8LSubSampleSize(transform->xsize_,
877 VP8LSubSampleSize(transform->ysize_,
879 0, dec, &transform->data_);
881 case COLOR_INDEXING_TRANSFORM: {
882 const int num_colors = VP8LReadBits(br, 8) + 1;
883 const int bits = (num_colors > 16) ? 0
884 : (num_colors > 4) ? 1
885 : (num_colors > 2) ? 2
887 *xsize = VP8LSubSampleSize(transform->xsize_, bits);
888 transform->bits_ = bits;
889 ok = DecodeImageStream(num_colors, 1, 0, dec, &transform->data_);
890 ok = ok && ExpandColorMap(num_colors, transform);
896 assert(0); // can't happen
903 // -----------------------------------------------------------------------------
906 static void InitMetadata(VP8LMetadata* const hdr) {
908 memset(hdr, 0, sizeof(*hdr));
911 static void ClearMetadata(VP8LMetadata* const hdr) {
914 free(hdr->huffman_image_);
915 DeleteHtreeGroups(hdr->htree_groups_, hdr->num_htree_groups_);
916 VP8LColorCacheClear(&hdr->color_cache_);
920 // -----------------------------------------------------------------------------
923 VP8LDecoder* VP8LNew(void) {
924 VP8LDecoder* const dec = (VP8LDecoder*)calloc(1, sizeof(*dec));
925 if (dec == NULL) return NULL;
926 dec->status_ = VP8_STATUS_OK;
927 dec->action_ = READ_DIM;
928 dec->state_ = READ_DIM;
932 void VP8LClear(VP8LDecoder* const dec) {
934 if (dec == NULL) return;
935 ClearMetadata(&dec->hdr_);
939 for (i = 0; i < dec->next_transform_; ++i) {
940 ClearTransform(&dec->transforms_[i]);
942 dec->next_transform_ = 0;
943 dec->transforms_seen_ = 0;
945 free(dec->rescaler_memory);
946 dec->rescaler_memory = NULL;
948 dec->output_ = NULL; // leave no trace behind
951 void VP8LDelete(VP8LDecoder* const dec) {
958 static void UpdateDecoder(VP8LDecoder* const dec, int width, int height) {
959 VP8LMetadata* const hdr = &dec->hdr_;
960 const int num_bits = hdr->huffman_subsample_bits_;
962 dec->height_ = height;
964 hdr->huffman_xsize_ = VP8LSubSampleSize(width, num_bits);
965 hdr->huffman_mask_ = (num_bits == 0) ? ~0 : (1 << num_bits) - 1;
968 static int DecodeImageStream(int xsize, int ysize,
970 VP8LDecoder* const dec,
971 uint32_t** const decoded_data) {
973 int transform_xsize = xsize;
974 int transform_ysize = ysize;
975 VP8LBitReader* const br = &dec->br_;
976 VP8LMetadata* const hdr = &dec->hdr_;
977 uint32_t* data = NULL;
978 int color_cache_bits = 0;
980 // Read the transforms (may recurse).
982 while (ok && VP8LReadBits(br, 1)) {
983 ok = ReadTransform(&transform_xsize, &transform_ysize, dec);
988 if (ok && VP8LReadBits(br, 1)) {
989 color_cache_bits = VP8LReadBits(br, 4);
990 ok = (color_cache_bits >= 1 && color_cache_bits <= MAX_CACHE_BITS);
992 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
997 // Read the Huffman codes (may recurse).
998 ok = ok && ReadHuffmanCodes(dec, transform_xsize, transform_ysize,
999 color_cache_bits, is_level0);
1001 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
1005 // Finish setting up the color-cache
1006 if (color_cache_bits > 0) {
1007 hdr->color_cache_size_ = 1 << color_cache_bits;
1008 if (!VP8LColorCacheInit(&hdr->color_cache_, color_cache_bits)) {
1009 dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
1014 hdr->color_cache_size_ = 0;
1016 UpdateDecoder(dec, transform_xsize, transform_ysize);
1018 if (is_level0) { // level 0 complete
1019 dec->state_ = READ_HDR;
1024 const uint64_t total_size = (uint64_t)transform_xsize * transform_ysize;
1025 data = (uint32_t*)WebPSafeMalloc(total_size, sizeof(*data));
1027 dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
1033 // Use the Huffman trees to decode the LZ77 encoded data.
1034 ok = DecodeImageData(dec, data, transform_xsize, transform_ysize, NULL);
1035 ok = ok && !br->error_;
1042 // If not enough data (br.eos_) resulted in BIT_STREAM_ERROR, update the
1043 // status appropriately.
1044 if (dec->status_ == VP8_STATUS_BITSTREAM_ERROR && dec->br_.eos_) {
1045 dec->status_ = VP8_STATUS_SUSPENDED;
1048 if (decoded_data != NULL) {
1049 *decoded_data = data;
1051 // We allocate image data in this function only for transforms. At level 0
1052 // (that is: not the transforms), we shouldn't have allocated anything.
1053 assert(data == NULL);
1056 if (!is_level0) ClearMetadata(hdr); // Clean up temporary data behind.
1061 //------------------------------------------------------------------------------
1062 // Allocate internal buffers dec->pixels_ and dec->argb_cache_.
1063 static int AllocateInternalBuffers(VP8LDecoder* const dec, int final_width,
1064 size_t bytes_per_pixel) {
1065 const int argb_cache_needed = (bytes_per_pixel == sizeof(uint32_t));
1066 const uint64_t num_pixels = (uint64_t)dec->width_ * dec->height_;
1067 // Scratch buffer corresponding to top-prediction row for transforming the
1068 // first row in the row-blocks. Not needed for paletted alpha.
1069 const uint64_t cache_top_pixels =
1070 argb_cache_needed ? (uint16_t)final_width : 0ULL;
1071 // Scratch buffer for temporary BGRA storage. Not needed for paletted alpha.
1072 const uint64_t cache_pixels =
1073 argb_cache_needed ? (uint64_t)final_width * NUM_ARGB_CACHE_ROWS : 0ULL;
1074 const uint64_t total_num_pixels =
1075 num_pixels + cache_top_pixels + cache_pixels;
1077 assert(dec->width_ <= final_width);
1078 dec->pixels_ = (uint32_t*)WebPSafeMalloc(total_num_pixels, bytes_per_pixel);
1079 if (dec->pixels_ == NULL) {
1080 dec->argb_cache_ = NULL; // for sanity check
1081 dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
1085 argb_cache_needed ? dec->pixels_ + num_pixels + cache_top_pixels : NULL;
1089 //------------------------------------------------------------------------------
1091 // Special row-processing that only stores the alpha data.
1092 static void ExtractAlphaRows(VP8LDecoder* const dec, int row) {
1093 const int num_rows = row - dec->last_row_;
1094 const uint32_t* const in = dec->pixels_ + dec->width_ * dec->last_row_;
1096 if (num_rows <= 0) return; // Nothing to be done.
1097 ApplyInverseTransforms(dec, num_rows, in);
1099 // Extract alpha (which is stored in the green plane).
1101 const int width = dec->io_->width; // the final width (!= dec->width_)
1102 const int cache_pixs = width * num_rows;
1103 uint8_t* const dst = (uint8_t*)dec->io_->opaque + width * dec->last_row_;
1104 const uint32_t* const src = dec->argb_cache_;
1106 for (i = 0; i < cache_pixs; ++i) dst[i] = (src[i] >> 8) & 0xff;
1108 dec->last_row_ = dec->last_out_row_ = row;
1111 // Row-processing for the special case when alpha data contains only one
1112 // transform: color indexing.
1113 static void ExtractPalettedAlphaRows(VP8LDecoder* const dec, int row) {
1114 const int num_rows = row - dec->last_row_;
1115 const uint8_t* const in =
1116 (uint8_t*)dec->pixels_ + dec->width_ * dec->last_row_;
1117 if (num_rows <= 0) return; // Nothing to be done.
1118 ApplyInverseTransformsAlpha(dec, num_rows, in);
1119 dec->last_row_ = dec->last_out_row_ = row;
1122 int VP8LDecodeAlphaImageStream(int width, int height, const uint8_t* const data,
1123 size_t data_size, uint8_t* const output) {
1126 VP8LDecoder* const dec = VP8LNew();
1127 size_t bytes_per_pixel = sizeof(uint32_t); // Default: BGRA mode.
1128 if (dec == NULL) return 0;
1130 dec->width_ = width;
1131 dec->height_ = height;
1135 WebPInitCustomIo(NULL, &io); // Just a sanity Init. io won't be used.
1140 dec->status_ = VP8_STATUS_OK;
1141 VP8LInitBitReader(&dec->br_, data, data_size);
1143 dec->action_ = READ_HDR;
1144 if (!DecodeImageStream(width, height, 1, dec, NULL)) goto Err;
1146 // Special case: if alpha data uses only the color indexing transform and
1147 // doesn't use color cache (a frequent case), we will use DecodeAlphaData()
1148 // method that only needs allocation of 1 byte per pixel (alpha channel).
1149 if (dec->next_transform_ == 1 &&
1150 dec->transforms_[0].type_ == COLOR_INDEXING_TRANSFORM &&
1151 dec->hdr_.color_cache_size_ == 0) {
1152 bytes_per_pixel = sizeof(uint8_t);
1155 // Allocate internal buffers (note that dec->width_ may have changed here).
1156 if (!AllocateInternalBuffers(dec, width, bytes_per_pixel)) goto Err;
1158 // Decode (with special row processing).
1159 dec->action_ = READ_DATA;
1160 ok = (bytes_per_pixel == sizeof(uint8_t)) ?
1161 DecodeAlphaData(dec, (uint8_t*)dec->pixels_, dec->width_, dec->height_,
1162 ExtractPalettedAlphaRows) :
1163 DecodeImageData(dec, dec->pixels_, dec->width_, dec->height_,
1171 //------------------------------------------------------------------------------
1173 int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io) {
1174 int width, height, has_alpha;
1176 if (dec == NULL) return 0;
1178 dec->status_ = VP8_STATUS_INVALID_PARAM;
1183 dec->status_ = VP8_STATUS_OK;
1184 VP8LInitBitReader(&dec->br_, io->data, io->data_size);
1185 if (!ReadImageInfo(&dec->br_, &width, &height, &has_alpha)) {
1186 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
1189 dec->state_ = READ_DIM;
1191 io->height = height;
1193 dec->action_ = READ_HDR;
1194 if (!DecodeImageStream(width, height, 1, dec, NULL)) goto Error;
1199 assert(dec->status_ != VP8_STATUS_OK);
1203 int VP8LDecodeImage(VP8LDecoder* const dec) {
1204 const size_t bytes_per_pixel = sizeof(uint32_t);
1206 WebPDecParams* params = NULL;
1209 if (dec == NULL) return 0;
1213 params = (WebPDecParams*)io->opaque;
1214 assert(params != NULL);
1215 dec->output_ = params->output;
1216 assert(dec->output_ != NULL);
1219 if (!WebPIoInitFromOptions(params->options, io, MODE_BGRA)) {
1220 dec->status_ = VP8_STATUS_INVALID_PARAM;
1224 if (!AllocateInternalBuffers(dec, io->width, bytes_per_pixel)) goto Err;
1226 if (io->use_scaling && !AllocateAndInitRescaler(dec, io)) goto Err;
1229 dec->action_ = READ_DATA;
1230 if (!DecodeImageData(dec, dec->pixels_, dec->width_, dec->height_,
1236 params->last_y = dec->last_out_row_;
1242 assert(dec->status_ != VP8_STATUS_OK);
1246 //------------------------------------------------------------------------------
1248 #if defined(__cplusplus) || defined(c_plusplus)