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 
WebPGetDecoderVersion(void)25 int 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 
32 typedef int (*GetCoeffsFunc)(VP8BitReader* const br,
33                              const VP8BandProbas* const prob[],
34                              int ctx, const quant_t dq, int n, int16_t* out);
35 static volatile GetCoeffsFunc GetCoeffs = NULL;
36 
37 static void InitGetCoeffs(void);
38 
39 //------------------------------------------------------------------------------
40 // VP8Decoder
41 
SetOk(VP8Decoder * const dec)42 static void SetOk(VP8Decoder* const dec) {
43   dec->status_ = VP8_STATUS_OK;
44   dec->error_msg_ = "OK";
45 }
46 
VP8InitIoInternal(VP8Io * const io,int version)47 int 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 
VP8New(void)57 VP8Decoder* 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 
VP8Status(VP8Decoder * const dec)69 VP8StatusCode VP8Status(VP8Decoder* const dec) {
70   if (!dec) return VP8_STATUS_INVALID_PARAM;
71   return dec->status_;
72 }
73 
VP8StatusMessage(VP8Decoder * const dec)74 const 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 
VP8Delete(VP8Decoder * const dec)80 void VP8Delete(VP8Decoder* const dec) {
81   if (dec != NULL) {
82     VP8Clear(dec);
83     WebPSafeFree(dec);
84   }
85 }
86 
VP8SetError(VP8Decoder * const dec,VP8StatusCode error,const char * const msg)87 int 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 
VP8CheckSignature(const uint8_t * const data,size_t data_size)100 int 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 
VP8GetInfo(const uint8_t * data,size_t data_size,size_t chunk_size,int * const width,int * const height)105 int 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 
ResetSegmentHeader(VP8SegmentHeader * const hdr)150 static 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
ParseSegmentHeader(VP8BitReader * br,VP8SegmentHeader * hdr,VP8Proba * proba)160 static 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.
ParsePartitions(VP8Decoder * const dec,const uint8_t * buf,size_t size)201 static 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
ParseFilterHeader(VP8BitReader * br,VP8Decoder * const dec)233 static 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
VP8GetHeaders(VP8Decoder * const dec,VP8Io * const io)259 int 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;   // sanity check
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 
396 static const uint8_t kCat3[] = { 173, 148, 140, 0 };
397 static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 };
398 static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 };
399 static const uint8_t kCat6[] =
400   { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0 };
401 static const uint8_t* const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 };
402 static 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: http://tools.ietf.org/html/rfc6386#section-13.2
GetLargeValue(VP8BitReader * const br,const uint8_t * const p)407 static 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
GetCoeffsFast(VP8BitReader * const br,const VP8BandProbas * const prob[],int ctx,const quant_t dq,int n,int16_t * out)439 static 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.
GetCoeffsAlt(VP8BitReader * const br,const VP8BandProbas * const prob[],int ctx,const quant_t dq,int n,int16_t * out)469 static 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 
WEBP_DSP_INIT_FUNC(InitGetCoeffs)497 WEBP_DSP_INIT_FUNC(InitGetCoeffs) {
498   if (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kSlowSSSE3)) {
499     GetCoeffs = GetCoeffsAlt;
500   } else {
501     GetCoeffs = GetCoeffsFast;
502   }
503 }
504 
NzCodeBits(uint32_t nz_coeffs,int nz,int dc_nz)505 static WEBP_INLINE uint32_t NzCodeBits(uint32_t nz_coeffs, int nz, int dc_nz) {
506   nz_coeffs <<= 2;
507   nz_coeffs |= (nz > 3) ? 3 : (nz > 1) ? 2 : dc_nz;
508   return nz_coeffs;
509 }
510 
ParseResiduals(VP8Decoder * const dec,VP8MB * const mb,VP8BitReader * const token_br)511 static int ParseResiduals(VP8Decoder* const dec,
512                           VP8MB* const mb, VP8BitReader* const token_br) {
513   const VP8BandProbas* (* const bands)[16 + 1] = dec->proba_.bands_ptr_;
514   const VP8BandProbas* const * ac_proba;
515   VP8MBData* const block = dec->mb_data_ + dec->mb_x_;
516   const VP8QuantMatrix* const q = &dec->dqm_[block->segment_];
517   int16_t* dst = block->coeffs_;
518   VP8MB* const left_mb = dec->mb_info_ - 1;
519   uint8_t tnz, lnz;
520   uint32_t non_zero_y = 0;
521   uint32_t non_zero_uv = 0;
522   int x, y, ch;
523   uint32_t out_t_nz, out_l_nz;
524   int first;
525 
526   memset(dst, 0, 384 * sizeof(*dst));
527   if (!block->is_i4x4_) {    // parse DC
528     int16_t dc[16] = { 0 };
529     const int ctx = mb->nz_dc_ + left_mb->nz_dc_;
530     const int nz = GetCoeffs(token_br, bands[1], ctx, q->y2_mat_, 0, dc);
531     mb->nz_dc_ = left_mb->nz_dc_ = (nz > 0);
532     if (nz > 1) {   // more than just the DC -> perform the full transform
533       VP8TransformWHT(dc, dst);
534     } else {        // only DC is non-zero -> inlined simplified transform
535       int i;
536       const int dc0 = (dc[0] + 3) >> 3;
537       for (i = 0; i < 16 * 16; i += 16) dst[i] = dc0;
538     }
539     first = 1;
540     ac_proba = bands[0];
541   } else {
542     first = 0;
543     ac_proba = bands[3];
544   }
545 
546   tnz = mb->nz_ & 0x0f;
547   lnz = left_mb->nz_ & 0x0f;
548   for (y = 0; y < 4; ++y) {
549     int l = lnz & 1;
550     uint32_t nz_coeffs = 0;
551     for (x = 0; x < 4; ++x) {
552       const int ctx = l + (tnz & 1);
553       const int nz = GetCoeffs(token_br, ac_proba, ctx, q->y1_mat_, first, dst);
554       l = (nz > first);
555       tnz = (tnz >> 1) | (l << 7);
556       nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
557       dst += 16;
558     }
559     tnz >>= 4;
560     lnz = (lnz >> 1) | (l << 7);
561     non_zero_y = (non_zero_y << 8) | nz_coeffs;
562   }
563   out_t_nz = tnz;
564   out_l_nz = lnz >> 4;
565 
566   for (ch = 0; ch < 4; ch += 2) {
567     uint32_t nz_coeffs = 0;
568     tnz = mb->nz_ >> (4 + ch);
569     lnz = left_mb->nz_ >> (4 + ch);
570     for (y = 0; y < 2; ++y) {
571       int l = lnz & 1;
572       for (x = 0; x < 2; ++x) {
573         const int ctx = l + (tnz & 1);
574         const int nz = GetCoeffs(token_br, bands[2], ctx, q->uv_mat_, 0, dst);
575         l = (nz > 0);
576         tnz = (tnz >> 1) | (l << 3);
577         nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
578         dst += 16;
579       }
580       tnz >>= 2;
581       lnz = (lnz >> 1) | (l << 5);
582     }
583     // Note: we don't really need the per-4x4 details for U/V blocks.
584     non_zero_uv |= nz_coeffs << (4 * ch);
585     out_t_nz |= (tnz << 4) << ch;
586     out_l_nz |= (lnz & 0xf0) << ch;
587   }
588   mb->nz_ = out_t_nz;
589   left_mb->nz_ = out_l_nz;
590 
591   block->non_zero_y_ = non_zero_y;
592   block->non_zero_uv_ = non_zero_uv;
593 
594   // We look at the mode-code of each block and check if some blocks have less
595   // than three non-zero coeffs (code < 2). This is to avoid dithering flat and
596   // empty blocks.
597   block->dither_ = (non_zero_uv & 0xaaaa) ? 0 : q->dither_;
598 
599   return !(non_zero_y | non_zero_uv);  // will be used for further optimization
600 }
601 
602 //------------------------------------------------------------------------------
603 // Main loop
604 
VP8DecodeMB(VP8Decoder * const dec,VP8BitReader * const token_br)605 int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) {
606   VP8MB* const left = dec->mb_info_ - 1;
607   VP8MB* const mb = dec->mb_info_ + dec->mb_x_;
608   VP8MBData* const block = dec->mb_data_ + dec->mb_x_;
609   int skip = dec->use_skip_proba_ ? block->skip_ : 0;
610 
611   if (!skip) {
612     skip = ParseResiduals(dec, mb, token_br);
613   } else {
614     left->nz_ = mb->nz_ = 0;
615     if (!block->is_i4x4_) {
616       left->nz_dc_ = mb->nz_dc_ = 0;
617     }
618     block->non_zero_y_ = 0;
619     block->non_zero_uv_ = 0;
620     block->dither_ = 0;
621   }
622 
623   if (dec->filter_type_ > 0) {  // store filter info
624     VP8FInfo* const finfo = dec->f_info_ + dec->mb_x_;
625     *finfo = dec->fstrengths_[block->segment_][block->is_i4x4_];
626     finfo->f_inner_ |= !skip;
627   }
628 
629   return !token_br->eof_;
630 }
631 
VP8InitScanline(VP8Decoder * const dec)632 void VP8InitScanline(VP8Decoder* const dec) {
633   VP8MB* const left = dec->mb_info_ - 1;
634   left->nz_ = 0;
635   left->nz_dc_ = 0;
636   memset(dec->intra_l_, B_DC_PRED, sizeof(dec->intra_l_));
637   dec->mb_x_ = 0;
638 }
639 
ParseFrame(VP8Decoder * const dec,VP8Io * io)640 static int ParseFrame(VP8Decoder* const dec, VP8Io* io) {
641   for (dec->mb_y_ = 0; dec->mb_y_ < dec->br_mb_y_; ++dec->mb_y_) {
642     // Parse bitstream for this row.
643     VP8BitReader* const token_br =
644         &dec->parts_[dec->mb_y_ & dec->num_parts_minus_one_];
645     if (!VP8ParseIntraModeRow(&dec->br_, dec)) {
646       return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
647                          "Premature end-of-partition0 encountered.");
648     }
649     for (; dec->mb_x_ < dec->mb_w_; ++dec->mb_x_) {
650       if (!VP8DecodeMB(dec, token_br)) {
651         return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
652                            "Premature end-of-file encountered.");
653       }
654     }
655     VP8InitScanline(dec);   // Prepare for next scanline
656 
657     // Reconstruct, filter and emit the row.
658     if (!VP8ProcessRow(dec, io)) {
659       return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted.");
660     }
661   }
662   if (dec->mt_method_ > 0) {
663     if (!WebPGetWorkerInterface()->Sync(&dec->worker_)) return 0;
664   }
665 
666   return 1;
667 }
668 
669 // Main entry point
VP8Decode(VP8Decoder * const dec,VP8Io * const io)670 int VP8Decode(VP8Decoder* const dec, VP8Io* const io) {
671   int ok = 0;
672   if (dec == NULL) {
673     return 0;
674   }
675   if (io == NULL) {
676     return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
677                        "NULL VP8Io parameter in VP8Decode().");
678   }
679 
680   if (!dec->ready_) {
681     if (!VP8GetHeaders(dec, io)) {
682       return 0;
683     }
684   }
685   assert(dec->ready_);
686 
687   // Finish setting up the decoding parameter. Will call io->setup().
688   ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK);
689   if (ok) {   // good to go.
690     // Will allocate memory and prepare everything.
691     if (ok) ok = VP8InitFrame(dec, io);
692 
693     // Main decoding loop
694     if (ok) ok = ParseFrame(dec, io);
695 
696     // Exit.
697     ok &= VP8ExitCritical(dec, io);
698   }
699 
700   if (!ok) {
701     VP8Clear(dec);
702     return 0;
703   }
704 
705   dec->ready_ = 0;
706   return ok;
707 }
708 
VP8Clear(VP8Decoder * const dec)709 void VP8Clear(VP8Decoder* const dec) {
710   if (dec == NULL) {
711     return;
712   }
713   WebPGetWorkerInterface()->End(&dec->worker_);
714   WebPDeallocateAlphaMemory(dec);
715   WebPSafeFree(dec->mem_);
716   dec->mem_ = NULL;
717   dec->mem_size_ = 0;
718   memset(&dec->br_, 0, sizeof(dec->br_));
719   dec->ready_ = 0;
720 }
721 
722 //------------------------------------------------------------------------------
723