Coverage Report

Created: 2025-08-28 07:12

/src/libvpx/vp8/decoder/decodeframe.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
 *  that can be found in the LICENSE file in the root of the source
6
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
9
 */
10
11
#include "vpx_config.h"
12
#include "vp8_rtcd.h"
13
#include "./vpx_scale_rtcd.h"
14
#include "onyxd_int.h"
15
#include "vp8/common/header.h"
16
#include "vp8/common/reconintra4x4.h"
17
#include "vp8/common/reconinter.h"
18
#include "detokenize.h"
19
#include "vp8/common/common.h"
20
#include "vp8/common/invtrans.h"
21
#include "vp8/common/alloccommon.h"
22
#include "vp8/common/entropymode.h"
23
#include "vp8/common/quant_common.h"
24
#include "vpx_scale/vpx_scale.h"
25
#include "vp8/common/reconintra.h"
26
#include "vp8/common/setupintrarecon.h"
27
28
#include "decodemv.h"
29
#include "vp8/common/extend.h"
30
#if CONFIG_ERROR_CONCEALMENT
31
#include "error_concealment.h"
32
#endif
33
#include "vpx_mem/vpx_mem.h"
34
#include "vp8/common/threading.h"
35
#include "decoderthreading.h"
36
#include "dboolhuff.h"
37
#include "vpx_dsp/vpx_dsp_common.h"
38
39
#include <assert.h>
40
#include <stdio.h>
41
42
31.7k
void vp8cx_init_de_quantizer(VP8D_COMP *pbi) {
43
31.7k
  int Q;
44
31.7k
  VP8_COMMON *const pc = &pbi->common;
45
46
4.09M
  for (Q = 0; Q < QINDEX_RANGE; ++Q) {
47
4.06M
    pc->Y1dequant[Q][0] = (short)vp8_dc_quant(Q, pc->y1dc_delta_q);
48
4.06M
    pc->Y2dequant[Q][0] = (short)vp8_dc2quant(Q, pc->y2dc_delta_q);
49
4.06M
    pc->UVdequant[Q][0] = (short)vp8_dc_uv_quant(Q, pc->uvdc_delta_q);
50
51
4.06M
    pc->Y1dequant[Q][1] = (short)vp8_ac_yquant(Q);
52
4.06M
    pc->Y2dequant[Q][1] = (short)vp8_ac2quant(Q, pc->y2ac_delta_q);
53
4.06M
    pc->UVdequant[Q][1] = (short)vp8_ac_uv_quant(Q, pc->uvac_delta_q);
54
4.06M
  }
55
31.7k
}
56
57
370M
void vp8_mb_init_dequantizer(VP8D_COMP *pbi, MACROBLOCKD *xd) {
58
370M
  int i;
59
370M
  int QIndex;
60
370M
  MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
61
370M
  VP8_COMMON *const pc = &pbi->common;
62
63
  /* Decide whether to use the default or alternate baseline Q value. */
64
370M
  if (xd->segmentation_enabled) {
65
    /* Abs Value */
66
370M
    if (xd->mb_segment_abs_delta == SEGMENT_ABSDATA) {
67
165M
      QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
68
69
      /* Delta Value */
70
205M
    } else {
71
205M
      QIndex = pc->base_qindex +
72
205M
               xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
73
205M
    }
74
75
370M
    QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ)
76
370M
                           : 0; /* Clamp to valid range */
77
370M
  } else {
78
19.7k
    QIndex = pc->base_qindex;
79
19.7k
  }
80
81
  /* Set up the macroblock dequant constants */
82
370M
  xd->dequant_y1_dc[0] = 1;
83
370M
  xd->dequant_y1[0] = pc->Y1dequant[QIndex][0];
84
370M
  xd->dequant_y2[0] = pc->Y2dequant[QIndex][0];
85
370M
  xd->dequant_uv[0] = pc->UVdequant[QIndex][0];
86
87
5.93G
  for (i = 1; i < 16; ++i) {
88
5.56G
    xd->dequant_y1_dc[i] = xd->dequant_y1[i] = pc->Y1dequant[QIndex][1];
89
5.56G
    xd->dequant_y2[i] = pc->Y2dequant[QIndex][1];
90
5.56G
    xd->dequant_uv[i] = pc->UVdequant[QIndex][1];
91
5.56G
  }
92
370M
}
93
94
static void decode_macroblock(VP8D_COMP *pbi, MACROBLOCKD *xd,
95
783M
                              unsigned int mb_idx) {
96
783M
  MB_PREDICTION_MODE mode;
97
783M
  int i;
98
#if CONFIG_ERROR_CONCEALMENT
99
  int corruption_detected = 0;
100
#else
101
783M
  (void)mb_idx;
102
783M
#endif
103
104
783M
  if (xd->mode_info_context->mbmi.mb_skip_coeff) {
105
9.64M
    vp8_reset_mb_tokens_context(xd);
106
773M
  } else if (!vp8dx_bool_error(xd->current_bc)) {
107
2.62M
    int eobtotal;
108
2.62M
    eobtotal = vp8_decode_mb_tokens(pbi, xd);
109
110
    /* Special case:  Force the loopfilter to skip when eobtotal is zero */
111
2.62M
    xd->mode_info_context->mbmi.mb_skip_coeff = (eobtotal == 0);
112
2.62M
  }
113
114
783M
  mode = xd->mode_info_context->mbmi.mode;
115
116
783M
  if (xd->segmentation_enabled) vp8_mb_init_dequantizer(pbi, xd);
117
118
#if CONFIG_ERROR_CONCEALMENT
119
120
  if (pbi->ec_active) {
121
    int throw_residual;
122
    /* When we have independent partitions we can apply residual even
123
     * though other partitions within the frame are corrupt.
124
     */
125
    throw_residual =
126
        (!pbi->independent_partitions && pbi->frame_corrupt_residual);
127
    throw_residual = (throw_residual || vp8dx_bool_error(xd->current_bc));
128
129
    if ((mb_idx >= pbi->mvs_corrupt_from_mb || throw_residual)) {
130
      /* MB with corrupt residuals or corrupt mode/motion vectors.
131
       * Better to use the predictor as reconstruction.
132
       */
133
      pbi->frame_corrupt_residual = 1;
134
      memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
135
136
      corruption_detected = 1;
137
138
      /* force idct to be skipped for B_PRED and use the
139
       * prediction only for reconstruction
140
       * */
141
      memset(xd->eobs, 0, 25);
142
    }
143
  }
144
#endif
145
146
  /* do prediction */
147
783M
  if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME) {
148
779M
    vp8_build_intra_predictors_mbuv_s(
149
779M
        xd, xd->recon_above[1], xd->recon_above[2], xd->recon_left[1],
150
779M
        xd->recon_left[2], xd->recon_left_stride[1], xd->dst.u_buffer,
151
779M
        xd->dst.v_buffer, xd->dst.uv_stride);
152
153
779M
    if (mode != B_PRED) {
154
173M
      vp8_build_intra_predictors_mby_s(
155
173M
          xd, xd->recon_above[0], xd->recon_left[0], xd->recon_left_stride[0],
156
173M
          xd->dst.y_buffer, xd->dst.y_stride);
157
605M
    } else {
158
605M
      short *DQC = xd->dequant_y1;
159
605M
      int dst_stride = xd->dst.y_stride;
160
161
      /* clear out residual eob info */
162
605M
      if (xd->mode_info_context->mbmi.mb_skip_coeff) memset(xd->eobs, 0, 25);
163
164
605M
      intra_prediction_down_copy(xd, xd->recon_above[0] + 16);
165
166
10.2G
      for (i = 0; i < 16; ++i) {
167
9.68G
        BLOCKD *b = &xd->block[i];
168
9.68G
        unsigned char *dst = xd->dst.y_buffer + b->offset;
169
9.68G
        B_PREDICTION_MODE b_mode = xd->mode_info_context->bmi[i].as_mode;
170
9.68G
        unsigned char *Above = dst - dst_stride;
171
9.68G
        unsigned char *yleft = dst - 1;
172
9.68G
        int left_stride = dst_stride;
173
9.68G
        unsigned char top_left = Above[-1];
174
175
9.68G
        vp8_intra4x4_predict(Above, yleft, left_stride, b_mode, dst, dst_stride,
176
9.68G
                             top_left);
177
178
9.68G
        if (xd->eobs[i]) {
179
3.52G
          if (xd->eobs[i] > 1) {
180
1.97G
            vp8_dequant_idct_add(b->qcoeff, DQC, dst, dst_stride);
181
1.97G
          } else {
182
1.54G
            vp8_dc_only_idct_add(b->qcoeff[0] * DQC[0], dst, dst_stride, dst,
183
1.54G
                                 dst_stride);
184
1.54G
            memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0]));
185
1.54G
          }
186
3.52G
        }
