Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

534 lines
13 KiB

  1. /*
  2. * libmad - MPEG audio decoder library
  3. * Copyright (C) 2000-2004 Underbit Technologies, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  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. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * $Id: layer12.c,v 1.17 2004/02/05 09:02:39 rob Exp $
  20. */
  21. # ifdef HAVE_CONFIG_H
  22. # include "config.h"
  23. # endif
  24. # include "global.h"
  25. # ifdef HAVE_LIMITS_H
  26. # include <limits.h>
  27. # else
  28. # define CHAR_BIT 8
  29. # endif
  30. # include "fixed.h"
  31. # include "bit.h"
  32. # include "stream.h"
  33. # include "frame.h"
  34. # include "layer12.h"
  35. /*
  36. * scalefactor table
  37. * used in both Layer I and Layer II decoding
  38. */
  39. static
  40. mad_fixed_t const sf_table[64] = {
  41. # include "sf_table.dat"
  42. };
  43. /* --- Layer I ------------------------------------------------------------- */
  44. /* linear scaling table */
  45. static
  46. mad_fixed_t const linear_table[14] = {
  47. MAD_F(0x15555555), /* 2^2 / (2^2 - 1) == 1.33333333333333 */
  48. MAD_F(0x12492492), /* 2^3 / (2^3 - 1) == 1.14285714285714 */
  49. MAD_F(0x11111111), /* 2^4 / (2^4 - 1) == 1.06666666666667 */
  50. MAD_F(0x10842108), /* 2^5 / (2^5 - 1) == 1.03225806451613 */
  51. MAD_F(0x10410410), /* 2^6 / (2^6 - 1) == 1.01587301587302 */
  52. MAD_F(0x10204081), /* 2^7 / (2^7 - 1) == 1.00787401574803 */
  53. MAD_F(0x10101010), /* 2^8 / (2^8 - 1) == 1.00392156862745 */
  54. MAD_F(0x10080402), /* 2^9 / (2^9 - 1) == 1.00195694716243 */
  55. MAD_F(0x10040100), /* 2^10 / (2^10 - 1) == 1.00097751710655 */
  56. MAD_F(0x10020040), /* 2^11 / (2^11 - 1) == 1.00048851978505 */
  57. MAD_F(0x10010010), /* 2^12 / (2^12 - 1) == 1.00024420024420 */
  58. MAD_F(0x10008004), /* 2^13 / (2^13 - 1) == 1.00012208521548 */
  59. MAD_F(0x10004001), /* 2^14 / (2^14 - 1) == 1.00006103888177 */
  60. MAD_F(0x10002000) /* 2^15 / (2^15 - 1) == 1.00003051850948 */
  61. };
  62. /*
  63. * NAME: I_sample()
  64. * DESCRIPTION: decode one requantized Layer I sample from a bitstream
  65. */
  66. static
  67. mad_fixed_t I_sample(struct mad_bitptr *ptr, unsigned int nb)
  68. {
  69. mad_fixed_t sample;
  70. sample = mad_bit_read(ptr, nb);
  71. /* invert most significant bit, extend sign, then scale to fixed format */
  72. sample ^= 1 << (nb - 1);
  73. sample |= -(sample & (1 << (nb - 1)));
  74. sample <<= MAD_F_FRACBITS - (nb - 1);
  75. /* requantize the sample */
  76. /* s'' = (2^nb / (2^nb - 1)) * (s''' + 2^(-nb + 1)) */
  77. sample += MAD_F_ONE >> (nb - 1);
  78. return mad_f_mul(sample, linear_table[nb - 2]);
  79. /* s' = factor * s'' */
  80. /* (to be performed by caller) */
  81. }
  82. /*
  83. * NAME: layer->I()
  84. * DESCRIPTION: decode a single Layer I frame
  85. */
  86. int mad_layer_I(struct mad_stream *stream, struct mad_frame *frame)
  87. {
  88. struct mad_header *header = &frame->header;
  89. unsigned int nch, bound, ch, s, sb, nb;
  90. unsigned char allocation[2][32], scalefactor[2][32];
  91. nch = MAD_NCHANNELS(header);
  92. bound = 32;
  93. if (header->mode == MAD_MODE_JOINT_STEREO) {
  94. header->flags |= MAD_FLAG_I_STEREO;
  95. bound = 4 + header->mode_extension * 4;
  96. }
  97. /* check CRC word */
  98. if (header->flags & MAD_FLAG_PROTECTION) {
  99. header->crc_check =
  100. mad_bit_crc(stream->ptr, 4 * (bound * nch + (32 - bound)),
  101. header->crc_check);
  102. if (header->crc_check != header->crc_target &&
  103. !(frame->options & MAD_OPTION_IGNORECRC)) {
  104. stream->error = MAD_ERROR_BADCRC;
  105. return -1;
  106. }
  107. }
  108. /* decode bit allocations */
  109. for (sb = 0; sb < bound; ++sb) {
  110. for (ch = 0; ch < nch; ++ch) {
  111. nb = mad_bit_read(&stream->ptr, 4);
  112. if (nb == 15) {
  113. stream->error = MAD_ERROR_BADBITALLOC;
  114. return -1;
  115. }
  116. allocation[ch][sb] = nb ? nb + 1 : 0;
  117. }
  118. }
  119. for (sb = bound; sb < 32; ++sb) {
  120. nb = mad_bit_read(&stream->ptr, 4);
  121. if (nb == 15) {
  122. stream->error = MAD_ERROR_BADBITALLOC;
  123. return -1;
  124. }
  125. allocation[0][sb] =
  126. allocation[1][sb] = nb ? nb + 1 : 0;
  127. }
  128. /* decode scalefactors */
  129. for (sb = 0; sb < 32; ++sb) {
  130. for (ch = 0; ch < nch; ++ch) {
  131. if (allocation[ch][sb]) {
  132. scalefactor[ch][sb] = mad_bit_read(&stream->ptr, 6);
  133. # if defined(OPT_STRICT)
  134. /*
  135. * Scalefactor index 63 does not appear in Table B.1 of
  136. * ISO/IEC 11172-3. Nonetheless, other implementations accept it,
  137. * so we only reject it if OPT_STRICT is defined.
  138. */
  139. if (scalefactor[ch][sb] == 63) {
  140. stream->error = MAD_ERROR_BADSCALEFACTOR;
  141. return -1;
  142. }
  143. # endif
  144. }
  145. }
  146. }
  147. /* decode samples */
  148. for (s = 0; s < 12; ++s) {
  149. for (sb = 0; sb < bound; ++sb) {
  150. for (ch = 0; ch < nch; ++ch) {
  151. nb = allocation[ch][sb];
  152. frame->sbsample[ch][s][sb] = nb ?
  153. mad_f_mul(I_sample(&stream->ptr, nb),
  154. sf_table[scalefactor[ch][sb]]) : 0;
  155. }
  156. }
  157. for (sb = bound; sb < 32; ++sb) {
  158. if ((nb = allocation[0][sb])) {
  159. mad_fixed_t sample;
  160. sample = I_sample(&stream->ptr, nb);
  161. for (ch = 0; ch < nch; ++ch) {
  162. frame->sbsample[ch][s][sb] =
  163. mad_f_mul(sample, sf_table[scalefactor[ch][sb]]);
  164. }
  165. }
  166. else {
  167. for (ch = 0; ch < nch; ++ch)
  168. frame->sbsample[ch][s][sb] = 0;
  169. }
  170. }
  171. }
  172. return 0;
  173. }
  174. /* --- Layer II ------------------------------------------------------------ */
  175. /* possible quantization per subband table */
  176. static
  177. struct {
  178. unsigned int sblimit;
  179. unsigned char const offsets[30];
  180. } const sbquant_table[5] = {
  181. /* ISO/IEC 11172-3 Table B.2a */
  182. { 27, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 0 */
  183. 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0 } },
  184. /* ISO/IEC 11172-3 Table B.2b */
  185. { 30, { 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 3, 3, 3, 3, 3, /* 1 */
  186. 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0 } },
  187. /* ISO/IEC 11172-3 Table B.2c */
  188. { 8, { 5, 5, 2, 2, 2, 2, 2, 2 } }, /* 2 */
  189. /* ISO/IEC 11172-3 Table B.2d */
  190. { 12, { 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } }, /* 3 */
  191. /* ISO/IEC 13818-3 Table B.1 */
  192. { 30, { 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, /* 4 */
  193. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }
  194. };
  195. /* bit allocation table */
  196. static
  197. struct {
  198. unsigned short nbal;
  199. unsigned short offset;
  200. } const bitalloc_table[8] = {
  201. { 2, 0 }, /* 0 */
  202. { 2, 3 }, /* 1 */
  203. { 3, 3 }, /* 2 */
  204. { 3, 1 }, /* 3 */
  205. { 4, 2 }, /* 4 */
  206. { 4, 3 }, /* 5 */
  207. { 4, 4 }, /* 6 */
  208. { 4, 5 } /* 7 */
  209. };
  210. /* offsets into quantization class table */
  211. static
  212. unsigned char const offset_table[6][15] = {
  213. { 0, 1, 16 }, /* 0 */
  214. { 0, 1, 2, 3, 4, 5, 16 }, /* 1 */
  215. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }, /* 2 */
  216. { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, /* 3 */
  217. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16 }, /* 4 */
  218. { 0, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 } /* 5 */
  219. };
  220. /* quantization class table */
  221. static
  222. struct quantclass {
  223. unsigned short nlevels;
  224. unsigned char group;
  225. unsigned char bits;
  226. mad_fixed_t C;
  227. mad_fixed_t D;
  228. } const qc_table[17] = {
  229. # include "qc_table.dat"
  230. };
  231. /*
  232. * NAME: II_samples()
  233. * DESCRIPTION: decode three requantized Layer II samples from a bitstream
  234. */
  235. static
  236. void II_samples(struct mad_bitptr *ptr,
  237. struct quantclass const *quantclass,
  238. mad_fixed_t output[3])
  239. {
  240. unsigned int nb, s, sample[3];
  241. if ((nb = quantclass->group)) {
  242. unsigned int c, nlevels;
  243. /* degrouping */
  244. c = mad_bit_read(ptr, quantclass->bits);
  245. nlevels = quantclass->nlevels;
  246. for (s = 0; s < 3; ++s) {
  247. sample[s] = c % nlevels;
  248. c /= nlevels;
  249. }
  250. }
  251. else {
  252. nb = quantclass->bits;
  253. for (s = 0; s < 3; ++s)
  254. sample[s] = mad_bit_read(ptr, nb);
  255. }
  256. for (s = 0; s < 3; ++s) {
  257. mad_fixed_t requantized;
  258. /* invert most significant bit, extend sign, then scale to fixed format */
  259. requantized = sample[s] ^ (1 << (nb - 1));
  260. requantized |= -(requantized & (1 << (nb - 1)));
  261. requantized <<= MAD_F_FRACBITS - (nb - 1);
  262. /* requantize the sample */
  263. /* s'' = C * (s''' + D) */
  264. output[s] = mad_f_mul(requantized + quantclass->D, quantclass->C);
  265. /* s' = factor * s'' */
  266. /* (to be performed by caller) */
  267. }
  268. }
  269. /*
  270. * NAME: layer->II()
  271. * DESCRIPTION: decode a single Layer II frame
  272. */
  273. int mad_layer_II(struct mad_stream *stream, struct mad_frame *frame)
  274. {
  275. struct mad_header *header = &frame->header;
  276. struct mad_bitptr start;
  277. unsigned int index, sblimit, nbal, nch, bound, gr, ch, s, sb;
  278. unsigned char const *offsets;
  279. unsigned char allocation[2][32], scfsi[2][32], scalefactor[2][32][3];
  280. mad_fixed_t samples[3];
  281. nch = MAD_NCHANNELS(header);
  282. if (header->flags & MAD_FLAG_LSF_EXT)
  283. index = 4;
  284. else if (header->flags & MAD_FLAG_FREEFORMAT)
  285. goto freeformat;
  286. else {
  287. unsigned long bitrate_per_channel;
  288. bitrate_per_channel = header->bitrate;
  289. if (nch == 2) {
  290. bitrate_per_channel /= 2;
  291. # if defined(OPT_STRICT)
  292. /*
  293. * ISO/IEC 11172-3 allows only single channel mode for 32, 48, 56, and
  294. * 80 kbps bitrates in Layer II, but some encoders ignore this
  295. * restriction. We enforce it if OPT_STRICT is defined.
  296. */
  297. if (bitrate_per_channel <= 28000 || bitrate_per_channel == 40000) {
  298. stream->error = MAD_ERROR_BADMODE;
  299. return -1;
  300. }
  301. # endif
  302. }
  303. else { /* nch == 1 */
  304. if (bitrate_per_channel > 192000) {
  305. /*
  306. * ISO/IEC 11172-3 does not allow single channel mode for 224, 256,
  307. * 320, or 384 kbps bitrates in Layer II.
  308. */
  309. stream->error = MAD_ERROR_BADMODE;
  310. return -1;
  311. }
  312. }
  313. if (bitrate_per_channel <= 48000)
  314. index = (header->samplerate == 32000) ? 3 : 2;
  315. else if (bitrate_per_channel <= 80000)
  316. index = 0;
  317. else {
  318. freeformat:
  319. index = (header->samplerate == 48000) ? 0 : 1;
  320. }
  321. }
  322. sblimit = sbquant_table[index].sblimit;
  323. offsets = sbquant_table[index].offsets;
  324. bound = 32;
  325. if (header->mode == MAD_MODE_JOINT_STEREO) {
  326. header->flags |= MAD_FLAG_I_STEREO;
  327. bound = 4 + header->mode_extension * 4;
  328. }
  329. if (bound > sblimit)
  330. bound = sblimit;
  331. start = stream->ptr;
  332. /* decode bit allocations */
  333. for (sb = 0; sb < bound; ++sb) {
  334. nbal = bitalloc_table[offsets[sb]].nbal;
  335. for (ch = 0; ch < nch; ++ch)
  336. allocation[ch][sb] = mad_bit_read(&stream->ptr, nbal);
  337. }
  338. for (sb = bound; sb < sblimit; ++sb) {
  339. nbal = bitalloc_table[offsets[sb]].nbal;
  340. allocation[0][sb] =
  341. allocation[1][sb] = mad_bit_read(&stream->ptr, nbal);
  342. }
  343. /* decode scalefactor selection info */
  344. for (sb = 0; sb < sblimit; ++sb) {
  345. for (ch = 0; ch < nch; ++ch) {
  346. if (allocation[ch][sb])
  347. scfsi[ch][sb] = mad_bit_read(&stream->ptr, 2);
  348. }
  349. }
  350. /* check CRC word */
  351. if (header->flags & MAD_FLAG_PROTECTION) {
  352. header->crc_check =
  353. mad_bit_crc(start, mad_bit_length(&start, &stream->ptr),
  354. header->crc_check);
  355. if (header->crc_check != header->crc_target &&
  356. !(frame->options & MAD_OPTION_IGNORECRC)) {
  357. stream->error = MAD_ERROR_BADCRC;
  358. return -1;
  359. }
  360. }
  361. /* decode scalefactors */
  362. for (sb = 0; sb < sblimit; ++sb) {
  363. for (ch = 0; ch < nch; ++ch) {
  364. if (allocation[ch][sb]) {
  365. scalefactor[ch][sb][0] = mad_bit_read(&stream->ptr, 6);
  366. switch (scfsi[ch][sb]) {
  367. case 2:
  368. scalefactor[ch][sb][2] =
  369. scalefactor[ch][sb][1] =
  370. scalefactor[ch][sb][0];
  371. break;
  372. case 0:
  373. scalefactor[ch][sb][1] = mad_bit_read(&stream->ptr, 6);
  374. /* fall through */
  375. case 1:
  376. case 3:
  377. scalefactor[ch][sb][2] = mad_bit_read(&stream->ptr, 6);
  378. }
  379. if (scfsi[ch][sb] & 1)
  380. scalefactor[ch][sb][1] = scalefactor[ch][sb][scfsi[ch][sb] - 1];
  381. # if defined(OPT_STRICT)
  382. /*
  383. * Scalefactor index 63 does not appear in Table B.1 of
  384. * ISO/IEC 11172-3. Nonetheless, other implementations accept it,
  385. * so we only reject it if OPT_STRICT is defined.
  386. */
  387. if (scalefactor[ch][sb][0] == 63 ||
  388. scalefactor[ch][sb][1] == 63 ||
  389. scalefactor[ch][sb][2] == 63) {
  390. stream->error = MAD_ERROR_BADSCALEFACTOR;
  391. return -1;
  392. }
  393. # endif
  394. }
  395. }
  396. }
  397. /* decode samples */
  398. for (gr = 0; gr < 12; ++gr) {
  399. for (sb = 0; sb < bound; ++sb) {
  400. for (ch = 0; ch < nch; ++ch) {
  401. if ((index = allocation[ch][sb])) {
  402. index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
  403. II_samples(&stream->ptr, &qc_table[index], samples);
  404. for (s = 0; s < 3; ++s) {
  405. frame->sbsample[ch][3 * gr + s][sb] =
  406. mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]);
  407. }
  408. }
  409. else {
  410. for (s = 0; s < 3; ++s)
  411. frame->sbsample[ch][3 * gr + s][sb] = 0;
  412. }
  413. }
  414. }
  415. for (sb = bound; sb < sblimit; ++sb) {
  416. if ((index = allocation[0][sb])) {
  417. index = offset_table[bitalloc_table[offsets[sb]].offset][index - 1];
  418. II_samples(&stream->ptr, &qc_table[index], samples);
  419. for (ch = 0; ch < nch; ++ch) {
  420. for (s = 0; s < 3; ++s) {
  421. frame->sbsample[ch][3 * gr + s][sb] =
  422. mad_f_mul(samples[s], sf_table[scalefactor[ch][sb][gr / 4]]);
  423. }
  424. }
  425. }
  426. else {
  427. for (ch = 0; ch < nch; ++ch) {
  428. for (s = 0; s < 3; ++s)
  429. frame->sbsample[ch][3 * gr + s][sb] = 0;
  430. }
  431. }
  432. }
  433. for (ch = 0; ch < nch; ++ch) {
  434. for (s = 0; s < 3; ++s) {
  435. for (sb = sblimit; sb < 32; ++sb)
  436. frame->sbsample[ch][3 * gr + s][sb] = 0;
  437. }
  438. }
  439. }
  440. return 0;
  441. }