home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / GFX / MPEG / mpeg3play.lha / mpeg3play / src / decode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-11  |  77.2 KB  |  2,643 lines

  1. /**********************************************************************
  2.  * ISO MPEG Audio Subgroup Software Simulation Group (1996)
  3.  * ISO 13818-3 MPEG-2 Audio Decoder - Lower Sampling Frequency Extension
  4.  *
  5.  * $Id: decode.c,v 1.2 1996/03/28 03:13:37 rowlands Exp $
  6.  *
  7.  * $Log: decode.c,v $
  8.  * Revision 1.2  1996/03/28 03:13:37  rowlands
  9.  * Merged layers 1-2 and layer 3 revisions
  10.  *
  11.  * Revision 1.1  1996/02/14 03:45:52  rowlands
  12.  * Initial revision
  13.  *
  14.  * Received from FhG
  15.  **********************************************************************/
  16. /**********************************************************************
  17.  *   date   programmers         comment                               *
  18.  * 2/25/91  Douglas Wong,       start of version 1.0 records          *
  19.  *          Davis Pan                                                 *
  20.  * 3/06/91  Douglas Wong        rename: setup.h to dedef.h            *
  21.  *                                      dfilter to defilter           *
  22.  *                                      dwindow to dewindow           *
  23.  *                              integrated "quantizer", "scalefactor" *
  24.  *                              combined window_samples routine into  *
  25.  *                              filter samples                        *
  26.  * 3/31/91  Bill Aspromonte     replaced read_filter by               *
  27.  *                              create_syn_filter and introduced a    *
  28.  *                              new Sub-Band Synthesis routine called *
  29.  *                              SubBandSynthesis()                    *
  30.  * 5/10/91  Vish (PRISM)        Ported to Macintosh and Unix.         *
  31.  *                              Changed "out_fifo()" so that last     *
  32.  *                              unfilled block is also written out.   *
  33.  *                              "create_syn_filter()" was modified so *
  34.  *                              that calculation precision is same as *
  35.  *                              in specification tables.              *
  36.  *                              Changed "decode_scale()" to reflect   *
  37.  *                              specifications.                       *
  38.  *                              Removed all routines used by          *
  39.  *                              "synchronize_buffer()".  This is now  *
  40.  *                              replaced by "seek_sync()".            *
  41.  *                              Incorporated Jean-Georges Fritsch's   *
  42.  *                              "bitstream.c" package.                *
  43.  *                              Deleted "reconstruct_sample()".       *
  44.  * 27jun91  dpwe (Aware)        Passed outFile and &sampFrames as     *
  45.  *                              args to out_fifo() - were global.     *
  46.  *                              Moved "alloc_*" reader to common.c.   *
  47.  *                              alloc, sblimit, stereo passed via new *
  48.  *                              'frame_params struct (were globals).  *
  49.  *                              Added JOINT STEREO decoding, lyrs I&II*
  50.  *                              Affects: decode_bitalloc,buffer_samps *
  51.  *                              Plus a few other cleanups.            *
  52.  * 6/10/91   Earle Jennings     conditional expansion added in        *
  53.  *                              II_dequantize_sample to handle range  *
  54.  *                              problems in MSDOS version             *
  55.  * 8/8/91    Jens Spille        Change for MS-C6.00                   *
  56.  *10/1/91    S.I. Sudharsanan,  Ported to IBM AIX platform.           *
  57.  *           Don H. Lee,                                              *
  58.  *           Peter W. Farrett                                         *
  59.  *10/3/91    Don H. Lee         implemented CRC-16 error protection   *
  60.  *                              newly introduced functions are        *
  61.  *                              buffer_CRC and recover_CRC_error.     *
  62.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  63.  *                              important fixes involved changing     *
  64.  *                              16-bit ints to long or unsigned in    *
  65.  *                              bit alloc routines for quant of 65535 *
  66.  *                              and passing proper function args.     *
  67.  *                              Removed "Other Joint Stereo" option   *
  68.  *                              and made bitrate be total channel     *
  69.  *                              bitrate, irrespective of the mode.    *
  70.  *                              Fixed many small bugs & reorganized.  *
  71.  * 7/27/92  Juan Pineda         Bug fix in SubBandSynthesis()         *
  72.  *--------------------------------------------------------------------*
  73.  * 6/14/92  Juan Pineda         Layer III decoding routines added.    *
  74.  *          Amit Gulati         Follows CD 3-11172 rev2.  Contains    *
  75.  *                              hacks deal with evolving available    *
  76.  *                              layerIII bitstreams.  Some (minor)    *
  77.  *                              modification of prior LI&II code.     *
  78.  * 10/25/92 Amit Gulati         Updated layerIII routines. Added code *
  79.  *                              for subblock_gain, switched block     *
  80.  *                              modes, stereo pre-processing.         *
  81.  *                              Corrected sign bits for huffman       *
  82.  *                              decoding of quadruples region and     *
  83.  *                              adjusted gain factor in III_dequant.  *
  84.  * 11/21/92 Amit Gulati         Several layerIII bugs fixed.          *
  85.  * 12/15/92 Amit Gulati         Corrected reordering (indexing)       *
  86.  *          Stan Searing        within IMDCT routine.                 *
  87.  *  8/24/93 Masahiro Iwadare    Included IS modification in Layer III.*
  88.  *                              Changed for 1 pass decoding.          *
  89.  *  9/07/93 Toshiyuki Ishino    Integrated Layer III with Ver 3.9.    *
  90.  *--------------------------------------------------------------------*
  91.  * 11/20/93 Masahiro Iwadare    Integrated Layer III with Ver 4.0.    *
  92.  *--------------------------------------------------------------------*
  93.  *  7/14/94 Juergen Koller      Bug fixes in Layer III code           *
  94.  *--------------------------------------------------------------------*
  95.  * 08/11/94 IIS                 Bug fixes in Layer III code           *
  96.  *--------------------------------------------------------------------*
  97.  * 9/20/94  Davis Pan           Modification to avoid premature       *
  98.  *                              synchword detection                   *
  99.  *--------------------------------------------------------------------*
  100.  * 11/09/94 Jon Rowlands        Merged premature synchword detection  *
  101.  *                              fix into layer III code version       *
  102.  *--------------------------------------------------------------------*
  103.  * 07/12/95 Soeren H. Nielsen   Changes for LSF Layer I and II        *
  104.  *--------------------------------------------------------------------*
  105.  *    8/95  Roland Bitto    Adapted to MPEG2                      *
  106.  *  9/8/95  Roalnd Bitto        Bugfix in Function III_stereo         * 
  107.  *--------------------------------------------------------------------*
  108.  * 12/16/96 Johan Hagman    Adapted for Solaris (mpeg3play 0.9)   *
  109.  **********************************************************************/
  110.  
  111. #include        "common.h"
  112. #include        "decoder.h"
  113. #include        "huffman.h"
  114.  
  115. /***************************************************************
  116.  *
  117.  * This module contains the core of the decoder ie all the
  118.  * computational routines. (Layer I and II only)
  119.  * Functions are common to both layer unless
  120.  * otherwise specified.
  121.  *
  122.  ***************************************************************/
  123.  
  124. extern Arguments_t Arguments;
  125. extern int       audiofd;
  126. extern int       smallAudioBuffer;
  127.  
  128. /*****************************************************************
  129.  *
  130.  * The following routines decode the system information
  131.  *
  132.  ****************************************************************/
  133.  
  134. /************ Layer I, Layer II & Layer III ******************/
  135.  
  136. void decode_info(
  137.     Bit_stream_struc    *bs,
  138.     frame_params    *fr_ps)
  139. {
  140.     layer *hdr = fr_ps->header;
  141.  
  142.     hdr->version    = get1bit(bs);
  143.     hdr->lay        = 4 - getbits(bs,2);
  144.     hdr->error_protection = !get1bit(bs); /* error protect. TRUE/FALSE */
  145.     hdr->bitrate_index    = getbits(bs,4);
  146.     hdr->sampling_frequency = getbits(bs,2);
  147.     hdr->padding    = get1bit(bs);
  148.     hdr->extension    = get1bit(bs);
  149.     hdr->mode = getbits(bs,2);
  150.     hdr->mode_ext    = getbits(bs,2);
  151.     hdr->copyright    = get1bit(bs);
  152.     hdr->original    = get1bit(bs);
  153.     hdr->emphasis    = getbits(bs,2);
  154. }
  155.  
  156. /*******************************************************************
  157.  *
  158.  * The bit allocation information is decoded. Layer I
  159.  * has 4 bit per subband whereas Layer II is Ws and bit rate
  160.  * dependent.
  161.  *
  162.  ********************************************************************/
  163.  
  164. /**************************** Layer II *************/
  165.  
  166. void II_decode_bitalloc(
  167.     Bit_stream_struc    *bs,
  168.     unsigned int     bit_alloc[2][SBLIMIT],
  169.     frame_params    *fr_ps)
  170. {
  171.     int         i, j;
  172.     int         stereo = fr_ps->stereo;
  173.     int         sblimit = fr_ps->sblimit;
  174.     int         jsbound = fr_ps->jsbound;
  175.     al_table    *alloc = fr_ps->alloc;
  176.  
  177.     for (i = 0; i < jsbound; i++)
  178.     for (j = 0; j < stereo; j++)
  179.         bit_alloc[j][i] = (char)getbits(bs, (*alloc)[i][0].bits);
  180.  
  181.     for (i = jsbound; i < sblimit; i++)        /* expand to 2 channels */
  182.     bit_alloc[0][i] = bit_alloc[1][i] =
  183.             (char)getbits(bs, (*alloc)[i][0].bits);
  184.  
  185.     for (i = sblimit; i < SBLIMIT; i++)
  186.     for (j = 0; j < stereo; j++)
  187.         bit_alloc[j][i] = 0;
  188. }
  189.  
  190. /**************************** Layer I *************/
  191.  
  192. void I_decode_bitalloc(
  193.     Bit_stream_struc *bs,
  194.     unsigned int bit_alloc[2][SBLIMIT],
  195.     frame_params *fr_ps)
  196. {
  197.     int i,j;
  198.     int stereo    = fr_ps->stereo;
  199.     /*int sblimit = fr_ps->sblimit;*/
  200.     int jsbound = fr_ps->jsbound;
  201.     int b;
  202.  
  203.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  204.         bit_alloc[j][i] = getbits(bs,4);
  205.     for (i=jsbound;i<SBLIMIT;i++) {
  206.         b = getbits(bs,4);
  207.         for (j=0;j<stereo;j++)
  208.             bit_alloc[j][i] = b;
  209.     }
  210. }
  211.  
  212. /*****************************************************************
  213.  *
  214.  * The following two functions implement the layer I and II
  215.  * format of scale factor extraction. Layer I involves reading
  216.  * 6 bit per subband as scale factor. Layer II requires reading
  217.  * first the scfsi which in turn indicate the number of scale factors
  218.  * transmitted.
  219.  *    Layer I : I_decode_scale
  220.  *   Layer II : II_decode_scale
  221.  *
  222.  ****************************************************************/
  223.  
  224. /************************** Layer I stuff ************************/
  225.  
  226. void I_decode_scale(
  227.     Bit_stream_struc *bs, unsigned int bit_alloc[2][SBLIMIT],
  228.     unsigned int scale_index[2][3][SBLIMIT], frame_params *fr_ps)
  229. {
  230.     int i,j;
  231.     int stereo = fr_ps->stereo;
  232.     /*int sblimit = fr_ps->sblimit;*/
  233.  
  234.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  235.         if (!bit_alloc[j][i])
  236.             scale_index[j][0][i] = SCALE_RANGE-1;
  237.         else                    /* 6 bit per scale factor */
  238.             scale_index[j][0][i] =  getbits(bs,6);
  239.  
  240. }
  241.  
  242. /*************************** Layer II stuff ***************************/
  243.  
  244. void II_decode_scale(
  245.     Bit_stream_struc    *bs,
  246.     unsigned int     scfsi[2][SBLIMIT],
  247.     unsigned int     bit_alloc[2][SBLIMIT],
  248.     unsigned int     scale_index[2][3][SBLIMIT],
  249.     frame_params    *fr_ps)
  250. {
  251.     int        i, j;
  252.     int        stereo = fr_ps->stereo;
  253.     int        sblimit = fr_ps->sblimit;
  254.  
  255. #ifdef OPTIMIZE   
  256.  
  257.     for (i = 0; i < sblimit; i++)
  258.     for (j = 0; j < stereo; j++) {        /* 2 bit scfsi */
  259.         if (bit_alloc[j][i])
  260.         scfsi[j][i] = (char)getbits(bs,2);
  261.         else
  262.         scfsi[j][i] = 0;
  263.     }
  264.  
  265.     for (i = 0; i < sblimit; i++) {
  266.     for (j = 0; j < stereo; j++) {
  267.         if (bit_alloc[j][i])   
  268.         switch (scfsi[j][i]) {
  269.         case 0 :
  270.             /* All three scale factors transmitted */
  271.             scale_index[j][0][i] = getbits(bs,6);
  272.             scale_index[j][1][i] = getbits(bs,6);
  273.             scale_index[j][2][i] = getbits(bs,6);
  274.             break;
  275.         case 1 :
  276.             /* Scale factor 1 & 3 transmitted */
  277.             scale_index[j][0][i] = scale_index[j][1][i] = getbits(bs,6);
  278.             scale_index[j][2][i] = getbits(bs,6);
  279.             break;
  280.         case 3 :
  281.             /* Scale factor 1 & 2 transmitted */
  282.             scale_index[j][0][i] = getbits(bs,6);
  283.             scale_index[j][1][i] = scale_index[j][2][i] = getbits(bs,6);
  284.             break;
  285.         case 2 :
  286.             /* Only one scale factor transmitted */
  287.             scale_index[j][0][i] = scale_index[j][1][i] =
  288.             scale_index[j][2][i] = getbits(bs,6);
  289.             break;
  290.         default :
  291.             break;
  292.         }
  293.         else {
  294.         scale_index[j][0][i] = scale_index[j][1][i] =
  295.             scale_index[j][2][i] = SCALE_RANGE-1;
  296.         }
  297.     }
  298.     }
  299.     for (i = sblimit; i < SBLIMIT; i++)
  300.     for (j = 0; j < stereo; j++)
  301.         scale_index[j][0][i] = scale_index[j][1][i] =
  302.             scale_index[j][2][i] = SCALE_RANGE - 1;
  303.  
  304. #else
  305.     /* Original code */
  306.  
  307.     for (i = 0; i < sblimit; i++)
  308.     for (j = 0; j < stereo; j++)        /* 2 bit scfsi */
  309.         if (bit_alloc[j][i])
  310.         scfsi[j][i] = (char)getbits(bs,2);
  311.  
  312.     for (i = sblimit; i < SBLIMIT; i++)
  313.     for (j = 0; j < stereo; j++)   
  314.         scfsi[j][i] = 0;
  315.  
  316.     for (i = 0; i < sblimit; i++) {
  317.     for (j = 0; j < stereo; j++) {
  318.         if (bit_alloc[j][i])   
  319.         switch (scfsi[j][i]) {
  320.         case 0 :
  321.             /* All three scale factors transmitted */
  322.             scale_index[j][0][i] = getbits(bs,6);
  323.             scale_index[j][1][i] = getbits(bs,6);
  324.             scale_index[j][2][i] = getbits(bs,6);
  325.             break;
  326.         case 1 :
  327.             /* Scale factor 1 & 3 transmitted */
  328.             scale_index[j][0][i] = scale_index[j][1][i] = getbits(bs,6);
  329.             scale_index[j][2][i] = getbits(bs,6);
  330.             break;
  331.         case 3 :
  332.             /* Scale factor 1 & 2 transmitted */
  333.             scale_index[j][0][i] = getbits(bs,6);
  334.             scale_index[j][1][i] = scale_index[j][2][i] = getbits(bs,6);
  335.             break;
  336.         case 2 :
  337.             /* Only one scale factor transmitted */
  338.             scale_index[j][0][i] = scale_index[j][1][i] =
  339.             scale_index[j][2][i] = getbits(bs,6);
  340.             break;
  341.         default :
  342.             break;
  343.         }
  344.         else {
  345.         scale_index[j][0][i] = scale_index[j][1][i] =
  346.             scale_index[j][2][i] = SCALE_RANGE-1;
  347.         }
  348.     }
  349.     }
  350.  
  351.     for (i = sblimit; i < SBLIMIT; i++)
  352.     for (j = 0; j < stereo; j++)
  353.         scale_index[j][0][i] = scale_index[j][1][i] =
  354.             scale_index[j][2][i] = SCALE_RANGE - 1;
  355. #endif /* OPTIMIZE */
  356. }
  357.  
  358. /**************************************************************
  359.  *
  360.  *   The following two routines take care of reading the
  361.  * compressed sample from the bit stream for both layer 1 and
  362.  * layer 2. For layer 1, read the number of bits as indicated
  363.  * by the bit_alloc information. For layer 2, if grouping is
  364.  * indicated for a particular subband, then the sample size has
  365.  * to be read from the bits_group and the merged samples has
  366.  * to be decompose into the three distinct samples. Otherwise,
  367.  * it is the same for as layer one.
  368.  *
  369.  **************************************************************/
  370.  
  371. /******************************* Layer I stuff ******************/
  372.  
  373. void I_buffer_sample(
  374.     Bit_stream_struc *bs,
  375.     unsigned int FAR sample[2][3][SBLIMIT],
  376.     unsigned int bit_alloc[2][SBLIMIT],
  377.     frame_params *fr_ps)
  378. {
  379.     int i,j,k;
  380.     int stereo = fr_ps->stereo;
  381.     /*int sblimit = fr_ps->sblimit;*/
  382.     int jsbound = fr_ps->jsbound;
  383.     unsigned int s;
  384.  
  385.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  386.         if ( (k = bit_alloc[j][i]) == 0)
  387.             sample[j][0][i] = 0;
  388.         else 
  389.             sample[j][0][i] = (unsigned int) getbits(bs,k+1);
  390.     for (i=jsbound;i<SBLIMIT;i++) {
  391.         if ( (k = bit_alloc[0][i]) == 0)
  392.             s = 0;
  393.         else 
  394.             s = (unsigned int)getbits(bs,k+1);
  395.         for (j=0;j<stereo;j++)
  396.             sample[j][0][i]    = s;
  397.     }
  398. }
  399.  
  400. /*************************** Layer II stuff ************************/
  401.  
  402. void II_buffer_sample(
  403.     Bit_stream_struc    *bs,
  404.     unsigned int FAR     sample[2][3][SBLIMIT],
  405.     unsigned int     bit_alloc[2][SBLIMIT],
  406.     frame_params    *fr_ps)
  407. {
  408.     int         i, j, m;
  409.     int         stereo = fr_ps->stereo;
  410.     int         sblimit = fr_ps->sblimit;
  411.     int         jsbound = fr_ps->jsbound;
  412.     al_table    *alloc = fr_ps->alloc;
  413.  
  414. #ifdef OPTIMIZE
  415.  
  416.     for (i = 0; i < sblimit; i++)
  417.     for (j = 0; j < ((i < jsbound) ? stereo : 1); j++) {
  418.         if (bit_alloc[j][i]) {
  419.  
  420.         /* Check for grouping in subband */
  421.         int k = (*alloc)[i][bit_alloc[j][i]].bits;
  422.  
  423.         if ((*alloc)[i][bit_alloc[j][i]].group == 3) {
  424.  
  425.             sample[j][0][i] = (unsigned int)getbits(bs, k);
  426.             sample[j][1][i] = (unsigned int)getbits(bs, k);
  427.             sample[j][2][i] = (unsigned int)getbits(bs, k);
  428.  
  429.         } else {
  430.             /* bit_alloc = 3, 5, 9 */
  431.             unsigned int nlevels, c;
  432.  
  433.             nlevels = (*alloc)[i][bit_alloc[j][i]].steps;
  434.             c = (unsigned int)getbits(bs, k);
  435.             /* Unrolled loop */
  436.             sample[j][0][i] = c % nlevels;
  437.             /* Assign both at once. Note that c is modified!! */
  438.             sample[j][1][i] = (c /= nlevels) % nlevels;
  439.             sample[j][2][i] = (c /= nlevels) % nlevels;
  440.         }
  441.         } else {
  442.         /* For no sample transmitted */
  443.         sample[j][0][i] = sample[j][1][i] = sample[j][2][i] = 0;
  444.         }
  445.         if (stereo == 2 && i >= jsbound) {    /* joint stereo: copy L to R */
  446.         sample[1][0][i] = sample[0][0][i];
  447.         sample[1][1][i] = sample[0][1][i];
  448.         sample[1][2][i] = sample[0][2][i];
  449.         }
  450.     }
  451.  
  452. #else
  453.     /* Original code */
  454.     int    k;
  455.  
  456.     for (i = 0; i < sblimit; i++)
  457.     for (j = 0; j < ((i<jsbound) ? stereo : 1); j++) {
  458.         if (bit_alloc[j][i]) {
  459.         /* Check for grouping in subband */
  460.         if ((*alloc)[i][bit_alloc[j][i]].group == 3)
  461.             for (m = 0; m < 3; m++) {
  462.             k = (*alloc)[i][bit_alloc[j][i]].bits;
  463.             sample[j][m][i] = (unsigned int)getbits(bs, k);
  464.             }
  465.         else {
  466.             /* bit_alloc = 3, 5, 9 */
  467.             unsigned int nlevels,
  468.                  c = 0;
  469.  
  470.             nlevels = (*alloc)[i][bit_alloc[j][i]].steps;
  471.             k = (*alloc)[i][bit_alloc[j][i]].bits;
  472.             c = (unsigned int)getbits(bs, k);
  473.             for (k = 0; k < 3; k++) {
  474.             sample[j][k][i] = c % nlevels;
  475.             c /= nlevels;
  476.             }
  477.         }
  478.         } else {
  479.         /* For no sample transmitted */
  480.         for (k = 0; k < 3; k++)
  481.             sample[j][k][i] = 0;
  482.         }
  483.         if (stereo == 2 && i>= jsbound)    /* joint stereo: copy L to R */
  484.         for (k = 0; k < 3; k++)
  485.             sample[1][k][i] = sample[0][k][i];
  486.     }
  487.  
  488.     for (i = sblimit; i < SBLIMIT; i++)
  489.     for (j = 0; j < stereo; j++)
  490.         for (k = 0; k < 3; k++)
  491.         sample[j][k][i] = 0;
  492. #endif /* OPTIMIZE */
  493. }      
  494.  
  495. /**************************************************************
  496.  *
  497.  *   Restore the compressed sample to a fractional number.
  498.  *   first complement the MSB of the sample
  499.  *    for layer I :
  500.  *    Use s = (s' + 2^(-nb+1) ) * 2^nb / (2^nb-1)
  501.  *   for Layer II :
  502.  *   Use the formula s = s' * c + d
  503.  *
  504.  **************************************************************/
  505.  
  506. static REAL c[17] = {    1.33333333333, 1.60000000000, 1.14285714286,
  507.             1.77777777777, 1.06666666666, 1.03225806452,
  508.             1.01587301587, 1.00787401575, 1.00392156863,
  509.             1.00195694716, 1.00097751711, 1.00048851979,
  510.             1.00024420024, 1.00012208522, 1.00006103888,
  511.             1.00003051851, 1.00001525902 };
  512.  
  513. static REAL d[17] = {    0.500000000, 0.500000000, 0.250000000, 0.500000000,
  514.             0.125000000, 0.062500000, 0.031250000, 0.015625000,
  515.             0.007812500, 0.003906250, 0.001953125, 0.0009765625,
  516.             0.00048828125, 0.00024414063, 0.00012207031,
  517.             0.00006103516, 0.00003051758 };
  518.  
  519. /************************** Layer II stuff ************************/
  520.  
  521.  
  522. #ifndef ASM_OPTIMIZE
  523.  
  524. void II_dequantize_sample(
  525.     unsigned int FAR     sample[2][3][SBLIMIT],
  526.     unsigned int     bit_alloc[2][SBLIMIT],
  527.     REAL FAR         fraction[2][3][SBLIMIT],
  528.     frame_params    *fr_ps)
  529. {
  530.     int         i, j, k;
  531.     int         stereo = fr_ps->stereo;
  532.     int         sblimit = fr_ps->sblimit;
  533.     al_table    *alloc = fr_ps->alloc;
  534.  
  535. #ifdef OPTIMIZE
  536.  
  537.     for (i = 0; i < sblimit; i++)
  538.     for (j = 0; j < 3; j++) {
  539.         for (k = 0; k < stereo; k++) {
  540.         REAL *f = &fraction[k][j][i];
  541.  
  542.         if (bit_alloc[k][i]) {
  543.             int  x = 0;
  544.             REAL r;
  545.  
  546.             /*-- Locate MSB in the sample --
  547.              * u is set outside loop to speed up the loop */
  548.             unsigned int u = (*alloc)[i][bit_alloc[k][i]].steps;
  549.             while ((1L<<x) < u)
  550.             x++;
  551.  
  552.             /*-- MSB inversion --*/
  553.             u = sample[k][j][i];    // read sample once
  554.  
  555.             x = 1L << (x - 1);        // for MSB test
  556.  
  557.             if ((u & x))
  558.             r = 0.0;        // MSB is set
  559.             else
  560.             r = -1.0;        // MSB is clear
  561.  
  562.             /*-- Form a 2's complement sample --
  563.             // This is a slow operation! */
  564.             r += (REAL)(u & (x - 1)) / (REAL)x;
  565.             /*        o r
  566.             // Call the optimized assembly routine
  567.             //cmpl(&r, u, x); */
  568.  
  569.             u = (*alloc)[i][bit_alloc[k][i]].quant;
  570.  
  571.             //-- Dequantize the sample --
  572.             r += d[u];
  573.             r *= c[u];
  574.             *f = r;        // destination touched only once
  575.  
  576.         } else
  577.             *f = 0.0;   
  578.         }
  579.     }
  580.  
  581. #else
  582.     // Original code
  583.     int x;
  584.  
  585.     for (i = 0; i < sblimit; i++)
  586.     for (j = 0; j < 3; j++)
  587.         for (k = 0; k < stereo; k++)
  588.         if (bit_alloc[k][i]) {
  589.             /* Locate MSB in the sample */
  590.             x = 0;
  591. #ifndef MS_DOS
  592.             while ((1L<<x) < (*alloc)[i][bit_alloc[k][i]].steps)
  593.             x++;
  594. #else
  595.             /* Microsoft C thinks an int is a short */
  596.             while (( (unsigned long) (1L<<(long)x) <
  597.                 (unsigned long)((*alloc)[i][bit_alloc[k][i]].steps))
  598.                 && ( x < 16) )
  599.             x++;
  600. #endif
  601.             /* MSB inversion */
  602.             if (((sample[k][j][i] >> x-1) & 1) == 1)
  603.             fraction[k][j][i] = 0.0;
  604.             else
  605.             fraction[k][j][i] = -1.0;
  606.  
  607.             /* Form a 2's complement sample */
  608.             fraction[k][j][i] +=
  609.             (REAL)(sample[k][j][i] & ((1<<x-1)-1)) /
  610.             (REAL)(1L<<x-1);
  611.  
  612.             /* Dequantize the sample */
  613.             fraction[k][j][i] += d[(*alloc)[i][bit_alloc[k][i]].quant];
  614.             fraction[k][j][i] *= c[(*alloc)[i][bit_alloc[k][i]].quant];
  615.         } else
  616.             fraction[k][j][i] = 0.0;   
  617.    
  618.     for (i = sblimit; i < SBLIMIT; i++)
  619.     for (j = 0; j < 3; j++)
  620.         for (k = 0; k < stereo; k++)
  621.         fraction[k][j][i] = 0.0;
  622.  
  623. #endif // OPTIMIZE
  624. }
  625. #endif // ASM_OPTIMIZE
  626.  
  627.  
  628. /***************************** Layer I stuff ***********************/
  629.  
  630. void I_dequantize_sample(
  631.     unsigned int FAR sample[2][3][SBLIMIT],
  632.     REAL FAR fraction[2][3][SBLIMIT],
  633.     unsigned int bit_alloc[2][SBLIMIT],
  634.     frame_params *fr_ps)
  635. {
  636.     int i, nb, k;
  637.     int stereo = fr_ps->stereo;
  638.     /*int sblimit = fr_ps->sblimit;*/
  639.  
  640.     for (i=0; i<SBLIMIT; i++)
  641.     for (k=0; k<stereo; k++)
  642.         if (bit_alloc[k][i]) {
  643.         nb = bit_alloc[k][i] + 1;
  644.         if (((sample[k][0][i] >> nb-1) & 1) == 1)
  645.             fraction[k][0][i] = 0.0;
  646.         else
  647.             fraction[k][0][i] = -1.0;
  648.  
  649.         fraction[k][0][i] += (sample[k][0][i] & ((1<<nb-1)-1)) /
  650.                      (1L<<nb-1);
  651.  
  652.         fraction[k][0][i] =
  653.                 (fraction[k][0][i] + 1.0 / (1L<<nb-1)) *
  654.                 (1L<<nb) / ((1L<<nb)-1);
  655.         } else
  656.         fraction[k][0][i] = 0.0;
  657. }
  658.  
  659. /************************************************************
  660.  *
  661.  *   Restore the original value of the sample ie multiply
  662.  *    the fraction value by its scalefactor.
  663.  *
  664.  ************************************************************/
  665.  
  666. /************************* Layer II Stuff **********************/
  667.  
  668. void II_denormalize_sample(
  669.     REAL FAR         fraction[2][3][SBLIMIT],
  670.     unsigned int     scale_index[2][3][SBLIMIT],
  671.     frame_params    *fr_ps,
  672.     int             x)
  673. {
  674.     int i, j;
  675.     int stereo = fr_ps->stereo;
  676.     int sblimit = fr_ps->sblimit;
  677.  
  678. #ifdef OPTIMIZE
  679.  
  680.     for (i = 0; i < sblimit; i++)
  681.     for (j = 0; j < stereo; j++) {
  682.         REAL m = multiple[scale_index[j][x][i]];
  683.  
  684.         fraction[j][0][i] *= m;
  685.         fraction[j][1][i] *= m;
  686.         fraction[j][2][i] *= m;
  687.     }
  688.  
  689. #else
  690.  
  691.     for (i = 0; i < sblimit; i++)
  692.     for (j = 0; j < stereo; j++) {
  693.         fraction[j][0][i] *= multiple[scale_index[j][x][i]];
  694.         fraction[j][1][i] *= multiple[scale_index[j][x][i]];
  695.         fraction[j][2][i] *= multiple[scale_index[j][x][i]];
  696.     }
  697. #endif // OPTIMIZE
  698. }
  699.  
  700. /**************************** Layer I stuff ******************************/
  701.  
  702. void I_denormalize_sample(
  703.     REAL FAR fraction[2][3][SBLIMIT],
  704.     unsigned int scale_index[2][3][SBLIMIT],
  705.     frame_params *fr_ps)
  706. {
  707.     int i, j;
  708.     int stereo = fr_ps->stereo;
  709.     int sblimit = fr_ps->sblimit;
  710.  
  711.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  712.         fraction[j][0][i] *= multiple[scale_index[j][0][i]];
  713. }
  714.  
  715. /*****************************************************************
  716.  *
  717.  * The following are the subband synthesis routines. They apply
  718.  * to both layer I and layer II stereo or mono. The user has to
  719.  * decide what parameters are to be passed to the routines.
  720.  *
  721.  ***************************************************************/
  722.  
  723. /*************************************************************
  724.  *
  725.  *   Pass the subband sample through the synthesis window
  726.  *
  727.  **************************************************************/
  728.  
  729. /* create in synthesis filter */
  730.  
  731. void create_syn_filter(REAL FAR filter[64][SBLIMIT])
  732. {
  733.     int i, k;
  734. #ifdef NO_MODFF
  735.     double temp;
  736. #endif
  737.  
  738.     for (i = 0; i < 64; i++)
  739.     for (k = 0; k < 32; k++) {
  740. #ifdef FSINGLE
  741.         filter[i][k] = 1e9 * cos((PI64*i+PI4)*(2.0*k+1.0));
  742.         if (filter[i][k] >= 0) {
  743. #ifdef NO_MODFF
  744.         modf(filter[i][k]+0.5, &temp);
  745.         filter[i][k] = (REAL) temp;
  746. #else
  747.         modff(filter[i][k]+0.5, &filter[i][k]);
  748. #endif /* NO_MODFF */
  749.         } else {
  750. #ifdef NO_MODFF
  751.         modf(filter[i][k]-0.5, &temp);
  752.         filter[i][k] = (REAL) temp;
  753. #else
  754.         modff(filter[i][k]-0.5, &filter[i][k]);
  755. #endif /* NO_MODFF */
  756.         }
  757. #else /* FSINGLE */
  758.         filter[i][k] = 1e9*cos((PI64*i+PI4)*(2*k+1));
  759.         if (filter[i][k] >= 0)
  760.         modf(filter[i][k]+0.5, &filter[i][k]);
  761.         else
  762.         modf(filter[i][k]-0.5, &filter[i][k]);
  763. #endif
  764.         filter[i][k] *= 1e-9;
  765.     }
  766. }
  767.  
  768.  
  769. #ifndef BUILTIN_TABLES
  770.  
  771. /***************************************************************
  772.  *
  773.  *   Window the restored sample
  774.  *
  775.  ***************************************************************/
  776.  
  777. /* read in synthesis window */
  778.  
  779. void read_syn_window(REAL FAR window[HAN_SIZE])
  780. {
  781.     int      i, j[4];
  782.     FILE *fp;
  783.     REAL  f[4];
  784.     char  t[150];
  785.  
  786.     if (!(fp = OpenTableFile("dewindow") )) {
  787.         fprintf(stderr, "Please check synthesis window table 'dewindow'\n");
  788.         exit(1);
  789.     }
  790.     for (i=0; i<512; i+=4) {
  791.         fgets(t, 150, fp);
  792. #ifdef FSINGLE
  793.     sscanf(t,"D[%d] = %f D[%d] = %f D[%d] = %f D[%d] = %f\n",
  794.         j, f, j+1, f+1, j+2, f+2, j+3, f+3);
  795. #else
  796.         sscanf(t,"D[%d] = %lf D[%d] = %lf D[%d] = %lf D[%d] = %lf\n",
  797.         j, f, j+1, f+1, j+2, f+2, j+3, f+3);
  798. #endif
  799.         if (i == j[0]) {
  800.             window[i] = f[0];
  801.             window[i+1] = f[1];
  802.             window[i+2] = f[2];
  803.             window[i+3] = f[3];
  804.         }
  805.         else {
  806.             fprintf(stderr, "Check index in synthesis window table\n");
  807.             exit(1);
  808.         }
  809.         fgets(t, 150, fp);
  810.     }
  811.     fclose(fp);
  812. }
  813.  
  814. #endif // BUILTIN_TABLES
  815.  
  816.  
  817. #if (!defined INT_MATH && !defined ASM_OPTIMIZE)
  818.  
  819. int SubBandSynthesis(
  820.     REAL    *bandPtr,
  821.     int         channel,
  822.     short    *samples)
  823. {
  824.     int             i, j, k;
  825.     REAL        *bufOffsetPtr, sum;
  826.     static int         init = 1;
  827.     typedef REAL     NN[64][32];
  828.     static NN FAR    *filter;
  829.     typedef REAL     BB[2][2*HAN_SIZE];
  830.     static BB FAR    *buf;
  831.     static int         bufOffset[2] = {64,64};
  832.     static REAL FAR    *window;
  833.     int             clip = 0;   // count & return how many samples clipped
  834.     long         foo;
  835. #ifdef OPTIMIZE
  836.     REAL        *filterp;
  837.     REAL        *bandp;
  838. #endif
  839.  
  840.     if (init == 1) {
  841.         buf = (BB FAR *) mem_alloc(sizeof(BB),"BB");
  842.         filter = (NN FAR *) mem_alloc(sizeof(NN), "NN");
  843.         create_syn_filter(*filter);
  844.         window = (REAL FAR *) mem_alloc(sizeof(REAL) * HAN_SIZE, "WIN");
  845.         read_syn_window(window);
  846.     init = 0;
  847.  
  848.     /* Verify even address alignment */
  849.     if ((int)buf & 1)
  850.         fprintf(stderr, "buf: bad alignment");
  851.     if ((int)filter & 1)
  852.         fprintf(stderr, "filter: bad alignment");
  853.     if ((int)window & 1)
  854.         fprintf(stderr, "window: bad alignment");
  855.     }
  856.  
  857.     bufOffset[channel] = (bufOffset[channel] - 64) & 0x3ff;
  858.     bufOffsetPtr = &((*buf)[channel][bufOffset[channel]]);
  859.  
  860. #ifdef OPTIMIZE
  861.     filterp = (REAL *)((*filter)[0]);
  862.     bandp = bandPtr;
  863.  
  864.     i = 64;
  865.  
  866.     while (i--) {
  867.     *bufOffsetPtr++ =
  868.         bandp[0] * filterp[0] +
  869.         bandp[1] * filterp[1] +
  870.         bandp[2] * filterp[2] +
  871.         bandp[3] * filterp[3] +
  872.         bandp[4] * filterp[4] +
  873.         bandp[5] * filterp[5] +
  874.         bandp[6] * filterp[6] +
  875.         bandp[7] * filterp[7] +
  876.         bandp[8] * filterp[8] +
  877.         bandp[9] * filterp[9] +
  878.         bandp[10] * filterp[10] +
  879.         bandp[11] * filterp[11] +
  880.         bandp[12] * filterp[12] +
  881.         bandp[13] * filterp[13] +
  882.         bandp[14] * filterp[14] +
  883.         bandp[15] * filterp[15] +
  884.         bandp[16] * filterp[16] +
  885.         bandp[17] * filterp[17] +
  886.         bandp[18] * filterp[18] +
  887.         bandp[19] * filterp[19] +
  888.         bandp[20] * filterp[20] +
  889.         bandp[21] * filterp[21] +
  890.         bandp[22] * filterp[22] +
  891.         bandp[23] * filterp[23] +
  892.         bandp[24] * filterp[24] +
  893.         bandp[25] * filterp[25] +
  894.         bandp[26] * filterp[26] +
  895.         bandp[27] * filterp[27] +
  896.         bandp[28] * filterp[28] +
  897.         bandp[29] * filterp[29] +
  898.         bandp[30] * filterp[30] +
  899.         bandp[31] * filterp[31];
  900.     filterp += 32;
  901.     }
  902.  
  903. #else
  904.  
  905.     for (i = 0; i < 64; i++) {
  906.     sum = 0.0;
  907.     for (k = 0; k < 32; k++)
  908.         sum += bandPtr[k] * (*filter)[i][k];
  909.     bufOffsetPtr[i] = sum;
  910.     }
  911. #endif // OPTIMIZE
  912.  
  913. #ifdef DETECT_CLIP
  914.     /* Detects and "smooths" clipping
  915.      *
  916.      *  S(i,j) = D(j+32i) * U(j+32i+((i+1)>>1)*64)
  917.      *  samples(i,j) = MWindow(j+32i) * bufPtr(j+32i+((i+1)>>1)*64)
  918.      */
  919.     for (j = 0; j < 32; j++) {
  920.     /* Unroll loop
  921.      * Here we multiply every subband by the appropriate scale factor
  922.      * and add 'em all up, so we get one sample. */
  923.     sum = window[j] * (*buf)[channel][(j + bufOffset[channel]) & 0x3ff] +
  924.         window[j + 32] * (*buf)[channel][(j + 96 + bufOffset[channel]) & 0x3ff] +
  925.         window[j + 64] * (*buf)[channel][(j + 128 + bufOffset[channel]) & 0x3ff] +
  926.         window[j + 96] * (*buf)[channel][(j + 224 + bufOffset[channel]) & 0x3ff] +
  927.         window[j + 128] * (*buf)[channel][(j + 256 + bufOffset[channel]) & 0x3ff] +
  928.         window[j + 160] * (*buf)[channel][(j + 352 + bufOffset[channel]) & 0x3ff] +
  929.         window[j + 192] * (*buf)[channel][(j + 384 + bufOffset[channel]) & 0x3ff] +
  930.         window[j + 224] * (*buf)[channel][(j + 480 + bufOffset[channel]) & 0x3ff] +
  931.         window[j + 256] * (*buf)[channel][(j + 512 + bufOffset[channel]) & 0x3ff] +
  932.         window[j + 288] * (*buf)[channel][(j + 608 + bufOffset[channel]) & 0x3ff] +
  933.         window[j + 320] * (*buf)[channel][(j + 640 + bufOffset[channel]) & 0x3ff] +
  934.         window[j + 352] * (*buf)[channel][(j + 736 + bufOffset[channel]) & 0x3ff] +
  935.         window[j + 384] * (*buf)[channel][(j + 768 + bufOffset[channel]) & 0x3ff] +
  936.         window[j + 416] * (*buf)[channel][(j + 864 + bufOffset[channel]) & 0x3ff] +
  937.         window[j + 448] * (*buf)[channel][(j + 896 + bufOffset[channel]) & 0x3ff] +
  938.         window[j + 480] * (*buf)[channel][(j + 992 + bufOffset[channel]) & 0x3ff];
  939.  
  940.     /*
  941.      * Casting truncates towards zero for both positive and negative
  942.      * numbers, the result is cross-over distortion,  1995-07-12 shn
  943.      */
  944.     if (sum > 0) {
  945.         foo = (long)(sum * SCALE + 0.5);
  946.     } else {
  947.         foo = (long)(sum * SCALE - 0.5);
  948.     } 
  949.  
  950.     if (foo >= (long)SCALE) {
  951.         samples[j] = (short)(SCALE-1);
  952.         ++clip;
  953.     } else if (foo < (long)-SCALE) {
  954.         samples[j] = (short)(-SCALE);
  955.         ++clip;
  956.     } else
  957.         samples[j] = (short)foo;
  958.     }
  959.  
  960. #else /* !DETECT_CLIP */
  961.  
  962.     /* Faster, but does not detect clipping
  963.      *
  964.      *  S(i,j) = D(j+32i) * U(j+32i+((i+1)>>1)*64)
  965.      *  samples(i,j) = MWindow(j+32i) * bufPtr(j+32i+((i+1)>>1)*64)
  966.      */
  967.     for (j = 0; j < 32; j++) {
  968.     sum = 0.0;
  969.  
  970.     for (i = 0; i < 16; i++) {
  971.         k = j + (i << 5);
  972.         sum += window[k] * (*buf)[channel]
  973.             [((k + (((i+1) >> 1) << 6)) + bufOffset[channel]) & 0x3ff];
  974.     }
  975.     samples[j] = sum * SCALE;
  976.     }
  977. #endif
  978.  
  979.     return clip;
  980. }
  981. #endif    // #if (defined INT_MATH && !defined ASM_OPTIMIZE)
  982.  
  983.  
  984. // Shift left and right channels for AIFF output
  985. static void swapWordsInLong(short *loc, int words)
  986. {
  987.     int             i;
  988.     unsigned short     dst0, dst1;
  989.     unsigned short    *src = (unsigned short*)loc;
  990.  
  991.     for (i = 0; i < words; i += 2) {
  992.     dst0 = src[0];
  993.     dst1 = src[1];
  994.     *src++ = dst1;
  995.     *src++ = dst0;
  996.     }
  997. }
  998.  
  999.  
  1000. void out_fifo(
  1001.     short FAR         pcm_sample[2][SSLIMIT][SBLIMIT],
  1002.     int             num,
  1003.     frame_params    *fr_ps,
  1004.     int             done,
  1005.     FILE        *outFile,
  1006.     unsigned long    *psampFrames)
  1007. {
  1008. #   define        BUFFERSIZE 8192        // * 2 bytes
  1009. // 4090 is the maximum size that Ultra1/Solaris 2.5 accepts
  1010. #   define        SMALLBUFSIZE 4090    // * 2 bytes
  1011.     /*int            bufferSize = BUFFERSIZE;*/
  1012.     int            i, j, l;
  1013.     int            stereo = fr_ps->stereo;
  1014.     /*int            sblimit = fr_ps->sblimit;*/
  1015.     static short int    outsamp[BUFFERSIZE];
  1016.     static long        k = 0;
  1017.  
  1018.   /* If "smallAudioBuffer" is set, we're on an Ultra/Solaris 2.5. Reduce
  1019.    * size of the output buffer to prevent write() from hanging. */
  1020.   if (smallAudioBuffer) {
  1021.  
  1022.     if (!done) {
  1023.     for (i = 0; i < num; i++)
  1024.         for (j = 0; j < SBLIMIT; j++) {    // SBLIMIT = 32
  1025.         (*psampFrames)++;
  1026.         for (l = 0; l < stereo; l++) {
  1027.             if (!(k % SMALLBUFSIZE) && k) {
  1028. #ifndef SOLARIS
  1029.             /*
  1030.              * Samples are big-endian. If this is a little-endian
  1031.              * machine we must swap
  1032.              */
  1033.             if ( NativeByteOrder == order_unknown ) {
  1034.                 NativeByteOrder = DetermineByteOrder();
  1035.                 if ( NativeByteOrder == order_unknown ) {
  1036.                 fprintf(stderr, "byte order not determined\n" );
  1037.                 exit(1);
  1038.                 }
  1039.             }
  1040.             if ( NativeByteOrder == order_littleEndian )
  1041.                 SwapBytesInWords(outsamp, SMALLBUFSIZE);
  1042. #endif
  1043.             if (Arguments.write_to_file) {
  1044.                 if (stereo == 2) {
  1045.                 // Swap channels for AIFF file
  1046.                 swapWordsInLong(outsamp, SMALLBUFSIZE);
  1047.                 }
  1048.                 fwrite(outsamp, 2, SMALLBUFSIZE, outFile);
  1049.             } else
  1050.                 write(audiofd, outsamp, SMALLBUFSIZE * 2);
  1051.             k = 0;
  1052.             }
  1053.             outsamp[k++] = pcm_sample[l][i][j];
  1054.         }
  1055.         }
  1056.  
  1057.     } else { // if done
  1058.  
  1059.     if (Arguments.write_to_file) {
  1060.         if (stereo == 2) {
  1061.         // Swap channels for AIFF file
  1062.         swapWordsInLong(outsamp, (int)k);
  1063.         }
  1064.         fwrite(outsamp, 2, (int)k, outFile);
  1065.     } else
  1066.         write(audiofd, outsamp, (int)k * 2);
  1067.     k = 0;
  1068.     }
  1069.  
  1070.   } else {
  1071.     // Bigger output buffer
  1072.  
  1073.     if (!done) {
  1074.         for (i = 0; i < num; i++)
  1075.         for (j = 0; j < SBLIMIT; j++) {    // SBLIMIT = 32
  1076.         (*psampFrames)++;
  1077.         for (l = 0; l < stereo; l++) {
  1078.             if (!(k & (BUFFERSIZE-1)) && k) {
  1079. #ifndef SOLARIS
  1080.             /*
  1081.              * Samples are big-endian. If this is a little-endian
  1082.              * machine we must swap
  1083.              */
  1084.             if ( NativeByteOrder == order_unknown ) {
  1085.                 NativeByteOrder = DetermineByteOrder();
  1086.                 if ( NativeByteOrder == order_unknown ) {
  1087.                 fprintf(stderr, "byte order not determined\n" );
  1088.                 exit(1);
  1089.                 }
  1090.             }
  1091.             if ( NativeByteOrder == order_littleEndian )
  1092.                 SwapBytesInWords(outsamp, BUFFERSIZE);
  1093. #endif
  1094.             if (Arguments.write_to_file) {
  1095.                 if (stereo == 2) {
  1096.                 // Swap channels for AIFF file
  1097.                 swapWordsInLong(outsamp, BUFFERSIZE);
  1098.                 }
  1099.                 fwrite(outsamp, 2, BUFFERSIZE, outFile);
  1100.             } else
  1101.                 write(audiofd, outsamp, BUFFERSIZE * 2);
  1102.             k = 0;
  1103.             }
  1104.             outsamp[k++] = pcm_sample[l][i][j];
  1105.         }
  1106.         }
  1107.  
  1108.     } else { // if done
  1109.  
  1110.     if (Arguments.write_to_file) {
  1111.         if (stereo == 2) {
  1112.         // Swap channels for AIFF file
  1113.         swapWordsInLong(outsamp, (int)k);
  1114.         }
  1115.         fwrite(outsamp, 2, (int)k, outFile);
  1116.     } else
  1117.         write(audiofd, outsamp, (int)k * 2);
  1118.     k = 0;
  1119.     }
  1120.   }
  1121. }
  1122.  
  1123.  
  1124. void buffer_CRC(Bit_stream_struc *bs, unsigned int *old_crc)
  1125. {
  1126.     *old_crc = getbits(bs, 16);
  1127. }
  1128.  
  1129.  
  1130. void recover_CRC_error(
  1131.     short FAR       pcm_sample[2][SSLIMIT][SBLIMIT],
  1132.     int           error_count,
  1133.     frame_params  *fr_ps,
  1134.     FILE      *outFile,
  1135.     unsigned long *psampFrames)
  1136. {
  1137.     int         stereo = fr_ps->stereo;
  1138.     int         num, done, i;
  1139.     int         samplesPerFrame, samplesPerSlot;
  1140.     layer    *hdr = fr_ps->header;
  1141.     long     offset;
  1142.     short    *temp;
  1143.  
  1144.     num = 3;
  1145.     if (hdr->lay == 1)
  1146.     num = 1;
  1147.  
  1148.     samplesPerSlot = SBLIMIT * num * stereo;
  1149.     samplesPerFrame = samplesPerSlot * 32;
  1150.  
  1151.     if (error_count == 1) {     /* replicate previous error_free frame */
  1152.         done = 1;
  1153.         /* Flush out fifo */
  1154.         out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  1155.         /* Go back to the beginning of the previous frame */
  1156.         offset = sizeof(short int) * samplesPerFrame;
  1157.         fseek(outFile, -offset, SEEK_CUR);
  1158.         done = 0;
  1159.         for (i = 0; i < SCALE_BLOCK; i++) {
  1160.             fread(pcm_sample, 2, samplesPerSlot, outFile);
  1161.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  1162.         }
  1163.  
  1164.     } else {                       /* mute the frame */
  1165.  
  1166.         temp = (short*)pcm_sample;
  1167.         done = 0;
  1168.         for (i = 0; i < 2*3*SBLIMIT; i++)
  1169.             *temp++ = MUTE;     /* MUTE value is in decoder.h */
  1170.         for (i = 0; i < SCALE_BLOCK; i++)
  1171.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  1172.     }
  1173. }
  1174.  
  1175. /************************* Layer III routines **********************/
  1176.  
  1177. void III_get_side_info(
  1178.     Bit_stream_struc *bs,
  1179.     III_side_info_t *si,
  1180.     frame_params *fr_ps)
  1181. {
  1182.    int    ch, gr, i;
  1183.    int    stereo = fr_ps->stereo;
  1184.    
  1185.    if (fr_ps->header->version != MPEG_PHASE2_LSF) {
  1186.     si->main_data_begin = getbits(bs, 9);
  1187.     if (stereo == 1)
  1188.         si->private_bits = getbits(bs,5);
  1189.     else
  1190.         si->private_bits = getbits(bs,3);
  1191.  
  1192.        for (ch = 0; ch < stereo; ch++)
  1193.         for (i = 0; i < 4; i++)
  1194.         si->ch[ch].scfsi[i] = get1bit(bs);
  1195.  
  1196.     for (gr = 0; gr < 2 ; gr++) {
  1197.         for (ch = 0; ch < stereo; ch++) {
  1198.         si->ch[ch].gr[gr].part2_3_length = getbits(bs, 12);
  1199.         si->ch[ch].gr[gr].big_values = getbits(bs, 9);
  1200.         si->ch[ch].gr[gr].global_gain = getbits(bs, 8);
  1201.         si->ch[ch].gr[gr].scalefac_compress = getbits(bs, 4);
  1202.         si->ch[ch].gr[gr].window_switching_flag = get1bit(bs);
  1203.         if (si->ch[ch].gr[gr].window_switching_flag) {
  1204.             si->ch[ch].gr[gr].block_type = getbits(bs, 2);
  1205.             si->ch[ch].gr[gr].mixed_block_flag = get1bit(bs);
  1206.             for (i = 0; i < 2; i++)
  1207.             si->ch[ch].gr[gr].table_select[i] = getbits(bs, 5);
  1208.             for (i = 0; i < 3; i++)
  1209.             si->ch[ch].gr[gr].subblock_gain[i] = getbits(bs, 3);
  1210.             /*
  1211.              * Set region_count parameters since they are
  1212.              * implicit in this case.
  1213.              */
  1214.             if (si->ch[ch].gr[gr].block_type == 0) {
  1215.             fprintf(stderr, "Side info bad: block_type 0 in split block\n");
  1216.             exit(0);
  1217.             } else if (si->ch[ch].gr[gr].block_type == 2
  1218.                 && si->ch[ch].gr[gr].mixed_block_flag == 0)
  1219.             si->ch[ch].gr[gr].region0_count = 8; /* MI 9; */
  1220.             else
  1221.             si->ch[ch].gr[gr].region0_count = 7; /* MI 8; */
  1222.             si->ch[ch].gr[gr].region1_count =
  1223.                 20 - si->ch[ch].gr[gr].region0_count;
  1224.         } else {
  1225.             for (i = 0; i < 3; i++)
  1226.             si->ch[ch].gr[gr].table_select[i] = getbits(bs, 5);
  1227.             si->ch[ch].gr[gr].region0_count = getbits(bs, 4);
  1228.             si->ch[ch].gr[gr].region1_count = getbits(bs, 3);
  1229.             si->ch[ch].gr[gr].block_type = 0;
  1230.         }       
  1231.         si->ch[ch].gr[gr].preflag = get1bit(bs);
  1232.         si->ch[ch].gr[gr].scalefac_scale = get1bit(bs);
  1233.         si->ch[ch].gr[gr].count1table_select = get1bit(bs);
  1234.         }
  1235.     }
  1236.  
  1237.     } else {    /* Layer 3 LSF */
  1238.  
  1239.     si->main_data_begin = getbits(bs, 8);
  1240.     if (stereo == 1)
  1241.          si->private_bits = getbits(bs,1);
  1242.     else
  1243.         si->private_bits = getbits(bs,2);
  1244.  
  1245.     for (gr = 0; gr < 1 ; gr++) {
  1246.         for (ch = 0; ch < stereo; ch++) {
  1247.         si->ch[ch].gr[gr].part2_3_length = getbits(bs, 12);
  1248.         si->ch[ch].gr[gr].big_values = getbits(bs, 9);
  1249.         si->ch[ch].gr[gr].global_gain = getbits(bs, 8);
  1250.         si->ch[ch].gr[gr].scalefac_compress = getbits(bs, 9);
  1251.         si->ch[ch].gr[gr].window_switching_flag = get1bit(bs);
  1252.         if (si->ch[ch].gr[gr].window_switching_flag) {
  1253.             si->ch[ch].gr[gr].block_type = getbits(bs, 2);
  1254.             si->ch[ch].gr[gr].mixed_block_flag = get1bit(bs);
  1255.             for (i = 0; i < 2; i++)
  1256.             si->ch[ch].gr[gr].table_select[i] = getbits(bs, 5);
  1257.             for (i = 0; i < 3; i++)
  1258.             si->ch[ch].gr[gr].subblock_gain[i] = getbits(bs, 3);
  1259.             /*
  1260.              * Set region_count parameters since they are
  1261.              * implicit in this case.
  1262.              */
  1263.             if (si->ch[ch].gr[gr].block_type == 0) {
  1264.             fprintf(stderr, "Side info bad: block_type 0 in split block\n");
  1265.             exit(0);
  1266.             } else if (si->ch[ch].gr[gr].block_type == 2
  1267.                 && si->ch[ch].gr[gr].mixed_block_flag == 0)
  1268.             si->ch[ch].gr[gr].region0_count = 8; /* MI 9; */
  1269.             else
  1270.             si->ch[ch].gr[gr].region0_count = 7; /* MI 8; */
  1271.             si->ch[ch].gr[gr].region1_count =
  1272.                 20 - si->ch[ch].gr[gr].region0_count;
  1273.         } else {
  1274.             for (i = 0; i < 3; i++)
  1275.             si->ch[ch].gr[gr].table_select[i] = getbits(bs, 5);
  1276.             si->ch[ch].gr[gr].region0_count = getbits(bs, 4);
  1277.             si->ch[ch].gr[gr].region1_count = getbits(bs, 3);
  1278.             si->ch[ch].gr[gr].block_type = 0;
  1279.         }      
  1280.  
  1281.         si->ch[ch].gr[gr].scalefac_scale = get1bit(bs);
  1282.         si->ch[ch].gr[gr].count1table_select = get1bit(bs);
  1283.         }
  1284.     }
  1285.     }
  1286. }
  1287.  
  1288.  
  1289. struct {
  1290.    int l[5];
  1291.    int s[3];} sfbtable = {{0, 6, 11, 16, 21},
  1292.                           {0, 6, 12}};
  1293.                          
  1294. int slen[2][16] = {{0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
  1295.                    {0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}};
  1296. struct  {
  1297.    int l[23];
  1298.    int s[14];} sfBandIndex[6] =
  1299.     {{{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  1300.      {0,4,8,12,18,24,32,42,56,74,100,132,174,192}},
  1301.     {{0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
  1302.      {0,4,8,12,18,26,36,48,62,80,104,136,180,192}},
  1303.     {{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
  1304.      {0,4,8,12,18,26,36,48,62,80,104,134,174,192}},
  1305.  
  1306.     {{0,4,8,12,16,20,24,30,36,44,52,62,74,90,110,134,162,196,238,288,342,418,576},
  1307.      {0,4,8,12,16,22,30,40,52,66,84,106,136,192}},
  1308.     {{0,4,8,12,16,20,24,30,36,42,50,60,72,88,106,128,156,190,230,276,330,384,576},
  1309.      {0,4,8,12,16,22,28,38,50,64,80,100,126,192}},
  1310.     {{0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
  1311.      {0,4,8,12,16,22,30,42,58,78,104,138,180,192}}};
  1312.  
  1313.  
  1314.  
  1315. void III_get_scale_factors(
  1316.     III_scalefac_t    *scalefac,
  1317.     III_side_info_t    *si,
  1318.     int             gr,
  1319.     int             ch,
  1320.     frame_params    *fr_ps)
  1321. {
  1322.     int             sfb, i, window;
  1323.     struct gr_info_s    *gr_info = &(si->ch[ch].gr[gr]);
  1324.  
  1325.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) { 
  1326.  
  1327.     if (gr_info->mixed_block_flag) {    /* MIXED */ /* NEW - ag 11/25 */
  1328.         for (sfb = 0; sfb < 8; sfb++)
  1329.         (*scalefac)[ch].l[sfb] =
  1330.             hgetbits(slen[0][gr_info->scalefac_compress]);
  1331.         for (sfb = 3; sfb < 6; sfb++)
  1332.         for (window=0; window<3; window++)
  1333.             (*scalefac)[ch].s[window][sfb] =
  1334.             hgetbits(slen[0][gr_info->scalefac_compress]);
  1335.         for (sfb = 6; sfb < 12; sfb++)
  1336.         for (window=0; window<3; window++)
  1337.             (*scalefac)[ch].s[window][sfb] =
  1338.             hgetbits(slen[1][gr_info->scalefac_compress]);
  1339.         for (sfb=12,window=0; window<3; window++)
  1340.         (*scalefac)[ch].s[window][sfb] = 0;
  1341.  
  1342.     } else {                /* SHORT*/
  1343.  
  1344.         for (i=0; i<2; i++) 
  1345.         for (sfb = sfbtable.s[i]; sfb < sfbtable.s[i+1]; sfb++)
  1346.             for (window=0; window<3; window++)
  1347.             (*scalefac)[ch].s[window][sfb] =
  1348.                 hgetbits(slen[i][gr_info->scalefac_compress]);
  1349.         for (sfb=12,window=0; window<3; window++)
  1350.         (*scalefac)[ch].s[window][sfb] = 0;
  1351.     }
  1352.  
  1353.     } else {   /* LONG types 0,1,3 */
  1354.  
  1355.     for (i=0; i<4; i++) {
  1356.         if ((si->ch[ch].scfsi[i] == 0) || (gr == 0))
  1357.         for (sfb = sfbtable.l[i]; sfb < sfbtable.l[i+1]; sfb++)
  1358.             (*scalefac)[ch].l[sfb] =
  1359.               hgetbits(slen[(i<2)?0:1][gr_info->scalefac_compress]);
  1360.     }
  1361.     (*scalefac)[ch].l[22] = 0; 
  1362.     }
  1363. }
  1364.  
  1365. /****************** new MPEG2 stuf  ***********/
  1366.  
  1367. static unsigned nr_of_sfb_block[6][3][4] = {{{6, 5, 5, 5},{ 9, 9, 9, 9 },{6, 9, 9, 9}},
  1368.                                          {{6, 5, 7, 3},{ 9, 9, 12, 6},{6, 9, 12, 6}},
  1369.                                          {{11, 10, 0, 0},{ 18, 18, 0, 0},{15,18,0,0 }},
  1370.                                          {{7, 7, 7, 0},{ 12, 12, 12, 0},{6, 15, 12, 0}},
  1371.                                          {{6, 6, 6, 3},{12, 9, 9, 6},{6, 12, 9, 6}},
  1372.                                          {{8, 8, 5, 0},{15,12,9,0},{6,18,9,0}}};
  1373. static unsigned scalefac_buffer[54];
  1374.  
  1375. void III_get_LSF_scale_data(
  1376.     III_scalefac_t *scalefac,
  1377.     III_side_info_t *si,
  1378.     int gr, int ch,
  1379.     frame_params *fr_ps)
  1380. {
  1381. short /*sfb,*/ i,j,k/*, window*/;
  1382. short blocktypenumber, blocknumber;
  1383.  
  1384. struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
  1385. unsigned scalefac_comp, int_scalefac_comp, new_slen[4];
  1386.    
  1387. layer *hdr = fr_ps->header;
  1388. scalefac_comp =  gr_info->scalefac_compress;
  1389.  
  1390.     blocktypenumber = 0;
  1391.     if ((gr_info->block_type == 2) && (gr_info->mixed_block_flag == 0))
  1392.                                                         blocktypenumber = 1;
  1393.  
  1394.    if ((gr_info->block_type == 2) && (gr_info->mixed_block_flag == 1))
  1395.                                                         blocktypenumber = 2;
  1396.  
  1397.     if(!((( hdr->mode_ext == 1) || (hdr->mode_ext == 3)) && (ch == 1)))
  1398.     {
  1399.     if(scalefac_comp < 400)
  1400.         {
  1401.         new_slen[0] = (scalefac_comp >> 4) / 5 ;
  1402.         new_slen[1] = (scalefac_comp >> 4) % 5 ;
  1403.         new_slen[2] = (scalefac_comp % 16) >> 2 ;
  1404.         new_slen[3] = (scalefac_comp % 4);
  1405.                 si->ch[ch].gr[gr].preflag = 0;
  1406.  
  1407.                 blocknumber = 0;
  1408.          }
  1409.  
  1410.     else if( scalefac_comp  < 500)
  1411.         {
  1412.         new_slen[0] = ((scalefac_comp - 400 )  >> 2) / 5 ;
  1413.         new_slen[1] = ((scalefac_comp - 400) >> 2) % 5 ;
  1414.         new_slen[2] = (scalefac_comp - 400 ) % 4 ;
  1415.         new_slen[3] = 0;
  1416.                 si->ch[ch].gr[gr].preflag = 0;
  1417.                 blocknumber = 1;
  1418.  
  1419.         }
  1420.  
  1421.     else if( scalefac_comp  < 512)
  1422.         {
  1423.         new_slen[0] = (scalefac_comp - 500 ) / 3 ;
  1424.         new_slen[1] = (scalefac_comp - 500)  % 3 ;
  1425.         new_slen[2] = 0 ;
  1426.         new_slen[3] = 0;
  1427.                 si->ch[ch].gr[gr].preflag = 1;
  1428.                 blocknumber = 2;
  1429.  
  1430.         }
  1431.      }
  1432.  
  1433.     if((((hdr->mode_ext == 1) || (hdr->mode_ext == 3)) && (ch == 1)))
  1434.     {
  1435.       /*   intensity_scale = scalefac_comp %2; */
  1436.          int_scalefac_comp = scalefac_comp >> 1;
  1437.     
  1438.         if(int_scalefac_comp  < 180)
  1439.         {
  1440.         new_slen[0] = int_scalefac_comp  / 36 ;
  1441.         new_slen[1] = (int_scalefac_comp % 36 ) / 6 ;
  1442.         new_slen[2] = (int_scalefac_comp % 36) % 6;
  1443.         new_slen[3] = 0;
  1444.                 si->ch[ch].gr[gr].preflag = 0;
  1445.                 blocknumber = 3;
  1446.  
  1447.          }
  1448.  
  1449.     else if( int_scalefac_comp  < 244)
  1450.         {
  1451.         new_slen[0] = ((int_scalefac_comp - 180 )  % 64 ) >> 4 ;
  1452.         new_slen[1] = ((int_scalefac_comp - 180) % 16) >> 2 ;
  1453.         new_slen[2] = (int_scalefac_comp - 180 ) % 4 ;
  1454.         new_slen[3] = 0;
  1455.                 si->ch[ch].gr[gr].preflag = 0;
  1456.                 blocknumber = 4;
  1457.  
  1458.         }
  1459.  
  1460.     else if( int_scalefac_comp  < 255)
  1461.         {
  1462.         new_slen[0] = (int_scalefac_comp - 244 ) / 3 ;
  1463.         new_slen[1] = (int_scalefac_comp - 244 )  % 3 ;
  1464.         new_slen[2] = 0 ;
  1465.         new_slen[3] = 0;
  1466.                 si->ch[ch].gr[gr].preflag = 0;
  1467.                 blocknumber = 5;
  1468.  
  1469.         }
  1470.      }
  1471.      
  1472.      for (i=0;i< 45;i++) scalefac_buffer[i] = 0;
  1473.  
  1474.      k = 0;
  1475.      for (i = 0;i < 4;i++)
  1476.      { 
  1477.           for(j = 0; j < nr_of_sfb_block[blocknumber][blocktypenumber][i]; j++)
  1478.         {
  1479.            if(new_slen[i] == 0)
  1480.            {
  1481.             scalefac_buffer[k] = 0;
  1482.            }
  1483.            else
  1484.            {   
  1485.                 scalefac_buffer[k] =  hgetbits(new_slen[i]);
  1486.            }
  1487.            k++;
  1488.  
  1489.         }
  1490.      }
  1491.  
  1492. }
  1493.  
  1494.  
  1495. void III_get_LSF_scale_factors(
  1496.     III_scalefac_t    *scalefac,
  1497.     III_side_info_t    *si,
  1498.     int             gr,
  1499.     int             ch,
  1500.     frame_params    *fr_ps)
  1501. {
  1502.     int             sfb, /*i,*/k = 0, window;
  1503.     struct gr_info_s    *gr_info = &(si->ch[ch].gr[gr]);
  1504.  
  1505.     III_get_LSF_scale_data(scalefac, si, gr, ch, fr_ps);
  1506.  
  1507.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) { 
  1508.  
  1509.     if (gr_info->mixed_block_flag) {    /* MIXED */ /* NEW - ag 11/25 */
  1510.         for (sfb = 0; sfb < 8; sfb++) {
  1511.         (*scalefac)[ch].l[sfb] = scalefac_buffer[k];
  1512.         k++;
  1513.         } 
  1514.         for (sfb = 3; sfb < 12; sfb++)
  1515.         for (window=0; window<3; window++) {
  1516.             (*scalefac)[ch].s[window][sfb] = scalefac_buffer[k];
  1517.             k++;
  1518.         }
  1519.         for (sfb=12,window=0; window<3; window++)
  1520.         (*scalefac)[ch].s[window][sfb] = 0;
  1521.  
  1522.     } else {                /* SHORT*/
  1523.  
  1524.         for (sfb = 0; sfb < 12; sfb++)
  1525.         for (window=0; window<3; window++) {
  1526.             (*scalefac)[ch].s[window][sfb] = scalefac_buffer[k];
  1527.             k++;
  1528.         }
  1529.         for (sfb=12,window=0; window<3; window++)
  1530.         (*scalefac)[ch].s[window][sfb] = 0;
  1531.     }
  1532.  
  1533.     } else {                    /* LONG types 0,1,3 */
  1534.  
  1535.     for (sfb = 0; sfb < 21; sfb++) {
  1536.         (*scalefac)[ch].l[sfb] = scalefac_buffer[k];
  1537.         k++;
  1538.     }
  1539.     (*scalefac)[ch].l[22] = 0; 
  1540.     }
  1541. }
  1542.  
  1543.  
  1544. #ifndef BUILTIN_TABLES
  1545.  
  1546. void initialize_huffman() {
  1547.     FILE    *fi;
  1548.     static int     huffman_initialized = FALSE;
  1549.  
  1550.     if (huffman_initialized)
  1551.     return;
  1552.     if (!(fi = OpenTableFile("huffdec") )) {
  1553.     fprintf(stderr, "Please check huffman table 'huffdec'\n");
  1554.     exit(1);
  1555.     }
  1556.  
  1557.     if (fi == NULL) {
  1558.     fprintf(stderr,"decoder table open error\n");
  1559.     exit(3);
  1560.     }
  1561.  
  1562.     if (read_decoder_table(fi) != HTN) {
  1563.     fprintf(stderr,"decoder table read error\n");
  1564.     exit(4);
  1565.     }
  1566.     fclose(fi);
  1567.     huffman_initialized = TRUE;
  1568. }
  1569. #endif
  1570.  
  1571.  
  1572. void III_hufman_decode(
  1573.     long int         is[SBLIMIT][SSLIMIT],
  1574.     III_side_info_t    *si,
  1575.     int             ch,
  1576.     int             gr,
  1577.     int             part2_start,
  1578.     frame_params    *fr_ps)
  1579. {
  1580.     int             i, x, y;
  1581.     int             v, w;
  1582.     struct huffcodetab    *h;
  1583.     int             region1Start;
  1584.     int             region2Start;
  1585.     int             sfreq;
  1586.     int             currentBit, grBits;
  1587.     my_gr_info        *gi;
  1588.     /*int             bt = (*si).ch[ch].gr[gr].window_switching_flag &&
  1589.                  ((*si).ch[ch].gr[gr].block_type == 2);*/
  1590.  
  1591.     gi = (my_gr_info *) &(*si).ch[ch].gr[gr];
  1592.     sfreq = fr_ps->header->sampling_frequency + (fr_ps->header->version * 3);
  1593.     initialize_huffman();
  1594.  
  1595.     /* Find region boundary for short block case. */
  1596.    
  1597.     if ( ((*si).ch[ch].gr[gr].window_switching_flag) && 
  1598.     ((*si).ch[ch].gr[gr].block_type == 2) ) { 
  1599.    
  1600.     /* Region2 */
  1601.     region1Start = 36;    /* sfb[9/3]*3=36 */
  1602.     region2Start = 576;    /* No Region2 for short block case. */
  1603.  
  1604.     } else {       /* Find region boundary for long block case. */
  1605.  
  1606.     region1Start = sfBandIndex[sfreq]
  1607.             .l[(*si).ch[ch].gr[gr].region0_count + 1]; /* MI */
  1608.     region2Start = sfBandIndex[sfreq]
  1609.             .l[(*si).ch[ch].gr[gr].region0_count +
  1610.             (*si).ch[ch].gr[gr].region1_count + 2]; /* MI */
  1611.     }
  1612.  
  1613.     grBits     = part2_start + (*si).ch[ch].gr[gr].part2_3_length;
  1614.     currentBit = hsstell();
  1615.  
  1616.     /* Read bigvalues area. */
  1617.     for (i=0; i < (*si).ch[ch].gr[gr].big_values*2; i+=2) {
  1618.     if (i<region1Start)
  1619.         h = &ht[(*si).ch[ch].gr[gr].table_select[0]];
  1620.     else if (i<region2Start)
  1621.         h = &ht[(*si).ch[ch].gr[gr].table_select[1]];
  1622.     else
  1623.         h = &ht[(*si).ch[ch].gr[gr].table_select[2]];
  1624.     huffman_decoder(h, &x, &y, &v, &w);
  1625.     is[i / SSLIMIT][i % SSLIMIT] = x;
  1626.     is[(i+1) / SSLIMIT][(i+1) % SSLIMIT] = y;
  1627.     }
  1628.  
  1629.     grBits     = part2_start + (*si).ch[ch].gr[gr].part2_3_length;
  1630.     currentBit = hsstell();
  1631.  
  1632.     /* Read count1 area. */
  1633.     h = &ht[(*si).ch[ch].gr[gr].count1table_select+32];
  1634.     while ((hsstell() < part2_start + (*si).ch[ch].gr[gr].part2_3_length ) &&
  1635.         ( i < SSLIMIT*SBLIMIT )) {
  1636.     huffman_decoder(h, &x, &y, &v, &w);
  1637.     is[i / SSLIMIT][i % SSLIMIT] = v;
  1638.     is[(i+1) / SSLIMIT][(i+1) % SSLIMIT] = w;
  1639.     is[(i+2) / SSLIMIT][(i+2) % SSLIMIT] = x;
  1640.     is[(i+3) / SSLIMIT][(i+3) % SSLIMIT] = y;
  1641.     i += 4;
  1642.     }
  1643.  
  1644.     grBits     = part2_start + (*si).ch[ch].gr[gr].part2_3_length;
  1645.     currentBit = hsstell();
  1646.  
  1647.     if (hsstell() > part2_start + (*si).ch[ch].gr[gr].part2_3_length) {
  1648.     i -=4;
  1649.     rewindNbits(hsstell()-part2_start - (*si).ch[ch].gr[gr].part2_3_length);
  1650.     }
  1651.  
  1652.     /* Dismiss stuffing Bits */
  1653.     grBits     = part2_start + (*si).ch[ch].gr[gr].part2_3_length;
  1654.     currentBit = hsstell();
  1655.     if ( currentBit < grBits )
  1656.     hgetbits( grBits - currentBit );
  1657.  
  1658.     /* Zero out rest. */
  1659.     for (; i < SSLIMIT * SBLIMIT; i++)
  1660.     is[i / SSLIMIT][i % SSLIMIT] = 0;
  1661. }
  1662.  
  1663.  
  1664. static 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};
  1665.  
  1666. void III_dequantize_sample(
  1667.     long int         is[SBLIMIT][SSLIMIT],
  1668.     REAL         xr[SBLIMIT][SSLIMIT],
  1669.     III_scalefac_t    *scalefac,
  1670.     struct gr_info_s    *gr_info,
  1671.     int             ch,
  1672.     frame_params    *fr_ps)
  1673. {
  1674.     int            ss, sb, cb = 0, sfreq;
  1675.     /*int            stereo = fr_ps->stereo;*/
  1676.     int            next_cb_boundary, cb_begin, cb_width;
  1677. #ifdef OPTIMIZE
  1678.     static REAL        globalGain;
  1679.     static int        lastGlobalGain = -1;
  1680.  
  1681. // A pow(2, n) cache, where n = [-16..5] step 0.25
  1682. #define POW2CACHENEG (16*4)        // # of cached numbers for n < 0
  1683. #define POW2CACHEPOS (5*4)        // # of cached numbers for n > 0
  1684. static REAL pow2[POW2CACHENEG+POW2CACHEPOS+1] = { // n < 0 + n > 0 + n = 0
  1685. // <<n>>    <<pow(2,n)>>
  1686. /*-16.00*/  0.00001526,  0.00001815,  0.00002158,  0.00002566,  0.00003052,
  1687. /*-14.75*/  0.00003629,  0.00004316,  0.00005132,  0.00006104,  0.00007258,
  1688. /*-13.50*/  0.00008632,  0.00010265,  0.00012207,  0.00014517,  0.00017263,
  1689. /*-12.25*/  0.00020530,  0.00024414,  0.00029033,  0.00034527,  0.00041059,
  1690. /*-11.00*/  0.00048828,  0.00058067,  0.00069053,  0.00082119,  0.00097656,
  1691. /* -9.75*/  0.00116134,  0.00138107,  0.00164238,  0.00195312,  0.00232267,
  1692. /* -8.50*/  0.00276214,  0.00328475,  0.00390625,  0.00464534,  0.00552427,
  1693. /* -7.25*/  0.00656950,  0.00781250,  0.00929068,  0.01104854,  0.01313901,
  1694. /* -6.00*/  0.01562500,  0.01858136,  0.02209709,  0.02627801,  0.03125000,
  1695. /* -4.75*/  0.03716272,  0.04419417,  0.05255603,  0.06250000,  0.07432544,
  1696. /* -3.50*/  0.08838835,  0.10511205,  0.12500000,  0.14865089,  0.17677670,
  1697. /* -2.25*/  0.21022410,  0.25000000,  0.29730178,  0.35355339,  0.42044821,
  1698. /* -1.00*/  0.50000000,  0.59460356,  0.70710678,  0.84089642,  1.00000000,
  1699. /*  0.25*/  1.18920712,  1.41421356,  1.68179283,  2.00000000,  2.37841423,
  1700. /*  1.50*/  2.82842712,  3.36358566,  4.00000000,  4.75682846,  5.65685425,
  1701. /*  2.75*/  6.72717132,  8.00000000,  9.51365692, 11.31370850, 13.45434264,
  1702. /*  4.00*/ 16.00000000, 19.02731384, 22.62741700, 26.90868529, 32.00000000
  1703. };
  1704.  
  1705.     // Skip calculation unless value has changed
  1706.     if (gr_info->global_gain != lastGlobalGain) {
  1707.  
  1708.     // Compute overall (global) scaling
  1709.  
  1710.     // All vars are integers -> required accuracy = 0.25
  1711.     int n = gr_info->global_gain - 210;
  1712.     if (n >= -POW2CACHENEG && n <= POW2CACHEPOS)
  1713.         globalGain = pow2[n + POW2CACHENEG];    // pow2[60] = 2^0 = 1
  1714.     else {
  1715.         globalGain = pow(2.0, (float)n * 0.25);
  1716.         //fprintf(stderr, "pow(2,n)[1] miss, n=%4.2f\n", (float)n * 0.25);
  1717.     }
  1718.     // Original calculation
  1719.     //globalGain = pow(2.0, 0.25 * (gr_info->global_gain - 210.0));
  1720.  
  1721.     lastGlobalGain = gr_info->global_gain;
  1722.     }
  1723.  
  1724.     sfreq = fr_ps->header->sampling_frequency + (fr_ps->header->version * 3);
  1725.  
  1726.     // Choose correct scalefactor band per block type, initalize boundary
  1727.  
  1728.     if (gr_info->window_switching_flag && (gr_info->block_type == 2))
  1729.     if (gr_info->mixed_block_flag) 
  1730.         next_cb_boundary=sfBandIndex[sfreq].l[1];    // LONG blocks: 0,1,3
  1731.     else {
  1732.         next_cb_boundary=sfBandIndex[sfreq].s[1]*3;    // Pure SHORT block
  1733.         cb_width = sfBandIndex[sfreq].s[1];
  1734.         cb_begin = 0;
  1735.     } else 
  1736.         next_cb_boundary=sfBandIndex[sfreq].l[1];    // LONG blocks: 0,1,3
  1737.  
  1738.     // Apply formula per block type
  1739.  
  1740.     for (sb = 0 ; sb < SBLIMIT ; sb++) {    // 32
  1741.     for (ss = 0 ; ss < SSLIMIT ; ss++) {    // 18
  1742.  
  1743.         int        insample;
  1744.         REAL    outsample;
  1745.  
  1746.         if ((sb * 18) + ss == next_cb_boundary) {
  1747.         // Adjust critical band boundary
  1748.  
  1749.         if (gr_info->window_switching_flag &&
  1750.                     (gr_info->block_type == 2)) {
  1751.             if (gr_info->mixed_block_flag)  {
  1752.             if (((sb * 18) + ss) == sfBandIndex[sfreq].l[8]) {
  1753.                 next_cb_boundary=sfBandIndex[sfreq].s[4] * 3; 
  1754.                 cb = 3;
  1755.                 cb_width = sfBandIndex[sfreq].s[cb+1] - 
  1756.                     sfBandIndex[sfreq].s[cb];
  1757.                 cb_begin = sfBandIndex[sfreq].s[cb] * 3;     
  1758.             } else if (((sb * 18) + ss) < sfBandIndex[sfreq].l[8]) 
  1759.                 next_cb_boundary = sfBandIndex[sfreq].l[(++cb) + 1];
  1760.             else {
  1761.                 next_cb_boundary =
  1762.                     sfBandIndex[sfreq].s[(++cb) + 1] * 3;
  1763.                 cb_width = sfBandIndex[sfreq].s[cb + 1] - 
  1764.                     sfBandIndex[sfreq].s[cb];
  1765.                 cb_begin = sfBandIndex[sfreq].s[cb] * 3;     
  1766.             }   
  1767.             } else {
  1768.             next_cb_boundary = sfBandIndex[sfreq].s[(++cb) + 1] * 3;
  1769.             cb_width = sfBandIndex[sfreq].s[cb+1] - 
  1770.                     sfBandIndex[sfreq].s[cb];
  1771.             cb_begin = sfBandIndex[sfreq].s[cb]*3;        
  1772.             } 
  1773.  
  1774.         } else // long blocks
  1775.  
  1776.             next_cb_boundary = sfBandIndex[sfreq].l[(++cb) + 1];
  1777.         }
  1778.  
  1779.         // Do long/short dependent scaling operations
  1780.  
  1781.         // Initialize to overall (global) scaling
  1782.         outsample = globalGain;
  1783.  
  1784.         if (gr_info->window_switching_flag && (
  1785.             ((gr_info->block_type == 2) &&
  1786.             (gr_info->mixed_block_flag == 0)) ||
  1787.             ((gr_info->block_type == 2) &&
  1788.             gr_info->mixed_block_flag && (sb >= 2)) )) {
  1789.  
  1790.         // All vars are integers -> required accuracy = 2
  1791.         // Ending expression = 1 should return pow(2, -2)
  1792.         int n = 4 * (-2 * gr_info->subblock_gain[(((sb * 18) + ss) -
  1793.                 cb_begin) / cb_width]);
  1794.  
  1795.         if (n >= -POW2CACHENEG && n <= POW2CACHEPOS)
  1796.             outsample *= pow2[n + POW2CACHENEG];
  1797.         else {
  1798.             outsample *= pow(2.0, (float)n * 0.25);
  1799.             //fprintf(stderr, "pow(2,n)[2] miss, n=%4.2f\n", n * 0.25);
  1800.         }
  1801.  
  1802.         // All vars are integers -> required accuracy = 0.5
  1803.         // Ending expression = 1 should return pow(2, -0.5)
  1804.         n = (int)-2 * ((1.0 + gr_info->scalefac_scale) *
  1805.              (*scalefac)[ch].s[(((sb * 18) + ss) -
  1806.              cb_begin) / cb_width][cb]);
  1807.  
  1808.         if (n >= -POW2CACHENEG && n <= POW2CACHEPOS)
  1809.             outsample *= pow2[n + POW2CACHENEG];
  1810.         else {
  1811.             outsample *= pow(2.0, (float)n * 0.25);
  1812.             //fprintf(stderr, "pow(2,n)[3] miss, n=%4.2f\n", n * 0.25);
  1813.         }
  1814.  
  1815.         } else {    // LONG block types 0,1,3 &
  1816.             // 1st 2 subbands of switched blocks
  1817.  
  1818.         // All vars are integers -> required accuracy = 0.5
  1819.         // Ending expression = 1 should return pow(2, -0.5)
  1820.         int n = (int)-2 * ((1.0 + gr_info->scalefac_scale) *
  1821.              ((*scalefac)[ch].l[cb] +
  1822.              gr_info->preflag * pretab[cb]));
  1823.  
  1824.         if (n >= -POW2CACHENEG && n <= POW2CACHEPOS)
  1825.             outsample *= pow2[n + POW2CACHENEG];
  1826.         else {
  1827.             if (n > -1000) {
  1828.             outsample *= pow(2.0, (float)n * 0.5);
  1829.             //fprintf(stderr, "pow(2,n)[4] miss, n=%4.2f\n",
  1830.             //    (float)n  -0.5);
  1831.             } else
  1832.             // Bug in the implementation here? x sometimes becomes
  1833.             // very small like -2009044012. Then mute.
  1834.             outsample = 0.0;
  1835.         }
  1836.         }
  1837.  
  1838.         /* Scale quantized value */
  1839.         insample = is[sb][ss];        // read input array once
  1840.         if (insample == 0)
  1841.         outsample = 0.0;        // optimize no input
  1842.         else {
  1843.  
  1844. // A pow(n, 4/3) cache, where n = [1..100] step 1
  1845. #define POWCACHED 100    // biggest number that is cached
  1846. static REAL powc[POWCACHED] = {
  1847. /* 1.00*/   1.00000000,   2.51984210,   4.32674871,   6.34960421,   8.54987973,
  1848. /* 6.00*/  10.90272356,  13.39051828,  16.00000000,  18.72075441,  21.54434690,
  1849. /*11.00*/  24.46378100,  27.47314182,  30.56735094,  33.74199170,  36.99318111,
  1850. /*16.00*/  40.31747360,  43.71178704,  47.17334510,  50.69963133,  54.28835233,
  1851. /*21.00*/  57.93740770,  61.64486527,  65.40894054,  69.22797937,  73.10044346,
  1852. /*26.00*/  77.02489778,  81.00000000,  85.02449121,  89.09718794,  93.21697518,
  1853. /*31.00*/  97.38280022, 101.59366733, 105.84863289, 110.14680124, 114.48732086,
  1854. /*36.00*/ 118.86938096, 123.29220851, 127.75506546, 132.25724628, 136.79807573,
  1855. /*41.00*/ 141.37690686, 145.99311909, 150.64611660, 155.33532675, 160.06019870,
  1856. /*46.00*/ 164.82020207, 169.61482577, 174.44357691, 179.30597979, 184.20157493,
  1857. /*51.00*/ 189.12991823, 194.09058015, 199.08314497, 204.10721008, 209.16238534,
  1858. /*56.00*/ 214.24829247, 219.36456448, 224.51084516, 229.68678854, 234.89205847,
  1859. /*61.00*/ 240.12632817, 245.38927980, 250.68060410, 256.00000000, 261.34717431,
  1860. /*66.00*/ 266.72184136, 272.12372273, 277.55254693, 283.00804915, 288.48997099,
  1861. /*71.00*/ 293.99806021, 299.53207052, 305.09176134, 310.67689758, 316.28724949,
  1862. /*76.00*/ 321.92259240, 327.58270661, 333.26737717, 338.97639374, 344.70955041,
  1863. /*81.00*/ 350.46664558, 356.24748183, 362.05186573, 367.87960775, 373.73052213,
  1864. /*86.00*/ 379.60442677, 385.50114309, 391.42049594, 397.36231351, 403.32642719,
  1865. /*91.00*/ 409.31267152, 415.32088406, 421.35090534, 427.40257871, 433.47575036,
  1866. /*96.00*/ 439.57026914, 445.68598654, 451.82275662, 457.98043591, 464.15888336
  1867. };
  1868.         int absSample = abs(insample);
  1869.  
  1870.         if (absSample <= POWCACHED)
  1871.             outsample *= powc[absSample - 1];
  1872.         else
  1873.             outsample *= pow(absSample, 4.0/3.0);
  1874.         if (insample < 0)
  1875.             outsample = -outsample;    // change the sign
  1876.         }
  1877.         xr[sb][ss] = outsample;        // touch output array once
  1878.     }
  1879.     }
  1880.  
  1881. #else
  1882.     // Original code
  1883.  
  1884.     int        sign;
  1885.  
  1886.     sfreq = fr_ps->header->sampling_frequency + (fr_ps->header->version * 3);
  1887.  
  1888.     // Choose correct scalefactor band per block type, initalize boundary
  1889.  
  1890.     if (gr_info->window_switching_flag && (gr_info->block_type == 2))
  1891.     if (gr_info->mixed_block_flag) 
  1892.         next_cb_boundary=sfBandIndex[sfreq].l[1];    // LONG blocks: 0,1,3
  1893.     else {
  1894.         next_cb_boundary=sfBandIndex[sfreq].s[1]*3;    // Pure SHORT block
  1895.         cb_width = sfBandIndex[sfreq].s[1];
  1896.         cb_begin = 0;
  1897.     } else 
  1898.         next_cb_boundary=sfBandIndex[sfreq].l[1];    // LONG blocks: 0,1,3
  1899.  
  1900.     // Apply formula per block type
  1901.  
  1902.     for (sb = 0 ; sb < SBLIMIT ; sb++) {
  1903.     for (ss = 0 ; ss < SSLIMIT ; ss++) {
  1904.         if ((sb*18) + ss == next_cb_boundary) {
  1905.         // Adjust critical band boundary
  1906.  
  1907.         if (gr_info->window_switching_flag &&
  1908.                     (gr_info->block_type == 2)) {
  1909.             if (gr_info->mixed_block_flag)  {
  1910.             if (((sb*18)+ss) == sfBandIndex[sfreq].l[8]) {
  1911.                 next_cb_boundary=sfBandIndex[sfreq].s[4]*3; 
  1912.                 cb = 3;
  1913.                 cb_width = sfBandIndex[sfreq].s[cb+1] - 
  1914.                     sfBandIndex[sfreq].s[cb];
  1915.                 cb_begin = sfBandIndex[sfreq].s[cb]*3;     
  1916.             } else if (((sb*18)+ss) < sfBandIndex[sfreq].l[8]) 
  1917.                 next_cb_boundary = sfBandIndex[sfreq].l[(++cb)+1];
  1918.             else {
  1919.                 next_cb_boundary = sfBandIndex[sfreq].s[(++cb)+1]*3;
  1920.                 cb_width = sfBandIndex[sfreq].s[cb+1] - 
  1921.                     sfBandIndex[sfreq].s[cb];
  1922.                 cb_begin = sfBandIndex[sfreq].s[cb]*3;     
  1923.             }   
  1924.             } else {
  1925.             next_cb_boundary = sfBandIndex[sfreq].s[(++cb)+1]*3;
  1926.             cb_width = sfBandIndex[sfreq].s[cb+1] - 
  1927.                     sfBandIndex[sfreq].s[cb];
  1928.             cb_begin = sfBandIndex[sfreq].s[cb]*3;        
  1929.             } 
  1930.  
  1931.         } else /* long blocks */
  1932.  
  1933.             next_cb_boundary = sfBandIndex[sfreq].l[(++cb)+1];
  1934.         }
  1935.  
  1936.         /* Compute overall (global) scaling */
  1937.  
  1938.         xr[sb][ss] = pow(2.0, (0.25 * (gr_info->global_gain - 210.0)));
  1939.  
  1940.         /* Do long/short dependent scaling operations */
  1941.         
  1942.         if (gr_info->window_switching_flag && (
  1943.             ((gr_info->block_type == 2) &&
  1944.             (gr_info->mixed_block_flag == 0)) ||
  1945.             ((gr_info->block_type == 2) &&
  1946.             gr_info->mixed_block_flag && (sb >= 2)) )) {
  1947.  
  1948.         xr[sb][ss] *= pow(2.0, 0.25 * -8.0 * gr_info->subblock_gain[
  1949.                 (((sb*18)+ss) - cb_begin)/cb_width]);
  1950.  
  1951.         xr[sb][ss] *= pow(2.0, 0.25 * -2.0 *
  1952.             (1.0+gr_info->scalefac_scale) *
  1953.             (*scalefac)[ch].s[(((sb*18)+ss) -
  1954.             cb_begin)/cb_width][cb]);
  1955.  
  1956.         } else {    // LONG block types 0,1,3 &
  1957.             // 1st 2 subbands of switched blocks
  1958.  
  1959.         xr[sb][ss] *= pow(2.0, -0.5 * (1.0+gr_info->scalefac_scale)
  1960.                     * ((*scalefac)[ch].l[cb]
  1961.                     + gr_info->preflag * pretab[cb]));
  1962.         }
  1963.  
  1964.         /* Scale quantized value */
  1965.         sign = (is[sb][ss]<0) ? 1 : 0; 
  1966.         xr[sb][ss] *= pow(abs(is[sb][ss]), 4.0/3.0);
  1967.         if (sign)
  1968.         xr[sb][ss] = -xr[sb][ss];
  1969.     }
  1970.     }
  1971. #endif // OPTIMIZE
  1972. }
  1973.  
  1974.  
  1975. void III_reorder (
  1976.     REAL         xr[SBLIMIT][SSLIMIT],
  1977.     REAL         ro[SBLIMIT][SSLIMIT],
  1978.     struct gr_info_s    *gr_info,
  1979.     frame_params    *fr_ps)
  1980. {
  1981.     int        sfreq;
  1982.     int        sfb, sfb_start, sfb_lines;
  1983.     int        sb, ss, window, freq, src_line, des_line;
  1984.  
  1985.     sfreq=fr_ps->header->sampling_frequency + (fr_ps->header->version * 3);
  1986.  
  1987. #ifdef OPTIMIZE
  1988.  
  1989.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  1990.  
  1991.     // Clear the whole array
  1992.     memset(&ro[0][0], 0, sizeof(REAL)*SBLIMIT*SSLIMIT);
  1993.  
  1994.     if (gr_info->mixed_block_flag) {
  1995.         /* No reorder for low 2 subbands */
  1996.         for (sb=0 ; sb < 2 ; sb++)
  1997.         for (ss=0 ; ss < SSLIMIT ; ss++) {
  1998.             ro[sb][ss] = xr[sb][ss];
  1999.         }
  2000.         /* Reordering for rest switched short */
  2001.         for (sfb = 3, sfb_start = sfBandIndex[sfreq].s[3],
  2002.             sfb_lines=sfBandIndex[sfreq].s[4] - sfb_start; 
  2003.             sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
  2004.             (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
  2005.         for(window=0; window<3; window++)
  2006.             for(freq=0;freq<sfb_lines;freq++) {
  2007.             src_line = sfb_start*3 + window*sfb_lines + freq; 
  2008.             des_line = (sfb_start*3) + window + (freq*3);
  2009.             ro[des_line/SSLIMIT][des_line%SSLIMIT] = 
  2010.                 xr[src_line/SSLIMIT][src_line%SSLIMIT];
  2011.             }
  2012.  
  2013.     } else {    /* pure short */
  2014.  
  2015.         for (sfb=0,sfb_start=0,sfb_lines=sfBandIndex[sfreq].s[1]; 
  2016.             sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
  2017.             (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
  2018.         for (window=0; window<3; window++)
  2019.             for(freq=0;freq<sfb_lines;freq++) {
  2020.             src_line = sfb_start*3 + window*sfb_lines + freq; 
  2021.             des_line = (sfb_start*3) + window + (freq*3);
  2022.             ro[des_line/SSLIMIT][des_line%SSLIMIT] = 
  2023.                 xr[src_line/SSLIMIT][src_line%SSLIMIT];
  2024.             }
  2025.     }
  2026.  
  2027.     } else {    /* long blocks */
  2028.  
  2029.     // Copy array xr[] to ro[]
  2030.     memcpy(&ro[0][0], &xr[0][0], sizeof(REAL)*SBLIMIT*SSLIMIT);
  2031.     }
  2032.  
  2033. #else
  2034.     // Original code
  2035.  
  2036.     for (sb=0; sb<SBLIMIT; sb++)
  2037.     for (ss=0; ss<SSLIMIT; ss++) 
  2038.         ro[sb][ss] = 0.0;
  2039.  
  2040.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  2041.     if (gr_info->mixed_block_flag) {
  2042.         /* No reorder for low 2 subbands */
  2043.         for (sb=0 ; sb < 2 ; sb++)
  2044.         for (ss=0 ; ss < SSLIMIT ; ss++) {
  2045.             ro[sb][ss] = xr[sb][ss];
  2046.         }
  2047.         /* Reordering for rest switched short */
  2048.         for (sfb = 3, sfb_start = sfBandIndex[sfreq].s[3],
  2049.             sfb_lines=sfBandIndex[sfreq].s[4] - sfb_start; 
  2050.             sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
  2051.             (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
  2052.         for(window=0; window<3; window++)
  2053.             for(freq=0;freq<sfb_lines;freq++) {
  2054.             src_line = sfb_start*3 + window*sfb_lines + freq; 
  2055.             des_line = (sfb_start*3) + window + (freq*3);
  2056.             ro[des_line/SSLIMIT][des_line%SSLIMIT] = 
  2057.                 xr[src_line/SSLIMIT][src_line%SSLIMIT];
  2058.             }
  2059.  
  2060.     } else {    /* pure short */
  2061.  
  2062.         for (sfb=0,sfb_start=0,sfb_lines=sfBandIndex[sfreq].s[1]; 
  2063.             sfb < 13; sfb++,sfb_start=sfBandIndex[sfreq].s[sfb],
  2064.             (sfb_lines=sfBandIndex[sfreq].s[sfb+1] - sfb_start))
  2065.         for (window=0; window<3; window++)
  2066.             for(freq=0;freq<sfb_lines;freq++) {
  2067.             src_line = sfb_start*3 + window*sfb_lines + freq; 
  2068.             des_line = (sfb_start*3) + window + (freq*3);
  2069.             ro[des_line/SSLIMIT][des_line%SSLIMIT] = 
  2070.                 xr[src_line/SSLIMIT][src_line%SSLIMIT];
  2071.             }
  2072.     }
  2073.  
  2074.     } else {    /* long blocks */
  2075.  
  2076.     for (sb=0 ; sb < SBLIMIT ; sb++)
  2077.         for (ss=0 ; ss < SSLIMIT ; ss++) 
  2078.         ro[sb][ss] = xr[sb][ss];
  2079.     }
  2080. #endif // OPTIMIZE
  2081. }
  2082.  
  2083.  
  2084. static void III_i_stereo_k_values(
  2085.     int        is_pos,
  2086.     REAL    io,
  2087.     int        i,
  2088.     REAL FAR    k[2][576])
  2089. {
  2090.     if (is_pos == 0) { 
  2091.     k[0][i] = 1.0;
  2092.     k[1][i] = 1.0;
  2093.     } else if ((is_pos % 2) == 1) {
  2094.     k[0][i] = pow(io, (float)(is_pos + 1)/2.0);
  2095.     k[1][i] = 1.0;
  2096.     } else {
  2097.     k[0][i] = 1.0;
  2098.     k[1][i] = pow(io, (float)is_pos/2.0);
  2099.     }
  2100. }
  2101.  
  2102.  
  2103. void III_stereo(
  2104.     REAL         xr[2][SBLIMIT][SSLIMIT],
  2105.     REAL         lr[2][SBLIMIT][SSLIMIT],
  2106.     III_scalefac_t    *scalefac,
  2107.     struct gr_info_s    *gr_info,
  2108.     frame_params    *fr_ps)
  2109. {
  2110.     int        sfreq;
  2111.     int        stereo = fr_ps->stereo;
  2112.     int        ms_stereo = (fr_ps->header->mode == MPG_MD_JOINT_STEREO) &&
  2113.             (fr_ps->header->mode_ext & 0x2); 
  2114.     int        i_stereo = (fr_ps->header->mode == MPG_MD_JOINT_STEREO) &&
  2115.            (fr_ps->header->mode_ext & 0x1);
  2116.     // Frequency line that marks the beginning of the zero part
  2117.     /*int        js_bound;*/
  2118.  
  2119.     int            sfb/*, next_sfb_boundary*/;
  2120.     int            i, j, sb, ss, ch;
  2121.     REAL        io;
  2122. #ifdef OPTIMIZE
  2123.     static short    is_pos[SBLIMIT*SSLIMIT]; 
  2124.     static REAL        is_ratio[SBLIMIT*SSLIMIT];
  2125.     static REAL FAR    k[2][SBLIMIT*SSLIMIT];
  2126. #else
  2127.     short        is_pos[SBLIMIT*SSLIMIT]; 
  2128.     REAL        is_ratio[SBLIMIT*SSLIMIT];
  2129.     REAL FAR        k[2][SBLIMIT*SSLIMIT];
  2130. #endif
  2131.  
  2132.     int lsf = (fr_ps->header->version == MPEG_PHASE2_LSF);
  2133.  
  2134.     if ((gr_info->scalefac_compress % 2) == 1)
  2135.     io = 0.707106781188;
  2136.     else
  2137.     io = 0.840896415256;
  2138.  
  2139.     sfreq = fr_ps->header->sampling_frequency + (fr_ps->header->version * 3);
  2140.  
  2141.     /* Intialization */
  2142.     for (i = 0; i < SBLIMIT*SSLIMIT; i++ )
  2143.     is_pos[i] = 7;
  2144.  
  2145.     if ((stereo == 2) && i_stereo ) {
  2146.     if (gr_info->window_switching_flag && (gr_info->block_type == 2)) {
  2147.         if( gr_info->mixed_block_flag ) {
  2148.         int max_sfb = 0;
  2149.  
  2150.         for ( j=0; j<3; j++ ) {
  2151.             int sfbcnt;
  2152.             sfbcnt = 2;
  2153.             for (sfb=12; sfb >=3; sfb--) {
  2154.             int lines;
  2155.             lines = sfBandIndex[sfreq].s[sfb+1] -
  2156.                 sfBandIndex[sfreq].s[sfb];
  2157.             i = 3*sfBandIndex[sfreq].s[sfb] + (j+1) * lines - 1;
  2158.             while (lines > 0) {
  2159.                 if (xr[1][i/SSLIMIT][i%SSLIMIT] != 0.0) {
  2160.                 sfbcnt = sfb;
  2161.                 sfb = -10;
  2162.                 lines = -10;
  2163.                 }
  2164.                 lines--;
  2165.                 i--;
  2166.             }
  2167.             }
  2168.             sfb = sfbcnt + 1;
  2169.  
  2170.             if ( sfb > max_sfb )
  2171.             max_sfb = sfb;
  2172.  
  2173.             while (sfb<12) {
  2174.             sb = sfBandIndex[sfreq].s[sfb+1] -
  2175.                 sfBandIndex[sfreq].s[sfb];
  2176.             i = 3*sfBandIndex[sfreq].s[sfb] + j * sb;
  2177.             for ( ; sb > 0; sb--) {
  2178.                 is_pos[i] = (*scalefac)[1].s[j][sfb];
  2179.                 if ( is_pos[i] != 7 )
  2180.                 if (lsf) {
  2181.                     III_i_stereo_k_values(is_pos[i],io,i,k);
  2182.                 } else {
  2183.                     is_ratio[i] = tan((float)is_pos[i] *
  2184.                         (PI / 12));
  2185.                 }
  2186.                 i++;
  2187.             }
  2188.             sfb++;
  2189.             }
  2190.  
  2191.             sb = sfBandIndex[sfreq].s[12]-sfBandIndex[sfreq].s[11];
  2192.             sfb = 3*sfBandIndex[sfreq].s[11] + j * sb;
  2193.             sb = sfBandIndex[sfreq].s[13]-sfBandIndex[sfreq].s[12];
  2194.  
  2195.             i = 3*sfBandIndex[sfreq].s[11] + j * sb;
  2196.             for ( ; sb > 0; sb-- ) {
  2197.             is_pos[i] = is_pos[sfb];
  2198.             is_ratio[i] = is_ratio[sfb];
  2199.             k[0][i] = k[0][sfb];
  2200.             k[1][i] = k[1][sfb];
  2201.             i++;
  2202.             }
  2203.         }
  2204.         if ( max_sfb <= 3 ) {
  2205.             i = 2;
  2206.             ss = 17;
  2207.             sb = -1;
  2208.             while ( i >= 0 ) {
  2209.             if ( xr[1][i][ss] != 0.0 ) {
  2210.                 sb = i*18+ss;
  2211.                 i = -1;
  2212.             } else {
  2213.                 ss--;
  2214.                 if ( ss < 0 ) {
  2215.                 i--;
  2216.                 ss = 17;
  2217.                 }
  2218.             }
  2219.             }
  2220.             i = 0;
  2221.             while ( sfBandIndex[sfreq].l[i] <= sb )
  2222.             i++;
  2223.             sfb = i;
  2224.             i = sfBandIndex[sfreq].l[i];
  2225.             for ( ; sfb<8; sfb++ ) {
  2226.             sb = sfBandIndex[sfreq].l[sfb+1]-sfBandIndex[sfreq].l[sfb];
  2227.             for ( ; sb > 0; sb--) {
  2228.                 is_pos[i] = (*scalefac)[1].l[sfb];
  2229.                 if ( is_pos[i] != 7 )
  2230.                 if ( lsf ) {
  2231.                     III_i_stereo_k_values(is_pos[i],io,i,k);
  2232.                 } else {
  2233.                     is_ratio[i] = tan((float)is_pos[i] * (PI / 12));
  2234.                 }
  2235.                 i++;
  2236.             }
  2237.             }
  2238.         }
  2239.  
  2240.         } else {
  2241.  
  2242.         for ( j=0; j<3; j++ ) {
  2243.             int sfbcnt;
  2244.             sfbcnt = -1;
  2245.             for( sfb=12; sfb >=0; sfb-- ) {
  2246.             int lines;
  2247.             lines = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
  2248.             i = 3*sfBandIndex[sfreq].s[sfb] + (j+1) * lines - 1;
  2249.             while ( lines > 0 ) {
  2250.                 if ( xr[1][i/SSLIMIT][i%SSLIMIT] != 0.0 ) {
  2251.                 sfbcnt = sfb;
  2252.                 sfb = -10;
  2253.                 lines = -10;
  2254.                 }
  2255.                 lines--;
  2256.                 i--;
  2257.             }
  2258.             }
  2259.             sfb = sfbcnt + 1;
  2260.             while ( sfb<12 ) {
  2261.             sb = sfBandIndex[sfreq].s[sfb+1]-sfBandIndex[sfreq].s[sfb];
  2262.             i = 3*sfBandIndex[sfreq].s[sfb] + j * sb;
  2263.             for ( ; sb > 0; sb--) {
  2264.                 is_pos[i] = (*scalefac)[1].s[j][sfb];
  2265.                 if ( is_pos[i] != 7 )
  2266.                 if( lsf ) {
  2267.                     III_i_stereo_k_values(is_pos[i],io,i,k);
  2268.                 } else {
  2269.                     is_ratio[i] = tan((float) is_pos[i] *
  2270.                             (PI / 12));
  2271.                 }
  2272.                 i++;
  2273.             }
  2274.             sfb++;
  2275.             }
  2276.  
  2277.             sb = sfBandIndex[sfreq].s[12]-sfBandIndex[sfreq].s[11];
  2278.             sfb = 3*sfBandIndex[sfreq].s[11] + j * sb;
  2279.             sb = sfBandIndex[sfreq].s[13]-sfBandIndex[sfreq].s[12];
  2280.  
  2281.             i = 3*sfBandIndex[sfreq].s[11] + j * sb;
  2282.             for ( ; sb > 0; sb-- ) {
  2283.             is_pos[i] = is_pos[sfb];
  2284.             is_ratio[i] = is_ratio[sfb];
  2285.             k[0][i] = k[0][sfb];
  2286.             k[1][i] = k[1][sfb];
  2287.             i++;
  2288.             }
  2289.         }
  2290.         }
  2291.     } else {
  2292.         i = 31;
  2293.         ss = 17;
  2294.         sb = 0;
  2295.         while ( i >= 0 ) {
  2296.         if ( xr[1][i][ss] != 0.0 ) {
  2297.             sb = i*18+ss;
  2298.             i = -1;
  2299.         } else {
  2300.             ss--;
  2301.             if ( ss < 0 ) {
  2302.             i--;
  2303.             ss = 17;
  2304.             }
  2305.         }
  2306.         }
  2307.         i = 0;
  2308.         while ( sfBandIndex[sfreq].l[i] <= sb )
  2309.         i++;
  2310.         sfb = i;
  2311.         i = sfBandIndex[sfreq].l[i];
  2312.         for ( ; sfb<21; sfb++ ) {
  2313.         sb = sfBandIndex[sfreq].l[sfb+1] - sfBandIndex[sfreq].l[sfb];
  2314.         for ( ; sb > 0; sb--) {
  2315.             is_pos[i] = (*scalefac)[1].l[sfb];
  2316.             if ( is_pos[i] != 7 )
  2317.             if( lsf ) {
  2318.                 III_i_stereo_k_values(is_pos[i],io,i,k);
  2319.             } else {
  2320.                 is_ratio[i] = tan((float)is_pos[i] * (PI / 12));
  2321.             }
  2322.             i++;
  2323.         }
  2324.         }
  2325.         sfb = sfBandIndex[sfreq].l[20];
  2326.         for ( sb = 576 - sfBandIndex[sfreq].l[21]; sb > 0; sb-- ) {
  2327.         is_pos[i] = is_pos[sfb];
  2328.         is_ratio[i] = is_ratio[sfb];
  2329.         k[0][i] = k[0][sfb];
  2330.         k[1][i] = k[1][sfb];
  2331.         i++;
  2332.         }
  2333.     }
  2334.     }
  2335.  
  2336.     for (ch = 0; ch < 2; ch++)
  2337.     for (sb = 0; sb < SBLIMIT; sb++)
  2338.         for (ss = 0; ss < SSLIMIT; ss++) 
  2339.         lr[ch][sb][ss] = 0;
  2340.  
  2341.     if (stereo == 2) 
  2342.     for(sb = 0; sb < SBLIMIT; sb++)
  2343.         for(ss = 0; ss < SSLIMIT; ss++) {
  2344.         i = (sb*18)+ss;
  2345.         if ( is_pos[i] == 7 ) {
  2346.             if ( ms_stereo ) {
  2347.             lr[0][sb][ss] = (xr[0][sb][ss]+xr[1][sb][ss])/1.41421356;
  2348.             lr[1][sb][ss] = (xr[0][sb][ss]-xr[1][sb][ss])/1.41421356;
  2349.             } else {
  2350.             lr[0][sb][ss] = xr[0][sb][ss];
  2351.             lr[1][sb][ss] = xr[1][sb][ss];
  2352.             }
  2353.         } else if (i_stereo ) {
  2354.             if ( lsf ) {
  2355.             lr[0][sb][ss] = xr[0][sb][ss] * k[0][i];
  2356.             lr[1][sb][ss] = xr[0][sb][ss] * k[1][i]; 
  2357.             } else {
  2358.             lr[0][sb][ss] = xr[0][sb][ss] * (is_ratio[i]/(1+is_ratio[i]));
  2359.             lr[1][sb][ss] = xr[0][sb][ss] * (1/(1+is_ratio[i])); 
  2360.             }
  2361.         } else {
  2362.             fprintf(stderr, "Error in stereo processing\n");
  2363.         }
  2364.         }
  2365.  
  2366.     else    /* mono , bypass xr[0][][] to lr[0][][]*/
  2367.  
  2368.     for (sb = 0; sb < SBLIMIT; sb++)
  2369.         for(ss = 0; ss < SSLIMIT; ss++)
  2370.         lr[0][sb][ss] = xr[0][sb][ss];
  2371. }
  2372.  
  2373. REAL Ci[8] = {-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
  2374.  
  2375.  
  2376. void III_antialias(
  2377.     REAL         xr[SBLIMIT][SSLIMIT],
  2378.     REAL         hybridIn[SBLIMIT][SSLIMIT],
  2379.     struct gr_info_s    *gr_info,
  2380.     frame_params    *fr_ps)
  2381. {
  2382.     static int      init = 1;
  2383.     static REAL      ca[8], cs[8];
  2384.     REAL      bu, bd;    /* upper and lower butterfly inputs */
  2385.     int       ss, sb, sblim;
  2386.  
  2387.     if (init == 1) {
  2388.     int    i;
  2389.     REAL    sq;
  2390.     for (i = 0; i < 8; i++) {
  2391.         sq = sqrt(1.0 + Ci[i] * Ci[i]);
  2392.         cs[i] = 1.0 / sq;
  2393.         ca[i] = Ci[i] / sq;
  2394.     }
  2395.     init = 0;
  2396.     }
  2397.    
  2398.     /* Clear all inputs */  
  2399.     for(sb=0;sb<SBLIMIT;sb++)
  2400.     for(ss=0;ss<SSLIMIT;ss++)
  2401.         hybridIn[sb][ss] = xr[sb][ss];
  2402.  
  2403.     if  (gr_info->window_switching_flag && (gr_info->block_type == 2) &&
  2404.             !gr_info->mixed_block_flag )
  2405.     return;
  2406.  
  2407.     if ( gr_info->window_switching_flag && gr_info->mixed_block_flag &&
  2408.             (gr_info->block_type == 2))
  2409.     sblim = 1;
  2410.     else
  2411.     sblim = SBLIMIT-1;
  2412.  
  2413.     /*
  2414.      * 31 alias-reduction operations between each pair of sub-bands
  2415.      * with 8 butterflies between each pair
  2416.      */
  2417.     for (sb = 0; sb < sblim; sb++)   
  2418.     for (ss = 0; ss < 8; ss++) {     
  2419.         bu = xr[sb][17-ss];
  2420.         bd = xr[sb+1][ss];
  2421.         hybridIn[sb][17-ss] = (bu * cs[ss]) - (bd * ca[ss]);
  2422.         hybridIn[sb+1][ss] = (bd * cs[ss]) + (bu * ca[ss]);
  2423.     }  
  2424. }
  2425.  
  2426.  
  2427. #ifndef ASM_OPTIMIZE
  2428.  
  2429. /*------------------------------------------------------------------*/
  2430. /*                                    */
  2431. /*    Function: Calculation of the inverse MDCT             */
  2432. /*    In the case of short blocks the 3 output vectors are already  */
  2433. /*    overlapped and added in this modul.                */
  2434. /*                                    */
  2435. /*    New layer3                            */
  2436. /*                                    */
  2437. /*------------------------------------------------------------------*/
  2438. void inv_mdct(
  2439.     REAL    in[18],
  2440.     REAL    out[36],
  2441.     int        block_type)
  2442. {
  2443.     int            i, m, N, p;
  2444.     static REAL        win[4][36];
  2445.     static int        init = 1;
  2446. #ifdef OPTIMIZE
  2447.     static REAL        const1[12][6];
  2448.     static REAL        const3[36][18];
  2449. #else
  2450.     REAL        sum;
  2451.     REAL        tmp[12];
  2452.     static REAL        COS[4 * 36];
  2453. #endif
  2454.  
  2455.     if (init == 1) {
  2456.  
  2457.     // Type 0
  2458.     for (i = 0; i < 36; i++)
  2459.         win[0][i] = sin(PI36 * (i + 0.5));
  2460.  
  2461.     // Type 1
  2462.     for (i = 0; i < 18; i++)
  2463.         win[1][i] = sin(PI36 * (i + 0.5));
  2464.     for (i = 18; i < 24; i++)
  2465.         win[1][i] = 1.0;
  2466.     for (i = 24; i < 30; i++)
  2467.         win[1][i] = sin(PI12 * (i + 0.5 - 18));
  2468.     for (i = 30; i < 36; i++)
  2469.         win[1][i] = 0.0;
  2470.  
  2471.     // Type 3
  2472.     for (i = 0; i < 6; i++)
  2473.         win[3][i] = 0.0;
  2474.     for (i = 6; i < 12; i++)
  2475.         win[3][i] = sin(PI12 * (i + 0.5 - 6));
  2476.     for (i = 12; i < 18; i++)
  2477.         win[3][i] = 1.0;
  2478.     for (i = 18; i < 36; i++)
  2479.         win[3][i] = sin(PI36 * (i + 0.5));
  2480.  
  2481.     // Type 2
  2482.     for (i = 0; i < 12; i++)
  2483.         win[2][i] = sin(PI12 * (i + 0.5)) ;
  2484.     for (i = 12; i < 36; i++)
  2485.         win[2][i] = 0.0 ;
  2486.  
  2487. #ifdef OPTIMIZE
  2488.  
  2489.     // Pre-calculate cosine array
  2490.     N = 12;
  2491.     for (p = 0; p < N; p++)
  2492.         for (m = 0; m < N/2; m++)
  2493.         const1[p][m] = cos((PI/(2*N)) * (2*p+1+N/2) * (2*m+1));
  2494.  
  2495.     // Create an optimized cosine lookup table for the
  2496.     // block_type != 2 case
  2497.     for (p = 0; p < 36; p++) {
  2498.         int pconst = (p * 2) + 19;
  2499.  
  2500.         for (m = 0; m < 18; m++)
  2501.         // Calculate cosine value for this position
  2502.         const3[p][m] = cos(PI/(2*36) *
  2503.                   ((pconst * ((m << 1) + 1)) % 144));
  2504.     }
  2505.     init = 0;
  2506.     }
  2507.  
  2508.     if (block_type == 2) {
  2509.  
  2510.     // Clear the out[] array
  2511.     memset(&out[0], 0, sizeof(REAL) * 36);
  2512.  
  2513.     for (i = 0; i < 3; i++) {
  2514.         for (p = 0; p < 12; p++) {
  2515.         REAL sum = 0.0;
  2516.  
  2517.         for (m = 0; m < 6; m++)
  2518.             sum += in[i + 3 * m] * const1[p][m];
  2519.         out[6 * i + p + 6] += sum * win[2][p];
  2520.         }
  2521.     }
  2522.  
  2523.     } else {
  2524.  
  2525.     for (p = 0; p < 36; p++) {
  2526.         REAL sum = 0.0;
  2527.  
  2528.         for (m = 0; m < 18; m++)
  2529.         sum += in[m] * const3[p][m];
  2530.         out[p] = sum * win[block_type][p];
  2531.     }
  2532.     }
  2533.  
  2534. #else
  2535.     // Original code
  2536.  
  2537.     for (i = 0; i < 4*36; i++)
  2538.         COS[i] = cos(PI/(2*36) * i);
  2539.  
  2540.     init = 0;
  2541.     }
  2542.  
  2543.     for (i = 0; i < 36; i++)
  2544.     out[i]=0;
  2545.  
  2546.     if (block_type == 2){
  2547.     N = 12;
  2548.     for (i=0; i<3; i++){
  2549.         for (p = 0; p<N; p++){
  2550.         sum = 0.0;
  2551.         for(m=0; m<N/2; m++)
  2552.             sum += in[i+3*m] * cos( PI/(2*N)*(2*p+1+N/2)*(2*m+1) );
  2553.         tmp[p] = sum * win[block_type][p] ;
  2554.         }
  2555.         for (p=0; p<N; p++)
  2556.         out[6*i+p+6] += tmp[p];
  2557.        }
  2558.     } else {
  2559.     N = 36;
  2560.     for (p=0; p<N; p++){
  2561.         sum = 0.0;
  2562.         for (m=0; m < N/2; m++)
  2563.         sum += in[m] * COS[((2*p+1+N/2)*(2*m+1))%(4*36)];
  2564.         out[p] = sum * win[block_type][p];
  2565.     }
  2566.     }
  2567. #endif // OPTIMIZE
  2568. }
  2569.  
  2570. #endif // ASM_OPTIMIZE
  2571.  
  2572.  
  2573. void III_hybrid(
  2574.     REAL         fsIn[SSLIMIT],   /* freq samples per subband in */
  2575.     REAL         tsOut[SSLIMIT],  /* time samples per subband out */
  2576.     int             sb,
  2577.     int             ch,
  2578.     struct gr_info_s    *gr_info,
  2579.     frame_params    *fr_ps)
  2580. {
  2581.     int         ss;
  2582. #ifdef OPTIMIZE
  2583.     static REAL     rawout[36];
  2584. #else
  2585.     REAL     rawout[36];
  2586. #endif
  2587.     static REAL     prevblck[2][SBLIMIT][SSLIMIT];
  2588.     static int     init = 1;
  2589.     int         bt;
  2590.  
  2591.     if (init == 1) {
  2592.     int i, j, k;
  2593.       
  2594.     for (i = 0; i < 2; i++)
  2595.         for (j = 0; j < SBLIMIT; j++)
  2596.         for (k = 0; k < SSLIMIT; k++)
  2597.             prevblck[i][j][k] = 0.0;
  2598.     init = 0;
  2599.     }
  2600.  
  2601.     bt = (gr_info->window_switching_flag && gr_info->mixed_block_flag &&
  2602.         (sb < 2)) ? 0 : gr_info->block_type; 
  2603.  
  2604.     inv_mdct(fsIn, rawout, bt);
  2605.  
  2606.     /* Overlap addition */
  2607.     for (ss = 0; ss < SSLIMIT; ss++) {        // 18
  2608.     tsOut[ss] = rawout[ss] + prevblck[ch][sb][ss];
  2609.     prevblck[ch][sb][ss] = rawout[ss+18];
  2610.     }
  2611. }
  2612.  
  2613.  
  2614. /* Return the number of slots for main data of current frame */
  2615.  
  2616. int main_data_slots(frame_params fr_ps)
  2617. {
  2618.     int nSlots =
  2619.     (144 * bitrate[fr_ps.header->version][2][fr_ps.header->bitrate_index]) /
  2620.     s_freq[fr_ps.header->version][fr_ps.header->sampling_frequency];
  2621.  
  2622.     if (fr_ps.header->version != MPEG_PHASE2_LSF) {
  2623.     if (fr_ps.stereo == 1)
  2624.         nSlots -= 17;
  2625.     else
  2626.         nSlots -=32;
  2627.     } else {
  2628.     nSlots = nSlots / 2;
  2629.     if (fr_ps.stereo == 1)
  2630.         nSlots -= 9;
  2631.     else
  2632.         nSlots -=17;
  2633.     }
  2634.  
  2635.     if (fr_ps.header->padding)
  2636.     nSlots++;
  2637.     nSlots -= 4;
  2638.     if (fr_ps.header->error_protection)
  2639.     nSlots -= 2;
  2640.  
  2641.     return nSlots;
  2642. }
  2643.