Coverage Report

Created: 2024-07-27 06:27

/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
0
static void SetOk(VP8Decoder* const dec) {
43
0
  dec->status_ = VP8_STATUS_OK;
44
0
  dec->error_msg_ = "OK";
45
0
}
46
47
0
int VP8InitIoInternal(VP8Io* const io, int version) {
48
0
  if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_DECODER_ABI_VERSION)) {
49
0
    return 0;  // mismatch error
50
0
  }
51
0
  if (io != NULL) {
52
0
    memset(io, 0, sizeof(*io));
53
0
  }
54
0
  return 1;
55
0
}
56
57
0
VP8Decoder* VP8New(void) {
58
0
  VP8Decoder* const dec = (VP8Decoder*)WebPSafeCalloc(1ULL, sizeof(*dec));
59
0
  if (dec != NULL) {
60
0
    SetOk(dec);
61
0
    WebPGetWorkerInterface()->Init(&dec->worker_);
62
0
    dec->ready_ = 0;
63
0
    dec->num_parts_minus_one_ = 0;
64
0
    InitGetCoeffs();
65
0
  }
66
0
  return dec;
67
0
}
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
0
void VP8Delete(VP8Decoder* const dec) {
81
0
  if (dec != NULL) {
82
0
    VP8Clear(dec);
83
0
    WebPSafeFree(dec);
84
0
  }
85
0
}
86
87
int VP8SetError(VP8Decoder* const dec,
88
0
                VP8StatusCode error, const char* const msg) {
89
  // VP8_STATUS_SUSPENDED is only meaningful in incremental decoding.
90
0
  assert(dec->incremental_ || error != VP8_STATUS_SUSPENDED);
91
  // The oldest error reported takes precedence over the new one.
92
0
  if (dec->status_ == VP8_STATUS_OK) {
93
0
    dec->status_ = error;
94
0
    dec->error_msg_ = msg;
95
0
    dec->ready_ = 0;
96
0
  }
97
0
  return 0;
98
0
}
99
100
//------------------------------------------------------------------------------
101
102
0
int VP8CheckSignature(const uint8_t* const data, size_t data_size) {
103
0
  return (data_size >= 3 &&
104
0
          data[0] == 0x9d && data[1] == 0x01 && data[2] == 0x2a);
105
0
}
106
107
int VP8GetInfo(const uint8_t* data, size_t data_size, size_t chunk_size,
108
0
               int* const width, int* const height) {
109
0
  if (data == NULL || data_size < VP8_FRAME_HEADER_SIZE) {
110
0
    return 0;         // not enough data
111
0
  }
112
  // check signature
113
0
  if (!VP8CheckSignature(data + 3, data_size - 3)) {
114
0
    return 0;         // Wrong signature.
115
0
  } else {
116
0
    const uint32_t bits = data[0] | (data[1] << 8) | (data[2] << 16);
117
0
    const int key_frame = !(bits & 1);
118
0
    const int w = ((data[7] << 8) | data[6]) & 0x3fff;
119
0
    const int h = ((data[9] << 8) | data[8]) & 0x3fff;
120
121
0
    if (!key_frame) {   // Not a keyframe.
122
0
      return 0;
123
0
    }
124
125
0
    if (((bits >> 1) & 7) > 3) {
126
0
      return 0;         // unknown profile
127
0
    }
128
0
    if (!((bits >> 4) & 1)) {
129
0
      return 0;         // first frame is invisible!
130
0
    }
131
0
    if (((bits >> 5)) >= chunk_size) {  // partition_length
132
0
      return 0;         // inconsistent size information.
133
0
    }
134
0
    if (w == 0 || h == 0) {
135
0
      return 0;         // We don't support both width and height to be zero.
136
0
    }
137
138
0
    if (width) {
139
0
      *width = w;
140
0
    }
141
0
    if (height) {
142
0
      *height = h;
143
0
    }
144
145
0
    return 1;
146
0
  }
147
0
}
148
149
//------------------------------------------------------------------------------
150
// Header parsing
151
152
0
static void ResetSegmentHeader(VP8SegmentHeader* const hdr) {
153
0
  assert(hdr != NULL);
154
0
  hdr->use_segment_ = 0;
155
0
  hdr->update_map_ = 0;
156
0
  hdr->absolute_delta_ = 1;
157
0
  memset(hdr->quantizer_, 0, sizeof(hdr->quantizer_));
158
0
  memset(hdr->filter_strength_, 0, sizeof(hdr->filter_strength_));
159
0
}
160
161
// Paragraph 9.3
162
static int ParseSegmentHeader(VP8BitReader* br,
163
0
                              VP8SegmentHeader* hdr, VP8Proba* proba) {
164
0
  assert(br != NULL);
165
0
  assert(hdr != NULL);
166
0
  hdr->use_segment_ = VP8Get(br, "global-header");
167
0
  if (hdr->use_segment_) {
168
0
    hdr->update_map_ = VP8Get(br, "global-header");
169
0
    if (VP8Get(br, "global-header")) {   // update data
170
0
      int s;
171
0
      hdr->absolute_delta_ = VP8Get(br, "global-header");
172
0
      for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
173
0
        hdr->quantizer_[s] = VP8Get(br, "global-header") ?
174
0
            VP8GetSignedValue(br, 7, "global-header") : 0;
175
0
      }
176
0
      for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
177
0
        hdr->filter_strength_[s] = VP8Get(br, "global-header") ?
178
0
            VP8GetSignedValue(br, 6, "global-header") : 0;
179
0
      }
180
0
    }
