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