kiss_fft.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. #ifdef FIXED_POINT
  37. #undef S_MUL_ADD
  38. static inline int S_MUL_ADD(int a, int b, int c, int d) {
  39. int m;
  40. long long ac1 = ((long long)a * (long long)b);
  41. long long ac2 = ((long long)c * (long long)d);
  42. ac1 += ac2;
  43. ac1 = ac1>>15;
  44. m = (int )(ac1);
  45. return m;
  46. }
  47. #undef S_MUL_SUB
  48. static inline int S_MUL_SUB(int a, int b, int c, int d) {
  49. int m;
  50. long long ac1 = ((long long)a * (long long)b);
  51. long long ac2 = ((long long)c * (long long)d);
  52. ac1 -= ac2;
  53. ac1 = ac1>>15;
  54. m = (int )(ac1);
  55. return m;
  56. }
  57. #undef C_MUL
  58. # define C_MUL(m,a,b) (m=C_MUL_fun(a,b))
  59. static inline kiss_fft_cpx C_MUL_fun(kiss_fft_cpx a, kiss_twiddle_cpx b) {
  60. kiss_fft_cpx m;
  61. long long ac1 = ((long long)a.r * (long long)b.r);
  62. long long ac2 = ((long long)a.i * (long long)b.i);
  63. ac1 = ac1 - ac2;
  64. ac1 = ac1 >> 15;
  65. m.r = ac1;
  66. ac1 = ((long long)a.r * (long long)b.i);
  67. ac2 = ((long long)a.i * (long long)b.r);
  68. ac1 = ac1 + ac2;
  69. ac1 = ac1 >> 15;
  70. m.i = ac1;
  71. return m;
  72. }
  73. #undef C_MUL4
  74. # define C_MUL4(m,a,b) (m=C_MUL4_fun(a,b))
  75. static inline kiss_fft_cpx C_MUL4_fun(kiss_fft_cpx a, kiss_twiddle_cpx b) {
  76. kiss_fft_cpx m;
  77. long long ac1 = ((long long)a.r * (long long)b.r);
  78. long long ac2 = ((long long)a.i * (long long)b.i);
  79. ac1 = ac1 - ac2;
  80. ac1 = ac1 >> 17;
  81. m.r = ac1;
  82. ac1 = ((long long)a.r * (long long)b.i);
  83. ac2 = ((long long)a.i * (long long)b.r);
  84. ac1 = ac1 + ac2;
  85. ac1 = ac1 >> 17;
  86. m.i = ac1;
  87. return m;
  88. }
  89. #undef C_MULC
  90. # define C_MULC(m,a,b) (m=C_MULC_fun(a,b))
  91. static inline kiss_fft_cpx C_MULC_fun(kiss_fft_cpx a, kiss_twiddle_cpx b) {
  92. kiss_fft_cpx m;
  93. long long ac1 = ((long long)a.r * (long long)b.r);
  94. long long ac2 = ((long long)a.i * (long long)b.i);
  95. ac1 = ac1 + ac2;
  96. ac1 = ac1 >> 15;
  97. m.r = ac1;
  98. ac1 = ((long long)a.i * (long long)b.r);
  99. ac2 = ((long long)a.r * (long long)b.i);
  100. ac1 = ac1 - ac2;
  101. ac1 = ac1 >> 15;
  102. m.i = ac1;
  103. return m;
  104. }
  105. #endif /* FIXED_POINT */
  106. /* The guts header contains all the multiplication and addition macros that are defined for
  107. complex numbers. It also delares the kf_ internal functions.
  108. */
  109. static void kf_bfly2(
  110. kiss_fft_cpx * Fout,
  111. const size_t fstride,
  112. const kiss_fft_state *st,
  113. int m,
  114. int N,
  115. int mm
  116. )
  117. {
  118. kiss_fft_cpx * Fout2;
  119. const kiss_twiddle_cpx * tw1;
  120. int i,j;
  121. kiss_fft_cpx * Fout_beg = Fout;
  122. for (i=0;i<N;i++)
  123. {
  124. Fout = Fout_beg + i*mm;
  125. Fout2 = Fout + m;
  126. tw1 = st->twiddles;
  127. for(j=0;j<m;j++)
  128. {
  129. kiss_fft_cpx t;
  130. Fout->r = SHR32(Fout->r, 1);Fout->i = SHR32(Fout->i, 1);
  131. Fout2->r = SHR32(Fout2->r, 1);Fout2->i = SHR32(Fout2->i, 1);
  132. C_MUL (t, *Fout2 , *tw1);
  133. tw1 += fstride;
  134. C_SUB( *Fout2 , *Fout , t );
  135. C_ADDTO( *Fout , t );
  136. ++Fout2;
  137. ++Fout;
  138. }
  139. }
  140. }
  141. static void ki_bfly2(
  142. kiss_fft_cpx * Fout,
  143. const size_t fstride,
  144. const kiss_fft_state *st,
  145. int m,
  146. int N,
  147. int mm
  148. )
  149. {
  150. kiss_fft_cpx * Fout2;
  151. const kiss_twiddle_cpx * tw1;
  152. kiss_fft_cpx t;
  153. int i,j;
  154. kiss_fft_cpx * Fout_beg = Fout;
  155. for (i=0;i<N;i++)
  156. {
  157. Fout = Fout_beg + i*mm;
  158. Fout2 = Fout + m;
  159. tw1 = st->twiddles;
  160. for(j=0;j<m;j++)
  161. {
  162. C_MULC (t, *Fout2 , *tw1);
  163. tw1 += fstride;
  164. C_SUB( *Fout2 , *Fout , t );
  165. C_ADDTO( *Fout , t );
  166. ++Fout2;
  167. ++Fout;
  168. }
  169. }
  170. }
  171. static void kf_bfly4(
  172. kiss_fft_cpx * Fout,
  173. const size_t fstride,
  174. const kiss_fft_state *st,
  175. int m,
  176. int N,
  177. int mm
  178. )
  179. {
  180. const kiss_twiddle_cpx *tw1,*tw2,*tw3;
  181. kiss_fft_cpx scratch[6];
  182. const size_t m2=2*m;
  183. const size_t m3=3*m;
  184. int i, j;
  185. kiss_fft_cpx * Fout_beg = Fout;
  186. for (i=0;i<N;i++)
  187. {
  188. Fout = Fout_beg + i*mm;
  189. tw3 = tw2 = tw1 = st->twiddles;
  190. for (j=0;j<m;j++)
  191. {
  192. C_MUL4(scratch[0],Fout[m] , *tw1 );
  193. C_MUL4(scratch[1],Fout[m2] , *tw2 );
  194. C_MUL4(scratch[2],Fout[m3] , *tw3 );
  195. Fout->r = PSHR32(Fout->r, 2);
  196. Fout->i = PSHR32(Fout->i, 2);
  197. C_SUB( scratch[5] , *Fout, scratch[1] );
  198. C_ADDTO(*Fout, scratch[1]);
  199. C_ADD( scratch[3] , scratch[0] , scratch[2] );
  200. C_SUB( scratch[4] , scratch[0] , scratch[2] );
  201. C_SUB( Fout[m2], *Fout, scratch[3] );
  202. tw1 += fstride;
  203. tw2 += fstride*2;
  204. tw3 += fstride*3;
  205. C_ADDTO( *Fout , scratch[3] );
  206. Fout[m].r = scratch[5].r + scratch[4].i;
  207. Fout[m].i = scratch[5].i - scratch[4].r;
  208. Fout[m3].r = scratch[5].r - scratch[4].i;
  209. Fout[m3].i = scratch[5].i + scratch[4].r;
  210. ++Fout;
  211. }
  212. }
  213. }
  214. static void ki_bfly4(
  215. kiss_fft_cpx * Fout,
  216. const size_t fstride,
  217. const kiss_fft_state *st,
  218. int m,
  219. int N,
  220. int mm
  221. )
  222. {
  223. const kiss_twiddle_cpx *tw1,*tw2,*tw3;
  224. kiss_fft_cpx scratch[6];
  225. const size_t m2=2*m;
  226. const size_t m3=3*m;
  227. int i, j;
  228. kiss_fft_cpx * Fout_beg = Fout;
  229. for (i=0;i<N;i++)
  230. {
  231. Fout = Fout_beg + i*mm;
  232. tw3 = tw2 = tw1 = st->twiddles;
  233. for (j=0;j<m;j++)
  234. {
  235. C_MULC(scratch[0],Fout[m] , *tw1 );
  236. C_MULC(scratch[1],Fout[m2] , *tw2 );
  237. C_MULC(scratch[2],Fout[m3] , *tw3 );
  238. C_SUB( scratch[5] , *Fout, scratch[1] );
  239. C_ADDTO(*Fout, scratch[1]);
  240. C_ADD( scratch[3] , scratch[0] , scratch[2] );
  241. C_SUB( scratch[4] , scratch[0] , scratch[2] );
  242. C_SUB( Fout[m2], *Fout, scratch[3] );
  243. tw1 += fstride;
  244. tw2 += fstride*2;
  245. tw3 += fstride*3;
  246. C_ADDTO( *Fout , scratch[3] );
  247. Fout[m].r = scratch[5].r - scratch[4].i;
  248. Fout[m].i = scratch[5].i + scratch[4].r;
  249. Fout[m3].r = scratch[5].r + scratch[4].i;
  250. Fout[m3].i = scratch[5].i - scratch[4].r;
  251. ++Fout;
  252. }
  253. }
  254. }
  255. #ifndef RADIX_TWO_ONLY
  256. static void kf_bfly3(
  257. kiss_fft_cpx * Fout,
  258. const size_t fstride,
  259. const kiss_fft_state *st,
  260. int m,
  261. int N,
  262. int mm
  263. )
  264. {
  265. int i;
  266. size_t k;
  267. const size_t m2 = 2*m;
  268. const kiss_twiddle_cpx *tw1,*tw2;
  269. kiss_fft_cpx scratch[5];
  270. kiss_twiddle_cpx epi3;
  271. kiss_fft_cpx * Fout_beg = Fout;
  272. epi3 = st->twiddles[fstride*m];
  273. for (i=0;i<N;i++)
  274. {
  275. Fout = Fout_beg + i*mm;
  276. tw1=tw2=st->twiddles;
  277. k=m;
  278. do {
  279. C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3);
  280. C_MUL(scratch[1],Fout[m] , *tw1);
  281. C_MUL(scratch[2],Fout[m2] , *tw2);
  282. C_ADD(scratch[3],scratch[1],scratch[2]);
  283. C_SUB(scratch[0],scratch[1],scratch[2]);
  284. tw1 += fstride;
  285. tw2 += fstride*2;
  286. Fout[m].r = Fout->r - HALF_OF(scratch[3].r);
  287. Fout[m].i = Fout->i - HALF_OF(scratch[3].i);
  288. C_MULBYSCALAR( scratch[0] , epi3.i );
  289. C_ADDTO(*Fout,scratch[3]);
  290. Fout[m2].r = Fout[m].r + scratch[0].i;
  291. Fout[m2].i = Fout[m].i - scratch[0].r;
  292. Fout[m].r -= scratch[0].i;
  293. Fout[m].i += scratch[0].r;
  294. ++Fout;
  295. } while(--k);
  296. }
  297. }
  298. static void ki_bfly3(
  299. kiss_fft_cpx * Fout,
  300. const size_t fstride,
  301. const kiss_fft_state *st,
  302. int m,
  303. int N,
  304. int mm
  305. )
  306. {
  307. int i, k;
  308. const size_t m2 = 2*m;
  309. const kiss_twiddle_cpx *tw1,*tw2;
  310. kiss_fft_cpx scratch[5];
  311. kiss_twiddle_cpx epi3;
  312. kiss_fft_cpx * Fout_beg = Fout;
  313. epi3 = st->twiddles[fstride*m];
  314. for (i=0;i<N;i++)
  315. {
  316. Fout = Fout_beg + i*mm;
  317. tw1=tw2=st->twiddles;
  318. k=m;
  319. do{
  320. C_MULC(scratch[1],Fout[m] , *tw1);
  321. C_MULC(scratch[2],Fout[m2] , *tw2);
  322. C_ADD(scratch[3],scratch[1],scratch[2]);
  323. C_SUB(scratch[0],scratch[1],scratch[2]);
  324. tw1 += fstride;
  325. tw2 += fstride*2;
  326. Fout[m].r = Fout->r - HALF_OF(scratch[3].r);
  327. Fout[m].i = Fout->i - HALF_OF(scratch[3].i);
  328. C_MULBYSCALAR( scratch[0] , -epi3.i );
  329. C_ADDTO(*Fout,scratch[3]);
  330. Fout[m2].r = Fout[m].r + scratch[0].i;
  331. Fout[m2].i = Fout[m].i - scratch[0].r;
  332. Fout[m].r -= scratch[0].i;
  333. Fout[m].i += scratch[0].r;
  334. ++Fout;
  335. }while(--k);
  336. }
  337. }
  338. static void kf_bfly5(
  339. kiss_fft_cpx * Fout,
  340. const size_t fstride,
  341. const kiss_fft_state *st,
  342. int m,
  343. int N,
  344. int mm
  345. )
  346. {
  347. kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
  348. int i, u;
  349. kiss_fft_cpx scratch[13];
  350. const kiss_twiddle_cpx * twiddles = st->twiddles;
  351. const kiss_twiddle_cpx *tw;
  352. kiss_twiddle_cpx ya,yb;
  353. kiss_fft_cpx * Fout_beg = Fout;
  354. ya = twiddles[fstride*m];
  355. yb = twiddles[fstride*2*m];
  356. tw=st->twiddles;
  357. for (i=0;i<N;i++)
  358. {
  359. Fout = Fout_beg + i*mm;
  360. Fout0=Fout;
  361. Fout1=Fout0+m;
  362. Fout2=Fout0+2*m;
  363. Fout3=Fout0+3*m;
  364. Fout4=Fout0+4*m;
  365. for ( u=0; u<m; ++u ) {
  366. C_FIXDIV( *Fout0,5); C_FIXDIV( *Fout1,5); C_FIXDIV( *Fout2,5); C_FIXDIV( *Fout3,5); C_FIXDIV( *Fout4,5);
  367. scratch[0] = *Fout0;
  368. C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
  369. C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
  370. C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
  371. C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
  372. C_ADD( scratch[7],scratch[1],scratch[4]);
  373. C_SUB( scratch[10],scratch[1],scratch[4]);
  374. C_ADD( scratch[8],scratch[2],scratch[3]);
  375. C_SUB( scratch[9],scratch[2],scratch[3]);
  376. Fout0->r += scratch[7].r + scratch[8].r;
  377. Fout0->i += scratch[7].i + scratch[8].i;
  378. scratch[5].r = scratch[0].r + S_MUL_ADD(scratch[7].r,ya.r,scratch[8].r,yb.r);
  379. scratch[5].i = scratch[0].i + S_MUL_ADD(scratch[7].i,ya.r,scratch[8].i,yb.r);
  380. scratch[6].r = S_MUL_ADD(scratch[10].i,ya.i,scratch[9].i,yb.i);
  381. scratch[6].i = -S_MUL_ADD(scratch[10].r,ya.i,scratch[9].r,yb.i);
  382. C_SUB(*Fout1,scratch[5],scratch[6]);
  383. C_ADD(*Fout4,scratch[5],scratch[6]);
  384. scratch[11].r = scratch[0].r + S_MUL_ADD(scratch[7].r,yb.r,scratch[8].r,ya.r);
  385. scratch[11].i = scratch[0].i + S_MUL_ADD(scratch[7].i,yb.r,scratch[8].i,ya.r);
  386. scratch[12].r = S_MUL_SUB(scratch[9].i,ya.i,scratch[10].i,yb.i);
  387. scratch[12].i = S_MUL_SUB(scratch[10].r,yb.i,scratch[9].r,ya.i);
  388. C_ADD(*Fout2,scratch[11],scratch[12]);
  389. C_SUB(*Fout3,scratch[11],scratch[12]);
  390. ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
  391. }
  392. }
  393. }
  394. static void ki_bfly5(
  395. kiss_fft_cpx * Fout,
  396. const size_t fstride,
  397. const kiss_fft_state *st,
  398. int m,
  399. int N,
  400. int mm
  401. )
  402. {
  403. kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
  404. int i, u;
  405. kiss_fft_cpx scratch[13];
  406. const kiss_twiddle_cpx * twiddles = st->twiddles;
  407. const kiss_twiddle_cpx *tw;
  408. kiss_twiddle_cpx ya,yb;
  409. kiss_fft_cpx * Fout_beg = Fout;
  410. ya = twiddles[fstride*m];
  411. yb = twiddles[fstride*2*m];
  412. tw=st->twiddles;
  413. for (i=0;i<N;i++)
  414. {
  415. Fout = Fout_beg + i*mm;
  416. Fout0=Fout;
  417. Fout1=Fout0+m;
  418. Fout2=Fout0+2*m;
  419. Fout3=Fout0+3*m;
  420. Fout4=Fout0+4*m;
  421. for ( u=0; u<m; ++u ) {
  422. scratch[0] = *Fout0;
  423. C_MULC(scratch[1] ,*Fout1, tw[u*fstride]);
  424. C_MULC(scratch[2] ,*Fout2, tw[2*u*fstride]);
  425. C_MULC(scratch[3] ,*Fout3, tw[3*u*fstride]);
  426. C_MULC(scratch[4] ,*Fout4, tw[4*u*fstride]);
  427. C_ADD( scratch[7],scratch[1],scratch[4]);
  428. C_SUB( scratch[10],scratch[1],scratch[4]);
  429. C_ADD( scratch[8],scratch[2],scratch[3]);
  430. C_SUB( scratch[9],scratch[2],scratch[3]);
  431. Fout0->r += scratch[7].r + scratch[8].r;
  432. Fout0->i += scratch[7].i + scratch[8].i;
  433. scratch[5].r = scratch[0].r + S_MUL_ADD(scratch[7].r,ya.r,scratch[8].r,yb.r);
  434. scratch[5].i = scratch[0].i + S_MUL_ADD(scratch[7].i,ya.r,scratch[8].i,yb.r);
  435. scratch[6].r = -S_MUL_ADD(scratch[10].i,ya.i,scratch[9].i,yb.i);
  436. scratch[6].i = S_MUL_ADD(scratch[10].r,ya.i,scratch[9].r,yb.i);
  437. C_SUB(*Fout1,scratch[5],scratch[6]);
  438. C_ADD(*Fout4,scratch[5],scratch[6]);
  439. scratch[11].r = scratch[0].r + S_MUL_ADD(scratch[7].r,yb.r,scratch[8].r,ya.r);
  440. scratch[11].i = scratch[0].i + S_MUL_ADD(scratch[7].i,yb.r,scratch[8].i,ya.r);
  441. scratch[12].r = S_MUL_SUB(scratch[10].i,yb.i,scratch[9].i,ya.i);
  442. scratch[12].i = S_MUL_SUB(scratch[9].r,ya.i,scratch[10].r,yb.i);
  443. C_ADD(*Fout2,scratch[11],scratch[12]);
  444. C_SUB(*Fout3,scratch[11],scratch[12]);
  445. ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
  446. }
  447. }
  448. }
  449. #endif
  450. #ifdef CUSTOM_MODES
  451. static
  452. void compute_bitrev_table(
  453. int Fout,
  454. opus_int16 *f,
  455. const size_t fstride,
  456. int in_stride,
  457. opus_int16 * factors,
  458. const kiss_fft_state *st
  459. )
  460. {
  461. const int p=*factors++; /* the radix */
  462. const int m=*factors++; /* stage's fft length/p */
  463. /*printf ("fft %d %d %d %d %d %d\n", p*m, m, p, s2, fstride*in_stride, N);*/
  464. if (m==1)
  465. {
  466. int j;
  467. for (j=0;j<p;j++)
  468. {
  469. *f = Fout+j;
  470. f += fstride*in_stride;
  471. }
  472. } else {
  473. int j;
  474. for (j=0;j<p;j++)
  475. {
  476. compute_bitrev_table( Fout , f, fstride*p, in_stride, factors,st);
  477. f += fstride*in_stride;
  478. Fout += m;
  479. }
  480. }
  481. }
  482. /* facbuf is populated by p1,m1,p2,m2, ...
  483. where
  484. p[i] * m[i] = m[i-1]
  485. m0 = n */
  486. static
  487. int kf_factor(int n,opus_int16 * facbuf)
  488. {
  489. int p=4;
  490. /*factor out powers of 4, powers of 2, then any remaining primes */
  491. do {
  492. while (n % p) {
  493. switch (p) {
  494. case 4: p = 2; break;
  495. case 2: p = 3; break;
  496. default: p += 2; break;
  497. }
  498. if (p>32000 || (opus_int32)p*(opus_int32)p > n)
  499. p = n; /* no more factors, skip to end */
  500. }
  501. n /= p;
  502. #ifdef RADIX_TWO_ONLY
  503. if (p!=2 && p != 4)
  504. #else
  505. if (p>5)
  506. #endif
  507. {
  508. return 0;
  509. }
  510. *facbuf++ = p;
  511. *facbuf++ = n;
  512. } while (n > 1);
  513. return 1;
  514. }
  515. static void compute_twiddles(kiss_twiddle_cpx *twiddles, int nfft)
  516. {
  517. int i;
  518. #ifdef FIXED_POINT
  519. for (i=0;i<nfft;++i) {
  520. opus_val32 phase = -i;
  521. kf_cexp2(twiddles+i, DIV32(SHL32(phase,17),nfft));
  522. }
  523. #else
  524. for (i=0;i<nfft;++i) {
  525. const double pi=3.14159265358979323846264338327;
  526. double phase = ( -2*pi /nfft ) * i;
  527. kf_cexp(twiddles+i, phase );
  528. }
  529. #endif
  530. }
  531. /*
  532. *
  533. * Allocates all necessary storage space for the fft and ifft.
  534. * The return value is a contiguous block of memory. As such,
  535. * It can be freed with free().
  536. * */
  537. kiss_fft_state *opus_fft_alloc_twiddles(int nfft,void * mem,size_t * lenmem, const kiss_fft_state *base)
  538. {
  539. kiss_fft_state *st=NULL;
  540. size_t memneeded = sizeof(struct kiss_fft_state); /* twiddle factors*/
  541. if ( lenmem==NULL ) {
  542. st = ( kiss_fft_state*)KISS_FFT_MALLOC( memneeded );
  543. }else{
  544. if (mem != NULL && *lenmem >= memneeded)
  545. st = (kiss_fft_state*)mem;
  546. *lenmem = memneeded;
  547. }
  548. if (st) {
  549. opus_int16 *bitrev;
  550. kiss_twiddle_cpx *twiddles;
  551. st->nfft=nfft;
  552. #ifndef FIXED_POINT
  553. st->scale = 1.f/nfft;
  554. #endif
  555. if (base != NULL)
  556. {
  557. st->twiddles = base->twiddles;
  558. st->shift = 0;
  559. while (nfft<<st->shift != base->nfft && st->shift < 32)
  560. st->shift++;
  561. if (st->shift>=32)
  562. goto fail;
  563. } else {
  564. st->twiddles = twiddles = (kiss_twiddle_cpx*)KISS_FFT_MALLOC(sizeof(kiss_twiddle_cpx)*nfft);
  565. compute_twiddles(twiddles, nfft);
  566. st->shift = -1;
  567. }
  568. if (!kf_factor(nfft,st->factors))
  569. {
  570. goto fail;
  571. }
  572. /* bitrev */
  573. st->bitrev = bitrev = (opus_int16*)KISS_FFT_MALLOC(sizeof(opus_int16)*nfft);
  574. if (st->bitrev==NULL)
  575. goto fail;
  576. compute_bitrev_table(0, bitrev, 1,1, st->factors,st);
  577. }
  578. return st;
  579. fail:
  580. opus_fft_free(st);
  581. return NULL;
  582. }
  583. kiss_fft_state *opus_fft_alloc(int nfft,void * mem,size_t * lenmem )
  584. {
  585. return opus_fft_alloc_twiddles(nfft, mem, lenmem, NULL);
  586. }
  587. void opus_fft_free(const kiss_fft_state *cfg)
  588. {
  589. if (cfg)
  590. {
  591. opus_free((opus_int16*)cfg->bitrev);
  592. if (cfg->shift < 0)
  593. opus_free((kiss_twiddle_cpx*)cfg->twiddles);
  594. opus_free((kiss_fft_state*)cfg);
  595. }
  596. }
  597. #endif /* CUSTOM_MODES */
  598. void opus_fft(const kiss_fft_state *st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
  599. {
  600. int m2, m;
  601. int p;
  602. int L;
  603. int fstride[MAXFACTORS];
  604. int i;
  605. int shift;
  606. /* st->shift can be -1 */
  607. shift = st->shift>0 ? st->shift : 0;
  608. celt_assert2 (fin != fout, "In-place FFT not supported");
  609. /* Bit-reverse the input */
  610. for (i=0;i<st->nfft;i++)
  611. {
  612. fout[st->bitrev[i]] = fin[i];
  613. #ifndef FIXED_POINT
  614. fout[st->bitrev[i]].r *= st->scale;
  615. fout[st->bitrev[i]].i *= st->scale;
  616. #endif
  617. }
  618. fstride[0] = 1;
  619. L=0;
  620. do {
  621. p = st->factors[2*L];
  622. m = st->factors[2*L+1];
  623. fstride[L+1] = fstride[L]*p;
  624. L++;
  625. } while(m!=1);
  626. m = st->factors[2*L-1];
  627. for (i=L-1;i>=0;i--)
  628. {
  629. if (i!=0)
  630. m2 = st->factors[2*i-1];
  631. else
  632. m2 = 1;
  633. switch (st->factors[2*i])
  634. {
  635. case 2:
  636. kf_bfly2(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  637. break;
  638. case 4:
  639. kf_bfly4(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  640. break;
  641. #ifndef RADIX_TWO_ONLY
  642. case 3:
  643. kf_bfly3(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  644. break;
  645. case 5:
  646. kf_bfly5(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  647. break;
  648. #endif
  649. }
  650. m = m2;
  651. }
  652. }
  653. void opus_ifft(const kiss_fft_state *st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
  654. {
  655. int m2, m;
  656. int p;
  657. int L;
  658. int fstride[MAXFACTORS];
  659. int i;
  660. int shift;
  661. /* st->shift can be -1 */
  662. shift = st->shift>0 ? st->shift : 0;
  663. celt_assert2 (fin != fout, "In-place FFT not supported");
  664. /* Bit-reverse the input */
  665. for (i=0;i<st->nfft;i++)
  666. fout[st->bitrev[i]] = fin[i];
  667. fstride[0] = 1;
  668. L=0;
  669. do {
  670. p = st->factors[2*L];
  671. m = st->factors[2*L+1];
  672. fstride[L+1] = fstride[L]*p;
  673. L++;
  674. } while(m!=1);
  675. m = st->factors[2*L-1];
  676. for (i=L-1;i>=0;i--)
  677. {
  678. if (i!=0)
  679. m2 = st->factors[2*i-1];
  680. else
  681. m2 = 1;
  682. switch (st->factors[2*i])
  683. {
  684. case 2:
  685. ki_bfly2(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  686. break;
  687. case 4:
  688. ki_bfly4(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  689. break;
  690. #ifndef RADIX_TWO_ONLY
  691. case 3:
  692. ki_bfly3(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  693. break;
  694. case 5:
  695. ki_bfly5(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  696. break;
  697. #endif
  698. }
  699. m = m2;
  700. }
  701. }