Path: blob/master/thirdparty/libwebp/src/dec/buffer_dec.c
9912 views
// Copyright 2011 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Everything about WebPDecBuffer10//11// Author: Skal ([email protected])1213#include <stdlib.h>1415#include "src/dec/vp8i_dec.h"16#include "src/dec/webpi_dec.h"17#include "src/utils/utils.h"1819//------------------------------------------------------------------------------20// WebPDecBuffer2122// Number of bytes per pixel for the different color-spaces.23static const uint8_t kModeBpp[MODE_LAST] = {243, 4, 3, 4, 4, 2, 2,254, 4, 4, 2, // pre-multiplied modes261, 1 };2728// Check that webp_csp_mode is within the bounds of WEBP_CSP_MODE.29// Convert to an integer to handle both the unsigned/signed enum cases30// without the need for casting to remove type limit warnings.31static int IsValidColorspace(int webp_csp_mode) {32return (webp_csp_mode >= MODE_RGB && webp_csp_mode < MODE_LAST);33}3435// strictly speaking, the very last (or first, if flipped) row36// doesn't require padding.37#define MIN_BUFFER_SIZE(WIDTH, HEIGHT, STRIDE) \38((uint64_t)(STRIDE) * ((HEIGHT) - 1) + (WIDTH))3940static VP8StatusCode CheckDecBuffer(const WebPDecBuffer* const buffer) {41int ok = 1;42const WEBP_CSP_MODE mode = buffer->colorspace;43const int width = buffer->width;44const int height = buffer->height;45if (!IsValidColorspace(mode)) {46ok = 0;47} else if (!WebPIsRGBMode(mode)) { // YUV checks48const WebPYUVABuffer* const buf = &buffer->u.YUVA;49const int uv_width = (width + 1) / 2;50const int uv_height = (height + 1) / 2;51const int y_stride = abs(buf->y_stride);52const int u_stride = abs(buf->u_stride);53const int v_stride = abs(buf->v_stride);54const int a_stride = abs(buf->a_stride);55const uint64_t y_size = MIN_BUFFER_SIZE(width, height, y_stride);56const uint64_t u_size = MIN_BUFFER_SIZE(uv_width, uv_height, u_stride);57const uint64_t v_size = MIN_BUFFER_SIZE(uv_width, uv_height, v_stride);58const uint64_t a_size = MIN_BUFFER_SIZE(width, height, a_stride);59ok &= (y_size <= buf->y_size);60ok &= (u_size <= buf->u_size);61ok &= (v_size <= buf->v_size);62ok &= (y_stride >= width);63ok &= (u_stride >= uv_width);64ok &= (v_stride >= uv_width);65ok &= (buf->y != NULL);66ok &= (buf->u != NULL);67ok &= (buf->v != NULL);68if (mode == MODE_YUVA) {69ok &= (a_stride >= width);70ok &= (a_size <= buf->a_size);71ok &= (buf->a != NULL);72}73} else { // RGB checks74const WebPRGBABuffer* const buf = &buffer->u.RGBA;75const int stride = abs(buf->stride);76const uint64_t size =77MIN_BUFFER_SIZE((uint64_t)width * kModeBpp[mode], height, stride);78ok &= (size <= buf->size);79ok &= (stride >= width * kModeBpp[mode]);80ok &= (buf->rgba != NULL);81}82return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM;83}84#undef MIN_BUFFER_SIZE8586static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) {87const int w = buffer->width;88const int h = buffer->height;89const WEBP_CSP_MODE mode = buffer->colorspace;9091if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) {92return VP8_STATUS_INVALID_PARAM;93}9495if (buffer->is_external_memory <= 0 && buffer->private_memory == NULL) {96uint8_t* output;97int uv_stride = 0, a_stride = 0;98uint64_t uv_size = 0, a_size = 0, total_size;99// We need memory and it hasn't been allocated yet.100// => initialize output buffer, now that dimensions are known.101int stride;102uint64_t size;103104if ((uint64_t)w * kModeBpp[mode] >= (1ull << 31)) {105return VP8_STATUS_INVALID_PARAM;106}107stride = w * kModeBpp[mode];108size = (uint64_t)stride * h;109if (!WebPIsRGBMode(mode)) {110uv_stride = (w + 1) / 2;111uv_size = (uint64_t)uv_stride * ((h + 1) / 2);112if (mode == MODE_YUVA) {113a_stride = w;114a_size = (uint64_t)a_stride * h;115}116}117total_size = size + 2 * uv_size + a_size;118119output = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*output));120if (output == NULL) {121return VP8_STATUS_OUT_OF_MEMORY;122}123buffer->private_memory = output;124125if (!WebPIsRGBMode(mode)) { // YUVA initialization126WebPYUVABuffer* const buf = &buffer->u.YUVA;127buf->y = output;128buf->y_stride = stride;129buf->y_size = (size_t)size;130buf->u = output + size;131buf->u_stride = uv_stride;132buf->u_size = (size_t)uv_size;133buf->v = output + size + uv_size;134buf->v_stride = uv_stride;135buf->v_size = (size_t)uv_size;136if (mode == MODE_YUVA) {137buf->a = output + size + 2 * uv_size;138}139buf->a_size = (size_t)a_size;140buf->a_stride = a_stride;141} else { // RGBA initialization142WebPRGBABuffer* const buf = &buffer->u.RGBA;143buf->rgba = output;144buf->stride = stride;145buf->size = (size_t)size;146}147}148return CheckDecBuffer(buffer);149}150151VP8StatusCode WebPFlipBuffer(WebPDecBuffer* const buffer) {152if (buffer == NULL) {153return VP8_STATUS_INVALID_PARAM;154}155if (WebPIsRGBMode(buffer->colorspace)) {156WebPRGBABuffer* const buf = &buffer->u.RGBA;157buf->rgba += (int64_t)(buffer->height - 1) * buf->stride;158buf->stride = -buf->stride;159} else {160WebPYUVABuffer* const buf = &buffer->u.YUVA;161const int64_t H = buffer->height;162buf->y += (H - 1) * buf->y_stride;163buf->y_stride = -buf->y_stride;164buf->u += ((H - 1) >> 1) * buf->u_stride;165buf->u_stride = -buf->u_stride;166buf->v += ((H - 1) >> 1) * buf->v_stride;167buf->v_stride = -buf->v_stride;168if (buf->a != NULL) {169buf->a += (H - 1) * buf->a_stride;170buf->a_stride = -buf->a_stride;171}172}173return VP8_STATUS_OK;174}175176VP8StatusCode WebPAllocateDecBuffer(int width, int height,177const WebPDecoderOptions* const options,178WebPDecBuffer* const buffer) {179VP8StatusCode status;180if (buffer == NULL || width <= 0 || height <= 0) {181return VP8_STATUS_INVALID_PARAM;182}183if (options != NULL) { // First, apply options if there is any.184if (options->use_cropping) {185const int cw = options->crop_width;186const int ch = options->crop_height;187const int x = options->crop_left & ~1;188const int y = options->crop_top & ~1;189if (!WebPCheckCropDimensions(width, height, x, y, cw, ch)) {190return VP8_STATUS_INVALID_PARAM; // out of frame boundary.191}192width = cw;193height = ch;194}195196if (options->use_scaling) {197#if !defined(WEBP_REDUCE_SIZE)198int scaled_width = options->scaled_width;199int scaled_height = options->scaled_height;200if (!WebPRescalerGetScaledDimensions(201width, height, &scaled_width, &scaled_height)) {202return VP8_STATUS_INVALID_PARAM;203}204width = scaled_width;205height = scaled_height;206#else207return VP8_STATUS_INVALID_PARAM; // rescaling not supported208#endif209}210}211buffer->width = width;212buffer->height = height;213214// Then, allocate buffer for real.215status = AllocateBuffer(buffer);216if (status != VP8_STATUS_OK) return status;217218// Use the stride trick if vertical flip is needed.219if (options != NULL && options->flip) {220status = WebPFlipBuffer(buffer);221}222return status;223}224225//------------------------------------------------------------------------------226// constructors / destructors227228int WebPInitDecBufferInternal(WebPDecBuffer* buffer, int version) {229if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {230return 0; // version mismatch231}232if (buffer == NULL) return 0;233memset(buffer, 0, sizeof(*buffer));234return 1;235}236237void WebPFreeDecBuffer(WebPDecBuffer* buffer) {238if (buffer != NULL) {239if (buffer->is_external_memory <= 0) {240WebPSafeFree(buffer->private_memory);241}242buffer->private_memory = NULL;243}244}245246void WebPCopyDecBuffer(const WebPDecBuffer* const src,247WebPDecBuffer* const dst) {248if (src != NULL && dst != NULL) {249*dst = *src;250if (src->private_memory != NULL) {251dst->is_external_memory = 1; // dst buffer doesn't own the memory.252dst->private_memory = NULL;253}254}255}256257// Copy and transfer ownership from src to dst (beware of parameter order!)258void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst) {259if (src != NULL && dst != NULL) {260*dst = *src;261if (src->private_memory != NULL) {262src->is_external_memory = 1; // src relinquishes ownership263src->private_memory = NULL;264}265}266}267268VP8StatusCode WebPCopyDecBufferPixels(const WebPDecBuffer* const src_buf,269WebPDecBuffer* const dst_buf) {270assert(src_buf != NULL && dst_buf != NULL);271assert(src_buf->colorspace == dst_buf->colorspace);272273dst_buf->width = src_buf->width;274dst_buf->height = src_buf->height;275if (CheckDecBuffer(dst_buf) != VP8_STATUS_OK) {276return VP8_STATUS_INVALID_PARAM;277}278if (WebPIsRGBMode(src_buf->colorspace)) {279const WebPRGBABuffer* const src = &src_buf->u.RGBA;280const WebPRGBABuffer* const dst = &dst_buf->u.RGBA;281WebPCopyPlane(src->rgba, src->stride, dst->rgba, dst->stride,282src_buf->width * kModeBpp[src_buf->colorspace],283src_buf->height);284} else {285const WebPYUVABuffer* const src = &src_buf->u.YUVA;286const WebPYUVABuffer* const dst = &dst_buf->u.YUVA;287WebPCopyPlane(src->y, src->y_stride, dst->y, dst->y_stride,288src_buf->width, src_buf->height);289WebPCopyPlane(src->u, src->u_stride, dst->u, dst->u_stride,290(src_buf->width + 1) / 2, (src_buf->height + 1) / 2);291WebPCopyPlane(src->v, src->v_stride, dst->v, dst->v_stride,292(src_buf->width + 1) / 2, (src_buf->height + 1) / 2);293if (WebPIsAlphaMode(src_buf->colorspace)) {294WebPCopyPlane(src->a, src->a_stride, dst->a, dst->a_stride,295src_buf->width, src_buf->height);296}297}298return VP8_STATUS_OK;299}300301int WebPAvoidSlowMemory(const WebPDecBuffer* const output,302const WebPBitstreamFeatures* const features) {303assert(output != NULL);304return (output->is_external_memory >= 2) &&305WebPIsPremultipliedMode(output->colorspace) &&306(features != NULL && features->has_alpha);307}308309//------------------------------------------------------------------------------310311312