diff options
Diffstat (limited to 'lib/rbcodec/codecs/libopus/celt/bands.c')
-rw-r--r-- | lib/rbcodec/codecs/libopus/celt/bands.c | 351 |
1 files changed, 247 insertions, 104 deletions
diff --git a/lib/rbcodec/codecs/libopus/celt/bands.c b/lib/rbcodec/codecs/libopus/celt/bands.c index caa70163b4..2702963c37 100644 --- a/lib/rbcodec/codecs/libopus/celt/bands.c +++ b/lib/rbcodec/codecs/libopus/celt/bands.c | |||
@@ -65,19 +65,19 @@ opus_uint32 celt_lcg_rand(opus_uint32 seed) | |||
65 | 65 | ||
66 | /* This is a cos() approximation designed to be bit-exact on any platform. Bit exactness | 66 | /* This is a cos() approximation designed to be bit-exact on any platform. Bit exactness |
67 | with this approximation is important because it has an impact on the bit allocation */ | 67 | with this approximation is important because it has an impact on the bit allocation */ |
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_sig_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_sig_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; |
@@ -90,13 +90,13 @@ static int bitexact_log2tan(int isin,int icos) | |||
90 | -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932); | 90 | -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932); |
91 | } | 91 | } |
92 | 92 | ||
93 | #if 0 | ||
94 | #ifdef FIXED_POINT | 93 | #ifdef FIXED_POINT |
95 | /* Compute the amplitude (sqrt energy) in each of the bands */ | 94 | /* Compute the amplitude (sqrt energy) in each of the bands */ |
96 | void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int LM) | 95 | void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int LM, int arch) |
97 | { | 96 | { |
98 | int i, c, N; | 97 | int i, c, N; |
99 | const opus_int16 *eBands = m->eBands; | 98 | const opus_int16 *eBands = m->eBands; |
99 | (void)arch; | ||
100 | N = m->shortMdctSize<<LM; | 100 | N = m->shortMdctSize<<LM; |
101 | c=0; do { | 101 | c=0; do { |
102 | for (i=0;i<end;i++) | 102 | for (i=0;i<end;i++) |
@@ -156,7 +156,7 @@ void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, cel | |||
156 | 156 | ||
157 | #else /* FIXED_POINT */ | 157 | #else /* FIXED_POINT */ |
158 | /* Compute the amplitude (sqrt energy) in each of the bands */ | 158 | /* Compute the amplitude (sqrt energy) in each of the bands */ |
159 | void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int LM) | 159 | void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int LM, int arch) |
160 | { | 160 | { |
161 | int i, c, N; | 161 | int i, c, N; |
162 | const opus_int16 *eBands = m->eBands; | 162 | const opus_int16 *eBands = m->eBands; |
@@ -165,7 +165,7 @@ void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *band | |||
165 | for (i=0;i<end;i++) | 165 | for (i=0;i<end;i++) |
166 | { | 166 | { |
167 | opus_val32 sum; | 167 | opus_val32 sum; |
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); | 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); |
169 | bandE[i+c*m->nbEBands] = celt_sqrt(sum); | 169 | bandE[i+c*m->nbEBands] = celt_sqrt(sum); |
170 | /*printf ("%f ", bandE[i+c*m->nbEBands]);*/ | 170 | /*printf ("%f ", bandE[i+c*m->nbEBands]);*/ |
171 | } | 171 | } |
@@ -191,7 +191,6 @@ void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, cel | |||
191 | } | 191 | } |
192 | 192 | ||
193 | #endif /* FIXED_POINT */ | 193 | #endif /* FIXED_POINT */ |
194 | #endif | ||
195 | 194 | ||
196 | /* De-normalise the energy to produce the synthesis from the unit-energy bands */ | 195 | /* De-normalise the energy to produce the synthesis from the unit-energy bands */ |
197 | void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, | 196 | void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, |
@@ -226,9 +225,9 @@ void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, | |||
226 | #endif | 225 | #endif |
227 | j=M*eBands[i]; | 226 | j=M*eBands[i]; |
228 | band_end = M*eBands[i+1]; | 227 | band_end = M*eBands[i+1]; |
229 | lg = ADD16(bandLogE[i], SHL16((opus_val16)eMeans[i],6)); | 228 | lg = SATURATE16(ADD32(bandLogE[i], SHL32((opus_val32)eMeans[i],6))); |
230 | #ifndef FIXED_POINT | 229 | #ifndef FIXED_POINT |
231 | g = celt_exp2(lg); | 230 | g = celt_exp2(MIN32(32.f, lg)); |
232 | #else | 231 | #else |
233 | /* Handle the integer part of the log energy */ | 232 | /* Handle the integer part of the log energy */ |
234 | shift = 16-(lg>>DB_SHIFT); | 233 | shift = 16-(lg>>DB_SHIFT); |
@@ -243,12 +242,12 @@ void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, | |||
243 | /* Handle extreme gains with negative shift. */ | 242 | /* Handle extreme gains with negative shift. */ |
244 | if (shift<0) | 243 | if (shift<0) |
245 | { | 244 | { |
246 | /* 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 |
247 | 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. |
248 | already corrupted. */ | 247 | This shouldn't trigger unless the bitstream is already corrupted. */ |
249 | if (shift < -2) | 248 | if (shift <= -2) |
250 | { | 249 | { |
251 | g = 32767; | 250 | g = 16384; |
252 | shift = -2; | 251 | shift = -2; |
253 | } | 252 | } |
254 | do { | 253 | do { |
@@ -268,7 +267,7 @@ void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, | |||
268 | /* This prevents energy collapse for transients with multiple short MDCTs */ | 267 | /* This prevents energy collapse for transients with multiple short MDCTs */ |
269 | void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_masks, int LM, int C, int size, | 268 | void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_masks, int LM, int C, int size, |
270 | int start, int end, const opus_val16 *logE, const opus_val16 *prev1logE, | 269 | int start, int end, const opus_val16 *logE, const opus_val16 *prev1logE, |
271 | const opus_val16 *prev2logE, const int *pulses, opus_uint32 seed) | 270 | const opus_val16 *prev2logE, const int *pulses, opus_uint32 seed, int arch) |
272 | { | 271 | { |
273 | int c, i, j, k; | 272 | int c, i, j, k; |
274 | for (i=start;i<end;i++) | 273 | for (i=start;i<end;i++) |
@@ -283,7 +282,7 @@ void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_mas | |||
283 | 282 | ||
284 | N0 = m->eBands[i+1]-m->eBands[i]; | 283 | N0 = m->eBands[i+1]-m->eBands[i]; |
285 | /* depth in 1/8 bits */ | 284 | /* depth in 1/8 bits */ |
286 | celt_assert(pulses[i]>=0); | 285 | celt_sig_assert(pulses[i]>=0); |
287 | depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i]))>>LM; | 286 | depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i]))>>LM; |
288 | 287 | ||
289 | #ifdef FIXED_POINT | 288 | #ifdef FIXED_POINT |
@@ -357,11 +356,35 @@ void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_mas | |||
357 | } | 356 | } |
358 | /* We just added some energy, so we need to renormalise */ | 357 | /* We just added some energy, so we need to renormalise */ |
359 | if (renormalize) | 358 | if (renormalize) |
360 | renormalise_vector(X, N0<<LM, Q15ONE); | 359 | renormalise_vector(X, N0<<LM, Q15ONE, arch); |
361 | } while (++c<C); | 360 | } while (++c<C); |
362 | } | 361 | } |
363 | } | 362 | } |
364 | 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 | #ifdef 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 | #ifdef 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 | |||
365 | static void intensity_stereo(const CELTMode *m, celt_norm * OPUS_RESTRICT X, const 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, const celt_norm * OPUS_RESTRICT Y, const celt_ener *bandE, int bandID, int N) |
366 | { | 389 | { |
367 | int i = bandID; | 390 | int i = bandID; |
@@ -400,7 +423,7 @@ static void stereo_split(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT | |||
400 | } | 423 | } |
401 | } | 424 | } |
402 | 425 | ||
403 | static void stereo_merge(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, opus_val16 mid, int N) | 426 | static void stereo_merge(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, opus_val16 mid, int N, int arch) |
404 | { | 427 | { |
405 | int j; | 428 | int j; |
406 | opus_val32 xp=0, side=0; | 429 | opus_val32 xp=0, side=0; |
@@ -412,11 +435,11 @@ static void stereo_merge(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT | |||
412 | opus_val32 t, lgain, rgain; | 435 | opus_val32 t, lgain, rgain; |
413 | 436 | ||
414 | /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */ | 437 | /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */ |
415 | dual_inner_prod(Y, X, Y, N, &xp, &side); | 438 | dual_inner_prod(Y, X, Y, N, &xp, &side, arch); |
416 | /* Compensating for the mid normalization */ | 439 | /* Compensating for the mid normalization */ |
417 | xp = MULT16_32_Q15(mid, xp); | 440 | xp = MULT16_32_Q15(mid, xp); |
418 | /* mid and side are in Q15, not Q14 like X and Y */ | 441 | /* mid and side are in Q15, not Q14 like X and Y */ |
419 | mid2 = SHR32(mid, 1); | 442 | mid2 = SHR16(mid, 1); |
420 | El = MULT16_16(mid2, mid2) + side - 2*xp; | 443 | El = MULT16_16(mid2, mid2) + side - 2*xp; |
421 | Er = MULT16_16(mid2, mid2) + side + 2*xp; | 444 | Er = MULT16_16(mid2, mid2) + side + 2*xp; |
422 | if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28)) | 445 | if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28)) |
@@ -452,11 +475,10 @@ static void stereo_merge(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT | |||
452 | } | 475 | } |
453 | } | 476 | } |
454 | 477 | ||
455 | #if 0 | ||
456 | /* Decide whether we should spread the pulses in the current frame */ | 478 | /* Decide whether we should spread the pulses in the current frame */ |
457 | int spreading_decision(const CELTMode *m, const celt_norm *X, int *average, | 479 | int spreading_decision(const CELTMode *m, const celt_norm *X, int *average, |
458 | int last_decision, int *hf_average, int *tapset_decision, int update_hf, | 480 | int last_decision, int *hf_average, int *tapset_decision, int update_hf, |
459 | int end, int C, int M) | 481 | int end, int C, int M, const int *spread_weight) |
460 | { | 482 | { |
461 | int i, c, N0; | 483 | int i, c, N0; |
462 | int sum = 0, nbBands=0; | 484 | int sum = 0, nbBands=0; |
@@ -497,8 +519,8 @@ int spreading_decision(const CELTMode *m, const celt_norm *X, int *average, | |||
497 | if (i>m->nbEBands-4) | 519 | if (i>m->nbEBands-4) |
498 | hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N); | 520 | hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N); |
499 | tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N); | 521 | tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N); |
500 | sum += tmp*256; | 522 | sum += tmp*spread_weight[i]; |
501 | nbBands++; | 523 | nbBands+=spread_weight[i]; |
502 | } | 524 | } |
503 | } while (++c<C); | 525 | } while (++c<C); |
504 | 526 | ||
@@ -522,7 +544,7 @@ int spreading_decision(const CELTMode *m, const celt_norm *X, int *average, | |||
522 | /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/ | 544 | /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/ |
523 | celt_assert(nbBands>0); /* end has to be non-zero */ | 545 | celt_assert(nbBands>0); /* end has to be non-zero */ |
524 | celt_assert(sum>=0); | 546 | celt_assert(sum>=0); |
525 | sum = celt_udiv(sum, nbBands); | 547 | sum = celt_udiv((opus_int32)sum<<8, nbBands); |
526 | /* Recursive averaging */ | 548 | /* Recursive averaging */ |
527 | sum = (sum+*average)>>1; | 549 | sum = (sum+*average)>>1; |
528 | *average = sum; | 550 | *average = sum; |
@@ -546,7 +568,6 @@ int spreading_decision(const CELTMode *m, const celt_norm *X, int *average, | |||
546 | #endif | 568 | #endif |
547 | return decision; | 569 | return decision; |
548 | } | 570 | } |
549 | #endif | ||
550 | 571 | ||
551 | /* Indexing table for converting from natural Hadamard to ordery Hadamard | 572 | /* Indexing table for converting from natural Hadamard to ordery Hadamard |
552 | This is essentially a bit-reversed Gray, on top of which we've added | 573 | This is essentially a bit-reversed Gray, on top of which we've added |
@@ -651,6 +672,7 @@ static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo) | |||
651 | 672 | ||
652 | struct band_ctx { | 673 | struct band_ctx { |
653 | int encode; | 674 | int encode; |
675 | int resynth; | ||
654 | const CELTMode *m; | 676 | const CELTMode *m; |
655 | int i; | 677 | int i; |
656 | int intensity; | 678 | int intensity; |
@@ -660,6 +682,10 @@ struct band_ctx { | |||
660 | opus_int32 remaining_bits; | 682 | opus_int32 remaining_bits; |
661 | const celt_ener *bandE; | 683 | const celt_ener *bandE; |
662 | opus_uint32 seed; | 684 | opus_uint32 seed; |
685 | int arch; | ||
686 | int theta_round; | ||
687 | int disable_inv; | ||
688 | int avoid_split_noise; | ||
663 | }; | 689 | }; |
664 | 690 | ||
665 | struct split_ctx { | 691 | struct split_ctx { |
@@ -711,14 +737,41 @@ static void compute_theta(struct band_ctx *ctx, struct split_ctx *sctx, | |||
711 | 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 |
712 | 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 |
713 | 2) they are orthogonal. */ | 739 | 2) they are orthogonal. */ |
714 | itheta = stereo_itheta(X, Y, stereo, N); | 740 | itheta = stereo_itheta(X, Y, stereo, N, ctx->arch); |
715 | } | 741 | } |
716 | tell = ec_tell_frac(ec); | 742 | tell = ec_tell_frac(ec); |
717 | if (qn!=1) | 743 | if (qn!=1) |
718 | { | 744 | { |
719 | if (encode) | 745 | if (encode) |
720 | itheta = (itheta*qn+8192)>>14; | 746 | { |
721 | 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 allocation | ||
753 | to inject noise on one side. If so, make sure the energy of that 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 | } | ||
722 | /* 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 |
723 | 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. */ |
724 | if (stereo && N>2) | 777 | if (stereo && N>2) |
@@ -796,7 +849,7 @@ static void compute_theta(struct band_ctx *ctx, struct split_ctx *sctx, | |||
796 | } else if (stereo) { | 849 | } else if (stereo) { |
797 | if (encode) | 850 | if (encode) |
798 | { | 851 | { |
799 | inv = itheta > 8192; | 852 | inv = itheta > 8192 && !ctx->disable_inv; |
800 | if (inv) | 853 | if (inv) |
801 | { | 854 | { |
802 | int j; | 855 | int j; |
@@ -813,6 +866,9 @@ static void compute_theta(struct band_ctx *ctx, struct split_ctx *sctx, | |||
813 | inv = ec_dec_bit_logp(ec, 2); | 866 | inv = ec_dec_bit_logp(ec, 2); |
814 | } else | 867 | } else |
815 | inv = 0; | 868 | inv = 0; |
869 | /* inv flag override to avoid problems with downmixing. */ | ||
870 | if (ctx->disable_inv) | ||
871 | inv = 0; | ||
816 | itheta = 0; | 872 | itheta = 0; |
817 | } | 873 | } |
818 | qalloc = ec_tell_frac(ec) - tell; | 874 | qalloc = ec_tell_frac(ec) - tell; |
@@ -848,11 +904,6 @@ static void compute_theta(struct band_ctx *ctx, struct split_ctx *sctx, | |||
848 | 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, |
849 | celt_norm *lowband_out) | 905 | celt_norm *lowband_out) |
850 | { | 906 | { |
851 | #ifdef RESYNTH | ||
852 | int resynth = 1; | ||
853 | #else | ||
854 | int resynth = !ctx->encode; | ||
855 | #endif | ||
856 | int c; | 907 | int c; |
857 | int stereo; | 908 | int stereo; |
858 | celt_norm *x = X; | 909 | celt_norm *x = X; |
@@ -877,7 +928,7 @@ static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y, | |||
877 | ctx->remaining_bits -= 1<<BITRES; | 928 | ctx->remaining_bits -= 1<<BITRES; |
878 | b-=1<<BITRES; | 929 | b-=1<<BITRES; |
879 | } | 930 | } |
880 | if (resynth) | 931 | if (ctx->resynth) |
881 | x[0] = sign ? -NORM_SCALING : NORM_SCALING; | 932 | x[0] = sign ? -NORM_SCALING : NORM_SCALING; |
882 | x = Y; | 933 | x = Y; |
883 | } while (++c<1+stereo); | 934 | } while (++c<1+stereo); |
@@ -902,11 +953,6 @@ static unsigned quant_partition(struct band_ctx *ctx, celt_norm *X, | |||
902 | int B0=B; | 953 | int B0=B; |
903 | opus_val16 mid=0, side=0; | 954 | opus_val16 mid=0, side=0; |
904 | unsigned cm=0; | 955 | unsigned cm=0; |
905 | #ifdef RESYNTH | ||
906 | int resynth = 1; | ||
907 | #else | ||
908 | int resynth = !ctx->encode; | ||
909 | #endif | ||
910 | celt_norm *Y=NULL; | 956 | celt_norm *Y=NULL; |
911 | int encode; | 957 | int encode; |
912 | const CELTMode *m; | 958 | const CELTMode *m; |
@@ -938,8 +984,7 @@ static unsigned quant_partition(struct band_ctx *ctx, celt_norm *X, | |||
938 | fill = (fill&1)|(fill<<1); | 984 | fill = (fill&1)|(fill<<1); |
939 | B = (B+1)>>1; | 985 | B = (B+1)>>1; |
940 | 986 | ||
941 | compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, | 987 | compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill); |
942 | LM, 0, &fill); | ||
943 | imid = sctx.imid; | 988 | imid = sctx.imid; |
944 | iside = sctx.iside; | 989 | iside = sctx.iside; |
945 | delta = sctx.delta; | 990 | delta = sctx.delta; |
@@ -973,24 +1018,20 @@ static unsigned quant_partition(struct band_ctx *ctx, celt_norm *X, | |||
973 | rebalance = ctx->remaining_bits; | 1018 | rebalance = ctx->remaining_bits; |
974 | if (mbits >= sbits) | 1019 | if (mbits >= sbits) |
975 | { | 1020 | { |
976 | cm = quant_partition(ctx, X, N, mbits, B, | 1021 | cm = quant_partition(ctx, X, N, mbits, B, lowband, LM, |
977 | lowband, LM, | ||
978 | MULT16_16_P15(gain,mid), fill); | 1022 | MULT16_16_P15(gain,mid), fill); |
979 | rebalance = mbits - (rebalance-ctx->remaining_bits); | 1023 | rebalance = mbits - (rebalance-ctx->remaining_bits); |
980 | if (rebalance > 3<<BITRES && itheta!=0) | 1024 | if (rebalance > 3<<BITRES && itheta!=0) |
981 | sbits += rebalance - (3<<BITRES); | 1025 | sbits += rebalance - (3<<BITRES); |
982 | cm |= quant_partition(ctx, Y, N, sbits, B, | 1026 | cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM, |
983 | next_lowband2, LM, | ||
984 | MULT16_16_P15(gain,side), fill>>B)<<(B0>>1); | 1027 | MULT16_16_P15(gain,side), fill>>B)<<(B0>>1); |
985 | } else { | 1028 | } else { |
986 | cm = quant_partition(ctx, Y, N, sbits, B, | 1029 | cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM, |
987 | next_lowband2, LM, | ||
988 | MULT16_16_P15(gain,side), fill>>B)<<(B0>>1); | 1030 | MULT16_16_P15(gain,side), fill>>B)<<(B0>>1); |
989 | rebalance = sbits - (rebalance-ctx->remaining_bits); | 1031 | rebalance = sbits - (rebalance-ctx->remaining_bits); |
990 | if (rebalance > 3<<BITRES && itheta!=16384) | 1032 | if (rebalance > 3<<BITRES && itheta!=16384) |
991 | mbits += rebalance - (3<<BITRES); | 1033 | mbits += rebalance - (3<<BITRES); |
992 | cm |= quant_partition(ctx, X, N, mbits, B, | 1034 | cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM, |
993 | lowband, LM, | ||
994 | MULT16_16_P15(gain,mid), fill); | 1035 | MULT16_16_P15(gain,mid), fill); |
995 | } | 1036 | } |
996 | } else { | 1037 | } else { |
@@ -1015,18 +1056,14 @@ static unsigned quant_partition(struct band_ctx *ctx, celt_norm *X, | |||
1015 | /* Finally do the actual quantization */ | 1056 | /* Finally do the actual quantization */ |
1016 | if (encode) | 1057 | if (encode) |
1017 | { | 1058 | { |
1018 | cm = alg_quant(X, N, K, spread, B, ec | 1059 | cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth, ctx->arch); |
1019 | #ifdef RESYNTH | ||
1020 | , gain | ||
1021 | #endif | ||
1022 | ); | ||
1023 | } else { | 1060 | } else { |
1024 | cm = alg_unquant(X, N, K, spread, B, ec, gain); | 1061 | cm = alg_unquant(X, N, K, spread, B, ec, gain); |
1025 | } | 1062 | } |
1026 | } else { | 1063 | } else { |
1027 | /* If there's no pulse, fill the band anyway */ | 1064 | /* If there's no pulse, fill the band anyway */ |
1028 | int j; | 1065 | int j; |
1029 | if (resynth) | 1066 | if (ctx->resynth) |
1030 | { | 1067 | { |
1031 | unsigned cm_mask; | 1068 | unsigned cm_mask; |
1032 | /* 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 |
@@ -1059,7 +1096,7 @@ static unsigned quant_partition(struct band_ctx *ctx, celt_norm *X, | |||
1059 | } | 1096 | } |
1060 | cm = fill; | 1097 | cm = fill; |
1061 | } | 1098 | } |
1062 | renormalise_vector(X, N, gain); | 1099 | renormalise_vector(X, N, gain, ctx->arch); |
1063 | } | 1100 | } |
1064 | } | 1101 | } |
1065 | } | 1102 | } |
@@ -1083,11 +1120,6 @@ static unsigned quant_band(struct band_ctx *ctx, celt_norm *X, | |||
1083 | int recombine=0; | 1120 | int recombine=0; |
1084 | int longBlocks; | 1121 | int longBlocks; |
1085 | unsigned cm=0; | 1122 | unsigned cm=0; |
1086 | #ifdef RESYNTH | ||
1087 | int resynth = 1; | ||
1088 | #else | ||
1089 | int resynth = !ctx->encode; | ||
1090 | #endif | ||
1091 | int k; | 1123 | int k; |
1092 | int encode; | 1124 | int encode; |
1093 | int tf_change; | 1125 | int tf_change; |
@@ -1154,11 +1186,10 @@ static unsigned quant_band(struct band_ctx *ctx, celt_norm *X, | |||
1154 | deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks); | 1186 | deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks); |
1155 | } | 1187 | } |
1156 | 1188 | ||
1157 | cm = quant_partition(ctx, X, N, b, B, lowband, | 1189 | cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill); |
1158 | LM, gain, fill); | ||
1159 | 1190 | ||
1160 | /* 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 */ |
1161 | if (resynth) | 1192 | if (ctx->resynth) |
1162 | { | 1193 | { |
1163 | /* Undo the sample reorganization going from time order to frequency order */ | 1194 | /* Undo the sample reorganization going from time order to frequency order */ |
1164 | if (B0>1) | 1195 | if (B0>1) |
@@ -1211,11 +1242,6 @@ static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm | |||
1211 | int inv = 0; | 1242 | int inv = 0; |
1212 | opus_val16 mid=0, side=0; | 1243 | opus_val16 mid=0, side=0; |
1213 | unsigned cm=0; | 1244 | unsigned cm=0; |
1214 | #ifdef RESYNTH | ||
1215 | int resynth = 1; | ||
1216 | #else | ||
1217 | int resynth = !ctx->encode; | ||
1218 | #endif | ||
1219 | int mbits, sbits, delta; | 1245 | int mbits, sbits, delta; |
1220 | int itheta; | 1246 | int itheta; |
1221 | int qalloc; | 1247 | int qalloc; |
@@ -1235,8 +1261,7 @@ static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm | |||
1235 | 1261 | ||
1236 | orig_fill = fill; | 1262 | orig_fill = fill; |
1237 | 1263 | ||
1238 | compute_theta(ctx, &sctx, X, Y, N, &b, B, B, | 1264 | compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill); |
1239 | LM, 1, &fill); | ||
1240 | inv = sctx.inv; | 1265 | inv = sctx.inv; |
1241 | imid = sctx.imid; | 1266 | imid = sctx.imid; |
1242 | iside = sctx.iside; | 1267 | iside = sctx.iside; |
@@ -1284,13 +1309,13 @@ static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm | |||
1284 | sign = 1-2*sign; | 1309 | sign = 1-2*sign; |
1285 | /* 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 |
1286 | itheta==16384, we'll have cleared the low bits of fill. */ | 1311 | itheta==16384, we'll have cleared the low bits of fill. */ |
1287 | cm = quant_band(ctx, x2, N, mbits, B, lowband, | 1312 | cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q15ONE, |
1288 | LM, lowband_out, Q15ONE, lowband_scratch, orig_fill); | 1313 | lowband_scratch, orig_fill); |
1289 | /* 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), |
1290 | 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. */ |
1291 | y2[0] = -sign*x2[1]; | 1316 | y2[0] = -sign*x2[1]; |
1292 | y2[1] = sign*x2[0]; | 1317 | y2[1] = sign*x2[0]; |
1293 | if (resynth) | 1318 | if (ctx->resynth) |
1294 | { | 1319 | { |
1295 | celt_norm tmp; | 1320 | celt_norm tmp; |
1296 | X[0] = MULT16_16_Q15(mid, X[0]); | 1321 | X[0] = MULT16_16_Q15(mid, X[0]); |
@@ -1317,41 +1342,35 @@ static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm | |||
1317 | { | 1342 | { |
1318 | /* 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 |
1319 | mid for folding later. */ | 1344 | mid for folding later. */ |
1320 | cm = quant_band(ctx, X, N, mbits, B, | 1345 | cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q15ONE, |
1321 | lowband, LM, lowband_out, | 1346 | lowband_scratch, fill); |
1322 | Q15ONE, lowband_scratch, fill); | ||
1323 | rebalance = mbits - (rebalance-ctx->remaining_bits); | 1347 | rebalance = mbits - (rebalance-ctx->remaining_bits); |
1324 | if (rebalance > 3<<BITRES && itheta!=0) | 1348 | if (rebalance > 3<<BITRES && itheta!=0) |
1325 | sbits += rebalance - (3<<BITRES); | 1349 | sbits += rebalance - (3<<BITRES); |
1326 | 1350 | ||
1327 | /* 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 |
1328 | folding will be done to the side. */ | 1352 | folding will be done to the side. */ |
1329 | cm |= quant_band(ctx, Y, N, sbits, B, | 1353 | cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B); |
1330 | NULL, LM, NULL, | ||
1331 | side, NULL, fill>>B); | ||
1332 | } else { | 1354 | } else { |
1333 | /* 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 |
1334 | folding will be done to the side. */ | 1356 | folding will be done to the side. */ |
1335 | cm = quant_band(ctx, Y, N, sbits, B, | 1357 | cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B); |
1336 | NULL, LM, NULL, | ||
1337 | side, NULL, fill>>B); | ||
1338 | rebalance = sbits - (rebalance-ctx->remaining_bits); | 1358 | rebalance = sbits - (rebalance-ctx->remaining_bits); |
1339 | if (rebalance > 3<<BITRES && itheta!=16384) | 1359 | if (rebalance > 3<<BITRES && itheta!=16384) |
1340 | mbits += rebalance - (3<<BITRES); | 1360 | mbits += rebalance - (3<<BITRES); |
1341 | /* 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 |
1342 | mid for folding later. */ | 1362 | mid for folding later. */ |
1343 | cm |= quant_band(ctx, X, N, mbits, B, | 1363 | cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q15ONE, |
1344 | lowband, LM, lowband_out, | 1364 | lowband_scratch, fill); |
1345 | Q15ONE, lowband_scratch, fill); | ||
1346 | } | 1365 | } |
1347 | } | 1366 | } |
1348 | 1367 | ||
1349 | 1368 | ||
1350 | /* 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 */ |
1351 | if (resynth) | 1370 | if (ctx->resynth) |
1352 | { | 1371 | { |
1353 | if (N!=2) | 1372 | if (N!=2) |
1354 | stereo_merge(X, Y, mid, N); | 1373 | stereo_merge(X, Y, mid, N, ctx->arch); |
1355 | if (inv) | 1374 | if (inv) |
1356 | { | 1375 | { |
1357 | int j; | 1376 | int j; |
@@ -1362,17 +1381,38 @@ static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm | |||
1362 | return cm; | 1381 | return cm; |
1363 | } | 1382 | } |
1364 | 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 second 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 | } | ||
1365 | 1396 | ||
1366 | 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, |
1367 | celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks, const celt_ener *bandE, int *pulses, | 1398 | celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks, |
1368 | int shortBlocks, int spread, int dual_stereo, int intensity, int *tf_res, | 1399 | const celt_ener *bandE, int *pulses, int shortBlocks, int spread, |
1369 | opus_int32 total_bits, opus_int32 balance, ec_ctx *ec, int LM, int codedBands, opus_uint32 *seed) | 1400 | int dual_stereo, int intensity, int *tf_res, opus_int32 total_bits, |
1401 | opus_int32 balance, ec_ctx *ec, int LM, int codedBands, | ||
1402 | opus_uint32 *seed, int complexity, int arch, int disable_inv) | ||
1370 | { | 1403 | { |
1371 | int i; | 1404 | int i; |
1372 | opus_int32 remaining_bits; | 1405 | opus_int32 remaining_bits; |
1373 | const opus_int16 * OPUS_RESTRICT eBands = m->eBands; | 1406 | const opus_int16 * OPUS_RESTRICT eBands = m->eBands; |
1374 | celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2; | 1407 | celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2; |
1375 | 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; | ||
1376 | celt_norm *lowband_scratch; | 1416 | celt_norm *lowband_scratch; |
1377 | int B; | 1417 | int B; |
1378 | int M; | 1418 | int M; |
@@ -1380,10 +1420,11 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1380 | int update_lowband = 1; | 1420 | int update_lowband = 1; |
1381 | int C = Y_ != NULL ? 2 : 1; | 1421 | int C = Y_ != NULL ? 2 : 1; |
1382 | int norm_offset; | 1422 | int norm_offset; |
1423 | int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8; | ||
1383 | #ifdef RESYNTH | 1424 | #ifdef RESYNTH |
1384 | int resynth = 1; | 1425 | int resynth = 1; |
1385 | #else | 1426 | #else |
1386 | int resynth = !encode; | 1427 | int resynth = !encode || theta_rdo; |
1387 | #endif | 1428 | #endif |
1388 | struct band_ctx ctx; | 1429 | struct band_ctx ctx; |
1389 | SAVE_STACK; | 1430 | SAVE_STACK; |
@@ -1396,9 +1437,24 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1396 | 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); |
1397 | norm = _norm; | 1438 | norm = _norm; |
1398 | norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset; | 1439 | norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset; |
1399 | /* We can use the last band as scratch space because we don't need that | 1440 | |
1400 | scratch space for the last band. */ | 1441 | /* For decoding, we can use the last band as scratch space because we don't need that |
1401 | lowband_scratch = X_+M*eBands[m->nbEBands-1]; | 1442 | scratch space for the last band and we don't care about the data there until 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); | ||
1402 | 1458 | ||
1403 | lowband_offset = 0; | 1459 | lowband_offset = 0; |
1404 | ctx.bandE = bandE; | 1460 | ctx.bandE = bandE; |
@@ -1408,6 +1464,12 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1408 | ctx.m = m; | 1464 | ctx.m = m; |
1409 | ctx.seed = *seed; | 1465 | ctx.seed = *seed; |
1410 | ctx.spread = spread; | 1466 | ctx.spread = spread; |
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; |
@@ -1430,6 +1492,7 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1430 | else | 1492 | else |
1431 | Y = NULL; | 1493 | Y = NULL; |
1432 | N = M*eBands[i+1]-M*eBands[i]; | 1494 | N = M*eBands[i+1]-M*eBands[i]; |
1495 | celt_assert(N > 0); | ||
1433 | tell = ec_tell_frac(ec); | 1496 | tell = ec_tell_frac(ec); |
1434 | 1497 | ||
1435 | /* Compute how many bits we want to allocate to this band */ | 1498 | /* Compute how many bits we want to allocate to this band */ |
@@ -1445,8 +1508,15 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1445 | b = 0; | 1508 | b = 0; |
1446 | } | 1509 | } |
1447 | 1510 | ||
1511 | #ifndef DISABLE_UPDATE_DRAFT | ||
1512 | if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0)) | ||
1513 | lowband_offset = i; | ||
1514 | if (i == start+1) | ||
1515 | special_hybrid_folding(m, norm, norm2, start, M, dual_stereo); | ||
1516 | #else | ||
1448 | if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0)) | 1517 | if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0)) |
1449 | lowband_offset = i; | 1518 | lowband_offset = i; |
1519 | #endif | ||
1450 | 1520 | ||
1451 | tf_change = tf_res[i]; | 1521 | tf_change = tf_res[i]; |
1452 | ctx.tf_change = tf_change; | 1522 | ctx.tf_change = tf_change; |
@@ -1457,7 +1527,7 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1457 | Y = norm; | 1527 | Y = norm; |
1458 | lowband_scratch = NULL; | 1528 | lowband_scratch = NULL; |
1459 | } | 1529 | } |
1460 | if (i==end-1) | 1530 | if (last && !theta_rdo) |
1461 | lowband_scratch = NULL; | 1531 | lowband_scratch = NULL; |
1462 | 1532 | ||
1463 | /* Get a conservative estimate of the collapse_mask's for the bands we're | 1533 | /* Get a conservative estimate of the collapse_mask's for the bands we're |
@@ -1472,7 +1542,11 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1472 | fold_start = lowband_offset; | 1542 | fold_start = lowband_offset; |
1473 | while(M*eBands[--fold_start] > effective_lowband+norm_offset); | 1543 | while(M*eBands[--fold_start] > effective_lowband+norm_offset); |
1474 | fold_end = lowband_offset-1; | 1544 | fold_end = lowband_offset-1; |
1545 | #ifndef DISABLE_UPDATE_DRAFT | ||
1546 | while(++fold_end < i && M*eBands[fold_end] < effective_lowband+norm_offset+N); | ||
1547 | #else | ||
1475 | while(M*eBands[++fold_end] < effective_lowband+norm_offset+N); | 1548 | while(M*eBands[++fold_end] < effective_lowband+norm_offset+N); |
1549 | #endif | ||
1476 | x_cm = y_cm = 0; | 1550 | x_cm = y_cm = 0; |
1477 | fold_i = fold_start; do { | 1551 | fold_i = fold_start; do { |
1478 | x_cm |= collapse_masks[fold_i*C+0]; | 1552 | x_cm |= collapse_masks[fold_i*C+0]; |
@@ -1505,13 +1579,79 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1505 | } else { | 1579 | } else { |
1506 | if (Y!=NULL) | 1580 | if (Y!=NULL) |
1507 | { | 1581 | { |
1508 | x_cm = quant_band_stereo(&ctx, X, Y, N, b, B, | 1582 | if (theta_rdo && i < intensity) |
1509 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, | 1583 | { |
1510 | last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm); | 1584 | ec_ctx ec_save, ec_save2; |
1585 | struct band_ctx ctx_save, ctx_save2; | ||
1586 | opus_val32 dist0, dist1; | ||
1587 | unsigned cm, cm2; | ||
1588 | int nstart_bytes, nend_bytes, save_bytes; | ||
1589 | unsigned char *bytes_buf; | ||
1590 | unsigned char bytes_save[1275]; | ||
1591 | opus_val16 w[2]; | ||
1592 | compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w); | ||
1593 | /* Make a copy. */ | ||
1594 | cm = x_cm|y_cm; | ||
1595 | ec_save = *ec; | ||
1596 | ctx_save = ctx; | ||
1597 | OPUS_COPY(X_save, X, N); | ||
1598 | OPUS_COPY(Y_save, Y, N); | ||
1599 | /* Encode and round down. */ | ||
1600 | ctx.theta_round = -1; | ||
1601 | x_cm = quant_band_stereo(&ctx, X, Y, N, b, B, | ||
1602 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, | ||
1603 | last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm); | ||
1604 | 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)); | ||
1605 | |||
1606 | /* Save first result. */ | ||
1607 | cm2 = x_cm; | ||
1608 | ec_save2 = *ec; | ||
1609 | ctx_save2 = ctx; | ||
1610 | OPUS_COPY(X_save2, X, N); | ||
1611 | OPUS_COPY(Y_save2, Y, N); | ||
1612 | if (!last) | ||
1613 | OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N); | ||
1614 | nstart_bytes = ec_save.offs; | ||
1615 | nend_bytes = ec_save.storage; | ||
1616 | bytes_buf = ec_save.buf+nstart_bytes; | ||
1617 | save_bytes = nend_bytes-nstart_bytes; | ||
1618 | OPUS_COPY(bytes_save, bytes_buf, save_bytes); | ||
1619 | |||
1620 | /* Restore */ | ||
1621 | *ec = ec_save; | ||
1622 | ctx = ctx_save; | ||
1623 | OPUS_COPY(X, X_save, N); | ||
1624 | OPUS_COPY(Y, Y_save, N); | ||
1625 | #ifndef DISABLE_UPDATE_DRAFT | ||
1626 | if (i == start+1) | ||
1627 | special_hybrid_folding(m, norm, norm2, start, M, dual_stereo); | ||
1628 | #endif | ||
1629 | /* Encode and round up. */ | ||
1630 | ctx.theta_round = 1; | ||
1631 | x_cm = quant_band_stereo(&ctx, X, Y, N, b, B, | ||
1632 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, | ||
1633 | last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm); | ||
1634 | 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)); | ||
1635 | if (dist0 >= dist1) { | ||
1636 | x_cm = cm2; | ||
1637 | *ec = ec_save2; | ||
1638 | ctx = ctx_save2; | ||
1639 | OPUS_COPY(X, X_save2, N); | ||
1640 | OPUS_COPY(Y, Y_save2, N); | ||
1641 | if (!last) | ||
1642 | OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N); | ||
1643 | OPUS_COPY(bytes_buf, bytes_save, save_bytes); | ||
1644 | } | ||
1645 | } else { | ||
1646 | ctx.theta_round = 0; | ||
1647 | x_cm = quant_band_stereo(&ctx, X, Y, N, b, B, | ||
1648 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, | ||
1649 | last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm); | ||
1650 | } | ||
1511 | } else { | 1651 | } else { |
1512 | x_cm = quant_band(&ctx, X, N, b, B, | 1652 | x_cm = quant_band(&ctx, X, N, b, B, |
1513 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, | 1653 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, |
1514 | last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, x_cm|y_cm); | 1654 | last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, x_cm|y_cm); |
1515 | } | 1655 | } |
1516 | y_cm = x_cm; | 1656 | y_cm = x_cm; |
1517 | } | 1657 | } |
@@ -1521,6 +1661,9 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, | |||
1521 | 1661 | ||
1522 | /* Update the folding position only as long as we have 1 bit/sample depth. */ | 1662 | /* Update the folding position only as long as we have 1 bit/sample depth. */ |
1523 | update_lowband = b>(N<<BITRES); | 1663 | update_lowband = b>(N<<BITRES); |
1664 | /* We only need to avoid noise on a split for the first band. After that, we | ||
1665 | have folding. */ | ||
1666 | ctx.avoid_split_noise = 0; | ||
1524 | } | 1667 | } |
1525 | *seed = ctx.seed; | 1668 | *seed = ctx.seed; |
1526 | 1669 | ||