181
0
    if (hdr->update_map_) {
182
0
      int s;
183
0
      for (s = 0; s < MB_FEATURE_TREE_PROBS; ++s) {
184
0
        proba->segments_[s] = VP8Get(br, "global-header") ?
185
0
            VP8GetValue(br, 8, "global-header") : 255u;
186
0
      }
187
0
    }
188
0
  } else {
189
0
    hdr->update_map_ = 0;
190
0
  }
191
0
  return !br->eof_;
192
0
}
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
0
                                     const uint8_t* buf, size_t size) {
205
0
  VP8BitReader* const br = &dec->br_;
206
0
  const uint8_t* sz = buf;
207
0
  const uint8_t* buf_end = buf + size;
208
0
  const uint8_t* part_start;
209
0
  size_t size_left = size;
210
0
  size_t last_part;
211
0
  size_t p;
212
213
0
  dec->num_parts_minus_one_ = (1 << VP8GetValue(br, 2, "global-header")) - 1;
214
0
  last_part = dec->num_parts_minus_one_;
215
0
  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
0
  part_start = buf + last_part * 3;
220
0
  size_left -= last_part * 3;
221
0
  for (p = 0; p < last_part; ++p) {
222
0
    size_t psize = sz[0] | (sz[1] << 8) | (sz[2] << 16);
223
0
    if (psize > size_left) psize = size_left;
224
0
    VP8InitBitReader(dec->parts_ + p, part_start, psize);
225
0
    part_start += psize;
226
0
    size_left -= psize;
227
0
    sz += 3;
228
0
  }
229
0
  VP8InitBitReader(dec->parts_ + last_part, part_start, size_left);
230
0
  if (part_start < buf_end) return VP8_STATUS_OK;
231
0
  return dec->incremental_
232
0
             ? VP8_STATUS_SUSPENDED  // Init is ok, but there's not enough data
233
0
             : VP8_STATUS_NOT_ENOUGH_DATA;
234
0
}
235
236
// Paragraph 9.4
237
0
static int ParseFilterHeader(VP8BitReader* br, VP8Decoder* const dec) {
238
0
  VP8FilterHeader* const hdr = &dec->filter_hdr_;
239
0
  hdr->simple_    = VP8Get(br, "global-header");
240
0
  hdr->level_     = VP8GetValue(br, 6, "global-header");
241
0
  hdr->sharpness_ = VP8GetValue(br, 3, "global-header");
242
0
  hdr->use_lf_delta_ = VP8Get(br, "global-header");
243
0
  if (hdr->use_lf_delta_) {
244
0
    if (VP8Get(br, "global-header")) {   // update lf-delta?
245
0
      int i;
246
0
      for (i = 0; i < NUM_REF_LF_DELTAS; ++i) {
247
0
        if (VP8Get(br, "global-header")) {
248
0
          hdr->ref_lf_delta_[i] = VP8GetSignedValue(br, 6, "global-header");
249
0
        }
250
0
      }
251
0
      for (i = 0; i < NUM_MODE_LF_DELTAS; ++i) {
252
0
        if (VP8Get(br, "global-header")) {
253
0
          hdr->mode_lf_delta_[i] = VP8GetSignedValue(br, 6, "global-header");
254
0
        }
255
0
      }
256
0
    }
257
0
  }
258
0
  dec->filter_type_ = (hdr->level_ == 0) ? 0 : hdr->simple_ ? 1 : 2;
259
0
  return !br->eof_;
260
0
}
261
262
// Topmost call
263
0
int VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io) {
264
0
  const uint8_t* buf;
265
0
  size_t buf_size;
266
0
  VP8FrameHeader* frm_hdr;
267
0
  VP8PictureHeader* pic_hdr;
268
0
  VP8BitReader* br;
269
0
  VP8StatusCode status;
270
271
0
  if (dec == NULL) {
272
0
    return 0;
273
0
  }
274
0
  SetOk(dec);
275
0
  if (io == NULL) {
276
0
    return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
277
0
                       "null VP8Io passed to VP8GetHeaders()");
278
0
  }