187
9.68G
      }
188
605M
    }
189
779M
  } else {
190
4.01M
    vp8_build_inter_predictors_mb(xd);
191
4.01M
  }
192
193
#if CONFIG_ERROR_CONCEALMENT
194
  if (corruption_detected) {
195
    return;
196
  }
197
#endif
198
199
783M
  if (!xd->mode_info_context->mbmi.mb_skip_coeff) {
200
    /* dequantization and idct */
201
772M
    if (mode != B_PRED) {
202
168M
      short *DQC = xd->dequant_y1;
203
204
168M
      if (mode != SPLITMV) {
205
167M
        BLOCKD *b = &xd->block[24];
206
207
        /* do 2nd order transform on the dc block */
208
167M
        if (xd->eobs[24] > 1) {
209
27.8M
          vp8_dequantize_b(b, xd->dequant_y2);
210
211
27.8M
          vp8_short_inv_walsh4x4(&b->dqcoeff[0], xd->qcoeff);
212
27.8M
          memset(b->qcoeff, 0, 16 * sizeof(b->qcoeff[0]));
213
139M
        } else {
214
139M
          b->dqcoeff[0] = (short)(b->qcoeff[0] * xd->dequant_y2[0]);
215
139M
          vp8_short_inv_walsh4x4_1(&b->dqcoeff[0], xd->qcoeff);
216
139M
          memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0]));
217
139M
        }
218
219
        /* override the dc dequant constant in order to preserve the
220
         * dc components
221
         */
222
167M
        DQC = xd->dequant_y1_dc;
223
167M
      }
224
225
168M
      vp8_dequant_idct_add_y_block(xd->qcoeff, DQC, xd->dst.y_buffer,
226
168M
                                   xd->dst.y_stride, xd->eobs);
227
168M
    }
228
229
772M
    vp8_dequant_idct_add_uv_block(xd->qcoeff + 16 * 16, xd->dequant_uv,
230
772M
                                  xd->dst.u_buffer, xd->dst.v_buffer,
231
772M
                                  xd->dst.uv_stride, xd->eobs + 16);
232
772M
  }
233
783M
}
234
235
162k
static int get_delta_q(vp8_reader *bc, int prev, int *q_update) {
236
162k
  int ret_val = 0;
237
238
162k
  if (vp8_read_bit(bc)) {
239
88.5k
    ret_val = vp8_read_literal(bc, 4);
240
241
88.5k
    if (vp8_read_bit(bc)) ret_val = -ret_val;
242
88.5k
  }
243
244
  /* Trigger a quantizer update if the delta-q value has changed */
245
162k
  if (ret_val != prev) *q_update = 1;
246
247
162k
  return ret_val;
248
162k
}
249
250
#ifdef PACKET_TESTING
251
#include <stdio.h>
252
FILE *vpxlog = 0;
253
#endif
254
255
32.4k
static void yv12_extend_frame_top_c(YV12_BUFFER_CONFIG *ybf) {
256
32.4k
  int i;
257
32.4k
  unsigned char *src_ptr1;
258
32.4k
  unsigned char *dest_ptr1;
259
260
32.4k
  unsigned int Border;
261
32.4k
  int plane_stride;
262
263
  /***********/
264
  /* Y Plane */
265
  /***********/
266
32.4k
  Border = ybf->border;
267
32.4k
  plane_stride = ybf->y_stride;
268
32.4k
  src_ptr1 = ybf->y_buffer - Border;
269
32.4k
  dest_ptr1 = src_ptr1 - (Border * plane_stride);
270
271
1.07M
  for (i = 0; i < (int)Border; ++i) {
272
1.03M
    memcpy(dest_ptr1, src_ptr1, plane_stride);
273
1.03M
    dest_ptr1 += plane_stride;
274
1.03M
  }
275
276
  /***********/
277
  /* U Plane */
278
  /***********/
279
32.4k
  plane_stride = ybf->uv_stride;
280
32.4k
  Border /= 2;
281
32.4k
  src_ptr1 = ybf->u_buffer - Border;
282
32.4k
  dest_ptr1 = src_ptr1 - (Border * plane_stride);
283
284
551k
  for (i = 0; i < (int)(Border); ++i) {
285
518k
    memcpy(dest_ptr1, src_ptr1, plane_stride);
286
518k
    dest_ptr1 += plane_stride;
287
518k
  }
288
289
  /***********/
290
  /* V Plane */
291
  /***********/
292
293
32.4k
  src_ptr1 = ybf->v_buffer - Border;
294
32.4k
  dest_ptr1 = src_ptr1 - (Border * plane_stride);
295
296
551k
  for (i = 0; i < (int)(Border); ++i) {
297
518k
    memcpy(dest_ptr1, src_ptr1, plane_stride);
298
518k
    dest_ptr1 += plane_stride;
299
518k
  }
300
32.4k
}
301
302
32.4k
static void yv12_extend_frame_bottom_c(YV12_BUFFER_CONFIG *ybf) {
303
32.4k
  int i;
304
32.4k
  unsigned char *src_ptr1, *src_ptr2;
305
32.4k
  unsigned char *dest_ptr2;
306
307
32.4k
  unsigned int Border;
308
32.4k
  int plane_stride;
309
32.4k
  int plane_height;
310
311
  /***********/
312
  /* Y Plane */
313
  /***********/
314
32.4k
  Border = ybf->border;
315
32.4k
  plane_stride = ybf->y_stride;
316
32.4k
  plane_height = ybf->y_height;
317
318
32.4k
  src_ptr1 = ybf->y_buffer - Border;
319
32.4k
  src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
320
32.4k
  dest_ptr2 = src_ptr2 + plane_stride;
321
322
1.07M
  for (i = 0; i < (int)Border; ++i) {
323
1.03M
    memcpy(dest_ptr2, src_ptr2, plane_stride);
324
1.03M
    dest_ptr2 += plane_stride;
325
1.03M
  }
326
327
  /***********/
328
  /* U Plane */
329
  /***********/
330
32.4k
  plane_stride = ybf->uv_stride;
331
32.4k
  plane_height = ybf->uv_height;
332
32.4k
  Border /= 2;
333
334
32.4k
  src_ptr1 = ybf->u_buffer - Border;
335
32.4k
  src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
336
32.4k
  dest_ptr2 = src_ptr2 + plane_stride;
337
338
551k
  for (i = 0; i < (int)(Border); ++i) {
339
518k
    memcpy(dest_ptr2, src_ptr2, plane_stride);
340
518k
    dest_ptr2 += plane_stride;
341
518k
  }
342
343
  /***********/
344
  /* V Plane */
345
  /***********/
346
347
32.4k
  src_ptr1 = ybf->v_buffer - Border;
348
32.4k
  src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
349
32.4k
  dest_ptr2 = src_ptr2 + plane_stride;
350
351
551k
  for (i = 0; i < (int)(Border); ++i) {
352
518k
    memcpy(dest_ptr2, src_ptr2, plane_stride);
353
518k
    dest_ptr2 += plane_stride;
354
518k
  }
355
32.4k
}
356
357
static void yv12_extend_frame_left_right_c(YV12_BUFFER_CONFIG *ybf,
358
                                           unsigned char *y_src,
359
                                           unsigned char *u_src,
360
2.58M
                                           unsigned char *v_src) {
361
2.58M
  int i;
362
2.58M
  unsigned char *src_ptr1, *src_ptr2;
363
2.58M
  unsigned char *dest_ptr1, *dest_ptr2;
364
365
2.58M
  unsigned int Border;
366
2.58M
  int plane_stride;
367
2.58M
  int plane_height;
368
2.58M
  int plane_width;
369
370
  /***********/
371
  /* Y Plane */
372
  /***********/
373
2.58M
  Border = ybf->border;
374
2.58M
  plane_stride = ybf->y_stride;
375
2.58M
  plane_height = 16;
376
2.58M
  plane_width = ybf->y_width;
377
378
  /* copy the left and right most columns out */
379
2.58M
  src_ptr1 = y_src;
380
2.58M
  src_ptr2 = src_ptr1 + plane_width - 1;
381
2.58M
  dest_ptr1 = src_ptr1 - Border;
382
2.58M
  dest_ptr2 = src_ptr2 + 1;
383
384
43.9M
  for (i = 0; i < plane_height; ++i) {
385
41.3M
    memset(dest_ptr1, src_ptr1[0], Border);
386
41.3M
    memset(dest_ptr2, src_ptr2[0], Border);
387
41.3M
    src_ptr1 += plane_stride;
388
41.3M
    src_ptr2 += plane_stride;
389
41.3M
    dest_ptr1 += plane_stride;
390
41.3M
    dest_ptr2 += plane_stride;
391
41.3M
  }
392
393
  /***********/
394
  /* U Plane */
395
  /***********/
396
2.58M
  plane_stride = ybf->uv_stride;
397
2.58M
  plane_height = 8;
398
2.58M
  plane_width = ybf->uv_width;
399
2.58M
  Border /= 2;
400
401
  /* copy the left and right most columns out */
402
2.58M
  src_ptr1 = u_src;
403
2.58M
  src_ptr2 = src_ptr1 + plane_width - 1;
404
2.58M
  dest_ptr1 = src_ptr1 - Border;
405
2.58M
  dest_ptr2 = src_ptr2 + 1;
406
407
23.2M
  for (i = 0; i < plane_height; ++i) {
408
20.6M
    memset(dest_ptr1, src_ptr1[0], Border);
409
20.6M
    memset(dest_ptr2, src_ptr2[0], Border);
410
20.6M
    src_ptr1 += plane_stride;
411
20.6M
    src_ptr2 += plane_stride;
412
20.6M
    dest_ptr1 += plane_stride;
413
20.6M
    dest_ptr2 += plane_stride;
414
20.6M
  }
415
416
  /***********/
417
  /* V Plane */
418
  /***********/
419
420
  /* copy the left and right most columns out */
421
2.58M
  src_ptr1 = v_src;
422
2.58M
  src_ptr2 = src_ptr1 + plane_width - 1;
423
2.58M
  dest_ptr1 = src_ptr1 - Border;
424
2.58M
  dest_ptr2 = src_ptr2 + 1;
425
426
23.2M
  for (i = 0; i < plane_height; ++i) {
427
20.6M
    memset(dest_ptr1, src_ptr1[0], Border);
428
20.6M
    memset(dest_ptr2, src_ptr2[0], Border);
429
20.6M
    src_ptr1 += plane_stride;
430
20.6M
    src_ptr2 += plane_stride;
431
20.6M
    dest_ptr1 += plane_stride;
432
20.6M
    dest_ptr2 += plane_stride;
433
20.6M
  }
434
2.58M
}
435
436
32.4k
static void decode_mb_rows(VP8D_COMP *pbi) {
437
32.4k
  VP8_COMMON *const pc = &pbi->common;
438
32.4k
  MACROBLOCKD *const xd = &pbi->mb;
439
440
32.4k
  MODE_INFO *lf_mic = xd->mode_info_context;
441
442
32.4k
  int ibc = 0;
443
32.4k
  int num_part = 1 << pc->multi_token_partition;
444
445
32.4k
  int recon_yoffset, recon_uvoffset;
446
32.4k
  int mb_row, mb_col;
447
32.4k
  int mb_idx = 0;
448
449
32.4k
  YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME];
