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 "./vp8i.h"
17#include "./vp8li.h"
18#include "./webpi.h"
19#include "../utils/bit_reader.h"
20
21#if defined(__cplusplus) || defined(c_plusplus)
22extern "C" {
23#endif
24
25//------------------------------------------------------------------------------
26
27int WebPGetDecoderVersion(void) {
28  return (DEC_MAJ_VERSION << 16) | (DEC_MIN_VERSION << 8) | DEC_REV_VERSION;
29}
30
31//------------------------------------------------------------------------------
32// VP8Decoder
33
34static void SetOk(VP8Decoder* const dec) {
35  dec->status_ = VP8_STATUS_OK;
36  dec->error_msg_ = "OK";
37}
38
39int VP8InitIoInternal(VP8Io* const io, int version) {
40  if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
41    return 0;  // mismatch error
42  }
43  if (io != NULL) {
44    memset(io, 0, sizeof(*io));
45  }
46  return 1;
47}
48
49VP8Decoder* VP8New(void) {
50  VP8Decoder* const dec = (VP8Decoder*)calloc(1, sizeof(*dec));
51  if (dec != NULL) {
52    SetOk(dec);
53    WebPWorkerInit(&dec->worker_);
54    dec->ready_ = 0;
55    dec->num_parts_ = 1;
56  }
57  return dec;
58}
59
60VP8StatusCode VP8Status(VP8Decoder* const dec) {
61  if (!dec) return VP8_STATUS_INVALID_PARAM;
62  return dec->status_;
63}
64
65const char* VP8StatusMessage(VP8Decoder* const dec) {
66  if (dec == NULL) return "no object";
67  if (!dec->error_msg_) return "OK";
68  return dec->error_msg_;
69}
70
71void VP8Delete(VP8Decoder* const dec) {
72  if (dec != NULL) {
73    VP8Clear(dec);
74    free(dec);
75  }
76}
77
78int VP8SetError(VP8Decoder* const dec,
79                VP8StatusCode error, const char* const msg) {
80  // TODO This check would be unnecessary if alpha decompression was separated
81  // from VP8ProcessRow/FinishRow. This avoids setting 'dec->status_' to
82  // something other than VP8_STATUS_BITSTREAM_ERROR on alpha decompression
83  // failure.
84  if (dec->status_ == VP8_STATUS_OK) {
85    dec->status_ = error;
86    dec->error_msg_ = msg;
87    dec->ready_ = 0;
88  }
89  return 0;
90}
91
92//------------------------------------------------------------------------------
93
94int VP8CheckSignature(const uint8_t* const data, size_t data_size) {
95  return (data_size >= 3 &&
96          data[0] == 0x9d && data[1] == 0x01 && data[2] == 0x2a);
97}
98
99int VP8GetInfo(const uint8_t* data, size_t data_size, size_t chunk_size,
100               int* const width, int* const height) {
101  if (data == NULL || data_size < VP8_FRAME_HEADER_SIZE) {
102    return 0;         // not enough data
103  }
104  // check signature
105  if (!VP8CheckSignature(data + 3, data_size - 3)) {
106    return 0;         // Wrong signature.
107  } else {
108    const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16);
109    const int key_frame = !(bits & 1);
110    const int w = ((data[7] << 8) | data[6]) & 0x3fff;
111    const int h = ((data[9] << 8) | data[8]) & 0x3fff;
112
113    if (!key_frame) {   // Not a keyframe.
114      return 0;
115    }
116
117    if (((bits >> 1) & 7) > 3) {
118      return 0;         // unknown profile
119    }
120    if (!((bits >> 4) & 1)) {
121      return 0;         // first frame is invisible!
122    }
123    if (((bits >> 5)) >= chunk_size) {  // partition_length
124      return 0;         // inconsistent size information.
125    }
126
127    if (width) {
128      *width = w;
129    }
130    if (height) {
131      *height = h;
132    }
133
134    return 1;
135  }
136}
137
138//------------------------------------------------------------------------------
139// Header parsing
140
141static void ResetSegmentHeader(VP8SegmentHeader* const hdr) {
142  assert(hdr != NULL);
143  hdr->use_segment_ = 0;
144  hdr->update_map_ = 0;
145  hdr->absolute_delta_ = 1;
146  memset(hdr->quantizer_, 0, sizeof(hdr->quantizer_));
147  memset(hdr->filter_strength_, 0, sizeof(hdr->filter_strength_));
148}
149
150// Paragraph 9.3
151static int ParseSegmentHeader(VP8BitReader* br,
152                              VP8SegmentHeader* hdr, VP8Proba* proba) {
153  assert(br != NULL);
154  assert(hdr != NULL);
155  hdr->use_segment_ = VP8Get(br);
156  if (hdr->use_segment_) {
157    hdr->update_map_ = VP8Get(br);
158    if (VP8Get(br)) {   // update data
159      int s;
160      hdr->absolute_delta_ = VP8Get(br);
161      for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
162        hdr->quantizer_[s] = VP8Get(br) ? VP8GetSignedValue(br, 7) : 0;
163      }
164      for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
165        hdr->filter_strength_[s] = VP8Get(br) ? VP8GetSignedValue(br, 6) : 0;
166      }
167    }
168    if (hdr->update_map_) {
169      int s;
170      for (s = 0; s < MB_FEATURE_TREE_PROBS; ++s) {
171        proba->segments_[s] = VP8Get(br) ? VP8GetValue(br, 8) : 255u;
172      }
173    }
174  } else {
175    hdr->update_map_ = 0;
176  }
177  return !br->eof_;
178}
179
180// Paragraph 9.5
181// This function returns VP8_STATUS_SUSPENDED if we don't have all the
182// necessary data in 'buf'.
183// This case is not necessarily an error (for incremental decoding).
184// Still, no bitreader is ever initialized to make it possible to read
185// unavailable memory.
186// If we don't even have the partitions' sizes, than VP8_STATUS_NOT_ENOUGH_DATA
187// is returned, and this is an unrecoverable error.
188// If the partitions were positioned ok, VP8_STATUS_OK is returned.
189static VP8StatusCode ParsePartitions(VP8Decoder* const dec,
190                                     const uint8_t* buf, size_t size) {
191  VP8BitReader* const br = &dec->br_;
192  const uint8_t* sz = buf;
193  const uint8_t* buf_end = buf + size;
194  const uint8_t* part_start;
195  int last_part;
196  int p;
197
198  dec->num_parts_ = 1 << VP8GetValue(br, 2);
199  last_part = dec->num_parts_ - 1;
200  part_start = buf + last_part * 3;
201  if (buf_end < part_start) {
202    // we can't even read the sizes with sz[]! That's a failure.
203    return VP8_STATUS_NOT_ENOUGH_DATA;
204  }
205  for (p = 0; p < last_part; ++p) {
206    const uint32_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16);
207    const uint8_t* part_end = part_start + psize;
208    if (part_end > buf_end) part_end = buf_end;
209    VP8InitBitReader(dec->parts_ + p, part_start, part_end);
210    part_start = part_end;
211    sz += 3;
212  }
213  VP8InitBitReader(dec->parts_ + last_part, part_start, buf_end);
214  return (part_start < buf_end) ? VP8_STATUS_OK :
215           VP8_STATUS_SUSPENDED;   // Init is ok, but there's not enough data
216}
217
218// Paragraph 9.4
219static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) {
220  VP8FilterHeader* const hdr = &dec->filter_hdr_;
221  hdr->simple_    = VP8Get(br);
222  hdr->level_     = VP8GetValue(br, 6);
223  hdr->sharpness_ = VP8GetValue(br, 3);
224  hdr->use_lf_delta_ = VP8Get(br);
225  if (hdr->use_lf_delta_) {
226    if (VP8Get(br)) {   // update lf-delta?
227      int i;
228      for (i = 0; i < NUM_REF_LF_DELTAS; ++i) {
229        if (VP8Get(br)) {
230          hdr->ref_lf_delta_[i] = VP8GetSignedValue(br, 6);
231        }
232      }
233      for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) {
234        if (VP8Get(br)) {
235          hdr->mode_lf_delta_[i] = VP8GetSignedValue(br, 6);
236        }
237      }
238    }
239  }
240  dec->filter_type_ = (hdr->level_ == 0) ? 0 : hdr->simple_ ? 1 : 2;
241  return !br->eof_;
242}
243
244// Topmost call
245int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
246  const uint8_t* buf;
247  size_t buf_size;
248  VP8FrameHeader* frm_hdr;
249  VP8PictureHeader* pic_hdr;
250  VP8BitReader* br;
251  VP8StatusCode status;
252  WebPHeaderStructure headers;
253
254  if (dec == NULL) {
255    return 0;
256  }
257  SetOk(dec);
258  if (io == NULL) {
259    return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
260                       "null VP8Io passed to VP8GetHeaders()");
261  }
262
263  // Process Pre-VP8 chunks.
264  headers.data = io->data;
265  headers.data_size = io->data_size;
266  status = WebPParseHeaders(&headers);
267  if (status != VP8_STATUS_OK) {
268    return VP8SetError(dec, status, "Incorrect/incomplete header.");
269  }
270  if (headers.is_lossless) {
271    return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
272                       "Unexpected lossless format encountered.");
273  }
274
275  if (dec->alpha_data_ == NULL) {
276    assert(dec->alpha_data_size_ == 0);
277    // We have NOT set alpha data yet. Set it now.
278    // (This is to ensure that dec->alpha_data_ is NOT reset to NULL if
279    // WebPParseHeaders() is called more than once, as in incremental decoding
280    // case.)
281    dec->alpha_data_ = headers.alpha_data;
282    dec->alpha_data_size_ = headers.alpha_data_size;
283  }
284
285  // Process the VP8 frame header.
286  buf = headers.data + headers.offset;
287  buf_size = headers.data_size - headers.offset;
288  assert(headers.data_size >= headers.offset);  // WebPParseHeaders' guarantee
289  if (buf_size < 4) {
290    return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
291                       "Truncated header.");
292  }
293
294  // Paragraph 9.1
295  {
296    const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16);
297    frm_hdr = &dec->frm_hdr_;
298    frm_hdr->key_frame_ = !(bits & 1);
299    frm_hdr->profile_ = (bits >> 1) & 7;
300    frm_hdr->show_ = (bits >> 4) & 1;
301    frm_hdr->partition_length_ = (bits >> 5);
302    if (frm_hdr->profile_ > 3)
303      return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
304                         "Incorrect keyframe parameters.");
305    if (!frm_hdr->show_)
306      return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
307                         "Frame not displayable.");
308    buf += 3;
309    buf_size -= 3;
310  }
311
312  pic_hdr = &dec->pic_hdr_;
313  if (frm_hdr->key_frame_) {
314    // Paragraph 9.2
315    if (buf_size < 7) {
316      return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
317                         "cannot parse picture header");
318    }
319    if (!VP8CheckSignature(buf, buf_size)) {
320      return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
321                         "Bad code word");
322    }
323    pic_hdr->width_ = ((buf[4] << 8) | buf[3]) & 0x3fff;
324    pic_hdr->xscale_ = buf[4] >> 6;   // ratio: 1, 5/4 5/3 or 2
325    pic_hdr->height_ = ((buf[6] << 8) | buf[5]) & 0x3fff;
326    pic_hdr->yscale_ = buf[6] >> 6;
327    buf += 7;
328    buf_size -= 7;
329
330    dec->mb_w_ = (pic_hdr->width_ + 15) >> 4;
331    dec->mb_h_ = (pic_hdr->height_ + 15) >> 4;
332    // Setup default output area (can be later modified during io->setup())
333    io->width = pic_hdr->width_;
334    io->height = pic_hdr->height_;
335    io->use_scaling  = 0;
336    io->use_cropping = 0;
337    io->crop_top  = 0;
338    io->crop_left = 0;
339    io->crop_right  = io->width;
340    io->crop_bottom = io->height;
341    io->mb_w = io->width;   // sanity check
342    io->mb_h = io->height;  // ditto
343
344    VP8ResetProba(&dec->proba_);
345    ResetSegmentHeader(&dec->segment_hdr_);
346    dec->segment_ = 0;    // default for intra
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, 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);
363    pic_hdr->clamp_type_ = VP8Get(br);
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    // Paragraph 9.7
385#ifndef ONLY_KEYFRAME_CODE
386    dec->buffer_flags_ = VP8Get(br) << 0;   // update golden
387    dec->buffer_flags_ |= VP8Get(br) << 1;  // update alt ref
388    if (!(dec->buffer_flags_ & 1)) {
389      dec->buffer_flags_ |= VP8GetValue(br, 2) << 2;
390    }
391    if (!(dec->buffer_flags_ & 2)) {
392      dec->buffer_flags_ |= VP8GetValue(br, 2) << 4;
393    }
394    dec->buffer_flags_ |= VP8Get(br) << 6;    // sign bias golden
395    dec->buffer_flags_ |= VP8Get(br) << 7;    // sign bias alt ref
396#else
397    return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
398                       "Not a key frame.");
399#endif
400  } else {
401    dec->buffer_flags_ = 0x003 | 0x100;
402  }
403
404  // Paragraph 9.8
405#ifndef ONLY_KEYFRAME_CODE
406  dec->update_proba_ = VP8Get(br);
407  if (!dec->update_proba_) {    // save for later restore
408    dec->proba_saved_ = dec->proba_;
409  }
410  dec->buffer_flags_ &= 1 << 8;
411  dec->buffer_flags_ |=
412      (frm_hdr->key_frame_ || VP8Get(br)) << 8;    // refresh last frame
413#else
414  VP8Get(br);   // just ignore the value of update_proba_
415#endif
416
417  VP8ParseProba(br, dec);
418
419#ifdef WEBP_EXPERIMENTAL_FEATURES
420  // Extensions
421  if (dec->pic_hdr_.colorspace_) {
422    const size_t kTrailerSize = 8;
423    const uint8_t kTrailerMarker = 0x01;
424    const uint8_t* ext_buf = buf - kTrailerSize;
425    size_t size;
426
427    if (frm_hdr->partition_length_ < kTrailerSize ||
428        ext_buf[kTrailerSize - 1] != kTrailerMarker) {
429      return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
430                         "RIFF: Inconsistent extra information.");
431    }
432
433    // Layer
434    size = (ext_buf[0] << 0) | (ext_buf[1] << 8) | (ext_buf[2] << 16);
435    dec->layer_data_size_ = size;
436    dec->layer_data_ = NULL;  // will be set later
437    dec->layer_colorspace_ = ext_buf[3];
438  }
439#endif
440
441  // sanitized state
442  dec->ready_ = 1;
443  return 1;
444}
445
446//------------------------------------------------------------------------------
447// Residual decoding (Paragraph 13.2 / 13.3)
448
449static const int kBands[16 + 1] = {
450  0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7,
451  0  // extra entry as sentinel
452};
453
454static const uint8_t kCat3[] = { 173, 148, 140, 0 };
455static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 };
456static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 };
457static const uint8_t kCat6[] =
458  { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0 };
459static const uint8_t* const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 };
460static const uint8_t kZigzag[16] = {
461  0, 1, 4, 8,  5, 2, 3, 6,  9, 12, 13, 10,  7, 11, 14, 15
462};
463
464typedef const uint8_t (*ProbaArray)[NUM_CTX][NUM_PROBAS];  // for const-casting
465typedef const uint8_t (*ProbaCtxArray)[NUM_PROBAS];
466
467// See section 13-2: http://tools.ietf.org/html/rfc6386#section-13.2
468static int GetLargeValue(VP8BitReader* const br, const uint8_t* const p) {
469  int v;
470  if (!VP8GetBit(br, p[3])) {
471    if (!VP8GetBit(br, p[4])) {
472      v = 2;
473    } else {
474      v = 3 + VP8GetBit(br, p[5]);
475    }
476  } else {
477    if (!VP8GetBit(br, p[6])) {
478      if (!VP8GetBit(br, p[7])) {
479        v = 5 + VP8GetBit(br, 159);
480      } else {
481        v = 7 + 2 * VP8GetBit(br, 165);
482        v += VP8GetBit(br, 145);
483      }
484    } else {
485      const uint8_t* tab;
486      const int bit1 = VP8GetBit(br, p[8]);
487      const int bit0 = VP8GetBit(br, p[9 + bit1]);
488      const int cat = 2 * bit1 + bit0;
489      v = 0;
490      for (tab = kCat3456[cat]; *tab; ++tab) {
491        v += v + VP8GetBit(br, *tab);
492      }
493      v += 3 + (8 << cat);
494    }
495  }
496  return v;
497}
498
499// Returns the position of the last non-zero coeff plus one
500// (and 0 if there's no coeff at all)
501static int GetCoeffs(VP8BitReader* const br, ProbaArray prob,
502                     int ctx, const quant_t dq, int n, int16_t* out) {
503  // n is either 0 or 1 here. kBands[n] is not necessary for extracting '*p'.
504  const uint8_t* p = prob[n][ctx];
505  if (!VP8GetBit(br, p[0])) {   // first EOB is more a 'CBP' bit.
506    return 0;
507  }
508  for (; n < 16; ++n) {
509    const ProbaCtxArray p_ctx = prob[kBands[n + 1]];
510    if (!VP8GetBit(br, p[1])) {
511      p = p_ctx[0];
512    } else {  // non zero coeff
513      int v;
514      if (!VP8GetBit(br, p[2])) {
515        v = 1;
516        p = p_ctx[1];
517      } else {
518        v = GetLargeValue(br, p);
519        p = p_ctx[2];
520      }
521      out[kZigzag[n]] = VP8GetSigned(br, v) * dq[n > 0];
522      if (n < 15 && !VP8GetBit(br, p[0])) {   // EOB
523        return n + 1;
524      }
525    }
526  }
527  return 16;
528}
529
530// Alias-safe way of converting 4bytes to 32bits.
531typedef union {
532  uint8_t  i8[4];
533  uint32_t i32;
534} PackedNz;
535
536// Table to unpack four bits into four bytes
537static const PackedNz kUnpackTab[16] = {
538  {{0, 0, 0, 0}},  {{1, 0, 0, 0}},  {{0, 1, 0, 0}},  {{1, 1, 0, 0}},
539  {{0, 0, 1, 0}},  {{1, 0, 1, 0}},  {{0, 1, 1, 0}},  {{1, 1, 1, 0}},
540  {{0, 0, 0, 1}},  {{1, 0, 0, 1}},  {{0, 1, 0, 1}},  {{1, 1, 0, 1}},
541  {{0, 0, 1, 1}},  {{1, 0, 1, 1}},  {{0, 1, 1, 1}},  {{1, 1, 1, 1}} };
542
543// Macro to pack four LSB of four bytes into four bits.
544#if defined(__PPC__) || defined(_M_PPC) || defined(_ARCH_PPC) || \
545    defined(__BIG_ENDIAN__)
546#define PACK_CST 0x08040201U
547#else
548#define PACK_CST 0x01020408U
549#endif
550#define PACK(X, S) ((((X).i32 * PACK_CST) & 0xff000000) >> (S))
551
552static void ParseResiduals(VP8Decoder* const dec,
553                           VP8MB* const mb, VP8BitReader* const token_br) {
554  int out_t_nz, out_l_nz, first;
555  ProbaArray ac_prob;
556  const VP8QuantMatrix* q = &dec->dqm_[dec->segment_];
557  int16_t* dst = dec->coeffs_;
558  VP8MB* const left_mb = dec->mb_info_ - 1;
559  PackedNz nz_ac, nz_dc;
560  PackedNz tnz, lnz;
561  uint32_t non_zero_ac = 0;
562  uint32_t non_zero_dc = 0;
563  int x, y, ch;
564
565  nz_dc.i32 = nz_ac.i32 = 0;
566  memset(dst, 0, 384 * sizeof(*dst));
567  if (!dec->is_i4x4_) {    // parse DC
568    int16_t dc[16] = { 0 };
569    const int ctx = mb->dc_nz_ + left_mb->dc_nz_;
570    mb->dc_nz_ = left_mb->dc_nz_ =
571        (GetCoeffs(token_br, (ProbaArray)dec->proba_.coeffs_[1],
572                   ctx, q->y2_mat_, 0, dc) > 0);
573    first = 1;
574    ac_prob = (ProbaArray)dec->proba_.coeffs_[0];
575    VP8TransformWHT(dc, dst);
576  } else {
577    first = 0;
578    ac_prob = (ProbaArray)dec->proba_.coeffs_[3];
579  }
580
581  tnz = kUnpackTab[mb->nz_ & 0xf];
582  lnz = kUnpackTab[left_mb->nz_ & 0xf];
583  for (y = 0; y < 4; ++y) {
584    int l = lnz.i8[y];
585    for (x = 0; x < 4; ++x) {
586      const int ctx = l + tnz.i8[x];
587      const int nz = GetCoeffs(token_br, ac_prob, ctx,
588                               q->y1_mat_, first, dst);
589      tnz.i8[x] = l = (nz > 0);
590      nz_dc.i8[x] = (dst[0] != 0);
591      nz_ac.i8[x] = (nz > 1);
592      dst += 16;
593    }
594    lnz.i8[y] = l;
595    non_zero_dc |= PACK(nz_dc, 24 - y * 4);
596    non_zero_ac |= PACK(nz_ac, 24 - y * 4);
597  }
598  out_t_nz = PACK(tnz, 24);
599  out_l_nz = PACK(lnz, 24);
600
601  tnz = kUnpackTab[mb->nz_ >> 4];
602  lnz = kUnpackTab[left_mb->nz_ >> 4];
603  for (ch = 0; ch < 4; ch += 2) {
604    for (y = 0; y < 2; ++y) {
605      int l = lnz.i8[ch + y];
606      for (x = 0; x < 2; ++x) {
607        const int ctx = l + tnz.i8[ch + x];
608        const int nz =
609            GetCoeffs(token_br, (ProbaArray)dec->proba_.coeffs_[2],
610                      ctx, q->uv_mat_, 0, dst);
611        tnz.i8[ch + x] = l = (nz > 0);
612        nz_dc.i8[y * 2 + x] = (dst[0] != 0);
613        nz_ac.i8[y * 2 + x] = (nz > 1);
614        dst += 16;
615      }
616      lnz.i8[ch + y] = l;
617    }
618    non_zero_dc |= PACK(nz_dc, 8 - ch * 2);
619    non_zero_ac |= PACK(nz_ac, 8 - ch * 2);
620  }
621  out_t_nz |= PACK(tnz, 20);
622  out_l_nz |= PACK(lnz, 20);
623  mb->nz_ = out_t_nz;
624  left_mb->nz_ = out_l_nz;
625
626  dec->non_zero_ac_ = non_zero_ac;
627  dec->non_zero_ = non_zero_ac | non_zero_dc;
628  mb->skip_ = !dec->non_zero_;
629}
630#undef PACK
631
632//------------------------------------------------------------------------------
633// Main loop
634
635int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) {
636  VP8BitReader* const br = &dec->br_;
637  VP8MB* const left = dec->mb_info_ - 1;
638  VP8MB* const info = dec->mb_info_ + dec->mb_x_;
639
640  // Note: we don't save segment map (yet), as we don't expect
641  // to decode more than 1 keyframe.
642  if (dec->segment_hdr_.update_map_) {
643    // Hardcoded tree parsing
644    dec->segment_ = !VP8GetBit(br, dec->proba_.segments_[0]) ?
645        VP8GetBit(br, dec->proba_.segments_[1]) :
646        2 + VP8GetBit(br, dec->proba_.segments_[2]);
647  }
648  info->skip_ = dec->use_skip_proba_ ? VP8GetBit(br, dec->skip_p_) : 0;
649
650  VP8ParseIntraMode(br, dec);
651  if (br->eof_) {
652    return 0;
653  }
654
655  if (!info->skip_) {
656    ParseResiduals(dec, info, token_br);
657  } else {
658    left->nz_ = info->nz_ = 0;
659    if (!dec->is_i4x4_) {
660      left->dc_nz_ = info->dc_nz_ = 0;
661    }
662    dec->non_zero_ = 0;
663    dec->non_zero_ac_ = 0;
664  }
665
666  if (dec->filter_type_ > 0) {  // store filter info
667    VP8FInfo* const finfo = dec->f_info_ + dec->mb_x_;
668    *finfo = dec->fstrengths_[dec->segment_][dec->is_i4x4_];
669    finfo->f_inner_ = (!info->skip_ || dec->is_i4x4_);
670  }
671
672  return (!token_br->eof_);
673}
674
675void VP8InitScanline(VP8Decoder* const dec) {
676  VP8MB* const left = dec->mb_info_ - 1;
677  left->nz_ = 0;
678  left->dc_nz_ = 0;
679  memset(dec->intra_l_, B_DC_PRED, sizeof(dec->intra_l_));
680  dec->filter_row_ =
681    (dec->filter_type_ > 0) &&
682    (dec->mb_y_ >= dec->tl_mb_y_) && (dec->mb_y_ <= dec->br_mb_y_);
683}
684
685static int ParseFrame(VP8Decoder* const dec, VP8Io* io) {
686  for (dec->mb_y_ = 0; dec->mb_y_ < dec->br_mb_y_; ++dec->mb_y_) {
687    VP8BitReader* const token_br =
688        &dec->parts_[dec->mb_y_ & (dec->num_parts_ - 1)];
689    VP8InitScanline(dec);
690    for (dec->mb_x_ = 0; dec->mb_x_ < dec->mb_w_;  dec->mb_x_++) {
691      if (!VP8DecodeMB(dec, token_br)) {
692        return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
693                           "Premature end-of-file encountered.");
694      }
695      // Reconstruct and emit samples.
696      VP8ReconstructBlock(dec);
697    }
698    if (!VP8ProcessRow(dec, io)) {
699      return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted.");
700    }
701  }
702  if (dec->use_threads_ && !WebPWorkerSync(&dec->worker_)) {
703    return 0;
704  }
705
706  // Finish
707#ifndef ONLY_KEYFRAME_CODE
708  if (!dec->update_proba_) {
709    dec->proba_ = dec->proba_saved_;
710  }
711#endif
712
713#ifdef WEBP_EXPERIMENTAL_FEATURES
714  if (dec->layer_data_size_ > 0) {
715    if (!VP8DecodeLayer(dec)) {
716      return 0;
717    }
718  }
719#endif
720
721  return 1;
722}
723
724// Main entry point
725int VP8Decode(VP8Decoder* const dec, VP8Io* const io) {
726  int ok = 0;
727  if (dec == NULL) {
728    return 0;
729  }
730  if (io == NULL) {
731    return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
732                       "NULL VP8Io parameter in VP8Decode().");
733  }
734
735  if (!dec->ready_) {
736    if (!VP8GetHeaders(dec, io)) {
737      return 0;
738    }
739  }
740  assert(dec->ready_);
741
742  // Finish setting up the decoding parameter. Will call io->setup().
743  ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK);
744  if (ok) {   // good to go.
745    // Will allocate memory and prepare everything.
746    if (ok) ok = VP8InitFrame(dec, io);
747
748    // Main decoding loop
749    if (ok) ok = ParseFrame(dec, io);
750
751    // Exit.
752    ok &= VP8ExitCritical(dec, io);
753  }
754
755  if (!ok) {
756    VP8Clear(dec);
757    return 0;
758  }
759
760  dec->ready_ = 0;
761  return ok;
762}
763
764void VP8Clear(VP8Decoder* const dec) {
765  if (dec == NULL) {
766    return;
767  }
768  if (dec->use_threads_) {
769    WebPWorkerEnd(&dec->worker_);
770  }
771  if (dec->mem_) {
772    free(dec->mem_);
773  }
774  dec->mem_ = NULL;
775  dec->mem_size_ = 0;
776  memset(&dec->br_, 0, sizeof(dec->br_));
777  dec->ready_ = 0;
778}
779
780//------------------------------------------------------------------------------
781
782#if defined(__cplusplus) || defined(c_plusplus)
783}    // extern "C"
784#endif
785