home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 Secrets / Secrets2.iso / Audio / WAV / MaplayP / _SETUP.1 / sublay1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-02  |  6.8 KB  |  235 lines

  1. /*  sublay1.cpp
  2.  
  3.     Implementation of layer I subband objects */
  4.  
  5. /*
  6.  *  @(#) subband_layer_1.cc 1.7, last edit: 6/15/94 16:51:49
  7.  *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
  8.  *  @(#) Berlin University of Technology
  9.  *
  10.  *  This program is free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 2 of the License, or
  13.  *  (at your option) any later version.
  14.  *
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program; if not, write to the Free Software
  22.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25. /*
  26.  *  Changes from version 1.1 to 1.2:
  27.  *    - scalefactors itself instead of scalefactor indices are stored in
  28.  *      SubbandLayer1... objects
  29.  *    - check for small values in [-1.0E-7, 1.0E-7] removed, because the
  30.  *      test itself was slower than some SynthesisFilter::input_sample() calls
  31.  *    - check for illegal scalefactor index 63 removed
  32.  */
  33.  
  34. #include "sublay1.h"
  35. #include "scalfact.h"
  36.  
  37. // factors and offsets for sample requantization:
  38. static const real table_factor[15] = {
  39.   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),
  40.   (1.0/16.0) * (32.0/31.0), (1.0/32.0) * (64.0/63.0), (1.0/64.0) * (128.0/127.0),
  41.   (1.0/128.0) * (256.0/255.0), (1.0/256.0) * (512.0/511.0),
  42.   (1.0/512.0) * (1024.0/1023.0), (1.0/1024.0) * (2048.0/2047.0),
  43.   (1.0/2048.0) * (4096.0/4095.0), (1.0/4096.0) * (8192.0/8191.0),
  44.   (1.0/8192.0) * (16384.0/16383.0), (1.0/16384.0) * (32768.0/32767.0)
  45. };
  46.  
  47. static const real table_offset[15] = {
  48.   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),
  49.   ((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),
  50.   ((1.0/128.0)-1.0) * (256.0/255.0), ((1.0/256.0)-1.0) * (512.0/511.0),
  51.   ((1.0/512.0)-1.0) * (1024.0/1023.0), ((1.0/1024.0)-1.0) * (2048.0/2047.0),
  52.   ((1.0/2048.0)-1.0) * (4096.0/4095.0), ((1.0/4096.0)-1.0) * (8192.0/8191.0),
  53.   ((1.0/8192.0)-1.0) * (16384.0/16383.0), ((1.0/16384.0)-1.0) * (32768.0/32767.0)
  54. };
  55.  
  56. /**********************/    // used for single channel mode
  57. /*** Standard Class ***/    // and in derived class for intensity
  58. /**********************/    // stereo mode
  59.  
  60. SubbandLayer1::SubbandLayer1 (uint32 subbandnumber)
  61. {
  62.   this->subbandnumber = subbandnumber;
  63.   samplenumber = 0;
  64. }
  65.  
  66.  
  67. void SubbandLayer1::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
  68. {
  69.   if ((allocation = stream->get_bits (4)) == 15) ;
  70. //     cerr << "WARNING: stream contains an illegal allocation!\n";    // MPEG-stream is corrupted!
  71.  
  72.   if (crc)
  73.      crc->add_bits (allocation, 4);
  74.   if (allocation)
  75.   {
  76.      samplelength = allocation + 1;
  77.      factor = table_factor[allocation];
  78.     offset = table_offset[allocation];
  79.   }
  80. }
  81.  
  82.  
  83. void SubbandLayer1::read_scalefactor (Ibitstream *stream, Header *)
  84. {
  85.   if (allocation)
  86.      scalefactor = scalefactors[stream->get_bits (6)];
  87. }
  88.  
  89.  
  90. bool SubbandLayer1::read_sampledata (Ibitstream *stream)
  91. {
  92.   if (allocation)
  93.   {
  94.      sample = real (stream->get_bits (samplelength));
  95.   }
  96.   if (++samplenumber == 12)
  97.   {
  98.      samplenumber = 0;
  99.      return(TRUE);
  100.   }
  101.   return(FALSE);
  102. }
  103.  
  104.  
  105. bool SubbandLayer1::put_next_sample (e_channels channels,
  106.                       SynthesisFilter *filter1, SynthesisFilter *)
  107. {
  108.   if (allocation && channels != right)
  109.   {
  110.      register real scaled_sample = (sample * factor + offset) * scalefactor;
  111.      filter1->input_sample (scaled_sample, subbandnumber);
  112.   }
  113.   return(TRUE);
  114. }
  115.  
  116.  
  117. /******************************/
  118. /*** Intensity Stereo Class ***/
  119. /******************************/
  120.  
  121. SubbandLayer1IntensityStereo::SubbandLayer1IntensityStereo (uint32 subbandnumber)
  122. : SubbandLayer1 (subbandnumber)
  123. {
  124. }
  125.  
  126.  
  127. void SubbandLayer1IntensityStereo::read_scalefactor (Ibitstream *stream, Header *)
  128. {
  129.   if (allocation)
  130.   {
  131.      scalefactor = scalefactors[stream->get_bits (6)];
  132.      channel2_scalefactor = scalefactors[stream->get_bits (6)];
  133.   }
  134. }
  135.  
  136.  
  137. bool SubbandLayer1IntensityStereo::put_next_sample (e_channels channels,
  138.     SynthesisFilter *filter1, SynthesisFilter *filter2)
  139. {
  140.   if (allocation)
  141.   {
  142.     sample = sample * factor + offset;        // requantization
  143.      if (channels == both)
  144.     {
  145.         register real sample1 = sample * scalefactor,
  146.              sample2 = sample * channel2_scalefactor;
  147.         filter1->input_sample (sample1, subbandnumber);
  148.         filter2->input_sample (sample2, subbandnumber);
  149.      }
  150.      else if (channels == left)
  151.      {
  152.         register real sample1 = sample * scalefactor;
  153.         filter1->input_sample (sample1, subbandnumber);
  154.      }
  155.      else
  156.      {
  157.         register real sample2 = sample * channel2_scalefactor;
  158.         filter1->input_sample (sample2, subbandnumber);
  159.      }
  160.   }
  161.   return(TRUE);
  162. }
  163.  
  164.  
  165.  
  166. /********************/
  167. /*** Stereo Class ***/
  168. /********************/
  169.  
  170. SubbandLayer1Stereo::SubbandLayer1Stereo (uint32 subbandnumber)
  171. : SubbandLayer1 (subbandnumber)
  172. {
  173. }
  174.  
  175.  
  176. void SubbandLayer1Stereo::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
  177. {
  178.   allocation = stream->get_bits (4);
  179.   channel2_allocation = stream->get_bits (4);
  180.   if (crc)
  181.   {
  182.      crc->add_bits (allocation, 4);
  183.     crc->add_bits (channel2_allocation, 4);
  184.   }
  185.   if (allocation)
  186.   {
  187.      samplelength = allocation + 1;
  188.     factor = table_factor[allocation];
  189.     offset = table_offset[allocation];
  190.   }
  191.   if (channel2_allocation)
  192.   {
  193.     channel2_samplelength = channel2_allocation + 1;
  194.      channel2_factor = table_factor[channel2_allocation];
  195.      channel2_offset = table_offset[channel2_allocation];
  196.   }
  197. }
  198.  
  199.  
  200. void SubbandLayer1Stereo::read_scalefactor (Ibitstream *stream, Header *)
  201. {
  202.   if (allocation)
  203.      scalefactor = scalefactors[stream->get_bits (6)];
  204.   if (channel2_allocation)
  205.      channel2_scalefactor = scalefactors[stream->get_bits (6)];
  206. }
  207.  
  208.  
  209. bool SubbandLayer1Stereo::read_sampledata (Ibitstream *stream)
  210. {
  211.   bool returnvalue = SubbandLayer1::read_sampledata (stream);
  212.   if (channel2_allocation)
  213.   {
  214.      channel2_sample = real (stream->get_bits (channel2_samplelength));
  215.   }
  216.   return(returnvalue);
  217. }
  218.  
  219.  
  220. bool SubbandLayer1Stereo::put_next_sample (e_channels channels,
  221.                         SynthesisFilter *filter1, SynthesisFilter *filter2)
  222. {
  223.   SubbandLayer1::put_next_sample (channels, filter1, filter2);
  224.   if (channel2_allocation && channels != left)
  225.   {
  226.      register float sample2 = (channel2_sample * channel2_factor + channel2_offset) *
  227.                   channel2_scalefactor;
  228.      if (channels == both)
  229.         filter2->input_sample (sample2, subbandnumber);
  230.      else
  231.         filter1->input_sample (sample2, subbandnumber);
  232.   }
  233.   return(TRUE);
  234. }
  235.