Coverage Report

Created: 2025-10-12 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/dec/frame_dec.c
Line
Count
Source
1
// Copyright 2010 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
// Frame-reconstruction function. Memory allocation.
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include <assert.h>
15
#include <stdlib.h>
16
#include <string.h>
17
18
#include "src/dec/common_dec.h"
19
#include "src/dec/vp8_dec.h"
20
#include "src/dec/vp8i_dec.h"
21
#include "src/dec/webpi_dec.h"
22
#include "src/dsp/dsp.h"
23
#include "src/utils/random_utils.h"
24
#include "src/utils/thread_utils.h"
25
#include "src/utils/utils.h"
26
#include "src/webp/decode.h"
27
#include "src/webp/types.h"
28
29
//------------------------------------------------------------------------------
30
// Main reconstruction function.
31
32
static const uint16_t kScan[16] = {
33
    0 + 0 * BPS,  4 + 0 * BPS,  8 + 0 * BPS,  12 + 0 * BPS,
34
    0 + 4 * BPS,  4 + 4 * BPS,  8 + 4 * BPS,  12 + 4 * BPS,
35
    0 + 8 * BPS,  4 + 8 * BPS,  8 + 8 * BPS,  12 + 8 * BPS,
36
    0 + 12 * BPS, 4 + 12 * BPS, 8 + 12 * BPS, 12 + 12 * BPS};
