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