lcd-m6sp.c: remove \r
[kugel-rb.git] / apps / codecs / liba52 / imdct.c
blobe93424c5fa4ca3990950fa573018c3a37d680a34
1 /*
2 * imdct.c
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"
29 #include <math.h>
30 #include <stdio.h>
31 #ifdef LIBA52_DJBFFT
32 #include <fftc4.h>
33 #include <fftc8.h>
34 #endif
35 #ifndef M_PI
36 #define M_PI 3.1415926535897932384626433832795029
37 #endif
38 #include <inttypes.h>
40 #include "a52.h"
41 #include "a52_internal.h"
42 #include "mm_accel.h"
44 typedef struct complex_s {
45 sample_t real;
46 sample_t imag;
47 } complex_t;
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)
82 sample_t r, i;
84 r = buf[0].real;
85 i = buf[0].imag;
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); \
120 } while (0)
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)); \
127 } while (0)
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; \
142 a0.real += tmp1; \
143 a0.imag += tmp2; \
144 a1.real += tmp3; \
145 a1.imag += tmp4; \
146 } while (0)
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; \
159 a0.real += tmp1; \
160 a0.imag += tmp2; \
161 a1.real += tmp3; \
162 a1.imag += tmp4; \
163 } while (0)
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; \
180 a0.real += tmp1; \
181 a0.imag += tmp2; \
182 a1.real += tmp3; \
183 a1.imag += tmp4; \
184 } while (0)
186 static inline void ifft8 (complex_t * buf)
188 sample_t tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
190 ifft4 (buf);
191 ifft2 (buf + 4);
192 ifft2 (buf + 6);
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)
199 complex_t * buf1;
200 complex_t * buf2;
201 complex_t * buf3;
202 sample_t tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
203 int i;
205 buf++;
206 buf1 = buf + n;
207 buf2 = buf + 2 * n;
208 buf3 = buf + 3 * n;
210 BUTTERFLY_ZERO (buf[-1], buf1[-1], buf2[-1], buf3[-1]);
212 i = n - 1;
214 do {
215 BUTTERFLY (buf[0], buf1[0], buf2[0], buf3[0],
216 weight[0], weight[2*i-n]);
217 buf++;
218 buf1++;
219 buf2++;
220 buf3++;
221 weight++;
222 } while (--i);
225 static void ifft16 (complex_t * buf)
227 ifft8 (buf);
228 ifft4 (buf + 8);
229 ifft4 (buf + 12);
230 ifft_pass (buf, roots16, 4);
233 static void ifft32 (complex_t * buf)
235 ifft16 (buf);
236 ifft8 (buf + 16);
237 ifft8 (buf + 24);
238 ifft_pass (buf, roots32, 8);
241 static void ifft64_c (complex_t * buf)
243 ifft32 (buf);
244 ifft16 (buf + 32);
245 ifft16 (buf + 48);
246 ifft_pass (buf, roots64, 16);
249 static void ifft128_c (complex_t * buf)
251 ifft32 (buf);
252 ifft16 (buf + 32);
253 ifft16 (buf + 48);
254 ifft_pass (buf, roots64, 16);
256 ifft32 (buf + 64);
257 ifft32 (buf + 96);
258 ifft_pass (buf, roots128, 32);
261 void a52_imdct_512 (sample_t * data, sample_t * delay)
263 int i, k;
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;
266 FFTComplex buf[128];
268 for (i = 0; i < 128; i++) {
269 k = fftorder[i];
270 t_r = pre1[i].real;
271 t_i = pre1[i].imag;
272 BUTTERFLY_0 (buf[i].re, buf[i].im, t_r, t_i, data[k], data[255-k]);
275 //ifft128 (buf);
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]) ; */
282 t_r = post1[i].real;
283 t_i = post1[i].imag;
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);
287 w_1 = window[2*i];
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]);
290 delay[2*i] = a_i;
292 w_1 = window[2*i+1];
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]);
295 delay[2*i+1] = b_i;
299 void a52_imdct_256 (sample_t * data, sample_t * delay)
301 int i, k;
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++) {
308 k = fftorder[i];
309 t_r = pre2[i].real;
310 t_i = pre2[i].imag;
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]);
315 //ifft64 (buf1);
316 //ifft64 (buf2);
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]) ; */
324 t_r = post2[i].real;
325 t_i = post2[i].imag;
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);
331 w_1 = window[2*i];
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]);
334 delay[2*i] = c_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;
341 w_1 = window[2*i+1];
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]);
344 delay[2*i+1] = d_r;
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)
356 double bessel = 1;
357 int i = 100;
360 bessel = bessel * x / (i * i) + 1;
361 while (--i);
362 return bessel;
366 void a52_imdct_init (uint32_t mm_accel)
368 (void)mm_accel;
369 //ff_fft_init(&s128, 7, 1);
370 //ff_fft_init(&s64, 6, 1);
372 /* int i, k;
373 double sum;
374 double local_imdct_window[256];*/
376 /* compute imdct window - kaiser-bessel derived window, alpha = 5.0 */
377 /* sum = 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;
382 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]);
392 printf("\n}\n");
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 ? "," : "")); }
405 printf("};\n");
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 ? "," : "")); }
412 printf("};\n");
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 ? "," : "")); }
419 printf("};\n");
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 ? "," : "")); }
436 printf("};\n");
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 ? "," : "")); }
446 printf("};\n");
450 for (i = 0; i < 64; i++) {
451 k = fftorder[i] / 4;
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 ? "," : "")); }
458 printf("};\n");
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 ? "," : "")); }
467 printf("};\n");
470 #ifdef LIBA52_DJBFFT
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;
475 #else
476 ifft128 = (void (*) (complex_t *)) fftc8_un128;
477 ifft64 = (void (*) (complex_t *)) fftc8_un64;
478 #endif
479 } else
480 #endif
482 ifft128 = ifft128_c;
483 ifft64 = ifft64_c;