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;
96 B = m->nbMdctBlocks*m->nbChannels;
97 for (i=0;i<m->nbEBands;i++)
101 for (j=B*eBands[i];j<B*eBands[i+1];j++)
102 bank[i] += X[j]*X[j];
103 bank[i] = sqrt(bank[i]);
107 /* Normalise each band such that the energy is one. */
108 void normalise_bands(const CELTMode *m, float *X, float *bank)
111 const int *eBands = m->eBands;
112 B = m->nbMdctBlocks*m->nbChannels;
113 for (i=0;i<m->nbEBands;i++)
116 float x = 1.f/(1e-10+bank[i]);
117 for (j=B*eBands[i];j<B*eBands[i+1];j++)
120 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
124 /* De-normalise the energy to produce the synthesis from the unit-energy bands */
125 void denormalise_bands(const CELTMode *m, float *X, float *bank)
128 const int *eBands = m->eBands;
129 B = m->nbMdctBlocks*m->nbChannels;
130 for (i=0;i<m->nbEBands;i++)
134 for (j=B*eBands[i];j<B*eBands[i+1];j++)
137 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
142 /* Compute the best gain for each "pitch band" */
143 void compute_pitch_gain(const CELTMode *m, float *X, float *P, float *gains, float *bank)
146 const int *eBands = m->eBands;
147 const int *pBands = m->pBands;
148 B = m->nbMdctBlocks*m->nbChannels;
149 float w[B*eBands[m->nbEBands]];
150 for (i=0;i<m->nbEBands;i++)
153 for (j=B*eBands[i];j<B*eBands[i+1];j++)
158 for (i=0;i<m->nbPBands;i++)
164 for (j=B*pBands[i];j<B*pBands[i+1];j++)
166 Sxy += X[j]*P[j]*w[j];
167 Sxx += X[j]*X[j]*w[j];
169 gain = Sxy/(1e-10+Sxx);
170 //gain = Sxy/(2*(pbank[i+1]-pbank[i]));
172 //gain *= 1+.02*gain;
180 for (i=B*pBands[m->nbPBands];i<B*pBands[m->nbPBands+1];i++)
184 /* Apply the (quantised) gain to each "pitch band" */
185 void pitch_quant_bands(const CELTMode *m, float *X, float *P, float *gains)
188 const int *pBands = m->pBands;
189 B = m->nbMdctBlocks*m->nbChannels;
190 for (i=0;i<m->nbPBands;i++)
193 for (j=B*pBands[i];j<B*pBands[i+1];j++)
195 //printf ("%f ", gain);
197 for (i=B*pBands[m->nbPBands];i<B*pBands[m->nbPBands+1];i++)
201 /* Quantisation of the residual */
202 void quant_bands(const CELTMode *m, float *X, float *P, float *W, ec_enc *enc)
205 const int *eBands = m->eBands;
206 B = m->nbMdctBlocks*m->nbChannels;
207 float norm[B*eBands[m->nbEBands+1]];
209 for (i=0;i<m->nbEBands;i++)
214 n = sqrt(B*(eBands[i+1]-eBands[i]));
215 theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+abs(m->nbPulses[i]));
219 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);
224 exp_rotation(P+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
225 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
226 alg_quant(X+B*eBands[i], W+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], 0.7, enc);
227 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, 1, B, 8);
229 for (j=B*eBands[i];j<B*eBands[i+1];j++)
231 //printf ("%f ", log2(ncwrs64(B*(eBands[i+1]-eBands[i]), q))/(B*(eBands[i+1]-eBands[i])));
232 //printf ("%f ", log2(ncwrs64(B*(eBands[i+1]-eBands[i]), q)));
235 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
239 /* Decoding of the residual */
240 void unquant_bands(const CELTMode *m, float *X, float *P, ec_dec *dec)
243 const int *eBands = m->eBands;
244 B = m->nbMdctBlocks*m->nbChannels;
245 float norm[B*eBands[m->nbEBands+1]];
247 for (i=0;i<m->nbEBands;i++)
252 n = sqrt(B*(eBands[i+1]-eBands[i]));
253 theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+abs(m->nbPulses[i]));
257 intra_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, norm, P+B*eBands[i], B, eBands[i], dec);
262 exp_rotation(P+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
263 alg_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], 0.7, dec);
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++)