vp9_decodframe.c 32.7 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, 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, 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, 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(&cm->seg, 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
static void decode_block(int plane, int block, BLOCK_SIZE plane_bsize,
91
                         TX_SIZE tx_size, void *arg) {
92
  MACROBLOCKD* const xd = arg;
93
  struct macroblockd_plane *const pd = &xd->plane[plane];
94
  int16_t* const qcoeff = BLOCK_OFFSET(pd->qcoeff, block);
95
  const int stride = pd->dst.stride;
96
  const int eob = pd->eobs[block];
97
98
99
  const int raster_block = txfrm_block_to_raster_block(plane_bsize, tx_size,
                                                       block);
  uint8_t* const dst = raster_block_offset_uint8(plane_bsize, raster_block,
100
                                                 pd->dst.buf, stride);
101
  switch (tx_size) {
102
103
    case TX_4X4: {
      const TX_TYPE tx_type = get_tx_type_4x4(pd->plane_type, xd, raster_block);
104
      if (tx_type == DCT_DCT)
105
        xd->itxm_add(qcoeff, dst, stride, eob);
106
      else
107
        vp9_iht_add_c(tx_type, qcoeff, dst, stride, eob);
108
      break;
109
    }
110
    case TX_8X8:
111
112
      vp9_iht_add_8x8_c(get_tx_type_8x8(pd->plane_type, xd), qcoeff, dst,
                        stride, eob);
113
114
      break;
    case TX_16X16:
115
116
      vp9_iht_add_16x16_c(get_tx_type_16x16(pd->plane_type, xd), qcoeff, dst,
                          stride, eob);
117
118
      break;
    case TX_32X32:
119
      vp9_idct_add_32x32(qcoeff, dst, stride, eob);
120
      break;
121
122
    default:
      assert(!"Invalid transform size");
123
124
125
  }
}

126
static void decode_block_intra(int plane, int block, BLOCK_SIZE plane_bsize,
127
                               TX_SIZE tx_size, void *arg) {
128
  MACROBLOCKD* const xd = arg;
129
  struct macroblockd_plane *const pd = &xd->plane[plane];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
130
  MODE_INFO *const mi = xd->mode_info_context;
131
132
133
  const int raster_block = txfrm_block_to_raster_block(plane_bsize, tx_size,
                                                       block);
  uint8_t* const dst = raster_block_offset_uint8(plane_bsize, raster_block,
134
                                                 pd->dst.buf, pd->dst.stride);
135
136
137
138
  const MB_PREDICTION_MODE mode = (plane == 0)
        ? ((mi->mbmi.sb_type < BLOCK_8X8) ? mi->bmi[raster_block].as_mode
                                          : mi->mbmi.mode)
        : mi->mbmi.uv_mode;
139

140
  if (xd->mb_to_right_edge < 0 || xd->mb_to_bottom_edge < 0)
141
    extend_for_intra(xd, plane_bsize, plane, block, tx_size);
142

143
144
145
  vp9_predict_intra_block(xd, raster_block >> tx_size,
                          b_width_log2(plane_bsize), tx_size, mode,
                          dst, pd->dst.stride, dst, pd->dst.stride);
146

Dmitry Kovalev's avatar
Dmitry Kovalev committed
147
148
  if (!mi->mbmi.skip_coeff)
    decode_block(plane, block, plane_bsize, tx_size, arg);
149
150
}

151
static int decode_tokens(VP9D_COMP *pbi, BLOCK_SIZE bsize, vp9_reader *r) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
152
  MACROBLOCKD *const xd = &pbi->mb;
153

Paul Wilkins's avatar
Paul Wilkins committed
154
  if (xd->mode_info_context->mbmi.skip_coeff) {
155
      reset_skip_context(xd, bsize);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
156
    return -1;
157
  } else {
158
    if (pbi->common.seg.enabled)
159
      init_dequantizer(&pbi->common, xd);
160

Dmitry Kovalev's avatar
Dmitry Kovalev committed
161
162
    // TODO(dkovalev) if (!vp9_reader_has_error(r))
    return vp9_decode_tokens(pbi, r, bsize);
163
164
165
  }
}

