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, 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(&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_TYPE 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
127
static void decode_block_intra(int plane, int block,
                               BLOCK_SIZE_TYPE plane_bsize,
128
                               TX_SIZE tx_size, void *arg) {
129
  MACROBLOCKD* const xd = arg;
130
  struct macroblockd_plane *const pd = &xd->plane[plane];
Dmitry Kovalev's avatar
Dmitry Kovalev committed
131
  MODE_INFO *const mi = xd->mode_info_context;
132
133
134
  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,
135
136
137
                                                 pd->dst.buf, pd->dst.stride);
  int b_mode;
  const int tx_ib = raster_block >> tx_size;
138
  const int mode = (plane == 0) ? mi->mbmi.mode : mi->mbmi.uv_mode;
139

140
  if (plane == 0 && mi->mbmi.sb_type < BLOCK_8X8) {
141
    assert(plane_bsize == BLOCK_8X8);
142
    b_mode = mi->bmi[raster_block].as_mode;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
143
  } else {
144
    b_mode = mode;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
145
  }
146

147
  if (xd->mb_to_right_edge < 0 || xd->mb_to_bottom_edge < 0)
148
    extend_for_intra(xd, plane_bsize, plane, block, tx_size);
149

150
  vp9_predict_intra_block(xd, tx_ib, b_width_log2(plane_bsize), tx_size, b_mode,
151
                          dst, pd->dst.stride,
152
                          dst, pd->dst.stride);
153

154
  // Early exit if there are no coefficients
Paul Wilkins's avatar
Paul Wilkins committed
155
  if (mi->mbmi.skip_coeff)
156
157
    return;

158
  decode_block(plane, block, plane_bsize, tx_size, arg);
159
160
}

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

Paul Wilkins's avatar
Paul Wilkins committed
164
  if (xd->mode_info_context->mbmi.skip_coeff) {
165
      reset_skip_context(xd, bsize);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
166
    return -1;
167
  } else {
168
    if (pbi->common.seg.enabled)
169
      init_dequantizer(&pbi->common, xd);
170

Dmitry Kovalev's avatar
Dmitry Kovalev committed
171
172
    // TODO(dkovalev) if (!vp9_reader_has_error(r))
    return vp9_decode_tokens(pbi, r, bsize);
173
174
175
  }
}

176
static void set_offsets(VP9D_COMP *pbi, BLOCK_SIZE_TYPE bsize,
177
                        int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
178
179
  VP9_COMMON *const cm = &pbi->common;
  MACROBLOCKD *const xd = &pbi->mb;
180
181
  const int bh = 1 << mi_height_log2(bsize);
  const int bw = 1 << mi_width_log2(bsize);
182
  const int mi_idx = mi_row * cm->mode_info_stride + mi_col;
183

184
  xd->mode_info_context = cm->mi + mi_idx;
185
  xd->mode_info_context->mbmi.sb_type = bsize;
186
187
  // Special case: if prev_mi is NULL, the previous mode info context
  // cannot be used.
188
  xd->prev_mode_info_context = cm->prev_mi ? cm->prev_mi + mi_idx : NULL;
189

190

191
  set_skip_context(cm, xd, mi_row, mi_col);
192
  set_partition_seg_context(cm, xd, mi_row, mi_col);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
193

194
195
  // 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
196
  set_mi_row_col(cm, xd, mi_row, bh, mi_col, bw);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
197

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

201
static void set_ref(VP9D_COMP *pbi, int i, int mi_row, int mi_col) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
202
203
  VP9_COMMON *const cm = &pbi->common;
  MACROBLOCKD *const xd = &pbi->mb;
204
  MB_MODE_INFO *const mbmi = &xd->mode_info_context->mbmi;
205
  const int ref = mbmi->ref_frame[i] - 1;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
206

207
208
  const YV12_BUFFER_CONFIG *cfg = &cm->yv12_fb[cm->active_ref_idx[ref]];
  xd->scale_factor[i] = cm->active_ref_scale[ref];
209
  setup_pre_planes(xd, i, cfg, mi_row, mi_col, &xd->scale_factor[i]);
210
  xd->corrupted |= cfg->corrupted;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
211
}
John Koleszar's avatar
John Koleszar committed
212

