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"
44 void exp_rotation(celt_norm_t *X, int len, celt_word16_t theta, int dir, int stride, int iter)
48 /* c = cos(theta); s = dir*sin(theta); but we're approximating here for small theta */
49 c = Q15ONE-MULT16_16_Q15(QCONST16(.5f,15),MULT16_16_Q15(theta,theta));
53 /* We could use MULT16_16_P15 instead of MULT16_16_Q15 for more accuracy,
54 but at this point, I really don't think it's necessary */
55 for (i=0;i<len-stride;i++)
60 X[i] = MULT16_16_Q15(c,x1) - MULT16_16_Q15(s,x2);
61 X[i+stride] = MULT16_16_Q15(c,x2) + MULT16_16_Q15(s,x1);
63 for (i=len-2*stride-1;i>=0;i--)
68 X[i] = MULT16_16_Q15(c,x1) - MULT16_16_Q15(s,x2);
69 X[i+stride] = MULT16_16_Q15(c,x2) + MULT16_16_Q15(s,x1);
74 /* Normalise each band such that the energy is one. */
75 void normalise_bands(const CELTMode *m, const celt_sig_t *freq, celt_norm_t *X, celt_ener_t *bank)
78 const int *eBands = m->eBands;
83 for (i=0;i<m->nbEBands;i++)
88 for (j=B*eBands[i];j<B*eBands[i+1];j++)
89 sum += SIG_SCALING_1*SIG_SCALING_1*freq[j*C+c]*freq[j*C+c];
90 bank[i*C+c] = ENER_SCALING*sqrt(sum);
91 g = 1.f/(1e-10+ENER_SCALING_1*bank[i*C+c]*sqrt(C));
92 for (j=B*eBands[i];j<B*eBands[i+1];j++)
93 X[j*C+c] = NORM_SCALING*SIG_SCALING_1*freq[j*C+c]*g;
96 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)
101 void renormalise_bands(const CELTMode *m, celt_norm_t *X)
104 VARDECL(celt_ener_t *tmpE);
105 VARDECL(celt_sig_t *freq);
107 ALLOC(tmpE, m->nbEBands*m->nbChannels, celt_ener_t);
108 ALLOC(freq, m->nbMdctBlocks*m->nbChannels*m->eBands[m->nbEBands+1], celt_sig_t);
109 for (i=0;i<m->nbMdctBlocks*m->nbChannels*m->eBands[m->nbEBands+1];i++)
110 freq[i] = SHL32(EXTEND32(X[i]), 10);
111 normalise_bands(m, freq, X, tmpE);
115 void renormalise_bands(const CELTMode *m, celt_norm_t *X)
117 VARDECL(celt_ener_t *tmpE);
119 ALLOC(tmpE, m->nbEBands*m->nbChannels, celt_ener_t);
120 normalise_bands(m, X, X, tmpE);
125 /* De-normalise the energy to produce the synthesis from the unit-energy bands */
126 void denormalise_bands(const CELTMode *m, const celt_norm_t *X, celt_sig_t *freq, const celt_ener_t *bank)
129 const celt_word16_t sqrtC_1[2] = {QCONST16(1.f, 14), QCONST16(1.414214f, 14)};
130 const int *eBands = m->eBands;
134 celt_fatal("denormalise_bands() not implemented for >2 channels");
137 for (i=0;i<m->nbEBands;i++)
140 celt_word32_t g = MULT16_32_Q14(sqrtC_1[C-1],bank[i*C+c]);
141 for (j=B*eBands[i];j<B*eBands[i+1];j++)
142 freq[j*C+c] = MULT16_32_Q14(X[j*C+c], g);
145 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)
150 /* Compute the best gain for each "pitch band" */
151 void compute_pitch_gain(const CELTMode *m, const celt_norm_t *X, const celt_norm_t *P, celt_pgain_t *gains)
154 const int *pBands = m->pBands;
155 B = m->nbMdctBlocks*m->nbChannels;
157 for (i=0;i<m->nbPBands;i++)
159 celt_word32_t Sxy=0, Sxx=0;
161 /* We know we're not going to overflow because Sxx can't be more than 1 (Q28) */
162 for (j=B*pBands[i];j<B*pBands[i+1];j++)
164 Sxy = MAC16_16(Sxy, X[j], P[j]);
165 Sxx = MAC16_16(Sxx, X[j], X[j]);
167 /* No negative gain allowed */
170 /* Not sure how that would happen, just making sure */
173 /* We need to be a bit conservative (multiply gain by 0.9), otherwise the
174 residual doesn't quantise well */
175 Sxy = MULT16_32_Q15(QCONST16(.9f, 15), Sxy);
177 gains[i] = DIV32_16(Sxy,ADD32(SHR32(Sxx, PGAIN_SHIFT),EPSILON));
178 /*printf ("%f ", 1-sqrt(1-gain*gain));*/
182 for (i=0;i<m->nbPBands;i++)
183 printf ("%f ", 1-sqrt(1-gains[i]*gains[i]));
188 /* Apply the (quantised) gain to each "pitch band" */
189 void pitch_quant_bands(const CELTMode *m, celt_norm_t *P, const celt_pgain_t *gains)
192 const int *pBands = m->pBands;
193 B = m->nbMdctBlocks*m->nbChannels;
194 for (i=0;i<m->nbPBands;i++)
197 for (j=B*pBands[i];j<B*pBands[i+1];j++)
198 P[j] = MULT16_16_Q15(gains[i], P[j]);
199 /*printf ("%f ", gain);*/
201 for (i=B*pBands[m->nbPBands];i<B*pBands[m->nbPBands+1];i++)
206 /* Quantisation of the residual */
207 void quant_bands(const CELTMode *m, celt_norm_t *X, celt_norm_t *P, celt_mask_t *W, int total_bits, ec_enc *enc)
210 const int *eBands = m->eBands;
212 VARDECL(celt_norm_t *norm);
213 VARDECL(int *pulses);
214 VARDECL(int *offsets);
217 B = m->nbMdctBlocks*m->nbChannels;
219 ALLOC(norm, B*eBands[m->nbEBands+1], celt_norm_t);
220 ALLOC(pulses, m->nbEBands, int);
221 ALLOC(offsets, m->nbEBands, int);
223 for (i=0;i<m->nbEBands;i++)
225 /* Use a single-bit margin to guard against overrunning (make sure it's enough) */
226 bits = total_bits - ec_enc_tell(enc, 0) - 1;
227 compute_allocation(m, offsets, bits, pulses);
229 /*printf("bits left: %d\n", bits);
230 for (i=0;i<m->nbEBands;i++)
231 printf ("%d ", pulses[i]);
233 /*printf ("%d %d\n", ec_enc_tell(enc, 0), compute_allocation(m, m->nbPulses));*/
234 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 = SHL16(celt_sqrt(B*(eBands[i+1]-eBands[i])),11);
243 /* If pitch isn't available, use intra-frame prediction */
244 if (eBands[i] >= m->pitchEnd || q<=0)
249 intra_fold(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), norm, P+B*eBands[i], B, eBands[i], eBands[m->nbEBands+1]);
251 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);
253 alpha = QCONST16(.7f,15);
258 celt_word16_t theta = DIV32_16(MULT16_16_16(QCONST16(.007f,15),B*(eBands[i+1]-eBands[i])),q);
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++)
265 norm[j] = MULT16_16_Q15(n,X[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++)
300 /*Scale factor of .0625f is just there to prevent overflows in fixed-point
301 (has no effect on float)*/
302 n = SHL16(celt_sqrt(B*(eBands[i+1]-eBands[i])),11);
304 /* If pitch isn't available, use intra-frame prediction */
305 if (eBands[i] >= m->pitchEnd || q<=0)
310 intra_fold(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), norm, P+B*eBands[i], B, eBands[i], eBands[m->nbEBands+1]);
312 intra_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, norm, P+B*eBands[i], B, eBands[i], dec);
314 alpha = QCONST16(.7f,15);
319 celt_word16_t theta = DIV32_16(MULT16_16_16(QCONST16(.007f,15),B*(eBands[i+1]-eBands[i])),q);
320 exp_rotation(P+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
321 alg_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], alpha, dec);
322 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, 1, B, 8);
324 for (j=B*eBands[i];j<B*eBands[i+1];j++)
325 norm[j] = MULT16_16_Q15(n,X[j]);
327 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
332 void stereo_mix(const CELTMode *m, celt_norm_t *X, const celt_ener_t *bank, int dir)
335 const int *eBands = m->eBands;
338 for (i=0;i<m->nbEBands;i++)
341 celt_ener_t left, right;
342 celt_word16_t a1, a2;
345 a1 = Q15ONE*1.f*left/sqrt(.01+left*1.f*left+right*1.f*right);
346 a2 = Q15ONE*1.f*dir*right/sqrt(.01+left*1.f*left+right*1.f*right);
347 for (j=B*eBands[i];j<B*eBands[i+1];j++)
352 X[j*C] = MULT16_16_Q15(a1,l) + MULT16_16_Q15(a2,r);
353 X[j*C+1] = MULT16_16_Q15(a1,r) - MULT16_16_Q15(a2,l);
356 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)