37
38
85.8k
static int CheckMode(int mb_x, int mb_y, int mode) {
39
85.8k
  if (mode == B_DC_PRED) {
40
43.2k
    if (mb_x == 0) {
41
23.4k
      return (mb_y == 0) ? B_DC_PRED_NOTOPLEFT : B_DC_PRED_NOLEFT;
42
23.4k
    } else {
43
19.8k
      return (mb_y == 0) ? B_DC_PRED_NOTOP : B_DC_PRED;
44
19.8k
    }
45
43.2k
  }
46
42.6k
  return mode;
47
85.8k
}
48
49
918k
static void Copy32b(uint8_t* const dst, const uint8_t* const src) {
50
918k
  memcpy(dst, src, 4);
51
918k
}
52
53
static WEBP_INLINE void DoTransform(uint32_t bits, const int16_t* const src,
54
490k
                                    uint8_t* const dst) {
55
490k
  switch (bits >> 30) {
56
22.0k
    case 3:
57
22.0k
      VP8Transform(src, dst, 0);
58
22.0k
      break;
59
20.6k
    case 2:
60
20.6k
      VP8TransformAC3(src, dst);
61
20.6k
      break;
62
67.6k
    case 1:
63
67.6k
      VP8TransformDC(src, dst);
64
67.6k
      break;
65
380k
    default:
66
380k
      break;
67
490k
  }
68
490k
}
69
70
static void DoUVTransform(uint32_t bits, const int16_t* const src,
71
109k
                          uint8_t* const dst) {
72
109k
  if (bits & 0xff) {             // any non-zero coeff at all?
73
14.4k
    if (bits & 0xaa) {           // any non-zero AC coefficient?
74
4.85k
      VP8TransformUV(src, dst);  // note we don't use the AC3 variant for U/V
75
9.57k
    } else {
76
9.57k
      VP8TransformDCUV(src, dst);
77
9.57k
    }
78
14.4k
  }
79
109k
}
80
81
static void ReconstructRow(const VP8Decoder* const dec,
82
28.4k
                           const VP8ThreadContext* ctx) {
83
28.4k
  int j;
84
28.4k
  int mb_x;
85
28.4k
  const int mb_y = ctx->mb_y;
86
28.4k
  const int cache_id = ctx->id;
87
28.4k
  uint8_t* const y_dst = dec->yuv_b + Y_OFF;
88
28.4k
  uint8_t* const u_dst = dec->yuv_b + U_OFF;
89
28.4k
  uint8_t* const v_dst = dec->yuv_b + V_OFF;
90
91
  // Initialize left-most block.
92
483k
  for (j = 0; j < 16; ++j) {
93
455k
    y_dst[j * BPS - 1] = 129;
94
455k
  }
95
255k
  for (j = 0; j < 8; ++j) {
96
227k
    u_dst[j * BPS - 1] = 129;
97
227k
    v_dst[j * BPS - 1] = 129;
98
227k
  }
99
100
  // Init top-left sample on left column too.
101
28.4k
  if (mb_y > 0) {
102
26.9k
    y_dst[-1 - BPS] = u_dst[-1 - BPS] = v_dst[-1 - BPS] = 129;
103
26.9k
  } else {
104
    // we only need to do this init once at block (0,0).
105
    // Afterward, it remains valid for the whole topmost row.
106
1.50k
    memset(y_dst - BPS - 1, 127, 16 + 4 + 1);
107
1.50k
    memset(u_dst - BPS - 1, 127, 8 + 1);
108
1.50k
    memset(v_dst - BPS - 1, 127, 8 + 1);
109
1.50k
  }
110
111
  // Reconstruct one row.
112
83.1k
  for (mb_x = 0; mb_x < dec->mb_w; ++mb_x) {
113
54.6k
    const VP8MBData* const block = ctx->mb_data + mb_x;
114
115
    // Rotate in the left samples from previously decoded block. We move four
116
    // pixels at a time for alignment reason, and because of in-loop filter.
117
54.6k
    if (mb_x > 0) {
118
472k
      for (j = -1; j < 16; ++j) {
119
446k
        Copy32b(&y_dst[j * BPS - 4], &y_dst[j * BPS + 12]);
120
446k
      }
121
262k
      for (j = -1; j < 8; ++j) {
122
236k
        Copy32b(&u_dst[j * BPS - 4], &u_dst[j * BPS + 4]);
123
236k
        Copy32b(&v_dst[j * BPS - 4], &v_dst[j * BPS + 4]);
124
236k
      }
125
26.2k
    }
126
54.6k
    {
127
      // bring top samples into the cache
128
54.6k
      VP8TopSamples* const top_yuv = dec->yuv_t + mb_x;
129
54.6k
      const int16_t* const coeffs = block->coeffs;
130
54.6k
      uint32_t bits = block->non_zero_y;
131
54.6k
      int n;
132
133
54.6k
      if (mb_y > 0) {
134
46.3k
        memcpy(y_dst - BPS, top_yuv[0].y, 16);
135
46.3k
        memcpy(u_dst - BPS, top_yuv[0].u, 8);
136
46.3k
        memcpy(v_dst - BPS, top_yuv[0].v, 8);
137
46.3k
      }
138
139
      // predict and add residuals
140
54.6k
      if (block->is_i4x4) {  // 4x4
141
23.5k
        uint32_t* const top_right = (uint32_t*)(y_dst - BPS + 16);
142
143
23.5k
        if (mb_y > 0) {
144
19.5k
          if (mb_x >= dec->mb_w - 1) {  // on rightmost border
145
8.88k
            memset(top_right, top_yuv[0].y[15], sizeof(*top_right));
146
10.6k
          } else {
147
10.6k
            memcpy(top_right, top_yuv[1].y, sizeof(*top_right));
148
10.6k
          }
149
19.5k
        }
150
        // replicate the top-right pixels below
151
23.5k
        top_right[BPS] = top_right[2 * BPS] = top_right[3 * BPS] = top_right[0];
152
153
        // predict and add residuals for all 4x4 blocks in turn.
154
399k
        for (n = 0; n < 16; ++n, bits <<= 2) {
155
376k
          uint8_t* const dst = y_dst + kScan[n];
156
376k
          VP8PredLuma4[block->imodes[n]](dst);
157
376k
          DoTransform(bits, coeffs + n * 16, dst);
158
376k
        }
159
31.1k
      } else {  // 16x16
160
31.1k
        const int pred_func = CheckMode(mb_x, mb_y, block->imodes[0]);
161
31.1k
        VP8PredLuma16[pred_func](y_dst);
162
31.1k
        if (bits != 0) {
163
121k
          for (n = 0; n < 16; ++n, bits <<= 2) {
164
114k
            DoTransform(bits, coeffs + n * 16, y_dst + kScan[n]);
165
114k
          }
166
7.14k
        }
167
31.1k
      }
168
54.6k
      {
169
        // Chroma
170
54.6k
        const uint32_t bits_uv = block->non_zero_uv;
171
54.6k
        const int pred_func = CheckMode(mb_x, mb_y, block->uvmode);
172
54.6k
        VP8PredChroma8[pred_func](u_dst);
173
54.6k
        VP8PredChroma8[pred_func](v_dst);
174
54.6k
        DoUVTransform(bits_uv >> 0, coeffs + 16 * 16, u_dst);
175
54.6k
        DoUVTransform(bits_uv >> 8, coeffs + 20 * 16, v_dst);
176
54.6k
      }
177
178
      // stash away top samples for next block
179
54.6k
      if (mb_y < dec->mb_h - 1) {
180
52.8k
        memcpy(top_yuv[0].y, y_dst + 15 * BPS, 16);
181
52.8k
        memcpy(top_yuv[0].u, u_dst + 7 * BPS, 8);
182
52.8k
        memcpy(top_yuv[0].v, v_dst + 7 * BPS, 8);
183
52.8k
      }
184
54.6k
    }
185
    // Transfer reconstructed samples from yuv_b cache to final destination.
186
54.6k
    {
187
54.6k
      const int y_offset = cache_id * 16 * dec->cache_y_stride;
188
54.6k
      const int uv_offset = cache_id * 8 * dec->cache_uv_stride;
189
54.6k
      uint8_t* const y_out = dec->cache_y + mb_x * 16 + y_offset;
190
54.6k
      uint8_t* const u_out = dec->cache_u + mb_x * 8 + uv_offset;
191
54.6k
      uint8_t* const v_out = dec->cache_v + mb_x * 8 + uv_offset;
192
929k
      for (j = 0; j < 16; ++j) {
193
874k
        memcpy(y_out + j * dec->cache_y_stride, y_dst + j * BPS, 16);
194
874k
      }
195
492k
      for (j = 0; j < 8; ++j) {
196
437k
        memcpy(u_out + j * dec->cache_uv_stride, u_dst + j * BPS, 8);
197
437k
        memcpy(v_out + j * dec->cache_uv_stride, v_dst + j * BPS, 8);
198
437k
      }
199
54.6k
    }
200
54.6k
  }
201
28.4k
}
202
203
//------------------------------------------------------------------------------
204
// Filtering
205
206
// kFilterExtraRows[] = How many extra lines are needed on the MB boundary
207
// for caching, given a filtering level.
208
// Simple filter:  up to 2 luma samples are read and 1 is written.
209
// Complex filter: up to 4 luma samples are read and 3 are written. Same for
210
//                 U/V, so it's 8 samples total (because of the 2x upsampling).
211
static const uint8_t kFilterExtraRows[3] = {0, 2, 8};
212
213
48.8k
static void DoFilter(const VP8Decoder* const dec, int mb_x, int mb_y) {
214
48.8k
  const VP8ThreadContext* const ctx = &dec->thread_ctx;
215
48.8k
  const int cache_id = ctx->id;
216
48.8k
  const int y_bps = dec->cache_y_stride;
217
48.8k
  const VP8FInfo* const f_info = ctx->f_info + mb_x;
218
48.8k
  uint8_t* const y_dst = dec->cache_y + cache_id * 16 * y_bps + mb_x * 16;
219
48.8k
  const int ilevel = f_info->f_ilevel;
220
48.8k
  const int limit = f_info->f_limit;
221
48.8k
  if (limit == 0) {
222
4.43k
    return;
223
4.43k
  }
224
48.8k
  assert(limit >= 3);
225
44.4k
  if (dec->filter_type == 1) {  // simple
226
32.2k
    if (mb_x > 0) {
227
12.0k
      VP8SimpleHFilter16(y_dst, y_bps, limit + 4);
228
12.0k
    }
229
32.2k
    if (f_info->f_inner) {
230
17.7k
      VP8SimpleHFilter16i(y_dst, y_bps, limit);
231
17.7k
    }
232
32.2k
    if (mb_y > 0) {
233
29.1k
      VP8SimpleVFilter16(y_dst, y_bps, limit + 4);
234
29.1k
    }
235
32.2k
    if (f_info->f_inner) {
236
17.7k
      VP8SimpleVFilter16i(y_dst, y_bps, limit);
237
17.7k
    }
238
32.2k
  } else {  // complex
239
12.1k
    const int uv_bps = dec->cache_uv_stride;
240
12.1k
    uint8_t* const u_dst = dec->cache_u + cache_id * 8 * uv_bps + mb_x * 8;
241
12.1k
    uint8_t* const v_dst = dec->cache_v + cache_id * 8 * uv_bps + mb_x * 8;
242
12.1k
    const int hev_thresh = f_info->hev_thresh;
243
12.1k
    if (mb_x > 0) {
244
7.20k
      VP8HFilter16(y_dst, y_bps, limit + 4, ilevel, hev_thresh);
245
7.20k
      VP8HFilter8(u_dst, v_dst, uv_bps, limit + 4, ilevel, hev_thresh);
246
7.20k
    }
247
12.1k
    if (f_info->f_inner) {
248
7.37k
      VP8HFilter16i(y_dst, y_bps, limit, ilevel, hev_thresh);
249
7.37k
      VP8HFilter8i(u_dst, v_dst, uv_bps, limit, ilevel, hev_thresh);
250
7.37k
    }
251
12.1k
    if (mb_y > 0) {
252
9.02k
      VP8VFilter16(y_dst, y_bps, limit + 4, ilevel, hev_thresh);
253
9.02k
      VP8VFilter8(u_dst, v_dst, uv_bps, limit + 4, ilevel, hev_thresh);
254
9.02k
    }
255
12.1k
    if (f_info->f_inner) {
256
7.37k
      VP8VFilter16i(y_dst, y_bps, limit, ilevel, hev_thresh);
257
7.37k
      VP8VFilter8i(u_dst, v_dst, uv_bps, limit, ilevel, hev_thresh);
258
7.37k
    }
259
12.1k
  }
260
44.4k
}
261
262
// Filter the decoded macroblock row (if needed)
263
25.9k
static void FilterRow(const VP8Decoder* const dec) {
264
25.9k
  int mb_x;
265
25.9k
  const int mb_y = dec->thread_ctx.mb_y;
266
25.9k
  assert(dec->thread_ctx.filter_row);
267
74.8k
  for (mb_x = dec->tl_mb_x; mb_x < dec->br_mb_x; ++mb_x) {
268
48.8k
    DoFilter(dec, mb_x, mb_y);
269
48.8k
  }
270
25.9k
}
271
272
//------------------------------------------------------------------------------
273
// Precompute the filtering strength for each segment and each i4x4/i16x16 mode.
274
275
1.90k
static void PrecomputeFilterStrengths(VP8Decoder* const dec) {
276
1.90k
  if (dec->filter_type > 0) {
277
1.37k
    int s;
278
1.37k
    const VP8FilterHeader* const hdr = &dec->filter_hdr;
279
6.88k
    for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
280
5.50k
      int i4x4;
281
      // First, compute the initial level
282
5.50k
      int base_level;
283
5.50k
      if (dec->segment_hdr.use_segment) {
284
1.66k
        base_level = dec->segment_hdr.filter_strength[s];
285
1.66k
        if (!dec->segment_hdr.absolute_delta) {
286
628
          base_level += hdr->level;
287
628
        }
288
3.83k
      } else {
289
3.83k
        base_level = hdr->level;
290
3.83k
      }
291
16.5k
      for (i4x4 = 0; i4x4 <= 1; ++i4x4) {
292
11.0k
        VP8FInfo* const info = &dec->fstrengths[s][i4x4];
293
11.0k
        int level = base_level;
294
11.0k
        if (hdr->use_lf_delta) {
295
3.25k
          level += hdr->ref_lf_delta[0];
296
3.25k
          if (i4x4) {
297
1.62k
            level += hdr->mode_lf_delta[0];
298
1.62k
          }
299
3.25k
        }
300
11.0k
        level = (level < 0) ? 0 : (level > 63) ? 63 : level;
301
11.0k
        if (level > 0) {
302
8.86k
          int ilevel = level;
303
8.86k
          if (hdr->sharpness > 0) {
304
3.97k
            if (hdr->sharpness > 4) {
305
1.80k
              ilevel >>= 2;
306
2.17k
            } else {
307
2.17k
              ilevel >>= 1;
308
2.17k
            }
309
3.97k
            if (ilevel > 9 - hdr->sharpness) {
310
2.57k
              ilevel = 9 - hdr->sharpness;
311
2.57k
            }
312
3.97k
          }
313
8.86k
          if (ilevel < 1) ilevel = 1;
314
8.86k
          info->f_ilevel = ilevel;
315
8.86k
          info->f_limit = 2 * level + ilevel;
316
8.86k
          info->hev_thresh = (level >= 40) ? 2 : (level >= 15) ? 1 : 0;
317
8.86k
        } else {
318
2.14k
          info->f_limit = 0;  // no filtering
319
2.14k
        }
320
11.0k
        info->f_inner = i4x4;
321
11.0k
      }
322
5.50k
    }
323
1.37k
  }
