fft.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: fft.c,v 1.1 2005/02/26 01:47:34 jrecker Exp $
  3. *
  4. * Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file,
  7. * are subject to the current version of the RealNetworks Public
  8. * Source License (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the current version of the RealNetworks Community
  11. * Source License (the "RCSL") available at
  12. * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13. * will apply. You may also obtain the license terms directly from
  14. * RealNetworks. You may not use this file except in compliance with
  15. * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16. * to this file, the RCSL. Please see the applicable RPSL or RCSL for
  17. * the rights, obligations and limitations governing use of the
  18. * contents of the file.
  19. *
  20. * This file is part of the Helix DNA Technology. RealNetworks is the
  21. * developer of the Original Code and owns the copyrights in the
  22. * portions it created.
  23. *
  24. * This file, and the files included with this file, is distributed
  25. * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  26. * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  27. * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  28. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  29. * ENJOYMENT OR NON-INFRINGEMENT.
  30. *
  31. * Technology Compatibility Kit Test Suite(s) Location:
  32. * http://www.helixcommunity.org/content/tck
  33. *
  34. * Contributor(s):
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /**************************************************************************************
  38. * Fixed-point HE-AAC decoder
  39. * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com)
  40. * February 2005
  41. *
  42. * fft.c - Ken's optimized radix-4 DIT FFT, optional radix-8 first pass for odd log2(N)
  43. **************************************************************************************/
  44. #include "coder.h"
  45. #include "assembly.h"
  46. #define NUM_FFT_SIZES 2
  47. static const int nfftTab[NUM_FFT_SIZES] PROGMEM ={64, 512};
  48. static const int nfftlog2Tab[NUM_FFT_SIZES] PROGMEM = {6, 9};
  49. #define SQRT1_2 0x5a82799a /* sqrt(1/2) in Q31 */
  50. #define swapcplx(p0,p1) \
  51. t = p0; t1 = *(&(p0)+1); p0 = p1; *(&(p0)+1) = *(&(p1)+1); p1 = t; *(&(p1)+1) = t1
  52. /**************************************************************************************
  53. * Function: BitReverse
  54. *
  55. * Description: Ken's fast in-place bit reverse, using super-small table
  56. *
  57. * Inputs: buffer of samples
  58. * table index (for transform size)
  59. *
  60. * Outputs: bit-reversed samples in same buffer
  61. *
  62. * Return: none
  63. **************************************************************************************/
  64. /*__attribute__ ((section (".data"))) */ static void BitReverse(int *inout, int tabidx)
  65. {
  66. int *part0, *part1;
  67. int a,b, t,t1;
  68. const unsigned char* tab = bitrevtab + bitrevtabOffset[tabidx];
  69. int nbits = nfftlog2Tab[tabidx];
  70. part0 = inout;
  71. part1 = inout + (1 << nbits);
  72. while ((a = pgm_read_byte(tab++)) != 0) {
  73. b = pgm_read_byte(tab++);
  74. swapcplx(part0[4*a+0], part0[4*b+0]); /* 0xxx0 <-> 0yyy0 */
  75. swapcplx(part0[4*a+2], part1[4*b+0]); /* 0xxx1 <-> 1yyy0 */
  76. swapcplx(part1[4*a+0], part0[4*b+2]); /* 1xxx0 <-> 0yyy1 */
  77. swapcplx(part1[4*a+2], part1[4*b+2]); /* 1xxx1 <-> 1yyy1 */
  78. }
  79. do {
  80. swapcplx(part0[4*a+2], part1[4*a+0]); /* 0xxx1 <-> 1xxx0 */
  81. } while ((a = pgm_read_byte(tab++)) != 0);
  82. }
  83. /**************************************************************************************
  84. * Function: R4FirstPass
  85. *
  86. * Description: radix-4 trivial pass for decimation-in-time FFT
  87. *
  88. * Inputs: buffer of (bit-reversed) samples
  89. * number of R4 butterflies per group (i.e. nfft / 4)
  90. *
  91. * Outputs: processed samples in same buffer
  92. *
  93. * Return: none
  94. *
  95. * Notes: assumes 2 guard bits, gains no integer bits,
  96. * guard bits out = guard bits in - 2
  97. **************************************************************************************/
  98. /* __attribute__ ((section (".data"))) */ static void R4FirstPass(int *x, int bg)
  99. {
  100. int ar, ai, br, bi, cr, ci, dr, di;
  101. for (; bg != 0; bg--) {
  102. ar = x[0] + x[2];
  103. br = x[0] - x[2];
  104. ai = x[1] + x[3];
  105. bi = x[1] - x[3];
  106. cr = x[4] + x[6];
  107. dr = x[4] - x[6];
  108. ci = x[5] + x[7];
  109. di = x[5] - x[7];
  110. /* max per-sample gain = 4.0 (adding 4 inputs together) */
  111. x[0] = ar + cr;
  112. x[4] = ar - cr;
  113. x[1] = ai + ci;
  114. x[5] = ai - ci;
  115. x[2] = br + di;
  116. x[6] = br - di;
  117. x[3] = bi - dr;
  118. x[7] = bi + dr;
  119. x += 8;
  120. }
  121. }
  122. /**************************************************************************************
  123. * Function: R8FirstPass
  124. *
  125. * Description: radix-8 trivial pass for decimation-in-time FFT
  126. *
  127. * Inputs: buffer of (bit-reversed) samples
  128. * number of R8 butterflies per group (i.e. nfft / 8)
  129. *
  130. * Outputs: processed samples in same buffer
  131. *
  132. * Return: none
  133. *
  134. * Notes: assumes 3 guard bits, gains 1 integer bit
  135. * guard bits out = guard bits in - 3 (if inputs are full scale)
  136. * or guard bits in - 2 (if inputs bounded to +/- sqrt(2)/2)
  137. * see scaling comments in code
  138. **************************************************************************************/
  139. /* __attribute__ ((section (".data"))) */ static void R8FirstPass(int *x, int bg)
  140. {
  141. int ar, ai, br, bi, cr, ci, dr, di;
  142. int sr, si, tr, ti, ur, ui, vr, vi;
  143. int wr, wi, xr, xi, yr, yi, zr, zi;
  144. for (; bg != 0; bg--) {
  145. ar = x[0] + x[2];
  146. br = x[0] - x[2];
  147. ai = x[1] + x[3];
  148. bi = x[1] - x[3];
  149. cr = x[4] + x[6];
  150. dr = x[4] - x[6];
  151. ci = x[5] + x[7];
  152. di = x[5] - x[7];
  153. sr = ar + cr;
  154. ur = ar - cr;
  155. si = ai + ci;
  156. ui = ai - ci;
  157. tr = br - di;
  158. vr = br + di;
  159. ti = bi + dr;
  160. vi = bi - dr;
  161. ar = x[ 8] + x[10];
  162. br = x[ 8] - x[10];
  163. ai = x[ 9] + x[11];
  164. bi = x[ 9] - x[11];
  165. cr = x[12] + x[14];
  166. dr = x[12] - x[14];
  167. ci = x[13] + x[15];
  168. di = x[13] - x[15];
  169. /* max gain of wr/wi/yr/yi vs input = 2
  170. * (sum of 4 samples >> 1)
  171. */
  172. wr = (ar + cr) >> 1;
  173. yr = (ar - cr) >> 1;
  174. wi = (ai + ci) >> 1;
  175. yi = (ai - ci) >> 1;
  176. /* max gain of output vs input = 4
  177. * (sum of 4 samples >> 1 + sum of 4 samples >> 1)
  178. */
  179. x[ 0] = (sr >> 1) + wr;
  180. x[ 8] = (sr >> 1) - wr;
  181. x[ 1] = (si >> 1) + wi;
  182. x[ 9] = (si >> 1) - wi;
  183. x[ 4] = (ur >> 1) + yi;
  184. x[12] = (ur >> 1) - yi;
  185. x[ 5] = (ui >> 1) - yr;
  186. x[13] = (ui >> 1) + yr;
  187. ar = br - di;
  188. cr = br + di;
  189. ai = bi + dr;
  190. ci = bi - dr;
  191. /* max gain of xr/xi/zr/zi vs input = 4*sqrt(2)/2 = 2*sqrt(2)
  192. * (sum of 8 samples, multiply by sqrt(2)/2, implicit >> 1 from Q31)
  193. */
  194. xr = MULSHIFT32(SQRT1_2, ar - ai);
  195. xi = MULSHIFT32(SQRT1_2, ar + ai);
  196. zr = MULSHIFT32(SQRT1_2, cr - ci);
  197. zi = MULSHIFT32(SQRT1_2, cr + ci);
  198. /* max gain of output vs input = (2 + 2*sqrt(2) ~= 4.83)
  199. * (sum of 4 samples >> 1, plus xr/xi/zr/zi with gain of 2*sqrt(2))
  200. * in absolute terms, we have max gain of appx 9.656 (4 + 0.707*8)
  201. * but we also gain 1 int bit (from MULSHIFT32 or from explicit >> 1)
  202. */
  203. x[ 6] = (tr >> 1) - xr;
  204. x[14] = (tr >> 1) + xr;
  205. x[ 7] = (ti >> 1) - xi;
  206. x[15] = (ti >> 1) + xi;
  207. x[ 2] = (vr >> 1) + zi;
  208. x[10] = (vr >> 1) - zi;
  209. x[ 3] = (vi >> 1) - zr;
  210. x[11] = (vi >> 1) + zr;
  211. x += 16;
  212. }
  213. }
  214. /**************************************************************************************
  215. * Function: R4Core
  216. *
  217. * Description: radix-4 pass for decimation-in-time FFT
  218. *
  219. * Inputs: buffer of samples
  220. * number of R4 butterflies per group
  221. * number of R4 groups per pass
  222. * pointer to twiddle factors tables
  223. *
  224. * Outputs: processed samples in same buffer
  225. *
  226. * Return: none
  227. *
  228. * Notes: gain 2 integer bits per pass (see scaling comments in code)
  229. * min 1 GB in
  230. * gbOut = gbIn - 1 (short block) or gbIn - 2 (long block)
  231. * uses 3-mul, 3-add butterflies instead of 4-mul, 2-add
  232. **************************************************************************************/
  233. /* __attribute__ ((section (".data"))) */ static void R4Core(int *x, int bg, int gp, int *wtab)
  234. {
  235. int ar, ai, br, bi, cr, ci, dr, di, tr, ti;
  236. int wd, ws, wi;
  237. int i, j, step;
  238. int *xptr, *wptr;
  239. for (; bg != 0; gp <<= 2, bg >>= 2) {
  240. step = 2*gp;
  241. xptr = x;
  242. /* max per-sample gain, per group < 1 + 3*sqrt(2) ~= 5.25 if inputs x are full-scale
  243. * do 3 groups for long block, 2 groups for short block (gain 2 int bits per group)
  244. *
  245. * very conservative scaling:
  246. * group 1: max gain = 5.25, int bits gained = 2, gb used = 1 (2^3 = 8)
  247. * group 2: max gain = 5.25^2 = 27.6, int bits gained = 4, gb used = 1 (2^5 = 32)
  248. * group 3: max gain = 5.25^3 = 144.7, int bits gained = 6, gb used = 2 (2^8 = 256)
  249. */
  250. for (i = bg; i != 0; i--) {
  251. wptr = wtab;
  252. for (j = gp; j != 0; j--) {
  253. ar = xptr[0];
  254. ai = xptr[1];
  255. xptr += step;
  256. /* gain 2 int bits for br/bi, cr/ci, dr/di (MULSHIFT32 by Q30)
  257. * gain 1 net GB
  258. */
  259. ws = wptr[0];
  260. wi = wptr[1];
  261. br = xptr[0];
  262. bi = xptr[1];
  263. wd = ws + 2*wi;
  264. tr = MULSHIFT32(wi, br + bi);
  265. br = MULSHIFT32(wd, br) - tr; /* cos*br + sin*bi */
  266. bi = MULSHIFT32(ws, bi) + tr; /* cos*bi - sin*br */
  267. xptr += step;
  268. ws = wptr[2];
  269. wi = wptr[3];
  270. cr = xptr[0];
  271. ci = xptr[1];
  272. wd = ws + 2*wi;
  273. tr = MULSHIFT32(wi, cr + ci);
  274. cr = MULSHIFT32(wd, cr) - tr;
  275. ci = MULSHIFT32(ws, ci) + tr;
  276. xptr += step;
  277. ws = wptr[4];
  278. wi = wptr[5];
  279. dr = xptr[0];
  280. di = xptr[1];
  281. wd = ws + 2*wi;
  282. tr = MULSHIFT32(wi, dr + di);
  283. dr = MULSHIFT32(wd, dr) - tr;
  284. di = MULSHIFT32(ws, di) + tr;
  285. wptr += 6;
  286. tr = ar;
  287. ti = ai;
  288. ar = (tr >> 2) - br;
  289. ai = (ti >> 2) - bi;
  290. br = (tr >> 2) + br;
  291. bi = (ti >> 2) + bi;
  292. tr = cr;
  293. ti = ci;
  294. cr = tr + dr;
  295. ci = di - ti;
  296. dr = tr - dr;
  297. di = di + ti;
  298. xptr[0] = ar + ci;
  299. xptr[1] = ai + dr;
  300. xptr -= step;
  301. xptr[0] = br - cr;
  302. xptr[1] = bi - di;
  303. xptr -= step;
  304. xptr[0] = ar - ci;
  305. xptr[1] = ai - dr;
  306. xptr -= step;
  307. xptr[0] = br + cr;
  308. xptr[1] = bi + di;
  309. xptr += 2;
  310. }
  311. xptr += 3*step;
  312. }
  313. wtab += 3*step;
  314. }
  315. }
  316. /**************************************************************************************
  317. * Function: R4FFT
  318. *
  319. * Description: Ken's very fast in-place radix-4 decimation-in-time FFT
  320. *
  321. * Inputs: table index (for transform size)
  322. * buffer of samples (non bit-reversed)
  323. *
  324. * Outputs: processed samples in same buffer
  325. *
  326. * Return: none
  327. *
  328. * Notes: assumes 5 guard bits in for nfft <= 512
  329. * gbOut = gbIn - 4 (assuming input is from PreMultiply)
  330. * gains log2(nfft) - 2 int bits total
  331. * so gain 7 int bits (LONG), 4 int bits (SHORT)
  332. **************************************************************************************/
  333. void R4FFT(int tabidx, int *x)
  334. {
  335. int order = nfftlog2Tab[tabidx];
  336. int nfft = nfftTab[tabidx];
  337. /* decimation in time */
  338. BitReverse(x, tabidx);
  339. if (order & 0x1) {
  340. /* long block: order = 9, nfft = 512 */
  341. R8FirstPass(x, nfft >> 3); /* gain 1 int bit, lose 2 GB */
  342. R4Core(x, nfft >> 5, 8, (int *)twidTabOdd); /* gain 6 int bits, lose 2 GB */
  343. } else {
  344. /* short block: order = 6, nfft = 64 */
  345. R4FirstPass(x, nfft >> 2); /* gain 0 int bits, lose 2 GB */
  346. R4Core(x, nfft >> 4, 4, (int *)twidTabEven); /* gain 4 int bits, lose 1 GB */
  347. }
  348. }