-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclrf256.cdr
177 lines (146 loc) · 5.11 KB
/
clrf256.cdr
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
/*
* Note it is up to the calling code to ensure that no overruns on input and
* output buffers occur.
*
* Call the input() and output() functions to set and query the current
* buffer locations.
*
* This version has a specialised versions of Encode and GetFreq named
* Encode256 and GetFreq256. These have the requirement of totFreq <=
* 255. The division is then approximated by a multiply and shift.
* This gives within 0.1% compression ratio but is significantly faster.
*/
#define DO(n) for (int _=0; _<n; _++)
#define TOP (1<<24)
static unsigned short div_lookup[256] = {
0, 65535, 32768, 21845, 16384, 13107, 10922, 9362,
8192, 7281, 6553, 5957, 5461, 5041, 4681, 4369,
4096, 3855, 3640, 3449, 3276, 3120, 2978, 2849,
2730, 2621, 2520, 2427, 2340, 2259, 2184, 2114,
2048, 1985, 1927, 1872, 1820, 1771, 1724, 1680,
1638, 1598, 1560, 1524, 1489, 1456, 1424, 1394,
1365, 1337, 1310, 1285, 1260, 1236, 1213, 1191,
1170, 1149, 1129, 1110, 1092, 1074, 1057, 1040,
1024, 1008, 992, 978, 963, 949, 936, 923,
910, 897, 885, 873, 862, 851, 840, 829,
819, 809, 799, 789, 780, 771, 762, 753,
744, 736, 728, 720, 712, 704, 697, 689,
682, 675, 668, 661, 655, 648, 642, 636,
630, 624, 618, 612, 606, 601, 595, 590,
585, 579, 574, 569, 564, 560, 555, 550,
546, 541, 537, 532, 528, 524, 520, 516,
512, 508, 504, 500, 496, 492, 489, 485,
481, 478, 474, 471, 468, 464, 461, 458,
455, 451, 448, 445, 442, 439, 436, 434,
431, 428, 425, 422, 420, 417, 414, 412,
409, 407, 404, 402, 399, 397, 394, 392,
390, 387, 385, 383, 381, 378, 376, 374,
372, 370, 368, 366, 364, 362, 360, 358,
356, 354, 352, 350, 348, 346, 344, 343,
341, 339, 337, 336, 334, 332, 330, 329,
327, 326, 324, 322, 321, 319, 318, 316,
315, 313, 312, 310, 309, 307, 306, 304,
303, 302, 300, 299, 297, 296, 295, 293,
292, 291, 289, 288, 287, 286, 284, 283,
282, 281, 280, 278, 277, 276, 275, 274,
273, 271, 270, 269, 268, 267, 266, 265,
264, 263, 262, 261, 260, 259, 258, 257
};
#define CodecName "CLRF"
typedef unsigned char uc;
class RangeCoder
{
uint64_t low;
uint range;
uint code;
uint cl;
public:
char *in_buf;
char *out_buf;
void input (char *in) { out_buf = in_buf = in; }
void output(char *out) { in_buf = out_buf = out; }
char *input(void) {return in_buf;}
char *output(void) {return out_buf;}
int size_out(void) {return out_buf - in_buf;}
int size_in(void) {return in_buf - out_buf;}
void StartEncode( void )
{
low = 0;
range = (uint)-1;
}
void StartDecode( void )
{
uc c;
cl = 0;
range = (uint)-1;
DO(8) c = *in_buf++,
code = (code<<8) | c,
cl = (cl<<1) + (c==0xFF);
}
void FinishEncode( void )
{
DO(8) *out_buf++ = low>>56, low<<=8;
}
void FinishDecode( void ) {}
void Encode (uint cumFreq, uint freq, uint totFreq)
{
range /= totFreq;
low += cumFreq * range;
range*= freq;
if( range<0x01000000 ) {
do {
*out_buf++ = uc(low>>56);
if( range<0x00010000 ) {
*out_buf++ = uc(low>>48);
if( range<0x00000100 ) {
*out_buf++ = uc(low>>40);
range<<=24, low<<=24;
} else range<<=16, low<<=16;
} else range<<=8, low<<=8;
} while( (int(low>>32)==-1) && (range=0xFF,1) );
}
}
void Encode256 (uint cumFreq, uint freq, uint totFreq)
{
range = ((uint64_t)range * div_lookup[totFreq]) >> 16;
low += cumFreq * range;
range*= freq;
if( range<0x01000000 ) {
do {
*out_buf++ = uc(low>>56);
if( range<0x00010000 ) {
*out_buf++ = uc(low>>48);
if( range<0x00000100 ) {
*out_buf++ = uc(low>>40);
range<<=24, low<<=24;
} else range<<=16, low<<=16;
} else range<<=8, low<<=8;
} while( (int(low>>32)==-1) && (range=0xFF,1) );
}
}
uint GetFreq (uint totFreq) {
return code/(range/=totFreq);
}
uint GetFreq256 (uint totFreq) {
range = ((uint64_t)range * div_lookup[totFreq]) >> 16;
return code / range;
}
void Decode (uint cumFreq, uint freq, uint totFreq)
{
code -= cumFreq * range;
range*= freq;
if( range<0x01000000 ) {
uc c;
do {
code=(code<<8)|(c=*in_buf++),cl=(cl<<1)+((c+1U)>>8);
if( range<0x00010000 ) {
code=(code<<8)|(c=*in_buf++),cl=(cl<<1)+((c+1U)>>8);
if( range<0x00000100 ) {
code=(code<<8)|(c=*in_buf++),cl=(cl<<1)+((c+1U)>>8);
range<<=24;
} else range<<=16;
} else range<<=8;
} while( (uc(cl)>=0xF0) && (range=0xFF,1) );
}
}
};