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

  1. /*    header.cpp
  2.  
  3.     Implementation of MPEG header class
  4.  
  5.     A few layer III, MPEG-2 LSF, and seeking modifications made by
  6.    Jeff Tsay. MPEG-2 LSF is now supported.
  7.  
  8.    Last modified : 06/04/97 */
  9.  
  10.    /*
  11.  *  @(#) header.cc 1.8, last edit: 6/15/94 16:51:44
  12.  *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
  13.  *  @(#) Berlin University of Technology
  14.  *
  15.  *  This program is free software; you can redistribute it and/or modify
  16.  *  it under the terms of the GNU General Public License as published by
  17.  *  the Free Software Foundation; either version 2 of the License, or
  18.  *  (at your option) any later version.
  19.  *
  20.  *  This program is distributed in the hope that it will be useful,
  21.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  *  GNU General Public License for more details.
  24.  *
  25.  *  You should have received a copy of the GNU General Public License
  26.  *  along with this program; if not, write to the Free Software
  27.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28.  */
  29.  
  30. /*
  31.  *  Changes from version 1.1 to 1.2:
  32.  *    - iostreams manipulator calls like "cerr << setw (2) << ..." replaced by
  33.  *      "cerr.width (2); ..." due to problems with older GNU C++ releases.
  34.  *    - syncword recognition slightly changed
  35.  */
  36.  
  37. #ifndef GUI
  38. #include <iostream.h>
  39. #endif  // GUI
  40.  
  41. #include "header.h"
  42.  
  43. const uint32 Header::frequencies[2][4] =
  44. {{22050, 24000, 16000, 1},
  45.  {44100, 48000, 32000, 1}};
  46.  
  47. Header::Header()
  48. {
  49.       framesize = 0;
  50.    nSlots    = 0;
  51.       crc       = NULL;
  52.    offset    = NULL;
  53.    initial_sync = FALSE;
  54. }
  55.  
  56. Header::~Header()
  57. {
  58.     delete [] offset;
  59. }
  60.  
  61. bool Header::read_header(Ibitstream *stream, Crc16 **crcp)
  62. {
  63.   uint32 headerstring, channel_bitrate;
  64.  
  65.   if (!initial_sync) {
  66.  
  67.       if (!stream->get_header(&headerstring, INITIAL_SYNC))
  68.          return(FALSE);
  69.  
  70.       h_version = (e_version) ((headerstring >> 19) & 1);
  71.  
  72.    if ((h_sample_frequency = (e_sample_frequency)
  73.                              ((headerstring >> 10) & 3)) == 3)
  74.    {
  75. #ifdef WIN32GUI
  76.          MessageBox(NULL, "Unknown sample frequency in header",
  77.                         "Stream not supported", MB_ICONSTOP | MB_OK);
  78. #else
  79.          cerr << "Unknown sample frequency!" << endl;
  80. #endif
  81.         return(FALSE);
  82.    }
  83.  
  84.    stream->set_syncword(headerstring & 0xFFF80CC0);
  85.  
  86.    initial_sync = TRUE;
  87.  
  88.   } else {
  89.  
  90.           if (!stream->get_header(&headerstring, STRICT_SYNC))
  91.              return(FALSE);
  92.   } // initial_sync
  93.  
  94. /*  if ((h_layer = (headerstring >> 17) & 3) == 0)
  95.   {
  96.      cerr << "unknown layer identifier found!\n";
  97.      exit (1);
  98.   }
  99.   h_layer = 4 - h_layer;        // now 1 means Layer I and 3 means Layer III   */
  100.  
  101.   h_layer   = 4 - (headerstring >> 17) & 3;
  102.  
  103.   h_protection_bit = (headerstring >> 16) & 1;
  104.  
  105.  
  106. /*  if ((h_bitrate_index = (headerstring >> 12) & 0xF) == 15)
  107.   {
  108.      cerr << "unknown bitrate index found!\n";
  109.      exit (1);
  110.   }           */
  111. /*  if (!h_bitrate_index)
  112.   {
  113.      cerr << "free format not yet implemented!\n";
  114.      exit (1);
  115.   } */
  116.  
  117.   h_bitrate_index  = (headerstring >> 12) & 0xF;
  118.  
  119.   h_padding_bit = (headerstring >> 9) & 1;
  120.   h_mode        = (e_mode)((headerstring >> 6) & 3);
  121.  
  122. /*  if (h_layer == 2)
  123.      // testing validity of mode and bitrate:
  124.      if ((((h_bitrate_index >= 1 && h_bitrate_index <= 3) || h_bitrate_index == 5) &&
  125.      h_mode != single_channel) ||
  126.     (h_bitrate_index >= 11 && h_mode == single_channel))
  127.      {
  128.         cerr << "illegal combination of mode and bitrate in a layer II stream:\n"
  129.             "  mode: " << mode_string ()
  130.         << "\n  bitrate: " << bitrate_string () << '\n';
  131.         exit (1);
  132.      } */
  133.  
  134.   h_mode_extension = (headerstring >> 4) & 3;
  135.  
  136.   if (h_mode == joint_stereo)
  137.      h_intensity_stereo_bound = (h_mode_extension << 2) + 4;
  138.   else
  139.      h_intensity_stereo_bound = 0;        // should never be used
  140.  
  141.   h_copyright = (bool) ((headerstring >> 3) & 1);
  142.   h_original  = (bool) ((headerstring >> 2) & 1);
  143.  
  144.   // calculate number of subbands:
  145.   if (h_layer == 1)
  146.      h_number_of_subbands = 32;
  147.   else
  148.   {
  149.      channel_bitrate = h_bitrate_index;
  150.  
  151.      // calculate bitrate per channel:
  152.      if (h_mode != single_channel)
  153.         if (channel_bitrate == 4)
  154.             channel_bitrate = 1;
  155.         else
  156.             channel_bitrate -= 4;
  157.  
  158.      if ((channel_bitrate == 1) || (channel_bitrate == 2))
  159.         if (h_sample_frequency == thirtytwo)
  160.             h_number_of_subbands = 12;
  161.         else
  162.             h_number_of_subbands = 8;
  163.      else
  164.         if ((h_sample_frequency == fourtyeight) || ((channel_bitrate >= 3) &&
  165.                                                                   (channel_bitrate <= 5)))
  166.             h_number_of_subbands = 27;
  167.         else
  168.             h_number_of_subbands = 30;
  169.   }
  170.  
  171.   if (h_intensity_stereo_bound > h_number_of_subbands)
  172.      h_intensity_stereo_bound = h_number_of_subbands;
  173.  
  174.  
  175.   // calculate framesize and nSlots
  176.     calculate_framesize();
  177.  
  178.   // read framedata:
  179.   if (stream->read_frame(framesize) == FALSE)
  180.      return(FALSE);
  181.  
  182.   if (!h_protection_bit)
  183.   {
  184.      // frame contains a crc checksum
  185.      checksum = (uint16) stream->get_bits(16);
  186.      if (!crc)
  187.        crc = new Crc16;
  188.      crc->add_bits(headerstring, 16);
  189.      *crcp = crc;
  190.   }
  191.   else
  192.      *crcp = NULL;
  193.  
  194. #ifdef SEEK_STOP
  195.   if (h_sample_frequency == fourtyfour_point_one) {
  196.  
  197.      if (!offset) {
  198.  
  199.         uint32 max = max_number_of_frames(stream);
  200.  
  201.         offset = new uint32[max];
  202.  
  203.       for(uint32 i=0; i<max; i++)
  204.           offset[i] = 0;
  205.     }
  206.  
  207.     {
  208.      int32 cf = stream->current_frame();
  209.      int32 lf = stream->last_frame();
  210.       if ((cf > 0) && (cf == lf)) {
  211.            offset[cf] = offset[cf-1] + h_padding_bit;
  212.      } else {
  213.             offset[0] = h_padding_bit;
  214.      }
  215.     }
  216.   }
  217. #endif // SEEK_STOP
  218.  
  219.   return(TRUE);
  220. }
  221.  
  222. uint32 Header::calculate_framesize()
  223. // calculates framesize in bytes excluding header size
  224. {
  225.   static const int32 bitrates[2][3][16] = {
  226.   {{0 /*free format*/, 32000, 48000, 56000, 64000, 80000, 96000,
  227.     112000, 128000, 144000, 160000, 176000, 192000 ,224000, 256000, 0},
  228.    {0 /*free format*/, 8000, 16000, 24000, 32000, 40000, 48000,
  229.     56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 0},
  230.    {0 /*free format*/, 8000, 16000, 24000, 32000, 40000, 48000,
  231.     56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 0}},
  232.   {{0 /*free format*/, 32000, 64000, 96000, 128000, 160000, 192000,
  233.      224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000, 0},
  234.     {0 /*free format*/, 32000, 48000, 56000, 64000, 80000, 96000,
  235.      112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000, 0},
  236.     {0 /*free format*/, 32000, 40000, 48000, 56000, 64000, 80000,
  237.      96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, 0}}
  238.   };
  239.  
  240.   if (h_layer == 1) {
  241.      framesize = (12 * bitrates[h_version][0][h_bitrate_index]) /
  242.                  frequencies[h_version][h_sample_frequency];
  243.  
  244.      if (h_padding_bit) framesize++;
  245.  
  246.      framesize <<= 2;        // one slot is 4 bytes long
  247.  
  248.     nSlots = 0;
  249.  
  250.   } else {
  251.  
  252.      framesize = (144 * bitrates[h_version][h_layer - 1][h_bitrate_index]) /
  253.                  frequencies[h_version][h_sample_frequency];
  254.  
  255.      if (h_version == MPEG2_LSF)
  256.         framesize >>= 1;
  257.  
  258.      if (h_padding_bit) framesize++;
  259.  
  260.      // Layer III slots
  261.      if (h_layer == 3) {
  262.  
  263.        if (h_version == MPEG1) {
  264.  
  265.              nSlots = framesize - ((h_mode == single_channel) ? 17 : 32) // side info size
  266.                                       -  (h_protection_bit ? 0 : 2)                // CRC size
  267.                                       - 4;                                              // header size
  268.        } else {  // MPEG-2 LSF
  269.              nSlots = framesize - ((h_mode == single_channel) ?  9 : 17) // side info size
  270.                                      -  (h_protection_bit ? 0 : 2)                // CRC size
  271.                                       - 4;                                              // header size
  272.        }
  273.      } else {
  274.          nSlots = 0;
  275.     }
  276.   }
  277.  
  278.   framesize -= 4;             // subtract header size
  279.  
  280.   return(framesize);
  281. }
  282.  
  283. const char *Header::layer_string()
  284. {
  285.   switch (h_layer)
  286.   {
  287.      case 1:
  288.         return "I";
  289.      case 2:
  290.         return "II";
  291.      case 3:
  292.         return "III";
  293.   }
  294.   return NULL;            // dummy
  295. }
  296.  
  297.  
  298. const char *Header::bitrate_string()
  299. {
  300.  
  301.   static const char *bitrate_str[2][3][16] = {
  302.   {{"free format", "32 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s",
  303.     "80 kbit/s", "96 kbit/s", "112 kbit/s", "128 kbit/s", "144 kbit/s",
  304.     "160 kbit/s", "176 kbit/s", "192 kbit/s", "224 kbit/s", "256 kbit/s",
  305.     "forbidden"},
  306.    {"free format", "8 kbit/s", "16 kbit/s", "24 kbit/s", "32 kbit/s",
  307.     "40 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s",
  308.     "96 kbit/s", "112 kbit/s", "128 kbit/s", "144 kbit/s", "160 kbit/s",
  309.     "forbidden"},
  310.    {"free format", "8 kbit/s", "16 kbit/s", "24 kbit/s", "32 kbit/s",
  311.     "40 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s",
  312.     "96 kbit/s", "112 kbit/s", "128 kbit/s", "144 kbit/s", "160 kbit/s",
  313.     "forbidden"}},
  314.   {{"free format", "32 kbit/s", "64 kbit/s", "96 kbit/s", "128 kbit/s",
  315.     "160 kbit/s", "192 kbit/s", "224 kbit/s", "256 kbit/s", "288 kbit/s",
  316.     "320 kbit/s", "352 kbit/s", "384 kbit/s", "416 kbit/s", "448 kbit/s",
  317.     "forbidden"},
  318.     {"free format", "32 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s",
  319.     "80 kbit/s", "96 kbit/s", "112 kbit/s", "128 kbit/s", "160 kbit/s",
  320.     "192 kbit/s", "224 kbit/s", "256 kbit/s", "320 kbit/s", "384 kbit/s",
  321.     "forbidden"},
  322.     {"free format", "32 kbit/s", "40 kbit/s", "48 kbit/s", "56 kbit/s",
  323.     "64 kbit/s", "80 kbit/s" , "96 kbit/s", "112 kbit/s", "128 kbit/s",
  324.     "160 kbit/s", "192 kbit/s", "224 kbit/s", "256 kbit/s", "320 kbit/s",
  325.     "forbidden"}}
  326.   };
  327.  
  328.   return(bitrate_str[h_version][h_layer - 1][h_bitrate_index]);
  329. }
  330.  
  331. const char *Header::sample_frequency_string()
  332. {
  333.   switch (h_sample_frequency)
  334.   {
  335.      case thirtytwo:
  336.         if (h_version == MPEG1)
  337.             return("32 kHz");
  338.       else
  339.           return("16 kHz");
  340.      case fourtyfour_point_one:
  341.         if (h_version == MPEG1)
  342.             return("44.1 kHz");
  343.       else
  344.           return("22.05 kHz");
  345.      case fourtyeight:
  346.         if (h_version == MPEG1)
  347.             return("48 kHz");
  348.       else
  349.           return("24 kHz");
  350.   }
  351.   return(NULL);            // dummy
  352. }
  353.  
  354. const char *Header::mode_string()
  355. {
  356.   switch (h_mode)
  357.   {
  358.      case stereo:
  359.         return("Stereo");
  360.      case joint_stereo:
  361.         return("Joint stereo");
  362.      case dual_channel:
  363.         return("Dual channel");
  364.      case single_channel:
  365.         return("Single channel");
  366.   }
  367.   return(NULL);            // dummy
  368. }
  369.  
  370. const char *Header::version_string()
  371. {
  372.      switch (h_version)
  373.    {
  374.        case MPEG1:
  375.         return("MPEG-1");
  376.       case MPEG2_LSF:
  377.         return("MPEG-2 LSF");
  378.    }
  379.    return(NULL);
  380. }
  381.  
  382. #ifdef SEEK_STOP
  383. // Stream searching routines
  384. bool Header::stream_seek(Ibitstream *stream, uint32 seek_pos)
  385. {
  386.    return ((h_sample_frequency == fourtyfour_point_one) ?
  387.               stream->seek_pad(seek_pos, framesize - h_padding_bit,
  388.                             this, offset) :
  389.                stream->seek(seek_pos, framesize));
  390. }
  391. #endif
  392.  
  393. uint32 Header::max_number_of_frames(Ibitstream *stream)
  394. // Returns the maximum number of frames in the stream
  395. {
  396.       return(stream->file_size() / (framesize + 4 - h_padding_bit));
  397. }
  398.  
  399. uint32 Header::min_number_of_frames(Ibitstream *stream)
  400. // Returns the minimum number of frames in the stream
  401. {
  402.       return(stream->file_size() / (framesize + 5 - h_padding_bit));
  403. }
  404.  
  405. real Header::ms_per_frame()
  406. {
  407.     static real ms_per_frame_array[3][3] = {{8.707483f,  8.0f, 12.0f},
  408.                                                        {26.12245f, 24.0f, 36.0f},
  409.                                             {26.12245f, 24.0f, 36.0f}};
  410.  
  411.     return(ms_per_frame_array[h_layer-1][h_sample_frequency]);
  412. }
  413.                                            
  414. real Header::total_ms(Ibitstream *stream)
  415. {
  416.     return(max_number_of_frames(stream) * ms_per_frame());
  417. }
  418.  
  419.