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