Coverage Report

Created: 2026-09-14 07:37

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
315k
static void SetOk(VP8Decoder* const dec) {
57
315k
  dec->status = VP8_STATUS_OK;
58
315k
  dec->error_msg = "OK";
59
315k
}
60
61
284k
int VP8InitIoInternal(VP8Io* const io, int version) {
62
284k
  if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
63
0
    return 0;  // mismatch error
64
0
  }
65
284k
  if (io != NULL) {
66
284k
    WEBP_UNSAFE_MEMSET(io, 0, sizeof(*io));
67
284k
  }
68
284k
  return 1;
69
284k
}
70
71
157k
VP8Decoder* VP8New(void) {
72
157k
  VP8Decoder* const dec = (VP8Decoder*)WebPSafeCalloc(1ULL, sizeof(*dec));
73
157k
  if (dec != NULL) {
74
157k
    SetOk(dec);
75
157k
    WebPGetWorkerInterface()->Init(&dec->worker);
76
157k
    dec->ready = 0;
77
157k
    dec->num_parts_minus_one = 0;
78
157k
    InitGetCoeffs();
79
157k
  }
80
157k
  return dec;
81
157k
}
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
157k
void VP8Delete(VP8Decoder* const dec) {
95
157k
  if (dec != NULL) {
96
157k
    VP8Clear(dec);
97
157k
    WebPSafeFree(dec);
98
157k
  }
99
157k
}
100
101
int VP8SetError(VP8Decoder* const dec, VP8StatusCode error,
102
50.9k
                const char* const msg) {
103
  // VP8_STATUS_SUSPENDED is only meaningful in incremental decoding.
104
50.9k
  assert(dec->incremental || error != VP8_STATUS_SUSPENDED);
105
  // The oldest error reported takes precedence over the new one.
106
50.9k
  if (dec->status == VP8_STATUS_OK) {
107
50.6k
    dec->status = error;
108
50.6k
    dec->error_msg = msg;
109
50.6k
    dec->ready = 0;
110
50.6k
  }
111
50.9k
  return 0;
112
50.9k
}
113
114
//------------------------------------------------------------------------------
115
116
int VP8CheckSignature(const uint8_t* const WEBP_COUNTED_BY(data_size) data,
117
717k
                      size_t data_size) {
118
717k
  return (data_size >= 3 && data[0] == 0x9d && data[1] == 0x01 &&
119
699k
          data[2] == 0x2a);
120
717k
}
121
122
int VP8GetInfo(const uint8_t* WEBP_COUNTED_BY(data_size) data, size_t data_size,
123
559k
               size_t chunk_size, int* const width, int* const height) {
124
559k
  if (data == NULL || data_size < VP8_FRAME_HEADER_SIZE) {
125
4
    return 0;  // not enough data
126
4
  }
127
  // check signature
128
559k
  if (!VP8CheckSignature(data + 3, data_size - 3)) {
129
19.7k
    return 0;  // Wrong signature.
130
539k
  } else {
131
539k
    const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16);
132
539k
    const int key_frame = !(bits & 1);
133
539k
    const int w = ((data[7] << 8) | data[6]) & 0x3fff;
134
539k
    const int h = ((data[9] << 8) | data[8]) & 0x3fff;
135
136
539k
    if (!key_frame) {  // Not a keyframe.
137
262
      return 0;
138
262
    }
139
140
539k
    if (((bits >> 1) & 7) > 3) {
141
172
      return 0;  // unknown profile
142
172
    }
143
539k
    if (!((bits >> 4) & 1)) {
144
514
      return 0;  // first frame is invisible!
145
514
    }
146
538k
    if (((bits >> 5)) >= chunk_size) {  // partition_length
147
678
      return 0;                         // inconsistent size information.
148
678
    }
149
538k
    if (w == 0 || h == 0) {
150
2.45k
      return 0;  // We don't support a zero width or height.
151
2.45k
    }
152
153
535k
    if (width) {
154
535k
      *width = w;
155
535k
    }
156
535k
    if (height) {
157
535k
      *height = h;
158
535k
    }
159
160
535k
    return 1;
161
538k
  }
