Initial 800x480 cabbiev2 port, based on 480x800x16 one
[kugel-rb.git] / apps / codecs / lib / fft-ffmpeg.c
bloba76f49fd1e2666fa41d02f461f1f993de2dca3ae
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 "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
61 #endif
63 #if 0
64 static int split_radix_permutation(int i, int n, int inverse)
66 int m;
67 if(n <= 2) return i&1;
68 m = n >> 1;
69 if(!(i&m)) return split_radix_permutation(i, m, inverse)*2;
70 m >>= 1;
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)
77 int j, k, np;
78 FFTComplex tmp;
79 //const uint16_t *revtab = s->revtab;
80 np = 1 << s->nbits;
82 const int revtab_shift = (12 - s->nbits);
84 /* reverse */
85 for(j=0;j<np;j++) {
86 k = revtab[j]>>revtab_shift;
87 if (k < j) {
88 tmp = z[k];
89 z[k] = z[j];
90 z[j] = tmp;
94 #endif
96 #define BF(x,y,a,b) {\
97 x = a - b;\
98 y = a + b;\
101 #define BF_REV(x,y,a,b) {\
102 x = a + b;\
103 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);\
140 #endif
143 see conjugate pair description in
144 http://www.fftw.org/newsplit.pdf
146 a0 = z[k]
147 a1 = z[k+N/4]
148 a2 = z[k+2N/4]
149 a3 = z[k+3N/4]
151 result:
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])
157 i.e.
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)
166 so therefore
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.
180 Hence
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)
192 Then we just output:
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;
209 r_re = z[n*2].re;
210 r_im = z[n*2].im;
211 XPROD31_R(r_re, r_im, wre, wim, t1,t2);
212 r_re = z[n*3].re;
213 r_im = z[n*3].im;
214 XNPROD31_R(r_re, r_im, wre, wim, t5,t6);
215 BUTTERFLIES(z[0],z[n],z[n*2],z[n*3]);
216 return z+1;
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;
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]);
230 return z+1;
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;
237 r_re = z[n*2].re;
238 r_im = z[n*2].im;
239 XPROD31_R(r_re, r_im, wre, wim, t1,t2);
240 r_re = z[n*3].re;
241 r_im = z[n*3].im;
242 XNPROD31_R(r_re, r_im, wre, wim, t5,t6);
243 BUTTERFLIES(z[0],z[n],z[n*2],z[n*3]);
244 return z+1;
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);
251 my_z += n*4;
252 t2 = MULT31(my_z[0], cPI2_8);
253 temp1 = MULT31(my_z[1], cPI2_8);
254 my_z += n*2;
255 temp2 = MULT31(my_z[0], cPI2_8);
256 t5 = MULT31(my_z[1], cPI2_8);
257 t1 = ( temp1 + t2 );
258 t2 = ( temp1 - t2 );
259 t6 = ( temp2 + t5 );
260 t5 = ( temp2 - t5 );
261 my_z -= n*6;
262 BUTTERFLIES(z[0],z[n],z[n*2],z[n*3]);
263 return z+1;
266 static inline FFTComplex* TRANSFORM_ZERO(FFTComplex * z, unsigned int n)
268 FFTSample t1,t2,t5,t6;
269 t1 = z[n*2].re;
270 t2 = z[n*2].im;
271 t5 = z[n*3].re;
272 t6 = z[n*3].im;
273 BUTTERFLIES(z[0],z[n],z[n*2],z[n*3]);
274 return z+1;
276 #endif
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);
293 w += STEP;
294 /* first pass forwards through sincos_lookup0*/
295 do {
296 z = TRANSFORM_W10(z,n,w);
297 w += STEP;
298 z = TRANSFORM_W10(z,n,w);
299 w += STEP;
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);
307 w -= STEP;
308 z = TRANSFORM_W01(z,n,w);
309 w -= STEP;
313 /* what is STEP?
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)\
324 fft##n2(z);\
325 fft##n4(z+n4*2);\
326 fft##n4(z+n4*3);\
327 pass(z,8192/n,n4);\
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
347 #endif
349 static void fft4_dispatch(FFTComplex *z)
351 fft4(z);
354 #ifndef FFT_FFMPEG_INCL_OPTIMISED_FFT8
355 static inline void fft8(FFTComplex *z)
357 fft4(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);
364 BF(t8, t1, t3, t1);
365 BF(t7, t2, t2, t4);
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);
371 z++;
372 TRANSFORM_EQUAL(z,2);
374 #endif
376 static void fft8_dispatch(FFTComplex *z)
378 fft8(z);
381 #ifndef CONFIG_SMALL
382 void fft16(FFTComplex *z) ICODE_ATTR_TREMOR_MDCT;
383 void fft16(FFTComplex *z)
385 fft8(z);
386 fft4(z+8);
387 fft4(z+12);
389 TRANSFORM_ZERO(z,4);
390 z+=2;
391 TRANSFORM_EQUAL(z,4);
392 z-=1;
393 TRANSFORM(z,4,cPI1_8,cPI3_8);
394 z+=2;
395 TRANSFORM(z,4,cPI3_8,cPI1_8);
397 #else
398 DECL_FFT(16,8,4)
399 #endif
400 DECL_FFT(32,16,8)
401 DECL_FFT(64,32,16)
402 DECL_FFT(128,64,32)
403 DECL_FFT(256,128,64)
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,
411 fft2048, fft4096
414 void ff_fft_calc_c(int nbits, FFTComplex *z)
416 fft_dispatch[nbits-2](z);
419 #if 0
420 int main (void)
422 #define PRECISION 16
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)
428 int j;
429 const long N = FFT_SIZE;
430 double r[FFT_SIZE] = {0.0}, i[FFT_SIZE] = {0.0};
431 long n;
432 double t;
433 double amp, phase;
434 clock_t start, end;
435 double exec_time = 0;
436 FFTContext s;
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) +
445 0.5 * sin(2.0 * t) +
446 (1.0/3.0) * sin(3.0 * t) +
447 0.25 * sin(4.0 * t) +
448 0.2 * sin(5.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);
453 //getchar();
456 ff_fft_init(&s, 10, 1);
457 //start = clock();
458 //for(n = 0; n < 1000000; n++)
459 ff_fft_permute_c(&s, z);
460 ff_fft_calc_c(&s, z);
461 //end = clock();
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)));
466 //getchar();
468 printf("muls = %d, adds = %d\n", muls, adds);
469 //printf(" Time elapsed = %f\n", exec_time);
470 //ff_fft_end(&s);
473 #endif