home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Applications / Musique / WarpAMP.lha / warpamp-ahi / getdata.c < prev    next >
C/C++ Source or Header  |  1997-05-31  |  7KB  |  231 lines

  1. /* this file is a part of amp software, (C) tomislav uzelac 1996,1997
  2. */
  3.  
  4. /* getdata.c  scalefactor & huffman data extraction
  5.  *
  6.  * Created by: tomislav uzelac  Apr 1996
  7.  * Last modified by: tomislav uzelac  Feb 27 1997
  8.  */
  9. #include "amp.h"
  10. #include "audio.h"
  11. #include "getbits.h"
  12. #include "huffman.h"
  13.  
  14. #define        GETDATA
  15. #include "getdata.h"
  16.  
  17. /* layer3 scalefactor decoding. should we check for the number
  18.  * of bits read, just in case?
  19.  */
  20. int decode_scalefactors(struct SIDE_INFO *info,struct AUDIO_HEADER *header,int gr,int ch)
  21. {
  22. int sfb,window;
  23. int slen1,slen2;
  24. int i1,i2,i=0;
  25. int j,k;
  26.     if (header->ID==1) {
  27.     /* this is MPEG-1 scalefactors format, quite different than 
  28.      * the MPEG-2 format. 
  29.      */
  30.         slen1=t_slen1[info->scalefac_compress[gr][ch]];
  31.         slen2=t_slen2[info->scalefac_compress[gr][ch]];
  32.         i1=3*slen1;
  33.         i2=3*slen2;
  34.         
  35.         if (info->window_switching_flag[gr][ch] && info->block_type[gr][ch]==2) {
  36.             if (info->mixed_block_flag[gr][ch]) {
  37.                 for (sfb=0;sfb<8;sfb++) {
  38.                     scalefac_l[gr][ch][sfb]=getbits(slen1);    
  39.                     i+=slen1;
  40.                 }
  41.                 for (sfb=3;sfb<6;sfb++) {
  42.                     for (window=0;window<3;window++)
  43.                         scalefac_s[gr][ch][sfb][window]=getbits(slen1);
  44.                     i+=i1;
  45.                 }
  46.                 for (;sfb<12;sfb++) {
  47.                     for (window=0;window<3;window++)
  48.                         scalefac_s[gr][ch][sfb][window]=getbits(slen2);
  49.                     i+=i2;
  50.                 }
  51.             } else { /* !mixed_block_flag */
  52.                 for (sfb=0;sfb<6;sfb++) {
  53.                     for (window=0;window<3;window++)
  54.                         scalefac_s[gr][ch][sfb][window]=getbits(slen1);
  55.                     i+=i1;
  56.                 }    
  57.                 for (;sfb<12;sfb++) {
  58.                     for (window=0;window<3;window++)
  59.                         scalefac_s[gr][ch][sfb][window]=getbits(slen2);
  60.                     i+=i2;
  61.                 }    
  62.             }    
  63.             for (window=0;window<3;window++) 
  64.                 scalefac_s[gr][ch][12][window]=0;
  65.         } else { /* block_type!=2 */
  66.             if ( !info->scfsi[ch][0] || !gr )
  67.                 for (sfb=0;sfb<6;sfb++) {
  68.                     scalefac_l[gr][ch][sfb]=getbits(slen1);
  69.                     i+=slen1;
  70.                 }    
  71.             else for (sfb=0;sfb<6;sfb++) {
  72.                 scalefac_l[1][ch][sfb]=scalefac_l[0][ch][sfb];
  73.             }    
  74.             if ( !info->scfsi[ch][1] || !gr )
  75.                 for (sfb=6;sfb<11;sfb++) {
  76.                     scalefac_l[gr][ch][sfb]=getbits(slen1);
  77.                     i+=slen1;
  78.                 }
  79.             else for (sfb=6;sfb<11;sfb++) {
  80.                 scalefac_l[1][ch][sfb]=scalefac_l[0][ch][sfb];                
  81.             }
  82.             if ( !info->scfsi[ch][2] || !gr )
  83.                 for (sfb=11;sfb<16;sfb++) {
  84.                     scalefac_l[gr][ch][sfb]=getbits(slen2);
  85.                     i+=slen2;
  86.                 }
  87.             else for (sfb=11;sfb<16;sfb++) {
  88.                 scalefac_l[1][ch][sfb]=scalefac_l[0][ch][sfb];                
  89.             }    
  90.             if ( !info->scfsi[ch][3] || !gr )
  91.                 for (sfb=16;sfb<21;sfb++) {
  92.                     scalefac_l[gr][ch][sfb]=getbits(slen2);
  93.                     i+=slen2;
  94.                 }    
  95.             else for (sfb=16;sfb<21;sfb++) {
  96.                 scalefac_l[1][ch][sfb]=scalefac_l[0][ch][sfb];
  97.             }
  98.             scalefac_l[gr][ch][21]=0;
  99.         }
  100.     } else { /* ID==0 */
  101.         int index=0,index2,spooky_index;
  102.         int slen[5],nr_of_sfb[5]; /* actually, there's four of each, not five, labelled 1 through 4, but
  103.                        * what's a word of storage compared to one's sanity. so [0] is irellevant.
  104.                        */
  105.  
  106.         /* ok, so we got 3 indexes. 
  107.          * spooky_index - indicates whether we use the normal set of slen eqs and nr_of_sfb tables
  108.          *                or the one for the right channel of intensity stereo coded frame
  109.          * index        - corresponds to the value of scalefac_compress, as listed in the standard
  110.          * index2    - 0 for long blocks, 1 for short wo/ mixed_block_flag, 2 for short with it
  111.          */
  112.         if ( (header->mode_extension==1 || header->mode_extension==3) && ch==1) { /* right ch... */
  113.             int int_scalefac_compress=info->scalefac_compress[0][ch]>>1;
  114.             intensity_scale=info->scalefac_compress[0][1]&1;
  115.             spooky_index=1;
  116.             if (int_scalefac_compress < 180) {
  117.                 slen[1]=int_scalefac_compress/36;
  118.                 slen[2]=(int_scalefac_compress%36)/6;
  119.                 slen[3]=(int_scalefac_compress%36)%6;
  120.                 slen[4]=0;
  121.                 info->preflag[0][ch]=0;
  122.                 index=0;
  123.             }
  124.             if ( 180 <= int_scalefac_compress && int_scalefac_compress < 244) {
  125.                 slen[1]=((int_scalefac_compress-180)%64)>>4;
  126.                 slen[2]=((int_scalefac_compress-180)%16)>>2;
  127.                 slen[3]=(int_scalefac_compress-180)%4;
  128.                 slen[4]=0;
  129.                 info->preflag[0][ch]=0;
  130.                 index=1;
  131.             }
  132.             if ( 244 <= int_scalefac_compress && int_scalefac_compress < 255) {
  133.                 slen[1]=(int_scalefac_compress-244)/3;
  134.                 slen[2]=(int_scalefac_compress-244)%3;
  135.                 slen[3]=0;
  136.                 slen[4]=0;
  137.                 info->preflag[0][ch]=0;
  138.                 index=2;
  139.             }
  140.         } else { /* the usual */
  141.             spooky_index=0;
  142.             if (info->scalefac_compress[0][ch] < 400) {
  143.                 slen[1]=(info->scalefac_compress[0][ch]>>4)/5;
  144.                 slen[2]=(info->scalefac_compress[0][ch]>>4)%5;
  145.                 slen[3]=(info->scalefac_compress[0][ch]%16)>>2;
  146.                 slen[4]=info->scalefac_compress[0][ch]%4;
  147.                 info->preflag[0][ch]=0;
  148.                 index=0;
  149.             }
  150.             if (info->scalefac_compress[0][ch] >= 400 && info->scalefac_compress[0][ch] < 500) {
  151.                 slen[1]=((info->scalefac_compress[0][ch]-400)>>2)/5;
  152.                 slen[2]=((info->scalefac_compress[0][ch]-400)>>2)%5;
  153.                 slen[3]=(info->scalefac_compress[0][ch]-400)%4;
  154.                 slen[4]=0;
  155.                 info->preflag[0][ch]=0;
  156.                 index=1;
  157.             } 
  158.             if (info->scalefac_compress[0][ch] >= 500 && info->scalefac_compress[0][ch] < 512) {
  159.                 slen[1]=(info->scalefac_compress[0][ch]-500)/3;
  160.                 slen[2]=(info->scalefac_compress[0][ch]-500)%3;
  161.                 slen[3]=0;
  162.                 slen[4]=0;
  163.                 info->preflag[0][ch]=1;
  164.                 index=2;
  165.             }
  166.         }
  167.  
  168.         if (info->window_switching_flag[0][ch] && info->block_type[0][ch]==2)
  169.             if (info->mixed_block_flag[0][ch]) index2=2;
  170.             else index2=1;
  171.         else index2=0;
  172.  
  173.         for (j=1;j<=4;j++) nr_of_sfb[j]=spooky_table[spooky_index][index][index2][j-1];
  174.  
  175.     /* now we'll do some actual scalefactor extraction, and a little more.
  176.      * for each scalefactor band we'll set the value of is_max to indicate
  177.      * illegal is_pos, since with MPEG2 it's not 'hardcoded' to 7.
  178.      */
  179.         if (!info->window_switching_flag[0][ch] || (info->window_switching_flag[0][ch] && info->block_type[0][ch]!=2)) {
  180.             sfb=0;
  181.             for (j=1;j<=4;j++) 
  182.                 for (k=0;k<nr_of_sfb[j];k++) {
  183.                     scalefac_l[0][ch][sfb]=getbits(slen[j]);
  184.                     i+=slen[j];
  185.                     if (ch) is_max[sfb]=(1<<slen[j])-1;
  186.                     sfb++;
  187.                 }
  188.         } else if (info->block_type[0][ch]==2)
  189.             if (!info->mixed_block_flag[0][ch]) {
  190.                 sfb=0;
  191.                 for (j=1;j<=4;j++)
  192.                     for (k=0;k<nr_of_sfb[j];k+=3) {
  193.                         /* we assume here that nr_of_sfb is divisible by 3. it is.
  194.                          */
  195.                         scalefac_s[0][ch][sfb][0]=getbits(slen[j]);
  196.                         scalefac_s[0][ch][sfb][1]=getbits(slen[j]);
  197.                         scalefac_s[0][ch][sfb][2]=getbits(slen[j]);
  198.                         i+=3*slen[j];
  199.                         if (ch) is_max[sfb+6]=(1<<slen[j])-1;
  200.                         sfb++;
  201.                     }
  202.             } else {
  203.                 /* what we do here is:
  204.                  * 1. assume that for every fs, the two lowest subbands are equal to the
  205.                  *    six lowest scalefactor bands for long blocks/MPEG2. they are.
  206.                  * 2. assume that for every fs, the two lowest subbands are equal to the
  207.                  *    three lowest scalefactor bands for short blocks. they are.
  208.                  */
  209.                 sfb=0;
  210.                 for (k=0;k<6;k++) {
  211.                     scalefac_l[0][ch][sfb]=getbits(slen[1]);
  212.                     i+=slen[j];
  213.                     if (ch) is_max[sfb]=(1<<slen[1])-1;
  214.                     sfb++;
  215.                 }
  216.                 nr_of_sfb[1]-=6;
  217.                 sfb=3;
  218.                 for (j=1;j<=4;j++)
  219.                     for (k=0;k<nr_of_sfb[j];k+=3) {
  220.                         scalefac_s[0][ch][sfb][0]=getbits(slen[j]);
  221.                         scalefac_s[0][ch][sfb][1]=getbits(slen[j]);
  222.                         scalefac_s[0][ch][sfb][2]=getbits(slen[j]);
  223.                         i+=3*slen[j];
  224.                         if (ch) is_max[sfb+6]=(1<<slen[j])-1;
  225.                         sfb++;
  226.                     }
  227.             } 
  228.     }
  229. return i;
  230. }
  231.