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.
39 /* Compute the energy in each of the bands */
40 void compute_band_energies(const CELTMode *m, float *X, float *bank)
43 const int *eBands = m->eBands;
45 for (i=0;i<m->nbEBands;i++)
49 for (j=B*eBands[i];j<B*eBands[i+1];j++)
51 bank[i] = sqrt(bank[i]);
55 /* Normalise each band such that the energy is one. */
56 void normalise_bands(const CELTMode *m, float *X, float *bank)
59 const int *eBands = m->eBands;
61 for (i=0;i<m->nbEBands;i++)
64 float x = 1.f/(1e-10+bank[i]);
65 for (j=B*eBands[i];j<B*eBands[i+1];j++)
68 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
72 /* De-normalise the energy to produce the synthesis from the unit-energy bands */
73 void denormalise_bands(const CELTMode *m, float *X, float *bank)
76 const int *eBands = m->eBands;
78 for (i=0;i<m->nbEBands;i++)
82 for (j=B*eBands[i];j<B*eBands[i+1];j++)
85 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
90 /* Compute the best gain for each "pitch band" */
91 void compute_pitch_gain(const CELTMode *m, float *X, float *P, float *gains, float *bank)
94 const int *eBands = m->eBands;
95 const int *pBands = m->pBands;
97 float w[B*eBands[m->nbEBands]];
98 for (i=0;i<m->nbEBands;i++)
101 for (j=B*eBands[i];j<B*eBands[i+1];j++)
106 for (i=0;i<m->nbPBands;i++)
112 for (j=B*pBands[i];j<B*pBands[i+1];j++)
114 Sxy += X[j]*P[j]*w[j];
115 Sxx += X[j]*X[j]*w[j];
117 gain = Sxy/(1e-10+Sxx);
118 //gain = Sxy/(2*(pbank[i+1]-pbank[i]));
120 //gain *= 1+.02*gain;
128 for (i=B*pBands[m->nbPBands];i<B*pBands[m->nbPBands+1];i++)
132 /* Apply the (quantised) gain to each "pitch band" */
133 void pitch_quant_bands(const CELTMode *m, float *X, float *P, float *gains)
136 const int *pBands = m->pBands;
138 for (i=0;i<m->nbPBands;i++)
141 for (j=B*pBands[i];j<B*pBands[i+1];j++)
143 //printf ("%f ", gain);
145 for (i=B*pBands[m->nbPBands];i<B*pBands[m->nbPBands+1];i++)
149 void quant_bands(const CELTMode *m, float *X, float *P, ec_enc *enc)
152 const int *eBands = m->eBands;
154 float norm[B*eBands[m->nbEBands+1]];
157 for (i=0;i<m->nbEBands;i++)
162 float n = sqrt(B*(eBands[i+1]-eBands[i]));
163 alg_quant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], enc);
164 for (j=B*eBands[i];j<B*eBands[i+1];j++)
166 //bits += log2(ncwrs(B*(eBands[i+1]-eBands[i]), q));
168 float n = sqrt(B*(eBands[i+1]-eBands[i]));
169 copy_quant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), -q, norm, B, eBands[i], enc);
170 for (j=B*eBands[i];j<B*eBands[i+1];j++)
172 //bits += 1+log2(eBands[i]-(eBands[i+1]-eBands[i]))+log2(ncwrs(B*(eBands[i+1]-eBands[i]), -q));
175 //printf ("%f\n", bits);
176 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
180 void unquant_bands(const CELTMode *m, float *X, float *P, ec_dec *dec)
183 const int *eBands = m->eBands;
185 float norm[B*eBands[m->nbEBands+1]];
187 for (i=0;i<m->nbEBands;i++)
192 float n = sqrt(B*(eBands[i+1]-eBands[i]));
193 alg_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], dec);
194 for (j=B*eBands[i];j<B*eBands[i+1];j++)
197 float n = sqrt(B*(eBands[i+1]-eBands[i]));
198 for (j=B*eBands[i];j<B*eBands[i+1];j++)
200 copy_unquant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), -q, norm, B, eBands[i], dec);
201 for (j=B*eBands[i];j<B*eBands[i+1];j++)
205 for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)