324
1.90k
}
325
326
//------------------------------------------------------------------------------
327
// Dithering
328
329
// minimal amp that will provide a non-zero dithering effect
330
0
#define MIN_DITHER_AMP 4
331
332
0
#define DITHER_AMP_TAB_SIZE 12
333
static const uint8_t kQuantToDitherAmp[DITHER_AMP_TAB_SIZE] = {
334
    // roughly, it's dqm->uv_mat[1]
335
    8, 7, 6, 4, 4, 2, 2, 2, 1, 1, 1, 1};
336
337
void VP8InitDithering(const WebPDecoderOptions* const options,
338
1.90k
                      VP8Decoder* const dec) {
339
1.90k
  assert(dec != NULL);
340
1.90k
  if (options != NULL) {
341
1.90k
    const int d = options->dithering_strength;
342
1.90k
    const int max_amp = (1 << VP8_RANDOM_DITHER_FIX) - 1;
343
1.90k
    const int f = (d < 0) ? 0 : (d > 100) ? max_amp : (d * max_amp / 100);
344
1.90k
    if (f > 0) {
345
0
      int s;
346
0
      int all_amp = 0;
347
0
      for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
348
0
        VP8QuantMatrix* const dqm = &dec->dqm[s];
349
0
        if (dqm->uv_quant < DITHER_AMP_TAB_SIZE) {
350
0
          const int idx = (dqm->uv_quant < 0) ? 0 : dqm->uv_quant;
351
0
          dqm->dither = (f * kQuantToDitherAmp[idx]) >> 3;
352
0
        }
353
0
        all_amp |= dqm->dither;
354
0
      }
355
0
      if (all_amp != 0) {
356
0
        VP8InitRandom(&dec->dithering_rg, 1.0f);
357
0
        dec->dither = 1;
358
0
      }
359
0
    }
360
    // potentially allow alpha dithering
361
1.90k
    dec->alpha_dithering = options->alpha_dithering_strength;
362
1.90k
    if (dec->alpha_dithering > 100) {
363
0
      dec->alpha_dithering = 100;
364
1.90k
    } else if (dec->alpha_dithering < 0) {
365
0
      dec->alpha_dithering = 0;
366
0
    }
367
1.90k
  }
