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>
48 #include "asm_mcf5249.h"
49 #include "codeclib_misc.h"
50 #include "mdct_lookup.h"
52 /* constants for fft_16 (same constants as in mdct_arm.S ... ) */
53 #define cPI1_8 (0x7641af3d) /* cos(pi/8) s.31 */
54 #define cPI2_8 (0x5a82799a) /* cos(2pi/8) = 1/sqrt(2) s.31 */
55 #define cPI3_8 (0x30fbc54d) /* cos(3pi/8) s.31 */
57 /* asm-optimised functions and/or macros */
58 #include "fft-ffmpeg_arm.h"
60 #ifndef ICODE_ATTR_TREMOR_MDCT
61 #define ICODE_ATTR_TREMOR_MDCT ICODE_ATTR
65 static int split_radix_permutation(int i
, int n
, int inverse
)
68 if(n
<= 2) return i
&1;
70 if(!(i
&m
)) return split_radix_permutation(i
, m
, inverse
)*2;
72 if(inverse
== !(i
&m
)) return split_radix_permutation(i
, m
, inverse
)*4 + 1;
73 else return split_radix_permutation(i
, m
, inverse
)*4 - 1;
76 static void ff_fft_permute_c(FFTContext
*s
, FFTComplex
*z
)
80 //const uint16_t *revtab = s->revtab;
83 const int revtab_shift
= (12 - s
->nbits
);
87 k
= revtab
[j
]>>revtab_shift
;
97 #define BF(x,y,a,b) {\
102 #define BF_REV(x,y,a,b) {\
107 #ifndef FFT_FFMPEG_INCL_OPTIMISED_BUTTERFLIES
108 #define BUTTERFLIES(a0,a1,a2,a3) {\
110 FFTSample temp1,temp2;\
111 BF(temp1, temp2, t5, t1);\
112 BF(a2.re, a0.re, a0.re, temp2);\
113 BF(a3.im, a1.im, a1.im, temp1);\
116 FFTSample temp1,temp2;\
117 BF(temp1, temp2, t2, t6);\
118 BF(a3.re, a1.re, a1.re, temp1);\
119 BF(a2.im, a0.im, a0.im, temp2);\
123 // force loading all the inputs before storing any.
124 // this is slightly slower for small data, but avoids store->load aliasing
125 // for addresses separated by large powers of 2.
126 #define BUTTERFLIES_BIG(a0,a1,a2,a3) {\
127 FFTSample r0=a0.re, i0=a0.im, r1=a1.re, i1=a1.im;\
129 FFTSample temp1, temp2;\
130 BF(temp1, temp2, t5, t1);\
131 BF(a2.re, a0.re, r0, temp2);\
132 BF(a3.im, a1.im, i1, temp1);\
135 FFTSample temp1, temp2;\
136 BF(temp1, temp2, t2, t6);\
137 BF(a3.re, a1.re, r1, temp1);\
138 BF(a2.im, a0.im, i0, temp2);\
144 see conjugate pair description in
145 http://www.fftw.org/newsplit.pdf
153 y[k] = z[k]+w(z[k+2N/4])+w'(z[k+3N/4])
154 y[k+N/4] = z[k+N/4]-iw(z[k+2N/4])+iw'(z[k+3N/4])
155 y[k+2N/4] = z[k]-w(z[k+2N/4])-w'(z[k+3N/4])
156 y[k+3N/4] = z[k+N/4]+iw(z[k+2N/4])-iw'(z[k+3N/4])
160 a0 = a0 + (w.a2 + w'.a3)
161 a1 = a1 - i(w.a2 - w'.a3)
162 a2 = a0 - (w.a2 + w'.a3)
163 a3 = a1 + i(w.a2 - w'.a3)
165 note re(w') = re(w) and im(w') = -im(w)
169 re(a0) = re(a0) + re(w.a2) + re(w.a3)
170 im(a0) = im(a0) + im(w.a2) - im(w.a3) etc
172 and remember also that
173 Re([s+it][u+iv]) = su-tv
174 Im([s+it][u+iv]) = sv+tu
177 Re(w'.(s+it)) = Re(w').s - Im(w').t = Re(w).s + Im(w).t
178 Im(w'.(s+it)) = Re(w').t + Im(w').s = Re(w).t - Im(w).s
180 For inverse dft we take the complex conjugate of all twiddle factors.
183 a0 = a0 + (w'.a2 + w.a3)
184 a1 = a1 - i(w'.a2 - w.a3)
185 a2 = a0 - (w'.a2 + w.a3)
186 a3 = a1 + i(w'.a2 - w.a3)
188 Define t1 = Re(w'.a2) = Re(w)*Re(a2) + Im(w)*Im(a2)
189 t2 = Im(w'.a2) = Re(w)*Im(a2) - Im(w)*Re(a2)
190 t5 = Re(w.a3) = Re(w)*Re(a3) - Im(w)*Im(a3)
191 t6 = Im(w.a3) = Re(w)*Im(a3) + Im(w)*Re(a3)
194 a0.re = a0.re + ( t1 + t5 )
195 a0.im = a0.im + ( t2 + t6 )
196 a1.re = a1.re + ( t2 - t6 ) // since we multiply by -i and i(-i) = 1
197 a1.im = a1.im - ( t1 - t5 ) // since we multiply by -i and 1(-i) = -i
198 a2.re = a0.re - ( t1 + t5 )
199 a2.im = a0.im - ( t1 + t5 )
200 a3.re = a1.re - ( t2 - t6 ) // since we multiply by +i and i(+i) = -1
201 a3.im = a1.im + ( t1 - t5 ) // since we multiply by +i and 1(+i) = i
206 #ifndef FFT_FFMPEG_INCL_OPTIMISED_TRANSFORM
207 static inline void TRANSFORM(FFTComplex
* z
, unsigned int n
, FFTSample wre
, FFTSample wim
)
209 register FFTSample t1
,t2
,t5
,t6
,r_re
,r_im
;
212 XPROD31_R(r_re
, r_im
, wre
, wim
, t1
,t2
);
215 XNPROD31_R(r_re
, r_im
, wre
, wim
, t5
,t6
);
216 BUTTERFLIES(z
[0],z
[n
],z
[n
*2],z
[n
*3]);
219 static inline void 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]);
232 static inline void TRANSFORM_W10(FFTComplex
* z
, unsigned int n
, const FFTSample
* w
)
234 register const FFTSample wim
=w
[0],wre
=w
[1];
235 register FFTSample t1
,t2
,t5
,t6
,r_re
,r_im
;
238 XPROD31_R(r_re
, r_im
, wre
, wim
, t1
,t2
);
241 XNPROD31_R(r_re
, r_im
, wre
, wim
, t5
,t6
);
242 BUTTERFLIES(z
[0],z
[n
],z
[n
*2],z
[n
*3]);
245 static inline void TRANSFORM_EQUAL(FFTComplex
* z
, unsigned int n
)
247 register FFTSample t1
,t2
,t5
,t6
,temp1
,temp2
;
248 register FFTSample
* my_z
= (FFTSample
*)(z
);
250 t2
= MULT31(my_z
[0], cPI2_8
);
251 temp1
= MULT31(my_z
[1], cPI2_8
);
253 temp2
= MULT31(my_z
[0], cPI2_8
);
254 t5
= MULT31(my_z
[1], cPI2_8
);
260 BUTTERFLIES(z
[0],z
[n
],z
[n
*2],z
[n
*3]);
263 static inline void TRANSFORM_ZERO(FFTComplex
* z
, unsigned int n
)
265 FFTSample t1
,t2
,t5
,t6
;
270 BUTTERFLIES(z
[0],z
[n
],z
[n
*2],z
[n
*3]);
274 /* z[0...8n-1], w[1...2n-1] */
275 void pass(FFTComplex
*z_arg
, unsigned int STEP_arg
, unsigned int n_arg
) ICODE_ATTR_TREMOR_MDCT
;
276 void pass(FFTComplex
*z_arg
, unsigned int STEP_arg
, unsigned int n_arg
)
278 register FFTComplex
* z
= z_arg
;
279 register unsigned int STEP
= STEP_arg
;
280 register unsigned int n
= n_arg
;
282 register const FFTSample
*w
= sincos_lookup0
+STEP
;
283 /* wre = *(wim+1) . ordering is sin,cos */
284 register const FFTSample
*w_end
= sincos_lookup0
+1024;
286 /* first two are special (well, first one is special, but we need to do pairs) */
289 TRANSFORM_W10(z
,n
,w
);
291 /* first pass forwards through sincos_lookup0*/
294 TRANSFORM_W10(z
,n
,w
);
297 TRANSFORM_W10(z
,n
,w
);
299 } while(LIKELY(w
< w_end
));
300 /* second half: pass backwards through sincos_lookup0*/
301 /* wim and wre are now in opposite places so ordering now [0],[1] */
302 w_end
=sincos_lookup0
;
303 while(LIKELY(w
>w_end
))
306 TRANSFORM_W01(z
,n
,w
);
309 TRANSFORM_W01(z
,n
,w
);
315 sincos_lookup0 has sin,cos pairs for 1/4 cycle, in 1024 points
316 so half cycle would be 2048 points
317 ff_cos_16 has 8 elements corresponding to 4 cos points and 4 sin points
318 so each of the 4 points pairs corresponds to a 256*2-byte jump in sincos_lookup0
319 8192/16 (from "ff_cos_16") is 512 bytes.
320 i.e. for fft16, STEP = 8192/16 */
321 #define DECL_FFT(n,n2,n4)\
322 void fft##n(FFTComplex *z) ICODE_ATTR_TREMOR_MDCT;\
323 void fft##n(FFTComplex *z)\
331 #ifndef FFT_FFMPEG_INCL_OPTIMISED_FFT4
332 static inline void fft4(FFTComplex
*z
)
334 FFTSample t1
, t2
, t3
, t4
, t5
, t6
, t7
, t8
;
336 BF(t3
, t1
, z
[0].re
, z
[1].re
); // t3=r1-r3 ; t1 = r1+r3
337 BF(t8
, t6
, z
[3].re
, z
[2].re
); // t8=r7-r5 ; t6 = r7+r5
339 BF(z
[2].re
, z
[0].re
, t1
, t6
); // r5=t1-t6 ; r1 = t1+t6
341 BF(t4
, t2
, z
[0].im
, z
[1].im
); // t4=r2-r4 ; t2 = r2+r4
342 BF(t7
, t5
, z
[2].im
, z
[3].im
); // t7=r6-r8 ; t5 = r6+r8
344 BF(z
[3].im
, z
[1].im
, t4
, t8
); // r8=t4-t8 ; r4 = t4+t8
345 BF(z
[3].re
, z
[1].re
, t3
, t7
); // r7=t3-t7 ; r3 = t3+t7
346 BF(z
[2].im
, z
[0].im
, t2
, t5
); // r6=t2-t5 ; r2 = t2+t5
350 static void fft4_dispatch(FFTComplex
*z
)
355 #ifndef FFT_FFMPEG_INCL_OPTIMISED_FFT8
356 static inline void fft8(FFTComplex
*z
)
359 FFTSample t1
,t2
,t3
,t4
,t7
,t8
;
361 BF(t1
, z
[5].re
, z
[4].re
, -z
[5].re
);
362 BF(t2
, z
[5].im
, z
[4].im
, -z
[5].im
);
363 BF(t3
, z
[7].re
, z
[6].re
, -z
[7].re
);
364 BF(t4
, z
[7].im
, z
[6].im
, -z
[7].im
);
367 BF(z
[4].re
, z
[0].re
, z
[0].re
, t1
);
368 BF(z
[4].im
, z
[0].im
, z
[0].im
, t2
);
369 BF(z
[6].re
, z
[2].re
, z
[2].re
, t7
);
370 BF(z
[6].im
, z
[2].im
, z
[2].im
, t8
);
373 TRANSFORM_EQUAL(z
,2);
377 static void fft8_dispatch(FFTComplex
*z
)
383 void fft16(FFTComplex
*z
) ICODE_ATTR_TREMOR_MDCT
;
384 void fft16(FFTComplex
*z
)
392 TRANSFORM_EQUAL(z
,4);
394 TRANSFORM(z
,4,cPI1_8
,cPI3_8
);
396 TRANSFORM(z
,4,cPI3_8
,cPI1_8
);
405 DECL_FFT(512,256,128)
406 DECL_FFT(1024,512,256)
407 DECL_FFT(2048,1024,512)
408 DECL_FFT(4096,2048,1024)
410 static void (*fft_dispatch
[])(FFTComplex
*) = {
411 fft4_dispatch
, fft8_dispatch
, fft16
, fft32
, fft64
, fft128
, fft256
, fft512
, fft1024
,
415 void ff_fft_calc_c(int nbits
, FFTComplex
*z
)
417 fft_dispatch
[nbits
-2](z
);
424 #define FFT_SIZE 1024
425 #define ftofix32(x) ((fixed32)((x) * (float)(1 << PRECISION) + ((x) < 0 ? -0.5 : 0.5)))
426 #define itofix32(x) ((x) << PRECISION)
427 #define fixtoi32(x) ((x) >> PRECISION)
430 const long N
= FFT_SIZE
;
431 double r
[FFT_SIZE
] = {0.0}, i
[FFT_SIZE
] = {0.0};
436 double exec_time
= 0;
438 FFTComplex z
[FFT_SIZE
];
439 memset(z
, 0, 64*sizeof(FFTComplex
));
441 /* Generate saw-tooth test data */
442 for (n
= 0; n
< FFT_SIZE
; n
++)
444 t
= (2 * M_PI
* n
)/N
;
445 /*z[n].re = 1.1 + sin( t) +
447 (1.0/3.0) * sin(3.0 * t) +
448 0.25 * sin(4.0 * t) +
450 (1.0/6.0) * sin(6.0 * t) +
451 (1.0/7.0) * sin(7.0 * t) ;*/
452 z
[n
].re
= ftofix32(cos(2*M_PI
*n
/64));
453 //printf("z[%d] = %f\n", n, z[n].re);
457 ff_fft_init(&s
, 10, 1);
459 //for(n = 0; n < 1000000; n++)
460 ff_fft_permute_c(&s
, z
);
461 ff_fft_calc_c(&s
, z
);
463 //exec_time = (((double)end-(double)start)/CLOCKS_PER_SEC);
464 for(j
= 0; j
< FFT_SIZE
; j
++)
466 printf("%8.4f\n", sqrt(pow(fixtof32(z
[j
].re
),2)+ pow(fixtof32(z
[j
].im
), 2)));
469 printf("muls = %d, adds = %d\n", muls
, adds
);
470 //printf(" Time elapsed = %f\n", exec_time);