162
559k
}
163
164
//------------------------------------------------------------------------------
165
// Header parsing
166
167
157k
static void ResetSegmentHeader(VP8SegmentHeader* const hdr) {
168
157k
  assert(hdr != NULL);
169
157k
  hdr->use_segment = 0;
170
157k
  hdr->update_map = 0;
171
157k
  hdr->absolute_delta = 1;
172
157k
  WEBP_UNSAFE_MEMSET(hdr->quantizer, 0, sizeof(hdr->quantizer));
173
157k
  WEBP_UNSAFE_MEMSET(hdr->filter_strength, 0, sizeof(hdr->filter_strength));
174
157k
}
175
176
// Paragraph 9.3
177
static int ParseSegmentHeader(VP8BitReader* br, VP8SegmentHeader* hdr,
178
157k
                              VP8Proba* proba) {
179
157k
  assert(br != NULL);
180
157k
  assert(hdr != NULL);
181
157k
  hdr->use_segment = VP8Get(br, "global-header");
182
157k
  if (hdr->use_segment) {
183
116k
    hdr->update_map = VP8Get(br, "global-header");
184
116k
    if (VP8Get(br, "global-header")) {  // update data
185
113k
      int s;
186
113k
      hdr->absolute_delta = VP8Get(br, "global-header");
187
566k
      for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
188
453k
        hdr->quantizer[s] = VP8Get(br, "global-header")
189
453k
                                ? VP8GetSignedValue(br, 7, "global-header")
190
453k
                                : 0;
191
453k
      }
192
566k
      for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
193
453k
        hdr->filter_strength[s] =
194
453k
            VP8Get(br, "global-header")
195
453k
                ? VP8GetSignedValue(br, 6, "global-header")
196
453k
                : 0;
197
453k
      }
198
113k
    }
199
116k
    if (hdr->update_map) {
200
115k
      int s;
201
461k
      for (s = 0; s < MB_FEATURE_TREE_PROBS; ++s) {
202
346k
        proba->segments[s] = VP8Get(br, "global-header")
203
346k
                                 ? VP8GetValue(br, 8, "global-header")
204
346k
                                 : 255u;
205
346k
      }
206
115k
    }
207
116k
  } else {
208
41.1k
    hdr->update_map = 0;
209
41.1k
  }
210
157k
  return !br->eof;
211
157k
}
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
155k
                                     size_t size) {
225
155k
  VP8BitReader* const br = &dec->br;
226
155k
  const uint8_t* WEBP_BIDI_INDEXABLE sz = buf;
227
155k
  const uint8_t* buf_end = buf + size;
228
155k
  const uint8_t* WEBP_BIDI_INDEXABLE part_start;
229
155k
  size_t size_left = size;
230
155k
  size_t last_part;
231
155k
  size_t p;
232
233
155k
  dec->num_parts_minus_one = (1 << VP8GetValue(br, 2, "global-header")) - 1;
234
155k
  last_part = dec->num_parts_minus_one;
235
155k
  if (size < 3 * last_part) {
236
    // we can't even read the sizes with sz[]! That's a failure.
237
511
    return VP8_STATUS_NOT_ENOUGH_DATA;
238
511
  }
239
154k
  part_start = buf + last_part * 3;
240
154k
  size_left -= last_part * 3;
241
246k
  for (p = 0; p < last_part; ++p) {
242
91.3k
    size_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16);
243
91.3k
    if (psize > size_left) psize = size_left;
244
91.3k
    VP8InitBitReader(dec->parts + p, part_start, psize);
245
91.3k
    part_start += psize;
246
91.3k
    size_left -= psize;
247
91.3k
    sz += 3;
248
91.3k
  }
249
154k
  VP8InitBitReader(dec->parts + last_part, part_start, size_left);
250
154k
  if (part_start < buf_end) return VP8_STATUS_OK;
251
20.3k
  return dec->incremental
252
20.3k
             ? VP8_STATUS_SUSPENDED  // Init is ok, but there's not enough data
253
20.3k
             : VP8_STATUS_NOT_ENOUGH_DATA;
