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