vp9_decodframe.c 33.3 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9
10
 */

11
#include <assert.h>
John Koleszar's avatar
John Koleszar committed
12

13
14
15
16
#include "./vp9_rtcd.h"
#include "vpx_mem/vpx_mem.h"
#include "vpx_scale/vpx_scale.h"

Dmitry Kovalev's avatar
Dmitry Kovalev committed
17
#include "vp9/common/vp9_alloccommon.h"
Ronald S. Bultje's avatar
Ronald S. Bultje committed
18
#include "vp9/common/vp9_common.h"
Yaowu Xu's avatar
Yaowu Xu committed
19
#include "vp9/common/vp9_entropy.h"
20
#include "vp9/common/vp9_entropymode.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
21
22
#include "vp9/common/vp9_extend.h"
#include "vp9/common/vp9_pred_common.h"
23
#include "vp9/common/vp9_quant_common.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
24
25
#include "vp9/common/vp9_reconintra.h"
#include "vp9/common/vp9_reconinter.h"
26
27
#include "vp9/common/vp9_seg_common.h"
#include "vp9/common/vp9_tile_common.h"
28
29

#include "vp9/decoder/vp9_dboolhuff.h"
30
31
32
#include "vp9/decoder/vp9_decodframe.h"
#include "vp9/decoder/vp9_detokenize.h"
#include "vp9/decoder/vp9_decodemv.h"
33
#include "vp9/decoder/vp9_dsubexp.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
34
#include "vp9/decoder/vp9_idct_blk.h"
35
36
#include "vp9/decoder/vp9_onyxd_int.h"
#include "vp9/decoder/vp9_read_bit_buffer.h"
37
#include "vp9/decoder/vp9_thread.h"
Dmitry Kovalev's avatar
Dmitry Kovalev committed
38
#include "vp9/decoder/vp9_treereader.h"
39

40
41
static int read_be32(const uint8_t *p) {
  return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
42
43
44
}

// len == 0 is not allowed
45
static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
46
47
48
  return start + len > start && start + len <= end;
}

49
50
51
52
53
static int decode_unsigned_max(struct vp9_read_bit_buffer *rb, int max) {
  const int data = vp9_rb_read_literal(rb, get_unsigned_bits(max));
  return data > max ? max : data;
}

54
55
56
57
58
static TX_MODE read_tx_mode(vp9_reader *r) {
  TX_MODE tx_mode = vp9_read_literal(r, 2);
  if (tx_mode == ALLOW_32X32)
    tx_mode += vp9_read_bit(r);
  return tx_mode;
59
60
}

61
static void read_tx_probs(struct tx_probs *tx_probs, vp9_reader *r) {
62
63
64
  int i, j;

  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
65
    for (j = 0; j < TX_SIZES - 3; ++j)
66
      if (vp9_read(r, VP9_MODE_UPDATE_PROB))
67
        vp9_diff_update_prob(r, &tx_probs->p8x8[i][j]);
68
69

  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
70
    for (j = 0; j < TX_SIZES - 2; ++j)
71
      if (vp9_read(r, VP9_MODE_UPDATE_PROB))
72
        vp9_diff_update_prob(r, &tx_probs->p16x16[i][j]);
73
74

  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
75
    for (j = 0; j < TX_SIZES - 1; ++j)
76
      if (vp9_read(r, VP9_MODE_UPDATE_PROB))
77
        vp9_diff_update_prob(r, &tx_probs->p32x32[i][j]);
John Koleszar's avatar
John Koleszar committed
78
79
}

80
static void init_dequantizer(VP9_COMMON *cm, MACROBLOCKD *xd) {
John Koleszar's avatar
John Koleszar committed
81
  int i;
82
  const int segment_id = xd->mode_info_context->mbmi.segment_id;
83
  xd->q_index = vp9_get_qindex(xd, segment_id, cm->base_qindex);
John Koleszar's avatar
John Koleszar committed
84

85
  xd->plane[0].dequant = cm->y_dequant[xd->q_index];
86
  for (i = 1; i < MAX_MB_PLANE; i++)
87
    xd->plane[i].dequant = cm->uv_dequant[xd->q_index];
John Koleszar's avatar
John Koleszar committed
88
89
}

