Statistics
| Branch: | Revision:

ffmpeg / libavcodec / liba52 / imdct.c @ 5bb6fbb3

History | View | Annotate | Download (11.4 KB)

1
/*
2
 * imdct.c
3
 * Copyright (C) 2000-2002 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 "a52.h"
28
#include "a52_internal.h"
29
#include "mm_accel.h"
30

    
31
typedef struct complex_s {
32
    sample_t real;
33
    sample_t imag;
34
} complex_t;
35

    
36
static complex_t buf[128];
37

    
38
static uint8_t fftorder[] = {
39
      0,128, 64,192, 32,160,224, 96, 16,144, 80,208,240,112, 48,176,
40
      8,136, 72,200, 40,168,232,104,248,120, 56,184, 24,152,216, 88,
41
      4,132, 68,196, 36,164,228,100, 20,148, 84,212,244,116, 52,180,
42
    252,124, 60,188, 28,156,220, 92, 12,140, 76,204,236,108, 44,172,
43
      2,130, 66,194, 34,162,226, 98, 18,146, 82,210,242,114, 50,178,
44
     10,138, 74,202, 42,170,234,106,250,122, 58,186, 26,154,218, 90,
45
    254,126, 62,190, 30,158,222, 94, 14,142, 78,206,238,110, 46,174,
46
      6,134, 70,198, 38,166,230,102,246,118, 54,182, 22,150,214, 86
47
};
48

    
49
/* Root values for IFFT */
50
static sample_t roots16[3];
51
static sample_t roots32[7];
52
static sample_t roots64[15];
53
static sample_t roots128[31];
54

    
55
/* Twiddle factors for IMDCT */
56
static complex_t pre1[128];
57
static complex_t post1[64];
58
static complex_t pre2[64];
59
static complex_t post2[32];
60

    
61
static sample_t a52_imdct_window[256];
62

    
63
static void (* ifft128) (complex_t * buf);
64
static void (* ifft64) (complex_t * buf);
65

    
66
static inline void ifft2 (complex_t * buf)
67
{
68
    double r, i;
69

    
70
    r = buf[0].real;
71
    i = buf[0].imag;
72
    buf[0].real += buf[1].real;
73
    buf[0].imag += buf[1].imag;
74
    buf[1].real = r - buf[1].real;
75
    buf[1].imag = i - buf[1].imag;
76
}
77

    
78
static inline void ifft4 (complex_t * buf)
79
{
80
    double tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
81

    
82
    tmp1 = buf[0].real + buf[1].real;
83
    tmp2 = buf[3].real + buf[2].real;
84
    tmp3 = buf[0].imag + buf[1].imag;
85
    tmp4 = buf[2].imag + buf[3].imag;
86
    tmp5 = buf[0].real - buf[1].real;
87
    tmp6 = buf[0].imag - buf[1].imag;
88
    tmp7 = buf[2].imag - buf[3].imag;
89
    tmp8 = buf[3].real - buf[2].real;
90

    
91
    buf[0].real = tmp1 + tmp2;
92
    buf[0].imag = tmp3 + tmp4;
93
    buf[2].real = tmp1 - tmp2;
94
    buf[2].imag = tmp3 - tmp4;
95
    buf[1].real = tmp5 + tmp7;
96
    buf[1].imag = tmp6 + tmp8;
97
    buf[3].real = tmp5 - tmp7;
98
    buf[3].imag = tmp6 - tmp8;
99
}
100

    
101
/* the basic split-radix ifft butterfly */
102

    
103
#define BUTTERFLY(a0,a1,a2,a3,wr,wi) do {        \
104
    tmp5 = a2.real * wr + a2.imag * wi;                \
105
    tmp6 = a2.imag * wr - a2.real * wi;                \
106
    tmp7 = a3.real * wr - a3.imag * wi;                \
107
    tmp8 = a3.imag * wr + a3.real * wi;                \
108
    tmp1 = tmp5 + tmp7;                                \
109
    tmp2 = tmp6 + tmp8;                                \
110
    tmp3 = tmp6 - tmp8;                                \
111
    tmp4 = tmp7 - tmp5;                                \
112
    a2.real = a0.real - tmp1;                        \
113
    a2.imag = a0.imag - tmp2;                        \
114
    a3.real = a1.real - tmp3;                        \
115
    a3.imag = a1.imag - tmp4;                        \
