decodframe.c 45.8 KB
Newer Older
    // Read common prediction model status flag probability updates for the
    // reference frame
    if ( pc->frame_type == KEY_FRAME )
    {
        // Set the prediction probabilities to defaults
        pc->ref_pred_probs[0] = 120;
        pc->ref_pred_probs[1] = 80;
        pc->ref_pred_probs[2] = 40;
    }
    else
    {
        for (i = 0; i < PREDICTION_PROBS; i++)
        {
            if ( vp8_read_bit(bc) )
                pc->ref_pred_probs[i] = (vp8_prob)vp8_read_literal(bc, 8);
        }
    }

    /* Read the loop filter level and type */
    pc->txfm_mode = (TXFM_MODE) vp8_read_bit(bc);

John Koleszar's avatar
John Koleszar committed
    pc->filter_type = (LOOPFILTERTYPE) vp8_read_bit(bc);
    pc->filter_level = vp8_read_literal(bc, 6);
    pc->sharpness_level = vp8_read_literal(bc, 3);

    /* Read in loop filter deltas applied at the MB level based on mode or ref frame. */
John Koleszar's avatar
John Koleszar committed
    xd->mode_ref_lf_delta_update = 0;
    xd->mode_ref_lf_delta_enabled = (unsigned char)vp8_read_bit(bc);

    if (xd->mode_ref_lf_delta_enabled)
    {
        /* Do the deltas need to be updated */
John Koleszar's avatar
John Koleszar committed
        xd->mode_ref_lf_delta_update = (unsigned char)vp8_read_bit(bc);

        if (xd->mode_ref_lf_delta_update)
        {
            /* Send update */
John Koleszar's avatar
John Koleszar committed
            for (i = 0; i < MAX_REF_LF_DELTAS; i++)
            {
                if (vp8_read_bit(bc))
                {
                    /*sign = vp8_read_bit( bc );*/
John Koleszar's avatar
John Koleszar committed
                    xd->ref_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);

                    if (vp8_read_bit(bc))        /* Apply sign */
John Koleszar's avatar
John Koleszar committed
                        xd->ref_lf_deltas[i] = xd->ref_lf_deltas[i] * -1;
                }
            }

            /* Send update */
John Koleszar's avatar
John Koleszar committed
            for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
            {
                if (vp8_read_bit(bc))
                {
                    /*sign = vp8_read_bit( bc );*/
John Koleszar's avatar
John Koleszar committed
                    xd->mode_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);

                    if (vp8_read_bit(bc))        /* Apply sign */
John Koleszar's avatar
John Koleszar committed
                        xd->mode_lf_deltas[i] = xd->mode_lf_deltas[i] * -1;
                }
            }
        }
    }

Paul Wilkins's avatar
Paul Wilkins committed
    setup_token_decoder(pbi, data + first_partition_length_in_bytes);

John Koleszar's avatar
John Koleszar committed
    xd->current_bc = &pbi->bc2;

    /* Read the default quantizers. */
John Koleszar's avatar
John Koleszar committed
    {
        int Q, q_update;

        Q = vp8_read_literal(bc, QINDEX_BITS);  /* AC 1st order Q = default */
John Koleszar's avatar
John Koleszar committed
        pc->base_qindex = Q;
        q_update = 0;
        pc->y1dc_delta_q = get_delta_q(bc, pc->y1dc_delta_q, &q_update);
        pc->y2dc_delta_q = get_delta_q(bc, pc->y2dc_delta_q, &q_update);
        pc->y2ac_delta_q = get_delta_q(bc, pc->y2ac_delta_q, &q_update);
        pc->uvdc_delta_q = get_delta_q(bc, pc->uvdc_delta_q, &q_update);
        pc->uvac_delta_q = get_delta_q(bc, pc->uvac_delta_q, &q_update);

        if (q_update)
            vp8cx_init_de_quantizer(pbi);

        /* MB level dequantizer setup */
John Koleszar's avatar
John Koleszar committed
        mb_init_dequantizer(pbi, &pbi->mb);
    }

    /* Determine if the golden frame or ARF buffer should be updated and how.
     * For all non key frames the GF and ARF refresh flags and sign bias
     * flags must be set explicitly.
     */