213
static void decode_modes_b(VP9D_COMP *pbi, int mi_row, int mi_col,
214
                           vp9_reader *r, BLOCK_SIZE_TYPE bsize) {
215
  VP9_COMMON *const cm = &pbi->common;
216
  MACROBLOCKD *const xd = &pbi->mb;
217
  const int less8x8 = bsize < BLOCK_8X8;
218
  MB_MODE_INFO *mbmi;
219

220
  if (less8x8)
221
222
    if (xd->ab_index > 0)
      return;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
223

224
  set_offsets(pbi, bsize, mi_row, mi_col);
225
  vp9_read_mode_info(pbi, mi_row, mi_col, r);
226

227
  if (less8x8)
228
    bsize = BLOCK_8X8;
229
230
231
232

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

233
  if (!is_inter_block(mbmi)) {
234
235
236
    // Intra reconstruction
    decode_tokens(pbi, bsize, r);
    foreach_transformed_block(xd, bsize, decode_block_intra, xd);
237
  } else {
238
239
    // Inter reconstruction
    int eobtotal;
240
241
242
243
244

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

245
246
247
248
249
250
251
252
253
    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)
254
255
        // skip loopfilter
        vp9_set_pred_flag_mbskip(cm, bsize, mi_row, mi_col, 1);
256
257
258
      else if (eobtotal > 0)
        foreach_transformed_block(xd, bsize, decode_block, xd);
    }
259
  }
260
  xd->corrupted |= vp9_reader_has_error(r);
261
262
}

263
static void decode_modes_sb(VP9D_COMP *pbi, int mi_row, int mi_col,
264
                            vp9_reader* r, BLOCK_SIZE_TYPE bsize) {
265
266
  VP9_COMMON *const pc = &pbi->common;
  MACROBLOCKD *const xd = &pbi->mb;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
267
  const int bs = (1 << mi_width_log2(bsize)) / 2;
268
269
270
  PARTITION_TYPE partition = PARTITION_NONE;
  BLOCK_SIZE_TYPE subsize;

271
  if (mi_row >= pc->mi_rows || mi_col >= pc->mi_cols)
272
273
    return;

274
  if (bsize < BLOCK_8X8) {
275
276
    if (xd->ab_index != 0)
      return;
277
  } else {
278
    int pl;
Dmitry Kovalev's avatar
Dmitry Kovalev committed
279
280
    const int idx = check_bsize_coverage(bs, pc->mi_rows, pc->mi_cols,
                                         mi_row, mi_col);
281
    set_partition_seg_context(pc, xd, mi_row, mi_col);
282
    pl = partition_plane_context(xd, bsize);
283
284
285
286
287
288
289
290
291
292

    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;

293
    pc->counts.partition[pl][partition]++;
294
295
  }

296
  subsize = get_subsize(bsize, partition);
297
  *(get_sb_index(xd, subsize)) = 0;
298

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

327
  // update partition context
328
329
  if (bsize >= BLOCK_8X8 &&
      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT)) {
330
331
332
    set_partition_seg_context(pc, xd, mi_row, mi_col);
    update_partition_context(xd, subsize, bsize);
  }
333
334
}

335
static void setup_token_decoder(VP9D_COMP *pbi,
336
                                const uint8_t *data, size_t read_size,
337
                                vp9_reader *r) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
338
  VP9_COMMON *pc = &pbi->common;
339
  const uint8_t *data_end = pbi->source + pbi->source_sz;
John Koleszar's avatar
John Koleszar committed
340

Dmitry Kovalev's avatar
Dmitry Kovalev committed
341
342
343
  // 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.
344
  if (!read_is_valid(data, read_size, data_end))
John Koleszar's avatar
John Koleszar committed
345
    vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
346
                       "Truncated packet or corrupt tile length");
John Koleszar's avatar
John Koleszar committed
347

348
  if (vp9_reader_init(r, data, read_size))
John Koleszar's avatar
John Koleszar committed
349
350
    vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
                       "Failed to allocate bool decoder %d", 1);
John Koleszar's avatar
John Koleszar committed
351
352
}

353
static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
354
                                   vp9_reader *r) {
355
356
357
358
359
360
361
362
363
364
365
  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]);
366
}
367

368
static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode,
369
                            vp9_reader *r) {
370
  read_coef_probs_common(fc->coef_probs[TX_4X4], r);
371

372
  if (tx_mode > ONLY_4X4)
373
    read_coef_probs_common(fc->coef_probs[TX_8X8], r);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
374

375
  if (tx_mode > ALLOW_8X8)
376
    read_coef_probs_common(fc->coef_probs[TX_16X16], r);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
377

378
  if (tx_mode > ALLOW_16X16)
379
    read_coef_probs_common(fc->coef_probs[TX_32X32], r);
380
381
}

