1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10
11
12 #include "vpx_config.h"
13 #include "vp8_rtcd.h"
14 #include "./vpx_scale_rtcd.h"
15 #include "onyxd_int.h"
16 #include "vp8/common/header.h"
17 #include "vp8/common/reconintra4x4.h"
18 #include "vp8/common/reconinter.h"
19 #include "detokenize.h"
20 #include "vp8/common/common.h"
21 #include "vp8/common/invtrans.h"
22 #include "vp8/common/alloccommon.h"
23 #include "vp8/common/entropymode.h"
24 #include "vp8/common/quant_common.h"
25 #include "vpx_scale/vpx_scale.h"
26 #include "vp8/common/reconintra.h"
27 #include "vp8/common/setupintrarecon.h"
28
29 #include "decodemv.h"
30 #include "vp8/common/extend.h"
31 #if CONFIG_ERROR_CONCEALMENT
32 #include "error_concealment.h"
33 #endif
34 #include "vpx_mem/vpx_mem.h"
35 #include "vp8/common/threading.h"
36 #include "decoderthreading.h"
37 #include "dboolhuff.h"
38 #include "vpx_dsp/vpx_dsp_common.h"
39
40 #include <assert.h>
41 #include <stdio.h>
42
vp8cx_init_de_quantizer(VP8D_COMP * pbi)43 void vp8cx_init_de_quantizer(VP8D_COMP *pbi)
44 {
45 int Q;
46 VP8_COMMON *const pc = & pbi->common;
47
48 for (Q = 0; Q < QINDEX_RANGE; Q++)
49 {
50 pc->Y1dequant[Q][0] = (short)vp8_dc_quant(Q, pc->y1dc_delta_q);
51 pc->Y2dequant[Q][0] = (short)vp8_dc2quant(Q, pc->y2dc_delta_q);
52 pc->UVdequant[Q][0] = (short)vp8_dc_uv_quant(Q, pc->uvdc_delta_q);
53
54 pc->Y1dequant[Q][1] = (short)vp8_ac_yquant(Q);
55 pc->Y2dequant[Q][1] = (short)vp8_ac2quant(Q, pc->y2ac_delta_q);
56 pc->UVdequant[Q][1] = (short)vp8_ac_uv_quant(Q, pc->uvac_delta_q);
57 }
58 }
59
vp8_mb_init_dequantizer(VP8D_COMP * pbi,MACROBLOCKD * xd)60 void vp8_mb_init_dequantizer(VP8D_COMP *pbi, MACROBLOCKD *xd)
61 {
62 int i;
63 int QIndex;
64 MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
65 VP8_COMMON *const pc = & pbi->common;
66
67 /* Decide whether to use the default or alternate baseline Q value. */
68 if (xd->segmentation_enabled)
69 {
70 /* Abs Value */
71 if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA)
72 QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
73
74 /* Delta Value */
75 else
76 QIndex = pc->base_qindex + xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
77
78 QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0; /* Clamp to valid range */
79 }
80 else
81 QIndex = pc->base_qindex;
82
83 /* Set up the macroblock dequant constants */
84 xd->dequant_y1_dc[0] = 1;
85 xd->dequant_y1[0] = pc->Y1dequant[QIndex][0];
86 xd->dequant_y2[0] = pc->Y2dequant[QIndex][0];
87 xd->dequant_uv[0] = pc->UVdequant[QIndex][0];
88
89 for (i = 1; i < 16; i++)
90 {
91 xd->dequant_y1_dc[i] =
92 xd->dequant_y1[i] = pc->Y1dequant[QIndex][1];
93 xd->dequant_y2[i] = pc->Y2dequant[QIndex][1];
94 xd->dequant_uv[i] = pc->UVdequant[QIndex][1];
95 }
96 }
97
decode_macroblock(VP8D_COMP * pbi,MACROBLOCKD * xd,unsigned int mb_idx)98 static void decode_macroblock(VP8D_COMP *pbi, MACROBLOCKD *xd,
99 unsigned int mb_idx)
100 {
101 MB_PREDICTION_MODE mode;
102 int i;
103 #if CONFIG_ERROR_CONCEALMENT
104 int corruption_detected = 0;
105 #else
106 (void)mb_idx;
107 #endif
108
109 if (xd->mode_info_context->mbmi.mb_skip_coeff)
110 {
111 vp8_reset_mb_tokens_context(xd);
112 }
113 else if (!vp8dx_bool_error(xd->current_bc))
114 {
115 int eobtotal;
116 eobtotal = vp8_decode_mb_tokens(pbi, xd);
117
118 /* Special case: Force the loopfilter to skip when eobtotal is zero */
119 xd->mode_info_context->mbmi.mb_skip_coeff = (eobtotal==0);
120 }
121
122 mode = xd->mode_info_context->mbmi.mode;
123
124 if (xd->segmentation_enabled)
125 vp8_mb_init_dequantizer(pbi, xd);
126
127
128 #if CONFIG_ERROR_CONCEALMENT
129
130 if(pbi->ec_active)
131 {
132 int throw_residual;
133 /* When we have independent partitions we can apply residual even
134 * though other partitions within the frame are corrupt.
135 */
136 throw_residual = (!pbi->independent_partitions &&
137 pbi->frame_corrupt_residual);
138 throw_residual = (throw_residual || vp8dx_bool_error(xd->current_bc));
139
140 if ((mb_idx >= pbi->mvs_corrupt_from_mb || throw_residual))
141 {
142 /* MB with corrupt residuals or corrupt mode/motion vectors.
143 * Better to use the predictor as reconstruction.
144 */
145 pbi->frame_corrupt_residual = 1;
146 memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
147 vp8_conceal_corrupt_mb(xd);
148
149
150 corruption_detected = 1;
151
152 /* force idct to be skipped for B_PRED and use the
153 * prediction only for reconstruction
154 * */
155 memset(xd->eobs, 0, 25);
156 }
157 }
158 #endif
159
160 /* do prediction */
161 if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
162 {
163 vp8_build_intra_predictors_mbuv_s(xd,
164 xd->recon_above[1],
165 xd->recon_above[2],
166 xd->recon_left[1],
167 xd->recon_left[2],
168 xd->recon_left_stride[1],
169 xd->dst.u_buffer, xd->dst.v_buffer,
170 xd->dst.uv_stride);
171
172 if (mode != B_PRED)
173 {
174 vp8_build_intra_predictors_mby_s(xd,
175 xd->recon_above[0],
176 xd->recon_left[0],
177 xd->recon_left_stride[0],
178 xd->dst.y_buffer,
179 xd->dst.y_stride);
180 }
181 else
182 {
183 short *DQC = xd->dequant_y1;
184 int dst_stride = xd->dst.y_stride;
185
186 /* clear out residual eob info */
187 if(xd->mode_info_context->mbmi.mb_skip_coeff)
188 memset(xd->eobs, 0, 25);
189
190 intra_prediction_down_copy(xd, xd->recon_above[0] + 16);
191
192 for (i = 0; i < 16; i++)
193 {
194 BLOCKD *b = &xd->block[i];
195 unsigned char *dst = xd->dst.y_buffer + b->offset;
196 B_PREDICTION_MODE b_mode =
197 xd->mode_info_context->bmi[i].as_mode;
198 unsigned char *Above = dst - dst_stride;
199 unsigned char *yleft = dst - 1;
200 int left_stride = dst_stride;
201 unsigned char top_left = Above[-1];
202
203 vp8_intra4x4_predict(Above, yleft, left_stride, b_mode,
204 dst, dst_stride, top_left);
205
206 if (xd->eobs[i])
207 {
208 if (xd->eobs[i] > 1)
209 {
210 vp8_dequant_idct_add(b->qcoeff, DQC, dst, dst_stride);
211 }
212 else
213 {
214 vp8_dc_only_idct_add
215 (b->qcoeff[0] * DQC[0],
216 dst, dst_stride,
217 dst, dst_stride);
218 memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0]));
219 }
220 }
221 }
222 }
223 }
224 else
225 {
226 vp8_build_inter_predictors_mb(xd);
227 }
228
229
230 #if CONFIG_ERROR_CONCEALMENT
231 if (corruption_detected)
232 {
233 return;
234 }
235 #endif
236
237 if(!xd->mode_info_context->mbmi.mb_skip_coeff)
238 {
239 /* dequantization and idct */
240 if (mode != B_PRED)
241 {
242 short *DQC = xd->dequant_y1;
243
244 if (mode != SPLITMV)
245 {
246 BLOCKD *b = &xd->block[24];
247
248 /* do 2nd order transform on the dc block */
249 if (xd->eobs[24] > 1)
250 {
251 vp8_dequantize_b(b, xd->dequant_y2);
252
253 vp8_short_inv_walsh4x4(&b->dqcoeff[0],
254 xd->qcoeff);
255 memset(b->qcoeff, 0, 16 * sizeof(b->qcoeff[0]));
256 }
257 else
258 {
259 b->dqcoeff[0] = b->qcoeff[0] * xd->dequant_y2[0];
260 vp8_short_inv_walsh4x4_1(&b->dqcoeff[0],
261 xd->qcoeff);
262 memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0]));
263 }
264
265 /* override the dc dequant constant in order to preserve the
266 * dc components
267 */
268 DQC = xd->dequant_y1_dc;
269 }
270
271 vp8_dequant_idct_add_y_block
272 (xd->qcoeff, DQC,
273 xd->dst.y_buffer,
274 xd->dst.y_stride, xd->eobs);
275 }
276
277 vp8_dequant_idct_add_uv_block
278 (xd->qcoeff+16*16, xd->dequant_uv,
279 xd->dst.u_buffer, xd->dst.v_buffer,
280 xd->dst.uv_stride, xd->eobs+16);
281 }
282 }
283
get_delta_q(vp8_reader * bc,int prev,int * q_update)284 static int get_delta_q(vp8_reader *bc, int prev, int *q_update)
285 {
286 int ret_val = 0;
287
288 if (vp8_read_bit(bc))
289 {
290 ret_val = vp8_read_literal(bc, 4);
291
292 if (vp8_read_bit(bc))
293 ret_val = -ret_val;
294 }
295
296 /* Trigger a quantizer update if the delta-q value has changed */
297 if (ret_val != prev)
298 *q_update = 1;
299
300 return ret_val;
301 }
302
303 #ifdef PACKET_TESTING
304 #include <stdio.h>
305 FILE *vpxlog = 0;
306 #endif
307
yv12_extend_frame_top_c(YV12_BUFFER_CONFIG * ybf)308 static void yv12_extend_frame_top_c(YV12_BUFFER_CONFIG *ybf)
309 {
310 int i;
311 unsigned char *src_ptr1;
312 unsigned char *dest_ptr1;
313
314 unsigned int Border;
315 int plane_stride;
316
317 /***********/
318 /* Y Plane */
319 /***********/
320 Border = ybf->border;
321 plane_stride = ybf->y_stride;
322 src_ptr1 = ybf->y_buffer - Border;
323 dest_ptr1 = src_ptr1 - (Border * plane_stride);
324
325 for (i = 0; i < (int)Border; i++)
326 {
327 memcpy(dest_ptr1, src_ptr1, plane_stride);
328 dest_ptr1 += plane_stride;
329 }
330
331
332 /***********/
333 /* U Plane */
334 /***********/
335 plane_stride = ybf->uv_stride;
336 Border /= 2;
337 src_ptr1 = ybf->u_buffer - Border;
338 dest_ptr1 = src_ptr1 - (Border * plane_stride);
339
340 for (i = 0; i < (int)(Border); i++)
341 {
342 memcpy(dest_ptr1, src_ptr1, plane_stride);
343 dest_ptr1 += plane_stride;
344 }
345
346 /***********/
347 /* V Plane */
348 /***********/
349
350 src_ptr1 = ybf->v_buffer - Border;
351 dest_ptr1 = src_ptr1 - (Border * plane_stride);
352
353 for (i = 0; i < (int)(Border); i++)
354 {
355 memcpy(dest_ptr1, src_ptr1, plane_stride);
356 dest_ptr1 += plane_stride;
357 }
358 }
359
yv12_extend_frame_bottom_c(YV12_BUFFER_CONFIG * ybf)360 static void yv12_extend_frame_bottom_c(YV12_BUFFER_CONFIG *ybf)
361 {
362 int i;
363 unsigned char *src_ptr1, *src_ptr2;
364 unsigned char *dest_ptr2;
365
366 unsigned int Border;
367 int plane_stride;
368 int plane_height;
369
370 /***********/
371 /* Y Plane */
372 /***********/
373 Border = ybf->border;
374 plane_stride = ybf->y_stride;
375 plane_height = ybf->y_height;
376
377 src_ptr1 = ybf->y_buffer - Border;
378 src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
379 dest_ptr2 = src_ptr2 + plane_stride;
380
381 for (i = 0; i < (int)Border; i++)
382 {
383 memcpy(dest_ptr2, src_ptr2, plane_stride);
384 dest_ptr2 += plane_stride;
385 }
386
387
388 /***********/
389 /* U Plane */
390 /***********/
391 plane_stride = ybf->uv_stride;
392 plane_height = ybf->uv_height;
393 Border /= 2;
394
395 src_ptr1 = ybf->u_buffer - Border;
396 src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
397 dest_ptr2 = src_ptr2 + plane_stride;
398
399 for (i = 0; i < (int)(Border); i++)
400 {
401 memcpy(dest_ptr2, src_ptr2, plane_stride);
402 dest_ptr2 += plane_stride;
403 }
404
405 /***********/
406 /* V Plane */
407 /***********/
408
409 src_ptr1 = ybf->v_buffer - Border;
410 src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
411 dest_ptr2 = src_ptr2 + plane_stride;
412
413 for (i = 0; i < (int)(Border); i++)
414 {
415 memcpy(dest_ptr2, src_ptr2, plane_stride);
416 dest_ptr2 += plane_stride;
417 }
418 }
419
yv12_extend_frame_left_right_c(YV12_BUFFER_CONFIG * ybf,unsigned char * y_src,unsigned char * u_src,unsigned char * v_src)420 static void yv12_extend_frame_left_right_c(YV12_BUFFER_CONFIG *ybf,
421 unsigned char *y_src,
422 unsigned char *u_src,
423 unsigned char *v_src)
424 {
425 int i;
426 unsigned char *src_ptr1, *src_ptr2;
427 unsigned char *dest_ptr1, *dest_ptr2;
428
429 unsigned int Border;
430 int plane_stride;
431 int plane_height;
432 int plane_width;
433
434 /***********/
435 /* Y Plane */
436 /***********/
437 Border = ybf->border;
438 plane_stride = ybf->y_stride;
439 plane_height = 16;
440 plane_width = ybf->y_width;
441
442 /* copy the left and right most columns out */
443 src_ptr1 = y_src;
444 src_ptr2 = src_ptr1 + plane_width - 1;
445 dest_ptr1 = src_ptr1 - Border;
446 dest_ptr2 = src_ptr2 + 1;
447
448 for (i = 0; i < plane_height; i++)
449 {
450 memset(dest_ptr1, src_ptr1[0], Border);
451 memset(dest_ptr2, src_ptr2[0], Border);
452 src_ptr1 += plane_stride;
453 src_ptr2 += plane_stride;
454 dest_ptr1 += plane_stride;
455 dest_ptr2 += plane_stride;
456 }
457
458 /***********/
459 /* U Plane */
460 /***********/
461 plane_stride = ybf->uv_stride;
462 plane_height = 8;
463 plane_width = ybf->uv_width;
464 Border /= 2;
465
466 /* copy the left and right most columns out */
467 src_ptr1 = u_src;
468 src_ptr2 = src_ptr1 + plane_width - 1;
469 dest_ptr1 = src_ptr1 - Border;
470 dest_ptr2 = src_ptr2 + 1;
471
472 for (i = 0; i < plane_height; i++)
473 {
474 memset(dest_ptr1, src_ptr1[0], Border);
475 memset(dest_ptr2, src_ptr2[0], Border);
476 src_ptr1 += plane_stride;
477 src_ptr2 += plane_stride;
478 dest_ptr1 += plane_stride;
479 dest_ptr2 += plane_stride;
480 }
481
482 /***********/
483 /* V Plane */
484 /***********/
485
486 /* copy the left and right most columns out */
487 src_ptr1 = v_src;
488 src_ptr2 = src_ptr1 + plane_width - 1;
489 dest_ptr1 = src_ptr1 - Border;
490 dest_ptr2 = src_ptr2 + 1;
491
492 for (i = 0; i < plane_height; i++)
493 {
494 memset(dest_ptr1, src_ptr1[0], Border);
495 memset(dest_ptr2, src_ptr2[0], Border);
496 src_ptr1 += plane_stride;
497 src_ptr2 += plane_stride;
498 dest_ptr1 += plane_stride;
499 dest_ptr2 += plane_stride;
500 }
501 }
502
decode_mb_rows(VP8D_COMP * pbi)503 static void decode_mb_rows(VP8D_COMP *pbi)
504 {
505 VP8_COMMON *const pc = & pbi->common;
506 MACROBLOCKD *const xd = & pbi->mb;
507
508 MODE_INFO *lf_mic = xd->mode_info_context;
509
510 int ibc = 0;
511 int num_part = 1 << pc->multi_token_partition;
512
513 int recon_yoffset, recon_uvoffset;
514 int mb_row, mb_col;
515 int mb_idx = 0;
516
517 YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME];
518
519 int recon_y_stride = yv12_fb_new->y_stride;
520 int recon_uv_stride = yv12_fb_new->uv_stride;
521
522 unsigned char *ref_buffer[MAX_REF_FRAMES][3];
523 unsigned char *dst_buffer[3];
524 unsigned char *lf_dst[3];
525 unsigned char *eb_dst[3];
526 int i;
527 int ref_fb_corrupted[MAX_REF_FRAMES];
528
529 ref_fb_corrupted[INTRA_FRAME] = 0;
530
531 for(i = 1; i < MAX_REF_FRAMES; i++)
532 {
533 YV12_BUFFER_CONFIG *this_fb = pbi->dec_fb_ref[i];
534
535 ref_buffer[i][0] = this_fb->y_buffer;
536 ref_buffer[i][1] = this_fb->u_buffer;
537 ref_buffer[i][2] = this_fb->v_buffer;
538
539 ref_fb_corrupted[i] = this_fb->corrupted;
540 }
541
542 /* Set up the buffer pointers */
543 eb_dst[0] = lf_dst[0] = dst_buffer[0] = yv12_fb_new->y_buffer;
544 eb_dst[1] = lf_dst[1] = dst_buffer[1] = yv12_fb_new->u_buffer;
545 eb_dst[2] = lf_dst[2] = dst_buffer[2] = yv12_fb_new->v_buffer;
546
547 xd->up_available = 0;
548
549 /* Initialize the loop filter for this frame. */
550 if(pc->filter_level)
551 vp8_loop_filter_frame_init(pc, xd, pc->filter_level);
552
553 vp8_setup_intra_recon_top_line(yv12_fb_new);
554
555 /* Decode the individual macro block */
556 for (mb_row = 0; mb_row < pc->mb_rows; mb_row++)
557 {
558 if (num_part > 1)
559 {
560 xd->current_bc = & pbi->mbc[ibc];
561 ibc++;
562
563 if (ibc == num_part)
564 ibc = 0;
565 }
566
567 recon_yoffset = mb_row * recon_y_stride * 16;
568 recon_uvoffset = mb_row * recon_uv_stride * 8;
569
570 /* reset contexts */
571 xd->above_context = pc->above_context;
572 memset(xd->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
573
574 xd->left_available = 0;
575
576 xd->mb_to_top_edge = -((mb_row * 16) << 3);
577 xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
578
579 xd->recon_above[0] = dst_buffer[0] + recon_yoffset;
580 xd->recon_above[1] = dst_buffer[1] + recon_uvoffset;
581 xd->recon_above[2] = dst_buffer[2] + recon_uvoffset;
582
583 xd->recon_left[0] = xd->recon_above[0] - 1;
584 xd->recon_left[1] = xd->recon_above[1] - 1;
585 xd->recon_left[2] = xd->recon_above[2] - 1;
586
587 xd->recon_above[0] -= xd->dst.y_stride;
588 xd->recon_above[1] -= xd->dst.uv_stride;
589 xd->recon_above[2] -= xd->dst.uv_stride;
590
591 /* TODO: move to outside row loop */
592 xd->recon_left_stride[0] = xd->dst.y_stride;
593 xd->recon_left_stride[1] = xd->dst.uv_stride;
594
595 setup_intra_recon_left(xd->recon_left[0], xd->recon_left[1],
596 xd->recon_left[2], xd->dst.y_stride,
597 xd->dst.uv_stride);
598
599 for (mb_col = 0; mb_col < pc->mb_cols; mb_col++)
600 {
601 /* Distance of Mb to the various image edges.
602 * These are specified to 8th pel as they are always compared to values
603 * that are in 1/8th pel units
604 */
605 xd->mb_to_left_edge = -((mb_col * 16) << 3);
606 xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
607
608 #if CONFIG_ERROR_CONCEALMENT
609 {
610 int corrupt_residual = (!pbi->independent_partitions &&
611 pbi->frame_corrupt_residual) ||
612 vp8dx_bool_error(xd->current_bc);
613 if (pbi->ec_active &&
614 xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME &&
615 corrupt_residual)
616 {
617 /* We have an intra block with corrupt coefficients, better to
618 * conceal with an inter block. Interpolate MVs from neighboring
619 * MBs.
620 *
621 * Note that for the first mb with corrupt residual in a frame,
622 * we might not discover that before decoding the residual. That
623 * happens after this check, and therefore no inter concealment
624 * will be done.
625 */
626 vp8_interpolate_motion(xd,
627 mb_row, mb_col,
628 pc->mb_rows, pc->mb_cols,
629 pc->mode_info_stride);
630 }
631 }
632 #endif
633
634 xd->dst.y_buffer = dst_buffer[0] + recon_yoffset;
635 xd->dst.u_buffer = dst_buffer[1] + recon_uvoffset;
636 xd->dst.v_buffer = dst_buffer[2] + recon_uvoffset;
637
638 if (xd->mode_info_context->mbmi.ref_frame >= LAST_FRAME) {
639 const MV_REFERENCE_FRAME ref = xd->mode_info_context->mbmi.ref_frame;
640 xd->pre.y_buffer = ref_buffer[ref][0] + recon_yoffset;
641 xd->pre.u_buffer = ref_buffer[ref][1] + recon_uvoffset;
642 xd->pre.v_buffer = ref_buffer[ref][2] + recon_uvoffset;
643 } else {
644 // ref_frame is INTRA_FRAME, pre buffer should not be used.
645 xd->pre.y_buffer = 0;
646 xd->pre.u_buffer = 0;
647 xd->pre.v_buffer = 0;
648 }
649
650 /* propagate errors from reference frames */
651 xd->corrupted |= ref_fb_corrupted[xd->mode_info_context->mbmi.ref_frame];
652
653 decode_macroblock(pbi, xd, mb_idx);
654
655 mb_idx++;
656 xd->left_available = 1;
657
658 /* check if the boolean decoder has suffered an error */
659 xd->corrupted |= vp8dx_bool_error(xd->current_bc);
660
661 xd->recon_above[0] += 16;
662 xd->recon_above[1] += 8;
663 xd->recon_above[2] += 8;
664 xd->recon_left[0] += 16;
665 xd->recon_left[1] += 8;
666 xd->recon_left[2] += 8;
667
668 recon_yoffset += 16;
669 recon_uvoffset += 8;
670
671 ++xd->mode_info_context; /* next mb */
672
673 xd->above_context++;
674 }
675
676 /* adjust to the next row of mbs */
677 vp8_extend_mb_row(yv12_fb_new, xd->dst.y_buffer + 16,
678 xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
679
680 ++xd->mode_info_context; /* skip prediction column */
681 xd->up_available = 1;
682
683 if(pc->filter_level)
684 {
685 if(mb_row > 0)
686 {
687 if (pc->filter_type == NORMAL_LOOPFILTER)
688 vp8_loop_filter_row_normal(pc, lf_mic, mb_row-1,
689 recon_y_stride, recon_uv_stride,
690 lf_dst[0], lf_dst[1], lf_dst[2]);
691 else
692 vp8_loop_filter_row_simple(pc, lf_mic, mb_row-1,
693 recon_y_stride, recon_uv_stride,
694 lf_dst[0], lf_dst[1], lf_dst[2]);
695 if(mb_row > 1)
696 {
697 yv12_extend_frame_left_right_c(yv12_fb_new,
698 eb_dst[0],
699 eb_dst[1],
700 eb_dst[2]);
701
702 eb_dst[0] += recon_y_stride * 16;
703 eb_dst[1] += recon_uv_stride * 8;
704 eb_dst[2] += recon_uv_stride * 8;
705 }
706
707 lf_dst[0] += recon_y_stride * 16;
708 lf_dst[1] += recon_uv_stride * 8;
709 lf_dst[2] += recon_uv_stride * 8;
710 lf_mic += pc->mb_cols;
711 lf_mic++; /* Skip border mb */
712 }
713 }
714 else
715 {
716 if(mb_row > 0)
717 {
718 /**/
719 yv12_extend_frame_left_right_c(yv12_fb_new,
720 eb_dst[0],
721 eb_dst[1],
722 eb_dst[2]);
723 eb_dst[0] += recon_y_stride * 16;
724 eb_dst[1] += recon_uv_stride * 8;
725 eb_dst[2] += recon_uv_stride * 8;
726 }
727 }
728 }
729
730 if(pc->filter_level)
731 {
732 if (pc->filter_type == NORMAL_LOOPFILTER)
733 vp8_loop_filter_row_normal(pc, lf_mic, mb_row-1, recon_y_stride,
734 recon_uv_stride, lf_dst[0], lf_dst[1],
735 lf_dst[2]);
736 else
737 vp8_loop_filter_row_simple(pc, lf_mic, mb_row-1, recon_y_stride,
738 recon_uv_stride, lf_dst[0], lf_dst[1],
739 lf_dst[2]);
740
741 yv12_extend_frame_left_right_c(yv12_fb_new,
742 eb_dst[0],
743 eb_dst[1],
744 eb_dst[2]);
745 eb_dst[0] += recon_y_stride * 16;
746 eb_dst[1] += recon_uv_stride * 8;
747 eb_dst[2] += recon_uv_stride * 8;
748 }
749 yv12_extend_frame_left_right_c(yv12_fb_new,
750 eb_dst[0],
751 eb_dst[1],
752 eb_dst[2]);
753 yv12_extend_frame_top_c(yv12_fb_new);
754 yv12_extend_frame_bottom_c(yv12_fb_new);
755
756 }
757
read_partition_size(VP8D_COMP * pbi,const unsigned char * cx_size)758 static unsigned int read_partition_size(VP8D_COMP *pbi,
759 const unsigned char *cx_size)
760 {
761 unsigned char temp[3];
762 if (pbi->decrypt_cb)
763 {
764 pbi->decrypt_cb(pbi->decrypt_state, cx_size, temp, 3);
765 cx_size = temp;
766 }
767 return cx_size[0] + (cx_size[1] << 8) + (cx_size[2] << 16);
768 }
769
read_is_valid(const unsigned char * start,size_t len,const unsigned char * end)770 static int read_is_valid(const unsigned char *start,
771 size_t len,
772 const unsigned char *end)
773 {
774 return (start + len > start && start + len <= end);
775 }
776
read_available_partition_size(VP8D_COMP * pbi,const unsigned char * token_part_sizes,const unsigned char * fragment_start,const unsigned char * first_fragment_end,const unsigned char * fragment_end,int i,int num_part)777 static unsigned int read_available_partition_size(
778 VP8D_COMP *pbi,
779 const unsigned char *token_part_sizes,
780 const unsigned char *fragment_start,
781 const unsigned char *first_fragment_end,
782 const unsigned char *fragment_end,
783 int i,
784 int num_part)
785 {
786 VP8_COMMON* pc = &pbi->common;
787 const unsigned char *partition_size_ptr = token_part_sizes + i * 3;
788 unsigned int partition_size = 0;
789 ptrdiff_t bytes_left = fragment_end - fragment_start;
790 /* Calculate the length of this partition. The last partition
791 * size is implicit. If the partition size can't be read, then
792 * either use the remaining data in the buffer (for EC mode)
793 * or throw an error.
794 */
795 if (i < num_part - 1)
796 {
797 if (read_is_valid(partition_size_ptr, 3, first_fragment_end))
798 partition_size = read_partition_size(pbi, partition_size_ptr);
799 else if (pbi->ec_active)
800 partition_size = (unsigned int)bytes_left;
801 else
802 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
803 "Truncated partition size data");
804 }
805 else
806 partition_size = (unsigned int)bytes_left;
807
808 /* Validate the calculated partition length. If the buffer
809 * described by the partition can't be fully read, then restrict
810 * it to the portion that can be (for EC mode) or throw an error.
811 */
812 if (!read_is_valid(fragment_start, partition_size, fragment_end))
813 {
814 if (pbi->ec_active)
815 partition_size = (unsigned int)bytes_left;
816 else
817 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
818 "Truncated packet or corrupt partition "
819 "%d length", i + 1);
820 }
821 return partition_size;
822 }
823
824
setup_token_decoder(VP8D_COMP * pbi,const unsigned char * token_part_sizes)825 static void setup_token_decoder(VP8D_COMP *pbi,
826 const unsigned char* token_part_sizes)
827 {
828 vp8_reader *bool_decoder = &pbi->mbc[0];
829 unsigned int partition_idx;
830 unsigned int fragment_idx;
831 unsigned int num_token_partitions;
832 const unsigned char *first_fragment_end = pbi->fragments.ptrs[0] +
833 pbi->fragments.sizes[0];
834
835 TOKEN_PARTITION multi_token_partition =
836 (TOKEN_PARTITION)vp8_read_literal(&pbi->mbc[8], 2);
837 if (!vp8dx_bool_error(&pbi->mbc[8]))
838 pbi->common.multi_token_partition = multi_token_partition;
839 num_token_partitions = 1 << pbi->common.multi_token_partition;
840
841 /* Check for partitions within the fragments and unpack the fragments
842 * so that each fragment pointer points to its corresponding partition. */
843 for (fragment_idx = 0; fragment_idx < pbi->fragments.count; ++fragment_idx)
844 {
845 unsigned int fragment_size = pbi->fragments.sizes[fragment_idx];
846 const unsigned char *fragment_end = pbi->fragments.ptrs[fragment_idx] +
847 fragment_size;
848 /* Special case for handling the first partition since we have already
849 * read its size. */
850 if (fragment_idx == 0)
851 {
852 /* Size of first partition + token partition sizes element */
853 ptrdiff_t ext_first_part_size = token_part_sizes -
854 pbi->fragments.ptrs[0] + 3 * (num_token_partitions - 1);
855 fragment_size -= (unsigned int)ext_first_part_size;
856 if (fragment_size > 0)
857 {
858 pbi->fragments.sizes[0] = (unsigned int)ext_first_part_size;
859 /* The fragment contains an additional partition. Move to
860 * next. */
861 fragment_idx++;
862 pbi->fragments.ptrs[fragment_idx] = pbi->fragments.ptrs[0] +
863 pbi->fragments.sizes[0];
864 }
865 }
866 /* Split the chunk into partitions read from the bitstream */
867 while (fragment_size > 0)
868 {
869 ptrdiff_t partition_size = read_available_partition_size(
870 pbi,
871 token_part_sizes,
872 pbi->fragments.ptrs[fragment_idx],
873 first_fragment_end,
874 fragment_end,
875 fragment_idx - 1,
876 num_token_partitions);
877 pbi->fragments.sizes[fragment_idx] = (unsigned int)partition_size;
878 fragment_size -= (unsigned int)partition_size;
879 assert(fragment_idx <= num_token_partitions);
880 if (fragment_size > 0)
881 {
882 /* The fragment contains an additional partition.
883 * Move to next. */
884 fragment_idx++;
885 pbi->fragments.ptrs[fragment_idx] =
886 pbi->fragments.ptrs[fragment_idx - 1] + partition_size;
887 }
888 }
889 }
890
891 pbi->fragments.count = num_token_partitions + 1;
892
893 for (partition_idx = 1; partition_idx < pbi->fragments.count; ++partition_idx)
894 {
895 if (vp8dx_start_decode(bool_decoder,
896 pbi->fragments.ptrs[partition_idx],
897 pbi->fragments.sizes[partition_idx],
898 pbi->decrypt_cb, pbi->decrypt_state))
899 vpx_internal_error(&pbi->common.error, VPX_CODEC_MEM_ERROR,
900 "Failed to allocate bool decoder %d",
901 partition_idx);
902
903 bool_decoder++;
904 }
905
906 #if CONFIG_MULTITHREAD
907 /* Clamp number of decoder threads */
908 if (pbi->decoding_thread_count > num_token_partitions - 1) {
909 pbi->decoding_thread_count = num_token_partitions - 1;
910 }
911 if (pbi->decoding_thread_count > pbi->common.mb_rows - 1) {
912 pbi->decoding_thread_count = pbi->common.mb_rows - 1;
913 }
914 #endif
915 }
916
917
init_frame(VP8D_COMP * pbi)918 static void init_frame(VP8D_COMP *pbi)
919 {
920 VP8_COMMON *const pc = & pbi->common;
921 MACROBLOCKD *const xd = & pbi->mb;
922
923 if (pc->frame_type == KEY_FRAME)
924 {
925 /* Various keyframe initializations */
926 memcpy(pc->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
927
928 vp8_init_mbmode_probs(pc);
929
930 vp8_default_coef_probs(pc);
931
932 /* reset the segment feature data to 0 with delta coding (Default state). */
933 memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
934 xd->mb_segement_abs_delta = SEGMENT_DELTADATA;
935
936 /* reset the mode ref deltasa for loop filter */
937 memset(xd->ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
938 memset(xd->mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
939
940 /* All buffers are implicitly updated on key frames. */
941 pc->refresh_golden_frame = 1;
942 pc->refresh_alt_ref_frame = 1;
943 pc->copy_buffer_to_gf = 0;
944 pc->copy_buffer_to_arf = 0;
945
946 /* Note that Golden and Altref modes cannot be used on a key frame so
947 * ref_frame_sign_bias[] is undefined and meaningless
948 */
949 pc->ref_frame_sign_bias[GOLDEN_FRAME] = 0;
950 pc->ref_frame_sign_bias[ALTREF_FRAME] = 0;
951 }
952 else
953 {
954 /* To enable choice of different interploation filters */
955 if (!pc->use_bilinear_mc_filter)
956 {
957 xd->subpixel_predict = vp8_sixtap_predict4x4;
958 xd->subpixel_predict8x4 = vp8_sixtap_predict8x4;
959 xd->subpixel_predict8x8 = vp8_sixtap_predict8x8;
960 xd->subpixel_predict16x16 = vp8_sixtap_predict16x16;
961 }
962 else
963 {
964 xd->subpixel_predict = vp8_bilinear_predict4x4;
965 xd->subpixel_predict8x4 = vp8_bilinear_predict8x4;
966 xd->subpixel_predict8x8 = vp8_bilinear_predict8x8;
967 xd->subpixel_predict16x16 = vp8_bilinear_predict16x16;
968 }
969
970 if (pbi->decoded_key_frame && pbi->ec_enabled && !pbi->ec_active)
971 pbi->ec_active = 1;
972 }
973
974 xd->left_context = &pc->left_context;
975 xd->mode_info_context = pc->mi;
976 xd->frame_type = pc->frame_type;
977 xd->mode_info_context->mbmi.mode = DC_PRED;
978 xd->mode_info_stride = pc->mode_info_stride;
979 xd->corrupted = 0; /* init without corruption */
980
981 xd->fullpixel_mask = 0xffffffff;
982 if(pc->full_pixel)
983 xd->fullpixel_mask = 0xfffffff8;
984
985 }
986
vp8_decode_frame(VP8D_COMP * pbi)987 int vp8_decode_frame(VP8D_COMP *pbi)
988 {
989 vp8_reader *const bc = &pbi->mbc[8];
990 VP8_COMMON *const pc = &pbi->common;
991 MACROBLOCKD *const xd = &pbi->mb;
992 const unsigned char *data = pbi->fragments.ptrs[0];
993 const unsigned char *data_end = data + pbi->fragments.sizes[0];
994 ptrdiff_t first_partition_length_in_bytes;
995
996 int i, j, k, l;
997 const int *const mb_feature_data_bits = vp8_mb_feature_data_bits;
998 int corrupt_tokens = 0;
999 int prev_independent_partitions = pbi->independent_partitions;
1000
1001 YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME];
1002
1003 /* start with no corruption of current frame */
1004 xd->corrupted = 0;
1005 yv12_fb_new->corrupted = 0;
1006
1007 if (data_end - data < 3)
1008 {
1009 if (!pbi->ec_active)
1010 {
1011 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
1012 "Truncated packet");
1013 }
1014
1015 /* Declare the missing frame as an inter frame since it will
1016 be handled as an inter frame when we have estimated its
1017 motion vectors. */
1018 pc->frame_type = INTER_FRAME;
1019 pc->version = 0;
1020 pc->show_frame = 1;
1021 first_partition_length_in_bytes = 0;
1022 }
1023 else
1024 {
1025 unsigned char clear_buffer[10];
1026 const unsigned char *clear = data;
1027 if (pbi->decrypt_cb)
1028 {
1029 int n = (int)VPXMIN(sizeof(clear_buffer), data_end - data);
1030 pbi->decrypt_cb(pbi->decrypt_state, data, clear_buffer, n);
1031 clear = clear_buffer;
1032 }
1033
1034 pc->frame_type = (FRAME_TYPE)(clear[0] & 1);
1035 pc->version = (clear[0] >> 1) & 7;
1036 pc->show_frame = (clear[0] >> 4) & 1;
1037 first_partition_length_in_bytes =
1038 (clear[0] | (clear[1] << 8) | (clear[2] << 16)) >> 5;
1039
1040 if (!pbi->ec_active &&
1041 (data + first_partition_length_in_bytes > data_end
1042 || data + first_partition_length_in_bytes < data))
1043 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
1044 "Truncated packet or corrupt partition 0 length");
1045
1046 data += 3;
1047 clear += 3;
1048
1049 vp8_setup_version(pc);
1050
1051
1052 if (pc->frame_type == KEY_FRAME)
1053 {
1054 /* vet via sync code */
1055 /* When error concealment is enabled we should only check the sync
1056 * code if we have enough bits available
1057 */
1058 if (!pbi->ec_active || data + 3 < data_end)
1059 {
1060 if (clear[0] != 0x9d || clear[1] != 0x01 || clear[2] != 0x2a)
1061 vpx_internal_error(&pc->error, VPX_CODEC_UNSUP_BITSTREAM,
1062 "Invalid frame sync code");
1063 }
1064
1065 /* If error concealment is enabled we should only parse the new size
1066 * if we have enough data. Otherwise we will end up with the wrong
1067 * size.
1068 */
1069 if (!pbi->ec_active || data + 6 < data_end)
1070 {
1071 pc->Width = (clear[3] | (clear[4] << 8)) & 0x3fff;
1072 pc->horiz_scale = clear[4] >> 6;
1073 pc->Height = (clear[5] | (clear[6] << 8)) & 0x3fff;
1074 pc->vert_scale = clear[6] >> 6;
1075 }
1076 data += 7;
1077 }
1078 else
1079 {
1080 memcpy(&xd->pre, yv12_fb_new, sizeof(YV12_BUFFER_CONFIG));
1081 memcpy(&xd->dst, yv12_fb_new, sizeof(YV12_BUFFER_CONFIG));
1082 }
1083 }
1084 if ((!pbi->decoded_key_frame && pc->frame_type != KEY_FRAME))
1085 {
1086 return -1;
1087 }
1088
1089 init_frame(pbi);
1090
1091 if (vp8dx_start_decode(bc, data, (unsigned int)(data_end - data),
1092 pbi->decrypt_cb, pbi->decrypt_state))
1093 vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
1094 "Failed to allocate bool decoder 0");
1095 if (pc->frame_type == KEY_FRAME) {
1096 (void)vp8_read_bit(bc); // colorspace
1097 pc->clamp_type = (CLAMP_TYPE)vp8_read_bit(bc);
1098 }
1099
1100 /* Is segmentation enabled */
1101 xd->segmentation_enabled = (unsigned char)vp8_read_bit(bc);
1102
1103 if (xd->segmentation_enabled)
1104 {
1105 /* Signal whether or not the segmentation map is being explicitly updated this frame. */
1106 xd->update_mb_segmentation_map = (unsigned char)vp8_read_bit(bc);
1107 xd->update_mb_segmentation_data = (unsigned char)vp8_read_bit(bc);
1108
1109 if (xd->update_mb_segmentation_data)
1110 {
1111 xd->mb_segement_abs_delta = (unsigned char)vp8_read_bit(bc);
1112
1113 memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
1114
1115 /* For each segmentation feature (Quant and loop filter level) */
1116 for (i = 0; i < MB_LVL_MAX; i++)
1117 {
1118 for (j = 0; j < MAX_MB_SEGMENTS; j++)
1119 {
1120 /* Frame level data */
1121 if (vp8_read_bit(bc))
1122 {
1123 xd->segment_feature_data[i][j] = (signed char)vp8_read_literal(bc, mb_feature_data_bits[i]);
1124
1125 if (vp8_read_bit(bc))
1126 xd->segment_feature_data[i][j] = -xd->segment_feature_data[i][j];
1127 }
1128 else
1129 xd->segment_feature_data[i][j] = 0;
1130 }
1131 }
1132 }
1133
1134 if (xd->update_mb_segmentation_map)
1135 {
1136 /* Which macro block level features are enabled */
1137 memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));
1138
1139 /* Read the probs used to decode the segment id for each macro block. */
1140 for (i = 0; i < MB_FEATURE_TREE_PROBS; i++)
1141 {
1142 /* If not explicitly set value is defaulted to 255 by memset above */
1143 if (vp8_read_bit(bc))
1144 xd->mb_segment_tree_probs[i] = (vp8_prob)vp8_read_literal(bc, 8);
1145 }
1146 }
1147 }
1148 else
1149 {
1150 /* No segmentation updates on this frame */
1151 xd->update_mb_segmentation_map = 0;
1152 xd->update_mb_segmentation_data = 0;
1153 }
1154
1155 /* Read the loop filter level and type */
1156 pc->filter_type = (LOOPFILTERTYPE) vp8_read_bit(bc);
1157 pc->filter_level = vp8_read_literal(bc, 6);
1158 pc->sharpness_level = vp8_read_literal(bc, 3);
1159
1160 /* Read in loop filter deltas applied at the MB level based on mode or ref frame. */
1161 xd->mode_ref_lf_delta_update = 0;
1162 xd->mode_ref_lf_delta_enabled = (unsigned char)vp8_read_bit(bc);
1163
1164 if (xd->mode_ref_lf_delta_enabled)
1165 {
1166 /* Do the deltas need to be updated */
1167 xd->mode_ref_lf_delta_update = (unsigned char)vp8_read_bit(bc);
1168
1169 if (xd->mode_ref_lf_delta_update)
1170 {
1171 /* Send update */
1172 for (i = 0; i < MAX_REF_LF_DELTAS; i++)
1173 {
1174 if (vp8_read_bit(bc))
1175 {
1176 /*sign = vp8_read_bit( bc );*/
1177 xd->ref_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
1178
1179 if (vp8_read_bit(bc)) /* Apply sign */
1180 xd->ref_lf_deltas[i] = xd->ref_lf_deltas[i] * -1;
1181 }
1182 }
1183
1184 /* Send update */
1185 for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
1186 {
1187 if (vp8_read_bit(bc))
1188 {
1189 /*sign = vp8_read_bit( bc );*/
1190 xd->mode_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
1191
1192 if (vp8_read_bit(bc)) /* Apply sign */
1193 xd->mode_lf_deltas[i] = xd->mode_lf_deltas[i] * -1;
1194 }
1195 }
1196 }
1197 }
1198
1199 setup_token_decoder(pbi, data + first_partition_length_in_bytes);
1200
1201 xd->current_bc = &pbi->mbc[0];
1202
1203 /* Read the default quantizers. */
1204 {
1205 int Q, q_update;
1206
1207 Q = vp8_read_literal(bc, 7); /* AC 1st order Q = default */
1208 pc->base_qindex = Q;
1209 q_update = 0;
1210 pc->y1dc_delta_q = get_delta_q(bc, pc->y1dc_delta_q, &q_update);
1211 pc->y2dc_delta_q = get_delta_q(bc, pc->y2dc_delta_q, &q_update);
1212 pc->y2ac_delta_q = get_delta_q(bc, pc->y2ac_delta_q, &q_update);
1213 pc->uvdc_delta_q = get_delta_q(bc, pc->uvdc_delta_q, &q_update);
1214 pc->uvac_delta_q = get_delta_q(bc, pc->uvac_delta_q, &q_update);
1215
1216 if (q_update)
1217 vp8cx_init_de_quantizer(pbi);
1218
1219 /* MB level dequantizer setup */
1220 vp8_mb_init_dequantizer(pbi, &pbi->mb);
1221 }
1222
1223 /* Determine if the golden frame or ARF buffer should be updated and how.
1224 * For all non key frames the GF and ARF refresh flags and sign bias
1225 * flags must be set explicitly.
1226 */
1227 if (pc->frame_type != KEY_FRAME)
1228 {
1229 /* Should the GF or ARF be updated from the current frame */
1230 pc->refresh_golden_frame = vp8_read_bit(bc);
1231 #if CONFIG_ERROR_CONCEALMENT
1232 /* Assume we shouldn't refresh golden if the bit is missing */
1233 xd->corrupted |= vp8dx_bool_error(bc);
1234 if (pbi->ec_active && xd->corrupted)
1235 pc->refresh_golden_frame = 0;
1236 #endif
1237
1238 pc->refresh_alt_ref_frame = vp8_read_bit(bc);
1239 #if CONFIG_ERROR_CONCEALMENT
1240 /* Assume we shouldn't refresh altref if the bit is missing */
1241 xd->corrupted |= vp8dx_bool_error(bc);
1242 if (pbi->ec_active && xd->corrupted)
1243 pc->refresh_alt_ref_frame = 0;
1244 #endif
1245
1246 /* Buffer to buffer copy flags. */
1247 pc->copy_buffer_to_gf = 0;
1248
1249 if (!pc->refresh_golden_frame)
1250 pc->copy_buffer_to_gf = vp8_read_literal(bc, 2);
1251
1252 #if CONFIG_ERROR_CONCEALMENT
1253 /* Assume we shouldn't copy to the golden if the bit is missing */
1254 xd->corrupted |= vp8dx_bool_error(bc);
1255 if (pbi->ec_active && xd->corrupted)
1256 pc->copy_buffer_to_gf = 0;
1257 #endif
1258
1259 pc->copy_buffer_to_arf = 0;
1260
1261 if (!pc->refresh_alt_ref_frame)
1262 pc->copy_buffer_to_arf = vp8_read_literal(bc, 2);
1263
1264 #if CONFIG_ERROR_CONCEALMENT
1265 /* Assume we shouldn't copy to the alt-ref if the bit is missing */
1266 xd->corrupted |= vp8dx_bool_error(bc);
1267 if (pbi->ec_active && xd->corrupted)
1268 pc->copy_buffer_to_arf = 0;
1269 #endif
1270
1271
1272 pc->ref_frame_sign_bias[GOLDEN_FRAME] = vp8_read_bit(bc);
1273 pc->ref_frame_sign_bias[ALTREF_FRAME] = vp8_read_bit(bc);
1274 }
1275
1276 pc->refresh_entropy_probs = vp8_read_bit(bc);
1277 #if CONFIG_ERROR_CONCEALMENT
1278 /* Assume we shouldn't refresh the probabilities if the bit is
1279 * missing */
1280 xd->corrupted |= vp8dx_bool_error(bc);
1281 if (pbi->ec_active && xd->corrupted)
1282 pc->refresh_entropy_probs = 0;
1283 #endif
1284 if (pc->refresh_entropy_probs == 0)
1285 {
1286 memcpy(&pc->lfc, &pc->fc, sizeof(pc->fc));
1287 }
1288
1289 pc->refresh_last_frame = pc->frame_type == KEY_FRAME || vp8_read_bit(bc);
1290
1291 #if CONFIG_ERROR_CONCEALMENT
1292 /* Assume we should refresh the last frame if the bit is missing */
1293 xd->corrupted |= vp8dx_bool_error(bc);
1294 if (pbi->ec_active && xd->corrupted)
1295 pc->refresh_last_frame = 1;
1296 #endif
1297
1298 if (0)
1299 {
1300 FILE *z = fopen("decodestats.stt", "a");
1301 fprintf(z, "%6d F:%d,G:%d,A:%d,L:%d,Q:%d\n",
1302 pc->current_video_frame,
1303 pc->frame_type,
1304 pc->refresh_golden_frame,
1305 pc->refresh_alt_ref_frame,
1306 pc->refresh_last_frame,
1307 pc->base_qindex);
1308 fclose(z);
1309 }
1310
1311 {
1312 pbi->independent_partitions = 1;
1313
1314 /* read coef probability tree */
1315 for (i = 0; i < BLOCK_TYPES; i++)
1316 for (j = 0; j < COEF_BANDS; j++)
1317 for (k = 0; k < PREV_COEF_CONTEXTS; k++)
1318 for (l = 0; l < ENTROPY_NODES; l++)
1319 {
1320
1321 vp8_prob *const p = pc->fc.coef_probs [i][j][k] + l;
1322
1323 if (vp8_read(bc, vp8_coef_update_probs [i][j][k][l]))
1324 {
1325 *p = (vp8_prob)vp8_read_literal(bc, 8);
1326
1327 }
1328 if (k > 0 && *p != pc->fc.coef_probs[i][j][k-1][l])
1329 pbi->independent_partitions = 0;
1330
1331 }
1332 }
1333
1334 /* clear out the coeff buffer */
1335 memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
1336
1337 vp8_decode_mode_mvs(pbi);
1338
1339 #if CONFIG_ERROR_CONCEALMENT
1340 if (pbi->ec_active &&
1341 pbi->mvs_corrupt_from_mb < (unsigned int)pc->mb_cols * pc->mb_rows)
1342 {
1343 /* Motion vectors are missing in this frame. We will try to estimate
1344 * them and then continue decoding the frame as usual */
1345 vp8_estimate_missing_mvs(pbi);
1346 }
1347 #endif
1348
1349 memset(pc->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) * pc->mb_cols);
1350 pbi->frame_corrupt_residual = 0;
1351
1352 #if CONFIG_MULTITHREAD
1353 if (pbi->b_multithreaded_rd && pc->multi_token_partition != ONE_PARTITION)
1354 {
1355 unsigned int thread;
1356 vp8mt_decode_mb_rows(pbi, xd);
1357 vp8_yv12_extend_frame_borders(yv12_fb_new);
1358 for (thread = 0; thread < pbi->decoding_thread_count; ++thread)
1359 corrupt_tokens |= pbi->mb_row_di[thread].mbd.corrupted;
1360 }
1361 else
1362 #endif
1363 {
1364 decode_mb_rows(pbi);
1365 corrupt_tokens |= xd->corrupted;
1366 }
1367
1368 /* Collect information about decoder corruption. */
1369 /* 1. Check first boolean decoder for errors. */
1370 yv12_fb_new->corrupted = vp8dx_bool_error(bc);
1371 /* 2. Check the macroblock information */
1372 yv12_fb_new->corrupted |= corrupt_tokens;
1373
1374 if (!pbi->decoded_key_frame)
1375 {
1376 if (pc->frame_type == KEY_FRAME &&
1377 !yv12_fb_new->corrupted)
1378 pbi->decoded_key_frame = 1;
1379 else
1380 vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME,
1381 "A stream must start with a complete key frame");
1382 }
1383
1384 /* vpx_log("Decoder: Frame Decoded, Size Roughly:%d bytes \n",bc->pos+pbi->bc2.pos); */
1385
1386 if (pc->refresh_entropy_probs == 0)
1387 {
1388 memcpy(&pc->fc, &pc->lfc, sizeof(pc->fc));
1389 pbi->independent_partitions = prev_independent_partitions;
1390 }
1391
1392 #ifdef PACKET_TESTING
1393 {
1394 FILE *f = fopen("decompressor.VP8", "ab");
1395 unsigned int size = pbi->bc2.pos + pbi->bc.pos + 8;
1396 fwrite((void *) &size, 4, 1, f);
1397 fwrite((void *) pbi->Source, size, 1, f);
1398 fclose(f);
1399 }
1400 #endif
1401
1402 return 0;
1403 }
1404