smallft.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
  6. * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and the XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: *unnormalized* fft transform
  14. last mod: $Id: smallft.c,v 1.9.2.2 2000/11/04 06:43:51 xiphmont Exp $
  15. ********************************************************************/
  16. /* FFT implementation from OggSquish, minus cosine transforms,
  17. * minus all but radix 2/4 case. In Vorbis we only need this
  18. * cut-down version.
  19. *
  20. * To do more than just power-of-two sized vectors, see the full
  21. * version I wrote for NetLib.
  22. *
  23. * Note that the packing is a little strange; rather than the FFT r/i
  24. * packing following R_0, I_n, R_1, I_1, R_2, I_2 ... R_n-1, I_n-1,
  25. * it follows R_0, R_1, I_1, R_2, I_2 ... R_n-1, I_n-1, I_n like the
  26. * FORTRAN version
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <math.h>
  31. #include "smallft.h"
  32. #include "misc.h"
  33. static void drfti1(int n, float *wa, int *ifac){
  34. static int ntryh[4] = { 4,2,3,5 };
  35. static float tpi = 6.28318530717958647692528676655900577;
  36. float arg,argh,argld,fi;
  37. int ntry=0,i,j=-1;
  38. int k1, l1, l2, ib;
  39. int ld, ii, ip, is, nq, nr;
  40. int ido, ipm, nfm1;
  41. int nl=n;
  42. int nf=0;
  43. L101:
  44. j++;
  45. if (j < 4)
  46. ntry=ntryh[j];
  47. else
  48. ntry+=2;
  49. L104:
  50. nq=nl/ntry;
  51. nr=nl-ntry*nq;
  52. if (nr!=0) goto L101;
  53. nf++;
  54. ifac[nf+1]=ntry;
  55. nl=nq;
  56. if(ntry!=2)goto L107;
  57. if(nf==1)goto L107;
  58. for (i=1;i<nf;i++){
  59. ib=nf-i+1;
  60. ifac[ib+1]=ifac[ib];
  61. }
  62. ifac[2] = 2;
  63. L107:
  64. if(nl!=1)goto L104;
  65. ifac[0]=n;
  66. ifac[1]=nf;
  67. argh=tpi/n;
  68. is=0;
  69. nfm1=nf-1;
  70. l1=1;
  71. if(nfm1==0)return;
  72. for (k1=0;k1<nfm1;k1++){
  73. ip=ifac[k1+2];
  74. ld=0;
  75. l2=l1*ip;
  76. ido=n/l2;
  77. ipm=ip-1;
  78. for (j=0;j<ipm;j++){
  79. ld+=l1;
  80. i=is;
  81. argld=(float)ld*argh;
  82. fi=0.;
  83. for (ii=2;ii<ido;ii+=2){
  84. fi+=1.;
  85. arg=fi*argld;
  86. wa[i++]=cos(arg);
  87. wa[i++]=sin(arg);
  88. }
  89. is+=ido;
  90. }
  91. l1=l2;
  92. }
  93. }
  94. static void fdrffti(int n, float *wsave, int *ifac){
  95. if (n == 1) return;
  96. drfti1(n, wsave+n, ifac);
  97. }
  98. static void dradf2(int ido,int l1,float *cc,float *ch,float *wa1){
  99. int i,k;
  100. float ti2,tr2;
  101. int t0,t1,t2,t3,t4,t5,t6;
  102. t1=0;
  103. t0=(t2=l1*ido);
  104. t3=ido<<1;
  105. for(k=0;k<l1;k++){
  106. ch[t1<<1]=cc[t1]+cc[t2];
  107. ch[(t1<<1)+t3-1]=cc[t1]-cc[t2];
  108. t1+=ido;
  109. t2+=ido;
  110. }
  111. if(ido<2)return;
  112. if(ido==2)goto L105;
  113. t1=0;
  114. t2=t0;
  115. for(k=0;k<l1;k++){
  116. t3=t2;
  117. t4=(t1<<1)+(ido<<1);
  118. t5=t1;
  119. t6=t1+t1;
  120. for(i=2;i<ido;i+=2){
  121. t3+=2;
  122. t4-=2;
  123. t5+=2;
  124. t6+=2;
  125. tr2=wa1[i-2]*cc[t3-1]+wa1[i-1]*cc[t3];
  126. ti2=wa1[i-2]*cc[t3]-wa1[i-1]*cc[t3-1];
  127. ch[t6]=cc[t5]+ti2;
  128. ch[t4]=ti2-cc[t5];
  129. ch[t6-1]=cc[t5-1]+tr2;
  130. ch[t4-1]=cc[t5-1]-tr2;
  131. }
  132. t1+=ido;
  133. t2+=ido;
  134. }
  135. if(ido%2==1)return;
  136. L105:
  137. t3=(t2=(t1=ido)-1);
  138. t2+=t0;
  139. for(k=0;k<l1;k++){
  140. ch[t1]=-cc[t2];
  141. ch[t1-1]=cc[t3];
  142. t1+=ido<<1;
  143. t2+=ido;
  144. t3+=ido;
  145. }
  146. }
  147. static void dradf4(int ido,int l1,float *cc,float *ch,float *wa1,
  148. float *wa2,float *wa3){
  149. static float hsqt2 = .70710678118654752440084436210485;
  150. int i,k,t0,t1,t2,t3,t4,t5,t6;
  151. float ci2,ci3,ci4,cr2,cr3,cr4,ti1,ti2,ti3,ti4,tr1,tr2,tr3,tr4;
  152. t0=l1*ido;
  153. t1=t0;
  154. t4=t1<<1;
  155. t2=t1+(t1<<1);
  156. t3=0;
  157. for(k=0;k<l1;k++){
  158. tr1=cc[t1]+cc[t2];
  159. tr2=cc[t3]+cc[t4];
  160. ch[t5=t3<<2]=tr1+tr2;
  161. ch[(ido<<2)+t5-1]=tr2-tr1;
  162. ch[(t5+=(ido<<1))-1]=cc[t3]-cc[t4];
  163. ch[t5]=cc[t2]-cc[t1];
  164. t1+=ido;
  165. t2+=ido;
  166. t3+=ido;
  167. t4+=ido;
  168. }
  169. if(ido<2)return;
  170. if(ido==2)goto L105;
  171. t1=0;
  172. for(k=0;k<l1;k++){
  173. t2=t1;
  174. t4=t1<<2;
  175. t5=(t6=ido<<1)+t4;
  176. for(i=2;i<ido;i+=2){
  177. t3=(t2+=2);
  178. t4+=2;
  179. t5-=2;
  180. t3+=t0;
  181. cr2=wa1[i-2]*cc[t3-1]+wa1[i-1]*cc[t3];
  182. ci2=wa1[i-2]*cc[t3]-wa1[i-1]*cc[t3-1];
  183. t3+=t0;
  184. cr3=wa2[i-2]*cc[t3-1]+wa2[i-1]*cc[t3];
  185. ci3=wa2[i-2]*cc[t3]-wa2[i-1]*cc[t3-1];
  186. t3+=t0;
  187. cr4=wa3[i-2]*cc[t3-1]+wa3[i-1]*cc[t3];
  188. ci4=wa3[i-2]*cc[t3]-wa3[i-1]*cc[t3-1];
  189. tr1=cr2+cr4;
  190. tr4=cr4-cr2;
  191. ti1=ci2+ci4;
  192. ti4=ci2-ci4;
  193. ti2=cc[t2]+ci3;
  194. ti3=cc[t2]-ci3;
  195. tr2=cc[t2-1]+cr3;
  196. tr3=cc[t2-1]-cr3;
  197. ch[t4-1]=tr1+tr2;
  198. ch[t4]=ti1+ti2;
  199. ch[t5-1]=tr3-ti4;
  200. ch[t5]=tr4-ti3;
  201. ch[t4+t6-1]=ti4+tr3;
  202. ch[t4+t6]=tr4+ti3;
  203. ch[t5+t6-1]=tr2-tr1;
  204. ch[t5+t6]=ti1-ti2;
  205. }
  206. t1+=ido;
  207. }
  208. if(ido&1)return;
  209. L105:
  210. t2=(t1=t0+ido-1)+(t0<<1);
  211. t3=ido<<2;
  212. t4=ido;
  213. t5=ido<<1;
  214. t6=ido;
  215. for(k=0;k<l1;k++){
  216. ti1=-hsqt2*(cc[t1]+cc[t2]);
  217. tr1=hsqt2*(cc[t1]-cc[t2]);
  218. ch[t4-1]=tr1+cc[t6-1];
  219. ch[t4+t5-1]=cc[t6-1]-tr1;
  220. ch[t4]=ti1-cc[t1+t0];
  221. ch[t4+t5]=ti1+cc[t1+t0];
  222. t1+=ido;
  223. t2+=ido;
  224. t4+=t3;
  225. t6+=ido;
  226. }
  227. }
  228. static void dradfg(int ido,int ip,int l1,int idl1,float *cc,float *c1,
  229. float *c2,float *ch,float *ch2,float *wa){
  230. static float tpi=6.28318530717958647692528676655900577;
  231. int idij,ipph,i,j,k,l,ic,ik,is;
  232. int t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
  233. float dc2,ai1,ai2,ar1,ar2,ds2;
  234. int nbd;
  235. float dcp,arg,dsp,ar1h,ar2h;
  236. int idp2,ipp2;
  237. arg=tpi/(float)ip;
  238. dcp=cos(arg);
  239. dsp=sin(arg);
  240. ipph=(ip+1)>>1;
  241. ipp2=ip;
  242. idp2=ido;
  243. nbd=(ido-1)>>1;
  244. t0=l1*ido;
  245. t10=ip*ido;
  246. if(ido==1)goto L119;
  247. for(ik=0;ik<idl1;ik++)ch2[ik]=c2[ik];
  248. t1=0;
  249. for(j=1;j<ip;j++){
  250. t1+=t0;
  251. t2=t1;
  252. for(k=0;k<l1;k++){
  253. ch[t2]=c1[t2];
  254. t2+=ido;
  255. }
  256. }
  257. is=-ido;
  258. t1=0;
  259. if(nbd>l1){
  260. for(j=1;j<ip;j++){
  261. t1+=t0;
  262. is+=ido;
  263. t2= -ido+t1;
  264. for(k=0;k<l1;k++){
  265. idij=is-1;
  266. t2+=ido;
  267. t3=t2;
  268. for(i=2;i<ido;i+=2){
  269. idij+=2;
  270. t3+=2;
  271. ch[t3-1]=wa[idij-1]*c1[t3-1]+wa[idij]*c1[t3];
  272. ch[t3]=wa[idij-1]*c1[t3]-wa[idij]*c1[t3-1];
  273. }
  274. }
  275. }
  276. }else{
  277. for(j=1;j<ip;j++){
  278. is+=ido;
  279. idij=is-1;
  280. t1+=t0;
  281. t2=t1;
  282. for(i=2;i<ido;i+=2){
  283. idij+=2;
  284. t2+=2;
  285. t3=t2;
  286. for(k=0;k<l1;k++){
  287. ch[t3-1]=wa[idij-1]*c1[t3-1]+wa[idij]*c1[t3];
  288. ch[t3]=wa[idij-1]*c1[t3]-wa[idij]*c1[t3-1];
  289. t3+=ido;
  290. }
  291. }
  292. }
  293. }
  294. t1=0;
  295. t2=ipp2*t0;
  296. if(nbd<l1){
  297. for(j=1;j<ipph;j++){
  298. t1+=t0;
  299. t2-=t0;
  300. t3=t1;
  301. t4=t2;
  302. for(i=2;i<ido;i+=2){
  303. t3+=2;
  304. t4+=2;
  305. t5=t3-ido;
  306. t6=t4-ido;
  307. for(k=0;k<l1;k++){
  308. t5+=ido;
  309. t6+=ido;
  310. c1[t5-1]=ch[t5-1]+ch[t6-1];
  311. c1[t6-1]=ch[t5]-ch[t6];
  312. c1[t5]=ch[t5]+ch[t6];
  313. c1[t6]=ch[t6-1]-ch[t5-1];
  314. }
  315. }
  316. }
  317. }else{
  318. for(j=1;j<ipph;j++){
  319. t1+=t0;
  320. t2-=t0;
  321. t3=t1;
  322. t4=t2;
  323. for(k=0;k<l1;k++){
  324. t5=t3;
  325. t6=t4;
  326. for(i=2;i<ido;i+=2){
  327. t5+=2;
  328. t6+=2;
  329. c1[t5-1]=ch[t5-1]+ch[t6-1];
  330. c1[t6-1]=ch[t5]-ch[t6];
  331. c1[t5]=ch[t5]+ch[t6];
  332. c1[t6]=ch[t6-1]-ch[t5-1];
  333. }
  334. t3+=ido;
  335. t4+=ido;
  336. }
  337. }
  338. }
  339. L119:
  340. for(ik=0;ik<idl1;ik++)c2[ik]=ch2[ik];
  341. t1=0;
  342. t2=ipp2*idl1;
  343. for(j=1;j<ipph;j++){
  344. t1+=t0;
  345. t2-=t0;
  346. t3=t1-ido;
  347. t4=t2-ido;
  348. for(k=0;k<l1;k++){
  349. t3+=ido;
  350. t4+=ido;
  351. c1[t3]=ch[t3]+ch[t4];
  352. c1[t4]=ch[t4]-ch[t3];
  353. }
  354. }
  355. ar1=1.;
  356. ai1=0.;
  357. t1=0;
  358. t2=ipp2*idl1;
  359. t3=(ip-1)*idl1;
  360. for(l=1;l<ipph;l++){
  361. t1+=idl1;
  362. t2-=idl1;
  363. ar1h=dcp*ar1-dsp*ai1;
  364. ai1=dcp*ai1+dsp*ar1;
  365. ar1=ar1h;
  366. t4=t1;
  367. t5=t2;
  368. t6=t3;
  369. t7=idl1;
  370. for(ik=0;ik<idl1;ik++){
  371. ch2[t4++]=c2[ik]+ar1*c2[t7++];
  372. ch2[t5++]=ai1*c2[t6++];
  373. }
  374. dc2=ar1;
  375. ds2=ai1;
  376. ar2=ar1;
  377. ai2=ai1;
  378. t4=idl1;
  379. t5=(ipp2-1)*idl1;
  380. for(j=2;j<ipph;j++){
  381. t4+=idl1;
  382. t5-=idl1;
  383. ar2h=dc2*ar2-ds2*ai2;
  384. ai2=dc2*ai2+ds2*ar2;
  385. ar2=ar2h;
  386. t6=t1;
  387. t7=t2;
  388. t8=t4;
  389. t9=t5;
  390. for(ik=0;ik<idl1;ik++){
  391. ch2[t6++]+=ar2*c2[t8++];
  392. ch2[t7++]+=ai2*c2[t9++];
  393. }
  394. }
  395. }
  396. t1=0;
  397. for(j=1;j<ipph;j++){
  398. t1+=idl1;
  399. t2=t1;
  400. for(ik=0;ik<idl1;ik++)ch2[ik]+=c2[t2++];
  401. }
  402. if(ido<l1)goto L132;
  403. t1=0;
  404. t2=0;
  405. for(k=0;k<l1;k++){
  406. t3=t1;
  407. t4=t2;
  408. for(i=0;i<ido;i++)cc[t4++]=ch[t3++];
  409. t1+=ido;
  410. t2+=t10;
  411. }
  412. goto L135;
  413. L132:
  414. for(i=0;i<ido;i++){
  415. t1=i;
  416. t2=i;
  417. for(k=0;k<l1;k++){
  418. cc[t2]=ch[t1];
  419. t1+=ido;
  420. t2+=t10;
  421. }
  422. }
  423. L135:
  424. t1=0;
  425. t2=ido<<1;
  426. t3=0;
  427. t4=ipp2*t0;
  428. for(j=1;j<ipph;j++){
  429. t1+=t2;
  430. t3+=t0;
  431. t4-=t0;
  432. t5=t1;
  433. t6=t3;
  434. t7=t4;
  435. for(k=0;k<l1;k++){
  436. cc[t5-1]=ch[t6];
  437. cc[t5]=ch[t7];
  438. t5+=t10;
  439. t6+=ido;
  440. t7+=ido;
  441. }
  442. }
  443. if(ido==1)return;
  444. if(nbd<l1)goto L141;
  445. t1=-ido;
  446. t3=0;
  447. t4=0;
  448. t5=ipp2*t0;
  449. for(j=1;j<ipph;j++){
  450. t1+=t2;
  451. t3+=t2;
  452. t4+=t0;
  453. t5-=t0;
  454. t6=t1;
  455. t7=t3;
  456. t8=t4;
  457. t9=t5;
  458. for(k=0;k<l1;k++){
  459. for(i=2;i<ido;i+=2){
  460. ic=idp2-i;
  461. cc[i+t7-1]=ch[i+t8-1]+ch[i+t9-1];
  462. cc[ic+t6-1]=ch[i+t8-1]-ch[i+t9-1];
  463. cc[i+t7]=ch[i+t8]+ch[i+t9];
  464. cc[ic+t6]=ch[i+t9]-ch[i+t8];
  465. }
  466. t6+=t10;
  467. t7+=t10;
  468. t8+=ido;
  469. t9+=ido;
  470. }
  471. }
  472. return;
  473. L141:
  474. t1=-ido;
  475. t3=0;
  476. t4=0;
  477. t5=ipp2*t0;
  478. for(j=1;j<ipph;j++){
  479. t1+=t2;
  480. t3+=t2;
  481. t4+=t0;
  482. t5-=t0;
  483. for(i=2;i<ido;i+=2){
  484. t6=idp2+t1-i;
  485. t7=i+t3;
  486. t8=i+t4;
  487. t9=i+t5;
  488. for(k=0;k<l1;k++){
  489. cc[t7-1]=ch[t8-1]+ch[t9-1];
  490. cc[t6-1]=ch[t8-1]-ch[t9-1];
  491. cc[t7]=ch[t8]+ch[t9];
  492. cc[t6]=ch[t9]-ch[t8];
  493. t6+=t10;
  494. t7+=t10;
  495. t8+=ido;
  496. t9+=ido;
  497. }
  498. }
  499. }
  500. }
  501. static void drftf1(int n,float *c,float *ch,float *wa,int *ifac){
  502. int i,k1,l1,l2;
  503. int na,kh,nf;
  504. int ip,iw,ido,idl1,ix2,ix3;
  505. nf=ifac[1];
  506. na=1;
  507. l2=n;
  508. iw=n;
  509. for(k1=0;k1<nf;k1++){
  510. kh=nf-k1;
  511. ip=ifac[kh+1];
  512. l1=l2/ip;
  513. ido=n/l2;
  514. idl1=ido*l1;
  515. iw-=(ip-1)*ido;
  516. na=1-na;
  517. if(ip!=4)goto L102;
  518. ix2=iw+ido;
  519. ix3=ix2+ido;
  520. if(na!=0)
  521. dradf4(ido,l1,ch,c,wa+iw-1,wa+ix2-1,wa+ix3-1);
  522. else
  523. dradf4(ido,l1,c,ch,wa+iw-1,wa+ix2-1,wa+ix3-1);
  524. goto L110;
  525. L102:
  526. if(ip!=2)goto L104;
  527. if(na!=0)goto L103;
  528. dradf2(ido,l1,c,ch,wa+iw-1);
  529. goto L110;
  530. L103:
  531. dradf2(ido,l1,ch,c,wa+iw-1);
  532. goto L110;
  533. L104:
  534. if(ido==1)na=1-na;
  535. if(na!=0)goto L109;
  536. dradfg(ido,ip,l1,idl1,c,c,c,ch,ch,wa+iw-1);
  537. na=1;
  538. goto L110;
  539. L109:
  540. dradfg(ido,ip,l1,idl1,ch,ch,ch,c,c,wa+iw-1);
  541. na=0;
  542. L110:
  543. l2=l1;
  544. }
  545. if(na==1)return;
  546. for(i=0;i<n;i++)c[i]=ch[i];
  547. }
  548. static void dradb2(int ido,int l1,float *cc,float *ch,float *wa1){
  549. int i,k,t0,t1,t2,t3,t4,t5,t6;
  550. float ti2,tr2;
  551. t0=l1*ido;
  552. t1=0;
  553. t2=0;
  554. t3=(ido<<1)-1;
  555. for(k=0;k<l1;k++){
  556. ch[t1]=cc[t2]+cc[t3+t2];
  557. ch[t1+t0]=cc[t2]-cc[t3+t2];
  558. t2=(t1+=ido)<<1;
  559. }
  560. if(ido<2)return;
  561. if(ido==2)goto L105;
  562. t1=0;
  563. t2=0;
  564. for(k=0;k<l1;k++){
  565. t3=t1;
  566. t5=(t4=t2)+(ido<<1);
  567. t6=t0+t1;
  568. for(i=2;i<ido;i+=2){
  569. t3+=2;
  570. t4+=2;
  571. t5-=2;
  572. t6+=2;
  573. ch[t3-1]=cc[t4-1]+cc[t5-1];
  574. tr2=cc[t4-1]-cc[t5-1];
  575. ch[t3]=cc[t4]-cc[t5];
  576. ti2=cc[t4]+cc[t5];
  577. ch[t6-1]=wa1[i-2]*tr2-wa1[i-1]*ti2;
  578. ch[t6]=wa1[i-2]*ti2+wa1[i-1]*tr2;
  579. }
  580. t2=(t1+=ido)<<1;
  581. }
  582. if(ido%2==1)return;
  583. L105:
  584. t1=ido-1;
  585. t2=ido-1;
  586. for(k=0;k<l1;k++){
  587. ch[t1]=cc[t2]+cc[t2];
  588. ch[t1+t0]=-(cc[t2+1]+cc[t2+1]);
  589. t1+=ido;
  590. t2+=ido<<1;
  591. }
  592. }
  593. static void dradb3(int ido,int l1,float *cc,float *ch,float *wa1,
  594. float *wa2){
  595. static float taur = -.5;
  596. static float taui = .86602540378443864676372317075293618;
  597. int i,k,t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
  598. float ci2,ci3,di2,di3,cr2,cr3,dr2,dr3,ti2,tr2;
  599. t0=l1*ido;
  600. t1=0;
  601. t2=t0<<1;
  602. t3=ido<<1;
  603. t4=ido+(ido<<1);
  604. t5=0;
  605. for(k=0;k<l1;k++){
  606. tr2=cc[t3-1]+cc[t3-1];
  607. cr2=cc[t5]+(taur*tr2);
  608. ch[t1]=cc[t5]+tr2;
  609. ci3=taui*(cc[t3]+cc[t3]);
  610. ch[t1+t0]=cr2-ci3;
  611. ch[t1+t2]=cr2+ci3;
  612. t1+=ido;
  613. t3+=t4;
  614. t5+=t4;
  615. }
  616. if(ido==1)return;
  617. t1=0;
  618. t3=ido<<1;
  619. for(k=0;k<l1;k++){
  620. t7=t1+(t1<<1);
  621. t6=(t5=t7+t3);
  622. t8=t1;
  623. t10=(t9=t1+t0)+t0;
  624. for(i=2;i<ido;i+=2){
  625. t5+=2;
  626. t6-=2;
  627. t7+=2;
  628. t8+=2;
  629. t9+=2;
  630. t10+=2;
  631. tr2=cc[t5-1]+cc[t6-1];
  632. cr2=cc[t7-1]+(taur*tr2);
  633. ch[t8-1]=cc[t7-1]+tr2;
  634. ti2=cc[t5]-cc[t6];
  635. ci2=cc[t7]+(taur*ti2);
  636. ch[t8]=cc[t7]+ti2;
  637. cr3=taui*(cc[t5-1]-cc[t6-1]);
  638. ci3=taui*(cc[t5]+cc[t6]);
  639. dr2=cr2-ci3;
  640. dr3=cr2+ci3;
  641. di2=ci2+cr3;
  642. di3=ci2-cr3;
  643. ch[t9-1]=wa1[i-2]*dr2-wa1[i-1]*di2;
  644. ch[t9]=wa1[i-2]*di2+wa1[i-1]*dr2;
  645. ch[t10-1]=wa2[i-2]*dr3-wa2[i-1]*di3;
  646. ch[t10]=wa2[i-2]*di3+wa2[i-1]*dr3;
  647. }
  648. t1+=ido;
  649. }
  650. }
  651. static void dradb4(int ido,int l1,float *cc,float *ch,float *wa1,
  652. float *wa2,float *wa3){
  653. static float sqrt2=1.4142135623730950488016887242097;
  654. int i,k,t0,t1,t2,t3,t4,t5,t6,t7,t8;
  655. float ci2,ci3,ci4,cr2,cr3,cr4,ti1,ti2,ti3,ti4,tr1,tr2,tr3,tr4;
  656. t0=l1*ido;
  657. t1=0;
  658. t2=ido<<2;
  659. t3=0;
  660. t6=ido<<1;
  661. for(k=0;k<l1;k++){
  662. t4=t3+t6;
  663. t5=t1;
  664. tr3=cc[t4-1]+cc[t4-1];
  665. tr4=cc[t4]+cc[t4];
  666. tr1=cc[t3]-cc[(t4+=t6)-1];
  667. tr2=cc[t3]+cc[t4-1];
  668. ch[t5]=tr2+tr3;
  669. ch[t5+=t0]=tr1-tr4;
  670. ch[t5+=t0]=tr2-tr3;
  671. ch[t5+=t0]=tr1+tr4;
  672. t1+=ido;
  673. t3+=t2;
  674. }
  675. if(ido<2)return;
  676. if(ido==2)goto L105;
  677. t1=0;
  678. for(k=0;k<l1;k++){
  679. t5=(t4=(t3=(t2=t1<<2)+t6))+t6;
  680. t7=t1;
  681. for(i=2;i<ido;i+=2){
  682. t2+=2;
  683. t3+=2;
  684. t4-=2;
  685. t5-=2;
  686. t7+=2;
  687. ti1=cc[t2]+cc[t5];
  688. ti2=cc[t2]-cc[t5];
  689. ti3=cc[t3]-cc[t4];
  690. tr4=cc[t3]+cc[t4];
  691. tr1=cc[t2-1]-cc[t5-1];
  692. tr2=cc[t2-1]+cc[t5-1];
  693. ti4=cc[t3-1]-cc[t4-1];
  694. tr3=cc[t3-1]+cc[t4-1];
  695. ch[t7-1]=tr2+tr3;
  696. cr3=tr2-tr3;
  697. ch[t7]=ti2+ti3;
  698. ci3=ti2-ti3;
  699. cr2=tr1-tr4;
  700. cr4=tr1+tr4;
  701. ci2=ti1+ti4;
  702. ci4=ti1-ti4;
  703. ch[(t8=t7+t0)-1]=wa1[i-2]*cr2-wa1[i-1]*ci2;
  704. ch[t8]=wa1[i-2]*ci2+wa1[i-1]*cr2;
  705. ch[(t8+=t0)-1]=wa2[i-2]*cr3-wa2[i-1]*ci3;
  706. ch[t8]=wa2[i-2]*ci3+wa2[i-1]*cr3;
  707. ch[(t8+=t0)-1]=wa3[i-2]*cr4-wa3[i-1]*ci4;
  708. ch[t8]=wa3[i-2]*ci4+wa3[i-1]*cr4;
  709. }
  710. t1+=ido;
  711. }
  712. if(ido%2 == 1)return;
  713. L105:
  714. t1=ido;
  715. t2=ido<<2;
  716. t3=ido-1;
  717. t4=ido+(ido<<1);
  718. for(k=0;k<l1;k++){
  719. t5=t3;
  720. ti1=cc[t1]+cc[t4];
  721. ti2=cc[t4]-cc[t1];
  722. tr1=cc[t1-1]-cc[t4-1];
  723. tr2=cc[t1-1]+cc[t4-1];
  724. ch[t5]=tr2+tr2;
  725. ch[t5+=t0]=sqrt2*(tr1-ti1);
  726. ch[t5+=t0]=ti2+ti2;
  727. ch[t5+=t0]=-sqrt2*(tr1+ti1);
  728. t3+=ido;
  729. t1+=t2;
  730. t4+=t2;
  731. }
  732. }
  733. static void dradbg(int ido,int ip,int l1,int idl1,float *cc,float *c1,
  734. float *c2,float *ch,float *ch2,float *wa){
  735. static float tpi=6.28318530717958647692528676655900577;
  736. int idij,ipph,i,j,k,l,ik,is,t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,
  737. t11,t12;
  738. float dc2,ai1,ai2,ar1,ar2,ds2;
  739. int nbd;
  740. float dcp,arg,dsp,ar1h,ar2h;
  741. int ipp2;
  742. t10=ip*ido;
  743. t0=l1*ido;
  744. arg=tpi/(float)ip;
  745. dcp=cos(arg);
  746. dsp=sin(arg);
  747. nbd=(ido-1)>>1;
  748. ipp2=ip;
  749. ipph=(ip+1)>>1;
  750. if(ido<l1)goto L103;
  751. t1=0;
  752. t2=0;
  753. for(k=0;k<l1;k++){
  754. t3=t1;
  755. t4=t2;
  756. for(i=0;i<ido;i++){
  757. ch[t3]=cc[t4];
  758. t3++;
  759. t4++;
  760. }
  761. t1+=ido;
  762. t2+=t10;
  763. }
  764. goto L106;
  765. L103:
  766. t1=0;
  767. for(i=0;i<ido;i++){
  768. t2=t1;
  769. t3=t1;
  770. for(k=0;k<l1;k++){
  771. ch[t2]=cc[t3];
  772. t2+=ido;
  773. t3+=t10;
  774. }
  775. t1++;
  776. }
  777. L106:
  778. t1=0;
  779. t2=ipp2*t0;
  780. t7=(t5=ido<<1);
  781. for(j=1;j<ipph;j++){
  782. t1+=t0;
  783. t2-=t0;
  784. t3=t1;
  785. t4=t2;
  786. t6=t5;
  787. for(k=0;k<l1;k++){
  788. ch[t3]=cc[t6-1]+cc[t6-1];
  789. ch[t4]=cc[t6]+cc[t6];
  790. t3+=ido;
  791. t4+=ido;
  792. t6+=t10;
  793. }
  794. t5+=t7;
  795. }
  796. if (ido == 1)goto L116;
  797. if(nbd<l1)goto L112;
  798. t1=0;
  799. t2=ipp2*t0;
  800. t7=0;
  801. for(j=1;j<ipph;j++){
  802. t1+=t0;
  803. t2-=t0;
  804. t3=t1;
  805. t4=t2;
  806. t7+=(ido<<1);
  807. t8=t7;
  808. for(k=0;k<l1;k++){
  809. t5=t3;
  810. t6=t4;
  811. t9=t8;
  812. t11=t8;
  813. for(i=2;i<ido;i+=2){
  814. t5+=2;
  815. t6+=2;
  816. t9+=2;
  817. t11-=2;
  818. ch[t5-1]=cc[t9-1]+cc[t11-1];
  819. ch[t6-1]=cc[t9-1]-cc[t11-1];
  820. ch[t5]=cc[t9]-cc[t11];
  821. ch[t6]=cc[t9]+cc[t11];
  822. }
  823. t3+=ido;
  824. t4+=ido;
  825. t8+=t10;
  826. }
  827. }
  828. goto L116;
  829. L112:
  830. t1=0;
  831. t2=ipp2*t0;
  832. t7=0;
  833. for(j=1;j<ipph;j++){
  834. t1+=t0;
  835. t2-=t0;
  836. t3=t1;
  837. t4=t2;
  838. t7+=(ido<<1);
  839. t8=t7;
  840. t9=t7;
  841. for(i=2;i<ido;i+=2){
  842. t3+=2;
  843. t4+=2;
  844. t8+=2;
  845. t9-=2;
  846. t5=t3;
  847. t6=t4;
  848. t11=t8;
  849. t12=t9;
  850. for(k=0;k<l1;k++){
  851. ch[t5-1]=cc[t11-1]+cc[t12-1];
  852. ch[t6-1]=cc[t11-1]-cc[t12-1];
  853. ch[t5]=cc[t11]-cc[t12];
  854. ch[t6]=cc[t11]+cc[t12];
  855. t5+=ido;
  856. t6+=ido;
  857. t11+=t10;
  858. t12+=t10;
  859. }
  860. }
  861. }
  862. L116:
  863. ar1=1.;
  864. ai1=0.;
  865. t1=0;
  866. t9=(t2=ipp2*idl1);
  867. t3=(ip-1)*idl1;
  868. for(l=1;l<ipph;l++){
  869. t1+=idl1;
  870. t2-=idl1;
  871. ar1h=dcp*ar1-dsp*ai1;
  872. ai1=dcp*ai1+dsp*ar1;
  873. ar1=ar1h;
  874. t4=t1;
  875. t5=t2;
  876. t6=0;
  877. t7=idl1;
  878. t8=t3;
  879. for(ik=0;ik<idl1;ik++){
  880. c2[t4++]=ch2[t6++]+ar1*ch2[t7++];
  881. c2[t5++]=ai1*ch2[t8++];
  882. }
  883. dc2=ar1;
  884. ds2=ai1;
  885. ar2=ar1;
  886. ai2=ai1;
  887. t6=idl1;
  888. t7=t9-idl1;
  889. for(j=2;j<ipph;j++){
  890. t6+=idl1;
  891. t7-=idl1;
  892. ar2h=dc2*ar2-ds2*ai2;
  893. ai2=dc2*ai2+ds2*ar2;
  894. ar2=ar2h;
  895. t4=t1;
  896. t5=t2;
  897. t11=t6;
  898. t12=t7;
  899. for(ik=0;ik<idl1;ik++){
  900. c2[t4++]+=ar2*ch2[t11++];
  901. c2[t5++]+=ai2*ch2[t12++];
  902. }
  903. }
  904. }
  905. t1=0;
  906. for(j=1;j<ipph;j++){
  907. t1+=idl1;
  908. t2=t1;
  909. for(ik=0;ik<idl1;ik++)ch2[ik]+=ch2[t2++];
  910. }
  911. t1=0;
  912. t2=ipp2*t0;
  913. for(j=1;j<ipph;j++){
  914. t1+=t0;
  915. t2-=t0;
  916. t3=t1;
  917. t4=t2;
  918. for(k=0;k<l1;k++){
  919. ch[t3]=c1[t3]-c1[t4];
  920. ch[t4]=c1[t3]+c1[t4];
  921. t3+=ido;
  922. t4+=ido;
  923. }
  924. }
  925. if(ido==1)goto L132;
  926. if(nbd<l1)goto L128;
  927. t1=0;
  928. t2=ipp2*t0;
  929. for(j=1;j<ipph;j++){
  930. t1+=t0;
  931. t2-=t0;
  932. t3=t1;
  933. t4=t2;
  934. for(k=0;k<l1;k++){
  935. t5=t3;
  936. t6=t4;
  937. for(i=2;i<ido;i+=2){
  938. t5+=2;
  939. t6+=2;
  940. ch[t5-1]=c1[t5-1]-c1[t6];
  941. ch[t6-1]=c1[t5-1]+c1[t6];
  942. ch[t5]=c1[t5]+c1[t6-1];
  943. ch[t6]=c1[t5]-c1[t6-1];
  944. }
  945. t3+=ido;
  946. t4+=ido;
  947. }
  948. }
  949. goto L132;
  950. L128:
  951. t1=0;
  952. t2=ipp2*t0;
  953. for(j=1;j<ipph;j++){
  954. t1+=t0;
  955. t2-=t0;
  956. t3=t1;
  957. t4=t2;
  958. for(i=2;i<ido;i+=2){
  959. t3+=2;
  960. t4+=2;
  961. t5=t3;
  962. t6=t4;
  963. for(k=0;k<l1;k++){
  964. ch[t5-1]=c1[t5-1]-c1[t6];
  965. ch[t6-1]=c1[t5-1]+c1[t6];
  966. ch[t5]=c1[t5]+c1[t6-1];
  967. ch[t6]=c1[t5]-c1[t6-1];
  968. t5+=ido;
  969. t6+=ido;
  970. }
  971. }
  972. }
  973. L132:
  974. if(ido==1)return;
  975. for(ik=0;ik<idl1;ik++)c2[ik]=ch2[ik];
  976. t1=0;
  977. for(j=1;j<ip;j++){
  978. t2=(t1+=t0);
  979. for(k=0;k<l1;k++){
  980. c1[t2]=ch[t2];
  981. t2+=ido;
  982. }
  983. }
  984. if(nbd>l1)goto L139;
  985. is= -ido-1;
  986. t1=0;
  987. for(j=1;j<ip;j++){
  988. is+=ido;
  989. t1+=t0;
  990. idij=is;
  991. t2=t1;
  992. for(i=2;i<ido;i+=2){
  993. t2+=2;
  994. idij+=2;
  995. t3=t2;
  996. for(k=0;k<l1;k++){
  997. c1[t3-1]=wa[idij-1]*ch[t3-1]-wa[idij]*ch[t3];
  998. c1[t3]=wa[idij-1]*ch[t3]+wa[idij]*ch[t3-1];
  999. t3+=ido;
  1000. }
  1001. }
  1002. }
  1003. return;
  1004. L139:
  1005. is= -ido-1;
  1006. t1=0;
  1007. for(j=1;j<ip;j++){
  1008. is+=ido;
  1009. t1+=t0;
  1010. t2=t1;
  1011. for(k=0;k<l1;k++){
  1012. idij=is;
  1013. t3=t2;
  1014. for(i=2;i<ido;i+=2){
  1015. idij+=2;
  1016. t3+=2;
  1017. c1[t3-1]=wa[idij-1]*ch[t3-1]-wa[idij]*ch[t3];
  1018. c1[t3]=wa[idij-1]*ch[t3]+wa[idij]*ch[t3-1];
  1019. }
  1020. t2+=ido;
  1021. }
  1022. }
  1023. }
  1024. static void drftb1(int n, float *c, float *ch, float *wa, int *ifac){
  1025. int i,k1,l1,l2;
  1026. int na;
  1027. int nf,ip,iw,ix2,ix3,ido,idl1;
  1028. nf=ifac[1];
  1029. na=0;
  1030. l1=1;
  1031. iw=1;
  1032. for(k1=0;k1<nf;k1++){
  1033. ip=ifac[k1 + 2];
  1034. l2=ip*l1;
  1035. ido=n/l2;
  1036. idl1=ido*l1;
  1037. if(ip!=4)goto L103;
  1038. ix2=iw+ido;
  1039. ix3=ix2+ido;
  1040. if(na!=0)
  1041. dradb4(ido,l1,ch,c,wa+iw-1,wa+ix2-1,wa+ix3-1);
  1042. else
  1043. dradb4(ido,l1,c,ch,wa+iw-1,wa+ix2-1,wa+ix3-1);
  1044. na=1-na;
  1045. goto L115;
  1046. L103:
  1047. if(ip!=2)goto L106;
  1048. if(na!=0)
  1049. dradb2(ido,l1,ch,c,wa+iw-1);
  1050. else
  1051. dradb2(ido,l1,c,ch,wa+iw-1);
  1052. na=1-na;
  1053. goto L115;
  1054. L106:
  1055. if(ip!=3)goto L109;
  1056. ix2=iw+ido;
  1057. if(na!=0)
  1058. dradb3(ido,l1,ch,c,wa+iw-1,wa+ix2-1);
  1059. else
  1060. dradb3(ido,l1,c,ch,wa+iw-1,wa+ix2-1);
  1061. na=1-na;
  1062. goto L115;
  1063. L109:
  1064. /* The radix five case can be translated later..... */
  1065. /* if(ip!=5)goto L112;
  1066. ix2=iw+ido;
  1067. ix3=ix2+ido;
  1068. ix4=ix3+ido;
  1069. if(na!=0)
  1070. dradb5(ido,l1,ch,c,wa+iw-1,wa+ix2-1,wa+ix3-1,wa+ix4-1);
  1071. else
  1072. dradb5(ido,l1,c,ch,wa+iw-1,wa+ix2-1,wa+ix3-1,wa+ix4-1);
  1073. na=1-na;
  1074. goto L115;
  1075. L112:*/
  1076. if(na!=0)
  1077. dradbg(ido,ip,l1,idl1,ch,ch,ch,c,c,wa+iw-1);
  1078. else
  1079. dradbg(ido,ip,l1,idl1,c,c,c,ch,ch,wa+iw-1);
  1080. if(ido==1)na=1-na;
  1081. L115:
  1082. l1=l2;
  1083. iw+=(ip-1)*ido;
  1084. }
  1085. if(na==0)return;
  1086. for(i=0;i<n;i++)c[i]=ch[i];
  1087. }
  1088. void drft_forward(drft_lookup *l,float *data){
  1089. if(l->n==1)return;
  1090. drftf1(l->n,data,l->trigcache,l->trigcache+l->n,l->splitcache);
  1091. }
  1092. void drft_backward(drft_lookup *l,float *data){
  1093. if (l->n==1)return;
  1094. drftb1(l->n,data,l->trigcache,l->trigcache+l->n,l->splitcache);
  1095. }
  1096. void drft_init(drft_lookup *l,int n){
  1097. l->n=n;
  1098. l->trigcache=_ogg_calloc(3*n,sizeof(float));
  1099. l->splitcache=_ogg_calloc(32,sizeof(int));
  1100. fdrffti(n, l->trigcache, l->splitcache);
  1101. }
  1102. void drft_clear(drft_lookup *l){
  1103. if(l){
  1104. if(l->trigcache)free(l->trigcache);
  1105. if(l->splitcache)free(l->splitcache);
  1106. memset(l,0,sizeof(drft_lookup));
  1107. }
  1108. }