166
static void set_offsets(VP9D_COMP *pbi, BLOCK_SIZE bsize,
167
                        int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
168
169
  VP9_COMMON *const cm = &pbi->common;
  MACROBLOCKD *const xd = &pbi->mb;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
170
171
172
  const int bh = num_8x8_blocks_high_lookup[bsize];
  const int bw = num_8x8_blocks_wide_lookup[bsize];
  const int offset = mi_row * cm->mode_info_stride + mi_col;
173

Dmitry Kovalev's avatar
Dmitry Kovalev committed
174
  xd->mode_info_context = cm->mi + offset;
175
  xd->mode_info_context->mbmi.sb_type = bsize;
176
177
  // Special case: if prev_mi is NULL, the previous mode info context
  // cannot be used.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
178
  xd->prev_mode_info_context = cm->prev_mi ? cm->prev_mi + offset : NULL;
179

180
  set_skip_context(cm, xd, mi_row, mi_col);
181
  set_partition_seg_context(cm, xd, mi_row, mi_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
182

183
184
  // 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
185
  set_mi_row_col(cm, xd, mi_row, bh, mi_col, bw);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
186

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

190
static void set_ref(VP9D_COMP *pbi, int i, int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
191
192
  VP9_COMMON *const cm = &pbi->common;
  MACROBLOCKD *const xd = &pbi->mb;
193
  MB_MODE_INFO *const mbmi = &xd->mode_info_context->mbmi;
194
  const int ref = mbmi->ref_frame[i] - LAST_FRAME;
195
  const YV12_BUFFER_CONFIG *cfg = &cm->yv12_fb[cm->active_ref_idx[ref]];
196
197
198
199
200
201
202
  const struct scale_factors *sf = &cm->active_ref_scale[ref];
  if (!vp9_is_valid_scale(sf))
    vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
                       "Invalid scale factors");

  xd->scale_factor[i] = *sf;
  setup_pre_planes(xd, i, cfg, mi_row, mi_col, sf);
203
  xd->corrupted |= cfg->corrupted;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
204
}
John Koleszar's avatar
John Koleszar committed
205

206
static void decode_modes_b(VP9D_COMP *pbi, int mi_row, int mi_col,
207
                           vp9_reader *r, BLOCK_SIZE bsize) {
208
  VP9_COMMON *const cm = &pbi->common;
209
  MACROBLOCKD *const xd = &pbi->mb;
210
  const int less8x8 = bsize < BLOCK_8X8;
211
  MB_MODE_INFO *mbmi;
212

213
  if (less8x8)
214
215
    if (xd->ab_index > 0)
      return;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
216

217
  set_offsets(pbi, bsize, mi_row, mi_col);
218
  vp9_read_mode_info(pbi, mi_row, mi_col, r);
219

220
  if (less8x8)
221
    bsize = BLOCK_8X8;
222
223
224
225

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

226
  if (!is_inter_block(mbmi)) {
227
228
229
    // Intra reconstruction
    decode_tokens(pbi, bsize, r);
    foreach_transformed_block(xd, bsize, decode_block_intra, xd);
230
  } else {
231
232
    // Inter reconstruction
    int eobtotal;
233
234

    set_ref(pbi, 0, mi_row, mi_col);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
235
    if (has_second_ref(mbmi))
236
237
      set_ref(pbi, 1, mi_row, mi_col);

238
239
240
241
242
243
244
245
246
    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)
247
248
        // skip loopfilter
        vp9_set_pred_flag_mbskip(cm, bsize, mi_row, mi_col, 1);
249
250
251
      else if (eobtotal > 0)
        foreach_transformed_block(xd, bsize, decode_block, xd);
    }
252
  }
253
  xd->corrupted |= vp9_reader_has_error(r);
254
255
}

