kiss_fft.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.*/
  24. #ifndef KISS_FFT_H
  25. #define KISS_FFT_H
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #include "arch.h"
  29. #include "cpu_support.h"
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #ifdef USE_SIMD
  34. # include <xmmintrin.h>
  35. # define kiss_fft_scalar __m128
  36. #define KISS_FFT_MALLOC(nbytes) memalign(16,nbytes)
  37. #else
  38. #define KISS_FFT_MALLOC opus_alloc
  39. #endif
  40. #ifdef FIXED_POINT
  41. #include "arch.h"
  42. # define kiss_fft_scalar opus_int32
  43. # define kiss_twiddle_scalar opus_int16
  44. #else
  45. # ifndef kiss_fft_scalar
  46. /* default is float */
  47. # define kiss_fft_scalar float
  48. # define kiss_twiddle_scalar float
  49. # define KF_SUFFIX _celt_single
  50. # endif
  51. #endif
  52. typedef struct {
  53. kiss_fft_scalar r;
  54. kiss_fft_scalar i;
  55. }kiss_fft_cpx;
  56. typedef struct {
  57. kiss_twiddle_scalar r;
  58. kiss_twiddle_scalar i;
  59. }kiss_twiddle_cpx;
  60. #define MAXFACTORS 8
  61. /* e.g. an fft of length 128 has 4 factors
  62. as far as kissfft is concerned
  63. 4*4*4*2
  64. */
  65. typedef struct arch_fft_state{
  66. int is_supported;
  67. void *priv;
  68. } arch_fft_state;
  69. typedef struct kiss_fft_state{
  70. int nfft;
  71. opus_val16 scale;
  72. #ifdef FIXED_POINT
  73. int scale_shift;
  74. #endif
  75. int shift;
  76. opus_int16 factors[2*MAXFACTORS];
  77. const opus_int16 *bitrev;
  78. const kiss_twiddle_cpx *twiddles;
  79. arch_fft_state *arch_fft;
  80. } kiss_fft_state;
  81. #if defined(HAVE_ARM_NE10)
  82. #include "arm/fft_arm.h"
  83. #endif
  84. /*typedef struct kiss_fft_state* kiss_fft_cfg;*/
  85. /**
  86. * opus_fft_alloc
  87. *
  88. * Initialize a FFT (or IFFT) algorithm's cfg/state buffer.
  89. *
  90. * typical usage: kiss_fft_cfg mycfg=opus_fft_alloc(1024,0,NULL,NULL);
  91. *
  92. * The return value from fft_alloc is a cfg buffer used internally
  93. * by the fft routine or NULL.
  94. *
  95. * If lenmem is NULL, then opus_fft_alloc will allocate a cfg buffer using malloc.
  96. * The returned value should be free()d when done to avoid memory leaks.
  97. *
  98. * The state can be placed in a user supplied buffer 'mem':
  99. * If lenmem is not NULL and mem is not NULL and *lenmem is large enough,
  100. * then the function places the cfg in mem and the size used in *lenmem
  101. * and returns mem.
  102. *
  103. * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough),
  104. * then the function returns NULL and places the minimum cfg
  105. * buffer size in *lenmem.
  106. * */
  107. kiss_fft_state *opus_fft_alloc_twiddles(int nfft,void * mem,size_t * lenmem, const kiss_fft_state *base, int arch);
  108. kiss_fft_state *opus_fft_alloc(int nfft,void * mem,size_t * lenmem, int arch);
  109. /**
  110. * opus_fft(cfg,in_out_buf)
  111. *
  112. * Perform an FFT on a complex input buffer.
  113. * for a forward FFT,
  114. * fin should be f[0] , f[1] , ... ,f[nfft-1]
  115. * fout will be F[0] , F[1] , ... ,F[nfft-1]
  116. * Note that each element is complex and can be accessed like
  117. f[k].r and f[k].i
  118. * */
  119. void opus_fft_c(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
  120. void opus_ifft_c(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
  121. void opus_fft_impl(const kiss_fft_state *st,kiss_fft_cpx *fout);
  122. void opus_ifft_impl(const kiss_fft_state *st,kiss_fft_cpx *fout);
  123. void opus_fft_free(const kiss_fft_state *cfg, int arch);
  124. void opus_fft_free_arch_c(kiss_fft_state *st);
  125. int opus_fft_alloc_arch_c(kiss_fft_state *st);
  126. #if !defined(OVERRIDE_OPUS_FFT)
  127. /* Is run-time CPU detection enabled on this platform? */
  128. #if defined(OPUS_HAVE_RTCD) && (defined(HAVE_ARM_NE10))
  129. extern int (*const OPUS_FFT_ALLOC_ARCH_IMPL[OPUS_ARCHMASK+1])(
  130. kiss_fft_state *st);
  131. #define opus_fft_alloc_arch(_st, arch) \
  132. ((*OPUS_FFT_ALLOC_ARCH_IMPL[(arch)&OPUS_ARCHMASK])(_st))
  133. extern void (*const OPUS_FFT_FREE_ARCH_IMPL[OPUS_ARCHMASK+1])(
  134. kiss_fft_state *st);
  135. #define opus_fft_free_arch(_st, arch) \
  136. ((*OPUS_FFT_FREE_ARCH_IMPL[(arch)&OPUS_ARCHMASK])(_st))
  137. extern void (*const OPUS_FFT[OPUS_ARCHMASK+1])(const kiss_fft_state *cfg,
  138. const kiss_fft_cpx *fin, kiss_fft_cpx *fout);
  139. #define opus_fft(_cfg, _fin, _fout, arch) \
  140. ((*OPUS_FFT[(arch)&OPUS_ARCHMASK])(_cfg, _fin, _fout))
  141. extern void (*const OPUS_IFFT[OPUS_ARCHMASK+1])(const kiss_fft_state *cfg,
  142. const kiss_fft_cpx *fin, kiss_fft_cpx *fout);
  143. #define opus_ifft(_cfg, _fin, _fout, arch) \
  144. ((*OPUS_IFFT[(arch)&OPUS_ARCHMASK])(_cfg, _fin, _fout))
  145. #else /* else for if defined(OPUS_HAVE_RTCD) && (defined(HAVE_ARM_NE10)) */
  146. #define opus_fft_alloc_arch(_st, arch) \
  147. ((void)(arch), opus_fft_alloc_arch_c(_st))
  148. #define opus_fft_free_arch(_st, arch) \
  149. ((void)(arch), opus_fft_free_arch_c(_st))
  150. #define opus_fft(_cfg, _fin, _fout, arch) \
  151. ((void)(arch), opus_fft_c(_cfg, _fin, _fout))
  152. #define opus_ifft(_cfg, _fin, _fout, arch) \
  153. ((void)(arch), opus_ifft_c(_cfg, _fin, _fout))
  154. #endif /* end if defined(OPUS_HAVE_RTCD) && (defined(HAVE_ARM_NE10)) */
  155. #endif /* end if !defined(OVERRIDE_OPUS_FFT) */
  156. #ifdef __cplusplus
  157. }
  158. #endif
  159. #endif