-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patharithmetic_codec.h
191 lines (146 loc) · 7.83 KB
/
arithmetic_codec.h
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
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// -
// **************************** -
// ARITHMETIC CODING EXAMPLES -
// **************************** -
// -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// -
// Fast arithmetic coding implementation -
// -> 32-bit variables, 32-bit product, periodic updates, table decoding -
// -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// -
// Version 1.00 - April 25, 2004 -
// -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// -
// WARNING -
// ========= -
// -
// The only purpose of this program is to demonstrate the basic principles -
// of arithmetic coding. It is provided as is, without any express or -
// implied warranty, without even the warranty of fitness for any particular -
// purpose, or that the implementations are correct. -
// -
// Permission to copy and redistribute this code is hereby granted, provided -
// that this warning and copyright notices are not removed or altered. -
// -
// Copyright (c) 2004 by Amir Said ([email protected]) & -
// William A. Pearlman ([email protected]) -
// -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// -
// A description of the arithmetic coding method used here is available in -
// -
// Lossless Compression Handbook, ed. K. Sayood -
// Chapter 5: Arithmetic Coding (A. Said), pp. 101-152, Academic Press, 2003 -
// -
// A. Said, Introduction to Arithetic Coding Theory and Practice -
// HP Labs report HPL-2004-76 - http://www.hpl.hp.com/techreports/ -
// -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - Definitions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#ifndef ARITHMETIC_CODEC
#define ARITHMETIC_CODEC
#include <stdio.h>
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - Class definitions - - - - - - - - - - - - - - - - - - - - - - - - - - -
class Static_Bit_Model // static model for binary data
{
public:
Static_Bit_Model(void);
void set_probability_0(double); // set probability of symbol '0'
private: // . . . . . . . . . . . . . . . . . . . . . .
unsigned bit_0_prob;
friend class Arithmetic_Codec;
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class Static_Data_Model // static model for general data
{
public:
Static_Data_Model(void);
~Static_Data_Model(void);
unsigned model_symbols(void) { return data_symbols; }
void set_distribution(unsigned number_of_symbols,
const double probability[] = 0); // 0 means uniform
private: // . . . . . . . . . . . . . . . . . . . . . .
unsigned * distribution, * decoder_table;
unsigned data_symbols, last_symbol, table_size, table_shift;
friend class Arithmetic_Codec;
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class Adaptive_Bit_Model // adaptive model for binary data
{
public:
Adaptive_Bit_Model(void);
void reset(void); // reset to equiprobable model
private: // . . . . . . . . . . . . . . . . . . . . . .
void update(void);
unsigned update_cycle, bits_until_update;
unsigned bit_0_prob, bit_0_count, bit_count;
friend class Arithmetic_Codec;
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class Adaptive_Data_Model // adaptive model for binary data
{
public:
Adaptive_Data_Model(void);
Adaptive_Data_Model(unsigned number_of_symbols);
~Adaptive_Data_Model(void);
unsigned model_symbols(void) { return data_symbols; }
void reset(void); // reset to equiprobable model
void set_alphabet(unsigned number_of_symbols);
private: // . . . . . . . . . . . . . . . . . . . . . .
void update(bool);
unsigned * distribution, * symbol_count, * decoder_table;
unsigned total_count, update_cycle, symbols_until_update;
unsigned data_symbols, last_symbol, table_size, table_shift;
friend class Arithmetic_Codec;
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - Encoder and decoder class - - - - - - - - - - - - - - - - - - - - - - -
// Class with both the arithmetic encoder and decoder. All compressed data is
// saved to a memory buffer
class Arithmetic_Codec
{
public:
Arithmetic_Codec(void);
~Arithmetic_Codec(void);
Arithmetic_Codec(unsigned max_code_bytes,
unsigned char * user_buffer = 0); // 0 = assign new
unsigned char * buffer(void) { return code_buffer; }
void set_buffer(unsigned max_code_bytes,
unsigned char * user_buffer = 0); // 0 = assign new
void start_encoder(void);
void start_decoder(void);
void read_from_file(FILE * code_file); // read code data, start decoder
unsigned stop_encoder(void); // returns number of bytes used
unsigned write_to_file(FILE * code_file); // stop encoder, write code data
void stop_decoder(void);
void put_bit(unsigned bit);
unsigned get_bit(void);
void put_bits(unsigned data, unsigned number_of_bits);
unsigned get_bits(unsigned number_of_bits);
void encode(unsigned bit,
Static_Bit_Model &);
unsigned decode(Static_Bit_Model &);
void encode(unsigned data,
Static_Data_Model &);
unsigned decode(Static_Data_Model &);
void encode(unsigned bit,
Adaptive_Bit_Model &);
unsigned decode(Adaptive_Bit_Model &);
void encode(unsigned data,
Adaptive_Data_Model &);
unsigned decode(Adaptive_Data_Model &);
private: // . . . . . . . . . . . . . . . . . . . . . .
void propagate_carry(void);
void renorm_enc_interval(void);
void renorm_dec_interval(void);
unsigned char * code_buffer, * new_buffer, * ac_pointer;
unsigned base, value, length; // arithmetic coding state
unsigned buffer_size, mode; // mode: 0 = undef, 1 = encoder, 2 = decoder
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#endif