1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <assert.h>
12#include <stdlib.h>  // qsort()
13
14#include "./vp9_rtcd.h"
15#include "./vpx_scale_rtcd.h"
16
17#include "vpx_mem/vpx_mem.h"
18#include "vpx_ports/mem_ops.h"
19#include "vpx_scale/vpx_scale.h"
20
21#include "vp9/common/vp9_alloccommon.h"
22#include "vp9/common/vp9_common.h"
23#include "vp9/common/vp9_entropy.h"
24#include "vp9/common/vp9_entropymode.h"
25#include "vp9/common/vp9_idct.h"
26#include "vp9/common/vp9_pred_common.h"
27#include "vp9/common/vp9_quant_common.h"
28#include "vp9/common/vp9_reconintra.h"
29#include "vp9/common/vp9_reconinter.h"
30#include "vp9/common/vp9_seg_common.h"
31#include "vp9/common/vp9_tile_common.h"
32
33#include "vp9/decoder/vp9_decodeframe.h"
34#include "vp9/decoder/vp9_detokenize.h"
35#include "vp9/decoder/vp9_decodemv.h"
36#include "vp9/decoder/vp9_decoder.h"
37#include "vp9/decoder/vp9_dsubexp.h"
38#include "vp9/decoder/vp9_dthread.h"
39#include "vp9/decoder/vp9_read_bit_buffer.h"
40#include "vp9/decoder/vp9_reader.h"
41#include "vp9/decoder/vp9_thread.h"
42
43static int is_compound_reference_allowed(const VP9_COMMON *cm) {
44  int i;
45  for (i = 1; i < REFS_PER_FRAME; ++i)
46    if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1])
47      return 1;
48
49  return 0;
50}
51
52static void setup_compound_reference_mode(VP9_COMMON *cm) {
53  if (cm->ref_frame_sign_bias[LAST_FRAME] ==
54          cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
55    cm->comp_fixed_ref = ALTREF_FRAME;
56    cm->comp_var_ref[0] = LAST_FRAME;
57    cm->comp_var_ref[1] = GOLDEN_FRAME;
58  } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
59                 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
60    cm->comp_fixed_ref = GOLDEN_FRAME;
61    cm->comp_var_ref[0] = LAST_FRAME;
62    cm->comp_var_ref[1] = ALTREF_FRAME;
63  } else {
64    cm->comp_fixed_ref = LAST_FRAME;
65    cm->comp_var_ref[0] = GOLDEN_FRAME;
66    cm->comp_var_ref[1] = ALTREF_FRAME;
67  }
68}
69
70static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
71  return len != 0 && len <= (size_t)(end - start);
72}
73
74static int decode_unsigned_max(struct vp9_read_bit_buffer *rb, int max) {
75  const int data = vp9_rb_read_literal(rb, get_unsigned_bits(max));
76  return data > max ? max : data;
77}
78
79static TX_MODE read_tx_mode(vp9_reader *r) {
80  TX_MODE tx_mode = vp9_read_literal(r, 2);
81  if (tx_mode == ALLOW_32X32)
82    tx_mode += vp9_read_bit(r);
83  return tx_mode;
84}
85
86static void read_tx_mode_probs(struct tx_probs *tx_probs, vp9_reader *r) {
87  int i, j;
88
89  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
90    for (j = 0; j < TX_SIZES - 3; ++j)
91      vp9_diff_update_prob(r, &tx_probs->p8x8[i][j]);
92
93  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
94    for (j = 0; j < TX_SIZES - 2; ++j)
95      vp9_diff_update_prob(r, &tx_probs->p16x16[i][j]);
96
97  for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
98    for (j = 0; j < TX_SIZES - 1; ++j)
99      vp9_diff_update_prob(r, &tx_probs->p32x32[i][j]);
100}
101
102static void read_switchable_interp_probs(FRAME_CONTEXT *fc, vp9_reader *r) {
103  int i, j;
104  for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
105    for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
106      vp9_diff_update_prob(r, &fc->switchable_interp_prob[j][i]);
107}
108
109static void read_inter_mode_probs(FRAME_CONTEXT *fc, vp9_reader *r) {
110  int i, j;
111  for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
112    for (j = 0; j < INTER_MODES - 1; ++j)
113      vp9_diff_update_prob(r, &fc->inter_mode_probs[i][j]);
114}
115
116static REFERENCE_MODE read_frame_reference_mode(const VP9_COMMON *cm,
117                                                vp9_reader *r) {
118  if (is_compound_reference_allowed(cm)) {
119    return vp9_read_bit(r) ? (vp9_read_bit(r) ? REFERENCE_MODE_SELECT
120                                              : COMPOUND_REFERENCE)
121                           : SINGLE_REFERENCE;
122  } else {
123    return SINGLE_REFERENCE;
124  }
125}
126
127static void read_frame_reference_mode_probs(VP9_COMMON *cm, vp9_reader *r) {
128  FRAME_CONTEXT *const fc = &cm->fc;
129  int i;
130
131  if (cm->reference_mode == REFERENCE_MODE_SELECT)
132    for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
133      vp9_diff_update_prob(r, &fc->comp_inter_prob[i]);
134
135  if (cm->reference_mode != COMPOUND_REFERENCE)
136    for (i = 0; i < REF_CONTEXTS; ++i) {
137      vp9_diff_update_prob(r, &fc->single_ref_prob[i][0]);
138      vp9_diff_update_prob(r, &fc->single_ref_prob[i][1]);
139    }
140
141  if (cm->reference_mode != SINGLE_REFERENCE)
142    for (i = 0; i < REF_CONTEXTS; ++i)
143      vp9_diff_update_prob(r, &fc->comp_ref_prob[i]);
144}
145
146static void update_mv_probs(vp9_prob *p, int n, vp9_reader *r) {
147  int i;
148  for (i = 0; i < n; ++i)
149    if (vp9_read(r, MV_UPDATE_PROB))
150      p[i] = (vp9_read_literal(r, 7) << 1) | 1;
151}
152
153static void read_mv_probs(nmv_context *ctx, int allow_hp, vp9_reader *r) {
154  int i, j;
155
156  update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
157
158  for (i = 0; i < 2; ++i) {
159    nmv_component *const comp_ctx = &ctx->comps[i];
160    update_mv_probs(&comp_ctx->sign, 1, r);
161    update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
162    update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
163    update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
164  }
165
166  for (i = 0; i < 2; ++i) {
167    nmv_component *const comp_ctx = &ctx->comps[i];
168    for (j = 0; j < CLASS0_SIZE; ++j)
169      update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
170    update_mv_probs(comp_ctx->fp, 3, r);
171  }
172
173  if (allow_hp) {
174    for (i = 0; i < 2; ++i) {
175      nmv_component *const comp_ctx = &ctx->comps[i];
176      update_mv_probs(&comp_ctx->class0_hp, 1, r);
177      update_mv_probs(&comp_ctx->hp, 1, r);
178    }
179  }
180}
181
182static void setup_plane_dequants(VP9_COMMON *cm, MACROBLOCKD *xd, int q_index) {
183  int i;
184  xd->plane[0].dequant = cm->y_dequant[q_index];
185
186  for (i = 1; i < MAX_MB_PLANE; i++)
187    xd->plane[i].dequant = cm->uv_dequant[q_index];
188}
189
190static void inverse_transform_block(MACROBLOCKD* xd, int plane, int block,
191                                    TX_SIZE tx_size, uint8_t *dst, int stride,
192                                    int eob) {
193  struct macroblockd_plane *const pd = &xd->plane[plane];
194  if (eob > 0) {
195    TX_TYPE tx_type;
196    const PLANE_TYPE plane_type = pd->plane_type;
197    int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
198    switch (tx_size) {
199      case TX_4X4:
200        tx_type = get_tx_type_4x4(plane_type, xd, block);
201        if (tx_type == DCT_DCT)
202          xd->itxm_add(dqcoeff, dst, stride, eob);
203        else
204          vp9_iht4x4_16_add(dqcoeff, dst, stride, tx_type);
205        break;
206      case TX_8X8:
207        tx_type = get_tx_type(plane_type, xd);
208        vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob);
209        break;
210      case TX_16X16:
211        tx_type = get_tx_type(plane_type, xd);
212        vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
213        break;
214      case TX_32X32:
215        tx_type = DCT_DCT;
216        vp9_idct32x32_add(dqcoeff, dst, stride, eob);
217        break;
218      default:
219        assert(0 && "Invalid transform size");
220    }
221
222    if (eob == 1) {
223      vpx_memset(dqcoeff, 0, 2 * sizeof(dqcoeff[0]));
224    } else {
225      if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
226        vpx_memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
227      else if (tx_size == TX_32X32 && eob <= 34)
228        vpx_memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
229      else
230        vpx_memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
231    }
232  }
233}
234
235struct intra_args {
236  VP9_COMMON *cm;
237  MACROBLOCKD *xd;
238  vp9_reader *r;
239};
240
241static void predict_and_reconstruct_intra_block(int plane, int block,
242                                                BLOCK_SIZE plane_bsize,
243                                                TX_SIZE tx_size, void *arg) {
244  struct intra_args *const args = (struct intra_args *)arg;
245  VP9_COMMON *const cm = args->cm;
246  MACROBLOCKD *const xd = args->xd;
247  struct macroblockd_plane *const pd = &xd->plane[plane];
248  MODE_INFO *const mi = xd->mi[0];
249  const MB_PREDICTION_MODE mode = (plane == 0) ? get_y_mode(mi, block)
250                                               : mi->mbmi.uv_mode;
251  int x, y;
252  uint8_t *dst;
253  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &x, &y);
254  dst = &pd->dst.buf[4 * y * pd->dst.stride + 4 * x];
255
256  vp9_predict_intra_block(xd, block >> (tx_size << 1),
257                          b_width_log2(plane_bsize), tx_size, mode,
258                          dst, pd->dst.stride, dst, pd->dst.stride,
259                          x, y, plane);
260
261  if (!mi->mbmi.skip) {
262    const int eob = vp9_decode_block_tokens(cm, xd, plane, block,
263                                            plane_bsize, x, y, tx_size,
264                                            args->r);
265    inverse_transform_block(xd, plane, block, tx_size, dst, pd->dst.stride,
266                            eob);
267  }
268}
269
270struct inter_args {
271  VP9_COMMON *cm;
272  MACROBLOCKD *xd;
273  vp9_reader *r;
274  int *eobtotal;
275};
276
277static void reconstruct_inter_block(int plane, int block,
278                                    BLOCK_SIZE plane_bsize,
279                                    TX_SIZE tx_size, void *arg) {
280  struct inter_args *args = (struct inter_args *)arg;
281  VP9_COMMON *const cm = args->cm;
282  MACROBLOCKD *const xd = args->xd;
283  struct macroblockd_plane *const pd = &xd->plane[plane];
284  int x, y, eob;
285  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &x, &y);
286  eob = vp9_decode_block_tokens(cm, xd, plane, block, plane_bsize, x, y,
287                                tx_size, args->r);
288  inverse_transform_block(xd, plane, block, tx_size,
289                          &pd->dst.buf[4 * y * pd->dst.stride + 4 * x],
290                          pd->dst.stride, eob);
291  *args->eobtotal += eob;
292}
293
294static MB_MODE_INFO *set_offsets(VP9_COMMON *const cm, MACROBLOCKD *const xd,
295                                 const TileInfo *const tile,
296                                 BLOCK_SIZE bsize, int mi_row, int mi_col) {
297  const int bw = num_8x8_blocks_wide_lookup[bsize];
298  const int bh = num_8x8_blocks_high_lookup[bsize];
299  const int x_mis = MIN(bw, cm->mi_cols - mi_col);
300  const int y_mis = MIN(bh, cm->mi_rows - mi_row);
301  const int offset = mi_row * cm->mi_stride + mi_col;
302  int x, y;
303
304  xd->mi = cm->mi_grid_visible + offset;
305  xd->mi[0] = &cm->mi[offset];
306  xd->mi[0]->mbmi.sb_type = bsize;
307  for (y = 0; y < y_mis; ++y)
308    for (x = !y; x < x_mis; ++x)
309      xd->mi[y * cm->mi_stride + x] = xd->mi[0];
310
311  set_skip_context(xd, mi_row, mi_col);
312
313  // Distance of Mb to the various image edges. These are specified to 8th pel
314  // as they are always compared to values that are in 1/8th pel units
315  set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
316
317  vp9_setup_dst_planes(xd, get_frame_new_buffer(cm), mi_row, mi_col);
318  return &xd->mi[0]->mbmi;
319}
320
321static void set_ref(VP9_COMMON *const cm, MACROBLOCKD *const xd,
322                    int idx, int mi_row, int mi_col) {
323  MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
324  RefBuffer *ref_buffer = &cm->frame_refs[mbmi->ref_frame[idx] - LAST_FRAME];
325  xd->block_refs[idx] = ref_buffer;
326  if (!vp9_is_valid_scale(&ref_buffer->sf))
327    vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
328                       "Invalid scale factors");
329  vp9_setup_pre_planes(xd, idx, ref_buffer->buf, mi_row, mi_col,
330                       &ref_buffer->sf);
331  xd->corrupted |= ref_buffer->buf->corrupted;
332}
333
334static void decode_block(VP9_COMMON *const cm, MACROBLOCKD *const xd,
335                         const TileInfo *const tile,
336                         int mi_row, int mi_col,
337                         vp9_reader *r, BLOCK_SIZE bsize) {
338  const int less8x8 = bsize < BLOCK_8X8;
339  MB_MODE_INFO *mbmi = set_offsets(cm, xd, tile, bsize, mi_row, mi_col);
340  vp9_read_mode_info(cm, xd, tile, mi_row, mi_col, r);
341
342  if (less8x8)
343    bsize = BLOCK_8X8;
344
345  if (mbmi->skip) {
346    reset_skip_context(xd, bsize);
347  } else {
348    if (cm->seg.enabled)
349      setup_plane_dequants(cm, xd, vp9_get_qindex(&cm->seg, mbmi->segment_id,
350                                                  cm->base_qindex));
351  }
352
353  if (!is_inter_block(mbmi)) {
354    struct intra_args arg = { cm, xd, r };
355    vp9_foreach_transformed_block(xd, bsize,
356                                  predict_and_reconstruct_intra_block, &arg);
357  } else {
358    // Setup
359    set_ref(cm, xd, 0, mi_row, mi_col);
360    if (has_second_ref(mbmi))
361      set_ref(cm, xd, 1, mi_row, mi_col);
362
363    // Prediction
364    vp9_dec_build_inter_predictors_sb(xd, mi_row, mi_col, bsize);
365
366    // Reconstruction
367    if (!mbmi->skip) {
368      int eobtotal = 0;
369      struct inter_args arg = { cm, xd, r, &eobtotal };
370      vp9_foreach_transformed_block(xd, bsize, reconstruct_inter_block, &arg);
371      if (!less8x8 && eobtotal == 0)
372        mbmi->skip = 1;  // skip loopfilter
373    }
374  }
375
376  xd->corrupted |= vp9_reader_has_error(r);
377}
378
379static PARTITION_TYPE read_partition(VP9_COMMON *cm, MACROBLOCKD *xd, int hbs,
380                                     int mi_row, int mi_col, BLOCK_SIZE bsize,
381                                     vp9_reader *r) {
382  const int ctx = partition_plane_context(xd, mi_row, mi_col, bsize);
383  const vp9_prob *const probs = get_partition_probs(cm, ctx);
384  const int has_rows = (mi_row + hbs) < cm->mi_rows;
385  const int has_cols = (mi_col + hbs) < cm->mi_cols;
386  PARTITION_TYPE p;
387
388  if (has_rows && has_cols)
389    p = (PARTITION_TYPE)vp9_read_tree(r, vp9_partition_tree, probs);
390  else if (!has_rows && has_cols)
391    p = vp9_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ;
392  else if (has_rows && !has_cols)
393    p = vp9_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT;
394  else
395    p = PARTITION_SPLIT;
396
397  if (!cm->frame_parallel_decoding_mode)
398    ++cm->counts.partition[ctx][p];
399
400  return p;
401}
402
403static void decode_partition(VP9_COMMON *const cm, MACROBLOCKD *const xd,
404                             const TileInfo *const tile,
405                             int mi_row, int mi_col,
406                             vp9_reader* r, BLOCK_SIZE bsize) {
407  const int hbs = num_8x8_blocks_wide_lookup[bsize] / 2;
408  PARTITION_TYPE partition;
409  BLOCK_SIZE subsize;
410
411  if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
412    return;
413
414  partition = read_partition(cm, xd, hbs, mi_row, mi_col, bsize, r);
415  subsize = get_subsize(bsize, partition);
416  if (subsize < BLOCK_8X8) {
417    decode_block(cm, xd, tile, mi_row, mi_col, r, subsize);
418  } else {
419    switch (partition) {
420      case PARTITION_NONE:
421        decode_block(cm, xd, tile, mi_row, mi_col, r, subsize);
422        break;
423      case PARTITION_HORZ:
424        decode_block(cm, xd, tile, mi_row, mi_col, r, subsize);
425        if (mi_row + hbs < cm->mi_rows)
426          decode_block(cm, xd, tile, mi_row + hbs, mi_col, r, subsize);
427        break;
428      case PARTITION_VERT:
429        decode_block(cm, xd, tile, mi_row, mi_col, r, subsize);
430        if (mi_col + hbs < cm->mi_cols)
431          decode_block(cm, xd, tile, mi_row, mi_col + hbs, r, subsize);
432        break;
433      case PARTITION_SPLIT:
434        decode_partition(cm, xd, tile, mi_row,       mi_col,       r, subsize);
435        decode_partition(cm, xd, tile, mi_row,       mi_col + hbs, r, subsize);
436        decode_partition(cm, xd, tile, mi_row + hbs, mi_col,       r, subsize);
437        decode_partition(cm, xd, tile, mi_row + hbs, mi_col + hbs, r, subsize);
438        break;
439      default:
440        assert(0 && "Invalid partition type");
441    }
442  }
443
444  // update partition context
445  if (bsize >= BLOCK_8X8 &&
446      (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
447    update_partition_context(xd, mi_row, mi_col, subsize, bsize);
448}
449
450static void setup_token_decoder(const uint8_t *data,
451                                const uint8_t *data_end,
452                                size_t read_size,
453                                struct vpx_internal_error_info *error_info,
454                                vp9_reader *r) {
455  // Validate the calculated partition length. If the buffer
456  // described by the partition can't be fully read, then restrict
457  // it to the portion that can be (for EC mode) or throw an error.
458  if (!read_is_valid(data, read_size, data_end))
459    vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
460                       "Truncated packet or corrupt tile length");
461
462  if (vp9_reader_init(r, data, read_size))
463    vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
464                       "Failed to allocate bool decoder %d", 1);
465}
466
467static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
468                                   vp9_reader *r) {
469  int i, j, k, l, m;
470
471  if (vp9_read_bit(r))
472    for (i = 0; i < PLANE_TYPES; ++i)
473      for (j = 0; j < REF_TYPES; ++j)
474        for (k = 0; k < COEF_BANDS; ++k)
475          for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
476            for (m = 0; m < UNCONSTRAINED_NODES; ++m)
477              vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
478}
479
480static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode,
481                            vp9_reader *r) {
482    const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
483    TX_SIZE tx_size;
484    for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
485      read_coef_probs_common(fc->coef_probs[tx_size], r);
486}
487
488static void setup_segmentation(struct segmentation *seg,
489                               struct vp9_read_bit_buffer *rb) {
490  int i, j;
491
492  seg->update_map = 0;
493  seg->update_data = 0;
494
495  seg->enabled = vp9_rb_read_bit(rb);
496  if (!seg->enabled)
497    return;
498
499  // Segmentation map update
500  seg->update_map = vp9_rb_read_bit(rb);
501  if (seg->update_map) {
502    for (i = 0; i < SEG_TREE_PROBS; i++)
503      seg->tree_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
504                                               : MAX_PROB;
505
506    seg->temporal_update = vp9_rb_read_bit(rb);
507    if (seg->temporal_update) {
508      for (i = 0; i < PREDICTION_PROBS; i++)
509        seg->pred_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
510                                                 : MAX_PROB;
511    } else {
512      for (i = 0; i < PREDICTION_PROBS; i++)
513        seg->pred_probs[i] = MAX_PROB;
514    }
515  }
516
517  // Segmentation data update
518  seg->update_data = vp9_rb_read_bit(rb);
519  if (seg->update_data) {
520    seg->abs_delta = vp9_rb_read_bit(rb);
521
522    vp9_clearall_segfeatures(seg);
523
524    for (i = 0; i < MAX_SEGMENTS; i++) {
525      for (j = 0; j < SEG_LVL_MAX; j++) {
526        int data = 0;
527        const int feature_enabled = vp9_rb_read_bit(rb);
528        if (feature_enabled) {
529          vp9_enable_segfeature(seg, i, j);
530          data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
531          if (vp9_is_segfeature_signed(j))
532            data = vp9_rb_read_bit(rb) ? -data : data;
533        }
534        vp9_set_segdata(seg, i, j, data);
535      }
536    }
537  }
538}
539
540static void setup_loopfilter(struct loopfilter *lf,
541                             struct vp9_read_bit_buffer *rb) {
542  lf->filter_level = vp9_rb_read_literal(rb, 6);
543  lf->sharpness_level = vp9_rb_read_literal(rb, 3);
544
545  // Read in loop filter deltas applied at the MB level based on mode or ref
546  // frame.
547  lf->mode_ref_delta_update = 0;
548
549  lf->mode_ref_delta_enabled = vp9_rb_read_bit(rb);
550  if (lf->mode_ref_delta_enabled) {
551    lf->mode_ref_delta_update = vp9_rb_read_bit(rb);
552    if (lf->mode_ref_delta_update) {
553      int i;
554
555      for (i = 0; i < MAX_REF_LF_DELTAS; i++)
556        if (vp9_rb_read_bit(rb))
557          lf->ref_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
558
559      for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
560        if (vp9_rb_read_bit(rb))
561          lf->mode_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
562    }
563  }
564}
565
566static int read_delta_q(struct vp9_read_bit_buffer *rb, int *delta_q) {
567  const int old = *delta_q;
568  *delta_q = vp9_rb_read_bit(rb) ? vp9_rb_read_signed_literal(rb, 4) : 0;
569  return old != *delta_q;
570}
571
572static void setup_quantization(VP9_COMMON *const cm, MACROBLOCKD *const xd,
573                               struct vp9_read_bit_buffer *rb) {
574  int update = 0;
575
576  cm->base_qindex = vp9_rb_read_literal(rb, QINDEX_BITS);
577  update |= read_delta_q(rb, &cm->y_dc_delta_q);
578  update |= read_delta_q(rb, &cm->uv_dc_delta_q);
579  update |= read_delta_q(rb, &cm->uv_ac_delta_q);
580  if (update)
581    vp9_init_dequantizer(cm);
582
583  xd->lossless = cm->base_qindex == 0 &&
584                 cm->y_dc_delta_q == 0 &&
585                 cm->uv_dc_delta_q == 0 &&
586                 cm->uv_ac_delta_q == 0;
587
588  xd->itxm_add = xd->lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
589}
590
591static INTERP_FILTER read_interp_filter(struct vp9_read_bit_buffer *rb) {
592  const INTERP_FILTER literal_to_filter[] = { EIGHTTAP_SMOOTH,
593                                              EIGHTTAP,
594                                              EIGHTTAP_SHARP,
595                                              BILINEAR };
596  return vp9_rb_read_bit(rb) ? SWITCHABLE
597                             : literal_to_filter[vp9_rb_read_literal(rb, 2)];
598}
599
600static void read_frame_size(struct vp9_read_bit_buffer *rb,
601                            int *width, int *height) {
602  const int w = vp9_rb_read_literal(rb, 16) + 1;
603  const int h = vp9_rb_read_literal(rb, 16) + 1;
604  *width = w;
605  *height = h;
606}
607
608static void setup_display_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
609  cm->display_width = cm->width;
610  cm->display_height = cm->height;
611  if (vp9_rb_read_bit(rb))
612    read_frame_size(rb, &cm->display_width, &cm->display_height);
613}
614
615static void apply_frame_size(VP9_COMMON *cm, int width, int height) {
616  if (cm->width != width || cm->height != height) {
617    // Change in frame size.
618    // TODO(agrange) Don't test width/height, check overall size.
619    if (width > cm->width || height > cm->height) {
620      // Rescale frame buffers only if they're not big enough already.
621      if (vp9_resize_frame_buffers(cm, width, height))
622        vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
623                           "Failed to allocate frame buffers");
624    }
625
626    cm->width = width;
627    cm->height = height;
628
629    vp9_update_frame_size(cm);
630  }
631
632  if (vp9_realloc_frame_buffer(
633          get_frame_new_buffer(cm), cm->width, cm->height,
634          cm->subsampling_x, cm->subsampling_y, VP9_DEC_BORDER_IN_PIXELS,
635          &cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer, cm->get_fb_cb,
636          cm->cb_priv)) {
637    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
638                       "Failed to allocate frame buffer");
639  }
640}
641
642static void setup_frame_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
643  int width, height;
644  read_frame_size(rb, &width, &height);
645  apply_frame_size(cm, width, height);
646  setup_display_size(cm, rb);
647}
648
649static void setup_frame_size_with_refs(VP9_COMMON *cm,
650                                       struct vp9_read_bit_buffer *rb) {
651  int width, height;
652  int found = 0, i;
653  for (i = 0; i < REFS_PER_FRAME; ++i) {
654    if (vp9_rb_read_bit(rb)) {
655      YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
656      width = buf->y_crop_width;
657      height = buf->y_crop_height;
658      found = 1;
659      break;
660    }
661  }
662
663  if (!found)
664    read_frame_size(rb, &width, &height);
665
666  if (width <= 0 || height <= 0)
667    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
668                       "Referenced frame with invalid size");
669
670  apply_frame_size(cm, width, height);
671  setup_display_size(cm, rb);
672}
673
674static void decode_tile(VP9Decoder *pbi, const TileInfo *const tile,
675                        vp9_reader *r) {
676  const int num_threads = pbi->oxcf.max_threads;
677  VP9_COMMON *const cm = &pbi->common;
678  int mi_row, mi_col;
679  MACROBLOCKD *xd = &pbi->mb;
680
681  if (pbi->do_loopfilter_inline) {
682    LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
683    lf_data->frame_buffer = get_frame_new_buffer(cm);
684    lf_data->cm = cm;
685    lf_data->xd = pbi->mb;
686    lf_data->stop = 0;
687    lf_data->y_only = 0;
688    vp9_loop_filter_frame_init(cm, cm->lf.filter_level);
689  }
690
691  for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
692       mi_row += MI_BLOCK_SIZE) {
693    // For a SB there are 2 left contexts, each pertaining to a MB row within
694    vp9_zero(xd->left_context);
695    vp9_zero(xd->left_seg_context);
696    for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
697         mi_col += MI_BLOCK_SIZE) {
698      decode_partition(cm, xd, tile, mi_row, mi_col, r, BLOCK_64X64);
699    }
700
701    if (pbi->do_loopfilter_inline) {
702      const int lf_start = mi_row - MI_BLOCK_SIZE;
703      LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
704
705      // delay the loopfilter by 1 macroblock row.
706      if (lf_start < 0) continue;
707
708      // decoding has completed: finish up the loop filter in this thread.
709      if (mi_row + MI_BLOCK_SIZE >= tile->mi_row_end) continue;
710
711      vp9_worker_sync(&pbi->lf_worker);
712      lf_data->start = lf_start;
713      lf_data->stop = mi_row;
714      if (num_threads > 1) {
715        vp9_worker_launch(&pbi->lf_worker);
716      } else {
717        vp9_worker_execute(&pbi->lf_worker);
718      }
719    }
720  }
721
722  if (pbi->do_loopfilter_inline) {
723    LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
724
725    vp9_worker_sync(&pbi->lf_worker);
726    lf_data->start = lf_data->stop;
727    lf_data->stop = cm->mi_rows;
728    vp9_worker_execute(&pbi->lf_worker);
729  }
730}
731
732static void setup_tile_info(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
733  int min_log2_tile_cols, max_log2_tile_cols, max_ones;
734  vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
735
736  // columns
737  max_ones = max_log2_tile_cols - min_log2_tile_cols;
738  cm->log2_tile_cols = min_log2_tile_cols;
739  while (max_ones-- && vp9_rb_read_bit(rb))
740    cm->log2_tile_cols++;
741
742  // rows
743  cm->log2_tile_rows = vp9_rb_read_bit(rb);
744  if (cm->log2_tile_rows)
745    cm->log2_tile_rows += vp9_rb_read_bit(rb);
746}
747
748// Reads the next tile returning its size and adjusting '*data' accordingly
749// based on 'is_last'.
750static size_t get_tile(const uint8_t *const data_end,
751                       int is_last,
752                       struct vpx_internal_error_info *error_info,
753                       const uint8_t **data) {
754  size_t size;
755
756  if (!is_last) {
757    if (!read_is_valid(*data, 4, data_end))
758      vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
759                         "Truncated packet or corrupt tile length");
760
761    size = mem_get_be32(*data);
762    *data += 4;
763
764    if (size > (size_t)(data_end - *data))
765      vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
766                         "Truncated packet or corrupt tile size");
767  } else {
768    size = data_end - *data;
769  }
770  return size;
771}
772
773typedef struct TileBuffer {
774  const uint8_t *data;
775  size_t size;
776  int col;  // only used with multi-threaded decoding
777} TileBuffer;
778
779static const uint8_t *decode_tiles(VP9Decoder *pbi,
780                                   const uint8_t *data,
781                                   const uint8_t *data_end) {
782  VP9_COMMON *const cm = &pbi->common;
783  const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
784  const int tile_cols = 1 << cm->log2_tile_cols;
785  const int tile_rows = 1 << cm->log2_tile_rows;
786  TileBuffer tile_buffers[4][1 << 6];
787  int tile_row, tile_col;
788  const uint8_t *end = NULL;
789  vp9_reader r;
790
791  assert(tile_rows <= 4);
792  assert(tile_cols <= (1 << 6));
793
794  // Note: this memset assumes above_context[0], [1] and [2]
795  // are allocated as part of the same buffer.
796  vpx_memset(cm->above_context, 0,
797             sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_cols);
798
799  vpx_memset(cm->above_seg_context, 0,
800             sizeof(*cm->above_seg_context) * aligned_cols);
801
802  // Load tile data into tile_buffers
803  for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
804    for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
805      const int last_tile = tile_row == tile_rows - 1 &&
806                            tile_col == tile_cols - 1;
807      const size_t size = get_tile(data_end, last_tile, &cm->error, &data);
808      TileBuffer *const buf = &tile_buffers[tile_row][tile_col];
809      buf->data = data;
810      buf->size = size;
811      data += size;
812    }
813  }
814
815  // Decode tiles using data from tile_buffers
816  for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
817    for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
818      const int col = pbi->oxcf.inv_tile_order ? tile_cols - tile_col - 1
819                                               : tile_col;
820      const int last_tile = tile_row == tile_rows - 1 &&
821                                 col == tile_cols - 1;
822      const TileBuffer *const buf = &tile_buffers[tile_row][col];
823      TileInfo tile;
824
825      vp9_tile_init(&tile, cm, tile_row, col);
826      setup_token_decoder(buf->data, data_end, buf->size, &cm->error, &r);
827      decode_tile(pbi, &tile, &r);
828
829      if (last_tile)
830        end = vp9_reader_find_end(&r);
831    }
832  }
833
834  return end;
835}
836
837static int tile_worker_hook(void *arg1, void *arg2) {
838  TileWorkerData *const tile_data = (TileWorkerData*)arg1;
839  const TileInfo *const tile = (TileInfo*)arg2;
840  int mi_row, mi_col;
841
842  for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
843       mi_row += MI_BLOCK_SIZE) {
844    vp9_zero(tile_data->xd.left_context);
845    vp9_zero(tile_data->xd.left_seg_context);
846    for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
847         mi_col += MI_BLOCK_SIZE) {
848      decode_partition(tile_data->cm, &tile_data->xd, tile,
849                       mi_row, mi_col, &tile_data->bit_reader, BLOCK_64X64);
850    }
851  }
852  return !tile_data->xd.corrupted;
853}
854
855// sorts in descending order
856static int compare_tile_buffers(const void *a, const void *b) {
857  const TileBuffer *const buf1 = (const TileBuffer*)a;
858  const TileBuffer *const buf2 = (const TileBuffer*)b;
859  if (buf1->size < buf2->size) {
860    return 1;
861  } else if (buf1->size == buf2->size) {
862    return 0;
863  } else {
864    return -1;
865  }
866}
867
868static const uint8_t *decode_tiles_mt(VP9Decoder *pbi,
869                                      const uint8_t *data,
870                                      const uint8_t *data_end) {
871  VP9_COMMON *const cm = &pbi->common;
872  const uint8_t *bit_reader_end = NULL;
873  const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
874  const int tile_cols = 1 << cm->log2_tile_cols;
875  const int tile_rows = 1 << cm->log2_tile_rows;
876  const int num_workers = MIN(pbi->oxcf.max_threads & ~1, tile_cols);
877  TileBuffer tile_buffers[1 << 6];
878  int n;
879  int final_worker = -1;
880
881  assert(tile_cols <= (1 << 6));
882  assert(tile_rows == 1);
883  (void)tile_rows;
884
885  // TODO(jzern): See if we can remove the restriction of passing in max
886  // threads to the decoder.
887  if (pbi->num_tile_workers == 0) {
888    const int num_threads = pbi->oxcf.max_threads & ~1;
889    int i;
890    // TODO(jzern): Allocate one less worker, as in the current code we only
891    // use num_threads - 1 workers.
892    CHECK_MEM_ERROR(cm, pbi->tile_workers,
893                    vpx_malloc(num_threads * sizeof(*pbi->tile_workers)));
894    for (i = 0; i < num_threads; ++i) {
895      VP9Worker *const worker = &pbi->tile_workers[i];
896      ++pbi->num_tile_workers;
897
898      vp9_worker_init(worker);
899      CHECK_MEM_ERROR(cm, worker->data1,
900                      vpx_memalign(32, sizeof(TileWorkerData)));
901      CHECK_MEM_ERROR(cm, worker->data2, vpx_malloc(sizeof(TileInfo)));
902      if (i < num_threads - 1 && !vp9_worker_reset(worker)) {
903        vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
904                           "Tile decoder thread creation failed");
905      }
906    }
907  }
908
909  // Reset tile decoding hook
910  for (n = 0; n < num_workers; ++n) {
911    pbi->tile_workers[n].hook = (VP9WorkerHook)tile_worker_hook;
912  }
913
914  // Note: this memset assumes above_context[0], [1] and [2]
915  // are allocated as part of the same buffer.
916  vpx_memset(cm->above_context, 0,
917             sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_mi_cols);
918  vpx_memset(cm->above_seg_context, 0,
919             sizeof(*cm->above_seg_context) * aligned_mi_cols);
920
921  // Load tile data into tile_buffers
922  for (n = 0; n < tile_cols; ++n) {
923    const size_t size =
924        get_tile(data_end, n == tile_cols - 1, &cm->error, &data);
925    TileBuffer *const buf = &tile_buffers[n];
926    buf->data = data;
927    buf->size = size;
928    buf->col = n;
929    data += size;
930  }
931
932  // Sort the buffers based on size in descending order.
933  qsort(tile_buffers, tile_cols, sizeof(tile_buffers[0]), compare_tile_buffers);
934
935  // Rearrange the tile buffers such that per-tile group the largest, and
936  // presumably the most difficult, tile will be decoded in the main thread.
937  // This should help minimize the number of instances where the main thread is
938  // waiting for a worker to complete.
939  {
940    int group_start = 0;
941    while (group_start < tile_cols) {
942      const TileBuffer largest = tile_buffers[group_start];
943      const int group_end = MIN(group_start + num_workers, tile_cols) - 1;
944      memmove(tile_buffers + group_start, tile_buffers + group_start + 1,
945              (group_end - group_start) * sizeof(tile_buffers[0]));
946      tile_buffers[group_end] = largest;
947      group_start = group_end + 1;
948    }
949  }
950
951  n = 0;
952  while (n < tile_cols) {
953    int i;
954    for (i = 0; i < num_workers && n < tile_cols; ++i) {
955      VP9Worker *const worker = &pbi->tile_workers[i];
956      TileWorkerData *const tile_data = (TileWorkerData*)worker->data1;
957      TileInfo *const tile = (TileInfo*)worker->data2;
958      TileBuffer *const buf = &tile_buffers[n];
959
960      tile_data->cm = cm;
961      tile_data->xd = pbi->mb;
962      tile_data->xd.corrupted = 0;
963      vp9_tile_init(tile, tile_data->cm, 0, buf->col);
964      setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
965                          &tile_data->bit_reader);
966      init_macroblockd(cm, &tile_data->xd);
967      vp9_zero(tile_data->xd.dqcoeff);
968
969      worker->had_error = 0;
970      if (i == num_workers - 1 || n == tile_cols - 1) {
971        vp9_worker_execute(worker);
972      } else {
973        vp9_worker_launch(worker);
974      }
975
976      if (buf->col == tile_cols - 1) {
977        final_worker = i;
978      }
979
980      ++n;
981    }
982
983    for (; i > 0; --i) {
984      VP9Worker *const worker = &pbi->tile_workers[i - 1];
985      pbi->mb.corrupted |= !vp9_worker_sync(worker);
986    }
987    if (final_worker > -1) {
988      TileWorkerData *const tile_data =
989          (TileWorkerData*)pbi->tile_workers[final_worker].data1;
990      bit_reader_end = vp9_reader_find_end(&tile_data->bit_reader);
991      final_worker = -1;
992    }
993  }
994
995  return bit_reader_end;
996}
997
998static void check_sync_code(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
999  if (vp9_rb_read_literal(rb, 8) != VP9_SYNC_CODE_0 ||
1000      vp9_rb_read_literal(rb, 8) != VP9_SYNC_CODE_1 ||
1001      vp9_rb_read_literal(rb, 8) != VP9_SYNC_CODE_2) {
1002    vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1003                       "Invalid frame sync code");
1004  }
1005}
1006
1007static void error_handler(void *data) {
1008  VP9_COMMON *const cm = (VP9_COMMON *)data;
1009  vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet");
1010}
1011
1012static BITSTREAM_PROFILE read_profile(struct vp9_read_bit_buffer *rb) {
1013  int profile = vp9_rb_read_bit(rb);
1014  profile |= vp9_rb_read_bit(rb) << 1;
1015  return (BITSTREAM_PROFILE) profile;
1016}
1017
1018static size_t read_uncompressed_header(VP9Decoder *pbi,
1019                                       struct vp9_read_bit_buffer *rb) {
1020  VP9_COMMON *const cm = &pbi->common;
1021  size_t sz;
1022  int i;
1023
1024  cm->last_frame_type = cm->frame_type;
1025
1026  if (vp9_rb_read_literal(rb, 2) != VP9_FRAME_MARKER)
1027      vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1028                         "Invalid frame marker");
1029
1030  cm->profile = read_profile(rb);
1031  if (cm->profile >= MAX_PROFILES)
1032    vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1033                       "Unsupported bitstream profile");
1034
1035  cm->show_existing_frame = vp9_rb_read_bit(rb);
1036  if (cm->show_existing_frame) {
1037    // Show an existing frame directly.
1038    const int frame_to_show = cm->ref_frame_map[vp9_rb_read_literal(rb, 3)];
1039
1040    if (cm->frame_bufs[frame_to_show].ref_count < 1)
1041      vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1042                         "Buffer %d does not contain a decoded frame",
1043                         frame_to_show);
1044
1045    ref_cnt_fb(cm->frame_bufs, &cm->new_fb_idx, frame_to_show);
1046    pbi->refresh_frame_flags = 0;
1047    cm->lf.filter_level = 0;
1048    cm->show_frame = 1;
1049    return 0;
1050  }
1051
1052  cm->frame_type = (FRAME_TYPE) vp9_rb_read_bit(rb);
1053  cm->show_frame = vp9_rb_read_bit(rb);
1054  cm->error_resilient_mode = vp9_rb_read_bit(rb);
1055
1056  if (cm->frame_type == KEY_FRAME) {
1057    check_sync_code(cm, rb);
1058    if (cm->profile > PROFILE_1)
1059      cm->bit_depth = vp9_rb_read_bit(rb) ? BITS_12 : BITS_10;
1060    cm->color_space = (COLOR_SPACE)vp9_rb_read_literal(rb, 3);
1061    if (cm->color_space != SRGB) {
1062      vp9_rb_read_bit(rb);  // [16,235] (including xvycc) vs [0,255] range
1063      if (cm->profile >= PROFILE_1) {
1064        cm->subsampling_x = vp9_rb_read_bit(rb);
1065        cm->subsampling_y = vp9_rb_read_bit(rb);
1066        vp9_rb_read_bit(rb);  // has extra plane
1067      } else {
1068        cm->subsampling_y = cm->subsampling_x = 1;
1069      }
1070    } else {
1071      if (cm->profile >= PROFILE_1) {
1072        cm->subsampling_y = cm->subsampling_x = 0;
1073        vp9_rb_read_bit(rb);  // has extra plane
1074      } else {
1075        vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1076                           "RGB not supported in profile 0");
1077      }
1078    }
1079
1080    pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
1081
1082    for (i = 0; i < REFS_PER_FRAME; ++i) {
1083      cm->frame_refs[i].idx = cm->new_fb_idx;
1084      cm->frame_refs[i].buf = get_frame_new_buffer(cm);
1085    }
1086
1087    setup_frame_size(cm, rb);
1088  } else {
1089    cm->intra_only = cm->show_frame ? 0 : vp9_rb_read_bit(rb);
1090
1091    cm->reset_frame_context = cm->error_resilient_mode ?
1092        0 : vp9_rb_read_literal(rb, 2);
1093
1094    if (cm->intra_only) {
1095      check_sync_code(cm, rb);
1096
1097      pbi->refresh_frame_flags = vp9_rb_read_literal(rb, REF_FRAMES);
1098      setup_frame_size(cm, rb);
1099    } else {
1100      pbi->refresh_frame_flags = vp9_rb_read_literal(rb, REF_FRAMES);
1101
1102      for (i = 0; i < REFS_PER_FRAME; ++i) {
1103        const int ref = vp9_rb_read_literal(rb, REF_FRAMES_LOG2);
1104        const int idx = cm->ref_frame_map[ref];
1105        cm->frame_refs[i].idx = idx;
1106        cm->frame_refs[i].buf = &cm->frame_bufs[idx].buf;
1107        cm->ref_frame_sign_bias[LAST_FRAME + i] = vp9_rb_read_bit(rb);
1108      }
1109
1110      setup_frame_size_with_refs(cm, rb);
1111
1112      cm->allow_high_precision_mv = vp9_rb_read_bit(rb);
1113      cm->interp_filter = read_interp_filter(rb);
1114
1115      for (i = 0; i < REFS_PER_FRAME; ++i) {
1116        RefBuffer *const ref_buf = &cm->frame_refs[i];
1117        vp9_setup_scale_factors_for_frame(&ref_buf->sf,
1118                                          ref_buf->buf->y_crop_width,
1119                                          ref_buf->buf->y_crop_height,
1120                                          cm->width, cm->height);
1121        if (vp9_is_scaled(&ref_buf->sf))
1122          vp9_extend_frame_borders(ref_buf->buf);
1123      }
1124    }
1125  }
1126
1127  if (!cm->error_resilient_mode) {
1128    cm->coding_use_prev_mi = 1;
1129    cm->refresh_frame_context = vp9_rb_read_bit(rb);
1130    cm->frame_parallel_decoding_mode = vp9_rb_read_bit(rb);
1131  } else {
1132    cm->coding_use_prev_mi = 0;
1133    cm->refresh_frame_context = 0;
1134    cm->frame_parallel_decoding_mode = 1;
1135  }
1136
1137  // This flag will be overridden by the call to vp9_setup_past_independence
1138  // below, forcing the use of context 0 for those frame types.
1139  cm->frame_context_idx = vp9_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
1140
1141  if (frame_is_intra_only(cm) || cm->error_resilient_mode)
1142    vp9_setup_past_independence(cm);
1143
1144  setup_loopfilter(&cm->lf, rb);
1145  setup_quantization(cm, &pbi->mb, rb);
1146  setup_segmentation(&cm->seg, rb);
1147
1148  setup_tile_info(cm, rb);
1149  sz = vp9_rb_read_literal(rb, 16);
1150
1151  if (sz == 0)
1152    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1153                       "Invalid header size");
1154
1155  return sz;
1156}
1157
1158static int read_compressed_header(VP9Decoder *pbi, const uint8_t *data,
1159                                  size_t partition_size) {
1160  VP9_COMMON *const cm = &pbi->common;
1161  MACROBLOCKD *const xd = &pbi->mb;
1162  FRAME_CONTEXT *const fc = &cm->fc;
1163  vp9_reader r;
1164  int k;
1165
1166  if (vp9_reader_init(&r, data, partition_size))
1167    vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1168                       "Failed to allocate bool decoder 0");
1169
1170  cm->tx_mode = xd->lossless ? ONLY_4X4 : read_tx_mode(&r);
1171  if (cm->tx_mode == TX_MODE_SELECT)
1172    read_tx_mode_probs(&fc->tx_probs, &r);
1173  read_coef_probs(fc, cm->tx_mode, &r);
1174
1175  for (k = 0; k < SKIP_CONTEXTS; ++k)
1176    vp9_diff_update_prob(&r, &fc->skip_probs[k]);
1177
1178  if (!frame_is_intra_only(cm)) {
1179    nmv_context *const nmvc = &fc->nmvc;
1180    int i, j;
1181
1182    read_inter_mode_probs(fc, &r);
1183
1184    if (cm->interp_filter == SWITCHABLE)
1185      read_switchable_interp_probs(fc, &r);
1186
1187    for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
1188      vp9_diff_update_prob(&r, &fc->intra_inter_prob[i]);
1189
1190    cm->reference_mode = read_frame_reference_mode(cm, &r);
1191    if (cm->reference_mode != SINGLE_REFERENCE)
1192      setup_compound_reference_mode(cm);
1193    read_frame_reference_mode_probs(cm, &r);
1194
1195    for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
1196      for (i = 0; i < INTRA_MODES - 1; ++i)
1197        vp9_diff_update_prob(&r, &fc->y_mode_prob[j][i]);
1198
1199    for (j = 0; j < PARTITION_CONTEXTS; ++j)
1200      for (i = 0; i < PARTITION_TYPES - 1; ++i)
1201        vp9_diff_update_prob(&r, &fc->partition_prob[j][i]);
1202
1203    read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
1204  }
1205
1206  return vp9_reader_has_error(&r);
1207}
1208
1209void vp9_init_dequantizer(VP9_COMMON *cm) {
1210  int q;
1211
1212  for (q = 0; q < QINDEX_RANGE; q++) {
1213    cm->y_dequant[q][0] = vp9_dc_quant(q, cm->y_dc_delta_q);
1214    cm->y_dequant[q][1] = vp9_ac_quant(q, 0);
1215
1216    cm->uv_dequant[q][0] = vp9_dc_quant(q, cm->uv_dc_delta_q);
1217    cm->uv_dequant[q][1] = vp9_ac_quant(q, cm->uv_ac_delta_q);
1218  }
1219}
1220
1221#ifdef NDEBUG
1222#define debug_check_frame_counts(cm) (void)0
1223#else  // !NDEBUG
1224// Counts should only be incremented when frame_parallel_decoding_mode and
1225// error_resilient_mode are disabled.
1226static void debug_check_frame_counts(const VP9_COMMON *const cm) {
1227  FRAME_COUNTS zero_counts;
1228  vp9_zero(zero_counts);
1229  assert(cm->frame_parallel_decoding_mode || cm->error_resilient_mode);
1230  assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
1231                 sizeof(cm->counts.y_mode)));
1232  assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
1233                 sizeof(cm->counts.uv_mode)));
1234  assert(!memcmp(cm->counts.partition, zero_counts.partition,
1235                 sizeof(cm->counts.partition)));
1236  assert(!memcmp(cm->counts.coef, zero_counts.coef,
1237                 sizeof(cm->counts.coef)));
1238  assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
1239                 sizeof(cm->counts.eob_branch)));
1240  assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
1241                 sizeof(cm->counts.switchable_interp)));
1242  assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
1243                 sizeof(cm->counts.inter_mode)));
1244  assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
1245                 sizeof(cm->counts.intra_inter)));
1246  assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
1247                 sizeof(cm->counts.comp_inter)));
1248  assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
1249                 sizeof(cm->counts.single_ref)));
1250  assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
1251                 sizeof(cm->counts.comp_ref)));
1252  assert(!memcmp(&cm->counts.tx, &zero_counts.tx, sizeof(cm->counts.tx)));
1253  assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
1254  assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
1255}
1256#endif  // NDEBUG
1257
1258int vp9_decode_frame(VP9Decoder *pbi,
1259                     const uint8_t *data, const uint8_t *data_end,
1260                     const uint8_t **p_data_end) {
1261  VP9_COMMON *const cm = &pbi->common;
1262  MACROBLOCKD *const xd = &pbi->mb;
1263
1264  struct vp9_read_bit_buffer rb = { data, data_end, 0, cm, error_handler };
1265  const size_t first_partition_size = read_uncompressed_header(pbi, &rb);
1266  const int keyframe = cm->frame_type == KEY_FRAME;
1267  const int tile_rows = 1 << cm->log2_tile_rows;
1268  const int tile_cols = 1 << cm->log2_tile_cols;
1269  YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
1270  xd->cur_buf = new_fb;
1271
1272  if (!first_partition_size) {
1273      // showing a frame directly
1274      *p_data_end = data + 1;
1275      return 0;
1276  }
1277
1278  if (!pbi->decoded_key_frame && !keyframe)
1279    return -1;
1280
1281  data += vp9_rb_bytes_read(&rb);
1282  if (!read_is_valid(data, first_partition_size, data_end))
1283    vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1284                       "Truncated packet or corrupt header length");
1285
1286  pbi->do_loopfilter_inline =
1287      (cm->log2_tile_rows | cm->log2_tile_cols) == 0 && cm->lf.filter_level;
1288  if (pbi->do_loopfilter_inline && pbi->lf_worker.data1 == NULL) {
1289    CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
1290                    vpx_memalign(32, sizeof(LFWorkerData)));
1291    pbi->lf_worker.hook = (VP9WorkerHook)vp9_loop_filter_worker;
1292    if (pbi->oxcf.max_threads > 1 && !vp9_worker_reset(&pbi->lf_worker)) {
1293      vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
1294                         "Loop filter thread creation failed");
1295    }
1296  }
1297
1298  init_macroblockd(cm, &pbi->mb);
1299  cm->prev_mi = get_prev_mi(cm);
1300
1301  setup_plane_dequants(cm, xd, cm->base_qindex);
1302  vp9_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
1303
1304  cm->fc = cm->frame_contexts[cm->frame_context_idx];
1305  vp9_zero(cm->counts);
1306  vp9_zero(xd->dqcoeff);
1307
1308  xd->corrupted = 0;
1309  new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
1310
1311  // TODO(jzern): remove frame_parallel_decoding_mode restriction for
1312  // single-frame tile decoding.
1313  if (pbi->oxcf.max_threads > 1 && tile_rows == 1 && tile_cols > 1 &&
1314      cm->frame_parallel_decoding_mode) {
1315    *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
1316  } else {
1317    *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
1318  }
1319
1320  new_fb->corrupted |= xd->corrupted;
1321
1322  if (!pbi->decoded_key_frame) {
1323    if (keyframe && !new_fb->corrupted)
1324      pbi->decoded_key_frame = 1;
1325    else
1326      vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1327                         "A stream must start with a complete key frame");
1328  }
1329
1330  if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
1331    vp9_adapt_coef_probs(cm);
1332
1333    if (!frame_is_intra_only(cm)) {
1334      vp9_adapt_mode_probs(cm);
1335      vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
1336    }
1337  } else {
1338    debug_check_frame_counts(cm);
1339  }
1340
1341  if (cm->refresh_frame_context)
1342    cm->frame_contexts[cm->frame_context_idx] = cm->fc;
1343
1344  return 0;
1345}
1346