webp.c revision 9e80ee991168a0a6c2a906dd2c17c5e17df4566e
1a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Copyright 2010 Google Inc. All Rights Reserved.
29aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//
30406ce1417f76f2034833414dcecc9f56253640cVikas Arora// Use of this source code is governed by a BSD-style license
40406ce1417f76f2034833414dcecc9f56253640cVikas Arora// that can be found in the COPYING file in the root of the source
50406ce1417f76f2034833414dcecc9f56253640cVikas Arora// tree. An additional intellectual property rights grant can be found
60406ce1417f76f2034833414dcecc9f56253640cVikas Arora// in the file PATENTS. All contributing project authors may
70406ce1417f76f2034833414dcecc9f56253640cVikas Arora// be found in the AUTHORS file in the root of the source tree.
89aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// -----------------------------------------------------------------------------
99aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//
109aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Main decoding functions for WEBP images.
119aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//
129aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Author: Skal (pascal.massimino@gmail.com)
139aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
149aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#include <stdlib.h>
15a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
16a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#include "./vp8i.h"
17a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#include "./vp8li.h"
18a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#include "./webpi.h"
199e80ee991168a0a6c2a906dd2c17c5e17df4566eJames Zern#include "../webp/mux_types.h"  // ALPHA_FLAG
209aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
21a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
229aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// RIFF layout is:
23a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//   Offset  tag
249aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//   0...3   "RIFF" 4-byte tag
259aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//   4...7   size of image data (including metadata) starting at offset 8
269aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//   8...11  "WEBP"   our form-type signature
27a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// The RIFF container (12 bytes) is followed by appropriate chunks:
28a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//   12..15  "VP8 ": 4-bytes tags, signaling the use of VP8 video format
299aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//   16..19  size of the raw VP8 image data, starting at offset 20
309aea642eefa7a641ab8b89d953251939221d2719Eric Hassold//   20....  the VP8 bytes
31a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Or,
32a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//   12..15  "VP8L": 4-bytes tags, signaling the use of VP8L lossless format
33a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//   16..19  size of the raw VP8L image data, starting at offset 20
34a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//   20....  the VP8L bytes
35a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Or,
36a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//   12..15  "VP8X": 4-bytes tags, describing the extended-VP8 chunk.
37a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//   16..19  size of the VP8X chunk starting at offset 20.
38a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//   20..23  VP8X flags bit-map corresponding to the chunk-types present.
39a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//   24..26  Width of the Canvas Image.
40a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//   27..29  Height of the Canvas Image.
411e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// There can be extra chunks after the "VP8X" chunk (ICCP, FRGM, ANMF, VP8,
421e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora// VP8L, XMP, EXIF  ...)
43a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// All sizes are in little-endian order.
44a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note: chunk data size must be padded to multiple of 2 when written.
45a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
46a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE uint32_t get_le24(const uint8_t* const data) {
47a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  return data[0] | (data[1] << 8) | (data[2] << 16);
48a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora}
499aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
50a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE uint32_t get_le32(const uint8_t* const data) {
51a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  return (uint32_t)get_le24(data) | (data[3] << 24);
529aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
539aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
54a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Validates the RIFF container (if detected) and skips over it.
5533f74dabbc7920a65ed435d7417987589febdc16Vikas Arora// If a RIFF container is detected, returns:
5633f74dabbc7920a65ed435d7417987589febdc16Vikas Arora//     VP8_STATUS_BITSTREAM_ERROR for invalid header,
5733f74dabbc7920a65ed435d7417987589febdc16Vikas Arora//     VP8_STATUS_NOT_ENOUGH_DATA for truncated data if have_all_data is true,
5833f74dabbc7920a65ed435d7417987589febdc16Vikas Arora// and VP8_STATUS_OK otherwise.
59a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// In case there are not enough bytes (partial RIFF container), return 0 for
60a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// *riff_size. Else return the RIFF size extracted from the header.
61a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode ParseRIFF(const uint8_t** const data,
6233f74dabbc7920a65ed435d7417987589febdc16Vikas Arora                               size_t* const data_size, int have_all_data,
63a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                               size_t* const riff_size) {
64a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(data != NULL);
65a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(data_size != NULL);
66a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(riff_size != NULL);
67a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
68a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  *riff_size = 0;  // Default: no RIFF present.
69a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (*data_size >= RIFF_HEADER_SIZE && !memcmp(*data, "RIFF", TAG_SIZE)) {
70a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (memcmp(*data + 8, "WEBP", TAG_SIZE)) {
71a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_BITSTREAM_ERROR;  // Wrong image file signature.
729aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    } else {
73a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      const uint32_t size = get_le32(*data + TAG_SIZE);
74a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      // Check that we have at least one chunk (i.e "WEBP" + "VP8?nnnn").
75a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      if (size < TAG_SIZE + CHUNK_HEADER_SIZE) {
76a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora        return VP8_STATUS_BITSTREAM_ERROR;
779aea642eefa7a641ab8b89d953251939221d2719Eric Hassold      }
78276905dd9bf0cffd7e04c78c95f2c1057275d5bdGeremy Condra      if (size > MAX_CHUNK_PAYLOAD) {
79276905dd9bf0cffd7e04c78c95f2c1057275d5bdGeremy Condra        return VP8_STATUS_BITSTREAM_ERROR;
80276905dd9bf0cffd7e04c78c95f2c1057275d5bdGeremy Condra      }
8133f74dabbc7920a65ed435d7417987589febdc16Vikas Arora      if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {
8233f74dabbc7920a65ed435d7417987589febdc16Vikas Arora        return VP8_STATUS_NOT_ENOUGH_DATA;  // Truncated bitstream.
8333f74dabbc7920a65ed435d7417987589febdc16Vikas Arora      }
8403d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora      // We have a RIFF container. Skip it.
85a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      *riff_size = size;
86a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      *data += RIFF_HEADER_SIZE;
87a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      *data_size -= RIFF_HEADER_SIZE;
88a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
89a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
90a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  return VP8_STATUS_OK;
91a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora}
92a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
93a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Validates the VP8X header and skips over it.
94a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns VP8_STATUS_BITSTREAM_ERROR for invalid VP8X header,
95a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//         VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
96a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//         VP8_STATUS_OK otherwise.
97a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// If a VP8X chunk is found, found_vp8x is set to true and *width_ptr,
98a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// *height_ptr and *flags_ptr are set to the corresponding values extracted
99a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// from the VP8X chunk.
100a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode ParseVP8X(const uint8_t** const data,
101a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                               size_t* const data_size,
102a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                               int* const found_vp8x,
103a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                               int* const width_ptr, int* const height_ptr,
104a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                               uint32_t* const flags_ptr) {
105a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
106a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(data != NULL);
107a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(data_size != NULL);
108a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(found_vp8x != NULL);
109a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
110a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  *found_vp8x = 0;
111a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
112a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (*data_size < CHUNK_HEADER_SIZE) {
113a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    return VP8_STATUS_NOT_ENOUGH_DATA;  // Insufficient data.
114a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
115a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
116a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (!memcmp(*data, "VP8X", TAG_SIZE)) {
117a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    int width, height;
118a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    uint32_t flags;
119a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    const uint32_t chunk_size = get_le32(*data + TAG_SIZE);
120a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (chunk_size != VP8X_CHUNK_SIZE) {
121a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_BITSTREAM_ERROR;  // Wrong chunk size.
122a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
123a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
124a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    // Verify if enough data is available to validate the VP8X chunk.
125a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (*data_size < vp8x_size) {
126a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_NOT_ENOUGH_DATA;  // Insufficient data.
127a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
128a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    flags = get_le32(*data + 8);
129a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    width = 1 + get_le24(*data + 12);
130a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    height = 1 + get_le24(*data + 15);
131a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (width * (uint64_t)height >= MAX_IMAGE_AREA) {
132a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_BITSTREAM_ERROR;  // image is too large
133a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
134a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
135a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (flags_ptr != NULL) *flags_ptr = flags;
136a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (width_ptr != NULL) *width_ptr = width;
137a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (height_ptr != NULL) *height_ptr = height;
138a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    // Skip over VP8X header bytes.
139a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *data += vp8x_size;
140a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *data_size -= vp8x_size;
141a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *found_vp8x = 1;
142a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
143a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  return VP8_STATUS_OK;
144a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora}
145a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
146a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Skips to the next VP8/VP8L chunk header in the data given the size of the
147a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// RIFF chunk 'riff_size'.
148a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns VP8_STATUS_BITSTREAM_ERROR if any invalid chunk size is encountered,
149a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//         VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
150a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//         VP8_STATUS_OK otherwise.
151a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// If an alpha chunk is found, *alpha_data and *alpha_size are set
152a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// appropriately.
153a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode ParseOptionalChunks(const uint8_t** const data,
154a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                         size_t* const data_size,
155a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                         size_t const riff_size,
156a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                         const uint8_t** const alpha_data,
157a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                         size_t* const alpha_size) {
158a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  const uint8_t* buf;
159a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  size_t buf_size;
160a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  uint32_t total_size = TAG_SIZE +           // "WEBP".
161a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                        CHUNK_HEADER_SIZE +  // "VP8Xnnnn".
162a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                        VP8X_CHUNK_SIZE;     // data.
163a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(data != NULL);
164a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(data_size != NULL);
165a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  buf = *data;
166a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  buf_size = *data_size;
167a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
168a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(alpha_data != NULL);
169a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(alpha_size != NULL);
170a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  *alpha_data = NULL;
171a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  *alpha_size = 0;
172a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
173a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  while (1) {
174a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    uint32_t chunk_size;
175a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    uint32_t disk_chunk_size;   // chunk_size with padding
176a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
177a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *data = buf;
178a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *data_size = buf_size;
179a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
180a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (buf_size < CHUNK_HEADER_SIZE) {  // Insufficient data.
181a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_NOT_ENOUGH_DATA;
182a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
183a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
184a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    chunk_size = get_le32(buf + TAG_SIZE);
185276905dd9bf0cffd7e04c78c95f2c1057275d5bdGeremy Condra    if (chunk_size > MAX_CHUNK_PAYLOAD) {
186276905dd9bf0cffd7e04c78c95f2c1057275d5bdGeremy Condra      return VP8_STATUS_BITSTREAM_ERROR;          // Not a valid chunk size.
187276905dd9bf0cffd7e04c78c95f2c1057275d5bdGeremy Condra    }
188a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    // For odd-sized chunk-payload, there's one byte padding at the end.
189a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    disk_chunk_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1;
190a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    total_size += disk_chunk_size;
191a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
192a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    // Check that total bytes skipped so far does not exceed riff_size.
193a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (riff_size > 0 && (total_size > riff_size)) {
194a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_BITSTREAM_ERROR;          // Not a valid chunk size.
195a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
196a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
1970406ce1417f76f2034833414dcecc9f56253640cVikas Arora    // Start of a (possibly incomplete) VP8/VP8L chunk implies that we have
1980406ce1417f76f2034833414dcecc9f56253640cVikas Arora    // parsed all the optional chunks.
1990406ce1417f76f2034833414dcecc9f56253640cVikas Arora    // Note: This check must occur before the check 'buf_size < disk_chunk_size'
2000406ce1417f76f2034833414dcecc9f56253640cVikas Arora    // below to allow incomplete VP8/VP8L chunks.
2010406ce1417f76f2034833414dcecc9f56253640cVikas Arora    if (!memcmp(buf, "VP8 ", TAG_SIZE) ||
2020406ce1417f76f2034833414dcecc9f56253640cVikas Arora        !memcmp(buf, "VP8L", TAG_SIZE)) {
2030406ce1417f76f2034833414dcecc9f56253640cVikas Arora      return VP8_STATUS_OK;
2040406ce1417f76f2034833414dcecc9f56253640cVikas Arora    }
2050406ce1417f76f2034833414dcecc9f56253640cVikas Arora
206a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (buf_size < disk_chunk_size) {             // Insufficient data.
207a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_NOT_ENOUGH_DATA;
208a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
209a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
210a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (!memcmp(buf, "ALPH", TAG_SIZE)) {         // A valid ALPH header.
211a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      *alpha_data = buf + CHUNK_HEADER_SIZE;
212a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      *alpha_size = chunk_size;
213a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
214a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
215a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    // We have a full and valid chunk; skip it.
216a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    buf += disk_chunk_size;
217a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    buf_size -= disk_chunk_size;
218a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
219a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora}
220a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
221a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Validates the VP8/VP8L Header ("VP8 nnnn" or "VP8L nnnn") and skips over it.
222a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than
223a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//         riff_size) VP8/VP8L header,
224a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//         VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
225a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//         VP8_STATUS_OK otherwise.
226a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// If a VP8/VP8L chunk is found, *chunk_size is set to the total number of bytes
227a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// extracted from the VP8/VP8L chunk header.
228a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// The flag '*is_lossless' is set to 1 in case of VP8L chunk / raw VP8L data.
229a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode ParseVP8Header(const uint8_t** const data_ptr,
23033f74dabbc7920a65ed435d7417987589febdc16Vikas Arora                                    size_t* const data_size, int have_all_data,
23133f74dabbc7920a65ed435d7417987589febdc16Vikas Arora                                    size_t riff_size, size_t* const chunk_size,
232a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                    int* const is_lossless) {
233a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  const uint8_t* const data = *data_ptr;
234a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  const int is_vp8 = !memcmp(data, "VP8 ", TAG_SIZE);
235a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  const int is_vp8l = !memcmp(data, "VP8L", TAG_SIZE);
236a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  const uint32_t minimal_size =
237a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      TAG_SIZE + CHUNK_HEADER_SIZE;  // "WEBP" + "VP8 nnnn" OR
238a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                     // "WEBP" + "VP8Lnnnn"
239a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(data != NULL);
240a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(data_size != NULL);
241a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(chunk_size != NULL);
242a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(is_lossless != NULL);
243a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
244a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (*data_size < CHUNK_HEADER_SIZE) {
245a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    return VP8_STATUS_NOT_ENOUGH_DATA;  // Insufficient data.
246a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
247a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
248a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (is_vp8 || is_vp8l) {
249a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    // Bitstream contains VP8/VP8L header.
250a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    const uint32_t size = get_le32(data + TAG_SIZE);
251a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if ((riff_size >= minimal_size) && (size > riff_size - minimal_size)) {
252a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_BITSTREAM_ERROR;  // Inconsistent size information.
253a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
25433f74dabbc7920a65ed435d7417987589febdc16Vikas Arora    if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {
25533f74dabbc7920a65ed435d7417987589febdc16Vikas Arora      return VP8_STATUS_NOT_ENOUGH_DATA;  // Truncated bitstream.
25633f74dabbc7920a65ed435d7417987589febdc16Vikas Arora    }
257a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    // Skip over CHUNK_HEADER_SIZE bytes from VP8/VP8L Header.
258a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *chunk_size = size;
259a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *data_ptr += CHUNK_HEADER_SIZE;
260a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *data_size -= CHUNK_HEADER_SIZE;
261a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *is_lossless = is_vp8l;
262a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  } else {
263a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    // Raw VP8/VP8L bitstream (no header).
264a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *is_lossless = VP8LCheckSignature(data, *data_size);
265a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *chunk_size = *data_size;
266a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
267a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
268a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  return VP8_STATUS_OK;
269a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora}
270a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
271a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
272a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
273a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Fetch '*width', '*height', '*has_alpha' and fill out 'headers' based on
274a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 'data'. All the output parameters may be NULL. If 'headers' is NULL only the
275a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// minimal amount will be read to fetch the remaining parameters.
276a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// If 'headers' is non-NULL this function will attempt to locate both alpha
277a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// data (with or without a VP8X chunk) and the bitstream chunk (VP8/VP8L).
278a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note: The following chunk sequences (before the raw VP8/VP8L data) are
279a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// considered valid by this function:
280a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// RIFF + VP8(L)
281a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// RIFF + VP8X + (optional chunks) + VP8(L)
282a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.
283a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// VP8(L)     <-- Not a valid WebP format: only allowed for internal purpose.
284a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode ParseHeadersInternal(const uint8_t* data,
285a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                          size_t data_size,
286a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                          int* const width,
287a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                          int* const height,
288a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                          int* const has_alpha,
2890406ce1417f76f2034833414dcecc9f56253640cVikas Arora                                          int* const has_animation,
2908b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora                                          int* const format,
291a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                          WebPHeaderStructure* const headers) {
2928b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  int canvas_width = 0;
2938b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  int canvas_height = 0;
2948b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  int image_width = 0;
2958b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  int image_height = 0;
296a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int found_riff = 0;
297a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int found_vp8x = 0;
2988b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  int animation_present = 0;
2998b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  int fragments_present = 0;
30033f74dabbc7920a65ed435d7417987589febdc16Vikas Arora  const int have_all_data = (headers != NULL) ? headers->have_all_data : 0;
3018b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora
302a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  VP8StatusCode status;
303a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  WebPHeaderStructure hdrs;
304a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
305a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (data == NULL || data_size < RIFF_HEADER_SIZE) {
306a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    return VP8_STATUS_NOT_ENOUGH_DATA;
307a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
308a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  memset(&hdrs, 0, sizeof(hdrs));
309a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  hdrs.data = data;
310a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  hdrs.data_size = data_size;
311a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
312a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Skip over RIFF header.
31333f74dabbc7920a65ed435d7417987589febdc16Vikas Arora  status = ParseRIFF(&data, &data_size, have_all_data, &hdrs.riff_size);
314a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (status != VP8_STATUS_OK) {
315a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    return status;   // Wrong RIFF header / insufficient data.
316a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
317a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  found_riff = (hdrs.riff_size > 0);
318a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
319a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Skip over VP8X.
320a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  {
321a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    uint32_t flags = 0;
3228b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    status = ParseVP8X(&data, &data_size, &found_vp8x,
3238b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora                       &canvas_width, &canvas_height, &flags);
324a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (status != VP8_STATUS_OK) {
325a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return status;  // Wrong VP8X / insufficient data.
326a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
3278b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    animation_present = !!(flags & ANIMATION_FLAG);
3288b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    fragments_present = !!(flags & FRAGMENTS_FLAG);
329a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (!found_riff && found_vp8x) {
330a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      // Note: This restriction may be removed in the future, if it becomes
331a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      // necessary to send VP8X chunk to the decoder.
332a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_BITSTREAM_ERROR;
333a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
3341e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora    if (has_alpha != NULL) *has_alpha = !!(flags & ALPHA_FLAG);
3358b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    if (has_animation != NULL) *has_animation = animation_present;
3368b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    if (format != NULL) *format = 0;   // default = undefined
3378b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora
3388b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    image_width = canvas_width;
3398b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    image_height = canvas_height;
3408b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    if (found_vp8x && (animation_present || fragments_present) &&
3418b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora        headers == NULL) {
3428b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora      status = VP8_STATUS_OK;
3438b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora      goto ReturnWidthHeight;  // Just return features from VP8X header.
344a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
345a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
346a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
3478b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  if (data_size < TAG_SIZE) {
3488b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    status = VP8_STATUS_NOT_ENOUGH_DATA;
3498b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    goto ReturnWidthHeight;
3508b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  }
351a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
352a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Skip over optional chunks if data started with "RIFF + VP8X" or "ALPH".
353a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if ((found_riff && found_vp8x) ||
354a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      (!found_riff && !found_vp8x && !memcmp(data, "ALPH", TAG_SIZE))) {
355a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    status = ParseOptionalChunks(&data, &data_size, hdrs.riff_size,
356a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                 &hdrs.alpha_data, &hdrs.alpha_data_size);
357a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (status != VP8_STATUS_OK) {
3588b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora      goto ReturnWidthHeight;  // Invalid chunk size / insufficient data.
359a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
360a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
361a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
362a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Skip over VP8/VP8L header.
36333f74dabbc7920a65ed435d7417987589febdc16Vikas Arora  status = ParseVP8Header(&data, &data_size, have_all_data, hdrs.riff_size,
364a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                          &hdrs.compressed_size, &hdrs.is_lossless);
365a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (status != VP8_STATUS_OK) {
3668b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    goto ReturnWidthHeight;  // Wrong VP8/VP8L chunk-header / insufficient data.
367a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
368a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (hdrs.compressed_size > MAX_CHUNK_PAYLOAD) {
369a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    return VP8_STATUS_BITSTREAM_ERROR;
370a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
371a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
3728b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  if (format != NULL && !(animation_present || fragments_present)) {
3738b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    *format = hdrs.is_lossless ? 2 : 1;
3748b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  }
3758b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora
376a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (!hdrs.is_lossless) {
377a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (data_size < VP8_FRAME_HEADER_SIZE) {
3788b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora      status = VP8_STATUS_NOT_ENOUGH_DATA;
3798b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora      goto ReturnWidthHeight;
380a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
381a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    // Validates raw VP8 data.
3828b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    if (!VP8GetInfo(data, data_size, (uint32_t)hdrs.compressed_size,
3838b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora                    &image_width, &image_height)) {
384a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_BITSTREAM_ERROR;
3859aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    }
386a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  } else {
387a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (data_size < VP8L_FRAME_HEADER_SIZE) {
3888b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora      status = VP8_STATUS_NOT_ENOUGH_DATA;
3898b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora      goto ReturnWidthHeight;
390a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
391a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    // Validates raw VP8L data.
3928b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    if (!VP8LGetInfo(data, data_size, &image_width, &image_height, has_alpha)) {
393a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_BITSTREAM_ERROR;
394a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
395a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
3968b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  // Validates image size coherency.
3978b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  if (found_vp8x) {
3988b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    if (canvas_width != image_width || canvas_height != image_height) {
3998b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora      return VP8_STATUS_BITSTREAM_ERROR;
4008b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    }
401a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
402a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (headers != NULL) {
403a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *headers = hdrs;
404a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    headers->offset = data - headers->data;
405a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    assert((uint64_t)(data - headers->data) < MAX_CHUNK_PAYLOAD);
406a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    assert(headers->offset == headers->data_size - data_size);
4079aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
4088b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora ReturnWidthHeight:
4098b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  if (status == VP8_STATUS_OK ||
4108b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora      (status == VP8_STATUS_NOT_ENOUGH_DATA && found_vp8x && headers == NULL)) {
4118b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    if (has_alpha != NULL) {
4128b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora      // If the data did not contain a VP8X/VP8L chunk the only definitive way
4138b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora      // to set this is by looking for alpha data (from an ALPH chunk).
4148b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora      *has_alpha |= (hdrs.alpha_data != NULL);
4158b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    }
4168b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    if (width != NULL) *width = image_width;
4178b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    if (height != NULL) *height = image_height;
4188b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    return VP8_STATUS_OK;
4198b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  } else {
4208b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora    return status;
4218b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  }
422a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora}
423a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
424a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraVP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) {
4250406ce1417f76f2034833414dcecc9f56253640cVikas Arora  VP8StatusCode status;
4260406ce1417f76f2034833414dcecc9f56253640cVikas Arora  int has_animation = 0;
427a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(headers != NULL);
428a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // fill out headers, ignore width/height/has_alpha.
4290406ce1417f76f2034833414dcecc9f56253640cVikas Arora  status = ParseHeadersInternal(headers->data, headers->data_size,
4308b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora                                NULL, NULL, NULL, &has_animation,
4318b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora                                NULL, headers);
4320406ce1417f76f2034833414dcecc9f56253640cVikas Arora  if (status == VP8_STATUS_OK || status == VP8_STATUS_NOT_ENOUGH_DATA) {
4330406ce1417f76f2034833414dcecc9f56253640cVikas Arora    // TODO(jzern): full support of animation frames will require API additions.
4340406ce1417f76f2034833414dcecc9f56253640cVikas Arora    if (has_animation) {
4350406ce1417f76f2034833414dcecc9f56253640cVikas Arora      status = VP8_STATUS_UNSUPPORTED_FEATURE;
4360406ce1417f76f2034833414dcecc9f56253640cVikas Arora    }
4370406ce1417f76f2034833414dcecc9f56253640cVikas Arora  }
4380406ce1417f76f2034833414dcecc9f56253640cVikas Arora  return status;
4399aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
4409aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
441a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
442466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// WebPDecParams
44303d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora
444466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Aroravoid WebPResetDecParams(WebPDecParams* const params) {
4458b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora  if (params != NULL) {
446466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    memset(params, 0, sizeof(*params));
44703d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora  }
44803d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora}
44903d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora
450a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
451466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// "Into" decoding variants
4529aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
453466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Main flow
454a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode DecodeInto(const uint8_t* const data, size_t data_size,
455466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                                WebPDecParams* const params) {
456a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  VP8StatusCode status;
4579aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  VP8Io io;
458a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  WebPHeaderStructure headers;
4599aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
460a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  headers.data = data;
461a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  headers.data_size = data_size;
46233f74dabbc7920a65ed435d7417987589febdc16Vikas Arora  headers.have_all_data = 1;
463a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  status = WebPParseHeaders(&headers);   // Process Pre-VP8 chunks.
464a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (status != VP8_STATUS_OK) {
465a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    return status;
4669aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
4679aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
468a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(params != NULL);
4699aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  VP8InitIo(&io);
470a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  io.data = headers.data + headers.offset;
471a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  io.data_size = headers.data_size - headers.offset;
472466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPInitCustomIo(params, &io);  // Plug the I/O functions.
4739aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
474a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (!headers.is_lossless) {
475a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    VP8Decoder* const dec = VP8New();
476a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (dec == NULL) {
477a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_OUT_OF_MEMORY;
478a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
479a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    dec->alpha_data_ = headers.alpha_data;
480a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    dec->alpha_data_size_ = headers.alpha_data_size;
481a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
482a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    // Decode bitstream header, update io->width/io->height.
483a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (!VP8GetHeaders(dec, &io)) {
484a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      status = dec->status_;   // An error occurred. Grab error status.
485a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    } else {
486a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      // Allocate/check output buffers.
487a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      status = WebPAllocateDecBuffer(io.width, io.height, params->options,
488a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                     params->output);
489a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      if (status == VP8_STATUS_OK) {  // Decode
4908b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora        // This change must be done before calling VP8Decode()
4918b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora        dec->mt_method_ = VP8GetThreadMethod(params->options, &headers,
4928b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora                                             io.width, io.height);
4938b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora        VP8InitDithering(params->options, dec);
494a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora        if (!VP8Decode(dec, &io)) {
495a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora          status = dec->status_;
496a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora        }
497a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      }
498a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
499a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    VP8Delete(dec);
500466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  } else {
501a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    VP8LDecoder* const dec = VP8LNew();
502a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (dec == NULL) {
503a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_OUT_OF_MEMORY;
504a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
505a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (!VP8LDecodeHeader(dec, &io)) {
506a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      status = dec->status_;   // An error occurred. Grab error status.
507a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    } else {
508a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      // Allocate/check output buffers.
509a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      status = WebPAllocateDecBuffer(io.width, io.height, params->options,
510a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                     params->output);
511a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      if (status == VP8_STATUS_OK) {  // Decode
512a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora        if (!VP8LDecodeImage(dec)) {
513a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora          status = dec->status_;
514a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora        }
515466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora      }
516466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    }
517a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    VP8LDelete(dec);
5189aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
519a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
520466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (status != VP8_STATUS_OK) {
521466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    WebPFreeDecBuffer(params->output);
522466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
52333f74dabbc7920a65ed435d7417987589febdc16Vikas Arora
52433f74dabbc7920a65ed435d7417987589febdc16Vikas Arora#if WEBP_DECODER_ABI_VERSION > 0x0203
52533f74dabbc7920a65ed435d7417987589febdc16Vikas Arora  if (params->options != NULL && params->options->flip) {
52633f74dabbc7920a65ed435d7417987589febdc16Vikas Arora    status = WebPFlipBuffer(params->output);
52733f74dabbc7920a65ed435d7417987589febdc16Vikas Arora  }
52833f74dabbc7920a65ed435d7417987589febdc16Vikas Arora#endif
529466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  return status;
5309aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5319aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
532466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Helpers
533466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arorastatic uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace,
534a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                     const uint8_t* const data,
535a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                     size_t data_size,
536a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                     uint8_t* const rgba,
537a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                     int stride, size_t size) {
53803d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora  WebPDecParams params;
539466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPDecBuffer buf;
540466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (rgba == NULL) {
5419aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    return NULL;
5429aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
543466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPInitDecBuffer(&buf);
544466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPResetDecParams(&params);
545466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  params.output = &buf;
546466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  buf.colorspace    = colorspace;
547466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  buf.u.RGBA.rgba   = rgba;
548466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  buf.u.RGBA.stride = stride;
549466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  buf.u.RGBA.size   = size;
550466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  buf.is_external_memory = 1;
551466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
552466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    return NULL;
553466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
554466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  return rgba;
555466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora}
5569aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
557a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size,
558a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                           uint8_t* output, size_t size, int stride) {
559466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size);
5609aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5619aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
562a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size,
563a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                            uint8_t* output, size_t size, int stride) {
564466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size);
565466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora}
5669aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
567a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size,
568a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                            uint8_t* output, size_t size, int stride) {
569466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size);
5709aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5719aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
572a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size,
573a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                           uint8_t* output, size_t size, int stride) {
574466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size);
5759aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5769aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
577a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size,
578a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                            uint8_t* output, size_t size, int stride) {
579466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size);
5809aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
5819aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
582a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeYUVInto(const uint8_t* data, size_t data_size,
583a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                           uint8_t* luma, size_t luma_size, int luma_stride,
584a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                           uint8_t* u, size_t u_size, int u_stride,
585a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                           uint8_t* v, size_t v_size, int v_stride) {
58603d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora  WebPDecParams params;
587466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPDecBuffer output;
588466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (luma == NULL) return NULL;
589466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPInitDecBuffer(&output);
590466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPResetDecParams(&params);
591466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  params.output = &output;
592466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  output.colorspace      = MODE_YUV;
593466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  output.u.YUVA.y        = luma;
594466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  output.u.YUVA.y_stride = luma_stride;
595466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  output.u.YUVA.y_size   = luma_size;
596466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  output.u.YUVA.u        = u;
597466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  output.u.YUVA.u_stride = u_stride;
598466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  output.u.YUVA.u_size   = u_size;
599466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  output.u.YUVA.v        = v;
600466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  output.u.YUVA.v_stride = v_stride;
601466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  output.u.YUVA.v_size   = v_size;
602466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  output.is_external_memory = 1;
603466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
6049aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    return NULL;
6059aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
606466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  return luma;
6079aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6089aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
609a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
6109aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
611a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* const data,
612a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                       size_t data_size, int* const width, int* const height,
613a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                       WebPDecBuffer* const keep_info) {
61403d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora  WebPDecParams params;
615466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPDecBuffer output;
616466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
617466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPInitDecBuffer(&output);
618466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPResetDecParams(&params);
619466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  params.output = &output;
620466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  output.colorspace = mode;
6219aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
622466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  // Retrieve (and report back) the required dimensions from bitstream.
623466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (!WebPGetInfo(data, data_size, &output.width, &output.height)) {
6249aea642eefa7a641ab8b89d953251939221d2719Eric Hassold    return NULL;
6259aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
626a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (width != NULL) *width = output.width;
627a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (height != NULL) *height = output.height;
6289aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
629466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  // Decode
630466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
631466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    return NULL;
6329aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
633a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (keep_info != NULL) {    // keep track of the side-info
634466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    WebPCopyDecBuffer(&output, keep_info);
6359aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
636466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  // return decoded samples (don't clear 'output'!)
637a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y;
6389aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6399aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
640a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size,
641466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                       int* width, int* height) {
6429aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  return Decode(MODE_RGB, data, data_size, width, height, NULL);
6439aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6449aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
645a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size,
646466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                        int* width, int* height) {
6479aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  return Decode(MODE_RGBA, data, data_size, width, height, NULL);
6489aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6499aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
650a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size,
651466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                        int* width, int* height) {
652466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  return Decode(MODE_ARGB, data, data_size, width, height, NULL);
653466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora}
654466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
655a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size,
656466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                       int* width, int* height) {
6579aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  return Decode(MODE_BGR, data, data_size, width, height, NULL);
6589aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6599aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
660a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size,
661466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                        int* width, int* height) {
6629aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  return Decode(MODE_BGRA, data, data_size, width, height, NULL);
6639aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6649aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
665a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size,
666466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                       int* width, int* height, uint8_t** u, uint8_t** v,
667466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                       int* stride, int* uv_stride) {
668466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPDecBuffer output;   // only to preserve the side-infos
6699aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  uint8_t* const out = Decode(MODE_YUV, data, data_size,
670466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                              width, height, &output);
6719aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
672a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (out != NULL) {
673466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    const WebPYUVABuffer* const buf = &output.u.YUVA;
674466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    *u = buf->u;
675466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    *v = buf->v;
676466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    *stride = buf->y_stride;
677466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    *uv_stride = buf->u_stride;
678466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    assert(buf->u_stride == buf->v_stride);
6799aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
6809aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  return out;
6819aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
6829aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
683466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arorastatic void DefaultFeatures(WebPBitstreamFeatures* const features) {
684a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  assert(features != NULL);
685466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  memset(features, 0, sizeof(*features));
686466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora}
687466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
688a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size,
689466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                                 WebPBitstreamFeatures* const features) {
690a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (features == NULL || data == NULL) {
691466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    return VP8_STATUS_INVALID_PARAM;
6929aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
693466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  DefaultFeatures(features);
694a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
6950406ce1417f76f2034833414dcecc9f56253640cVikas Arora  // Only parse enough of the data to retrieve the features.
696a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  return ParseHeadersInternal(data, data_size,
697a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                              &features->width, &features->height,
6980406ce1417f76f2034833414dcecc9f56253640cVikas Arora                              &features->has_alpha, &features->has_animation,
6998b720228d581a84fd173b6dcb2fa295b59db489aVikas Arora                              &features->format, NULL);
700a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora}
701a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
702a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
703a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// WebPGetInfo()
704a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
705a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroraint WebPGetInfo(const uint8_t* data, size_t data_size,
706a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                int* width, int* height) {
707a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  WebPBitstreamFeatures features;
708a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
709a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (GetFeatures(data, data_size, &features) != VP8_STATUS_OK) {
710a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    return 0;
711466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
712a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
713a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (width != NULL) {
714a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *width  = features.width;
715466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
716a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (height != NULL) {
717a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    *height = features.height;
718466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
719a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
720a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  return 1;
721466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora}
7229aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
723a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
724466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Advance decoding API
7259aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
726a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroraint WebPInitDecoderConfigInternal(WebPDecoderConfig* config,
727466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora                                  int version) {
728a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
729466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    return 0;   // version mismatch
730466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
731466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (config == NULL) {
732466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    return 0;
733466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
734466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  memset(config, 0, sizeof(*config));
735466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  DefaultFeatures(&config->input);
736466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPInitDecBuffer(&config->output);
737466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  return 1;
738466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora}
7399aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
740a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraVP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size,
741a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                      WebPBitstreamFeatures* features,
742a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                                      int version) {
743a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
744466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    return VP8_STATUS_INVALID_PARAM;   // version mismatch
745466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
746466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (features == NULL) {
747466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    return VP8_STATUS_INVALID_PARAM;
748466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
7491e7bf8805bd030c19924a5306837ecd72c295751Vikas Arora  return GetFeatures(data, data_size, features);
750466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora}
751466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
752a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraVP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,
753a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                         WebPDecoderConfig* config) {
754466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPDecParams params;
755466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  VP8StatusCode status;
7569aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
757a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (config == NULL) {
758466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    return VP8_STATUS_INVALID_PARAM;
7599aea642eefa7a641ab8b89d953251939221d2719Eric Hassold  }
760466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
761a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  status = GetFeatures(data, data_size, &config->input);
762466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  if (status != VP8_STATUS_OK) {
763a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
764a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return VP8_STATUS_BITSTREAM_ERROR;  // Not-enough-data treated as error.
765a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
766466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora    return status;
767466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  }
768466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
769466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  WebPResetDecParams(&params);
770466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  params.output = &config->output;
771466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  params.options = &config->options;
772466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  status = DecodeInto(data, data_size, &params);
773466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora
774466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora  return status;
7759aea642eefa7a641ab8b89d953251939221d2719Eric Hassold}
7769aea642eefa7a641ab8b89d953251939221d2719Eric Hassold
777a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
778a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Cropping and rescaling.
779a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
780a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroraint WebPIoInitFromOptions(const WebPDecoderOptions* const options,
781a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                          VP8Io* const io, WEBP_CSP_MODE src_colorspace) {
782a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  const int W = io->width;
783a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  const int H = io->height;
784a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  int x = 0, y = 0, w = W, h = H;
785a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
786a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Cropping
787a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  io->use_cropping = (options != NULL) && (options->use_cropping > 0);
788a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (io->use_cropping) {
789a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    w = options->crop_width;
790a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    h = options->crop_height;
791a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    x = options->crop_left;
792a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    y = options->crop_top;
79333f74dabbc7920a65ed435d7417987589febdc16Vikas Arora    if (!WebPIsRGBMode(src_colorspace)) {   // only snap for YUV420
794a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      x &= ~1;
79533f74dabbc7920a65ed435d7417987589febdc16Vikas Arora      y &= ~1;
796a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
797a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (x < 0 || y < 0 || w <= 0 || h <= 0 || x + w > W || y + h > H) {
798a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return 0;  // out of frame boundary error
799a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
800a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
801a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  io->crop_left   = x;
802a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  io->crop_top    = y;
803a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  io->crop_right  = x + w;
804a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  io->crop_bottom = y + h;
805a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  io->mb_w = w;
806a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  io->mb_h = h;
807a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
808a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Scaling
809a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  io->use_scaling = (options != NULL) && (options->use_scaling > 0);
810a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (io->use_scaling) {
811a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    if (options->scaled_width <= 0 || options->scaled_height <= 0) {
812a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora      return 0;
813a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    }
814a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    io->scaled_width = options->scaled_width;
815a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    io->scaled_height = options->scaled_height;
816a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
817a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
818a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Filter
819a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  io->bypass_filtering = options && options->bypass_filtering;
820a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
821a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  // Fancy upsampler
822a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#ifdef FANCY_UPSAMPLING
823a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling);
824a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#endif
825a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
826a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  if (io->use_scaling) {
827a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    // disable filter (only for large downscaling ratio).
828a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    io->bypass_filtering = (io->scaled_width < W * 3 / 4) &&
829a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora                           (io->scaled_height < H * 3 / 4);
830a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora    io->fancy_upsampling = 0;
831a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  }
832a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora  return 1;
833a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora}
834a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
835a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------
836a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora
837