256
static void decode_modes_sb(VP9D_COMP *pbi, int mi_row, int mi_col,
257
                            vp9_reader* r, BLOCK_SIZE bsize) {
258
259
  VP9_COMMON *const pc = &pbi->common;
  MACROBLOCKD *const xd = &pbi->mb;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
260
  const int hbs = num_8x8_blocks_wide_lookup[bsize] / 2;
261
  PARTITION_TYPE partition = PARTITION_NONE;
262
  BLOCK_SIZE subsize;
263

264
  if (mi_row >= pc->mi_rows || mi_col >= pc->mi_cols)
265
266
    return;

267
  if (bsize < BLOCK_8X8) {
268
269
    if (xd->ab_index != 0)
      return;
270
  } else {
271
    int pl;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
272
    const int idx = check_bsize_coverage(hbs, pc->mi_rows, pc->mi_cols,
Dmitry Kovalev's avatar
Dmitry Kovalev committed
273
                                         mi_row, mi_col);
274
    set_partition_seg_context(pc, xd, mi_row, mi_col);
275
    pl = partition_plane_context(xd, bsize);
276
277
278
279
280
281
282
283
284
285

    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;

286
    pc->counts.partition[pl][partition]++;
287
288
  }

289
  subsize = get_subsize(bsize, partition);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
290
  *get_sb_index(xd, subsize) = 0;
291

292
293
  switch (partition) {
    case PARTITION_NONE:
294
      decode_modes_b(pbi, mi_row, mi_col, r, subsize);
295
296
      break;
    case PARTITION_HORZ:
297
      decode_modes_b(pbi, mi_row, mi_col, r, subsize);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
298
299
300
      *get_sb_index(xd, subsize) = 1;
      if (mi_row + hbs < pc->mi_rows)
        decode_modes_b(pbi, mi_row + hbs, mi_col, r, subsize);
301
302
      break;
    case PARTITION_VERT:
303
      decode_modes_b(pbi, mi_row, mi_col, r, subsize);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
304
305
306
      *get_sb_index(xd, subsize) = 1;
      if (mi_col + hbs < pc->mi_cols)
        decode_modes_b(pbi, mi_row, mi_col + hbs, r, subsize);
307
      break;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
308
309
    case PARTITION_SPLIT: {
      int n;
310
      for (n = 0; n < 4; n++) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
311
        const int j = n >> 1, i = n & 1;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
312
313
        *get_sb_index(xd, subsize) = n;
        decode_modes_sb(pbi, mi_row + j * hbs, mi_col + i * hbs, r, subsize);
314
      }
Dmitry Kovalev's avatar
Dmitry Kovalev committed
315
    } break;
316
    default:
317
      assert(!"Invalid partition type");
318
  }
319

320
  // update partition context
321
322
  if (bsize >= BLOCK_8X8 &&
      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT)) {
323
324
325
    set_partition_seg_context(pc, xd, mi_row, mi_col);
    update_partition_context(xd, subsize, bsize);
  }
326
327
}

328
static void setup_token_decoder(VP9D_COMP *pbi,
329
                                const uint8_t *data, size_t read_size,
330
                                vp9_reader *r) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
331
  VP9_COMMON *pc = &pbi->common;
332
  const uint8_t *data_end = pbi->source + pbi->source_sz;
John Koleszar's avatar
John Koleszar committed
333

Dmitry Kovalev's avatar
Dmitry Kovalev committed
334
335
336
  // 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.
337
  if (!read_is_valid(data, read_size, data_end))
John Koleszar's avatar
John Koleszar committed
338
    vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
339
                       "Truncated packet or corrupt tile length");
John Koleszar's avatar
John Koleszar committed
340

341
  if (vp9_reader_init(r, data, read_size))
John Koleszar's avatar
John Koleszar committed
342
343
    vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate bool decoder %d", 1);
John Koleszar's avatar
John Koleszar committed
344
345
}

346
static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
347
                                   vp9_reader *r) {
348
349
350
351
352
353
354
355
356
357
358
  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]);
359
}
360

361
static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode,
362
                            vp9_reader *r) {
363
  read_coef_probs_common(fc->coef_probs[TX_4X4], r);
364

365
  if (tx_mode > ONLY_4X4)
366
    read_coef_probs_common(fc->coef_probs[TX_8X8], r);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
367

368
  if (tx_mode > ALLOW_8X8)
369
    read_coef_probs_common(fc->coef_probs[TX_16X16], r);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
370

371
  if (tx_mode > ALLOW_16X16)
372
    read_coef_probs_common(fc->coef_probs[TX_32X32], r);
373
374
}

