subband_layer_2.cc
Upload User: aoeyumen
Upload Date: 2007-01-06
Package Size: 3329k
Code Size: 19k
Category:

DVD

Development Platform:

Unix_Linux

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