Coverage Report

Created: 2024-06-18 06:09

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