-
Notifications
You must be signed in to change notification settings - Fork 2
/
BitStream.pas
269 lines (225 loc) · 6.83 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
(*
* 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.
*)
unit BitStream;
interface
uses
SysUtils, Classes;
type
TSyncMode = (INITIAL_SYNC, STRICT_SYNC);
const
BUFFER_SIZE = 1448; // 320000 {max. bitrate} / 32000 {freq} * 144 {num samples / 8} + 8 {padding, crc, header}
type
{ TBitstreamReader }
TBitstreamReader = class
private
bits: uint32;
mask: longword;
cur: pbyte;
buffer: pbyte;
public
constructor Create(const memory_buffer: pbyte);
procedure Start();
function GetBytePosition: longword;
function GetBitPosition: longword;
function Read(): longword;
function Read(count: longword): longword;
procedure SkipBack(count: longword);
end;
TBitStream = class
private
FStream: TStream;
FBuffer: array[0..BUFFER_SIZE-1] of byte;
FFrameSize: Cardinal; // number of valid bytes in buffer
FSyncWord: Cardinal;
FSingleChMode: Boolean;
FCurrentFrameNumber: Integer;
bs: TBitstreamReader;
public
constructor Create(input_stream: TStream);
destructor Destroy; override;
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): Boolean;
// 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.
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.
function CurrentDataPointer: pbyte;
end;
implementation
//fpc has SwapEndian, but it doesn't get inlined
function bswap(n: longword): longword; inline;
begin
{$ifdef ENDIAN_LITTLE}
result := (n shr 24) or
(n shl 24) or
((n shr 8) and $ff00) or
((n shl 8) and $ff0000);
{$else}
result := n;
{$endif}
end;
{ TBitStream }
constructor TBitStream.Create(input_stream: TStream);
begin
FStream := input_stream;
FStream.Seek(0, soFromBeginning);
FCurrentFrameNumber := -1;
bs := TBitstreamReader.Create(@FBuffer);
end;
destructor TBitStream.Destroy;
begin
bs.Free;
inherited Destroy;
end;
function TBitStream.FileSize: Cardinal;
begin
Result := FStream.Size;
end;
function TBitStream.CurrentDataPointer: pbyte;
var
bytes_from_buffer: integer;
begin
bytes_from_buffer := (32 - bs.mask) div 8;
result := bs.cur + bytes_from_buffer;
end;
function TBitStream.GetBits(NumberOfBits: Cardinal): Cardinal;
begin
result := bs.Read(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;
HeaderString^ := bswap(HeaderString^);
inc(FCurrentFrameNumber);
Result := True;
end;
function TBitStream.ReadFrame(ByteSize: Cardinal): Boolean;
var NumRead: Integer;
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);
bs.Start();
FFrameSize := ByteSize;
Result := Cardinal(NumRead) = FFrameSize;
end;
procedure TBitStream.SetSyncWord(SyncWord: Cardinal);
begin
FSyncWord := bswap(Syncword and $FFFFFF3F);
FSingleChMode := ((SyncWord and $000000C0) = $000000C0);
end;
{ TBitstreamReader }
constructor TBitstreamReader.Create(const memory_buffer: pbyte);
begin
buffer := memory_buffer;
Start;
end;
//called after memory buffer data changes
procedure TBitstreamReader.Start();
begin
if cur <> buffer then begin
cur := buffer;
mask := 32;
end;
bits := bswap( plongword(cur)^ );
end;
function TBitstreamReader.GetBytePosition: longword;
begin
result := cur - buffer;
//result += (32 - mask + 7) div 8; //bytes used from current buffer
end;
function TBitstreamReader.GetBitPosition: longword;
begin
result := 8 * (cur - buffer) + (32 - mask);
end;
function TBitstreamReader.Read(): longword;
begin
mask -= 1;
result := (bits shr mask) and 1;
if mask = 0 then begin
cur += 4;
bits := bswap( plongword(cur)^ );
mask := 32;
end;
end;
function TBitstreamReader.Read(count: longword): longword;
var
bits_left: longword;
begin
Assert(count < 32, 'count of bits to read is over 31');
if mask > count then begin
result := bits shr (mask - count) and ($ffffffff shr (32 - count));
mask -= count;
end
else begin
bits_left := count - mask;
result := bits and ($ffffffff shr (32 - mask)) shl bits_left;
cur += 4;
bits := bswap( plongword(cur)^ );
mask := 32 - bits_left;
if bits_left > 0 then
result := result or (bits shr mask);
end;
end;
procedure TBitstreamReader.SkipBack(count: longword);
begin
Assert(count < 32, 'count of bits to skip is over 31');
if mask + count <= 32 then
mask += count
else begin
cur -= 4;
bits := bswap( plongword(cur)^ );
mask := mask + count - 32;
end;
end;
end.