116
    a0.real += tmp1;                                \
117
    a0.imag += tmp2;                                \
118
    a1.real += tmp3;                                \
119
    a1.imag += tmp4;                                \
120
} while (0)
121

    
122
/* split-radix ifft butterfly, specialized for wr=1 wi=0 */
123

    
124
#define BUTTERFLY_ZERO(a0,a1,a2,a3) do {        \
125
    tmp1 = a2.real + a3.real;                        \
126
    tmp2 = a2.imag + a3.imag;                        \
127
    tmp3 = a2.imag - a3.imag;                        \
128
    tmp4 = a3.real - a2.real;                        \
129
    a2.real = a0.real - tmp1;                        \
130
    a2.imag = a0.imag - tmp2;                        \
131
    a3.real = a1.real - tmp3;                        \
132
    a3.imag = a1.imag - tmp4;                        \
133
    a0.real += tmp1;                                \
134
    a0.imag += tmp2;                                \
135
    a1.real += tmp3;                                \
136
    a1.imag += tmp4;                                \
137
} while (0)
138

    
139
/* split-radix ifft butterfly, specialized for wr=wi */
140

    
141
#define BUTTERFLY_HALF(a0,a1,a2,a3,w) do {        \
142
    tmp5 = (a2.real + a2.imag) * w;                \
143
    tmp6 = (a2.imag - a2.real) * w;                \
144
    tmp7 = (a3.real - a3.imag) * w;                \
145
    tmp8 = (a3.imag + a3.real) * w;                \
146
    tmp1 = tmp5 + tmp7;                                \
147
    tmp2 = tmp6 + tmp8;                                \
148
    tmp3 = tmp6 - tmp8;                                \
149
    tmp4 = tmp7 - tmp5;                                \
150
    a2.real = a0.real - tmp1;                        \
151
    a2.imag = a0.imag - tmp2;                        \
152
    a3.real = a1.real - tmp3;                        \
153
    a3.imag = a1.imag - tmp4;                        \
154
    a0.real += tmp1;                                \
155
    a0.imag += tmp2;                                \
156
    a1.real += tmp3;                                \
157
    a1.imag += tmp4;                                \
