upsf.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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: upsf.c,v 1.3 1999/10/19 07:13:09 elrod Exp $
  18. ____________________________________________________________________________*/
  19. /**** upsf.c ***************************************************
  20. Layer III
  21. unpack scale factors
  22. ******************************************************************/
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <float.h>
  26. #include <math.h>
  27. #include "L3.h"
  28. //extern int iframe;
  29. unsigned int bitget(int n);
  30. /*------------------------------------------------------------*/
  31. static const int slen_table[16][2] =
  32. {
  33. {0, 0}, {0, 1},
  34. {0, 2}, {0, 3},
  35. {3, 0}, {1, 1},
  36. {1, 2}, {1, 3},
  37. {2, 1}, {2, 2},
  38. {2, 3}, {3, 1},
  39. {3, 2}, {3, 3},
  40. {4, 2}, {4, 3},
  41. };
  42. /* nr_table[size+3*is_right][block type 0,1,3 2, 2+mixed][4] */
  43. /* for bt=2 nr is count for group of 3 */
  44. static const int nr_table[6][3][4] =
  45. {
  46. {{6, 5, 5, 5},
  47. {3, 3, 3, 3},
  48. {6, 3, 3, 3}},
  49. {{6, 5, 7, 3},
  50. {3, 3, 4, 2},
  51. {6, 3, 4, 2}},
  52. {{11, 10, 0, 0},
  53. {6, 6, 0, 0},
  54. {6, 3, 6, 0}}, /* adjusted *//* 15, 18, 0, 0, */
  55. /*-intensity stereo right chan--*/
  56. {{7, 7, 7, 0},
  57. {4, 4, 4, 0},
  58. {6, 5, 4, 0}},
  59. {{6, 6, 6, 3},
  60. {4, 3, 3, 2},
  61. {6, 4, 3, 2}},
  62. {{8, 8, 5, 0},
  63. {5, 4, 3, 0},
  64. {6, 6, 3, 0}},
  65. };
  66. /*=============================================================*/
  67. void unpack_sf_sub_MPEG1(SCALEFACT sf[],
  68. GR * grdat,
  69. int scfsi, /* bit flag */
  70. int gr)
  71. {
  72. int sfb;
  73. int slen0, slen1;
  74. int block_type, mixed_block_flag, scalefac_compress;
  75. block_type = grdat->block_type;
  76. mixed_block_flag = grdat->mixed_block_flag;
  77. scalefac_compress = grdat->scalefac_compress;
  78. slen0 = slen_table[scalefac_compress][0];
  79. slen1 = slen_table[scalefac_compress][1];
  80. if (block_type == 2)
  81. {
  82. if (mixed_block_flag)
  83. { /* mixed */
  84. for (sfb = 0; sfb < 8; sfb++)
  85. sf[0].l[sfb] = bitget(slen0);
  86. for (sfb = 3; sfb < 6; sfb++)
  87. {
  88. sf[0].s[0][sfb] = bitget(slen0);
  89. sf[0].s[1][sfb] = bitget(slen0);
  90. sf[0].s[2][sfb] = bitget(slen0);
  91. }
  92. for (sfb = 6; sfb < 12; sfb++)
  93. {
  94. sf[0].s[0][sfb] = bitget(slen1);
  95. sf[0].s[1][sfb] = bitget(slen1);
  96. sf[0].s[2][sfb] = bitget(slen1);
  97. }
  98. return;
  99. }
  100. for (sfb = 0; sfb < 6; sfb++)
  101. {
  102. sf[0].s[0][sfb] = bitget(slen0);
  103. sf[0].s[1][sfb] = bitget(slen0);
  104. sf[0].s[2][sfb] = bitget(slen0);
  105. }
  106. for (; sfb < 12; sfb++)
  107. {
  108. sf[0].s[0][sfb] = bitget(slen1);
  109. sf[0].s[1][sfb] = bitget(slen1);
  110. sf[0].s[2][sfb] = bitget(slen1);
  111. }
  112. return;
  113. }
  114. /* long blocks types 0 1 3, first granule */
  115. if (gr == 0)
  116. {
  117. for (sfb = 0; sfb < 11; sfb++)
  118. sf[0].l[sfb] = bitget(slen0);
  119. for (; sfb < 21; sfb++)
  120. sf[0].l[sfb] = bitget(slen1);
  121. return;
  122. }
  123. /* long blocks 0, 1, 3, second granule */
  124. sfb = 0;
  125. if (scfsi & 8)
  126. for (; sfb < 6; sfb++)
  127. sf[0].l[sfb] = sf[-2].l[sfb];
  128. else
  129. for (; sfb < 6; sfb++)
  130. sf[0].l[sfb] = bitget(slen0);
  131. if (scfsi & 4)
  132. for (; sfb < 11; sfb++)
  133. sf[0].l[sfb] = sf[-2].l[sfb];
  134. else
  135. for (; sfb < 11; sfb++)
  136. sf[0].l[sfb] = bitget(slen0);
  137. if (scfsi & 2)
  138. for (; sfb < 16; sfb++)
  139. sf[0].l[sfb] = sf[-2].l[sfb];
  140. else
  141. for (; sfb < 16; sfb++)
  142. sf[0].l[sfb] = bitget(slen1);
  143. if (scfsi & 1)
  144. for (; sfb < 21; sfb++)
  145. sf[0].l[sfb] = sf[-2].l[sfb];
  146. else
  147. for (; sfb < 21; sfb++)
  148. sf[0].l[sfb] = bitget(slen1);
  149. return;
  150. }
  151. /*=============================================================*/
  152. void unpack_sf_sub_MPEG2(SCALEFACT sf[],
  153. GR * grdat,
  154. int is_and_ch, IS_SF_INFO * sf_info)
  155. {
  156. int sfb;
  157. int slen1, slen2, slen3, slen4;
  158. int nr1, nr2, nr3, nr4;
  159. int i, k;
  160. int preflag, intensity_scale;
  161. int block_type, mixed_block_flag, scalefac_compress;
  162. block_type = grdat->block_type;
  163. mixed_block_flag = grdat->mixed_block_flag;
  164. scalefac_compress = grdat->scalefac_compress;
  165. preflag = 0;
  166. intensity_scale = 0; /* to avoid compiler warning */
  167. if (is_and_ch == 0)
  168. {
  169. if (scalefac_compress < 400)
  170. {
  171. slen2 = scalefac_compress >> 4;
  172. slen1 = slen2 / 5;
  173. slen2 = slen2 % 5;
  174. slen4 = scalefac_compress & 15;
  175. slen3 = slen4 >> 2;
  176. slen4 = slen4 & 3;
  177. k = 0;
  178. }
  179. else if (scalefac_compress < 500)
  180. {
  181. scalefac_compress -= 400;
  182. slen2 = scalefac_compress >> 2;
  183. slen1 = slen2 / 5;
  184. slen2 = slen2 % 5;
  185. slen3 = scalefac_compress & 3;
  186. slen4 = 0;
  187. k = 1;
  188. }
  189. else
  190. {
  191. scalefac_compress -= 500;
  192. slen1 = scalefac_compress / 3;
  193. slen2 = scalefac_compress % 3;
  194. slen3 = slen4 = 0;
  195. if (mixed_block_flag)
  196. {
  197. slen3 = slen2; /* adjust for long/short mix logic */
  198. slen2 = slen1;
  199. }
  200. preflag = 1;
  201. k = 2;
  202. }
  203. }
  204. else
  205. { /* intensity stereo ch = 1 (right) */
  206. intensity_scale = scalefac_compress & 1;
  207. scalefac_compress >>= 1;
  208. if (scalefac_compress < 180)
  209. {
  210. slen1 = scalefac_compress / 36;
  211. slen2 = scalefac_compress % 36;
  212. slen3 = slen2 % 6;
  213. slen2 = slen2 / 6;
  214. slen4 = 0;
  215. k = 3 + 0;
  216. }
  217. else if (scalefac_compress < 244)
  218. {
  219. scalefac_compress -= 180;
  220. slen3 = scalefac_compress & 3;
  221. scalefac_compress >>= 2;
  222. slen2 = scalefac_compress & 3;
  223. slen1 = scalefac_compress >> 2;
  224. slen4 = 0;
  225. k = 3 + 1;
  226. }
  227. else
  228. {
  229. scalefac_compress -= 244;
  230. slen1 = scalefac_compress / 3;
  231. slen2 = scalefac_compress % 3;
  232. slen3 = slen4 = 0;
  233. k = 3 + 2;
  234. }
  235. }
  236. i = 0;
  237. if (block_type == 2)
  238. i = (mixed_block_flag & 1) + 1;
  239. nr1 = nr_table[k][i][0];
  240. nr2 = nr_table[k][i][1];
  241. nr3 = nr_table[k][i][2];
  242. nr4 = nr_table[k][i][3];
  243. /* return is scale factor info (for right chan is mode) */
  244. if (is_and_ch)
  245. {
  246. sf_info->nr[0] = nr1;
  247. sf_info->nr[1] = nr2;
  248. sf_info->nr[2] = nr3;
  249. sf_info->slen[0] = slen1;
  250. sf_info->slen[1] = slen2;
  251. sf_info->slen[2] = slen3;
  252. sf_info->intensity_scale = intensity_scale;
  253. }
  254. grdat->preflag = preflag; /* return preflag */
  255. /*--------------------------------------*/
  256. if (block_type == 2)
  257. {
  258. if (mixed_block_flag)
  259. { /* mixed */
  260. if (slen1 != 0) /* long block portion */
  261. for (sfb = 0; sfb < 6; sfb++)
  262. sf[0].l[sfb] = bitget(slen1);
  263. else
  264. for (sfb = 0; sfb < 6; sfb++)
  265. sf[0].l[sfb] = 0;
  266. sfb = 3; /* start sfb for short */
  267. }
  268. else
  269. { /* all short, initial short blocks */
  270. sfb = 0;
  271. if (slen1 != 0)
  272. for (i = 0; i < nr1; i++, sfb++)
  273. {
  274. sf[0].s[0][sfb] = bitget(slen1);
  275. sf[0].s[1][sfb] = bitget(slen1);
  276. sf[0].s[2][sfb] = bitget(slen1);
  277. }
  278. else
  279. for (i = 0; i < nr1; i++, sfb++)
  280. {
  281. sf[0].s[0][sfb] = 0;
  282. sf[0].s[1][sfb] = 0;
  283. sf[0].s[2][sfb] = 0;
  284. }
  285. }
  286. /* remaining short blocks */
  287. if (slen2 != 0)
  288. for (i = 0; i < nr2; i++, sfb++)
  289. {
  290. sf[0].s[0][sfb] = bitget(slen2);
  291. sf[0].s[1][sfb] = bitget(slen2);
  292. sf[0].s[2][sfb] = bitget(slen2);
  293. }
  294. else
  295. for (i = 0; i < nr2; i++, sfb++)
  296. {
  297. sf[0].s[0][sfb] = 0;
  298. sf[0].s[1][sfb] = 0;
  299. sf[0].s[2][sfb] = 0;
  300. }
  301. if (slen3 != 0)
  302. for (i = 0; i < nr3; i++, sfb++)
  303. {
  304. sf[0].s[0][sfb] = bitget(slen3);
  305. sf[0].s[1][sfb] = bitget(slen3);
  306. sf[0].s[2][sfb] = bitget(slen3);
  307. }
  308. else
  309. for (i = 0; i < nr3; i++, sfb++)
  310. {
  311. sf[0].s[0][sfb] = 0;
  312. sf[0].s[1][sfb] = 0;
  313. sf[0].s[2][sfb] = 0;
  314. }
  315. if (slen4 != 0)
  316. for (i = 0; i < nr4; i++, sfb++)
  317. {
  318. sf[0].s[0][sfb] = bitget(slen4);
  319. sf[0].s[1][sfb] = bitget(slen4);
  320. sf[0].s[2][sfb] = bitget(slen4);
  321. }
  322. else
  323. for (i = 0; i < nr4; i++, sfb++)
  324. {
  325. sf[0].s[0][sfb] = 0;
  326. sf[0].s[1][sfb] = 0;
  327. sf[0].s[2][sfb] = 0;
  328. }
  329. return;
  330. }
  331. /* long blocks types 0 1 3 */
  332. sfb = 0;
  333. if (slen1 != 0)
  334. for (i = 0; i < nr1; i++, sfb++)
  335. sf[0].l[sfb] = bitget(slen1);
  336. else
  337. for (i = 0; i < nr1; i++, sfb++)
  338. sf[0].l[sfb] = 0;
  339. if (slen2 != 0)
  340. for (i = 0; i < nr2; i++, sfb++)
  341. sf[0].l[sfb] = bitget(slen2);
  342. else
  343. for (i = 0; i < nr2; i++, sfb++)
  344. sf[0].l[sfb] = 0;
  345. if (slen3 != 0)
  346. for (i = 0; i < nr3; i++, sfb++)
  347. sf[0].l[sfb] = bitget(slen3);
  348. else
  349. for (i = 0; i < nr3; i++, sfb++)
  350. sf[0].l[sfb] = 0;
  351. if (slen4 != 0)
  352. for (i = 0; i < nr4; i++, sfb++)
  353. sf[0].l[sfb] = bitget(slen4);
  354. else
  355. for (i = 0; i < nr4; i++, sfb++)
  356. sf[0].l[sfb] = 0;
  357. }
  358. /*-------------------------------------------------*/