John Koleszar's avatar
John Koleszar committed
    if (pc->frame_type != KEY_FRAME)
    {
        /* Should the GF or ARF be updated from the current frame */
John Koleszar's avatar
John Koleszar committed
        pc->refresh_golden_frame = vp8_read_bit(bc);
        pc->refresh_alt_ref_frame = vp8_read_bit(bc);

        if(pc->refresh_alt_ref_frame)
            vpx_memcpy(&pc->fc, &pc->lfc_a, sizeof(pc->fc));
            vpx_memcpy( pc->vp8_mode_contexts,
                        pc->mode_context_a,
                        sizeof(pc->vp8_mode_contexts));
        }
            vpx_memcpy(&pc->fc, &pc->lfc, sizeof(pc->fc));
            vpx_memcpy( pc->vp8_mode_contexts,
                        pc->mode_context,
                        sizeof(pc->vp8_mode_contexts));
        }
        /* Buffer to buffer copy flags. */
John Koleszar's avatar
John Koleszar committed
        pc->copy_buffer_to_gf = 0;

        if (!pc->refresh_golden_frame)
            pc->copy_buffer_to_gf = vp8_read_literal(bc, 2);

        pc->copy_buffer_to_arf = 0;

        if (!pc->refresh_alt_ref_frame)
            pc->copy_buffer_to_arf = vp8_read_literal(bc, 2);

        pc->ref_frame_sign_bias[GOLDEN_FRAME] = vp8_read_bit(bc);
        pc->ref_frame_sign_bias[ALTREF_FRAME] = vp8_read_bit(bc);

#if CONFIG_HIGH_PRECISION_MV
        /* Is high precision mv allowed */
        xd->allow_high_precision_mv = (unsigned char)vp8_read_bit(bc);
#endif
#if CONFIG_ENHANCED_INTERP
        // Read the type of subpel filter to use
        pc->mcomp_filter_type = vp8_read_literal(bc, 2);
        /* To enable choice of different interploation filters */
        if (pc->mcomp_filter_type == SIXTAP)
        {
            xd->subpixel_predict      = SUBPIX_INVOKE(RTCD_VTABLE(subpix), sixtap4x4);
            xd->subpixel_predict8x4   = SUBPIX_INVOKE(RTCD_VTABLE(subpix), sixtap8x4);
            xd->subpixel_predict8x8   = SUBPIX_INVOKE(RTCD_VTABLE(subpix), sixtap8x8);
            xd->subpixel_predict16x16 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), sixtap16x16);
            xd->subpixel_predict_avg  = SUBPIX_INVOKE(RTCD_VTABLE(subpix), sixtap_avg4x4);
            xd->subpixel_predict_avg8x8 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), sixtap_avg8x8);
            xd->subpixel_predict_avg16x16 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), sixtap_avg16x16);
        }
        else if (pc->mcomp_filter_type == EIGHTTAP)
        {
            xd->subpixel_predict      = SUBPIX_INVOKE(RTCD_VTABLE(subpix), eighttap4x4);
            xd->subpixel_predict8x4   = SUBPIX_INVOKE(RTCD_VTABLE(subpix), eighttap8x4);
            xd->subpixel_predict8x8   = SUBPIX_INVOKE(RTCD_VTABLE(subpix), eighttap8x8);
            xd->subpixel_predict16x16 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), eighttap16x16);
            xd->subpixel_predict_avg8x8 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), eighttap_avg8x8);
            xd->subpixel_predict_avg16x16 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), eighttap_avg16x16);
        }
        else if (pc->mcomp_filter_type == EIGHTTAP_SHARP)
        {
            xd->subpixel_predict      = SUBPIX_INVOKE(RTCD_VTABLE(subpix), eighttap4x4_sharp);
            xd->subpixel_predict8x4   = SUBPIX_INVOKE(RTCD_VTABLE(subpix), eighttap8x4_sharp);
            xd->subpixel_predict8x8   = SUBPIX_INVOKE(RTCD_VTABLE(subpix), eighttap8x8_sharp);
            xd->subpixel_predict16x16 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), eighttap16x16_sharp);
            xd->subpixel_predict_avg8x8 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), eighttap_avg8x8_sharp);
            xd->subpixel_predict_avg16x16 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), eighttap_avg16x16_sharp);
        }
        else
        {
            xd->subpixel_predict      = SUBPIX_INVOKE(RTCD_VTABLE(subpix), bilinear4x4);
            xd->subpixel_predict8x4   = SUBPIX_INVOKE(RTCD_VTABLE(subpix), bilinear8x4);
            xd->subpixel_predict8x8   = SUBPIX_INVOKE(RTCD_VTABLE(subpix), bilinear8x8);
            xd->subpixel_predict16x16 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), bilinear16x16);
            xd->subpixel_predict_avg  = SUBPIX_INVOKE(RTCD_VTABLE(subpix), bilinear_avg4x4);
            xd->subpixel_predict_avg8x8 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), bilinear_avg8x8);
            xd->subpixel_predict_avg16x16 = SUBPIX_INVOKE(RTCD_VTABLE(subpix), bilinear_avg16x16);
        }
