-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.cpp
162 lines (121 loc) · 3.83 KB
/
compress.cpp
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
#include "compress.h"
#include "header.h"
#include "bwt.h"
#include "mtf.h"
#include "arithcoder.h"
#include "outputstream.h"
#include "inputbuffer.h"
#include "structuredmodel.h"
#include "rle.h"
//----------------------------------------------------------------------------//
static void getModelStatistics(const uint8* pBuffer, const uint32 length, uint8* pIncrements, float* pStats);
static void optimizeIncrements(const uint8* pBuffer, const uint32 length, uint8* pIncrements);
static void writeHeader(OutputStream& os, header& h);
//----------------------------------------------------------------------------//
void compress(const char* pInputFilename, const char* pOutputFilename) {
FileInputBuffer input(pInputFilename);
FileOutputStream output(pOutputFilename);
compress(input, output);
}
//----------------------------------------------------------------------------//
void compress(InputBuffer& input, OutputStream& output) {
header h;
h.fileSize = input.getSize();
uint8* pInputBuffer = input.getBuffer();
uint8* pOutputBuffer = new uint8[h.fileSize];
RLE(pInputBuffer, pOutputBuffer, h.fileSize, &h.tempSize);
BWT(pOutputBuffer, pInputBuffer, h.tempSize, &h.first);
MTF(pInputBuffer, h.tempSize);
optimizeIncrements(pInputBuffer, h.tempSize, h.increments);
#if 0
float model_stats[8];
getModelStatistics(pInputBuffer, h.tempSize, h.increments, model_stats);
float sum = 0;
for (uint32 i = 0; i < 8; i++) {
printf("model #%d: %12.2f bits (increment: %3d)\n", i, model_stats[i], h.increments[i]);
sum += model_stats[i];
}
printf("%f bpb\n", sum / h.fileSize);
#endif
writeHeader(output, h);
ArithmeticEncoder encoder(output);
StructuredModel model(h.increments);
model.encode(encoder, pInputBuffer, h.tempSize);
delete[] pInputBuffer;
delete[] pOutputBuffer;
}
//----------------------------------------------------------------------------//
static void writeHeader(OutputStream& os, header& h) {
os.outputBits(h.first, 32);
for (uint32 i = 0; i < 8; i++) {
os.outputBits(h.increments[i], 8);
}
os.outputBits(h.fileSize, 32);
os.outputBits(h.tempSize, 32);
}
//----------------------------------------------------------------------------//
static void getModelStatistics(const uint8* pBuffer, const uint32 length, uint8* pIncrements, float* pStats) {
StructuredModel model(pIncrements);
for (uint32 i = 0; i < 8; i++) {
pStats[i] = 0.0f;
}
uint32 zeroRun = 0;
for (uint32 i = 0; i < length; i++) {
uint32 symbol = pBuffer[i];
if (symbol == 0) {
zeroRun++;
} else {
if (zeroRun) {
model.getZeroRunEntropy(pStats, zeroRun);
zeroRun = 0;
}
model.getSymbolEntropy(pStats, symbol);
}
}
if (zeroRun) {
model.getZeroRunEntropy(pStats, zeroRun);
}
}
//----------------------------------------------------------------------------//
static void optimizeIncrements(const uint8* pBuffer, const uint32 length, uint8* pIncrements) {
uint8 left[8];
uint8 right[8];
for (uint32 i = 0; i < 8; i++) {
left[i] = 0;
right[i] = 255;
}
uint32 iter = 0;
uint32 not_done = 0xff;
while (not_done) {
uint8 left3[8];
uint8 right3[8];
for (uint32 i = 0; i < 8; i++) {
left3[i] = (2*left[i] + right[i]) / 3;
right3[i] = ( left[i] + 2*right[i]) / 3;
}
float f_left[8];
float f_right[8];
getModelStatistics(pBuffer, length, left3, f_left);
getModelStatistics(pBuffer, length, right3, f_right);
for (uint32 i = 0; i < 8; i++) {
if ((right3[i] - left3[i]) == 1) {
if (f_left[i] < f_right[i]) {
pIncrements[i] = left3[i];
} else {
pIncrements[i] = right3[i];
}
not_done &= ~(1 << i);
}
else {
if (f_left[i] < f_right[i]) {
right[i] = right3[i];
} else {
left[i] = left3[i];
}
}
}
iter++;
}
// printf("num iterations: %d\n", iter);
}
//----------------------------------------------------------------------------//