kiss_fft.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. /* The guts header contains all the multiplication and addition macros that are defined for
  37. complex numbers. It also delares the kf_ internal functions.
  38. */
  39. static void kf_bfly2(
  40. kiss_fft_cpx * Fout,
  41. int m,
  42. int N
  43. )
  44. {
  45. kiss_fft_cpx * Fout2;
  46. int i;
  47. (void)m;
  48. #ifdef CUSTOM_MODES
  49. if (m==1)
  50. {
  51. celt_assert(m==1);
  52. for (i=0;i<N;i++)
  53. {
  54. kiss_fft_cpx t;
  55. Fout2 = Fout + 1;
  56. t = *Fout2;
  57. C_SUB( *Fout2 , *Fout , t );
  58. C_ADDTO( *Fout , t );
  59. Fout += 2;
  60. }
  61. } else
  62. #endif
  63. {
  64. opus_val16 tw;
  65. tw = QCONST16(0.7071067812f, 15);
  66. /* We know that m==4 here because the radix-2 is just after a radix-4 */
  67. celt_assert(m==4);
  68. for (i=0;i<N;i++)
  69. {
  70. kiss_fft_cpx t;
  71. Fout2 = Fout + 4;
  72. t = Fout2[0];
  73. C_SUB( Fout2[0] , Fout[0] , t );
  74. C_ADDTO( Fout[0] , t );
  75. t.r = S_MUL(Fout2[1].r+Fout2[1].i, tw);
  76. t.i = S_MUL(Fout2[1].i-Fout2[1].r, tw);
  77. C_SUB( Fout2[1] , Fout[1] , t );
  78. C_ADDTO( Fout[1] , t );
  79. t.r = Fout2[2].i;
  80. t.i = -Fout2[2].r;
  81. C_SUB( Fout2[2] , Fout[2] , t );
  82. C_ADDTO( Fout[2] , t );
  83. t.r = S_MUL(Fout2[3].i-Fout2[3].r, tw);
  84. t.i = S_MUL(-Fout2[3].i-Fout2[3].r, tw);
  85. C_SUB( Fout2[3] , Fout[3] , t );
  86. C_ADDTO( Fout[3] , t );
  87. Fout += 8;
  88. }
  89. }
  90. }
  91. static void kf_bfly4(
  92. kiss_fft_cpx * Fout,
  93. const size_t fstride,
  94. const kiss_fft_state *st,
  95. int m,
  96. int N,
  97. int mm
  98. )
  99. {
  100. int i;
  101. if (m==1)
  102. {
  103. /* Degenerate case where all the twiddles are 1. */
  104. for (i=0;i<N;i++)
  105. {
  106. kiss_fft_cpx scratch0, scratch1;
  107. C_SUB( scratch0 , *Fout, Fout[2] );
  108. C_ADDTO(*Fout, Fout[2]);
  109. C_ADD( scratch1 , Fout[1] , Fout[3] );
  110. C_SUB( Fout[2], *Fout, scratch1 );
  111. C_ADDTO( *Fout , scratch1 );
  112. C_SUB( scratch1 , Fout[1] , Fout[3] );
  113. Fout[1].r = scratch0.r + scratch1.i;
  114. Fout[1].i = scratch0.i - scratch1.r;
  115. Fout[3].r = scratch0.r - scratch1.i;
  116. Fout[3].i = scratch0.i + scratch1.r;
  117. Fout+=4;
  118. }
  119. } else {
  120. int j;
  121. kiss_fft_cpx scratch[6];
  122. const kiss_twiddle_cpx *tw1,*tw2,*tw3;
  123. const int m2=2*m;
  124. const int m3=3*m;
  125. kiss_fft_cpx * Fout_beg = Fout;
  126. for (i=0;i<N;i++)
  127. {
  128. Fout = Fout_beg + i*mm;
  129. tw3 = tw2 = tw1 = st->twiddles;
  130. /* m is guaranteed to be a multiple of 4. */
  131. for (j=0;j<m;j++)
  132. {
  133. C_MUL(scratch[0],Fout[m] , *tw1 );
  134. C_MUL(scratch[1],Fout[m2] , *tw2 );
  135. C_MUL(scratch[2],Fout[m3] , *tw3 );
  136. C_SUB( scratch[5] , *Fout, scratch[1] );
  137. C_ADDTO(*Fout, scratch[1]);
  138. C_ADD( scratch[3] , scratch[0] , scratch[2] );
  139. C_SUB( scratch[4] , scratch[0] , scratch[2] );
  140. C_SUB( Fout[m2], *Fout, scratch[3] );
  141. tw1 += fstride;
  142. tw2 += fstride*2;
  143. tw3 += fstride*3;
  144. C_ADDTO( *Fout , scratch[3] );
  145. Fout[m].r = scratch[5].r + scratch[4].i;
  146. Fout[m].i = scratch[5].i - scratch[4].r;
  147. Fout[m3].r = scratch[5].r - scratch[4].i;
  148. Fout[m3].i = scratch[5].i + scratch[4].r;
  149. ++Fout;
  150. }
  151. }
  152. }
  153. }
  154. #ifndef RADIX_TWO_ONLY
  155. static void kf_bfly3(
  156. kiss_fft_cpx * Fout,
  157. const size_t fstride,
  158. const kiss_fft_state *st,
  159. int m,
  160. int N,
  161. int mm
  162. )
  163. {
  164. int i;
  165. size_t k;
  166. const size_t m2 = 2*m;
  167. const kiss_twiddle_cpx *tw1,*tw2;
  168. kiss_fft_cpx scratch[5];
  169. kiss_twiddle_cpx epi3;
  170. kiss_fft_cpx * Fout_beg = Fout;
  171. #ifdef FIXED_POINT
  172. /*epi3.r = -16384;*/ /* Unused */
  173. epi3.i = -28378;
  174. #else
  175. epi3 = st->twiddles[fstride*m];
  176. #endif
  177. for (i=0;i<N;i++)
  178. {
  179. Fout = Fout_beg + i*mm;
  180. tw1=tw2=st->twiddles;
  181. /* For non-custom modes, m is guaranteed to be a multiple of 4. */
  182. k=m;
  183. do {
  184. C_MUL(scratch[1],Fout[m] , *tw1);
  185. C_MUL(scratch[2],Fout[m2] , *tw2);
  186. C_ADD(scratch[3],scratch[1],scratch[2]);
  187. C_SUB(scratch[0],scratch[1],scratch[2]);
  188. tw1 += fstride;
  189. tw2 += fstride*2;
  190. Fout[m].r = Fout->r - HALF_OF(scratch[3].r);
  191. Fout[m].i = Fout->i - HALF_OF(scratch[3].i);
  192. C_MULBYSCALAR( scratch[0] , epi3.i );
  193. C_ADDTO(*Fout,scratch[3]);
  194. Fout[m2].r = Fout[m].r + scratch[0].i;
  195. Fout[m2].i = Fout[m].i - scratch[0].r;
  196. Fout[m].r -= scratch[0].i;
  197. Fout[m].i += scratch[0].r;
  198. ++Fout;
  199. } while(--k);
  200. }
  201. }
  202. #ifndef OVERRIDE_kf_bfly5
  203. static void kf_bfly5(
  204. kiss_fft_cpx * Fout,
  205. const size_t fstride,
  206. const kiss_fft_state *st,
  207. int m,
  208. int N,
  209. int mm
  210. )
  211. {
  212. kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
  213. int i, u;
  214. kiss_fft_cpx scratch[13];
  215. const kiss_twiddle_cpx *tw;
  216. kiss_twiddle_cpx ya,yb;
  217. kiss_fft_cpx * Fout_beg = Fout;
  218. #ifdef FIXED_POINT
  219. ya.r = 10126;
  220. ya.i = -31164;
  221. yb.r = -26510;
  222. yb.i = -19261;
  223. #else
  224. ya = st->twiddles[fstride*m];
  225. yb = st->twiddles[fstride*2*m];
  226. #endif
  227. tw=st->twiddles;
  228. for (i=0;i<N;i++)
  229. {
  230. Fout = Fout_beg + i*mm;
  231. Fout0=Fout;
  232. Fout1=Fout0+m;
  233. Fout2=Fout0+2*m;
  234. Fout3=Fout0+3*m;
  235. Fout4=Fout0+4*m;
  236. /* For non-custom modes, m is guaranteed to be a multiple of 4. */
  237. for ( u=0; u<m; ++u ) {
  238. scratch[0] = *Fout0;
  239. C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
  240. C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
  241. C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
  242. C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
  243. C_ADD( scratch[7],scratch[1],scratch[4]);
  244. C_SUB( scratch[10],scratch[1],scratch[4]);
  245. C_ADD( scratch[8],scratch[2],scratch[3]);
  246. C_SUB( scratch[9],scratch[2],scratch[3]);
  247. Fout0->r += scratch[7].r + scratch[8].r;
  248. Fout0->i += scratch[7].i + scratch[8].i;
  249. scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r);
  250. scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r);
  251. scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i);
  252. scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i);
  253. C_SUB(*Fout1,scratch[5],scratch[6]);
  254. C_ADD(*Fout4,scratch[5],scratch[6]);
  255. scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r);
  256. scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r);
  257. scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i);
  258. scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i);
  259. C_ADD(*Fout2,scratch[11],scratch[12]);
  260. C_SUB(*Fout3,scratch[11],scratch[12]);
  261. ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
  262. }
  263. }
  264. }
  265. #endif /* OVERRIDE_kf_bfly5 */
  266. #endif
  267. #ifdef CUSTOM_MODES
  268. static
  269. void compute_bitrev_table(
  270. int Fout,
  271. opus_int16 *f,
  272. const size_t fstride,
  273. int in_stride,
  274. opus_int16 * factors,
  275. const kiss_fft_state *st
  276. )
  277. {
  278. const int p=*factors++; /* the radix */
  279. const int m=*factors++; /* stage's fft length/p */
  280. /*printf ("fft %d %d %d %d %d %d\n", p*m, m, p, s2, fstride*in_stride, N);*/
  281. if (m==1)
  282. {
  283. int j;
  284. for (j=0;j<p;j++)
  285. {
  286. *f = Fout+j;
  287. f += fstride*in_stride;
  288. }
  289. } else {
  290. int j;
  291. for (j=0;j<p;j++)
  292. {
  293. compute_bitrev_table( Fout , f, fstride*p, in_stride, factors,st);
  294. f += fstride*in_stride;
  295. Fout += m;
  296. }
  297. }
  298. }
  299. /* facbuf is populated by p1,m1,p2,m2, ...
  300. where
  301. p[i] * m[i] = m[i-1]
  302. m0 = n */
  303. static
  304. int kf_factor(int n,opus_int16 * facbuf)
  305. {
  306. int p=4;
  307. int i;
  308. int stages=0;
  309. int nbak = n;
  310. /*factor out powers of 4, powers of 2, then any remaining primes */
  311. do {
  312. while (n % p) {
  313. switch (p) {
  314. case 4: p = 2; break;
  315. case 2: p = 3; break;
  316. default: p += 2; break;
  317. }
  318. if (p>32000 || (opus_int32)p*(opus_int32)p > n)
  319. p = n; /* no more factors, skip to end */
  320. }
  321. n /= p;
  322. #ifdef RADIX_TWO_ONLY
  323. if (p!=2 && p != 4)
  324. #else
  325. if (p>5)
  326. #endif
  327. {
  328. return 0;
  329. }
  330. facbuf[2*stages] = p;
  331. if (p==2 && stages > 1)
  332. {
  333. facbuf[2*stages] = 4;
  334. facbuf[2] = 2;
  335. }
  336. stages++;
  337. } while (n > 1);
  338. n = nbak;
  339. /* Reverse the order to get the radix 4 at the end, so we can use the
  340. fast degenerate case. It turns out that reversing the order also
  341. improves the noise behaviour. */
  342. for (i=0;i<stages/2;i++)
  343. {
  344. int tmp;
  345. tmp = facbuf[2*i];
  346. facbuf[2*i] = facbuf[2*(stages-i-1)];
  347. facbuf[2*(stages-i-1)] = tmp;
  348. }
  349. for (i=0;i<stages;i++)
  350. {
  351. n /= facbuf[2*i];
  352. facbuf[2*i+1] = n;
  353. }
  354. return 1;
  355. }
  356. static void compute_twiddles(kiss_twiddle_cpx *twiddles, int nfft)
  357. {
  358. int i;
  359. #ifdef FIXED_POINT
  360. for (i=0;i<nfft;++i) {
  361. opus_val32 phase = -i;
  362. kf_cexp2(twiddles+i, DIV32(SHL32(phase,17),nfft));
  363. }
  364. #else
  365. for (i=0;i<nfft;++i) {
  366. const double pi=3.14159265358979323846264338327;
  367. double phase = ( -2*pi /nfft ) * i;
  368. kf_cexp(twiddles+i, phase );
  369. }
  370. #endif
  371. }
  372. int opus_fft_alloc_arch_c(kiss_fft_state *st) {
  373. (void)st;
  374. return 0;
  375. }
  376. /*
  377. *
  378. * Allocates all necessary storage space for the fft and ifft.
  379. * The return value is a contiguous block of memory. As such,
  380. * It can be freed with free().
  381. * */
  382. kiss_fft_state *opus_fft_alloc_twiddles(int nfft,void * mem,size_t * lenmem,
  383. const kiss_fft_state *base, int arch)
  384. {
  385. kiss_fft_state *st=NULL;
  386. size_t memneeded = sizeof(struct kiss_fft_state); /* twiddle factors*/
  387. if ( lenmem==NULL ) {
  388. st = ( kiss_fft_state*)KISS_FFT_MALLOC( memneeded );
  389. }else{
  390. if (mem != NULL && *lenmem >= memneeded)
  391. st = (kiss_fft_state*)mem;
  392. *lenmem = memneeded;
  393. }
  394. if (st) {
  395. opus_int16 *bitrev;
  396. kiss_twiddle_cpx *twiddles;
  397. st->nfft=nfft;
  398. #ifdef FIXED_POINT
  399. st->scale_shift = celt_ilog2(st->nfft);
  400. if (st->nfft == 1<<st->scale_shift)
  401. st->scale = Q15ONE;
  402. else
  403. st->scale = (1073741824+st->nfft/2)/st->nfft>>(15-st->scale_shift);
  404. #else
  405. st->scale = 1.f/nfft;
  406. #endif
  407. if (base != NULL)
  408. {
  409. st->twiddles = base->twiddles;
  410. st->shift = 0;
  411. while (st->shift < 32 && nfft<<st->shift != base->nfft)
  412. st->shift++;
  413. if (st->shift>=32)
  414. goto fail;
  415. } else {
  416. st->twiddles = twiddles = (kiss_twiddle_cpx*)KISS_FFT_MALLOC(sizeof(kiss_twiddle_cpx)*nfft);
  417. compute_twiddles(twiddles, nfft);
  418. st->shift = -1;
  419. }
  420. if (!kf_factor(nfft,st->factors))
  421. {
  422. goto fail;
  423. }
  424. /* bitrev */
  425. st->bitrev = bitrev = (opus_int16*)KISS_FFT_MALLOC(sizeof(opus_int16)*nfft);
  426. if (st->bitrev==NULL)
  427. goto fail;
  428. compute_bitrev_table(0, bitrev, 1,1, st->factors,st);
  429. /* Initialize architecture specific fft parameters */
  430. if (opus_fft_alloc_arch(st, arch))
  431. goto fail;
  432. }
  433. return st;
  434. fail:
  435. opus_fft_free(st, arch);
  436. return NULL;
  437. }
  438. kiss_fft_state *opus_fft_alloc(int nfft,void * mem,size_t * lenmem, int arch)
  439. {
  440. return opus_fft_alloc_twiddles(nfft, mem, lenmem, NULL, arch);
  441. }
  442. void opus_fft_free_arch_c(kiss_fft_state *st) {
  443. (void)st;
  444. }
  445. void opus_fft_free(const kiss_fft_state *cfg, int arch)
  446. {
  447. if (cfg)
  448. {
  449. opus_fft_free_arch((kiss_fft_state *)cfg, arch);
  450. opus_free((opus_int16*)cfg->bitrev);
  451. if (cfg->shift < 0)
  452. opus_free((kiss_twiddle_cpx*)cfg->twiddles);
  453. opus_free((kiss_fft_state*)cfg);
  454. }
  455. }
  456. #endif /* CUSTOM_MODES */
  457. void opus_fft_impl(const kiss_fft_state *st,kiss_fft_cpx *fout)
  458. {
  459. int m2, m;
  460. int p;
  461. int L;
  462. int fstride[MAXFACTORS];
  463. int i;
  464. int shift;
  465. /* st->shift can be -1 */
  466. shift = st->shift>0 ? st->shift : 0;
  467. fstride[0] = 1;
  468. L=0;
  469. do {
  470. p = st->factors[2*L];
  471. m = st->factors[2*L+1];
  472. fstride[L+1] = fstride[L]*p;
  473. L++;
  474. } while(m!=1);
  475. m = st->factors[2*L-1];
  476. for (i=L-1;i>=0;i--)
  477. {
  478. if (i!=0)
  479. m2 = st->factors[2*i-1];
  480. else
  481. m2 = 1;
  482. switch (st->factors[2*i])
  483. {
  484. case 2:
  485. kf_bfly2(fout, m, fstride[i]);
  486. break;
  487. case 4:
  488. kf_bfly4(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  489. break;
  490. #ifndef RADIX_TWO_ONLY
  491. case 3:
  492. kf_bfly3(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  493. break;
  494. case 5:
  495. kf_bfly5(fout,fstride[i]<<shift,st,m, fstride[i], m2);
  496. break;
  497. #endif
  498. }
  499. m = m2;
  500. }
  501. }
  502. void opus_fft_c(const kiss_fft_state *st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
  503. {
  504. int i;
  505. opus_val16 scale;
  506. #ifdef FIXED_POINT
  507. /* Allows us to scale with MULT16_32_Q16(), which is faster than
  508. MULT16_32_Q15() on ARM. */
  509. int scale_shift = st->scale_shift-1;
  510. #endif
  511. scale = st->scale;
  512. celt_assert2 (fin != fout, "In-place FFT not supported");
  513. /* Bit-reverse the input */
  514. for (i=0;i<st->nfft;i++)
  515. {
  516. kiss_fft_cpx x = fin[i];
  517. fout[st->bitrev[i]].r = SHR32(MULT16_32_Q16(scale, x.r), scale_shift);
  518. fout[st->bitrev[i]].i = SHR32(MULT16_32_Q16(scale, x.i), scale_shift);
  519. }
  520. opus_fft_impl(st, fout);
  521. }
  522. void opus_ifft_c(const kiss_fft_state *st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
  523. {
  524. int i;
  525. celt_assert2 (fin != fout, "In-place FFT not supported");
  526. /* Bit-reverse the input */
  527. for (i=0;i<st->nfft;i++)
  528. fout[st->bitrev[i]] = fin[i];
  529. for (i=0;i<st->nfft;i++)
  530. fout[i].i = -fout[i].i;
  531. opus_fft_impl(st, fout);
  532. for (i=0;i<st->nfft;i++)
  533. fout[i].i = -fout[i].i;
  534. }