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