header.cpp
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:13k
源码类别:

多媒体编程

开发平台:

Visual C++

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