annotate src/opus-1.3/celt/bands.c @ 154:4664ac0c1032

Add Opus sources and macOS builds
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 23 Jan 2019 13:48:08 +0000
parents
children
rev   line source
cannam@154 1 /* Copyright (c) 2007-2008 CSIRO
cannam@154 2 Copyright (c) 2007-2009 Xiph.Org Foundation
cannam@154 3 Copyright (c) 2008-2009 Gregory Maxwell
cannam@154 4 Written by Jean-Marc Valin and Gregory Maxwell */
cannam@154 5 /*
cannam@154 6 Redistribution and use in source and binary forms, with or without
cannam@154 7 modification, are permitted provided that the following conditions
cannam@154 8 are met:
cannam@154 9
cannam@154 10 - Redistributions of source code must retain the above copyright
cannam@154 11 notice, this list of conditions and the following disclaimer.
cannam@154 12
cannam@154 13 - Redistributions in binary form must reproduce the above copyright
cannam@154 14 notice, this list of conditions and the following disclaimer in the
cannam@154 15 documentation and/or other materials provided with the distribution.
cannam@154 16
cannam@154 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
cannam@154 18 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
cannam@154 19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
cannam@154 20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
cannam@154 21 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
cannam@154 22 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
cannam@154 23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
cannam@154 24 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
cannam@154 25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
cannam@154 26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
cannam@154 27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
cannam@154 28 */
cannam@154 29
cannam@154 30 #ifdef HAVE_CONFIG_H
cannam@154 31 #include "config.h"
cannam@154 32 #endif
cannam@154 33
cannam@154 34 #include <math.h>
cannam@154 35 #include "bands.h"
cannam@154 36 #include "modes.h"
cannam@154 37 #include "vq.h"
cannam@154 38 #include "cwrs.h"
cannam@154 39 #include "stack_alloc.h"
cannam@154 40 #include "os_support.h"
cannam@154 41 #include "mathops.h"
cannam@154 42 #include "rate.h"
cannam@154 43 #include "quant_bands.h"
cannam@154 44 #include "pitch.h"
cannam@154 45
cannam@154 46 int hysteresis_decision(opus_val16 val, const opus_val16 *thresholds, const opus_val16 *hysteresis, int N, int prev)
cannam@154 47 {
cannam@154 48 int i;
cannam@154 49 for (i=0;i<N;i++)
cannam@154 50 {
cannam@154 51 if (val < thresholds[i])
cannam@154 52 break;
cannam@154 53 }
cannam@154 54 if (i>prev && val < thresholds[prev]+hysteresis[prev])
cannam@154 55 i=prev;
cannam@154 56 if (i<prev && val > thresholds[prev-1]-hysteresis[prev-1])
cannam@154 57 i=prev;
cannam@154 58 return i;
cannam@154 59 }
cannam@154 60
cannam@154 61 opus_uint32 celt_lcg_rand(opus_uint32 seed)
cannam@154 62 {
cannam@154 63 return 1664525 * seed + 1013904223;
cannam@154 64 }
cannam@154 65
cannam@154 66 /* This is a cos() approximation designed to be bit-exact on any platform. Bit exactness
cannam@154 67 with this approximation is important because it has an impact on the bit allocation */
cannam@154 68 opus_int16 bitexact_cos(opus_int16 x)
cannam@154 69 {
cannam@154 70 opus_int32 tmp;
cannam@154 71 opus_int16 x2;
cannam@154 72 tmp = (4096+((opus_int32)(x)*(x)))>>13;
cannam@154 73 celt_sig_assert(tmp<=32767);
cannam@154 74 x2 = tmp;
cannam@154 75 x2 = (32767-x2) + FRAC_MUL16(x2, (-7651 + FRAC_MUL16(x2, (8277 + FRAC_MUL16(-626, x2)))));
cannam@154 76 celt_sig_assert(x2<=32766);
cannam@154 77 return 1+x2;
cannam@154 78 }
cannam@154 79
cannam@154 80 int bitexact_log2tan(int isin,int icos)
cannam@154 81 {
cannam@154 82 int lc;
cannam@154 83 int ls;
cannam@154 84 lc=EC_ILOG(icos);
cannam@154 85 ls=EC_ILOG(isin);
cannam@154 86 icos<<=15-lc;
cannam@154 87 isin<<=15-ls;
cannam@154 88 return (ls-lc)*(1<<11)
cannam@154 89 +FRAC_MUL16(isin, FRAC_MUL16(isin, -2597) + 7932)
cannam@154 90 -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932);
cannam@154 91 }
cannam@154 92
cannam@154 93 #ifdef FIXED_POINT
cannam@154 94 /* Compute the amplitude (sqrt energy) in each of the bands */
cannam@154 95 void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int LM, int arch)
cannam@154 96 {
cannam@154 97 int i, c, N;
cannam@154 98 const opus_int16 *eBands = m->eBands;
cannam@154 99 (void)arch;
cannam@154 100 N = m->shortMdctSize<<LM;
cannam@154 101 c=0; do {
cannam@154 102 for (i=0;i<end;i++)
cannam@154 103 {
cannam@154 104 int j;
cannam@154 105 opus_val32 maxval=0;
cannam@154 106 opus_val32 sum = 0;
cannam@154 107
cannam@154 108 maxval = celt_maxabs32(&X[c*N+(eBands[i]<<LM)], (eBands[i+1]-eBands[i])<<LM);
cannam@154 109 if (maxval > 0)
cannam@154 110 {
cannam@154 111 int shift = celt_ilog2(maxval) - 14 + (((m->logN[i]>>BITRES)+LM+1)>>1);
cannam@154 112 j=eBands[i]<<LM;
cannam@154 113 if (shift>0)
cannam@154 114 {
cannam@154 115 do {
cannam@154 116 sum = MAC16_16(sum, EXTRACT16(SHR32(X[j+c*N],shift)),
cannam@154 117 EXTRACT16(SHR32(X[j+c*N],shift)));
cannam@154 118 } while (++j<eBands[i+1]<<LM);
cannam@154 119 } else {
cannam@154 120 do {
cannam@154 121 sum = MAC16_16(sum, EXTRACT16(SHL32(X[j+c*N],-shift)),
cannam@154 122 EXTRACT16(SHL32(X[j+c*N],-shift)));
cannam@154 123 } while (++j<eBands[i+1]<<LM);
cannam@154 124 }
cannam@154 125 /* We're adding one here to ensure the normalized band isn't larger than unity norm */
cannam@154 126 bandE[i+c*m->nbEBands] = EPSILON+VSHR32(EXTEND32(celt_sqrt(sum)),-shift);
cannam@154 127 } else {
cannam@154 128 bandE[i+c*m->nbEBands] = EPSILON;
cannam@154 129 }
cannam@154 130 /*printf ("%f ", bandE[i+c*m->nbEBands]);*/
cannam@154 131 }
cannam@154 132 } while (++c<C);
cannam@154 133 /*printf ("\n");*/
cannam@154 134 }
cannam@154 135
cannam@154 136 /* Normalise each band such that the energy is one. */
cannam@154 137 void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, celt_norm * OPUS_RESTRICT X, const celt_ener *bandE, int end, int C, int M)
cannam@154 138 {
cannam@154 139 int i, c, N;
cannam@154 140 const opus_int16 *eBands = m->eBands;
cannam@154 141 N = M*m->shortMdctSize;
cannam@154 142 c=0; do {
cannam@154 143 i=0; do {
cannam@154 144 opus_val16 g;
cannam@154 145 int j,shift;
cannam@154 146 opus_val16 E;
cannam@154 147 shift = celt_zlog2(bandE[i+c*m->nbEBands])-13;
cannam@154 148 E = VSHR32(bandE[i+c*m->nbEBands], shift);
cannam@154 149 g = EXTRACT16(celt_rcp(SHL32(E,3)));
cannam@154 150 j=M*eBands[i]; do {
cannam@154 151 X[j+c*N] = MULT16_16_Q15(VSHR32(freq[j+c*N],shift-1),g);
cannam@154 152 } while (++j<M*eBands[i+1]);
cannam@154 153 } while (++i<end);
cannam@154 154 } while (++c<C);
cannam@154 155 }
cannam@154 156
cannam@154 157 #else /* FIXED_POINT */
cannam@154 158 /* Compute the amplitude (sqrt energy) in each of the bands */
cannam@154 159 void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int LM, int arch)
cannam@154 160 {
cannam@154 161 int i, c, N;
cannam@154 162 const opus_int16 *eBands = m->eBands;
cannam@154 163 N = m->shortMdctSize<<LM;
cannam@154 164 c=0; do {
cannam@154 165 for (i=0;i<end;i++)
cannam@154 166 {
cannam@154 167 opus_val32 sum;
cannam@154 168 sum = 1e-27f + celt_inner_prod(&X[c*N+(eBands[i]<<LM)], &X[c*N+(eBands[i]<<LM)], (eBands[i+1]-eBands[i])<<LM, arch);
cannam@154 169 bandE[i+c*m->nbEBands] = celt_sqrt(sum);
cannam@154 170 /*printf ("%f ", bandE[i+c*m->nbEBands]);*/
cannam@154 171 }
cannam@154 172 } while (++c<C);
cannam@154 173 /*printf ("\n");*/
cannam@154 174 }
cannam@154 175
cannam@154 176 /* Normalise each band such that the energy is one. */
cannam@154 177 void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, celt_norm * OPUS_RESTRICT X, const celt_ener *bandE, int end, int C, int M)
cannam@154 178 {
cannam@154 179 int i, c, N;
cannam@154 180 const opus_int16 *eBands = m->eBands;
cannam@154 181 N = M*m->shortMdctSize;
cannam@154 182 c=0; do {
cannam@154 183 for (i=0;i<end;i++)
cannam@154 184 {
cannam@154 185 int j;
cannam@154 186 opus_val16 g = 1.f/(1e-27f+bandE[i+c*m->nbEBands]);
cannam@154 187 for (j=M*eBands[i];j<M*eBands[i+1];j++)
cannam@154 188 X[j+c*N] = freq[j+c*N]*g;
cannam@154 189 }
cannam@154 190 } while (++c<C);
cannam@154 191 }
cannam@154 192
cannam@154 193 #endif /* FIXED_POINT */
cannam@154 194
cannam@154 195 /* De-normalise the energy to produce the synthesis from the unit-energy bands */
cannam@154 196 void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X,
cannam@154 197 celt_sig * OPUS_RESTRICT freq, const opus_val16 *bandLogE, int start,
cannam@154 198 int end, int M, int downsample, int silence)
cannam@154 199 {
cannam@154 200 int i, N;
cannam@154 201 int bound;
cannam@154 202 celt_sig * OPUS_RESTRICT f;
cannam@154 203 const celt_norm * OPUS_RESTRICT x;
cannam@154 204 const opus_int16 *eBands = m->eBands;
cannam@154 205 N = M*m->shortMdctSize;
cannam@154 206 bound = M*eBands[end];
cannam@154 207 if (downsample!=1)
cannam@154 208 bound = IMIN(bound, N/downsample);
cannam@154 209 if (silence)
cannam@154 210 {
cannam@154 211 bound = 0;
cannam@154 212 start = end = 0;
cannam@154 213 }
cannam@154 214 f = freq;
cannam@154 215 x = X+M*eBands[start];
cannam@154 216 for (i=0;i<M*eBands[start];i++)
cannam@154 217 *f++ = 0;
cannam@154 218 for (i=start;i<end;i++)
cannam@154 219 {
cannam@154 220 int j, band_end;
cannam@154 221 opus_val16 g;
cannam@154 222 opus_val16 lg;
cannam@154 223 #ifdef FIXED_POINT
cannam@154 224 int shift;
cannam@154 225 #endif
cannam@154 226 j=M*eBands[i];
cannam@154 227 band_end = M*eBands[i+1];
cannam@154 228 lg = SATURATE16(ADD32(bandLogE[i], SHL32((opus_val32)eMeans[i],6)));
cannam@154 229 #ifndef FIXED_POINT
cannam@154 230 g = celt_exp2(MIN32(32.f, lg));
cannam@154 231 #else
cannam@154 232 /* Handle the integer part of the log energy */
cannam@154 233 shift = 16-(lg>>DB_SHIFT);
cannam@154 234 if (shift>31)
cannam@154 235 {
cannam@154 236 shift=0;
cannam@154 237 g=0;
cannam@154 238 } else {
cannam@154 239 /* Handle the fractional part. */
cannam@154 240 g = celt_exp2_frac(lg&((1<<DB_SHIFT)-1));
cannam@154 241 }
cannam@154 242 /* Handle extreme gains with negative shift. */
cannam@154 243 if (shift<0)
cannam@154 244 {
cannam@154 245 /* For shift <= -2 and g > 16384 we'd be likely to overflow, so we're
cannam@154 246 capping the gain here, which is equivalent to a cap of 18 on lg.
cannam@154 247 This shouldn't trigger unless the bitstream is already corrupted. */
cannam@154 248 if (shift <= -2)
cannam@154 249 {
cannam@154 250 g = 16384;
cannam@154 251 shift = -2;
cannam@154 252 }
cannam@154 253 do {
cannam@154 254 *f++ = SHL32(MULT16_16(*x++, g), -shift);
cannam@154 255 } while (++j<band_end);
cannam@154 256 } else
cannam@154 257 #endif
cannam@154 258 /* Be careful of the fixed-point "else" just above when changing this code */
cannam@154 259 do {
cannam@154 260 *f++ = SHR32(MULT16_16(*x++, g), shift);
cannam@154 261 } while (++j<band_end);
cannam@154 262 }
cannam@154 263 celt_assert(start <= end);
cannam@154 264 OPUS_CLEAR(&freq[bound], N-bound);
cannam@154 265 }
cannam@154 266
cannam@154 267 /* This prevents energy collapse for transients with multiple short MDCTs */
cannam@154 268 void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_masks, int LM, int C, int size,
cannam@154 269 int start, int end, const opus_val16 *logE, const opus_val16 *prev1logE,
cannam@154 270 const opus_val16 *prev2logE, const int *pulses, opus_uint32 seed, int arch)
cannam@154 271 {
cannam@154 272 int c, i, j, k;
cannam@154 273 for (i=start;i<end;i++)
cannam@154 274 {
cannam@154 275 int N0;
cannam@154 276 opus_val16 thresh, sqrt_1;
cannam@154 277 int depth;
cannam@154 278 #ifdef FIXED_POINT
cannam@154 279 int shift;
cannam@154 280 opus_val32 thresh32;
cannam@154 281 #endif
cannam@154 282
cannam@154 283 N0 = m->eBands[i+1]-m->eBands[i];
cannam@154 284 /* depth in 1/8 bits */
cannam@154 285 celt_sig_assert(pulses[i]>=0);
cannam@154 286 depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i]))>>LM;
cannam@154 287
cannam@154 288 #ifdef FIXED_POINT
cannam@154 289 thresh32 = SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1);
cannam@154 290 thresh = MULT16_32_Q15(QCONST16(0.5f, 15), MIN32(32767,thresh32));
cannam@154 291 {
cannam@154 292 opus_val32 t;
cannam@154 293 t = N0<<LM;
cannam@154 294 shift = celt_ilog2(t)>>1;
cannam@154 295 t = SHL32(t, (7-shift)<<1);
cannam@154 296 sqrt_1 = celt_rsqrt_norm(t);
cannam@154 297 }
cannam@154 298 #else
cannam@154 299 thresh = .5f*celt_exp2(-.125f*depth);
cannam@154 300 sqrt_1 = celt_rsqrt(N0<<LM);
cannam@154 301 #endif
cannam@154 302
cannam@154 303 c=0; do
cannam@154 304 {
cannam@154 305 celt_norm *X;
cannam@154 306 opus_val16 prev1;
cannam@154 307 opus_val16 prev2;
cannam@154 308 opus_val32 Ediff;
cannam@154 309 opus_val16 r;
cannam@154 310 int renormalize=0;
cannam@154 311 prev1 = prev1logE[c*m->nbEBands+i];
cannam@154 312 prev2 = prev2logE[c*m->nbEBands+i];
cannam@154 313 if (C==1)
cannam@154 314 {
cannam@154 315 prev1 = MAX16(prev1,prev1logE[m->nbEBands+i]);
cannam@154 316 prev2 = MAX16(prev2,prev2logE[m->nbEBands+i]);
cannam@154 317 }
cannam@154 318 Ediff = EXTEND32(logE[c*m->nbEBands+i])-EXTEND32(MIN16(prev1,prev2));
cannam@154 319 Ediff = MAX32(0, Ediff);
cannam@154 320
cannam@154 321 #ifdef FIXED_POINT
cannam@154 322 if (Ediff < 16384)
cannam@154 323 {
cannam@154 324 opus_val32 r32 = SHR32(celt_exp2(-EXTRACT16(Ediff)),1);
cannam@154 325 r = 2*MIN16(16383,r32);
cannam@154 326 } else {
cannam@154 327 r = 0;
cannam@154 328 }
cannam@154 329 if (LM==3)
cannam@154 330 r = MULT16_16_Q14(23170, MIN32(23169, r));
cannam@154 331 r = SHR16(MIN16(thresh, r),1);
cannam@154 332 r = SHR32(MULT16_16_Q15(sqrt_1, r),shift);
cannam@154 333 #else
cannam@154 334 /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because
cannam@154 335 short blocks don't have the same energy as long */
cannam@154 336 r = 2.f*celt_exp2(-Ediff);
cannam@154 337 if (LM==3)
cannam@154 338 r *= 1.41421356f;
cannam@154 339 r = MIN16(thresh, r);
cannam@154 340 r = r*sqrt_1;
cannam@154 341 #endif
cannam@154 342 X = X_+c*size+(m->eBands[i]<<LM);
cannam@154 343 for (k=0;k<1<<LM;k++)
cannam@154 344 {
cannam@154 345 /* Detect collapse */
cannam@154 346 if (!(collapse_masks[i*C+c]&1<<k))
cannam@154 347 {
cannam@154 348 /* Fill with noise */
cannam@154 349 for (j=0;j<N0;j++)
cannam@154 350 {
cannam@154 351 seed = celt_lcg_rand(seed);
cannam@154 352 X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
cannam@154 353 }
cannam@154 354 renormalize = 1;
cannam@154 355 }
cannam@154 356 }
cannam@154 357 /* We just added some energy, so we need to renormalise */
cannam@154 358 if (renormalize)
cannam@154 359 renormalise_vector(X, N0<<LM, Q15ONE, arch);
cannam@154 360 } while (++c<C);
cannam@154 361 }
cannam@154 362 }
cannam@154 363
cannam@154 364 /* Compute the weights to use for optimizing normalized distortion across
cannam@154 365 channels. We use the amplitude to weight square distortion, which means
cannam@154 366 that we use the square root of the value we would have been using if we
cannam@154 367 wanted to minimize the MSE in the non-normalized domain. This roughly
cannam@154 368 corresponds to some quick-and-dirty perceptual experiments I ran to
cannam@154 369 measure inter-aural masking (there doesn't seem to be any published data
cannam@154 370 on the topic). */
cannam@154 371 static void compute_channel_weights(celt_ener Ex, celt_ener Ey, opus_val16 w[2])
cannam@154 372 {
cannam@154 373 celt_ener minE;
cannam@154 374 #if FIXED_POINT
cannam@154 375 int shift;
cannam@154 376 #endif
cannam@154 377 minE = MIN32(Ex, Ey);
cannam@154 378 /* Adjustment to make the weights a bit more conservative. */
cannam@154 379 Ex = ADD32(Ex, minE/3);
cannam@154 380 Ey = ADD32(Ey, minE/3);
cannam@154 381 #if FIXED_POINT
cannam@154 382 shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
cannam@154 383 #endif
cannam@154 384 w[0] = VSHR32(Ex, shift);
cannam@154 385 w[1] = VSHR32(Ey, shift);
cannam@154 386 }
cannam@154 387
cannam@154 388 static void intensity_stereo(const CELTMode *m, celt_norm * OPUS_RESTRICT X, const celt_norm * OPUS_RESTRICT Y, const celt_ener *bandE, int bandID, int N)
cannam@154 389 {
cannam@154 390 int i = bandID;
cannam@154 391 int j;
cannam@154 392 opus_val16 a1, a2;
cannam@154 393 opus_val16 left, right;
cannam@154 394 opus_val16 norm;
cannam@154 395 #ifdef FIXED_POINT
cannam@154 396 int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
cannam@154 397 #endif
cannam@154 398 left = VSHR32(bandE[i],shift);
cannam@154 399 right = VSHR32(bandE[i+m->nbEBands],shift);
cannam@154 400 norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
cannam@154 401 a1 = DIV32_16(SHL32(EXTEND32(left),14),norm);
cannam@154 402 a2 = DIV32_16(SHL32(EXTEND32(right),14),norm);
cannam@154 403 for (j=0;j<N;j++)
cannam@154 404 {
cannam@154 405 celt_norm r, l;
cannam@154 406 l = X[j];
cannam@154 407 r = Y[j];
cannam@154 408 X[j] = EXTRACT16(SHR32(MAC16_16(MULT16_16(a1, l), a2, r), 14));
cannam@154 409 /* Side is not encoded, no need to calculate */
cannam@154 410 }
cannam@154 411 }
cannam@154 412
cannam@154 413 static void stereo_split(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, int N)
cannam@154 414 {
cannam@154 415 int j;
cannam@154 416 for (j=0;j<N;j++)
cannam@154 417 {
cannam@154 418 opus_val32 r, l;
cannam@154 419 l = MULT16_16(QCONST16(.70710678f, 15), X[j]);
cannam@154 420 r = MULT16_16(QCONST16(.70710678f, 15), Y[j]);
cannam@154 421 X[j] = EXTRACT16(SHR32(ADD32(l, r), 15));
cannam@154 422 Y[j] = EXTRACT16(SHR32(SUB32(r, l), 15));
cannam@154 423 }
cannam@154 424 }
cannam@154 425
cannam@154 426 static void stereo_merge(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, opus_val16 mid, int N, int arch)
cannam@154 427 {
cannam@154 428 int j;
cannam@154 429 opus_val32 xp=0, side=0;
cannam@154 430 opus_val32 El, Er;
cannam@154 431 opus_val16 mid2;
cannam@154 432 #ifdef FIXED_POINT
cannam@154 433 int kl, kr;
cannam@154 434 #endif
cannam@154 435 opus_val32 t, lgain, rgain;
cannam@154 436
cannam@154 437 /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
cannam@154 438 dual_inner_prod(Y, X, Y, N, &xp, &side, arch);
cannam@154 439 /* Compensating for the mid normalization */
cannam@154 440 xp = MULT16_32_Q15(mid, xp);
cannam@154 441 /* mid and side are in Q15, not Q14 like X and Y */
cannam@154 442 mid2 = SHR16(mid, 1);
cannam@154 443 El = MULT16_16(mid2, mid2) + side - 2*xp;
cannam@154 444 Er = MULT16_16(mid2, mid2) + side + 2*xp;
cannam@154 445 if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
cannam@154 446 {
cannam@154 447 OPUS_COPY(Y, X, N);
cannam@154 448 return;
cannam@154 449 }
cannam@154 450
cannam@154 451 #ifdef FIXED_POINT
cannam@154 452 kl = celt_ilog2(El)>>1;
cannam@154 453 kr = celt_ilog2(Er)>>1;
cannam@154 454 #endif
cannam@154 455 t = VSHR32(El, (kl-7)<<1);
cannam@154 456 lgain = celt_rsqrt_norm(t);
cannam@154 457 t = VSHR32(Er, (kr-7)<<1);
cannam@154 458 rgain = celt_rsqrt_norm(t);
cannam@154 459
cannam@154 460 #ifdef FIXED_POINT
cannam@154 461 if (kl < 7)
cannam@154 462 kl = 7;
cannam@154 463 if (kr < 7)
cannam@154 464 kr = 7;
cannam@154 465 #endif
cannam@154 466
cannam@154 467 for (j=0;j<N;j++)
cannam@154 468 {
cannam@154 469 celt_norm r, l;
cannam@154 470 /* Apply mid scaling (side is already scaled) */
cannam@154 471 l = MULT16_16_P15(mid, X[j]);
cannam@154 472 r = Y[j];
cannam@154 473 X[j] = EXTRACT16(PSHR32(MULT16_16(lgain, SUB16(l,r)), kl+1));
cannam@154 474 Y[j] = EXTRACT16(PSHR32(MULT16_16(rgain, ADD16(l,r)), kr+1));
cannam@154 475 }
cannam@154 476 }
cannam@154 477
cannam@154 478 /* Decide whether we should spread the pulses in the current frame */
cannam@154 479 int spreading_decision(const CELTMode *m, const celt_norm *X, int *average,
cannam@154 480 int last_decision, int *hf_average, int *tapset_decision, int update_hf,
cannam@154 481 int end, int C, int M, const int *spread_weight)
cannam@154 482 {
cannam@154 483 int i, c, N0;
cannam@154 484 int sum = 0, nbBands=0;
cannam@154 485 const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
cannam@154 486 int decision;
cannam@154 487 int hf_sum=0;
cannam@154 488
cannam@154 489 celt_assert(end>0);
cannam@154 490
cannam@154 491 N0 = M*m->shortMdctSize;
cannam@154 492
cannam@154 493 if (M*(eBands[end]-eBands[end-1]) <= 8)
cannam@154 494 return SPREAD_NONE;
cannam@154 495 c=0; do {
cannam@154 496 for (i=0;i<end;i++)
cannam@154 497 {
cannam@154 498 int j, N, tmp=0;
cannam@154 499 int tcount[3] = {0,0,0};
cannam@154 500 const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
cannam@154 501 N = M*(eBands[i+1]-eBands[i]);
cannam@154 502 if (N<=8)
cannam@154 503 continue;
cannam@154 504 /* Compute rough CDF of |x[j]| */
cannam@154 505 for (j=0;j<N;j++)
cannam@154 506 {
cannam@154 507 opus_val32 x2N; /* Q13 */
cannam@154 508
cannam@154 509 x2N = MULT16_16(MULT16_16_Q15(x[j], x[j]), N);
cannam@154 510 if (x2N < QCONST16(0.25f,13))
cannam@154 511 tcount[0]++;
cannam@154 512 if (x2N < QCONST16(0.0625f,13))
cannam@154 513 tcount[1]++;
cannam@154 514 if (x2N < QCONST16(0.015625f,13))
cannam@154 515 tcount[2]++;
cannam@154 516 }
cannam@154 517
cannam@154 518 /* Only include four last bands (8 kHz and up) */
cannam@154 519 if (i>m->nbEBands-4)
cannam@154 520 hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
cannam@154 521 tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
cannam@154 522 sum += tmp*spread_weight[i];
cannam@154 523 nbBands+=spread_weight[i];
cannam@154 524 }
cannam@154 525 } while (++c<C);
cannam@154 526
cannam@154 527 if (update_hf)
cannam@154 528 {
cannam@154 529 if (hf_sum)
cannam@154 530 hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
cannam@154 531 *hf_average = (*hf_average+hf_sum)>>1;
cannam@154 532 hf_sum = *hf_average;
cannam@154 533 if (*tapset_decision==2)
cannam@154 534 hf_sum += 4;
cannam@154 535 else if (*tapset_decision==0)
cannam@154 536 hf_sum -= 4;
cannam@154 537 if (hf_sum > 22)
cannam@154 538 *tapset_decision=2;
cannam@154 539 else if (hf_sum > 18)
cannam@154 540 *tapset_decision=1;
cannam@154 541 else
cannam@154 542 *tapset_decision=0;
cannam@154 543 }
cannam@154 544 /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
cannam@154 545 celt_assert(nbBands>0); /* end has to be non-zero */
cannam@154 546 celt_assert(sum>=0);
cannam@154 547 sum = celt_udiv((opus_int32)sum<<8, nbBands);
cannam@154 548 /* Recursive averaging */
cannam@154 549 sum = (sum+*average)>>1;
cannam@154 550 *average = sum;
cannam@154 551 /* Hysteresis */
cannam@154 552 sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
cannam@154 553 if (sum < 80)
cannam@154 554 {
cannam@154 555 decision = SPREAD_AGGRESSIVE;
cannam@154 556 } else if (sum < 256)
cannam@154 557 {
cannam@154 558 decision = SPREAD_NORMAL;
cannam@154 559 } else if (sum < 384)
cannam@154 560 {
cannam@154 561 decision = SPREAD_LIGHT;
cannam@154 562 } else {
cannam@154 563 decision = SPREAD_NONE;
cannam@154 564 }
cannam@154 565 #ifdef FUZZING
cannam@154 566 decision = rand()&0x3;
cannam@154 567 *tapset_decision=rand()%3;
cannam@154 568 #endif
cannam@154 569 return decision;
cannam@154 570 }
cannam@154 571
cannam@154 572 /* Indexing table for converting from natural Hadamard to ordery Hadamard
cannam@154 573 This is essentially a bit-reversed Gray, on top of which we've added
cannam@154 574 an inversion of the order because we want the DC at the end rather than
cannam@154 575 the beginning. The lines are for N=2, 4, 8, 16 */
cannam@154 576 static const int ordery_table[] = {
cannam@154 577 1, 0,
cannam@154 578 3, 0, 2, 1,
cannam@154 579 7, 0, 4, 3, 6, 1, 5, 2,
cannam@154 580 15, 0, 8, 7, 12, 3, 11, 4, 14, 1, 9, 6, 13, 2, 10, 5,
cannam@154 581 };
cannam@154 582
cannam@154 583 static void deinterleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
cannam@154 584 {
cannam@154 585 int i,j;
cannam@154 586 VARDECL(celt_norm, tmp);
cannam@154 587 int N;
cannam@154 588 SAVE_STACK;
cannam@154 589 N = N0*stride;
cannam@154 590 ALLOC(tmp, N, celt_norm);
cannam@154 591 celt_assert(stride>0);
cannam@154 592 if (hadamard)
cannam@154 593 {
cannam@154 594 const int *ordery = ordery_table+stride-2;
cannam@154 595 for (i=0;i<stride;i++)
cannam@154 596 {
cannam@154 597 for (j=0;j<N0;j++)
cannam@154 598 tmp[ordery[i]*N0+j] = X[j*stride+i];
cannam@154 599 }
cannam@154 600 } else {
cannam@154 601 for (i=0;i<stride;i++)
cannam@154 602 for (j=0;j<N0;j++)
cannam@154 603 tmp[i*N0+j] = X[j*stride+i];
cannam@154 604 }
cannam@154 605 OPUS_COPY(X, tmp, N);
cannam@154 606 RESTORE_STACK;
cannam@154 607 }
cannam@154 608
cannam@154 609 static void interleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
cannam@154 610 {
cannam@154 611 int i,j;
cannam@154 612 VARDECL(celt_norm, tmp);
cannam@154 613 int N;
cannam@154 614 SAVE_STACK;
cannam@154 615 N = N0*stride;
cannam@154 616 ALLOC(tmp, N, celt_norm);
cannam@154 617 if (hadamard)
cannam@154 618 {
cannam@154 619 const int *ordery = ordery_table+stride-2;
cannam@154 620 for (i=0;i<stride;i++)
cannam@154 621 for (j=0;j<N0;j++)
cannam@154 622 tmp[j*stride+i] = X[ordery[i]*N0+j];
cannam@154 623 } else {
cannam@154 624 for (i=0;i<stride;i++)
cannam@154 625 for (j=0;j<N0;j++)
cannam@154 626 tmp[j*stride+i] = X[i*N0+j];
cannam@154 627 }
cannam@154 628 OPUS_COPY(X, tmp, N);
cannam@154 629 RESTORE_STACK;
cannam@154 630 }
cannam@154 631
cannam@154 632 void haar1(celt_norm *X, int N0, int stride)
cannam@154 633 {
cannam@154 634 int i, j;
cannam@154 635 N0 >>= 1;
cannam@154 636 for (i=0;i<stride;i++)
cannam@154 637 for (j=0;j<N0;j++)
cannam@154 638 {
cannam@154 639 opus_val32 tmp1, tmp2;
cannam@154 640 tmp1 = MULT16_16(QCONST16(.70710678f,15), X[stride*2*j+i]);
cannam@154 641 tmp2 = MULT16_16(QCONST16(.70710678f,15), X[stride*(2*j+1)+i]);
cannam@154 642 X[stride*2*j+i] = EXTRACT16(PSHR32(ADD32(tmp1, tmp2), 15));
cannam@154 643 X[stride*(2*j+1)+i] = EXTRACT16(PSHR32(SUB32(tmp1, tmp2), 15));
cannam@154 644 }
cannam@154 645 }
cannam@154 646
cannam@154 647 static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo)
cannam@154 648 {
cannam@154 649 static const opus_int16 exp2_table8[8] =
cannam@154 650 {16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048};
cannam@154 651 int qn, qb;
cannam@154 652 int N2 = 2*N-1;
cannam@154 653 if (stereo && N==2)
cannam@154 654 N2--;
cannam@154 655 /* The upper limit ensures that in a stereo split with itheta==16384, we'll
cannam@154 656 always have enough bits left over to code at least one pulse in the
cannam@154 657 side; otherwise it would collapse, since it doesn't get folded. */
cannam@154 658 qb = celt_sudiv(b+N2*offset, N2);
cannam@154 659 qb = IMIN(b-pulse_cap-(4<<BITRES), qb);
cannam@154 660
cannam@154 661 qb = IMIN(8<<BITRES, qb);
cannam@154 662
cannam@154 663 if (qb<(1<<BITRES>>1)) {
cannam@154 664 qn = 1;
cannam@154 665 } else {
cannam@154 666 qn = exp2_table8[qb&0x7]>>(14-(qb>>BITRES));
cannam@154 667 qn = (qn+1)>>1<<1;
cannam@154 668 }
cannam@154 669 celt_assert(qn <= 256);
cannam@154 670 return qn;
cannam@154 671 }
cannam@154 672
cannam@154 673 struct band_ctx {
cannam@154 674 int encode;
cannam@154 675 int resynth;
cannam@154 676 const CELTMode *m;
cannam@154 677 int i;
cannam@154 678 int intensity;
cannam@154 679 int spread;
cannam@154 680 int tf_change;
cannam@154 681 ec_ctx *ec;
cannam@154 682 opus_int32 remaining_bits;
cannam@154 683 const celt_ener *bandE;
cannam@154 684 opus_uint32 seed;
cannam@154 685 int arch;
cannam@154 686 int theta_round;
cannam@154 687 int disable_inv;
cannam@154 688 int avoid_split_noise;
cannam@154 689 };
cannam@154 690
cannam@154 691 struct split_ctx {
cannam@154 692 int inv;
cannam@154 693 int imid;
cannam@154 694 int iside;
cannam@154 695 int delta;
cannam@154 696 int itheta;
cannam@154 697 int qalloc;
cannam@154 698 };
cannam@154 699
cannam@154 700 static void compute_theta(struct band_ctx *ctx, struct split_ctx *sctx,
cannam@154 701 celt_norm *X, celt_norm *Y, int N, int *b, int B, int B0,
cannam@154 702 int LM,
cannam@154 703 int stereo, int *fill)
cannam@154 704 {
cannam@154 705 int qn;
cannam@154 706 int itheta=0;
cannam@154 707 int delta;
cannam@154 708 int imid, iside;
cannam@154 709 int qalloc;
cannam@154 710 int pulse_cap;
cannam@154 711 int offset;
cannam@154 712 opus_int32 tell;
cannam@154 713 int inv=0;
cannam@154 714 int encode;
cannam@154 715 const CELTMode *m;
cannam@154 716 int i;
cannam@154 717 int intensity;
cannam@154 718 ec_ctx *ec;
cannam@154 719 const celt_ener *bandE;
cannam@154 720
cannam@154 721 encode = ctx->encode;
cannam@154 722 m = ctx->m;
cannam@154 723 i = ctx->i;
cannam@154 724 intensity = ctx->intensity;
cannam@154 725 ec = ctx->ec;
cannam@154 726 bandE = ctx->bandE;
cannam@154 727
cannam@154 728 /* Decide on the resolution to give to the split parameter theta */
cannam@154 729 pulse_cap = m->logN[i]+LM*(1<<BITRES);
cannam@154 730 offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
cannam@154 731 qn = compute_qn(N, *b, offset, pulse_cap, stereo);
cannam@154 732 if (stereo && i>=intensity)
cannam@154 733 qn = 1;
cannam@154 734 if (encode)
cannam@154 735 {
cannam@154 736 /* theta is the atan() of the ratio between the (normalized)
cannam@154 737 side and mid. With just that parameter, we can re-scale both
cannam@154 738 mid and side because we know that 1) they have unit norm and
cannam@154 739 2) they are orthogonal. */
cannam@154 740 itheta = stereo_itheta(X, Y, stereo, N, ctx->arch);
cannam@154 741 }
cannam@154 742 tell = ec_tell_frac(ec);
cannam@154 743 if (qn!=1)
cannam@154 744 {
cannam@154 745 if (encode)
cannam@154 746 {
cannam@154 747 if (!stereo || ctx->theta_round == 0)
cannam@154 748 {
cannam@154 749 itheta = (itheta*(opus_int32)qn+8192)>>14;
cannam@154 750 if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
cannam@154 751 {
cannam@154 752 /* Check if the selected value of theta will cause the bit allocation
cannam@154 753 to inject noise on one side. If so, make sure the energy of that side
cannam@154 754 is zero. */
cannam@154 755 int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
cannam@154 756 imid = bitexact_cos((opus_int16)unquantized);
cannam@154 757 iside = bitexact_cos((opus_int16)(16384-unquantized));
cannam@154 758 delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
cannam@154 759 if (delta > *b)
cannam@154 760 itheta = qn;
cannam@154 761 else if (delta < -*b)
cannam@154 762 itheta = 0;
cannam@154 763 }
cannam@154 764 } else {
cannam@154 765 int down;
cannam@154 766 /* Bias quantization towards itheta=0 and itheta=16384. */
cannam@154 767 int bias = itheta > 8192 ? 32767/qn : -32767/qn;
cannam@154 768 down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
cannam@154 769 if (ctx->theta_round < 0)
cannam@154 770 itheta = down;
cannam@154 771 else
cannam@154 772 itheta = down+1;
cannam@154 773 }
cannam@154 774 }
cannam@154 775 /* Entropy coding of the angle. We use a uniform pdf for the
cannam@154 776 time split, a step for stereo, and a triangular one for the rest. */
cannam@154 777 if (stereo && N>2)
cannam@154 778 {
cannam@154 779 int p0 = 3;
cannam@154 780 int x = itheta;
cannam@154 781 int x0 = qn/2;
cannam@154 782 int ft = p0*(x0+1) + x0;
cannam@154 783 /* Use a probability of p0 up to itheta=8192 and then use 1 after */
cannam@154 784 if (encode)
cannam@154 785 {
cannam@154 786 ec_encode(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft);
cannam@154 787 } else {
cannam@154 788 int fs;
cannam@154 789 fs=ec_decode(ec,ft);
cannam@154 790 if (fs<(x0+1)*p0)
cannam@154 791 x=fs/p0;
cannam@154 792 else
cannam@154 793 x=x0+1+(fs-(x0+1)*p0);
cannam@154 794 ec_dec_update(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft);
cannam@154 795 itheta = x;
cannam@154 796 }
cannam@154 797 } else if (B0>1 || stereo) {
cannam@154 798 /* Uniform pdf */
cannam@154 799 if (encode)
cannam@154 800 ec_enc_uint(ec, itheta, qn+1);
cannam@154 801 else
cannam@154 802 itheta = ec_dec_uint(ec, qn+1);
cannam@154 803 } else {
cannam@154 804 int fs=1, ft;
cannam@154 805 ft = ((qn>>1)+1)*((qn>>1)+1);
cannam@154 806 if (encode)
cannam@154 807 {
cannam@154 808 int fl;
cannam@154 809
cannam@154 810 fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
cannam@154 811 fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
cannam@154 812 ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
cannam@154 813
cannam@154 814 ec_encode(ec, fl, fl+fs, ft);
cannam@154 815 } else {
cannam@154 816 /* Triangular pdf */
cannam@154 817 int fl=0;
cannam@154 818 int fm;
cannam@154 819 fm = ec_decode(ec, ft);
cannam@154 820
cannam@154 821 if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
cannam@154 822 {
cannam@154 823 itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
cannam@154 824 fs = itheta + 1;
cannam@154 825 fl = itheta*(itheta + 1)>>1;
cannam@154 826 }
cannam@154 827 else
cannam@154 828 {
cannam@154 829 itheta = (2*(qn + 1)
cannam@154 830 - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
cannam@154 831 fs = qn + 1 - itheta;
cannam@154 832 fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
cannam@154 833 }
cannam@154 834
cannam@154 835 ec_dec_update(ec, fl, fl+fs, ft);
cannam@154 836 }
cannam@154 837 }
cannam@154 838 celt_assert(itheta>=0);
cannam@154 839 itheta = celt_udiv((opus_int32)itheta*16384, qn);
cannam@154 840 if (encode && stereo)
cannam@154 841 {
cannam@154 842 if (itheta==0)
cannam@154 843 intensity_stereo(m, X, Y, bandE, i, N);
cannam@154 844 else
cannam@154 845 stereo_split(X, Y, N);
cannam@154 846 }
cannam@154 847 /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate.
cannam@154 848 Let's do that at higher complexity */
cannam@154 849 } else if (stereo) {
cannam@154 850 if (encode)
cannam@154 851 {
cannam@154 852 inv = itheta > 8192 && !ctx->disable_inv;
cannam@154 853 if (inv)
cannam@154 854 {
cannam@154 855 int j;
cannam@154 856 for (j=0;j<N;j++)
cannam@154 857 Y[j] = -Y[j];
cannam@154 858 }
cannam@154 859 intensity_stereo(m, X, Y, bandE, i, N);
cannam@154 860 }
cannam@154 861 if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
cannam@154 862 {
cannam@154 863 if (encode)
cannam@154 864 ec_enc_bit_logp(ec, inv, 2);
cannam@154 865 else
cannam@154 866 inv = ec_dec_bit_logp(ec, 2);
cannam@154 867 } else
cannam@154 868 inv = 0;
cannam@154 869 /* inv flag override to avoid problems with downmixing. */
cannam@154 870 if (ctx->disable_inv)
cannam@154 871 inv = 0;
cannam@154 872 itheta = 0;
cannam@154 873 }
cannam@154 874 qalloc = ec_tell_frac(ec) - tell;
cannam@154 875 *b -= qalloc;
cannam@154 876
cannam@154 877 if (itheta == 0)
cannam@154 878 {
cannam@154 879 imid = 32767;
cannam@154 880 iside = 0;
cannam@154 881 *fill &= (1<<B)-1;
cannam@154 882 delta = -16384;
cannam@154 883 } else if (itheta == 16384)
cannam@154 884 {
cannam@154 885 imid = 0;
cannam@154 886 iside = 32767;
cannam@154 887 *fill &= ((1<<B)-1)<<B;
cannam@154 888 delta = 16384;
cannam@154 889 } else {
cannam@154 890 imid = bitexact_cos((opus_int16)itheta);
cannam@154 891 iside = bitexact_cos((opus_int16)(16384-itheta));
cannam@154 892 /* This is the mid vs side allocation that minimizes squared error
cannam@154 893 in that band. */
cannam@154 894 delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
cannam@154 895 }
cannam@154 896
cannam@154 897 sctx->inv = inv;
cannam@154 898 sctx->imid = imid;
cannam@154 899 sctx->iside = iside;
cannam@154 900 sctx->delta = delta;
cannam@154 901 sctx->itheta = itheta;
cannam@154 902 sctx->qalloc = qalloc;
cannam@154 903 }
cannam@154 904 static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y, int b,
cannam@154 905 celt_norm *lowband_out)
cannam@154 906 {
cannam@154 907 int c;
cannam@154 908 int stereo;
cannam@154 909 celt_norm *x = X;
cannam@154 910 int encode;
cannam@154 911 ec_ctx *ec;
cannam@154 912
cannam@154 913 encode = ctx->encode;
cannam@154 914 ec = ctx->ec;
cannam@154 915
cannam@154 916 stereo = Y != NULL;
cannam@154 917 c=0; do {
cannam@154 918 int sign=0;
cannam@154 919 if (ctx->remaining_bits>=1<<BITRES)
cannam@154 920 {
cannam@154 921 if (encode)
cannam@154 922 {
cannam@154 923 sign = x[0]<0;
cannam@154 924 ec_enc_bits(ec, sign, 1);
cannam@154 925 } else {
cannam@154 926 sign = ec_dec_bits(ec, 1);
cannam@154 927 }
cannam@154 928 ctx->remaining_bits -= 1<<BITRES;
cannam@154 929 b-=1<<BITRES;
cannam@154 930 }
cannam@154 931 if (ctx->resynth)
cannam@154 932 x[0] = sign ? -NORM_SCALING : NORM_SCALING;
cannam@154 933 x = Y;
cannam@154 934 } while (++c<1+stereo);
cannam@154 935 if (lowband_out)
cannam@154 936 lowband_out[0] = SHR16(X[0],4);
cannam@154 937 return 1;
cannam@154 938 }
cannam@154 939
cannam@154 940 /* This function is responsible for encoding and decoding a mono partition.
cannam@154 941 It can split the band in two and transmit the energy difference with
cannam@154 942 the two half-bands. It can be called recursively so bands can end up being
cannam@154 943 split in 8 parts. */
cannam@154 944 static unsigned quant_partition(struct band_ctx *ctx, celt_norm *X,
cannam@154 945 int N, int b, int B, celt_norm *lowband,
cannam@154 946 int LM,
cannam@154 947 opus_val16 gain, int fill)
cannam@154 948 {
cannam@154 949 const unsigned char *cache;
cannam@154 950 int q;
cannam@154 951 int curr_bits;
cannam@154 952 int imid=0, iside=0;
cannam@154 953 int B0=B;
cannam@154 954 opus_val16 mid=0, side=0;
cannam@154 955 unsigned cm=0;
cannam@154 956 celt_norm *Y=NULL;
cannam@154 957 int encode;
cannam@154 958 const CELTMode *m;
cannam@154 959 int i;
cannam@154 960 int spread;
cannam@154 961 ec_ctx *ec;
cannam@154 962
cannam@154 963 encode = ctx->encode;
cannam@154 964 m = ctx->m;
cannam@154 965 i = ctx->i;
cannam@154 966 spread = ctx->spread;
cannam@154 967 ec = ctx->ec;
cannam@154 968
cannam@154 969 /* If we need 1.5 more bit than we can produce, split the band in two. */
cannam@154 970 cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
cannam@154 971 if (LM != -1 && b > cache[cache[0]]+12 && N>2)
cannam@154 972 {
cannam@154 973 int mbits, sbits, delta;
cannam@154 974 int itheta;
cannam@154 975 int qalloc;
cannam@154 976 struct split_ctx sctx;
cannam@154 977 celt_norm *next_lowband2=NULL;
cannam@154 978 opus_int32 rebalance;
cannam@154 979
cannam@154 980 N >>= 1;
cannam@154 981 Y = X+N;
cannam@154 982 LM -= 1;
cannam@154 983 if (B==1)
cannam@154 984 fill = (fill&1)|(fill<<1);
cannam@154 985 B = (B+1)>>1;
cannam@154 986
cannam@154 987 compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill);
cannam@154 988 imid = sctx.imid;
cannam@154 989 iside = sctx.iside;
cannam@154 990 delta = sctx.delta;
cannam@154 991 itheta = sctx.itheta;
cannam@154 992 qalloc = sctx.qalloc;
cannam@154 993 #ifdef FIXED_POINT
cannam@154 994 mid = imid;
cannam@154 995 side = iside;
cannam@154 996 #else
cannam@154 997 mid = (1.f/32768)*imid;
cannam@154 998 side = (1.f/32768)*iside;
cannam@154 999 #endif
cannam@154 1000
cannam@154 1001 /* Give more bits to low-energy MDCTs than they would otherwise deserve */
cannam@154 1002 if (B0>1 && (itheta&0x3fff))
cannam@154 1003 {
cannam@154 1004 if (itheta > 8192)
cannam@154 1005 /* Rough approximation for pre-echo masking */
cannam@154 1006 delta -= delta>>(4-LM);
cannam@154 1007 else
cannam@154 1008 /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
cannam@154 1009 delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
cannam@154 1010 }
cannam@154 1011 mbits = IMAX(0, IMIN(b, (b-delta)/2));
cannam@154 1012 sbits = b-mbits;
cannam@154 1013 ctx->remaining_bits -= qalloc;
cannam@154 1014
cannam@154 1015 if (lowband)
cannam@154 1016 next_lowband2 = lowband+N; /* >32-bit split case */
cannam@154 1017
cannam@154 1018 rebalance = ctx->remaining_bits;
cannam@154 1019 if (mbits >= sbits)
cannam@154 1020 {
cannam@154 1021 cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
cannam@154 1022 MULT16_16_P15(gain,mid), fill);
cannam@154 1023 rebalance = mbits - (rebalance-ctx->remaining_bits);
cannam@154 1024 if (rebalance > 3<<BITRES && itheta!=0)
cannam@154 1025 sbits += rebalance - (3<<BITRES);
cannam@154 1026 cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
cannam@154 1027 MULT16_16_P15(gain,side), fill>>B)<<(B0>>1);
cannam@154 1028 } else {
cannam@154 1029 cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
cannam@154 1030 MULT16_16_P15(gain,side), fill>>B)<<(B0>>1);
cannam@154 1031 rebalance = sbits - (rebalance-ctx->remaining_bits);
cannam@154 1032 if (rebalance > 3<<BITRES && itheta!=16384)
cannam@154 1033 mbits += rebalance - (3<<BITRES);
cannam@154 1034 cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
cannam@154 1035 MULT16_16_P15(gain,mid), fill);
cannam@154 1036 }
cannam@154 1037 } else {
cannam@154 1038 /* This is the basic no-split case */
cannam@154 1039 q = bits2pulses(m, i, LM, b);
cannam@154 1040 curr_bits = pulses2bits(m, i, LM, q);
cannam@154 1041 ctx->remaining_bits -= curr_bits;
cannam@154 1042
cannam@154 1043 /* Ensures we can never bust the budget */
cannam@154 1044 while (ctx->remaining_bits < 0 && q > 0)
cannam@154 1045 {
cannam@154 1046 ctx->remaining_bits += curr_bits;
cannam@154 1047 q--;
cannam@154 1048 curr_bits = pulses2bits(m, i, LM, q);
cannam@154 1049 ctx->remaining_bits -= curr_bits;
cannam@154 1050 }
cannam@154 1051
cannam@154 1052 if (q!=0)
cannam@154 1053 {
cannam@154 1054 int K = get_pulses(q);
cannam@154 1055
cannam@154 1056 /* Finally do the actual quantization */
cannam@154 1057 if (encode)
cannam@154 1058 {
cannam@154 1059 cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth, ctx->arch);
cannam@154 1060 } else {
cannam@154 1061 cm = alg_unquant(X, N, K, spread, B, ec, gain);
cannam@154 1062 }
cannam@154 1063 } else {
cannam@154 1064 /* If there's no pulse, fill the band anyway */
cannam@154 1065 int j;
cannam@154 1066 if (ctx->resynth)
cannam@154 1067 {
cannam@154 1068 unsigned cm_mask;
cannam@154 1069 /* B can be as large as 16, so this shift might overflow an int on a
cannam@154 1070 16-bit platform; use a long to get defined behavior.*/
cannam@154 1071 cm_mask = (unsigned)(1UL<<B)-1;
cannam@154 1072 fill &= cm_mask;
cannam@154 1073 if (!fill)
cannam@154 1074 {
cannam@154 1075 OPUS_CLEAR(X, N);
cannam@154 1076 } else {
cannam@154 1077 if (lowband == NULL)
cannam@154 1078 {
cannam@154 1079 /* Noise */
cannam@154 1080 for (j=0;j<N;j++)
cannam@154 1081 {
cannam@154 1082 ctx->seed = celt_lcg_rand(ctx->seed);
cannam@154 1083 X[j] = (celt_norm)((opus_int32)ctx->seed>>20);
cannam@154 1084 }
cannam@154 1085 cm = cm_mask;
cannam@154 1086 } else {
cannam@154 1087 /* Folded spectrum */
cannam@154 1088 for (j=0;j<N;j++)
cannam@154 1089 {
cannam@154 1090 opus_val16 tmp;
cannam@154 1091 ctx->seed = celt_lcg_rand(ctx->seed);
cannam@154 1092 /* About 48 dB below the "normal" folding level */
cannam@154 1093 tmp = QCONST16(1.0f/256, 10);
cannam@154 1094 tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
cannam@154 1095 X[j] = lowband[j]+tmp;
cannam@154 1096 }
cannam@154 1097 cm = fill;
cannam@154 1098 }
cannam@154 1099 renormalise_vector(X, N, gain, ctx->arch);
cannam@154 1100 }
cannam@154 1101 }
cannam@154 1102 }
cannam@154 1103 }
cannam@154 1104
cannam@154 1105 return cm;
cannam@154 1106 }
cannam@154 1107
cannam@154 1108
cannam@154 1109 /* This function is responsible for encoding and decoding a band for the mono case. */
cannam@154 1110 static unsigned quant_band(struct band_ctx *ctx, celt_norm *X,
cannam@154 1111 int N, int b, int B, celt_norm *lowband,
cannam@154 1112 int LM, celt_norm *lowband_out,
cannam@154 1113 opus_val16 gain, celt_norm *lowband_scratch, int fill)
cannam@154 1114 {
cannam@154 1115 int N0=N;
cannam@154 1116 int N_B=N;
cannam@154 1117 int N_B0;
cannam@154 1118 int B0=B;
cannam@154 1119 int time_divide=0;
cannam@154 1120 int recombine=0;
cannam@154 1121 int longBlocks;
cannam@154 1122 unsigned cm=0;
cannam@154 1123 int k;
cannam@154 1124 int encode;
cannam@154 1125 int tf_change;
cannam@154 1126
cannam@154 1127 encode = ctx->encode;
cannam@154 1128 tf_change = ctx->tf_change;
cannam@154 1129
cannam@154 1130 longBlocks = B0==1;
cannam@154 1131
cannam@154 1132 N_B = celt_udiv(N_B, B);
cannam@154 1133
cannam@154 1134 /* Special case for one sample */
cannam@154 1135 if (N==1)
cannam@154 1136 {
cannam@154 1137 return quant_band_n1(ctx, X, NULL, b, lowband_out);
cannam@154 1138 }
cannam@154 1139
cannam@154 1140 if (tf_change>0)
cannam@154 1141 recombine = tf_change;
cannam@154 1142 /* Band recombining to increase frequency resolution */
cannam@154 1143
cannam@154 1144 if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
cannam@154 1145 {
cannam@154 1146 OPUS_COPY(lowband_scratch, lowband, N);
cannam@154 1147 lowband = lowband_scratch;
cannam@154 1148 }
cannam@154 1149
cannam@154 1150 for (k=0;k<recombine;k++)
cannam@154 1151 {
cannam@154 1152 static const unsigned char bit_interleave_table[16]={
cannam@154 1153 0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
cannam@154 1154 };
cannam@154 1155 if (encode)
cannam@154 1156 haar1(X, N>>k, 1<<k);
cannam@154 1157 if (lowband)
cannam@154 1158 haar1(lowband, N>>k, 1<<k);
cannam@154 1159 fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
cannam@154 1160 }
cannam@154 1161 B>>=recombine;
cannam@154 1162 N_B<<=recombine;
cannam@154 1163
cannam@154 1164 /* Increasing the time resolution */
cannam@154 1165 while ((N_B&1) == 0 && tf_change<0)
cannam@154 1166 {
cannam@154 1167 if (encode)
cannam@154 1168 haar1(X, N_B, B);
cannam@154 1169 if (lowband)
cannam@154 1170 haar1(lowband, N_B, B);
cannam@154 1171 fill |= fill<<B;
cannam@154 1172 B <<= 1;
cannam@154 1173 N_B >>= 1;
cannam@154 1174 time_divide++;
cannam@154 1175 tf_change++;
cannam@154 1176 }
cannam@154 1177 B0=B;
cannam@154 1178 N_B0 = N_B;
cannam@154 1179
cannam@154 1180 /* Reorganize the samples in time order instead of frequency order */
cannam@154 1181 if (B0>1)
cannam@154 1182 {
cannam@154 1183 if (encode)
cannam@154 1184 deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
cannam@154 1185 if (lowband)
cannam@154 1186 deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
cannam@154 1187 }
cannam@154 1188
cannam@154 1189 cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill);
cannam@154 1190
cannam@154 1191 /* This code is used by the decoder and by the resynthesis-enabled encoder */
cannam@154 1192 if (ctx->resynth)
cannam@154 1193 {
cannam@154 1194 /* Undo the sample reorganization going from time order to frequency order */
cannam@154 1195 if (B0>1)
cannam@154 1196 interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
cannam@154 1197
cannam@154 1198 /* Undo time-freq changes that we did earlier */
cannam@154 1199 N_B = N_B0;
cannam@154 1200 B = B0;
cannam@154 1201 for (k=0;k<time_divide;k++)
cannam@154 1202 {
cannam@154 1203 B >>= 1;
cannam@154 1204 N_B <<= 1;
cannam@154 1205 cm |= cm>>B;
cannam@154 1206 haar1(X, N_B, B);
cannam@154 1207 }
cannam@154 1208
cannam@154 1209 for (k=0;k<recombine;k++)
cannam@154 1210 {
cannam@154 1211 static const unsigned char bit_deinterleave_table[16]={
cannam@154 1212 0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
cannam@154 1213 0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
cannam@154 1214 };
cannam@154 1215 cm = bit_deinterleave_table[cm];
cannam@154 1216 haar1(X, N0>>k, 1<<k);
cannam@154 1217 }
cannam@154 1218 B<<=recombine;
cannam@154 1219
cannam@154 1220 /* Scale output for later folding */
cannam@154 1221 if (lowband_out)
cannam@154 1222 {
cannam@154 1223 int j;
cannam@154 1224 opus_val16 n;
cannam@154 1225 n = celt_sqrt(SHL32(EXTEND32(N0),22));
cannam@154 1226 for (j=0;j<N0;j++)
cannam@154 1227 lowband_out[j] = MULT16_16_Q15(n,X[j]);
cannam@154 1228 }
cannam@154 1229 cm &= (1<<B)-1;
cannam@154 1230 }
cannam@154 1231 return cm;
cannam@154 1232 }
cannam@154 1233
cannam@154 1234
cannam@154 1235 /* This function is responsible for encoding and decoding a band for the stereo case. */
cannam@154 1236 static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm *Y,
cannam@154 1237 int N, int b, int B, celt_norm *lowband,
cannam@154 1238 int LM, celt_norm *lowband_out,
cannam@154 1239 celt_norm *lowband_scratch, int fill)
cannam@154 1240 {
cannam@154 1241 int imid=0, iside=0;
cannam@154 1242 int inv = 0;
cannam@154 1243 opus_val16 mid=0, side=0;
cannam@154 1244 unsigned cm=0;
cannam@154 1245 int mbits, sbits, delta;
cannam@154 1246 int itheta;
cannam@154 1247 int qalloc;
cannam@154 1248 struct split_ctx sctx;
cannam@154 1249 int orig_fill;
cannam@154 1250 int encode;
cannam@154 1251 ec_ctx *ec;
cannam@154 1252
cannam@154 1253 encode = ctx->encode;
cannam@154 1254 ec = ctx->ec;
cannam@154 1255
cannam@154 1256 /* Special case for one sample */
cannam@154 1257 if (N==1)
cannam@154 1258 {
cannam@154 1259 return quant_band_n1(ctx, X, Y, b, lowband_out);
cannam@154 1260 }
cannam@154 1261
cannam@154 1262 orig_fill = fill;
cannam@154 1263
cannam@154 1264 compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill);
cannam@154 1265 inv = sctx.inv;
cannam@154 1266 imid = sctx.imid;
cannam@154 1267 iside = sctx.iside;
cannam@154 1268 delta = sctx.delta;
cannam@154 1269 itheta = sctx.itheta;
cannam@154 1270 qalloc = sctx.qalloc;
cannam@154 1271 #ifdef FIXED_POINT
cannam@154 1272 mid = imid;
cannam@154 1273 side = iside;
cannam@154 1274 #else
cannam@154 1275 mid = (1.f/32768)*imid;
cannam@154 1276 side = (1.f/32768)*iside;
cannam@154 1277 #endif
cannam@154 1278
cannam@154 1279 /* This is a special case for N=2 that only works for stereo and takes
cannam@154 1280 advantage of the fact that mid and side are orthogonal to encode
cannam@154 1281 the side with just one bit. */
cannam@154 1282 if (N==2)
cannam@154 1283 {
cannam@154 1284 int c;
cannam@154 1285 int sign=0;
cannam@154 1286 celt_norm *x2, *y2;
cannam@154 1287 mbits = b;
cannam@154 1288 sbits = 0;
cannam@154 1289 /* Only need one bit for the side. */
cannam@154 1290 if (itheta != 0 && itheta != 16384)
cannam@154 1291 sbits = 1<<BITRES;
cannam@154 1292 mbits -= sbits;
cannam@154 1293 c = itheta > 8192;
cannam@154 1294 ctx->remaining_bits -= qalloc+sbits;
cannam@154 1295
cannam@154 1296 x2 = c ? Y : X;
cannam@154 1297 y2 = c ? X : Y;
cannam@154 1298 if (sbits)
cannam@154 1299 {
cannam@154 1300 if (encode)
cannam@154 1301 {
cannam@154 1302 /* Here we only need to encode a sign for the side. */
cannam@154 1303 sign = x2[0]*y2[1] - x2[1]*y2[0] < 0;
cannam@154 1304 ec_enc_bits(ec, sign, 1);
cannam@154 1305 } else {
cannam@154 1306 sign = ec_dec_bits(ec, 1);
cannam@154 1307 }
cannam@154 1308 }
cannam@154 1309 sign = 1-2*sign;
cannam@154 1310 /* We use orig_fill here because we want to fold the side, but if
cannam@154 1311 itheta==16384, we'll have cleared the low bits of fill. */
cannam@154 1312 cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q15ONE,
cannam@154 1313 lowband_scratch, orig_fill);
cannam@154 1314 /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse),
cannam@154 1315 and there's no need to worry about mixing with the other channel. */
cannam@154 1316 y2[0] = -sign*x2[1];
cannam@154 1317 y2[1] = sign*x2[0];
cannam@154 1318 if (ctx->resynth)
cannam@154 1319 {
cannam@154 1320 celt_norm tmp;
cannam@154 1321 X[0] = MULT16_16_Q15(mid, X[0]);
cannam@154 1322 X[1] = MULT16_16_Q15(mid, X[1]);
cannam@154 1323 Y[0] = MULT16_16_Q15(side, Y[0]);
cannam@154 1324 Y[1] = MULT16_16_Q15(side, Y[1]);
cannam@154 1325 tmp = X[0];
cannam@154 1326 X[0] = SUB16(tmp,Y[0]);
cannam@154 1327 Y[0] = ADD16(tmp,Y[0]);
cannam@154 1328 tmp = X[1];
cannam@154 1329 X[1] = SUB16(tmp,Y[1]);
cannam@154 1330 Y[1] = ADD16(tmp,Y[1]);
cannam@154 1331 }
cannam@154 1332 } else {
cannam@154 1333 /* "Normal" split code */
cannam@154 1334 opus_int32 rebalance;
cannam@154 1335
cannam@154 1336 mbits = IMAX(0, IMIN(b, (b-delta)/2));
cannam@154 1337 sbits = b-mbits;
cannam@154 1338 ctx->remaining_bits -= qalloc;
cannam@154 1339
cannam@154 1340 rebalance = ctx->remaining_bits;
cannam@154 1341 if (mbits >= sbits)
cannam@154 1342 {
cannam@154 1343 /* In stereo mode, we do not apply a scaling to the mid because we need the normalized
cannam@154 1344 mid for folding later. */
cannam@154 1345 cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q15ONE,
cannam@154 1346 lowband_scratch, fill);
cannam@154 1347 rebalance = mbits - (rebalance-ctx->remaining_bits);
cannam@154 1348 if (rebalance > 3<<BITRES && itheta!=0)
cannam@154 1349 sbits += rebalance - (3<<BITRES);
cannam@154 1350
cannam@154 1351 /* For a stereo split, the high bits of fill are always zero, so no
cannam@154 1352 folding will be done to the side. */
cannam@154 1353 cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B);
cannam@154 1354 } else {
cannam@154 1355 /* For a stereo split, the high bits of fill are always zero, so no
cannam@154 1356 folding will be done to the side. */
cannam@154 1357 cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B);
cannam@154 1358 rebalance = sbits - (rebalance-ctx->remaining_bits);
cannam@154 1359 if (rebalance > 3<<BITRES && itheta!=16384)
cannam@154 1360 mbits += rebalance - (3<<BITRES);
cannam@154 1361 /* In stereo mode, we do not apply a scaling to the mid because we need the normalized
cannam@154 1362 mid for folding later. */
cannam@154 1363 cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q15ONE,
cannam@154 1364 lowband_scratch, fill);
cannam@154 1365 }
cannam@154 1366 }
cannam@154 1367
cannam@154 1368
cannam@154 1369 /* This code is used by the decoder and by the resynthesis-enabled encoder */
cannam@154 1370 if (ctx->resynth)
cannam@154 1371 {
cannam@154 1372 if (N!=2)
cannam@154 1373 stereo_merge(X, Y, mid, N, ctx->arch);
cannam@154 1374 if (inv)
cannam@154 1375 {
cannam@154 1376 int j;
cannam@154 1377 for (j=0;j<N;j++)
cannam@154 1378 Y[j] = -Y[j];
cannam@154 1379 }
cannam@154 1380 }
cannam@154 1381 return cm;
cannam@154 1382 }
cannam@154 1383
cannam@154 1384 static void special_hybrid_folding(const CELTMode *m, celt_norm *norm, celt_norm *norm2, int start, int M, int dual_stereo)
cannam@154 1385 {
cannam@154 1386 int n1, n2;
cannam@154 1387 const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
cannam@154 1388 n1 = M*(eBands[start+1]-eBands[start]);
cannam@154 1389 n2 = M*(eBands[start+2]-eBands[start+1]);
cannam@154 1390 /* Duplicate enough of the first band folding data to be able to fold the second band.
cannam@154 1391 Copies no data for CELT-only mode. */
cannam@154 1392 OPUS_COPY(&norm[n1], &norm[2*n1 - n2], n2-n1);
cannam@154 1393 if (dual_stereo)
cannam@154 1394 OPUS_COPY(&norm2[n1], &norm2[2*n1 - n2], n2-n1);
cannam@154 1395 }
cannam@154 1396
cannam@154 1397 void quant_all_bands(int encode, const CELTMode *m, int start, int end,
cannam@154 1398 celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks,
cannam@154 1399 const celt_ener *bandE, int *pulses, int shortBlocks, int spread,
cannam@154 1400 int dual_stereo, int intensity, int *tf_res, opus_int32 total_bits,
cannam@154 1401 opus_int32 balance, ec_ctx *ec, int LM, int codedBands,
cannam@154 1402 opus_uint32 *seed, int complexity, int arch, int disable_inv)
cannam@154 1403 {
cannam@154 1404 int i;
cannam@154 1405 opus_int32 remaining_bits;
cannam@154 1406 const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
cannam@154 1407 celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
cannam@154 1408 VARDECL(celt_norm, _norm);
cannam@154 1409 VARDECL(celt_norm, _lowband_scratch);
cannam@154 1410 VARDECL(celt_norm, X_save);
cannam@154 1411 VARDECL(celt_norm, Y_save);
cannam@154 1412 VARDECL(celt_norm, X_save2);
cannam@154 1413 VARDECL(celt_norm, Y_save2);
cannam@154 1414 VARDECL(celt_norm, norm_save2);
cannam@154 1415 int resynth_alloc;
cannam@154 1416 celt_norm *lowband_scratch;
cannam@154 1417 int B;
cannam@154 1418 int M;
cannam@154 1419 int lowband_offset;
cannam@154 1420 int update_lowband = 1;
cannam@154 1421 int C = Y_ != NULL ? 2 : 1;
cannam@154 1422 int norm_offset;
cannam@154 1423 int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
cannam@154 1424 #ifdef RESYNTH
cannam@154 1425 int resynth = 1;
cannam@154 1426 #else
cannam@154 1427 int resynth = !encode || theta_rdo;
cannam@154 1428 #endif
cannam@154 1429 struct band_ctx ctx;
cannam@154 1430 SAVE_STACK;
cannam@154 1431
cannam@154 1432 M = 1<<LM;
cannam@154 1433 B = shortBlocks ? M : 1;
cannam@154 1434 norm_offset = M*eBands[start];
cannam@154 1435 /* No need to allocate norm for the last band because we don't need an
cannam@154 1436 output in that band. */
cannam@154 1437 ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
cannam@154 1438 norm = _norm;
cannam@154 1439 norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset;
cannam@154 1440
cannam@154 1441 /* For decoding, we can use the last band as scratch space because we don't need that
cannam@154 1442 scratch space for the last band and we don't care about the data there until we're
cannam@154 1443 decoding the last band. */
cannam@154 1444 if (encode && resynth)
cannam@154 1445 resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
cannam@154 1446 else
cannam@154 1447 resynth_alloc = ALLOC_NONE;
cannam@154 1448 ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
cannam@154 1449 if (encode && resynth)
cannam@154 1450 lowband_scratch = _lowband_scratch;
cannam@154 1451 else
cannam@154 1452 lowband_scratch = X_+M*eBands[m->nbEBands-1];
cannam@154 1453 ALLOC(X_save, resynth_alloc, celt_norm);
cannam@154 1454 ALLOC(Y_save, resynth_alloc, celt_norm);
cannam@154 1455 ALLOC(X_save2, resynth_alloc, celt_norm);
cannam@154 1456 ALLOC(Y_save2, resynth_alloc, celt_norm);
cannam@154 1457 ALLOC(norm_save2, resynth_alloc, celt_norm);
cannam@154 1458
cannam@154 1459 lowband_offset = 0;
cannam@154 1460 ctx.bandE = bandE;
cannam@154 1461 ctx.ec = ec;
cannam@154 1462 ctx.encode = encode;
cannam@154 1463 ctx.intensity = intensity;
cannam@154 1464 ctx.m = m;
cannam@154 1465 ctx.seed = *seed;
cannam@154 1466 ctx.spread = spread;
cannam@154 1467 ctx.arch = arch;
cannam@154 1468 ctx.disable_inv = disable_inv;
cannam@154 1469 ctx.resynth = resynth;
cannam@154 1470 ctx.theta_round = 0;
cannam@154 1471 /* Avoid injecting noise in the first band on transients. */
cannam@154 1472 ctx.avoid_split_noise = B > 1;
cannam@154 1473 for (i=start;i<end;i++)
cannam@154 1474 {
cannam@154 1475 opus_int32 tell;
cannam@154 1476 int b;
cannam@154 1477 int N;
cannam@154 1478 opus_int32 curr_balance;
cannam@154 1479 int effective_lowband=-1;
cannam@154 1480 celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
cannam@154 1481 int tf_change=0;
cannam@154 1482 unsigned x_cm;
cannam@154 1483 unsigned y_cm;
cannam@154 1484 int last;
cannam@154 1485
cannam@154 1486 ctx.i = i;
cannam@154 1487 last = (i==end-1);
cannam@154 1488
cannam@154 1489 X = X_+M*eBands[i];
cannam@154 1490 if (Y_!=NULL)
cannam@154 1491 Y = Y_+M*eBands[i];
cannam@154 1492 else
cannam@154 1493 Y = NULL;
cannam@154 1494 N = M*eBands[i+1]-M*eBands[i];
cannam@154 1495 celt_assert(N > 0);
cannam@154 1496 tell = ec_tell_frac(ec);
cannam@154 1497
cannam@154 1498 /* Compute how many bits we want to allocate to this band */
cannam@154 1499 if (i != start)
cannam@154 1500 balance -= tell;
cannam@154 1501 remaining_bits = total_bits-tell-1;
cannam@154 1502 ctx.remaining_bits = remaining_bits;
cannam@154 1503 if (i <= codedBands-1)
cannam@154 1504 {
cannam@154 1505 curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
cannam@154 1506 b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
cannam@154 1507 } else {
cannam@154 1508 b = 0;
cannam@154 1509 }
cannam@154 1510
cannam@154 1511 #ifndef DISABLE_UPDATE_DRAFT
cannam@154 1512 if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
cannam@154 1513 lowband_offset = i;
cannam@154 1514 if (i == start+1)
cannam@154 1515 special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
cannam@154 1516 #else
cannam@154 1517 if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0))
cannam@154 1518 lowband_offset = i;
cannam@154 1519 #endif
cannam@154 1520
cannam@154 1521 tf_change = tf_res[i];
cannam@154 1522 ctx.tf_change = tf_change;
cannam@154 1523 if (i>=m->effEBands)
cannam@154 1524 {
cannam@154 1525 X=norm;
cannam@154 1526 if (Y_!=NULL)
cannam@154 1527 Y = norm;
cannam@154 1528 lowband_scratch = NULL;
cannam@154 1529 }
cannam@154 1530 if (last && !theta_rdo)
cannam@154 1531 lowband_scratch = NULL;
cannam@154 1532
cannam@154 1533 /* Get a conservative estimate of the collapse_mask's for the bands we're
cannam@154 1534 going to be folding from. */
cannam@154 1535 if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
cannam@154 1536 {
cannam@154 1537 int fold_start;
cannam@154 1538 int fold_end;
cannam@154 1539 int fold_i;
cannam@154 1540 /* This ensures we never repeat spectral content within one band */
cannam@154 1541 effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
cannam@154 1542 fold_start = lowband_offset;
cannam@154 1543 while(M*eBands[--fold_start] > effective_lowband+norm_offset);
cannam@154 1544 fold_end = lowband_offset-1;
cannam@154 1545 #ifndef DISABLE_UPDATE_DRAFT
cannam@154 1546 while(++fold_end < i && M*eBands[fold_end] < effective_lowband+norm_offset+N);
cannam@154 1547 #else
cannam@154 1548 while(M*eBands[++fold_end] < effective_lowband+norm_offset+N);
cannam@154 1549 #endif
cannam@154 1550 x_cm = y_cm = 0;
cannam@154 1551 fold_i = fold_start; do {
cannam@154 1552 x_cm |= collapse_masks[fold_i*C+0];
cannam@154 1553 y_cm |= collapse_masks[fold_i*C+C-1];
cannam@154 1554 } while (++fold_i<fold_end);
cannam@154 1555 }
cannam@154 1556 /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
cannam@154 1557 always) be non-zero. */
cannam@154 1558 else
cannam@154 1559 x_cm = y_cm = (1<<B)-1;
cannam@154 1560
cannam@154 1561 if (dual_stereo && i==intensity)
cannam@154 1562 {
cannam@154 1563 int j;
cannam@154 1564
cannam@154 1565 /* Switch off dual stereo to do intensity. */
cannam@154 1566 dual_stereo = 0;
cannam@154 1567 if (resynth)
cannam@154 1568 for (j=0;j<M*eBands[i]-norm_offset;j++)
cannam@154 1569 norm[j] = HALF32(norm[j]+norm2[j]);
cannam@154 1570 }
cannam@154 1571 if (dual_stereo)
cannam@154 1572 {
cannam@154 1573 x_cm = quant_band(&ctx, X, N, b/2, B,
cannam@154 1574 effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
cannam@154 1575 last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, x_cm);
cannam@154 1576 y_cm = quant_band(&ctx, Y, N, b/2, B,
cannam@154 1577 effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
cannam@154 1578 last?NULL:norm2+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, y_cm);
cannam@154 1579 } else {
cannam@154 1580 if (Y!=NULL)
cannam@154 1581 {
cannam@154 1582 if (theta_rdo && i < intensity)
cannam@154 1583 {
cannam@154 1584 ec_ctx ec_save, ec_save2;
cannam@154 1585 struct band_ctx ctx_save, ctx_save2;
cannam@154 1586 opus_val32 dist0, dist1;
cannam@154 1587 unsigned cm, cm2;
cannam@154 1588 int nstart_bytes, nend_bytes, save_bytes;
cannam@154 1589 unsigned char *bytes_buf;
cannam@154 1590 unsigned char bytes_save[1275];
cannam@154 1591 opus_val16 w[2];
cannam@154 1592 compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
cannam@154 1593 /* Make a copy. */
cannam@154 1594 cm = x_cm|y_cm;
cannam@154 1595 ec_save = *ec;
cannam@154 1596 ctx_save = ctx;
cannam@154 1597 OPUS_COPY(X_save, X, N);
cannam@154 1598 OPUS_COPY(Y_save, Y, N);
cannam@154 1599 /* Encode and round down. */
cannam@154 1600 ctx.theta_round = -1;
cannam@154 1601 x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
cannam@154 1602 effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
cannam@154 1603 last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm);
cannam@154 1604 dist0 = MULT16_32_Q15(w[0], celt_inner_prod(X_save, X, N, arch)) + MULT16_32_Q15(w[1], celt_inner_prod(Y_save, Y, N, arch));
cannam@154 1605
cannam@154 1606 /* Save first result. */
cannam@154 1607 cm2 = x_cm;
cannam@154 1608 ec_save2 = *ec;
cannam@154 1609 ctx_save2 = ctx;
cannam@154 1610 OPUS_COPY(X_save2, X, N);
cannam@154 1611 OPUS_COPY(Y_save2, Y, N);
cannam@154 1612 if (!last)
cannam@154 1613 OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
cannam@154 1614 nstart_bytes = ec_save.offs;
cannam@154 1615 nend_bytes = ec_save.storage;
cannam@154 1616 bytes_buf = ec_save.buf+nstart_bytes;
cannam@154 1617 save_bytes = nend_bytes-nstart_bytes;
cannam@154 1618 OPUS_COPY(bytes_save, bytes_buf, save_bytes);
cannam@154 1619
cannam@154 1620 /* Restore */
cannam@154 1621 *ec = ec_save;
cannam@154 1622 ctx = ctx_save;
cannam@154 1623 OPUS_COPY(X, X_save, N);
cannam@154 1624 OPUS_COPY(Y, Y_save, N);
cannam@154 1625 #ifndef DISABLE_UPDATE_DRAFT
cannam@154 1626 if (i == start+1)
cannam@154 1627 special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
cannam@154 1628 #endif
cannam@154 1629 /* Encode and round up. */
cannam@154 1630 ctx.theta_round = 1;
cannam@154 1631 x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
cannam@154 1632 effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
cannam@154 1633 last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm);
cannam@154 1634 dist1 = MULT16_32_Q15(w[0], celt_inner_prod(X_save, X, N, arch)) + MULT16_32_Q15(w[1], celt_inner_prod(Y_save, Y, N, arch));
cannam@154 1635 if (dist0 >= dist1) {
cannam@154 1636 x_cm = cm2;
cannam@154 1637 *ec = ec_save2;
cannam@154 1638 ctx = ctx_save2;
cannam@154 1639 OPUS_COPY(X, X_save2, N);
cannam@154 1640 OPUS_COPY(Y, Y_save2, N);
cannam@154 1641 if (!last)
cannam@154 1642 OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
cannam@154 1643 OPUS_COPY(bytes_buf, bytes_save, save_bytes);
cannam@154 1644 }
cannam@154 1645 } else {
cannam@154 1646 ctx.theta_round = 0;
cannam@154 1647 x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
cannam@154 1648 effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
cannam@154 1649 last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm);
cannam@154 1650 }
cannam@154 1651 } else {
cannam@154 1652 x_cm = quant_band(&ctx, X, N, b, B,
cannam@154 1653 effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
cannam@154 1654 last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, x_cm|y_cm);
cannam@154 1655 }
cannam@154 1656 y_cm = x_cm;
cannam@154 1657 }
cannam@154 1658 collapse_masks[i*C+0] = (unsigned char)x_cm;
cannam@154 1659 collapse_masks[i*C+C-1] = (unsigned char)y_cm;
cannam@154 1660 balance += pulses[i] + tell;
cannam@154 1661
cannam@154 1662 /* Update the folding position only as long as we have 1 bit/sample depth. */
cannam@154 1663 update_lowband = b>(N<<BITRES);
cannam@154 1664 /* We only need to avoid noise on a split for the first band. After that, we
cannam@154 1665 have folding. */
cannam@154 1666 ctx.avoid_split_noise = 0;
cannam@154 1667 }
cannam@154 1668 *seed = ctx.seed;
cannam@154 1669
cannam@154 1670 RESTORE_STACK;
cannam@154 1671 }
cannam@154 1672