Coverage Report

Created: 2026-06-07 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/dec/vp8_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
// main entry for the decoder
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include "src/dec/vp8_dec.h"
15
16
#include <assert.h>
17
#include <stdlib.h>
18
#include <string.h>
19
20
#include "src/dec/alphai_dec.h"
21
#include "src/dec/common_dec.h"
22
#include "src/dec/vp8i_dec.h"
23
#include "src/dec/vp8li_dec.h"
24
#include "src/dec/webpi_dec.h"
25
#include "src/dsp/cpu.h"
26
#include "src/dsp/dsp.h"
27
#include "src/utils/bit_reader_inl_utils.h"
28
#include "src/utils/bit_reader_utils.h"
29
#include "src/utils/thread_utils.h"
30
#include "src/utils/utils.h"
31
#include "src/webp/decode.h"
32
#include "src/webp/format_constants.h"
33
#include "src/webp/types.h"
34
35
WEBP_ASSUME_UNSAFE_INDEXABLE_ABI
36
37
//------------------------------------------------------------------------------
38
39
0
int WebPGetDecoderVersion(void) {
40
0
  return (DEC_MAJ_VERSION << 16) | (DEC_MIN_VERSION << 8) | DEC_REV_VERSION;
41
0
}
42
43
//------------------------------------------------------------------------------
44
// Signature and pointer-to-function for GetCoeffs() variants below.
45
46
typedef int (*GetCoeffsFunc)(VP8BitReader* const br,
47
                             const VP8BandProbas* const prob[], int ctx,
48
                             const quant_t dq, int n, int16_t* out);
49
static volatile GetCoeffsFunc GetCoeffs = NULL;
50
51
static void InitGetCoeffs(void);
52
53
//------------------------------------------------------------------------------
54
// VP8Decoder
55
56
1.78k
static void SetOk(VP8Decoder* const dec) {
57
1.78k
  dec->status = VP8_STATUS_OK;
58
1.78k
  dec->error_msg = "OK";
59
1.78k
}
60
61
4.00k
int VP8InitIoInternal(VP8Io* const io, int version) {
62
4.00k
  if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
63
0
    return 0;  // mismatch error
64
0
  }
65
4.00k
  if (io != NULL) {
66
4.00k
    WEBP_UNSAFE_MEMSET(io, 0, sizeof(*io));
67
4.00k
  }
68
4.00k
  return 1;
