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 "./vp8i_dec.h"
     17 #include "./webpi_dec.h"
     18 #include "../utils/utils.h"
     19 
     20 //------------------------------------------------------------------------------
     21 // WebPDecBuffer
     22 
     23 // Number of bytes per pixel for the different color-spaces.
     24 static const int 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 = MIN_BUFFER_SIZE(width, height, stride);
     78     ok &= (size <= buf->size);
     79     ok &= (stride >= width * kModeBpp[mode]);
     80     ok &= (buf->rgba != NULL);
     81   }
     82   return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM;
     83 }
     84 #undef MIN_BUFFER_SIZE
     85 
     86 static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) {
     87   const int w = buffer->width;
     88   const int h = buffer->height;
     89   const WEBP_CSP_MODE mode = buffer->colorspace;
     90 
     91   if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) {
     92     return VP8_STATUS_INVALID_PARAM;
     93   }
     94 
     95   if (buffer->is_external_memory <= 0 && buffer->private_memory == NULL) {
     96     uint8_t* output;
     97     int uv_stride = 0, a_stride = 0;
     98     uint64_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.
    101     const int stride = w * kModeBpp[mode];
    102     const uint64_t size = (uint64_t)stride * h;
    103 
    104     if (!WebPIsRGBMode(mode)) {
    105       uv_stride = (w + 1) / 2;
    106       uv_size = (uint64_t)uv_stride * ((h + 1) / 2);
    107       if (mode == MODE_YUVA) {
    108         a_stride = w;
    109         a_size = (uint64_t)a_stride * h;
    110       }
    111     }
    112     total_size = size + 2 * uv_size + a_size;
    113 
    114     // Security/sanity checks
    115     output = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*output));
    116     if (output == NULL) {
    117       return VP8_STATUS_OUT_OF_MEMORY;
    118     }
    119     buffer->private_memory = output;
    120 
    121     if (!WebPIsRGBMode(mode)) {   // YUVA initialization
    122       WebPYUVABuffer* const buf = &buffer->u.YUVA;
    123       buf->y = output;
    124       buf->y_stride = stride;
    125       buf->y_size = (size_t)size;
    126       buf->u = output + size;
    127       buf->u_stride = uv_stride;
    128       buf->u_size = (size_t)uv_size;
    129       buf->v = output + size + uv_size;
    130       buf->v_stride = uv_stride;
    131       buf->v_size = (size_t)uv_size;
    132       if (mode == MODE_YUVA) {
    133         buf->a = output + size + 2 * uv_size;
    134       }
    135       buf->a_size = (size_t)a_size;
    136       buf->a_stride = a_stride;
    137     } else {  // RGBA initialization
    138       WebPRGBABuffer* const buf = &buffer->u.RGBA;
    139       buf->rgba = output;
    140       buf->stride = stride;
    141       buf->size = (size_t)size;
    142     }
    143   }
    144   return CheckDecBuffer(buffer);
    145 }
    146 
    147 VP8StatusCode WebPFlipBuffer(WebPDecBuffer* const buffer) {
    148   if (buffer == NULL) {
    149     return VP8_STATUS_INVALID_PARAM;
    150   }
    151   if (WebPIsRGBMode(buffer->colorspace)) {
    152     WebPRGBABuffer* const buf = &buffer->u.RGBA;
    153     buf->rgba += (buffer->height - 1) * buf->stride;
    154     buf->stride = -buf->stride;
    155   } else {
    156     WebPYUVABuffer* const buf = &buffer->u.YUVA;
    157     const int H = buffer->height;
    158     buf->y += (H - 1) * buf->y_stride;
    159     buf->y_stride = -buf->y_stride;
    160     buf->u += ((H - 1) >> 1) * buf->u_stride;
    161     buf->u_stride = -buf->u_stride;
    162     buf->v += ((H - 1) >> 1) * buf->v_stride;
    163     buf->v_stride = -buf->v_stride;
    164     if (buf->a != NULL) {
    165       buf->a += (H - 1) * buf->a_stride;
    166       buf->a_stride = -buf->a_stride;
    167     }
    168   }
    169   return VP8_STATUS_OK;
    170 }
    171 
    172 VP8StatusCode WebPAllocateDecBuffer(int w, int h,
    173                                     const WebPDecoderOptions* const options,
    174                                     WebPDecBuffer* const out) {
    175   VP8StatusCode status;
    176   if (out == NULL || w <= 0 || h <= 0) {
    177     return VP8_STATUS_INVALID_PARAM;
    178   }
    179   if (options != NULL) {    // First, apply options if there is any.
    180     if (options->use_cropping) {
    181       const int cw = options->crop_width;
    182       const int ch = options->crop_height;
    183       const int x = options->crop_left & ~1;
    184       const int y = options->crop_top & ~1;
    185       if (x < 0 || y < 0 || cw <= 0 || ch <= 0 || x + cw > w || y + ch > h) {
    186         return VP8_STATUS_INVALID_PARAM;   // out of frame boundary.
    187       }
    188       w = cw;
    189       h = ch;
    190     }
    191     if (options->use_scaling) {
    192       int scaled_width = options->scaled_width;
    193       int scaled_height = options->scaled_height;
    194       if (!WebPRescalerGetScaledDimensions(
    195               w, h, &scaled_width, &scaled_height)) {
    196         return VP8_STATUS_INVALID_PARAM;
    197       }
    198       w = scaled_width;
    199       h = scaled_height;
    200     }
    201   }
    202   out->width = w;
    203   out->height = h;
    204 
    205   // Then, allocate buffer for real.
    206   status = AllocateBuffer(out);
    207   if (status != VP8_STATUS_OK) return status;
    208 
    209   // Use the stride trick if vertical flip is needed.
    210   if (options != NULL && options->flip) {
    211     status = WebPFlipBuffer(out);
    212   }
    213   return status;
    214 }
    215 
    216 //------------------------------------------------------------------------------
    217 // constructors / destructors
    218 
    219 int WebPInitDecBufferInternal(WebPDecBuffer* buffer, int version) {
    220   if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
    221     return 0;  // version mismatch
    222   }
    223   if (buffer == NULL) return 0;
    224   memset(buffer, 0, sizeof(*buffer));
    225   return 1;
    226 }
    227 
    228 void WebPFreeDecBuffer(WebPDecBuffer* buffer) {
    229   if (buffer != NULL) {
    230     if (buffer->is_external_memory <= 0) {
    231       WebPSafeFree(buffer->private_memory);
    232     }
    233     buffer->private_memory = NULL;
    234   }
    235 }
    236 
    237 void WebPCopyDecBuffer(const WebPDecBuffer* const src,
    238                        WebPDecBuffer* const dst) {
    239   if (src != NULL && dst != NULL) {
    240     *dst = *src;
    241     if (src->private_memory != NULL) {
    242       dst->is_external_memory = 1;   // dst buffer doesn't own the memory.
    243       dst->private_memory = NULL;
    244     }
    245   }
    246 }
    247 
    248 // Copy and transfer ownership from src to dst (beware of parameter order!)
    249 void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst) {
    250   if (src != NULL && dst != NULL) {
    251     *dst = *src;
    252     if (src->private_memory != NULL) {
    253       src->is_external_memory = 1;   // src relinquishes ownership
    254       src->private_memory = NULL;
    255     }
    256   }
    257 }
    258 
    259 VP8StatusCode WebPCopyDecBufferPixels(const WebPDecBuffer* const src_buf,
    260                                       WebPDecBuffer* const dst_buf) {
    261   assert(src_buf != NULL && dst_buf != NULL);
    262   assert(src_buf->colorspace == dst_buf->colorspace);
    263 
    264   dst_buf->width = src_buf->width;
    265   dst_buf->height = src_buf->height;
    266   if (CheckDecBuffer(dst_buf) != VP8_STATUS_OK) {
    267     return VP8_STATUS_INVALID_PARAM;
    268   }
    269   if (WebPIsRGBMode(src_buf->colorspace)) {
    270     const WebPRGBABuffer* const src = &src_buf->u.RGBA;
    271     const WebPRGBABuffer* const dst = &dst_buf->u.RGBA;
    272     WebPCopyPlane(src->rgba, src->stride, dst->rgba, dst->stride,
    273                   src_buf->width * kModeBpp[src_buf->colorspace],
    274                   src_buf->height);
    275   } else {
    276     const WebPYUVABuffer* const src = &src_buf->u.YUVA;
    277     const WebPYUVABuffer* const dst = &dst_buf->u.YUVA;
    278     WebPCopyPlane(src->y, src->y_stride, dst->y, dst->y_stride,
    279                   src_buf->width, src_buf->height);
    280     WebPCopyPlane(src->u, src->u_stride, dst->u, dst->u_stride,
    281                   (src_buf->width + 1) / 2, (src_buf->height + 1) / 2);
    282     WebPCopyPlane(src->v, src->v_stride, dst->v, dst->v_stride,
    283                   (src_buf->width + 1) / 2, (src_buf->height + 1) / 2);
    284     if (WebPIsAlphaMode(src_buf->colorspace)) {
    285       WebPCopyPlane(src->a, src->a_stride, dst->a, dst->a_stride,
    286                     src_buf->width, src_buf->height);
    287     }
    288   }
    289   return VP8_STATUS_OK;
    290 }
    291 
    292 int WebPAvoidSlowMemory(const WebPDecBuffer* const output,
    293                         const WebPBitstreamFeatures* const features) {
    294   assert(output != NULL);
    295   return (output->is_external_memory >= 2) &&
    296          WebPIsPremultipliedMode(output->colorspace) &&
    297          (features != NULL && features->has_alpha);
    298 }
    299 
    300 //------------------------------------------------------------------------------
    301