-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffman.h
282 lines (227 loc) · 7.81 KB
/
Huffman.h
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
/*
Copyright 2015 Esri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
A local copy of the license and additional notices are located with the
source distribution at:
http://github.com/Esri/lerc/
Contributors: Thomas Maurer
*/
#ifndef HUFFMAN_H
#define HUFFMAN_H
#include <vector>
#include <cstring>
#include <utility>
#include "Defines.h"
NAMESPACE_LERC_START
class Huffman
{
public:
Huffman() : m_maxHistoSize(1 << 15), m_maxNumBitsLUT(12), m_numBitsToSkipInTree(0), m_root(nullptr) {}
~Huffman() { Clear(); }
// Limitation: We limit the max Huffman code length to 32 bit. If this happens, the function ComputeCodes()
// returns false. In that case don't use Huffman coding but Lerc only instead.
// This won't happen easily. For the worst case input maximizing the Huffman code length the counts in the
// histogram have to follow the Fibonacci sequence. Even then, for < 9,227,465 data values, 32 bit is
// the max Huffman code length possible.
bool ComputeCodes(const std::vector<int>& histo); // input histogram, size < 2^15
bool ComputeCompressedSize(const std::vector<int>& histo, int& numBytes, double& avgBpp) const;
// LUT of same size as histogram, each entry has length of Huffman bit code, and the bit code
const std::vector<std::pair<unsigned short, unsigned int> >& GetCodes() const { return m_codeTable; }
bool SetCodes(const std::vector<std::pair<unsigned short, unsigned int> >& codeTable);
bool WriteCodeTable(Byte** ppByte, int lerc2Version) const;
bool ReadCodeTable(const Byte** ppByte, size_t& nBytesRemaining, int lerc2Version);
bool BuildTreeFromCodes(int& numBitsLUT);
bool DecodeOneValue(const unsigned int** ppSrc, size_t& nBytesRemaining, int& bitPos, int numBitsLUT, int& value) const;
bool DecodeOneValue_NoOverrunCheck(const unsigned int** ppSrc, size_t& nBytesRemaining, int& bitPos, int numBitsLUT, int& value) const;
void Clear();
private:
struct Node
{
int weight;
short value;
Node *child0, *child1;
Node(short val, int cnt) // new leaf node for val
{
value = val;
weight = -cnt;
child0 = child1 = nullptr;
}
Node(Node* c0, Node* c1) // new internal node from children c0 and c1
{
value = -1;
weight = c0->weight + c1->weight;
child0 = c0;
child1 = c1;
}
bool operator < (const Node& other) const { return weight < other.weight; }
bool TreeToLUT(unsigned short numBits, unsigned int bits, std::vector<std::pair<unsigned short, unsigned int> >& luTable) const
{
if (child0)
{
if (numBits == 32 // the max huffman code length we allow
|| !child0->TreeToLUT(numBits + 1, (bits << 1) + 0, luTable)
|| !child1->TreeToLUT(numBits + 1, (bits << 1) + 1, luTable))
{
return false;
}
}
else
luTable[value] = std::pair<unsigned short, unsigned int>(numBits, bits);
return true;
}
void FreeTree(int& n)
{
if (child0)
{
child0->FreeTree(n);
delete child0;
child0 = nullptr;
n--;
}
if (child1)
{
child1->FreeTree(n);
delete child1;
child1 = nullptr;
n--;
}
}
};
private:
size_t m_maxHistoSize;
std::vector<std::pair<unsigned short, unsigned int> > m_codeTable;
std::vector<std::pair<short, short> > m_decodeLUT;
int m_maxNumBitsLUT;
int m_numBitsToSkipInTree;
Node* m_root;
static int GetIndexWrapAround(int i, int size) { return i - (i < size ? 0 : size); }
bool ComputeNumBytesCodeTable(int& numBytes) const;
bool GetRange(int& i0, int& i1, int& maxCodeLength) const;
bool BitStuffCodes(Byte** ppByte, int i0, int i1) const;
bool BitUnStuffCodes(const Byte** ppByte, size_t& nBytesRemaining, int i0, int i1);
bool ConvertCodesToCanonical();
void ClearTree();
};
// -------------------------------------------------------------------------- ;
inline bool Huffman::DecodeOneValue(const unsigned int** ppSrc, size_t& nBytesRemaining, int& bitPos, int numBitsLUT, int& value) const
{
const size_t sizeUInt = sizeof(unsigned int);
if (!ppSrc || !(*ppSrc) || bitPos < 0 || bitPos >= 32 || nBytesRemaining < sizeUInt)
return false;
// first get the next (up to) 12 bits as a copy
int valTmp = ((**ppSrc) << bitPos) >> (32 - numBitsLUT);
if (32 - bitPos < numBitsLUT)
{
if (nBytesRemaining < 2 * sizeUInt)
return false;
valTmp |= (*(*ppSrc + 1)) >> (64 - bitPos - numBitsLUT);
}
if (m_decodeLUT[valTmp].first >= 0) // if there, move the correct number of bits and done
{
value = m_decodeLUT[valTmp].second;
bitPos += m_decodeLUT[valTmp].first;
if (bitPos >= 32)
{
bitPos -= 32;
(*ppSrc)++;
nBytesRemaining -= sizeUInt;
}
return true;
}
// if not there, go through the tree (slower)
if (!m_root)
return false;
// skip leading 0 bits before entering the tree
bitPos += m_numBitsToSkipInTree;
if (bitPos >= 32)
{
bitPos -= 32;
(*ppSrc)++;
nBytesRemaining -= sizeUInt;
}
const Node* node = m_root;
value = -1;
while (value < 0 && nBytesRemaining >= sizeUInt)
{
int bit = ((**ppSrc) << bitPos) >> 31;
bitPos++;
if (bitPos == 32)
{
bitPos = 0;
(*ppSrc)++;
nBytesRemaining -= sizeUInt;
}
node = bit ? node->child1 : node->child0;
if (!node)
return false;
if (node->value >= 0) // reached a leaf node
value = node->value;
}
return (value >= 0);
}
// -------------------------------------------------------------------------- ;
inline bool Huffman::DecodeOneValue_NoOverrunCheck(const unsigned int** ppSrc, size_t& nBytesRemaining, int& bitPos, int numBitsLUT, int& value) const
{
const size_t sizeUInt = sizeof(unsigned int);
if (!ppSrc || !(*ppSrc) || bitPos < 0 || bitPos >= 32)
return false;
// first get the next (up to) 12 bits as a copy
int valTmp = ((**ppSrc) << bitPos) >> (32 - numBitsLUT);
if (32 - bitPos < numBitsLUT)
{
valTmp |= (*(*ppSrc + 1)) >> (64 - bitPos - numBitsLUT);
}
if (m_decodeLUT[valTmp].first >= 0) // if there, move the correct number of bits and done
{
value = m_decodeLUT[valTmp].second;
bitPos += m_decodeLUT[valTmp].first;
if (bitPos >= 32)
{
bitPos -= 32;
(*ppSrc)++;
nBytesRemaining -= sizeUInt;
}
return true;
}
// if not there, go through the tree (slower)
if (!m_root)
return false;
// skip leading 0 bits before entering the tree
bitPos += m_numBitsToSkipInTree;
if (bitPos >= 32)
{
bitPos -= 32;
(*ppSrc)++;
nBytesRemaining -= sizeUInt;
}
const Node* node = m_root;
value = -1;
while (value < 0)
{
int bit = ((**ppSrc) << bitPos) >> 31;
bitPos++;
if (bitPos == 32)
{
bitPos = 0;
(*ppSrc)++;
nBytesRemaining -= sizeUInt;
}
node = bit ? node->child1 : node->child0;
if (!node)
return false;
if (node->value >= 0) // reached a leaf node
value = node->value;
}
return (value >= 0);
}
// -------------------------------------------------------------------------- ;
NAMESPACE_LERC_END
#endif