69
4.00k
}
70
71
891
VP8Decoder* VP8New(void) {
72
891
  VP8Decoder* const dec = (VP8Decoder*)WebPSafeCalloc(1ULL, sizeof(*dec));
73
891
  if (dec != NULL) {
74
891
    SetOk(dec);
75
891
    WebPGetWorkerInterface()->Init(&dec->worker);
76
891
    dec->ready = 0;
77
891
    dec->num_parts_minus_one = 0;
78
891
    InitGetCoeffs();
79
891
  }
80
891
  return dec;
81
891
}
82
83
0
VP8StatusCode VP8Status(VP8Decoder* const dec) {
84
0
  if (!dec) return VP8_STATUS_INVALID_PARAM;
85
0
  return dec->status;
86
0
}
87
88
0
const char* VP8StatusMessage(VP8Decoder* const dec) {
89
0
  if (dec == NULL) return "no object";
90
0
  if (!dec->error_msg) return "OK";
91
0
  return dec->error_msg;
92
0
}
93
94
891
void VP8Delete(VP8Decoder* const dec) {
95
891
  if (dec != NULL) {
96
891
    VP8Clear(dec);
97
891
    WebPSafeFree(dec);
98
891
  }
99
891
}
100
101
int VP8SetError(VP8Decoder* const dec, VP8StatusCode error,
102
627
                const char* const msg) {
103
  // VP8_STATUS_SUSPENDED is only meaningful in incremental decoding.
104
627
  assert(dec->incremental || error != VP8_STATUS_SUSPENDED);
105
  // The oldest error reported takes precedence over the new one.
106
627
  if (dec->status == VP8_STATUS_OK) {
107
532
    dec->status = error;
108
532
    dec->error_msg = msg;
109
532
    dec->ready = 0;
110
532
  }
111
627
  return 0;
112
627
}
113
114
//------------------------------------------------------------------------------
115
116
int VP8CheckSignature(const uint8_t* const WEBP_COUNTED_BY(data_size) data,
117
7.46k
                      size_t data_size) {
118
7.46k
  return (data_size >= 3 && data[0] == 0x9d && data[1] == 0x01 &&
119
7.42k
          data[2] == 0x2a);
120
7.46k
}
121
122
int VP8GetInfo(const uint8_t* WEBP_COUNTED_BY(data_size) data, size_t data_size,
123
6.57k
               size_t chunk_size, int* const width, int* const height) {
124
6.57k
  if (data == NULL || data_size < VP8_FRAME_HEADER_SIZE) {
125
6
    return 0;  // not enough data
126
6
  }
127
  // check signature
128
6.57k
  if (!VP8CheckSignature(data + 3, data_size - 3)) {
129
42
    return 0;  // Wrong signature.
130
6.53k
  } else {
131
6.53k
    const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16);
132
6.53k
    const int key_frame = !(bits & 1);
133
6.53k
    const int w = ((data[7] << 8) | data[6]) & 0x3fff;
134
6.53k
    const int h = ((data[9] << 8) | data[8]) & 0x3fff;
135
136
6.53k
    if (!key_frame) {  // Not a keyframe.
137
4
      return 0;
138
4
    }
139
140
6.52k
    if (((bits >> 1) & 7) > 3) {
141
6
      return 0;  // unknown profile
142
6
    }
143
6.52k
    if (!((bits >> 4) & 1)) {
144
3
      return 0;  // first frame is invisible!
145
3
    }
146
6.51k
    if (((bits >> 5)) >= chunk_size) {  // partition_length
147
7
      return 0;                         // inconsistent size information.
148
7
    }
149
6.51k
    if (w == 0 || h == 0) {
150
10
      return 0;  // We don't support both width and height to be zero.
151
10
    }
152
153
6.50k
    if (width) {
154
6.50k
      *width = w;
155
6.50k
    }
156
6.50k
    if (height) {
157
6.50k
      *height = h;
158
6.50k
    }
159
160
6.50k
    return 1;
161
6.51k
  }
162
6.57k
}
163
164
//------------------------------------------------------------------------------
165
// Header parsing
166
167
891
static void ResetSegmentHeader(VP8SegmentHeader* const hdr) {
168
891
  assert(hdr != NULL);
169
891
  hdr->use_segment = 0;
170
891
  hdr->update_map = 0;
171
891
  hdr->absolute_delta = 1;
172
891
  WEBP_UNSAFE_MEMSET(hdr->quantizer, 0, sizeof(hdr->quantizer));
173
891
  WEBP_UNSAFE_MEMSET(hdr->filter_strength, 0, sizeof(hdr->filter_strength));
174
891
}
175
176
// Paragraph 9.3
177
static int ParseSegmentHeader(VP8BitReader* br, VP8SegmentHeader* hdr,
178
891
                              VP8Proba* proba) {
179
891
  assert(br != NULL);
180
891
  assert(hdr != NULL);
181
891
  hdr->use_segment = VP8Get(br, "global-header");
182
891
  if (hdr->use_segment) {
183
150
    hdr->update_map = VP8Get(br, "global-header");
184
150
    if (VP8Get(br, "global-header")) {  // update data
185
84
      int s;
186
84
      hdr->absolute_delta = VP8Get(br, "global-header");
187
420
      for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
188
336
        hdr->quantizer[s] = VP8Get(br, "global-header")
189
336
                                ? VP8GetSignedValue(br, 7, "global-header")
190
336
                                : 0;
191
336
      }
192
420
      for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
193
336
        hdr->filter_strength[s] =
194
336
            VP8Get(br, "global-header")
195
336
                ? VP8GetSignedValue(br, 6, "global-header")
196
336
                : 0;
197
336
      }
198
84
    }
199
150
    if (hdr->update_map) {
200
95
      int s;
201
380
      for (s = 0; s < MB_FEATURE_TREE_PROBS; ++s) {
202
285
        proba->segments[s] = VP8Get(br, "global-header")
203
285
                                 ? VP8GetValue(br, 8, "global-header")
204
285
                                 : 255u;
205
285
      }
206
95
    }
207
741
  } else {
208
741
    hdr->update_map = 0;
209
741
  }
210
891
  return !br->eof;