450
451
32.4k
  int recon_y_stride = yv12_fb_new->y_stride;
452
32.4k
  int recon_uv_stride = yv12_fb_new->uv_stride;
453
454
32.4k
  unsigned char *ref_buffer[MAX_REF_FRAMES][3];
455
32.4k
  unsigned char *dst_buffer[3];
456
32.4k
  unsigned char *lf_dst[3];
457
32.4k
  unsigned char *eb_dst[3];
458
32.4k
  int i;
459
32.4k
  int ref_fb_corrupted[MAX_REF_FRAMES];
460
461
32.4k
  ref_fb_corrupted[INTRA_FRAME] = 0;
462
463
129k
  for (i = 1; i < MAX_REF_FRAMES; ++i) {
464
97.3k
    YV12_BUFFER_CONFIG *this_fb = pbi->dec_fb_ref[i];
465
466
97.3k
    ref_buffer[i][0] = this_fb->y_buffer;
467
97.3k
    ref_buffer[i][1] = this_fb->u_buffer;
468
97.3k
    ref_buffer[i][2] = this_fb->v_buffer;
469
470
97.3k
    ref_fb_corrupted[i] = this_fb->corrupted;
471
97.3k
  }
472
473
  /* Set up the buffer pointers */
474
32.4k
  eb_dst[0] = lf_dst[0] = dst_buffer[0] = yv12_fb_new->y_buffer;
475
32.4k
  eb_dst[1] = lf_dst[1] = dst_buffer[1] = yv12_fb_new->u_buffer;
476
32.4k
  eb_dst[2] = lf_dst[2] = dst_buffer[2] = yv12_fb_new->v_buffer;
477
478
32.4k
  xd->up_available = 0;
479
480
  /* Initialize the loop filter for this frame. */
481
32.4k
  if (pc->filter_level) vp8_loop_filter_frame_init(pc, xd, pc->filter_level);
482
483
32.4k
  vp8_setup_intra_recon_top_line(yv12_fb_new);
484
485
  /* Decode the individual macro block */
