bands.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2009 Xiph.Org Foundation
  3. Copyright (c) 2008-2009 Gregory Maxwell
  4. Written by Jean-Marc Valin and Gregory Maxwell */
  5. /*
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. - Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. - Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in the
  13. documentation and/or other materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include <math.h>
  30. #include "bands.h"
  31. #include "modes.h"
  32. #include "vq.h"
  33. #include "cwrs.h"
  34. #include "stack_alloc.h"
  35. #include "os_support.h"
  36. #include "mathops.h"
  37. #include "rate.h"
  38. opus_uint32 celt_lcg_rand(opus_uint32 seed)
  39. {
  40. return 1664525 * seed + 1013904223;
  41. }
  42. /* This is a cos() approximation designed to be bit-exact on any platform. Bit exactness
  43. with this approximation is important because it has an impact on the bit allocation */
  44. static opus_int16 bitexact_cos(opus_int16 x)
  45. {
  46. opus_int32 tmp;
  47. opus_int16 x2;
  48. tmp = (4096+((opus_int32)(x)*(x)))>>13;
  49. celt_assert(tmp<=32767);
  50. x2 = tmp;
  51. x2 = (32767-x2) + FRAC_MUL16(x2, (-7651 + FRAC_MUL16(x2, (8277 + FRAC_MUL16(-626, x2)))));
  52. celt_assert(x2<=32766);
  53. return 1+x2;
  54. }
  55. static int bitexact_log2tan(int isin,int icos)
  56. {
  57. int lc;
  58. int ls;
  59. lc=EC_ILOG(icos);
  60. ls=EC_ILOG(isin);
  61. icos<<=15-lc;
  62. isin<<=15-ls;
  63. return ((ls-lc)<<11)
  64. +FRAC_MUL16(isin, FRAC_MUL16(isin, -2597) + 7932)
  65. -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932);
  66. }
  67. #ifdef FIXED_POINT
  68. /* Compute the amplitude (sqrt energy) in each of the bands */
  69. void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bank, int end, int _C, int M)
  70. {
  71. int i, c, N;
  72. const opus_int16 *eBands = m->eBands;
  73. const int C = CHANNELS(_C);
  74. N = M*m->shortMdctSize;
  75. c=0; do {
  76. for (i=0;i<end;i++)
  77. {
  78. int j;
  79. opus_val32 maxval=0;
  80. opus_val32 sum = 0;
  81. j=M*eBands[i]; do {
  82. maxval = MAX32(maxval, X[j+c*N]);
  83. maxval = MAX32(maxval, -X[j+c*N]);
  84. } while (++j<M*eBands[i+1]);
  85. if (maxval > 0)
  86. {
  87. int shift = celt_ilog2(maxval)-10;
  88. j=M*eBands[i]; do {
  89. sum = MAC16_16(sum, EXTRACT16(VSHR32(X[j+c*N],shift)),
  90. EXTRACT16(VSHR32(X[j+c*N],shift)));
  91. } while (++j<M*eBands[i+1]);
  92. /* We're adding one here to make damn sure we never end up with a pitch vector that's
  93. larger than unity norm */
  94. bank[i+c*m->nbEBands] = EPSILON+VSHR32(EXTEND32(celt_sqrt(sum)),-shift);
  95. } else {
  96. bank[i+c*m->nbEBands] = EPSILON;
  97. }
  98. /*printf ("%f ", bank[i+c*m->nbEBands]);*/
  99. }
  100. } while (++c<C);
  101. /*printf ("\n");*/
  102. }
  103. /* Normalise each band such that the energy is one. */
  104. void normalise_bands(const CELTMode *m, const celt_sig * restrict freq, celt_norm * restrict X, const celt_ener *bank, int end, int _C, int M)
  105. {
  106. int i, c, N;
  107. const opus_int16 *eBands = m->eBands;
  108. const int C = CHANNELS(_C);
  109. N = M*m->shortMdctSize;
  110. c=0; do {
  111. i=0; do {
  112. opus_val16 g;
  113. int j,shift;
  114. opus_val16 E;
  115. shift = celt_zlog2(bank[i+c*m->nbEBands])-13;
  116. E = VSHR32(bank[i+c*m->nbEBands], shift);
  117. g = EXTRACT16(celt_rcp(SHL32(E,3)));
  118. j=M*eBands[i]; do {
  119. X[j+c*N] = MULT16_16_Q15(VSHR32(freq[j+c*N],shift-1),g);
  120. } while (++j<M*eBands[i+1]);
  121. } while (++i<end);
  122. } while (++c<C);
  123. }
  124. #else /* FIXED_POINT */
  125. /* Compute the amplitude (sqrt energy) in each of the bands */
  126. void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bank, int end, int _C, int M)
  127. {
  128. int i, c, N;
  129. const opus_int16 *eBands = m->eBands;
  130. const int C = CHANNELS(_C);
  131. N = M*m->shortMdctSize;
  132. c=0; do {
  133. for (i=0;i<end;i++)
  134. {
  135. int j;
  136. opus_val32 sum = 1e-27f;
  137. for (j=M*eBands[i];j<M*eBands[i+1];j++)
  138. sum += X[j+c*N]*X[j+c*N];
  139. bank[i+c*m->nbEBands] = celt_sqrt(sum);
  140. /*printf ("%f ", bank[i+c*m->nbEBands]);*/
  141. }
  142. } while (++c<C);
  143. /*printf ("\n");*/
  144. }
  145. /* Normalise each band such that the energy is one. */
  146. void normalise_bands(const CELTMode *m, const celt_sig * restrict freq, celt_norm * restrict X, const celt_ener *bank, int end, int _C, int M)
  147. {
  148. int i, c, N;
  149. const opus_int16 *eBands = m->eBands;
  150. const int C = CHANNELS(_C);
  151. N = M*m->shortMdctSize;
  152. c=0; do {
  153. for (i=0;i<end;i++)
  154. {
  155. int j;
  156. opus_val16 g = 1.f/(1e-27f+bank[i+c*m->nbEBands]);
  157. for (j=M*eBands[i];j<M*eBands[i+1];j++)
  158. X[j+c*N] = freq[j+c*N]*g;
  159. }
  160. } while (++c<C);
  161. }
  162. #endif /* FIXED_POINT */
  163. /* De-normalise the energy to produce the synthesis from the unit-energy bands */
  164. void denormalise_bands(const CELTMode *m, const celt_norm * restrict X, celt_sig * restrict freq, const celt_ener *bank, int end, int _C, int M)
  165. {
  166. int i, c, N;
  167. const opus_int16 *eBands = m->eBands;
  168. const int C = CHANNELS(_C);
  169. N = M*m->shortMdctSize;
  170. celt_assert2(C<=2, "denormalise_bands() not implemented for >2 channels");
  171. c=0; do {
  172. celt_sig * restrict f;
  173. const celt_norm * restrict x;
  174. f = freq+c*N;
  175. x = X+c*N;
  176. for (i=0;i<end;i++)
  177. {
  178. int j, band_end;
  179. opus_val32 g = SHR32(bank[i+c*m->nbEBands],1);
  180. j=M*eBands[i];
  181. band_end = M*eBands[i+1];
  182. do {
  183. *f++ = SHL32(MULT16_32_Q15(*x, g),2);
  184. x++;
  185. } while (++j<band_end);
  186. }
  187. for (i=M*eBands[end];i<N;i++)
  188. *f++ = 0;
  189. } while (++c<C);
  190. }
  191. /* This prevents energy collapse for transients with multiple short MDCTs */
  192. void anti_collapse(const CELTMode *m, celt_norm *_X, unsigned char *collapse_masks, int LM, int C, int CC, int size,
  193. int start, int end, opus_val16 *logE, opus_val16 *prev1logE,
  194. opus_val16 *prev2logE, int *pulses, opus_uint32 seed)
  195. {
  196. int c, i, j, k;
  197. for (i=start;i<end;i++)
  198. {
  199. int N0;
  200. opus_val16 thresh, sqrt_1;
  201. int depth;
  202. #ifdef FIXED_POINT
  203. int shift;
  204. #endif
  205. N0 = m->eBands[i+1]-m->eBands[i];
  206. /* depth in 1/8 bits */
  207. depth = (1+pulses[i])/((m->eBands[i+1]-m->eBands[i])<<LM);
  208. #ifdef FIXED_POINT
  209. thresh = MULT16_32_Q15(QCONST16(0.5f, 15), MIN32(32767,SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1) ));
  210. {
  211. opus_val32 t;
  212. t = N0<<LM;
  213. shift = celt_ilog2(t)>>1;
  214. t = SHL32(t, (7-shift)<<1);
  215. sqrt_1 = celt_rsqrt_norm(t);
  216. }
  217. #else
  218. thresh = .5f*celt_exp2(-.125f*depth);
  219. sqrt_1 = celt_rsqrt(N0<<LM);
  220. #endif
  221. c=0; do
  222. {
  223. celt_norm *X;
  224. opus_val16 prev1;
  225. opus_val16 prev2;
  226. opus_val16 Ediff;
  227. opus_val16 r;
  228. int renormalize=0;
  229. prev1 = prev1logE[c*m->nbEBands+i];
  230. prev2 = prev2logE[c*m->nbEBands+i];
  231. if (C<CC)
  232. {
  233. prev1 = MAX16(prev1,prev1logE[m->nbEBands+i]);
  234. prev2 = MAX16(prev2,prev2logE[m->nbEBands+i]);
  235. }
  236. Ediff = logE[c*m->nbEBands+i]-MIN16(prev1,prev2);
  237. Ediff = MAX16(0, Ediff);
  238. #ifdef FIXED_POINT
  239. if (Ediff < 16384)
  240. r = 2*MIN16(16383,SHR32(celt_exp2(-Ediff),1));
  241. else
  242. r = 0;
  243. if (LM==3)
  244. r = MULT16_16_Q14(23170, MIN32(23169, r));
  245. r = SHR16(MIN16(thresh, r),1);
  246. r = SHR32(MULT16_16_Q15(sqrt_1, r),shift);
  247. #else
  248. /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because
  249. short blocks don't have the same energy as long */
  250. r = 2.f*celt_exp2(-Ediff);
  251. if (LM==3)
  252. r *= 1.41421356f;
  253. r = MIN16(thresh, r);
  254. r = r*sqrt_1;
  255. #endif
  256. X = _X+c*size+(m->eBands[i]<<LM);
  257. for (k=0;k<1<<LM;k++)
  258. {
  259. /* Detect collapse */
  260. if (!(collapse_masks[i*C+c]&1<<k))
  261. {
  262. /* Fill with noise */
  263. for (j=0;j<N0;j++)
  264. {
  265. seed = celt_lcg_rand(seed);
  266. X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
  267. }
  268. renormalize = 1;
  269. }
  270. }
  271. /* We just added some energy, so we need to renormalise */
  272. if (renormalize)
  273. renormalise_vector(X, N0<<LM, Q15ONE);
  274. } while (++c<C);
  275. }
  276. }
  277. static void intensity_stereo(const CELTMode *m, celt_norm *X, celt_norm *Y, const celt_ener *bank, int bandID, int N)
  278. {
  279. int i = bandID;
  280. int j;
  281. opus_val16 a1, a2;
  282. opus_val16 left, right;
  283. opus_val16 norm;
  284. #ifdef FIXED_POINT
  285. int shift = celt_zlog2(MAX32(bank[i], bank[i+m->nbEBands]))-13;
  286. #endif
  287. left = VSHR32(bank[i],shift);
  288. right = VSHR32(bank[i+m->nbEBands],shift);
  289. norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
  290. a1 = DIV32_16(SHL32(EXTEND32(left),14),norm);
  291. a2 = DIV32_16(SHL32(EXTEND32(right),14),norm);
  292. for (j=0;j<N;j++)
  293. {
  294. celt_norm r, l;
  295. l = X[j];
  296. r = Y[j];
  297. X[j] = MULT16_16_Q14(a1,l) + MULT16_16_Q14(a2,r);
  298. /* Side is not encoded, no need to calculate */
  299. }
  300. }
  301. static void stereo_split(celt_norm *X, celt_norm *Y, int N)
  302. {
  303. int j;
  304. for (j=0;j<N;j++)
  305. {
  306. celt_norm r, l;
  307. l = MULT16_16_Q15(QCONST16(.70710678f,15), X[j]);
  308. r = MULT16_16_Q15(QCONST16(.70710678f,15), Y[j]);
  309. X[j] = l+r;
  310. Y[j] = r-l;
  311. }
  312. }
  313. static void stereo_merge(celt_norm *X, celt_norm *Y, opus_val16 mid, int N)
  314. {
  315. int j;
  316. opus_val32 xp=0, side=0;
  317. opus_val32 El, Er;
  318. opus_val16 mid2;
  319. #ifdef FIXED_POINT
  320. int kl, kr;
  321. #endif
  322. opus_val32 t, lgain, rgain;
  323. /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
  324. for (j=0;j<N;j++)
  325. {
  326. xp = MAC16_16(xp, X[j], Y[j]);
  327. side = MAC16_16(side, Y[j], Y[j]);
  328. }
  329. /* Compensating for the mid normalization */
  330. xp = MULT16_32_Q15(mid, xp);
  331. /* mid and side are in Q15, not Q14 like X and Y */
  332. mid2 = SHR32(mid, 1);
  333. El = MULT16_16(mid2, mid2) + side - 2*xp;
  334. Er = MULT16_16(mid2, mid2) + side + 2*xp;
  335. if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
  336. {
  337. for (j=0;j<N;j++)
  338. Y[j] = X[j];
  339. return;
  340. }
  341. #ifdef FIXED_POINT
  342. kl = celt_ilog2(El)>>1;
  343. kr = celt_ilog2(Er)>>1;
  344. #endif
  345. t = VSHR32(El, (kl-7)<<1);
  346. lgain = celt_rsqrt_norm(t);
  347. t = VSHR32(Er, (kr-7)<<1);
  348. rgain = celt_rsqrt_norm(t);
  349. #ifdef FIXED_POINT
  350. if (kl < 7)
  351. kl = 7;
  352. if (kr < 7)
  353. kr = 7;
  354. #endif
  355. for (j=0;j<N;j++)
  356. {
  357. celt_norm r, l;
  358. /* Apply mid scaling (side is already scaled) */
  359. l = MULT16_16_Q15(mid, X[j]);
  360. r = Y[j];
  361. X[j] = EXTRACT16(PSHR32(MULT16_16(lgain, SUB16(l,r)), kl+1));
  362. Y[j] = EXTRACT16(PSHR32(MULT16_16(rgain, ADD16(l,r)), kr+1));
  363. }
  364. }
  365. /* Decide whether we should spread the pulses in the current frame */
  366. int spreading_decision(const CELTMode *m, celt_norm *X, int *average,
  367. int last_decision, int *hf_average, int *tapset_decision, int update_hf,
  368. int end, int _C, int M)
  369. {
  370. int i, c, N0;
  371. int sum = 0, nbBands=0;
  372. const int C = CHANNELS(_C);
  373. const opus_int16 * restrict eBands = m->eBands;
  374. int decision;
  375. int hf_sum=0;
  376. N0 = M*m->shortMdctSize;
  377. if (M*(eBands[end]-eBands[end-1]) <= 8)
  378. return SPREAD_NONE;
  379. c=0; do {
  380. for (i=0;i<end;i++)
  381. {
  382. int j, N, tmp=0;
  383. int tcount[3] = {0,0,0};
  384. celt_norm * restrict x = X+M*eBands[i]+c*N0;
  385. N = M*(eBands[i+1]-eBands[i]);
  386. if (N<=8)
  387. continue;
  388. /* Compute rough CDF of |x[j]| */
  389. for (j=0;j<N;j++)
  390. {
  391. opus_val32 x2N; /* Q13 */
  392. x2N = MULT16_16(MULT16_16_Q15(x[j], x[j]), N);
  393. if (x2N < QCONST16(0.25f,13))
  394. tcount[0]++;
  395. if (x2N < QCONST16(0.0625f,13))
  396. tcount[1]++;
  397. if (x2N < QCONST16(0.015625f,13))
  398. tcount[2]++;
  399. }
  400. /* Only include four last bands (8 kHz and up) */
  401. if (i>m->nbEBands-4)
  402. hf_sum += 32*(tcount[1]+tcount[0])/N;
  403. tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
  404. sum += tmp*256;
  405. nbBands++;
  406. }
  407. } while (++c<C);
  408. if (update_hf)
  409. {
  410. if (hf_sum)
  411. hf_sum /= C*(4-m->nbEBands+end);
  412. *hf_average = (*hf_average+hf_sum)>>1;
  413. hf_sum = *hf_average;
  414. if (*tapset_decision==2)
  415. hf_sum += 4;
  416. else if (*tapset_decision==0)
  417. hf_sum -= 4;
  418. if (hf_sum > 22)
  419. *tapset_decision=2;
  420. else if (hf_sum > 18)
  421. *tapset_decision=1;
  422. else
  423. *tapset_decision=0;
  424. }
  425. /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
  426. sum /= nbBands;
  427. /* Recursive averaging */
  428. sum = (sum+*average)>>1;
  429. *average = sum;
  430. /* Hysteresis */
  431. sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
  432. if (sum < 80)
  433. {
  434. decision = SPREAD_AGGRESSIVE;
  435. } else if (sum < 256)
  436. {
  437. decision = SPREAD_NORMAL;
  438. } else if (sum < 384)
  439. {
  440. decision = SPREAD_LIGHT;
  441. } else {
  442. decision = SPREAD_NONE;
  443. }
  444. #ifdef FUZZING
  445. decision = rand()&0x3;
  446. *tapset_decision=rand()%3;
  447. #endif
  448. return decision;
  449. }
  450. #ifdef MEASURE_NORM_MSE
  451. float MSE[30] = {0};
  452. int nbMSEBands = 0;
  453. int MSECount[30] = {0};
  454. void dump_norm_mse(void)
  455. {
  456. int i;
  457. for (i=0;i<nbMSEBands;i++)
  458. {
  459. printf ("%g ", MSE[i]/MSECount[i]);
  460. }
  461. printf ("\n");
  462. }
  463. void measure_norm_mse(const CELTMode *m, float *X, float *X0, float *bandE, float *bandE0, int M, int N, int C)
  464. {
  465. static int init = 0;
  466. int i;
  467. if (!init)
  468. {
  469. atexit(dump_norm_mse);
  470. init = 1;
  471. }
  472. for (i=0;i<m->nbEBands;i++)
  473. {
  474. int j;
  475. int c;
  476. float g;
  477. if (bandE0[i]<10 || (C==2 && bandE0[i+m->nbEBands]<1))
  478. continue;
  479. c=0; do {
  480. g = bandE[i+c*m->nbEBands]/(1e-15+bandE0[i+c*m->nbEBands]);
  481. for (j=M*m->eBands[i];j<M*m->eBands[i+1];j++)
  482. MSE[i] += (g*X[j+c*N]-X0[j+c*N])*(g*X[j+c*N]-X0[j+c*N]);
  483. } while (++c<C);
  484. MSECount[i]+=C;
  485. }
  486. nbMSEBands = m->nbEBands;
  487. }
  488. #endif
  489. /* Indexing table for converting from natural Hadamard to ordery Hadamard
  490. This is essentially a bit-reversed Gray, on top of which we've added
  491. an inversion of the order because we want the DC at the end rather than
  492. the beginning. The lines are for N=2, 4, 8, 16 */
  493. static const int ordery_table[] = {
  494. 1, 0,
  495. 3, 0, 2, 1,
  496. 7, 0, 4, 3, 6, 1, 5, 2,
  497. 15, 0, 8, 7, 12, 3, 11, 4, 14, 1, 9, 6, 13, 2, 10, 5,
  498. };
  499. static void deinterleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
  500. {
  501. int i,j;
  502. VARDECL(celt_norm, tmp);
  503. int N;
  504. SAVE_STACK;
  505. N = N0*stride;
  506. ALLOC(tmp, N, celt_norm);
  507. if (hadamard)
  508. {
  509. const int *ordery = ordery_table+stride-2;
  510. for (i=0;i<stride;i++)
  511. {
  512. for (j=0;j<N0;j++)
  513. tmp[ordery[i]*N0+j] = X[j*stride+i];
  514. }
  515. } else {
  516. for (i=0;i<stride;i++)
  517. for (j=0;j<N0;j++)
  518. tmp[i*N0+j] = X[j*stride+i];
  519. }
  520. for (j=0;j<N;j++)
  521. X[j] = tmp[j];
  522. RESTORE_STACK;
  523. }
  524. static void interleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
  525. {
  526. int i,j;
  527. VARDECL(celt_norm, tmp);
  528. int N;
  529. SAVE_STACK;
  530. N = N0*stride;
  531. ALLOC(tmp, N, celt_norm);
  532. if (hadamard)
  533. {
  534. const int *ordery = ordery_table+stride-2;
  535. for (i=0;i<stride;i++)
  536. for (j=0;j<N0;j++)
  537. tmp[j*stride+i] = X[ordery[i]*N0+j];
  538. } else {
  539. for (i=0;i<stride;i++)
  540. for (j=0;j<N0;j++)
  541. tmp[j*stride+i] = X[i*N0+j];
  542. }
  543. for (j=0;j<N;j++)
  544. X[j] = tmp[j];
  545. RESTORE_STACK;
  546. }
  547. void haar1(celt_norm *X, int N0, int stride)
  548. {
  549. int i, j;
  550. N0 >>= 1;
  551. for (i=0;i<stride;i++)
  552. for (j=0;j<N0;j++)
  553. {
  554. celt_norm tmp1, tmp2;
  555. tmp1 = MULT16_16_Q15(QCONST16(.70710678f,15), X[stride*2*j+i]);
  556. tmp2 = MULT16_16_Q15(QCONST16(.70710678f,15), X[stride*(2*j+1)+i]);
  557. X[stride*2*j+i] = tmp1 + tmp2;
  558. X[stride*(2*j+1)+i] = tmp1 - tmp2;
  559. }
  560. }
  561. static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo)
  562. {
  563. static const opus_int16 exp2_table8[8] =
  564. {16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048};
  565. int qn, qb;
  566. int N2 = 2*N-1;
  567. if (stereo && N==2)
  568. N2--;
  569. /* The upper limit ensures that in a stereo split with itheta==16384, we'll
  570. always have enough bits left over to code at least one pulse in the
  571. side; otherwise it would collapse, since it doesn't get folded. */
  572. qb = IMIN(b-pulse_cap-(4<<BITRES), (b+N2*offset)/N2);
  573. qb = IMIN(8<<BITRES, qb);
  574. if (qb<(1<<BITRES>>1)) {
  575. qn = 1;
  576. } else {
  577. qn = exp2_table8[qb&0x7]>>(14-(qb>>BITRES));
  578. qn = (qn+1)>>1<<1;
  579. }
  580. celt_assert(qn <= 256);
  581. return qn;
  582. }
  583. /* This function is responsible for encoding and decoding a band for both
  584. the mono and stereo case. Even in the mono case, it can split the band
  585. in two and transmit the energy difference with the two half-bands. It
  586. can be called recursively so bands can end up being split in 8 parts. */
  587. static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, celt_norm *Y,
  588. int N, int b, int spread, int B, int intensity, int tf_change, celt_norm *lowband, ec_ctx *ec,
  589. opus_int32 *remaining_bits, int LM, celt_norm *lowband_out, const celt_ener *bandE, int level,
  590. opus_uint32 *seed, opus_val16 gain, celt_norm *lowband_scratch, int fill)
  591. {
  592. const unsigned char *cache;
  593. int q;
  594. int curr_bits;
  595. int stereo, split;
  596. int imid=0, iside=0;
  597. int N0=N;
  598. int N_B=N;
  599. int N_B0;
  600. int B0=B;
  601. int time_divide=0;
  602. int recombine=0;
  603. int inv = 0;
  604. opus_val16 mid=0, side=0;
  605. int longBlocks;
  606. unsigned cm=0;
  607. #ifdef RESYNTH
  608. int resynth = 1;
  609. #else
  610. int resynth = !encode;
  611. #endif
  612. longBlocks = B0==1;
  613. N_B /= B;
  614. N_B0 = N_B;
  615. split = stereo = Y != NULL;
  616. /* Special case for one sample */
  617. if (N==1)
  618. {
  619. int c;
  620. celt_norm *x = X;
  621. c=0; do {
  622. int sign=0;
  623. if (*remaining_bits>=1<<BITRES)
  624. {
  625. if (encode)
  626. {
  627. sign = x[0]<0;
  628. ec_enc_bits(ec, sign, 1);
  629. } else {
  630. sign = ec_dec_bits(ec, 1);
  631. }
  632. *remaining_bits -= 1<<BITRES;
  633. b-=1<<BITRES;
  634. }
  635. if (resynth)
  636. x[0] = sign ? -NORM_SCALING : NORM_SCALING;
  637. x = Y;
  638. } while (++c<1+stereo);
  639. if (lowband_out)
  640. lowband_out[0] = SHR16(X[0],4);
  641. return 1;
  642. }
  643. if (!stereo && level == 0)
  644. {
  645. int k;
  646. if (tf_change>0)
  647. recombine = tf_change;
  648. /* Band recombining to increase frequency resolution */
  649. if (lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
  650. {
  651. int j;
  652. for (j=0;j<N;j++)
  653. lowband_scratch[j] = lowband[j];
  654. lowband = lowband_scratch;
  655. }
  656. for (k=0;k<recombine;k++)
  657. {
  658. static const unsigned char bit_interleave_table[16]={
  659. 0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
  660. };
  661. if (encode)
  662. haar1(X, N>>k, 1<<k);
  663. if (lowband)
  664. haar1(lowband, N>>k, 1<<k);
  665. fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
  666. }
  667. B>>=recombine;
  668. N_B<<=recombine;
  669. /* Increasing the time resolution */
  670. while ((N_B&1) == 0 && tf_change<0)
  671. {
  672. if (encode)
  673. haar1(X, N_B, B);
  674. if (lowband)
  675. haar1(lowband, N_B, B);
  676. fill |= fill<<B;
  677. B <<= 1;
  678. N_B >>= 1;
  679. time_divide++;
  680. tf_change++;
  681. }
  682. B0=B;
  683. N_B0 = N_B;
  684. /* Reorganize the samples in time order instead of frequency order */
  685. if (B0>1)
  686. {
  687. if (encode)
  688. deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
  689. if (lowband)
  690. deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
  691. }
  692. }
  693. /* If we need 1.5 more bit than we can produce, split the band in two. */
  694. cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
  695. if (!stereo && LM != -1 && b > cache[cache[0]]+12 && N>2)
  696. {
  697. if (LM>0 || (N&1)==0)
  698. {
  699. N >>= 1;
  700. Y = X+N;
  701. split = 1;
  702. LM -= 1;
  703. if (B==1)
  704. fill = (fill&1)|(fill<<1);
  705. B = (B+1)>>1;
  706. }
  707. }
  708. if (split)
  709. {
  710. int qn;
  711. int itheta=0;
  712. int mbits, sbits, delta;
  713. int qalloc;
  714. int pulse_cap;
  715. int offset;
  716. int orig_fill;
  717. opus_int32 tell;
  718. /* Decide on the resolution to give to the split parameter theta */
  719. pulse_cap = m->logN[i]+(LM<<BITRES);
  720. offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
  721. qn = compute_qn(N, b, offset, pulse_cap, stereo);
  722. if (stereo && i>=intensity)
  723. qn = 1;
  724. if (encode)
  725. {
  726. /* theta is the atan() of the ratio between the (normalized)
  727. side and mid. With just that parameter, we can re-scale both
  728. mid and side because we know that 1) they have unit norm and
  729. 2) they are orthogonal. */
  730. itheta = stereo_itheta(X, Y, stereo, N);
  731. }
  732. tell = ec_tell_frac(ec);
  733. if (qn!=1)
  734. {
  735. if (encode)
  736. itheta = (itheta*qn+8192)>>14;
  737. /* Entropy coding of the angle. We use a uniform pdf for the
  738. time split, a step for stereo, and a triangular one for the rest. */
  739. if (stereo && N>2)
  740. {
  741. int p0 = 3;
  742. int x = itheta;
  743. int x0 = qn/2;
  744. int ft = p0*(x0+1) + x0;
  745. /* Use a probability of p0 up to itheta=8192 and then use 1 after */
  746. if (encode)
  747. {
  748. ec_encode(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft);
  749. } else {
  750. int fs;
  751. fs=ec_decode(ec,ft);
  752. if (fs<(x0+1)*p0)
  753. x=fs/p0;
  754. else
  755. x=x0+1+(fs-(x0+1)*p0);
  756. 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);
  757. itheta = x;
  758. }
  759. } else if (B0>1 || stereo) {
  760. /* Uniform pdf */
  761. if (encode)
  762. ec_enc_uint(ec, itheta, qn+1);
  763. else
  764. itheta = ec_dec_uint(ec, qn+1);
  765. } else {
  766. int fs=1, ft;
  767. ft = ((qn>>1)+1)*((qn>>1)+1);
  768. if (encode)
  769. {
  770. int fl;
  771. fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
  772. fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
  773. ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
  774. ec_encode(ec, fl, fl+fs, ft);
  775. } else {
  776. /* Triangular pdf */
  777. int fl=0;
  778. int fm;
  779. fm = ec_decode(ec, ft);
  780. if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
  781. {
  782. itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
  783. fs = itheta + 1;
  784. fl = itheta*(itheta + 1)>>1;
  785. }
  786. else
  787. {
  788. itheta = (2*(qn + 1)
  789. - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
  790. fs = qn + 1 - itheta;
  791. fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
  792. }
  793. ec_dec_update(ec, fl, fl+fs, ft);
  794. }
  795. }
  796. itheta = (opus_int32)itheta*16384/qn;
  797. if (encode && stereo)
  798. {
  799. if (itheta==0)
  800. intensity_stereo(m, X, Y, bandE, i, N);
  801. else
  802. stereo_split(X, Y, N);
  803. }
  804. /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate.
  805. Let's do that at higher complexity */
  806. } else if (stereo) {
  807. if (encode)
  808. {
  809. inv = itheta > 8192;
  810. if (inv)
  811. {
  812. int j;
  813. for (j=0;j<N;j++)
  814. Y[j] = -Y[j];
  815. }
  816. intensity_stereo(m, X, Y, bandE, i, N);
  817. }
  818. if (b>2<<BITRES && *remaining_bits > 2<<BITRES)
  819. {
  820. if (encode)
  821. ec_enc_bit_logp(ec, inv, 2);
  822. else
  823. inv = ec_dec_bit_logp(ec, 2);
  824. } else
  825. inv = 0;
  826. itheta = 0;
  827. }
  828. qalloc = ec_tell_frac(ec) - tell;
  829. b -= qalloc;
  830. orig_fill = fill;
  831. if (itheta == 0)
  832. {
  833. imid = 32767;
  834. iside = 0;
  835. fill &= (1<<B)-1;
  836. delta = -16384;
  837. } else if (itheta == 16384)
  838. {
  839. imid = 0;
  840. iside = 32767;
  841. fill &= ((1<<B)-1)<<B;
  842. delta = 16384;
  843. } else {
  844. imid = bitexact_cos(itheta);
  845. iside = bitexact_cos(16384-itheta);
  846. /* This is the mid vs side allocation that minimizes squared error
  847. in that band. */
  848. delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
  849. }
  850. #ifdef FIXED_POINT
  851. mid = imid;
  852. side = iside;
  853. #else
  854. mid = (1.f/32768)*imid;
  855. side = (1.f/32768)*iside;
  856. #endif
  857. /* This is a special case for N=2 that only works for stereo and takes
  858. advantage of the fact that mid and side are orthogonal to encode
  859. the side with just one bit. */
  860. if (N==2 && stereo)
  861. {
  862. int c;
  863. int sign=0;
  864. celt_norm *x2, *y2;
  865. mbits = b;
  866. sbits = 0;
  867. /* Only need one bit for the side */
  868. if (itheta != 0 && itheta != 16384)
  869. sbits = 1<<BITRES;
  870. mbits -= sbits;
  871. c = itheta > 8192;
  872. *remaining_bits -= qalloc+sbits;
  873. x2 = c ? Y : X;
  874. y2 = c ? X : Y;
  875. if (sbits)
  876. {
  877. if (encode)
  878. {
  879. /* Here we only need to encode a sign for the side */
  880. sign = x2[0]*y2[1] - x2[1]*y2[0] < 0;
  881. ec_enc_bits(ec, sign, 1);
  882. } else {
  883. sign = ec_dec_bits(ec, 1);
  884. }
  885. }
  886. sign = 1-2*sign;
  887. /* We use orig_fill here because we want to fold the side, but if
  888. itheta==16384, we'll have cleared the low bits of fill. */
  889. cm = quant_band(encode, m, i, x2, NULL, N, mbits, spread, B, intensity, tf_change, lowband, ec, remaining_bits, LM, lowband_out, NULL, level, seed, gain, lowband_scratch, orig_fill);
  890. /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse),
  891. and there's no need to worry about mixing with the other channel. */
  892. y2[0] = -sign*x2[1];
  893. y2[1] = sign*x2[0];
  894. if (resynth)
  895. {
  896. celt_norm tmp;
  897. X[0] = MULT16_16_Q15(mid, X[0]);
  898. X[1] = MULT16_16_Q15(mid, X[1]);
  899. Y[0] = MULT16_16_Q15(side, Y[0]);
  900. Y[1] = MULT16_16_Q15(side, Y[1]);
  901. tmp = X[0];
  902. X[0] = SUB16(tmp,Y[0]);
  903. Y[0] = ADD16(tmp,Y[0]);
  904. tmp = X[1];
  905. X[1] = SUB16(tmp,Y[1]);
  906. Y[1] = ADD16(tmp,Y[1]);
  907. }
  908. } else {
  909. /* "Normal" split code */
  910. celt_norm *next_lowband2=NULL;
  911. celt_norm *next_lowband_out1=NULL;
  912. int next_level=0;
  913. opus_int32 rebalance;
  914. /* Give more bits to low-energy MDCTs than they would otherwise deserve */
  915. if (B0>1 && !stereo && (itheta&0x3fff))
  916. {
  917. if (itheta > 8192)
  918. /* Rough approximation for pre-echo masking */
  919. delta -= delta>>(4-LM);
  920. else
  921. /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
  922. delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
  923. }
  924. mbits = IMAX(0, IMIN(b, (b-delta)/2));
  925. sbits = b-mbits;
  926. *remaining_bits -= qalloc;
  927. if (lowband && !stereo)
  928. next_lowband2 = lowband+N; /* >32-bit split case */
  929. /* Only stereo needs to pass on lowband_out. Otherwise, it's
  930. handled at the end */
  931. if (stereo)
  932. next_lowband_out1 = lowband_out;
  933. else
  934. next_level = level+1;
  935. rebalance = *remaining_bits;
  936. if (mbits >= sbits)
  937. {
  938. /* In stereo mode, we do not apply a scaling to the mid because we need the normalized
  939. mid for folding later */
  940. cm = quant_band(encode, m, i, X, NULL, N, mbits, spread, B, intensity, tf_change,
  941. lowband, ec, remaining_bits, LM, next_lowband_out1,
  942. NULL, next_level, seed, stereo ? Q15ONE : MULT16_16_P15(gain,mid), lowband_scratch, fill);
  943. rebalance = mbits - (rebalance-*remaining_bits);
  944. if (rebalance > 3<<BITRES && itheta!=0)
  945. sbits += rebalance - (3<<BITRES);
  946. /* For a stereo split, the high bits of fill are always zero, so no
  947. folding will be done to the side. */
  948. cm |= quant_band(encode, m, i, Y, NULL, N, sbits, spread, B, intensity, tf_change,
  949. next_lowband2, ec, remaining_bits, LM, NULL,
  950. NULL, next_level, seed, MULT16_16_P15(gain,side), NULL, fill>>B)<<((B0>>1)&(stereo-1));
  951. } else {
  952. /* For a stereo split, the high bits of fill are always zero, so no
  953. folding will be done to the side. */
  954. cm = quant_band(encode, m, i, Y, NULL, N, sbits, spread, B, intensity, tf_change,
  955. next_lowband2, ec, remaining_bits, LM, NULL,
  956. NULL, next_level, seed, MULT16_16_P15(gain,side), NULL, fill>>B)<<((B0>>1)&(stereo-1));
  957. rebalance = sbits - (rebalance-*remaining_bits);
  958. if (rebalance > 3<<BITRES && itheta!=16384)
  959. mbits += rebalance - (3<<BITRES);
  960. /* In stereo mode, we do not apply a scaling to the mid because we need the normalized
  961. mid for folding later */
  962. cm |= quant_band(encode, m, i, X, NULL, N, mbits, spread, B, intensity, tf_change,
  963. lowband, ec, remaining_bits, LM, next_lowband_out1,
  964. NULL, next_level, seed, stereo ? Q15ONE : MULT16_16_P15(gain,mid), lowband_scratch, fill);
  965. }
  966. }
  967. } else {
  968. /* This is the basic no-split case */
  969. q = bits2pulses(m, i, LM, b);
  970. curr_bits = pulses2bits(m, i, LM, q);
  971. *remaining_bits -= curr_bits;
  972. /* Ensures we can never bust the budget */
  973. while (*remaining_bits < 0 && q > 0)
  974. {
  975. *remaining_bits += curr_bits;
  976. q--;
  977. curr_bits = pulses2bits(m, i, LM, q);
  978. *remaining_bits -= curr_bits;
  979. }
  980. if (q!=0)
  981. {
  982. int K = get_pulses(q);
  983. /* Finally do the actual quantization */
  984. if (encode)
  985. {
  986. cm = alg_quant(X, N, K, spread, B, ec
  987. #ifdef RESYNTH
  988. , gain
  989. #endif
  990. );
  991. } else {
  992. cm = alg_unquant(X, N, K, spread, B, ec, gain);
  993. }
  994. } else {
  995. /* If there's no pulse, fill the band anyway */
  996. int j;
  997. if (resynth)
  998. {
  999. unsigned cm_mask;
  1000. /*B can be as large as 16, so this shift might overflow an int on a
  1001. 16-bit platform; use a long to get defined behavior.*/
  1002. cm_mask = (unsigned)(1UL<<B)-1;
  1003. fill &= cm_mask;
  1004. if (!fill)
  1005. {
  1006. for (j=0;j<N;j++)
  1007. X[j] = 0;
  1008. } else {
  1009. if (lowband == NULL)
  1010. {
  1011. /* Noise */
  1012. for (j=0;j<N;j++)
  1013. {
  1014. *seed = celt_lcg_rand(*seed);
  1015. X[j] = (celt_norm)((opus_int32)*seed>>20);
  1016. }
  1017. cm = cm_mask;
  1018. } else {
  1019. /* Folded spectrum */
  1020. for (j=0;j<N;j++)
  1021. {
  1022. opus_val16 tmp;
  1023. *seed = celt_lcg_rand(*seed);
  1024. /* About 48 dB below the "normal" folding level */
  1025. tmp = QCONST16(1.0f/256, 10);
  1026. tmp = (*seed)&0x8000 ? tmp : -tmp;
  1027. X[j] = lowband[j]+tmp;
  1028. }
  1029. cm = fill;
  1030. }
  1031. renormalise_vector(X, N, gain);
  1032. }
  1033. }
  1034. }
  1035. }
  1036. /* This code is used by the decoder and by the resynthesis-enabled encoder */
  1037. if (resynth)
  1038. {
  1039. if (stereo)
  1040. {
  1041. if (N!=2)
  1042. stereo_merge(X, Y, mid, N);
  1043. if (inv)
  1044. {
  1045. int j;
  1046. for (j=0;j<N;j++)
  1047. Y[j] = -Y[j];
  1048. }
  1049. } else if (level == 0)
  1050. {
  1051. int k;
  1052. /* Undo the sample reorganization going from time order to frequency order */
  1053. if (B0>1)
  1054. interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
  1055. /* Undo time-freq changes that we did earlier */
  1056. N_B = N_B0;
  1057. B = B0;
  1058. for (k=0;k<time_divide;k++)
  1059. {
  1060. B >>= 1;
  1061. N_B <<= 1;
  1062. cm |= cm>>B;
  1063. haar1(X, N_B, B);
  1064. }
  1065. for (k=0;k<recombine;k++)
  1066. {
  1067. static const unsigned char bit_deinterleave_table[16]={
  1068. 0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
  1069. 0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
  1070. };
  1071. cm = bit_deinterleave_table[cm];
  1072. haar1(X, N0>>k, 1<<k);
  1073. }
  1074. B<<=recombine;
  1075. /* Scale output for later folding */
  1076. if (lowband_out)
  1077. {
  1078. int j;
  1079. opus_val16 n;
  1080. n = celt_sqrt(SHL32(EXTEND32(N0),22));
  1081. for (j=0;j<N0;j++)
  1082. lowband_out[j] = MULT16_16_Q15(n,X[j]);
  1083. }
  1084. cm &= (1<<B)-1;
  1085. }
  1086. }
  1087. return cm;
  1088. }
  1089. void quant_all_bands(int encode, const CELTMode *m, int start, int end,
  1090. celt_norm *_X, celt_norm *_Y, unsigned char *collapse_masks, const celt_ener *bandE, int *pulses,
  1091. int shortBlocks, int spread, int dual_stereo, int intensity, int *tf_res,
  1092. opus_int32 total_bits, opus_int32 balance, ec_ctx *ec, int LM, int codedBands, opus_uint32 *seed)
  1093. {
  1094. int i;
  1095. opus_int32 remaining_bits;
  1096. const opus_int16 * restrict eBands = m->eBands;
  1097. celt_norm * restrict norm, * restrict norm2;
  1098. VARDECL(celt_norm, _norm);
  1099. VARDECL(celt_norm, lowband_scratch);
  1100. int B;
  1101. int M;
  1102. int lowband_offset;
  1103. int update_lowband = 1;
  1104. int C = _Y != NULL ? 2 : 1;
  1105. #ifdef RESYNTH
  1106. int resynth = 1;
  1107. #else
  1108. int resynth = !encode;
  1109. #endif
  1110. SAVE_STACK;
  1111. M = 1<<LM;
  1112. B = shortBlocks ? M : 1;
  1113. ALLOC(_norm, C*M*eBands[m->nbEBands], celt_norm);
  1114. ALLOC(lowband_scratch, M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]), celt_norm);
  1115. norm = _norm;
  1116. norm2 = norm + M*eBands[m->nbEBands];
  1117. lowband_offset = 0;
  1118. for (i=start;i<end;i++)
  1119. {
  1120. opus_int32 tell;
  1121. int b;
  1122. int N;
  1123. opus_int32 curr_balance;
  1124. int effective_lowband=-1;
  1125. celt_norm * restrict X, * restrict Y;
  1126. int tf_change=0;
  1127. unsigned x_cm;
  1128. unsigned y_cm;
  1129. X = _X+M*eBands[i];
  1130. if (_Y!=NULL)
  1131. Y = _Y+M*eBands[i];
  1132. else
  1133. Y = NULL;
  1134. N = M*eBands[i+1]-M*eBands[i];
  1135. tell = ec_tell_frac(ec);
  1136. /* Compute how many bits we want to allocate to this band */
  1137. if (i != start)
  1138. balance -= tell;
  1139. remaining_bits = total_bits-tell-1;
  1140. if (i <= codedBands-1)
  1141. {
  1142. curr_balance = balance / IMIN(3, codedBands-i);
  1143. b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
  1144. } else {
  1145. b = 0;
  1146. }
  1147. if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0))
  1148. lowband_offset = i;
  1149. tf_change = tf_res[i];
  1150. if (i>=m->effEBands)
  1151. {
  1152. X=norm;
  1153. if (_Y!=NULL)
  1154. Y = norm;
  1155. }
  1156. /* Get a conservative estimate of the collapse_mask's for the bands we're
  1157. going to be folding from. */
  1158. if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
  1159. {
  1160. int fold_start;
  1161. int fold_end;
  1162. int fold_i;
  1163. /* This ensures we never repeat spectral content within one band */
  1164. effective_lowband = IMAX(M*eBands[start], M*eBands[lowband_offset]-N);
  1165. fold_start = lowband_offset;
  1166. while(M*eBands[--fold_start] > effective_lowband);
  1167. fold_end = lowband_offset-1;
  1168. while(M*eBands[++fold_end] < effective_lowband+N);
  1169. x_cm = y_cm = 0;
  1170. fold_i = fold_start; do {
  1171. x_cm |= collapse_masks[fold_i*C+0];
  1172. y_cm |= collapse_masks[fold_i*C+C-1];
  1173. } while (++fold_i<fold_end);
  1174. }
  1175. /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
  1176. always) be non-zero.*/
  1177. else
  1178. x_cm = y_cm = (1<<B)-1;
  1179. if (dual_stereo && i==intensity)
  1180. {
  1181. int j;
  1182. /* Switch off dual stereo to do intensity */
  1183. dual_stereo = 0;
  1184. for (j=M*eBands[start];j<M*eBands[i];j++)
  1185. norm[j] = HALF32(norm[j]+norm2[j]);
  1186. }
  1187. if (dual_stereo)
  1188. {
  1189. x_cm = quant_band(encode, m, i, X, NULL, N, b/2, spread, B, intensity, tf_change,
  1190. effective_lowband != -1 ? norm+effective_lowband : NULL, ec, &remaining_bits, LM,
  1191. norm+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, x_cm);
  1192. y_cm = quant_band(encode, m, i, Y, NULL, N, b/2, spread, B, intensity, tf_change,
  1193. effective_lowband != -1 ? norm2+effective_lowband : NULL, ec, &remaining_bits, LM,
  1194. norm2+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, y_cm);
  1195. } else {
  1196. x_cm = quant_band(encode, m, i, X, Y, N, b, spread, B, intensity, tf_change,
  1197. effective_lowband != -1 ? norm+effective_lowband : NULL, ec, &remaining_bits, LM,
  1198. norm+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, x_cm|y_cm);
  1199. y_cm = x_cm;
  1200. }
  1201. collapse_masks[i*C+0] = (unsigned char)x_cm;
  1202. collapse_masks[i*C+C-1] = (unsigned char)y_cm;
  1203. balance += pulses[i] + tell;
  1204. /* Update the folding position only as long as we have 1 bit/sample depth */
  1205. update_lowband = b>(N<<BITRES);
  1206. }
  1207. RESTORE_STACK;
  1208. }