An audio player for macOS 10.8 and newer. https://kode54.net/cog
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.

372 lines
9.6KB

  1. /*
  2. layer2.c: the layer 2 decoder, root of mpg123
  3. copyright 1994-2009 by the mpg123 project - free software under the terms of the LGPL 2.1
  4. see COPYING and AUTHORS files in distribution or http://mpg123.org
  5. initially written by Michael Hipp
  6. mpg123 started as mp2 decoder a long time ago...
  7. part of this file is required for layer 1, too.
  8. */
  9. #include "mpg123lib_intern.h"
  10. #ifndef NO_LAYER2
  11. #include "l2tables.h"
  12. #endif
  13. #include "getbits.h"
  14. #ifndef NO_LAYER12 /* Stuff needed for layer I and II. */
  15. static int grp_3tab[32 * 3] = { 0, }; /* used: 27 */
  16. static int grp_5tab[128 * 3] = { 0, }; /* used: 125 */
  17. static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
  18. #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES)
  19. #include "l12_integer_tables.h"
  20. #else
  21. static const double mulmul[27] =
  22. {
  23. 0.0 , -2.0/3.0 , 2.0/3.0 ,
  24. 2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 ,
  25. 2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 ,
  26. 2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 ,
  27. -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 ,
  28. -8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0
  29. };
  30. #endif
  31. void init_layer12(void)
  32. {
  33. const int base[3][9] =
  34. {
  35. { 1 , 0, 2 , } ,
  36. { 17, 18, 0 , 19, 20 , } ,
  37. { 21, 1, 22, 23, 0, 24, 25, 2, 26 }
  38. };
  39. int i,j,k,l,len;
  40. const int tablen[3] = { 3 , 5 , 9 };
  41. int *itable;
  42. int *tables[3] = { grp_3tab , grp_5tab , grp_9tab };
  43. for(i=0;i<3;i++)
  44. {
  45. itable = tables[i];
  46. len = tablen[i];
  47. for(j=0;j<len;j++)
  48. for(k=0;k<len;k++)
  49. for(l=0;l<len;l++)
  50. {
  51. *itable++ = base[i][l];
  52. *itable++ = base[i][k];
  53. *itable++ = base[i][j];
  54. }
  55. }
  56. }
  57. void init_layer12_stuff(mpg123_handle *fr, real* (*init_table)(mpg123_handle *fr, real *table, int m))
  58. {
  59. int k;
  60. real *table;
  61. for(k=0;k<27;k++)
  62. {
  63. table = init_table(fr, fr->muls[k], k);
  64. *table++ = 0.0;
  65. }
  66. }
  67. real* init_layer12_table(mpg123_handle *fr, real *table, int m)
  68. {
  69. #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES)
  70. int i;
  71. for(i=0;i<63;i++)
  72. *table++ = layer12_table[m][i];
  73. #else
  74. int i,j;
  75. for(j=3,i=0;i<63;i++,j--)
  76. *table++ = DOUBLE_TO_REAL_SCALE_LAYER12(mulmul[m] * pow(2.0,(double) j / 3.0));
  77. #endif
  78. return table;
  79. }
  80. #ifdef OPT_MMXORSSE
  81. real* init_layer12_table_mmx(mpg123_handle *fr, real *table, int m)
  82. {
  83. int i,j;
  84. if(!fr->p.down_sample)
  85. {
  86. for(j=3,i=0;i<63;i++,j--)
  87. *table++ = DOUBLE_TO_REAL(16384 * mulmul[m] * pow(2.0,(double) j / 3.0));
  88. }
  89. else
  90. {
  91. for(j=3,i=0;i<63;i++,j--)
  92. *table++ = DOUBLE_TO_REAL(mulmul[m] * pow(2.0,(double) j / 3.0));
  93. }
  94. return table;
  95. }
  96. #endif
  97. #endif /* NO_LAYER12 */
  98. /* The rest is the actual decoding of layer II data. */
  99. #ifndef NO_LAYER2
  100. static void II_step_one(unsigned int *bit_alloc,int *scale,mpg123_handle *fr)
  101. {
  102. int stereo = fr->stereo-1;
  103. int sblimit = fr->II_sblimit;
  104. int jsbound = fr->jsbound;
  105. int sblimit2 = fr->II_sblimit<<stereo;
  106. const struct al_table *alloc1 = fr->alloc;
  107. int i;
  108. unsigned int scfsi_buf[64];
  109. unsigned int *scfsi,*bita;
  110. int sc,step;
  111. bita = bit_alloc;
  112. if(stereo)
  113. {
  114. for(i=jsbound;i;i--,alloc1+=(1<<step))
  115. {
  116. step=alloc1->bits;
  117. *bita++ = (char) getbits(fr, step);
  118. *bita++ = (char) getbits(fr, step);
  119. }
  120. for(i=sblimit-jsbound;i;i--,alloc1+=(1<<step))
  121. {
  122. step=alloc1->bits;
  123. bita[0] = (char) getbits(fr, step);
  124. bita[1] = bita[0];
  125. bita+=2;
  126. }
  127. bita = bit_alloc;
  128. scfsi=scfsi_buf;
  129. for(i=sblimit2;i;i--)
  130. if(*bita++) *scfsi++ = (char) getbits_fast(fr, 2);
  131. }
  132. else /* mono */
  133. {
  134. for(i=sblimit;i;i--,alloc1+=(1<<step))
  135. {
  136. step=alloc1->bits;
  137. *bita++ = (char) getbits(fr, step);
  138. }
  139. bita = bit_alloc;
  140. scfsi=scfsi_buf;
  141. for(i=sblimit;i;i--)
  142. if(*bita++) *scfsi++ = (char) getbits_fast(fr, 2);
  143. }
  144. bita = bit_alloc;
  145. scfsi=scfsi_buf;
  146. for(i=sblimit2;i;i--)
  147. if(*bita++)
  148. switch(*scfsi++)
  149. {
  150. case 0:
  151. *scale++ = (int) getbits_fast(fr, 6);
  152. *scale++ = (int) getbits_fast(fr, 6);
  153. *scale++ = (int) getbits_fast(fr, 6);
  154. break;
  155. case 1 :
  156. *scale++ = sc = (int) getbits_fast(fr, 6);
  157. *scale++ = sc;
  158. *scale++ = (int) getbits_fast(fr, 6);
  159. break;
  160. case 2:
  161. *scale++ = sc = (int) getbits_fast(fr, 6);
  162. *scale++ = sc;
  163. *scale++ = sc;
  164. break;
  165. default: /* case 3 */
  166. *scale++ = (int) getbits_fast(fr, 6);
  167. *scale++ = sc = (int) getbits_fast(fr, 6);
  168. *scale++ = sc;
  169. break;
  170. }
  171. }
  172. static void II_step_two(unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,mpg123_handle *fr,int x1)
  173. {
  174. int i,j,k,ba;
  175. int stereo = fr->stereo;
  176. int sblimit = fr->II_sblimit;
  177. int jsbound = fr->jsbound;
  178. const struct al_table *alloc2,*alloc1 = fr->alloc;
  179. unsigned int *bita=bit_alloc;
  180. int d1,step;
  181. for(i=0;i<jsbound;i++,alloc1+=(1<<step))
  182. {
  183. step = alloc1->bits;
  184. for(j=0;j<stereo;j++)
  185. {
  186. if( (ba=*bita++) )
  187. {
  188. k=(alloc2 = alloc1+ba)->bits;
  189. if( (d1=alloc2->d) < 0)
  190. {
  191. real cm=fr->muls[k][scale[x1]];
  192. fraction[j][0][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm);
  193. fraction[j][1][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm);
  194. fraction[j][2][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm);
  195. }
  196. else
  197. {
  198. const int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
  199. unsigned int idx,*tab,m=scale[x1];
  200. idx = (unsigned int) getbits(fr, k);
  201. tab = (unsigned int *) (table[d1] + idx + idx + idx);
  202. fraction[j][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m]);
  203. fraction[j][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m]);
  204. fraction[j][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m]);
  205. }
  206. scale+=3;
  207. }
  208. else
  209. fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = DOUBLE_TO_REAL(0.0);
  210. }
  211. }
  212. for(i=jsbound;i<sblimit;i++,alloc1+=(1<<step))
  213. {
  214. step = alloc1->bits;
  215. bita++; /* channel 1 and channel 2 bitalloc are the same */
  216. if( (ba=*bita++) )
  217. {
  218. k=(alloc2 = alloc1+ba)->bits;
  219. if( (d1=alloc2->d) < 0)
  220. {
  221. real cm;
  222. cm=fr->muls[k][scale[x1+3]];
  223. fraction[0][0][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1);
  224. fraction[0][1][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1);
  225. fraction[0][2][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1);
  226. fraction[1][0][i] = REAL_MUL_SCALE_LAYER12(fraction[0][0][i], cm);
  227. fraction[1][1][i] = REAL_MUL_SCALE_LAYER12(fraction[0][1][i], cm);
  228. fraction[1][2][i] = REAL_MUL_SCALE_LAYER12(fraction[0][2][i], cm);
  229. cm=fr->muls[k][scale[x1]];
  230. fraction[0][0][i] = REAL_MUL_SCALE_LAYER12(fraction[0][0][i], cm);
  231. fraction[0][1][i] = REAL_MUL_SCALE_LAYER12(fraction[0][1][i], cm);
  232. fraction[0][2][i] = REAL_MUL_SCALE_LAYER12(fraction[0][2][i], cm);
  233. }
  234. else
  235. {
  236. const int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
  237. unsigned int idx,*tab,m1,m2;
  238. m1 = scale[x1]; m2 = scale[x1+3];
  239. idx = (unsigned int) getbits(fr, k);
  240. tab = (unsigned int *) (table[d1] + idx + idx + idx);
  241. fraction[0][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m2]);
  242. fraction[0][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m2]);
  243. fraction[0][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m2]);
  244. }
  245. scale+=6;
  246. }
  247. else
  248. {
  249. fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] =
  250. fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = DOUBLE_TO_REAL(0.0);
  251. }
  252. /*
  253. Historic comment...
  254. should we use individual scalefac for channel 2 or
  255. is the current way the right one , where we just copy channel 1 to
  256. channel 2 ??
  257. The current 'strange' thing is, that we throw away the scalefac
  258. values for the second channel ...!!
  259. -> changed .. now we use the scalefac values of channel one !!
  260. */
  261. }
  262. if(sblimit > (fr->down_sample_sblimit) )
  263. sblimit = fr->down_sample_sblimit;
  264. for(i=sblimit;i<SBLIMIT;i++)
  265. for (j=0;j<stereo;j++)
  266. fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = DOUBLE_TO_REAL(0.0);
  267. }
  268. static void II_select_table(mpg123_handle *fr)
  269. {
  270. const int translate[3][2][16] =
  271. {
  272. {
  273. { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 },
  274. { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 }
  275. },
  276. {
  277. { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 },
  278. { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 }
  279. },
  280. {
  281. { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 },
  282. { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 }
  283. }
  284. };
  285. int table,sblim;
  286. const struct al_table *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 };
  287. const int sblims[5] = { 27 , 30 , 8, 12 , 30 };
  288. if(fr->sampling_frequency >= 3) /* Or equivalent: (fr->lsf == 1) */
  289. table = 4;
  290. else
  291. table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index];
  292. sblim = sblims[table];
  293. fr->alloc = tables[table];
  294. fr->II_sblimit = sblim;
  295. }
  296. int do_layer2(mpg123_handle *fr)
  297. {
  298. int clip=0;
  299. int i,j;
  300. int stereo = fr->stereo;
  301. /* pick_table clears unused subbands */
  302. /* replacement for real fraction[2][4][SBLIMIT], needs alignment. */
  303. real (*fraction)[4][SBLIMIT] = fr->layer2.fraction;
  304. unsigned int bit_alloc[64];
  305. int scale[192];
  306. int single = fr->single;
  307. II_select_table(fr);
  308. fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : fr->II_sblimit;
  309. if(fr->jsbound > fr->II_sblimit)
  310. {
  311. fprintf(stderr, "Truncating stereo boundary to sideband limit.\n");
  312. fr->jsbound=fr->II_sblimit;
  313. }
  314. /* TODO: What happens with mono mixing, actually? */
  315. if(stereo == 1 || single == SINGLE_MIX) /* also, mix not really handled */
  316. single = SINGLE_LEFT;
  317. II_step_one(bit_alloc, scale, fr);
  318. for(i=0;i<SCALE_BLOCK;i++)
  319. {
  320. II_step_two(bit_alloc,fraction,scale,fr,i>>2);
  321. for(j=0;j<3;j++)
  322. {
  323. if(single != SINGLE_STEREO)
  324. clip += (fr->synth_mono)(fraction[single][j], fr);
  325. else
  326. clip += (fr->synth_stereo)(fraction[0][j], fraction[1][j], fr);
  327. }
  328. }
  329. return clip;
  330. }
  331. #endif /* NO_LAYER2 */