486
2.61M
  for (mb_row = 0; mb_row < pc->mb_rows; ++mb_row) {
487
2.58M
    if (num_part > 1) {
488
60.0k
      xd->current_bc = &pbi->mbc[ibc];
489
60.0k
      ibc++;
490
491
60.0k
      if (ibc == num_part) ibc = 0;
492
60.0k
    }
493
494
2.58M
    recon_yoffset = mb_row * recon_y_stride * 16;
495
2.58M
    recon_uvoffset = mb_row * recon_uv_stride * 8;
496
497
    /* reset contexts */
498
2.58M
    xd->above_context = pc->above_context;
499
2.58M
    memset(xd->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
500
501
2.58M
    xd->left_available = 0;
502
503
2.58M
    xd->mb_to_top_edge = -((mb_row * 16) << 3);
504
2.58M
    xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
505
506
2.58M
    xd->recon_above[0] = dst_buffer[0] + recon_yoffset;
507
2.58M
    xd->recon_above[1] = dst_buffer[1] + recon_uvoffset;
508
2.58M
    xd->recon_above[2] = dst_buffer[2] + recon_uvoffset;
509
510
2.58M
    xd->recon_left[0] = xd->recon_above[0] - 1;
511
2.58M
    xd->recon_left[1] = xd->recon_above[1] - 1;
512
2.58M
    xd->recon_left[2] = xd->recon_above[2] - 1;
513
514
2.58M
    xd->recon_above[0] -= xd->dst.y_stride;
515
2.58M
    xd->recon_above[1] -= xd->dst.uv_stride;
516
2.58M
    xd->recon_above[2] -= xd->dst.uv_stride;
517
518
    /* TODO: move to outside row loop */
519
2.58M
    xd->recon_left_stride[0] = xd->dst.y_stride;
520
2.58M
    xd->recon_left_stride[1] = xd->dst.uv_stride;
521
522
2.58M
    setup_intra_recon_left(xd->recon_left[0], xd->recon_left[1],
523
2.58M
                           xd->recon_left[2], xd->dst.y_stride,
524
2.58M
                           xd->dst.uv_stride);
525
526
785M
    for (mb_col = 0; mb_col < pc->mb_cols; ++mb_col) {
527
      /* Distance of Mb to the various image edges.
528
       * These are specified to 8th pel as they are always compared to values
529
       * that are in 1/8th pel units
530
       */
531
783M
      xd->mb_to_left_edge = -((mb_col * 16) << 3);
532
783M
      xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
533
534
#if CONFIG_ERROR_CONCEALMENT
535
      {
536
        int corrupt_residual =
537
            (!pbi->independent_partitions && pbi->frame_corrupt_residual) ||
538
            vp8dx_bool_error(xd->current_bc);
539
        if (pbi->ec_active &&
540
            xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME &&
541
            corrupt_residual) {
542
          /* We have an intra block with corrupt coefficients, better to
543
           * conceal with an inter block. Interpolate MVs from neighboring
544
           * MBs.
545
           *
546
           * Note that for the first mb with corrupt residual in a frame,
547
           * we might not discover that before decoding the residual. That
548
           * happens after this check, and therefore no inter concealment
549
           * will be done.
550
           */
551
          vp8_interpolate_motion(xd, mb_row, mb_col, pc->mb_rows, pc->mb_cols);
552
        }
553
      }
554
#endif
555
556
783M
      xd->dst.y_buffer = dst_buffer[0] + recon_yoffset;
557
783M
      xd->dst.u_buffer = dst_buffer[1] + recon_uvoffset;
558
783M
      xd->dst.v_buffer = dst_buffer[2] + recon_uvoffset;
559
560
783M
      if (xd->mode_info_context->mbmi.ref_frame >= LAST_FRAME) {
561
4.01M
        const MV_REFERENCE_FRAME ref = xd->mode_info_context->mbmi.ref_frame;
562
4.01M
        xd->pre.y_buffer = ref_buffer[ref][0] + recon_yoffset;
563
4.01M
        xd->pre.u_buffer = ref_buffer[ref][1] + recon_uvoffset;
564
4.01M
        xd->pre.v_buffer = ref_buffer[ref][2] + recon_uvoffset;
565
779M
      } else {
566
        // ref_frame is INTRA_FRAME, pre buffer should not be used.
567
779M
        xd->pre.y_buffer = 0;
568
779M
        xd->pre.u_buffer = 0;
569
779M
        xd->pre.v_buffer = 0;
570
779M
      }
571
572
      /* propagate errors from reference frames */
573
783M
      xd->corrupted |= ref_fb_corrupted[xd->mode_info_context->mbmi.ref_frame];
574
575
783M
      decode_macroblock(pbi, xd, mb_idx);
576
577
783M
      mb_idx++;
578
783M
      xd->left_available = 1;
579
580
      /* check if the boolean decoder has suffered an error */
581
783M
      xd->corrupted |= vp8dx_bool_error(xd->current_bc);
582
583
783M
      xd->recon_above[0] += 16;
584
783M
      xd->recon_above[1] += 8;
585
783M
      xd->recon_above[2] += 8;
586
783M
      xd->recon_left[0] += 16;
587
783M
      xd->recon_left[1] += 8;
588
783M
      xd->recon_left[2] += 8;
589
590
783M
      recon_yoffset += 16;
591
783M
      recon_uvoffset += 8;
592
593
783M
      ++xd->mode_info_context; /* next mb */
594
595
783M
      xd->above_context++;
596
783M
    }
597
598
    /* adjust to the next row of mbs */
599
2.58M
    vp8_extend_mb_row(yv12_fb_new, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8,
600
2.58M
                      xd->dst.v_buffer + 8);
601
602
2.58M
    ++xd->mode_info_context; /* skip prediction column */
603
2.58M
    xd->up_available = 1;
604
605
2.58M
    if (pc->filter_level) {
606
1.68M
      if (mb_row > 0) {
607
1.66M
        if (pc->filter_type == NORMAL_LOOPFILTER) {
608
1.03M
          vp8_loop_filter_row_normal(pc, lf_mic, mb_row - 1, recon_y_stride,
609
1.03M
                                     recon_uv_stride, lf_dst[0], lf_dst[1],
610
1.03M
                                     lf_dst[2]);
611
1.03M
        } else {
612
625k
          vp8_loop_filter_row_simple(pc, lf_mic, mb_row - 1, recon_y_stride,
613
625k
                                     lf_dst[0]);
614
625k
        }
615
1.66M
        if (mb_row > 1) {
616
1.64M
          yv12_extend_frame_left_right_c(yv12_fb_new, eb_dst[0], eb_dst[1],
617
1.64M
                                         eb_dst[2]);
618
619
1.64M
          eb_dst[0] += recon_y_stride * 16;
620
1.64M
          eb_dst[1] += recon_uv_stride * 8;
621
1.64M
          eb_dst[2] += recon_uv_stride * 8;
622
1.64M
        }
623
624
1.66M
        lf_dst[0] += recon_y_stride * 16;
625
1.66M
        lf_dst[1] += recon_uv_stride * 8;
626
1.66M
        lf_dst[2] += recon_uv_stride * 8;
627
1.66M
        lf_mic += pc->mb_cols;
628
1.66M
        lf_mic++; /* Skip border mb */
629
1.66M
      }
630
1.68M
    } else {
631
896k
      if (mb_row > 0) {
632
        /**/
633
886k
        yv12_extend_frame_left_right_c(yv12_fb_new, eb_dst[0], eb_dst[1],
634
886k
                                       eb_dst[2]);
635
886k
        eb_dst[0] += recon_y_stride * 16;
636
886k
        eb_dst[1] += recon_uv_stride * 8;
637
886k
        eb_dst[2] += recon_uv_stride * 8;
638
886k
      }
639
896k
    }
640
2.58M
  }
641
642
32.4k
  if (pc->filter_level) {
643
22.5k
    if (pc->filter_type == NORMAL_LOOPFILTER) {
644
13.5k
      vp8_loop_filter_row_normal(pc, lf_mic, mb_row - 1, recon_y_stride,
645
13.5k
                                 recon_uv_stride, lf_dst[0], lf_dst[1],
646
13.5k
                                 lf_dst[2]);
647
13.5k
    } else {
648
8.98k
      vp8_loop_filter_row_simple(pc, lf_mic, mb_row - 1, recon_y_stride,
649
8.98k
                                 lf_dst[0]);
650
8.98k
    }
651
652
22.5k
    yv12_extend_frame_left_right_c(yv12_fb_new, eb_dst[0], eb_dst[1],
653
22.5k
                                   eb_dst[2]);
654
22.5k
    eb_dst[0] += recon_y_stride * 16;
655
22.5k
    eb_dst[1] += recon_uv_stride * 8;
656
22.5k
    eb_dst[2] += recon_uv_stride * 8;
657
22.5k
  }
658
32.4k
  yv12_extend_frame_left_right_c(yv12_fb_new, eb_dst[0], eb_dst[1], eb_dst[2]);
659
32.4k
  yv12_extend_frame_top_c(yv12_fb_new);
660
32.4k
  yv12_extend_frame_bottom_c(yv12_fb_new);
661
32.4k
}
662
663
static unsigned int read_partition_size(VP8D_COMP *pbi,
664
1.89k
                                        const unsigned char *cx_size) {
665
1.89k
  unsigned char temp[3];
666
1.89k
  if (pbi->decrypt_cb) {
667
0
    pbi->decrypt_cb(pbi->decrypt_state, cx_size, temp, 3);
668
0
    cx_size = temp;
669
0
  }
670
1.89k
  return cx_size[0] + (cx_size[1] << 8) + (cx_size[2] << 16);
671
1.89k
}
672
673
static int read_is_valid(const unsigned char *start, size_t len,
674
35.8k
                         const unsigned char *end) {
675
35.8k
  return len != 0 && end > start && len <= (size_t)(end - start);
676
35.8k
}
677
678
static unsigned int read_available_partition_size(
679
    VP8D_COMP *pbi, const unsigned char *token_part_sizes,
680
    const unsigned char *fragment_start,
681
    const unsigned char *first_fragment_end, const unsigned char *fragment_end,
682
33.9k
    int i, int num_part) {
683
33.9k
  VP8_COMMON *pc = &pbi->common;
684
33.9k
  const unsigned char *partition_size_ptr = token_part_sizes + i * 3;
685
33.9k
  unsigned int partition_size = 0;
686
33.9k
  ptrdiff_t bytes_left = fragment_end - fragment_start;
687
33.9k
  if (bytes_left < 0) {
688
0
    vpx_internal_error(
689
0
        &pc->error, VPX_CODEC_CORRUPT_FRAME,
690
0
        "Truncated packet or corrupt partition. No bytes left %d.",
691
0
        (int)bytes_left);
692
0
  }
693
  /* Calculate the length of this partition. The last partition
694
   * size is implicit. If the partition size can't be read, then
695
   * either use the remaining data in the buffer (for EC mode)
696
   * or throw an error.
697
   */
698
33.9k
  if (i < num_part - 1) {
699
1.89k
    if (read_is_valid(partition_size_ptr, 3, first_fragment_end)) {
700
1.89k
      partition_size = read_partition_size(pbi, partition_size_ptr);
701
1.89k
    } else if (pbi->ec_active) {
702
0
      partition_size = (unsigned int)bytes_left;
703
0
    } else {
704
0
      vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
705
0
                         "Truncated partition size data");
706
0
    }
707
32.0k
  } else {
708
32.0k
    partition_size = (unsigned int)bytes_left;
709
32.0k
  }
