media/libopus/celt/quant_bands.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libopus/celt/quant_bands.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,556 @@
     1.4 +/* Copyright (c) 2007-2008 CSIRO
     1.5 +   Copyright (c) 2007-2009 Xiph.Org Foundation
     1.6 +   Written by Jean-Marc Valin */
     1.7 +/*
     1.8 +   Redistribution and use in source and binary forms, with or without
     1.9 +   modification, are permitted provided that the following conditions
    1.10 +   are met:
    1.11 +
    1.12 +   - Redistributions of source code must retain the above copyright
    1.13 +   notice, this list of conditions and the following disclaimer.
    1.14 +
    1.15 +   - Redistributions in binary form must reproduce the above copyright
    1.16 +   notice, this list of conditions and the following disclaimer in the
    1.17 +   documentation and/or other materials provided with the distribution.
    1.18 +
    1.19 +   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.20 +   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.21 +   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.22 +   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    1.23 +   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.24 +   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.25 +   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.26 +   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.27 +   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.28 +   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.29 +   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.30 +*/
    1.31 +
    1.32 +#ifdef HAVE_CONFIG_H
    1.33 +#include "config.h"
    1.34 +#endif
    1.35 +
    1.36 +#include "quant_bands.h"
    1.37 +#include "laplace.h"
    1.38 +#include <math.h>
    1.39 +#include "os_support.h"
    1.40 +#include "arch.h"
    1.41 +#include "mathops.h"
    1.42 +#include "stack_alloc.h"
    1.43 +#include "rate.h"
    1.44 +
    1.45 +#ifdef FIXED_POINT
    1.46 +/* Mean energy in each band quantized in Q4 */
    1.47 +const signed char eMeans[25] = {
    1.48 +      103,100, 92, 85, 81,
    1.49 +       77, 72, 70, 78, 75,
    1.50 +       73, 71, 78, 74, 69,
    1.51 +       72, 70, 74, 76, 71,
    1.52 +       60, 60, 60, 60, 60
    1.53 +};
    1.54 +#else
    1.55 +/* Mean energy in each band quantized in Q4 and converted back to float */
    1.56 +const opus_val16 eMeans[25] = {
    1.57 +      6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f,
    1.58 +      4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f,
    1.59 +      4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f,
    1.60 +      4.500000f, 4.375000f, 4.625000f, 4.750000f, 4.437500f,
    1.61 +      3.750000f, 3.750000f, 3.750000f, 3.750000f, 3.750000f
    1.62 +};
    1.63 +#endif
    1.64 +/* prediction coefficients: 0.9, 0.8, 0.65, 0.5 */
    1.65 +#ifdef FIXED_POINT
    1.66 +static const opus_val16 pred_coef[4] = {29440, 26112, 21248, 16384};
    1.67 +static const opus_val16 beta_coef[4] = {30147, 22282, 12124, 6554};
    1.68 +static const opus_val16 beta_intra = 4915;
    1.69 +#else
    1.70 +static const opus_val16 pred_coef[4] = {29440/32768., 26112/32768., 21248/32768., 16384/32768.};
    1.71 +static const opus_val16 beta_coef[4] = {30147/32768., 22282/32768., 12124/32768., 6554/32768.};
    1.72 +static const opus_val16 beta_intra = 4915/32768.;
    1.73 +#endif
    1.74 +
    1.75 +/*Parameters of the Laplace-like probability models used for the coarse energy.
    1.76 +  There is one pair of parameters for each frame size, prediction type
    1.77 +   (inter/intra), and band number.
    1.78 +  The first number of each pair is the probability of 0, and the second is the
    1.79 +   decay rate, both in Q8 precision.*/
    1.80 +static const unsigned char e_prob_model[4][2][42] = {
    1.81 +   /*120 sample frames.*/
    1.82 +   {
    1.83 +      /*Inter*/
    1.84 +      {
    1.85 +          72, 127,  65, 129,  66, 128,  65, 128,  64, 128,  62, 128,  64, 128,
    1.86 +          64, 128,  92,  78,  92,  79,  92,  78,  90,  79, 116,  41, 115,  40,
    1.87 +         114,  40, 132,  26, 132,  26, 145,  17, 161,  12, 176,  10, 177,  11
    1.88 +      },
    1.89 +      /*Intra*/
    1.90 +      {
    1.91 +          24, 179,  48, 138,  54, 135,  54, 132,  53, 134,  56, 133,  55, 132,
    1.92 +          55, 132,  61, 114,  70,  96,  74,  88,  75,  88,  87,  74,  89,  66,
    1.93 +          91,  67, 100,  59, 108,  50, 120,  40, 122,  37,  97,  43,  78,  50
    1.94 +      }
    1.95 +   },
    1.96 +   /*240 sample frames.*/
    1.97 +   {
    1.98 +      /*Inter*/
    1.99 +      {
   1.100 +          83,  78,  84,  81,  88,  75,  86,  74,  87,  71,  90,  73,  93,  74,
   1.101 +          93,  74, 109,  40, 114,  36, 117,  34, 117,  34, 143,  17, 145,  18,
   1.102 +         146,  19, 162,  12, 165,  10, 178,   7, 189,   6, 190,   8, 177,   9
   1.103 +      },
   1.104 +      /*Intra*/
   1.105 +      {
   1.106 +          23, 178,  54, 115,  63, 102,  66,  98,  69,  99,  74,  89,  71,  91,
   1.107 +          73,  91,  78,  89,  86,  80,  92,  66,  93,  64, 102,  59, 103,  60,
   1.108 +         104,  60, 117,  52, 123,  44, 138,  35, 133,  31,  97,  38,  77,  45
   1.109 +      }
   1.110 +   },
   1.111 +   /*480 sample frames.*/
   1.112 +   {
   1.113 +      /*Inter*/
   1.114 +      {
   1.115 +          61,  90,  93,  60, 105,  42, 107,  41, 110,  45, 116,  38, 113,  38,
   1.116 +         112,  38, 124,  26, 132,  27, 136,  19, 140,  20, 155,  14, 159,  16,
   1.117 +         158,  18, 170,  13, 177,  10, 187,   8, 192,   6, 175,   9, 159,  10
   1.118 +      },
   1.119 +      /*Intra*/
   1.120 +      {
   1.121 +          21, 178,  59, 110,  71,  86,  75,  85,  84,  83,  91,  66,  88,  73,
   1.122 +          87,  72,  92,  75,  98,  72, 105,  58, 107,  54, 115,  52, 114,  55,
   1.123 +         112,  56, 129,  51, 132,  40, 150,  33, 140,  29,  98,  35,  77,  42
   1.124 +      }
   1.125 +   },
   1.126 +   /*960 sample frames.*/
   1.127 +   {
   1.128 +      /*Inter*/
   1.129 +      {
   1.130 +          42, 121,  96,  66, 108,  43, 111,  40, 117,  44, 123,  32, 120,  36,
   1.131 +         119,  33, 127,  33, 134,  34, 139,  21, 147,  23, 152,  20, 158,  25,
   1.132 +         154,  26, 166,  21, 173,  16, 184,  13, 184,  10, 150,  13, 139,  15
   1.133 +      },
   1.134 +      /*Intra*/
   1.135 +      {
   1.136 +          22, 178,  63, 114,  74,  82,  84,  83,  92,  82, 103,  62,  96,  72,
   1.137 +          96,  67, 101,  73, 107,  72, 113,  55, 118,  52, 125,  52, 118,  52,
   1.138 +         117,  55, 135,  49, 137,  39, 157,  32, 145,  29,  97,  33,  77,  40
   1.139 +      }
   1.140 +   }
   1.141 +};
   1.142 +
   1.143 +static const unsigned char small_energy_icdf[3]={2,1,0};
   1.144 +
   1.145 +static opus_val32 loss_distortion(const opus_val16 *eBands, opus_val16 *oldEBands, int start, int end, int len, int C)
   1.146 +{
   1.147 +   int c, i;
   1.148 +   opus_val32 dist = 0;
   1.149 +   c=0; do {
   1.150 +      for (i=start;i<end;i++)
   1.151 +      {
   1.152 +         opus_val16 d = SUB16(SHR16(eBands[i+c*len], 3), SHR16(oldEBands[i+c*len], 3));
   1.153 +         dist = MAC16_16(dist, d,d);
   1.154 +      }
   1.155 +   } while (++c<C);
   1.156 +   return MIN32(200,SHR32(dist,2*DB_SHIFT-6));
   1.157 +}
   1.158 +
   1.159 +static int quant_coarse_energy_impl(const CELTMode *m, int start, int end,
   1.160 +      const opus_val16 *eBands, opus_val16 *oldEBands,
   1.161 +      opus_int32 budget, opus_int32 tell,
   1.162 +      const unsigned char *prob_model, opus_val16 *error, ec_enc *enc,
   1.163 +      int C, int LM, int intra, opus_val16 max_decay, int lfe)
   1.164 +{
   1.165 +   int i, c;
   1.166 +   int badness = 0;
   1.167 +   opus_val32 prev[2] = {0,0};
   1.168 +   opus_val16 coef;
   1.169 +   opus_val16 beta;
   1.170 +
   1.171 +   if (tell+3 <= budget)
   1.172 +      ec_enc_bit_logp(enc, intra, 3);
   1.173 +   if (intra)
   1.174 +   {
   1.175 +      coef = 0;
   1.176 +      beta = beta_intra;
   1.177 +   } else {
   1.178 +      beta = beta_coef[LM];
   1.179 +      coef = pred_coef[LM];
   1.180 +   }
   1.181 +
   1.182 +   /* Encode at a fixed coarse resolution */
   1.183 +   for (i=start;i<end;i++)
   1.184 +   {
   1.185 +      c=0;
   1.186 +      do {
   1.187 +         int bits_left;
   1.188 +         int qi, qi0;
   1.189 +         opus_val32 q;
   1.190 +         opus_val16 x;
   1.191 +         opus_val32 f, tmp;
   1.192 +         opus_val16 oldE;
   1.193 +         opus_val16 decay_bound;
   1.194 +         x = eBands[i+c*m->nbEBands];
   1.195 +         oldE = MAX16(-QCONST16(9.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]);
   1.196 +#ifdef FIXED_POINT
   1.197 +         f = SHL32(EXTEND32(x),7) - PSHR32(MULT16_16(coef,oldE), 8) - prev[c];
   1.198 +         /* Rounding to nearest integer here is really important! */
   1.199 +         qi = (f+QCONST32(.5f,DB_SHIFT+7))>>(DB_SHIFT+7);
   1.200 +         decay_bound = EXTRACT16(MAX32(-QCONST16(28.f,DB_SHIFT),
   1.201 +               SUB32((opus_val32)oldEBands[i+c*m->nbEBands],max_decay)));
   1.202 +#else
   1.203 +         f = x-coef*oldE-prev[c];
   1.204 +         /* Rounding to nearest integer here is really important! */
   1.205 +         qi = (int)floor(.5f+f);
   1.206 +         decay_bound = MAX16(-QCONST16(28.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]) - max_decay;
   1.207 +#endif
   1.208 +         /* Prevent the energy from going down too quickly (e.g. for bands
   1.209 +            that have just one bin) */
   1.210 +         if (qi < 0 && x < decay_bound)
   1.211 +         {
   1.212 +            qi += (int)SHR16(SUB16(decay_bound,x), DB_SHIFT);
   1.213 +            if (qi > 0)
   1.214 +               qi = 0;
   1.215 +         }
   1.216 +         qi0 = qi;
   1.217 +         /* If we don't have enough bits to encode all the energy, just assume
   1.218 +             something safe. */
   1.219 +         tell = ec_tell(enc);
   1.220 +         bits_left = budget-tell-3*C*(end-i);
   1.221 +         if (i!=start && bits_left < 30)
   1.222 +         {
   1.223 +            if (bits_left < 24)
   1.224 +               qi = IMIN(1, qi);
   1.225 +            if (bits_left < 16)
   1.226 +               qi = IMAX(-1, qi);
   1.227 +         }
   1.228 +         if (lfe && i>=2)
   1.229 +            qi = IMIN(qi, 0);
   1.230 +         if (budget-tell >= 15)
   1.231 +         {
   1.232 +            int pi;
   1.233 +            pi = 2*IMIN(i,20);
   1.234 +            ec_laplace_encode(enc, &qi,
   1.235 +                  prob_model[pi]<<7, prob_model[pi+1]<<6);
   1.236 +         }
   1.237 +         else if(budget-tell >= 2)
   1.238 +         {
   1.239 +            qi = IMAX(-1, IMIN(qi, 1));
   1.240 +            ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2);
   1.241 +         }
   1.242 +         else if(budget-tell >= 1)
   1.243 +         {
   1.244 +            qi = IMIN(0, qi);
   1.245 +            ec_enc_bit_logp(enc, -qi, 1);
   1.246 +         }
   1.247 +         else
   1.248 +            qi = -1;
   1.249 +         error[i+c*m->nbEBands] = PSHR32(f,7) - SHL16(qi,DB_SHIFT);
   1.250 +         badness += abs(qi0-qi);
   1.251 +         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
   1.252 +
   1.253 +         tmp = PSHR32(MULT16_16(coef,oldE),8) + prev[c] + SHL32(q,7);
   1.254 +#ifdef FIXED_POINT
   1.255 +         tmp = MAX32(-QCONST32(28.f, DB_SHIFT+7), tmp);
   1.256 +#endif
   1.257 +         oldEBands[i+c*m->nbEBands] = PSHR32(tmp, 7);
   1.258 +         prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8));
   1.259 +      } while (++c < C);
   1.260 +   }
   1.261 +   return lfe ? 0 : badness;
   1.262 +}
   1.263 +
   1.264 +void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
   1.265 +      const opus_val16 *eBands, opus_val16 *oldEBands, opus_uint32 budget,
   1.266 +      opus_val16 *error, ec_enc *enc, int C, int LM, int nbAvailableBytes,
   1.267 +      int force_intra, opus_val32 *delayedIntra, int two_pass, int loss_rate, int lfe)
   1.268 +{
   1.269 +   int intra;
   1.270 +   opus_val16 max_decay;
   1.271 +   VARDECL(opus_val16, oldEBands_intra);
   1.272 +   VARDECL(opus_val16, error_intra);
   1.273 +   ec_enc enc_start_state;
   1.274 +   opus_uint32 tell;
   1.275 +   int badness1=0;
   1.276 +   opus_int32 intra_bias;
   1.277 +   opus_val32 new_distortion;
   1.278 +   SAVE_STACK;
   1.279 +
   1.280 +   intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C);
   1.281 +   intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512));
   1.282 +   new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C);
   1.283 +
   1.284 +   tell = ec_tell(enc);
   1.285 +   if (tell+3 > budget)
   1.286 +      two_pass = intra = 0;
   1.287 +
   1.288 +   max_decay = QCONST16(16.f,DB_SHIFT);
   1.289 +   if (end-start>10)
   1.290 +   {
   1.291 +#ifdef FIXED_POINT
   1.292 +      max_decay = MIN32(max_decay, SHL32(EXTEND32(nbAvailableBytes),DB_SHIFT-3));
   1.293 +#else
   1.294 +      max_decay = MIN32(max_decay, .125f*nbAvailableBytes);
   1.295 +#endif
   1.296 +   }
   1.297 +   if (lfe)
   1.298 +      max_decay=3;
   1.299 +   enc_start_state = *enc;
   1.300 +
   1.301 +   ALLOC(oldEBands_intra, C*m->nbEBands, opus_val16);
   1.302 +   ALLOC(error_intra, C*m->nbEBands, opus_val16);
   1.303 +   OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
   1.304 +
   1.305 +   if (two_pass || intra)
   1.306 +   {
   1.307 +      badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
   1.308 +            tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lfe);
   1.309 +   }
   1.310 +
   1.311 +   if (!intra)
   1.312 +   {
   1.313 +      unsigned char *intra_buf;
   1.314 +      ec_enc enc_intra_state;
   1.315 +      opus_int32 tell_intra;
   1.316 +      opus_uint32 nstart_bytes;
   1.317 +      opus_uint32 nintra_bytes;
   1.318 +      opus_uint32 save_bytes;
   1.319 +      int badness2;
   1.320 +      VARDECL(unsigned char, intra_bits);
   1.321 +
   1.322 +      tell_intra = ec_tell_frac(enc);
   1.323 +
   1.324 +      enc_intra_state = *enc;
   1.325 +
   1.326 +      nstart_bytes = ec_range_bytes(&enc_start_state);
   1.327 +      nintra_bytes = ec_range_bytes(&enc_intra_state);
   1.328 +      intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes;
   1.329 +      save_bytes = nintra_bytes-nstart_bytes;
   1.330 +      if (save_bytes == 0)
   1.331 +         save_bytes = ALLOC_NONE;
   1.332 +      ALLOC(intra_bits, save_bytes, unsigned char);
   1.333 +      /* Copy bits from intra bit-stream */
   1.334 +      OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes);
   1.335 +
   1.336 +      *enc = enc_start_state;
   1.337 +
   1.338 +      badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
   1.339 +            tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe);
   1.340 +
   1.341 +      if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra)))
   1.342 +      {
   1.343 +         *enc = enc_intra_state;
   1.344 +         /* Copy intra bits to bit-stream */
   1.345 +         OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes);
   1.346 +         OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
   1.347 +         OPUS_COPY(error, error_intra, C*m->nbEBands);
   1.348 +         intra = 1;
   1.349 +      }
   1.350 +   } else {
   1.351 +      OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
   1.352 +      OPUS_COPY(error, error_intra, C*m->nbEBands);
   1.353 +   }
   1.354 +
   1.355 +   if (intra)
   1.356 +      *delayedIntra = new_distortion;
   1.357 +   else
   1.358 +      *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra),
   1.359 +            new_distortion);
   1.360 +
   1.361 +   RESTORE_STACK;
   1.362 +}
   1.363 +
   1.364 +void quant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, ec_enc *enc, int C)
   1.365 +{
   1.366 +   int i, c;
   1.367 +
   1.368 +   /* Encode finer resolution */
   1.369 +   for (i=start;i<end;i++)
   1.370 +   {
   1.371 +      opus_int16 frac = 1<<fine_quant[i];
   1.372 +      if (fine_quant[i] <= 0)
   1.373 +         continue;
   1.374 +      c=0;
   1.375 +      do {
   1.376 +         int q2;
   1.377 +         opus_val16 offset;
   1.378 +#ifdef FIXED_POINT
   1.379 +         /* Has to be without rounding */
   1.380 +         q2 = (error[i+c*m->nbEBands]+QCONST16(.5f,DB_SHIFT))>>(DB_SHIFT-fine_quant[i]);
   1.381 +#else
   1.382 +         q2 = (int)floor((error[i+c*m->nbEBands]+.5f)*frac);
   1.383 +#endif
   1.384 +         if (q2 > frac-1)
   1.385 +            q2 = frac-1;
   1.386 +         if (q2<0)
   1.387 +            q2 = 0;
   1.388 +         ec_enc_bits(enc, q2, fine_quant[i]);
   1.389 +#ifdef FIXED_POINT
   1.390 +         offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5f,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT));
   1.391 +#else
   1.392 +         offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f;
   1.393 +#endif
   1.394 +         oldEBands[i+c*m->nbEBands] += offset;
   1.395 +         error[i+c*m->nbEBands] -= offset;
   1.396 +         /*printf ("%f ", error[i] - offset);*/
   1.397 +      } while (++c < C);
   1.398 +   }
   1.399 +}
   1.400 +
   1.401 +void quant_energy_finalise(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, int *fine_priority, int bits_left, ec_enc *enc, int C)
   1.402 +{
   1.403 +   int i, prio, c;
   1.404 +
   1.405 +   /* Use up the remaining bits */
   1.406 +   for (prio=0;prio<2;prio++)
   1.407 +   {
   1.408 +      for (i=start;i<end && bits_left>=C ;i++)
   1.409 +      {
   1.410 +         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
   1.411 +            continue;
   1.412 +         c=0;
   1.413 +         do {
   1.414 +            int q2;
   1.415 +            opus_val16 offset;
   1.416 +            q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
   1.417 +            ec_enc_bits(enc, q2, 1);
   1.418 +#ifdef FIXED_POINT
   1.419 +            offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5f,DB_SHIFT),fine_quant[i]+1);
   1.420 +#else
   1.421 +            offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
   1.422 +#endif
   1.423 +            oldEBands[i+c*m->nbEBands] += offset;
   1.424 +            bits_left--;
   1.425 +         } while (++c < C);
   1.426 +      }
   1.427 +   }
   1.428 +}
   1.429 +
   1.430 +void unquant_coarse_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int intra, ec_dec *dec, int C, int LM)
   1.431 +{
   1.432 +   const unsigned char *prob_model = e_prob_model[LM][intra];
   1.433 +   int i, c;
   1.434 +   opus_val32 prev[2] = {0, 0};
   1.435 +   opus_val16 coef;
   1.436 +   opus_val16 beta;
   1.437 +   opus_int32 budget;
   1.438 +   opus_int32 tell;
   1.439 +
   1.440 +   if (intra)
   1.441 +   {
   1.442 +      coef = 0;
   1.443 +      beta = beta_intra;
   1.444 +   } else {
   1.445 +      beta = beta_coef[LM];
   1.446 +      coef = pred_coef[LM];
   1.447 +   }
   1.448 +
   1.449 +   budget = dec->storage*8;
   1.450 +
   1.451 +   /* Decode at a fixed coarse resolution */
   1.452 +   for (i=start;i<end;i++)
   1.453 +   {
   1.454 +      c=0;
   1.455 +      do {
   1.456 +         int qi;
   1.457 +         opus_val32 q;
   1.458 +         opus_val32 tmp;
   1.459 +         /* It would be better to express this invariant as a
   1.460 +            test on C at function entry, but that isn't enough
   1.461 +            to make the static analyzer happy. */
   1.462 +         celt_assert(c<2);
   1.463 +         tell = ec_tell(dec);
   1.464 +         if(budget-tell>=15)
   1.465 +         {
   1.466 +            int pi;
   1.467 +            pi = 2*IMIN(i,20);
   1.468 +            qi = ec_laplace_decode(dec,
   1.469 +                  prob_model[pi]<<7, prob_model[pi+1]<<6);
   1.470 +         }
   1.471 +         else if(budget-tell>=2)
   1.472 +         {
   1.473 +            qi = ec_dec_icdf(dec, small_energy_icdf, 2);
   1.474 +            qi = (qi>>1)^-(qi&1);
   1.475 +         }
   1.476 +         else if(budget-tell>=1)
   1.477 +         {
   1.478 +            qi = -ec_dec_bit_logp(dec, 1);
   1.479 +         }
   1.480 +         else
   1.481 +            qi = -1;
   1.482 +         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
   1.483 +
   1.484 +         oldEBands[i+c*m->nbEBands] = MAX16(-QCONST16(9.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]);
   1.485 +         tmp = PSHR32(MULT16_16(coef,oldEBands[i+c*m->nbEBands]),8) + prev[c] + SHL32(q,7);
   1.486 +#ifdef FIXED_POINT
   1.487 +         tmp = MAX32(-QCONST32(28.f, DB_SHIFT+7), tmp);
   1.488 +#endif
   1.489 +         oldEBands[i+c*m->nbEBands] = PSHR32(tmp, 7);
   1.490 +         prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8));
   1.491 +      } while (++c < C);
   1.492 +   }
   1.493 +}
   1.494 +
   1.495 +void unquant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int *fine_quant, ec_dec *dec, int C)
   1.496 +{
   1.497 +   int i, c;
   1.498 +   /* Decode finer resolution */
   1.499 +   for (i=start;i<end;i++)
   1.500 +   {
   1.501 +      if (fine_quant[i] <= 0)
   1.502 +         continue;
   1.503 +      c=0;
   1.504 +      do {
   1.505 +         int q2;
   1.506 +         opus_val16 offset;
   1.507 +         q2 = ec_dec_bits(dec, fine_quant[i]);
   1.508 +#ifdef FIXED_POINT
   1.509 +         offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5f,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT));
   1.510 +#else
   1.511 +         offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f;
   1.512 +#endif
   1.513 +         oldEBands[i+c*m->nbEBands] += offset;
   1.514 +      } while (++c < C);
   1.515 +   }
   1.516 +}
   1.517 +
   1.518 +void unquant_energy_finalise(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int *fine_quant,  int *fine_priority, int bits_left, ec_dec *dec, int C)
   1.519 +{
   1.520 +   int i, prio, c;
   1.521 +
   1.522 +   /* Use up the remaining bits */
   1.523 +   for (prio=0;prio<2;prio++)
   1.524 +   {
   1.525 +      for (i=start;i<end && bits_left>=C ;i++)
   1.526 +      {
   1.527 +         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
   1.528 +            continue;
   1.529 +         c=0;
   1.530 +         do {
   1.531 +            int q2;
   1.532 +            opus_val16 offset;
   1.533 +            q2 = ec_dec_bits(dec, 1);
   1.534 +#ifdef FIXED_POINT
   1.535 +            offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5f,DB_SHIFT),fine_quant[i]+1);
   1.536 +#else
   1.537 +            offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
   1.538 +#endif
   1.539 +            oldEBands[i+c*m->nbEBands] += offset;
   1.540 +            bits_left--;
   1.541 +         } while (++c < C);
   1.542 +      }
   1.543 +   }
   1.544 +}
   1.545 +
   1.546 +void amp2Log2(const CELTMode *m, int effEnd, int end,
   1.547 +      celt_ener *bandE, opus_val16 *bandLogE, int C)
   1.548 +{
   1.549 +   int c, i;
   1.550 +   c=0;
   1.551 +   do {
   1.552 +      for (i=0;i<effEnd;i++)
   1.553 +         bandLogE[i+c*m->nbEBands] =
   1.554 +               celt_log2(SHL32(bandE[i+c*m->nbEBands],2))
   1.555 +               - SHL16((opus_val16)eMeans[i],6);
   1.556 +      for (i=effEnd;i<end;i++)
   1.557 +         bandLogE[c*m->nbEBands+i] = -QCONST16(14.f,DB_SHIFT);
   1.558 +   } while (++c < C);
   1.559 +}

mercurial