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++)
231 /* Quantisation of the residual */
232 void quant_bands(const CELTMode *m, float *X, float *P, float *W, struct alloc_data *alloc, int total_bits, ec_enc *enc)
235 const int *eBands = m->eBands;
236 B = m->nbMdctBlocks*m->nbChannels;
237 float norm[B*eBands[m->nbEBands+1]];
238 int pulses[m->nbEBands];
239 int offsets[m->nbEBands];
241 for (i=0;i<m->nbEBands;i++)
243 /* Use a single-bit margin to guard against overrunning (make sure it's enough) */
244 bits = total_bits - ec_enc_tell(enc, 0) - 1;
245 compute_allocation(alloc, offsets, bits, pulses);
247 /*printf("bits left: %d\n", bits);
248 for (i=0;i<m->nbEBands;i++)
249 printf ("%d ", pulses[i]);
251 /*printf ("%d %d\n", ec_enc_tell(enc, 0), compute_allocation(m, m->nbPulses));*/
252 for (i=0;i<m->nbEBands;i++)
257 //q = m->nbPulses[i];
258 n = sqrt(B*(eBands[i+1]-eBands[i]));
259 theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+abs(q));
263 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);
268 exp_rotation(P+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
269 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
270 alg_quant(X+B*eBands[i], W+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], 0.7, enc);
271 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, 1, B, 8);
273 for (j=B*eBands[i];j<B*eBands[i+1];j++)
275 //printf ("%f ", log2(ncwrs64(B*(eBands[i+1]-eBands[i]), q))/(B*(eBands[i+1]-eBands[i])));
276 //printf ("%f ", log2(ncwrs64(B*(eBands[i+1]-eBands[i]), q)));
279 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
283 /* Decoding of the residual */
284 void unquant_bands(const CELTMode *m, float *X, float *P, struct alloc_data *alloc, int total_bits, ec_dec *dec)
287 const int *eBands = m->eBands;
288 B = m->nbMdctBlocks*m->nbChannels;
289 float norm[B*eBands[m->nbEBands+1]];
290 int pulses[m->nbEBands];
291 int offsets[m->nbEBands];
293 for (i=0;i<m->nbEBands;i++)
295 /* Use a single-bit margin to guard against overrunning (make sure it's enough) */
296 bits = total_bits - ec_dec_tell(dec, 0) - 1;
297 compute_allocation(alloc, offsets, bits, pulses);
299 for (i=0;i<m->nbEBands;i++)
304 //q = m->nbPulses[i];
305 n = sqrt(B*(eBands[i+1]-eBands[i]));
306 theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+abs(q));
310 intra_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, norm, P+B*eBands[i], B, eBands[i], dec);
315 exp_rotation(P+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, -1, B, 8);
316 alg_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], 0.7, dec);
317 exp_rotation(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), theta, 1, B, 8);
319 for (j=B*eBands[i];j<B*eBands[i+1];j++)
322 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
326 void stereo_mix(const CELTMode *m, float *X, float *bank, int dir)
329 const int *eBands = m->eBands;
332 for (i=0;i<m->nbEBands;i++)
339 a1 = left/sqrt(.01+left*left+right*right);
340 a2 = dir*right/sqrt(.01+left*left+right*right);
341 for (j=B*eBands[i];j<B*eBands[i+1];j++)
346 X[j*C] = a1*l + a2*r;
347 X[j*C+1] = a1*r - a2*l;
350 for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)