annotate src/opus-1.3/celt/bands.c @ 69:7aeed7906520

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