home *** CD-ROM | disk | FTP | other *** search
/ Da Capo / da_capo_vol1.bin / programs / amiga / misc / mpegaudio / decode.c < prev    next >
C/C++ Source or Header  |  1994-03-21  |  24KB  |  678 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][S