Home | History | Annotate | Download | only in enc
      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 // WebPPicture class basis
     11 //
     12 // Author: Skal (pascal.massimino (at) gmail.com)
     13 
     14 #include <assert.h>
     15 #include <stdlib.h>
     16 
     17 #include "./vp8enci.h"
     18 #include "../dsp/dsp.h"
     19 #include "../utils/utils.h"
     20 
     21 //------------------------------------------------------------------------------
     22 // WebPPicture
     23 //------------------------------------------------------------------------------
     24 
     25 static int DummyWriter(const uint8_t* data, size_t data_size,
     26                        const WebPPicture* const picture) {
     27   // The following are to prevent 'unused variable' error message.
     28   (void)data;
     29   (void)data_size;
     30   (void)picture;
     31   return 1;
     32 }
     33 
     34 int WebPPictureInitInternal(WebPPicture* picture, int version) {
     35   if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_ENCODER_ABI_VERSION)) {
     36     return 0;   // caller/system version mismatch!
     37   }
     38   if (picture != NULL) {
     39     memset(picture, 0, sizeof(*picture));
     40     picture->writer = DummyWriter;
     41     WebPEncodingSetError(picture, VP8_ENC_OK);
     42   }
     43   return 1;
     44 }
     45 
     46 //------------------------------------------------------------------------------
     47 
     48 static void WebPPictureResetBufferARGB(WebPPicture* const picture) {
     49   picture->memory_argb_ = NULL;
     50   picture->argb = NULL;
     51   picture->argb_stride = 0;
     52 }
     53 
     54 static void WebPPictureResetBufferYUVA(WebPPicture* const picture) {
     55   picture->memory_ = NULL;
     56   picture->y = picture->u = picture->v = picture->a = NULL;
     57   picture->y_stride = picture->uv_stride = 0;
     58   picture->a_stride = 0;
     59 }
     60 
     61 void WebPPictureResetBuffers(WebPPicture* const picture) {
     62   WebPPictureResetBufferARGB(picture);
     63   WebPPictureResetBufferYUVA(picture);
     64 }
     65 
     66 int WebPPictureAllocARGB(WebPPicture* const picture, int width, int height) {
     67   void* memory;
     68   const uint64_t argb_size = (uint64_t)width * height;
     69 
     70   assert(picture != NULL);
     71 
     72   WebPSafeFree(picture->memory_argb_);
     73   WebPPictureResetBufferARGB(picture);
     74 
     75   if (width <= 0 || height <= 0) {
     76     return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);
     77   }
     78   // allocate a new buffer.
     79   memory = WebPSafeMalloc(argb_size, sizeof(*picture->argb));
     80   if (memory == NULL) {
     81     return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
     82   }
     83   // TODO(skal): align plane to cache line?
     84   picture->memory_argb_ = memory;
     85   picture->argb = (uint32_t*)memory;
     86   picture->argb_stride = width;
     87   return 1;
     88 }
     89 
     90 int WebPPictureAllocYUVA(WebPPicture* const picture, int width, int height) {
     91   const WebPEncCSP uv_csp = picture->colorspace & WEBP_CSP_UV_MASK;
     92   const int has_alpha = picture->colorspace & WEBP_CSP_ALPHA_BIT;
     93   const int y_stride = width;
     94   const int uv_width = (width + 1) >> 1;
     95   const int uv_height = (height + 1) >> 1;
     96   const int uv_stride = uv_width;
     97   int a_width, a_stride;
     98   uint64_t y_size, uv_size, a_size, total_size;
     99   uint8_t* mem;
    100 
    101   assert(picture != NULL);
    102 
    103   WebPSafeFree(picture->memory_);
    104   WebPPictureResetBufferYUVA(picture);
    105 
    106   if (uv_csp != WEBP_YUV420) {
    107     return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);
    108   }
    109 
    110   // alpha
    111   a_width = has_alpha ? width : 0;
    112   a_stride = a_width;
    113   y_size = (uint64_t)y_stride * height;
    114   uv_size = (uint64_t)uv_stride * uv_height;
    115   a_size =  (uint64_t)a_stride * height;
    116 
    117   total_size = y_size + a_size + 2 * uv_size;
    118 
    119   // Security and validation checks
    120   if (width <= 0 || height <= 0 ||         // luma/alpha param error
    121       uv_width < 0 || uv_height < 0) {     // u/v param error
    122     return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);
    123   }
    124   // allocate a new buffer.
    125   mem = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*mem));
    126   if (mem == NULL) {
    127     return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
    128   }
    129 
    130   // From now on, we're in the clear, we can no longer fail...
    131   picture->memory_ = (void*)mem;
    132   picture->y_stride  = y_stride;
    133   picture->uv_stride = uv_stride;
    134   picture->a_stride  = a_stride;
    135 
    136   // TODO(skal): we could align the y/u/v planes and adjust stride.
    137   picture->y = mem;
    138   mem += y_size;
    139 
    140   picture->u = mem;
    141   mem += uv_size;
    142   picture->v = mem;
    143   mem += uv_size;
    144 
    145   if (a_size > 0) {
    146     picture->a = mem;
    147     mem += a_size;
    148   }
    149   (void)mem;  // makes the static analyzer happy
    150   return 1;
    151 }
    152 
    153 int WebPPictureAlloc(WebPPicture* picture) {
    154   if (picture != NULL) {
    155     const int width = picture->width;
    156     const int height = picture->height;
    157 
    158     WebPPictureFree(picture);   // erase previous buffer
    159 
    160     if (!picture->use_argb) {
    161       return WebPPictureAllocYUVA(picture, width, height);
    162     } else {
    163       return WebPPictureAllocARGB(picture, width, height);
    164     }
    165   }
    166   return 1;
    167 }
    168 
    169 void WebPPictureFree(WebPPicture* picture) {
    170   if (picture != NULL) {
    171     WebPSafeFree(picture->memory_);
    172     WebPSafeFree(picture->memory_argb_);
    173     WebPPictureResetBuffers(picture);
    174   }
    175 }
    176 
    177 //------------------------------------------------------------------------------
    178 // WebPMemoryWriter: Write-to-memory
    179 
    180 void WebPMemoryWriterInit(WebPMemoryWriter* writer) {
    181   writer->mem = NULL;
    182   writer->size = 0;
    183   writer->max_size = 0;
    184 }
    185 
    186 int WebPMemoryWrite(const uint8_t* data, size_t data_size,
    187                     const WebPPicture* picture) {
    188   WebPMemoryWriter* const w = (WebPMemoryWriter*)picture->custom_ptr;
    189   uint64_t next_size;
    190   if (w == NULL) {
    191     return 1;
    192   }
    193   next_size = (uint64_t)w->size + data_size;
    194   if (next_size > w->max_size) {
    195     uint8_t* new_mem;
    196     uint64_t next_max_size = 2ULL * w->max_size;
    197     if (next_max_size < next_size) next_max_size = next_size;
    198     if (next_max_size < 8192ULL) next_max_size = 8192ULL;
    199     new_mem = (uint8_t*)WebPSafeMalloc(next_max_size, 1);
    200     if (new_mem == NULL) {
    201       return 0;
    202     }
    203     if (w->size > 0) {
    204       memcpy(new_mem, w->mem, w->size);
    205     }
    206     WebPSafeFree(w->mem);
    207     w->mem = new_mem;
    208     // down-cast is ok, thanks to WebPSafeMalloc
    209     w->max_size = (size_t)next_max_size;
    210   }
    211   if (data_size > 0) {
    212     memcpy(w->mem + w->size, data, data_size);
    213     w->size += data_size;
    214   }
    215   return 1;
    216 }
    217 
    218 void WebPMemoryWriterClear(WebPMemoryWriter* writer) {
    219   if (writer != NULL) {
    220     WebPSafeFree(writer->mem);
    221     writer->mem = NULL;
    222     writer->size = 0;
    223     writer->max_size = 0;
    224   }
    225 }
    226 
    227 //------------------------------------------------------------------------------
    228 // Simplest high-level calls:
    229 
    230 typedef int (*Importer)(WebPPicture* const, const uint8_t* const, int);
    231 
    232 static size_t Encode(const uint8_t* rgba, int width, int height, int stride,
    233                      Importer import, float quality_factor, int lossless,
    234                      uint8_t** output) {
    235   WebPPicture pic;
    236   WebPConfig config;
    237   WebPMemoryWriter wrt;
    238   int ok;
    239 
    240   if (!WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, quality_factor) ||
    241       !WebPPictureInit(&pic)) {
    242     return 0;  // shouldn't happen, except if system installation is broken
    243   }
    244 
    245   config.lossless = !!lossless;
    246   pic.use_argb = !!lossless;
    247   pic.width = width;
    248   pic.height = height;
    249   pic.writer = WebPMemoryWrite;
    250   pic.custom_ptr = &wrt;
    251   WebPMemoryWriterInit(&wrt);
    252 
    253   ok = import(&pic, rgba, stride) && WebPEncode(&config, &pic);
    254   WebPPictureFree(&pic);
    255   if (!ok) {
    256     WebPMemoryWriterClear(&wrt);
    257     *output = NULL;
    258     return 0;
    259   }
    260   *output = wrt.mem;
    261   return wrt.size;
    262 }
    263 
    264 #define ENCODE_FUNC(NAME, IMPORTER)                                     \
    265 size_t NAME(const uint8_t* in, int w, int h, int bps, float q,          \
    266             uint8_t** out) {                                            \
    267   return Encode(in, w, h, bps, IMPORTER, q, 0, out);                    \
    268 }
    269 
    270 ENCODE_FUNC(WebPEncodeRGB, WebPPictureImportRGB)
    271 ENCODE_FUNC(WebPEncodeBGR, WebPPictureImportBGR)
    272 ENCODE_FUNC(WebPEncodeRGBA, WebPPictureImportRGBA)
    273 ENCODE_FUNC(WebPEncodeBGRA, WebPPictureImportBGRA)
    274 
    275 #undef ENCODE_FUNC
    276 
    277 #define LOSSLESS_DEFAULT_QUALITY 70.
    278 #define LOSSLESS_ENCODE_FUNC(NAME, IMPORTER)                                 \
    279 size_t NAME(const uint8_t* in, int w, int h, int bps, uint8_t** out) {       \
    280   return Encode(in, w, h, bps, IMPORTER, LOSSLESS_DEFAULT_QUALITY, 1, out);  \
    281 }
    282 
    283 LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGB, WebPPictureImportRGB)
    284 LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGR, WebPPictureImportBGR)
    285 LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGBA, WebPPictureImportRGBA)
    286 LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGRA, WebPPictureImportBGRA)
    287 
    288 #undef LOSSLESS_ENCODE_FUNC
    289 
    290 //------------------------------------------------------------------------------
    291