Coverage Report

Created: 2026-10-28 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/enc/iterator_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
// VP8Iterator: block iterator
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include <string.h>
15
16
#include "src/dsp/cpu.h"
17
#include "src/dsp/dsp.h"
18
#include "src/enc/vp8i_enc.h"
19
#include "src/utils/utils.h"
20
#include "src/webp/types.h"
21
22
//------------------------------------------------------------------------------
23
// VP8Iterator
24
//------------------------------------------------------------------------------
25
26
910k
static void InitLeft(VP8EncIterator* const it) {
27
910k
  it->y_left[-1] = it->u_left[-1] = it->v_left[-1] = (it->y > 0) ? 129 : 127;
28
910k
  memset(it->y_left, 129, 16);
29
910k
  memset(it->u_left, 129, 8);
30
910k
  memset(it->v_left, 129, 8);
31
910k
  it->left_nz[8] = 0;
32
910k
  if (it->top_derr != NULL) {
33
910k
    memset(&it->left_derr, 0, sizeof(it->left_derr));
34
910k
  }
35
910k
}
36
37
20.4k
static void InitTop(VP8EncIterator* const it) {
38
20.4k
  const VP8Encoder* const enc = it->enc;
39
20.4k
  const size_t top_size = enc->mb_w * 16;
40
20.4k
  memset(enc->y_top, 127, 2 * top_size);
41
20.4k
  memset(enc->nz, 0, enc->mb_w * sizeof(*enc->nz));
42
20.4k
  if (enc->top_derr != NULL) {
43
20.4k
    memset(enc->top_derr, 0, enc->mb_w * sizeof(*enc->top_derr));
44
20.4k
  }
45
20.4k
}
46
47
689k
void VP8IteratorSetRow(VP8EncIterator* const it, int y) {
48
689k
  VP8Encoder* const enc = it->enc;
49
689k
  it->x = 0;
50
689k
  it->y = y;
51
689k
  it->bw = &enc->parts[y & (enc->num_parts - 1)];
52
689k
  it->preds = enc->preds + y * 4 * enc->preds_w;
53
689k
  it->nz = enc->nz;
54
689k
  it->mb = enc->mb_info + y * enc->mb_w;
55
689k
  it->y_top = enc->y_top;
56
689k
  it->uv_top = enc->uv_top;
57
689k
  InitLeft(it);
58
689k
}
59
60
// restart a scan
61
20.4k
static void VP8IteratorReset(VP8EncIterator* const it) {
62
20.4k
  VP8Encoder* const enc = it->enc;
63
20.4k
  VP8IteratorSetRow(it, 0);
64
20.4k
  VP8IteratorSetCountDown(it, enc->mb_w * enc->mb_h);  // default
65
20.4k
  InitTop(it);
66
20.4k
  memset(it->bit_count, 0, sizeof(it->bit_count));
67
20.4k
  it->do_trellis = 0;
68
20.4k
}
69
70
27.2k
void VP8IteratorSetCountDown(VP8EncIterator* const it, int count_down) {
71
27.2k
  it->count_down = it->count_down0 = count_down;
72
27.2k
}
73
74
6.80k
int VP8IteratorIsDone(const VP8EncIterator* const it) {
75
6.80k
  return (it->count_down <= 0);
76
6.80k
}
77
78
20.4k
void VP8IteratorInit(VP8Encoder* const enc, VP8EncIterator* const it) {
79
20.4k
  it->enc = enc;
80
20.4k
  it->yuv_in = (uint8_t*)WEBP_ALIGN(it->yuv_mem);
81
20.4k
  it->yuv_out = it->yuv_in + YUV_SIZE_ENC;
82
20.4k
  it->yuv_out2 = it->yuv_out + YUV_SIZE_ENC;
83
20.4k
  it->yuv_p = it->yuv_out2 + YUV_SIZE_ENC;
84
20.4k
  it->lf_stats = enc->lf_stats;
85
20.4k
  it->percent0 = enc->percent;
86
20.4k
  it->y_left = (uint8_t*)WEBP_ALIGN(it->yuv_left_mem + 1);
87
20.4k
  it->u_left = it->y_left + 16 + 16;
88
20.4k
  it->v_left = it->u_left + 16;
89
20.4k
  it->top_derr = enc->top_derr;
90
20.4k
  VP8IteratorReset(it);
91
20.4k
}
92
93
11.3M
int VP8IteratorProgress(const VP8EncIterator* const it, int delta) {
94
11.3M
  VP8Encoder* const enc = it->enc;
95
11.3M
  if (delta && enc->pic->progress_hook != NULL) {
96
10.4M
    const int done = it->count_down0 - it->count_down;
97
10.4M
    const int percent = (it->count_down0 <= 0)
98
10.4M
                            ? it->percent0
99
10.4M
                            : it->percent0 + delta * done / it->count_down0;
100
10.4M
    return WebPReportProgress(enc->pic, percent, &enc->percent);
101
10.4M
  }
102
923k
  return 1;
103
11.3M
}
104
105
//------------------------------------------------------------------------------
106
// Import the source samples into the cache. Takes care of replicating
107
// boundary pixels if necessary.
108
109
22.6M
static WEBP_INLINE int MinSize(int a, int b) { return (a < b) ? a : b; }
110
111
static void ImportBlock(const uint8_t* src, int src_stride, uint8_t* dst, int w,
112
34.0M
                        int h, int size) {
113
34.0M
  int i;
114
394M
  for (i = 0; i < h; ++i) {
115
360M
    memcpy(dst, src, w);
116
360M
    if (w < size) {
117
12.4M
      memset(dst + w, dst[w - 1], size - w);
118
12.4M
    }
119
360M
    dst += BPS;
120
360M
    src += src_stride;
121
360M
  }
122
37.0M
  for (i = h; i < size; ++i) {
123
2.96M
    memcpy(dst, dst - BPS, size);
124
2.96M
    dst += BPS;
125
2.96M
  }
126
34.0M
}
127
128
static void ImportLine(const uint8_t* src, int src_stride, uint8_t* dst,
129
33.1M
                       int len, int total_len) {
130
33.1M
  int i;
131
381M
  for (i = 0; i < len; ++i, src += src_stride) dst[i] = *src;
132
38.5M
  for (; i < total_len; ++i) dst[i] = dst[len - 1];
133
33.1M
}
134
135
11.3M
void VP8IteratorImport(VP8EncIterator* const it, uint8_t* const tmp_32) {
136
11.3M
  const VP8Encoder* const enc = it->enc;
137
11.3M
  const int x = it->x, y = it->y;
138
11.3M
  const WebPPicture* const pic = enc->pic;
139
11.3M
  const uint8_t* const ysrc = pic->y + ((ptrdiff_t)y * pic->y_stride + x) * 16;
140
11.3M
  const uint8_t* const usrc = pic->u + ((ptrdiff_t)y * pic->uv_stride + x) * 8;
141
11.3M
  const uint8_t* const vsrc = pic->v + ((ptrdiff_t)y * pic->uv_stride + x) * 8;
142
11.3M
  const int w = MinSize(pic->width - x * 16, 16);
143
11.3M
  const int h = MinSize(pic->height - y * 16, 16);
144
11.3M
  const int uv_w = (w + 1) >> 1;
145
11.3M
  const int uv_h = (h + 1) >> 1;
146
147
11.3M
  ImportBlock(ysrc, pic->y_stride, it->yuv_in + Y_OFF_ENC, w, h, 16);
148
11.3M
  ImportBlock(usrc, pic->uv_stride, it->yuv_in + U_OFF_ENC, uv_w, uv_h, 8);
149
11.3M
  ImportBlock(vsrc, pic->uv_stride, it->yuv_in + V_OFF_ENC, uv_w, uv_h, 8);
150
151
11.3M
  if (tmp_32 == NULL) return;
152
153
  // Import source (uncompressed) samples into boundary.
154
5.67M
  if (x == 0) {
155
220k
    InitLeft(it);
156
5.45M
  } else {
157
5.45M
    if (y == 0) {
158
82.0k
      it->y_left[-1] = it->u_left[-1] = it->v_left[-1] = 127;
159
5.37M
    } else {
160
5.37M
      it->y_left[-1] = ysrc[-1 - pic->y_stride];
161
5.37M
      it->u_left[-1] = usrc[-1 - pic->uv_stride];
162
5.37M
      it->v_left[-1] = vsrc[-1 - pic->uv_stride];
163
5.37M
    }
164
5.45M
    ImportLine(ysrc - 1, pic->y_stride, it->y_left, h, 16);
165
5.45M
    ImportLine(usrc - 1, pic->uv_stride, it->u_left, uv_h, 8);
166
5.45M
    ImportLine(vsrc - 1, pic->uv_stride, it->v_left, uv_h, 8);
167
5.45M
  }
168
169
5.67M
  it->y_top = tmp_32 + 0;
170
5.67M
  it->uv_top = tmp_32 + 16;
171
5.67M
  if (y == 0) {
172
88.8k
    memset(tmp_32, 127, 32 * sizeof(*tmp_32));
173
5.58M
  } else {
174
5.58M
    ImportLine(ysrc - pic->y_stride, 1, tmp_32, w, 16);
175
5.58M
    ImportLine(usrc - pic->uv_stride, 1, tmp_32 + 16, uv_w, 8);
176
5.58M
    ImportLine(vsrc - pic->uv_stride, 1, tmp_32 + 16 + 8, uv_w, 8);
177
5.58M
  }
178
5.67M
}
179
180
//------------------------------------------------------------------------------
181
// Copy back the compressed samples into user space if requested.
182
183
static void ExportBlock(const uint8_t* src, uint8_t* dst, int dst_stride, int w,
184
0
                        int h) {
185
0
  while (h-- > 0) {
186
0
    memcpy(dst, src, w);
187
0
    dst += dst_stride;
188
0
    src += BPS;
189
0
  }
190
0
}
191
192
5.67M
void VP8IteratorExport(const VP8EncIterator* const it) {
193
5.67M
  const VP8Encoder* const enc = it->enc;
194
5.67M
  if (enc->config->show_compressed) {
195
0
    const int x = it->x, y = it->y;
196
0
    const uint8_t* const ysrc = it->yuv_out + Y_OFF_ENC;
197
0
    const uint8_t* const usrc = it->yuv_out + U_OFF_ENC;
198
0
    const uint8_t* const vsrc = it->yuv_out + V_OFF_ENC;
199
0
    const WebPPicture* const pic = enc->pic;
200
0
    uint8_t* const ydst = pic->y + ((ptrdiff_t)y * pic->y_stride + x) * 16;
201
0
    uint8_t* const udst = pic->u + ((ptrdiff_t)y * pic->uv_stride + x) * 8;
202
0
    uint8_t* const vdst = pic->v + ((ptrdiff_t)y * pic->uv_stride + x) * 8;
203
0
    int w = (pic->width - x * 16);
204
0
    int h = (pic->height - y * 16);
205
206
0
    if (w > 16) w = 16;
207
0
    if (h > 16) h = 16;
208
209
    // Luma plane
210
0
    ExportBlock(ysrc, ydst, pic->y_stride, w, h);
211
212
0
    {  // U/V planes
213
0
      const int uv_w = (w + 1) >> 1;
214
0
      const int uv_h = (h + 1) >> 1;
215
0
      ExportBlock(usrc, udst, pic->uv_stride, uv_w, uv_h);
216
0
      ExportBlock(vsrc, vdst, pic->uv_stride, uv_w, uv_h);
217
0
    }
218
0
  }
219
5.67M
}
220
221
//------------------------------------------------------------------------------
222
// Non-zero contexts setup/teardown
223
224
// Nz bits:
225
//  0  1  2  3  Y
226
//  4  5  6  7
227
//  8  9 10 11
228
// 12 13 14 15
229
// 16 17        U
230
// 18 19
231
// 20 21        V
232
// 22 23
233
// 24           DC-intra16
234
235
// Convert packed context to byte array
236
964M
#define BIT(nz, n) (!!((nz) & (1 << (n))))
237
238
56.7M
void VP8IteratorNzToBytes(VP8EncIterator* const it) {
239
56.7M
  const int tnz = it->nz[0], lnz = it->nz[-1];
240
56.7M
  int* const top_nz = it->top_nz;
241
56.7M
  int* const left_nz = it->left_nz;
242
243
  // Top-Y
244
56.7M
  top_nz[0] = BIT(tnz, 12);
245
56.7M
  top_nz[1] = BIT(tnz, 13);
246
56.7M
  top_nz[2] = BIT(tnz, 14);
247
56.7M
  top_nz[3] = BIT(tnz, 15);
248
  // Top-U
249
56.7M
  top_nz[4] = BIT(tnz, 18);
250
56.7M
  top_nz[5] = BIT(tnz, 19);
251
  // Top-V
252
56.7M
  top_nz[6] = BIT(tnz, 22);
253
56.7M
  top_nz[7] = BIT(tnz, 23);
254
  // DC
255
56.7M
  top_nz[8] = BIT(tnz, 24);
256
257
  // left-Y
258
56.7M
  left_nz[0] = BIT(lnz, 3);
259
56.7M
  left_nz[1] = BIT(lnz, 7);
260
56.7M
  left_nz[2] = BIT(lnz, 11);
261
56.7M
  left_nz[3] = BIT(lnz, 15);
262
  // left-U
263
56.7M
  left_nz[4] = BIT(lnz, 17);
264
56.7M
  left_nz[5] = BIT(lnz, 19);
265
  // left-V
266
56.7M
  left_nz[6] = BIT(lnz, 21);
267
56.7M
  left_nz[7] = BIT(lnz, 23);
268
  // left-DC is special, iterated separately
269
56.7M
}
270
271
5.67M
void VP8IteratorBytesToNz(VP8EncIterator* const it) {
272
5.67M
  uint32_t nz = 0;
273
5.67M
  const int* const top_nz = it->top_nz;
274
5.67M
  const int* const left_nz = it->left_nz;
275
  // top
276
5.67M
  nz |= (top_nz[0] << 12) | (top_nz[1] << 13);
277
5.67M
  nz |= (top_nz[2] << 14) | (top_nz[3] << 15);
278
5.67M
  nz |= (top_nz[4] << 18) | (top_nz[5] << 19);
279
5.67M
  nz |= (top_nz[6] << 22) | (top_nz[7] << 23);
280
5.67M
  nz |= (top_nz[8] << 24);  // we propagate the _top_ bit, esp. for intra4
281
  // left. left_nz[3], [5] and [7] are already set, as top_nz[3], [5], [7].
282
5.67M
  nz |= (left_nz[0] << 3) | (left_nz[1] << 7);
283
5.67M
  nz |= (left_nz[2] << 11);
284
5.67M
  nz |= (left_nz[4] << 17) | (left_nz[6] << 21);
285
286
5.67M
  *it->nz = nz;
287
5.67M
}
288
289
#undef BIT
290
291
//------------------------------------------------------------------------------
292
// Advance to the next position, doing the bookkeeping.
293
294
5.67M
void VP8IteratorSaveBoundary(VP8EncIterator* const it) {
295
5.67M
  VP8Encoder* const enc = it->enc;
296
5.67M
  const int x = it->x, y = it->y;
297
5.67M
  const uint8_t* const ysrc = it->yuv_out + Y_OFF_ENC;
298
5.67M
  const uint8_t* const uvsrc = it->yuv_out + U_OFF_ENC;
299
5.67M
  if (x < enc->mb_w - 1) {  // left
300
5.45M
    int i;
301
92.7M
    for (i = 0; i < 16; ++i) {
302
87.2M
      it->y_left[i] = ysrc[15 + i * BPS];
303
87.2M
    }
304
49.0M
    for (i = 0; i < 8; ++i) {
305
43.6M
      it->u_left[i] = uvsrc[7 + i * BPS];
306
43.6M
      it->v_left[i] = uvsrc[15 + i * BPS];
307
43.6M
    }
308
    // top-left (before 'top'!)
309
5.45M
    it->y_left[-1] = it->y_top[15];
310
5.45M
    it->u_left[-1] = it->uv_top[0 + 7];
311
5.45M
    it->v_left[-1] = it->uv_top[8 + 7];
312
5.45M
  }
313
5.67M
  if (y < enc->mb_h - 1) {  // top
314
5.58M
    memcpy(it->y_top, ysrc + 15 * BPS, 16);
315
5.58M
    memcpy(it->uv_top, uvsrc + 7 * BPS, 8 + 8);
316
5.58M
  }
317
5.67M
}
318
319
17.0M
int VP8IteratorNext(VP8EncIterator* const it) {
320
17.0M
  if (++it->x == it->enc->mb_w) {
321
662k
    VP8IteratorSetRow(it, ++it->y);
322
16.3M
  } else {
323
16.3M
    it->preds += 4;
324
16.3M
    it->mb += 1;
325
16.3M
    it->nz += 1;
326
16.3M
    it->y_top += 16;
327
16.3M
    it->uv_top += 16;
328
16.3M
  }
329
17.0M
  return (0 < --it->count_down);
330
17.0M
}
331
332
//------------------------------------------------------------------------------
333
// Helper function to set mode properties
334
335
17.0M
void VP8SetIntra16Mode(const VP8EncIterator* const it, int mode) {
336
17.0M
  uint8_t* preds = it->preds;
337
17.0M
  int y;
338
85.1M
  for (y = 0; y < 4; ++y) {
339
68.0M
    memset(preds, mode, 4);
340
68.0M
    preds += it->enc->preds_w;
341
68.0M
  }
342
17.0M
  it->mb->type = 1;
343
17.0M
}
344
345
1.87M
void VP8SetIntra4Mode(const VP8EncIterator* const it, const uint8_t* modes) {
346
1.87M
  uint8_t* preds = it->preds;
347
1.87M
  int y;
348
9.36M
  for (y = 4; y > 0; --y) {
349
7.48M
    memcpy(preds, modes, 4 * sizeof(*modes));
350
7.48M
    preds += it->enc->preds_w;
351
7.48M
    modes += 4;
352
7.48M
  }
353
1.87M
  it->mb->type = 0;
354
1.87M
}
355
356
11.3M
void VP8SetIntraUVMode(const VP8EncIterator* const it, int mode) {
357
11.3M
  it->mb->uv_mode = mode;
358
11.3M
}
359
360
11.3M
void VP8SetSkip(const VP8EncIterator* const it, int skip) {
361
11.3M
  it->mb->skip = skip;
362
11.3M
}
363
364
5.67M
void VP8SetSegment(const VP8EncIterator* const it, int segment) {
365
5.67M
  it->mb->segment = segment;
366
5.67M
}
367
368
//------------------------------------------------------------------------------
369
// Intra4x4 sub-blocks iteration
370
//
371
//  We store and update the boundary samples into an array of 37 pixels. They
372
//  are updated as we iterate and reconstructs each intra4x4 blocks in turn.
373
//  The position of the samples has the following snake pattern:
374
//
375
// 16|17 18 19 20|21 22 23 24|25 26 27 28|29 30 31 32|33 34 35 36  <- Top-right
376
// --+-----------+-----------+-----------+-----------+
377
// 15|         19|         23|         27|         31|
378
// 14|         18|         22|         26|         30|
379
// 13|         17|         21|         25|         29|
380
// 12|13 14 15 16|17 18 19 20|21 22 23 24|25 26 27 28|
381
// --+-----------+-----------+-----------+-----------+
382
// 11|         15|         19|         23|         27|
383
// 10|         14|         18|         22|         26|
384
//  9|         13|         17|         21|         25|
385
//  8| 9 10 11 12|13 14 15 16|17 18 19 20|21 22 23 24|
386
// --+-----------+-----------+-----------+-----------+
387
//  7|         11|         15|         19|         23|
388
//  6|         10|         14|         18|         22|
389
//  5|          9|         13|         17|         21|
390
//  4| 5  6  7  8| 9 10 11 12|13 14 15 16|17 18 19 20|
391
// --+-----------+-----------+-----------+-----------+
392
//  3|          7|         11|         15|         19|
393
//  2|          6|         10|         14|         18|
394
//  1|          5|          9|         13|         17|
395
//  0| 1  2  3  4| 5  6  7  8| 9 10 11 12|13 14 15 16|
396
// --+-----------+-----------+-----------+-----------+
397
398
// Array to record the position of the top sample to pass to the prediction
399
// functions in dsp.c.
400
static const uint8_t VP8TopLeftI4[16] = {17, 21, 25, 29, 13, 17, 21, 25,
401
                                         9,  13, 17, 21, 5,  9,  13, 17};
