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.
36 #include "quant_bands.h"
39 #include "os_support.h"
44 const celt_word16_t eMeans[24] = {11520, -2048, -3072, -640, 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
46 const celt_word16_t eMeans[24] = {45.f, -8.f, -12.f, -2.5f, 1.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
49 /*const int frac[24] = {4, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};*/
50 const int frac[24] = {8, 6, 5, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
53 static inline celt_ener_t dB2Amp(celt_ener_t dB)
56 amp = PSHR32(celt_exp2(MULT16_16_Q14(21771,dB)),2)-QCONST16(.3f, 14);
63 static inline celt_word16_t amp2dB(celt_ener_t amp)
65 /* equivalent to return 6.0207*log2(.3+amp) */
66 return ROUND(MULT16_16(24661,celt_log2(ADD32(QCONST32(.3f,14),amp))),12);
67 /* return DB_SCALING*20*log10(.3+ENER_SCALING_1*amp); */
70 static inline celt_ener_t dB2Amp(celt_ener_t dB)
73 amp = pow(10, .05*dB)-.3;
78 static inline celt_word16_t amp2dB(celt_ener_t amp)
80 return 20*log10(.3+amp);
84 static const celt_word16_t base_resolution = QCONST16(6.f,8);
86 static void quant_energy_mono(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, int budget, ec_enc *enc)
90 celt_word16_t prev = 0;
91 float coef = m->ePredCoef;
92 VARDECL(celt_word16_t *error);
93 /* The .7 is a heuristic */
96 ALLOC(error, m->nbEBands, celt_word16_t);
97 bits = ec_enc_tell(enc, 0);
98 for (i=0;i<m->nbEBands;i++)
101 celt_word16_t q; /* dB */
102 celt_word16_t x; /* dB */
103 celt_word16_t f; /* Q8 */
104 celt_word16_t mean = (1-coef)*eMeans[i];
105 x = amp2dB(eBands[i]);
106 f = DIV32_16(SHL32(EXTEND32(x-mean-coef*oldEBands[i]-prev),8),base_resolution);
108 /* Rounding to nearest integer here is really important! */
111 qi = (int)floor(.5+f);
113 /*ec_laplace_encode(enc, qi, i==0?11192:6192);*/
114 /*ec_laplace_encode(enc, qi, 8500-i*200);*/
115 /* If we don't have enough bits to encode all the energy, just assume something safe. */
116 if (ec_enc_tell(enc, 0) - bits > budget)
119 ec_laplace_encode(enc, qi, 6000-i*200);
120 q = qi*base_resolution;
121 error[i] = f - SHL16(qi,8);
123 /*printf("%d ", qi);*/
124 /*printf("%f %f ", pred+prev+q, x);*/
125 /*printf("%f ", x-pred);*/
127 oldEBands[i] = mean+coef*oldEBands[i]+prev+q;
129 prev = mean+prev+(1-beta)*q;
131 /*bits = ec_enc_tell(enc, 0) - bits;*/
132 /*printf ("%d\n", bits);*/
133 for (i=0;i<m->nbEBands;i++)
136 celt_word16_t offset = (error[i]+QCONST16(.5f,8))*frac[i];
137 /* FIXME: Instead of giving up without warning, we should degrade everything gracefully */
138 if (ec_enc_tell(enc, 0) - bits +EC_ILOG(frac[i])> budget)
141 /* Has to be without rounding */
144 q2 = (int)floor(offset);
148 ec_enc_uint(enc, q2, frac[i]);
149 offset = DIV32_16(SHL16(q2,8)+QCONST16(.5,8),frac[i])-QCONST16(.5f,8);
150 oldEBands[i] += PSHR32(MULT16_16(DB_SCALING*6,offset),8);
151 /*printf ("%f ", error[i] - offset);*/
153 for (i=0;i<m->nbEBands;i++)
155 eBands[i] = dB2Amp(oldEBands[i]);
157 /*printf ("%d\n", ec_enc_tell(enc, 0)-9);*/
162 static void unquant_energy_mono(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, int budget, ec_dec *dec)
166 celt_word16_t prev = 0;
167 float coef = m->ePredCoef;
168 /* The .7 is a heuristic */
169 float beta = .7*coef;
170 bits = ec_dec_tell(dec, 0);
171 for (i=0;i<m->nbEBands;i++)
175 celt_word16_t mean = (1-coef)*eMeans[i];
176 /* If we didn't have enough bits to encode all the energy, just assume something safe. */
177 if (ec_dec_tell(dec, 0) - bits > budget)
180 qi = ec_laplace_decode(dec, 6000-i*200);
181 q = qi*base_resolution;
183 /*printf("%d ", qi);*/
184 /*printf("%f %f ", pred+prev+q, x);*/
185 /*printf("%f ", x-pred);*/
187 oldEBands[i] = mean+coef*oldEBands[i]+prev+q;
189 prev = mean+prev+(1-beta)*q;
191 for (i=0;i<m->nbEBands;i++)
194 celt_word16_t offset;
195 if (ec_dec_tell(dec, 0) - bits +EC_ILOG(frac[i])> budget)
197 q2 = ec_dec_uint(dec, frac[i]);
198 offset = DIV32_16(SHL16(q2,8)+QCONST16(.5,8),frac[i])-QCONST16(.5f,8);
199 oldEBands[i] += PSHR32(MULT16_16(DB_SCALING*6,offset),8);
201 for (i=0;i<m->nbEBands;i++)
203 eBands[i] = dB2Amp(oldEBands[i]);
210 void quant_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, int budget, ec_enc *enc)
217 quant_energy_mono(m, eBands, oldEBands, budget, enc);
222 VARDECL(celt_ener_t *E);
223 ALLOC(E, m->nbEBands, celt_ener_t);
227 for (i=0;i<m->nbEBands;i++)
228 E[i] = eBands[C*i+c];
229 quant_energy_mono(m, E, oldEBands+c*m->nbEBands, budget/C, enc);
230 for (i=0;i<m->nbEBands;i++)
231 eBands[C*i+c] = E[i];
238 int NB = m->nbEBands;
240 celt_ener_t side[NB];
243 //left = eBands[C*i];
244 //right = eBands[C*i+1];
245 mid[i] = ENER_SCALING_1*sqrt(eBands[C*i]*eBands[C*i] + eBands[C*i+1]*eBands[C*i+1]);
246 side[i] = 20*log10((ENER_SCALING_1*eBands[2*i]+.3)/(ENER_SCALING_1*eBands[2*i+1]+.3));
247 //printf ("%f %f ", mid[i], side[i]);
250 quant_energy_mono(m, mid, oldEBands, enc);
252 side[i] = pow(10.f,floor(.5f+side[i])/10.f);
254 //quant_energy_side(m, side, oldEBands+NB, enc);
257 eBands[C*i] = ENER_SCALING*mid[i]*sqrt(side[i]/(1.f+side[i]));
258 eBands[C*i+1] = ENER_SCALING*mid[i]*sqrt(1.f/(1.f+side[i]));
259 //printf ("%f %f ", mid[i], side[i]);
263 celt_fatal("more than 2 channels not supported");
270 void unquant_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, int budget, ec_dec *dec)
276 unquant_energy_mono(m, eBands, oldEBands, budget, dec);
279 VARDECL(celt_ener_t *E);
280 ALLOC(E, m->nbEBands, celt_ener_t);
284 unquant_energy_mono(m, E, oldEBands+c*m->nbEBands, budget/C, dec);
285 for (i=0;i<m->nbEBands;i++)
286 eBands[C*i+c] = E[i];