-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqad.cpp
1839 lines (1623 loc) · 67.1 KB
/
qad.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
/*
* qad.cpp - base class providing RPC like mechanisms
*
* (c) 2014 - 2016 Petr Kucera, kuc406.moxo.cz <quadro2/at/email.cz>
*
* The BWT-QLFC based compressor, utilizing a cyclic tree and RLE
* compression of either branches or states. Final entropy coder,
* either 32-bit or 64-bit is based on a Subbotin's Range Coder
* implementation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include <time.h>
#include <iostream>
#include <fstream>
#include <omp.h>
#include <unistd.h>
//#include <math.h>
#include <stdlib.h>
//#include <iomanip>
#include <cstring>
//#include <fcntl.h>
//#define Build32bit // for 32-bit RC with bit-aligned output
#define Build64bit // for 64-bit RC with byte-aligned output
#define VERSION 1.13
#define DelayRleBitMantisaCtx // affects both compression methods 'c' and 'c2'
//#define RLEtprankPrediction // affects compression method 'c' only, this is implemented also vice-versa,
// as one of changes between compression methof c and c2
// note since version 1.0, RLEtprankPrediction is not used
//#define DEBUG
#ifdef Build64bit
// Byte-aligned 64bit RC output limists
#define TOP 0x0100000000000000
#define ALI 8 // output aligment in bits
#endif
#ifdef Build32bit
// Bit-aligned 32bit RC output limists
#define TOP 0x80000000
#define ALI 1
#endif
// nibble bassed, 32 bit
//#define TOP 0x10000000
// 2D array -> 1D recomputation
#define p( ctx, mode, c ) ctx + ( mode * CTXs ) + ( ( c ) * SubCTXs * CTXs )
// decoder read single bit for bit-aligned input
#ifdef Build32bit
#define GetRCb( c, p ) ( c << 1 ) + \
( ( ( unsigned char )inBuf[ ctx ][ 3 + ( p >> 3 ) ] >> ( 7 - ( p & 7 ) ) ) & 1 )
#else
#define GetRCb( c, p ) ( c << 1 ) + \
( ( ( unsigned char )inBuf[ ctx ][ 7 + ( p >> 3 ) ] >> ( 7 - ( p & 7 ) ) ) & 1 )
#endif
#define MAXCHAR 0xFF
using namespace std;
unsigned long size;
unsigned int bwtIndex; // BWT Index
bool bwtI = false;
const int READ_SEG = 16384;
const int BUFPOS = 8;
const int TREESIZE = 9; // cyclic tree to encode mtf/qlfc ranks
// maximum treshold for (rdiv = char / TREESIZE), treeRLE method
const int TREETRESH = 15; // set 0 to ignore, default 15
//RC Variables
const int alphabet = 1; // size of alphabet
const int CTXs = 12; // number of contexts
const int SubCTXs = 430000; // /subcontexts
struct read64 { // struct to help parse large numbers
#ifdef Build64bit // -not so effective
unsigned int b1;
#endif
unsigned short b2;
unsigned char b3;
unsigned char b4;
} ;
#ifdef Build64bit
unsigned long long LR[ CTXs ]; // left + range
unsigned long long RCLow[ CTXs ] = { 0 }, RCRange[ CTXs ];
#endif
#ifdef Build32bit
unsigned int LR[ CTXs ];
unsigned int RCLow[ CTXs ] = { 0 }, RCRange[ CTXs ];
#endif
read64 * readLow[ CTXs ], * readLr[ CTXs ]; // struct pointer for parsing large numbers
unsigned int * fq; // symbol cumulative prob buffer (1D, will be mapped into 2D with p( macro)
unsigned int * sRt; // symbol rank table (for cum. prob. table re-scalle RC hashing)
unsigned int pos[ CTXs ] = { 0 }, isCTXUsed[ CTXs ] = { 0 }; // possition in buffer, context usage identifier
unsigned int RCbufP[ CTXs ] = { 0 }; // buffer position
// DeRC
#ifdef Build64bit
unsigned long long code[ CTXs ]; // input code for RC decoder
#endif
#ifdef Build32bit
unsigned int code[ CTXs ];
#endif
unsigned long pozInBufBin[ CTXs ] = { BUFPOS };
char * inBuf[ CTXs ]; // input buffer
//RC Variables ends
char * outBuf[ CTXs ], storedChars[ MAXCHAR + 1 ]; //variety buffers
long b[ MAXCHAR + 2 ], i, p0, p1, treshold, pivot = -1, aC = 0, trueMax = 0, sumLen;
int inFileOffst, pOffset, tt[ MAXCHAR + 1 ], sf[ MAXCHAR + 3 ], sf2[ MAXCHAR + 3 ],
sc[ MAXCHAR + 3 ], pendPos[ MAXCHAR + 3 ], order[ MAXCHAR + 3 ]; // some sf- symbol freq. are not needed
unsigned long rleSeq = 0; // rleSeq - length of RLE sequence
unsigned long r2[ 8 ]; // approximate RLE distribution
long rank = 0, rdiv = 0; // rdiv - rank modulo tree size
unsigned long RLEfreq[ MAXCHAR + 1 ], RLEtop[ 4 ], RLEdistEst = 0, RNKcnt = 0, RNKcnt2 = 0;
unsigned int * bq;
unsigned char * data, * bQlfc, * bRLE, stripFilters = 0, globalMode = 0; // compression mode, 0 = default, 1 = fast
unsigned char binOut = 0, bits[ MAXCHAR + 1 ], bitH[ MAXCHAR + 1 ] = { 0 }; // bit history (7 bits);
unsigned char lbit[ CTXs ] = { 0 };
ofstream * outF;
clock_t start = clock();
/*
Precompute symbol freq. table with assumption that frequency of
higher ranks will gradually decrease after qlfc/mtf transform
*/
void FillInAritfq( void ) {
int t = TREESIZE, f[ 25258 /*7355*/ ];
//#pragma omp parallel for
for( int i = 0; i <= 14670; i++ ) f[ i ] = ( 14670 + 1 - i ) << 1;
//#pragma omp parallel for
for( int i = 0; i <= 14670; i++ ) {
int b = 0;
int m = ( i ) % TREESIZE;
if( m >= 8 ) b = 1, fq[ p( 0, i / t, b ) ] += f[ i ];
else b = 0, fq[ p( 0, i / t, b ) ] += f[ i ], fq[ p( 0, i / t, 1 ) ] += f[ i ];
if( m <= 2 ) {
if( CTXs > 1 ) b = 1, fq[ p( 1, i / t, b ) ] += f[ i ];
if( m == 0 ) {
if( CTXs > 2 ) b = 0, fq[ p( 2, i / t, b ) ] += f[ i ], fq[ p( 2, i / t, 1 ) ] += f[ i ];
} else {
if( CTXs > 2 ) b = 1, fq[ p( 2, i / t, b ) ] += f[ i ];
if( m == 1 ) {
if( CTXs > 3 ) b = 0, fq[ p( 3, i / t, b ) ] += f[ i ], fq[ p( 3, i / t, 1 ) ] += f[ i ];
} else {
if( CTXs > 3 ) b = 1, fq[ p( 3, i / t, b ) ] += f[ i ];
}
}
} else {
if( CTXs > 1 ) b = 0, fq[ p( 1, i / t, b ) ] += f[ i ], fq[ p( 1, i / t, 1 ) ] += f[ i ];
if( m <= 4 ) {
if( CTXs > 4 ) b = 0, fq[ p( 4, i / t, b ) ] += f[ i ], fq[ p( 4, i / t, 1 ) ] += f[ i ];
if( m == 3 ) {
if( CTXs > 6 ) b = 1, fq[ p( 6, i / t, b ) ] += f[ i ];
} else {
if( CTXs > 6 ) b = 0, fq[ p( 6, i / t, b ) ] += f[ i ], fq[ p( 6, i / t, 1 ) ] += f[ i ];
}
} else {
if( CTXs > 4 ) b = 1, fq[ p( 4, i / t, b ) ] += f[ i ];
if( m <= 6 ) {
if( CTXs > 5 ) b = 1, fq[ p( 5, i / t, b ) ] += f[ i ];
if( CTXs > 8 ) b = 1, fq[ p( 8, i / t, b ) ] += f[ i ];
if( CTXs > 10) b = 1, fq[ p( 10, i / t, b ) ] += f[ i ];
if( m == 5 ) {
if( CTXs > 7 ) b = 1, fq[ p( 7, i / t, b ) ] += f[ i ];
if( CTXs > 9 ) b = 1, fq[ p( 9, i / t, b ) ] += f[ i ];
} else {
if( CTXs > 7 ) b = 0, fq[ p( 7, i / t, b ) ] += f[ i ], fq[ p( 7, i / t, 1 ) ] += f[ i ];
if( CTXs > 9 ) b = 0, fq[ p( 9, i / t, b ) ] += f[ i ], fq[ p( 9, i / t, 1 ) ] += f[ i ];
}
} else {
if( CTXs > 5 ) b = 0, fq[ p( 5, i / t, b ) ] += f[ i ], fq[ p( 5, i / t, 1 ) ] += f[ i ];
if( CTXs > 8 ) b = 0, fq[ p( 8, i / t, b ) ] += f[ i ], fq[ p( 8, i / t, 1 ) ] += f[ i ];
if( CTXs > 10) b = 0, fq[ p( 10, i / t, b ) ] += f[ i ], fq[ p( 10, i / t, 1 ) ] += f[ i ];
}
}
}
}
}
/*
Initialize range decoder, parse compressed file
*/
int DeBinRCInit( char * deFile ) {
ifstream file( deFile, ios::in | ios::binary | ios::ate );
unsigned long citO[ CTXs ], pom = 0;
int ctxMark = 16383, ctxCount = 0, ctxO = 0; // read used contexts
long sh;
if( file.is_open() )
{
size = ( unsigned long ) file.tellg();
sh = size;
file.seekg( sh - ( sizeof( char ) + sizeof( char ) ), ios::beg );
file.read( ( char *) &( trueMax ), sizeof( char ) );
file.read( ( char *) &( globalMode ), sizeof( char ) );
RLEdistEst = globalMode >> 4;
binOut = ( globalMode >> 1 ) & 1;
globalMode &= 0x01;
int sTail = ( 2 * sizeof( char ) ) + sizeof( char );
if( globalMode != 1 ) {
sTail += sizeof( unsigned int ) + sizeof( unsigned int );
file.seekg( sh - ( sTail - sizeof( char ) ), ios::beg );
file.read( ( char *) &( RNKcnt ), sizeof( unsigned int ) );
file.read( ( char *) &( RNKcnt2 ), sizeof( unsigned int ) );
}
if( trueMax < 10 || binOut ) {
ctxO = 2;
file.seekg( sh - sizeof( char ) - sTail, ios::beg );
file.read( ( char *) &( ctxMark ), 2 );
}
if( globalMode == 1 ) ctxMark &= 2047;
for( i = 0; i < CTXs; i++ ) if( ( ( ctxMark >> i ) & 1 ) == 1 ) ctxCount++;
int sub = 0, pairs = 0;
if( trueMax > 0 && trueMax < MAXCHAR - 1 ) {
file.seekg( sh - sTail - ctxO, ios::beg );
file.read( ( char *) &( pairs ), sizeof( char ) );
if( pairs > 0 ) {
file.seekg( sh - ( ( ( pairs * 2 ) ) * sizeof( char ) ) - sTail - ctxO, ios::beg );
for( i = 0; i < pairs; i++ ) {
file.read( ( char *) &( p0 ), sizeof( char ) );
file.read( ( char *) &( p1 ), sizeof( char ) );
if( p1 > p0 )
for( int r = p0; r <= p1; r++ ) storedChars[ sub++ ] = r;
}
}
}
sTail = ( globalMode != 1 ? sizeof( int )<<1 : 0 );
int remCnt = 0, dex = sTail + ( ( trueMax > 0 && trueMax < MAXCHAR - 1) ? 2 : 1 );
if( trueMax <= 127 ) remCnt = trueMax + 1; else remCnt = MAXCHAR + 1 - ( trueMax + 1 );
if( remCnt - sub > 0 ) {
remCnt -= sub;
file.seekg( sh - (( dex + ( pairs << 1 ) + remCnt ) * sizeof( char ) ) - ctxO - 1, ios::beg );
for( i = 0; i < remCnt; i++ ) {
file.read( ( char *) &storedChars[ sub++ ], sizeof( char ) );
}
remCnt += pairs << 1;
} else remCnt = pairs << 1;
file.seekg( sh - ( ( dex + remCnt ) * sizeof( char ) ) - ( ctxCount * sizeof( int ) ) -
3 * sizeof( int ) - ctxO - 1, ios::beg );
file.read( ( char *) &( bwtIndex ), sizeof( int ) );
file.read( ( char *) &( sf2[ 0 ] ), 2 * sizeof( int ) );
file.read( ( char *) &( pos[ 0 ] ), /*CTXs*/ctxCount * sizeof( int ) );
for( int ctx = 0; ctx < CTXs; ctx++ ) {
if( ( ( ctxMark >> ctx) & 1 ) == 1 ) citO[ ctx ] = pos[ pom++ ]; else citO[ ctx ] = 0;
}
sf2[ MAXCHAR + 1 ] = sf2[ 1 ];
sumLen = sf2[ 1 ];
for( i = 1; i < MAXCHAR + 1; i++ ) sf2[ i ] = -1;
remCnt = sub;
if( remCnt == 0 )
for( i = 1; i < MAXCHAR + 1; i++ ) sf2[ i ] = 1; // all symbols will be used
if( trueMax <= 127 )
for( i = 0; i < remCnt; i++ ) sf2[ ( unsigned char ) storedChars[ i ] + 1 ] = 1;
if( trueMax > 127 && trueMax < MAXCHAR ) {
for( i = 1; i < MAXCHAR + 1; i++ ) sf2[ i ] = 1; // all symbols will be used
for( i = 0; i < remCnt; i++ ) sf2[ ( unsigned char )storedChars[ i ] + 1 ] = -1; // mask unused symbols
}
file.seekg( 0, ios::beg );
}
for( int ctx = 0; ctx < CTXs; ctx++ )
{
if( citO[ ctx ] == 0 ) continue;
inBuf[ ctx ] = (char*) malloc ( citO[ ctx ] + 8 );
if( inBuf[ ctx ] == NULL ) cout << "\n Not enough free memory", exit( 1 );
file.read( ( char *) &inBuf[ ctx ][ 0 ], citO[ ctx ] );
#ifdef Build64bit
for( int x = 0; x < 8; x++ ) {
#endif
#ifdef Build32bit
for( int x = 0; x < 4; x++ ) {
#endif
code[ ctx ] <<= 8;
code[ ctx ] += ( unsigned char )inBuf[ ctx ][ x ];
}
}
return 0;
}
void Bin( int ctx, unsigned char cC, bool finish = false ) {
isCTXUsed[ ctx ] = 1;
// if finish == TRUE, write out compressed buffer
if( finish ) outF->write( ( char *) &outBuf[ ctx ][ 0 ], pos[ ctx ] );
outBuf[ ctx ][ pos[ ctx ] ] = ( outBuf[ ctx ][ pos[ ctx ] ] << 1 ) + cC;
if( ( ++RCbufP[ ctx ] & 7 ) == 0 ) pos[ ctx ]++;
}
//inline
bool DeBin( int ctx ) {
#ifdef Build64bit
bool bin = ( ( code[ ctx ] >> 63 ) );
#else
bool bin = ( code[ ctx ] >> 31 );
#endif
code[ ctx ] = GetRCb( code[ ctx ], pozInBufBin[ ctx ] ), pozInBufBin[ ctx ]++;
return bin;
}
#define STATEMIX_23 \
if( ctx == 2 && cC ) fq[ p( 3, sub, 1 ) ] += 1, fq[ p( 3, sub, 0 ) ] += 1; \
if( ctx == 10 && sub == 0 && cC ) fq[ p( 10, 1, 1 ) ] += 1, fq[ p( 10, 1, 0 ) ] += 1; \
if( ctx == 3 ) { if( cC == 0 ) { if( fq[ p( 2, sub, 1 ) ] > 10 ) fq[ p( 2, sub, 1 ) ] -= 10; \
} else fq[ p( 2, sub, 1 ) ] += 24; \
}
int eff, ef[ CTXs ] = { 100 }, ef2[ CTXs ] = { 100 };
// update character cum. prob. according bit history (the last 7 bits), or without it
#define UPDATE_PROBABILITY( b ) \
if( ef[ b ] > 7900 ) \
ef[ b ] >>= 1, ef2[ b ] >>= 1; \
eff = ( ef[ b ] + 1 ) / ( ef2[ b ] + 1 ); \
eff = 100 + ( eff > 8 ?( eff > 10 ? 59 : 29 ): 8 ); \
if( ( ctx == 11 || ( ctx >= 2 && ctx <= 8 ) ) && sub <= 2 ) { \
unsigned char id = ( ( ctx - 2 ) << 2 ) + sub, bitC = bits[ bitH[ id ] = ( bitH[ id ] << 1 ) + cC ]; \
if( cC == 0 ) { \
if( bitC <= 1 ) id = eff - ( 64 - ( bitC ? 48 : 0 ) ), fq[ ft[ 0 ] ] += id, fq[ ft[ 1 ] ] += id; \
else fq[ ft[ 0 ] ] += eff + 8, fq[ ft[ 1 ] ] += eff + 8; \
} else { \
if( bitC >= 5 ) fq[ ft[ cC ] ] += eff - ( 16 - ( ( 7 - bitC ) << 1 ) ); \
else fq[ ft[ 1 ] ] += eff + 2; \
} \
} else { \
fq[ ft[ cC ] ] += eff; \
if( cC == 0 ) fq[ ft[ 1 ] ] += eff; \
} \
ft[ 3 ] = p( ctx, sub, 3), sRt[ ft[ 0 ] ]++;
// context-adaptive hash for freq. table re-scale, handled via &3
// possibly little help only for context 2,(or below) which is quite redundand
// edit: added context 11 (it's derived from context 2)
// -don't go above 2^14 in total cumulative freq, the bottom limit
#define FREQUENCY_TABLE_RESCALE \
if( fq[ ft[ 1 ] ] >= 13900 || ((sRt[ ft[ 0 ]] >= ( sRt[ ft[ 3 ] ] >> 2 ) + 30 && \
( ctx == 2 || ctx == 11 ) ) ) ) \
{ \
if( ctx == 2 || ctx == 11 ) { \
ft[ 2 ] = p( ctx, sub, 2 ), sRt[ ft[ 1 ] ]++; \
sRt[ ft[ sRt[ ft[ 1 ] ] & 3 ] ] = sRt[ ft[ 0 ] ], sRt[ ft[ 0 ] ] = 0; \
} \
fq[ ft[ 1 ] ] -= fq[ ft[ 0 ] ] >> 1, fq[ ft[ 0 ] ] -= fq[ ft[ 0 ]] >> 1; \
fq[ ft[ 1 ] ] -= ( fq[ ft[ 1 ] ] - fq[ ft[ 0 ] ] ) >> 1; \
};
/*
Range decoder, based on Subbotin's RC, ft-pointer into symbol cum.prob.
table fq, which is 2D array, stored 1D. ctx-context, sub-subcontext
(sharing the same Low, Range & code-space), cC-coded character
*/
#ifdef DEBUG
long binRCcnt[ CTXs ] = { 0 }, bitRLECcnt[ CTXs ] = { 0 }, treeRLEcnt = 0;
#endif
unsigned int DeBinRC( int ctx, int sub, unsigned char cC = 0 ) {
#ifdef DEBUG
binRCcnt[ ctx ]++;
#endif
if( binOut ) return DeBin( ctx );
unsigned long ft[ 4 ], total = fq[ ft[ 1 ] = p( ctx, sub, 1 ) ];
ft[ 0 ] = p( ctx, sub, 0 );
RCRange[ ctx ] /= total;
cC = ( ( ( code[ ctx ] - RCLow[ ctx ] ) / ( RCRange[ ctx ] ) ) >= fq[ ft[ 0 ] ] );
RCLow[ ctx ] += ( cC == 0 ? 0 : fq[ ft[ 0 ] ] ) * RCRange[ ctx ];
RCRange[ ctx ] *= ( unsigned long ) ( cC == 0 ? fq[ ft[ 0 ] ] : total - fq[ ft[ 0 ] ] );
STATEMIX_23
UPDATE_PROBABILITY( ctx )
FREQUENCY_TABLE_RESCALE
ef[ ctx ]++;
LR[ ctx ] = RCLow[ ctx ] + RCRange[ ctx ];
while( (
//( readLow[ ctx ]->b4 == readLr[ ctx ]->b4 ) ) || //comparision instead xor, seems not work yet
( RCLow[ ctx ] ^ ( LR[ ctx ] ) ) < TOP ) ||
( RCRange[ ctx ] <= 0xFFFF && ( (RCRange[ ctx ] = -RCLow[ ctx ] ), 1 ) ) ) {
ef2[ ctx ]++;
#ifdef Build64bit
// byte-aligned input
code[ ctx ] = ( code[ ctx ] << ALI ) + ( unsigned char ) inBuf[ ctx ][ pozInBufBin[ ctx ]++ ];
#else
// bit-aligned input
code[ ctx ] = GetRCb(code[ ctx ], pozInBufBin[ ctx ] ), pozInBufBin[ ctx ]++;
#endif
RCLow[ ctx ] <<= ALI, RCRange[ ctx ] <<= ALI, LR[ ctx ] <<= ALI; // RCLow[ctx] + RCRange[ctx];
}
return cC;
}
/*
Range coder, based on Subbotin's RC, ft-pointer into symbol freq.
table fq, which is 2D array, stored 1D. ctx-context, sub-subcontext
(sharing the same Low, Range & code-space), cC-coded character
*/
void BinRC( int ctx, int sub, unsigned char cC, bool finish = false ) {
#ifdef DEBUG
binRCcnt[ ctx ]++;
#endif
if( binOut ) { Bin( ctx, cC, finish ); return; }
unsigned long ft[ 4 ], total = fq[ ft[ 1 ] = p( ctx, sub, 1 ) ]; isCTXUsed[ ctx ] = 1;
ft[ 0 ] = p( ctx, sub, 0 );
RCRange[ ctx ] /= total;
RCLow[ ctx ] += ( cC ? fq[ ft[ 0] ] : 0 ) * RCRange[ ctx ];
RCRange[ ctx ] *= ( cC ? total - fq[ ft[ 0 ] ] : fq[ ft[ 0 ] ] );
STATEMIX_23
UPDATE_PROBABILITY( ctx )
FREQUENCY_TABLE_RESCALE
ef[ ctx ]++;
// if finish == TRUE, write out compressed buffer
if( finish ) outF->write( ( char *) &outBuf[ ctx ][ 0 ], pos[ ctx ] );
LR[ ctx ] = RCLow[ ctx ] + RCRange[ ctx ];
while( (
//( readLow[ ctx ]->b4 == readLr[ ctx ]->b4 ) ) || // comparision instead ^, somehow buggy
( RCLow[ ctx ] ^ ( LR[ ctx ] ) ) < TOP ) ||
( RCRange[ ctx ] <= 0xFFFF && ( ( RCRange[ ctx ] = -RCLow[ ctx ] ), 1 ) ) ) {
ef2[ ctx ]++;
#ifdef Build64bit
outBuf[ ctx ][ pos[ ctx ]++ ] = ( RCLow[ ctx ] >> 32 ) >> 24; // >> 56, byte aligned output
//outBuf[ ctx ][ pos[ ctx ]++ ] = readLow[ ctx ]->b4; // byte aligned output (64bit emulation)
#else
// (seem larger aligments, up to 1Byte, are less error prone than 1bit with a 32bit Low)
if( ( ++RCbufP[ ctx ] & ( ( 8 / ALI ) - 1 ) ) == 1 )
outBuf[ ctx ][ pos[ ctx ] ] = readLow[ ctx ]->b4 >> ( 8 - ALI );
else outBuf[ ctx ][ pos[ ctx ] ] =
( outBuf[ ctx ][ pos[ ctx ] ] << ALI ) + ( readLow[ ctx ]->b4 >> ( 8 - ALI) ),
( ( RCbufP[ctx] & ( ( 8 / ALI ) - 1) ) != 0 ) ? 1 : pos[ ctx ]++;
#endif
RCLow[ ctx ] <<= ALI, RCRange[ ctx ] <<= ALI, LR[ ctx ] <<= ALI; //RCLow[ctx] + RCRange[ctx];
}
lbit[ ctx ] = cC;
}
long rleDelayedMantisaCTX[ CTXs ] = { 0 }, rleAvgCTX[ CTXs ][2] = { {1},{1} }, s00[ CTXs ] = { 0 };
unsigned long deBitRLE( int b, int levl, int d ) {
#ifdef DEBUG
bitRLECcnt[ b ]++;
#endif
unsigned long l = 0, sl = 0, ret = 0;
// decode mantisa
while( DeBinRC( 0 + b, rleDelayedMantisaCTX[ b ] + levl + ( ( s00[ b ] & 0xE ) > 0 ) + l++ ) == 0 ) ;
s00[ b ] = ( ( s00[ b ] << 1 ) + ( l <= 1 ) ) & 0xF;
#ifdef DelayRleBitMantisaCtx
if( b != 11 ) // delayed context back for mantisa, may hurt compression on small files
rleDelayedMantisaCTX[ b ] = l << 7;
#endif
// decode rest of RLE
for( unsigned int t = 0; t < l; t++ ) ret |= DeBinRC( 0 + b, 40 + levl + ( l << 7 ) + sl++ ) << t;
return ( ret | ( 1 << l ) ) - 1;
}
// beware, mantisa and rest of RLE is packed into same context to save memory
// it may hurt compression if they mix by an accident
void bitRLE( int b, int p, int levl ) {
#ifdef DEBUG
bitRLECcnt[ b ]++;
#endif
/*unsigned*/ long seq = r2[ p ] + 1, l = 0, sl = 0, m;
if( seq <= 1 ) return; // sequence was of zero length
m = seq; // first encode mantisa
while( m > 1 ) BinRC( 0 + b, rleDelayedMantisaCTX[ b ] + levl + ( ( s00[ b ] & 0xE ) > 0 ) + l++,
( ( ( m >>= 1 ) > 1 ) ? 0 : 1 ), false );
s00[ b ] = ( ( s00[ b ] << 1 ) + ( l <= 1 /*seq <= 4*/ ) ) & 0xF;
l <<= 7; // mantisa will chose context of RLE-bit, according to its length l
while( seq > 1 ) BinRC( 0 + b, 40 + levl + ( l ) + sl++, (seq & 1), false ), seq >>= 1;
#ifdef DelayRleBitMantisaCtx
if( b != 11 ) // delayed context back for mantisa, may hurt compression on small files
rleDelayedMantisaCTX[ b ] = l;
#endif
}
bool decodeStart = true;
long forwr = 0, d1 = 0, d2 = 0, r24 = 0, a3 = 0, a1b = -1;
long ncdiv = -1, scdiv = -1, firstRun = 1, nc2 = -1, c4 = 0;
long nc4 = 0, nc8 = 8, ls0 = -1, o2 = 0, o3[ 0xFF ] = { 0 };
#define INIT_DeTreeRLE firstRun = 1, nc2 = ncdiv = scdiv = -1, c4 = 0, nc4 = 0, \
nc8 = 8, ls0 = -1, decodeStart = true, forwr = 0, r24 = 0, o2 = 0, a3 = 0; \
for(int s = 0; s <= 100; s++) o3[ s ] = 0;
void deTreeRLE_R25_Init( ) {
a1b = 2;
if( a3 <= 2 ) {
a1b = ( DeBinRC( 10, 0 ) == 0 ), a3 = -1;
} else a3 = 0;
if( a1b > 0 )
r2[ 5 ] = deBitRLE( 10, 0, 8 ) + ( a3 < 0 );
else r2[ 5 ] = 1;
a3 += r2[ 5 ]; // - ( a1b != 2 );
}
void deTreeRLEInit( ) {
d2 = deBitRLE( 0, 0, 8);
r2[ 4 ] = 2;
if( d2 != 1 )
r2[ 4 ] = 1 + ( ( 1 - DeBinRC( 1, 0 ) ) << 1 );
r24 = r2[ 4 ];
if( r2[ 4 ] > 1 )
r2[ 4 ] += deBitRLE( 1, 0, 8 ) - 2;
}
int deTreeRLE( int mode, int rdivInit = 0 )
{
#ifdef DEBUG
treeRLEcnt++;
#endif
long rt = 0, rnk = 0, c2 = 0, b4 = 0, b8, rdiv0, rdiv0F;
rdiv = 0;
if( firstRun ) {
if( r2[ 4 ] == 1 )
{
rdiv = decodeStart ? rdivInit : forwr;
forwr = deBitRLE( 0, 0, 8 );
r2[ 4 ] = 2;
if( ( rdiv != 1 && forwr != 1 ) || r24 == 1 )
r2[ 4 ] = 1 + ( ( 1 - DeBinRC( 1, 0 ) ) << 1 );
r24 = r2[ 4 ];
if( r2[ 4 ] > 1 )
r2[ 4 ] += deBitRLE( 1, 0, 8 ) - 2;
decodeStart = false;
} else r2[ 4 ]--;
}
firstRun = 0;
if( ncdiv != -1 ) rdiv = ncdiv;
if( scdiv == -1 ) {
if( r2[ 4 ] == 1 ) {
ncdiv = decodeStart ? rdivInit : forwr;
forwr = deBitRLE( 0, 0, 8 );
r2[ 4 ] = 2;
if( ( ncdiv != 1 && forwr != 1 ) || r24 ==1 )
r2[ 4 ] = 1 + ( ( 1 - DeBinRC( 1, 0 ) ) << 1 );
r24 = r2[ 4 ];
if( r2[ 4 ] > 1 )
r2[ 4 ] += deBitRLE( 1, 0, 8 ) - 2;
decodeStart = false;
}
else r2[ 4 ]--, ncdiv = 0;
} else ncdiv = scdiv;
if( r2[ 4 ] == 1 ) {
scdiv = decodeStart ? rdivInit : forwr;
forwr = deBitRLE( 0, 0, 8 );
r2[ 4 ] = 2;
if( ( scdiv != 1 && forwr != 1 ) || r24 == 1 )
r2[ 4 ] = 1 + ( ( 1 - DeBinRC( 1, 0 ) ) << 1 );
r24 = r2[ 4 ];
if( r2[ 4 ] > 1 )
r2[ 4 ] += deBitRLE( 1, 0, 8 ) - 2;
decodeStart = false;
} else r2[ 4 ]--, scdiv = 0;
ls0 -= ( rnk += TREESIZE * rdiv );
rdiv0 = rdiv; // base value
rdiv0F = rdiv & 0xF;
int rdivTresh = rdiv, ncdTresh = ncdiv;
if( TREETRESH && rdivTresh > TREETRESH ) rdivTresh = TREETRESH;
if( TREETRESH && ncdTresh > TREETRESH ) ncdTresh = TREETRESH;
long r = ( rdivTresh << 1 ) + ( ( ncdiv > rdiv ) | ( ls0 >= TREESIZE ) ),
ncd = ( ncdTresh << 1 ) + ( ( scdiv > ncdiv ) | ( rdiv > ncdiv ) );
if( ( ncd ) >= SubCTXs - 1 ) ncd = SubCTXs - 2; // buffer overflow prottection
if( ( r ) >= SubCTXs - 1 ) r = SubCTXs - 2;
if( ( r <<= 1 ) >= SubCTXs - 1 ) r = SubCTXs - 2;
r += ( ( o2 & 0x1E ) != 0 );
if( ( ncd <<= 1 ) >= SubCTXs - 1 ) ncd = SubCTXs - 2;
ncd += ( ( ( ( ( o2 << 1 ) + ( rdiv > 0 ) ) & 0x1F ) & 0x1E ) != 0 );
if( mode ) {
if( nc2 == -1 ) {
c2 = ( rdiv == 0 && r2[ 5 ] == 1 ) ||
( rdiv > 0 && DeBinRC( 9, r ) );
if( rdiv == 0 ) {
if( c2 ) {
deTreeRLE_R25_Init( );
} else r2[ 5 ]--;
}
} else c2 = nc2;
nc2 = ( ncdiv == 0 && r2[ 5 ] == 1 ) ||
( ncdiv > 0 && DeBinRC( 9, ncd ) );
if( ncdiv == 0 ) {
if( nc2 ) {
deTreeRLE_R25_Init( );
} else r2[ 5 ]--;
}
} else { // mode == 0
if( nc2 == -1 ) {
c2 = DeBinRC( 9, r );
} else c2 = nc2;
nc2 = DeBinRC( 9, ncd );
}
if( c2 )
{ // rank > 2
if( c4 == 0 && ( b4 = DeBinRC( 4, r ) ) && ( b8 = DeBinRC( binOut ? 4 : 8, r ) ) ) { };
if( c4 && ( b4 = nc4 ) ) b8 = nc8;
if( nc2 && ncdiv == rdiv ) {
c4 = 1, nc8 = 0, nc4 = DeBinRC( 4, ncd );
if( nc4 ) nc8 = DeBinRC( binOut ? 4 : 8, ncd );
} else c4 = 0, nc4 = nc8 = ( ncdiv > rdiv ); // no direct ctx read
if( TREETRESH && rdiv > TREETRESH ) rdiv = TREETRESH;
if( ( rdiv <<= 1 ) >= SubCTXs - 1 ) rdiv = SubCTXs - 2; // buffer overflow prottection
if( b4 == 0 )
rnk += 3 + DeBinRC( 6, rdiv + ( nc4 | ( ls0 >= 4 ) | ( ( o3[ 60 ] & 0xE ) > 0 ) ) );
else {
if( b8 ) rnk += 7 + DeBinRC( 5, r );
else rnk += 5 + DeBinRC( binOut ? 6 : 7, rdiv + ( nc8 | ( ls0 >= 6 ) |
( ( o3[ 80 ] & 0xE ) > 0 ) ) );
}
} // <= 2
else {
if( TREETRESH && rdiv > TREETRESH ) rdiv = TREETRESH;
if( ( rdiv <<= 1 ) >= SubCTXs - 1 ) rdiv = SubCTXs - 2; // overflow protection
rdiv += ( ( nc2 & ( ncdiv == rdiv0 ) ) | ( ncdiv > rdiv0 ) | ( ls0 >= 2 ) |
( ( o3[ rdiv0F ] & 0xE ) > 0 ) );
if( DeBinRC( 2, rdiv ) ) rnk += 1 + DeBinRC( 3, rdiv );
}
o2 = ( ( o2 << 1 ) + ( rdiv0 > 0 ) ) & 0x1F;
o3[ rdiv0F ] = ( ( o3[ rdiv0F ] << 1 ) + ( ( rt = rnk % TREESIZE ) >= 2 ) ) & 0xF;
o3[ 60 ] = ( ( o3[ 60 ] << 1 ) + ( rt >= 5 ) ) & 0xF;
o3[ 80 ] = ( ( o3[ 80 ] << 1 ) + ( rt >= 7 ) ) & 0xF;
return ls0 = rnk;
}
long pp1 = -1, pz1, pz2, pr1 = -1, ps1 = -1, st1, d0 = -1, ordiv, rdivR2;
void treeRLE( long rnk, int mode )
{
pp1 = ps1, pz2 = pz1 = rnk, ps1 = rnk = pr1, pr1 = pz1;
if( rnk < 0 ) return;
#ifdef DEBUG
treeRLEcnt++;
#endif
if( ( ordiv = rdiv = rnk / TREESIZE) > 0 )
{
r2[ 3 ] = rdiv;
if( TREETRESH && rdiv > TREETRESH ) rdiv = TREETRESH;
bitRLE( 0, 3, 0);
if( ( d2 != 1 && r2[ 3 ] != 1 ) || d0 == 0 )
BinRC( 1, 0, r2[ 4 ] == 1, false ), r2[ 4 ]--;
d0 = r2[ 4 ];
if( r2[ 4 ] > 0 )
bitRLE( 1, 4, 0 );
d2 = r2[ 3 ];
r2[ 4 ] = 1;
if( ( rdiv <<= 1 ) >= SubCTXs - 1 ) rdiv = SubCTXs - 2; // buffer overflow prottection
rnk %= TREESIZE;
} else r2[ 4 ]++;
pp1 -= ps1 - rnk, pz2 -= ps1 - rnk;
rdiv += ( st1 = ( ( pz2 >= TREESIZE ) | ( pp1 >= TREESIZE ) ) );
rdivR2 = ( ( rdiv << 1 ) >= SubCTXs - 1 ) ? SubCTXs - 2 : ( rdiv << 1 ); // buffer overflow prottection
rdivR2 += ( ( o2 & 0x1E ) != 0 ); // 1E - previous rdiv used by pp1 already
if( rnk <= 2 ) {
if( mode == 0 || rdiv > 1 )
BinRC( 9, rdivR2, 0, false ); // rdiv! ending footer for rc9
else
r2[ 5 ]++;
rdiv = rdiv - st1 + ( ( pz2 >= 3 ) | ( pp1 >= 2 ) | ( ( o3[ ordiv & 0xF ] & 0xE ) > 0 ) );
if( rnk == 0 ) BinRC( 2, rdiv, 0, false );
else
BinRC( 2, rdiv, 1, false ), BinRC( 3, rdiv, rnk == 2, false );
} else {
if( mode == 0 || rdiv > 1 )
BinRC( 9, rdivR2, 1, false ); // rdiv! ending footer for rc9
else {
if( a3 <= 2 )
BinRC( 10, 0, r2[ 5 ] == 1, false ), r2[ 5 ]--;
a3 = r2[ 5 ];
if( r2[ 5 ] > 0 )
bitRLE( 10, 5, 0 );
r2[ 5 ] = 1;
}
if( rnk <= 4 )
BinRC( 4, rdivR2, 0, false ),
BinRC( 6, rdiv - st1 + ( ( pz2 >= 5 ) | ( pp1 >= 4 ) |
( ( o3[ 60 ] & 0xE ) != 0 ) ), rnk == 4, false ); // 5
else {
BinRC( 4, rdivR2, 1, false );
if( rnk <= 6 )
BinRC( binOut ? 4 : 8, rdivR2, 0, false ),
BinRC( binOut ? 6 : 7, rdiv - st1 + ( ( pz2 >= 7 ) |
( pp1 >= 6 ) | ( ( o3[ 80 ] & 0xE ) != 0 ) ), rnk == 6, false ); // 7
else
BinRC( binOut ? 4 : 8, rdivR2, 1, false ), BinRC( 5, rdivR2, rnk == 8, false );
}
}
o2 = ( ( o2 << 1 ) + ( rdiv > 1 ) ) & 0x1F;
o3[ ordiv & 0xF ] = ( ( o3[ ordiv & 0xF ] << 1 ) + ( rnk >= 2 ) ) & 0xF;
o3[ 60 ] = ( ( o3[ 60 ] << 1 ) + ( rnk >= 5 ) ) & 0xF;
o3[ 80 ] = ( ( o3[ 80 ] << 1 ) + ( rnk >= 7 ) ) & 0xF;
}
#ifdef DEBUG
int pass = 0;
void stats_debug_pass( ) {
cout<<"\n--------------------------------------";
cout<<"\n-------------- pass " << ( pass++ ) << " ----------------";
cout<<"\n--------------------------------------";
for(int f=0; f<CTXs; f++)
cout<<"\n binRCcnt["<<f<<"]: "<< binRCcnt[f];
cout<<"\n--------------------------------------";
for(int f=0; f<CTXs; f++)
cout<<"\n bitRLECcnt["<<f<<"]: "<< bitRLECcnt[f];
cout<<"\n--------------------------------------";
cout<<"\n treeRLEcnt: "<< treeRLEcnt;
}
#endif
int decode( char * deFile ) {
DeBinRCInit( deFile );
int rankCount = ( sumLen == 0 ? 0 : sf2[ 0 ] ), ouBrlePos = 0; sf2[ 0 ] = 0;
unsigned char *ouBrle, *ouBrank;
ouBrle = ( unsigned char *) malloc( sumLen + ( trueMax << 4 ) + 2 );
if( ouBrle == NULL ) cout << "\n Not enough free memory", exit( 1 );
memset( ouBrle, 0, sumLen + ( trueMax << 4 ) + 2 );
ouBrank = ( unsigned char *) malloc( rankCount + trueMax + 2 );
if( ouBrank == NULL ) cout << "\n Not enough free memory", exit( 1 );
memset( ouBrank, 0, rankCount + trueMax + 2 );
if( rankCount != 0 ) {
if( ( int )( unsigned char ) globalMode == 0 ) {
unsigned long bulk, upRankCount = 0;
if( RNKcnt ) {
unsigned long nextp = rankCount + trueMax + 1 - RNKcnt, nextp2 = 0, lrank = 0, rd0cnt = 0;
// read rdiv for characters and rle ( rank / TREESIZE )
// init
deTreeRLEInit();
deTreeRLE_R25_Init( );
for( unsigned long ouBrnkPos = nextp, it = 0; it < (RNKcnt+0); it++) {
rdiv = binOut ? deTreeRLE( 1, d2 ) + 1 : deTreeRLE( 1, d2 );
ouBrank[ ++ouBrnkPos ] = rdiv + 1;
}
INIT_DeTreeRLE
#ifdef DEBUG
stats_debug_pass( );
#endif
deTreeRLEInit(); if( binOut ) r2[ 5 ] = deBitRLE( 10, 0, 8 );
unsigned long ouBrnkPs = 0;
for( unsigned long it = 0, lxtr = 0; it < RNKcnt+1; it++) {
bulk = 2;
++nextp;
if( ( ( lrank != 2 && ouBrank[ nextp + 0 ] != 2 ) || lxtr == 1 ) && it < RNKcnt ) {
lxtr = DeBinRC( 10, 0 );
bulk = 1 + ( ( lxtr == 0 ) << 1 );
} else lxtr = 0;
lrank = ouBrank[ nextp + 0 ];
if( bulk > 1 ) {
bulk += deTreeRLE( binOut, d2 ) - 1;
}
nextp2 += bulk;
if( it == RNKcnt ) break;
upRankCount++;
while( --bulk > 0 ) ouBrank[ouBrnkPs++] = 0;
ouBrank[ ouBrnkPs++ ] = ouBrank[ nextp ];
}
if( bulk > 9 ) deTreeRLE( binOut, d2 );
// zero last characters
memset( ouBrank + ouBrnkPs, 0, rankCount + trueMax + 2 - ouBrnkPs );
INIT_DeTreeRLE
#ifdef DEBUG
stats_debug_pass( );
#endif
RNKcnt = nextp2 - 1;
long a1d = 0, a1c = -1; // init
// add sign if rank is larger than 2 or not, when rdiv == 0
deTreeRLEInit(); if( binOut ) r2[ 5 ] = deBitRLE( 10, 0, 8 );
unsigned long skip = 0;
for( unsigned long it = 0, ouBrnkPos = 0; ( it < RNKcnt - upRankCount ); ) {
a1c = 2;
if( a1d <= 2 && rd0cnt < RNKcnt2 ) {
a1c = DeBinRC( 10, 0 );
}
if( a1c > 0 )
it += ( skip = binOut ? deTreeRLE( 1, d2 ) + 1 + ( a1c == 1 ) :
deTreeRLE( 0, d2 ) + ( a1c == 1 ) );
else it += ( skip = 1 );
a1d = skip - ( a1c != 2 );
if( /*it > RNKcnt - upRankCount ||*/ rd0cnt == RNKcnt2 ) break;
rd0cnt++;
while( skip > 0 ) if( ouBrank[ ouBrnkPos++ ] == 0 ) skip--;
ouBrank[ ouBrnkPos - 1 ] = 1; // mark all symbols larger than 2 and smaller than TREESIZE
}
if( skip >= 9 ) deTreeRLE( 0, d2 );
INIT_DeTreeRLE
#ifdef DEBUG
stats_debug_pass( );
#endif
// compute % TREESIZE for ranks, skip all zeros following escape-symbols(2)
int ppc = -1, nC2 = -1, nextChar, ncdiv, nextSC, nC4, nC8,
c4 = 0, b4 = 0, b8 = 0, o0 = 0, rdiv2;
for( /*unsigned*/ long ouBrnkPos = 0, c2 = 0, l2 = 0, rdiv0, rdiv0F, ncd2;
( unsigned )ouBrnkPos < RNKcnt; )
{
long rank = 0;
if( ouBrank[ ouBrnkPos ] >= 2 ) {
rdiv0 = rdiv = ouBrank[ ouBrnkPos ] - 1;
ppc -= ( rank = rdiv * TREESIZE );
if( ( rdiv <<= 1 ) >= SubCTXs - 1 ) rdiv = SubCTXs - 2; // buffer overflow prottection
if( nC2 == -1 ) { // read first character
l2 = ouBrank[ ouBrnkPos + ( ( unsigned )ouBrnkPos + 1 < RNKcnt ) ],
l2 = l2 ? l2 - 1 : 0;
int rdivR2 = rdiv + ( ( l2 > rdiv0 ) | ( ppc >= TREESIZE ) );
if( ( rdivR2 <<= 1 ) >= SubCTXs - 1 ) rdivR2 = SubCTXs - 2; // buffer overflow prottection
rdivR2 += ( o0 != 0 );
c2 = DeBinRC( 9, rdivR2 );
} else c2 = nC2;
} else rdiv0 = rdiv = 0, c2 = ouBrank[ ouBrnkPos ];
rdiv0F = rdiv0 & 0xF;
nextChar = ( ( unsigned )ouBrnkPos + 1 < RNKcnt );
nextSC = ( ( unsigned )ouBrnkPos + 2 < RNKcnt ),
l2 = ouBrank[ ouBrnkPos + nextChar + nextSC ], l2 = l2 ? l2 - 1 : 0;
if( ( ncdiv = ouBrank[ ouBrnkPos + nextChar ] - 1 ) >= 1 ) {
ncd2 = ( ( ncdiv << 1 ) >= SubCTXs - 1 ) ? SubCTXs - 2 : ncdiv << 1;
int ncdivR2 = ncd2 + ( ( l2 > ncdiv ) | ( rdiv0 > ncdiv ) );
if( ( ncdivR2 <<= 1 ) >= SubCTXs - 1 ) ncdivR2 = SubCTXs - 2; // buffer overflow prottection
ncdivR2 += ( ( ( ( o0 << 1 ) + ( rdiv0 > 0 ) ) & 0xFF ) != 0 );
nC2 = nextChar ?
DeBinRC( 9, ncdivR2 ) : nC2,
rdiv += ( c2 & ( ( ncdiv >= ouBrank[ ouBrnkPos ] ) | ( ppc >= TREESIZE ) ) );
} else ncd2 = ncdiv = 0, nC2 = ouBrank[ ouBrnkPos + nextChar ],
rdiv += ( c2 & ( ppc >= TREESIZE ) );
rdiv2 = ( ( rdiv << 1 ) >= SubCTXs - 1 ) ? SubCTXs - 2 : rdiv << 1;
rdiv2 += ( o0 != 0 );
if( c2 ) { // >= 3
if( c4 == 0 && ( b4 = DeBinRC( 4, rdiv2 ) ) && ( b8 = DeBinRC( 8, rdiv2 ) ) ) { }; //rdiv2
if( c4 && ( b4 = nC4 ) ) b8 = nC8;
if( nC2 && ncdiv == ouBrank[ ouBrnkPos ] - 1 ) {
c4 = 1, nC8 = 0;
ncdiv = ncd2 + ( ( l2 > ncdiv ) | ( rdiv0 > ncdiv ) );
if( ( ncdiv <<= 1 ) >= SubCTXs - 1 ) ncdiv = SubCTXs - 2; // buffer overflow prottection
ncdiv += ( ( ( ( o0 << 1 ) + ( rdiv0 > 0 ) ) & 0xFF ) != 0 );
nC4 = nextChar ? DeBinRC( 4, ncdiv ) : 0;
if( nC4 ) nC8 = nextChar ? DeBinRC( 8, ncdiv ) : nC8;
// no direct ctx read
} else c4 = 0, nC4 = nC8 = ( ncdiv > ouBrank[ ouBrnkPos ] - 1 );
if( b4 == 0 ) rank += DeBinRC( 6, ( rdiv ^ ( rdiv & 1 ) ) +
( nC4 | ( ppc >= 4 ) | ( o3[ 20 + rdiv0F ] > 0 ) ) ) + 3;
else rank += ( b8 ?
DeBinRC( 5, rdiv2 ) + 7 : //rdiv2
DeBinRC( 7, ( rdiv ^ ( rdiv & 1 ) ) + ( nC8 | ( ppc >= 6 ) |
( o3[ 40 + rdiv0F ] > 0 ) ) ) + 5 );
} else { // for ranks <= 2
rdiv += ( ( ppc >= 2 ) |
( ( nC2 & ( ncdiv == rdiv0 ) ) | ( ncdiv > rdiv0 ) ) );
if( DeBinRC( 2, rdiv ) ) rank += DeBinRC( 3, rdiv ) + 1;
}
o0 = ( ( o0 << 1 ) + ( rdiv0 > 0 ) ) & 0xFF;
o3[ 20 + rdiv0F ] = ( ( o3[ 20 + rdiv0F ] << 1 ) + ( ( rank % TREESIZE ) >= 5 ) ) & 0xFF;
o3[ 40 + rdiv0F ] = ( ( o3[ 40 + rdiv0F ] << 1 ) + ( ( rank % TREESIZE ) >= 7 ) ) & 0xFF;
ouBrank[ ouBrnkPos++ ] = ppc = rank;
}
#ifdef DEBUG
stats_debug_pass( );
#endif
} // RNKcnt != 0
// join rle and character buffers ouBrle, ouBrank,
// if symbol >= RLEdistEst read from first rle buffer, otherwise reconstruct second
unsigned long uFP = 1, ouBrnkPos = 0; while( sf2[ uFP ] == -1 ) uFP++;
unsigned long rnkSeq = 0, rnkSeq2 = 0, rleSecCTX = 0, rnkSecCTX = 0, prank = 0;
unsigned long rnk0Seq = 0, rnk1Seq = 0, rnkSecCTX2 = 0, prank1 = 0;
ouBrlePos = 0;
int lr0 = 0, lr1 = 0, lr2 = 0, lr3 = 0, lr4 = 0;
deTreeRLEInit();
if( binOut ) r2[ 0 ] = deBitRLE( 7, 0, 8 ), r2[ 5 ] = deBitRLE( 10, 0, 8 );
for( unsigned int i = 0; i < ( unsigned )rankCount; i++ )
{
rdiv = 0, rnkSeq++;
// read first rle channel
if( prank + rnkSeq < rleSecCTX ) {
if( ( rdiv = deTreeRLE( binOut, d2 ) ) ) rnkSeq = -1;
} else { // read second rle channel
if( ( rdiv = binOut ? ( r2[ 0 ] <= 1 ) : DeBinRC( binOut ? 8 : 10, 0 + ( lr0 > 0 ) ) )
&& DeBinRC( binOut ? 8 : 10, 2 + ( lr1 > 0 ) ) )
rdiv = 2 + deTreeRLE( binOut, d2 );
if( binOut && --r2[ 0 ] <= 0 ) r2[ 0 ] = deBitRLE( 7, 0, 8 );
lr0 = ( ( lr0 << 1 ) + ( rdiv == 0 ) ) & 0xF;
lr1 = ( ( lr1 << 1 ) + ( rdiv >= 2 ) ) & 0xF;
rnkSeq = 0, rleSecCTX = ( ( unsigned )rdiv <= RLEdistEst ? rdiv : RLEdistEst );
}
// recover rank buffer according rank channels
rnkSeq2++; // read first rank channel
if( prank >= ( unsigned )TREESIZE || ( rdiv >> 1 ) + rnkSeq2 < rnkSecCTX ) {
if( ( prank = ouBrank[ ouBrnkPos++ ] ) ) rnkSeq2 = -1;
} else { // read second rank channel
if( ( prank = DeBinRC( 11, 0 + ( lr2 > 0 ) ) ) ) {
rnk1Seq++;
if( prank1 >= ( unsigned )TREESIZE || ( rnk0Seq >> 1 ) + rnk1Seq < rnkSecCTX2 ) {
if( DeBinRC( 11, 2 + ( lr4 > 0 ) ) ) prank = 2 + ouBrank[ ouBrnkPos++ ];
prank1 = prank - 1;
if( ( prank1 > 0 ) ) rnk1Seq = -1;
} else { // read second rank channel
if( ( prank1 = DeBinRC( 11, 4 + ( lr3 > 0 ) ) ) && DeBinRC( 11, 6 + ( lr3 > 0 ) ) )
prank1 = 2 + ouBrank[ ouBrnkPos++ ];
prank = prank1 + 1;
lr3 = ( ( lr3 << 1 ) + ( prank >= 2 ) ) & 0xF;
rnk1Seq = 0, rnkSecCTX2 = ( prank - 1 <= RLEdistEst ? prank - 1 : RLEdistEst );
}
rnk0Seq = 0;
} else rnk0Seq++;
lr4 = ( ( lr4 << 1 ) + ( prank >= 2 ) ) & 0xF;
lr2 = ( ( lr2 << 1 ) + ( prank >= 1 ) ) & 0xF;