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