1 /*
2 * Mpeg Layer-2 audio decoder
3 * --------------------------
4 * copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README'
5 *
6 */
7
8 /* $Id: layer2.c,v 1.1 2002/03/29 19:14:40 snelg Exp $ */
9
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13
14 #ifdef USE_LAYER_2
15
16
17 #include "common.h"
18 #include "layer2.h"
19 #include "l2tables.h"
20 #include "decode_i386.h"
21
22 #ifdef WITH_DMALLOC
23 #include <dmalloc.h>
24 #endif
25
26 static int grp_3tab[32 * 3] = { 0, }; /* used: 27 */
27 static int grp_5tab[128 * 3] = { 0, }; /* used: 125 */
28 static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
29
init_layer2(void)30 void init_layer2(void)
31 {
32 static const double mulmul[27] = {
33 0.0 , -2.0/3.0 , 2.0/3.0 ,
34 2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 ,
35 2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 ,
36 2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 ,
37 -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 ,
38 -8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0 };
39 static const int base[3][9] = {
40 { 1 , 0, 2 , } ,
41 { 17, 18, 0 , 19, 20 , } ,
42 { 21, 1, 22, 23, 0, 24, 25, 2, 26 } };
43 int i,j,k,l,len;
44 real *table;
45 static const int tablen[3] = { 3 , 5 , 9 };
46 static int *itable,*tables[3] = { grp_3tab , grp_5tab , grp_9tab };
47
48 for(i=0;i<3;i++)
49 {
50 itable = tables[i];
51 len = tablen[i];
52 for(j=0;j<len;j++)
53 for(k=0;k<len;k++)
54 for(l=0;l<len;l++)
55 {
56 *itable++ = base[i][l];
57 *itable++ = base[i][k];
58 *itable++ = base[i][j];
59 }
60 }
61
62 for(k=0;k<27;k++)
63 {
64 double m=mulmul[k];
65 table = muls[k];
66 for(j=3,i=0;i<63;i++,j--)
67 *table++ = (real)(m * pow(2.0,(double) j / 3.0));
68 *table++ = 0.0;
69 }
70 }
71
72
II_step_one(unsigned int * bit_alloc,int * scale,struct frame * fr)73 void II_step_one(unsigned int *bit_alloc,int *scale,struct frame *fr)
74 {
75 int stereo = fr->stereo-1;
76 int sblimit = fr->II_sblimit;
77 int jsbound = fr->jsbound;
78 int sblimit2 = fr->II_sblimit<<stereo;
79 struct al_table2 *alloc1 = fr->alloc;
80 int i;
81 static unsigned int scfsi_buf[64];
82 unsigned int *scfsi,*bita;
83 int sc,step;
84
85 bita = bit_alloc;
86 if(stereo)
87 {
88 for (i=jsbound;i;i--,alloc1+=(1<<step))
89 {
90 *bita++ = (char) getbits(step=alloc1->bits);
91 *bita++ = (char) getbits(step);
92 }
93 for (i=sblimit-jsbound;i;i--,alloc1+=(1<<step))
94 {
95 bita[0] = (char) getbits(step=alloc1->bits);
96 bita[1] = bita[0];
97 bita+=2;
98 }
99 bita = bit_alloc;
100 scfsi=scfsi_buf;
101 for (i=sblimit2;i;i--)
102 if (*bita++)
103 *scfsi++ = (char) getbits_fast(2);
104 }
105 else /* mono */
106 {
107 for (i=sblimit;i;i--,alloc1+=(1<<step))
108 *bita++ = (char) getbits(step=alloc1->bits);
109 bita = bit_alloc;
110 scfsi=scfsi_buf;
111 for (i=sblimit;i;i--)
112 if (*bita++)
113 *scfsi++ = (char) getbits_fast(2);
114 }
115
116 bita = bit_alloc;
117 scfsi=scfsi_buf;
118 for (i=sblimit2;i;i--)
119 if (*bita++)
120 switch (*scfsi++)
121 {
122 case 0:
123 *scale++ = getbits_fast(6);
124 *scale++ = getbits_fast(6);
125 *scale++ = getbits_fast(6);
126 break;
127 case 1 :
128 *scale++ = sc = getbits_fast(6);
129 *scale++ = sc;
130 *scale++ = getbits_fast(6);
131 break;
132 case 2:
133 *scale++ = sc = getbits_fast(6);
134 *scale++ = sc;
135 *scale++ = sc;
136 break;
137 default: /* case 3 */
138 *scale++ = getbits_fast(6);
139 *scale++ = sc = getbits_fast(6);
140 *scale++ = sc;
141 break;
142 }
143
144 }
145
II_step_two(unsigned int * bit_alloc,real fraction[2][4][SBLIMIT],int * scale,struct frame * fr,int x1)146 void II_step_two(unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,struct frame *fr,int x1)
147 {
148 int i,j,k,ba;
149 int stereo = fr->stereo;
150 int sblimit = fr->II_sblimit;
151 int jsbound = fr->jsbound;
152 struct al_table2 *alloc2,*alloc1 = fr->alloc;
153 unsigned int *bita=bit_alloc;
154 int d1,step;
155
156 for (i=0;i<jsbound;i++,alloc1+=(1<<step))
157 {
158 step = alloc1->bits;
159 for (j=0;j<stereo;j++)
160 {
161 if ( (ba=*bita++) )
162 {
163 k=(alloc2 = alloc1+ba)->bits;
164 if( (d1=alloc2->d) < 0)
165 {
166 real cm=muls[k][scale[x1]];
167 fraction[j][0][i] = ((real) ((int)getbits(k) + d1)) * cm;
168 fraction[j][1][i] = ((real) ((int)getbits(k) + d1)) * cm;
169 fraction[j][2][i] = ((real) ((int)getbits(k) + d1)) * cm;
170 }
171 else
172 {
173 static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
174 unsigned int idx,*tab,m=scale[x1];
175 idx = (unsigned int) getbits(k);
176 tab = (unsigned int *) (table[d1] + idx + idx + idx);
177 fraction[j][0][i] = muls[*tab++][m];
178 fraction[j][1][i] = muls[*tab++][m];
179 fraction[j][2][i] = muls[*tab][m];
180 }
181 scale+=3;
182 }
183 else
184 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
185 }
186 }
187
188 for (i=jsbound;i<sblimit;i++,alloc1+=(1<<step))
189 {
190 step = alloc1->bits;
191 bita++; /* channel 1 and channel 2 bitalloc are the same */
192 if ( (ba=*bita++) )
193 {
194 k=(alloc2 = alloc1+ba)->bits;
195 if( (d1=alloc2->d) < 0)
196 {
197 real cm;
198 cm=muls[k][scale[x1+3]];
199 fraction[1][0][i] = (fraction[0][0][i] = (real) ((int)getbits(k) + d1) ) * cm;
200 fraction[1][1][i] = (fraction[0][1][i] = (real) ((int)getbits(k) + d1) ) * cm;
201 fraction[1][2][i] = (fraction[0][2][i] = (real) ((int)getbits(k) + d1) ) * cm;
202 cm=muls[k][scale[x1]];
203 fraction[0][0][i] *= cm; fraction[0][1][i] *= cm; fraction[0][2][i] *= cm;
204 }
205 else
206 {
207 static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
208 unsigned int idx,*tab,m1,m2;
209 m1 = scale[x1]; m2 = scale[x1+3];
210 idx = (unsigned int) getbits(k);
211 tab = (unsigned int *) (table[d1] + idx + idx + idx);
212 fraction[0][0][i] = muls[*tab][m1]; fraction[1][0][i] = muls[*tab++][m2];
213 fraction[0][1][i] = muls[*tab][m1]; fraction[1][1][i] = muls[*tab++][m2];
214 fraction[0][2][i] = muls[*tab][m1]; fraction[1][2][i] = muls[*tab][m2];
215 }
216 scale+=6;
217 }
218 else {
219 fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] =
220 fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0;
221 }
222 /*
223 should we use individual scalefac for channel 2 or
224 is the current way the right one , where we just copy channel 1 to
225 channel 2 ??
226 The current 'strange' thing is, that we throw away the scalefac
227 values for the second channel ...!!
228 -> changed .. now we use the scalefac values of channel one !!
229 */
230 }
231
232 // if(sblimit > (fr->down_sample_sblimit) )
233 // sblimit = fr->down_sample_sblimit;
234
235 for(i=sblimit;i<SBLIMIT;i++)
236 for (j=0;j<stereo;j++)
237 fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
238
239 }
240
II_select_table(struct frame * fr)241 static void II_select_table(struct frame *fr)
242 {
243 static const int translate[3][2][16] =
244 { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
245 { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
246 { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
247 { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
248 { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
249 { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
250
251 int table,sblim;
252 static const struct al_table2 *tables[5] =
253 { alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 };
254 static const int sblims[5] = { 27 , 30 , 8, 12 , 30 };
255
256 if(fr->lsf)
257 table = 4;
258 else
259 table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index];
260 sblim = sblims[table];
261
262 fr->alloc = (struct al_table2*)tables[table];
263 fr->II_sblimit = sblim;
264 }
265
266
do_layer2(PMPSTR mp,unsigned char * pcm_sample,int * pcm_point)267 int do_layer2( PMPSTR mp,unsigned char *pcm_sample,int *pcm_point)
268 //int do_layer2(struct frame *fr,int outmode,struct audio_info_struct *ai)
269 {
270 int clip=0;
271 int i,j;
272 real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */
273 unsigned int bit_alloc[64];
274 int scale[192];
275 struct frame *fr=&(mp->fr);
276 int stereo = fr->stereo;
277 int single = fr->single;
278
279 II_select_table(fr);
280 fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
281 (fr->mode_ext<<2)+4 : fr->II_sblimit;
282
283 if(stereo == 1 || single == 3)
284 single = 0;
285
286 II_step_one(bit_alloc, scale, fr);
287
288 for (i=0;i<SCALE_BLOCK;i++)
289 {
290 II_step_two(bit_alloc,fraction,scale,fr,i>>2);
291 for (j=0;j<3;j++)
292 {
293 if(single >= 0)
294 {
295 clip += synth_1to1_mono(mp, fraction[single][j],pcm_sample,pcm_point);
296 }
297 else {
298 int p1 = *pcm_point;
299 clip += synth_1to1(mp, fraction[0][j],0,pcm_sample,&p1);
300 clip += synth_1to1(mp, fraction[1][j],1,pcm_sample,pcm_point);
301 }
302 }
303 }
304
305 return clip;
306 }
307
308
309 #endif
310