Debian Codegraph @

Externals/FreeSurround/source/KissFFTR.cpp

raw
1 /*
2 Copyright (c) 2003-2004, Mark Borgerding
3
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without modification,
7 are permitted
8 provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions
12 and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright notice,
14 this list of
15 conditions and the following disclaimer in the documentation and/or other
16 materials provided with
17 the distribution.
18 * Neither the author nor the names of any contributors may be used to
19 endorse or promote
20 products derived from this software without specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR
24 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 MERCHANTABILITY AND
26 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 OWNER OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29 OR CONSEQUENTIAL
30 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 SERVICES; LOSS OF USE,
32 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 LIABILITY, WHETHER
34 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF
36 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include "FreeSurround/KissFFTR.h"
40 #include "FreeSurround/_KissFFTGuts.h"
41
42 struct kiss_fftr_state {
43 kiss_fft_cfg substate;
44 kiss_fft_cpx *tmpbuf;
45 kiss_fft_cpx *super_twiddles;
46 #ifdef USE_SIMD
47 void *pad;
48 #endif
49 };
50
51 kiss_fftr_cfg kiss_fftr_alloc(int nfft, int inverse_fft, void *mem,
52 size_t *lenmem) {
53 int i;
54 kiss_fftr_cfg st = NULL;
55 size_t subsize = 65536 * 4, memneeded = 0;
56
57 if (nfft & 1) {
58 fprintf(stderr, "Real FFT optimization must be even.\n");
59 return NULL;
60 }
61 nfft >>= 1;
62
63 kiss_fft_alloc(nfft, inverse_fft, NULL, &subsize);
64 memneeded = sizeof(struct kiss_fftr_state) + subsize +
65 sizeof(kiss_fft_cpx) * (nfft * 3 / 2);
66
67 if (lenmem == NULL) {
68 st = (kiss_fftr_cfg)malloc(memneeded);
69 } else {
70 if (*lenmem >= memneeded)
71 st = (kiss_fftr_cfg)mem;
72 *lenmem = memneeded;
73 }
74 if (!st)
75 return NULL;
76
77 st->substate = (kiss_fft_cfg)(st + 1); /*just beyond kiss_fftr_state struct */
78 st->tmpbuf = (kiss_fft_cpx *)(((char *)st->substate) + subsize);
79 st->super_twiddles = st->tmpbuf + nfft;
80 kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
81
82 for (i = 0; i < nfft / 2; ++i) {
83 double phase =
84 -3.14159265358979323846264338327 * ((double)(i + 1) / nfft + .5);
85 if (inverse_fft)
86 phase *= -1;
87 kf_cexp(st->super_twiddles + i, phase);
88 }
89 return st;
90 }
91
92 void kiss_fftr(kiss_fftr_cfg st, const kiss_fft_scalar *timedata,
93 kiss_fft_cpx *freqdata) {
94 /* input buffer timedata is stored row-wise */
95 int k, ncfft;
96 kiss_fft_cpx fpnk, fpk, f1k, f2k, tw, tdc;
97
98 if (st->substate->inverse) {
99 fprintf(stderr, "kiss fft usage error: improper alloc\n");
100 exit(1);
101 }
102
103 ncfft = st->substate->nfft;
104
105 /*perform the parallel fft of two real signals packed in real,imag*/
106 kiss_fft(st->substate, (const kiss_fft_cpx *)timedata, st->tmpbuf);
107 /* The real part of the DC element of the frequency spectrum in st->tmpbuf
108 * contains the sum of the even-numbered elements of the input time sequence
109 * The imag part is the sum of the odd-numbered elements
110 *
111 * The sum of tdc.r and tdc.i is the sum of the input time sequence.
112 * yielding DC of input time sequence
113 * The difference of tdc.r - tdc.i is the sum of the input (dot product)
114 * [1,-1,1,-1...
115 * yielding Nyquist bin of input time sequence
116 */
117
118 tdc.r = st->tmpbuf[0].r;
119 tdc.i = st->tmpbuf[0].i;
120 C_FIXDIV(tdc, 2);
121 CHECK_OVERFLOW_OP(tdc.r, +, tdc.i);
122 CHECK_OVERFLOW_OP(tdc.r, -, tdc.i);
123 freqdata[0].r = tdc.r + tdc.i;
124 freqdata[ncfft].r = tdc.r - tdc.i;
125 #ifdef USE_SIMD
126 freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0);
127 #else
128 freqdata[ncfft].i = freqdata[0].i = 0;
129 #endif
130
131 for (k = 1; k <= ncfft / 2; ++k) {
132 fpk = st->tmpbuf[k];
133 fpnk.r = st->tmpbuf[ncfft - k].r;
134 fpnk.i = -st->tmpbuf[ncfft - k].i;
135 C_FIXDIV(fpk, 2);
136 C_FIXDIV(fpnk, 2);
137
138 C_ADD(f1k, fpk, fpnk);
139 C_SUB(f2k, fpk, fpnk);
140 C_MUL(tw, f2k, st->super_twiddles[k - 1]);
141
142 freqdata[k].r = HALF_OF(f1k.r + tw.r);
143 freqdata[k].i = HALF_OF(f1k.i + tw.i);
144 freqdata[ncfft - k].r = HALF_OF(f1k.r - tw.r);
145 freqdata[ncfft - k].i = HALF_OF(tw.i - f1k.i);
146 }
147 }
148
149 void kiss_fftri(kiss_fftr_cfg st, const kiss_fft_cpx *freqdata,
150 kiss_fft_scalar *timedata) {
151 /* input buffer timedata is stored row-wise */
152 int k, ncfft;
153
154 if (st->substate->inverse == 0) {
155 fprintf(stderr, "kiss fft usage error: improper alloc\n");
156 exit(1);
157 }
158
159 ncfft = st->substate->nfft;
160
161 st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
162 st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
163 C_FIXDIV(st->tmpbuf[0], 2);
164
165 for (k = 1; k <= ncfft / 2; ++k) {
166 kiss_fft_cpx fk, fnkc, fek, fok, tmp;
167 fk = freqdata[k];
168 fnkc.r = freqdata[ncfft - k].r;
169 fnkc.i = -freqdata[ncfft - k].i;
170 C_FIXDIV(fk, 2);
171 C_FIXDIV(fnkc, 2);
172
173 C_ADD(fek, fk, fnkc);
174 C_SUB(tmp, fk, fnkc);
175 C_MUL(fok, tmp, st->super_twiddles[k - 1]);
176 C_ADD(st->tmpbuf[k], fek, fok);
177 C_SUB(st->tmpbuf[ncfft - k], fek, fok);
178 #ifdef USE_SIMD
179 st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
180 #else
181 st->tmpbuf[ncfft - k].i *= -1;
182 #endif
183 }
184 kiss_fft(st->substate, st->tmpbuf, (kiss_fft_cpx *)timedata);
185 }