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