254
154k
}
255
256
// Paragraph 9.4
257
157k
static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) {
258
157k
  VP8FilterHeader* const hdr = &dec->filter_hdr;
259
157k
  hdr->simple = VP8Get(br, "global-header");
260
157k
  hdr->level = VP8GetValue(br, 6, "global-header");
261
157k
  hdr->sharpness = VP8GetValue(br, 3, "global-header");
262
157k
  hdr->use_lf_delta = VP8Get(br, "global-header");
263
157k
  if (hdr->use_lf_delta) {
264
65.2k
    if (VP8Get(br, "global-header")) {  // update lf-delta?
265
54.3k
      int i;
266
271k
      for (i = 0; i < NUM_REF_LF_DELTAS; ++i) {
267
217k
        if (VP8Get(br, "global-header")) {
268
121k
          hdr->ref_lf_delta[i] = VP8GetSignedValue(br, 6, "global-header");
269
121k
        }
270
217k
      }
271
271k
      for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) {
272
217k
        if (VP8Get(br, "global-header")) {
273
111k
          hdr->mode_lf_delta[i] = VP8GetSignedValue(br, 6, "global-header");
274
111k
        }
275
217k
      }
276
54.3k
    }
277
65.2k
  }
278
157k
  dec->filter_type = (hdr->level == 0) ? 0 : hdr->simple ? 1 : 2;
279
157k
  return !br->eof;
280
157k
}
281
282
// Topmost call
283
157k
int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
284
157k
  size_t buf_size;
285
157k
  const uint8_t* WEBP_COUNTED_BY(buf_size) buf;
286
157k
  VP8FrameHeader* frm_hdr;
287
157k
  VP8PictureHeader* pic_hdr;
288
157k
  VP8BitReader* br;
289
157k
  VP8StatusCode status;
290
291
157k
  if (dec == NULL) {
292
0
    return 0;
293
0
  }
294
157k
  SetOk(dec);
295
157k
  if (io == NULL) {
296
0
    return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
297
0
                       "null VP8Io passed to VP8GetHeaders()");
298
0
  }
299
157k
  buf_size = io->data_size;
300
157k
  buf =
301
157k
      WEBP_UNSAFE_FORGE_BIDI_INDEXABLE(const uint8_t*, io->data, io->data_size);
302
157k
  if (buf_size < 4) {
303
0
    return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, "Truncated header.");
304
0
  }
305
306
  // Paragraph 9.1
307
157k
  {
308
157k
    const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16);
309
157k
    frm_hdr = &dec->frm_hdr;
310
157k
    frm_hdr->key_frame = !(bits & 1);
311
157k
    frm_hdr->profile = (bits >> 1) & 7;
312
157k
    frm_hdr->show = (bits >> 4) & 1;
313
157k
    frm_hdr->partition_length = (bits >> 5);
314
157k
    if (frm_hdr->profile > 3) {
315
0
      return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
316
0
                         "Incorrect keyframe parameters.");
317
0
    }
318
157k
    if (!frm_hdr->show) {
319
0
      return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
320
0
                         "Frame not displayable.");
321
0
    }
322
157k
    buf += 3;
323
157k
    buf_size -= 3;
324
157k
  }
325
326
0
  pic_hdr = &dec->pic_hdr;
327
157k
  if (frm_hdr->key_frame) {
328
    // Paragraph 9.2
329
157k
    if (buf_size < 7) {
330
0
      return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
331
0
                         "cannot parse picture header");
332
0
    }
333
157k
    if (!VP8CheckSignature(buf, buf_size)) {
334
0
      return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR, "Bad code word");
335
0
    }
336
157k
    pic_hdr->width = ((buf[4] << 8) | buf[3]) & 0x3fff;
337
157k
    pic_hdr->xscale = buf[4] >> 6;  // ratio: 1, 5/4 5/3 or 2
338
157k
    pic_hdr->height = ((buf[6] << 8) | buf[5]) & 0x3fff;
339
157k
    pic_hdr->yscale = buf[6] >> 6;
340
157k
    buf += 7;
341
157k
    buf_size -= 7;
342
343
157k
    dec->mb_w = (pic_hdr->width + 15) >> 4;