382
383
static void setup_segmentation(struct segmentation *seg,
                               struct vp9_read_bit_buffer *rb) {
384
385
  int i, j;

386
387
  seg->update_map = 0;
  seg->update_data = 0;
388

389
390
  seg->enabled = vp9_rb_read_bit(rb);
  if (!seg->enabled)
391
392
393
    return;

  // Segmentation map update
394
395
  seg->update_map = vp9_rb_read_bit(rb);
  if (seg->update_map) {
Paul Wilkins's avatar
Paul Wilkins committed
396
    for (i = 0; i < SEG_TREE_PROBS; i++)
397
398
      seg->tree_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
                                               : MAX_PROB;
399

400
401
    seg->temporal_update = vp9_rb_read_bit(rb);
    if (seg->temporal_update) {
402
      for (i = 0; i < PREDICTION_PROBS; i++)
403
404
        seg->pred_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
                                                 : MAX_PROB;
405
406
    } else {
      for (i = 0; i < PREDICTION_PROBS; i++)
407
        seg->pred_probs[i] = MAX_PROB;
408
    }
409
  }
410

411
  // Segmentation data update
412
413
414
  seg->update_data = vp9_rb_read_bit(rb);
  if (seg->update_data) {
    seg->abs_delta = vp9_rb_read_bit(rb);
415

416
    vp9_clearall_segfeatures(seg);
417

Paul Wilkins's avatar
Paul Wilkins committed
418
    for (i = 0; i < MAX_SEGMENTS; i++) {
419
420
      for (j = 0; j < SEG_LVL_MAX; j++) {
        int data = 0;
421
        const int feature_enabled = vp9_rb_read_bit(rb);
422
        if (feature_enabled) {
423
          vp9_enable_segfeature(seg, i, j);
424
          data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
425
          if (vp9_is_segfeature_signed(j))
426
            data = vp9_rb_read_bit(rb) ? -data : data;
427
        }
428
        vp9_set_segdata(seg, i, j, data);
429
430
431
432
433
      }
    }
  }
}

434
435
static void setup_loopfilter(struct loopfilter *lf,
                             struct vp9_read_bit_buffer *rb) {
436

437
438
  lf->filter_level = vp9_rb_read_literal(rb, 6);
  lf->sharpness_level = vp9_rb_read_literal(rb, 3);
439
440
441

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

444
445
446
447
  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) {
448
449
      int i;

450
451
      for (i = 0; i < MAX_REF_LF_DELTAS; i++)
        if (vp9_rb_read_bit(rb))
452
          lf->ref_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
453

454
455
      for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
        if (vp9_rb_read_bit(rb))
456
          lf->mode_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
457
458
459
460
    }
  }
}

461
462
static int read_delta_q(struct vp9_read_bit_buffer *rb, int *delta_q) {
  const int old = *delta_q;
463
464
  if (vp9_rb_read_bit(rb))
    *delta_q = vp9_rb_read_signed_literal(rb, 4);
465
466
  return old != *delta_q;
}
467

468
static void setup_quantization(VP9D_COMP *pbi, struct vp9_read_bit_buffer *rb) {
469
  MACROBLOCKD *const xd = &pbi->mb;
470
471
  VP9_COMMON *const cm = &pbi->common;
  int update = 0;
472

473
474
475
476
477
478
  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);
479
480
481
482
483

  xd->lossless = cm->base_qindex == 0 &&
                 cm->y_dc_delta_q == 0 &&
                 cm->uv_dc_delta_q == 0 &&
                 cm->uv_ac_delta_q == 0;
484
485
486

  xd->itxm_add = xd->lossless ? vp9_idct_add_lossless_c
                              : vp9_idct_add;
487
488
}

489
490
static INTERPOLATIONFILTERTYPE read_interp_filter_type(
    struct vp9_read_bit_buffer *rb) {
491
492
493
  const INTERPOLATIONFILTERTYPE literal_to_type[] = { EIGHTTAP_SMOOTH,
                                                      EIGHTTAP,
                                                      EIGHTTAP_SHARP };
494
  return vp9_rb_read_bit(rb) ? SWITCHABLE
495
                             : literal_to_type[vp9_rb_read_literal(rb, 2)];
496
497
}

498
static void read_frame_size(struct vp9_read_bit_buffer *rb,
499
                            int *width, int *height) {
500
501
  const int w = vp9_rb_read_literal(rb, 16) + 1;
  const int h = vp9_rb_read_literal(rb, 16) + 1;
502
503
  *width = w;
  *height = h;
504
505
}

506
static void setup_display_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
507
508
509
  cm->display_width = cm->width;
  cm->display_height = cm->height;
  if (vp9_rb_read_bit(rb))
510
    read_frame_size(rb, &cm->display_width, &cm->display_height);
511
}
512

