3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 * The ifft algorithms in this file have been largely inspired by Dan
7 * Bernstein's work, djbfft, available at http://cr.yp.to/djbfft.html
9 * This file is part of a52dec, a free ATSC A-52 stream decoder.
10 * See http://liba52.sourceforge.net/ for updates.
12 * a52dec is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * a52dec is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "config-a52.h"
36 #define M_PI 3.1415926535897932384626433832795029
41 #include "a52_internal.h"
44 typedef struct complex_s
{
49 static const uint8_t fftorder
[] = {
50 0,128, 64,192, 32,160,224, 96, 16,144, 80,208,240,112, 48,176,
51 8,136, 72,200, 40,168,232,104,248,120, 56,184, 24,152,216, 88,
52 4,132, 68,196, 36,164,228,100, 20,148, 84,212,244,116, 52,180,
53 252,124, 60,188, 28,156,220, 92, 12,140, 76,204,236,108, 44,172,
54 2,130, 66,194, 34,162,226, 98, 18,146, 82,210,242,114, 50,178,
55 10,138, 74,202, 42,170,234,106,250,122, 58,186, 26,154,218, 90,
56 254,126, 62,190, 30,158,222, 94, 14,142, 78,206,238,110, 46,174,
57 6,134, 70,198, 38,166,230,102,246,118, 54,182, 22,150,214, 86
60 /* Root values for IFFT */
61 //static sample_t roots16[3];
62 //static sample_t roots32[7];
63 //static sample_t roots64[15];
64 //static sample_t roots128[31];
66 /* Twiddle factors for IMDCT */
67 //static complex_t pre1[128];
68 //static complex_t post1[64];
69 //static complex_t pre2[64];
70 //static complex_t post2[32];
72 //static sample_t a52_imdct_window[256];
73 #include "imdct_lookups.h"
77 static void (* ifft128) (complex_t * buf);
78 static void (* ifft64) (complex_t * buf);
80 static inline void ifft2 (complex_t * buf)
86 buf[0].real += buf[1].real;
87 buf[0].imag += buf[1].imag;
88 buf[1].real = r - buf[1].real;
89 buf[1].imag = i - buf[1].imag;
92 static inline void ifft4 (complex_t * buf)
94 sample_t tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
96 tmp1 = buf[0].real + buf[1].real;
97 tmp2 = buf[3].real + buf[2].real;
98 tmp3 = buf[0].imag + buf[1].imag;
99 tmp4 = buf[2].imag + buf[3].imag;
100 tmp5 = buf[0].real - buf[1].real;
101 tmp6 = buf[0].imag - buf[1].imag;
102 tmp7 = buf[2].imag - buf[3].imag;
103 tmp8 = buf[3].real - buf[2].real;
105 buf[0].real = tmp1 + tmp2;
106 buf[0].imag = tmp3 + tmp4;
107 buf[2].real = tmp1 - tmp2;
108 buf[2].imag = tmp3 - tmp4;
109 buf[1].real = tmp5 + tmp7;
110 buf[1].imag = tmp6 + tmp8;
111 buf[3].real = tmp5 - tmp7;
112 buf[3].imag = tmp6 - tmp8;
115 /* basic radix-2 ifft butterfly */
117 #define BUTTERFLY_0(t0,t1,W0,W1,d0,d1) do { \
118 t0 = MUL (W1, d1) + MUL (W0, d0); \
119 t1 = MUL (W0, d1) - MUL (W1, d0); \
122 /* radix-2 ifft butterfly with bias */
124 #define BUTTERFLY_B(t0,t1,W0,W1,d0,d1) do { \
125 t0 = BIAS (MUL (d1, W1) + MUL (d0, W0)); \
126 t1 = BIAS (MUL (d1, W0) - MUL (d0, W1)); \
129 /* the basic split-radix ifft butterfly */
131 #define BUTTERFLY(a0,a1,a2,a3,wr,wi) do { \
132 BUTTERFLY_0 (tmp5, tmp6, wr, wi, a2.real, a2.imag); \
133 BUTTERFLY_0 (tmp8, tmp7, wr, wi, a3.imag, a3.real); \
134 tmp1 = tmp5 + tmp7; \
135 tmp2 = tmp6 + tmp8; \
136 tmp3 = tmp6 - tmp8; \
137 tmp4 = tmp7 - tmp5; \
138 a2.real = a0.real - tmp1; \
139 a2.imag = a0.imag - tmp2; \
140 a3.real = a1.real - tmp3; \
141 a3.imag = a1.imag - tmp4; \
148 /* split-radix ifft butterfly, specialized for wr=1 wi=0 */
150 #define BUTTERFLY_ZERO(a0,a1,a2,a3) do { \
151 tmp1 = a2.real + a3.real; \
152 tmp2 = a2.imag + a3.imag; \
153 tmp3 = a2.imag - a3.imag; \
154 tmp4 = a3.real - a2.real; \
155 a2.real = a0.real - tmp1; \
156 a2.imag = a0.imag - tmp2; \
157 a3.real = a1.real - tmp3; \
158 a3.imag = a1.imag - tmp4; \
165 /* split-radix ifft butterfly, specialized for wr=wi */
167 #define BUTTERFLY_HALF(a0,a1,a2,a3,w) do { \
168 tmp5 = MUL (a2.real + a2.imag, w); \
169 tmp6 = MUL (a2.imag - a2.real, w); \
170 tmp7 = MUL (a3.real - a3.imag, w); \
171 tmp8 = MUL (a3.imag + a3.real, w); \
172 tmp1 = tmp5 + tmp7; \
173 tmp2 = tmp6 + tmp8; \
174 tmp3 = tmp6 - tmp8; \
175 tmp4 = tmp7 - tmp5; \
176 a2.real = a0.real - tmp1; \
177 a2.imag = a0.imag - tmp2; \
178 a3.real = a1.real - tmp3; \
179 a3.imag = a1.imag - tmp4; \
186 static inline void ifft8 (complex_t * buf)
188 sample_t tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
193 BUTTERFLY_ZERO (buf[0], buf[2], buf[4], buf[6]);
194 BUTTERFLY_HALF (buf[1], buf[3], buf[5], buf[7], roots16[1]);
197 static void ifft_pass (complex_t * buf, const sample_t * weight, int n)
202 sample_t tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
210 BUTTERFLY_ZERO (buf[-1], buf1[-1], buf2[-1], buf3[-1]);
215 BUTTERFLY (buf[0], buf1[0], buf2[0], buf3[0],
216 weight[0], weight[2*i-n]);
225 static void ifft16 (complex_t * buf)
230 ifft_pass (buf, roots16, 4);
233 static void ifft32 (complex_t * buf)
238 ifft_pass (buf, roots32, 8);
241 static void ifft64_c (complex_t * buf)
246 ifft_pass (buf, roots64, 16);
249 static void ifft128_c (complex_t * buf)
254 ifft_pass (buf, roots64, 16);
258 ifft_pass (buf, roots128, 32);
261 void a52_imdct_512 (sample_t
* data
, sample_t
* delay
)
264 sample_t t_r
, t_i
, a_r
, a_i
, b_r
, b_i
, w_1
, w_2
;
265 const sample_t
* window
= a52_imdct_window
;
268 for (i
= 0; i
< 128; i
++) {
272 BUTTERFLY_0 (buf
[i
].re
, buf
[i
].im
, t_r
, t_i
, data
[k
], data
[255-k
]);
276 ff_fft_calc_c(7, (FFTComplex
*)&buf
);
278 /* Post IFFT complex multiply plus IFFT complex conjugate*/
279 /* Window and convert to real valued signal */
280 for (i
= 0; i
< 64; i
++) {
281 /* y[n] = z[n] * (xcos1[n] + j * xsin1[n]) ; */
284 BUTTERFLY_0 (a_r
, a_i
, t_i
, t_r
, buf
[i
].im
, buf
[i
].re
);
285 BUTTERFLY_0 (b_r
, b_i
, t_r
, t_i
, buf
[127-i
].im
, buf
[127-i
].re
);
288 w_2
= window
[255-2*i
];
289 BUTTERFLY_B (data
[255-2*i
], data
[2*i
], w_2
, w_1
, a_r
, delay
[2*i
]);
293 w_2
= window
[254-2*i
];
294 BUTTERFLY_B (data
[2*i
+1], data
[254-2*i
], w_1
, w_2
, b_r
, delay
[2*i
+1]);
299 void a52_imdct_256 (sample_t
* data
, sample_t
* delay
)
302 sample_t t_r
, t_i
, a_r
, a_i
, b_r
, b_i
, c_r
, c_i
, d_r
, d_i
, w_1
, w_2
;
303 const sample_t
* window
= a52_imdct_window
;
304 FFTComplex buf1
[64], buf2
[64];
306 /* Pre IFFT complex multiply plus IFFT cmplx conjugate */
307 for (i
= 0; i
< 64; i
++) {
311 BUTTERFLY_0 (buf1
[i
].re
, buf1
[i
].im
, t_r
, t_i
, data
[k
], data
[254-k
]);
312 BUTTERFLY_0 (buf2
[i
].re
, buf2
[i
].im
, t_r
, t_i
, data
[k
+1], data
[255-k
]);
317 ff_fft_calc_c(6, (FFTComplex
*)&buf1
);
318 ff_fft_calc_c(6, (FFTComplex
*)&buf2
);
320 /* Post IFFT complex multiply */
321 /* Window and convert to real valued signal */
322 for (i
= 0; i
< 32; i
++) {
323 /* y1[n] = z1[n] * (xcos2[n] + j * xs in2[n]) ; */
326 BUTTERFLY_0 (a_r
, a_i
, t_i
, t_r
, buf1
[i
].im
, buf1
[i
].re
);
327 BUTTERFLY_0 (b_r
, b_i
, t_r
, t_i
, buf1
[63-i
].im
, buf1
[63-i
].re
);
328 BUTTERFLY_0 (c_r
, c_i
, t_i
, t_r
, buf2
[i
].im
, buf2
[i
].re
);
329 BUTTERFLY_0 (d_r
, d_i
, t_r
, t_i
, buf2
[63-i
].im
, buf2
[63-i
].re
);
332 w_2
= window
[255-2*i
];
333 BUTTERFLY_B (data
[255-2*i
], data
[2*i
], w_2
, w_1
, a_r
, delay
[2*i
]);
336 w_1
= window
[128+2*i
];
337 w_2
= window
[127-2*i
];
338 BUTTERFLY_B (data
[128+2*i
], data
[127-2*i
], w_1
, w_2
, a_i
, delay
[127-2*i
]);
339 delay
[127-2*i
] = c_r
;
342 w_2
= window
[254-2*i
];
343 BUTTERFLY_B (data
[254-2*i
], data
[2*i
+1], w_2
, w_1
, b_i
, delay
[2*i
+1]);
346 w_1
= window
[129+2*i
];
347 w_2
= window
[126-2*i
];
348 BUTTERFLY_B (data
[129+2*i
], data
[126-2*i
], w_1
, w_2
, b_r
, delay
[126-2*i
]);
349 delay
[126-2*i
] = d_i
;
354 static double besselI0 (double x)
360 bessel = bessel * x / (i * i) + 1;
366 void a52_imdct_init (uint32_t mm_accel
)
369 //ff_fft_init(&s128, 7, 1);
370 //ff_fft_init(&s64, 6, 1);
374 double local_imdct_window[256];*/
376 /* compute imdct window - kaiser-bessel derived window, alpha = 5.0 */
378 for (i = 0; i < 256; i++) {
379 sum += besselI0 (i * (256 - i) * (5 * M_PI / 256) * (5 * M_PI / 256));
380 local_imdct_window[i] = sum;
384 /* for (i = 0; i < 256; i++)
385 a52_imdct_window[i] = SAMPLE (sqrt (local_imdct_window[i] / sum));
387 printf("static sample_t a52_imdct_window[256]={");
388 for (i=0;i<256;i++) {
389 if ((i % 16)==0) { printf("\n"); }
390 printf("%d,",a52_imdct_window[i]);
395 /* for (i = 0; i < 3; i++)
396 roots16[i] = SAMPLE (cos ((M_PI / 8) * (i + 1)));
398 printf("static sample_t roots16[3]={%d,%d,%d};\n\n",roots16[0],roots16[1],roots16[2]);
400 for (i = 0; i < 7; i++)
401 roots32[i] = SAMPLE (cos ((M_PI / 16) * (i + 1)));
403 printf("static sample_t roots32[7]={");
404 for (i=0;i<7;i++) { printf("%d%s",roots32[i],(i < 6 ? "," : "")); }
407 for (i = 0; i < 15; i++)
408 roots64[i] = SAMPLE (cos ((M_PI / 32) * (i + 1)));
410 printf("static sample_t roots64[15]={");
411 for (i=0;i<15;i++) { printf("%d%s",roots64[i],(i < 14 ? "," : "")); }
414 for (i = 0; i < 31; i++)
415 roots128[i] = SAMPLE (cos ((M_PI / 64) * (i + 1)));
417 printf("static sample_t roots128[31]={");
418 for (i=0;i<31;i++) { printf("%d%s",roots128[i],(i < 30 ? "," : "")); }
422 for (i = 0; i < 64; i++) {
423 k = fftorder[i] / 2 + 64;
424 pre1[i].real = SAMPLE (cos ((M_PI / 256) * (k - 0.25)));
425 pre1[i].imag = SAMPLE (sin ((M_PI / 256) * (k - 0.25)));
428 for (i = 64; i < 128; i++) {
429 k = fftorder[i] / 2 + 64;
430 pre1[i].real = SAMPLE (-cos ((M_PI / 256) * (k - 0.25)));
431 pre1[i].imag = SAMPLE (-sin ((M_PI / 256) * (k - 0.25)));
434 printf("static complex_t pre1[128]={");
435 for (i=0;i<128;i++) { printf("{%d,%d}%s",pre1[i].real,pre1[i].imag,(i < 127 ? "," : "")); }
439 for (i = 0; i < 64; i++) {
440 post1[i].real = SAMPLE (cos ((M_PI / 256) * (i + 0.5)));
441 post1[i].imag = SAMPLE (sin ((M_PI / 256) * (i + 0.5)));
444 printf("static complex_t post1[64]={");
445 for (i=0;i<64;i++) { printf("{%d,%d}%s",post1[i].real,post1[i].imag,(i < 63 ? "," : "")); }
450 for (i = 0; i < 64; i++) {
452 pre2[i].real = SAMPLE (cos ((M_PI / 128) * (k - 0.25)));
453 pre2[i].imag = SAMPLE (sin ((M_PI / 128) * (k - 0.25)));
456 printf("static complex_t pre2[64]={");
457 for (i=0;i<64;i++) { printf("{%d,%d}%s",pre2[i].real,pre2[i].imag,(i < 63 ? "," : "")); }
460 for (i = 0; i < 32; i++) {
461 post2[i].real = SAMPLE (cos ((M_PI / 128) * (i + 0.5)));
462 post2[i].imag = SAMPLE (sin ((M_PI / 128) * (i + 0.5)));
465 printf("static complex_t post2[32]={");
466 for (i=0;i<32;i++) { printf("{%d,%d}%s",post2[i].real,post2[i].imag,(i < 31 ? "," : "")); }
471 if (mm_accel & MM_ACCEL_DJBFFT) {
472 #ifndef LIBA52_DOUBLE
473 ifft128 = (void (*) (complex_t *)) fftc4_un128;
474 ifft64 = (void (*) (complex_t *)) fftc4_un64;
476 ifft128 = (void (*) (complex_t *)) fftc8_un128;
477 ifft64 = (void (*) (complex_t *)) fftc8_un64;