Coverage Report

Created: 2024-07-27 06:27

/src/libwebp/src/dec/buffer_dec.c
Line
Count
Source (jump to first uncovered line)
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.
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
0
static int IsValidColorspace(int webp_csp_mode) {
33
0
  return (webp_csp_mode >= MODE_RGB && webp_csp_mode < MODE_LAST);
34
0
}
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
0
    ((uint64_t)(STRIDE) * ((HEIGHT) - 1) + (WIDTH))
40
41
0
static VP8StatusCode CheckDecBuffer(const WebPDecBuffer* const buffer) {
42
0
  int ok = 1;
43
0
  const WEBP_CSP_MODE mode = buffer->colorspace;
44
0
  const int width = buffer->width;
45
0
  const int height = buffer->height;
46
0
  if (!IsValidColorspace(mode)) {
47
0
    ok = 0;
48
0
  } else if (!WebPIsRGBMode(mode)) {   // YUV checks
49
0
    const WebPYUVABuffer* const buf = &buffer->u.YUVA;
50
0
    const int uv_width  = (width  + 1) / 2;
51
0
    const int uv_height = (height + 1) / 2;
52
0
    const int y_stride = abs(buf->y_stride);
53
0
    const int u_stride = abs(buf->u_stride);
54
0
    const int v_stride = abs(buf->v_stride);
55
0
    const int a_stride = abs(buf->a_stride);
56
0
    const uint64_t y_size = MIN_BUFFER_SIZE(width, height, y_stride);
57
0
    const uint64_t u_size = MIN_BUFFER_SIZE(uv_width, uv_height, u_stride);
58
0
    const uint64_t v_size = MIN_BUFFER_SIZE(uv_width, uv_height, v_stride);
59
0
    const uint64_t a_size = MIN_BUFFER_SIZE(width, height, a_stride);
60
0
    ok &= (y_size <= buf->y_size);
61
0
    ok &= (u_size <= buf->u_size);
62
0
    ok &= (v_size <= buf->v_size);
63
0
    ok &= (y_stride >= width);
64
0
    ok &= (u_stride >= uv_width);
65
0
    ok &= (v_stride >= uv_width);
66
0
    ok &= (buf->y != NULL);
67
0
    ok &= (buf->u != NULL);
68
0
    ok &= (buf->v != NULL);
69
0
    if (mode == MODE_YUVA) {
70
0
      ok &= (a_stride >= width);
71
0
      ok &= (a_size <= buf->a_size);
72
0
      ok &= (buf->a != NULL);
73
0
    }
74
0
  } else {    // RGB checks
75
0
    const WebPRGBABuffer* const buf = &buffer->u.RGBA;
76
0
    const int stride = abs(buf->stride);
77
0
    const uint64_t size =
78
0
        MIN_BUFFER_SIZE((uint64_t)width * kModeBpp[mode], height, stride);
79
0
    ok &= (size <= buf->size);
80
0
    ok &= (stride >= width * kModeBpp[mode]);
81
0
    ok &= (buf->rgba != NULL);
82
0
  }
83
0
  return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM;
84
0
}
85
#undef MIN_BUFFER_SIZE
86
87
0
static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) {
88
0
  const int w = buffer->width;
89
0
  const int h = buffer->height;
90
0
  const WEBP_CSP_MODE mode = buffer->colorspace;
91
92
0
  if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) {
93
0
    return VP8_STATUS_INVALID_PARAM;
94
0
  }
95
96
0
  if (buffer->is_external_memory <= 0 && buffer->private_memory == NULL) {
97
0
    uint8_t* output;
98
0
    int uv_stride = 0, a_stride = 0;
99
0
    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
0
    int stride;
103
0
    uint64_t size;
104
105
0
    if ((uint64_t)w * kModeBpp[mode] >= (1ull << 31)) {
106
0
      return VP8_STATUS_INVALID_PARAM;
107
0
    }
108
0
    stride = w * kModeBpp[mode];
109
0
    size = (uint64_t)stride * h;
110
0
    if (!WebPIsRGBMode(mode)) {
111
0
      uv_stride = (w + 1) / 2;
112
0
      uv_size = (uint64_t)uv_stride * ((h + 1) / 2);
113
0
      if (mode == MODE_YUVA) {
114
0
        a_stride = w;
115
0
        a_size = (uint64_t)a_stride * h;
116
0
      }
117
0
    }
118
0
    total_size = size + 2 * uv_size + a_size;
119
120
0
    output = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*output));
