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 11 // 12 // Author: Skal (pascal.massimino (at) gmail.com) 13 14 #include <stdlib.h> 15 16 #include "./alphai.h" 17 #include "./vp8i.h" 18 #include "./vp8li.h" 19 #include "./webpi.h" 20 #include "../utils/bit_reader_inl.h" 21 #include "../utils/utils.h" 22 23 //------------------------------------------------------------------------------ 24 25 int WebPGetDecoderVersion(void) { 26 return (DEC_MAJ_VERSION << 16) | (DEC_MIN_VERSION << 8) | DEC_REV_VERSION; 27 } 28 29 //------------------------------------------------------------------------------ 30 // VP8Decoder 31 32 static void SetOk(VP8Decoder* const dec) { 33 dec->status_ = VP8_STATUS_OK; 34 dec->error_msg_ = "OK"; 35 } 36 37 int VP8InitIoInternal(VP8Io* const io, int version) { 38 if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) { 39 return 0; // mismatch error 40 } 41 if (io != NULL) { 42 memset(io, 0, sizeof(*io)); 43 } 44 return 1; 45 } 46 47 VP8Decoder* VP8New(void) { 48 VP8Decoder* const dec = (VP8Decoder*)WebPSafeCalloc(1ULL, sizeof(*dec)); 49 if (dec != NULL) { 50 SetOk(dec); 51 WebPGetWorkerInterface()->Init(&dec->worker_); 52 dec->ready_ = 0; 53 dec->num_parts_ = 1; 54 } 55 return dec; 56 } 57 58 VP8StatusCode VP8Status(VP8Decoder* const dec) { 59 if (!dec) return VP8_STATUS_INVALID_PARAM; 60 return dec->status_; 61 } 62 63 const char* VP8StatusMessage(VP8Decoder* const dec) { 64 if (dec == NULL) return "no object"; 65 if (!dec->error_msg_) return "OK"; 66 return dec->error_msg_; 67 } 68 69 void VP8Delete(VP8Decoder* const dec) { 70 if (dec != NULL) { 71 VP8Clear(dec); 72 WebPSafeFree(dec); 73 } 74 } 75 76 int VP8SetError(VP8Decoder* const dec, 77 VP8StatusCode error, const char* const msg) { 78 // TODO This check would be unnecessary if alpha decompression was separated 79 // from VP8ProcessRow/FinishRow. This avoids setting 'dec->status_' to 80 // something other than VP8_STATUS_BITSTREAM_ERROR on alpha decompression 81 // failure. 82 if (dec->status_ == VP8_STATUS_OK) { 83 dec->status_ = error; 84 dec->error_msg_ = msg; 85 dec->ready_ = 0; 86 } 87 return 0; 88 } 89 90 //------------------------------------------------------------------------------ 91 92 int VP8CheckSignature(const uint8_t* const data, size_t data_size) { 93 return (data_size >= 3 && 94 data[0] == 0x9d && data[1] == 0x01 && data[2] == 0x2a); 95 } 96 97 int VP8GetInfo(const uint8_t* data, size_t data_size, size_t chunk_size, 98 int* const width, int* const height) { 99 if (data == NULL || data_size < VP8_FRAME_HEADER_SIZE) { 100 return 0; // not enough data 101 } 102 // check signature 103 if (!VP8CheckSignature(data + 3, data_size - 3)) { 104 return 0; // Wrong signature. 105 } else { 106 const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16); 107 const int key_frame = !(bits & 1); 108 const int w = ((data[7] << 8) | data[6]) & 0x3fff; 109 const int h = ((data[9] << 8) | data[8]) & 0x3fff; 110 111 if (!key_frame) { // Not a keyframe. 112 return 0; 113 } 114 115 if (((bits >> 1) & 7) > 3) { 116 return 0; // unknown profile 117 } 118 if (!((bits >> 4) & 1)) { 119 return 0; // first frame is invisible! 120 } 121 if (((bits >> 5)) >= chunk_size) { // partition_length 122 return 0; // inconsistent size information. 123 } 124 if (w == 0 || h == 0) { 125 return 0; // We don't support both width and height to be zero. 126 } 127 128 if (width) { 129 *width = w; 130 } 131 if (height) { 132 *height = h; 133 } 134 135 return 1; 136 } 137 } 138 139 //------------------------------------------------------------------------------ 140 // Header parsing 141 142 static void ResetSegmentHeader(VP8SegmentHeader* const hdr) { 143 assert(hdr != NULL); 144 hdr->use_segment_ = 0; 145 hdr->update_map_ = 0; 146 hdr->absolute_delta_ = 1; 147 memset(hdr->quantizer_, 0, sizeof(hdr->quantizer_)); 148 memset(hdr->filter_strength_, 0, sizeof(hdr->filter_strength_)); 149 } 150 151 // Paragraph 9.3 152 static int ParseSegmentHeader(VP8BitReader* br, 153 VP8SegmentHeader* hdr, VP8Proba* proba) { 154 assert(br != NULL); 155 assert(hdr != NULL); 156 hdr->use_segment_ = VP8Get(br); 157 if (hdr->use_segment_) { 158 hdr->update_map_ = VP8Get(br); 159 if (VP8Get(br)) { // update data 160 int s; 161 hdr->absolute_delta_ = VP8Get(br); 162 for (s = 0; s < NUM_MB_SEGMENTS; ++s) { 163 hdr->quantizer_[s] = VP8Get(br) ? VP8GetSignedValue(br, 7) : 0; 164 } 165 for (s = 0; s < NUM_MB_SEGMENTS; ++s) { 166 hdr->filter_strength_[s] = VP8Get(br) ? VP8GetSignedValue(br, 6) : 0; 167 } 168 } 169 if (hdr->update_map_) { 170 int s; 171 for (s = 0; s < MB_FEATURE_TREE_PROBS; ++s) { 172 proba->segments_[s] = VP8Get(br) ? VP8GetValue(br, 8) : 255u; 173 } 174 } 175 } else { 176 hdr->update_map_ = 0; 177 } 178 return !br->eof_; 179 } 180 181 // Paragraph 9.5 182 // This function returns VP8_STATUS_SUSPENDED if we don't have all the 183 // necessary data in 'buf'. 184 // This case is not necessarily an error (for incremental decoding). 185 // Still, no bitreader is ever initialized to make it possible to read 186 // unavailable memory. 187 // If we don't even have the partitions' sizes, than VP8_STATUS_NOT_ENOUGH_DATA 188 // is returned, and this is an unrecoverable error. 189 // If the partitions were positioned ok, VP8_STATUS_OK is returned. 190 static VP8StatusCode ParsePartitions(VP8Decoder* const dec, 191 const uint8_t* buf, size_t size) { 192 VP8BitReader* const br = &dec->br_; 193 const uint8_t* sz = buf; 194 const uint8_t* buf_end = buf + size; 195 const uint8_t* part_start; 196 int last_part; 197 int p; 198 199 dec->num_parts_ = 1 << VP8GetValue(br, 2); 200 last_part = dec->num_parts_ - 1; 201 part_start = buf + last_part * 3; 202 if (buf_end < part_start) { 203 // we can't even read the sizes with sz[]! That's a failure. 204 return VP8_STATUS_NOT_ENOUGH_DATA; 205 } 206 for (p = 0; p < last_part; ++p) { 207 const uint32_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16); 208 const uint8_t* part_end = part_start + psize; 209 if (part_end > buf_end) part_end = buf_end; 210 VP8InitBitReader(dec->parts_ + p, part_start, part_end); 211 part_start = part_end; 212 sz += 3; 213 } 214 VP8InitBitReader(dec->parts_ + last_part, part_start, buf_end); 215 return (part_start < buf_end) ? VP8_STATUS_OK : 216 VP8_STATUS_SUSPENDED; // Init is ok, but there's not enough data 217 } 218 219 // Paragraph 9.4 220 static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) { 221 VP8FilterHeader* const hdr = &dec->filter_hdr_; 222 hdr->simple_ = VP8Get(br); 223 hdr->level_ = VP8GetValue(br, 6); 224 hdr->sharpness_ = VP8GetValue(br, 3); 225 hdr->use_lf_delta_ = VP8Get(br); 226 if (hdr->use_lf_delta_) { 227 if (VP8Get(br)) { // update lf-delta? 228 int i; 229 for (i = 0; i < NUM_REF_LF_DELTAS; ++i) { 230 if (VP8Get(br)) { 231 hdr->ref_lf_delta_[i] = VP8GetSignedValue(br, 6); 232 } 233 } 234 for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) { 235 if (VP8Get(br)) { 236 hdr->mode_lf_delta_[i] = VP8GetSignedValue(br, 6); 237 } 238 } 239 } 240 } 241 dec->filter_type_ = (hdr->level_ == 0) ? 0 : hdr->simple_ ? 1 : 2; 242 return !br->eof_; 243 } 244 245 // Topmost call 246 int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) { 247 const uint8_t* buf; 248 size_t buf_size; 249 VP8FrameHeader* frm_hdr; 250 VP8PictureHeader* pic_hdr; 251 VP8BitReader* br; 252 VP8StatusCode status; 253 254 if (dec == NULL) { 255 return 0; 256 } 257 SetOk(dec); 258 if (io == NULL) { 259 return VP8SetError(dec, VP8_STATUS_INVALID_PARAM, 260 "null VP8Io passed to VP8GetHeaders()"); 261 } 262 buf = io->data; 263 buf_size = io->data_size; 264 if (buf_size < 4) { 265 return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, 266 "Truncated header."); 267 } 268 269 // Paragraph 9.1 270 { 271 const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16); 272 frm_hdr = &dec->frm_hdr_; 273 frm_hdr->key_frame_ = !(bits & 1); 274 frm_hdr->profile_ = (bits >> 1) & 7; 275 frm_hdr->show_ = (bits >> 4) & 1; 276 frm_hdr->partition_length_ = (bits >> 5); 277 if (frm_hdr->profile_ > 3) 278 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, 279 "Incorrect keyframe parameters."); 280 if (!frm_hdr->show_) 281 return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE, 282 "Frame not displayable."); 283 buf += 3; 284 buf_size -= 3; 285 } 286 287 pic_hdr = &dec->pic_hdr_; 288 if (frm_hdr->key_frame_) { 289 // Paragraph 9.2 290 if (buf_size < 7) { 291 return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, 292 "cannot parse picture header"); 293 } 294 if (!VP8CheckSignature(buf, buf_size)) { 295 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, 296 "Bad code word"); 297 } 298 pic_hdr->width_ = ((buf[4] << 8) | buf[3]) & 0x3fff; 299 pic_hdr->xscale_ = buf[4] >> 6; // ratio: 1, 5/4 5/3 or 2 300 pic_hdr->height_ = ((buf[6] << 8) | buf[5]) & 0x3fff; 301 pic_hdr->yscale_ = buf[6] >> 6; 302 buf += 7; 303 buf_size -= 7; 304 305 dec->mb_w_ = (pic_hdr->width_ + 15) >> 4; 306 dec->mb_h_ = (pic_hdr->height_ + 15) >> 4; 307 // Setup default output area (can be later modified during io->setup()) 308 io->width = pic_hdr->width_; 309 io->height = pic_hdr->height_; 310 io->use_scaling = 0; 311 io->use_cropping = 0; 312 io->crop_top = 0; 313 io->crop_left = 0; 314 io->crop_right = io->width; 315 io->crop_bottom = io->height; 316 io->mb_w = io->width; // sanity check 317 io->mb_h = io->height; // ditto 318 319 VP8ResetProba(&dec->proba_); 320 ResetSegmentHeader(&dec->segment_hdr_); 321 } 322 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"); 328 } 329 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_; 334 335 if (frm_hdr->key_frame_) { 336 pic_hdr->colorspace_ = VP8Get(br); 337 pic_hdr->clamp_type_ = VP8Get(br); 338 } 339 if (!ParseSegmentHeader(br, &dec->segment_hdr_, &dec->proba_)) { 340 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, 341 "cannot parse segment header"); 342 } 343 // Filter specs 344 if (!ParseFilterHeader(br, dec)) { 345 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, 346 "cannot parse filter header"); 347 } 348 status = ParsePartitions(dec, buf, buf_size); 349 if (status != VP8_STATUS_OK) { 350 return VP8SetError(dec, status, "cannot parse partitions"); 351 } 352 353 // quantizer change 354 VP8ParseQuant(dec); 355 356 // Frame buffer marking 357 if (!frm_hdr->key_frame_) { 358 return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE, 359 "Not a key frame."); 360 } 361 362 VP8Get(br); // ignore the value of update_proba_ 363 364 VP8ParseProba(br, dec); 365 366 // sanitized state 367 dec->ready_ = 1; 368 return 1; 369 } 370 371 //------------------------------------------------------------------------------ 372 // Residual decoding (Paragraph 13.2 / 13.3) 373 374 static const int kBands[16 + 1] = { 375 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 376 0 // extra entry as sentinel 377 }; 378 379 static const uint8_t kCat3[] = { 173, 148, 140, 0 }; 380 static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 }; 381 static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 }; 382 static const uint8_t kCat6[] = 383 { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0 }; 384 static const uint8_t* const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 }; 385 static const uint8_t kZigzag[16] = { 386 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 387 }; 388 389 // See section 13-2: http://tools.ietf.org/html/rfc6386#section-13.2 390 static int GetLargeValue(VP8BitReader* const br, const uint8_t* const p) { 391 int v; 392 if (!VP8GetBit(br, p[3])) { 393 if (!VP8GetBit(br, p[4])) { 394 v = 2; 395 } else { 396 v = 3 + VP8GetBit(br, p[5]); 397 } 398 } else { 399 if (!VP8GetBit(br, p[6])) { 400 if (!VP8GetBit(br, p[7])) { 401 v = 5 + VP8GetBit(br, 159); 402 } else { 403 v = 7 + 2 * VP8GetBit(br, 165); 404 v += VP8GetBit(br, 145); 405 } 406 } else { 407 const uint8_t* tab; 408 const int bit1 = VP8GetBit(br, p[8]); 409 const int bit0 = VP8GetBit(br, p[9 + bit1]); 410 const int cat = 2 * bit1 + bit0; 411 v = 0; 412 for (tab = kCat3456[cat]; *tab; ++tab) { 413 v += v + VP8GetBit(br, *tab); 414 } 415 v += 3 + (8 << cat); 416 } 417 } 418 return v; 419 } 420 421 // Returns the position of the last non-zero coeff plus one 422 static int GetCoeffs(VP8BitReader* const br, const VP8BandProbas* const prob, 423 int ctx, const quant_t dq, int n, int16_t* out) { 424 // n is either 0 or 1 here. kBands[n] is not necessary for extracting '*p'. 425 const uint8_t* p = prob[n].probas_[ctx]; 426 for (; n < 16; ++n) { 427 if (!VP8GetBit(br, p[0])) { 428 return n; // previous coeff was last non-zero coeff 429 } 430 while (!VP8GetBit(br, p[1])) { // sequence of zero coeffs 431 p = prob[kBands[++n]].probas_[0]; 432 if (n == 16) return 16; 433 } 434 { // non zero coeff 435 const VP8ProbaArray* const p_ctx = &prob[kBands[n + 1]].probas_[0]; 436 int v; 437 if (!VP8GetBit(br, p[2])) { 438 v = 1; 439 p = p_ctx[1]; 440 } else { 441 v = GetLargeValue(br, p); 442 p = p_ctx[2]; 443 } 444 out[kZigzag[n]] = VP8GetSigned(br, v) * dq[n > 0]; 445 } 446 } 447 return 16; 448 } 449 450 static WEBP_INLINE uint32_t NzCodeBits(uint32_t nz_coeffs, int nz, int dc_nz) { 451 nz_coeffs <<= 2; 452 nz_coeffs |= (nz > 3) ? 3 : (nz > 1) ? 2 : dc_nz; 453 return nz_coeffs; 454 } 455 456 static int ParseResiduals(VP8Decoder* const dec, 457 VP8MB* const mb, VP8BitReader* const token_br) { 458 VP8BandProbas (* const bands)[NUM_BANDS] = dec->proba_.bands_; 459 const VP8BandProbas* ac_proba; 460 VP8MBData* const block = dec->mb_data_ + dec->mb_x_; 461 const VP8QuantMatrix* const q = &dec->dqm_[block->segment_]; 462 int16_t* dst = block->coeffs_; 463 VP8MB* const left_mb = dec->mb_info_ - 1; 464 uint8_t tnz, lnz; 465 uint32_t non_zero_y = 0; 466 uint32_t non_zero_uv = 0; 467 int x, y, ch; 468 uint32_t out_t_nz, out_l_nz; 469 int first; 470 471 memset(dst, 0, 384 * sizeof(*dst)); 472 if (!block->is_i4x4_) { // parse DC 473 int16_t dc[16] = { 0 }; 474 const int ctx = mb->nz_dc_ + left_mb->nz_dc_; 475 const int nz = GetCoeffs(token_br, bands[1], ctx, q->y2_mat_, 0, dc); 476 mb->nz_dc_ = left_mb->nz_dc_ = (nz > 0); 477 if (nz > 1) { // more than just the DC -> perform the full transform 478 VP8TransformWHT(dc, dst); 479 } else { // only DC is non-zero -> inlined simplified transform 480 int i; 481 const int dc0 = (dc[0] + 3) >> 3; 482 for (i = 0; i < 16 * 16; i += 16) dst[i] = dc0; 483 } 484 first = 1; 485 ac_proba = bands[0]; 486 } else { 487 first = 0; 488 ac_proba = bands[3]; 489 } 490 491 tnz = mb->nz_ & 0x0f; 492 lnz = left_mb->nz_ & 0x0f; 493 for (y = 0; y < 4; ++y) { 494 int l = lnz & 1; 495 uint32_t nz_coeffs = 0; 496 for (x = 0; x < 4; ++x) { 497 const int ctx = l + (tnz & 1); 498 const int nz = GetCoeffs(token_br, ac_proba, ctx, q->y1_mat_, first, dst); 499 l = (nz > first); 500 tnz = (tnz >> 1) | (l << 7); 501 nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0); 502 dst += 16; 503 } 504 tnz >>= 4; 505 lnz = (lnz >> 1) | (l << 7); 506 non_zero_y = (non_zero_y << 8) | nz_coeffs; 507 } 508 out_t_nz = tnz; 509 out_l_nz = lnz >> 4; 510 511 for (ch = 0; ch < 4; ch += 2) { 512 uint32_t nz_coeffs = 0; 513 tnz = mb->nz_ >> (4 + ch); 514 lnz = left_mb->nz_ >> (4 + ch); 515 for (y = 0; y < 2; ++y) { 516 int l = lnz & 1; 517 for (x = 0; x < 2; ++x) { 518 const int ctx = l + (tnz & 1); 519 const int nz = GetCoeffs(token_br, bands[2], ctx, q->uv_mat_, 0, dst); 520 l = (nz > 0); 521 tnz = (tnz >> 1) | (l << 3); 522 nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0); 523 dst += 16; 524 } 525 tnz >>= 2; 526 lnz = (lnz >> 1) | (l << 5); 527 } 528 // Note: we don't really need the per-4x4 details for U/V blocks. 529 non_zero_uv |= nz_coeffs << (4 * ch); 530 out_t_nz |= (tnz << 4) << ch; 531 out_l_nz |= (lnz & 0xf0) << ch; 532 } 533 mb->nz_ = out_t_nz; 534 left_mb->nz_ = out_l_nz; 535 536 block->non_zero_y_ = non_zero_y; 537 block->non_zero_uv_ = non_zero_uv; 538 539 // We look at the mode-code of each block and check if some blocks have less 540 // than three non-zero coeffs (code < 2). This is to avoid dithering flat and 541 // empty blocks. 542 block->dither_ = (non_zero_uv & 0xaaaa) ? 0 : q->dither_; 543 544 return !(non_zero_y | non_zero_uv); // will be used for further optimization 545 } 546 547 //------------------------------------------------------------------------------ 548 // Main loop 549 550 int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) { 551 VP8MB* const left = dec->mb_info_ - 1; 552 VP8MB* const mb = dec->mb_info_ + dec->mb_x_; 553 VP8MBData* const block = dec->mb_data_ + dec->mb_x_; 554 int skip = dec->use_skip_proba_ ? block->skip_ : 0; 555 556 if (!skip) { 557 skip = ParseResiduals(dec, mb, token_br); 558 } else { 559 left->nz_ = mb->nz_ = 0; 560 if (!block->is_i4x4_) { 561 left->nz_dc_ = mb->nz_dc_ = 0; 562 } 563 block->non_zero_y_ = 0; 564 block->non_zero_uv_ = 0; 565 } 566 567 if (dec->filter_type_ > 0) { // store filter info 568 VP8FInfo* const finfo = dec->f_info_ + dec->mb_x_; 569 *finfo = dec->fstrengths_[block->segment_][block->is_i4x4_]; 570 finfo->f_inner_ |= !skip; 571 } 572 573 return !token_br->eof_; 574 } 575 576 void VP8InitScanline(VP8Decoder* const dec) { 577 VP8MB* const left = dec->mb_info_ - 1; 578 left->nz_ = 0; 579 left->nz_dc_ = 0; 580 memset(dec->intra_l_, B_DC_PRED, sizeof(dec->intra_l_)); 581 dec->mb_x_ = 0; 582 } 583 584 static int ParseFrame(VP8Decoder* const dec, VP8Io* io) { 585 for (dec->mb_y_ = 0; dec->mb_y_ < dec->br_mb_y_; ++dec->mb_y_) { 586 // Parse bitstream for this row. 587 VP8BitReader* const token_br = 588 &dec->parts_[dec->mb_y_ & (dec->num_parts_ - 1)]; 589 if (!VP8ParseIntraModeRow(&dec->br_, dec)) { 590 return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, 591 "Premature end-of-partition0 encountered."); 592 } 593 for (; dec->mb_x_ < dec->mb_w_; ++dec->mb_x_) { 594 if (!VP8DecodeMB(dec, token_br)) { 595 return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, 596 "Premature end-of-file encountered."); 597 } 598 } 599 VP8InitScanline(dec); // Prepare for next scanline 600 601 // Reconstruct, filter and emit the row. 602 if (!VP8ProcessRow(dec, io)) { 603 return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted."); 604 } 605 } 606 if (dec->mt_method_ > 0) { 607 if (!WebPGetWorkerInterface()->Sync(&dec->worker_)) return 0; 608 } 609 610 return 1; 611 } 612 613 // Main entry point 614 int VP8Decode(VP8Decoder* const dec, VP8Io* const io) { 615 int ok = 0; 616 if (dec == NULL) { 617 return 0; 618 } 619 if (io == NULL) { 620 return VP8SetError(dec, VP8_STATUS_INVALID_PARAM, 621 "NULL VP8Io parameter in VP8Decode()."); 622 } 623 624 if (!dec->ready_) { 625 if (!VP8GetHeaders(dec, io)) { 626 return 0; 627 } 628 } 629 assert(dec->ready_); 630 631 // Finish setting up the decoding parameter. Will call io->setup(). 632 ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK); 633 if (ok) { // good to go. 634 // Will allocate memory and prepare everything. 635 if (ok) ok = VP8InitFrame(dec, io); 636 637 // Main decoding loop 638 if (ok) ok = ParseFrame(dec, io); 639 640 // Exit. 641 ok &= VP8ExitCritical(dec, io); 642 } 643 644 if (!ok) { 645 VP8Clear(dec); 646 return 0; 647 } 648 649 dec->ready_ = 0; 650 return ok; 651 } 652 653 void VP8Clear(VP8Decoder* const dec) { 654 if (dec == NULL) { 655 return; 656 } 657 WebPGetWorkerInterface()->End(&dec->worker_); 658 ALPHDelete(dec->alph_dec_); 659 dec->alph_dec_ = NULL; 660 WebPSafeFree(dec->mem_); 661 dec->mem_ = NULL; 662 dec->mem_size_ = 0; 663 memset(&dec->br_, 0, sizeof(dec->br_)); 664 dec->ready_ = 0; 665 } 666 667 //------------------------------------------------------------------------------ 668 669