211
891
}
212
213
// Paragraph 9.5
214
// If we don't have all the necessary data in 'buf', this function returns
215
// VP8_STATUS_SUSPENDED in incremental decoding, VP8_STATUS_NOT_ENOUGH_DATA
216
// otherwise.
217
// In incremental decoding, this case is not necessarily an error. Still, no
218
// bitreader is ever initialized to make it possible to read unavailable memory.
219
// If we don't even have the partitions' sizes, then VP8_STATUS_NOT_ENOUGH_DATA
220
// is returned, and this is an unrecoverable error.
221
// If the partitions were positioned ok, VP8_STATUS_OK is returned.
222
static VP8StatusCode ParsePartitions(VP8Decoder* const dec,
223
                                     const uint8_t* WEBP_COUNTED_BY(size) buf,
224
885
                                     size_t size) {
225
885
  VP8BitReader* const br = &dec->br;
226
885
  const uint8_t* WEBP_BIDI_INDEXABLE sz = buf;
227
885
  const uint8_t* buf_end = buf + size;
228
885
  const uint8_t* WEBP_BIDI_INDEXABLE part_start;
229
885
  size_t size_left = size;
230
885
  size_t last_part;
231
885
  size_t p;
232
233
885
  dec->num_parts_minus_one = (1 << VP8GetValue(br, 2, "global-header")) - 1;
234
885
  last_part = dec->num_parts_minus_one;
235
885
  if (size < 3 * last_part) {
236
    // we can't even read the sizes with sz[]! That's a failure.
237
1
    return VP8_STATUS_NOT_ENOUGH_DATA;
238
1
  }
239
884
  part_start = buf + last_part * 3;
240
884
  size_left -= last_part * 3;
241
911
  for (p = 0; p < last_part; ++p) {
242
27
    size_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16);
243
27
    if (psize > size_left) psize = size_left;
244
27
    VP8InitBitReader(dec->parts + p, part_start, psize);
245
27
    part_start += psize;
246
27
    size_left -= psize;
247
27
    sz += 3;
248
27
  }
249
884
  VP8InitBitReader(dec->parts + last_part, part_start, size_left);
250
884
  if (part_start < buf_end) return VP8_STATUS_OK;
251
8
  return dec->incremental
252
8
             ? VP8_STATUS_SUSPENDED  // Init is ok, but there's not enough data
253
8
             : VP8_STATUS_NOT_ENOUGH_DATA;
254
884
}
255
256
// Paragraph 9.4
257
887
static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) {
258
887
  VP8FilterHeader* const hdr = &dec->filter_hdr;
259
887
  hdr->simple = VP8Get(br, "global-header");
260
887
  hdr->level = VP8GetValue(br, 6, "global-header");
261
887
  hdr->sharpness = VP8GetValue(br, 3, "global-header");
262
887
  hdr->use_lf_delta = VP8Get(br, "global-header");
263
887
  if (hdr->use_lf_delta) {
264
206
    if (VP8Get(br, "global-header")) {  // update lf-delta?
265
133
      int i;
266
665
      for (i = 0; i < NUM_REF_LF_DELTAS; ++i) {
267
532
        if (VP8Get(br, "global-header")) {
268
173
          hdr->ref_lf_delta[i] = VP8GetSignedValue(br, 6, "global-header");
269
173
        }
270
532
      }
271
665
      for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) {
272
532
        if (VP8Get(br, "global-header")) {
273
117
          hdr->mode_lf_delta[i] = VP8GetSignedValue(br, 6, "global-header");
274
117
        }
275
532
      }
276
133
    }
277
206
  }
278
887
  dec->filter_type = (hdr->level == 0) ? 0 : hdr->simple ? 1 : 2;
279
887
  return !br->eof;
280
887
}
281
282
// Topmost call
283
891
int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
284
891
  size_t buf_size;
285
891
  const uint8_t* WEBP_COUNTED_BY(buf_size) buf;
286
891
  VP8FrameHeader* frm_hdr;
287
891
  VP8PictureHeader* pic_hdr;
288
891
  VP8BitReader* br;
289
891
  VP8StatusCode status;
290
291
891
  if (dec == NULL) {
292
0
    return 0;
293
0
  }
294
891
  SetOk(dec);
295
891
  if (io == NULL) {
296
0
    return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
297
0
                       "null VP8Io passed to VP8GetHeaders()");
