-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepack_lite.c
361 lines (312 loc) · 6.69 KB
/
prepack_lite.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
Fast general purpose data preprocessor
The MIT License (MIT)
Copyright (c) 2016 Lucas Marsh
How it works:
encoding:
pass 1: skims over input file to get a rough idea of how many channels there are
pass 2: encodes a 1 byte header with the amount of channels then encodes the entire file
decoding is a single pass that un-interleaves the data with n channels.
encode method is either delta (best for image) or adaptive LPC with a single weight (best for audio)
*/
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#define blocksize 24576
#define boost 24
#define totalChannels 15
#define breakpoint 10
typedef uint8_t byte;
byte indexToChannel[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 6, 8}; // 0-9 Delta, 10-15 Adaptive
int weight = 0; // weight [-1, 1]
int rate = 6; // learning rate for filter
char buffer[blocksize];
/// delta ///
byte previous[8]; // up to 8 channels for delta
byte deltaEnc(byte b, int i)
{
byte delta = previous[i] - b;
previous[i] = b;
return delta;
}
byte deltaDec(byte delta, int i)
{
byte b = previous[i] - delta;
previous[i] = b;
return b;
}
/// adaptive LPC ///
byte previousByte[8]; // sample 1
byte secondPreviousByte[8]; // sample 2
void updateWeight(byte err)
{
if (err < 127) weight++;
if (err > 127) weight--;
if (weight == 1281) weight--;
if (weight == -1281) weight++;
}
byte adaptiveDeltaEnc(byte b, int i)
{
// find error of prediction
byte prediction = (previousByte[i] - secondPreviousByte[i]) + previousByte[i];
int w = (weight >> rate);
byte error = w + (prediction - b);
// update variables
updateWeight(error);
secondPreviousByte[i] = previousByte[i];
previousByte[i] = w + b; // store a refined representation of history (slightly improves compression)
return error;
}
byte adaptiveDeltaDec(byte error, int i)
{
// find error of prediction
byte prediction = (previousByte[i] - secondPreviousByte[i]) + previousByte[i];
int w = (weight >> rate);
byte b = w + (prediction - error);
// update variables
updateWeight(error);
secondPreviousByte[i] = previousByte[i];
previousByte[i] = w + b;
return b;
}
/// synthetic modulo ///
int d = 0;
int modulo(int max)
{
d++;
if(d == max) d = 0;
return d;
}
void resetModulo()
{
d = 0;
}
/// entropy calc ///
long freq[totalChannels][256];
double fileLength;
void count(byte current, int channel)
{
freq[channel][current]++;
}
double calcEntropy(int channel)
{
double entropy = 0;
for (int i = 0; i < 256; i++)
{
double charProbability = freq[channel][i] / fileLength;
if (charProbability != 0)
{
entropy += (charProbability)*(-log(charProbability) / log(2));
}
}
return entropy;
}
/// find best encode method ///
int findSmallestChannel()
{
double chanEnt[totalChannels];
for (int i = 0; i < totalChannels; i++)
{
chanEnt[i] = calcEntropy(i);
}
/// find smallest entropy ///
int index = 0;
double min = chanEnt[index];
for (int i = 1; i < totalChannels; i++)
{
if (chanEnt[i] < min)
{
min = chanEnt[i];
index = i;
}
}
return index;
}
/// scan for best encode method //
int scan(FILE* in)
{
int read = 0;
/// gather some file info ///
fseek(in, 0, SEEK_END);
fileLength = ftell(in);
fseek(in, 0, SEEK_SET);
int eof = fileLength;
/// scan input ///
while (feof(in) == 0)
{
read = fread(&buffer, 1, blocksize, in);
/// test all encoding methods ///
for (int index = 0; index < totalChannels; index++)
{
int channel = indexToChannel[index];
for (int i = 0; i < read; i++)
{
if (channel == 0)
{
count(buffer[i], index);
}
else if (index < 7)
{
int mod = modulo(channel);
count(deltaEnc(buffer[i], mod), index);
}
else if(index >= 7)
{
int mod = modulo(channel);
count(adaptiveDeltaEnc(buffer[i], mod), index);
}
}
resetModulo();
}
// if there's room to stride, stride
if ((ftell(in) + (blocksize*boost)) < eof)
{
fseek(in, (blocksize*boost), SEEK_CUR);
}
}
int channel = findSmallestChannel();
// reset variables
for (int j = 0; j < 8; j++)
{
previous[j] = 0;
previousByte[j] = 0;
secondPreviousByte[j] = 0;
weight = 0;
}
return channel;
}
void encode(int channel, FILE* in, FILE* out)
{
int read = 0;
if(channel < breakpoint)
printf("\nencoding channel %i standard\n", indexToChannel[channel]);
else
printf("\nencoding channel %i adaptive\n", indexToChannel[channel]);
/// write header ///
putc(channel, out);
rewind(in);
/// encode ///
int ch = indexToChannel[channel];
while (feof(in) == 0)
{
read = fread(buffer, 1, blocksize, in);
// encode
if(channel < breakpoint)
{
if (ch != 0)
{
for (int i = 0; i < read; i++)
{
int mod = modulo(ch);
buffer[i] = deltaEnc(buffer[i], mod);
}
}
}
else
{
if (ch != 0)
{
for (int i = 0; i < read; i++)
{
int mod = modulo(ch);
buffer[i] = adaptiveDeltaEnc(buffer[i], mod);
}
}
}
fwrite(buffer, 1, read, out);
}
}
void decode(FILE* in, FILE* out)
{
int channel = getc(in);
if(channel < breakpoint)
printf("\ndecoding channel %i standard\n", indexToChannel[channel]);
else
printf("\ndecoding channel %i adaptive\n", indexToChannel[channel]);
// set variables //
for (int j = 0; j < 8; j++)
{
previous[j] = 0;
previousByte[j] = 0;
secondPreviousByte[j] = 0;
}
weight = 0;
int read = 0;
int ch = indexToChannel[channel];
while (feof(in) == 0)
{
read = fread(buffer, 1, blocksize, in);
// decode //
if(channel < breakpoint)
{
if (ch != 0)
{
for (int i = 0; i < read; i++)
{
int mod = modulo(ch);
buffer[i] = deltaDec(buffer[i], mod);
}
}
}
else
{
if (ch != 0)
{
for (int i = 0; i < read; i++)
{
int mod = modulo(ch);
buffer[i] = adaptiveDeltaDec(buffer[i], mod);
}
}
}
fwrite(buffer, 1, read, out);
}
}
/// main ///
int main(int argc, char* argv[])
{
time_t start, end;
start = clock();
// Check arguments //
if(argc != 4)
{
printf("usage: prepack_light.exe e/d in out\n");
return 1;
}
else
{
// Check input and output //
FILE* in, *out;
if((in=fopen(argv[2],"rb"))==NULL)
{
printf("no input!\n");
return 2;
}
if((out=fopen(argv[3],"wb"))==NULL)
{
printf("no output!\n");
return 3;
}
if(argv[1][0]=='e')
{
char method = scan(in);
encode(method, in, out);
}
else if(argv[1][0]=='d')
{
decode(in, out);
}
else
{
printf("unknown argument!\n");
return 4;
}
fclose(in);
fclose(out);
}
end = clock();
printf("took %li seconds\n", (end - start) / CLOCKS_PER_SEC);
return 0;
}