Statistics
| Branch: | Revision:

ffmpeg / libavcodec / liba52 / imdct.c @ 976f969b

History | View | Annotate | Download (11.6 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 "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 complex_t buf[128];
49

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

    
61
/* Root values for IFFT */
62
static sample_t roots16[3];
63
static sample_t roots32[7];
64
static sample_t roots64[15];
65
static sample_t roots128[31];
66

    
67
/* Twiddle factors for IMDCT */
68
static complex_t pre1[128];
69
static complex_t post1[64];
70
static complex_t pre2[64];
71
static complex_t post2[32];
72

    
73
static sample_t a52_imdct_window[256];
74

    
75
static void (* ifft128) (complex_t * buf);
76
static void (* ifft64) (complex_t * buf);
77

    
78
static inline void ifft2 (complex_t * buf)
79
{
80
    double r, i;
81

    
82
    r = buf[0].real;
83
    i = buf[0].imag;
84
    buf[0].real += buf[1].real;
85
    buf[0].imag += buf[1].imag;
86
    buf[1].real = r - buf[1].real;
87
    buf[1].imag = i - buf[1].imag;
88
}
89

    
90
static inline void ifft4 (complex_t * buf)
91
{
92
    double tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
93

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

    
103
    buf[0].real = tmp1 + tmp2;
104
    buf[0].imag = tmp3 + tmp4;
105
    buf[2].real = tmp1 - tmp2;
106
    buf[2].imag = tmp3 - tmp4;
107
    buf[1].real = tmp5 + tmp7;
108
    buf[1].imag = tmp6 + tmp8;
109
    buf[3].real = tmp5 - tmp7;
110
    buf[3].imag = tmp6 - tmp8;
111
}
112

    
113
/* the basic split-radix ifft butterfly */
114

    
115
#define BUTTERFLY(a0,a1,a2,a3,wr,wi) do {        \
116
    tmp5 = a2.real * wr + a2.imag * wi;                \
117
    tmp6 = a2.imag * wr - a2.real * wi;                \
118
    tmp7 = a3.real * wr - a3.imag * wi;                \
119
    tmp8 = a3.imag * wr + a3.real * wi;                \
120
    tmp1 = tmp5 + tmp7;                                \
121
    tmp2 = tmp6 + tmp8;                                \
122
    tmp3 = tmp6 - tmp8;                                \
123
    tmp4 = tmp7 - tmp5;                                \
124
    a2.real = a0.real - tmp1;                        \
125
    a2.imag = a0.imag - tmp2;                        \
126
    a3.real = a1.real - tmp3;                        \
127
    a3.imag = a1.imag - tmp4;                        \
128
    a0.real += tmp1;                                \
129
    a0.imag += tmp2;                                \
130
    a1.real += tmp3;                                \
131
    a1.imag += tmp4;                                \
132
} while (0)
133

    
134
/* split-radix ifft butterfly, specialized for wr=1 wi=0 */
135

    
136
#define BUTTERFLY_ZERO(a0,a1,a2,a3) do {        \
137
    tmp1 = a2.real + a3.real;                        \
138
    tmp2 = a2.imag + a3.imag;                        \
139
    tmp3 = a2.imag - a3.imag;                        \
140
    tmp4 = a3.real - a2.real;                        \
141
    a2.real = a0.real - tmp1;                        \
142
    a2.imag = a0.imag - tmp2;                        \
143
    a3.real = a1.real - tmp3;                        \
144
    a3.imag = a1.imag - tmp4;                        \
145
    a0.real += tmp1;                                \
146
    a0.imag += tmp2;                                \
147
    a1.real += tmp3;                                \
148
    a1.imag += tmp4;                                \
149
} while (0)
150

    
151
/* split-radix ifft butterfly, specialized for wr=wi */
152

    
153
#define BUTTERFLY_HALF(a0,a1,a2,a3,w) do {        \
154
    tmp5 = (a2.real + a2.imag) * w;                \
155
    tmp6 = (a2.imag - a2.real) * w;                \
156
    tmp7 = (a3.real - a3.imag) * w;                \