90
91
92
static void decode_block(int plane, int block, BLOCK_SIZE_TYPE bsize,
                         int ss_txfrm_size, void *arg) {
  MACROBLOCKD* const xd = arg;
93
94
95
  struct macroblockd_plane *pd = &xd->plane[plane];
  int16_t* const qcoeff = BLOCK_OFFSET(pd->qcoeff, block, 16);
  const int stride = pd->dst.stride;
96
  const int eob = pd->eobs[block];
97
98
99
100
  const int raster_block = txfrm_block_to_raster_block(xd, bsize, plane,
                                                       block, ss_txfrm_size);
  uint8_t* const dst = raster_block_offset_uint8(xd, bsize, plane,
                                                 raster_block,
101
                                                 pd->dst.buf, stride);
102

103
  switch (ss_txfrm_size / 2) {
104
105
    case TX_4X4: {
      const TX_TYPE tx_type = get_tx_type_4x4(pd->plane_type, xd, raster_block);
106
      if (tx_type == DCT_DCT)
107
        xd->itxm_add(qcoeff, dst, stride, eob);
108
      else
109
        vp9_iht_add_c(tx_type, qcoeff, dst, stride, eob);
110
      break;
111
    }
112
    case TX_8X8:
113
114
      vp9_iht_add_8x8_c(get_tx_type_8x8(pd->plane_type, xd), qcoeff, dst,
                        stride, eob);
115
116
      break;
    case TX_16X16:
117
118
      vp9_iht_add_16x16_c(get_tx_type_16x16(pd->plane_type, xd), qcoeff, dst,
                          stride, eob);
119
120
      break;
    case TX_32X32:
121
      vp9_idct_add_32x32(qcoeff, dst, stride, eob);
122
      break;
123
124
125
  }
}

126
127
128
static void decode_block_intra(int plane, int block, BLOCK_SIZE_TYPE bsize,
                               int ss_txfrm_size, void *arg) {
  MACROBLOCKD* const xd = arg;
129
  struct macroblockd_plane *pd = &xd->plane[plane];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
130
  MODE_INFO *const mi = xd->mode_info_context;
131

132
133
134
135
  const int raster_block = txfrm_block_to_raster_block(xd, bsize, plane,
                                                       block, ss_txfrm_size);
  uint8_t* const dst = raster_block_offset_uint8(xd, bsize, plane,
                                                 raster_block,
136
                                                 pd->dst.buf, pd->dst.stride);
137
  const TX_SIZE tx_size = (TX_SIZE)(ss_txfrm_size / 2);
138
  int b_mode;
139
  int plane_b_size;
140
  const int tx_ib = raster_block >> tx_size;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
141
142
  const int mode = plane == 0 ? mi->mbmi.mode
                              : mi->mbmi.uv_mode;
143

144
145
  if (plane == 0 && mi->mbmi.sb_type < BLOCK_8X8) {
    assert(bsize == BLOCK_8X8);
146
    b_mode = mi->bmi[raster_block].as_mode;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
147
  } else {
148
    b_mode = mode;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
149
  }
150

151
  if (xd->mb_to_right_edge < 0 || xd->mb_to_bottom_edge < 0)
152
153
    extend_for_intra(xd, plane, block, bsize, ss_txfrm_size);

154
155
  plane_b_size = b_width_log2(bsize) - pd->subsampling_x;
  vp9_predict_intra_block(xd, tx_ib, plane_b_size, tx_size, b_mode,
156
                          dst, pd->dst.stride,
157
                          dst, pd->dst.stride);
158

159
  // Early exit if there are no coefficients
Dmitry Kovalev's avatar
Dmitry Kovalev committed
160
  if (mi->mbmi.mb_skip_coeff)
161
162
    return;

163
  decode_block(plane, block, bsize, ss_txfrm_size, arg);
164
165
}

Dmitry Kovalev's avatar
Dmitry Kovalev committed
166
167
static int decode_tokens(VP9D_COMP *pbi, BLOCK_SIZE_TYPE bsize, vp9_reader *r) {
  MACROBLOCKD *const xd = &pbi->mb;
168

Dmitry Kovalev's avatar
Dmitry Kovalev committed
169
  if (xd->mode_info_context->mbmi.mb_skip_coeff) {
170
    vp9_reset_sb_tokens_context(xd, bsize);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
171
    return -1;
172
  } else {
173
    if (xd->seg.enabled)
174
      init_dequantizer(&pbi->common, xd);
175

Dmitry Kovalev's avatar
Dmitry Kovalev committed
176
177
    // TODO(dkovalev) if (!vp9_reader_has_error(r))
    return vp9_decode_tokens(pbi, r, bsize);
178
179
180
  }
}

