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