1 /* (C) 2007 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.
42 /* Applies a series of rotations so that pulses are spread like a two-sided
43 exponential. The effect of this is to reduce the tonal noise created by the
44 sparse spectrum resulting from the pulse codebook */
45 static void exp_rotation(float *X, int len, float theta, int dir, int stride, int iter)
53 for (i=0;i<len-stride;i++)
59 X[i+stride] = c*x2 + s*x1;
61 for (i=len-2*stride-1;i>=0;i--)
67 X[i+stride] = c*x2 + s*x1;
72 /* Compute the amplitude (sqrt energy) in each of the bands */
73 void compute_band_energies(const CELTMode *m, float *X, float *bank)
76 const int *eBands = m->eBands;
81 for (i=0;i<m->nbEBands;i++)
85 for (j=B*eBands[i];j<B*eBands[i+1];j++)
86 sum += X[j*C+c]*X[j*C+c];
87 bank[i*C+c] = sqrt(C*sum);
88 /*printf ("%f ", bank[i*C+c]);*/
94 /* Normalise each band such that the energy is one. */
95 void normalise_bands(const CELTMode *m, float *X, float *bank)
98 const int *eBands = m->eBands;
103 for (i=0;i<m->nbEBands;i++)
106 float g = 1.f/(1e-10+bank[i*C+c]);
107 for (j=B*eBands[i];j<B*eBands[i+1];j++)
111 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)
115 void renormalise_bands(const CELTMode *m, float *X)
117 VARDECL(float *tmpE);
118 ALLOC(tmpE, m->nbEBands*m->nbChannels, float);
119 compute_band_energies(m, X, tmpE);
120 normalise_bands(m, X, tmpE);
123 /* De-normalise the energy to produce the synthesis from the unit-energy bands */
124 void denormalise_bands(const CELTMode *m, float *X, float *bank)
127 const int *eBands = m->eBands;
132 for (i=0;i<m->nbEBands;i++)
135 float g = bank[i*C+c];
136 for (j=B*eBands[i];j<B*eBands[i+1];j++)
140 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)
145 /* Compute the best gain for each "pitch band" */
146 void compute_pitch_gain(const CELTMode *m, float *X, float *P, float *gains, float *bank)
149 const int *eBands = m->eBands;
150 const int *pBands = m->pBands;
152 B = m->nbMdctBlocks*m->nbChannels;
153 ALLOC(w, B*eBands[m->nbEBands], float);
154 for (i=0;i<m->nbEBands;i++)
157 for (j=B*eBands[i];j<B*eBands[i+1];j++)
162 for (i=0;i<m->nbPBands;i++)
168 for (j=B*pBands[i];j<B*pBands[i+1];j++)
170 Sxy += X[j]*P[j]*w[j];
171 Sxx += X[j]*X[j]*w[j];
173 gain = Sxy/(1e-10+Sxx);
178 /* We need to be a bit conservative, otherwise residual doesn't quantise well */
181 /*printf ("%f ", 1-sqrt(1-gain*gain));*/
185 for (i=0;i<m->nbPBands;i++)
186 printf ("%f ", 1-sqrt(1-gains[i]*gains[i]));
189 for (i=B*pBands[m->nbPBands];i<B*pBands[m->nbPBands+1];i++)
193 /* Apply the (quantised) gain to each "pitch band" */
194 void pitch_quant_bands(const CELTMode *m, float *X, float *P, float *gains)
197 const int *pBands = m->pBands;
198 B = m->nbMdctBlocks*m->nbChannels;
199 for (i=0;i<m->nbPBands;i++)
202 for (j=B*pBands[i];j<B*pBands[i+1];j++)
204 /*printf ("%f ", gain);*/
206 for (i=B*pBands[m->nbPBands];i<B*pBands[m->nbPBands+1];i++)
211 /* Quantisation of the residual */
212 void quant_bands(const CELTMode *m, float *X, float *P, float *W, int total_bits, ec_enc *enc)
215 const int *eBands = m->eBands;
217 VARDECL(float *norm);
218 VARDECL(int *pulses);
219 VARDECL(int *offsets);
221 B = m->nbMdctBlocks*m->nbChannels;
223 ALLOC(norm, B*eBands[m->nbEBands+1], float);
224 ALLOC(pulses, m->nbEBands, int);
225 ALLOC(offsets, m->nbEBands, int);
227 for (i=0;i<m->nbEBands;i++)
229 /* Use a single-bit margin to guard against overrunning (make sure it's enough) */
230 bits = total_bits - ec_enc_tell(enc, 0) - 1;
231 compute_allocation(m, offsets, bits, pulses);
233 /*printf("bits left: %d\n", bits);
234 for (i=0;i<m->nbEBands;i++)
235 printf ("%d ", pulses[i]);
237 /*printf ("%d %d\n", ec_enc_tell(enc, 0), compute_allocation(m, m->nbPulses));*/
238 for (i=0;i<m->nbEBands;i++)
243 n = sqrt(B*(eBands[i+1]-eBands[i]));
244 theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+q);
246 /* If pitch isn't available, use intra-frame prediction */
247 if (eBands[i] >= m->pitchEnd || q<=0)
252 intra_fold(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), norm, P+B*eBands[i], B, eBands[i], eBands[m->nbEBands+1]);
254 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);
261 exp_rotation(P+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
262 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
263 alg_quant(X+B*eBands[i], W+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], alpha, enc);
264 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, 1, B, 8);
266 for (j=B*eBands[i];j<B*eBands[i+1];j++)
269 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
273 /* Decoding of the residual */
274 void unquant_bands(const CELTMode *m, float *X, float *P, int total_bits, ec_dec *dec)
277 const int *eBands = m->eBands;
279 VARDECL(float *norm);
280 VARDECL(int *pulses);
281 VARDECL(int *offsets);
283 B = m->nbMdctBlocks*m->nbChannels;
285 ALLOC(norm, B*eBands[m->nbEBands+1], float);
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 n = sqrt(B*(eBands[i+1]-eBands[i]));
301 theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+q);
303 /* If pitch isn't available, use intra-frame prediction */
304 if (eBands[i] >= m->pitchEnd || q<=0)
309 intra_fold(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), norm, P+B*eBands[i], B, eBands[i], eBands[m->nbEBands+1]);
311 intra_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, norm, P+B*eBands[i], B, eBands[i], dec);
318 exp_rotation(P+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
319 alg_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], alpha, dec);
320 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, 1, B, 8);
322 for (j=B*eBands[i];j<B*eBands[i+1];j++)
325 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
329 void stereo_mix(const CELTMode *m, float *X, float *bank, int dir)
332 const int *eBands = m->eBands;
335 for (i=0;i<m->nbEBands;i++)
342 a1 = left/sqrt(.01+left*left+right*right);
343 a2 = dir*right/sqrt(.01+left*left+right*right);
344 for (j=B*eBands[i];j<B*eBands[i+1];j++)
349 X[j*C] = a1*l + a2*r;
350 X[j*C+1] = a1*r - a2*l;
353 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)