181
static void set_offsets(VP9D_COMP *pbi, BLOCK_SIZE_TYPE bsize,
182
                        int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
183
184
  VP9_COMMON *const cm = &pbi->common;
  MACROBLOCKD *const xd = &pbi->mb;
185
186
  const int bh = 1 << mi_height_log2(bsize);
  const int bw = 1 << mi_width_log2(bsize);
187
  const int mi_idx = mi_row * cm->mode_info_stride + mi_col;
188
  int i;
189

190
  xd->mode_info_context = cm->mi + mi_idx;
191
  xd->mode_info_context->mbmi.sb_type = bsize;
192
193
  // Special case: if prev_mi is NULL, the previous mode info context
  // cannot be used.
194
  xd->prev_mode_info_context = cm->prev_mi ? cm->prev_mi + mi_idx : NULL;
195

196
  for (i = 0; i < MAX_MB_PLANE; i++) {
197
198
199
200
201
    struct macroblockd_plane *pd = &xd->plane[i];
    pd->above_context = cm->above_context[i] +
                            (mi_col * 2 >> pd->subsampling_x);
    pd->left_context = cm->left_context[i] +
                           (((mi_row * 2) & 15) >> pd->subsampling_y);
202
  }
203
204

  set_partition_seg_context(cm, xd, mi_row, mi_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
205

206
207
  // Distance of Mb to the various image edges. These are specified to 8th pel
  // as they are always compared to values that are in 1/8th pel units
208
  set_mi_row_col(cm, xd, mi_row, bh, mi_col, bw);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
209

210
  setup_dst_planes(xd, &cm->yv12_fb[cm->new_fb_idx], mi_row, mi_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
211
}
John Koleszar's avatar
John Koleszar committed
212

213
static void set_ref(VP9D_COMP *pbi, int i, int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
214
215
  VP9_COMMON *const cm = &pbi->common;
  MACROBLOCKD *const xd = &pbi->mb;
216
  MB_MODE_INFO *const mbmi = &xd->mode_info_context->mbmi;
217
  const int ref = mbmi->ref_frame[i] - 1;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
218

219
220
  const YV12_BUFFER_CONFIG *cfg = &cm->yv12_fb[cm->active_ref_idx[ref]];
  xd->scale_factor[i] = cm->active_ref_scale[ref];
221
  setup_pre_planes(xd, i, cfg, mi_row, mi_col, &xd->scale_factor[i]);
222
  xd->corrupted |= cfg->corrupted;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
223
}
John Koleszar's avatar
John Koleszar committed
224

225
static void decode_modes_b(VP9D_COMP *pbi, int mi_row, int mi_col,
226
                           vp9_reader *r, BLOCK_SIZE_TYPE bsize) {
227
  VP9_COMMON *const cm = &pbi->common;
228
  MACROBLOCKD *const xd = &pbi->mb;
229
  const int less8x8 = bsize < BLOCK_8X8;
230
  MB_MODE_INFO *mbmi;
231

232
  if (less8x8)
233
234
    if (xd->ab_index > 0)
      return;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
235

236
  set_offsets(pbi, bsize, mi_row, mi_col);
237
  vp9_read_mode_info(pbi, mi_row, mi_col, r);
238

239
  if (less8x8)
240
    bsize = BLOCK_8X8;
241
242
243
244

  // Has to be called after set_offsets
  mbmi = &xd->mode_info_context->mbmi;

245
  if (!is_inter_block(mbmi)) {
246
247
248
    // Intra reconstruction
    decode_tokens(pbi, bsize, r);
    foreach_transformed_block(xd, bsize, decode_block_intra, xd);
249
  } else {
250
251
    // Inter reconstruction
    int eobtotal;
252
253
254
255
256

    set_ref(pbi, 0, mi_row, mi_col);
    if (mbmi->ref_frame[1] > INTRA_FRAME)
      set_ref(pbi, 1, mi_row, mi_col);

257
258
259
260
261
262
263
264
265
    vp9_setup_interp_filters(xd, mbmi->interp_filter, cm);
    vp9_build_inter_predictors_sb(xd, mi_row, mi_col, bsize);
    eobtotal = decode_tokens(pbi, bsize, r);
    if (less8x8) {
      if (eobtotal >= 0)
        foreach_transformed_block(xd, bsize, decode_block, xd);
    } else {
      assert(mbmi->sb_type == bsize);
      if (eobtotal == 0)
266
267
        // skip loopfilter
        vp9_set_pred_flag_mbskip(cm, bsize, mi_row, mi_col, 1);
268
269
270
      else if (eobtotal > 0)
        foreach_transformed_block(xd, bsize, decode_block, xd);
    }
271
  }
272
  xd->corrupted |= vp9_reader_has_error(r);
273
274
}

275
static void decode_modes_sb(VP9D_COMP *pbi, int mi_row, int mi_col,
276
                            vp9_reader* r, BLOCK_SIZE_TYPE bsize) {
277
278
  VP9_COMMON *const pc = &pbi->common;
  MACROBLOCKD *const xd = &pbi->mb;
279
  int bs = (1 << mi_width_log2(bsize)) / 2, n;
280
281
282
  PARTITION_TYPE partition = PARTITION_NONE;
  BLOCK_SIZE_TYPE subsize;

283
  if (mi_row >= pc->mi_rows || mi_col >= pc->mi_cols)
284
285
    return;

286
  if (bsize < BLOCK_8X8) {
287
288
    if (xd->ab_index != 0)
      return;
289
  } else {
290
    int pl;
291
    const int idx = check_bsize_coverage(pc, mi_row, mi_col, bsize);
292
    set_partition_seg_context(pc, xd, mi_row, mi_col);
293
    pl = partition_plane_context(xd, bsize);
294
295
296
297
298
299
300
301
302
303

    if (idx == 0)
      partition = treed_read(r, vp9_partition_tree,
                             pc->fc.partition_prob[pc->frame_type][pl]);
    else if (idx > 0 &&
        !vp9_read(r, pc->fc.partition_prob[pc->frame_type][pl][idx]))
      partition = (idx == 1) ? PARTITION_HORZ : PARTITION_VERT;
    else
      partition = PARTITION_SPLIT;

304
    pc->counts.partition[pl][partition]++;
305
306
  }

307
  subsize = get_subsize(bsize, partition);
308
  *(get_sb_index(xd, subsize)) = 0;
309

310
311
  switch (partition) {
    case PARTITION_NONE:
312
      decode_modes_b(pbi, mi_row, mi_col, r, subsize);
313
314
      break;
    case PARTITION_HORZ:
315
      decode_modes_b(pbi, mi_row, mi_col, r, subsize);
316
      *(get_sb_index(xd, subsize)) = 1;
317
      if (mi_row + bs < pc->mi_rows)
318
        decode_modes_b(pbi, mi_row + bs, mi_col, r, subsize);
319
320
      break;
    case PARTITION_VERT:
321
      decode_modes_b(pbi, mi_row, mi_col, r, subsize);
322
      *(get_sb_index(xd, subsize)) = 1;
323
      if (mi_col + bs < pc->mi_cols)
324
        decode_modes_b(pbi, mi_row, mi_col + bs, r, subsize);
325
326
327
328
      break;
    case PARTITION_SPLIT:
      for (n = 0; n < 4; n++) {
        int j = n >> 1, i = n & 0x01;
329
        *(get_sb_index(xd, subsize)) = n;
330
        decode_modes_sb(pbi, mi_row + j * bs, mi_col + i * bs, r, subsize);
331
332
333
      }
      break;
    default:
334
      assert(!"Invalid partition type");
335
  }
336

337
  // update partition context
338
339
  if (bsize >= BLOCK_8X8 &&
      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT)) {
340
341
342
    set_partition_seg_context(pc, xd, mi_row, mi_col);
    update_partition_context(xd, subsize, bsize);
  }
343
344
}

345
static void setup_token_decoder(VP9D_COMP *pbi,
346
                                const uint8_t *data, size_t read_size,
347
                                vp9_reader *r) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
348
  VP9_COMMON *pc = &pbi->common;
349
  const uint8_t *data_end = pbi->source + pbi->source_sz;
John Koleszar's avatar
John Koleszar committed
350

Dmitry Kovalev's avatar
Dmitry Kovalev committed
351
352
353
  // Validate the calculated partition length. If the buffer
  // described by the partition can't be fully read, then restrict
  // it to the portion that can be (for EC mode) or throw an error.
354
  if (!read_is_valid(data, read_size, data_end))
John Koleszar's avatar
John Koleszar committed
355
    vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
356
                       "Truncated packet or corrupt tile length");