710
711
  /* Validate the calculated partition length. If the buffer
712
   * described by the partition can't be fully read, then restrict
713
   * it to the portion that can be (for EC mode) or throw an error.
714
   */
715
33.9k
  if (!read_is_valid(fragment_start, partition_size, fragment_end)) {
716
1.75k
    if (pbi->ec_active) {
717
0
      partition_size = (unsigned int)bytes_left;
718
1.75k
    } else {
719
1.75k
      vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
720
1.75k
                         "Truncated packet or corrupt partition "
721
1.75k
                         "%d length",
722
1.75k
                         i + 1);
723
1.75k
    }
724
1.75k
  }
725
33.9k
  return partition_size;
726
33.9k
}
727
728
static void setup_token_decoder(VP8D_COMP *pbi,
729
34.7k
                                const unsigned char *token_part_sizes) {
730
34.7k
  vp8_reader *bool_decoder = &pbi->mbc[0];
731
34.7k
  unsigned int partition_idx;
732
34.7k
  unsigned int fragment_idx;
733
34.7k
  unsigned int num_token_partitions;
734
34.7k
  const unsigned char *first_fragment_end =
735
34.7k
      pbi->fragments.ptrs[0] + pbi->fragments.sizes[0];
736
737
34.7k
  TOKEN_PARTITION multi_token_partition =
738
34.7k
      (TOKEN_PARTITION)vp8_read_literal(&pbi->mbc[8], 2);
739
34.7k
  if (!vp8dx_bool_error(&pbi->mbc[8])) {
740
34.0k
    pbi->common.multi_token_partition = multi_token_partition;
741
34.0k
  }
742
34.7k
  num_token_partitions = 1 << pbi->common.multi_token_partition;
743
744
  /* Check for partitions within the fragments and unpack the fragments
745
   * so that each fragment pointer points to its corresponding partition. */
746
69.5k
  for (fragment_idx = 0; fragment_idx < pbi->fragments.count; ++fragment_idx) {
747
34.7k
    unsigned int fragment_size = pbi->fragments.sizes[fragment_idx];
748
34.7k
    const unsigned char *fragment_end =
749
34.7k
        pbi->fragments.ptrs[fragment_idx] + fragment_size;
750
    /* Special case for handling the first partition since we have already
751
     * read its size. */
752
34.7k
    if (fragment_idx == 0) {
753
      /* Size of first partition + token partition sizes element */
754
34.7k
      ptrdiff_t ext_first_part_size = token_part_sizes -
755
34.7k
                                      pbi->fragments.ptrs[0] +
756
34.7k
                                      3 * (num_token_partitions - 1);
757
34.7k
      if (fragment_size < (unsigned int)ext_first_part_size)
758
594
        vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME,
759
594
                           "Corrupted fragment size %d", fragment_size);
760
34.7k
      fragment_size -= (unsigned int)ext_first_part_size;
761
34.7k
      if (fragment_size > 0) {
762
33.8k
        pbi->fragments.sizes[0] = (unsigned int)ext_first_part_size;
763
        /* The fragment contains an additional partition. Move to
764
         * next. */
765
33.8k
        fragment_idx++;
766
33.8k
        pbi->fragments.ptrs[fragment_idx] =
767
33.8k
            pbi->fragments.ptrs[0] + pbi->fragments.sizes[0];
768
33.8k
      }
769
34.7k
    }
770
    /* Split the chunk into partitions read from the bitstream */
771
68.7k
    while (fragment_size > 0) {
772
33.9k
      ptrdiff_t partition_size = read_available_partition_size(
773
33.9k
          pbi, token_part_sizes, pbi->fragments.ptrs[fragment_idx],
774
33.9k
          first_fragment_end, fragment_end, fragment_idx - 1,
775
33.9k
          num_token_partitions);
776
33.9k
      pbi->fragments.sizes[fragment_idx] = (unsigned int)partition_size;
777
33.9k
      if (fragment_size < (unsigned int)partition_size)
778
0
        vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME,
779
0
                           "Corrupted fragment size %d", fragment_size);
780
33.9k
      fragment_size -= (unsigned int)partition_size;
781
33.9k
      assert(fragment_idx <= num_token_partitions);
782
33.9k
      if (fragment_size > 0) {
783
        /* The fragment contains an additional partition.
784
         * Move to next. */
785
136
        fragment_idx++;
786
136
        pbi->fragments.ptrs[fragment_idx] =
787
136
            pbi->fragments.ptrs[fragment_idx - 1] + partition_size;
788
136
      }
789
33.9k
    }
790
34.7k
  }
791
792
34.7k
  pbi->fragments.count = num_token_partitions + 1;
793
794
67.6k
  for (partition_idx = 1; partition_idx < pbi->fragments.count;
795
34.7k
       ++partition_idx) {
796
32.8k
    if (vp8dx_start_decode(bool_decoder, pbi->fragments.ptrs[partition_idx],
797
32.8k
                           pbi->fragments.sizes[partition_idx], pbi->decrypt_cb,
798
32.8k
                           pbi->decrypt_state)) {
799
0
      vpx_internal_error(&pbi->common.error, VPX_CODEC_MEM_ERROR,
800
0
                         "Failed to allocate bool decoder %d", partition_idx);
801
0
    }
802
803
32.8k
    bool_decoder++;
804
32.8k
  }
805
806
34.7k
#if CONFIG_MULTITHREAD
807
  /* Clamp number of decoder threads */
808
34.7k
  if (pbi->decoding_thread_count > num_token_partitions - 1) {
809
0
    pbi->decoding_thread_count = num_token_partitions - 1;
810
0
  }