298
0
  }
299
891
  buf_size = io->data_size;
300
891
  buf =
301
891
      WEBP_UNSAFE_FORGE_BIDI_INDEXABLE(const uint8_t*, io->data, io->data_size);
302
891
  if (buf_size < 4) {
303
0
    return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, "Truncated header.");
304
0
  }
305
306
  // Paragraph 9.1
307
891
  {
308
891
    const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16);
309
891
    frm_hdr = &dec->frm_hdr;
310
891
    frm_hdr->key_frame = !(bits & 1);
311
891
    frm_hdr->profile = (bits >> 1) & 7;
312
891
    frm_hdr->show = (bits >> 4) & 1;
313
891
    frm_hdr->partition_length = (bits >> 5);
314
891
    if (frm_hdr->profile > 3) {
315
0
      return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
316
0
                         "Incorrect keyframe parameters.");
317
0
    }
318
891
    if (!frm_hdr->show) {
319
0
      return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
320
0
                         "Frame not displayable.");
321
0
    }
322
891
    buf += 3;
323
891
    buf_size -= 3;
324
891
  }
325
326
0
  pic_hdr = &dec->pic_hdr;
327
891
  if (frm_hdr->key_frame) {
328
    // Paragraph 9.2
329
891
    if (buf_size < 7) {
330
0
      return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
331
0
                         "cannot parse picture header");
332
0
    }
333
891
    if (!VP8CheckSignature(buf, buf_size)) {
334
0
      return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, "Bad code word");
335
0
    }
336
891
    pic_hdr->width = ((buf[4] << 8) | buf[3]) & 0x3fff;
337
891
    pic_hdr->xscale = buf[4] >> 6;  // ratio: 1, 5/4 5/3 or 2
338
891
    pic_hdr->height = ((buf[6] << 8) | buf[5]) & 0x3fff;
339
891
    pic_hdr->yscale = buf[6] >> 6;
340
891
    buf += 7;
341
891
    buf_size -= 7;
342
343
891
    dec->mb_w = (pic_hdr->width + 15) >> 4;
344
891
    dec->mb_h = (pic_hdr->height + 15) >> 4;
345
346
    // Setup default output area (can be later modified during io->setup())
347
891
    io->width = pic_hdr->width;
348
891
    io->height = pic_hdr->height;
349
    // IMPORTANT! use some sane dimensions in crop* and scaled* fields.
350
    // So they can be used interchangeably without always testing for
351
    // 'use_cropping'.
352
891
    io->use_cropping = 0;
353
891
    io->crop_top = 0;
354
891
    io->crop_left = 0;
355
891
    io->crop_right = io->width;
356
891
    io->crop_bottom = io->height;
357
891
    io->use_scaling = 0;
358
891
    io->scaled_width = io->width;
359
891
    io->scaled_height = io->height;
360
361
891
    io->mb_w = io->width;   // for soundness
362
891
    io->mb_h = io->height;  // ditto
363
364
891
    VP8ResetProba(&dec->proba);
365
891
    ResetSegmentHeader(&dec->segment_hdr);
366
891
  }
367
368
  // Check if we have all the partition #0 available, and initialize dec->br
369
  // to read this partition (and this partition only).
370
891
  if (frm_hdr->partition_length > buf_size) {
371
0
    return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, "bad partition length");
372
0
  }
373
374
891
  br = &dec->br;
375
891
  VP8InitBitReader(br, buf, frm_hdr->partition_length);
376
891
  buf += frm_hdr->partition_length;
377
891
  buf_size -= frm_hdr->partition_length;
378
379
891
  if (frm_hdr->key_frame) {
380
891
    pic_hdr->colorspace = VP8Get(br, "global-header");
381
891
    pic_hdr->clamp_type = VP8Get(br, "global-header");
382
891
  }
383
891
  if (!ParseSegmentHeader(br, &dec->segment_hdr, &dec->proba)) {
384
4
    return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
385
4
                       "cannot parse segment header");
386
4
  }
387
  // Filter specs
388
887
  if (!ParseFilterHeader(br, dec)) {
389
2
    return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
390
2
                       "cannot parse filter header");
391
2
  }
392
885
  status = ParsePartitions(dec, buf, buf_size);
393
885
  if (status != VP8_STATUS_OK) {
394
9
    return VP8SetError(dec, status, "cannot parse partitions");
395
9
  }