121
0
    if (output == NULL) {
122
0
      return VP8_STATUS_OUT_OF_MEMORY;
123
0
    }
124
0
    buffer->private_memory = output;
125
126
0
    if (!WebPIsRGBMode(mode)) {   // YUVA initialization
127
0
      WebPYUVABuffer* const buf = &buffer->u.YUVA;
128
0
      buf->y = output;
129
0
      buf->y_stride = stride;
130
0
      buf->y_size = (size_t)size;
131
0
      buf->u = output + size;
132
0
      buf->u_stride = uv_stride;
133
0
      buf->u_size = (size_t)uv_size;
134
0
      buf->v = output + size + uv_size;
135
0
      buf->v_stride = uv_stride;
136
0
      buf->v_size = (size_t)uv_size;
137
0
      if (mode == MODE_YUVA) {
138
0
        buf->a = output + size + 2 * uv_size;
139
0
      }
140
0
      buf->a_size = (size_t)a_size;
141
0
      buf->a_stride = a_stride;
142
0
    } else {  // RGBA initialization
143
0
      WebPRGBABuffer* const buf = &buffer->u.RGBA;
144
0
      buf->rgba = output;
145
0
      buf->stride = stride;
146
0
      buf->size = (size_t)size;
147
0
    }
148
0
  }
149
0
  return CheckDecBuffer(buffer);
150
0
}
151
152
0
VP8StatusCode WebPFlipBuffer(WebPDecBuffer* const buffer) {
153
0
  if (buffer == NULL) {
154
0
    return VP8_STATUS_INVALID_PARAM;
155
0
  }
156
0
  if (WebPIsRGBMode(buffer->colorspace)) {
157
0
    WebPRGBABuffer* const buf = &buffer->u.RGBA;
158
0
    buf->rgba += (int64_t)(buffer->height - 1) * buf->stride;
159
0
    buf->stride = -buf->stride;
160
0
  } else {
161
0
    WebPYUVABuffer* const buf = &buffer->u.YUVA;
162
0
    const int64_t H = buffer->height;
163
0
    buf->y += (H - 1) * buf->y_stride;
164
0
    buf->y_stride = -buf->y_stride;
165
0
    buf->u += ((H - 1) >> 1) * buf->u_stride;
166
0
    buf->u_stride = -buf->u_stride;
167
0
    buf->v += ((H - 1) >> 1) * buf->v_stride;
168
0
    buf->v_stride = -buf->v_stride;
169
0
    if (buf->a != NULL) {
170
0
      buf->a += (H - 1) * buf->a_stride;
171
0
      buf->a_stride = -buf->a_stride;
172
0
    }
173
0
  }
174
0
  return VP8_STATUS_OK;
175
0
}
176
177
VP8StatusCode WebPAllocateDecBuffer(int width, int height,
178
                                    const WebPDecoderOptions* const options,
179
0
                                    WebPDecBuffer* const buffer) {
180
0
  VP8StatusCode status;
181
0
  if (buffer == NULL || width <= 0 || height <= 0) {
182
0
    return VP8_STATUS_INVALID_PARAM;
183
0
  }
184
0
  if (options != NULL) {    // First, apply options if there is any.
185
0
    if (options->use_cropping) {
186
0
      const int cw = options->crop_width;
187
0
      const int ch = options->crop_height;
188
0
      const int x = options->crop_left & ~1;
189
0
      const int y = options->crop_top & ~1;
190
0
      if (!WebPCheckCropDimensions(width, height, x, y, cw, ch)) {
191
0
        return VP8_STATUS_INVALID_PARAM;   // out of frame boundary.
192
0
      }
193
0
      width = cw;
194
0
      height = ch;
195
0
    }
196
197
0
    if (options->use_scaling) {
198
0
#if !defined(WEBP_REDUCE_SIZE)
199
0
      int scaled_width = options->scaled_width;
200
0
      int scaled_height = options->scaled_height;
201
0
      if (!WebPRescalerGetScaledDimensions(
202
0
              width, height, &scaled_width, &scaled_height)) {
203
0
        return VP8_STATUS_INVALID_PARAM;
204
0
      }
205
0
      width = scaled_width;
206
0
      height = scaled_height;
207
#else
208
      return VP8_STATUS_INVALID_PARAM;   // rescaling not supported
209
#endif
210
0
    }
211
0
  }
212
0
  buffer->width = width;
213
0
  buffer->height = height;
214
215
  // Then, allocate buffer for real.
216
0
  status = AllocateBuffer(buffer);
217
0
  if (status != VP8_STATUS_OK) return status;
218
219
  // Use the stride trick if vertical flip is needed.
220
0
  if (options != NULL && options->flip) {
221
0
    status = WebPFlipBuffer(buffer);
222
0
  }
223
0
  return status;
224
0
}
225
226
//------------------------------------------------------------------------------
227
// constructors / destructors
228
229
0
int WebPInitDecBufferInternal(WebPDecBuffer* buffer, int version) {
230
0
  if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
231
0
    return 0;  // version mismatch
232
0
  }
