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)
49 for (i=0;i<len-stride;i++)
55 X[i+stride] = c*x2 + s*x1;
57 for (i=len-2*stride-1;i>=0;i--)
63 X[i+stride] = c*x2 + s*x1;
68 /* Compute the amplitude (sqrt energy) in each of the bands */
69 void compute_band_energies(const CELTMode *m, float *X, float *bank)
72 const int *eBands = m->eBands;
77 for (i=0;i<m->nbEBands;i++)
81 for (j=B*eBands[i];j<B*eBands[i+1];j++)
82 sum += X[j*C+c]*X[j*C+c];
83 bank[i*C+c] = sqrt(C*sum);
84 //printf ("%f ", bank[i*C+c]);
90 /* Normalise each band such that the energy is one. */
91 void normalise_bands(const CELTMode *m, float *X, float *bank)
94 const int *eBands = m->eBands;
99 for (i=0;i<m->nbEBands;i++)
102 float g = 1.f/(1e-10+bank[i*C+c]);
103 for (j=B*eBands[i];j<B*eBands[i+1];j++)
107 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)
111 void renormalise_bands(const CELTMode *m, float *X)
113 float tmpE[m->nbEBands*m->nbChannels];
114 compute_band_energies(m, X, tmpE);
115 normalise_bands(m, X, tmpE);
118 /* De-normalise the energy to produce the synthesis from the unit-energy bands */
119 void denormalise_bands(const CELTMode *m, float *X, float *bank)
122 const int *eBands = m->eBands;
127 for (i=0;i<m->nbEBands;i++)
130 float g = bank[i*C+c];
131 for (j=B*eBands[i];j<B*eBands[i+1];j++)
135 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)
140 /* Compute the best gain for each "pitch band" */
141 void compute_pitch_gain(const CELTMode *m, float *X, float *P, float *gains, float *bank)
144 const int *eBands = m->eBands;
145 const int *pBands = m->pBands;
146 B = m->nbMdctBlocks*m->nbChannels;
147 float w[B*eBands[m->nbEBands]];
148 for (i=0;i<m->nbEBands;i++)
151 for (j=B*eBands[i];j<B*eBands[i+1];j++)
156 for (i=0;i<m->nbPBands;i++)
162 for (j=B*pBands[i];j<B*pBands[i+1];j++)
164 Sxy += X[j]*P[j]*w[j];
165 Sxx += X[j]*X[j]*w[j];
167 gain = Sxy/(1e-10+Sxx);
168 //gain = Sxy/(2*(pbank[i+1]-pbank[i]));
170 //gain *= 1+.02*gain;
175 /* We need to be a bit conservative, otherwise residual doesn't quantise well */
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]));
186 for (i=B*pBands[m->nbPBands];i<B*pBands[m->nbPBands+1];i++)
190 /* Apply the (quantised) gain to each "pitch band" */
191 void pitch_quant_bands(const CELTMode *m, float *X, float *P, float *gains)
194 const int *pBands = m->pBands;
195 B = m->nbMdctBlocks*m->nbChannels;
196 for (i=0;i<m->nbPBands;i++)
199 for (j=B*pBands[i];j<B*pBands[i+1];j++)
201 //printf ("%f ", gain);
203 for (i=B*pBands[m->nbPBands];i<B*pBands[m->nbPBands+1];i++)
208 /* Quantisation of the residual */
209 void quant_bands(const CELTMode *m, float *X, float *P, float *W, int total_bits, ec_enc *enc)
212 const int *eBands = m->eBands;
213 B = m->nbMdctBlocks*m->nbChannels;
214 float norm[B*eBands[m->nbEBands+1]];
215 int pulses[m->nbEBands];
216 int offsets[m->nbEBands];
219 for (i=0;i<m->nbEBands;i++)
221 /* Use a single-bit margin to guard against overrunning (make sure it's enough) */
222 bits = total_bits - ec_enc_tell(enc, 0) - 1;
223 compute_allocation(m, offsets, bits, pulses);
225 /*printf("bits left: %d\n", bits);
226 for (i=0;i<m->nbEBands;i++)
227 printf ("%d ", pulses[i]);
229 /*printf ("%d %d\n", ec_enc_tell(enc, 0), compute_allocation(m, m->nbPulses));*/
230 for (i=0;i<m->nbEBands;i++)
235 //q = m->nbPulses[i];
236 n = sqrt(B*(eBands[i+1]-eBands[i]));
237 theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+q);
239 /* If pitch isn't available, use intra-frame prediction */
240 if (eBands[i] >= m->pitchEnd || q<=0)
245 intra_fold(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), norm, P+B*eBands[i], B, eBands[i], eBands[m->nbEBands+1]);
247 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 exp_rotation(P+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
255 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
256 alg_quant(X+B*eBands[i], W+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], alpha, enc);
257 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, 1, B, 8);
259 for (j=B*eBands[i];j<B*eBands[i+1];j++)
261 //printf ("%f ", log2(ncwrs64(B*(eBands[i+1]-eBands[i]), q))/(B*(eBands[i+1]-eBands[i])));
262 //printf ("%f ", log2(ncwrs64(B*(eBands[i+1]-eBands[i]), q)));
265 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
269 /* Decoding of the residual */
270 void unquant_bands(const CELTMode *m, float *X, float *P, int total_bits, ec_dec *dec)
273 const int *eBands = m->eBands;
274 B = m->nbMdctBlocks*m->nbChannels;
275 float norm[B*eBands[m->nbEBands+1]];
276 int pulses[m->nbEBands];
277 int offsets[m->nbEBands];
280 for (i=0;i<m->nbEBands;i++)
282 /* Use a single-bit margin to guard against overrunning (make sure it's enough) */
283 bits = total_bits - ec_dec_tell(dec, 0) - 1;
284 compute_allocation(m, offsets, bits, pulses);
286 for (i=0;i<m->nbEBands;i++)
291 //q = m->nbPulses[i];
292 n = sqrt(B*(eBands[i+1]-eBands[i]));
293 theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+q);
295 /* If pitch isn't available, use intra-frame prediction */
296 if (eBands[i] >= m->pitchEnd || q<=0)
301 intra_fold(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), norm, P+B*eBands[i], B, eBands[i], eBands[m->nbEBands+1]);
303 intra_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, norm, P+B*eBands[i], B, eBands[i], dec);
310 exp_rotation(P+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
311 alg_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], alpha, dec);
312 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, 1, B, 8);
314 for (j=B*eBands[i];j<B*eBands[i+1];j++)
317 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
321 void stereo_mix(const CELTMode *m, float *X, float *bank, int dir)
324 const int *eBands = m->eBands;
327 for (i=0;i<m->nbEBands;i++)
334 a1 = left/sqrt(.01+left*left+right*right);
335 a2 = dir*right/sqrt(.01+left*left+right*right);
336 for (j=B*eBands[i];j<B*eBands[i+1];j++)
341 X[j*C] = a1*l + a2*r;
342 X[j*C+1] = a1*r - a2*l;
345 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)