2 layer2.c: the layer 2 decoder, root of mpg123
4 copyright 1994-2009 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Michael Hipp
8 mpg123 started as mp2 decoder a long time ago...
9 part of this file is required for layer 1, too.
13 #include "mpg123lib_intern.h"
19 #ifndef NO_LAYER12 /* Stuff needed for layer I and II. */
21 static int grp_3tab
[32 * 3] = { 0, }; /* used: 27 */
22 static int grp_5tab
[128 * 3] = { 0, }; /* used: 125 */
23 static int grp_9tab
[1024 * 3] = { 0, }; /* used: 729 */
25 #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES)
26 #include "l12_integer_tables.h"
28 static const double mulmul
[27] =
30 0.0 , -2.0/3.0 , 2.0/3.0 ,
31 2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 ,
32 2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 ,
33 2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 ,
34 -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 ,
35 -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 void init_layer12(void)
41 const int base
[3][9] =
44 { 17, 18, 0 , 19, 20 , } ,
45 { 21, 1, 22, 23, 0, 24, 25, 2, 26 }
48 const int tablen
[3] = { 3 , 5 , 9 };
50 int *tables
[3] = { grp_3tab
, grp_5tab
, grp_9tab
};
60 *itable
++ = base
[i
][l
];
61 *itable
++ = base
[i
][k
];
62 *itable
++ = base
[i
][j
];
67 void init_layer12_stuff(mpg123_handle
*fr
, real
* (*init_table
)(mpg123_handle
*fr
, real
*table
, int m
))
73 table
= init_table(fr
, fr
->muls
[k
], k
);
78 real
* init_layer12_table(mpg123_handle
*fr
, real
*table
, int m
)
80 #if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES)
83 *table
++ = layer12_table
[m
][i
];
86 for(j
=3,i
=0;i
<63;i
++,j
--)
87 *table
++ = DOUBLE_TO_REAL_SCALE_LAYER12(mulmul
[m
] * pow(2.0,(double) j
/ 3.0));
94 real
* init_layer12_table_mmx(mpg123_handle
*fr
, real
*table
, int m
)
97 if(!fr
->p
.down_sample
)
99 for(j
=3,i
=0;i
<63;i
++,j
--)
100 *table
++ = DOUBLE_TO_REAL(16384 * mulmul
[m
] * pow(2.0,(double) j
/ 3.0));
104 for(j
=3,i
=0;i
<63;i
++,j
--)
105 *table
++ = DOUBLE_TO_REAL(mulmul
[m
] * pow(2.0,(double) j
/ 3.0));
111 #endif /* NO_LAYER12 */
113 /* The rest is the actual decoding of layer II data. */
117 void II_step_one(unsigned int *bit_alloc
,int *scale
,mpg123_handle
*fr
)
119 int stereo
= fr
->stereo
-1;
120 int sblimit
= fr
->II_sblimit
;
121 int jsbound
= fr
->jsbound
;
122 int sblimit2
= fr
->II_sblimit
<<stereo
;
123 const struct al_table
*alloc1
= fr
->alloc
;
125 unsigned int scfsi_buf
[64];
126 unsigned int *scfsi
,*bita
;
132 for(i
=jsbound
;i
;i
--,alloc1
+=(1<<step
))
135 *bita
++ = (char) getbits(fr
, step
);
136 *bita
++ = (char) getbits(fr
, step
);
138 for(i
=sblimit
-jsbound
;i
;i
--,alloc1
+=(1<<step
))
141 bita
[0] = (char) getbits(fr
, step
);
148 for(i
=sblimit2
;i
;i
--)
149 if(*bita
++) *scfsi
++ = (char) getbits_fast(fr
, 2);
153 for(i
=sblimit
;i
;i
--,alloc1
+=(1<<step
))
156 *bita
++ = (char) getbits(fr
, step
);
161 if(*bita
++) *scfsi
++ = (char) getbits_fast(fr
, 2);
166 for(i
=sblimit2
;i
;i
--)
171 *scale
++ = getbits_fast(fr
, 6);
172 *scale
++ = getbits_fast(fr
, 6);
173 *scale
++ = getbits_fast(fr
, 6);
176 *scale
++ = sc
= getbits_fast(fr
, 6);
178 *scale
++ = getbits_fast(fr
, 6);
181 *scale
++ = sc
= getbits_fast(fr
, 6);
185 default: /* case 3 */
186 *scale
++ = getbits_fast(fr
, 6);
187 *scale
++ = sc
= getbits_fast(fr
, 6);
194 void II_step_two(unsigned int *bit_alloc
,real fraction
[2][4][SBLIMIT
],int *scale
,mpg123_handle
*fr
,int x1
)
197 int stereo
= fr
->stereo
;
198 int sblimit
= fr
->II_sblimit
;
199 int jsbound
= fr
->jsbound
;
200 const struct al_table
*alloc2
,*alloc1
= fr
->alloc
;
201 unsigned int *bita
=bit_alloc
;
204 for(i
=0;i
<jsbound
;i
++,alloc1
+=(1<<step
))
207 for(j
=0;j
<stereo
;j
++)
211 k
=(alloc2
= alloc1
+ba
)->bits
;
212 if( (d1
=alloc2
->d
) < 0)
214 real cm
=fr
->muls
[k
][scale
[x1
]];
215 fraction
[j
][0][i
] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr
, k
) + d1
), cm
);
216 fraction
[j
][1][i
] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr
, k
) + d1
), cm
);
217 fraction
[j
][2][i
] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr
, k
) + d1
), cm
);
221 const int *table
[] = { 0,0,0,grp_3tab
,0,grp_5tab
,0,0,0,grp_9tab
};
222 unsigned int idx
,*tab
,m
=scale
[x1
];
223 idx
= (unsigned int) getbits(fr
, k
);
224 tab
= (unsigned int *) (table
[d1
] + idx
+ idx
+ idx
);
225 fraction
[j
][0][i
] = REAL_SCALE_LAYER12(fr
->muls
[*tab
++][m
]);
226 fraction
[j
][1][i
] = REAL_SCALE_LAYER12(fr
->muls
[*tab
++][m
]);
227 fraction
[j
][2][i
] = REAL_SCALE_LAYER12(fr
->muls
[*tab
][m
]);
232 fraction
[j
][0][i
] = fraction
[j
][1][i
] = fraction
[j
][2][i
] = DOUBLE_TO_REAL(0.0);
236 for(i
=jsbound
;i
<sblimit
;i
++,alloc1
+=(1<<step
))
239 bita
++; /* channel 1 and channel 2 bitalloc are the same */
242 k
=(alloc2
= alloc1
+ba
)->bits
;
243 if( (d1
=alloc2
->d
) < 0)
246 cm
=fr
->muls
[k
][scale
[x1
+3]];
247 fraction
[0][0][i
] = DOUBLE_TO_REAL_15((int)getbits(fr
, k
) + d1
);
248 fraction
[0][1][i
] = DOUBLE_TO_REAL_15((int)getbits(fr
, k
) + d1
);
249 fraction
[0][2][i
] = DOUBLE_TO_REAL_15((int)getbits(fr
, k
) + d1
);
250 fraction
[1][0][i
] = REAL_MUL_SCALE_LAYER12(fraction
[0][0][i
], cm
);
251 fraction
[1][1][i
] = REAL_MUL_SCALE_LAYER12(fraction
[0][1][i
], cm
);
252 fraction
[1][2][i
] = REAL_MUL_SCALE_LAYER12(fraction
[0][2][i
], cm
);
253 cm
=fr
->muls
[k
][scale
[x1
]];
254 fraction
[0][0][i
] = REAL_MUL_SCALE_LAYER12(fraction
[0][0][i
], cm
);
255 fraction
[0][1][i
] = REAL_MUL_SCALE_LAYER12(fraction
[0][1][i
], cm
);
256 fraction
[0][2][i
] = REAL_MUL_SCALE_LAYER12(fraction
[0][2][i
], cm
);
260 const int *table
[] = { 0,0,0,grp_3tab
,0,grp_5tab
,0,0,0,grp_9tab
};
261 unsigned int idx
,*tab
,m1
,m2
;
262 m1
= scale
[x1
]; m2
= scale
[x1
+3];
263 idx
= (unsigned int) getbits(fr
, k
);
264 tab
= (unsigned int *) (table
[d1
] + idx
+ idx
+ idx
);
265 fraction
[0][0][i
] = REAL_SCALE_LAYER12(fr
->muls
[*tab
][m1
]); fraction
[1][0][i
] = REAL_SCALE_LAYER12(fr
->muls
[*tab
++][m2
]);
266 fraction
[0][1][i
] = REAL_SCALE_LAYER12(fr
->muls
[*tab
][m1
]); fraction
[1][1][i
] = REAL_SCALE_LAYER12(fr
->muls
[*tab
++][m2
]);
267 fraction
[0][2][i
] = REAL_SCALE_LAYER12(fr
->muls
[*tab
][m1
]); fraction
[1][2][i
] = REAL_SCALE_LAYER12(fr
->muls
[*tab
][m2
]);
273 fraction
[0][0][i
] = fraction
[0][1][i
] = fraction
[0][2][i
] =
274 fraction
[1][0][i
] = fraction
[1][1][i
] = fraction
[1][2][i
] = DOUBLE_TO_REAL(0.0);
278 should we use individual scalefac for channel 2 or
279 is the current way the right one , where we just copy channel 1 to
281 The current 'strange' thing is, that we throw away the scalefac
282 values for the second channel ...!!
283 -> changed .. now we use the scalefac values of channel one !!
287 if(sblimit
> (fr
->down_sample_sblimit
) )
288 sblimit
= fr
->down_sample_sblimit
;
290 for(i
=sblimit
;i
<SBLIMIT
;i
++)
291 for (j
=0;j
<stereo
;j
++)
292 fraction
[j
][0][i
] = fraction
[j
][1][i
] = fraction
[j
][2][i
] = DOUBLE_TO_REAL(0.0);
296 static void II_select_table(mpg123_handle
*fr
)
298 const int translate
[3][2][16] =
301 { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 },
302 { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 }
305 { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 },
306 { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 }
309 { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 },
310 { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 }
315 const struct al_table
*tables
[5] = { alloc_0
, alloc_1
, alloc_2
, alloc_3
, alloc_4
};
316 const int sblims
[5] = { 27 , 30 , 8, 12 , 30 };
318 if(fr
->sampling_frequency
>= 3) /* Or equivalent: (fr->lsf == 1) */
321 table
= translate
[fr
->sampling_frequency
][2-fr
->stereo
][fr
->bitrate_index
];
323 sblim
= sblims
[table
];
324 fr
->alloc
= tables
[table
];
325 fr
->II_sblimit
= sblim
;
329 int do_layer2(mpg123_handle
*fr
)
333 int stereo
= fr
->stereo
;
334 /* pick_table clears unused subbands */
335 /* replacement for real fraction[2][4][SBLIMIT], needs alignment. */
336 real (*fraction
)[4][SBLIMIT
] = fr
->layer2
.fraction
;
337 unsigned int bit_alloc
[64];
339 int single
= fr
->single
;
342 fr
->jsbound
= (fr
->mode
== MPG_MD_JOINT_STEREO
) ? (fr
->mode_ext
<<2)+4 : fr
->II_sblimit
;
344 if(fr
->jsbound
> fr
->II_sblimit
)
346 fprintf(stderr
, "Truncating stereo boundary to sideband limit.\n");
347 fr
->jsbound
=fr
->II_sblimit
;
350 /* TODO: What happens with mono mixing, actually? */
351 if(stereo
== 1 || single
== SINGLE_MIX
) /* also, mix not really handled */
352 single
= SINGLE_LEFT
;
354 II_step_one(bit_alloc
, scale
, fr
);
356 for(i
=0;i
<SCALE_BLOCK
;i
++)
358 II_step_two(bit_alloc
,fraction
,scale
,fr
,i
>>2);
361 if(single
!= SINGLE_STEREO
)
362 clip
+= (fr
->synth_mono
)(fraction
[single
][j
], fr
);
364 clip
+= (fr
->synth_stereo
)(fraction
[0][j
], fraction
[1][j
], fr
);
371 #endif /* NO_LAYER2 */