[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / third_party / libwebp / dec / vp8.c
blobbfd0e8f9d3827ad77469d9ef8137acfadddcd7ab
1 // Copyright 2010 Google Inc. All Rights Reserved.
2 //
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 // -----------------------------------------------------------------------------
9 //
10 // main entry for the decoder
12 // Author: Skal (pascal.massimino@gmail.com)
14 #include <stdlib.h>
16 #include "./alphai.h"
17 #include "./vp8i.h"
18 #include "./vp8li.h"
19 #include "./webpi.h"
20 #include "../utils/bit_reader.h"
22 //------------------------------------------------------------------------------
24 int WebPGetDecoderVersion(void) {
25 return (DEC_MAJ_VERSION << 16) | (DEC_MIN_VERSION << 8) | DEC_REV_VERSION;
28 //------------------------------------------------------------------------------
29 // VP8Decoder
31 static void SetOk(VP8Decoder* const dec) {
32 dec->status_ = VP8_STATUS_OK;
33 dec->error_msg_ = "OK";
36 int VP8InitIoInternal(VP8Io* const io, int version) {
37 if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
38 return 0; // mismatch error
40 if (io != NULL) {
41 memset(io, 0, sizeof(*io));
43 return 1;
46 VP8Decoder* VP8New(void) {
47 VP8Decoder* const dec = (VP8Decoder*)calloc(1, sizeof(*dec));
48 if (dec != NULL) {
49 SetOk(dec);
50 WebPWorkerInit(&dec->worker_);
51 dec->ready_ = 0;
52 dec->num_parts_ = 1;
54 return dec;
57 VP8StatusCode VP8Status(VP8Decoder* const dec) {
58 if (!dec) return VP8_STATUS_INVALID_PARAM;
59 return dec->status_;
62 const char* VP8StatusMessage(VP8Decoder* const dec) {
63 if (dec == NULL) return "no object";
64 if (!dec->error_msg_) return "OK";
65 return dec->error_msg_;
68 void VP8Delete(VP8Decoder* const dec) {
69 if (dec != NULL) {
70 VP8Clear(dec);
71 free(dec);
75 int VP8SetError(VP8Decoder* const dec,
76 VP8StatusCode error, const char* const msg) {
77 // TODO This check would be unnecessary if alpha decompression was separated
78 // from VP8ProcessRow/FinishRow. This avoids setting 'dec->status_' to
79 // something other than VP8_STATUS_BITSTREAM_ERROR on alpha decompression
80 // failure.
81 if (dec->status_ == VP8_STATUS_OK) {
82 dec->status_ = error;
83 dec->error_msg_ = msg;
84 dec->ready_ = 0;
86 return 0;
89 //------------------------------------------------------------------------------
91 int VP8CheckSignature(const uint8_t* const data, size_t data_size) {
92 return (data_size >= 3 &&
93 data[0] == 0x9d && data[1] == 0x01 && data[2] == 0x2a);
96 int VP8GetInfo(const uint8_t* data, size_t data_size, size_t chunk_size,
97 int* const width, int* const height) {
98 if (data == NULL || data_size < VP8_FRAME_HEADER_SIZE) {
99 return 0; // not enough data
101 // check signature
102 if (!VP8CheckSignature(data + 3, data_size - 3)) {
103 return 0; // Wrong signature.
104 } else {
105 const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16);
106 const int key_frame = !(bits & 1);
107 const int w = ((data[7] << 8) | data[6]) & 0x3fff;
108 const int h = ((data[9] << 8) | data[8]) & 0x3fff;
110 if (!key_frame) { // Not a keyframe.
111 return 0;
114 if (((bits >> 1) & 7) > 3) {
115 return 0; // unknown profile
117 if (!((bits >> 4) & 1)) {
118 return 0; // first frame is invisible!
120 if (((bits >> 5)) >= chunk_size) { // partition_length
121 return 0; // inconsistent size information.
123 if (w == 0 || h == 0) {
124 return 0; // We don't support both width and height to be zero.
127 if (width) {
128 *width = w;
130 if (height) {
131 *height = h;
134 return 1;
138 //------------------------------------------------------------------------------
139 // Header parsing
141 static void ResetSegmentHeader(VP8SegmentHeader* const hdr) {
142 assert(hdr != NULL);
143 hdr->use_segment_ = 0;
144 hdr->update_map_ = 0;
145 hdr->absolute_delta_ = 1;
146 memset(hdr->quantizer_, 0, sizeof(hdr->quantizer_));
147 memset(hdr->filter_strength_, 0, sizeof(hdr->filter_strength_));
150 // Paragraph 9.3
151 static int ParseSegmentHeader(VP8BitReader* br,
152 VP8SegmentHeader* hdr, VP8Proba* proba) {
153 assert(br != NULL);
154 assert(hdr != NULL);
155 hdr->use_segment_ = VP8Get(br);
156 if (hdr->use_segment_) {
157 hdr->update_map_ = VP8Get(br);
158 if (VP8Get(br)) { // update data
159 int s;
160 hdr->absolute_delta_ = VP8Get(br);
161 for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
162 hdr->quantizer_[s] = VP8Get(br) ? VP8GetSignedValue(br, 7) : 0;
164 for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
165 hdr->filter_strength_[s] = VP8Get(br) ? VP8GetSignedValue(br, 6) : 0;
168 if (hdr->update_map_) {
169 int s;
170 for (s = 0; s < MB_FEATURE_TREE_PROBS; ++s) {
171 proba->segments_[s] = VP8Get(br) ? VP8GetValue(br, 8) : 255u;
174 } else {
175 hdr->update_map_ = 0;
177 return !br->eof_;
180 // Paragraph 9.5
181 // This function returns VP8_STATUS_SUSPENDED if we don't have all the
182 // necessary data in 'buf'.
183 // This case is not necessarily an error (for incremental decoding).
184 // Still, no bitreader is ever initialized to make it possible to read
185 // unavailable memory.
186 // If we don't even have the partitions' sizes, than VP8_STATUS_NOT_ENOUGH_DATA
187 // is returned, and this is an unrecoverable error.
188 // If the partitions were positioned ok, VP8_STATUS_OK is returned.
189 static VP8StatusCode ParsePartitions(VP8Decoder* const dec,
190 const uint8_t* buf, size_t size) {
191 VP8BitReader* const br = &dec->br_;
192 const uint8_t* sz = buf;
193 const uint8_t* buf_end = buf + size;
194 const uint8_t* part_start;
195 int last_part;
196 int p;
198 dec->num_parts_ = 1 << VP8GetValue(br, 2);
199 last_part = dec->num_parts_ - 1;
200 part_start = buf + last_part * 3;
201 if (buf_end < part_start) {
202 // we can't even read the sizes with sz[]! That's a failure.
203 return VP8_STATUS_NOT_ENOUGH_DATA;
205 for (p = 0; p < last_part; ++p) {
206 const uint32_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16);
207 const uint8_t* part_end = part_start + psize;
208 if (part_end > buf_end) part_end = buf_end;
209 VP8InitBitReader(dec->parts_ + p, part_start, part_end);
210 part_start = part_end;
211 sz += 3;
213 VP8InitBitReader(dec->parts_ + last_part, part_start, buf_end);
214 return (part_start < buf_end) ? VP8_STATUS_OK :
215 VP8_STATUS_SUSPENDED; // Init is ok, but there's not enough data
218 // Paragraph 9.4
219 static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) {
220 VP8FilterHeader* const hdr = &dec->filter_hdr_;
221 hdr->simple_ = VP8Get(br);
222 hdr->level_ = VP8GetValue(br, 6);
223 hdr->sharpness_ = VP8GetValue(br, 3);
224 hdr->use_lf_delta_ = VP8Get(br);
225 if (hdr->use_lf_delta_) {
226 if (VP8Get(br)) { // update lf-delta?
227 int i;
228 for (i = 0; i < NUM_REF_LF_DELTAS; ++i) {
229 if (VP8Get(br)) {
230 hdr->ref_lf_delta_[i] = VP8GetSignedValue(br, 6);
233 for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) {
234 if (VP8Get(br)) {
235 hdr->mode_lf_delta_[i] = VP8GetSignedValue(br, 6);
240 dec->filter_type_ = (hdr->level_ == 0) ? 0 : hdr->simple_ ? 1 : 2;
241 return !br->eof_;
244 // Topmost call
245 int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
246 const uint8_t* buf;
247 size_t buf_size;
248 VP8FrameHeader* frm_hdr;
249 VP8PictureHeader* pic_hdr;
250 VP8BitReader* br;
251 VP8StatusCode status;
253 if (dec == NULL) {
254 return 0;
256 SetOk(dec);
257 if (io == NULL) {
258 return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
259 "null VP8Io passed to VP8GetHeaders()");
261 buf = io->data;
262 buf_size = io->data_size;
263 if (buf_size < 4) {
264 return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
265 "Truncated header.");
268 // Paragraph 9.1
270 const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16);
271 frm_hdr = &dec->frm_hdr_;
272 frm_hdr->key_frame_ = !(bits & 1);
273 frm_hdr->profile_ = (bits >> 1) & 7;
274 frm_hdr->show_ = (bits >> 4) & 1;
275 frm_hdr->partition_length_ = (bits >> 5);
276 if (frm_hdr->profile_ > 3)
277 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
278 "Incorrect keyframe parameters.");
279 if (!frm_hdr->show_)
280 return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
281 "Frame not displayable.");
282 buf += 3;
283 buf_size -= 3;
286 pic_hdr = &dec->pic_hdr_;
287 if (frm_hdr->key_frame_) {
288 // Paragraph 9.2
289 if (buf_size < 7) {
290 return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
291 "cannot parse picture header");
293 if (!VP8CheckSignature(buf, buf_size)) {
294 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
295 "Bad code word");
297 pic_hdr->width_ = ((buf[4] << 8) | buf[3]) & 0x3fff;
298 pic_hdr->xscale_ = buf[4] >> 6; // ratio: 1, 5/4 5/3 or 2
299 pic_hdr->height_ = ((buf[6] << 8) | buf[5]) & 0x3fff;
300 pic_hdr->yscale_ = buf[6] >> 6;
301 buf += 7;
302 buf_size -= 7;
304 dec->mb_w_ = (pic_hdr->width_ + 15) >> 4;
305 dec->mb_h_ = (pic_hdr->height_ + 15) >> 4;
306 // Setup default output area (can be later modified during io->setup())
307 io->width = pic_hdr->width_;
308 io->height = pic_hdr->height_;
309 io->use_scaling = 0;
310 io->use_cropping = 0;
311 io->crop_top = 0;
312 io->crop_left = 0;
313 io->crop_right = io->width;
314 io->crop_bottom = io->height;
315 io->mb_w = io->width; // sanity check
316 io->mb_h = io->height; // ditto
318 VP8ResetProba(&dec->proba_);
319 ResetSegmentHeader(&dec->segment_hdr_);
320 dec->segment_ = 0; // default for intra
323 // Check if we have all the partition #0 available, and initialize dec->br_
324 // to read this partition (and this partition only).
325 if (frm_hdr->partition_length_ > buf_size) {
326 return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
327 "bad partition length");
330 br = &dec->br_;
331 VP8InitBitReader(br, buf, buf + frm_hdr->partition_length_);
332 buf += frm_hdr->partition_length_;
333 buf_size -= frm_hdr->partition_length_;
335 if (frm_hdr->key_frame_) {
336 pic_hdr->colorspace_ = VP8Get(br);
337 pic_hdr->clamp_type_ = VP8Get(br);
339 if (!ParseSegmentHeader(br, &dec->segment_hdr_, &dec->proba_)) {
340 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
341 "cannot parse segment header");
343 // Filter specs
344 if (!ParseFilterHeader(br, dec)) {
345 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
346 "cannot parse filter header");
348 status = ParsePartitions(dec, buf, buf_size);
349 if (status != VP8_STATUS_OK) {
350 return VP8SetError(dec, status, "cannot parse partitions");
353 // quantizer change
354 VP8ParseQuant(dec);
356 // Frame buffer marking
357 if (!frm_hdr->key_frame_) {
358 return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
359 "Not a key frame.");
362 VP8Get(br); // ignore the value of update_proba_
364 VP8ParseProba(br, dec);
366 #ifdef WEBP_EXPERIMENTAL_FEATURES
367 // Extensions
368 if (dec->pic_hdr_.colorspace_) {
369 const size_t kTrailerSize = 8;
370 const uint8_t kTrailerMarker = 0x01;
371 const uint8_t* ext_buf = buf - kTrailerSize;
372 size_t size;
374 if (frm_hdr->partition_length_ < kTrailerSize ||
375 ext_buf[kTrailerSize - 1] != kTrailerMarker) {
376 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
377 "RIFF: Inconsistent extra information.");
380 // Layer
381 size = (ext_buf[0] << 0) | (ext_buf[1] << 8) | (ext_buf[2] << 16);
382 dec->layer_data_size_ = size;
383 dec->layer_data_ = NULL; // will be set later
384 dec->layer_colorspace_ = ext_buf[3];
386 #endif
388 // sanitized state
389 dec->ready_ = 1;
390 return 1;
393 //------------------------------------------------------------------------------
394 // Residual decoding (Paragraph 13.2 / 13.3)
396 static const int kBands[16 + 1] = {
397 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7,
398 0 // extra entry as sentinel
401 static const uint8_t kCat3[] = { 173, 148, 140, 0 };
402 static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 };
403 static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 };
404 static const uint8_t kCat6[] =
405 { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0 };
406 static const uint8_t* const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 };
407 static const uint8_t kZigzag[16] = {
408 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
411 // See section 13-2: http://tools.ietf.org/html/rfc6386#section-13.2
412 static int GetLargeValue(VP8BitReader* const br, const uint8_t* const p) {
413 int v;
414 if (!VP8GetBit(br, p[3])) {
415 if (!VP8GetBit(br, p[4])) {
416 v = 2;
417 } else {
418 v = 3 + VP8GetBit(br, p[5]);
420 } else {
421 if (!VP8GetBit(br, p[6])) {
422 if (!VP8GetBit(br, p[7])) {
423 v = 5 + VP8GetBit(br, 159);
424 } else {
425 v = 7 + 2 * VP8GetBit(br, 165);
426 v += VP8GetBit(br, 145);
428 } else {
429 const uint8_t* tab;
430 const int bit1 = VP8GetBit(br, p[8]);
431 const int bit0 = VP8GetBit(br, p[9 + bit1]);
432 const int cat = 2 * bit1 + bit0;
433 v = 0;
434 for (tab = kCat3456[cat]; *tab; ++tab) {
435 v += v + VP8GetBit(br, *tab);
437 v += 3 + (8 << cat);
440 return v;
443 // Returns the position of the last non-zero coeff plus one
444 static int GetCoeffs(VP8BitReader* const br, const VP8BandProbas* const prob,
445 int ctx, const quant_t dq, int n, int16_t* out) {
446 // n is either 0 or 1 here. kBands[n] is not necessary for extracting '*p'.
447 const uint8_t* p = prob[n].probas_[ctx];
448 for (; n < 16; ++n) {
449 if (!VP8GetBit(br, p[0])) {
450 return n; // previous coeff was last non-zero coeff
452 while (!VP8GetBit(br, p[1])) { // sequence of zero coeffs
453 p = prob[kBands[++n]].probas_[0];
454 if (n == 16) return 16;
456 { // non zero coeff
457 const VP8ProbaArray* const p_ctx = &prob[kBands[n + 1]].probas_[0];
458 int v;
459 if (!VP8GetBit(br, p[2])) {
460 v = 1;
461 p = p_ctx[1];
462 } else {
463 v = GetLargeValue(br, p);
464 p = p_ctx[2];
466 out[kZigzag[n]] = VP8GetSigned(br, v) * dq[n > 0];
469 return 16;
472 static WEBP_INLINE uint32_t NzCodeBits(uint32_t nz_coeffs, int nz, int dc_nz) {
473 nz_coeffs <<= 2;
474 nz_coeffs |= (nz > 3) ? 3 : (nz > 1) ? 2 : dc_nz;
475 return nz_coeffs;
478 static int ParseResiduals(VP8Decoder* const dec,
479 VP8MB* const mb, VP8BitReader* const token_br) {
480 VP8BandProbas (* const bands)[NUM_BANDS] = dec->proba_.bands_;
481 const VP8BandProbas* ac_proba;
482 const VP8QuantMatrix* const q = &dec->dqm_[dec->segment_];
483 VP8MBData* const block = dec->mb_data_ + dec->mb_x_;
484 int16_t* dst = block->coeffs_;
485 VP8MB* const left_mb = dec->mb_info_ - 1;
486 uint8_t tnz, lnz;
487 uint32_t non_zero_y = 0;
488 uint32_t non_zero_uv = 0;
489 int x, y, ch;
490 uint32_t out_t_nz, out_l_nz;
491 int first;
493 memset(dst, 0, 384 * sizeof(*dst));
494 if (!block->is_i4x4_) { // parse DC
495 int16_t dc[16] = { 0 };
496 const int ctx = mb->nz_dc_ + left_mb->nz_dc_;
497 const int nz = GetCoeffs(token_br, bands[1], ctx, q->y2_mat_, 0, dc);
498 mb->nz_dc_ = left_mb->nz_dc_ = (nz > 0);
499 if (nz > 1) { // more than just the DC -> perform the full transform
500 VP8TransformWHT(dc, dst);
501 } else { // only DC is non-zero -> inlined simplified transform
502 int i;
503 const int dc0 = (dc[0] + 3) >> 3;
504 for (i = 0; i < 16 * 16; i += 16) dst[i] = dc0;
506 first = 1;
507 ac_proba = bands[0];
508 } else {
509 first = 0;
510 ac_proba = bands[3];
513 tnz = mb->nz_ & 0x0f;
514 lnz = left_mb->nz_ & 0x0f;
515 for (y = 0; y < 4; ++y) {
516 int l = lnz & 1;
517 uint32_t nz_coeffs = 0;
518 for (x = 0; x < 4; ++x) {
519 const int ctx = l + (tnz & 1);
520 const int nz = GetCoeffs(token_br, ac_proba, ctx, q->y1_mat_, first, dst);
521 l = (nz > first);
522 tnz = (tnz >> 1) | (l << 7);
523 nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
524 dst += 16;
526 tnz >>= 4;
527 lnz = (lnz >> 1) | (l << 7);
528 non_zero_y = (non_zero_y << 8) | nz_coeffs;
530 out_t_nz = tnz;
531 out_l_nz = lnz >> 4;
533 for (ch = 0; ch < 4; ch += 2) {
534 uint32_t nz_coeffs = 0;
535 tnz = mb->nz_ >> (4 + ch);
536 lnz = left_mb->nz_ >> (4 + ch);
537 for (y = 0; y < 2; ++y) {
538 int l = lnz & 1;
539 for (x = 0; x < 2; ++x) {
540 const int ctx = l + (tnz & 1);
541 const int nz = GetCoeffs(token_br, bands[2], ctx, q->uv_mat_, 0, dst);
542 l = (nz > 0);
543 tnz = (tnz >> 1) | (l << 3);
544 nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
545 dst += 16;
547 tnz >>= 2;
548 lnz = (lnz >> 1) | (l << 5);
550 // Note: we don't really need the per-4x4 details for U/V blocks.
551 non_zero_uv |= nz_coeffs << (4 * ch);
552 out_t_nz |= (tnz << 4) << ch;
553 out_l_nz |= (lnz & 0xf0) << ch;
555 mb->nz_ = out_t_nz;
556 left_mb->nz_ = out_l_nz;
558 block->non_zero_y_ = non_zero_y;
559 block->non_zero_uv_ = non_zero_uv;
561 // We look at the mode-code of each block and check if some blocks have less
562 // than three non-zero coeffs (code < 2). This is to avoid dithering flat and
563 // empty blocks.
564 block->dither_ = (non_zero_uv & 0xaaaa) ? 0 : q->dither_;
566 return !(non_zero_y | non_zero_uv); // will be used for further optimization
569 //------------------------------------------------------------------------------
570 // Main loop
572 int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) {
573 VP8BitReader* const br = &dec->br_;
574 VP8MB* const left = dec->mb_info_ - 1;
575 VP8MB* const mb = dec->mb_info_ + dec->mb_x_;
576 VP8MBData* const block = dec->mb_data_ + dec->mb_x_;
577 int skip;
579 // Note: we don't save segment map (yet), as we don't expect
580 // to decode more than 1 keyframe.
581 if (dec->segment_hdr_.update_map_) {
582 // Hardcoded tree parsing
583 dec->segment_ = !VP8GetBit(br, dec->proba_.segments_[0]) ?
584 VP8GetBit(br, dec->proba_.segments_[1]) :
585 2 + VP8GetBit(br, dec->proba_.segments_[2]);
587 skip = dec->use_skip_proba_ ? VP8GetBit(br, dec->skip_p_) : 0;
589 VP8ParseIntraMode(br, dec);
590 if (br->eof_) {
591 return 0;
594 if (!skip) {
595 skip = ParseResiduals(dec, mb, token_br);
596 } else {
597 left->nz_ = mb->nz_ = 0;
598 if (!block->is_i4x4_) {
599 left->nz_dc_ = mb->nz_dc_ = 0;
601 block->non_zero_y_ = 0;
602 block->non_zero_uv_ = 0;
605 if (dec->filter_type_ > 0) { // store filter info
606 VP8FInfo* const finfo = dec->f_info_ + dec->mb_x_;
607 *finfo = dec->fstrengths_[dec->segment_][block->is_i4x4_];
608 finfo->f_inner_ |= !skip;
611 return !token_br->eof_;
614 void VP8InitScanline(VP8Decoder* const dec) {
615 VP8MB* const left = dec->mb_info_ - 1;
616 left->nz_ = 0;
617 left->nz_dc_ = 0;
618 memset(dec->intra_l_, B_DC_PRED, sizeof(dec->intra_l_));
619 dec->mb_x_ = 0;
622 static int ParseFrame(VP8Decoder* const dec, VP8Io* io) {
623 for (dec->mb_y_ = 0; dec->mb_y_ < dec->br_mb_y_; ++dec->mb_y_) {
624 // Parse bitstream for this row.
625 VP8BitReader* const token_br =
626 &dec->parts_[dec->mb_y_ & (dec->num_parts_ - 1)];
627 for (; dec->mb_x_ < dec->mb_w_; ++dec->mb_x_) {
628 if (!VP8DecodeMB(dec, token_br)) {
629 return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
630 "Premature end-of-file encountered.");
633 VP8InitScanline(dec); // Prepare for next scanline
635 // Reconstruct, filter and emit the row.
636 if (!VP8ProcessRow(dec, io)) {
637 return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted.");
640 if (dec->mt_method_ > 0) {
641 if (!WebPWorkerSync(&dec->worker_)) return 0;
644 // Finish
645 #ifdef WEBP_EXPERIMENTAL_FEATURES
646 if (dec->layer_data_size_ > 0) {
647 if (!VP8DecodeLayer(dec)) {
648 return 0;
651 #endif
653 return 1;
656 // Main entry point
657 int VP8Decode(VP8Decoder* const dec, VP8Io* const io) {
658 int ok = 0;
659 if (dec == NULL) {
660 return 0;
662 if (io == NULL) {
663 return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
664 "NULL VP8Io parameter in VP8Decode().");
667 if (!dec->ready_) {
668 if (!VP8GetHeaders(dec, io)) {
669 return 0;
672 assert(dec->ready_);
674 // Finish setting up the decoding parameter. Will call io->setup().
675 ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK);
676 if (ok) { // good to go.
677 // Will allocate memory and prepare everything.
678 if (ok) ok = VP8InitFrame(dec, io);
680 // Main decoding loop
681 if (ok) ok = ParseFrame(dec, io);
683 // Exit.
684 ok &= VP8ExitCritical(dec, io);
687 if (!ok) {
688 VP8Clear(dec);
689 return 0;
692 dec->ready_ = 0;
693 return ok;
696 void VP8Clear(VP8Decoder* const dec) {
697 if (dec == NULL) {
698 return;
700 if (dec->mt_method_ > 0) {
701 WebPWorkerEnd(&dec->worker_);
703 ALPHDelete(dec->alph_dec_);
704 dec->alph_dec_ = NULL;
705 free(dec->mem_);
706 dec->mem_ = NULL;
707 dec->mem_size_ = 0;
708 memset(&dec->br_, 0, sizeof(dec->br_));
709 dec->ready_ = 0;
712 //------------------------------------------------------------------------------