media/libvpx/vp8/encoder/quantize.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libvpx/vp8/encoder/quantize.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,815 @@
     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 <math.h>
    1.16 +#include "vpx_mem/vpx_mem.h"
    1.17 +
    1.18 +#include "onyx_int.h"
    1.19 +#include "quantize.h"
    1.20 +#include "vp8/common/quant_common.h"
    1.21 +
    1.22 +#define EXACT_QUANT
    1.23 +
    1.24 +#ifdef EXACT_FASTQUANT
    1.25 +void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
    1.26 +{
    1.27 +    int i, rc, eob;
    1.28 +    int zbin;
    1.29 +    int x, y, z, sz;
    1.30 +    short *coeff_ptr       = b->coeff;
    1.31 +    short *zbin_ptr        = b->zbin;
    1.32 +    short *round_ptr       = b->round;
    1.33 +    short *quant_ptr       = b->quant_fast;
    1.34 +    unsigned char *quant_shift_ptr = b->quant_shift;
    1.35 +    short *qcoeff_ptr      = d->qcoeff;
    1.36 +    short *dqcoeff_ptr     = d->dqcoeff;
    1.37 +    short *dequant_ptr     = d->dequant;
    1.38 +
    1.39 +    vpx_memset(qcoeff_ptr, 0, 32);
    1.40 +    vpx_memset(dqcoeff_ptr, 0, 32);
    1.41 +
    1.42 +    eob = -1;
    1.43 +
    1.44 +    for (i = 0; i < 16; i++)
    1.45 +    {
    1.46 +        rc   = vp8_default_zig_zag1d[i];
    1.47 +        z    = coeff_ptr[rc];
    1.48 +        zbin = zbin_ptr[rc] ;
    1.49 +
    1.50 +        sz = (z >> 31);                              /* sign of z */
    1.51 +        x  = (z ^ sz) - sz;                          /* x = abs(z) */
    1.52 +
    1.53 +        if (x >= zbin)
    1.54 +        {
    1.55 +            x += round_ptr[rc];
    1.56 +            y  = ((((x * quant_ptr[rc]) >> 16) + x)
    1.57 +                 * quant_shift_ptr[rc]) >> 16;       /* quantize (x) */
    1.58 +            x  = (y ^ sz) - sz;                      /* get the sign back */
    1.59 +            qcoeff_ptr[rc] = x;                      /* write to destination */
    1.60 +            dqcoeff_ptr[rc] = x * dequant_ptr[rc];   /* dequantized value */
    1.61 +
    1.62 +            if (y)
    1.63 +            {
    1.64 +                eob = i;                             /* last nonzero coeffs */
    1.65 +            }
    1.66 +        }
    1.67 +    }
    1.68 +    *d->eob = (char)(eob + 1);
    1.69 +}
    1.70 +
    1.71 +#else
    1.72 +
    1.73 +void vp8_fast_quantize_b_c(BLOCK *b, BLOCKD *d)
    1.74 +{
    1.75 +    int i, rc, eob;
    1.76 +    int x, y, z, sz;
    1.77 +    short *coeff_ptr   = b->coeff;
    1.78 +    short *round_ptr   = b->round;
    1.79 +    short *quant_ptr   = b->quant_fast;
    1.80 +    short *qcoeff_ptr  = d->qcoeff;
    1.81 +    short *dqcoeff_ptr = d->dqcoeff;
    1.82 +    short *dequant_ptr = d->dequant;
    1.83 +
    1.84 +    eob = -1;
    1.85 +    for (i = 0; i < 16; i++)
    1.86 +    {
    1.87 +        rc   = vp8_default_zig_zag1d[i];
    1.88 +        z    = coeff_ptr[rc];
    1.89 +
    1.90 +        sz = (z >> 31);                              /* sign of z */
    1.91 +        x  = (z ^ sz) - sz;                          /* x = abs(z) */
    1.92 +
    1.93 +        y  = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; /* quantize (x) */
    1.94 +        x  = (y ^ sz) - sz;                          /* get the sign back */
    1.95 +        qcoeff_ptr[rc] = x;                          /* write to destination */
    1.96 +        dqcoeff_ptr[rc] = x * dequant_ptr[rc];       /* dequantized value */
    1.97 +
    1.98 +        if (y)
    1.99 +        {
   1.100 +            eob = i;                                 /* last nonzero coeffs */
   1.101 +        }
   1.102 +    }
   1.103 +    *d->eob = (char)(eob + 1);
   1.104 +}
   1.105 +
   1.106 +#endif
   1.107 +
   1.108 +#ifdef EXACT_QUANT
   1.109 +void vp8_regular_quantize_b_c(BLOCK *b, BLOCKD *d)
   1.110 +{
   1.111 +    int i, rc, eob;
   1.112 +    int zbin;
   1.113 +    int x, y, z, sz;
   1.114 +    short *zbin_boost_ptr  = b->zrun_zbin_boost;
   1.115 +    short *coeff_ptr       = b->coeff;
   1.116 +    short *zbin_ptr        = b->zbin;
   1.117 +    short *round_ptr       = b->round;
   1.118 +    short *quant_ptr       = b->quant;
   1.119 +    short *quant_shift_ptr = b->quant_shift;
   1.120 +    short *qcoeff_ptr      = d->qcoeff;
   1.121 +    short *dqcoeff_ptr     = d->dqcoeff;
   1.122 +    short *dequant_ptr     = d->dequant;
   1.123 +    short zbin_oq_value    = b->zbin_extra;
   1.124 +
   1.125 +    vpx_memset(qcoeff_ptr, 0, 32);
   1.126 +    vpx_memset(dqcoeff_ptr, 0, 32);
   1.127 +
   1.128 +    eob = -1;
   1.129 +
   1.130 +    for (i = 0; i < 16; i++)
   1.131 +    {
   1.132 +        rc   = vp8_default_zig_zag1d[i];
   1.133 +        z    = coeff_ptr[rc];
   1.134 +
   1.135 +        zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
   1.136 +
   1.137 +        zbin_boost_ptr ++;
   1.138 +        sz = (z >> 31);                              /* sign of z */
   1.139 +        x  = (z ^ sz) - sz;                          /* x = abs(z) */
   1.140 +
   1.141 +        if (x >= zbin)
   1.142 +        {
   1.143 +            x += round_ptr[rc];
   1.144 +            y  = ((((x * quant_ptr[rc]) >> 16) + x)
   1.145 +                 * quant_shift_ptr[rc]) >> 16;       /* quantize (x) */
   1.146 +            x  = (y ^ sz) - sz;                      /* get the sign back */
   1.147 +            qcoeff_ptr[rc]  = x;                     /* write to destination */
   1.148 +            dqcoeff_ptr[rc] = x * dequant_ptr[rc];   /* dequantized value */
   1.149 +
   1.150 +            if (y)
   1.151 +            {
   1.152 +                eob = i;                             /* last nonzero coeffs */
   1.153 +                zbin_boost_ptr = b->zrun_zbin_boost; /* reset zero runlength */
   1.154 +            }
   1.155 +        }
   1.156 +    }
   1.157 +
   1.158 +    *d->eob = (char)(eob + 1);
   1.159 +}
   1.160 +
   1.161 +/* Perform regular quantization, with unbiased rounding and no zero bin. */
   1.162 +void vp8_strict_quantize_b_c(BLOCK *b, BLOCKD *d)
   1.163 +{
   1.164 +    int i;
   1.165 +    int rc;
   1.166 +    int eob;
   1.167 +    int x;
   1.168 +    int y;
   1.169 +    int z;
   1.170 +    int sz;
   1.171 +    short *coeff_ptr;
   1.172 +    short *quant_ptr;
   1.173 +    short *quant_shift_ptr;
   1.174 +    short *qcoeff_ptr;
   1.175 +    short *dqcoeff_ptr;
   1.176 +    short *dequant_ptr;
   1.177 +
   1.178 +    coeff_ptr       = b->coeff;
   1.179 +    quant_ptr       = b->quant;
   1.180 +    quant_shift_ptr = b->quant_shift;
   1.181 +    qcoeff_ptr      = d->qcoeff;
   1.182 +    dqcoeff_ptr     = d->dqcoeff;
   1.183 +    dequant_ptr     = d->dequant;
   1.184 +    eob = - 1;
   1.185 +    vpx_memset(qcoeff_ptr, 0, 32);
   1.186 +    vpx_memset(dqcoeff_ptr, 0, 32);
   1.187 +    for (i = 0; i < 16; i++)
   1.188 +    {
   1.189 +        int dq;
   1.190 +        int rounding;
   1.191 +
   1.192 +        /*TODO: These arrays should be stored in zig-zag order.*/
   1.193 +        rc = vp8_default_zig_zag1d[i];
   1.194 +        z = coeff_ptr[rc];
   1.195 +        dq = dequant_ptr[rc];
   1.196 +        rounding = dq >> 1;
   1.197 +        /* Sign of z. */
   1.198 +        sz = -(z < 0);
   1.199 +        x = (z + sz) ^ sz;
   1.200 +        x += rounding;
   1.201 +        if (x >= dq)
   1.202 +        {
   1.203 +            /* Quantize x. */
   1.204 +            y  = ((((x * quant_ptr[rc]) >> 16) + x) * quant_shift_ptr[rc]) >> 16;
   1.205 +            /* Put the sign back. */
   1.206 +            x = (y + sz) ^ sz;
   1.207 +            /* Save the coefficient and its dequantized value. */
   1.208 +            qcoeff_ptr[rc] = x;
   1.209 +            dqcoeff_ptr[rc] = x * dq;
   1.210 +            /* Remember the last non-zero coefficient. */
   1.211 +            if (y)
   1.212 +                eob = i;
   1.213 +        }
   1.214 +    }
   1.215 +
   1.216 +    *d->eob = (char)(eob + 1);
   1.217 +}
   1.218 +
   1.219 +#else
   1.220 +
   1.221 +void vp8_regular_quantize_b_c(BLOCK *b, BLOCKD *d)
   1.222 +{
   1.223 +    int i, rc, eob;
   1.224 +    int zbin;
   1.225 +    int x, y, z, sz;
   1.226 +    short *zbin_boost_ptr = b->zrun_zbin_boost;
   1.227 +    short *coeff_ptr      = b->coeff;
   1.228 +    short *zbin_ptr       = b->zbin;
   1.229 +    short *round_ptr      = b->round;
   1.230 +    short *quant_ptr      = b->quant;
   1.231 +    short *qcoeff_ptr     = d->qcoeff;
   1.232 +    short *dqcoeff_ptr    = d->dqcoeff;
   1.233 +    short *dequant_ptr    = d->dequant;
   1.234 +    short zbin_oq_value   = b->zbin_extra;
   1.235 +
   1.236 +    vpx_memset(qcoeff_ptr, 0, 32);
   1.237 +    vpx_memset(dqcoeff_ptr, 0, 32);
   1.238 +
   1.239 +    eob = -1;
   1.240 +
   1.241 +    for (i = 0; i < 16; i++)
   1.242 +    {
   1.243 +        rc   = vp8_default_zig_zag1d[i];
   1.244 +        z    = coeff_ptr[rc];
   1.245 +
   1.246 +        zbin = zbin_ptr[rc] + *zbin_boost_ptr + zbin_oq_value;
   1.247 +
   1.248 +        zbin_boost_ptr ++;
   1.249 +        sz = (z >> 31);                              /* sign of z */
   1.250 +        x  = (z ^ sz) - sz;                          /* x = abs(z) */
   1.251 +
   1.252 +        if (x >= zbin)
   1.253 +        {
   1.254 +            y  = ((x + round_ptr[rc]) * quant_ptr[rc]) >> 16; /* quantize (x) */
   1.255 +            x  = (y ^ sz) - sz;                      /* get the sign back */
   1.256 +            qcoeff_ptr[rc]  = x;                     /* write to destination */
   1.257 +            dqcoeff_ptr[rc] = x * dequant_ptr[rc];   /* dequantized value */
   1.258 +
   1.259 +            if (y)
   1.260 +            {
   1.261 +                eob = i;                             /* last nonzero coeffs */
   1.262 +                zbin_boost_ptr = &b->zrun_zbin_boost[0]; /* reset zrl */
   1.263 +            }
   1.264 +        }
   1.265 +    }
   1.266 +
   1.267 +    *d->eob = (char)(eob + 1);
   1.268 +}
   1.269 +
   1.270 +#endif
   1.271 +
   1.272 +void vp8_quantize_mby_c(MACROBLOCK *x)
   1.273 +{
   1.274 +    int i;
   1.275 +    int has_2nd_order = (x->e_mbd.mode_info_context->mbmi.mode != B_PRED
   1.276 +        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
   1.277 +
   1.278 +    for (i = 0; i < 16; i++)
   1.279 +        x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
   1.280 +
   1.281 +    if(has_2nd_order)
   1.282 +        x->quantize_b(&x->block[24], &x->e_mbd.block[24]);
   1.283 +}
   1.284 +
   1.285 +void vp8_quantize_mb_c(MACROBLOCK *x)
   1.286 +{
   1.287 +    int i;
   1.288 +    int has_2nd_order=(x->e_mbd.mode_info_context->mbmi.mode != B_PRED
   1.289 +        && x->e_mbd.mode_info_context->mbmi.mode != SPLITMV);
   1.290 +
   1.291 +    for (i = 0; i < 24+has_2nd_order; i++)
   1.292 +        x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
   1.293 +}
   1.294 +
   1.295 +
   1.296 +void vp8_quantize_mbuv_c(MACROBLOCK *x)
   1.297 +{
   1.298 +    int i;
   1.299 +
   1.300 +    for (i = 16; i < 24; i++)
   1.301 +        x->quantize_b(&x->block[i], &x->e_mbd.block[i]);
   1.302 +}
   1.303 +
   1.304 +/* quantize_b_pair function pointer in MACROBLOCK structure is set to one of
   1.305 + * these two C functions if corresponding optimized routine is not available.
   1.306 + * NEON optimized version implements currently the fast quantization for pair
   1.307 + * of blocks. */
   1.308 +void vp8_regular_quantize_b_pair(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
   1.309 +{
   1.310 +    vp8_regular_quantize_b(b1, d1);
   1.311 +    vp8_regular_quantize_b(b2, d2);
   1.312 +}
   1.313 +
   1.314 +void vp8_fast_quantize_b_pair_c(BLOCK *b1, BLOCK *b2, BLOCKD *d1, BLOCKD *d2)
   1.315 +{
   1.316 +    vp8_fast_quantize_b_c(b1, d1);
   1.317 +    vp8_fast_quantize_b_c(b2, d2);
   1.318 +}
   1.319 +
   1.320 +
   1.321 +static const int qrounding_factors[129] =
   1.322 +{
   1.323 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.324 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.325 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.326 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.327 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.328 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.329 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.330 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.331 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.332 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.333 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.334 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.335 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.336 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.337 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.338 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.339 +    48
   1.340 +};
   1.341 +
   1.342 +
   1.343 +static const int qzbin_factors[129] =
   1.344 +{
   1.345 +    84, 84, 84, 84, 84, 84, 84, 84,
   1.346 +    84, 84, 84, 84, 84, 84, 84, 84,
   1.347 +    84, 84, 84, 84, 84, 84, 84, 84,
   1.348 +    84, 84, 84, 84, 84, 84, 84, 84,
   1.349 +    84, 84, 84, 84, 84, 84, 84, 84,
   1.350 +    84, 84, 84, 84, 84, 84, 84, 84,
   1.351 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.352 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.353 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.354 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.355 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.356 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.357 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.358 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.359 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.360 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.361 +    80
   1.362 +};
   1.363 +
   1.364 +
   1.365 +static const int qrounding_factors_y2[129] =
   1.366 +{
   1.367 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.368 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.369 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.370 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.371 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.372 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.373 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.374 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.375 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.376 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.377 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.378 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.379 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.380 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.381 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.382 +    48, 48, 48, 48, 48, 48, 48, 48,
   1.383 +    48
   1.384 +};
   1.385 +
   1.386 +
   1.387 +static const int qzbin_factors_y2[129] =
   1.388 +{
   1.389 +    84, 84, 84, 84, 84, 84, 84, 84,
   1.390 +    84, 84, 84, 84, 84, 84, 84, 84,
   1.391 +    84, 84, 84, 84, 84, 84, 84, 84,
   1.392 +    84, 84, 84, 84, 84, 84, 84, 84,
   1.393 +    84, 84, 84, 84, 84, 84, 84, 84,
   1.394 +    84, 84, 84, 84, 84, 84, 84, 84,
   1.395 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.396 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.397 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.398 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.399 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.400 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.401 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.402 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.403 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.404 +    80, 80, 80, 80, 80, 80, 80, 80,
   1.405 +    80
   1.406 +};
   1.407 +
   1.408 +
   1.409 +#define EXACT_QUANT
   1.410 +#ifdef EXACT_QUANT
   1.411 +static void invert_quant(int improved_quant, short *quant,
   1.412 +                         short *shift, short d)
   1.413 +{
   1.414 +    if(improved_quant)
   1.415 +    {
   1.416 +        unsigned t;
   1.417 +        int l;
   1.418 +        t = d;
   1.419 +        for(l = 0; t > 1; l++)
   1.420 +            t>>=1;
   1.421 +        t = 1 + (1<<(16+l))/d;
   1.422 +        *quant = (short)(t - (1<<16));
   1.423 +        *shift = l;
   1.424 +        /* use multiplication and constant shift by 16 */
   1.425 +        *shift = 1 << (16 - *shift);
   1.426 +    }
   1.427 +    else
   1.428 +    {
   1.429 +        *quant = (1 << 16) / d;
   1.430 +        *shift = 0;
   1.431 +        /* use multiplication and constant shift by 16 */
   1.432 +        *shift = 1 << (16 - *shift);
   1.433 +    }
   1.434 +}
   1.435 +
   1.436 +
   1.437 +void vp8cx_init_quantizer(VP8_COMP *cpi)
   1.438 +{
   1.439 +    int i;
   1.440 +    int quant_val;
   1.441 +    int Q;
   1.442 +
   1.443 +    int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44,
   1.444 +                          44, 44};
   1.445 +
   1.446 +    for (Q = 0; Q < QINDEX_RANGE; Q++)
   1.447 +    {
   1.448 +        /* dc values */
   1.449 +        quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
   1.450 +        cpi->Y1quant_fast[Q][0] = (1 << 16) / quant_val;
   1.451 +        invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 0,
   1.452 +                     cpi->Y1quant_shift[Q] + 0, quant_val);
   1.453 +        cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
   1.454 +        cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
   1.455 +        cpi->common.Y1dequant[Q][0] = quant_val;
   1.456 +        cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;
   1.457 +
   1.458 +        quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
   1.459 +        cpi->Y2quant_fast[Q][0] = (1 << 16) / quant_val;
   1.460 +        invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 0,
   1.461 +                     cpi->Y2quant_shift[Q] + 0, quant_val);
   1.462 +        cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
   1.463 +        cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
   1.464 +        cpi->common.Y2dequant[Q][0] = quant_val;
   1.465 +        cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;
   1.466 +
   1.467 +        quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
   1.468 +        cpi->UVquant_fast[Q][0] = (1 << 16) / quant_val;
   1.469 +        invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 0,
   1.470 +                     cpi->UVquant_shift[Q] + 0, quant_val);
   1.471 +        cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
   1.472 +        cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
   1.473 +        cpi->common.UVdequant[Q][0] = quant_val;
   1.474 +        cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;
   1.475 +
   1.476 +        /* all the ac values = ; */
   1.477 +        quant_val = vp8_ac_yquant(Q);
   1.478 +        cpi->Y1quant_fast[Q][1] = (1 << 16) / quant_val;
   1.479 +        invert_quant(cpi->sf.improved_quant, cpi->Y1quant[Q] + 1,
   1.480 +                     cpi->Y1quant_shift[Q] + 1, quant_val);
   1.481 +        cpi->Y1zbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
   1.482 +        cpi->Y1round[Q][1] = (qrounding_factors[Q] * quant_val) >> 7;
   1.483 +        cpi->common.Y1dequant[Q][1] = quant_val;
   1.484 +        cpi->zrun_zbin_boost_y1[Q][1] = (quant_val * zbin_boost[1]) >> 7;
   1.485 +
   1.486 +        quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
   1.487 +        cpi->Y2quant_fast[Q][1] = (1 << 16) / quant_val;
   1.488 +        invert_quant(cpi->sf.improved_quant, cpi->Y2quant[Q] + 1,
   1.489 +                     cpi->Y2quant_shift[Q] + 1, quant_val);
   1.490 +        cpi->Y2zbin[Q][1] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
   1.491 +        cpi->Y2round[Q][1] = (qrounding_factors_y2[Q] * quant_val) >> 7;
   1.492 +        cpi->common.Y2dequant[Q][1] = quant_val;
   1.493 +        cpi->zrun_zbin_boost_y2[Q][1] = (quant_val * zbin_boost[1]) >> 7;
   1.494 +
   1.495 +        quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
   1.496 +        cpi->UVquant_fast[Q][1] = (1 << 16) / quant_val;
   1.497 +        invert_quant(cpi->sf.improved_quant, cpi->UVquant[Q] + 1,
   1.498 +                     cpi->UVquant_shift[Q] + 1, quant_val);
   1.499 +        cpi->UVzbin[Q][1] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
   1.500 +        cpi->UVround[Q][1] = (qrounding_factors[Q] * quant_val) >> 7;
   1.501 +        cpi->common.UVdequant[Q][1] = quant_val;
   1.502 +        cpi->zrun_zbin_boost_uv[Q][1] = (quant_val * zbin_boost[1]) >> 7;
   1.503 +
   1.504 +        for (i = 2; i < 16; i++)
   1.505 +        {
   1.506 +            cpi->Y1quant_fast[Q][i] = cpi->Y1quant_fast[Q][1];
   1.507 +            cpi->Y1quant[Q][i] = cpi->Y1quant[Q][1];
   1.508 +            cpi->Y1quant_shift[Q][i] = cpi->Y1quant_shift[Q][1];
   1.509 +            cpi->Y1zbin[Q][i] = cpi->Y1zbin[Q][1];
   1.510 +            cpi->Y1round[Q][i] = cpi->Y1round[Q][1];
   1.511 +            cpi->zrun_zbin_boost_y1[Q][i] = (cpi->common.Y1dequant[Q][1] *
   1.512 +                                             zbin_boost[i]) >> 7;
   1.513 +
   1.514 +            cpi->Y2quant_fast[Q][i] = cpi->Y2quant_fast[Q][1];
   1.515 +            cpi->Y2quant[Q][i] = cpi->Y2quant[Q][1];
   1.516 +            cpi->Y2quant_shift[Q][i] = cpi->Y2quant_shift[Q][1];
   1.517 +            cpi->Y2zbin[Q][i] = cpi->Y2zbin[Q][1];
   1.518 +            cpi->Y2round[Q][i] = cpi->Y2round[Q][1];
   1.519 +            cpi->zrun_zbin_boost_y2[Q][i] = (cpi->common.Y2dequant[Q][1] *
   1.520 +                                             zbin_boost[i]) >> 7;
   1.521 +
   1.522 +            cpi->UVquant_fast[Q][i] = cpi->UVquant_fast[Q][1];
   1.523 +            cpi->UVquant[Q][i] = cpi->UVquant[Q][1];
   1.524 +            cpi->UVquant_shift[Q][i] = cpi->UVquant_shift[Q][1];
   1.525 +            cpi->UVzbin[Q][i] = cpi->UVzbin[Q][1];
   1.526 +            cpi->UVround[Q][i] = cpi->UVround[Q][1];
   1.527 +            cpi->zrun_zbin_boost_uv[Q][i] = (cpi->common.UVdequant[Q][1] *
   1.528 +                                             zbin_boost[i]) >> 7;
   1.529 +        }
   1.530 +    }
   1.531 +}
   1.532 +#else
   1.533 +void vp8cx_init_quantizer(VP8_COMP *cpi)
   1.534 +{
   1.535 +    int i;
   1.536 +    int quant_val;
   1.537 +    int Q;
   1.538 +
   1.539 +    int zbin_boost[16] = {0, 0, 8, 10, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44, 44, 44};
   1.540 +
   1.541 +    for (Q = 0; Q < QINDEX_RANGE; Q++)
   1.542 +    {
   1.543 +        /* dc values */
   1.544 +        quant_val = vp8_dc_quant(Q, cpi->common.y1dc_delta_q);
   1.545 +        cpi->Y1quant[Q][0] = (1 << 16) / quant_val;
   1.546 +        cpi->Y1zbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
   1.547 +        cpi->Y1round[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
   1.548 +        cpi->common.Y1dequant[Q][0] = quant_val;
   1.549 +        cpi->zrun_zbin_boost_y1[Q][0] = (quant_val * zbin_boost[0]) >> 7;
   1.550 +
   1.551 +        quant_val = vp8_dc2quant(Q, cpi->common.y2dc_delta_q);
   1.552 +        cpi->Y2quant[Q][0] = (1 << 16) / quant_val;
   1.553 +        cpi->Y2zbin[Q][0] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
   1.554 +        cpi->Y2round[Q][0] = (qrounding_factors_y2[Q] * quant_val) >> 7;
   1.555 +        cpi->common.Y2dequant[Q][0] = quant_val;
   1.556 +        cpi->zrun_zbin_boost_y2[Q][0] = (quant_val * zbin_boost[0]) >> 7;
   1.557 +
   1.558 +        quant_val = vp8_dc_uv_quant(Q, cpi->common.uvdc_delta_q);
   1.559 +        cpi->UVquant[Q][0] = (1 << 16) / quant_val;
   1.560 +        cpi->UVzbin[Q][0] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;;
   1.561 +        cpi->UVround[Q][0] = (qrounding_factors[Q] * quant_val) >> 7;
   1.562 +        cpi->common.UVdequant[Q][0] = quant_val;
   1.563 +        cpi->zrun_zbin_boost_uv[Q][0] = (quant_val * zbin_boost[0]) >> 7;
   1.564 +
   1.565 +        /* all the ac values = ; */
   1.566 +        for (i = 1; i < 16; i++)
   1.567 +        {
   1.568 +            int rc = vp8_default_zig_zag1d[i];
   1.569 +
   1.570 +            quant_val = vp8_ac_yquant(Q);
   1.571 +            cpi->Y1quant[Q][rc] = (1 << 16) / quant_val;
   1.572 +            cpi->Y1zbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
   1.573 +            cpi->Y1round[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
   1.574 +            cpi->common.Y1dequant[Q][rc] = quant_val;
   1.575 +            cpi->zrun_zbin_boost_y1[Q][i] = (quant_val * zbin_boost[i]) >> 7;
   1.576 +
   1.577 +            quant_val = vp8_ac2quant(Q, cpi->common.y2ac_delta_q);
   1.578 +            cpi->Y2quant[Q][rc] = (1 << 16) / quant_val;
   1.579 +            cpi->Y2zbin[Q][rc] = ((qzbin_factors_y2[Q] * quant_val) + 64) >> 7;
   1.580 +            cpi->Y2round[Q][rc] = (qrounding_factors_y2[Q] * quant_val) >> 7;
   1.581 +            cpi->common.Y2dequant[Q][rc] = quant_val;
   1.582 +            cpi->zrun_zbin_boost_y2[Q][i] = (quant_val * zbin_boost[i]) >> 7;
   1.583 +
   1.584 +            quant_val = vp8_ac_uv_quant(Q, cpi->common.uvac_delta_q);
   1.585 +            cpi->UVquant[Q][rc] = (1 << 16) / quant_val;
   1.586 +            cpi->UVzbin[Q][rc] = ((qzbin_factors[Q] * quant_val) + 64) >> 7;
   1.587 +            cpi->UVround[Q][rc] = (qrounding_factors[Q] * quant_val) >> 7;
   1.588 +            cpi->common.UVdequant[Q][rc] = quant_val;
   1.589 +            cpi->zrun_zbin_boost_uv[Q][i] = (quant_val * zbin_boost[i]) >> 7;
   1.590 +        }
   1.591 +    }
   1.592 +}
   1.593 +#endif
   1.594 +
   1.595 +#define ZBIN_EXTRA_Y \
   1.596 +    (( cpi->common.Y1dequant[QIndex][1] *  \
   1.597 +    ( x->zbin_over_quant +  \
   1.598 +      x->zbin_mode_boost +  \
   1.599 +      x->act_zbin_adj ) ) >> 7)
   1.600 +
   1.601 +#define ZBIN_EXTRA_UV \
   1.602 +    (( cpi->common.UVdequant[QIndex][1] *  \
   1.603 +    ( x->zbin_over_quant +  \
   1.604 +      x->zbin_mode_boost +  \
   1.605 +      x->act_zbin_adj ) ) >> 7)
   1.606 +
   1.607 +#define ZBIN_EXTRA_Y2 \
   1.608 +    (( cpi->common.Y2dequant[QIndex][1] *  \
   1.609 +    ( (x->zbin_over_quant / 2) +  \
   1.610 +       x->zbin_mode_boost +  \
   1.611 +       x->act_zbin_adj ) ) >> 7)
   1.612 +
   1.613 +void vp8cx_mb_init_quantizer(VP8_COMP *cpi, MACROBLOCK *x, int ok_to_skip)
   1.614 +{
   1.615 +    int i;
   1.616 +    int QIndex;
   1.617 +    MACROBLOCKD *xd = &x->e_mbd;
   1.618 +    int zbin_extra;
   1.619 +
   1.620 +    /* Select the baseline MB Q index. */
   1.621 +    if (xd->segmentation_enabled)
   1.622 +    {
   1.623 +        /* Abs Value */
   1.624 +        if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA)
   1.625 +            QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
   1.626 +        /* Delta Value */
   1.627 +        else
   1.628 +        {
   1.629 +            QIndex = cpi->common.base_qindex + xd->segment_feature_data[MB_LVL_ALT_Q][xd->mode_info_context->mbmi.segment_id];
   1.630 +            /* Clamp to valid range */
   1.631 +            QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ) : 0;
   1.632 +        }
   1.633 +    }
   1.634 +    else
   1.635 +        QIndex = cpi->common.base_qindex;
   1.636 +
   1.637 +    /* This initialization should be called at least once. Use ok_to_skip to
   1.638 +     * decide if it is ok to skip.
   1.639 +     * Before encoding a frame, this function is always called with ok_to_skip
   1.640 +     * =0, which means no skiping of calculations. The "last" values are
   1.641 +     * initialized at that time.
   1.642 +     */
   1.643 +    if (!ok_to_skip || QIndex != x->q_index)
   1.644 +    {
   1.645 +
   1.646 +        xd->dequant_y1_dc[0] = 1;
   1.647 +        xd->dequant_y1[0] = cpi->common.Y1dequant[QIndex][0];
   1.648 +        xd->dequant_y2[0] = cpi->common.Y2dequant[QIndex][0];
   1.649 +        xd->dequant_uv[0] = cpi->common.UVdequant[QIndex][0];
   1.650 +
   1.651 +        for (i = 1; i < 16; i++)
   1.652 +        {
   1.653 +            xd->dequant_y1_dc[i] =
   1.654 +            xd->dequant_y1[i] = cpi->common.Y1dequant[QIndex][1];
   1.655 +            xd->dequant_y2[i] = cpi->common.Y2dequant[QIndex][1];
   1.656 +            xd->dequant_uv[i] = cpi->common.UVdequant[QIndex][1];
   1.657 +        }
   1.658 +#if 1
   1.659 +        /*TODO:  Remove dequant from BLOCKD.  This is a temporary solution until
   1.660 +         * the quantizer code uses a passed in pointer to the dequant constants.
   1.661 +         * This will also require modifications to the x86 and neon assembly.
   1.662 +         * */
   1.663 +        for (i = 0; i < 16; i++)
   1.664 +            x->e_mbd.block[i].dequant = xd->dequant_y1;
   1.665 +        for (i = 16; i < 24; i++)
   1.666 +            x->e_mbd.block[i].dequant = xd->dequant_uv;
   1.667 +        x->e_mbd.block[24].dequant = xd->dequant_y2;
   1.668 +#endif
   1.669 +
   1.670 +        /* Y */
   1.671 +        zbin_extra = ZBIN_EXTRA_Y;
   1.672 +
   1.673 +        for (i = 0; i < 16; i++)
   1.674 +        {
   1.675 +            x->block[i].quant = cpi->Y1quant[QIndex];
   1.676 +            x->block[i].quant_fast = cpi->Y1quant_fast[QIndex];
   1.677 +            x->block[i].quant_shift = cpi->Y1quant_shift[QIndex];
   1.678 +            x->block[i].zbin = cpi->Y1zbin[QIndex];
   1.679 +            x->block[i].round = cpi->Y1round[QIndex];
   1.680 +            x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_y1[QIndex];
   1.681 +            x->block[i].zbin_extra = (short)zbin_extra;
   1.682 +        }
   1.683 +
   1.684 +        /* UV */
   1.685 +        zbin_extra = ZBIN_EXTRA_UV;
   1.686 +
   1.687 +        for (i = 16; i < 24; i++)
   1.688 +        {
   1.689 +            x->block[i].quant = cpi->UVquant[QIndex];
   1.690 +            x->block[i].quant_fast = cpi->UVquant_fast[QIndex];
   1.691 +            x->block[i].quant_shift = cpi->UVquant_shift[QIndex];
   1.692 +            x->block[i].zbin = cpi->UVzbin[QIndex];
   1.693 +            x->block[i].round = cpi->UVround[QIndex];
   1.694 +            x->block[i].zrun_zbin_boost = cpi->zrun_zbin_boost_uv[QIndex];
   1.695 +            x->block[i].zbin_extra = (short)zbin_extra;
   1.696 +        }
   1.697 +
   1.698 +        /* Y2 */
   1.699 +        zbin_extra = ZBIN_EXTRA_Y2;
   1.700 +
   1.701 +        x->block[24].quant_fast = cpi->Y2quant_fast[QIndex];
   1.702 +        x->block[24].quant = cpi->Y2quant[QIndex];
   1.703 +        x->block[24].quant_shift = cpi->Y2quant_shift[QIndex];
   1.704 +        x->block[24].zbin = cpi->Y2zbin[QIndex];
   1.705 +        x->block[24].round = cpi->Y2round[QIndex];
   1.706 +        x->block[24].zrun_zbin_boost = cpi->zrun_zbin_boost_y2[QIndex];
   1.707 +        x->block[24].zbin_extra = (short)zbin_extra;
   1.708 +
   1.709 +        /* save this macroblock QIndex for vp8_update_zbin_extra() */
   1.710 +        x->q_index = QIndex;
   1.711 +
   1.712 +        x->last_zbin_over_quant = x->zbin_over_quant;
   1.713 +        x->last_zbin_mode_boost = x->zbin_mode_boost;
   1.714 +        x->last_act_zbin_adj = x->act_zbin_adj;
   1.715 +
   1.716 +
   1.717 +
   1.718 +    }
   1.719 +    else if(x->last_zbin_over_quant != x->zbin_over_quant
   1.720 +            || x->last_zbin_mode_boost != x->zbin_mode_boost
   1.721 +            || x->last_act_zbin_adj != x->act_zbin_adj)
   1.722 +    {
   1.723 +        /* Y */
   1.724 +        zbin_extra = ZBIN_EXTRA_Y;
   1.725 +
   1.726 +        for (i = 0; i < 16; i++)
   1.727 +            x->block[i].zbin_extra = (short)zbin_extra;
   1.728 +
   1.729 +        /* UV */
   1.730 +        zbin_extra = ZBIN_EXTRA_UV;
   1.731 +
   1.732 +        for (i = 16; i < 24; i++)
   1.733 +            x->block[i].zbin_extra = (short)zbin_extra;
   1.734 +
   1.735 +        /* Y2 */
   1.736 +        zbin_extra = ZBIN_EXTRA_Y2;
   1.737 +        x->block[24].zbin_extra = (short)zbin_extra;
   1.738 +
   1.739 +        x->last_zbin_over_quant = x->zbin_over_quant;
   1.740 +        x->last_zbin_mode_boost = x->zbin_mode_boost;
   1.741 +        x->last_act_zbin_adj = x->act_zbin_adj;
   1.742 +    }
   1.743 +}
   1.744 +
   1.745 +void vp8_update_zbin_extra(VP8_COMP *cpi, MACROBLOCK *x)
   1.746 +{
   1.747 +    int i;
   1.748 +    int QIndex = x->q_index;
   1.749 +    int zbin_extra;
   1.750 +
   1.751 +    /* Y */
   1.752 +    zbin_extra = ZBIN_EXTRA_Y;
   1.753 +
   1.754 +    for (i = 0; i < 16; i++)
   1.755 +        x->block[i].zbin_extra = (short)zbin_extra;
   1.756 +
   1.757 +    /* UV */
   1.758 +    zbin_extra = ZBIN_EXTRA_UV;
   1.759 +
   1.760 +    for (i = 16; i < 24; i++)
   1.761 +        x->block[i].zbin_extra = (short)zbin_extra;
   1.762 +
   1.763 +    /* Y2 */
   1.764 +    zbin_extra = ZBIN_EXTRA_Y2;
   1.765 +    x->block[24].zbin_extra = (short)zbin_extra;
   1.766 +}
   1.767 +#undef ZBIN_EXTRA_Y
   1.768 +#undef ZBIN_EXTRA_UV
   1.769 +#undef ZBIN_EXTRA_Y2
   1.770 +
   1.771 +void vp8cx_frame_init_quantizer(VP8_COMP *cpi)
   1.772 +{
   1.773 +    /* Clear Zbin mode boost for default case */
   1.774 +    cpi->mb.zbin_mode_boost = 0;
   1.775 +
   1.776 +    /* MB level quantizer setup */
   1.777 +    vp8cx_mb_init_quantizer(cpi, &cpi->mb, 0);
   1.778 +}
   1.779 +
   1.780 +
   1.781 +void vp8_set_quantizer(struct VP8_COMP *cpi, int Q)
   1.782 +{
   1.783 +    VP8_COMMON *cm = &cpi->common;
   1.784 +    MACROBLOCKD *mbd = &cpi->mb.e_mbd;
   1.785 +    int update = 0;
   1.786 +    int new_delta_q;
   1.787 +    cm->base_qindex = Q;
   1.788 +
   1.789 +    /* if any of the delta_q values are changing update flag has to be set */
   1.790 +    /* currently only y2dc_delta_q may change */
   1.791 +
   1.792 +    cm->y1dc_delta_q = 0;
   1.793 +    cm->y2ac_delta_q = 0;
   1.794 +    cm->uvdc_delta_q = 0;
   1.795 +    cm->uvac_delta_q = 0;
   1.796 +
   1.797 +    if (Q < 4)
   1.798 +    {
   1.799 +        new_delta_q = 4-Q;
   1.800 +    }
   1.801 +    else
   1.802 +        new_delta_q = 0;
   1.803 +
   1.804 +    update |= cm->y2dc_delta_q != new_delta_q;
   1.805 +    cm->y2dc_delta_q = new_delta_q;
   1.806 +
   1.807 +
   1.808 +    /* Set Segment specific quatizers */
   1.809 +    mbd->segment_feature_data[MB_LVL_ALT_Q][0] = cpi->segment_feature_data[MB_LVL_ALT_Q][0];
   1.810 +    mbd->segment_feature_data[MB_LVL_ALT_Q][1] = cpi->segment_feature_data[MB_LVL_ALT_Q][1];
   1.811 +    mbd->segment_feature_data[MB_LVL_ALT_Q][2] = cpi->segment_feature_data[MB_LVL_ALT_Q][2];
   1.812 +    mbd->segment_feature_data[MB_LVL_ALT_Q][3] = cpi->segment_feature_data[MB_LVL_ALT_Q][3];
   1.813 +
   1.814 +    /* quantizer has to be reinitialized for any delta_q changes */
   1.815 +    if(update)
   1.816 +        vp8cx_init_quantizer(cpi);
   1.817 +
   1.818 +}

mercurial