344
157k
    dec->mb_h = (pic_hdr->height + 15) >> 4;
345
346
    // Setup default output area (can be later modified during io->setup())
347
157k
    io->width = pic_hdr->width;
348
157k
    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
157k
    io->use_cropping = 0;
353
157k
    io->crop_top = 0;
354
157k
    io->crop_left = 0;
355
157k
    io->crop_right = io->width;
356
157k
    io->crop_bottom = io->height;
357
157k
    io->use_scaling = 0;
358
157k
    io->scaled_width = io->width;
359
157k
    io->scaled_height = io->height;
360
361
157k
    io->mb_w = io->width;   // for soundness
362
157k
    io->mb_h = io->height;  // ditto
363
364
157k
    VP8ResetProba(&dec->proba);
365
157k
    ResetSegmentHeader(&dec->segment_hdr);
366
157k
  }
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
157k
  if (frm_hdr->partition_length > buf_size) {
371
79
    return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA, "bad partition length");
372
79
  }
373
374
157k
  br = &dec->br;
375
157k
  VP8InitBitReader(br, buf, frm_hdr->partition_length);
376
157k
  buf += frm_hdr->partition_length;
377
157k
  buf_size -= frm_hdr->partition_length;
378
379
157k
  if (frm_hdr->key_frame) {
380
157k
    pic_hdr->colorspace = VP8Get(br, "global-header");
381
157k
    pic_hdr->clamp_type = VP8Get(br, "global-header");
382
157k
  }
383
157k
  if (!ParseSegmentHeader(br, &dec->segment_hdr, &dec->proba)) {
384
446
    return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
385
446
                       "cannot parse segment header");
386
446
  }
387
  // Filter specs
388
157k
  if (!ParseFilterHeader(br, dec)) {
389
2.05k
    return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
390
2.05k
                       "cannot parse filter header");
391
2.05k
  }
392
155k
  status = ParsePartitions(dec, buf, buf_size);
393
155k
  if (status != VP8_STATUS_OK) {
394
20.8k
    return VP8SetError(dec, status, "cannot parse partitions");
395
20.8k
  }
396
397
  // quantizer change
398
134k
  VP8ParseQuant(dec);
399
400
  // Frame buffer marking
401
134k
  if (!frm_hdr->key_frame) {
402
0
    return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE, "Not a key frame.");
403
0
  }
404
405
134k
  VP8Get(br, "global-header");  // ignore the value of 'update_proba'
406
407
134k
  VP8ParseProba(br, dec);
408
409
  // sanitized state
410
134k
  dec->ready = 1;
411
134k
  return 1;
