home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09964.iso / sound / mpw32-5s.zip / SUBAND1.CPP < prev    next >
C/C++ Source or Header  |  1996-06-23  |  8KB  |  293 lines

  1. /*
  2.  *  @(#) subband_layer_1.cc 1.7, last edit: 6/15/94 16:51:49
  3.  *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
  4.  *  @(#) Berlin University of Technology
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /*
  22.  *  Changes from version 1.1 to 1.2:
  23.  *    - scalefactors itself instead of scalefactor indices are stored in
  24.  *      SubbandLayer1... objects
  25.  *    - check for small values in [-1.0E-7, 1.0E-7] removed, because the
  26.  *      test itself was slower than some SynthesisFilter::input_sample() calls
  27.  *    - check for illegal scalefactor index 63 removed
  28.  */
  29.  
  30. #define STRICT
  31. #define WIN32_LEAN_AND_MEAN
  32. #define NOMCX
  33. #define NOIME
  34. #define NOGDI
  35. #define NOUSER
  36. #define NOSOUND
  37. #define NOCOMM
  38. #define NODRIVERS
  39. #define OEMRESOURCE
  40. #define NONLS
  41. #define NOSERVICE
  42. #define NOKANJI
  43. #define NOMINMAX
  44. #define NOLOGERROR
  45. #define NOPROFILER
  46. #define NOMEMMGR
  47. #define NOLFILEIO
  48. #define NOOPENFILE
  49. #define NORESOURCE
  50. #define NOATOM
  51. #define NOLANGUAGE
  52. #define NOLSTRING
  53. #define NODBCS
  54. #define NOKEYBOARDINFO
  55. #define NOGDICAPMASKS
  56. #define NOCOLOR
  57. #define NOGDIOBJ
  58. #define NODRAWTEXT
  59. #define NOTEXTMETRIC
  60. #define NOSCALABLEFONT
  61. #define NOBITMAP
  62. #define NORASTEROPS
  63. #define NOMETAFILE
  64. #define NOSYSMETRICS
  65. #define NOSYSTEMPARAMSINFO
  66. #define NOMSG
  67. #define NOWINSTYLES
  68. #define NOWINOFFSETS
  69. #define NOSHOWWINDOW
  70. #define NODEFERWINDOWPOS
  71. #define NOVIRTUALKEYCODES
  72. #define NOKEYSTATES
  73. #define NOWH
  74. #define NOMENUS
  75. #define NOSCROLL
  76. #define NOCLIPBOARD
  77. #define NOICONS
  78. #define NOMB
  79. #define NOSYSCOMMANDS
  80. #define NOMDI
  81. #define NOCTLMGR
  82. #define NOWINMESSAGES
  83. #define NOHELP
  84. #define _WINUSER_
  85. #define __oleidl_h__
  86. #define _OLE2_H_
  87. #include <windows.h>
  88.  
  89. #include "suband1.h"
  90. #include "scalfact.h"
  91.  
  92.  
  93. // factors and offsets for sample requantization:
  94. static const real table_factor[15] = {
  95.   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),
  96.   (1.0/16.0) * (32.0/31.0), (1.0/32.0) * (64.0/63.0), (1.0/64.0) * (128.0/127.0),
  97.   (1.0/128.0) * (256.0/255.0), (1.0/256.0) * (512.0/511.0),
  98.   (1.0/512.0) * (1024.0/1023.0), (1.0/1024.0) * (2048.0/2047.0),
  99.   (1.0/2048.0) * (4096.0/4095.0), (1.0/4096.0) * (8192.0/8191.0),
  100.   (1.0/8192.0) * (16384.0/16383.0), (1.0/16384.0) * (32768.0/32767.0)
  101. };
  102.  
  103. static const real table_offset[15] = {
  104.   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),
  105.   ((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),
  106.   ((1.0/128.0)-1.0) * (256.0/255.0), ((1.0/256.0)-1.0) * (512.0/511.0),
  107.   ((1.0/512.0)-1.0) * (1024.0/1023.0), ((1.0/1024.0)-1.0) * (2048.0/2047.0),
  108.   ((1.0/2048.0)-1.0) * (4096.0/4095.0), ((1.0/4096.0)-1.0) * (8192.0/8191.0),
  109.   ((1.0/8192.0)-1.0) * (16384.0/16383.0), ((1.0/16384.0)-1.0) * (32768.0/32767.0)
  110. };
  111.  
  112.  
  113.  
  114. /**********************/    // used for single channel mode
  115. /*** Standard Class ***/    // and in derived class for intensity
  116. /**********************/    // stereo mode
  117.  
  118. SubbandLayer1::SubbandLayer1 (uint32 subbandnumber)
  119. {
  120.   this->subbandnumber = subbandnumber;
  121.   samplenumber = 0;
  122. }
  123.  
  124.  
  125. void SubbandLayer1::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
  126. {
  127.   if ((allocation = stream->get_bits (4)) == 15) ;
  128. //     cerr << "WARNING: stream contains an illegal allocation!\n";    // MPEG-stream is corrupted!
  129.  
  130.   if (crc)
  131.      crc->add_bits (allocation, 4);
  132.   if (allocation)
  133.   {
  134.      samplelength = allocation + 1;
  135.      factor = table_factor[allocation];
  136.     offset = table_offset[allocation];
  137.   }
  138. }
  139.  
  140.  
  141. void SubbandLayer1::read_scalefactor (Ibitstream *stream, Header *)
  142. {
  143.   if (allocation)
  144.      scalefactor = scalefactors[stream->get_bits (6)];
  145. }
  146.  
  147.  
  148. BOOL SubbandLayer1::read_sampledata (Ibitstream *stream)
  149. {
  150.   if (allocation)
  151.   {
  152.      sample = real (stream->get_bits (samplelength));
  153.   }
  154.   if (++samplenumber == 12)
  155.   {
  156.      samplenumber = 0;
  157.      return TRUE;
  158.   }
  159.   return FALSE;
  160. }
  161.  
  162.  
  163. BOOL SubbandLayer1::put_next_sample (e_channels channels,
  164.                       SynthesisFilter *filter1, SynthesisFilter *)
  165. {
  166.   if (allocation && channels != right)
  167.   {
  168.      register real scaled_sample = (sample * factor + offset) * scalefactor;
  169.      filter1->input_sample (scaled_sample, subbandnumber);
  170.   }
  171.   return TRUE;
  172. }
  173.  
  174.  
  175. /******************************/
  176. /*** Intensity Stereo Class ***/
  177. /******************************/
  178.  
  179. SubbandLayer1IntensityStereo::SubbandLayer1IntensityStereo (uint32 subbandnumber)
  180. : SubbandLayer1 (subbandnumber)
  181. {
  182. }
  183.  
  184.  
  185. void SubbandLayer1IntensityStereo::read_scalefactor (Ibitstream *stream, Header *)
  186. {
  187.   if (allocation)
  188.   {
  189.      scalefactor = scalefactors[stream->get_bits (6)];
  190.      channel2_scalefactor = scalefactors[stream->get_bits (6)];
  191.   }
  192. }
  193.  
  194.  
  195. BOOL SubbandLayer1IntensityStereo::put_next_sample (e_channels channels,
  196.     SynthesisFilter *filter1, SynthesisFilter *filter2)
  197. {
  198.   if (allocation)
  199.   {
  200.     sample = sample * factor + offset;        // requantization
  201.      if (channels == both)
  202.     {
  203.         register real sample1 = sample * scalefactor,
  204.              sample2 = sample * channel2_scalefactor;
  205.         filter1->input_sample (sample1, subbandnumber);
  206.         filter2->input_sample (sample2, subbandnumber);
  207.      }
  208.      else if (channels == left)
  209.      {
  210.         register real sample1 = sample * scalefactor;
  211.         filter1->input_sample (sample1, subbandnumber);
  212.      }
  213.      else
  214.      {
  215.         register real sample2 = sample * channel2_scalefactor;
  216.         filter1->input_sample (sample2, subbandnumber);
  217.      }
  218.   }
  219.   return TRUE;
  220. }
  221.  
  222.  
  223.  
  224. /********************/
  225. /*** Stereo Class ***/
  226. /********************/
  227.  
  228. SubbandLayer1Stereo::SubbandLayer1Stereo (uint32 subbandnumber)
  229. : SubbandLayer1 (subbandnumber)
  230. {
  231. }
  232.  
  233.  
  234. void SubbandLayer1Stereo::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
  235. {
  236.   allocation = stream->get_bits (4);
  237.   channel2_allocation = stream->get_bits (4);
  238.   if (crc)
  239.   {
  240.      crc->add_bits (allocation, 4);
  241.     crc->add_bits (channel2_allocation, 4);
  242.   }
  243.   if (allocation)
  244.   {
  245.      samplelength = allocation + 1;
  246.     factor = table_factor[allocation];
  247.     offset = table_offset[allocation];
  248.   }
  249.   if (channel2_allocation)
  250.   {
  251.     channel2_samplelength = channel2_allocation + 1;
  252.      channel2_factor = table_factor[channel2_allocation];
  253.      channel2_offset = table_offset[channel2_allocation];
  254.   }
  255. }
  256.  
  257.  
  258. void SubbandLayer1Stereo::read_scalefactor (Ibitstream *stream, Header *)
  259. {
  260.   if (allocation)
  261.      scalefactor = scalefactors[stream->get_bits (6)];
  262.   if (channel2_allocation)
  263.      channel2_scalefactor = scalefactors[stream->get_bits (6)];
  264. }
  265.  
  266.  
  267. BOOL SubbandLayer1Stereo::read_sampledata (Ibitstream *stream)
  268. {
  269.   BOOL returnvalue = SubbandLayer1::read_sampledata (stream);
  270.   if (channel2_allocation)
  271.   {
  272.      channel2_sample = real (stream->get_bits (channel2_samplelength));
  273.   }
  274.   return returnvalue;
  275. }
  276.  
  277.  
  278. BOOL SubbandLayer1Stereo::put_next_sample (e_channels channels,
  279.                         SynthesisFilter *filter1, SynthesisFilter *filter2)
  280. {
  281.   SubbandLayer1::put_next_sample (channels, filter1, filter2);
  282.   if (channel2_allocation && channels != left)
  283.   {
  284.      register float sample2 = (channel2_sample * channel2_factor + channel2_offset) *
  285.                   channel2_scalefactor;
  286.      if (channels == both)
  287.         filter2->input_sample (sample2, subbandnumber);
  288.      else
  289.         filter1->input_sample (sample2, subbandnumber);
  290.   }
  291.   return TRUE;
  292. }
  293.