CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/ext/at3_standalone/fft.cpp
Views: 1401
1
/*
2
* FFT/IFFT transforms
3
* Copyright (c) 2008 Loren Merritt
4
* Copyright (c) 2002 Fabrice Bellard
5
* Partly based on libdjbfft by D. J. Bernstein
6
*
7
* This file is part of FFmpeg.
8
*
9
* FFmpeg is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU Lesser General Public
11
* License as published by the Free Software Foundation; either
12
* version 2.1 of the License, or (at your option) any later version.
13
*
14
* FFmpeg is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
* Lesser General Public License for more details.
18
*
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with FFmpeg; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
*/
23
24
/**
25
* @file
26
* FFT/IFFT transforms.
27
*/
28
29
#include <stdlib.h>
30
#include <string.h>
31
32
#define _USE_MATH_DEFINES
33
#include <math.h>
34
35
#include "mem.h"
36
#include "fft.h"
37
38
#define sqrthalf (float)M_SQRT1_2
39
40
void imdct_calc(FFTContext *s, FFTSample *output, const FFTSample *input);
41
void imdct_half(FFTContext *s, FFTSample *output, const FFTSample *input);
42
43
/* cos(2*pi*x/n) for 0<=x<=n/4, followed by its reverse */
44
COSTABLE(16);
45
COSTABLE(32);
46
COSTABLE(64);
47
COSTABLE(128);
48
COSTABLE(256);
49
COSTABLE(512);
50
COSTABLE(1024);
51
52
static FFTSample * const av_cos_tabs[] = {
53
NULL, NULL, NULL, NULL,
54
av_cos_16,
55
av_cos_32,
56
av_cos_64,
57
av_cos_128,
58
av_cos_256,
59
av_cos_512,
60
av_cos_1024,
61
};
62
63
void fft_calc(FFTContext *s, FFTComplex *z);
64
65
static int split_radix_permutation(int i, int n, int inverse)
66
{
67
int m;
68
if(n <= 2) return i&1;
69
m = n >> 1;
70
if(!(i&m)) return split_radix_permutation(i, m, inverse)*2;
71
m >>= 1;
72
if(inverse == !(i&m)) return split_radix_permutation(i, m, inverse)*4 + 1;
73
else return split_radix_permutation(i, m, inverse)*4 - 1;
74
}
75
76
void ff_init_ff_cos_tabs(int index)
77
{
78
int i;
79
int m = 1<<index;
80
double freq = 2*M_PI/m;
81
FFTSample *tab = av_cos_tabs[index];
82
for(i=0; i<=m/4; i++)
83
tab[i] = cos(i*freq);
84
for(i=1; i<m/4; i++)
85
tab[m/2-i] = tab[i];
86
}
87
88
static const int avx_tab[] = {
89
0, 4, 1, 5, 8, 12, 9, 13, 2, 6, 3, 7, 10, 14, 11, 15
90
};
91
92
static int is_second_half_of_fft32(int i, int n)
93
{
94
if (n <= 32)
95
return i >= 16;
96
else if (i < n/2)
97
return is_second_half_of_fft32(i, n/2);
98
else if (i < 3*n/4)
99
return is_second_half_of_fft32(i - n/2, n/4);
100
else
101
return is_second_half_of_fft32(i - 3*n/4, n/4);
102
}
103
104
int ff_fft_init(FFTContext *s, int nbits, int inverse)
105
{
106
int i, j, n;
107
108
if (nbits < 2 || nbits > 16)
109
goto fail;
110
s->nbits = nbits;
111
n = 1 << nbits;
112
113
s->revtab = (uint16_t *)av_malloc(n * sizeof(uint16_t));
114
if (!s->revtab)
115
goto fail;
116
s->tmp_buf = (FFTComplex *)av_malloc(n * sizeof(FFTComplex));
117
if (!s->tmp_buf)
118
goto fail;
119
s->inverse = inverse;
120
121
for(j=4; j<=nbits; j++) {
122
ff_init_ff_cos_tabs(j);
123
}
124
125
for(i=0; i<n; i++) {
126
j = i;
127
int index = -split_radix_permutation(i, n, s->inverse) & (n - 1);
128
s->revtab[index] = j;
129
}
130
131
return 0;
132
fail:
133
av_freep(&s->revtab);
134
av_freep(&s->tmp_buf);
135
return -1;
136
}
137
138
void ff_fft_end(FFTContext *s)
139
{
140
av_freep(&s->revtab);
141
av_freep(&s->tmp_buf);
142
}
143
144
#define BF(x, y, a, b) do { \
145
x = a - b; \
146
y = a + b; \
147
} while (0)
148
149
#define BUTTERFLIES(a0,a1,a2,a3) {\
150
BF(t3, t5, t5, t1);\
151
BF(a2.re, a0.re, a0.re, t5);\
152
BF(a3.im, a1.im, a1.im, t3);\
153
BF(t4, t6, t2, t6);\
154
BF(a3.re, a1.re, a1.re, t4);\
155
BF(a2.im, a0.im, a0.im, t6);\
156
}
157
158
// force loading all the inputs before storing any.
159
// this is slightly slower for small data, but avoids store->load aliasing
160
// for addresses separated by large powers of 2.
161
#define BUTTERFLIES_BIG(a0,a1,a2,a3) {\
162
FFTSample r0=a0.re, i0=a0.im, r1=a1.re, i1=a1.im;\
163
BF(t3, t5, t5, t1);\
164
BF(a2.re, a0.re, r0, t5);\
165
BF(a3.im, a1.im, i1, t3);\
166
BF(t4, t6, t2, t6);\
167
BF(a3.re, a1.re, r1, t4);\
168
BF(a2.im, a0.im, i0, t6);\
169
}
170
171
#define TRANSFORM(a0,a1,a2,a3,wre,wim) {\
172
CMUL(t1, t2, a2.re, a2.im, wre, -wim);\
173
CMUL(t5, t6, a3.re, a3.im, wre, wim);\
174
BUTTERFLIES(a0,a1,a2,a3)\
175
}
176
177
#define TRANSFORM_ZERO(a0,a1,a2,a3) {\
178
t1 = a2.re;\
179
t2 = a2.im;\
180
t5 = a3.re;\
181
t6 = a3.im;\
182
BUTTERFLIES(a0,a1,a2,a3)\
183
}
184
185
/* z[0...8n-1], w[1...2n-1] */
186
#define PASS(name)\
187
static void name(FFTComplex *z, const FFTSample *wre, unsigned int n)\
188
{\
189
FFTDouble t1, t2, t3, t4, t5, t6;\
190
int o1 = 2*n;\
191
int o2 = 4*n;\
192
int o3 = 6*n;\
193
const FFTSample *wim = wre+o1;\
194
n--;\
195
\
196
TRANSFORM_ZERO(z[0],z[o1],z[o2],z[o3]);\
197
TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
198
do {\
199
z += 2;\
200
wre += 2;\
201
wim -= 2;\
202
TRANSFORM(z[0],z[o1],z[o2],z[o3],wre[0],wim[0]);\
203
TRANSFORM(z[1],z[o1+1],z[o2+1],z[o3+1],wre[1],wim[-1]);\
204
} while(--n);\
205
}
206
207
PASS(pass)
208
#undef BUTTERFLIES
209
#define BUTTERFLIES BUTTERFLIES_BIG
210
PASS(pass_big)
211
212
#define DECL_FFT(n,n2,n4)\
213
static void fft##n(FFTComplex *z)\
214
{\
215
fft##n2(z);\
216
fft##n4(z+n4*2);\
217
fft##n4(z+n4*3);\
218
pass(z,av_cos_##n,n4/2);\
219
}
220
221
static void fft4(FFTComplex *z)
222
{
223
FFTDouble t1, t2, t3, t4, t5, t6, t7, t8;
224
225
BF(t3, t1, z[0].re, z[1].re);
226
BF(t8, t6, z[3].re, z[2].re);
227
BF(z[2].re, z[0].re, t1, t6);
228
BF(t4, t2, z[0].im, z[1].im);
229
BF(t7, t5, z[2].im, z[3].im);
230
BF(z[3].im, z[1].im, t4, t8);
231
BF(z[3].re, z[1].re, t3, t7);
232
BF(z[2].im, z[0].im, t2, t5);
233
}
234
235
static void fft8(FFTComplex *z)
236
{
237
FFTDouble t1, t2, t3, t4, t5, t6;
238
239
fft4(z);
240
241
BF(t1, z[5].re, z[4].re, -z[5].re);
242
BF(t2, z[5].im, z[4].im, -z[5].im);
243
BF(t5, z[7].re, z[6].re, -z[7].re);
244
BF(t6, z[7].im, z[6].im, -z[7].im);
245
246
BUTTERFLIES(z[0],z[2],z[4],z[6]);
247
TRANSFORM(z[1],z[3],z[5],z[7],sqrthalf,sqrthalf);
248
}
249
250
static void fft16(FFTComplex *z)
251
{
252
FFTDouble t1, t2, t3, t4, t5, t6;
253
FFTSample cos_16_1 = av_cos_16[1];
254
FFTSample cos_16_3 = av_cos_16[3];
255
256
fft8(z);
257
fft4(z+8);
258
fft4(z+12);
259
260
TRANSFORM_ZERO(z[0],z[4],z[8],z[12]);
261
TRANSFORM(z[2],z[6],z[10],z[14],sqrthalf,sqrthalf);
262
TRANSFORM(z[1],z[5],z[9],z[13],cos_16_1,cos_16_3);
263
TRANSFORM(z[3],z[7],z[11],z[15],cos_16_3,cos_16_1);
264
}
265
DECL_FFT(32,16,8)
266
DECL_FFT(64,32,16)
267
DECL_FFT(128,64,32)
268
DECL_FFT(256,128,64)
269
DECL_FFT(512,256,128)
270
#define pass pass_big
271
DECL_FFT(1024,512,256)
272
273
static void (* const fft_dispatch[])(FFTComplex*) = {
274
fft4, fft8, fft16, fft32, fft64, fft128, fft256, fft512, fft1024,
275
};
276
277
void fft_calc(FFTContext *s, FFTComplex *z) {
278
fft_dispatch[s->nbits-2](z);
279
}
280
281
#include <stdlib.h>
282
#include <string.h>
283
284
#include "fft.h"
285
#include "mem.h"
286
287
/**
288
* init MDCT or IMDCT computation.
289
*/
290
int ff_mdct_init(FFTContext *s, int nbits, int inverse, double scale)
291
{
292
int n, n4, i;
293
double alpha, theta;
294
int tstep;
295
296
memset(s, 0, sizeof(*s));
297
n = 1 << nbits;
298
s->mdct_bits = nbits;
299
s->mdct_size = n;
300
n4 = n >> 2;
301
s->mdct_permutation = FF_MDCT_PERM_NONE;
302
303
if (ff_fft_init(s, s->mdct_bits - 2, inverse) < 0)
304
goto fail;
305
306
s->tcos = (FFTSample *)av_malloc_array(n / 2, sizeof(FFTSample));
307
if (!s->tcos)
308
goto fail;
309
310
switch (s->mdct_permutation) {
311
case FF_MDCT_PERM_NONE:
312
s->tsin = s->tcos + n4;
313
tstep = 1;
314
break;
315
case FF_MDCT_PERM_INTERLEAVE:
316
s->tsin = s->tcos + 1;
317
tstep = 2;
318
break;
319
default:
320
goto fail;
321
}
322
323
theta = 1.0 / 8.0 + (scale < 0 ? n4 : 0);
324
scale = sqrt(fabs(scale));
325
for (i = 0; i < n4; i++) {
326
alpha = 2 * M_PI * (i + theta) / n;
327
s->tcos[i * tstep] = -cos(alpha) * scale;
328
s->tsin[i * tstep] = -sin(alpha) * scale;
329
}
330
return 0;
331
fail:
332
ff_mdct_end(s);
333
return -1;
334
}
335
336
/**
337
* Compute the middle half of the inverse MDCT of size N = 2^nbits,
338
* thus excluding the parts that can be derived by symmetry
339
* @param output N/2 samples
340
* @param input N/2 samples
341
*/
342
void imdct_half(FFTContext *s, FFTSample *output, const FFTSample *input)
343
{
344
int k, n8, n4, n2, n, j;
345
const uint16_t *revtab = s->revtab;
346
const FFTSample *tcos = s->tcos;
347
const FFTSample *tsin = s->tsin;
348
const FFTSample *in1, *in2;
349
FFTComplex *z = (FFTComplex *)output;
350
351
n = 1 << s->mdct_bits;
352
n2 = n >> 1;
353
n4 = n >> 2;
354
n8 = n >> 3;
355
356
/* pre rotation */
357
in1 = input;
358
in2 = input + n2 - 1;
359
for (k = 0; k < n4; k++) {
360
j = revtab[k];
361
CMUL(z[j].re, z[j].im, *in2, *in1, tcos[k], tsin[k]);
362
in1 += 2;
363
in2 -= 2;
364
}
365
fft_calc(s, z);
366
367
/* post rotation + reordering */
368
for (k = 0; k < n8; k++) {
369
FFTSample r0, i0, r1, i1;
370
CMUL(r0, i1, z[n8 - k - 1].im, z[n8 - k - 1].re, tsin[n8 - k - 1], tcos[n8 - k - 1]);
371
CMUL(r1, i0, z[n8 + k].im, z[n8 + k].re, tsin[n8 + k], tcos[n8 + k]);
372
z[n8 - k - 1].re = r0;
373
z[n8 - k - 1].im = i0;
374
z[n8 + k].re = r1;
375
z[n8 + k].im = i1;
376
}
377
}
378
379
/**
380
* Compute inverse MDCT of size N = 2^nbits
381
* @param output N samples
382
* @param input N/2 samples
383
*/
384
void imdct_calc(FFTContext *s, FFTSample *output, const FFTSample *input)
385
{
386
int k;
387
int n = 1 << s->mdct_bits;
388
int n2 = n >> 1;
389
int n4 = n >> 2;
390
391
imdct_half(s, output + n4, input);
392
393
for (k = 0; k < n4; k++) {
394
output[k] = -output[n2 - k - 1];
395
output[n - k - 1] = output[n2 + k];
396
}
397
}
398
399
void ff_mdct_end(FFTContext *s)
400
{
401
av_freep(&s->tcos);
402
ff_fft_end(s);
403
}
404
405