]> 4ch.mooo.com Git - 16.git/blob - src/lib/doslib/ext/faad/cfft.c
wwww
[16.git] / src / lib / doslib / ext / faad / cfft.c
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4 **  
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 ** 
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ** GNU General Public License for more details.
14 ** 
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software 
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 **
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
21 **
22 ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23 ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24 **
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27 **
28 ** $Id: cfft.c,v 1.35 2007/11/01 12:33:29 menno Exp $
29 **/
30
31 /*
32  * Algorithmically based on Fortran-77 FFTPACK
33  * by Paul N. Swarztrauber(Version 4, 1985).
34  *
35  * Does even sized fft only
36  */
37
38 /* isign is +1 for backward and -1 for forward transforms */
39
40 #include "common.h"
41 #include "structs.h"
42
43 #include <stdlib.h>
44
45 #include "cfft.h"
46 #include "cfft_tab.h"
47
48
49 /* static function declarations */
50 static void passf2pos(const uint16_t ido, const uint16_t l1, const complex_t *cc,
51                       complex_t *ch, const complex_t *wa);
52 static void passf2neg(const uint16_t ido, const uint16_t l1, const complex_t *cc,
53                       complex_t *ch, const complex_t *wa);
54 static void passf3(const uint16_t ido, const uint16_t l1, const complex_t *cc,
55                    complex_t *ch, const complex_t *wa1, const complex_t *wa2, const int8_t isign);
56 static void passf4pos(const uint16_t ido, const uint16_t l1, const complex_t *cc, complex_t *ch,
57                       const complex_t *wa1, const complex_t *wa2, const complex_t *wa3);
58 static void passf4neg(const uint16_t ido, const uint16_t l1, const complex_t *cc, complex_t *ch,
59                       const complex_t *wa1, const complex_t *wa2, const complex_t *wa3);
60 static void passf5(const uint16_t ido, const uint16_t l1, const complex_t *cc, complex_t *ch,
61                    const complex_t *wa1, const complex_t *wa2, const complex_t *wa3,
62                    const complex_t *wa4, const int8_t isign);
63 INLINE void cfftf1(uint16_t n, complex_t *c, complex_t *ch,
64                    const uint16_t *ifac, const complex_t *wa, const int8_t isign);
65 static void cffti1(uint16_t n, complex_t *wa, uint16_t *ifac);
66
67
68 /*----------------------------------------------------------------------
69    passf2, passf3, passf4, passf5. Complex FFT passes fwd and bwd.
70   ----------------------------------------------------------------------*/
71
72 static void passf2pos(const uint16_t ido, const uint16_t l1, const complex_t *cc,
73                       complex_t *ch, const complex_t *wa)
74 {
75     uint16_t i, k, ah, ac;
76
77     if (ido == 1)
78     {
79         for (k = 0; k < l1; k++)
80         {
81             ah = 2*k;
82             ac = 4*k;
83
84             RE(ch[ah])    = RE(cc[ac]) + RE(cc[ac+1]);
85             RE(ch[ah+l1]) = RE(cc[ac]) - RE(cc[ac+1]);
86             IM(ch[ah])    = IM(cc[ac]) + IM(cc[ac+1]);
87             IM(ch[ah+l1]) = IM(cc[ac]) - IM(cc[ac+1]);
88         }
89     } else {
90         for (k = 0; k < l1; k++)
91         {
92             ah = k*ido;
93             ac = 2*k*ido;
94
95             for (i = 0; i < ido; i++)
96             {
97                 complex_t t2;
98
99                 RE(ch[ah+i]) = RE(cc[ac+i]) + RE(cc[ac+i+ido]);
100                 RE(t2)       = RE(cc[ac+i]) - RE(cc[ac+i+ido]);
101
102                 IM(ch[ah+i]) = IM(cc[ac+i]) + IM(cc[ac+i+ido]);
103                 IM(t2)       = IM(cc[ac+i]) - IM(cc[ac+i+ido]);
104
105 #if 1
106                 ComplexMult(&IM(ch[ah+i+l1*ido]), &RE(ch[ah+i+l1*ido]),
107                     IM(t2), RE(t2), RE(wa[i]), IM(wa[i]));
108 #else
109                 ComplexMult(&RE(ch[ah+i+l1*ido]), &IM(ch[ah+i+l1*ido]),
110                     RE(t2), IM(t2), RE(wa[i]), IM(wa[i]));
111 #endif
112             }
113         }
114     }
115 }
116
117 static void passf2neg(const uint16_t ido, const uint16_t l1, const complex_t *cc,
118                       complex_t *ch, const complex_t *wa)
119 {
120     uint16_t i, k, ah, ac;
121
122     if (ido == 1)
123     {
124         for (k = 0; k < l1; k++)
125         {
126             ah = 2*k;
127             ac = 4*k;
128
129             RE(ch[ah])    = RE(cc[ac]) + RE(cc[ac+1]);
130             RE(ch[ah+l1]) = RE(cc[ac]) - RE(cc[ac+1]);
131             IM(ch[ah])    = IM(cc[ac]) + IM(cc[ac+1]);
132             IM(ch[ah+l1]) = IM(cc[ac]) - IM(cc[ac+1]);
133         }
134     } else {
135         for (k = 0; k < l1; k++)
136         {
137             ah = k*ido;
138             ac = 2*k*ido;
139
140             for (i = 0; i < ido; i++)
141             {
142                 complex_t t2;
143
144                 RE(ch[ah+i]) = RE(cc[ac+i]) + RE(cc[ac+i+ido]);
145                 RE(t2)       = RE(cc[ac+i]) - RE(cc[ac+i+ido]);
146
147                 IM(ch[ah+i]) = IM(cc[ac+i]) + IM(cc[ac+i+ido]);
148                 IM(t2)       = IM(cc[ac+i]) - IM(cc[ac+i+ido]);
149
150 #if 1
151                 ComplexMult(&RE(ch[ah+i+l1*ido]), &IM(ch[ah+i+l1*ido]),
152                     RE(t2), IM(t2), RE(wa[i]), IM(wa[i]));
153 #else
154                 ComplexMult(&IM(ch[ah+i+l1*ido]), &RE(ch[ah+i+l1*ido]),
155                     IM(t2), RE(t2), RE(wa[i]), IM(wa[i]));
156 #endif
157             }
158         }
159     }
160 }
161
162
163 static void passf3(const uint16_t ido, const uint16_t l1, const complex_t *cc,
164                    complex_t *ch, const complex_t *wa1, const complex_t *wa2,
165                    const int8_t isign)
166 {
167     static real_t taur = FRAC_CONST(-0.5);
168     static real_t taui = FRAC_CONST(0.866025403784439);
169     uint16_t i, k, ac, ah;
170     complex_t c2, c3, d2, d3, t2;
171
172     if (ido == 1)
173     {
174         if (isign == 1)
175         {
176             for (k = 0; k < l1; k++)
177             {
178                 ac = 3*k+1;
179                 ah = k;
180
181                 RE(t2) = RE(cc[ac]) + RE(cc[ac+1]);
182                 IM(t2) = IM(cc[ac]) + IM(cc[ac+1]);
183                 RE(c2) = RE(cc[ac-1]) + MUL_F(RE(t2),taur);
184                 IM(c2) = IM(cc[ac-1]) + MUL_F(IM(t2),taur);
185
186                 RE(ch[ah]) = RE(cc[ac-1]) + RE(t2);
187                 IM(ch[ah]) = IM(cc[ac-1]) + IM(t2);
188
189                 RE(c3) = MUL_F((RE(cc[ac]) - RE(cc[ac+1])), taui);
190                 IM(c3) = MUL_F((IM(cc[ac]) - IM(cc[ac+1])), taui);
191
192                 RE(ch[ah+l1]) = RE(c2) - IM(c3);
193                 IM(ch[ah+l1]) = IM(c2) + RE(c3);
194                 RE(ch[ah+2*l1]) = RE(c2) + IM(c3);
195                 IM(ch[ah+2*l1]) = IM(c2) - RE(c3);
196             }
197         } else {
198             for (k = 0; k < l1; k++)
199             {
200                 ac = 3*k+1;
201                 ah = k;
202
203                 RE(t2) = RE(cc[ac]) + RE(cc[ac+1]);
204                 IM(t2) = IM(cc[ac]) + IM(cc[ac+1]);
205                 RE(c2) = RE(cc[ac-1]) + MUL_F(RE(t2),taur);
206                 IM(c2) = IM(cc[ac-1]) + MUL_F(IM(t2),taur);
207
208                 RE(ch[ah]) = RE(cc[ac-1]) + RE(t2);
209                 IM(ch[ah]) = IM(cc[ac-1]) + IM(t2);
210
211                 RE(c3) = MUL_F((RE(cc[ac]) - RE(cc[ac+1])), taui);
212                 IM(c3) = MUL_F((IM(cc[ac]) - IM(cc[ac+1])), taui);
213
214                 RE(ch[ah+l1]) = RE(c2) + IM(c3);
215                 IM(ch[ah+l1]) = IM(c2) - RE(c3);
216                 RE(ch[ah+2*l1]) = RE(c2) - IM(c3);
217                 IM(ch[ah+2*l1]) = IM(c2) + RE(c3);
218             }
219         }
220     } else {
221         if (isign == 1)
222         {
223             for (k = 0; k < l1; k++)
224             {
225                 for (i = 0; i < ido; i++)
226                 {
227                     ac = i + (3*k+1)*ido;
228                     ah = i + k * ido;
229
230                     RE(t2) = RE(cc[ac]) + RE(cc[ac+ido]);
231                     RE(c2) = RE(cc[ac-ido]) + MUL_F(RE(t2),taur);
232                     IM(t2) = IM(cc[ac]) + IM(cc[ac+ido]);
233                     IM(c2) = IM(cc[ac-ido]) + MUL_F(IM(t2),taur);
234
235                     RE(ch[ah]) = RE(cc[ac-ido]) + RE(t2);
236                     IM(ch[ah]) = IM(cc[ac-ido]) + IM(t2);
237
238                     RE(c3) = MUL_F((RE(cc[ac]) - RE(cc[ac+ido])), taui);
239                     IM(c3) = MUL_F((IM(cc[ac]) - IM(cc[ac+ido])), taui);
240
241                     RE(d2) = RE(c2) - IM(c3);
242                     IM(d3) = IM(c2) - RE(c3);
243                     RE(d3) = RE(c2) + IM(c3);
244                     IM(d2) = IM(c2) + RE(c3);
245
246 #if 1
247                     ComplexMult(&IM(ch[ah+l1*ido]), &RE(ch[ah+l1*ido]),
248                         IM(d2), RE(d2), RE(wa1[i]), IM(wa1[i]));
249                     ComplexMult(&IM(ch[ah+2*l1*ido]), &RE(ch[ah+2*l1*ido]),
250                         IM(d3), RE(d3), RE(wa2[i]), IM(wa2[i]));
251 #else
252                     ComplexMult(&RE(ch[ah+l1*ido]), &IM(ch[ah+l1*ido]),
253                         RE(d2), IM(d2), RE(wa1[i]), IM(wa1[i]));
254                     ComplexMult(&RE(ch[ah+2*l1*ido]), &IM(ch[ah+2*l1*ido]),
255                         RE(d3), IM(d3), RE(wa2[i]), IM(wa2[i]));
256 #endif
257                 }
258             }
259         } else {
260             for (k = 0; k < l1; k++)
261             {
262                 for (i = 0; i < ido; i++)
263                 {
264                     ac = i + (3*k+1)*ido;
265                     ah = i + k * ido;
266
267                     RE(t2) = RE(cc[ac]) + RE(cc[ac+ido]);
268                     RE(c2) = RE(cc[ac-ido]) + MUL_F(RE(t2),taur);
269                     IM(t2) = IM(cc[ac]) + IM(cc[ac+ido]);
270                     IM(c2) = IM(cc[ac-ido]) + MUL_F(IM(t2),taur);
271
272                     RE(ch[ah]) = RE(cc[ac-ido]) + RE(t2);
273                     IM(ch[ah]) = IM(cc[ac-ido]) + IM(t2);
274
275                     RE(c3) = MUL_F((RE(cc[ac]) - RE(cc[ac+ido])), taui);
276                     IM(c3) = MUL_F((IM(cc[ac]) - IM(cc[ac+ido])), taui);
277
278                     RE(d2) = RE(c2) + IM(c3);
279                     IM(d3) = IM(c2) + RE(c3);
280                     RE(d3) = RE(c2) - IM(c3);
281                     IM(d2) = IM(c2) - RE(c3);
282
283 #if 1
284                     ComplexMult(&RE(ch[ah+l1*ido]), &IM(ch[ah+l1*ido]),
285                         RE(d2), IM(d2), RE(wa1[i]), IM(wa1[i]));
286                     ComplexMult(&RE(ch[ah+2*l1*ido]), &IM(ch[ah+2*l1*ido]),
287                         RE(d3), IM(d3), RE(wa2[i]), IM(wa2[i]));
288 #else
289                     ComplexMult(&IM(ch[ah+l1*ido]), &RE(ch[ah+l1*ido]),
290                         IM(d2), RE(d2), RE(wa1[i]), IM(wa1[i]));
291                     ComplexMult(&IM(ch[ah+2*l1*ido]), &RE(ch[ah+2*l1*ido]),
292                         IM(d3), RE(d3), RE(wa2[i]), IM(wa2[i]));
293 #endif
294                 }
295             }
296         }
297     }
298 }
299
300
301 static void passf4pos(const uint16_t ido, const uint16_t l1, const complex_t *cc,
302                       complex_t *ch, const complex_t *wa1, const complex_t *wa2,
303                       const complex_t *wa3)
304 {
305     uint16_t i, k, ac, ah;
306
307     if (ido == 1)
308     {
309         for (k = 0; k < l1; k++)
310         {
311             complex_t t1, t2, t3, t4;
312
313             ac = 4*k;
314             ah = k;
315
316             RE(t2) = RE(cc[ac])   + RE(cc[ac+2]);
317             RE(t1) = RE(cc[ac])   - RE(cc[ac+2]);
318             IM(t2) = IM(cc[ac])   + IM(cc[ac+2]);
319             IM(t1) = IM(cc[ac])   - IM(cc[ac+2]);
320             RE(t3) = RE(cc[ac+1]) + RE(cc[ac+3]);
321             IM(t4) = RE(cc[ac+1]) - RE(cc[ac+3]);
322             IM(t3) = IM(cc[ac+3]) + IM(cc[ac+1]);
323             RE(t4) = IM(cc[ac+3]) - IM(cc[ac+1]);
324
325             RE(ch[ah])      = RE(t2) + RE(t3);
326             RE(ch[ah+2*l1]) = RE(t2) - RE(t3);
327
328             IM(ch[ah])      = IM(t2) + IM(t3);
329             IM(ch[ah+2*l1]) = IM(t2) - IM(t3);
330
331             RE(ch[ah+l1])   = RE(t1) + RE(t4);
332             RE(ch[ah+3*l1]) = RE(t1) - RE(t4);
333
334             IM(ch[ah+l1])   = IM(t1) + IM(t4);
335             IM(ch[ah+3*l1]) = IM(t1) - IM(t4);
336         }
337     } else {
338         for (k = 0; k < l1; k++)
339         {
340             ac = 4*k*ido;
341             ah = k*ido;
342
343             for (i = 0; i < ido; i++)
344             {
345                 complex_t c2, c3, c4, t1, t2, t3, t4;
346
347                 RE(t2) = RE(cc[ac+i]) + RE(cc[ac+i+2*ido]);
348                 RE(t1) = RE(cc[ac+i]) - RE(cc[ac+i+2*ido]);
349                 IM(t2) = IM(cc[ac+i]) + IM(cc[ac+i+2*ido]);
350                 IM(t1) = IM(cc[ac+i]) - IM(cc[ac+i+2*ido]);
351                 RE(t3) = RE(cc[ac+i+ido]) + RE(cc[ac+i+3*ido]);
352                 IM(t4) = RE(cc[ac+i+ido]) - RE(cc[ac+i+3*ido]);
353                 IM(t3) = IM(cc[ac+i+3*ido]) + IM(cc[ac+i+ido]);
354                 RE(t4) = IM(cc[ac+i+3*ido]) - IM(cc[ac+i+ido]);
355
356                 RE(c2) = RE(t1) + RE(t4);
357                 RE(c4) = RE(t1) - RE(t4);
358
359                 IM(c2) = IM(t1) + IM(t4);
360                 IM(c4) = IM(t1) - IM(t4);
361
362                 RE(ch[ah+i]) = RE(t2) + RE(t3);
363                 RE(c3)       = RE(t2) - RE(t3);
364
365                 IM(ch[ah+i]) = IM(t2) + IM(t3);
366                 IM(c3)       = IM(t2) - IM(t3);
367
368 #if 1
369                 ComplexMult(&IM(ch[ah+i+l1*ido]), &RE(ch[ah+i+l1*ido]),
370                     IM(c2), RE(c2), RE(wa1[i]), IM(wa1[i]));
371                 ComplexMult(&IM(ch[ah+i+2*l1*ido]), &RE(ch[ah+i+2*l1*ido]),
372                     IM(c3), RE(c3), RE(wa2[i]), IM(wa2[i]));
373                 ComplexMult(&IM(ch[ah+i+3*l1*ido]), &RE(ch[ah+i+3*l1*ido]),
374                     IM(c4), RE(c4), RE(wa3[i]), IM(wa3[i]));
375 #else
376                 ComplexMult(&RE(ch[ah+i+l1*ido]), &IM(ch[ah+i+l1*ido]),
377                     RE(c2), IM(c2), RE(wa1[i]), IM(wa1[i]));
378                 ComplexMult(&RE(ch[ah+i+2*l1*ido]), &IM(ch[ah+i+2*l1*ido]),
379                     RE(c3), IM(c3), RE(wa2[i]), IM(wa2[i]));
380                 ComplexMult(&RE(ch[ah+i+3*l1*ido]), &IM(ch[ah+i+3*l1*ido]),
381                     RE(c4), IM(c4), RE(wa3[i]), IM(wa3[i]));
382 #endif
383             }
384         }
385     }
386 }
387
388 static void passf4neg(const uint16_t ido, const uint16_t l1, const complex_t *cc,
389                       complex_t *ch, const complex_t *wa1, const complex_t *wa2,
390                       const complex_t *wa3)
391 {
392     uint16_t i, k, ac, ah;
393
394     if (ido == 1)
395     {
396         for (k = 0; k < l1; k++)
397         {
398             complex_t t1, t2, t3, t4;
399
400             ac = 4*k;
401             ah = k;
402
403             RE(t2) = RE(cc[ac])   + RE(cc[ac+2]);
404             RE(t1) = RE(cc[ac])   - RE(cc[ac+2]);
405             IM(t2) = IM(cc[ac])   + IM(cc[ac+2]);
406             IM(t1) = IM(cc[ac])   - IM(cc[ac+2]);
407             RE(t3) = RE(cc[ac+1]) + RE(cc[ac+3]);
408             IM(t4) = RE(cc[ac+1]) - RE(cc[ac+3]);
409             IM(t3) = IM(cc[ac+3]) + IM(cc[ac+1]);
410             RE(t4) = IM(cc[ac+3]) - IM(cc[ac+1]);
411
412             RE(ch[ah])      = RE(t2) + RE(t3);
413             RE(ch[ah+2*l1]) = RE(t2) - RE(t3);
414
415             IM(ch[ah])      = IM(t2) + IM(t3);
416             IM(ch[ah+2*l1]) = IM(t2) - IM(t3);
417
418             RE(ch[ah+l1])   = RE(t1) - RE(t4);
419             RE(ch[ah+3*l1]) = RE(t1) + RE(t4);
420
421             IM(ch[ah+l1])   = IM(t1) - IM(t4);
422             IM(ch[ah+3*l1]) = IM(t1) + IM(t4);
423         }
424     } else {
425         for (k = 0; k < l1; k++)
426         {
427             ac = 4*k*ido;
428             ah = k*ido;
429
430             for (i = 0; i < ido; i++)
431             {
432                 complex_t c2, c3, c4, t1, t2, t3, t4;
433
434                 RE(t2) = RE(cc[ac+i]) + RE(cc[ac+i+2*ido]);
435                 RE(t1) = RE(cc[ac+i]) - RE(cc[ac+i+2*ido]);
436                 IM(t2) = IM(cc[ac+i]) + IM(cc[ac+i+2*ido]);
437                 IM(t1) = IM(cc[ac+i]) - IM(cc[ac+i+2*ido]);
438                 RE(t3) = RE(cc[ac+i+ido]) + RE(cc[ac+i+3*ido]);
439                 IM(t4) = RE(cc[ac+i+ido]) - RE(cc[ac+i+3*ido]);
440                 IM(t3) = IM(cc[ac+i+3*ido]) + IM(cc[ac+i+ido]);
441                 RE(t4) = IM(cc[ac+i+3*ido]) - IM(cc[ac+i+ido]);
442
443                 RE(c2) = RE(t1) - RE(t4);
444                 RE(c4) = RE(t1) + RE(t4);
445
446                 IM(c2) = IM(t1) - IM(t4);
447                 IM(c4) = IM(t1) + IM(t4);
448
449                 RE(ch[ah+i]) = RE(t2) + RE(t3);
450                 RE(c3)       = RE(t2) - RE(t3);
451
452                 IM(ch[ah+i]) = IM(t2) + IM(t3);
453                 IM(c3)       = IM(t2) - IM(t3);
454
455 #if 1
456                 ComplexMult(&RE(ch[ah+i+l1*ido]), &IM(ch[ah+i+l1*ido]),
457                     RE(c2), IM(c2), RE(wa1[i]), IM(wa1[i]));
458                 ComplexMult(&RE(ch[ah+i+2*l1*ido]), &IM(ch[ah+i+2*l1*ido]),
459                     RE(c3), IM(c3), RE(wa2[i]), IM(wa2[i]));
460                 ComplexMult(&RE(ch[ah+i+3*l1*ido]), &IM(ch[ah+i+3*l1*ido]),
461                     RE(c4), IM(c4), RE(wa3[i]), IM(wa3[i]));
462 #else
463                 ComplexMult(&IM(ch[ah+i+l1*ido]), &RE(ch[ah+i+l1*ido]),
464                     IM(c2), RE(c2), RE(wa1[i]), IM(wa1[i]));
465                 ComplexMult(&IM(ch[ah+i+2*l1*ido]), &RE(ch[ah+i+2*l1*ido]),
466                     IM(c3), RE(c3), RE(wa2[i]), IM(wa2[i]));
467                 ComplexMult(&IM(ch[ah+i+3*l1*ido]), &RE(ch[ah+i+3*l1*ido]),
468                     IM(c4), RE(c4), RE(wa3[i]), IM(wa3[i]));
469 #endif
470             }
471         }
472     }
473 }
474
475 static void passf5(const uint16_t ido, const uint16_t l1, const complex_t *cc,
476                    complex_t *ch, const complex_t *wa1, const complex_t *wa2, const complex_t *wa3,
477                    const complex_t *wa4, const int8_t isign)
478 {
479     static real_t tr11 = FRAC_CONST(0.309016994374947);
480     static real_t ti11 = FRAC_CONST(0.951056516295154);
481     static real_t tr12 = FRAC_CONST(-0.809016994374947);
482     static real_t ti12 = FRAC_CONST(0.587785252292473);
483     uint16_t i, k, ac, ah;
484     complex_t c2, c3, c4, c5, d3, d4, d5, d2, t2, t3, t4, t5;
485
486     if (ido == 1)
487     {
488         if (isign == 1)
489         {
490             for (k = 0; k < l1; k++)
491             {
492                 ac = 5*k + 1;
493                 ah = k;
494
495                 RE(t2) = RE(cc[ac]) + RE(cc[ac+3]);
496                 IM(t2) = IM(cc[ac]) + IM(cc[ac+3]);
497                 RE(t3) = RE(cc[ac+1]) + RE(cc[ac+2]);
498                 IM(t3) = IM(cc[ac+1]) + IM(cc[ac+2]);
499                 RE(t4) = RE(cc[ac+1]) - RE(cc[ac+2]);
500                 IM(t4) = IM(cc[ac+1]) - IM(cc[ac+2]);
501                 RE(t5) = RE(cc[ac]) - RE(cc[ac+3]);
502                 IM(t5) = IM(cc[ac]) - IM(cc[ac+3]);
503
504                 RE(ch[ah]) = RE(cc[ac-1]) + RE(t2) + RE(t3);
505                 IM(ch[ah]) = IM(cc[ac-1]) + IM(t2) + IM(t3);
506
507                 RE(c2) = RE(cc[ac-1]) + MUL_F(RE(t2),tr11) + MUL_F(RE(t3),tr12);
508                 IM(c2) = IM(cc[ac-1]) + MUL_F(IM(t2),tr11) + MUL_F(IM(t3),tr12);
509                 RE(c3) = RE(cc[ac-1]) + MUL_F(RE(t2),tr12) + MUL_F(RE(t3),tr11);
510                 IM(c3) = IM(cc[ac-1]) + MUL_F(IM(t2),tr12) + MUL_F(IM(t3),tr11);
511
512                 ComplexMult(&RE(c5), &RE(c4),
513                     ti11, ti12, RE(t5), RE(t4));
514                 ComplexMult(&IM(c5), &IM(c4),
515                     ti11, ti12, IM(t5), IM(t4));
516
517                 RE(ch[ah+l1]) = RE(c2) - IM(c5);
518                 IM(ch[ah+l1]) = IM(c2) + RE(c5);
519                 RE(ch[ah+2*l1]) = RE(c3) - IM(c4);
520                 IM(ch[ah+2*l1]) = IM(c3) + RE(c4);
521                 RE(ch[ah+3*l1]) = RE(c3) + IM(c4);
522                 IM(ch[ah+3*l1]) = IM(c3) - RE(c4);
523                 RE(ch[ah+4*l1]) = RE(c2) + IM(c5);
524                 IM(ch[ah+4*l1]) = IM(c2) - RE(c5);
525             }
526         } else {
527             for (k = 0; k < l1; k++)
528             {
529                 ac = 5*k + 1;
530                 ah = k;
531
532                 RE(t2) = RE(cc[ac]) + RE(cc[ac+3]);
533                 IM(t2) = IM(cc[ac]) + IM(cc[ac+3]);
534                 RE(t3) = RE(cc[ac+1]) + RE(cc[ac+2]);
535                 IM(t3) = IM(cc[ac+1]) + IM(cc[ac+2]);
536                 RE(t4) = RE(cc[ac+1]) - RE(cc[ac+2]);
537                 IM(t4) = IM(cc[ac+1]) - IM(cc[ac+2]);
538                 RE(t5) = RE(cc[ac]) - RE(cc[ac+3]);
539                 IM(t5) = IM(cc[ac]) - IM(cc[ac+3]);
540
541                 RE(ch[ah]) = RE(cc[ac-1]) + RE(t2) + RE(t3);
542                 IM(ch[ah]) = IM(cc[ac-1]) + IM(t2) + IM(t3);
543
544                 RE(c2) = RE(cc[ac-1]) + MUL_F(RE(t2),tr11) + MUL_F(RE(t3),tr12);
545                 IM(c2) = IM(cc[ac-1]) + MUL_F(IM(t2),tr11) + MUL_F(IM(t3),tr12);
546                 RE(c3) = RE(cc[ac-1]) + MUL_F(RE(t2),tr12) + MUL_F(RE(t3),tr11);
547                 IM(c3) = IM(cc[ac-1]) + MUL_F(IM(t2),tr12) + MUL_F(IM(t3),tr11);
548
549                 ComplexMult(&RE(c4), &RE(c5),
550                     ti12, ti11, RE(t5), RE(t4));
551                 ComplexMult(&IM(c4), &IM(c5),
552                     ti12, ti11, IM(t5), IM(t4));
553
554                 RE(ch[ah+l1]) = RE(c2) + IM(c5);
555                 IM(ch[ah+l1]) = IM(c2) - RE(c5);
556                 RE(ch[ah+2*l1]) = RE(c3) + IM(c4);
557                 IM(ch[ah+2*l1]) = IM(c3) - RE(c4);
558                 RE(ch[ah+3*l1]) = RE(c3) - IM(c4);
559                 IM(ch[ah+3*l1]) = IM(c3) + RE(c4);
560                 RE(ch[ah+4*l1]) = RE(c2) - IM(c5);
561                 IM(ch[ah+4*l1]) = IM(c2) + RE(c5);
562             }
563         }
564     } else {
565         if (isign == 1)
566         {
567             for (k = 0; k < l1; k++)
568             {
569                 for (i = 0; i < ido; i++)
570                 {
571                     ac = i + (k*5 + 1) * ido;
572                     ah = i + k * ido;
573
574                     RE(t2) = RE(cc[ac]) + RE(cc[ac+3*ido]);
575                     IM(t2) = IM(cc[ac]) + IM(cc[ac+3*ido]);
576                     RE(t3) = RE(cc[ac+ido]) + RE(cc[ac+2*ido]);
577                     IM(t3) = IM(cc[ac+ido]) + IM(cc[ac+2*ido]);
578                     RE(t4) = RE(cc[ac+ido]) - RE(cc[ac+2*ido]);
579                     IM(t4) = IM(cc[ac+ido]) - IM(cc[ac+2*ido]);
580                     RE(t5) = RE(cc[ac]) - RE(cc[ac+3*ido]);
581                     IM(t5) = IM(cc[ac]) - IM(cc[ac+3*ido]);
582
583                     RE(ch[ah]) = RE(cc[ac-ido]) + RE(t2) + RE(t3);
584                     IM(ch[ah]) = IM(cc[ac-ido]) + IM(t2) + IM(t3);
585
586                     RE(c2) = RE(cc[ac-ido]) + MUL_F(RE(t2),tr11) + MUL_F(RE(t3),tr12);
587                     IM(c2) = IM(cc[ac-ido]) + MUL_F(IM(t2),tr11) + MUL_F(IM(t3),tr12);
588                     RE(c3) = RE(cc[ac-ido]) + MUL_F(RE(t2),tr12) + MUL_F(RE(t3),tr11);
589                     IM(c3) = IM(cc[ac-ido]) + MUL_F(IM(t2),tr12) + MUL_F(IM(t3),tr11);
590
591                     ComplexMult(&RE(c5), &RE(c4),
592                         ti11, ti12, RE(t5), RE(t4));
593                     ComplexMult(&IM(c5), &IM(c4),
594                         ti11, ti12, IM(t5), IM(t4));
595
596                     IM(d2) = IM(c2) + RE(c5);
597                     IM(d3) = IM(c3) + RE(c4);
598                     RE(d4) = RE(c3) + IM(c4);
599                     RE(d5) = RE(c2) + IM(c5);
600                     RE(d2) = RE(c2) - IM(c5);
601                     IM(d5) = IM(c2) - RE(c5);
602                     RE(d3) = RE(c3) - IM(c4);
603                     IM(d4) = IM(c3) - RE(c4);
604
605 #if 1
606                     ComplexMult(&IM(ch[ah+l1*ido]), &RE(ch[ah+l1*ido]),
607                         IM(d2), RE(d2), RE(wa1[i]), IM(wa1[i]));
608                     ComplexMult(&IM(ch[ah+2*l1*ido]), &RE(ch[ah+2*l1*ido]),
609                         IM(d3), RE(d3), RE(wa2[i]), IM(wa2[i]));
610                     ComplexMult(&IM(ch[ah+3*l1*ido]), &RE(ch[ah+3*l1*ido]),
611                         IM(d4), RE(d4), RE(wa3[i]), IM(wa3[i]));
612                     ComplexMult(&IM(ch[ah+4*l1*ido]), &RE(ch[ah+4*l1*ido]),
613                         IM(d5), RE(d5), RE(wa4[i]), IM(wa4[i]));
614 #else
615                     ComplexMult(&RE(ch[ah+l1*ido]), &IM(ch[ah+l1*ido]),
616                         RE(d2), IM(d2), RE(wa1[i]), IM(wa1[i]));
617                     ComplexMult(&RE(ch[ah+2*l1*ido]), &IM(ch[ah+2*l1*ido]),
618                         RE(d3), IM(d3), RE(wa2[i]), IM(wa2[i]));
619                     ComplexMult(&RE(ch[ah+3*l1*ido]), &IM(ch[ah+3*l1*ido]),
620                         RE(d4), IM(d4), RE(wa3[i]), IM(wa3[i]));
621                     ComplexMult(&RE(ch[ah+4*l1*ido]), &IM(ch[ah+4*l1*ido]),
622                         RE(d5), IM(d5), RE(wa4[i]), IM(wa4[i]));
623 #endif
624                 }
625             }
626         } else {
627             for (k = 0; k < l1; k++)
628             {
629                 for (i = 0; i < ido; i++)
630                 {
631                     ac = i + (k*5 + 1) * ido;
632                     ah = i + k * ido;
633
634                     RE(t2) = RE(cc[ac]) + RE(cc[ac+3*ido]);
635                     IM(t2) = IM(cc[ac]) + IM(cc[ac+3*ido]);
636                     RE(t3) = RE(cc[ac+ido]) + RE(cc[ac+2*ido]);
637                     IM(t3) = IM(cc[ac+ido]) + IM(cc[ac+2*ido]);
638                     RE(t4) = RE(cc[ac+ido]) - RE(cc[ac+2*ido]);
639                     IM(t4) = IM(cc[ac+ido]) - IM(cc[ac+2*ido]);
640                     RE(t5) = RE(cc[ac]) - RE(cc[ac+3*ido]);
641                     IM(t5) = IM(cc[ac]) - IM(cc[ac+3*ido]);
642
643                     RE(ch[ah]) = RE(cc[ac-ido]) + RE(t2) + RE(t3);
644                     IM(ch[ah]) = IM(cc[ac-ido]) + IM(t2) + IM(t3);
645
646                     RE(c2) = RE(cc[ac-ido]) + MUL_F(RE(t2),tr11) + MUL_F(RE(t3),tr12);
647                     IM(c2) = IM(cc[ac-ido]) + MUL_F(IM(t2),tr11) + MUL_F(IM(t3),tr12);
648                     RE(c3) = RE(cc[ac-ido]) + MUL_F(RE(t2),tr12) + MUL_F(RE(t3),tr11);
649                     IM(c3) = IM(cc[ac-ido]) + MUL_F(IM(t2),tr12) + MUL_F(IM(t3),tr11);
650
651                     ComplexMult(&RE(c4), &RE(c5),
652                         ti12, ti11, RE(t5), RE(t4));
653                     ComplexMult(&IM(c4), &IM(c5),
654                         ti12, ti11, IM(t5), IM(t4));
655
656                     IM(d2) = IM(c2) - RE(c5);
657                     IM(d3) = IM(c3) - RE(c4);
658                     RE(d4) = RE(c3) - IM(c4);
659                     RE(d5) = RE(c2) - IM(c5);
660                     RE(d2) = RE(c2) + IM(c5);
661                     IM(d5) = IM(c2) + RE(c5);
662                     RE(d3) = RE(c3) + IM(c4);
663                     IM(d4) = IM(c3) + RE(c4);
664
665 #if 1
666                     ComplexMult(&RE(ch[ah+l1*ido]), &IM(ch[ah+l1*ido]),
667                         RE(d2), IM(d2), RE(wa1[i]), IM(wa1[i]));
668                     ComplexMult(&RE(ch[ah+2*l1*ido]), &IM(ch[ah+2*l1*ido]),
669                         RE(d3), IM(d3), RE(wa2[i]), IM(wa2[i]));
670                     ComplexMult(&RE(ch[ah+3*l1*ido]), &IM(ch[ah+3*l1*ido]),
671                         RE(d4), IM(d4), RE(wa3[i]), IM(wa3[i]));
672                     ComplexMult(&RE(ch[ah+4*l1*ido]), &IM(ch[ah+4*l1*ido]),
673                         RE(d5), IM(d5), RE(wa4[i]), IM(wa4[i]));
674 #else
675                     ComplexMult(&IM(ch[ah+l1*ido]), &RE(ch[ah+l1*ido]),
676                         IM(d2), RE(d2), RE(wa1[i]), IM(wa1[i]));
677                     ComplexMult(&IM(ch[ah+2*l1*ido]), &RE(ch[ah+2*l1*ido]),
678                         IM(d3), RE(d3), RE(wa2[i]), IM(wa2[i]));
679                     ComplexMult(&IM(ch[ah+3*l1*ido]), &RE(ch[ah+3*l1*ido]),
680                         IM(d4), RE(d4), RE(wa3[i]), IM(wa3[i]));
681                     ComplexMult(&IM(ch[ah+4*l1*ido]), &RE(ch[ah+4*l1*ido]),
682                         IM(d5), RE(d5), RE(wa4[i]), IM(wa4[i]));
683 #endif
684                 }
685             }
686         }
687     }
688 }
689
690
691 /*----------------------------------------------------------------------
692    cfftf1, cfftf, cfftb, cffti1, cffti. Complex FFTs.
693   ----------------------------------------------------------------------*/
694
695 static INLINE void cfftf1pos(uint16_t n, complex_t *c, complex_t *ch,
696                              const uint16_t *ifac, const complex_t *wa,
697                              const int8_t isign)
698 {
699     uint16_t i;
700     uint16_t k1, l1, l2;
701     uint16_t na, nf, ip, iw, ix2, ix3, ix4, ido, idl1;
702
703     nf = ifac[1];
704     na = 0;
705     l1 = 1;
706     iw = 0;
707
708     for (k1 = 2; k1 <= nf+1; k1++)
709     {
710         ip = ifac[k1];
711         l2 = ip*l1;
712         ido = n / l2;
713         idl1 = ido*l1;
714
715         switch (ip)
716         {
717         case 4:
718             ix2 = iw + ido;
719             ix3 = ix2 + ido;
720
721             if (na == 0)
722                 passf4pos((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)c, ch, &wa[iw], &wa[ix2], &wa[ix3]);
723             else
724                 passf4pos((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)ch, c, &wa[iw], &wa[ix2], &wa[ix3]);
725
726             na = 1 - na;
727             break;
728         case 2:
729             if (na == 0)
730                 passf2pos((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)c, ch, &wa[iw]);
731             else
732                 passf2pos((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)ch, c, &wa[iw]);
733
734             na = 1 - na;
735             break;
736         case 3:
737             ix2 = iw + ido;
738
739             if (na == 0)
740                 passf3((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)c, ch, &wa[iw], &wa[ix2], isign);
741             else
742                 passf3((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)ch, c, &wa[iw], &wa[ix2], isign);
743
744             na = 1 - na;
745             break;
746         case 5:
747             ix2 = iw + ido;
748             ix3 = ix2 + ido;
749             ix4 = ix3 + ido;
750
751             if (na == 0)
752                 passf5((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)c, ch, &wa[iw], &wa[ix2], &wa[ix3], &wa[ix4], isign);
753             else
754                 passf5((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)ch, c, &wa[iw], &wa[ix2], &wa[ix3], &wa[ix4], isign);
755
756             na = 1 - na;
757             break;
758         }
759
760         l1 = l2;
761         iw += (ip-1) * ido;
762     }
763
764     if (na == 0)
765         return;
766
767     for (i = 0; i < n; i++)
768     {
769         RE(c[i]) = RE(ch[i]);
770         IM(c[i]) = IM(ch[i]);
771     }
772 }
773
774 static INLINE void cfftf1neg(uint16_t n, complex_t *c, complex_t *ch,
775                              const uint16_t *ifac, const complex_t *wa,
776                              const int8_t isign)
777 {
778     uint16_t i;
779     uint16_t k1, l1, l2;
780     uint16_t na, nf, ip, iw, ix2, ix3, ix4, ido, idl1;
781
782     nf = ifac[1];
783     na = 0;
784     l1 = 1;
785     iw = 0;
786
787     for (k1 = 2; k1 <= nf+1; k1++)
788     {
789         ip = ifac[k1];
790         l2 = ip*l1;
791         ido = n / l2;
792         idl1 = ido*l1;
793
794         switch (ip)
795         {
796         case 4:
797             ix2 = iw + ido;
798             ix3 = ix2 + ido;
799
800             if (na == 0)
801                 passf4neg((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)c, ch, &wa[iw], &wa[ix2], &wa[ix3]);
802             else
803                 passf4neg((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)ch, c, &wa[iw], &wa[ix2], &wa[ix3]);
804
805             na = 1 - na;
806             break;
807         case 2:
808             if (na == 0)
809                 passf2neg((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)c, ch, &wa[iw]);
810             else
811                 passf2neg((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)ch, c, &wa[iw]);
812
813             na = 1 - na;
814             break;
815         case 3:
816             ix2 = iw + ido;
817
818             if (na == 0)
819                 passf3((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)c, ch, &wa[iw], &wa[ix2], isign);
820             else
821                 passf3((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)ch, c, &wa[iw], &wa[ix2], isign);
822
823             na = 1 - na;
824             break;
825         case 5:
826             ix2 = iw + ido;
827             ix3 = ix2 + ido;
828             ix4 = ix3 + ido;
829
830             if (na == 0)
831                 passf5((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)c, ch, &wa[iw], &wa[ix2], &wa[ix3], &wa[ix4], isign);
832             else
833                 passf5((const uint16_t)ido, (const uint16_t)l1, (const complex_t*)ch, c, &wa[iw], &wa[ix2], &wa[ix3], &wa[ix4], isign);
834
835             na = 1 - na;
836             break;
837         }
838
839         l1 = l2;
840         iw += (ip-1) * ido;
841     }
842
843     if (na == 0)
844         return;
845
846     for (i = 0; i < n; i++)
847     {
848         RE(c[i]) = RE(ch[i]);
849         IM(c[i]) = IM(ch[i]);
850     }
851 }
852
853 void cfftf(cfft_info *cfft, complex_t *c)
854 {
855     cfftf1neg(cfft->n, c, cfft->work, (const uint16_t*)cfft->ifac, (const complex_t*)cfft->tab, -1);
856 }
857
858 void cfftb(cfft_info *cfft, complex_t *c)
859 {
860     cfftf1pos(cfft->n, c, cfft->work, (const uint16_t*)cfft->ifac, (const complex_t*)cfft->tab, +1);
861 }
862
863 static void cffti1(uint16_t n, complex_t *wa, uint16_t *ifac)
864 {
865     static uint16_t ntryh[4] = {3, 4, 2, 5};
866 #ifndef FIXED_POINT
867     real_t arg, argh, argld, fi;
868     uint16_t ido, ipm;
869     uint16_t i1, k1, l1, l2;
870     uint16_t ld, ii, ip;
871 #endif
872     uint16_t ntry = 0, i, j;
873     uint16_t ib;
874     uint16_t nf, nl, nq, nr;
875
876     nl = n;
877     nf = 0;
878     j = 0;
879
880 startloop:
881     j++;
882
883     if (j <= 4)
884         ntry = ntryh[j-1];
885     else
886         ntry += 2;
887
888     do
889     {
890         nq = nl / ntry;
891         nr = nl - ntry*nq;
892
893         if (nr != 0)
894             goto startloop;
895
896         nf++;
897         ifac[nf+1] = ntry;
898         nl = nq;
899
900         if (ntry == 2 && nf != 1)
901         {
902             for (i = 2; i <= nf; i++)
903             {
904                 ib = nf - i + 2;
905                 ifac[ib+1] = ifac[ib];
906             }
907             ifac[2] = 2;
908         }
909     } while (nl != 1);
910
911     ifac[0] = n;
912     ifac[1] = nf;
913
914 #ifndef FIXED_POINT
915     argh = (real_t)2.0*(real_t)M_PI / (real_t)n;
916     i = 0;
917     l1 = 1;
918
919     for (k1 = 1; k1 <= nf; k1++)
920     {
921         ip = ifac[k1+1];
922         ld = 0;
923         l2 = l1*ip;
924         ido = n / l2;
925         ipm = ip - 1;
926
927         for (j = 0; j < ipm; j++)
928         {
929             i1 = i;
930             RE(wa[i]) = 1.0;
931             IM(wa[i]) = 0.0;
932             ld += l1;
933             fi = 0;
934             argld = ld*argh;
935
936             for (ii = 0; ii < ido; ii++)
937             {
938                 i++;
939                 fi++;
940                 arg = fi * argld;
941                 RE(wa[i]) = (real_t)cos(arg);
942 #if 1
943                 IM(wa[i]) = (real_t)sin(arg);
944 #else
945                 IM(wa[i]) = (real_t)-sin(arg);
946 #endif
947             }
948
949             if (ip > 5)
950             {
951                 RE(wa[i1]) = RE(wa[i]);
952                 IM(wa[i1]) = IM(wa[i]);
953             }
954         }
955         l1 = l2;
956     }
957 #endif
958 }
959
960 cfft_info *cffti(uint16_t n)
961 {
962     cfft_info *cfft = (cfft_info*)faad_malloc(sizeof(cfft_info));
963
964     cfft->n = n;
965     cfft->work = (complex_t*)faad_malloc(n*sizeof(complex_t));
966
967 #ifndef FIXED_POINT
968     cfft->tab = (complex_t*)faad_malloc(n*sizeof(complex_t));
969
970     cffti1(n, cfft->tab, cfft->ifac);
971 #else
972     cffti1(n, NULL, cfft->ifac);
973
974     switch (n)
975     {
976     case 64: cfft->tab = (complex_t*)cfft_tab_64; break;
977     case 512: cfft->tab = (complex_t*)cfft_tab_512; break;
978 #ifdef LD_DEC
979     case 256: cfft->tab = (complex_t*)cfft_tab_256; break;
980 #endif
981
982 #ifdef ALLOW_SMALL_FRAMELENGTH
983     case 60: cfft->tab = (complex_t*)cfft_tab_60; break;
984     case 480: cfft->tab = (complex_t*)cfft_tab_480; break;
985 #ifdef LD_DEC
986     case 240: cfft->tab = (complex_t*)cfft_tab_240; break;
987 #endif
988 #endif
989     case 128: cfft->tab = (complex_t*)cfft_tab_128; break;
990     }
991 #endif
992
993     return cfft;
994 }
995
996 void cfftu(cfft_info *cfft)
997 {
998     if (cfft->work) faad_free(cfft->work);
999 #ifndef FIXED_POINT
1000     if (cfft->tab) faad_free(cfft->tab);
1001 #endif
1002
1003     if (cfft) faad_free(cfft);
1004 }
1005