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