kiss_fft.c 20 KB

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