diff options
Diffstat (limited to 'src/libmpg123/layer1.c')
-rw-r--r-- | src/libmpg123/layer1.c | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/src/libmpg123/layer1.c b/src/libmpg123/layer1.c new file mode 100644 index 000000000..dad35766c --- /dev/null +++ b/src/libmpg123/layer1.c @@ -0,0 +1,159 @@ +/* + * Mpeg Layer-1 audio decoder + * -------------------------- + * copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README' + * near unoptimzed ... + * + * may have a few bugs after last optimization ... + * + */ + +#include "mpg123.h" + +void I_step_one(unsigned int balloc[], unsigned int scale_index[2][SBLIMIT],struct frame *fr) +{ + unsigned int *ba=balloc; + unsigned int *sca = (unsigned int *) scale_index; + + if(fr->stereo) { + int i; + int jsbound = fr->jsbound; + for (i=0;i<jsbound;i++) { + *ba++ = getbits(4); + *ba++ = getbits(4); + } + for (i=jsbound;i<SBLIMIT;i++) + *ba++ = getbits(4); + + ba = balloc; + + for (i=0;i<jsbound;i++) { + if ((*ba++)) + *sca++ = getbits(6); + if ((*ba++)) + *sca++ = getbits(6); + } + for (i=jsbound;i<SBLIMIT;i++) + if ((*ba++)) { + *sca++ = getbits(6); + *sca++ = getbits(6); + } + } + else { + int i; + for (i=0;i<SBLIMIT;i++) + *ba++ = getbits(4); + ba = balloc; + for (i=0;i<SBLIMIT;i++) + if ((*ba++)) + *sca++ = getbits(6); + } +} + +void I_step_two(real fraction[2][SBLIMIT],unsigned int balloc[2*SBLIMIT], + unsigned int scale_index[2][SBLIMIT],struct frame *fr) +{ + int i,n; + int smpb[2*SBLIMIT]; /* values: 0-65535 */ + int *sample; + register unsigned int *ba; + register unsigned int *sca = (unsigned int *) scale_index; + + if(fr->stereo) { + int jsbound = fr->jsbound; + register real *f0 = fraction[0]; + register real *f1 = fraction[1]; + ba = balloc; + for (sample=smpb,i=0;i<jsbound;i++) { + if ((n = *ba++)) + *sample++ = getbits(n+1); + if ((n = *ba++)) + *sample++ = getbits(n+1); + } + for (i=jsbound;i<SBLIMIT;i++) + if ((n = *ba++)) + *sample++ = getbits(n+1); + + ba = balloc; + for (sample=smpb,i=0;i<jsbound;i++) { + if((n=*ba++)) + *f0++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++]; + else + *f0++ = 0.0; + if((n=*ba++)) + *f1++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++]; + else + *f1++ = 0.0; + } + for (i=jsbound;i<SBLIMIT;i++) { + if ((n=*ba++)) { + real samp = ( ((-1)<<n) + (*sample++) + 1); + *f0++ = samp * muls[n+1][*sca++]; + *f1++ = samp * muls[n+1][*sca++]; + } + else + *f0++ = *f1++ = 0.0; + } + } + else { + register real *f0 = fraction[0]; + ba = balloc; + for (sample=smpb,i=0;i<SBLIMIT;i++) + if ((n = *ba++)) + *sample++ = getbits(n+1); + ba = balloc; + for (sample=smpb,i=0;i<SBLIMIT;i++) { + if((n=*ba++)) + *f0++ = (real) ( ((-1)<<n) + (*sample++) + 1) * muls[n+1][*sca++]; + else + *f0++ = 0.0; + } + } +} + +void do_layer1(mpgaudio_t *mp, uint32_t pts) +{ + int clip=0; + struct frame *fr = &mp->fr; + int i,stereo = fr->stereo; + static unsigned int balloc[2*SBLIMIT]; + static unsigned int scale_index[2][SBLIMIT]; + static real fraction[2][SBLIMIT]; + int single = fr->single; + int num_bytes; + + fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : 32; + + if(stereo == 1 || single == 3) + single = 0; + + I_step_one(balloc,scale_index,fr); + + num_bytes=0; + for (i=0;i<SCALE_BLOCK;i++) + { + I_step_two(fraction,balloc,scale_index,fr); + + if(single >= 0) { + clip += synth_1to1_mono(mp, (real*)fraction[single],mp->osspace,&num_bytes); + } + else { + int p1 = num_bytes; + clip += synth_1to1(mp, (real*)fraction[0],0,mp->osspace,&p1); + clip += synth_1to1(mp, (real*)fraction[1],1,mp->osspace,&num_bytes); + } + } + + if (!mp->is_output_initialized) { + mp->ao_output->open (16, fr->sample_rate, + stereo-1 ? AO_MODE_STEREO: AO_MODE_MONO); + mp->is_output_initialized = 1; + + printf ("layer1\n"); + } + + mp->ao_output->write_audio_data ((int16_t*)mp->osspace, num_bytes/(stereo-1 ? 4:2), pts); + +} + + |