2
* Mpeg Layer-2 audio decoder
3
* --------------------------
4
* copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README'
13
static int grp_3tab[32 * 3] = { 0, }; /* used: 27 */
14
static int grp_5tab[128 * 3] = { 0, }; /* used: 125 */
15
static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */
17
real muls[27][64]; /* also used by layer 1 */
20
void init_layer2(void)
22
static double mulmul[27] = {
23
0.0 , -2.0/3.0 , 2.0/3.0 ,
24
2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 ,
25
2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 ,
26
2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 ,
27
-4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 ,
28
-8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0 };
29
static int base[3][9] = {
31
{ 17, 18, 0 , 19, 20 , } ,
32
{ 21, 1, 22, 23, 0, 24, 25, 2, 26 } };
35
static int tablen[3] = { 3 , 5 , 9 };
36
static int *itable,*tables[3] = { grp_3tab , grp_5tab , grp_9tab };
38
const double twotothethird = pow((double)2.0, (double)1/3);
49
*itable++ = base[i][l];
50
*itable++ = base[i][k];
51
*itable++ = base[i][j];
59
double oldtable = m * 2.0 * twotothethird;
64
if(!param.down_sample)
65
for(j=3,i=0;i<63;i++,j--)
66
*table++ = 16384 * m * pow(2.0,(double) j / 3.0);
69
for(j=3,i=0;i<63;i++,j--) {
71
*table = oldtable / twotothethird;
74
*table++ = m * pow(2.0,(double) j / 3.0);
82
void II_step_one(unsigned int *bit_alloc,int *scale,struct frame *fr)
84
int stereo = fr->stereo-1;
85
int sblimit = fr->II_sblimit;
86
int jsbound = fr->jsbound;
87
int sblimit2 = fr->II_sblimit<<stereo;
88
struct al_table *alloc1 = fr->alloc;
90
static unsigned int scfsi_buf[64];
91
unsigned int *scfsi,*bita;
97
for (i=jsbound;i;i--,alloc1+=(1<<step))
99
*bita++ = (char) getbits(&bsi,step=alloc1->bits);
100
*bita++ = (char) getbits(&bsi,step);
102
for (i=sblimit-jsbound;i;i--,alloc1+=(1<<step))
104
bita[0] = (char) getbits(&bsi,step=alloc1->bits);
110
for (i=sblimit2;i;i--)
112
*scfsi++ = (char) getbits_fast(&bsi,2);
116
for (i=sblimit;i;i--,alloc1+=(1<<step))
117
*bita++ = (char) getbits(&bsi,step=alloc1->bits);
120
for (i=sblimit;i;i--)
122
*scfsi++ = (char) getbits_fast(&bsi,2);
127
for (i=sblimit2;i;i--)
132
*scale++ = getbits_fast(&bsi,6);
133
*scale++ = getbits_fast(&bsi,6);
134
*scale++ = getbits_fast(&bsi,6);
137
*scale++ = sc = getbits_fast(&bsi,6);
139
*scale++ = getbits_fast(&bsi,6);
142
*scale++ = sc = getbits_fast(&bsi,6);
146
default: /* case 3 */
147
*scale++ = getbits_fast(&bsi,6);
148
*scale++ = sc = getbits_fast(&bsi,6);
155
void II_step_two(unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,struct frame *fr,int x1)
158
int stereo = fr->stereo;
159
int sblimit = fr->II_sblimit;
160
int jsbound = fr->jsbound;
161
struct al_table *alloc2,*alloc1 = fr->alloc;
162
unsigned int *bita=bit_alloc;
165
for (i=0;i<jsbound;i++,alloc1+=(1<<step))
168
for (j=0;j<stereo;j++)
172
k=(alloc2 = alloc1+ba)->bits;
173
if( (d1=alloc2->d) < 0)
175
real cm=muls[k][scale[x1]];
176
fraction[j][0][i] = ((real) ((int)getbits(&bsi,k) + d1)) * cm;
177
fraction[j][1][i] = ((real) ((int)getbits(&bsi,k) + d1)) * cm;
178
fraction[j][2][i] = ((real) ((int)getbits(&bsi,k) + d1)) * cm;
182
static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
183
unsigned int idx,*tab,m=scale[x1];
184
idx = (unsigned int) getbits(&bsi,k);
185
tab = (unsigned int *) (table[d1] + idx + idx + idx);
186
fraction[j][0][i] = muls[*tab++][m];
187
fraction[j][1][i] = muls[*tab++][m];
188
fraction[j][2][i] = muls[*tab][m];
193
fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
197
for (i=jsbound;i<sblimit;i++,alloc1+=(1<<step))
200
bita++; /* channel 1 and channel 2 bitalloc are the same */
203
k=(alloc2 = alloc1+ba)->bits;
204
if( (d1=alloc2->d) < 0)
207
cm=muls[k][scale[x1+3]];
208
fraction[1][0][i] = (fraction[0][0][i] = (real) ((int)getbits(&bsi,k) + d1) ) * cm;
209
fraction[1][1][i] = (fraction[0][1][i] = (real) ((int)getbits(&bsi,k) + d1) ) * cm;
210
fraction[1][2][i] = (fraction[0][2][i] = (real) ((int)getbits(&bsi,k) + d1) ) * cm;
211
cm=muls[k][scale[x1]];
212
fraction[0][0][i] *= cm; fraction[0][1][i] *= cm; fraction[0][2][i] *= cm;
216
static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab };
217
unsigned int idx,*tab,m1,m2;
218
m1 = scale[x1]; m2 = scale[x1+3];
219
idx = (unsigned int) getbits(&bsi,k);
220
tab = (unsigned int *) (table[d1] + idx + idx + idx);
221
fraction[0][0][i] = muls[*tab][m1]; fraction[1][0][i] = muls[*tab++][m2];
222
fraction[0][1][i] = muls[*tab][m1]; fraction[1][1][i] = muls[*tab++][m2];
223
fraction[0][2][i] = muls[*tab][m1]; fraction[1][2][i] = muls[*tab][m2];
228
fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] =
229
fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0;
232
should we use individual scalefac for channel 2 or
233
is the current way the right one , where we just copy channel 1 to
235
The current 'strange' thing is, that we throw away the scalefac
236
values for the second channel ...!!
237
-> changed .. now we use the scalefac values of channel one !!
241
if(sblimit > (fr->down_sample_sblimit) )
242
sblimit = fr->down_sample_sblimit;
244
for(i=sblimit;i<SBLIMIT;i++)
245
for (j=0;j<stereo;j++)
246
fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0;
250
static void II_select_table(struct frame *fr)
252
static int translate[3][2][16] =
253
{ { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } ,
254
{ 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } ,
255
{ { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } ,
256
{ 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } ,
257
{ { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } ,
258
{ 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } };
261
static struct al_table *tables[5] =
262
{ alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 };
263
static int sblims[5] = { 27 , 30 , 8, 12 , 30 };
268
table = translate[fr->sampling_frequency][2-fr->stereo][fr->bitrate_index];
269
sblim = sblims[table];
271
fr->alloc = tables[table];
272
fr->II_sblimit = sblim;
276
int do_layer2(struct mpstr *mp,struct frame *fr,int outmode,struct audio_info_struct *ai)
280
int stereo = fr->stereo;
281
real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */
282
unsigned int bit_alloc[64];
284
int single = fr->single;
287
fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ?
288
(fr->mode_ext<<2)+4 : fr->II_sblimit;
290
if(stereo == 1 || single == 3)
293
II_step_one(bit_alloc, scale, fr);
295
for (i=0;i<SCALE_BLOCK;i++)
297
II_step_two(bit_alloc,fraction,scale,fr,i>>2);
302
clip += (fr->synth_mono) (fraction[single][j],pcm_sample,&pcm_point);
306
clip += (fr->synth) (fraction[0][j],0,pcm_sample,&p1);
307
clip += (fr->synth) (fraction[1][j],1,pcm_sample,&pcm_point);
310
if(pcm_point >= audiobufsize)
311
audio_flush(outmode,ai);