Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Unified Diff: third_party/libwebp/dec/webp.c

Issue 10832153: libwebp: update snapshot to v0.2.0-rc1 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/libwebp/dec/webp.c
diff --git a/third_party/libwebp/dec/webp.c b/third_party/libwebp/dec/webp.c
index 91ac75f7bb88a967eb6476817b1430b46e21934a..edd348cbe70c509dc721bffd502621e8c9f08d22 100644
--- a/third_party/libwebp/dec/webp.c
+++ b/third_party/libwebp/dec/webp.c
@@ -1,4 +1,4 @@
-// Copyright 2010 Google Inc.
+// Copyright 2010 Google Inc. All Rights Reserved.
//
// This code is licensed under the same terms as WebM:
// Software License Agreement: http://www.webmproject.org/license/software/
@@ -10,8 +10,11 @@
// Author: Skal (pascal.massimino@gmail.com)
#include <stdlib.h>
-#include "vp8i.h"
-#include "webpi.h"
+
+#include "./vp8i.h"
+#include "./vp8li.h"
+#include "./webpi.h"
+#include "../webp/format_constants.h"
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
@@ -24,105 +27,148 @@ extern "C" {
// 4...7 size of image data (including metadata) starting at offset 8
// 8...11 "WEBP" our form-type signature
// The RIFF container (12 bytes) is followed by appropriate chunks:
-// 12..15 "VP8 ": 4-bytes tags, describing the raw video format used
+// 12..15 "VP8 ": 4-bytes tags, signaling the use of VP8 video format
// 16..19 size of the raw VP8 image data, starting at offset 20
// 20.... the VP8 bytes
// Or,
+// 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,
// 12..15 "VP8X": 4-bytes tags, describing the extended-VP8 chunk.
// 16..19 size of the VP8X chunk starting at offset 20.
// 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.
+// 24..26 Width of the Canvas Image.
+// 27..29 Height of the Canvas Image.
// There can be extra chunks after the "VP8X" chunk (ICCP, TILE, FRM, VP8,
// META ...)
-// All 32-bits sizes are in little-endian order.
-// Note: chunk data must be padded to multiple of 2 in size
+// All sizes are in little-endian order.
+// Note: chunk data size must be padded to multiple of 2 when written.
-static inline uint32_t get_le32(const uint8_t* const data) {
- return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
+static WEBP_INLINE uint32_t get_le24(const uint8_t* const data) {
+ return data[0] | (data[1] << 8) | (data[2] << 16);
}
-VP8StatusCode WebPParseRIFF(const uint8_t** data, uint32_t* data_size,
- uint32_t* riff_size) {
- assert(data);
- assert(data_size);
- assert(riff_size);
+static WEBP_INLINE uint32_t get_le32(const uint8_t* const data) {
+ return (uint32_t)get_le24(data) | (data[3] << 24);
+}
- if (*data_size >= RIFF_HEADER_SIZE &&
- !memcmp(*data, "RIFF", TAG_SIZE)) {
+// 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.
+static VP8StatusCode ParseRIFF(const uint8_t** const data,
+ size_t* const data_size,
+ size_t* const riff_size) {
+ assert(data != NULL);
+ assert(data_size != NULL);
+ assert(riff_size != NULL);
+
+ *riff_size = 0; // Default: no RIFF present.
+ if (*data_size >= RIFF_HEADER_SIZE && !memcmp(*data, "RIFF", TAG_SIZE)) {
if (memcmp(*data + 8, "WEBP", TAG_SIZE)) {
return VP8_STATUS_BITSTREAM_ERROR; // Wrong image file signature.
} else {
- *riff_size = get_le32(*data + TAG_SIZE);
+ const uint32_t size = get_le32(*data + TAG_SIZE);
// Check that we have at least one chunk (i.e "WEBP" + "VP8?nnnn").
- if (*riff_size < TAG_SIZE + CHUNK_HEADER_SIZE) {
+ if (size < TAG_SIZE + CHUNK_HEADER_SIZE) {
return VP8_STATUS_BITSTREAM_ERROR;
}
// We have a RIFF container. Skip it.
+ *riff_size = size;
*data += RIFF_HEADER_SIZE;
*data_size -= RIFF_HEADER_SIZE;
}
- } else {
- *riff_size = 0; // Did not get full RIFF Header.
}
return VP8_STATUS_OK;
}
-VP8StatusCode WebPParseVP8X(const uint8_t** data, uint32_t* data_size,
- uint32_t* bytes_skipped,
- int* width, int* height, uint32_t* flags) {
- assert(data);
- assert(data_size);
- assert(bytes_skipped);
+// 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_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) {
+ const uint32_t vp8x_size = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
+ assert(data != NULL);
+ assert(data_size != NULL);
+ assert(found_vp8x != NULL);
+
+ *found_vp8x = 0;
- *bytes_skipped = 0;
-
- if (*data_size < CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE) {
+ if (*data_size < CHUNK_HEADER_SIZE) {
return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
}
if (!memcmp(*data, "VP8X", TAG_SIZE)) {
+ int width, height;
+ uint32_t flags;
const uint32_t chunk_size = get_le32(*data + TAG_SIZE);
if (chunk_size != VP8X_CHUNK_SIZE) {
return VP8_STATUS_BITSTREAM_ERROR; // Wrong chunk size.
}
- if (flags) {
- *flags = get_le32(*data + 8);
- }
- if (width) {
- *width = get_le32(*data + 12);
+
+ // Verify if enough data is available to validate the VP8X chunk.
+ if (*data_size < vp8x_size) {
+ return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
}
- if (height) {
- *height = get_le32(*data + 16);
+ flags = get_le32(*data + 8);
+ width = 1 + get_le24(*data + 12);
+ height = 1 + get_le24(*data + 15);
+ if (width * (uint64_t)height >= MAX_IMAGE_AREA) {
+ return VP8_STATUS_BITSTREAM_ERROR; // image is too large
}
- // We have consumed 20 bytes from VP8X. Skip them.
- *bytes_skipped = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
- *data += *bytes_skipped;
- *data_size -= *bytes_skipped;
+
+ if (flags_ptr != NULL) *flags_ptr = flags;
+ if (width_ptr != NULL) *width_ptr = width;
+ if (height_ptr != NULL) *height_ptr = height;
+ // Skip over VP8X header bytes.
+ *data += vp8x_size;
+ *data_size -= vp8x_size;
+ *found_vp8x = 1;
}
return VP8_STATUS_OK;
}
-VP8StatusCode WebPParseOptionalChunks(const uint8_t** data, uint32_t* data_size,
- uint32_t riff_size,
- uint32_t* bytes_skipped) {
+// Skips to the next VP8/VP8L 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** const data,
+ size_t* const data_size,
+ size_t const riff_size,
+ const uint8_t** const alpha_data,
+ size_t* const alpha_size) {
const uint8_t* buf;
- uint32_t buf_size;
-
- assert(data);
- assert(data_size);
- assert(bytes_skipped);
-
+ size_t buf_size;
+ uint32_t total_size = TAG_SIZE + // "WEBP".
+ CHUNK_HEADER_SIZE + // "VP8Xnnnn".
+ VP8X_CHUNK_SIZE; // data.
+ assert(data != NULL);
+ assert(data_size != NULL);
buf = *data;
buf_size = *data_size;
- *bytes_skipped = 0;
+
+ assert(alpha_data != NULL);
+ assert(alpha_size != NULL);
+ *alpha_data = NULL;
+ *alpha_size = 0;
while (1) {
uint32_t chunk_size;
- uint32_t cur_skip_size;
- const uint32_t bytes_skipped_header = TAG_SIZE + // "WEBP".
- CHUNK_HEADER_SIZE + // "VP8Xnnnn".
- VP8X_CHUNK_SIZE; // Data.
+ uint32_t disk_chunk_size; // chunk_size with padding
+
*data = buf;
*data_size = buf_size;
@@ -131,119 +177,197 @@ VP8StatusCode WebPParseOptionalChunks(const uint8_t** data, uint32_t* data_size,
}
chunk_size = get_le32(buf + TAG_SIZE);
- cur_skip_size = CHUNK_HEADER_SIZE + chunk_size;
+ // For odd-sized chunk-payload, there's one byte padding at the end.
+ disk_chunk_size = (CHUNK_HEADER_SIZE + chunk_size + 1) & ~1;
+ total_size += disk_chunk_size;
- // Check that total bytes skipped along with current chunk size
- // does not exceed riff_size.
- if (riff_size > 0 &&
- (bytes_skipped_header + *bytes_skipped + cur_skip_size > riff_size)) {
- return VP8_STATUS_BITSTREAM_ERROR; // Not a valid chunk size.
+ // 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.
}
- if (buf_size < cur_skip_size) { // Insufficient data.
+ if (buf_size < disk_chunk_size) { // Insufficient data.
return VP8_STATUS_NOT_ENOUGH_DATA;
}
- if (!memcmp(buf, "VP8 ", TAG_SIZE)) { // A valid VP8 header.
+ if (!memcmp(buf, "ALPH", TAG_SIZE)) { // A valid ALPH header.
+ *alpha_data = buf + CHUNK_HEADER_SIZE;
+ *alpha_size = chunk_size;
+ } else if (!memcmp(buf, "VP8 ", TAG_SIZE) ||
+ !memcmp(buf, "VP8L", TAG_SIZE)) { // A valid VP8/VP8L header.
return VP8_STATUS_OK; // Found.
}
- // We have a full & valid chunk; skip it.
- buf += cur_skip_size;
- buf_size -= cur_skip_size;
- *bytes_skipped += cur_skip_size;
+ // We have a full and valid chunk; skip it.
+ buf += disk_chunk_size;
+ buf_size -= disk_chunk_size;
}
}
-VP8StatusCode WebPParseVP8Header(const uint8_t** data, uint32_t* data_size,
- uint32_t riff_size, uint32_t* bytes_skipped,
- uint32_t* vp8_chunk_size) {
- assert(data);
- assert(data_size);
- assert(bytes_skipped);
- assert(vp8_chunk_size);
-
- *bytes_skipped = 0;
- *vp8_chunk_size = 0;
+// Validates the VP8/VP8L Header ("VP8 nnnn" or "VP8L nnnn") and skips over it.
+// Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than
+// riff_size) VP8/VP8L 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 / raw VP8L data.
+static VP8StatusCode ParseVP8Header(const uint8_t** const data_ptr,
+ size_t* const data_size,
+ size_t riff_size,
+ size_t* const chunk_size,
+ int* const is_lossless) {
+ 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);
+ const uint32_t minimal_size =
+ TAG_SIZE + CHUNK_HEADER_SIZE; // "WEBP" + "VP8 nnnn" OR
+ // "WEBP" + "VP8Lnnnn"
+ assert(data != NULL);
+ assert(data_size != NULL);
+ assert(chunk_size != NULL);
+ assert(is_lossless != NULL);
if (*data_size < CHUNK_HEADER_SIZE) {
return VP8_STATUS_NOT_ENOUGH_DATA; // Insufficient data.
}
- if (!memcmp(*data, "VP8 ", TAG_SIZE)) {
- *vp8_chunk_size = get_le32(*data + TAG_SIZE);
- if (riff_size >= TAG_SIZE + CHUNK_HEADER_SIZE && // "WEBP" + "VP8 nnnn".
- (*vp8_chunk_size > riff_size - (TAG_SIZE + CHUNK_HEADER_SIZE))) {
+ if (is_vp8 || is_vp8l) {
+ // Bitstream contains VP8/VP8L header.
+ const uint32_t size = get_le32(data + TAG_SIZE);
+ if ((riff_size >= minimal_size) && (size > riff_size - minimal_size)) {
return VP8_STATUS_BITSTREAM_ERROR; // Inconsistent size information.
}
- // We have consumed CHUNK_HEADER_SIZE bytes from VP8 Header. Skip them.
- *bytes_skipped = CHUNK_HEADER_SIZE;
- *data += *bytes_skipped;
- *data_size -= *bytes_skipped;
+ // Skip over CHUNK_HEADER_SIZE bytes from VP8/VP8L Header.
+ *chunk_size = size;
+ *data_ptr += CHUNK_HEADER_SIZE;
+ *data_size -= CHUNK_HEADER_SIZE;
+ *is_lossless = is_vp8l;
+ } else {
+ // Raw VP8/VP8L bitstream (no header).
+ *is_lossless = VP8LCheckSignature(data, *data_size);
+ *chunk_size = *data_size;
}
+
return VP8_STATUS_OK;
}
-VP8StatusCode WebPParseHeaders(const uint8_t** data, uint32_t* data_size,
- uint32_t* vp8_size, uint32_t* bytes_skipped) {
- const uint8_t* buf;
- uint32_t buf_size;
- uint32_t riff_size;
- uint32_t vp8_size_tmp;
- uint32_t optional_data_size;
- uint32_t vp8x_skip_size;
- uint32_t vp8_skip_size;
- VP8StatusCode status;
-
- assert(data);
- assert(data_size);
- assert(vp8_size);
- assert(bytes_skipped);
-
- buf = *data;
- buf_size = *data_size;
+//------------------------------------------------------------------------------
- *vp8_size = 0;
- *bytes_skipped = 0;
+// 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.
+// 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).
+// 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.
+static VP8StatusCode ParseHeadersInternal(const uint8_t* data,
+ size_t data_size,
+ int* const width,
+ int* const height,
+ int* const has_alpha,
+ WebPHeaderStructure* const headers) {
+ int found_riff = 0;
+ int found_vp8x = 0;
+ VP8StatusCode status;
+ WebPHeaderStructure hdrs;
- if (buf == NULL || buf_size < RIFF_HEADER_SIZE) {
+ if (data == NULL || data_size < RIFF_HEADER_SIZE) {
return VP8_STATUS_NOT_ENOUGH_DATA;
}
+ memset(&hdrs, 0, sizeof(hdrs));
+ hdrs.data = data;
+ hdrs.data_size = data_size;
// Skip over RIFF header.
- if (WebPParseRIFF(&buf, &buf_size, &riff_size) != VP8_STATUS_OK) {
- return VP8_STATUS_BITSTREAM_ERROR; // Wrong RIFF Header.
+ status = ParseRIFF(&data, &data_size, &hdrs.riff_size);
+ if (status != VP8_STATUS_OK) {
+ return status; // Wrong RIFF header / insufficient data.
}
+ found_riff = (hdrs.riff_size > 0);
- // Skip over VP8X header.
- status = WebPParseVP8X(&buf, &buf_size, &vp8x_skip_size, NULL, NULL, NULL);
- if (status != VP8_STATUS_OK) {
- return status; // Wrong VP8X Chunk / Insufficient data.
+ // Skip over VP8X.
+ {
+ 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.
+ }
+ 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;
+ }
+ if (has_alpha != NULL) *has_alpha = !!(flags & ALPHA_FLAG_BIT);
+ if (found_vp8x && headers == NULL) {
+ return VP8_STATUS_OK; // Return features from VP8X header.
+ }
}
- if (vp8x_skip_size > 0) {
- // Skip over optional chunks.
- status = WebPParseOptionalChunks(&buf, &buf_size, riff_size,
- &optional_data_size);
+
+ if (data_size < TAG_SIZE) return VP8_STATUS_NOT_ENOUGH_DATA;
+
+ // 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))) {
+ status = ParseOptionalChunks(&data, &data_size, hdrs.riff_size,
+ &hdrs.alpha_data, &hdrs.alpha_data_size);
if (status != VP8_STATUS_OK) {
- return status; // Found an invalid chunk size / Insufficient data.
+ return status; // Found an invalid chunk size / insufficient data.
}
}
- // Skip over VP8 chunk header.
- status = WebPParseVP8Header(&buf, &buf_size, riff_size, &vp8_skip_size,
- &vp8_size_tmp);
+ // Skip over VP8/VP8L header.
+ status = ParseVP8Header(&data, &data_size, hdrs.riff_size,
+ &hdrs.compressed_size, &hdrs.is_lossless);
if (status != VP8_STATUS_OK) {
- return status; // Invalid VP8 header / Insufficient data.
+ return status; // Wrong VP8/VP8L chunk-header / insufficient data.
}
- if (vp8_skip_size > 0) {
- *vp8_size = vp8_size_tmp;
+ if (hdrs.compressed_size > MAX_CHUNK_PAYLOAD) {
+ return VP8_STATUS_BITSTREAM_ERROR;
}
- *bytes_skipped = buf - *data;
- assert(*bytes_skipped == *data_size - buf_size);
- *data = buf;
- *data_size = buf_size;
- return VP8_STATUS_OK;
+ if (!hdrs.is_lossless) {
+ if (data_size < VP8_FRAME_HEADER_SIZE) {
+ return VP8_STATUS_NOT_ENOUGH_DATA;
+ }
+ // Validates raw VP8 data.
+ if (!VP8GetInfo(data, data_size,
+ (uint32_t)hdrs.compressed_size, width, height)) {
+ return VP8_STATUS_BITSTREAM_ERROR;
+ }
+ } else {
+ if (data_size < VP8L_FRAME_HEADER_SIZE) {
+ return VP8_STATUS_NOT_ENOUGH_DATA;
+ }
+ // Validates raw VP8L data.
+ if (!VP8LGetInfo(data, data_size, width, height, has_alpha)) {
+ return VP8_STATUS_BITSTREAM_ERROR;
+ }
+ }
+
+ 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 (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);
+ }
+ return VP8_STATUS_OK; // Return features from VP8 header.
+}
+
+VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) {
+ assert(headers != NULL);
+ // fill out headers, ignore width/height/has_alpha.
+ return ParseHeadersInternal(headers->data, headers->data_size,
+ NULL, NULL, NULL, headers);
}
//------------------------------------------------------------------------------
@@ -259,43 +383,72 @@ void WebPResetDecParams(WebPDecParams* const params) {
// "Into" decoding variants
// Main flow
-static VP8StatusCode DecodeInto(const uint8_t* data, uint32_t data_size,
+static VP8StatusCode DecodeInto(const uint8_t* const data, size_t data_size,
WebPDecParams* const params) {
- VP8Decoder* dec = VP8New();
- VP8StatusCode status = VP8_STATUS_OK;
+ VP8StatusCode status;
VP8Io io;
+ WebPHeaderStructure headers;
- assert(params);
- if (dec == NULL) {
- return VP8_STATUS_INVALID_PARAM;
+ headers.data = data;
+ headers.data_size = data_size;
+ status = WebPParseHeaders(&headers); // Process Pre-VP8 chunks.
+ if (status != VP8_STATUS_OK) {
+ return status;
}
+ assert(params != NULL);
VP8InitIo(&io);
- io.data = data;
- io.data_size = data_size;
+ io.data = headers.data + headers.offset;
+ io.data_size = headers.data_size - headers.offset;
WebPInitCustomIo(params, &io); // Plug the I/O functions.
+ if (!headers.is_lossless) {
+ VP8Decoder* const dec = VP8New();
+ if (dec == NULL) {
+ return VP8_STATUS_OUT_OF_MEMORY;
+ }
#ifdef WEBP_USE_THREAD
- dec->use_threads_ = params->options && (params->options->use_threads > 0);
+ dec->use_threads_ = params->options && (params->options->use_threads > 0);
#else
- dec->use_threads_ = 0;
+ dec->use_threads_ = 0;
#endif
+ dec->alpha_data_ = headers.alpha_data;
+ dec->alpha_data_size_ = headers.alpha_data_size;
- // Decode bitstream header, update io->width/io->height.
- if (!VP8GetHeaders(dec, &io)) {
- status = VP8_STATUS_BITSTREAM_ERROR;
+ // 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
+ if (!VP8Decode(dec, &io)) {
+ status = dec->status_;
+ }
+ }
+ }
+ VP8Delete(dec);
} else {
- // Allocate/check output buffers.
- status = WebPAllocateDecBuffer(io.width, io.height, params->options,
- params->output);
- if (status == VP8_STATUS_OK) {
- // Decode
- if (!VP8Decode(dec, &io)) {
- status = dec->status_;
+ 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_;
+ }
}
}
+ VP8LDelete(dec);
}
- VP8Delete(dec);
+
if (status != VP8_STATUS_OK) {
WebPFreeDecBuffer(params->output);
}
@@ -304,8 +457,10 @@ static VP8StatusCode DecodeInto(const uint8_t* data, uint32_t data_size,
// Helpers
static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace,
- const uint8_t* data, uint32_t data_size,
- uint8_t* rgba, int stride, int size) {
+ const uint8_t* const data,
+ size_t data_size,
+ uint8_t* const rgba,
+ int stride, size_t size) {
WebPDecParams params;
WebPDecBuffer buf;
if (rgba == NULL) {
@@ -325,35 +480,35 @@ static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace,
return rgba;
}
-uint8_t* WebPDecodeRGBInto(const uint8_t* data, uint32_t data_size,
- uint8_t* output, int size, int stride) {
+uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size,
+ uint8_t* output, size_t size, int stride) {
return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size);
}
-uint8_t* WebPDecodeRGBAInto(const uint8_t* data, uint32_t data_size,
- uint8_t* output, int size, int stride) {
+uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size,
+ uint8_t* output, size_t size, int stride) {
return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size);
}
-uint8_t* WebPDecodeARGBInto(const uint8_t* data, uint32_t data_size,
- uint8_t* output, int size, int stride) {
+uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size,
+ uint8_t* output, size_t size, int stride) {
return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size);
}
-uint8_t* WebPDecodeBGRInto(const uint8_t* data, uint32_t data_size,
- uint8_t* output, int size, int stride) {
+uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size,
+ uint8_t* output, size_t size, int stride) {
return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size);
}
-uint8_t* WebPDecodeBGRAInto(const uint8_t* data, uint32_t data_size,
- uint8_t* output, int size, int stride) {
+uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size,
+ uint8_t* output, size_t size, int stride) {
return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size);
}
-uint8_t* WebPDecodeYUVInto(const uint8_t* data, uint32_t data_size,
- uint8_t* luma, int luma_size, int luma_stride,
- uint8_t* u, int u_size, int u_stride,
- uint8_t* v, int v_size, int v_stride) {
+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) {
WebPDecParams params;
WebPDecBuffer output;
if (luma == NULL) return NULL;
@@ -379,9 +534,9 @@ uint8_t* WebPDecodeYUVInto(const uint8_t* data, uint32_t data_size,
//------------------------------------------------------------------------------
-static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* data,
- uint32_t data_size, int* width, int* height,
- WebPDecBuffer* keep_info) {
+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) {
WebPDecParams params;
WebPDecBuffer output;
@@ -394,53 +549,53 @@ static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* data,
if (!WebPGetInfo(data, data_size, &output.width, &output.height)) {
return NULL;
}
- if (width) *width = output.width;
- if (height) *height = output.height;
+ if (width != NULL) *width = output.width;
+ if (height != NULL) *height = output.height;
// Decode
if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
return NULL;
}
- if (keep_info) { // keep track of the side-info
+ if (keep_info != NULL) { // keep track of the side-info
WebPCopyDecBuffer(&output, keep_info);
}
// return decoded samples (don't clear 'output'!)
- return (mode >= MODE_YUV) ? output.u.YUVA.y : output.u.RGBA.rgba;
+ return WebPIsRGBMode(mode) ? output.u.RGBA.rgba : output.u.YUVA.y;
}
-uint8_t* WebPDecodeRGB(const uint8_t* data, uint32_t data_size,
+uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size,
int* width, int* height) {
return Decode(MODE_RGB, data, data_size, width, height, NULL);
}
-uint8_t* WebPDecodeRGBA(const uint8_t* data, uint32_t data_size,
+uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size,
int* width, int* height) {
return Decode(MODE_RGBA, data, data_size, width, height, NULL);
}
-uint8_t* WebPDecodeARGB(const uint8_t* data, uint32_t data_size,
+uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size,
int* width, int* height) {
return Decode(MODE_ARGB, data, data_size, width, height, NULL);
}
-uint8_t* WebPDecodeBGR(const uint8_t* data, uint32_t data_size,
+uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size,
int* width, int* height) {
return Decode(MODE_BGR, data, data_size, width, height, NULL);
}
-uint8_t* WebPDecodeBGRA(const uint8_t* data, uint32_t data_size,
+uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size,
int* width, int* height) {
return Decode(MODE_BGRA, data, data_size, width, height, NULL);
}
-uint8_t* WebPDecodeYUV(const uint8_t* data, uint32_t data_size,
+uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size,
int* width, int* height, uint8_t** u, uint8_t** v,
int* stride, int* uv_stride) {
WebPDecBuffer output; // only to preserve the side-infos
uint8_t* const out = Decode(MODE_YUV, data, data_size,
width, height, &output);
- if (out) {
+ if (out != NULL) {
const WebPYUVABuffer* const buf = &output.u.YUVA;
*u = buf->u;
*v = buf->v;
@@ -452,69 +607,28 @@ uint8_t* WebPDecodeYUV(const uint8_t* data, uint32_t data_size,
}
static void DefaultFeatures(WebPBitstreamFeatures* const features) {
- assert(features);
+ assert(features != NULL);
memset(features, 0, sizeof(*features));
features->bitstream_version = 0;
}
-static VP8StatusCode GetFeatures(const uint8_t* data, uint32_t data_size,
+static VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size,
WebPBitstreamFeatures* const features) {
- uint32_t vp8_chunk_size = 0;
- uint32_t riff_size = 0;
- uint32_t flags = 0;
- uint32_t vp8x_skip_size = 0;
- uint32_t vp8_skip_size = 0;
- VP8StatusCode status;
-
- if (features == NULL) {
+ if (features == NULL || data == NULL) {
return VP8_STATUS_INVALID_PARAM;
}
DefaultFeatures(features);
- if (data == NULL) {
- return VP8_STATUS_INVALID_PARAM;
- }
-
- // Skip over RIFF header.
- status = WebPParseRIFF(&data, &data_size, &riff_size);
- if (status != VP8_STATUS_OK) {
- return status; // Wrong RIFF Header / Insufficient data.
- }
-
- // Skip over VP8X.
- status = WebPParseVP8X(&data, &data_size, &vp8x_skip_size, &features->width,
- &features->height, &flags);
- if (status != VP8_STATUS_OK) {
- return status; // Wrong VP8X / insufficient data.
-
- }
- if (vp8x_skip_size > 0) {
- return VP8_STATUS_OK; // Return features from VP8X header.
- }
-
- // Skip over VP8 header.
- status = WebPParseVP8Header(&data, &data_size, riff_size, &vp8_skip_size,
- &vp8_chunk_size);
- if (status != VP8_STATUS_OK) {
- return status; // Wrong VP8 Chunk-header / insufficient data.
- }
- if (vp8_skip_size == 0) {
- vp8_chunk_size = data_size; // No VP8 chunk wrapper over raw VP8 data.
- }
-
- // Validates raw VP8 data.
- if (!VP8GetInfo(data, data_size, vp8_chunk_size,
- &features->width, &features->height, &features->has_alpha)) {
- return VP8_STATUS_BITSTREAM_ERROR;
- }
-
- return VP8_STATUS_OK; // Return features from VP8 header.
+ // Only parse enough of the data to retrieve width/height/has_alpha.
+ return ParseHeadersInternal(data, data_size,
+ &features->width, &features->height,
+ &features->has_alpha, NULL);
}
//------------------------------------------------------------------------------
// WebPGetInfo()
-int WebPGetInfo(const uint8_t* data, uint32_t data_size,
+int WebPGetInfo(const uint8_t* data, size_t data_size,
int* width, int* height) {
WebPBitstreamFeatures features;
@@ -522,10 +636,10 @@ int WebPGetInfo(const uint8_t* data, uint32_t data_size,
return 0;
}
- if (width) {
+ if (width != NULL) {
*width = features.width;
}
- if (height) {
+ if (height != NULL) {
*height = features.height;
}
@@ -535,9 +649,9 @@ int WebPGetInfo(const uint8_t* data, uint32_t data_size,
//------------------------------------------------------------------------------
// Advance decoding API
-int WebPInitDecoderConfigInternal(WebPDecoderConfig* const config,
+int WebPInitDecoderConfigInternal(WebPDecoderConfig* config,
int version) {
- if (version != WEBP_DECODER_ABI_VERSION) {
+ if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
return 0; // version mismatch
}
if (config == NULL) {
@@ -549,11 +663,11 @@ int WebPInitDecoderConfigInternal(WebPDecoderConfig* const config,
return 1;
}
-VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, uint32_t data_size,
- WebPBitstreamFeatures* const features,
+VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size,
+ WebPBitstreamFeatures* features,
int version) {
VP8StatusCode status;
- if (version != WEBP_DECODER_ABI_VERSION) {
+ if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
return VP8_STATUS_INVALID_PARAM; // version mismatch
}
if (features == NULL) {
@@ -562,24 +676,24 @@ VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, uint32_t data_size,
status = GetFeatures(data, data_size, features);
if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
- return VP8_STATUS_BITSTREAM_ERROR; // Not enough data treated as error.
+ return VP8_STATUS_BITSTREAM_ERROR; // Not-enough-data treated as error.
}
return status;
}
-VP8StatusCode WebPDecode(const uint8_t* data, uint32_t data_size,
- WebPDecoderConfig* const config) {
+VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,
+ WebPDecoderConfig* config) {
WebPDecParams params;
VP8StatusCode status;
- if (!config) {
+ if (config == NULL) {
return VP8_STATUS_INVALID_PARAM;
}
status = GetFeatures(data, data_size, &config->input);
if (status != VP8_STATUS_OK) {
if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
- return VP8_STATUS_BITSTREAM_ERROR; // Not enough data treated as error.
+ return VP8_STATUS_BITSTREAM_ERROR; // Not-enough-data treated as error.
}
return status;
}
@@ -592,6 +706,66 @@ VP8StatusCode WebPDecode(const uint8_t* data, uint32_t data_size,
return status;
}
+//------------------------------------------------------------------------------
+// Cropping and rescaling.
+
+int WebPIoInitFromOptions(const WebPDecoderOptions* const options,
+ VP8Io* const io, WEBP_CSP_MODE src_colorspace) {
+ 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;
+ x = options->crop_left;
+ y = options->crop_top;
+ if (!WebPIsRGBMode(src_colorspace)) { // only snap for YUV420 or YUV422
+ x &= ~1;
+ y &= ~1; // TODO(later): only for YUV420, not YUV422.
+ }
+ 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;
+}
+
+//------------------------------------------------------------------------------
+
#if defined(__cplusplus) || defined(c_plusplus)
} // extern "C"
#endif

Powered by Google App Engine
This is Rietveld 408576698