368
1.90k
}
369
370
// Convert to range: [-2,2] for dither=50, [-4,4] for dither=100
371
0
static void Dither8x8(VP8Random* const rg, uint8_t* dst, int bps, int amp) {
372
0
  uint8_t dither[64];
373
0
  int i;
374
0
  for (i = 0; i < 8 * 8; ++i) {
375
0
    dither[i] = VP8RandomBits2(rg, VP8_DITHER_AMP_BITS + 1, amp);
376
0
  }
377
0
  VP8DitherCombine8x8(dither, dst, bps);
378
0
}
379
380
0
static void DitherRow(VP8Decoder* const dec) {
381
0
  int mb_x;
382
0
  assert(dec->dither);
383
0
  for (mb_x = dec->tl_mb_x; mb_x < dec->br_mb_x; ++mb_x) {
384
0
    const VP8ThreadContext* const ctx = &dec->thread_ctx;
385
0
    const VP8MBData* const data = ctx->mb_data + mb_x;
386
0
    const int cache_id = ctx->id;
387
0
    const int uv_bps = dec->cache_uv_stride;
388
0
    if (data->dither >= MIN_DITHER_AMP) {
389
0
      uint8_t* const u_dst = dec->cache_u + cache_id * 8 * uv_bps + mb_x * 8;
390
0
      uint8_t* const v_dst = dec->cache_v + cache_id * 8 * uv_bps + mb_x * 8;
391
0
      Dither8x8(&dec->dithering_rg, u_dst, uv_bps, data->dither);
392
0
      Dither8x8(&dec->dithering_rg, v_dst, uv_bps, data->dither);
393
0
    }
394
0
  }
395
0
}
396
397
//------------------------------------------------------------------------------
398
// This function is called after a row of macroblocks is finished decoding.
399
// It also takes into account the following restrictions:
400
//  * In case of in-loop filtering, we must hold off sending some of the bottom
401
//    pixels as they are yet unfiltered. They will be when the next macroblock
402
//    row is decoded. Meanwhile, we must preserve them by rotating them in the
403
//    cache area. This doesn't hold for the very bottom row of the uncropped
404
//    picture of course.
405
//  * we must clip the remaining pixels against the cropping area. The VP8Io
406
//    struct must have the following fields set correctly before calling put():
407
408
56.8k
#define MACROBLOCK_VPOS(mb_y) ((mb_y) * 16)  // vertical position of a MB
409
410
// Finalize and transmit a complete row. Return false in case of user-abort.
411
28.4k
static int FinishRow(void* arg1, void* arg2) {
412
28.4k
  VP8Decoder* const dec = (VP8Decoder*)arg1;
413
28.4k
  VP8Io* const io = (VP8Io*)arg2;
414
28.4k
  int ok = 1;
415
28.4k
  const VP8ThreadContext* const ctx = &dec->thread_ctx;
416
28.4k
  const int cache_id = ctx->id;
417
28.4k
  const int extra_y_rows = kFilterExtraRows[dec->filter_type];
418
28.4k
  const int ysize = extra_y_rows * dec->cache_y_stride;
419
28.4k
  const int uvsize = (extra_y_rows / 2) * dec->cache_uv_stride;
420
28.4k
  const int y_offset = cache_id * 16 * dec->cache_y_stride;
421
28.4k
  const int uv_offset = cache_id * 8 * dec->cache_uv_stride;
422
28.4k
  uint8_t* const ydst = dec->cache_y - ysize + y_offset;
423
28.4k
  uint8_t* const udst = dec->cache_u - uvsize + uv_offset;
424
28.4k
  uint8_t* const vdst = dec->cache_v - uvsize + uv_offset;
425
28.4k
  const int mb_y = ctx->mb_y;
426
28.4k
  const int is_first_row = (mb_y == 0);
427
28.4k
  const int is_last_row = (mb_y >= dec->br_mb_y - 1);
428
429
28.4k
  if (dec->mt_method == 2) {
430
0
    ReconstructRow(dec, ctx);
431
0
  }
432
433
28.4k
  if (ctx->filter_row) {
434
25.9k
    FilterRow(dec);
435
25.9k
  }
436
437
28.4k
  if (dec->dither) {
438
0
    DitherRow(dec);
439
0
  }
440
441
28.4k
  if (io->put != NULL) {
442
28.4k
    int y_start = MACROBLOCK_VPOS(mb_y);
443
28.4k
    int y_end = MACROBLOCK_VPOS(mb_y + 1);
444
28.4k
    if (!is_first_row) {
445
26.9k
      y_start -= extra_y_rows;
446
26.9k
      io->y = ydst;
447
26.9k
      io->u = udst;
448
26.9k
      io->v = vdst;
449
26.9k
    } else {
450
1.50k
      io->y = dec->cache_y + y_offset;
451
1.50k
      io->u = dec->cache_u + uv_offset;
452
1.50k
      io->v = dec->cache_v + uv_offset;
453
1.50k
    }
454
455
28.4k
    if (!is_last_row) {
456
27.8k
      y_end -= extra_y_rows;
457
27.8k
    }
458
28.4k
    if (y_end > io->crop_bottom) {
459
519
      y_end = io->crop_bottom;  // make sure we don't overflow on last row.
460
519
    }
461
    // If dec->alpha_data is not NULL, we have some alpha plane present.
462
28.4k
    io->a = NULL;
463
28.4k
    if (dec->alpha_data != NULL && y_start < y_end) {
464
2.10k
      io->a = VP8DecompressAlphaRows(dec, io, y_start, y_end - y_start);
465
2.10k
      if (io->a == NULL) {
466
163
        return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
467
163
                           "Could not decode alpha data.");
468
163
      }
469
2.10k
    }
470
28.2k
    if (y_start < io->crop_top) {
471
0
      const int delta_y = io->crop_top - y_start;
472
0
      y_start = io->crop_top;
473
0
      assert(!(delta_y & 1));
474
0
      io->y += dec->cache_y_stride * delta_y;
475
0
      io->u += dec->cache_uv_stride * (delta_y >> 1);
476
0
      io->v += dec->cache_uv_stride * (delta_y >> 1);
477
0
      if (io->a != NULL) {
478
0
        io->a += io->width * delta_y;
479
0
      }
480
0
    }
481
28.2k
    if (y_start < y_end) {
482
28.2k
      io->y += io->crop_left;
483
28.2k
      io->u += io->crop_left >> 1;
484
28.2k
      io->v += io->crop_left >> 1;
485
28.2k
      if (io->a != NULL) {
486
1.94k
        io->a += io->crop_left;
487
1.94k
      }
488
28.2k
      io->mb_y = y_start - io->crop_top;
489
28.2k
      io->mb_w = io->crop_right - io->crop_left;
490
28.2k
      io->mb_h = y_end - y_start;
491
28.2k
      ok = io->put(io);
492
28.2k
    }
493
28.2k
  }