John Koleszar's avatar
John Koleszar committed
    }

    pc->refresh_entropy_probs = vp8_read_bit(bc);
    if (pc->refresh_entropy_probs == 0)
    {
        vpx_memcpy(&pc->lfc, &pc->fc, sizeof(pc->fc));
    }

    pc->refresh_last_frame = pc->frame_type == KEY_FRAME  ||  vp8_read_bit(bc);

    if (0)
    {
        FILE *z = fopen("decodestats.stt", "a");
        fprintf(z, "%6d F:%d,G:%d,A:%d,L:%d,Q:%d\n",
                pc->current_video_frame,
                pc->frame_type,
                pc->refresh_golden_frame,
                pc->refresh_alt_ref_frame,
                pc->refresh_last_frame,
                pc->base_qindex);
        fclose(z);
    }

    {
        /* read coef probability tree */
John Koleszar's avatar
John Koleszar committed
        for (i = 0; i < BLOCK_TYPES; i++)
            for (j = 0; j < COEF_BANDS; j++)
                for (k = 0; k < PREV_COEF_CONTEXTS; k++)
                    for (l = 0; l < ENTROPY_NODES; l++)
John Koleszar's avatar
John Koleszar committed
                    {
                        vp8_prob *const p = pc->fc.coef_probs [i][j][k] + l;

                        if (vp8_read(bc, vp8_coef_update_probs [i][j][k][l]))
                        {
                            *p = (vp8_prob)vp8_read_literal(bc, 8);

                        }
                    }
John Koleszar's avatar
John Koleszar committed
    }
Yaowu Xu's avatar
Yaowu Xu committed

    if(pbi->common.txfm_mode == ALLOW_8X8 && vp8_read_bit(bc))
    {
        // read coef probability tree
        for (i = 0; i < BLOCK_TYPES; i++)
            for (j = 0; j < COEF_BANDS; j++)
                for (k = 0; k < PREV_COEF_CONTEXTS; k++)
                    for (l = 0; l < MAX_ENTROPY_TOKENS - 1; l++)
                    {

                        vp8_prob *const p = pc->fc.coef_probs_8x8 [i][j][k] + l;

                        if (vp8_read(bc, vp8_coef_update_probs_8x8 [i][j][k][l]))
                        {
                            *p = (vp8_prob)vp8_read_literal(bc, 8);

                        }
                    }
    }
