MEPP2 Project
arithmetic_codec.hpp
Go to the documentation of this file.
1 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2 // -
3 // **************************** -
4 // ARITHMETIC CODING EXAMPLES -
5 // **************************** -
6 // -
7 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8 // -
9 // Fast arithmetic coding implementation -
10 // -> 32-bit variables, 32-bit product, periodic updates, table decoding -
11 // -
12 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
13 // -
14 // Version 1.01 - November 28, 2005 -
15 // -
16 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17 // -
18 // WARNING -
19 // ========= -
20 // -
21 // The only purpose of this program is to demonstrate the basic principles -
22 // of arithmetic coding. It is provided as is, without any express or -
23 // implied warranty, without even the warranty of fitness for any particular -
24 // purpose, or that the implementations are correct. -
25 // -
26 // Permission to copy and redistribute this code is hereby granted, provided -
27 // that this warning and copyright notices are not removed or altered. -
28 // -
29 // Copyright (c) 2004 by Amir Said (said@ieee.org) & -
30 // William A. Pearlman (pearlw@ecse.rpi.edu) -
31 // -
32 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
33 // -
34 // A description of the arithmetic coding method used here is available in -
35 // -
36 // Lossless Compression Handbook, ed. K. Sayood -
37 // Chapter 5: Arithmetic Coding (A. Said), pp. 101-152, Academic Press, 2003 -
38 // -
39 // A. Said, Introduction to Arithetic Coding Theory and Practice -
40 // HP Labs report HPL-2004-76 - http://www.hpl.hp.com/techreports/ -
41 // -
42 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43 
44 
45 // - - Definitions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46 
47 #pragma once
48 
49 
50 #include <cstdio>
51 
52 
53 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
54 // - - Class definitions - - - - - - - - - - - - - - - - - - - - - - - - - - -
55 
56 class Static_Bit_Model // static model for binary data
57 {
58 public:
59  Static_Bit_Model(void);
60 
61  void set_probability_0(double); // set probability of symbol '0'
62 
63 private: // . . . . . . . . . . . . . . . . . . . . . .
64  unsigned bit_0_prob;
65  friend class Arithmetic_Codec;
66 };
67 
68 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
69 
70 class Static_Data_Model // static model for general data
71 {
72 public:
73  Static_Data_Model(void);
74  ~Static_Data_Model(void);
75 
76  unsigned model_symbols(void) { return data_symbols; }
77 
78  void set_distribution(unsigned number_of_symbols,
79  const double probability[] = 0); // 0 means uniform
80 
81 private: // . . . . . . . . . . . . . . . . . . . . . .
84  friend class Arithmetic_Codec;
85 };
86 
87 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88 
96 class Adaptive_Bit_Model // adaptive model for binary data
97 {
98 public:
99  Adaptive_Bit_Model(void);
100 
101  void reset(void); // reset to equiprobable model
102 
103 private: // . . . . . . . . . . . . . . . . . . . . . .
104  void update(void);
107  friend class Arithmetic_Codec;
108 };
109 
110 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111 
119 class Adaptive_Data_Model // adaptive model for binary data
120 {
121 public:
122  Adaptive_Data_Model(void);
123  Adaptive_Data_Model(unsigned number_of_symbols);
124  ~Adaptive_Data_Model(void);
125 
126  unsigned model_symbols(void) { return data_symbols; }
127 
128  void reset(void); // reset to equiprobable model
129  void set_alphabet(unsigned number_of_symbols);
130 
131 private: // . . . . . . . . . . . . . . . . . . . . . .
132  void update(bool);
136  friend class Arithmetic_Codec;
137 };
138 
139 
140 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141 // - - Encoder and decoder class - - - - - - - - - - - - - - - - - - - - - - -
142 
143 // Class with both the arithmetic encoder and decoder. All compressed data is
144 // saved to a memory buffer
145 
154 {
155 public:
156  Arithmetic_Codec(void);
157  ~Arithmetic_Codec(void);
158  Arithmetic_Codec(unsigned max_code_bytes,
159  unsigned char *user_buffer = 0); // 0 = assign new
160 
161  unsigned char *buffer(void) { return code_buffer; }
162 
163  void set_buffer(unsigned max_code_bytes,
164  unsigned char *user_buffer = 0); // 0 = assign new
165 
166  void start_encoder(void);
167  void start_decoder(void);
168  void read_from_file(FILE *code_file); // read code data, start decoder
169 
170  unsigned stop_encoder(void); // returns number of bytes used
171  unsigned write_to_file(FILE *code_file); // stop encoder, write code data
172  void stop_decoder(void);
173 
174  void put_bit(unsigned bit);
175  unsigned get_bit(void);
176 
177  void put_bits(unsigned data, unsigned number_of_bits);
178  unsigned get_bits(unsigned number_of_bits);
179 
180  void encode(unsigned bit, Static_Bit_Model &);
181  unsigned decode(Static_Bit_Model &);
182 
183  void encode(unsigned data, Static_Data_Model &);
184  unsigned decode(Static_Data_Model &);
185 
186  void encode(unsigned bit, Adaptive_Bit_Model &);
187  unsigned decode(Adaptive_Bit_Model &);
188 
189  void encode(unsigned data, Adaptive_Data_Model &);
190  unsigned decode(Adaptive_Data_Model &);
191 
192  unsigned calculate_current_decoded_size(void) // Ho
193  {
194  if(mode == 2)
195  {
196  return (unsigned)(ac_pointer - code_buffer); // MT
197  }
198  else
199  return 0;
200  }
201 
202 private: // . . . . . . . . . . . . . . . . . . . . . .
203  void propagate_carry(void);
204  void renorm_enc_interval(void);
205  void renorm_dec_interval(void);
206  unsigned char *code_buffer, *new_buffer, *ac_pointer;
207  unsigned base, value, length; // arithmetic coding state
208  unsigned buffer_size, mode; // mode: 0 = undef, 1 = encoder, 2 = decoder
209 };
210 
211 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
212 
213 #include "arithmetic_codec.inl"
Arithmetic_Codec::put_bit
void put_bit(unsigned bit)
Definition: arithmetic_codec.inl:151
Static_Data_Model::table_size
unsigned table_size
Definition: arithmetic_codec.hpp:83
Adaptive_Bit_Model::update_cycle
unsigned update_cycle
Definition: arithmetic_codec.hpp:105
Arithmetic_Codec::encode
void encode(unsigned bit, Static_Bit_Model &)
Definition: arithmetic_codec.inl:243
Static_Bit_Model::bit_0_prob
unsigned bit_0_prob
Definition: arithmetic_codec.hpp:64
Adaptive_Data_Model::table_shift
unsigned table_shift
Definition: arithmetic_codec.hpp:135
Arithmetic_Codec::mode
unsigned mode
Definition: arithmetic_codec.hpp:208
Adaptive_Bit_Model::Adaptive_Bit_Model
Adaptive_Bit_Model(void)
Definition: arithmetic_codec.inl:790
Arithmetic_Codec::base
unsigned base
Definition: arithmetic_codec.hpp:207
Arithmetic_Codec::length
unsigned length
Definition: arithmetic_codec.hpp:207
Static_Bit_Model
Definition: arithmetic_codec.hpp:57
Adaptive_Bit_Model
Adaptive bit model.
Definition: arithmetic_codec.hpp:97
Static_Data_Model::table_shift
unsigned table_shift
Definition: arithmetic_codec.hpp:83
Static_Data_Model::set_distribution
void set_distribution(unsigned number_of_symbols, const double probability[]=0)
Definition: arithmetic_codec.inl:849
Arithmetic_Codec::~Arithmetic_Codec
~Arithmetic_Codec(void)
Definition: arithmetic_codec.inl:598
Adaptive_Data_Model::~Adaptive_Data_Model
~Adaptive_Data_Model(void)
Definition: arithmetic_codec.inl:929
arithmetic_codec.inl
Adaptive_Data_Model::distribution
unsigned * distribution
Definition: arithmetic_codec.hpp:133
Static_Data_Model::model_symbols
unsigned model_symbols(void)
Definition: arithmetic_codec.hpp:76
Static_Data_Model::last_symbol
unsigned last_symbol
Definition: arithmetic_codec.hpp:83
Static_Data_Model::Static_Data_Model
Static_Data_Model(void)
Definition: arithmetic_codec.inl:836
Adaptive_Data_Model::table_size
unsigned table_size
Definition: arithmetic_codec.hpp:135
Arithmetic_Codec::value
unsigned value
Definition: arithmetic_codec.hpp:207
Arithmetic_Codec::stop_decoder
void stop_decoder(void)
Definition: arithmetic_codec.inl:757
Adaptive_Bit_Model::update
void update(void)
Definition: arithmetic_codec.inl:809
Adaptive_Bit_Model::bits_until_update
unsigned bits_until_update
Definition: arithmetic_codec.hpp:105
Arithmetic_Codec::start_decoder
void start_decoder(void)
Definition: arithmetic_codec.inl:653
Adaptive_Data_Model::symbols_until_update
unsigned symbols_until_update
Definition: arithmetic_codec.hpp:134
Adaptive_Data_Model::set_alphabet
void set_alphabet(unsigned number_of_symbols)
Definition: arithmetic_codec.inl:935
Adaptive_Data_Model::data_symbols
unsigned data_symbols
Definition: arithmetic_codec.hpp:135
Arithmetic_Codec::buffer_size
unsigned buffer_size
Definition: arithmetic_codec.hpp:208
Adaptive_Data_Model
Adaptive data model.
Definition: arithmetic_codec.hpp:120
Adaptive_Data_Model::Adaptive_Data_Model
Adaptive_Data_Model(void)
Definition: arithmetic_codec.inl:914
Arithmetic_Codec::read_from_file
void read_from_file(FILE *code_file)
Definition: arithmetic_codec.inl:672
Arithmetic_Codec::decode
unsigned decode(Static_Bit_Model &)
Definition: arithmetic_codec.inl:271
Adaptive_Bit_Model::bit_count
unsigned bit_count
Definition: arithmetic_codec.hpp:106
Static_Data_Model::data_symbols
unsigned data_symbols
Definition: arithmetic_codec.hpp:83
Static_Bit_Model::set_probability_0
void set_probability_0(double)
Definition: arithmetic_codec.inl:778
Arithmetic_Codec::start_encoder
void start_encoder(void)
Definition: arithmetic_codec.inl:636
Arithmetic_Codec::ac_pointer
unsigned char * ac_pointer
Definition: arithmetic_codec.hpp:206
Arithmetic_Codec::get_bits
unsigned get_bits(unsigned number_of_bits)
Definition: arithmetic_codec.inl:221
Arithmetic_Codec::new_buffer
unsigned char * new_buffer
Definition: arithmetic_codec.hpp:206
Adaptive_Data_Model::update
void update(bool)
Definition: arithmetic_codec.inl:974
Arithmetic_Codec::put_bits
void put_bits(unsigned data, unsigned number_of_bits)
Definition: arithmetic_codec.inl:197
Adaptive_Data_Model::last_symbol
unsigned last_symbol
Definition: arithmetic_codec.hpp:135
Arithmetic_Codec::set_buffer
void set_buffer(unsigned max_code_bytes, unsigned char *user_buffer=0)
Definition: arithmetic_codec.inl:604
Arithmetic_Codec::calculate_current_decoded_size
unsigned calculate_current_decoded_size(void)
Definition: arithmetic_codec.hpp:192
Arithmetic_Codec
Arithmetic codec.
Definition: arithmetic_codec.hpp:154
Static_Bit_Model::Static_Bit_Model
Static_Bit_Model(void)
Definition: arithmetic_codec.inl:769
Arithmetic_Codec::renorm_dec_interval
void renorm_dec_interval(void)
Definition: arithmetic_codec.inl:139
Adaptive_Data_Model::symbol_count
unsigned * symbol_count
Definition: arithmetic_codec.hpp:133
Adaptive_Data_Model::reset
void reset(void)
Definition: arithmetic_codec.inl:1020
Static_Data_Model::~Static_Data_Model
~Static_Data_Model(void)
Definition: arithmetic_codec.inl:843
Static_Data_Model::distribution
unsigned * distribution
Definition: arithmetic_codec.hpp:82
Arithmetic_Codec::renorm_enc_interval
void renorm_enc_interval(void)
Definition: arithmetic_codec.inl:127
Arithmetic_Codec::buffer
unsigned char * buffer(void)
Definition: arithmetic_codec.hpp:161
Arithmetic_Codec::propagate_carry
void propagate_carry(void)
Definition: arithmetic_codec.inl:116
Adaptive_Bit_Model::bit_0_prob
unsigned bit_0_prob
Definition: arithmetic_codec.hpp:106
Arithmetic_Codec::code_buffer
unsigned char * code_buffer
Definition: arithmetic_codec.hpp:206
Adaptive_Data_Model::update_cycle
unsigned update_cycle
Definition: arithmetic_codec.hpp:134
Static_Data_Model
Definition: arithmetic_codec.hpp:71
Arithmetic_Codec::get_bit
unsigned get_bit(void)
Definition: arithmetic_codec.inl:175
Adaptive_Data_Model::decoder_table
unsigned * decoder_table
Definition: arithmetic_codec.hpp:133
Static_Data_Model::decoder_table
unsigned * decoder_table
Definition: arithmetic_codec.hpp:82
Adaptive_Bit_Model::bit_0_count
unsigned bit_0_count
Definition: arithmetic_codec.hpp:106
Arithmetic_Codec::write_to_file
unsigned write_to_file(FILE *code_file)
Definition: arithmetic_codec.inl:732
Arithmetic_Codec::Arithmetic_Codec
Arithmetic_Codec(void)
Definition: arithmetic_codec.inl:582
Arithmetic_Codec::stop_encoder
unsigned stop_encoder(void)
Definition: arithmetic_codec.inl:697
Adaptive_Data_Model::model_symbols
unsigned model_symbols(void)
Definition: arithmetic_codec.hpp:126
Adaptive_Bit_Model::reset
void reset(void)
Definition: arithmetic_codec.inl:796
Adaptive_Data_Model::total_count
unsigned total_count
Definition: arithmetic_codec.hpp:134