-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrangecode.c
323 lines (262 loc) · 8.77 KB
/
rangecode.c
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
/*
* QTC: rangecode.c (c) 2011, 2012 50m30n3
*
* The range coder in this file is based on the carry-less range coder
* by Dmitry Subbotin and the imlementation found here:
* http://www.sachingarg.com/compression/entropy_coding/64bit/
*
* This file is part of QTC.
*
* QTC 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 3 of the License, or
* (at your option) any later version.
*
* QTC 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 QTC. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include "databuffer.h"
#include "rangecode.h"
unsigned const int maxrange = 0xFFFFFFFF;
unsigned const int top = 0x01<<24;
unsigned const int bottom = 0x01<<16;
/*******************************************************************************
* This function creates a new range coder using a markov chain model *
* *
* order specifies the order of the markov chain model used for prediciton *
* bits specifies the number of bits per symbol *
* *
* Returns a new range coder struct *
*******************************************************************************/
struct rangecoder *rangecoder_create( int order, int bits )
{
struct rangecoder *coder;
int i;
int symbols, fsize, tsize;
if( order < 0 )
{
perror( "rangecoder_create: out of order" );
return NULL;
}
coder = malloc( sizeof( *coder ) );
if( coder == NULL )
{
perror( "rangecoder_create: malloc" );
return NULL;
}
coder->order = order;
coder->bits = bits;
symbols = 1<<bits;
fsize = 1<<(bits*(order+1));
tsize = 1<<(bits*order);
coder->freqs = malloc( sizeof( *coder->freqs ) * fsize );
if( coder->freqs == NULL )
{
perror( "rangecoder_create: malloc" );
return NULL;
}
coder->totals = malloc( sizeof( *coder->freqs ) * tsize );
if( coder->freqs == NULL )
{
perror( "rangecoder_create: malloc" );
return NULL;
}
for( i=0; i<fsize; i++ )
coder->freqs[i] = 1;
for( i=0; i<tsize; i++ )
coder->totals[i] = symbols;
return coder;
}
/*******************************************************************************
* This function resets the model of a range coder *
* *
* coder is the range coder that contains the model to be reset *
* *
* Modifies coder *
*******************************************************************************/
void rangecoder_reset( struct rangecoder *coder )
{
int i;
int symbols, fsize, tsize;
symbols = 1<<coder->bits;
fsize = 1<<(coder->bits*(coder->order+1));
tsize = 1<<(coder->bits*coder->order);
for( i=0; i<fsize; i++ )
coder->freqs[i] = 1;
for( i=0; i<tsize; i++ )
coder->totals[i] = symbols;
}
/*******************************************************************************
* This function frees a range coder and its internal structures *
* *
* coder is the range coder to be freed *
* *
* Modifies coder *
*******************************************************************************/
void rangecoder_free( struct rangecoder *coder )
{
free( coder->freqs );
free( coder->totals );
free( coder );
}
/*******************************************************************************
* This function compresses a databuffer using a range coder *
* *
* coder is the range coder to be used during compression *
* in contains the data to be compressed *
* out is the databuffer that the compressed data will be written to *
* *
* Modifies coder, in and out *
*******************************************************************************/
int rangecode_compress( struct rangecoder *coder, struct databuffer *in, struct databuffer *out )
{
int *freqs, *totals;
unsigned int count;
int symbol;
int i;
int bits, symbols, idx, mask;
int start, size, total;
unsigned int low = 0x00;
unsigned int range = maxrange;
freqs = coder->freqs;
totals = coder->totals;
bits = coder->bits;
symbols = 1<<bits;
mask = ~((~0x00)<<(bits*(coder->order+1)));
idx = 0x00;
for( count=0; count<in->size*8; count+=bits )
{
if( bits == 8 )
symbol = databuffer_get_byte( in );
else
symbol = databuffer_get_bits( in, bits );
start = 0;
for( i=0; i<symbol; i++ )
start += freqs[idx+i];
size = freqs[idx+symbol];
total = totals[idx>>bits];
range /= total;
low += start * range;
range *= size;
while( ( (low^(low+range)) < top ) || ( range < bottom ) )
{
if( ( range < bottom ) && ( ( (low^(low+range)) >= top ) ) )
range = (-low)&(bottom-1);
databuffer_add_byte( ( low >> 24 ) & 0xFF, out );
low <<= 8;
range <<= 8;
}
freqs[idx+symbol] += 32;
totals[idx>>bits] += 32;
if( totals[idx>>bits] >= 0xFFFF )
{
totals[idx>>bits] = 0;
for( i=0; i<symbols; i++ )
{
freqs[idx+i] /= 2;
if( freqs[idx+i] == 0 )
freqs[idx+i] = 1;
totals[idx>>bits] += freqs[idx+i];
}
}
idx = ((idx+symbol)<<bits)&mask;
}
for( i=0; i<4; i++ )
{
databuffer_add_byte( ( low >> 24 ) & 0xFF, out );
low <<= 8;
}
return 1;
}
/*******************************************************************************
* This function decompresses a databuffer using a range coder *
* *
* coder is the range coder to be used during compression *
* in contains the data to be decompressed *
* out is the databuffer that the decompressed data will be written to *
* length is the uncompressed data length *
* *
* Modifies coder, in and out *
*******************************************************************************/
int rangecode_decompress( struct rangecoder *coder, struct databuffer *in, struct databuffer *out, unsigned int length )
{
int *freqs, *totals;
unsigned int count;
int symbol;
int i;
int start, size, total, value;
int bits, symbols, idx, mask;
unsigned int low = 0x00;
unsigned int range = maxrange;
unsigned int code = 0x00;
freqs = coder->freqs;
totals = coder->totals;
bits = coder->bits;
symbols = 1<<bits;
for( i=0; i<4; i++ )
{
code <<= 8;
code |= databuffer_get_byte( in ) & 0xFF;
}
mask = ~((~0x00)<<(bits*(coder->order+1)));
idx = 0x00;
for( count=0; count<length*8; count+=bits )
{
total = totals[idx>>bits];
range /= total;
value = ( code - low ) / range;
i = 0;
while( ( value >= 0 ) && ( i < symbols ) )
{
value -= freqs[idx+i];
i++;
}
if( value >= 0 )
{
fputs( "rangecode_decompress: decompression error\n", stderr );
return 0;
}
symbol = i-1;
if( bits == 8 )
databuffer_add_byte( symbol, out );
else
databuffer_add_bits( symbol, out, bits );
start = 0;
for( i=0; i<symbol; i++ )
start += freqs[idx+i];
size = freqs[idx+symbol];
low += start * range;
range *= size;
while( ( (low^(low+range)) < top ) || ( range < bottom ) )
{
if( ( range < bottom ) && ( ( (low^(low+range)) >= top ) ) )
range = (-low)&(bottom-1);
code <<= 8;
code |= databuffer_get_byte( in ) & 0xFF;
low <<= 8;
range <<= 8;
}
freqs[idx+symbol] += 32;
totals[idx>>bits] += 32;
if( totals[idx>>bits] >= 0xFFFF )
{
totals[idx>>bits] = 0;
for( i=0; i<symbols; i++ )
{
freqs[idx+i] /= 2;
if( freqs[idx+i] == 0 )
freqs[idx+i] = 1;
totals[idx>>bits] += freqs[idx+i];
}
}
idx = ((idx+symbol)<<bits)&mask;
}
return 1;
}