home *** CD-ROM | disk | FTP | other *** search
/ Internet MPEG Audio Archive / IMAA.mdf / UTIL / UNIX / MAPLAY.TAR / subband_layer_1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-10  |  7.2 KB  |  238 lines

  1. /*
  2.  * @(#) subband_layer_1.c 1.2, last edit: 1/28/94 17:47:35
  3.  * @(#) (C) 1993/94 Tobias Bading (bading@cs.tu-berlin.de)
  4.  * @(#) Berlin University of Technology
  5.  */
  6.  
  7. #include <stdlib.h>
  8. #include "subband_layer_1.h"
  9. #include "scalefactors.h"
  10.  
  11.  
  12. // factors and offsets for sample requantization:
  13. static const real table_factor[15] = {
  14.   0.0, (1.0/2.0) * (4.0/3.0), (1.0/4.0) * (8.0/7.0), (1.0/8.0) * (16.0/15.0),
  15.   (1.0/16.0) * (32.0/31.0), (1.0/32.0) * (64.0/63.0), (1.0/64.0) * (128.0/127.0),
  16.   (1.0/128.0) * (256.0/255.0), (1.0/256.0) * (512.0/511.0),
  17.   (1.0/512.0) * (1024.0/1023.0), (1.0/1024.0) * (2048.0/2047.0),
  18.   (1.0/2048.0) * (4096.0/4095.0), (1.0/4096.0) * (8192.0/8191.0),
  19.   (1.0/8192.0) * (16384.0/16383.0), (1.0/16384.0) * (32768.0/32767.0)
  20. };
  21.  
  22. static const real table_offset[15] = {
  23.   0.0, ((1.0/2.0)-1.0) * (4.0/3.0), ((1.0/4.0)-1.0) * (8.0/7.0), ((1.0/8.0)-1.0) * (16.0/15.0),
  24.   ((1.0/16.0)-1.0) * (32.0/31.0), ((1.0/32.0)-1.0) * (64.0/63.0), ((1.0/64.0)-1.0) * (128.0/127.0),
  25.   ((1.0/128.0)-1.0) * (256.0/255.0), ((1.0/256.0)-1.0) * (512.0/511.0),
  26.   ((1.0/512.0)-1.0) * (1024.0/1023.0), ((1.0/1024.0)-1.0) * (2048.0/2047.0),
  27.   ((1.0/2048.0)-1.0) * (4096.0/4095.0), ((1.0/4096.0)-1.0) * (8192.0/8191.0),
  28.   ((1.0/8192.0)-1.0) * (16384.0/16383.0), ((1.0/16384.0)-1.0) * (32768.0/32767.0)
  29. };
  30.  
  31.  
  32.  
  33. /**********************/    // used for single channel mode
  34. /*** Standard Class ***/    // and in derived class for intensity
  35. /**********************/    // stereo mode
  36.  
  37. SubbandLayer1::SubbandLayer1 (unsigned subbandnumber)
  38. {
  39.   this->subbandnumber = subbandnumber;
  40.   samplenumber = 0;
  41. }
  42.  
  43.  
  44. void SubbandLayer1::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
  45. {
  46.   if ((allocation = stream->get_bits (4)) == 15)
  47.     cerr << "WARNING: stream contains an illegal allocation!\n";    // MPEG-stream is corrupted!
  48.   if (crc)
  49.     crc->add_bits (allocation, 4);
  50.   samplelength = allocation + 1;
  51.   factor = table_factor[allocation];
  52.   offset = table_offset[allocation];
  53. }
  54.  
  55.  
  56. void SubbandLayer1::read_scalefactor (Ibitstream *stream, Header *)
  57. {
  58.   if (allocation)
  59.     if ((scalefactor = stream->get_bits (6)) == 63)
  60.       cerr << "WARNING: stream contains an illegal scalefactor!\n";    // MPEG-stream is corrupted!
  61. }
  62.  
  63.  
  64. bool SubbandLayer1::read_sampledata (Ibitstream *stream)
  65. {
  66.   if (allocation)
  67.     sample = real (stream->get_bits (samplelength));
  68.   if (++samplenumber == 12)
  69.   {
  70.     samplenumber = 0;
  71.     return True;
  72.   }
  73.   else
  74.     return False;
  75. }
  76.  
  77.  
  78. bool SubbandLayer1::put_next_sample (e_channels channels,
  79.                      SynthesisFilter *filter1, SynthesisFilter *filter2)
  80. {
  81.   if (allocation && channels != right)
  82.   {
  83.     register real scaled_sample = (sample * factor + offset) * scalefactors[scalefactor];
  84. #ifdef DEBUG
  85.     if (scaled_sample < -1.0 || scaled_sample > 1.0)
  86.       cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  87.       // this should never occur
  88. #endif
  89.     if (scaled_sample < -1.0E-7 || scaled_sample > 1.0E-7)
  90.       filter1->input_sample (scaled_sample, subbandnumber);
  91.   }
  92.   return True;
  93. }
  94.  
  95.  
  96. /******************************/
  97. /*** Intensity Stereo Class ***/
  98. /******************************/
  99.  
  100. SubbandLayer1IntensityStereo::SubbandLayer1IntensityStereo (unsigned subbandnumber)
  101. : SubbandLayer1 (subbandnumber)
  102. {
  103. }
  104.  
  105.  
  106. void SubbandLayer1IntensityStereo::read_scalefactor (Ibitstream *stream, Header *)
  107. {
  108.   if (allocation)
  109.   {
  110.     scalefactor = stream->get_bits (6);
  111.     channel2_scalefactor = stream->get_bits (6);
  112.     if (scalefactor == 63 || channel2_scalefactor == 63)
  113.       cerr << "WARNING: stream contains an illegal scalefactor!\n";
  114.       // MPEG-stream is corrupted!
  115.   }
  116. }
  117.  
  118.  
  119. bool SubbandLayer1IntensityStereo::put_next_sample (e_channels channels,
  120.     SynthesisFilter *filter1, SynthesisFilter *filter2)
  121. {
  122.   if (allocation)
  123.   {
  124.     sample = sample * factor + offset;        // requantization
  125.     if (channels == both)
  126.     {
  127.       register real sample1 = sample * scalefactors[scalefactor],
  128.             sample2 = sample * scalefactors[channel2_scalefactor];
  129. #ifdef DEBUG
  130.       if (sample1 < -1.0 || sample1 > 1.0 || sample2 < -1.0 || sample2 > 1.0)
  131.     cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  132.     // this should never occur
  133. #endif
  134.       if (sample1 < -1.0E-7 || sample1 > 1.0E-7)
  135.     filter1->input_sample (sample1, subbandnumber);
  136.       if (sample2 < -1.0E-7 || sample2 > 1.0E-7)
  137.     filter2->input_sample (sample2, subbandnumber);
  138.     }
  139.     else if (channels == left)
  140.     {
  141.       register real sample1 = sample * scalefactors[scalefactor];
  142. #ifdef DEBUG
  143.       if (sample1 < -1.0 || sample1 > 1.0)
  144.     cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  145.     // this should never occur
  146. #endif
  147.       if (sample1 < -1.0E-7 || sample1 > 1.0E-7)
  148.     filter1->input_sample (sample1, subbandnumber);
  149.     }
  150.     else
  151.     {
  152.       register real sample2 = sample * scalefactors[channel2_scalefactor];
  153. #ifdef DEBUG
  154.       if (sample2 < -1.0 || sample2 > 1.0)
  155.     cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  156.     // this should never occur
  157. #endif
  158.       if (sample2 < -1.0E-7 || sample2 > 1.0E-7)
  159.     filter1->input_sample (sample2, subbandnumber);
  160.     }
  161.   }
  162.   return True;
  163. }
  164.  
  165.  
  166.  
  167. /********************/
  168. /*** Stereo Class ***/
  169. /********************/
  170.  
  171. SubbandLayer1Stereo::SubbandLayer1Stereo (unsigned subbandnumber)
  172. : SubbandLayer1 (subbandnumber)
  173. {
  174. }
  175.  
  176.  
  177. void SubbandLayer1Stereo::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
  178. {
  179.   allocation = stream->get_bits (4);
  180.   channel2_allocation = stream->get_bits (4);
  181.   if (crc)
  182.   {
  183.     crc->add_bits (allocation, 4);
  184.     crc->add_bits (channel2_allocation, 4);
  185.   }
  186.   if (allocation == 15 || channel2_allocation == 15)
  187.     cerr << "WARNING: stream contains an illegal allocation!\n";    // MPEG-stream is corrupted!
  188.   samplelength = allocation + 1;
  189.   channel2_samplelength = channel2_allocation + 1;
  190.   factor = table_factor[allocation];
  191.   offset = table_offset[allocation];
  192.   channel2_factor = table_factor[channel2_allocation];
  193.   channel2_offset = table_offset[channel2_allocation];
  194. }
  195.  
  196.  
  197. void SubbandLayer1Stereo::read_scalefactor (Ibitstream *stream, Header *)
  198. {
  199.   if (allocation)
  200.     if ((scalefactor = stream->get_bits (6)) == 63)
  201.       cerr << "WARNING: stream contains an illegal allocation!\n";    // MPEG-stream is corrupted!
  202.   if (channel2_allocation)
  203.     if ((channel2_scalefactor = stream->get_bits (6)) == 63)
  204.       cerr << "WARNING: stream contains an illegal allocation!\n";    // MPEG-stream is corrupted!
  205. }
  206.  
  207.  
  208. bool SubbandLayer1Stereo::read_sampledata (Ibitstream *stream)
  209. {
  210.   bool returnvalue = SubbandLayer1::read_sampledata (stream);
  211.   if (channel2_allocation)
  212.     channel2_sample = real (stream->get_bits (channel2_samplelength));
  213.   return returnvalue;
  214. }
  215.  
  216.  
  217. bool SubbandLayer1Stereo::put_next_sample (e_channels channels,
  218.                        SynthesisFilter *filter1, SynthesisFilter *filter2)
  219. {
  220.   SubbandLayer1::put_next_sample (channels, filter1, filter2);
  221.   if (channel2_allocation && channels != left)
  222.   {
  223.     register float sample2 = (channel2_sample * channel2_factor + channel2_offset)
  224.                  * scalefactors[channel2_scalefactor];
  225. #ifdef DEBUG
  226.     if (sample2 < -1.0 || sample2 > 1.0)
  227.       cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  228.       // this should never occur
  229. #endif
  230.     if (sample2 < -1.0E-7 || sample2 > 1.0E-7)
  231.       if (channels == both)
  232.     filter2->input_sample (sample2, subbandnumber);
  233.       else
  234.     filter1->input_sample (sample2, subbandnumber);
  235.   }
  236.   return True;
  237. }
  238.