media/libvpx/vp8/decoder/decodframe.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp8/decoder/decodframe.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1398 @@
     1.4 +/*
     1.5 + *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
     1.6 + *
     1.7 + *  Use of this source code is governed by a BSD-style license
     1.8 + *  that can be found in the LICENSE file in the root of the source
     1.9 + *  tree. An additional intellectual property rights grant can be found
    1.10 + *  in the file PATENTS.  All contributing project authors may
    1.11 + *  be found in the AUTHORS file in the root of the source tree.
    1.12 + */
    1.13 +
    1.14 +
    1.15 +#include "vpx_config.h"
    1.16 +#include "vp8_rtcd.h"
    1.17 +#include "./vpx_scale_rtcd.h"
    1.18 +#include "onyxd_int.h"
    1.19 +#include "vp8/common/header.h"
    1.20 +#include "vp8/common/reconintra4x4.h"
    1.21 +#include "vp8/common/reconinter.h"
    1.22 +#include "detokenize.h"
    1.23 +#include "vp8/common/invtrans.h"
    1.24 +#include "vp8/common/alloccommon.h"
    1.25 +#include "vp8/common/entropymode.h"
    1.26 +#include "vp8/common/quant_common.h"
    1.27 +#include "vpx_scale/vpx_scale.h"
    1.28 +#include "vp8/common/setupintrarecon.h"
    1.29 +
    1.30 +#include "decodemv.h"
    1.31 +#include "vp8/common/extend.h"
    1.32 +#if CONFIG_ERROR_CONCEALMENT
    1.33 +#include "error_concealment.h"
    1.34 +#endif
    1.35 +#include "vpx_mem/vpx_mem.h"
    1.36 +#include "vp8/common/threading.h"
    1.37 +#include "decoderthreading.h"
    1.38 +#include "dboolhuff.h"
    1.39 +
    1.40 +#include <assert.h>
    1.41 +#include <stdio.h>
    1.42 +
    1.43 +void vp8cx_init_de_quantizer(VP8D_COMP *pbi)
    1.44 +{
    1.45 +    int Q;
    1.46 +    VP8_COMMON *const pc = & pbi->common;
    1.47 +
    1.48 +    for (Q = 0; Q < QINDEX_RANGE; Q++)
    1.49 +    {
    1.50 +        pc->Y1dequant[Q][0] = (short)vp8_dc_quant(Q, pc->y1dc_delta_q);
    1.51 +        pc->Y2dequant[Q][0] = (short)vp8_dc2quant(Q, pc->y2dc_delta_q);
    1.52 +        pc->UVdequant[Q][0] = (short)vp8_dc_uv_quant(Q, pc->uvdc_delta_q);
    1.53 +
    1.54 +        pc->Y1dequant[Q][1] = (short)vp8_ac_yquant(Q);
    1.55 +        pc->Y2dequant[Q][1] = (short)vp8_ac2quant(Q, pc->y2ac_delta_q);
    1.56 +        pc->UVdequant[Q][1] = (short)vp8_ac_uv_quant(Q, pc->uvac_delta_q);
    1.57 +    }
    1.58 +}
    1.59 +
    1.60 +void vp8_mb_init_dequantizer(VP8D_COMP *pbi, MACROBLOCKD *xd)
    1.61 +{
    1.62 +    int i;
    1.63 +    int QIndex;
    1.64 +    MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
    1.65 +    VP8_COMMON *const pc = & pbi->common;
    1.66 +
    1.67 +    /* Decide whether to use the default or alternate baseline Q value. */
    1.68 +    if (xd->segmentation_enabled)
    1.69 +    {
    1.70 +        /* Abs Value */
    1.71 +        if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA)
    1.72 +            QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
    1.73 +
    1.74 +        /* Delta Value */
    1.75 +        else
    1.76 +        {
    1.77 +            QIndex = pc->base_qindex + xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
    1.78 +            QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0;    /* Clamp to valid range */
    1.79 +        }
    1.80 +    }
    1.81 +    else
    1.82 +        QIndex = pc->base_qindex;
    1.83 +
    1.84 +    /* Set up the macroblock dequant constants */
    1.85 +    xd->dequant_y1_dc[0] = 1;
    1.86 +    xd->dequant_y1[0] = pc->Y1dequant[QIndex][0];
    1.87 +    xd->dequant_y2[0] = pc->Y2dequant[QIndex][0];
    1.88 +    xd->dequant_uv[0] = pc->UVdequant[QIndex][0];
    1.89 +
    1.90 +    for (i = 1; i < 16; i++)
    1.91 +    {
    1.92 +        xd->dequant_y1_dc[i] =
    1.93 +        xd->dequant_y1[i] = pc->Y1dequant[QIndex][1];
    1.94 +        xd->dequant_y2[i] = pc->Y2dequant[QIndex][1];
    1.95 +        xd->dequant_uv[i] = pc->UVdequant[QIndex][1];
    1.96 +    }
    1.97 +}
    1.98 +
    1.99 +static void decode_macroblock(VP8D_COMP *pbi, MACROBLOCKD *xd,
   1.100 +                              unsigned int mb_idx)
   1.101 +{
   1.102 +    MB_PREDICTION_MODE mode;
   1.103 +    int i;
   1.104 +#if CONFIG_ERROR_CONCEALMENT
   1.105 +    int corruption_detected = 0;
   1.106 +#endif
   1.107 +
   1.108 +    if (xd->mode_info_context->mbmi.mb_skip_coeff)
   1.109 +    {
   1.110 +        vp8_reset_mb_tokens_context(xd);
   1.111 +    }
   1.112 +    else if (!vp8dx_bool_error(xd->current_bc))
   1.113 +    {
   1.114 +        int eobtotal;
   1.115 +        eobtotal = vp8_decode_mb_tokens(pbi, xd);
   1.116 +
   1.117 +        /* Special case:  Force the loopfilter to skip when eobtotal is zero */
   1.118 +        xd->mode_info_context->mbmi.mb_skip_coeff = (eobtotal==0);
   1.119 +    }
   1.120 +
   1.121 +    mode = xd->mode_info_context->mbmi.mode;
   1.122 +
   1.123 +    if (xd->segmentation_enabled)
   1.124 +        vp8_mb_init_dequantizer(pbi, xd);
   1.125 +
   1.126 +
   1.127 +#if CONFIG_ERROR_CONCEALMENT
   1.128 +
   1.129 +    if(pbi->ec_active)
   1.130 +    {
   1.131 +        int throw_residual;
   1.132 +        /* When we have independent partitions we can apply residual even
   1.133 +         * though other partitions within the frame are corrupt.
   1.134 +         */
   1.135 +        throw_residual = (!pbi->independent_partitions &&
   1.136 +                          pbi->frame_corrupt_residual);
   1.137 +        throw_residual = (throw_residual || vp8dx_bool_error(xd->current_bc));
   1.138 +
   1.139 +        if ((mb_idx >= pbi->mvs_corrupt_from_mb || throw_residual))
   1.140 +        {
   1.141 +            /* MB with corrupt residuals or corrupt mode/motion vectors.
   1.142 +             * Better to use the predictor as reconstruction.
   1.143 +             */
   1.144 +            pbi->frame_corrupt_residual = 1;
   1.145 +            vpx_memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
   1.146 +            vp8_conceal_corrupt_mb(xd);
   1.147 +
   1.148 +
   1.149 +            corruption_detected = 1;
   1.150 +
   1.151 +            /* force idct to be skipped for B_PRED and use the
   1.152 +             * prediction only for reconstruction
   1.153 +             * */
   1.154 +            vpx_memset(xd->eobs, 0, 25);
   1.155 +        }
   1.156 +    }
   1.157 +#endif
   1.158 +
   1.159 +    /* do prediction */
   1.160 +    if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
   1.161 +    {
   1.162 +        vp8_build_intra_predictors_mbuv_s(xd,
   1.163 +                                          xd->recon_above[1],
   1.164 +                                          xd->recon_above[2],
   1.165 +                                          xd->recon_left[1],
   1.166 +                                          xd->recon_left[2],
   1.167 +                                          xd->recon_left_stride[1],
   1.168 +                                          xd->dst.u_buffer, xd->dst.v_buffer,
   1.169 +                                          xd->dst.uv_stride);
   1.170 +
   1.171 +        if (mode != B_PRED)
   1.172 +        {
   1.173 +            vp8_build_intra_predictors_mby_s(xd,
   1.174 +                                                 xd->recon_above[0],
   1.175 +                                                 xd->recon_left[0],
   1.176 +                                                 xd->recon_left_stride[0],
   1.177 +                                                 xd->dst.y_buffer,
   1.178 +                                                 xd->dst.y_stride);
   1.179 +        }
   1.180 +        else
   1.181 +        {
   1.182 +            short *DQC = xd->dequant_y1;
   1.183 +            int dst_stride = xd->dst.y_stride;
   1.184 +
   1.185 +            /* clear out residual eob info */
   1.186 +            if(xd->mode_info_context->mbmi.mb_skip_coeff)
   1.187 +                vpx_memset(xd->eobs, 0, 25);
   1.188 +
   1.189 +            intra_prediction_down_copy(xd, xd->recon_above[0] + 16);
   1.190 +
   1.191 +            for (i = 0; i < 16; i++)
   1.192 +            {
   1.193 +                BLOCKD *b = &xd->block[i];
   1.194 +                unsigned char *dst = xd->dst.y_buffer + b->offset;
   1.195 +                B_PREDICTION_MODE b_mode =
   1.196 +                    xd->mode_info_context->bmi[i].as_mode;
   1.197 +                unsigned char *Above = dst - dst_stride;
   1.198 +                unsigned char *yleft = dst - 1;
   1.199 +                int left_stride = dst_stride;
   1.200 +                unsigned char top_left = Above[-1];
   1.201 +
   1.202 +                vp8_intra4x4_predict(Above, yleft, left_stride, b_mode,
   1.203 +                                     dst, dst_stride, top_left);
   1.204 +
   1.205 +                if (xd->eobs[i])
   1.206 +                {
   1.207 +                    if (xd->eobs[i] > 1)
   1.208 +                    {
   1.209 +                    vp8_dequant_idct_add(b->qcoeff, DQC, dst, dst_stride);
   1.210 +                    }
   1.211 +                    else
   1.212 +                    {
   1.213 +                        vp8_dc_only_idct_add
   1.214 +                            (b->qcoeff[0] * DQC[0],
   1.215 +                                dst, dst_stride,
   1.216 +                                dst, dst_stride);
   1.217 +                        vpx_memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0]));
   1.218 +                    }
   1.219 +                }
   1.220 +            }
   1.221 +        }
   1.222 +    }
   1.223 +    else
   1.224 +    {
   1.225 +        vp8_build_inter_predictors_mb(xd);
   1.226 +    }
   1.227 +
   1.228 +
   1.229 +#if CONFIG_ERROR_CONCEALMENT
   1.230 +    if (corruption_detected)
   1.231 +    {
   1.232 +        return;
   1.233 +    }
   1.234 +#endif
   1.235 +
   1.236 +    if(!xd->mode_info_context->mbmi.mb_skip_coeff)
   1.237 +    {
   1.238 +        /* dequantization and idct */
   1.239 +        if (mode != B_PRED)
   1.240 +        {
   1.241 +            short *DQC = xd->dequant_y1;
   1.242 +
   1.243 +            if (mode != SPLITMV)
   1.244 +            {
   1.245 +                BLOCKD *b = &xd->block[24];
   1.246 +
   1.247 +                /* do 2nd order transform on the dc block */
   1.248 +                if (xd->eobs[24] > 1)
   1.249 +                {
   1.250 +                    vp8_dequantize_b(b, xd->dequant_y2);
   1.251 +
   1.252 +                    vp8_short_inv_walsh4x4(&b->dqcoeff[0],
   1.253 +                        xd->qcoeff);
   1.254 +                    vpx_memset(b->qcoeff, 0, 16 * sizeof(b->qcoeff[0]));
   1.255 +                }
   1.256 +                else
   1.257 +                {
   1.258 +                    b->dqcoeff[0] = b->qcoeff[0] * xd->dequant_y2[0];
   1.259 +                    vp8_short_inv_walsh4x4_1(&b->dqcoeff[0],
   1.260 +                        xd->qcoeff);
   1.261 +                    vpx_memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0]));
   1.262 +                }
   1.263 +
   1.264 +                /* override the dc dequant constant in order to preserve the
   1.265 +                 * dc components
   1.266 +                 */
   1.267 +                DQC = xd->dequant_y1_dc;
   1.268 +            }
   1.269 +
   1.270 +            vp8_dequant_idct_add_y_block
   1.271 +                            (xd->qcoeff, DQC,
   1.272 +                             xd->dst.y_buffer,
   1.273 +                             xd->dst.y_stride, xd->eobs);
   1.274 +        }
   1.275 +
   1.276 +        vp8_dequant_idct_add_uv_block
   1.277 +                        (xd->qcoeff+16*16, xd->dequant_uv,
   1.278 +                         xd->dst.u_buffer, xd->dst.v_buffer,
   1.279 +                         xd->dst.uv_stride, xd->eobs+16);
   1.280 +    }
   1.281 +}
   1.282 +
   1.283 +static int get_delta_q(vp8_reader *bc, int prev, int *q_update)
   1.284 +{
   1.285 +    int ret_val = 0;
   1.286 +
   1.287 +    if (vp8_read_bit(bc))
   1.288 +    {
   1.289 +        ret_val = vp8_read_literal(bc, 4);
   1.290 +
   1.291 +        if (vp8_read_bit(bc))
   1.292 +            ret_val = -ret_val;
   1.293 +    }
   1.294 +
   1.295 +    /* Trigger a quantizer update if the delta-q value has changed */
   1.296 +    if (ret_val != prev)
   1.297 +        *q_update = 1;
   1.298 +
   1.299 +    return ret_val;
   1.300 +}
   1.301 +
   1.302 +#ifdef PACKET_TESTING
   1.303 +#include <stdio.h>
   1.304 +FILE *vpxlog = 0;
   1.305 +#endif
   1.306 +
   1.307 +static void yv12_extend_frame_top_c(YV12_BUFFER_CONFIG *ybf)
   1.308 +{
   1.309 +    int i;
   1.310 +    unsigned char *src_ptr1;
   1.311 +    unsigned char *dest_ptr1;
   1.312 +
   1.313 +    unsigned int Border;
   1.314 +    int plane_stride;
   1.315 +
   1.316 +    /***********/
   1.317 +    /* Y Plane */
   1.318 +    /***********/
   1.319 +    Border = ybf->border;
   1.320 +    plane_stride = ybf->y_stride;
   1.321 +    src_ptr1 = ybf->y_buffer - Border;
   1.322 +    dest_ptr1 = src_ptr1 - (Border * plane_stride);
   1.323 +
   1.324 +    for (i = 0; i < (int)Border; i++)
   1.325 +    {
   1.326 +        vpx_memcpy(dest_ptr1, src_ptr1, plane_stride);
   1.327 +        dest_ptr1 += plane_stride;
   1.328 +    }
   1.329 +
   1.330 +
   1.331 +    /***********/
   1.332 +    /* U Plane */
   1.333 +    /***********/
   1.334 +    plane_stride = ybf->uv_stride;
   1.335 +    Border /= 2;
   1.336 +    src_ptr1 = ybf->u_buffer - Border;
   1.337 +    dest_ptr1 = src_ptr1 - (Border * plane_stride);
   1.338 +
   1.339 +    for (i = 0; i < (int)(Border); i++)
   1.340 +    {
   1.341 +        vpx_memcpy(dest_ptr1, src_ptr1, plane_stride);
   1.342 +        dest_ptr1 += plane_stride;
   1.343 +    }
   1.344 +
   1.345 +    /***********/
   1.346 +    /* V Plane */
   1.347 +    /***********/
   1.348 +
   1.349 +    src_ptr1 = ybf->v_buffer - Border;
   1.350 +    dest_ptr1 = src_ptr1 - (Border * plane_stride);
   1.351 +
   1.352 +    for (i = 0; i < (int)(Border); i++)
   1.353 +    {
   1.354 +        vpx_memcpy(dest_ptr1, src_ptr1, plane_stride);
   1.355 +        dest_ptr1 += plane_stride;
   1.356 +    }
   1.357 +}
   1.358 +
   1.359 +static void yv12_extend_frame_bottom_c(YV12_BUFFER_CONFIG *ybf)
   1.360 +{
   1.361 +    int i;
   1.362 +    unsigned char *src_ptr1, *src_ptr2;
   1.363 +    unsigned char *dest_ptr2;
   1.364 +
   1.365 +    unsigned int Border;
   1.366 +    int plane_stride;
   1.367 +    int plane_height;
   1.368 +
   1.369 +    /***********/
   1.370 +    /* Y Plane */
   1.371 +    /***********/
   1.372 +    Border = ybf->border;
   1.373 +    plane_stride = ybf->y_stride;
   1.374 +    plane_height = ybf->y_height;
   1.375 +
   1.376 +    src_ptr1 = ybf->y_buffer - Border;
   1.377 +    src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
   1.378 +    dest_ptr2 = src_ptr2 + plane_stride;
   1.379 +
   1.380 +    for (i = 0; i < (int)Border; i++)
   1.381 +    {
   1.382 +        vpx_memcpy(dest_ptr2, src_ptr2, plane_stride);
   1.383 +        dest_ptr2 += plane_stride;
   1.384 +    }
   1.385 +
   1.386 +
   1.387 +    /***********/
   1.388 +    /* U Plane */
   1.389 +    /***********/
   1.390 +    plane_stride = ybf->uv_stride;
   1.391 +    plane_height = ybf->uv_height;
   1.392 +    Border /= 2;
   1.393 +
   1.394 +    src_ptr1 = ybf->u_buffer - Border;
   1.395 +    src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
   1.396 +    dest_ptr2 = src_ptr2 + plane_stride;
   1.397 +
   1.398 +    for (i = 0; i < (int)(Border); i++)
   1.399 +    {
   1.400 +        vpx_memcpy(dest_ptr2, src_ptr2, plane_stride);
   1.401 +        dest_ptr2 += plane_stride;
   1.402 +    }
   1.403 +
   1.404 +    /***********/
   1.405 +    /* V Plane */
   1.406 +    /***********/
   1.407 +
   1.408 +    src_ptr1 = ybf->v_buffer - Border;
   1.409 +    src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
   1.410 +    dest_ptr2 = src_ptr2 + plane_stride;
   1.411 +
   1.412 +    for (i = 0; i < (int)(Border); i++)
   1.413 +    {
   1.414 +        vpx_memcpy(dest_ptr2, src_ptr2, plane_stride);
   1.415 +        dest_ptr2 += plane_stride;
   1.416 +    }
   1.417 +}
   1.418 +
   1.419 +static void yv12_extend_frame_left_right_c(YV12_BUFFER_CONFIG *ybf,
   1.420 +                                           unsigned char *y_src,
   1.421 +                                           unsigned char *u_src,
   1.422 +                                           unsigned char *v_src)
   1.423 +{
   1.424 +    int i;
   1.425 +    unsigned char *src_ptr1, *src_ptr2;
   1.426 +    unsigned char *dest_ptr1, *dest_ptr2;
   1.427 +
   1.428 +    unsigned int Border;
   1.429 +    int plane_stride;
   1.430 +    int plane_height;
   1.431 +    int plane_width;
   1.432 +
   1.433 +    /***********/
   1.434 +    /* Y Plane */
   1.435 +    /***********/
   1.436 +    Border = ybf->border;
   1.437 +    plane_stride = ybf->y_stride;
   1.438 +    plane_height = 16;
   1.439 +    plane_width = ybf->y_width;
   1.440 +
   1.441 +    /* copy the left and right most columns out */
   1.442 +    src_ptr1 = y_src;
   1.443 +    src_ptr2 = src_ptr1 + plane_width - 1;
   1.444 +    dest_ptr1 = src_ptr1 - Border;
   1.445 +    dest_ptr2 = src_ptr2 + 1;
   1.446 +
   1.447 +    for (i = 0; i < plane_height; i++)
   1.448 +    {
   1.449 +        vpx_memset(dest_ptr1, src_ptr1[0], Border);
   1.450 +        vpx_memset(dest_ptr2, src_ptr2[0], Border);
   1.451 +        src_ptr1  += plane_stride;
   1.452 +        src_ptr2  += plane_stride;
   1.453 +        dest_ptr1 += plane_stride;
   1.454 +        dest_ptr2 += plane_stride;
   1.455 +    }
   1.456 +
   1.457 +    /***********/
   1.458 +    /* U Plane */
   1.459 +    /***********/
   1.460 +    plane_stride = ybf->uv_stride;
   1.461 +    plane_height = 8;
   1.462 +    plane_width = ybf->uv_width;
   1.463 +    Border /= 2;
   1.464 +
   1.465 +    /* copy the left and right most columns out */
   1.466 +    src_ptr1 = u_src;
   1.467 +    src_ptr2 = src_ptr1 + plane_width - 1;
   1.468 +    dest_ptr1 = src_ptr1 - Border;
   1.469 +    dest_ptr2 = src_ptr2 + 1;
   1.470 +
   1.471 +    for (i = 0; i < plane_height; i++)
   1.472 +    {
   1.473 +        vpx_memset(dest_ptr1, src_ptr1[0], Border);
   1.474 +        vpx_memset(dest_ptr2, src_ptr2[0], Border);
   1.475 +        src_ptr1  += plane_stride;
   1.476 +        src_ptr2  += plane_stride;
   1.477 +        dest_ptr1 += plane_stride;
   1.478 +        dest_ptr2 += plane_stride;
   1.479 +    }
   1.480 +
   1.481 +    /***********/
   1.482 +    /* V Plane */
   1.483 +    /***********/
   1.484 +
   1.485 +    /* copy the left and right most columns out */
   1.486 +    src_ptr1 = v_src;
   1.487 +    src_ptr2 = src_ptr1 + plane_width - 1;
   1.488 +    dest_ptr1 = src_ptr1 - Border;
   1.489 +    dest_ptr2 = src_ptr2 + 1;
   1.490 +
   1.491 +    for (i = 0; i < plane_height; i++)
   1.492 +    {
   1.493 +        vpx_memset(dest_ptr1, src_ptr1[0], Border);
   1.494 +        vpx_memset(dest_ptr2, src_ptr2[0], Border);
   1.495 +        src_ptr1  += plane_stride;
   1.496 +        src_ptr2  += plane_stride;
   1.497 +        dest_ptr1 += plane_stride;
   1.498 +        dest_ptr2 += plane_stride;
   1.499 +    }
   1.500 +}
   1.501 +
   1.502 +static void decode_mb_rows(VP8D_COMP *pbi)
   1.503 +{
   1.504 +    VP8_COMMON *const pc = & pbi->common;
   1.505 +    MACROBLOCKD *const xd  = & pbi->mb;
   1.506 +
   1.507 +    MODE_INFO *lf_mic = xd->mode_info_context;
   1.508 +
   1.509 +    int ibc = 0;
   1.510 +    int num_part = 1 << pc->multi_token_partition;
   1.511 +
   1.512 +    int recon_yoffset, recon_uvoffset;
   1.513 +    int mb_row, mb_col;
   1.514 +    int mb_idx = 0;
   1.515 +
   1.516 +    YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME];
   1.517 +
   1.518 +    int recon_y_stride = yv12_fb_new->y_stride;
   1.519 +    int recon_uv_stride = yv12_fb_new->uv_stride;
   1.520 +
   1.521 +    unsigned char *ref_buffer[MAX_REF_FRAMES][3];
   1.522 +    unsigned char *dst_buffer[3];
   1.523 +    unsigned char *lf_dst[3];
   1.524 +    unsigned char *eb_dst[3];
   1.525 +    int i;
   1.526 +    int ref_fb_corrupted[MAX_REF_FRAMES];
   1.527 +
   1.528 +    ref_fb_corrupted[INTRA_FRAME] = 0;
   1.529 +
   1.530 +    for(i = 1; i < MAX_REF_FRAMES; i++)
   1.531 +    {
   1.532 +        YV12_BUFFER_CONFIG *this_fb = pbi->dec_fb_ref[i];
   1.533 +
   1.534 +        ref_buffer[i][0] = this_fb->y_buffer;
   1.535 +        ref_buffer[i][1] = this_fb->u_buffer;
   1.536 +        ref_buffer[i][2] = this_fb->v_buffer;
   1.537 +
   1.538 +        ref_fb_corrupted[i] = this_fb->corrupted;
   1.539 +    }
   1.540 +
   1.541 +    /* Set up the buffer pointers */
   1.542 +    eb_dst[0] = lf_dst[0] = dst_buffer[0] = yv12_fb_new->y_buffer;
   1.543 +    eb_dst[1] = lf_dst[1] = dst_buffer[1] = yv12_fb_new->u_buffer;
   1.544 +    eb_dst[2] = lf_dst[2] = dst_buffer[2] = yv12_fb_new->v_buffer;
   1.545 +
   1.546 +    xd->up_available = 0;
   1.547 +
   1.548 +    /* Initialize the loop filter for this frame. */
   1.549 +    if(pc->filter_level)
   1.550 +        vp8_loop_filter_frame_init(pc, xd, pc->filter_level);
   1.551 +
   1.552 +    vp8_setup_intra_recon_top_line(yv12_fb_new);
   1.553 +
   1.554 +    /* Decode the individual macro block */
   1.555 +    for (mb_row = 0; mb_row < pc->mb_rows; mb_row++)
   1.556 +    {
   1.557 +        if (num_part > 1)
   1.558 +        {
   1.559 +            xd->current_bc = & pbi->mbc[ibc];
   1.560 +            ibc++;
   1.561 +
   1.562 +            if (ibc == num_part)
   1.563 +                ibc = 0;
   1.564 +        }
   1.565 +
   1.566 +        recon_yoffset = mb_row * recon_y_stride * 16;
   1.567 +        recon_uvoffset = mb_row * recon_uv_stride * 8;
   1.568 +
   1.569 +        /* reset contexts */
   1.570 +        xd->above_context = pc->above_context;
   1.571 +        vpx_memset(xd->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
   1.572 +
   1.573 +        xd->left_available = 0;
   1.574 +
   1.575 +        xd->mb_to_top_edge = -((mb_row * 16) << 3);
   1.576 +        xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
   1.577 +
   1.578 +        xd->recon_above[0] = dst_buffer[0] + recon_yoffset;
   1.579 +        xd->recon_above[1] = dst_buffer[1] + recon_uvoffset;
   1.580 +        xd->recon_above[2] = dst_buffer[2] + recon_uvoffset;
   1.581 +
   1.582 +        xd->recon_left[0] = xd->recon_above[0] - 1;
   1.583 +        xd->recon_left[1] = xd->recon_above[1] - 1;
   1.584 +        xd->recon_left[2] = xd->recon_above[2] - 1;
   1.585 +
   1.586 +        xd->recon_above[0] -= xd->dst.y_stride;
   1.587 +        xd->recon_above[1] -= xd->dst.uv_stride;
   1.588 +        xd->recon_above[2] -= xd->dst.uv_stride;
   1.589 +
   1.590 +        /* TODO: move to outside row loop */
   1.591 +        xd->recon_left_stride[0] = xd->dst.y_stride;
   1.592 +        xd->recon_left_stride[1] = xd->dst.uv_stride;
   1.593 +
   1.594 +        setup_intra_recon_left(xd->recon_left[0], xd->recon_left[1],
   1.595 +                               xd->recon_left[2], xd->dst.y_stride,
   1.596 +                               xd->dst.uv_stride);
   1.597 +
   1.598 +        for (mb_col = 0; mb_col < pc->mb_cols; mb_col++)
   1.599 +        {
   1.600 +            /* Distance of Mb to the various image edges.
   1.601 +             * These are specified to 8th pel as they are always compared to values
   1.602 +             * that are in 1/8th pel units
   1.603 +             */
   1.604 +            xd->mb_to_left_edge = -((mb_col * 16) << 3);
   1.605 +            xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
   1.606 +
   1.607 +#if CONFIG_ERROR_CONCEALMENT
   1.608 +            {
   1.609 +                int corrupt_residual = (!pbi->independent_partitions &&
   1.610 +                                       pbi->frame_corrupt_residual) ||
   1.611 +                                       vp8dx_bool_error(xd->current_bc);
   1.612 +                if (pbi->ec_active &&
   1.613 +                    xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME &&
   1.614 +                    corrupt_residual)
   1.615 +                {
   1.616 +                    /* We have an intra block with corrupt coefficients, better to
   1.617 +                     * conceal with an inter block. Interpolate MVs from neighboring
   1.618 +                     * MBs.
   1.619 +                     *
   1.620 +                     * Note that for the first mb with corrupt residual in a frame,
   1.621 +                     * we might not discover that before decoding the residual. That
   1.622 +                     * happens after this check, and therefore no inter concealment
   1.623 +                     * will be done.
   1.624 +                     */
   1.625 +                    vp8_interpolate_motion(xd,
   1.626 +                                           mb_row, mb_col,
   1.627 +                                           pc->mb_rows, pc->mb_cols,
   1.628 +                                           pc->mode_info_stride);
   1.629 +                }
   1.630 +            }
   1.631 +#endif
   1.632 +
   1.633 +            xd->dst.y_buffer = dst_buffer[0] + recon_yoffset;
   1.634 +            xd->dst.u_buffer = dst_buffer[1] + recon_uvoffset;
   1.635 +            xd->dst.v_buffer = dst_buffer[2] + recon_uvoffset;
   1.636 +
   1.637 +            xd->pre.y_buffer = ref_buffer[xd->mode_info_context->mbmi.ref_frame][0] + recon_yoffset;
   1.638 +            xd->pre.u_buffer = ref_buffer[xd->mode_info_context->mbmi.ref_frame][1] + recon_uvoffset;
   1.639 +            xd->pre.v_buffer = ref_buffer[xd->mode_info_context->mbmi.ref_frame][2] + recon_uvoffset;
   1.640 +
   1.641 +            /* propagate errors from reference frames */
   1.642 +            xd->corrupted |= ref_fb_corrupted[xd->mode_info_context->mbmi.ref_frame];
   1.643 +
   1.644 +            decode_macroblock(pbi, xd, mb_idx);
   1.645 +
   1.646 +            mb_idx++;
   1.647 +            xd->left_available = 1;
   1.648 +
   1.649 +            /* check if the boolean decoder has suffered an error */
   1.650 +            xd->corrupted |= vp8dx_bool_error(xd->current_bc);
   1.651 +
   1.652 +            xd->recon_above[0] += 16;
   1.653 +            xd->recon_above[1] += 8;
   1.654 +            xd->recon_above[2] += 8;
   1.655 +            xd->recon_left[0] += 16;
   1.656 +            xd->recon_left[1] += 8;
   1.657 +            xd->recon_left[2] += 8;
   1.658 +
   1.659 +            recon_yoffset += 16;
   1.660 +            recon_uvoffset += 8;
   1.661 +
   1.662 +            ++xd->mode_info_context;  /* next mb */
   1.663 +
   1.664 +            xd->above_context++;
   1.665 +        }
   1.666 +
   1.667 +        /* adjust to the next row of mbs */
   1.668 +        vp8_extend_mb_row(yv12_fb_new, xd->dst.y_buffer + 16,
   1.669 +                          xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
   1.670 +
   1.671 +        ++xd->mode_info_context;      /* skip prediction column */
   1.672 +        xd->up_available = 1;
   1.673 +
   1.674 +        if(pc->filter_level)
   1.675 +        {
   1.676 +            if(mb_row > 0)
   1.677 +            {
   1.678 +                if (pc->filter_type == NORMAL_LOOPFILTER)
   1.679 +                    vp8_loop_filter_row_normal(pc, lf_mic, mb_row-1,
   1.680 +                                               recon_y_stride, recon_uv_stride,
   1.681 +                                               lf_dst[0], lf_dst[1], lf_dst[2]);
   1.682 +                else
   1.683 +                    vp8_loop_filter_row_simple(pc, lf_mic, mb_row-1,
   1.684 +                                               recon_y_stride, recon_uv_stride,
   1.685 +                                               lf_dst[0], lf_dst[1], lf_dst[2]);
   1.686 +
   1.687 +                if(mb_row > 1)
   1.688 +                {
   1.689 +                    yv12_extend_frame_left_right_c(yv12_fb_new,
   1.690 +                                                   eb_dst[0],
   1.691 +                                                   eb_dst[1],
   1.692 +                                                   eb_dst[2]);
   1.693 +
   1.694 +                    eb_dst[0] += recon_y_stride  * 16;
   1.695 +                    eb_dst[1] += recon_uv_stride *  8;
   1.696 +                    eb_dst[2] += recon_uv_stride *  8;
   1.697 +
   1.698 +                    if(mb_row == 2)
   1.699 +                        yv12_extend_frame_top_c(yv12_fb_new);
   1.700 +
   1.701 +                }
   1.702 +
   1.703 +                lf_dst[0] += recon_y_stride  * 16;
   1.704 +                lf_dst[1] += recon_uv_stride *  8;
   1.705 +                lf_dst[2] += recon_uv_stride *  8;
   1.706 +                lf_mic += pc->mb_cols;
   1.707 +                lf_mic++;         /* Skip border mb */
   1.708 +            }
   1.709 +        }
   1.710 +        else
   1.711 +        {
   1.712 +            if(mb_row > 0)
   1.713 +            {
   1.714 +                /**/
   1.715 +                yv12_extend_frame_left_right_c(yv12_fb_new,
   1.716 +                                               eb_dst[0],
   1.717 +                                               eb_dst[1],
   1.718 +                                               eb_dst[2]);
   1.719 +
   1.720 +                eb_dst[0] += recon_y_stride  * 16;
   1.721 +                eb_dst[1] += recon_uv_stride *  8;
   1.722 +                eb_dst[2] += recon_uv_stride *  8;
   1.723 +
   1.724 +                if(mb_row == 1)
   1.725 +                    yv12_extend_frame_top_c(yv12_fb_new);
   1.726 +            }
   1.727 +        }
   1.728 +    }
   1.729 +
   1.730 +    if(pc->filter_level)
   1.731 +    {
   1.732 +        if (pc->filter_type == NORMAL_LOOPFILTER)
   1.733 +            vp8_loop_filter_row_normal(pc, lf_mic, mb_row-1, recon_y_stride,
   1.734 +                                       recon_uv_stride, lf_dst[0], lf_dst[1],
   1.735 +                                       lf_dst[2]);
   1.736 +        else
   1.737 +            vp8_loop_filter_row_simple(pc, lf_mic, mb_row-1, recon_y_stride,
   1.738 +                                       recon_uv_stride, lf_dst[0], lf_dst[1],
   1.739 +                                       lf_dst[2]);
   1.740 +
   1.741 +        yv12_extend_frame_left_right_c(yv12_fb_new,
   1.742 +                                       eb_dst[0],
   1.743 +                                       eb_dst[1],
   1.744 +                                       eb_dst[2]);
   1.745 +        eb_dst[0] += recon_y_stride  * 16;
   1.746 +        eb_dst[1] += recon_uv_stride *  8;
   1.747 +        eb_dst[2] += recon_uv_stride *  8;
   1.748 +    }
   1.749 +    yv12_extend_frame_left_right_c(yv12_fb_new,
   1.750 +                                   eb_dst[0],
   1.751 +                                   eb_dst[1],
   1.752 +                                   eb_dst[2]);
   1.753 +
   1.754 +    yv12_extend_frame_bottom_c(yv12_fb_new);
   1.755 +
   1.756 +}
   1.757 +
   1.758 +static unsigned int read_partition_size(VP8D_COMP *pbi,
   1.759 +                                        const unsigned char *cx_size)
   1.760 +{
   1.761 +    unsigned char temp[3];
   1.762 +    if (pbi->decrypt_cb)
   1.763 +    {
   1.764 +        pbi->decrypt_cb(pbi->decrypt_state, cx_size, temp, 3);
   1.765 +        cx_size = temp;
   1.766 +    }
   1.767 +    return cx_size[0] + (cx_size[1] << 8) + (cx_size[2] << 16);
   1.768 +}
   1.769 +
   1.770 +static int read_is_valid(const unsigned char *start,
   1.771 +                         size_t               len,
   1.772 +                         const unsigned char *end)
   1.773 +{
   1.774 +    return (start + len > start && start + len <= end);
   1.775 +}
   1.776 +
   1.777 +static unsigned int read_available_partition_size(
   1.778 +                                       VP8D_COMP *pbi,
   1.779 +                                       const unsigned char *token_part_sizes,
   1.780 +                                       const unsigned char *fragment_start,
   1.781 +                                       const unsigned char *first_fragment_end,
   1.782 +                                       const unsigned char *fragment_end,
   1.783 +                                       int i,
   1.784 +                                       int num_part)
   1.785 +{
   1.786 +    VP8_COMMON* pc = &pbi->common;
   1.787 +    const unsigned char *partition_size_ptr = token_part_sizes + i * 3;
   1.788 +    unsigned int partition_size = 0;
   1.789 +    ptrdiff_t bytes_left = fragment_end - fragment_start;
   1.790 +    /* Calculate the length of this partition. The last partition
   1.791 +     * size is implicit. If the partition size can't be read, then
   1.792 +     * either use the remaining data in the buffer (for EC mode)
   1.793 +     * or throw an error.
   1.794 +     */
   1.795 +    if (i < num_part - 1)
   1.796 +    {
   1.797 +        if (read_is_valid(partition_size_ptr, 3, first_fragment_end))
   1.798 +            partition_size = read_partition_size(pbi, partition_size_ptr);
   1.799 +        else if (pbi->ec_active)
   1.800 +            partition_size = (unsigned int)bytes_left;
   1.801 +        else
   1.802 +            vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
   1.803 +                               "Truncated partition size data");
   1.804 +    }
   1.805 +    else
   1.806 +        partition_size = (unsigned int)bytes_left;
   1.807 +
   1.808 +    /* Validate the calculated partition length. If the buffer
   1.809 +     * described by the partition can't be fully read, then restrict
   1.810 +     * it to the portion that can be (for EC mode) or throw an error.
   1.811 +     */
   1.812 +    if (!read_is_valid(fragment_start, partition_size, fragment_end))
   1.813 +    {
   1.814 +        if (pbi->ec_active)
   1.815 +            partition_size = (unsigned int)bytes_left;
   1.816 +        else
   1.817 +            vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
   1.818 +                               "Truncated packet or corrupt partition "
   1.819 +                               "%d length", i + 1);
   1.820 +    }
   1.821 +    return partition_size;
   1.822 +}
   1.823 +
   1.824 +
   1.825 +static void setup_token_decoder(VP8D_COMP *pbi,
   1.826 +                                const unsigned char* token_part_sizes)
   1.827 +{
   1.828 +    vp8_reader *bool_decoder = &pbi->mbc[0];
   1.829 +    unsigned int partition_idx;
   1.830 +    unsigned int fragment_idx;
   1.831 +    unsigned int num_token_partitions;
   1.832 +    const unsigned char *first_fragment_end = pbi->fragments.ptrs[0] +
   1.833 +                                          pbi->fragments.sizes[0];
   1.834 +
   1.835 +    TOKEN_PARTITION multi_token_partition =
   1.836 +            (TOKEN_PARTITION)vp8_read_literal(&pbi->mbc[8], 2);
   1.837 +    if (!vp8dx_bool_error(&pbi->mbc[8]))
   1.838 +        pbi->common.multi_token_partition = multi_token_partition;
   1.839 +    num_token_partitions = 1 << pbi->common.multi_token_partition;
   1.840 +
   1.841 +    /* Check for partitions within the fragments and unpack the fragments
   1.842 +     * so that each fragment pointer points to its corresponding partition. */
   1.843 +    for (fragment_idx = 0; fragment_idx < pbi->fragments.count; ++fragment_idx)
   1.844 +    {
   1.845 +        unsigned int fragment_size = pbi->fragments.sizes[fragment_idx];
   1.846 +        const unsigned char *fragment_end = pbi->fragments.ptrs[fragment_idx] +
   1.847 +                                            fragment_size;
   1.848 +        /* Special case for handling the first partition since we have already
   1.849 +         * read its size. */
   1.850 +        if (fragment_idx == 0)
   1.851 +        {
   1.852 +            /* Size of first partition + token partition sizes element */
   1.853 +            ptrdiff_t ext_first_part_size = token_part_sizes -
   1.854 +                pbi->fragments.ptrs[0] + 3 * (num_token_partitions - 1);
   1.855 +            fragment_size -= (unsigned int)ext_first_part_size;
   1.856 +            if (fragment_size > 0)
   1.857 +            {
   1.858 +                pbi->fragments.sizes[0] = (unsigned int)ext_first_part_size;
   1.859 +                /* The fragment contains an additional partition. Move to
   1.860 +                 * next. */
   1.861 +                fragment_idx++;
   1.862 +                pbi->fragments.ptrs[fragment_idx] = pbi->fragments.ptrs[0] +
   1.863 +                  pbi->fragments.sizes[0];
   1.864 +            }
   1.865 +        }
   1.866 +        /* Split the chunk into partitions read from the bitstream */
   1.867 +        while (fragment_size > 0)
   1.868 +        {
   1.869 +            ptrdiff_t partition_size = read_available_partition_size(
   1.870 +                                                 pbi,
   1.871 +                                                 token_part_sizes,
   1.872 +                                                 pbi->fragments.ptrs[fragment_idx],
   1.873 +                                                 first_fragment_end,
   1.874 +                                                 fragment_end,
   1.875 +                                                 fragment_idx - 1,
   1.876 +                                                 num_token_partitions);
   1.877 +            pbi->fragments.sizes[fragment_idx] = (unsigned int)partition_size;
   1.878 +            fragment_size -= (unsigned int)partition_size;
   1.879 +            assert(fragment_idx <= num_token_partitions);
   1.880 +            if (fragment_size > 0)
   1.881 +            {
   1.882 +                /* The fragment contains an additional partition.
   1.883 +                 * Move to next. */
   1.884 +                fragment_idx++;
   1.885 +                pbi->fragments.ptrs[fragment_idx] =
   1.886 +                    pbi->fragments.ptrs[fragment_idx - 1] + partition_size;
   1.887 +            }
   1.888 +        }
   1.889 +    }
   1.890 +
   1.891 +    pbi->fragments.count = num_token_partitions + 1;
   1.892 +
   1.893 +    for (partition_idx = 1; partition_idx < pbi->fragments.count; ++partition_idx)
   1.894 +    {
   1.895 +        if (vp8dx_start_decode(bool_decoder,
   1.896 +                               pbi->fragments.ptrs[partition_idx],
   1.897 +                               pbi->fragments.sizes[partition_idx],
   1.898 +                               pbi->decrypt_cb, pbi->decrypt_state))
   1.899 +            vpx_internal_error(&pbi->common.error, VPX_CODEC_MEM_ERROR,
   1.900 +                               "Failed to allocate bool decoder %d",
   1.901 +                               partition_idx);
   1.902 +
   1.903 +        bool_decoder++;
   1.904 +    }
   1.905 +
   1.906 +#if CONFIG_MULTITHREAD
   1.907 +    /* Clamp number of decoder threads */
   1.908 +    if (pbi->decoding_thread_count > num_token_partitions - 1)
   1.909 +        pbi->decoding_thread_count = num_token_partitions - 1;
   1.910 +#endif
   1.911 +}
   1.912 +
   1.913 +
   1.914 +static void init_frame(VP8D_COMP *pbi)
   1.915 +{
   1.916 +    VP8_COMMON *const pc = & pbi->common;
   1.917 +    MACROBLOCKD *const xd  = & pbi->mb;
   1.918 +
   1.919 +    if (pc->frame_type == KEY_FRAME)
   1.920 +    {
   1.921 +        /* Various keyframe initializations */
   1.922 +        vpx_memcpy(pc->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
   1.923 +
   1.924 +        vp8_init_mbmode_probs(pc);
   1.925 +
   1.926 +        vp8_default_coef_probs(pc);
   1.927 +
   1.928 +        /* reset the segment feature data to 0 with delta coding (Default state). */
   1.929 +        vpx_memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
   1.930 +        xd->mb_segement_abs_delta = SEGMENT_DELTADATA;
   1.931 +
   1.932 +        /* reset the mode ref deltasa for loop filter */
   1.933 +        vpx_memset(xd->ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
   1.934 +        vpx_memset(xd->mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
   1.935 +
   1.936 +        /* All buffers are implicitly updated on key frames. */
   1.937 +        pc->refresh_golden_frame = 1;
   1.938 +        pc->refresh_alt_ref_frame = 1;
   1.939 +        pc->copy_buffer_to_gf = 0;
   1.940 +        pc->copy_buffer_to_arf = 0;
   1.941 +
   1.942 +        /* Note that Golden and Altref modes cannot be used on a key frame so
   1.943 +         * ref_frame_sign_bias[] is undefined and meaningless
   1.944 +         */
   1.945 +        pc->ref_frame_sign_bias[GOLDEN_FRAME] = 0;
   1.946 +        pc->ref_frame_sign_bias[ALTREF_FRAME] = 0;
   1.947 +    }
   1.948 +    else
   1.949 +    {
   1.950 +        /* To enable choice of different interploation filters */
   1.951 +        if (!pc->use_bilinear_mc_filter)
   1.952 +        {
   1.953 +            xd->subpixel_predict        = vp8_sixtap_predict4x4;
   1.954 +            xd->subpixel_predict8x4     = vp8_sixtap_predict8x4;
   1.955 +            xd->subpixel_predict8x8     = vp8_sixtap_predict8x8;
   1.956 +            xd->subpixel_predict16x16   = vp8_sixtap_predict16x16;
   1.957 +        }
   1.958 +        else
   1.959 +        {
   1.960 +            xd->subpixel_predict        = vp8_bilinear_predict4x4;
   1.961 +            xd->subpixel_predict8x4     = vp8_bilinear_predict8x4;
   1.962 +            xd->subpixel_predict8x8     = vp8_bilinear_predict8x8;
   1.963 +            xd->subpixel_predict16x16   = vp8_bilinear_predict16x16;
   1.964 +        }
   1.965 +
   1.966 +        if (pbi->decoded_key_frame && pbi->ec_enabled && !pbi->ec_active)
   1.967 +            pbi->ec_active = 1;
   1.968 +    }
   1.969 +
   1.970 +    xd->left_context = &pc->left_context;
   1.971 +    xd->mode_info_context = pc->mi;
   1.972 +    xd->frame_type = pc->frame_type;
   1.973 +    xd->mode_info_context->mbmi.mode = DC_PRED;
   1.974 +    xd->mode_info_stride = pc->mode_info_stride;
   1.975 +    xd->corrupted = 0; /* init without corruption */
   1.976 +
   1.977 +    xd->fullpixel_mask = 0xffffffff;
   1.978 +    if(pc->full_pixel)
   1.979 +        xd->fullpixel_mask = 0xfffffff8;
   1.980 +
   1.981 +}
   1.982 +
   1.983 +int vp8_decode_frame(VP8D_COMP *pbi)
   1.984 +{
   1.985 +    vp8_reader *const bc = &pbi->mbc[8];
   1.986 +    VP8_COMMON *const pc = &pbi->common;
   1.987 +    MACROBLOCKD *const xd  = &pbi->mb;
   1.988 +    const unsigned char *data = pbi->fragments.ptrs[0];
   1.989 +    const unsigned char *data_end =  data + pbi->fragments.sizes[0];
   1.990 +    ptrdiff_t first_partition_length_in_bytes;
   1.991 +
   1.992 +    int i, j, k, l;
   1.993 +    const int *const mb_feature_data_bits = vp8_mb_feature_data_bits;
   1.994 +    int corrupt_tokens = 0;
   1.995 +    int prev_independent_partitions = pbi->independent_partitions;
   1.996 +
   1.997 +    YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME];
   1.998 +
   1.999 +    /* start with no corruption of current frame */
  1.1000 +    xd->corrupted = 0;
  1.1001 +    yv12_fb_new->corrupted = 0;
  1.1002 +
  1.1003 +    if (data_end - data < 3)
  1.1004 +    {
  1.1005 +        if (!pbi->ec_active)
  1.1006 +        {
  1.1007 +            vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
  1.1008 +                               "Truncated packet");
  1.1009 +        }
  1.1010 +
  1.1011 +        /* Declare the missing frame as an inter frame since it will
  1.1012 +           be handled as an inter frame when we have estimated its
  1.1013 +           motion vectors. */
  1.1014 +        pc->frame_type = INTER_FRAME;
  1.1015 +        pc->version = 0;
  1.1016 +        pc->show_frame = 1;
  1.1017 +        first_partition_length_in_bytes = 0;
  1.1018 +    }
  1.1019 +    else
  1.1020 +    {
  1.1021 +        unsigned char clear_buffer[10];
  1.1022 +        const unsigned char *clear = data;
  1.1023 +        if (pbi->decrypt_cb)
  1.1024 +        {
  1.1025 +            int n = (int)(data_end - data);
  1.1026 +            if (n > 10) n = 10;
  1.1027 +            pbi->decrypt_cb(pbi->decrypt_state, data, clear_buffer, n);
  1.1028 +            clear = clear_buffer;
  1.1029 +        }
  1.1030 +
  1.1031 +        pc->frame_type = (FRAME_TYPE)(clear[0] & 1);
  1.1032 +        pc->version = (clear[0] >> 1) & 7;
  1.1033 +        pc->show_frame = (clear[0] >> 4) & 1;
  1.1034 +        first_partition_length_in_bytes =
  1.1035 +            (clear[0] | (clear[1] << 8) | (clear[2] << 16)) >> 5;
  1.1036 +
  1.1037 +        if (!pbi->ec_active &&
  1.1038 +            (data + first_partition_length_in_bytes > data_end
  1.1039 +            || data + first_partition_length_in_bytes < data))
  1.1040 +            vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
  1.1041 +                               "Truncated packet or corrupt partition 0 length");
  1.1042 +
  1.1043 +        data += 3;
  1.1044 +        clear += 3;
  1.1045 +
  1.1046 +        vp8_setup_version(pc);
  1.1047 +
  1.1048 +
  1.1049 +        if (pc->frame_type == KEY_FRAME)
  1.1050 +        {
  1.1051 +            /* vet via sync code */
  1.1052 +            /* When error concealment is enabled we should only check the sync
  1.1053 +             * code if we have enough bits available
  1.1054 +             */
  1.1055 +            if (!pbi->ec_active || data + 3 < data_end)
  1.1056 +            {
  1.1057 +                if (clear[0] != 0x9d || clear[1] != 0x01 || clear[2] != 0x2a)
  1.1058 +                    vpx_internal_error(&pc->error, VPX_CODEC_UNSUP_BITSTREAM,
  1.1059 +                                   "Invalid frame sync code");
  1.1060 +            }
  1.1061 +
  1.1062 +            /* If error concealment is enabled we should only parse the new size
  1.1063 +             * if we have enough data. Otherwise we will end up with the wrong
  1.1064 +             * size.
  1.1065 +             */
  1.1066 +            if (!pbi->ec_active || data + 6 < data_end)
  1.1067 +            {
  1.1068 +                pc->Width = (clear[3] | (clear[4] << 8)) & 0x3fff;
  1.1069 +                pc->horiz_scale = clear[4] >> 6;
  1.1070 +                pc->Height = (clear[5] | (clear[6] << 8)) & 0x3fff;
  1.1071 +                pc->vert_scale = clear[6] >> 6;
  1.1072 +            }
  1.1073 +            data += 7;
  1.1074 +            clear += 7;
  1.1075 +        }
  1.1076 +        else
  1.1077 +        {
  1.1078 +          vpx_memcpy(&xd->pre, yv12_fb_new, sizeof(YV12_BUFFER_CONFIG));
  1.1079 +          vpx_memcpy(&xd->dst, yv12_fb_new, sizeof(YV12_BUFFER_CONFIG));
  1.1080 +        }
  1.1081 +    }
  1.1082 +    if ((!pbi->decoded_key_frame && pc->frame_type != KEY_FRAME))
  1.1083 +    {
  1.1084 +        return -1;
  1.1085 +    }
  1.1086 +
  1.1087 +    init_frame(pbi);
  1.1088 +
  1.1089 +    if (vp8dx_start_decode(bc, data, (unsigned int)(data_end - data),
  1.1090 +                           pbi->decrypt_cb, pbi->decrypt_state))
  1.1091 +        vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
  1.1092 +                           "Failed to allocate bool decoder 0");
  1.1093 +    if (pc->frame_type == KEY_FRAME) {
  1.1094 +        (void)vp8_read_bit(bc);  // colorspace
  1.1095 +        pc->clamp_type  = (CLAMP_TYPE)vp8_read_bit(bc);
  1.1096 +    }
  1.1097 +
  1.1098 +    /* Is segmentation enabled */
  1.1099 +    xd->segmentation_enabled = (unsigned char)vp8_read_bit(bc);
  1.1100 +
  1.1101 +    if (xd->segmentation_enabled)
  1.1102 +    {
  1.1103 +        /* Signal whether or not the segmentation map is being explicitly updated this frame. */
  1.1104 +        xd->update_mb_segmentation_map = (unsigned char)vp8_read_bit(bc);
  1.1105 +        xd->update_mb_segmentation_data = (unsigned char)vp8_read_bit(bc);
  1.1106 +
  1.1107 +        if (xd->update_mb_segmentation_data)
  1.1108 +        {
  1.1109 +            xd->mb_segement_abs_delta = (unsigned char)vp8_read_bit(bc);
  1.1110 +
  1.1111 +            vpx_memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
  1.1112 +
  1.1113 +            /* For each segmentation feature (Quant and loop filter level) */
  1.1114 +            for (i = 0; i < MB_LVL_MAX; i++)
  1.1115 +            {
  1.1116 +                for (j = 0; j < MAX_MB_SEGMENTS; j++)
  1.1117 +                {
  1.1118 +                    /* Frame level data */
  1.1119 +                    if (vp8_read_bit(bc))
  1.1120 +                    {
  1.1121 +                        xd->segment_feature_data[i][j] = (signed char)vp8_read_literal(bc, mb_feature_data_bits[i]);
  1.1122 +
  1.1123 +                        if (vp8_read_bit(bc))
  1.1124 +                            xd->segment_feature_data[i][j] = -xd->segment_feature_data[i][j];
  1.1125 +                    }
  1.1126 +                    else
  1.1127 +                        xd->segment_feature_data[i][j] = 0;
  1.1128 +                }
  1.1129 +            }
  1.1130 +        }
  1.1131 +
  1.1132 +        if (xd->update_mb_segmentation_map)
  1.1133 +        {
  1.1134 +            /* Which macro block level features are enabled */
  1.1135 +            vpx_memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));
  1.1136 +
  1.1137 +            /* Read the probs used to decode the segment id for each macro block. */
  1.1138 +            for (i = 0; i < MB_FEATURE_TREE_PROBS; i++)
  1.1139 +            {
  1.1140 +                /* If not explicitly set value is defaulted to 255 by memset above */
  1.1141 +                if (vp8_read_bit(bc))
  1.1142 +                    xd->mb_segment_tree_probs[i] = (vp8_prob)vp8_read_literal(bc, 8);
  1.1143 +            }
  1.1144 +        }
  1.1145 +    }
  1.1146 +    else
  1.1147 +    {
  1.1148 +        /* No segmentation updates on this frame */
  1.1149 +        xd->update_mb_segmentation_map = 0;
  1.1150 +        xd->update_mb_segmentation_data = 0;
  1.1151 +    }
  1.1152 +
  1.1153 +    /* Read the loop filter level and type */
  1.1154 +    pc->filter_type = (LOOPFILTERTYPE) vp8_read_bit(bc);
  1.1155 +    pc->filter_level = vp8_read_literal(bc, 6);
  1.1156 +    pc->sharpness_level = vp8_read_literal(bc, 3);
  1.1157 +
  1.1158 +    /* Read in loop filter deltas applied at the MB level based on mode or ref frame. */
  1.1159 +    xd->mode_ref_lf_delta_update = 0;
  1.1160 +    xd->mode_ref_lf_delta_enabled = (unsigned char)vp8_read_bit(bc);
  1.1161 +
  1.1162 +    if (xd->mode_ref_lf_delta_enabled)
  1.1163 +    {
  1.1164 +        /* Do the deltas need to be updated */
  1.1165 +        xd->mode_ref_lf_delta_update = (unsigned char)vp8_read_bit(bc);
  1.1166 +
  1.1167 +        if (xd->mode_ref_lf_delta_update)
  1.1168 +        {
  1.1169 +            /* Send update */
  1.1170 +            for (i = 0; i < MAX_REF_LF_DELTAS; i++)
  1.1171 +            {
  1.1172 +                if (vp8_read_bit(bc))
  1.1173 +                {
  1.1174 +                    /*sign = vp8_read_bit( bc );*/
  1.1175 +                    xd->ref_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
  1.1176 +
  1.1177 +                    if (vp8_read_bit(bc))        /* Apply sign */
  1.1178 +                        xd->ref_lf_deltas[i] = xd->ref_lf_deltas[i] * -1;
  1.1179 +                }
  1.1180 +            }
  1.1181 +
  1.1182 +            /* Send update */
  1.1183 +            for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
  1.1184 +            {
  1.1185 +                if (vp8_read_bit(bc))
  1.1186 +                {
  1.1187 +                    /*sign = vp8_read_bit( bc );*/
  1.1188 +                    xd->mode_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
  1.1189 +
  1.1190 +                    if (vp8_read_bit(bc))        /* Apply sign */
  1.1191 +                        xd->mode_lf_deltas[i] = xd->mode_lf_deltas[i] * -1;
  1.1192 +                }
  1.1193 +            }
  1.1194 +        }
  1.1195 +    }
  1.1196 +
  1.1197 +    setup_token_decoder(pbi, data + first_partition_length_in_bytes);
  1.1198 +
  1.1199 +    xd->current_bc = &pbi->mbc[0];
  1.1200 +
  1.1201 +    /* Read the default quantizers. */
  1.1202 +    {
  1.1203 +        int Q, q_update;
  1.1204 +
  1.1205 +        Q = vp8_read_literal(bc, 7);  /* AC 1st order Q = default */
  1.1206 +        pc->base_qindex = Q;
  1.1207 +        q_update = 0;
  1.1208 +        pc->y1dc_delta_q = get_delta_q(bc, pc->y1dc_delta_q, &q_update);
  1.1209 +        pc->y2dc_delta_q = get_delta_q(bc, pc->y2dc_delta_q, &q_update);
  1.1210 +        pc->y2ac_delta_q = get_delta_q(bc, pc->y2ac_delta_q, &q_update);
  1.1211 +        pc->uvdc_delta_q = get_delta_q(bc, pc->uvdc_delta_q, &q_update);
  1.1212 +        pc->uvac_delta_q = get_delta_q(bc, pc->uvac_delta_q, &q_update);
  1.1213 +
  1.1214 +        if (q_update)
  1.1215 +            vp8cx_init_de_quantizer(pbi);
  1.1216 +
  1.1217 +        /* MB level dequantizer setup */
  1.1218 +        vp8_mb_init_dequantizer(pbi, &pbi->mb);
  1.1219 +    }
  1.1220 +
  1.1221 +    /* Determine if the golden frame or ARF buffer should be updated and how.
  1.1222 +     * For all non key frames the GF and ARF refresh flags and sign bias
  1.1223 +     * flags must be set explicitly.
  1.1224 +     */
  1.1225 +    if (pc->frame_type != KEY_FRAME)
  1.1226 +    {
  1.1227 +        /* Should the GF or ARF be updated from the current frame */
  1.1228 +        pc->refresh_golden_frame = vp8_read_bit(bc);
  1.1229 +#if CONFIG_ERROR_CONCEALMENT
  1.1230 +        /* Assume we shouldn't refresh golden if the bit is missing */
  1.1231 +        xd->corrupted |= vp8dx_bool_error(bc);
  1.1232 +        if (pbi->ec_active && xd->corrupted)
  1.1233 +            pc->refresh_golden_frame = 0;
  1.1234 +#endif
  1.1235 +
  1.1236 +        pc->refresh_alt_ref_frame = vp8_read_bit(bc);
  1.1237 +#if CONFIG_ERROR_CONCEALMENT
  1.1238 +        /* Assume we shouldn't refresh altref if the bit is missing */
  1.1239 +        xd->corrupted |= vp8dx_bool_error(bc);
  1.1240 +        if (pbi->ec_active && xd->corrupted)
  1.1241 +            pc->refresh_alt_ref_frame = 0;
  1.1242 +#endif
  1.1243 +
  1.1244 +        /* Buffer to buffer copy flags. */
  1.1245 +        pc->copy_buffer_to_gf = 0;
  1.1246 +
  1.1247 +        if (!pc->refresh_golden_frame)
  1.1248 +            pc->copy_buffer_to_gf = vp8_read_literal(bc, 2);
  1.1249 +
  1.1250 +#if CONFIG_ERROR_CONCEALMENT
  1.1251 +        /* Assume we shouldn't copy to the golden if the bit is missing */
  1.1252 +        xd->corrupted |= vp8dx_bool_error(bc);
  1.1253 +        if (pbi->ec_active && xd->corrupted)
  1.1254 +            pc->copy_buffer_to_gf = 0;
  1.1255 +#endif
  1.1256 +
  1.1257 +        pc->copy_buffer_to_arf = 0;
  1.1258 +
  1.1259 +        if (!pc->refresh_alt_ref_frame)
  1.1260 +            pc->copy_buffer_to_arf = vp8_read_literal(bc, 2);
  1.1261 +
  1.1262 +#if CONFIG_ERROR_CONCEALMENT
  1.1263 +        /* Assume we shouldn't copy to the alt-ref if the bit is missing */
  1.1264 +        xd->corrupted |= vp8dx_bool_error(bc);
  1.1265 +        if (pbi->ec_active && xd->corrupted)
  1.1266 +            pc->copy_buffer_to_arf = 0;
  1.1267 +#endif
  1.1268 +
  1.1269 +
  1.1270 +        pc->ref_frame_sign_bias[GOLDEN_FRAME] = vp8_read_bit(bc);
  1.1271 +        pc->ref_frame_sign_bias[ALTREF_FRAME] = vp8_read_bit(bc);
  1.1272 +    }
  1.1273 +
  1.1274 +    pc->refresh_entropy_probs = vp8_read_bit(bc);
  1.1275 +#if CONFIG_ERROR_CONCEALMENT
  1.1276 +    /* Assume we shouldn't refresh the probabilities if the bit is
  1.1277 +     * missing */
  1.1278 +    xd->corrupted |= vp8dx_bool_error(bc);
  1.1279 +    if (pbi->ec_active && xd->corrupted)
  1.1280 +        pc->refresh_entropy_probs = 0;
  1.1281 +#endif
  1.1282 +    if (pc->refresh_entropy_probs == 0)
  1.1283 +    {
  1.1284 +        vpx_memcpy(&pc->lfc, &pc->fc, sizeof(pc->fc));
  1.1285 +    }
  1.1286 +
  1.1287 +    pc->refresh_last_frame = pc->frame_type == KEY_FRAME  ||  vp8_read_bit(bc);
  1.1288 +
  1.1289 +#if CONFIG_ERROR_CONCEALMENT
  1.1290 +    /* Assume we should refresh the last frame if the bit is missing */
  1.1291 +    xd->corrupted |= vp8dx_bool_error(bc);
  1.1292 +    if (pbi->ec_active && xd->corrupted)
  1.1293 +        pc->refresh_last_frame = 1;
  1.1294 +#endif
  1.1295 +
  1.1296 +    if (0)
  1.1297 +    {
  1.1298 +        FILE *z = fopen("decodestats.stt", "a");
  1.1299 +        fprintf(z, "%6d F:%d,G:%d,A:%d,L:%d,Q:%d\n",
  1.1300 +                pc->current_video_frame,
  1.1301 +                pc->frame_type,
  1.1302 +                pc->refresh_golden_frame,
  1.1303 +                pc->refresh_alt_ref_frame,
  1.1304 +                pc->refresh_last_frame,
  1.1305 +                pc->base_qindex);
  1.1306 +        fclose(z);
  1.1307 +    }
  1.1308 +
  1.1309 +    {
  1.1310 +        pbi->independent_partitions = 1;
  1.1311 +
  1.1312 +        /* read coef probability tree */
  1.1313 +        for (i = 0; i < BLOCK_TYPES; i++)
  1.1314 +            for (j = 0; j < COEF_BANDS; j++)
  1.1315 +                for (k = 0; k < PREV_COEF_CONTEXTS; k++)
  1.1316 +                    for (l = 0; l < ENTROPY_NODES; l++)
  1.1317 +                    {
  1.1318 +
  1.1319 +                        vp8_prob *const p = pc->fc.coef_probs [i][j][k] + l;
  1.1320 +
  1.1321 +                        if (vp8_read(bc, vp8_coef_update_probs [i][j][k][l]))
  1.1322 +                        {
  1.1323 +                            *p = (vp8_prob)vp8_read_literal(bc, 8);
  1.1324 +
  1.1325 +                        }
  1.1326 +                        if (k > 0 && *p != pc->fc.coef_probs[i][j][k-1][l])
  1.1327 +                            pbi->independent_partitions = 0;
  1.1328 +
  1.1329 +                    }
  1.1330 +    }
  1.1331 +
  1.1332 +    /* clear out the coeff buffer */
  1.1333 +    vpx_memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
  1.1334 +
  1.1335 +    vp8_decode_mode_mvs(pbi);
  1.1336 +
  1.1337 +#if CONFIG_ERROR_CONCEALMENT
  1.1338 +    if (pbi->ec_active &&
  1.1339 +            pbi->mvs_corrupt_from_mb < (unsigned int)pc->mb_cols * pc->mb_rows)
  1.1340 +    {
  1.1341 +        /* Motion vectors are missing in this frame. We will try to estimate
  1.1342 +         * them and then continue decoding the frame as usual */
  1.1343 +        vp8_estimate_missing_mvs(pbi);
  1.1344 +    }
  1.1345 +#endif
  1.1346 +
  1.1347 +    vpx_memset(pc->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) * pc->mb_cols);
  1.1348 +    pbi->frame_corrupt_residual = 0;
  1.1349 +
  1.1350 +#if CONFIG_MULTITHREAD
  1.1351 +    if (pbi->b_multithreaded_rd && pc->multi_token_partition != ONE_PARTITION)
  1.1352 +    {
  1.1353 +        unsigned int thread;
  1.1354 +        vp8mt_decode_mb_rows(pbi, xd);
  1.1355 +        vp8_yv12_extend_frame_borders(yv12_fb_new);
  1.1356 +        for (thread = 0; thread < pbi->decoding_thread_count; ++thread)
  1.1357 +            corrupt_tokens |= pbi->mb_row_di[thread].mbd.corrupted;
  1.1358 +    }
  1.1359 +    else
  1.1360 +#endif
  1.1361 +    {
  1.1362 +        decode_mb_rows(pbi);
  1.1363 +        corrupt_tokens |= xd->corrupted;
  1.1364 +    }
  1.1365 +
  1.1366 +    /* Collect information about decoder corruption. */
  1.1367 +    /* 1. Check first boolean decoder for errors. */
  1.1368 +    yv12_fb_new->corrupted = vp8dx_bool_error(bc);
  1.1369 +    /* 2. Check the macroblock information */
  1.1370 +    yv12_fb_new->corrupted |= corrupt_tokens;
  1.1371 +
  1.1372 +    if (!pbi->decoded_key_frame)
  1.1373 +    {
  1.1374 +        if (pc->frame_type == KEY_FRAME &&
  1.1375 +            !yv12_fb_new->corrupted)
  1.1376 +            pbi->decoded_key_frame = 1;
  1.1377 +        else
  1.1378 +            vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME,
  1.1379 +                               "A stream must start with a complete key frame");
  1.1380 +    }
  1.1381 +
  1.1382 +    /* vpx_log("Decoder: Frame Decoded, Size Roughly:%d bytes  \n",bc->pos+pbi->bc2.pos); */
  1.1383 +
  1.1384 +    if (pc->refresh_entropy_probs == 0)
  1.1385 +    {
  1.1386 +        vpx_memcpy(&pc->fc, &pc->lfc, sizeof(pc->fc));
  1.1387 +        pbi->independent_partitions = prev_independent_partitions;
  1.1388 +    }
  1.1389 +
  1.1390 +#ifdef PACKET_TESTING
  1.1391 +    {
  1.1392 +        FILE *f = fopen("decompressor.VP8", "ab");
  1.1393 +        unsigned int size = pbi->bc2.pos + pbi->bc.pos + 8;
  1.1394 +        fwrite((void *) &size, 4, 1, f);
  1.1395 +        fwrite((void *) pbi->Source, size, 1, f);
  1.1396 +        fclose(f);
  1.1397 +    }
  1.1398 +#endif
  1.1399 +
  1.1400 +    return 0;
  1.1401 +}

mercurial