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