158
} while (0)
159

    
160
static inline void ifft8 (complex_t * buf)
161
{
162
    double tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
163

    
164
    ifft4 (buf);
165
    ifft2 (buf + 4);
166
    ifft2 (buf + 6);
167
    BUTTERFLY_ZERO (buf[0], buf[2], buf[4], buf[6]);
168
    BUTTERFLY_HALF (buf[1], buf[3], buf[5], buf[7], roots16[1]);
169
}
170

    
171
static void ifft_pass (complex_t * buf, sample_t * weight, int n)
172
{
173
    complex_t * buf1;
174
    complex_t * buf2;
175
    complex_t * buf3;
176
    double tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
177
    int i;
178

    
179
    buf++;
180
    buf1 = buf + n;
181
    buf2 = buf + 2 * n;
182
    buf3 = buf + 3 * n;
183

    
184
    BUTTERFLY_ZERO (buf[-1], buf1[-1], buf2[-1], buf3[-1]);
185

    
186
    i = n - 1;
187

    
188
    do {
189
        BUTTERFLY (buf[0], buf1[0], buf2[0], buf3[0], weight[n], weight[2*i]);
190
        buf++;
191
        buf1++;
192
        buf2++;
193
        buf3++;
194
        weight++;
195
    } while (--i);
196
}
197

    
198
static void ifft16 (complex_t * buf)
199
{
200
    ifft8 (buf);
201
    ifft4 (buf + 8);
202
    ifft4 (buf + 12);
203
    ifft_pass (buf, roots16 - 4, 4);
204
}
205

    
206
static void ifft32 (complex_t * buf)
207
{
208
    ifft16 (buf);
209
    ifft8 (buf + 16);
210
    ifft8 (buf + 24);
211
    ifft_pass (buf, roots32 - 8, 8);
212
}
213

    
214
static void ifft64_c (complex_t * buf)
215
{
216
    ifft32 (buf);
217
    ifft16 (buf + 32);
218
    ifft16 (buf + 48);
219
    ifft_pass (buf, roots64 - 16, 16);
220
}
221

    
222
static void ifft128_c (complex_t * buf)
223
{
224
    ifft32 (buf);
225
    ifft16 (buf + 32);
226
    ifft16 (buf + 48);
227
    ifft_pass (buf, roots64 - 16, 16);
228

    
229
    ifft32 (buf + 64);
230
    ifft32 (buf + 96);
231
    ifft_pass (buf, roots128 - 32, 32);
232
}
233

    
234
void a52_imdct_512 (sample_t * data, sample_t * delay, sample_t bias)
235
{
236
    int i, k;
237
    sample_t t_r, t_i, a_r, a_i, b_r, b_i, w_1, w_2;
238
    const sample_t * window = a52_imdct_window;
239
        
240
    for (i = 0; i < 128; i++) {
241
        k = fftorder[i];
242
        t_r = pre1[i].real;
243
        t_i = pre1[i].imag;
244

    
245
        buf[i].real = t_i * data[255-k] + t_r * data[k];
246
        buf[i].imag = t_r * data[255-k] - t_i * data[k];
247
    }
248

    
249
    ifft128 (buf);
250

    
251
    /* Post IFFT complex multiply plus IFFT complex conjugate*/
252
    /* Window and convert to real valued signal */
253
    for (i = 0; i < 64; i++) {
254
        /* y[n] = z[n] * (xcos1[n] + j * xsin1[n]) ; */
255
        t_r = post1[i].real;
256
        t_i = post1[i].imag;
257

    
258
        a_r = t_r * buf[i].real     + t_i * buf[i].imag;
259
        a_i = t_i * buf[i].real     - t_r * buf[i].imag;
260
        b_r = t_i * buf[127-i].real + t_r * buf[127-i].imag;
261
        b_i = t_r * buf[127-i].real - t_i * buf[127-i].imag;
262

    
263
        w_1 = window[2*i];
264
        w_2 = window[255-2*i];
265
        data[2*i]     = delay[2*i] * w_2 - a_r * w_1 + bias;
266
        data[255-2*i] = delay[2*i] * w_1 + a_r * w_2 + bias;
267
        delay[2*i] = a_i;
268

    
269
        w_1 = window[2*i+1];
270
        w_2 = window[254-2*i];
271
        data[2*i+1]   = delay[2*i+1] * w_2 + b_r * w_1 + bias;
272
        data[254-2*i] = delay[2*i+1] * w_1 - b_r * w_2 + bias;
273
        delay[2*i+1] = b_i;
274
    }
275
}
276

    
277
void a52_imdct_256(sample_t data[],sample_t delay[],sample_t bias)
278
{
279
    int i, k;
280
    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;
281
    complex_t * buf1, * buf2;
282
    const sample_t * window = a52_imdct_window;
283

    
284
    buf1 = &buf[0];
285
    buf2 = &buf[64];
286

    
287
    /* Pre IFFT complex multiply plus IFFT cmplx conjugate */
288
    for (i = 0; i < 64; i++) {
289
        k = fftorder[i];
290
        t_r = pre2[i].real;
291
        t_i = pre2[i].imag;
292

    
293
        buf1[i].real = t_i * data[254-k] + t_r * data[k];
294
        buf1[i].imag = t_r * data[254-k] - t_i * data[k];
295

    
296
        buf2[i].real = t_i * data[255-k] + t_r * data[k+1];
297
        buf2[i].imag = t_r * data[255-k] - t_i * data[k+1];
298
    }
299

    
300
    ifft64 (buf1);
301
    ifft64 (buf2);
302

    
303
    /* Post IFFT complex multiply */
304
    /* Window and convert to real valued signal */
305
    for (i = 0; i < 32; i++) {
306
        /* y1[n] = z1[n] * (xcos2[n] + j * xs in2[n]) ; */ 
307
        t_r = post2[i].real;
308
        t_i = post2[i].imag;
309

    
310
        a_r = t_r * buf1[i].real    + t_i * buf1[i].imag;
311
        a_i = t_i * buf1[i].real    - t_r * buf1[i].imag;
312
        b_r = t_i * buf1[63-i].real + t_r * buf1[63-i].imag;
313
        b_i = t_r * buf1[63-i].real - t_i * buf1[63-i].imag;
314

    
315
        c_r = t_r * buf2[i].real    + t_i * buf2[i].imag;
316
        c_i = t_i * buf2[i].real    - t_r * buf2[i].imag;
317
        d_r = t_i * buf2[63-i].real + t_r * buf2[63-i].imag;
318
        d_i = t_r * buf2[63-i].real - t_i * buf2[63-i].imag;
319

    
320
        w_1 = window[2*i];
321
        w_2 = window[255-2*i];
322
        data[2*i]     = delay[2*i] * w_2 - a_r * w_1 + bias;
323
        data[255-2*i] = delay[2*i] * w_1 + a_r * w_2 + bias;
324
        delay[2*i] = c_i;
325

    
326
        w_1 = window[128+2*i];
327
        w_2 = window[127-2*i];
328
        data[128+2*i] = delay[127-2*i] * w_2 + a_i * w_1 + bias;
329
        data[127-2*i] = delay[127-2*i] * w_1 - a_i * w_2 + bias;
330
        delay[127-2*i] = c_r;
331

    
332
        w_1 = window[2*i+1];
333
        w_2 = window[254-2*i];
334
        data[2*i+1]   = delay[2*i+1] * w_2 - b_i * w_1 + bias;
335
        data[254-2*i] = delay[2*i+1] * w_1 + b_i * w_2 + bias;
336
        delay[2*i+1] = d_r;
337

    
338
        w_1 = window[129+2*i];
339
        w_2 = window[126-2*i];
340
        data[129+2*i] = delay[126-2*i] * w_2 + b_r * w_1 + bias;
341
        data[126-2*i] = delay[126-2*i] * w_1 - b_r * w_2 + bias;
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

    
362
    /* compute imdct window - kaiser-bessel derived window, alpha = 5.0 */
363
    sum = 0;
364
    for (i = 0; i < 256; i++) {
365
        sum += besselI0 (i * (256 - i) * (5 * M_PI / 256) * (5 * M_PI / 256));
366
        a52_imdct_window[i] = sum;
367
    }
368
    sum++;
369
    for (i = 0; i < 256; i++)
370
        a52_imdct_window[i] = sqrt (a52_imdct_window[i] / sum);
371

    
372
    for (i = 0; i < 3; i++)
373
        roots16[i] = cos ((M_PI / 8) * (i + 1));
374

    
375
    for (i = 0; i < 7; i++)
376
        roots32[i] = cos ((M_PI / 16) * (i + 1));
377

    
378
    for (i = 0; i < 15; i++)
379
        roots64[i] = cos ((M_PI / 32) * (i + 1));
380

    
381
    for (i = 0; i < 31; i++)
382
        roots128[i] = cos ((M_PI / 64) * (i + 1));
383

    
384
    for (i = 0; i < 64; i++) {
385
        k = fftorder[i] / 2 + 64;
386
        pre1[i].real = cos ((M_PI / 256) * (k - 0.25));
387
        pre1[i].imag = sin ((M_PI / 256) * (k - 0.25));
388
    }
389

    
390
    for (i = 64; i < 128; i++) {
391
        k = fftorder[i] / 2 + 64;
392
        pre1[i].real = -cos ((M_PI / 256) * (k - 0.25));
393
        pre1[i].imag = -sin ((M_PI / 256) * (k - 0.25));
394
    }
395

    
396
    for (i = 0; i < 64; i++) {
397
        post1[i].real = cos ((M_PI / 256) * (i + 0.5));
398
        post1[i].imag = sin ((M_PI / 256) * (i + 0.5));
399
    }
400

    
401
    for (i = 0; i < 64; i++) {
402
        k = fftorder[i] / 4;
403
        pre2[i].real = cos ((M_PI / 128) * (k - 0.25));
404
        pre2[i].imag = sin ((M_PI / 128) * (k - 0.25));
405
    }
406

    
407
    for (i = 0; i < 32; i++) {
408
        post2[i].real = cos ((M_PI / 128) * (i + 0.5));
409
        post2[i].imag = sin ((M_PI / 128) * (i + 0.5));
410
    }
411

    
412
#ifdef LIBA52_DJBFFT
413
    if (mm_accel & MM_ACCEL_DJBFFT) {
414
        fprintf (stderr, "Using djbfft for IMDCT transform\n");
415
        ifft128 = (void (*) (complex_t *)) fftc4_un128;
416
        ifft64 = (void (*) (complex_t *)) fftc4_un64;
417
    } else
418
#endif
419
    {
420
        fprintf (stderr, "No accelerated IMDCT transform found\n");
421
        ifft128 = ifft128_c;
422
        ifft64 = ifft64_c;
423
    }
424
}