Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libwebp/src/dec/buffer_dec.c
9912 views
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 ([email protected])
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
static 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
41
static 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 =
78
MIN_BUFFER_SIZE((uint64_t)width * kModeBpp[mode], height, stride);
79
ok &= (size <= buf->size);
80
ok &= (stride >= width * kModeBpp[mode]);
81
ok &= (buf->rgba != NULL);
82
}
83
return ok ? VP8_STATUS_OK : VP8_STATUS_INVALID_PARAM;
84
}
85
#undef MIN_BUFFER_SIZE
86
87
static VP8StatusCode AllocateBuffer(WebPDecBuffer* const buffer) {
88
const int w = buffer->width;
89
const int h = buffer->height;
90
const WEBP_CSP_MODE mode = buffer->colorspace;
91
92
if (w <= 0 || h <= 0 || !IsValidColorspace(mode)) {
93
return VP8_STATUS_INVALID_PARAM;
94
}
95
96
if (buffer->is_external_memory <= 0 && buffer->private_memory == NULL) {
97
uint8_t* output;
98
int uv_stride = 0, a_stride = 0;
99
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
int stride;
103
uint64_t size;
104
105
if ((uint64_t)w * kModeBpp[mode] >= (1ull << 31)) {
106
return VP8_STATUS_INVALID_PARAM;
107
}
108
stride = w * kModeBpp[mode];
109
size = (uint64_t)stride * h;
110
if (!WebPIsRGBMode(mode)) {
111
uv_stride = (w + 1) / 2;
112
uv_size = (uint64_t)uv_stride * ((h + 1) / 2);
113
if (mode == MODE_YUVA) {
114
a_stride = w;
115
a_size = (uint64_t)a_stride * h;
116
}
117
}
118
total_size = size + 2 * uv_size + a_size;
119
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
152
VP8StatusCode 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 += (int64_t)(buffer->height - 1) * buf->stride;
159
buf->stride = -buf->stride;
160
} else {
161
WebPYUVABuffer* const buf = &buffer->u.YUVA;
162
const int64_t 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
177
VP8StatusCode 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 (!WebPCheckCropDimensions(width, height, x, y, cw, ch)) {
191
return VP8_STATUS_INVALID_PARAM; // out of frame boundary.
192
}
193
width = cw;
194
height = ch;
195
}
196
197
if (options->use_scaling) {
198
#if !defined(WEBP_REDUCE_SIZE)
199
int scaled_width = options->scaled_width;
200
int scaled_height = options->scaled_height;
201
if (!WebPRescalerGetScaledDimensions(
202
width, height, &scaled_width, &scaled_height)) {
203
return VP8_STATUS_INVALID_PARAM;
204
}
205
width = scaled_width;
206
height = scaled_height;
207
#else
208
return VP8_STATUS_INVALID_PARAM; // rescaling not supported
209
#endif
210
}
211
}
212
buffer->width = width;
213
buffer->height = height;
214
215
// Then, allocate buffer for real.
216
status = AllocateBuffer(buffer);
217
if (status != VP8_STATUS_OK) return status;
218
219
// Use the stride trick if vertical flip is needed.
220
if (options != NULL && options->flip) {
221
status = WebPFlipBuffer(buffer);
222
}
223
return status;
224
}
225
226
//------------------------------------------------------------------------------
227
// constructors / destructors
228
229
int WebPInitDecBufferInternal(WebPDecBuffer* buffer, int version) {
230
if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
231
return 0; // version mismatch
232
}
233
if (buffer == NULL) return 0;
234
memset(buffer, 0, sizeof(*buffer));
235
return 1;
236
}
237
238
void WebPFreeDecBuffer(WebPDecBuffer* buffer) {
239
if (buffer != NULL) {
240
if (buffer->is_external_memory <= 0) {
241
WebPSafeFree(buffer->private_memory);
242
}
243
buffer->private_memory = NULL;
244
}
245
}
246
247
void WebPCopyDecBuffer(const WebPDecBuffer* const src,
248
WebPDecBuffer* const dst) {
249
if (src != NULL && dst != NULL) {
250
*dst = *src;
251
if (src->private_memory != NULL) {
252
dst->is_external_memory = 1; // dst buffer doesn't own the memory.
253
dst->private_memory = NULL;
254
}
255
}
256
}
257
258
// Copy and transfer ownership from src to dst (beware of parameter order!)
259
void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst) {
260
if (src != NULL && dst != NULL) {
261
*dst = *src;
262
if (src->private_memory != NULL) {
263
src->is_external_memory = 1; // src relinquishes ownership
264
src->private_memory = NULL;
265
}
266
}
267
}
268
269
VP8StatusCode WebPCopyDecBufferPixels(const WebPDecBuffer* const src_buf,
270
WebPDecBuffer* const dst_buf) {
271
assert(src_buf != NULL && dst_buf != NULL);
272
assert(src_buf->colorspace == dst_buf->colorspace);
273
274
dst_buf->width = src_buf->width;
275
dst_buf->height = src_buf->height;
276
if (CheckDecBuffer(dst_buf) != VP8_STATUS_OK) {
277
return VP8_STATUS_INVALID_PARAM;
278
}
279
if (WebPIsRGBMode(src_buf->colorspace)) {
280
const WebPRGBABuffer* const src = &src_buf->u.RGBA;
281
const WebPRGBABuffer* const dst = &dst_buf->u.RGBA;
282
WebPCopyPlane(src->rgba, src->stride, dst->rgba, dst->stride,
283
src_buf->width * kModeBpp[src_buf->colorspace],
284
src_buf->height);
285
} else {
286
const WebPYUVABuffer* const src = &src_buf->u.YUVA;
287
const WebPYUVABuffer* const dst = &dst_buf->u.YUVA;
288
WebPCopyPlane(src->y, src->y_stride, dst->y, dst->y_stride,
289
src_buf->width, src_buf->height);
290
WebPCopyPlane(src->u, src->u_stride, dst->u, dst->u_stride,
291
(src_buf->width + 1) / 2, (src_buf->height + 1) / 2);
292
WebPCopyPlane(src->v, src->v_stride, dst->v, dst->v_stride,
293
(src_buf->width + 1) / 2, (src_buf->height + 1) / 2);
294
if (WebPIsAlphaMode(src_buf->colorspace)) {
295
WebPCopyPlane(src->a, src->a_stride, dst->a, dst->a_stride,
296
src_buf->width, src_buf->height);
297
}
298
}
299
return VP8_STATUS_OK;
300
}
301
302
int WebPAvoidSlowMemory(const WebPDecBuffer* const output,
303
const WebPBitstreamFeatures* const features) {
304
assert(output != NULL);
305
return (output->is_external_memory >= 2) &&
306
WebPIsPremultipliedMode(output->colorspace) &&
307
(features != NULL && features->has_alpha);
308
}
309
310
//------------------------------------------------------------------------------
311
312