2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
12 #include <stdlib.h> // qsort()
14 #include "./vp9_rtcd.h"
15 #include "./vpx_scale_rtcd.h"
17 #include "vpx_mem/vpx_mem.h"
18 #include "vpx_ports/mem_ops.h"
19 #include "vpx_scale/vpx_scale.h"
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"
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"
43 #define MAX_VP9_HEADER_SIZE 80
45 static int is_compound_reference_allowed(const VP9_COMMON *cm) {
47 for (i = 1; i < REFS_PER_FRAME; ++i)
48 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1])
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;
66 cm->comp_fixed_ref = LAST_FRAME;
67 cm->comp_var_ref[0] = GOLDEN_FRAME;
68 cm->comp_var_ref[1] = ALTREF_FRAME;
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);
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;
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);
88 static void read_tx_mode_probs(struct tx_probs *tx_probs, vp9_reader *r) {
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]);
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]);
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]);
104 static void read_switchable_interp_probs(FRAME_CONTEXT *fc, vp9_reader *r) {
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]);
111 static void read_inter_mode_probs(FRAME_CONTEXT *fc, vp9_reader *r) {
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]);
118 static REFERENCE_MODE read_frame_reference_mode(const VP9_COMMON *cm,
120 if (is_compound_reference_allowed(cm)) {
121 return vp9_read_bit(r) ? (vp9_read_bit(r) ? REFERENCE_MODE_SELECT
122 : COMPOUND_REFERENCE)
125 return SINGLE_REFERENCE;
129 static void read_frame_reference_mode_probs(VP9_COMMON *cm, vp9_reader *r) {
130 FRAME_CONTEXT *const fc = &cm->fc;
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]);
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]);
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]);
148 static void update_mv_probs(vp9_prob *p, int n, vp9_reader *r) {
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;
155 static void read_mv_probs(nmv_context *ctx, int allow_hp, vp9_reader *r) {
158 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
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);
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);
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);
184 static void setup_plane_dequants(VP9_COMMON *cm, MACROBLOCKD *xd, int q_index) {
186 xd->plane[0].dequant = cm->y_dequant[q_index];
188 for (i = 1; i < MAX_MB_PLANE; i++)
189 xd->plane[i].dequant = cm->uv_dequant[q_index];
192 static void inverse_transform_block(MACROBLOCKD* xd, int plane, int block,
193 TX_SIZE tx_size, uint8_t *dst, int stride,
195 struct macroblockd_plane *const pd = &xd->plane[plane];
198 const PLANE_TYPE plane_type = pd->plane_type;
199 int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
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);
206 vp9_iht4x4_16_add(dqcoeff, dst, stride, tx_type);
209 tx_type = get_tx_type(plane_type, xd);
210 vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob);
213 tx_type = get_tx_type(plane_type, xd);
214 vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
218 vp9_idct32x32_add(dqcoeff, dst, stride, eob);
221 assert(0 && "Invalid transform size");
225 vpx_memset(dqcoeff, 0, 2 * sizeof(dqcoeff[0]));
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]));
232 vpx_memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
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 PREDICTION_MODE mode = (plane == 0) ? get_y_mode(mi, block)
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];
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,
263 if (!mi->mbmi.skip) {
264 const int eob = vp9_decode_block_tokens(cm, xd, plane, block,
265 plane_bsize, x, y, tx_size,
267 inverse_transform_block(xd, plane, block, tx_size, dst, pd->dst.stride,
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];
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,
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;
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;
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];
313 set_skip_context(xd, mi_row, mi_col);
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);
319 vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
320 return &xd->mi[0]->mbmi;
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,
333 xd->corrupted |= ref_buffer->buf->corrupted;
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);
348 reset_skip_context(xd, bsize);
351 setup_plane_dequants(cm, xd, vp9_get_qindex(&cm->seg, mbmi->segment_id,
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);
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);
366 vp9_dec_build_inter_predictors_sb(xd, mi_row, mi_col, bsize);
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
378 xd->corrupted |= vp9_reader_has_error(r);
381 static PARTITION_TYPE read_partition(VP9_COMMON *cm, MACROBLOCKD *xd, int hbs,
382 int mi_row, int mi_col, BLOCK_SIZE bsize,
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;
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;
399 if (!cm->frame_parallel_decoding_mode)
400 ++cm->counts.partition[ctx][p];
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;
413 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
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);
423 decode_block(cm, xd, tile, mi_row, mi_col, r, subsize);
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);
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);
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);
442 assert(0 && "Invalid partition type");
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);
452 static void setup_token_decoder(const uint8_t *data,
453 const uint8_t *data_end,
455 struct vpx_internal_error_info *error_info,
457 vpx_decrypt_cb decrypt_cb,
458 void *decrypt_state) {
459 // Validate the calculated partition length. If the buffer
460 // described by the partition can't be fully read, then restrict
461 // it to the portion that can be (for EC mode) or throw an error.
462 if (!read_is_valid(data, read_size, data_end))
463 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
464 "Truncated packet or corrupt tile length");
466 if (vp9_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
467 vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
468 "Failed to allocate bool decoder %d", 1);
471 static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
476 for (i = 0; i < PLANE_TYPES; ++i)
477 for (j = 0; j < REF_TYPES; ++j)
478 for (k = 0; k < COEF_BANDS; ++k)
479 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
480 for (m = 0; m < UNCONSTRAINED_NODES; ++m)
481 vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
484 static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode,
486 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
488 for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
489 read_coef_probs_common(fc->coef_probs[tx_size], r);
492 static void setup_segmentation(struct segmentation *seg,
493 struct vp9_read_bit_buffer *rb) {
497 seg->update_data = 0;
499 seg->enabled = vp9_rb_read_bit(rb);
503 // Segmentation map update
504 seg->update_map = vp9_rb_read_bit(rb);
505 if (seg->update_map) {
506 for (i = 0; i < SEG_TREE_PROBS; i++)
507 seg->tree_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
510 seg->temporal_update = vp9_rb_read_bit(rb);
511 if (seg->temporal_update) {
512 for (i = 0; i < PREDICTION_PROBS; i++)
513 seg->pred_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
516 for (i = 0; i < PREDICTION_PROBS; i++)
517 seg->pred_probs[i] = MAX_PROB;
521 // Segmentation data update
522 seg->update_data = vp9_rb_read_bit(rb);
523 if (seg->update_data) {
524 seg->abs_delta = vp9_rb_read_bit(rb);
526 vp9_clearall_segfeatures(seg);
528 for (i = 0; i < MAX_SEGMENTS; i++) {
529 for (j = 0; j < SEG_LVL_MAX; j++) {
531 const int feature_enabled = vp9_rb_read_bit(rb);
532 if (feature_enabled) {
533 vp9_enable_segfeature(seg, i, j);
534 data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
535 if (vp9_is_segfeature_signed(j))
536 data = vp9_rb_read_bit(rb) ? -data : data;
538 vp9_set_segdata(seg, i, j, data);
544 static void setup_loopfilter(struct loopfilter *lf,
545 struct vp9_read_bit_buffer *rb) {
546 lf->filter_level = vp9_rb_read_literal(rb, 6);
547 lf->sharpness_level = vp9_rb_read_literal(rb, 3);
549 // Read in loop filter deltas applied at the MB level based on mode or ref
551 lf->mode_ref_delta_update = 0;
553 lf->mode_ref_delta_enabled = vp9_rb_read_bit(rb);
554 if (lf->mode_ref_delta_enabled) {
555 lf->mode_ref_delta_update = vp9_rb_read_bit(rb);
556 if (lf->mode_ref_delta_update) {
559 for (i = 0; i < MAX_REF_LF_DELTAS; i++)
560 if (vp9_rb_read_bit(rb))
561 lf->ref_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
563 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
564 if (vp9_rb_read_bit(rb))
565 lf->mode_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
570 static int read_delta_q(struct vp9_read_bit_buffer *rb, int *delta_q) {
571 const int old = *delta_q;
572 *delta_q = vp9_rb_read_bit(rb) ? vp9_rb_read_signed_literal(rb, 4) : 0;
573 return old != *delta_q;
576 static void setup_quantization(VP9_COMMON *const cm, MACROBLOCKD *const xd,
577 struct vp9_read_bit_buffer *rb) {
580 cm->base_qindex = vp9_rb_read_literal(rb, QINDEX_BITS);
581 update |= read_delta_q(rb, &cm->y_dc_delta_q);
582 update |= read_delta_q(rb, &cm->uv_dc_delta_q);
583 update |= read_delta_q(rb, &cm->uv_ac_delta_q);
585 vp9_init_dequantizer(cm);
587 xd->lossless = cm->base_qindex == 0 &&
588 cm->y_dc_delta_q == 0 &&
589 cm->uv_dc_delta_q == 0 &&
590 cm->uv_ac_delta_q == 0;
592 xd->itxm_add = xd->lossless ? vp9_iwht4x4_add : vp9_idct4x4_add;
595 static INTERP_FILTER read_interp_filter(struct vp9_read_bit_buffer *rb) {
596 const INTERP_FILTER literal_to_filter[] = { EIGHTTAP_SMOOTH,
600 return vp9_rb_read_bit(rb) ? SWITCHABLE
601 : literal_to_filter[vp9_rb_read_literal(rb, 2)];
604 static void read_frame_size(struct vp9_read_bit_buffer *rb,
605 int *width, int *height) {
606 const int w = vp9_rb_read_literal(rb, 16) + 1;
607 const int h = vp9_rb_read_literal(rb, 16) + 1;
612 static void setup_display_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
613 cm->display_width = cm->width;
614 cm->display_height = cm->height;
615 if (vp9_rb_read_bit(rb))
616 read_frame_size(rb, &cm->display_width, &cm->display_height);
619 static void apply_frame_size(VP9_COMMON *cm, int width, int height) {
620 if (cm->width != width || cm->height != height) {
621 // Change in frame size.
622 // TODO(agrange) Don't test width/height, check overall size.
623 if (width > cm->width || height > cm->height) {
624 // Rescale frame buffers only if they're not big enough already.
625 if (vp9_resize_frame_buffers(cm, width, height))
626 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
627 "Failed to allocate frame buffers");
633 vp9_update_frame_size(cm);
636 if (vp9_realloc_frame_buffer(
637 get_frame_new_buffer(cm), cm->width, cm->height,
638 cm->subsampling_x, cm->subsampling_y, VP9_DEC_BORDER_IN_PIXELS,
639 &cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer, cm->get_fb_cb,
641 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
642 "Failed to allocate frame buffer");
646 static void setup_frame_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
648 read_frame_size(rb, &width, &height);
649 apply_frame_size(cm, width, height);
650 setup_display_size(cm, rb);
653 static void setup_frame_size_with_refs(VP9_COMMON *cm,
654 struct vp9_read_bit_buffer *rb) {
657 for (i = 0; i < REFS_PER_FRAME; ++i) {
658 if (vp9_rb_read_bit(rb)) {
659 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
660 width = buf->y_crop_width;
661 height = buf->y_crop_height;
668 read_frame_size(rb, &width, &height);
670 if (width <= 0 || height <= 0)
671 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
672 "Referenced frame with invalid size");
674 apply_frame_size(cm, width, height);
675 setup_display_size(cm, rb);
678 static void decode_tile(VP9Decoder *pbi, const TileInfo *const tile,
679 int do_loopfilter_inline, vp9_reader *r) {
680 const int num_threads = pbi->max_threads;
681 VP9_COMMON *const cm = &pbi->common;
683 MACROBLOCKD *xd = &pbi->mb;
685 if (do_loopfilter_inline) {
686 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
687 lf_data->frame_buffer = get_frame_new_buffer(cm);
689 vp9_copy(lf_data->planes, pbi->mb.plane);
692 vp9_loop_filter_frame_init(cm, cm->lf.filter_level);
695 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
696 mi_row += MI_BLOCK_SIZE) {
697 // For a SB there are 2 left contexts, each pertaining to a MB row within
698 vp9_zero(xd->left_context);
699 vp9_zero(xd->left_seg_context);
700 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
701 mi_col += MI_BLOCK_SIZE) {
702 decode_partition(cm, xd, tile, mi_row, mi_col, r, BLOCK_64X64);
705 if (do_loopfilter_inline) {
706 const int lf_start = mi_row - MI_BLOCK_SIZE;
707 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
709 // delay the loopfilter by 1 macroblock row.
710 if (lf_start < 0) continue;
712 // decoding has completed: finish up the loop filter in this thread.
713 if (mi_row + MI_BLOCK_SIZE >= tile->mi_row_end) continue;
715 vp9_worker_sync(&pbi->lf_worker);
716 lf_data->start = lf_start;
717 lf_data->stop = mi_row;
718 if (num_threads > 1) {
719 vp9_worker_launch(&pbi->lf_worker);
721 vp9_worker_execute(&pbi->lf_worker);
726 if (do_loopfilter_inline) {
727 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
729 vp9_worker_sync(&pbi->lf_worker);
730 lf_data->start = lf_data->stop;
731 lf_data->stop = cm->mi_rows;
732 vp9_worker_execute(&pbi->lf_worker);
736 static void setup_tile_info(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
737 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
738 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
741 max_ones = max_log2_tile_cols - min_log2_tile_cols;
742 cm->log2_tile_cols = min_log2_tile_cols;
743 while (max_ones-- && vp9_rb_read_bit(rb))
744 cm->log2_tile_cols++;
747 cm->log2_tile_rows = vp9_rb_read_bit(rb);
748 if (cm->log2_tile_rows)
749 cm->log2_tile_rows += vp9_rb_read_bit(rb);
752 typedef struct TileBuffer {
755 int col; // only used with multi-threaded decoding
758 // Reads the next tile returning its size and adjusting '*data' accordingly
759 // based on 'is_last'.
760 static void get_tile_buffer(const uint8_t *const data_end,
762 struct vpx_internal_error_info *error_info,
763 const uint8_t **data,
764 vpx_decrypt_cb decrypt_cb, void *decrypt_state,
769 if (!read_is_valid(*data, 4, data_end))
770 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
771 "Truncated packet or corrupt tile length");
775 decrypt_cb(decrypt_state, *data, be_data, 4);
776 size = mem_get_be32(be_data);
778 size = mem_get_be32(*data);
782 if (size > (size_t)(data_end - *data))
783 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
784 "Truncated packet or corrupt tile size");
786 size = data_end - *data;
795 static void get_tile_buffers(VP9Decoder *pbi,
796 const uint8_t *data, const uint8_t *data_end,
797 int tile_cols, int tile_rows,
798 TileBuffer (*tile_buffers)[1 << 6]) {
801 for (r = 0; r < tile_rows; ++r) {
802 for (c = 0; c < tile_cols; ++c) {
803 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
804 TileBuffer *const buf = &tile_buffers[r][c];
806 get_tile_buffer(data_end, is_last, &pbi->common.error, &data,
807 pbi->decrypt_cb, pbi->decrypt_state, buf);
812 static const uint8_t *decode_tiles(VP9Decoder *pbi,
814 const uint8_t *data_end,
815 int do_loopfilter_inline) {
816 VP9_COMMON *const cm = &pbi->common;
817 const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
818 const int tile_cols = 1 << cm->log2_tile_cols;
819 const int tile_rows = 1 << cm->log2_tile_rows;
820 TileBuffer tile_buffers[4][1 << 6];
821 int tile_row, tile_col;
822 const uint8_t *end = NULL;
825 assert(tile_rows <= 4);
826 assert(tile_cols <= (1 << 6));
828 // Note: this memset assumes above_context[0], [1] and [2]
829 // are allocated as part of the same buffer.
830 vpx_memset(cm->above_context, 0,
831 sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_cols);
833 vpx_memset(cm->above_seg_context, 0,
834 sizeof(*cm->above_seg_context) * aligned_cols);
836 get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, tile_buffers);
838 // Decode tiles using data from tile_buffers
839 for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
840 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
841 const int col = pbi->inv_tile_order ? tile_cols - tile_col - 1 : tile_col;
842 const int last_tile = tile_row == tile_rows - 1 &&
843 col == tile_cols - 1;
844 const TileBuffer *const buf = &tile_buffers[tile_row][col];
847 vp9_tile_init(&tile, cm, tile_row, col);
848 setup_token_decoder(buf->data, data_end, buf->size, &cm->error, &r,
849 pbi->decrypt_cb, pbi->decrypt_state);
850 decode_tile(pbi, &tile, do_loopfilter_inline, &r);
853 end = vp9_reader_find_end(&r);
860 static int tile_worker_hook(void *arg1, void *arg2) {
861 TileWorkerData *const tile_data = (TileWorkerData*)arg1;
862 const TileInfo *const tile = (TileInfo*)arg2;
865 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
866 mi_row += MI_BLOCK_SIZE) {
867 vp9_zero(tile_data->xd.left_context);
868 vp9_zero(tile_data->xd.left_seg_context);
869 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
870 mi_col += MI_BLOCK_SIZE) {
871 decode_partition(tile_data->cm, &tile_data->xd, tile,
872 mi_row, mi_col, &tile_data->bit_reader, BLOCK_64X64);
875 return !tile_data->xd.corrupted;
878 // sorts in descending order
879 static int compare_tile_buffers(const void *a, const void *b) {
880 const TileBuffer *const buf1 = (const TileBuffer*)a;
881 const TileBuffer *const buf2 = (const TileBuffer*)b;
882 if (buf1->size < buf2->size) {
884 } else if (buf1->size == buf2->size) {
891 static const uint8_t *decode_tiles_mt(VP9Decoder *pbi,
893 const uint8_t *data_end) {
894 VP9_COMMON *const cm = &pbi->common;
895 const uint8_t *bit_reader_end = NULL;
896 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
897 const int tile_cols = 1 << cm->log2_tile_cols;
898 const int tile_rows = 1 << cm->log2_tile_rows;
899 const int num_workers = MIN(pbi->max_threads & ~1, tile_cols);
900 TileBuffer tile_buffers[1][1 << 6];
902 int final_worker = -1;
904 assert(tile_cols <= (1 << 6));
905 assert(tile_rows == 1);
908 // TODO(jzern): See if we can remove the restriction of passing in max
909 // threads to the decoder.
910 if (pbi->num_tile_workers == 0) {
911 const int num_threads = pbi->max_threads & ~1;
913 // TODO(jzern): Allocate one less worker, as in the current code we only
914 // use num_threads - 1 workers.
915 CHECK_MEM_ERROR(cm, pbi->tile_workers,
916 vpx_malloc(num_threads * sizeof(*pbi->tile_workers)));
917 for (i = 0; i < num_threads; ++i) {
918 VP9Worker *const worker = &pbi->tile_workers[i];
919 ++pbi->num_tile_workers;
921 vp9_worker_init(worker);
922 CHECK_MEM_ERROR(cm, worker->data1,
923 vpx_memalign(32, sizeof(TileWorkerData)));
924 CHECK_MEM_ERROR(cm, worker->data2, vpx_malloc(sizeof(TileInfo)));
925 if (i < num_threads - 1 && !vp9_worker_reset(worker)) {
926 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
927 "Tile decoder thread creation failed");
932 // Reset tile decoding hook
933 for (n = 0; n < num_workers; ++n) {
934 pbi->tile_workers[n].hook = (VP9WorkerHook)tile_worker_hook;
937 // Note: this memset assumes above_context[0], [1] and [2]
938 // are allocated as part of the same buffer.
939 vpx_memset(cm->above_context, 0,
940 sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_mi_cols);
941 vpx_memset(cm->above_seg_context, 0,
942 sizeof(*cm->above_seg_context) * aligned_mi_cols);
944 // Load tile data into tile_buffers
945 get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, tile_buffers);
947 // Sort the buffers based on size in descending order.
948 qsort(tile_buffers[0], tile_cols, sizeof(tile_buffers[0][0]),
949 compare_tile_buffers);
951 // Rearrange the tile buffers such that per-tile group the largest, and
952 // presumably the most difficult, tile will be decoded in the main thread.
953 // This should help minimize the number of instances where the main thread is
954 // waiting for a worker to complete.
957 while (group_start < tile_cols) {
958 const TileBuffer largest = tile_buffers[0][group_start];
959 const int group_end = MIN(group_start + num_workers, tile_cols) - 1;
960 memmove(tile_buffers[0] + group_start, tile_buffers[0] + group_start + 1,
961 (group_end - group_start) * sizeof(tile_buffers[0][0]));
962 tile_buffers[0][group_end] = largest;
963 group_start = group_end + 1;
968 while (n < tile_cols) {
970 for (i = 0; i < num_workers && n < tile_cols; ++i) {
971 VP9Worker *const worker = &pbi->tile_workers[i];
972 TileWorkerData *const tile_data = (TileWorkerData*)worker->data1;
973 TileInfo *const tile = (TileInfo*)worker->data2;
974 TileBuffer *const buf = &tile_buffers[0][n];
977 tile_data->xd = pbi->mb;
978 tile_data->xd.corrupted = 0;
979 vp9_tile_init(tile, tile_data->cm, 0, buf->col);
980 setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
981 &tile_data->bit_reader, pbi->decrypt_cb,
983 init_macroblockd(cm, &tile_data->xd);
984 vp9_zero(tile_data->xd.dqcoeff);
986 worker->had_error = 0;
987 if (i == num_workers - 1 || n == tile_cols - 1) {
988 vp9_worker_execute(worker);
990 vp9_worker_launch(worker);
993 if (buf->col == tile_cols - 1) {
1000 for (; i > 0; --i) {
1001 VP9Worker *const worker = &pbi->tile_workers[i - 1];
1002 pbi->mb.corrupted |= !vp9_worker_sync(worker);
1004 if (final_worker > -1) {
1005 TileWorkerData *const tile_data =
1006 (TileWorkerData*)pbi->tile_workers[final_worker].data1;
1007 bit_reader_end = vp9_reader_find_end(&tile_data->bit_reader);
1012 return bit_reader_end;
1015 static void check_sync_code(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
1016 if (vp9_rb_read_literal(rb, 8) != VP9_SYNC_CODE_0 ||
1017 vp9_rb_read_literal(rb, 8) != VP9_SYNC_CODE_1 ||
1018 vp9_rb_read_literal(rb, 8) != VP9_SYNC_CODE_2) {
1019 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1020 "Invalid frame sync code");
1024 static void error_handler(void *data) {
1025 VP9_COMMON *const cm = (VP9_COMMON *)data;
1026 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet");
1029 static BITSTREAM_PROFILE read_profile(struct vp9_read_bit_buffer *rb) {
1030 int profile = vp9_rb_read_bit(rb);
1031 profile |= vp9_rb_read_bit(rb) << 1;
1032 return (BITSTREAM_PROFILE) profile;
1035 static size_t read_uncompressed_header(VP9Decoder *pbi,
1036 struct vp9_read_bit_buffer *rb) {
1037 VP9_COMMON *const cm = &pbi->common;
1041 cm->last_frame_type = cm->frame_type;
1043 if (vp9_rb_read_literal(rb, 2) != VP9_FRAME_MARKER)
1044 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1045 "Invalid frame marker");
1047 cm->profile = read_profile(rb);
1048 if (cm->profile >= MAX_PROFILES)
1049 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1050 "Unsupported bitstream profile");
1052 cm->show_existing_frame = vp9_rb_read_bit(rb);
1053 if (cm->show_existing_frame) {
1054 // Show an existing frame directly.
1055 const int frame_to_show = cm->ref_frame_map[vp9_rb_read_literal(rb, 3)];
1057 if (cm->frame_bufs[frame_to_show].ref_count < 1)
1058 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1059 "Buffer %d does not contain a decoded frame",
1062 ref_cnt_fb(cm->frame_bufs, &cm->new_fb_idx, frame_to_show);
1063 pbi->refresh_frame_flags = 0;
1064 cm->lf.filter_level = 0;
1069 cm->frame_type = (FRAME_TYPE) vp9_rb_read_bit(rb);
1070 cm->show_frame = vp9_rb_read_bit(rb);
1071 cm->error_resilient_mode = vp9_rb_read_bit(rb);
1073 if (cm->frame_type == KEY_FRAME) {
1074 check_sync_code(cm, rb);
1075 if (cm->profile > PROFILE_1)
1076 cm->bit_depth = vp9_rb_read_bit(rb) ? BITS_12 : BITS_10;
1077 cm->color_space = (COLOR_SPACE)vp9_rb_read_literal(rb, 3);
1078 if (cm->color_space != SRGB) {
1079 vp9_rb_read_bit(rb); // [16,235] (including xvycc) vs [0,255] range
1080 if (cm->profile >= PROFILE_1) {
1081 cm->subsampling_x = vp9_rb_read_bit(rb);
1082 cm->subsampling_y = vp9_rb_read_bit(rb);
1083 vp9_rb_read_bit(rb); // has extra plane
1085 cm->subsampling_y = cm->subsampling_x = 1;
1088 if (cm->profile >= PROFILE_1) {
1089 cm->subsampling_y = cm->subsampling_x = 0;
1090 vp9_rb_read_bit(rb); // has extra plane
1092 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1093 "RGB not supported in profile 0");
1097 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
1099 for (i = 0; i < REFS_PER_FRAME; ++i) {
1100 cm->frame_refs[i].idx = cm->new_fb_idx;
1101 cm->frame_refs[i].buf = get_frame_new_buffer(cm);
1104 setup_frame_size(cm, rb);
1106 cm->intra_only = cm->show_frame ? 0 : vp9_rb_read_bit(rb);
1108 cm->reset_frame_context = cm->error_resilient_mode ?
1109 0 : vp9_rb_read_literal(rb, 2);
1111 if (cm->intra_only) {
1112 check_sync_code(cm, rb);
1114 pbi->refresh_frame_flags = vp9_rb_read_literal(rb, REF_FRAMES);
1115 setup_frame_size(cm, rb);
1117 pbi->refresh_frame_flags = vp9_rb_read_literal(rb, REF_FRAMES);
1119 for (i = 0; i < REFS_PER_FRAME; ++i) {
1120 const int ref = vp9_rb_read_literal(rb, REF_FRAMES_LOG2);
1121 const int idx = cm->ref_frame_map[ref];
1122 cm->frame_refs[i].idx = idx;
1123 cm->frame_refs[i].buf = &cm->frame_bufs[idx].buf;
1124 cm->ref_frame_sign_bias[LAST_FRAME + i] = vp9_rb_read_bit(rb);
1127 setup_frame_size_with_refs(cm, rb);
1129 cm->allow_high_precision_mv = vp9_rb_read_bit(rb);
1130 cm->interp_filter = read_interp_filter(rb);
1132 for (i = 0; i < REFS_PER_FRAME; ++i) {
1133 RefBuffer *const ref_buf = &cm->frame_refs[i];
1134 vp9_setup_scale_factors_for_frame(&ref_buf->sf,
1135 ref_buf->buf->y_crop_width,
1136 ref_buf->buf->y_crop_height,
1137 cm->width, cm->height);
1138 if (vp9_is_scaled(&ref_buf->sf))
1139 vp9_extend_frame_borders(ref_buf->buf);
1144 if (!cm->error_resilient_mode) {
1145 cm->coding_use_prev_mi = 1;
1146 cm->refresh_frame_context = vp9_rb_read_bit(rb);
1147 cm->frame_parallel_decoding_mode = vp9_rb_read_bit(rb);
1149 cm->coding_use_prev_mi = 0;
1150 cm->refresh_frame_context = 0;
1151 cm->frame_parallel_decoding_mode = 1;
1154 // This flag will be overridden by the call to vp9_setup_past_independence
1155 // below, forcing the use of context 0 for those frame types.
1156 cm->frame_context_idx = vp9_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
1158 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
1159 vp9_setup_past_independence(cm);
1161 setup_loopfilter(&cm->lf, rb);
1162 setup_quantization(cm, &pbi->mb, rb);
1163 setup_segmentation(&cm->seg, rb);
1165 setup_tile_info(cm, rb);
1166 sz = vp9_rb_read_literal(rb, 16);
1169 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1170 "Invalid header size");
1175 static int read_compressed_header(VP9Decoder *pbi, const uint8_t *data,
1176 size_t partition_size) {
1177 VP9_COMMON *const cm = &pbi->common;
1178 MACROBLOCKD *const xd = &pbi->mb;
1179 FRAME_CONTEXT *const fc = &cm->fc;
1183 if (vp9_reader_init(&r, data, partition_size, pbi->decrypt_cb,
1184 pbi->decrypt_state))
1185 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1186 "Failed to allocate bool decoder 0");
1188 cm->tx_mode = xd->lossless ? ONLY_4X4 : read_tx_mode(&r);
1189 if (cm->tx_mode == TX_MODE_SELECT)
1190 read_tx_mode_probs(&fc->tx_probs, &r);
1191 read_coef_probs(fc, cm->tx_mode, &r);
1193 for (k = 0; k < SKIP_CONTEXTS; ++k)
1194 vp9_diff_update_prob(&r, &fc->skip_probs[k]);
1196 if (!frame_is_intra_only(cm)) {
1197 nmv_context *const nmvc = &fc->nmvc;
1200 read_inter_mode_probs(fc, &r);
1202 if (cm->interp_filter == SWITCHABLE)
1203 read_switchable_interp_probs(fc, &r);
1205 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
1206 vp9_diff_update_prob(&r, &fc->intra_inter_prob[i]);
1208 cm->reference_mode = read_frame_reference_mode(cm, &r);
1209 if (cm->reference_mode != SINGLE_REFERENCE)
1210 setup_compound_reference_mode(cm);
1211 read_frame_reference_mode_probs(cm, &r);
1213 for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
1214 for (i = 0; i < INTRA_MODES - 1; ++i)
1215 vp9_diff_update_prob(&r, &fc->y_mode_prob[j][i]);
1217 for (j = 0; j < PARTITION_CONTEXTS; ++j)
1218 for (i = 0; i < PARTITION_TYPES - 1; ++i)
1219 vp9_diff_update_prob(&r, &fc->partition_prob[j][i]);
1221 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
1224 return vp9_reader_has_error(&r);
1227 void vp9_init_dequantizer(VP9_COMMON *cm) {
1230 for (q = 0; q < QINDEX_RANGE; q++) {
1231 cm->y_dequant[q][0] = vp9_dc_quant(q, cm->y_dc_delta_q);
1232 cm->y_dequant[q][1] = vp9_ac_quant(q, 0);
1234 cm->uv_dequant[q][0] = vp9_dc_quant(q, cm->uv_dc_delta_q);
1235 cm->uv_dequant[q][1] = vp9_ac_quant(q, cm->uv_ac_delta_q);
1240 #define debug_check_frame_counts(cm) (void)0
1242 // Counts should only be incremented when frame_parallel_decoding_mode and
1243 // error_resilient_mode are disabled.
1244 static void debug_check_frame_counts(const VP9_COMMON *const cm) {
1245 FRAME_COUNTS zero_counts;
1246 vp9_zero(zero_counts);
1247 assert(cm->frame_parallel_decoding_mode || cm->error_resilient_mode);
1248 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
1249 sizeof(cm->counts.y_mode)));
1250 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
1251 sizeof(cm->counts.uv_mode)));
1252 assert(!memcmp(cm->counts.partition, zero_counts.partition,
1253 sizeof(cm->counts.partition)));
1254 assert(!memcmp(cm->counts.coef, zero_counts.coef,
1255 sizeof(cm->counts.coef)));
1256 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
1257 sizeof(cm->counts.eob_branch)));
1258 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
1259 sizeof(cm->counts.switchable_interp)));
1260 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
1261 sizeof(cm->counts.inter_mode)));
1262 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
1263 sizeof(cm->counts.intra_inter)));
1264 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
1265 sizeof(cm->counts.comp_inter)));
1266 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
1267 sizeof(cm->counts.single_ref)));
1268 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
1269 sizeof(cm->counts.comp_ref)));
1270 assert(!memcmp(&cm->counts.tx, &zero_counts.tx, sizeof(cm->counts.tx)));
1271 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
1272 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
1276 static struct vp9_read_bit_buffer* init_read_bit_buffer(
1278 struct vp9_read_bit_buffer *rb,
1279 const uint8_t *data,
1280 const uint8_t *data_end,
1281 uint8_t *clear_data /* buffer size MAX_VP9_HEADER_SIZE */) {
1284 rb->error_handler = error_handler;
1285 rb->error_handler_data = &pbi->common;
1286 if (pbi->decrypt_cb) {
1287 const int n = (int)MIN(MAX_VP9_HEADER_SIZE, data_end - data);
1288 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
1289 rb->bit_buffer = clear_data;
1290 rb->bit_buffer_end = clear_data + n;
1292 rb->bit_buffer = data;
1293 rb->bit_buffer_end = data_end;
1298 int vp9_decode_frame(VP9Decoder *pbi,
1299 const uint8_t *data, const uint8_t *data_end,
1300 const uint8_t **p_data_end) {
1301 VP9_COMMON *const cm = &pbi->common;
1302 MACROBLOCKD *const xd = &pbi->mb;
1303 struct vp9_read_bit_buffer rb;
1304 uint8_t clear_data[MAX_VP9_HEADER_SIZE];
1305 const size_t first_partition_size = read_uncompressed_header(pbi,
1306 init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
1307 const int keyframe = cm->frame_type == KEY_FRAME;
1308 const int tile_rows = 1 << cm->log2_tile_rows;
1309 const int tile_cols = 1 << cm->log2_tile_cols;
1310 YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
1311 const int do_loopfilter_inline = tile_rows == 1 && tile_cols == 1 &&
1312 cm->lf.filter_level;
1313 xd->cur_buf = new_fb;
1315 if (!first_partition_size) {
1316 // showing a frame directly
1317 *p_data_end = data + 1;
1321 if (!pbi->decoded_key_frame && !keyframe)
1324 data += vp9_rb_bytes_read(&rb);
1325 if (!read_is_valid(data, first_partition_size, data_end))
1326 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1327 "Truncated packet or corrupt header length");
1329 init_macroblockd(cm, &pbi->mb);
1331 if (cm->coding_use_prev_mi)
1336 setup_plane_dequants(cm, xd, cm->base_qindex);
1337 vp9_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
1339 cm->fc = cm->frame_contexts[cm->frame_context_idx];
1340 vp9_zero(cm->counts);
1341 vp9_zero(xd->dqcoeff);
1344 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
1346 // TODO(jzern): remove frame_parallel_decoding_mode restriction for
1347 // single-frame tile decoding.
1348 if (pbi->max_threads > 1 && tile_rows == 1 && tile_cols > 1 &&
1349 cm->frame_parallel_decoding_mode) {
1350 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
1351 // If multiple threads are used to decode tiles, then we use those threads
1352 // to do parallel loopfiltering.
1353 vp9_loop_filter_frame_mt(new_fb, pbi, cm, cm->lf.filter_level, 0);
1355 if (do_loopfilter_inline && pbi->lf_worker.data1 == NULL) {
1356 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
1357 vpx_memalign(32, sizeof(LFWorkerData)));
1358 pbi->lf_worker.hook = (VP9WorkerHook)vp9_loop_filter_worker;
1359 if (pbi->max_threads > 1 && !vp9_worker_reset(&pbi->lf_worker)) {
1360 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
1361 "Loop filter thread creation failed");
1364 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end,
1365 do_loopfilter_inline);
1366 if (!do_loopfilter_inline)
1367 vp9_loop_filter_frame(new_fb, cm, &pbi->mb, cm->lf.filter_level, 0, 0);
1370 new_fb->corrupted |= xd->corrupted;
1372 if (!pbi->decoded_key_frame) {
1373 if (keyframe && !new_fb->corrupted)
1374 pbi->decoded_key_frame = 1;
1376 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1377 "A stream must start with a complete key frame");
1380 if (!new_fb->corrupted) {
1381 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
1382 vp9_adapt_coef_probs(cm);
1384 if (!frame_is_intra_only(cm)) {
1385 vp9_adapt_mode_probs(cm);
1386 vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
1389 debug_check_frame_counts(cm);
1393 if (cm->refresh_frame_context)
1394 cm->frame_contexts[cm->frame_context_idx] = cm->fc;