233
0
  if (buffer == NULL) return 0;
234
0
  memset(buffer, 0, sizeof(*buffer));
235
0
  return 1;
236
0
}
237
238
0
void WebPFreeDecBuffer(WebPDecBuffer* buffer) {
239
0
  if (buffer != NULL) {
240
0
    if (buffer->is_external_memory <= 0) {
241
0
      WebPSafeFree(buffer->private_memory);
242
0
    }
243
0
    buffer->private_memory = NULL;
244
0
  }
245
0
}
246
247
void WebPCopyDecBuffer(const WebPDecBuffer* const src,
248
0
                       WebPDecBuffer* const dst) {
249
0
  if (src != NULL && dst != NULL) {
250
0
    *dst = *src;
251
0
    if (src->private_memory != NULL) {
252
0
      dst->is_external_memory = 1;   // dst buffer doesn't own the memory.
253
0
      dst->private_memory = NULL;
254
0
    }
255
0
  }
256
0
}
257
258
// Copy and transfer ownership from src to dst (beware of parameter order!)
259
0
void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst) {
260
0
  if (src != NULL && dst != NULL) {
261
0
    *dst = *src;
262
0
    if (src->private_memory != NULL) {
263
0
      src->is_external_memory = 1;   // src relinquishes ownership
264
0
      src->private_memory = NULL;
265
0
    }
266
0
  }
267
0
}
268
269
VP8StatusCode WebPCopyDecBufferPixels(const WebPDecBuffer* const src_buf,
270
0
                                      WebPDecBuffer* const dst_buf) {
271
0
  assert(src_buf != NULL && dst_buf != NULL);
272
0
  assert(src_buf->colorspace == dst_buf->colorspace);
273
274
0
  dst_buf->width = src_buf->width;
275
0
  dst_buf->height = src_buf->height;
276
0
  if (CheckDecBuffer(dst_buf) != VP8_STATUS_OK) {
277
0
    return VP8_STATUS_INVALID_PARAM;
278
0
  }
279
0
  if (WebPIsRGBMode(src_buf->colorspace)) {
280
0
    const WebPRGBABuffer* const src = &src_buf->u.RGBA;
281
0
    const WebPRGBABuffer* const dst = &dst_buf->u.RGBA;
282
0
    WebPCopyPlane(src->rgba, src->stride, dst->rgba, dst->stride,
283
0
                  src_buf->width * kModeBpp[src_buf->colorspace],
284
0
                  src_buf->height);
285
0
  } else {
286
0
    const WebPYUVABuffer* const src = &src_buf->u.YUVA;
287
0
    const WebPYUVABuffer* const dst = &dst_buf->u.YUVA;
288
0
    WebPCopyPlane(src->y, src->y_stride, dst->y, dst->y_stride,
289
0
                  src_buf->width, src_buf->height);
290
0
    WebPCopyPlane(src->u, src->u_stride, dst->u, dst->u_stride,
291
0
                  (src_buf->width + 1) / 2, (src_buf->height + 1) / 2);
292
0
    WebPCopyPlane(src->v, src->v_stride, dst->v, dst->v_stride,
293
0
                  (src_buf->width + 1) / 2, (src_buf->height + 1) / 2);
294
0
    if (WebPIsAlphaMode(src_buf->colorspace)) {
295
0
      WebPCopyPlane(src->a, src->a_stride, dst->a, dst->a_stride,
296
0
                    src_buf->width, src_buf->height);
297
0
    }
298
0
  }
299
0
  return VP8_STATUS_OK;
300
0
}
301
302
int WebPAvoidSlowMemory(const WebPDecBuffer* const output,
303
0
                        const WebPBitstreamFeatures* const features) {
304
0
  assert(output != NULL);
305
0
  return (output->is_external_memory >= 2) &&
306
0
         WebPIsPremultipliedMode(output->colorspace) &&
307
0
         (features != NULL && features->has_alpha);
308
0
}
309
310
//------------------------------------------------------------------------------