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