• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "cpu_support.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 #ifdef USE_SIMD
42 # include <xmmintrin.h>
43 # define kiss_fft_scalar __m128
44 #define KISS_FFT_MALLOC(nbytes) memalign(16,nbytes)
45 #else
46 #define KISS_FFT_MALLOC opus_alloc
47 #endif
48 
49 #ifdef FIXED_POINT
50 #include "arch.h"
51 
52 #  define kiss_fft_scalar opus_int32
53 #  define kiss_twiddle_scalar opus_int16
54 
55 /* Some 32-bit CPUs would load/store a kiss_twiddle_cpx with a single memory
56  * access, and could benefit from additional alignment.
57  */
58 #  define KISS_TWIDDLE_CPX_ALIGNMENT (sizeof(opus_int32))
59 
60 #else
61 # ifndef kiss_fft_scalar
62 /*  default is float */
63 #   define kiss_fft_scalar float
64 #   define kiss_twiddle_scalar float
65 #   define KF_SUFFIX _celt_single
66 # endif
67 #endif
68 
69 #if defined(__GNUC__) && defined(KISS_TWIDDLE_CPX_ALIGNMENT)
70 #define KISS_TWIDDLE_CPX_ALIGNED __attribute__((aligned(KISS_TWIDDLE_CPX_ALIGNMENT)))
71 #else
72 #define KISS_TWIDDLE_CPX_ALIGNED
73 #endif
74 
75 typedef struct {
76     kiss_fft_scalar r;
77     kiss_fft_scalar i;
78 }kiss_fft_cpx;
79 
80 typedef struct {
81    kiss_twiddle_scalar r;
82    kiss_twiddle_scalar i;
83 } KISS_TWIDDLE_CPX_ALIGNED kiss_twiddle_cpx;
84 
85 #define MAXFACTORS 8
86 /* e.g. an fft of length 128 has 4 factors
87  as far as kissfft is concerned
88  4*4*4*2
89  */
90 
91 typedef struct arch_fft_state{
92    int is_supported;
93    void *priv;
94 } arch_fft_state;
95 
96 typedef struct kiss_fft_state{
97     int nfft;
98     opus_val16 scale;
99 #ifdef FIXED_POINT
100     int scale_shift;
101 #endif
102     int shift;
103     opus_int16 factors[2*MAXFACTORS];
104     const opus_int16 *bitrev;
105     const kiss_twiddle_cpx *twiddles;
106     arch_fft_state *arch_fft;
107 } kiss_fft_state;
108 
109 #if defined(HAVE_ARM_NE10)
110 #include "arm/fft_arm.h"
111 #endif
112 
113 /*typedef struct kiss_fft_state* kiss_fft_cfg;*/
114 
115 /**
116  *  opus_fft_alloc
117  *
118  *  Initialize a FFT (or IFFT) algorithm's cfg/state buffer.
119  *
120  *  typical usage:      kiss_fft_cfg mycfg=opus_fft_alloc(1024,0,NULL,NULL);
121  *
122  *  The return value from fft_alloc is a cfg buffer used internally
123  *  by the fft routine or NULL.
124  *
125  *  If lenmem is NULL, then opus_fft_alloc will allocate a cfg buffer using malloc.
126  *  The returned value should be free()d when done to avoid memory leaks.
127  *
128  *  The state can be placed in a user supplied buffer 'mem':
129  *  If lenmem is not NULL and mem is not NULL and *lenmem is large enough,
130  *      then the function places the cfg in mem and the size used in *lenmem
131  *      and returns mem.
132  *
133  *  If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough),
134  *      then the function returns NULL and places the minimum cfg
135  *      buffer size in *lenmem.
136  * */
137 
138 kiss_fft_state *opus_fft_alloc_twiddles(int nfft,void * mem,size_t * lenmem, const kiss_fft_state *base, int arch);
139 
140 kiss_fft_state *opus_fft_alloc(int nfft,void * mem,size_t * lenmem, int arch);
141 
142 /**
143  * opus_fft(cfg,in_out_buf)
144  *
145  * Perform an FFT on a complex input buffer.
146  * for a forward FFT,
147  * fin should be  f[0] , f[1] , ... ,f[nfft-1]
148  * fout will be   F[0] , F[1] , ... ,F[nfft-1]
149  * Note that each element is complex and can be accessed like
150     f[k].r and f[k].i
151  * */
152 void opus_fft_c(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
153 void opus_ifft_c(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
154 
155 void opus_fft_impl(const kiss_fft_state *st,kiss_fft_cpx *fout);
156 void opus_ifft_impl(const kiss_fft_state *st,kiss_fft_cpx *fout);
157 
158 void opus_fft_free(const kiss_fft_state *cfg, int arch);
159 
160 
161 void opus_fft_free_arch_c(kiss_fft_state *st);
162 int opus_fft_alloc_arch_c(kiss_fft_state *st);
163 
164 #if !defined(OVERRIDE_OPUS_FFT)
165 /* Is run-time CPU detection enabled on this platform? */
166 #if defined(OPUS_HAVE_RTCD) && (defined(HAVE_ARM_NE10))
167 
168 extern int (*const OPUS_FFT_ALLOC_ARCH_IMPL[OPUS_ARCHMASK+1])(
169  kiss_fft_state *st);
170 
171 #define opus_fft_alloc_arch(_st, arch) \
172          ((*OPUS_FFT_ALLOC_ARCH_IMPL[(arch)&OPUS_ARCHMASK])(_st))
173 
174 extern void (*const OPUS_FFT_FREE_ARCH_IMPL[OPUS_ARCHMASK+1])(
175  kiss_fft_state *st);
176 #define opus_fft_free_arch(_st, arch) \
177          ((*OPUS_FFT_FREE_ARCH_IMPL[(arch)&OPUS_ARCHMASK])(_st))
178 
179 extern void (*const OPUS_FFT[OPUS_ARCHMASK+1])(const kiss_fft_state *cfg,
180  const kiss_fft_cpx *fin, kiss_fft_cpx *fout);
181 #define opus_fft(_cfg, _fin, _fout, arch) \
182    ((*OPUS_FFT[(arch)&OPUS_ARCHMASK])(_cfg, _fin, _fout))
183 
184 extern void (*const OPUS_IFFT[OPUS_ARCHMASK+1])(const kiss_fft_state *cfg,
185  const kiss_fft_cpx *fin, kiss_fft_cpx *fout);
186 #define opus_ifft(_cfg, _fin, _fout, arch) \
187    ((*OPUS_IFFT[(arch)&OPUS_ARCHMASK])(_cfg, _fin, _fout))
188 
189 #else /* else for if defined(OPUS_HAVE_RTCD) && (defined(HAVE_ARM_NE10)) */
190 
191 #define opus_fft_alloc_arch(_st, arch) \
192          ((void)(arch), opus_fft_alloc_arch_c(_st))
193 
194 #define opus_fft_free_arch(_st, arch) \
195          ((void)(arch), opus_fft_free_arch_c(_st))
196 
197 #define opus_fft(_cfg, _fin, _fout, arch) \
198          ((void)(arch), opus_fft_c(_cfg, _fin, _fout))
199 
200 #define opus_ifft(_cfg, _fin, _fout, arch) \
201          ((void)(arch), opus_ifft_c(_cfg, _fin, _fout))
202 
203 #endif /* end if defined(OPUS_HAVE_RTCD) && (defined(HAVE_ARM_NE10)) */
204 #endif /* end if !defined(OVERRIDE_OPUS_FFT) */
205 
206 #ifdef __cplusplus
207 }
208 #endif
209 
210 #endif
211