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