402
403
5.67M
void VP8IteratorStartI4(VP8EncIterator* const it) {
404
5.67M
  const VP8Encoder* const enc = it->enc;
405
5.67M
  int i;
406
407
5.67M
  it->i4 = 0;  // first 4x4 sub-block
408
5.67M
  it->i4_top = it->i4_boundary + VP8TopLeftI4[0];
409
410
  // Import the boundary samples
411
102M
  for (i = 0; i < 17; ++i) {  // left
412
96.4M
    it->i4_boundary[i] = it->y_left[15 - i];
413
96.4M
  }
414
96.4M
  for (i = 0; i < 16; ++i) {  // top
415
90.7M
    it->i4_boundary[17 + i] = it->y_top[i];
416
90.7M
  }
417
  // top-right samples have a special case on the far right of the picture
418
5.67M
  if (it->x < enc->mb_w - 1) {
419
27.2M
    for (i = 16; i < 16 + 4; ++i) {
420
21.8M
      it->i4_boundary[17 + i] = it->y_top[i];
421
21.8M
    }
422
5.45M
  } else {  // else, replicate the last valid pixel four times
423
1.10M
    for (i = 16; i < 16 + 4; ++i) {
424
883k
      it->i4_boundary[17 + i] = it->i4_boundary[17 + 15];
425
883k
    }
426
220k
  }
427
#if WEBP_AARCH64 && BPS == 32 && defined(WEBP_MSAN)
428
  // Intra4Preds_NEON() reads 3 uninitialized bytes from 'i4_boundary' when top
429
  // is positioned at offset 29 (VP8TopLeftI4[3]). The values are not used
430
  // meaningfully, but due to limitations in MemorySanitizer related to
431
  // modeling of tbl instructions, a warning will be issued. This can be
432
  // removed if MSan is updated to support the instructions. See
433
  // https://issues.webmproject.org/372109644.
434
  memset(it->i4_boundary + sizeof(it->i4_boundary) - 3, 0xaa, 3);
435
#endif
436
5.67M
  VP8IteratorNzToBytes(it);  // import the non-zero context
437
5.67M
}
438
439
int VP8IteratorRotateI4(VP8EncIterator* const it,
440
55.2M
                        const uint8_t* const yuv_out) {
441
55.2M
  const uint8_t* const blk = yuv_out + VP8Scan[it->i4];
442
55.2M
  uint8_t* const top = it->i4_top;
443
55.2M
  int i;
444
445
  // Update the cache with 7 fresh samples
446
276M
  for (i = 0; i <= 3; ++i) {
447
221M
    top[-4 + i] = blk[i + 3 * BPS];  // store future top samples
448
221M
  }
449
55.2M
  if ((it->i4 & 3) != 3) {  // if not on the right sub-blocks #3, #7, #11, #15
450
173M
    for (i = 0; i <= 2; ++i) {  // store future left samples
451
129M
      top[i] = blk[3 + (2 - i) * BPS];
452
129M
    }
453
43.2M
  } else {  // else replicate top-right samples, as says the specs.
454
60.0M
    for (i = 0; i <= 3; ++i) {
455
48.0M
      top[i] = top[i + 4];
456
48.0M
    }
457
12.0M
  }
458
  // move pointers to next sub-block
459
55.2M
  ++it->i4;
460
55.2M
  if (it->i4 == 16) {  // we're done
461
1.87M
    return 0;
462
1.87M
  }
463
464
53.3M
  it->i4_top = it->i4_boundary + VP8TopLeftI4[it->i4];
465
53.3M
  return 1;
466
55.2M
}
467
468
//------------------------------------------------------------------------------