-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilters.cpp
490 lines (451 loc) · 13 KB
/
filters.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*********************************************
* Embedded delta and linear prediction filters
* These are blockwise adaptive transforms which lower the entropy of complex semi-static structures
*
* Definitions of compression arguments:
* -f0 : no filter, do not preprocess.
* -f1 : heuristic filter, detect the width of channels and pick whatever transform is the best, if any.
* -f2 : brute force, apply all possible transformations and pick the best one.
*
* There are 3 filter types:
* 1) delta : standard reorder of channels with delta encoder.
* 2) linear prediction : standard reorder of channels with linear prediction encoder.
* 3) inline delta : no reordering, deltas are encoded in place by a table. (context preserving, simd friendly)
*
* Each filter can encode a width of 1 to 32, width 0 means raw.
* In total there are 96 filters + raw configuration, in brute force mode it tries all filters and picks the best, in heuristic mode
* it checks for repeated symbols at pos%32 in O(n) time via a stride histogram with sorted entropy calculation (generalized bwt).
**********************************************/
#include "filters.hpp"
void Filters::DeltaEncode(unsigned char *in, int len)
{
unsigned char previous = 0, cur = 0;
for(int i = 0; i < len; i++)
{
cur = in[i];
in[i] = cur - previous;
previous = cur;
}
}
void Filters::DeltaDecode(unsigned char *in, int len)
{
unsigned char previous = 0;
for(int i = 0; i < len; i++)
{
previous = in[i] += previous;
}
}
void Filters::UpdateWeight(unsigned char err, int *weight)
{
const int upper = 0xff;
const int rate = 6;
*weight += (err - *weight) >> rate;
assert(*weight >= -upper && *weight <= upper);
}
void Filters::LpcEncode(unsigned char *in, int len)
{
int weight = 0;
unsigned char p1 = 0;
unsigned char p2 = 0;
unsigned char cur = 0;
unsigned char err = 0;
for(int i = 0; i < len; i++)
{
cur = in[i];
err = weight + (((p1 - p2) + p1) - cur);
in[i] = err;
UpdateWeight(err, &weight);
p2 = p1;
p1 = cur;
}
}
void Filters::LpcDecode(unsigned char *in, int len)
{
int weight = 0;
unsigned char p1 = 0;
unsigned char p2 = 0;
unsigned char cur = 0;
unsigned char err = 0;
for(int i = 0; i < len; i++)
{
err = in[i];
cur = weight + (((p1 - p2) + p1) - err);
in[i] = cur;
UpdateWeight(err, &weight);
p2 = p1;
p1 = cur;
}
}
void Filters::Reorder(unsigned char *in, unsigned char *out, int width, int len)
{
Index i, j, pos = 0;
for(i = 0; i < width; i++)
for(j = i; j < len; j += width)
out[pos++] = in[j];
}
void Filters::Unreorder(unsigned char *in, unsigned char *out, int width, int len)
{
Index i, j, pos = 0;
for(i = 0; i < width; i++)
for(j = i; j < len; j += width)
out[j] = in[pos++];
}
void Filters::InlineDelta(unsigned char *in, unsigned char *out, int width, int len)
{
unsigned char *p = (unsigned char*)calloc(MAX_CHANNEL_WIDTH, sizeof(unsigned char));
Index i = 0;
Index align = len % width;
for(; i < align; i++)
out[i] = in[i];
while(i < len) // process a bunch of deltas at once without reorder (cache friendly, simd optimizable, context preserving)
{
for(Index j = 0; j < width; j++)
{
out[i + j] = in[i + j] - p[j];
p[j] = in[i + j];
}
i += width;
}
free(p);
}
void Filters::InlineUndelta(unsigned char *in, unsigned char *out, int width, int len)
{
unsigned char *p = (unsigned char*)calloc(MAX_CHANNEL_WIDTH, sizeof(unsigned char));
Index i = 0;
Index align = len % width;
for(; i < align; i++)
out[i] = in[i];
while(i < len) // process a bunch of deltas at once without reorder
{
for(Index j = 0; j < width; j++)
{
out[i + j] = in[i + j] + p[j];
p[j] = out[i + j];
}
i += width;
}
free(p);
}
/**
* Detect the width of a standard delta channel
*/
int Filters::FindStride(unsigned char *in, int len)
{
Index stride = 0;
Index dist[256] = {0};
Index *hist = (Index*)calloc((MAX_CHANNEL_WIDTH + 1), sizeof(Index));
unsigned char sym = 0;
for(int i = 0; i < len; i++)
{
sym = in[i];
stride = i - dist[sym];
dist[sym] = i;
hist[stride % (MAX_CHANNEL_WIDTH + 1)]++;
}
Index average = 0;
for (int j = 0; j <= MAX_CHANNEL_WIDTH; j++)
average += hist[j];
average /= (MAX_CHANNEL_WIDTH + 1);
Index smallest = 0;
double min = hist[0];
for (int j = 1; j <= MAX_CHANNEL_WIDTH; j++)
{
if (hist[j] > (average * 2) && hist[j] > min)
{
min = hist[j];
smallest = j;
}
}
free(hist);
return smallest;
}
/**
* Detect the width of a linear prediction channel
*/
int Filters::FindProjection(unsigned char *in, int len)
{
Index stride = 0;
Index projection = 0;
Index dist0[256] = {0};
Index dist1[256] = {0};
Index *hist = (Index*)calloc((MAX_CHANNEL_WIDTH + 1), sizeof(Index));
unsigned char sym = 0;
for(int i = 0; i < len; i++)
{
sym = in[i];
stride = i - dist0[sym];
projection = i - dist1[stride % 256];
dist1[stride % 256] = i;
dist0[sym] = i;
hist[projection % (MAX_CHANNEL_WIDTH + 1)]++;
}
Index average = 0;
for (int j = 0; j <= MAX_CHANNEL_WIDTH; j++)
average += hist[j];
average /= (MAX_CHANNEL_WIDTH + 1);
double min = hist[0];
Index smallest = 0;
for (int j = 1; j <= MAX_CHANNEL_WIDTH; j++)
{
if (hist[j] > (average * 2) && hist[j] > min)
{
min = hist[j];
smallest = j;
}
}
free(hist);
return smallest;
}
/**
* Structural modelling, detect deltas and fixed points within the input and encode it.
*/
void Filters::Encode(Buffer Input, Buffer Output, Options Opt)
{
if(Opt.Filters < 0)
Opt.Filters = 0;
if(Opt.Filters > 2)
Opt.Filters = 2;
Utils *eCalc = new Utils;
double *eScores[3];
eScores[0] = (double*)malloc(MAX_CHANNEL_WIDTH * sizeof(double)); // Standard delta
eScores[1] = (double*)malloc(MAX_CHANNEL_WIDTH * sizeof(double)); // Linear prediction
eScores[2] = (double*)malloc(MAX_CHANNEL_WIDTH * sizeof(double)); // Inline delta
if(eScores[0] == NULL || eScores[1] == NULL || eScores[2] == NULL)
Error("Failed to allocate filter prediction buffer!");
// Read all of the input
Index Outp = 0;
int DeltaBlocks = 0, RawBlocks = 0;
int PrevType = 0, PrevWidth = 0;
for(Index i = 0; i < *Input.size;)
{
// Split input into chunks
int len = ((i + FILTER_BLOCK_SIZE) < *Input.size) ? FILTER_BLOCK_SIZE : (*Input.size - i);
int boostread = len;
for(int k = 0; k <= MAX_CHANNEL_WIDTH; k++)
{
eScores[0][k] = 8.0f;
eScores[1][k] = 8.0f;
eScores[2][k] = 8.0f;
}
if(Opt.Filters == 2)
{
// Brute force standard delta, linear prediction, and raw entropy calculation
#pragma omp parallel for num_threads(Opt.Threads)
for(int Ch = 0; Ch <= MAX_CHANNEL_WIDTH; Ch++)
{
unsigned char *dbuf = (unsigned char*)malloc(FILTER_BLOCK_SIZE * sizeof(unsigned char));
unsigned char *lbuf = (unsigned char*)malloc(FILTER_BLOCK_SIZE * sizeof(unsigned char));
unsigned char *ibuf = (unsigned char*)malloc(FILTER_BLOCK_SIZE * sizeof(unsigned char));
if(dbuf == NULL || lbuf == NULL || ibuf == NULL)
Error("Failed to allocate filter prediction buffers!");
if(Ch > 0)
{
Reorder(&Input.block[i], dbuf, Ch, boostread);
memcpy(lbuf, dbuf, boostread);
DeltaEncode(dbuf, boostread);
LpcEncode(lbuf, boostread);
InlineDelta(&Input.block[i], ibuf, Ch, boostread);
eScores[0][Ch] = eCalc->CalculateMixedEntropy(dbuf, boostread);
eScores[1][Ch] = eCalc->CalculateMixedEntropy(lbuf, boostread);
eScores[2][Ch] = eCalc->CalculateMixedEntropy(ibuf, boostread);
}
else
{
eScores[0][Ch] = eCalc->CalculateMixedEntropy(&Input.block[i], boostread);
}
free(dbuf);
free(lbuf);
free(ibuf);
}
}
if(Opt.Filters == 1)
{
double pconfig;
#pragma omp parallel sections num_threads(Opt.Threads)
{
// Compute raw entropy
#pragma omp section
{
eScores[0][0] = eCalc->CalculateSortedEntropy(&Input.block[i], boostread);
}
// Compute delta entropy
#pragma omp section
{
unsigned char *dbuf = (unsigned char*)malloc(FILTER_BLOCK_SIZE * sizeof(unsigned char));
if(dbuf == NULL)
Error("Failed to allocate filter prediction buffer!");
int Ch = FindStride(&Input.block[i], len);
if(Ch > 0)
{
Reorder(&Input.block[i], dbuf, Ch, boostread);
DeltaEncode(dbuf, boostread);
eScores[0][Ch] = eCalc->CalculateSortedEntropy(dbuf, boostread);
}
free(dbuf);
}
// Compute linear prediction entropy
#pragma omp section
{
unsigned char *lbuf = (unsigned char*)malloc(FILTER_BLOCK_SIZE * sizeof(unsigned char));
if(lbuf == NULL)
Error("Failed to allocate filter prediction buffer!");
int Ch = FindProjection(&Input.block[i], len);
if(Ch > 0)
{
Reorder(&Input.block[i], lbuf, Ch, boostread);
LpcEncode(lbuf, boostread);
eScores[1][Ch] = eCalc->CalculateSortedEntropy(lbuf, boostread);
}
free(lbuf);
}
// Compute inline delta entropy
#pragma omp section
{
unsigned char *ibuf = (unsigned char*)malloc(FILTER_BLOCK_SIZE * sizeof(unsigned char));
if(ibuf == NULL)
Error("Failed to allocate filter prediction buffer!");
int Ch = FindStride(&Input.block[i], len);
if(Ch > 0)
{
InlineDelta(&Input.block[i], ibuf, Ch, boostread);
eScores[2][Ch] = eCalc->CalculateSortedEntropy(ibuf, boostread);
}
free(ibuf);
}
// Compute entropy of previous block configuration on current block
// Due to the previous state not being taken into account by other threads this can cause scheduling issues when writing the entropy score if it collides with another, 'pconfig' fixes that.
#pragma omp section
{
unsigned char *pbuf = (unsigned char*)malloc(FILTER_BLOCK_SIZE * sizeof(unsigned char));
if(pbuf == NULL)
Error("Failed to allocate filter prediction buffer!");
if(PrevWidth > 0)
{
Reorder(&Input.block[i], pbuf, PrevWidth, boostread);
}
if(PrevType)
{
LpcEncode(pbuf, boostread);
}
else
{
DeltaEncode(pbuf, boostread);
}
pconfig = eCalc->CalculateSortedEntropy(pbuf, boostread);
free(pbuf);
}
}
if(eScores[PrevType][PrevWidth] == 8.0f)
eScores[PrevType][PrevWidth] = pconfig;
}
// Find the best filter configuration (channel width and type)
unsigned char smallest = 0;
unsigned char type = 0;
double min = eScores[0][smallest];
for(int k = 0; k < MAX_SUPPORTED_CONFIGURATION; k++)
{
for (int j = 1; j <= MAX_CHANNEL_WIDTH; j++)
{
if (eScores[k][j] < min)
{
min = eScores[k][j];
smallest = j;
type = k;
}
}
}
//printf("Using filter %i with a width of %i has entropy %.2f\n", type, smallest, min);
unsigned char *buf = (unsigned char*)malloc(FILTER_BLOCK_SIZE * sizeof(unsigned char));
if(buf == NULL)
Error("Failed to allocate filter prediction buffer!");
if(type >= MAX_SUPPORTED_CONFIGURATION || smallest > MAX_CHANNEL_WIDTH)
Error("Filter is trying to encode from an unsupported configuration!");
if(smallest > 0)
{
switch(type)
{
case 0 : // Delta
{
Reorder(&Input.block[i], buf, smallest, len);
DeltaEncode(buf, len);
} break;
case 1 : // LPC
{
Reorder(&Input.block[i], buf, smallest, len);
LpcEncode(buf, len);
} break;
case 2 : // Inline Delta
{
InlineDelta(&Input.block[i], buf, smallest, len);
} break;
}
Output.block[Outp++] = type;
Output.block[Outp++] = smallest;
memcpy(&Output.block[Outp], buf, len * sizeof(unsigned char));
DeltaBlocks++;
}
else
{
Output.block[Outp++] = 0;
Output.block[Outp++] = 0;
memcpy(&Output.block[Outp], &Input.block[i], len * sizeof(unsigned char));
RawBlocks++;
}
free(buf);
PrevType = type;
PrevWidth = smallest;
Outp += len;
i += len;
}
//printf("\ndelta blocks: %i, raw blocks: %i\n", DeltaBlocks, RawBlocks);
delete eCalc;
free(eScores[0]);
free(eScores[1]);
free(eScores[2]);
*Output.size = Outp;
}
void Filters::Decode(Buffer Input, Buffer Output)
{
unsigned char *dbuf = (unsigned char*)malloc(FILTER_BLOCK_SIZE * sizeof(unsigned char));
if(dbuf == NULL)
Error("Failed to allocate filter prediction buffer!");
Index Outp = 0;
for(Index i = 0; i < *Input.size;)
{
unsigned char type = Input.block[i++];
unsigned char channel = Input.block[i++];
if(type >= MAX_SUPPORTED_CONFIGURATION || channel > MAX_CHANNEL_WIDTH)
Error("Filter is trying to decode from unsupported configuration!");
int len = ((i + FILTER_BLOCK_SIZE) < *Input.size) ? FILTER_BLOCK_SIZE : (*Input.size - i);
if(channel > 0)
{
switch(type)
{
case 0 : // Delta
{
memcpy(dbuf, &Input.block[i], len * sizeof(unsigned char));
DeltaDecode(dbuf, len);
Unreorder(dbuf, &Output.block[Outp], channel, len);
} break;
case 1 : // LPC
{
memcpy(dbuf, &Input.block[i], len * sizeof(unsigned char));
LpcDecode(dbuf, len);
Unreorder(dbuf, &Output.block[Outp], channel, len);
} break;
case 2 : // Inline Delta
{
InlineUndelta(&Input.block[i], &Output.block[Outp], channel, len);
} break;
}
}
else
{
memcpy(&Output.block[Outp], &Input.block[i], len * sizeof(unsigned char));
}
Outp += len;
i += len;
}
free(dbuf);
*Output.size = Outp;
}