home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / converters / audiompeg_1 / !AudioMPEG / c / decode < prev    next >
Text File  |  1995-05-16  |  25KB  |  698 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. decode.c
  4. **********************************************************************/
  5. /**********************************************************************
  6.  * MPEG/audio coding/decoding software, work in progress              *
  7.  *   NOT for public distribution until verified and approved by the   *
  8.  *   MPEG/audio committee.  For further information, please contact   *
  9.  *   Davis Pan, 508-493-2241, e-mail: pan@3d.enet.dec.com          *
  10.  *                                                                    *
  11.  * VERSION 3.9                                                       *
  12.  *   changes made since last update:                                  *
  13.  *   date   programmers         comment                               *
  14.  * 2/25/91  Douglas Wong,       start of version 1.0 records          *
  15.  *          Davis Pan                                                 *
  16.  * 3/06/91  Douglas Wong        rename: setup.h to dedef.h            *
  17.  *                                      dfilter to defilter           *
  18.  *                                      dwindow to dewindow           *
  19.  *                              integrated "quantizer", "scalefactor" *
  20.  *                              combined window_samples routine into  *
  21.  *                              filter samples                        *
  22.  * 3/31/91  Bill Aspromonte     replaced read_filter by               *
  23.  *                              create_syn_filter and introduced a    *
  24.  *                              new Sub-Band Synthesis routine called *
  25.  *                              SubBandSynthesis()                    *
  26.  * 5/10/91  Vish (PRISM)        Ported to Macintosh and Unix.         *
  27.  *                              Changed "out_fifo()" so that last     *
  28.  *                              unfilled block is also written out.   *
  29.  *                              "create_syn_filter()" was modified so *
  30.  *                              that calculation precision is same as *
  31.  *                              in specification tables.              *
  32.  *                              Changed "decode_scale()" to reflect   *
  33.  *                              specifications.                       *
  34.  *                              Removed all routines used by          *
  35.  *                              "synchronize_buffer()".  This is now  *
  36.  *                              replaced by "seek_sync()".            *
  37.  *                              Incorporated Jean-Georges Fritsch's   *
  38.  *                              "bitstream.c" package.                *
  39.  *                              Deleted "reconstruct_sample()".       *
  40.  * 27jun91  dpwe (Aware)        Passed outFile and &sampFrames as     *
  41.  *                              args to out_fifo() - were global.     *
  42.  *                              Moved "alloc_*" reader to common.c.   *
  43.  *                              alloc, sblimit, stereo passed via new *
  44.  *                              'frame_params struct (were globals).  *
  45.  *                              Added JOINT STEREO decoding, lyrs I&II*
  46.  *                              Affects: decode_bitalloc,buffer_samps *
  47.  *                              Plus a few other cleanups.            *
  48.  * 6/10/91   Earle Jennings     conditional expansion added in        *
  49.  *                              II_dequantize_sample to handle range  *
  50.  *                              problems in MSDOS version             *
  51.  * 8/8/91    Jens Spille        Change for MS-C6.00                   *
  52.  *10/1/91    S.I. Sudharsanan,  Ported to IBM AIX platform.           *
  53.  *           Don H. Lee,                                              *
  54.  *           Peter W. Farrett                                         *
  55.  *10/3/91    Don H. Lee         implemented CRC-16 error protection   *
  56.  *                              newly introduced functions are        *
  57.  *                              buffer_CRC and recover_CRC_error.     *
  58.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  59.  *                              important fixes involved changing     *
  60.  *                              16-bit ints to long or unsigned in    *
  61.  *                              bit alloc routines for quant of 65535 *
  62.  *                              and passing proper function args.     *
  63.  *                              Removed "Other Joint Stereo" option   *
  64.  *                              and made bitrate be total channel     *
  65.  *                              bitrate, irrespective of the mode.    *
  66.  *                              Fixed many small bugs & reorganized.  *
  67.  * 7/27/92  Juan Pineda         Bug fix in SubBandSynthesis()         *
  68.  **********************************************************************/
  69.  
  70. #include        "common.h"
  71. #include        "decoder.h"
  72.  
  73. /***************************************************************
  74.  *
  75.  * This module contains the core of the decoder ie all the
  76.  * computational routines. (Layer I and II only)
  77.  * Functions are common to both layer unless
  78.  * otherwise specified.
  79.  *
  80.  ***************************************************************/
  81.  
  82. /*****************************************************************
  83.  *
  84.  * The following routines decode the system information
  85.  *
  86.  ****************************************************************/
  87.  
  88. /************ Layer I, Layer II & Layer III ******************/
  89.  
  90. void decode_info(bs, fr_ps)
  91. Bit_stream_struc *bs;
  92. frame_params *fr_ps;
  93. {
  94.     layer *hdr = fr_ps->header;
  95.  
  96.     hdr->version = get1bit(bs);
  97.     hdr->lay = 4-getbits(bs,2);
  98.     hdr->error_protection = !get1bit(bs); /* error protect. TRUE/FALSE */
  99.     hdr->bitrate_index = getbits(bs,4);
  100.     hdr->sampling_frequency = getbits(bs,2);
  101.     hdr->padding = get1bit(bs);
  102.     hdr->extension = get1bit(bs);
  103.     hdr->mode = getbits(bs,2);
  104.     hdr->mode_ext = getbits(bs,2);
  105.     hdr->copyright = get1bit(bs);
  106.     hdr->original = get1bit(bs);
  107.     hdr->emphasis = getbits(bs,2);
  108. }
  109.  
  110. /*******************************************************************
  111.  *
  112.  * The bit allocation information is decoded. Layer I
  113.  * has 4 bit per subband whereas Layer II is Ws and bit rate
  114.  * dependent.
  115.  *
  116.  ********************************************************************/
  117.  
  118. /**************************** Layer II *************/
  119.  
  120. void II_decode_bitalloc(bs, bit_alloc, fr_ps)
  121. Bit_stream_struc *bs;
  122. unsigned int bit_alloc[2][SBLIMIT];
  123. frame_params *fr_ps;
  124. {
  125.     int i,j;
  126.     int stereo = fr_ps->stereo;
  127.     int sblimit = fr_ps->sblimit;
  128.     int jsbound = fr_ps->jsbound;
  129.     al_table *alloc = fr_ps->alloc;
  130.  
  131.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  132.         bit_alloc[j][i] = (char) getbits(bs,(*alloc)[i][0].bits);
  133.  
  134.     for (i=jsbound;i<sblimit;i++) /* expand to 2 channels */
  135.         bit_alloc[0][i] = bit_alloc[1][i] =
  136.             (char) getbits(bs,(*alloc)[i][0].bits);
  137.  
  138.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  139.         bit_alloc[j][i] = 0;
  140. }
  141.  
  142. /**************************** Layer I *************/
  143.  
  144. void I_decode_bitalloc(bs, bit_alloc, fr_ps)
  145. Bit_stream_struc *bs;
  146. unsigned int bit_alloc[2][SBLIMIT];
  147. frame_params *fr_ps;
  148. {
  149.     int i,j;
  150.     int stereo  = fr_ps->stereo;
  151.     int sblimit = fr_ps->sblimit;
  152.     int jsbound = fr_ps->jsbound;
  153.     int b;
  154.  
  155.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  156.         bit_alloc[j][i] = getbits(bs,4);
  157.     for (i=jsbound;i<SBLIMIT;i++) {
  158.         b = getbits(bs,4);
  159.         for (j=0;j<stereo;j++)
  160.             bit_alloc[j][i] = b;
  161.     }
  162. }
  163.  
  164. /*****************************************************************
  165.  *
  166.  * The following two functions implement the layer I and II
  167.  * format of scale factor extraction. Layer I involves reading
  168.  * 6 bit per subband as scale factor. Layer II requires reading
  169.  * first the scfsi which in turn indicate the number of scale factors
  170.  * transmitted.
  171.  *    Layer I : I_decode_scale
  172.  *   Layer II : II_decode_scale
  173.  *
  174.  ****************************************************************/
  175.  
  176. /************************** Layer I stuff ************************/
  177.  
  178. void I_decode_scale(bs, bit_alloc, scale_index, fr_ps)
  179. Bit_stream_struc *bs;
  180. unsigned int bit_alloc[2][SBLIMIT], scale_index[2][3][SBLIMIT];
  181. frame_params *fr_ps;
  182. {
  183.     int i,j;
  184.     int stereo = fr_ps->stereo;
  185.     int sblimit = fr_ps->sblimit;
  186.  
  187.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  188.         if (!bit_alloc[j][i])
  189.             scale_index[j][0][i] = SCALE_RANGE-1;
  190.         else                    /* 6 bit per scale factor */
  191.             scale_index[j][0][i] =  getbits(bs,6);
  192.  
  193. }
  194.  
  195. /*************************** Layer II stuff ***************************/
  196.  
  197. void II_decode_scale(bs,scfsi, bit_alloc,scale_index, fr_ps)
  198. Bit_stream_struc *bs;
  199. unsigned int scfsi[2][SBLIMIT], bit_alloc[2][SBLIMIT],
  200.              scale_index[2][3][SBLIMIT];
  201. frame_params *fr_ps;
  202. {
  203.     int i,j;
  204.     int stereo = fr_ps->stereo;
  205.     int sblimit = fr_ps->sblimit;
  206.    
  207.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) /* 2 bit scfsi */
  208.         if (bit_alloc[j][i]) scfsi[j][i] = (char) getbits(bs,2);
  209.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++)   
  210.         scfsi[j][i] = 0;
  211.  
  212.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) {
  213.         if (bit_alloc[j][i])   
  214.             switch (scfsi[j][i]) {
  215.                 /* all three scale factors transmitted */
  216.              case 0 : scale_index[j][0][i] = getbits(bs,6);
  217.                 scale_index[j][1][i] = getbits(bs,6);
  218.                 scale_index[j][2][i] = getbits(bs,6);
  219.                 break;
  220.                 /* scale factor 1 & 3 transmitted */
  221.              case 1 : scale_index[j][0][i] =
  222.                  scale_index[j][1][i] = getbits(bs,6);
  223.                 scale_index[j][2][i] = getbits(bs,6);
  224.                 break;
  225.                 /* scale factor 1 & 2 transmitted */
  226.              case 3 : scale_index[j][0][i] = getbits(bs,6);
  227.                 scale_index[j][1][i] =
  228.                     scale_index[j][2][i] =  getbits(bs,6);
  229.                 break;
  230.                 /* only one scale factor transmitted */
  231.              case 2 : scale_index[j][0][i] =
  232.                  scale_index[j][1][i] =
  233.                      scale_index[j][2][i] = getbits(bs,6);
  234.                 break;
  235.                 default : break;
  236.             }
  237.         else {
  238.             scale_index[j][0][i] = scale_index[j][1][i] =
  239.                 scale_index[j][2][i] = SCALE_RANGE-1;
  240.         }
  241.     }
  242.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++) {
  243.         scale_index[j][0][i] = scale_index[j][1][i] =
  244.             scale_index[j][2][i] = SCALE_RANGE-1;
  245.     }
  246. }
  247.  
  248. /**************************************************************
  249.  *
  250.  *   The following two routines take care of reading the
  251.  * compressed sample from the bit stream for both layer 1 and
  252.  * layer 2. For layer 1, read the number of bits as indicated
  253.  * by the bit_alloc information. For layer 2, if grouping is
  254.  * indicated for a particular subband, then the sample size has
  255.  * to be read from the bits_group and the merged samples has
  256.  * to be decompose into the three distinct samples. Otherwise,
  257.  * it is the same for as layer one.
  258.  *
  259.  **************************************************************/
  260.  
  261. /******************************* Layer I stuff ******************/
  262.  
  263. void I_buffer_sample(bs, sample, bit_alloc, fr_ps)
  264. unsigned int FAR sample[2][3][SBLIMIT];
  265. unsigned int bit_alloc[2][SBLIMIT];
  266. Bit_stream_struc *bs;
  267. frame_params *fr_ps;
  268. {
  269.     int i,j,k;
  270.     int stereo = fr_ps->stereo;
  271.     int sblimit = fr_ps->sblimit;
  272.     int jsbound = fr_ps->jsbound;
  273.     unsigned int s;
  274.  
  275.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  276.         if ( (k = bit_alloc[j][i]) == 0)
  277.             sample[j][0][i] = 0;
  278.         else 
  279.             sample[j][0][i] = (unsigned int) getbits(bs,k+1);
  280.     for (i=jsbound;i<SBLIMIT;i++) {
  281.         if ( (k = bit_alloc[0][i]) == 0)
  282.             s = 0;
  283.         else 
  284.             s = (unsigned int)getbits(bs,k+1);
  285.         for (j=0;j<stereo;j++)
  286.             sample[j][0][i]    = s;
  287.     }
  288. }
  289.  
  290. /*************************** Layer II stuff ************************/
  291.  
  292. void II_buffer_sample(bs,sample,bit_alloc,fr_ps)
  293. unsigned int FAR sample[2][3][SBLIMIT];
  294. unsigned int bit_alloc[2][SBLIMIT];
  295. Bit_stream_struc *bs;
  296. frame_params *fr_ps;
  297. {
  298.     int i,j,k,m;
  299.     int stereo = fr_ps->stereo;
  300.     int sblimit = fr_ps->sblimit;
  301.     int jsbound = fr_ps->jsbound;
  302.     al_table *alloc = fr_ps->alloc;
  303.  
  304.     for (i=0;i<sblimit;i++) for (j=0;j<((i<jsbound)?stereo:1);j++) {
  305.         if (bit_alloc[j][i]) {
  306.             /* check for grouping in subband */
  307.             if ((*alloc)[i][bit_alloc[j][i]].group==3)
  308.                 for (m=0;m<3;m++) {
  309.                     k = (*alloc)[i][bit_alloc[j][i]].bits;
  310.                     sample[j][m][i] = (unsigned int) getbits(bs,k);
  311.                 }         
  312.             else {              /* bit_alloc = 3, 5, 9 */
  313.                 unsigned int nlevels, c=0;
  314.  
  315.                 nlevels = (*alloc)[i][bit_alloc[j][i]].steps;
  316.                 k=(*alloc)[i][bit_alloc[j][i]].bits;
  317.                 c = (unsigned int) getbits(bs, k);
  318.                 for (k=0;k<3;k++) {
  319.                     sample[j][k][i] = c % nlevels;
  320.                     c /= nlevels;
  321.                 }
  322.             }
  323.         }
  324.         else {                  /* for no sample transmitted */
  325.             for (k=0;k<3;k++) sample[j][k][i] = 0;
  326.         }
  327.         if(stereo == 2 && i>= jsbound) /* joint stereo : copy L to R */
  328.             for (k=0;k<3;k++) sample[1][k][i] = sample[0][k][i];
  329.     }
  330.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++) for (k=0;k<3;k++)
  331.         sample[j][k][i] = 0;
  332. }      
  333.  
  334. /**************************************************************
  335.  *
  336.  *   Restore the compressed sample to a factional number.
  337.  *   first complement the MSB of the sample
  338.  *    for layer I :
  339.  *    Use s = (s' + 2^(-nb+1) ) * 2^nb / (2^nb-1)
  340.  *   for Layer II :
  341.  *   Use the formula s = s' * c + d
  342.  *
  343.  **************************************************************/
  344.  
  345. static double c[17] = { 1.33333333333, 1.60000000000, 1.14285714286,
  346.                         1.77777777777, 1.06666666666, 1.03225806452,
  347.                         1.01587301587, 1.00787401575, 1.00392156863,
  348.                         1.00195694716, 1.00097751711, 1.00048851979,
  349.                         1.00024420024, 1.00012208522, 1.00006103888,
  350.                         1.00003051851, 1.00001525902 };
  351.  
  352. static double d[17] = { 0.500000000, 0.500000000, 0.250000000, 0.500000000,
  353.                         0.125000000, 0.062500000, 0.031250000, 0.015625000,
  354.                         0.007812500, 0.003906250, 0.001953125, 0.0009765625,
  355.                         0.00048828125, 0.00024414063, 0.00012207031,
  356.                         0.00006103516, 0.00003051758 };
  357.  
  358. /************************** Layer II stuff ************************/
  359.  
  360. void II_dequantize_sample(sample, bit_alloc, fraction, fr_ps)
  361. unsigned int FAR sample[2][3][SBLIMIT];
  362. unsigned int bit_alloc[2][SBLIMIT];
  363. double FAR fraction[2][3][SBLIMIT];
  364. frame_params *fr_ps;
  365. {
  366.     int i, j, k, x;
  367.     int stereo = fr_ps->stereo;
  368.     int sblimit = fr_ps->sblimit;
  369.     al_table *alloc = fr_ps->alloc;
  370.  
  371.     for (i=0;i<sblimit;i++)  for (j=0;j<3;j++) for (k=0;k<stereo;k++)
  372.         if (bit_alloc[k][i]) {
  373.  
  374.             /* locate MSB in the sample */
  375.             x = 0;
  376.             while ((1L<<x) < (*alloc)[i][bit_alloc[k][i]].steps) x++;
  377.  
  378.             /* MSB inversion */
  379.             if (((sample[k][j][i] >> x-1) & 1) == 1)
  380.                 fraction[k][j][i] = 0.0;
  381.             else  fraction[k][j][i] = -1.0;
  382.  
  383.             /* Form a 2's complement sample */
  384.             fraction[k][j][i] += (double) (sample[k][j][i] & ((1<<x-1)-1)) /
  385.                 (double) (1L<<x-1);
  386.  
  387.             /* Dequantize the sample */
  388.             fraction[k][j][i] += d[(*alloc)[i][bit_alloc[k][i]].quant];
  389.             fraction[k][j][i] *= c[(*alloc)[i][bit_alloc[k][i]].quant];
  390.         }
  391.         else fraction[k][j][i] = 0.0;   
  392.    
  393.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<3;j++) for(k=0;k<stereo;k++)
  394.         fraction[k][j][i] = 0.0;
  395. }
  396.  
  397. /***************************** Layer I stuff ***********************/
  398.  
  399. void I_dequantize_sample(sample, fraction, bit_alloc, fr_ps)
  400. unsigned int FAR sample[2][3][SBLIMIT];
  401. unsigned int bit_alloc[2][SBLIMIT];
  402. double FAR fraction[2][3][SBLIMIT];
  403. frame_params *fr_ps;
  404. {
  405.     int i, nb, k;
  406.     int stereo = fr_ps->stereo;
  407.     int sblimit = fr_ps->sblimit;
  408.  
  409.     for (i=0;i<SBLIMIT;i++)
  410.         for (k=0;k<stereo;k++)
  411.             if (bit_alloc[k][i]) {
  412.                 nb = bit_alloc[k][i] + 1;
  413.                 if (((sample[k][0][i] >> nb-1) & 1) == 1) fraction[k][0][i] = 0.0;
  414.                 else fraction[k][0][i] = -1.0;
  415.                 fraction[k][0][i] += (double) (sample[k][0][i] & ((1<<nb-1)-1)) /
  416.                     (double) (1L<<nb-1);
  417.  
  418.                 fraction[k][0][i] =
  419.                     (double) (fraction[k][0][i]+1.0/(double)(1L<<nb-1)) *
  420.                         (double) (1L<<nb) / (double) ((1L<<nb)-1);
  421.             }
  422.             else fraction[k][0][i] = 0.0;
  423. }
  424.  
  425. /************************************************************
  426.  *
  427.  *   Restore the original value of the sample ie multiply
  428.  *    the fraction value by its scalefactor.
  429.  *
  430.  ************************************************************/
  431.  
  432. /************************* Layer II Stuff **********************/
  433.  
  434. void II_denormalize_sample(fraction, scale_index,fr_ps,x)
  435. double FAR fraction[2][3][SBLIMIT];
  436. unsigned int scale_index[2][3][SBLIMIT];
  437. frame_params *fr_ps;
  438. int x;
  439. {
  440.     int i,j,k;
  441.     int stereo = fr_ps->stereo;
  442.     int sblimit = fr_ps->sblimit;
  443.  
  444.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) {
  445.         fraction[j][0][i] *= multiple[scale_index[j][x][i]];
  446.         fraction[j][1][i] *= multiple[scale_index[j][x][i]];
  447.         fraction[j][2][i] *= multiple[scale_index[j][x][i]];
  448.     }
  449. }
  450.  
  451. /**************************** Layer I stuff ******************************/
  452.  
  453. void I_denormalize_sample(fraction,scale_index,fr_ps)
  454. double FAR fraction[2][3][SBLIMIT];
  455. unsigned int scale_index[2][3][SBLIMIT];
  456. frame_params *fr_ps;
  457. {
  458.     int i,j,k;
  459.     int stereo = fr_ps->stereo;
  460.     int sblimit = fr_ps->sblimit;
  461.  
  462.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  463.         fraction[j][0][i] *= multiple[scale_index[j][0][i]];
  464. }
  465.  
  466. /*****************************************************************
  467.  *
  468.  * The following are the subband synthesis routines. They apply
  469.  * to both layer I and layer II stereo or mono. The user has to
  470.  * decide what parameters are to be passed to the routines.
  471.  *
  472.  ***************************************************************/
  473.  
  474. /*************************************************************
  475.  *
  476.  *   Pass the subband sample through the synthesis window
  477.  *
  478.  **************************************************************/
  479.  
  480. /* create in synthesis filter */
  481.  
  482. void create_syn_filter(filter)
  483. double FAR filter[64][SBLIMIT];
  484. {
  485.     register int i,k;
  486.  
  487.     for (i=0; i<64; i++)
  488.         for (k=0; k<32; k++) {
  489.             if ((filter[i][k] = 1e9*cos((double)((PI64*i+PI4)*(2*k+1)))) >= 0)
  490.                 modf(filter[i][k]+0.5, &filter[i][k]);
  491.             else
  492.                 modf(filter[i][k]-0.5, &filter[i][k]);
  493.             filter[i][k] *= 1e-9;
  494.         }
  495. }
  496.  
  497. /***************************************************************
  498.  *
  499.  *   Window the restored sample
  500.  *
  501.  ***************************************************************/
  502.  
  503. /* read in synthesis window */
  504.  
  505. void read_syn_window(window)
  506. double FAR window[HAN_SIZE];
  507. {
  508.     int i,j[4];
  509.     FILE *fp;
  510.     double f[4];
  511.     char t[150];
  512.  
  513.     if (!(fp = OpenTableFile("dewindow") )) {
  514.         printf("Please check synthesis window table 'dewindow'\n");
  515.         exit(1);
  516.     }
  517.     for (i=0;i<512;i+=4) {
  518.         fgets(t, 150, fp);
  519.         sscanf(t,"D[%d] = %lf D[%d] = %lf D[%d] = %lf D[%d] = %lf\n",
  520.                j, f,j+1,f+1,j+2,f+2,j+3,f+3);
  521.         if (i==j[0]) {
  522.             window[i] = f[0];
  523.             window[i+1] = f[1];
  524.             window[i+2] = f[2];
  525.             window[i+3] = f[3];
  526.         }
  527.         else {
  528.             printf("Check index in synthesis window table\n");
  529.             exit(1);
  530.         }
  531.         fgets(t,150,fp);
  532.     }
  533.     fclose(fp);
  534. }
  535.  
  536. int SubBandSynthesis (bandPtr, channel, samples)
  537. double *bandPtr;
  538. int channel;
  539. short *samples;
  540. {
  541.     register int i,j,k;
  542.     register double *bufOffsetPtr, sum;
  543.     static int init = 1;
  544.     typedef double NN[64][32];
  545.     static NN FAR *filter;
  546.     typedef double BB[2][2*HAN_SIZE];
  547.     static BB FAR *buf;
  548.     static int bufOffset = 64;
  549.     static double FAR *window;
  550.     int clip = 0;               /* count & return how many samples clipped */
  551.  
  552.     if (init) {
  553.         buf = (BB FAR *) mem_alloc(sizeof(BB),"BB");
  554.         filter = (NN FAR *) mem_alloc(sizeof(NN), "NN");
  555.         create_syn_filter(*filter);
  556.         window = (double FAR *) mem_alloc(sizeof(double) * HAN_SIZE, "WIN");
  557.         read_syn_window(window);
  558.         bufOffset = 64;
  559.         init = 0;
  560.     }
  561.     if (channel == 0) bufOffset = (bufOffset - 64) & 0x3ff;
  562.     bufOffsetPtr = &((*buf)[channel][bufOffset]);
  563.  
  564.     for (i=0; i<64; i++) {
  565.         sum = 0;
  566.         for (k=0; k<32; k++)
  567.             sum += bandPtr[k] * (*filter)[i][k];
  568.         bufOffsetPtr[i] = sum;
  569.     }
  570.     /*  S(i,j) = D(j+32i) * U(j+32i+((i+1)>>1)*64)  */
  571.     /*  samples(i,j) = MWindow(j+32i) * bufPtr(j+32i+((i+1)>>1)*64)  */
  572.     for (j=0; j<32; j++) {
  573.         sum = 0;
  574.         for (i=0; i<16; i++) {
  575.             k = j + (i<<5);
  576.             sum += window[k] * (*buf) [channel] [( (k + ( ((i+1)>>1) <<6) ) +
  577.                                                   bufOffset) & 0x3ff];
  578.         }
  579.  
  580. /*   {long foo = (sum > 0) ? sum * SCALE + 0.5 : sum * SCALE - 0.5; */
  581.      {long foo = sum * SCALE;
  582.      if (foo >= (long) SCALE)      {samples[j] = SCALE-1; ++clip;}
  583.      else if (foo < (long) -SCALE) {samples[j] = -SCALE;  ++clip;}
  584.      else                           samples[j] = foo;
  585.  }
  586.     }
  587.     return(clip);
  588. }
  589.  
  590.  
  591. void out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames)
  592. short FAR pcm_sample[2][3][SBLIMIT];
  593. int num;
  594. frame_params *fr_ps;
  595. int done;
  596. FILE *outFile;
  597. unsigned long *psampFrames;
  598. {
  599.     int i,j,l;
  600.     int stereo = fr_ps->stereo;
  601.     int sblimit = fr_ps->sblimit;
  602.     static short int outsamp[1600];
  603.     static long k = 0;
  604.     short int val, swapped[1600];
  605.     int byte1, byte2, ii;
  606.  
  607.     if (!done)
  608.         for (i=0;i<num;i++) for (j=0;j<SBLIMIT;j++) {
  609.             (*psampFrames)++;
  610.             for (l=0;l<stereo;l++) {
  611.                 if (!(k%1600) && k) {
  612.                     for (ii = 0; ii < 1600; ii++)
  613.                     {
  614.                       val = outsamp[ii];
  615.                       byte1 = val & 0x00ff;
  616.                       byte2 = val & 0xff00;
  617.                       swapped[ii] = (byte1<<8) | (byte2>>8);
  618.                     }
  619.                     if (highbytefirst == 1)
  620.                     {
  621.                       fwrite(swapped,2,1600,outFile);
  622.                     } else {
  623.                       fwrite(outsamp,2,1600,outFile);
  624.                     }
  625.                     k = 0;
  626.                 }
  627.                 outsamp[k++] = pcm_sample[l][i][j];
  628.             }
  629.         }
  630.     else {
  631.        for (ii = 0; ii < (int)k; ii++)
  632.        {
  633.          val = outsamp[ii];
  634.          byte1 = val & 0x00ff;
  635.          byte2 = val & 0xff00;
  636.          swapped[ii] = (byte1<<8) | (byte2>>8);
  637.        }
  638.        if (highbytefirst == 1)
  639.        {
  640.          fwrite(swapped,2,(int)k,outFile);
  641.        } else {
  642.          fwrite(outsamp,2,(int)k,outFile);
  643.        }
  644.        k = 0;
  645.     }
  646. }
  647.  
  648. void  buffer_CRC(bs, old_crc)
  649. Bit_stream_struc  *bs;
  650. unsigned int  *old_crc;
  651. {
  652.     *old_crc = getbits(bs, 16);
  653. }
  654.  
  655. void  recover_CRC_error(pcm_sample, error_count, fr_ps, outFile, psampFrames)
  656. short FAR pcm_sample[2][3][SBLIMIT];
  657. int error_count;
  658. frame_params *fr_ps;
  659. FILE *outFile;
  660. unsigned long *psampFrames;
  661. {
  662.     int  stereo = fr_ps->stereo;
  663.     int  num, done, i;
  664.     int  samplesPerFrame, samplesPerSlot;
  665.     layer  *hdr = fr_ps->header;
  666.     long  offset;
  667.     short  *temp;
  668.  
  669.     num = 3;
  670.     if (hdr->lay == 1) num = 1;
  671.  
  672.     samplesPerSlot = SBLIMIT * num * stereo;
  673.     samplesPerFrame = samplesPerSlot * 32;
  674.  
  675.     if (error_count == 1) {     /* replicate previous error_free frame */
  676.         done = 1;
  677.         /* flush out fifo */
  678.         out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  679.         /* go back to the beginning of the previous frame */
  680.         offset = sizeof(short int) * samplesPerFrame;
  681.         fseek(outFile, -offset, SEEK_CUR);
  682.         done = 0;
  683.         for (i = 0; i < SCALE_BLOCK; i++) {
  684.             fread(pcm_sample, 2, samplesPerSlot, outFile);
  685.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  686.         }
  687.     }
  688.     else{                       /* mute the frame */
  689.         temp = (short*) pcm_sample;
  690.         done = 0;
  691.         for (i = 0; i < 2*3*SBLIMIT; i++)
  692.             *temp++ = MUTE;     /* MUTE value is in decoder.h */
  693.         for (i = 0; i < SCALE_BLOCK; i++)
  694.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  695.     }
  696. }
  697.  
  698.