Newer
Older
monitord / lame-3.97 / mpglib / .svn / text-base / layer1.c.svn-base
  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.  
  11. /* $Id: layer1.c,v 1.20 2004/04/14 22:15:44 robert Exp $ */
  12.  
  13. #ifdef HAVE_CONFIG_H
  14. # include <config.h>
  15. #endif
  16.  
  17. #ifdef USE_LAYER_1
  18.  
  19. #include <assert.h>
  20. #include "common.h"
  21. #include "decode_i386.h"
  22.  
  23. #ifdef WITH_DMALLOC
  24. #include <dmalloc.h>
  25. #endif
  26.  
  27. static void
  28. I_step_one(PMPSTR mp, unsigned int balloc[], unsigned int scale_index[2][SBLIMIT],struct frame *fr)
  29. {
  30. unsigned int *ba=balloc;
  31. unsigned int *sca = (unsigned int *) scale_index;
  32.  
  33. assert ( fr->stereo == 1 || fr->stereo == 2 );
  34. if(fr->stereo==2) {
  35. int i;
  36. int jsbound = fr->jsbound;
  37. for (i=0;i<jsbound;i++) {
  38. *ba++ = getbits(mp,4);
  39. *ba++ = getbits(mp,4);
  40. }
  41. for (i=jsbound;i<SBLIMIT;i++)
  42. *ba++ = getbits(mp,4);
  43.  
  44. ba = balloc;
  45.  
  46. for (i=0;i<jsbound;i++) {
  47. if ((*ba++))
  48. *sca++ = getbits(mp,6);
  49. if ((*ba++))
  50. *sca++ = getbits(mp,6);
  51. }
  52. for (i=jsbound;i<SBLIMIT;i++)
  53. if ((*ba++)) {
  54. *sca++ = getbits(mp,6);
  55. *sca++ = getbits(mp,6);
  56. }
  57. }
  58. else {
  59. int i;
  60. for (i=0;i<SBLIMIT;i++)
  61. *ba++ = getbits(mp,4);
  62. ba = balloc;
  63. for (i=0;i<SBLIMIT;i++)
  64. if ((*ba++))
  65. *sca++ = getbits(mp,6);
  66. }
  67. }
  68.  
  69. static void
  70. I_step_two(PMPSTR mp, real fraction[2][SBLIMIT],unsigned int balloc[2*SBLIMIT],
  71. unsigned int scale_index[2][SBLIMIT],struct frame *fr)
  72. {
  73. int i,n;
  74. int smpb[2*SBLIMIT]; /* values: 0-65535 */
  75. int *sample;
  76. unsigned int *ba;
  77. unsigned int *sca = (unsigned int *) scale_index;
  78.  
  79. assert ( fr->stereo == 1 || fr->stereo == 2 );
  80. if(fr->stereo == 2) {
  81. int jsbound = fr->jsbound;
  82. real *f0 = fraction[0];
  83. real *f1 = fraction[1];
  84. ba = balloc;
  85. for (sample=smpb,i=0;i<jsbound;i++) {
  86. if ((n = *ba++))
  87. *sample++ = getbits(mp,n+1);
  88. if ((n = *ba++))
  89. *sample++ = getbits(mp,n+1);
  90. }
  91. for (i=jsbound;i<SBLIMIT;i++)
  92. if ((n = *ba++))
  93. *sample++ = getbits(mp,n+1);
  94.  
  95. ba = balloc;
  96. for (sample=smpb,i=0;i<jsbound;i++) {
  97. if((n=*ba++))
  98. *f0++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++];
  99. else
  100. *f0++ = 0.0;
  101. if((n=*ba++))
  102. *f1++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++];
  103. else
  104. *f1++ = 0.0;
  105. }
  106. for (i=jsbound;i<SBLIMIT;i++) {
  107. if ((n=*ba++)) {
  108. real samp = (real)( ((-1)<<n) + (*sample++) + 1);
  109. *f0++ = samp * muls[n+1][*sca++];
  110. *f1++ = samp * muls[n+1][*sca++];
  111. }
  112. else
  113. *f0++ = *f1++ = 0.0;
  114. }
  115. for(i=fr->down_sample_sblimit;i<32;i++)
  116. fraction[0][i] = fraction[1][i] = 0.0;
  117. }
  118. else {
  119. real *f0 = fraction[0];
  120. ba = balloc;
  121. for (sample=smpb,i=0;i<SBLIMIT;i++)
  122. if ((n = *ba++))
  123. *sample++ = getbits(mp,n+1);
  124. ba = balloc;
  125. for (sample=smpb,i=0;i<SBLIMIT;i++) {
  126. if((n=*ba++))
  127. *f0++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++];
  128. else
  129. *f0++ = 0.0;
  130. }
  131. for(i=fr->down_sample_sblimit;i<32;i++)
  132. fraction[0][i] = 0.0;
  133. }
  134. }
  135.  
  136. /*int do_layer1(struct frame *fr,int outmode,struct audio_info_struct *ai) */
  137. int do_layer1(PMPSTR mp, unsigned char *pcm_sample,int *pcm_point)
  138. {
  139. int clip=0;
  140. unsigned int balloc[2*SBLIMIT];
  141. unsigned int scale_index[2][SBLIMIT];
  142. real fraction[2][SBLIMIT];
  143. struct frame *fr=&(mp->fr);
  144. int i,stereo = fr->stereo;
  145. int single = fr->single;
  146.  
  147. fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : 32;
  148.  
  149. if (stereo == 1 || single == 3)
  150. single = 0;
  151.  
  152. I_step_one(mp,balloc,scale_index,fr);
  153.  
  154. for (i=0;i<SCALE_BLOCK;i++)
  155. {
  156. I_step_two(mp,fraction,balloc,scale_index,fr);
  157.  
  158. if(single >= 0)
  159. {
  160. clip += synth_1to1_mono( mp, (real *) fraction[single],pcm_sample,pcm_point);
  161. }
  162. else {
  163. int p1 = *pcm_point;
  164. clip += synth_1to1( mp, (real *) fraction[0],0,pcm_sample,&p1);
  165. clip += synth_1to1( mp, (real *) fraction[1],1,pcm_sample,pcm_point);
  166. }
  167. }
  168.  
  169. return clip;
  170. }
  171.  
  172. #endif
  173.