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 "onyxd_int.h"
14 #include "reconintra.h"
15 #include "reconintra4x4.h"
17 #include "reconinter.h"
18 #include "dequantize.h"
19 #include "detokenize.h"
21 #include "alloccommon.h"
22 #include "entropymode.h"
23 #include "quant_common.h"
24 #include "vpx_scale/vpxscale.h"
25 #include "vpx_scale/yv12extend.h"
26 #include "setupintrarecon.h"
30 #include "vpx_mem/vpx_mem.h"
32 #include "dequantize.h"
33 #include "predictdc.h"
34 #include "threading.h"
35 #include "decoderthreading.h"
36 #include "dboolhuff.h"
41 void vp8cx_init_de_quantizer(VP8D_COMP
*pbi
)
45 VP8_COMMON
*const pc
= & pbi
->common
;
47 for (Q
= 0; Q
< QINDEX_RANGE
; Q
++)
49 pc
->Y1dequant
[Q
][0] = (short)vp8_dc_quant(Q
, pc
->y1dc_delta_q
);
50 pc
->Y2dequant
[Q
][0] = (short)vp8_dc2quant(Q
, pc
->y2dc_delta_q
);
51 pc
->UVdequant
[Q
][0] = (short)vp8_dc_uv_quant(Q
, pc
->uvdc_delta_q
);
53 /* all the ac values = ; */
54 for (i
= 1; i
< 16; i
++)
56 int rc
= vp8_default_zig_zag1d
[i
];
58 pc
->Y1dequant
[Q
][rc
] = (short)vp8_ac_yquant(Q
);
59 pc
->Y2dequant
[Q
][rc
] = (short)vp8_ac2quant(Q
, pc
->y2ac_delta_q
);
60 pc
->UVdequant
[Q
][rc
] = (short)vp8_ac_uv_quant(Q
, pc
->uvac_delta_q
);
65 void mb_init_dequantizer(VP8D_COMP
*pbi
, MACROBLOCKD
*xd
)
69 MB_MODE_INFO
*mbmi
= &xd
->mode_info_context
->mbmi
;
70 VP8_COMMON
*const pc
= & pbi
->common
;
72 /* Decide whether to use the default or alternate baseline Q value. */
73 if (xd
->segmentation_enabled
)
76 if (xd
->mb_segement_abs_delta
== SEGMENT_ABSDATA
)
77 QIndex
= xd
->segment_feature_data
[MB_LVL_ALT_Q
][mbmi
->segment_id
];
82 QIndex
= pc
->base_qindex
+ xd
->segment_feature_data
[MB_LVL_ALT_Q
][mbmi
->segment_id
];
83 QIndex
= (QIndex
>= 0) ? ((QIndex
<= MAXQ
) ? QIndex
: MAXQ
) : 0; /* Clamp to valid range */
87 QIndex
= pc
->base_qindex
;
89 /* Set up the block level dequant pointers */
90 for (i
= 0; i
< 16; i
++)
92 xd
->block
[i
].dequant
= pc
->Y1dequant
[QIndex
];
95 for (i
= 16; i
< 24; i
++)
97 xd
->block
[i
].dequant
= pc
->UVdequant
[QIndex
];
100 xd
->block
[24].dequant
= pc
->Y2dequant
[QIndex
];
104 #if CONFIG_RUNTIME_CPU_DETECT
105 #define RTCD_VTABLE(x) (&(pbi)->common.rtcd.x)
107 #define RTCD_VTABLE(x) NULL
110 /* skip_recon_mb() is Modified: Instead of writing the result to predictor buffer and then copying it
111 * to dst buffer, we can write the result directly to dst buffer. This eliminates unnecessary copy.
113 static void skip_recon_mb(VP8D_COMP
*pbi
, MACROBLOCKD
*xd
)
115 if (xd
->frame_type
== KEY_FRAME
|| xd
->mode_info_context
->mbmi
.ref_frame
== INTRA_FRAME
)
118 vp8_build_intra_predictors_mbuv_s(xd
);
119 vp8_build_intra_predictors_mby_s_ptr(xd
);
124 vp8_build_inter_predictors_mb_s(xd
);
128 static void clamp_mv_to_umv_border(MV
*mv
, const MACROBLOCKD
*xd
)
130 /* If the MV points so far into the UMV border that no visible pixels
131 * are used for reconstruction, the subpel part of the MV can be
132 * discarded and the MV limited to 16 pixels with equivalent results.
134 * This limit kicks in at 19 pixels for the top and left edges, for
135 * the 16 pixels plus 3 taps right of the central pixel when subpel
136 * filtering. The bottom and right edges use 16 pixels plus 2 pixels
137 * left of the central pixel when filtering.
139 if (mv
->col
< (xd
->mb_to_left_edge
- (19 << 3)))
140 mv
->col
= xd
->mb_to_left_edge
- (16 << 3);
141 else if (mv
->col
> xd
->mb_to_right_edge
+ (18 << 3))
142 mv
->col
= xd
->mb_to_right_edge
+ (16 << 3);
144 if (mv
->row
< (xd
->mb_to_top_edge
- (19 << 3)))
145 mv
->row
= xd
->mb_to_top_edge
- (16 << 3);
146 else if (mv
->row
> xd
->mb_to_bottom_edge
+ (18 << 3))
147 mv
->row
= xd
->mb_to_bottom_edge
+ (16 << 3);
150 /* A version of the above function for chroma block MVs.*/
151 static void clamp_uvmv_to_umv_border(MV
*mv
, const MACROBLOCKD
*xd
)
153 mv
->col
= (2*mv
->col
< (xd
->mb_to_left_edge
- (19 << 3))) ? (xd
->mb_to_left_edge
- (16 << 3)) >> 1 : mv
->col
;
154 mv
->col
= (2*mv
->col
> xd
->mb_to_right_edge
+ (18 << 3)) ? (xd
->mb_to_right_edge
+ (16 << 3)) >> 1 : mv
->col
;
156 mv
->row
= (2*mv
->row
< (xd
->mb_to_top_edge
- (19 << 3))) ? (xd
->mb_to_top_edge
- (16 << 3)) >> 1 : mv
->row
;
157 mv
->row
= (2*mv
->row
> xd
->mb_to_bottom_edge
+ (18 << 3)) ? (xd
->mb_to_bottom_edge
+ (16 << 3)) >> 1 : mv
->row
;
160 void clamp_mvs(MACROBLOCKD
*xd
)
162 if (xd
->mode_info_context
->mbmi
.mode
== SPLITMV
)
167 clamp_mv_to_umv_border(&xd
->block
[i
].bmi
.mv
.as_mv
, xd
);
168 for (i
=16; i
<24; i
++)
169 clamp_uvmv_to_umv_border(&xd
->block
[i
].bmi
.mv
.as_mv
, xd
);
173 clamp_mv_to_umv_border(&xd
->mode_info_context
->mbmi
.mv
.as_mv
, xd
);
174 clamp_uvmv_to_umv_border(&xd
->block
[16].bmi
.mv
.as_mv
, xd
);
179 void vp8_decode_macroblock(VP8D_COMP
*pbi
, MACROBLOCKD
*xd
)
182 int i
, do_clamp
= xd
->mode_info_context
->mbmi
.need_to_clamp_mvs
;
184 if (xd
->mode_info_context
->mbmi
.mb_skip_coeff
)
186 vp8_reset_mb_tokens_context(xd
);
190 eobtotal
= vp8_decode_mb_tokens(pbi
, xd
);
193 /* Perform temporary clamping of the MV to be used for prediction */
199 xd
->mode_info_context
->mbmi
.dc_diff
= 1;
201 if (xd
->mode_info_context
->mbmi
.mode
!= B_PRED
&& xd
->mode_info_context
->mbmi
.mode
!= SPLITMV
&& eobtotal
== 0)
203 xd
->mode_info_context
->mbmi
.dc_diff
= 0;
204 skip_recon_mb(pbi
, xd
);
208 if (xd
->segmentation_enabled
)
209 mb_init_dequantizer(pbi
, xd
);
212 if (xd
->frame_type
== KEY_FRAME
|| xd
->mode_info_context
->mbmi
.ref_frame
== INTRA_FRAME
)
214 vp8_build_intra_predictors_mbuv(xd
);
216 if (xd
->mode_info_context
->mbmi
.mode
!= B_PRED
)
218 vp8_build_intra_predictors_mby_ptr(xd
);
220 vp8_intra_prediction_down_copy(xd
);
225 vp8_build_inter_predictors_mb(xd
);
228 /* dequantization and idct */
229 if (xd
->mode_info_context
->mbmi
.mode
!= B_PRED
&& xd
->mode_info_context
->mbmi
.mode
!= SPLITMV
)
231 BLOCKD
*b
= &xd
->block
[24];
232 DEQUANT_INVOKE(&pbi
->dequant
, block
)(b
);
234 /* do 2nd order transform on the dc block */
235 if (xd
->eobs
[24] > 1)
237 IDCT_INVOKE(RTCD_VTABLE(idct
), iwalsh16
)(&b
->dqcoeff
[0], b
->diff
);
238 ((int *)b
->qcoeff
)[0] = 0;
239 ((int *)b
->qcoeff
)[1] = 0;
240 ((int *)b
->qcoeff
)[2] = 0;
241 ((int *)b
->qcoeff
)[3] = 0;
242 ((int *)b
->qcoeff
)[4] = 0;
243 ((int *)b
->qcoeff
)[5] = 0;
244 ((int *)b
->qcoeff
)[6] = 0;
245 ((int *)b
->qcoeff
)[7] = 0;
249 IDCT_INVOKE(RTCD_VTABLE(idct
), iwalsh1
)(&b
->dqcoeff
[0], b
->diff
);
250 ((int *)b
->qcoeff
)[0] = 0;
253 DEQUANT_INVOKE (&pbi
->dequant
, dc_idct_add_y_block
)
254 (xd
->qcoeff
, xd
->block
[0].dequant
,
255 xd
->predictor
, xd
->dst
.y_buffer
,
256 xd
->dst
.y_stride
, xd
->eobs
, xd
->block
[24].diff
);
258 else if ((xd
->frame_type
== KEY_FRAME
|| xd
->mode_info_context
->mbmi
.ref_frame
== INTRA_FRAME
) && xd
->mode_info_context
->mbmi
.mode
== B_PRED
)
260 for (i
= 0; i
< 16; i
++)
263 BLOCKD
*b
= &xd
->block
[i
];
264 vp8_predict_intra4x4(b
, b
->bmi
.mode
, b
->predictor
);
268 DEQUANT_INVOKE(&pbi
->dequant
, idct_add
)
269 (b
->qcoeff
, b
->dequant
, b
->predictor
,
270 *(b
->base_dst
) + b
->dst
, 16, b
->dst_stride
);
274 IDCT_INVOKE(RTCD_VTABLE(idct
), idct1_scalar_add
)
275 (b
->qcoeff
[0] * b
->dequant
[0], b
->predictor
,
276 *(b
->base_dst
) + b
->dst
, 16, b
->dst_stride
);
277 ((int *)b
->qcoeff
)[0] = 0;
284 DEQUANT_INVOKE (&pbi
->dequant
, idct_add_y_block
)
285 (xd
->qcoeff
, xd
->block
[0].dequant
,
286 xd
->predictor
, xd
->dst
.y_buffer
,
287 xd
->dst
.y_stride
, xd
->eobs
);
290 DEQUANT_INVOKE (&pbi
->dequant
, idct_add_uv_block
)
291 (xd
->qcoeff
+16*16, xd
->block
[16].dequant
,
292 xd
->predictor
+16*16, xd
->dst
.u_buffer
, xd
->dst
.v_buffer
,
293 xd
->dst
.uv_stride
, xd
->eobs
+16);
297 static int get_delta_q(vp8_reader
*bc
, int prev
, int *q_update
)
301 if (vp8_read_bit(bc
))
303 ret_val
= vp8_read_literal(bc
, 4);
305 if (vp8_read_bit(bc
))
309 /* Trigger a quantizer update if the delta-q value has changed */
316 #ifdef PACKET_TESTING
323 void vp8_decode_mb_row(VP8D_COMP
*pbi
,
330 int recon_yoffset
, recon_uvoffset
;
332 int ref_fb_idx
= pc
->lst_fb_idx
;
333 int dst_fb_idx
= pc
->new_fb_idx
;
334 int recon_y_stride
= pc
->yv12_fb
[ref_fb_idx
].y_stride
;
335 int recon_uv_stride
= pc
->yv12_fb
[ref_fb_idx
].uv_stride
;
337 vpx_memset(&pc
->left_context
, 0, sizeof(pc
->left_context
));
338 recon_yoffset
= mb_row
* recon_y_stride
* 16;
339 recon_uvoffset
= mb_row
* recon_uv_stride
* 8;
340 /* reset above block coeffs */
342 xd
->above_context
= pc
->above_context
;
343 xd
->up_available
= (mb_row
!= 0);
345 xd
->mb_to_top_edge
= -((mb_row
* 16)) << 3;
346 xd
->mb_to_bottom_edge
= ((pc
->mb_rows
- 1 - mb_row
) * 16) << 3;
348 for (mb_col
= 0; mb_col
< pc
->mb_cols
; mb_col
++)
351 if (xd
->mode_info_context
->mbmi
.mode
== SPLITMV
|| xd
->mode_info_context
->mbmi
.mode
== B_PRED
)
353 for (i
= 0; i
< 16; i
++)
355 BLOCKD
*d
= &xd
->block
[i
];
356 vpx_memcpy(&d
->bmi
, &xd
->mode_info_context
->bmi
[i
], sizeof(B_MODE_INFO
));
360 /* Distance of Mb to the various image edges.
361 * These are specified to 8th pel as they are always compared to values that are in 1/8th pel units
363 xd
->mb_to_left_edge
= -((mb_col
* 16) << 3);
364 xd
->mb_to_right_edge
= ((pc
->mb_cols
- 1 - mb_col
) * 16) << 3;
366 xd
->dst
.y_buffer
= pc
->yv12_fb
[dst_fb_idx
].y_buffer
+ recon_yoffset
;
367 xd
->dst
.u_buffer
= pc
->yv12_fb
[dst_fb_idx
].u_buffer
+ recon_uvoffset
;
368 xd
->dst
.v_buffer
= pc
->yv12_fb
[dst_fb_idx
].v_buffer
+ recon_uvoffset
;
370 xd
->left_available
= (mb_col
!= 0);
372 /* Select the appropriate reference frame for this MB */
373 if (xd
->mode_info_context
->mbmi
.ref_frame
== LAST_FRAME
)
374 ref_fb_idx
= pc
->lst_fb_idx
;
375 else if (xd
->mode_info_context
->mbmi
.ref_frame
== GOLDEN_FRAME
)
376 ref_fb_idx
= pc
->gld_fb_idx
;
378 ref_fb_idx
= pc
->alt_fb_idx
;
380 xd
->pre
.y_buffer
= pc
->yv12_fb
[ref_fb_idx
].y_buffer
+ recon_yoffset
;
381 xd
->pre
.u_buffer
= pc
->yv12_fb
[ref_fb_idx
].u_buffer
+ recon_uvoffset
;
382 xd
->pre
.v_buffer
= pc
->yv12_fb
[ref_fb_idx
].v_buffer
+ recon_uvoffset
;
384 vp8_build_uvmvs(xd
, pc
->full_pixel
);
387 if(pc->current_video_frame==0 &&mb_col==1 && mb_row==0)
392 vp8_decode_macroblock(pbi
, xd
);
398 ++xd
->mode_info_context
; /* next mb */
404 /* adjust to the next row of mbs */
406 &pc
->yv12_fb
[dst_fb_idx
],
407 xd
->dst
.y_buffer
+ 16, xd
->dst
.u_buffer
+ 8, xd
->dst
.v_buffer
+ 8
410 ++xd
->mode_info_context
; /* skip prediction column */
414 static unsigned int read_partition_size(const unsigned char *cx_size
)
416 const unsigned int size
=
417 cx_size
[0] + (cx_size
[1] << 8) + (cx_size
[2] << 16);
422 static void setup_token_decoder(VP8D_COMP
*pbi
,
423 const unsigned char *cx_data
)
427 VP8_COMMON
*pc
= &pbi
->common
;
428 const unsigned char *user_data_end
= pbi
->Source
+ pbi
->source_sz
;
429 vp8_reader
*bool_decoder
;
430 const unsigned char *partition
;
432 /* Parse number of token partitions to use */
433 pc
->multi_token_partition
= (TOKEN_PARTITION
)vp8_read_literal(&pbi
->bc
, 2);
434 num_part
= 1 << pc
->multi_token_partition
;
436 /* Set up pointers to the first partition */
438 bool_decoder
= &pbi
->bc2
;
442 CHECK_MEM_ERROR(pbi
->mbc
, vpx_malloc(num_part
* sizeof(vp8_reader
)));
443 bool_decoder
= pbi
->mbc
;
444 partition
+= 3 * (num_part
- 1);
447 for (i
= 0; i
< num_part
; i
++)
449 const unsigned char *partition_size_ptr
= cx_data
+ i
* 3;
450 ptrdiff_t partition_size
;
452 /* Calculate the length of this partition. The last partition
455 if (i
< num_part
- 1)
457 partition_size
= read_partition_size(partition_size_ptr
);
461 partition_size
= user_data_end
- partition
;
464 if (partition
+ partition_size
> user_data_end
465 || partition
+ partition_size
< partition
)
466 vpx_internal_error(&pc
->error
, VPX_CODEC_CORRUPT_FRAME
,
467 "Truncated packet or corrupt partition "
470 if (vp8dx_start_decode(bool_decoder
, IF_RTCD(&pbi
->dboolhuff
),
471 partition
, partition_size
))
472 vpx_internal_error(&pc
->error
, VPX_CODEC_MEM_ERROR
,
473 "Failed to allocate bool decoder %d", i
+ 1);
475 /* Advance to the next partition */
476 partition
+= partition_size
;
480 /* Clamp number of decoder threads */
481 if (pbi
->decoding_thread_count
> num_part
- 1)
482 pbi
->decoding_thread_count
= num_part
- 1;
486 static void stop_token_decoder(VP8D_COMP
*pbi
)
489 VP8_COMMON
*pc
= &pbi
->common
;
491 if (pc
->multi_token_partition
!= ONE_PARTITION
)
495 static void init_frame(VP8D_COMP
*pbi
)
497 VP8_COMMON
*const pc
= & pbi
->common
;
498 MACROBLOCKD
*const xd
= & pbi
->mb
;
500 if (pc
->frame_type
== KEY_FRAME
)
502 /* Various keyframe initializations */
503 vpx_memcpy(pc
->fc
.mvc
, vp8_default_mv_context
, sizeof(vp8_default_mv_context
));
505 vp8_init_mbmode_probs(pc
);
507 vp8_default_coef_probs(pc
);
508 vp8_kf_default_bmode_probs(pc
->kf_bmode_prob
);
510 /* reset the segment feature data to 0 with delta coding (Default state). */
511 vpx_memset(xd
->segment_feature_data
, 0, sizeof(xd
->segment_feature_data
));
512 xd
->mb_segement_abs_delta
= SEGMENT_DELTADATA
;
514 /* reset the mode ref deltasa for loop filter */
515 vpx_memset(xd
->ref_lf_deltas
, 0, sizeof(xd
->ref_lf_deltas
));
516 vpx_memset(xd
->mode_lf_deltas
, 0, sizeof(xd
->mode_lf_deltas
));
518 /* All buffers are implicitly updated on key frames. */
519 pc
->refresh_golden_frame
= 1;
520 pc
->refresh_alt_ref_frame
= 1;
521 pc
->copy_buffer_to_gf
= 0;
522 pc
->copy_buffer_to_arf
= 0;
524 /* Note that Golden and Altref modes cannot be used on a key frame so
525 * ref_frame_sign_bias[] is undefined and meaningless
527 pc
->ref_frame_sign_bias
[GOLDEN_FRAME
] = 0;
528 pc
->ref_frame_sign_bias
[ALTREF_FRAME
] = 0;
532 if (!pc
->use_bilinear_mc_filter
)
533 pc
->mcomp_filter_type
= SIXTAP
;
535 pc
->mcomp_filter_type
= BILINEAR
;
537 /* To enable choice of different interploation filters */
538 if (pc
->mcomp_filter_type
== SIXTAP
)
540 xd
->subpixel_predict
= SUBPIX_INVOKE(RTCD_VTABLE(subpix
), sixtap4x4
);
541 xd
->subpixel_predict8x4
= SUBPIX_INVOKE(RTCD_VTABLE(subpix
), sixtap8x4
);
542 xd
->subpixel_predict8x8
= SUBPIX_INVOKE(RTCD_VTABLE(subpix
), sixtap8x8
);
543 xd
->subpixel_predict16x16
= SUBPIX_INVOKE(RTCD_VTABLE(subpix
), sixtap16x16
);
547 xd
->subpixel_predict
= SUBPIX_INVOKE(RTCD_VTABLE(subpix
), bilinear4x4
);
548 xd
->subpixel_predict8x4
= SUBPIX_INVOKE(RTCD_VTABLE(subpix
), bilinear8x4
);
549 xd
->subpixel_predict8x8
= SUBPIX_INVOKE(RTCD_VTABLE(subpix
), bilinear8x8
);
550 xd
->subpixel_predict16x16
= SUBPIX_INVOKE(RTCD_VTABLE(subpix
), bilinear16x16
);
554 xd
->left_context
= &pc
->left_context
;
555 xd
->mode_info_context
= pc
->mi
;
556 xd
->frame_type
= pc
->frame_type
;
557 xd
->mode_info_context
->mbmi
.mode
= DC_PRED
;
558 xd
->mode_info_stride
= pc
->mode_info_stride
;
561 int vp8_decode_frame(VP8D_COMP
*pbi
)
563 vp8_reader
*const bc
= & pbi
->bc
;
564 VP8_COMMON
*const pc
= & pbi
->common
;
565 MACROBLOCKD
*const xd
= & pbi
->mb
;
566 const unsigned char *data
= (const unsigned char *)pbi
->Source
;
567 const unsigned char *const data_end
= data
+ pbi
->source_sz
;
568 ptrdiff_t first_partition_length_in_bytes
;
572 const int *const mb_feature_data_bits
= vp8_mb_feature_data_bits
;
574 if (data_end
- data
< 3)
575 vpx_internal_error(&pc
->error
, VPX_CODEC_CORRUPT_FRAME
,
577 pc
->frame_type
= (FRAME_TYPE
)(data
[0] & 1);
578 pc
->version
= (data
[0] >> 1) & 7;
579 pc
->show_frame
= (data
[0] >> 4) & 1;
580 first_partition_length_in_bytes
=
581 (data
[0] | (data
[1] << 8) | (data
[2] << 16)) >> 5;
584 if (data
+ first_partition_length_in_bytes
> data_end
585 || data
+ first_partition_length_in_bytes
< data
)
586 vpx_internal_error(&pc
->error
, VPX_CODEC_CORRUPT_FRAME
,
587 "Truncated packet or corrupt partition 0 length");
588 vp8_setup_version(pc
);
590 if (pc
->frame_type
== KEY_FRAME
)
592 const int Width
= pc
->Width
;
593 const int Height
= pc
->Height
;
595 /* vet via sync code */
596 if (data
[0] != 0x9d || data
[1] != 0x01 || data
[2] != 0x2a)
597 vpx_internal_error(&pc
->error
, VPX_CODEC_UNSUP_BITSTREAM
,
598 "Invalid frame sync code");
600 pc
->Width
= (data
[3] | (data
[4] << 8)) & 0x3fff;
601 pc
->horiz_scale
= data
[4] >> 6;
602 pc
->Height
= (data
[5] | (data
[6] << 8)) & 0x3fff;
603 pc
->vert_scale
= data
[6] >> 6;
606 if (Width
!= pc
->Width
|| Height
!= pc
->Height
)
608 int prev_mb_rows
= pc
->mb_rows
;
613 vpx_internal_error(&pc
->error
, VPX_CODEC_CORRUPT_FRAME
,
614 "Invalid frame width");
620 vpx_internal_error(&pc
->error
, VPX_CODEC_CORRUPT_FRAME
,
621 "Invalid frame height");
624 if (vp8_alloc_frame_buffers(pc
, pc
->Width
, pc
->Height
))
625 vpx_internal_error(&pc
->error
, VPX_CODEC_MEM_ERROR
,
626 "Failed to allocate frame buffers");
628 #if CONFIG_MULTITHREAD
629 if (pbi
->b_multithreaded_rd
)
630 vp8mt_alloc_temp_buffers(pbi
, pc
->Width
, prev_mb_rows
);
635 if (pc
->Width
== 0 || pc
->Height
== 0)
642 if (vp8dx_start_decode(bc
, IF_RTCD(&pbi
->dboolhuff
),
643 data
, data_end
- data
))
644 vpx_internal_error(&pc
->error
, VPX_CODEC_MEM_ERROR
,
645 "Failed to allocate bool decoder 0");
646 if (pc
->frame_type
== KEY_FRAME
) {
647 pc
->clr_type
= (YUV_TYPE
)vp8_read_bit(bc
);
648 pc
->clamp_type
= (CLAMP_TYPE
)vp8_read_bit(bc
);
651 /* Is segmentation enabled */
652 xd
->segmentation_enabled
= (unsigned char)vp8_read_bit(bc
);
654 if (xd
->segmentation_enabled
)
656 /* Signal whether or not the segmentation map is being explicitly updated this frame. */
657 xd
->update_mb_segmentation_map
= (unsigned char)vp8_read_bit(bc
);
658 xd
->update_mb_segmentation_data
= (unsigned char)vp8_read_bit(bc
);
660 if (xd
->update_mb_segmentation_data
)
662 xd
->mb_segement_abs_delta
= (unsigned char)vp8_read_bit(bc
);
664 vpx_memset(xd
->segment_feature_data
, 0, sizeof(xd
->segment_feature_data
));
666 /* For each segmentation feature (Quant and loop filter level) */
667 for (i
= 0; i
< MB_LVL_MAX
; i
++)
669 for (j
= 0; j
< MAX_MB_SEGMENTS
; j
++)
671 /* Frame level data */
672 if (vp8_read_bit(bc
))
674 xd
->segment_feature_data
[i
][j
] = (signed char)vp8_read_literal(bc
, mb_feature_data_bits
[i
]);
676 if (vp8_read_bit(bc
))
677 xd
->segment_feature_data
[i
][j
] = -xd
->segment_feature_data
[i
][j
];
680 xd
->segment_feature_data
[i
][j
] = 0;
685 if (xd
->update_mb_segmentation_map
)
687 /* Which macro block level features are enabled */
688 vpx_memset(xd
->mb_segment_tree_probs
, 255, sizeof(xd
->mb_segment_tree_probs
));
690 /* Read the probs used to decode the segment id for each macro block. */
691 for (i
= 0; i
< MB_FEATURE_TREE_PROBS
; i
++)
693 /* If not explicitly set value is defaulted to 255 by memset above */
694 if (vp8_read_bit(bc
))
695 xd
->mb_segment_tree_probs
[i
] = (vp8_prob
)vp8_read_literal(bc
, 8);
700 /* Read the loop filter level and type */
701 pc
->filter_type
= (LOOPFILTERTYPE
) vp8_read_bit(bc
);
702 pc
->filter_level
= vp8_read_literal(bc
, 6);
703 pc
->sharpness_level
= vp8_read_literal(bc
, 3);
705 /* Read in loop filter deltas applied at the MB level based on mode or ref frame. */
706 xd
->mode_ref_lf_delta_update
= 0;
707 xd
->mode_ref_lf_delta_enabled
= (unsigned char)vp8_read_bit(bc
);
709 if (xd
->mode_ref_lf_delta_enabled
)
711 /* Do the deltas need to be updated */
712 xd
->mode_ref_lf_delta_update
= (unsigned char)vp8_read_bit(bc
);
714 if (xd
->mode_ref_lf_delta_update
)
717 for (i
= 0; i
< MAX_REF_LF_DELTAS
; i
++)
719 if (vp8_read_bit(bc
))
721 /*sign = vp8_read_bit( bc );*/
722 xd
->ref_lf_deltas
[i
] = (signed char)vp8_read_literal(bc
, 6);
724 if (vp8_read_bit(bc
)) /* Apply sign */
725 xd
->ref_lf_deltas
[i
] = xd
->ref_lf_deltas
[i
] * -1;
730 for (i
= 0; i
< MAX_MODE_LF_DELTAS
; i
++)
732 if (vp8_read_bit(bc
))
734 /*sign = vp8_read_bit( bc );*/
735 xd
->mode_lf_deltas
[i
] = (signed char)vp8_read_literal(bc
, 6);
737 if (vp8_read_bit(bc
)) /* Apply sign */
738 xd
->mode_lf_deltas
[i
] = xd
->mode_lf_deltas
[i
] * -1;
744 setup_token_decoder(pbi
, data
+ first_partition_length_in_bytes
);
745 xd
->current_bc
= &pbi
->bc2
;
747 /* Read the default quantizers. */
751 Q
= vp8_read_literal(bc
, 7); /* AC 1st order Q = default */
754 pc
->y1dc_delta_q
= get_delta_q(bc
, pc
->y1dc_delta_q
, &q_update
);
755 pc
->y2dc_delta_q
= get_delta_q(bc
, pc
->y2dc_delta_q
, &q_update
);
756 pc
->y2ac_delta_q
= get_delta_q(bc
, pc
->y2ac_delta_q
, &q_update
);
757 pc
->uvdc_delta_q
= get_delta_q(bc
, pc
->uvdc_delta_q
, &q_update
);
758 pc
->uvac_delta_q
= get_delta_q(bc
, pc
->uvac_delta_q
, &q_update
);
761 vp8cx_init_de_quantizer(pbi
);
763 /* MB level dequantizer setup */
764 mb_init_dequantizer(pbi
, &pbi
->mb
);
767 /* Determine if the golden frame or ARF buffer should be updated and how.
768 * For all non key frames the GF and ARF refresh flags and sign bias
769 * flags must be set explicitly.
771 if (pc
->frame_type
!= KEY_FRAME
)
773 /* Should the GF or ARF be updated from the current frame */
774 pc
->refresh_golden_frame
= vp8_read_bit(bc
);
775 pc
->refresh_alt_ref_frame
= vp8_read_bit(bc
);
777 /* Buffer to buffer copy flags. */
778 pc
->copy_buffer_to_gf
= 0;
780 if (!pc
->refresh_golden_frame
)
781 pc
->copy_buffer_to_gf
= vp8_read_literal(bc
, 2);
783 pc
->copy_buffer_to_arf
= 0;
785 if (!pc
->refresh_alt_ref_frame
)
786 pc
->copy_buffer_to_arf
= vp8_read_literal(bc
, 2);
788 pc
->ref_frame_sign_bias
[GOLDEN_FRAME
] = vp8_read_bit(bc
);
789 pc
->ref_frame_sign_bias
[ALTREF_FRAME
] = vp8_read_bit(bc
);
792 pc
->refresh_entropy_probs
= vp8_read_bit(bc
);
793 if (pc
->refresh_entropy_probs
== 0)
795 vpx_memcpy(&pc
->lfc
, &pc
->fc
, sizeof(pc
->fc
));
798 pc
->refresh_last_frame
= pc
->frame_type
== KEY_FRAME
|| vp8_read_bit(bc
);
802 FILE *z
= fopen("decodestats.stt", "a");
803 fprintf(z
, "%6d F:%d,G:%d,A:%d,L:%d,Q:%d\n",
804 pc
->current_video_frame
,
806 pc
->refresh_golden_frame
,
807 pc
->refresh_alt_ref_frame
,
808 pc
->refresh_last_frame
,
815 /* read coef probability tree */
817 for (i
= 0; i
< BLOCK_TYPES
; i
++)
818 for (j
= 0; j
< COEF_BANDS
; j
++)
819 for (k
= 0; k
< PREV_COEF_CONTEXTS
; k
++)
820 for (l
= 0; l
< MAX_ENTROPY_TOKENS
- 1; l
++)
823 vp8_prob
*const p
= pc
->fc
.coef_probs
[i
][j
][k
] + l
;
825 if (vp8_read(bc
, vp8_coef_update_probs
[i
][j
][k
][l
]))
827 *p
= (vp8_prob
)vp8_read_literal(bc
, 8);
833 vpx_memcpy(&xd
->pre
, &pc
->yv12_fb
[pc
->lst_fb_idx
], sizeof(YV12_BUFFER_CONFIG
));
834 vpx_memcpy(&xd
->dst
, &pc
->yv12_fb
[pc
->new_fb_idx
], sizeof(YV12_BUFFER_CONFIG
));
836 /* set up frame new frame for intra coded blocks */
837 if (!(pbi
->b_multithreaded_rd
) || pc
->multi_token_partition
== ONE_PARTITION
|| !(pc
->filter_level
))
838 vp8_setup_intra_recon(&pc
->yv12_fb
[pc
->new_fb_idx
]);
840 vp8_setup_block_dptrs(xd
);
842 vp8_build_block_doffsets(xd
);
844 /* clear out the coeff buffer */
845 vpx_memset(xd
->qcoeff
, 0, sizeof(xd
->qcoeff
));
847 /* Read the mb_no_coeff_skip flag */
848 pc
->mb_no_coeff_skip
= (int)vp8_read_bit(bc
);
851 vp8_decode_mode_mvs(pbi
);
853 vpx_memset(pc
->above_context
, 0, sizeof(ENTROPY_CONTEXT_PLANES
) * pc
->mb_cols
);
855 vpx_memcpy(&xd
->block
[0].bmi
, &xd
->mode_info_context
->bmi
[0], sizeof(B_MODE_INFO
));
857 if (pbi
->b_multithreaded_rd
&& pc
->multi_token_partition
!= ONE_PARTITION
)
859 vp8mt_decode_mb_rows(pbi
, xd
);
860 if(pbi
->common
.filter_level
)
862 /*vp8_mt_loop_filter_frame(pbi);*/ /*cm, &pbi->mb, cm->filter_level);*/
864 pc
->last_frame_type
= pc
->frame_type
;
865 pc
->last_filter_type
= pc
->filter_type
;
866 pc
->last_sharpness_level
= pc
->sharpness_level
;
868 vp8_yv12_extend_frame_borders_ptr(&pc
->yv12_fb
[pc
->new_fb_idx
]); /*cm->frame_to_show);*/
873 int num_part
= 1 << pc
->multi_token_partition
;
875 /* Decode the individual macro block */
876 for (mb_row
= 0; mb_row
< pc
->mb_rows
; mb_row
++)
881 xd
->current_bc
= & pbi
->mbc
[ibc
];
888 vp8_decode_mb_row(pbi
, pc
, mb_row
, xd
);
893 stop_token_decoder(pbi
);
895 /* vpx_log("Decoder: Frame Decoded, Size Roughly:%d bytes \n",bc->pos+pbi->bc2.pos); */
897 /* If this was a kf or Gf note the Q used */
898 if ((pc
->frame_type
== KEY_FRAME
) ||
899 pc
->refresh_golden_frame
|| pc
->refresh_alt_ref_frame
)
901 pc
->last_kf_gf_q
= pc
->base_qindex
;
904 if (pc
->refresh_entropy_probs
== 0)
906 vpx_memcpy(&pc
->fc
, &pc
->lfc
, sizeof(pc
->fc
));
909 #ifdef PACKET_TESTING
911 FILE *f
= fopen("decompressor.VP8", "ab");
912 unsigned int size
= pbi
->bc2
.pos
+ pbi
->bc
.pos
+ 8;
913 fwrite((void *) &size
, 4, 1, f
);
914 fwrite((void *) pbi
->Source
, size
, 1, f
);