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 "os_support.h"
43 void exp_rotation(celt_norm_t *X, int len, celt_word16_t theta, int dir, int stride, int iter)
47 /* c = cos(theta); s = dir*sin(theta); but we're approximating here for small theta */
48 c = Q15ONE-MULT16_16_Q15(QCONST16(.5f,15),MULT16_16_Q15(theta,theta));
52 /* We could use MULT16_16_P15 instead of MULT16_16_Q15 for more accuracy,
53 but at this point, I really don't think it's necessary */
54 for (i=0;i<len-stride;i++)
59 X[i] = MULT16_16_Q15(c,x1) - MULT16_16_Q15(s,x2);
60 X[i+stride] = MULT16_16_Q15(c,x2) + MULT16_16_Q15(s,x1);
62 for (i=len-2*stride-1;i>=0;i--)
67 X[i] = MULT16_16_Q15(c,x1) - MULT16_16_Q15(s,x2);
68 X[i+stride] = MULT16_16_Q15(c,x2) + MULT16_16_Q15(s,x1);
73 /* Normalise each band such that the energy is one. */
74 void normalise_bands(const CELTMode *m, const celt_sig_t *freq, celt_norm_t *X, celt_ener_t *bank)
77 const int *eBands = m->eBands;
82 for (i=0;i<m->nbEBands;i++)
87 for (j=B*eBands[i];j<B*eBands[i+1];j++)
88 sum += SIG_SCALING_1*SIG_SCALING_1*freq[j*C+c]*freq[j*C+c];
89 bank[i*C+c] = ENER_SCALING*sqrt(sum);
90 g = 1.f/(1e-10+ENER_SCALING_1*bank[i*C+c]*sqrt(C));
91 for (j=B*eBands[i];j<B*eBands[i+1];j++)
92 X[j*C+c] = NORM_SCALING*SIG_SCALING_1*freq[j*C+c]*g;
95 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)
100 void renormalise_bands(const CELTMode *m, celt_norm_t *X)
103 VARDECL(celt_ener_t *tmpE);
104 VARDECL(celt_sig_t *freq);
106 ALLOC(tmpE, m->nbEBands*m->nbChannels, celt_ener_t);
107 ALLOC(freq, m->nbMdctBlocks*m->nbChannels*m->eBands[m->nbEBands+1], celt_sig_t);
108 for (i=0;i<m->nbMdctBlocks*m->nbChannels*m->eBands[m->nbEBands+1];i++)
109 freq[i] = SHL32(EXTEND32(X[i]), 10);
110 normalise_bands(m, freq, X, tmpE);
114 void renormalise_bands(const CELTMode *m, celt_norm_t *X)
116 VARDECL(celt_ener_t *tmpE);
118 ALLOC(tmpE, m->nbEBands*m->nbChannels, celt_ener_t);
119 normalise_bands(m, X, X, tmpE);
124 /* De-normalise the energy to produce the synthesis from the unit-energy bands */
125 void denormalise_bands(const CELTMode *m, const celt_norm_t *X, celt_sig_t *freq, const celt_ener_t *bank)
128 const celt_word16_t sqrtC_1[2] = {QCONST16(1.f, 14), QCONST16(1.414214f, 14)};
129 const int *eBands = m->eBands;
133 celt_fatal("denormalise_bands() not implemented for >2 channels");
136 for (i=0;i<m->nbEBands;i++)
139 celt_word32_t g = MULT16_32_Q14(sqrtC_1[C-1],bank[i*C+c]);
140 for (j=B*eBands[i];j<B*eBands[i+1];j++)
141 freq[j*C+c] = MULT16_32_Q14(X[j*C+c], g);
144 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)
149 /* Compute the best gain for each "pitch band" */
150 void compute_pitch_gain(const CELTMode *m, const celt_norm_t *X, const celt_norm_t *P, celt_pgain_t *gains)
153 const int *pBands = m->pBands;
154 B = m->nbMdctBlocks*m->nbChannels;
156 for (i=0;i<m->nbPBands;i++)
158 celt_word32_t Sxy=0, Sxx=0;
160 /* We know we're not going to overflow because Sxx can't be more than 1 (Q28) */
161 for (j=B*pBands[i];j<B*pBands[i+1];j++)
163 Sxy = MAC16_16(Sxy, X[j], P[j]);
164 Sxx = MAC16_16(Sxx, X[j], X[j]);
166 /* No negative gain allowed */
169 /* Not sure how that would happen, just making sure */
172 /* We need to be a bit conservative (multiply gain by 0.9), otherwise the
173 residual doesn't quantise well */
174 Sxy = MULT16_32_Q15(QCONST16(.9f, 15), Sxy);
176 gains[i] = DIV32_16(Sxy,ADD32(SHR32(Sxx, PGAIN_SHIFT),EPSILON));
177 /*printf ("%f ", 1-sqrt(1-gain*gain));*/
181 for (i=0;i<m->nbPBands;i++)
182 printf ("%f ", 1-sqrt(1-gains[i]*gains[i]));
187 /* Apply the (quantised) gain to each "pitch band" */
188 void pitch_quant_bands(const CELTMode *m, celt_norm_t *P, const celt_pgain_t *gains)
191 const int *pBands = m->pBands;
192 B = m->nbMdctBlocks*m->nbChannels;
193 for (i=0;i<m->nbPBands;i++)
196 for (j=B*pBands[i];j<B*pBands[i+1];j++)
197 P[j] = MULT16_16_Q15(gains[i], P[j]);
198 /*printf ("%f ", gain);*/
200 for (i=B*pBands[m->nbPBands];i<B*pBands[m->nbPBands+1];i++)
205 /* Quantisation of the residual */
206 void quant_bands(const CELTMode *m, celt_norm_t *X, celt_norm_t *P, celt_mask_t *W, int total_bits, ec_enc *enc)
209 const int *eBands = m->eBands;
211 VARDECL(celt_norm_t *norm);
212 VARDECL(int *pulses);
213 VARDECL(int *offsets);
216 B = m->nbMdctBlocks*m->nbChannels;
218 ALLOC(norm, B*eBands[m->nbEBands+1], celt_norm_t);
219 ALLOC(pulses, m->nbEBands, int);
220 ALLOC(offsets, m->nbEBands, int);
222 for (i=0;i<m->nbEBands;i++)
224 /* Use a single-bit margin to guard against overrunning (make sure it's enough) */
225 bits = total_bits - ec_enc_tell(enc, 0) - 1;
226 compute_allocation(m, offsets, bits, pulses);
228 /*printf("bits left: %d\n", bits);
229 for (i=0;i<m->nbEBands;i++)
230 printf ("%d ", pulses[i]);
232 /*printf ("%d %d\n", ec_enc_tell(enc, 0), compute_allocation(m, m->nbPulses));*/
233 for (i=0;i<m->nbEBands;i++)
239 /*Scale factor of .0625f is just there to prevent overflows in fixed-point
240 (has no effect on float)*/
241 n = .0625f*sqrt(B*(eBands[i+1]-eBands[i]));
242 theta = Q15ONE*.007*(B*(eBands[i+1]-eBands[i]))/(.1f+q);
244 /* If pitch isn't available, use intra-frame prediction */
245 if (eBands[i] >= m->pitchEnd || q<=0)
250 intra_fold(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), norm, P+B*eBands[i], B, eBands[i], eBands[m->nbEBands+1]);
252 intra_prediction(X+B*eBands[i], W+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, norm, P+B*eBands[i], B, eBands[i], enc);
254 alpha = QCONST16(.7f,15);
259 exp_rotation(P+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
260 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
261 alg_quant(X+B*eBands[i], W+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], alpha, enc);
262 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, 1, B, 8);
264 for (j=B*eBands[i];j<B*eBands[i+1];j++)
267 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
272 /* Decoding of the residual */
273 void unquant_bands(const CELTMode *m, celt_norm_t *X, celt_norm_t *P, int total_bits, ec_dec *dec)
276 const int *eBands = m->eBands;
278 VARDECL(celt_norm_t *norm);
279 VARDECL(int *pulses);
280 VARDECL(int *offsets);
283 B = m->nbMdctBlocks*m->nbChannels;
285 ALLOC(norm, B*eBands[m->nbEBands+1], celt_norm_t);
286 ALLOC(pulses, m->nbEBands, int);
287 ALLOC(offsets, m->nbEBands, int);
289 for (i=0;i<m->nbEBands;i++)
291 /* Use a single-bit margin to guard against overrunning (make sure it's enough) */
292 bits = total_bits - ec_dec_tell(dec, 0) - 1;
293 compute_allocation(m, offsets, bits, pulses);
295 for (i=0;i<m->nbEBands;i++)
301 /*Scale factor of .0625f is just there to prevent overflows in fixed-point
302 (has no effect on float)*/
303 n = .0625f*sqrt(B*(eBands[i+1]-eBands[i]));
304 theta = Q15ONE*.007*(B*(eBands[i+1]-eBands[i]))/(.1f+q);
306 /* If pitch isn't available, use intra-frame prediction */
307 if (eBands[i] >= m->pitchEnd || q<=0)
312 intra_fold(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), norm, P+B*eBands[i], B, eBands[i], eBands[m->nbEBands+1]);
314 intra_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, norm, P+B*eBands[i], B, eBands[i], dec);
316 alpha = QCONST16(.7f,15);
321 exp_rotation(P+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
322 alg_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], alpha, dec);
323 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, 1, B, 8);
325 for (j=B*eBands[i];j<B*eBands[i+1];j++)
328 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
333 void stereo_mix(const CELTMode *m, celt_norm_t *X, const celt_ener_t *bank, int dir)
336 const int *eBands = m->eBands;
339 for (i=0;i<m->nbEBands;i++)
342 celt_ener_t left, right;
343 celt_word16_t a1, a2;
346 a1 = Q15ONE*1.f*left/sqrt(.01+left*1.f*left+right*1.f*right);
347 a2 = Q15ONE*1.f*dir*right/sqrt(.01+left*1.f*left+right*1.f*right);
348 for (j=B*eBands[i];j<B*eBands[i+1];j++)
353 X[j*C] = MULT16_16_Q15(a1,l) + MULT16_16_Q15(a2,r);
354 X[j*C+1] = MULT16_16_Q15(a1,r) - MULT16_16_Q15(a2,l);
357 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)