Yaowu Xu's avatar
Yaowu Xu committed

    vpx_memcpy(&xd->pre, &pc->yv12_fb[pc->lst_fb_idx], sizeof(YV12_BUFFER_CONFIG));
    vpx_memcpy(&xd->dst, &pc->yv12_fb[pc->new_fb_idx], sizeof(YV12_BUFFER_CONFIG));
     // Create the segmentation map structure and set to 0
     if (!pc->last_frame_seg_map)
       CHECK_MEM_ERROR(pc->last_frame_seg_map,
                       vpx_calloc((pc->mb_rows * pc->mb_cols), 1));
    /* set up frame new frame for intra coded blocks */
Paul Wilkins's avatar
Paul Wilkins committed
    vp8_setup_intra_recon(&pc->yv12_fb[pc->new_fb_idx]);
John Koleszar's avatar
John Koleszar committed

    vp8_setup_block_dptrs(xd);

    vp8_build_block_doffsets(xd);

    /* clear out the coeff buffer */
John Koleszar's avatar
John Koleszar committed
    vpx_memset(xd->qcoeff, 0, sizeof(xd->qcoeff));

    /* Read the mb_no_coeff_skip flag */
John Koleszar's avatar
John Koleszar committed
    pc->mb_no_coeff_skip = (int)vp8_read_bit(bc);

    vp8_decode_mode_mvs(pbi);
    if(pbi->common.frame_type != KEY_FRAME)
Yaowu Xu's avatar
Yaowu Xu committed
    {
        vp8_update_mode_context(&pbi->common);
    }
    vpx_memset(pc->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) * pc->mb_cols);
    // Resset the macroblock mode info context to the start of the list
    xd->mode_info_context = pc->mi;

Adrian Grange's avatar
Adrian Grange committed
    /* Decode a row of superblocks */
    for (mb_row = 0; mb_row < pc->mb_rows; mb_row+=2)
Paul Wilkins's avatar
Paul Wilkins committed
    {
Adrian Grange's avatar
Adrian Grange committed
        decode_sb_row(pbi, pc, mb_row, xd);
John Koleszar's avatar
John Koleszar committed
    }
Paul Wilkins's avatar
Paul Wilkins committed
    corrupt_tokens |= xd->corrupted;
    /* Collect information about decoder corruption. */
    /* 1. Check first boolean decoder for errors. */
    pc->yv12_fb[pc->new_fb_idx].corrupted = vp8dx_bool_error(bc);
    /* 2. Check the macroblock information */
    pc->yv12_fb[pc->new_fb_idx].corrupted |= corrupt_tokens;

    if (!pbi->decoded_key_frame)
    {
        if (pc->frame_type == KEY_FRAME &&
            !pc->yv12_fb[pc->new_fb_idx].corrupted)
            pbi->decoded_key_frame = 1;
        else
            vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME,
                               "A stream must start with a complete key frame");
    }
    /* vpx_log("Decoder: Frame Decoded, Size Roughly:%d bytes  \n",bc->pos+pbi->bc2.pos); */
    /* If this was a kf or Gf note the Q used */
    if ((pc->frame_type == KEY_FRAME) ||
         pc->refresh_golden_frame || pc->refresh_alt_ref_frame)
    {
John Koleszar's avatar
John Koleszar committed
        pc->last_kf_gf_q = pc->base_qindex;
    if(pc->refresh_entropy_probs)
    {
        if(pc->refresh_alt_ref_frame)
            vpx_memcpy(&pc->lfc_a, &pc->fc, sizeof(pc->fc));
        else
            vpx_memcpy(&pc->lfc, &pc->fc, sizeof(pc->fc));
    }
John Koleszar's avatar
John Koleszar committed

#ifdef PACKET_TESTING
    {
        FILE *f = fopen("decompressor.VP8", "ab");
        unsigned int size = pbi->bc2.pos + pbi->bc.pos + 8;
        fwrite((void *) &size, 4, 1, f);
        fwrite((void *) pbi->Source, size, 1, f);
        fclose(f);
    }
#endif
    //printf("Frame %d Done\n", frame_count++);
John Koleszar's avatar
John Koleszar committed

    return 0;
}