375
376
static void setup_segmentation(struct segmentation *seg,
                               struct vp9_read_bit_buffer *rb) {
377
378
  int i, j;

379
380
  seg->update_map = 0;
  seg->update_data = 0;
381

382
383
  seg->enabled = vp9_rb_read_bit(rb);
  if (!seg->enabled)
384
385
386
    return;

  // Segmentation map update
387
388
  seg->update_map = vp9_rb_read_bit(rb);
  if (seg->update_map) {
Paul Wilkins's avatar
Paul Wilkins committed
389
    for (i = 0; i < SEG_TREE_PROBS; i++)
390
391
      seg->tree_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
                                               : MAX_PROB;
392

393
394
    seg->temporal_update = vp9_rb_read_bit(rb);
    if (seg->temporal_update) {
395
      for (i = 0; i < PREDICTION_PROBS; i++)
396
397
        seg->pred_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
                                                 : MAX_PROB;
398
399
    } else {
      for (i = 0; i < PREDICTION_PROBS; i++)
400
        seg->pred_probs[i] = MAX_PROB;
401
    }
402
  }
403

404
  // Segmentation data update
405
406
407
  seg->update_data = vp9_rb_read_bit(rb);
  if (seg->update_data) {
    seg->abs_delta = vp9_rb_read_bit(rb);
408

409
    vp9_clearall_segfeatures(seg);
410

Paul Wilkins's avatar
Paul Wilkins committed
411
    for (i = 0; i < MAX_SEGMENTS; i++) {
412
413
      for (j = 0; j < SEG_LVL_MAX; j++) {
        int data = 0;
414
        const int feature_enabled = vp9_rb_read_bit(rb);
415
        if (feature_enabled) {
416
          vp9_enable_segfeature(seg, i, j);
417
          data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
418
          if (vp9_is_segfeature_signed(j))
419
            data = vp9_rb_read_bit(rb) ? -data : data;
420
        }
421
        vp9_set_segdata(seg, i, j, data);
422
423
424
425
426
      }
    }
  }
}

427
428
static void setup_loopfilter(struct loopfilter *lf,
                             struct vp9_read_bit_buffer *rb) {
429

430
431
  lf->filter_level = vp9_rb_read_literal(rb, 6);
  lf->sharpness_level = vp9_rb_read_literal(rb, 3);
432
433
434

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

437
438
439
440
  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) {
441
442
      int i;

443
444
      for (i = 0; i < MAX_REF_LF_DELTAS; i++)
        if (vp9_rb_read_bit(rb))
445
          lf->ref_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
446

447
448
      for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
        if (vp9_rb_read_bit(rb))
449
          lf->mode_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
450
451
452
453
    }
  }
}

454
455
static int read_delta_q(struct vp9_read_bit_buffer *rb, int *delta_q) {
  const int old = *delta_q;
456
457
  if (vp9_rb_read_bit(rb))
    *delta_q = vp9_rb_read_signed_literal(rb, 4);
458
459
  return old != *delta_q;
}
460

461
static void setup_quantization(VP9D_COMP *pbi, struct vp9_read_bit_buffer *rb) {
462
  MACROBLOCKD *const xd = &pbi->mb;
463
464
  VP9_COMMON *const cm = &pbi->common;
  int update = 0;
465

466
467
468
469
470
471
  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);
472
473
474
475
476

  xd->lossless = cm->base_qindex == 0 &&
                 cm->y_dc_delta_q == 0 &&
                 cm->uv_dc_delta_q == 0 &&
                 cm->uv_ac_delta_q == 0;
477
478
479

  xd->itxm_add = xd->lossless ? vp9_idct_add_lossless_c
                              : vp9_idct_add;
480
481
}

482
483
static INTERPOLATIONFILTERTYPE read_interp_filter_type(
    struct vp9_read_bit_buffer *rb) {
484
485
486
  const INTERPOLATIONFILTERTYPE literal_to_type[] = { EIGHTTAP_SMOOTH,
                                                      EIGHTTAP,
                                                      EIGHTTAP_SHARP };
487
  return vp9_rb_read_bit(rb) ? SWITCHABLE
488
                             : literal_to_type[vp9_rb_read_literal(rb, 2)];
489
490
}

491
static void read_frame_size(struct vp9_read_bit_buffer *rb,
492
                            int *width, int *height) {
493
494
  const int w = vp9_rb_read_literal(rb, 16) + 1;
  const int h = vp9_rb_read_literal(rb, 16) + 1;
495
496
  *width = w;
  *height = h;
497
498
}

499
static void setup_display_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
500
501
502
  cm->display_width = cm->width;
  cm->display_height = cm->height;
  if (vp9_rb_read_bit(rb))
503
    read_frame_size(rb, &cm->display_width, &cm->display_height);
