-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitStream.pas
325 lines (271 loc) · 9.33 KB
/
BitStream.pas
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
(*
* File: $RCSfile: BitStream.pas,v $
* Revision: $Revision: 1.1.1.1 $
* Version : $Id: BitStream.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.
*)
{$DEFINE SEEK_STOP}
{$DEFINE DAMN_INTEL_BYTE_ORDER}
unit BitStream;
interface
uses
Windows, SysUtils, Classes, BitReserve;
type
TSyncMode = (INITIAL_SYNC, STRICT_SYNC);
const
BUFFERINTSIZE = 433;
// max. 1730 bytes per frame: 144 * 384kbit/s / 32000 Hz + 2 Bytes CRC
type
// Class to extract bitstrings from files:
TBitStream = class
private
FStream: TFileStream;
FBuffer: array[0..BUFFERINTSIZE-1] of Cardinal;
FFrameSize: Cardinal; // number of valid bytes in buffer
FWordPointer: PCardinal; // position of next unsigned int for get_bits()
FBitIndex: Cardinal; // number (0-31, from MSB to LSB) of next bit for get_bits()
FSyncWord: Cardinal;
FSingleChMode: Boolean;
FCurrentFrameNumber: Integer;
FLastFrameNumber: Integer;
public
NonSeekable: Boolean;
StreamHandle: Pointer;
PlayerObj: Pointer;
property CurrentFrame: Integer read FCurrentFrameNumber;
property LastFrame: Integer read FLastFrameNumber;
constructor Create(FileName: PChar);
destructor Destroy; override;
function Restart: Boolean;
function GetHeader(HeaderString: PCardinal; SyncMode: TSyncMode): Boolean;
// get next 32 bits from bitstream in an unsigned int,
// returned value False => end of stream
function ReadFrame(ByteSize: Cardinal): Bool;
// fill buffer with data from bitstream, returned value False => end of stream
function GetBits(NumberOfBits: Cardinal): Cardinal;
// read bits (1 <= number_of_bits <= 16) from buffer into the lower bits
// of an unsigned int. The LSB contains the latest read bit of the stream.
function GetBitsFloat(NumberOfBits: Cardinal): Single;
// read bits (1 <= number_of_bits <= 16) from buffer into the lower bits
// of a floating point. The LSB contains the latest read bit of the stream.
procedure SetSyncWord(SyncWord: Cardinal);
// Set the word we want to sync the header to, in
// Big-Endian byte order
function FileSize: Cardinal;
// Returns the size, in bytes, of the input file.
// Stream searching routines (Jeff Tsay)
function Seek(Frame: Integer; FrameSize: Integer): Boolean;
// Seeks to frames
function SeekPad(Frame: Integer; FrameSize: Integer; var Header: TObject{THeader}; Offset: PCardinalArray): Boolean;
// Seeks frames for 44.1 or 22.05 kHz (padded) files
end;
implementation
uses
CRC, Header;
function SwapInt32(Value: Cardinal): Cardinal;
begin
Result := (Value shl 24) or ((Value shl 8) and $00ff0000) or
((Value shr 8) and $0000ff00) or (Value shr 24);
end;
{ TBitStream }
constructor TBitStream.Create(FileName: PChar);
begin
FStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyNone);
Restart;
NonSeekable := false;
end;
destructor TBitStream.Destroy;
begin
FreeAndNil(FStream);
inherited Destroy;
end;
function TBitStream.FileSize: Cardinal;
begin
Result := FStream.Size;
end;
const
BitMask: array[0..17] of Cardinal = (
0, // dummy
$00000001, $00000003, $00000007, $0000000F,
$0000001F, $0000003F, $0000007F, $000000FF,
$000001FF, $000003FF, $000007FF, $00000FFF,
$00001FFF, $00003FFF, $00007FFF, $0000FFFF,
$0001FFFF);
function TBitStream.GetBits(NumberOfBits: Cardinal): Cardinal;
var ReturnValue: Cardinal;
Sum: Cardinal;
begin
Sum := FBitIndex + NumberOfBits;
if (sum <= 32) then begin
// all bits contained in *wordpointer
Result := (FWordPointer^ shr (32 - sum)) and BitMask[NumberOfBits];
inc(FBitIndex, NumberOfBits);
if (FBitIndex = 32) then begin
FBitIndex := 0;
inc(FWordPointer);
end;
exit;
end;
{$IFDEF DAMN_INTEL_BYTE_ORDER}
PWord(@PByteArray(@ReturnValue)[2])^ := PWord(FWordPointer)^;
inc(FWordPointer);
PWord(@ReturnValue)^ := PWord(@PByteArray(FWordPointer)[2])^;
{$ELSE}
PWord(@ReturnValue)^ := PWord(@PByteArray(FWordPointer)[2])^;
inc(FWordPointer);
PWord(@PByteArray(@ReturnValue)[2])^ := PWord(FWordPointer)^;
{$ENDIF}
ReturnValue := ReturnValue shr (48 - Sum); // returnvalue >>= 16 - (number_of_bits - (32 - bitindex))
Result := ReturnValue and BitMask[NumberOfBits];
FBitIndex := Sum - 32;
end;
function TBitStream.GetBitsFloat(NumberOfBits: Cardinal): Single;
begin
PCardinal(@Result)^ := GetBits(NumberOfBits);
end;
function TBitStream.GetHeader(HeaderString: PCardinal;
SyncMode: TSyncMode): Boolean;
var Sync: Boolean;
NumRead: Integer;
begin
repeat
// Read 4 bytes from the file, placing the number of bytes actually
// read in numread
NumRead := FStream.Read(HeaderString^, 4);
Result := (NumRead = 4);
if (not Result) then
exit;
if (SyncMode = INITIAL_SYNC) then
Sync := ((HeaderString^ and $0000F0FF) = $0000F0FF)
else
Sync := ((HeaderString^ and $000CF8FF) = FSyncWord) and
(((HeaderString^ and $C0000000) = $C0000000) = FSingleChMode);
// if ((HeaderString^ and $0000FFFF) = $0000FFFF) then
// Sync := false;
if (not Sync) then
// rewind 3 bytes in the file so we can try to sync again, if
// successful set result to TRUE
FStream.Seek(-3, soFromCurrent);
until (Sync) or (not Result);
if (not Result) then
exit;
{$IFDEF DAMN_INTEL_BYTE_ORDER}
HeaderString^ := SwapInt32(HeaderString^);
{$ENDIF}
inc(FCurrentFrameNumber);
{$IFDEF SEEK_STOP}
if (FLastFrameNumber < FCurrentFrameNumber) then
FLastFrameNumber := FCurrentFrameNumber;
{$ENDIF}
Result := True;
end;
function TBitStream.ReadFrame(ByteSize: Cardinal): Bool;
var NumRead: Integer;
{$IFDEF DAMN_INTEL_BYTE_ORDER}
WordP: PCardinal;
{$ENDIF}
begin
// read bytesize bytes from the file, placing the number of bytes
// actually read in numread and setting result to TRUE if
// successful
NumRead := FStream.Read(FBuffer, ByteSize);
FWordPointer := @FBuffer;
FBitIndex := 0;
FFrameSize := ByteSize;
{$IFDEF DAMN_INTEL_BYTE_ORDER}
WordP := @FBuffer[(ByteSize-1) shr 2];
while (Cardinal(WordP) >= Cardinal(@FBuffer)) do begin
WordP^ := SwapInt32(WordP^);
dec(WordP);
end;
{$ENDIF}
Result := Cardinal(NumRead) = FFrameSize;
end;
function TBitStream.Restart: Boolean;
begin
FStream.Seek(0, soFromBeginning);
FWordPointer := @FBuffer;
FBitIndex := 0;
// Seeking variables
FCurrentFrameNumber := -1;
FLastFrameNumber := -1;
Result := true;
end;
function TBitStream.Seek(Frame, FrameSize: Integer): Boolean;
begin
FCurrentFrameNumber := Frame - 1;
if (NonSeekable) then begin
Result := False;
exit;
end;
FStream.Seek(Frame * (FrameSize + 4), soFromBeginning);
Result := True;
end;
function TBitStream.SeekPad(Frame, FrameSize: Integer;
var Header: TObject; Offset: PCardinalArray): Boolean;
var CRC: TCRC16;
TotalFrameSize: Integer;
Diff: Integer;
begin
// base_frame_size is the frame size _without_ padding.
if (NonSeekable) then begin
Result := False;
exit;
end;
CRC := nil;
TotalFrameSize := FrameSize + 4;
if (FLastFrameNumber < Frame) then begin
if (FLastFrameNumber >= 0) then
Diff := Offset[FLastFrameNumber]
else
Diff := 0;
// set the file pointer to ((last_frame_number+1) * total_frame_size)
// bytes after the beginning of the file
FStream.Seek((FLastFrameNumber + 1) * TotalFrameSize + Diff, soFromBeginning);
FCurrentFrameNumber := FLastFrameNumber;
repeat
if (not THeader(Header).ReadHeader(Self, CRC)) then begin
Result := False;
exit;
end;
until (FLastFrameNumber >= Frame);
Result := True;
end else begin
if (Frame > 0) then
Diff := Offset[Frame-1]
else
Diff := 0;
// set the file pointer to (frame * total_frame_size + diff) bytes
// after the beginning of the file
FStream.Seek(Frame * TotalFrameSize + Diff, soFrombeginning);
FCurrentFrameNumber := Frame - 1;
Result := THeader(Header).ReadHeader(Self, CRC);
end;
if (CRC <> nil) then
FreeAndNil(CRC);
end;
procedure TBitStream.SetSyncWord(SyncWord: Cardinal);
begin
{$IFDEF DAMN_INTEL_BYTE_ORDER}
FSyncWord := SwapInt32(Syncword and $FFFFFF3F);
{$ELSE}
FSyncWord := SyncWord and $FFFFFF3F;
{$ENDIF}
FSingleChMode := ((SyncWord and $000000C0) = $000000C0);
end;
end.