494
  // rotate top samples if needed
495
28.2k
  if (cache_id + 1 == dec->num_caches) {
496
28.2k
    if (!is_last_row) {
497
27.6k
      memcpy(dec->cache_y - ysize, ydst + 16 * dec->cache_y_stride, ysize);
498
27.6k
      memcpy(dec->cache_u - uvsize, udst + 8 * dec->cache_uv_stride, uvsize);
499
27.6k
      memcpy(dec->cache_v - uvsize, vdst + 8 * dec->cache_uv_stride, uvsize);
500
27.6k
    }
501
28.2k
  }
502
503
28.2k
  return ok;
504
28.4k
}
505
506
#undef MACROBLOCK_VPOS
507
508
//------------------------------------------------------------------------------
509
510
28.4k
int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io) {
511
28.4k
  int ok = 1;
512
28.4k
  VP8ThreadContext* const ctx = &dec->thread_ctx;
513
28.4k
  const int filter_row = (dec->filter_type > 0) &&
514
25.9k
                         (dec->mb_y >= dec->tl_mb_y) &&
515
25.9k
                         (dec->mb_y <= dec->br_mb_y);
516
28.4k
  if (dec->mt_method == 0) {
517
    // ctx->id and ctx->f_info are already set
518
28.4k
    ctx->mb_y = dec->mb_y;
519
28.4k
    ctx->filter_row = filter_row;
520
28.4k
    ReconstructRow(dec, ctx);
521
28.4k
    ok = FinishRow(dec, io);
522
28.4k
  } else {
523
0
    WebPWorker* const worker = &dec->worker;
524
    // Finish previous job *before* updating context
525
0
    ok &= WebPGetWorkerInterface()->Sync(worker);
526
0
    assert(worker->status == OK);
527
0
    if (ok) {  // spawn a new deblocking/output job
528
0
      ctx->io = *io;
529
0
      ctx->id = dec->cache_id;
530
0
      ctx->mb_y = dec->mb_y;
531
0
      ctx->filter_row = filter_row;
532
0
      if (dec->mt_method == 2) {  // swap macroblock data
533
0
        VP8MBData* const tmp = ctx->mb_data;
534
0
        ctx->mb_data = dec->mb_data;
535
0
        dec->mb_data = tmp;
536
0
      } else {
537
        // perform reconstruction directly in main thread
538
0
        ReconstructRow(dec, ctx);
539
0
      }
540
0
      if (filter_row) {  // swap filter info
541
0
        VP8FInfo* const tmp = ctx->f_info;
542
0
        ctx->f_info = dec->f_info;
543
0
        dec->f_info = tmp;
544
0
      }
545
      // (reconstruct)+filter in parallel
546
0
      WebPGetWorkerInterface()->Launch(worker);
547
0
      if (++dec->cache_id == dec->num_caches) {
548
0
        dec->cache_id = 0;
549
0
      }
550
0
    }
551
0
  }
552
28.4k
  return ok;
553
28.4k
}
554
555
//------------------------------------------------------------------------------
556
// Finish setting up the decoding parameter once user's setup() is called.
557
558
1.90k
VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io) {
559
  // Call setup() first. This may trigger additional decoding features on 'io'.
560
  // Note: Afterward, we must call teardown() no matter what.
561
1.90k
  if (io->setup != NULL && !io->setup(io)) {
562
0
    VP8SetError(dec, VP8_STATUS_INVALID_PARAM, "Frame setup failed");
563
0
    return dec->status;
564
0
  }
565
566
  // Disable filtering per user request
567
1.90k
  if (io->bypass_filtering) {
568
0
    dec->filter_type = 0;
569
0
  }
570
571
  // Define the area where we can skip in-loop filtering, in case of cropping.
572
  //
573
  // 'Simple' filter reads two luma samples outside of the macroblock
574
  // and filters one. It doesn't filter the chroma samples. Hence, we can
575
  // avoid doing the in-loop filtering before crop_top/crop_left position.
576
  // For the 'Complex' filter, 3 samples are read and up to 3 are filtered.
577
  // Means: there's a dependency chain that goes all the way up to the
578
  // top-left corner of the picture (MB #0). We must filter all the previous
579
  // macroblocks.
580
1.90k
  {
581
1.90k
    const int extra_pixels = kFilterExtraRows[dec->filter_type];
582
1.90k
    if (dec->filter_type == 2) {
583
      // For complex filter, we need to preserve the dependency chain.
584
820
      dec->tl_mb_x = 0;
585
820
      dec->tl_mb_y = 0;
586
1.08k
    } else {
587
      // For simple filter, we can filter only the cropped region.
588
      // We include 'extra_pixels' on the other side of the boundary, since
589
      // vertical or horizontal filtering of the previous macroblock can
590
      // modify some abutting pixels.
591
1.08k
      dec->tl_mb_x = (io->crop_left - extra_pixels) >> 4;
592
1.08k
      dec->tl_mb_y = (io->crop_top - extra_pixels) >> 4;
593
1.08k
      if (dec->tl_mb_x < 0) dec->tl_mb_x = 0;
594
1.08k
      if (dec->tl_mb_y < 0) dec->tl_mb_y = 0;
595
1.08k
    }
596
    // We need some 'extra' pixels on the right/bottom.
597
1.90k
    dec->br_mb_y = (io->crop_bottom + 15 + extra_pixels) >> 4;
598
1.90k
    dec->br_mb_x = (io->crop_right + 15 + extra_pixels) >> 4;
599
1.90k
    if (dec->br_mb_x > dec->mb_w) {
600
402
      dec->br_mb_x = dec->mb_w;
601
402
    }
602
1.90k
    if (dec->br_mb_y > dec->mb_h) {
603
566
      dec->br_mb_y = dec->mb_h;
604
566
    }
605
1.90k
  }
606
1.90k
  PrecomputeFilterStrengths(dec);
607
1.90k
  return VP8_STATUS_OK;
608
1.90k
}
609
610
1.90k
int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io) {
611
1.90k
  int ok = 1;
612
1.90k
  if (dec->mt_method > 0) {
613
0
    ok = WebPGetWorkerInterface()->Sync(&dec->worker);
614
0
  }
615
616
1.90k
  if (io->teardown != NULL) {
617
1.90k
    io->teardown(io);
618
1.90k
  }
619
1.90k
  return ok;
620
1.90k
}
621
622
//------------------------------------------------------------------------------
623
// For multi-threaded decoding we need to use 3 rows of 16 pixels as delay line.
624
//
625
// Reason is: the deblocking filter cannot deblock the bottom horizontal edges
626
// immediately, and needs to wait for first few rows of the next macroblock to
627
// be decoded. Hence, deblocking is lagging behind by 4 or 8 pixels (depending
628
// on strength).
629
// With two threads, the vertical positions of the rows being decoded are:
630
// Decode:  [ 0..15][16..31][32..47][48..63][64..79][...
631
// Deblock:         [ 0..11][12..27][28..43][44..59][...
632
// If we use two threads and two caches of 16 pixels, the sequence would be:
633
// Decode:  [ 0..15][16..31][ 0..15!!][16..31][ 0..15][...
634
// Deblock:         [ 0..11][12..27!!][-4..11][12..27][...
635
// The problem occurs during row [12..15!!] that both the decoding and
636
// deblocking threads are writing simultaneously.
637
// With 3 cache lines, one get a safe write pattern:
638
// Decode:  [ 0..15][16..31][32..47][ 0..15][16..31][32..47][0..
639
// Deblock:         [ 0..11][12..27][28..43][-4..11][12..27][28...
640
// Note that multi-threaded output _without_ deblocking can make use of two
641
// cache lines of 16 pixels only, since there's no lagging behind. The decoding
642
// and output process have non-concurrent writing:
643
// Decode:  [ 0..15][16..31][ 0..15][16..31][...
644
// io->put:         [ 0..15][16..31][ 0..15][...
645
646
0
#define MT_CACHE_LINES 3
647
1.90k
#define ST_CACHE_LINES 1  // 1 cache row only for single-threaded case
648
649
// Initialize multi/single-thread worker
650
1.90k
static int InitThreadContext(VP8Decoder* const dec) {
651
1.90k
  dec->cache_id = 0;
652
1.90k
  if (dec->mt_method > 0) {
653
0
    WebPWorker* const worker = &dec->worker;
654
0
    if (!WebPGetWorkerInterface()->Reset(worker)) {
655
0
      return VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
656
0
                         "thread initialization failed.");
657
0
    }
658
0
    worker->data1 = dec;
659
0
    worker->data2 = (void*)&dec->thread_ctx.io;
660
0
    worker->hook = FinishRow;
661
0
    dec->num_caches =
662
0
        (dec->filter_type > 0) ? MT_CACHE_LINES : MT_CACHE_LINES - 1;
663
1.90k
  } else {
664
1.90k
    dec->num_caches = ST_CACHE_LINES;
665
1.90k
  }