504
}
505

506
507
static void apply_frame_size(VP9D_COMP *pbi, int width, int height) {
  VP9_COMMON *cm = &pbi->common;
508

509
  if (cm->width != width || cm->height != height) {
510
    if (!pbi->initial_width || !pbi->initial_height) {
511
512
      if (vp9_alloc_frame_buffers(cm, width, height))
        vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
513
                           "Failed to allocate frame buffers");
514
515
      pbi->initial_width = width;
      pbi->initial_height = height;
516
517
    } else {
      if (width > pbi->initial_width)
518
        vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
519
                           "Frame width too large");
520

521
      if (height > pbi->initial_height)
522
        vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
523
                           "Frame height too large");
524
525
    }

526
527
    cm->width = width;
    cm->height = height;
528

529
    vp9_update_frame_size(cm);
530
  }
531

532
533
  vp9_realloc_frame_buffer(&cm->yv12_fb[cm->new_fb_idx], cm->width, cm->height,
                           cm->subsampling_x, cm->subsampling_y,
534
                           VP9BORDERINPIXELS);
535
536
}

537
538
539
static void setup_frame_size(VP9D_COMP *pbi,
                             struct vp9_read_bit_buffer *rb) {
  int width, height;
540
  read_frame_size(rb, &width, &height);
541
  apply_frame_size(pbi, width, height);
542
  setup_display_size(&pbi->common, rb);
543
544
}

545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
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)
562
    read_frame_size(rb, &width, &height);
563

564
565
566
567
  if (!width || !height)
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
                       "Referenced frame with invalid size");

568
  apply_frame_size(pbi, width, height);
569
  setup_display_size(cm, rb);
570
571
}

572
static void decode_tile(VP9D_COMP *pbi, vp9_reader *r) {
573
  const int num_threads = pbi->oxcf.max_threads;
574
  VP9_COMMON *const pc = &pbi->common;
575
  int mi_row, mi_col;
576
  YV12_BUFFER_CONFIG *const fb = &pc->yv12_fb[pc->new_fb_idx];
577

578
  if (pbi->do_loopfilter_inline) {
579
580
    if (num_threads > 1) {
      LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
581
      lf_data->frame_buffer = fb;
582
583
      lf_data->cm = pc;
      lf_data->xd = pbi->mb;
584
      lf_data->stop = 0;
585
586
      lf_data->y_only = 0;
    }
587
    vp9_loop_filter_frame_init(pc, pc->lf.filter_level);
588
589
  }

590
591
  for (mi_row = pc->cur_tile_mi_row_start; mi_row < pc->cur_tile_mi_row_end;
       mi_row += MI_BLOCK_SIZE) {
592
    // For a SB there are 2 left contexts, each pertaining to a MB row within
593
594
    vp9_zero(pc->left_context);
    vp9_zero(pc->left_seg_context);
595
    for (mi_col = pc->cur_tile_mi_col_start; mi_col < pc->cur_tile_mi_col_end;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
596
         mi_col += MI_BLOCK_SIZE)
597
      decode_modes_sb(pbi, mi_row, mi_col, r, BLOCK_64X64);
598
599
600
601
602

    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;
603
604
605
606

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

607
608
609
        // decoding has completed: finish up the loop filter in this thread.
        if (mi_row + MI_BLOCK_SIZE >= pc->cur_tile_mi_row_end) continue;

610
611
612
613
614
615
616
617
        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 {
        vp9_loop_filter_rows(fb, pc, &pbi->mb, lf_start, mi_row, 0);
      }
618
619
620
621
    }
  }

  if (pbi->do_loopfilter_inline) {
622
    int lf_start;
623
    if (num_threads > 1) {
624
625
      LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;

626
      vp9_worker_sync(&pbi->lf_worker);
627
628
629
      lf_start = lf_data->stop;
    } else {
      lf_start = mi_row - MI_BLOCK_SIZE;
630
    }
631
    vp9_loop_filter_rows(fb, pc, &pbi->mb,
632
                         lf_start, pc->mi_rows, 0);
633
634
635
  }
}

636
static void setup_tile_info(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
637
638
  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);
639

Dmitry Kovalev's avatar
Dmitry Kovalev committed
640
641
642
643
644
  // 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++;
645

Dmitry Kovalev's avatar
Dmitry Kovalev committed
646
  // rows