John Koleszar's avatar
John Koleszar committed
357

358
  if (vp9_reader_init(r, data, read_size))
John Koleszar's avatar
John Koleszar committed
359
360
    vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate bool decoder %d", 1);
John Koleszar's avatar
John Koleszar committed
361
362
}

363
static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
364
                                   vp9_reader *r) {
365
366
367
368
369
370
371
372
373
374
375
  int i, j, k, l, m;

  if (vp9_read_bit(r))
    for (i = 0; i < BLOCK_TYPES; i++)
      for (j = 0; j < REF_TYPES; j++)
        for (k = 0; k < COEF_BANDS; k++)
          for (l = 0; l < PREV_COEF_CONTEXTS; l++)
            if (k > 0 || l < 3)
              for (m = 0; m < UNCONSTRAINED_NODES; m++)
                if (vp9_read(r, VP9_COEF_UPDATE_PROB))
                  vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
376
}
377

378
static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode,
379
                            vp9_reader *r) {
380
  read_coef_probs_common(fc->coef_probs[TX_4X4], r);
381

382
  if (tx_mode > ONLY_4X4)
383
    read_coef_probs_common(fc->coef_probs[TX_8X8], r);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
384

385
  if (tx_mode > ALLOW_8X8)
386
    read_coef_probs_common(fc->coef_probs[TX_16X16], r);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
387

388
  if (tx_mode > ALLOW_16X16)
389
    read_coef_probs_common(fc->coef_probs[TX_32X32], r);
390
391
}

