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