-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHeader.pas
More file actions
287 lines (238 loc) · 7.34 KB
/
Copy pathHeader.pas
File metadata and controls
287 lines (238 loc) · 7.34 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
(*
* File: $RCSfile: Header.pas,v $
* Revision: $Revision: 1.1.1.1 $
* Version : $Id: Header.pas,v 1.1.1.1 2002/04/21 12:57:16 fobmagog Exp $
* Author: $Author: fobmagog $
* Homepage: http://delphimpeg.sourceforge.net/
*
* 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.
*)
unit Header;
{$mode delphi}
interface
uses
BitStream, L3Tables;
type
TVersion = (MPEG2_LSF, MPEG1);
TMode = (Stereo, JointStereo, DualChannel, SingleChannel);
TCRC16 = object
private
FCRC: Word;
public
procedure Init;
procedure AddBits(BitString: Cardinal; Length: Cardinal);
function Checksum: Word;
end;
// Class for extraction information from a frame header:
THeader = class
private
FLayer: Cardinal;
FProtectionBit: Cardinal;
FBitrateIndex: Cardinal;
FPaddingBit: Cardinal;
FModeExtension: Cardinal;
FVersion: TVersion;
FMode: TMode;
FSampleFrequency: TSampleFrequency;
FNumberOfSubbands: Cardinal;
FIntensityStereoBound: Cardinal;
FInitialSync: Boolean;
//FCRC: TCRC16;
FChecksum: Cardinal;
FFrameSize: Cardinal;
FNumSlots: Cardinal;
function GetFrequency: Cardinal;
function GetChecksums: Boolean;
function GetPadding: Boolean;
public
property Version: TVersion read FVersion;
property Layer: Cardinal read FLayer;
property BitrateIndex: Cardinal read FBitrateIndex;
property SampleFrequency: TSampleFrequency read FSampleFrequency;
property Frequency: Cardinal read GetFrequency;
property Mode: TMode read FMode;
property Checksums: Boolean read GetChecksums;
property Padding: Boolean read GetPadding;
property Slots: Cardinal read FNumSlots;
property ModeExtension: Cardinal read FModeExtension;
property NumberOfSubbands: Cardinal read FNumberOfSubbands;
// returns the number of subbands in the current frame
property IntensityStereoBound: Cardinal read FIntensityStereoBound;
// (Layer II joint stereo only)
// returns the number of subbands which are in stereo mode,
// subbands above that limit are in intensity stereo mode
constructor Create;
function ReadHeader(Stream: TBitStream): Boolean;
// read a 32-bit header from the bitstream
function Bitrate: Cardinal;
function CalculateFrameSize: Cardinal;
end;
implementation
const
POLYNOMIAL: Word = $8005;
mp3_bit_rate_index_tab: array[0..14] of word = (
0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320
);
mp3_sampling_frequency_tab: array[TSampleFrequency] of word = (
44100, 48000, 32000, 0
);
{ THeader }
function THeader.Bitrate: Cardinal;
begin
Result := mp3_bit_rate_index_tab[FBitrateIndex] * 1000;
end;
// calculates framesize in bytes excluding header size
function THeader.CalculateFrameSize: Cardinal;
var
Val1, Val2: Cardinal;
begin
Assert(FLayer = 3, 'not layer 3');
Assert(FVersion = MPEG1, 'invalid format version');
FFrameSize := 144 * mp3_bit_rate_index_tab[FBitrateIndex] * 1000 div mp3_sampling_frequency_tab[FSampleFrequency];
if (FPaddingBit <> 0) then
FFrameSize += 1;
// Layer III slots
if (FMode = SingleChannel) then
Val1 := 17
else
Val1 := 32;
if (FProtectionBit <> 0) then
Val2 := 0
else
Val2 := 2;
FNumSlots := FFramesize - Val1 - Val2 - 4;
dec(FFrameSize, 4); // subtract header size
Result := FFrameSize;
end;
constructor THeader.Create;
begin
FFrameSize := 0;
FNumSlots := 0;
FInitialSync := False;
//FCRC := FCRC.Create;
end;
function THeader.GetChecksums: Boolean;
begin
Result := (FProtectionBit = 0);
end;
function THeader.GetFrequency: Cardinal;
begin
Result := mp3_sampling_frequency_tab[FSampleFrequency];
end;
function THeader.GetPadding: Boolean;
begin
Result := (FPaddingBit <> 0);
end;
function THeader.ReadHeader(Stream: TBitStream): Boolean;
var
HeaderString, ChannelBitrate: integer;
begin
Result := False;
if (not FInitialSync) then begin
if (not Stream.GetHeader(@HeaderString, INITIAL_SYNC)) then
exit;
FVersion := TVersion((HeaderString shr 19) and 1);
//allow mpeg layer 3 only
if FVersion <> MPEG1 then
exit;
FSampleFrequency := TSampleFrequency((HeaderString shr 10) and 3);
if (FSampleFrequency = Unknown) then begin
// report error - not supported header
exit;
end;
Stream.SetSyncWord(HeaderString and $FFF80CC0);
FInitialSync := True;
end else begin
if (not Stream.GetHeader(@HeaderString, STRICT_SYNC)) then begin
exit;
end;
end;
FLayer := 4 - (HeaderString shr 17) and 3;
//allow mpeg layer 3 only
if Flayer <> 3 then
exit;
FProtectionBit := (HeaderString shr 16) and 1;
FBitrateIndex := (HeaderString shr 12) and $F;
FPaddingBit := (HeaderString shr 9) and 1;
FMode := TMode((HeaderString shr 6) and 3);
FModeExtension := (HeaderString shr 4) and 3;
if (FMode = JointStereo) then
FIntensityStereoBound := (FModeExtension shl 2) + 4
else
FIntensityStereoBound := 0; // should never be used
// calculate number of subbands:
if (FLayer = 1) then
FNumberOfSubbands := 32
else begin
ChannelBitrate := FBitrateIndex;
// calculate bitrate per channel:
if (FMode <> SingleChannel) then
if (ChannelBitrate <= 4) then
ChannelBitrate := 1
else
dec(ChannelBitrate, 4);
if ((ChannelBitrate = 1) or (ChannelBitrate = 2)) then begin
if (FSampleFrequency = SampleFreq_32) then
FNumberOfSubbands := 12
else
FNumberOfSubbands := 8;
end else begin
if ((FSampleFrequency = SampleFreq_48) or ((ChannelBitrate >= 3) and (ChannelBitrate <= 5))) then
FNumberOfSubbands := 27
else
FNumberOfSubbands := 30;
end;
end;
if (FIntensityStereoBound > FNumberOfSubbands) then
FIntensityStereoBound := FNumberOfSubbands;
// calculate framesize and nSlots
CalculateFrameSize;
// read framedata:
if (not Stream.ReadFrame(FFrameSize)) then begin
exit;
end;
if (FProtectionBit = 0) then begin
FChecksum := Stream.GetBits(16);
{ not useful until whole frame data is checked
FCRC.Init();
FCRC.AddBits(HeaderString, 16); //16bits of header starting from bitrate index
}
end;
Result := True;
end;
{ TCRC16 }
// feed a bitstring to the crc calculation (0 < length <= 32)
procedure TCRC16.AddBits(BitString, Length: Cardinal);
var BitMask: Cardinal;
begin
BitMask := 1 shl (Length - 1);
repeat
if ((FCRC and $8000 = 0) xor (BitString and BitMask = 0)) then begin
FCRC := FCRC shl 1;
FCRC := FCRC xor POLYNOMIAL;
end else
FCRC := FCRC shl 1;
BitMask := BitMask shr 1;
until (BitMask = 0);
end;
function TCRC16.Checksum: Word;
begin
Result := FCRC;
end;
procedure TCRC16.Init;
begin
FCRC := $FFFF;
end;
end.