Coverage Report

Created: 2024-07-27 06:27

/src/libwebp/src/enc/picture_enc.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
// WebPPicture class basis
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include <assert.h>
15
#include <limits.h>
16
#include <stdlib.h>
17
18
#include "src/enc/vp8i_enc.h"
19
#include "src/utils/utils.h"
20
21
//------------------------------------------------------------------------------
22
// WebPPicture
23
//------------------------------------------------------------------------------
24
25
static int DummyWriter(const uint8_t* data, size_t data_size,
26
0
                       const WebPPicture* const picture) {
27
  // The following are to prevent 'unused variable' error message.
28
0
  (void)data;
29
0
  (void)data_size;
30
0
  (void)picture;
31
0
  return 1;
32
0
}
33
34
0
int WebPPictureInitInternal(WebPPicture* picture, int version) {
35
0
  if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_ENCODER_ABI_VERSION)) {
36
0
    return 0;   // caller/system version mismatch!
37
0
  }
38
0
  if (picture != NULL) {
39
0
    memset(picture, 0, sizeof(*picture));
40
0
    picture->writer = DummyWriter;
41
0
    WebPEncodingSetError(picture, VP8_ENC_OK);
42
0
  }
43
0
  return 1;
44
0
}
45
46
//------------------------------------------------------------------------------
47
48
0
int WebPValidatePicture(const WebPPicture* const picture) {
49
0
  if (picture == NULL) return 0;
50
0
  if (picture->width <= 0 || picture->height <= 0) {
51
0
    return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);
52
0
  }
53
0
  if (picture->width <= 0 || picture->width / 4 > INT_MAX / 4 ||
54
0
      picture->height <= 0 || picture->height / 4 > INT_MAX / 4) {
55
0
    return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);
56
0
  }
57
0
  if (picture->colorspace != WEBP_YUV420 &&
58
0
      picture->colorspace != WEBP_YUV420A) {
59
0
    return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);
60
0
  }
61
0
  return 1;
62
0
}
63
64
0
static void WebPPictureResetBufferARGB(WebPPicture* const picture) {
65
0
  picture->memory_argb_ = NULL;
66
0
  picture->argb = NULL;
67
0
  picture->argb_stride = 0;
68
0
}
69
70
0
static void WebPPictureResetBufferYUVA(WebPPicture* const picture) {
71
0
  picture->memory_ = NULL;
72
0
  picture->y = picture->u = picture->v = picture->a = NULL;
73
0
  picture->y_stride = picture->uv_stride = 0;
74
0
  picture->a_stride = 0;
75
0
}
76
77
0
void WebPPictureResetBuffers(WebPPicture* const picture) {
78
0
  WebPPictureResetBufferARGB(picture);
79
0
  WebPPictureResetBufferYUVA(picture);
80
0
}
81
82
0
int WebPPictureAllocARGB(WebPPicture* const picture) {
83
0
  void* memory;
84
0
  const int width = picture->width;
85
0
  const int height = picture->height;
86
0
  const uint64_t argb_size = (uint64_t)width * height;
87
88
0
  if (!WebPValidatePicture(picture)) return 0;
89
90
0
  WebPSafeFree(picture->memory_argb_);
91
0
  WebPPictureResetBufferARGB(picture);
92
93
  // allocate a new buffer.
94
0
  memory = WebPSafeMalloc(argb_size + WEBP_ALIGN_CST, sizeof(*picture->argb));
95
0
  if (memory == NULL) {
96
0
    return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
97
0
  }
98
0
  picture->memory_argb_ = memory;
99
0
  picture->argb = (uint32_t*)WEBP_ALIGN(memory);
100
0
  picture->argb_stride = width;
101
0
  return 1;
102
0
}
103
104
0
int WebPPictureAllocYUVA(WebPPicture* const picture) {
105
0
  const int has_alpha = (int)picture->colorspace & WEBP_CSP_ALPHA_BIT;
106
0
  const int width = picture->width;
107
0
  const int height = picture->height;
108
0
  const int y_stride = width;
109
0
  const int uv_width = (int)(((int64_t)width + 1) >> 1);
110
0
  const int uv_height = (int)(((int64_t)height + 1) >> 1);
111
0
  const int uv_stride = uv_width;
112
0
  int a_width, a_stride;
113
0
  uint64_t y_size, uv_size, a_size, total_size;
114
0
  uint8_t* mem;
115
116
0
  if (!WebPValidatePicture(picture)) return 0;
117
118
0
  WebPSafeFree(picture->memory_);
119
0
  WebPPictureResetBufferYUVA(picture);
120
121
  // alpha
122
0
  a_width = has_alpha ? width : 0;
123
0
  a_stride = a_width;
124
0
  y_size = (uint64_t)y_stride * height;
125
0
  uv_size = (uint64_t)uv_stride * uv_height;
126
0
  a_size =  (uint64_t)a_stride * height;
127
128
0
  total_size = y_size + a_size + 2 * uv_size;
129
130
  // Security and validation checks
131
0
  if (width <= 0 || height <= 0 ||           // luma/alpha param error
132
0
      uv_width <= 0 || uv_height <= 0) {     // u/v param error
133
0
    return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);
134
0
  }
