home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / dos / ampeg43 / source / decodsrc / decode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  56.5 KB  |  1,614 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. decode.c
  4. **********************************************************************/
  5. /**********************************************************************
  6.  * MPEG/audio coding/decoding software, work in progress              *
  7.  *   NOT for public distribution until verified and approved by the   *
  8.  *   MPEG/audio committee.  For further information, please contact   *
  9.  *   Davis Pan, 708-538-5671, e-mail: pan@ukraine.corp.mot.com        *
  10.  *                                                                    *
  11.  * VERSION 4.3                                                        *
  12.  *   changes made since last update:                                  *
  13.  *   date   programmers         comment                               *
  14.  * 2/25/91  Douglas Wong,       start of version 1.0 records          *
  15.  *          Davis Pan                                                 *
  16.  * 3/06/91  Douglas Wong        rename: setup.h to dedef.h            *
  17.  *                                      dfilter to defilter           *
  18.  *                                      dwindow to dewindow           *
  19.  *                              integrated "quantizer", "scalefactor" *
  20.  *                              combined window_samples routine into  *
  21.  *                              filter samples                        *
  22.  * 3/31/91  Bill Aspromonte     replaced read_filter by               *
  23.  *                              create_syn_filter and introduced a    *
  24.  *                              new Sub-Band Synthesis routine called *
  25.  *                              SubBandSynthesis()                    *
  26.  * 5/10/91  Vish (PRISM)        Ported to Macintosh and Unix.         *
  27.  *                              Changed "out_fifo()" so that last     *
  28.  *                              unfilled block is also written out.   *
  29.  *                              "create_syn_filter()" was modified so *
  30.  *                              that calculation precision is same as *
  31.  *                              in specification tables.              *
  32.  *                              Changed "decode_scale()" to reflect   *
  33.  *                              specifications.                       *
  34.  *                              Removed all routines used by          *
  35.  *                              "synchronize_buffer()".  This is now  *
  36.  *                              replaced by "seek_sync()".            *
  37.  *                              Incorporated Jean-Georges Fritsch's   *
  38.  *                              "bitstream.c" package.                *
  39.  *                              Deleted "reconstruct_sample()".       *
  40.  * 27jun91  dpwe (Aware)        Passed outFile and &sampFrames as     *
  41.  *                              args to out_fifo() - were global.     *
  42.  *                              Moved "alloc_*" reader to common.c.   *
  43.  *                              alloc, sblimit, stereo passed via new *
  44.  *                              'frame_params struct (were globals).  *
  45.  *                              Added JOINT STEREO decoding, lyrs I&II*
  46.  *                              Affects: decode_bitalloc,buffer_samps *
  47.  *                              Plus a few other cleanups.            *
  48.  * 6/10/91   Earle Jennings     conditional expansion added in        *
  49.  *                              II_dequantize_sample to handle range  *
  50.  *                              problems in MSDOS version             *
  51.  * 8/8/91    Jens Spille        Change for MS-C6.00                   *
  52.  *10/1/91    S.I. Sudharsanan,  Ported to IBM AIX platform.           *
  53.  *           Don H. Lee,                                              *
  54.  *           Peter W. Farrett                                         *
  55.  *10/3/91    Don H. Lee         implemented CRC-16 error protection   *
  56.  *                              newly introduced functions are        *
  57.  *                              buffer_CRC and recover_CRC_error.     *
  58.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  59.  *                              important fixes involved changing     *
  60.  *                              16-bit ints to long or unsigned in    *
  61.  *                              bit alloc routines for quant of 65535 *
  62.  *                              and passing proper function args.     *
  63.  *                              Removed "Other Joint Stereo" option   *
  64.  *                              and made bitrate be total channel     *
  65.  *                              bitrate, irrespective of the mode.    *
  66.  *                              Fixed many small bugs & reorganized.  *
  67.  * 7/27/92  Juan Pineda         Bug fix in SubBandSynthesis()         *
  68.  *--------------------------------------------------------------------*
  69.  * 6/14/92  Juan Pineda         Layer III decoding routines added.    *
  70.  *          Amit Gulati         Follows CD 3-11172 rev2.  Contains    *
  71.  *                              hacks deal with evolving available    *
  72.  *                              layerIII bitstreams.  Some (minor)    *
  73.  *                              modification of prior LI&II code.     *
  74.  * 10/25/92 Amit Gulati         Updated layerIII routines. Added code *
  75.  *                              for subblock_gain, switched block     *
  76.  *                              modes, stereo pre-processing.         *
  77.  *                              Corrected sign bits for huffman       *
  78.  *                              decoding of quadruples region and     *
  79.  *                              adjusted gain factor in III_dequant.  *
  80.  * 11/21/92 Amit Gulati         Several layerIII bugs fixed.          *
  81.  * 12/15/92 Amit Gulati         Corrected reordering (indexing)       *
  82.  *          Stan Searing        within IMDCT routine.                 *
  83.  *  8/24/93 Masahiro Iwadare    Included IS modification in Layer III.*
  84.  *                              Changed for 1 pass decoding.          *
  85.  *  9/07/93 Toshiyuki Ishino    Integrated Layer III with Ver 3.9.    *
  86.  *--------------------------------------------------------------------*
  87.  * 11/20/93 Masahiro Iwadare    Integrated Layer III with Ver 4.0.    *
  88.  *--------------------------------------------------------------------*
  89.  *  7/14/94 Juergen Koller      Bug fixes in Layer III code           *
  90.  *--------------------------------------------------------------------*
  91.  * 08/11/94 IIS                 Bug fixes in Layer III code           *
  92.  *--------------------------------------------------------------------*
  93.  * 9/20/94  Davis Pan           Modification to avoid premature       *
  94.  *                              synchword detection                   *
  95.  *--------------------------------------------------------------------*
  96.  * 11/09/94 Jon Rowlands        Merged premature synchword detection  *
  97.  *                              fix into layer III code version       *
  98.  **********************************************************************/
  99.  
  100. #include        "common.h"
  101. #include        "decoder.h"
  102. #include        "huffman.h"
  103.  
  104. /***************************************************************
  105. /*
  106. /* This module contains the core of the decoder ie all the
  107. /* computational routines. (Layer I and II only)
  108. /* Functions are common to both layer unless
  109. /* otherwise specified.
  110. /*
  111. /***************************************************************/
  112.  
  113. /*****************************************************************
  114. /*
  115. /* The following routines decode the system information
  116. /*
  117. /****************************************************************/
  118.  
  119. /************ Layer I, Layer II & Layer III ******************/
  120.  
  121. void decode_info(bs, fr_ps)
  122. Bit_stream_struc *bs;
  123. frame_params *fr_ps;
  124. {
  125.     unsigned int bits;
  126.  
  127.     layer *hdr = fr_ps->header;
  128.     while( (bits=getbits(bs,8)) == 255); /*discard leading 0xFF's of syncword*/
  129.     hdr->bitrate_index = bits & 0xF;
  130.     bits = bits >> 4;
  131.     switch(bits) {
  132.       case 0:
  133.          hdr->version = 0;
  134.          hdr->lay = 4;
  135.          hdr->error_protection = 1;
  136.          break;
  137.       case 1:
  138.          hdr->version = 0;
  139.          hdr->lay = 4;
  140.          hdr->error_protection = 0;
  141.          break;
  142.       case 2:
  143.          hdr->version = 0;
  144.          hdr->lay = 3;
  145.          hdr->error_protection = 1;
  146.          break;
  147.       case 3:
  148.          hdr->version = 0;
  149.          hdr->lay = 3;
  150.          hdr->error_protection = 0;
  151.          break;
  152.       case 4:
  153.          hdr->version = 0;
  154.          hdr->lay = 2;
  155.          hdr->error_protection = 1;
  156.          break;
  157.       case 5:
  158.          hdr->version = 0;
  159.          hdr->lay = 2;
  160.          hdr->error_protection = 0;
  161.          break;
  162.       case 6:
  163.          hdr->version = 0;
  164.          hdr->lay = 1;
  165.          hdr->error_protection = 1;
  166.          break;
  167.       case 7:
  168.          hdr->version = 0;
  169.          hdr->lay = 1;
  170.          hdr->error_protection = 0;
  171.          break;
  172.       case 8:
  173.          hdr->version = 1;
  174.          hdr->lay = 4;
  175.          hdr->error_protection = 1;
  176.          break;
  177.       case 9:
  178.          hdr->version = 1;
  179.          hdr->lay = 4;
  180.          hdr->error_protection = 0;
  181.          break;
  182.       case 10:
  183.          hdr->version = 1;
  184.          hdr->lay = 3;
  185.          hdr->error_protection = 1;
  186.          break;
  187.       case 11:
  188.          hdr->version = 1;
  189.          hdr->lay = 3;
  190.          hdr->error_protection = 0;
  191.          break;
  192.       case 12:
  193.          hdr->version = 1;
  194.          hdr->lay = 2;
  195.          hdr->error_protection = 1;
  196.          break;
  197.       case 13:
  198.          hdr->version = 1;
  199.          hdr->lay = 2;
  200.          hdr->error_protection = 0;
  201.          break;
  202.       case 14:
  203.          hdr->version = 1;
  204.          hdr->lay = 1;
  205.          hdr->error_protection = 1;
  206.          break;
  207.       default:
  208.          hdr->version = 1;
  209.          hdr->lay = 1;
  210.          hdr->error_protection = 0;
  211.     }
  212.     hdr->sampling_frequency = getbits(bs,2);
  213.     hdr->padding = get1bit(bs);
  214.     hdr->extension = get1bit(bs);
  215.     hdr->mode = getbits(bs,2);
  216.     hdr->mode_ext = getbits(bs,2);
  217.     hdr->copyright = get1bit(bs);
  218.     hdr->original = get1bit(bs);
  219.     hdr->emphasis = getbits(bs,2);
  220. }
  221.  
  222. /*******************************************************************
  223. /*
  224. /* The bit allocation information is decoded. Layer I
  225. /* has 4 bit per subband whereas Layer II is Ws and bit rate
  226. /* dependent.
  227. /*
  228. /********************************************************************/
  229.  
  230. /**************************** Layer II *************/
  231.  
  232. void II_decode_bitalloc(bs, bit_alloc, fr_ps)
  233. Bit_stream_struc *bs;
  234. unsigned int bit_alloc[2][SBLIMIT];
  235. frame_params *fr_ps;
  236. {
  237.     int i,j;
  238.     int stereo = fr_ps->stereo;
  239.     int sblimit = fr_ps->sblimit;
  240.     int jsbound = fr_ps->jsbound;
  241.     al_table *alloc = fr_ps->alloc;
  242.  
  243.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  244.         bit_alloc[j][i] = (char) getbits(bs,(*alloc)[i][0].bits);
  245.  
  246.     for (i=jsbound;i<sblimit;i++) /* expand to 2 channels */
  247.         bit_alloc[0][i] = bit_alloc[1][i] =
  248.             (char) getbits(bs,(*alloc)[i][0].bits);
  249.  
  250.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  251.         bit_alloc[j][i] = 0;
  252. }
  253.  
  254. /**************************** Layer I *************/
  255.  
  256. void I_decode_bitalloc(bs, bit_alloc, fr_ps)
  257. Bit_stream_struc *bs;
  258. unsigned int bit_alloc[2][SBLIMIT];
  259. frame_params *fr_ps;
  260. {
  261.     int i,j;
  262.     int stereo  = fr_ps->stereo;
  263.     int sblimit = fr_ps->sblimit;
  264.     int jsbound = fr_ps->jsbound;
  265.     int b;
  266.  
  267.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  268.         bit_alloc[j][i] = getbits(bs,4);
  269.     for (i=jsbound;i<SBLIMIT;i++) {
  270.         b = getbits(bs,4);
  271.         for (j=0;j<stereo;j++)
  272.             bit_alloc[j][i] = b;
  273.     }
  274. }
  275.  
  276. /*****************************************************************
  277. /*
  278. /* The following two functions implement the layer I and II
  279. /* format of scale factor extraction. Layer I involves reading
  280. /* 6 bit per subband as scale factor. Layer II requires reading
  281. /* first the scfsi which in turn indicate the number of scale factors
  282. /* transmitted.
  283. /*    Layer I : I_decode_scale
  284. /*   Layer II : II_decode_scale
  285. /*
  286. /****************************************************************/
  287.  
  288. /************************** Layer I stuff ************************/
  289.  
  290. void I_decode_scale(bs, bit_alloc, scale_index, fr_ps)
  291. Bit_stream_struc *bs;
  292. unsigned int bit_alloc[2][SBLIMIT], scale_index[2][3][SBLIMIT];
  293. frame_params *fr_ps;
  294. {
  295.     int i,j;
  296.     int stereo = fr_ps->stereo;
  297.     int sblimit = fr_ps->sblimit;
  298.  
  299.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  300.         if (!bit_alloc[j][i])
  301.             scale_index[j][0][i] = SCALE_RANGE-1;
  302.         else                    /* 6 bit per scale factor */
  303.             scale_index[j][0][i] =  getbits(bs,6);
  304.  
  305. }
  306.  
  307. /*************************** Layer II stuff ***************************/
  308.  
  309. void II_decode_scale(bs,scfsi, bit_alloc,scale_index, fr_ps)
  310. Bit_stream_struc *bs;
  311. unsigned int scfsi[2][SBLIMIT], bit_alloc[2][SBLIMIT],
  312.              scale_index[2][3][SBLIMIT];
  313. frame_params *fr_ps;
  314. {
  315.     int i,j;
  316.     int stereo = fr_ps->stereo;
  317.     int sblimit = fr_ps->sblimit;
  318.    
  319.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) /* 2 bit scfsi */
  320.         if (bit_alloc[j][i]) scfsi[j][i] = (char) getbits(bs,2);
  321.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++)   
  322.         scfsi[j][i] = 0;
  323.  
  324.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) {
  325.         if (bit_alloc[j][i])   
  326.             switch (scfsi[j][i]) {
  327.                 /* all three scale factors transmitted */
  328.              case 0 : scale_index[j][0][i] = getbits(bs,6);
  329.                 scale_index[j][1][i] = getbits(bs,6);
  330.                 scale_index[j][2][i] = getbits(bs,6);
  331.                 break;
  332.                 /* scale factor 1 & 3 transmitted */
  333.              case 1 : scale_index[j][0][i] =
  334.                  scale_index[j][1][i] = getbits(bs,6);
  335.                 scale_index[j][2][i] = getbits(bs,6);
  336.                 break;
  337.                 /* scale factor 1 & 2 transmitted */
  338.              case 3 : scale_index[j][0][i] = getbits(bs,6);
  339.                 scale_index[j][1][i] =
  340.                     scale_index[j][2][i] =  getbits(bs,6);
  341.                 break;
  342.                 /* only one scale factor transmitted */
  343.              case 2 : scale_index[j][0][i] =
  344.                  scale_index[j][1][i] =
  345.                      scale_index[j][2][i] = getbits(bs,6);
  346.                 break;
  347.                 default : break;
  348.             }
  349.         else {
  350.             scale_index[j][0][i] = scale_index[j][1][i] =
  351.                 scale_index[j][2][i] = SCALE_RANGE-1;
  352.         }
  353.     }
  354.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++) {
  355.         scale_index[j][0][i] = scale_index[j][1][i] =
  356.             scale_index[j][2][i] = SCALE_RANGE-1;
  357.     }
  358. }
  359.  
  360. /**************************************************************
  361. /*
  362. /*   The following two routines take care of reading the
  363. /* compressed sample from the bit stream for both layer 1 and
  364. /* layer 2. For layer 1, read the number of bits as indicated
  365. /* by the bit_alloc information. For layer 2, if grouping is
  366. /* indicated for a particular subband, then the sample size has
  367. /* to be read from the bits_group and the merged samples has
  368. /* to be decompose into the three distinct samples. Otherwise,
  369. /* it is the same for as layer one.
  370. /*
  371. /**************************************************************/
  372.  
  373. /******************************* Layer I stuff ******************/
  374.  
  375. void I_buffer_sample(bs, sample, bit_alloc, fr_ps)
  376. unsigned int FAR sample[2][3][SBLIMIT];
  377. unsigned int bit_alloc[2][SBLIMIT];
  378. Bit_stream_struc *bs;
  379. frame_params *fr_ps;
  380. {
  381.     int i,j,k;
  382.     int stereo = fr_ps->stereo;
  383.     int sblimit = fr_ps->sblimit;
  384.     int jsbound = fr_ps->jsbound;
  385.     unsigned int s;
  386.  
  387.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  388.         if ( (k = bit_alloc[j][i]) == 0)
  389.             sample[j][0][i] = 0;
  390.         else 
  391.             sample[j][0][i] = (unsigned int) getbits(bs,k+1);
  392.     for (i=jsbound;i<SBLIMIT;i++) {
  393.         if ( (k = bit_alloc[0][i]) == 0)
  394.             s = 0;
  395.         else 
  396.             s = (unsigned int)getbits(bs,k+1);
  397.         for (j=0;j<stereo;j++)
  398.             sample[j][0][i]    = s;
  399.     }
  400. }
  401.  
  402. /*************************** Layer II stuff ************************/
  403.  
  404. void II_buffer_sample(bs,sample,bit_alloc,fr_ps)
  405. unsigned int FAR sample[2][3][SBLIMIT];
  406. unsigned int bit_alloc[2][SBLIMIT];
  407. Bit_stream_struc *bs;
  408. frame_params *fr_ps;
  409. {
  410.     int i,j,k,m;
  411.     int stereo = fr_ps->stereo;
  412.     int sblimit = fr_ps->sblimit;
  413.     int jsbound = fr_ps->jsbound;
  414.     al_table *alloc = fr_ps->alloc;
  415.  
  416.     for (i=0;i<sblimit;i++) for (j=0;j<((i<jsbound)?stereo:1);j++) {
  417.         if (bit_alloc[j][i]) {
  418.             /* check for grouping in subband */
  419.             if ((*alloc)[i][bit_alloc[j][i]].group==3)
  420.                 for (m=0;m<3;m++) {
  421.                     k = (*alloc)[i][bit_alloc[j][i]].bits;
  422.                     sample[j][m][i] = (unsigned int) getbits(bs,k);
  423.                 }         
  424.             else {              /* bit_alloc = 3, 5, 9 */
  425.                 unsigned int nlevels, c=0;
  426.  
  427.                 nlevels = (*alloc)[i][bit_alloc[j][i]].steps;
  428.                 k=(*alloc)[i][bit_alloc[j][i]].bits;
  429.                 c = (unsigned int) getbits(bs, k);
  430.                 for (k=0;k<3;k++) {
  431.                     sample[j][k][i] = c % nlevels;
  432.                     c /= nlevels;
  433.                 }
  434.             }
  435.         }
  436.         else {                  /* for no sample transmitted */
  437.             for (k=0;k<3;k++) sample[j][k][i] = 0;
  438.         }
  439.         if(stereo == 2 && i>= jsbound) /* joint stereo : copy L to R */
  440.             for (k=0;k<3;k++) sample[1][k][i] = sample[0][k][i];
  441.     }
  442.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++) for (k=0;k<3;k++)
  443.         sample[j][k][i] = 0;
  444. }      
  445.  
  446. /**************************************************************
  447. /*
  448. /*   Restore the compressed sample to a factional number.
  449. /*   first complement the MSB of the sample
  450. /*    for layer I :
  451. /*    Use s = (s' + 2^(-nb+1) ) * 2^nb / (2^nb-1)
  452. /*   for Layer II :
  453. /*   Use the formula s = s' * c + d
  454. /*
  455. /**************************************************************/
  456.  
  457. static double c[17] = { 1.33333333333, 1.60000000000, 1.14285714286,
  458.                         1.77777777777, 1.06666666666, 1.03225806452,
  459.                         1.01587301587, 1.00787401575, 1.00392156863,
  460.                         1.00195694716, 1.00097751711, 1.00048851979,
  461.                         1.00024420024, 1.00012208522, 1.00006103888,
  462.                         1.00003051851, 1.00001525902 };
  463.  
  464. static double d[17] = { 0.500000000, 0.500000000, 0.250000000, 0.500000000,
  465.                         0.125000000, 0.062500000, 0.031250000, 0.015625000,
  466.                         0.007812500, 0.003906250, 0.001953125, 0.0009765625,
  467.                         0.00048828125, 0.00024414063, 0.00012207031,
  468.                         0.00006103516, 0.00003051758 };
  469.  
  470. /************************** Layer II stuff ************************/
  471.  
  472. void II_dequantize_sample(sample, bit_alloc, fraction, fr_ps)
  473. unsigned int FAR sample[2][3][SBLIMIT];
  474. unsigned int bit_alloc[2][SBLIMIT];
  475. double FAR fraction[2][3][SBLIMIT];
  476. frame_params *fr_ps;
  477. {
  478.     int i, j, k, x;
  479.     int stereo = fr_ps->stereo;
  480.     int sblimit = fr_ps->sblimit;
  481.     al_table *alloc = fr_ps->alloc;
  482.  
  483.     for (i=0;i<sblimit;i++)  for (j=0;j<3;j++) for (k=0;k<stereo;k++)
  484.         if (bit_alloc[k][i]) {
  485.  
  486.             /* locate MSB in the sample */
  487.             x = 0;
  488. #ifndef MS_DOS
  489.             while ((1L<<x) < (*alloc)[i][bit_alloc[k][i]].steps) x++;
  490. #else
  491.             /* microsoft C thinks an int is a short */
  492.             while (( (unsigned long) (1L<<(long)x) <
  493.                     (unsigned long)( (*alloc)[i][bit_alloc[k][i]].steps)
  494.                     ) && ( x < 16) ) x++;
  495. #endif
  496.  
  497.             /* MSB inversion */
  498.             if (((sample[k][j][i] >> x-1) & 1) == 1)
  499.                 fraction[k][j][i] = 0.0;
  500.             else  fraction[k][j][i] = -1.0;
  501.  
  502.             /* Form a 2's complement sample */
  503.             fraction[k][j][i] += (double) (sample[k][j][i] & ((1<<x-1)-1)) /
  504.                 (double) (1L<<x-1);
  505.  
  506.             /* Dequantize the sample */
  507.             fraction[k][j][i] += d[(*alloc)[i][bit_alloc[k][i]].quant];
  508.             fraction[k][j][i] *= c[(*alloc)[i][bit_alloc[k][i]].quant];
  509.         }
  510.         else fraction[k][j][i] = 0.0;   
  511.    
  512.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<3;j++) for(k=0;k<stereo;k++)
  513.         fraction[k][j][i] = 0.0;
  514. }
  515.  
  516. /***************************** Layer I stuff ***********************/
  517.  
  518. void I_dequantize_sample(sample, fraction, bit_alloc, fr_ps)
  519. unsigned int FAR sample[2][3][SBLIMIT];
  520. unsigned int bit_alloc[2][SBLIMIT];
  521. double FAR fraction[2][3][SBLIMIT];
  522. frame_params *fr_ps;
  523. {
  524.     int i, nb, k;
  525.     int stereo = fr_ps->stereo;
  526.     int sblimit = fr_ps->sblimit;
  527.  
  528.     for (i=0;i<SBLIMIT;i++)
  529.         for (k=0;k<stereo;k++)
  530.             if (bit_alloc[k][i]) {
  531.                 nb = bit_alloc[k][i] + 1;
  532.                 if (((sample[k][0][i] >> nb-1) & 1) == 1) fraction[k][0][i] = 0.0;
  533.                 else fraction[k][0][i] = -1.0;
  534.                 fraction[k][0][i] += (double) (sample[k][0][i] & ((1<<nb-1)-1)) /
  535.                     (double) (1L<<nb-1);
  536.  
  537.                 fraction[k][0][i] =
  538.                     (double) (fraction[k][0][i]+1.0/(double)(1L<<nb-1)) *
  539.                         (double) (1L<<nb) / (double) ((1L<<nb)-1);
  540.             }
  541.             else fraction[k][0][i] = 0.0;
  542. }
  543.  
  544. /************************************************************
  545. /*
  546. /*   Restore the original value of the sample ie multiply
  547. /*    the fraction value by its scalefactor.
  548. /*
  549. /************************************************************/
  550.  
  551. /************************* Layer II Stuff **********************/
  552.  
  553. void II_denormalize_sample(fraction, scale_index,fr_ps,x)
  554. double FAR fraction[2][3][SBLIMIT];
  555. unsigned int scale_index[2][3][SBLIMIT];
  556. frame_params *fr_ps;
  557. int x;
  558. {
  559.     int i,j,k;
  560.     int stereo = fr_ps->stereo;
  561.     int sblimit = fr_ps->sblimit;
  562.  
  563.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) {
  564.         fraction[j][0][i] *= multiple[scale_index[j][x][i]];
  565.         fraction[j][1][i] *= multiple[scale_index[j][x][i]];
  566.         fraction[j][2][i] *= multiple[scale_index[j][x][i]];
  567.     }
  568. }
  569.  
  570. /**************************** Layer I stuff ******************************/
  571.  
  572. void I_denormalize_sample(fraction,scale_index,fr_ps)
  573. double FAR fraction[2][3][SBLIMIT];
  574. unsigned int scale_index[2][3][SBLIMIT];
  575. frame_params *fr_ps;
  576. {
  577.     int i,j,k;
  578.     int stereo = fr_ps->stereo;
  579.     int sblimit = fr_ps->sblimit;
  580.  
  581.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  582.         fraction[j][0][i] *= multiple[scale_index[j][0][i]];
  583. }
  584.  
  585. /*****************************************************************
  586. /*
  587. /* The following are the subband synthesis routines. They apply
  588. /* to both layer I and layer II stereo or mono. The user has to
  589. /* decide what parameters are to be passed to the routines.
  590. /*
  591. /***************************************************************/
  592.  
  593. /*************************************************************
  594. /*
  595. /*   Pass the subband sample through the synthesis window
  596. /*
  597. /**************************************************************/
  598.  
  599. /* create in synthesis filter */
  600.  
  601. void create_syn_filter(filter)
  602. double FAR filter[64][SBLIMIT];
  603. {
  604.     register int i,k;
  605.  
  606.     for (i=0; i<64; i++)
  607.         for (k=0; k<32; k++) {
  608.             if ((filter[i][k] = 1e9*cos((double)((PI64*i+PI4)*(2*k+1)))) >= 0)
  609.                 modf(filter[i][k]+0.5, &filter[i][k]);
  610.             else
  611.                 modf(filter[i][k]-0.5, &filter[i][k]);
  612.             filter[i][k] *= 1e-9;
  613.         }
  614. }
  615.  
  616. /***************************************************************
  617. /*
  618. /*   Window the restored sample
  619. /*
  620. /***************************************************************/
  621.  
  622. /* read in synthesis window */
  623.  
  624. void read_syn_window(window)
  625. double FAR window[HAN_SIZE];
  626. {
  627.     int i,j[4];
  628.     FILE *fp;
  629.     double f[4];
  630.     char t[150];
  631.  
  632.     if (!(fp = OpenTableFile("dewindow") )) {
  633.         printf("Please check synthesis window table 'dewindow'\n");
  634.         exit(1);
  635.     }
  636.     for (i=0;i<512;i+=4) {
  637.         fgets(t, 150, fp);
  638.         sscanf(t,"D[%d] = %lf D[%d] = %lf D[%d] = %lf D[%d] = %lf\n",
  639.                j, f,j+1,f+1,j+2,f+2,j+3,f+3);
  640.         if (i==j[0]) {
  641.             window[i] = f[0];
  642.             window[i+1] = f[1];
  643.             window[i+2] = f[2];
  644.             window[i+3] = f[3];
  645.         }
  646.         else {
  647.             printf("Check index in synthesis window table\n");
  648.             exit(1);
  649.         }
  650.         fgets(t,150,fp);
  651.     }
  652.     fclose(fp);
  653. }
  654.  
  655. int SubBandSynthesis (bandPtr, channel, samples)
  656. double *bandPtr;
  657. int channel;
  658. short *samples;
  659. {
  660.     register int i,j,k;
  661.     register double *bufOffsetPtr, sum;
  662.     static int init = 1;
  663.     typedef double NN[64][32];
  664.     static NN FAR *filter;
  665.     typedef double BB[2][2*HAN_SIZE];
  666.     static BB FAR *buf;
  667.     static int bufOffset[2] = {64,64};
  668.     static double FAR *window;
  669.     int clip = 0;               /* count & return how many samples clipped */
  670.  
  671.     if (init) {
  672.         buf = (BB FAR *) mem_alloc(sizeof(BB),"BB");
  673.         filter = (NN FAR *) mem_alloc(sizeof(NN), "NN");
  674.         create_syn_filter(*filter);
  675.         window = (double FAR *) mem_alloc(sizeof(double) * HAN_SIZE, "WIN");
  676.         read_syn_window(window);
  677.         init = 0;
  678.     }
  679. /*    if (channel == 0) */
  680.     bufOffset[channel] = (bufOffset[channel] - 64) & 0x3ff;
  681.     bufOffsetPtr = &((*buf)[channel][bufOffset[channel]]);
  682.  
  683.     for (i=0; i<64; i++) {
  684.         sum = 0;
  685.         for (k=0; k<32; k++)
  686.             sum += bandPtr[k] * (*filter)[i][k];
  687.         bufOffsetPtr[i] = sum;
  688.     }
  689.     /*  S(i,j) = D(j+32i) * U(j+32i+((i+1)>>1)*64)  */
  690.     /*  samples(i,j) = MWindow(j+32i) * bufPtr(j+32i+((i+1)>>1)*64)  */
  691.     for (j=0; j<32; j++) {
  692.         sum = 0;
  693.         for (i=0; i<16; i++) {
  694.             k = j + (i<<5);
  695.             sum += window[k] * (*buf) [channel] [( (k + ( ((i+1)>>1) <<6) ) +
  696.                                                   bufOffset[channel]) & 0x3ff];
  697.         }
  698.  
  699. /*   {long foo = (sum > 0) ? sum * SCALE + 0.5 : sum * SCALE - 0.5; */
  700.      {long foo = sum * SCALE;
  701.      if (foo >= (long) SCALE)      {samples[j] = SCALE-1; ++clip;}
  702.      else if (foo < (long) -SCALE) {samples[j] = -SCALE;  ++clip;}
  703.      else                           samples[j] = foo;
  704.  }
  705.     }
  706.     return(clip);
  707. }
  708.  
  709. void out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames)
  710. short FAR pcm_sample[2][SSLIMIT][SBLIMIT];
  711. int num;
  712. frame_params *fr_ps;
  713. int done;
  714. FILE *outFile;
  715. unsigned long *psampFrames;
  716. {
  717.     int i,j,l;
  718.     int stereo = fr_ps->stereo;
  719.     int sblimit = fr_ps->sblimit;
  720.     static short int outsamp[1600];
  721.     static long k = 0;
  722.  
  723.     if (!done)
  724.         for (i=0;i<num;i++) for (j=0;j<SBLIMIT;j++) {
  725.             (*psampFrames)++;
  726.             for (l=0;l<stereo;l++) {
  727.                 if (!(k%1600) && k) {
  728.                     fwrite(outsamp,2,1600,outFile);
  729.                     k = 0;
  730.                 }
  731.                 outsamp[k++] = pcm_sample[l][i][j];
  732.             }
  733.         }
  734.     else {
  735.         fwrite(outsamp,2,(int)k,outFile);
  736.         k = 0;
  737.     }
  738. }
  739.  
  740. void  buffer_CRC(bs, old_crc)
  741. Bit_stream_struc  *bs;
  742. unsigned int  *old_crc;
  743. {
  744.     *old_crc = getbits(bs, 16);
  745. }
  746.  
  747. void  recover_CRC_error(pcm_sample, error_count, fr_ps, outFile, psampFrames)
  748. short FAR pcm_sample[2][SSLIMIT][SBLIMIT];
  749. int error_count;
  750. frame_params *fr_ps;
  751. FILE *outFile;
  752. unsigned long *psampFrames;
  753. {
  754.     int  stereo = fr_ps->stereo;
  755.     int  num, done, i;
  756.     int  samplesPerFrame, samplesPerSlot;
  757.     layer  *hdr = fr_ps->header;
  758.     long  offset;
  759.     short  *temp;
  760.  
  761.     num = 3;
  762.     if (hdr->lay == 1) num = 1;
  763.  
  764.     samplesPerSlot = SBLIMIT * num * stereo;
  765.     samplesPerFrame = samplesPerSlot * 32;
  766.  
  767.     if (error_count == 1) {     /* replicate previous error_free frame */
  768.         done = 1;
  769.         /* flush out fifo */
  770.         out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  771.         /* go back to the beginning of the previous frame */
  772.         offset = sizeof(short int) * samplesPerFrame;
  773.         fseek(outFile, -offset, SEEK_CUR);
  774.         done = 0;
  775.         for (i = 0; i < SCALE_BLOCK; i++) {
  776.             fread(pcm_sample, 2, samplesPerSlot, outFile);
  777.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  778.         }
  779.     }
  780.     else{                       /* mute the frame */
  781.         temp = (short*) pcm_sample;
  782.         done = 0;
  783.         for (i = 0; i < 2*3*SBLIMIT; i++)
  784.             *temp++ = MUTE;     /* MUTE value is in decoder.h */
  785.         for (i = 0; i < SCALE_BLOCK; i++)
  786.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  787.     }
  788. }
  789.  
  790. /************************* Layer III routines **********************/
  791.  
  792. void III_get_side_info(bs, si, fr_ps)
  793. Bit_stream_struc *bs;
  794. III_side_info_t *si;
  795. frame_params *fr_ps;
  796. {
  797.    int ch, gr, i;
  798.    int stereo = fr_ps->stereo;
  799.    
  800.    si->main_data_begin = getbits(bs, 9);
  801.    if (stereo == 1)
  802.       si->private_bits = getbits(bs,5);
  803.       else si->private_bits = getbits(bs,3);
  804.  
  805.    for (ch=0; ch<stereo; ch++)
  806.       for (i=0; i<4; i++)
  807.       si->ch[ch].scfsi[i] = get1bit(bs);
  808.  
  809.    for (gr=0; gr<2; gr++) {
  810.       for (ch=0; ch<stereo; ch++) {
  811.          si->ch[ch].gr[gr].part2_3_length = getbits(bs, 12);
  812.          si->ch[ch].gr[gr].big_values = getbits(bs, 9);
  813.          si->ch[ch].gr[gr].global_gain = getbits(bs, 8);
  814.          si->ch[ch].gr[gr].scalefac_compress = getbits(bs, 4);
  815.          si->ch[ch].gr[gr].window_switching_flag = get1bit(bs);
  816.          if (si->ch[ch].gr[gr].window_switching_flag) {
  817.             si->ch[ch].gr[gr].block_type = getbits(bs, 2);
  818.             si->ch[ch].gr[gr].mixed_block_flag = get1bit(bs);
  819.             for (i=0; i<2; i++)
  820.                si->ch[ch].gr[gr].table_select[i] = getbits(bs, 5);
  821.             for (i=0; i<3; i++)
  822.                si->ch[ch].gr[gr].subblock_gain[i] = getbits(bs, 3);
  823.                
  824.             /* Set region_count parameters since they are implicit in this case. */
  825.             
  826.             if (si->ch[ch].gr[gr].block_type == 0) {
  827.                printf("Side info bad: block_type == 0 in split block.\n");
  828.                exit(0);
  829.                }
  830.             else if (si->ch[ch].gr[gr].block_type == 2
  831.                      && si->ch[ch].gr[gr].mixed_block_flag == 0)
  832.                si->ch[ch].gr[gr].region0_count = 8; /* MI 9; */
  833.             else si->ch[ch].gr[gr].region0_count = 7; /* MI 8; */
  834.             si->ch[ch].gr[gr].region1_count = 20 -
  835.                       si->ch[ch].gr[gr].region0_count;
  836.             }
  837.          else {
  838.             for (i=0; i<3; i++)
  839.                si->ch[ch].gr[gr].table_select[i] = getbits(bs, 5);
  840.             si->ch[ch].gr[gr].region0_count = getbits(bs, 4);
  841.             si->ch[ch].gr[gr].region1_count = getbits(bs, 3);
  842.             si->ch[ch].gr[gr].block_type = 0;
  843.             }      
  844.          si->ch[ch].gr[gr].preflag = get1bit(bs);
  845.          si->ch[ch].gr[gr].scalefac_scale = get1bit(bs);
  846.          si->ch[ch].gr[gr].count1table_select = get1bit(bs);
  847.          }
  848.       }
  849. }
  850.  
  851. void III_put_side_info(bs, si, fr_ps)
  852. frame_params *fr_ps;
  853. Bit_stream_struc *bs;
  854. III_side_info_t *si;
  855. {
  856.    int ch, gr, i;
  857.    int stereo = fr_ps->stereo;
  858.  
  859.    putbits(bs, si->main_data_begin,9);
  860.    if (stereo == 1)
  861.       putbits(bs, si->private_bits, 5);
  862.       else putbits(bs, si->private_bits, 3);
  863.  
  864.    for (ch=0; ch<stereo; ch++)
  865.       for (i=0; i<4; i++)
  866.          put1bit(bs, si->ch[ch].scfsi[i]);
  867.  
  868.    for (gr=0; gr<2; gr++) {
  869.       for (ch=0; ch<stereo; ch++) {
  870.          putbits(bs, si->ch[ch].gr[gr].part2_3_length, 12);
  871.          putbits(bs, si->ch[ch].gr[gr].big_values, 9);
  872.          putbits(bs, si->ch[ch].gr[gr].global_gain, 8);
  873.          putbits(bs, si->ch[ch].gr[gr].scalefac_compress, 4);
  874.          put1bit(bs, si->ch[ch].gr[gr].window_switching_flag);
  875.          if (si->ch[ch].gr[gr].window_switching_flag) {
  876.             putbits(bs, si->ch[ch].gr[gr].block_type, 2);
  877.             put1bit(bs, si->ch[ch].gr[gr].mixed_block_flag);
  878.             for (i=0; i<2; i++)
  879.                putbits(bs, si->ch[ch].gr[gr].table_select[i], 5);
  880.             for (i=0; i<3; i++)
  881.                putbits(bs, si->ch[ch].gr[gr].subblock_gain[i], 3);
  882.             }
  883.          else {
  884.             for (i=0; i<3; i++)
  885.             putbits(bs, si->ch[ch].gr[gr].table_select[i], 5);
  886.             putbits(bs, si->ch[ch].gr[gr].region0_count, 4);
  887.             putbits(bs, si->ch[ch].gr[gr].region1_count, 3);
  888.             }      
  889.  
  890.          put1bit(bs, si->ch[ch].gr[gr].preflag);
  891.          put1bit(bs, si->ch[ch].gr[gr].scalefac_scale);
  892.          put1bit(bs, si->ch[ch].gr[gr].count1table_select);
  893.          }
  894.       }
  895. }
  896.  
  897. struct {
  898.    int l[5];
  899.    int s[3];} sfbtable = {{0, 6, 11, 16, 21},
  900.                           {0, 6, 12}};
  901.                          
  902. int slen[2][16] = {{0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
  903.                    {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}};
  904. struct  {
  905.    int l[23];
  906.    int s[14];} sfBandIndex[3] =   
  907.    {{{0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
  908.      {0,4,8,12,16,22,30,40,52,66,84,106,136,192}},
  909.     {{0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
  910.      {0,4,8,12,16,22,28,38,50,64,80,100,126,192}},
  911.     {{0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
  912.      {0,4,8,12,16,22,30,42,58,78,104,138,180,192}}};
  913.  
  914.  
  915. void III_get_scale_factors(scalefac, si, gr, ch, fr_ps)
  916. III_scalefac_t *scalefac;
  917. III_side_info_t *si;
  918. int gr, ch;
  919. frame_params *fr_ps;
  920. {
  921. int sfb, i, window;
  922. struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
  923.  
  924.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) { 
  925.       if (gr_info->mixed_block_flag) { /* MIXED */ /* NEW - ag 11/25 */
  926.          for (sfb = 0; sfb < 8; sfb++)
  927.             (*scalefac)[ch].l[sfb] = hgetbits( 
  928.                  slen[0][gr_info->scalefac_compress]);
  929.          for (sfb = 3; sfb < 6; sfb++)
  930.             for (window=0; window<3; window++)
  931.                (*scalefac)[ch].s[window][sfb] = hgetbits(
  932.                  slen[0][gr_info->scalefac_compress]);
  933.          for (sfb = 6; sfb < 12; sfb++)
  934.             for (window=0; window<3; window++)
  935.                (*scalefac)[ch].s[window][sfb] = hgetbits(
  936.                  slen[1][gr_info->scalefac_compress]);
  937.          for (sfb=12,window=0; window<3; window++)
  938.             (*scalefac)[ch].s[window][sfb] = 0;
  939.       }
  940.       else {  /* SHORT*/
  941.          for (i=0; i<2; i++) 
  942.             for (sfb = sfbtable.s[i]; sfb < sfbtable.s[i+1]; sfb++)
  943.                for (window=0; window<3; window++)
  944.                   (*scalefac)[ch].s[window][sfb] = hgetbits( 
  945.                     slen[i][gr_info->scalefac_compress]);
  946.          for (sfb=12,window=0; window<3; window++)
  947.             (*scalefac)[ch].s[window][sfb] = 0;
  948.       }
  949.     }          
  950.     else {   /* LONG types 0,1,3 */
  951.         for (i=0; i<4; i++) {
  952.            if ((si->ch[ch].scfsi[i] == 0) || (gr == 0))
  953.               for (sfb = sfbtable.l[i]; sfb < sfbtable.l[i+1]; sfb++)
  954.                   (*scalefac)[ch].l[sfb] = hgetbits(
  955.                  slen[(i<2)?0:1][gr_info->scalefac_compress]);
  956.         }
  957.         (*scalefac)[ch].l[22] = 0; 
  958.     }
  959. }
  960.  
  961.  
  962. /* Already declared in huffman.c
  963. struct huffcodetab ht[HTN];
  964. */
  965. int huffman_initialized = FALSE;
  966.  
  967. void initialize_huffman() {
  968.    FILE *fi;
  969.   
  970.    if (huffman_initialized) return;
  971.    if (!(fi = OpenTableFile("huffdec") )) {
  972.       printf("Please check huffman table 'huffdec'\n");
  973.       exit(1);
  974.    }
  975.  
  976.    if (fi==NULL) {
  977.  
  978.       fprintf(stderr,"decoder table open error\n");
  979.  
  980.       exit(3);
  981.  
  982.       }
  983.  
  984.    if (read_decoder_table(fi) != HTN) {
  985.       fprintf(stderr,"decoder table read error\n");
  986.       exit(4);
  987.       }
  988. huffman_initialized = TRUE;
  989. }
  990.  
  991. III_hufman_decode(is, si, ch, gr, part2_start, fr_ps)
  992. long int is[SBLIMIT][SSLIMIT];
  993. III_side_info_t *si;
  994. int gr, ch, part2_start;
  995. frame_params *fr_ps;
  996. {
  997.    int i, x, y;
  998.    int v, w;
  999.    struct huffcodetab *h;
  1000.    int region1Start;
  1001.    int region2Start;
  1002.    int bt = (*si).ch[ch].gr[gr].window_switching_flag && ((*si).ch[ch].gr[gr].block_type == 2);
  1003.  
  1004.    initialize_huffman();
  1005.  
  1006.    /* Find region boundary for short block case. */
  1007.    
  1008.    if ( ((*si).ch[ch].gr[gr].window_switching_flag) && 
  1009.         ((*si).ch[ch].gr[gr].block_type == 2) ) { 
  1010.    
  1011.       /* Region2. */
  1012.  
  1013.       region1Start = 36;  /* sfb[9/3]*3=36 */
  1014.       region2Start = 576; /* No Region2 for short block case. */
  1015.    }
  1016.  
  1017.  
  1018.    else {          /* Find region boundary for long block case. */
  1019.  
  1020.       region1Start = sfBandIndex[fr_ps->header->sampling_frequency]
  1021.                            .l[(*si).ch[ch].gr[gr].region0_count + 1]; /* MI */
  1022.       region2Start = sfBandIndex[fr_ps->header->sampling_frequency]
  1023.                               .l[(*si).ch[ch].gr[gr].region0_count +
  1024.                               (*si).ch[ch].gr[gr].region1_count + 2]; /* MI */
  1025.       }
  1026.  
  1027.  
  1028.    /* Read bigvalues area. */
  1029.    for (i=0; i<(*si).ch[ch].gr[gr].big_values*2; i+=2) {
  1030.       if      (i<region1Start) h = &ht[(*si).ch[ch].gr[gr].table_select[0]];
  1031.       else if (i<region2Start) h = &ht[(*si).ch[ch].gr[gr].table_select[1]];
  1032.            else                h = &ht[(*si).ch[ch].gr[gr].table_select[2]];
  1033.       huffman_decoder(h, &x, &y, &v, &w);
  1034.       is[i/SSLIMIT][i%SSLIMIT] = x;
  1035.       is[(i+1)/SSLIMIT][(i+1)%SSLIMIT] = y;
  1036.       }
  1037.  
  1038.    /* Read count1 area. */
  1039.    h = &ht[(*si).ch[ch].gr[gr].count1table_select+32];
  1040.    while ((hsstell() < part2_start + (*si).ch[ch].gr[gr].part2_3_length ) &&
  1041.      ( i < SSLIMIT*SBLIMIT )) {
  1042.       huffman_decoder(h, &x, &y, &v, &w);
  1043.       is[i/SSLIMIT][i%SSLIMIT] = v;
  1044.       is[(i+1)/SSLIMIT][(i+1)%SSLIMIT] = w;
  1045.       is[(i+2)/SSLIMIT][(i+2)%SSLIMIT] = x;
  1046.       is[(i+3)/SSLIMIT][(i+3)%SSLIMIT] = y;
  1047.       i += 4;
  1048.       }
  1049.  
  1050.    if (hsstell() > part2_start + (*si).ch[ch].gr[gr].part2_3_length)
  1051.    {  i -=4;
  1052.       rewindNbits(hsstell()-part2_start - (*si).ch[ch].gr[gr].part2_3_length);
  1053.    }
  1054.  
  1055.    /* Dismiss stuffing Bits */
  1056.    if ( hsstell() < part2_start + (*si).ch[ch].gr[gr].part2_3_length )
  1057.       hgetbits( part2_start + (*si).ch[ch].gr[gr].part2_3_length - hsstell());
  1058.  
  1059.    /* Zero out rest. */
  1060.    for (; i<SSLIMIT*SBLIMIT; i++)
  1061.       is[i/SSLIMIT][i%SSLIMIT] = 0;
  1062. }
  1063.  
  1064.  
  1065. int pretab[22] = {0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0};
  1066.  
  1067. void III_dequantize_sample(is,xr,scalefac,gr_info, ch,fr_ps)
  1068. long int is[SBLIMIT][SSLIMIT];
  1069. double xr[SBLIMIT][SSLIMIT];
  1070. struct gr_info_s *gr_info;
  1071. III_scalefac_t *scalefac;
  1072. frame_params *fr_ps;
  1073. int ch;
  1074. {
  1075.    int ss,sb,cb=0,sfreq=fr_ps->header->sampling_frequency;
  1076.    int stereo = fr_ps->stereo;
  1077.    int next_cb_boundary, cb_begin, cb_width, sign;
  1078.  
  1079.    /* choose correct scalefactor band per block type, initalize boundary */
  1080.  
  1081.    if (gr_info->window_switching_flag && (gr_info->block_type == 2) )
  1082.       if (gr_info->mixed_block_flag) 
  1083.          next_cb_boundary=sfBandIndex[sfreq].l[1];  /* LONG blocks: 0,1,3 */
  1084.       else {
  1085.          next_cb_boundary=sfBandIndex[sfreq].s[1]*3; /* pure SHORT block */
  1086.     cb_width = sfBandIndex[sfreq].s[1];
  1087.     cb_begin = 0;
  1088.       }  
  1089.    else 
  1090.       next_cb_boundary=sfBandIndex[sfreq].l[1];  /* LONG blocks: 0,1,3 */
  1091.  
  1092.    /* apply formula per block type */
  1093.  
  1094.    for (sb=0 ; sb < SBLIMIT ; sb++)
  1095.       for (ss=0 ; ss < SSLIMIT ; ss++) {
  1096.  
  1097.          if ( (sb*18)+ss == next_cb_boundary)  { /* Adjust critical band boundary */
  1098.             if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  1099.                if (gr_info->mixed_block_flag)  {
  1100.                   if (((sb*18)+ss) == sfBandIndex[sfreq].l[8])  {
  1101.                      next_cb_boundary=sfBandIndex[sfreq].s[4]*3; 
  1102.                      cb = 3;
  1103.                      cb_width = sfBandIndex[sfreq].s[cb+1] - 
  1104.                                 sfBandIndex[sfreq].s[cb];
  1105.                      cb_begin = sfBandIndex[sfreq].s[cb]*3;      
  1106.                   }
  1107.                   else if (((sb*18)+ss) < sfBandIndex[sfreq].l[8]) 
  1108.                      next_cb_boundary = sfBandIndex[sfreq].l[(++cb)+1];
  1109.                   else {
  1110.                      next_cb_boundary = sfBandIndex[sfreq].s[(++cb)+1]*3;
  1111.                      cb_width = sfBandIndex[sfreq].s[cb+1] - 
  1112.                                     sfBandIndex[sfreq].s[cb];
  1113.                      cb_begin = sfBandIndex[sfreq].s[cb]*3;      
  1114.                   }   
  1115.                }
  1116.                else  {
  1117.                   next_cb_boundary = sfBandIndex[sfreq].s[(++cb)+1]*3;
  1118.                   cb_width = sfBandIndex[sfreq].s[cb+1] - 
  1119.                                sfBandIndex[sfreq].s[cb];
  1120.                 cb_begin = sfBandIndex[sfreq].s[cb]*3;      
  1121.                } 
  1122.             }
  1123.             else /* long blocks */
  1124.                next_cb_boundary = sfBandIndex[sfreq].l[(++cb)+1];
  1125.          }
  1126.  
  1127.          /* Compute overall (global) scaling. */
  1128.  
  1129.          xr[sb][ss] = pow( 2.0 , (0.25 * (gr_info->global_gain - 210.0)));
  1130.  
  1131.          /* Do long/short dependent scaling operations. */
  1132.         
  1133.          if (gr_info->window_switching_flag && (
  1134.             ((gr_info->block_type == 2) && (gr_info->mixed_block_flag == 0)) ||
  1135.             ((gr_info->block_type == 2) && gr_info->mixed_block_flag && (sb >= 2)) )) {
  1136.  
  1137.             xr[sb][ss] *= pow(2.0, 0.25 * -8.0 * 
  1138.                     gr_info->subblock_gain[(((sb*18)+ss) - cb_begin)/cb_width]);
  1139.             xr[sb][ss] *= pow(2.0, 0.25 * -2.0 * (1.0+gr_info->scalefac_scale)
  1140.               * (*scalefac)[ch].s[(((sb*18)+ss) - cb_begin)/cb_width][cb]);
  1141.          }
  1142.          else {   /* LONG block types 0,1,3 & 1st 2 subbands of switched blocks */
  1143.             xr[sb][ss] *= pow(2.0, -0.5 * (1.0+gr_info->scalefac_scale)
  1144.                                         * ((*scalefac)[ch].l[cb]
  1145.                                         + gr_info->preflag * pretab[cb]));
  1146.          }
  1147.  
  1148.          /* Scale quantized value. */
  1149.         
  1150.          sign = (is[sb][ss]<0) ? 1 : 0; 
  1151.          xr[sb][ss] *= pow( (double) abs(is[sb][ss]), ((double)4.0/3.0) );
  1152.          if (sign) xr[sb][ss] = -xr[sb][ss];
  1153.       }
  1154. }
  1155.  
  1156. III_reorder (xr, ro, gr_info, fr_ps) 
  1157. double xr[SBLIMIT][SSLIMIT]; 
  1158. double ro[SBLIMIT][SSLIMIT]; 
  1159. struct gr_info_s *gr_info;
  1160. frame_params *fr_ps;
  1161. {
  1162.    int sfreq=fr_ps->header->sampling_frequency;
  1163.    int sfb, sfb_start, sfb_lines;
  1164.    int sb, ss, window, freq, src_line, des_line;
  1165.  
  1166.    for(sb=0;sb<SBLIMIT;sb++)
  1167.       for(ss=0;ss<SSLIMIT;ss++) 
  1168.          ro[sb][ss] = 0;
  1169.  
  1170.    if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  1171.       if (gr_info->mixed_block_flag) {
  1172.          /* NO REORDER FOR LOW 2 SUBBANDS */
  1173.          for (sb=0 ; sb < 2 ; sb++)
  1174.             for (ss=0 ; ss < SSLIMIT ; ss++) {
  1175.                ro[sb][ss] = xr[sb][ss];
  1176.             }
  1177.          /* REORDERING FOR REST SWITCHED SHORT */
  1178.          for(sfb=3,sfb_start=sfBandIndex[sfreq].s[3],
  1179.             sfb_lines=sfBandIndex[sfreq].s[4] - sfb_start; 
  1180.             sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
  1181.             (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
  1182.                for(window=0; window<3; window++)
  1183.                   for(freq=0;freq<sfb_lines;freq++) {
  1184.                      src_line = sfb_start*3 + window*sfb_lines + freq; 
  1185.                      des_line = (sfb_start*3) + window + (freq*3);
  1186.                      ro[des_line/SSLIMIT][des_line%SSLIMIT] = 
  1187.                                     xr[src_line/SSLIMIT][src_line%SSLIMIT];
  1188.                }
  1189.       } 
  1190.       else {  /* pure short */
  1191.          for(sfb=0,sfb_start=0,sfb_lines=sfBandIndex[sfreq].s[1]; 
  1192.             sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
  1193.             (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
  1194.                for(window=0; window<3; window++)
  1195.                   for(freq=0;freq<sfb_lines;freq++) {
  1196.                      src_line = sfb_start*3 + window*sfb_lines + freq; 
  1197.                      des_line = (sfb_start*3) + window + (freq*3);
  1198.                      ro[des_line/SSLIMIT][des_line%SSLIMIT] = 
  1199.                                     xr[src_line/SSLIMIT][src_line%SSLIMIT];
  1200.                }
  1201.       }
  1202.    }
  1203.    else {   /*long blocks */
  1204.       for (sb=0 ; sb < SBLIMIT ; sb++)
  1205.          for (ss=0 ; ss < SSLIMIT ; ss++) 
  1206.             ro[sb][ss] = xr[sb][ss];
  1207.    }
  1208. }
  1209.  
  1210.  
  1211.  
  1212. void III_stereo(xr, lr, scalefac, gr_info, fr_ps)
  1213. double xr[2][SBLIMIT][SSLIMIT];
  1214. double lr[2][SBLIMIT][SSLIMIT];
  1215. III_scalefac_t *scalefac;
  1216. struct gr_info_s *gr_info;
  1217. frame_params *fr_ps;
  1218. {
  1219.    int sfreq = fr_ps->header->sampling_frequency;
  1220.    int stereo = fr_ps->stereo;
  1221.    int ms_stereo = (fr_ps->header->mode == MPG_MD_JOINT_STEREO) &&
  1222.                    (fr_ps->header->mode_ext & 0x2); 
  1223.    int i_stereo = (fr_ps->header->mode == MPG_MD_JOINT_STEREO) &&
  1224.                   (fr_ps->header->mode_ext & 0x1);
  1225.    int js_bound;  /* frequency line that marks the beggining of the zero part */  
  1226.    int sfb,next_sfb_boundary;
  1227.    int i,j,sb,ss,ch,is_pos[576]; 
  1228.    double is_ratio[576];
  1229.   
  1230.    /* intialization */
  1231.    for ( i=0; i<576; i++ )
  1232.       is_pos[i] = 7;
  1233.  
  1234.    if ((stereo == 2) && i_stereo )
  1235.    {  if (gr_info->window_switching_flag && (gr_info->block_type == 2))
  1236.       {  if( gr_info->mixed_block_flag )
  1237.          {  int max_sfb = 0;
  1238.  
  1239.             for ( j=0; j<3; j++ )
  1240.             {  int sfbcnt;
  1241.                sfbcnt = 2;
  1242.                for( sfb=12; sfb >=3; sfb-- )
  1243.                {  int lines;
  1244.                   lines = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
  1245.                   i = 3*sfBandIndex[sfreq].s[sfb] + (j+1) * lines - 1;
  1246.                   while ( lines > 0 )
  1247.                   {  if ( xr[1][i/SSLIMIT][i%SSLIMIT] != 0.0 )
  1248.                      {  sfbcnt = sfb;
  1249.                         sfb = -10;
  1250.                         lines = -10;
  1251.                      }
  1252.                      lines--;
  1253.                      i--;
  1254.                   }
  1255.                }
  1256.                sfb = sfbcnt + 1;
  1257.  
  1258.                if ( sfb > max_sfb )
  1259.                   max_sfb = sfb;
  1260.  
  1261.                while( sfb<12 )
  1262.                {  sb = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
  1263.                   i = 3*sfBandIndex[sfreq].s[sfb] + j * sb;
  1264.                   for ( ; sb > 0; sb--)
  1265.                   {  is_pos[i] = (*scalefac)[1].s[j][sfb];
  1266.                      if ( is_pos[i] != 7 )
  1267.                         is_ratio[i] = tan( is_pos[i] * (PI / 12));
  1268.                      i++;
  1269.                   }
  1270.                   sfb++;
  1271.                }
  1272.                sb = sfBandIndex[sfreq].s[11]-sfBandIndex[sfreq].s[10];
  1273.                sfb = 3*sfBandIndex[sfreq].s[10] + j * sb;
  1274.                sb = sfBandIndex[sfreq].s[12]-sfBandIndex[sfreq].s[11];
  1275.                i = 3*sfBandIndex[sfreq].s[11] + j * sb;
  1276.                for ( ; sb > 0; sb-- )
  1277.                {  is_pos[i] = is_pos[sfb];
  1278.                   is_ratio[i] = is_ratio[sfb];
  1279.                   i++;
  1280.                }
  1281.              }
  1282.              if ( max_sfb <= 3 )
  1283.              {  i = 2;
  1284.                 ss = 17;
  1285.                 sb = -1;
  1286.                 while ( i >= 0 )
  1287.                 {  if ( xr[1][i][ss] != 0.0 )
  1288.                    {  sb = i*18+ss;
  1289.                       i = -1;
  1290.                    } else
  1291.                    {  ss--;
  1292.                       if ( ss < 0 )
  1293.                       {  i--;
  1294.                          ss = 17;
  1295.                       }
  1296.                    }
  1297.                 }
  1298.                 i = 0;
  1299.                 while ( sfBandIndex[sfreq].l[i] <= sb )
  1300.                    i++;
  1301.                 sfb = i;
  1302.                 i = sfBandIndex[sfreq].l[i];
  1303.                 for ( ; sfb<8; sfb++ )
  1304.                 {  sb = sfBandIndex[sfreq].l[sfb+1]-sfBandIndex[sfreq].l[sfb];
  1305.                    for ( ; sb > 0; sb--)
  1306.                    {  is_pos[i] = (*scalefac)[1].l[sfb];
  1307.                       if ( is_pos[i] != 7 )
  1308.                          is_ratio[i] = tan( is_pos[i] * (PI / 12));
  1309.                       i++;
  1310.                    }
  1311.                 }
  1312.             }
  1313.          } else
  1314.          {  for ( j=0; j<3; j++ )
  1315.             {  int sfbcnt;
  1316.                sfbcnt = -1;
  1317.                for( sfb=12; sfb >=0; sfb-- )
  1318.                {  int lines;
  1319.                   lines = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
  1320.                   i = 3*sfBandIndex[sfreq].s[sfb] + (j+1) * lines - 1;
  1321.                   while ( lines > 0 )
  1322.                   {  if ( xr[1][i/SSLIMIT][i%SSLIMIT] != 0.0 )
  1323.                      {  sfbcnt = sfb;
  1324.                         sfb = -10;
  1325.                         lines = -10;
  1326.                      }
  1327.                      lines--;
  1328.                      i--;
  1329.                   }
  1330.                }
  1331.                sfb = sfbcnt + 1;
  1332.                while( sfb<12 )
  1333.                {  sb = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
  1334.                   i = 3*sfBandIndex[sfreq].s[sfb] + j * sb;
  1335.                   for ( ; sb > 0; sb--)
  1336.                   {  is_pos[i] = (*scalefac)[1].s[j][sfb];
  1337.                      if ( is_pos[i] != 7 )
  1338.                         is_ratio[i] = tan( is_pos[i] * (PI / 12));
  1339.                      i++;
  1340.                   }
  1341.                   sfb++;
  1342.                }
  1343.  
  1344.                sb = sfBandIndex[sfreq].s[11]-sfBandIndex[sfreq].s[10];
  1345.                sfb = 3*sfBandIndex[sfreq].s[10] + j * sb;
  1346.                sb = sfBandIndex[sfreq].s[12]-sfBandIndex[sfreq].s[11];
  1347.                i = 3*sfBandIndex[sfreq].s[11] + j * sb;
  1348.                for ( ; sb > 0; sb-- )
  1349.                {  is_pos[i] = is_pos[sfb];
  1350.                   is_ratio[i] = is_ratio[sfb];
  1351.                   i++;
  1352.                }
  1353.             }
  1354.          }
  1355.       } else
  1356.       {  i = 31;
  1357.          ss = 17;
  1358.          sb = 0;
  1359.          while ( i >= 0 )
  1360.          {  if ( xr[1][i][ss] != 0.0 )
  1361.             {  sb = i*18+ss;
  1362.                i = -1;
  1363.             } else
  1364.             {  ss--;
  1365.                if ( ss < 0 )
  1366.                {  i--;
  1367.                   ss = 17;
  1368.                }
  1369.             }
  1370.          }
  1371.          i = 0;
  1372.          while ( sfBandIndex[sfreq].l[i] <= sb )
  1373.             i++;
  1374.          sfb = i;
  1375.          i = sfBandIndex[sfreq].l[i];
  1376.          for ( ; sfb<21; sfb++ )
  1377.          {  sb = sfBandIndex[sfreq].l[sfb+1] - sfBandIndex[sfreq].l[sfb];
  1378.             for ( ; sb > 0; sb--)
  1379.             {  is_pos[i] = (*scalefac)[1].l[sfb];
  1380.                if ( is_pos[i] != 7 )
  1381.                   is_ratio[i] = tan( is_pos[i] * (PI / 12));
  1382.                i++;
  1383.             }
  1384.          }
  1385.          sfb = sfBandIndex[sfreq].l[20];
  1386.          for ( sb = 576 - sfBandIndex[sfreq].l[21]; sb > 0; sb-- )
  1387.          {  is_pos[i] = is_pos[sfb];
  1388.             is_ratio[i] = is_ratio[sfb];
  1389.             i++;
  1390.          }
  1391.       }
  1392.    }
  1393.  
  1394.    for(ch=0;ch<2;ch++)
  1395.       for(sb=0;sb<SBLIMIT;sb++)
  1396.          for(ss=0;ss<SSLIMIT;ss++) 
  1397.             lr[ch][sb][ss] = 0;
  1398.  
  1399.    if (stereo==2) 
  1400.       for(sb=0;sb<SBLIMIT;sb++)
  1401.          for(ss=0;ss<SSLIMIT;ss++) {
  1402.             i = (sb*18)+ss;
  1403.             if ( is_pos[i] == 7 ) {
  1404.                if ( ms_stereo ) {
  1405.                   lr[0][sb][ss] = (xr[0][sb][ss]+xr[1][sb][ss])/1.41421356;
  1406.                   lr[1][sb][ss] = (xr[0][sb][ss]-xr[1][sb][ss])/1.41421356;
  1407.                }
  1408.                else {
  1409.                   lr[0][sb][ss] = xr[0][sb][ss];
  1410.                   lr[1][sb][ss] = xr[1][sb][ss];
  1411.                }
  1412.             }
  1413.             else if (i_stereo ) {
  1414.                lr[0][sb][ss] = xr[0][sb][ss] * (is_ratio[i]/(1+is_ratio[i]));
  1415.                lr[1][sb][ss] = xr[0][sb][ss] * (1/(1+is_ratio[i])); 
  1416.             }
  1417.             else {
  1418.                printf("Error in streo processing\n");
  1419.             }
  1420.          }
  1421.    else  /* mono , bypass xr[0][][] to lr[0][][]*/
  1422.       for(sb=0;sb<SBLIMIT;sb++)
  1423.          for(ss=0;ss<SSLIMIT;ss++)
  1424.             lr[0][sb][ss] = xr[0][sb][ss];
  1425.  
  1426. }
  1427.  
  1428. double Ci[8]={-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
  1429.  
  1430. void III_antialias(xr, hybridIn, gr_info, fr_ps)
  1431. double xr[SBLIMIT][SSLIMIT];    
  1432. double hybridIn[SBLIMIT][SSLIMIT];
  1433. struct gr_info_s *gr_info;             
  1434. frame_params *fr_ps;            
  1435. {
  1436.    static int    init = 1;
  1437.    static double ca[8],cs[8];
  1438.    double        bu,bd;  /* upper and lower butterfly inputs */
  1439.    int           ss,sb,sblim;
  1440.  
  1441.    if (init) {
  1442.       int i;
  1443.       double    sq;
  1444.       for (i=0;i<8;i++) {
  1445.          sq=sqrt(1.0+Ci[i]*Ci[i]);
  1446.          cs[i] = 1.0/sq;
  1447.          ca[i] = Ci[i]/sq;
  1448.       }
  1449.       init = 0;
  1450.    }
  1451.    
  1452.    /* clear all inputs */  
  1453.       
  1454.     for(sb=0;sb<SBLIMIT;sb++)
  1455.        for(ss=0;ss<SSLIMIT;ss++)
  1456.           hybridIn[sb][ss] = xr[sb][ss];
  1457.  
  1458.    if  (gr_info->window_switching_flag && (gr_info->block_type == 2) &&
  1459.        !gr_info->mixed_block_flag ) return;
  1460.  
  1461.    if ( gr_info->window_switching_flag && gr_info->mixed_block_flag &&
  1462.      (gr_info->block_type == 2))
  1463.       sblim = 1;
  1464.    else
  1465.       sblim = SBLIMIT-1;
  1466.  
  1467.    /* 31 alias-reduction operations between each pair of sub-bands */
  1468.    /* with 8 butterflies between each pair                         */
  1469.  
  1470.    for(sb=0;sb<sblim;sb++)   
  1471.       for(ss=0;ss<8;ss++) {      
  1472.          bu = xr[sb][17-ss];
  1473.          bd = xr[sb+1][ss];
  1474.          hybridIn[sb][17-ss] = (bu * cs[ss]) - (bd * ca[ss]);
  1475.          hybridIn[sb+1][ss] = (bd * cs[ss]) + (bu * ca[ss]);
  1476.          }  
  1477. }
  1478.  
  1479. void inv_mdct(in, out, block_type)
  1480. double in[18];
  1481. double out[36];
  1482. int block_type;
  1483. {
  1484. /*------------------------------------------------------------------*/
  1485. /*                                                                  */
  1486. /*    Function: Calculation of the inverse MDCT                     */
  1487. /*    In the case of short blocks the 3 output vectors are already  */
  1488. /*    overlapped and added in this modul.                           */
  1489. /*                                                                  */
  1490. /*    New layer3                                                    */
  1491. /*                                                                  */
  1492. /*------------------------------------------------------------------*/
  1493.  
  1494. int     k,i,m,N,p;
  1495. double  tmp[12],sum;
  1496. static  double  win[4][36];
  1497. static  int init=0;
  1498. static  double COS[4*36];
  1499.  
  1500.     if(init==0){
  1501.  
  1502.     /* type 0 */
  1503.       for(i=0;i<36;i++)
  1504.          win[0][i] = sin( PI/36 *(i+0.5) );
  1505.  
  1506.     /* type 1*/
  1507.       for(i=0;i<18;i++)
  1508.          win[1][i] = sin( PI/36 *(i+0.5) );
  1509.       for(i=18;i<24;i++)
  1510.          win[1][i] = 1.0;
  1511.       for(i=24;i<30;i++)
  1512.          win[1][i] = sin( PI/12 *(i+0.5-18) );
  1513.       for(i=30;i<36;i++)
  1514.          win[1][i] = 0.0;
  1515.  
  1516.     /* type 3*/
  1517.       for(i=0;i<6;i++)
  1518.          win[3][i] = 0.0;
  1519.       for(i=6;i<12;i++)
  1520.          win[3][i] = sin( PI/12 *(i+0.5-6) );
  1521.       for(i=12;i<18;i++)
  1522.          win[3][i] =1.0;
  1523.       for(i=18;i<36;i++)
  1524.          win[3][i] = sin( PI/36*(i+0.5) );
  1525.  
  1526.     /* type 2*/
  1527.       for(i=0;i<12;i++)
  1528.          win[2][i] = sin( PI/12*(i+0.5) ) ;
  1529.       for(i=12;i<36;i++)
  1530.          win[2][i] = 0.0 ;
  1531.  
  1532.       for (i=0; i<4*36; i++)
  1533.          COS[i] = cos(PI/(2*36) * i);
  1534.  
  1535.       init++;
  1536.     }
  1537.  
  1538.     for(i=0;i<36;i++)
  1539.        out[i]=0;
  1540.  
  1541.     if(block_type == 2){
  1542.        N=12;
  1543.        for(i=0;i<3;i++){
  1544.           for(p= 0;p<N;p++){
  1545.              sum = 0.0;
  1546.              for(m=0;m<N/2;m++)
  1547.                 sum += in[i+3*m] * cos( PI/(2*N)*(2*p+1+N/2)*(2*m+1) );
  1548.              tmp[p] = sum * win[block_type][p] ;
  1549.           }
  1550.           for(p=0;p<N;p++)
  1551.              out[6*i+p+6] += tmp[p];
  1552.        }
  1553.     }
  1554.     else{
  1555.       N=36;
  1556.       for(p= 0;p<N;p++){
  1557.          sum = 0.0;
  1558.          for(m=0;m<N/2;m++)
  1559.            sum += in[m] * COS[((2*p+1+N/2)*(2*m+1))%(4*36)];
  1560.          out[p] = sum * win[block_type][p];
  1561.       }
  1562.     }
  1563. }
  1564.  
  1565. void III_hybrid(fsIn, tsOut ,sb, ch, gr_info, fr_ps)
  1566. double fsIn[SSLIMIT];   /* freq samples per subband in */
  1567. double tsOut[SSLIMIT];  /* time samples per subband out */
  1568. int sb, ch;
  1569. struct gr_info_s *gr_info;             
  1570. frame_params *fr_ps;            
  1571. {
  1572.    int ss;
  1573.    double rawout[36];
  1574.    static double prevblck[2][SBLIMIT][SSLIMIT];
  1575.    static int init = 1;
  1576.    int bt;
  1577.  
  1578.    if (init) {
  1579.       int i,j,k;
  1580.       
  1581.       for(i=0;i<2;i++)
  1582.          for(j=0;j<SBLIMIT;j++)
  1583.             for(k=0;k<SSLIMIT;k++)
  1584.                prevblck[i][j][k]=0.0;
  1585.       init = 0;
  1586.    }
  1587.  
  1588.    bt = (gr_info->window_switching_flag && gr_info->mixed_block_flag &&
  1589.           (sb < 2)) ? 0 : gr_info->block_type; 
  1590.  
  1591.    inv_mdct( fsIn, rawout, bt);
  1592.  
  1593.    /* overlap addition */
  1594.    for(ss=0; ss<SSLIMIT; ss++) {
  1595.       tsOut[ss] = rawout[ss] + prevblck[ch][sb][ss];
  1596.       prevblck[ch][sb][ss] = rawout[ss+18];
  1597.    }
  1598. }
  1599.  
  1600. /* Return the number of slots for main data of current frame, */
  1601.  
  1602. int main_data_slots(fr_ps)
  1603. frame_params fr_ps;
  1604. {int nSlots;
  1605.  
  1606.    nSlots = (144 * bitrate[2][fr_ps.header->bitrate_index])
  1607.         / s_freq[fr_ps.header->sampling_frequency];
  1608.   if (fr_ps.header->padding) nSlots++;
  1609.   nSlots -= 4;
  1610.   if (fr_ps.header->error_protection) nSlots -= 2;
  1611.   if (fr_ps.stereo == 1) nSlots -= 17; else nSlots -=32;
  1612.   return(nSlots);
  1613. }
  1614.