Coverage Report

Created: 2025-06-16 07:00

/src/libwebp/src/enc/webp_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
// WebP encoder: main entry point
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include <assert.h>
15
#include <math.h>
16
#include <stdlib.h>
17
#include <string.h>
18
19
#include "src/dec/common_dec.h"
20
#include "src/webp/types.h"
21
#include "src/dsp/dsp.h"
22
#include "src/enc/cost_enc.h"
23
#include "src/enc/vp8i_enc.h"
24
#include "src/enc/vp8li_enc.h"
25
#include "src/utils/utils.h"
26
#include "src/webp/encode.h"
27
28
// #define PRINT_MEMORY_INFO
29
30
#ifdef PRINT_MEMORY_INFO
31
#include <stdio.h>
32
#endif
33
34
//------------------------------------------------------------------------------
35
36
30
int WebPGetEncoderVersion(void) {
37
30
  return (ENC_MAJ_VERSION << 16) | (ENC_MIN_VERSION << 8) | ENC_REV_VERSION;
38
30
}
39
40
//------------------------------------------------------------------------------
41
// VP8Encoder
42
//------------------------------------------------------------------------------
43
44
1.82k
static void ResetSegmentHeader(VP8Encoder* const enc) {
45
1.82k
  VP8EncSegmentHeader* const hdr = &enc->segment_hdr;
46
1.82k
  hdr->num_segments = enc->config->segments;
47
1.82k
  hdr->update_map  = (hdr->num_segments > 1);
48
1.82k
  hdr->size = 0;
49
1.82k
}
50
51
1.82k
static void ResetFilterHeader(VP8Encoder* const enc) {
52
1.82k
  VP8EncFilterHeader* const hdr = &enc->filter_hdr;
53
1.82k
  hdr->simple = 1;
54
1.82k
  hdr->level = 0;
55
1.82k
  hdr->sharpness = 0;
56
1.82k
  hdr->i4x4_lf_delta = 0;
57
1.82k
}
58
59
1.82k
static void ResetBoundaryPredictions(VP8Encoder* const enc) {
60
  // init boundary values once for all
61
  // Note: actually, initializing the 'preds[]' is only needed for intra4.
62
1.82k
  int i;
63
1.82k
  uint8_t* const top = enc->preds - enc->preds_w;
64
1.82k
  uint8_t* const left = enc->preds - 1;
65
245k
  for (i = -1; i < 4 * enc->mb_w; ++i) {
66
243k
    top[i] = B_DC_PRED;
67
243k
  }
68
262k
  for (i = 0; i < 4 * enc->mb_h; ++i) {
69
260k
    left[i * enc->preds_w] = B_DC_PRED;
70
260k
  }
71
1.82k
  enc->nz[-1] = 0;   // constant
72
1.82k
}
73
74
// Mapping from config->method to coding tools used.
75
//-------------------+---+---+---+---+---+---+---+
76
//   Method          | 0 | 1 | 2 | 3 |(4)| 5 | 6 |
77
//-------------------+---+---+---+---+---+---+---+
78
// fast probe        | x |   |   | x |   |   |   |
79
//-------------------+---+---+---+---+---+---+---+
80
// dynamic proba     | ~ | x | x | x | x | x | x |
81
//-------------------+---+---+---+---+---+---+---+
82
// fast mode analysis|[x]|[x]|   |   | x | x | x |
83
//-------------------+---+---+---+---+---+---+---+
84
// basic rd-opt      |   |   |   | x | x | x | x |
85
//-------------------+---+---+---+---+---+---+---+
86
// disto-refine i4/16| x | x | x |   |   |   |   |
87
//-------------------+---+---+---+---+---+---+---+
88
// disto-refine uv   |   | x | x |   |   |   |   |
89
//-------------------+---+---+---+---+---+---+---+
90
// rd-opt i4/16      |   |   | ~ | x | x | x | x |
91
//-------------------+---+---+---+---+---+---+---+
92
// token buffer (opt)|   |   |   | x | x | x | x |
93
//-------------------+---+---+---+---+---+---+---+
94
// Trellis           |   |   |   |   |   | x |Ful|
95
//-------------------+---+---+---+---+---+---+---+
96
// full-SNS          |   |   |   |   | x | x | x |
97
//-------------------+---+---+---+---+---+---+---+
98
99
1.82k
static void MapConfigToTools(VP8Encoder* const enc) {
100
1.82k
  const WebPConfig* const config = enc->config;
101
1.82k
  const int method = config->method;
102
1.82k
  const int limit = 100 - config->partition_limit;
103
1.82k
  enc->method = method;
104
1.82k
  enc->rd_opt_level = (method >= 6) ? RD_OPT_TRELLIS_ALL
105
1.82k
                    : (method >= 5) ? RD_OPT_TRELLIS
106
1.82k
                    : (method >= 3) ? RD_OPT_BASIC
107
1.82k
                    : RD_OPT_NONE;
108
1.82k
  enc->max_i4_header_bits =
109
1.82k
      256 * 16 * 16 *                 // upper bound: up to 16bit per 4x4 block
110
1.82k
      (limit * limit) / (100 * 100);  // ... modulated with a quadratic curve.
111
112
  // partition0 = 512k max.
113
1.82k
  enc->mb_header_limit =
114
1.82k
      (score_t)256 * 510 * 8 * 1024 / (enc->mb_w * enc->mb_h);
115
116
1.82k
  enc->thread_level = config->thread_level;
117
118
1.82k
  enc->do_search = (config->target_size > 0 || config->target_PSNR > 0);
119
1.82k
  if (!config->low_memory) {
120
1.82k
#if !defined(DISABLE_TOKEN_BUFFER)
121
1.82k
    enc->use_tokens = (enc->rd_opt_level >= RD_OPT_BASIC);  // need rd stats
122
1.82k
#endif
123
1.82k
    if (enc->use_tokens) {
124
1.82k
      enc->num_parts = 1;   // doesn't work with multi-partition
125
1.82k
    }
126
1.82k
  }
127
1.82k
}
128
129
// Memory scaling with dimensions:
130
//  memory (bytes) ~= 2.25 * w + 0.0625 * w * h
131
//
132
// Typical memory footprint (614x440 picture)
133
//              encoder: 22111
134
//                 info: 4368
135
//                preds: 17741
136
//          top samples: 1263
137
//             non-zero: 175
138
//             lf-stats: 0
139
//                total: 45658
140
// Transient object sizes:
141
//       VP8EncIterator: 3360
142
//         VP8ModeScore: 872
143
//       VP8SegmentInfo: 732
144
//          VP8EncProba: 18352
145
//              LFStats: 2048
146
// Picture size (yuv): 419328
147
148
static VP8Encoder* InitVP8Encoder(const WebPConfig* const config,
149
1.82k
                                  WebPPicture* const picture) {
150
1.82k
  VP8Encoder* enc;
151
1.82k
  const int use_filter =
152
1.82k
      (config->filter_strength > 0) || (config->autofilter > 0);
153
1.82k
  const int mb_w = (picture->width + 15) >> 4;
154
1.82k
  const int mb_h = (picture->height + 15) >> 4;
155
1.82k
  const int preds_w = 4 * mb_w + 1;
156
1.82k
  const int preds_h = 4 * mb_h + 1;
157
1.82k
  const size_t preds_size = preds_w * preds_h * sizeof(*enc->preds);
158
1.82k
  const int top_stride = mb_w * 16;
159
1.82k
  const size_t nz_size = (mb_w + 1) * sizeof(*enc->nz) + WEBP_ALIGN_CST;
160
1.82k
  const size_t info_size = mb_w * mb_h * sizeof(*enc->mb_info);
161
1.82k
  const size_t samples_size =
162
1.82k
      2 * top_stride * sizeof(*enc->y_top)   // top-luma/u/v
163
1.82k
      + WEBP_ALIGN_CST;                      // align all
164
1.82k
  const size_t lf_stats_size =
165
1.82k
      config->autofilter ? sizeof(*enc->lf_stats) + WEBP_ALIGN_CST : 0;
166
1.82k
  const size_t top_derr_size =
167
1.82k
      (config->quality <= ERROR_DIFFUSION_QUALITY || config->pass > 1) ?
168
1.82k
          mb_w * sizeof(*enc->top_derr) : 0;
169
1.82k
  uint8_t* mem;
170
1.82k
  const uint64_t size = (uint64_t)sizeof(*enc)   // main struct
171
1.82k
                      + WEBP_ALIGN_CST           // cache alignment
172
1.82k
                      + info_size                // modes info
173
1.82k
                      + preds_size               // prediction modes
174
1.82k
                      + samples_size             // top/left samples
175
1.82k
                      + top_derr_size            // top diffusion error
176
1.82k
                      + nz_size                  // coeff context bits
177
1.82k
                      + lf_stats_size;           // autofilter stats
178
179
#ifdef PRINT_MEMORY_INFO
180
  printf("===================================\n");
181
  printf("Memory used:\n"
182
         "             encoder: %ld\n"
183
         "                info: %ld\n"
184
         "               preds: %ld\n"
185
         "         top samples: %ld\n"
186
         "       top diffusion: %ld\n"
187
         "            non-zero: %ld\n"
188
         "            lf-stats: %ld\n"
189
         "               total: %ld\n",
190
         sizeof(*enc) + WEBP_ALIGN_CST, info_size,
191
         preds_size, samples_size, top_derr_size, nz_size, lf_stats_size, size);
192
  printf("Transient object sizes:\n"
193
         "      VP8EncIterator: %ld\n"
194
         "        VP8ModeScore: %ld\n"
195
         "      VP8SegmentInfo: %ld\n"
196
         "         VP8EncProba: %ld\n"
197
         "             LFStats: %ld\n",
198
         sizeof(VP8EncIterator), sizeof(VP8ModeScore),
199
         sizeof(VP8SegmentInfo), sizeof(VP8EncProba),
200
         sizeof(LFStats));
201
  printf("Picture size (yuv): %ld\n",
202
         mb_w * mb_h * 384 * sizeof(uint8_t));
203
  printf("===================================\n");
204
#endif
205
1.82k
  mem = (uint8_t*)WebPSafeMalloc(size, sizeof(*mem));
206
1.82k
  if (mem == NULL) {
207
0
    WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
208
0
    return NULL;
209
0
  }
210
1.82k
  enc = (VP8Encoder*)mem;
211
1.82k
  mem = (uint8_t*)WEBP_ALIGN(mem + sizeof(*enc));
212
1.82k
  memset(enc, 0, sizeof(*enc));
213
1.82k
  enc->num_parts = 1 << config->partitions;
214
1.82k
  enc->mb_w = mb_w;
215
1.82k
  enc->mb_h = mb_h;
216
1.82k
  enc->preds_w = preds_w;
217
1.82k
  enc->mb_info = (VP8MBInfo*)mem;
218
1.82k
  mem += info_size;
219
1.82k
  enc->preds = mem + 1 + enc->preds_w;
220
1.82k
  mem += preds_size;
221
1.82k
  enc->nz = 1 + (uint32_t*)WEBP_ALIGN(mem);
222
1.82k
  mem += nz_size;
223
1.82k
  enc->lf_stats = lf_stats_size ? (LFStats*)WEBP_ALIGN(mem) : NULL;
224
1.82k
  mem += lf_stats_size;
225
226
  // top samples (all 16-aligned)
227
1.82k
  mem = (uint8_t*)WEBP_ALIGN(mem);
228
1.82k
  enc->y_top = mem;
229
1.82k
  enc->uv_top = enc->y_top + top_stride;
230
1.82k
  mem += 2 * top_stride;
231
1.82k
  enc->top_derr = top_derr_size ? (DError*)mem : NULL;
232
1.82k
  mem += top_derr_size;
233
1.82k
  assert(mem <= (uint8_t*)enc + size);
234
235
1.82k
  enc->config = config;
236
1.82k
  enc->profile = use_filter ? ((config->filter_type == 1) ? 0 : 1) : 2;
237
1.82k
  enc->pic = picture;
238
1.82k
  enc->percent = 0;
239
240
1.82k
  MapConfigToTools(enc);
241
1.82k
  VP8EncDspInit();
242
1.82k
  VP8DefaultProbas(enc);
243
1.82k
  ResetSegmentHeader(enc);
244
1.82k
  ResetFilterHeader(enc);
245
1.82k
  ResetBoundaryPredictions(enc);
246
1.82k
  VP8EncDspCostInit();
247
1.82k
  VP8EncInitAlpha(enc);
248
249
  // lower quality means smaller output -> we modulate a little the page
250
  // size based on quality. This is just a crude 1rst-order prediction.
251
1.82k
  {
252
1.82k
    const float scale = 1.f + config->quality * 5.f / 100.f;  // in [1,6]
253
1.82k
    VP8TBufferInit(&enc->tokens, (int)(mb_w * mb_h * 4 * scale));
254
1.82k
  }
255
1.82k
  return enc;
256
1.82k
}
257
258
1.82k
static int DeleteVP8Encoder(VP8Encoder* enc) {
259
1.82k
  int ok = 1;
260
1.82k
  if (enc != NULL) {
261
1.82k
    ok = VP8EncDeleteAlpha(enc);
262
1.82k
    VP8TBufferClear(&enc->tokens);
263
1.82k
    WebPSafeFree(enc);
264
1.82k
  }
265
1.82k
  return ok;
266
1.82k
}
267
268
//------------------------------------------------------------------------------
269
270
#if !defined(WEBP_DISABLE_STATS)
271
0
static double GetPSNR(uint64_t err, uint64_t size) {
272
0
  return (err > 0 && size > 0) ? 10. * log10(255. * 255. * size / err) : 99.;
273
0
}
274
275
0
static void FinalizePSNR(const VP8Encoder* const enc) {
276
0
  WebPAuxStats* stats = enc->pic->stats;
277
0
  const uint64_t size = enc->sse_count;
278
0
  const uint64_t* const sse = enc->sse;
279
0
  stats->PSNR[0] = (float)GetPSNR(sse[0], size);
280
0
  stats->PSNR[1] = (float)GetPSNR(sse[1], size / 4);
281
0
  stats->PSNR[2] = (float)GetPSNR(sse[2], size / 4);
282
0
  stats->PSNR[3] = (float)GetPSNR(sse[0] + sse[1] + sse[2], size * 3 / 2);
283
0
  stats->PSNR[4] = (float)GetPSNR(sse[3], size);
284
0
}
285
#endif  // !defined(WEBP_DISABLE_STATS)
286
287
1.82k
static void StoreStats(VP8Encoder* const enc) {
288
1.82k
#if !defined(WEBP_DISABLE_STATS)
289
1.82k
  WebPAuxStats* const stats = enc->pic->stats;
290
1.82k
  if (stats != NULL) {
291
0
    int i, s;
292
0
    for (i = 0; i < NUM_MB_SEGMENTS; ++i) {
293
0
      stats->segment_level[i] = enc->dqm[i].fstrength;
294
0
      stats->segment_quant[i] = enc->dqm[i].quant;
295
0
      for (s = 0; s <= 2; ++s) {
296
0
        stats->residual_bytes[s][i] = enc->residual_bytes[s][i];
297
0
      }
298
0
    }
299
0
    FinalizePSNR(enc);
300
0
    stats->coded_size = enc->coded_size;
301
0
    for (i = 0; i < 3; ++i) {
302
0
      stats->block_count[i] = enc->block_count[i];
303
0
    }
304
0
  }
305
#else  // defined(WEBP_DISABLE_STATS)
306
  WebPReportProgress(enc->pic, 100, &enc->percent);  // done!
307
#endif  // !defined(WEBP_DISABLE_STATS)
308
1.82k
}
309
310
int WebPEncodingSetError(const WebPPicture* const pic,
311
8.23k
                         WebPEncodingError error) {
312
8.23k
  assert((int)error < VP8_ENC_ERROR_LAST);
313
8.23k
  assert((int)error >= VP8_ENC_OK);
314
  // The oldest error reported takes precedence over the new one.
315
8.23k
  if (pic->error_code == VP8_ENC_OK) {
316
8.23k
    ((WebPPicture*)pic)->error_code = error;
317
8.23k
  }
318
8.23k
  return 0;
319
8.23k
}
320
321
int WebPReportProgress(const WebPPicture* const pic,
322
448M
                       int percent, int* const percent_store) {
323
448M
  if (percent_store != NULL && percent != *percent_store) {
324
88.7k
    *percent_store = percent;
325
88.7k
    if (pic->progress_hook && !pic->progress_hook(percent, pic)) {
326
      // user abort requested
327
0
      return WebPEncodingSetError(pic, VP8_ENC_ERROR_USER_ABORT);
328
0
    }
329
88.7k
  }
330
448M
  return 1;  // ok
331
448M
}
332
//------------------------------------------------------------------------------
333
334
2.88k
int WebPEncode(const WebPConfig* config, WebPPicture* pic) {
335
2.88k
  int ok = 0;
336
2.88k
  if (pic == NULL) return 0;
337
338
2.88k
  pic->error_code = VP8_ENC_OK;  // all ok so far
339
2.88k
  if (config == NULL) {  // bad params
340
0
    return WebPEncodingSetError(pic, VP8_ENC_ERROR_NULL_PARAMETER);
341
0
  }
342
2.88k
  if (!WebPValidateConfig(config)) {
343
0
    return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION);
344
0
  }
