-
Notifications
You must be signed in to change notification settings - Fork 0
/
NLZM.cpp
2178 lines (1736 loc) · 54.5 KB
/
NLZM.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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// NLZM 1.03 64-bit - Written by Nauful
// Released into the public domain.
// Please drop a comment if you find this useful.
#ifndef _CRT_DISABLE_PERFCRIT_LOCKS
# define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
#ifndef _CRT_SECURE_NO_WARNINGS
# define _CRT_SECURE_NO_WARNINGS
#endif
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef MSVC
# include <crtdbg.h>
# include <intrin.h>
# define _fpos64(f) _ftelli64(f)
# define ASSERT(x) { if (!(x)) { printf("Assert failed " #x "\n"); __debugbreak(); } }
#else
# define _fpos64(f) ftello64(f)
# define ASSERT(x) { if (!(x)) { printf("Assert failed " #x "\n"); exit(-1); } }
#endif
#ifdef DEBUG
# define ASSERT_DEBUG(x) ASSERT(x)
#else
# define ASSERT_DEBUG(x)
#endif
typedef unsigned char byte;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;
typedef short int16;
typedef int int32;
typedef long long int64;
template<typename T, size_t N> constexpr size_t CountOf(T const (&)[N]) { return N; }
template<typename T> constexpr T Min(const T a, const T b) { return (a < b) ? a : b; }
template<typename T> constexpr T Max(const T a, const T b) { return (a < b) ? b : a; }
template<typename T> constexpr T Clamp(const T v, const T low, const T high) {
if (v < low) {
return low;
}
else if (v > high) {
return high;
}
else {
return v;
}
}
uint32 clz32(uint32 x) {
#ifdef MSVC
unsigned long v0;
_BitScanReverse(&v0, x);
return v0;
#else
uint32 r, q;
r = (x > 0xFFFF) << 4; x >>= r;
q = (x > 0xFF) << 3; x >>= q; r |= q;
q = (x > 0xF) << 2; x >>= q; r |= q;
q = (x > 0x3) << 1; x >>= q; r |= q;
r |= (x >> 1);
return r;
#endif
}
uint32 popcnt32(uint32 x) {
x -= ((x >> 1) & 0x55555555);
x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
x = (((x >> 4) + x) & 0x0F0F0F0F);
x += (x >> 8);
x += (x >> 16);
return x & 0x0000003F;
}
uint32 ctz32(uint32 x) {
#ifdef MSVC
unsigned long v0;
_BitScanForward(&v0, x);
return v0;
#else
return popcnt32((x & uint32(-int32(x))) - 1);
#endif
}
const int LOG2_LUT_SIZE_BITS = 8;
const int LOG2_LUT_SCALE_BITS = 5;
const int LOG2_LUT_PRECISION = 16;
uint16 log2_lut[1 << LOG2_LUT_SIZE_BITS];
void log2_init() {
const int table_size = 1 << LOG2_LUT_SIZE_BITS;
const int half_table_size = 1 << (LOG2_LUT_SIZE_BITS - 1);
const int scale = 1 << LOG2_LUT_SCALE_BITS;
for (int i = 1; i < table_size; i++) {
uint32 next = 1 << LOG2_LUT_PRECISION;
uint16 acc = 0;
for (int sum = 0; sum < scale; sum++) {
const uint32 v = (i * next) >> LOG2_LUT_SIZE_BITS;
const uint32 num_bits = LOG2_LUT_PRECISION - clz32(v);
acc += num_bits - 1;
next = v << (num_bits - 1);
}
log2_lut[i] = acc;
}
log2_lut[0] = log2_lut[1];
}
static uint32 crc32_table[16][256];
void crc32_init() {
// Reversed bit order for CRC32
const static uint32 POLY = 0xEDB88320;
uint32 n, crc, k;
for (n = 0; n < 256; n++) {
crc = n;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
crc32_table[0][n] = crc;
}
for (n = 0; n < 256; n++) {
crc = crc32_table[0][n];
for (k = 1; k < 16; k++) {
crc = crc32_table[0][crc & 0xff] ^ (crc >> 8);
crc32_table[k][n] = crc;
}
}
}
uint32 crc32_calc(const byte *window, int64 n, uint64 crci) {
uint64 crc = crci ^ 0xFFFFFFFF;
const byte *next = window;
while (n && (byte(size_t(next)) & 15)) {
crc = crc32_table[0][(crc ^ (*next++)) & 0xFF] ^ (crc >> 8);
--n;
}
while (n >= 16) {
crc ^= *reinterpret_cast<const uint64 *>(next);
uint64 high = *reinterpret_cast<const uint64 *>(next + 8);
crc =
crc32_table[15][crc & 0xFF] ^
crc32_table[14][(crc >> 8) & 0xFF] ^
crc32_table[13][(crc >> 16) & 0xFF] ^
crc32_table[12][(crc >> 24) & 0xFF] ^
crc32_table[11][(crc >> 32) & 0xFF] ^
crc32_table[10][(crc >> 40) & 0xFF] ^
crc32_table[9][(crc >> 48) & 0xFF] ^
crc32_table[8][crc >> 56] ^
crc32_table[7][high & 0xFF] ^
crc32_table[6][(high >> 8) & 0xFF] ^
crc32_table[5][(high >> 16) & 0xFF] ^
crc32_table[4][(high >> 24) & 0xFF] ^
crc32_table[3][(high >> 32) & 0xFF] ^
crc32_table[2][(high >> 40) & 0xFF] ^
crc32_table[1][(high >> 48) & 0xFF] ^
crc32_table[0][high >> 56];
next += 16;
n -= 16;
}
while (n) {
crc = crc32_table[0][(crc ^ (*next++)) & 0xFF] ^ (crc >> 8);
--n;
}
crci = crc ^ 0xFFFFFFFF;
return crci;
}
uint32 crc32_file(FILE *f) {
byte buf[0x4000];
uint32 n = 0, crc = 0;
while ((n = fread(buf, 1, sizeof(buf), f)) > 0) {
crc = crc32_calc(buf, n, crc);
}
return crc;
}
const int16 CDF_ADAPT_BITS = 7;
const int16 CDF_SCALE_BITS = 14;
const int16 CDF_ADAPT_TOTAL = 1 << CDF_ADAPT_BITS;
const int16 CDF_SCALE_TOTAL = 1 << CDF_SCALE_BITS;
const int16 CDF_SCALE_TOTAL_MASK = CDF_SCALE_TOTAL - 1;
struct CDF1 {
uint16 cell[3];
};
struct CDF2 {
uint16 cell[5];
};
struct CDF3 {
#ifdef USE_SSE
union {
__m128i _cell[2];
#endif
uint16 cell[9];
#ifdef USE_SSE
};
#endif
};
struct CDF4 {
#ifdef USE_SSE
union {
__m128i _cell[3];
#endif
uint16 cell[17];
#ifdef USE_SSE
};
#endif
};
const int16 cdf_mixin1[2] = { CDF_SCALE_TOTAL - CDF_ADAPT_TOTAL - 1, CDF_ADAPT_TOTAL + 1 };
int16 cdf_mixin2[1 << 2][1 << 2];
int16 cdf_mixin3[1 << 3][1 << 3];
int16 cdf_mixin4[1 << 4][1 << 4];
#ifdef USE_SSE
__m128i _cdf_mixin3[8];
__m128i _cdf_mixin4[16][2];
#endif
const int16 cdf_initial1[3] = {
0, CDF_SCALE_TOTAL / 2,
CDF_SCALE_TOTAL
};
const int16 cdf_initial2[5] = {
0, CDF_SCALE_TOTAL / 4, 2 * CDF_SCALE_TOTAL / 4, 3 * CDF_SCALE_TOTAL / 4,
CDF_SCALE_TOTAL
};
const int16 cdf_initial3[9] = {
0, CDF_SCALE_TOTAL / 8, 2 * CDF_SCALE_TOTAL / 8, 3 * CDF_SCALE_TOTAL / 8,
4 * CDF_SCALE_TOTAL / 8, 5 * CDF_SCALE_TOTAL / 8, 6 * CDF_SCALE_TOTAL / 8, 7 * CDF_SCALE_TOTAL / 8,
CDF_SCALE_TOTAL
};
const int16 cdf_initial4[17] = {
0, CDF_SCALE_TOTAL / 16, 2 * CDF_SCALE_TOTAL / 16, 3 * CDF_SCALE_TOTAL / 16,
4 * CDF_SCALE_TOTAL / 16, 5 * CDF_SCALE_TOTAL / 16, 6 * CDF_SCALE_TOTAL / 16, 7 * CDF_SCALE_TOTAL / 16,
8 * CDF_SCALE_TOTAL / 16, 9 * CDF_SCALE_TOTAL / 16, 10 * CDF_SCALE_TOTAL / 16, 11 * CDF_SCALE_TOTAL / 16,
12 * CDF_SCALE_TOTAL / 16, 13 * CDF_SCALE_TOTAL / 16, 14 * CDF_SCALE_TOTAL / 16, 15 * CDF_SCALE_TOTAL / 16,
CDF_SCALE_TOTAL
};
template<int num_syms, int adapt_bits, int total>
void init_mixin_table(int16(&mixin)[num_syms][num_syms]) {
const int mixin_bias = (1 << adapt_bits) - 1 - num_syms;
ASSERT_DEBUG(mixin_bias > 0);
for (int y = 0; y < num_syms; y++) {
for (int x = 0; x <= y; x++) {
mixin[y][x] = x;
}
for (int x = y + 1; x < num_syms; x++) {
mixin[y][x] = total + x + mixin_bias;
}
}
}
void cdf_init_mixins() {
init_mixin_table<1 << 2, CDF_ADAPT_BITS, CDF_SCALE_TOTAL>(cdf_mixin2);
init_mixin_table<1 << 3, CDF_ADAPT_BITS, CDF_SCALE_TOTAL>(cdf_mixin3);
init_mixin_table<1 << 4, CDF_ADAPT_BITS, CDF_SCALE_TOTAL>(cdf_mixin4);
#ifdef USE_SSE
for (int i = 0; i < 8; i++) {
_cdf_mixin3[i] = _mm_set_epi16(
cdf_mixin3[i][7], cdf_mixin3[i][6], cdf_mixin3[i][5], cdf_mixin3[i][4],
cdf_mixin3[i][3], cdf_mixin3[i][2], cdf_mixin3[i][1], cdf_mixin3[i][0]);
}
for (int i = 0; i < 16; i++) {
_cdf_mixin4[i][0] = _mm_set_epi16(
cdf_mixin4[i][7], cdf_mixin4[i][6], cdf_mixin4[i][5], cdf_mixin4[i][4],
cdf_mixin4[i][3], cdf_mixin4[i][2], cdf_mixin4[i][1], cdf_mixin4[i][0]);
_cdf_mixin4[i][1] = _mm_set_epi16(
cdf_mixin4[i][15], cdf_mixin4[i][14], cdf_mixin4[i][13], cdf_mixin4[i][12],
cdf_mixin4[i][11], cdf_mixin4[i][10], cdf_mixin4[i][9], cdf_mixin4[i][8]);
}
#endif
}
void cdf_init(CDF1 &cdf) {
for (int i = 0; i < 3; i++) {
cdf.cell[i] = cdf_initial1[i];
}
}
void cdf_init(CDF2 &cdf) {
for (int i = 0; i < 5; i++) {
cdf.cell[i] = cdf_initial2[i];
}
}
void cdf_init(CDF3 &cdf) {
for (int i = 0; i < 9; i++) {
cdf.cell[i] = cdf_initial3[i];
}
}
void cdf_init(CDF4 &cdf) {
for (int i = 0; i < 17; i++) {
cdf.cell[i] = cdf_initial4[i];
}
}
void cdf_update(CDF1 &cdf, int y) {
cdf.cell[1] += (cdf_mixin1[y] - cdf.cell[1]) >> CDF_ADAPT_BITS;
}
void cdf_update(CDF2 &cdf, int y) {
for (int i = 0; i < 4; i++) {
cdf.cell[i] += (cdf_mixin2[y][i] - cdf.cell[i]) >> CDF_ADAPT_BITS;
}
}
void cdf_update(CDF3 &cdf, int y) {
#ifdef USE_SSE
__m128i upd = _mm_srai_epi16(_mm_sub_epi16(_cdf_mixin3[y], cdf._cell[0]), CDF_ADAPT_BITS);
cdf._cell[0] = _mm_add_epi16(cdf._cell[0], upd);
#else
for (int i = 0; i < 8; i++) {
cdf.cell[i] += (cdf_mixin3[y][i] - cdf.cell[i]) >> CDF_ADAPT_BITS;
}
#endif
}
void cdf_update(CDF4 &cdf, int y) {
#ifdef USE_SSE
__m128i upd0 = _mm_srai_epi16(_mm_sub_epi16(_cdf_mixin4[y][0], cdf._cell[0]), CDF_ADAPT_BITS);
__m128i upd1 = _mm_srai_epi16(_mm_sub_epi16(_cdf_mixin4[y][1], cdf._cell[1]), CDF_ADAPT_BITS);
cdf._cell[0] = _mm_add_epi16(cdf._cell[0], upd0);
cdf._cell[1] = _mm_add_epi16(cdf._cell[1], upd1);
#else
for (int i = 0; i < 16; i++) {
cdf.cell[i] += (cdf_mixin4[y][i] - cdf.cell[i]) >> CDF_ADAPT_BITS;
}
#endif
}
int cdf_lookup(const CDF1 &cdf, int f) {
return f >= cdf.cell[1];
}
int cdf_lookup(const CDF2 &cdf, int f) {
int r = 2 * (f >= cdf.cell[2]);
r += f >= cdf.cell[1 + r];
return r;
}
int cdf_lookup(const CDF3 &cdf, int f) {
#ifdef USE_SSE
__m128i _f = _mm_cvtsi32_si128(f);
_f = _mm_shuffle_epi32(_mm_unpacklo_epi16(_f, _f), 0);
__m128i _cmp0 = _mm_cmpgt_epi16(cdf._cell[0], _f);
int idx_mask = _mm_movemask_epi8(_cmp0) | 0x10000;
int idx = ctz32(idx_mask) >> 1;
return idx - 1;
#else
int r = 4 * (f >= cdf.cell[4]);
r += 2 * (f >= cdf.cell[2 + r]);
r += f >= cdf.cell[1 + r];
return r;
#endif
}
int cdf_lookup(const CDF4 &cdf, int f) {
#ifdef USE_SSE
__m128i _f = _mm_cvtsi32_si128(f);
_f = _mm_shuffle_epi32(_mm_unpacklo_epi16(_f, _f), 0);
__m128i _cmp0 = _mm_cmpgt_epi16(cdf._cell[0], _f);
__m128i _cmp1 = _mm_cmpgt_epi16(cdf._cell[1], _f);
int idx_mask = _mm_movemask_epi8(_mm_packs_epi16(_cmp0, _cmp1)) | 0x10000;
int idx = ctz32(idx_mask);
return idx - 1;
#else
int r = 8 * (f >= cdf.cell[8]);
r += 4 * (f >= cdf.cell[4 + r]);
r += 2 * (f >= cdf.cell[2 + r]);
r += f >= cdf.cell[1 + r];
return r;
#endif
}
uint16 cdf_cost(const CDF1 &cdf, int y) { return log2_lut[(cdf.cell[y + 1] - cdf.cell[y]) >> (CDF_SCALE_BITS - LOG2_LUT_SIZE_BITS)]; }
uint16 cdf_cost(const CDF2 &cdf, int y) { return log2_lut[(cdf.cell[y + 1] - cdf.cell[y]) >> (CDF_SCALE_BITS - LOG2_LUT_SIZE_BITS)]; }
uint16 cdf_cost(const CDF3 &cdf, int y) { return log2_lut[(cdf.cell[y + 1] - cdf.cell[y]) >> (CDF_SCALE_BITS - LOG2_LUT_SIZE_BITS)]; }
uint16 cdf_cost(const CDF4 &cdf, int y) { return log2_lut[(cdf.cell[y + 1] - cdf.cell[y]) >> (CDF_SCALE_BITS - LOG2_LUT_SIZE_BITS)]; }
typedef uint32 rans_t;
const rans_t RANS_MID = 1 << 16;
rans_t rans_enc_put(rans_t x, byte **pptr, uint32 start, uint32 freq) {
uint32 x_max = ((RANS_MID >> CDF_SCALE_BITS) << 16);
x_max *= freq;
if (x >= x_max) {
*--(pptr[0]) = (byte)x;
*--(pptr[0]) = (byte)(x >> 8);
x >>= 16;
}
return ((x / freq) << CDF_SCALE_BITS) + (x % freq) + start;
}
rans_t rans_dec_consume(rans_t x, uint32 start, uint32 freq) {
return freq * (x >> CDF_SCALE_BITS) + (x & CDF_SCALE_TOTAL_MASK) - start;
}
void rans_enc_flush(rans_t r, byte **pptr) {
uint32 x = r;
pptr[0] -= 4;
pptr[0][0] = (byte)(x);
pptr[0][1] = (byte)(x >> 8);
pptr[0][2] = (byte)(x >> 16);
pptr[0][3] = (byte)(x >> 24);
}
rans_t rans_dec_init(byte **pptr) {
uint32 x = pptr[0][0];
x |= pptr[0][1] << 8;
x |= pptr[0][2] << 16;
x |= pptr[0][3] << 24;
pptr[0] += 4;
return x;
}
rans_t rans_dec_normalize(rans_t x, byte **pptr) {
if (x < RANS_MID) {
x = (x << 16) + (pptr[0][0] << 8) + pptr[0][1];
*pptr += 2;
}
return x;
}
struct CodeFrame {
byte *start, *end, *ptr_bits;
uint32 word, word_bits;
uint32 write_limit;
uint32 num_ops;
uint32 *buf_rans, num_rans_syms, max_rans_syms, est_rans_bits;
void Init(byte *start, byte *end, uint32 *buf_rans, uint32 max_rans_syms);
bool Size() const;
bool NeedsFlush() const;
void WriteRange(uint16 start, uint16 freq);
void WriteBits(uint32 v, uint32 nb);
void WriteCDF(const CDF1 &cdf, int y);
void WriteCDF(const CDF2 &cdf, int y);
void WriteCDF(const CDF3 &cdf, int y);
void WriteCDF(const CDF4 &cdf, int y);
uint32 Flush();
};
struct DecodeFrame {
byte *start, *end, *ptr_bits, *ptr_rans;
uint32 word, word_bits;
uint32 num_ops;
rans_t rans_state[4];
byte rans_index;
uint32 Init(byte *start, byte *end);
int ReadCDF(const CDF1 &cdf);
int ReadCDF(const CDF2 &cdf);
int ReadCDF(const CDF3 &cdf);
int ReadCDF(const CDF4 &cdf);
uint32 ReadBits(uint32 nb);
};
void CodeFrame::Init(byte *start, byte *end, uint32 *buf_rans, uint32 max_rans_syms) {
ASSERT_DEBUG(end - start >= 1024);
this->start = start;
this->end = end;
this->ptr_bits = start + 12;
this->word = 0;
this->word_bits = 0;
this->write_limit = (15 * (end - start)) / 16;
this->num_ops = 0;
this->buf_rans = buf_rans;
this->max_rans_syms = max_rans_syms;
this->num_rans_syms = 0;
this->est_rans_bits = 0;
}
bool CodeFrame::Size() const { return end - start; }
bool CodeFrame::NeedsFlush() const {
return num_rans_syms + 8 >= max_rans_syms ||
(ptr_bits - start) + (est_rans_bits >> (8 + LOG2_LUT_SCALE_BITS)) + 64 >= write_limit;
}
void CodeFrame::WriteRange(uint16 start, uint16 freq) {
ASSERT(num_rans_syms <= max_rans_syms);
ASSERT(freq > 0);
++num_ops;
buf_rans[num_rans_syms++] = (freq << 16) + start;
est_rans_bits += log2_lut[freq >> (CDF_SCALE_BITS - LOG2_LUT_SIZE_BITS)];
}
void CodeFrame::WriteCDF(const CDF1 &cdf, int y) { WriteRange(cdf.cell[y], cdf.cell[y + 1] - cdf.cell[y]); }
void CodeFrame::WriteCDF(const CDF2 &cdf, int y) { WriteRange(cdf.cell[y], cdf.cell[y + 1] - cdf.cell[y]); }
void CodeFrame::WriteCDF(const CDF3 &cdf, int y) { WriteRange(cdf.cell[y], cdf.cell[y + 1] - cdf.cell[y]); }
void CodeFrame::WriteCDF(const CDF4 &cdf, int y) { WriteRange(cdf.cell[y], cdf.cell[y + 1] - cdf.cell[y]); }
void CodeFrame::WriteBits(uint32 v, uint32 nb) {
ASSERT(v < (1u << nb));
++num_ops;
word |= v << (32 - word_bits - nb);
word_bits += nb;
while (word_bits >= 8) {
ASSERT(ptr_bits < end);
*ptr_bits++ = word >> 24;
word <<= 8;
word_bits -= 8;
}
}
uint32 CodeFrame::Flush() {
for (int i = 0; i < 4; i++) {
ASSERT(ptr_bits < end);
*ptr_bits++ = word >> 24;
word <<= 8;
word_bits -= Min(word_bits, 8u);
}
byte *wptr = end - 1;
rans_t st[4] = { RANS_MID, RANS_MID, RANS_MID, RANS_MID };
for (uint32 i = num_rans_syms - 1; i + 1 > 0; i--) {
st[i & 3] = rans_enc_put(st[i & 3], &wptr, buf_rans[i] & 0xFFFF, buf_rans[i] >> 16);
}
rans_enc_flush(st[3], &wptr);
rans_enc_flush(st[2], &wptr);
rans_enc_flush(st[1], &wptr);
rans_enc_flush(st[0], &wptr);
ASSERT(wptr >= ptr_bits);
uint32 num_rans_bytes = (end - 1) - wptr;
memmove(ptr_bits, wptr, num_rans_bytes);
uint32 num_bits_bytes = ptr_bits - start;
start[0] = byte(num_ops >> 24);
start[1] = byte(num_ops >> 16);
start[2] = byte(num_ops >> 8);
start[3] = byte(num_ops);
start[4] = byte(num_bits_bytes >> 24);
start[5] = byte(num_bits_bytes >> 16);
start[6] = byte(num_bits_bytes >> 8);
start[7] = byte(num_bits_bytes);
start[8] = byte(num_rans_bytes >> 24);
start[9] = byte(num_rans_bytes >> 16);
start[10] = byte(num_rans_bytes >> 8);
start[11] = byte(num_rans_bytes);
ptr_bits = start + 12;
word = 0;
word_bits = 0;
num_ops = 0;
num_rans_syms = 0;
est_rans_bits = 0;
return num_bits_bytes + num_rans_bytes;
}
uint32 DecodeFrame::Init(byte *start, byte *end) {
this->start = start;
this->end = end;
this->num_ops = (start[0] << 24) + (start[1] << 16) + (start[2] << 8) + start[3];
if (!this->num_ops) {
return -1;
}
this->ptr_bits = start + 12;
uint32 num_bits_bytes = (start[4] << 24) + (start[5] << 16) + (start[6] << 8) + start[7];
uint32 num_rans_bytes = (start[8] << 24) + (start[9] << 16) + (start[10] << 8) + start[11];
this->ptr_rans = start + num_bits_bytes;
this->word = 0;
this->word_bits = 0;
for (int i = 0; i < 4; i++) {
this->rans_state[i] = rans_dec_init(&ptr_rans);
}
this->rans_index = 0;
return num_bits_bytes + num_rans_bytes;
}
int DecodeFrame::ReadCDF(const CDF1 &cdf) {
--num_ops;
rans_t &rs = rans_state[rans_index++ & 3];
uint16 f = rs & CDF_SCALE_TOTAL_MASK;
int y = cdf_lookup(cdf, f);
rs = rans_dec_consume(rs, cdf.cell[y], cdf.cell[y + 1] - cdf.cell[y]);
rs = rans_dec_normalize(rs, &ptr_rans);
return y;
}
int DecodeFrame::ReadCDF(const CDF2 &cdf) {
--num_ops;
rans_t &rs = rans_state[rans_index++ & 3];
uint16 f = rs & CDF_SCALE_TOTAL_MASK;
int y = cdf_lookup(cdf, f);
rs = rans_dec_consume(rs, cdf.cell[y], cdf.cell[y + 1] - cdf.cell[y]);
rs = rans_dec_normalize(rs, &ptr_rans);
return y;
}
int DecodeFrame::ReadCDF(const CDF3 &cdf) {
--num_ops;
rans_t &rs = rans_state[rans_index++ & 3];
uint16 f = rs & CDF_SCALE_TOTAL_MASK;
int y = cdf_lookup(cdf, f);
rs = rans_dec_consume(rs, cdf.cell[y], cdf.cell[y + 1] - cdf.cell[y]);
rs = rans_dec_normalize(rs, &ptr_rans);
return y;
}
int DecodeFrame::ReadCDF(const CDF4 &cdf) {
--num_ops;
rans_t &rs = rans_state[rans_index++ & 3];
uint16 f = rs & CDF_SCALE_TOTAL_MASK;
int y = cdf_lookup(cdf, f);
rs = rans_dec_consume(rs, cdf.cell[y], cdf.cell[y + 1] - cdf.cell[y]);
rs = rans_dec_normalize(rs, &ptr_rans);
return y;
}
uint32 DecodeFrame::ReadBits(uint32 nb) {
--num_ops;
while (word_bits < 24) {
ASSERT(ptr_bits < end);
word |= *ptr_bits++ << (24 - word_bits);
word_bits += 8;
}
ASSERT(nb <= word_bits);
uint32 y = word >> (32 - nb);
word <<= nb;
word_bits -= nb;
return y;
}
const static uint32 MATCH_MIN = 2;
const static uint32 MATCH_NICE_LENGTH = 64;
const static uint32 MATCH_SKIP_UPDATES_NICE_LENGTH = 7;
const static uint32 MATCH_NICE_RK_LENGTH = 256;
const static uint32 MATCH_MAX = MATCH_MIN + 255 + 7;
#define HASH4(x) ((x) * 987660757u)
#define VALUE4(p) (*(uint32 *)(p))
#define VALUE3(p) (VALUE4(p) & 0xFFFFFFu)
#define VALUE2(p) (*(uint16 *)(p))
uint32 get_match_min(uint32 dist);
struct MatchTable {
uint16 max_len;
uint32 delta[MATCH_MAX + 1];
void Update(uint32 delta, uint16 len);
void CarryFrom(MatchTable &prev, uint32 shift);
};
struct RingDictionary {
byte *hist, *lookahead;
uint32 hist_bits, hist_mask;
uint32 hist_pos, lookahead_len;
uint32 MatchLengthSigned(uint32 p0, uint32 p1, uint16 max_len, uint16 initial_len) const;
uint32 MatchLength(uint32 p0, uint32 p1, uint16 max_len) const;
int CharAtPosition(uint32 p) const;
void Shift(uint32 shift);
};
struct MatchFinderHT {
uint32 hash_bits, num_rows, window_bits, hash_shift, hash_mask, window_mask;
uint32 *rows;
uint32 Init(uint32 hash_bits, uint32 num_rows, uint32 window_bits);
void Release();
void FindAndUpdate(MatchTable &mt, uint32 h4, uint32 p, const RingDictionary &dict);
void Shift(uint32 shift);
};
struct MatchFinderBT {
const static int MAX_TESTS = 256;
uint32 hash_bits, window_bits, hash_shift;
uint32 *nodes, *heads, *tree;
uint32 Init(uint32 hash_bits, uint32 window_bits);
void Release();
void FindAndUpdate(MatchTable &mt, uint32 h4, uint32 p, const RingDictionary &dict);
void Shift(uint32 shift);
};
struct MatchFinderRK256 {
const static uint32 BLOCK_BITS = 8;
const static uint32 BLOCK_SIZE = 1 << BLOCK_BITS;
const static uint32 BLOCK_MASK = BLOCK_SIZE - 1;
const static uint32 ADDH = 0x2F0FD693u;
//uint32 remh = 1; for (int i = 0; i < BLOCK_SIZE; i++) { remh *= addh; }
const static uint32 REMH = 0x0E4EA401u;
uint32 rolling_hash_add(uint32 p, int y) { return (y + p) * ADDH; }
uint32 rolling_hash_add_remove(uint32 p, int yr, int yl) { return (yr + p - yl * REMH) * ADDH; }
uint32 hash_shift, window_bits, hash_mask, window_mask;
uint32 *table;
uint32 carry_match_from, carry_match_to, carry_match_len;
uint32 rh, rh_end;
uint32 Init(uint32 hash_bits, uint32 window_bits);
void Release();
void FindAndUpdate(MatchTable &mt, uint32 p, const RingDictionary &dict);
void Shift(uint32 shift);
};
uint32 get_match_min(uint32 dist) {
uint32 match_min = MATCH_MIN;
if (dist & ~0xFF) { ++match_min; }
if (dist & ~0xFFF) { ++match_min; }
if (dist & ~0xFFFFF) { ++match_min; }
return match_min;
}
void MatchTable::CarryFrom(MatchTable &prev, uint32 shift) {
if (prev.max_len <= shift) {
max_len = 0;
}
else {
max_len = prev.max_len - shift;
for (uint16 i = 0; i <= max_len; i++) {
delta[i] = prev.delta[i + shift];
}
}
}
void MatchTable::Update(uint32 mdelta, uint16 mlen) {
ASSERT(mlen >= get_match_min(mdelta));
ASSERT(mlen <= MATCH_MAX);
ASSERT(mdelta > 0);
int i = 0;
while (i <= mlen && i <= max_len) {
delta[i] = Min(mdelta, delta[i]);
++i;
}
while (i <= mlen) {
delta[i] = mdelta;
++i;
}
max_len = Max(max_len, mlen);
}
uint32 RingDictionary::MatchLengthSigned(uint32 p0, uint32 p1, uint16 max_len, uint16 initial_len) const {
ASSERT(p0 < p1);
p0 += initial_len;
p1 += initial_len;
ASSERT((p0 >= hist_pos && p0 - hist_pos < lookahead_len) || hist_pos - p0 <= hist_mask);
ASSERT((p1 >= hist_pos && p1 - hist_pos < lookahead_len) || hist_pos - p1 <= hist_mask);
uint16 mlen = initial_len;
while (mlen < max_len) {
byte c0 = p0 >= hist_pos ? lookahead[p0 - hist_pos] : hist[p0 & hist_mask];
byte c1 = p1 >= hist_pos ? lookahead[p1 - hist_pos] : hist[p1 & hist_mask];
if (c0 != c1) {
return mlen | ((c0 < c1) << 31);
}
++p0;
++p1;
++mlen;
}
return mlen;
}
uint32 RingDictionary::MatchLength(uint32 p0, uint32 p1, uint16 max_len) const {
return RingDictionary::MatchLengthSigned(p0, p1, max_len, 0) & 0x7FFFFFFF;
}
int RingDictionary::CharAtPosition(uint32 p) const {
ASSERT((p >= hist_pos && p - hist_pos < lookahead_len) || hist_pos - p <= hist_mask);
return p >= hist_pos ? lookahead[p - hist_pos] : hist[p & hist_mask];
}
void RingDictionary::Shift(uint32 shift) {
ASSERT(!(shift & hist_mask));
hist_pos -= shift;
}
uint32 MatchFinderHT::Init(uint32 hash_bits, uint32 num_rows, uint32 window_bits) {
this->hash_bits = hash_bits;
this->hash_shift = 32 - hash_bits;
this->window_bits = window_bits;
this->num_rows = num_rows;
this->window_mask = (1u << window_bits) - 1;
this->hash_mask = (1u << (32 - window_bits)) - 1;
rows = new uint32[num_rows << hash_bits];
memset(rows, -1, (4U * num_rows) << hash_bits);
return (4 * num_rows) << hash_bits;
}
void MatchFinderHT::Release() {
delete[] rows;
}
void MatchFinderHT::FindAndUpdate(MatchTable &mt, uint32 h4, uint32 p, const RingDictionary &dict) {
uint32 h_row = h4 & hash_mask;
uint32 *ht = rows + (h4 >> hash_shift);
uint32 carry = p | (h_row << window_bits);
uint16 max_len = Min(dict.lookahead_len + dict.hist_pos - p, MATCH_MAX);
uint32 best = MATCH_MIN - 1;
uint32 best_p = -1;
for (uint32 i = 0; i < num_rows; i++) {
uint32 row = ht[i];
if (best < max_len && (row >> window_bits) == h_row) {
uint32 sp = row & window_mask;
if (sp < p && p - sp <= dict.hist_mask) {
uint16 mlen = dict.MatchLength(sp, p, max_len);
if (mlen > best && mlen >= get_match_min(p - sp)) {
mt.Update(p - sp, mlen);
best = mlen;
}
}
}
ht[i] = carry;
carry = row;
}
}
void MatchFinderHT::Shift(uint32 shift) {
uint32 *row = rows;
uint32 *end = rows + (num_rows << hash_bits);
while (row < end) {
uint32 h = *rows >> window_bits;
uint32 p = *rows & window_mask;
if (p >= shift && *rows != uint32(-1)) {
p -= shift;
*rows = p | (h << window_bits);
}
else {
*rows = -1;
}
++row;
}
}
uint32 MatchFinderBT::Init(uint32 hash_bits, uint32 window_bits) {
this->hash_bits = hash_bits;
this->hash_shift = 32 - hash_bits;
this->window_bits = window_bits;
nodes = new uint32[(1u << hash_bits) + (2u << window_bits)];
heads = nodes;
tree = nodes + (1u << hash_bits);
memset(heads, -1, 4u << hash_bits);
memset(tree, -1, 8u << window_bits);
return (4 << hash_bits) + (8 << window_bits);
}
void MatchFinderBT::Release() {
delete[] nodes;
}
void MatchFinderBT::FindAndUpdate(MatchTable &mt, uint32 h4, uint32 p, const RingDictionary &dict) {
uint32 *pending_left = tree + ((p & dict.hist_mask) << 1);
uint32 *pending_right = pending_left + 1;
uint32 left_len = 0, right_len = 0;
uint32 sp = heads[h4 >> hash_shift];
heads[h4 >> hash_shift] = p;
ASSERT((h4 >> hash_shift) < (1u << (32 - hash_shift)));
uint16 max_len = Min(dict.lookahead_len + dict.hist_pos - p, MATCH_MAX);
uint16 tests_left = MAX_TESTS;
while (sp != (uint32)-1 && p > sp && p - sp <= dict.hist_mask && tests_left-- > 0) {
ASSERT(sp < p);
uint32 *pair = tree + ((sp & dict.hist_mask) << 1);
uint32 mlen_signed = dict.MatchLengthSigned(sp, p, max_len, Min(left_len, right_len));
uint32 mlen = mlen_signed & 0x7FFFFFFF;
if (mlen >= get_match_min(p - sp)) {
mt.Update(p - sp, mlen);
}
if (mlen == max_len) {