/src/opus/celt/quant_bands.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2007-2008 CSIRO |
2 | | Copyright (c) 2007-2009 Xiph.Org Foundation |
3 | | Written by Jean-Marc Valin */ |
4 | | /* |
5 | | Redistribution and use in source and binary forms, with or without |
6 | | modification, are permitted provided that the following conditions |
7 | | are met: |
8 | | |
9 | | - Redistributions of source code must retain the above copyright |
10 | | notice, this list of conditions and the following disclaimer. |
11 | | |
12 | | - Redistributions in binary form must reproduce the above copyright |
13 | | notice, this list of conditions and the following disclaimer in the |
14 | | documentation and/or other materials provided with the distribution. |
15 | | |
16 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 | | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
20 | | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
21 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
22 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
23 | | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
24 | | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
25 | | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
26 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | | */ |
28 | | |
29 | | #ifdef HAVE_CONFIG_H |
30 | | #include "config.h" |
31 | | #endif |
32 | | |
33 | | #include "quant_bands.h" |
34 | | #include "laplace.h" |
35 | | #include <math.h> |
36 | | #include "os_support.h" |
37 | | #include "arch.h" |
38 | | #include "mathops.h" |
39 | | #include "stack_alloc.h" |
40 | | #include "rate.h" |
41 | | |
42 | | #ifdef FIXED_POINT |
43 | | /* Mean energy in each band quantized in Q4 */ |
44 | | const signed char eMeans[25] = { |
45 | | 103,100, 92, 85, 81, |
46 | | 77, 72, 70, 78, 75, |
47 | | 73, 71, 78, 74, 69, |
48 | | 72, 70, 74, 76, 71, |
49 | | 60, 60, 60, 60, 60 |
50 | | }; |
51 | | #else |
52 | | /* Mean energy in each band quantized in Q4 and converted back to float */ |
53 | | const opus_val16 eMeans[25] = { |
54 | | 6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f, |
55 | | 4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f, |
56 | | 4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f, |
57 | | 4.500000f, 4.375000f, 4.625000f, 4.750000f, 4.437500f, |
58 | | 3.750000f, 3.750000f, 3.750000f, 3.750000f, 3.750000f |
59 | | }; |
60 | | #endif |
61 | | /* prediction coefficients: 0.9, 0.8, 0.65, 0.5 */ |
62 | | #ifdef FIXED_POINT |
63 | | static const opus_val16 pred_coef[4] = {29440, 26112, 21248, 16384}; |
64 | | static const opus_val16 beta_coef[4] = {30147, 22282, 12124, 6554}; |
65 | | static const opus_val16 beta_intra = 4915; |
66 | | #else |
67 | | static const opus_val16 pred_coef[4] = {29440/32768., 26112/32768., 21248/32768., 16384/32768.}; |
68 | | static const opus_val16 beta_coef[4] = {30147/32768., 22282/32768., 12124/32768., 6554/32768.}; |
69 | | static const opus_val16 beta_intra = 4915/32768.; |
70 | | #endif |
71 | | |
72 | | /*Parameters of the Laplace-like probability models used for the coarse energy. |
73 | | There is one pair of parameters for each frame size, prediction type |
74 | | (inter/intra), and band number. |
75 | | The first number of each pair is the probability of 0, and the second is the |
76 | | decay rate, both in Q8 precision.*/ |
77 | | static const unsigned char e_prob_model[4][2][42] = { |
78 | | /*120 sample frames.*/ |
79 | | { |
80 | | /*Inter*/ |
81 | | { |
82 | | 72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128, |
83 | | 64, 128, 92, 78, 92, 79, 92, 78, 90, 79, 116, 41, 115, 40, |
84 | | 114, 40, 132, 26, 132, 26, 145, 17, 161, 12, 176, 10, 177, 11 |
85 | | }, |
86 | | /*Intra*/ |
87 | | { |
88 | | 24, 179, 48, 138, 54, 135, 54, 132, 53, 134, 56, 133, 55, 132, |
89 | | 55, 132, 61, 114, 70, 96, 74, 88, 75, 88, 87, 74, 89, 66, |
90 | | 91, 67, 100, 59, 108, 50, 120, 40, 122, 37, 97, 43, 78, 50 |
91 | | } |
92 | | }, |
93 | | /*240 sample frames.*/ |
94 | | { |
95 | | /*Inter*/ |
96 | | { |
97 | | 83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73, 93, 74, |
98 | | 93, 74, 109, 40, 114, 36, 117, 34, 117, 34, 143, 17, 145, 18, |
99 | | 146, 19, 162, 12, 165, 10, 178, 7, 189, 6, 190, 8, 177, 9 |
100 | | }, |
101 | | /*Intra*/ |
102 | | { |
103 | | 23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91, |
104 | | 73, 91, 78, 89, 86, 80, 92, 66, 93, 64, 102, 59, 103, 60, |
105 | | 104, 60, 117, 52, 123, 44, 138, 35, 133, 31, 97, 38, 77, 45 |
106 | | } |
107 | | }, |
108 | | /*480 sample frames.*/ |
109 | | { |
110 | | /*Inter*/ |
111 | | { |
112 | | 61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38, |
113 | | 112, 38, 124, 26, 132, 27, 136, 19, 140, 20, 155, 14, 159, 16, |
114 | | 158, 18, 170, 13, 177, 10, 187, 8, 192, 6, 175, 9, 159, 10 |
115 | | }, |
116 | | /*Intra*/ |
117 | | { |
118 | | 21, 178, 59, 110, 71, 86, 75, 85, 84, 83, 91, 66, 88, 73, |
119 | | 87, 72, 92, 75, 98, 72, 105, 58, 107, 54, 115, 52, 114, 55, |
120 | | 112, 56, 129, 51, 132, 40, 150, 33, 140, 29, 98, 35, 77, 42 |
121 | | } |
122 | | }, |
123 | | /*960 sample frames.*/ |
124 | | { |
125 | | /*Inter*/ |
126 | | { |
127 | | 42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32, 120, 36, |
128 | | 119, 33, 127, 33, 134, 34, 139, 21, 147, 23, 152, 20, 158, 25, |
129 | | 154, 26, 166, 21, 173, 16, 184, 13, 184, 10, 150, 13, 139, 15 |
130 | | }, |
131 | | /*Intra*/ |
132 | | { |
133 | | 22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72, |
134 | | 96, 67, 101, 73, 107, 72, 113, 55, 118, 52, 125, 52, 118, 52, |
135 | | 117, 55, 135, 49, 137, 39, 157, 32, 145, 29, 97, 33, 77, 40 |
136 | | } |
137 | | } |
138 | | }; |
139 | | |
140 | | static const unsigned char small_energy_icdf[3]={2,1,0}; |
141 | | |
142 | | static opus_val32 loss_distortion(const celt_glog *eBands, celt_glog *oldEBands, int start, int end, int len, int C) |
143 | 0 | { |
144 | 0 | int c, i; |
145 | 0 | opus_val32 dist = 0; |
146 | 0 | c=0; do { |
147 | 0 | for (i=start;i<end;i++) |
148 | 0 | { |
149 | 0 | celt_glog d = PSHR32(SUB32(eBands[i+c*len], oldEBands[i+c*len]), DB_SHIFT-7); |
150 | 0 | dist = MAC16_16(dist, d,d); |
151 | 0 | } |
152 | 0 | } while (++c<C); |
153 | 0 | return MIN32(200,SHR32(dist,14)); |
154 | 0 | } |
155 | | |
156 | | static int quant_coarse_energy_impl(const CELTMode *m, int start, int end, |
157 | | const celt_glog *eBands, celt_glog *oldEBands, |
158 | | opus_int32 budget, opus_int32 tell, |
159 | | const unsigned char *prob_model, celt_glog *error, ec_enc *enc, |
160 | | int C, int LM, int intra, celt_glog max_decay, int lfe) |
161 | 0 | { |
162 | 0 | int i, c; |
163 | 0 | int badness = 0; |
164 | 0 | opus_val32 prev[2] = {0,0}; |
165 | 0 | opus_val16 coef; |
166 | 0 | opus_val16 beta; |
167 | |
|
168 | 0 | if (tell+3 <= budget) |
169 | 0 | ec_enc_bit_logp(enc, intra, 3); |
170 | 0 | if (intra) |
171 | 0 | { |
172 | 0 | coef = 0; |
173 | 0 | beta = beta_intra; |
174 | 0 | } else { |
175 | 0 | beta = beta_coef[LM]; |
176 | 0 | coef = pred_coef[LM]; |
177 | 0 | } |
178 | | |
179 | | /* Encode at a fixed coarse resolution */ |
180 | 0 | for (i=start;i<end;i++) |
181 | 0 | { |
182 | 0 | c=0; |
183 | 0 | do { |
184 | 0 | int bits_left; |
185 | 0 | int qi, qi0; |
186 | 0 | opus_val32 q; |
187 | 0 | celt_glog x; |
188 | 0 | opus_val32 f, tmp; |
189 | 0 | celt_glog oldE; |
190 | 0 | celt_glog decay_bound; |
191 | 0 | x = eBands[i+c*m->nbEBands]; |
192 | 0 | oldE = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]); |
193 | | #ifdef FIXED_POINT |
194 | | f = x - MULT16_32_Q15(coef,oldE) - prev[c]; |
195 | | /* Rounding to nearest integer here is really important! */ |
196 | | qi = (f+QCONST32(.5f,DB_SHIFT))>>DB_SHIFT; |
197 | | decay_bound = MAXG(-GCONST(28.f), SUB32((opus_val32)oldEBands[i+c*m->nbEBands],max_decay)); |
198 | | #else |
199 | 0 | f = x-coef*oldE-prev[c]; |
200 | | /* Rounding to nearest integer here is really important! */ |
201 | 0 | qi = (int)floor(.5f+f); |
202 | 0 | decay_bound = MAXG(-GCONST(28.f), oldEBands[i+c*m->nbEBands]) - max_decay; |
203 | 0 | #endif |
204 | | /* Prevent the energy from going down too quickly (e.g. for bands |
205 | | that have just one bin) */ |
206 | 0 | if (qi < 0 && x < decay_bound) |
207 | 0 | { |
208 | 0 | qi += (int)SHR32(SUB32(decay_bound,x), DB_SHIFT); |
209 | 0 | if (qi > 0) |
210 | 0 | qi = 0; |
211 | 0 | } |
212 | 0 | qi0 = qi; |
213 | | /* If we don't have enough bits to encode all the energy, just assume |
214 | | something safe. */ |
215 | 0 | tell = ec_tell(enc); |
216 | 0 | bits_left = budget-tell-3*C*(end-i); |
217 | 0 | if (i!=start && bits_left < 30) |
218 | 0 | { |
219 | 0 | if (bits_left < 24) |
220 | 0 | qi = IMIN(1, qi); |
221 | 0 | if (bits_left < 16) |
222 | 0 | qi = IMAX(-1, qi); |
223 | 0 | } |
224 | 0 | if (lfe && i>=2) |
225 | 0 | qi = IMIN(qi, 0); |
226 | 0 | if (budget-tell >= 15) |
227 | 0 | { |
228 | 0 | int pi; |
229 | 0 | pi = 2*IMIN(i,20); |
230 | 0 | ec_laplace_encode(enc, &qi, |
231 | 0 | prob_model[pi]<<7, prob_model[pi+1]<<6); |
232 | 0 | } |
233 | 0 | else if(budget-tell >= 2) |
234 | 0 | { |
235 | 0 | qi = IMAX(-1, IMIN(qi, 1)); |
236 | 0 | ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2); |
237 | 0 | } |
238 | 0 | else if(budget-tell >= 1) |
239 | 0 | { |
240 | 0 | qi = IMIN(0, qi); |
241 | 0 | ec_enc_bit_logp(enc, -qi, 1); |
242 | 0 | } |
243 | 0 | else |
244 | 0 | qi = -1; |
245 | 0 | error[i+c*m->nbEBands] = f - SHL32(qi,DB_SHIFT); |
246 | 0 | badness += abs(qi0-qi); |
247 | 0 | q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT); |
248 | |
|
249 | 0 | tmp = MULT16_32_Q15(coef,oldE) + prev[c] + q; |
250 | | #ifdef FIXED_POINT |
251 | | tmp = MAX32(-GCONST(28.f), tmp); |
252 | | #endif |
253 | 0 | oldEBands[i+c*m->nbEBands] = tmp; |
254 | 0 | prev[c] = prev[c] + q - MULT16_32_Q15(beta,q); |
255 | 0 | } while (++c < C); |
256 | 0 | } |
257 | 0 | return lfe ? 0 : badness; |
258 | 0 | } |
259 | | |
260 | | void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd, |
261 | | const celt_glog *eBands, celt_glog *oldEBands, opus_uint32 budget, |
262 | | celt_glog *error, ec_enc *enc, int C, int LM, int nbAvailableBytes, |
263 | | int force_intra, opus_val32 *delayedIntra, int two_pass, int loss_rate, int lfe) |
264 | 0 | { |
265 | 0 | int intra; |
266 | 0 | celt_glog max_decay; |
267 | 0 | VARDECL(celt_glog, oldEBands_intra); |
268 | 0 | VARDECL(celt_glog, error_intra); |
269 | 0 | ec_enc enc_start_state; |
270 | 0 | opus_uint32 tell; |
271 | 0 | int badness1=0; |
272 | 0 | opus_int32 intra_bias; |
273 | 0 | opus_val32 new_distortion; |
274 | 0 | SAVE_STACK; |
275 | |
|
276 | 0 | intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C); |
277 | 0 | intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512)); |
278 | 0 | new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C); |
279 | |
|
280 | 0 | tell = ec_tell(enc); |
281 | 0 | if (tell+3 > budget) |
282 | 0 | two_pass = intra = 0; |
283 | |
|
284 | 0 | max_decay = GCONST(16.f); |
285 | 0 | if (end-start>10) |
286 | 0 | { |
287 | | #ifdef FIXED_POINT |
288 | | max_decay = SHL32(MIN32(SHR32(max_decay,DB_SHIFT-3), EXTEND32(nbAvailableBytes)),DB_SHIFT-3); |
289 | | #else |
290 | 0 | max_decay = MIN32(max_decay, .125f*nbAvailableBytes); |
291 | 0 | #endif |
292 | 0 | } |
293 | 0 | if (lfe) |
294 | 0 | max_decay = GCONST(3.f); |
295 | 0 | enc_start_state = *enc; |
296 | |
|
297 | 0 | ALLOC(oldEBands_intra, C*m->nbEBands, celt_glog); |
298 | 0 | ALLOC(error_intra, C*m->nbEBands, celt_glog); |
299 | 0 | OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands); |
300 | |
|
301 | 0 | if (two_pass || intra) |
302 | 0 | { |
303 | 0 | badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget, |
304 | 0 | tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lfe); |
305 | 0 | } |
306 | |
|
307 | 0 | if (!intra) |
308 | 0 | { |
309 | 0 | unsigned char *intra_buf; |
310 | 0 | ec_enc enc_intra_state; |
311 | 0 | opus_int32 tell_intra; |
312 | 0 | opus_uint32 nstart_bytes; |
313 | 0 | opus_uint32 nintra_bytes; |
314 | 0 | opus_uint32 save_bytes; |
315 | 0 | int badness2; |
316 | 0 | VARDECL(unsigned char, intra_bits); |
317 | |
|
318 | 0 | tell_intra = ec_tell_frac(enc); |
319 | |
|
320 | 0 | enc_intra_state = *enc; |
321 | |
|
322 | 0 | nstart_bytes = ec_range_bytes(&enc_start_state); |
323 | 0 | nintra_bytes = ec_range_bytes(&enc_intra_state); |
324 | 0 | intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes; |
325 | 0 | save_bytes = nintra_bytes-nstart_bytes; |
326 | 0 | if (save_bytes == 0) |
327 | 0 | save_bytes = ALLOC_NONE; |
328 | 0 | ALLOC(intra_bits, save_bytes, unsigned char); |
329 | | /* Copy bits from intra bit-stream */ |
330 | 0 | OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes); |
331 | |
|
332 | 0 | *enc = enc_start_state; |
333 | |
|
334 | 0 | badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget, |
335 | 0 | tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe); |
336 | |
|
337 | 0 | if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra))) |
338 | 0 | { |
339 | 0 | *enc = enc_intra_state; |
340 | | /* Copy intra bits to bit-stream */ |
341 | 0 | OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes); |
342 | 0 | OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands); |
343 | 0 | OPUS_COPY(error, error_intra, C*m->nbEBands); |
344 | 0 | intra = 1; |
345 | 0 | } |
346 | 0 | } else { |
347 | 0 | OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands); |
348 | 0 | OPUS_COPY(error, error_intra, C*m->nbEBands); |
349 | 0 | } |
350 | |
|
351 | 0 | if (intra) |
352 | 0 | *delayedIntra = new_distortion; |
353 | 0 | else |
354 | 0 | *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra), |
355 | 0 | new_distortion); |
356 | |
|
357 | 0 | RESTORE_STACK; |
358 | 0 | } |
359 | | |
360 | | void quant_fine_energy(const CELTMode *m, int start, int end, celt_glog *oldEBands, celt_glog *error, int *fine_quant, ec_enc *enc, int C) |
361 | 0 | { |
362 | 0 | int i, c; |
363 | | |
364 | | /* Encode finer resolution */ |
365 | 0 | for (i=start;i<end;i++) |
366 | 0 | { |
367 | 0 | opus_int16 frac = 1<<fine_quant[i]; |
368 | 0 | if (fine_quant[i] <= 0) |
369 | 0 | continue; |
370 | 0 | c=0; |
371 | 0 | do { |
372 | 0 | int q2; |
373 | 0 | celt_glog offset; |
374 | | #ifdef FIXED_POINT |
375 | | /* Has to be without rounding */ |
376 | | q2 = (error[i+c*m->nbEBands]+GCONST(.5f))>>(DB_SHIFT-fine_quant[i]); |
377 | | #else |
378 | 0 | q2 = (int)floor((error[i+c*m->nbEBands]+.5f)*frac); |
379 | 0 | #endif |
380 | 0 | if (q2 > frac-1) |
381 | 0 | q2 = frac-1; |
382 | 0 | if (q2<0) |
383 | 0 | q2 = 0; |
384 | 0 | ec_enc_bits(enc, q2, fine_quant[i]); |
385 | | #ifdef FIXED_POINT |
386 | | offset = SUB32(VSHR32(2*q2+1, fine_quant[i]-DB_SHIFT+1), GCONST(.5f)); |
387 | | #else |
388 | 0 | offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f; |
389 | 0 | #endif |
390 | 0 | oldEBands[i+c*m->nbEBands] += offset; |
391 | 0 | error[i+c*m->nbEBands] -= offset; |
392 | | /*printf ("%f ", error[i] - offset);*/ |
393 | 0 | } while (++c < C); |
394 | 0 | } |
395 | 0 | } |
396 | | |
397 | | void quant_energy_finalise(const CELTMode *m, int start, int end, celt_glog *oldEBands, celt_glog *error, int *fine_quant, int *fine_priority, int bits_left, ec_enc *enc, int C) |
398 | 0 | { |
399 | 0 | int i, prio, c; |
400 | | |
401 | | /* Use up the remaining bits */ |
402 | 0 | for (prio=0;prio<2;prio++) |
403 | 0 | { |
404 | 0 | for (i=start;i<end && bits_left>=C ;i++) |
405 | 0 | { |
406 | 0 | if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio) |
407 | 0 | continue; |
408 | 0 | c=0; |
409 | 0 | do { |
410 | 0 | int q2; |
411 | 0 | celt_glog offset; |
412 | 0 | q2 = error[i+c*m->nbEBands]<0 ? 0 : 1; |
413 | 0 | ec_enc_bits(enc, q2, 1); |
414 | | #ifdef FIXED_POINT |
415 | | offset = SHR32(SHL32(q2,DB_SHIFT)-GCONST(.5f),fine_quant[i]+1); |
416 | | #else |
417 | 0 | offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384); |
418 | 0 | #endif |
419 | 0 | oldEBands[i+c*m->nbEBands] += offset; |
420 | 0 | error[i+c*m->nbEBands] -= offset; |
421 | 0 | bits_left--; |
422 | 0 | } while (++c < C); |
423 | 0 | } |
424 | 0 | } |
425 | 0 | } |
426 | | |
427 | | void unquant_coarse_energy(const CELTMode *m, int start, int end, celt_glog *oldEBands, int intra, ec_dec *dec, int C, int LM) |
428 | 116k | { |
429 | 116k | const unsigned char *prob_model = e_prob_model[LM][intra]; |
430 | 116k | int i, c; |
431 | 116k | opus_val64 prev[2] = {0, 0}; |
432 | 116k | opus_val16 coef; |
433 | 116k | opus_val16 beta; |
434 | 116k | opus_int32 budget; |
435 | 116k | opus_int32 tell; |
436 | | |
437 | 116k | if (intra) |
438 | 6.48k | { |
439 | 6.48k | coef = 0; |
440 | 6.48k | beta = beta_intra; |
441 | 110k | } else { |
442 | 110k | beta = beta_coef[LM]; |
443 | 110k | coef = pred_coef[LM]; |
444 | 110k | } |
445 | | |
446 | 116k | budget = dec->storage*8; |
447 | | |
448 | | /* Decode at a fixed coarse resolution */ |
449 | 1.55M | for (i=start;i<end;i++) |
450 | 1.44M | { |
451 | 1.44M | c=0; |
452 | 1.77M | do { |
453 | 1.77M | int qi; |
454 | 1.77M | opus_val32 q; |
455 | 1.77M | opus_val32 tmp; |
456 | | /* It would be better to express this invariant as a |
457 | | test on C at function entry, but that isn't enough |
458 | | to make the static analyzer happy. */ |
459 | 1.77M | celt_sig_assert(c<2); |
460 | 1.77M | tell = ec_tell(dec); |
461 | 1.77M | if(budget-tell>=15) |
462 | 1.31M | { |
463 | 1.31M | int pi; |
464 | 1.31M | pi = 2*IMIN(i,20); |
465 | 1.31M | qi = ec_laplace_decode(dec, |
466 | 1.31M | prob_model[pi]<<7, prob_model[pi+1]<<6); |
467 | 1.31M | } |
468 | 467k | else if(budget-tell>=2) |
469 | 194k | { |
470 | 194k | qi = ec_dec_icdf(dec, small_energy_icdf, 2); |
471 | 194k | qi = (qi>>1)^-(qi&1); |
472 | 194k | } |
473 | 272k | else if(budget-tell>=1) |
474 | 8.88k | { |
475 | 8.88k | qi = -ec_dec_bit_logp(dec, 1); |
476 | 8.88k | } |
477 | 263k | else |
478 | 263k | qi = -1; |
479 | 1.77M | q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT); |
480 | | |
481 | 1.77M | oldEBands[i+c*m->nbEBands] = MAXG(-GCONST(9.f), oldEBands[i+c*m->nbEBands]); |
482 | 1.77M | tmp = MULT16_32_Q15(coef,oldEBands[i+c*m->nbEBands]) + prev[c] + q; |
483 | | #ifdef FIXED_POINT |
484 | | tmp = MIN32(GCONST(28.f), MAX32(-GCONST(28.f), tmp)); |
485 | | #endif |
486 | 1.77M | oldEBands[i+c*m->nbEBands] = tmp; |
487 | 1.77M | prev[c] = prev[c] + q - MULT16_32_Q15(beta,q); |
488 | 1.77M | } while (++c < C); |
489 | 1.44M | } |
490 | 116k | } |
491 | | |
492 | | void unquant_fine_energy(const CELTMode *m, int start, int end, celt_glog *oldEBands, int *fine_quant, ec_dec *dec, int C) |
493 | 116k | { |
494 | 116k | int i, c; |
495 | | /* Decode finer resolution */ |
496 | 1.55M | for (i=start;i<end;i++) |
497 | 1.44M | { |
498 | 1.44M | if (fine_quant[i] <= 0) |
499 | 732k | continue; |
500 | 708k | c=0; |
501 | 896k | do { |
502 | 896k | int q2; |
503 | 896k | celt_glog offset; |
504 | 896k | q2 = ec_dec_bits(dec, fine_quant[i]); |
505 | | #ifdef FIXED_POINT |
506 | | offset = SUB32(VSHR32(2*q2+1, fine_quant[i]-DB_SHIFT+1), GCONST(.5f)); |
507 | | #else |
508 | 896k | offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f; |
509 | 896k | #endif |
510 | 896k | oldEBands[i+c*m->nbEBands] += offset; |
511 | 896k | } while (++c < C); |
512 | 708k | } |
513 | 116k | } |
514 | | |
515 | | void unquant_energy_finalise(const CELTMode *m, int start, int end, celt_glog *oldEBands, int *fine_quant, int *fine_priority, int bits_left, ec_dec *dec, int C) |
516 | 116k | { |
517 | 116k | int i, prio, c; |
518 | | |
519 | | /* Use up the remaining bits */ |
520 | 350k | for (prio=0;prio<2;prio++) |
521 | 233k | { |
522 | 1.08M | for (i=start;i<end && bits_left>=C ;i++) |
523 | 846k | { |
524 | 846k | if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio) |
525 | 682k | continue; |
526 | 164k | c=0; |
527 | 218k | do { |
528 | 218k | int q2; |
529 | 218k | celt_glog offset; |
530 | 218k | q2 = ec_dec_bits(dec, 1); |
531 | | #ifdef FIXED_POINT |
532 | | offset = SHR32(SHL32(q2,DB_SHIFT)-GCONST(.5f),fine_quant[i]+1); |
533 | | #else |
534 | 218k | offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384); |
535 | 218k | #endif |
536 | 218k | oldEBands[i+c*m->nbEBands] += offset; |
537 | 218k | bits_left--; |
538 | 218k | } while (++c < C); |
539 | 164k | } |
540 | 233k | } |
541 | 116k | } |
542 | | |
543 | | void amp2Log2(const CELTMode *m, int effEnd, int end, |
544 | | celt_ener *bandE, celt_glog *bandLogE, int C) |
545 | 0 | { |
546 | 0 | int c, i; |
547 | 0 | c=0; |
548 | 0 | do { |
549 | 0 | for (i=0;i<effEnd;i++) |
550 | 0 | { |
551 | 0 | bandLogE[i+c*m->nbEBands] = |
552 | 0 | celt_log2_db(bandE[i+c*m->nbEBands]) |
553 | 0 | - SHL32((celt_glog)eMeans[i],DB_SHIFT-4); |
554 | | #ifdef FIXED_POINT |
555 | | /* Compensate for bandE[] being Q12 but celt_log2() taking a Q14 input. */ |
556 | | bandLogE[i+c*m->nbEBands] += GCONST(2.f); |
557 | | #endif |
558 | 0 | } |
559 | 0 | for (i=effEnd;i<end;i++) |
560 | 0 | bandLogE[c*m->nbEBands+i] = -GCONST(14.f); |
561 | 0 | } while (++c < C); |
562 | 0 | } |