vp9_decodframe.c 32.6 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 setup_plane_dequants(VP9_COMMON *cm, MACROBLOCKD *xd, int q_index) {
John Koleszar's avatar
John Koleszar committed
81
  int i;
82
  xd->plane[0].dequant = cm->y_dequant[q_index];
83
  for (i = 1; i < MAX_MB_PLANE; i++)
84
    xd->plane[i].dequant = cm->uv_dequant[q_index];
John Koleszar's avatar
John Koleszar committed
85
86
}

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

123
static void decode_block_intra(int plane, int block, BLOCK_SIZE plane_bsize,
124
                               TX_SIZE tx_size, void *arg) {
125
  MACROBLOCKD* const xd = arg;
126
  struct macroblockd_plane *const pd = &xd->plane[plane];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
127
  MODE_INFO *const mi = xd->mode_info_context;
128
129
130
  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,
131
                                                 pd->dst.buf, pd->dst.stride);
132
133
134
135
  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;
136

137
  if (xd->mb_to_right_edge < 0 || xd->mb_to_bottom_edge < 0)
138
    extend_for_intra(xd, plane_bsize, plane, block, tx_size);
139

140
141
142
  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);
143

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

148
static int decode_tokens(VP9D_COMP *pbi, BLOCK_SIZE bsize, vp9_reader *r) {
149
  VP9_COMMON *const cm = &pbi->common;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
150
  MACROBLOCKD *const xd = &pbi->mb;
151
  MB_MODE_INFO *const mbmi = &xd->mode_info_context->mbmi;
152

153
154
  if (mbmi->skip_coeff) {
    reset_skip_context(xd, bsize);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
155
    return -1;
156
  } else {
157
158
159
    if (cm->seg.enabled)
      setup_plane_dequants(cm, xd, vp9_get_qindex(&cm->seg, mbmi->segment_id,
                                                  cm->base_qindex));
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
  xd->mode_info_stride = cm->mode_info_stride;
177
178
  // Special case: if prev_mi is NULL, the previous mode info context
  // cannot be used.
Dmitry Kovalev's avatar
Dmitry Kovalev committed
179
  xd->prev_mode_info_context = cm->prev_mi ? cm->prev_mi + offset : NULL;
180

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

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

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

191
static void set_ref(VP9D_COMP *pbi, int i, int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
192
193
  VP9_COMMON *const cm = &pbi->common;
  MACROBLOCKD *const xd = &pbi->mb;
194
  MB_MODE_INFO *const mbmi = &xd->mode_info_context->mbmi;
195
  const int ref = mbmi->ref_frame[i] - LAST_FRAME;
196
  const YV12_BUFFER_CONFIG *cfg = &cm->yv12_fb[cm->active_ref_idx[ref]];
197
198
199
200
201
202
203
  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);
204
  xd->corrupted |= cfg->corrupted;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
205
}
John Koleszar's avatar
John Koleszar committed
206

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

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

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

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

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

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

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

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

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

265
  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
266
267
    return;

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

    if (idx == 0)
      partition = treed_read(r, vp9_partition_tree,
280
                             cm->fc.partition_prob[cm->frame_type][pl]);
281
    else if (idx > 0 &&
282
        !vp9_read(r, cm->fc.partition_prob[cm->frame_type][pl][idx]))
283
284
285
286
      partition = (idx == 1) ? PARTITION_HORZ : PARTITION_VERT;
    else
      partition = PARTITION_SPLIT;

287
    cm->counts.partition[pl][partition]++;
288
289
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

410
    vp9_clearall_segfeatures(seg);
411

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

530
    vp9_update_frame_size(cm);
531
  }
532

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

656
  VP9_COMMON *const cm = &pbi->common;
657

658
  const uint8_t *const data_end = pbi->source + pbi->source_sz;
659
660
661
  const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  const int tile_cols = 1 << cm->log2_tile_cols;
  const int tile_rows = 1 << cm->log2_tile_rows;
662
  int tile_row, tile_col;
663

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

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

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

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

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

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

714
        vp9_get_tile_col_offsets(cm, tile_col);
715

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

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

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

  return vp9_reader_find_end(&residual_bc);
736
737
}

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