layer1.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Mpeg Layer-1 audio decoder
  3. * --------------------------
  4. * copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README'
  5. * near unoptimzed ...
  6. *
  7. * may have a few bugs after last optimization ...
  8. *
  9. */
  10. #include "mpg123.h"
  11. void I_step_one(unsigned int balloc[], unsigned int scale_index[2][SBLIMIT],struct frame *fr)
  12. {
  13. unsigned int *ba=balloc;
  14. unsigned int *sca = (unsigned int *) scale_index;
  15. if(fr->stereo) {
  16. int i;
  17. int jsbound = fr->jsbound;
  18. for (i=0;i<jsbound;i++) {
  19. *ba++ = getbits(4);
  20. *ba++ = getbits(4);
  21. }
  22. for (i=jsbound;i<SBLIMIT;i++)
  23. *ba++ = getbits(4);
  24. ba = balloc;
  25. for (i=0;i<jsbound;i++) {
  26. if ((*ba++))
  27. *sca++ = getbits(6);
  28. if ((*ba++))
  29. *sca++ = getbits(6);
  30. }
  31. for (i=jsbound;i<SBLIMIT;i++)
  32. if ((*ba++)) {
  33. *sca++ = getbits(6);
  34. *sca++ = getbits(6);
  35. }
  36. }
  37. else {
  38. int i;
  39. for (i=0;i<SBLIMIT;i++)
  40. *ba++ = getbits(4);
  41. ba = balloc;
  42. for (i=0;i<SBLIMIT;i++)
  43. if ((*ba++))
  44. *sca++ = getbits(6);
  45. }
  46. }
  47. void I_step_two(real fraction[2][SBLIMIT],unsigned int balloc[2*SBLIMIT],
  48. unsigned int scale_index[2][SBLIMIT],struct frame *fr)
  49. {
  50. int i,n;
  51. int smpb[2*SBLIMIT]; /* values: 0-65535 */
  52. int *sample;
  53. register unsigned int *ba;
  54. register unsigned int *sca = (unsigned int *) scale_index;
  55. if(fr->stereo) {
  56. int jsbound = fr->jsbound;
  57. register real *f0 = fraction[0];
  58. register real *f1 = fraction[1];
  59. ba = balloc;
  60. for (sample=smpb,i=0;i<jsbound;i++) {
  61. if ((n = *ba++))
  62. *sample++ = getbits(n+1);
  63. if ((n = *ba++))
  64. *sample++ = getbits(n+1);
  65. }
  66. for (i=jsbound;i<SBLIMIT;i++)
  67. if ((n = *ba++))
  68. *sample++ = getbits(n+1);
  69. ba = balloc;
  70. for (sample=smpb,i=0;i<jsbound;i++) {
  71. if((n=*ba++))
  72. *f0++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++];
  73. else
  74. *f0++ = 0.0;
  75. if((n=*ba++))
  76. *f1++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++];
  77. else
  78. *f1++ = 0.0;
  79. }
  80. for (i=jsbound;i<SBLIMIT;i++) {
  81. if ((n=*ba++)) {
  82. real samp = ( ((-1)<<n) + (*sample++) + 1);
  83. *f0++ = samp * muls[n+1][*sca++];
  84. *f1++ = samp * muls[n+1][*sca++];
  85. }
  86. else
  87. *f0++ = *f1++ = 0.0;
  88. }
  89. for(i=fr->down_sample_sblimit;i<32;i++)
  90. fraction[0][i] = fraction[1][i] = 0.0;
  91. }
  92. else {
  93. register real *f0 = fraction[0];
  94. ba = balloc;
  95. for (sample=smpb,i=0;i<SBLIMIT;i++)
  96. if ((n = *ba++))
  97. *sample++ = getbits(n+1);
  98. ba = balloc;
  99. for (sample=smpb,i=0;i<SBLIMIT;i++) {
  100. if((n=*ba++))
  101. *f0++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++];
  102. else
  103. *f0++ = 0.0;
  104. }
  105. for(i=fr->down_sample_sblimit;i<32;i++)
  106. fraction[0][i] = 0.0;
  107. }
  108. }
  109. int do_layer1(struct frame *fr,int outmode,struct audio_info_struct *ai)
  110. {
  111. int clip=0;
  112. int i,stereo = fr->stereo;
  113. unsigned int balloc[2*SBLIMIT];
  114. unsigned int scale_index[2][SBLIMIT];
  115. real fraction[2][SBLIMIT];
  116. int single = fr->single;
  117. fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : 32;
  118. if(stereo == 1 || single == 3)
  119. single = 0;
  120. I_step_one(balloc,scale_index,fr);
  121. for (i=0;i<SCALE_BLOCK;i++)
  122. {
  123. I_step_two(fraction,balloc,scale_index,fr);
  124. if(single >= 0)
  125. {
  126. clip += (fr->synth_mono)( (real *) fraction[single],pcm_sample,&pcm_point);
  127. }
  128. else {
  129. int p1 = pcm_point;
  130. clip += (fr->synth)( (real *) fraction[0],0,pcm_sample,&p1);
  131. clip += (fr->synth)( (real *) fraction[1],1,pcm_sample,&pcm_point);
  132. }
  133. if(pcm_point >= audiobufsize)
  134. audio_flush(outmode,ai);
  135. }
  136. return clip;
  137. }