1 /*Copyright (c) 2003-2004, Mark Borgerding
2   Lots of modifications by Jean-Marc Valin
3   Copyright (c) 2005-2007, Xiph.Org Foundation
4   Copyright (c) 2008,      Xiph.Org Foundation, CSIRO
5 
6   All rights reserved.
7 
8   Redistribution and use in source and binary forms, with or without
9    modification, are permitted provided that the following conditions are met:
10 
11     * Redistributions of source code must retain the above copyright notice,
12        this list of conditions and the following disclaimer.
13     * Redistributions in binary form must reproduce the above copyright notice,
14        this list of conditions and the following disclaimer in the
15        documentation and/or other materials provided with the distribution.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27   POSSIBILITY OF SUCH DAMAGE.*/
28 
29 #ifndef KISS_FFT_H
30 #define KISS_FFT_H
31 
32 #include <stdlib.h>
33 #include <math.h>
34 #include "arch.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #ifdef USE_SIMD
41 # include <xmmintrin.h>
42 # define kiss_fft_scalar __m128
43 #define KISS_FFT_MALLOC(nbytes) memalign(16,nbytes)
44 #else
45 #define KISS_FFT_MALLOC opus_alloc
46 #endif
47 
48 #ifdef FIXED_POINT
49 #include "arch.h"
50 
51 #  define kiss_fft_scalar opus_int32
52 #  define kiss_twiddle_scalar opus_int16
53 
54 
55 #else
56 # ifndef kiss_fft_scalar
57 /*  default is float */
58 #   define kiss_fft_scalar float
59 #   define kiss_twiddle_scalar float
60 #   define KF_SUFFIX _celt_single
61 # endif
62 #endif
63 
64 typedef struct {
65     kiss_fft_scalar r;
66     kiss_fft_scalar i;
67 }kiss_fft_cpx;
68 
69 typedef struct {
70    kiss_twiddle_scalar r;
71    kiss_twiddle_scalar i;
72 }kiss_twiddle_cpx;
73 
74 #define MAXFACTORS 8
75 /* e.g. an fft of length 128 has 4 factors
76  as far as kissfft is concerned
77  4*4*4*2
78  */
79 
80 typedef struct kiss_fft_state{
81     int nfft;
82 #ifndef FIXED_POINT
83     kiss_fft_scalar scale;
84 #endif
85     int shift;
86     opus_int16 factors[2*MAXFACTORS];
87     const opus_int16 *bitrev;
88     const kiss_twiddle_cpx *twiddles;
89 } kiss_fft_state;
90 
91 /*typedef struct kiss_fft_state* kiss_fft_cfg;*/
92 
93 /**
94  *  opus_fft_alloc
95  *
96  *  Initialize a FFT (or IFFT) algorithm's cfg/state buffer.
97  *
98  *  typical usage:      kiss_fft_cfg mycfg=opus_fft_alloc(1024,0,NULL,NULL);
99  *
100  *  The return value from fft_alloc is a cfg buffer used internally
101  *  by the fft routine or NULL.
102  *
103  *  If lenmem is NULL, then opus_fft_alloc will allocate a cfg buffer using malloc.
104  *  The returned value should be free()d when done to avoid memory leaks.
105  *
106  *  The state can be placed in a user supplied buffer 'mem':
107  *  If lenmem is not NULL and mem is not NULL and *lenmem is large enough,
108  *      then the function places the cfg in mem and the size used in *lenmem
109  *      and returns mem.
110  *
111  *  If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough),
112  *      then the function returns NULL and places the minimum cfg
113  *      buffer size in *lenmem.
114  * */
115 
116 kiss_fft_state *opus_fft_alloc_twiddles(int nfft,void * mem,size_t * lenmem, const kiss_fft_state *base);
117 
118 kiss_fft_state *opus_fft_alloc(int nfft,void * mem,size_t * lenmem);
119 
120 /**
121  * opus_fft(cfg,in_out_buf)
122  *
123  * Perform an FFT on a complex input buffer.
124  * for a forward FFT,
125  * fin should be  f[0] , f[1] , ... ,f[nfft-1]
126  * fout will be   F[0] , F[1] , ... ,F[nfft-1]
127  * Note that each element is complex and can be accessed like
128     f[k].r and f[k].i
129  * */
130 void opus_fft(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
131 void opus_ifft(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
132 
133 void opus_fft_free(const kiss_fft_state *cfg);
134 
135 #ifdef __cplusplus
136 }
137 #endif
138 
139 #endif
140