392
393
static void setup_segmentation(struct segmentation *seg,
                               struct vp9_read_bit_buffer *rb) {
394
395
  int i, j;

396
397
  seg->update_map = 0;
  seg->update_data = 0;
398

399
400
  seg->enabled = vp9_rb_read_bit(rb);
  if (!seg->enabled)
401
402
403
    return;

  // Segmentation map update
404
405
  seg->update_map = vp9_rb_read_bit(rb);
  if (seg->update_map) {
Paul Wilkins's avatar
Paul Wilkins committed
406
    for (i = 0; i < SEG_TREE_PROBS; i++)
407
408
      seg->tree_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
                                               : MAX_PROB;
409

410
411
    seg->temporal_update = vp9_rb_read_bit(rb);
    if (seg->temporal_update) {
412
      for (i = 0; i < PREDICTION_PROBS; i++)
413
414
        seg->pred_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
                                                 : MAX_PROB;
415
416
    } else {
      for (i = 0; i < PREDICTION_PROBS; i++)
417
        seg->pred_probs[i] = MAX_PROB;
418
    }
419
  }
420

421
  // Segmentation data update
422
423
424
  seg->update_data = vp9_rb_read_bit(rb);
  if (seg->update_data) {
    seg->abs_delta = vp9_rb_read_bit(rb);
425

426
    vp9_clearall_segfeatures(seg);
427

Paul Wilkins's avatar
Paul Wilkins committed
428
    for (i = 0; i < MAX_SEGMENTS; i++) {
429
430
      for (j = 0; j < SEG_LVL_MAX; j++) {
        int data = 0;
431
        const int feature_enabled = vp9_rb_read_bit(rb);
432
        if (feature_enabled) {
433
          vp9_enable_segfeature(seg, i, j);
434
          data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
435
          if (vp9_is_segfeature_signed(j))
436
            data = vp9_rb_read_bit(rb) ? -data : data;
437
        }
438
        vp9_set_segdata(seg, i, j, data);
439
440
441
442
443
      }
    }
  }
}

444
445
static void setup_loopfilter(struct loopfilter *lf,
                             struct vp9_read_bit_buffer *rb) {
446

447
448
  lf->filter_level = vp9_rb_read_literal(rb, 6);
  lf->sharpness_level = vp9_rb_read_literal(rb, 3);
449
450
451

  // Read in loop filter deltas applied at the MB level based on mode or ref
  // frame.
452
  lf->mode_ref_delta_update = 0;
453

454
455
456
457
  lf->mode_ref_delta_enabled = vp9_rb_read_bit(rb);
  if (lf->mode_ref_delta_enabled) {
    lf->mode_ref_delta_update = vp9_rb_read_bit(rb);
    if (lf->mode_ref_delta_update) {
458
459
      int i;

460
461
      for (i = 0; i < MAX_REF_LF_DELTAS; i++)
        if (vp9_rb_read_bit(rb))
462
          lf->ref_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
463

464
465
      for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
        if (vp9_rb_read_bit(rb))
466
          lf->mode_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
467
468
469
470
    }
  }
}

471
472
static int read_delta_q(struct vp9_read_bit_buffer *rb, int *delta_q) {
  const int old = *delta_q;
473
474
  if (vp9_rb_read_bit(rb))
    *delta_q = vp9_rb_read_signed_literal(rb, 4);
475
476
  return old != *delta_q;
}
477

478
static void setup_quantization(VP9D_COMP *pbi, struct vp9_read_bit_buffer *rb) {
479
  MACROBLOCKD *const xd = &pbi->mb;
480
481
  VP9_COMMON *const cm = &pbi->common;
  int update = 0;
482

483
484
485
486
487
488
  cm->base_qindex = vp9_rb_read_literal(rb, QINDEX_BITS);
  update |= read_delta_q(rb, &cm->y_dc_delta_q);
  update |= read_delta_q(rb, &cm->uv_dc_delta_q);
  update |= read_delta_q(rb, &cm->uv_ac_delta_q);
  if (update)
    vp9_init_dequantizer(cm);
489
490
491
492
493

  xd->lossless = cm->base_qindex == 0 &&
                 cm->y_dc_delta_q == 0 &&
                 cm->uv_dc_delta_q == 0 &&
                 cm->uv_ac_delta_q == 0;
494
495
496

  xd->itxm_add = xd->lossless ? vp9_idct_add_lossless_c
                              : vp9_idct_add;
497
498
}

