webp.c revision a2415724fb3466168b2af5b08bd94ba732c0e753
1a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Copyright 2010 Google Inc. All Rights Reserved. 29aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 39aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// This code is licensed under the same terms as WebM: 49aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Software License Agreement: http://www.webmproject.org/license/software/ 59aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Additional IP Rights Grant: http://www.webmproject.org/license/additional/ 69aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// ----------------------------------------------------------------------------- 79aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 89aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Main decoding functions for WEBP images. 99aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 109aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// Author: Skal (pascal.massimino@gmail.com) 119aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 129aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#include <stdlib.h> 13a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 14a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#include "./vp8i.h" 15a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#include "./vp8li.h" 16a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#include "./webpi.h" 17a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#include "webp/format_constants.h" 189aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 199aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#if defined(__cplusplus) || defined(c_plusplus) 209aea642eefa7a641ab8b89d953251939221d2719Eric Hassoldextern "C" { 219aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#endif 229aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 23a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 249aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// RIFF layout is: 25a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Offset tag 269aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 0...3 "RIFF" 4-byte tag 279aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 4...7 size of image data (including metadata) starting at offset 8 289aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 8...11 "WEBP" our form-type signature 29a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// The RIFF container (12 bytes) is followed by appropriate chunks: 30a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 12..15 "VP8 ": 4-bytes tags, signaling the use of VP8 video format 319aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 16..19 size of the raw VP8 image data, starting at offset 20 329aea642eefa7a641ab8b89d953251939221d2719Eric Hassold// 20.... the VP8 bytes 33a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Or, 34a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 12..15 "VP8L": 4-bytes tags, signaling the use of VP8L lossless format 35a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 16..19 size of the raw VP8L image data, starting at offset 20 36a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 20.... the VP8L bytes 37a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Or, 38a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 12..15 "VP8X": 4-bytes tags, describing the extended-VP8 chunk. 39a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 16..19 size of the VP8X chunk starting at offset 20. 40a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 20..23 VP8X flags bit-map corresponding to the chunk-types present. 41a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 24..26 Width of the Canvas Image. 42a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 27..29 Height of the Canvas Image. 43a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// There can be extra chunks after the "VP8X" chunk (ICCP, TILE, FRM, VP8, 44a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// META ...) 45a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// All sizes are in little-endian order. 46a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note: chunk data size must be padded to multiple of 2 when written. 47a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 48a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE uint32_t get_le24(const uint8_t* const data) { 49a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return data[0] | (data[1] << 8) | (data[2] << 16); 50a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} 519aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 52a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE uint32_t get_le32(const uint8_t* const data) { 53a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return (uint32_t)get_le24(data) | (data[3] << 24); 549aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 559aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 56a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Validates the RIFF container (if detected) and skips over it. 57a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// If a RIFF container is detected, 58a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns VP8_STATUS_BITSTREAM_ERROR for invalid header, and 59a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// VP8_STATUS_OK otherwise. 60a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// In case there are not enough bytes (partial RIFF container), return 0 for 61a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// *riff_size. Else return the RIFF size extracted from the header. 62a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode ParseRIFF(const uint8_t** const data, 63a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t* const data_size, 64a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t* const riff_size) { 65a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(data != NULL); 66a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(data_size != NULL); 67a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(riff_size != NULL); 68a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 69a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *riff_size = 0; // Default: no RIFF present. 70a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (*data_size >= RIFF_HEADER_SIZE && !memcmp(*data, "RIFF", TAG_SIZE)) { 71a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (memcmp(*data + 8, "WEBP", TAG_SIZE)) { 72a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_BITSTREAM_ERROR; // Wrong image file signature. 739aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } else { 74a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint32_t size = get_le32(*data + TAG_SIZE); 75a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Check that we have at least one chunk (i.e "WEBP" + "VP8?nnnn"). 76a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (size < TAG_SIZE + CHUNK_HEADER_SIZE) { 77a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_BITSTREAM_ERROR; 789aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } 7903d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora // We have a RIFF container. Skip it. 80a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *riff_size = size; 81a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *data += RIFF_HEADER_SIZE; 82a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *data_size -= RIFF_HEADER_SIZE; 83a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 84a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 85a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_OK; 86a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} 87a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 88a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Validates the VP8X header and skips over it. 89a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns VP8_STATUS_BITSTREAM_ERROR for invalid VP8X header, 90a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and 91a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// VP8_STATUS_OK otherwise. 92a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// If a VP8X chunk is found, found_vp8x is set to true and *width_ptr, 93a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// *height_ptr and *flags_ptr are set to the corresponding values extracted 94a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// from the VP8X chunk. 95a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode ParseVP8X(const uint8_t** const data, 96a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t* const data_size, 97a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int* const found_vp8x, 98a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int* const width_ptr, int* const height_ptr, 99a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t* const flags_ptr) { 100a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE; 101a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(data != NULL); 102a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(data_size != NULL); 103a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(found_vp8x != NULL); 104a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 105a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *found_vp8x = 0; 106a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 107a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (*data_size < CHUNK_HEADER_SIZE) { 108a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data. 109a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 110a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 111a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (!memcmp(*data, "VP8X", TAG_SIZE)) { 112a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int width, height; 113a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t flags; 114a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint32_t chunk_size = get_le32(*data + TAG_SIZE); 115a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (chunk_size != VP8X_CHUNK_SIZE) { 116a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_BITSTREAM_ERROR; // Wrong chunk size. 117a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 118a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 119a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Verify if enough data is available to validate the VP8X chunk. 120a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (*data_size < vp8x_size) { 121a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data. 122a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 123a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora flags = get_le32(*data + 8); 124a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora width = 1 + get_le24(*data + 12); 125a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora height = 1 + get_le24(*data + 15); 126a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (width * (uint64_t)height >= MAX_IMAGE_AREA) { 127a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_BITSTREAM_ERROR; // image is too large 128a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 129a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 130a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (flags_ptr != NULL) *flags_ptr = flags; 131a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (width_ptr != NULL) *width_ptr = width; 132a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (height_ptr != NULL) *height_ptr = height; 133a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Skip over VP8X header bytes. 134a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *data += vp8x_size; 135a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *data_size -= vp8x_size; 136a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *found_vp8x = 1; 137a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 138a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_OK; 139a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} 140a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 141a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Skips to the next VP8/VP8L chunk header in the data given the size of the 142a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// RIFF chunk 'riff_size'. 143a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns VP8_STATUS_BITSTREAM_ERROR if any invalid chunk size is encountered, 144a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and 145a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// VP8_STATUS_OK otherwise. 146a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// If an alpha chunk is found, *alpha_data and *alpha_size are set 147a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// appropriately. 148a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode ParseOptionalChunks(const uint8_t** const data, 149a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t* const data_size, 150a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t const riff_size, 151a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t** const alpha_data, 152a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t* const alpha_size) { 153a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* buf; 154a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t buf_size; 155a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t total_size = TAG_SIZE + // "WEBP". 156a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora CHUNK_HEADER_SIZE + // "VP8Xnnnn". 157a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8X_CHUNK_SIZE; // data. 158a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(data != NULL); 159a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(data_size != NULL); 160a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora buf = *data; 161a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora buf_size = *data_size; 162a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 163a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(alpha_data != NULL); 164a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(alpha_size != NULL); 165a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *alpha_data = NULL; 166a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *alpha_size = 0; 167a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 168a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora while (1) { 169a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t chunk_size; 170a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t disk_chunk_size; // chunk_size with padding 171a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 172a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *data = buf; 173a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *data_size = buf_size; 174a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 175a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (buf_size < CHUNK_HEADER_SIZE) { // Insufficient data. 176a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_NOT_ENOUGH_DATA; 177a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 178a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 179a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora chunk_size = get_le32(buf + TAG_SIZE); 180a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // For odd-sized chunk-payload, there's one byte padding at the end. 181a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora disk_chunk_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1; 182a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora total_size += disk_chunk_size; 183a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 184a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Check that total bytes skipped so far does not exceed riff_size. 185a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (riff_size > 0 && (total_size > riff_size)) { 186a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size. 187a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 188a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 189a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (buf_size < disk_chunk_size) { // Insufficient data. 190a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_NOT_ENOUGH_DATA; 191a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 192a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 193a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (!memcmp(buf, "ALPH", TAG_SIZE)) { // A valid ALPH header. 194a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *alpha_data = buf + CHUNK_HEADER_SIZE; 195a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *alpha_size = chunk_size; 196a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } else if (!memcmp(buf, "VP8 ", TAG_SIZE) || 197a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora !memcmp(buf, "VP8L", TAG_SIZE)) { // A valid VP8/VP8L header. 198a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_OK; // Found. 199a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 200a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 201a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // We have a full and valid chunk; skip it. 202a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora buf += disk_chunk_size; 203a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora buf_size -= disk_chunk_size; 204a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 205a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} 206a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 207a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Validates the VP8/VP8L Header ("VP8 nnnn" or "VP8L nnnn") and skips over it. 208a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than 209a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// riff_size) VP8/VP8L header, 210a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and 211a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// VP8_STATUS_OK otherwise. 212a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// If a VP8/VP8L chunk is found, *chunk_size is set to the total number of bytes 213a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// extracted from the VP8/VP8L chunk header. 214a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// The flag '*is_lossless' is set to 1 in case of VP8L chunk / raw VP8L data. 215a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode ParseVP8Header(const uint8_t** const data_ptr, 216a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t* const data_size, 217a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t riff_size, 218a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t* const chunk_size, 219a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int* const is_lossless) { 220a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* const data = *data_ptr; 221a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const int is_vp8 = !memcmp(data, "VP8 ", TAG_SIZE); 222a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const int is_vp8l = !memcmp(data, "VP8L", TAG_SIZE); 223a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint32_t minimal_size = 224a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora TAG_SIZE + CHUNK_HEADER_SIZE; // "WEBP" + "VP8 nnnn" OR 225a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // "WEBP" + "VP8Lnnnn" 226a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(data != NULL); 227a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(data_size != NULL); 228a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(chunk_size != NULL); 229a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(is_lossless != NULL); 230a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 231a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (*data_size < CHUNK_HEADER_SIZE) { 232a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data. 233a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 234a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 235a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (is_vp8 || is_vp8l) { 236a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Bitstream contains VP8/VP8L header. 237a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint32_t size = get_le32(data + TAG_SIZE); 238a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if ((riff_size >= minimal_size) && (size > riff_size - minimal_size)) { 239a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_BITSTREAM_ERROR; // Inconsistent size information. 240a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 241a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Skip over CHUNK_HEADER_SIZE bytes from VP8/VP8L Header. 242a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *chunk_size = size; 243a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *data_ptr += CHUNK_HEADER_SIZE; 244a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *data_size -= CHUNK_HEADER_SIZE; 245a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *is_lossless = is_vp8l; 246a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } else { 247a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Raw VP8/VP8L bitstream (no header). 248a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *is_lossless = VP8LCheckSignature(data, *data_size); 249a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *chunk_size = *data_size; 250a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 251a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 252a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_OK; 253a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} 254a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 255a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 256a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 257a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Fetch '*width', '*height', '*has_alpha' and fill out 'headers' based on 258a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 'data'. All the output parameters may be NULL. If 'headers' is NULL only the 259a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// minimal amount will be read to fetch the remaining parameters. 260a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// If 'headers' is non-NULL this function will attempt to locate both alpha 261a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// data (with or without a VP8X chunk) and the bitstream chunk (VP8/VP8L). 262a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Note: The following chunk sequences (before the raw VP8/VP8L data) are 263a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// considered valid by this function: 264a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// RIFF + VP8(L) 265a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// RIFF + VP8X + (optional chunks) + VP8(L) 266a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose. 267a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// VP8(L) <-- Not a valid WebP format: only allowed for internal purpose. 268a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode ParseHeadersInternal(const uint8_t* data, 269a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t data_size, 270a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int* const width, 271a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int* const height, 272a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int* const has_alpha, 273a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPHeaderStructure* const headers) { 274a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int found_riff = 0; 275a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int found_vp8x = 0; 276a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8StatusCode status; 277a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPHeaderStructure hdrs; 278a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 279a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (data == NULL || data_size < RIFF_HEADER_SIZE) { 280a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_NOT_ENOUGH_DATA; 281a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 282a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora memset(&hdrs, 0, sizeof(hdrs)); 283a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora hdrs.data = data; 284a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora hdrs.data_size = data_size; 285a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 286a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Skip over RIFF header. 287a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora status = ParseRIFF(&data, &data_size, &hdrs.riff_size); 288a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (status != VP8_STATUS_OK) { 289a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return status; // Wrong RIFF header / insufficient data. 290a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 291a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora found_riff = (hdrs.riff_size > 0); 292a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 293a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Skip over VP8X. 294a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora { 295a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint32_t flags = 0; 296a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora status = ParseVP8X(&data, &data_size, &found_vp8x, width, height, &flags); 297a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (status != VP8_STATUS_OK) { 298a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return status; // Wrong VP8X / insufficient data. 299a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 300a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (!found_riff && found_vp8x) { 301a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Note: This restriction may be removed in the future, if it becomes 302a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // necessary to send VP8X chunk to the decoder. 303a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_BITSTREAM_ERROR; 304a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 305a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (has_alpha != NULL) *has_alpha = !!(flags & ALPHA_FLAG_BIT); 306a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (found_vp8x && headers == NULL) { 307a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_OK; // Return features from VP8X header. 308a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 309a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 310a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 311a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (data_size < TAG_SIZE) return VP8_STATUS_NOT_ENOUGH_DATA; 312a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 313a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Skip over optional chunks if data started with "RIFF + VP8X" or "ALPH". 314a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if ((found_riff && found_vp8x) || 315a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora (!found_riff && !found_vp8x && !memcmp(data, "ALPH", TAG_SIZE))) { 316a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora status = ParseOptionalChunks(&data, &data_size, hdrs.riff_size, 317a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora &hdrs.alpha_data, &hdrs.alpha_data_size); 318a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (status != VP8_STATUS_OK) { 319a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return status; // Found an invalid chunk size / insufficient data. 320a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 321a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 322a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 323a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Skip over VP8/VP8L header. 324a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora status = ParseVP8Header(&data, &data_size, hdrs.riff_size, 325a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora &hdrs.compressed_size, &hdrs.is_lossless); 326a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (status != VP8_STATUS_OK) { 327a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return status; // Wrong VP8/VP8L chunk-header / insufficient data. 328a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 329a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (hdrs.compressed_size > MAX_CHUNK_PAYLOAD) { 330a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_BITSTREAM_ERROR; 331a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 332a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 333a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (!hdrs.is_lossless) { 334a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (data_size < VP8_FRAME_HEADER_SIZE) { 335a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_NOT_ENOUGH_DATA; 336a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 337a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Validates raw VP8 data. 338a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (!VP8GetInfo(data, data_size, 339a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora (uint32_t)hdrs.compressed_size, width, height)) { 340a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_BITSTREAM_ERROR; 3419aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } 342a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } else { 343a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (data_size < VP8L_FRAME_HEADER_SIZE) { 344a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_NOT_ENOUGH_DATA; 345a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 346a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Validates raw VP8L data. 347a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (!VP8LGetInfo(data, data_size, width, height, has_alpha)) { 348a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_BITSTREAM_ERROR; 349a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 350a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 351a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 352a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (has_alpha != NULL) { 353a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // If the data did not contain a VP8X/VP8L chunk the only definitive way 354a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // to set this is by looking for alpha data (from an ALPH chunk). 355a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *has_alpha |= (hdrs.alpha_data != NULL); 356a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 357a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (headers != NULL) { 358a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *headers = hdrs; 359a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora headers->offset = data - headers->data; 360a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert((uint64_t)(data - headers->data) < MAX_CHUNK_PAYLOAD); 361a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(headers->offset == headers->data_size - data_size); 3629aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } 363a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_OK; // Return features from VP8 header. 364a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} 365a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 366a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraVP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) { 367a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(headers != NULL); 368a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // fill out headers, ignore width/height/has_alpha. 369a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return ParseHeadersInternal(headers->data, headers->data_size, 370a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora NULL, NULL, NULL, headers); 3719aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 3729aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 373a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 374466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// WebPDecParams 37503d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora 376466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Aroravoid WebPResetDecParams(WebPDecParams* const params) { 377466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora if (params) { 378466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora memset(params, 0, sizeof(*params)); 37903d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora } 38003d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora} 38103d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora 382a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 383466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// "Into" decoding variants 3849aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 385466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Main flow 386a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode DecodeInto(const uint8_t* const data, size_t data_size, 387466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPDecParams* const params) { 388a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8StatusCode status; 3899aea642eefa7a641ab8b89d953251939221d2719Eric Hassold VP8Io io; 390a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPHeaderStructure headers; 3919aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 392a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora headers.data = data; 393a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora headers.data_size = data_size; 394a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora status = WebPParseHeaders(&headers); // Process Pre-VP8 chunks. 395a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (status != VP8_STATUS_OK) { 396a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return status; 3979aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } 3989aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 399a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(params != NULL); 4009aea642eefa7a641ab8b89d953251939221d2719Eric Hassold VP8InitIo(&io); 401a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io.data = headers.data + headers.offset; 402a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io.data_size = headers.data_size - headers.offset; 403466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPInitCustomIo(params, &io); // Plug the I/O functions. 4049aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 405a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (!headers.is_lossless) { 406a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8Decoder* const dec = VP8New(); 407a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (dec == NULL) { 408a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_OUT_OF_MEMORY; 409a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 410a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#ifdef WEBP_USE_THREAD 411a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora dec->use_threads_ = params->options && (params->options->use_threads > 0); 412a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#else 413a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora dec->use_threads_ = 0; 414a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#endif 415a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora dec->alpha_data_ = headers.alpha_data; 416a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora dec->alpha_data_size_ = headers.alpha_data_size; 417a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 418a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Decode bitstream header, update io->width/io->height. 419a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (!VP8GetHeaders(dec, &io)) { 420a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora status = dec->status_; // An error occurred. Grab error status. 421a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } else { 422a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Allocate/check output buffers. 423a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora status = WebPAllocateDecBuffer(io.width, io.height, params->options, 424a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora params->output); 425a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (status == VP8_STATUS_OK) { // Decode 426a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (!VP8Decode(dec, &io)) { 427a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora status = dec->status_; 428a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 429a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 430a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 431a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8Delete(dec); 432466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora } else { 433a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8LDecoder* const dec = VP8LNew(); 434a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (dec == NULL) { 435a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_OUT_OF_MEMORY; 436a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 437a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (!VP8LDecodeHeader(dec, &io)) { 438a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora status = dec->status_; // An error occurred. Grab error status. 439a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } else { 440a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Allocate/check output buffers. 441a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora status = WebPAllocateDecBuffer(io.width, io.height, params->options, 442a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora params->output); 443a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (status == VP8_STATUS_OK) { // Decode 444a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (!VP8LDecodeImage(dec)) { 445a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora status = dec->status_; 446a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 447466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora } 448466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora } 449a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8LDelete(dec); 4509aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } 451a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 452466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora if (status != VP8_STATUS_OK) { 453466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPFreeDecBuffer(params->output); 454466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora } 455466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return status; 4569aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 4579aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 458466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Helpers 459466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arorastatic uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace, 460a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* const data, 461a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t data_size, 462a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* const rgba, 463a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int stride, size_t size) { 46403d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora WebPDecParams params; 465466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPDecBuffer buf; 466466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora if (rgba == NULL) { 4679aea642eefa7a641ab8b89d953251939221d2719Eric Hassold return NULL; 4689aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } 469466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPInitDecBuffer(&buf); 470466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPResetDecParams(¶ms); 471466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora params.output = &buf; 472466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora buf.colorspace = colorspace; 473466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora buf.u.RGBA.rgba = rgba; 474466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora buf.u.RGBA.stride = stride; 475466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora buf.u.RGBA.size = size; 476466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora buf.is_external_memory = 1; 477466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) { 478466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return NULL; 479466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora } 480466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return rgba; 481466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora} 4829aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 483a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size, 484a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* output, size_t size, int stride) { 485466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size); 4869aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 4879aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 488a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size, 489a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* output, size_t size, int stride) { 490466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size); 491466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora} 4929aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 493a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size, 494a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* output, size_t size, int stride) { 495466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size); 4969aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 4979aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 498a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size, 499a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* output, size_t size, int stride) { 500466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size); 5019aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 5029aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 503a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size, 504a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* output, size_t size, int stride) { 505466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size); 5069aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 5079aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 508a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeYUVInto(const uint8_t* data, size_t data_size, 509a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* luma, size_t luma_size, int luma_stride, 510a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* u, size_t u_size, int u_stride, 511a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora uint8_t* v, size_t v_size, int v_stride) { 51203d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora WebPDecParams params; 513466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPDecBuffer output; 514466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora if (luma == NULL) return NULL; 515466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPInitDecBuffer(&output); 516466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPResetDecParams(¶ms); 517466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora params.output = &output; 518466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora output.colorspace = MODE_YUV; 519466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora output.u.YUVA.y = luma; 520466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora output.u.YUVA.y_stride = luma_stride; 521466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora output.u.YUVA.y_size = luma_size; 522466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora output.u.YUVA.u = u; 523466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora output.u.YUVA.u_stride = u_stride; 524466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora output.u.YUVA.u_size = u_size; 525466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora output.u.YUVA.v = v; 526466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora output.u.YUVA.v_stride = v_stride; 527466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora output.u.YUVA.v_size = v_size; 528466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora output.is_external_memory = 1; 529466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) { 5309aea642eefa7a641ab8b89d953251939221d2719Eric Hassold return NULL; 5319aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } 532466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return luma; 5339aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 5349aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 535a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 5369aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 537a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* const data, 538a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t data_size, int* const width, int* const height, 539a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPDecBuffer* const keep_info) { 54003d5e34c70f174c16282b0efdc6bb9473df5f8f1Vikas Arora WebPDecParams params; 541466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPDecBuffer output; 542466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 543466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPInitDecBuffer(&output); 544466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPResetDecParams(¶ms); 545466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora params.output = &output; 546466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora output.colorspace = mode; 5479aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 548466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // Retrieve (and report back) the required dimensions from bitstream. 549466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora if (!WebPGetInfo(data, data_size, &output.width, &output.height)) { 5509aea642eefa7a641ab8b89d953251939221d2719Eric Hassold return NULL; 5519aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } 552a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (width != NULL) *width = output.width; 553a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (height != NULL) *height = output.height; 5549aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 555466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // Decode 556466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) { 557466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return NULL; 5589aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } 559a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (keep_info != NULL) { // keep track of the side-info 560466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPCopyDecBuffer(&output, keep_info); 5619aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } 562466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora // return decoded samples (don't clear 'output'!) 563a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y; 5649aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 5659aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 566a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size, 567466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* width, int* height) { 5689aea642eefa7a641ab8b89d953251939221d2719Eric Hassold return Decode(MODE_RGB, data, data_size, width, height, NULL); 5699aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 5709aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 571a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size, 572466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* width, int* height) { 5739aea642eefa7a641ab8b89d953251939221d2719Eric Hassold return Decode(MODE_RGBA, data, data_size, width, height, NULL); 5749aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 5759aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 576a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size, 577466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* width, int* height) { 578466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return Decode(MODE_ARGB, data, data_size, width, height, NULL); 579466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora} 580466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 581a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size, 582466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* width, int* height) { 5839aea642eefa7a641ab8b89d953251939221d2719Eric Hassold return Decode(MODE_BGR, data, data_size, width, height, NULL); 5849aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 5859aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 586a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size, 587466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* width, int* height) { 5889aea642eefa7a641ab8b89d953251939221d2719Eric Hassold return Decode(MODE_BGRA, data, data_size, width, height, NULL); 5899aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 5909aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 591a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorauint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size, 592466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* width, int* height, uint8_t** u, uint8_t** v, 593466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int* stride, int* uv_stride) { 594466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPDecBuffer output; // only to preserve the side-infos 5959aea642eefa7a641ab8b89d953251939221d2719Eric Hassold uint8_t* const out = Decode(MODE_YUV, data, data_size, 596466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora width, height, &output); 5979aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 598a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (out != NULL) { 599466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora const WebPYUVABuffer* const buf = &output.u.YUVA; 600466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora *u = buf->u; 601466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora *v = buf->v; 602466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora *stride = buf->y_stride; 603466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora *uv_stride = buf->u_stride; 604466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora assert(buf->u_stride == buf->v_stride); 6059aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } 6069aea642eefa7a641ab8b89d953251939221d2719Eric Hassold return out; 6079aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 6089aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 609466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arorastatic void DefaultFeatures(WebPBitstreamFeatures* const features) { 610a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora assert(features != NULL); 611466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora memset(features, 0, sizeof(*features)); 612466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora features->bitstream_version = 0; 613466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora} 614466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 615a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size, 616466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPBitstreamFeatures* const features) { 617a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (features == NULL || data == NULL) { 618466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return VP8_STATUS_INVALID_PARAM; 6199aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } 620466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora DefaultFeatures(features); 621a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 622a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Only parse enough of the data to retrieve width/height/has_alpha. 623a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return ParseHeadersInternal(data, data_size, 624a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora &features->width, &features->height, 625a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora &features->has_alpha, NULL); 626a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} 627a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 628a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 629a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// WebPGetInfo() 630a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 631a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroraint WebPGetInfo(const uint8_t* data, size_t data_size, 632a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int* width, int* height) { 633a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPBitstreamFeatures features; 634a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 635a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (GetFeatures(data, data_size, &features) != VP8_STATUS_OK) { 636a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return 0; 637466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora } 638a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 639a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (width != NULL) { 640a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *width = features.width; 641466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora } 642a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (height != NULL) { 643a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora *height = features.height; 644466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora } 645a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 646a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return 1; 647466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora} 6489aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 649a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 650466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora// Advance decoding API 6519aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 652a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroraint WebPInitDecoderConfigInternal(WebPDecoderConfig* config, 653466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora int version) { 654a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) { 655466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return 0; // version mismatch 656466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora } 657466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora if (config == NULL) { 658466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return 0; 659466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora } 660466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora memset(config, 0, sizeof(*config)); 661466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora DefaultFeatures(&config->input); 662466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPInitDecBuffer(&config->output); 663466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return 1; 664466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora} 6659aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 666a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraVP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size, 667a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPBitstreamFeatures* features, 668a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int version) { 669a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8StatusCode status; 670a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) { 671466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return VP8_STATUS_INVALID_PARAM; // version mismatch 672466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora } 673466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora if (features == NULL) { 674466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return VP8_STATUS_INVALID_PARAM; 675466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora } 676a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 677a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora status = GetFeatures(data, data_size, features); 678a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (status == VP8_STATUS_NOT_ENOUGH_DATA) { 679a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_BITSTREAM_ERROR; // Not-enough-data treated as error. 680a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 681a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return status; 682466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora} 683466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 684a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraVP8StatusCode WebPDecode(const uint8_t* data, size_t data_size, 685a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora WebPDecoderConfig* config) { 686466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPDecParams params; 687466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora VP8StatusCode status; 6889aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 689a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (config == NULL) { 690466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return VP8_STATUS_INVALID_PARAM; 6919aea642eefa7a641ab8b89d953251939221d2719Eric Hassold } 692466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 693a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora status = GetFeatures(data, data_size, &config->input); 694466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora if (status != VP8_STATUS_OK) { 695a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (status == VP8_STATUS_NOT_ENOUGH_DATA) { 696a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8_STATUS_BITSTREAM_ERROR; // Not-enough-data treated as error. 697a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 698466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return status; 699466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora } 700466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 701466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora WebPResetDecParams(¶ms); 702466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora params.output = &config->output; 703466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora params.options = &config->options; 704466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora status = DecodeInto(data, data_size, ¶ms); 705466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora 706466727975bcc57c0c5597bcd0747a2fe4777b303Vikas Arora return status; 7079aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} 7089aea642eefa7a641ab8b89d953251939221d2719Eric Hassold 709a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 710a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Cropping and rescaling. 711a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 712a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroraint WebPIoInitFromOptions(const WebPDecoderOptions* const options, 713a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8Io* const io, WEBP_CSP_MODE src_colorspace) { 714a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const int W = io->width; 715a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const int H = io->height; 716a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int x = 0, y = 0, w = W, h = H; 717a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 718a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Cropping 719a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->use_cropping = (options != NULL) && (options->use_cropping > 0); 720a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (io->use_cropping) { 721a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora w = options->crop_width; 722a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora h = options->crop_height; 723a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora x = options->crop_left; 724a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora y = options->crop_top; 725a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (!WebPIsRGBMode(src_colorspace)) { // only snap for YUV420 or YUV422 726a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora x &= ~1; 727a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora y &= ~1; // TODO(later): only for YUV420, not YUV422. 728a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 729a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (x < 0 || y < 0 || w <= 0 || h <= 0 || x + w > W || y + h > H) { 730a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return 0; // out of frame boundary error 731a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 732a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 733a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->crop_left = x; 734a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->crop_top = y; 735a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->crop_right = x + w; 736a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->crop_bottom = y + h; 737a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->mb_w = w; 738a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->mb_h = h; 739a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 740a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Scaling 741a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->use_scaling = (options != NULL) && (options->use_scaling > 0); 742a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (io->use_scaling) { 743a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (options->scaled_width <= 0 || options->scaled_height <= 0) { 744a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return 0; 745a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 746a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->scaled_width = options->scaled_width; 747a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->scaled_height = options->scaled_height; 748a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 749a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 750a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Filter 751a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->bypass_filtering = options && options->bypass_filtering; 752a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 753a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Fancy upsampler 754a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#ifdef FANCY_UPSAMPLING 755a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling); 756a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#endif 757a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 758a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora if (io->use_scaling) { 759a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // disable filter (only for large downscaling ratio). 760a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->bypass_filtering = (io->scaled_width < W * 3 / 4) && 761a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora (io->scaled_height < H * 3 / 4); 762a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora io->fancy_upsampling = 0; 763a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora } 764a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return 1; 765a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} 766a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 767a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 768a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 7699aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#if defined(__cplusplus) || defined(c_plusplus) 7709aea642eefa7a641ab8b89d953251939221d2719Eric Hassold} // extern "C" 7719aea642eefa7a641ab8b89d953251939221d2719Eric Hassold#endif 772