666
1.90k
  return 1;
667
1.90k
}
668
669
int VP8GetThreadMethod(const WebPDecoderOptions* const options,
670
                       const WebPHeaderStructure* const headers, int width,
671
1.90k
                       int height) {
672
1.90k
  if (options == NULL || options->use_threads == 0) {
673
1.90k
    return 0;
674
1.90k
  }
675
0
  (void)headers;
676
0
  (void)width;
677
0
  (void)height;
678
0
  assert(headers == NULL || !headers->is_lossless);
679
0
#if defined(WEBP_USE_THREAD)
680
0
  if (width >= MIN_WIDTH_FOR_THREADS) return 2;
681
0
#endif
682
0
  return 0;
683
0
}
684
685
#undef MT_CACHE_LINES
686
#undef ST_CACHE_LINES
687
688
//------------------------------------------------------------------------------
689
// Memory setup
690
691
1.90k
static int AllocateMemory(VP8Decoder* const dec) {
692
1.90k
  const int num_caches = dec->num_caches;
693
1.90k
  const int mb_w = dec->mb_w;
694
  // Note: we use 'size_t' when there's no overflow risk, uint64_t otherwise.
695
1.90k
  const size_t intra_pred_mode_size = 4 * mb_w * sizeof(uint8_t);
696
1.90k
  const size_t top_size = sizeof(VP8TopSamples) * mb_w;
697
1.90k
  const size_t mb_info_size = (mb_w + 1) * sizeof(VP8MB);
698
1.90k
  const size_t f_info_size =
699
1.90k
      (dec->filter_type > 0)
700
1.90k
          ? mb_w * (dec->mt_method > 0 ? 2 : 1) * sizeof(VP8FInfo)
701
1.90k
          : 0;
702
1.90k
  const size_t yuv_size = YUV_SIZE * sizeof(*dec->yuv_b);
703
1.90k
  const size_t mb_data_size =
704
1.90k
      (dec->mt_method == 2 ? 2 : 1) * mb_w * sizeof(*dec->mb_data);
705
1.90k
  const size_t cache_height =
706
1.90k
      (16 * num_caches + kFilterExtraRows[dec->filter_type]) * 3 / 2;
707
1.90k
  const size_t cache_size = top_size * cache_height;
708
  // alpha_size is the only one that scales as width x height.
709
1.90k
  const uint64_t alpha_size =
710
1.90k
      (dec->alpha_data != NULL)
711
1.90k
          ? (uint64_t)dec->pic_hdr.width * dec->pic_hdr.height
712
1.90k
          : 0ULL;
713
1.90k
  const uint64_t needed = (uint64_t)intra_pred_mode_size + top_size +
714
1.90k
                          mb_info_size + f_info_size + yuv_size + mb_data_size +
715
1.90k
                          cache_size + alpha_size + WEBP_ALIGN_CST;
716
1.90k
  uint8_t* mem;
717
718
1.90k
  if (!CheckSizeOverflow(needed)) return 0;  // check for overflow
719
1.90k
  if (needed > dec->mem_size) {
720
1.90k
    WebPSafeFree(dec->mem);
721
1.90k
    dec->mem_size = 0;
722
1.90k
    dec->mem = WebPSafeMalloc(needed, sizeof(uint8_t));
723
1.90k
    if (dec->mem == NULL) {
724
0
      return VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
725
0
                         "no memory during frame initialization.");
726
0
    }
727
    // down-cast is ok, thanks to WebPSafeMalloc() above.
728
1.90k
    dec->mem_size = (size_t)needed;
729
1.90k
  }
