kiss_fft.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*Copyright (c) 2003-2012 IETF Trust, Mark Borgerding, Jean-Marc Valin
  2. Xiph.Org Foundation, CSIRO. All rights reserved.
  3. This file is extracted from RFC6716. Please see that RFC for additional
  4. information.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  13. names of specific contributors, may be used to endorse or promote
  14. products derived from this software without specific prior written
  15. permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE.*/
  27. /* This code is originally from Mark Borgerding's KISS-FFT but has been
  28. heavily modified to better suit Opus */
  29. #ifndef SKIP_CONFIG_H
  30. # ifdef HAVE_CONFIG_H
  31. # include "config.h"
  32. # endif
  33. #endif
  34. #include "_kiss_fft_guts.h"
  35. #include "arch.h"
  36. #include "os_support.h"
  37. #include "mathops.h"
  38. #include "stack_alloc.h"
  39. #include "os_support.h"
  40. /* The guts header contains all the multiplication and addition macros that are defined for
  41. complex numbers. It also delares the kf_ internal functions.
  42. */
  43. static void kf_bfly2(
  44. kiss_fft_cpx * Fout,
  45. const size_t fstride,
  46. const kiss_fft_state *st,
  47. int m,
  48. int N,
  49. int mm
  50. )
  51. {
  52. kiss_fft_cpx * Fout2;
  53. const kiss_twiddle_cpx * tw1;
  54. int i,j;
  55. kiss_fft_cpx * Fout_beg = Fout;
  56. for (i=0;i<N;i++)
  57. {
  58. Fout = Fout_beg + i*mm;
  59. Fout2 = Fout + m;
  60. tw1 = st->twiddles;
  61. for(j=0;j<m;j++)
  62. {
  63. kiss_fft_cpx t;
  64. Fout->r = SHR(Fout->r, 1);Fout->i = SHR(Fout->i, 1);
  65. Fout2->r = SHR(Fout2->r, 1);Fout2->i = SHR(Fout2->i, 1);
  66. C_MUL (t, *Fout2 , *tw1);
  67. tw1 += fstride;
  68. C_SUB( *Fout2 , *Fout , t );
  69. C_ADDTO( *Fout , t );
  70. ++Fout2;
  71. ++Fout;
  72. }
  73. }
  74. }
  75. static void ki_bfly2(
  76. kiss_fft_cpx * Fout,
  77. const size_t fstride,
  78. const kiss_fft_state *st,
  79. int m,
  80. int N,
  81. int mm
  82. )
  83. {
  84. kiss_fft_cpx * Fout2;
  85. const kiss_twiddle_cpx * tw1;
  86. kiss_fft_cpx t;
  87. int i,j;
  88. kiss_fft_cpx * Fout_beg = Fout;
  89. for (i=0;i<N;i++)
  90. {
  91. Fout = Fout_beg + i*mm;
  92. Fout2 = Fout + m;
  93. tw1 = st->twiddles;
  94. for(j=0;j<m;j++)
  95. {
  96. C_MULC (t, *Fout2 , *tw1);
  97. tw1 += fstride;
  98. C_SUB( *Fout2 , *Fout , t );
  99. C_ADDTO( *Fout , t );
  100. ++Fout2;
  101. ++Fout;
  102. }
  103. }
  104. }
  105. static void kf_bfly4(
  106. kiss_fft_cpx * Fout,
  107. const size_t fstride,
  108. const kiss_fft_state *st,
  109. int m,
  110. int N,
  111. int mm
  112. )
  113. {
  114. const kiss_twiddle_cpx *tw1,*tw2,*tw3;
  115. kiss_fft_cpx scratch[6];
  116. const size_t m2=2*m;
  117. const size_t m3=3*m;
  118. int i, j;
  119. kiss_fft_cpx * Fout_beg = Fout;
  120. for (i=0;i<N;i++)
  121. {
  122. Fout = Fout_beg + i*mm;
  123. tw3 = tw2 = tw1 = st->twiddles;
  124. for (j=0;j<m;j++)
  125. {
  126. C_MUL4(scratch[0],Fout[m] , *tw1 );
  127. C_MUL4(scratch[1],Fout[m2] , *tw2 );
  128. C_MUL4(scratch[2],Fout[m3] , *tw3 );
  129. Fout->r = PSHR(Fout->r, 2);
  130. Fout->i = PSHR(Fout->i, 2);
  131. C_SUB( scratch[5] , *Fout, scratch[1] );
  132. C_ADDTO(*Fout, scratch[1]);
  133. C_ADD( scratch[3] , scratch[0] , scratch[2] );
  134. C_SUB( scratch[4] , scratch[0] , scratch[2] );
  135. Fout[m2].r = PSHR(Fout[m2].r, 2);
  136. Fout[m2].i = PSHR(Fout[m2].i, 2);
  137. C_SUB( Fout[m2], *Fout, scratch[3] );
  138. tw1 += fstride;
  139. tw2 += fstride*2;
  140. tw3 += fstride*3;
  141. C_ADDTO( *Fout , scratch[3] );
  142. Fout[m].r = scratch[5].r + scratch[4].i;
  143. Fout[m].i = scratch[5].i - scratch[4].r;
  144. Fout[m3].r = scratch[5].r - scratch[4].i;
  145. Fout[m3].i = scratch[5].i + scratch[4].r;
  146. ++Fout;
  147. }
  148. }
  149. }
  150. static void ki_bfly4(
  151. kiss_fft_cpx * Fout,
  152. const size_t fstride,
  153. const kiss_fft_state *st,
  154. int m,
  155. int N,
  156. int mm
  157. )
  158. {
  159. const kiss_twiddle_cpx *tw1,*tw2,*tw3;
  160. kiss_fft_cpx scratch[6];
  161. const size_t m2=2*m;
  162. const size_t m3=3*m;
  163. int i, j;
  164. kiss_fft_cpx * Fout_beg = Fout;
  165. for (i=0;i<N;i++)
  166. {
  167. Fout = Fout_beg + i*mm;
  168. tw3 = tw2 = tw1 = st->twiddles;
  169. for (j=0;j<m;j++)
  170. {
  171. C_MULC(scratch[0],Fout[m] , *tw1 );
  172. C_MULC(scratch[1],Fout[m2] , *tw2 );
  173. C_MULC(scratch[2],Fout[m3] , *tw3 );
  174. C_SUB( scratch[5] , *Fout, scratch[1] );
  175. C_ADDTO(*Fout, scratch[1]);
  176. C_ADD( scratch[3] , scratch[0] , scratch[2] );
  177. C_SUB( scratch[4] , scratch[0] , scratch[2] );
  178. C_SUB( Fout[m2], *Fout, scratch[3] );
  179. tw1 += fstride;
  180. tw2 += fstride*2;
  181. tw3 += fstride*3;
  182. C_ADDTO( *Fout , scratch[3] );
  183. Fout[m].r = scratch[5].r - scratch[4].i;
  184. Fout[m].i = scratch[5].i + scratch[4].r;
  185. Fout[m3].r = scratch[5].r + scratch[4].i;
  186. Fout[m3].i = scratch[5].i - scratch[4].r;
  187. ++Fout;
  188. }
  189. }
  190. }
  191. #ifndef RADIX_TWO_ONLY
  192. static void kf_bfly3(
  193. kiss_fft_cpx * Fout,
  194. const size_t fstride,
  195. const kiss_fft_state *st,
  196. int m,
  197. int N,
  198. int mm
  199. )
  200. {
  201. int i;
  202. size_t k;
  203. const size_t m2 = 2*m;
  204. const kiss_twiddle_cpx *tw1,*tw2;
  205. kiss_fft_cpx scratch[5];
  206. kiss_twiddle_cpx epi3;
  207. kiss_fft_cpx * Fout_beg = Fout;
  208. epi3 = st->twiddles[fstride*m];
  209. for (i=0;i<N;i++)
  210. {
  211. Fout = Fout_beg + i*mm;
  212. tw1=tw2=st->twiddles;
  213. k=m;
  214. do {
  215. C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3);
  216. C_MUL(scratch[1],Fout[m] , *tw1);
  217. C_MUL(scratch[2],Fout[m2] , *tw2);
  218. C_ADD(scratch[3],scratch[1],scratch[2]);
  219. C_SUB(scratch[0],scratch[1],scratch[2]);
  220. tw1 += fstride;
  221. tw2 += fstride*2;
  222. Fout[m].r = Fout->r - HALF_OF(scratch[3].r);
  223. Fout[m].i = Fout->i - HALF_OF(scratch[3].i);
  224. C_MULBYSCALAR( scratch[0] , epi3.i );
  225. C_ADDTO(*Fout,scratch[3]);
  226. Fout[m2].r = Fout[m].r + scratch[0].i;
  227. Fout[m2].i = Fout[m].i - scratch[0].r;
  228. Fout[m].r -= scratch[0].i;
  229. Fout[m].i += scratch[0].r;
  230. ++Fout;
  231. } while(--k);
  232. }
  233. }
  234. static void ki_bfly3(
  235. kiss_fft_cpx * Fout,
  236. const size_t fstride,
  237. const kiss_fft_state *st,
  238. int m,
  239. int N,
  240. int mm
  241. )
  242. {
  243. int i, k;
  244. const size_t m2 = 2*m;
  245. const kiss_twiddle_cpx *tw1,*tw2;
  246. kiss_fft_cpx scratch[5];
  247. kiss_twiddle_cpx epi3;
  248. kiss_fft_cpx * Fout_beg = Fout;
  249. epi3 = st->twiddles[fstride*m];
  250. for (i=0;i<N;i++)
  251. {
  252. Fout = Fout_beg + i*mm;
  253. tw1=tw2=st->twiddles;
  254. k=m;
  255. do{
  256. C_MULC(scratch[1],Fout[m] , *tw1);
  257. C_MULC(scratch[2],Fout[m2] , *tw2);
  258. C_ADD(scratch[3],scratch[1],scratch[2]);
  259. C_SUB(scratch[0],scratch[1],scratch[2]);
  260. tw1 += fstride;
  261. tw2 += fstride*2;
  262. Fout[m].r = Fout->r - HALF_OF(scratch[3].r);
  263. Fout[m].i = Fout->i - HALF_OF(scratch[3].i);
  264. C_MULBYSCALAR( scratch[0] , -epi3.i );
  265. C_ADDTO(*Fout,scratch[3]);
  266. Fout[m2].r = Fout[m].r + scratch[0].i;
  267. Fout[m2].i = Fout[m].i - scratch[0].r;
  268. Fout[m].r -= scratch[0].i;
  269. Fout[m].i += scratch[0].r;
  270. ++Fout;
  271. }while(--k);
  272. }
  273. }
  274. static void kf_bfly5(
  275. kiss_fft_cpx * Fout,
  276. const size_t fstride,
  277. const kiss_fft_state *st,
  278. int m,
  279. int N,
  280. int mm
  281. )
  282. {
  283. kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
  284. int i, u;
  285. kiss_fft_cpx scratch[13];
  286. const kiss_twiddle_cpx * twiddles = st->twiddles;
  287. const kiss_twiddle_cpx *tw;
  288. kiss_twiddle_cpx ya,yb;
  289. kiss_fft_cpx * Fout_beg = Fout;
  290. ya = twiddles[fstride*m];
  291. yb = twiddles[fstride*2*m];
  292. tw=st->twiddles;
  293. for (i=0;i<N;i++)
  294. {
  295. Fout = Fout_beg + i*mm;
  296. Fout0=Fout;
  297. Fout1=Fout0+m;
  298. Fout2=Fout0+2*m;
  299. Fout3=Fout0+3*m;
  300. Fout4=Fout0+4*m;
  301. for ( u=0; u<m; ++u ) {
  302. C_FIXDIV( *Fout0,5); C_FIXDIV( *Fout1,5); C_FIXDIV( *Fout2,5); C_FIXDIV( *Fout3,5); C_FIXDIV( *Fout4,5);
  303. scratch[0] = *Fout0;
  304. C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
  305. C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
  306. C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
  307. C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
  308. C_ADD( scratch[7],scratch[1],scratch[4]);
  309. C_SUB( scratch[10],scratch[1],scratch[4]);
  310. C_ADD( scratch[8],scratch[2],scratch[3]);
  311. C_SUB( scratch[9],scratch[2],scratch[3]);
  312. Fout0->r += scratch[7].r + scratch[8].r;
  313. Fout0->i += scratch[7].i + scratch[8].i;
  314. scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r);
  315. scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r);
  316. scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i);
  317. scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i);
  318. C_SUB(*Fout1,scratch[5],scratch[6]);
  319. C_ADD(*Fout4,scratch[5],scratch[6]);
  320. scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r);
  321. scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r);
  322. scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i);
  323. scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i);
  324. C_ADD(*Fout2,scratch[11],scratch[12]);
  325. C_SUB(*Fout3,scratch[11],scratch[12]);
  326. ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
  327. }
  328. }
  329. }
  330. static void ki_bfly5(
  331. kiss_fft_cpx * Fout,
  332. const size_t fstride,
  333. const kiss_fft_state *st,
  334. int m,
  335. int N,
  336. int mm
  337. )
  338. {
  339. kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
  340. int i, u;
  341. kiss_fft_cpx scratch[13];
  342. const kiss_twiddle_cpx * twiddles = st->twiddles;
  343. const kiss_twiddle_cpx *tw;
  344. kiss_twiddle_cpx ya,yb;
  345. kiss_fft_cpx * Fout_beg = Fout;
  346. ya = twiddles[fstride*m];
  347. yb = twiddles[fstride*2*m];
  348. tw=st->twiddles;
  349. for (i=0;i<N;i++)
  350. {
  351. Fout = Fout_beg + i*mm;
  352. Fout0=Fout;
  353. Fout1=Fout0+m;
  354. Fout2=Fout0+2*m;
  355. Fout3=Fout0+3*m;
  356. Fout4=Fout0+4*m;
  357. for ( u=0; u<m; ++u ) {
  358. scratch[0] = *Fout0;
  359. C_MULC(scratch[1] ,*Fout1, tw[u*fstride]);
  360. C_MULC(scratch[2] ,*Fout2, tw[2*u*fstride]);
  361. C_MULC(scratch[3] ,*Fout3, tw[3*u*fstride]);
  362. C_MULC(scratch[4] ,*Fout4, tw[4*u*fstride]);
  363. C_ADD( scratch[7],scratch[1],scratch[4]);
  364. C_SUB( scratch[10],scratch[1],scratch[4]);
  365. C_ADD( scratch[8],scratch[2],scratch[3]);
  366. C_SUB( scratch[9],scratch[2],scratch[3]);
  367. Fout0->r += scratch[7].r + scratch[8].r;
  368. Fout0->i += scratch[7].i + scratch[8].i;
  369. scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r);
  370. scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r);
  371. scratch[6].r = -S_MUL(scratch[10].i,ya.i) - S_MUL(scratch[9].i,yb.i);
  372. scratch[6].i = S_MUL(scratch[10].r,ya.i) + S_MUL(scratch[9].r,yb.i);
  373. C_SUB(*Fout1,scratch[5],scratch[6]);
  374. C_ADD(*Fout4,scratch[5],scratch[6]);
  375. scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r);
  376. scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r);
  377. scratch[12].r = S_MUL(scratch[10].i,yb.i) - S_MUL(scratch[9].i,ya.i);
  378. scratch[12].i = -S_MUL(scratch[10].r,yb.i) + S_MUL(scratch[9].r,ya.i);
  379. C_ADD(*Fout2,scratch[11],scratch[12]);
  380. C_SUB(*Fout3,scratch[11],scratch[12]);
  381. ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
  382. }
  383. }
  384. }
  385. #endif
  386. #ifdef CUSTOM_MODES
  387. static
  388. void compute_bitrev_table(
  389. int Fout,
  390. opus_int16 *f,
  391. const size_t fstride,
  392. int in_stride,
  393. opus_int16 * factors,
  394. const kiss_fft_state *st
  395. )
  396. {
  397. const int p=*factors++; /* the radix */
  398. const int m=*factors++; /* stage's fft length/p */
  399. /*printf ("fft %d %d %d %d %d %d\n", p*m, m, p, s2, fstride*in_stride, N);*/
  400. if (m==1)
  401. {
  402. int j;
  403. for (j=0;j<p;j++)
  404. {
  405. *f = Fout+j;
  406. f += fstride*in_stride;
  407. }
  408. } else {
  409. int j;
  410. for (j=0;j<p;j++)
  411. {
  412. compute_bitrev_table( Fout , f, fstride*p, in_stride, factors,st);
  413. f += fstride*in_stride;
  414. Fout += m;
  415. }
  416. }
  417. }
  418. /* facbuf is populated by p1,m1,p2,m2, ...
  419. where
  420. p[i] * m[i] = m[i-1]
  421. m0 = n */
  422. static
  423. int kf_factor(int n,opus_int16 * facbuf)
  424. {
  425. int p=4;
  426. /*factor out powers of 4, powers of 2, then any remaining primes */
  427. do {
  428. while (n % p) {
  429. switch (p) {
  430. case 4: p = 2; break;
  431. case 2: p = 3; break;
  432. default: p += 2; break;
  433. }
  434. if (p>32000 || (opus_int32)p*(opus_int32)p > n)
  435. p = n; /* no more factors, skip to end */
  436. }
  437. n /= p;
  438. #ifdef RADIX_TWO_ONLY
  439. if (p!=2 && p != 4)
  440. #else
  441. if (p>5)
  442. #endif
  443. {
  444. return 0;
  445. }
  446. *facbuf++ = p;
  447. *facbuf++ = n;
  448. } while (n > 1);
  449. return 1;
  450. }
  451. static void compute_twiddles(kiss_twiddle_cpx *twiddles, int nfft)
  452. {
  453. int i;
  454. #ifdef FIXED_POINT
  455. for (i=0;i<nfft;++i) {
  456. opus_val32 phase = -i;
  457. kf_cexp2(twiddles+i, DIV32(SHL32(phase,17),nfft));
  458. }
  459. #else
  460. for (i=0;i<nfft;++i) {
  461. const double pi=3.14159265358979323846264338327;
  462. double phase = ( -2*pi /nfft ) * i;
  463. kf_cexp(twiddles+i, phase );
  464. }
  465. #endif
  466. }
  467. /*
  468. *
  469. * Allocates all necessary storage space for the fft and ifft.
  470. * The return value is a contiguous block of memory. As such,
  471. * It can be freed with free().
  472. * */
  473. kiss_fft_state *opus_fft_alloc_twiddles(int nfft,void * mem,size_t * lenmem, const kiss_fft_state *base)
  474. {
  475. kiss_fft_state *st=NULL;
  476. size_t memneeded = sizeof(struct kiss_fft_state); /* twiddle factors*/
  477. if ( lenmem==NULL ) {
  478. st = ( kiss_fft_state*)KISS_FFT_MALLOC( memneeded );
  479. }else{
  480. if (mem != NULL && *lenmem >= memneeded)
  481. st = (kiss_fft_state*)mem;
  482. *lenmem = memneeded;
  483. }
  484. if (st) {
  485. opus_int16 *bitrev;
  486. kiss_twiddle_cpx *twiddles;
  487. st->nfft=nfft;
  488. #ifndef FIXED_POINT
  489. st->scale = 1./nfft;
  490. #endif
  491. if (base != NULL)
  492. {
  493. st->twiddles = base->twiddles;
  494. st->shift = 0;
  495. while (nfft<<st->shift != base->nfft && st->shift < 32)
  496. st->shift++;
  497. if (st->shift>=32)
  498. goto fail;
  499. } else {
  500. st->twiddles = twiddles = (kiss_twiddle_cpx*)KISS_FFT_MALLOC(sizeof(kiss_twiddle_cpx)*nfft);
  501. compute_twiddles(twiddles, nfft);
  502. st->shift = -1;
  503. }
  504. if (!kf_factor(nfft,st->factors))
  505. {
  506. goto fail;
  507. }
  508. /* bitrev */
  509. st->bitrev = bitrev = (opus_int16*)KISS_FFT_MALLOC(sizeof(opus_int16)*nfft);
  510. if (st->bitrev==NULL)
  511. goto fail;
  512. compute_bitrev_table(0, bitrev, 1,1, st->factors,st);
  513. }
  514. return st;
  515. fail:
  516. opus_fft_free(st);
  517. return NULL;
  518. }
  519. kiss_fft_state *opus_fft_alloc(int nfft,void * mem,size_t * lenmem )
  520. {
  521. return opus_fft_alloc_twiddles(nfft, mem, lenmem, NULL);
  522. }
  523. void opus_fft_free(const kiss_fft_state *cfg)
  524. {
  525. if (cfg)
  526. {
  527. opus_free((opus_int16*)cfg->bitrev);
  528. if (cfg->shift < 0)
  529. opus_free((kiss_twiddle_cpx*)cfg->twiddles);
  530. opus_free((kiss_fft_state*)cfg);
  531. }
  532. }
  533. #endif /* CUSTOM_MODES */
  534. void opus_fft(const kiss_fft_state *st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
  535. {
  536. int m2, m;
  537. int p;
  538. int L;
  539. int fstride[MAXFACTORS];
  540. int i;
  541. int shift;
  542. /* st->shift can be -1 */
  543. shift = st->shift>0 ? st->shift : 0;
  544. celt_assert2 (fin != fout, "In-place FFT not supported");
  545. /* Bit-reverse the input */
  546. for (i=0;i<st->nfft;i++)
  547. {
  548. fout[st->bitrev[i]] = fin[i];
  549. #ifndef FIXED_POINT
  550. fout[st->bitrev[i]].r *= st->scale;
  551. fout[st->bitrev[i]].i *= st->scale;
  552. #endif
  553. }
  554. fstride[0] = 1;
  555. L=0;
  556. do {
  557. p = st->factors[2*L];
  558. m = st->factors[2*L+1];
  559. fstride[L+1] = fstride[L]*p;
  560. L++;
  561. } while(m!=1);
  562. m = st->factors[2*L-1];
  563. for (i=L-1;i>=0;i--)
  564. {
  565. if (i!=0)
  566. m2 = st->factors[2*i-1];
  567. else
  568. m2 = 1;
  569. switch (st->factors[2*i])
  570. {
  571. case 2:
  572. kf_bfly2(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  573. break;
  574. case 4:
  575. kf_bfly4(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  576. break;
  577. #ifndef RADIX_TWO_ONLY
  578. case 3:
  579. kf_bfly3(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  580. break;
  581. case 5:
  582. kf_bfly5(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  583. break;
  584. #endif
  585. }
  586. m = m2;
  587. }
  588. }
  589. void opus_ifft(const kiss_fft_state *st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
  590. {
  591. int m2, m;
  592. int p;
  593. int L;
  594. int fstride[MAXFACTORS];
  595. int i;
  596. int shift;
  597. /* st->shift can be -1 */
  598. shift = st->shift>0 ? st->shift : 0;
  599. celt_assert2 (fin != fout, "In-place FFT not supported");
  600. /* Bit-reverse the input */
  601. for (i=0;i<st->nfft;i++)
  602. fout[st->bitrev[i]] = fin[i];
  603. fstride[0] = 1;
  604. L=0;
  605. do {
  606. p = st->factors[2*L];
  607. m = st->factors[2*L+1];
  608. fstride[L+1] = fstride[L]*p;
  609. L++;
  610. } while(m!=1);
  611. m = st->factors[2*L-1];
  612. for (i=L-1;i>=0;i--)
  613. {
  614. if (i!=0)
  615. m2 = st->factors[2*i-1];
  616. else
  617. m2 = 1;
  618. switch (st->factors[2*i])
  619. {
  620. case 2:
  621. ki_bfly2(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  622. break;
  623. case 4:
  624. ki_bfly4(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  625. break;
  626. #ifndef RADIX_TWO_ONLY
  627. case 3:
  628. ki_bfly3(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  629. break;
  630. case 5:
  631. ki_bfly5(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  632. break;
  633. #endif
  634. }
  635. m = m2;
  636. }
  637. }