1 // Copyright 2012 Google Inc. All Rights Reserved.
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 // -----------------------------------------------------------------------------
8 // main entry for the decoder
10 // Authors: Vikas Arora (vikaas.arora@gmail.com)
11 // Jyrki Alakuijala (jyrki@google.com)
16 #include "../dsp/lossless.h"
17 #include "../dsp/yuv.h"
18 #include "../utils/huffman.h"
19 #include "../utils/utils.h"
21 #if defined(__cplusplus) || defined(c_plusplus)
25 #define NUM_ARGB_CACHE_ROWS 16
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 };
32 // -----------------------------------------------------------------------------
33 // Five Huffman codes are used at each meta code:
34 // 1. green + length prefix codes + color cache codes,
38 // 5. distance prefix codes.
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,
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
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
75 static int DecodeImageStream(int xsize, int ysize,
77 VP8LDecoder* const dec,
78 uint32_t** const decoded_data);
80 //------------------------------------------------------------------------------
82 int VP8LCheckSignature(const uint8_t* const data, size_t size) {
83 return (size >= 1) && (data[0] == VP8L_MAGIC_BYTE);
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)) {
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.
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
107 VP8LInitBitReader(&br, data, data_size);
108 if (!ReadImageInfo(&br, &w, &h, &a)) {
111 if (width != NULL) *width = w;
112 if (height != NULL) *height = h;
113 if (has_alpha != NULL) *has_alpha = a;
118 //------------------------------------------------------------------------------
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;
126 extra_bits = (distance_symbol - 2) >> 1;
127 offset = (2 + (distance_symbol & 1)) << extra_bits;
128 return offset + VP8LReadBits(br, extra_bits) + 1;
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);
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;
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;
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_;
157 uint32_t bits = VP8LPrefetchBits(br);
158 assert(node != NULL);
159 while (!HuffmanTreeNodeIsLeaf(node)) {
160 node = HuffmanTreeNextNode(node, bits & 1);
164 VP8LDiscardBits(br, num_bits);
165 return node->symbol_;
168 static int ReadHuffmanCodeLengths(
169 VP8LDecoder* const dec, const int* const code_length_code_lengths,
170 int num_symbols, int* const code_lengths) {
172 VP8LBitReader* const br = &dec->br_;
175 int prev_code_len = DEFAULT_CODE_LENGTH;
178 if (!HuffmanTreeBuildImplicit(&tree, code_length_code_lengths,
179 NUM_CODE_LENGTH_CODES)) {
180 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
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;
192 max_symbol = num_symbols;
196 while (symbol < num_symbols) {
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;
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;
214 const int length = use_prev ? prev_code_len : 0;
215 while (repeat-- > 0) code_lengths[symbol++] = length;
222 HuffmanTreeRelease(&tree);
226 static int ReadHuffmanCode(int alphabet_size, VP8LDecoder* const dec,
227 HuffmanTree* const tree) {
229 VP8LBitReader* const br = &dec->br_;
230 const int simple_code = VP8LReadBits(br, 1);
232 if (simple_code) { // Read symbols, codes & code lengths directly.
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);
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);
246 code_lengths[1] = num_symbols - 1;
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;
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;
261 (int*)WebPSafeCalloc((uint64_t)alphabet_size, sizeof(*code_lengths));
262 if (code_lengths == NULL) {
263 dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
267 for (i = 0; i < num_codes; ++i) {
268 code_length_code_lengths[kCodeLengthCodeOrder[i]] = VP8LReadBits(br, 3);
270 ok = ReadHuffmanCodeLengths(dec, code_length_code_lengths, alphabet_size,
273 ok = HuffmanTreeBuildImplicit(tree, code_lengths, alphabet_size);
277 ok = ok && !br->error_;
279 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
285 static void DeleteHtreeGroups(HTreeGroup* htree_groups, int num_htree_groups) {
286 if (htree_groups != NULL) {
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]);
298 static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
299 int color_cache_bits, int allow_recursion) {
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;
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,
315 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
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;
329 if (br->error_) goto Error;
331 assert(num_htree_groups <= 0x10000);
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;
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;
347 if (!ReadHuffmanCode(alphabet_size, dec, htrees + j)) goto Error;
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;
359 DeleteHtreeGroups(htree_groups, num_htree_groups);
363 //------------------------------------------------------------------------------
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;
384 assert(dec->rescaler_memory == NULL);
385 dec->rescaler_memory = memory;
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;
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);
399 //------------------------------------------------------------------------------
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);
414 return num_lines_out;
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,
430 num_lines_out += Export(dec->rescaler, colorspace, out_stride, row_out);
432 return num_lines_out;
435 // Emit rows without any scaling.
436 static int EmitRows(WEBP_CSP_MODE colorspace,
437 const uint32_t* const data, int in_stride,
439 uint8_t* const out, int out_stride) {
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);
446 row_out += out_stride;
448 return mb_h; // Num rows out == num rows in.
451 //------------------------------------------------------------------------------
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
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);
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;
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;
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;
508 // Lastly, store alpha if needed.
509 if (buf->a != NULL) {
511 uint8_t* const a = buf->a + y_pos * buf->a_stride;
512 for (i = 0; i < width; ++i) a[i] = (src[i] >> 24);
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_);
527 return num_lines_out;
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,
540 y_pos += ExportYUVA(dec, y_pos);
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_);
558 //------------------------------------------------------------------------------
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.
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;
578 if (y_start >= y_end) return 0; // Crop window is empty.
580 *in_data += io->crop_left;
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.
588 //------------------------------------------------------------------------------
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)];
596 static WEBP_INLINE HTreeGroup* GetHtreeGroupForPos(VP8LMetadata* const hdr,
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;
604 //------------------------------------------------------------------------------
605 // Main loop, with custom row-processing function
607 typedef void (*ProcessRowsFunc)(VP8LDecoder* const dec, int row);
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_;
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));
622 VP8LTransform* const transform = &dec->transforms_[n];
623 VP8LInverseTransform(transform, start_row, end_row, rows_in, rows_out);
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,
642 // Processes (transforms, scales & color-converts) the rows decoded after the
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_;
648 if (num_rows <= 0) return; // Nothing to be done.
649 ApplyInverseTransforms(dec, num_rows, rows);
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).
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,
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);
675 assert(dec->last_out_row_ <= output->height);
679 // Update 'last_row_'.
680 dec->last_row_ = row;
681 assert(dec->last_row_ <= dec->height_);
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) { \
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_; \
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) { \
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); \
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); \
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); \
722 if (col >= width) { \
725 if ((process_func != NULL) && (row % NUM_ARGB_CACHE_ROWS == 0)) { \
726 process_func(dec, row); \
728 if (color_cache != NULL) { \
729 while (last_cached < src) { \
730 VP8LColorCacheInsert(color_cache, *last_cached++); \
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) { \
748 for (i = 0; i < length; ++i) src[i] = src[i - dist]; \
752 while (col >= width) { \
755 if ((process_func != NULL) && (row % NUM_ARGB_CACHE_ROWS == 0)) { \
756 process_func(dec, row); \
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++); \
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++); \
773 *src = VP8LColorCacheLookup(color_cache, key); \
775 } else { /* Not reached */ \
782 /* Process the remaining rows corresponding to last row-block. */ \
783 if (process_func != NULL) process_func(dec, row); \
785 if (br->error_ || !ok || (br->eos_ && src < src_end)) { \
788 (!br->eos_) ? VP8_STATUS_BITSTREAM_ERROR : VP8_STATUS_SUSPENDED; \
789 } else if (src == src_end) { \
790 dec->state_ = READ_DATA; \
795 static WEBP_INLINE uint32_t GetARGBPixel(int alpha, int red, int green,
797 return (alpha << 24) | (red << 16) | (green << 8) | blue;
800 static WEBP_INLINE uint8_t GetAlphaPixel(int alpha, int red, int green,
805 return green; // Alpha value is stored in green channel.
808 DECODE_DATA_FUNC(DecodeImageData, uint32_t, GetARGBPixel)
809 DECODE_DATA_FUNC(DecodeAlphaData, uint8_t, GetAlphaPixel)
811 #undef DECODE_DATA_FUNC
813 // -----------------------------------------------------------------------------
816 static void ClearTransform(VP8LTransform* const transform) {
817 free(transform->data_);
818 transform->data_ = NULL;
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) {
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) {
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;
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;
847 static int ReadTransform(int* const xsize, int const* ysize,
848 VP8LDecoder* const dec) {
850 VP8LBitReader* const br = &dec->br_;
851 VP8LTransform* transform = &dec->transforms_[dec->next_transform_];
852 const VP8LImageTransformType type =
853 (VP8LImageTransformType)VP8LReadBits(br, 2);
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.
859 dec->transforms_seen_ |= (1U << type);
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);
869 case PREDICTOR_TRANSFORM:
870 case CROSS_COLOR_TRANSFORM:
871 transform->bits_ = VP8LReadBits(br, 3) + 2;
872 ok = DecodeImageStream(VP8LSubSampleSize(transform->xsize_,
874 VP8LSubSampleSize(transform->ysize_,
876 0, dec, &transform->data_);
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
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);
893 assert(0); // can't happen
900 // -----------------------------------------------------------------------------
903 static void InitMetadata(VP8LMetadata* const hdr) {
905 memset(hdr, 0, sizeof(*hdr));
908 static void ClearMetadata(VP8LMetadata* const hdr) {
911 free(hdr->huffman_image_);
912 DeleteHtreeGroups(hdr->htree_groups_, hdr->num_htree_groups_);
913 VP8LColorCacheClear(&hdr->color_cache_);
917 // -----------------------------------------------------------------------------
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;
929 void VP8LClear(VP8LDecoder* const dec) {
931 if (dec == NULL) return;
932 ClearMetadata(&dec->hdr_);
936 for (i = 0; i < dec->next_transform_; ++i) {
937 ClearTransform(&dec->transforms_[i]);
939 dec->next_transform_ = 0;
940 dec->transforms_seen_ = 0;
942 free(dec->rescaler_memory);
943 dec->rescaler_memory = NULL;
945 dec->output_ = NULL; // leave no trace behind
948 void VP8LDelete(VP8LDecoder* const dec) {
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_;
959 dec->height_ = height;
961 hdr->huffman_xsize_ = VP8LSubSampleSize(width, num_bits);
962 hdr->huffman_mask_ = (num_bits == 0) ? ~0 : (1 << num_bits) - 1;
965 static int DecodeImageStream(int xsize, int ysize,
967 VP8LDecoder* const dec,
968 uint32_t** const decoded_data) {
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;
977 // Read the transforms (may recurse).
979 while (ok && VP8LReadBits(br, 1)) {
980 ok = ReadTransform(&transform_xsize, &transform_ysize, dec);
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);
989 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
994 // Read the Huffman codes (may recurse).
995 ok = ok && ReadHuffmanCodes(dec, transform_xsize, transform_ysize,
996 color_cache_bits, is_level0);
998 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
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;
1011 hdr->color_cache_size_ = 0;
1013 UpdateDecoder(dec, transform_xsize, transform_ysize);
1015 if (is_level0) { // level 0 complete
1016 dec->state_ = READ_HDR;
1021 const uint64_t total_size = (uint64_t)transform_xsize * transform_ysize;
1022 data = (uint32_t*)WebPSafeMalloc(total_size, sizeof(*data));
1024 dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
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_;
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;
1045 if (decoded_data != NULL) {
1046 *decoded_data = data;
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);
1053 if (!is_level0) ClearMetadata(hdr); // Clean up temporary data behind.
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;
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;
1082 argb_cache_needed ? dec->pixels_ + num_pixels + cache_top_pixels : NULL;
1086 //------------------------------------------------------------------------------
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_;
1093 if (num_rows <= 0) return; // Nothing to be done.
1094 ApplyInverseTransforms(dec, num_rows, in);
1096 // Extract alpha (which is stored in the green plane).
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_;
1103 for (i = 0; i < cache_pixs; ++i) dst[i] = (src[i] >> 8) & 0xff;
1105 dec->last_row_ = dec->last_out_row_ = row;
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;
1119 int VP8LDecodeAlphaImageStream(int width, int height, const uint8_t* const data,
1120 size_t data_size, uint8_t* const output) {
1123 VP8LDecoder* const dec = VP8LNew();
1124 size_t bytes_per_pixel = sizeof(uint32_t); // Default: BGRA mode.
1125 if (dec == NULL) return 0;
1127 dec->width_ = width;
1128 dec->height_ = height;
1132 WebPInitCustomIo(NULL, &io); // Just a sanity Init. io won't be used.
1137 dec->status_ = VP8_STATUS_OK;
1138 VP8LInitBitReader(&dec->br_, data, data_size);
1140 dec->action_ = READ_HDR;
1141 if (!DecodeImageStream(width, height, 1, dec, NULL)) goto Err;
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);
1151 // Allocate internal buffers (note that dec->width_ may have changed here).
1152 if (!AllocateInternalBuffers(dec, width, bytes_per_pixel)) goto Err;
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_,
1167 //------------------------------------------------------------------------------
1169 int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io) {
1170 int width, height, has_alpha;
1172 if (dec == NULL) return 0;
1174 dec->status_ = VP8_STATUS_INVALID_PARAM;
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;
1185 dec->state_ = READ_DIM;
1187 io->height = height;
1189 dec->action_ = READ_HDR;
1190 if (!DecodeImageStream(width, height, 1, dec, NULL)) goto Error;
1195 assert(dec->status_ != VP8_STATUS_OK);
1199 int VP8LDecodeImage(VP8LDecoder* const dec) {
1200 const size_t bytes_per_pixel = sizeof(uint32_t);
1202 WebPDecParams* params = NULL;
1205 if (dec == NULL) return 0;
1209 params = (WebPDecParams*)io->opaque;
1210 assert(params != NULL);
1211 dec->output_ = params->output;
1212 assert(dec->output_ != NULL);
1215 if (!WebPIoInitFromOptions(params->options, io, MODE_BGRA)) {
1216 dec->status_ = VP8_STATUS_INVALID_PARAM;
1220 if (!AllocateInternalBuffers(dec, io->width, bytes_per_pixel)) goto Err;
1222 if (io->use_scaling && !AllocateAndInitRescaler(dec, io)) goto Err;
1225 dec->action_ = READ_DATA;
1226 if (!DecodeImageData(dec, dec->pixels_, dec->width_, dec->height_,
1232 params->last_y = dec->last_out_row_;
1238 assert(dec->status_ != VP8_STATUS_OK);
1242 //------------------------------------------------------------------------------
1244 #if defined(__cplusplus) || defined(c_plusplus)