1 /* (C) 2007-2008 Jean-Marc Valin, CSIRO
2 (C) 2008-2009 Gregory Maxwell */
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.
41 #include "stack_alloc.h"
42 #include "os_support.h"
46 const celt_word16_t sqrtC_1[2] = {QCONST16(1.f, 14), QCONST16(1.414214f, 14)};
51 /* Compute the amplitude (sqrt energy) in each of the bands */
52 void compute_band_energies(const CELTMode *m, const celt_sig_t *X, celt_ener_t *bank)
55 const celt_int16_t *eBands = m->eBands;
56 const int C = CHANNELS(m);
60 for (i=0;i<m->nbEBands;i++)
63 celt_word32_t maxval=0;
64 celt_word32_t sum = 0;
67 maxval = MAX32(maxval, X[j+c*N]);
68 maxval = MAX32(maxval, -X[j+c*N]);
69 } while (++j<eBands[i+1]);
73 int shift = celt_ilog2(maxval)-10;
75 sum = MAC16_16(sum, EXTRACT16(VSHR32(X[j+c*N],shift)),
76 EXTRACT16(VSHR32(X[j+c*N],shift)));
77 } while (++j<eBands[i+1]);
78 /* We're adding one here to make damn sure we never end up with a pitch vector that's
79 larger than unity norm */
80 bank[i+c*m->nbEBands] = EPSILON+VSHR32(EXTEND32(celt_sqrt(sum)),-shift);
82 bank[i+c*m->nbEBands] = EPSILON;
84 /*printf ("%f ", bank[i+c*m->nbEBands]);*/
90 /* Normalise each band such that the energy is one. */
91 void normalise_bands(const CELTMode *m, const celt_sig_t * restrict freq, celt_norm_t * restrict X, const celt_ener_t *bank)
94 const celt_int16_t *eBands = m->eBands;
95 const int C = CHANNELS(m);
103 shift = celt_zlog2(bank[i+c*m->nbEBands])-13;
104 E = VSHR32(bank[i+c*m->nbEBands], shift);
105 g = EXTRACT16(celt_rcp(SHL32(E,3)));
107 X[j*C+c] = MULT16_16_Q15(VSHR32(freq[j+c*N],shift-1),g);
108 } while (++j<eBands[i+1]);
109 } while (++i<m->nbEBands);
113 #else /* FIXED_POINT */
114 /* Compute the amplitude (sqrt energy) in each of the bands */
115 void compute_band_energies(const CELTMode *m, const celt_sig_t *X, celt_ener_t *bank)
118 const celt_int16_t *eBands = m->eBands;
119 const int C = CHANNELS(m);
123 for (i=0;i<m->nbEBands;i++)
126 celt_word32_t sum = 1e-10;
127 for (j=eBands[i];j<eBands[i+1];j++)
128 sum += X[j+c*N]*X[j+c*N];
129 bank[i+c*m->nbEBands] = sqrt(sum);
130 /*printf ("%f ", bank[i+c*m->nbEBands]);*/
137 void compute_noise_energies(const CELTMode *m, const celt_sig_t *X, const celt_word16_t *tonality, celt_ener_t *bank)
140 const celt_int16_t *eBands = m->eBands;
141 const int C = CHANNELS(m);
145 for (i=0;i<m->nbEBands;i++)
148 celt_word32_t sum = 1e-10;
149 for (j=eBands[i];j<eBands[i+1];j++)
150 sum += X[j*C+c]*X[j+c*N]*tonality[j];
151 bank[i+c*m->nbEBands] = sqrt(sum);
152 /*printf ("%f ", bank[i+c*m->nbEBands]);*/
159 /* Normalise each band such that the energy is one. */
160 void normalise_bands(const CELTMode *m, const celt_sig_t * restrict freq, celt_norm_t * restrict X, const celt_ener_t *bank)
163 const celt_int16_t *eBands = m->eBands;
164 const int C = CHANNELS(m);
168 for (i=0;i<m->nbEBands;i++)
171 celt_word16_t g = 1.f/(1e-10+bank[i+c*m->nbEBands]);
172 for (j=eBands[i];j<eBands[i+1];j++)
173 X[j*C+c] = freq[j+c*N]*g;
178 #endif /* FIXED_POINT */
180 #ifndef DISABLE_STEREO
181 void renormalise_bands(const CELTMode *m, celt_norm_t * restrict X)
184 const celt_int16_t *eBands = m->eBands;
185 const int C = CHANNELS(m);
189 renormalise_vector(X+C*eBands[i]+c, QCONST16(0.70711f, 15), eBands[i+1]-eBands[i], C);
190 } while (++i<m->nbEBands);
193 #endif /* DISABLE_STEREO */
195 /* De-normalise the energy to produce the synthesis from the unit-energy bands */
196 void denormalise_bands(const CELTMode *m, const celt_norm_t * restrict X, celt_sig_t * restrict freq, const celt_ener_t *bank)
199 const celt_int16_t *eBands = m->eBands;
200 const int C = CHANNELS(m);
203 celt_fatal("denormalise_bands() not implemented for >2 channels");
206 for (i=0;i<m->nbEBands;i++)
209 celt_word32_t g = SHR32(bank[i+c*m->nbEBands],1);
211 freq[j+c*N] = SHL32(MULT16_32_Q15(X[j*C+c], g),2);
212 } while (++j<eBands[i+1]);
214 for (i=eBands[m->nbEBands];i<eBands[m->nbEBands+1];i++)
219 int compute_pitch_gain(const CELTMode *m, const celt_sig_t *X, const celt_sig_t *P, int norm_rate, int *gain_id)
224 const int C = CHANNELS(m);
225 celt_word32_t Sxy=0, Sxx=0, Syy=0;
226 int len = m->pitchEnd;
229 celt_word32_t maxabs=0;
232 maxabs = MAX32(maxabs, ABS32(X[j]));
233 maxabs = MAX32(maxabs, ABS32(P[j]));
235 shift = celt_ilog2(maxabs)-12;
239 delta = PDIV32_16(Q15ONE, len);
242 celt_word16_t gg = Q15ONE;
245 celt_word16_t Xj, Pj;
246 Xj = EXTRACT16(SHR32(X[C*j+c], shift));
247 Pj = MULT16_16_P15(gg,EXTRACT16(SHR32(P[C*j+c], shift)));
248 Sxy = MAC16_16(Sxy, Xj, Pj);
249 Sxx = MAC16_16(Sxx, Pj, Pj);
250 Syy = MAC16_16(Syy, Xj, Xj);
251 gg = SUB16(gg, delta);
256 celt_word32_t num, den;
258 fact = MULT16_16(QCONST16(.04, 14), norm_rate);
259 if (fact < QCONST16(1., 14))
260 fact = QCONST16(1., 14);
262 den = EPSILON+Sxx+MULT16_32_Q15(QCONST16(.03,15),Syy);
263 shift = celt_ilog2(Sxy)-16;
266 g = DIV32(SHL32(SHR32(num,shift),14),SHR32(den,shift));
267 if (Sxy < MULT16_32_Q15(fact, MULT16_16(celt_sqrt(EPSILON+Sxx),celt_sqrt(EPSILON+Syy))))
269 /* This MUST round down so that we don't over-estimate the gain */
270 *gain_id = EXTRACT16(SHR32(MULT16_16(20,(g-QCONST16(.5,14))),14));
274 float fact = .04*norm_rate;
277 g = Sxy/(.1+Sxx+.03*Syy);
278 if (Sxy < .5*fact*celt_sqrt(1+Sxx*Syy))
280 /* This MUST round down so that we don't over-estimate the gain */
281 *gain_id = floor(20*(g-.5));
295 void apply_pitch(const CELTMode *m, celt_sig_t *X, const celt_sig_t *P, int gain_id, int pred)
300 const int C = CHANNELS(m);
301 int len = m->pitchEnd;
303 gain = ADD16(QCONST16(.5,14), MULT16_16_16(QCONST16(.05,14),gain_id));
304 delta = PDIV32_16(gain, len);
311 celt_word16_t gg = gain;
314 X[C*j+c] += SHL(MULT16_32_Q15(gg,P[C*j+c]),1);
315 gg = ADD16(gg, delta);
320 #ifndef DISABLE_STEREO
322 static void stereo_band_mix(const CELTMode *m, celt_norm_t *X, const celt_ener_t *bank, int stereo_mode, int bandID, int dir)
325 const celt_int16_t *eBands = m->eBands;
326 const int C = CHANNELS(m);
328 celt_word16_t a1, a2;
331 /* Do mid-side when not doing intensity stereo */
332 a1 = QCONST16(.70711f,14);
333 a2 = dir*QCONST16(.70711f,14);
335 celt_word16_t left, right;
338 int shift = celt_zlog2(MAX32(bank[i], bank[i+m->nbEBands]))-13;
340 left = VSHR32(bank[i],shift);
341 right = VSHR32(bank[i+m->nbEBands],shift);
342 norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
343 a1 = DIV32_16(SHL32(EXTEND32(left),14),norm);
344 a2 = dir*DIV32_16(SHL32(EXTEND32(right),14),norm);
346 for (j=eBands[i];j<eBands[i+1];j++)
351 X[j*C] = MULT16_16_Q14(a1,l) + MULT16_16_Q14(a2,r);
352 X[j*C+1] = MULT16_16_Q14(a1,r) - MULT16_16_Q14(a2,l);
357 void interleave(celt_norm_t *x, int N)
360 VARDECL(celt_norm_t, tmp);
362 ALLOC(tmp, N, celt_norm_t);
369 x[(i<<1)+1] = tmp[i+(N>>1)];
374 void deinterleave(celt_norm_t *x, int N)
377 VARDECL(celt_norm_t, tmp);
379 ALLOC(tmp, N, celt_norm_t);
386 x[i+(N>>1)] = tmp[(i<<1)+1];
391 #endif /* DISABLE_STEREO */
393 int folding_decision(const CELTMode *m, celt_norm_t *X, celt_word16_t *average, int *last_decision)
397 celt_word32_t ratio = EPSILON;
398 const int C = CHANNELS(m);
399 const celt_int16_t * restrict eBands = m->eBands;
402 for (i=0;i<m->nbEBands;i++)
406 celt_word16_t max_val=EPSILON;
407 celt_word32_t floor_ener=EPSILON;
408 celt_norm_t * restrict x = X+C*eBands[i]+c;
409 N = eBands[i+1]-eBands[i];
412 if (ABS16(x[C*j])>max_val)
414 max_val = ABS16(x[C*j]);
422 floor_ener += x[j]*x[j];
425 floor_ener = QCONST32(1.,28)-MULT16_16(max_val,max_val);
427 floor_ener -= MULT16_16(x[C*(max_i+1)], x[C*(max_i+1)]);
429 floor_ener -= MULT16_16(x[C*(max_i+2)], x[C*(max_i+2)]);
431 floor_ener -= MULT16_16(x[C*(max_i-1)], x[C*(max_i-1)]);
433 floor_ener -= MULT16_16(x[C*(max_i-2)], x[C*(max_i-2)]);
434 floor_ener = MAX32(floor_ener, EPSILON);
439 celt_word16_t den = celt_sqrt(floor_ener);
440 den = MAX32(QCONST16(.02, 15), den);
441 r = DIV32_16(SHL32(EXTEND32(max_val),8),den);
442 ratio = ADD32(ratio, EXTEND32(r));
448 ratio = DIV32_16(ratio, NR);
449 ratio = ADD32(HALF32(ratio), HALF32(*average));
452 *last_decision = (ratio < QCONST16(1.8,8));
454 *last_decision = (ratio < QCONST16(3.,8));
456 *average = EXTRACT16(ratio);
457 return *last_decision;
460 /* Quantisation of the residual */
461 void quant_bands(const CELTMode *m, celt_norm_t * restrict X, const celt_ener_t *bandE, int *pulses, int shortBlocks, int fold, int total_bits, ec_enc *enc)
463 int i, j, remaining_bits, balance;
464 const celt_int16_t * restrict eBands = m->eBands;
465 celt_norm_t * restrict norm;
466 VARDECL(celt_norm_t, _norm);
470 B = shortBlocks ? m->nbShortMdcts : 1;
471 ALLOC(_norm, eBands[m->nbEBands+1], celt_norm_t);
475 for (i=0;i<m->nbEBands;i++)
481 const celt_int16_t * const *BPbits;
483 int curr_balance, curr_bits;
485 N = eBands[i+1]-eBands[i];
488 tell = ec_enc_tell(enc, BITRES);
491 remaining_bits = (total_bits<<BITRES)-tell-1;
492 curr_balance = (m->nbEBands-i);
493 if (curr_balance > 3)
495 curr_balance = balance / curr_balance;
496 q = bits2pulses(m, BPbits[i], N, pulses[i]+curr_balance);
497 curr_bits = pulses2bits(BPbits[i], N, q);
498 remaining_bits -= curr_bits;
499 while (remaining_bits < 0 && q > 0)
501 remaining_bits += curr_bits;
503 curr_bits = pulses2bits(BPbits[i], N, q);
504 remaining_bits -= curr_bits;
506 balance += pulses[i] + tell;
508 n = SHL16(celt_sqrt(eBands[i+1]-eBands[i]),11);
512 int spread = fold ? B : 0;
513 alg_quant(X+eBands[i], eBands[i+1]-eBands[i], q, spread, enc);
515 intra_fold(m, eBands[i+1]-eBands[i], norm, X+eBands[i], eBands[i], B);
517 for (j=eBands[i];j<eBands[i+1];j++)
518 norm[j] = MULT16_16_Q15(n,X[j]);
523 #ifndef DISABLE_STEREO
525 void quant_bands_stereo(const CELTMode *m, celt_norm_t * restrict X, const celt_ener_t *bandE, int *pulses, int shortBlocks, int fold, int total_bits, ec_enc *enc)
527 int i, j, remaining_bits, balance;
528 const celt_int16_t * restrict eBands = m->eBands;
529 celt_norm_t * restrict norm;
530 VARDECL(celt_norm_t, _norm);
531 const int C = CHANNELS(m);
533 celt_word16_t mid, side;
536 B = shortBlocks ? m->nbShortMdcts : 1;
537 ALLOC(_norm, eBands[m->nbEBands+1], celt_norm_t);
541 for (i=0;i<m->nbEBands;i++)
547 const celt_int16_t * const *BPbits;
550 int curr_balance, curr_bits;
551 int imid, iside, itheta;
552 int mbits, sbits, delta;
557 N = eBands[i+1]-eBands[i];
558 tell = ec_enc_tell(enc, BITRES);
561 remaining_bits = (total_bits<<BITRES)-tell-1;
562 curr_balance = (m->nbEBands-i);
563 if (curr_balance > 3)
565 curr_balance = balance / curr_balance;
566 b = IMIN(remaining_bits+1,pulses[i]+curr_balance);
570 qb = (b-2*(N-1)*(QTHETA_OFFSET-log2_frac(N,BITRES)))/(32*(N-1));
571 if (qb > (b>>BITRES)-1)
578 stereo_band_mix(m, X, bandE, qb==0, i, 1);
580 mid = renormalise_vector(X+C*eBands[i], Q15ONE, N, C);
581 side = renormalise_vector(X+C*eBands[i]+1, Q15ONE, N, C);
583 itheta = MULT16_16_Q15(QCONST16(0.63662,15),celt_atan2p(side, mid));
585 itheta = floor(.5+16384*0.63662*atan2(side,mid));
587 qalloc = log2_frac((1<<qb)+1,BITRES);
594 itheta = (itheta+(1<<shift>>1))>>shift;
595 ec_enc_uint(enc, itheta, (1<<qb)+1);
603 } else if (itheta == 16384)
609 imid = bitexact_cos(itheta);
610 iside = bitexact_cos(16384-itheta);
611 delta = (N-1)*(log2_frac(iside,BITRES+2)-log2_frac(imid,BITRES+2))>>2;
613 n = SHL16(celt_sqrt((eBands[i+1]-eBands[i])),11);
619 celt_norm_t v[2], w[2];
620 celt_norm_t *x2 = X+C*eBands[i];
623 if (itheta != 0 && itheta != 16384)
626 c = itheta > 8192 ? 1 : 0;
633 q1 = bits2pulses(m, BPbits[i], N, mbits);
634 curr_bits = pulses2bits(BPbits[i], N, q1)+qalloc+sbits;
635 remaining_bits -= curr_bits;
636 while (remaining_bits < 0 && q1 > 0)
638 remaining_bits += curr_bits;
640 curr_bits = pulses2bits(BPbits[i], N, q1)+qalloc;
641 remaining_bits -= curr_bits;
646 int spread = fold ? B : 0;
647 alg_quant(v, N, q1, spread, enc);
649 v[0] = QCONST16(1.f, 14);
654 if (v[0]*w[1] - v[1]*w[0] > 0)
658 ec_enc_bits(enc, sign==1, 1);
678 mbits = (b-qalloc/2-delta)/2;
679 if (mbits > b-qalloc)
683 sbits = b-qalloc-mbits;
684 q1 = bits2pulses(m, BPbits[i], N, mbits);
685 q2 = bits2pulses(m, BPbits[i], N, sbits);
686 curr_bits = pulses2bits(BPbits[i], N, q1)+pulses2bits(BPbits[i], N, q2)+qalloc;
687 remaining_bits -= curr_bits;
688 while (remaining_bits < 0 && (q1 > 0 || q2 > 0))
690 remaining_bits += curr_bits;
694 curr_bits = pulses2bits(BPbits[i], N, q1)+pulses2bits(BPbits[i], N, q2)+qalloc;
697 curr_bits = pulses2bits(BPbits[i], N, q1)+pulses2bits(BPbits[i], N, q2)+qalloc;
699 remaining_bits -= curr_bits;
702 deinterleave(X+C*eBands[i], C*N);
704 int spread = fold ? B : 0;
705 alg_quant(X+C*eBands[i], N, q1, spread, enc);
707 intra_fold(m, eBands[i+1]-eBands[i], norm, X+C*eBands[i], eBands[i], B);
710 int spread = fold ? B : 0;
711 alg_quant(X+C*eBands[i]+N, N, q2, spread, enc);
713 for (j=C*eBands[i]+N;j<C*eBands[i+1];j++)
717 balance += pulses[i] + tell;
723 mid = (1./32768)*imid;
724 side = (1./32768)*iside;
727 norm[eBands[i]+j] = MULT16_16_Q15(n,X[C*eBands[i]+j]);
730 X[C*eBands[i]+j] = MULT16_16_Q15(X[C*eBands[i]+j], mid);
732 X[C*eBands[i]+N+j] = MULT16_16_Q15(X[C*eBands[i]+N+j], side);
734 interleave(X+C*eBands[i], C*N);
737 stereo_band_mix(m, X, bandE, 0, i, -1);
738 renormalise_vector(X+C*eBands[i], Q15ONE, N, C);
739 renormalise_vector(X+C*eBands[i]+1, Q15ONE, N, C);
743 #endif /* DISABLE_STEREO */
745 /* Decoding of the residual */
746 void unquant_bands(const CELTMode *m, celt_norm_t * restrict X, const celt_ener_t *bandE, int *pulses, int shortBlocks, int fold, int total_bits, ec_dec *dec)
748 int i, j, remaining_bits, balance;
749 const celt_int16_t * restrict eBands = m->eBands;
750 celt_norm_t * restrict norm;
751 VARDECL(celt_norm_t, _norm);
755 B = shortBlocks ? m->nbShortMdcts : 1;
756 ALLOC(_norm, eBands[m->nbEBands+1], celt_norm_t);
760 for (i=0;i<m->nbEBands;i++)
766 const celt_int16_t * const *BPbits;
768 int curr_balance, curr_bits;
770 N = eBands[i+1]-eBands[i];
773 tell = ec_dec_tell(dec, BITRES);
776 remaining_bits = (total_bits<<BITRES)-tell-1;
777 curr_balance = (m->nbEBands-i);
778 if (curr_balance > 3)
780 curr_balance = balance / curr_balance;
781 q = bits2pulses(m, BPbits[i], N, pulses[i]+curr_balance);
782 curr_bits = pulses2bits(BPbits[i], N, q);
783 remaining_bits -= curr_bits;
784 while (remaining_bits < 0 && q > 0)
786 remaining_bits += curr_bits;
788 curr_bits = pulses2bits(BPbits[i], N, q);
789 remaining_bits -= curr_bits;
791 balance += pulses[i] + tell;
793 n = SHL16(celt_sqrt(eBands[i+1]-eBands[i]),11);
797 int spread = fold ? B : 0;
798 alg_unquant(X+eBands[i], eBands[i+1]-eBands[i], q, spread, dec);
800 intra_fold(m, eBands[i+1]-eBands[i], norm, X+eBands[i], eBands[i], B);
802 for (j=eBands[i];j<eBands[i+1];j++)
803 norm[j] = MULT16_16_Q15(n,X[j]);
808 #ifndef DISABLE_STEREO
810 void unquant_bands_stereo(const CELTMode *m, celt_norm_t * restrict X, const celt_ener_t *bandE, int *pulses, int shortBlocks, int fold, int total_bits, ec_dec *dec)
812 int i, j, remaining_bits, balance;
813 const celt_int16_t * restrict eBands = m->eBands;
814 celt_norm_t * restrict norm;
815 VARDECL(celt_norm_t, _norm);
816 const int C = CHANNELS(m);
818 celt_word16_t mid, side;
821 B = shortBlocks ? m->nbShortMdcts : 1;
822 ALLOC(_norm, eBands[m->nbEBands+1], celt_norm_t);
826 for (i=0;i<m->nbEBands;i++)
832 const celt_int16_t * const *BPbits;
835 int curr_balance, curr_bits;
836 int imid, iside, itheta;
837 int mbits, sbits, delta;
842 N = eBands[i+1]-eBands[i];
843 tell = ec_dec_tell(dec, BITRES);
846 remaining_bits = (total_bits<<BITRES)-tell-1;
847 curr_balance = (m->nbEBands-i);
848 if (curr_balance > 3)
850 curr_balance = balance / curr_balance;
851 b = IMIN(remaining_bits+1,pulses[i]+curr_balance);
855 qb = (b-2*(N-1)*(QTHETA_OFFSET-log2_frac(N,BITRES)))/(32*(N-1));
856 if (qb > (b>>BITRES)-1)
862 qalloc = log2_frac((1<<qb)+1,BITRES);
869 itheta = ec_dec_uint(dec, (1<<qb)+1);
877 } else if (itheta == 16384)
883 imid = bitexact_cos(itheta);
884 iside = bitexact_cos(16384-itheta);
885 delta = (N-1)*(log2_frac(iside,BITRES+2)-log2_frac(imid,BITRES+2))>>2;
887 n = SHL16(celt_sqrt((eBands[i+1]-eBands[i])),11);
893 celt_norm_t v[2], w[2];
894 celt_norm_t *x2 = X+C*eBands[i];
897 if (itheta != 0 && itheta != 16384)
900 c = itheta > 8192 ? 1 : 0;
907 q1 = bits2pulses(m, BPbits[i], N, mbits);
908 curr_bits = pulses2bits(BPbits[i], N, q1)+qalloc+sbits;
909 remaining_bits -= curr_bits;
910 while (remaining_bits < 0 && q1 > 0)
912 remaining_bits += curr_bits;
914 curr_bits = pulses2bits(BPbits[i], N, q1)+qalloc;
915 remaining_bits -= curr_bits;
920 int spread = fold ? B : 0;
921 alg_unquant(v, N, q1, spread, dec);
923 v[0] = QCONST16(1.f, 14);
927 sign = 2*ec_dec_bits(dec, 1)-1;
945 mbits = (b-qalloc/2-delta)/2;
946 if (mbits > b-qalloc)
950 sbits = b-qalloc-mbits;
951 q1 = bits2pulses(m, BPbits[i], N, mbits);
952 q2 = bits2pulses(m, BPbits[i], N, sbits);
953 curr_bits = pulses2bits(BPbits[i], N, q1)+pulses2bits(BPbits[i], N, q2)+qalloc;
954 remaining_bits -= curr_bits;
955 while (remaining_bits < 0 && (q1 > 0 || q2 > 0))
957 remaining_bits += curr_bits;
961 curr_bits = pulses2bits(BPbits[i], N, q1)+pulses2bits(BPbits[i], N, q2)+qalloc;
964 curr_bits = pulses2bits(BPbits[i], N, q1)+pulses2bits(BPbits[i], N, q2)+qalloc;
966 remaining_bits -= curr_bits;
969 deinterleave(X+C*eBands[i], C*N);
972 int spread = fold ? B : 0;
973 alg_unquant(X+C*eBands[i], N, q1, spread, dec);
975 intra_fold(m, eBands[i+1]-eBands[i], norm, X+C*eBands[i], eBands[i], B);
978 int spread = fold ? B : 0;
979 alg_unquant(X+C*eBands[i]+N, N, q2, spread, dec);
981 for (j=C*eBands[i]+N;j<C*eBands[i+1];j++)
983 /*orthogonalize(X+C*eBands[i], X+C*eBands[i]+N, N);*/
985 balance += pulses[i] + tell;
991 mid = (1./32768)*imid;
992 side = (1./32768)*iside;
995 norm[eBands[i]+j] = MULT16_16_Q15(n,X[C*eBands[i]+j]);
998 X[C*eBands[i]+j] = MULT16_16_Q15(X[C*eBands[i]+j], mid);
1000 X[C*eBands[i]+N+j] = MULT16_16_Q15(X[C*eBands[i]+N+j], side);
1002 interleave(X+C*eBands[i], C*N);
1004 stereo_band_mix(m, X, bandE, 0, i, -1);
1005 renormalise_vector(X+C*eBands[i], Q15ONE, N, C);
1006 renormalise_vector(X+C*eBands[i]+1, Q15ONE, N, C);
1011 #endif /* DISABLE_STEREO */