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