647
648
649
650
651
  cm->log2_tile_rows = vp9_rb_read_bit(rb);
  if (cm->log2_tile_rows)
    cm->log2_tile_rows += vp9_rb_read_bit(rb);
}

652
653
654
static const uint8_t *decode_tiles(VP9D_COMP *pbi, const uint8_t *data) {
  vp9_reader residual_bc;

655
656
  VP9_COMMON *const pc = &pbi->common;

657
658
  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
659
660
  const int tile_cols = 1 << pc->log2_tile_cols;
  const int tile_rows = 1 << pc->log2_tile_rows;
661
  int tile_row, tile_col;
662

663
664
  // Note: this memset assumes above_context[0], [1] and [2]
  // are allocated as part of the same buffer.
665
  vpx_memset(pc->above_context[0], 0,
666
             sizeof(ENTROPY_CONTEXT) * MAX_MB_PLANE * (2 * aligned_mi_cols));
667

668
669
  vpx_memset(pc->above_seg_context, 0,
             sizeof(PARTITION_CONTEXT) * aligned_mi_cols);
670
671
672

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

    // pre-initialize the offsets, we're going to read in inverse order
676
    data_ptr2[0][0] = data;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
677
    for (tile_row = 0; tile_row < tile_rows; tile_row++) {
678
      if (tile_row) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
679
680
681
        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;
682
683
      }

Dmitry Kovalev's avatar
Dmitry Kovalev committed
684
      for (tile_col = 1; tile_col < tile_cols; tile_col++) {
685
        const int size = read_be32(data_ptr2[tile_row][tile_col - 1]);
686
687
688
689
690
691
        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
692
    for (tile_row = 0; tile_row < tile_rows; tile_row++) {
693
      vp9_get_tile_row_offsets(pc, tile_row);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
694
      for (tile_col = tile_cols - 1; tile_col >= 0; tile_col--) {
695
        vp9_get_tile_col_offsets(pc, tile_col);
696
697
        setup_token_decoder(pbi, data_ptr2[tile_row][tile_col],
                            data_end - data_ptr2[tile_row][tile_col],
698
699
                            &residual_bc);
        decode_tile(pbi, &residual_bc);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
700
        if (tile_row == tile_rows - 1 && tile_col == tile_cols - 1)
701
          bc_bak = residual_bc;
702
703
      }
    }
704
    residual_bc = bc_bak;
705
706
707
  } else {
    int has_more;

Dmitry Kovalev's avatar
Dmitry Kovalev committed
708
    for (tile_row = 0; tile_row < tile_rows; tile_row++) {
709
      vp9_get_tile_row_offsets(pc, tile_row);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
710
      for (tile_col = 0; tile_col < tile_cols; tile_col++) {
711
712
        size_t size;

713
714
        vp9_get_tile_col_offsets(pc, tile_col);

Dmitry Kovalev's avatar
Dmitry Kovalev committed
715
        has_more = tile_col < tile_cols - 1 || tile_row < tile_rows - 1;
716
        if (has_more) {
717
          if (!read_is_valid(data, 4, data_end))
718
719
720
            vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
                         "Truncated packet or corrupt tile length");

721
722
          size = read_be32(data);
          data += 4;
723
        } else {
724
          size = data_end - data;
725
        }
726

727
728
729
        setup_token_decoder(pbi, data, size, &residual_bc);
        decode_tile(pbi, &residual_bc);
        data += size;
730
731
732
      }
    }
  }
733
734

  return vp9_reader_find_end(&residual_bc);
735
736
}

737
738
739
740
741
742
743
744
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
745

Frank Galligan's avatar
Frank Galligan committed
746
static void error_handler(void *data, size_t bit_offset) {
747
748
749
750
  VP9_COMMON *const cm = (VP9_COMMON *)data;
  vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet");
}

751
752
753
754
static void setup_inter_inter(VP9_COMMON *cm) {
  int i;

  cm->allow_comp_inter_inter = 0;
755
756
  for (i = 1; i < ALLOWED_REFS_PER_FRAME; ++i)
    cm->allow_comp_inter_inter |=
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
        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;
    }
  }
}

779
780
781
782
783
#define RESERVED \
  if (vp9_rb_read_bit(rb)) \
      vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, \
                         "Reserved bit must be unset")