kiss_fft.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #ifdef USE_SIMD
  33. # include <xmmintrin.h>
  34. # define kiss_fft_scalar __m128
  35. #define KISS_FFT_MALLOC(nbytes) memalign(16,nbytes)
  36. #else
  37. #define KISS_FFT_MALLOC opus_alloc
  38. #endif
  39. #ifdef FIXED_POINT
  40. #include "arch.h"
  41. # define kiss_fft_scalar opus_int32
  42. # define kiss_twiddle_scalar opus_int16
  43. #else
  44. # ifndef kiss_fft_scalar
  45. /* default is float */
  46. # define kiss_fft_scalar float
  47. # define kiss_twiddle_scalar float
  48. # define KF_SUFFIX _celt_single
  49. # endif
  50. #endif
  51. typedef struct {
  52. kiss_fft_scalar r;
  53. kiss_fft_scalar i;
  54. }kiss_fft_cpx;
  55. typedef struct {
  56. kiss_twiddle_scalar r;
  57. kiss_twiddle_scalar i;
  58. }kiss_twiddle_cpx;
  59. #define MAXFACTORS 8
  60. /* e.g. an fft of length 128 has 4 factors
  61. as far as kissfft is concerned
  62. 4*4*4*2
  63. */
  64. typedef struct kiss_fft_state{
  65. int nfft;
  66. opus_val16 scale;
  67. #ifdef FIXED_POINT
  68. int scale_shift;
  69. #endif
  70. int shift;
  71. opus_int16 factors[2*MAXFACTORS];
  72. const opus_int16 *bitrev;
  73. const kiss_twiddle_cpx *twiddles;
  74. } kiss_fft_state;
  75. /*typedef struct kiss_fft_state* kiss_fft_cfg;*/
  76. /**
  77. * opus_fft_alloc
  78. *
  79. * Initialize a FFT (or IFFT) algorithm's cfg/state buffer.
  80. *
  81. * typical usage: kiss_fft_cfg mycfg=opus_fft_alloc(1024,0,NULL,NULL);
  82. *
  83. * The return value from fft_alloc is a cfg buffer used internally
  84. * by the fft routine or NULL.
  85. *
  86. * If lenmem is NULL, then opus_fft_alloc will allocate a cfg buffer using malloc.
  87. * The returned value should be free()d when done to avoid memory leaks.
  88. *
  89. * The state can be placed in a user supplied buffer 'mem':
  90. * If lenmem is not NULL and mem is not NULL and *lenmem is large enough,
  91. * then the function places the cfg in mem and the size used in *lenmem
  92. * and returns mem.
  93. *
  94. * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough),
  95. * then the function returns NULL and places the minimum cfg
  96. * buffer size in *lenmem.
  97. * */
  98. kiss_fft_state *opus_fft_alloc_twiddles(int nfft,void * mem,size_t * lenmem, const kiss_fft_state *base);
  99. kiss_fft_state *opus_fft_alloc(int nfft,void * mem,size_t * lenmem);
  100. /**
  101. * opus_fft(cfg,in_out_buf)
  102. *
  103. * Perform an FFT on a complex input buffer.
  104. * for a forward FFT,
  105. * fin should be f[0] , f[1] , ... ,f[nfft-1]
  106. * fout will be F[0] , F[1] , ... ,F[nfft-1]
  107. * Note that each element is complex and can be accessed like
  108. f[k].r and f[k].i
  109. * */
  110. void opus_fft(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
  111. void opus_ifft(const kiss_fft_state *cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
  112. void opus_fft_impl(const kiss_fft_state *st,kiss_fft_cpx *fout);
  113. void opus_ifft_impl(const kiss_fft_state *st,kiss_fft_cpx *fout);
  114. void opus_fft_free(const kiss_fft_state *cfg);
  115. #ifdef __cplusplus
  116. }
  117. #endif
  118. #endif