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