cdct.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*____________________________________________________________________________
  2. FreeAmp - The Free MP3 Player
  3. MP3 Decoder originally Copyright (C) 1995-1997 Xing Technology
  4. Corp. http://www.xingtech.com
  5. Portions Copyright (C) 1998-1999 EMusic.com
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. $Id: cdct.c,v 1.11 1999/10/19 07:13:08 elrod Exp $
  18. ____________________________________________________________________________*/
  19. /**** cdct.c ***************************************************
  20. mod 5/16/95 first stage in 8 pt dct does not drop last sb mono
  21. MPEG audio decoder, dct
  22. portable C
  23. ******************************************************************/
  24. #include "config.h"
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <float.h>
  28. #include <math.h>
  29. #pragma warning ( disable : 4711 ) // function 'xxxx' selected for automatic inline expansion
  30. float coef32[31]; /* 32 pt dct coefs */ // !!!!!!!!!!!!!!!!!! (only generated once (always to same value)
  31. /*------------------------------------------------------------*/
  32. float *dct_coef_addr()
  33. {
  34. return coef32;
  35. }
  36. /*------------------------------------------------------------*/
  37. static void forward_bf(int m, int n, float x[], float f[], float coef[])
  38. {
  39. int i, j, n2;
  40. int p, q, p0, k;
  41. p0 = 0;
  42. n2 = n >> 1;
  43. for (i = 0; i < m; i++, p0 += n)
  44. {
  45. k = 0;
  46. p = p0;
  47. q = p + n - 1;
  48. for (j = 0; j < n2; j++, p++, q--, k++)
  49. {
  50. f[p] = x[p] + x[q];
  51. f[n2 + p] = coef[k] * (x[p] - x[q]);
  52. }
  53. }
  54. }
  55. /*------------------------------------------------------------*/
  56. static void back_bf(int m, int n, float x[], float f[])
  57. {
  58. int i, j, n2, n21;
  59. int p, q, p0;
  60. p0 = 0;
  61. n2 = n >> 1;
  62. n21 = n2 - 1;
  63. for (i = 0; i < m; i++, p0 += n)
  64. {
  65. p = p0;
  66. q = p0;
  67. for (j = 0; j < n2; j++, p += 2, q++)
  68. f[p] = x[q];
  69. p = p0 + 1;
  70. for (j = 0; j < n21; j++, p += 2, q++)
  71. f[p] = x[q] + x[q + 1];
  72. f[p] = x[q];
  73. }
  74. }
  75. /*------------------------------------------------------------*/
  76. void fdct32(float x[], float c[])
  77. {
  78. float a[32]; /* ping pong buffers */
  79. float b[32];
  80. int p, q;
  81. float *src = x;
  82. /* special first stage */
  83. for (p = 0, q = 31; p < 16; p++, q--)
  84. {
  85. a[p] = src[p] + src[q];
  86. a[16 + p] = coef32[p] * (src[p] - src[q]);
  87. }
  88. forward_bf(2, 16, a, b, coef32 + 16);
  89. forward_bf(4, 8, b, a, coef32 + 16 + 8);
  90. forward_bf(8, 4, a, b, coef32 + 16 + 8 + 4);
  91. forward_bf(16, 2, b, a, coef32 + 16 + 8 + 4 + 2);
  92. back_bf(8, 4, a, b);
  93. back_bf(4, 8, b, a);
  94. back_bf(2, 16, a, b);
  95. back_bf(1, 32, b, c);
  96. }
  97. /*------------------------------------------------------------*/
  98. void fdct32_dual(float x[], float c[])
  99. {
  100. float a[32]; /* ping pong buffers */
  101. float b[32];
  102. int p, pp, qq;
  103. /* special first stage for dual chan (interleaved x) */
  104. pp = 0;
  105. qq = 2 * 31;
  106. for (p = 0; p < 16; p++, pp += 2, qq -= 2)
  107. {
  108. a[p] = x[pp] + x[qq];
  109. a[16 + p] = coef32[p] * (x[pp] - x[qq]);
  110. }
  111. forward_bf(2, 16, a, b, coef32 + 16);
  112. forward_bf(4, 8, b, a, coef32 + 16 + 8);
  113. forward_bf(8, 4, a, b, coef32 + 16 + 8 + 4);
  114. forward_bf(16, 2, b, a, coef32 + 16 + 8 + 4 + 2);
  115. back_bf(8, 4, a, b);
  116. back_bf(4, 8, b, a);
  117. back_bf(2, 16, a, b);
  118. back_bf(1, 32, b, c);
  119. }
  120. /*---------------convert dual to mono------------------------------*/
  121. void fdct32_dual_mono(float x[], float c[])
  122. {
  123. float a[32]; /* ping pong buffers */
  124. float b[32];
  125. float t1, t2;
  126. int p, pp, qq;
  127. /* special first stage */
  128. pp = 0;
  129. qq = 2 * 31;
  130. for (p = 0; p < 16; p++, pp += 2, qq -= 2)
  131. {
  132. t1 = 0.5F * (x[pp] + x[pp + 1]);
  133. t2 = 0.5F * (x[qq] + x[qq + 1]);
  134. a[p] = t1 + t2;
  135. a[16 + p] = coef32[p] * (t1 - t2);
  136. }
  137. forward_bf(2, 16, a, b, coef32 + 16);
  138. forward_bf(4, 8, b, a, coef32 + 16 + 8);
  139. forward_bf(8, 4, a, b, coef32 + 16 + 8 + 4);
  140. forward_bf(16, 2, b, a, coef32 + 16 + 8 + 4 + 2);
  141. back_bf(8, 4, a, b);
  142. back_bf(4, 8, b, a);
  143. back_bf(2, 16, a, b);
  144. back_bf(1, 32, b, c);
  145. }
  146. /*------------------------------------------------------------*/
  147. /*---------------- 16 pt fdct -------------------------------*/
  148. void fdct16(float x[], float c[])
  149. {
  150. float a[16]; /* ping pong buffers */
  151. float b[16];
  152. int p, q;
  153. /* special first stage (drop highest sb) */
  154. a[0] = x[0];
  155. a[8] = coef32[16] * x[0];
  156. for (p = 1, q = 14; p < 8; p++, q--)
  157. {
  158. a[p] = x[p] + x[q];
  159. a[8 + p] = coef32[16 + p] * (x[p] - x[q]);
  160. }
  161. forward_bf(2, 8, a, b, coef32 + 16 + 8);
  162. forward_bf(4, 4, b, a, coef32 + 16 + 8 + 4);
  163. forward_bf(8, 2, a, b, coef32 + 16 + 8 + 4 + 2);
  164. back_bf(4, 4, b, a);
  165. back_bf(2, 8, a, b);
  166. back_bf(1, 16, b, c);
  167. }
  168. /*------------------------------------------------------------*/
  169. /*---------------- 16 pt fdct dual chan---------------------*/
  170. void fdct16_dual(float x[], float c[])
  171. {
  172. float a[16]; /* ping pong buffers */
  173. float b[16];
  174. int p, pp, qq;
  175. /* special first stage for interleaved input */
  176. a[0] = x[0];
  177. a[8] = coef32[16] * x[0];
  178. pp = 2;
  179. qq = 2 * 14;
  180. for (p = 1; p < 8; p++, pp += 2, qq -= 2)
  181. {
  182. a[p] = x[pp] + x[qq];
  183. a[8 + p] = coef32[16 + p] * (x[pp] - x[qq]);
  184. }
  185. forward_bf(2, 8, a, b, coef32 + 16 + 8);
  186. forward_bf(4, 4, b, a, coef32 + 16 + 8 + 4);
  187. forward_bf(8, 2, a, b, coef32 + 16 + 8 + 4 + 2);
  188. back_bf(4, 4, b, a);
  189. back_bf(2, 8, a, b);
  190. back_bf(1, 16, b, c);
  191. }
  192. /*------------------------------------------------------------*/
  193. /*---------------- 16 pt fdct dual to mono-------------------*/
  194. void fdct16_dual_mono(float x[], float c[])
  195. {
  196. float a[16]; /* ping pong buffers */
  197. float b[16];
  198. float t1, t2;
  199. int p, pp, qq;
  200. /* special first stage */
  201. a[0] = 0.5F * (x[0] + x[1]);
  202. a[8] = coef32[16] * a[0];
  203. pp = 2;
  204. qq = 2 * 14;
  205. for (p = 1; p < 8; p++, pp += 2, qq -= 2)
  206. {
  207. t1 = 0.5F * (x[pp] + x[pp + 1]);
  208. t2 = 0.5F * (x[qq] + x[qq + 1]);
  209. a[p] = t1 + t2;
  210. a[8 + p] = coef32[16 + p] * (t1 - t2);
  211. }
  212. forward_bf(2, 8, a, b, coef32 + 16 + 8);
  213. forward_bf(4, 4, b, a, coef32 + 16 + 8 + 4);
  214. forward_bf(8, 2, a, b, coef32 + 16 + 8 + 4 + 2);
  215. back_bf(4, 4, b, a);
  216. back_bf(2, 8, a, b);
  217. back_bf(1, 16, b, c);
  218. }
  219. /*------------------------------------------------------------*/
  220. /*---------------- 8 pt fdct -------------------------------*/
  221. void fdct8(float x[], float c[])
  222. {
  223. float a[8]; /* ping pong buffers */
  224. float b[8];
  225. int p, q;
  226. /* special first stage */
  227. b[0] = x[0] + x[7];
  228. b[4] = coef32[16 + 8] * (x[0] - x[7]);
  229. for (p = 1, q = 6; p < 4; p++, q--)
  230. {
  231. b[p] = x[p] + x[q];
  232. b[4 + p] = coef32[16 + 8 + p] * (x[p] - x[q]);
  233. }
  234. forward_bf(2, 4, b, a, coef32 + 16 + 8 + 4);
  235. forward_bf(4, 2, a, b, coef32 + 16 + 8 + 4 + 2);
  236. back_bf(2, 4, b, a);
  237. back_bf(1, 8, a, c);
  238. }
  239. /*------------------------------------------------------------*/
  240. /*---------------- 8 pt fdct dual chan---------------------*/
  241. void fdct8_dual(float x[], float c[])
  242. {
  243. float a[8]; /* ping pong buffers */
  244. float b[8];
  245. int p, pp, qq;
  246. /* special first stage for interleaved input */
  247. b[0] = x[0] + x[14];
  248. b[4] = coef32[16 + 8] * (x[0] - x[14]);
  249. pp = 2;
  250. qq = 2 * 6;
  251. for (p = 1; p < 4; p++, pp += 2, qq -= 2)
  252. {
  253. b[p] = x[pp] + x[qq];
  254. b[4 + p] = coef32[16 + 8 + p] * (x[pp] - x[qq]);
  255. }
  256. forward_bf(2, 4, b, a, coef32 + 16 + 8 + 4);
  257. forward_bf(4, 2, a, b, coef32 + 16 + 8 + 4 + 2);
  258. back_bf(2, 4, b, a);
  259. back_bf(1, 8, a, c);
  260. }
  261. /*------------------------------------------------------------*/
  262. /*---------------- 8 pt fdct dual to mono---------------------*/
  263. void fdct8_dual_mono(float x[], float c[])
  264. {
  265. float a[8]; /* ping pong buffers */
  266. float b[8];
  267. float t1, t2;
  268. int p, pp, qq;
  269. /* special first stage */
  270. t1 = 0.5F * (x[0] + x[1]);
  271. t2 = 0.5F * (x[14] + x[15]);
  272. b[0] = t1 + t2;
  273. b[4] = coef32[16 + 8] * (t1 - t2);
  274. pp = 2;
  275. qq = 2 * 6;
  276. for (p = 1; p < 4; p++, pp += 2, qq -= 2)
  277. {
  278. t1 = 0.5F * (x[pp] + x[pp + 1]);
  279. t2 = 0.5F * (x[qq] + x[qq + 1]);
  280. b[p] = t1 + t2;
  281. b[4 + p] = coef32[16 + 8 + p] * (t1 - t2);
  282. }
  283. forward_bf(2, 4, b, a, coef32 + 16 + 8 + 4);
  284. forward_bf(4, 2, a, b, coef32 + 16 + 8 + 4 + 2);
  285. back_bf(2, 4, b, a);
  286. back_bf(1, 8, a, c);
  287. }
  288. /*------------------------------------------------------------*/