File: header.cc

package info (click to toggle)
maplay 1.2b-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 432 kB
  • ctags: 441
  • sloc: cpp: 5,421; sh: 97; makefile: 86
file content (251 lines) | stat: -rw-r--r-- 6,960 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 *  @(#) header.cc 1.10, last edit: 27 Oct 1994 15:58:26
 *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
 *  @(#) Berlin University of Technology
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 *  Changes from version 1.1 to 1.2:
 *    - iostreams manipulator calls like "cerr << setw (2) << ..." replaced by
 *      "cerr.width (2); ..." due to problems with older GNU C++ releases.
 *    - syncword recognition slightly changed
 */

#include <sys/types.h>
#include <unistd.h>
#include <iostream.h>
#ifndef NeXT
#include <iomanip.h>
#endif
#include <stdlib.h>
#include "header.h"


const uint32 Header::frequencies[3] = { 44100, 48000, 32000 };


boil Header::read_header (Ibitstream *stream, Crc16 **crcp)
{
  uint32 headerstring;

  if (!stream->get_header (&headerstring))
    return False;

  if ((headerstring & 0xFFF80000) != 0xFFF80000)
  {
    cerr << "invalid syncword 0x";
    cerr.width (8);
    cerr.fill ('0');
    cerr << hex << headerstring
	 << " found at fileoffset " << dec
	 << lseek (stream->filedescriptor (), 0, SEEK_CUR) - 4 << '\n';
    return False;
  }

  if ((h_layer = (headerstring >> 17) & 3) == 0)
  {
    cerr << "unknown layer identifier found!\n";
    exit (1);
  }
  h_layer = 4 - h_layer;		// now 1 means Layer I and 3 means Layer III
  if (h_layer == 3)
  {
    cerr << "Sorry, Layer III not implemented!\n";
    exit (1);
  }
  h_protection_bit = (headerstring >> 16) & 1;
  if ((h_bitrate_index = (headerstring >> 12) & 0xF) == 15)
  {
    cerr << "unknown bitrate index found!\n";
    exit (1);
  }
  if (!h_bitrate_index)
  {
    cerr << "free format not yet implemented!\n";
    exit (1);
  }

  if ((h_sample_frequency = (e_sample_frequency)((headerstring >> 10) & 3)) == 3)
  {
    cerr << "unknown sample frequency!\n";
    exit (1);
  }
  h_padding_bit = (headerstring >> 9) & 1;
  h_mode = (e_mode)((headerstring >> 6) & 3);
  if (h_layer == 2)
    // testing validity of mode and bitrate:
    if ((((h_bitrate_index >= 1 && h_bitrate_index <= 3) || h_bitrate_index == 5) &&
	 h_mode != single_channel) ||
	(h_bitrate_index >= 11 && h_mode == single_channel))
    {
      cerr << "illegal combination of mode and bitrate in a layer II stream:\n"
	      "  mode: " << mode_string ()
	   << "\n  bitrate: " << bitrate_string () << '\n';
      exit (1);
    }
  h_mode_extension = (headerstring >> 4) & 3;
  if (h_mode == joint_stereo)
    h_intensity_stereo_bound = (h_mode_extension << 2) + 4;
  else
    h_intensity_stereo_bound = 0;		// should never be used
  h_copyright = (headerstring >> 3) & 1;
  h_original = (headerstring >> 2) & 1;

  // calculate number of subbands:
  if (h_layer == 1)
    h_number_of_subbands = 32;
  else
  {
    uint32 channel_bitrate = h_bitrate_index;

    // calculate bitrate per channel:
    if (h_mode != single_channel)
      if (channel_bitrate == 4)
	channel_bitrate = 1;
      else
	channel_bitrate -= 4;

    if (channel_bitrate == 1 || channel_bitrate == 2)
      if (h_sample_frequency == thirtytwo)
	h_number_of_subbands = 12;
      else
	h_number_of_subbands = 8;
    else
      if (h_sample_frequency == fourtyeight || (channel_bitrate >= 3 && channel_bitrate <= 5))
	h_number_of_subbands = 27;
      else
	h_number_of_subbands = 30;
  }

  if (h_intensity_stereo_bound > h_number_of_subbands)
    h_intensity_stereo_bound = h_number_of_subbands;

  // read framedata:
  if (!stream->read_frame (calculate_framesize ()))
    return False;

  if (!h_protection_bit)
  {
    // frame contains a crc checksum
    checksum = (uint16)stream->get_bits (16);
    if (!crc)
      crc = new Crc16;
    crc->add_bits (headerstring, 16);
    *crcp = crc;
  }
  else
    *crcp = (Crc16 *)0;

  return True;
}


uint32 Header::calculate_framesize ()
/* calculates framesize in bytes excluding header size */
{
  static const int32 bitrates_layer_1[15] = {
    0 /*free format*/, 32000, 64000, 96000, 128000, 160000, 192000,
    224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000 };
  static const int32 bitrates_layer_2[15] = {
    0 /*free format*/, 32000, 48000, 56000, 64000, 80000, 96000,
    112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000 };
  static const samplefrequencies[3] = { 44100, 48000, 32000 };
  uint32 framesize;

  if (h_layer == 1)
  {
    framesize = (12 * bitrates_layer_1[h_bitrate_index]) / samplefrequencies[h_sample_frequency];
    if (h_sample_frequency == fourtyfour_point_one && h_padding_bit)
      ++framesize;
    framesize <<= 2;		// one slot is 4 bytes long
  }
  else
  {
    framesize = (144 * bitrates_layer_2[h_bitrate_index]) / samplefrequencies[h_sample_frequency];
    if (h_sample_frequency == fourtyfour_point_one && h_padding_bit)
      ++framesize;
  }

  return framesize - 4;		// subtract header size
}


const char *Header::layer_string (void)
{
  switch (h_layer)
  {
    case 1:
      return "I";
    case 2:
      return "II";
    case 3:
      return "III (not implemented!)";
  }
  return NULL;			// dummy
}


const char *Header::bitrate_string (void)
{
  static const char *layer1_bitrates[16] = {
    "free format", "32 kbis/s", "64 kbit/s", "96 kbit/s", "128 kbit/s", "160 kbit/s", 
    "192 kbit/s", "224 kbit/s", "256 kbit/s", "288 kbit/s", "320 kbit/s", "352 kbit/s",
    "384 kbit/s", "416 kbit/s", "448 kbit/s", "forbidden"
  };
  static const char *layer2_bitrates[16] = {
    "free format", "32 kbis/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s", 
    "96 kbit/s", "112 kbit/s", "128 kbit/s", "160 kbit/s", "192 kbit/s", "224 kbit/s",
    "256 kbit/s", "320 kbit/s", "384 kbit/s", "forbidden"
  };

  if (h_layer == 1)
    return layer1_bitrates[h_bitrate_index];
  else
    return layer2_bitrates[h_bitrate_index];
}


const char *Header::sample_frequency_string (void)
{
  switch (h_sample_frequency)
  {
    case thirtytwo:
      return "32 kHz";
    case fourtyfour_point_one:
      return "44.1 kHz";
    case fourtyeight:
      return "48 kHz";
  }
  return NULL;			// dummy
}


const char *Header::mode_string (void)
{
  switch (h_mode)
  {
    case stereo:
      return "stereo";
    case joint_stereo:
      return "joint stereo";
    case dual_channel:
      return "dual channel";
    case single_channel:
      return "single channel";
  }
  return NULL;			// dummy
}