diff options
author | Nils Wallménius <nils@rockbox.org> | 2013-05-20 22:25:57 +0200 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2013-08-31 08:30:51 +0200 |
commit | 580b307fd791c0997a8831bc800bba87797bfb7e (patch) | |
tree | 807846056f06fd944a750ce41217a877910ebd59 /lib/rbcodec/codecs/libopus/celt/bands.c | |
parent | 74761b70acd96cecc0d35450dd56a98ad9ee7d3d (diff) | |
download | rockbox-580b307fd791c0997a8831bc800bba87797bfb7e.tar.gz rockbox-580b307fd791c0997a8831bc800bba87797bfb7e.zip |
Sync opus codec to upstream git
Sync opus codec to upstream commit
02fed471a4568852d6618e041c4f2af0d7730ee2 (August 30 2013)
This brings in a lot of optimizations but also makes the diff
between our codec and the upstream much smaller as most of our
optimizations have been upstreamed or supeceded.
Speedups across the board for CELT mode files:
64kbps 128kbps
H300 9.82MHz 15.48MHz
c200 4.86MHz 9.63MHz
fuze v1 10.32MHz 15.92MHz
For the silk mode test file (16kbps) arm targets get a speedup
of about 2MHz while the H300 is 7.8MHz slower, likely because it's
now using the pseudostack more rather than the real stack which
is in iram. Patches to get around that are upcomming.
Change-Id: Ifecf963e461c51ac42e09dac1e91bc4bc3b12fa3
Diffstat (limited to 'lib/rbcodec/codecs/libopus/celt/bands.c')
-rw-r--r-- | lib/rbcodec/codecs/libopus/celt/bands.c | 1190 |
1 files changed, 697 insertions, 493 deletions
diff --git a/lib/rbcodec/codecs/libopus/celt/bands.c b/lib/rbcodec/codecs/libopus/celt/bands.c index c7cb0d5500..5c715aff53 100644 --- a/lib/rbcodec/codecs/libopus/celt/bands.c +++ b/lib/rbcodec/codecs/libopus/celt/bands.c | |||
@@ -28,7 +28,7 @@ | |||
28 | */ | 28 | */ |
29 | 29 | ||
30 | #ifdef HAVE_CONFIG_H | 30 | #ifdef HAVE_CONFIG_H |
31 | #include "opus_config.h" | 31 | #include "config.h" |
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | #include <math.h> | 34 | #include <math.h> |
@@ -40,6 +40,23 @@ | |||
40 | #include "os_support.h" | 40 | #include "os_support.h" |
41 | #include "mathops.h" | 41 | #include "mathops.h" |
42 | #include "rate.h" | 42 | #include "rate.h" |
43 | #include "quant_bands.h" | ||
44 | #include "pitch.h" | ||
45 | |||
46 | int hysteresis_decision(opus_val16 val, const opus_val16 *thresholds, const opus_val16 *hysteresis, int N, int prev) | ||
47 | { | ||
48 | int i; | ||
49 | for (i=0;i<N;i++) | ||
50 | { | ||
51 | if (val < thresholds[i]) | ||
52 | break; | ||
53 | } | ||
54 | if (i>prev && val < thresholds[prev]+hysteresis[prev]) | ||
55 | i=prev; | ||
56 | if (i<prev && val > thresholds[prev-1]-hysteresis[prev-1]) | ||
57 | i=prev; | ||
58 | return i; | ||
59 | } | ||
43 | 60 | ||
44 | opus_uint32 celt_lcg_rand(opus_uint32 seed) | 61 | opus_uint32 celt_lcg_rand(opus_uint32 seed) |
45 | { | 62 | { |
@@ -174,7 +191,8 @@ void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, cel | |||
174 | #endif | 191 | #endif |
175 | 192 | ||
176 | /* De-normalise the energy to produce the synthesis from the unit-energy bands */ | 193 | /* De-normalise the energy to produce the synthesis from the unit-energy bands */ |
177 | void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, celt_sig * OPUS_RESTRICT freq, const celt_ener *bandE, int end, int C, int M) | 194 | void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, |
195 | celt_sig * OPUS_RESTRICT freq, const opus_val16 *bandLogE, int start, int end, int C, int M) | ||
178 | { | 196 | { |
179 | int i, c, N; | 197 | int i, c, N; |
180 | const opus_int16 *eBands = m->eBands; | 198 | const opus_int16 *eBands = m->eBands; |
@@ -184,18 +202,39 @@ void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, cel | |||
184 | celt_sig * OPUS_RESTRICT f; | 202 | celt_sig * OPUS_RESTRICT f; |
185 | const celt_norm * OPUS_RESTRICT x; | 203 | const celt_norm * OPUS_RESTRICT x; |
186 | f = freq+c*N; | 204 | f = freq+c*N; |
187 | x = X+c*N; | 205 | x = X+c*N+M*eBands[start]; |
188 | for (i=0;i<end;i++) | 206 | for (i=0;i<M*eBands[start];i++) |
207 | *f++ = 0; | ||
208 | for (i=start;i<end;i++) | ||
189 | { | 209 | { |
190 | int j, band_end; | 210 | int j, band_end; |
191 | opus_val32 g = SHR32(bandE[i+c*m->nbEBands],1); | 211 | opus_val16 g; |
212 | opus_val16 lg; | ||
213 | #ifdef FIXED_POINT | ||
214 | int shift; | ||
215 | #endif | ||
192 | j=M*eBands[i]; | 216 | j=M*eBands[i]; |
193 | band_end = M*eBands[i+1]; | 217 | band_end = M*eBands[i+1]; |
218 | lg = ADD16(bandLogE[i+c*m->nbEBands], SHL16((opus_val16)eMeans[i],6)); | ||
219 | #ifdef FIXED_POINT | ||
220 | /* Handle the integer part of the log energy */ | ||
221 | shift = 16-(lg>>DB_SHIFT); | ||
222 | if (shift>31) | ||
223 | { | ||
224 | shift=0; | ||
225 | g=0; | ||
226 | } else { | ||
227 | /* Handle the fractional part. */ | ||
228 | g = celt_exp2_frac(lg&((1<<DB_SHIFT)-1)); | ||
229 | } | ||
230 | #else | ||
231 | g = celt_exp2(lg); | ||
232 | #endif | ||
194 | do { | 233 | do { |
195 | *f++ = SHL32(MULT16_32_Q15(*x, g),2); | 234 | *f++ = SHR32(MULT16_16(*x++, g), shift); |
196 | x++; | ||
197 | } while (++j<band_end); | 235 | } while (++j<band_end); |
198 | } | 236 | } |
237 | celt_assert(start <= end); | ||
199 | for (i=M*eBands[end];i<N;i++) | 238 | for (i=M*eBands[end];i<N;i++) |
200 | *f++ = 0; | 239 | *f++ = 0; |
201 | } while (++c<C); | 240 | } while (++c<C); |
@@ -347,11 +386,7 @@ static void stereo_merge(celt_norm *X, celt_norm *Y, opus_val16 mid, int N) | |||
347 | opus_val32 t, lgain, rgain; | 386 | opus_val32 t, lgain, rgain; |
348 | 387 | ||
349 | /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */ | 388 | /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */ |
350 | for (j=0;j<N;j++) | 389 | dual_inner_prod(Y, X, Y, N, &xp, &side); |
351 | { | ||
352 | xp = MAC16_16(xp, X[j], Y[j]); | ||
353 | side = MAC16_16(side, Y[j], Y[j]); | ||
354 | } | ||
355 | /* Compensating for the mid normalization */ | 390 | /* Compensating for the mid normalization */ |
356 | xp = MULT16_32_Q15(mid, xp); | 391 | xp = MULT16_32_Q15(mid, xp); |
357 | /* mid and side are in Q15, not Q14 like X and Y */ | 392 | /* mid and side are in Q15, not Q14 like X and Y */ |
@@ -487,50 +522,6 @@ int spreading_decision(const CELTMode *m, celt_norm *X, int *average, | |||
487 | } | 522 | } |
488 | #endif | 523 | #endif |
489 | 524 | ||
490 | #ifdef MEASURE_NORM_MSE | ||
491 | |||
492 | float MSE[30] = {0}; | ||
493 | int nbMSEBands = 0; | ||
494 | int MSECount[30] = {0}; | ||
495 | |||
496 | void dump_norm_mse(void) | ||
497 | { | ||
498 | int i; | ||
499 | for (i=0;i<nbMSEBands;i++) | ||
500 | { | ||
501 | printf ("%g ", MSE[i]/MSECount[i]); | ||
502 | } | ||
503 | printf ("\n"); | ||
504 | } | ||
505 | |||
506 | void measure_norm_mse(const CELTMode *m, float *X, float *X0, float *bandE, float *bandE0, int M, int N, int C) | ||
507 | { | ||
508 | static int init = 0; | ||
509 | int i; | ||
510 | if (!init) | ||
511 | { | ||
512 | atexit(dump_norm_mse); | ||
513 | init = 1; | ||
514 | } | ||
515 | for (i=0;i<m->nbEBands;i++) | ||
516 | { | ||
517 | int j; | ||
518 | int c; | ||
519 | float g; | ||
520 | if (bandE0[i]<10 || (C==2 && bandE0[i+m->nbEBands]<1)) | ||
521 | continue; | ||
522 | c=0; do { | ||
523 | g = bandE[i+c*m->nbEBands]/(1e-15+bandE0[i+c*m->nbEBands]); | ||
524 | for (j=M*m->eBands[i];j<M*m->eBands[i+1];j++) | ||
525 | MSE[i] += (g*X[j+c*N]-X0[j+c*N])*(g*X[j+c*N]-X0[j+c*N]); | ||
526 | } while (++c<C); | ||
527 | MSECount[i]+=C; | ||
528 | } | ||
529 | nbMSEBands = m->nbEBands; | ||
530 | } | ||
531 | |||
532 | #endif | ||
533 | |||
534 | /* Indexing table for converting from natural Hadamard to ordery Hadamard | 525 | /* Indexing table for converting from natural Hadamard to ordery Hadamard |
535 | This is essentially a bit-reversed Gray, on top of which we've added | 526 | This is essentially a bit-reversed Gray, on top of which we've added |
536 | an inversion of the order because we want the DC at the end rather than | 527 | an inversion of the order because we want the DC at the end rather than |
@@ -633,289 +624,304 @@ static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo) | |||
633 | return qn; | 624 | return qn; |
634 | } | 625 | } |
635 | 626 | ||
636 | /* This function is responsible for encoding and decoding a band for both | 627 | struct band_ctx { |
637 | the mono and stereo case. Even in the mono case, it can split the band | 628 | int encode; |
638 | in two and transmit the energy difference with the two half-bands. It | 629 | const CELTMode *m; |
639 | can be called recursively so bands can end up being split in 8 parts. */ | 630 | int i; |
640 | static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, celt_norm *Y, | 631 | int intensity; |
641 | int N, int b, int spread, int B, int intensity, int tf_change, celt_norm *lowband, ec_ctx *ec, | 632 | int spread; |
642 | opus_int32 *remaining_bits, int LM, celt_norm *lowband_out, const celt_ener *bandE, int level, | 633 | int tf_change; |
643 | opus_uint32 *seed, opus_val16 gain, celt_norm *lowband_scratch, int fill) | 634 | ec_ctx *ec; |
635 | opus_int32 remaining_bits; | ||
636 | const celt_ener *bandE; | ||
637 | opus_uint32 seed; | ||
638 | }; | ||
639 | |||
640 | struct split_ctx { | ||
641 | int inv; | ||
642 | int imid; | ||
643 | int iside; | ||
644 | int delta; | ||
645 | int itheta; | ||
646 | int qalloc; | ||
647 | }; | ||
648 | |||
649 | static void compute_theta(struct band_ctx *ctx, struct split_ctx *sctx, | ||
650 | celt_norm *X, celt_norm *Y, int N, int *b, int B, int B0, | ||
651 | int LM, | ||
652 | int stereo, int *fill) | ||
644 | { | 653 | { |
645 | const unsigned char *cache; | 654 | int qn; |
646 | int q; | 655 | int itheta=0; |
647 | int curr_bits; | 656 | int delta; |
648 | int stereo, split; | 657 | int imid, iside; |
649 | int imid=0, iside=0; | 658 | int qalloc; |
650 | int N0=N; | 659 | int pulse_cap; |
651 | int N_B=N; | 660 | int offset; |
652 | int N_B0; | 661 | opus_int32 tell; |
653 | int B0=B; | 662 | int inv=0; |
654 | int time_divide=0; | 663 | int encode; |
655 | int recombine=0; | 664 | const CELTMode *m; |
656 | int inv = 0; | 665 | int i; |
657 | opus_val16 mid=0, side=0; | 666 | int intensity; |
658 | int longBlocks; | 667 | ec_ctx *ec; |
659 | unsigned cm=0; | 668 | const celt_ener *bandE; |
660 | #ifdef RESYNTH | 669 | |
661 | int resynth = 1; | 670 | encode = ctx->encode; |
662 | #else | 671 | m = ctx->m; |
663 | int resynth = !encode; | 672 | i = ctx->i; |
664 | #endif | 673 | intensity = ctx->intensity; |
674 | ec = ctx->ec; | ||
675 | bandE = ctx->bandE; | ||
676 | |||
677 | /* Decide on the resolution to give to the split parameter theta */ | ||
678 | pulse_cap = m->logN[i]+LM*(1<<BITRES); | ||
679 | offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET); | ||
680 | qn = compute_qn(N, *b, offset, pulse_cap, stereo); | ||
681 | if (stereo && i>=intensity) | ||
682 | qn = 1; | ||
683 | if (encode) | ||
684 | { | ||
685 | /* theta is the atan() of the ratio between the (normalized) | ||
686 | side and mid. With just that parameter, we can re-scale both | ||
687 | mid and side because we know that 1) they have unit norm and | ||
688 | 2) they are orthogonal. */ | ||
689 | itheta = stereo_itheta(X, Y, stereo, N); | ||
690 | } | ||
691 | tell = ec_tell_frac(ec); | ||
692 | if (qn!=1) | ||
693 | { | ||
694 | if (encode) | ||
695 | itheta = (itheta*qn+8192)>>14; | ||
665 | 696 | ||
666 | longBlocks = B0==1; | 697 | /* Entropy coding of the angle. We use a uniform pdf for the |
698 | time split, a step for stereo, and a triangular one for the rest. */ | ||
699 | if (stereo && N>2) | ||
700 | { | ||
701 | int p0 = 3; | ||
702 | int x = itheta; | ||
703 | int x0 = qn/2; | ||
704 | int ft = p0*(x0+1) + x0; | ||
705 | /* Use a probability of p0 up to itheta=8192 and then use 1 after */ | ||
706 | if (encode) | ||
707 | { | ||
708 | ec_encode(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft); | ||
709 | } else { | ||
710 | int fs; | ||
711 | fs=ec_decode(ec,ft); | ||
712 | if (fs<(x0+1)*p0) | ||
713 | x=fs/p0; | ||
714 | else | ||
715 | x=x0+1+(fs-(x0+1)*p0); | ||
716 | ec_dec_update(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft); | ||
717 | itheta = x; | ||
718 | } | ||
719 | } else if (B0>1 || stereo) { | ||
720 | /* Uniform pdf */ | ||
721 | if (encode) | ||
722 | ec_enc_uint(ec, itheta, qn+1); | ||
723 | else | ||
724 | itheta = ec_dec_uint(ec, qn+1); | ||
725 | } else { | ||
726 | int fs=1, ft; | ||
727 | ft = ((qn>>1)+1)*((qn>>1)+1); | ||
728 | if (encode) | ||
729 | { | ||
730 | int fl; | ||
667 | 731 | ||
668 | N_B /= B; | 732 | fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta; |
669 | N_B0 = N_B; | 733 | fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 : |
734 | ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1); | ||
670 | 735 | ||
671 | split = stereo = Y != NULL; | 736 | ec_encode(ec, fl, fl+fs, ft); |
737 | } else { | ||
738 | /* Triangular pdf */ | ||
739 | int fl=0; | ||
740 | int fm; | ||
741 | fm = ec_decode(ec, ft); | ||
672 | 742 | ||
673 | /* Special case for one sample */ | 743 | if (fm < ((qn>>1)*((qn>>1) + 1)>>1)) |
674 | if (N==1) | ||
675 | { | ||
676 | int c; | ||
677 | celt_norm *x = X; | ||
678 | c=0; do { | ||
679 | int sign=0; | ||
680 | if (*remaining_bits>=1<<BITRES) | ||
681 | { | ||
682 | if (encode) | ||
683 | { | 744 | { |
684 | sign = x[0]<0; | 745 | itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1; |
685 | ec_enc_bits(ec, sign, 1); | 746 | fs = itheta + 1; |
686 | } else { | 747 | fl = itheta*(itheta + 1)>>1; |
687 | sign = ec_dec_bits(ec, 1); | 748 | } |
749 | else | ||
750 | { | ||
751 | itheta = (2*(qn + 1) | ||
752 | - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1; | ||
753 | fs = qn + 1 - itheta; | ||
754 | fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1); | ||
688 | } | 755 | } |
689 | *remaining_bits -= 1<<BITRES; | ||
690 | b-=1<<BITRES; | ||
691 | } | ||
692 | if (resynth) | ||
693 | x[0] = sign ? -NORM_SCALING : NORM_SCALING; | ||
694 | x = Y; | ||
695 | } while (++c<1+stereo); | ||
696 | if (lowband_out) | ||
697 | lowband_out[0] = SHR16(X[0],4); | ||
698 | return 1; | ||
699 | } | ||
700 | |||
701 | if (!stereo && level == 0) | ||
702 | { | ||
703 | int k; | ||
704 | if (tf_change>0) | ||
705 | recombine = tf_change; | ||
706 | /* Band recombining to increase frequency resolution */ | ||
707 | 756 | ||
708 | if (lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1)) | 757 | ec_dec_update(ec, fl, fl+fs, ft); |
709 | { | 758 | } |
710 | int j; | ||
711 | for (j=0;j<N;j++) | ||
712 | lowband_scratch[j] = lowband[j]; | ||
713 | lowband = lowband_scratch; | ||
714 | } | 759 | } |
715 | 760 | itheta = (opus_int32)itheta*16384/qn; | |
716 | for (k=0;k<recombine;k++) | 761 | if (encode && stereo) |
717 | { | 762 | { |
718 | static const unsigned char bit_interleave_table[16]={ | 763 | if (itheta==0) |
719 | 0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3 | 764 | intensity_stereo(m, X, Y, bandE, i, N); |
720 | }; | 765 | else |
721 | if (encode) | 766 | stereo_split(X, Y, N); |
722 | haar1(X, N>>k, 1<<k); | ||
723 | if (lowband) | ||
724 | haar1(lowband, N>>k, 1<<k); | ||
725 | fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2; | ||
726 | } | 767 | } |
727 | B>>=recombine; | 768 | /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate. |
728 | N_B<<=recombine; | 769 | Let's do that at higher complexity */ |
729 | 770 | } else if (stereo) { | |
730 | /* Increasing the time resolution */ | 771 | if (encode) |
731 | while ((N_B&1) == 0 && tf_change<0) | ||
732 | { | 772 | { |
733 | if (encode) | 773 | inv = itheta > 8192; |
734 | haar1(X, N_B, B); | 774 | if (inv) |
735 | if (lowband) | 775 | { |
736 | haar1(lowband, N_B, B); | 776 | int j; |
737 | fill |= fill<<B; | 777 | for (j=0;j<N;j++) |
738 | B <<= 1; | 778 | Y[j] = -Y[j]; |
739 | N_B >>= 1; | 779 | } |
740 | time_divide++; | 780 | intensity_stereo(m, X, Y, bandE, i, N); |
741 | tf_change++; | ||
742 | } | 781 | } |
743 | B0=B; | 782 | if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES) |
744 | N_B0 = N_B; | ||
745 | |||
746 | /* Reorganize the samples in time order instead of frequency order */ | ||
747 | if (B0>1) | ||
748 | { | 783 | { |
749 | if (encode) | 784 | if (encode) |
750 | deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks); | 785 | ec_enc_bit_logp(ec, inv, 2); |
751 | if (lowband) | 786 | else |
752 | deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks); | 787 | inv = ec_dec_bit_logp(ec, 2); |
753 | } | 788 | } else |
789 | inv = 0; | ||
790 | itheta = 0; | ||
754 | } | 791 | } |
792 | qalloc = ec_tell_frac(ec) - tell; | ||
793 | *b -= qalloc; | ||
755 | 794 | ||
756 | /* If we need 1.5 more bit than we can produce, split the band in two. */ | 795 | if (itheta == 0) |
757 | cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i]; | ||
758 | if (!stereo && LM != -1 && b > cache[cache[0]]+12 && N>2) | ||
759 | { | 796 | { |
760 | N >>= 1; | 797 | imid = 32767; |
761 | Y = X+N; | 798 | iside = 0; |
762 | split = 1; | 799 | *fill &= (1<<B)-1; |
763 | LM -= 1; | 800 | delta = -16384; |
764 | if (B==1) | 801 | } else if (itheta == 16384) |
765 | fill = (fill&1)|(fill<<1); | 802 | { |
766 | B = (B+1)>>1; | 803 | imid = 0; |
804 | iside = 32767; | ||
805 | *fill &= ((1<<B)-1)<<B; | ||
806 | delta = 16384; | ||
807 | } else { | ||
808 | imid = bitexact_cos((opus_int16)itheta); | ||
809 | iside = bitexact_cos((opus_int16)(16384-itheta)); | ||
810 | /* This is the mid vs side allocation that minimizes squared error | ||
811 | in that band. */ | ||
812 | delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid)); | ||
767 | } | 813 | } |
768 | 814 | ||
769 | if (split) | 815 | sctx->inv = inv; |
770 | { | 816 | sctx->imid = imid; |
771 | int qn; | 817 | sctx->iside = iside; |
772 | int itheta=0; | 818 | sctx->delta = delta; |
773 | int mbits, sbits, delta; | 819 | sctx->itheta = itheta; |
774 | int qalloc; | 820 | sctx->qalloc = qalloc; |
775 | int pulse_cap; | 821 | } |
776 | int offset; | 822 | static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y, int b, |
777 | int orig_fill; | 823 | celt_norm *lowband_out) |
778 | opus_int32 tell; | 824 | { |
825 | #ifdef RESYNTH | ||
826 | int resynth = 1; | ||
827 | #else | ||
828 | int resynth = !ctx->encode; | ||
829 | #endif | ||
830 | int c; | ||
831 | int stereo; | ||
832 | celt_norm *x = X; | ||
833 | int encode; | ||
834 | ec_ctx *ec; | ||
779 | 835 | ||
780 | /* Decide on the resolution to give to the split parameter theta */ | 836 | encode = ctx->encode; |
781 | pulse_cap = m->logN[i]+LM*(1<<BITRES); | 837 | ec = ctx->ec; |
782 | offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET); | 838 | |
783 | qn = compute_qn(N, b, offset, pulse_cap, stereo); | 839 | stereo = Y != NULL; |
784 | if (stereo && i>=intensity) | 840 | c=0; do { |
785 | qn = 1; | 841 | int sign=0; |
786 | if (encode) | 842 | if (ctx->remaining_bits>=1<<BITRES) |
787 | { | ||
788 | /* theta is the atan() of the ratio between the (normalized) | ||
789 | side and mid. With just that parameter, we can re-scale both | ||
790 | mid and side because we know that 1) they have unit norm and | ||
791 | 2) they are orthogonal. */ | ||
792 | itheta = stereo_itheta(X, Y, stereo, N); | ||
793 | } | ||
794 | tell = ec_tell_frac(ec); | ||
795 | if (qn!=1) | ||
796 | { | 843 | { |
797 | if (encode) | 844 | if (encode) |
798 | itheta = (itheta*qn+8192)>>14; | ||
799 | |||
800 | /* Entropy coding of the angle. We use a uniform pdf for the | ||
801 | time split, a step for stereo, and a triangular one for the rest. */ | ||
802 | if (stereo && N>2) | ||
803 | { | 845 | { |
804 | int p0 = 3; | 846 | sign = x[0]<0; |
805 | int x = itheta; | 847 | ec_enc_bits(ec, sign, 1); |
806 | int x0 = qn/2; | ||
807 | int ft = p0*(x0+1) + x0; | ||
808 | /* Use a probability of p0 up to itheta=8192 and then use 1 after */ | ||
809 | if (encode) | ||
810 | { | ||
811 | ec_encode(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft); | ||
812 | } else { | ||
813 | int fs; | ||
814 | fs=ec_decode(ec,ft); | ||
815 | if (fs<(x0+1)*p0) | ||
816 | x=fs/p0; | ||
817 | else | ||
818 | x=x0+1+(fs-(x0+1)*p0); | ||
819 | ec_dec_update(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft); | ||
820 | itheta = x; | ||
821 | } | ||
822 | } else if (B0>1 || stereo) { | ||
823 | /* Uniform pdf */ | ||
824 | if (encode) | ||
825 | ec_enc_uint(ec, itheta, qn+1); | ||
826 | else | ||
827 | itheta = ec_dec_uint(ec, qn+1); | ||
828 | } else { | 848 | } else { |
829 | int fs=1, ft; | 849 | sign = ec_dec_bits(ec, 1); |
830 | ft = ((qn>>1)+1)*((qn>>1)+1); | 850 | } |
831 | if (encode) | 851 | ctx->remaining_bits -= 1<<BITRES; |
832 | { | 852 | b-=1<<BITRES; |
833 | int fl; | 853 | } |
854 | if (resynth) | ||
855 | x[0] = sign ? -NORM_SCALING : NORM_SCALING; | ||
856 | x = Y; | ||
857 | } while (++c<1+stereo); | ||
858 | if (lowband_out) | ||
859 | lowband_out[0] = SHR16(X[0],4); | ||
860 | return 1; | ||
861 | } | ||
834 | 862 | ||
835 | fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta; | 863 | /* This function is responsible for encoding and decoding a mono partition. |
836 | fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 : | 864 | It can split the band in two and transmit the energy difference with |
837 | ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1); | 865 | the two half-bands. It can be called recursively so bands can end up being |
866 | split in 8 parts. */ | ||
867 | static unsigned quant_partition(struct band_ctx *ctx, celt_norm *X, | ||
868 | int N, int b, int B, celt_norm *lowband, | ||
869 | int LM, | ||
870 | opus_val16 gain, int fill) | ||
871 | { | ||
872 | const unsigned char *cache; | ||
873 | int q; | ||
874 | int curr_bits; | ||
875 | int imid=0, iside=0; | ||
876 | int N_B=N; | ||
877 | int B0=B; | ||
878 | opus_val16 mid=0, side=0; | ||
879 | unsigned cm=0; | ||
880 | #ifdef RESYNTH | ||
881 | int resynth = 1; | ||
882 | #else | ||
883 | int resynth = !ctx->encode; | ||
884 | #endif | ||
885 | celt_norm *Y=NULL; | ||
886 | int encode; | ||
887 | const CELTMode *m; | ||
888 | int i; | ||
889 | int spread; | ||
890 | ec_ctx *ec; | ||
838 | 891 | ||
839 | ec_encode(ec, fl, fl+fs, ft); | 892 | encode = ctx->encode; |
840 | } else { | 893 | m = ctx->m; |
841 | /* Triangular pdf */ | 894 | i = ctx->i; |
842 | int fl=0; | 895 | spread = ctx->spread; |
843 | int fm; | 896 | ec = ctx->ec; |
844 | fm = ec_decode(ec, ft); | ||
845 | 897 | ||
846 | if (fm < ((qn>>1)*((qn>>1) + 1)>>1)) | 898 | N_B /= B; |
847 | { | ||
848 | itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1; | ||
849 | fs = itheta + 1; | ||
850 | fl = itheta*(itheta + 1)>>1; | ||
851 | } | ||
852 | else | ||
853 | { | ||
854 | itheta = (2*(qn + 1) | ||
855 | - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1; | ||
856 | fs = qn + 1 - itheta; | ||
857 | fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1); | ||
858 | } | ||
859 | 899 | ||
860 | ec_dec_update(ec, fl, fl+fs, ft); | 900 | /* If we need 1.5 more bit than we can produce, split the band in two. */ |
861 | } | 901 | cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i]; |
862 | } | 902 | if (LM != -1 && b > cache[cache[0]]+12 && N>2) |
863 | itheta = (opus_int32)itheta*16384/qn; | 903 | { |
864 | if (encode && stereo) | 904 | int mbits, sbits, delta; |
865 | { | 905 | int itheta; |
866 | if (itheta==0) | 906 | int qalloc; |
867 | intensity_stereo(m, X, Y, bandE, i, N); | 907 | struct split_ctx sctx; |
868 | else | 908 | celt_norm *next_lowband2=NULL; |
869 | stereo_split(X, Y, N); | 909 | opus_int32 rebalance; |
870 | } | ||
871 | /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate. | ||
872 | Let's do that at higher complexity */ | ||
873 | } else if (stereo) { | ||
874 | if (encode) | ||
875 | { | ||
876 | inv = itheta > 8192; | ||
877 | if (inv) | ||
878 | { | ||
879 | int j; | ||
880 | for (j=0;j<N;j++) | ||
881 | Y[j] = -Y[j]; | ||
882 | } | ||
883 | intensity_stereo(m, X, Y, bandE, i, N); | ||
884 | } | ||
885 | if (b>2<<BITRES && *remaining_bits > 2<<BITRES) | ||
886 | { | ||
887 | if (encode) | ||
888 | ec_enc_bit_logp(ec, inv, 2); | ||
889 | else | ||
890 | inv = ec_dec_bit_logp(ec, 2); | ||
891 | } else | ||
892 | inv = 0; | ||
893 | itheta = 0; | ||
894 | } | ||
895 | qalloc = ec_tell_frac(ec) - tell; | ||
896 | b -= qalloc; | ||
897 | 910 | ||
898 | orig_fill = fill; | 911 | N >>= 1; |
899 | if (itheta == 0) | 912 | Y = X+N; |
900 | { | 913 | LM -= 1; |
901 | imid = 32767; | 914 | if (B==1) |
902 | iside = 0; | 915 | fill = (fill&1)|(fill<<1); |
903 | fill &= (1<<B)-1; | 916 | B = (B+1)>>1; |
904 | delta = -16384; | ||
905 | } else if (itheta == 16384) | ||
906 | { | ||
907 | imid = 0; | ||
908 | iside = 32767; | ||
909 | fill &= ((1<<B)-1)<<B; | ||
910 | delta = 16384; | ||
911 | } else { | ||
912 | imid = bitexact_cos(itheta); | ||
913 | iside = bitexact_cos(16384-itheta); | ||
914 | /* This is the mid vs side allocation that minimizes squared error | ||
915 | in that band. */ | ||
916 | delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid)); | ||
917 | } | ||
918 | 917 | ||
918 | compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, | ||
919 | LM, 0, &fill); | ||
920 | imid = sctx.imid; | ||
921 | iside = sctx.iside; | ||
922 | delta = sctx.delta; | ||
923 | itheta = sctx.itheta; | ||
924 | qalloc = sctx.qalloc; | ||
919 | #ifdef FIXED_POINT | 925 | #ifdef FIXED_POINT |
920 | mid = imid; | 926 | mid = imid; |
921 | side = iside; | 927 | side = iside; |
@@ -924,136 +930,59 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c | |||
924 | side = (1.f/32768)*iside; | 930 | side = (1.f/32768)*iside; |
925 | #endif | 931 | #endif |
926 | 932 | ||
927 | /* This is a special case for N=2 that only works for stereo and takes | 933 | /* Give more bits to low-energy MDCTs than they would otherwise deserve */ |
928 | advantage of the fact that mid and side are orthogonal to encode | 934 | if (B0>1 && (itheta&0x3fff)) |
929 | the side with just one bit. */ | ||
930 | if (N==2 && stereo) | ||
931 | { | 935 | { |
932 | int c; | 936 | if (itheta > 8192) |
933 | int sign=0; | 937 | /* Rough approximation for pre-echo masking */ |
934 | celt_norm *x2, *y2; | 938 | delta -= delta>>(4-LM); |
935 | mbits = b; | ||
936 | sbits = 0; | ||
937 | /* Only need one bit for the side */ | ||
938 | if (itheta != 0 && itheta != 16384) | ||
939 | sbits = 1<<BITRES; | ||
940 | mbits -= sbits; | ||
941 | c = itheta > 8192; | ||
942 | *remaining_bits -= qalloc+sbits; | ||
943 | |||
944 | x2 = c ? Y : X; | ||
945 | y2 = c ? X : Y; | ||
946 | if (sbits) | ||
947 | { | ||
948 | if (encode) | ||
949 | { | ||
950 | /* Here we only need to encode a sign for the side */ | ||
951 | sign = x2[0]*y2[1] - x2[1]*y2[0] < 0; | ||
952 | ec_enc_bits(ec, sign, 1); | ||
953 | } else { | ||
954 | sign = ec_dec_bits(ec, 1); | ||
955 | } | ||
956 | } | ||
957 | sign = 1-2*sign; | ||
958 | /* We use orig_fill here because we want to fold the side, but if | ||
959 | itheta==16384, we'll have cleared the low bits of fill. */ | ||
960 | cm = quant_band(encode, m, i, x2, NULL, N, mbits, spread, B, intensity, tf_change, lowband, ec, remaining_bits, LM, lowband_out, NULL, level, seed, gain, lowband_scratch, orig_fill); | ||
961 | /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse), | ||
962 | and there's no need to worry about mixing with the other channel. */ | ||
963 | y2[0] = -sign*x2[1]; | ||
964 | y2[1] = sign*x2[0]; | ||
965 | if (resynth) | ||
966 | { | ||
967 | celt_norm tmp; | ||
968 | X[0] = MULT16_16_Q15(mid, X[0]); | ||
969 | X[1] = MULT16_16_Q15(mid, X[1]); | ||
970 | Y[0] = MULT16_16_Q15(side, Y[0]); | ||
971 | Y[1] = MULT16_16_Q15(side, Y[1]); | ||
972 | tmp = X[0]; | ||
973 | X[0] = SUB16(tmp,Y[0]); | ||
974 | Y[0] = ADD16(tmp,Y[0]); | ||
975 | tmp = X[1]; | ||
976 | X[1] = SUB16(tmp,Y[1]); | ||
977 | Y[1] = ADD16(tmp,Y[1]); | ||
978 | } | ||
979 | } else { | ||
980 | /* "Normal" split code */ | ||
981 | celt_norm *next_lowband2=NULL; | ||
982 | celt_norm *next_lowband_out1=NULL; | ||
983 | int next_level=0; | ||
984 | opus_int32 rebalance; | ||
985 | |||
986 | /* Give more bits to low-energy MDCTs than they would otherwise deserve */ | ||
987 | if (B0>1 && !stereo && (itheta&0x3fff)) | ||
988 | { | ||
989 | if (itheta > 8192) | ||
990 | /* Rough approximation for pre-echo masking */ | ||
991 | delta -= delta>>(4-LM); | ||
992 | else | ||
993 | /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */ | ||
994 | delta = IMIN(0, delta + (N<<BITRES>>(5-LM))); | ||
995 | } | ||
996 | mbits = IMAX(0, IMIN(b, (b-delta)/2)); | ||
997 | sbits = b-mbits; | ||
998 | *remaining_bits -= qalloc; | ||
999 | |||
1000 | if (lowband && !stereo) | ||
1001 | next_lowband2 = lowband+N; /* >32-bit split case */ | ||
1002 | |||
1003 | /* Only stereo needs to pass on lowband_out. Otherwise, it's | ||
1004 | handled at the end */ | ||
1005 | if (stereo) | ||
1006 | next_lowband_out1 = lowband_out; | ||
1007 | else | 939 | else |
1008 | next_level = level+1; | 940 | /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */ |
1009 | 941 | delta = IMIN(0, delta + (N<<BITRES>>(5-LM))); | |
1010 | rebalance = *remaining_bits; | ||
1011 | if (mbits >= sbits) | ||
1012 | { | ||
1013 | /* In stereo mode, we do not apply a scaling to the mid because we need the normalized | ||
1014 | mid for folding later */ | ||
1015 | cm = quant_band(encode, m, i, X, NULL, N, mbits, spread, B, intensity, tf_change, | ||
1016 | lowband, ec, remaining_bits, LM, next_lowband_out1, | ||
1017 | NULL, next_level, seed, stereo ? Q15ONE : MULT16_16_P15(gain,mid), lowband_scratch, fill); | ||
1018 | rebalance = mbits - (rebalance-*remaining_bits); | ||
1019 | if (rebalance > 3<<BITRES && itheta!=0) | ||
1020 | sbits += rebalance - (3<<BITRES); | ||
1021 | |||
1022 | /* For a stereo split, the high bits of fill are always zero, so no | ||
1023 | folding will be done to the side. */ | ||
1024 | cm |= quant_band(encode, m, i, Y, NULL, N, sbits, spread, B, intensity, tf_change, | ||
1025 | next_lowband2, ec, remaining_bits, LM, NULL, | ||
1026 | NULL, next_level, seed, MULT16_16_P15(gain,side), NULL, fill>>B)<<((B0>>1)&(stereo-1)); | ||
1027 | } else { | ||
1028 | /* For a stereo split, the high bits of fill are always zero, so no | ||
1029 | folding will be done to the side. */ | ||
1030 | cm = quant_band(encode, m, i, Y, NULL, N, sbits, spread, B, intensity, tf_change, | ||
1031 | next_lowband2, ec, remaining_bits, LM, NULL, | ||
1032 | NULL, next_level, seed, MULT16_16_P15(gain,side), NULL, fill>>B)<<((B0>>1)&(stereo-1)); | ||
1033 | rebalance = sbits - (rebalance-*remaining_bits); | ||
1034 | if (rebalance > 3<<BITRES && itheta!=16384) | ||
1035 | mbits += rebalance - (3<<BITRES); | ||
1036 | /* In stereo mode, we do not apply a scaling to the mid because we need the normalized | ||
1037 | mid for folding later */ | ||
1038 | cm |= quant_band(encode, m, i, X, NULL, N, mbits, spread, B, intensity, tf_change, | ||
1039 | lowband, ec, remaining_bits, LM, next_lowband_out1, | ||
1040 | NULL, next_level, seed, stereo ? Q15ONE : MULT16_16_P15(gain,mid), lowband_scratch, fill); | ||
1041 | } | ||
1042 | } | 942 | } |
943 | mbits = IMAX(0, IMIN(b, (b-delta)/2)); | ||
944 | sbits = b-mbits; | ||
945 | ctx->remaining_bits -= qalloc; | ||
946 | |||
947 | if (lowband) | ||
948 | next_lowband2 = lowband+N; /* >32-bit split case */ | ||
1043 | 949 | ||
950 | rebalance = ctx->remaining_bits; | ||
951 | if (mbits >= sbits) | ||
952 | { | ||
953 | cm = quant_partition(ctx, X, N, mbits, B, | ||
954 | lowband, LM, | ||
955 | MULT16_16_P15(gain,mid), fill); | ||
956 | rebalance = mbits - (rebalance-ctx->remaining_bits); | ||
957 | if (rebalance > 3<<BITRES && itheta!=0) | ||
958 | sbits += rebalance - (3<<BITRES); | ||
959 | cm |= quant_partition(ctx, Y, N, sbits, B, | ||
960 | next_lowband2, LM, | ||
961 | MULT16_16_P15(gain,side), fill>>B)<<(B0>>1); | ||
962 | } else { | ||
963 | cm = quant_partition(ctx, Y, N, sbits, B, | ||
964 | next_lowband2, LM, | ||
965 | MULT16_16_P15(gain,side), fill>>B)<<(B0>>1); | ||
966 | rebalance = sbits - (rebalance-ctx->remaining_bits); | ||
967 | if (rebalance > 3<<BITRES && itheta!=16384) | ||
968 | mbits += rebalance - (3<<BITRES); | ||
969 | cm |= quant_partition(ctx, X, N, mbits, B, | ||
970 | lowband, LM, | ||
971 | MULT16_16_P15(gain,mid), fill); | ||
972 | } | ||
1044 | } else { | 973 | } else { |
1045 | /* This is the basic no-split case */ | 974 | /* This is the basic no-split case */ |
1046 | q = bits2pulses(m, i, LM, b); | 975 | q = bits2pulses(m, i, LM, b); |
1047 | curr_bits = pulses2bits(m, i, LM, q); | 976 | curr_bits = pulses2bits(m, i, LM, q); |
1048 | *remaining_bits -= curr_bits; | 977 | ctx->remaining_bits -= curr_bits; |
1049 | 978 | ||
1050 | /* Ensures we can never bust the budget */ | 979 | /* Ensures we can never bust the budget */ |
1051 | while (*remaining_bits < 0 && q > 0) | 980 | while (ctx->remaining_bits < 0 && q > 0) |
1052 | { | 981 | { |
1053 | *remaining_bits += curr_bits; | 982 | ctx->remaining_bits += curr_bits; |
1054 | q--; | 983 | q--; |
1055 | curr_bits = pulses2bits(m, i, LM, q); | 984 | curr_bits = pulses2bits(m, i, LM, q); |
1056 | *remaining_bits -= curr_bits; | 985 | ctx->remaining_bits -= curr_bits; |
1057 | } | 986 | } |
1058 | 987 | ||
1059 | if (q!=0) | 988 | if (q!=0) |
@@ -1077,7 +1006,7 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c | |||
1077 | if (resynth) | 1006 | if (resynth) |
1078 | { | 1007 | { |
1079 | unsigned cm_mask; | 1008 | unsigned cm_mask; |
1080 | /*B can be as large as 16, so this shift might overflow an int on a | 1009 | /* B can be as large as 16, so this shift might overflow an int on a |
1081 | 16-bit platform; use a long to get defined behavior.*/ | 1010 | 16-bit platform; use a long to get defined behavior.*/ |
1082 | cm_mask = (unsigned)(1UL<<B)-1; | 1011 | cm_mask = (unsigned)(1UL<<B)-1; |
1083 | fill &= cm_mask; | 1012 | fill &= cm_mask; |
@@ -1091,8 +1020,8 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c | |||
1091 | /* Noise */ | 1020 | /* Noise */ |
1092 | for (j=0;j<N;j++) | 1021 | for (j=0;j<N;j++) |
1093 | { | 1022 | { |
1094 | *seed = celt_lcg_rand(*seed); | 1023 | ctx->seed = celt_lcg_rand(ctx->seed); |
1095 | X[j] = (celt_norm)((opus_int32)*seed>>20); | 1024 | X[j] = (celt_norm)((opus_int32)ctx->seed>>20); |
1096 | } | 1025 | } |
1097 | cm = cm_mask; | 1026 | cm = cm_mask; |
1098 | } else { | 1027 | } else { |
@@ -1100,10 +1029,10 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c | |||
1100 | for (j=0;j<N;j++) | 1029 | for (j=0;j<N;j++) |
1101 | { | 1030 | { |
1102 | opus_val16 tmp; | 1031 | opus_val16 tmp; |
1103 | *seed = celt_lcg_rand(*seed); | 1032 | ctx->seed = celt_lcg_rand(ctx->seed); |
1104 | /* About 48 dB below the "normal" folding level */ | 1033 | /* About 48 dB below the "normal" folding level */ |
1105 | tmp = QCONST16(1.0f/256, 10); | 1034 | tmp = QCONST16(1.0f/256, 10); |
1106 | tmp = (*seed)&0x8000 ? tmp : -tmp; | 1035 | tmp = (ctx->seed)&0x8000 ? tmp : -tmp; |
1107 | X[j] = lowband[j]+tmp; | 1036 | X[j] = lowband[j]+tmp; |
1108 | } | 1037 | } |
1109 | cm = fill; | 1038 | cm = fill; |
@@ -1114,64 +1043,307 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c | |||
1114 | } | 1043 | } |
1115 | } | 1044 | } |
1116 | 1045 | ||
1046 | return cm; | ||
1047 | } | ||
1048 | |||
1049 | |||
1050 | /* This function is responsible for encoding and decoding a band for the mono case. */ | ||
1051 | static unsigned quant_band(struct band_ctx *ctx, celt_norm *X, | ||
1052 | int N, int b, int B, celt_norm *lowband, | ||
1053 | int LM, celt_norm *lowband_out, | ||
1054 | opus_val16 gain, celt_norm *lowband_scratch, int fill) | ||
1055 | { | ||
1056 | int N0=N; | ||
1057 | int N_B=N; | ||
1058 | int N_B0; | ||
1059 | int B0=B; | ||
1060 | int time_divide=0; | ||
1061 | int recombine=0; | ||
1062 | int longBlocks; | ||
1063 | unsigned cm=0; | ||
1064 | #ifdef RESYNTH | ||
1065 | int resynth = 1; | ||
1066 | #else | ||
1067 | int resynth = !ctx->encode; | ||
1068 | #endif | ||
1069 | int k; | ||
1070 | int encode; | ||
1071 | int tf_change; | ||
1072 | |||
1073 | encode = ctx->encode; | ||
1074 | tf_change = ctx->tf_change; | ||
1075 | |||
1076 | longBlocks = B0==1; | ||
1077 | |||
1078 | N_B /= B; | ||
1079 | N_B0 = N_B; | ||
1080 | |||
1081 | /* Special case for one sample */ | ||
1082 | if (N==1) | ||
1083 | { | ||
1084 | return quant_band_n1(ctx, X, NULL, b, lowband_out); | ||
1085 | } | ||
1086 | |||
1087 | if (tf_change>0) | ||
1088 | recombine = tf_change; | ||
1089 | /* Band recombining to increase frequency resolution */ | ||
1090 | |||
1091 | if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1)) | ||
1092 | { | ||
1093 | int j; | ||
1094 | for (j=0;j<N;j++) | ||
1095 | lowband_scratch[j] = lowband[j]; | ||
1096 | lowband = lowband_scratch; | ||
1097 | } | ||
1098 | |||
1099 | for (k=0;k<recombine;k++) | ||
1100 | { | ||
1101 | static const unsigned char bit_interleave_table[16]={ | ||
1102 | 0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3 | ||
1103 | }; | ||
1104 | if (encode) | ||
1105 | haar1(X, N>>k, 1<<k); | ||
1106 | if (lowband) | ||
1107 | haar1(lowband, N>>k, 1<<k); | ||
1108 | fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2; | ||
1109 | } | ||
1110 | B>>=recombine; | ||
1111 | N_B<<=recombine; | ||
1112 | |||
1113 | /* Increasing the time resolution */ | ||
1114 | while ((N_B&1) == 0 && tf_change<0) | ||
1115 | { | ||
1116 | if (encode) | ||
1117 | haar1(X, N_B, B); | ||
1118 | if (lowband) | ||
1119 | haar1(lowband, N_B, B); | ||
1120 | fill |= fill<<B; | ||
1121 | B <<= 1; | ||
1122 | N_B >>= 1; | ||
1123 | time_divide++; | ||
1124 | tf_change++; | ||
1125 | } | ||
1126 | B0=B; | ||
1127 | N_B0 = N_B; | ||
1128 | |||
1129 | /* Reorganize the samples in time order instead of frequency order */ | ||
1130 | if (B0>1) | ||
1131 | { | ||
1132 | if (encode) | ||
1133 | deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks); | ||
1134 | if (lowband) | ||
1135 | deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks); | ||
1136 | } | ||
1137 | |||
1138 | cm = quant_partition(ctx, X, N, b, B, lowband, | ||
1139 | LM, gain, fill); | ||
1140 | |||
1117 | /* This code is used by the decoder and by the resynthesis-enabled encoder */ | 1141 | /* This code is used by the decoder and by the resynthesis-enabled encoder */ |
1118 | if (resynth) | 1142 | if (resynth) |
1119 | { | 1143 | { |
1120 | if (stereo) | 1144 | /* Undo the sample reorganization going from time order to frequency order */ |
1145 | if (B0>1) | ||
1146 | interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks); | ||
1147 | |||
1148 | /* Undo time-freq changes that we did earlier */ | ||
1149 | N_B = N_B0; | ||
1150 | B = B0; | ||
1151 | for (k=0;k<time_divide;k++) | ||
1121 | { | 1152 | { |
1122 | if (N!=2) | 1153 | B >>= 1; |
1123 | stereo_merge(X, Y, mid, N); | 1154 | N_B <<= 1; |
1124 | if (inv) | 1155 | cm |= cm>>B; |
1125 | { | 1156 | haar1(X, N_B, B); |
1126 | int j; | 1157 | } |
1127 | for (j=0;j<N;j++) | 1158 | |
1128 | Y[j] = -Y[j]; | 1159 | for (k=0;k<recombine;k++) |
1129 | } | ||
1130 | } else if (level == 0) | ||
1131 | { | 1160 | { |
1132 | int k; | 1161 | static const unsigned char bit_deinterleave_table[16]={ |
1162 | 0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F, | ||
1163 | 0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF | ||
1164 | }; | ||
1165 | cm = bit_deinterleave_table[cm]; | ||
1166 | haar1(X, N0>>k, 1<<k); | ||
1167 | } | ||
1168 | B<<=recombine; | ||
1133 | 1169 | ||
1134 | /* Undo the sample reorganization going from time order to frequency order */ | 1170 | /* Scale output for later folding */ |
1135 | if (B0>1) | 1171 | if (lowband_out) |
1136 | interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks); | 1172 | { |
1173 | int j; | ||
1174 | opus_val16 n; | ||
1175 | n = celt_sqrt(SHL32(EXTEND32(N0),22)); | ||
1176 | for (j=0;j<N0;j++) | ||
1177 | lowband_out[j] = MULT16_16_Q15(n,X[j]); | ||
1178 | } | ||
1179 | cm &= (1<<B)-1; | ||
1180 | } | ||
1181 | return cm; | ||
1182 | } | ||
1137 | 1183 | ||
1138 | /* Undo time-freq changes that we did earlier */ | ||
1139 | N_B = N_B0; | ||
1140 | B = B0; | ||
1141 | for (k=0;k<time_divide;k++) | ||
1142 | { | ||
1143 | B >>= 1; | ||
1144 | N_B <<= 1; | ||
1145 | cm |= cm>>B; | ||
1146 | haar1(X, N_B, B); | ||
1147 | } | ||
1148 | 1184 | ||
1149 | for (k=0;k<recombine;k++) | 1185 | /* This function is responsible for encoding and decoding a band for the stereo case. */ |
1150 | { | 1186 | static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm *Y, |
1151 | static const unsigned char bit_deinterleave_table[16]={ | 1187 | int N, int b, int B, celt_norm *lowband, |
1152 | 0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F, | 1188 | int LM, celt_norm *lowband_out, |
1153 | 0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF | 1189 | celt_norm *lowband_scratch, int fill) |
1154 | }; | 1190 | { |
1155 | cm = bit_deinterleave_table[cm]; | 1191 | int imid=0, iside=0; |
1156 | haar1(X, N0>>k, 1<<k); | 1192 | int inv = 0; |
1157 | } | 1193 | opus_val16 mid=0, side=0; |
1158 | B<<=recombine; | 1194 | unsigned cm=0; |
1195 | #ifdef RESYNTH | ||
1196 | int resynth = 1; | ||
1197 | #else | ||
1198 | int resynth = !ctx->encode; | ||
1199 | #endif | ||
1200 | int mbits, sbits, delta; | ||
1201 | int itheta; | ||
1202 | int qalloc; | ||
1203 | struct split_ctx sctx; | ||
1204 | int orig_fill; | ||
1205 | int encode; | ||
1206 | ec_ctx *ec; | ||
1207 | |||
1208 | encode = ctx->encode; | ||
1209 | ec = ctx->ec; | ||
1210 | |||
1211 | /* Special case for one sample */ | ||
1212 | if (N==1) | ||
1213 | { | ||
1214 | return quant_band_n1(ctx, X, Y, b, lowband_out); | ||
1215 | } | ||
1216 | |||
1217 | orig_fill = fill; | ||
1218 | |||
1219 | compute_theta(ctx, &sctx, X, Y, N, &b, B, B, | ||
1220 | LM, 1, &fill); | ||
1221 | inv = sctx.inv; | ||
1222 | imid = sctx.imid; | ||
1223 | iside = sctx.iside; | ||
1224 | delta = sctx.delta; | ||
1225 | itheta = sctx.itheta; | ||
1226 | qalloc = sctx.qalloc; | ||
1227 | #ifdef FIXED_POINT | ||
1228 | mid = imid; | ||
1229 | side = iside; | ||
1230 | #else | ||
1231 | mid = (1.f/32768)*imid; | ||
1232 | side = (1.f/32768)*iside; | ||
1233 | #endif | ||
1159 | 1234 | ||
1160 | /* Scale output for later folding */ | 1235 | /* This is a special case for N=2 that only works for stereo and takes |
1161 | if (lowband_out) | 1236 | advantage of the fact that mid and side are orthogonal to encode |
1237 | the side with just one bit. */ | ||
1238 | if (N==2) | ||
1239 | { | ||
1240 | int c; | ||
1241 | int sign=0; | ||
1242 | celt_norm *x2, *y2; | ||
1243 | mbits = b; | ||
1244 | sbits = 0; | ||
1245 | /* Only need one bit for the side. */ | ||
1246 | if (itheta != 0 && itheta != 16384) | ||
1247 | sbits = 1<<BITRES; | ||
1248 | mbits -= sbits; | ||
1249 | c = itheta > 8192; | ||
1250 | ctx->remaining_bits -= qalloc+sbits; | ||
1251 | |||
1252 | x2 = c ? Y : X; | ||
1253 | y2 = c ? X : Y; | ||
1254 | if (sbits) | ||
1255 | { | ||
1256 | if (encode) | ||
1162 | { | 1257 | { |
1163 | int j; | 1258 | /* Here we only need to encode a sign for the side. */ |
1164 | opus_val16 n; | 1259 | sign = x2[0]*y2[1] - x2[1]*y2[0] < 0; |
1165 | n = celt_sqrt(SHL32(EXTEND32(N0),22)); | 1260 | ec_enc_bits(ec, sign, 1); |
1166 | for (j=0;j<N0;j++) | 1261 | } else { |
1167 | lowband_out[j] = MULT16_16_Q15(n,X[j]); | 1262 | sign = ec_dec_bits(ec, 1); |
1168 | } | 1263 | } |
1169 | cm &= (1<<B)-1; | 1264 | } |
1265 | sign = 1-2*sign; | ||
1266 | /* We use orig_fill here because we want to fold the side, but if | ||
1267 | itheta==16384, we'll have cleared the low bits of fill. */ | ||
1268 | cm = quant_band(ctx, x2, N, mbits, B, lowband, | ||
1269 | LM, lowband_out, Q15ONE, lowband_scratch, orig_fill); | ||
1270 | /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse), | ||
1271 | and there's no need to worry about mixing with the other channel. */ | ||
1272 | y2[0] = -sign*x2[1]; | ||
1273 | y2[1] = sign*x2[0]; | ||
1274 | if (resynth) | ||
1275 | { | ||
1276 | celt_norm tmp; | ||
1277 | X[0] = MULT16_16_Q15(mid, X[0]); | ||
1278 | X[1] = MULT16_16_Q15(mid, X[1]); | ||
1279 | Y[0] = MULT16_16_Q15(side, Y[0]); | ||
1280 | Y[1] = MULT16_16_Q15(side, Y[1]); | ||
1281 | tmp = X[0]; | ||
1282 | X[0] = SUB16(tmp,Y[0]); | ||
1283 | Y[0] = ADD16(tmp,Y[0]); | ||
1284 | tmp = X[1]; | ||
1285 | X[1] = SUB16(tmp,Y[1]); | ||
1286 | Y[1] = ADD16(tmp,Y[1]); | ||
1287 | } | ||
1288 | } else { | ||
1289 | /* "Normal" split code */ | ||
1290 | opus_int32 rebalance; | ||
1291 | |||
1292 | mbits = IMAX(0, IMIN(b, (b-delta)/2)); | ||
1293 | sbits = b-mbits; | ||
1294 | ctx->remaining_bits -= qalloc; | ||
1295 | |||
1296 | rebalance = ctx->remaining_bits; | ||
1297 | if (mbits >= sbits) | ||
1298 | { | ||
1299 | /* In stereo mode, we do not apply a scaling to the mid because we need the normalized | ||
1300 | mid for folding later. */ | ||
1301 | cm = quant_band(ctx, X, N, mbits, B, | ||
1302 | lowband, LM, lowband_out, | ||
1303 | Q15ONE, lowband_scratch, fill); | ||
1304 | rebalance = mbits - (rebalance-ctx->remaining_bits); | ||
1305 | if (rebalance > 3<<BITRES && itheta!=0) | ||
1306 | sbits += rebalance - (3<<BITRES); | ||
1307 | |||
1308 | /* For a stereo split, the high bits of fill are always zero, so no | ||
1309 | folding will be done to the side. */ | ||
1310 | cm |= quant_band(ctx, Y, N, sbits, B, | ||
1311 | NULL, LM, NULL, | ||
1312 | side, NULL, fill>>B); | ||
1313 | } else { | ||
1314 | /* For a stereo split, the high bits of fill are always zero, so no | ||
1315 | folding will be done to the side. */ | ||
1316 | cm = quant_band(ctx, Y, N, sbits, B, | ||
1317 | NULL, LM, NULL, | ||
1318 | side, NULL, fill>>B); | ||
1319 | rebalance = sbits - (rebalance-ctx->remaining_bits); | ||
1320 | if (rebalance > 3<<BITRES && itheta!=16384) | ||
1321 | mbits += rebalance - (3<<BITRES); | ||
1322 | /* In stereo mode, we do not apply a scaling to the mid because we need the normalized | ||
1323 | mid for folding later. */ | ||
1324 | cm |= quant_band(ctx, X, N, mbits, B, | ||
1325 | lowband, LM, lowband_out, | ||
1326 | Q15ONE, lowband_scratch, fill); | ||
1327 | } | ||
1328 | } | ||
1329 | |||
1330 | |||
1331 | /* This code is used by the decoder and by the resynthesis-enabled encoder */ | ||
1332 | if (resynth) | ||
1333 | { | ||
1334 | if (N!=2) | ||
1335 | stereo_merge(X, Y, mid, N); | ||
1336 | if (inv) | ||
1337 | { | ||
1338 | int j; | ||
1339 | for (j=0;j<N;j++) | ||
1340 | Y[j] = -Y[j]; | ||
1170 | } | 1341 | } |
1171 | } | 1342 | } |
1172 | return cm; | 1343 | return cm; |
1173 | } | 1344 | } |
1174 | 1345 | ||
1346 | |||
1175 | void quant_all_bands(int encode, const CELTMode *m, int start, int end, | 1347 | void quant_all_bands(int encode, const CELTMode *m, int start, int end, |
1176 | celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks, const celt_ener *bandE, int *pulses, | 1348 | celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks, const celt_ener *bandE, int *pulses, |
1177 | int shortBlocks, int spread, int dual_stereo, int intensity, int *tf_res, | 1349 | int shortBlocks, int spread, int dual_stereo, int intensity, int *tf_res, |
@@ -1182,27 +1354,41 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1182 | const opus_int16 * OPUS_RESTRICT eBands = m->eBands; | 1354 | const opus_int16 * OPUS_RESTRICT eBands = m->eBands; |
1183 | celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2; | 1355 | celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2; |
1184 | VARDECL(celt_norm, _norm); | 1356 | VARDECL(celt_norm, _norm); |
1185 | VARDECL(celt_norm, lowband_scratch); | 1357 | celt_norm *lowband_scratch; |
1186 | int B; | 1358 | int B; |
1187 | int M; | 1359 | int M; |
1188 | int lowband_offset; | 1360 | int lowband_offset; |
1189 | int update_lowband = 1; | 1361 | int update_lowband = 1; |
1190 | int C = Y_ != NULL ? 2 : 1; | 1362 | int C = Y_ != NULL ? 2 : 1; |
1363 | int norm_offset; | ||
1191 | #ifdef RESYNTH | 1364 | #ifdef RESYNTH |
1192 | int resynth = 1; | 1365 | int resynth = 1; |
1193 | #else | 1366 | #else |
1194 | int resynth = !encode; | 1367 | int resynth = !encode; |
1195 | #endif | 1368 | #endif |
1369 | struct band_ctx ctx; | ||
1196 | SAVE_STACK; | 1370 | SAVE_STACK; |
1197 | 1371 | ||
1198 | M = 1<<LM; | 1372 | M = 1<<LM; |
1199 | B = shortBlocks ? M : 1; | 1373 | B = shortBlocks ? M : 1; |
1200 | ALLOC(_norm, C*M*eBands[m->nbEBands], celt_norm); | 1374 | norm_offset = M*eBands[start]; |
1201 | ALLOC(lowband_scratch, M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]), celt_norm); | 1375 | /* No need to allocate norm for the last band because we don't need an |
1376 | output in that band. */ | ||
1377 | ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm); | ||
1202 | norm = _norm; | 1378 | norm = _norm; |
1203 | norm2 = norm + M*eBands[m->nbEBands]; | 1379 | norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset; |
1380 | /* We can use the last band as scratch space because we don't need that | ||
1381 | scratch space for the last band. */ | ||
1382 | lowband_scratch = X_+M*eBands[m->nbEBands-1]; | ||
1204 | 1383 | ||
1205 | lowband_offset = 0; | 1384 | lowband_offset = 0; |
1385 | ctx.bandE = bandE; | ||
1386 | ctx.ec = ec; | ||
1387 | ctx.encode = encode; | ||
1388 | ctx.intensity = intensity; | ||
1389 | ctx.m = m; | ||
1390 | ctx.seed = *seed; | ||
1391 | ctx.spread = spread; | ||
1206 | for (i=start;i<end;i++) | 1392 | for (i=start;i<end;i++) |
1207 | { | 1393 | { |
1208 | opus_int32 tell; | 1394 | opus_int32 tell; |
@@ -1214,6 +1400,10 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1214 | int tf_change=0; | 1400 | int tf_change=0; |
1215 | unsigned x_cm; | 1401 | unsigned x_cm; |
1216 | unsigned y_cm; | 1402 | unsigned y_cm; |
1403 | int last; | ||
1404 | |||
1405 | ctx.i = i; | ||
1406 | last = (i==end-1); | ||
1217 | 1407 | ||
1218 | X = X_+M*eBands[i]; | 1408 | X = X_+M*eBands[i]; |
1219 | if (Y_!=NULL) | 1409 | if (Y_!=NULL) |
@@ -1227,6 +1417,7 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1227 | if (i != start) | 1417 | if (i != start) |
1228 | balance -= tell; | 1418 | balance -= tell; |
1229 | remaining_bits = total_bits-tell-1; | 1419 | remaining_bits = total_bits-tell-1; |
1420 | ctx.remaining_bits = remaining_bits; | ||
1230 | if (i <= codedBands-1) | 1421 | if (i <= codedBands-1) |
1231 | { | 1422 | { |
1232 | curr_balance = balance / IMIN(3, codedBands-i); | 1423 | curr_balance = balance / IMIN(3, codedBands-i); |
@@ -1239,26 +1430,30 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1239 | lowband_offset = i; | 1430 | lowband_offset = i; |
1240 | 1431 | ||
1241 | tf_change = tf_res[i]; | 1432 | tf_change = tf_res[i]; |
1433 | ctx.tf_change = tf_change; | ||
1242 | if (i>=m->effEBands) | 1434 | if (i>=m->effEBands) |
1243 | { | 1435 | { |
1244 | X=norm; | 1436 | X=norm; |
1245 | if (Y_!=NULL) | 1437 | if (Y_!=NULL) |
1246 | Y = norm; | 1438 | Y = norm; |
1439 | lowband_scratch = NULL; | ||
1247 | } | 1440 | } |
1441 | if (i==end-1) | ||
1442 | lowband_scratch = NULL; | ||
1248 | 1443 | ||
1249 | /* Get a conservative estimate of the collapse_mask's for the bands we're | 1444 | /* Get a conservative estimate of the collapse_mask's for the bands we're |
1250 | going to be folding from. */ | 1445 | going to be folding from. */ |
1251 | if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0)) | 1446 | if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0)) |
1252 | { | 1447 | { |
1253 | int fold_start; | 1448 | int fold_start; |
1254 | int fold_end; | 1449 | int fold_end; |
1255 | int fold_i; | 1450 | int fold_i; |
1256 | /* This ensures we never repeat spectral content within one band */ | 1451 | /* This ensures we never repeat spectral content within one band */ |
1257 | effective_lowband = IMAX(M*eBands[start], M*eBands[lowband_offset]-N); | 1452 | effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N); |
1258 | fold_start = lowband_offset; | 1453 | fold_start = lowband_offset; |
1259 | while(M*eBands[--fold_start] > effective_lowband); | 1454 | while(M*eBands[--fold_start] > effective_lowband+norm_offset); |
1260 | fold_end = lowband_offset-1; | 1455 | fold_end = lowband_offset-1; |
1261 | while(M*eBands[++fold_end] < effective_lowband+N); | 1456 | while(M*eBands[++fold_end] < effective_lowband+norm_offset+N); |
1262 | x_cm = y_cm = 0; | 1457 | x_cm = y_cm = 0; |
1263 | fold_i = fold_start; do { | 1458 | fold_i = fold_start; do { |
1264 | x_cm |= collapse_masks[fold_i*C+0]; | 1459 | x_cm |= collapse_masks[fold_i*C+0]; |
@@ -1266,7 +1461,7 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1266 | } while (++fold_i<fold_end); | 1461 | } while (++fold_i<fold_end); |
1267 | } | 1462 | } |
1268 | /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost | 1463 | /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost |
1269 | always) be non-zero.*/ | 1464 | always) be non-zero. */ |
1270 | else | 1465 | else |
1271 | x_cm = y_cm = (1<<B)-1; | 1466 | x_cm = y_cm = (1<<B)-1; |
1272 | 1467 | ||
@@ -1274,33 +1469,42 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1274 | { | 1469 | { |
1275 | int j; | 1470 | int j; |
1276 | 1471 | ||
1277 | /* Switch off dual stereo to do intensity */ | 1472 | /* Switch off dual stereo to do intensity. */ |
1278 | dual_stereo = 0; | 1473 | dual_stereo = 0; |
1279 | if (resynth) | 1474 | if (resynth) |
1280 | for (j=M*eBands[start];j<M*eBands[i];j++) | 1475 | for (j=0;j<M*eBands[i]-norm_offset;j++) |
1281 | norm[j] = HALF32(norm[j]+norm2[j]); | 1476 | norm[j] = HALF32(norm[j]+norm2[j]); |
1282 | } | 1477 | } |
1283 | if (dual_stereo) | 1478 | if (dual_stereo) |
1284 | { | 1479 | { |
1285 | x_cm = quant_band(encode, m, i, X, NULL, N, b/2, spread, B, intensity, tf_change, | 1480 | x_cm = quant_band(&ctx, X, N, b/2, B, |
1286 | effective_lowband != -1 ? norm+effective_lowband : NULL, ec, &remaining_bits, LM, | 1481 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, |
1287 | norm+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, x_cm); | 1482 | last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, x_cm); |
1288 | y_cm = quant_band(encode, m, i, Y, NULL, N, b/2, spread, B, intensity, tf_change, | 1483 | y_cm = quant_band(&ctx, Y, N, b/2, B, |
1289 | effective_lowband != -1 ? norm2+effective_lowband : NULL, ec, &remaining_bits, LM, | 1484 | effective_lowband != -1 ? norm2+effective_lowband : NULL, LM, |
1290 | norm2+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, y_cm); | 1485 | last?NULL:norm2+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, y_cm); |
1291 | } else { | 1486 | } else { |
1292 | x_cm = quant_band(encode, m, i, X, Y, N, b, spread, B, intensity, tf_change, | 1487 | if (Y!=NULL) |
1293 | effective_lowband != -1 ? norm+effective_lowband : NULL, ec, &remaining_bits, LM, | 1488 | { |
1294 | norm+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, x_cm|y_cm); | 1489 | x_cm = quant_band_stereo(&ctx, X, Y, N, b, B, |
1490 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, | ||
1491 | last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm); | ||
1492 | } else { | ||
1493 | x_cm = quant_band(&ctx, X, N, b, B, | ||
1494 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, | ||
1495 | last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, x_cm|y_cm); | ||
1496 | } | ||
1295 | y_cm = x_cm; | 1497 | y_cm = x_cm; |
1296 | } | 1498 | } |
1297 | collapse_masks[i*C+0] = (unsigned char)x_cm; | 1499 | collapse_masks[i*C+0] = (unsigned char)x_cm; |
1298 | collapse_masks[i*C+C-1] = (unsigned char)y_cm; | 1500 | collapse_masks[i*C+C-1] = (unsigned char)y_cm; |
1299 | balance += pulses[i] + tell; | 1501 | balance += pulses[i] + tell; |
1300 | 1502 | ||
1301 | /* Update the folding position only as long as we have 1 bit/sample depth */ | 1503 | /* Update the folding position only as long as we have 1 bit/sample depth. */ |
1302 | update_lowband = b>(N<<BITRES); | 1504 | update_lowband = b>(N<<BITRES); |
1303 | } | 1505 | } |
1506 | *seed = ctx.seed; | ||
1507 | |||
1304 | RESTORE_STACK; | 1508 | RESTORE_STACK; |
1305 | } | 1509 | } |
1306 | 1510 | ||