499
500
static INTERPOLATIONFILTERTYPE read_interp_filter_type(
    struct vp9_read_bit_buffer *rb) {
501
502
503
  const INTERPOLATIONFILTERTYPE literal_to_type[] = { EIGHTTAP_SMOOTH,
                                                      EIGHTTAP,
                                                      EIGHTTAP_SHARP };
504
  return vp9_rb_read_bit(rb) ? SWITCHABLE
505
                             : literal_to_type[vp9_rb_read_literal(rb, 2)];
506
507
}

508
static void read_frame_size(struct vp9_read_bit_buffer *rb,
509
                            int *width, int *height) {
510
511
  const int w = vp9_rb_read_literal(rb, 16) + 1;
  const int h = vp9_rb_read_literal(rb, 16) + 1;
512
513
  *width = w;
  *height = h;
514
515
}

516
static void setup_display_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
517
518
519
  cm->display_width = cm->width;
  cm->display_height = cm->height;
  if (vp9_rb_read_bit(rb))
520
    read_frame_size(rb, &cm->display_width, &cm->display_height);
521
}
522

523
524
static void apply_frame_size(VP9D_COMP *pbi, int width, int height) {
  VP9_COMMON *cm = &pbi->common;
525

526
  if (cm->width != width || cm->height != height) {
527
    if (!pbi->initial_width || !pbi->initial_height) {
528
529
      if (vp9_alloc_frame_buffers(cm, width, height))
        vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
530
                           "Failed to allocate frame buffers");
531
532
      pbi->initial_width = width;
      pbi->initial_height = height;
533
534
    } else {
      if (width > pbi->initial_width)
535
        vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
536
                           "Frame width too large");
537

538
      if (height > pbi->initial_height)
539
        vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
540
                           "Frame height too large");
541
542
    }

543
544
    cm->width = width;
    cm->height = height;
545

546
    vp9_update_frame_size(cm);
547
  }
548

549
550
  vp9_realloc_frame_buffer(&cm->yv12_fb[cm->new_fb_idx], cm->width, cm->height,
                           cm->subsampling_x, cm->subsampling_y,
551
                           VP9BORDERINPIXELS);
552
553
}

554
555
556
static void setup_frame_size(VP9D_COMP *pbi,
                             struct vp9_read_bit_buffer *rb) {
  int width, height;
557
558
  read_frame_size(rb, &width, &height);
  setup_display_size(&pbi->common, rb);
559
560
561
  apply_frame_size(pbi, width, height);
}

562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
static void setup_frame_size_with_refs(VP9D_COMP *pbi,
                                       struct vp9_read_bit_buffer *rb) {
  VP9_COMMON *const cm = &pbi->common;

  int width, height;
  int found = 0, i;
  for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) {
    if (vp9_rb_read_bit(rb)) {
      YV12_BUFFER_CONFIG *cfg = &cm->yv12_fb[cm->active_ref_idx[i]];
      width = cfg->y_crop_width;
      height = cfg->y_crop_height;
      found = 1;
      break;
    }
  }

  if (!found)
579
    read_frame_size(rb, &width, &height);
580

581
582
583
584
  if (!width || !height)
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
                       "Referenced frame with invalid size");

585
  setup_display_size(cm, rb);
586
587
588
  apply_frame_size(pbi, width, height);
}

