2
* Mpeg Layer-2 audio decoder
3
* --------------------------
4
* copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README'
11
static int grp_3tab[32 * 3] = { 0, }; /* used: 27 */
12
static int grp_5tab[128 * 3] = { 0, }; /* used: 125 */
13
static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
15
real muls[27][64]; /* also used by layer 1 */
17
void init_layer2(void)
19
static double mulmul[27] = {
20
0.0 , -2.0/3.0 , 2.0/3.0 ,
21
2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 ,
22
2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 ,
23
2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 ,
24
-4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 ,
25
-8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0 };
26
static int base[3][9] = {
28
{ 17, 18, 0 , 19, 20 , } ,
29
{ 21, 1, 22, 23, 0, 24, 25, 2, 26 } };
32
static int tablen[3] = { 3 , 5 , 9 };
33
static int *itable,*tables[3] = { grp_3tab , grp_5tab , grp_9tab };
43
*itable++ = base[i][l];
44
*itable++ = base[i][k];
45
*itable++ = base[i][j];
53
for(j=3,i=0;i<63;i++,j--)
54
*table++ = m * pow(2.0,(double) j / 3.0);
60
void II_step_one(unsigned int *bit_alloc,int *scale,struct frame *fr)
62
int stereo = fr->stereo-1;
63
int sblimit = fr->II_sblimit;
64
int jsbound = fr->jsbound;
65
int sblimit2 = fr->II_sblimit<<stereo;
66
struct al_table *alloc1 = fr->alloc;
68
static unsigned int scfsi_buf[64];
69
unsigned int *scfsi,*bita;
75
for (i=jsbound;i;i--,alloc1+=(1<<step))
77
*bita++ = (char) getbits(step=alloc1->bits);
78
*bita++ = (char) getbits(step);
80
for (i=sblimit-jsbound;i;i--,alloc1+=(1<<step))
82
bita[0] = (char) getbits(step=alloc1->bits);
88
for (i=sblimit2;i;i--)
90
*scfsi++ = (char) getbits_fast(2);
94
for (i=sblimit;i;i--,alloc1+=(1<<step))
95
*bita++ = (char) getbits(step=alloc1->bits);
100
*scfsi++ = (char) getbits_fast(2);
105
for (i=sblimit2;i;i--)
110
*scale++ = getbits_fast(6);
111
*scale++ = getbits_fast(6);
112
*scale++ = getbits_fast(6);
115
*scale++ = sc = getbits_fast(6);
117
*scale++ = getbits_fast(6);
120
*scale++ = sc = getbits_fast(6);
124
default: /* case 3 */
125
*scale++ = getbits_fast(6);
126
*scale++ = sc = getbits_fast(6);
133
void II_step_two(unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,struct frame *fr,int x1)
136
int stereo = fr->stereo;
137
int sblimit = fr->II_sblimit;
138
int jsbound = fr->jsbound;
139
struct al_table *alloc2,*alloc1 = fr->alloc;
140
unsigned int *bita=bit_alloc;
143
for (i=0;i<jsbound;i++,alloc1+=(1<<step))
146
for (j=0;j<stereo;j++)
150
k=(alloc2 = alloc1+ba)->bits;
151
if( (d1=alloc2->d) < 0)
153
real cm=muls[k][scale[x1]];
154
fraction[j][0][i] = ((real) ((int)getbits(k) + d1)) * cm;
155
fraction[j][1][i] = ((real) ((int)getbits(k) + d1)) * cm;
156
fraction[j][2][i] = ((real) ((int)getbits(k) + d1)) * cm;
160
static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
161
unsigned int idx,*tab,m=scale[x1];
162
idx = (unsigned int) getbits(k);
163
tab = (unsigned int *) (table[d1] + idx + idx + idx);
164
fraction[j][0][i] = muls[*tab++][m];
165
fraction[j][1][i] = muls[*tab++][m];
166
fraction[j][2][i] = muls[*tab][m];
171
fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
175
for (i=jsbound;i<sblimit;i++,alloc1+=(1<<step))
178
bita++; /* channel 1 and channel 2 bitalloc are the same */
181
k=(alloc2 = alloc1+ba)->bits;
182
if( (d1=alloc2->d) < 0)
185
cm=muls[k][scale[x1+3]];
186
fraction[1][0][i] = (fraction[0][0][i] = (real) ((int)getbits(k) + d1) ) * cm;
187
fraction[1][1][i] = (fraction[0][1][i] = (real) ((int)getbits(k) + d1) ) * cm;
188
fraction[1][2][i] = (fraction[0][2][i] = (real) ((int)getbits(k) + d1) ) * cm;
189
cm=muls[k][scale[x1]];
190
fraction[0][0][i] *= cm; fraction[0][1][i] *= cm; fraction[0][2][i] *= cm;
194
static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
195
unsigned int idx,*tab,m1,m2;
196
m1 = scale[x1]; m2 = scale[x1+3];
197
idx = (unsigned int) getbits(k);
198
tab = (unsigned int *) (table[d1] + idx + idx + idx);
199
fraction[0][0][i] = muls[*tab][m1]; fraction[1][0][i] = muls[*tab++][m2];
200
fraction[0][1][i] = muls[*tab][m1]; fraction[1][1][i] = muls[*tab++][m2];
201
fraction[0][2][i] = muls[*tab][m1]; fraction[1][2][i] = muls[*tab][m2];
206
fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] =
207
fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0;
210
should we use individual scalefac for channel 2 or
211
is the current way the right one , where we just copy channel 1 to
213
The current 'strange' thing is, that we throw away the scalefac
214
values for the second channel ...!!
215
-> changed .. now we use the scalefac values of channel one !!
219
if(sblimit > (fr->down_sample_sblimit) )
220
sblimit = fr->down_sample_sblimit;
222
for(i=sblimit;i<SBLIMIT;i++)
223
for (j=0;j<stereo;j++)
224
fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
228
static void II_select_table(struct frame *fr)
230
static int translate[3][2][16] =
231
{ { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
232
{ 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
233
{ { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
234
{ 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
235
{ { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
236
{ 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
239
static struct al_table *tables[5] =
240
{ alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 };
241
static int sblims[5] = { 27 , 30 , 8, 12 , 30 };
246
table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index];
247
sblim = sblims[table];
249
fr->alloc = tables[table];
250
fr->II_sblimit = sblim;
254
int do_layer2(struct frame *fr,int outmode,struct audio_info_struct *ai)
258
int stereo = fr->stereo;
259
real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */
260
unsigned int bit_alloc[64];
262
int single = fr->single;
265
fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
266
(fr->mode_ext<<2)+4 : fr->II_sblimit;
268
if(stereo == 1 || single == 3)
271
II_step_one(bit_alloc, scale, fr);
273
for (i=0;i<SCALE_BLOCK;i++)
275
II_step_two(bit_alloc,fraction,scale,fr,i>>2);
280
clip += (fr->synth_mono) (fraction[single][j],pcm_sample,&pcm_point);
284
clip += (fr->synth) (fraction[0][j],0,pcm_sample,&p1);
285
clip += (fr->synth) (fraction[1][j],1,pcm_sample,&pcm_point);
288
if(pcm_point >= audiobufsize)
289
audio_flush(outmode,ai);