730
731
1.90k
  mem = (uint8_t*)dec->mem;
732
1.90k
  dec->intra_t = mem;
733
1.90k
  mem += intra_pred_mode_size;
734
735
1.90k
  dec->yuv_t = (VP8TopSamples*)mem;
736
1.90k
  mem += top_size;
737
738
1.90k
  dec->mb_info = ((VP8MB*)mem) + 1;
739
1.90k
  mem += mb_info_size;
740
741
1.90k
  dec->f_info = f_info_size ? (VP8FInfo*)mem : NULL;
742
1.90k
  mem += f_info_size;
743
1.90k
  dec->thread_ctx.id = 0;
744
1.90k
  dec->thread_ctx.f_info = dec->f_info;
745
1.90k
  if (dec->filter_type > 0 && dec->mt_method > 0) {
746
    // secondary cache line. The deblocking process need to make use of the
747
    // filtering strength from previous macroblock row, while the new ones
748
    // are being decoded in parallel. We'll just swap the pointers.
749
0
    dec->thread_ctx.f_info += mb_w;
750
0
  }
751
752
1.90k
  mem = (uint8_t*)WEBP_ALIGN(mem);
753
1.90k
  assert((yuv_size & WEBP_ALIGN_CST) == 0);
754
1.90k
  dec->yuv_b = mem;