396
397
  // quantizer change
398
876
  VP8ParseQuant(dec);
399
400
  // Frame buffer marking
401
876
  if (!frm_hdr->key_frame) {
402
0
    return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE, "Not a key frame.");
403
0
  }
404
405
876
  VP8Get(br, "global-header");  // ignore the value of 'update_proba'
406
407
876
  VP8ParseProba(br, dec);
408
409
  // sanitized state
410
876
  dec->ready = 1;
411
876
  return 1;
412
876
}
413
414
//------------------------------------------------------------------------------
415
// Residual decoding (Paragraph 13.2 / 13.3)
416
417
static const uint8_t kCat3[] = {173, 148, 140, 0};
418
static const uint8_t kCat4[] = {176, 155, 140, 135, 0};
419
static const uint8_t kCat5[] = {180, 157, 141, 134, 130, 0};
420
static const uint8_t kCat6[] = {254, 254, 243, 230, 196, 177,
421
                                153, 140, 133, 130, 129, 0};
422
static const uint8_t* const kCat3456[] = {kCat3, kCat4, kCat5, kCat6};
423
static const uint8_t kZigzag[16] = {0, 1,  4,  8,  5, 2,  3,  6,
424
                                    9, 12, 13, 10, 7, 11, 14, 15};
425
426
// See section 13-2: https://datatracker.ietf.org/doc/html/rfc6386#section-13.2
427
33.6k
static int GetLargeValue(VP8BitReader* const br, const uint8_t* const p) {
428
33.6k
  int v;
429
33.6k
  if (!VP8GetBit(br, p[3], "coeffs")) {
430
15.3k
    if (!VP8GetBit(br, p[4], "coeffs")) {
431
10.2k
      v = 2;
432
10.2k
    } else {
433
5.08k
      v = 3 + VP8GetBit(br, p[5], "coeffs");
434
5.08k
    }
435
18.3k
  } else {
436
18.3k
    if (!VP8GetBit(br, p[6], "coeffs")) {
437
6.27k
      if (!VP8GetBit(br, p[7], "coeffs")) {
438
2.77k
        v = 5 + VP8GetBit(br, 159, "coeffs");
439
3.49k
      } else {
440
3.49k
        v = 7 + 2 * VP8GetBit(br, 165, "coeffs");
441
3.49k
        v += VP8GetBit(br, 145, "coeffs");
442
3.49k
      }
443
12.0k
    } else {
444
12.0k
      const uint8_t* tab;
445
12.0k
      const int bit1 = VP8GetBit(br, p[8], "coeffs");
446
12.0k
      const int bit0 = VP8GetBit(br, p[9 + bit1], "coeffs");
447
12.0k
      const int cat = 2 * bit1 + bit0;
448
12.0k
      v = 0;
449
135k
      for (tab = kCat3456[cat]; *tab; ++tab) {
450
123k
        v += v + VP8GetBit(br, *tab, "coeffs");
451
123k
      }
452
12.0k
      v += 3 + (8 << cat);
453
12.0k
    }
454
18.3k
  }
455
33.6k
  return v;
456
33.6k
}
457
458
// Returns the position of the last non-zero coeff plus one
459
static int GetCoeffsFast(VP8BitReader* const br,
460
                         const VP8BandProbas* const prob[], int ctx,
461
363k
                         const quant_t dq, int n, int16_t* out) {
462
363k
  const uint8_t* p = prob[n]->probas[ctx];
463
460k
  for (; n < 16; ++n) {
464
453k
    if (!VP8GetBit(br, p[0], "coeffs")) {
465
355k
      return n;  // previous coeff was last non-zero coeff
466
355k
    }
467
236k
    while (!VP8GetBit(br, p[1], "coeffs")) {  // sequence of zero coeffs
468
139k
      p = prob[++n]->probas[0];
469
139k
      if (n == 16) return 16;
470
139k
    }
471
97.3k
    {  // non zero coeff
472
97.3k
      const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas[0];
473
97.3k
      int v;
474
97.3k
      if (!VP8GetBit(br, p[2], "coeffs")) {
475
63.6k
        v = 1;
476
63.6k
        p = p_ctx[1];
477
63.6k
      } else {
478
33.6k
        v = GetLargeValue(br, p);
479
33.6k
        p = p_ctx[2];
480
33.6k
      }
481
97.3k
      out[kZigzag[n]] = VP8GetSigned(br, v, "coeffs") * dq[n > 0];
482
97.3k
    }
483
97.3k
  }
484
6.79k
  return 16;
485
363k
}
486
487
// This version of GetCoeffs() uses VP8GetBitAlt() which is an alternate version
488
// of VP8GetBitAlt() targeting specific platforms.
489
static int GetCoeffsAlt(VP8BitReader* const br,
490
                        const VP8BandProbas* const prob[], int ctx,
491
0
                        const quant_t dq, int n, int16_t* out) {
492
0
  const uint8_t* p = prob[n]->probas[ctx];
493
0
  for (; n < 16; ++n) {
494
0
    if (!VP8GetBitAlt(br, p[0], "coeffs")) {
495
0
      return n;  // previous coeff was last non-zero coeff
496
0
    }
497
0
    while (!VP8GetBitAlt(br, p[1], "coeffs")) {  // sequence of zero coeffs
498
0
      p = prob[++n]->probas[0];
499
0
      if (n == 16) return 16;
500
0
    }
501
0
    {  // non zero coeff
502
0
      const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas[0];
503
0
      int v;
504
0
      if (!VP8GetBitAlt(br, p[2], "coeffs")) {
505
0
        v = 1;
506
0
        p = p_ctx[1];
507
0
      } else {
508
0
        v = GetLargeValue(br, p);
509
0
        p = p_ctx[2];
510
0
      }
511
0
      out[kZigzag[n]] = VP8GetSigned(br, v, "coeffs") * dq[n > 0];
512
0
    }
513
0
  }
514
0
  return 16;
515
0
}
516
517
extern VP8CPUInfo VP8GetCPUInfo;
518
519
1
WEBP_DSP_INIT_FUNC(InitGetCoeffs) {
520
1
  if (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kSlowSSSE3)) {
521
0
    GetCoeffs = GetCoeffsAlt;
522
1
  } else {
523
1
    GetCoeffs = GetCoeffsFast;
524
1
  }
