fft.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. ** FFT and FHT routines
  3. ** Copyright 1988, 1993; Ron Mayer
  4. ** Copyright (c) 1999-2000 Takehiro Tominaga
  5. **
  6. ** fht(fz,n);
  7. ** Does a hartley transform of "n" points in the array "fz".
  8. **
  9. ** NOTE: This routine uses at least 2 patented algorithms, and may be
  10. ** under the restrictions of a bunch of different organizations.
  11. ** Although I wrote it completely myself; it is kind of a derivative
  12. ** of a routine I once authored and released under the GPL, so it
  13. ** may fall under the free software foundation's restrictions;
  14. ** it was worked on as a Stanford Univ project, so they claim
  15. ** some rights to it; it was further optimized at work here, so
  16. ** I think this company claims parts of it. The patents are
  17. ** held by R. Bracewell (the FHT algorithm) and O. Buneman (the
  18. ** trig generator), both at Stanford Univ.
  19. ** If it were up to me, I'd say go do whatever you want with it;
  20. ** but it would be polite to give credit to the following people
  21. ** if you use this anywhere:
  22. ** Euler - probable inventor of the fourier transform.
  23. ** Gauss - probable inventor of the FFT.
  24. ** Hartley - probable inventor of the hartley transform.
  25. ** Buneman - for a really cool trig generator
  26. ** Mayer(me) - for authoring this particular version and
  27. ** including all the optimizations in one package.
  28. ** Thanks,
  29. ** Ron Mayer; [email protected]
  30. ** and added some optimization by
  31. ** Mather - idea of using lookup table
  32. ** Takehiro - some dirty hack for speed up
  33. */
  34. /* $Id: fft.c,v 1.39 2017/09/06 15:07:29 robert Exp $ */
  35. #ifdef HAVE_CONFIG_H
  36. # include <config.h>
  37. #endif
  38. #include "lame.h"
  39. #include "machine.h"
  40. #include "encoder.h"
  41. #include "util.h"
  42. #include "fft.h"
  43. #include "vector/lame_intrin.h"
  44. #define TRI_SIZE (5-1) /* 1024 = 4**5 */
  45. /* fft.c */
  46. static const FLOAT costab[TRI_SIZE * 2] = {
  47. 9.238795325112867e-01, 3.826834323650898e-01,
  48. 9.951847266721969e-01, 9.801714032956060e-02,
  49. 9.996988186962042e-01, 2.454122852291229e-02,
  50. 9.999811752826011e-01, 6.135884649154475e-03
  51. };
  52. static void
  53. fht(FLOAT * fz, int n)
  54. {
  55. const FLOAT *tri = costab;
  56. int k4;
  57. FLOAT *fi, *gi;
  58. FLOAT const *fn;
  59. n <<= 1; /* to get BLKSIZE, because of 3DNow! ASM routine */
  60. fn = fz + n;
  61. k4 = 4;
  62. do {
  63. FLOAT s1, c1;
  64. int i, k1, k2, k3, kx;
  65. kx = k4 >> 1;
  66. k1 = k4;
  67. k2 = k4 << 1;
  68. k3 = k2 + k1;
  69. k4 = k2 << 1;
  70. fi = fz;
  71. gi = fi + kx;
  72. do {
  73. FLOAT f0, f1, f2, f3;
  74. f1 = fi[0] - fi[k1];
  75. f0 = fi[0] + fi[k1];
  76. f3 = fi[k2] - fi[k3];
  77. f2 = fi[k2] + fi[k3];
  78. fi[k2] = f0 - f2;
  79. fi[0] = f0 + f2;
  80. fi[k3] = f1 - f3;
  81. fi[k1] = f1 + f3;
  82. f1 = gi[0] - gi[k1];
  83. f0 = gi[0] + gi[k1];
  84. f3 = SQRT2 * gi[k3];
  85. f2 = SQRT2 * gi[k2];
  86. gi[k2] = f0 - f2;
  87. gi[0] = f0 + f2;
  88. gi[k3] = f1 - f3;
  89. gi[k1] = f1 + f3;
  90. gi += k4;
  91. fi += k4;
  92. } while (fi < fn);
  93. c1 = tri[0];
  94. s1 = tri[1];
  95. for (i = 1; i < kx; i++) {
  96. FLOAT c2, s2;
  97. c2 = 1 - (2 * s1) * s1;
  98. s2 = (2 * s1) * c1;
  99. fi = fz + i;
  100. gi = fz + k1 - i;
  101. do {
  102. FLOAT a, b, g0, f0, f1, g1, f2, g2, f3, g3;
  103. b = s2 * fi[k1] - c2 * gi[k1];
  104. a = c2 * fi[k1] + s2 * gi[k1];
  105. f1 = fi[0] - a;
  106. f0 = fi[0] + a;
  107. g1 = gi[0] - b;
  108. g0 = gi[0] + b;
  109. b = s2 * fi[k3] - c2 * gi[k3];
  110. a = c2 * fi[k3] + s2 * gi[k3];
  111. f3 = fi[k2] - a;
  112. f2 = fi[k2] + a;
  113. g3 = gi[k2] - b;
  114. g2 = gi[k2] + b;
  115. b = s1 * f2 - c1 * g3;
  116. a = c1 * f2 + s1 * g3;
  117. fi[k2] = f0 - a;
  118. fi[0] = f0 + a;
  119. gi[k3] = g1 - b;
  120. gi[k1] = g1 + b;
  121. b = c1 * g2 - s1 * f3;
  122. a = s1 * g2 + c1 * f3;
  123. gi[k2] = g0 - a;
  124. gi[0] = g0 + a;
  125. fi[k3] = f1 - b;
  126. fi[k1] = f1 + b;
  127. gi += k4;
  128. fi += k4;
  129. } while (fi < fn);
  130. c2 = c1;
  131. c1 = c2 * tri[0] - s1 * tri[1];
  132. s1 = c2 * tri[1] + s1 * tri[0];
  133. }
  134. tri += 2;
  135. } while (k4 < n);
  136. }
  137. static const unsigned char rv_tbl[] = {
  138. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  139. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  140. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  141. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  142. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  143. 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  144. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  145. 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  146. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  147. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  148. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  149. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  150. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  151. 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  152. 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  153. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe
  154. };
  155. #define ch01(index) (buffer[chn][index])
  156. #define ml00(f) (window[i ] * f(i))
  157. #define ml10(f) (window[i + 0x200] * f(i + 0x200))
  158. #define ml20(f) (window[i + 0x100] * f(i + 0x100))
  159. #define ml30(f) (window[i + 0x300] * f(i + 0x300))
  160. #define ml01(f) (window[i + 0x001] * f(i + 0x001))
  161. #define ml11(f) (window[i + 0x201] * f(i + 0x201))
  162. #define ml21(f) (window[i + 0x101] * f(i + 0x101))
  163. #define ml31(f) (window[i + 0x301] * f(i + 0x301))
  164. #define ms00(f) (window_s[i ] * f(i + k))
  165. #define ms10(f) (window_s[0x7f - i] * f(i + k + 0x80))
  166. #define ms20(f) (window_s[i + 0x40] * f(i + k + 0x40))
  167. #define ms30(f) (window_s[0x3f - i] * f(i + k + 0xc0))
  168. #define ms01(f) (window_s[i + 0x01] * f(i + k + 0x01))
  169. #define ms11(f) (window_s[0x7e - i] * f(i + k + 0x81))
  170. #define ms21(f) (window_s[i + 0x41] * f(i + k + 0x41))
  171. #define ms31(f) (window_s[0x3e - i] * f(i + k + 0xc1))
  172. void
  173. fft_short(lame_internal_flags const *const gfc,
  174. FLOAT x_real[3][BLKSIZE_s], int chn, const sample_t *const buffer[2])
  175. {
  176. int i;
  177. int j;
  178. int b;
  179. #define window_s gfc->cd_psy->window_s
  180. #define window gfc->cd_psy->window
  181. for (b = 0; b < 3; b++) {
  182. FLOAT *x = &x_real[b][BLKSIZE_s / 2];
  183. short const k = (576 / 3) * (b + 1);
  184. j = BLKSIZE_s / 8 - 1;
  185. do {
  186. FLOAT f0, f1, f2, f3, w;
  187. i = rv_tbl[j << 2];
  188. f0 = ms00(ch01);
  189. w = ms10(ch01);
  190. f1 = f0 - w;
  191. f0 = f0 + w;
  192. f2 = ms20(ch01);
  193. w = ms30(ch01);
  194. f3 = f2 - w;
  195. f2 = f2 + w;
  196. x -= 4;
  197. x[0] = f0 + f2;
  198. x[2] = f0 - f2;
  199. x[1] = f1 + f3;
  200. x[3] = f1 - f3;
  201. f0 = ms01(ch01);
  202. w = ms11(ch01);
  203. f1 = f0 - w;
  204. f0 = f0 + w;
  205. f2 = ms21(ch01);
  206. w = ms31(ch01);
  207. f3 = f2 - w;
  208. f2 = f2 + w;
  209. x[BLKSIZE_s / 2 + 0] = f0 + f2;
  210. x[BLKSIZE_s / 2 + 2] = f0 - f2;
  211. x[BLKSIZE_s / 2 + 1] = f1 + f3;
  212. x[BLKSIZE_s / 2 + 3] = f1 - f3;
  213. } while (--j >= 0);
  214. #undef window
  215. #undef window_s
  216. gfc->fft_fht(x, BLKSIZE_s / 2);
  217. /* BLKSIZE_s/2 because of 3DNow! ASM routine */
  218. }
  219. }
  220. void
  221. fft_long(lame_internal_flags const *const gfc,
  222. FLOAT x[BLKSIZE], int chn, const sample_t *const buffer[2])
  223. {
  224. int i;
  225. int jj = BLKSIZE / 8 - 1;
  226. x += BLKSIZE / 2;
  227. #define window_s gfc->cd_psy->window_s
  228. #define window gfc->cd_psy->window
  229. do {
  230. FLOAT f0, f1, f2, f3, w;
  231. i = rv_tbl[jj];
  232. f0 = ml00(ch01);
  233. w = ml10(ch01);
  234. f1 = f0 - w;
  235. f0 = f0 + w;
  236. f2 = ml20(ch01);
  237. w = ml30(ch01);
  238. f3 = f2 - w;
  239. f2 = f2 + w;
  240. x -= 4;
  241. x[0] = f0 + f2;
  242. x[2] = f0 - f2;
  243. x[1] = f1 + f3;
  244. x[3] = f1 - f3;
  245. f0 = ml01(ch01);
  246. w = ml11(ch01);
  247. f1 = f0 - w;
  248. f0 = f0 + w;
  249. f2 = ml21(ch01);
  250. w = ml31(ch01);
  251. f3 = f2 - w;
  252. f2 = f2 + w;
  253. x[BLKSIZE / 2 + 0] = f0 + f2;
  254. x[BLKSIZE / 2 + 2] = f0 - f2;
  255. x[BLKSIZE / 2 + 1] = f1 + f3;
  256. x[BLKSIZE / 2 + 3] = f1 - f3;
  257. } while (--jj >= 0);
  258. #undef window
  259. #undef window_s
  260. gfc->fft_fht(x, BLKSIZE / 2);
  261. /* BLKSIZE/2 because of 3DNow! ASM routine */
  262. }
  263. #ifdef HAVE_NASM
  264. extern void fht_3DN(FLOAT * fz, int n);
  265. extern void fht_SSE(FLOAT * fz, int n);
  266. #endif
  267. void
  268. init_fft(lame_internal_flags * const gfc)
  269. {
  270. int i;
  271. /* The type of window used here will make no real difference, but */
  272. /* in the interest of merging nspsytune stuff - switch to blackman window */
  273. for (i = 0; i < BLKSIZE; i++)
  274. /* blackman window */
  275. gfc->cd_psy->window[i] = 0.42 - 0.5 * cos(2 * PI * (i + .5) / BLKSIZE) +
  276. 0.08 * cos(4 * PI * (i + .5) / BLKSIZE);
  277. for (i = 0; i < BLKSIZE_s / 2; i++)
  278. gfc->cd_psy->window_s[i] = 0.5 * (1.0 - cos(2.0 * PI * (i + 0.5) / BLKSIZE_s));
  279. gfc->fft_fht = fht;
  280. #ifdef HAVE_NASM
  281. if (gfc->CPU_features.AMD_3DNow) {
  282. gfc->fft_fht = fht_3DN;
  283. }
  284. else if (gfc->CPU_features.SSE) {
  285. gfc->fft_fht = fht_SSE;
  286. }
  287. else {
  288. gfc->fft_fht = fht;
  289. }
  290. #else
  291. #ifdef HAVE_XMMINTRIN_H
  292. #ifdef MIN_ARCH_SSE
  293. gfc->fft_fht = fht_SSE2;
  294. #endif
  295. #endif
  296. #endif
  297. }