755
1.90k
  mem += yuv_size;
756
757
1.90k
  dec->mb_data = (VP8MBData*)mem;
758
1.90k
  dec->thread_ctx.mb_data = (VP8MBData*)mem;
759
1.90k
  if (dec->mt_method == 2) {
760
0
    dec->thread_ctx.mb_data += mb_w;
761
0
  }
762
1.90k
  mem += mb_data_size;
763
764
1.90k
  dec->cache_y_stride = 16 * mb_w;
765
1.90k
  dec->cache_uv_stride = 8 * mb_w;
766
1.90k
  {
767
1.90k
    const int extra_rows = kFilterExtraRows[dec->filter_type];
768
1.90k
    const int extra_y = extra_rows * dec->cache_y_stride;
769
1.90k
    const int extra_uv = (extra_rows / 2) * dec->cache_uv_stride;
770
1.90k
    dec->cache_y = mem + extra_y;
771
1.90k
    dec->cache_u =
772
1.90k
        dec->cache_y + 16 * num_caches * dec->cache_y_stride + extra_uv;
773
1.90k
    dec->cache_v =
774
1.90k
        dec->cache_u + 8 * num_caches * dec->cache_uv_stride + extra_uv;
775
1.90k
    dec->cache_id = 0;
776
1.90k
  }
777
1.90k
  mem += cache_size;
778
779
  // alpha plane
780
1.90k
  dec->alpha_plane = alpha_size ? mem : NULL;
781
1.90k
  mem += alpha_size;
782
1.90k
  assert(mem <= (uint8_t*)dec->mem + dec->mem_size);
783
784
  // note: left/top-info is initialized once for all.
785
1.90k
  memset(dec->mb_info - 1, 0, mb_info_size);
786
1.90k
  VP8InitScanline(dec);  // initialize left too.
787
788
  // initialize top
789
1.90k
  memset(dec->intra_t, B_DC_PRED, intra_pred_mode_size);
790
791
1.90k
  return 1;
792
1.90k
}
793
794
1.90k
static void InitIo(VP8Decoder* const dec, VP8Io* io) {
795
  // prepare 'io'
796
1.90k
  io->mb_y = 0;
797
1.90k
  io->y = dec->cache_y;
798
1.90k
  io->u = dec->cache_u;
799
1.90k
  io->v = dec->cache_v;
800
1.90k
  io->y_stride = dec->cache_y_stride;
801
1.90k
  io->uv_stride = dec->cache_uv_stride;
802
1.90k
  io->a = NULL;
803
1.90k
}
804
805
1.90k
int VP8InitFrame(VP8Decoder* const dec, VP8Io* const io) {
806
1.90k
  if (!InitThreadContext(dec)) return 0;  // call first. Sets dec->num_caches.
807
1.90k
  if (!AllocateMemory(dec)) return 0;
808
1.90k
  InitIo(dec, io);
809
1.90k
  VP8DspInit();  // Init critical function pointers and look-up tables.
810
1.90k
  return 1;
811
1.90k
}
812
813
//------------------------------------------------------------------------------