Coverage Report

Created: 2025-12-03 07:28

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