Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: third_party/opus/src/celt/bands.c

Issue 2962373002: [Opus] Update to v1.2.1 (Closed)
Patch Set: Pre-increment instead of post-increment Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/opus/src/celt/bands.h ('k') | third_party/opus/src/celt/celt.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 Copyright (c) 2008-2009 Gregory Maxwell 3 Copyright (c) 2008-2009 Gregory Maxwell
4 Written by Jean-Marc Valin and Gregory Maxwell */ 4 Written by Jean-Marc Valin and Gregory Maxwell */
5 /* 5 /*
6 Redistribution and use in source and binary forms, with or without 6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions 7 modification, are permitted provided that the following conditions
8 are met: 8 are met:
9 9
10 - Redistributions of source code must retain the above copyright 10 - Redistributions of source code must retain the above copyright
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 return i; 58 return i;
59 } 59 }
60 60
61 opus_uint32 celt_lcg_rand(opus_uint32 seed) 61 opus_uint32 celt_lcg_rand(opus_uint32 seed)
62 { 62 {
63 return 1664525 * seed + 1013904223; 63 return 1664525 * seed + 1013904223;
64 } 64 }
65 65
66 /* This is a cos() approximation designed to be bit-exact on any platform. Bit e xactness 66 /* This is a cos() approximation designed to be bit-exact on any platform. Bit e xactness
67 with this approximation is important because it has an impact on the bit allo cation */ 67 with this approximation is important because it has an impact on the bit allo cation */
68 static opus_int16 bitexact_cos(opus_int16 x) 68 opus_int16 bitexact_cos(opus_int16 x)
69 { 69 {
70 opus_int32 tmp; 70 opus_int32 tmp;
71 opus_int16 x2; 71 opus_int16 x2;
72 tmp = (4096+((opus_int32)(x)*(x)))>>13; 72 tmp = (4096+((opus_int32)(x)*(x)))>>13;
73 celt_assert(tmp<=32767); 73 celt_assert(tmp<=32767);
74 x2 = tmp; 74 x2 = tmp;
75 x2 = (32767-x2) + FRAC_MUL16(x2, (-7651 + FRAC_MUL16(x2, (8277 + FRAC_MUL16(- 626, x2))))); 75 x2 = (32767-x2) + FRAC_MUL16(x2, (-7651 + FRAC_MUL16(x2, (8277 + FRAC_MUL16(- 626, x2)))));
76 celt_assert(x2<=32766); 76 celt_assert(x2<=32766);
77 return 1+x2; 77 return 1+x2;
78 } 78 }
79 79
80 static int bitexact_log2tan(int isin,int icos) 80 int bitexact_log2tan(int isin,int icos)
81 { 81 {
82 int lc; 82 int lc;
83 int ls; 83 int ls;
84 lc=EC_ILOG(icos); 84 lc=EC_ILOG(icos);
85 ls=EC_ILOG(isin); 85 ls=EC_ILOG(isin);
86 icos<<=15-lc; 86 icos<<=15-lc;
87 isin<<=15-ls; 87 isin<<=15-ls;
88 return (ls-lc)*(1<<11) 88 return (ls-lc)*(1<<11)
89 +FRAC_MUL16(isin, FRAC_MUL16(isin, -2597) + 7932) 89 +FRAC_MUL16(isin, FRAC_MUL16(isin, -2597) + 7932)
90 -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932); 90 -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932);
91 } 91 }
92 92
93 #ifdef FIXED_POINT 93 #ifdef FIXED_POINT
94 /* Compute the amplitude (sqrt energy) in each of the bands */ 94 /* Compute the amplitude (sqrt energy) in each of the bands */
95 void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *band E, int end, int C, int LM) 95 void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *band E, int end, int C, int LM, int arch)
96 { 96 {
97 int i, c, N; 97 int i, c, N;
98 const opus_int16 *eBands = m->eBands; 98 const opus_int16 *eBands = m->eBands;
99 (void)arch;
99 N = m->shortMdctSize<<LM; 100 N = m->shortMdctSize<<LM;
100 c=0; do { 101 c=0; do {
101 for (i=0;i<end;i++) 102 for (i=0;i<end;i++)
102 { 103 {
103 int j; 104 int j;
104 opus_val32 maxval=0; 105 opus_val32 maxval=0;
105 opus_val32 sum = 0; 106 opus_val32 sum = 0;
106 107
107 maxval = celt_maxabs32(&X[c*N+(eBands[i]<<LM)], (eBands[i+1]-eBands[i]) <<LM); 108 maxval = celt_maxabs32(&X[c*N+(eBands[i]<<LM)], (eBands[i+1]-eBands[i]) <<LM);
108 if (maxval > 0) 109 if (maxval > 0)
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 g = EXTRACT16(celt_rcp(SHL32(E,3))); 149 g = EXTRACT16(celt_rcp(SHL32(E,3)));
149 j=M*eBands[i]; do { 150 j=M*eBands[i]; do {
150 X[j+c*N] = MULT16_16_Q15(VSHR32(freq[j+c*N],shift-1),g); 151 X[j+c*N] = MULT16_16_Q15(VSHR32(freq[j+c*N],shift-1),g);
151 } while (++j<M*eBands[i+1]); 152 } while (++j<M*eBands[i+1]);
152 } while (++i<end); 153 } while (++i<end);
153 } while (++c<C); 154 } while (++c<C);
154 } 155 }
155 156
156 #else /* FIXED_POINT */ 157 #else /* FIXED_POINT */
157 /* Compute the amplitude (sqrt energy) in each of the bands */ 158 /* Compute the amplitude (sqrt energy) in each of the bands */
158 void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *band E, int end, int C, int LM) 159 void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *band E, int end, int C, int LM, int arch)
159 { 160 {
160 int i, c, N; 161 int i, c, N;
161 const opus_int16 *eBands = m->eBands; 162 const opus_int16 *eBands = m->eBands;
162 N = m->shortMdctSize<<LM; 163 N = m->shortMdctSize<<LM;
163 c=0; do { 164 c=0; do {
164 for (i=0;i<end;i++) 165 for (i=0;i<end;i++)
165 { 166 {
166 opus_val32 sum; 167 opus_val32 sum;
167 sum = 1e-27f + celt_inner_prod_c(&X[c*N+(eBands[i]<<LM)], &X[c*N+(eBand s[i]<<LM)], (eBands[i+1]-eBands[i])<<LM); 168 sum = 1e-27f + celt_inner_prod(&X[c*N+(eBands[i]<<LM)], &X[c*N+(eBands[ i]<<LM)], (eBands[i+1]-eBands[i])<<LM, arch);
168 bandE[i+c*m->nbEBands] = celt_sqrt(sum); 169 bandE[i+c*m->nbEBands] = celt_sqrt(sum);
169 /*printf ("%f ", bandE[i+c*m->nbEBands]);*/ 170 /*printf ("%f ", bandE[i+c*m->nbEBands]);*/
170 } 171 }
171 } while (++c<C); 172 } while (++c<C);
172 /*printf ("\n");*/ 173 /*printf ("\n");*/
173 } 174 }
174 175
175 /* Normalise each band such that the energy is one. */ 176 /* Normalise each band such that the energy is one. */
176 void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, cel t_norm * OPUS_RESTRICT X, const celt_ener *bandE, int end, int C, int M) 177 void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, cel t_norm * OPUS_RESTRICT X, const celt_ener *bandE, int end, int C, int M)
177 { 178 {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 for (i=start;i<end;i++) 218 for (i=start;i<end;i++)
218 { 219 {
219 int j, band_end; 220 int j, band_end;
220 opus_val16 g; 221 opus_val16 g;
221 opus_val16 lg; 222 opus_val16 lg;
222 #ifdef FIXED_POINT 223 #ifdef FIXED_POINT
223 int shift; 224 int shift;
224 #endif 225 #endif
225 j=M*eBands[i]; 226 j=M*eBands[i];
226 band_end = M*eBands[i+1]; 227 band_end = M*eBands[i+1];
227 lg = ADD16(bandLogE[i], SHL16((opus_val16)eMeans[i],6)); 228 lg = SATURATE16(ADD32(bandLogE[i], SHL32((opus_val32)eMeans[i],6)));
228 #ifndef FIXED_POINT 229 #ifndef FIXED_POINT
229 g = celt_exp2(MIN32(32.f, lg)); 230 g = celt_exp2(MIN32(32.f, lg));
230 #else 231 #else
231 /* Handle the integer part of the log energy */ 232 /* Handle the integer part of the log energy */
232 shift = 16-(lg>>DB_SHIFT); 233 shift = 16-(lg>>DB_SHIFT);
233 if (shift>31) 234 if (shift>31)
234 { 235 {
235 shift=0; 236 shift=0;
236 g=0; 237 g=0;
237 } else { 238 } else {
238 /* Handle the fractional part. */ 239 /* Handle the fractional part. */
239 g = celt_exp2_frac(lg&((1<<DB_SHIFT)-1)); 240 g = celt_exp2_frac(lg&((1<<DB_SHIFT)-1));
240 } 241 }
241 /* Handle extreme gains with negative shift. */ 242 /* Handle extreme gains with negative shift. */
242 if (shift<0) 243 if (shift<0)
243 { 244 {
244 /* For shift < -2 we'd be likely to overflow, so we're capping 245 /* For shift <= -2 and g > 16384 we'd be likely to overflow, so we're
245 the gain here. This shouldn't happen unless the bitstream is 246 capping the gain here, which is equivalent to a cap of 18 on lg.
246 already corrupted. */ 247 This shouldn't trigger unless the bitstream is already corrupted. */
247 if (shift < -2) 248 if (shift <= -2)
248 { 249 {
249 g = 32767; 250 g = 16384;
250 shift = -2; 251 shift = -2;
251 } 252 }
252 do { 253 do {
253 *f++ = SHL32(MULT16_16(*x++, g), -shift); 254 *f++ = SHL32(MULT16_16(*x++, g), -shift);
254 } while (++j<band_end); 255 } while (++j<band_end);
255 } else 256 } else
256 #endif 257 #endif
257 /* Be careful of the fixed-point "else" just above when changing this c ode */ 258 /* Be careful of the fixed-point "else" just above when changing this c ode */
258 do { 259 do {
259 *f++ = SHR32(MULT16_16(*x++, g), shift); 260 *f++ = SHR32(MULT16_16(*x++, g), shift);
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 renormalize = 1; 354 renormalize = 1;
354 } 355 }
355 } 356 }
356 /* We just added some energy, so we need to renormalise */ 357 /* We just added some energy, so we need to renormalise */
357 if (renormalize) 358 if (renormalize)
358 renormalise_vector(X, N0<<LM, Q15ONE, arch); 359 renormalise_vector(X, N0<<LM, Q15ONE, arch);
359 } while (++c<C); 360 } while (++c<C);
360 } 361 }
361 } 362 }
362 363
364 /* Compute the weights to use for optimizing normalized distortion across
365 channels. We use the amplitude to weight square distortion, which means
366 that we use the square root of the value we would have been using if we
367 wanted to minimize the MSE in the non-normalized domain. This roughly
368 corresponds to some quick-and-dirty perceptual experiments I ran to
369 measure inter-aural masking (there doesn't seem to be any published data
370 on the topic). */
371 static void compute_channel_weights(celt_ener Ex, celt_ener Ey, opus_val16 w[2])
372 {
373 celt_ener minE;
374 #if FIXED_POINT
375 int shift;
376 #endif
377 minE = MIN32(Ex, Ey);
378 /* Adjustment to make the weights a bit more conservative. */
379 Ex = ADD32(Ex, minE/3);
380 Ey = ADD32(Ey, minE/3);
381 #if FIXED_POINT
382 shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
383 #endif
384 w[0] = VSHR32(Ex, shift);
385 w[1] = VSHR32(Ey, shift);
386 }
387
363 static void intensity_stereo(const CELTMode *m, celt_norm * OPUS_RESTRICT X, con st celt_norm * OPUS_RESTRICT Y, const celt_ener *bandE, int bandID, int N) 388 static void intensity_stereo(const CELTMode *m, celt_norm * OPUS_RESTRICT X, con st celt_norm * OPUS_RESTRICT Y, const celt_ener *bandE, int bandID, int N)
364 { 389 {
365 int i = bandID; 390 int i = bandID;
366 int j; 391 int j;
367 opus_val16 a1, a2; 392 opus_val16 a1, a2;
368 opus_val16 left, right; 393 opus_val16 left, right;
369 opus_val16 norm; 394 opus_val16 norm;
370 #ifdef FIXED_POINT 395 #ifdef FIXED_POINT
371 int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13; 396 int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
372 #endif 397 #endif
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 } else { 665 } else {
641 qn = exp2_table8[qb&0x7]>>(14-(qb>>BITRES)); 666 qn = exp2_table8[qb&0x7]>>(14-(qb>>BITRES));
642 qn = (qn+1)>>1<<1; 667 qn = (qn+1)>>1<<1;
643 } 668 }
644 celt_assert(qn <= 256); 669 celt_assert(qn <= 256);
645 return qn; 670 return qn;
646 } 671 }
647 672
648 struct band_ctx { 673 struct band_ctx {
649 int encode; 674 int encode;
675 int resynth;
650 const CELTMode *m; 676 const CELTMode *m;
651 int i; 677 int i;
652 int intensity; 678 int intensity;
653 int spread; 679 int spread;
654 int tf_change; 680 int tf_change;
655 ec_ctx *ec; 681 ec_ctx *ec;
656 opus_int32 remaining_bits; 682 opus_int32 remaining_bits;
657 const celt_ener *bandE; 683 const celt_ener *bandE;
658 opus_uint32 seed; 684 opus_uint32 seed;
659 int arch; 685 int arch;
686 int theta_round;
687 int disable_inv;
688 int avoid_split_noise;
660 }; 689 };
661 690
662 struct split_ctx { 691 struct split_ctx {
663 int inv; 692 int inv;
664 int imid; 693 int imid;
665 int iside; 694 int iside;
666 int delta; 695 int delta;
667 int itheta; 696 int itheta;
668 int qalloc; 697 int qalloc;
669 }; 698 };
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 /* theta is the atan() of the ratio between the (normalized) 736 /* theta is the atan() of the ratio between the (normalized)
708 side and mid. With just that parameter, we can re-scale both 737 side and mid. With just that parameter, we can re-scale both
709 mid and side because we know that 1) they have unit norm and 738 mid and side because we know that 1) they have unit norm and
710 2) they are orthogonal. */ 739 2) they are orthogonal. */
711 itheta = stereo_itheta(X, Y, stereo, N, ctx->arch); 740 itheta = stereo_itheta(X, Y, stereo, N, ctx->arch);
712 } 741 }
713 tell = ec_tell_frac(ec); 742 tell = ec_tell_frac(ec);
714 if (qn!=1) 743 if (qn!=1)
715 { 744 {
716 if (encode) 745 if (encode)
717 itheta = (itheta*(opus_int32)qn+8192)>>14; 746 {
718 747 if (!stereo || ctx->theta_round == 0)
748 {
749 itheta = (itheta*(opus_int32)qn+8192)>>14;
750 if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
751 {
752 /* Check if the selected value of theta will cause the bit alloca tion
753 to inject noise on one side. If so, make sure the energy of th at side
754 is zero. */
755 int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
756 imid = bitexact_cos((opus_int16)unquantized);
757 iside = bitexact_cos((opus_int16)(16384-unquantized));
758 delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
759 if (delta > *b)
760 itheta = qn;
761 else if (delta < -*b)
762 itheta = 0;
763 }
764 } else {
765 int down;
766 /* Bias quantization towards itheta=0 and itheta=16384. */
767 int bias = itheta > 8192 ? 32767/qn : -32767/qn;
768 down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
769 if (ctx->theta_round < 0)
770 itheta = down;
771 else
772 itheta = down+1;
773 }
774 }
719 /* Entropy coding of the angle. We use a uniform pdf for the 775 /* Entropy coding of the angle. We use a uniform pdf for the
720 time split, a step for stereo, and a triangular one for the rest. */ 776 time split, a step for stereo, and a triangular one for the rest. */
721 if (stereo && N>2) 777 if (stereo && N>2)
722 { 778 {
723 int p0 = 3; 779 int p0 = 3;
724 int x = itheta; 780 int x = itheta;
725 int x0 = qn/2; 781 int x0 = qn/2;
726 int ft = p0*(x0+1) + x0; 782 int ft = p0*(x0+1) + x0;
727 /* Use a probability of p0 up to itheta=8192 and then use 1 after */ 783 /* Use a probability of p0 up to itheta=8192 and then use 1 after */
728 if (encode) 784 if (encode)
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
786 if (itheta==0) 842 if (itheta==0)
787 intensity_stereo(m, X, Y, bandE, i, N); 843 intensity_stereo(m, X, Y, bandE, i, N);
788 else 844 else
789 stereo_split(X, Y, N); 845 stereo_split(X, Y, N);
790 } 846 }
791 /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high r ate. 847 /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high r ate.
792 Let's do that at higher complexity */ 848 Let's do that at higher complexity */
793 } else if (stereo) { 849 } else if (stereo) {
794 if (encode) 850 if (encode)
795 { 851 {
796 inv = itheta > 8192; 852 inv = itheta > 8192 && !ctx->disable_inv;
797 if (inv) 853 if (inv)
798 { 854 {
799 int j; 855 int j;
800 for (j=0;j<N;j++) 856 for (j=0;j<N;j++)
801 Y[j] = -Y[j]; 857 Y[j] = -Y[j];
802 } 858 }
803 intensity_stereo(m, X, Y, bandE, i, N); 859 intensity_stereo(m, X, Y, bandE, i, N);
804 } 860 }
805 if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES) 861 if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
806 { 862 {
807 if (encode) 863 if (encode)
808 ec_enc_bit_logp(ec, inv, 2); 864 ec_enc_bit_logp(ec, inv, 2);
809 else 865 else
810 inv = ec_dec_bit_logp(ec, 2); 866 inv = ec_dec_bit_logp(ec, 2);
811 } else 867 } else
812 inv = 0; 868 inv = 0;
869 /* inv flag override to avoid problems with downmixing. */
870 if (ctx->disable_inv)
871 inv = 0;
813 itheta = 0; 872 itheta = 0;
814 } 873 }
815 qalloc = ec_tell_frac(ec) - tell; 874 qalloc = ec_tell_frac(ec) - tell;
816 *b -= qalloc; 875 *b -= qalloc;
817 876
818 if (itheta == 0) 877 if (itheta == 0)
819 { 878 {
820 imid = 32767; 879 imid = 32767;
821 iside = 0; 880 iside = 0;
822 *fill &= (1<<B)-1; 881 *fill &= (1<<B)-1;
(...skipping 15 matching lines...) Expand all
838 sctx->inv = inv; 897 sctx->inv = inv;
839 sctx->imid = imid; 898 sctx->imid = imid;
840 sctx->iside = iside; 899 sctx->iside = iside;
841 sctx->delta = delta; 900 sctx->delta = delta;
842 sctx->itheta = itheta; 901 sctx->itheta = itheta;
843 sctx->qalloc = qalloc; 902 sctx->qalloc = qalloc;
844 } 903 }
845 static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y, int b, 904 static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y, int b,
846 celt_norm *lowband_out) 905 celt_norm *lowband_out)
847 { 906 {
848 #ifdef RESYNTH
849 int resynth = 1;
850 #else
851 int resynth = !ctx->encode;
852 #endif
853 int c; 907 int c;
854 int stereo; 908 int stereo;
855 celt_norm *x = X; 909 celt_norm *x = X;
856 int encode; 910 int encode;
857 ec_ctx *ec; 911 ec_ctx *ec;
858 912
859 encode = ctx->encode; 913 encode = ctx->encode;
860 ec = ctx->ec; 914 ec = ctx->ec;
861 915
862 stereo = Y != NULL; 916 stereo = Y != NULL;
863 c=0; do { 917 c=0; do {
864 int sign=0; 918 int sign=0;
865 if (ctx->remaining_bits>=1<<BITRES) 919 if (ctx->remaining_bits>=1<<BITRES)
866 { 920 {
867 if (encode) 921 if (encode)
868 { 922 {
869 sign = x[0]<0; 923 sign = x[0]<0;
870 ec_enc_bits(ec, sign, 1); 924 ec_enc_bits(ec, sign, 1);
871 } else { 925 } else {
872 sign = ec_dec_bits(ec, 1); 926 sign = ec_dec_bits(ec, 1);
873 } 927 }
874 ctx->remaining_bits -= 1<<BITRES; 928 ctx->remaining_bits -= 1<<BITRES;
875 b-=1<<BITRES; 929 b-=1<<BITRES;
876 } 930 }
877 if (resynth) 931 if (ctx->resynth)
878 x[0] = sign ? -NORM_SCALING : NORM_SCALING; 932 x[0] = sign ? -NORM_SCALING : NORM_SCALING;
879 x = Y; 933 x = Y;
880 } while (++c<1+stereo); 934 } while (++c<1+stereo);
881 if (lowband_out) 935 if (lowband_out)
882 lowband_out[0] = SHR16(X[0],4); 936 lowband_out[0] = SHR16(X[0],4);
883 return 1; 937 return 1;
884 } 938 }
885 939
886 /* This function is responsible for encoding and decoding a mono partition. 940 /* This function is responsible for encoding and decoding a mono partition.
887 It can split the band in two and transmit the energy difference with 941 It can split the band in two and transmit the energy difference with
888 the two half-bands. It can be called recursively so bands can end up being 942 the two half-bands. It can be called recursively so bands can end up being
889 split in 8 parts. */ 943 split in 8 parts. */
890 static unsigned quant_partition(struct band_ctx *ctx, celt_norm *X, 944 static unsigned quant_partition(struct band_ctx *ctx, celt_norm *X,
891 int N, int b, int B, celt_norm *lowband, 945 int N, int b, int B, celt_norm *lowband,
892 int LM, 946 int LM,
893 opus_val16 gain, int fill) 947 opus_val16 gain, int fill)
894 { 948 {
895 const unsigned char *cache; 949 const unsigned char *cache;
896 int q; 950 int q;
897 int curr_bits; 951 int curr_bits;
898 int imid=0, iside=0; 952 int imid=0, iside=0;
899 int B0=B; 953 int B0=B;
900 opus_val16 mid=0, side=0; 954 opus_val16 mid=0, side=0;
901 unsigned cm=0; 955 unsigned cm=0;
902 #ifdef RESYNTH
903 int resynth = 1;
904 #else
905 int resynth = !ctx->encode;
906 #endif
907 celt_norm *Y=NULL; 956 celt_norm *Y=NULL;
908 int encode; 957 int encode;
909 const CELTMode *m; 958 const CELTMode *m;
910 int i; 959 int i;
911 int spread; 960 int spread;
912 ec_ctx *ec; 961 ec_ctx *ec;
913 962
914 encode = ctx->encode; 963 encode = ctx->encode;
915 m = ctx->m; 964 m = ctx->m;
916 i = ctx->i; 965 i = ctx->i;
(...skipping 11 matching lines...) Expand all
928 celt_norm *next_lowband2=NULL; 977 celt_norm *next_lowband2=NULL;
929 opus_int32 rebalance; 978 opus_int32 rebalance;
930 979
931 N >>= 1; 980 N >>= 1;
932 Y = X+N; 981 Y = X+N;
933 LM -= 1; 982 LM -= 1;
934 if (B==1) 983 if (B==1)
935 fill = (fill&1)|(fill<<1); 984 fill = (fill&1)|(fill<<1);
936 B = (B+1)>>1; 985 B = (B+1)>>1;
937 986
938 compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, 987 compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill);
939 LM, 0, &fill);
940 imid = sctx.imid; 988 imid = sctx.imid;
941 iside = sctx.iside; 989 iside = sctx.iside;
942 delta = sctx.delta; 990 delta = sctx.delta;
943 itheta = sctx.itheta; 991 itheta = sctx.itheta;
944 qalloc = sctx.qalloc; 992 qalloc = sctx.qalloc;
945 #ifdef FIXED_POINT 993 #ifdef FIXED_POINT
946 mid = imid; 994 mid = imid;
947 side = iside; 995 side = iside;
948 #else 996 #else
949 mid = (1.f/32768)*imid; 997 mid = (1.f/32768)*imid;
(...skipping 13 matching lines...) Expand all
963 mbits = IMAX(0, IMIN(b, (b-delta)/2)); 1011 mbits = IMAX(0, IMIN(b, (b-delta)/2));
964 sbits = b-mbits; 1012 sbits = b-mbits;
965 ctx->remaining_bits -= qalloc; 1013 ctx->remaining_bits -= qalloc;
966 1014
967 if (lowband) 1015 if (lowband)
968 next_lowband2 = lowband+N; /* >32-bit split case */ 1016 next_lowband2 = lowband+N; /* >32-bit split case */
969 1017
970 rebalance = ctx->remaining_bits; 1018 rebalance = ctx->remaining_bits;
971 if (mbits >= sbits) 1019 if (mbits >= sbits)
972 { 1020 {
973 cm = quant_partition(ctx, X, N, mbits, B, 1021 cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
974 lowband, LM,
975 MULT16_16_P15(gain,mid), fill); 1022 MULT16_16_P15(gain,mid), fill);
976 rebalance = mbits - (rebalance-ctx->remaining_bits); 1023 rebalance = mbits - (rebalance-ctx->remaining_bits);
977 if (rebalance > 3<<BITRES && itheta!=0) 1024 if (rebalance > 3<<BITRES && itheta!=0)
978 sbits += rebalance - (3<<BITRES); 1025 sbits += rebalance - (3<<BITRES);
979 cm |= quant_partition(ctx, Y, N, sbits, B, 1026 cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
980 next_lowband2, LM,
981 MULT16_16_P15(gain,side), fill>>B)<<(B0>>1); 1027 MULT16_16_P15(gain,side), fill>>B)<<(B0>>1);
982 } else { 1028 } else {
983 cm = quant_partition(ctx, Y, N, sbits, B, 1029 cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
984 next_lowband2, LM,
985 MULT16_16_P15(gain,side), fill>>B)<<(B0>>1); 1030 MULT16_16_P15(gain,side), fill>>B)<<(B0>>1);
986 rebalance = sbits - (rebalance-ctx->remaining_bits); 1031 rebalance = sbits - (rebalance-ctx->remaining_bits);
987 if (rebalance > 3<<BITRES && itheta!=16384) 1032 if (rebalance > 3<<BITRES && itheta!=16384)
988 mbits += rebalance - (3<<BITRES); 1033 mbits += rebalance - (3<<BITRES);
989 cm |= quant_partition(ctx, X, N, mbits, B, 1034 cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
990 lowband, LM,
991 MULT16_16_P15(gain,mid), fill); 1035 MULT16_16_P15(gain,mid), fill);
992 } 1036 }
993 } else { 1037 } else {
994 /* This is the basic no-split case */ 1038 /* This is the basic no-split case */
995 q = bits2pulses(m, i, LM, b); 1039 q = bits2pulses(m, i, LM, b);
996 curr_bits = pulses2bits(m, i, LM, q); 1040 curr_bits = pulses2bits(m, i, LM, q);
997 ctx->remaining_bits -= curr_bits; 1041 ctx->remaining_bits -= curr_bits;
998 1042
999 /* Ensures we can never bust the budget */ 1043 /* Ensures we can never bust the budget */
1000 while (ctx->remaining_bits < 0 && q > 0) 1044 while (ctx->remaining_bits < 0 && q > 0)
1001 { 1045 {
1002 ctx->remaining_bits += curr_bits; 1046 ctx->remaining_bits += curr_bits;
1003 q--; 1047 q--;
1004 curr_bits = pulses2bits(m, i, LM, q); 1048 curr_bits = pulses2bits(m, i, LM, q);
1005 ctx->remaining_bits -= curr_bits; 1049 ctx->remaining_bits -= curr_bits;
1006 } 1050 }
1007 1051
1008 if (q!=0) 1052 if (q!=0)
1009 { 1053 {
1010 int K = get_pulses(q); 1054 int K = get_pulses(q);
1011 1055
1012 /* Finally do the actual quantization */ 1056 /* Finally do the actual quantization */
1013 if (encode) 1057 if (encode)
1014 { 1058 {
1015 cm = alg_quant(X, N, K, spread, B, ec 1059 cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth, ctx->arch );
1016 #ifdef RESYNTH
1017 , gain
1018 #endif
1019 );
1020 } else { 1060 } else {
1021 cm = alg_unquant(X, N, K, spread, B, ec, gain); 1061 cm = alg_unquant(X, N, K, spread, B, ec, gain);
1022 } 1062 }
1023 } else { 1063 } else {
1024 /* If there's no pulse, fill the band anyway */ 1064 /* If there's no pulse, fill the band anyway */
1025 int j; 1065 int j;
1026 if (resynth) 1066 if (ctx->resynth)
1027 { 1067 {
1028 unsigned cm_mask; 1068 unsigned cm_mask;
1029 /* B can be as large as 16, so this shift might overflow an int on a 1069 /* B can be as large as 16, so this shift might overflow an int on a
1030 16-bit platform; use a long to get defined behavior.*/ 1070 16-bit platform; use a long to get defined behavior.*/
1031 cm_mask = (unsigned)(1UL<<B)-1; 1071 cm_mask = (unsigned)(1UL<<B)-1;
1032 fill &= cm_mask; 1072 fill &= cm_mask;
1033 if (!fill) 1073 if (!fill)
1034 { 1074 {
1035 OPUS_CLEAR(X, N); 1075 OPUS_CLEAR(X, N);
1036 } else { 1076 } else {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1073 opus_val16 gain, celt_norm *lowband_scratch, int fill) 1113 opus_val16 gain, celt_norm *lowband_scratch, int fill)
1074 { 1114 {
1075 int N0=N; 1115 int N0=N;
1076 int N_B=N; 1116 int N_B=N;
1077 int N_B0; 1117 int N_B0;
1078 int B0=B; 1118 int B0=B;
1079 int time_divide=0; 1119 int time_divide=0;
1080 int recombine=0; 1120 int recombine=0;
1081 int longBlocks; 1121 int longBlocks;
1082 unsigned cm=0; 1122 unsigned cm=0;
1083 #ifdef RESYNTH
1084 int resynth = 1;
1085 #else
1086 int resynth = !ctx->encode;
1087 #endif
1088 int k; 1123 int k;
1089 int encode; 1124 int encode;
1090 int tf_change; 1125 int tf_change;
1091 1126
1092 encode = ctx->encode; 1127 encode = ctx->encode;
1093 tf_change = ctx->tf_change; 1128 tf_change = ctx->tf_change;
1094 1129
1095 longBlocks = B0==1; 1130 longBlocks = B0==1;
1096 1131
1097 N_B = celt_udiv(N_B, B); 1132 N_B = celt_udiv(N_B, B);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1144 1179
1145 /* Reorganize the samples in time order instead of frequency order */ 1180 /* Reorganize the samples in time order instead of frequency order */
1146 if (B0>1) 1181 if (B0>1)
1147 { 1182 {
1148 if (encode) 1183 if (encode)
1149 deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks); 1184 deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1150 if (lowband) 1185 if (lowband)
1151 deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlock s); 1186 deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlock s);
1152 } 1187 }
1153 1188
1154 cm = quant_partition(ctx, X, N, b, B, lowband, 1189 cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill);
1155 LM, gain, fill);
1156 1190
1157 /* This code is used by the decoder and by the resynthesis-enabled encoder */ 1191 /* This code is used by the decoder and by the resynthesis-enabled encoder */
1158 if (resynth) 1192 if (ctx->resynth)
1159 { 1193 {
1160 /* Undo the sample reorganization going from time order to frequency order */ 1194 /* Undo the sample reorganization going from time order to frequency order */
1161 if (B0>1) 1195 if (B0>1)
1162 interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks); 1196 interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1163 1197
1164 /* Undo time-freq changes that we did earlier */ 1198 /* Undo time-freq changes that we did earlier */
1165 N_B = N_B0; 1199 N_B = N_B0;
1166 B = B0; 1200 B = B0;
1167 for (k=0;k<time_divide;k++) 1201 for (k=0;k<time_divide;k++)
1168 { 1202 {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1201 /* This function is responsible for encoding and decoding a band for the stereo case. */ 1235 /* This function is responsible for encoding and decoding a band for the stereo case. */
1202 static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm *Y, 1236 static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm *Y,
1203 int N, int b, int B, celt_norm *lowband, 1237 int N, int b, int B, celt_norm *lowband,
1204 int LM, celt_norm *lowband_out, 1238 int LM, celt_norm *lowband_out,
1205 celt_norm *lowband_scratch, int fill) 1239 celt_norm *lowband_scratch, int fill)
1206 { 1240 {
1207 int imid=0, iside=0; 1241 int imid=0, iside=0;
1208 int inv = 0; 1242 int inv = 0;
1209 opus_val16 mid=0, side=0; 1243 opus_val16 mid=0, side=0;
1210 unsigned cm=0; 1244 unsigned cm=0;
1211 #ifdef RESYNTH
1212 int resynth = 1;
1213 #else
1214 int resynth = !ctx->encode;
1215 #endif
1216 int mbits, sbits, delta; 1245 int mbits, sbits, delta;
1217 int itheta; 1246 int itheta;
1218 int qalloc; 1247 int qalloc;
1219 struct split_ctx sctx; 1248 struct split_ctx sctx;
1220 int orig_fill; 1249 int orig_fill;
1221 int encode; 1250 int encode;
1222 ec_ctx *ec; 1251 ec_ctx *ec;
1223 1252
1224 encode = ctx->encode; 1253 encode = ctx->encode;
1225 ec = ctx->ec; 1254 ec = ctx->ec;
1226 1255
1227 /* Special case for one sample */ 1256 /* Special case for one sample */
1228 if (N==1) 1257 if (N==1)
1229 { 1258 {
1230 return quant_band_n1(ctx, X, Y, b, lowband_out); 1259 return quant_band_n1(ctx, X, Y, b, lowband_out);
1231 } 1260 }
1232 1261
1233 orig_fill = fill; 1262 orig_fill = fill;
1234 1263
1235 compute_theta(ctx, &sctx, X, Y, N, &b, B, B, 1264 compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill);
1236 LM, 1, &fill);
1237 inv = sctx.inv; 1265 inv = sctx.inv;
1238 imid = sctx.imid; 1266 imid = sctx.imid;
1239 iside = sctx.iside; 1267 iside = sctx.iside;
1240 delta = sctx.delta; 1268 delta = sctx.delta;
1241 itheta = sctx.itheta; 1269 itheta = sctx.itheta;
1242 qalloc = sctx.qalloc; 1270 qalloc = sctx.qalloc;
1243 #ifdef FIXED_POINT 1271 #ifdef FIXED_POINT
1244 mid = imid; 1272 mid = imid;
1245 side = iside; 1273 side = iside;
1246 #else 1274 #else
(...skipping 27 matching lines...) Expand all
1274 /* Here we only need to encode a sign for the side. */ 1302 /* Here we only need to encode a sign for the side. */
1275 sign = x2[0]*y2[1] - x2[1]*y2[0] < 0; 1303 sign = x2[0]*y2[1] - x2[1]*y2[0] < 0;
1276 ec_enc_bits(ec, sign, 1); 1304 ec_enc_bits(ec, sign, 1);
1277 } else { 1305 } else {
1278 sign = ec_dec_bits(ec, 1); 1306 sign = ec_dec_bits(ec, 1);
1279 } 1307 }
1280 } 1308 }
1281 sign = 1-2*sign; 1309 sign = 1-2*sign;
1282 /* We use orig_fill here because we want to fold the side, but if 1310 /* We use orig_fill here because we want to fold the side, but if
1283 itheta==16384, we'll have cleared the low bits of fill. */ 1311 itheta==16384, we'll have cleared the low bits of fill. */
1284 cm = quant_band(ctx, x2, N, mbits, B, lowband, 1312 cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q15ONE,
1285 LM, lowband_out, Q15ONE, lowband_scratch, orig_fill); 1313 lowband_scratch, orig_fill);
1286 /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse), 1314 /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse),
1287 and there's no need to worry about mixing with the other channel. */ 1315 and there's no need to worry about mixing with the other channel. */
1288 y2[0] = -sign*x2[1]; 1316 y2[0] = -sign*x2[1];
1289 y2[1] = sign*x2[0]; 1317 y2[1] = sign*x2[0];
1290 if (resynth) 1318 if (ctx->resynth)
1291 { 1319 {
1292 celt_norm tmp; 1320 celt_norm tmp;
1293 X[0] = MULT16_16_Q15(mid, X[0]); 1321 X[0] = MULT16_16_Q15(mid, X[0]);
1294 X[1] = MULT16_16_Q15(mid, X[1]); 1322 X[1] = MULT16_16_Q15(mid, X[1]);
1295 Y[0] = MULT16_16_Q15(side, Y[0]); 1323 Y[0] = MULT16_16_Q15(side, Y[0]);
1296 Y[1] = MULT16_16_Q15(side, Y[1]); 1324 Y[1] = MULT16_16_Q15(side, Y[1]);
1297 tmp = X[0]; 1325 tmp = X[0];
1298 X[0] = SUB16(tmp,Y[0]); 1326 X[0] = SUB16(tmp,Y[0]);
1299 Y[0] = ADD16(tmp,Y[0]); 1327 Y[0] = ADD16(tmp,Y[0]);
1300 tmp = X[1]; 1328 tmp = X[1];
1301 X[1] = SUB16(tmp,Y[1]); 1329 X[1] = SUB16(tmp,Y[1]);
1302 Y[1] = ADD16(tmp,Y[1]); 1330 Y[1] = ADD16(tmp,Y[1]);
1303 } 1331 }
1304 } else { 1332 } else {
1305 /* "Normal" split code */ 1333 /* "Normal" split code */
1306 opus_int32 rebalance; 1334 opus_int32 rebalance;
1307 1335
1308 mbits = IMAX(0, IMIN(b, (b-delta)/2)); 1336 mbits = IMAX(0, IMIN(b, (b-delta)/2));
1309 sbits = b-mbits; 1337 sbits = b-mbits;
1310 ctx->remaining_bits -= qalloc; 1338 ctx->remaining_bits -= qalloc;
1311 1339
1312 rebalance = ctx->remaining_bits; 1340 rebalance = ctx->remaining_bits;
1313 if (mbits >= sbits) 1341 if (mbits >= sbits)
1314 { 1342 {
1315 /* In stereo mode, we do not apply a scaling to the mid because we need the normalized 1343 /* In stereo mode, we do not apply a scaling to the mid because we need the normalized
1316 mid for folding later. */ 1344 mid for folding later. */
1317 cm = quant_band(ctx, X, N, mbits, B, 1345 cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q15ONE,
1318 lowband, LM, lowband_out, 1346 lowband_scratch, fill);
1319 Q15ONE, lowband_scratch, fill);
1320 rebalance = mbits - (rebalance-ctx->remaining_bits); 1347 rebalance = mbits - (rebalance-ctx->remaining_bits);
1321 if (rebalance > 3<<BITRES && itheta!=0) 1348 if (rebalance > 3<<BITRES && itheta!=0)
1322 sbits += rebalance - (3<<BITRES); 1349 sbits += rebalance - (3<<BITRES);
1323 1350
1324 /* For a stereo split, the high bits of fill are always zero, so no 1351 /* For a stereo split, the high bits of fill are always zero, so no
1325 folding will be done to the side. */ 1352 folding will be done to the side. */
1326 cm |= quant_band(ctx, Y, N, sbits, B, 1353 cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill> >B);
1327 NULL, LM, NULL,
1328 side, NULL, fill>>B);
1329 } else { 1354 } else {
1330 /* For a stereo split, the high bits of fill are always zero, so no 1355 /* For a stereo split, the high bits of fill are always zero, so no
1331 folding will be done to the side. */ 1356 folding will be done to the side. */
1332 cm = quant_band(ctx, Y, N, sbits, B, 1357 cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>> B);
1333 NULL, LM, NULL,
1334 side, NULL, fill>>B);
1335 rebalance = sbits - (rebalance-ctx->remaining_bits); 1358 rebalance = sbits - (rebalance-ctx->remaining_bits);
1336 if (rebalance > 3<<BITRES && itheta!=16384) 1359 if (rebalance > 3<<BITRES && itheta!=16384)
1337 mbits += rebalance - (3<<BITRES); 1360 mbits += rebalance - (3<<BITRES);
1338 /* In stereo mode, we do not apply a scaling to the mid because we need the normalized 1361 /* In stereo mode, we do not apply a scaling to the mid because we need the normalized
1339 mid for folding later. */ 1362 mid for folding later. */
1340 cm |= quant_band(ctx, X, N, mbits, B, 1363 cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q15ONE,
1341 lowband, LM, lowband_out, 1364 lowband_scratch, fill);
1342 Q15ONE, lowband_scratch, fill);
1343 } 1365 }
1344 } 1366 }
1345 1367
1346 1368
1347 /* This code is used by the decoder and by the resynthesis-enabled encoder */ 1369 /* This code is used by the decoder and by the resynthesis-enabled encoder */
1348 if (resynth) 1370 if (ctx->resynth)
1349 { 1371 {
1350 if (N!=2) 1372 if (N!=2)
1351 stereo_merge(X, Y, mid, N, ctx->arch); 1373 stereo_merge(X, Y, mid, N, ctx->arch);
1352 if (inv) 1374 if (inv)
1353 { 1375 {
1354 int j; 1376 int j;
1355 for (j=0;j<N;j++) 1377 for (j=0;j<N;j++)
1356 Y[j] = -Y[j]; 1378 Y[j] = -Y[j];
1357 } 1379 }
1358 } 1380 }
1359 return cm; 1381 return cm;
1360 } 1382 }
1361 1383
1384 static void special_hybrid_folding(const CELTMode *m, celt_norm *norm, celt_norm *norm2, int start, int M, int dual_stereo)
1385 {
1386 int n1, n2;
1387 const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1388 n1 = M*(eBands[start+1]-eBands[start]);
1389 n2 = M*(eBands[start+2]-eBands[start+1]);
1390 /* Duplicate enough of the first band folding data to be able to fold the sec ond band.
1391 Copies no data for CELT-only mode. */
1392 OPUS_COPY(&norm[n1], &norm[2*n1 - n2], n2-n1);
1393 if (dual_stereo)
1394 OPUS_COPY(&norm2[n1], &norm2[2*n1 - n2], n2-n1);
1395 }
1362 1396
1363 void quant_all_bands(int encode, const CELTMode *m, int start, int end, 1397 void quant_all_bands(int encode, const CELTMode *m, int start, int end,
1364 celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks, 1398 celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks,
1365 const celt_ener *bandE, int *pulses, int shortBlocks, int spread, 1399 const celt_ener *bandE, int *pulses, int shortBlocks, int spread,
1366 int dual_stereo, int intensity, int *tf_res, opus_int32 total_bits, 1400 int dual_stereo, int intensity, int *tf_res, opus_int32 total_bits,
1367 opus_int32 balance, ec_ctx *ec, int LM, int codedBands, 1401 opus_int32 balance, ec_ctx *ec, int LM, int codedBands,
1368 opus_uint32 *seed, int arch) 1402 opus_uint32 *seed, int complexity, int arch, int disable_inv)
1369 { 1403 {
1370 int i; 1404 int i;
1371 opus_int32 remaining_bits; 1405 opus_int32 remaining_bits;
1372 const opus_int16 * OPUS_RESTRICT eBands = m->eBands; 1406 const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1373 celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2; 1407 celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1374 VARDECL(celt_norm, _norm); 1408 VARDECL(celt_norm, _norm);
1409 VARDECL(celt_norm, _lowband_scratch);
1410 VARDECL(celt_norm, X_save);
1411 VARDECL(celt_norm, Y_save);
1412 VARDECL(celt_norm, X_save2);
1413 VARDECL(celt_norm, Y_save2);
1414 VARDECL(celt_norm, norm_save2);
1415 int resynth_alloc;
1375 celt_norm *lowband_scratch; 1416 celt_norm *lowband_scratch;
1376 int B; 1417 int B;
1377 int M; 1418 int M;
1378 int lowband_offset; 1419 int lowband_offset;
1379 int update_lowband = 1; 1420 int update_lowband = 1;
1380 int C = Y_ != NULL ? 2 : 1; 1421 int C = Y_ != NULL ? 2 : 1;
1381 int norm_offset; 1422 int norm_offset;
1423 int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1382 #ifdef RESYNTH 1424 #ifdef RESYNTH
1383 int resynth = 1; 1425 int resynth = 1;
1384 #else 1426 #else
1385 int resynth = !encode; 1427 int resynth = !encode || theta_rdo;
1386 #endif 1428 #endif
1387 struct band_ctx ctx; 1429 struct band_ctx ctx;
1388 SAVE_STACK; 1430 SAVE_STACK;
1389 1431
1390 M = 1<<LM; 1432 M = 1<<LM;
1391 B = shortBlocks ? M : 1; 1433 B = shortBlocks ? M : 1;
1392 norm_offset = M*eBands[start]; 1434 norm_offset = M*eBands[start];
1393 /* No need to allocate norm for the last band because we don't need an 1435 /* No need to allocate norm for the last band because we don't need an
1394 output in that band. */ 1436 output in that band. */
1395 ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm); 1437 ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1396 norm = _norm; 1438 norm = _norm;
1397 norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset; 1439 norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset;
1398 /* We can use the last band as scratch space because we don't need that 1440
1399 scratch space for the last band. */ 1441 /* For decoding, we can use the last band as scratch space because we don't n eed that
1400 lowband_scratch = X_+M*eBands[m->nbEBands-1]; 1442 scratch space for the last band and we don't care about the data there unt il we're
1443 decoding the last band. */
1444 if (encode && resynth)
1445 resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1446 else
1447 resynth_alloc = ALLOC_NONE;
1448 ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1449 if (encode && resynth)
1450 lowband_scratch = _lowband_scratch;
1451 else
1452 lowband_scratch = X_+M*eBands[m->nbEBands-1];
1453 ALLOC(X_save, resynth_alloc, celt_norm);
1454 ALLOC(Y_save, resynth_alloc, celt_norm);
1455 ALLOC(X_save2, resynth_alloc, celt_norm);
1456 ALLOC(Y_save2, resynth_alloc, celt_norm);
1457 ALLOC(norm_save2, resynth_alloc, celt_norm);
1401 1458
1402 lowband_offset = 0; 1459 lowband_offset = 0;
1403 ctx.bandE = bandE; 1460 ctx.bandE = bandE;
1404 ctx.ec = ec; 1461 ctx.ec = ec;
1405 ctx.encode = encode; 1462 ctx.encode = encode;
1406 ctx.intensity = intensity; 1463 ctx.intensity = intensity;
1407 ctx.m = m; 1464 ctx.m = m;
1408 ctx.seed = *seed; 1465 ctx.seed = *seed;
1409 ctx.spread = spread; 1466 ctx.spread = spread;
1410 ctx.arch = arch; 1467 ctx.arch = arch;
1468 ctx.disable_inv = disable_inv;
1469 ctx.resynth = resynth;
1470 ctx.theta_round = 0;
1471 /* Avoid injecting noise in the first band on transients. */
1472 ctx.avoid_split_noise = B > 1;
1411 for (i=start;i<end;i++) 1473 for (i=start;i<end;i++)
1412 { 1474 {
1413 opus_int32 tell; 1475 opus_int32 tell;
1414 int b; 1476 int b;
1415 int N; 1477 int N;
1416 opus_int32 curr_balance; 1478 opus_int32 curr_balance;
1417 int effective_lowband=-1; 1479 int effective_lowband=-1;
1418 celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y; 1480 celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1419 int tf_change=0; 1481 int tf_change=0;
1420 unsigned x_cm; 1482 unsigned x_cm;
(...skipping 17 matching lines...) Expand all
1438 remaining_bits = total_bits-tell-1; 1500 remaining_bits = total_bits-tell-1;
1439 ctx.remaining_bits = remaining_bits; 1501 ctx.remaining_bits = remaining_bits;
1440 if (i <= codedBands-1) 1502 if (i <= codedBands-1)
1441 { 1503 {
1442 curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i)); 1504 curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1443 b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance))) ; 1505 b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance))) ;
1444 } else { 1506 } else {
1445 b = 0; 1507 b = 0;
1446 } 1508 }
1447 1509
1510 #ifdef ENABLE_UPDATE_DRAFT
1511 if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update _lowband || lowband_offset==0))
1512 lowband_offset = i;
1513 if (i == start+1)
1514 special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1515 #else
1448 if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowb and_offset==0)) 1516 if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowb and_offset==0))
1449 lowband_offset = i; 1517 lowband_offset = i;
1518 #endif
1450 1519
1451 tf_change = tf_res[i]; 1520 tf_change = tf_res[i];
1452 ctx.tf_change = tf_change; 1521 ctx.tf_change = tf_change;
1453 if (i>=m->effEBands) 1522 if (i>=m->effEBands)
1454 { 1523 {
1455 X=norm; 1524 X=norm;
1456 if (Y_!=NULL) 1525 if (Y_!=NULL)
1457 Y = norm; 1526 Y = norm;
1458 lowband_scratch = NULL; 1527 lowband_scratch = NULL;
1459 } 1528 }
1460 if (i==end-1) 1529 if (last && !theta_rdo)
1461 lowband_scratch = NULL; 1530 lowband_scratch = NULL;
1462 1531
1463 /* Get a conservative estimate of the collapse_mask's for the bands we're 1532 /* Get a conservative estimate of the collapse_mask's for the bands we're
1464 going to be folding from. */ 1533 going to be folding from. */
1465 if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change< 0)) 1534 if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change< 0))
1466 { 1535 {
1467 int fold_start; 1536 int fold_start;
1468 int fold_end; 1537 int fold_end;
1469 int fold_i; 1538 int fold_i;
1470 /* This ensures we never repeat spectral content within one band */ 1539 /* This ensures we never repeat spectral content within one band */
1471 effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N); 1540 effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1472 fold_start = lowband_offset; 1541 fold_start = lowband_offset;
1473 while(M*eBands[--fold_start] > effective_lowband+norm_offset); 1542 while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1474 fold_end = lowband_offset-1; 1543 fold_end = lowband_offset-1;
1544 #ifdef ENABLE_UPDATE_DRAFT
1545 while(++fold_end < i && M*eBands[fold_end] < effective_lowband+norm_off set+N);
1546 #else
1475 while(M*eBands[++fold_end] < effective_lowband+norm_offset+N); 1547 while(M*eBands[++fold_end] < effective_lowband+norm_offset+N);
1548 #endif
1476 x_cm = y_cm = 0; 1549 x_cm = y_cm = 0;
1477 fold_i = fold_start; do { 1550 fold_i = fold_start; do {
1478 x_cm |= collapse_masks[fold_i*C+0]; 1551 x_cm |= collapse_masks[fold_i*C+0];
1479 y_cm |= collapse_masks[fold_i*C+C-1]; 1552 y_cm |= collapse_masks[fold_i*C+C-1];
1480 } while (++fold_i<fold_end); 1553 } while (++fold_i<fold_end);
1481 } 1554 }
1482 /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost 1555 /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1483 always) be non-zero. */ 1556 always) be non-zero. */
1484 else 1557 else
1485 x_cm = y_cm = (1<<B)-1; 1558 x_cm = y_cm = (1<<B)-1;
(...skipping 12 matching lines...) Expand all
1498 { 1571 {
1499 x_cm = quant_band(&ctx, X, N, b/2, B, 1572 x_cm = quant_band(&ctx, X, N, b/2, B,
1500 effective_lowband != -1 ? norm+effective_lowband : NULL, LM, 1573 effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1501 last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, x_cm); 1574 last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, x_cm);
1502 y_cm = quant_band(&ctx, Y, N, b/2, B, 1575 y_cm = quant_band(&ctx, Y, N, b/2, B,
1503 effective_lowband != -1 ? norm2+effective_lowband : NULL, LM, 1576 effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1504 last?NULL:norm2+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, y_cm); 1577 last?NULL:norm2+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, y_cm);
1505 } else { 1578 } else {
1506 if (Y!=NULL) 1579 if (Y!=NULL)
1507 { 1580 {
1508 x_cm = quant_band_stereo(&ctx, X, Y, N, b, B, 1581 if (theta_rdo && i < intensity)
1509 effective_lowband != -1 ? norm+effective_lowband : NULL, LM, 1582 {
1510 last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm); 1583 ec_ctx ec_save, ec_save2;
1584 struct band_ctx ctx_save, ctx_save2;
1585 opus_val32 dist0, dist1;
1586 unsigned cm, cm2;
1587 int nstart_bytes, nend_bytes, save_bytes;
1588 unsigned char *bytes_buf;
1589 unsigned char bytes_save[1275];
1590 opus_val16 w[2];
1591 compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1592 /* Make a copy. */
1593 cm = x_cm|y_cm;
1594 ec_save = *ec;
1595 ctx_save = ctx;
1596 OPUS_COPY(X_save, X, N);
1597 OPUS_COPY(Y_save, Y, N);
1598 /* Encode and round down. */
1599 ctx.theta_round = -1;
1600 x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1601 effective_lowband != -1 ? norm+effective_lowband : NULL, LM ,
1602 last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm );
1603 dist0 = MULT16_32_Q15(w[0], celt_inner_prod(X_save, X, N, arch)) + MULT16_32_Q15(w[1], celt_inner_prod(Y_save, Y, N, arch));
1604
1605 /* Save first result. */
1606 cm2 = x_cm;
1607 ec_save2 = *ec;
1608 ctx_save2 = ctx;
1609 OPUS_COPY(X_save2, X, N);
1610 OPUS_COPY(Y_save2, Y, N);
1611 if (!last)
1612 OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1613 nstart_bytes = ec_save.offs;
1614 nend_bytes = ec_save.storage;
1615 bytes_buf = ec_save.buf+nstart_bytes;
1616 save_bytes = nend_bytes-nstart_bytes;
1617 OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1618
1619 /* Restore */
1620 *ec = ec_save;
1621 ctx = ctx_save;
1622 OPUS_COPY(X, X_save, N);
1623 OPUS_COPY(Y, Y_save, N);
1624 if (i == start+1)
1625 special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1626 /* Encode and round up. */
1627 ctx.theta_round = 1;
1628 x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1629 effective_lowband != -1 ? norm+effective_lowband : NULL, LM ,
1630 last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm );
1631 dist1 = MULT16_32_Q15(w[0], celt_inner_prod(X_save, X, N, arch)) + MULT16_32_Q15(w[1], celt_inner_prod(Y_save, Y, N, arch));
1632 if (dist0 >= dist1) {
1633 x_cm = cm2;
1634 *ec = ec_save2;
1635 ctx = ctx_save2;
1636 OPUS_COPY(X, X_save2, N);
1637 OPUS_COPY(Y, Y_save2, N);
1638 if (!last)
1639 OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1640 OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1641 }
1642 } else {
1643 ctx.theta_round = 0;
1644 x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1645 effective_lowband != -1 ? norm+effective_lowband : NULL, LM ,
1646 last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_ cm|y_cm);
1647 }
1511 } else { 1648 } else {
1512 x_cm = quant_band(&ctx, X, N, b, B, 1649 x_cm = quant_band(&ctx, X, N, b, B,
1513 effective_lowband != -1 ? norm+effective_lowband : NULL, LM, 1650 effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1514 last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_ scratch, x_cm|y_cm); 1651 last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_scratc h, x_cm|y_cm);
1515 } 1652 }
1516 y_cm = x_cm; 1653 y_cm = x_cm;
1517 } 1654 }
1518 collapse_masks[i*C+0] = (unsigned char)x_cm; 1655 collapse_masks[i*C+0] = (unsigned char)x_cm;
1519 collapse_masks[i*C+C-1] = (unsigned char)y_cm; 1656 collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1520 balance += pulses[i] + tell; 1657 balance += pulses[i] + tell;
1521 1658
1522 /* Update the folding position only as long as we have 1 bit/sample depth. */ 1659 /* Update the folding position only as long as we have 1 bit/sample depth. */
1523 update_lowband = b>(N<<BITRES); 1660 update_lowband = b>(N<<BITRES);
1661 /* We only need to avoid noise on a split for the first band. After that, we
1662 have folding. */
1663 ctx.avoid_split_noise = 0;
1524 } 1664 }
1525 *seed = ctx.seed; 1665 *seed = ctx.seed;
1526 1666
1527 RESTORE_STACK; 1667 RESTORE_STACK;
1528 } 1668 }
1529 1669
OLDNEW
« no previous file with comments | « third_party/opus/src/celt/bands.h ('k') | third_party/opus/src/celt/celt.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698