135
  // allocate a new buffer.
136
0
  mem = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*mem));
137
0
  if (mem == NULL) {
138
0
    return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
139
0
  }
140
141
  // From now on, we're in the clear, we can no longer fail...
142
0
  picture->memory_ = (void*)mem;
143
0
  picture->y_stride  = y_stride;
144
0
  picture->uv_stride = uv_stride;
145
0
  picture->a_stride  = a_stride;
146
147
  // TODO(skal): we could align the y/u/v planes and adjust stride.
148
0
  picture->y = mem;
149
0
  mem += y_size;
150
151
0
  picture->u = mem;
152
0
  mem += uv_size;
153
0
  picture->v = mem;
154
0
  mem += uv_size;
155
156
0
  if (a_size > 0) {
157
0
    picture->a = mem;
158
0
    mem += a_size;
159
0
  }
160
0
  (void)mem;  // makes the static analyzer happy
161
0
  return 1;
162
0
}
163
164
0
int WebPPictureAlloc(WebPPicture* picture) {
165
0
  if (picture != NULL) {
166
0
    WebPPictureFree(picture);   // erase previous buffer
167
168
0
    if (!picture->use_argb) {
169
0
      return WebPPictureAllocYUVA(picture);
170
0
    } else {
171
0
      return WebPPictureAllocARGB(picture);
172
0
    }
173
0
  }
174
0
  return 1;
175
0
}
176
177
0
void WebPPictureFree(WebPPicture* picture) {
178
0
  if (picture != NULL) {
179
0
    WebPSafeFree(picture->memory_);
180
0
    WebPSafeFree(picture->memory_argb_);
181
0
    WebPPictureResetBuffers(picture);
182
0
  }
183
0
}
184
185
//------------------------------------------------------------------------------
186
// WebPMemoryWriter: Write-to-memory
187
188
0
void WebPMemoryWriterInit(WebPMemoryWriter* writer) {
189
0
  writer->mem = NULL;
190
0
  writer->size = 0;
191
0
  writer->max_size = 0;
192
0
}
193
194
int WebPMemoryWrite(const uint8_t* data, size_t data_size,
195
0
                    const WebPPicture* picture) {
196
0
  WebPMemoryWriter* const w = (WebPMemoryWriter*)picture->custom_ptr;
197
0
  uint64_t next_size;
198
0
  if (w == NULL) {
199
0
    return 1;
200
0
  }
201
0
  next_size = (uint64_t)w->size + data_size;
202
0
  if (next_size > w->max_size) {
203
0
    uint8_t* new_mem;
204
0
    uint64_t next_max_size = 2ULL * w->max_size;
205
0
    if (next_max_size < next_size) next_max_size = next_size;
206
0
    if (next_max_size < 8192ULL) next_max_size = 8192ULL;
207
0
    new_mem = (uint8_t*)WebPSafeMalloc(next_max_size, 1);
208
0
    if (new_mem == NULL) {
209
0
      return 0;
210
0
    }
211
0
    if (w->size > 0) {
212
0
      memcpy(new_mem, w->mem, w->size);
213
0
    }
214
0
    WebPSafeFree(w->mem);
215
0
    w->mem = new_mem;
216
    // down-cast is ok, thanks to WebPSafeMalloc
217
0
    w->max_size = (size_t)next_max_size;
218
0
  }
219
0
  if (data_size > 0) {
220
0
    memcpy(w->mem + w->size, data, data_size);
221
0
    w->size += data_size;
222
0
  }
223
0
  return 1;
224
0
}
225
226
0
void WebPMemoryWriterClear(WebPMemoryWriter* writer) {
227
0
  if (writer != NULL) {
228
0
    WebPSafeFree(writer->mem);
229
0
    writer->mem = NULL;
230
0
    writer->size = 0;
231
0
    writer->max_size = 0;
232
0
  }
233
0
}
234
235
//------------------------------------------------------------------------------
236
// Simplest high-level calls:
237
238
typedef int (*Importer)(WebPPicture* const, const uint8_t* const, int);
239
240
static size_t Encode(const uint8_t* rgba, int width, int height, int stride,
241
                     Importer import, float quality_factor, int lossless,
242
0
                     uint8_t** output) {
243
0
  WebPPicture pic;
244
0
  WebPConfig config;
245
0
  WebPMemoryWriter wrt;
246
0
  int ok;
247
248
0
  if (output == NULL) return 0;
249
250
0
  if (!WebPConfigPreset(&config, WEBP_PRESET_DEFAULT, quality_factor) ||
251
0
      !WebPPictureInit(&pic)) {
252
0
    return 0;  // shouldn't happen, except if system installation is broken
253
0
  }
254
255
0
  config.lossless = !!lossless;
256
0
  pic.use_argb = !!lossless;
257
0
  pic.width = width;
258
0
  pic.height = height;
259
0
  pic.writer = WebPMemoryWrite;
260
0
  pic.custom_ptr = &wrt;
261
0
  WebPMemoryWriterInit(&wrt);
262
263
0
  ok = import(&pic, rgba, stride) && WebPEncode(&config, &pic);
264
0
  WebPPictureFree(&pic);
265
0
  if (!ok) {
266
0
    WebPMemoryWriterClear(&wrt);
267
0
    *output = NULL;
268
0
    return 0;
269
0
  }
270
0
  *output = wrt.mem;
271
0
  return wrt.size;
272
0
}
273
274
#define ENCODE_FUNC(NAME, IMPORTER)                                     \
275
size_t NAME(const uint8_t* in, int w, int h, int bps, float q,          \
276
0
            uint8_t** out) {                                            \
277
0
  return Encode(in, w, h, bps, IMPORTER, q, 0, out);                    \
278
0
}
Unexecuted instantiation: WebPEncodeRGB
Unexecuted instantiation: WebPEncodeRGBA
Unexecuted instantiation: WebPEncodeBGR
Unexecuted instantiation: WebPEncodeBGRA
279
280
ENCODE_FUNC(WebPEncodeRGB, WebPPictureImportRGB)
281
ENCODE_FUNC(WebPEncodeRGBA, WebPPictureImportRGBA)
282
#if !defined(WEBP_REDUCE_CSP)
283
ENCODE_FUNC(WebPEncodeBGR, WebPPictureImportBGR)
284
ENCODE_FUNC(WebPEncodeBGRA, WebPPictureImportBGRA)
285
#endif  // WEBP_REDUCE_CSP
286
287
#undef ENCODE_FUNC
288
289
0
#define LOSSLESS_DEFAULT_QUALITY 70.
290
#define LOSSLESS_ENCODE_FUNC(NAME, IMPORTER)                                 \
291
0
size_t NAME(const uint8_t* in, int w, int h, int bps, uint8_t** out) {       \
292
0
  return Encode(in, w, h, bps, IMPORTER, LOSSLESS_DEFAULT_QUALITY, 1, out);  \
293
0
}
Unexecuted instantiation: WebPEncodeLosslessRGB
Unexecuted instantiation: WebPEncodeLosslessRGBA
Unexecuted instantiation: WebPEncodeLosslessBGR
Unexecuted instantiation: WebPEncodeLosslessBGRA
294
295
LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGB, WebPPictureImportRGB)
296
LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessRGBA, WebPPictureImportRGBA)
297
#if !defined(WEBP_REDUCE_CSP)
298
LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGR, WebPPictureImportBGR)
299
LOSSLESS_ENCODE_FUNC(WebPEncodeLosslessBGRA, WebPPictureImportBGRA)
300
#endif  // WEBP_REDUCE_CSP
301
302
#undef LOSSLESS_ENCODE_FUNC
303
304
//------------------------------------------------------------------------------