Home | History | Annotate | Download | only in dec
      1 // Copyright 2011 Google Inc. All Rights Reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style license
      4 // that can be found in the COPYING file in the root of the source
      5 // tree. An additional intellectual property rights grant can be found
      6 // in the file PATENTS. All contributing project authors may
      7 // be found in the AUTHORS file in the root of the source tree.
      8 // -----------------------------------------------------------------------------
      9 //
     10 // Everything about WebPDecBuffer
     11 //
     12 // Author: Skal (pascal.massimino (at) gmail.com)
     13 
     14 #include <stdlib.h>
     15 
     16 #include "src/dec/vp8i_dec.h"
     17 #include "src/dec/webpi_dec.h"
     18 #include "src/utils/utils.h"
     19 
     20 //------------------------------------------------------------------------------
     21 // WebPDecBuffer
     22 
     23 // Number of bytes per pixel for the different color-spaces.
     24 static const uint8_t kModeBpp[MODE_LAST] = {
     25   3, 4, 3, 4, 4, 2, 2,
     26   4, 4, 4, 2,    // pre-multiplied modes
     27   1, 1 };
     28 
     29 // Check that webp_csp_mode is within the bounds of WEBP_CSP_MODE.
     30 // Convert to an integer to handle both the unsigned/signed enum cases
     31 // without the need for casting to remove type limit warnings.
     32 static int IsValidColorspace(int webp_csp_mode) {
     33   return (webp_csp_mode >= MODE_RGB && webp_csp_mode < MODE_LAST);
     34 }
     35 
     36 // strictly speaking, the very last (or first, if flipped) row
     37 // doesn't require padding.
     38 #define MIN_BUFFER_SIZE(WIDTH, HEIGHT, STRIDE)       \
     39     ((uint64_t)(STRIDE) * ((HEIGHT) - 1) + (WIDTH))
     40 
     41 static VP8StatusCode CheckDecBuffer(const WebPDecBuffer* const buffer) {
     42   int ok = 1;
     43   const WEBP_CSP_MODE mode = buffer->colorspace;
     44   const int width = buffer->width;
     45   const int height = buffer->height;
     46   if (!IsValidColorspace(mode)) {
     47     ok = 0;
     48   } else if (!WebPIsRGBMode(mode)) {   // YUV checks
     49     const WebPYUVABuffer* const buf = &buffer->u.YUVA;
     50     const int uv_width  = (width  + 1) / 2;
     51     const int uv_height = (height + 1) / 2;
     52     const int y_stride = abs(buf->y_stride);
     53     const int u_stride = abs(buf->u_stride);
     54     const int v_stride = abs(buf->v_stride);
     55     const int a_stride = abs(buf->a_stride);
     56     const uint64_t y_size = MIN_BUFFER_SIZE(width, height, y_stride);
     57     const uint64_t u_size = MIN_BUFFER_SIZE(uv_width, uv_height, u_stride);
     58     const uint64_t v_size = MIN_BUFFER_SIZE(uv_width, uv_height, v_stride);
     59     const uint64_t a_size = MIN_BUFFER_SIZE(width, height, a_stride);
     60     ok &= (y_size <= buf->y_size);
     61     ok &= (u_size <= buf->u_size);
     62     ok &= (v_size <= buf->v_size);
     63     ok &= (y_stride >= width);
     64     ok &= (u_stride >= uv_width);
     65     ok &= (v_stride >= uv_width);
     66     ok &= (buf->y != NULL);
     67     ok &= (buf->u != NULL);
     68     ok &= (buf->v != NULL);
     69     if (mode == MODE_YUVA) {
     70       ok &= (a_stride >= width);
     71       ok &= (a_size <= buf->a_size);
     72       ok &= (buf->a != NULL);
     73     }
     74   } else {    // RGB checks
     75     const WebPRGBABuffer* const buf = &buffer->u.RGBA;
     76     const int stride = abs(buf->stride);
     77     const uint64_t size =
     78         MIN_BUFFER_SIZE(width * kModeBpp[mode], height, stride);
     79     ok &= (size <= buf->size);
     80     ok &= (stride >= width * kModeBpp[mode]);
     81     ok &= (buf->rgba != NULL);
     82   }
     83   return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM;
     84 }
     85 #undef MIN_BUFFER_SIZE
     86 
     87 static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) {
     88   const int w = buffer->width;
     89   const int h = buffer->height;
     90   const WEBP_CSP_MODE mode = buffer->colorspace;
     91 
     92   if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) {
     93     return VP8_STATUS_INVALID_PARAM;
     94   }
     95 
     96   if (buffer->is_external_memory <= 0 && buffer->private_memory == NULL) {
     97     uint8_t* output;
     98     int uv_stride = 0, a_stride = 0;
     99     uint64_t uv_size = 0, a_size = 0, total_size;
    100     // We need memory and it hasn't been allocated yet.
    101     // => initialize output buffer, now that dimensions are known.
    102     int stride;
    103     uint64_t size;
    104 
    105     if ((uint64_t)w * kModeBpp[mode] >= (1ull << 32)) {
    106       return VP8_STATUS_INVALID_PARAM;
    107     }
    108     stride = w * kModeBpp[mode];
    109     size = (uint64_t)stride * h;
    110     if (!WebPIsRGBMode(mode)) {
    111       uv_stride = (w + 1) / 2;
    112       uv_size = (uint64_t)uv_stride * ((h + 1) / 2);
    113       if (mode == MODE_YUVA) {
    114         a_stride = w;
    115         a_size = (uint64_t)a_stride * h;
    116       }
    117     }
    118     total_size = size + 2 * uv_size + a_size;
    119 
    120     // Security/sanity checks
    121     output = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*output));
    122     if (output == NULL) {
    123       return VP8_STATUS_OUT_OF_MEMORY;
    124     }
    125     buffer->private_memory = output;
    126 
    127     if (!WebPIsRGBMode(mode)) {   // YUVA initialization
    128       WebPYUVABuffer* const buf = &buffer->u.YUVA;
    129       buf->y = output;
    130       buf->y_stride = stride;
    131       buf->y_size = (size_t)size;
    132       buf->u = output + size;
    133       buf->u_stride = uv_stride;
    134       buf->u_size = (size_t)uv_size;
    135       buf->v = output + size + uv_size;
    136       buf->v_stride = uv_stride;
    137       buf->v_size = (size_t)uv_size;
    138       if (mode == MODE_YUVA) {
    139         buf->a = output + size + 2 * uv_size;
    140       }
    141       buf->a_size = (size_t)a_size;
    142       buf->a_stride = a_stride;
    143     } else {  // RGBA initialization
    144       WebPRGBABuffer* const buf = &buffer->u.RGBA;
    145       buf->rgba = output;
    146       buf->stride = stride;
    147       buf->size = (size_t)size;
    148     }
    149   }
    150   return CheckDecBuffer(buffer);
    151 }
    152 
    153 VP8StatusCode WebPFlipBuffer(WebPDecBuffer* const buffer) {
    154   if (buffer == NULL) {
    155     return VP8_STATUS_INVALID_PARAM;
    156   }
    157   if (WebPIsRGBMode(buffer->colorspace)) {
    158     WebPRGBABuffer* const buf = &buffer->u.RGBA;
    159     buf->rgba += (buffer->height - 1) * buf->stride;
    160     buf->stride = -buf->stride;
    161   } else {
    162     WebPYUVABuffer* const buf = &buffer->u.YUVA;
    163     const int H = buffer->height;
    164     buf->y += (H - 1) * buf->y_stride;
    165     buf->y_stride = -buf->y_stride;
    166     buf->u += ((H - 1) >> 1) * buf->u_stride;
    167     buf->u_stride = -buf->u_stride;
    168     buf->v += ((H - 1) >> 1) * buf->v_stride;
    169     buf->v_stride = -buf->v_stride;
    170     if (buf->a != NULL) {
    171       buf->a += (H - 1) * buf->a_stride;
    172       buf->a_stride = -buf->a_stride;
    173     }
    174   }
    175   return VP8_STATUS_OK;
    176 }
    177 
    178 VP8StatusCode WebPAllocateDecBuffer(int width, int height,
    179                                     const WebPDecoderOptions* const options,
    180                                     WebPDecBuffer* const buffer) {
    181   VP8StatusCode status;
    182   if (buffer == NULL || width <= 0 || height <= 0) {
    183     return VP8_STATUS_INVALID_PARAM;
    184   }
    185   if (options != NULL) {    // First, apply options if there is any.
    186     if (options->use_cropping) {
    187       const int cw = options->crop_width;
    188       const int ch = options->crop_height;
    189       const int x = options->crop_left & ~1;
    190       const int y = options->crop_top & ~1;
    191       if (x < 0 || y < 0 || cw <= 0 || ch <= 0 ||
    192           x + cw > width || y + ch > height) {
    193         return VP8_STATUS_INVALID_PARAM;   // out of frame boundary.
    194       }
    195       width = cw;
    196       height = ch;
    197     }
    198 
    199     if (options->use_scaling) {
    200 #if !defined(WEBP_REDUCE_SIZE)
    201       int scaled_width = options->scaled_width;
    202       int scaled_height = options->scaled_height;
    203       if (!WebPRescalerGetScaledDimensions(
    204               width, height, &scaled_width, &scaled_height)) {
    205         return VP8_STATUS_INVALID_PARAM;
    206       }
    207       width = scaled_width;
    208       height = scaled_height;
    209 #else
    210       return VP8_STATUS_INVALID_PARAM;   // rescaling not supported
    211 #endif
    212     }
    213   }
    214   buffer->width = width;
    215   buffer->height = height;
    216 
    217   // Then, allocate buffer for real.
    218   status = AllocateBuffer(buffer);
    219   if (status != VP8_STATUS_OK) return status;
    220 
    221   // Use the stride trick if vertical flip is needed.
    222   if (options != NULL && options->flip) {
    223     status = WebPFlipBuffer(buffer);
    224   }
    225   return status;
    226 }
    227 
    228 //------------------------------------------------------------------------------
    229 // constructors / destructors
    230 
    231 int WebPInitDecBufferInternal(WebPDecBuffer* buffer, int version) {
    232   if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
    233     return 0;  // version mismatch
    234   }
    235   if (buffer == NULL) return 0;
    236   memset(buffer, 0, sizeof(*buffer));
    237   return 1;
    238 }
    239 
    240 void WebPFreeDecBuffer(WebPDecBuffer* buffer) {
    241   if (buffer != NULL) {
    242     if (buffer->is_external_memory <= 0) {
    243       WebPSafeFree(buffer->private_memory);
    244     }
    245     buffer->private_memory = NULL;
    246   }
    247 }
    248 
    249 void WebPCopyDecBuffer(const WebPDecBuffer* const src,
    250                        WebPDecBuffer* const dst) {
    251   if (src != NULL && dst != NULL) {
    252     *dst = *src;
    253     if (src->private_memory != NULL) {
    254       dst->is_external_memory = 1;   // dst buffer doesn't own the memory.
    255       dst->private_memory = NULL;
    256     }
    257   }
    258 }
    259 
    260 // Copy and transfer ownership from src to dst (beware of parameter order!)
    261 void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst) {
    262   if (src != NULL && dst != NULL) {
    263     *dst = *src;
    264     if (src->private_memory != NULL) {
    265       src->is_external_memory = 1;   // src relinquishes ownership
    266       src->private_memory = NULL;
    267     }
    268   }
    269 }
    270 
    271 VP8StatusCode WebPCopyDecBufferPixels(const WebPDecBuffer* const src_buf,
    272                                       WebPDecBuffer* const dst_buf) {
    273   assert(src_buf != NULL && dst_buf != NULL);
    274   assert(src_buf->colorspace == dst_buf->colorspace);
    275 
    276   dst_buf->width = src_buf->width;
    277   dst_buf->height = src_buf->height;
    278   if (CheckDecBuffer(dst_buf) != VP8_STATUS_OK) {
    279     return VP8_STATUS_INVALID_PARAM;
    280   }
    281   if (WebPIsRGBMode(src_buf->colorspace)) {
    282     const WebPRGBABuffer* const src = &src_buf->u.RGBA;
    283     const WebPRGBABuffer* const dst = &dst_buf->u.RGBA;
    284     WebPCopyPlane(src->rgba, src->stride, dst->rgba, dst->stride,
    285                   src_buf->width * kModeBpp[src_buf->colorspace],
    286                   src_buf->height);
    287   } else {
    288     const WebPYUVABuffer* const src = &src_buf->u.YUVA;
    289     const WebPYUVABuffer* const dst = &dst_buf->u.YUVA;
    290     WebPCopyPlane(src->y, src->y_stride, dst->y, dst->y_stride,
    291                   src_buf->width, src_buf->height);
    292     WebPCopyPlane(src->u, src->u_stride, dst->u, dst->u_stride,
    293                   (src_buf->width + 1) / 2, (src_buf->height + 1) / 2);
    294     WebPCopyPlane(src->v, src->v_stride, dst->v, dst->v_stride,
    295                   (src_buf->width + 1) / 2, (src_buf->height + 1) / 2);
    296     if (WebPIsAlphaMode(src_buf->colorspace)) {
    297       WebPCopyPlane(src->a, src->a_stride, dst->a, dst->a_stride,
    298                     src_buf->width, src_buf->height);
    299     }
    300   }
    301   return VP8_STATUS_OK;
    302 }
    303 
    304 int WebPAvoidSlowMemory(const WebPDecBuffer* const output,
    305                         const WebPBitstreamFeatures* const features) {
    306   assert(output != NULL);
    307   return (output->is_external_memory >= 2) &&
    308          WebPIsPremultipliedMode(output->colorspace) &&
    309          (features != NULL && features->has_alpha);
    310 }
    311 
    312 //------------------------------------------------------------------------------
    313