279
0
  buf = io->data;
280
0
  buf_size = io->data_size;
281
0
  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
0
  {
288
0
    const uint32_t bits = buf[0] | (buf[1] << 8) | (buf[2] << 16);
289
0
    frm_hdr = &dec->frm_hdr_;
290
0
    frm_hdr->key_frame_ = !(bits & 1);
291
0
    frm_hdr->profile_ = (bits >> 1) & 7;
292
0
    frm_hdr->show_ = (bits >> 4) & 1;
293
0
    frm_hdr->partition_length_ = (bits >> 5);
294
0
    if (frm_hdr->profile_ > 3) {
295
0
      return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
296
0
                         "Incorrect keyframe parameters.");
297
0
    }
298
0
    if (!frm_hdr->show_) {
299
0
      return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
300
0
                         "Frame not displayable.");
301
0
    }
302
0
    buf += 3;
303
0
    buf_size -= 3;
304
0
  }
305
306
0
  pic_hdr = &dec->pic_hdr_;
307
0
  if (frm_hdr->key_frame_) {
308
    // Paragraph 9.2
309
0
    if (buf_size < 7) {
310
0
      return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
311
0
                         "cannot parse picture header");
312
0
    }
313
0
    if (!VP8CheckSignature(buf, buf_size)) {
314
0
      return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
315
0
                         "Bad code word");
316
0
    }
317
0
    pic_hdr->width_ = ((buf[4] << 8) | buf[3]) & 0x3fff;
318
0
    pic_hdr->xscale_ = buf[4] >> 6;   // ratio: 1, 5/4 5/3 or 2
319
0
    pic_hdr->height_ = ((buf[6] << 8) | buf[5]) & 0x3fff;
320
0
    pic_hdr->yscale_ = buf[6] >> 6;
321
0
    buf += 7;
322
0
    buf_size -= 7;
323
324
0
    dec->mb_w_ = (pic_hdr->width_ + 15) >> 4;
325
0
    dec->mb_h_ = (pic_hdr->height_ + 15) >> 4;