525
1
}
526
527
355k
static WEBP_INLINE uint32_t NzCodeBits(uint32_t nz_coeffs, int nz, int dc_nz) {
528
355k
  nz_coeffs <<= 2;
529
355k
  nz_coeffs |= (nz > 3) ? 3 : (nz > 1) ? 2 : dc_nz;
530
355k
  return nz_coeffs;
531
355k
}
532
533
static int ParseResiduals(VP8Decoder* const dec, VP8MB* const mb,
534
14.8k
                          VP8BitReader* const token_br) {
535
14.8k
  const VP8BandProbas*(*const bands)[16 + 1] = dec->proba.bands_ptr;
536
14.8k
  const VP8BandProbas* const* ac_proba;
537
14.8k
  VP8MBData* const block = dec->mb_data + dec->mb_x;
538
14.8k
  const VP8QuantMatrix* const q = &dec->dqm[block->segment];
539
14.8k
  int16_t* dst = block->coeffs;
540
14.8k
  VP8MB* const left_mb = dec->mb_info - 1;
541
14.8k
  uint8_t tnz, lnz;
542
14.8k
  uint32_t non_zero_y = 0;
543
14.8k
  uint32_t non_zero_uv = 0;
544
14.8k
  int x, y, ch;
545
14.8k
  uint32_t out_t_nz, out_l_nz;
546
14.8k
  int first;
547
548
14.8k
  WEBP_UNSAFE_MEMSET(dst, 0, 384 * sizeof(*dst));
549
14.8k
  if (!block->is_i4x4) {  // parse DC
550
7.52k
    int16_t dc[16] = {0};
551
7.52k
    const int ctx = mb->nz_dc + left_mb->nz_dc;
552
7.52k
    const int nz = GetCoeffs(token_br, bands[1], ctx, q->y2_mat, 0, dc);
553
7.52k
    mb->nz_dc = left_mb->nz_dc = (nz > 0);
554
7.52k
    if (nz > 1) {  // more than just the DC -> perform the full transform
555
1.34k
      VP8TransformWHT(dc, dst);
556
6.18k
    } else {  // only DC is non-zero -> inlined simplified transform
557
6.18k
      int i;
558
6.18k
      const int dc0 = (dc[0] + 3) >> 3;
559
105k
      for (i = 0; i < 16 * 16; i += 16) dst[i] = dc0;
560
6.18k
    }
561
7.52k
    first = 1;
562
7.52k
    ac_proba = bands[0];
563
7.52k
  } else {
564
7.29k
    first = 0;
565
7.29k
    ac_proba = bands[3];
566
7.29k
  }
567
568
14.8k
  tnz = mb->nz & 0x0f;
569
14.8k
  lnz = left_mb->nz & 0x0f;
570
74.1k
  for (y = 0; y < 4; ++y) {
571
59.2k
    int l = lnz & 1;
572
59.2k
    uint32_t nz_coeffs = 0;
573
296k
    for (x = 0; x < 4; ++x) {
574
237k
      const int ctx = l + (tnz & 1);
575
237k
      const int nz = GetCoeffs(token_br, ac_proba, ctx, q->y1_mat, first, dst);
576
237k
      l = (nz > first);
577
237k
      tnz = (tnz >> 1) | (l << 7);
578
237k
      nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
579
237k
      dst += 16;
580
237k
    }
581
59.2k
    tnz >>= 4;
582
59.2k
    lnz = (lnz >> 1) | (l << 7);
583
59.2k
    non_zero_y = (non_zero_y << 8) | nz_coeffs;
584
59.2k
  }
585
14.8k
  out_t_nz = tnz;
586
14.8k
  out_l_nz = lnz >> 4;
587
588
44.4k
  for (ch = 0; ch < 4; ch += 2) {
589
29.6k
    uint32_t nz_coeffs = 0;
590
29.6k
    tnz = mb->nz >> (4 + ch);
591
29.6k
    lnz = left_mb->nz >> (4 + ch);
592
88.9k
    for (y = 0; y < 2; ++y) {
593
59.2k
      int l = lnz & 1;
594
177k
      for (x = 0; x < 2; ++x) {
595
118k
        const int ctx = l + (tnz & 1);
596
118k
        const int nz = GetCoeffs(token_br, bands[2], ctx, q->uv_mat, 0, dst);
597
118k
        l = (nz > 0);
598
118k
        tnz = (tnz >> 1) | (l << 3);
599
118k
        nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
600
118k
        dst += 16;
601
118k
      }
602
59.2k
      tnz >>= 2;
603
59.2k
      lnz = (lnz >> 1) | (l << 5);
604
59.2k
    }
605
    // Note: we don't really need the per-4x4 details for U/V blocks.
606
29.6k
    non_zero_uv |= nz_coeffs << (4 * ch);
607
29.6k
    out_t_nz |= (tnz << 4) << ch;
608
29.6k
    out_l_nz |= (lnz & 0xf0) << ch;
609
29.6k
  }
610
14.8k
  mb->nz = out_t_nz;
611
14.8k
  left_mb->nz = out_l_nz;
612
613
14.8k
  block->non_zero_y = non_zero_y;
614
14.8k
  block->non_zero_uv = non_zero_uv;
615
616
  // We look at the mode-code of each block and check if some blocks have less
617
  // than three non-zero coeffs (code < 2). This is to avoid dithering flat and
618
  // empty blocks.
619
14.8k
  block->dither = (non_zero_uv & 0xaaaa) ? 0 : q->dither;
620
621
14.8k
  return !(non_zero_y | non_zero_uv);  // will be used for further optimization
622
14.8k
}
623
624
//------------------------------------------------------------------------------
625
// Main loop
626
627
18.2k
int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) {
628
18.2k
  VP8MB* const left = dec->mb_info - 1;
629
18.2k
  VP8MB* const mb = dec->mb_info + dec->mb_x;
630
18.2k
  VP8MBData* const block = dec->mb_data + dec->mb_x;
631
18.2k
  int skip = dec->use_skip_proba ? block->skip : 0;
632
633
18.2k
  if (!skip) {
634
14.8k
    skip = ParseResiduals(dec, mb, token_br);
635
14.8k
  } else {
636
3.46k
    left->nz = mb->nz = 0;
637
3.46k
    if (!block->is_i4x4) {
638
2.26k
      left->nz_dc = mb->nz_dc = 0;
639
2.26k
    }
640
3.46k
    block->non_zero_y = 0;
641
3.46k
    block->non_zero_uv = 0;
642
3.46k
    block->dither = 0;
643
3.46k
  }
644
645
18.2k
  if (dec->filter_type > 0) {  // store filter info
646
15.2k
    VP8FInfo* const finfo = dec->f_info + dec->mb_x;
647
15.2k
    *finfo = dec->fstrengths[block->segment][block->is_i4x4];
648
15.2k
    finfo->f_inner |= !skip;
649
15.2k
  }
650
651
18.2k
  return !token_br->eof;
652
18.2k
}
653
654
5.25k
void VP8InitScanline(VP8Decoder* const dec) {
655
5.25k
  VP8MB* const left = dec->mb_info - 1;
656
5.25k
  left->nz = 0;
657
5.25k
  left->nz_dc = 0;
658
5.25k
  WEBP_UNSAFE_MEMSET(dec->intra_l, B_DC_PRED, sizeof(dec->intra_l));
659
5.25k
  dec->mb_x = 0;
660
5.25k
}
661
662
876
static int ParseFrame(VP8Decoder* const dec, VP8Io* io) {
663
5.17k
  for (dec->mb_y = 0; dec->mb_y < dec->br_mb_y; ++dec->mb_y) {
664
    // Parse bitstream for this row.
665
4.81k
    VP8BitReader* const token_br =
666
4.81k
        &dec->parts[dec->mb_y & dec->num_parts_minus_one];
667
4.81k
    if (!VP8ParseIntraModeRow(&dec->br, dec)) {
668
197
      return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
669
197
                         "Premature end-of-partition0 encountered.");
670
197
    }
671
22.6k
    for (; dec->mb_x < dec->mb_w; ++dec->mb_x) {
672
18.2k
      if (!VP8DecodeMB(dec, token_br)) {
673
242
        return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
674
242
                           "Premature end-of-file encountered.");
675
242
      }
676
18.2k
    }
