home *** CD-ROM | disk | FTP | other *** search
/ PC Shareware 1997 June / PC_Shareware-1997-06.iso / manga / mp2win95 / _setup.1 / HEADER.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-31  |  9.5 KB  |  349 lines

  1. /*
  2.  *  @(#) header.cc 1.8, last edit: 6/15/94 16:51:44
  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.  *    - iostreams manipulator calls like "cerr << setw (2) << ..." replaced by
  24.  *      "cerr.width (2); ..." due to problems with older GNU C++ releases.
  25.  *    - syncword recognition slightly changed
  26.  */
  27.  
  28. /* A few layer III and seeking modifications made by Jeff Tsay.
  29.  
  30.    Last modified : 01/31/97 */
  31.  
  32. #define STRICT
  33. #define WIN32_LEAN_AND_MEAN
  34. #define NOMCX
  35. #define NOIME
  36. #define NOGDI
  37. #define NOUSER
  38. #define NOSOUND
  39. #define NOCOMM
  40. #define NODRIVERS
  41. #define OEMRESOURCE
  42. #define NONLS
  43. #define NOSERVICE
  44. #define NOKANJI
  45. #define NOMINMAX
  46. #define NOLOGERROR
  47. #define NOPROFILER
  48. #define NOMEMMGR
  49. #define NOLFILEIO
  50. #define NOOPENFILE
  51. #define NORESOURCE
  52. #define NOATOM
  53. #define NOLANGUAGE
  54. #define NOLSTRING
  55. #define NODBCS
  56. #define NOKEYBOARDINFO
  57. #define NOGDICAPMASKS
  58. #define NOCOLOR
  59. #define NOGDIOBJ
  60. #define NODRAWTEXT
  61. #define NOTEXTMETRIC
  62. #define NOSCALABLEFONT
  63. #define NOBITMAP
  64. #define NORASTEROPS
  65. #define NOMETAFILE
  66. #define NOSYSMETRICS
  67. #define NOSYSTEMPARAMSINFO
  68. #define NOMSG
  69. #define NOWINSTYLES
  70. #define NOWINOFFSETS
  71. #define NOSHOWWINDOW
  72. #define NODEFERWINDOWPOS
  73. #define NOVIRTUALKEYCODES
  74. #define NOKEYSTATES
  75. #define NOWH
  76. #define NOMENUS
  77. #define NOSCROLL
  78. #define NOCLIPBOARD
  79. #define NOICONS
  80. #define NOMB
  81. #define NOSYSCOMMANDS
  82. #define NOMDI
  83. #define NOCTLMGR
  84. #define NOWINMESSAGES
  85. #define NOHELP
  86. #define _WINUSER_
  87. #define __oleidl_h__
  88. #define _OLE2_H_
  89.  
  90. #include <windows.h>
  91.  
  92. #include "header.h"
  93.  
  94. const uint32 Header::frequencies[3] = { 44100, 48000, 32000 };
  95.  
  96. BOOL Header::read_header (Ibitstream *stream, Crc16 **crcp)
  97. {
  98.   uint32 headerstring;
  99.  
  100.   if (!stream->get_header (&headerstring))
  101.      return(FALSE);
  102.  
  103. /*  if ((h_layer = (headerstring >> 17) & 3) == 0)
  104.   {
  105.      cerr << "unknown layer identifier found!\n";
  106.      exit (1);
  107.   }
  108.   h_layer = 4 - h_layer;        // now 1 means Layer I and 3 means Layer III   */
  109.  
  110.   h_layer = 4 - (headerstring >> 17) & 3;
  111.  
  112.   h_protection_bit = (headerstring >> 16) & 1;
  113.   h_bitrate_index  = (headerstring >> 12) & 0xF;
  114.  
  115. /*  if ((h_bitrate_index = (headerstring >> 12) & 0xF) == 15)
  116.   {
  117.      cerr << "unknown bitrate index found!\n";
  118.      exit (1);
  119.   }           */
  120. /*  if (!h_bitrate_index)
  121.   {
  122.      cerr << "free format not yet implemented!\n";
  123.      exit (1);
  124.   } */
  125.  
  126.   h_sample_frequency = (e_sample_frequency) (headerstring >> 10) & 3;
  127.  
  128. /*  if ((h_sample_frequency = (e_sample_frequency)((headerstring >> 10) & 3)) == 3)
  129.   {
  130.      cerr << "unknown sample frequency!\n";
  131.      exit (1);
  132.   }          */
  133.  
  134.   h_padding_bit = (headerstring >> 9) & 1;
  135.   h_mode = (e_mode)((headerstring >> 6) & 3);
  136.  
  137. /*  if (h_layer == 2)
  138.      // testing validity of mode and bitrate:
  139.      if ((((h_bitrate_index >= 1 && h_bitrate_index <= 3) || h_bitrate_index == 5) &&
  140.      h_mode != single_channel) ||
  141.     (h_bitrate_index >= 11 && h_mode == single_channel))
  142.      {
  143.         cerr << "illegal combination of mode and bitrate in a layer II stream:\n"
  144.             "  mode: " << mode_string ()
  145.         << "\n  bitrate: " << bitrate_string () << '\n';
  146.         exit (1);
  147.      } */
  148.  
  149.  
  150.   h_mode_extension = (headerstring >> 4) & 3;
  151.  
  152.   if (h_mode == joint_stereo)
  153.      h_intensity_stereo_bound = (h_mode_extension << 2) + 4;
  154.   else
  155.      h_intensity_stereo_bound = 0;        // should never be used
  156.  
  157.   h_copyright = (BOOL) ((headerstring >> 3) & 1);
  158.   h_original = (BOOL) ((headerstring >> 2) & 1);
  159.  
  160.   // calculate number of subbands:
  161.   if (h_layer == 1)
  162.      h_number_of_subbands = 32;
  163.   else
  164.   {
  165.      uint32 channel_bitrate = h_bitrate_index;
  166.  
  167.      // calculate bitrate per channel:
  168.      if (h_mode != single_channel)
  169.         if (channel_bitrate == 4)
  170.     channel_bitrate = 1;
  171.         else
  172.     channel_bitrate -= 4;
  173.  
  174.      if (channel_bitrate == 1 || channel_bitrate == 2)
  175.         if (h_sample_frequency == thirtytwo)
  176.     h_number_of_subbands = 12;
  177.         else
  178.     h_number_of_subbands = 8;
  179.      else
  180.         if (h_sample_frequency == fourtyeight || (channel_bitrate >= 3 && channel_bitrate <= 5))
  181.     h_number_of_subbands = 27;
  182.         else
  183.     h_number_of_subbands = 30;
  184.   }
  185.  
  186.   if (h_intensity_stereo_bound > h_number_of_subbands)
  187.      h_intensity_stereo_bound = h_number_of_subbands;
  188.  
  189.   // read framedata:
  190.   if (!stream->read_frame (calculate_framesize()))
  191.      return(FALSE);
  192.  
  193.   if (!h_protection_bit)
  194.   {
  195.      // frame contains a crc checksum
  196.      checksum = (uint16)stream->get_bits (16);
  197.      if (!crc)
  198.         crc = new Crc16;
  199.      crc->add_bits (headerstring, 16);
  200.      *crcp = crc;
  201.   }
  202.   else
  203.      *crcp = (Crc16 *)0;
  204.  
  205.   if (h_sample_frequency == fourtyfour_point_one) {
  206.  
  207.      if (!stream->offset)
  208.         stream->offset = new int32 [max_number_of_frames(stream)];
  209.  
  210.      if (stream->current_frame > 0) {
  211.             if (stream->current_frame == stream->last_frame_number)
  212.               stream->offset[stream->current_frame] =
  213.                     stream->offset[stream->current_frame - 1] + h_padding_bit;
  214.         } else {
  215.           stream->offset[0] = h_padding_bit;
  216.         }
  217.   }
  218.  
  219.   return(TRUE);
  220. }
  221.  
  222.  
  223. uint32 Header::calculate_framesize ()
  224. /* calculates framesize in bytes excluding header size */
  225. {
  226.   static const int32 bitrates[3][15] = {
  227.     {0 /*free format*/, 32000, 64000, 96000, 128000, 160000, 192000,
  228.      224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000},
  229.     {0 /*free format*/, 32000, 48000, 56000, 64000, 80000, 96000,
  230.      112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000},
  231.     {0 /*free format*/, 32000, 40000, 48000, 56000, 64000, 80000,
  232.      96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000}
  233.   };
  234.  
  235.   static const samplefrequencies[3] = { 44100, 48000, 32000 };
  236.   uint32 framesize;
  237.  
  238.   if (h_layer == 1)
  239.   {
  240.      framesize = (12 * bitrates[0][h_bitrate_index]) / samplefrequencies[h_sample_frequency];
  241.      if (h_sample_frequency == fourtyfour_point_one && h_padding_bit)
  242.         ++framesize;
  243.      framesize <<= 2;        // one slot is 4 bytes long
  244.   }
  245.   else
  246.   {
  247.      framesize = (144 * bitrates[h_layer - 1][h_bitrate_index]) / samplefrequencies[h_sample_frequency];
  248.      if (h_padding_bit) framesize++;
  249.  
  250.      // Layer III slots
  251.      if (h_layer == 3)
  252.          nSlots = framesize - ((h_mode == single_channel) ? 17 : 32) // side info size
  253.                                   -  (h_protection_bit ? 0 : 2)                // CRC size
  254.                                   - 4;                                              // header size
  255.     else
  256.          nSlots = 0;
  257.   }
  258.  
  259.   return(framesize - 4);        // subtract header size
  260. }
  261.  
  262. const char *Header::layer_string (void)
  263. {
  264.   switch (h_layer)
  265.   {
  266.      case 1:
  267.         return "I                                      ";
  268.      case 2:
  269.         return "II                                     ";
  270.      case 3:
  271.         return "III                                    ";
  272.   }
  273.   return NULL;            // dummy
  274. }
  275.  
  276.  
  277. const char *Header::bitrate_string (void)
  278. {
  279.  
  280.   static const char *bitrate_str[3][16] = {
  281.     {"free format", "32 kbit/s", "64 kbit/s", "96 kbit/s", "128 kbit/s", "160 kbit/s",
  282.      "192 kbit/s", "224 kbit/s", "256 kbit/s", "288 kbit/s", "320 kbit/s", "352 kbit/s",
  283.      "384 kbit/s", "416 kbit/s", "448 kbit/s", "forbidden"},
  284.     {"free format", "32 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s",
  285.      "96 kbit/s", "112 kbit/s", "128 kbit/s", "160 kbit/s", "192 kbit/s", "224 kbit/s",
  286.      "256 kbit/s", "320 kbit/s", "384 kbit/s", "forbidden"},
  287.     {"free format", "32 kbit/s", "40 kbit/s", "48 kbit/s", "56 kbit/s", "64 kbit/s",
  288.      "80 kbit/s" , "96 kbit/s", "112 kbit/s", "128 kbit/s", "160 kbit/s",
  289.      "192 kbit/s", "224 kbit/s", "256 kbit/s", "320 kbit/s", "forbidden"}
  290.   };
  291.  
  292.   return(bitrate_str[h_layer - 1][h_bitrate_index]);
  293. }
  294.  
  295. const char *Header::sample_frequency_string (void)
  296. {
  297.   switch (h_sample_frequency)
  298.   {
  299.      case thirtytwo:
  300.         return("32 kHz        ");
  301.      case fourtyfour_point_one:
  302.         return("44.1 kHz     ");
  303.      case fourtyeight:
  304.         return("48 kHz        ");
  305.   }
  306.   return(NULL);            // dummy
  307. }
  308.  
  309. const char *Header::mode_string (void)
  310. {
  311.   switch (h_mode)
  312.   {
  313.      case stereo:
  314.         return("Stereo                             ");
  315.      case joint_stereo:
  316.         return("Joint stereo                      ");
  317.      case dual_channel:
  318.         return("Dual channel                    ");
  319.      case single_channel:
  320.         return("Single channel                ");
  321.   }
  322.   return(NULL);            // dummy
  323. }
  324.  
  325. // Stream searching routines
  326. uint32 Header::max_number_of_frames (Ibitstream *stream)
  327. // Returns the maximum number of frames in the stream
  328. {
  329.       return ( stream->file_size() /
  330.                   (calculate_framesize() + 4 - h_padding_bit) );
  331. }
  332.  
  333. uint32 Header::min_number_of_frames (Ibitstream *stream)
  334. // Returns the minimum number of frames in the stream
  335. {
  336.       return ( stream->file_size() /
  337.                   (calculate_framesize() + 5 - h_padding_bit) );
  338. }
  339.  
  340. static real ms_per_frame_array[3][3] = {{8.707483f,  8.0f, 12.0f},
  341.                                                    {26.12245f, 24.0f, 36.0f},
  342.                                         {26.12245f, 24.0f, 36.0f}};
  343.  
  344. real Header::ms_per_frame(void)
  345. {
  346.     return (ms_per_frame_array[h_layer-1][h_sample_frequency]);
  347. }
  348.  
  349.