326
327
    // Setup default output area (can be later modified during io->setup())
328
0
    io->width = pic_hdr->width_;
329
0
    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
0
    io->use_cropping = 0;
334
0
    io->crop_top  = 0;
335
0
    io->crop_left = 0;
336
0
    io->crop_right  = io->width;
337
0
    io->crop_bottom = io->height;
338
0
    io->use_scaling  = 0;
339
0
    io->scaled_width = io->width;
340
0
    io->scaled_height = io->height;
341
342
0
    io->mb_w = io->width;   // for soundness
343
0
    io->mb_h = io->height;  // ditto
344
345
0
    VP8ResetProba(&dec->proba_);
346
0
    ResetSegmentHeader(&dec->segment_hdr_);
347
0
  }
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
0
  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
0
  br = &dec->br_;
357
0
  VP8InitBitReader(br, buf, frm_hdr->partition_length_);
358
0
  buf += frm_hdr->partition_length_;
359
0
  buf_size -= frm_hdr->partition_length_;
360
361
0
  if (frm_hdr->key_frame_) {
362
0
    pic_hdr->colorspace_ = VP8Get(br, "global-header");
363
0
    pic_hdr->clamp_type_ = VP8Get(br, "global-header");
364
0
  }
365
0
  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
0
  if (!ParseFilterHeader(br, dec)) {
371
0
    return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
372
0
                       "cannot parse filter header");
373
0
  }
374
0
  status = ParsePartitions(dec, buf, buf_size);
375
0
  if (status != VP8_STATUS_OK) {
376
0
    return VP8SetError(dec, status, "cannot parse partitions");
377
0
  }
378
379
  // quantizer change
380
0
  VP8ParseQuant(dec);
381
382
  // Frame buffer marking
383
0
  if (!frm_hdr->key_frame_) {
384
0
    return VP8SetError(dec, VP8_STATUS_UNSUPPORTED_FEATURE,
385
0
                       "Not a key frame.");
386
0
  }
387
388
0
  VP8Get(br, "global-header");   // ignore the value of update_proba_
389
390
0
  VP8ParseProba(br, dec);
391
392
  // sanitized state
393
0
  dec->ready_ = 1;
394
0
  return 1;
395
0
}
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
0
static int GetLargeValue(VP8BitReader* const br, const uint8_t* const p) {
412
0
  int v;
413
0
  if (!VP8GetBit(br, p[3], "coeffs")) {
414
0
    if (!VP8GetBit(br, p[4], "coeffs")) {
415
0
      v = 2;
416
0
    } else {
417
0
      v = 3 + VP8GetBit(br, p[5], "coeffs");
418
0
    }
419
0
  } else {
420
0
    if (!VP8GetBit(br, p[6], "coeffs")) {
421
0
      if (!VP8GetBit(br, p[7], "coeffs")) {
422
0
        v = 5 + VP8GetBit(br, 159, "coeffs");
423
0
      } else {
424
0
        v = 7 + 2 * VP8GetBit(br, 165, "coeffs");
425
0
        v += VP8GetBit(br, 145, "coeffs");
426
0
      }
427
0
    } else {
428
0
      const uint8_t* tab;
429
0
      const int bit1 = VP8GetBit(br, p[8], "coeffs");
430
0
      const int bit0 = VP8GetBit(br, p[9 + bit1], "coeffs");
431
0
      const int cat = 2 * bit1 + bit0;
432
0
      v = 0;
433
0
      for (tab = kCat3456[cat]; *tab; ++tab) {
434
0
        v += v + VP8GetBit(br, *tab, "coeffs");
435
0
      }
436
0
      v += 3 + (8 << cat);
437
0
    }
438
0
  }
439
0
  return v;
440
0
}
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
0
                         int ctx, const quant_t dq, int n, int16_t* out) {
446
0
  const uint8_t* p = prob[n]->probas_[ctx];
447
0
  for (; n < 16; ++n) {
448
0
    if (!VP8GetBit(br, p[0], "coeffs")) {
449
0
      return n;  // previous coeff was last non-zero coeff
450
0
    }
451
0
    while (!VP8GetBit(br, p[1], "coeffs")) {       // sequence of zero coeffs
452
0
      p = prob[++n]->probas_[0];
453
0
      if (n == 16) return 16;
454
0
    }
455
0
    {        // non zero coeff
456
0
      const VP8ProbaArray* const p_ctx = &prob[n + 1]->probas_[0];
457
0
      int v;
458
0
      if (!VP8GetBit(br, p[2], "coeffs")) {
459
0
        v = 1;
460
0
        p = p_ctx[1];
461
0
      } else {
462
0
        v = GetLargeValue(br, p);
463
0
        p = p_ctx[2];
464
0
      }
465
0
      out[kZigzag[n]] = VP8GetSigned(br, v, "coeffs") * dq[n > 0];
466
0
    }
467
0
  }
468
0
  return 16;
469
0
}
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
0
WEBP_DSP_INIT_FUNC(InitGetCoeffs) {
504
0
  if (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kSlowSSSE3)) {
505
0
    GetCoeffs = GetCoeffsAlt;
506
0
  } else {
507
0
    GetCoeffs = GetCoeffsFast;
508
0
  }