157
    tmp8 = (a3.imag + a3.real) * w;                \
158
    tmp1 = tmp5 + tmp7;                                \
159
    tmp2 = tmp6 + tmp8;                                \
160
    tmp3 = tmp6 - tmp8;                                \
161
    tmp4 = tmp7 - tmp5;                                \
162
    a2.real = a0.real - tmp1;                        \
163
    a2.imag = a0.imag - tmp2;                        \
164
    a3.real = a1.real - tmp3;                        \
165
    a3.imag = a1.imag - tmp4;                        \
166
    a0.real += tmp1;                                \
167
    a0.imag += tmp2;                                \
168
    a1.real += tmp3;                                \
169
    a1.imag += tmp4;                                \
170
} while (0)
171

    
172
static inline void ifft8 (complex_t * buf)
173
{
174
    double tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
175

    
176
    ifft4 (buf);
177
    ifft2 (buf + 4);
178
    ifft2 (buf + 6);
179
    BUTTERFLY_ZERO (buf[0], buf[2], buf[4], buf[6]);
180
    BUTTERFLY_HALF (buf[1], buf[3], buf[5], buf[7], roots16[1]);
181
}
182

    
183
static void ifft_pass (complex_t * buf, sample_t * weight, int n)
184
{
185
    complex_t * buf1;
186
    complex_t * buf2;
187
    complex_t * buf3;
188
    double tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
189
    int i;
190

    
191
    buf++;
192
    buf1 = buf + n;
193
    buf2 = buf + 2 * n;
194
    buf3 = buf + 3 * n;
195

    
196
    BUTTERFLY_ZERO (buf[-1], buf1[-1], buf2[-1], buf3[-1]);
197

    
198
    i = n - 1;
199

    
200
    do {
201
        BUTTERFLY (buf[0], buf1[0], buf2[0], buf3[0], weight[n], weight[2*i]);
202
        buf++;
203
        buf1++;
204
        buf2++;
205
        buf3++;
206
        weight++;
207
    } while (--i);
208
}
209

    
210
static void ifft16 (complex_t * buf)
211
{
212
    ifft8 (buf);
213
    ifft4 (buf + 8);
214
    ifft4 (buf + 12);
215
    ifft_pass (buf, roots16 - 4, 4);
216
}
217

    
218
static void ifft32 (complex_t * buf)
219
{
220
    ifft16 (buf);
221
    ifft8 (buf + 16);
222
    ifft8 (buf + 24);
223
    ifft_pass (buf, roots32 - 8, 8);
224
}
225

    
226
static void ifft64_c (complex_t * buf)
227
{
228
    ifft32 (buf);
229
    ifft16 (buf + 32);
230
    ifft16 (buf + 48);
231
    ifft_pass (buf, roots64 - 16, 16);
232
}
233

    
234
static void ifft128_c (complex_t * buf)
235
{
236
    ifft32 (buf);
237
    ifft16 (buf + 32);
238
    ifft16 (buf + 48);
239
    ifft_pass (buf, roots64 - 16, 16);
240

    
241
    ifft32 (buf + 64);
242
    ifft32 (buf + 96);
243
    ifft_pass (buf, roots128 - 32, 32);
244
}
245

    
246
void a52_imdct_512 (sample_t * data, sample_t * delay, sample_t bias)
247
{
248
    int i, k;
249
    sample_t t_r, t_i, a_r, a_i, b_r, b_i, w_1, w_2;
250
    const sample_t * window = a52_imdct_window;
251
        
252
    for (i = 0; i < 128; i++) {
253
        k = fftorder[i];
254
        t_r = pre1[i].real;
255
        t_i = pre1[i].imag;
256

    
257
        buf[i].real = t_i * data[255-k] + t_r * data[k];
258
        buf[i].imag = t_r * data[255-k] - t_i * data[k];
259
    }
260

    
261
    ifft128 (buf);
262

    
263
    /* Post IFFT complex multiply plus IFFT complex conjugate*/
264
    /* Window and convert to real valued signal */
265
    for (i = 0; i < 64; i++) {
266
        /* y[n] = z[n] * (xcos1[n] + j * xsin1[n]) ; */
267
        t_r = post1[i].real;
268
        t_i = post1[i].imag;
269

    
270
        a_r = t_r * buf[i].real     + t_i * buf[i].imag;
271
        a_i = t_i * buf[i].real     - t_r * buf[i].imag;
272
        b_r = t_i * buf[127-i].real + t_r * buf[127-i].imag;
273
        b_i = t_r * buf[127-i].real - t_i * buf[127-i].imag;
274

    
275
        w_1 = window[2*i];
276
        w_2 = window[255-2*i];
277
        data[2*i]     = delay[2*i] * w_2 - a_r * w_1 + bias;
278
        data[255-2*i] = delay[2*i] * w_1 + a_r * w_2 + bias;
279
        delay[2*i] = a_i;
280

    
281
        w_1 = window[2*i+1];
282
        w_2 = window[254-2*i];
283
        data[2*i+1]   = delay[2*i+1] * w_2 + b_r * w_1 + bias;
284
        data[254-2*i] = delay[2*i+1] * w_1 - b_r * w_2 + bias;
285
        delay[2*i+1] = b_i;
286
    }
287
}
288

    
289
void a52_imdct_256(sample_t data[],sample_t delay[],sample_t bias)
290
{
291
    int i, k;
292
    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;
293
    complex_t * buf1, * buf2;
294
    const sample_t * window = a52_imdct_window;
295

    
296
    buf1 = &buf[0];
297
    buf2 = &buf[64];
298

    
299
    /* Pre IFFT complex multiply plus IFFT cmplx conjugate */
300
    for (i = 0; i < 64; i++) {
301
        k = fftorder[i];
302
        t_r = pre2[i].real;
303
        t_i = pre2[i].imag;
304

    
305
        buf1[i].real = t_i * data[254-k] + t_r * data[k];
306
        buf1[i].imag = t_r * data[254-k] - t_i * data[k];
307

    
308
        buf2[i].real = t_i * data[255-k] + t_r * data[k+1];
309
        buf2[i].imag = t_r * data[255-k] - t_i * data[k+1];
310
    }
311

    
312
    ifft64 (buf1);
313
    ifft64 (buf2);
314

    
315
    /* Post IFFT complex multiply */
316
    /* Window and convert to real valued signal */
317
    for (i = 0; i < 32; i++) {
318
        /* y1[n] = z1[n] * (xcos2[n] + j * xs in2[n]) ; */ 
319
        t_r = post2[i].real;
320
        t_i = post2[i].imag;
321

    
322
        a_r = t_r * buf1[i].real    + t_i * buf1[i].imag;
323
        a_i = t_i * buf1[i].real    - t_r * buf1[i].imag;
324
        b_r = t_i * buf1[63-i].real + t_r * buf1[63-i].imag;
325
        b_i = t_r * buf1[63-i].real - t_i * buf1[63-i].imag;
326

    
327
        c_r = t_r * buf2[i].real    + t_i * buf2[i].imag;
328
        c_i = t_i * buf2[i].real    - t_r * buf2[i].imag;
329
        d_r = t_i * buf2[63-i].real + t_r * buf2[63-i].imag;
330
        d_i = t_r * buf2[63-i].real - t_i * buf2[63-i].imag;
331

    
332
        w_1 = window[2*i];
333
        w_2 = window[255-2*i];
334
        data[2*i]     = delay[2*i] * w_2 - a_r * w_1 + bias;
335
        data[255-2*i] = delay[2*i] * w_1 + a_r * w_2 + bias;
336
        delay[2*i] = c_i;
337

    
338
        w_1 = window[128+2*i];
339
        w_2 = window[127-2*i];
340
        data[128+2*i] = delay[127-2*i] * w_2 + a_i * w_1 + bias;
341
        data[127-2*i] = delay[127-2*i] * w_1 - a_i * w_2 + bias;
342
        delay[127-2*i] = c_r;
343

    
344
        w_1 = window[2*i+1];
345
        w_2 = window[254-2*i];
346
        data[2*i+1]   = delay[2*i+1] * w_2 - b_i * w_1 + bias;
347
        data[254-2*i] = delay[2*i+1] * w_1 + b_i * w_2 + bias;
348
        delay[2*i+1] = d_r;
349

    
350
        w_1 = window[129+2*i];
351
        w_2 = window[126-2*i];
352
        data[129+2*i] = delay[126-2*i] * w_2 + b_r * w_1 + bias;
353
        data[126-2*i] = delay[126-2*i] * w_1 - b_r * w_2 + bias;
354
        delay[126-2*i] = d_i;
355
    }
356
}
357

    
358
static double besselI0 (double x)
359
{
360
    double bessel = 1;
361
    int i = 100;
362

    
363
    do
364
        bessel = bessel * x / (i * i) + 1;
365
    while (--i);
366
    return bessel;
367
}
368

    
369
void a52_imdct_init (uint32_t mm_accel)
370
{
371
    int i, k;
372
    double sum;
373

    
374
    /* compute imdct window - kaiser-bessel derived window, alpha = 5.0 */
375
    sum = 0;
376
    for (i = 0; i < 256; i++) {
377
        sum += besselI0 (i * (256 - i) * (5 * M_PI / 256) * (5 * M_PI / 256));
378
        a52_imdct_window[i] = sum;
379
    }
380
    sum++;
381
    for (i = 0; i < 256; i++)
382
        a52_imdct_window[i] = sqrt (a52_imdct_window[i] / sum);
383

    
384
    for (i = 0; i < 3; i++)
385
        roots16[i] = cos ((M_PI / 8) * (i + 1));
386

    
387
    for (i = 0; i < 7; i++)
388
        roots32[i] = cos ((M_PI / 16) * (i + 1));
389

    
390
    for (i = 0; i < 15; i++)
391
        roots64[i] = cos ((M_PI / 32) * (i + 1));
392

    
393
    for (i = 0; i < 31; i++)
394
        roots128[i] = cos ((M_PI / 64) * (i + 1));
395

    
396
    for (i = 0; i < 64; i++) {
397
        k = fftorder[i] / 2 + 64;
398
        pre1[i].real = cos ((M_PI / 256) * (k - 0.25));
399
        pre1[i].imag = sin ((M_PI / 256) * (k - 0.25));
400
    }
401

    
402
    for (i = 64; i < 128; i++) {
403
        k = fftorder[i] / 2 + 64;
404
        pre1[i].real = -cos ((M_PI / 256) * (k - 0.25));
405
        pre1[i].imag = -sin ((M_PI / 256) * (k - 0.25));
406
    }
407

    
408
    for (i = 0; i < 64; i++) {
409
        post1[i].real = cos ((M_PI / 256) * (i + 0.5));
410
        post1[i].imag = sin ((M_PI / 256) * (i + 0.5));
411
    }
412

    
413
    for (i = 0; i < 64; i++) {
414
        k = fftorder[i] / 4;
415
        pre2[i].real = cos ((M_PI / 128) * (k - 0.25));
416
        pre2[i].imag = sin ((M_PI / 128) * (k - 0.25));
417
    }
418

    
419
    for (i = 0; i < 32; i++) {
420
        post2[i].real = cos ((M_PI / 128) * (i + 0.5));
421
        post2[i].imag = sin ((M_PI / 128) * (i + 0.5));
422
    }
423

    
424
#ifdef LIBA52_DJBFFT
425
    if (mm_accel & MM_ACCEL_DJBFFT) {
426
        fprintf (stderr, "Using djbfft for IMDCT transform\n");
427
        ifft128 = (void (*) (complex_t *)) fftc4_un128;
428
        ifft64 = (void (*) (complex_t *)) fftc4_un64;
429
    } else
430
#endif
431
    {
432
        fprintf (stderr, "No accelerated IMDCT transform found\n");
433
        ifft128 = ifft128_c;
434
        ifft64 = ifft64_c;
435
    }
436
}