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 "./vp10_rtcd.h"
15 #include "./vpx_dsp_rtcd.h"
16 #include "./vpx_scale_rtcd.h"
18 #include "vpx_dsp/bitreader_buffer.h"
19 #include "vpx_dsp/bitreader.h"
20 #include "vpx_dsp/vpx_dsp_common.h"
21 #include "vpx_mem/vpx_mem.h"
22 #include "vpx_ports/mem.h"
23 #include "vpx_ports/mem_ops.h"
24 #include "vpx_scale/vpx_scale.h"
25 #include "vpx_util/vpx_thread.h"
27 #include "vp10/common/alloccommon.h"
28 #include "vp10/common/common.h"
29 #include "vp10/common/entropy.h"
30 #include "vp10/common/entropymode.h"
31 #include "vp10/common/idct.h"
32 #include "vp10/common/thread_common.h"
33 #include "vp10/common/pred_common.h"
34 #include "vp10/common/quant_common.h"
35 #include "vp10/common/reconintra.h"
36 #include "vp10/common/reconinter.h"
37 #include "vp10/common/seg_common.h"
38 #include "vp10/common/tile_common.h"
40 #include "vp10/decoder/decodeframe.h"
41 #include "vp10/decoder/detokenize.h"
42 #include "vp10/decoder/decodemv.h"
43 #include "vp10/decoder/decoder.h"
44 #include "vp10/decoder/dsubexp.h"
46 #define MAX_VP9_HEADER_SIZE 80
48 static int is_compound_reference_allowed(const VP10_COMMON *cm) {
50 for (i = 1; i < REFS_PER_FRAME; ++i)
51 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1])
57 static void setup_compound_reference_mode(VP10_COMMON *cm) {
58 if (cm->ref_frame_sign_bias[LAST_FRAME] ==
59 cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
60 cm->comp_fixed_ref = ALTREF_FRAME;
61 cm->comp_var_ref[0] = LAST_FRAME;
62 cm->comp_var_ref[1] = GOLDEN_FRAME;
63 } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
64 cm->ref_frame_sign_bias[ALTREF_FRAME]) {
65 cm->comp_fixed_ref = GOLDEN_FRAME;
66 cm->comp_var_ref[0] = LAST_FRAME;
67 cm->comp_var_ref[1] = ALTREF_FRAME;
69 cm->comp_fixed_ref = LAST_FRAME;
70 cm->comp_var_ref[0] = GOLDEN_FRAME;
71 cm->comp_var_ref[1] = ALTREF_FRAME;
75 static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
76 return len != 0 && len <= (size_t)(end - start);
79 static int decode_unsigned_max(struct vpx_read_bit_buffer *rb, int max) {
80 const int data = vpx_rb_read_literal(rb, get_unsigned_bits(max));
81 return data > max ? max : data;
84 static TX_MODE read_tx_mode(vpx_reader *r) {
85 TX_MODE tx_mode = vpx_read_literal(r, 2);
86 if (tx_mode == ALLOW_32X32)
87 tx_mode += vpx_read_bit(r);
91 static void read_tx_mode_probs(struct tx_probs *tx_probs, vpx_reader *r) {
94 for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
95 for (j = 0; j < TX_SIZES - 3; ++j)
96 vp10_diff_update_prob(r, &tx_probs->p8x8[i][j]);
98 for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
99 for (j = 0; j < TX_SIZES - 2; ++j)
100 vp10_diff_update_prob(r, &tx_probs->p16x16[i][j]);
102 for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
103 for (j = 0; j < TX_SIZES - 1; ++j)
104 vp10_diff_update_prob(r, &tx_probs->p32x32[i][j]);
107 static void read_switchable_interp_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
109 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
110 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
111 vp10_diff_update_prob(r, &fc->switchable_interp_prob[j][i]);
114 static void read_inter_mode_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
116 for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
117 for (j = 0; j < INTER_MODES - 1; ++j)
118 vp10_diff_update_prob(r, &fc->inter_mode_probs[i][j]);
121 static REFERENCE_MODE read_frame_reference_mode(const VP10_COMMON *cm,
123 if (is_compound_reference_allowed(cm)) {
124 return vpx_read_bit(r) ? (vpx_read_bit(r) ? REFERENCE_MODE_SELECT
125 : COMPOUND_REFERENCE)
128 return SINGLE_REFERENCE;
132 static void read_frame_reference_mode_probs(VP10_COMMON *cm, vpx_reader *r) {
133 FRAME_CONTEXT *const fc = cm->fc;
136 if (cm->reference_mode == REFERENCE_MODE_SELECT)
137 for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
138 vp10_diff_update_prob(r, &fc->comp_inter_prob[i]);
140 if (cm->reference_mode != COMPOUND_REFERENCE)
141 for (i = 0; i < REF_CONTEXTS; ++i) {
142 vp10_diff_update_prob(r, &fc->single_ref_prob[i][0]);
143 vp10_diff_update_prob(r, &fc->single_ref_prob[i][1]);
146 if (cm->reference_mode != SINGLE_REFERENCE)
147 for (i = 0; i < REF_CONTEXTS; ++i)
148 vp10_diff_update_prob(r, &fc->comp_ref_prob[i]);
151 static void update_mv_probs(vpx_prob *p, int n, vpx_reader *r) {
153 for (i = 0; i < n; ++i)
154 if (vpx_read(r, MV_UPDATE_PROB))
155 p[i] = (vpx_read_literal(r, 7) << 1) | 1;
158 static void read_mv_probs(nmv_context *ctx, int allow_hp, vpx_reader *r) {
161 update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
163 for (i = 0; i < 2; ++i) {
164 nmv_component *const comp_ctx = &ctx->comps[i];
165 update_mv_probs(&comp_ctx->sign, 1, r);
166 update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
167 update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
168 update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
171 for (i = 0; i < 2; ++i) {
172 nmv_component *const comp_ctx = &ctx->comps[i];
173 for (j = 0; j < CLASS0_SIZE; ++j)
174 update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
175 update_mv_probs(comp_ctx->fp, 3, r);
179 for (i = 0; i < 2; ++i) {
180 nmv_component *const comp_ctx = &ctx->comps[i];
181 update_mv_probs(&comp_ctx->class0_hp, 1, r);
182 update_mv_probs(&comp_ctx->hp, 1, r);
187 static void inverse_transform_block_inter(MACROBLOCKD* xd, int plane,
188 const TX_SIZE tx_size,
189 uint8_t *dst, int stride,
190 int eob, int block) {
191 struct macroblockd_plane *const pd = &xd->plane[plane];
192 TX_TYPE tx_type = get_tx_type(pd->plane_type, xd, block);
194 tran_low_t *const dqcoeff = pd->dqcoeff;
195 #if CONFIG_VP9_HIGHBITDEPTH
196 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
199 vp10_highbd_inv_txfm_add_4x4(dqcoeff, dst, stride, eob, xd->bd,
200 tx_type, xd->lossless ?
201 vp10_highbd_iwht4x4_add :
202 vp10_highbd_idct4x4_add);
205 vp10_highbd_inv_txfm_add_8x8(dqcoeff, dst, stride, eob, xd->bd,
209 vp10_highbd_inv_txfm_add_16x16(dqcoeff, dst, stride, eob, xd->bd,
213 vp10_highbd_inv_txfm_add_32x32(dqcoeff, dst, stride, eob, xd->bd,
217 assert(0 && "Invalid transform size");
221 #endif // CONFIG_VP9_HIGHBITDEPTH
224 vp10_inv_txfm_add_4x4(dqcoeff, dst, stride, eob, tx_type,
225 xd->lossless ? vp10_iwht4x4_add :
229 vp10_inv_txfm_add_8x8(dqcoeff, dst, stride, eob, tx_type);
232 vp10_inv_txfm_add_16x16(dqcoeff, dst, stride, eob, tx_type);
235 vp10_inv_txfm_add_32x32(dqcoeff, dst, stride, eob, tx_type);
238 assert(0 && "Invalid transform size");
241 #if CONFIG_VP9_HIGHBITDEPTH
243 #endif // CONFIG_VP9_HIGHBITDEPTH
248 if (tx_size <= TX_16X16 && eob <= 10)
249 memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
250 else if (tx_size == TX_32X32 && eob <= 34)
251 memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
253 memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
258 static void inverse_transform_block_intra(MACROBLOCKD* xd, int plane,
259 const TX_TYPE tx_type,
260 const TX_SIZE tx_size,
261 uint8_t *dst, int stride,
263 struct macroblockd_plane *const pd = &xd->plane[plane];
265 tran_low_t *const dqcoeff = pd->dqcoeff;
266 #if CONFIG_VP9_HIGHBITDEPTH
267 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
270 vp10_highbd_inv_txfm_add_4x4(dqcoeff, dst, stride, eob, xd->bd,
271 tx_type, xd->lossless ?
272 vp10_highbd_iwht4x4_add :
273 vp10_highbd_idct4x4_add);
276 vp10_highbd_inv_txfm_add_8x8(dqcoeff, dst, stride, eob, xd->bd,
280 vp10_highbd_inv_txfm_add_16x16(dqcoeff, dst, stride, eob, xd->bd,
284 vp10_highbd_inv_txfm_add_32x32(dqcoeff, dst, stride, eob, xd->bd,
288 assert(0 && "Invalid transform size");
292 #endif // CONFIG_VP9_HIGHBITDEPTH
295 vp10_inv_txfm_add_4x4(dqcoeff, dst, stride, eob, tx_type,
296 xd->lossless ? vp10_iwht4x4_add :
300 vp10_inv_txfm_add_8x8(dqcoeff, dst, stride, eob, tx_type);
303 vp10_inv_txfm_add_16x16(dqcoeff, dst, stride, eob, tx_type);
306 vp10_inv_txfm_add_32x32(dqcoeff, dst, stride, eob, tx_type);
309 assert(0 && "Invalid transform size");
312 #if CONFIG_VP9_HIGHBITDEPTH
314 #endif // CONFIG_VP9_HIGHBITDEPTH
319 if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
320 memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
321 else if (tx_size == TX_32X32 && eob <= 34)
322 memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
324 memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
329 static void predict_and_reconstruct_intra_block(MACROBLOCKD *const xd,
331 MB_MODE_INFO *const mbmi,
335 struct macroblockd_plane *const pd = &xd->plane[plane];
336 PREDICTION_MODE mode = (plane == 0) ? mbmi->mode : mbmi->uv_mode;
337 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
339 int block_idx = (row << 1) + col;
340 dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
342 if (mbmi->sb_type < BLOCK_8X8)
344 mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
346 vp10_predict_intra_block(xd, pd->n4_wl, tx_size, mode,
347 dst, pd->dst.stride, dst, pd->dst.stride,
351 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx);
352 const scan_order *sc = get_scan(tx_size, tx_type);
353 const int eob = vp10_decode_block_tokens(xd, plane, sc, col, row, tx_size,
354 r, mbmi->segment_id);
355 inverse_transform_block_intra(xd, plane, tx_type, tx_size,
356 dst, pd->dst.stride, eob);
360 static int reconstruct_inter_block(MACROBLOCKD *const xd, vpx_reader *r,
361 MB_MODE_INFO *const mbmi, int plane,
362 int row, int col, TX_SIZE tx_size) {
363 struct macroblockd_plane *const pd = &xd->plane[plane];
364 PLANE_TYPE plane_type = (plane == 0) ? PLANE_TYPE_Y : PLANE_TYPE_UV;
365 int block_idx = (row << 1) + col;
366 TX_TYPE tx_type = get_tx_type(plane_type, xd, block_idx);
367 const scan_order *sc = get_scan(tx_size, tx_type);
368 const int eob = vp10_decode_block_tokens(xd, plane, sc, col, row, tx_size, r,
371 inverse_transform_block_inter(xd, plane, tx_size,
372 &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
373 pd->dst.stride, eob, block_idx);
377 static void build_mc_border(const uint8_t *src, int src_stride,
378 uint8_t *dst, int dst_stride,
379 int x, int y, int b_w, int b_h, int w, int h) {
380 // Get a pointer to the start of the real data for this row.
381 const uint8_t *ref_row = src - x - y * src_stride;
384 ref_row += (h - 1) * src_stride;
386 ref_row += y * src_stride;
390 int left = x < 0 ? -x : 0;
401 copy = b_w - left - right;
404 memset(dst, ref_row[0], left);
407 memcpy(dst + left, ref_row + x + left, copy);
410 memset(dst + left + copy, ref_row[w - 1], right);
416 ref_row += src_stride;
420 #if CONFIG_VP9_HIGHBITDEPTH
421 static void high_build_mc_border(const uint8_t *src8, int src_stride,
422 uint16_t *dst, int dst_stride,
423 int x, int y, int b_w, int b_h,
425 // Get a pointer to the start of the real data for this row.
426 const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
427 const uint16_t *ref_row = src - x - y * src_stride;
430 ref_row += (h - 1) * src_stride;
432 ref_row += y * src_stride;
436 int left = x < 0 ? -x : 0;
447 copy = b_w - left - right;
450 vpx_memset16(dst, ref_row[0], left);
453 memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
456 vpx_memset16(dst + left + copy, ref_row[w - 1], right);
462 ref_row += src_stride;
465 #endif // CONFIG_VP9_HIGHBITDEPTH
467 #if CONFIG_VP9_HIGHBITDEPTH
468 static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
469 int x0, int y0, int b_w, int b_h,
470 int frame_width, int frame_height,
472 uint8_t *const dst, int dst_buf_stride,
473 int subpel_x, int subpel_y,
474 const InterpKernel *kernel,
475 const struct scale_factors *sf,
477 int w, int h, int ref, int xs, int ys) {
478 DECLARE_ALIGNED(16, uint16_t, mc_buf_high[80 * 2 * 80 * 2]);
479 const uint8_t *buf_ptr;
481 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
482 high_build_mc_border(buf_ptr1, pre_buf_stride, mc_buf_high, b_w,
483 x0, y0, b_w, b_h, frame_width, frame_height);
484 buf_ptr = CONVERT_TO_BYTEPTR(mc_buf_high) + border_offset;
486 build_mc_border(buf_ptr1, pre_buf_stride, (uint8_t *)mc_buf_high, b_w,
487 x0, y0, b_w, b_h, frame_width, frame_height);
488 buf_ptr = ((uint8_t *)mc_buf_high) + border_offset;
491 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
492 high_inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x,
493 subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
495 inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x,
496 subpel_y, sf, w, h, ref, kernel, xs, ys);
500 static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
501 int x0, int y0, int b_w, int b_h,
502 int frame_width, int frame_height,
504 uint8_t *const dst, int dst_buf_stride,
505 int subpel_x, int subpel_y,
506 const InterpKernel *kernel,
507 const struct scale_factors *sf,
508 int w, int h, int ref, int xs, int ys) {
509 DECLARE_ALIGNED(16, uint8_t, mc_buf[80 * 2 * 80 * 2]);
510 const uint8_t *buf_ptr;
512 build_mc_border(buf_ptr1, pre_buf_stride, mc_buf, b_w,
513 x0, y0, b_w, b_h, frame_width, frame_height);
514 buf_ptr = mc_buf + border_offset;
516 inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x,
517 subpel_y, sf, w, h, ref, kernel, xs, ys);
519 #endif // CONFIG_VP9_HIGHBITDEPTH
521 static void dec_build_inter_predictors(VP10Decoder *const pbi, MACROBLOCKD *xd,
522 int plane, int bw, int bh, int x,
523 int y, int w, int h, int mi_x, int mi_y,
524 const InterpKernel *kernel,
525 const struct scale_factors *sf,
526 struct buf_2d *pre_buf,
527 struct buf_2d *dst_buf, const MV* mv,
528 RefCntBuffer *ref_frame_buf,
529 int is_scaled, int ref) {
530 VP10_COMMON *const cm = &pbi->common;
531 struct macroblockd_plane *const pd = &xd->plane[plane];
532 uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
534 int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height,
535 buf_stride, subpel_x, subpel_y;
536 uint8_t *ref_frame, *buf_ptr;
538 // Get reference frame pointer, width and height.
540 frame_width = ref_frame_buf->buf.y_crop_width;
541 frame_height = ref_frame_buf->buf.y_crop_height;
542 ref_frame = ref_frame_buf->buf.y_buffer;
544 frame_width = ref_frame_buf->buf.uv_crop_width;
545 frame_height = ref_frame_buf->buf.uv_crop_height;
546 ref_frame = plane == 1 ? ref_frame_buf->buf.u_buffer
547 : ref_frame_buf->buf.v_buffer;
551 const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, mv, bw, bh,
554 // Co-ordinate of containing block to pixel precision.
555 int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
556 int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
558 // Co-ordinate of the block to 1/16th pixel precision.
559 x0_16 = (x_start + x) << SUBPEL_BITS;
560 y0_16 = (y_start + y) << SUBPEL_BITS;
562 // Co-ordinate of current block in reference frame
563 // to 1/16th pixel precision.
564 x0_16 = sf->scale_value_x(x0_16, sf);
565 y0_16 = sf->scale_value_y(y0_16, sf);
567 // Map the top left corner of the block into the reference frame.
568 x0 = sf->scale_value_x(x_start + x, sf);
569 y0 = sf->scale_value_y(y_start + y, sf);
571 // Scale the MV and incorporate the sub-pixel offset of the block
572 // in the reference frame.
573 scaled_mv = vp10_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
577 // Co-ordinate of containing block to pixel precision.
578 x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
579 y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
581 // Co-ordinate of the block to 1/16th pixel precision.
582 x0_16 = x0 << SUBPEL_BITS;
583 y0_16 = y0 << SUBPEL_BITS;
585 scaled_mv.row = mv->row * (1 << (1 - pd->subsampling_y));
586 scaled_mv.col = mv->col * (1 << (1 - pd->subsampling_x));
589 subpel_x = scaled_mv.col & SUBPEL_MASK;
590 subpel_y = scaled_mv.row & SUBPEL_MASK;
592 // Calculate the top left corner of the best matching block in the
594 x0 += scaled_mv.col >> SUBPEL_BITS;
595 y0 += scaled_mv.row >> SUBPEL_BITS;
596 x0_16 += scaled_mv.col;
597 y0_16 += scaled_mv.row;
599 // Get reference block pointer.
600 buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
601 buf_stride = pre_buf->stride;
603 // Do border extension if there is motion or the
604 // width/height is not a multiple of 8 pixels.
605 if (is_scaled || scaled_mv.col || scaled_mv.row ||
606 (frame_width & 0x7) || (frame_height & 0x7)) {
607 int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
609 // Get reference block bottom right horizontal coordinate.
610 int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
611 int x_pad = 0, y_pad = 0;
613 if (subpel_x || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
614 x0 -= VP9_INTERP_EXTEND - 1;
615 x1 += VP9_INTERP_EXTEND;
619 if (subpel_y || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
620 y0 -= VP9_INTERP_EXTEND - 1;
621 y1 += VP9_INTERP_EXTEND;
625 // Wait until reference block is ready. Pad 7 more pixels as last 7
626 // pixels of each superblock row can be changed by next superblock row.
627 if (cm->frame_parallel_decode)
628 vp10_frameworker_wait(pbi->frame_worker_owner, ref_frame_buf,
629 VPXMAX(0, (y1 + 7)) << (plane == 0 ? 0 : 1));
631 // Skip border extension if block is inside the frame.
632 if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 ||
633 y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
634 // Extend the border.
635 const uint8_t *const buf_ptr1 = ref_frame + y0 * buf_stride + x0;
636 const int b_w = x1 - x0 + 1;
637 const int b_h = y1 - y0 + 1;
638 const int border_offset = y_pad * 3 * b_w + x_pad * 3;
640 extend_and_predict(buf_ptr1, buf_stride, x0, y0, b_w, b_h,
641 frame_width, frame_height, border_offset,
642 dst, dst_buf->stride,
645 #if CONFIG_VP9_HIGHBITDEPTH
652 // Wait until reference block is ready. Pad 7 more pixels as last 7
653 // pixels of each superblock row can be changed by next superblock row.
654 if (cm->frame_parallel_decode) {
655 const int y1 = (y0_16 + (h - 1) * ys) >> SUBPEL_BITS;
656 vp10_frameworker_wait(pbi->frame_worker_owner, ref_frame_buf,
657 VPXMAX(0, (y1 + 7)) << (plane == 0 ? 0 : 1));
660 #if CONFIG_VP9_HIGHBITDEPTH
661 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
662 high_inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
663 subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
665 inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
666 subpel_y, sf, w, h, ref, kernel, xs, ys);
669 inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
670 subpel_y, sf, w, h, ref, kernel, xs, ys);
671 #endif // CONFIG_VP9_HIGHBITDEPTH
674 static void dec_build_inter_predictors_sb(VP10Decoder *const pbi,
676 int mi_row, int mi_col) {
678 const int mi_x = mi_col * MI_SIZE;
679 const int mi_y = mi_row * MI_SIZE;
680 const MODE_INFO *mi = xd->mi[0];
681 const InterpKernel *kernel = vp10_filter_kernels[mi->mbmi.interp_filter];
682 const BLOCK_SIZE sb_type = mi->mbmi.sb_type;
683 const int is_compound = has_second_ref(&mi->mbmi);
685 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
686 struct macroblockd_plane *const pd = &xd->plane[plane];
687 struct buf_2d *const dst_buf = &pd->dst;
688 const int num_4x4_w = pd->n4_w;
689 const int num_4x4_h = pd->n4_h;
691 const int n4w_x4 = 4 * num_4x4_w;
692 const int n4h_x4 = 4 * num_4x4_h;
695 for (ref = 0; ref < 1 + is_compound; ++ref) {
696 const struct scale_factors *const sf = &xd->block_refs[ref]->sf;
697 struct buf_2d *const pre_buf = &pd->pre[ref];
698 const int idx = xd->block_refs[ref]->idx;
699 BufferPool *const pool = pbi->common.buffer_pool;
700 RefCntBuffer *const ref_frame_buf = &pool->frame_bufs[idx];
701 const int is_scaled = vp10_is_scaled(sf);
703 if (sb_type < BLOCK_8X8) {
705 for (y = 0; y < num_4x4_h; ++y) {
706 for (x = 0; x < num_4x4_w; ++x) {
707 const MV mv = average_split_mvs(pd, mi, ref, i++);
708 dec_build_inter_predictors(pbi, xd, plane, n4w_x4, n4h_x4,
709 4 * x, 4 * y, 4, 4, mi_x, mi_y, kernel,
710 sf, pre_buf, dst_buf, &mv,
711 ref_frame_buf, is_scaled, ref);
715 const MV mv = mi->mbmi.mv[ref].as_mv;
716 dec_build_inter_predictors(pbi, xd, plane, n4w_x4, n4h_x4,
717 0, 0, n4w_x4, n4h_x4, mi_x, mi_y, kernel,
718 sf, pre_buf, dst_buf, &mv, ref_frame_buf,
725 static INLINE TX_SIZE dec_get_uv_tx_size(const MB_MODE_INFO *mbmi,
726 int n4_wl, int n4_hl) {
727 // get minimum log2 num4x4s dimension
728 const int x = VPXMIN(n4_wl, n4_hl);
729 return VPXMIN(mbmi->tx_size, x);
732 static INLINE void dec_reset_skip_context(MACROBLOCKD *xd) {
734 for (i = 0; i < MAX_MB_PLANE; i++) {
735 struct macroblockd_plane *const pd = &xd->plane[i];
736 memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_w);
737 memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_h);
741 static void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh, int bwl,
744 for (i = 0; i < MAX_MB_PLANE; i++) {
745 xd->plane[i].n4_w = (bw << 1) >> xd->plane[i].subsampling_x;
746 xd->plane[i].n4_h = (bh << 1) >> xd->plane[i].subsampling_y;
747 xd->plane[i].n4_wl = bwl - xd->plane[i].subsampling_x;
748 xd->plane[i].n4_hl = bhl - xd->plane[i].subsampling_y;
752 static MB_MODE_INFO *set_offsets(VP10_COMMON *const cm, MACROBLOCKD *const xd,
753 BLOCK_SIZE bsize, int mi_row, int mi_col,
754 int bw, int bh, int x_mis, int y_mis,
756 const int offset = mi_row * cm->mi_stride + mi_col;
758 const TileInfo *const tile = &xd->tile;
760 xd->mi = cm->mi_grid_visible + offset;
761 xd->mi[0] = &cm->mi[offset];
762 // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
763 // passing bsize from decode_partition().
764 xd->mi[0]->mbmi.sb_type = bsize;
765 for (y = 0; y < y_mis; ++y)
766 for (x = !y; x < x_mis; ++x) {
767 xd->mi[y * cm->mi_stride + x] = xd->mi[0];
770 set_plane_n4(xd, bw, bh, bwl, bhl);
772 set_skip_context(xd, mi_row, mi_col);
774 // Distance of Mb to the various image edges. These are specified to 8th pel
775 // as they are always compared to values that are in 1/8th pel units
776 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
778 vp10_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
779 return &xd->mi[0]->mbmi;
782 static void decode_block(VP10Decoder *const pbi, MACROBLOCKD *const xd,
783 int mi_row, int mi_col,
784 vpx_reader *r, BLOCK_SIZE bsize,
786 VP10_COMMON *const cm = &pbi->common;
787 const int less8x8 = bsize < BLOCK_8X8;
788 const int bw = 1 << (bwl - 1);
789 const int bh = 1 << (bhl - 1);
790 const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
791 const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
793 MB_MODE_INFO *mbmi = set_offsets(cm, xd, bsize, mi_row, mi_col,
794 bw, bh, x_mis, y_mis, bwl, bhl);
796 if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
797 const BLOCK_SIZE uv_subsize =
798 ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
799 if (uv_subsize == BLOCK_INVALID)
800 vpx_internal_error(xd->error_info,
801 VPX_CODEC_CORRUPT_FRAME, "Invalid block size.");
804 vp10_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
807 dec_reset_skip_context(xd);
810 if (!is_inter_block(mbmi)) {
812 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
813 const struct macroblockd_plane *const pd = &xd->plane[plane];
814 const TX_SIZE tx_size =
815 plane ? dec_get_uv_tx_size(mbmi, pd->n4_wl, pd->n4_hl)
817 const int num_4x4_w = pd->n4_w;
818 const int num_4x4_h = pd->n4_h;
819 const int step = (1 << tx_size);
821 const int max_blocks_wide = num_4x4_w + (xd->mb_to_right_edge >= 0 ?
822 0 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
823 const int max_blocks_high = num_4x4_h + (xd->mb_to_bottom_edge >= 0 ?
824 0 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
826 for (row = 0; row < max_blocks_high; row += step)
827 for (col = 0; col < max_blocks_wide; col += step)
828 predict_and_reconstruct_intra_block(xd, r, mbmi, plane,
833 dec_build_inter_predictors_sb(pbi, xd, mi_row, mi_col);
840 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
841 const struct macroblockd_plane *const pd = &xd->plane[plane];
842 const TX_SIZE tx_size =
843 plane ? dec_get_uv_tx_size(mbmi, pd->n4_wl, pd->n4_hl)
845 const int num_4x4_w = pd->n4_w;
846 const int num_4x4_h = pd->n4_h;
847 const int step = (1 << tx_size);
849 const int max_blocks_wide = num_4x4_w + (xd->mb_to_right_edge >= 0 ?
850 0 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
851 const int max_blocks_high = num_4x4_h + (xd->mb_to_bottom_edge >= 0 ?
852 0 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
854 for (row = 0; row < max_blocks_high; row += step)
855 for (col = 0; col < max_blocks_wide; col += step)
856 eobtotal += reconstruct_inter_block(xd, r, mbmi, plane, row, col,
860 if (!less8x8 && eobtotal == 0)
861 mbmi->skip = 1; // skip loopfilter
865 xd->corrupted |= vpx_reader_has_error(r);
868 static INLINE int dec_partition_plane_context(const MACROBLOCKD *xd,
869 int mi_row, int mi_col,
871 const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
872 const PARTITION_CONTEXT *left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
873 int above = (*above_ctx >> bsl) & 1 , left = (*left_ctx >> bsl) & 1;
877 return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
880 static INLINE void dec_update_partition_context(MACROBLOCKD *xd,
881 int mi_row, int mi_col,
884 PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
885 PARTITION_CONTEXT *const left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
887 // update the partition context at the end notes. set partition bits
888 // of block sizes larger than the current one to be one, and partition
889 // bits of smaller block sizes to be zero.
890 memset(above_ctx, partition_context_lookup[subsize].above, bw);
891 memset(left_ctx, partition_context_lookup[subsize].left, bw);
894 static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
896 int has_rows, int has_cols, int bsl) {
897 const int ctx = dec_partition_plane_context(xd, mi_row, mi_col, bsl);
898 const vpx_prob *const probs = get_partition_probs(xd, ctx);
899 FRAME_COUNTS *counts = xd->counts;
902 if (has_rows && has_cols)
903 p = (PARTITION_TYPE)vpx_read_tree(r, vp10_partition_tree, probs);
904 else if (!has_rows && has_cols)
905 p = vpx_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ;
906 else if (has_rows && !has_cols)
907 p = vpx_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT;
912 ++counts->partition[ctx][p];
917 // TODO(slavarnway): eliminate bsize and subsize in future commits
918 static void decode_partition(VP10Decoder *const pbi, MACROBLOCKD *const xd,
919 int mi_row, int mi_col,
920 vpx_reader* r, BLOCK_SIZE bsize, int n4x4_l2) {
921 VP10_COMMON *const cm = &pbi->common;
922 const int n8x8_l2 = n4x4_l2 - 1;
923 const int num_8x8_wh = 1 << n8x8_l2;
924 const int hbs = num_8x8_wh >> 1;
925 PARTITION_TYPE partition;
927 const int has_rows = (mi_row + hbs) < cm->mi_rows;
928 const int has_cols = (mi_col + hbs) < cm->mi_cols;
930 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
933 partition = read_partition(xd, mi_row, mi_col, r, has_rows, has_cols,
935 subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
937 // calculate bmode block dimensions (log 2)
938 xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
939 xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
940 decode_block(pbi, xd, mi_row, mi_col, r, subsize, 1, 1);
944 decode_block(pbi, xd, mi_row, mi_col, r, subsize, n4x4_l2, n4x4_l2);
947 decode_block(pbi, xd, mi_row, mi_col, r, subsize, n4x4_l2, n8x8_l2);
949 decode_block(pbi, xd, mi_row + hbs, mi_col, r, subsize, n4x4_l2,
953 decode_block(pbi, xd, mi_row, mi_col, r, subsize, n8x8_l2, n4x4_l2);
955 decode_block(pbi, xd, mi_row, mi_col + hbs, r, subsize, n8x8_l2,
958 case PARTITION_SPLIT:
959 decode_partition(pbi, xd, mi_row, mi_col, r, subsize, n8x8_l2);
960 decode_partition(pbi, xd, mi_row, mi_col + hbs, r, subsize, n8x8_l2);
961 decode_partition(pbi, xd, mi_row + hbs, mi_col, r, subsize, n8x8_l2);
962 decode_partition(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize,
966 assert(0 && "Invalid partition type");
970 // update partition context
971 if (bsize >= BLOCK_8X8 &&
972 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
973 dec_update_partition_context(xd, mi_row, mi_col, subsize, num_8x8_wh);
976 static void setup_token_decoder(const uint8_t *data,
977 const uint8_t *data_end,
979 struct vpx_internal_error_info *error_info,
981 vpx_decrypt_cb decrypt_cb,
982 void *decrypt_state) {
983 // Validate the calculated partition length. If the buffer
984 // described by the partition can't be fully read, then restrict
985 // it to the portion that can be (for EC mode) or throw an error.
986 if (!read_is_valid(data, read_size, data_end))
987 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
988 "Truncated packet or corrupt tile length");
990 if (vpx_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
991 vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
992 "Failed to allocate bool decoder %d", 1);
995 static void read_coef_probs_common(vp10_coeff_probs_model *coef_probs,
1000 for (i = 0; i < PLANE_TYPES; ++i)
1001 for (j = 0; j < REF_TYPES; ++j)
1002 for (k = 0; k < COEF_BANDS; ++k)
1003 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
1004 for (m = 0; m < UNCONSTRAINED_NODES; ++m)
1005 vp10_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
1008 static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode,
1010 const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
1012 for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
1013 read_coef_probs_common(fc->coef_probs[tx_size], r);
1016 static void setup_segmentation(VP10_COMMON *const cm,
1017 struct vpx_read_bit_buffer *rb) {
1018 struct segmentation *const seg = &cm->seg;
1021 seg->update_map = 0;
1022 seg->update_data = 0;
1024 seg->enabled = vpx_rb_read_bit(rb);
1028 // Segmentation map update
1029 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
1030 seg->update_map = 1;
1032 seg->update_map = vpx_rb_read_bit(rb);
1034 if (seg->update_map) {
1035 for (i = 0; i < SEG_TREE_PROBS; i++)
1036 seg->tree_probs[i] = vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8)
1039 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
1040 seg->temporal_update = 0;
1042 seg->temporal_update = vpx_rb_read_bit(rb);
1044 if (seg->temporal_update) {
1045 for (i = 0; i < PREDICTION_PROBS; i++)
1046 seg->pred_probs[i] = vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8)
1049 for (i = 0; i < PREDICTION_PROBS; i++)
1050 seg->pred_probs[i] = MAX_PROB;
1054 // Segmentation data update
1055 seg->update_data = vpx_rb_read_bit(rb);
1056 if (seg->update_data) {
1057 seg->abs_delta = vpx_rb_read_bit(rb);
1059 vp10_clearall_segfeatures(seg);
1061 for (i = 0; i < MAX_SEGMENTS; i++) {
1062 for (j = 0; j < SEG_LVL_MAX; j++) {
1064 const int feature_enabled = vpx_rb_read_bit(rb);
1065 if (feature_enabled) {
1066 vp10_enable_segfeature(seg, i, j);
1067 data = decode_unsigned_max(rb, vp10_seg_feature_data_max(j));
1068 if (vp10_is_segfeature_signed(j))
1069 data = vpx_rb_read_bit(rb) ? -data : data;
1071 vp10_set_segdata(seg, i, j, data);
1077 static void setup_loopfilter(struct loopfilter *lf,
1078 struct vpx_read_bit_buffer *rb) {
1079 lf->filter_level = vpx_rb_read_literal(rb, 6);
1080 lf->sharpness_level = vpx_rb_read_literal(rb, 3);
1082 // Read in loop filter deltas applied at the MB level based on mode or ref
1084 lf->mode_ref_delta_update = 0;
1086 lf->mode_ref_delta_enabled = vpx_rb_read_bit(rb);
1087 if (lf->mode_ref_delta_enabled) {
1088 lf->mode_ref_delta_update = vpx_rb_read_bit(rb);
1089 if (lf->mode_ref_delta_update) {
1092 for (i = 0; i < MAX_REF_FRAMES; i++)
1093 if (vpx_rb_read_bit(rb))
1094 lf->ref_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
1096 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
1097 if (vpx_rb_read_bit(rb))
1098 lf->mode_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
1103 static INLINE int read_delta_q(struct vpx_read_bit_buffer *rb) {
1104 return vpx_rb_read_bit(rb) ? vpx_rb_read_signed_literal(rb, 4) : 0;
1107 static void setup_quantization(VP10_COMMON *const cm, MACROBLOCKD *const xd,
1108 struct vpx_read_bit_buffer *rb) {
1109 cm->base_qindex = vpx_rb_read_literal(rb, QINDEX_BITS);
1110 cm->y_dc_delta_q = read_delta_q(rb);
1111 cm->uv_dc_delta_q = read_delta_q(rb);
1112 cm->uv_ac_delta_q = read_delta_q(rb);
1113 cm->dequant_bit_depth = cm->bit_depth;
1114 xd->lossless = cm->base_qindex == 0 &&
1115 cm->y_dc_delta_q == 0 &&
1116 cm->uv_dc_delta_q == 0 &&
1117 cm->uv_ac_delta_q == 0;
1119 #if CONFIG_VP9_HIGHBITDEPTH
1120 xd->bd = (int)cm->bit_depth;
1124 static void setup_segmentation_dequant(VP10_COMMON *const cm) {
1125 // Build y/uv dequant values based on segmentation.
1126 if (cm->seg.enabled) {
1128 for (i = 0; i < MAX_SEGMENTS; ++i) {
1129 const int qindex = vp10_get_qindex(&cm->seg, i, cm->base_qindex);
1130 cm->y_dequant[i][0] = vp10_dc_quant(qindex, cm->y_dc_delta_q,
1132 cm->y_dequant[i][1] = vp10_ac_quant(qindex, 0, cm->bit_depth);
1133 cm->uv_dequant[i][0] = vp10_dc_quant(qindex, cm->uv_dc_delta_q,
1135 cm->uv_dequant[i][1] = vp10_ac_quant(qindex, cm->uv_ac_delta_q,
1139 const int qindex = cm->base_qindex;
1140 // When segmentation is disabled, only the first value is used. The
1141 // remaining are don't cares.
1142 cm->y_dequant[0][0] = vp10_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
1143 cm->y_dequant[0][1] = vp10_ac_quant(qindex, 0, cm->bit_depth);
1144 cm->uv_dequant[0][0] = vp10_dc_quant(qindex, cm->uv_dc_delta_q,
1146 cm->uv_dequant[0][1] = vp10_ac_quant(qindex, cm->uv_ac_delta_q,
1151 static INTERP_FILTER read_interp_filter(struct vpx_read_bit_buffer *rb) {
1152 return vpx_rb_read_bit(rb) ? SWITCHABLE : vpx_rb_read_literal(rb, 2);
1155 static void setup_display_size(VP10_COMMON *cm,
1156 struct vpx_read_bit_buffer *rb) {
1157 cm->display_width = cm->width;
1158 cm->display_height = cm->height;
1159 if (vpx_rb_read_bit(rb))
1160 vp10_read_frame_size(rb, &cm->display_width, &cm->display_height);
1163 static void resize_mv_buffer(VP10_COMMON *cm) {
1164 vpx_free(cm->cur_frame->mvs);
1165 cm->cur_frame->mi_rows = cm->mi_rows;
1166 cm->cur_frame->mi_cols = cm->mi_cols;
1167 cm->cur_frame->mvs = (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
1168 sizeof(*cm->cur_frame->mvs));
1171 static void resize_context_buffers(VP10_COMMON *cm, int width, int height) {
1172 #if CONFIG_SIZE_LIMIT
1173 if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
1174 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1175 "Dimensions of %dx%d beyond allowed size of %dx%d.",
1176 width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
1178 if (cm->width != width || cm->height != height) {
1179 const int new_mi_rows =
1180 ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
1181 const int new_mi_cols =
1182 ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
1184 // Allocations in vp10_alloc_context_buffers() depend on individual
1185 // dimensions as well as the overall size.
1186 if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
1187 if (vp10_alloc_context_buffers(cm, width, height))
1188 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1189 "Failed to allocate context buffers");
1191 vp10_set_mb_mi(cm, width, height);
1193 vp10_init_context_buffers(cm);
1195 cm->height = height;
1197 if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
1198 cm->mi_cols > cm->cur_frame->mi_cols) {
1199 resize_mv_buffer(cm);
1203 static void setup_frame_size(VP10_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1205 BufferPool *const pool = cm->buffer_pool;
1206 vp10_read_frame_size(rb, &width, &height);
1207 resize_context_buffers(cm, width, height);
1208 setup_display_size(cm, rb);
1210 lock_buffer_pool(pool);
1211 if (vpx_realloc_frame_buffer(
1212 get_frame_new_buffer(cm), cm->width, cm->height,
1213 cm->subsampling_x, cm->subsampling_y,
1214 #if CONFIG_VP9_HIGHBITDEPTH
1215 cm->use_highbitdepth,
1217 VP9_DEC_BORDER_IN_PIXELS,
1219 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1221 unlock_buffer_pool(pool);
1222 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1223 "Failed to allocate frame buffer");
1225 unlock_buffer_pool(pool);
1227 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1228 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1229 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1230 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1233 static INLINE int valid_ref_frame_img_fmt(vpx_bit_depth_t ref_bit_depth,
1234 int ref_xss, int ref_yss,
1235 vpx_bit_depth_t this_bit_depth,
1236 int this_xss, int this_yss) {
1237 return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
1238 ref_yss == this_yss;
1241 static void setup_frame_size_with_refs(VP10_COMMON *cm,
1242 struct vpx_read_bit_buffer *rb) {
1245 int has_valid_ref_frame = 0;
1246 BufferPool *const pool = cm->buffer_pool;
1247 for (i = 0; i < REFS_PER_FRAME; ++i) {
1248 if (vpx_rb_read_bit(rb)) {
1249 YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
1250 width = buf->y_crop_width;
1251 height = buf->y_crop_height;
1258 vp10_read_frame_size(rb, &width, &height);
1260 if (width <= 0 || height <= 0)
1261 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1262 "Invalid frame size");
1264 // Check to make sure at least one of frames that this frame references
1265 // has valid dimensions.
1266 for (i = 0; i < REFS_PER_FRAME; ++i) {
1267 RefBuffer *const ref_frame = &cm->frame_refs[i];
1268 has_valid_ref_frame |= valid_ref_frame_size(ref_frame->buf->y_crop_width,
1269 ref_frame->buf->y_crop_height,
1272 if (!has_valid_ref_frame)
1273 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1274 "Referenced frame has invalid size");
1275 for (i = 0; i < REFS_PER_FRAME; ++i) {
1276 RefBuffer *const ref_frame = &cm->frame_refs[i];
1277 if (!valid_ref_frame_img_fmt(
1278 ref_frame->buf->bit_depth,
1279 ref_frame->buf->subsampling_x,
1280 ref_frame->buf->subsampling_y,
1284 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1285 "Referenced frame has incompatible color format");
1288 resize_context_buffers(cm, width, height);
1289 setup_display_size(cm, rb);
1291 lock_buffer_pool(pool);
1292 if (vpx_realloc_frame_buffer(
1293 get_frame_new_buffer(cm), cm->width, cm->height,
1294 cm->subsampling_x, cm->subsampling_y,
1295 #if CONFIG_VP9_HIGHBITDEPTH
1296 cm->use_highbitdepth,
1298 VP9_DEC_BORDER_IN_PIXELS,
1300 &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1302 unlock_buffer_pool(pool);
1303 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1304 "Failed to allocate frame buffer");
1306 unlock_buffer_pool(pool);
1308 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1309 pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1310 pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1311 pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1314 static void setup_tile_info(VP10_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1315 int min_log2_tile_cols, max_log2_tile_cols, max_ones;
1316 vp10_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1319 max_ones = max_log2_tile_cols - min_log2_tile_cols;
1320 cm->log2_tile_cols = min_log2_tile_cols;
1321 while (max_ones-- && vpx_rb_read_bit(rb))
1322 cm->log2_tile_cols++;
1324 if (cm->log2_tile_cols > 6)
1325 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1326 "Invalid number of tile columns");
1329 cm->log2_tile_rows = vpx_rb_read_bit(rb);
1330 if (cm->log2_tile_rows)
1331 cm->log2_tile_rows += vpx_rb_read_bit(rb);
1334 typedef struct TileBuffer {
1335 const uint8_t *data;
1337 int col; // only used with multi-threaded decoding
1340 // Reads the next tile returning its size and adjusting '*data' accordingly
1341 // based on 'is_last'.
1342 static void get_tile_buffer(const uint8_t *const data_end,
1344 struct vpx_internal_error_info *error_info,
1345 const uint8_t **data,
1346 vpx_decrypt_cb decrypt_cb, void *decrypt_state,
1351 if (!read_is_valid(*data, 4, data_end))
1352 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1353 "Truncated packet or corrupt tile length");
1357 decrypt_cb(decrypt_state, *data, be_data, 4);
1358 size = mem_get_be32(be_data);
1360 size = mem_get_be32(*data);
1364 if (size > (size_t)(data_end - *data))
1365 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1366 "Truncated packet or corrupt tile size");
1368 size = data_end - *data;
1377 static void get_tile_buffers(VP10Decoder *pbi,
1378 const uint8_t *data, const uint8_t *data_end,
1379 int tile_cols, int tile_rows,
1380 TileBuffer (*tile_buffers)[1 << 6]) {
1383 for (r = 0; r < tile_rows; ++r) {
1384 for (c = 0; c < tile_cols; ++c) {
1385 const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
1386 TileBuffer *const buf = &tile_buffers[r][c];
1388 get_tile_buffer(data_end, is_last, &pbi->common.error, &data,
1389 pbi->decrypt_cb, pbi->decrypt_state, buf);
1394 static const uint8_t *decode_tiles(VP10Decoder *pbi,
1395 const uint8_t *data,
1396 const uint8_t *data_end) {
1397 VP10_COMMON *const cm = &pbi->common;
1398 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
1399 const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1400 const int tile_cols = 1 << cm->log2_tile_cols;
1401 const int tile_rows = 1 << cm->log2_tile_rows;
1402 TileBuffer tile_buffers[4][1 << 6];
1403 int tile_row, tile_col;
1405 TileData *tile_data = NULL;
1407 if (cm->lf.filter_level && !cm->skip_loop_filter &&
1408 pbi->lf_worker.data1 == NULL) {
1409 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
1410 vpx_memalign(32, sizeof(LFWorkerData)));
1411 pbi->lf_worker.hook = (VPxWorkerHook)vp10_loop_filter_worker;
1412 if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
1413 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
1414 "Loop filter thread creation failed");
1418 if (cm->lf.filter_level && !cm->skip_loop_filter) {
1419 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
1420 // Be sure to sync as we might be resuming after a failed frame decode.
1421 winterface->sync(&pbi->lf_worker);
1422 vp10_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
1426 assert(tile_rows <= 4);
1427 assert(tile_cols <= (1 << 6));
1429 // Note: this memset assumes above_context[0], [1] and [2]
1430 // are allocated as part of the same buffer.
1431 memset(cm->above_context, 0,
1432 sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_cols);
1434 memset(cm->above_seg_context, 0,
1435 sizeof(*cm->above_seg_context) * aligned_cols);
1437 get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, tile_buffers);
1439 if (pbi->tile_data == NULL ||
1440 (tile_cols * tile_rows) != pbi->total_tiles) {
1441 vpx_free(pbi->tile_data);
1445 vpx_memalign(32, tile_cols * tile_rows * (sizeof(*pbi->tile_data))));
1446 pbi->total_tiles = tile_rows * tile_cols;
1449 // Load all tile information into tile_data.
1450 for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
1451 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
1452 const TileBuffer *const buf = &tile_buffers[tile_row][tile_col];
1453 tile_data = pbi->tile_data + tile_cols * tile_row + tile_col;
1455 tile_data->xd = pbi->mb;
1456 tile_data->xd.corrupted = 0;
1457 tile_data->xd.counts = cm->frame_parallel_decoding_mode ?
1459 vp10_zero(tile_data->dqcoeff);
1460 vp10_tile_init(&tile_data->xd.tile, tile_data->cm, tile_row, tile_col);
1461 setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
1462 &tile_data->bit_reader, pbi->decrypt_cb,
1463 pbi->decrypt_state);
1464 vp10_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
1468 for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
1470 vp10_tile_set_row(&tile, cm, tile_row);
1471 for (mi_row = tile.mi_row_start; mi_row < tile.mi_row_end;
1472 mi_row += MI_BLOCK_SIZE) {
1473 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
1474 const int col = pbi->inv_tile_order ?
1475 tile_cols - tile_col - 1 : tile_col;
1476 tile_data = pbi->tile_data + tile_cols * tile_row + col;
1477 vp10_tile_set_col(&tile, tile_data->cm, col);
1478 vp10_zero(tile_data->xd.left_context);
1479 vp10_zero(tile_data->xd.left_seg_context);
1480 for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end;
1481 mi_col += MI_BLOCK_SIZE) {
1482 decode_partition(pbi, &tile_data->xd, mi_row,
1483 mi_col, &tile_data->bit_reader, BLOCK_64X64, 4);
1485 pbi->mb.corrupted |= tile_data->xd.corrupted;
1486 if (pbi->mb.corrupted)
1487 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1488 "Failed to decode tile data");
1490 // Loopfilter one row.
1491 if (cm->lf.filter_level && !cm->skip_loop_filter) {
1492 const int lf_start = mi_row - MI_BLOCK_SIZE;
1493 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
1495 // delay the loopfilter by 1 macroblock row.
1496 if (lf_start < 0) continue;
1498 // decoding has completed: finish up the loop filter in this thread.
1499 if (mi_row + MI_BLOCK_SIZE >= cm->mi_rows) continue;
1501 winterface->sync(&pbi->lf_worker);
1502 lf_data->start = lf_start;
1503 lf_data->stop = mi_row;
1504 if (pbi->max_threads > 1) {
1505 winterface->launch(&pbi->lf_worker);
1507 winterface->execute(&pbi->lf_worker);
1510 // After loopfiltering, the last 7 row pixels in each superblock row may
1511 // still be changed by the longest loopfilter of the next superblock
1513 if (cm->frame_parallel_decode)
1514 vp10_frameworker_broadcast(pbi->cur_buf,
1515 mi_row << MI_BLOCK_SIZE_LOG2);
1519 // Loopfilter remaining rows in the frame.
1520 if (cm->lf.filter_level && !cm->skip_loop_filter) {
1521 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
1522 winterface->sync(&pbi->lf_worker);
1523 lf_data->start = lf_data->stop;
1524 lf_data->stop = cm->mi_rows;
1525 winterface->execute(&pbi->lf_worker);
1528 // Get last tile data.
1529 tile_data = pbi->tile_data + tile_cols * tile_rows - 1;
1531 if (cm->frame_parallel_decode)
1532 vp10_frameworker_broadcast(pbi->cur_buf, INT_MAX);
1533 return vpx_reader_find_end(&tile_data->bit_reader);
1536 static int tile_worker_hook(TileWorkerData *const tile_data,
1537 const TileInfo *const tile) {
1540 if (setjmp(tile_data->error_info.jmp)) {
1541 tile_data->error_info.setjmp = 0;
1542 tile_data->xd.corrupted = 1;
1546 tile_data->error_info.setjmp = 1;
1547 tile_data->xd.error_info = &tile_data->error_info;
1549 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
1550 mi_row += MI_BLOCK_SIZE) {
1551 vp10_zero(tile_data->xd.left_context);
1552 vp10_zero(tile_data->xd.left_seg_context);
1553 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
1554 mi_col += MI_BLOCK_SIZE) {
1555 decode_partition(tile_data->pbi, &tile_data->xd,
1556 mi_row, mi_col, &tile_data->bit_reader,
1560 return !tile_data->xd.corrupted;
1563 // sorts in descending order
1564 static int compare_tile_buffers(const void *a, const void *b) {
1565 const TileBuffer *const buf1 = (const TileBuffer*)a;
1566 const TileBuffer *const buf2 = (const TileBuffer*)b;
1567 return (int)(buf2->size - buf1->size);
1570 static const uint8_t *decode_tiles_mt(VP10Decoder *pbi,
1571 const uint8_t *data,
1572 const uint8_t *data_end) {
1573 VP10_COMMON *const cm = &pbi->common;
1574 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
1575 const uint8_t *bit_reader_end = NULL;
1576 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1577 const int tile_cols = 1 << cm->log2_tile_cols;
1578 const int tile_rows = 1 << cm->log2_tile_rows;
1579 const int num_workers = VPXMIN(pbi->max_threads & ~1, tile_cols);
1580 TileBuffer tile_buffers[1][1 << 6];
1582 int final_worker = -1;
1584 assert(tile_cols <= (1 << 6));
1585 assert(tile_rows == 1);
1588 // TODO(jzern): See if we can remove the restriction of passing in max
1589 // threads to the decoder.
1590 if (pbi->num_tile_workers == 0) {
1591 const int num_threads = pbi->max_threads & ~1;
1593 CHECK_MEM_ERROR(cm, pbi->tile_workers,
1594 vpx_malloc(num_threads * sizeof(*pbi->tile_workers)));
1595 // Ensure tile data offsets will be properly aligned. This may fail on
1596 // platforms without DECLARE_ALIGNED().
1597 assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
1598 CHECK_MEM_ERROR(cm, pbi->tile_worker_data,
1599 vpx_memalign(32, num_threads *
1600 sizeof(*pbi->tile_worker_data)));
1601 CHECK_MEM_ERROR(cm, pbi->tile_worker_info,
1602 vpx_malloc(num_threads * sizeof(*pbi->tile_worker_info)));
1603 for (i = 0; i < num_threads; ++i) {
1604 VPxWorker *const worker = &pbi->tile_workers[i];
1605 ++pbi->num_tile_workers;
1607 winterface->init(worker);
1608 if (i < num_threads - 1 && !winterface->reset(worker)) {
1609 vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
1610 "Tile decoder thread creation failed");
1615 // Reset tile decoding hook
1616 for (n = 0; n < num_workers; ++n) {
1617 VPxWorker *const worker = &pbi->tile_workers[n];
1618 winterface->sync(worker);
1619 worker->hook = (VPxWorkerHook)tile_worker_hook;
1620 worker->data1 = &pbi->tile_worker_data[n];
1621 worker->data2 = &pbi->tile_worker_info[n];
1624 // Note: this memset assumes above_context[0], [1] and [2]
1625 // are allocated as part of the same buffer.
1626 memset(cm->above_context, 0,
1627 sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_mi_cols);
1628 memset(cm->above_seg_context, 0,
1629 sizeof(*cm->above_seg_context) * aligned_mi_cols);
1631 // Load tile data into tile_buffers
1632 get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, tile_buffers);
1634 // Sort the buffers based on size in descending order.
1635 qsort(tile_buffers[0], tile_cols, sizeof(tile_buffers[0][0]),
1636 compare_tile_buffers);
1638 // Rearrange the tile buffers such that per-tile group the largest, and
1639 // presumably the most difficult, tile will be decoded in the main thread.
1640 // This should help minimize the number of instances where the main thread is
1641 // waiting for a worker to complete.
1643 int group_start = 0;
1644 while (group_start < tile_cols) {
1645 const TileBuffer largest = tile_buffers[0][group_start];
1646 const int group_end = VPXMIN(group_start + num_workers, tile_cols) - 1;
1647 memmove(tile_buffers[0] + group_start, tile_buffers[0] + group_start + 1,
1648 (group_end - group_start) * sizeof(tile_buffers[0][0]));
1649 tile_buffers[0][group_end] = largest;
1650 group_start = group_end + 1;
1654 // Initialize thread frame counts.
1655 if (!cm->frame_parallel_decoding_mode) {
1658 for (i = 0; i < num_workers; ++i) {
1659 TileWorkerData *const tile_data =
1660 (TileWorkerData*)pbi->tile_workers[i].data1;
1661 vp10_zero(tile_data->counts);
1666 while (n < tile_cols) {
1668 for (i = 0; i < num_workers && n < tile_cols; ++i) {
1669 VPxWorker *const worker = &pbi->tile_workers[i];
1670 TileWorkerData *const tile_data = (TileWorkerData*)worker->data1;
1671 TileInfo *const tile = (TileInfo*)worker->data2;
1672 TileBuffer *const buf = &tile_buffers[0][n];
1674 tile_data->pbi = pbi;
1675 tile_data->xd = pbi->mb;
1676 tile_data->xd.corrupted = 0;
1677 tile_data->xd.counts = cm->frame_parallel_decoding_mode ?
1678 0 : &tile_data->counts;
1679 vp10_zero(tile_data->dqcoeff);
1680 vp10_tile_init(tile, cm, 0, buf->col);
1681 vp10_tile_init(&tile_data->xd.tile, cm, 0, buf->col);
1682 setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
1683 &tile_data->bit_reader, pbi->decrypt_cb,
1684 pbi->decrypt_state);
1685 vp10_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
1687 worker->had_error = 0;
1688 if (i == num_workers - 1 || n == tile_cols - 1) {
1689 winterface->execute(worker);
1691 winterface->launch(worker);
1694 if (buf->col == tile_cols - 1) {
1701 for (; i > 0; --i) {
1702 VPxWorker *const worker = &pbi->tile_workers[i - 1];
1703 // TODO(jzern): The tile may have specific error data associated with
1704 // its vpx_internal_error_info which could be propagated to the main info
1705 // in cm. Additionally once the threads have been synced and an error is
1706 // detected, there's no point in continuing to decode tiles.
1707 pbi->mb.corrupted |= !winterface->sync(worker);
1709 if (final_worker > -1) {
1710 TileWorkerData *const tile_data =
1711 (TileWorkerData*)pbi->tile_workers[final_worker].data1;
1712 bit_reader_end = vpx_reader_find_end(&tile_data->bit_reader);
1716 // Accumulate thread frame counts.
1717 if (n >= tile_cols && !cm->frame_parallel_decoding_mode) {
1718 for (i = 0; i < num_workers; ++i) {
1719 TileWorkerData *const tile_data =
1720 (TileWorkerData*)pbi->tile_workers[i].data1;
1721 vp10_accumulate_frame_counts(cm, &tile_data->counts, 1);
1726 return bit_reader_end;
1729 static void error_handler(void *data) {
1730 VP10_COMMON *const cm = (VP10_COMMON *)data;
1731 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet");
1734 static void read_bitdepth_colorspace_sampling(
1735 VP10_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1736 if (cm->profile >= PROFILE_2) {
1737 cm->bit_depth = vpx_rb_read_bit(rb) ? VPX_BITS_12 : VPX_BITS_10;
1738 #if CONFIG_VP9_HIGHBITDEPTH
1739 cm->use_highbitdepth = 1;
1742 cm->bit_depth = VPX_BITS_8;
1743 #if CONFIG_VP9_HIGHBITDEPTH
1744 cm->use_highbitdepth = 0;
1747 cm->color_space = vpx_rb_read_literal(rb, 3);
1748 if (cm->color_space != VPX_CS_SRGB) {
1749 vpx_rb_read_bit(rb); // [16,235] (including xvycc) vs [0,255] range
1750 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
1751 cm->subsampling_x = vpx_rb_read_bit(rb);
1752 cm->subsampling_y = vpx_rb_read_bit(rb);
1753 if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
1754 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1755 "4:2:0 color not supported in profile 1 or 3");
1756 if (vpx_rb_read_bit(rb))
1757 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1758 "Reserved bit set");
1760 cm->subsampling_y = cm->subsampling_x = 1;
1763 if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
1764 // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
1765 // 4:2:2 or 4:4:0 chroma sampling is not allowed.
1766 cm->subsampling_y = cm->subsampling_x = 0;
1767 if (vpx_rb_read_bit(rb))
1768 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1769 "Reserved bit set");
1771 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1772 "4:4:4 color not supported in profile 0 or 2");
1777 static size_t read_uncompressed_header(VP10Decoder *pbi,
1778 struct vpx_read_bit_buffer *rb) {
1779 VP10_COMMON *const cm = &pbi->common;
1780 BufferPool *const pool = cm->buffer_pool;
1781 RefCntBuffer *const frame_bufs = pool->frame_bufs;
1782 int i, mask, ref_index = 0;
1785 cm->last_frame_type = cm->frame_type;
1786 cm->last_intra_only = cm->intra_only;
1788 if (vpx_rb_read_literal(rb, 2) != VP9_FRAME_MARKER)
1789 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1790 "Invalid frame marker");
1792 cm->profile = vp10_read_profile(rb);
1793 #if CONFIG_VP9_HIGHBITDEPTH
1794 if (cm->profile >= MAX_PROFILES)
1795 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1796 "Unsupported bitstream profile");
1798 if (cm->profile >= PROFILE_2)
1799 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1800 "Unsupported bitstream profile");
1803 cm->show_existing_frame = vpx_rb_read_bit(rb);
1804 if (cm->show_existing_frame) {
1805 // Show an existing frame directly.
1806 const int frame_to_show = cm->ref_frame_map[vpx_rb_read_literal(rb, 3)];
1807 lock_buffer_pool(pool);
1808 if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
1809 unlock_buffer_pool(pool);
1810 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1811 "Buffer %d does not contain a decoded frame",
1815 ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
1816 unlock_buffer_pool(pool);
1817 pbi->refresh_frame_flags = 0;
1818 cm->lf.filter_level = 0;
1821 if (cm->frame_parallel_decode) {
1822 for (i = 0; i < REF_FRAMES; ++i)
1823 cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
1828 cm->frame_type = (FRAME_TYPE) vpx_rb_read_bit(rb);
1829 cm->show_frame = vpx_rb_read_bit(rb);
1830 cm->error_resilient_mode = vpx_rb_read_bit(rb);
1832 if (cm->frame_type == KEY_FRAME) {
1833 if (!vp10_read_sync_code(rb))
1834 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1835 "Invalid frame sync code");
1837 read_bitdepth_colorspace_sampling(cm, rb);
1838 pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
1840 for (i = 0; i < REFS_PER_FRAME; ++i) {
1841 cm->frame_refs[i].idx = INVALID_IDX;
1842 cm->frame_refs[i].buf = NULL;
1845 setup_frame_size(cm, rb);
1846 if (pbi->need_resync) {
1847 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
1848 pbi->need_resync = 0;
1851 cm->intra_only = cm->show_frame ? 0 : vpx_rb_read_bit(rb);
1853 if (cm->error_resilient_mode) {
1854 cm->reset_frame_context = RESET_FRAME_CONTEXT_ALL;
1856 #if CONFIG_MISC_FIXES
1857 if (cm->intra_only) {
1858 cm->reset_frame_context =
1859 vpx_rb_read_bit(rb) ? RESET_FRAME_CONTEXT_ALL
1860 : RESET_FRAME_CONTEXT_CURRENT;
1862 cm->reset_frame_context =
1863 vpx_rb_read_bit(rb) ? RESET_FRAME_CONTEXT_CURRENT
1864 : RESET_FRAME_CONTEXT_NONE;
1865 if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT)
1866 cm->reset_frame_context =
1867 vpx_rb_read_bit(rb) ? RESET_FRAME_CONTEXT_ALL
1868 : RESET_FRAME_CONTEXT_CURRENT;
1871 static const RESET_FRAME_CONTEXT_MODE reset_frame_context_conv_tbl[4] = {
1872 RESET_FRAME_CONTEXT_NONE, RESET_FRAME_CONTEXT_NONE,
1873 RESET_FRAME_CONTEXT_CURRENT, RESET_FRAME_CONTEXT_ALL
1876 cm->reset_frame_context =
1877 reset_frame_context_conv_tbl[vpx_rb_read_literal(rb, 2)];
1881 if (cm->intra_only) {
1882 if (!vp10_read_sync_code(rb))
1883 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1884 "Invalid frame sync code");
1885 if (cm->profile > PROFILE_0) {
1886 read_bitdepth_colorspace_sampling(cm, rb);
1888 // NOTE: The intra-only frame header does not include the specification
1889 // of either the color format or color sub-sampling in profile 0. VP9
1890 // specifies that the default color format should be YUV 4:2:0 in this
1891 // case (normative).
1892 cm->color_space = VPX_CS_BT_601;
1893 cm->subsampling_y = cm->subsampling_x = 1;
1894 cm->bit_depth = VPX_BITS_8;
1895 #if CONFIG_VP9_HIGHBITDEPTH
1896 cm->use_highbitdepth = 0;
1900 pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
1901 setup_frame_size(cm, rb);
1902 if (pbi->need_resync) {
1903 memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
1904 pbi->need_resync = 0;
1906 } else if (pbi->need_resync != 1) { /* Skip if need resync */
1907 pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
1908 for (i = 0; i < REFS_PER_FRAME; ++i) {
1909 const int ref = vpx_rb_read_literal(rb, REF_FRAMES_LOG2);
1910 const int idx = cm->ref_frame_map[ref];
1911 RefBuffer *const ref_frame = &cm->frame_refs[i];
1912 ref_frame->idx = idx;
1913 ref_frame->buf = &frame_bufs[idx].buf;
1914 cm->ref_frame_sign_bias[LAST_FRAME + i] = vpx_rb_read_bit(rb);
1917 setup_frame_size_with_refs(cm, rb);
1919 cm->allow_high_precision_mv = vpx_rb_read_bit(rb);
1920 cm->interp_filter = read_interp_filter(rb);
1922 for (i = 0; i < REFS_PER_FRAME; ++i) {
1923 RefBuffer *const ref_buf = &cm->frame_refs[i];
1924 #if CONFIG_VP9_HIGHBITDEPTH
1925 vp10_setup_scale_factors_for_frame(&ref_buf->sf,
1926 ref_buf->buf->y_crop_width,
1927 ref_buf->buf->y_crop_height,
1928 cm->width, cm->height,
1929 cm->use_highbitdepth);
1931 vp10_setup_scale_factors_for_frame(&ref_buf->sf,
1932 ref_buf->buf->y_crop_width,
1933 ref_buf->buf->y_crop_height,
1934 cm->width, cm->height);
1939 #if CONFIG_VP9_HIGHBITDEPTH
1940 get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
1942 get_frame_new_buffer(cm)->color_space = cm->color_space;
1944 if (pbi->need_resync) {
1945 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1946 "Keyframe / intra-only frame required to reset decoder"
1950 if (!cm->error_resilient_mode) {
1951 cm->refresh_frame_context = vpx_rb_read_bit(rb);
1952 cm->frame_parallel_decoding_mode = vpx_rb_read_bit(rb);
1954 cm->refresh_frame_context = 0;
1955 cm->frame_parallel_decoding_mode = 1;
1958 // This flag will be overridden by the call to vp10_setup_past_independence
1959 // below, forcing the use of context 0 for those frame types.
1960 cm->frame_context_idx = vpx_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
1962 // Generate next_ref_frame_map.
1963 lock_buffer_pool(pool);
1964 for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
1966 cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
1967 ++frame_bufs[cm->new_fb_idx].ref_count;
1969 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
1971 // Current thread holds the reference frame.
1972 if (cm->ref_frame_map[ref_index] >= 0)
1973 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
1977 for (; ref_index < REF_FRAMES; ++ref_index) {
1978 cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
1979 // Current thread holds the reference frame.
1980 if (cm->ref_frame_map[ref_index] >= 0)
1981 ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
1983 unlock_buffer_pool(pool);
1984 pbi->hold_ref_buf = 1;
1986 if (frame_is_intra_only(cm) || cm->error_resilient_mode)
1987 vp10_setup_past_independence(cm);
1989 setup_loopfilter(&cm->lf, rb);
1990 setup_quantization(cm, &pbi->mb, rb);
1991 setup_segmentation(cm, rb);
1992 setup_segmentation_dequant(cm);
1994 setup_tile_info(cm, rb);
1995 sz = vpx_rb_read_literal(rb, 16);
1998 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1999 "Invalid header size");
2004 static int read_compressed_header(VP10Decoder *pbi, const uint8_t *data,
2005 size_t partition_size) {
2006 VP10_COMMON *const cm = &pbi->common;
2007 MACROBLOCKD *const xd = &pbi->mb;
2008 FRAME_CONTEXT *const fc = cm->fc;
2012 if (vpx_reader_init(&r, data, partition_size, pbi->decrypt_cb,
2013 pbi->decrypt_state))
2014 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
2015 "Failed to allocate bool decoder 0");
2017 cm->tx_mode = xd->lossless ? ONLY_4X4 : read_tx_mode(&r);
2018 if (cm->tx_mode == TX_MODE_SELECT)
2019 read_tx_mode_probs(&fc->tx_probs, &r);
2020 read_coef_probs(fc, cm->tx_mode, &r);
2022 for (k = 0; k < SKIP_CONTEXTS; ++k)
2023 vp10_diff_update_prob(&r, &fc->skip_probs[k]);
2025 if (!frame_is_intra_only(cm)) {
2026 nmv_context *const nmvc = &fc->nmvc;
2029 read_inter_mode_probs(fc, &r);
2031 if (cm->interp_filter == SWITCHABLE)
2032 read_switchable_interp_probs(fc, &r);
2034 for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
2035 vp10_diff_update_prob(&r, &fc->intra_inter_prob[i]);
2037 cm->reference_mode = read_frame_reference_mode(cm, &r);
2038 if (cm->reference_mode != SINGLE_REFERENCE)
2039 setup_compound_reference_mode(cm);
2040 read_frame_reference_mode_probs(cm, &r);
2042 for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
2043 for (i = 0; i < INTRA_MODES - 1; ++i)
2044 vp10_diff_update_prob(&r, &fc->y_mode_prob[j][i]);
2046 for (j = 0; j < PARTITION_CONTEXTS; ++j)
2047 for (i = 0; i < PARTITION_TYPES - 1; ++i)
2048 vp10_diff_update_prob(&r, &fc->partition_prob[j][i]);
2050 read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
2053 return vpx_reader_has_error(&r);
2057 #define debug_check_frame_counts(cm) (void)0
2059 // Counts should only be incremented when frame_parallel_decoding_mode and
2060 // error_resilient_mode are disabled.
2061 static void debug_check_frame_counts(const VP10_COMMON *const cm) {
2062 FRAME_COUNTS zero_counts;
2063 vp10_zero(zero_counts);
2064 assert(cm->frame_parallel_decoding_mode || cm->error_resilient_mode);
2065 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode,
2066 sizeof(cm->counts.y_mode)));
2067 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode,
2068 sizeof(cm->counts.uv_mode)));
2069 assert(!memcmp(cm->counts.partition, zero_counts.partition,
2070 sizeof(cm->counts.partition)));
2071 assert(!memcmp(cm->counts.coef, zero_counts.coef,
2072 sizeof(cm->counts.coef)));
2073 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch,
2074 sizeof(cm->counts.eob_branch)));
2075 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp,
2076 sizeof(cm->counts.switchable_interp)));
2077 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode,
2078 sizeof(cm->counts.inter_mode)));
2079 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter,
2080 sizeof(cm->counts.intra_inter)));
2081 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter,
2082 sizeof(cm->counts.comp_inter)));
2083 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref,
2084 sizeof(cm->counts.single_ref)));
2085 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref,
2086 sizeof(cm->counts.comp_ref)));
2087 assert(!memcmp(&cm->counts.tx, &zero_counts.tx, sizeof(cm->counts.tx)));
2088 assert(!memcmp(cm->counts.skip, zero_counts.skip, sizeof(cm->counts.skip)));
2089 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv)));
2093 static struct vpx_read_bit_buffer *init_read_bit_buffer(
2095 struct vpx_read_bit_buffer *rb,
2096 const uint8_t *data,
2097 const uint8_t *data_end,
2098 uint8_t clear_data[MAX_VP9_HEADER_SIZE]) {
2100 rb->error_handler = error_handler;
2101 rb->error_handler_data = &pbi->common;
2102 if (pbi->decrypt_cb) {
2103 const int n = (int)VPXMIN(MAX_VP9_HEADER_SIZE, data_end - data);
2104 pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
2105 rb->bit_buffer = clear_data;
2106 rb->bit_buffer_end = clear_data + n;
2108 rb->bit_buffer = data;
2109 rb->bit_buffer_end = data_end;
2114 //------------------------------------------------------------------------------
2116 int vp10_read_sync_code(struct vpx_read_bit_buffer *const rb) {
2117 return vpx_rb_read_literal(rb, 8) == VP10_SYNC_CODE_0 &&
2118 vpx_rb_read_literal(rb, 8) == VP10_SYNC_CODE_1 &&
2119 vpx_rb_read_literal(rb, 8) == VP10_SYNC_CODE_2;
2122 void vp10_read_frame_size(struct vpx_read_bit_buffer *rb,
2123 int *width, int *height) {
2124 *width = vpx_rb_read_literal(rb, 16) + 1;
2125 *height = vpx_rb_read_literal(rb, 16) + 1;
2128 BITSTREAM_PROFILE vp10_read_profile(struct vpx_read_bit_buffer *rb) {
2129 int profile = vpx_rb_read_bit(rb);
2130 profile |= vpx_rb_read_bit(rb) << 1;
2132 profile += vpx_rb_read_bit(rb);
2133 return (BITSTREAM_PROFILE) profile;
2136 void vp10_decode_frame(VP10Decoder *pbi,
2137 const uint8_t *data, const uint8_t *data_end,
2138 const uint8_t **p_data_end) {
2139 VP10_COMMON *const cm = &pbi->common;
2140 MACROBLOCKD *const xd = &pbi->mb;
2141 struct vpx_read_bit_buffer rb;
2142 int context_updated = 0;
2143 uint8_t clear_data[MAX_VP9_HEADER_SIZE];
2144 const size_t first_partition_size = read_uncompressed_header(pbi,
2145 init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
2146 const int tile_rows = 1 << cm->log2_tile_rows;
2147 const int tile_cols = 1 << cm->log2_tile_cols;
2148 YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
2149 xd->cur_buf = new_fb;
2151 if (!first_partition_size) {
2152 // showing a frame directly
2153 *p_data_end = data + (cm->profile <= PROFILE_2 ? 1 : 2);
2157 data += vpx_rb_bytes_read(&rb);
2158 if (!read_is_valid(data, first_partition_size, data_end))
2159 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2160 "Truncated packet or corrupt header length");
2162 cm->use_prev_frame_mvs = !cm->error_resilient_mode &&
2163 cm->width == cm->last_width &&
2164 cm->height == cm->last_height &&
2165 !cm->last_intra_only &&
2166 cm->last_show_frame &&
2167 (cm->last_frame_type != KEY_FRAME);
2169 vp10_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
2171 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
2172 if (!cm->fc->initialized)
2173 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2174 "Uninitialized entropy context.");
2176 vp10_zero(cm->counts);
2179 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
2180 if (new_fb->corrupted)
2181 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2182 "Decode failed. Frame data header is corrupted.");
2184 if (cm->lf.filter_level && !cm->skip_loop_filter) {
2185 vp10_loop_filter_frame_init(cm, cm->lf.filter_level);
2188 // If encoded in frame parallel mode, frame context is ready after decoding
2189 // the frame header.
2190 if (cm->frame_parallel_decode && cm->frame_parallel_decoding_mode) {
2191 VPxWorker *const worker = pbi->frame_worker_owner;
2192 FrameWorkerData *const frame_worker_data = worker->data1;
2193 if (cm->refresh_frame_context) {
2194 context_updated = 1;
2195 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
2197 vp10_frameworker_lock_stats(worker);
2198 pbi->cur_buf->row = -1;
2199 pbi->cur_buf->col = -1;
2200 frame_worker_data->frame_context_ready = 1;
2201 // Signal the main thread that context is ready.
2202 vp10_frameworker_signal_stats(worker);
2203 vp10_frameworker_unlock_stats(worker);
2206 if (pbi->max_threads > 1 && tile_rows == 1 && tile_cols > 1) {
2207 // Multi-threaded tile decoder
2208 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
2209 if (!xd->corrupted) {
2210 if (!cm->skip_loop_filter) {
2211 // If multiple threads are used to decode tiles, then we use those
2212 // threads to do parallel loopfiltering.
2213 vp10_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane,
2214 cm->lf.filter_level, 0, 0, pbi->tile_workers,
2215 pbi->num_tile_workers, &pbi->lf_row_sync);
2218 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2219 "Decode failed. Frame data is corrupted.");
2223 *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
2226 if (!xd->corrupted) {
2227 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
2228 vp10_adapt_coef_probs(cm);
2230 if (!frame_is_intra_only(cm)) {
2231 vp10_adapt_mode_probs(cm);
2232 vp10_adapt_mv_probs(cm, cm->allow_high_precision_mv);
2235 debug_check_frame_counts(cm);
2238 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2239 "Decode failed. Frame data is corrupted.");
2242 // Non frame parallel update frame context here.
2243 if (cm->refresh_frame_context && !context_updated)
2244 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;