2 * FFT/IFFT transforms converted to integer precision
3 * Copyright (c) 2010 Dave Hooper, Mohamed Tarek, Michael Giacomelli
4 * Copyright (c) 2008 Loren Merritt
5 * Copyright (c) 2002 Fabrice Bellard
6 * Partly based on libdjbfft by D. J. Bernstein
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * @file libavcodec/fft.c
27 * FFT/IFFT transforms.
32 // we definitely want CONFIG_SMALL undefined for ipod
33 // so we get the inlined version of fft16 (which is measurably faster)
45 #include <codecs/lib/codeclib.h>
47 #include "codeclib_misc.h"
48 #include "mdct_lookup.h"
50 /* constants for fft_16 (same constants as in mdct_arm.S ... ) */
51 #define cPI1_8 (0x7641af3d) /* cos(pi/8) s.31 */
52 #define cPI2_8 (0x5a82799a) /* cos(2pi/8) = 1/sqrt(2) s.31 */
53 #define cPI3_8 (0x30fbc54d) /* cos(3pi/8) s.31 */
55 /* asm-optimised functions and/or macros */
56 #include "fft-ffmpeg_arm.h"
57 #include "fft-ffmpeg_cf.h"
59 #ifndef ICODE_ATTR_TREMOR_MDCT
60 #define ICODE_ATTR_TREMOR_MDCT ICODE_ATTR
64 static int split_radix_permutation(int i
, int n
, int inverse
)
67 if(n
<= 2) return i
&1;
69 if(!(i
&m
)) return split_radix_permutation(i
, m
, inverse
)*2;
71 if(inverse
== !(i
&m
)) return split_radix_permutation(i
, m
, inverse
)*4 + 1;
72 else return split_radix_permutation(i
, m
, inverse
)*4 - 1;
75 static void ff_fft_permute_c(FFTContext
*s
, FFTComplex
*z
)
79 //const uint16_t *revtab = s->revtab;
82 const int revtab_shift
= (12 - s
->nbits
);
86 k
= revtab
[j
]>>revtab_shift
;
96 #define BF(x,y,a,b) {\
101 #define BF_REV(x,y,a,b) {\
106 #ifndef FFT_FFMPEG_INCL_OPTIMISED_BUTTERFLIES
107 #define BUTTERFLIES(a0,a1,a2,a3) {\
109 FFTSample temp1,temp2;\
110 BF(temp1, temp2, t5, t1);\
111 BF(a2.re, a0.re, a0.re, temp2);\
112 BF(a3.im, a1.im, a1.im, temp1);\
115 FFTSample temp1,temp2;\
116 BF(temp1, temp2, t2, t6);\
117 BF(a3.re, a1.re, a1.re, temp1);\
118 BF(a2.im, a0.im, a0.im, temp2);\
122 // force loading all the inputs before storing any.
123 // this is slightly slower for small data, but avoids store->load aliasing
124 // for addresses separated by large powers of 2.
125 #define BUTTERFLIES_BIG(a0,a1,a2,a3) {\
126 FFTSample r0=a0.re, i0=a0.im, r1=a1.re, i1=a1.im;\
128 FFTSample temp1, temp2;\
129 BF(temp1, temp2, t5, t1);\
130 BF(a2.re, a0.re, r0, temp2);\
131 BF(a3.im, a1.im, i1, temp1);\
134 FFTSample temp1, temp2;\
135 BF(temp1, temp2, t2, t6);\
136 BF(a3.re, a1.re, r1, temp1);\
137 BF(a2.im, a0.im, i0, temp2);\
143 see conjugate pair description in
144 http://www.fftw.org/newsplit.pdf
152 y[k] = z[k]+w(z[k+2N/4])+w'(z[k+3N/4])
153 y[k+N/4] = z[k+N/4]-iw(z[k+2N/4])+iw'(z[k+3N/4])
154 y[k+2N/4] = z[k]-w(z[k+2N/4])-w'(z[k+3N/4])
155 y[k+3N/4] = z[k+N/4]+iw(z[k+2N/4])-iw'(z[k+3N/4])
159 a0 = a0 + (w.a2 + w'.a3)
160 a1 = a1 - i(w.a2 - w'.a3)
161 a2 = a0 - (w.a2 + w'.a3)
162 a3 = a1 + i(w.a2 - w'.a3)
164 note re(w') = re(w) and im(w') = -im(w)
168 re(a0) = re(a0) + re(w.a2) + re(w.a3)
169 im(a0) = im(a0) + im(w.a2) - im(w.a3) etc
171 and remember also that
172 Re([s+it][u+iv]) = su-tv
173 Im([s+it][u+iv]) = sv+tu
176 Re(w'.(s+it)) = Re(w').s - Im(w').t = Re(w).s + Im(w).t
177 Im(w'.(s+it)) = Re(w').t + Im(w').s = Re(w).t - Im(w).s
179 For inverse dft we take the complex conjugate of all twiddle factors.
182 a0 = a0 + (w'.a2 + w.a3)
183 a1 = a1 - i(w'.a2 - w.a3)
184 a2 = a0 - (w'.a2 + w.a3)
185 a3 = a1 + i(w'.a2 - w.a3)
187 Define t1 = Re(w'.a2) = Re(w)*Re(a2) + Im(w)*Im(a2)
188 t2 = Im(w'.a2) = Re(w)*Im(a2) - Im(w)*Re(a2)
189 t5 = Re(w.a3) = Re(w)*Re(a3) - Im(w)*Im(a3)
190 t6 = Im(w.a3) = Re(w)*Im(a3) + Im(w)*Re(a3)
193 a0.re = a0.re + ( t1 + t5 )
194 a0.im = a0.im + ( t2 + t6 )
195 a1.re = a1.re + ( t2 - t6 ) // since we multiply by -i and i(-i) = 1
196 a1.im = a1.im - ( t1 - t5 ) // since we multiply by -i and 1(-i) = -i
197 a2.re = a0.re - ( t1 + t5 )
198 a2.im = a0.im - ( t1 + t5 )
199 a3.re = a1.re - ( t2 - t6 ) // since we multiply by +i and i(+i) = -1
200 a3.im = a1.im + ( t1 - t5 ) // since we multiply by +i and 1(+i) = i
205 #ifndef FFT_FFMPEG_INCL_OPTIMISED_TRANSFORM
206 static inline FFTComplex
* TRANSFORM(FFTComplex
* z
, unsigned int n
, FFTSample wre
, FFTSample wim
)
208 register FFTSample t1
,t2
,t5
,t6
,r_re
,r_im
;
211 XPROD31_R(r_re
, r_im
, wre
, wim
, t1
,t2
);
214 XNPROD31_R(r_re
, r_im
, wre
, wim
, t5
,t6
);
215 BUTTERFLIES(z
[0],z
[n
],z
[n
*2],z
[n
*3]);
219 static inline FFTComplex
* TRANSFORM_W01(FFTComplex
* z
, unsigned int n
, const FFTSample
* w
)
221 register const FFTSample wre
=w
[0],wim
=w
[1];
222 register FFTSample t1
,t2
,t5
,t6
,r_re
,r_im
;
225 XPROD31_R(r_re
, r_im
, wre
, wim
, t1
,t2
);
228 XNPROD31_R(r_re
, r_im
, wre
, wim
, t5
,t6
);
229 BUTTERFLIES(z
[0],z
[n
],z
[n
*2],z
[n
*3]);
233 static inline FFTComplex
* TRANSFORM_W10(FFTComplex
* z
, unsigned int n
, const FFTSample
* w
)
235 register const FFTSample wim
=w
[0],wre
=w
[1];
236 register FFTSample t1
,t2
,t5
,t6
,r_re
,r_im
;
239 XPROD31_R(r_re
, r_im
, wre
, wim
, t1
,t2
);
242 XNPROD31_R(r_re
, r_im
, wre
, wim
, t5
,t6
);
243 BUTTERFLIES(z
[0],z
[n
],z
[n
*2],z
[n
*3]);
247 static inline FFTComplex
* TRANSFORM_EQUAL(FFTComplex
* z
, unsigned int n
)
249 register FFTSample t1
,t2
,t5
,t6
,temp1
,temp2
;
250 register FFTSample
* my_z
= (FFTSample
*)(z
);
252 t2
= MULT31(my_z
[0], cPI2_8
);
253 temp1
= MULT31(my_z
[1], cPI2_8
);
255 temp2
= MULT31(my_z
[0], cPI2_8
);
256 t5
= MULT31(my_z
[1], cPI2_8
);
262 BUTTERFLIES(z
[0],z
[n
],z
[n
*2],z
[n
*3]);
266 static inline FFTComplex
* TRANSFORM_ZERO(FFTComplex
* z
, unsigned int n
)
268 FFTSample t1
,t2
,t5
,t6
;
273 BUTTERFLIES(z
[0],z
[n
],z
[n
*2],z
[n
*3]);
278 /* z[0...8n-1], w[1...2n-1] */
279 void pass(FFTComplex
*z_arg
, unsigned int STEP_arg
, unsigned int n_arg
) ICODE_ATTR_TREMOR_MDCT
;
280 void pass(FFTComplex
*z_arg
, unsigned int STEP_arg
, unsigned int n_arg
)
282 register FFTComplex
* z
= z_arg
;
283 register unsigned int STEP
= STEP_arg
;
284 register unsigned int n
= n_arg
;
286 register const FFTSample
*w
= sincos_lookup0
+STEP
;
287 /* wre = *(wim+1) . ordering is sin,cos */
288 register const FFTSample
*w_end
= sincos_lookup0
+1024;
290 /* first two are special (well, first one is special, but we need to do pairs) */
291 z
= TRANSFORM_ZERO(z
,n
);
292 z
= TRANSFORM_W10(z
,n
,w
);
294 /* first pass forwards through sincos_lookup0*/
296 z
= TRANSFORM_W10(z
,n
,w
);
298 z
= TRANSFORM_W10(z
,n
,w
);
300 } while(LIKELY(w
< w_end
));
301 /* second half: pass backwards through sincos_lookup0*/
302 /* wim and wre are now in opposite places so ordering now [0],[1] */
303 w_end
=sincos_lookup0
;
304 while(LIKELY(w
>w_end
))
306 z
= TRANSFORM_W01(z
,n
,w
);
308 z
= TRANSFORM_W01(z
,n
,w
);
314 sincos_lookup0 has sin,cos pairs for 1/4 cycle, in 1024 points
315 so half cycle would be 2048 points
316 ff_cos_16 has 8 elements corresponding to 4 cos points and 4 sin points
317 so each of the 4 points pairs corresponds to a 256*2-byte jump in sincos_lookup0
318 8192/16 (from "ff_cos_16") is 512 bytes.
319 i.e. for fft16, STEP = 8192/16 */
320 #define DECL_FFT(n,n2,n4)\
321 void fft##n(FFTComplex *z) ICODE_ATTR_TREMOR_MDCT;\
322 void fft##n(FFTComplex *z)\
330 #ifndef FFT_FFMPEG_INCL_OPTIMISED_FFT4
331 static inline void fft4(FFTComplex
*z
)
333 FFTSample t1
, t2
, t3
, t4
, t5
, t6
, t7
, t8
;
335 BF(t3
, t1
, z
[0].re
, z
[1].re
); // t3=r1-r3 ; t1 = r1+r3
336 BF(t8
, t6
, z
[3].re
, z
[2].re
); // t8=r7-r5 ; t6 = r7+r5
338 BF(z
[2].re
, z
[0].re
, t1
, t6
); // r5=t1-t6 ; r1 = t1+t6
340 BF(t4
, t2
, z
[0].im
, z
[1].im
); // t4=r2-r4 ; t2 = r2+r4
341 BF(t7
, t5
, z
[2].im
, z
[3].im
); // t7=r6-r8 ; t5 = r6+r8
343 BF(z
[3].im
, z
[1].im
, t4
, t8
); // r8=t4-t8 ; r4 = t4+t8
344 BF(z
[3].re
, z
[1].re
, t3
, t7
); // r7=t3-t7 ; r3 = t3+t7
345 BF(z
[2].im
, z
[0].im
, t2
, t5
); // r6=t2-t5 ; r2 = t2+t5
349 static void fft4_dispatch(FFTComplex
*z
)
354 #ifndef FFT_FFMPEG_INCL_OPTIMISED_FFT8
355 static inline void fft8(FFTComplex
*z
)
358 FFTSample t1
,t2
,t3
,t4
,t7
,t8
;
360 BF(t1
, z
[5].re
, z
[4].re
, -z
[5].re
);
361 BF(t2
, z
[5].im
, z
[4].im
, -z
[5].im
);
362 BF(t3
, z
[7].re
, z
[6].re
, -z
[7].re
);
363 BF(t4
, z
[7].im
, z
[6].im
, -z
[7].im
);
366 BF(z
[4].re
, z
[0].re
, z
[0].re
, t1
);
367 BF(z
[4].im
, z
[0].im
, z
[0].im
, t2
);
368 BF(z
[6].re
, z
[2].re
, z
[2].re
, t7
);
369 BF(z
[6].im
, z
[2].im
, z
[2].im
, t8
);
372 TRANSFORM_EQUAL(z
,2);
376 static void fft8_dispatch(FFTComplex
*z
)
382 void fft16(FFTComplex
*z
) ICODE_ATTR_TREMOR_MDCT
;
383 void fft16(FFTComplex
*z
)
391 TRANSFORM_EQUAL(z
,4);
393 TRANSFORM(z
,4,cPI1_8
,cPI3_8
);
395 TRANSFORM(z
,4,cPI3_8
,cPI1_8
);
404 DECL_FFT(512,256,128)
405 DECL_FFT(1024,512,256)
406 DECL_FFT(2048,1024,512)
407 DECL_FFT(4096,2048,1024)
409 static void (*fft_dispatch
[])(FFTComplex
*) = {
410 fft4_dispatch
, fft8_dispatch
, fft16
, fft32
, fft64
, fft128
, fft256
, fft512
, fft1024
,
414 void ff_fft_calc_c(int nbits
, FFTComplex
*z
)
416 fft_dispatch
[nbits
-2](z
);
423 #define FFT_SIZE 1024
424 #define ftofix32(x) ((fixed32)((x) * (float)(1 << PRECISION) + ((x) < 0 ? -0.5 : 0.5)))
425 #define itofix32(x) ((x) << PRECISION)
426 #define fixtoi32(x) ((x) >> PRECISION)
429 const long N
= FFT_SIZE
;
430 double r
[FFT_SIZE
] = {0.0}, i
[FFT_SIZE
] = {0.0};
435 double exec_time
= 0;
437 FFTComplex z
[FFT_SIZE
];
438 memset(z
, 0, 64*sizeof(FFTComplex
));
440 /* Generate saw-tooth test data */
441 for (n
= 0; n
< FFT_SIZE
; n
++)
443 t
= (2 * M_PI
* n
)/N
;
444 /*z[n].re = 1.1 + sin( t) +
446 (1.0/3.0) * sin(3.0 * t) +
447 0.25 * sin(4.0 * t) +
449 (1.0/6.0) * sin(6.0 * t) +
450 (1.0/7.0) * sin(7.0 * t) ;*/
451 z
[n
].re
= ftofix32(cos(2*M_PI
*n
/64));
452 //printf("z[%d] = %f\n", n, z[n].re);
456 ff_fft_init(&s
, 10, 1);
458 //for(n = 0; n < 1000000; n++)
459 ff_fft_permute_c(&s
, z
);
460 ff_fft_calc_c(&s
, z
);
462 //exec_time = (((double)end-(double)start)/CLOCKS_PER_SEC);
463 for(j
= 0; j
< FFT_SIZE
; j
++)
465 printf("%8.4f\n", sqrt(pow(fixtof32(z
[j
].re
),2)+ pow(fixtof32(z
[j
].im
), 2)));
468 printf("muls = %d, adds = %d\n", muls
, adds
);
469 //printf(" Time elapsed = %f\n", exec_time);