509
0
}
510
511
0
static WEBP_INLINE uint32_t NzCodeBits(uint32_t nz_coeffs, int nz, int dc_nz) {
512
0
  nz_coeffs <<= 2;
513
0
  nz_coeffs |= (nz > 3) ? 3 : (nz > 1) ? 2 : dc_nz;
514
0
  return nz_coeffs;
515
0
}
516
517
static int ParseResiduals(VP8Decoder* const dec,
518
0
                          VP8MB* const mb, VP8BitReader* const token_br) {
519
0
  const VP8BandProbas* (* const bands)[16 + 1] = dec->proba_.bands_ptr_;
520
0
  const VP8BandProbas* const * ac_proba;
521
0
  VP8MBData* const block = dec->mb_data_ + dec->mb_x_;
522
0
  const VP8QuantMatrix* const q = &dec->dqm_[block->segment_];
523
0
  int16_t* dst = block->coeffs_;
524
0
  VP8MB* const left_mb = dec->mb_info_ - 1;
525
0
  uint8_t tnz, lnz;
526
0
  uint32_t non_zero_y = 0;
527
0
  uint32_t non_zero_uv = 0;
528
0
  int x, y, ch;
529
0
  uint32_t out_t_nz, out_l_nz;
530
0
  int first;
531
532
0
  memset(dst, 0, 384 * sizeof(*dst));
533
0
  if (!block->is_i4x4_) {    // parse DC
534
0
    int16_t dc[16] = { 0 };
535
0
    const int ctx = mb->nz_dc_ + left_mb->nz_dc_;
536
0
    const int nz = GetCoeffs(token_br, bands[1], ctx, q->y2_mat_, 0, dc);
537
0
    mb->nz_dc_ = left_mb->nz_dc_ = (nz > 0);
538
0
    if (nz > 1) {   // more than just the DC -> perform the full transform
539
0
      VP8TransformWHT(dc, dst);
540
0
    } else {        // only DC is non-zero -> inlined simplified transform
541
0
      int i;
542
0
      const int dc0 = (dc[0] + 3) >> 3;
543
0
      for (i = 0; i < 16 * 16; i += 16) dst[i] = dc0;
544
0
    }
545
0
    first = 1;
546
0
    ac_proba = bands[0];
547
0
  } else {
548
0
    first = 0;
549
0
    ac_proba = bands[3];
550
0
  }
551
552
0
  tnz = mb->nz_ & 0x0f;
553
0
  lnz = left_mb->nz_ & 0x0f;
554
0
  for (y = 0; y < 4; ++y) {
555
0
    int l = lnz & 1;
556
0
    uint32_t nz_coeffs = 0;
557
0
    for (x = 0; x < 4; ++x) {
558
0
      const int ctx = l + (tnz & 1);
559
0
      const int nz = GetCoeffs(token_br, ac_proba, ctx, q->y1_mat_, first, dst);
560
0
      l = (nz > first);
561
0
      tnz = (tnz >> 1) | (l << 7);
562
0
      nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
563
0
      dst += 16;
564
0
    }
565
0
    tnz >>= 4;
566
0
    lnz = (lnz >> 1) | (l << 7);
567
0
    non_zero_y = (non_zero_y << 8) | nz_coeffs;
568
0
  }
569
0
  out_t_nz = tnz;
570
0
  out_l_nz = lnz >> 4;
571
572
0
  for (ch = 0; ch < 4; ch += 2) {
573
0
    uint32_t nz_coeffs = 0;
574
0
    tnz = mb->nz_ >> (4 + ch);
575
0
    lnz = left_mb->nz_ >> (4 + ch);
576
0
    for (y = 0; y < 2; ++y) {
577
0
      int l = lnz & 1;
578
0
      for (x = 0; x < 2; ++x) {
579
0
        const int ctx = l + (tnz & 1);
580
0
        const int nz = GetCoeffs(token_br, bands[2], ctx, q->uv_mat_, 0, dst);
581
0
        l = (nz > 0);
582
0
        tnz = (tnz >> 1) | (l << 3);
583
0
        nz_coeffs = NzCodeBits(nz_coeffs, nz, dst[0] != 0);
584
0
        dst += 16;
585
0
      }
586
0
      tnz >>= 2;
587
0
      lnz = (lnz >> 1) | (l << 5);
588
0
    }
589
    // Note: we don't really need the per-4x4 details for U/V blocks.
590
0
    non_zero_uv |= nz_coeffs << (4 * ch);
591
0
    out_t_nz |= (tnz << 4) << ch;
592
0
    out_l_nz |= (lnz & 0xf0) << ch;
593
0
  }
594
0
  mb->nz_ = out_t_nz;
595
0
  left_mb->nz_ = out_l_nz;
596
597
0
  block->non_zero_y_ = non_zero_y;
598
0
  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
0
  block->dither_ = (non_zero_uv & 0xaaaa) ? 0 : q->dither_;
604
605
0
  return !(non_zero_y | non_zero_uv);  // will be used for further optimization
606
0
}
607
608
//------------------------------------------------------------------------------
609
// Main loop
610
611
0
int VP8DecodeMB(VP8Decoder* const dec, VP8BitReader* const token_br) {
612
0
  VP8MB* const left = dec->mb_info_ - 1;
613
0
  VP8MB* const mb = dec->mb_info_ + dec->mb_x_;
614
0
  VP8MBData* const block = dec->mb_data_ + dec->mb_x_;
615
0
  int skip = dec->use_skip_proba_ ? block->skip_ : 0;
616
617
0
  if (!skip) {
618
0
    skip = ParseResiduals(dec, mb, token_br);
619
0
  } else {
620
0
    left->nz_ = mb->nz_ = 0;
621
0
    if (!block->is_i4x4_) {
622
0
      left->nz_dc_ = mb->nz_dc_ = 0;
623
0
    }
624
0
    block->non_zero_y_ = 0;
625
0
    block->non_zero_uv_ = 0;
626
0
    block->dither_ = 0;
627
0
  }
628
629
0
  if (dec->filter_type_ > 0) {  // store filter info
630
0
    VP8FInfo* const finfo = dec->f_info_ + dec->mb_x_;
631
0
    *finfo = dec->fstrengths_[block->segment_][block->is_i4x4_];
632
0
    finfo->f_inner_ |= !skip;
633
0
  }
634
635
0
  return !token_br->eof_;
636
0
}
637
638
0
void VP8InitScanline(VP8Decoder* const dec) {
639
0
  VP8MB* const left = dec->mb_info_ - 1;
640
0
  left->nz_ = 0;
641
0
  left->nz_dc_ = 0;
642
0
  memset(dec->intra_l_, B_DC_PRED, sizeof(dec->intra_l_));
643
0
  dec->mb_x_ = 0;
644
0
}
645
646
0
static int ParseFrame(VP8Decoder* const dec, VP8Io* io) {
647
0
  for (dec->mb_y_ = 0; dec->mb_y_ < dec->br_mb_y_; ++dec->mb_y_) {
648
    // Parse bitstream for this row.
649
0
    VP8BitReader* const token_br =
650
0
        &dec->parts_[dec->mb_y_ & dec->num_parts_minus_one_];
651
0
    if (!VP8ParseIntraModeRow(&dec->br_, dec)) {
652
0
      return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
653
0
                         "Premature end-of-partition0 encountered.");
654
0
    }
655
0
    for (; dec->mb_x_ < dec->mb_w_; ++dec->mb_x_) {
656
0
      if (!VP8DecodeMB(dec, token_br)) {
657
0
        return VP8SetError(dec, VP8_STATUS_NOT_ENOUGH_DATA,
658
0
                           "Premature end-of-file encountered.");
659
0
      }
660
0
    }
661
0
    VP8InitScanline(dec);   // Prepare for next scanline
662
663
    // Reconstruct, filter and emit the row.
664
0
    if (!VP8ProcessRow(dec, io)) {
665
0
      return VP8SetError(dec, VP8_STATUS_USER_ABORT, "Output aborted.");
666
0
    }
667
0
  }
