Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: third_party/libwebp/dec/vp8l.c

Issue 10832153: libwebp: update snapshot to v0.2.0-rc1 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
3 // This code is licensed under the same terms as WebM:
4 // Software License Agreement: http://www.webmproject.org/license/software/
5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/
6 // -----------------------------------------------------------------------------
7 //
8 // main entry for the decoder
9 //
10 // Authors: Vikas Arora (vikaas.arora@gmail.com)
11 // Jyrki Alakuijala (jyrki@google.com)
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "./vp8li.h"
16 #include "../dsp/lossless.h"
17 #include "../dsp/yuv.h"
18 #include "../utils/huffman.h"
19 #include "../utils/utils.h"
20
21 #if defined(__cplusplus) || defined(c_plusplus)
22 extern "C" {
23 #endif
24
25 #define NUM_ARGB_CACHE_ROWS 16
26
27 static const int kCodeLengthLiterals = 16;
28 static const int kCodeLengthRepeatCode = 16;
29 static const int kCodeLengthExtraBits[3] = { 2, 3, 7 };
30 static const int kCodeLengthRepeatOffsets[3] = { 3, 3, 11 };
31
32 // -----------------------------------------------------------------------------
33 // Five Huffman codes are used at each meta code:
34 // 1. green + length prefix codes + color cache codes,
35 // 2. alpha,
36 // 3. red,
37 // 4. blue, and,
38 // 5. distance prefix codes.
39 typedef enum {
40 GREEN = 0,
41 RED = 1,
42 BLUE = 2,
43 ALPHA = 3,
44 DIST = 4
45 } HuffIndex;
46
47 static const uint16_t kAlphabetSize[HUFFMAN_CODES_PER_META_CODE] = {
48 NUM_LITERAL_CODES + NUM_LENGTH_CODES,
49 NUM_LITERAL_CODES, NUM_LITERAL_CODES, NUM_LITERAL_CODES,
50 NUM_DISTANCE_CODES
51 };
52
53
54 #define NUM_CODE_LENGTH_CODES 19
55 static const uint8_t kCodeLengthCodeOrder[NUM_CODE_LENGTH_CODES] = {
56 17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
57 };
58
59 #define CODE_TO_PLANE_CODES 120
60 static const uint8_t code_to_plane_lut[CODE_TO_PLANE_CODES] = {
61 0x18, 0x07, 0x17, 0x19, 0x28, 0x06, 0x27, 0x29, 0x16, 0x1a,
62 0x26, 0x2a, 0x38, 0x05, 0x37, 0x39, 0x15, 0x1b, 0x36, 0x3a,
63 0x25, 0x2b, 0x48, 0x04, 0x47, 0x49, 0x14, 0x1c, 0x35, 0x3b,
64 0x46, 0x4a, 0x24, 0x2c, 0x58, 0x45, 0x4b, 0x34, 0x3c, 0x03,
65 0x57, 0x59, 0x13, 0x1d, 0x56, 0x5a, 0x23, 0x2d, 0x44, 0x4c,
66 0x55, 0x5b, 0x33, 0x3d, 0x68, 0x02, 0x67, 0x69, 0x12, 0x1e,
67 0x66, 0x6a, 0x22, 0x2e, 0x54, 0x5c, 0x43, 0x4d, 0x65, 0x6b,
68 0x32, 0x3e, 0x78, 0x01, 0x77, 0x79, 0x53, 0x5d, 0x11, 0x1f,
69 0x64, 0x6c, 0x42, 0x4e, 0x76, 0x7a, 0x21, 0x2f, 0x75, 0x7b,
70 0x31, 0x3f, 0x63, 0x6d, 0x52, 0x5e, 0x00, 0x74, 0x7c, 0x41,
71 0x4f, 0x10, 0x20, 0x62, 0x6e, 0x30, 0x73, 0x7d, 0x51, 0x5f,
72 0x40, 0x72, 0x7e, 0x61, 0x6f, 0x50, 0x71, 0x7f, 0x60, 0x70
73 };
74
75 static int DecodeImageStream(int xsize, int ysize,
76 int is_level0,
77 VP8LDecoder* const dec,
78 uint32_t** const decoded_data);
79
80 //------------------------------------------------------------------------------
81
82 int VP8LCheckSignature(const uint8_t* const data, size_t size) {
83 return (size >= 1) && (data[0] == VP8L_MAGIC_BYTE);
84 }
85
86 static int ReadImageInfo(VP8LBitReader* const br,
87 int* const width, int* const height,
88 int* const has_alpha) {
89 const uint8_t signature = VP8LReadBits(br, 8);
90 if (!VP8LCheckSignature(&signature, 1)) {
91 return 0;
92 }
93 *width = VP8LReadBits(br, VP8L_IMAGE_SIZE_BITS) + 1;
94 *height = VP8LReadBits(br, VP8L_IMAGE_SIZE_BITS) + 1;
95 *has_alpha = VP8LReadBits(br, 1);
96 VP8LReadBits(br, VP8L_VERSION_BITS); // Read/ignore the version number.
97 return 1;
98 }
99
100 int VP8LGetInfo(const uint8_t* data, size_t data_size,
101 int* const width, int* const height, int* const has_alpha) {
102 if (data == NULL || data_size < VP8L_FRAME_HEADER_SIZE) {
103 return 0; // not enough data
104 } else {
105 int w, h, a;
106 VP8LBitReader br;
107 VP8LInitBitReader(&br, data, data_size);
108 if (!ReadImageInfo(&br, &w, &h, &a)) {
109 return 0;
110 }
111 if (width != NULL) *width = w;
112 if (height != NULL) *height = h;
113 if (has_alpha != NULL) *has_alpha = a;
114 return 1;
115 }
116 }
117
118 //------------------------------------------------------------------------------
119
120 static WEBP_INLINE int GetCopyDistance(int distance_symbol,
121 VP8LBitReader* const br) {
122 int extra_bits, offset;
123 if (distance_symbol < 4) {
124 return distance_symbol + 1;
125 }
126 extra_bits = (distance_symbol - 2) >> 1;
127 offset = (2 + (distance_symbol & 1)) << extra_bits;
128 return offset + VP8LReadBits(br, extra_bits) + 1;
129 }
130
131 static WEBP_INLINE int GetCopyLength(int length_symbol,
132 VP8LBitReader* const br) {
133 // Length and distance prefixes are encoded the same way.
134 return GetCopyDistance(length_symbol, br);
135 }
136
137 static WEBP_INLINE int PlaneCodeToDistance(int xsize, int plane_code) {
138 if (plane_code > CODE_TO_PLANE_CODES) {
139 return plane_code - CODE_TO_PLANE_CODES;
140 } else {
141 const int dist_code = code_to_plane_lut[plane_code - 1];
142 const int yoffset = dist_code >> 4;
143 const int xoffset = 8 - (dist_code & 0xf);
144 const int dist = yoffset * xsize + xoffset;
145 return (dist >= 1) ? dist : 1;
146 }
147 }
148
149 //------------------------------------------------------------------------------
150 // Decodes the next Huffman code from bit-stream.
151 // FillBitWindow(br) needs to be called at minimum every second call
152 // to ReadSymbolUnsafe.
153 static 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
162 static 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
177 static 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
235 static 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
294 static 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
307 static 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
375 static 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
412 static 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.
427 static 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.
445 static 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
463 static 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
525 static 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
539 static 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
554 static 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.
575 static 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
599 static 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
605 static 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
616 typedef void (*ProcessRowsFunc)(VP8LDecoder* const dec, int row);
617
618 static void ApplyTransforms(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 uint32_t* rows_data = dec->argb_cache_;
623 const int start_row = dec->last_row_;
624 const int end_row = start_row + num_rows;
625
626 // Inverse transforms.
627 // TODO: most transforms only need to operate on the cropped region only.
628 memcpy(rows_data, rows, cache_pixs * sizeof(*rows_data));
629 while (n-- > 0) {
630 VP8LTransform* const transform = &dec->transforms_[n];
631 VP8LInverseTransform(transform, start_row, end_row, rows, rows_data);
632 }
633 }
634
635 // Processes (transforms, scales & color-converts) the rows decoded after the
636 // last call.
637 static void ProcessRows(VP8LDecoder* const dec, int row) {
638 const uint32_t* const rows = dec->argb_ + dec->width_ * dec->last_row_;
639 const int num_rows = row - dec->last_row_;
640
641 if (num_rows <= 0) return; // Nothing to be done.
642 ApplyTransforms(dec, num_rows, rows);
643
644 // Emit output.
645 {
646 VP8Io* const io = dec->io_;
647 const uint32_t* rows_data = dec->argb_cache_;
648 if (!SetCropWindow(io, dec->last_row_, row, &rows_data, io->width)) {
649 // Nothing to output (this time).
650 } else {
651 const WebPDecBuffer* const output = dec->output_;
652 const int in_stride = io->width * sizeof(*rows_data);
653 if (output->colorspace < MODE_YUV) { // convert to RGBA
654 const WebPRGBABuffer* const buf = &output->u.RGBA;
655 uint8_t* const rgba = buf->rgba + dec->last_out_row_ * buf->stride;
656 const int num_rows_out = io->use_scaling ?
657 EmitRescaledRows(dec, rows_data, in_stride, io->mb_h,
658 rgba, buf->stride) :
659 EmitRows(output->colorspace, rows_data, in_stride,
660 io->mb_w, io->mb_h, rgba, buf->stride);
661 // Update 'last_out_row_'.
662 dec->last_out_row_ += num_rows_out;
663 } else { // convert to YUVA
664 dec->last_out_row_ = io->use_scaling ?
665 EmitRescaledRowsYUVA(dec, rows_data, in_stride, io->mb_h) :
666 EmitRowsYUVA(dec, rows_data, in_stride, io->mb_w, io->mb_h);
667 }
668 assert(dec->last_out_row_ <= output->height);
669 }
670 }
671
672 // Update 'last_row_'.
673 dec->last_row_ = row;
674 assert(dec->last_row_ <= dec->height_);
675 }
676
677 static int DecodeImageData(VP8LDecoder* const dec,
678 uint32_t* const data, int width, int height,
679 ProcessRowsFunc process_func) {
680 int ok = 1;
681 int col = 0, row = 0;
682 VP8LBitReader* const br = &dec->br_;
683 VP8LMetadata* const hdr = &dec->hdr_;
684 HTreeGroup* htree_group = hdr->htree_groups_;
685 uint32_t* src = data;
686 uint32_t* last_cached = data;
687 uint32_t* const src_end = data + width * height;
688 const int len_code_limit = NUM_LITERAL_CODES + NUM_LENGTH_CODES;
689 const int color_cache_limit = len_code_limit + hdr->color_cache_size_;
690 VP8LColorCache* const color_cache =
691 (hdr->color_cache_size_ > 0) ? &hdr->color_cache_ : NULL;
692 const int mask = hdr->huffman_mask_;
693
694 assert(htree_group != NULL);
695
696 while (!br->eos_ && src < src_end) {
697 int code;
698 // Only update when changing tile. Note we could use the following test:
699 // if "((((prev_col ^ col) | prev_row ^ row)) > mask)" -> tile changed
700 // but that's actually slower and requires storing the previous col/row
701 if ((col & mask) == 0) {
702 htree_group = GetHtreeGroupForPos(hdr, col, row);
703 }
704 VP8LFillBitWindow(br);
705 code = ReadSymbol(&htree_group->htrees_[GREEN], br);
706 if (code < NUM_LITERAL_CODES) { // Literal.
707 int red, green, blue, alpha;
708 red = ReadSymbol(&htree_group->htrees_[RED], br);
709 green = code;
710 VP8LFillBitWindow(br);
711 blue = ReadSymbol(&htree_group->htrees_[BLUE], br);
712 alpha = ReadSymbol(&htree_group->htrees_[ALPHA], br);
713 *src = (alpha << 24) + (red << 16) + (green << 8) + blue;
714 AdvanceByOne:
715 ++src;
716 ++col;
717 if (col >= width) {
718 col = 0;
719 ++row;
720 if ((process_func != NULL) && (row % NUM_ARGB_CACHE_ROWS == 0)) {
721 process_func(dec, row);
722 }
723 if (color_cache != NULL) {
724 while (last_cached < src) {
725 VP8LColorCacheInsert(color_cache, *last_cached++);
726 }
727 }
728 }
729 } else if (code < len_code_limit) { // Backward reference
730 int dist_code, dist;
731 const int length_sym = code - NUM_LITERAL_CODES;
732 const int length = GetCopyLength(length_sym, br);
733 const int dist_symbol = ReadSymbol(&htree_group->htrees_[DIST], br);
734 VP8LFillBitWindow(br);
735 dist_code = GetCopyDistance(dist_symbol, br);
736 dist = PlaneCodeToDistance(width, dist_code);
737 if (src - data < dist || src_end - src < length) {
738 ok = 0;
739 goto End;
740 }
741 {
742 int i;
743 for (i = 0; i < length; ++i) src[i] = src[i - dist];
744 src += length;
745 }
746 col += length;
747 while (col >= width) {
748 col -= width;
749 ++row;
750 if ((process_func != NULL) && (row % NUM_ARGB_CACHE_ROWS == 0)) {
751 process_func(dec, row);
752 }
753 }
754 if (src < src_end) {
755 htree_group = GetHtreeGroupForPos(hdr, col, row);
756 if (color_cache != NULL) {
757 while (last_cached < src) {
758 VP8LColorCacheInsert(color_cache, *last_cached++);
759 }
760 }
761 }
762 } else if (code < color_cache_limit) { // Color cache.
763 const int key = code - len_code_limit;
764 assert(color_cache != NULL);
765 while (last_cached < src) {
766 VP8LColorCacheInsert(color_cache, *last_cached++);
767 }
768 *src = VP8LColorCacheLookup(color_cache, key);
769 goto AdvanceByOne;
770 } else { // Not reached.
771 ok = 0;
772 goto End;
773 }
774 ok = !br->error_;
775 if (!ok) goto End;
776 }
777 // Process the remaining rows corresponding to last row-block.
778 if (process_func != NULL) process_func(dec, row);
779
780 End:
781 if (br->error_ || !ok || (br->eos_ && src < src_end)) {
782 ok = 0;
783 dec->status_ = (!br->eos_) ?
784 VP8_STATUS_BITSTREAM_ERROR : VP8_STATUS_SUSPENDED;
785 } else if (src == src_end) {
786 dec->state_ = READ_DATA;
787 }
788
789 return ok;
790 }
791
792 // -----------------------------------------------------------------------------
793 // VP8LTransform
794
795 static void ClearTransform(VP8LTransform* const transform) {
796 free(transform->data_);
797 transform->data_ = NULL;
798 }
799
800 static void ApplyInverseTransforms(VP8LDecoder* const dec, int start_idx,
801 uint32_t* const decoded_data) {
802 int n = dec->next_transform_;
803 assert(start_idx >= 0);
804 while (n-- > start_idx) {
805 VP8LTransform* const transform = &dec->transforms_[n];
806 VP8LInverseTransform(transform, 0, transform->ysize_,
807 decoded_data, decoded_data);
808 ClearTransform(transform);
809 }
810 dec->next_transform_ = start_idx;
811 }
812
813 // For security reason, we need to remap the color map to span
814 // the total possible bundled values, and not just the num_colors.
815 static int ExpandColorMap(int num_colors, VP8LTransform* const transform) {
816 int i;
817 const int final_num_colors = 1 << (8 >> transform->bits_);
818 uint32_t* const new_color_map =
819 (uint32_t*)WebPSafeMalloc((uint64_t)final_num_colors,
820 sizeof(*new_color_map));
821 if (new_color_map == NULL) {
822 return 0;
823 } else {
824 uint8_t* const data = (uint8_t*)transform->data_;
825 uint8_t* const new_data = (uint8_t*)new_color_map;
826 new_color_map[0] = transform->data_[0];
827 for (i = 4; i < 4 * num_colors; ++i) {
828 // Equivalent to AddPixelEq(), on a byte-basis.
829 new_data[i] = (data[i] + new_data[i - 4]) & 0xff;
830 }
831 for (; i < 4 * final_num_colors; ++i)
832 new_data[i] = 0; // black tail.
833 free(transform->data_);
834 transform->data_ = new_color_map;
835 }
836 return 1;
837 }
838
839 static int ReadTransform(int* const xsize, int const* ysize,
840 VP8LDecoder* const dec) {
841 int ok = 1;
842 VP8LBitReader* const br = &dec->br_;
843 VP8LTransform* transform = &dec->transforms_[dec->next_transform_];
844 const VP8LImageTransformType type =
845 (VP8LImageTransformType)VP8LReadBits(br, 2);
846
847 // Each transform type can only be present once in the stream.
848 if (dec->transforms_seen_ & (1U << type)) {
849 return 0; // Already there, let's not accept the second same transform.
850 }
851 dec->transforms_seen_ |= (1U << type);
852
853 transform->type_ = type;
854 transform->xsize_ = *xsize;
855 transform->ysize_ = *ysize;
856 transform->data_ = NULL;
857 ++dec->next_transform_;
858 assert(dec->next_transform_ <= NUM_TRANSFORMS);
859
860 switch (type) {
861 case PREDICTOR_TRANSFORM:
862 case CROSS_COLOR_TRANSFORM:
863 transform->bits_ = VP8LReadBits(br, 3) + 2;
864 ok = DecodeImageStream(VP8LSubSampleSize(transform->xsize_,
865 transform->bits_),
866 VP8LSubSampleSize(transform->ysize_,
867 transform->bits_),
868 0, dec, &transform->data_);
869 break;
870 case COLOR_INDEXING_TRANSFORM: {
871 const int num_colors = VP8LReadBits(br, 8) + 1;
872 const int bits = (num_colors > 16) ? 0
873 : (num_colors > 4) ? 1
874 : (num_colors > 2) ? 2
875 : 3;
876 *xsize = VP8LSubSampleSize(transform->xsize_, bits);
877 transform->bits_ = bits;
878 ok = DecodeImageStream(num_colors, 1, 0, dec, &transform->data_);
879 ok = ok && ExpandColorMap(num_colors, transform);
880 break;
881 }
882 case SUBTRACT_GREEN:
883 break;
884 default:
885 assert(0); // can't happen
886 break;
887 }
888
889 return ok;
890 }
891
892 // -----------------------------------------------------------------------------
893 // VP8LMetadata
894
895 static void InitMetadata(VP8LMetadata* const hdr) {
896 assert(hdr);
897 memset(hdr, 0, sizeof(*hdr));
898 }
899
900 static void ClearMetadata(VP8LMetadata* const hdr) {
901 assert(hdr);
902
903 free(hdr->huffman_image_);
904 DeleteHtreeGroups(hdr->htree_groups_, hdr->num_htree_groups_);
905 VP8LColorCacheClear(&hdr->color_cache_);
906 InitMetadata(hdr);
907 }
908
909 // -----------------------------------------------------------------------------
910 // VP8LDecoder
911
912 VP8LDecoder* VP8LNew(void) {
913 VP8LDecoder* const dec = (VP8LDecoder*)calloc(1, sizeof(*dec));
914 if (dec == NULL) return NULL;
915 dec->status_ = VP8_STATUS_OK;
916 dec->action_ = READ_DIM;
917 dec->state_ = READ_DIM;
918 return dec;
919 }
920
921 void VP8LClear(VP8LDecoder* const dec) {
922 int i;
923 if (dec == NULL) return;
924 ClearMetadata(&dec->hdr_);
925
926 free(dec->argb_);
927 dec->argb_ = NULL;
928 for (i = 0; i < dec->next_transform_; ++i) {
929 ClearTransform(&dec->transforms_[i]);
930 }
931 dec->next_transform_ = 0;
932 dec->transforms_seen_ = 0;
933
934 free(dec->rescaler_memory);
935 dec->rescaler_memory = NULL;
936
937 dec->output_ = NULL; // leave no trace behind
938 }
939
940 void VP8LDelete(VP8LDecoder* const dec) {
941 if (dec != NULL) {
942 VP8LClear(dec);
943 free(dec);
944 }
945 }
946
947 static void UpdateDecoder(VP8LDecoder* const dec, int width, int height) {
948 VP8LMetadata* const hdr = &dec->hdr_;
949 const int num_bits = hdr->huffman_subsample_bits_;
950 dec->width_ = width;
951 dec->height_ = height;
952
953 hdr->huffman_xsize_ = VP8LSubSampleSize(width, num_bits);
954 hdr->huffman_mask_ = (num_bits == 0) ? ~0 : (1 << num_bits) - 1;
955 }
956
957 static int DecodeImageStream(int xsize, int ysize,
958 int is_level0,
959 VP8LDecoder* const dec,
960 uint32_t** const decoded_data) {
961 int ok = 1;
962 int transform_xsize = xsize;
963 int transform_ysize = ysize;
964 VP8LBitReader* const br = &dec->br_;
965 VP8LMetadata* const hdr = &dec->hdr_;
966 uint32_t* data = NULL;
967 const int transform_start_idx = dec->next_transform_;
968 int color_cache_bits = 0;
969
970 // Read the transforms (may recurse).
971 if (is_level0) {
972 while (ok && VP8LReadBits(br, 1)) {
973 ok = ReadTransform(&transform_xsize, &transform_ysize, dec);
974 }
975 }
976
977 // Color cache
978 if (ok && VP8LReadBits(br, 1)) {
979 color_cache_bits = VP8LReadBits(br, 4);
980 ok = (color_cache_bits >= 1 && color_cache_bits <= MAX_CACHE_BITS);
981 if (!ok) {
982 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
983 goto End;
984 }
985 }
986
987 // Read the Huffman codes (may recurse).
988 ok = ok && ReadHuffmanCodes(dec, transform_xsize, transform_ysize,
989 color_cache_bits, is_level0);
990 if (!ok) {
991 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
992 goto End;
993 }
994
995 // Finish setting up the color-cache
996 if (color_cache_bits > 0) {
997 hdr->color_cache_size_ = 1 << color_cache_bits;
998 if (!VP8LColorCacheInit(&hdr->color_cache_, color_cache_bits)) {
999 dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
1000 ok = 0;
1001 goto End;
1002 }
1003 } else {
1004 hdr->color_cache_size_ = 0;
1005 }
1006 UpdateDecoder(dec, transform_xsize, transform_ysize);
1007
1008 if (is_level0) { // level 0 complete
1009 dec->state_ = READ_HDR;
1010 goto End;
1011 }
1012
1013 {
1014 const uint64_t total_size = (uint64_t)transform_xsize * transform_ysize;
1015 data = (uint32_t*)WebPSafeMalloc(total_size, sizeof(*data));
1016 if (data == NULL) {
1017 dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
1018 ok = 0;
1019 goto End;
1020 }
1021 }
1022
1023 // Use the Huffman trees to decode the LZ77 encoded data.
1024 ok = DecodeImageData(dec, data, transform_xsize, transform_ysize, NULL);
1025 ok = ok && !br->error_;
1026
1027 // Apply transforms on the decoded data.
1028 if (ok) ApplyInverseTransforms(dec, transform_start_idx, data);
1029
1030 End:
1031
1032 if (!ok) {
1033 free(data);
1034 ClearMetadata(hdr);
1035 // If not enough data (br.eos_) resulted in BIT_STREAM_ERROR, update the
1036 // status appropriately.
1037 if (dec->status_ == VP8_STATUS_BITSTREAM_ERROR && dec->br_.eos_) {
1038 dec->status_ = VP8_STATUS_SUSPENDED;
1039 }
1040 } else {
1041 if (decoded_data != NULL) {
1042 *decoded_data = data;
1043 } else {
1044 // We allocate image data in this function only for transforms. At level 0
1045 // (that is: not the transforms), we shouldn't have allocated anything.
1046 assert(data == NULL);
1047 assert(is_level0);
1048 }
1049 if (!is_level0) ClearMetadata(hdr); // Clean up temporary data behind.
1050 }
1051 return ok;
1052 }
1053
1054 //------------------------------------------------------------------------------
1055 // Allocate dec->argb_ and dec->argb_cache_ using dec->width_ and dec->height_
1056
1057 static int AllocateARGBBuffers(VP8LDecoder* const dec, int final_width) {
1058 const uint64_t num_pixels = (uint64_t)dec->width_ * dec->height_;
1059 // Scratch buffer corresponding to top-prediction row for transforming the
1060 // first row in the row-blocks.
1061 const uint64_t cache_top_pixels = final_width;
1062 // Scratch buffer for temporary BGRA storage.
1063 const uint64_t cache_pixels = (uint64_t)final_width * NUM_ARGB_CACHE_ROWS;
1064 const uint64_t total_num_pixels =
1065 num_pixels + cache_top_pixels + cache_pixels;
1066
1067 assert(dec->width_ <= final_width);
1068 dec->argb_ = (uint32_t*)WebPSafeMalloc(total_num_pixels, sizeof(*dec->argb_));
1069 if (dec->argb_ == NULL) {
1070 dec->argb_cache_ = NULL; // for sanity check
1071 dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
1072 return 0;
1073 }
1074 dec->argb_cache_ = dec->argb_ + num_pixels + cache_top_pixels;
1075 return 1;
1076 }
1077
1078 //------------------------------------------------------------------------------
1079 // Special row-processing that only stores the alpha data.
1080
1081 static void ExtractAlphaRows(VP8LDecoder* const dec, int row) {
1082 const int num_rows = row - dec->last_row_;
1083 const uint32_t* const in = dec->argb_ + dec->width_ * dec->last_row_;
1084
1085 if (num_rows <= 0) return; // Nothing to be done.
1086 ApplyTransforms(dec, num_rows, in);
1087
1088 // Extract alpha (which is stored in the green plane).
1089 {
1090 const int width = dec->io_->width; // the final width (!= dec->width_)
1091 const int cache_pixs = width * num_rows;
1092 uint8_t* const dst = (uint8_t*)dec->io_->opaque + width * dec->last_row_;
1093 const uint32_t* const src = dec->argb_cache_;
1094 int i;
1095 for (i = 0; i < cache_pixs; ++i) dst[i] = (src[i] >> 8) & 0xff;
1096 }
1097
1098 dec->last_row_ = dec->last_out_row_ = row;
1099 }
1100
1101 int VP8LDecodeAlphaImageStream(int width, int height, const uint8_t* const data,
1102 size_t data_size, uint8_t* const output) {
1103 VP8Io io;
1104 int ok = 0;
1105 VP8LDecoder* const dec = VP8LNew();
1106 if (dec == NULL) return 0;
1107
1108 dec->width_ = width;
1109 dec->height_ = height;
1110 dec->io_ = &io;
1111
1112 VP8InitIo(&io);
1113 WebPInitCustomIo(NULL, &io); // Just a sanity Init. io won't be used.
1114 io.opaque = output;
1115 io.width = width;
1116 io.height = height;
1117
1118 dec->status_ = VP8_STATUS_OK;
1119 VP8LInitBitReader(&dec->br_, data, data_size);
1120
1121 dec->action_ = READ_HDR;
1122 if (!DecodeImageStream(width, height, 1, dec, NULL)) goto Err;
1123
1124 // Allocate output (note that dec->width_ may have changed here).
1125 if (!AllocateARGBBuffers(dec, width)) goto Err;
1126
1127 // Decode (with special row processing).
1128 dec->action_ = READ_DATA;
1129 ok = DecodeImageData(dec, dec->argb_, dec->width_, dec->height_,
1130 ExtractAlphaRows);
1131
1132 Err:
1133 VP8LDelete(dec);
1134 return ok;
1135 }
1136
1137 //------------------------------------------------------------------------------
1138
1139 int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io) {
1140 int width, height, has_alpha;
1141
1142 if (dec == NULL) return 0;
1143 if (io == NULL) {
1144 dec->status_ = VP8_STATUS_INVALID_PARAM;
1145 return 0;
1146 }
1147
1148 dec->io_ = io;
1149 dec->status_ = VP8_STATUS_OK;
1150 VP8LInitBitReader(&dec->br_, io->data, io->data_size);
1151 if (!ReadImageInfo(&dec->br_, &width, &height, &has_alpha)) {
1152 dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
1153 goto Error;
1154 }
1155 dec->state_ = READ_DIM;
1156 io->width = width;
1157 io->height = height;
1158
1159 dec->action_ = READ_HDR;
1160 if (!DecodeImageStream(width, height, 1, dec, NULL)) goto Error;
1161 return 1;
1162
1163 Error:
1164 VP8LClear(dec);
1165 assert(dec->status_ != VP8_STATUS_OK);
1166 return 0;
1167 }
1168
1169 int VP8LDecodeImage(VP8LDecoder* const dec) {
1170 VP8Io* io = NULL;
1171 WebPDecParams* params = NULL;
1172
1173 // Sanity checks.
1174 if (dec == NULL) return 0;
1175
1176 io = dec->io_;
1177 assert(io != NULL);
1178 params = (WebPDecParams*)io->opaque;
1179 assert(params != NULL);
1180 dec->output_ = params->output;
1181 assert(dec->output_ != NULL);
1182
1183 // Initialization.
1184 if (!WebPIoInitFromOptions(params->options, io, MODE_BGRA)) {
1185 dec->status_ = VP8_STATUS_INVALID_PARAM;
1186 goto Err;
1187 }
1188
1189 if (!AllocateARGBBuffers(dec, io->width)) goto Err;
1190
1191 if (io->use_scaling && !AllocateAndInitRescaler(dec, io)) goto Err;
1192
1193 // Decode.
1194 dec->action_ = READ_DATA;
1195 if (!DecodeImageData(dec, dec->argb_, dec->width_, dec->height_,
1196 ProcessRows)) {
1197 goto Err;
1198 }
1199
1200 // Cleanup.
1201 params->last_y = dec->last_out_row_;
1202 VP8LClear(dec);
1203 return 1;
1204
1205 Err:
1206 VP8LClear(dec);
1207 assert(dec->status_ != VP8_STATUS_OK);
1208 return 0;
1209 }
1210
1211 //------------------------------------------------------------------------------
1212
1213 #if defined(__cplusplus) || defined(c_plusplus)
1214 } // extern "C"
1215 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698