2012-01-06 23:49:06 +01:00
|
|
|
// Copyright 2010 Google Inc. All Rights Reserved.
|
2010-09-30 15:34:38 +02:00
|
|
|
//
|
|
|
|
// This code is licensed under the same terms as WebM:
|
|
|
|
// Software License Agreement: http://www.webmproject.org/license/software/
|
|
|
|
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// 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"
|
2012-02-08 21:30:30 +01:00
|
|
|
#include "../mux/muxi.h" // For MAX_CHUNK_PAYLOAD.
|
2011-12-01 04:27:36 +01:00
|
|
|
#include "../webp/mux.h" // For 'ALPHA_FLAG'.
|
2010-09-30 15:34:38 +02:00
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
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.
|
|
|
|
// 24..27 Width of the Canvas Image.
|
|
|
|
// 28..31 Height of the Canvas Image.
|
|
|
|
// There can be extra chunks after the "VP8X" chunk (ICCP, TILE, FRM, VP8,
|
|
|
|
// META ...)
|
2010-09-30 15:34:38 +02:00
|
|
|
// All 32-bits sizes are in little-endian order.
|
|
|
|
// Note: chunk data must be padded to multiple of 2 in size
|
|
|
|
|
2011-11-05 03:44:57 +01:00
|
|
|
static WEBP_INLINE uint32_t get_le32(const uint8_t* const data) {
|
2010-09-30 15:34:38 +02:00
|
|
|
return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
|
|
|
|
}
|
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
// Validates the RIFF container (if detected) and skips over it.
|
|
|
|
// If a RIFF container is detected,
|
|
|
|
// Returns VP8_STATUS_BITSTREAM_ERROR for invalid header, and
|
|
|
|
// VP8_STATUS_OK otherwise.
|
|
|
|
// In case there are not enough bytes (partial RIFF container), return 0 for
|
|
|
|
// riff_size. Else return the riff_size extracted from the header.
|
2012-04-18 01:01:33 +02:00
|
|
|
static VP8StatusCode ParseRIFF(const uint8_t** data, size_t* data_size,
|
2012-04-05 13:19:24 +02:00
|
|
|
size_t* riff_size) {
|
2011-09-13 11:35:10 +02:00
|
|
|
assert(data);
|
|
|
|
assert(data_size);
|
|
|
|
assert(riff_size);
|
|
|
|
|
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 {
|
2012-04-11 03:41:18 +02:00
|
|
|
const uint32_t size = get_le32(*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
|
|
|
}
|
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.
|
|
|
|
// If a VP8X chunk is found, found_vp8x is set to true and *width, *height and
|
|
|
|
// *flags are set to the corresponding values extracted from the VP8X chunk.
|
2012-04-18 01:01:33 +02:00
|
|
|
static VP8StatusCode ParseVP8X(const uint8_t** data, size_t* data_size,
|
2012-04-11 03:41:18 +02:00
|
|
|
int* found_vp8x,
|
|
|
|
int* width, int* height, uint32_t* flags) {
|
|
|
|
const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
|
2011-09-13 11:35:10 +02:00
|
|
|
assert(data);
|
|
|
|
assert(data_size);
|
2012-04-11 03:41:18 +02:00
|
|
|
assert(found_vp8x);
|
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-04-11 03:41:18 +02:00
|
|
|
if (*data_size < vp8x_size) {
|
2011-09-13 11:35:10 +02:00
|
|
|
return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!memcmp(*data, "VP8X", TAG_SIZE)) {
|
|
|
|
const uint32_t chunk_size = get_le32(*data + TAG_SIZE);
|
|
|
|
if (chunk_size != VP8X_CHUNK_SIZE) {
|
|
|
|
return VP8_STATUS_BITSTREAM_ERROR; // Wrong chunk size.
|
|
|
|
}
|
2012-01-23 08:20:53 +01:00
|
|
|
if (flags != NULL) {
|
2011-09-13 11:35:10 +02:00
|
|
|
*flags = get_le32(*data + 8);
|
|
|
|
}
|
2012-01-23 08:20:53 +01:00
|
|
|
if (width != NULL) {
|
2011-09-13 11:35:10 +02:00
|
|
|
*width = get_le32(*data + 12);
|
|
|
|
}
|
2012-01-23 08:20:53 +01:00
|
|
|
if (height != NULL) {
|
2011-09-13 11:35:10 +02:00
|
|
|
*height = get_le32(*data + 16);
|
|
|
|
}
|
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-04-11 03:41:18 +02:00
|
|
|
// Skips to the next VP8 chunk header in the data given the size of the RIFF
|
|
|
|
// chunk 'riff_size'.
|
|
|
|
// 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.
|
|
|
|
// If an alpha chunk is found, alpha_data and alpha_size are set appropriately.
|
|
|
|
static VP8StatusCode ParseOptionalChunks(const uint8_t** data,
|
2012-04-18 01:01:33 +02:00
|
|
|
size_t* data_size,
|
|
|
|
size_t riff_size,
|
2012-04-11 03:41:18 +02:00
|
|
|
const uint8_t** alpha_data,
|
2012-04-18 01:01:33 +02:00
|
|
|
size_t* 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.
|
2011-09-13 11:35:10 +02:00
|
|
|
assert(data);
|
|
|
|
assert(data_size);
|
|
|
|
buf = *data;
|
|
|
|
buf_size = *data_size;
|
2012-04-11 03:41:18 +02:00
|
|
|
|
|
|
|
assert(alpha_data);
|
|
|
|
assert(alpha_size);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
chunk_size = get_le32(buf + TAG_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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
} else if (!memcmp(buf, "VP8 ", TAG_SIZE)) { // A valid VP8 header.
|
2011-09-13 11:35:10 +02:00
|
|
|
return VP8_STATUS_OK; // Found.
|
|
|
|
}
|
|
|
|
|
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-04-11 03:41:18 +02:00
|
|
|
// Validates the VP8 Header ("VP8 nnnn" or "VP8L nnnn") and skips over it.
|
|
|
|
// Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than
|
|
|
|
// riff_size) VP8 header,
|
|
|
|
// VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and
|
|
|
|
// VP8_STATUS_OK otherwise.
|
|
|
|
// If a VP8/VP8L chunk is found, chunk_size is set to the total number of bytes
|
|
|
|
// extracted from the VP8/VP8L chunk header.
|
|
|
|
// The flag 'is_lossless' is set to 1 in case of VP8L chunk.
|
2012-04-18 01:01:33 +02:00
|
|
|
static VP8StatusCode ParseVP8Header(const uint8_t** data, size_t* data_size,
|
2012-04-05 13:19:24 +02:00
|
|
|
size_t riff_size,
|
|
|
|
size_t* chunk_size, int* is_lossless) {
|
2012-04-11 03:41:18 +02:00
|
|
|
const int is_vp8 = !memcmp(*data, "VP8 ", TAG_SIZE);
|
|
|
|
const int is_vp8l = !memcmp(*data, "VP8L", TAG_SIZE);
|
|
|
|
const uint32_t minimal_size =
|
|
|
|
TAG_SIZE + CHUNK_HEADER_SIZE; // "WEBP" + "VP8 nnnn"
|
2011-09-13 11:35:10 +02:00
|
|
|
assert(data);
|
|
|
|
assert(data_size);
|
2012-04-11 03:41:18 +02:00
|
|
|
assert(chunk_size);
|
|
|
|
assert(is_lossless);
|
2011-09-13 11:35:10 +02:00
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
*chunk_size = *data_size; // default: raw data
|
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) {
|
|
|
|
const uint32_t size = get_le32(*data + TAG_SIZE);
|
|
|
|
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.
|
|
|
|
}
|
2012-01-23 08:20:53 +01:00
|
|
|
// Skip over CHUNK_HEADER_SIZE bytes from VP8 Header.
|
2012-04-11 03:41:18 +02:00
|
|
|
*chunk_size = size;
|
|
|
|
*data += CHUNK_HEADER_SIZE;
|
|
|
|
*data_size -= CHUNK_HEADER_SIZE;
|
|
|
|
*is_lossless = is_vp8l;
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
|
|
|
return VP8_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) {
|
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
|
|
|
int found_vp8x;
|
2011-09-13 11:35:10 +02:00
|
|
|
VP8StatusCode status;
|
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
assert(headers);
|
2011-09-13 11:35:10 +02:00
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
buf = headers->data;
|
|
|
|
buf_size = headers->data_size;
|
|
|
|
headers->alpha_data = NULL;
|
|
|
|
headers->alpha_data_size = 0;
|
2012-04-05 13:19:24 +02:00
|
|
|
headers->compressed_size = 0;
|
2012-04-11 03:41:18 +02:00
|
|
|
headers->is_lossless = 0;
|
|
|
|
headers->offset = 0;
|
|
|
|
headers->riff_size = 0;
|
2011-09-13 11:35:10 +02:00
|
|
|
|
|
|
|
if (buf == NULL || buf_size < RIFF_HEADER_SIZE) {
|
|
|
|
return VP8_STATUS_NOT_ENOUGH_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip over RIFF header.
|
2012-04-11 03:41:18 +02:00
|
|
|
if (ParseRIFF(&buf, &buf_size, &headers->riff_size) != VP8_STATUS_OK) {
|
2012-01-23 08:20:53 +01:00
|
|
|
return VP8_STATUS_BITSTREAM_ERROR; // Wrong RIFF header.
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
|
|
|
|
2011-09-16 21:07:55 +02:00
|
|
|
// Skip over VP8X header.
|
2012-04-11 03:41:18 +02:00
|
|
|
status = ParseVP8X(&buf, &buf_size, &found_vp8x, NULL, NULL, NULL);
|
2011-09-13 11:35:10 +02:00
|
|
|
if (status != VP8_STATUS_OK) {
|
2012-01-23 08:20:53 +01:00
|
|
|
return status; // Wrong VP8X chunk / insufficient data.
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
2012-04-11 03:41:18 +02:00
|
|
|
if (found_vp8x) {
|
2011-09-13 11:35:10 +02:00
|
|
|
// Skip over optional chunks.
|
2012-04-11 03:41:18 +02:00
|
|
|
status = ParseOptionalChunks(&buf, &buf_size, headers->riff_size,
|
|
|
|
&headers->alpha_data,
|
|
|
|
&headers->alpha_data_size);
|
2011-09-13 11:35:10 +02:00
|
|
|
if (status != VP8_STATUS_OK) {
|
2012-01-23 08:20:53 +01:00
|
|
|
return status; // Found an invalid chunk size / insufficient data.
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip over VP8 chunk header.
|
2012-04-11 03:41:18 +02:00
|
|
|
status = ParseVP8Header(&buf, &buf_size, headers->riff_size,
|
2012-04-05 13:19:24 +02:00
|
|
|
&headers->compressed_size, &headers->is_lossless);
|
2011-09-13 11:35:10 +02:00
|
|
|
if (status != VP8_STATUS_OK) {
|
2012-01-23 08:20:53 +01:00
|
|
|
return status; // Invalid VP8 header / insufficient data.
|
2011-09-13 11:35:10 +02:00
|
|
|
}
|
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
headers->offset = (uint32_t)(buf - headers->data);
|
|
|
|
assert((uint64_t)(buf - headers->data) < MAX_CHUNK_PAYLOAD);
|
|
|
|
assert(headers->offset == headers->data_size - buf_size);
|
2011-09-13 11:35:10 +02:00
|
|
|
return VP8_STATUS_OK;
|
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) {
|
2011-06-20 09:45:15 +02:00
|
|
|
if (params) {
|
|
|
|
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-04-18 01:01:33 +02:00
|
|
|
static VP8StatusCode DecodeInto(const uint8_t* 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;
|
|
|
|
status = WebPParseHeaders(&headers); // Process Pre-VP8 chunks.
|
|
|
|
if (status != VP8_STATUS_OK) {
|
|
|
|
return status;
|
2010-09-30 15:34:38 +02:00
|
|
|
}
|
|
|
|
|
2012-04-05 11:31:10 +02:00
|
|
|
assert(params);
|
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;
|
|
|
|
}
|
2011-07-22 22:09:10 +02:00
|
|
|
#ifdef WEBP_USE_THREAD
|
2012-04-05 11:31:10 +02:00
|
|
|
dec->use_threads_ = params->options && (params->options->use_threads > 0);
|
2011-07-22 22:09:10 +02:00
|
|
|
#else
|
2012-04-05 11:31:10 +02:00
|
|
|
dec->use_threads_ = 0;
|
2011-07-22 22:09:10 +02:00
|
|
|
#endif
|
2012-04-05 11:31:10 +02:00
|
|
|
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
|
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);
|
|
|
|
}
|
|
|
|
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-04-18 01:01:33 +02:00
|
|
|
const uint8_t* data, size_t data_size,
|
|
|
|
uint8_t* 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
|
|
|
|
2011-03-09 04:23:16 +01:00
|
|
|
static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* data,
|
2012-04-18 01:01:33 +02:00
|
|
|
size_t data_size, int* width, int* height,
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPDecBuffer* 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'!)
|
|
|
|
return (mode >= MODE_YUV) ? output.u.YUVA.y : output.u.RGBA.rgba;
|
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) {
|
|
|
|
assert(features);
|
|
|
|
memset(features, 0, sizeof(*features));
|
|
|
|
features->bitstream_version = 0;
|
|
|
|
}
|
|
|
|
|
2012-04-18 01:01:33 +02:00
|
|
|
static VP8StatusCode GetFeatures(const uint8_t* data, size_t data_size,
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPBitstreamFeatures* const features) {
|
2012-04-05 13:19:24 +02:00
|
|
|
size_t chunk_size = 0;
|
|
|
|
size_t riff_size = 0;
|
2012-01-17 09:49:56 +01:00
|
|
|
int* const width = &features->width;
|
|
|
|
int* const height = &features->height;
|
2012-04-11 03:41:18 +02:00
|
|
|
int found_vp8x;
|
|
|
|
int is_lossless = 0;
|
2011-09-13 11:35:10 +02:00
|
|
|
VP8StatusCode status;
|
2011-08-18 20:07:13 +02:00
|
|
|
|
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
|
|
|
|
2011-09-13 11:35:10 +02:00
|
|
|
// Skip over RIFF header.
|
2012-04-11 03:41:18 +02:00
|
|
|
status = ParseRIFF(&data, &data_size, &riff_size);
|
2011-09-13 11:35:10 +02:00
|
|
|
if (status != VP8_STATUS_OK) {
|
2012-01-23 08:20:53 +01:00
|
|
|
return status; // Wrong RIFF header / insufficient data.
|
2011-08-18 20:07:13 +02:00
|
|
|
}
|
|
|
|
|
2011-09-16 21:07:55 +02:00
|
|
|
// Skip over VP8X.
|
2012-04-11 03:41:18 +02:00
|
|
|
{
|
|
|
|
uint32_t flags = 0;
|
|
|
|
status = ParseVP8X(&data, &data_size, &found_vp8x, width, height, &flags);
|
|
|
|
if (status != VP8_STATUS_OK) {
|
|
|
|
return status; // Wrong VP8X / insufficient data.
|
|
|
|
}
|
|
|
|
features->has_alpha = !!(flags & ALPHA_FLAG);
|
|
|
|
if (found_vp8x) {
|
|
|
|
return VP8_STATUS_OK; // Return features from VP8X header.
|
|
|
|
}
|
2011-06-20 09:45:15 +02:00
|
|
|
}
|
2011-08-18 20:07:13 +02:00
|
|
|
|
2011-09-13 11:35:10 +02:00
|
|
|
// Skip over VP8 header.
|
2012-04-11 03:41:18 +02:00
|
|
|
status = ParseVP8Header(&data, &data_size, riff_size,
|
|
|
|
&chunk_size, &is_lossless);
|
2011-09-13 11:35:10 +02:00
|
|
|
if (status != VP8_STATUS_OK) {
|
2012-01-23 08:20:53 +01:00
|
|
|
return status; // Wrong VP8 chunk-header / insufficient data.
|
2011-08-18 20:07:13 +02:00
|
|
|
}
|
|
|
|
|
2012-04-11 03:41:18 +02:00
|
|
|
if (!is_lossless) {
|
|
|
|
// Validates raw VP8 data.
|
2012-04-05 13:19:24 +02:00
|
|
|
if (chunk_size != (uint32_t)chunk_size ||
|
|
|
|
!VP8GetInfo(data, data_size, (uint32_t)chunk_size, width, height)) {
|
2012-04-11 03:41:18 +02:00
|
|
|
return VP8_STATUS_BITSTREAM_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Validates raw VP8L data.
|
|
|
|
if (!VP8LGetInfo(data, data_size, width, height)) {
|
|
|
|
return VP8_STATUS_BITSTREAM_ERROR;
|
|
|
|
}
|
|
|
|
features->has_alpha = 1;
|
2011-06-20 09:45:15 +02:00
|
|
|
}
|
2011-08-18 20:07:13 +02:00
|
|
|
|
2011-09-13 11:35:10 +02:00
|
|
|
return VP8_STATUS_OK; // Return features from VP8 header.
|
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
|
|
|
|
|
|
|
|
int WebPInitDecoderConfigInternal(WebPDecoderConfig* const config,
|
|
|
|
int version) {
|
|
|
|
if (version != WEBP_DECODER_ABI_VERSION) {
|
|
|
|
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,
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPBitstreamFeatures* const features,
|
2011-09-13 11:35:10 +02:00
|
|
|
int version) {
|
|
|
|
VP8StatusCode status;
|
2011-06-20 09:45:15 +02:00
|
|
|
if (version != WEBP_DECODER_ABI_VERSION) {
|
|
|
|
return VP8_STATUS_INVALID_PARAM; // version mismatch
|
|
|
|
}
|
|
|
|
if (features == NULL) {
|
|
|
|
return VP8_STATUS_INVALID_PARAM;
|
|
|
|
}
|
2011-09-13 11:35:10 +02:00
|
|
|
|
|
|
|
status = GetFeatures(data, data_size, features);
|
|
|
|
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
|
|
|
}
|
|
|
|
return status;
|
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,
|
2011-06-20 09:45:15 +02:00
|
|
|
WebPDecoderConfig* const config) {
|
|
|
|
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.output = &config->output;
|
|
|
|
params.options = &config->options;
|
|
|
|
status = DecodeInto(data, data_size, ¶ms);
|
|
|
|
|
|
|
|
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;
|
|
|
|
if (src_colorspace >= MODE_YUV) { // only snap for YUV420 or YUV422
|
|
|
|
x &= ~1;
|
|
|
|
y &= ~1; // TODO(later): only for YUV420, not YUV422.
|
|
|
|
}
|
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) {
|
|
|
|
if (options->scaled_width <= 0 || options->scaled_height <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
io->scaled_width = options->scaled_width;
|
|
|
|
io->scaled_height = options->scaled_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter
|
|
|
|
io->bypass_filtering = options && options->bypass_filtering;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2010-09-30 15:34:38 +02:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|