csbt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 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: csbt.c,v 1.2 1999/10/19 07:13:08 elrod Exp $
  18. ____________________________________________________________________________*/
  19. /**** csbt.c ***************************************************
  20. MPEG audio decoder, dct and window
  21. portable C
  22. 1/7/96 mod for Layer III
  23. ******************************************************************/
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <float.h>
  27. #include <math.h>
  28. void fdct32(float *, float *);
  29. void fdct32_dual(float *, float *);
  30. void fdct32_dual_mono(float *, float *);
  31. void fdct16(float *, float *);
  32. void fdct16_dual(float *, float *);
  33. void fdct16_dual_mono(float *, float *);
  34. void fdct8(float *, float *);
  35. void fdct8_dual(float *, float *);
  36. void fdct8_dual_mono(float *, float *);
  37. void window(float *vbuf, int vb_ptr, short *pcm);
  38. void window_dual(float *vbuf, int vb_ptr, short *pcm);
  39. void window16(float *vbuf, int vb_ptr, short *pcm);
  40. void window16_dual(float *vbuf, int vb_ptr, short *pcm);
  41. void window8(float *vbuf, int vb_ptr, short *pcm);
  42. void window8_dual(float *vbuf, int vb_ptr, short *pcm);
  43. /*-------------------------------------------------------------------------*/
  44. /* circular window buffers */
  45. #include "mp3struct.h"
  46. ////static signed int vb_ptr; // !!!!!!!!!!!!!
  47. ////static signed int vb2_ptr; // !!!!!!!!!!!!!
  48. ////static float pMP3Stream->vbuf[512]; // !!!!!!!!!!!!!
  49. ////static float vbuf2[512]; // !!!!!!!!!!!!!
  50. float *dct_coef_addr();
  51. /*======================================================================*/
  52. static void gencoef() /* gen coef for N=32 (31 coefs) */
  53. {
  54. static int iOnceOnly = 0;
  55. int p, n, i, k;
  56. double t, pi;
  57. float *coef32;
  58. if (!iOnceOnly++)
  59. {
  60. coef32 = dct_coef_addr();
  61. pi = 4.0 * atan(1.0);
  62. n = 16;
  63. k = 0;
  64. for (i = 0; i < 5; i++, n = n / 2)
  65. {
  66. for (p = 0; p < n; p++, k++)
  67. {
  68. t = (pi / (4 * n)) * (2 * p + 1);
  69. coef32[k] = (float) (0.50 / cos(t));
  70. }
  71. }
  72. }
  73. }
  74. /*------------------------------------------------------------*/
  75. void sbt_init()
  76. {
  77. int i;
  78. static int first_pass = 1;
  79. if (first_pass)
  80. {
  81. gencoef();
  82. first_pass = 0;
  83. }
  84. /* clear window pMP3Stream->vbuf */
  85. for (i = 0; i < 512; i++)
  86. {
  87. pMP3Stream->vbuf[i] = 0.0F;
  88. pMP3Stream->vbuf2[i] = 0.0F;
  89. }
  90. pMP3Stream->vb2_ptr = pMP3Stream->vb_ptr = 0;
  91. }
  92. /*============================================================*/
  93. /*============================================================*/
  94. /*============================================================*/
  95. void sbt_mono(float *sample, short *pcm, int n)
  96. {
  97. int i;
  98. for (i = 0; i < n; i++)
  99. {
  100. fdct32(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  101. window(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  102. sample += 64;
  103. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 32) & 511;
  104. pcm += 32;
  105. }
  106. }
  107. /*------------------------------------------------------------*/
  108. void sbt_dual(float *sample, short *pcm, int n)
  109. {
  110. int i;
  111. for (i = 0; i < n; i++)
  112. {
  113. fdct32_dual(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  114. fdct32_dual(sample + 1, pMP3Stream->vbuf2 + pMP3Stream->vb_ptr);
  115. window_dual(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  116. window_dual(pMP3Stream->vbuf2, pMP3Stream->vb_ptr, pcm + 1);
  117. sample += 64;
  118. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 32) & 511;
  119. pcm += 64;
  120. }
  121. }
  122. /*------------------------------------------------------------*/
  123. /* convert dual to mono */
  124. void sbt_dual_mono(float *sample, short *pcm, int n)
  125. {
  126. int i;
  127. for (i = 0; i < n; i++)
  128. {
  129. fdct32_dual_mono(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  130. window(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  131. sample += 64;
  132. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 32) & 511;
  133. pcm += 32;
  134. }
  135. }
  136. /*------------------------------------------------------------*/
  137. /* convert dual to left */
  138. void sbt_dual_left(float *sample, short *pcm, int n)
  139. {
  140. int i;
  141. for (i = 0; i < n; i++)
  142. {
  143. fdct32_dual(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  144. window(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  145. sample += 64;
  146. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 32) & 511;
  147. pcm += 32;
  148. }
  149. }
  150. /*------------------------------------------------------------*/
  151. /* convert dual to right */
  152. void sbt_dual_right(float *sample, short *pcm, int n)
  153. {
  154. int i;
  155. sample++; /* point to right chan */
  156. for (i = 0; i < n; i++)
  157. {
  158. fdct32_dual(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  159. window(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  160. sample += 64;
  161. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 32) & 511;
  162. pcm += 32;
  163. }
  164. }
  165. /*------------------------------------------------------------*/
  166. /*---------------- 16 pt sbt's -------------------------------*/
  167. /*------------------------------------------------------------*/
  168. void sbt16_mono(float *sample, short *pcm, int n)
  169. {
  170. int i;
  171. for (i = 0; i < n; i++)
  172. {
  173. fdct16(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  174. window16(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  175. sample += 64;
  176. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 16) & 255;
  177. pcm += 16;
  178. }
  179. }
  180. /*------------------------------------------------------------*/
  181. void sbt16_dual(float *sample, short *pcm, int n)
  182. {
  183. int i;
  184. for (i = 0; i < n; i++)
  185. {
  186. fdct16_dual(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  187. fdct16_dual(sample + 1, pMP3Stream->vbuf2 + pMP3Stream->vb_ptr);
  188. window16_dual(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  189. window16_dual(pMP3Stream->vbuf2, pMP3Stream->vb_ptr, pcm + 1);
  190. sample += 64;
  191. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 16) & 255;
  192. pcm += 32;
  193. }
  194. }
  195. /*------------------------------------------------------------*/
  196. void sbt16_dual_mono(float *sample, short *pcm, int n)
  197. {
  198. int i;
  199. for (i = 0; i < n; i++)
  200. {
  201. fdct16_dual_mono(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  202. window16(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  203. sample += 64;
  204. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 16) & 255;
  205. pcm += 16;
  206. }
  207. }
  208. /*------------------------------------------------------------*/
  209. void sbt16_dual_left(float *sample, short *pcm, int n)
  210. {
  211. int i;
  212. for (i = 0; i < n; i++)
  213. {
  214. fdct16_dual(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  215. window16(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  216. sample += 64;
  217. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 16) & 255;
  218. pcm += 16;
  219. }
  220. }
  221. /*------------------------------------------------------------*/
  222. void sbt16_dual_right(float *sample, short *pcm, int n)
  223. {
  224. int i;
  225. sample++;
  226. for (i = 0; i < n; i++)
  227. {
  228. fdct16_dual(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  229. window16(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  230. sample += 64;
  231. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 16) & 255;
  232. pcm += 16;
  233. }
  234. }
  235. /*------------------------------------------------------------*/
  236. /*---------------- 8 pt sbt's -------------------------------*/
  237. /*------------------------------------------------------------*/
  238. void sbt8_mono(float *sample, short *pcm, int n)
  239. {
  240. int i;
  241. for (i = 0; i < n; i++)
  242. {
  243. fdct8(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  244. window8(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  245. sample += 64;
  246. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 8) & 127;
  247. pcm += 8;
  248. }
  249. }
  250. /*------------------------------------------------------------*/
  251. void sbt8_dual(float *sample, short *pcm, int n)
  252. {
  253. int i;
  254. for (i = 0; i < n; i++)
  255. {
  256. fdct8_dual(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  257. fdct8_dual(sample + 1, pMP3Stream->vbuf2 + pMP3Stream->vb_ptr);
  258. window8_dual(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  259. window8_dual(pMP3Stream->vbuf2, pMP3Stream->vb_ptr, pcm + 1);
  260. sample += 64;
  261. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 8) & 127;
  262. pcm += 16;
  263. }
  264. }
  265. /*------------------------------------------------------------*/
  266. void sbt8_dual_mono(float *sample, short *pcm, int n)
  267. {
  268. int i;
  269. for (i = 0; i < n; i++)
  270. {
  271. fdct8_dual_mono(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  272. window8(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  273. sample += 64;
  274. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 8) & 127;
  275. pcm += 8;
  276. }
  277. }
  278. /*------------------------------------------------------------*/
  279. void sbt8_dual_left(float *sample, short *pcm, int n)
  280. {
  281. int i;
  282. for (i = 0; i < n; i++)
  283. {
  284. fdct8_dual(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  285. window8(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  286. sample += 64;
  287. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 8) & 127;
  288. pcm += 8;
  289. }
  290. }
  291. /*------------------------------------------------------------*/
  292. void sbt8_dual_right(float *sample, short *pcm, int n)
  293. {
  294. int i;
  295. sample++;
  296. for (i = 0; i < n; i++)
  297. {
  298. fdct8_dual(sample, pMP3Stream->vbuf + pMP3Stream->vb_ptr);
  299. window8(pMP3Stream->vbuf, pMP3Stream->vb_ptr, pcm);
  300. sample += 64;
  301. pMP3Stream->vb_ptr = (pMP3Stream->vb_ptr - 8) & 127;
  302. pcm += 8;
  303. }
  304. }
  305. /*------------------------------------------------------------*/
  306. /*------------------------------------------------------------*/
  307. #define COMPILE_ME
  308. #include "csbtb.c" /* 8 bit output */
  309. #include "csbtL3.c" /* Layer III */
  310. /*------------------------------------------------------------*/