Detect toolchain based on gcc -dumpmachine
[profile/ivi/libvpx.git] / vp8 / decoder / decodframe.c
1 /*
2  *  Copyright (c) 2010 The VP8 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 "onyxd_int.h"
13 #include "header.h"
14 #include "reconintra.h"
15 #include "reconintra4x4.h"
16 #include "recon.h"
17 #include "reconinter.h"
18 #include "dequantize.h"
19 #include "detokenize.h"
20 #include "invtrans.h"
21 #include "alloccommon.h"
22 #include "entropymode.h"
23 #include "quant_common.h"
24 #include "segmentation_common.h"
25 #include "setupintrarecon.h"
26 #include "demode.h"
27 #include "decodemv.h"
28 #include "extend.h"
29 #include "vpx_mem/vpx_mem.h"
30 #include "idct.h"
31 #include "dequantize.h"
32 #include "predictdc.h"
33 #include "threading.h"
34 #include "decoderthreading.h"
35 #include "dboolhuff.h"
36
37 #include <assert.h>
38 #include <stdio.h>
39
40 void vp8cx_init_de_quantizer(VP8D_COMP *pbi)
41 {
42     int r, c;
43     int i;
44     int Q;
45     VP8_COMMON *const pc = & pbi->common;
46
47     for (Q = 0; Q < QINDEX_RANGE; Q++)
48     {
49         pc->Y1dequant[Q][0][0] = (short)vp8_dc_quant(Q, pc->y1dc_delta_q);
50         pc->Y2dequant[Q][0][0] = (short)vp8_dc2quant(Q, pc->y2dc_delta_q);
51         pc->UVdequant[Q][0][0] = (short)vp8_dc_uv_quant(Q, pc->uvdc_delta_q);
52
53         // all the ac values = ;
54         for (i = 1; i < 16; i++)
55         {
56             int rc = vp8_default_zig_zag1d[i];
57             r = (rc >> 2);
58             c = (rc & 3);
59
60             pc->Y1dequant[Q][r][c] = (short)vp8_ac_yquant(Q);
61             pc->Y2dequant[Q][r][c] = (short)vp8_ac2quant(Q, pc->y2ac_delta_q);
62             pc->UVdequant[Q][r][c] = (short)vp8_ac_uv_quant(Q, pc->uvac_delta_q);
63         }
64     }
65 }
66
67 static void mb_init_dequantizer(VP8D_COMP *pbi, MACROBLOCKD *xd)
68 {
69     int i;
70     int QIndex;
71     MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
72     VP8_COMMON *const pc = & pbi->common;
73
74     // Decide whether to use the default or alternate baseline Q value.
75     if (xd->segmentation_enabled)
76     {
77         // Abs Value
78         if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA)
79             QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
80
81         // Delta Value
82         else
83         {
84             QIndex = pc->base_qindex + xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
85             QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0;    // Clamp to valid range
86         }
87     }
88     else
89         QIndex = pc->base_qindex;
90
91     // Set up the block level dequant pointers
92     for (i = 0; i < 16; i++)
93     {
94         xd->block[i].dequant = pc->Y1dequant[QIndex];
95     }
96
97     for (i = 16; i < 24; i++)
98     {
99         xd->block[i].dequant = pc->UVdequant[QIndex];
100     }
101
102     xd->block[24].dequant = pc->Y2dequant[QIndex];
103
104 }
105
106 #if CONFIG_RUNTIME_CPU_DETECT
107 #define RTCD_VTABLE(x) (&(pbi)->common.rtcd.x)
108 #else
109 #define RTCD_VTABLE(x) NULL
110 #endif
111
112 //skip_recon_mb() is Modified: Instead of writing the result to predictor buffer and then copying it
113 // to dst buffer, we can write the result directly to dst buffer. This eliminates unnecessary copy.
114 static void skip_recon_mb(VP8D_COMP *pbi, MACROBLOCKD *xd)
115 {
116     if (xd->frame_type == KEY_FRAME  ||  xd->mbmi.ref_frame == INTRA_FRAME)
117     {
118
119         vp8_build_intra_predictors_mbuv_s(xd);
120         vp8_build_intra_predictors_mby_s_ptr(xd);
121
122     }
123     else
124     {
125         vp8_build_inter_predictors_mb_s(xd);
126     }
127 }
128
129 static void reconstruct_mb(VP8D_COMP *pbi, MACROBLOCKD *xd)
130 {
131     if (xd->frame_type == KEY_FRAME  ||  xd->mbmi.ref_frame == INTRA_FRAME)
132     {
133         vp8_build_intra_predictors_mbuv(xd);
134
135         if (xd->mbmi.mode != B_PRED)
136         {
137             vp8_build_intra_predictors_mby_ptr(xd);
138             vp8_recon16x16mb(RTCD_VTABLE(recon), xd);
139         }
140         else
141         {
142             vp8_recon_intra4x4mb(RTCD_VTABLE(recon), xd);
143         }
144     }
145     else
146     {
147         vp8_build_inter_predictors_mb(xd);
148         vp8_recon16x16mb(RTCD_VTABLE(recon), xd);
149     }
150 }
151
152
153 static void de_quantand_idct(VP8D_COMP *pbi, MACROBLOCKD *xd)
154 {
155     int i;
156     BLOCKD *b = &xd->block[24];
157
158
159     if (xd->mbmi.mode != B_PRED && xd->mbmi.mode != SPLITMV)
160     {
161         DEQUANT_INVOKE(&pbi->dequant, block)(b);
162
163         // do 2nd order transform on the dc block
164         if (b->eob > 1)
165         {
166             IDCT_INVOKE(RTCD_VTABLE(idct), iwalsh16)(&b->dqcoeff[0], b->diff);
167             ((int *)b->qcoeff)[0] = 0;
168             ((int *)b->qcoeff)[1] = 0;
169             ((int *)b->qcoeff)[2] = 0;
170             ((int *)b->qcoeff)[3] = 0;
171             ((int *)b->qcoeff)[4] = 0;
172             ((int *)b->qcoeff)[5] = 0;
173             ((int *)b->qcoeff)[6] = 0;
174             ((int *)b->qcoeff)[7] = 0;
175         }
176         else
177         {
178             IDCT_INVOKE(RTCD_VTABLE(idct), iwalsh1)(&b->dqcoeff[0], b->diff);
179             ((int *)b->qcoeff)[0] = 0;
180         }
181
182
183         for (i = 0; i < 16; i++)
184         {
185
186             b = &xd->block[i];
187
188             if (b->eob > 1)
189             {
190                 DEQUANT_INVOKE(&pbi->dequant, idct_dc)(b->qcoeff, &b->dequant[0][0], b->diff, 32, xd->block[24].diff[i]);
191             }
192             else
193             {
194                 IDCT_INVOKE(RTCD_VTABLE(idct), idct1_scalar)(xd->block[24].diff[i], b->diff, 32);
195             }
196         }
197
198         for (i = 16; i < 24; i++)
199         {
200             b = &xd->block[i];
201
202             if (b->eob > 1)
203             {
204                 DEQUANT_INVOKE(&pbi->dequant, idct)(b->qcoeff, &b->dequant[0][0], b->diff, 16);
205             }
206             else
207             {
208                 IDCT_INVOKE(RTCD_VTABLE(idct), idct1_scalar)(b->qcoeff[0] * b->dequant[0][0], b->diff, 16);
209                 ((int *)b->qcoeff)[0] = 0;
210             }
211         }
212     }
213     else
214     {
215         for (i = 0; i < 24; i++)
216         {
217
218             b = &xd->block[i];
219
220             if (b->eob > 1)
221             {
222                 DEQUANT_INVOKE(&pbi->dequant, idct)(b->qcoeff, &b->dequant[0][0], b->diff, (32 - (i & 16)));
223             }
224             else
225             {
226                 IDCT_INVOKE(RTCD_VTABLE(idct), idct1_scalar)(b->qcoeff[0] * b->dequant[0][0], b->diff, (32 - (i & 16)));
227                 ((int *)b->qcoeff)[0] = 0;
228             }
229         }
230     }
231 }
232
233 void vp8_decode_macroblock(VP8D_COMP *pbi, MACROBLOCKD *xd)
234 {
235     int eobtotal = 0;
236
237     if (xd->mbmi.mb_skip_coeff)
238     {
239         vp8_reset_mb_tokens_context(xd);
240     }
241     else
242     {
243         eobtotal = vp8_decode_mb_tokens(pbi, xd);
244     }
245
246     xd->mode_info_context->mbmi.dc_diff = 1;
247
248     if (xd->mbmi.mode != B_PRED && xd->mbmi.mode != SPLITMV && eobtotal == 0)
249     {
250         xd->mode_info_context->mbmi.dc_diff = 0;
251         skip_recon_mb(pbi, xd);
252         return;
253     }
254
255     if (xd->segmentation_enabled)
256         mb_init_dequantizer(pbi, xd);
257
258     de_quantand_idct(pbi, xd);
259     reconstruct_mb(pbi, xd);
260 }
261
262 static int get_delta_q(vp8_reader *bc, int prev, int *q_update)
263 {
264     int ret_val = 0;
265
266     if (vp8_read_bit(bc))
267     {
268         ret_val = vp8_read_literal(bc, 4);
269
270         if (vp8_read_bit(bc))
271             ret_val = -ret_val;
272     }
273
274     /* Trigger a quantizer update if the delta-q value has changed */
275     if (ret_val != prev)
276         *q_update = 1;
277
278     return ret_val;
279 }
280
281 #ifdef PACKET_TESTING
282 #include <stdio.h>
283 FILE *vpxlog = 0;
284 #endif
285
286
287
288 void vp8_decode_mb_row(VP8D_COMP *pbi,
289                        VP8_COMMON *pc,
290                        int mb_row,
291                        MACROBLOCKD *xd)
292 {
293
294     int i;
295     int recon_yoffset, recon_uvoffset;
296     int mb_col;
297     int recon_y_stride = pc->last_frame.y_stride;
298     int recon_uv_stride = pc->last_frame.uv_stride;
299
300     vpx_memset(pc->left_context, 0, sizeof(pc->left_context));
301     recon_yoffset = mb_row * recon_y_stride * 16;
302     recon_uvoffset = mb_row * recon_uv_stride * 8;
303     // reset above block coeffs
304
305     xd->above_context[Y1CONTEXT] = pc->above_context[Y1CONTEXT];
306     xd->above_context[UCONTEXT ] = pc->above_context[UCONTEXT];
307     xd->above_context[VCONTEXT ] = pc->above_context[VCONTEXT];
308     xd->above_context[Y2CONTEXT] = pc->above_context[Y2CONTEXT];
309     xd->up_available = (mb_row != 0);
310
311     xd->mb_to_top_edge = -((mb_row * 16)) << 3;
312     xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
313
314     for (mb_col = 0; mb_col < pc->mb_cols; mb_col++)
315     {
316         // Take a copy of the mode and Mv information for this macroblock into the xd->mbmi
317         vpx_memcpy(&xd->mbmi, &xd->mode_info_context->mbmi, 32); //sizeof(MB_MODE_INFO) );
318
319         if (xd->mbmi.mode == SPLITMV || xd->mbmi.mode == B_PRED)
320         {
321             for (i = 0; i < 16; i++)
322             {
323                 BLOCKD *d = &xd->block[i];
324                 vpx_memcpy(&d->bmi, &xd->mode_info_context->bmi[i], sizeof(B_MODE_INFO));
325             }
326         }
327
328         // Distance of Mb to the various image edges.
329         // These specified to 8th pel as they are always compared to values that are in 1/8th pel units
330         xd->mb_to_left_edge = -((mb_col * 16) << 3);
331         xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
332
333         xd->dst.y_buffer = pc->new_frame.y_buffer + recon_yoffset;
334         xd->dst.u_buffer = pc->new_frame.u_buffer + recon_uvoffset;
335         xd->dst.v_buffer = pc->new_frame.v_buffer + recon_uvoffset;
336
337         xd->left_available = (mb_col != 0);
338
339         // Select the appropriate reference frame for this MB
340         if (xd->mbmi.ref_frame == LAST_FRAME)
341         {
342             xd->pre.y_buffer = pc->last_frame.y_buffer + recon_yoffset;
343             xd->pre.u_buffer = pc->last_frame.u_buffer + recon_uvoffset;
344             xd->pre.v_buffer = pc->last_frame.v_buffer + recon_uvoffset;
345         }
346         else if (xd->mbmi.ref_frame == GOLDEN_FRAME)
347         {
348             // Golden frame reconstruction buffer
349             xd->pre.y_buffer = pc->golden_frame.y_buffer + recon_yoffset;
350             xd->pre.u_buffer = pc->golden_frame.u_buffer + recon_uvoffset;
351             xd->pre.v_buffer = pc->golden_frame.v_buffer + recon_uvoffset;
352         }
353         else
354         {
355             // Alternate reference frame reconstruction buffer
356             xd->pre.y_buffer = pc->alt_ref_frame.y_buffer + recon_yoffset;
357             xd->pre.u_buffer = pc->alt_ref_frame.u_buffer + recon_uvoffset;
358             xd->pre.v_buffer = pc->alt_ref_frame.v_buffer + recon_uvoffset;
359         }
360
361         vp8_build_uvmvs(xd, pc->full_pixel);
362
363         /*
364         if(pbi->common.current_video_frame==0 &&mb_col==1 && mb_row==0)
365         pbi->debugoutput =1;
366         else
367         pbi->debugoutput =0;
368         */
369         vp8dx_bool_decoder_fill(xd->current_bc);
370         vp8_decode_macroblock(pbi, xd);
371
372
373         recon_yoffset += 16;
374         recon_uvoffset += 8;
375
376         ++xd->mode_info_context;  /* next mb */
377
378         xd->gf_active_ptr++;      // GF useage flag for next MB
379
380         xd->above_context[Y1CONTEXT] += 4;
381         xd->above_context[UCONTEXT ] += 2;
382         xd->above_context[VCONTEXT ] += 2;
383         xd->above_context[Y2CONTEXT] ++;
384
385         pbi->current_mb_col_main = mb_col;
386     }
387
388     // adjust to the next row of mbs
389     vp8_extend_mb_row(
390         &pc->new_frame,
391         xd->dst.y_buffer + 16, xd->dst.u_buffer + 8, xd->dst.v_buffer + 8
392     );
393
394     ++xd->mode_info_context;      /* skip prediction column */
395
396     pbi->last_mb_row_decoded = mb_row;
397 }
398
399
400 static unsigned int read_partition_size(const unsigned char *cx_size)
401 {
402     const unsigned int size =
403         cx_size[0] + (cx_size[1] << 8) + (cx_size[2] << 16);
404     return size;
405 }
406
407
408 static void setup_token_decoder(VP8D_COMP *pbi,
409                                 const unsigned char *cx_data)
410 {
411     int num_part;
412     int i;
413     VP8_COMMON          *pc = &pbi->common;
414     const unsigned char *user_data_end = pbi->Source + pbi->source_sz;
415     vp8_reader          *bool_decoder;
416     const unsigned char *partition;
417
418     /* Parse number of token partitions to use */
419     pc->multi_token_partition = (TOKEN_PARTITION)vp8_read_literal(&pbi->bc, 2);
420     num_part = 1 << pc->multi_token_partition;
421
422     /* Set up pointers to the first partition */
423     partition = cx_data;
424     bool_decoder = &pbi->bc2;
425
426     if (num_part > 1)
427     {
428         CHECK_MEM_ERROR(pbi->mbc, vpx_malloc(num_part * sizeof(vp8_reader)));
429         bool_decoder = pbi->mbc;
430         partition += 3 * (num_part - 1);
431     }
432
433     for (i = 0; i < num_part; i++)
434     {
435         const unsigned char *partition_size_ptr = cx_data + i * 3;
436         unsigned int         partition_size;
437
438         /* Calculate the length of this partition. The last partition
439          * size is implicit.
440          */
441         if (i < num_part - 1)
442         {
443             partition_size = read_partition_size(partition_size_ptr);
444         }
445         else
446         {
447             partition_size = user_data_end - partition;
448         }
449
450         if (partition + partition_size > user_data_end)
451             vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
452                                "Truncated packet or corrupt partition "
453                                "%d length", i + 1);
454
455         if (vp8dx_start_decode(bool_decoder, IF_RTCD(&pbi->dboolhuff),
456                                partition, partition_size))
457             vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
458                                "Failed to allocate bool decoder %d", i + 1);
459
460         /* Advance to the next partition */
461         partition += partition_size;
462         bool_decoder++;
463     }
464
465     /* Clamp number of decoder threads */
466     if (pbi->decoding_thread_count > num_part - 1)
467         pbi->decoding_thread_count = num_part - 1;
468 }
469
470
471 static void stop_token_decoder(VP8D_COMP *pbi)
472 {
473     int i;
474     VP8_COMMON *pc = &pbi->common;
475
476     if (pc->multi_token_partition != ONE_PARTITION)
477     {
478         int num_part = (1 << pc->multi_token_partition);
479
480         for (i = 0; i < num_part; i++)
481         {
482             vp8dx_stop_decode(&pbi->mbc[i]);
483         }
484
485         vpx_free(pbi->mbc);
486     }
487     else
488         vp8dx_stop_decode(& pbi->bc2);
489 }
490
491 static void init_frame(VP8D_COMP *pbi)
492 {
493     VP8_COMMON *const pc = & pbi->common;
494     MACROBLOCKD *const xd  = & pbi->mb;
495
496     if (pc->frame_type == KEY_FRAME)
497     {
498         // Various keyframe initializations
499         vpx_memcpy(pc->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
500
501         vp8_init_mbmode_probs(pc);
502
503         vp8_default_coef_probs(pc);
504         vp8_kf_default_bmode_probs(pc->kf_bmode_prob);
505
506         // reset the segment feature data to 0 with delta coding (Default state).
507         vpx_memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
508         xd->mb_segement_abs_delta = SEGMENT_DELTADATA;
509
510        // reset the mode ref deltasa for loop filter
511         vpx_memset(xd->ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
512         vpx_memset(xd->mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
513
514         // All buffers are implicitly updated on key frames.
515         pc->refresh_golden_frame = 1;
516         pc->refresh_alt_ref_frame = 1;
517         pc->copy_buffer_to_gf = 0;
518         pc->copy_buffer_to_arf = 0;
519
520         // Note that Golden and Altref modes cannot be used on a key frame so
521         // ref_frame_sign_bias[] is undefined and meaningless
522         pc->ref_frame_sign_bias[GOLDEN_FRAME] = 0;
523         pc->ref_frame_sign_bias[ALTREF_FRAME] = 0;
524     }
525     else
526     {
527         if (!pc->use_bilinear_mc_filter)
528             pc->mcomp_filter_type = SIXTAP;
529         else
530             pc->mcomp_filter_type = BILINEAR;
531
532         // To enable choice of different interploation filters
533         if (pc->mcomp_filter_type == SIXTAP)
534         {
535             xd->subpixel_predict      = SUBPIX_INVOKE(RTCD_VTABLE(subpix), sixtap4x4);
536             xd->subpixel_predict8x4   = SUBPIX_INVOKE(RTCD_VTABLE(subpix), sixtap8x4);
537             xd->subpixel_predict8x8   = SUBPIX_INVOKE(RTCD_VTABLE(subpix), sixtap8x8);
538             xd->subpixel_predict16x16 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), sixtap16x16);
539         }
540         else
541         {
542             xd->subpixel_predict      = SUBPIX_INVOKE(RTCD_VTABLE(subpix), bilinear4x4);
543             xd->subpixel_predict8x4   = SUBPIX_INVOKE(RTCD_VTABLE(subpix), bilinear8x4);
544             xd->subpixel_predict8x8   = SUBPIX_INVOKE(RTCD_VTABLE(subpix), bilinear8x8);
545             xd->subpixel_predict16x16 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), bilinear16x16);
546         }
547     }
548
549     xd->left_context = pc->left_context;
550     xd->mode_info_context = pc->mi;
551     xd->frame_type = pc->frame_type;
552     xd->mbmi.mode = DC_PRED;
553     xd->mode_info_stride = pc->mode_info_stride;
554 }
555
556 int vp8_decode_frame(VP8D_COMP *pbi)
557 {
558     vp8_reader *const bc = & pbi->bc;
559     VP8_COMMON *const pc = & pbi->common;
560     MACROBLOCKD *const xd  = & pbi->mb;
561     const unsigned char *data = (const unsigned char *)pbi->Source;
562     const unsigned char *const data_end = data + pbi->source_sz;
563     int first_partition_length_in_bytes;
564
565     int mb_row;
566     int i, j, k, l;
567     const int *const mb_feature_data_bits = vp8_mb_feature_data_bits;
568
569     pc->frame_type = (FRAME_TYPE)(data[0] & 1);
570     pc->version = (data[0] >> 1) & 7;
571     pc->show_frame = (data[0] >> 4) & 1;
572     first_partition_length_in_bytes =
573         (data[0] | (data[1] << 8) | (data[2] << 16)) >> 5;
574     data += 3;
575
576     if (data + first_partition_length_in_bytes > data_end)
577         vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
578                            "Truncated packet or corrupt partition 0 length");
579     vp8_setup_version(pc);
580
581     if (pc->frame_type == KEY_FRAME)
582     {
583         const int Width = pc->Width;
584         const int Height = pc->Height;
585
586         // vet via sync code
587         if (data[0] != 0x9d || data[1] != 0x01 || data[2] != 0x2a)
588             vpx_internal_error(&pc->error, VPX_CODEC_UNSUP_BITSTREAM,
589                                "Invalid frame sync code");
590
591         pc->Width = (data[3] | (data[4] << 8)) & 0x3fff;
592         pc->horiz_scale = data[4] >> 6;
593         pc->Height = (data[5] | (data[6] << 8)) & 0x3fff;
594         pc->vert_scale = data[6] >> 6;
595         data += 7;
596
597         if (Width != pc->Width  ||  Height != pc->Height)
598         {
599             if (pc->Width <= 0)
600             {
601                 pc->Width = Width;
602                 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
603                                    "Invalid frame width");
604             }
605
606             if (pc->Height <= 0)
607             {
608                 pc->Height = Height;
609                 vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
610                                    "Invalid frame height");
611             }
612
613             if (vp8_alloc_frame_buffers(&pbi->common, pc->Width, pc->Height))
614                 vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
615                                    "Failed to allocate frame buffers");
616         }
617     }
618
619     if (pc->Width == 0 || pc->Height == 0)
620     {
621         return -1;
622     }
623
624     init_frame(pbi);
625
626     if (vp8dx_start_decode(bc, IF_RTCD(&pbi->dboolhuff),
627                            data, data_end - data))
628         vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
629                            "Failed to allocate bool decoder 0");
630     if (pc->frame_type == KEY_FRAME) {
631         pc->clr_type    = (YUV_TYPE)vp8_read_bit(bc);
632         pc->clamp_type  = (CLAMP_TYPE)vp8_read_bit(bc);
633     }
634
635     // Is segmentation enabled
636     xd->segmentation_enabled = (unsigned char)vp8_read_bit(bc);
637
638     if (xd->segmentation_enabled)
639     {
640         // Signal whether or not the segmentation map is being explicitly updated this frame.
641         xd->update_mb_segmentation_map = (unsigned char)vp8_read_bit(bc);
642         xd->update_mb_segmentation_data = (unsigned char)vp8_read_bit(bc);
643
644         if (xd->update_mb_segmentation_data)
645         {
646             xd->mb_segement_abs_delta = (unsigned char)vp8_read_bit(bc);
647
648             vpx_memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
649
650             // For each segmentation feature (Quant and loop filter level)
651             for (i = 0; i < MB_LVL_MAX; i++)
652             {
653                 for (j = 0; j < MAX_MB_SEGMENTS; j++)
654                 {
655                     // Frame level data
656                     if (vp8_read_bit(bc))
657                     {
658                         xd->segment_feature_data[i][j] = (signed char)vp8_read_literal(bc, mb_feature_data_bits[i]);
659
660                         if (vp8_read_bit(bc))
661                             xd->segment_feature_data[i][j] = -xd->segment_feature_data[i][j];
662                     }
663                     else
664                         xd->segment_feature_data[i][j] = 0;
665                 }
666             }
667         }
668
669         if (xd->update_mb_segmentation_map)
670         {
671             // Which macro block level features are enabled
672             vpx_memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));
673
674             // Read the probs used to decode the segment id for each macro block.
675             for (i = 0; i < MB_FEATURE_TREE_PROBS; i++)
676             {
677                 // If not explicitly set value is defaulted to 255 by memset above
678                 if (vp8_read_bit(bc))
679                     xd->mb_segment_tree_probs[i] = (vp8_prob)vp8_read_literal(bc, 8);
680             }
681         }
682     }
683
684     // Read the loop filter level and type
685     pc->filter_type = (LOOPFILTERTYPE) vp8_read_bit(bc);
686     pc->filter_level = vp8_read_literal(bc, 6);
687     pc->sharpness_level = vp8_read_literal(bc, 3);
688
689     // Read in loop filter deltas applied at the MB level based on mode or ref frame.
690     xd->mode_ref_lf_delta_update = 0;
691     xd->mode_ref_lf_delta_enabled = (unsigned char)vp8_read_bit(bc);
692
693     if (xd->mode_ref_lf_delta_enabled)
694     {
695         // Do the deltas need to be updated
696         xd->mode_ref_lf_delta_update = (unsigned char)vp8_read_bit(bc);
697
698         if (xd->mode_ref_lf_delta_update)
699         {
700             // Send update
701             for (i = 0; i < MAX_REF_LF_DELTAS; i++)
702             {
703                 if (vp8_read_bit(bc))
704                 {
705                     //sign = vp8_read_bit( bc );
706                     xd->ref_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
707
708                     if (vp8_read_bit(bc))        // Apply sign
709                         xd->ref_lf_deltas[i] = xd->ref_lf_deltas[i] * -1;
710                 }
711             }
712
713             // Send update
714             for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
715             {
716                 if (vp8_read_bit(bc))
717                 {
718                     //sign = vp8_read_bit( bc );
719                     xd->mode_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
720
721                     if (vp8_read_bit(bc))        // Apply sign
722                         xd->mode_lf_deltas[i] = xd->mode_lf_deltas[i] * -1;
723                 }
724             }
725         }
726     }
727
728     setup_token_decoder(pbi, data + first_partition_length_in_bytes);
729     xd->current_bc = &pbi->bc2;
730
731     // Read the default quantizers.
732     {
733         int Q, q_update;
734
735         Q = vp8_read_literal(bc, 7);  // AC 1st order Q = default
736         pc->base_qindex = Q;
737         q_update = 0;
738         pc->y1dc_delta_q = get_delta_q(bc, pc->y1dc_delta_q, &q_update);
739         pc->y2dc_delta_q = get_delta_q(bc, pc->y2dc_delta_q, &q_update);
740         pc->y2ac_delta_q = get_delta_q(bc, pc->y2ac_delta_q, &q_update);
741         pc->uvdc_delta_q = get_delta_q(bc, pc->uvdc_delta_q, &q_update);
742         pc->uvac_delta_q = get_delta_q(bc, pc->uvac_delta_q, &q_update);
743
744         if (q_update)
745             vp8cx_init_de_quantizer(pbi);
746
747         // MB level dequantizer setup
748         mb_init_dequantizer(pbi, &pbi->mb);
749     }
750
751     // Determine if the golden frame or ARF buffer should be updated and how.
752     // For all non key frames the GF and ARF refresh flags and sign bias
753     // flags must be set explicitly.
754     if (pc->frame_type != KEY_FRAME)
755     {
756         // Should the GF or ARF be updated from the current frame
757         pc->refresh_golden_frame = vp8_read_bit(bc);
758         pc->refresh_alt_ref_frame = vp8_read_bit(bc);
759
760         // Buffer to buffer copy flags.
761         pc->copy_buffer_to_gf = 0;
762
763         if (!pc->refresh_golden_frame)
764             pc->copy_buffer_to_gf = vp8_read_literal(bc, 2);
765
766         pc->copy_buffer_to_arf = 0;
767
768         if (!pc->refresh_alt_ref_frame)
769             pc->copy_buffer_to_arf = vp8_read_literal(bc, 2);
770
771         pc->ref_frame_sign_bias[GOLDEN_FRAME] = vp8_read_bit(bc);
772         pc->ref_frame_sign_bias[ALTREF_FRAME] = vp8_read_bit(bc);
773     }
774
775     pc->refresh_entropy_probs = vp8_read_bit(bc);
776     if (pc->refresh_entropy_probs == 0)
777     {
778         vpx_memcpy(&pc->lfc, &pc->fc, sizeof(pc->fc));
779     }
780
781     pc->refresh_last_frame = pc->frame_type == KEY_FRAME  ||  vp8_read_bit(bc);
782
783     if (0)
784     {
785         FILE *z = fopen("decodestats.stt", "a");
786         fprintf(z, "%6d F:%d,G:%d,A:%d,L:%d,Q:%d\n",
787                 pc->current_video_frame,
788                 pc->frame_type,
789                 pc->refresh_golden_frame,
790                 pc->refresh_alt_ref_frame,
791                 pc->refresh_last_frame,
792                 pc->base_qindex);
793         fclose(z);
794     }
795
796
797     vp8dx_bool_decoder_fill(bc);
798     {
799         // read coef probability tree
800
801         for (i = 0; i < BLOCK_TYPES; i++)
802             for (j = 0; j < COEF_BANDS; j++)
803                 for (k = 0; k < PREV_COEF_CONTEXTS; k++)
804                     for (l = 0; l < MAX_ENTROPY_TOKENS - 1; l++)
805                     {
806
807                         vp8_prob *const p = pc->fc.coef_probs [i][j][k] + l;
808
809                         if (vp8_read(bc, vp8_coef_update_probs [i][j][k][l]))
810                         {
811                             *p = (vp8_prob)vp8_read_literal(bc, 8);
812
813                         }
814                     }
815     }
816
817     vpx_memcpy(&xd->pre, &pc->last_frame, sizeof(YV12_BUFFER_CONFIG));
818     vpx_memcpy(&xd->dst, &pc->new_frame, sizeof(YV12_BUFFER_CONFIG));
819
820     // set up frame new frame for intra coded blocks
821     vp8_setup_intra_recon(&pc->new_frame);
822
823     vp8_setup_block_dptrs(xd);
824
825     vp8_build_block_doffsets(xd);
826
827     // clear out the coeff buffer
828     vpx_memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
829
830     // Read the mb_no_coeff_skip flag
831     pc->mb_no_coeff_skip = (int)vp8_read_bit(bc);
832
833     if (pc->frame_type == KEY_FRAME)
834         vp8_kfread_modes(pbi);
835     else
836         vp8_decode_mode_mvs(pbi);
837
838     // reset since these guys are used as iterators
839     vpx_memset(pc->above_context[Y1CONTEXT], 0, sizeof(ENTROPY_CONTEXT) * pc->mb_cols * 4);
840     vpx_memset(pc->above_context[UCONTEXT ], 0, sizeof(ENTROPY_CONTEXT) * pc->mb_cols * 2);
841     vpx_memset(pc->above_context[VCONTEXT ], 0, sizeof(ENTROPY_CONTEXT) * pc->mb_cols * 2);
842     vpx_memset(pc->above_context[Y2CONTEXT], 0, sizeof(ENTROPY_CONTEXT) * pc->mb_cols);
843
844     xd->gf_active_ptr = (signed char *)pc->gf_active_flags;     // Point to base of GF active flags data structure
845
846
847     vpx_memcpy(&xd->block[0].bmi, &xd->mode_info_context->bmi[0], sizeof(B_MODE_INFO));
848
849
850     if (pbi->b_multithreaded_lf && pbi->common.filter_level != 0)
851         vp8_start_lfthread(pbi);
852
853     if (pbi->b_multithreaded_rd && pbi->common.multi_token_partition != ONE_PARTITION)
854     {
855         vp8_mtdecode_mb_rows(pbi, xd);
856     }
857     else
858     {
859         int ibc = 0;
860         int num_part = 1 << pbi->common.multi_token_partition;
861
862         // Decode the individual macro block
863         for (mb_row = 0; mb_row < pc->mb_rows; mb_row++)
864         {
865
866             if (num_part > 1)
867             {
868                 xd->current_bc = & pbi->mbc[ibc];
869                 ibc++;
870
871                 if (ibc == num_part)
872                     ibc = 0;
873             }
874
875             vp8_decode_mb_row(pbi, pc, mb_row, xd);
876         }
877
878         pbi->last_mb_row_decoded = mb_row;
879     }
880
881
882     stop_token_decoder(pbi);
883
884     vp8dx_stop_decode(bc);
885
886     // vpx_log("Decoder: Frame Decoded, Size Roughly:%d bytes  \n",bc->pos+pbi->bc2.pos);
887
888     // If this was a kf or Gf note the Q used
889     if ((pc->frame_type == KEY_FRAME) || (pc->refresh_golden_frame) || pbi->common.refresh_alt_ref_frame)
890         pc->last_kf_gf_q = pc->base_qindex;
891
892     if (pc->refresh_entropy_probs == 0)
893     {
894         vpx_memcpy(&pc->fc, &pc->lfc, sizeof(pc->fc));
895     }
896
897 #ifdef PACKET_TESTING
898     {
899         FILE *f = fopen("decompressor.VP8", "ab");
900         unsigned int size = pbi->bc2.pos + pbi->bc.pos + 8;
901         fwrite((void *) &size, 4, 1, f);
902         fwrite((void *) pbi->Source, size, 1, f);
903         fclose(f);
904     }
905 #endif
906
907     return 0;
908 }