412
134k
}
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
3.39M
static int GetLargeValue(VP8BitReader* const br, const uint8_t* const p) {
428
3.39M
  int v;
429
3.39M
  if (!VP8GetBit(br, p[3], "coeffs")) {
430
727k
    if (!VP8GetBit(br, p[4], "coeffs")) {
431
498k
      v = 2;
432
498k
    } else {
433
229k
      v = 3 + VP8GetBit(br, p[5], "coeffs");
434
229k
    }
435
2.66M
  } else {
436
2.66M
    if (!VP8GetBit(br, p[6], "coeffs")) {
437
116k
      if (!VP8GetBit(br, p[7], "coeffs")) {
438
74.5k
        v = 5 + VP8GetBit(br, 159, "coeffs");
439
74.5k
      } else {
440
41.6k
        v = 7 + 2 * VP8GetBit(br, 165, "coeffs");
441
41.6k
        v += VP8GetBit(br, 145, "coeffs");
442
41.6k
      }
443
2.55M
    } else {
444
2.55M
      const uint8_t* tab;
445
2.55M
      const int bit1 = VP8GetBit(br, p[8], "coeffs");
446
2.55M
      const int bit0 = VP8GetBit(br, p[9 + bit1], "coeffs");
447
2.55M
      const int cat = 2 * bit1 + bit0;
448
2.55M
      v = 0;
449
30.2M
      for (tab = kCat3456[cat]; *tab; ++tab) {
450
27.6M
        v += v + VP8GetBit(br, *tab, "coeffs");
451
27.6M
      }
452
2.55M
      v += 3 + (8 << cat);
453
2.55M
    }
454
2.66M
  }
455
3.39M
  return v;
456
3.39M
}
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
16.2M
                         const quant_t dq, int n, int16_t* out) {
462
16.2M
  const uint8_t* p = prob[n]->probas[ctx];
463
22.5M
  for (; n < 16; ++n) {
464
22.3M
    if (!VP8GetBit(br, p[0], "coeffs")) {
465
16.0M
      return n;  // previous coeff was last non-zero coeff
466
16.0M
    }
467
8.36M
    while (!VP8GetBit(br, p[1], "coeffs")) {  // sequence of zero coeffs
468
2.11M
      p = prob[++n]->probas[0];
469
2.11M
      if (n == 16) return 16;
470
2.11M
    }
471
6.25M
    {  // non zero coeff
472
6.25M
      const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas[0];
473
6.25M
      int v;
474
6.25M
      if (!VP8GetBit(br, p[2], "coeffs")) {
475
2.85M
        v = 1;
476
2.85M
        p = p_ctx[1];
477
3.39M
      } else {
478
3.39M
        v = GetLargeValue(br, p);
479
3.39M
        p = p_ctx[2];
480
3.39M
      }
481
6.25M
      out[kZigzag[n]] = VP8GetSigned(br, v, "coeffs") * dq[n > 0];
482
6.25M
    }
483
6.25M
  }
484
178k
  return 16;
485
16.2M
}
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
7
WEBP_DSP_INIT_FUNC(InitGetCoeffs) {
520
7
  if (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kSlowSSSE3)) {
521
0
    GetCoeffs = GetCoeffsAlt;
522
7
  } else {
523
7
    GetCoeffs = GetCoeffsFast;
524
7
  }
