2012-01-06 23:49:06 +01:00
|
|
|
// Copyright 2010 Google Inc. All Rights Reserved.
|
2010-09-30 15:34:38 +02:00
|
|
|
//
|
2013-06-07 08:05:58 +02:00
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
2010-09-30 15:34:38 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Main decoding functions for WEBP images.
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2011-12-01 11:00:03 +01:00
|
|
|
|
|
|
|
#include "./vp8i.h"
|
2012-04-11 03:41:18 +02:00
|
|
|
#include "./vp8li.h"
|
2011-12-01 11:00:03 +01:00
|
|
|
#include "./webpi.h"
|
2015-11-24 07:53:27 +01:00
|
|
|
#include "../utils/utils.h"
|
2013-03-20 00:02:36 +01:00
|
|
|
#include "../webp/mux_types.h" // ALPHA_FLAG
|
2010-09-30 15:34:38 +02:00
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2010-09-30 15:34:38 +02:00
|
|
|
// RIFF layout is:
|
2011-09-16 21:07:55 +02:00
|
|
|
// Offset tag
|
2010-09-30 15:34:38 +02:00
|
|
|
// 0...3 "RIFF" 4-byte tag
|
|
|
|
// 4...7 size of image data (including metadata) starting at offset 8
|
|
|
|
// 8...11 "WEBP" our form-type signature
|
2011-08-18 20:07:13 +02:00
|
|
|
// The RIFF container (12 bytes) is followed by appropriate chunks:
|
2012-01-17 09:49:56 +01:00
|
|
|
// 12..15 "VP8 ": 4-bytes tags, signaling the use of VP8 video format
|
2010-09-30 15:34:38 +02:00
|
|
|
// 16..19 size of the raw VP8 image data, starting at offset 20
|
|
|
|
// 20.... the VP8 bytes
|
2011-08-18 20:07:13 +02:00
|
|
|
// Or,
|
2012-04-11 03:41:18 +02:00
|
|
|
// 12..15 "VP8L": 4-bytes tags, signaling the use of VP8L lossless format
|
|
|
|
// 16..19 size of the raw VP8L image data, starting at offset 20
|
|
|
|
// 20.... the VP8L bytes
|
|
|
|
// Or,
|
2011-08-18 20:07:13 +02:00
|
|
|
// 12..15 "VP8X": 4-bytes tags, describing the extended-VP8 chunk.
|
2011-09-13 11:35:10 +02:00
|
|
|
// 16..19 size of the VP8X chunk starting at offset 20.
|
2011-08-18 20:07:13 +02:00
|
|
|
// 20..23 VP8X flags bit-map corresponding to the chunk-types present.
|
2012-06-28 09:20:28 +02:00
|
|
|
// 24..26 Width of the Canvas Image.
|
|
|
|
// 27..29 Height of the Canvas Image.
|
2016-09-02 14:26:37 +02:00
|
|
|
// There can be extra chunks after the "VP8X" chunk (ICCP, ANMF, VP8, VP8L,
|
|
|
|
// XMP, EXIF ...)
|
2012-06-28 09:20:28 +02:00
|
|
|
// All sizes are in little-endian order.
|
|
|
|
// Note: chunk data size must be padded to multiple of 2 when written.
|
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
// Validates the RIFF container (if detected) and skips over it.
|
2014-02-11 01:35:27 +01:00
|
|
|
// If a RIFF container is detected, returns:
|
|
|
|
// VP8_STATUS_BITSTREAM_ERROR for invalid header,
|
|
|
|
// VP8_STATUS_NOT_ENOUGH_DATA for truncated data if have_all_data is true,
|
|
|
|
// and VP8_STATUS_OK otherwise.
|
2012-04-11 03:41:18 +02:00
|
|
|
// In case there are not enough bytes (partial RIFF container), return 0 for
|
2012-07-03 14:42:13 +02:00
|
|
|
// *riff_size. Else return the RIFF size extracted from the header.
|
2012-07-03 14:50:36 +02:00
|
|
|
static VP8StatusCode ParseRIFF(const uint8_t** const data,
|
2014-02-11 01:35:27 +01:00
|
|
|
size_t* const data_size, int have_all_data,
|
2012-07-03 14:50:36 +02:00
|
|
|
size_t* const riff_size) {
|
|
|
|
assert(data != NULL);
|
|
|
|
assert(data_size != NULL);
|
|
|
|
assert(riff_size != NULL);
|
2011-09-13 11:35:10 +02:00
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
*riff_size = 0; // Default: no RIFF present.
|
2012-01-17 09:49:56 +01:00
|
|
|
if (*data_size >= RIFF_HEADER_SIZE && !memcmp(*data, "RIFF", TAG_SIZE)) {
|
2011-09-13 11:35:10 +02:00
|
|
|
if (memcmp(*data + 8, "WEBP", TAG_SIZE)) {
|
|
|
|
return VP8_STATUS_BITSTREAM_ERROR; // Wrong image file signature.
|
2010-10-06 01:58:47 +02:00
|
|
|
} else {
|
2015-11-24 07:53:27 +01:00
|
|
|
const uint32_t size = GetLE32(*data + TAG_SIZE);
|
2011-09-13 11:35:10 +02:00
|
|
|
// Check that we have at least one chunk (i.e "WEBP" + "VP8?nnnn").
|
2012-04-11 03:41:18 +02:00
|
|
|
if (size < TAG_SIZE + CHUNK_HEADER_SIZE) {
|
2011-09-13 11:35:10 +02:00
|
|
|
return VP8_STATUS_BITSTREAM_ERROR;
|
2010-10-06 01:58:47 +02:00
|
|
|
}
|
2012-10-23 06:57:59 +02:00
|
|
|
if (size > MAX_CHUNK_PAYLOAD) {
|
|
|
|
return VP8_STATUS_BITSTREAM_ERROR;
|
|
|
|
}
|
2014-02-11 01:35:27 +01:00
|
|
|
if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {
|
|
|
|
return VP8_STATUS_NOT_ENOUGH_DATA; // Truncated bitstream.
|
|
|
|
}
|
2011-02-17 18:59:34 +01:00
|
|
|
// We have a RIFF container. Skip it.
|
2012-04-11 03:41:18 +02:00
|
|
|
*riff_size = size;
|
2011-09-13 11:35:10 +02:00
|
|
|
*data += RIFF_HEADER_SIZE;
|
|
|
|
*data_size -= RIFF_HEADER_SIZE;
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-13 11:35:10 +02:00
|
|
|
return VP8_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
// Validates the VP8X header and skips over it.
|
|
|
|
// Returns VP8_STATUS_BITSTREAM_ERROR for invalid VP8X header,
|
|
|
|
// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
|
|
|
|
// VP8_STATUS_OK otherwise.
|
2012-07-03 14:42:13 +02:00
|
|
|
// If a VP8X chunk is found, found_vp8x is set to true and *width_ptr,
|
|
|
|
// *height_ptr and *flags_ptr are set to the corresponding values extracted
|
|
|
|
// from the VP8X chunk.
|
|
|
|
static VP8StatusCode ParseVP8X(const uint8_t** const data,
|
|
|
|
size_t* const data_size,
|
|
|
|
int* const found_vp8x,
|
|
|
|
int* const width_ptr, int* const height_ptr,
|
|
|
|
uint32_t* const flags_ptr) {
|
2012-04-11 03:41:18 +02:00
|
|
|
const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
|
2012-07-03 14:42:13 +02:00
|
|
|
assert(data != NULL);
|
|
|
|
assert(data_size != NULL);
|
|
|
|
assert(found_vp8x != NULL);
|
2011-09-13 11:35:10 +02:00
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
*found_vp8x = 0;
|
2011-09-13 11:35:10 +02:00
|
|
|
|
2012-06-08 10:59:26 +02:00
|
|
|
if (*data_size < CHUNK_HEADER_SIZE) {
|
2011-09-13 11:35:10 +02:00
|
|
|
return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!memcmp(*data, "VP8X", TAG_SIZE)) {
|
2012-07-03 14:42:13 +02:00
|
|
|
int width, height;
|
|
|
|
uint32_t flags;
|
2015-11-24 07:53:27 +01:00
|
|
|
const uint32_t chunk_size = GetLE32(*data + TAG_SIZE);
|
2011-09-13 11:35:10 +02:00
|
|
|
if (chunk_size != VP8X_CHUNK_SIZE) {
|
|
|
|
return VP8_STATUS_BITSTREAM_ERROR; // Wrong chunk size.
|
|
|
|
}
|
2012-06-08 10:59:26 +02:00
|
|
|
|
|
|
|
// Verify if enough data is available to validate the VP8X chunk.
|
|
|
|
if (*data_size < vp8x_size) {
|
|
|
|
return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
|
|
|
|
}
|
2015-11-24 07:53:27 +01:00
|
|
|
flags = GetLE32(*data + 8);
|
|
|
|
width = 1 + GetLE24(*data + 12);
|
|
|
|
height = 1 + GetLE24(*data + 15);
|
2012-07-03 14:42:13 +02:00
|
|
|
if (width * (uint64_t)height >= MAX_IMAGE_AREA) {
|
|
|
|
return VP8_STATUS_BITSTREAM_ERROR; // image is too large
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
2012-07-03 14:42:13 +02:00
|
|
|
|
|
|
|
if (flags_ptr != NULL) *flags_ptr = flags;
|
|
|
|
if (width_ptr != NULL) *width_ptr = width;
|
|
|
|
if (height_ptr != NULL) *height_ptr = height;
|
2012-01-23 08:20:53 +01:00
|
|
|
// Skip over VP8X header bytes.
|
2012-04-11 03:41:18 +02:00
|
|
|
*data += vp8x_size;
|
|
|
|
*data_size -= vp8x_size;
|
|
|
|
*found_vp8x = 1;
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
|
|
|
return VP8_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-23 10:29:02 +02:00
|
|
|
// Skips to the next VP8/VP8L chunk header in the data given the size of the
|
|
|
|
// RIFF chunk 'riff_size'.
|
2012-04-11 03:41:18 +02:00
|
|
|
// Returns VP8_STATUS_BITSTREAM_ERROR if any invalid chunk size is encountered,
|
|
|
|
// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
|
|
|
|
// VP8_STATUS_OK otherwise.
|
2012-07-03 14:42:13 +02:00
|
|
|
// If an alpha chunk is found, *alpha_data and *alpha_size are set
|
|
|
|
// appropriately.
|
2012-07-03 14:50:36 +02:00
|
|
|
static VP8StatusCode ParseOptionalChunks(const uint8_t** const data,
|
|
|
|
size_t* const data_size,
|
|
|
|
size_t const riff_size,
|
|
|
|
const uint8_t** const alpha_data,
|
|
|
|
size_t* const alpha_size) {
|
2011-09-13 11:35:10 +02:00
|
|
|
const uint8_t* buf;
|
2012-04-18 01:01:33 +02:00
|
|
|
size_t buf_size;
|
2012-04-11 03:41:18 +02:00
|
|
|
uint32_t total_size = TAG_SIZE + // "WEBP".
|
|
|
|
CHUNK_HEADER_SIZE + // "VP8Xnnnn".
|
|
|
|
VP8X_CHUNK_SIZE; // data.
|
2012-07-03 14:50:36 +02:00
|
|
|
assert(data != NULL);
|
|
|
|
assert(data_size != NULL);
|
2011-09-13 11:35:10 +02:00
|
|
|
buf = *data;
|
|
|
|
buf_size = *data_size;
|
2012-04-11 03:41:18 +02:00
|
|
|
|
2012-07-03 14:50:36 +02:00
|
|
|
assert(alpha_data != NULL);
|
|
|
|
assert(alpha_size != NULL);
|
2011-12-01 04:27:36 +01:00
|
|
|
*alpha_data = NULL;
|
|
|
|
*alpha_size = 0;
|
2011-09-13 11:35:10 +02:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
uint32_t chunk_size;
|
2012-04-11 03:41:18 +02:00
|
|
|
uint32_t disk_chunk_size; // chunk_size with padding
|
|
|
|
|
2011-09-13 11:35:10 +02:00
|
|
|
*data = buf;
|
|
|
|
*data_size = buf_size;
|
|
|
|
|
|
|
|
if (buf_size < CHUNK_HEADER_SIZE) { // Insufficient data.
|
|
|
|
return VP8_STATUS_NOT_ENOUGH_DATA;
|
|
|
|
}
|
|
|
|
|
2015-11-24 07:53:27 +01:00
|
|
|
chunk_size = GetLE32(buf + TAG_SIZE);
|
2012-10-23 06:57:59 +02:00
|
|
|
if (chunk_size > MAX_CHUNK_PAYLOAD) {
|
|
|
|
return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.
|
|
|
|
}
|
2012-01-23 08:20:53 +01:00
|
|
|
// For odd-sized chunk-payload, there's one byte padding at the end.
|
2012-04-11 03:41:18 +02:00
|
|
|
disk_chunk_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1;
|
|
|
|
total_size += disk_chunk_size;
|
2011-09-13 11:35:10 +02:00
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
// Check that total bytes skipped so far does not exceed riff_size.
|
|
|
|
if (riff_size > 0 && (total_size > riff_size)) {
|
|
|
|
return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
|
|
|
|
2013-04-05 23:01:07 +02:00
|
|
|
// Start of a (possibly incomplete) VP8/VP8L chunk implies that we have
|
|
|
|
// parsed all the optional chunks.
|
|
|
|
// Note: This check must occur before the check 'buf_size < disk_chunk_size'
|
|
|
|
// below to allow incomplete VP8/VP8L chunks.
|
|
|
|
if (!memcmp(buf, "VP8 ", TAG_SIZE) ||
|
|
|
|
!memcmp(buf, "VP8L", TAG_SIZE)) {
|
|
|
|
return VP8_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
if (buf_size < disk_chunk_size) { // Insufficient data.
|
2011-09-13 11:35:10 +02:00
|
|
|
return VP8_STATUS_NOT_ENOUGH_DATA;
|
|
|
|
}
|
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
if (!memcmp(buf, "ALPH", TAG_SIZE)) { // A valid ALPH header.
|
2011-12-01 04:27:36 +01:00
|
|
|
*alpha_data = buf + CHUNK_HEADER_SIZE;
|
|
|
|
*alpha_size = chunk_size;
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
|
|
|
|
2012-01-23 08:20:53 +01:00
|
|
|
// We have a full and valid chunk; skip it.
|
2012-04-11 03:41:18 +02:00
|
|
|
buf += disk_chunk_size;
|
|
|
|
buf_size -= disk_chunk_size;
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-31 07:46:32 +02:00
|
|
|
// Validates the VP8/VP8L Header ("VP8 nnnn" or "VP8L nnnn") and skips over it.
|
2012-04-11 03:41:18 +02:00
|
|
|
// Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than
|
2012-05-31 07:46:32 +02:00
|
|
|
// riff_size) VP8/VP8L header,
|
2012-04-11 03:41:18 +02:00
|
|
|
// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
|
|
|
|
// VP8_STATUS_OK otherwise.
|
2012-07-03 14:42:13 +02:00
|
|
|
// If a VP8/VP8L chunk is found, *chunk_size is set to the total number of bytes
|
2012-04-11 03:41:18 +02:00
|
|
|
// extracted from the VP8/VP8L chunk header.
|
2012-07-03 14:42:13 +02:00
|
|
|
// The flag '*is_lossless' is set to 1 in case of VP8L chunk / raw VP8L data.
|
2012-07-03 14:50:36 +02:00
|
|
|
static VP8StatusCode ParseVP8Header(const uint8_t** const data_ptr,
|
2014-02-11 01:35:27 +01:00
|
|
|
size_t* const data_size, int have_all_data,
|
|
|
|
size_t riff_size, size_t* const chunk_size,
|
2012-07-03 14:50:36 +02:00
|
|
|
int* const is_lossless) {
|
2012-05-31 07:46:32 +02:00
|
|
|
const uint8_t* const data = *data_ptr;
|
|
|
|
const int is_vp8 = !memcmp(data, "VP8 ", TAG_SIZE);
|
|
|
|
const int is_vp8l = !memcmp(data, "VP8L", TAG_SIZE);
|
2012-04-11 03:41:18 +02:00
|
|
|
const uint32_t minimal_size =
|
2012-05-31 07:46:32 +02:00
|
|
|
TAG_SIZE + CHUNK_HEADER_SIZE; // "WEBP" + "VP8 nnnn" OR
|
|
|
|
// "WEBP" + "VP8Lnnnn"
|
2012-07-03 14:50:36 +02:00
|
|
|
assert(data != NULL);
|
|
|
|
assert(data_size != NULL);
|
|
|
|
assert(chunk_size != NULL);
|
|
|
|
assert(is_lossless != NULL);
|
2011-09-13 11:35:10 +02:00
|
|
|
|
|
|
|
if (*data_size < CHUNK_HEADER_SIZE) {
|
|
|
|
return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
|
|
|
|
}
|
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
if (is_vp8 || is_vp8l) {
|
2012-05-31 07:46:32 +02:00
|
|
|
// Bitstream contains VP8/VP8L header.
|
2015-11-24 07:53:27 +01:00
|
|
|
const uint32_t size = GetLE32(data + TAG_SIZE);
|
2012-04-11 03:41:18 +02:00
|
|
|
if ((riff_size >= minimal_size) && (size > riff_size - minimal_size)) {
|
2011-09-13 11:35:10 +02:00
|
|
|
return VP8_STATUS_BITSTREAM_ERROR; // Inconsistent size information.
|
|
|
|
}
|
2014-02-11 01:35:27 +01:00
|
|
|
if (have_all_data && (size > *data_size - CHUNK_HEADER_SIZE)) {
|
|
|
|
return VP8_STATUS_NOT_ENOUGH_DATA; // Truncated bitstream.
|
|
|
|
}
|
2012-05-23 10:29:02 +02:00
|
|
|
// Skip over CHUNK_HEADER_SIZE bytes from VP8/VP8L Header.
|
2012-04-11 03:41:18 +02:00
|
|
|
*chunk_size = size;
|
2012-05-31 07:46:32 +02:00
|
|
|
*data_ptr += CHUNK_HEADER_SIZE;
|
2012-04-11 03:41:18 +02:00
|
|
|
*data_size -= CHUNK_HEADER_SIZE;
|
|
|
|
*is_lossless = is_vp8l;
|
2012-05-31 07:46:32 +02:00
|
|
|
} else {
|
|
|
|
// Raw VP8/VP8L bitstream (no header).
|
|
|
|
*is_lossless = VP8LCheckSignature(data, *data_size);
|
|
|
|
*chunk_size = *data_size;
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
2012-05-31 07:46:32 +02:00
|
|
|
|
2011-09-13 11:35:10 +02:00
|
|
|
return VP8_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2012-07-03 14:42:13 +02:00
|
|
|
// Fetch '*width', '*height', '*has_alpha' and fill out 'headers' based on
|
|
|
|
// 'data'. All the output parameters may be NULL. If 'headers' is NULL only the
|
|
|
|
// minimal amount will be read to fetch the remaining parameters.
|
2012-06-08 23:50:45 +02:00
|
|
|
// If 'headers' is non-NULL this function will attempt to locate both alpha
|
|
|
|
// data (with or without a VP8X chunk) and the bitstream chunk (VP8/VP8L).
|
2012-06-29 11:01:32 +02:00
|
|
|
// Note: The following chunk sequences (before the raw VP8/VP8L data) are
|
|
|
|
// considered valid by this function:
|
|
|
|
// RIFF + VP8(L)
|
|
|
|
// RIFF + VP8X + (optional chunks) + VP8(L)
|
|
|
|
// ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.
|
|
|
|
// VP8(L) <-- Not a valid WebP format: only allowed for internal purpose.
|
2012-07-03 14:50:36 +02:00
|
|
|
static VP8StatusCode ParseHeadersInternal(const uint8_t* data,
|
|
|
|
size_t data_size,
|
|
|
|
int* const width,
|
|
|
|
int* const height,
|
|
|
|
int* const has_alpha,
|
2013-03-20 20:41:29 +01:00
|
|
|
int* const has_animation,
|
2013-08-21 23:56:35 +02:00
|
|
|
int* const format,
|
2012-07-03 14:50:36 +02:00
|
|
|
WebPHeaderStructure* const headers) {
|
2013-07-03 00:43:42 +02:00
|
|
|
int canvas_width = 0;
|
|
|
|
int canvas_height = 0;
|
|
|
|
int image_width = 0;
|
|
|
|
int image_height = 0;
|
2012-06-29 11:01:32 +02:00
|
|
|
int found_riff = 0;
|
2012-06-08 23:50:45 +02:00
|
|
|
int found_vp8x = 0;
|
2013-08-21 23:56:35 +02:00
|
|
|
int animation_present = 0;
|
2014-02-11 01:35:27 +01:00
|
|
|
const int have_all_data = (headers != NULL) ? headers->have_all_data : 0;
|
2013-08-21 23:56:35 +02:00
|
|
|
|
2012-06-08 23:50:45 +02:00
|
|
|
VP8StatusCode status;
|
|
|
|
WebPHeaderStructure hdrs;
|
2011-09-13 11:35:10 +02:00
|
|
|
|
2012-06-08 23:50:45 +02:00
|
|
|
if (data == NULL || data_size < RIFF_HEADER_SIZE) {
|
2011-09-13 11:35:10 +02:00
|
|
|
return VP8_STATUS_NOT_ENOUGH_DATA;
|
|
|
|
}
|
2012-06-08 23:50:45 +02:00
|
|
|
memset(&hdrs, 0, sizeof(hdrs));
|
|
|
|
hdrs.data = data;
|
|
|
|
hdrs.data_size = data_size;
|
2011-09-13 11:35:10 +02:00
|
|
|
|
|
|
|
// Skip over RIFF header.
|
2014-02-11 01:35:27 +01:00
|
|
|
status = ParseRIFF(&data, &data_size, have_all_data, &hdrs.riff_size);
|
2012-06-08 23:50:45 +02:00
|
|
|
if (status != VP8_STATUS_OK) {
|
|
|
|
return status; // Wrong RIFF header / insufficient data.
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
2012-06-29 11:01:32 +02:00
|
|
|
found_riff = (hdrs.riff_size > 0);
|
2011-09-13 11:35:10 +02:00
|
|
|
|
2012-06-08 23:50:45 +02:00
|
|
|
// Skip over VP8X.
|
|
|
|
{
|
|
|
|
uint32_t flags = 0;
|
2013-07-03 00:43:42 +02:00
|
|
|
status = ParseVP8X(&data, &data_size, &found_vp8x,
|
|
|
|
&canvas_width, &canvas_height, &flags);
|
2012-06-08 23:50:45 +02:00
|
|
|
if (status != VP8_STATUS_OK) {
|
|
|
|
return status; // Wrong VP8X / insufficient data.
|
|
|
|
}
|
2013-07-03 00:43:42 +02:00
|
|
|
animation_present = !!(flags & ANIMATION_FLAG);
|
2012-06-29 11:01:32 +02:00
|
|
|
if (!found_riff && found_vp8x) {
|
|
|
|
// Note: This restriction may be removed in the future, if it becomes
|
|
|
|
// necessary to send VP8X chunk to the decoder.
|
|
|
|
return VP8_STATUS_BITSTREAM_ERROR;
|
|
|
|
}
|
2012-11-19 20:21:37 +01:00
|
|
|
if (has_alpha != NULL) *has_alpha = !!(flags & ALPHA_FLAG);
|
2013-07-03 00:43:42 +02:00
|
|
|
if (has_animation != NULL) *has_animation = animation_present;
|
2013-08-21 23:56:35 +02:00
|
|
|
if (format != NULL) *format = 0; // default = undefined
|
2013-07-03 00:43:42 +02:00
|
|
|
|
2013-12-19 01:44:01 +01:00
|
|
|
image_width = canvas_width;
|
|
|
|
image_height = canvas_height;
|
2016-09-02 14:26:37 +02:00
|
|
|
if (found_vp8x && animation_present && headers == NULL) {
|
2013-12-19 01:44:01 +01:00
|
|
|
status = VP8_STATUS_OK;
|
|
|
|
goto ReturnWidthHeight; // Just return features from VP8X header.
|
2012-06-08 23:50:45 +02:00
|
|
|
}
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
2012-06-08 23:50:45 +02:00
|
|
|
|
2013-12-19 01:44:01 +01:00
|
|
|
if (data_size < TAG_SIZE) {
|
|
|
|
status = VP8_STATUS_NOT_ENOUGH_DATA;
|
|
|
|
goto ReturnWidthHeight;
|
|
|
|
}
|
2012-06-08 23:50:45 +02:00
|
|
|
|
2012-06-29 11:01:32 +02:00
|
|
|
// Skip over optional chunks if data started with "RIFF + VP8X" or "ALPH".
|
|
|
|
if ((found_riff && found_vp8x) ||
|
|
|
|
(!found_riff && !found_vp8x && !memcmp(data, "ALPH", TAG_SIZE))) {
|
2012-06-08 23:50:45 +02:00
|
|
|
status = ParseOptionalChunks(&data, &data_size, hdrs.riff_size,
|
|
|
|
&hdrs.alpha_data, &hdrs.alpha_data_size);
|
2011-09-13 11:35:10 +02:00
|
|
|
if (status != VP8_STATUS_OK) {
|
2013-12-19 01:44:01 +01:00
|
|
|
goto ReturnWidthHeight; // Invalid chunk size / insufficient data.
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 23:50:45 +02:00
|
|
|
// Skip over VP8/VP8L header.
|
2014-02-11 01:35:27 +01:00
|
|
|
status = ParseVP8Header(&data, &data_size, have_all_data, hdrs.riff_size,
|
2012-06-08 23:50:45 +02:00
|
|
|
&hdrs.compressed_size, &hdrs.is_lossless);
|
2011-09-13 11:35:10 +02:00
|
|
|
if (status != VP8_STATUS_OK) {
|
2013-12-19 01:44:01 +01:00
|
|
|
goto ReturnWidthHeight; // Wrong VP8/VP8L chunk-header / insufficient data.
|
2012-06-08 23:50:45 +02:00
|
|
|
}
|
|
|
|
if (hdrs.compressed_size > MAX_CHUNK_PAYLOAD) {
|
|
|
|
return VP8_STATUS_BITSTREAM_ERROR;
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
|
|
|
|
2016-09-02 14:26:37 +02:00
|
|
|
if (format != NULL && !animation_present) {
|
2013-08-21 23:56:35 +02:00
|
|
|
*format = hdrs.is_lossless ? 2 : 1;
|
|
|
|
}
|
|
|
|
|
2012-06-08 23:50:45 +02:00
|
|
|
if (!hdrs.is_lossless) {
|
|
|
|
if (data_size < VP8_FRAME_HEADER_SIZE) {
|
2013-12-19 01:44:01 +01:00
|
|
|
status = VP8_STATUS_NOT_ENOUGH_DATA;
|
|
|
|
goto ReturnWidthHeight;
|
2012-06-08 23:50:45 +02:00
|
|
|
}
|
|
|
|
// Validates raw VP8 data.
|
2013-07-03 00:43:42 +02:00
|
|
|
if (!VP8GetInfo(data, data_size, (uint32_t)hdrs.compressed_size,
|
|
|
|
&image_width, &image_height)) {
|
2012-06-08 23:50:45 +02:00
|
|
|
return VP8_STATUS_BITSTREAM_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (data_size < VP8L_FRAME_HEADER_SIZE) {
|
2013-12-19 01:44:01 +01:00
|
|
|
status = VP8_STATUS_NOT_ENOUGH_DATA;
|
|
|
|
goto ReturnWidthHeight;
|
2012-06-08 23:50:45 +02:00
|
|
|
}
|
|
|
|
// Validates raw VP8L data.
|
2013-07-03 00:43:42 +02:00
|
|
|
if (!VP8LGetInfo(data, data_size, &image_width, &image_height, has_alpha)) {
|
2012-06-08 23:50:45 +02:00
|
|
|
return VP8_STATUS_BITSTREAM_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2013-07-19 04:04:54 +02:00
|
|
|
// Validates image size coherency.
|
2013-07-03 00:43:42 +02:00
|
|
|
if (found_vp8x) {
|
|
|
|
if (canvas_width != image_width || canvas_height != image_height) {
|
|
|
|
return VP8_STATUS_BITSTREAM_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2012-06-08 23:50:45 +02:00
|
|
|
if (headers != NULL) {
|
|
|
|
*headers = hdrs;
|
|
|
|
headers->offset = data - headers->data;
|
|
|
|
assert((uint64_t)(data - headers->data) < MAX_CHUNK_PAYLOAD);
|
|
|
|
assert(headers->offset == headers->data_size - data_size);
|
|
|
|
}
|
2013-12-19 01:44:01 +01:00
|
|
|
ReturnWidthHeight:
|
|
|
|
if (status == VP8_STATUS_OK ||
|
|
|
|
(status == VP8_STATUS_NOT_ENOUGH_DATA && found_vp8x && headers == NULL)) {
|
|
|
|
if (has_alpha != NULL) {
|
|
|
|
// If the data did not contain a VP8X/VP8L chunk the only definitive way
|
|
|
|
// to set this is by looking for alpha data (from an ALPH chunk).
|
|
|
|
*has_alpha |= (hdrs.alpha_data != NULL);
|
|
|
|
}
|
|
|
|
if (width != NULL) *width = image_width;
|
|
|
|
if (height != NULL) *height = image_height;
|
|
|
|
return VP8_STATUS_OK;
|
|
|
|
} else {
|
|
|
|
return status;
|
|
|
|
}
|
2012-06-08 23:50:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) {
|
2016-05-03 05:22:05 +02:00
|
|
|
// status is marked volatile as a workaround for a clang-3.8 (aarch64) bug
|
|
|
|
volatile VP8StatusCode status;
|
2013-03-20 20:59:29 +01:00
|
|
|
int has_animation = 0;
|
2012-06-08 23:50:45 +02:00
|
|
|
assert(headers != NULL);
|
2013-03-20 20:59:29 +01:00
|
|
|
// fill out headers, ignore width/height/has_alpha.
|
|
|
|
status = ParseHeadersInternal(headers->data, headers->data_size,
|
2013-08-21 23:56:35 +02:00
|
|
|
NULL, NULL, NULL, &has_animation,
|
|
|
|
NULL, headers);
|
2013-03-20 20:59:29 +01:00
|
|
|
if (status == VP8_STATUS_OK || status == VP8_STATUS_NOT_ENOUGH_DATA) {
|
|
|
|
// TODO(jzern): full support of animation frames will require API additions.
|
|
|
|
if (has_animation) {
|
|
|
|
status = VP8_STATUS_UNSUPPORTED_FEATURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-06-20 09:45:15 +02:00
|
|
|
// WebPDecParams
|
2011-03-09 04:23:16 +01:00
|
|
|
|
2011-05-20 04:07:01 +02:00
|
|
|
void WebPResetDecParams(WebPDecParams* const params) {
|
2013-04-11 09:57:31 +02:00
|
|
|
if (params != NULL) {
|
2011-06-20 09:45:15 +02:00
|
|
|
memset(params, 0, sizeof(*params));
|
2011-03-24 02:03:53 +01:00
|
|
|
}
|
2011-03-13 09:22:57 +01:00
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-06-20 09:45:15 +02:00
|
|
|
// "Into" decoding variants
|
2010-09-30 15:34:38 +02:00
|
|
|
|
2011-06-20 09:45:15 +02:00
|
|
|
// Main flow
|
2012-07-03 14:50:36 +02:00
|
|
|
static VP8StatusCode DecodeInto(const uint8_t* const data, size_t data_size,
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPDecParams* const params) {
|
2012-04-05 11:31:10 +02:00
|
|
|
VP8StatusCode status;
|
2010-10-06 01:58:47 +02:00
|
|
|
VP8Io io;
|
2012-04-05 11:31:10 +02:00
|
|
|
WebPHeaderStructure headers;
|
2010-10-06 01:58:47 +02:00
|
|
|
|
2012-04-05 11:31:10 +02:00
|
|
|
headers.data = data;
|
|
|
|
headers.data_size = data_size;
|
2014-02-11 01:35:27 +01:00
|
|
|
headers.have_all_data = 1;
|
2012-04-05 11:31:10 +02:00
|
|
|
status = WebPParseHeaders(&headers); // Process Pre-VP8 chunks.
|
|
|
|
if (status != VP8_STATUS_OK) {
|
|
|
|
return status;
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
|
2012-07-03 14:50:36 +02:00
|
|
|
assert(params != NULL);
|
2010-09-30 15:34:38 +02:00
|
|
|
VP8InitIo(&io);
|
2012-04-05 11:31:10 +02:00
|
|
|
io.data = headers.data + headers.offset;
|
|
|
|
io.data_size = headers.data_size - headers.offset;
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPInitCustomIo(params, &io); // Plug the I/O functions.
|
2010-09-30 15:34:38 +02:00
|
|
|
|
2012-04-05 11:31:10 +02:00
|
|
|
if (!headers.is_lossless) {
|
|
|
|
VP8Decoder* const dec = VP8New();
|
|
|
|
if (dec == NULL) {
|
|
|
|
return VP8_STATUS_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
dec->alpha_data_ = headers.alpha_data;
|
|
|
|
dec->alpha_data_size_ = headers.alpha_data_size;
|
2011-07-22 22:09:10 +02:00
|
|
|
|
2012-04-05 11:31:10 +02:00
|
|
|
// Decode bitstream header, update io->width/io->height.
|
|
|
|
if (!VP8GetHeaders(dec, &io)) {
|
|
|
|
status = dec->status_; // An error occurred. Grab error status.
|
|
|
|
} else {
|
|
|
|
// Allocate/check output buffers.
|
|
|
|
status = WebPAllocateDecBuffer(io.width, io.height, params->options,
|
|
|
|
params->output);
|
|
|
|
if (status == VP8_STATUS_OK) { // Decode
|
2013-10-15 23:58:31 +02:00
|
|
|
// This change must be done before calling VP8Decode()
|
|
|
|
dec->mt_method_ = VP8GetThreadMethod(params->options, &headers,
|
|
|
|
io.width, io.height);
|
2013-11-26 22:59:02 +01:00
|
|
|
VP8InitDithering(params->options, dec);
|
2012-04-11 03:41:18 +02:00
|
|
|
if (!VP8Decode(dec, &io)) {
|
|
|
|
status = dec->status_;
|
|
|
|
}
|
2012-04-05 11:31:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
VP8Delete(dec);
|
|
|
|
} else {
|
|
|
|
VP8LDecoder* const dec = VP8LNew();
|
|
|
|
if (dec == NULL) {
|
|
|
|
return VP8_STATUS_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
if (!VP8LDecodeHeader(dec, &io)) {
|
|
|
|
status = dec->status_; // An error occurred. Grab error status.
|
|
|
|
} else {
|
|
|
|
// Allocate/check output buffers.
|
|
|
|
status = WebPAllocateDecBuffer(io.width, io.height, params->options,
|
|
|
|
params->output);
|
|
|
|
if (status == VP8_STATUS_OK) { // Decode
|
|
|
|
if (!VP8LDecodeImage(dec)) {
|
|
|
|
status = dec->status_;
|
2012-04-11 03:41:18 +02:00
|
|
|
}
|
2011-06-20 09:45:15 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-05 11:31:10 +02:00
|
|
|
VP8LDelete(dec);
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
2012-04-05 11:31:10 +02:00
|
|
|
|
2011-06-20 09:45:15 +02:00
|
|
|
if (status != VP8_STATUS_OK) {
|
|
|
|
WebPFreeDecBuffer(params->output);
|
2016-02-18 14:58:18 +01:00
|
|
|
} else {
|
2016-02-18 15:14:41 +01:00
|
|
|
if (params->options != NULL && params->options->flip) {
|
add extra meaning to WebPDecBuffer::is_external_memory
If value is '2', it means the buffer is a 'slow' one, like GPU-mapped memory.
This change is backward compatible (setting is_external_memory to 2
will be a no-op in previous libraries)
dwebp: add flags to force a particular colorspace format
new flags is:
-pixel_format {RGB,RGBA,BGR,BGRA,ARGB,RGBA_4444,RGB_565,
rgbA,bgrA,Argb,rgbA_4444,YUV,YUVA}
and also,external_memory {0,1,2}
These flags are mostly for debuggging purpose, and hence are not documented.
Change-Id: Iac88ce1e10b35163dd7af57f9660f062f5d8ed5e
2016-03-09 08:58:58 +01:00
|
|
|
// This restores the original stride values if options->flip was used
|
|
|
|
// during the call to WebPAllocateDecBuffer above.
|
2016-02-18 15:14:41 +01:00
|
|
|
status = WebPFlipBuffer(params->output);
|
|
|
|
}
|
2016-02-18 14:58:18 +01:00
|
|
|
}
|
2011-06-20 09:45:15 +02:00
|
|
|
return status;
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
|
2011-06-20 09:45:15 +02:00
|
|
|
// Helpers
|
|
|
|
static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace,
|
2012-07-03 14:50:36 +02:00
|
|
|
const uint8_t* const data,
|
|
|
|
size_t data_size,
|
|
|
|
uint8_t* const rgba,
|
|
|
|
int stride, size_t size) {
|
2011-03-09 04:23:16 +01:00
|
|
|
WebPDecParams params;
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPDecBuffer buf;
|
|
|
|
if (rgba == NULL) {
|
2010-09-30 15:34:38 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPInitDecBuffer(&buf);
|
2011-05-20 04:07:01 +02:00
|
|
|
WebPResetDecParams(¶ms);
|
2011-06-20 09:45:15 +02:00
|
|
|
params.output = &buf;
|
|
|
|
buf.colorspace = colorspace;
|
|
|
|
buf.u.RGBA.rgba = rgba;
|
|
|
|
buf.u.RGBA.stride = stride;
|
|
|
|
buf.u.RGBA.size = size;
|
|
|
|
buf.is_external_memory = 1;
|
|
|
|
if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) {
|
2010-09-30 15:34:38 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-06-20 09:45:15 +02:00
|
|
|
return rgba;
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size,
|
|
|
|
uint8_t* output, size_t size, int stride) {
|
2011-06-20 09:45:15 +02:00
|
|
|
return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size);
|
|
|
|
}
|
2010-10-06 01:58:47 +02:00
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size,
|
|
|
|
uint8_t* output, size_t size, int stride) {
|
2011-06-20 09:45:15 +02:00
|
|
|
return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size);
|
|
|
|
}
|
2010-10-06 01:58:47 +02:00
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size,
|
|
|
|
uint8_t* output, size_t size, int stride) {
|
2011-07-14 20:41:23 +02:00
|
|
|
return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size);
|
|
|
|
}
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size,
|
|
|
|
uint8_t* output, size_t size, int stride) {
|
2011-06-20 09:45:15 +02:00
|
|
|
return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size);
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size,
|
|
|
|
uint8_t* output, size_t size, int stride) {
|
2011-06-20 09:45:15 +02:00
|
|
|
return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size);
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
uint8_t* WebPDecodeYUVInto(const uint8_t* data, size_t data_size,
|
|
|
|
uint8_t* luma, size_t luma_size, int luma_stride,
|
|
|
|
uint8_t* u, size_t u_size, int u_stride,
|
|
|
|
uint8_t* v, size_t v_size, int v_stride) {
|
2011-03-09 04:23:16 +01:00
|
|
|
WebPDecParams params;
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPDecBuffer output;
|
|
|
|
if (luma == NULL) return NULL;
|
|
|
|
WebPInitDecBuffer(&output);
|
|
|
|
WebPResetDecParams(¶ms);
|
|
|
|
params.output = &output;
|
|
|
|
output.colorspace = MODE_YUV;
|
|
|
|
output.u.YUVA.y = luma;
|
|
|
|
output.u.YUVA.y_stride = luma_stride;
|
|
|
|
output.u.YUVA.y_size = luma_size;
|
|
|
|
output.u.YUVA.u = u;
|
|
|
|
output.u.YUVA.u_stride = u_stride;
|
|
|
|
output.u.YUVA.u_size = u_size;
|
|
|
|
output.u.YUVA.v = v;
|
|
|
|
output.u.YUVA.v_stride = v_stride;
|
|
|
|
output.u.YUVA.v_size = v_size;
|
|
|
|
output.is_external_memory = 1;
|
|
|
|
if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) {
|
2010-09-30 15:34:38 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-06-20 09:45:15 +02:00
|
|
|
return luma;
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2010-09-30 15:34:38 +02:00
|
|
|
|
2012-07-03 14:50:36 +02:00
|
|
|
static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* const data,
|
|
|
|
size_t data_size, int* const width, int* const height,
|
|
|
|
WebPDecBuffer* const keep_info) {
|
2011-03-25 23:04:11 +01:00
|
|
|
WebPDecParams params;
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPDecBuffer output;
|
2010-10-06 01:58:47 +02:00
|
|
|
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPInitDecBuffer(&output);
|
2011-05-20 04:07:01 +02:00
|
|
|
WebPResetDecParams(¶ms);
|
2011-06-20 09:45:15 +02:00
|
|
|
params.output = &output;
|
|
|
|
output.colorspace = mode;
|
|
|
|
|
|
|
|
// Retrieve (and report back) the required dimensions from bitstream.
|
|
|
|
if (!WebPGetInfo(data, data_size, &output.width, &output.height)) {
|
2010-09-30 15:34:38 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-01-23 08:20:53 +01:00
|
|
|
if (width != NULL) *width = output.width;
|
|
|
|
if (height != NULL) *height = output.height;
|
2011-03-09 04:23:16 +01:00
|
|
|
|
2011-06-20 09:45:15 +02:00
|
|
|
// Decode
|
|
|
|
if (DecodeInto(data, data_size, ¶ms) != VP8_STATUS_OK) {
|
|
|
|
return NULL;
|
2011-03-13 09:22:57 +01:00
|
|
|
}
|
2012-01-23 08:20:53 +01:00
|
|
|
if (keep_info != NULL) { // keep track of the side-info
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPCopyDecBuffer(&output, keep_info);
|
2011-03-14 18:29:02 +01:00
|
|
|
}
|
2011-06-20 09:45:15 +02:00
|
|
|
// return decoded samples (don't clear 'output'!)
|
2012-06-04 16:40:32 +02:00
|
|
|
return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y;
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size,
|
2011-06-20 09:45:15 +02:00
|
|
|
int* width, int* height) {
|
2010-09-30 15:34:38 +02:00
|
|
|
return Decode(MODE_RGB, data, data_size, width, height, NULL);
|
|
|
|
}
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size,
|
2011-06-20 09:45:15 +02:00
|
|
|
int* width, int* height) {
|
2010-09-30 15:34:38 +02:00
|
|
|
return Decode(MODE_RGBA, data, data_size, width, height, NULL);
|
|
|
|
}
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size,
|
2011-07-13 18:08:58 +02:00
|
|
|
int* width, int* height) {
|
|
|
|
return Decode(MODE_ARGB, data, data_size, width, height, NULL);
|
|
|
|
}
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size,
|
2011-06-20 09:45:15 +02:00
|
|
|
int* width, int* height) {
|
2010-09-30 15:34:38 +02:00
|
|
|
return Decode(MODE_BGR, data, data_size, width, height, NULL);
|
|
|
|
}
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size,
|
2011-06-20 09:45:15 +02:00
|
|
|
int* width, int* height) {
|
2010-09-30 15:34:38 +02:00
|
|
|
return Decode(MODE_BGRA, data, data_size, width, height, NULL);
|
|
|
|
}
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size,
|
2011-06-20 09:45:15 +02:00
|
|
|
int* width, int* height, uint8_t** u, uint8_t** v,
|
|
|
|
int* stride, int* uv_stride) {
|
|
|
|
WebPDecBuffer output; // only to preserve the side-infos
|
2010-09-30 15:34:38 +02:00
|
|
|
uint8_t* const out = Decode(MODE_YUV, data, data_size,
|
2011-06-20 09:45:15 +02:00
|
|
|
width, height, &output);
|
2010-09-30 15:34:38 +02:00
|
|
|
|
2012-01-23 08:20:53 +01:00
|
|
|
if (out != NULL) {
|
2011-06-20 09:45:15 +02:00
|
|
|
const WebPYUVABuffer* const buf = &output.u.YUVA;
|
|
|
|
*u = buf->u;
|
|
|
|
*v = buf->v;
|
|
|
|
*stride = buf->y_stride;
|
|
|
|
*uv_stride = buf->u_stride;
|
|
|
|
assert(buf->u_stride == buf->v_stride);
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2011-06-20 09:45:15 +02:00
|
|
|
static void DefaultFeatures(WebPBitstreamFeatures* const features) {
|
2012-07-03 14:50:36 +02:00
|
|
|
assert(features != NULL);
|
2011-06-20 09:45:15 +02:00
|
|
|
memset(features, 0, sizeof(*features));
|
|
|
|
}
|
|
|
|
|
2012-07-03 14:50:36 +02:00
|
|
|
static VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size,
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPBitstreamFeatures* const features) {
|
2012-01-17 09:49:56 +01:00
|
|
|
if (features == NULL || data == NULL) {
|
2011-06-20 09:45:15 +02:00
|
|
|
return VP8_STATUS_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
DefaultFeatures(features);
|
2011-08-18 20:07:13 +02:00
|
|
|
|
2013-03-20 20:41:29 +01:00
|
|
|
// Only parse enough of the data to retrieve the features.
|
2012-06-08 23:50:45 +02:00
|
|
|
return ParseHeadersInternal(data, data_size,
|
|
|
|
&features->width, &features->height,
|
2013-03-20 20:41:29 +01:00
|
|
|
&features->has_alpha, &features->has_animation,
|
2013-08-21 23:56:35 +02:00
|
|
|
&features->format, NULL);
|
2011-06-20 09:45:15 +02:00
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-08-18 20:07:13 +02:00
|
|
|
// WebPGetInfo()
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
int WebPGetInfo(const uint8_t* data, size_t data_size,
|
2011-08-18 20:07:13 +02:00
|
|
|
int* width, int* height) {
|
|
|
|
WebPBitstreamFeatures features;
|
|
|
|
|
2011-09-13 11:35:10 +02:00
|
|
|
if (GetFeatures(data, data_size, &features) != VP8_STATUS_OK) {
|
2011-08-18 20:07:13 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-23 08:20:53 +01:00
|
|
|
if (width != NULL) {
|
2011-08-18 20:07:13 +02:00
|
|
|
*width = features.width;
|
|
|
|
}
|
2012-01-23 08:20:53 +01:00
|
|
|
if (height != NULL) {
|
2011-08-18 20:07:13 +02:00
|
|
|
*height = features.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-06-20 09:45:15 +02:00
|
|
|
// Advance decoding API
|
|
|
|
|
2012-07-18 00:01:30 +02:00
|
|
|
int WebPInitDecoderConfigInternal(WebPDecoderConfig* config,
|
2011-06-20 09:45:15 +02:00
|
|
|
int version) {
|
2012-07-18 20:53:25 +02:00
|
|
|
if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
|
2011-06-20 09:45:15 +02:00
|
|
|
return 0; // version mismatch
|
|
|
|
}
|
|
|
|
if (config == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memset(config, 0, sizeof(*config));
|
|
|
|
DefaultFeatures(&config->input);
|
|
|
|
WebPInitDecBuffer(&config->output);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size,
|
2012-07-18 00:01:30 +02:00
|
|
|
WebPBitstreamFeatures* features,
|
2011-09-13 11:35:10 +02:00
|
|
|
int version) {
|
2012-07-18 20:53:25 +02:00
|
|
|
if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
|
2011-06-20 09:45:15 +02:00
|
|
|
return VP8_STATUS_INVALID_PARAM; // version mismatch
|
|
|
|
}
|
|
|
|
if (features == NULL) {
|
|
|
|
return VP8_STATUS_INVALID_PARAM;
|
|
|
|
}
|
2013-02-28 00:27:43 +01:00
|
|
|
return GetFeatures(data, data_size, features);
|
2011-06-20 09:45:15 +02:00
|
|
|
}
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,
|
2012-07-18 00:01:30 +02:00
|
|
|
WebPDecoderConfig* config) {
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPDecParams params;
|
|
|
|
VP8StatusCode status;
|
|
|
|
|
2012-01-23 08:20:53 +01:00
|
|
|
if (config == NULL) {
|
2011-06-20 09:45:15 +02:00
|
|
|
return VP8_STATUS_INVALID_PARAM;
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
2011-06-20 09:45:15 +02:00
|
|
|
|
2011-09-13 11:35:10 +02:00
|
|
|
status = GetFeatures(data, data_size, &config->input);
|
2011-06-20 09:45:15 +02:00
|
|
|
if (status != VP8_STATUS_OK) {
|
2011-09-13 11:35:10 +02:00
|
|
|
if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
|
2012-01-23 08:20:53 +01:00
|
|
|
return VP8_STATUS_BITSTREAM_ERROR; // Not-enough-data treated as error.
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
2011-06-20 09:45:15 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
WebPResetDecParams(¶ms);
|
|
|
|
params.options = &config->options;
|
add extra meaning to WebPDecBuffer::is_external_memory
If value is '2', it means the buffer is a 'slow' one, like GPU-mapped memory.
This change is backward compatible (setting is_external_memory to 2
will be a no-op in previous libraries)
dwebp: add flags to force a particular colorspace format
new flags is:
-pixel_format {RGB,RGBA,BGR,BGRA,ARGB,RGBA_4444,RGB_565,
rgbA,bgrA,Argb,rgbA_4444,YUV,YUVA}
and also,external_memory {0,1,2}
These flags are mostly for debuggging purpose, and hence are not documented.
Change-Id: Iac88ce1e10b35163dd7af57f9660f062f5d8ed5e
2016-03-09 08:58:58 +01:00
|
|
|
params.output = &config->output;
|
|
|
|
if (WebPAvoidSlowMemory(params.output, &config->input)) {
|
|
|
|
// decoding to slow memory: use a temporary in-mem buffer to decode into.
|
|
|
|
WebPDecBuffer in_mem_buffer;
|
|
|
|
WebPInitDecBuffer(&in_mem_buffer);
|
|
|
|
in_mem_buffer.colorspace = config->output.colorspace;
|
|
|
|
in_mem_buffer.width = config->input.width;
|
|
|
|
in_mem_buffer.height = config->input.height;
|
|
|
|
params.output = &in_mem_buffer;
|
|
|
|
status = DecodeInto(data, data_size, ¶ms);
|
|
|
|
if (status == VP8_STATUS_OK) { // do the slow-copy
|
|
|
|
status = WebPCopyDecBufferPixels(&in_mem_buffer, &config->output);
|
|
|
|
}
|
|
|
|
WebPFreeDecBuffer(&in_mem_buffer);
|
|
|
|
} else {
|
|
|
|
status = DecodeInto(data, data_size, ¶ms);
|
|
|
|
}
|
2011-06-20 09:45:15 +02:00
|
|
|
|
|
|
|
return status;
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
|
2012-03-20 11:59:22 +01:00
|
|
|
//------------------------------------------------------------------------------
|
2012-03-22 11:30:20 +01:00
|
|
|
// Cropping and rescaling.
|
2012-03-20 11:59:22 +01:00
|
|
|
|
|
|
|
int WebPIoInitFromOptions(const WebPDecoderOptions* const options,
|
2012-03-27 14:24:56 +02:00
|
|
|
VP8Io* const io, WEBP_CSP_MODE src_colorspace) {
|
2012-03-20 11:59:22 +01:00
|
|
|
const int W = io->width;
|
|
|
|
const int H = io->height;
|
|
|
|
int x = 0, y = 0, w = W, h = H;
|
|
|
|
|
|
|
|
// Cropping
|
|
|
|
io->use_cropping = (options != NULL) && (options->use_cropping > 0);
|
|
|
|
if (io->use_cropping) {
|
|
|
|
w = options->crop_width;
|
|
|
|
h = options->crop_height;
|
2012-03-27 14:24:56 +02:00
|
|
|
x = options->crop_left;
|
|
|
|
y = options->crop_top;
|
2014-07-03 22:20:06 +02:00
|
|
|
if (!WebPIsRGBMode(src_colorspace)) { // only snap for YUV420
|
2012-03-27 14:24:56 +02:00
|
|
|
x &= ~1;
|
2014-07-03 22:20:06 +02:00
|
|
|
y &= ~1;
|
2012-03-27 14:24:56 +02:00
|
|
|
}
|
2012-03-20 11:59:22 +01:00
|
|
|
if (x < 0 || y < 0 || w <= 0 || h <= 0 || x + w > W || y + h > H) {
|
|
|
|
return 0; // out of frame boundary error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
io->crop_left = x;
|
|
|
|
io->crop_top = y;
|
|
|
|
io->crop_right = x + w;
|
|
|
|
io->crop_bottom = y + h;
|
|
|
|
io->mb_w = w;
|
|
|
|
io->mb_h = h;
|
|
|
|
|
|
|
|
// Scaling
|
|
|
|
io->use_scaling = (options != NULL) && (options->use_scaling > 0);
|
|
|
|
if (io->use_scaling) {
|
2015-08-14 05:51:52 +02:00
|
|
|
int scaled_width = options->scaled_width;
|
|
|
|
int scaled_height = options->scaled_height;
|
|
|
|
if (!WebPRescalerGetScaledDimensions(w, h, &scaled_width, &scaled_height)) {
|
2012-03-20 11:59:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-08-14 05:51:52 +02:00
|
|
|
io->scaled_width = scaled_width;
|
|
|
|
io->scaled_height = scaled_height;
|
2012-03-20 11:59:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter
|
2016-05-25 08:21:54 +02:00
|
|
|
io->bypass_filtering = (options != NULL) && options->bypass_filtering;
|
2012-03-20 11:59:22 +01:00
|
|
|
|
|
|
|
// Fancy upsampler
|
|
|
|
#ifdef FANCY_UPSAMPLING
|
|
|
|
io->fancy_upsampling = (options == NULL) || (!options->no_fancy_upsampling);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (io->use_scaling) {
|
|
|
|
// disable filter (only for large downscaling ratio).
|
|
|
|
io->bypass_filtering = (io->scaled_width < W * 3 / 4) &&
|
|
|
|
(io->scaled_height < H * 3 / 4);
|
|
|
|
io->fancy_upsampling = 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|