forked from deus-libri/preflate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreflate_tree_predictor.cpp
646 lines (607 loc) · 22 KB
/
preflate_tree_predictor.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/* Copyright 2018 Dirk Steinke
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. */
#include <algorithm>
#include <string.h>
#include "preflate_constants.h"
#include "preflate_statistical_codec.h"
#include "preflate_statistical_model.h"
#include "preflate_tree_predictor.h"
PreflateTreePredictor::PreflateTreePredictor(
const std::vector<unsigned char>& dump,
const size_t off)
: input(dump)
, predictionFailure(false) {
input.advance(off);
}
struct FreqIdxPair {
unsigned freq;
unsigned idx;
};
struct TreeNode {
unsigned parent;
unsigned idx;
};
/* ===========================================================================
* Compares to subtrees, using the tree depth as tie breaker when
* the subtrees have equal frequency. This minimizes the worst case length.
*/
bool pq_smaller(const FreqIdxPair& p1, const FreqIdxPair& p2, const unsigned char* nodeDepth) {
return p1.freq < p2.freq || (p1.freq == p2.freq && nodeDepth[p1.idx] <= nodeDepth[p2.idx]);
}
/* ===========================================================================
* Restore the heap property by moving down the tree starting at node k,
* exchanging a node with the smallest of its two sons if necessary, stopping
* when the heap property is re-established (each father smaller than its
* two sons).
*/
void pq_downheap(FreqIdxPair* ptr, const unsigned index, const unsigned len, const unsigned char* depth) {
unsigned k = index;
FreqIdxPair v = ptr[k];
unsigned j = k * 2 + 1; /* left son of k */
while (j < len) {
/* Set j to the smallest of the two sons: */
if (j + 1 < len && pq_smaller(ptr[j + 1], ptr[j], depth)) {
j++;
}
/* Exit if v is smaller than both sons */
if (pq_smaller(v, ptr[j], depth)) break;
/* Exchange v with the smallest son */
ptr[k] = ptr[j];
k = j;
/* And continue down the tree, setting j to the left son of k */
j = k * 2 + 1;
}
ptr[k] = v;
}
void pq_makeheap(FreqIdxPair* ptr, const unsigned len, const unsigned char* depth) {
for (unsigned n = (len - 1) / 2 + 1; n > 0; n--) {
pq_downheap(ptr, n - 1, len, depth);
}
}
FreqIdxPair pq_remove(FreqIdxPair* ptr, unsigned& len, const unsigned char* depth) {
FreqIdxPair result = ptr[0];
ptr[0] = ptr[--len];
pq_downheap(ptr, 0, len, depth);
return result;
}
unsigned PreflateTreePredictor::calcBitLengths(
unsigned char* symBitLen,
const unsigned* symFreq,
const unsigned symCount,
const unsigned maxBits,
const unsigned minMaxCode) {
FreqIdxPair toSort[PreflateConstants::LITLEN_CODE_COUNT];
TreeNode nodes[PreflateConstants::LITLEN_CODE_COUNT * 2 + 1];
unsigned char nodeBitLen[PreflateConstants::LITLEN_CODE_COUNT * 2 + 1];
unsigned char nodeDepth[PreflateConstants::LITLEN_CODE_COUNT * 2 + 1];
memset(nodeBitLen, 0, sizeof(nodeBitLen));
memset(nodeDepth, 0, sizeof(nodeDepth));
unsigned maxCode = 0, len = 0, nodeCount = 0, nodeId = symCount;
for (unsigned i = 0; i < symCount; ++i) {
if (symFreq[i]) {
toSort[len++] = FreqIdxPair {symFreq[i], maxCode = i};
}
}
if (len < 2) {
memset(symBitLen, 0, symCount);
symBitLen[maxCode] = 1;
symBitLen[maxCode < 2 ? ++maxCode : 0] = 1;
return std::max(minMaxCode, maxCode + 1);
}
pq_makeheap(toSort, len, nodeDepth);
while (len > 1) {
FreqIdxPair least1 = pq_remove(toSort, len, nodeDepth);
FreqIdxPair least2 = toSort[0];
toSort[0] = FreqIdxPair {least1.freq + least2.freq, nodeId};
nodes[nodeCount++] = TreeNode {nodeId, least1.idx};
nodes[nodeCount++] = TreeNode {nodeId, least2.idx};
nodeDepth[nodeId] = std::max(nodeDepth[least1.idx], nodeDepth[least2.idx]) + 1;
// note? original code put new entry at top of heap, and moved it downwards
// while push_heap pushes it upwards
pq_downheap(toSort, 0, len, nodeDepth);
nodeId++;
}
unsigned overflow = 0;
unsigned bl_count[16];
memset(bl_count, 0, sizeof(bl_count));
unsigned orgNodeCount = nodeCount;
while (nodeCount-- > 0) {
unsigned char newLen = nodeBitLen[nodes[nodeCount].parent] + 1;
if (newLen > maxBits) {
newLen = maxBits;
++overflow;
}
unsigned idx = nodes[nodeCount].idx;
nodeBitLen[idx] = newLen;
if (idx < symCount) {
bl_count[newLen]++;
}
}
if (overflow) {
unsigned bits;
do {
for (bits = maxBits - 1; bl_count[bits] == 0; bits--) {
}
bl_count[bits]--; /* move one leaf down the tree */
bl_count[bits + 1] += 2; /* move one overflow item as its brother */
bl_count[maxBits]--;
/* The brother of the overflow item also moves one step up,
* but this does not affect bl_count[max_length]
*/
overflow -= 2;
} while (overflow > 0);
for (bits = maxBits, nodeCount = orgNodeCount; nodeCount > 0; ) {
--nodeCount;
unsigned idx = nodes[nodeCount].idx;
if (idx >= symCount) {
continue;
}
while (bl_count[bits] == 0) {
bits--;
}
nodeBitLen[idx] = bits;
bl_count[bits]--;
}
}
memcpy(symBitLen, nodeBitLen, symCount);
return std::max(minMaxCode, maxCode + 1);
}
TreeCodeType PreflateTreePredictor::predictCodeType(const unsigned char* symBitLen,
const unsigned symCount,
const bool first) {
unsigned char code = symBitLen[0];
if (code == 0) {
unsigned char curlen = 1;
unsigned char maxCurLen = std::min(symCount, 11u);
while (curlen < maxCurLen && symBitLen[curlen] == 0) {
++curlen;
}
if (curlen >= 11) {
return TCT_REPZL;
}
if (curlen >= 3) {
return TCT_REPZS;
}
return TCT_BITS;
}
if (!first && code == symBitLen[-1]) {
unsigned char curlen = 1;
unsigned char maxCurLen = std::min(symCount, 3u);
while (curlen < maxCurLen && symBitLen[curlen] == code) {
++curlen;
}
if (curlen >= 3) {
return TCT_REP;
}
}
return TCT_BITS;
}
unsigned char PreflateTreePredictor::predictCodeData(const unsigned char* symBitLen,
const TreeCodeType type,
const unsigned symCount,
const bool first) {
unsigned char code = symBitLen[0];
switch (type) {
default:
case TCT_BITS:
return code;
case TCT_REP:
{
unsigned char curlen = 3;
unsigned char maxCurLen = std::min(symCount, 6u);
while (curlen < maxCurLen && symBitLen[curlen] == code) {
++curlen;
}
return curlen;
}
case TCT_REPZS:
case TCT_REPZL:
{
unsigned char curlen = type == TCT_REPZS ? 3 : 11;
unsigned char maxCurLen = std::min(symCount, type == TCT_REPZS ? 10u : 138u);
while (curlen < maxCurLen && symBitLen[curlen] == 0) {
++curlen;
}
return curlen;
}
}
}
void PreflateTreePredictor::predictLDTrees(
BlockAnalysisResult& analysis,
unsigned* frequencies,
const unsigned char* symBitLen,
const unsigned symLCount,
const unsigned symDCount,
const unsigned char* targetCodes,
const unsigned targetCodeSize) {
memset(frequencies, 0, sizeof(unsigned) * PreflateConstants::CODETREE_CODE_COUNT);
const unsigned char* ptr = symBitLen;
const unsigned char* code = targetCodes;
unsigned codeSize = targetCodeSize;
unsigned count1 = symLCount;
unsigned count2 = symDCount;
bool first = true;
while (codeSize > 0) {
TreeCodeType targetTreeCodeType;
switch (code[0]) {
case 16: targetTreeCodeType = TCT_REP; break;
case 17: targetTreeCodeType = TCT_REPZS; break;
case 18: targetTreeCodeType = TCT_REPZL; break;
default: targetTreeCodeType = TCT_BITS; break;
}
if (codeSize < 2 && targetTreeCodeType != TCT_BITS) {
predictionFailure = true;
return;
}
TreeCodeType predictedTreeCodeType = predictCodeType(ptr, count1, first);
unsigned char info = predictedTreeCodeType | ((targetTreeCodeType != predictedTreeCodeType) << 2);
if (targetTreeCodeType != predictedTreeCodeType) {
analysis.correctives.push_back(targetTreeCodeType);
}
unsigned char targetTreeCodeData = code[targetTreeCodeType != TCT_BITS];
unsigned l = 1 + (targetTreeCodeType != TCT_BITS);
code += l;
codeSize -= l;
unsigned char predictedTreeCodeData = predictCodeData(ptr, targetTreeCodeType, count1, first);
first = false;
if (targetTreeCodeType != TCT_BITS) {
analysis.correctives.push_back(predictedTreeCodeData);
if (targetTreeCodeData != predictedTreeCodeData) {
info |= 8;
analysis.correctives.push_back(targetTreeCodeData);
}
} else {
analysis.correctives.push_back(predictedTreeCodeData);
analysis.correctives.push_back(targetTreeCodeData - predictedTreeCodeData);
}
if (targetTreeCodeType != TCT_BITS) {
frequencies[targetTreeCodeType + 15]++;
l = targetTreeCodeData;
} else {
frequencies[targetTreeCodeData]++;
l = 1;
}
ptr += l;
if (count1 > l) {
count1 -= l;
} else {
count1 += count2;
count2 = 0;
first = true;
if (count1 >= l) {
count1 -= l;
} else {
predictionFailure = true;
return;
}
}
analysis.tokenInfo.push_back(info);
}
analysis.tokenInfo.push_back(0xff);
if (count1 + count2 != 0) {
predictionFailure = true;
}
}
void PreflateTreePredictor::collectTokenStatistics(
unsigned Lcodes[],
unsigned Dcodes[],
unsigned& Lcount,
unsigned& Dcount,
const PreflateTokenBlock& block) {
memset(Lcodes, 0, sizeof(unsigned) * PreflateConstants::LITLEN_CODE_COUNT);
memset(Dcodes, 0, sizeof(unsigned) * PreflateConstants::DIST_CODE_COUNT);
Lcount = 0;
Dcount = 0;
for (unsigned i = 0, n = block.tokens.size(); i < n; ++i) {
PreflateToken targetToken = block.tokens[i];
if (targetToken.len == 1) {
Lcodes[input.curChar()]++;
Lcount++;
input.advance(1);
} else {
Lcodes[PreflateConstants::NONLEN_CODE_COUNT + PreflateConstants::LCode(targetToken.len)]++;
Lcount++;
Dcodes[PreflateConstants::DCode(targetToken.dist)]++;
Dcount++;
input.advance(targetToken.len);
}
}
Lcodes[256] = 1;
}
unsigned PreflateTreePredictor::buildLBitlenghs(
unsigned char bitLengths[],
unsigned Lcodes[]) {
return calcBitLengths(bitLengths, Lcodes, PreflateConstants::LITLEN_CODE_COUNT, 15, PreflateConstants::NONLEN_CODE_COUNT);
}
unsigned PreflateTreePredictor::buildDBitlenghs(
unsigned char bitLengths[],
unsigned Dcodes[]) {
return calcBitLengths(bitLengths, Dcodes, PreflateConstants::DIST_CODE_COUNT, 15, 0);
}
unsigned PreflateTreePredictor::buildTCBitlengths(
unsigned char (&simpleCodeTree)[PreflateConstants::CODETREE_CODE_COUNT],
unsigned (&BLfreqs)[PreflateConstants::CODETREE_CODE_COUNT]) {
memset(simpleCodeTree, 0, sizeof(simpleCodeTree));
calcBitLengths(simpleCodeTree, BLfreqs, PreflateConstants::CODETREE_CODE_COUNT, 7, 0);
unsigned predictedCTreeSize = PreflateConstants::CODETREE_CODE_COUNT;
while (predictedCTreeSize > 4
&& simpleCodeTree[PreflateConstants::treeCodeOrderTable[predictedCTreeSize - 1]] == 0) {
--predictedCTreeSize;
}
return predictedCTreeSize;
}
void PreflateTreePredictor::analyzeBlock(
const unsigned blockno,
const PreflateTokenBlock& block) {
if (blockno != analysisResults.size() || predictionFailure) {
return;
}
analysisResults.push_back(BlockAnalysisResult());
BlockAnalysisResult& analysis = analysisResults[blockno];
analysis.blockType = block.type;
if (analysis.blockType != PreflateTokenBlock::DYNAMIC_HUFF) {
return;
}
unsigned Lcodes[PreflateConstants::LITLEN_CODE_COUNT], Dcodes[PreflateConstants::DIST_CODE_COUNT];
unsigned Lcount = 0, Dcount = 0;
collectTokenStatistics(Lcodes, Dcodes, Lcount, Dcount, block);
unsigned char bitLengths[PreflateConstants::LITLENDIST_CODE_COUNT];
memset(bitLengths, 0, sizeof(bitLengths));
unsigned predictedLTreeSize = buildLBitlenghs(bitLengths, Lcodes);
analysis.tokenInfo.push_back(predictedLTreeSize != block.nlen);
if (predictedLTreeSize != block.nlen) {
analysis.correctives.push_back(block.nlen);
}
predictedLTreeSize = block.nlen;
unsigned predictedDTreeSize = buildDBitlenghs(bitLengths + predictedLTreeSize, Dcodes);
analysis.tokenInfo.push_back(predictedDTreeSize != block.ndist);
if (predictedDTreeSize != block.ndist) {
analysis.correctives.push_back(block.ndist);
}
predictedDTreeSize = block.ndist;
unsigned BLfreqs[PreflateConstants::CODETREE_CODE_COUNT];
const unsigned char* targetCodes = &block.treecodes[0];
unsigned targetCodeSize = block.treecodes.size();
predictLDTrees(analysis, BLfreqs, bitLengths, predictedLTreeSize, predictedDTreeSize, targetCodes + block.ncode, targetCodeSize - block.ncode);
unsigned char simpleCodeTree[PreflateConstants::CODETREE_CODE_COUNT];
unsigned predictedCTreeSize = buildTCBitlengths(simpleCodeTree, BLfreqs);
analysis.tokenInfo.push_back(block.ncode);
analysis.tokenInfo.push_back(predictedCTreeSize != block.ncode);
predictedCTreeSize = block.ncode;
for (unsigned i = 0; i < predictedCTreeSize; ++i) {
unsigned predictedBL = simpleCodeTree[PreflateConstants::treeCodeOrderTable[i]];
analysis.correctives.push_back(predictedBL);
analysis.correctives.push_back(targetCodes[i] - predictedBL);
}
}
void PreflateTreePredictor::encodeBlock(
PreflatePredictionEncoder* codec,
const unsigned blockno) {
BlockAnalysisResult& analysis = analysisResults[blockno];
if (analysis.blockType != PreflateTokenBlock::DYNAMIC_HUFF) {
return;
}
unsigned infoPos = 0, correctivePos = 0;
unsigned char info = analysis.tokenInfo[infoPos++];
codec->encodeLiteralCountMisprediction(info);
if (info) {
codec->encodeValue(analysis.correctives[correctivePos++] - PreflateConstants::NONLEN_CODE_COUNT, 5);
}
info = analysis.tokenInfo[infoPos++];
codec->encodeDistanceCountMisprediction(info);
if (info) {
codec->encodeValue(analysis.correctives[correctivePos++], 5);
}
while ((info = analysis.tokenInfo[infoPos++]) != 0xff) {
unsigned type = (info & 3);
if (info & 4) {
unsigned newType = analysis.correctives[correctivePos++];
codec->encodeLDTypeCorrection(type, newType);
type = newType;
} else {
codec->encodeLDTypeCorrection(type, type);
}
if (type != TCT_BITS) {
unsigned predRepeat = analysis.correctives[correctivePos++];
if (info & 8) {
unsigned newRepeat = analysis.correctives[correctivePos++];
codec->encodeRepeatCountCorrection(predRepeat, newRepeat, type);
} else {
codec->encodeRepeatCountCorrection(predRepeat, predRepeat, type);
}
} else {
unsigned bl_pred = analysis.correctives[correctivePos++];
int bl_diff = analysis.correctives[correctivePos++];
codec->encodeLDBitLengthCorrection(bl_pred, bl_pred + bl_diff);
}
}
unsigned blcount = analysis.tokenInfo[infoPos++];
info = analysis.tokenInfo[infoPos++];
codec->encodeTreeCodeCountMisprediction(info);
if (info) {
codec->encodeValue(blcount - 4, 4);
}
for (unsigned i = 0; i < blcount; ++i) {
int bl_pred = analysis.correctives[correctivePos++];
int bl_diff = analysis.correctives[correctivePos++];
codec->encodeTreeCodeBitLengthCorrection(bl_pred, bl_pred + bl_diff);
}
}
void PreflateTreePredictor::updateCounters(
PreflateStatisticsCounter* model,
const unsigned blockno) {
BlockAnalysisResult& analysis = analysisResults[blockno];
if (analysis.blockType != PreflateTokenBlock::DYNAMIC_HUFF) {
return;
}
unsigned infoPos = 0, correctivePos = 0;
unsigned char info = analysis.tokenInfo[infoPos++];
model->treecode.incLiteralCountPredictionWrong(info);
if (info) {
correctivePos++;
}
info = analysis.tokenInfo[infoPos++];
model->treecode.incDistanceCountPredictionWrong(info);
if (info) {
correctivePos++;
}
while ((info = analysis.tokenInfo[infoPos++]) != 0xff) {
unsigned type = (info & 3);
model->treecode.incLDCodeTypePredictionWrong(type, (info & 4) != 0);
if (info & 4) {
unsigned newType = analysis.correctives[correctivePos++];
model->treecode.incLDCodeTypeReplacement(newType);
type = newType;
}
if (type != TCT_BITS) {
unsigned predRepeat = analysis.correctives[correctivePos++];
if (info & 8) {
unsigned newRepeat = analysis.correctives[correctivePos++];
model->treecode.incLDCodeRepeatDiffToPrediction(newRepeat - predRepeat);
} else {
model->treecode.incLDCodeRepeatDiffToPrediction(0);
}
} else {
/*unsigned bl_pred = analysis.correctives[*/correctivePos++/*]*/;
int bl_diff = analysis.correctives[correctivePos++];
model->treecode.incLDCodeLengthDiffToPrediction(bl_diff);
}
}
unsigned blcount = analysis.tokenInfo[infoPos++];
info = analysis.tokenInfo[infoPos++];
model->treecode.incTreeCodeCountPredictionWrong(info);
for (unsigned i = 0; i < blcount; ++i) {
/*int bl_pred = analysis.correctives[*/correctivePos++/*]*/;
int bl_diff = analysis.correctives[correctivePos++];
model->treecode.incTreeCodeLengthDiffToPrediction(bl_diff);
}
}
unsigned PreflateTreePredictor::reconstructLDTrees(
PreflatePredictionDecoder* codec,
unsigned* frequencies,
unsigned char* targetCodes,
const unsigned targetCodeSize,
const unsigned char* symBitLen,
const unsigned symLCount,
const unsigned symDCount) {
memset(frequencies, 0, sizeof(unsigned) * PreflateConstants::CODETREE_CODE_COUNT);
const unsigned char* ptr = symBitLen;
unsigned osize = 0;
unsigned count1 = symLCount;
unsigned count2 = symDCount;
bool first = true;
while (count1 + count2 > 0) {
TreeCodeType predictedTreeCodeType = predictCodeType(ptr, count1, first);
unsigned newType = codec->decodeLDTypeCorrection(predictedTreeCodeType);
switch (newType) {
case TCT_BITS:
predictedTreeCodeType = TCT_BITS;
break;
case TCT_REP:
predictedTreeCodeType = TCT_REP;
break;
case TCT_REPZS:
predictedTreeCodeType = TCT_REPZS;
break;
case TCT_REPZL:
predictedTreeCodeType = TCT_REPZL;
break;
}
unsigned char predictedTreeCodeData = predictCodeData(ptr, predictedTreeCodeType, count1, first);
first = false;
if (predictedTreeCodeType != TCT_BITS) {
predictedTreeCodeData = codec->decodeRepeatCountCorrection(predictedTreeCodeData, predictedTreeCodeType);
} else {
predictedTreeCodeData = codec->decodeLDBitLengthCorrection(predictedTreeCodeData);;
}
unsigned l;
if (predictedTreeCodeType != TCT_BITS) {
frequencies[predictedTreeCodeType + 15]++;
l = predictedTreeCodeData;
if (osize + 2 > targetCodeSize) {
predictionFailure = true;
break;
}
targetCodes[osize++] = predictedTreeCodeType + 15;
targetCodes[osize++] = predictedTreeCodeData;
} else {
frequencies[predictedTreeCodeData]++;
l = 1;
if (osize >= targetCodeSize) {
predictionFailure = true;
break;
}
targetCodes[osize++] = predictedTreeCodeData;
}
ptr += l;
if (count1 > l) {
count1 -= l;
} else {
count1 += count2;
count2 = 0;
first = true;
if (count1 >= l) {
count1 -= l;
} else {
predictionFailure = true;
break;
}
}
}
if (count1 + count2 != 0) {
predictionFailure = true;
}
return predictionFailure ? 0 : osize;
}
bool PreflateTreePredictor::decodeBlock(
PreflateTokenBlock& block,
PreflatePredictionDecoder* codec) {
if (block.type != PreflateTokenBlock::DYNAMIC_HUFF) {
return true;
}
unsigned Lcodes[PreflateConstants::LITLEN_CODE_COUNT], Dcodes[PreflateConstants::DIST_CODE_COUNT];
unsigned Lcount = 0, Dcount = 0;
collectTokenStatistics(Lcodes, Dcodes, Lcount, Dcount, block);
unsigned char bitLengths[PreflateConstants::LITLENDIST_CODE_COUNT];
memset(bitLengths, 0, sizeof(bitLengths));
unsigned predictedLTreeSize = buildLBitlenghs(bitLengths, Lcodes);
if (codec->decodeLiteralCountMisprediction()) {
predictedLTreeSize = codec->decodeValue(5) + PreflateConstants::NONLEN_CODE_COUNT;
}
block.nlen = predictedLTreeSize;
unsigned predictedDTreeSize = buildDBitlenghs(bitLengths + predictedLTreeSize, Dcodes);
if (codec->decodeDistanceCountMisprediction()) {
predictedDTreeSize = codec->decodeValue(5);
}
block.ndist = predictedDTreeSize;
unsigned BLfreqs[PreflateConstants::CODETREE_CODE_COUNT];
unsigned char compressedLDtrees[PreflateConstants::LITLENDIST_CODE_COUNT];
unsigned targetCodeSize = reconstructLDTrees(codec, BLfreqs, compressedLDtrees, PreflateConstants::LITLENDIST_CODE_COUNT,
bitLengths, predictedLTreeSize, predictedDTreeSize);
if (predictionFailure) {
return false;
}
unsigned char simpleCodeTree[PreflateConstants::CODETREE_CODE_COUNT];
unsigned predictedCTreeSize = buildTCBitlengths(simpleCodeTree, BLfreqs);
if (codec->decodeTreeCodeCountMisprediction()) {
predictedCTreeSize = codec->decodeValue(4) + 4;
}
block.ncode = predictedCTreeSize;
unsigned char shuffledCodeTree[PreflateConstants::CODETREE_CODE_COUNT];
for (unsigned i = 0; i < predictedCTreeSize; ++i) {
unsigned predictedBL = simpleCodeTree[PreflateConstants::treeCodeOrderTable[i]];
shuffledCodeTree[i] = codec->decodeTreeCodeBitLengthCorrection(predictedBL);
}
block.treecodes.reserve(predictedCTreeSize + targetCodeSize);
block.treecodes.insert(block.treecodes.end(), shuffledCodeTree, shuffledCodeTree + predictedCTreeSize);
block.treecodes.insert(block.treecodes.end(), compressedLDtrees, compressedLDtrees + targetCodeSize);
return true;
}