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 "vpx_config.h"
14 #include "onyxd_int.h"
15 #include "vp8/common/header.h"
16 #include "vp8/common/reconintra4x4.h"
17 #include "vp8/common/reconinter.h"
18 #include "detokenize.h"
19 #include "vp8/common/invtrans.h"
20 #include "vp8/common/alloccommon.h"
21 #include "vp8/common/entropymode.h"
22 #include "vp8/common/quant_common.h"
23 #include "vpx_scale/vpxscale.h"
24 #include "vp8/common/setupintrarecon.h"
27 #include "vp8/common/extend.h"
28 #if CONFIG_ERROR_CONCEALMENT
29 #include "error_concealment.h"
31 #include "vpx_mem/vpx_mem.h"
32 #include "vp8/common/threading.h"
33 #include "decoderthreading.h"
34 #include "dboolhuff.h"
39 void vp8cx_init_de_quantizer(VP8D_COMP *pbi)
42 VP8_COMMON *const pc = & pbi->common;
44 for (Q = 0; Q < QINDEX_RANGE; Q++)
46 pc->Y1dequant[Q][0] = (short)vp8_dc_quant(Q, pc->y1dc_delta_q);
47 pc->Y2dequant[Q][0] = (short)vp8_dc2quant(Q, pc->y2dc_delta_q);
48 pc->UVdequant[Q][0] = (short)vp8_dc_uv_quant(Q, pc->uvdc_delta_q);
50 pc->Y1dequant[Q][1] = (short)vp8_ac_yquant(Q);
51 pc->Y2dequant[Q][1] = (short)vp8_ac2quant(Q, pc->y2ac_delta_q);
52 pc->UVdequant[Q][1] = (short)vp8_ac_uv_quant(Q, pc->uvac_delta_q);
56 void vp8_mb_init_dequantizer(VP8D_COMP *pbi, MACROBLOCKD *xd)
60 MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
61 VP8_COMMON *const pc = & pbi->common;
63 /* Decide whether to use the default or alternate baseline Q value. */
64 if (xd->segmentation_enabled)
67 if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA)
68 QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
73 QIndex = pc->base_qindex + xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
74 QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0; /* Clamp to valid range */
78 QIndex = pc->base_qindex;
80 /* Set up the macroblock dequant constants */
81 xd->dequant_y1_dc[0] = 1;
82 xd->dequant_y1[0] = pc->Y1dequant[QIndex][0];
83 xd->dequant_y2[0] = pc->Y2dequant[QIndex][0];
84 xd->dequant_uv[0] = pc->UVdequant[QIndex][0];
86 for (i = 1; i < 16; i++)
88 xd->dequant_y1_dc[i] =
89 xd->dequant_y1[i] = pc->Y1dequant[QIndex][1];
90 xd->dequant_y2[i] = pc->Y2dequant[QIndex][1];
91 xd->dequant_uv[i] = pc->UVdequant[QIndex][1];
95 static void decode_macroblock(VP8D_COMP *pbi, MACROBLOCKD *xd,
98 MB_PREDICTION_MODE mode;
100 #if CONFIG_ERROR_CONCEALMENT
101 int corruption_detected = 0;
104 if (xd->mode_info_context->mbmi.mb_skip_coeff)
106 vp8_reset_mb_tokens_context(xd);
108 else if (!vp8dx_bool_error(xd->current_bc))
111 eobtotal = vp8_decode_mb_tokens(pbi, xd);
113 /* Special case: Force the loopfilter to skip when eobtotal is zero */
114 xd->mode_info_context->mbmi.mb_skip_coeff = (eobtotal==0);
117 mode = xd->mode_info_context->mbmi.mode;
119 if (xd->segmentation_enabled)
120 vp8_mb_init_dequantizer(pbi, xd);
123 #if CONFIG_ERROR_CONCEALMENT
128 /* When we have independent partitions we can apply residual even
129 * though other partitions within the frame are corrupt.
131 throw_residual = (!pbi->independent_partitions &&
132 pbi->frame_corrupt_residual);
133 throw_residual = (throw_residual || vp8dx_bool_error(xd->current_bc));
135 if ((mb_idx >= pbi->mvs_corrupt_from_mb || throw_residual))
137 /* MB with corrupt residuals or corrupt mode/motion vectors.
138 * Better to use the predictor as reconstruction.
140 pbi->frame_corrupt_residual = 1;
141 vpx_memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
142 vp8_conceal_corrupt_mb(xd);
145 corruption_detected = 1;
147 /* force idct to be skipped for B_PRED and use the
148 * prediction only for reconstruction
150 vpx_memset(xd->eobs, 0, 25);
156 if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
158 vp8_build_intra_predictors_mbuv_s(xd,
163 xd->recon_left_stride[1],
164 xd->dst.u_buffer, xd->dst.v_buffer,
169 vp8_build_intra_predictors_mby_s(xd,
172 xd->recon_left_stride[0],
178 short *DQC = xd->dequant_y1;
179 int dst_stride = xd->dst.y_stride;
180 unsigned char *base_dst = xd->dst.y_buffer;
182 /* clear out residual eob info */
183 if(xd->mode_info_context->mbmi.mb_skip_coeff)
184 vpx_memset(xd->eobs, 0, 25);
186 intra_prediction_down_copy(xd, xd->recon_above[0] + 16);
188 for (i = 0; i < 16; i++)
190 BLOCKD *b = &xd->block[i];
191 int b_mode = xd->mode_info_context->bmi[i].as_mode;
192 unsigned char *yabove;
193 unsigned char *yleft;
195 unsigned char top_left;
197 yabove = base_dst + b->offset - dst_stride;
198 yleft = base_dst + b->offset - 1;
199 left_stride = dst_stride;
200 top_left = yabove[-1];
202 // vp8_intra4x4_predict (base_dst + b->offset, dst_stride, b_mode,
203 // base_dst + b->offset, dst_stride );
204 vp8_intra4x4_predict_d_c(yabove, yleft, left_stride,
206 base_dst + b->offset, dst_stride,
215 base_dst + b->offset, dst_stride);
220 (b->qcoeff[0] * DQC[0],
221 base_dst + b->offset, dst_stride,
222 base_dst + b->offset, dst_stride);
223 ((int *)b->qcoeff)[0] = 0;
231 vp8_build_inter_predictors_mb(xd);
235 #if CONFIG_ERROR_CONCEALMENT
236 if (corruption_detected)
242 if(!xd->mode_info_context->mbmi.mb_skip_coeff)
244 /* dequantization and idct */
247 short *DQC = xd->dequant_y1;
251 BLOCKD *b = &xd->block[24];
253 /* do 2nd order transform on the dc block */
254 if (xd->eobs[24] > 1)
256 vp8_dequantize_b(b, xd->dequant_y2);
258 vp8_short_inv_walsh4x4(&b->dqcoeff[0],
260 ((int *)b->qcoeff)[0] = 0;
261 ((int *)b->qcoeff)[1] = 0;
262 ((int *)b->qcoeff)[2] = 0;
263 ((int *)b->qcoeff)[3] = 0;
264 ((int *)b->qcoeff)[4] = 0;
265 ((int *)b->qcoeff)[5] = 0;
266 ((int *)b->qcoeff)[6] = 0;
267 ((int *)b->qcoeff)[7] = 0;
271 b->dqcoeff[0] = b->qcoeff[0] * xd->dequant_y2[0];
272 vp8_short_inv_walsh4x4_1(&b->dqcoeff[0],
274 ((int *)b->qcoeff)[0] = 0;
277 /* override the dc dequant constant in order to preserve the
280 DQC = xd->dequant_y1_dc;
283 vp8_dequant_idct_add_y_block
286 xd->dst.y_stride, xd->eobs);
289 vp8_dequant_idct_add_uv_block
290 (xd->qcoeff+16*16, xd->dequant_uv,
291 xd->dst.u_buffer, xd->dst.v_buffer,
292 xd->dst.uv_stride, xd->eobs+16);
296 static int get_delta_q(vp8_reader *bc, int prev, int *q_update)
300 if (vp8_read_bit(bc))
302 ret_val = vp8_read_literal(bc, 4);
304 if (vp8_read_bit(bc))
308 /* Trigger a quantizer update if the delta-q value has changed */
315 #ifdef PACKET_TESTING
320 static void decode_mb_rows(VP8D_COMP *pbi)
322 VP8_COMMON *const pc = & pbi->common;
323 MACROBLOCKD *const xd = & pbi->mb;
326 int num_part = 1 << pc->multi_token_partition;
328 int recon_yoffset, recon_uvoffset;
331 int dst_fb_idx = pc->new_fb_idx;
332 int recon_y_stride = pc->yv12_fb[dst_fb_idx].y_stride;
333 int recon_uv_stride = pc->yv12_fb[dst_fb_idx].uv_stride;
335 unsigned char *ref_buffer[MAX_REF_FRAMES][3];
336 unsigned char *dst_buffer[3];
338 int ref_fb_index[MAX_REF_FRAMES];
339 int ref_fb_corrupted[MAX_REF_FRAMES];
341 ref_fb_corrupted[INTRA_FRAME] = 0;
343 ref_fb_index[LAST_FRAME] = pc->lst_fb_idx;
344 ref_fb_index[GOLDEN_FRAME] = pc->gld_fb_idx;
345 ref_fb_index[ALTREF_FRAME] = pc->alt_fb_idx;
347 for(i = 1; i < MAX_REF_FRAMES; i++)
349 ref_buffer[i][0] = pc->yv12_fb[ref_fb_index[i]].y_buffer;
350 ref_buffer[i][1] = pc->yv12_fb[ref_fb_index[i]].u_buffer;
351 ref_buffer[i][2] = pc->yv12_fb[ref_fb_index[i]].v_buffer;
353 ref_fb_corrupted[i] = pc->yv12_fb[ref_fb_index[i]].corrupted;
356 dst_buffer[0] = pc->yv12_fb[dst_fb_idx].y_buffer;
357 dst_buffer[1] = pc->yv12_fb[dst_fb_idx].u_buffer;
358 dst_buffer[2] = pc->yv12_fb[dst_fb_idx].v_buffer;
360 xd->up_available = 0;
362 /* Decode the individual macro block */
363 for (mb_row = 0; mb_row < pc->mb_rows; mb_row++)
367 xd->current_bc = & pbi->mbc[ibc];
374 recon_yoffset = mb_row * recon_y_stride * 16;
375 recon_uvoffset = mb_row * recon_uv_stride * 8;
378 xd->above_context = pc->above_context;
379 vpx_memset(xd->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
381 xd->left_available = 0;
383 xd->mb_to_top_edge = -((mb_row * 16)) << 3;
384 xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
386 xd->recon_above[0] = dst_buffer[0] + recon_yoffset;
387 xd->recon_above[1] = dst_buffer[1] + recon_uvoffset;
388 xd->recon_above[2] = dst_buffer[2] + recon_uvoffset;
390 xd->recon_left[0] = xd->recon_above[0] - 1;
391 xd->recon_left[1] = xd->recon_above[1] - 1;
392 xd->recon_left[2] = xd->recon_above[2] - 1;
394 xd->recon_above[0] -= xd->dst.y_stride;
395 xd->recon_above[1] -= xd->dst.uv_stride;
396 xd->recon_above[2] -= xd->dst.uv_stride;
398 //TODO: move to outside row loop
399 xd->recon_left_stride[0] = xd->dst.y_stride;
400 xd->recon_left_stride[1] = xd->dst.uv_stride;
402 for (mb_col = 0; mb_col < pc->mb_cols; mb_col++)
404 /* Distance of Mb to the various image edges.
405 * These are specified to 8th pel as they are always compared to values
406 * that are in 1/8th pel units
408 xd->mb_to_left_edge = -((mb_col * 16) << 3);
409 xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
411 #if CONFIG_ERROR_CONCEALMENT
413 int corrupt_residual = (!pbi->independent_partitions &&
414 pbi->frame_corrupt_residual) ||
415 vp8dx_bool_error(xd->current_bc);
416 if (pbi->ec_active &&
417 xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME &&
420 /* We have an intra block with corrupt coefficients, better to
421 * conceal with an inter block. Interpolate MVs from neighboring
424 * Note that for the first mb with corrupt residual in a frame,
425 * we might not discover that before decoding the residual. That
426 * happens after this check, and therefore no inter concealment
429 vp8_interpolate_motion(xd,
431 pc->mb_rows, pc->mb_cols,
432 pc->mode_info_stride);
437 xd->dst.y_buffer = dst_buffer[0] + recon_yoffset;
438 xd->dst.u_buffer = dst_buffer[1] + recon_uvoffset;
439 xd->dst.v_buffer = dst_buffer[2] + recon_uvoffset;
441 xd->pre.y_buffer = ref_buffer[xd->mode_info_context->mbmi.ref_frame][0] + recon_yoffset;
442 xd->pre.u_buffer = ref_buffer[xd->mode_info_context->mbmi.ref_frame][1] + recon_uvoffset;
443 xd->pre.v_buffer = ref_buffer[xd->mode_info_context->mbmi.ref_frame][2] + recon_uvoffset;
445 /* propagate errors from reference frames */
446 xd->corrupted |= ref_fb_corrupted[xd->mode_info_context->mbmi.ref_frame];
448 decode_macroblock(pbi, xd, mb_idx);
451 xd->left_available = 1;
453 /* check if the boolean decoder has suffered an error */
454 xd->corrupted |= vp8dx_bool_error(xd->current_bc);
456 xd->recon_above[0] += 16;
457 xd->recon_above[1] += 8;
458 xd->recon_above[2] += 8;
459 xd->recon_left[0] += 16;
460 xd->recon_left[1] += 8;
461 xd->recon_left[2] += 8;
467 ++xd->mode_info_context; /* next mb */
473 /* adjust to the next row of mbs */
475 &pc->yv12_fb[dst_fb_idx],
476 xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8
479 ++xd->mode_info_context; /* skip prediction column */
480 xd->up_available = 1;
485 static unsigned int read_partition_size(const unsigned char *cx_size)
487 const unsigned int size =
488 cx_size[0] + (cx_size[1] << 8) + (cx_size[2] << 16);
492 static int read_is_valid(const unsigned char *start,
494 const unsigned char *end)
496 return (start + len > start && start + len <= end);
499 static unsigned int read_available_partition_size(
501 const unsigned char *token_part_sizes,
502 const unsigned char *fragment_start,
503 const unsigned char *first_fragment_end,
504 const unsigned char *fragment_end,
508 VP8_COMMON* pc = &pbi->common;
509 const unsigned char *partition_size_ptr = token_part_sizes + i * 3;
510 unsigned int partition_size = 0;
511 ptrdiff_t bytes_left = fragment_end - fragment_start;
512 /* Calculate the length of this partition. The last partition
513 * size is implicit. If the partition size can't be read, then
514 * either use the remaining data in the buffer (for EC mode)
517 if (i < num_part - 1)
519 if (read_is_valid(partition_size_ptr, 3, first_fragment_end))
520 partition_size = read_partition_size(partition_size_ptr);
521 else if (pbi->ec_active)
522 partition_size = bytes_left;
524 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
525 "Truncated partition size data");
528 partition_size = bytes_left;
530 /* Validate the calculated partition length. If the buffer
531 * described by the partition can't be fully read, then restrict
532 * it to the portion that can be (for EC mode) or throw an error.
534 if (!read_is_valid(fragment_start, partition_size, fragment_end))
537 partition_size = bytes_left;
539 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
540 "Truncated packet or corrupt partition "
543 return partition_size;
547 static void setup_token_decoder(VP8D_COMP *pbi,
548 const unsigned char* token_part_sizes)
550 vp8_reader *bool_decoder = &pbi->bc2;
551 unsigned int partition_idx;
553 int num_token_partitions;
554 const unsigned char *first_fragment_end = pbi->fragments[0] +
555 pbi->fragment_sizes[0];
557 TOKEN_PARTITION multi_token_partition =
558 (TOKEN_PARTITION)vp8_read_literal(&pbi->bc, 2);
559 if (!vp8dx_bool_error(&pbi->bc))
560 pbi->common.multi_token_partition = multi_token_partition;
561 num_token_partitions = 1 << pbi->common.multi_token_partition;
562 if (num_token_partitions > 1)
564 CHECK_MEM_ERROR(pbi->mbc, vpx_malloc(num_token_partitions *
565 sizeof(vp8_reader)));
566 bool_decoder = pbi->mbc;
569 /* Check for partitions within the fragments and unpack the fragments
570 * so that each fragment pointer points to its corresponding partition. */
571 for (fragment_idx = 0; fragment_idx < pbi->num_fragments; ++fragment_idx)
573 unsigned int fragment_size = pbi->fragment_sizes[fragment_idx];
574 const unsigned char *fragment_end = pbi->fragments[fragment_idx] +
576 /* Special case for handling the first partition since we have already
578 if (fragment_idx == 0)
580 /* Size of first partition + token partition sizes element */
581 ptrdiff_t ext_first_part_size = token_part_sizes -
582 pbi->fragments[0] + 3 * (num_token_partitions - 1);
583 fragment_size -= ext_first_part_size;
584 if (fragment_size > 0)
586 pbi->fragment_sizes[0] = ext_first_part_size;
587 /* The fragment contains an additional partition. Move to
590 pbi->fragments[fragment_idx] = pbi->fragments[0] +
591 pbi->fragment_sizes[0];
594 /* Split the chunk into partitions read from the bitstream */
595 while (fragment_size > 0)
597 ptrdiff_t partition_size = read_available_partition_size(
600 pbi->fragments[fragment_idx],
604 num_token_partitions);
605 pbi->fragment_sizes[fragment_idx] = partition_size;
606 fragment_size -= partition_size;
607 assert(fragment_idx <= num_token_partitions);
608 if (fragment_size > 0)
610 /* The fragment contains an additional partition.
613 pbi->fragments[fragment_idx] =
614 pbi->fragments[fragment_idx - 1] + partition_size;
619 pbi->num_fragments = num_token_partitions + 1;
621 for (partition_idx = 1; partition_idx < pbi->num_fragments; ++partition_idx)
623 if (vp8dx_start_decode(bool_decoder,
624 pbi->fragments[partition_idx],
625 pbi->fragment_sizes[partition_idx]))
626 vpx_internal_error(&pbi->common.error, VPX_CODEC_MEM_ERROR,
627 "Failed to allocate bool decoder %d",
633 #if CONFIG_MULTITHREAD
634 /* Clamp number of decoder threads */
635 if (pbi->decoding_thread_count > num_token_partitions - 1)
636 pbi->decoding_thread_count = num_token_partitions - 1;
640 static void stop_token_decoder(VP8D_COMP *pbi)
642 VP8_COMMON *pc = &pbi->common;
644 if (pc->multi_token_partition != ONE_PARTITION)
651 static void init_frame(VP8D_COMP *pbi)
653 VP8_COMMON *const pc = & pbi->common;
654 MACROBLOCKD *const xd = & pbi->mb;
656 if (pc->frame_type == KEY_FRAME)
658 /* Various keyframe initializations */
659 vpx_memcpy(pc->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
661 vp8_init_mbmode_probs(pc);
663 vp8_default_coef_probs(pc);
664 vp8_kf_default_bmode_probs(pc->kf_bmode_prob);
666 /* reset the segment feature data to 0 with delta coding (Default state). */
667 vpx_memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
668 xd->mb_segement_abs_delta = SEGMENT_DELTADATA;
670 /* reset the mode ref deltasa for loop filter */
671 vpx_memset(xd->ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
672 vpx_memset(xd->mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
674 /* All buffers are implicitly updated on key frames. */
675 pc->refresh_golden_frame = 1;
676 pc->refresh_alt_ref_frame = 1;
677 pc->copy_buffer_to_gf = 0;
678 pc->copy_buffer_to_arf = 0;
680 /* Note that Golden and Altref modes cannot be used on a key frame so
681 * ref_frame_sign_bias[] is undefined and meaningless
683 pc->ref_frame_sign_bias[GOLDEN_FRAME] = 0;
684 pc->ref_frame_sign_bias[ALTREF_FRAME] = 0;
688 if (!pc->use_bilinear_mc_filter)
689 pc->mcomp_filter_type = SIXTAP;
691 pc->mcomp_filter_type = BILINEAR;
693 /* To enable choice of different interploation filters */
694 if (pc->mcomp_filter_type == SIXTAP)
696 xd->subpixel_predict = vp8_sixtap_predict4x4;
697 xd->subpixel_predict8x4 = vp8_sixtap_predict8x4;
698 xd->subpixel_predict8x8 = vp8_sixtap_predict8x8;
699 xd->subpixel_predict16x16 = vp8_sixtap_predict16x16;
703 xd->subpixel_predict = vp8_bilinear_predict4x4;
704 xd->subpixel_predict8x4 = vp8_bilinear_predict8x4;
705 xd->subpixel_predict8x8 = vp8_bilinear_predict8x8;
706 xd->subpixel_predict16x16 = vp8_bilinear_predict16x16;
709 if (pbi->decoded_key_frame && pbi->ec_enabled && !pbi->ec_active)
713 xd->left_context = &pc->left_context;
714 xd->mode_info_context = pc->mi;
715 xd->frame_type = pc->frame_type;
716 xd->mode_info_context->mbmi.mode = DC_PRED;
717 xd->mode_info_stride = pc->mode_info_stride;
718 xd->corrupted = 0; /* init without corruption */
720 xd->fullpixel_mask = 0xffffffff;
722 xd->fullpixel_mask = 0xfffffff8;
726 int vp8_decode_frame(VP8D_COMP *pbi)
728 vp8_reader *const bc = & pbi->bc;
729 VP8_COMMON *const pc = & pbi->common;
730 MACROBLOCKD *const xd = & pbi->mb;
731 const unsigned char *data = pbi->fragments[0];
732 const unsigned char *data_end = data + pbi->fragment_sizes[0];
733 ptrdiff_t first_partition_length_in_bytes;
736 const int *const mb_feature_data_bits = vp8_mb_feature_data_bits;
737 int corrupt_tokens = 0;
738 int prev_independent_partitions = pbi->independent_partitions;
740 /* start with no corruption of current frame */
742 pc->yv12_fb[pc->new_fb_idx].corrupted = 0;
744 if (data_end - data < 3)
748 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
752 /* Declare the missing frame as an inter frame since it will
753 be handled as an inter frame when we have estimated its
755 pc->frame_type = INTER_FRAME;
758 first_partition_length_in_bytes = 0;
762 pc->frame_type = (FRAME_TYPE)(data[0] & 1);
763 pc->version = (data[0] >> 1) & 7;
764 pc->show_frame = (data[0] >> 4) & 1;
765 first_partition_length_in_bytes =
766 (data[0] | (data[1] << 8) | (data[2] << 16)) >> 5;
768 if (!pbi->ec_active && (data + first_partition_length_in_bytes > data_end
769 || data + first_partition_length_in_bytes < data))
770 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
771 "Truncated packet or corrupt partition 0 length");
775 vp8_setup_version(pc);
777 if (pc->frame_type == KEY_FRAME)
779 const int Width = pc->Width;
780 const int Height = pc->Height;
782 /* vet via sync code */
783 /* When error concealment is enabled we should only check the sync
784 * code if we have enough bits available
786 if (!pbi->ec_active || data + 3 < data_end)
788 if (data[0] != 0x9d || data[1] != 0x01 || data[2] != 0x2a)
789 vpx_internal_error(&pc->error, VPX_CODEC_UNSUP_BITSTREAM,
790 "Invalid frame sync code");
793 /* If error concealment is enabled we should only parse the new size
794 * if we have enough data. Otherwise we will end up with the wrong
797 if (!pbi->ec_active || data + 6 < data_end)
799 pc->Width = (data[3] | (data[4] << 8)) & 0x3fff;
800 pc->horiz_scale = data[4] >> 6;
801 pc->Height = (data[5] | (data[6] << 8)) & 0x3fff;
802 pc->vert_scale = data[6] >> 6;
806 if (Width != pc->Width || Height != pc->Height)
808 int prev_mb_rows = pc->mb_rows;
813 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
814 "Invalid frame width");
820 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
821 "Invalid frame height");
824 if (vp8_alloc_frame_buffers(pc, pc->Width, pc->Height))
825 vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
826 "Failed to allocate frame buffers");
828 #if CONFIG_ERROR_CONCEALMENT
829 pbi->overlaps = NULL;
832 if (vp8_alloc_overlap_lists(pbi))
833 vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
834 "Failed to allocate overlap lists "
835 "for error concealment");
839 #if CONFIG_MULTITHREAD
840 if (pbi->b_multithreaded_rd)
841 vp8mt_alloc_temp_buffers(pbi, pc->Width, prev_mb_rows);
847 if ((!pbi->decoded_key_frame && pc->frame_type != KEY_FRAME) ||
848 pc->Width == 0 || pc->Height == 0)
855 if (vp8dx_start_decode(bc, data, data_end - data))
856 vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
857 "Failed to allocate bool decoder 0");
858 if (pc->frame_type == KEY_FRAME) {
859 pc->clr_type = (YUV_TYPE)vp8_read_bit(bc);
860 pc->clamp_type = (CLAMP_TYPE)vp8_read_bit(bc);
863 /* Is segmentation enabled */
864 xd->segmentation_enabled = (unsigned char)vp8_read_bit(bc);
866 if (xd->segmentation_enabled)
868 /* Signal whether or not the segmentation map is being explicitly updated this frame. */
869 xd->update_mb_segmentation_map = (unsigned char)vp8_read_bit(bc);
870 xd->update_mb_segmentation_data = (unsigned char)vp8_read_bit(bc);
872 if (xd->update_mb_segmentation_data)
874 xd->mb_segement_abs_delta = (unsigned char)vp8_read_bit(bc);
876 vpx_memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
878 /* For each segmentation feature (Quant and loop filter level) */
879 for (i = 0; i < MB_LVL_MAX; i++)
881 for (j = 0; j < MAX_MB_SEGMENTS; j++)
883 /* Frame level data */
884 if (vp8_read_bit(bc))
886 xd->segment_feature_data[i][j] = (signed char)vp8_read_literal(bc, mb_feature_data_bits[i]);
888 if (vp8_read_bit(bc))
889 xd->segment_feature_data[i][j] = -xd->segment_feature_data[i][j];
892 xd->segment_feature_data[i][j] = 0;
897 if (xd->update_mb_segmentation_map)
899 /* Which macro block level features are enabled */
900 vpx_memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));
902 /* Read the probs used to decode the segment id for each macro block. */
903 for (i = 0; i < MB_FEATURE_TREE_PROBS; i++)
905 /* If not explicitly set value is defaulted to 255 by memset above */
906 if (vp8_read_bit(bc))
907 xd->mb_segment_tree_probs[i] = (vp8_prob)vp8_read_literal(bc, 8);
913 /* No segmentation updates on this frame */
914 xd->update_mb_segmentation_map = 0;
915 xd->update_mb_segmentation_data = 0;
918 /* Read the loop filter level and type */
919 pc->filter_type = (LOOPFILTERTYPE) vp8_read_bit(bc);
920 pc->filter_level = vp8_read_literal(bc, 6);
921 pc->sharpness_level = vp8_read_literal(bc, 3);
923 /* Read in loop filter deltas applied at the MB level based on mode or ref frame. */
924 xd->mode_ref_lf_delta_update = 0;
925 xd->mode_ref_lf_delta_enabled = (unsigned char)vp8_read_bit(bc);
927 if (xd->mode_ref_lf_delta_enabled)
929 /* Do the deltas need to be updated */
930 xd->mode_ref_lf_delta_update = (unsigned char)vp8_read_bit(bc);
932 if (xd->mode_ref_lf_delta_update)
935 for (i = 0; i < MAX_REF_LF_DELTAS; i++)
937 if (vp8_read_bit(bc))
939 /*sign = vp8_read_bit( bc );*/
940 xd->ref_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
942 if (vp8_read_bit(bc)) /* Apply sign */
943 xd->ref_lf_deltas[i] = xd->ref_lf_deltas[i] * -1;
948 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
950 if (vp8_read_bit(bc))
952 /*sign = vp8_read_bit( bc );*/
953 xd->mode_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
955 if (vp8_read_bit(bc)) /* Apply sign */
956 xd->mode_lf_deltas[i] = xd->mode_lf_deltas[i] * -1;
962 setup_token_decoder(pbi, data + first_partition_length_in_bytes);
964 xd->current_bc = &pbi->bc2;
966 /* Read the default quantizers. */
970 Q = vp8_read_literal(bc, 7); /* AC 1st order Q = default */
973 pc->y1dc_delta_q = get_delta_q(bc, pc->y1dc_delta_q, &q_update);
974 pc->y2dc_delta_q = get_delta_q(bc, pc->y2dc_delta_q, &q_update);
975 pc->y2ac_delta_q = get_delta_q(bc, pc->y2ac_delta_q, &q_update);
976 pc->uvdc_delta_q = get_delta_q(bc, pc->uvdc_delta_q, &q_update);
977 pc->uvac_delta_q = get_delta_q(bc, pc->uvac_delta_q, &q_update);
980 vp8cx_init_de_quantizer(pbi);
982 /* MB level dequantizer setup */
983 vp8_mb_init_dequantizer(pbi, &pbi->mb);
986 /* Determine if the golden frame or ARF buffer should be updated and how.
987 * For all non key frames the GF and ARF refresh flags and sign bias
988 * flags must be set explicitly.
990 if (pc->frame_type != KEY_FRAME)
992 /* Should the GF or ARF be updated from the current frame */
993 pc->refresh_golden_frame = vp8_read_bit(bc);
994 #if CONFIG_ERROR_CONCEALMENT
995 /* Assume we shouldn't refresh golden if the bit is missing */
996 xd->corrupted |= vp8dx_bool_error(bc);
997 if (pbi->ec_active && xd->corrupted)
998 pc->refresh_golden_frame = 0;
1001 pc->refresh_alt_ref_frame = vp8_read_bit(bc);
1002 #if CONFIG_ERROR_CONCEALMENT
1003 /* Assume we shouldn't refresh altref if the bit is missing */
1004 xd->corrupted |= vp8dx_bool_error(bc);
1005 if (pbi->ec_active && xd->corrupted)
1006 pc->refresh_alt_ref_frame = 0;
1009 /* Buffer to buffer copy flags. */
1010 pc->copy_buffer_to_gf = 0;
1012 if (!pc->refresh_golden_frame)
1013 pc->copy_buffer_to_gf = vp8_read_literal(bc, 2);
1015 #if CONFIG_ERROR_CONCEALMENT
1016 /* Assume we shouldn't copy to the golden if the bit is missing */
1017 xd->corrupted |= vp8dx_bool_error(bc);
1018 if (pbi->ec_active && xd->corrupted)
1019 pc->copy_buffer_to_gf = 0;
1022 pc->copy_buffer_to_arf = 0;
1024 if (!pc->refresh_alt_ref_frame)
1025 pc->copy_buffer_to_arf = vp8_read_literal(bc, 2);
1027 #if CONFIG_ERROR_CONCEALMENT
1028 /* Assume we shouldn't copy to the alt-ref if the bit is missing */
1029 xd->corrupted |= vp8dx_bool_error(bc);
1030 if (pbi->ec_active && xd->corrupted)
1031 pc->copy_buffer_to_arf = 0;
1035 pc->ref_frame_sign_bias[GOLDEN_FRAME] = vp8_read_bit(bc);
1036 pc->ref_frame_sign_bias[ALTREF_FRAME] = vp8_read_bit(bc);
1039 pc->refresh_entropy_probs = vp8_read_bit(bc);
1040 #if CONFIG_ERROR_CONCEALMENT
1041 /* Assume we shouldn't refresh the probabilities if the bit is
1043 xd->corrupted |= vp8dx_bool_error(bc);
1044 if (pbi->ec_active && xd->corrupted)
1045 pc->refresh_entropy_probs = 0;
1047 if (pc->refresh_entropy_probs == 0)
1049 vpx_memcpy(&pc->lfc, &pc->fc, sizeof(pc->fc));
1052 pc->refresh_last_frame = pc->frame_type == KEY_FRAME || vp8_read_bit(bc);
1054 #if CONFIG_ERROR_CONCEALMENT
1055 /* Assume we should refresh the last frame if the bit is missing */
1056 xd->corrupted |= vp8dx_bool_error(bc);
1057 if (pbi->ec_active && xd->corrupted)
1058 pc->refresh_last_frame = 1;
1063 FILE *z = fopen("decodestats.stt", "a");
1064 fprintf(z, "%6d F:%d,G:%d,A:%d,L:%d,Q:%d\n",
1065 pc->current_video_frame,
1067 pc->refresh_golden_frame,
1068 pc->refresh_alt_ref_frame,
1069 pc->refresh_last_frame,
1075 pbi->independent_partitions = 1;
1077 /* read coef probability tree */
1078 for (i = 0; i < BLOCK_TYPES; i++)
1079 for (j = 0; j < COEF_BANDS; j++)
1080 for (k = 0; k < PREV_COEF_CONTEXTS; k++)
1081 for (l = 0; l < ENTROPY_NODES; l++)
1084 vp8_prob *const p = pc->fc.coef_probs [i][j][k] + l;
1086 if (vp8_read(bc, vp8_coef_update_probs [i][j][k][l]))
1088 *p = (vp8_prob)vp8_read_literal(bc, 8);
1091 if (k > 0 && *p != pc->fc.coef_probs[i][j][k-1][l])
1092 pbi->independent_partitions = 0;
1097 vpx_memcpy(&xd->pre, &pc->yv12_fb[pc->lst_fb_idx], sizeof(YV12_BUFFER_CONFIG));
1098 vpx_memcpy(&xd->dst, &pc->yv12_fb[pc->new_fb_idx], sizeof(YV12_BUFFER_CONFIG));
1100 /* set up frame new frame for intra coded blocks */
1101 #if CONFIG_MULTITHREAD
1102 if (!(pbi->b_multithreaded_rd) || pc->multi_token_partition == ONE_PARTITION || !(pc->filter_level))
1104 vp8_setup_intra_recon(&pc->yv12_fb[pc->new_fb_idx]);
1106 vp8_setup_block_dptrs(xd);
1108 vp8_build_block_doffsets(xd);
1110 /* clear out the coeff buffer */
1111 vpx_memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
1113 /* Read the mb_no_coeff_skip flag */
1114 pc->mb_no_coeff_skip = (int)vp8_read_bit(bc);
1117 vp8_decode_mode_mvs(pbi);
1119 #if CONFIG_ERROR_CONCEALMENT
1120 if (pbi->ec_active &&
1121 pbi->mvs_corrupt_from_mb < (unsigned int)pc->mb_cols * pc->mb_rows)
1123 /* Motion vectors are missing in this frame. We will try to estimate
1124 * them and then continue decoding the frame as usual */
1125 vp8_estimate_missing_mvs(pbi);
1129 vpx_memset(pc->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) * pc->mb_cols);
1130 pbi->frame_corrupt_residual = 0;
1132 #if CONFIG_MULTITHREAD
1133 if (pbi->b_multithreaded_rd && pc->multi_token_partition != ONE_PARTITION)
1136 vp8mt_decode_mb_rows(pbi, xd);
1137 vp8_yv12_extend_frame_borders(&pc->yv12_fb[pc->new_fb_idx]); /*cm->frame_to_show);*/
1138 for (i = 0; i < pbi->decoding_thread_count; ++i)
1139 corrupt_tokens |= pbi->mb_row_di[i].mbd.corrupted;
1144 decode_mb_rows(pbi);
1145 corrupt_tokens |= xd->corrupted;
1148 stop_token_decoder(pbi);
1150 /* Collect information about decoder corruption. */
1151 /* 1. Check first boolean decoder for errors. */
1152 pc->yv12_fb[pc->new_fb_idx].corrupted = vp8dx_bool_error(bc);
1153 /* 2. Check the macroblock information */
1154 pc->yv12_fb[pc->new_fb_idx].corrupted |= corrupt_tokens;
1156 if (!pbi->decoded_key_frame)
1158 if (pc->frame_type == KEY_FRAME &&
1159 !pc->yv12_fb[pc->new_fb_idx].corrupted)
1160 pbi->decoded_key_frame = 1;
1162 vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME,
1163 "A stream must start with a complete key frame");
1166 /* vpx_log("Decoder: Frame Decoded, Size Roughly:%d bytes \n",bc->pos+pbi->bc2.pos); */
1168 /* If this was a kf or Gf note the Q used */
1169 if ((pc->frame_type == KEY_FRAME) ||
1170 pc->refresh_golden_frame || pc->refresh_alt_ref_frame)
1172 pc->last_kf_gf_q = pc->base_qindex;
1175 if (pc->refresh_entropy_probs == 0)
1177 vpx_memcpy(&pc->fc, &pc->lfc, sizeof(pc->fc));
1178 pbi->independent_partitions = prev_independent_partitions;
1181 #ifdef PACKET_TESTING
1183 FILE *f = fopen("decompressor.VP8", "ab");
1184 unsigned int size = pbi->bc2.pos + pbi->bc.pos + 8;
1185 fwrite((void *) &size, 4, 1, f);
1186 fwrite((void *) pbi->Source, size, 1, f);