layer2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * layer2.c: Mpeg Layer-2 audio decoder
  3. *
  4. * Copyright (C) 1999-2010 The L.A.M.E. project
  5. *
  6. * Initially written by Michael Hipp, see also AUTHORS and README.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public
  19. * License along with this library; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. */
  23. /* $Id: layer2.c,v 1.34 2017/08/22 23:31:07 robert Exp $ */
  24. #ifdef HAVE_CONFIG_H
  25. # include <config.h>
  26. #endif
  27. #include "common.h"
  28. #include "layer2.h"
  29. #include "l2tables.h"
  30. #include "decode_i386.h"
  31. #ifdef WITH_DMALLOC
  32. #include <dmalloc.h>
  33. #endif
  34. #include <assert.h>
  35. static int gd_are_hip_tables_layer2_initialized = 0;
  36. static unsigned char grp_3tab[32 * 3] = { 0, }; /* used: 27 */
  37. static unsigned char grp_5tab[128 * 3] = { 0, }; /* used: 125 */
  38. static unsigned char grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
  39. void
  40. hip_init_tables_layer2(void)
  41. {
  42. static const double mulmul[27] = {
  43. 0.0, -2.0 / 3.0, 2.0 / 3.0,
  44. 2.0 / 7.0, 2.0 / 15.0, 2.0 / 31.0, 2.0 / 63.0, 2.0 / 127.0, 2.0 / 255.0,
  45. 2.0 / 511.0, 2.0 / 1023.0, 2.0 / 2047.0, 2.0 / 4095.0, 2.0 / 8191.0,
  46. 2.0 / 16383.0, 2.0 / 32767.0, 2.0 / 65535.0,
  47. -4.0 / 5.0, -2.0 / 5.0, 2.0 / 5.0, 4.0 / 5.0,
  48. -8.0 / 9.0, -4.0 / 9.0, -2.0 / 9.0, 2.0 / 9.0, 4.0 / 9.0, 8.0 / 9.0
  49. };
  50. static const unsigned char base[3][9] = {
  51. {1, 0, 2,},
  52. {17, 18, 0, 19, 20,},
  53. {21, 1, 22, 23, 0, 24, 25, 2, 26}
  54. };
  55. int i, j, k, l, len;
  56. real *table;
  57. static const int tablen[3] = { 3, 5, 9 };
  58. static unsigned char *itable, *tables[3] = { grp_3tab, grp_5tab, grp_9tab };
  59. if (gd_are_hip_tables_layer2_initialized) {
  60. return;
  61. }
  62. gd_are_hip_tables_layer2_initialized = 1;
  63. for (i = 0; i < 3; i++) {
  64. itable = tables[i];
  65. len = tablen[i];
  66. for (j = 0; j < len; j++)
  67. for (k = 0; k < len; k++)
  68. for (l = 0; l < len; l++) {
  69. *itable++ = base[i][l];
  70. *itable++ = base[i][k];
  71. *itable++ = base[i][j];
  72. }
  73. }
  74. for (k = 0; k < 27; k++) {
  75. double m = mulmul[k];
  76. table = muls[k];
  77. for (j = 3, i = 0; i < 63; i++, j--)
  78. *table++ = (real) (m * pow(2.0, (double) j / 3.0));
  79. *table++ = 0.0;
  80. }
  81. }
  82. static unsigned char*
  83. grp_table_select(short d1, unsigned int idx)
  84. {
  85. /* RH: it seems to be common, that idx is larger than the table's sizes.
  86. is it OK to return a zero vector in this case? FIXME
  87. /*/
  88. static unsigned char dummy_table[] = { 0,0,0 };
  89. unsigned int x;
  90. switch (d1) {
  91. case 3:
  92. x = 3*3*3;
  93. idx = idx < x ? idx : x;
  94. return &grp_3tab[3 * idx];
  95. case 5:
  96. x = 5*5*5;
  97. idx = idx < x ? idx : x;
  98. return &grp_5tab[3 * idx];
  99. case 9:
  100. x = 9*9*9;
  101. idx = idx < x ? idx : x;
  102. return &grp_9tab[3 * idx];
  103. default:
  104. /* fatal error */
  105. assert(0);
  106. }
  107. return &dummy_table[0];
  108. }
  109. typedef struct sideinfo_layer_II_struct
  110. {
  111. unsigned char allocation[SBLIMIT][2];
  112. unsigned char scalefactor[SBLIMIT][2][3]; /* subband / channel / block */
  113. } sideinfo_layer_II;
  114. static void
  115. II_step_one(PMPSTR mp, sideinfo_layer_II *si, struct frame *fr)
  116. {
  117. int nch = fr->stereo;
  118. int sblimit = fr->II_sblimit;
  119. int jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : fr->II_sblimit;
  120. struct al_table2 const *alloc1 = fr->alloc;
  121. unsigned char scfsi[SBLIMIT][2];
  122. int i, ch;
  123. memset(si, 0, sizeof(*si));
  124. if (jsbound > sblimit)
  125. jsbound = sblimit;
  126. if (nch == 2) {
  127. for (i = 0; i < jsbound; ++i) {
  128. short step = alloc1->bits;
  129. unsigned char b0 = get_leq_8_bits(mp, step);
  130. unsigned char b1 = get_leq_8_bits(mp, step);
  131. alloc1 += ((size_t)1 << step);
  132. si->allocation[i][0] = b0;
  133. si->allocation[i][1] = b1;
  134. }
  135. for (i = jsbound; i < sblimit; ++i) {
  136. short step = alloc1->bits;
  137. unsigned char b0 = get_leq_8_bits(mp, step);
  138. alloc1 += ((size_t)1 << step);
  139. si->allocation[i][0] = b0;
  140. si->allocation[i][1] = b0;
  141. }
  142. for (i = 0; i < sblimit; ++i) {
  143. unsigned char n0 = si->allocation[i][0];
  144. unsigned char n1 = si->allocation[i][1];
  145. unsigned char b0 = n0 ? get_leq_8_bits(mp, 2) : 0;
  146. unsigned char b1 = n1 ? get_leq_8_bits(mp, 2) : 0;
  147. scfsi[i][0] = b0;
  148. scfsi[i][1] = b1;
  149. }
  150. }
  151. else { /* mono */
  152. for (i = 0; i < sblimit; ++i) {
  153. short step = alloc1->bits;
  154. unsigned char b0 = get_leq_8_bits(mp, step);
  155. alloc1 += ((size_t)1 << step);
  156. si->allocation[i][0] = b0;
  157. }
  158. for (i = 0; i < sblimit; ++i) {
  159. unsigned char n0 = si->allocation[i][0];
  160. unsigned char b0 = n0 ? get_leq_8_bits(mp, 2) : 0;
  161. scfsi[i][0] = b0;
  162. }
  163. }
  164. for (i = 0; i < sblimit; ++i) {
  165. for (ch = 0; ch < nch; ++ch) {
  166. unsigned char s0 = 0, s1 = 0, s2 = 0;
  167. if (si->allocation[i][ch]) {
  168. switch (scfsi[i][ch]) {
  169. case 0:
  170. s0 = get_leq_8_bits(mp, 6);
  171. s1 = get_leq_8_bits(mp, 6);
  172. s2 = get_leq_8_bits(mp, 6);
  173. break;
  174. case 1:
  175. s0 = get_leq_8_bits(mp, 6);
  176. s1 = s0;
  177. s2 = get_leq_8_bits(mp, 6);
  178. break;
  179. case 2:
  180. s0 = get_leq_8_bits(mp, 6);
  181. s1 = s0;
  182. s2 = s0;
  183. break;
  184. case 3:
  185. s0 = get_leq_8_bits(mp, 6);
  186. s1 = get_leq_8_bits(mp, 6);
  187. s2 = s1;
  188. break;
  189. default:
  190. assert(0);
  191. }
  192. }
  193. si->scalefactor[i][ch][0] = s0;
  194. si->scalefactor[i][ch][1] = s1;
  195. si->scalefactor[i][ch][2] = s2;
  196. }
  197. }
  198. }
  199. static void
  200. II_step_two(PMPSTR mp, sideinfo_layer_II* si, struct frame *fr, int gr, real fraction[2][4][SBLIMIT])
  201. {
  202. struct al_table2 const *alloc1 = fr->alloc;
  203. int sblimit = fr->II_sblimit;
  204. int jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : fr->II_sblimit;
  205. int i, ch, nch = fr->stereo;
  206. double cm, r0, r1, r2;
  207. if (jsbound > sblimit)
  208. jsbound = sblimit;
  209. for (i = 0; i < jsbound; ++i) {
  210. short step = alloc1->bits;
  211. for (ch = 0; ch < nch; ++ch) {
  212. unsigned char ba = si->allocation[i][ch];
  213. if (ba) {
  214. unsigned char x1 = si->scalefactor[i][ch][gr];
  215. struct al_table2 const *alloc2 = alloc1 + ba;
  216. short k = alloc2->bits;
  217. short d1 = alloc2->d;
  218. assert( k <= 16 );
  219. k = (k <= 16) ? k : 16;
  220. assert( x1 < 64 );
  221. x1 = (x1 < 64) ? x1 : 63;
  222. if (d1 < 0) {
  223. int v0 = getbits(mp, k);
  224. int v1 = getbits(mp, k);
  225. int v2 = getbits(mp, k);
  226. cm = muls[k][x1];
  227. r0 = (v0 + d1) * cm;
  228. r1 = (v1 + d1) * cm;
  229. r2 = (v2 + d1) * cm;
  230. }
  231. else {
  232. unsigned int idx = getbits(mp, k);
  233. unsigned char *tab = grp_table_select(d1, idx);
  234. unsigned char k0 = tab[0];
  235. unsigned char k1 = tab[1];
  236. unsigned char k2 = tab[2];
  237. r0 = muls[k0][x1];
  238. r1 = muls[k1][x1];
  239. r2 = muls[k2][x1];
  240. }
  241. fraction[ch][0][i] = (real) r0;
  242. fraction[ch][1][i] = (real) r1;
  243. fraction[ch][2][i] = (real) r2;
  244. }
  245. else {
  246. fraction[ch][0][i] = fraction[ch][1][i] = fraction[ch][2][i] = 0.0;
  247. }
  248. }
  249. alloc1 += ((size_t)1 << step);
  250. }
  251. for (i = jsbound; i < sblimit; i++) {
  252. short step = alloc1->bits;
  253. unsigned char ba = si->allocation[i][0];
  254. if (ba) {
  255. struct al_table2 const *alloc2 = alloc1 + ba;
  256. short k = alloc2->bits;
  257. short d1 = alloc2->d;
  258. assert( k <= 16 );
  259. k = (k <= 16) ? k : 16;
  260. if (d1 < 0) {
  261. int v0 = getbits(mp, k);
  262. int v1 = getbits(mp, k);
  263. int v2 = getbits(mp, k);
  264. for (ch = 0; ch < nch; ++ch) {
  265. unsigned char x1 = si->scalefactor[i][ch][gr];
  266. assert( x1 < 64 );
  267. x1 = (x1 < 64) ? x1 : 63;
  268. cm = muls[k][x1];
  269. r0 = (v0 + d1) * cm;
  270. r1 = (v1 + d1) * cm;
  271. r2 = (v2 + d1) * cm;
  272. fraction[ch][0][i] = (real) r0;
  273. fraction[ch][1][i] = (real) r1;
  274. fraction[ch][2][i] = (real) r2;
  275. }
  276. }
  277. else {
  278. unsigned int idx = getbits(mp, k);
  279. unsigned char *tab = grp_table_select(d1, idx);
  280. unsigned char k0 = tab[0];
  281. unsigned char k1 = tab[1];
  282. unsigned char k2 = tab[2];
  283. for (ch = 0; ch < nch; ++ch) {
  284. unsigned char x1 = si->scalefactor[i][ch][gr];
  285. assert( x1 < 64 );
  286. x1 = (x1 < 64) ? x1 : 63;
  287. r0 = muls[k0][x1];
  288. r1 = muls[k1][x1];
  289. r2 = muls[k2][x1];
  290. fraction[ch][0][i] = (real) r0;
  291. fraction[ch][1][i] = (real) r1;
  292. fraction[ch][2][i] = (real) r2;
  293. }
  294. }
  295. }
  296. else {
  297. fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] = 0.0;
  298. fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0;
  299. }
  300. alloc1 += ((size_t)1 << step);
  301. }
  302. if (sblimit > fr->down_sample_sblimit) {
  303. sblimit = fr->down_sample_sblimit;
  304. }
  305. for (ch = 0; ch < nch; ++ch) {
  306. for (i = sblimit; i < SBLIMIT; ++i) {
  307. fraction[ch][0][i] = fraction[ch][1][i] = fraction[ch][2][i] = 0.0;
  308. }
  309. }
  310. }
  311. static void
  312. II_select_table(struct frame *fr)
  313. {
  314. /* *INDENT-OFF* */
  315. static const int translate[3][2][16] =
  316. { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
  317. { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
  318. { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
  319. { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
  320. { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
  321. { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
  322. /* *INDENT-ON* */
  323. int table, sblim;
  324. static const struct al_table2 *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3, alloc_4 };
  325. static const int sblims[5] = { 27, 30, 8, 12, 30 };
  326. if (fr->lsf)
  327. table = 4;
  328. else
  329. table = translate[fr->sampling_frequency][2 - fr->stereo][fr->bitrate_index];
  330. sblim = sblims[table];
  331. fr->alloc = (struct al_table2 const *) tables[table];
  332. fr->II_sblimit = sblim;
  333. }
  334. int
  335. decode_layer2_sideinfo(PMPSTR mp)
  336. {
  337. (void) mp;
  338. /* FIXME: extract side information and check values */
  339. return 0;
  340. }
  341. int
  342. decode_layer2_frame(PMPSTR mp, unsigned char *pcm_sample, int *pcm_point)
  343. {
  344. real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */
  345. sideinfo_layer_II si;
  346. struct frame *fr = &(mp->fr);
  347. int single = fr->single;
  348. int i, j, clip = 0;
  349. II_select_table(fr);
  350. II_step_one(mp, &si, fr);
  351. if (fr->stereo == 1 || single == 3)
  352. single = 0;
  353. if (single >= 0) {
  354. for (i = 0; i < SCALE_BLOCK; i++) {
  355. II_step_two(mp, &si, fr, i >> 2, fraction);
  356. for (j = 0; j < 3; j++) {
  357. clip += synth_1to1_mono(mp, fraction[single][j], pcm_sample, pcm_point);
  358. }
  359. }
  360. }
  361. else {
  362. for (i = 0; i < SCALE_BLOCK; i++) {
  363. II_step_two(mp, &si, fr, i >> 2, fraction);
  364. for (j = 0; j < 3; j++) {
  365. int p1 = *pcm_point;
  366. clip += synth_1to1(mp, fraction[0][j], 0, pcm_sample, &p1);
  367. clip += synth_1to1(mp, fraction[1][j], 1, pcm_sample, pcm_point);
  368. }
  369. }
  370. }
  371. return clip;
  372. }