525
7
}
526
527
16.1M
static WEBP_INLINE uint32_t NzCodeBits(uint32_t nz_coeffs, int nz, int dc_nz) {
528
16.1M
  nz_coeffs <<= 2;
529
16.1M
  nz_coeffs |= (nz > 3) ? 3 : (nz > 1) ? 2 : dc_nz;
530
16.1M
  return nz_coeffs;
531
16.1M
}
532
533
static int ParseResiduals(VP8Decoder* const dec, VP8MB* const mb,
534
672k
                          VP8BitReader* const token_br) {
535
672k
  const VP8BandProbas*(*const bands)[16 + 1] = dec->proba.bands_ptr;
536
672k
  const VP8BandProbas* const* ac_proba;
537
672k
  VP8MBData* const block = dec->mb_data + dec->mb_x;
538
672k
  const VP8QuantMatrix* const q = &dec->dqm[block->segment];
539
672k
  int16_t* dst = block->coeffs;
540
672k
  VP8MB* const left_mb = dec->mb_info - 1;
541
672k
  uint8_t tnz, lnz;
542
672k
  uint32_t non_zero_y = 0;
543
672k
  uint32_t non_zero_uv = 0;
544
672k
  int x, y, ch;
545
672k
  uint32_t out_t_nz, out_l_nz;
546
672k
  int first;
547
548
672k
  WEBP_UNSAFE_MEMSET(dst, 0, 384 * sizeof(*dst));
549
672k
  if (!block->is_i4x4) {  // parse DC
550
138k
    int16_t dc[16] = {0};
551
138k
    const int ctx = mb->nz_dc + left_mb->nz_dc;
552
138k
    const int nz = GetCoeffs(token_br, bands[1], ctx, q->y2_mat, 0, dc);
553
138k
    mb->nz_dc = left_mb->nz_dc = (nz > 0);
554
138k
    if (nz > 1) {  // more than just the DC -> perform the full transform
555
15.8k
      VP8TransformWHT(dc, dst);
556
123k
    } else {  // only DC is non-zero -> inlined simplified transform
557
123k
      int i;
558
123k
      const int dc0 = (dc[0] + 3) >> 3;
559
2.09M
      for (i = 0; i < 16 * 16; i += 16) dst[i] = dc0;
560
123k
    }
561
138k
    first = 1;
562
138k
    ac_proba = bands[0];
563
533k
  } else {
564
533k
    first = 0;
565
533k
    ac_proba = bands[3];
566
533k
  }
567
568
672k
  tnz = mb->nz & 0x0f;
569
672k
  lnz = left_mb->nz & 0x0f;
570
3.36M
  for (y = 0; y < 4; ++y) {
571
2.69M
    int l = lnz & 1;
572
2.69M
    uint32_t nz_coeffs = 0;
573
13.4M
    for (x = 0; x < 4; ++x) {
574
10.7M
      const int ctx = l + (tnz & 1);
575
10.7M
      const int nz = GetCoeffs(token_br, ac_proba, ctx, q->y1_mat, first, dst);
576
10.7M
      l = (nz > first);
577
10.7M
      tnz = (tnz >> 1) | (l << 7);
578
10.7M
      nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
579
10.7M
      dst += 16;
580
10.7M
    }
581
2.69M
    tnz >>= 4;
582
2.69M
    lnz = (lnz >> 1) | (l << 7);
583
2.69M
    non_zero_y = (non_zero_y << 8) | nz_coeffs;
584
2.69M
  }
585
672k
  out_t_nz = tnz;
586
672k
  out_l_nz = lnz >> 4;
587
588
2.01M
  for (ch = 0; ch < 4; ch += 2) {
589
1.34M
    uint32_t nz_coeffs = 0;
590
1.34M
    tnz = mb->nz >> (4 + ch);
591
1.34M
    lnz = left_mb->nz >> (4 + ch);
592
4.03M
    for (y = 0; y < 2; ++y) {
593
2.69M
      int l = lnz & 1;
594
8.07M
      for (x = 0; x < 2; ++x) {
595
5.38M
        const int ctx = l + (tnz & 1);
596
5.38M
        const int nz = GetCoeffs(token_br, bands[2], ctx, q->uv_mat, 0, dst);
597
5.38M
        l = (nz > 0);
598
5.38M
        tnz = (tnz >> 1) | (l << 3);
599
5.38M
        nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
600
5.38M
        dst += 16;
601
5.38M
      }
602
2.69M
      tnz >>= 2;
603
2.69M
      lnz = (lnz >> 1) | (l << 5);
604
2.69M
    }
605
    // Note: we don't really need the per-4x4 details for U/V blocks.
606
1.34M
    non_zero_uv |= nz_coeffs << (4 * ch);
607
1.34M
    out_t_nz |= (tnz << 4) << ch;
608
1.34M
    out_l_nz |= (lnz & 0xf0) << ch;
609
1.34M
  }
610
672k
  mb->nz = out_t_nz;
611
672k
  left_mb->nz = out_l_nz;
612
613
672k
  block->non_zero_y = non_zero_y;
614
672k
  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
672k
  block->dither = (non_zero_uv & 0xaaaa) ? 0 : q->dither;
620
621
672k
  return !(non_zero_y | non_zero_uv);  // will be used for further optimization
622
672k
}
623
624
//------------------------------------------------------------------------------
625
// Main loop
626
627
745k
int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) {
628
745k
  VP8MB* const left = dec->mb_info - 1;
629
745k
  VP8MB* const mb = dec->mb_info + dec->mb_x;
630
745k
  VP8MBData* const block = dec->mb_data + dec->mb_x;
631
745k
  int skip = dec->use_skip_proba ? block->skip : 0;
632
633
745k
  if (!skip) {
634
672k
    skip = ParseResiduals(dec, mb, token_br);
635
672k
  } else {
636
72.4k
    left->nz = mb->nz = 0;
637
72.4k
    if (!block->is_i4x4) {
638
46.5k
      left->nz_dc = mb->nz_dc = 0;
639
46.5k
    }
640
72.4k
    block->non_zero_y = 0;
641
72.4k
    block->non_zero_uv = 0;
642
72.4k
    block->dither = 0;
643
72.4k
  }
644
645
745k
  if (dec->filter_type > 0) {  // store filter info
646
374k
    VP8FInfo* const finfo = dec->f_info + dec->mb_x;
647
374k
    *finfo = dec->fstrengths[block->segment][block->is_i4x4];
648
374k
    finfo->f_inner |= !skip;
649
374k
  }
650
651
745k
  return !token_br->eof;
652
745k
}
653
654
425k
void VP8InitScanline(VP8Decoder* const dec) {
655
425k
  VP8MB* const left = dec->mb_info - 1;
656
425k
  left->nz = 0;
657
425k
  left->nz_dc = 0;
658
425k
  WEBP_UNSAFE_MEMSET(dec->intra_l, B_DC_PRED, sizeof(dec->intra_l));
659
425k
  dec->mb_x = 0;
660
425k
}
661
662
130k
static int ParseFrame(VP8Decoder* const dec, VP8Io* io) {
663
251k
  for (dec->mb_y = 0; dec->mb_y < dec->br_mb_y; ++dec->mb_y) {
664
    // Parse bitstream for this row.
665
148k
    VP8BitReader* const token_br =
666
148k
        &dec->parts[dec->mb_y & dec->num_parts_minus_one];
667
148k
    if (!VP8ParseIntraModeRow(&dec->br, dec)) {
668
2.88k
      return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
669
2.88k
                         "Premature end-of-partition0 encountered.");
670
2.88k
    }
671
351k
    for (; dec->mb_x < dec->mb_w; ++dec->mb_x) {
672
230k
      if (!VP8DecodeMB(dec, token_br)) {
673
24.2k
        return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
674
24.2k
                           "Premature end-of-file encountered.");
675
24.2k
      }
676
230k
    }
