Path: blob/master/3rdparty/libwebp/src/dec/buffer_dec.c
16358 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 = MIN_BUFFER_SIZE(width, height, stride);77ok &= (size <= buf->size);78ok &= (stride >= width * kModeBpp[mode]);79ok &= (buf->rgba != NULL);80}81return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM;82}83#undef MIN_BUFFER_SIZE8485static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) {86const int w = buffer->width;87const int h = buffer->height;88const WEBP_CSP_MODE mode = buffer->colorspace;8990if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) {91return VP8_STATUS_INVALID_PARAM;92}9394if (buffer->is_external_memory <= 0 && buffer->private_memory == NULL) {95uint8_t* output;96int uv_stride = 0, a_stride = 0;97uint64_t uv_size = 0, a_size = 0, total_size;98// We need memory and it hasn't been allocated yet.99// => initialize output buffer, now that dimensions are known.100int stride;101uint64_t size;102103if ((uint64_t)w * kModeBpp[mode] >= (1ull << 32)) {104return VP8_STATUS_INVALID_PARAM;105}106stride = w * kModeBpp[mode];107size = (uint64_t)stride * h;108if (!WebPIsRGBMode(mode)) {109uv_stride = (w + 1) / 2;110uv_size = (uint64_t)uv_stride * ((h + 1) / 2);111if (mode == MODE_YUVA) {112a_stride = w;113a_size = (uint64_t)a_stride * h;114}115}116total_size = size + 2 * uv_size + a_size;117118// Security/sanity checks119output = (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 += (buffer->height - 1) * buf->stride;158buf->stride = -buf->stride;159} else {160WebPYUVABuffer* const buf = &buffer->u.YUVA;161const int 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 (x < 0 || y < 0 || cw <= 0 || ch <= 0 ||190x + cw > width || y + ch > height) {191return VP8_STATUS_INVALID_PARAM; // out of frame boundary.192}193width = cw;194height = ch;195}196197if (options->use_scaling) {198#if !defined(WEBP_REDUCE_SIZE)199int scaled_width = options->scaled_width;200int scaled_height = options->scaled_height;201if (!WebPRescalerGetScaledDimensions(202width, height, &scaled_width, &scaled_height)) {203return VP8_STATUS_INVALID_PARAM;204}205width = scaled_width;206height = scaled_height;207#else208return VP8_STATUS_INVALID_PARAM; // rescaling not supported209#endif210}211}212buffer->width = width;213buffer->height = height;214215// Then, allocate buffer for real.216status = AllocateBuffer(buffer);217if (status != VP8_STATUS_OK) return status;218219// Use the stride trick if vertical flip is needed.220if (options != NULL && options->flip) {221status = WebPFlipBuffer(buffer);222}223return status;224}225226//------------------------------------------------------------------------------227// constructors / destructors228229int WebPInitDecBufferInternal(WebPDecBuffer* buffer, int version) {230if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {231return 0; // version mismatch232}233if (buffer == NULL) return 0;234memset(buffer, 0, sizeof(*buffer));235return 1;236}237238void WebPFreeDecBuffer(WebPDecBuffer* buffer) {239if (buffer != NULL) {240if (buffer->is_external_memory <= 0) {241WebPSafeFree(buffer->private_memory);242}243buffer->private_memory = NULL;244}245}246247void WebPCopyDecBuffer(const WebPDecBuffer* const src,248WebPDecBuffer* const dst) {249if (src != NULL && dst != NULL) {250*dst = *src;251if (src->private_memory != NULL) {252dst->is_external_memory = 1; // dst buffer doesn't own the memory.253dst->private_memory = NULL;254}255}256}257258// Copy and transfer ownership from src to dst (beware of parameter order!)259void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst) {260if (src != NULL && dst != NULL) {261*dst = *src;262if (src->private_memory != NULL) {263src->is_external_memory = 1; // src relinquishes ownership264src->private_memory = NULL;265}266}267}268269VP8StatusCode WebPCopyDecBufferPixels(const WebPDecBuffer* const src_buf,270WebPDecBuffer* const dst_buf) {271assert(src_buf != NULL && dst_buf != NULL);272assert(src_buf->colorspace == dst_buf->colorspace);273274dst_buf->width = src_buf->width;275dst_buf->height = src_buf->height;276if (CheckDecBuffer(dst_buf) != VP8_STATUS_OK) {277return VP8_STATUS_INVALID_PARAM;278}279if (WebPIsRGBMode(src_buf->colorspace)) {280const WebPRGBABuffer* const src = &src_buf->u.RGBA;281const WebPRGBABuffer* const dst = &dst_buf->u.RGBA;282WebPCopyPlane(src->rgba, src->stride, dst->rgba, dst->stride,283src_buf->width * kModeBpp[src_buf->colorspace],284src_buf->height);285} else {286const WebPYUVABuffer* const src = &src_buf->u.YUVA;287const WebPYUVABuffer* const dst = &dst_buf->u.YUVA;288WebPCopyPlane(src->y, src->y_stride, dst->y, dst->y_stride,289src_buf->width, src_buf->height);290WebPCopyPlane(src->u, src->u_stride, dst->u, dst->u_stride,291(src_buf->width + 1) / 2, (src_buf->height + 1) / 2);292WebPCopyPlane(src->v, src->v_stride, dst->v, dst->v_stride,293(src_buf->width + 1) / 2, (src_buf->height + 1) / 2);294if (WebPIsAlphaMode(src_buf->colorspace)) {295WebPCopyPlane(src->a, src->a_stride, dst->a, dst->a_stride,296src_buf->width, src_buf->height);297}298}299return VP8_STATUS_OK;300}301302int WebPAvoidSlowMemory(const WebPDecBuffer* const output,303const WebPBitstreamFeatures* const features) {304assert(output != NULL);305return (output->is_external_memory >= 2) &&306WebPIsPremultipliedMode(output->colorspace) &&307(features != NULL && features->has_alpha);308}309310//------------------------------------------------------------------------------311312313