811
34.7k
  if ((int)pbi->decoding_thread_count > pbi->common.mb_rows - 1) {
812
0
    assert(pbi->common.mb_rows > 0);
813
0
    pbi->decoding_thread_count = pbi->common.mb_rows - 1;
814
0
  }
815
34.7k
#endif
816
34.7k
}
817
818
34.7k
static void init_frame(VP8D_COMP *pbi) {
819
34.7k
  VP8_COMMON *const pc = &pbi->common;
820
34.7k
  MACROBLOCKD *const xd = &pbi->mb;
821
822
34.7k
  if (pc->frame_type == KEY_FRAME) {
823
    /* Various keyframe initializations */
824
6.93k
    memcpy(pc->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
825
826
6.93k
    vp8_init_mbmode_probs(pc);
827
828
6.93k
    vp8_default_coef_probs(pc);
829
830
    /* reset the segment feature data to 0 with delta coding (Default state). */
831
6.93k
    memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
832
6.93k
    xd->mb_segment_abs_delta = SEGMENT_DELTADATA;
833
834
    /* reset the mode ref deltasa for loop filter */
835
6.93k
    memset(xd->ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
836
6.93k
    memset(xd->mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
837
838
    /* All buffers are implicitly updated on key frames. */
839
6.93k
    pc->refresh_golden_frame = 1;
840
6.93k
    pc->refresh_alt_ref_frame = 1;
841
6.93k
    pc->copy_buffer_to_gf = 0;
842
6.93k
    pc->copy_buffer_to_arf = 0;
843
844
    /* Note that Golden and Altref modes cannot be used on a key frame so
845
     * ref_frame_sign_bias[] is undefined and meaningless
846
     */
847
6.93k
    pc->ref_frame_sign_bias[GOLDEN_FRAME] = 0;
848
6.93k
    pc->ref_frame_sign_bias[ALTREF_FRAME] = 0;
849
27.8k
  } else {
850
    /* To enable choice of different interploation filters */
851
27.8k
    if (!pc->use_bilinear_mc_filter) {
852
24.8k
      xd->subpixel_predict = vp8_sixtap_predict4x4;
853
24.8k
      xd->subpixel_predict8x4 = vp8_sixtap_predict8x4;
854
24.8k
      xd->subpixel_predict8x8 = vp8_sixtap_predict8x8;
855
24.8k
      xd->subpixel_predict16x16 = vp8_sixtap_predict16x16;
856
24.8k
    } else {
857
2.97k
      xd->subpixel_predict = vp8_bilinear_predict4x4;
858
2.97k
      xd->subpixel_predict8x4 = vp8_bilinear_predict8x4;
859
2.97k
      xd->subpixel_predict8x8 = vp8_bilinear_predict8x8;
860
2.97k
      xd->subpixel_predict16x16 = vp8_bilinear_predict16x16;
861
2.97k
    }
862
863
27.8k
    if (pbi->decoded_key_frame && pbi->ec_enabled && !pbi->ec_active) {
864
0
      pbi->ec_active = 1;
865
0
    }
866
27.8k
  }
867
868
34.7k
  xd->left_context = &pc->left_context;
869
34.7k
  xd->mode_info_context = pc->mi;
870
34.7k
  xd->frame_type = pc->frame_type;
871
34.7k
  xd->mode_info_context->mbmi.mode = DC_PRED;
872
34.7k
  xd->mode_info_stride = pc->mode_info_stride;
873
34.7k
  xd->corrupted = 0; /* init without corruption */
874
875
34.7k
  xd->fullpixel_mask = ~0;
876
34.7k
  if (pc->full_pixel) xd->fullpixel_mask = ~7;
877
34.7k
}
878
879
82.9k
int vp8_decode_frame(VP8D_COMP *pbi) {
880
82.9k
  vp8_reader *const bc = &pbi->mbc[8];
881
82.9k
  VP8_COMMON *const pc = &pbi->common;
882
82.9k
  MACROBLOCKD *const xd = &pbi->mb;
883
82.9k
  const unsigned char *data = pbi->fragments.ptrs[0];
884
82.9k
  const unsigned int data_sz = pbi->fragments.sizes[0];
885
82.9k
  const unsigned char *data_end = data + data_sz;
886
82.9k
  ptrdiff_t first_partition_length_in_bytes;
887
888
82.9k
  int i, j, k, l;
889
82.9k
  const int *const mb_feature_data_bits = vp8_mb_feature_data_bits;
890
82.9k
  int corrupt_tokens = 0;
891
82.9k
  int prev_independent_partitions = pbi->independent_partitions;
892
893
82.9k
  YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME];
894
895
  /* start with no corruption of current frame */
896
82.9k
  xd->corrupted = 0;
897
82.9k
  yv12_fb_new->corrupted = 0;
898
899
82.9k
  if (data_end - data < 3) {
900
35.8k
    if (!pbi->ec_active) {
901
35.8k
      vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
902
35.8k
                         "Truncated packet");
903
35.8k
    }
904
905
    /* Declare the missing frame as an inter frame since it will
906
       be handled as an inter frame when we have estimated its
907
       motion vectors. */
908
35.8k
    pc->frame_type = INTER_FRAME;
909
35.8k
    pc->version = 0;
910
35.8k
    pc->show_frame = 1;
911
35.8k
    first_partition_length_in_bytes = 0;
912
47.0k
  } else {
913
47.0k
    unsigned char clear_buffer[10];
914
47.0k
    const unsigned char *clear = data;
915
47.0k
    if (pbi->decrypt_cb) {
916
0
      int n = (int)VPXMIN(sizeof(clear_buffer), data_sz);
917
0
      pbi->decrypt_cb(pbi->decrypt_state, data, clear_buffer, n);
918
0
      clear = clear_buffer;
919
0
    }
920
921
47.0k
    pc->frame_type = (FRAME_TYPE)(clear[0] & 1);
922
47.0k
    pc->version = (clear[0] >> 1) & 7;
923
47.0k
    pc->show_frame = (clear[0] >> 4) & 1;
924
47.0k
    first_partition_length_in_bytes =
925
47.0k
        (clear[0] | (clear[1] << 8) | (clear[2] << 16)) >> 5;
926
927
47.0k
    if (!pbi->ec_active && (data + first_partition_length_in_bytes > data_end ||
928
47.0k
                            data + first_partition_length_in_bytes < data)) {
929
11.3k
      vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
930
11.3k
                         "Truncated packet or corrupt partition 0 length");
931
11.3k
    }
932
933
47.0k
    data += 3;
934
47.0k
    clear += 3;
935
936
47.0k
    vp8_setup_version(pc);
937
938
47.0k
    if (pc->frame_type == KEY_FRAME) {
939
      /* vet via sync code */
940
      /* When error concealment is enabled we should only check the sync
941
       * code if we have enough bits available
942
       */
943
7.61k
      if (data + 3 < data_end) {
944
7.26k
        if (clear[0] != 0x9d || clear[1] != 0x01 || clear[2] != 0x2a) {
945
323
          vpx_internal_error(&pc->error, VPX_CODEC_UNSUP_BITSTREAM,
946
323
                             "Invalid frame sync code");
947
323
        }
948
7.26k
      }
949
950
      /* If error concealment is enabled we should only parse the new size
951
       * if we have enough data. Otherwise we will end up with the wrong
952
       * size.
953
       */
954
7.61k
      if (data + 6 < data_end) {
955
6.93k
        pc->Width = (clear[3] | (clear[4] << 8)) & 0x3fff;
956
6.93k
        pc->horiz_scale = clear[4] >> 6;
957
6.93k
        pc->Height = (clear[5] | (clear[6] << 8)) & 0x3fff;
958
6.93k
        pc->vert_scale = clear[6] >> 6;
959
6.93k
        data += 7;
960
6.93k
      } else if (!pbi->ec_active) {
961
357
        vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
962
357
                           "Truncated key frame header");
963
357
      } else {
964
        /* Error concealment is active, clear the frame. */
965
323
        data = data_end;
966
323
      }
967
39.4k
    } else {
968
39.4k
      xd->pre = *yv12_fb_new;
969
39.4k
      xd->dst = *yv12_fb_new;
970
39.4k
    }
971
47.0k
  }