677
121k
    VP8InitScanline(dec);  // Prepare for next scanline
678
679
    // Reconstruct, filter and emit the row.
680
121k
    if (!VP8ProcessRow(dec, io)) {
681
140
      return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted.");
682
140
    }
683
121k
  }
684
103k
  if (dec->mt_method > 0) {
685
    // Collect the last row's put(), which may have aborted.
686
0
    if (!WebPGetWorkerInterface()->Sync(&dec->worker)) {
687
0
      return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted.");
688
0
    }
689
0
  }
690
691
103k
  return 1;
692
103k
}
693
694
// Main entry point
695
130k
int VP8Decode(VP8Decoder* const dec, VP8Io* const io) {
696
130k
  int ok = 0;
697
130k
  if (dec == NULL) {
698
0
    return 0;
699
0
  }
700
130k
  if (io == NULL) {
701
0
    return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
702
0
                       "NULL VP8Io parameter in VP8Decode().");
703
0
  }
704
705
130k
  if (!dec->ready) {
706
0
    if (!VP8GetHeaders(dec, io)) {
707
0
      return 0;
708
0
    }
709
0
  }
710
130k
  assert(dec->ready);
711
712
  // Finish setting up the decoding parameter. Will call io->setup().
713
130k
  ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK);
714
130k
  if (ok) {  // good to go.
715
    // Will allocate memory and prepare everything.
716
130k
    if (ok) ok = VP8InitFrame(dec, io);
717
718
    // Main decoding loop
719
130k
    if (ok) ok = ParseFrame(dec, io);
720
721
    // Exit.
722
130k
    ok &= VP8ExitCritical(dec, io);
723
130k
  }
724
725
130k
  if (!ok) {
726
27.2k
    VP8Clear(dec);
727
27.2k
    return 0;
728
27.2k
  }
729
730
103k
  dec->ready = 0;
731
103k
  return ok;
732
130k
}
733
734
185k
void VP8Clear(VP8Decoder* const dec) {
735
185k
  if (dec == NULL) {
736
0
    return;
737
0
  }
738
185k
  WebPGetWorkerInterface()->End(&dec->worker);
739
185k
  WebPDeallocateAlphaMemory(dec);
740
185k
  WebPSafeFree(dec->mem);
741
185k
  dec->mem = NULL;
742
185k
  dec->mem_size = 0;
743
185k
  WEBP_UNSAFE_MEMSET(&dec->br, 0, sizeof(dec->br));
744
185k
  dec->ready = 0;
745
185k
}
746
747
//------------------------------------------------------------------------------