-
Notifications
You must be signed in to change notification settings - Fork 1
/
BitsUtils.cs
158 lines (133 loc) · 2.73 KB
/
BitsUtils.cs
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
using System;
/*
** BitsUtils.cs
**
** Copyright (c) 2010-2016 Peter McQuillan
**
** All Rights Reserved.
**
** Distributed under the BSD Software License (see license.txt)
***/
class BitsUtils
{
internal static Bitstream getbit(Bitstream bs)
{
if (bs.bc > 0)
{
bs.bc--;
}
else
{
bs.ptr++;
bs.buf_index++;
bs.bc = 7;
if (bs.ptr == bs.end)
{
// wrap call here
bs = bs_read(bs);
}
bs.sr = (bs.buf[bs.buf_index] & 0xff);
}
bs.bitval = (int)(bs.sr & 1);
bs.sr = bs.sr >> 1;
return bs;
}
internal static long getbits(int nbits, Bitstream bs)
{
int uns_buf;
long retval;
while ((nbits) > bs.bc)
{
bs.ptr++;
bs.buf_index++;
if (bs.ptr == bs.end)
{
bs = bs_read(bs);
}
uns_buf = (int) (bs.buf[bs.buf_index] & 0xff);
bs.sr = bs.sr | (uns_buf << bs.bc); // values in buffer must be unsigned
bs.sr = bs.sr & 0xFFFFFFFFL; // sr is an unsigned 32 bit variable
bs.bc += 8;
}
retval = bs.sr;
if (bs.bc > 32)
{
bs.bc -= (nbits);
bs.sr = (bs.buf[bs.buf_index] & 0xff) >> (8 - bs.bc);
}
else
{
bs.bc -= (nbits);
bs.sr >>= (nbits);
}
return (retval);
}
internal static Bitstream bs_open_read(byte[] stream, short buffer_start, short buffer_end, System.IO.BinaryReader file, int file_bytes, int passed)
{
// CLEAR (*bs);
Bitstream bs = new Bitstream();
bs.buf = stream;
bs.buf_index = buffer_start;
bs.end = buffer_end;
bs.sr = 0;
bs.bc = 0;
if (passed != 0)
{
bs.ptr = (short) (bs.end - 1);
bs.file_bytes = file_bytes;
bs.file = file;
}
else
{
/* Strange to set an index to -1, but the very first call to getbit will iterate this */
bs.buf_index = - 1;
bs.ptr = (short) (- 1);
}
return bs;
}
internal static Bitstream bs_read(Bitstream bs)
{
if (bs.file_bytes > 0)
{
int bytes_to_read;
int bytes_read;
bytes_to_read = Defines.BITSTREAM_BUFFER_SIZE;
if (bytes_to_read > bs.file_bytes)
bytes_to_read = bs.file_bytes;
try
{
bytes_read = bs.file.BaseStream.Read(bs.buf, 0, bytes_to_read);
bs.buf_index = 0;
}
catch (System.Exception e)
{
System.Console.Error.WriteLine("Big error while reading file: " + e);
bytes_read = 0;
}
if (bytes_read > 0)
{
bs.end = (short) (bytes_read);
bs.file_bytes -= bytes_read;
}
else
{
for (int i = 0; i < Defines.BITSTREAM_BUFFER_SIZE; i++)
{
bs.buf[i] = unchecked((byte)-1);
}
bs.error = 1;
}
}
else
{
bs.error = 1;
for (int i = 0; i < Defines.BITSTREAM_BUFFER_SIZE; i++)
{
bs.buf[i] = unchecked((byte)-1);
}
}
bs.ptr = 0;
bs.buf_index = 0;
return bs;
}
}