Coverage Report

Created: 2025-12-31 07:15

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