2012-01-06 23:49:06 +01:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
2011-06-20 09:45:15 +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/
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Everything about WebPDecBuffer
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2011-12-01 11:00:03 +01:00
|
|
|
|
|
|
|
#include "./vp8i.h"
|
|
|
|
#include "./webpi.h"
|
2011-06-20 09:45:15 +02:00
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-06-20 09:45:15 +02:00
|
|
|
// WebPDecBuffer
|
|
|
|
|
2011-07-13 18:08:58 +02:00
|
|
|
// Number of bytes per pixel for the different color-spaces.
|
|
|
|
static const int kModeBpp[MODE_LAST] = { 3, 4, 3, 4, 4, 2, 2, 1, 1 };
|
|
|
|
|
2011-11-14 21:44:51 +01:00
|
|
|
// Check that webp_csp_mode is within the bounds of WEBP_CSP_MODE.
|
|
|
|
// Convert to an integer to handle both the unsigned/signed enum cases
|
|
|
|
// without the need for casting to remove type limit warnings.
|
|
|
|
static int IsValidColorspace(int webp_csp_mode) {
|
|
|
|
return (webp_csp_mode >= MODE_RGB && webp_csp_mode < MODE_LAST);
|
|
|
|
}
|
|
|
|
|
2011-06-20 09:45:15 +02:00
|
|
|
static VP8StatusCode CheckDecBuffer(const WebPDecBuffer* const buffer) {
|
|
|
|
int ok = 1;
|
|
|
|
WEBP_CSP_MODE mode = buffer->colorspace;
|
|
|
|
const int width = buffer->width;
|
|
|
|
const int height = buffer->height;
|
2011-11-14 21:44:51 +01:00
|
|
|
if (!IsValidColorspace(mode)) {
|
|
|
|
ok = 0;
|
|
|
|
} else if (mode >= MODE_YUV) { // YUV checks
|
2011-06-20 09:45:15 +02:00
|
|
|
const WebPYUVABuffer* const buf = &buffer->u.YUVA;
|
|
|
|
const int size = buf->y_stride * height;
|
|
|
|
const int u_size = buf->u_stride * ((height + 1) / 2);
|
|
|
|
const int v_size = buf->v_stride * ((height + 1) / 2);
|
|
|
|
const int a_size = buf->a_stride * height;
|
|
|
|
ok &= (size <= buf->y_size);
|
|
|
|
ok &= (u_size <= buf->u_size);
|
|
|
|
ok &= (v_size <= buf->v_size);
|
|
|
|
ok &= (a_size <= buf->a_size);
|
|
|
|
ok &= (buf->y_stride >= width);
|
|
|
|
ok &= (buf->u_stride >= (width + 1) / 2);
|
|
|
|
ok &= (buf->v_stride >= (width + 1) / 2);
|
|
|
|
if (buf->a) {
|
|
|
|
ok &= (buf->a_stride >= width);
|
|
|
|
}
|
|
|
|
} else { // RGB checks
|
|
|
|
const WebPRGBABuffer* const buf = &buffer->u.RGBA;
|
|
|
|
ok &= (buf->stride * height <= buf->size);
|
2011-07-13 18:08:58 +02:00
|
|
|
ok &= (buf->stride >= width * kModeBpp[mode]);
|
2011-06-20 09:45:15 +02:00
|
|
|
}
|
|
|
|
return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) {
|
|
|
|
const int w = buffer->width;
|
|
|
|
const int h = buffer->height;
|
|
|
|
|
2011-11-14 21:44:51 +01:00
|
|
|
if (w <= 0 || h <= 0 || !IsValidColorspace(buffer->colorspace)) {
|
2011-06-20 09:45:15 +02:00
|
|
|
return VP8_STATUS_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
|
2011-07-15 20:35:36 +02:00
|
|
|
if (!buffer->is_external_memory && buffer->private_memory == NULL) {
|
2011-06-20 09:45:15 +02:00
|
|
|
uint8_t* output;
|
|
|
|
WEBP_CSP_MODE mode = buffer->colorspace;
|
|
|
|
int stride;
|
|
|
|
int uv_stride = 0, a_stride = 0;
|
|
|
|
int uv_size = 0;
|
|
|
|
uint64_t size, a_size = 0, total_size;
|
|
|
|
// We need memory and it hasn't been allocated yet.
|
|
|
|
// => initialize output buffer, now that dimensions are known.
|
2011-07-13 18:08:58 +02:00
|
|
|
stride = w * kModeBpp[mode];
|
2011-06-20 09:45:15 +02:00
|
|
|
size = (uint64_t)stride * h;
|
|
|
|
|
|
|
|
if (mode >= MODE_YUV) {
|
|
|
|
uv_stride = (w + 1) / 2;
|
|
|
|
uv_size = (uint64_t)uv_stride * ((h + 1) / 2);
|
|
|
|
if (mode == MODE_YUVA) {
|
|
|
|
a_stride = w;
|
|
|
|
a_size = (uint64_t)a_stride * h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
total_size = size + 2 * uv_size + a_size;
|
|
|
|
|
|
|
|
// Security/sanity checks
|
|
|
|
if (((size_t)total_size != total_size) || (total_size >= (1ULL << 40))) {
|
|
|
|
return VP8_STATUS_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
|
2011-07-15 20:35:36 +02:00
|
|
|
buffer->private_memory = output = (uint8_t*)malloc((size_t)total_size);
|
2011-06-20 09:45:15 +02:00
|
|
|
if (output == NULL) {
|
|
|
|
return VP8_STATUS_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode >= MODE_YUV) { // YUVA initialization
|
|
|
|
WebPYUVABuffer* const buf = &buffer->u.YUVA;
|
|
|
|
buf->y = output;
|
|
|
|
buf->y_stride = stride;
|
2011-12-21 23:43:52 +01:00
|
|
|
buf->y_size = (int)size;
|
2011-06-20 09:45:15 +02:00
|
|
|
buf->u = output + size;
|
|
|
|
buf->u_stride = uv_stride;
|
|
|
|
buf->u_size = uv_size;
|
|
|
|
buf->v = output + size + uv_size;
|
|
|
|
buf->v_stride = uv_stride;
|
|
|
|
buf->v_size = uv_size;
|
|
|
|
if (mode == MODE_YUVA) {
|
|
|
|
buf->a = output + size + 2 * uv_size;
|
|
|
|
}
|
2011-12-21 23:43:52 +01:00
|
|
|
buf->a_size = (int)a_size;
|
2011-06-20 09:45:15 +02:00
|
|
|
buf->a_stride = a_stride;
|
|
|
|
} else { // RGBA initialization
|
|
|
|
WebPRGBABuffer* const buf = &buffer->u.RGBA;
|
|
|
|
buf->rgba = output;
|
|
|
|
buf->stride = stride;
|
2011-12-21 23:43:52 +01:00
|
|
|
buf->size = (int)size;
|
2011-06-20 09:45:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return CheckDecBuffer(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
VP8StatusCode WebPAllocateDecBuffer(int w, int h,
|
|
|
|
const WebPDecoderOptions* const options,
|
|
|
|
WebPDecBuffer* const out) {
|
|
|
|
if (out == NULL || w <= 0 || h <= 0) {
|
|
|
|
return VP8_STATUS_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
if (options != NULL) { // First, apply options if there is any.
|
|
|
|
if (options->use_cropping) {
|
|
|
|
const int cw = options->crop_width;
|
|
|
|
const int ch = options->crop_height;
|
|
|
|
const int x = options->crop_left & ~1;
|
|
|
|
const int y = options->crop_top & ~1;
|
|
|
|
if (x < 0 || y < 0 || cw <= 0 || ch <= 0 || x + cw > w || y + ch > h) {
|
|
|
|
return VP8_STATUS_INVALID_PARAM; // out of frame boundary.
|
|
|
|
}
|
|
|
|
w = cw;
|
|
|
|
h = ch;
|
|
|
|
}
|
|
|
|
if (options->use_scaling) {
|
|
|
|
if (options->scaled_width <= 0 || options->scaled_height <= 0) {
|
|
|
|
return VP8_STATUS_INVALID_PARAM;
|
|
|
|
}
|
|
|
|
w = options->scaled_width;
|
|
|
|
h = options->scaled_height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out->width = w;
|
|
|
|
out->height = h;
|
|
|
|
|
|
|
|
// Then, allocate buffer for real
|
|
|
|
return AllocateBuffer(out);
|
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-06-20 09:45:15 +02:00
|
|
|
// constructors / destructors
|
|
|
|
|
|
|
|
int WebPInitDecBufferInternal(WebPDecBuffer* const buffer, int version) {
|
|
|
|
if (version != WEBP_DECODER_ABI_VERSION) return 0; // version mismatch
|
|
|
|
if (!buffer) return 0;
|
|
|
|
memset(buffer, 0, sizeof(*buffer));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebPFreeDecBuffer(WebPDecBuffer* const buffer) {
|
|
|
|
if (buffer) {
|
|
|
|
if (!buffer->is_external_memory)
|
2011-07-15 20:35:36 +02:00
|
|
|
free(buffer->private_memory);
|
|
|
|
buffer->private_memory = NULL;
|
2011-06-20 09:45:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebPCopyDecBuffer(const WebPDecBuffer* const src,
|
|
|
|
WebPDecBuffer* const dst) {
|
|
|
|
if (src && dst) {
|
|
|
|
*dst = *src;
|
2011-07-15 20:35:36 +02:00
|
|
|
if (src->private_memory) {
|
2011-06-20 09:45:15 +02:00
|
|
|
dst->is_external_memory = 1; // dst buffer doesn't own the memory.
|
2011-07-15 20:35:36 +02:00
|
|
|
dst->private_memory = NULL;
|
2011-06-20 09:45:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy and transfer ownership from src to dst (beware of parameter order!)
|
|
|
|
void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst) {
|
|
|
|
if (src && dst) {
|
|
|
|
*dst = *src;
|
2011-07-15 20:35:36 +02:00
|
|
|
if (src->private_memory) {
|
2011-06-20 09:45:15 +02:00
|
|
|
src->is_external_memory = 1; // src relinquishes ownership
|
2011-07-15 20:35:36 +02:00
|
|
|
src->private_memory = NULL;
|
2011-06-20 09:45:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 23:22:32 +02:00
|
|
|
//------------------------------------------------------------------------------
|
2011-06-20 09:45:15 +02:00
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|