home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / sound / warpamp-ahi / layer2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-09  |  12.7 KB  |  323 lines

  1. /* this file is a part of amp software, (C) tomislav uzelac 1996,1997
  2. */
  3.  
  4. /* layer2.c   MPEG audio layer2 support
  5.  *
  6.  * Created by: Tomislav Uzelac  Mar 1996
  7.  * merged with amp, May 19 1997
  8.  */
  9. #include "amp.h"
  10. #include "audio.h"
  11. #include "getbits.h"
  12. #include "transform.h"
  13.  
  14. #define LAYER2
  15. #include "layer2.h"
  16.  
  17. extern int output;
  18.  
  19. int layer2_frame(struct AUDIO_HEADER *header,int cnt)
  20. {
  21. int i,s,sb,ch,gr,bitrate,bound;
  22. char (*nbal)[],(*bit_alloc_index)[][16];
  23. unsigned char allocation[2][32];
  24. unsigned char scfsi[2][32];
  25. float scalefactor[2][32][3];
  26. float subband_sample[2][32][36];
  27. int sblimit,nlevels,grouping; 
  28.  
  29. float  c,d;
  30. int no_of_bits,mpi;                             
  31. unsigned short sb_sample_buf[3];        
  32.  
  33. int hsize,fs,mean_frame_size;
  34.  
  35.         hsize=4;
  36.         if (header->protection_bit==0) hsize+=2;
  37.  
  38.         bitrate=t_bitrate[header->ID][3-header->layer][header->bitrate_index];
  39.         fs=t_sampling_frequency[header->ID][header->sampling_frequency];
  40.         if (header->ID) mean_frame_size=144000*bitrate/fs;
  41.         else mean_frame_size=72000*bitrate/fs;
  42.  
  43.         /* layers 1 and 2 do not have a 'bit reservoir'
  44.          */
  45.         append=data=0;
  46.  
  47.         fillbfr(mean_frame_size + header->padding_bit - hsize);
  48.  
  49.         switch (header->mode)
  50.                 {
  51.                 case 0 : 
  52.                 case 2 : nch=2; bound=32; bitrate=bitrate/2;  
  53.                         break;
  54.                 case 3 : nch=1; bound=32; 
  55.                         break;
  56.                 case 1 : nch=2; bitrate=bitrate/2; bound=(header->mode_extension+1)*4; 
  57.                 }
  58.                 
  59.         if (header->ID==1) switch (header->sampling_frequency) {
  60.                 case 0 : switch (bitrate)       /* 0 = 44.1 kHz */
  61.                                 {
  62.                                 case 56  :
  63.                                 case 64  :
  64.                                 case 80  : bit_alloc_index=(void *)&t_alloc0;
  65.                                            nbal=(void *)&t_nbal0;
  66.                                            sblimit=27;
  67.                                            break;
  68.                                 case 96  :
  69.                                 case 112 :
  70.                                 case 128 :
  71.                                 case 160 :
  72.                                 case 192 : bit_alloc_index=(void *)&t_alloc1;
  73.                                            nbal=(void *)&t_nbal1;
  74.                                            sblimit=30;  
  75.                                            break;
  76.                                 case 32  :
  77.                                 case 48  : bit_alloc_index=(void *)&t_alloc2;
  78.                                            nbal=(void *)&t_nbal2;
  79.                                            sblimit=8;
  80.                                            break;
  81.                                 default  : if (output) printf(" bit alloc info no gud ");
  82.                                 }
  83.                                 break;
  84.                 case 1 : switch (bitrate)       /* 1 = 48 kHz */
  85.                                 {
  86.                                 case 56  : 
  87.                                 case 64  :
  88.                                 case 80  :
  89.                                 case 96  :
  90.                                 case 112 :
  91.                                 case 128 :
  92.                                 case 160 :
  93.                                 case 192 : bit_alloc_index=(void *)&t_alloc0;
  94.                                            nbal=(void *)&t_nbal0;
  95.                                            sblimit=27;
  96.                                            break;
  97.                                 case 32  :
  98.                                 case 48  : bit_alloc_index=(void *)&t_alloc2;
  99.                                            nbal=(void *)&t_nbal2;
  100.                                            sblimit=8;
  101.                                            break;
  102.                                 default  : if (output) printf(" bit alloc info no gud ");
  103.                                 }
  104.                                 break;
  105.                 case 2 : switch (bitrate)       /* 2 = 32 kHz */
  106.                                 {
  107.                         case 56  :
  108.                         case 64  :
  109.                         case 80  : bit_alloc_index=(void *)&t_alloc0;
  110.                                    nbal=(void *)&t_nbal0;
  111.                                    sblimit=27;
  112.                                    break;
  113.                         case 96  :
  114.                         case 112 :
  115.                         case 128 :
  116.                         case 160 :
  117.                         case 192 : bit_alloc_index=(void *)&t_alloc1;
  118.                                    nbal=(void *)&t_nbal1;
  119.                                    sblimit=30;
  120.                                    break;
  121.                         case 32  :
  122.                         case 48  : bit_alloc_index=(void *)&t_alloc3;
  123.                                    nbal=(void *)&t_nbal3;
  124.                                    sblimit=12;
  125.                                    break;
  126.                         default  : if (output) printf("bit alloc info not ok\n");
  127.                         }
  128.                         break;                                                    
  129.                 default  : if (output) printf("sampling freq. not ok/n");
  130.         } else {
  131.                 bit_alloc_index=(void *)&t_allocMPG2;
  132.                 nbal=(void *)&t_nbalMPG2;
  133.                 sblimit=30;
  134.         }
  135.  
  136. /*
  137.  * bit allocation per subband per channel decoding *****************************
  138.  */
  139.  
  140.         if (bound==32) bound=sblimit;   /* bound=32 means there is no intensity stereo */
  141.          
  142.         for (sb=0;sb<bound;sb++)
  143.                 for (ch=0;ch<nch;ch++)
  144.                         allocation[ch][sb]=getbits((*nbal)[sb]);
  145.  
  146.         for (sb=bound;sb<sblimit;sb++)
  147.                 allocation[1][sb] = allocation[0][sb] = getbits((*nbal)[sb]);
  148.  
  149.  
  150. /*
  151.  * scfsi ***********************************************************************
  152.  */
  153.  
  154.         for (sb=0;sb<sblimit;sb++)
  155.                 for (ch=0;ch<nch;ch++)
  156.                         if (allocation[ch][sb]!=0) scfsi[ch][sb]=getbits(2);
  157.                         else scfsi[ch][sb]=0;
  158.  
  159. /*
  160.  * scalefactors ****************************************************************
  161.  */
  162.  
  163.         for (sb=0;sb<sblimit;sb++)
  164.         for (ch=0;ch<nch;ch++)
  165.                 if (allocation[ch][sb]!=0) {
  166.                         scalefactor[ch][sb][0]=t_scalefactor[getbits(6)];
  167.                         switch (scfsi[ch][sb])
  168.                         {
  169.                         case 0: scalefactor[ch][sb][1]=t_scalefactor[getbits(6)];
  170.                                 scalefactor[ch][sb][2]=t_scalefactor[getbits(6)];
  171.                                 break;
  172.                         case 1: scalefactor[ch][sb][2]=t_scalefactor[getbits(6)];
  173.                                 scalefactor[ch][sb][1]=scalefactor[ch][sb][0];
  174.                                 break;
  175.                         case 2: scalefactor[ch][sb][1]=scalefactor[ch][sb][0];
  176.                                 scalefactor[ch][sb][2]=scalefactor[ch][sb][0];
  177.                                 break;
  178.                         case 3: scalefactor[ch][sb][2]=t_scalefactor[getbits(6)];
  179.                                 scalefactor[ch][sb][1]=scalefactor[ch][sb][2];
  180.                         }                       
  181.                 } 
  182.                 else scalefactor[ch][sb][0]=scalefactor[ch][sb][1]=\
  183.                         scalefactor[ch][sb][2]=0.0;
  184.  
  185.  
  186. /*
  187.  * samples *********************************************************************
  188.  */
  189.  
  190.         for (gr=0;gr<12;gr++) {
  191.                 /*
  192.                  * normal ********************************
  193.                  */
  194.         
  195.                 for (sb=0;sb<bound;sb++)
  196.                 for (ch=0;ch<nch;ch++)
  197.                 if (allocation[ch][sb]!=0) {
  198.                         mpi=(*bit_alloc_index)[sb][allocation[ch][sb]]; 
  199.                         no_of_bits=t_bpc[mpi];
  200.                         c=t_c[mpi];
  201.                         d=t_d[mpi];
  202.                         grouping=t_grouping[mpi];
  203.                         nlevels=t_nlevels[mpi];
  204.  
  205.                         if (grouping) {
  206.                                 int samplecode=getbits(no_of_bits);
  207.                                 convert_samplecode(samplecode,grouping,sb_sample_buf);
  208.  
  209.                                 for (s=0;s<3;s++)
  210.                                         subband_sample[ch][sb][3*gr+s]=requantize_sample (sb_sample_buf[s],nlevels,c,d,scalefactor[ch][sb][gr/4]);
  211.                         } else {
  212.                                 for (s=0;s<3;s++) sb_sample_buf[s]=getbits(no_of_bits);
  213.                                 
  214.                                 for (s=0;s<3;s++) { 
  215.                                         /*subband_sample[ch][sb][3*gr+s]=requantize_sample (sb_sample_buf[s],nlevels,c,d,scalefactor[ch][sb][gr/4]);*/
  216.                                         subband_sample[ch][sb][3*gr+s]=(t_dd[mpi]+sb_sample_buf[s]*t_nli[mpi])*c*scalefactor[ch][sb][gr>>2];
  217.                                 }
  218.                         }
  219.                 } else 
  220.                         for (s=0;s<3;s++) subband_sample[ch][sb][3*gr+s]=0;
  221.  
  222.  
  223.                 /*
  224.                  * joint stereo ********************************************
  225.                  */
  226.       
  227.                 for (sb=bound;sb<sblimit;sb++)
  228.                 if (allocation[0][sb]!=0) {
  229.                         /*ispravka!
  230.                         */
  231.                         mpi=(*bit_alloc_index)[sb][allocation[0][sb]];  
  232.                         no_of_bits=t_bpc[mpi];
  233.                         c=t_c[mpi];
  234.                         d=t_d[mpi];
  235.                         grouping=t_grouping[mpi];
  236.                         nlevels=t_nlevels[mpi]; 
  237.                    
  238.                         if (grouping) {
  239.                                 int samplecode=getbits(no_of_bits);
  240.                                 convert_samplecode(samplecode,grouping,sb_sample_buf);
  241.  
  242.                                 for (s=0;s<3;s++) {
  243.                                         subband_sample[0][sb][3*gr+s]=requantize_sample (sb_sample_buf[s],nlevels,c,d,scalefactor[0][sb][gr/4]);
  244.                                         subband_sample[1][sb][3*gr+s]=subband_sample[0][sb][3*gr+s];
  245.                                 }
  246.                         } else {
  247.                                 for (s=0;s<3;s++) sb_sample_buf[s]=getbits(no_of_bits);
  248.  
  249.                                 for (s=0;s<3;s++) { 
  250.                                         subband_sample[0][sb][3*gr+s]=subband_sample[1][sb][3*gr+s]=\
  251.                                                 (t_dd[mpi]+sb_sample_buf[s]*t_nli[mpi])*c*scalefactor[0][sb][gr>>2];
  252.                                 }
  253.                         }
  254.  
  255.                 } else for (s=0;s<3;s++) {
  256.                         subband_sample[0][sb][3*gr+s]=0;
  257.                         subband_sample[1][sb][3*gr+s]=0;
  258.                 }
  259.  
  260.                 /*
  261.                  * the rest *******************************************
  262.                  */
  263.                 for (sb=sblimit;sb<32;sb++)
  264.                         for (ch=0;ch<nch;ch++)
  265.  
  266.                                 for (s=0;s<3;s++) subband_sample[ch][sb][3*gr+s]=0;
  267.         }       
  268.  
  269.         /*
  270.          * this is, in fact, horrible, but I had to adjust it to amp/mp3. The hack to make downmixing
  271.          * work is as ugly as possible.
  272.          */
  273.  
  274.         if (A_DOWNMIX && header->mode!=3) {
  275.                 for (ch=0;ch<nch;ch++) 
  276.                         for (sb=0;sb<32;sb++)
  277.                                 for (i=0;i<36;i++)
  278.                                         subband_sample[0][sb][i]=(subband_sample[0][sb][i]+subband_sample[1][sb][i])*0.5f;
  279.                 nch=1;
  280.         }
  281.  
  282.         for (ch=0;ch<nch;ch++) {
  283.                 for (sb=0;sb<32;sb++) 
  284.                         for (i=0;i<18;i++) res[sb][i]=subband_sample[ch][sb][i]; 
  285.                 for (i=0;i<18;i++)
  286.                         poly(ch,i);
  287.         }
  288.         printout();
  289.         for (ch=0;ch<nch;ch++) {
  290.                 for (sb=0;sb<32;sb++)
  291.                         for (i=0;i<18;i++) res[sb][i]=subband_sample[ch][sb][i+18];
  292.                 for (i=0;i<18;i++)
  293.                         poly(ch,i);
  294.         }
  295.         printout();
  296.  
  297.         if (A_DOWNMIX && header->mode!=3) nch=2;
  298.  
  299.         return 0;
  300. }                        
  301. /****************************************************************************/  
  302. /****************************************************************************/
  303.  
  304. void convert_samplecode(unsigned int samplecode,unsigned int nlevels,unsigned short* sb_sample_buf)
  305. {
  306. int i;
  307.  
  308.         for (i=0;i<3;i++) {
  309.                 *sb_sample_buf=samplecode%nlevels;
  310.                 samplecode=samplecode/nlevels;
  311.                 sb_sample_buf++;
  312.         }
  313. }
  314.  
  315. float requantize_sample(unsigned short s4,unsigned short nlevels,float c,float d,float factor)
  316. {
  317. register float s,s2,s3;
  318. s3=-1.0+s4*2.0/(nlevels+1);
  319. s2=c*(s3+d);
  320. s=factor*s2;
  321. return s;
  322.