home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / sound / players / maplay_t.z / maplay_t / subband_layer_2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  19.8 KB  |  634 lines

  1. /*
  2.  *  @(#) subband_layer_2.c 1.5, last edit: 2/21/94 18:10:10
  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. #include <stdlib.h>
  22. #include "subband_layer_2.h"
  23. #include "scalefactors.h"
  24.  
  25.  
  26. // data taken from ISO/IEC DIS 11172, Annexes 3-B.2[abcd] and 3-B.4:
  27.  
  28. // subbands 0-2 in tables 3-B.2a and 2b: (index is allocation)
  29. static const uint32 table_ab1_codelength[16] =
  30.   // bits per codeword
  31. { 0, 5, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  32.  
  33. static const uint32 table_ab1_quantizationsteps[16] =
  34.   // number of steps if grouped, 0 if ungrouped
  35. { 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  36.  
  37. static const real table_ab1_factor[16] =
  38.   // factor for requantization: (real)sample * factor - 1.0 gives requantized sample
  39. { 0.0, 1.0/2.0, 1.0/4.0, 1.0/8.0, 1.0/16.0, 1.0/32.0, 1.0/64.0,
  40.   1.0/128.0, 1.0/256.0, 1.0/512.0, 1.0/1024.0, 1.0/2048.0,
  41.   1.0/4096.0, 1.0/8192.0, 1.0/16384.0, 1.0/32768.0 };
  42.  
  43. static const real table_ab1_c[16] =
  44.   // factor c for requantization from table 3-B.4
  45. { 0.0,           1.33333333333, 1.14285714286, 1.06666666666, 1.03225806452,
  46.   1.01587301587, 1.00787401575, 1.00392156863, 1.00195694716, 1.00097751711,
  47.   1.00048851979, 1.00024420024, 1.00012208522, 1.00006103888, 1.00003051851,
  48.   1.00001525902 };
  49. static const real table_ab1_d[16] =
  50.   // addend d for requantization from table 3-B.4
  51. { 0.0,           0.50000000000, 0.25000000000, 0.12500000000, 0.06250000000,
  52.   0.03125000000, 0.01562500000, 0.00781250000, 0.00390625000, 0.00195312500,
  53.   0.00097656250, 0.00048828125, 0.00024414063, 0.00012207031, 0.00006103516,
  54.   0.00003051758 };
  55.  
  56. // subbands 3-... tables 3-B.2a and 2b:
  57. static const uint32 table_ab234_quantizationsteps[16] =
  58. { 0, 3, 5, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  59.  
  60. // subbands 3-10 in tables 3-B.2a and 2b:
  61. static const uint32 table_ab2_codelength[16] =
  62. { 0, 5, 7, 3, 10, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 16 };
  63. static const real table_ab2_factor[16] =
  64. { 0.0, 1.0/2.0, 1.0/4.0, 1.0/4.0, 1.0/8.0, 1.0/8.0, 1.0/16.0,
  65.   1.0/32.0, 1.0/64.0, 1.0/128.0, 1.0/256.0, 1.0/512.0,
  66.   1.0/1024.0, 1.0/2048.0, 1.0/4096.0, 1.0/32768.0 };
  67. static const real table_ab2_c[16] =
  68. { 0.0,           1.33333333333, 1.60000000000, 1.14285714286, 1.77777777777,
  69.   1.06666666666, 1.03225806452, 1.01587301587, 1.00787401575, 1.00392156863,
  70.   1.00195694716, 1.00097751711, 1.00048851979, 1.00024420024, 1.00012208522,
  71.   1.00001525902 };
  72. static const real table_ab2_d[16] =
  73. { 0.0,           0.50000000000, 0.50000000000, 0.25000000000, 0.50000000000,
  74.   0.12500000000, 0.06250000000, 0.03125000000, 0.01562500000, 0.00781250000,
  75.   0.00390625000, 0.00195312500, 0.00097656250, 0.00048828125, 0.00024414063,
  76.   0.00003051758 };
  77.  
  78. // subbands 11-22 in tables 3-B.2a and 2b:
  79. static const uint32 table_ab3_codelength[8] = { 0, 5, 7, 3, 10, 4, 5, 16 };
  80. static const real table_ab3_factor[8] =
  81. { 0.0, 1.0/2.0, 1.0/4.0, 1.0/4.0, 1.0/8.0, 1.0/8.0, 1.0/16.0, 1.0/32768.0 };
  82. static const real table_ab3_c[8] =
  83. { 0.0,           1.33333333333, 1.60000000000, 1.14285714286, 1.77777777777,
  84.   1.06666666666, 1.03225806452, 1.00001525902 };
  85. static const real table_ab3_d[8] =
  86. { 0.0,           0.50000000000, 0.50000000000, 0.25000000000, 0.50000000000,
  87.   0.12500000000, 0.06250000000, 0.00003051758 };
  88.  
  89. // subbands 23-... in tables 3-B.2a and 2b:
  90. static const uint32 table_ab4_codelength[4] = { 0, 5, 7, 16 };
  91. static const real table_ab4_factor[8] = { 0.0, 1.0/2.0, 1.0/4.0, 1.0/32768.0 };
  92. static const real table_ab4_c[4] = { 0.0, 1.33333333333, 1.60000000000, 1.00001525902 };
  93. static const real table_ab4_d[4] = { 0.0, 0.50000000000, 0.50000000000, 0.00003051758 };
  94.  
  95. // subbands in tables 3-B.2c and 2d:
  96. static const uint32 table_cd_codelength[16] =
  97. { 0, 5, 7, 10, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  98. static const uint32 table_cd_quantizationsteps[16] =
  99. { 0, 3, 5, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  100. static const real table_cd_factor[16] =
  101. { 0.0, 1.0/2.0, 1.0/4.0, 1.0/8.0, 1.0/8.0, 1.0/16.0, 1.0/32.0, 1.0/64.0,
  102.   1.0/128.0, 1.0/256.0, 1.0/512.0, 1.0/1024.0, 1.0/2048.0, 1.0/4096.0,
  103.   1.0/8192.0, 1.0/16384.0 };
  104. static const real table_cd_c[16] =
  105. { 0.0,           1.33333333333, 1.60000000000, 1.77777777777, 1.06666666666,
  106.   1.03225806452, 1.01587301587, 1.00787401575, 1.00392156863, 1.00195694716,
  107.   1.00097751711, 1.00048851979, 1.00024420024, 1.00012208522, 1.00006103888,
  108.   1.00003051851 };
  109. static const real table_cd_d[16] =
  110. { 0.0,           0.50000000000, 0.50000000000, 0.50000000000, 0.12500000000,
  111.   0.06250000000, 0.03125000000, 0.01562500000, 0.00781250000, 0.00390625000,
  112.   0.00195312500, 0.00097656250, 0.00048828125, 0.00024414063, 0.00012207031,
  113.   0.00006103516 };
  114.  
  115.  
  116.  
  117. /**********************/    // used for single channel mode
  118. /*** Standard Class ***/    // and in derived class for intensity
  119. /**********************/    // stereo mode
  120.  
  121. SubbandLayer2::SubbandLayer2 (uint32 subbandnumber)
  122. {
  123.   this->subbandnumber = subbandnumber;
  124.   groupnumber = samplenumber = 0;
  125. }
  126.  
  127.  
  128. uint32 SubbandLayer2::get_allocationlength (Header *header)
  129. {
  130.   uint32 channel_bitrate = header->bitrate_index ();
  131.  
  132.   // calculate bitrate per channel:
  133.   if (header->mode () != single_channel)
  134.     if (channel_bitrate == 4)
  135.       channel_bitrate = 1;
  136.     else
  137.       channel_bitrate -= 4;
  138.  
  139.   if (channel_bitrate == 1 || channel_bitrate == 2)
  140.     // table 3-B.2c or 3-B.2d
  141.     if (subbandnumber <= 1)
  142.       return 4;
  143.     else
  144.       return 3;
  145.   else
  146.     // tables 3-B.2a or 3-B.2b
  147.     if (subbandnumber <= 10)
  148.       return 4;
  149.     else if (subbandnumber <= 22)
  150.       return 3;
  151.     else
  152.       return 2;
  153. }
  154.  
  155.  
  156. uint32 SubbandLayer2::prepare_sample_reading (Header *header, uint32 allocation, bool *grouping,
  157.      uint32 *quantizationsteps, real *factor, uint32 *codelength, real *c, real *d)
  158. {
  159.   uint32 channel_bitrate = header->bitrate_index ();
  160.  
  161.   // calculate bitrate per channel:
  162.   if (header->mode () != single_channel)
  163.     if (channel_bitrate == 4)
  164.       channel_bitrate = 1;
  165.     else
  166.       channel_bitrate -= 4;
  167.  
  168.   *grouping = False;
  169.   if (channel_bitrate == 1 || channel_bitrate == 2)
  170.   {
  171.     // table 3-B.2c or 3-B.2d
  172.     if (*quantizationsteps = table_cd_quantizationsteps[allocation])
  173.       *grouping = True;
  174.     *factor = table_cd_factor[allocation];
  175.     *codelength = table_cd_codelength[allocation];
  176.     *c = table_cd_c[allocation];
  177.     *d = table_cd_d[allocation];
  178.   }
  179.   else
  180.   {
  181.     // tables 3-B.2a or 3-B.2b
  182.     if (subbandnumber <= 2)
  183.     {
  184.       if (*quantizationsteps = table_ab1_quantizationsteps[allocation])
  185.     *grouping = True;
  186.       *factor = table_ab1_factor[allocation];
  187.       *codelength = table_ab1_codelength[allocation];
  188.       *c = table_ab1_c[allocation];
  189.       *d = table_ab1_d[allocation];
  190.     }
  191.     else
  192.     {
  193.       if (*quantizationsteps = table_ab234_quantizationsteps[allocation])
  194.     *grouping = True;
  195.       if (subbandnumber <= 10)
  196.       {
  197.     *factor = table_ab2_factor[allocation];
  198.     *codelength = table_ab2_codelength[allocation];
  199.     *c = table_ab2_c[allocation];
  200.     *d = table_ab2_d[allocation];
  201.       }
  202.       else if (subbandnumber <= 22)
  203.       {
  204.     *factor = table_ab3_factor[allocation];
  205.     *codelength = table_ab3_codelength[allocation];
  206.     *c = table_ab3_c[allocation];
  207.     *d = table_ab3_d[allocation];
  208.       }
  209.       else
  210.       {
  211.     *factor = table_ab4_factor[allocation];
  212.     *codelength = table_ab4_codelength[allocation];
  213.     *c = table_ab4_c[allocation];
  214.     *d = table_ab4_d[allocation];
  215.       }
  216.     }
  217.   }
  218. }
  219.  
  220.  
  221. void SubbandLayer2::read_allocation (Ibitstream *stream, Header *header, Crc16 *crc)
  222. {
  223.   register uint32 length = get_allocationlength (header);
  224.   allocation = stream->get_bits (length);
  225.   if (crc)
  226.     crc->add_bits (allocation, length);
  227. }
  228.  
  229.  
  230. void SubbandLayer2::read_scalefactor_selection (Ibitstream *stream, Crc16 *crc)
  231. {
  232.   if (allocation)
  233.   {
  234.     scfsi = stream->get_bits (2);
  235.     if (crc)
  236.       crc->add_bits (scfsi, 2);
  237.   }
  238. }
  239.  
  240.  
  241. void SubbandLayer2::read_scalefactor (Ibitstream *stream, Header *header)
  242. {
  243.   if (allocation)
  244.   {
  245.     switch (scfsi)
  246.     {
  247.       case 0:
  248.     scalefactor1 = stream->get_bits (6);
  249.     scalefactor2 = stream->get_bits (6);
  250.     scalefactor3 = stream->get_bits (6);
  251.     break;
  252.       case 1:
  253.     scalefactor1 = scalefactor2 = stream->get_bits (6);
  254.     scalefactor3 = stream->get_bits (6);
  255.     break;
  256.       case 2:
  257.     scalefactor1 = scalefactor2 = scalefactor3 = stream->get_bits (6);
  258.     break;
  259.       case 3:
  260.     scalefactor1 = stream->get_bits (6);
  261.     scalefactor2 = scalefactor3 = stream->get_bits (6);
  262.     break;
  263.     }
  264.     if (scalefactor1 == 63 || scalefactor2 == 63 || scalefactor3 == 63)
  265.       cerr << "WARNING: stream contains an illegal scalefactor!\n";    // MPEG-stream is corrupted!
  266.  
  267.     prepare_sample_reading (header, allocation, &grouping, &quantizationsteps,
  268.                 &factor, &codelength, &c, &d);
  269.   }
  270. }
  271.  
  272.  
  273. bool SubbandLayer2::read_sampledata (Ibitstream *stream)
  274. {
  275.   if (allocation)
  276.     if (grouping)
  277.     {
  278.       uint32 samplecode = stream->get_bits (codelength);
  279. #ifdef DEBUG
  280.       if (samplecode == (1 << codelength) - 1)
  281.     cerr << "WARNING: stream contains an illegal subband sample!\n";  // MPEG-stream is corrupted!
  282. #endif
  283.       samples[0] = real (samplecode % quantizationsteps) * factor - 1.0;
  284.       samplecode /= quantizationsteps;
  285.       samples[1] = real (samplecode % quantizationsteps) * factor - 1.0;
  286.       samplecode /= quantizationsteps;
  287.       samples[2] = real (samplecode % quantizationsteps) * factor - 1.0;
  288.     }
  289.     else
  290.     {
  291.       samples[0] = real (stream->get_bits (codelength)) * factor - 1.0;
  292. #ifdef DEBUG
  293.       if (samples[0] == (1 << codelength) - 1)
  294.     cerr << "WARNING: stream contains an illegal subband sample!\n";  // MPEG-stream is corrupted!
  295. #endif
  296.       samples[1] = real (stream->get_bits (codelength)) * factor - 1.0;
  297. #ifdef DEBUG
  298.       if (samples[1] == (1 << codelength) - 1)
  299.     cerr << "WARNING: stream contains an illegal subband sample!\n";  // MPEG-stream is corrupted!
  300. #endif
  301.       samples[2] = real (stream->get_bits (codelength)) * factor - 1.0;
  302. #ifdef DEBUG
  303.       if (samples[2] == (1 << codelength) - 1)
  304.     cerr << "WARNING: stream contains an illegal subband sample!\n";  // MPEG-stream is corrupted!
  305. #endif
  306.     }
  307.  
  308.   samplenumber = 0;
  309.   if (++groupnumber == 12)
  310.     return True;
  311.   else
  312.     return False;
  313. }
  314.  
  315.  
  316. bool SubbandLayer2::put_next_sample (e_channels channels,
  317.                      SynthesisFilter *filter1, SynthesisFilter *filter2)
  318. {
  319. #ifdef DEBUG
  320.   if (samplenumber >= 3)
  321.   {
  322.     // don't call put_next_sample if the previvious call returned True;
  323.     cerr << "Illegal call to SubbandLayer2::put_next_sample()!\n";
  324.     exit (1);
  325.   }
  326. #endif
  327.   if (allocation && channels != right)
  328.   {
  329.     register real sample = (samples[samplenumber] + d) * c;
  330.     if (groupnumber <= 4)
  331.       sample *= scalefactors[scalefactor1];
  332.     else if (groupnumber <= 8)
  333.       sample *= scalefactors[scalefactor2];
  334.     else
  335.       sample *= scalefactors[scalefactor3];
  336. #ifdef DEBUG
  337.     if (sample < -1.0 || sample > 1.0)
  338.       cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  339.       // this should never occur
  340. #endif
  341.     if (sample < -1.0E-7 || sample > 1.0E-7)
  342.       filter1->input_sample (sample, subbandnumber);
  343.   }
  344.  
  345.   if (++samplenumber == 3)
  346.     return True;
  347.   else
  348.     return False;
  349. }
  350.  
  351.  
  352.  
  353. /******************************/
  354. /*** Intensity Stereo Class ***/
  355. /******************************/
  356.  
  357. SubbandLayer2IntensityStereo::SubbandLayer2IntensityStereo (uint32 subbandnumber)
  358. : SubbandLayer2 (subbandnumber)
  359. {
  360. }
  361.  
  362.  
  363. void SubbandLayer2IntensityStereo::read_scalefactor_selection (Ibitstream *stream, Crc16 *crc)
  364. {
  365.   if (allocation)
  366.   {
  367.     scfsi = stream->get_bits (2);
  368.     channel2_scfsi = stream->get_bits (2);
  369.     if (crc)
  370.     {
  371.       crc->add_bits (scfsi, 2);
  372.       crc->add_bits (channel2_scfsi, 2);
  373.     }
  374.   }
  375. }
  376.  
  377.  
  378. void SubbandLayer2IntensityStereo::read_scalefactor (Ibitstream *stream, Header *header)
  379. {
  380.   if (allocation)
  381.   {
  382.     SubbandLayer2::read_scalefactor (stream, header);
  383.     switch (channel2_scfsi)
  384.     {
  385.       case 0:
  386.     channel2_scalefactor1 = stream->get_bits (6);
  387.     channel2_scalefactor2 = stream->get_bits (6);
  388.     channel2_scalefactor3 = stream->get_bits (6);
  389.     break;
  390.       case 1:
  391.     channel2_scalefactor1 = channel2_scalefactor2 = stream->get_bits (6);
  392.     channel2_scalefactor3 = stream->get_bits (6);
  393.     break;
  394.       case 2:
  395.     channel2_scalefactor1 = channel2_scalefactor2 =
  396.     channel2_scalefactor3 = stream->get_bits (6);
  397.     break;
  398.       case 3:
  399.     channel2_scalefactor1 = stream->get_bits (6);
  400.     channel2_scalefactor2 = channel2_scalefactor3 = stream->get_bits (6);
  401.     break;
  402.     }
  403.     if (channel2_scalefactor1 == 63 || channel2_scalefactor2 == 63 || channel2_scalefactor3 == 63)
  404.       cerr << "WARNING: stream contains an illegal scalefactor!\n";    // MPEG-stream is corrupted!
  405.   }
  406. }
  407.  
  408.  
  409. bool SubbandLayer2IntensityStereo::put_next_sample (e_channels channels,
  410.     SynthesisFilter *filter1, SynthesisFilter *filter2)
  411. {
  412. #ifdef DEBUG
  413.   if (samplenumber >= 3)
  414.   {
  415.     // don't call put_next_sample if the previvious call returned True;
  416.     cerr << "Illegal call to SubbandLayer2::put_next_sample()!\n";
  417.     exit (1);
  418.   }
  419. #endif
  420.   if (allocation)
  421.   {
  422.     register real sample = (samples[samplenumber] + d) * c;
  423.     if (channels == both)
  424.     {
  425.       register float sample2 = sample;
  426.       if (groupnumber <= 4)
  427.       {
  428.     sample *= scalefactors[scalefactor1];
  429.     sample2 *= scalefactors[channel2_scalefactor1];
  430.       }
  431.       else if (groupnumber <= 8)
  432.       {
  433.     sample *= scalefactors[scalefactor2];
  434.     sample2 *= scalefactors[channel2_scalefactor2];
  435.       }
  436.       else
  437.       {
  438.     sample *= scalefactors[scalefactor3];
  439.     sample2 *= scalefactors[channel2_scalefactor3];
  440.       }
  441. #ifdef DEBUG
  442.       if (sample < -1.0 || sample > 1.0 || sample2 < -1.0 || sample2 > 1.0)
  443.     cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  444.     // this should never occur
  445. #endif
  446.       if (sample < -1.0E-7 || sample > 1.0E-7)
  447.     filter1->input_sample (sample, subbandnumber);
  448.       if (sample2 < -1.0E-7 || sample2 > 1.0E-7)
  449.     filter2->input_sample (sample2, subbandnumber);
  450.     }
  451.     else if (channels == left)
  452.     {
  453.       if (groupnumber <= 4)
  454.     sample *= scalefactors[scalefactor1];
  455.       else if (groupnumber <= 8)
  456.     sample *= scalefactors[scalefactor2];
  457.       else
  458.     sample *= scalefactors[scalefactor3];
  459. #ifdef DEBUG
  460.       if (sample < -1.0 || sample > 1.0)
  461.     cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  462.     // this should never occur
  463. #endif
  464.       if (sample < -1.0E-7 || sample > 1.0E-7)
  465.     filter1->input_sample (sample, subbandnumber);
  466.     }
  467.     else
  468.     {
  469.       if (groupnumber <= 4)
  470.     sample *= scalefactors[channel2_scalefactor1];
  471.       else if (groupnumber <= 8)
  472.     sample *= scalefactors[channel2_scalefactor2];
  473.       else
  474.     sample *= scalefactors[channel2_scalefactor3];
  475. #ifdef DEBUG
  476.       if (sample < -1.0 || sample > 1.0)
  477.     cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  478.     // this should never occur
  479. #endif
  480.       if (sample < -1.0E-7 || sample > 1.0E-7)
  481.     filter1->input_sample (sample, subbandnumber);
  482.     }
  483.   }
  484.  
  485.   if (++samplenumber == 3)
  486.     return True;
  487.   else
  488.     return False;
  489. }
  490.  
  491.  
  492.  
  493. /********************/
  494. /*** Stereo Class ***/
  495. /********************/
  496.  
  497. SubbandLayer2Stereo::SubbandLayer2Stereo (uint32 subbandnumber)
  498. : SubbandLayer2 (subbandnumber)
  499. {
  500. }
  501.  
  502.  
  503. void SubbandLayer2Stereo::read_allocation (Ibitstream *stream, Header *header, Crc16 *crc)
  504. {
  505.   uint32 length = get_allocationlength (header);
  506.   allocation = stream->get_bits (length);
  507.   channel2_allocation = stream->get_bits (length);
  508.   if (crc)
  509.   {
  510.     crc->add_bits (allocation, length);
  511.     crc->add_bits (channel2_allocation, length);
  512.   }
  513. }
  514.  
  515.  
  516. void SubbandLayer2Stereo::read_scalefactor_selection (Ibitstream *stream, Crc16 *crc)
  517. {
  518.   if (allocation)
  519.   {
  520.     scfsi = stream->get_bits (2);
  521.     if (crc)
  522.       crc->add_bits (scfsi, 2);
  523.   }
  524.   if (channel2_allocation)
  525.   {
  526.     channel2_scfsi = stream->get_bits (2);
  527.     if (crc)
  528.       crc->add_bits (channel2_scfsi, 2);
  529.   }
  530. }
  531.  
  532.  
  533. void SubbandLayer2Stereo::read_scalefactor (Ibitstream *stream, Header *header)
  534. {
  535.   SubbandLayer2::read_scalefactor (stream, header);
  536.   if (channel2_allocation)
  537.   {
  538.     switch (channel2_scfsi)
  539.     {
  540.       case 0:
  541.     channel2_scalefactor1 = stream->get_bits (6);
  542.     channel2_scalefactor2 = stream->get_bits (6);
  543.     channel2_scalefactor3 = stream->get_bits (6);
  544.     break;
  545.       case 1:
  546.     channel2_scalefactor1 = channel2_scalefactor2 = stream->get_bits (6);
  547.     channel2_scalefactor3 = stream->get_bits (6);
  548.     break;
  549.       case 2:
  550.     channel2_scalefactor1 = channel2_scalefactor2 =
  551.     channel2_scalefactor3 = stream->get_bits (6);
  552.     break;
  553.       case 3:
  554.     channel2_scalefactor1 = stream->get_bits (6);
  555.     channel2_scalefactor2 = channel2_scalefactor3 = stream->get_bits (6);
  556.     break;
  557.     }
  558.     if (channel2_scalefactor1 == 63 || channel2_scalefactor2 == 63 || channel2_scalefactor3 == 63)
  559.       cerr << "WARNING: stream contains an illegal scalefactor!\n";    // MPEG-stream is corrupted!
  560.  
  561.     prepare_sample_reading (header, channel2_allocation, &channel2_grouping,
  562.                 &channel2_quantizationsteps, &channel2_factor,
  563.                 &channel2_codelength, &channel2_c, &channel2_d);
  564.   }
  565. }
  566.  
  567.  
  568. bool SubbandLayer2Stereo::read_sampledata (Ibitstream *stream)
  569. {
  570.   bool returnvalue = SubbandLayer2::read_sampledata (stream);
  571.  
  572.   if (channel2_allocation)
  573.     if (channel2_grouping)
  574.     {
  575.       uint32 samplecode = stream->get_bits (channel2_codelength);
  576. #ifdef DEBUG
  577.       if (samplecode == (1 << channel2_codelength) - 1)
  578.     cerr << "WARNING: stream contains an illegal subband sample!\n";  // MPEG-stream is corrupted!
  579. #endif
  580.       channel2_samples[0] = real (samplecode % channel2_quantizationsteps) * channel2_factor - 1.0;
  581.       samplecode /= channel2_quantizationsteps;
  582.       channel2_samples[1] = real (samplecode % channel2_quantizationsteps) * channel2_factor - 1.0;
  583.       samplecode /= channel2_quantizationsteps;
  584.       channel2_samples[2] = real (samplecode % channel2_quantizationsteps) * channel2_factor - 1.0;
  585.     }
  586.     else
  587.     {
  588.       channel2_samples[0] = real (stream->get_bits (channel2_codelength)) * channel2_factor - 1.0;
  589. #ifdef DEBUG
  590.       if (channel2_samples[0] == (1 << channel2_codelength) - 1)
  591.     cerr << "WARNING: stream contains an illegal subband sample!\n";  // MPEG-stream is corrupted!
  592. #endif
  593.       channel2_samples[1] = real (stream->get_bits (channel2_codelength)) * channel2_factor - 1.0;
  594. #ifdef DEBUG
  595.       if (channel2_samples[1] == (1 << channel2_codelength) - 1)
  596.     cerr << "WARNING: stream contains an illegal subband sample!\n";  // MPEG-stream is corrupted!
  597. #endif
  598.       channel2_samples[2] = real (stream->get_bits (channel2_codelength)) * channel2_factor - 1.0;
  599. #ifdef DEBUG
  600.       if (channel2_samples[2] == (1 << channel2_codelength) - 1)
  601.     cerr << "WARNING: stream contains an illegal subband sample!\n";  // MPEG-stream is corrupted!
  602. #endif
  603.     }
  604.   return returnvalue;
  605. }
  606.  
  607.  
  608. bool SubbandLayer2Stereo::put_next_sample (e_channels channels,
  609.                        SynthesisFilter *filter1, SynthesisFilter *filter2)
  610. {
  611.   bool returnvalue = SubbandLayer2::put_next_sample (channels, filter1, filter2);
  612.   if (channel2_allocation && channels != left)
  613.   {
  614.     register real sample = (channel2_samples[samplenumber - 1] + channel2_d) * channel2_c;
  615.     if (groupnumber <= 4)
  616.       sample *= scalefactors[channel2_scalefactor1];
  617.     else if (groupnumber <= 8)
  618.       sample *= scalefactors[channel2_scalefactor2];
  619.     else
  620.       sample *= scalefactors[channel2_scalefactor3];
  621. #ifdef DEBUG
  622.     if (sample < -1.0 || sample > 1.0)
  623.       cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  624.       // this should never occur
  625. #endif
  626.     if (sample < -1.0E-7 || sample > 1.0E-7)
  627.       if (channels == both)
  628.     filter2->input_sample (sample, subbandnumber);
  629.       else
  630.     filter1->input_sample (sample, subbandnumber);
  631.   }
  632.   return returnvalue;
  633. }
  634.