668
0
  if (dec->mt_method_ > 0) {
669
0
    if (!WebPGetWorkerInterface()->Sync(&dec->worker_)) return 0;
670
0
  }
671
672
0
  return 1;
673
0
}
674
675
// Main entry point
676
0
int VP8Decode(VP8Decoder* const dec, VP8Io* const io) {
677
0
  int ok = 0;
678
0
  if (dec == NULL) {
679
0
    return 0;
680
0
  }
681
0
  if (io == NULL) {
682
0
    return VP8SetError(dec, VP8_STATUS_INVALID_PARAM,
683
0
                       "NULL VP8Io parameter in VP8Decode().");
684
0
  }
685
686
0
  if (!dec->ready_) {
687
0
    if (!VP8GetHeaders(dec, io)) {
688
0
      return 0;
689
0
    }
690
0
  }
691
0
  assert(dec->ready_);
692
693
  // Finish setting up the decoding parameter. Will call io->setup().
694
0
  ok = (VP8EnterCritical(dec, io) == VP8_STATUS_OK);
695
0
  if (ok) {   // good to go.
696
    // Will allocate memory and prepare everything.
697
0
    if (ok) ok = VP8InitFrame(dec, io);
698
699
    // Main decoding loop
700
0
    if (ok) ok = ParseFrame(dec, io);
701
702
    // Exit.
703
0
    ok &= VP8ExitCritical(dec, io);
704
0
  }
705
706
0
  if (!ok) {
707
0
    VP8Clear(dec);
708
0
    return 0;
709
0
  }
710
711
0
  dec->ready_ = 0;
712
0
  return ok;
713
0
}
714
715
0
void VP8Clear(VP8Decoder* const dec) {
716
0
  if (dec == NULL) {
717
0
    return;
718
0
  }
719
0
  WebPGetWorkerInterface()->End(&dec->worker_);
720
0
  WebPDeallocateAlphaMemory(dec);
721
0
  WebPSafeFree(dec->mem_);
722
0
  dec->mem_ = NULL;
723
0
  dec->mem_size_ = 0;
724
0
  memset(&dec->br_, 0, sizeof(dec->br_));
725
0
  dec->ready_ = 0;
726
0
}
727
728
//------------------------------------------------------------------------------