media/libvpx/vp8/decoder/detokenize.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp8/decoder/detokenize.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,245 @@
     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 "vp8/common/blockd.h"
    1.16 +#include "onyxd_int.h"
    1.17 +#include "vpx_mem/vpx_mem.h"
    1.18 +#include "vpx_ports/mem.h"
    1.19 +#include "detokenize.h"
    1.20 +
    1.21 +void vp8_reset_mb_tokens_context(MACROBLOCKD *x)
    1.22 +{
    1.23 +    ENTROPY_CONTEXT *a_ctx = ((ENTROPY_CONTEXT *)x->above_context);
    1.24 +    ENTROPY_CONTEXT *l_ctx = ((ENTROPY_CONTEXT *)x->left_context);
    1.25 +
    1.26 +    vpx_memset(a_ctx, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
    1.27 +    vpx_memset(l_ctx, 0, sizeof(ENTROPY_CONTEXT_PLANES)-1);
    1.28 +
    1.29 +    /* Clear entropy contexts for Y2 blocks */
    1.30 +    if (!x->mode_info_context->mbmi.is_4x4)
    1.31 +    {
    1.32 +        a_ctx[8] = l_ctx[8] = 0;
    1.33 +    }
    1.34 +}
    1.35 +
    1.36 +/*
    1.37 +    ------------------------------------------------------------------------------
    1.38 +    Residual decoding (Paragraph 13.2 / 13.3)
    1.39 +*/
    1.40 +static const uint8_t kBands[16 + 1] = {
    1.41 +  0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7,
    1.42 +  0  /* extra entry as sentinel */
    1.43 +};
    1.44 +
    1.45 +static const uint8_t kCat3[] = { 173, 148, 140, 0 };
    1.46 +static const uint8_t kCat4[] = { 176, 155, 140, 135, 0 };
    1.47 +static const uint8_t kCat5[] = { 180, 157, 141, 134, 130, 0 };
    1.48 +static const uint8_t kCat6[] =
    1.49 +  { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129, 0 };
    1.50 +static const uint8_t* const kCat3456[] = { kCat3, kCat4, kCat5, kCat6 };
    1.51 +static const uint8_t kZigzag[16] = {
    1.52 +  0, 1, 4, 8,  5, 2, 3, 6,  9, 12, 13, 10,  7, 11, 14, 15
    1.53 +};
    1.54 +
    1.55 +#define VP8GetBit vp8dx_decode_bool
    1.56 +#define NUM_PROBAS  11
    1.57 +#define NUM_CTX  3
    1.58 +
    1.59 +/* for const-casting */
    1.60 +typedef const uint8_t (*ProbaArray)[NUM_CTX][NUM_PROBAS];
    1.61 +
    1.62 +static int GetSigned(BOOL_DECODER *br, int value_to_sign)
    1.63 +{
    1.64 +    int split = (br->range + 1) >> 1;
    1.65 +    VP8_BD_VALUE bigsplit = (VP8_BD_VALUE)split << (VP8_BD_VALUE_SIZE - 8);
    1.66 +    int v;
    1.67 +
    1.68 +    if(br->count < 0)
    1.69 +        vp8dx_bool_decoder_fill(br);
    1.70 +
    1.71 +    if ( br->value < bigsplit )
    1.72 +    {
    1.73 +        br->range = split;
    1.74 +        v= value_to_sign;
    1.75 +    }
    1.76 +    else
    1.77 +    {
    1.78 +        br->range = br->range-split;
    1.79 +        br->value = br->value-bigsplit;
    1.80 +        v = -value_to_sign;
    1.81 +    }
    1.82 +    br->range +=br->range;
    1.83 +    br->value +=br->value;
    1.84 +    br->count--;
    1.85 +
    1.86 +    return v;
    1.87 +}
    1.88 +/*
    1.89 +   Returns the position of the last non-zero coeff plus one
    1.90 +   (and 0 if there's no coeff at all)
    1.91 +*/
    1.92 +static int GetCoeffs(BOOL_DECODER *br, ProbaArray prob,
    1.93 +                     int ctx, int n, int16_t* out)
    1.94 +{
    1.95 +    const uint8_t* p = prob[n][ctx];
    1.96 +    if (!VP8GetBit(br, p[0]))
    1.97 +    {   /* first EOB is more a 'CBP' bit. */
    1.98 +        return 0;
    1.99 +    }
   1.100 +    while (1)
   1.101 +    {
   1.102 +        ++n;
   1.103 +        if (!VP8GetBit(br, p[1]))
   1.104 +        {
   1.105 +            p = prob[kBands[n]][0];
   1.106 +        }
   1.107 +        else
   1.108 +        {  /* non zero coeff */
   1.109 +            int v, j;
   1.110 +            if (!VP8GetBit(br, p[2]))
   1.111 +            {
   1.112 +                p = prob[kBands[n]][1];
   1.113 +                v = 1;
   1.114 +            }
   1.115 +            else
   1.116 +            {
   1.117 +                if (!VP8GetBit(br, p[3]))
   1.118 +                {
   1.119 +                    if (!VP8GetBit(br, p[4]))
   1.120 +                    {
   1.121 +                        v = 2;
   1.122 +                    }
   1.123 +                    else
   1.124 +                    {
   1.125 +                        v = 3 + VP8GetBit(br, p[5]);
   1.126 +                    }
   1.127 +                }
   1.128 +                else
   1.129 +                {
   1.130 +                    if (!VP8GetBit(br, p[6]))
   1.131 +                    {
   1.132 +                        if (!VP8GetBit(br, p[7]))
   1.133 +                        {
   1.134 +                            v = 5 + VP8GetBit(br, 159);
   1.135 +                        } else
   1.136 +                        {
   1.137 +                            v = 7 + 2 * VP8GetBit(br, 165);
   1.138 +                            v += VP8GetBit(br, 145);
   1.139 +                        }
   1.140 +                    }
   1.141 +                    else
   1.142 +                    {
   1.143 +                        const uint8_t* tab;
   1.144 +                        const int bit1 = VP8GetBit(br, p[8]);
   1.145 +                        const int bit0 = VP8GetBit(br, p[9 + bit1]);
   1.146 +                        const int cat = 2 * bit1 + bit0;
   1.147 +                        v = 0;
   1.148 +                        for (tab = kCat3456[cat]; *tab; ++tab)
   1.149 +                        {
   1.150 +                            v += v + VP8GetBit(br, *tab);
   1.151 +                        }
   1.152 +                        v += 3 + (8 << cat);
   1.153 +                    }
   1.154 +                }
   1.155 +                p = prob[kBands[n]][2];
   1.156 +            }
   1.157 +            j = kZigzag[n - 1];
   1.158 +
   1.159 +            out[j] = GetSigned(br, v);
   1.160 +
   1.161 +            if (n == 16 || !VP8GetBit(br, p[0]))
   1.162 +            {   /* EOB */
   1.163 +                return n;
   1.164 +            }
   1.165 +        }
   1.166 +        if (n == 16)
   1.167 +        {
   1.168 +            return 16;
   1.169 +        }
   1.170 +    }
   1.171 +}
   1.172 +
   1.173 +int vp8_decode_mb_tokens(VP8D_COMP *dx, MACROBLOCKD *x)
   1.174 +{
   1.175 +    BOOL_DECODER *bc = x->current_bc;
   1.176 +    const FRAME_CONTEXT * const fc = &dx->common.fc;
   1.177 +    char *eobs = x->eobs;
   1.178 +
   1.179 +    int i;
   1.180 +    int nonzeros;
   1.181 +    int eobtotal = 0;
   1.182 +
   1.183 +    short *qcoeff_ptr;
   1.184 +    ProbaArray coef_probs;
   1.185 +    ENTROPY_CONTEXT *a_ctx = ((ENTROPY_CONTEXT *)x->above_context);
   1.186 +    ENTROPY_CONTEXT *l_ctx = ((ENTROPY_CONTEXT *)x->left_context);
   1.187 +    ENTROPY_CONTEXT *a;
   1.188 +    ENTROPY_CONTEXT *l;
   1.189 +    int skip_dc = 0;
   1.190 +
   1.191 +    qcoeff_ptr = &x->qcoeff[0];
   1.192 +
   1.193 +    if (!x->mode_info_context->mbmi.is_4x4)
   1.194 +    {
   1.195 +        a = a_ctx + 8;
   1.196 +        l = l_ctx + 8;
   1.197 +
   1.198 +        coef_probs = fc->coef_probs [1];
   1.199 +
   1.200 +        nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), 0, qcoeff_ptr + 24 * 16);
   1.201 +        *a = *l = (nonzeros > 0);
   1.202 +
   1.203 +        eobs[24] = nonzeros;
   1.204 +        eobtotal += nonzeros - 16;
   1.205 +
   1.206 +        coef_probs = fc->coef_probs [0];
   1.207 +        skip_dc = 1;
   1.208 +    }
   1.209 +    else
   1.210 +    {
   1.211 +        coef_probs = fc->coef_probs [3];
   1.212 +        skip_dc = 0;
   1.213 +    }
   1.214 +
   1.215 +    for (i = 0; i < 16; ++i)
   1.216 +    {
   1.217 +        a = a_ctx + (i&3);
   1.218 +        l = l_ctx + ((i&0xc)>>2);
   1.219 +
   1.220 +        nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), skip_dc, qcoeff_ptr);
   1.221 +        *a = *l = (nonzeros > 0);
   1.222 +
   1.223 +        nonzeros += skip_dc;
   1.224 +        eobs[i] = nonzeros;
   1.225 +        eobtotal += nonzeros;
   1.226 +        qcoeff_ptr += 16;
   1.227 +    }
   1.228 +
   1.229 +    coef_probs = fc->coef_probs [2];
   1.230 +
   1.231 +    a_ctx += 4;
   1.232 +    l_ctx += 4;
   1.233 +    for (i = 16; i < 24; ++i)
   1.234 +    {
   1.235 +        a = a_ctx + ((i > 19)<<1) + (i&1);
   1.236 +        l = l_ctx + ((i > 19)<<1) + ((i&3)>1);
   1.237 +
   1.238 +        nonzeros = GetCoeffs(bc, coef_probs, (*a + *l), 0, qcoeff_ptr);
   1.239 +        *a = *l = (nonzeros > 0);
   1.240 +
   1.241 +        eobs[i] = nonzeros;
   1.242 +        eobtotal += nonzeros;
   1.243 +        qcoeff_ptr += 16;
   1.244 +    }
   1.245 +
   1.246 +    return eobtotal;
   1.247 +}
   1.248 +

mercurial