lcd-m6sp.c: remove \r
[kugel-rb.git] / apps / codecs / lib / fft-ffmpeg.c
blobd18f0481c0790a78a8ca7d1fa5d4789076916722
1 /*
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
25 /**
26 * @file libavcodec/fft.c
27 * FFT/IFFT transforms.
31 #ifdef CPU_ARM
32 // we definitely want CONFIG_SMALL undefined for ipod
33 // so we get the inlined version of fft16 (which is measurably faster)
34 #undef CONFIG_SMALL
35 #else
36 #undef CONFIG_SMALL
37 #endif
39 #include "fft.h"
40 #include <string.h>
41 #include <stdlib.h>
42 #include <math.h>
43 #include <inttypes.h>
44 #include <time.h>
45 #include <codecs/lib/codeclib.h>
47 #include "asm_arm.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
62 #endif
64 #if 0
65 static int split_radix_permutation(int i, int n, int inverse)
67 int m;
68 if(n <= 2) return i&1;
69 m = n >> 1;
70 if(!(i&m)) return split_radix_permutation(i, m, inverse)*2;
71 m >>= 1;
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)
78 int j, k, np;
79 FFTComplex tmp;
80 //const uint16_t *revtab = s->revtab;
81 np = 1 << s->nbits;
83 const int revtab_shift = (12 - s->nbits);
85 /* reverse */
86 for(j=0;j<np;j++) {
87 k = revtab[j]>>revtab_shift;
88 if (k < j) {
89 tmp = z[k];
90 z[k] = z[j];
91 z[j] = tmp;
95 #endif
97 #define BF(x,y,a,b) {\
98 x = a - b;\
99 y = a + b;\
102 #define BF_REV(x,y,a,b) {\
103 x = a + b;\
104 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);\
141 #endif
144 see conjugate pair description in
145 http://www.fftw.org/newsplit.pdf
147 a0 = z[k]
148 a1 = z[k+N/4]
149 a2 = z[k+2N/4]
150 a3 = z[k+3N/4]
152 result:
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])
158 i.e.
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)
167 so therefore
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.
181 Hence
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)
193 Then we just output:
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;
210 r_re = z[n*2].re;
211 r_im = z[n*2].im;
212 XPROD31_R(r_re, r_im, wre, wim, t1,t2);
213 r_re = z[n*3].re;
214 r_im = z[n*3].im;
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;
223 r_re = z[n*2].re;
224 r_im = z[n*2].im;
225 XPROD31_R(r_re, r_im, wre, wim, t1,t2);
226 r_re = z[n*3].re;
227 r_im = z[n*3].im;
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;
236 r_re = z[n*2].re;
237 r_im = z[n*2].im;
238 XPROD31_R(r_re, r_im, wre, wim, t1,t2);
239 r_re = z[n*3].re;
240 r_im = z[n*3].im;
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);
249 my_z += n*4;
250 t2 = MULT31(my_z[0], cPI2_8);
251 temp1 = MULT31(my_z[1], cPI2_8);
252 my_z += n*2;
253 temp2 = MULT31(my_z[0], cPI2_8);
254 t5 = MULT31(my_z[1], cPI2_8);
255 t1 = ( temp1 + t2 );
256 t2 = ( temp1 - t2 );
257 t6 = ( temp2 + t5 );
258 t5 = ( temp2 - t5 );
259 my_z -= n*6;
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;
266 t1 = z[n*2].re;
267 t2 = z[n*2].im;
268 t5 = z[n*3].re;
269 t6 = z[n*3].im;
270 BUTTERFLIES(z[0],z[n],z[n*2],z[n*3]);
272 #endif
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) */
287 TRANSFORM_ZERO(z,n);
288 z++;
289 TRANSFORM_W10(z,n,w);
290 w += STEP;
291 /* first pass forwards through sincos_lookup0*/
292 do {
293 z++;
294 TRANSFORM_W10(z,n,w);
295 w += STEP;
296 z++;
297 TRANSFORM_W10(z,n,w);
298 w += STEP;
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))
305 z++;
306 TRANSFORM_W01(z,n,w);
307 w -= STEP;
308 z++;
309 TRANSFORM_W01(z,n,w);
310 w -= STEP;
314 /* what is STEP?
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)\
325 fft##n2(z);\
326 fft##n4(z+n4*2);\
327 fft##n4(z+n4*3);\
328 pass(z,8192/n,n4);\
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
348 #endif
350 static void fft4_dispatch(FFTComplex *z)
352 fft4(z);
355 #ifndef FFT_FFMPEG_INCL_OPTIMISED_FFT8
356 static inline void fft8(FFTComplex *z)
358 fft4(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);
365 BF(t8, t1, t3, t1);
366 BF(t7, t2, t2, t4);
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);
372 z++;
373 TRANSFORM_EQUAL(z,2);
375 #endif
377 static void fft8_dispatch(FFTComplex *z)
379 fft8(z);
382 #ifndef CONFIG_SMALL
383 void fft16(FFTComplex *z) ICODE_ATTR_TREMOR_MDCT;
384 void fft16(FFTComplex *z)
386 fft8(z);
387 fft4(z+8);
388 fft4(z+12);
390 TRANSFORM_ZERO(z,4);
391 z+=2;
392 TRANSFORM_EQUAL(z,4);
393 z-=1;
394 TRANSFORM(z,4,cPI1_8,cPI3_8);
395 z+=2;
396 TRANSFORM(z,4,cPI3_8,cPI1_8);
398 #else
399 DECL_FFT(16,8,4)
400 #endif
401 DECL_FFT(32,16,8)
402 DECL_FFT(64,32,16)
403 DECL_FFT(128,64,32)
404 DECL_FFT(256,128,64)
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,
412 fft2048, fft4096
415 void ff_fft_calc_c(int nbits, FFTComplex *z)
417 fft_dispatch[nbits-2](z);
420 #if 0
421 int main (void)
423 #define PRECISION 16
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)
429 int j;
430 const long N = FFT_SIZE;
431 double r[FFT_SIZE] = {0.0}, i[FFT_SIZE] = {0.0};
432 long n;
433 double t;
434 double amp, phase;
435 clock_t start, end;
436 double exec_time = 0;
437 FFTContext s;
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) +
446 0.5 * sin(2.0 * t) +
447 (1.0/3.0) * sin(3.0 * t) +
448 0.25 * sin(4.0 * t) +
449 0.2 * sin(5.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);
454 //getchar();
457 ff_fft_init(&s, 10, 1);
458 //start = clock();
459 //for(n = 0; n < 1000000; n++)
460 ff_fft_permute_c(&s, z);
461 ff_fft_calc_c(&s, z);
462 //end = clock();
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)));
467 //getchar();
469 printf("muls = %d, adds = %d\n", muls, adds);
470 //printf(" Time elapsed = %f\n", exec_time);
471 //ff_fft_end(&s);
474 #endif