677
4.37k
    VP8InitScanline(dec);  // Prepare for next scanline
678
679
    // Reconstruct, filter and emit the row.
680
4.37k
    if (!VP8ProcessRow(dec, io)) {
681
78
      return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted.");
682
78
    }
683
4.37k
  }
684
359
  if (dec->mt_method > 0) {
685
0
    if (!WebPGetWorkerInterface()->Sync(&dec->worker)) return 0;
686
0
  }
687
688
359
  return 1;
689
359
}
690
691
// Main entry point
692
876
int VP8Decode(VP8Decoder* const dec, VP8Io* const io) {
693
876
  int ok = 0;
694
876
  if (dec == NULL) {
695
0
    return 0;
696
0
  }
697
876
  if (io == NULL) {
698
0
    return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
699
0
                       "NULL VP8Io parameter in VP8Decode().");
700
0
  }
701
702
876
  if (!dec->ready) {
703
0
    if (!VP8GetHeaders(dec, io)) {
704
0
      return 0;
705
0
    }
706
0
  }
707
876
  assert(dec->ready);
708
709
  // Finish setting up the decoding parameter. Will call io->setup().
710
876
  ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK);
711
876
  if (ok) {  // good to go.
712
    // Will allocate memory and prepare everything.
713
876
    if (ok) ok = VP8InitFrame(dec, io);
714
715
    // Main decoding loop
716
876
    if (ok) ok = ParseFrame(dec, io);
717
718
    // Exit.
719
876
    ok &= VP8ExitCritical(dec, io);
720
876
  }
721
722
876
  if (!ok) {
723
517
    VP8Clear(dec);
724
517
    return 0;
725
517
  }
726
727
359
  dec->ready = 0;
728
359
  return ok;
729
876
}
730
731
1.40k
void VP8Clear(VP8Decoder* const dec) {
732
1.40k
  if (dec == NULL) {
733
0
    return;
734
0
  }
735
1.40k
  WebPGetWorkerInterface()->End(&dec->worker);
736
1.40k
  WebPDeallocateAlphaMemory(dec);
737
1.40k
  WebPSafeFree(dec->mem);
738
1.40k
  dec->mem = NULL;
739
1.40k
  dec->mem_size = 0;
740
1.40k
  WEBP_UNSAFE_MEMSET(&dec->br, 0, sizeof(dec->br));
741
1.40k
  dec->ready = 0;
742
1.40k
}
743
744
//------------------------------------------------------------------------------