972
82.9k
  if ((!pbi->decoded_key_frame && pc->frame_type != KEY_FRAME)) {
973
285
    return -1;
974
285
  }
975
976
82.6k
  init_frame(pbi);
977
978
82.6k
  if (vp8dx_start_decode(bc, data, (unsigned int)(data_end - data),
979
82.6k
                         pbi->decrypt_cb, pbi->decrypt_state)) {
980
0
    vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
981
0
                       "Failed to allocate bool decoder 0");
982
0
  }
983
82.6k
  if (pc->frame_type == KEY_FRAME) {
984
6.93k
    (void)vp8_read_bit(bc);  // colorspace
985
6.93k
    pc->clamp_type = (CLAMP_TYPE)vp8_read_bit(bc);
986
6.93k
  }
987
988
  /* Is segmentation enabled */
989
82.6k
  xd->segmentation_enabled = (unsigned char)vp8_read_bit(bc);
990
991
82.6k
  if (xd->segmentation_enabled) {
992
    /* Signal whether or not the segmentation map is being explicitly updated
993
     * this frame. */
994
13.4k
    xd->update_mb_segmentation_map = (unsigned char)vp8_read_bit(bc);
995
13.4k
    xd->update_mb_segmentation_data = (unsigned char)vp8_read_bit(bc);
996
997
13.4k
    if (xd->update_mb_segmentation_data) {
998
12.4k
      xd->mb_segment_abs_delta = (unsigned char)vp8_read_bit(bc);
999
1000
12.4k
      memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
1001
1002
      /* For each segmentation feature (Quant and loop filter level) */
1003
37.4k
      for (i = 0; i < MB_LVL_MAX; ++i) {
1004
124k
        for (j = 0; j < MAX_MB_SEGMENTS; ++j) {
1005
          /* Frame level data */
1006
99.7k
          if (vp8_read_bit(bc)) {
1007
71.9k
            xd->segment_feature_data[i][j] =
1008
71.9k
                (signed char)vp8_read_literal(bc, mb_feature_data_bits[i]);
1009
1010
71.9k
            if (vp8_read_bit(bc)) {
1011
39.8k
              xd->segment_feature_data[i][j] = -xd->segment_feature_data[i][j];
1012
39.8k
            }
1013
71.9k
          } else {
1014
27.8k
            xd->segment_feature_data[i][j] = 0;
1015
27.8k
          }
1016
99.7k
        }
1017
24.9k
      }
1018
12.4k
    }
1019
1020
13.4k
    if (xd->update_mb_segmentation_map) {
1021
      /* Which macro block level features are enabled */
1022
12.6k
      memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));
1023
1024
      /* Read the probs used to decode the segment id for each macro block. */
1025
50.7k
      for (i = 0; i < MB_FEATURE_TREE_PROBS; ++i) {
1026
        /* If not explicitly set value is defaulted to 255 by memset above */
1027
38.0k
        if (vp8_read_bit(bc)) {
1028
24.8k
          xd->mb_segment_tree_probs[i] = (vp8_prob)vp8_read_literal(bc, 8);
1029
24.8k
        }
1030
38.0k
      }
1031
12.6k
    }
1032
69.1k
  } else {
1033
    /* No segmentation updates on this frame */
1034
69.1k
    xd->update_mb_segmentation_map = 0;
1035
69.1k
    xd->update_mb_segmentation_data = 0;
1036
69.1k
  }
1037
1038
  /* Read the loop filter level and type */
1039
82.6k
  pc->filter_type = (LOOPFILTERTYPE)vp8_read_bit(bc);
1040
82.6k
  pc->filter_level = vp8_read_literal(bc, 6);
1041
82.6k
  pc->sharpness_level = vp8_read_literal(bc, 3);
1042
1043
  /* Read in loop filter deltas applied at the MB level based on mode or ref
1044
   * frame. */
1045
82.6k
  xd->mode_ref_lf_delta_update = 0;
1046
82.6k
  xd->mode_ref_lf_delta_enabled = (unsigned char)vp8_read_bit(bc);
1047
1048
82.6k
  if (xd->mode_ref_lf_delta_enabled) {
1049
    /* Do the deltas need to be updated */
1050
7.83k
    xd->mode_ref_lf_delta_update = (unsigned char)vp8_read_bit(bc);
1051
1052
7.83k
    if (xd->mode_ref_lf_delta_update) {
1053
      /* Send update */
1054
21.8k
      for (i = 0; i < MAX_REF_LF_DELTAS; ++i) {
1055
17.4k
        if (vp8_read_bit(bc)) {
1056
          /*sign = vp8_read_bit( bc );*/
1057
7.92k
          xd->ref_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
1058
1059
7.92k
          if (vp8_read_bit(bc)) { /* Apply sign */
1060
5.34k
            xd->ref_lf_deltas[i] = xd->ref_lf_deltas[i] * -1;
1061
5.34k
          }
1062
7.92k
        }
1063
17.4k
      }
1064
1065
      /* Send update */
1066
21.8k
      for (i = 0; i < MAX_MODE_LF_DELTAS; ++i) {
1067
17.4k
        if (vp8_read_bit(bc)) {
1068
          /*sign = vp8_read_bit( bc );*/
1069
5.93k
          xd->mode_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
1070
1071
5.93k
          if (vp8_read_bit(bc)) { /* Apply sign */
1072
4.50k
            xd->mode_lf_deltas[i] = xd->mode_lf_deltas[i] * -1;
1073
4.50k
          }
1074
5.93k
        }
1075
17.4k
      }
1076
4.36k
    }
1077
7.83k
  }
1078
1079
82.6k
  setup_token_decoder(pbi, data + first_partition_length_in_bytes);
1080
1081
82.6k
  xd->current_bc = &pbi->mbc[0];
1082
1083
  /* Read the default quantizers. */
1084
82.6k
  {
1085
82.6k
    int Q, q_update;
1086
1087
82.6k
    Q = vp8_read_literal(bc, 7); /* AC 1st order Q = default */
1088
82.6k
    pc->base_qindex = Q;
1089
82.6k
    q_update = 0;
1090
82.6k
    pc->y1dc_delta_q = get_delta_q(bc, pc->y1dc_delta_q, &q_update);
1091
82.6k
    pc->y2dc_delta_q = get_delta_q(bc, pc->y2dc_delta_q, &q_update);
1092
82.6k
    pc->y2ac_delta_q = get_delta_q(bc, pc->y2ac_delta_q, &q_update);
1093
82.6k
    pc->uvdc_delta_q = get_delta_q(bc, pc->uvdc_delta_q, &q_update);
1094
82.6k
    pc->uvac_delta_q = get_delta_q(bc, pc->uvac_delta_q, &q_update);
1095
1096
82.6k
    if (q_update) vp8cx_init_de_quantizer(pbi);
1097
1098
    /* MB level dequantizer setup */
1099
82.6k
    vp8_mb_init_dequantizer(pbi, &pbi->mb);
1100
82.6k
  }
1101
1102
  /* Determine if the golden frame or ARF buffer should be updated and how.
1103
   * For all non key frames the GF and ARF refresh flags and sign bias
1104
   * flags must be set explicitly.
1105
   */
