OLD | NEW |
1 /* Copyright (c) 2007-2008 CSIRO | 1 /* Copyright (c) 2007-2008 CSIRO |
2 Copyright (c) 2007-2009 Xiph.Org Foundation | 2 Copyright (c) 2007-2009 Xiph.Org Foundation |
3 Written by Jean-Marc Valin */ | 3 Written by Jean-Marc Valin */ |
4 /* | 4 /* |
5 Redistribution and use in source and binary forms, with or without | 5 Redistribution and use in source and binary forms, with or without |
6 modification, are permitted provided that the following conditions | 6 modification, are permitted provided that the following conditions |
7 are met: | 7 are met: |
8 | 8 |
9 - Redistributions of source code must retain the above copyright | 9 - Redistributions of source code must retain the above copyright |
10 notice, this list of conditions and the following disclaimer. | 10 notice, this list of conditions and the following disclaimer. |
(...skipping 22 matching lines...) Expand all Loading... |
33 #include "quant_bands.h" | 33 #include "quant_bands.h" |
34 #include "laplace.h" | 34 #include "laplace.h" |
35 #include <math.h> | 35 #include <math.h> |
36 #include "os_support.h" | 36 #include "os_support.h" |
37 #include "arch.h" | 37 #include "arch.h" |
38 #include "mathops.h" | 38 #include "mathops.h" |
39 #include "stack_alloc.h" | 39 #include "stack_alloc.h" |
40 #include "rate.h" | 40 #include "rate.h" |
41 | 41 |
42 #ifdef FIXED_POINT | 42 #ifdef FIXED_POINT |
43 /* Mean energy in each band quantized in Q6 */ | 43 /* Mean energy in each band quantized in Q4 */ |
44 static const signed char eMeans[25] = { | 44 const signed char eMeans[25] = { |
45 103,100, 92, 85, 81, | 45 103,100, 92, 85, 81, |
46 77, 72, 70, 78, 75, | 46 77, 72, 70, 78, 75, |
47 73, 71, 78, 74, 69, | 47 73, 71, 78, 74, 69, |
48 72, 70, 74, 76, 71, | 48 72, 70, 74, 76, 71, |
49 60, 60, 60, 60, 60 | 49 60, 60, 60, 60, 60 |
50 }; | 50 }; |
51 #else | 51 #else |
52 /* Mean energy in each band quantized in Q6 and converted back to float */ | 52 /* Mean energy in each band quantized in Q4 and converted back to float */ |
53 static const opus_val16 eMeans[25] = { | 53 const opus_val16 eMeans[25] = { |
54 6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f, | 54 6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f, |
55 4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f, | 55 4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f, |
56 4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f, | 56 4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f, |
57 4.500000f, 4.375000f, 4.625000f, 4.750000f, 4.437500f, | 57 4.500000f, 4.375000f, 4.625000f, 4.750000f, 4.437500f, |
58 3.750000f, 3.750000f, 3.750000f, 3.750000f, 3.750000f | 58 3.750000f, 3.750000f, 3.750000f, 3.750000f, 3.750000f |
59 }; | 59 }; |
60 #endif | 60 #endif |
61 /* prediction coefficients: 0.9, 0.8, 0.65, 0.5 */ | 61 /* prediction coefficients: 0.9, 0.8, 0.65, 0.5 */ |
62 #ifdef FIXED_POINT | 62 #ifdef FIXED_POINT |
63 static const opus_val16 pred_coef[4] = {29440, 26112, 21248, 16384}; | 63 static const opus_val16 pred_coef[4] = {29440, 26112, 21248, 16384}; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 dist = MAC16_16(dist, d,d); | 150 dist = MAC16_16(dist, d,d); |
151 } | 151 } |
152 } while (++c<C); | 152 } while (++c<C); |
153 return MIN32(200,SHR32(dist,2*DB_SHIFT-6)); | 153 return MIN32(200,SHR32(dist,2*DB_SHIFT-6)); |
154 } | 154 } |
155 | 155 |
156 static int quant_coarse_energy_impl(const CELTMode *m, int start, int end, | 156 static int quant_coarse_energy_impl(const CELTMode *m, int start, int end, |
157 const opus_val16 *eBands, opus_val16 *oldEBands, | 157 const opus_val16 *eBands, opus_val16 *oldEBands, |
158 opus_int32 budget, opus_int32 tell, | 158 opus_int32 budget, opus_int32 tell, |
159 const unsigned char *prob_model, opus_val16 *error, ec_enc *enc, | 159 const unsigned char *prob_model, opus_val16 *error, ec_enc *enc, |
160 int C, int LM, int intra, opus_val16 max_decay) | 160 int C, int LM, int intra, opus_val16 max_decay, int lfe) |
161 { | 161 { |
162 int i, c; | 162 int i, c; |
163 int badness = 0; | 163 int badness = 0; |
164 opus_val32 prev[2] = {0,0}; | 164 opus_val32 prev[2] = {0,0}; |
165 opus_val16 coef; | 165 opus_val16 coef; |
166 opus_val16 beta; | 166 opus_val16 beta; |
167 | 167 |
168 if (tell+3 <= budget) | 168 if (tell+3 <= budget) |
169 ec_enc_bit_logp(enc, intra, 3); | 169 ec_enc_bit_logp(enc, intra, 3); |
170 if (intra) | 170 if (intra) |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 something safe. */ | 215 something safe. */ |
216 tell = ec_tell(enc); | 216 tell = ec_tell(enc); |
217 bits_left = budget-tell-3*C*(end-i); | 217 bits_left = budget-tell-3*C*(end-i); |
218 if (i!=start && bits_left < 30) | 218 if (i!=start && bits_left < 30) |
219 { | 219 { |
220 if (bits_left < 24) | 220 if (bits_left < 24) |
221 qi = IMIN(1, qi); | 221 qi = IMIN(1, qi); |
222 if (bits_left < 16) | 222 if (bits_left < 16) |
223 qi = IMAX(-1, qi); | 223 qi = IMAX(-1, qi); |
224 } | 224 } |
| 225 if (lfe && i>=2) |
| 226 qi = IMIN(qi, 0); |
225 if (budget-tell >= 15) | 227 if (budget-tell >= 15) |
226 { | 228 { |
227 int pi; | 229 int pi; |
228 pi = 2*IMIN(i,20); | 230 pi = 2*IMIN(i,20); |
229 ec_laplace_encode(enc, &qi, | 231 ec_laplace_encode(enc, &qi, |
230 prob_model[pi]<<7, prob_model[pi+1]<<6); | 232 prob_model[pi]<<7, prob_model[pi+1]<<6); |
231 } | 233 } |
232 else if(budget-tell >= 2) | 234 else if(budget-tell >= 2) |
233 { | 235 { |
234 qi = IMAX(-1, IMIN(qi, 1)); | 236 qi = IMAX(-1, IMIN(qi, 1)); |
(...skipping 11 matching lines...) Expand all Loading... |
246 q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT); | 248 q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT); |
247 | 249 |
248 tmp = PSHR32(MULT16_16(coef,oldE),8) + prev[c] + SHL32(q,7); | 250 tmp = PSHR32(MULT16_16(coef,oldE),8) + prev[c] + SHL32(q,7); |
249 #ifdef FIXED_POINT | 251 #ifdef FIXED_POINT |
250 tmp = MAX32(-QCONST32(28.f, DB_SHIFT+7), tmp); | 252 tmp = MAX32(-QCONST32(28.f, DB_SHIFT+7), tmp); |
251 #endif | 253 #endif |
252 oldEBands[i+c*m->nbEBands] = PSHR32(tmp, 7); | 254 oldEBands[i+c*m->nbEBands] = PSHR32(tmp, 7); |
253 prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8)); | 255 prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8)); |
254 } while (++c < C); | 256 } while (++c < C); |
255 } | 257 } |
256 return badness; | 258 return lfe ? 0 : badness; |
257 } | 259 } |
258 | 260 |
259 void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd, | 261 void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd, |
260 const opus_val16 *eBands, opus_val16 *oldEBands, opus_uint32 budget, | 262 const opus_val16 *eBands, opus_val16 *oldEBands, opus_uint32 budget, |
261 opus_val16 *error, ec_enc *enc, int C, int LM, int nbAvailableBytes, | 263 opus_val16 *error, ec_enc *enc, int C, int LM, int nbAvailableBytes, |
262 int force_intra, opus_val32 *delayedIntra, int two_pass, int loss_rate) | 264 int force_intra, opus_val32 *delayedIntra, int two_pass, int loss_rate, in
t lfe) |
263 { | 265 { |
264 int intra; | 266 int intra; |
265 opus_val16 max_decay; | 267 opus_val16 max_decay; |
266 VARDECL(opus_val16, oldEBands_intra); | 268 VARDECL(opus_val16, oldEBands_intra); |
267 VARDECL(opus_val16, error_intra); | 269 VARDECL(opus_val16, error_intra); |
268 ec_enc enc_start_state; | 270 ec_enc enc_start_state; |
269 opus_uint32 tell; | 271 opus_uint32 tell; |
270 int badness1=0; | 272 int badness1=0; |
271 opus_int32 intra_bias; | 273 opus_int32 intra_bias; |
272 opus_val32 new_distortion; | 274 opus_val32 new_distortion; |
273 SAVE_STACK; | 275 SAVE_STACK; |
274 | 276 |
275 intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvail
ableBytes > (end-start)*C); | 277 intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvail
ableBytes > (end-start)*C); |
276 intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512)); | 278 intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512)); |
277 new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBand
s, C); | 279 new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBand
s, C); |
278 | 280 |
279 tell = ec_tell(enc); | 281 tell = ec_tell(enc); |
280 if (tell+3 > budget) | 282 if (tell+3 > budget) |
281 two_pass = intra = 0; | 283 two_pass = intra = 0; |
282 | 284 |
283 /* Encode the global flags using a simple probability model | |
284 (first symbols in the stream) */ | |
285 | |
286 max_decay = QCONST16(16.f,DB_SHIFT); | 285 max_decay = QCONST16(16.f,DB_SHIFT); |
287 if (end-start>10) | 286 if (end-start>10) |
288 { | 287 { |
289 #ifdef FIXED_POINT | 288 #ifdef FIXED_POINT |
290 max_decay = MIN32(max_decay, SHL32(EXTEND32(nbAvailableBytes),DB_SHIFT-3))
; | 289 max_decay = MIN32(max_decay, SHL32(EXTEND32(nbAvailableBytes),DB_SHIFT-3))
; |
291 #else | 290 #else |
292 max_decay = MIN32(max_decay, .125f*nbAvailableBytes); | 291 max_decay = MIN32(max_decay, .125f*nbAvailableBytes); |
293 #endif | 292 #endif |
294 } | 293 } |
| 294 if (lfe) |
| 295 max_decay=3; |
295 enc_start_state = *enc; | 296 enc_start_state = *enc; |
296 | 297 |
297 ALLOC(oldEBands_intra, C*m->nbEBands, opus_val16); | 298 ALLOC(oldEBands_intra, C*m->nbEBands, opus_val16); |
298 ALLOC(error_intra, C*m->nbEBands, opus_val16); | 299 ALLOC(error_intra, C*m->nbEBands, opus_val16); |
299 OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands); | 300 OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands); |
300 | 301 |
301 if (two_pass || intra) | 302 if (two_pass || intra) |
302 { | 303 { |
303 badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra
, budget, | 304 badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra
, budget, |
304 tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay); | 305 tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lf
e); |
305 } | 306 } |
306 | 307 |
307 if (!intra) | 308 if (!intra) |
308 { | 309 { |
309 unsigned char *intra_buf; | 310 unsigned char *intra_buf; |
310 ec_enc enc_intra_state; | 311 ec_enc enc_intra_state; |
311 opus_int32 tell_intra; | 312 opus_int32 tell_intra; |
312 opus_uint32 nstart_bytes; | 313 opus_uint32 nstart_bytes; |
313 opus_uint32 nintra_bytes; | 314 opus_uint32 nintra_bytes; |
314 int badness2; | 315 int badness2; |
315 VARDECL(unsigned char, intra_bits); | 316 VARDECL(unsigned char, intra_bits); |
316 | 317 |
317 tell_intra = ec_tell_frac(enc); | 318 tell_intra = ec_tell_frac(enc); |
318 | 319 |
319 enc_intra_state = *enc; | 320 enc_intra_state = *enc; |
320 | 321 |
321 nstart_bytes = ec_range_bytes(&enc_start_state); | 322 nstart_bytes = ec_range_bytes(&enc_start_state); |
322 nintra_bytes = ec_range_bytes(&enc_intra_state); | 323 nintra_bytes = ec_range_bytes(&enc_intra_state); |
323 intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes; | 324 intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes; |
324 ALLOC(intra_bits, nintra_bytes-nstart_bytes, unsigned char); | 325 ALLOC(intra_bits, nintra_bytes-nstart_bytes, unsigned char); |
325 /* Copy bits from intra bit-stream */ | 326 /* Copy bits from intra bit-stream */ |
326 OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes); | 327 OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes); |
327 | 328 |
328 *enc = enc_start_state; | 329 *enc = enc_start_state; |
329 | 330 |
330 badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budg
et, | 331 badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budg
et, |
331 tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay); | 332 tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe)
; |
332 | 333 |
333 if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_in
t32)ec_tell_frac(enc))+intra_bias > tell_intra))) | 334 if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_in
t32)ec_tell_frac(enc))+intra_bias > tell_intra))) |
334 { | 335 { |
335 *enc = enc_intra_state; | 336 *enc = enc_intra_state; |
336 /* Copy intra bits to bit-stream */ | 337 /* Copy intra bits to bit-stream */ |
337 OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes); | 338 OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes); |
338 OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands); | 339 OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands); |
339 OPUS_COPY(error, error_intra, C*m->nbEBands); | 340 OPUS_COPY(error, error_intra, C*m->nbEBands); |
340 intra = 1; | 341 intra = 1; |
341 } | 342 } |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 #else | 529 #else |
529 offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384); | 530 offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384); |
530 #endif | 531 #endif |
531 oldEBands[i+c*m->nbEBands] += offset; | 532 oldEBands[i+c*m->nbEBands] += offset; |
532 bits_left--; | 533 bits_left--; |
533 } while (++c < C); | 534 } while (++c < C); |
534 } | 535 } |
535 } | 536 } |
536 } | 537 } |
537 | 538 |
538 void log2Amp(const CELTMode *m, int start, int end, | |
539 celt_ener *eBands, const opus_val16 *oldEBands, int C) | |
540 { | |
541 int c, i; | |
542 c=0; | |
543 do { | |
544 for (i=0;i<start;i++) | |
545 eBands[i+c*m->nbEBands] = 0; | |
546 for (;i<end;i++) | |
547 { | |
548 opus_val16 lg = ADD16(oldEBands[i+c*m->nbEBands], | |
549 SHL16((opus_val16)eMeans[i],6)); | |
550 eBands[i+c*m->nbEBands] = PSHR32(celt_exp2(lg),4); | |
551 } | |
552 for (;i<m->nbEBands;i++) | |
553 eBands[i+c*m->nbEBands] = 0; | |
554 } while (++c < C); | |
555 } | |
556 | |
557 void amp2Log2(const CELTMode *m, int effEnd, int end, | 539 void amp2Log2(const CELTMode *m, int effEnd, int end, |
558 celt_ener *bandE, opus_val16 *bandLogE, int C) | 540 celt_ener *bandE, opus_val16 *bandLogE, int C) |
559 { | 541 { |
560 int c, i; | 542 int c, i; |
561 c=0; | 543 c=0; |
562 do { | 544 do { |
563 for (i=0;i<effEnd;i++) | 545 for (i=0;i<effEnd;i++) |
564 bandLogE[i+c*m->nbEBands] = | 546 bandLogE[i+c*m->nbEBands] = |
565 celt_log2(SHL32(bandE[i+c*m->nbEBands],2)) | 547 celt_log2(SHL32(bandE[i+c*m->nbEBands],2)) |
566 - SHL16((opus_val16)eMeans[i],6); | 548 - SHL16((opus_val16)eMeans[i],6); |
567 for (i=effEnd;i<end;i++) | 549 for (i=effEnd;i<end;i++) |
568 bandLogE[c*m->nbEBands+i] = -QCONST16(14.f,DB_SHIFT); | 550 bandLogE[c*m->nbEBands+i] = -QCONST16(14.f,DB_SHIFT); |
569 } while (++c < C); | 551 } while (++c < C); |
570 } | 552 } |
OLD | NEW |