345
2.88k
  if (!WebPValidatePicture(pic)) return 0;
346
2.88k
  if (pic->width > WEBP_MAX_DIMENSION || pic->height > WEBP_MAX_DIMENSION) {
347
0
    return WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_DIMENSION);
348
0
  }
349
350
2.88k
  if (pic->stats != NULL) memset(pic->stats, 0, sizeof(*pic->stats));
351
352
2.88k
  if (!config->lossless) {
353
1.82k
    VP8Encoder* enc = NULL;
354
355
1.82k
    if (pic->use_argb || pic->y == NULL || pic->u == NULL || pic->v == NULL) {
356
      // Make sure we have YUVA samples.
357
1.82k
      if (config->use_sharp_yuv || (config->preprocessing & 4)) {
358
0
        if (!WebPPictureSharpARGBToYUVA(pic)) {
359
0
          return 0;
360
0
        }
361
1.82k
      } else {
362
1.82k
        float dithering = 0.f;
363
1.82k
        if (config->preprocessing & 2) {
364
0
          const float x = config->quality / 100.f;
365
0
          const float x2 = x * x;
366
          // slowly decreasing from max dithering at low quality (q->0)
367
          // to 0.5 dithering amplitude at high quality (q->100)
368
0
          dithering = 1.0f + (0.5f - 1.0f) * x2 * x2;
369
0
        }
370
1.82k
        if (!WebPPictureARGBToYUVADithered(pic, WEBP_YUV420, dithering)) {
371
0
          return 0;
372
0
        }
373
1.82k
      }
374
1.82k
    }
375
376
1.82k
    if (!config->exact) {
377
1.82k
      WebPCleanupTransparentArea(pic);
378
1.82k
    }
379
380
1.82k
    enc = InitVP8Encoder(config, pic);
381
1.82k
    if (enc == NULL) return 0;  // pic->error is already set.
382
    // Note: each of the tasks below account for 20% in the progress report.
383
1.82k
    ok = VP8EncAnalyze(enc);
384
385
    // Analysis is done, proceed to actual coding.
386
1.82k
    ok = ok && VP8EncStartAlpha(enc);   // possibly done in parallel
387
1.82k
    if (!enc->use_tokens) {
388
0
      ok = ok && VP8EncLoop(enc);
389
1.82k
    } else {
390
1.82k
      ok = ok && VP8EncTokenLoop(enc);
391
1.82k
    }
392
1.82k
    ok = ok && VP8EncFinishAlpha(enc);
393
394
1.82k
    ok = ok && VP8EncWrite(enc);
395
1.82k
    StoreStats(enc);
396
1.82k
    if (!ok) {
397
0
      VP8EncFreeBitWriters(enc);
398
0
    }
399
1.82k
    ok &= DeleteVP8Encoder(enc);  // must always be called, even if !ok
400
1.82k
  } else {
401
    // Make sure we have ARGB samples.
402
1.06k
    if (pic->argb == NULL && !WebPPictureYUVAToARGB(pic)) {
403
0
      return 0;
404
0
    }
405
406
1.06k
    if (!config->exact) {
407
1.06k
      WebPReplaceTransparentPixels(pic, 0x000000);
408
1.06k
    }
409
410
1.06k
    ok = VP8LEncodeImage(config, pic);  // Sets pic->error in case of problem.
411
1.06k
  }
412
413
2.88k
  return ok;
414
2.88k
}