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