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