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 "./vp8i_enc.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 =
     92       (WebPEncCSP)((int)picture->colorspace & WEBP_CSP_UV_MASK);
     93   const int has_alpha = (int)picture->colorspace & WEBP_CSP_ALPHA_BIT;
     94   const int y_stride = width;
     95   const int uv_width = (width + 1) >> 1;
     96   const int uv_height = (height + 1) >> 1;
     97   const int uv_stride = uv_width;
     98   int a_width, a_stride;
     99   uint64_t y_size, uv_size, a_size, total_size;
    100   uint8_t* mem;
    101 
    102   assert(picture != NULL);
    103 
    104   WebPSafeFree(picture->memory_);
    105   WebPPictureResetBufferYUVA(picture);
    106 
    107   if (uv_csp != WEBP_YUV420) {
    108     return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);
    109   }
    110 
    111   // alpha
    112   a_width = has_alpha ? width : 0;
    113   a_stride = a_width;
    114   y_size = (uint64_t)y_stride * height;
    115   uv_size = (uint64_t)uv_stride * uv_height;
    116   a_size =  (uint64_t)a_stride * height;
    117 
    118   total_size = y_size + a_size + 2 * uv_size;
    119 
    120   // Security and validation checks
    121   if (width <= 0 || height <= 0 ||         // luma/alpha param error
    122       uv_width < 0 || uv_height < 0) {     // u/v param error
    123     return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);
    124   }
    125   // allocate a new buffer.
    126   mem = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*mem));
    127   if (mem == NULL) {
    128     return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
    129   }
    130 
    131   // From now on, we're in the clear, we can no longer fail...
    132   picture->memory_ = (void*)mem;
    133   picture->y_stride  = y_stride;
    134   picture->uv_stride = uv_stride;
    135   picture->a_stride  = a_stride;
    136 
    137   // TODO(skal): we could align the y/u/v planes and adjust stride.
    138   picture->y = mem;
    139   mem += y_size;
    140 
    141   picture->u = mem;
    142   mem += uv_size;
    143   picture->v = mem;
    144   mem += uv_size;
    145 
    146   if (a_size > 0) {
    147     picture->a = mem;
    148     mem += a_size;
    149   }
    150   (void)mem;  // makes the static analyzer happy
    151   return 1;
    152 }
    153 
    154 int WebPPictureAlloc(WebPPicture* picture) {
    155   if (picture != NULL) {
    156     const int width = picture->width;
    157     const int height = picture->height;
    158 
    159     WebPPictureFree(picture);   // erase previous buffer
    160 
    161     if (!picture->use_argb) {
    162       return WebPPictureAllocYUVA(picture, width, height);
    163     } else {
    164       return WebPPictureAllocARGB(picture, width, height);
    165     }
    166   }
    167   return 1;
    168 }
    169 
    170 void WebPPictureFree(WebPPicture* picture) {
    171   if (picture != NULL) {
    172     WebPSafeFree(picture->memory_);
    173     WebPSafeFree(picture->memory_argb_);
    174     WebPPictureResetBuffers(picture);
    175   }
    176 }
    177 
    178 //------------------------------------------------------------------------------
    179 // WebPMemoryWriter: Write-to-memory
    180 
    181 void WebPMemoryWriterInit(WebPMemoryWriter* writer) {
    182   writer->mem = NULL;
    183   writer->size = 0;
    184   writer->max_size = 0;
    185 }
    186 
    187 int WebPMemoryWrite(const uint8_t* data, size_t data_size,
    188                     const WebPPicture* picture) {
    189   WebPMemoryWriter* const w = (WebPMemoryWriter*)picture->custom_ptr;
    190   uint64_t next_size;
    191   if (w == NULL) {
    192     return 1;
    193   }
    194   next_size = (uint64_t)w->size + data_size;
    195   if (next_size > w->max_size) {
    196     uint8_t* new_mem;
    197     uint64_t next_max_size = 2ULL * w->max_size;
    198     if (next_max_size < next_size) next_max_size = next_size;
    199     if (next_max_size < 8192ULL) next_max_size = 8192ULL;
    200     new_mem = (uint8_t*)WebPSafeMalloc(next_max_size, 1);
    201     if (new_mem == NULL) {
    202       return 0;
    203     }
    204     if (w->size > 0) {
    205       memcpy(new_mem, w->mem, w->size);
    206     }
    207     WebPSafeFree(w->mem);
    208     w->mem = new_mem;
    209     // down-cast is ok, thanks to WebPSafeMalloc
    210     w->max_size = (size_t)next_max_size;
    211   }
    212   if (data_size > 0) {
    213     memcpy(w->mem + w->size, data, data_size);
    214     w->size += data_size;
    215   }
    216   return 1;
    217 }
    218 
    219 void WebPMemoryWriterClear(WebPMemoryWriter* writer) {
    220   if (writer != NULL) {
    221     WebPSafeFree(writer->mem);
    222     writer->mem = NULL;
    223     writer->size = 0;
    224     writer->max_size = 0;
    225   }
    226 }
    227 
    228 //------------------------------------------------------------------------------
    229 // Simplest high-level calls:
    230 
    231 typedef int (*Importer)(WebPPicture* const, const uint8_t* const, int);
    232 
    233 static size_t Encode(const uint8_t* rgba, int width, int height, int stride,
    234                      Importer import, float quality_factor, int lossless,
    235                      uint8_t** output) {
    236   WebPPicture pic;
    237   WebPConfig config;
    238   WebPMemoryWriter wrt;
    239   int ok;
    240 
    241   if (output == NULL) return 0;
    242 
    243   if (!WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, quality_factor) ||
    244       !WebPPictureInit(&pic)) {
    245     return 0;  // shouldn't happen, except if system installation is broken
    246   }
    247 
    248   config.lossless = !!lossless;
    249   pic.use_argb = !!lossless;
    250   pic.width = width;
    251   pic.height = height;
    252   pic.writer = WebPMemoryWrite;
    253   pic.custom_ptr = &wrt;
    254   WebPMemoryWriterInit(&wrt);
    255 
    256   ok = import(&pic, rgba, stride) && WebPEncode(&config, &pic);
    257   WebPPictureFree(&pic);
    258   if (!ok) {
    259     WebPMemoryWriterClear(&wrt);
    260     *output = NULL;
    261     return 0;
    262   }
    263   *output = wrt.mem;
    264   return wrt.size;
    265 }
    266 
    267 #define ENCODE_FUNC(NAME, IMPORTER)                                     \
    268 size_t NAME(const uint8_t* in, int w, int h, int bps, float q,          \
    269             uint8_t** out) {                                            \
    270   return Encode(in, w, h, bps, IMPORTER, q, 0, out);                    \
    271 }
    272 
    273 ENCODE_FUNC(WebPEncodeRGB, WebPPictureImportRGB)
    274 ENCODE_FUNC(WebPEncodeBGR, WebPPictureImportBGR)
    275 ENCODE_FUNC(WebPEncodeRGBA, WebPPictureImportRGBA)
    276 ENCODE_FUNC(WebPEncodeBGRA, WebPPictureImportBGRA)
    277 
    278 #undef ENCODE_FUNC
    279 
    280 #define LOSSLESS_DEFAULT_QUALITY 70.
    281 #define LOSSLESS_ENCODE_FUNC(NAME, IMPORTER)                                 \
    282 size_t NAME(const uint8_t* in, int w, int h, int bps, uint8_t** out) {       \
    283   return Encode(in, w, h, bps, IMPORTER, LOSSLESS_DEFAULT_QUALITY, 1, out);  \
    284 }
    285 
    286 LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGB, WebPPictureImportRGB)
    287 LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGR, WebPPictureImportBGR)
    288 LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGBA, WebPPictureImportRGBA)
    289 LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGRA, WebPPictureImportBGRA)
    290 
    291 #undef LOSSLESS_ENCODE_FUNC
    292 
    293 //------------------------------------------------------------------------------
    294