Chris@69: /*Copyright (c) 2003-2004, Mark Borgerding Chris@69: Lots of modifications by Jean-Marc Valin Chris@69: Copyright (c) 2005-2007, Xiph.Org Foundation Chris@69: Copyright (c) 2008, Xiph.Org Foundation, CSIRO Chris@69: Chris@69: All rights reserved. Chris@69: Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions are met: Chris@69: Chris@69: * Redistributions of source code must retain the above copyright notice, Chris@69: this list of conditions and the following disclaimer. Chris@69: * Redistributions in binary form must reproduce the above copyright notice, Chris@69: this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" Chris@69: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE Chris@69: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE Chris@69: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE Chris@69: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR Chris@69: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF Chris@69: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS Chris@69: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN Chris@69: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) Chris@69: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE Chris@69: POSSIBILITY OF SUCH DAMAGE.*/ Chris@69: Chris@69: #ifndef KISS_FFT_H Chris@69: #define KISS_FFT_H Chris@69: Chris@69: #include Chris@69: #include Chris@69: #include "arch.h" Chris@69: #include "cpu_support.h" Chris@69: Chris@69: #ifdef __cplusplus Chris@69: extern "C" { Chris@69: #endif Chris@69: Chris@69: #ifdef USE_SIMD Chris@69: # include Chris@69: # define kiss_fft_scalar __m128 Chris@69: #define KISS_FFT_MALLOC(nbytes) memalign(16,nbytes) Chris@69: #else Chris@69: #define KISS_FFT_MALLOC opus_alloc Chris@69: #endif Chris@69: Chris@69: #ifdef FIXED_POINT Chris@69: #include "arch.h" Chris@69: Chris@69: # define kiss_fft_scalar opus_int32 Chris@69: # define kiss_twiddle_scalar opus_int16 Chris@69: Chris@69: Chris@69: #else Chris@69: # ifndef kiss_fft_scalar Chris@69: /* default is float */ Chris@69: # define kiss_fft_scalar float Chris@69: # define kiss_twiddle_scalar float Chris@69: # define KF_SUFFIX _celt_single Chris@69: # endif Chris@69: #endif Chris@69: Chris@69: typedef struct { Chris@69: kiss_fft_scalar r; Chris@69: kiss_fft_scalar i; Chris@69: }kiss_fft_cpx; Chris@69: Chris@69: typedef struct { Chris@69: kiss_twiddle_scalar r; Chris@69: kiss_twiddle_scalar i; Chris@69: }kiss_twiddle_cpx; Chris@69: Chris@69: #define MAXFACTORS 8 Chris@69: /* e.g. an fft of length 128 has 4 factors Chris@69: as far as kissfft is concerned Chris@69: 4*4*4*2 Chris@69: */ Chris@69: Chris@69: typedef struct arch_fft_state{ Chris@69: int is_supported; Chris@69: void *priv; Chris@69: } arch_fft_state; Chris@69: Chris@69: typedef struct kiss_fft_state{ Chris@69: int nfft; Chris@69: opus_val16 scale; Chris@69: #ifdef FIXED_POINT Chris@69: int scale_shift; Chris@69: #endif Chris@69: int shift; Chris@69: opus_int16 factors[2*MAXFACTORS]; Chris@69: const opus_int16 *bitrev; Chris@69: const kiss_twiddle_cpx *twiddles; Chris@69: arch_fft_state *arch_fft; Chris@69: } kiss_fft_state; Chris@69: Chris@69: #if defined(HAVE_ARM_NE10) Chris@69: #include "arm/fft_arm.h" Chris@69: #endif Chris@69: Chris@69: /*typedef struct kiss_fft_state* kiss_fft_cfg;*/ Chris@69: Chris@69: /** Chris@69: * opus_fft_alloc Chris@69: * Chris@69: * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. Chris@69: * Chris@69: * typical usage: kiss_fft_cfg mycfg=opus_fft_alloc(1024,0,NULL,NULL); Chris@69: * Chris@69: * The return value from fft_alloc is a cfg buffer used internally Chris@69: * by the fft routine or NULL. Chris@69: * Chris@69: * If lenmem is NULL, then opus_fft_alloc will allocate a cfg buffer using malloc. Chris@69: * The returned value should be free()d when done to avoid memory leaks. Chris@69: * Chris@69: * The state can be placed in a user supplied buffer 'mem': Chris@69: * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, Chris@69: * then the function places the cfg in mem and the size used in *lenmem Chris@69: * and returns mem. Chris@69: * Chris@69: * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), Chris@69: * then the function returns NULL and places the minimum cfg Chris@69: * buffer size in *lenmem. Chris@69: * */ Chris@69: Chris@69: kiss_fft_state *opus_fft_alloc_twiddles(int nfft,void * mem,size_t * lenmem, const kiss_fft_state *base, int arch); Chris@69: Chris@69: kiss_fft_state *opus_fft_alloc(int nfft,void * mem,size_t * lenmem, int arch); Chris@69: Chris@69: /** Chris@69: * opus_fft(cfg,in_out_buf) Chris@69: * Chris@69: * Perform an FFT on a complex input buffer. Chris@69: * for a forward FFT, Chris@69: * fin should be f[0] , f[1] , ... ,f[nfft-1] Chris@69: * fout will be F[0] , F[1] , ... ,F[nfft-1] Chris@69: * Note that each element is complex and can be accessed like Chris@69: f[k].r and f[k].i Chris@69: * */ Chris@69: void opus_fft_c(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); Chris@69: void opus_ifft_c(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); Chris@69: Chris@69: void opus_fft_impl(const kiss_fft_state *st,kiss_fft_cpx *fout); Chris@69: void opus_ifft_impl(const kiss_fft_state *st,kiss_fft_cpx *fout); Chris@69: Chris@69: void opus_fft_free(const kiss_fft_state *cfg, int arch); Chris@69: Chris@69: Chris@69: void opus_fft_free_arch_c(kiss_fft_state *st); Chris@69: int opus_fft_alloc_arch_c(kiss_fft_state *st); Chris@69: Chris@69: #if !defined(OVERRIDE_OPUS_FFT) Chris@69: /* Is run-time CPU detection enabled on this platform? */ Chris@69: #if defined(OPUS_HAVE_RTCD) && (defined(HAVE_ARM_NE10)) Chris@69: Chris@69: extern int (*const OPUS_FFT_ALLOC_ARCH_IMPL[OPUS_ARCHMASK+1])( Chris@69: kiss_fft_state *st); Chris@69: Chris@69: #define opus_fft_alloc_arch(_st, arch) \ Chris@69: ((*OPUS_FFT_ALLOC_ARCH_IMPL[(arch)&OPUS_ARCHMASK])(_st)) Chris@69: Chris@69: extern void (*const OPUS_FFT_FREE_ARCH_IMPL[OPUS_ARCHMASK+1])( Chris@69: kiss_fft_state *st); Chris@69: #define opus_fft_free_arch(_st, arch) \ Chris@69: ((*OPUS_FFT_FREE_ARCH_IMPL[(arch)&OPUS_ARCHMASK])(_st)) Chris@69: Chris@69: extern void (*const OPUS_FFT[OPUS_ARCHMASK+1])(const kiss_fft_state *cfg, Chris@69: const kiss_fft_cpx *fin, kiss_fft_cpx *fout); Chris@69: #define opus_fft(_cfg, _fin, _fout, arch) \ Chris@69: ((*OPUS_FFT[(arch)&OPUS_ARCHMASK])(_cfg, _fin, _fout)) Chris@69: Chris@69: extern void (*const OPUS_IFFT[OPUS_ARCHMASK+1])(const kiss_fft_state *cfg, Chris@69: const kiss_fft_cpx *fin, kiss_fft_cpx *fout); Chris@69: #define opus_ifft(_cfg, _fin, _fout, arch) \ Chris@69: ((*OPUS_IFFT[(arch)&OPUS_ARCHMASK])(_cfg, _fin, _fout)) Chris@69: Chris@69: #else /* else for if defined(OPUS_HAVE_RTCD) && (defined(HAVE_ARM_NE10)) */ Chris@69: Chris@69: #define opus_fft_alloc_arch(_st, arch) \ Chris@69: ((void)(arch), opus_fft_alloc_arch_c(_st)) Chris@69: Chris@69: #define opus_fft_free_arch(_st, arch) \ Chris@69: ((void)(arch), opus_fft_free_arch_c(_st)) Chris@69: Chris@69: #define opus_fft(_cfg, _fin, _fout, arch) \ Chris@69: ((void)(arch), opus_fft_c(_cfg, _fin, _fout)) Chris@69: Chris@69: #define opus_ifft(_cfg, _fin, _fout, arch) \ Chris@69: ((void)(arch), opus_ifft_c(_cfg, _fin, _fout)) Chris@69: Chris@69: #endif /* end if defined(OPUS_HAVE_RTCD) && (defined(HAVE_ARM_NE10)) */ Chris@69: #endif /* end if !defined(OVERRIDE_OPUS_FFT) */ Chris@69: Chris@69: #ifdef __cplusplus Chris@69: } Chris@69: #endif Chris@69: Chris@69: #endif