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.
13 #include "./vp9_rtcd.h"
14 #include "vpx_mem/vpx_mem.h"
15 #include "vpx_scale/vpx_scale.h"
17 #include "vp9/common/vp9_alloccommon.h"
18 #include "vp9/common/vp9_common.h"
19 #include "vp9/common/vp9_entropy.h"
20 #include "vp9/common/vp9_entropymode.h"
21 #include "vp9/common/vp9_extend.h"
22 #include "vp9/common/vp9_pred_common.h"
23 #include "vp9/common/vp9_quant_common.h"
24 #include "vp9/common/vp9_reconintra.h"
25 #include "vp9/common/vp9_reconinter.h"
26 #include "vp9/common/vp9_seg_common.h"
27 #include "vp9/common/vp9_tile_common.h"
29 #include "vp9/decoder/vp9_dboolhuff.h"
30 #include "vp9/decoder/vp9_decodframe.h"
31 #include "vp9/decoder/vp9_detokenize.h"
32 #include "vp9/decoder/vp9_decodemv.h"
33 #include "vp9/decoder/vp9_dsubexp.h"
34 #include "vp9/decoder/vp9_idct_blk.h"
35 #include "vp9/decoder/vp9_onyxd_int.h"
36 #include "vp9/decoder/vp9_read_bit_buffer.h"
37 #include "vp9/decoder/vp9_thread.h"
38 #include "vp9/decoder/vp9_treereader.h"
40 static int read_be32(const uint8_t *p) {
41 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
44 // len == 0 is not allowed
45 static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
46 return start + len > start && start + len <= end;
49 static int decode_unsigned_max(struct vp9_read_bit_buffer *rb, int max) {
50 const int data = vp9_rb_read_literal(rb, get_unsigned_bits(max));
51 return data > max ? max : data;
54 static TX_MODE read_tx_mode(vp9_reader *r) {
55 TX_MODE tx_mode = vp9_read_literal(r, 2);
56 if (tx_mode == ALLOW_32X32)
57 tx_mode += vp9_read_bit(r);
61 static void read_tx_probs(struct tx_probs *tx_probs, vp9_reader *r) {
64 for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
65 for (j = 0; j < TX_SIZES - 3; ++j)
66 if (vp9_read(r, VP9_MODE_UPDATE_PROB))
67 vp9_diff_update_prob(r, &tx_probs->p8x8[i][j]);
69 for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
70 for (j = 0; j < TX_SIZES - 2; ++j)
71 if (vp9_read(r, VP9_MODE_UPDATE_PROB))
72 vp9_diff_update_prob(r, &tx_probs->p16x16[i][j]);
74 for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
75 for (j = 0; j < TX_SIZES - 1; ++j)
76 if (vp9_read(r, VP9_MODE_UPDATE_PROB))
77 vp9_diff_update_prob(r, &tx_probs->p32x32[i][j]);
80 static void init_dequantizer(VP9_COMMON *cm, MACROBLOCKD *xd) {
82 const int segment_id = xd->mode_info_context->mbmi.segment_id;
83 xd->q_index = vp9_get_qindex(xd, segment_id, cm->base_qindex);
85 xd->plane[0].dequant = cm->y_dequant[xd->q_index];
86 for (i = 1; i < MAX_MB_PLANE; i++)
87 xd->plane[i].dequant = cm->uv_dequant[xd->q_index];
90 static void decode_block(int plane, int block, BLOCK_SIZE_TYPE bsize,
91 int ss_txfrm_size, void *arg) {
92 MACROBLOCKD* const xd = arg;
93 struct macroblockd_plane *pd = &xd->plane[plane];
94 int16_t* const qcoeff = BLOCK_OFFSET(pd->qcoeff, block);
95 const int stride = pd->dst.stride;
96 const int eob = pd->eobs[block];
97 const int raster_block = txfrm_block_to_raster_block(xd, bsize, plane,
98 block, ss_txfrm_size);
99 uint8_t* const dst = raster_block_offset_uint8(xd, bsize, plane,
101 pd->dst.buf, stride);
102 const TX_SIZE tx_size = (TX_SIZE)(ss_txfrm_size >> 1);
106 const TX_TYPE tx_type = get_tx_type_4x4(pd->plane_type, xd, raster_block);
107 if (tx_type == DCT_DCT)
108 xd->itxm_add(qcoeff, dst, stride, eob);
110 vp9_iht_add_c(tx_type, qcoeff, dst, stride, eob);
114 vp9_iht_add_8x8_c(get_tx_type_8x8(pd->plane_type, xd), qcoeff, dst,
118 vp9_iht_add_16x16_c(get_tx_type_16x16(pd->plane_type, xd), qcoeff, dst,
122 vp9_idct_add_32x32(qcoeff, dst, stride, eob);
125 assert(!"Invalid transform size");
129 static void decode_block_intra(int plane, int block, BLOCK_SIZE_TYPE bsize,
130 int ss_txfrm_size, void *arg) {
131 MACROBLOCKD* const xd = arg;
132 struct macroblockd_plane *pd = &xd->plane[plane];
133 MODE_INFO *const mi = xd->mode_info_context;
135 const int raster_block = txfrm_block_to_raster_block(xd, bsize, plane,
136 block, ss_txfrm_size);
137 uint8_t* const dst = raster_block_offset_uint8(xd, bsize, plane,
139 pd->dst.buf, pd->dst.stride);
140 const TX_SIZE tx_size = (TX_SIZE)(ss_txfrm_size >> 1);
143 const int tx_ib = raster_block >> tx_size;
144 const int mode = plane == 0 ? mi->mbmi.mode
147 if (plane == 0 && mi->mbmi.sb_type < BLOCK_8X8) {
148 assert(bsize == BLOCK_8X8);
149 b_mode = mi->bmi[raster_block].as_mode;
154 if (xd->mb_to_right_edge < 0 || xd->mb_to_bottom_edge < 0)
155 extend_for_intra(xd, plane, block, bsize, ss_txfrm_size);
157 plane_b_size = b_width_log2(bsize) - pd->subsampling_x;
158 vp9_predict_intra_block(xd, tx_ib, plane_b_size, tx_size, b_mode,
160 dst, pd->dst.stride);
162 // Early exit if there are no coefficients
163 if (mi->mbmi.skip_coeff)
166 decode_block(plane, block, bsize, ss_txfrm_size, arg);
169 static int decode_tokens(VP9D_COMP *pbi, BLOCK_SIZE_TYPE bsize, vp9_reader *r) {
170 MACROBLOCKD *const xd = &pbi->mb;
172 if (xd->mode_info_context->mbmi.skip_coeff) {
173 reset_skip_context(xd, bsize);
177 init_dequantizer(&pbi->common, xd);
179 // TODO(dkovalev) if (!vp9_reader_has_error(r))
180 return vp9_decode_tokens(pbi, r, bsize);
184 static void set_offsets(VP9D_COMP *pbi, BLOCK_SIZE_TYPE bsize,
185 int mi_row, int mi_col) {
186 VP9_COMMON *const cm = &pbi->common;
187 MACROBLOCKD *const xd = &pbi->mb;
188 const int bh = 1 << mi_height_log2(bsize);
189 const int bw = 1 << mi_width_log2(bsize);
190 const int mi_idx = mi_row * cm->mode_info_stride + mi_col;
192 xd->mode_info_context = cm->mi + mi_idx;
193 xd->mode_info_context->mbmi.sb_type = bsize;
194 // Special case: if prev_mi is NULL, the previous mode info context
196 xd->prev_mode_info_context = cm->prev_mi ? cm->prev_mi + mi_idx : NULL;
199 set_skip_context(cm, xd, mi_row, mi_col);
200 set_partition_seg_context(cm, xd, mi_row, mi_col);
202 // Distance of Mb to the various image edges. These are specified to 8th pel
203 // as they are always compared to values that are in 1/8th pel units
204 set_mi_row_col(cm, xd, mi_row, bh, mi_col, bw);
206 setup_dst_planes(xd, &cm->yv12_fb[cm->new_fb_idx], mi_row, mi_col);
209 static void set_ref(VP9D_COMP *pbi, int i, int mi_row, int mi_col) {
210 VP9_COMMON *const cm = &pbi->common;
211 MACROBLOCKD *const xd = &pbi->mb;
212 MB_MODE_INFO *const mbmi = &xd->mode_info_context->mbmi;
213 const int ref = mbmi->ref_frame[i] - 1;
215 const YV12_BUFFER_CONFIG *cfg = &cm->yv12_fb[cm->active_ref_idx[ref]];
216 xd->scale_factor[i] = cm->active_ref_scale[ref];
217 setup_pre_planes(xd, i, cfg, mi_row, mi_col, &xd->scale_factor[i]);
218 xd->corrupted |= cfg->corrupted;
221 static void decode_modes_b(VP9D_COMP *pbi, int mi_row, int mi_col,
222 vp9_reader *r, BLOCK_SIZE_TYPE bsize) {
223 VP9_COMMON *const cm = &pbi->common;
224 MACROBLOCKD *const xd = &pbi->mb;
225 const int less8x8 = bsize < BLOCK_8X8;
229 if (xd->ab_index > 0)
232 set_offsets(pbi, bsize, mi_row, mi_col);
233 vp9_read_mode_info(pbi, mi_row, mi_col, r);
238 // Has to be called after set_offsets
239 mbmi = &xd->mode_info_context->mbmi;
241 if (!is_inter_block(mbmi)) {
242 // Intra reconstruction
243 decode_tokens(pbi, bsize, r);
244 foreach_transformed_block(xd, bsize, decode_block_intra, xd);
246 // Inter reconstruction
249 set_ref(pbi, 0, mi_row, mi_col);
250 if (mbmi->ref_frame[1] > INTRA_FRAME)
251 set_ref(pbi, 1, mi_row, mi_col);
253 vp9_setup_interp_filters(xd, mbmi->interp_filter, cm);
254 vp9_build_inter_predictors_sb(xd, mi_row, mi_col, bsize);
255 eobtotal = decode_tokens(pbi, bsize, r);
258 foreach_transformed_block(xd, bsize, decode_block, xd);
260 assert(mbmi->sb_type == bsize);
263 vp9_set_pred_flag_mbskip(cm, bsize, mi_row, mi_col, 1);
264 else if (eobtotal > 0)
265 foreach_transformed_block(xd, bsize, decode_block, xd);
268 xd->corrupted |= vp9_reader_has_error(r);
271 static void decode_modes_sb(VP9D_COMP *pbi, int mi_row, int mi_col,
272 vp9_reader* r, BLOCK_SIZE_TYPE bsize) {
273 VP9_COMMON *const pc = &pbi->common;
274 MACROBLOCKD *const xd = &pbi->mb;
275 int bs = (1 << mi_width_log2(bsize)) / 2, n;
276 PARTITION_TYPE partition = PARTITION_NONE;
277 BLOCK_SIZE_TYPE subsize;
279 if (mi_row >= pc->mi_rows || mi_col >= pc->mi_cols)
282 if (bsize < BLOCK_8X8) {
283 if (xd->ab_index != 0)
287 const int idx = check_bsize_coverage(pc, mi_row, mi_col, bsize);
288 set_partition_seg_context(pc, xd, mi_row, mi_col);
289 pl = partition_plane_context(xd, bsize);
292 partition = treed_read(r, vp9_partition_tree,
293 pc->fc.partition_prob[pc->frame_type][pl]);
295 !vp9_read(r, pc->fc.partition_prob[pc->frame_type][pl][idx]))
296 partition = (idx == 1) ? PARTITION_HORZ : PARTITION_VERT;
298 partition = PARTITION_SPLIT;
300 pc->counts.partition[pl][partition]++;
303 subsize = get_subsize(bsize, partition);
304 *(get_sb_index(xd, subsize)) = 0;
308 decode_modes_b(pbi, mi_row, mi_col, r, subsize);
311 decode_modes_b(pbi, mi_row, mi_col, r, subsize);
312 *(get_sb_index(xd, subsize)) = 1;
313 if (mi_row + bs < pc->mi_rows)
314 decode_modes_b(pbi, mi_row + bs, mi_col, r, subsize);
317 decode_modes_b(pbi, mi_row, mi_col, r, subsize);
318 *(get_sb_index(xd, subsize)) = 1;
319 if (mi_col + bs < pc->mi_cols)
320 decode_modes_b(pbi, mi_row, mi_col + bs, r, subsize);
322 case PARTITION_SPLIT:
323 for (n = 0; n < 4; n++) {
324 int j = n >> 1, i = n & 0x01;
325 *(get_sb_index(xd, subsize)) = n;
326 decode_modes_sb(pbi, mi_row + j * bs, mi_col + i * bs, r, subsize);
330 assert(!"Invalid partition type");
333 // update partition context
334 if (bsize >= BLOCK_8X8 &&
335 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT)) {
336 set_partition_seg_context(pc, xd, mi_row, mi_col);
337 update_partition_context(xd, subsize, bsize);
341 static void setup_token_decoder(VP9D_COMP *pbi,
342 const uint8_t *data, size_t read_size,
344 VP9_COMMON *pc = &pbi->common;
345 const uint8_t *data_end = pbi->source + pbi->source_sz;
347 // Validate the calculated partition length. If the buffer
348 // described by the partition can't be fully read, then restrict
349 // it to the portion that can be (for EC mode) or throw an error.
350 if (!read_is_valid(data, read_size, data_end))
351 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
352 "Truncated packet or corrupt tile length");
354 if (vp9_reader_init(r, data, read_size))
355 vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
356 "Failed to allocate bool decoder %d", 1);
359 static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
364 for (i = 0; i < BLOCK_TYPES; i++)
365 for (j = 0; j < REF_TYPES; j++)
366 for (k = 0; k < COEF_BANDS; k++)
367 for (l = 0; l < PREV_COEF_CONTEXTS; l++)
369 for (m = 0; m < UNCONSTRAINED_NODES; m++)
370 if (vp9_read(r, VP9_COEF_UPDATE_PROB))
371 vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
374 static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode,
376 read_coef_probs_common(fc->coef_probs[TX_4X4], r);
378 if (tx_mode > ONLY_4X4)
379 read_coef_probs_common(fc->coef_probs[TX_8X8], r);
381 if (tx_mode > ALLOW_8X8)
382 read_coef_probs_common(fc->coef_probs[TX_16X16], r);
384 if (tx_mode > ALLOW_16X16)
385 read_coef_probs_common(fc->coef_probs[TX_32X32], r);
388 static void setup_segmentation(struct segmentation *seg,
389 struct vp9_read_bit_buffer *rb) {
393 seg->update_data = 0;
395 seg->enabled = vp9_rb_read_bit(rb);
399 // Segmentation map update
400 seg->update_map = vp9_rb_read_bit(rb);
401 if (seg->update_map) {
402 for (i = 0; i < SEG_TREE_PROBS; i++)
403 seg->tree_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
406 seg->temporal_update = vp9_rb_read_bit(rb);
407 if (seg->temporal_update) {
408 for (i = 0; i < PREDICTION_PROBS; i++)
409 seg->pred_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8)
412 for (i = 0; i < PREDICTION_PROBS; i++)
413 seg->pred_probs[i] = MAX_PROB;
417 // Segmentation data update
418 seg->update_data = vp9_rb_read_bit(rb);
419 if (seg->update_data) {
420 seg->abs_delta = vp9_rb_read_bit(rb);
422 vp9_clearall_segfeatures(seg);
424 for (i = 0; i < MAX_SEGMENTS; i++) {
425 for (j = 0; j < SEG_LVL_MAX; j++) {
427 const int feature_enabled = vp9_rb_read_bit(rb);
428 if (feature_enabled) {
429 vp9_enable_segfeature(seg, i, j);
430 data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
431 if (vp9_is_segfeature_signed(j))
432 data = vp9_rb_read_bit(rb) ? -data : data;
434 vp9_set_segdata(seg, i, j, data);
440 static void setup_loopfilter(struct loopfilter *lf,
441 struct vp9_read_bit_buffer *rb) {
443 lf->filter_level = vp9_rb_read_literal(rb, 6);
444 lf->sharpness_level = vp9_rb_read_literal(rb, 3);
446 // Read in loop filter deltas applied at the MB level based on mode or ref
448 lf->mode_ref_delta_update = 0;
450 lf->mode_ref_delta_enabled = vp9_rb_read_bit(rb);
451 if (lf->mode_ref_delta_enabled) {
452 lf->mode_ref_delta_update = vp9_rb_read_bit(rb);
453 if (lf->mode_ref_delta_update) {
456 for (i = 0; i < MAX_REF_LF_DELTAS; i++)
457 if (vp9_rb_read_bit(rb))
458 lf->ref_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
460 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
461 if (vp9_rb_read_bit(rb))
462 lf->mode_deltas[i] = vp9_rb_read_signed_literal(rb, 6);
467 static int read_delta_q(struct vp9_read_bit_buffer *rb, int *delta_q) {
468 const int old = *delta_q;
469 if (vp9_rb_read_bit(rb))
470 *delta_q = vp9_rb_read_signed_literal(rb, 4);
471 return old != *delta_q;
474 static void setup_quantization(VP9D_COMP *pbi, struct vp9_read_bit_buffer *rb) {
475 MACROBLOCKD *const xd = &pbi->mb;
476 VP9_COMMON *const cm = &pbi->common;
479 cm->base_qindex = vp9_rb_read_literal(rb, QINDEX_BITS);
480 update |= read_delta_q(rb, &cm->y_dc_delta_q);
481 update |= read_delta_q(rb, &cm->uv_dc_delta_q);
482 update |= read_delta_q(rb, &cm->uv_ac_delta_q);
484 vp9_init_dequantizer(cm);
486 xd->lossless = cm->base_qindex == 0 &&
487 cm->y_dc_delta_q == 0 &&
488 cm->uv_dc_delta_q == 0 &&
489 cm->uv_ac_delta_q == 0;
491 xd->itxm_add = xd->lossless ? vp9_idct_add_lossless_c
495 static INTERPOLATIONFILTERTYPE read_interp_filter_type(
496 struct vp9_read_bit_buffer *rb) {
497 const INTERPOLATIONFILTERTYPE literal_to_type[] = { EIGHTTAP_SMOOTH,
500 return vp9_rb_read_bit(rb) ? SWITCHABLE
501 : literal_to_type[vp9_rb_read_literal(rb, 2)];
504 static void read_frame_size(struct vp9_read_bit_buffer *rb,
505 int *width, int *height) {
506 const int w = vp9_rb_read_literal(rb, 16) + 1;
507 const int h = vp9_rb_read_literal(rb, 16) + 1;
512 static void setup_display_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
513 cm->display_width = cm->width;
514 cm->display_height = cm->height;
515 if (vp9_rb_read_bit(rb))
516 read_frame_size(rb, &cm->display_width, &cm->display_height);
519 static void apply_frame_size(VP9D_COMP *pbi, int width, int height) {
520 VP9_COMMON *cm = &pbi->common;
522 if (cm->width != width || cm->height != height) {
523 if (!pbi->initial_width || !pbi->initial_height) {
524 if (vp9_alloc_frame_buffers(cm, width, height))
525 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
526 "Failed to allocate frame buffers");
527 pbi->initial_width = width;
528 pbi->initial_height = height;
530 if (width > pbi->initial_width)
531 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
532 "Frame width too large");
534 if (height > pbi->initial_height)
535 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
536 "Frame height too large");
542 vp9_update_frame_size(cm);
545 vp9_realloc_frame_buffer(&cm->yv12_fb[cm->new_fb_idx], cm->width, cm->height,
546 cm->subsampling_x, cm->subsampling_y,
550 static void setup_frame_size(VP9D_COMP *pbi,
551 struct vp9_read_bit_buffer *rb) {
553 read_frame_size(rb, &width, &height);
554 setup_display_size(&pbi->common, rb);
555 apply_frame_size(pbi, width, height);
558 static void setup_frame_size_with_refs(VP9D_COMP *pbi,
559 struct vp9_read_bit_buffer *rb) {
560 VP9_COMMON *const cm = &pbi->common;
564 for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) {
565 if (vp9_rb_read_bit(rb)) {
566 YV12_BUFFER_CONFIG *cfg = &cm->yv12_fb[cm->active_ref_idx[i]];
567 width = cfg->y_crop_width;
568 height = cfg->y_crop_height;
575 read_frame_size(rb, &width, &height);
577 if (!width || !height)
578 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
579 "Referenced frame with invalid size");
581 setup_display_size(cm, rb);
582 apply_frame_size(pbi, width, height);
585 static void decode_tile(VP9D_COMP *pbi, vp9_reader *r) {
586 const int num_threads = pbi->oxcf.max_threads;
587 VP9_COMMON *const pc = &pbi->common;
589 YV12_BUFFER_CONFIG *const fb = &pc->yv12_fb[pc->new_fb_idx];
591 if (pbi->do_loopfilter_inline) {
592 if (num_threads > 1) {
593 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
594 lf_data->frame_buffer = fb;
596 lf_data->xd = pbi->mb;
599 vp9_loop_filter_frame_init(pc, &pbi->mb, pc->lf.filter_level);
602 for (mi_row = pc->cur_tile_mi_row_start; mi_row < pc->cur_tile_mi_row_end;
603 mi_row += MI_BLOCK_SIZE) {
604 // For a SB there are 2 left contexts, each pertaining to a MB row within
605 vp9_zero(pc->left_context);
606 vp9_zero(pc->left_seg_context);
607 for (mi_col = pc->cur_tile_mi_col_start; mi_col < pc->cur_tile_mi_col_end;
608 mi_col += MI_BLOCK_SIZE) {
609 decode_modes_sb(pbi, mi_row, mi_col, r, BLOCK_64X64);
612 if (pbi->do_loopfilter_inline) {
613 // delay the loopfilter by 1 macroblock row.
614 const int lf_start = mi_row - MI_BLOCK_SIZE;
615 if (lf_start < 0) continue;
617 if (num_threads > 1) {
618 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
620 vp9_worker_sync(&pbi->lf_worker);
621 lf_data->start = lf_start;
622 lf_data->stop = mi_row;
623 pbi->lf_worker.hook = vp9_loop_filter_worker;
624 vp9_worker_launch(&pbi->lf_worker);
626 vp9_loop_filter_rows(fb, pc, &pbi->mb, lf_start, mi_row, 0);
631 if (pbi->do_loopfilter_inline) {
632 if (num_threads > 1) {
633 // TODO(jzern): since the loop filter is delayed one mb row, this will be
634 // forced to wait for the last row scheduled in the for loop.
635 vp9_worker_sync(&pbi->lf_worker);
637 vp9_loop_filter_rows(fb, pc, &pbi->mb,
638 mi_row - MI_BLOCK_SIZE, pc->mi_rows, 0);
642 static void setup_tile_info(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
643 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
644 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
647 max_ones = max_log2_tile_cols - min_log2_tile_cols;
648 cm->log2_tile_cols = min_log2_tile_cols;
649 while (max_ones-- && vp9_rb_read_bit(rb))
650 cm->log2_tile_cols++;
653 cm->log2_tile_rows = vp9_rb_read_bit(rb);
654 if (cm->log2_tile_rows)
655 cm->log2_tile_rows += vp9_rb_read_bit(rb);
658 static const uint8_t *decode_tiles(VP9D_COMP *pbi, const uint8_t *data) {
659 vp9_reader residual_bc;
661 VP9_COMMON *const pc = &pbi->common;
663 const uint8_t *const data_end = pbi->source + pbi->source_sz;
664 const int aligned_mi_cols = mi_cols_aligned_to_sb(pc->mi_cols);
665 const int tile_cols = 1 << pc->log2_tile_cols;
666 const int tile_rows = 1 << pc->log2_tile_rows;
667 int tile_row, tile_col;
669 // Note: this memset assumes above_context[0], [1] and [2]
670 // are allocated as part of the same buffer.
671 vpx_memset(pc->above_context[0], 0,
672 sizeof(ENTROPY_CONTEXT) * MAX_MB_PLANE * (2 * aligned_mi_cols));
674 vpx_memset(pc->above_seg_context, 0,
675 sizeof(PARTITION_CONTEXT) * aligned_mi_cols);
677 if (pbi->oxcf.inv_tile_order) {
678 const uint8_t *data_ptr2[4][1 << 6];
679 vp9_reader bc_bak = {0};
681 // pre-initialize the offsets, we're going to read in inverse order
682 data_ptr2[0][0] = data;
683 for (tile_row = 0; tile_row < tile_rows; tile_row++) {
685 const int size = read_be32(data_ptr2[tile_row - 1][tile_cols - 1]);
686 data_ptr2[tile_row - 1][tile_cols - 1] += 4;
687 data_ptr2[tile_row][0] = data_ptr2[tile_row - 1][tile_cols - 1] + size;
690 for (tile_col = 1; tile_col < tile_cols; tile_col++) {
691 const int size = read_be32(data_ptr2[tile_row][tile_col - 1]);
692 data_ptr2[tile_row][tile_col - 1] += 4;
693 data_ptr2[tile_row][tile_col] =
694 data_ptr2[tile_row][tile_col - 1] + size;
698 for (tile_row = 0; tile_row < tile_rows; tile_row++) {
699 vp9_get_tile_row_offsets(pc, tile_row);
700 for (tile_col = tile_cols - 1; tile_col >= 0; tile_col--) {
701 vp9_get_tile_col_offsets(pc, tile_col);
702 setup_token_decoder(pbi, data_ptr2[tile_row][tile_col],
703 data_end - data_ptr2[tile_row][tile_col],
705 decode_tile(pbi, &residual_bc);
706 if (tile_row == tile_rows - 1 && tile_col == tile_cols - 1)
707 bc_bak = residual_bc;
710 residual_bc = bc_bak;
714 for (tile_row = 0; tile_row < tile_rows; tile_row++) {
715 vp9_get_tile_row_offsets(pc, tile_row);
716 for (tile_col = 0; tile_col < tile_cols; tile_col++) {
719 vp9_get_tile_col_offsets(pc, tile_col);
721 has_more = tile_col < tile_cols - 1 || tile_row < tile_rows - 1;
723 if (!read_is_valid(data, 4, data_end))
724 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
725 "Truncated packet or corrupt tile length");
727 size = read_be32(data);
730 size = data_end - data;
733 setup_token_decoder(pbi, data, size, &residual_bc);
734 decode_tile(pbi, &residual_bc);
740 return vp9_reader_find_end(&residual_bc);
743 static void check_sync_code(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) {
744 if (vp9_rb_read_literal(rb, 8) != SYNC_CODE_0 ||
745 vp9_rb_read_literal(rb, 8) != SYNC_CODE_1 ||
746 vp9_rb_read_literal(rb, 8) != SYNC_CODE_2) {
747 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
748 "Invalid frame sync code");
752 static void error_handler(void *data, size_t bit_offset) {
753 VP9_COMMON *const cm = (VP9_COMMON *)data;
754 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet");
757 static void setup_inter_inter(VP9_COMMON *cm) {
760 cm->allow_comp_inter_inter = 0;
761 for (i = 1; i < ALLOWED_REFS_PER_FRAME; ++i)
762 cm->allow_comp_inter_inter |=
763 cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1];
765 if (cm->allow_comp_inter_inter) {
766 // which one is always-on in comp inter-inter?
767 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
768 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
769 cm->comp_fixed_ref = ALTREF_FRAME;
770 cm->comp_var_ref[0] = LAST_FRAME;
771 cm->comp_var_ref[1] = GOLDEN_FRAME;
772 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
773 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
774 cm->comp_fixed_ref = GOLDEN_FRAME;
775 cm->comp_var_ref[0] = LAST_FRAME;
776 cm->comp_var_ref[1] = ALTREF_FRAME;
778 cm->comp_fixed_ref = LAST_FRAME;
779 cm->comp_var_ref[0] = GOLDEN_FRAME;
780 cm->comp_var_ref[1] = ALTREF_FRAME;
786 if (vp9_rb_read_bit(rb)) \
787 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, \
788 "Reserved bit must be unset")
790 static size_t read_uncompressed_header(VP9D_COMP *pbi,
791 struct vp9_read_bit_buffer *rb) {
792 VP9_COMMON *const cm = &pbi->common;
793 MACROBLOCKD *const xd = &pbi->mb;
796 cm->last_frame_type = cm->frame_type;
798 if (vp9_rb_read_literal(rb, 2) != 0x2)
799 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
800 "Invalid frame marker");
802 cm->version = vp9_rb_read_bit(rb);
805 if (vp9_rb_read_bit(rb)) {
806 // show an existing frame directly
807 int frame_to_show = cm->ref_frame_map[vp9_rb_read_literal(rb, 3)];
808 ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->new_fb_idx, frame_to_show);
809 pbi->refresh_frame_flags = 0;
810 cm->lf.filter_level = 0;
814 cm->frame_type = (FRAME_TYPE) vp9_rb_read_bit(rb);
815 cm->show_frame = vp9_rb_read_bit(rb);
816 cm->error_resilient_mode = vp9_rb_read_bit(rb);
818 if (cm->frame_type == KEY_FRAME) {
821 check_sync_code(cm, rb);
823 csp = vp9_rb_read_literal(rb, 3); // colorspace
824 if (csp != 7) { // != sRGB
825 vp9_rb_read_bit(rb); // [16,235] (including xvycc) vs [0,255] range
826 if (cm->version == 1) {
827 cm->subsampling_x = vp9_rb_read_bit(rb);
828 cm->subsampling_y = vp9_rb_read_bit(rb);
829 vp9_rb_read_bit(rb); // has extra plane
831 cm->subsampling_y = cm->subsampling_x = 1;
834 if (cm->version == 1) {
835 cm->subsampling_y = cm->subsampling_x = 0;
836 vp9_rb_read_bit(rb); // has extra plane
838 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
839 "RGB not supported in profile 0");
843 pbi->refresh_frame_flags = (1 << NUM_REF_FRAMES) - 1;
845 for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i)
846 cm->active_ref_idx[i] = cm->new_fb_idx;
848 setup_frame_size(pbi, rb);
850 cm->intra_only = cm->show_frame ? 0 : vp9_rb_read_bit(rb);
852 cm->reset_frame_context = cm->error_resilient_mode ?
853 0 : vp9_rb_read_literal(rb, 2);
855 if (cm->intra_only) {
856 check_sync_code(cm, rb);
858 pbi->refresh_frame_flags = vp9_rb_read_literal(rb, NUM_REF_FRAMES);
859 setup_frame_size(pbi, rb);
861 pbi->refresh_frame_flags = vp9_rb_read_literal(rb, NUM_REF_FRAMES);
863 for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) {
864 const int ref = vp9_rb_read_literal(rb, NUM_REF_FRAMES_LOG2);
865 cm->active_ref_idx[i] = cm->ref_frame_map[ref];
866 cm->ref_frame_sign_bias[LAST_FRAME + i] = vp9_rb_read_bit(rb);
869 setup_frame_size_with_refs(pbi, rb);
871 xd->allow_high_precision_mv = vp9_rb_read_bit(rb);
872 cm->mcomp_filter_type = read_interp_filter_type(rb);
874 for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i)
875 vp9_setup_scale_factors(cm, i);
877 setup_inter_inter(cm);
881 if (!cm->error_resilient_mode) {
882 cm->refresh_frame_context = vp9_rb_read_bit(rb);
883 cm->frame_parallel_decoding_mode = vp9_rb_read_bit(rb);
885 cm->refresh_frame_context = 0;
886 cm->frame_parallel_decoding_mode = 1;
889 cm->frame_context_idx = vp9_rb_read_literal(rb, NUM_FRAME_CONTEXTS_LOG2);
891 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode || cm->intra_only)
892 vp9_setup_past_independence(cm, xd);
894 setup_loopfilter(&cm->lf, rb);
895 setup_quantization(pbi, rb);
896 setup_segmentation(&xd->seg, rb);
898 setup_tile_info(cm, rb);
900 return vp9_rb_read_literal(rb, 16);
903 static int read_compressed_header(VP9D_COMP *pbi, const uint8_t *data,
904 size_t partition_size) {
905 VP9_COMMON *const cm = &pbi->common;
906 MACROBLOCKD *const xd = &pbi->mb;
909 if (vp9_reader_init(&r, data, partition_size))
910 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
911 "Failed to allocate bool decoder 0");
913 cm->tx_mode = xd->lossless ? ONLY_4X4 : read_tx_mode(&r);
914 if (cm->tx_mode == TX_MODE_SELECT)
915 read_tx_probs(&cm->fc.tx_probs, &r);
916 read_coef_probs(&cm->fc, cm->tx_mode, &r);
918 vp9_prepare_read_mode_info(pbi, &r);
920 return vp9_reader_has_error(&r);
923 void vp9_init_dequantizer(VP9_COMMON *cm) {
926 for (q = 0; q < QINDEX_RANGE; q++) {
927 cm->y_dequant[q][0] = vp9_dc_quant(q, cm->y_dc_delta_q);
928 cm->y_dequant[q][1] = vp9_ac_quant(q, 0);
930 cm->uv_dequant[q][0] = vp9_dc_quant(q, cm->uv_dc_delta_q);
931 cm->uv_dequant[q][1] = vp9_ac_quant(q, cm->uv_ac_delta_q);
935 int vp9_decode_frame(VP9D_COMP *pbi, const uint8_t **p_data_end) {
937 VP9_COMMON *const pc = &pbi->common;
938 MACROBLOCKD *const xd = &pbi->mb;
940 const uint8_t *data = pbi->source;
941 const uint8_t *data_end = pbi->source + pbi->source_sz;
943 struct vp9_read_bit_buffer rb = { data, data_end, 0,
945 const size_t first_partition_size = read_uncompressed_header(pbi, &rb);
946 const int keyframe = pc->frame_type == KEY_FRAME;
947 YV12_BUFFER_CONFIG *new_fb = &pc->yv12_fb[pc->new_fb_idx];
949 if (!first_partition_size) {
950 // showing a frame directly
951 *p_data_end = data + 1;
954 data += vp9_rb_bytes_read(&rb);
956 new_fb->corrupted = 0;
957 pbi->do_loopfilter_inline =
958 (pc->log2_tile_rows | pc->log2_tile_cols) == 0 && pc->lf.filter_level;
960 if (!pbi->decoded_key_frame && !keyframe)
963 if (!read_is_valid(data, first_partition_size, data_end))
964 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
965 "Truncated packet or corrupt header length");
967 xd->mode_info_context = pc->mi;
968 xd->prev_mode_info_context = pc->prev_mi;
969 xd->mode_info_stride = pc->mode_info_stride;
971 init_dequantizer(pc, &pbi->mb);
974 vp9_setup_interp_filters(xd, pc->mcomp_filter_type, pc);
976 pc->fc = pc->frame_contexts[pc->frame_context_idx];
978 vp9_zero(pc->counts);
980 // Initialize xd pointers. Any reference should do for xd->pre, so use 0.
981 setup_pre_planes(xd, 0, &pc->yv12_fb[pc->active_ref_idx[0]], 0, 0, NULL);
982 setup_dst_planes(xd, new_fb, 0, 0);
984 new_fb->corrupted |= read_compressed_header(pbi, data, first_partition_size);
986 // Create the segmentation map structure and set to 0
987 if (!pc->last_frame_seg_map)
988 CHECK_MEM_ERROR(pc, pc->last_frame_seg_map,
989 vpx_calloc((pc->mi_rows * pc->mi_cols), 1));
991 setup_block_dptrs(xd, pc->subsampling_x, pc->subsampling_y);
993 // clear out the coeff buffer
994 for (i = 0; i < MAX_MB_PLANE; ++i)
995 vp9_zero(xd->plane[i].qcoeff);
999 *p_data_end = decode_tiles(pbi, data + first_partition_size);
1001 pc->last_width = pc->width;
1002 pc->last_height = pc->height;
1004 new_fb->corrupted |= xd->corrupted;
1006 if (!pbi->decoded_key_frame) {
1007 if (keyframe && !new_fb->corrupted)
1008 pbi->decoded_key_frame = 1;
1010 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
1011 "A stream must start with a complete key frame");
1014 if (!pc->error_resilient_mode && !pc->frame_parallel_decoding_mode) {
1015 vp9_adapt_coef_probs(pc);
1017 if (!keyframe && !pc->intra_only) {
1018 vp9_adapt_mode_probs(pc);
1019 vp9_adapt_mv_probs(pc, xd->allow_high_precision_mv);
1023 if (pc->refresh_frame_context)
1024 pc->frame_contexts[pc->frame_context_idx] = pc->fc;