vp8_dec.c (22777B)
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 <assert.h> 15 #include <stdlib.h> 16 #include <string.h> 17 18 #include "src/dec/alphai_dec.h" 19 #include "src/dec/common_dec.h" 20 #include "src/dec/vp8_dec.h" 21 #include "src/dec/vp8i_dec.h" 22 #include "src/dec/vp8li_dec.h" 23 #include "src/dec/webpi_dec.h" 24 #include "src/dsp/cpu.h" 25 #include "src/dsp/dsp.h" 26 #include "src/utils/bit_reader_inl_utils.h" 27 #include "src/utils/bit_reader_utils.h" 28 #include "src/utils/thread_utils.h" 29 #include "src/utils/utils.h" 30 #include "src/webp/decode.h" 31 #include "src/webp/format_constants.h" 32 #include "src/webp/types.h" 33 34 //------------------------------------------------------------------------------ 35 36 int WebPGetDecoderVersion(void) { 37 return (DEC_MAJ_VERSION << 16) | (DEC_MIN_VERSION << 8) | DEC_REV_VERSION; 38 } 39 40 //------------------------------------------------------------------------------ 41 // Signature and pointer-to-function for GetCoeffs() variants below. 42 43 typedef int (*GetCoeffsFunc)(VP8BitReader* const br, 44 const VP8BandProbas* const prob[], 45 int ctx, const quant_t dq, int n, int16_t* out); 46 static volatile GetCoeffsFunc GetCoeffs = NULL; 47 48 static void InitGetCoeffs(void); 49 50 //------------------------------------------------------------------------------ 51 // VP8Decoder 52 53 static void SetOk(VP8Decoder* const dec) { 54 dec->status = VP8_STATUS_OK; 55 dec->error_msg = "OK"; 56 } 57 58 int VP8InitIoInternal(VP8Io* const io, int version) { 59 if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) { 60 return 0; // mismatch error 61 } 62 if (io != NULL) { 63 memset(io, 0, sizeof(*io)); 64 } 65 return 1; 66 } 67 68 VP8Decoder* VP8New(void) { 69 VP8Decoder* const dec = (VP8Decoder*)WebPSafeCalloc(1ULL, sizeof(*dec)); 70 if (dec != NULL) { 71 SetOk(dec); 72 WebPGetWorkerInterface()->Init(&dec->worker); 73 dec->ready = 0; 74 dec->num_parts_minus_one = 0; 75 InitGetCoeffs(); 76 } 77 return dec; 78 } 79 80 VP8StatusCode VP8Status(VP8Decoder* const dec) { 81 if (!dec) return VP8_STATUS_INVALID_PARAM; 82 return dec->status; 83 } 84 85 const char* VP8StatusMessage(VP8Decoder* const dec) { 86 if (dec == NULL) return "no object"; 87 if (!dec->error_msg) return "OK"; 88 return dec->error_msg; 89 } 90 91 void VP8Delete(VP8Decoder* const dec) { 92 if (dec != NULL) { 93 VP8Clear(dec); 94 WebPSafeFree(dec); 95 } 96 } 97 98 int VP8SetError(VP8Decoder* const dec, 99 VP8StatusCode error, const char* const msg) { 100 // VP8_STATUS_SUSPENDED is only meaningful in incremental decoding. 101 assert(dec->incremental || error != VP8_STATUS_SUSPENDED); 102 // The oldest error reported takes precedence over the new one. 103 if (dec->status == VP8_STATUS_OK) { 104 dec->status = error; 105 dec->error_msg = msg; 106 dec->ready = 0; 107 } 108 return 0; 109 } 110 111 //------------------------------------------------------------------------------ 112 113 int VP8CheckSignature(const uint8_t* const data, size_t data_size) { 114 return (data_size >= 3 && 115 data[0] == 0x9d && data[1] == 0x01 && data[2] == 0x2a); 116 } 117 118 int VP8GetInfo(const uint8_t* data, size_t data_size, size_t chunk_size, 119 int* const width, int* const height) { 120 if (data == NULL || data_size < VP8_FRAME_HEADER_SIZE) { 121 return 0; // not enough data 122 } 123 // check signature 124 if (!VP8CheckSignature(data + 3, data_size - 3)) { 125 return 0; // Wrong signature. 126 } else { 127 const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16); 128 const int key_frame = !(bits & 1); 129 const int w = ((data[7] << 8) | data[6]) & 0x3fff; 130 const int h = ((data[9] << 8) | data[8]) & 0x3fff; 131 132 if (!key_frame) { // Not a keyframe. 133 return 0; 134 } 135 136 if (((bits >> 1) & 7) > 3) { 137 return 0; // unknown profile 138 } 139 if (!((bits >> 4) & 1)) { 140 return 0; // first frame is invisible! 141 } 142 if (((bits >> 5)) >= chunk_size) { // partition_length 143 return 0; // inconsistent size information. 144 } 145 if (w == 0 || h == 0) { 146 return 0; // We don't support both width and height to be zero. 147 } 148 149 if (width) { 150 *width = w; 151 } 152 if (height) { 153 *height = h; 154 } 155 156 return 1; 157 } 158 } 159 160 //------------------------------------------------------------------------------ 161 // Header parsing 162 163 static void ResetSegmentHeader(VP8SegmentHeader* const hdr) { 164 assert(hdr != NULL); 165 hdr->use_segment = 0; 166 hdr->update_map = 0; 167 hdr->absolute_delta = 1; 168 memset(hdr->quantizer, 0, sizeof(hdr->quantizer)); 169 memset(hdr->filter_strength, 0, sizeof(hdr->filter_strength)); 170 } 171 172 // Paragraph 9.3 173 static int ParseSegmentHeader(VP8BitReader* br, 174 VP8SegmentHeader* hdr, VP8Proba* proba) { 175 assert(br != NULL); 176 assert(hdr != NULL); 177 hdr->use_segment = VP8Get(br, "global-header"); 178 if (hdr->use_segment) { 179 hdr->update_map = VP8Get(br, "global-header"); 180 if (VP8Get(br, "global-header")) { // update data 181 int s; 182 hdr->absolute_delta = VP8Get(br, "global-header"); 183 for (s = 0; s < NUM_MB_SEGMENTS; ++s) { 184 hdr->quantizer[s] = VP8Get(br, "global-header") ? 185 VP8GetSignedValue(br, 7, "global-header") : 0; 186 } 187 for (s = 0; s < NUM_MB_SEGMENTS; ++s) { 188 hdr->filter_strength[s] = VP8Get(br, "global-header") ? 189 VP8GetSignedValue(br, 6, "global-header") : 0; 190 } 191 } 192 if (hdr->update_map) { 193 int s; 194 for (s = 0; s < MB_FEATURE_TREE_PROBS; ++s) { 195 proba->segments[s] = VP8Get(br, "global-header") ? 196 VP8GetValue(br, 8, "global-header") : 255u; 197 } 198 } 199 } else { 200 hdr->update_map = 0; 201 } 202 return !br->eof; 203 } 204 205 // Paragraph 9.5 206 // If we don't have all the necessary data in 'buf', this function returns 207 // VP8_STATUS_SUSPENDED in incremental decoding, VP8_STATUS_NOT_ENOUGH_DATA 208 // otherwise. 209 // In incremental decoding, this case is not necessarily an error. Still, no 210 // bitreader is ever initialized to make it possible to read unavailable memory. 211 // If we don't even have the partitions' sizes, then VP8_STATUS_NOT_ENOUGH_DATA 212 // is returned, and this is an unrecoverable error. 213 // If the partitions were positioned ok, VP8_STATUS_OK is returned. 214 static VP8StatusCode ParsePartitions(VP8Decoder* const dec, 215 const uint8_t* buf, size_t size) { 216 VP8BitReader* const br = &dec->br; 217 const uint8_t* sz = buf; 218 const uint8_t* buf_end = buf + size; 219 const uint8_t* part_start; 220 size_t size_left = size; 221 size_t last_part; 222 size_t p; 223 224 dec->num_parts_minus_one = (1 << VP8GetValue(br, 2, "global-header")) - 1; 225 last_part = dec->num_parts_minus_one; 226 if (size < 3 * last_part) { 227 // we can't even read the sizes with sz[]! That's a failure. 228 return VP8_STATUS_NOT_ENOUGH_DATA; 229 } 230 part_start = buf + last_part * 3; 231 size_left -= last_part * 3; 232 for (p = 0; p < last_part; ++p) { 233 size_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16); 234 if (psize > size_left) psize = size_left; 235 VP8InitBitReader(dec->parts + p, part_start, psize); 236 part_start += psize; 237 size_left -= psize; 238 sz += 3; 239 } 240 VP8InitBitReader(dec->parts + last_part, part_start, size_left); 241 if (part_start < buf_end) return VP8_STATUS_OK; 242 return dec->incremental 243 ? VP8_STATUS_SUSPENDED // Init is ok, but there's not enough data 244 : VP8_STATUS_NOT_ENOUGH_DATA; 245 } 246 247 // Paragraph 9.4 248 static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) { 249 VP8FilterHeader* const hdr = &dec->filter_hdr; 250 hdr->simple = VP8Get(br, "global-header"); 251 hdr->level = VP8GetValue(br, 6, "global-header"); 252 hdr->sharpness = VP8GetValue(br, 3, "global-header"); 253 hdr->use_lf_delta = VP8Get(br, "global-header"); 254 if (hdr->use_lf_delta) { 255 if (VP8Get(br, "global-header")) { // update lf-delta? 256 int i; 257 for (i = 0; i < NUM_REF_LF_DELTAS; ++i) { 258 if (VP8Get(br, "global-header")) { 259 hdr->ref_lf_delta[i] = VP8GetSignedValue(br, 6, "global-header"); 260 } 261 } 262 for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) { 263 if (VP8Get(br, "global-header")) { 264 hdr->mode_lf_delta[i] = VP8GetSignedValue(br, 6, "global-header"); 265 } 266 } 267 } 268 } 269 dec->filter_type = (hdr->level == 0) ? 0 : hdr->simple ? 1 : 2; 270 return !br->eof; 271 } 272 273 // Topmost call 274 int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) { 275 const uint8_t* buf; 276 size_t buf_size; 277 VP8FrameHeader* frm_hdr; 278 VP8PictureHeader* pic_hdr; 279 VP8BitReader* br; 280 VP8StatusCode status; 281 282 if (dec == NULL) { 283 return 0; 284 } 285 SetOk(dec); 286 if (io == NULL) { 287 return VP8SetError(dec, VP8_STATUS_INVALID_PARAM, 288 "null VP8Io passed to VP8GetHeaders()"); 289 } 290 buf = io->data; 291 buf_size = io->data_size; 292 if (buf_size < 4) { 293 return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, 294 "Truncated header."); 295 } 296 297 // Paragraph 9.1 298 { 299 const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16); 300 frm_hdr = &dec->frm_hdr; 301 frm_hdr->key_frame = !(bits & 1); 302 frm_hdr->profile = (bits >> 1) & 7; 303 frm_hdr->show = (bits >> 4) & 1; 304 frm_hdr->partition_length = (bits >> 5); 305 if (frm_hdr->profile > 3) { 306 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, 307 "Incorrect keyframe parameters."); 308 } 309 if (!frm_hdr->show) { 310 return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE, 311 "Frame not displayable."); 312 } 313 buf += 3; 314 buf_size -= 3; 315 } 316 317 pic_hdr = &dec->pic_hdr; 318 if (frm_hdr->key_frame) { 319 // Paragraph 9.2 320 if (buf_size < 7) { 321 return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, 322 "cannot parse picture header"); 323 } 324 if (!VP8CheckSignature(buf, buf_size)) { 325 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, 326 "Bad code word"); 327 } 328 pic_hdr->width = ((buf[4] << 8) | buf[3]) & 0x3fff; 329 pic_hdr->xscale = buf[4] >> 6; // ratio: 1, 5/4 5/3 or 2 330 pic_hdr->height = ((buf[6] << 8) | buf[5]) & 0x3fff; 331 pic_hdr->yscale = buf[6] >> 6; 332 buf += 7; 333 buf_size -= 7; 334 335 dec->mb_w = (pic_hdr->width + 15) >> 4; 336 dec->mb_h = (pic_hdr->height + 15) >> 4; 337 338 // Setup default output area (can be later modified during io->setup()) 339 io->width = pic_hdr->width; 340 io->height = pic_hdr->height; 341 // IMPORTANT! use some sane dimensions in crop* and scaled* fields. 342 // So they can be used interchangeably without always testing for 343 // 'use_cropping'. 344 io->use_cropping = 0; 345 io->crop_top = 0; 346 io->crop_left = 0; 347 io->crop_right = io->width; 348 io->crop_bottom = io->height; 349 io->use_scaling = 0; 350 io->scaled_width = io->width; 351 io->scaled_height = io->height; 352 353 io->mb_w = io->width; // for soundness 354 io->mb_h = io->height; // ditto 355 356 VP8ResetProba(&dec->proba); 357 ResetSegmentHeader(&dec->segment_hdr); 358 } 359 360 // Check if we have all the partition #0 available, and initialize dec->br 361 // to read this partition (and this partition only). 362 if (frm_hdr->partition_length > buf_size) { 363 return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, 364 "bad partition length"); 365 } 366 367 br = &dec->br; 368 VP8InitBitReader(br, buf, frm_hdr->partition_length); 369 buf += frm_hdr->partition_length; 370 buf_size -= frm_hdr->partition_length; 371 372 if (frm_hdr->key_frame) { 373 pic_hdr->colorspace = VP8Get(br, "global-header"); 374 pic_hdr->clamp_type = VP8Get(br, "global-header"); 375 } 376 if (!ParseSegmentHeader(br, &dec->segment_hdr, &dec->proba)) { 377 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, 378 "cannot parse segment header"); 379 } 380 // Filter specs 381 if (!ParseFilterHeader(br, dec)) { 382 return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, 383 "cannot parse filter header"); 384 } 385 status = ParsePartitions(dec, buf, buf_size); 386 if (status != VP8_STATUS_OK) { 387 return VP8SetError(dec, status, "cannot parse partitions"); 388 } 389 390 // quantizer change 391 VP8ParseQuant(dec); 392 393 // Frame buffer marking 394 if (!frm_hdr->key_frame) { 395 return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE, 396 "Not a key frame."); 397 } 398 399 VP8Get(br, "global-header"); // ignore the value of 'update_proba' 400 401 VP8ParseProba(br, dec); 402 403 // sanitized state 404 dec->ready = 1; 405 return 1; 406 } 407 408 //------------------------------------------------------------------------------ 409 // Residual decoding (Paragraph 13.2 / 13.3) 410 411 static const uint8_t kCat3[] = { 173, 148, 140, 0 }; 412 static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 }; 413 static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 }; 414 static const uint8_t kCat6[] = 415 { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0 }; 416 static const uint8_t* const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 }; 417 static const uint8_t kZigzag[16] = { 418 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 419 }; 420 421 // See section 13-2: https://datatracker.ietf.org/doc/html/rfc6386#section-13.2 422 static int GetLargeValue(VP8BitReader* const br, const uint8_t* const p) { 423 int v; 424 if (!VP8GetBit(br, p[3], "coeffs")) { 425 if (!VP8GetBit(br, p[4], "coeffs")) { 426 v = 2; 427 } else { 428 v = 3 + VP8GetBit(br, p[5], "coeffs"); 429 } 430 } else { 431 if (!VP8GetBit(br, p[6], "coeffs")) { 432 if (!VP8GetBit(br, p[7], "coeffs")) { 433 v = 5 + VP8GetBit(br, 159, "coeffs"); 434 } else { 435 v = 7 + 2 * VP8GetBit(br, 165, "coeffs"); 436 v += VP8GetBit(br, 145, "coeffs"); 437 } 438 } else { 439 const uint8_t* tab; 440 const int bit1 = VP8GetBit(br, p[8], "coeffs"); 441 const int bit0 = VP8GetBit(br, p[9 + bit1], "coeffs"); 442 const int cat = 2 * bit1 + bit0; 443 v = 0; 444 for (tab = kCat3456[cat]; *tab; ++tab) { 445 v += v + VP8GetBit(br, *tab, "coeffs"); 446 } 447 v += 3 + (8 << cat); 448 } 449 } 450 return v; 451 } 452 453 // Returns the position of the last non-zero coeff plus one 454 static int GetCoeffsFast(VP8BitReader* const br, 455 const VP8BandProbas* const prob[], 456 int ctx, const quant_t dq, int n, int16_t* out) { 457 const uint8_t* p = prob[n]->probas[ctx]; 458 for (; n < 16; ++n) { 459 if (!VP8GetBit(br, p[0], "coeffs")) { 460 return n; // previous coeff was last non-zero coeff 461 } 462 while (!VP8GetBit(br, p[1], "coeffs")) { // sequence of zero coeffs 463 p = prob[++n]->probas[0]; 464 if (n == 16) return 16; 465 } 466 { // non zero coeff 467 const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas[0]; 468 int v; 469 if (!VP8GetBit(br, p[2], "coeffs")) { 470 v = 1; 471 p = p_ctx[1]; 472 } else { 473 v = GetLargeValue(br, p); 474 p = p_ctx[2]; 475 } 476 out[kZigzag[n]] = VP8GetSigned(br, v, "coeffs") * dq[n > 0]; 477 } 478 } 479 return 16; 480 } 481 482 // This version of GetCoeffs() uses VP8GetBitAlt() which is an alternate version 483 // of VP8GetBitAlt() targeting specific platforms. 484 static int GetCoeffsAlt(VP8BitReader* const br, 485 const VP8BandProbas* const prob[], 486 int ctx, const quant_t dq, int n, int16_t* out) { 487 const uint8_t* p = prob[n]->probas[ctx]; 488 for (; n < 16; ++n) { 489 if (!VP8GetBitAlt(br, p[0], "coeffs")) { 490 return n; // previous coeff was last non-zero coeff 491 } 492 while (!VP8GetBitAlt(br, p[1], "coeffs")) { // sequence of zero coeffs 493 p = prob[++n]->probas[0]; 494 if (n == 16) return 16; 495 } 496 { // non zero coeff 497 const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas[0]; 498 int v; 499 if (!VP8GetBitAlt(br, p[2], "coeffs")) { 500 v = 1; 501 p = p_ctx[1]; 502 } else { 503 v = GetLargeValue(br, p); 504 p = p_ctx[2]; 505 } 506 out[kZigzag[n]] = VP8GetSigned(br, v, "coeffs") * dq[n > 0]; 507 } 508 } 509 return 16; 510 } 511 512 extern VP8CPUInfo VP8GetCPUInfo; 513 514 WEBP_DSP_INIT_FUNC(InitGetCoeffs) { 515 if (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kSlowSSSE3)) { 516 GetCoeffs = GetCoeffsAlt; 517 } else { 518 GetCoeffs = GetCoeffsFast; 519 } 520 } 521 522 static WEBP_INLINE uint32_t NzCodeBits(uint32_t nz_coeffs, int nz, int dc_nz) { 523 nz_coeffs <<= 2; 524 nz_coeffs |= (nz > 3) ? 3 : (nz > 1) ? 2 : dc_nz; 525 return nz_coeffs; 526 } 527 528 static int ParseResiduals(VP8Decoder* const dec, 529 VP8MB* const mb, VP8BitReader* const token_br) { 530 const VP8BandProbas* (* const bands)[16 + 1] = dec->proba.bands_ptr; 531 const VP8BandProbas* const * ac_proba; 532 VP8MBData* const block = dec->mb_data + dec->mb_x; 533 const VP8QuantMatrix* const q = &dec->dqm[block->segment]; 534 int16_t* dst = block->coeffs; 535 VP8MB* const left_mb = dec->mb_info - 1; 536 uint8_t tnz, lnz; 537 uint32_t non_zero_y = 0; 538 uint32_t non_zero_uv = 0; 539 int x, y, ch; 540 uint32_t out_t_nz, out_l_nz; 541 int first; 542 543 memset(dst, 0, 384 * sizeof(*dst)); 544 if (!block->is_i4x4) { // parse DC 545 int16_t dc[16] = { 0 }; 546 const int ctx = mb->nz_dc + left_mb->nz_dc; 547 const int nz = GetCoeffs(token_br, bands[1], ctx, q->y2_mat, 0, dc); 548 mb->nz_dc = left_mb->nz_dc = (nz > 0); 549 if (nz > 1) { // more than just the DC -> perform the full transform 550 VP8TransformWHT(dc, dst); 551 } else { // only DC is non-zero -> inlined simplified transform 552 int i; 553 const int dc0 = (dc[0] + 3) >> 3; 554 for (i = 0; i < 16 * 16; i += 16) dst[i] = dc0; 555 } 556 first = 1; 557 ac_proba = bands[0]; 558 } else { 559 first = 0; 560 ac_proba = bands[3]; 561 } 562 563 tnz = mb->nz & 0x0f; 564 lnz = left_mb->nz & 0x0f; 565 for (y = 0; y < 4; ++y) { 566 int l = lnz & 1; 567 uint32_t nz_coeffs = 0; 568 for (x = 0; x < 4; ++x) { 569 const int ctx = l + (tnz & 1); 570 const int nz = GetCoeffs(token_br, ac_proba, ctx, q->y1_mat, first, dst); 571 l = (nz > first); 572 tnz = (tnz >> 1) | (l << 7); 573 nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0); 574 dst += 16; 575 } 576 tnz >>= 4; 577 lnz = (lnz >> 1) | (l << 7); 578 non_zero_y = (non_zero_y << 8) | nz_coeffs; 579 } 580 out_t_nz = tnz; 581 out_l_nz = lnz >> 4; 582 583 for (ch = 0; ch < 4; ch += 2) { 584 uint32_t nz_coeffs = 0; 585 tnz = mb->nz >> (4 + ch); 586 lnz = left_mb->nz >> (4 + ch); 587 for (y = 0; y < 2; ++y) { 588 int l = lnz & 1; 589 for (x = 0; x < 2; ++x) { 590 const int ctx = l + (tnz & 1); 591 const int nz = GetCoeffs(token_br, bands[2], ctx, q->uv_mat, 0, dst); 592 l = (nz > 0); 593 tnz = (tnz >> 1) | (l << 3); 594 nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0); 595 dst += 16; 596 } 597 tnz >>= 2; 598 lnz = (lnz >> 1) | (l << 5); 599 } 600 // Note: we don't really need the per-4x4 details for U/V blocks. 601 non_zero_uv |= nz_coeffs << (4 * ch); 602 out_t_nz |= (tnz << 4) << ch; 603 out_l_nz |= (lnz & 0xf0) << ch; 604 } 605 mb->nz = out_t_nz; 606 left_mb->nz = out_l_nz; 607 608 block->non_zero_y = non_zero_y; 609 block->non_zero_uv = non_zero_uv; 610 611 // We look at the mode-code of each block and check if some blocks have less 612 // than three non-zero coeffs (code < 2). This is to avoid dithering flat and 613 // empty blocks. 614 block->dither = (non_zero_uv & 0xaaaa) ? 0 : q->dither; 615 616 return !(non_zero_y | non_zero_uv); // will be used for further optimization 617 } 618 619 //------------------------------------------------------------------------------ 620 // Main loop 621 622 int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) { 623 VP8MB* const left = dec->mb_info - 1; 624 VP8MB* const mb = dec->mb_info + dec->mb_x; 625 VP8MBData* const block = dec->mb_data + dec->mb_x; 626 int skip = dec->use_skip_proba ? block->skip : 0; 627 628 if (!skip) { 629 skip = ParseResiduals(dec, mb, token_br); 630 } else { 631 left->nz = mb->nz = 0; 632 if (!block->is_i4x4) { 633 left->nz_dc = mb->nz_dc = 0; 634 } 635 block->non_zero_y = 0; 636 block->non_zero_uv = 0; 637 block->dither = 0; 638 } 639 640 if (dec->filter_type > 0) { // store filter info 641 VP8FInfo* const finfo = dec->f_info + dec->mb_x; 642 *finfo = dec->fstrengths[block->segment][block->is_i4x4]; 643 finfo->f_inner |= !skip; 644 } 645 646 return !token_br->eof; 647 } 648 649 void VP8InitScanline(VP8Decoder* const dec) { 650 VP8MB* const left = dec->mb_info - 1; 651 left->nz = 0; 652 left->nz_dc = 0; 653 memset(dec->intra_l, B_DC_PRED, sizeof(dec->intra_l)); 654 dec->mb_x = 0; 655 } 656 657 static int ParseFrame(VP8Decoder* const dec, VP8Io* io) { 658 for (dec->mb_y = 0; dec->mb_y < dec->br_mb_y; ++dec->mb_y) { 659 // Parse bitstream for this row. 660 VP8BitReader* const token_br = 661 &dec->parts[dec->mb_y & dec->num_parts_minus_one]; 662 if (!VP8ParseIntraModeRow(&dec->br, dec)) { 663 return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, 664 "Premature end-of-partition0 encountered."); 665 } 666 for (; dec->mb_x < dec->mb_w; ++dec->mb_x) { 667 if (!VP8DecodeMB(dec, token_br)) { 668 return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, 669 "Premature end-of-file encountered."); 670 } 671 } 672 VP8InitScanline(dec); // Prepare for next scanline 673 674 // Reconstruct, filter and emit the row. 675 if (!VP8ProcessRow(dec, io)) { 676 return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted."); 677 } 678 } 679 if (dec->mt_method > 0) { 680 if (!WebPGetWorkerInterface()->Sync(&dec->worker)) return 0; 681 } 682 683 return 1; 684 } 685 686 // Main entry point 687 int VP8Decode(VP8Decoder* const dec, VP8Io* const io) { 688 int ok = 0; 689 if (dec == NULL) { 690 return 0; 691 } 692 if (io == NULL) { 693 return VP8SetError(dec, VP8_STATUS_INVALID_PARAM, 694 "NULL VP8Io parameter in VP8Decode()."); 695 } 696 697 if (!dec->ready) { 698 if (!VP8GetHeaders(dec, io)) { 699 return 0; 700 } 701 } 702 assert(dec->ready); 703 704 // Finish setting up the decoding parameter. Will call io->setup(). 705 ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK); 706 if (ok) { // good to go. 707 // Will allocate memory and prepare everything. 708 if (ok) ok = VP8InitFrame(dec, io); 709 710 // Main decoding loop 711 if (ok) ok = ParseFrame(dec, io); 712 713 // Exit. 714 ok &= VP8ExitCritical(dec, io); 715 } 716 717 if (!ok) { 718 VP8Clear(dec); 719 return 0; 720 } 721 722 dec->ready = 0; 723 return ok; 724 } 725 726 void VP8Clear(VP8Decoder* const dec) { 727 if (dec == NULL) { 728 return; 729 } 730 WebPGetWorkerInterface()->End(&dec->worker); 731 WebPDeallocateAlphaMemory(dec); 732 WebPSafeFree(dec->mem); 733 dec->mem = NULL; 734 dec->mem_size = 0; 735 memset(&dec->br, 0, sizeof(dec->br)); 736 dec->ready = 0; 737 } 738 739 //------------------------------------------------------------------------------