-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlacBitReader.cs
181 lines (161 loc) · 5.01 KB
/
FlacBitReader.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using NAudio.Utils;
namespace NAudio.Flac
{
public class FlacBitReader : BitReader
{
/*unary table from codeproject*/
internal static readonly byte[] UnaryTable = new byte[]
{
8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
public FlacBitReader(byte[] buffer, int offset)
: base(buffer, offset)
{
}
public unsafe FlacBitReader(byte* buffer, int offset)
: base(buffer, offset)
{
}
public uint ReadUnary()
{
uint result = 0;
uint unaryindicator = Cache >> 24;
while (unaryindicator == 0)
{
SeekBits(8);
result += 8;
unaryindicator = Cache >> 24;
}
result += UnaryTable[unaryindicator];
SeekBits((int)(result & 7) + 1);
return result;
}
public int ReadUnarySigned()
{
var value = ReadUnary();
return (int)(value >> 1 ^ -((int)(value & 1)));
}
#region utf8
public bool ReadUTF8_64(out ulong result)
{
uint x = ReadBits(8);
ulong v;
int i;
if ((x & 0x80) == 0)
{
v = x;
i = 0;
}
else if ((x & 0xC0) != 0 && (x & 0x20) == 0)
{
v = x & 0x1F;
i = 1;
}
else if ((x & 0xE0) != 0 && (x & 0x10) == 0) /* 1110xxxx */
{
v = x & 0x0F;
i = 2;
}
else if ((x & 0xF0) != 0 && (x & 0x08) == 0) /* 11110xxx */
{
v = x & 0x07;
i = 3;
}
else if ((x & 0xF8) != 0 && (x & 0x04) == 0) /* 111110xx */
{
v = x & 0x03;
i = 4;
}
else if ((x & 0xFC) != 0 && (x & 0x02) == 0) /* 1111110x */
{
v = x & 0x01;
i = 5;
}
else if ((x & 0xFE) != 0 && (x & 0x01) == 0)
{
v = 0;
i = 6;
}
else
{
result = ulong.MaxValue;
return false;
}
for (; i != 0; i--)
{
x = ReadBits(8);
if ((x & 0xC0) != 0x80)
{
result = ulong.MaxValue;
return false;
}
v <<= 6;
v |= (x & 0x3F);
}
result = v;
return true;
}
public bool ReadUTF8_32(out uint result)
{
uint v, x;
int i;
x = ReadBits(8);
if ((x & 0x80) == 0)
{
v = x;
i = 0;
}
else if ((x & 0xC0) != 0 && (x & 0x20) == 0)
{
v = x & 0x1F;
i = 1;
}
else if ((x & 0xE0) != 0 && (x & 0x10) == 0) /* 1110xxxx */
{
v = x & 0x0F;
i = 2;
}
else if ((x & 0xF0) != 0 && (x & 0x08) == 0) /* 11110xxx */
{
v = x & 0x07;
i = 3;
}
else if ((x & 0xF8) != 0 && (x & 0x04) == 0) /* 111110xx */
{
v = x & 0x03;
i = 4;
}
else if ((x & 0xFC) != 0 && (x & 0x02) == 0) /* 1111110x */
{
v = x & 0x01;
i = 5;
}
else
{
result = uint.MaxValue;
return false;
}
for (; i != 0; i--)
{
x = ReadBits(8);
if ((x & 0xC0) != 0x80)
{
result = uint.MaxValue;
return false;
}
v <<= 6;
v |= (x & 0x3F);
}
result = v;
return true;
}
#endregion utf8
}
}