layer12.c 13 KB

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