bands.c 40 KB

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