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