msis.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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: msis.c,v 1.4 1999/10/19 07:13:09 elrod Exp $
  18. ____________________________________________________________________________*/
  19. /**** msis.c ***************************************************
  20. Layer III
  21. antialias, ms and is stereo precessing
  22. **** is_process assumes never switch
  23. from short to long in is region *****
  24. is_process does ms or stereo in "forbidded sf regions"
  25. //ms_mode = 0
  26. lr[0][i][0] = 1.0f;
  27. lr[0][i][1] = 0.0f;
  28. // ms_mode = 1, in is bands is routine does ms processing
  29. lr[1][i][0] = 1.0f;
  30. lr[1][i][1] = 1.0f;
  31. ******************************************************************/
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <float.h>
  35. #include <math.h>
  36. #include "L3.h"
  37. #include "mp3struct.h"
  38. typedef float ARRAY2[2];
  39. typedef float ARRAY8_2[8][2];
  40. float csa[8][2]; /* antialias */ // effectively constant
  41. /* pMP3Stream->nBand[0] = long, pMP3Stream->nBand[1] = short */
  42. ////@@@@extern int pMP3Stream->nBand[2][22];
  43. ////@@@@extern int pMP3Stream->sfBandIndex[2][22]; /* [long/short][cb] */
  44. /* intensity stereo */
  45. /* if ms mode quant pre-scales all values by 1.0/sqrt(2.0) ms_mode in table
  46. compensates */
  47. static float lr[2][8][2]; /* [ms_mode 0/1][sf][left/right] */ // effectively constant
  48. /* intensity stereo MPEG2 */
  49. /* lr2[intensity_scale][ms_mode][sflen_offset+sf][left/right] */
  50. typedef float ARRAY2_64_2[2][64][2];
  51. typedef float ARRAY64_2[64][2];
  52. static float lr2[2][2][64][2]; // effectively constant
  53. /*===============================================================*/
  54. ARRAY2 *alias_init_addr()
  55. {
  56. return csa;
  57. }
  58. /*-----------------------------------------------------------*/
  59. ARRAY8_2 *msis_init_addr()
  60. {
  61. /*-------
  62. pi = 4.0*atan(1.0);
  63. t = pi/12.0;
  64. for(i=0;i<7;i++) {
  65. s = sin(i*t);
  66. c = cos(i*t);
  67. // ms_mode = 0
  68. lr[0][i][0] = (float)(s/(s+c));
  69. lr[0][i][1] = (float)(c/(s+c));
  70. // ms_mode = 1
  71. lr[1][i][0] = (float)(sqrt(2.0)*(s/(s+c)));
  72. lr[1][i][1] = (float)(sqrt(2.0)*(c/(s+c)));
  73. }
  74. //sf = 7
  75. //ms_mode = 0
  76. lr[0][i][0] = 1.0f;
  77. lr[0][i][1] = 0.0f;
  78. // ms_mode = 1, in is bands is routine does ms processing
  79. lr[1][i][0] = 1.0f;
  80. lr[1][i][1] = 1.0f;
  81. ------------*/
  82. return lr;
  83. }
  84. /*-------------------------------------------------------------*/
  85. ARRAY2_64_2 *msis_init_addr_MPEG2()
  86. {
  87. return lr2;
  88. }
  89. /*===============================================================*/
  90. void antialias(float x[], int n)
  91. {
  92. int i, k;
  93. float a, b;
  94. for (k = 0; k < n; k++)
  95. {
  96. for (i = 0; i < 8; i++)
  97. {
  98. a = x[17 - i];
  99. b = x[18 + i];
  100. x[17 - i] = a * csa[i][0] - b * csa[i][1];
  101. x[18 + i] = b * csa[i][0] + a * csa[i][1];
  102. }
  103. x += 18;
  104. }
  105. }
  106. /*===============================================================*/
  107. void ms_process(float x[][1152], int n) /* sum-difference stereo */
  108. {
  109. int i;
  110. float xl, xr;
  111. /*-- note: sqrt(2) done scaling by dequant ---*/
  112. for (i = 0; i < n; i++)
  113. {
  114. xl = x[0][i] + x[1][i];
  115. xr = x[0][i] - x[1][i];
  116. x[0][i] = xl;
  117. x[1][i] = xr;
  118. }
  119. return;
  120. }
  121. /*===============================================================*/
  122. void is_process_MPEG1(float x[][1152], /* intensity stereo */
  123. SCALEFACT * sf,
  124. CB_INFO cb_info[2], /* [ch] */
  125. int nsamp, int ms_mode)
  126. {
  127. int i, j, n, cb, w;
  128. float fl, fr;
  129. int m;
  130. int isf;
  131. float fls[3], frs[3];
  132. int cb0;
  133. cb0 = cb_info[1].cbmax; /* start at end of right */
  134. i = pMP3Stream->sfBandIndex[cb_info[1].cbtype][cb0];
  135. cb0++;
  136. m = nsamp - i; /* process to len of left */
  137. if (cb_info[1].cbtype)
  138. goto short_blocks;
  139. /*------------------------*/
  140. /* long_blocks: */
  141. for (cb = cb0; cb < 21; cb++)
  142. {
  143. isf = sf->l[cb];
  144. n = pMP3Stream->nBand[0][cb];
  145. fl = lr[ms_mode][isf][0];
  146. fr = lr[ms_mode][isf][1];
  147. for (j = 0; j < n; j++, i++)
  148. {
  149. if (--m < 0)
  150. goto exit;
  151. x[1][i] = fr * x[0][i];
  152. x[0][i] = fl * x[0][i];
  153. }
  154. }
  155. return;
  156. /*------------------------*/
  157. short_blocks:
  158. for (cb = cb0; cb < 12; cb++)
  159. {
  160. for (w = 0; w < 3; w++)
  161. {
  162. isf = sf->s[w][cb];
  163. fls[w] = lr[ms_mode][isf][0];
  164. frs[w] = lr[ms_mode][isf][1];
  165. }
  166. n = pMP3Stream->nBand[1][cb];
  167. for (j = 0; j < n; j++)
  168. {
  169. m -= 3;
  170. if (m < 0)
  171. goto exit;
  172. x[1][i] = frs[0] * x[0][i];
  173. x[0][i] = fls[0] * x[0][i];
  174. x[1][1 + i] = frs[1] * x[0][1 + i];
  175. x[0][1 + i] = fls[1] * x[0][1 + i];
  176. x[1][2 + i] = frs[2] * x[0][2 + i];
  177. x[0][2 + i] = fls[2] * x[0][2 + i];
  178. i += 3;
  179. }
  180. }
  181. exit:
  182. return;
  183. }
  184. /*===============================================================*/
  185. void is_process_MPEG2(float x[][1152], /* intensity stereo */
  186. SCALEFACT * sf,
  187. CB_INFO cb_info[2], /* [ch] */
  188. IS_SF_INFO * is_sf_info,
  189. int nsamp, int ms_mode)
  190. {
  191. int i, j, k, n, cb, w;
  192. float fl, fr;
  193. int m;
  194. int isf;
  195. int il[21];
  196. int tmp;
  197. int r;
  198. ARRAY2 *lr;
  199. int cb0, cb1;
  200. lr = lr2[is_sf_info->intensity_scale][ms_mode];
  201. if (cb_info[1].cbtype)
  202. goto short_blocks;
  203. /*------------------------*/
  204. /* long_blocks: */
  205. cb0 = cb_info[1].cbmax; /* start at end of right */
  206. i = pMP3Stream->sfBandIndex[0][cb0];
  207. m = nsamp - i; /* process to len of left */
  208. /* gen sf info */
  209. for (k = r = 0; r < 3; r++)
  210. {
  211. tmp = (1 << is_sf_info->slen[r]) - 1;
  212. for (j = 0; j < is_sf_info->nr[r]; j++, k++)
  213. il[k] = tmp;
  214. }
  215. for (cb = cb0 + 1; cb < 21; cb++)
  216. {
  217. isf = il[cb] + sf->l[cb];
  218. fl = lr[isf][0];
  219. fr = lr[isf][1];
  220. n = pMP3Stream->nBand[0][cb];
  221. for (j = 0; j < n; j++, i++)
  222. {
  223. if (--m < 0)
  224. goto exit;
  225. x[1][i] = fr * x[0][i];
  226. x[0][i] = fl * x[0][i];
  227. }
  228. }
  229. return;
  230. /*------------------------*/
  231. short_blocks:
  232. for (k = r = 0; r < 3; r++)
  233. {
  234. tmp = (1 << is_sf_info->slen[r]) - 1;
  235. for (j = 0; j < is_sf_info->nr[r]; j++, k++)
  236. il[k] = tmp;
  237. }
  238. for (w = 0; w < 3; w++)
  239. {
  240. cb0 = cb_info[1].cbmax_s[w]; /* start at end of right */
  241. i = pMP3Stream->sfBandIndex[1][cb0] + w;
  242. cb1 = cb_info[0].cbmax_s[w]; /* process to end of left */
  243. for (cb = cb0 + 1; cb <= cb1; cb++)
  244. {
  245. isf = il[cb] + sf->s[w][cb];
  246. fl = lr[isf][0];
  247. fr = lr[isf][1];
  248. n = pMP3Stream->nBand[1][cb];
  249. for (j = 0; j < n; j++)
  250. {
  251. x[1][i] = fr * x[0][i];
  252. x[0][i] = fl * x[0][i];
  253. i += 3;
  254. }
  255. }
  256. }
  257. exit:
  258. return;
  259. }
  260. /*===============================================================*/