1 /* (C) 2007-2008 Jean-Marc Valin, CSIRO
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
15 - Neither the name of the Xiph.org Foundation nor the names of its
16 contributors may be used to endorse or promote products derived from
17 this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include "stack_alloc.h"
42 #include "os_support.h"
45 static void dctIV(float *X, int len, int dim)
58 sum += x[n]*cos(M_PI/len*(n+.5)*(k+.5));
59 X[dim*k+d] = sqrt(2.f/len)*sum;
64 void exp_rotation(celt_norm_t *X, int len, int dir, int stride, int iter)
69 /* Equivalent to cos(.3) and sin(.3) */
70 c = QCONST16(0.95534,15);
71 s = dir*QCONST16(0.29552,15);
74 /* We could use MULT16_16_P15 instead of MULT16_16_Q15 for more accuracy,
75 but at this point, I really don't think it's necessary */
77 for (i=0;i<len-stride;i++)
82 Xptr[stride] = MULT16_16_Q15(c,x2) + MULT16_16_Q15(s,x1);
83 *Xptr++ = MULT16_16_Q15(c,x1) - MULT16_16_Q15(s,x2);
85 Xptr = &X[len-2*stride-1];
86 for (i=len-2*stride-1;i>=0;i--)
91 Xptr[stride] = MULT16_16_Q15(c,x2) + MULT16_16_Q15(s,x1);
92 *Xptr-- = MULT16_16_Q15(c,x1) - MULT16_16_Q15(s,x2);
99 const celt_word16_t sqrtC_1[2] = {QCONST16(1.f, 14), QCONST16(1.414214f, 14)};
102 /* Compute the amplitude (sqrt energy) in each of the bands */
103 void compute_band_energies(const CELTMode *m, const celt_sig_t *X, celt_ener_t *bank)
106 const celt_int16_t *eBands = m->eBands;
107 const int C = CHANNELS(m);
110 for (i=0;i<m->nbEBands;i++)
113 celt_word32_t maxval=0;
114 celt_word32_t sum = 0;
117 maxval = MAX32(maxval, X[j*C+c]);
118 maxval = MAX32(maxval, -X[j*C+c]);
119 } while (++j<eBands[i+1]);
123 int shift = celt_ilog2(maxval)-10;
125 sum += MULT16_16(EXTRACT16(VSHR32(X[j*C+c],shift)),
126 EXTRACT16(VSHR32(X[j*C+c],shift)));
127 } while (++j<eBands[i+1]);
128 /* We're adding one here to make damn sure we never end up with a pitch vector that's
129 larger than unity norm */
130 bank[i*C+c] = EPSILON+VSHR32(EXTEND32(celt_sqrt(sum)),-shift);
132 bank[i*C+c] = EPSILON;
134 /*printf ("%f ", bank[i*C+c]);*/
140 /* Normalise each band such that the energy is one. */
141 void normalise_bands(const CELTMode *m, const celt_sig_t * restrict freq, celt_norm_t * restrict X, const celt_ener_t *bank)
144 const celt_int16_t *eBands = m->eBands;
145 const int C = CHANNELS(m);
152 shift = celt_zlog2(bank[i*C+c])-13;
153 E = VSHR32(bank[i*C+c], shift);
154 g = EXTRACT16(celt_rcp(SHR32(MULT16_16(E,sqrtC_1[C-1]),11)));
156 X[j*C+c] = MULT16_16_Q15(VSHR32(freq[j*C+c],shift-1),g);
157 } while (++j<eBands[i+1]);
158 } while (++i<m->nbEBands);
162 #ifndef DISABLE_STEREO
163 void renormalise_bands(const CELTMode *m, celt_norm_t * restrict X)
166 VARDECL(celt_ener_t, tmpE);
167 VARDECL(celt_sig_t, freq);
169 ALLOC(tmpE, m->nbEBands*m->nbChannels, celt_ener_t);
170 ALLOC(freq, m->nbChannels*m->eBands[m->nbEBands+1], celt_sig_t);
171 for (i=0;i<m->nbChannels*m->eBands[m->nbEBands+1];i++)
172 freq[i] = SHL32(EXTEND32(X[i]), 10);
173 compute_band_energies(m, freq, tmpE);
174 normalise_bands(m, freq, X, tmpE);
177 #endif /* DISABLE_STEREO */
178 #else /* FIXED_POINT */
179 /* Compute the amplitude (sqrt energy) in each of the bands */
180 void compute_band_energies(const CELTMode *m, const celt_sig_t *X, celt_ener_t *bank)
183 const celt_int16_t *eBands = m->eBands;
184 const int C = CHANNELS(m);
187 for (i=0;i<m->nbEBands;i++)
190 celt_word32_t sum = 1e-10;
191 for (j=eBands[i];j<eBands[i+1];j++)
192 sum += X[j*C+c]*X[j*C+c];
193 bank[i*C+c] = sqrt(sum);
194 /*printf ("%f ", bank[i*C+c]);*/
200 /* Normalise each band such that the energy is one. */
201 void normalise_bands(const CELTMode *m, const celt_sig_t * restrict freq, celt_norm_t * restrict X, const celt_ener_t *bank)
204 const celt_int16_t *eBands = m->eBands;
205 const int C = CHANNELS(m);
208 for (i=0;i<m->nbEBands;i++)
211 celt_word16_t g = 1.f/(1e-10+bank[i*C+c]*sqrt(C));
212 for (j=eBands[i];j<eBands[i+1];j++)
213 X[j*C+c] = freq[j*C+c]*g;
218 #ifndef DISABLE_STEREO
219 void renormalise_bands(const CELTMode *m, celt_norm_t * restrict X)
221 VARDECL(celt_ener_t, tmpE);
223 ALLOC(tmpE, m->nbEBands*m->nbChannels, celt_ener_t);
224 compute_band_energies(m, X, tmpE);
225 normalise_bands(m, X, X, tmpE);
228 #endif /* DISABLE_STEREO */
229 #endif /* FIXED_POINT */
231 /* De-normalise the energy to produce the synthesis from the unit-energy bands */
232 void denormalise_bands(const CELTMode *m, const celt_norm_t * restrict X, celt_sig_t * restrict freq, const celt_ener_t *bank)
235 const celt_int16_t *eBands = m->eBands;
236 const int C = CHANNELS(m);
238 celt_fatal("denormalise_bands() not implemented for >2 channels");
241 for (i=0;i<m->nbEBands;i++)
244 celt_word32_t g = MULT16_32_Q13(sqrtC_1[C-1],bank[i*C+c]);
246 freq[j*C+c] = MULT16_32_Q15(X[j*C+c], g);
247 } while (++j<eBands[i+1]);
250 for (i=C*eBands[m->nbEBands];i<C*eBands[m->nbEBands+1];i++)
255 /* Compute the best gain for each "pitch band" */
256 void compute_pitch_gain(const CELTMode *m, const celt_norm_t *X, const celt_norm_t *P, celt_pgain_t *gains)
259 const celt_int16_t *pBands = m->pBands;
260 const int C = CHANNELS(m);
262 for (i=0;i<m->nbPBands;i++)
264 celt_word32_t Sxy=0, Sxx=0;
266 /* We know we're not going to overflow because Sxx can't be more than 1 (Q28) */
267 for (j=C*pBands[i];j<C*pBands[i+1];j++)
269 Sxy = MAC16_16(Sxy, X[j], P[j]);
270 Sxx = MAC16_16(Sxx, X[j], X[j]);
272 /* No negative gain allowed */
275 /* Not sure how that would happen, just making sure */
278 /* We need to be a bit conservative (multiply gain by 0.9), otherwise the
279 residual doesn't quantise well */
280 Sxy = MULT16_32_Q15(QCONST16(.9f, 15), Sxy);
282 gains[i] = EXTRACT16(celt_div(Sxy,ADD32(SHR32(Sxx, PGAIN_SHIFT),EPSILON)));
283 /*printf ("%f ", 1-sqrt(1-gain*gain));*/
287 for (i=0;i<m->nbPBands;i++)
288 printf ("%f ", 1-sqrt(1-gains[i]*gains[i]));
293 /* Apply the (quantised) gain to each "pitch band" */
294 void pitch_quant_bands(const CELTMode *m, celt_norm_t * restrict P, const celt_pgain_t * restrict gains)
297 const celt_int16_t *pBands = m->pBands;
298 const int C = CHANNELS(m);
299 for (i=0;i<m->nbPBands;i++)
302 for (j=C*pBands[i];j<C*pBands[i+1];j++)
303 P[j] = MULT16_16_Q15(gains[i], P[j]);
304 /*printf ("%f ", gain);*/
306 for (i=C*pBands[m->nbPBands];i<C*pBands[m->nbPBands+1];i++)
310 static void intensity_band(celt_norm_t * restrict X, int len)
313 celt_word32_t E = 1e-15;
314 celt_word32_t E2 = 1e-15;
318 E += MULT16_16(X[j],X[j]);
319 E2 += MULT16_16(X[2*j+1],X[2*j+1]);
322 E = celt_sqrt(E+E2)/celt_sqrt(E);
331 static void dup_band(celt_norm_t * restrict X, int len)
334 for (j=len-1;j>=0;j--)
336 X[2*j] = MULT16_16_Q15(QCONST16(.70711f,15),X[j]);
337 X[2*j+1] = MULT16_16_Q15(QCONST16(.70711f,15),X[j]);
341 static void stereo_band_mix(const CELTMode *m, celt_norm_t *X, const celt_ener_t *bank, const int *stereo_mode, int bandID, int dir)
344 const celt_int16_t *eBands = m->eBands;
345 const int C = CHANNELS(m);
348 if (stereo_mode[i] && dir <0)
350 dup_band(X+C*eBands[i], eBands[i+1]-eBands[i]);
352 celt_word16_t a1, a2;
353 if (stereo_mode[i]==0)
355 /* Do mid-side when not doing intensity stereo */
356 a1 = QCONST16(.70711f,14);
357 a2 = dir*QCONST16(.70711f,14);
359 celt_word16_t left, right;
362 int shift = celt_zlog2(MAX32(bank[i*C], bank[i*C+1]))-13;
364 left = VSHR32(bank[i*C],shift);
365 right = VSHR32(bank[i*C+1],shift);
366 norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
367 a1 = DIV32_16(SHL32(EXTEND32(left),14),norm);
368 a2 = dir*DIV32_16(SHL32(EXTEND32(right),14),norm);
370 for (j=eBands[i];j<eBands[i+1];j++)
375 X[j*C] = MULT16_16_Q14(a1,l) + MULT16_16_Q14(a2,r);
376 X[j*C+1] = MULT16_16_Q14(a1,r) - MULT16_16_Q14(a2,l);
379 if (stereo_mode[i] && dir>0)
381 intensity_band(X+C*eBands[i], eBands[i+1]-eBands[i]);
386 void stereo_decision(const CELTMode *m, celt_norm_t * restrict X, int *stereo_mode, int len)
389 for (i=0;i<len-5;i++)
397 /* Quantisation of the residual */
398 void quant_bands(const CELTMode *m, celt_norm_t * restrict X, celt_norm_t *P, celt_mask_t *W, const celt_ener_t *bandE, const int *stereo_mode, int total_bits, int time_domain, ec_enc *enc)
401 const celt_int16_t * restrict eBands = m->eBands;
402 celt_norm_t * restrict norm;
403 VARDECL(celt_norm_t, _norm);
404 VARDECL(int, pulses);
405 VARDECL(int, offsets);
406 const int C = CHANNELS(m);
409 ALLOC(_norm, C*eBands[m->nbEBands+1], celt_norm_t);
410 ALLOC(pulses, m->nbEBands, int);
411 ALLOC(offsets, m->nbEBands, int);
414 for (i=0;i<m->nbEBands;i++)
416 /* Use a single-bit margin to guard against overrunning (make sure it's enough) */
417 bits = total_bits - ec_enc_tell(enc, 0) - 1;
418 compute_allocation(m, offsets, stereo_mode, bits, pulses);
420 /*printf("bits left: %d\n", bits);
421 for (i=0;i<m->nbEBands;i++)
422 printf ("%d ", pulses[i]);
424 /*printf ("%d %d\n", ec_enc_tell(enc, 0), compute_allocation(m, m->nbPulses));*/
425 for (i=0;i<m->nbEBands;i++)
430 n = SHL16(celt_sqrt(C*(eBands[i+1]-eBands[i])),11);
433 dctIV(X+C*eBands[i], eBands[i+1]-eBands[i], C);
434 /* If pitch isn't available, use intra-frame prediction */
435 if (eBands[i] >= m->pitchEnd || q<=0)
441 intra_fold(m, X+C*eBands[i], eBands[i+1]-eBands[i], norm, P+C*eBands[i], eBands[i], eBands[m->nbEBands+1]);
443 intra_prediction(m, X+C*eBands[i], W+C*eBands[i], eBands[i+1]-eBands[i], q, norm, P+C*eBands[i], eBands[i], eBands[m->nbEBands+1], enc);
450 if (C==2 && stereo_mode[i]==1)
454 stereo_band_mix(m, X, bandE, stereo_mode, i, 1);
455 stereo_band_mix(m, P, bandE, stereo_mode, i, 1);
457 alg_quant(X+C*eBands[i], W+C*eBands[i], ch*(eBands[i+1]-eBands[i]), q, P+C*eBands[i], enc);
459 stereo_band_mix(m, X, bandE, stereo_mode, i, -1);
461 for (j=C*eBands[i];j<C*eBands[i+1];j++)
465 dctIV(X+C*eBands[i], eBands[i+1]-eBands[i], C);
466 for (j=C*eBands[i];j<C*eBands[i+1];j++)
467 norm[j] = MULT16_16_Q15(n,X[j]);
472 /* Decoding of the residual */
473 void unquant_bands(const CELTMode *m, celt_norm_t * restrict X, celt_norm_t *P, const celt_ener_t *bandE, const int *stereo_mode, int total_bits, int time_domain, ec_dec *dec)
476 const celt_int16_t * restrict eBands = m->eBands;
477 celt_norm_t * restrict norm;
478 VARDECL(celt_norm_t, _norm);
479 VARDECL(int, pulses);
480 VARDECL(int, offsets);
481 const int C = CHANNELS(m);
484 ALLOC(_norm, C*eBands[m->nbEBands+1], celt_norm_t);
485 ALLOC(pulses, m->nbEBands, int);
486 ALLOC(offsets, m->nbEBands, int);
489 for (i=0;i<m->nbEBands;i++)
491 /* Use a single-bit margin to guard against overrunning (make sure it's enough) */
492 bits = total_bits - ec_dec_tell(dec, 0) - 1;
493 compute_allocation(m, offsets, stereo_mode, bits, pulses);
495 for (i=0;i<m->nbEBands;i++)
500 n = SHL16(celt_sqrt(C*(eBands[i+1]-eBands[i])),11);
502 /* If pitch isn't available, use intra-frame prediction */
503 if (eBands[i] >= m->pitchEnd || q<=0)
509 intra_fold(m, X+C*eBands[i], eBands[i+1]-eBands[i], norm, P+C*eBands[i], eBands[i], eBands[m->nbEBands+1]);
511 intra_unquant(m, X+C*eBands[i], eBands[i+1]-eBands[i], q, norm, P+C*eBands[i], eBands[i], eBands[m->nbEBands+1], dec);
518 if (C==2 && stereo_mode[i]==1)
521 stereo_band_mix(m, P, bandE, stereo_mode, i, 1);
522 alg_unquant(X+C*eBands[i], ch*(eBands[i+1]-eBands[i]), q, P+C*eBands[i], dec);
524 stereo_band_mix(m, X, bandE, stereo_mode, i, -1);
526 for (j=C*eBands[i];j<C*eBands[i+1];j++)
530 dctIV(X+C*eBands[i], eBands[i+1]-eBands[i], C);
531 for (j=C*eBands[i];j<C*eBands[i+1];j++)
532 norm[j] = MULT16_16_Q15(n,X[j]);