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
|
#include <base/system.h>
#include "huffman.h"
struct CHuffmanConstructNode
{
unsigned short m_NodeId;
int m_Frequency;
};
void CHuffman::Setbits_r(CNode *pNode, int Bits, unsigned Depth)
{
if(pNode->m_aLeafs[1] != 0xffff)
Setbits_r(&m_aNodes[pNode->m_aLeafs[1]], Bits|(1<<Depth), Depth+1);
if(pNode->m_aLeafs[0] != 0xffff)
Setbits_r(&m_aNodes[pNode->m_aLeafs[0]], Bits, Depth+1);
if(pNode->m_NumBits)
{
pNode->m_Bits = Bits;
pNode->m_NumBits = Depth;
}
}
// TODO: this should be something faster, but it's enough for now
static void BubbleSort(CHuffmanConstructNode **ppList, int Size)
{
int Changed = 1;
CHuffmanConstructNode *pTemp;
while(Changed)
{
Changed = 0;
for(int i = 0; i < Size-1; i++)
{
if(ppList[i]->m_Frequency < ppList[i+1]->m_Frequency)
{
pTemp = ppList[i];
ppList[i] = ppList[i+1];
ppList[i+1] = pTemp;
Changed = 1;
}
}
Size--;
}
}
void CHuffman::ConstructTree(const unsigned *pFrequencies)
{
CHuffmanConstructNode aNodesLeftStorage[HUFFMAN_MAX_SYMBOLS];
CHuffmanConstructNode *apNodesLeft[HUFFMAN_MAX_SYMBOLS];
int NumNodesLeft = HUFFMAN_MAX_SYMBOLS;
// add the symbols
for(int i = 0; i < HUFFMAN_MAX_SYMBOLS; i++)
{
m_aNodes[i].m_NumBits = 0xFFFFFFFF;
m_aNodes[i].m_Symbol = i;
m_aNodes[i].m_aLeafs[0] = -1;
m_aNodes[i].m_aLeafs[1] = -1;
if(i == HUFFMAN_EOF_SYMBOL)
aNodesLeftStorage[i].m_Frequency = 1;
else
aNodesLeftStorage[i].m_Frequency = pFrequencies[i];
aNodesLeftStorage[i].m_NodeId = i;
apNodesLeft[i] = &aNodesLeftStorage[i];
}
m_NumNodes = HUFFMAN_MAX_SYMBOLS;
// construct the table
while(NumNodesLeft > 1)
{
// we can't rely on stdlib's qsort for this, it can generate different results on different implementations
BubbleSort(apNodesLeft, NumNodesLeft);
m_aNodes[m_NumNodes].m_NumBits = 0;
m_aNodes[m_NumNodes].m_aLeafs[0] = apNodesLeft[NumNodesLeft-1]->m_NodeId;
m_aNodes[m_NumNodes].m_aLeafs[1] = apNodesLeft[NumNodesLeft-2]->m_NodeId;
apNodesLeft[NumNodesLeft-2]->m_NodeId = m_NumNodes;
apNodesLeft[NumNodesLeft-2]->m_Frequency = apNodesLeft[NumNodesLeft-1]->m_Frequency + apNodesLeft[NumNodesLeft-2]->m_Frequency;
m_NumNodes++;
NumNodesLeft--;
}
// set start node
m_pStartNode = &m_aNodes[m_NumNodes-1];
// build symbol bits
Setbits_r(m_pStartNode, 0, 0);
}
void CHuffman::Init(const unsigned *pFrequencies)
{
int i;
// make sure to cleanout every thing
mem_zero(this, sizeof(*this));
// construct the tree
ConstructTree(pFrequencies);
// build decode LUT
for(i = 0; i < HUFFMAN_LUTSIZE; i++)
{
unsigned Bits = i;
int k;
CNode *pNode = m_pStartNode;
for(k = 0; k < HUFFMAN_LUTBITS; k++)
{
pNode = &m_aNodes[pNode->m_aLeafs[Bits&1]];
Bits >>= 1;
if(!pNode)
break;
if(pNode->m_NumBits)
{
m_apDecodeLut[i] = pNode;
break;
}
}
if(k == HUFFMAN_LUTBITS)
m_apDecodeLut[i] = pNode;
}
}
//***************************************************************
int CHuffman::Compress(const void *pInput, int InputSize, void *pOutput, int OutputSize)
{
// this macro loads a symbol for a byte into bits and bitcount
#define HUFFMAN_MACRO_LOADSYMBOL(Sym) \
Bits |= m_aNodes[Sym].m_Bits << Bitcount; \
Bitcount += m_aNodes[Sym].m_NumBits;
// this macro writes the symbol stored in bits and bitcount to the dst pointer
#define HUFFMAN_MACRO_WRITE() \
while(Bitcount >= 8) \
{ \
*pDst++ = (unsigned char)(Bits&0xff); \
if(pDst == pDstEnd) \
return -1; \
Bits >>= 8; \
Bitcount -= 8; \
}
// setup buffer pointers
const unsigned char *pSrc = (const unsigned char *)pInput;
const unsigned char *pSrcEnd = pSrc + InputSize;
unsigned char *pDst = (unsigned char *)pOutput;
unsigned char *pDstEnd = pDst + OutputSize;
// symbol variables
unsigned Bits = 0;
unsigned Bitcount = 0;
// make sure that we have data that we want to compress
if(InputSize)
{
// {A} load the first symbol
int Symbol = *pSrc++;
while(pSrc != pSrcEnd)
{
// {B} load the symbol
HUFFMAN_MACRO_LOADSYMBOL(Symbol)
// {C} fetch next symbol, this is done here because it will reduce dependency in the code
Symbol = *pSrc++;
// {B} write the symbol loaded at
HUFFMAN_MACRO_WRITE()
}
// write the last symbol loaded from {C} or {A} in the case of only 1 byte input buffer
HUFFMAN_MACRO_LOADSYMBOL(Symbol)
HUFFMAN_MACRO_WRITE()
}
// write EOF symbol
HUFFMAN_MACRO_LOADSYMBOL(HUFFMAN_EOF_SYMBOL)
HUFFMAN_MACRO_WRITE()
// write out the last bits
*pDst++ = Bits;
// return the size of the output
return (int)(pDst - (const unsigned char *)pOutput);
// remove macros
#undef HUFFMAN_MACRO_LOADSYMBOL
#undef HUFFMAN_MACRO_WRITE
}
//***************************************************************
int CHuffman::Decompress(const void *pInput, int InputSize, void *pOutput, int OutputSize)
{
// setup buffer pointers
unsigned char *pDst = (unsigned char *)pOutput;
unsigned char *pSrc = (unsigned char *)pInput;
unsigned char *pDstEnd = pDst + OutputSize;
unsigned char *pSrcEnd = pSrc + InputSize;
unsigned Bits = 0;
unsigned Bitcount = 0;
CNode *pEof = &m_aNodes[HUFFMAN_EOF_SYMBOL];
CNode *pNode = 0;
while(1)
{
// {A} try to load a node now, this will reduce dependency at location {D}
pNode = 0;
if(Bitcount >= HUFFMAN_LUTBITS)
pNode = m_apDecodeLut[Bits&HUFFMAN_LUTMASK];
// {B} fill with new bits
while(Bitcount < 24 && pSrc != pSrcEnd)
{
Bits |= (*pSrc++) << Bitcount;
Bitcount += 8;
}
// {C} load symbol now if we didn't that earlier at location {A}
if(!pNode)
pNode = m_apDecodeLut[Bits&HUFFMAN_LUTMASK];
// {D} check if we hit a symbol already
if(pNode->m_NumBits)
{
// remove the bits for that symbol
Bits >>= pNode->m_NumBits;
Bitcount -= pNode->m_NumBits;
}
else
{
// remove the bits that the lut checked up for us
Bits >>= HUFFMAN_LUTBITS;
Bitcount -= HUFFMAN_LUTBITS;
// walk the tree bit by bit
while(1)
{
// traverse tree
pNode = &m_aNodes[pNode->m_aLeafs[Bits&1]];
// remove bit
Bitcount--;
Bits >>= 1;
// check if we hit a symbol
if(pNode->m_NumBits)
break;
// no more bits, decoding error
if(Bitcount == 0)
return -1;
}
}
// check for eof
if(pNode == pEof)
break;
// output character
if(pDst == pDstEnd)
return -1;
*pDst++ = pNode->m_Symbol;
}
// return the size of the decompressed buffer
return (int)(pDst - (const unsigned char *)pOutput);
}
|