github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/webp/libwebp/src/dec/vp8.c (about) 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@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.h" 21 22 //------------------------------------------------------------------------------ 23 24 int WebPGetDecoderVersion(void) { 25 return (DEC_MAJ_VERSION << 16) | (DEC_MIN_VERSION << 8) | DEC_REV_VERSION; 26 } 27 28 //------------------------------------------------------------------------------ 29 // VP8Decoder 30 31 static void SetOk(VP8Decoder* const dec) { 32 dec->status_ = VP8_STATUS_OK; 33 dec->error_msg_ = "OK"; 34 } 35 36 int VP8InitIoInternal(VP8Io* const io, int version) { 37 if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) { 38 return 0; // mismatch error 39 } 40 if (io != NULL) { 41 memset(io, 0, sizeof(*io)); 42 } 43 return 1; 44 } 45 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; 53 } 54 return dec; 55 } 56 57 VP8StatusCode VP8Status(VP8Decoder* const dec) { 58 if (!dec) return VP8_STATUS_INVALID_PARAM; 59 return dec->status_; 60 } 61 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_; 66 } 67 68 void VP8Delete(VP8Decoder* const dec) { 69 if (dec != NULL) { 70 VP8Clear(dec); 71 free(dec); 72 } 73 } 74 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; 85 } 86 return 0; 87 } 88 89 //------------------------------------------------------------------------------ 90 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); 94 } 95 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 100 } 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; 109 110 if (!key_frame) { // Not a keyframe. 111 return 0; 112 } 113 114 if (((bits >> 1) & 7) > 3) { 115 return 0; // unknown profile 116 } 117 if (!((bits >> 4) & 1)) { 118 return 0; // first frame is invisible! 119 } 120 if (((bits >> 5)) >= chunk_size) { // partition_length 121 return 0; // inconsistent size information. 122 } 123 if (w == 0 || h == 0) { 124 return 0; // We don't support both width and height to be zero. 125 } 126 127 if (width) { 128 *width = w; 129 } 130 if (height) { 131 *height = h; 132 } 133 134 return 1; 135 } 136 } 137 138 //------------------------------------------------------------------------------ 139 // Header parsing 140 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_)); 148 } 149 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; 163 } 164 for (s = 0; s < NUM_MB_SEGMENTS; ++s) { 165 hdr->filter_strength_[s] = VP8Get(br) ? VP8GetSignedValue(br, 6) : 0; 166 } 167 } 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; 172 } 173 } 174 } else { 175 hdr->update_map_ = 0; 176 } 177 return !br->eof_; 178 } 179 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; 197 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; 204 } 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; 212 } 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 216 } 217 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); 231 } 232 } 233 for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) { 234 if (VP8Get(br)) { 235 hdr->mode_lf_delta_[i] = VP8GetSignedValue(br, 6); 236 } 237 } 238 } 239 } 240 dec->filter_type_ = (hdr->level_ == 0) ? 0 : hdr->simple_ ? 1 : 2; 241 return !br->eof_; 242 } 243 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; 252 253 if (dec == NULL) { 254 return 0; 255 } 256 SetOk(dec); 257 if (io == NULL) { 258 return VP8SetError(dec, VP8_STATUS_INVALID_PARAM, 259 "null VP8Io passed to VP8GetHeaders()"); 260 } 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."); 266 } 267 268 // Paragraph 9.1 269 { 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; 284 } 285 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"); 292 } 293 if (!VP8CheckSignature(buf, buf_size)) { 294 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, 295 "Bad code word"); 296 } 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; 303 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 317 318 VP8ResetProba(&dec->proba_); 319 ResetSegmentHeader(&dec->segment_hdr_); 320 dec->segment_ = 0; // default for intra 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 #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; 373 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."); 378 } 379 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]; 385 } 386 #endif 387 388 // sanitized state 389 dec->ready_ = 1; 390 return 1; 391 } 392 393 //------------------------------------------------------------------------------ 394 // Residual decoding (Paragraph 13.2 / 13.3) 395 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 399 }; 400 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 409 }; 410 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]); 419 } 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); 427 } 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); 436 } 437 v += 3 + (8 << cat); 438 } 439 } 440 return v; 441 } 442 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 451 } 452 while (!VP8GetBit(br, p[1])) { // sequence of zero coeffs 453 p = prob[kBands[++n]].probas_[0]; 454 if (n == 16) return 16; 455 } 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]; 465 } 466 out[kZigzag[n]] = VP8GetSigned(br, v) * dq[n > 0]; 467 } 468 } 469 return 16; 470 } 471 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; 476 } 477 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; 492 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; 505 } 506 first = 1; 507 ac_proba = bands[0]; 508 } else { 509 first = 0; 510 ac_proba = bands[3]; 511 } 512 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; 525 } 526 tnz >>= 4; 527 lnz = (lnz >> 1) | (l << 7); 528 non_zero_y = (non_zero_y << 8) | nz_coeffs; 529 } 530 out_t_nz = tnz; 531 out_l_nz = lnz >> 4; 532 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; 546 } 547 tnz >>= 2; 548 lnz = (lnz >> 1) | (l << 5); 549 } 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; 554 } 555 mb->nz_ = out_t_nz; 556 left_mb->nz_ = out_l_nz; 557 558 block->non_zero_y_ = non_zero_y; 559 block->non_zero_uv_ = non_zero_uv; 560 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_; 565 566 return !(non_zero_y | non_zero_uv); // will be used for further optimization 567 } 568 569 //------------------------------------------------------------------------------ 570 // Main loop 571 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; 578 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]); 586 } 587 skip = dec->use_skip_proba_ ? VP8GetBit(br, dec->skip_p_) : 0; 588 589 VP8ParseIntraMode(br, dec); 590 if (br->eof_) { 591 return 0; 592 } 593 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; 600 } 601 block->non_zero_y_ = 0; 602 block->non_zero_uv_ = 0; 603 } 604 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; 609 } 610 611 return !token_br->eof_; 612 } 613 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; 620 } 621 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."); 631 } 632 } 633 VP8InitScanline(dec); // Prepare for next scanline 634 635 // Reconstruct, filter and emit the row. 636 if (!VP8ProcessRow(dec, io)) { 637 return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted."); 638 } 639 } 640 if (dec->mt_method_ > 0) { 641 if (!WebPWorkerSync(&dec->worker_)) return 0; 642 } 643 644 // Finish 645 #ifdef WEBP_EXPERIMENTAL_FEATURES 646 if (dec->layer_data_size_ > 0) { 647 if (!VP8DecodeLayer(dec)) { 648 return 0; 649 } 650 } 651 #endif 652 653 return 1; 654 } 655 656 // Main entry point 657 int VP8Decode(VP8Decoder* const dec, VP8Io* const io) { 658 int ok = 0; 659 if (dec == NULL) { 660 return 0; 661 } 662 if (io == NULL) { 663 return VP8SetError(dec, VP8_STATUS_INVALID_PARAM, 664 "NULL VP8Io parameter in VP8Decode()."); 665 } 666 667 if (!dec->ready_) { 668 if (!VP8GetHeaders(dec, io)) { 669 return 0; 670 } 671 } 672 assert(dec->ready_); 673 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); 679 680 // Main decoding loop 681 if (ok) ok = ParseFrame(dec, io); 682 683 // Exit. 684 ok &= VP8ExitCritical(dec, io); 685 } 686 687 if (!ok) { 688 VP8Clear(dec); 689 return 0; 690 } 691 692 dec->ready_ = 0; 693 return ok; 694 } 695 696 void VP8Clear(VP8Decoder* const dec) { 697 if (dec == NULL) { 698 return; 699 } 700 if (dec->mt_method_ > 0) { 701 WebPWorkerEnd(&dec->worker_); 702 } 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; 710 } 711 712 //------------------------------------------------------------------------------ 713