589
static void decode_tile(VP9D_COMP *pbi, vp9_reader *r) {
590
  const int num_threads = pbi->oxcf.max_threads;
591
  VP9_COMMON *const pc = &pbi->common;
592
  int mi_row, mi_col;
593

594
  if (pbi->do_loopfilter_inline) {
595
596
597
598
599
600
601
    if (num_threads > 1) {
      LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
      lf_data->frame_buffer = &pbi->common.yv12_fb[pbi->common.new_fb_idx];
      lf_data->cm = pc;
      lf_data->xd = pbi->mb;
      lf_data->y_only = 0;
    }
602
603
604
    vp9_loop_filter_frame_init(pc, &pbi->mb, pbi->mb.lf.filter_level);
  }

605
606
  for (mi_row = pc->cur_tile_mi_row_start; mi_row < pc->cur_tile_mi_row_end;
       mi_row += MI_BLOCK_SIZE) {
607
    // For a SB there are 2 left contexts, each pertaining to a MB row within
608
    vpx_memset(&pc->left_context, 0, sizeof(pc->left_context));
609
    vpx_memset(pc->left_seg_context, 0, sizeof(pc->left_seg_context));
610
    for (mi_col = pc->cur_tile_mi_col_start; mi_col < pc->cur_tile_mi_col_end;
611
         mi_col += MI_BLOCK_SIZE) {
612
      decode_modes_sb(pbi, mi_row, mi_col, r, BLOCK_64X64);
613
614
615
616
617
618
    }

    if (pbi->do_loopfilter_inline) {
      // delay the loopfilter by 1 macroblock row.
      const int lf_start = mi_row - MI_BLOCK_SIZE;
      if (lf_start < 0) continue;
619
620
621
622
623
624
625
626
627
628
629
630
631
632

      if (num_threads > 1) {
        LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;

        vp9_worker_sync(&pbi->lf_worker);
        lf_data->start = lf_start;
        lf_data->stop = mi_row;
        pbi->lf_worker.hook = vp9_loop_filter_worker;
        vp9_worker_launch(&pbi->lf_worker);
      } else {
        YV12_BUFFER_CONFIG *const fb =
            &pbi->common.yv12_fb[pbi->common.new_fb_idx];
        vp9_loop_filter_rows(fb, pc, &pbi->mb, lf_start, mi_row, 0);
      }
633
634
635
636
637
    }
  }

  if (pbi->do_loopfilter_inline) {
    YV12_BUFFER_CONFIG *const fb = &pbi->common.yv12_fb[pbi->common.new_fb_idx];
638
639
640
641
642
    if (num_threads > 1) {
      // TODO(jzern): since the loop filter is delayed one mb row, this will be
      // forced to wait for the last row scheduled in the for loop.
      vp9_worker_sync(&pbi->lf_worker);
    }
643
644
    vp9_loop_filter_rows(fb, pc, &pbi->mb,
                         mi_row - MI_BLOCK_SIZE, pc->mi_rows, 0);
645
646
647
  }
}

648
static void setup_tile_info(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
649
650
  int min_log2_tile_cols, max_log2_tile_cols, max_ones;
  vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
651

Dmitry Kovalev's avatar
Dmitry Kovalev committed
652
653
654
655
656
  // columns
  max_ones = max_log2_tile_cols - min_log2_tile_cols;
  cm->log2_tile_cols = min_log2_tile_cols;
  while (max_ones-- && vp9_rb_read_bit(rb))
    cm->log2_tile_cols++;
657

Dmitry Kovalev's avatar
Dmitry Kovalev committed
658
  // rows
659
660
661
662
663
  cm->log2_tile_rows = vp9_rb_read_bit(rb);
  if (cm->log2_tile_rows)
    cm->log2_tile_rows += vp9_rb_read_bit(rb);
}

