github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/webp/libwebp/src/dec/vp8l.c (about)

     1  // Copyright 2012 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  // Authors: Vikas Arora (vikaas.arora@gmail.com)
    13  //          Jyrki Alakuijala (jyrki@google.com)
    14  
    15  #include <stdio.h>
    16  #include <stdlib.h>
    17  #include "./alphai.h"
    18  #include "./vp8li.h"
    19  #include "../dsp/lossless.h"
    20  #include "../dsp/yuv.h"
    21  #include "../utils/alpha_processing.h"
    22  #include "../utils/huffman.h"
    23  #include "../utils/utils.h"
    24  
    25  #define NUM_ARGB_CACHE_ROWS          16
    26  
    27  static const int kCodeLengthLiterals = 16;
    28  static const int kCodeLengthRepeatCode = 16;
    29  static const int kCodeLengthExtraBits[3] = { 2, 3, 7 };
    30  static const int kCodeLengthRepeatOffsets[3] = { 3, 3, 11 };
    31  
    32  // -----------------------------------------------------------------------------
    33  //  Five Huffman codes are used at each meta code:
    34  //  1. green + length prefix codes + color cache codes,
    35  //  2. alpha,
    36  //  3. red,
    37  //  4. blue, and,
    38  //  5. distance prefix codes.
    39  typedef enum {
    40    GREEN = 0,
    41    RED   = 1,
    42    BLUE  = 2,
    43    ALPHA = 3,
    44    DIST  = 4
    45  } HuffIndex;
    46  
    47  static const uint16_t kAlphabetSize[HUFFMAN_CODES_PER_META_CODE] = {
    48    NUM_LITERAL_CODES + NUM_LENGTH_CODES,
    49    NUM_LITERAL_CODES, NUM_LITERAL_CODES, NUM_LITERAL_CODES,
    50    NUM_DISTANCE_CODES
    51  };
    52  
    53  
    54  #define NUM_CODE_LENGTH_CODES       19
    55  static const uint8_t kCodeLengthCodeOrder[NUM_CODE_LENGTH_CODES] = {
    56    17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
    57  };
    58  
    59  #define CODE_TO_PLANE_CODES        120
    60  static const uint8_t kCodeToPlane[CODE_TO_PLANE_CODES] = {
    61    0x18, 0x07, 0x17, 0x19, 0x28, 0x06, 0x27, 0x29, 0x16, 0x1a,
    62    0x26, 0x2a, 0x38, 0x05, 0x37, 0x39, 0x15, 0x1b, 0x36, 0x3a,
    63    0x25, 0x2b, 0x48, 0x04, 0x47, 0x49, 0x14, 0x1c, 0x35, 0x3b,
    64    0x46, 0x4a, 0x24, 0x2c, 0x58, 0x45, 0x4b, 0x34, 0x3c, 0x03,
    65    0x57, 0x59, 0x13, 0x1d, 0x56, 0x5a, 0x23, 0x2d, 0x44, 0x4c,
    66    0x55, 0x5b, 0x33, 0x3d, 0x68, 0x02, 0x67, 0x69, 0x12, 0x1e,
    67    0x66, 0x6a, 0x22, 0x2e, 0x54, 0x5c, 0x43, 0x4d, 0x65, 0x6b,
    68    0x32, 0x3e, 0x78, 0x01, 0x77, 0x79, 0x53, 0x5d, 0x11, 0x1f,
    69    0x64, 0x6c, 0x42, 0x4e, 0x76, 0x7a, 0x21, 0x2f, 0x75, 0x7b,
    70    0x31, 0x3f, 0x63, 0x6d, 0x52, 0x5e, 0x00, 0x74, 0x7c, 0x41,
    71    0x4f, 0x10, 0x20, 0x62, 0x6e, 0x30, 0x73, 0x7d, 0x51, 0x5f,
    72    0x40, 0x72, 0x7e, 0x61, 0x6f, 0x50, 0x71, 0x7f, 0x60, 0x70
    73  };
    74  
    75  static int DecodeImageStream(int xsize, int ysize,
    76                               int is_level0,
    77                               VP8LDecoder* const dec,
    78                               uint32_t** const decoded_data);
    79  
    80  //------------------------------------------------------------------------------
    81  
    82  int VP8LCheckSignature(const uint8_t* const data, size_t size) {
    83    return (size >= VP8L_FRAME_HEADER_SIZE &&
    84            data[0] == VP8L_MAGIC_BYTE &&
    85            (data[4] >> 5) == 0);  // version
    86  }
    87  
    88  static int ReadImageInfo(VP8LBitReader* const br,
    89                           int* const width, int* const height,
    90                           int* const has_alpha) {
    91    if (VP8LReadBits(br, 8) != VP8L_MAGIC_BYTE) return 0;
    92    *width = VP8LReadBits(br, VP8L_IMAGE_SIZE_BITS) + 1;
    93    *height = VP8LReadBits(br, VP8L_IMAGE_SIZE_BITS) + 1;
    94    *has_alpha = VP8LReadBits(br, 1);
    95    if (VP8LReadBits(br, VP8L_VERSION_BITS) != 0) return 0;
    96    return 1;
    97  }
    98  
    99  int VP8LGetInfo(const uint8_t* data, size_t data_size,
   100                  int* const width, int* const height, int* const has_alpha) {
   101    if (data == NULL || data_size < VP8L_FRAME_HEADER_SIZE) {
   102      return 0;         // not enough data
   103    } else if (!VP8LCheckSignature(data, data_size)) {
   104      return 0;         // bad signature
   105    } else {
   106      int w, h, a;
   107      VP8LBitReader br;
   108      VP8LInitBitReader(&br, data, data_size);
   109      if (!ReadImageInfo(&br, &w, &h, &a)) {
   110        return 0;
   111      }
   112      if (width != NULL) *width = w;
   113      if (height != NULL) *height = h;
   114      if (has_alpha != NULL) *has_alpha = a;
   115      return 1;
   116    }
   117  }
   118  
   119  //------------------------------------------------------------------------------
   120  
   121  static WEBP_INLINE int GetCopyDistance(int distance_symbol,
   122                                         VP8LBitReader* const br) {
   123    int extra_bits, offset;
   124    if (distance_symbol < 4) {
   125      return distance_symbol + 1;
   126    }
   127    extra_bits = (distance_symbol - 2) >> 1;
   128    offset = (2 + (distance_symbol & 1)) << extra_bits;
   129    return offset + VP8LReadBits(br, extra_bits) + 1;
   130  }
   131  
   132  static WEBP_INLINE int GetCopyLength(int length_symbol,
   133                                       VP8LBitReader* const br) {
   134    // Length and distance prefixes are encoded the same way.
   135    return GetCopyDistance(length_symbol, br);
   136  }
   137  
   138  static WEBP_INLINE int PlaneCodeToDistance(int xsize, int plane_code) {
   139    if (plane_code > CODE_TO_PLANE_CODES) {
   140      return plane_code - CODE_TO_PLANE_CODES;
   141    } else {
   142      const int dist_code = kCodeToPlane[plane_code - 1];
   143      const int yoffset = dist_code >> 4;
   144      const int xoffset = 8 - (dist_code & 0xf);
   145      const int dist = yoffset * xsize + xoffset;
   146      return (dist >= 1) ? dist : 1;  // dist<1 can happen if xsize is very small
   147    }
   148  }
   149  
   150  //------------------------------------------------------------------------------
   151  // Decodes the next Huffman code from bit-stream.
   152  // FillBitWindow(br) needs to be called at minimum every second call
   153  // to ReadSymbol, in order to pre-fetch enough bits.
   154  static WEBP_INLINE int ReadSymbol(const HuffmanTree* tree,
   155                                    VP8LBitReader* const br) {
   156    const HuffmanTreeNode* node = tree->root_;
   157    uint32_t bits = VP8LPrefetchBits(br);
   158    int bitpos = br->bit_pos_;
   159    // Check if we find the bit combination from the Huffman lookup table.
   160    const int lut_ix = bits & (HUFF_LUT - 1);
   161    const int lut_bits = tree->lut_bits_[lut_ix];
   162    if (lut_bits <= HUFF_LUT_BITS) {
   163      VP8LSetBitPos(br, bitpos + lut_bits);
   164      return tree->lut_symbol_[lut_ix];
   165    }
   166    node += tree->lut_jump_[lut_ix];
   167    bitpos += HUFF_LUT_BITS;
   168    bits >>= HUFF_LUT_BITS;
   169  
   170    // Decode the value from a binary tree.
   171    assert(node != NULL);
   172    do {
   173      node = HuffmanTreeNextNode(node, bits & 1);
   174      bits >>= 1;
   175      ++bitpos;
   176    } while (HuffmanTreeNodeIsNotLeaf(node));
   177    VP8LSetBitPos(br, bitpos);
   178    return node->symbol_;
   179  }
   180  
   181  static int ReadHuffmanCodeLengths(
   182      VP8LDecoder* const dec, const int* const code_length_code_lengths,
   183      int num_symbols, int* const code_lengths) {
   184    int ok = 0;
   185    VP8LBitReader* const br = &dec->br_;
   186    int symbol;
   187    int max_symbol;
   188    int prev_code_len = DEFAULT_CODE_LENGTH;
   189    HuffmanTree tree;
   190  
   191    if (!HuffmanTreeBuildImplicit(&tree, code_length_code_lengths,
   192                                  NUM_CODE_LENGTH_CODES)) {
   193      dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
   194      return 0;
   195    }
   196  
   197    if (VP8LReadBits(br, 1)) {    // use length
   198      const int length_nbits = 2 + 2 * VP8LReadBits(br, 3);
   199      max_symbol = 2 + VP8LReadBits(br, length_nbits);
   200      if (max_symbol > num_symbols) {
   201        dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
   202        goto End;
   203      }
   204    } else {
   205      max_symbol = num_symbols;
   206    }
   207  
   208    symbol = 0;
   209    while (symbol < num_symbols) {
   210      int code_len;
   211      if (max_symbol-- == 0) break;
   212      VP8LFillBitWindow(br);
   213      code_len = ReadSymbol(&tree, br);
   214      if (code_len < kCodeLengthLiterals) {
   215        code_lengths[symbol++] = code_len;
   216        if (code_len != 0) prev_code_len = code_len;
   217      } else {
   218        const int use_prev = (code_len == kCodeLengthRepeatCode);
   219        const int slot = code_len - kCodeLengthLiterals;
   220        const int extra_bits = kCodeLengthExtraBits[slot];
   221        const int repeat_offset = kCodeLengthRepeatOffsets[slot];
   222        int repeat = VP8LReadBits(br, extra_bits) + repeat_offset;
   223        if (symbol + repeat > num_symbols) {
   224          dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
   225          goto End;
   226        } else {
   227          const int length = use_prev ? prev_code_len : 0;
   228          while (repeat-- > 0) code_lengths[symbol++] = length;
   229        }
   230      }
   231    }
   232    ok = 1;
   233  
   234   End:
   235    HuffmanTreeRelease(&tree);
   236    return ok;
   237  }
   238  
   239  static int ReadHuffmanCode(int alphabet_size, VP8LDecoder* const dec,
   240                             HuffmanTree* const tree) {
   241    int ok = 0;
   242    VP8LBitReader* const br = &dec->br_;
   243    const int simple_code = VP8LReadBits(br, 1);
   244  
   245    if (simple_code) {  // Read symbols, codes & code lengths directly.
   246      int symbols[2];
   247      int codes[2];
   248      int code_lengths[2];
   249      const int num_symbols = VP8LReadBits(br, 1) + 1;
   250      const int first_symbol_len_code = VP8LReadBits(br, 1);
   251      // The first code is either 1 bit or 8 bit code.
   252      symbols[0] = VP8LReadBits(br, (first_symbol_len_code == 0) ? 1 : 8);
   253      codes[0] = 0;
   254      code_lengths[0] = num_symbols - 1;
   255      // The second code (if present), is always 8 bit long.
   256      if (num_symbols == 2) {
   257        symbols[1] = VP8LReadBits(br, 8);
   258        codes[1] = 1;
   259        code_lengths[1] = num_symbols - 1;
   260      }
   261      ok = HuffmanTreeBuildExplicit(tree, code_lengths, codes, symbols,
   262                                    alphabet_size, num_symbols);
   263    } else {  // Decode Huffman-coded code lengths.
   264      int* code_lengths = NULL;
   265      int i;
   266      int code_length_code_lengths[NUM_CODE_LENGTH_CODES] = { 0 };
   267      const int num_codes = VP8LReadBits(br, 4) + 4;
   268      if (num_codes > NUM_CODE_LENGTH_CODES) {
   269        dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
   270        return 0;
   271      }
   272  
   273      code_lengths =
   274          (int*)WebPSafeCalloc((uint64_t)alphabet_size, sizeof(*code_lengths));
   275      if (code_lengths == NULL) {
   276        dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
   277        return 0;
   278      }
   279  
   280      for (i = 0; i < num_codes; ++i) {
   281        code_length_code_lengths[kCodeLengthCodeOrder[i]] = VP8LReadBits(br, 3);
   282      }
   283      ok = ReadHuffmanCodeLengths(dec, code_length_code_lengths, alphabet_size,
   284                                  code_lengths);
   285      if (ok) {
   286        ok = HuffmanTreeBuildImplicit(tree, code_lengths, alphabet_size);
   287      }
   288      free(code_lengths);
   289    }
   290    ok = ok && !br->error_;
   291    if (!ok) {
   292      dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
   293      return 0;
   294    }
   295    return 1;
   296  }
   297  
   298  static void DeleteHtreeGroups(HTreeGroup* htree_groups, int num_htree_groups) {
   299    if (htree_groups != NULL) {
   300      int i, j;
   301      for (i = 0; i < num_htree_groups; ++i) {
   302        HuffmanTree* const htrees = htree_groups[i].htrees_;
   303        for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
   304          HuffmanTreeRelease(&htrees[j]);
   305        }
   306      }
   307      free(htree_groups);
   308    }
   309  }
   310  
   311  static int ReadHuffmanCodes(VP8LDecoder* const dec, int xsize, int ysize,
   312                              int color_cache_bits, int allow_recursion) {
   313    int i, j;
   314    VP8LBitReader* const br = &dec->br_;
   315    VP8LMetadata* const hdr = &dec->hdr_;
   316    uint32_t* huffman_image = NULL;
   317    HTreeGroup* htree_groups = NULL;
   318    int num_htree_groups = 1;
   319  
   320    if (allow_recursion && VP8LReadBits(br, 1)) {
   321      // use meta Huffman codes.
   322      const int huffman_precision = VP8LReadBits(br, 3) + 2;
   323      const int huffman_xsize = VP8LSubSampleSize(xsize, huffman_precision);
   324      const int huffman_ysize = VP8LSubSampleSize(ysize, huffman_precision);
   325      const int huffman_pixs = huffman_xsize * huffman_ysize;
   326      if (!DecodeImageStream(huffman_xsize, huffman_ysize, 0, dec,
   327                             &huffman_image)) {
   328        dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
   329        goto Error;
   330      }
   331      hdr->huffman_subsample_bits_ = huffman_precision;
   332      for (i = 0; i < huffman_pixs; ++i) {
   333        // The huffman data is stored in red and green bytes.
   334        const int group = (huffman_image[i] >> 8) & 0xffff;
   335        huffman_image[i] = group;
   336        if (group >= num_htree_groups) {
   337          num_htree_groups = group + 1;
   338        }
   339      }
   340    }
   341  
   342    if (br->error_) goto Error;
   343  
   344    assert(num_htree_groups <= 0x10000);
   345    htree_groups =
   346        (HTreeGroup*)WebPSafeCalloc((uint64_t)num_htree_groups,
   347                                    sizeof(*htree_groups));
   348    if (htree_groups == NULL) {
   349      dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
   350      goto Error;
   351    }
   352  
   353    for (i = 0; i < num_htree_groups; ++i) {
   354      HuffmanTree* const htrees = htree_groups[i].htrees_;
   355      for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
   356        int alphabet_size = kAlphabetSize[j];
   357        if (j == 0 && color_cache_bits > 0) {
   358          alphabet_size += 1 << color_cache_bits;
   359        }
   360        if (!ReadHuffmanCode(alphabet_size, dec, htrees + j)) goto Error;
   361      }
   362    }
   363  
   364    // All OK. Finalize pointers and return.
   365    hdr->huffman_image_ = huffman_image;
   366    hdr->num_htree_groups_ = num_htree_groups;
   367    hdr->htree_groups_ = htree_groups;
   368    return 1;
   369  
   370   Error:
   371    free(huffman_image);
   372    DeleteHtreeGroups(htree_groups, num_htree_groups);
   373    return 0;
   374  }
   375  
   376  //------------------------------------------------------------------------------
   377  // Scaling.
   378  
   379  static int AllocateAndInitRescaler(VP8LDecoder* const dec, VP8Io* const io) {
   380    const int num_channels = 4;
   381    const int in_width = io->mb_w;
   382    const int out_width = io->scaled_width;
   383    const int in_height = io->mb_h;
   384    const int out_height = io->scaled_height;
   385    const uint64_t work_size = 2 * num_channels * (uint64_t)out_width;
   386    int32_t* work;        // Rescaler work area.
   387    const uint64_t scaled_data_size = num_channels * (uint64_t)out_width;
   388    uint32_t* scaled_data;  // Temporary storage for scaled BGRA data.
   389    const uint64_t memory_size = sizeof(*dec->rescaler) +
   390                                 work_size * sizeof(*work) +
   391                                 scaled_data_size * sizeof(*scaled_data);
   392    uint8_t* memory = (uint8_t*)WebPSafeCalloc(memory_size, sizeof(*memory));
   393    if (memory == NULL) {
   394      dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
   395      return 0;
   396    }
   397    assert(dec->rescaler_memory == NULL);
   398    dec->rescaler_memory = memory;
   399  
   400    dec->rescaler = (WebPRescaler*)memory;
   401    memory += sizeof(*dec->rescaler);
   402    work = (int32_t*)memory;
   403    memory += work_size * sizeof(*work);
   404    scaled_data = (uint32_t*)memory;
   405  
   406    WebPRescalerInit(dec->rescaler, in_width, in_height, (uint8_t*)scaled_data,
   407                     out_width, out_height, 0, num_channels,
   408                     in_width, out_width, in_height, out_height, work);
   409    return 1;
   410  }
   411  
   412  //------------------------------------------------------------------------------
   413  // Export to ARGB
   414  
   415  // We have special "export" function since we need to convert from BGRA
   416  static int Export(WebPRescaler* const rescaler, WEBP_CSP_MODE colorspace,
   417                    int rgba_stride, uint8_t* const rgba) {
   418    uint32_t* const src = (uint32_t*)rescaler->dst;
   419    const int dst_width = rescaler->dst_width;
   420    int num_lines_out = 0;
   421    while (WebPRescalerHasPendingOutput(rescaler)) {
   422      uint8_t* const dst = rgba + num_lines_out * rgba_stride;
   423      WebPRescalerExportRow(rescaler);
   424      WebPMultARGBRow(src, dst_width, 1);
   425      VP8LConvertFromBGRA(src, dst_width, colorspace, dst);
   426      ++num_lines_out;
   427    }
   428    return num_lines_out;
   429  }
   430  
   431  // Emit scaled rows.
   432  static int EmitRescaledRowsRGBA(const VP8LDecoder* const dec,
   433                                  uint8_t* in, int in_stride, int mb_h,
   434                                  uint8_t* const out, int out_stride) {
   435    const WEBP_CSP_MODE colorspace = dec->output_->colorspace;
   436    int num_lines_in = 0;
   437    int num_lines_out = 0;
   438    while (num_lines_in < mb_h) {
   439      uint8_t* const row_in = in + num_lines_in * in_stride;
   440      uint8_t* const row_out = out + num_lines_out * out_stride;
   441      const int lines_left = mb_h - num_lines_in;
   442      const int needed_lines = WebPRescaleNeededLines(dec->rescaler, lines_left);
   443      assert(needed_lines > 0 && needed_lines <= lines_left);
   444      WebPMultARGBRows(row_in, in_stride,
   445                       dec->rescaler->src_width, needed_lines, 0);
   446      WebPRescalerImport(dec->rescaler, lines_left, row_in, in_stride);
   447      num_lines_in += needed_lines;
   448      num_lines_out += Export(dec->rescaler, colorspace, out_stride, row_out);
   449    }
   450    return num_lines_out;
   451  }
   452  
   453  // Emit rows without any scaling.
   454  static int EmitRows(WEBP_CSP_MODE colorspace,
   455                      const uint8_t* row_in, int in_stride,
   456                      int mb_w, int mb_h,
   457                      uint8_t* const out, int out_stride) {
   458    int lines = mb_h;
   459    uint8_t* row_out = out;
   460    while (lines-- > 0) {
   461      VP8LConvertFromBGRA((const uint32_t*)row_in, mb_w, colorspace, row_out);
   462      row_in += in_stride;
   463      row_out += out_stride;
   464    }
   465    return mb_h;  // Num rows out == num rows in.
   466  }
   467  
   468  //------------------------------------------------------------------------------
   469  // Export to YUVA
   470  
   471  static void ConvertToYUVA(const uint32_t* const src, int width, int y_pos,
   472                            const WebPDecBuffer* const output) {
   473    const WebPYUVABuffer* const buf = &output->u.YUVA;
   474    // first, the luma plane
   475    {
   476      int i;
   477      uint8_t* const y = buf->y + y_pos * buf->y_stride;
   478      for (i = 0; i < width; ++i) {
   479        const uint32_t p = src[i];
   480        y[i] = VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >> 0) & 0xff,
   481                         YUV_HALF);
   482      }
   483    }
   484  
   485    // then U/V planes
   486    {
   487      uint8_t* const u = buf->u + (y_pos >> 1) * buf->u_stride;
   488      uint8_t* const v = buf->v + (y_pos >> 1) * buf->v_stride;
   489      const int uv_width = width >> 1;
   490      int i;
   491      for (i = 0; i < uv_width; ++i) {
   492        const uint32_t v0 = src[2 * i + 0];
   493        const uint32_t v1 = src[2 * i + 1];
   494        // VP8RGBToU/V expects four accumulated pixels. Hence we need to
   495        // scale r/g/b value by a factor 2. We just shift v0/v1 one bit less.
   496        const int r = ((v0 >> 15) & 0x1fe) + ((v1 >> 15) & 0x1fe);
   497        const int g = ((v0 >>  7) & 0x1fe) + ((v1 >>  7) & 0x1fe);
   498        const int b = ((v0 <<  1) & 0x1fe) + ((v1 <<  1) & 0x1fe);
   499        if (!(y_pos & 1)) {  // even lines: store values
   500          u[i] = VP8RGBToU(r, g, b, YUV_HALF << 2);
   501          v[i] = VP8RGBToV(r, g, b, YUV_HALF << 2);
   502        } else {             // odd lines: average with previous values
   503          const int tmp_u = VP8RGBToU(r, g, b, YUV_HALF << 2);
   504          const int tmp_v = VP8RGBToV(r, g, b, YUV_HALF << 2);
   505          // Approximated average-of-four. But it's an acceptable diff.
   506          u[i] = (u[i] + tmp_u + 1) >> 1;
   507          v[i] = (v[i] + tmp_v + 1) >> 1;
   508        }
   509      }
   510      if (width & 1) {       // last pixel
   511        const uint32_t v0 = src[2 * i + 0];
   512        const int r = (v0 >> 14) & 0x3fc;
   513        const int g = (v0 >>  6) & 0x3fc;
   514        const int b = (v0 <<  2) & 0x3fc;
   515        if (!(y_pos & 1)) {  // even lines
   516          u[i] = VP8RGBToU(r, g, b, YUV_HALF << 2);
   517          v[i] = VP8RGBToV(r, g, b, YUV_HALF << 2);
   518        } else {             // odd lines (note: we could just skip this)
   519          const int tmp_u = VP8RGBToU(r, g, b, YUV_HALF << 2);
   520          const int tmp_v = VP8RGBToV(r, g, b, YUV_HALF << 2);
   521          u[i] = (u[i] + tmp_u + 1) >> 1;
   522          v[i] = (v[i] + tmp_v + 1) >> 1;
   523        }
   524      }
   525    }
   526    // Lastly, store alpha if needed.
   527    if (buf->a != NULL) {
   528      int i;
   529      uint8_t* const a = buf->a + y_pos * buf->a_stride;
   530      for (i = 0; i < width; ++i) a[i] = (src[i] >> 24);
   531    }
   532  }
   533  
   534  static int ExportYUVA(const VP8LDecoder* const dec, int y_pos) {
   535    WebPRescaler* const rescaler = dec->rescaler;
   536    uint32_t* const src = (uint32_t*)rescaler->dst;
   537    const int dst_width = rescaler->dst_width;
   538    int num_lines_out = 0;
   539    while (WebPRescalerHasPendingOutput(rescaler)) {
   540      WebPRescalerExportRow(rescaler);
   541      WebPMultARGBRow(src, dst_width, 1);
   542      ConvertToYUVA(src, dst_width, y_pos, dec->output_);
   543      ++y_pos;
   544      ++num_lines_out;
   545    }
   546    return num_lines_out;
   547  }
   548  
   549  static int EmitRescaledRowsYUVA(const VP8LDecoder* const dec,
   550                                  uint8_t* in, int in_stride, int mb_h) {
   551    int num_lines_in = 0;
   552    int y_pos = dec->last_out_row_;
   553    while (num_lines_in < mb_h) {
   554      const int lines_left = mb_h - num_lines_in;
   555      const int needed_lines = WebPRescaleNeededLines(dec->rescaler, lines_left);
   556      WebPMultARGBRows(in, in_stride, dec->rescaler->src_width, needed_lines, 0);
   557      WebPRescalerImport(dec->rescaler, lines_left, in, in_stride);
   558      num_lines_in += needed_lines;
   559      in += needed_lines * in_stride;
   560      y_pos += ExportYUVA(dec, y_pos);
   561    }
   562    return y_pos;
   563  }
   564  
   565  static int EmitRowsYUVA(const VP8LDecoder* const dec,
   566                          const uint8_t* in, int in_stride,
   567                          int mb_w, int num_rows) {
   568    int y_pos = dec->last_out_row_;
   569    while (num_rows-- > 0) {
   570      ConvertToYUVA((const uint32_t*)in, mb_w, y_pos, dec->output_);
   571      in += in_stride;
   572      ++y_pos;
   573    }
   574    return y_pos;
   575  }
   576  
   577  //------------------------------------------------------------------------------
   578  // Cropping.
   579  
   580  // Sets io->mb_y, io->mb_h & io->mb_w according to start row, end row and
   581  // crop options. Also updates the input data pointer, so that it points to the
   582  // start of the cropped window. Note that pixels are in ARGB format even if
   583  // 'in_data' is uint8_t*.
   584  // Returns true if the crop window is not empty.
   585  static int SetCropWindow(VP8Io* const io, int y_start, int y_end,
   586                           uint8_t** const in_data, int pixel_stride) {
   587    assert(y_start < y_end);
   588    assert(io->crop_left < io->crop_right);
   589    if (y_end > io->crop_bottom) {
   590      y_end = io->crop_bottom;  // make sure we don't overflow on last row.
   591    }
   592    if (y_start < io->crop_top) {
   593      const int delta = io->crop_top - y_start;
   594      y_start = io->crop_top;
   595      *in_data += delta * pixel_stride;
   596    }
   597    if (y_start >= y_end) return 0;  // Crop window is empty.
   598  
   599    *in_data += io->crop_left * sizeof(uint32_t);
   600  
   601    io->mb_y = y_start - io->crop_top;
   602    io->mb_w = io->crop_right - io->crop_left;
   603    io->mb_h = y_end - y_start;
   604    return 1;  // Non-empty crop window.
   605  }
   606  
   607  //------------------------------------------------------------------------------
   608  
   609  static WEBP_INLINE int GetMetaIndex(
   610      const uint32_t* const image, int xsize, int bits, int x, int y) {
   611    if (bits == 0) return 0;
   612    return image[xsize * (y >> bits) + (x >> bits)];
   613  }
   614  
   615  static WEBP_INLINE HTreeGroup* GetHtreeGroupForPos(VP8LMetadata* const hdr,
   616                                                     int x, int y) {
   617    const int meta_index = GetMetaIndex(hdr->huffman_image_, hdr->huffman_xsize_,
   618                                        hdr->huffman_subsample_bits_, x, y);
   619    assert(meta_index < hdr->num_htree_groups_);
   620    return hdr->htree_groups_ + meta_index;
   621  }
   622  
   623  //------------------------------------------------------------------------------
   624  // Main loop, with custom row-processing function
   625  
   626  typedef void (*ProcessRowsFunc)(VP8LDecoder* const dec, int row);
   627  
   628  static void ApplyInverseTransforms(VP8LDecoder* const dec, int num_rows,
   629                                     const uint32_t* const rows) {
   630    int n = dec->next_transform_;
   631    const int cache_pixs = dec->width_ * num_rows;
   632    const int start_row = dec->last_row_;
   633    const int end_row = start_row + num_rows;
   634    const uint32_t* rows_in = rows;
   635    uint32_t* const rows_out = dec->argb_cache_;
   636  
   637    // Inverse transforms.
   638    // TODO: most transforms only need to operate on the cropped region only.
   639    memcpy(rows_out, rows_in, cache_pixs * sizeof(*rows_out));
   640    while (n-- > 0) {
   641      VP8LTransform* const transform = &dec->transforms_[n];
   642      VP8LInverseTransform(transform, start_row, end_row, rows_in, rows_out);
   643      rows_in = rows_out;
   644    }
   645  }
   646  
   647  // Special method for paletted alpha data.
   648  static void ApplyInverseTransformsAlpha(VP8LDecoder* const dec, int num_rows,
   649                                          const uint8_t* const rows) {
   650    const int start_row = dec->last_row_;
   651    const int end_row = start_row + num_rows;
   652    const uint8_t* rows_in = rows;
   653    uint8_t* rows_out = (uint8_t*)dec->io_->opaque + dec->io_->width * start_row;
   654    VP8LTransform* const transform = &dec->transforms_[0];
   655    assert(dec->next_transform_ == 1);
   656    assert(transform->type_ == COLOR_INDEXING_TRANSFORM);
   657    VP8LColorIndexInverseTransformAlpha(transform, start_row, end_row, rows_in,
   658                                        rows_out);
   659  }
   660  
   661  // Processes (transforms, scales & color-converts) the rows decoded after the
   662  // last call.
   663  static void ProcessRows(VP8LDecoder* const dec, int row) {
   664    const uint32_t* const rows = dec->pixels_ + dec->width_ * dec->last_row_;
   665    const int num_rows = row - dec->last_row_;
   666  
   667    if (num_rows <= 0) return;  // Nothing to be done.
   668    ApplyInverseTransforms(dec, num_rows, rows);
   669  
   670    // Emit output.
   671    {
   672      VP8Io* const io = dec->io_;
   673      uint8_t* rows_data = (uint8_t*)dec->argb_cache_;
   674      const int in_stride = io->width * sizeof(uint32_t);  // in unit of RGBA
   675      if (!SetCropWindow(io, dec->last_row_, row, &rows_data, in_stride)) {
   676        // Nothing to output (this time).
   677      } else {
   678        const WebPDecBuffer* const output = dec->output_;
   679        if (output->colorspace < MODE_YUV) {  // convert to RGBA
   680          const WebPRGBABuffer* const buf = &output->u.RGBA;
   681          uint8_t* const rgba = buf->rgba + dec->last_out_row_ * buf->stride;
   682          const int num_rows_out = io->use_scaling ?
   683              EmitRescaledRowsRGBA(dec, rows_data, in_stride, io->mb_h,
   684                                   rgba, buf->stride) :
   685              EmitRows(output->colorspace, rows_data, in_stride,
   686                       io->mb_w, io->mb_h, rgba, buf->stride);
   687          // Update 'last_out_row_'.
   688          dec->last_out_row_ += num_rows_out;
   689        } else {                              // convert to YUVA
   690          dec->last_out_row_ = io->use_scaling ?
   691              EmitRescaledRowsYUVA(dec, rows_data, in_stride, io->mb_h) :
   692              EmitRowsYUVA(dec, rows_data, in_stride, io->mb_w, io->mb_h);
   693        }
   694        assert(dec->last_out_row_ <= output->height);
   695      }
   696    }
   697  
   698    // Update 'last_row_'.
   699    dec->last_row_ = row;
   700    assert(dec->last_row_ <= dec->height_);
   701  }
   702  
   703  // Row-processing for the special case when alpha data contains only one
   704  // transform (color indexing), and trivial non-green literals.
   705  static int Is8bOptimizable(const VP8LMetadata* const hdr) {
   706    int i;
   707    if (hdr->color_cache_size_ > 0) return 0;
   708    // When the Huffman tree contains only one symbol, we can skip the
   709    // call to ReadSymbol() for red/blue/alpha channels.
   710    for (i = 0; i < hdr->num_htree_groups_; ++i) {
   711      const HuffmanTree* const htrees = hdr->htree_groups_[i].htrees_;
   712      if (htrees[RED].num_nodes_ > 1) return 0;
   713      if (htrees[BLUE].num_nodes_ > 1) return 0;
   714      if (htrees[ALPHA].num_nodes_ > 1) return 0;
   715    }
   716    return 1;
   717  }
   718  
   719  static void ExtractPalettedAlphaRows(VP8LDecoder* const dec, int row) {
   720    const int num_rows = row - dec->last_row_;
   721    const uint8_t* const in =
   722        (uint8_t*)dec->pixels_ + dec->width_ * dec->last_row_;
   723    if (num_rows > 0) {
   724      ApplyInverseTransformsAlpha(dec, num_rows, in);
   725    }
   726    dec->last_row_ = dec->last_out_row_ = row;
   727  }
   728  
   729  static int DecodeAlphaData(VP8LDecoder* const dec, uint8_t* const data,
   730                             int width, int height, int last_row) {
   731    int ok = 1;
   732    int row = dec->last_pixel_ / width;
   733    int col = dec->last_pixel_ % width;
   734    VP8LBitReader* const br = &dec->br_;
   735    VP8LMetadata* const hdr = &dec->hdr_;
   736    const HTreeGroup* htree_group = GetHtreeGroupForPos(hdr, col, row);
   737    int pos = dec->last_pixel_;         // current position
   738    const int end = width * height;     // End of data
   739    const int last = width * last_row;  // Last pixel to decode
   740    const int len_code_limit = NUM_LITERAL_CODES + NUM_LENGTH_CODES;
   741    const int mask = hdr->huffman_mask_;
   742    assert(htree_group != NULL);
   743    assert(last_row <= height);
   744    assert(Is8bOptimizable(hdr));
   745  
   746    while (!br->eos_ && pos < last) {
   747      int code;
   748      // Only update when changing tile.
   749      if ((col & mask) == 0) {
   750        htree_group = GetHtreeGroupForPos(hdr, col, row);
   751      }
   752      VP8LFillBitWindow(br);
   753      code = ReadSymbol(&htree_group->htrees_[GREEN], br);
   754      if (code < NUM_LITERAL_CODES) {  // Literal
   755        data[pos] = code;
   756        ++pos;
   757        ++col;
   758        if (col >= width) {
   759          col = 0;
   760          ++row;
   761          if (row % NUM_ARGB_CACHE_ROWS == 0) {
   762            ExtractPalettedAlphaRows(dec, row);
   763          }
   764        }
   765      } else if (code < len_code_limit) {  // Backward reference
   766        int dist_code, dist;
   767        const int length_sym = code - NUM_LITERAL_CODES;
   768        const int length = GetCopyLength(length_sym, br);
   769        const int dist_symbol = ReadSymbol(&htree_group->htrees_[DIST], br);
   770        VP8LFillBitWindow(br);
   771        dist_code = GetCopyDistance(dist_symbol, br);
   772        dist = PlaneCodeToDistance(width, dist_code);
   773        if (pos >= dist && end - pos >= length) {
   774          int i;
   775          for (i = 0; i < length; ++i) data[pos + i] = data[pos + i - dist];
   776        } else {
   777          ok = 0;
   778          goto End;
   779        }
   780        pos += length;
   781        col += length;
   782        while (col >= width) {
   783          col -= width;
   784          ++row;
   785          if (row % NUM_ARGB_CACHE_ROWS == 0) {
   786            ExtractPalettedAlphaRows(dec, row);
   787          }
   788        }
   789        if (pos < last && (col & mask)) {
   790          htree_group = GetHtreeGroupForPos(hdr, col, row);
   791        }
   792      } else {  // Not reached
   793        ok = 0;
   794        goto End;
   795      }
   796      ok = !br->error_;
   797      if (!ok) goto End;
   798    }
   799    // Process the remaining rows corresponding to last row-block.
   800    ExtractPalettedAlphaRows(dec, row);
   801  
   802   End:
   803    if (br->error_ || !ok || (br->eos_ && pos < end)) {
   804      ok = 0;
   805      dec->status_ = br->eos_ ? VP8_STATUS_SUSPENDED
   806                              : VP8_STATUS_BITSTREAM_ERROR;
   807    } else {
   808      dec->last_pixel_ = (int)pos;
   809      if (pos == end) dec->state_ = READ_DATA;
   810    }
   811    return ok;
   812  }
   813  
   814  static int DecodeImageData(VP8LDecoder* const dec, uint32_t* const data,
   815                             int width, int height, int last_row,
   816                             ProcessRowsFunc process_func) {
   817    int ok = 1;
   818    int row = dec->last_pixel_ / width;
   819    int col = dec->last_pixel_ % width;
   820    VP8LBitReader* const br = &dec->br_;
   821    VP8LMetadata* const hdr = &dec->hdr_;
   822    HTreeGroup* htree_group = GetHtreeGroupForPos(hdr, col, row);
   823    uint32_t* src = data + dec->last_pixel_;
   824    uint32_t* last_cached = src;
   825    uint32_t* const src_end = data + width * height;     // End of data
   826    uint32_t* const src_last = data + width * last_row;  // Last pixel to decode
   827    const int len_code_limit = NUM_LITERAL_CODES + NUM_LENGTH_CODES;
   828    const int color_cache_limit = len_code_limit + hdr->color_cache_size_;
   829    VP8LColorCache* const color_cache =
   830        (hdr->color_cache_size_ > 0) ? &hdr->color_cache_ : NULL;
   831    const int mask = hdr->huffman_mask_;
   832    assert(htree_group != NULL);
   833    assert(src_last <= src_end);
   834  
   835    while (!br->eos_ && src < src_last) {
   836      int code;
   837      // Only update when changing tile. Note we could use this test:
   838      // if "((((prev_col ^ col) | prev_row ^ row)) > mask)" -> tile changed
   839      // but that's actually slower and needs storing the previous col/row.
   840      if ((col & mask) == 0) {
   841        htree_group = GetHtreeGroupForPos(hdr, col, row);
   842      }
   843      VP8LFillBitWindow(br);
   844      code = ReadSymbol(&htree_group->htrees_[GREEN], br);
   845      if (code < NUM_LITERAL_CODES) {  // Literal
   846        int red, green, blue, alpha;
   847        red = ReadSymbol(&htree_group->htrees_[RED], br);
   848        green = code;
   849        VP8LFillBitWindow(br);
   850        blue = ReadSymbol(&htree_group->htrees_[BLUE], br);
   851        alpha = ReadSymbol(&htree_group->htrees_[ALPHA], br);
   852        *src = (alpha << 24) | (red << 16) | (green << 8) | blue;
   853      AdvanceByOne:
   854        ++src;
   855        ++col;
   856        if (col >= width) {
   857          col = 0;
   858          ++row;
   859          if ((row % NUM_ARGB_CACHE_ROWS == 0) && (process_func != NULL)) {
   860            process_func(dec, row);
   861          }
   862          if (color_cache != NULL) {
   863            while (last_cached < src) {
   864              VP8LColorCacheInsert(color_cache, *last_cached++);
   865            }
   866          }
   867        }
   868      } else if (code < len_code_limit) {  // Backward reference
   869        int dist_code, dist;
   870        const int length_sym = code - NUM_LITERAL_CODES;
   871        const int length = GetCopyLength(length_sym, br);
   872        const int dist_symbol = ReadSymbol(&htree_group->htrees_[DIST], br);
   873        VP8LFillBitWindow(br);
   874        dist_code = GetCopyDistance(dist_symbol, br);
   875        dist = PlaneCodeToDistance(width, dist_code);
   876        if (src - data < (ptrdiff_t)dist || src_end - src < (ptrdiff_t)length) {
   877          ok = 0;
   878          goto End;
   879        } else {
   880          int i;
   881          for (i = 0; i < length; ++i) src[i] = src[i - dist];
   882          src += length;
   883        }
   884        col += length;
   885        while (col >= width) {
   886          col -= width;
   887          ++row;
   888          if ((row % NUM_ARGB_CACHE_ROWS == 0) && (process_func != NULL)) {
   889            process_func(dec, row);
   890          }
   891        }
   892        if (src < src_last) {
   893          if (col & mask) htree_group = GetHtreeGroupForPos(hdr, col, row);
   894          if (color_cache != NULL) {
   895            while (last_cached < src) {
   896              VP8LColorCacheInsert(color_cache, *last_cached++);
   897            }
   898          }
   899        }
   900      } else if (code < color_cache_limit) {  // Color cache
   901        const int key = code - len_code_limit;
   902        assert(color_cache != NULL);
   903        while (last_cached < src) {
   904          VP8LColorCacheInsert(color_cache, *last_cached++);
   905        }
   906        *src = VP8LColorCacheLookup(color_cache, key);
   907        goto AdvanceByOne;
   908      } else {  // Not reached
   909        ok = 0;
   910        goto End;
   911      }
   912      ok = !br->error_;
   913      if (!ok) goto End;
   914    }
   915    // Process the remaining rows corresponding to last row-block.
   916    if (process_func != NULL) process_func(dec, row);
   917  
   918   End:
   919    if (br->error_ || !ok || (br->eos_ && src < src_end)) {
   920      ok = 0;
   921      dec->status_ = br->eos_ ? VP8_STATUS_SUSPENDED
   922                              : VP8_STATUS_BITSTREAM_ERROR;
   923    } else {
   924      dec->last_pixel_ = (int)(src - data);
   925      if (src == src_end) dec->state_ = READ_DATA;
   926    }
   927    return ok;
   928  }
   929  
   930  // -----------------------------------------------------------------------------
   931  // VP8LTransform
   932  
   933  static void ClearTransform(VP8LTransform* const transform) {
   934    free(transform->data_);
   935    transform->data_ = NULL;
   936  }
   937  
   938  // For security reason, we need to remap the color map to span
   939  // the total possible bundled values, and not just the num_colors.
   940  static int ExpandColorMap(int num_colors, VP8LTransform* const transform) {
   941    int i;
   942    const int final_num_colors = 1 << (8 >> transform->bits_);
   943    uint32_t* const new_color_map =
   944        (uint32_t*)WebPSafeMalloc((uint64_t)final_num_colors,
   945                                  sizeof(*new_color_map));
   946    if (new_color_map == NULL) {
   947      return 0;
   948    } else {
   949      uint8_t* const data = (uint8_t*)transform->data_;
   950      uint8_t* const new_data = (uint8_t*)new_color_map;
   951      new_color_map[0] = transform->data_[0];
   952      for (i = 4; i < 4 * num_colors; ++i) {
   953        // Equivalent to AddPixelEq(), on a byte-basis.
   954        new_data[i] = (data[i] + new_data[i - 4]) & 0xff;
   955      }
   956      for (; i < 4 * final_num_colors; ++i)
   957        new_data[i] = 0;  // black tail.
   958      free(transform->data_);
   959      transform->data_ = new_color_map;
   960    }
   961    return 1;
   962  }
   963  
   964  static int ReadTransform(int* const xsize, int const* ysize,
   965                           VP8LDecoder* const dec) {
   966    int ok = 1;
   967    VP8LBitReader* const br = &dec->br_;
   968    VP8LTransform* transform = &dec->transforms_[dec->next_transform_];
   969    const VP8LImageTransformType type =
   970        (VP8LImageTransformType)VP8LReadBits(br, 2);
   971  
   972    // Each transform type can only be present once in the stream.
   973    if (dec->transforms_seen_ & (1U << type)) {
   974      return 0;  // Already there, let's not accept the second same transform.
   975    }
   976    dec->transforms_seen_ |= (1U << type);
   977  
   978    transform->type_ = type;
   979    transform->xsize_ = *xsize;
   980    transform->ysize_ = *ysize;
   981    transform->data_ = NULL;
   982    ++dec->next_transform_;
   983    assert(dec->next_transform_ <= NUM_TRANSFORMS);
   984  
   985    switch (type) {
   986      case PREDICTOR_TRANSFORM:
   987      case CROSS_COLOR_TRANSFORM:
   988        transform->bits_ = VP8LReadBits(br, 3) + 2;
   989        ok = DecodeImageStream(VP8LSubSampleSize(transform->xsize_,
   990                                                 transform->bits_),
   991                               VP8LSubSampleSize(transform->ysize_,
   992                                                 transform->bits_),
   993                               0, dec, &transform->data_);
   994        break;
   995      case COLOR_INDEXING_TRANSFORM: {
   996         const int num_colors = VP8LReadBits(br, 8) + 1;
   997         const int bits = (num_colors > 16) ? 0
   998                        : (num_colors > 4) ? 1
   999                        : (num_colors > 2) ? 2
  1000                        : 3;
  1001         *xsize = VP8LSubSampleSize(transform->xsize_, bits);
  1002         transform->bits_ = bits;
  1003         ok = DecodeImageStream(num_colors, 1, 0, dec, &transform->data_);
  1004         ok = ok && ExpandColorMap(num_colors, transform);
  1005        break;
  1006      }
  1007      case SUBTRACT_GREEN:
  1008        break;
  1009      default:
  1010        assert(0);    // can't happen
  1011        break;
  1012    }
  1013  
  1014    return ok;
  1015  }
  1016  
  1017  // -----------------------------------------------------------------------------
  1018  // VP8LMetadata
  1019  
  1020  static void InitMetadata(VP8LMetadata* const hdr) {
  1021    assert(hdr);
  1022    memset(hdr, 0, sizeof(*hdr));
  1023  }
  1024  
  1025  static void ClearMetadata(VP8LMetadata* const hdr) {
  1026    assert(hdr);
  1027  
  1028    free(hdr->huffman_image_);
  1029    DeleteHtreeGroups(hdr->htree_groups_, hdr->num_htree_groups_);
  1030    VP8LColorCacheClear(&hdr->color_cache_);
  1031    InitMetadata(hdr);
  1032  }
  1033  
  1034  // -----------------------------------------------------------------------------
  1035  // VP8LDecoder
  1036  
  1037  VP8LDecoder* VP8LNew(void) {
  1038    VP8LDecoder* const dec = (VP8LDecoder*)calloc(1, sizeof(*dec));
  1039    if (dec == NULL) return NULL;
  1040    dec->status_ = VP8_STATUS_OK;
  1041    dec->action_ = READ_DIM;
  1042    dec->state_ = READ_DIM;
  1043  
  1044    VP8LDspInit();  // Init critical function pointers.
  1045  
  1046    return dec;
  1047  }
  1048  
  1049  void VP8LClear(VP8LDecoder* const dec) {
  1050    int i;
  1051    if (dec == NULL) return;
  1052    ClearMetadata(&dec->hdr_);
  1053  
  1054    free(dec->pixels_);
  1055    dec->pixels_ = NULL;
  1056    for (i = 0; i < dec->next_transform_; ++i) {
  1057      ClearTransform(&dec->transforms_[i]);
  1058    }
  1059    dec->next_transform_ = 0;
  1060    dec->transforms_seen_ = 0;
  1061  
  1062    free(dec->rescaler_memory);
  1063    dec->rescaler_memory = NULL;
  1064  
  1065    dec->output_ = NULL;   // leave no trace behind
  1066  }
  1067  
  1068  void VP8LDelete(VP8LDecoder* const dec) {
  1069    if (dec != NULL) {
  1070      VP8LClear(dec);
  1071      free(dec);
  1072    }
  1073  }
  1074  
  1075  static void UpdateDecoder(VP8LDecoder* const dec, int width, int height) {
  1076    VP8LMetadata* const hdr = &dec->hdr_;
  1077    const int num_bits = hdr->huffman_subsample_bits_;
  1078    dec->width_ = width;
  1079    dec->height_ = height;
  1080  
  1081    hdr->huffman_xsize_ = VP8LSubSampleSize(width, num_bits);
  1082    hdr->huffman_mask_ = (num_bits == 0) ? ~0 : (1 << num_bits) - 1;
  1083  }
  1084  
  1085  static int DecodeImageStream(int xsize, int ysize,
  1086                               int is_level0,
  1087                               VP8LDecoder* const dec,
  1088                               uint32_t** const decoded_data) {
  1089    int ok = 1;
  1090    int transform_xsize = xsize;
  1091    int transform_ysize = ysize;
  1092    VP8LBitReader* const br = &dec->br_;
  1093    VP8LMetadata* const hdr = &dec->hdr_;
  1094    uint32_t* data = NULL;
  1095    int color_cache_bits = 0;
  1096  
  1097    // Read the transforms (may recurse).
  1098    if (is_level0) {
  1099      while (ok && VP8LReadBits(br, 1)) {
  1100        ok = ReadTransform(&transform_xsize, &transform_ysize, dec);
  1101      }
  1102    }
  1103  
  1104    // Color cache
  1105    if (ok && VP8LReadBits(br, 1)) {
  1106      color_cache_bits = VP8LReadBits(br, 4);
  1107      ok = (color_cache_bits >= 1 && color_cache_bits <= MAX_CACHE_BITS);
  1108      if (!ok) {
  1109        dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
  1110        goto End;
  1111      }
  1112    }
  1113  
  1114    // Read the Huffman codes (may recurse).
  1115    ok = ok && ReadHuffmanCodes(dec, transform_xsize, transform_ysize,
  1116                                color_cache_bits, is_level0);
  1117    if (!ok) {
  1118      dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
  1119      goto End;
  1120    }
  1121  
  1122    // Finish setting up the color-cache
  1123    if (color_cache_bits > 0) {
  1124      hdr->color_cache_size_ = 1 << color_cache_bits;
  1125      if (!VP8LColorCacheInit(&hdr->color_cache_, color_cache_bits)) {
  1126        dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
  1127        ok = 0;
  1128        goto End;
  1129      }
  1130    } else {
  1131      hdr->color_cache_size_ = 0;
  1132    }
  1133    UpdateDecoder(dec, transform_xsize, transform_ysize);
  1134  
  1135    if (is_level0) {   // level 0 complete
  1136      dec->state_ = READ_HDR;
  1137      goto End;
  1138    }
  1139  
  1140    {
  1141      const uint64_t total_size = (uint64_t)transform_xsize * transform_ysize;
  1142      data = (uint32_t*)WebPSafeMalloc(total_size, sizeof(*data));
  1143      if (data == NULL) {
  1144        dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
  1145        ok = 0;
  1146        goto End;
  1147      }
  1148    }
  1149  
  1150    // Use the Huffman trees to decode the LZ77 encoded data.
  1151    ok = DecodeImageData(dec, data, transform_xsize, transform_ysize,
  1152                         transform_ysize, NULL);
  1153    ok = ok && !br->error_;
  1154  
  1155   End:
  1156  
  1157    if (!ok) {
  1158      free(data);
  1159      ClearMetadata(hdr);
  1160      // If not enough data (br.eos_) resulted in BIT_STREAM_ERROR, update the
  1161      // status appropriately.
  1162      if (dec->status_ == VP8_STATUS_BITSTREAM_ERROR && dec->br_.eos_) {
  1163        dec->status_ = VP8_STATUS_SUSPENDED;
  1164      }
  1165    } else {
  1166      if (decoded_data != NULL) {
  1167        *decoded_data = data;
  1168      } else {
  1169        // We allocate image data in this function only for transforms. At level 0
  1170        // (that is: not the transforms), we shouldn't have allocated anything.
  1171        assert(data == NULL);
  1172        assert(is_level0);
  1173      }
  1174      dec->last_pixel_ = 0;  // Reset for future DECODE_DATA_FUNC() calls.
  1175      if (!is_level0) ClearMetadata(hdr);  // Clean up temporary data behind.
  1176    }
  1177    return ok;
  1178  }
  1179  
  1180  //------------------------------------------------------------------------------
  1181  // Allocate internal buffers dec->pixels_ and dec->argb_cache_.
  1182  static int AllocateInternalBuffers32b(VP8LDecoder* const dec, int final_width) {
  1183    const uint64_t num_pixels = (uint64_t)dec->width_ * dec->height_;
  1184    // Scratch buffer corresponding to top-prediction row for transforming the
  1185    // first row in the row-blocks. Not needed for paletted alpha.
  1186    const uint64_t cache_top_pixels = (uint16_t)final_width;
  1187    // Scratch buffer for temporary BGRA storage. Not needed for paletted alpha.
  1188    const uint64_t cache_pixels = (uint64_t)final_width * NUM_ARGB_CACHE_ROWS;
  1189    const uint64_t total_num_pixels =
  1190        num_pixels + cache_top_pixels + cache_pixels;
  1191  
  1192    assert(dec->width_ <= final_width);
  1193    dec->pixels_ = (uint32_t*)WebPSafeMalloc(total_num_pixels, sizeof(uint32_t));
  1194    if (dec->pixels_ == NULL) {
  1195      dec->argb_cache_ = NULL;    // for sanity check
  1196      dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
  1197      return 0;
  1198    }
  1199    dec->argb_cache_ = dec->pixels_ + num_pixels + cache_top_pixels;
  1200    return 1;
  1201  }
  1202  
  1203  static int AllocateInternalBuffers8b(VP8LDecoder* const dec) {
  1204    const uint64_t total_num_pixels = (uint64_t)dec->width_ * dec->height_;
  1205    dec->argb_cache_ = NULL;    // for sanity check
  1206    dec->pixels_ = (uint32_t*)WebPSafeMalloc(total_num_pixels, sizeof(uint8_t));
  1207    if (dec->pixels_ == NULL) {
  1208      dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
  1209      return 0;
  1210    }
  1211    return 1;
  1212  }
  1213  
  1214  //------------------------------------------------------------------------------
  1215  
  1216  // Special row-processing that only stores the alpha data.
  1217  static void ExtractAlphaRows(VP8LDecoder* const dec, int row) {
  1218    const int num_rows = row - dec->last_row_;
  1219    const uint32_t* const in = dec->pixels_ + dec->width_ * dec->last_row_;
  1220  
  1221    if (num_rows <= 0) return;  // Nothing to be done.
  1222    ApplyInverseTransforms(dec, num_rows, in);
  1223  
  1224    // Extract alpha (which is stored in the green plane).
  1225    {
  1226      const int width = dec->io_->width;      // the final width (!= dec->width_)
  1227      const int cache_pixs = width * num_rows;
  1228      uint8_t* const dst = (uint8_t*)dec->io_->opaque + width * dec->last_row_;
  1229      const uint32_t* const src = dec->argb_cache_;
  1230      int i;
  1231      for (i = 0; i < cache_pixs; ++i) dst[i] = (src[i] >> 8) & 0xff;
  1232    }
  1233    dec->last_row_ = dec->last_out_row_ = row;
  1234  }
  1235  
  1236  int VP8LDecodeAlphaHeader(ALPHDecoder* const alph_dec,
  1237                            const uint8_t* const data, size_t data_size,
  1238                            uint8_t* const output) {
  1239    int ok = 0;
  1240    VP8LDecoder* dec;
  1241    VP8Io* io;
  1242    assert(alph_dec != NULL);
  1243    alph_dec->vp8l_dec_ = VP8LNew();
  1244    if (alph_dec->vp8l_dec_ == NULL) return 0;
  1245    dec = alph_dec->vp8l_dec_;
  1246  
  1247    dec->width_ = alph_dec->width_;
  1248    dec->height_ = alph_dec->height_;
  1249    dec->io_ = &alph_dec->io_;
  1250    io = dec->io_;
  1251  
  1252    VP8InitIo(io);
  1253    WebPInitCustomIo(NULL, io);  // Just a sanity Init. io won't be used.
  1254    io->opaque = output;
  1255    io->width = alph_dec->width_;
  1256    io->height = alph_dec->height_;
  1257  
  1258    dec->status_ = VP8_STATUS_OK;
  1259    VP8LInitBitReader(&dec->br_, data, data_size);
  1260  
  1261    dec->action_ = READ_HDR;
  1262    if (!DecodeImageStream(alph_dec->width_, alph_dec->height_, 1, dec, NULL)) {
  1263      goto Err;
  1264    }
  1265  
  1266    // Special case: if alpha data uses only the color indexing transform and
  1267    // doesn't use color cache (a frequent case), we will use DecodeAlphaData()
  1268    // method that only needs allocation of 1 byte per pixel (alpha channel).
  1269    if (dec->next_transform_ == 1 &&
  1270        dec->transforms_[0].type_ == COLOR_INDEXING_TRANSFORM &&
  1271        Is8bOptimizable(&dec->hdr_)) {
  1272      alph_dec->use_8b_decode = 1;
  1273      ok = AllocateInternalBuffers8b(dec);
  1274    } else {
  1275      // Allocate internal buffers (note that dec->width_ may have changed here).
  1276      alph_dec->use_8b_decode = 0;
  1277      ok = AllocateInternalBuffers32b(dec, alph_dec->width_);
  1278    }
  1279  
  1280    if (!ok) goto Err;
  1281  
  1282    dec->action_ = READ_DATA;
  1283    return 1;
  1284  
  1285   Err:
  1286    VP8LDelete(alph_dec->vp8l_dec_);
  1287    alph_dec->vp8l_dec_ = NULL;
  1288    return 0;
  1289  }
  1290  
  1291  int VP8LDecodeAlphaImageStream(ALPHDecoder* const alph_dec, int last_row) {
  1292    VP8LDecoder* const dec = alph_dec->vp8l_dec_;
  1293    assert(dec != NULL);
  1294    assert(dec->action_ == READ_DATA);
  1295    assert(last_row <= dec->height_);
  1296  
  1297    // Decode (with special row processing).
  1298    return alph_dec->use_8b_decode ?
  1299        DecodeAlphaData(dec, (uint8_t*)dec->pixels_, dec->width_, dec->height_,
  1300                        last_row) :
  1301        DecodeImageData(dec, dec->pixels_, dec->width_, dec->height_,
  1302                        last_row, ExtractAlphaRows);
  1303  }
  1304  
  1305  //------------------------------------------------------------------------------
  1306  
  1307  int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io) {
  1308    int width, height, has_alpha;
  1309  
  1310    if (dec == NULL) return 0;
  1311    if (io == NULL) {
  1312      dec->status_ = VP8_STATUS_INVALID_PARAM;
  1313      return 0;
  1314    }
  1315  
  1316    dec->io_ = io;
  1317    dec->status_ = VP8_STATUS_OK;
  1318    VP8LInitBitReader(&dec->br_, io->data, io->data_size);
  1319    if (!ReadImageInfo(&dec->br_, &width, &height, &has_alpha)) {
  1320      dec->status_ = VP8_STATUS_BITSTREAM_ERROR;
  1321      goto Error;
  1322    }
  1323    dec->state_ = READ_DIM;
  1324    io->width = width;
  1325    io->height = height;
  1326  
  1327    dec->action_ = READ_HDR;
  1328    if (!DecodeImageStream(width, height, 1, dec, NULL)) goto Error;
  1329    return 1;
  1330  
  1331   Error:
  1332    VP8LClear(dec);
  1333    assert(dec->status_ != VP8_STATUS_OK);
  1334    return 0;
  1335  }
  1336  
  1337  int VP8LDecodeImage(VP8LDecoder* const dec) {
  1338    VP8Io* io = NULL;
  1339    WebPDecParams* params = NULL;
  1340  
  1341    // Sanity checks.
  1342    if (dec == NULL) return 0;
  1343  
  1344    io = dec->io_;
  1345    assert(io != NULL);
  1346    params = (WebPDecParams*)io->opaque;
  1347    assert(params != NULL);
  1348    dec->output_ = params->output;
  1349    assert(dec->output_ != NULL);
  1350  
  1351    // Initialization.
  1352    if (!WebPIoInitFromOptions(params->options, io, MODE_BGRA)) {
  1353      dec->status_ = VP8_STATUS_INVALID_PARAM;
  1354      goto Err;
  1355    }
  1356  
  1357    if (!AllocateInternalBuffers32b(dec, io->width)) goto Err;
  1358  
  1359    if (io->use_scaling && !AllocateAndInitRescaler(dec, io)) goto Err;
  1360  
  1361    // Decode.
  1362    dec->action_ = READ_DATA;
  1363    if (!DecodeImageData(dec, dec->pixels_, dec->width_, dec->height_,
  1364                         dec->height_, ProcessRows)) {
  1365      goto Err;
  1366    }
  1367  
  1368    // Cleanup.
  1369    params->last_y = dec->last_out_row_;
  1370    VP8LClear(dec);
  1371    return 1;
  1372  
  1373   Err:
  1374    VP8LClear(dec);
  1375    assert(dec->status_ != VP8_STATUS_OK);
  1376    return 0;
  1377  }
  1378  
  1379  //------------------------------------------------------------------------------
  1380