sbrfft.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: sbrfft.c,v 1.1 2005/02/26 01:47:35 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. * sbrfft.c - optimized FFT for SBR QMF filters
  43. **************************************************************************************/
  44. #include "sbr.h"
  45. #include "assembly.h"
  46. #define SQRT1_2 0x5a82799a
  47. /* swap RE{p0} with RE{p1} and IM{P0} with IM{P1} */
  48. #define swapcplx(p0,p1) \
  49. t = p0; t1 = *(&(p0)+1); p0 = p1; *(&(p0)+1) = *(&(p1)+1); p1 = t; *(&(p1)+1) = t1
  50. /* nfft = 32, hard coded since small, fixed size FFT
  51. static const unsigned char bitrevtab32[9] = {
  52. 0x01, 0x04, 0x03, 0x06, 0x00, 0x02, 0x05, 0x07, 0x00,
  53. };
  54. */
  55. /* twiddle table for radix 4 pass, format = Q31 */
  56. static const int twidTabOdd32[8*6] = {
  57. 0x40000000, 0x00000000, 0x40000000, 0x00000000, 0x40000000, 0x00000000, 0x539eba45, 0xe7821d59,
  58. 0x4b418bbe, 0xf383a3e2, 0x58c542c5, 0xdc71898d, 0x5a82799a, 0xd2bec333, 0x539eba45, 0xe7821d59,
  59. 0x539eba45, 0xc4df2862, 0x539eba45, 0xc4df2862, 0x58c542c5, 0xdc71898d, 0x3248d382, 0xc13ad060,
  60. 0x40000000, 0xc0000000, 0x5a82799a, 0xd2bec333, 0x00000000, 0xd2bec333, 0x22a2f4f8, 0xc4df2862,
  61. 0x58c542c5, 0xcac933ae, 0xcdb72c7e, 0xf383a3e2, 0x00000000, 0xd2bec333, 0x539eba45, 0xc4df2862,
  62. 0xac6145bb, 0x187de2a7, 0xdd5d0b08, 0xe7821d59, 0x4b418bbe, 0xc13ad060, 0xa73abd3b, 0x3536cc52,
  63. };
  64. /**************************************************************************************
  65. * Function: BitReverse32
  66. *
  67. * Description: Ken's fast in-place bit reverse
  68. *
  69. * Inputs: buffer of 32 complex samples
  70. *
  71. * Outputs: bit-reversed samples in same buffer
  72. *
  73. * Return: none
  74. **************************************************************************************/
  75. static void BitReverse32(int *inout)
  76. {
  77. int t, t1;
  78. swapcplx(inout[2], inout[32]);
  79. swapcplx(inout[4], inout[16]);
  80. swapcplx(inout[6], inout[48]);
  81. swapcplx(inout[10], inout[40]);
  82. swapcplx(inout[12], inout[24]);
  83. swapcplx(inout[14], inout[56]);
  84. swapcplx(inout[18], inout[36]);
  85. swapcplx(inout[22], inout[52]);
  86. swapcplx(inout[26], inout[44]);
  87. swapcplx(inout[30], inout[60]);
  88. swapcplx(inout[38], inout[50]);
  89. swapcplx(inout[46], inout[58]);
  90. }
  91. /**************************************************************************************
  92. * Function: R8FirstPass32
  93. *
  94. * Description: radix-8 trivial pass for decimation-in-time FFT (log2(N) = 5)
  95. *
  96. * Inputs: buffer of (bit-reversed) samples
  97. *
  98. * Outputs: processed samples in same buffer
  99. *
  100. * Return: none
  101. *
  102. * Notes: assumes 3 guard bits, gains 1 integer bit
  103. * guard bits out = guard bits in - 3 (if inputs are full scale)
  104. * or guard bits in - 2 (if inputs bounded to +/- sqrt(2)/2)
  105. * see scaling comments in fft.c for base AAC
  106. * should compile with no stack spills on ARM (verify compiled output)
  107. * current instruction count (per pass): 16 LDR, 16 STR, 4 SMULL, 61 ALU
  108. **************************************************************************************/
  109. static void R8FirstPass32(int *r0)
  110. {
  111. int r1, r2, r3, r4, r5, r6, r7;
  112. int r8, r9, r10, r11, r12, r14;
  113. /* number of passes = fft size / 8 = 32 / 8 = 4 */
  114. r1 = (32 >> 3);
  115. do {
  116. r2 = r0[8];
  117. r3 = r0[9];
  118. r4 = r0[10];
  119. r5 = r0[11];
  120. r6 = r0[12];
  121. r7 = r0[13];
  122. r8 = r0[14];
  123. r9 = r0[15];
  124. r10 = r2 + r4;
  125. r11 = r3 + r5;
  126. r12 = r6 + r8;
  127. r14 = r7 + r9;
  128. r2 -= r4;
  129. r3 -= r5;
  130. r6 -= r8;
  131. r7 -= r9;
  132. r4 = r2 - r7;
  133. r5 = r2 + r7;
  134. r8 = r3 - r6;
  135. r9 = r3 + r6;
  136. r2 = r4 - r9;
  137. r3 = r4 + r9;
  138. r6 = r5 - r8;
  139. r7 = r5 + r8;
  140. r2 = MULSHIFT32(SQRT1_2, r2); /* can use r4, r5, r8, or r9 for constant and lo32 scratch reg */
  141. r3 = MULSHIFT32(SQRT1_2, r3);
  142. r6 = MULSHIFT32(SQRT1_2, r6);
  143. r7 = MULSHIFT32(SQRT1_2, r7);
  144. r4 = r10 + r12;
  145. r5 = r10 - r12;
  146. r8 = r11 + r14;
  147. r9 = r11 - r14;
  148. r10 = r0[0];
  149. r11 = r0[2];
  150. r12 = r0[4];
  151. r14 = r0[6];
  152. r10 += r11;
  153. r12 += r14;
  154. r4 >>= 1;
  155. r10 += r12;
  156. r4 += (r10 >> 1);
  157. r0[ 0] = r4;
  158. r4 -= (r10 >> 1);
  159. r4 = (r10 >> 1) - r4;
  160. r0[ 8] = r4;
  161. r9 >>= 1;
  162. r10 -= 2*r12;
  163. r4 = (r10 >> 1) + r9;
  164. r0[ 4] = r4;
  165. r4 = (r10 >> 1) - r9;
  166. r0[12] = r4;
  167. r10 += r12;
  168. r10 -= 2*r11;
  169. r12 -= 2*r14;
  170. r4 = r0[1];
  171. r9 = r0[3];
  172. r11 = r0[5];
  173. r14 = r0[7];
  174. r4 += r9;
  175. r11 += r14;
  176. r8 >>= 1;
  177. r4 += r11;
  178. r8 += (r4 >> 1);
  179. r0[ 1] = r8;
  180. r8 -= (r4 >> 1);
  181. r8 = (r4 >> 1) - r8;
  182. r0[ 9] = r8;
  183. r5 >>= 1;
  184. r4 -= 2*r11;
  185. r8 = (r4 >> 1) - r5;
  186. r0[ 5] = r8;
  187. r8 = (r4 >> 1) + r5;
  188. r0[13] = r8;
  189. r4 += r11;
  190. r4 -= 2*r9;
  191. r11 -= 2*r14;
  192. r9 = r10 - r11;
  193. r10 += r11;
  194. r14 = r4 + r12;
  195. r4 -= r12;
  196. r5 = (r10 >> 1) + r7;
  197. r8 = (r4 >> 1) - r6;
  198. r0[ 2] = r5;
  199. r0[ 3] = r8;
  200. r5 = (r9 >> 1) - r2;
  201. r8 = (r14 >> 1) - r3;
  202. r0[ 6] = r5;
  203. r0[ 7] = r8;
  204. r5 = (r10 >> 1) - r7;
  205. r8 = (r4 >> 1) + r6;
  206. r0[10] = r5;
  207. r0[11] = r8;
  208. r5 = (r9 >> 1) + r2;
  209. r8 = (r14 >> 1) + r3;
  210. r0[14] = r5;
  211. r0[15] = r8;
  212. r0 += 16;
  213. r1--;
  214. } while (r1 != 0);
  215. }
  216. /**************************************************************************************
  217. * Function: R4Core32
  218. *
  219. * Description: radix-4 pass for 32-point decimation-in-time FFT
  220. *
  221. * Inputs: buffer of samples
  222. *
  223. * Outputs: processed samples in same buffer
  224. *
  225. * Return: none
  226. *
  227. * Notes: gain 2 integer bits
  228. * guard bits out = guard bits in - 1 (if inputs are full scale)
  229. * see scaling comments in fft.c for base AAC
  230. * uses 3-mul, 3-add butterflies instead of 4-mul, 2-add
  231. * should compile with no stack spills on ARM (verify compiled output)
  232. * current instruction count (per pass): 16 LDR, 16 STR, 4 SMULL, 61 ALU
  233. **************************************************************************************/
  234. static void R4Core32(int *r0)
  235. {
  236. int r2, r3, r4, r5, r6, r7;
  237. int r8, r9, r10, r12, r14;
  238. int *r1;
  239. r1 = (int *)twidTabOdd32;
  240. r10 = 8;
  241. do {
  242. /* can use r14 for lo32 scratch register in all MULSHIFT32 */
  243. r2 = r1[0];
  244. r3 = r1[1];
  245. r4 = r0[16];
  246. r5 = r0[17];
  247. r12 = r4 + r5;
  248. r12 = MULSHIFT32(r3, r12);
  249. r5 = MULSHIFT32(r2, r5) + r12;
  250. r2 += 2*r3;
  251. r4 = MULSHIFT32(r2, r4) - r12;
  252. r2 = r1[2];
  253. r3 = r1[3];
  254. r6 = r0[32];
  255. r7 = r0[33];
  256. r12 = r6 + r7;
  257. r12 = MULSHIFT32(r3, r12);
  258. r7 = MULSHIFT32(r2, r7) + r12;
  259. r2 += 2*r3;
  260. r6 = MULSHIFT32(r2, r6) - r12;
  261. r2 = r1[4];
  262. r3 = r1[5];
  263. r8 = r0[48];
  264. r9 = r0[49];
  265. r12 = r8 + r9;
  266. r12 = MULSHIFT32(r3, r12);
  267. r9 = MULSHIFT32(r2, r9) + r12;
  268. r2 += 2*r3;
  269. r8 = MULSHIFT32(r2, r8) - r12;
  270. r2 = r0[0];
  271. r3 = r0[1];
  272. r12 = r6 + r8;
  273. r8 = r6 - r8;
  274. r14 = r9 - r7;
  275. r9 = r9 + r7;
  276. r6 = (r2 >> 2) - r4;
  277. r7 = (r3 >> 2) - r5;
  278. r4 += (r2 >> 2);
  279. r5 += (r3 >> 2);
  280. r2 = r4 + r12;
  281. r3 = r5 + r9;
  282. r0[0] = r2;
  283. r0[1] = r3;
  284. r2 = r6 - r14;
  285. r3 = r7 - r8;
  286. r0[16] = r2;
  287. r0[17] = r3;
  288. r2 = r4 - r12;
  289. r3 = r5 - r9;
  290. r0[32] = r2;
  291. r0[33] = r3;
  292. r2 = r6 + r14;
  293. r3 = r7 + r8;
  294. r0[48] = r2;
  295. r0[49] = r3;
  296. r0 += 2;
  297. r1 += 6;
  298. r10--;
  299. } while (r10 != 0);
  300. }
  301. /**************************************************************************************
  302. * Function: FFT32C
  303. *
  304. * Description: Ken's very fast in-place radix-4 decimation-in-time FFT
  305. *
  306. * Inputs: buffer of 32 complex samples (before bit-reversal)
  307. *
  308. * Outputs: processed samples in same buffer
  309. *
  310. * Return: none
  311. *
  312. * Notes: assumes 3 guard bits in, gains 3 integer bits
  313. * guard bits out = guard bits in - 2
  314. * (guard bit analysis includes assumptions about steps immediately
  315. * before and after, i.e. PreMul and PostMul for DCT)
  316. **************************************************************************************/
  317. void FFT32C(int *x)
  318. {
  319. /* decimation in time */
  320. BitReverse32(x);
  321. /* 32-point complex FFT */
  322. R8FirstPass32(x); /* gain 1 int bit, lose 2 GB (making assumptions about input) */
  323. R4Core32(x); /* gain 2 int bits, lose 0 GB (making assumptions about input) */
  324. }