/* this file is a part of amp software, (C) tomislav uzelac 1996,1997 */ /* layer2.c MPEG audio layer2 support * * Created by: Tomislav Uzelac Mar 1996 * merged with amp, May 19 1997 */ #include "amp.h" #include "audio.h" #include "getbits.h" #include "transform.h" #define LAYER2 #include "layer2.h" #include "Supermodel.h" int layer2_frame(struct AUDIO_HEADER *header,int cnt) { int i,s,sb,ch,gr,bitrate,bound=0; char (*nbal)[],(*bit_alloc_index)[][16]; unsigned char allocation[2][32]; unsigned char scfsi[2][32]; float scalefactor[2][32][3]; float subband_sample[2][32][36]; int sblimit,nlevels,grouping; float c,d; int no_of_bits,mpi; unsigned short sb_sample_buf[3]; int hsize,fs,mean_frame_size; bit_alloc_index=(char (*)[][16])&t_alloc0; nbal=(char (*)[])&t_nbal0; // shut up compiler sblimit = 0; hsize=4; if (header->protection_bit==0) hsize+=2; bitrate=t_bitrate[header->ID][3-header->layer][header->bitrate_index]; fs=t_sampling_frequency[header->ID][header->sampling_frequency]; if (header->ID) mean_frame_size=144000*bitrate/fs; else mean_frame_size=72000*bitrate/fs; /* layers 1 and 2 do not have a 'bit reservoir' */ append=data=0; fillbfr(mean_frame_size + header->padding_bit - hsize); switch (header->mode) { case 0 : case 2 : nch=2; bound=32; bitrate=bitrate/2; break; case 3 : nch=1; bound=32; break; case 1 : nch=2; bitrate=bitrate/2; bound=(header->mode_extension+1)*4; } if (header->ID==1) switch (header->sampling_frequency) { case 0 : switch (bitrate) /* 0 = 44.1 kHz */ { case 56 : case 64 : case 80 : bit_alloc_index=(char (*)[][16])&t_alloc0; nbal=(char (*)[])&t_nbal0; sblimit=27; break; case 96 : case 112 : case 128 : case 160 : case 192 : bit_alloc_index=(char (*)[][16])&t_alloc1; nbal=(char (*)[])&t_nbal1; sblimit=30; break; case 32 : case 48 : bit_alloc_index=(char (*)[][16])&t_alloc2; nbal=(char (*)[])&t_nbal2; sblimit=8; break; default : ErrorLog("Internal error in MPEG decoder (%s:%d).", __FILE__, __LINE__); } break; case 1 : switch (bitrate) /* 1 = 48 kHz */ { case 56 : case 64 : case 80 : case 96 : case 112 : case 128 : case 160 : case 192 : bit_alloc_index=(char (*)[][16])&t_alloc0; nbal=(char (*)[])&t_nbal0; sblimit=27; break; case 32 : case 48 : bit_alloc_index=(char (*)[][16])&t_alloc2; nbal=(char (*)[])&t_nbal2; sblimit=8; break; default : ErrorLog("Internal error in MPEG decoder (%s:%d).", __FILE__, __LINE__); } break; case 2 : switch (bitrate) /* 2 = 32 kHz */ { case 56 : case 64 : case 80 : bit_alloc_index=(char (*)[][16])&t_alloc0; nbal=(char (*)[])&t_nbal0; sblimit=27; break; case 96 : case 112 : case 128 : case 160 : case 192 : bit_alloc_index=(char (*)[][16])&t_alloc1; nbal=(char (*)[])&t_nbal1; sblimit=30; break; case 32 : case 48 : bit_alloc_index=(char (*)[][16])&t_alloc3; nbal=(char (*)[])&t_nbal3; sblimit=12; break; default : ErrorLog("Internal error in MPEG decoder (%s:%d).", __FILE__, __LINE__); } break; default : ErrorLog("Internal error in MPEG decoder (%s:%d).", __FILE__, __LINE__); // sampling frequency no good } else { bit_alloc_index=(char (*)[][16])&t_allocMPG2; nbal=(char (*)[])&t_nbalMPG2; sblimit=30; } /* * bit allocation per subband per channel decoding ***************************** */ if (bound==32) bound=sblimit; /* bound=32 means there is no intensity stereo */ for (sb=0;sb>2]; } } } else for (s=0;s<3;s++) subband_sample[ch][sb][3*gr+s]=0; /* * joint stereo ******************************************** */ for (sb=bound;sb>2]; } } } else for (s=0;s<3;s++) { subband_sample[0][sb][3*gr+s]=0; subband_sample[1][sb][3*gr+s]=0; } /* * the rest ******************************************* */ for (sb=sblimit;sb<32;sb++) for (ch=0;chmode!=3) { for (ch=0;chmode!=3) nch=2; return 0; } /****************************************************************************/ /****************************************************************************/ void convert_samplecode(unsigned int samplecode,unsigned int nlevels,unsigned short* sb_sample_buf) { int i; for (i=0;i<3;i++) { *sb_sample_buf=samplecode%nlevels; samplecode=samplecode/nlevels; sb_sample_buf++; } } float requantize_sample(unsigned short s4,unsigned short nlevels,float c,float d,float factor) { register float s,s2,s3; s3=(float) (-1.0+s4*2.0/(nlevels+1)); s2=c*(s3+d); s=factor*s2; return s; }