]> git.sesse.net Git - ffmpeg/blob - libavcodec/liba52/imdct.c
works with old gcc versions too
[ffmpeg] / libavcodec / liba52 / imdct.c
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>
5  *
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
8  *
9  * This file is part of a52dec, a free ATSC A-52 stream decoder.
10  * See http://liba52.sourceforge.net/ for updates.
11  *
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.
16  *
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.
21  *
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
25  */
26
27 #include "config.h"
28
29 #include <math.h>
30 #include <stdio.h>
31 #ifdef LIBA52_DJBFFT
32 #include <fftc4.h>
33 #endif
34 #ifndef M_PI
35 #define M_PI 3.1415926535897932384626433832795029
36 #endif
37 #include <inttypes.h>
38
39 #include "a52.h"
40 #include "a52_internal.h"
41 #include "mm_accel.h"
42
43 typedef struct complex_s {
44     sample_t real;
45     sample_t imag;
46 } complex_t;
47
48 static uint8_t fftorder[] = {
49       0,128, 64,192, 32,160,224, 96, 16,144, 80,208,240,112, 48,176,
50       8,136, 72,200, 40,168,232,104,248,120, 56,184, 24,152,216, 88,
51       4,132, 68,196, 36,164,228,100, 20,148, 84,212,244,116, 52,180,
52     252,124, 60,188, 28,156,220, 92, 12,140, 76,204,236,108, 44,172,
53       2,130, 66,194, 34,162,226, 98, 18,146, 82,210,242,114, 50,178,
54      10,138, 74,202, 42,170,234,106,250,122, 58,186, 26,154,218, 90,
55     254,126, 62,190, 30,158,222, 94, 14,142, 78,206,238,110, 46,174,
56       6,134, 70,198, 38,166,230,102,246,118, 54,182, 22,150,214, 86
57 };
58
59 /* Root values for IFFT */
60 static sample_t roots16[3];
61 static sample_t roots32[7];
62 static sample_t roots64[15];
63 static sample_t roots128[31];
64
65 /* Twiddle factors for IMDCT */
66 static complex_t pre1[128];
67 static complex_t post1[64];
68 static complex_t pre2[64];
69 static complex_t post2[32];
70
71 static sample_t a52_imdct_window[256];
72
73 static void (* ifft128) (complex_t * buf);
74 static void (* ifft64) (complex_t * buf);
75
76 static inline void ifft2 (complex_t * buf)
77 {
78     sample_t r, i;
79
80     r = buf[0].real;
81     i = buf[0].imag;
82     buf[0].real += buf[1].real;
83     buf[0].imag += buf[1].imag;
84     buf[1].real = r - buf[1].real;
85     buf[1].imag = i - buf[1].imag;
86 }
87
88 static inline void ifft4 (complex_t * buf)
89 {
90     sample_t tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
91
92     tmp1 = buf[0].real + buf[1].real;
93     tmp2 = buf[3].real + buf[2].real;
94     tmp3 = buf[0].imag + buf[1].imag;
95     tmp4 = buf[2].imag + buf[3].imag;
96     tmp5 = buf[0].real - buf[1].real;
97     tmp6 = buf[0].imag - buf[1].imag;
98     tmp7 = buf[2].imag - buf[3].imag;
99     tmp8 = buf[3].real - buf[2].real;
100
101     buf[0].real = tmp1 + tmp2;
102     buf[0].imag = tmp3 + tmp4;
103     buf[2].real = tmp1 - tmp2;
104     buf[2].imag = tmp3 - tmp4;
105     buf[1].real = tmp5 + tmp7;
106     buf[1].imag = tmp6 + tmp8;
107     buf[3].real = tmp5 - tmp7;
108     buf[3].imag = tmp6 - tmp8;
109 }
110
111 /* basic radix-2 ifft butterfly */
112
113 #define BUTTERFLY_0(t0,t1,W0,W1,d0,d1) do {     \
114     t0 = MUL (W1, d1) + MUL (W0, d0);           \
115     t1 = MUL (W0, d1) - MUL (W1, d0);           \
116 } while (0)
117
118 /* radix-2 ifft butterfly with bias */
119
120 #define BUTTERFLY_B(t0,t1,W0,W1,d0,d1) do {     \
121     t0 = BIAS (MUL (d1, W1) + MUL (d0, W0));    \
122     t1 = BIAS (MUL (d1, W0) - MUL (d0, W1));    \
123 } while (0)
124
125 /* the basic split-radix ifft butterfly */
126
127 #define BUTTERFLY(a0,a1,a2,a3,wr,wi) do {               \
128     BUTTERFLY_0 (tmp5, tmp6, wr, wi, a2.real, a2.imag); \
129     BUTTERFLY_0 (tmp8, tmp7, wr, wi, a3.imag, a3.real); \
130     tmp1 = tmp5 + tmp7;                                 \
131     tmp2 = tmp6 + tmp8;                                 \
132     tmp3 = tmp6 - tmp8;                                 \
133     tmp4 = tmp7 - tmp5;                                 \
134     a2.real = a0.real - tmp1;                           \
135     a2.imag = a0.imag - tmp2;                           \
136     a3.real = a1.real - tmp3;                           \
137     a3.imag = a1.imag - tmp4;                           \
138     a0.real += tmp1;                                    \
139     a0.imag += tmp2;                                    \
140     a1.real += tmp3;                                    \
141     a1.imag += tmp4;                                    \
142 } while (0)
143
144 /* split-radix ifft butterfly, specialized for wr=1 wi=0 */
145
146 #define BUTTERFLY_ZERO(a0,a1,a2,a3) do {        \
147     tmp1 = a2.real + a3.real;                   \
148     tmp2 = a2.imag + a3.imag;                   \
149     tmp3 = a2.imag - a3.imag;                   \
150     tmp4 = a3.real - a2.real;                   \
151     a2.real = a0.real - tmp1;                   \
152     a2.imag = a0.imag - tmp2;                   \
153     a3.real = a1.real - tmp3;                   \
154     a3.imag = a1.imag - tmp4;                   \
155     a0.real += tmp1;                            \
156     a0.imag += tmp2;                            \
157     a1.real += tmp3;                            \
158     a1.imag += tmp4;                            \
159 } while (0)
160
161 /* split-radix ifft butterfly, specialized for wr=wi */
162
163 #define BUTTERFLY_HALF(a0,a1,a2,a3,w) do {      \
164     tmp5 = MUL (a2.real + a2.imag, w);          \
165     tmp6 = MUL (a2.imag - a2.real, w);          \
166     tmp7 = MUL (a3.real - a3.imag, w);          \
167     tmp8 = MUL (a3.imag + a3.real, w);          \
168     tmp1 = tmp5 + tmp7;                         \
169     tmp2 = tmp6 + tmp8;                         \
170     tmp3 = tmp6 - tmp8;                         \
171     tmp4 = tmp7 - tmp5;                         \
172     a2.real = a0.real - tmp1;                   \
173     a2.imag = a0.imag - tmp2;                   \
174     a3.real = a1.real - tmp3;                   \
175     a3.imag = a1.imag - tmp4;                   \
176     a0.real += tmp1;                            \
177     a0.imag += tmp2;                            \
178     a1.real += tmp3;                            \
179     a1.imag += tmp4;                            \
180 } while (0)
181
182 static inline void ifft8 (complex_t * buf)
183 {
184     sample_t tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
185
186     ifft4 (buf);
187     ifft2 (buf + 4);
188     ifft2 (buf + 6);
189     BUTTERFLY_ZERO (buf[0], buf[2], buf[4], buf[6]);
190     BUTTERFLY_HALF (buf[1], buf[3], buf[5], buf[7], roots16[1]);
191 }
192
193 static void ifft_pass (complex_t * buf, sample_t * weight, int n)
194 {
195     complex_t * buf1;
196     complex_t * buf2;
197     complex_t * buf3;
198     sample_t tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
199     int i;
200
201     buf++;
202     buf1 = buf + n;
203     buf2 = buf + 2 * n;
204     buf3 = buf + 3 * n;
205
206     BUTTERFLY_ZERO (buf[-1], buf1[-1], buf2[-1], buf3[-1]);
207
208     i = n - 1;
209
210     do {
211         BUTTERFLY (buf[0], buf1[0], buf2[0], buf3[0],
212                    weight[0], weight[2*i-n]);
213         buf++;
214         buf1++;
215         buf2++;
216         buf3++;
217         weight++;
218     } while (--i);
219 }
220
221 static void ifft16 (complex_t * buf)
222 {
223     ifft8 (buf);
224     ifft4 (buf + 8);
225     ifft4 (buf + 12);
226     ifft_pass (buf, roots16, 4);
227 }
228
229 static void ifft32 (complex_t * buf)
230 {
231     ifft16 (buf);
232     ifft8 (buf + 16);
233     ifft8 (buf + 24);
234     ifft_pass (buf, roots32, 8);
235 }
236
237 static void ifft64_c (complex_t * buf)
238 {
239     ifft32 (buf);
240     ifft16 (buf + 32);
241     ifft16 (buf + 48);
242     ifft_pass (buf, roots64, 16);
243 }
244
245 static void ifft128_c (complex_t * buf)
246 {
247     ifft32 (buf);
248     ifft16 (buf + 32);
249     ifft16 (buf + 48);
250     ifft_pass (buf, roots64, 16);
251
252     ifft32 (buf + 64);
253     ifft32 (buf + 96);
254     ifft_pass (buf, roots128, 32);
255 }
256
257 void a52_imdct_512 (sample_t * data, sample_t * delay, sample_t bias)
258 {
259     int i, k;
260     sample_t t_r, t_i, a_r, a_i, b_r, b_i, w_1, w_2;
261     const sample_t * window = a52_imdct_window;
262     complex_t buf[128];
263         
264     for (i = 0; i < 128; i++) {
265         k = fftorder[i];
266         t_r = pre1[i].real;
267         t_i = pre1[i].imag;
268         BUTTERFLY_0 (buf[i].real, buf[i].imag, t_r, t_i, data[k], data[255-k]);
269     }
270
271     ifft128 (buf);
272
273     /* Post IFFT complex multiply plus IFFT complex conjugate*/
274     /* Window and convert to real valued signal */
275     for (i = 0; i < 64; i++) {
276         /* y[n] = z[n] * (xcos1[n] + j * xsin1[n]) ; */
277         t_r = post1[i].real;
278         t_i = post1[i].imag;
279         BUTTERFLY_0 (a_r, a_i, t_i, t_r, buf[i].imag, buf[i].real);
280         BUTTERFLY_0 (b_r, b_i, t_r, t_i, buf[127-i].imag, buf[127-i].real);
281
282         w_1 = window[2*i];
283         w_2 = window[255-2*i];
284         BUTTERFLY_B (data[255-2*i], data[2*i], w_2, w_1, a_r, delay[2*i]);
285         delay[2*i] = a_i;
286
287         w_1 = window[2*i+1];
288         w_2 = window[254-2*i];
289         BUTTERFLY_B (data[2*i+1], data[254-2*i], w_1, w_2, b_r, delay[2*i+1]);
290         delay[2*i+1] = b_i;
291     }
292 }
293
294 void a52_imdct_256 (sample_t * data, sample_t * delay, sample_t bias)
295 {
296     int i, k;
297     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;
298     const sample_t * window = a52_imdct_window;
299     complex_t buf1[64], buf2[64];
300
301     /* Pre IFFT complex multiply plus IFFT cmplx conjugate */
302     for (i = 0; i < 64; i++) {
303         k = fftorder[i];
304         t_r = pre2[i].real;
305         t_i = pre2[i].imag;
306         BUTTERFLY_0 (buf1[i].real, buf1[i].imag, t_r, t_i, data[k], data[254-k]);
307         BUTTERFLY_0 (buf2[i].real, buf2[i].imag, t_r, t_i, data[k+1], data[255-k]);
308     }
309
310     ifft64 (buf1);
311     ifft64 (buf2);
312
313     /* Post IFFT complex multiply */
314     /* Window and convert to real valued signal */
315     for (i = 0; i < 32; i++) {
316         /* y1[n] = z1[n] * (xcos2[n] + j * xs in2[n]) ; */ 
317         t_r = post2[i].real;
318         t_i = post2[i].imag;
319         BUTTERFLY_0 (a_r, a_i, t_i, t_r, buf1[i].imag, buf1[i].real);
320         BUTTERFLY_0 (b_r, b_i, t_r, t_i, buf1[63-i].imag, buf1[63-i].real);
321         BUTTERFLY_0 (c_r, c_i, t_i, t_r, buf2[i].imag, buf2[i].real);
322         BUTTERFLY_0 (d_r, d_i, t_r, t_i, buf2[63-i].imag, buf2[63-i].real);
323
324         w_1 = window[2*i];
325         w_2 = window[255-2*i];
326         BUTTERFLY_B (data[255-2*i], data[2*i], w_2, w_1, a_r, delay[2*i]);
327         delay[2*i] = c_i;
328
329         w_1 = window[128+2*i];
330         w_2 = window[127-2*i];
331         BUTTERFLY_B (data[128+2*i], data[127-2*i], w_1, w_2, a_i, delay[127-2*i]);
332         delay[127-2*i] = c_r;
333
334         w_1 = window[2*i+1];
335         w_2 = window[254-2*i];
336         BUTTERFLY_B (data[254-2*i], data[2*i+1], w_2, w_1, b_i, delay[2*i+1]);
337         delay[2*i+1] = d_r;
338
339         w_1 = window[129+2*i];
340         w_2 = window[126-2*i];
341         BUTTERFLY_B (data[129+2*i], data[126-2*i], w_1, w_2, b_r, delay[126-2*i]);
342         delay[126-2*i] = d_i;
343     }
344 }
345
346 static double besselI0 (double x)
347 {
348     double bessel = 1;
349     int i = 100;
350
351     do
352         bessel = bessel * x / (i * i) + 1;
353     while (--i);
354     return bessel;
355 }
356
357 void a52_imdct_init (uint32_t mm_accel)
358 {
359     int i, k;
360     double sum;
361     double local_imdct_window[256];
362
363     /* compute imdct window - kaiser-bessel derived window, alpha = 5.0 */
364     sum = 0;
365     for (i = 0; i < 256; i++) {
366         sum += besselI0 (i * (256 - i) * (5 * M_PI / 256) * (5 * M_PI / 256));
367         local_imdct_window[i] = sum;
368     }
369     sum++;
370     for (i = 0; i < 256; i++)
371         a52_imdct_window[i] = SAMPLE (sqrt (local_imdct_window[i] / sum));
372
373     for (i = 0; i < 3; i++)
374         roots16[i] = SAMPLE (cos ((M_PI / 8) * (i + 1)));
375
376     for (i = 0; i < 7; i++)
377         roots32[i] = SAMPLE (cos ((M_PI / 16) * (i + 1)));
378
379     for (i = 0; i < 15; i++)
380         roots64[i] = SAMPLE (cos ((M_PI / 32) * (i + 1)));
381
382     for (i = 0; i < 31; i++)
383         roots128[i] = SAMPLE (cos ((M_PI / 64) * (i + 1)));
384
385     for (i = 0; i < 64; i++) {
386         k = fftorder[i] / 2 + 64;
387         pre1[i].real = SAMPLE (cos ((M_PI / 256) * (k - 0.25)));
388         pre1[i].imag = SAMPLE (sin ((M_PI / 256) * (k - 0.25)));
389     }
390
391     for (i = 64; i < 128; i++) {
392         k = fftorder[i] / 2 + 64;
393         pre1[i].real = SAMPLE (-cos ((M_PI / 256) * (k - 0.25)));
394         pre1[i].imag = SAMPLE (-sin ((M_PI / 256) * (k - 0.25)));
395     }
396
397     for (i = 0; i < 64; i++) {
398         post1[i].real = SAMPLE (cos ((M_PI / 256) * (i + 0.5)));
399         post1[i].imag = SAMPLE (sin ((M_PI / 256) * (i + 0.5)));
400     }
401
402     for (i = 0; i < 64; i++) {
403         k = fftorder[i] / 4;
404         pre2[i].real = SAMPLE (cos ((M_PI / 128) * (k - 0.25)));
405         pre2[i].imag = SAMPLE (sin ((M_PI / 128) * (k - 0.25)));
406     }
407
408     for (i = 0; i < 32; i++) {
409         post2[i].real = SAMPLE (cos ((M_PI / 128) * (i + 0.5)));
410         post2[i].imag = SAMPLE (sin ((M_PI / 128) * (i + 0.5)));
411     }
412
413 #ifdef LIBA52_DJBFFT
414     if (mm_accel & MM_ACCEL_DJBFFT) {
415         ifft128 = (void (*) (complex_t *)) fftc4_un128;
416         ifft64 = (void (*) (complex_t *)) fftc4_un64;
417     } else
418 #endif
419     {
420         ifft128 = ifft128_c;
421         ifft64 = ifft64_c;
422     }
423 }