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.
38 /* Applies a series of rotations so that pulses are spread like a two-sided
39 exponential. The effect of this is to reduce the tonal noise created by the
40 sparse spectrum resulting from the pulse codebook */
41 static void exp_rotation(float *X, int len, float theta, int dir, int stride, int iter)
51 for (i=0;i<len-stride;i++)
57 X[i+stride] = c*x2 + s*x1;
59 for (i=len-2*stride-1;i>=0;i--)
65 X[i+stride] = c*x2 + s*x1;
71 for (i=0;i<len-2*stride;i++)
77 X[i+stride] = c*x2 - s*x1;
79 for (i=len-stride-1;i>=0;i--)
85 X[i+stride] = c*x2 - s*x1;
91 /* Compute the amplitude (sqrt energy) in each of the bands */
92 void compute_band_energies(const CELTMode *m, float *X, float *bank)
95 const int *eBands = m->eBands;
100 for (i=0;i<m->nbEBands;i++)
104 for (j=B*eBands[i];j<B*eBands[i+1];j++)
105 sum += X[j*C+c]*X[j*C+c];
106 bank[i*C+c] = sqrt(C*sum);
107 //printf ("%f ", bank[i*C+c]);
113 /* Normalise each band such that the energy is one. */
114 void normalise_bands(const CELTMode *m, float *X, float *bank)
117 const int *eBands = m->eBands;
122 for (i=0;i<m->nbEBands;i++)
125 float g = 1.f/(1e-10+bank[i*C+c]);
126 for (j=B*eBands[i];j<B*eBands[i+1];j++)
130 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)
134 void renormalise_bands(const CELTMode *m, float *X)
136 float tmpE[m->nbEBands*m->nbChannels];
137 compute_band_energies(m, X, tmpE);
138 normalise_bands(m, X, tmpE);
141 /* De-normalise the energy to produce the synthesis from the unit-energy bands */
142 void denormalise_bands(const CELTMode *m, float *X, float *bank)
145 const int *eBands = m->eBands;
150 for (i=0;i<m->nbEBands;i++)
153 float g = bank[i*C+c];
154 for (j=B*eBands[i];j<B*eBands[i+1];j++)
158 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)
163 /* Compute the best gain for each "pitch band" */
164 void compute_pitch_gain(const CELTMode *m, float *X, float *P, float *gains, float *bank)
167 const int *eBands = m->eBands;
168 const int *pBands = m->pBands;
169 B = m->nbMdctBlocks*m->nbChannels;
170 float w[B*eBands[m->nbEBands]];
171 for (i=0;i<m->nbEBands;i++)
174 for (j=B*eBands[i];j<B*eBands[i+1];j++)
179 for (i=0;i<m->nbPBands;i++)
185 for (j=B*pBands[i];j<B*pBands[i+1];j++)
187 Sxy += X[j]*P[j]*w[j];
188 Sxx += X[j]*X[j]*w[j];
190 gain = Sxy/(1e-10+Sxx);
191 //gain = Sxy/(2*(pbank[i+1]-pbank[i]));
193 //gain *= 1+.02*gain;
198 /* We need to be a bit conservative, otherwise residual doesn't quantise well */
201 //printf ("%f ", 1-sqrt(1-gain*gain));
205 for (i=0;i<m->nbPBands;i++)
206 printf ("%f ", 1-sqrt(1-gains[i]*gains[i]));
209 for (i=B*pBands[m->nbPBands];i<B*pBands[m->nbPBands+1];i++)
213 /* Apply the (quantised) gain to each "pitch band" */
214 void pitch_quant_bands(const CELTMode *m, float *X, float *P, float *gains)
217 const int *pBands = m->pBands;
218 B = m->nbMdctBlocks*m->nbChannels;
219 for (i=0;i<m->nbPBands;i++)
222 for (j=B*pBands[i];j<B*pBands[i+1];j++)
224 //printf ("%f ", gain);
226 for (i=B*pBands[m->nbPBands];i<B*pBands[m->nbPBands+1];i++)
230 static int compute_allocation(const CELTMode *m, int *pulses)
233 const int *eBands = m->eBands;
234 BC = m->nbMdctBlocks*m->nbChannels;
236 for (i=0;i<m->nbEBands;i++)
239 N = BC*(eBands[i+1]-eBands[i]);
243 bits += ec_ilog64(eBands[i] - (eBands[i+1]-eBands[i])) + 1;
247 bits += ec_ilog64(ncwrs64(N, pulses[i])) + 1;
252 /* Quantisation of the residual */
253 void quant_bands(const CELTMode *m, float *X, float *P, float *W, ec_enc *enc)
256 const int *eBands = m->eBands;
257 B = m->nbMdctBlocks*m->nbChannels;
258 float norm[B*eBands[m->nbEBands+1]];
260 /*printf ("%d %d\n", ec_enc_tell(enc, 0), compute_allocation(m, m->nbPulses));*/
261 for (i=0;i<m->nbEBands;i++)
266 n = sqrt(B*(eBands[i+1]-eBands[i]));
267 theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+abs(m->nbPulses[i]));
271 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);
276 exp_rotation(P+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
277 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
278 alg_quant(X+B*eBands[i], W+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], 0.7, enc);
279 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, 1, B, 8);
281 for (j=B*eBands[i];j<B*eBands[i+1];j++)
283 //printf ("%f ", log2(ncwrs64(B*(eBands[i+1]-eBands[i]), q))/(B*(eBands[i+1]-eBands[i])));
284 //printf ("%f ", log2(ncwrs64(B*(eBands[i+1]-eBands[i]), q)));
287 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
291 /* Decoding of the residual */
292 void unquant_bands(const CELTMode *m, float *X, float *P, ec_dec *dec)
295 const int *eBands = m->eBands;
296 B = m->nbMdctBlocks*m->nbChannels;
297 float norm[B*eBands[m->nbEBands+1]];
299 for (i=0;i<m->nbEBands;i++)
304 n = sqrt(B*(eBands[i+1]-eBands[i]));
305 theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+abs(m->nbPulses[i]));
309 intra_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, norm, P+B*eBands[i], B, eBands[i], dec);
314 exp_rotation(P+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
315 alg_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], 0.7, dec);
316 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, 1, B, 8);
318 for (j=B*eBands[i];j<B*eBands[i+1];j++)
321 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
325 void stereo_mix(const CELTMode *m, float *X, float *bank, int dir)
328 const int *eBands = m->eBands;
331 for (i=0;i<m->nbEBands;i++)
338 a1 = left/sqrt(.01+left*left+right*right);
339 a2 = dir*right/sqrt(.01+left*left+right*right);
340 for (j=B*eBands[i];j<B*eBands[i+1];j++)
345 X[j*C] = a1*l + a2*r;
346 X[j*C+1] = a1*r - a2*l;
349 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)