513
514
static void apply_frame_size(VP9D_COMP *pbi, int width, int height) {
  VP9_COMMON *cm = &pbi->common;
515

516
  if (cm->width != width || cm->height != height) {
517
    if (!pbi->initial_width || !pbi->initial_height) {
518
519
      if (vp9_alloc_frame_buffers(cm, width, height))
        vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
520
                           "Failed to allocate frame buffers");
521
522
      pbi->initial_width = width;
      pbi->initial_height = height;
523
524
    } else {
      if (width > pbi->initial_width)
525
        vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
526
                           "Frame width too large");
527

528
      if (height > pbi->initial_height)
529
        vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
530
                           "Frame height too large");
531
532
    }

533
534
    cm->width = width;
    cm->height = height;
535

536
    vp9_update_frame_size(cm);
537
  }
538

539
540
  vp9_realloc_frame_buffer(&cm->yv12_fb[cm->new_fb_idx], cm->width, cm->height,
                           cm->subsampling_x, cm->subsampling_y,
541
                           VP9BORDERINPIXELS);
542
543
}

544
545
546
static void setup_frame_size(VP9D_COMP *pbi,
                             struct vp9_read_bit_buffer *rb) {
  int width, height;
547
548
  read_frame_size(rb, &width, &height);
  setup_display_size(&pbi->common, rb);
549
550
551
  apply_frame_size(pbi, width, height);
}

552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
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)
569
    read_frame_size(rb, &width, &height);
570

571
572
573
574
  if (!width || !height)
    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
                       "Referenced frame with invalid size");

575
  setup_display_size(cm, rb);
576
577
578
  apply_frame_size(pbi, width, height);
}

579
static void decode_tile(VP9D_COMP *pbi, vp9_reader *r) {
580
  const int num_threads = pbi->oxcf.max_threads;
581
  VP9_COMMON *const pc = &pbi->common;
582
  int mi_row, mi_col;
583
  YV12_BUFFER_CONFIG *const fb = &pc->yv12_fb[pc->new_fb_idx];
584

585
  if (pbi->do_loopfilter_inline) {
586
587
    if (num_threads > 1) {
      LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
588
      lf_data->frame_buffer = fb;
589
590
591
592
      lf_data->cm = pc;
      lf_data->xd = pbi->mb;
      lf_data->y_only = 0;
    }
593
    vp9_loop_filter_frame_init(pc, pc->lf.filter_level);
594
595
  }

596
597
  for (mi_row = pc->cur_tile_mi_row_start; mi_row < pc->cur_tile_mi_row_end;
       mi_row += MI_BLOCK_SIZE) {
598
    // For a SB there are 2 left contexts, each pertaining to a MB row within
599
600
    vp9_zero(pc->left_context);
    vp9_zero(pc->left_seg_context);
601
    for (mi_col = pc->cur_tile_mi_col_start; mi_col < pc->cur_tile_mi_col_end;
602
         mi_col += MI_BLOCK_SIZE) {
603
      decode_modes_sb(pbi, mi_row, mi_col, r, BLOCK_64X64);
604
605
606
607
608
609
    }

    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;
610
611
612
613
614
615
616
617
618
619
620
621

      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 {
        vp9_loop_filter_rows(fb, pc, &pbi->mb, lf_start, mi_row, 0);
      }
622
623
624
625
    }
  }

  if (pbi->do_loopfilter_inline) {
626
627
628
629
630
    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);
    }
631
632
    vp9_loop_filter_rows(fb, pc, &pbi->mb,
                         mi_row - MI_BLOCK_SIZE, 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")

784
785
static size_t read_uncompressed_header(VP9D_COMP *pbi,
                                       struct vp9_read_bit_buffer *rb) {
786
  VP9_COMMON *const cm = &pbi->common;
787
  MACROBLOCKD *const xd = &pbi->mb;
788
  int i;
789
790

  cm->last_frame_type = cm->frame_type;
791

792
793
794
795
796
797
798
  if (vp9_rb_read_literal(rb, 2) != 0x2)
      vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
                         "Invalid frame marker");

  cm->version = vp9_rb_read_bit(rb);
  RESERVED;

799
800
801
802
803
  if (vp9_rb_read_bit(rb)) {
    // show an existing frame directly
    int frame_to_show = cm->ref_frame_map[vp9_rb_read_literal(rb, 3)];
    ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->new_fb_idx, frame_to_show);
    pbi->refresh_frame_flags = 0;
804
    cm->lf.filter_level = 0;
805
806
    return 0;
  }
807

808
809
  cm->frame_type = (FRAME_TYPE) vp9_rb_read_bit(rb);
  cm->show_frame = vp9_rb_read_bit(rb);
810
  cm->error_resilient_mode = vp9_rb_read_bit(rb);
Dmitry Kovalev's avatar