1106
82.6k
  if (pc->frame_type != KEY_FRAME) {
1107
    /* Should the GF or ARF be updated from the current frame */
1108
26.7k
    pc->refresh_golden_frame = vp8_read_bit(bc);
1109
#if CONFIG_ERROR_CONCEALMENT
1110
    /* Assume we shouldn't refresh golden if the bit is missing */
1111
    xd->corrupted |= vp8dx_bool_error(bc);
1112
    if (pbi->ec_active && xd->corrupted) pc->refresh_golden_frame = 0;
1113
#endif
1114
1115
26.7k
    pc->refresh_alt_ref_frame = vp8_read_bit(bc);
1116
#if CONFIG_ERROR_CONCEALMENT
1117
    /* Assume we shouldn't refresh altref if the bit is missing */
1118
    xd->corrupted |= vp8dx_bool_error(bc);
1119
    if (pbi->ec_active && xd->corrupted) pc->refresh_alt_ref_frame = 0;
1120
#endif
1121
1122
    /* Buffer to buffer copy flags. */
1123
26.7k
    pc->copy_buffer_to_gf = 0;
1124
1125
26.7k
    if (!pc->refresh_golden_frame) {
1126
10.8k
      pc->copy_buffer_to_gf = vp8_read_literal(bc, 2);
1127
10.8k
    }
1128
1129
#if CONFIG_ERROR_CONCEALMENT
1130
    /* Assume we shouldn't copy to the golden if the bit is missing */
1131
    xd->corrupted |= vp8dx_bool_error(bc);
1132
    if (pbi->ec_active && xd->corrupted) pc->copy_buffer_to_gf = 0;
1133
#endif
1134
1135
26.7k
    pc->copy_buffer_to_arf = 0;
1136
1137
26.7k
    if (!pc->refresh_alt_ref_frame) {
1138
22.5k
      pc->copy_buffer_to_arf = vp8_read_literal(bc, 2);
1139
22.5k
    }
1140
1141
#if CONFIG_ERROR_CONCEALMENT
1142
    /* Assume we shouldn't copy to the alt-ref if the bit is missing */
1143
    xd->corrupted |= vp8dx_bool_error(bc);
1144
    if (pbi->ec_active && xd->corrupted) pc->copy_buffer_to_arf = 0;
1145
#endif
1146
1147
26.7k
    pc->ref_frame_sign_bias[GOLDEN_FRAME] = vp8_read_bit(bc);
1148
26.7k
    pc->ref_frame_sign_bias[ALTREF_FRAME] = vp8_read_bit(bc);
1149
26.7k
  }
1150
1151
82.6k
  pc->refresh_entropy_probs = vp8_read_bit(bc);
1152
#if CONFIG_ERROR_CONCEALMENT
1153
  /* Assume we shouldn't refresh the probabilities if the bit is
1154
   * missing */
1155
  xd->corrupted |= vp8dx_bool_error(bc);
1156
  if (pbi->ec_active && xd->corrupted) pc->refresh_entropy_probs = 0;
1157
#endif
1158
82.6k
  if (pc->refresh_entropy_probs == 0) {
1159
14.9k
    pc->lfc = pc->fc;
1160
14.9k
  }
1161
1162
82.6k
  pc->refresh_last_frame = pc->frame_type == KEY_FRAME || vp8_read_bit(bc);
1163
1164
#if CONFIG_ERROR_CONCEALMENT
1165
  /* Assume we should refresh the last frame if the bit is missing */
1166
  xd->corrupted |= vp8dx_bool_error(bc);
1167
  if (pbi->ec_active && xd->corrupted) pc->refresh_last_frame = 1;
1168
#endif
1169
1170
82.6k
  {
1171
82.6k
    pbi->independent_partitions = 1;
1172
1173
    /* read coef probability tree */
1174
212k
    for (i = 0; i < BLOCK_TYPES; ++i) {
1175
1.16M
      for (j = 0; j < COEF_BANDS; ++j) {
1176
4.15M
        for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
1177
37.3M
          for (l = 0; l < ENTROPY_NODES; ++l) {
1178
34.2M
            vp8_prob *const p = pc->fc.coef_probs[i][j][k] + l;
1179
1180
34.2M
            if (vp8_read(bc, vp8_coef_update_probs[i][j][k][l])) {
1181
300k
              *p = (vp8_prob)vp8_read_literal(bc, 8);
1182
300k
            }
1183
34.2M
            if (k > 0 && *p != pc->fc.coef_probs[i][j][k - 1][l]) {
1184
13.3M
              pbi->independent_partitions = 0;
1185
13.3M
            }
1186
34.2M
          }
1187
3.11M
        }
1188
1.03M
      }
1189
129k
    }
1190
82.6k
  }
1191
1192
  /* clear out the coeff buffer */
1193
82.6k
  memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
1194
1195
82.6k
  vp8_decode_mode_mvs(pbi);
1196
1197
#if CONFIG_ERROR_CONCEALMENT
1198
  if (pbi->ec_active &&
1199
      pbi->mvs_corrupt_from_mb < (unsigned int)pc->mb_cols * pc->mb_rows) {
1200
    /* Motion vectors are missing in this frame. We will try to estimate
1201
     * them and then continue decoding the frame as usual */
1202
    vp8_estimate_missing_mvs(pbi);
1203
  }
1204
#endif
1205
1206
82.6k
  memset(pc->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) * pc->mb_cols);
1207
82.6k
  pbi->frame_corrupt_residual = 0;
1208
1209
82.6k
#if CONFIG_MULTITHREAD
1210
82.6k
  if (vpx_atomic_load_acquire(&pbi->b_multithreaded_rd) &&
1211
82.6k
      pc->multi_token_partition != ONE_PARTITION) {
1212
0
    unsigned int thread;
1213
0
    if (vp8mt_decode_mb_rows(pbi, xd)) {
1214
0
      vp8_decoder_remove_threads(pbi);
1215
0
      pbi->restart_threads = 1;
1216
0
      vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME, NULL);
1217
0
    }
1218
0
    vp8_yv12_extend_frame_borders(yv12_fb_new);
1219
0
    for (thread = 0; thread < pbi->decoding_thread_count; ++thread) {
1220
0
      corrupt_tokens |= pbi->mb_row_di[thread].mbd.corrupted;
1221
0
    }
1222
0
  } else
1223
82.6k
#endif
1224
82.6k
  {
1225
82.6k
    decode_mb_rows(pbi);
1226
82.6k
    corrupt_tokens |= xd->corrupted;
1227
82.6k
  }
1228
1229
  /* Collect information about decoder corruption. */
1230
  /* 1. Check first boolean decoder for errors. */
1231
82.6k
  yv12_fb_new->corrupted = vp8dx_bool_error(bc);
1232
  /* 2. Check the macroblock information */
1233
82.6k
  yv12_fb_new->corrupted |= corrupt_tokens;
1234
1235
82.6k
  if (!pbi->decoded_key_frame) {
1236
5.31k
    if (pc->frame_type == KEY_FRAME && !yv12_fb_new->corrupted) {
1237
1.13k
      pbi->decoded_key_frame = 1;
1238
4.17k
    } else {
1239
4.17k
      vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME,
1240
4.17k
                         "A stream must start with a complete key frame");
1241
4.17k
    }
1242
5.31k
  }
1243
1244
  /* vpx_log("Decoder: Frame Decoded, Size Roughly:%d bytes
1245
   * \n",bc->pos+pbi->bc2.pos); */
1246
1247
82.6k
  if (pc->refresh_entropy_probs == 0) {
1248
11.8k
    pc->fc = pc->lfc;
1249
11.8k
    pbi->independent_partitions = prev_independent_partitions;
1250
11.8k
  }
1251
1252
#ifdef PACKET_TESTING
1253
  {
1254
    FILE *f = fopen("decompressor.VP8", "ab");
1255
    unsigned int size = pbi->bc2.pos + pbi->bc.pos + 8;
1256
    fwrite((void *)&size, 4, 1, f);
1257
    fwrite((void *)pbi->Source, size, 1, f);
1258
    fclose(f);
1259
  }
1260
#endif
1261
1262
82.6k
  return 0;
1263
82.9k
}