664
665
666
static const uint8_t *decode_tiles(VP9D_COMP *pbi, const uint8_t *data) {
  vp9_reader residual_bc;

667
668
  VP9_COMMON *const pc = &pbi->common;

669
670
  const uint8_t *const data_end = pbi->source + pbi->source_sz;
  const int aligned_mi_cols = mi_cols_aligned_to_sb(pc->mi_cols);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
671
672
  const int tile_cols = 1 << pc->log2_tile_cols;
  const int tile_rows = 1 << pc->log2_tile_rows;
673
  int tile_row, tile_col;
674

675
676
  // Note: this memset assumes above_context[0], [1] and [2]
  // are allocated as part of the same buffer.
677
678
  vpx_memset(pc->above_context[0], 0,
             sizeof(ENTROPY_CONTEXT) * 2 * MAX_MB_PLANE * aligned_mi_cols);
679

680
681
  vpx_memset(pc->above_seg_context, 0,
             sizeof(PARTITION_CONTEXT) * aligned_mi_cols);
682
683
684

  if (pbi->oxcf.inv_tile_order) {
    const uint8_t *data_ptr2[4][1 << 6];
685
    vp9_reader bc_bak = {0};
686
687

    // pre-initialize the offsets, we're going to read in inverse order
688
    data_ptr2[0][0] = data;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
689
    for (tile_row = 0; tile_row < tile_rows; tile_row++) {
690
      if (tile_row) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
691
692
693
        const int size = read_be32(data_ptr2[tile_row - 1][tile_cols - 1]);
        data_ptr2[tile_row - 1][tile_cols - 1] += 4;
        data_ptr2[tile_row][0] = data_ptr2[tile_row - 1][tile_cols - 1] + size;
694
695
      }

Dmitry Kovalev's avatar
Dmitry Kovalev committed
696
      for (tile_col = 1; tile_col < tile_cols; tile_col++) {
697
        const int size = read_be32(data_ptr2[tile_row][tile_col - 1]);
698
699
700
701
702
703
        data_ptr2[tile_row][tile_col - 1] += 4;
        data_ptr2[tile_row][tile_col] =
            data_ptr2[tile_row][tile_col - 1] + size;
      }
    }

Dmitry Kovalev's avatar
Dmitry Kovalev committed
704
    for (tile_row = 0; tile_row < tile_rows; tile_row++) {
705
      vp9_get_tile_row_offsets(pc, tile_row);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
706
      for (tile_col = tile_cols - 1; tile_col >= 0; tile_col--) {
707
        vp9_get_tile_col_offsets(pc, tile_col);
708
709
        setup_token_decoder(pbi, data_ptr2[tile_row][tile_col],
                            data_end - data_ptr2[tile_row][tile_col],
710
711
                            &residual_bc);
        decode_tile(pbi, &residual_bc);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
712
        if (tile_row == tile_rows - 1 && tile_col == tile_cols - 1)
713
          bc_bak = residual_bc;
714
715
      }
    }
716
    residual_bc = bc_bak;
717
718
719
  } else {
    int has_more;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
720
    for (tile_row = 0; tile_row < tile_rows; tile_row++) {
721
      vp9_get_tile_row_offsets(pc, tile_row);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
722
      for (tile_col = 0; tile_col < tile_cols; tile_col++) {
723
724
        size_t size;

725
726
        vp9_get_tile_col_offsets(pc, tile_col);

Dmitry Kovalev's avatar
Dmitry Kovalev committed
727
        has_more = tile_col < tile_cols - 1 || tile_row < tile_rows - 1;
728
        if (has_more) {
729
          if (!read_is_valid(data, 4, data_end))
730
731
732
            vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
                         "Truncated packet or corrupt tile length");

733
734
          size = read_be32(data);
          data += 4;
735
        } else {
736
          size = data_end - data;
737
        }
738

739
740
741
        setup_token_decoder(pbi, data, size, &residual_bc);
        decode_tile(pbi, &residual_bc);
        data += size;
742
743
744
      }
    }
  }
745
746

  return vp9_reader_find_end(&residual_bc);
747
748
}

749
750
751
752
753
754
755
756
static void check_sync_code(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
  if (vp9_rb_read_literal(rb, 8) != SYNC_CODE_0 ||
      vp9_rb_read_literal(rb, 8) != SYNC_CODE_1 ||
      vp9_rb_read_literal(rb, 8) != SYNC_CODE_2) {
    vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
                       "Invalid frame sync code");
  }
}
John Koleszar's avatar
John Koleszar committed
757

Frank Galligan's avatar
Frank Galligan committed
758
static void error_handler(void *data, size_t bit_offset) {
759
760
761
762
  VP9_COMMON *const cm = (VP9_COMMON *)data;
  vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet");
}

763
764
765
766
static void setup_inter_inter(VP9_COMMON *cm) {
  int i;

  cm->allow_comp_inter_inter = 0;
767
768
  for (i = 1; i < ALLOWED_REFS_PER_FRAME; ++i)
    cm->allow_comp_inter_inter |=
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
        cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1];

  if (cm->allow_comp_inter_inter) {
    // which one is always-on in comp inter-inter?
    if (cm->ref_frame_sign_bias[LAST_FRAME] ==
        cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
      cm->comp_fixed_ref = ALTREF_FRAME;
      cm->comp_var_ref[0] = LAST_FRAME;
      cm->comp_var_ref[1] = GOLDEN_FRAME;
    } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
               cm->ref_frame_sign_bias[ALTREF_FRAME]) {
      cm->comp_fixed_ref = GOLDEN_FRAME;
      cm->comp_var_ref[0] = LAST_FRAME;
      cm->comp_var_ref[1] = ALTREF_FRAME;
    } else {
      cm->comp_fixed_ref = LAST_FRAME;
      cm->comp_var_ref[0] = GOLDEN_FRAME;
      cm->comp_var_ref[1] = ALTREF_FRAME;
    }
  }
}

791
792
793
794
795
#define RESERVED \
  if (vp9_rb_read_bit(rb)) \
      vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, \
                         "Reserved bit must be unset")

796
797
static size_t read_uncompressed_header(VP9D_COMP *pbi,
                                       struct vp9_read_bit_buffer *rb) {
798
  VP9_COMMON *const cm = &pbi->common;
799
  MACROBLOCKD *const xd = &pbi->mb;