Arithmetic coding tutorial code  1
AC
arith_code.hh
Go to the documentation of this file.
1 /*
2  * @author Andrea C. G. Mennucci
3  *
4  * @file arith_code.hh
5  *
6  * @copyright (C) 2010-2019 Andrea C. G. Mennucci
7  *
8  * The new BSD License is applied to this software, see LICENSE.txt
9  *
10  */
11 
12 
13 
14 namespace AC {
15 
16 #include "arith_code_config.hh"
17 
18 
19 /*********** general constants ****/
20 
22 #if (AC_representation_bitsize <= 15)
23 typedef uint16_t I_t;
24 typedef uint32_t long_I_t;
25 #define AC_PRI_I_t PRId16
26 #define AC_PRI_long_I_t PRId32
27 #define AC_SIZE 16
28 #elif (AC_representation_bitsize <= 31)
29 typedef uint32_t I_t;
30 typedef uint64_t long_I_t;
32 #define AC_PRI_I_t PRId32
33 #define AC_PRI_long_I_t PRId64
35 #define AC_SIZE 32
36 #elif (AC_representation_bitsize <= 63)
37 typedef uint64_t I_t;
38 typedef __int128 long_I_t;
39 #define AC_PRI_I_t PRId64
40 #define AC_PRI_long_I_t PRId128 //this does not exist...
41 #define AC_SIZE 64
42 #else
43 #error "ARITHMETIC CODEC says: too many bits"
44 #endif
45 
47 typedef I_t F_t;
49 #define AC_PRI_F_t AC_PRI_I_t
50 
51 const I_t MAX_FREQ = ((I_t)1) << (AC_representation_bitsize-2) ;
52 
54 typedef std::pair<I_t, I_t> interval_t;
55 
57 typedef std::function<void(int,void *)> output_callback_t;
58 
60 typedef std::function<int(void *)> input_callback_t;
61 
62 
66 class Base{
67 
68 public:
70  const char *prefix;
71 
74  FILE * verbose_stream = stdout;
75 
77  unsigned int number_input_bits();
79  unsigned int number_input_symbols();
81  unsigned int number_output_bits();
83  unsigned int number_output_symbols();
84 
86  void print_state();
87 
89  interval_t S_interval();
90 
92  interval_t B_interval();
93 
95  int output_bit();
96 
98  void output_bits(output_callback_t out);
99 
101 
110  I_t max_symbol = -1;
111 
116  F_t * frequencies = NULL;
117 
123 
124 
127  void *callback_data = NULL;
128 
129 protected:
130  //typedef std::function<void(int,Base *)> callback_B_t;
131 
132 
133 #ifdef AC_QUARTER_ZOOM
134  /* counts virtual bits, in case of centered zooms */
135  unsigned int bitsToFollow;
136  /* stores the value of the virtual bit , or -1 if there is no virtual bit */
138 #endif
139 
140  const I_t One = 1;
142  const I_t Top = (One << AC_representation_bitsize);
144  const I_t Qtr = (One << (AC_representation_bitsize-2));
146  const I_t QtrMinus = (One << (AC_representation_bitsize-2)) - One;
148  const I_t Half = (Qtr*2);
150  const I_t ThreeQtr = (Qtr*3);
151 
153  F_t const cum_freq_uniform8[9] = { 8 , 7 , 6 , 5, 4 , 3 , 2 , 1 , 0 };
154 
156  static const int n_symbols_flush = 8;
157 
160 
161  // the old table was
162  //F_t const cum_freq_flush[n_symbols_flush+1] = { Qtr , QtrMinus , 1 , 0 };
163 
165  I_t Slow;
167  I_t Shigh;
169  long_I_t Srange;
170 
172  I_t Blow;
174  I_t Bhigh;
175 
176  // operations on intervals
177  void doubleit();
178  void doublehi();
179  void doublelow();
180  void doublecen();
181 
186  I_t separ_low_high(int symb, const F_t * cum_freq);
187 
189  int resize_pull_one_bit();
190 
192  I_t interval_right(int symb, const F_t * cum_freq);
193 
195  I_t interval_left(int symb, const F_t * cum_freq);
196 
198  void push_symbol(int symb, const F_t * cum_freq);
199 
201  void push_bit(int bit);
202 
204  unsigned int significant_bits;
205 
207  unsigned int n_in_bits,
209  n_in_symbs,
211  n_zooms,
213  n_out_bits,
215  n_out_symbs;
216 
217  Base();
218 
219 };
220 
221 
222 
223 /**************** ENCODER **********/
224 class Encoder : public Base {
225 
226 public:
228  output_callback_t output_callback;
229 
232  Encoder(
233  output_callback_t output_callback_ = NULL);
234 
236  void input_symbol(
237  int symb,
239  const F_t * cum_freq = NULL);
240 
245  void flush();
246 };
247 
248 
249 /********************* DECODER **********/
250 class Decoder : public Base {
251 
252 private:
253  //
254  int input_eof = 0;
255 
256  /* callback when the decoder decodes a symbol */
257  output_callback_t output_callback;
258 
259 
260  /* when this callback is called, the S-interval in the decoder is the same as the
261  * S-interval in the encoder (at the same bitcount)
262  * This is used only for
263  */
264  output_callback_t bit_callback;
265 
267  input_callback_t read_bit_call;
268 
269  /* signal that the next symbol will be deflushed */
270  int flag_flush=0;
271 
275  int search(
276  const F_t * cum_freq,
278  int max_symb );
279 
283  int search_fast(
284  const F_t * cum_freq,
286  int max_symb );
290  int output_symbol_standard(
291  const F_t * cp,
293  I_t ms
294  );
295 
296 public:
297 
298  /* inizializza */
299  Decoder(
300  output_callback_t output_callback_ = NULL ,
304  input_callback_t read_bit_call_ = NULL ,
306  output_callback_t bit_callback_ = NULL
307  );
308 
315  int output_symbol(
316  F_t const * cum_freq = NULL,
319  I_t max_symb = -1
320  );
321 
322 #if 0
323  void push_bit(int bit);
325 #endif
326 
328 
331  void input_bit(int bit);
332 
338  void run();
339 
348  void prepare_for_deflush();
349 
351  int is_deflushing();
352 };
353 
354 
355 
356 /***************** utilities ***********/
357 
359 void freq2cum_freq(F_t cum_freq[], F_t freq[], int max_symb, int assert_non_zero);
360 
361 
362 }
I_t Blow
B-interval left extreme.
Definition: arith_code.hh:172
std::function< void(int, void *)> output_callback_t
type for callbacks
Definition: arith_code.hh:57
unsigned int number_input_symbols()
number of symbols inserted in the state
Definition: arith_code.cc:160
int virtual_bit
Definition: arith_code.hh:137
I_t max_symbol
if the output callback is used in decoding, then the "cumulative_frequencies" and "max_symbol" must b...
Definition: arith_code.hh:110
void output_bits(output_callback_t out)
outputs multiple bits, returns them using a callback (if not null; else they are lost) ...
Definition: arith_code.cc:251
void push_bit(int bit)
put bit in B-interval
Definition: arith_code.cc:317
void doubleit()
Definition: arith_code.cc:139
I_t interval_left(int symb, const F_t *cum_freq)
left extreme of a S-sub-interval
Definition: arith_code.cc:290
interval_t S_interval()
return the S_interval
Definition: arith_code.cc:182
Definition: arith_code.hh:66
const I_t Top
representation of 1
Definition: arith_code.hh:142
unsigned int n_out_bits
number of bits extracted from the state
Definition: arith_code.hh:207
long_I_t Srange
S-interval width.
Definition: arith_code.hh:169
#define AC_representation_bitsize
the number of bits to represent the intervals
Definition: arith_code_config.hh:21
F_t const * cum_freq_flush
special cumulative table used for flushing
Definition: arith_code.hh:159
unsigned int n_in_symbs
number of symbols inserted in the state
Definition: arith_code.hh:207
void doublehi()
Definition: arith_code.cc:140
const I_t ThreeQtr
representation of 3/4
Definition: arith_code.hh:150
F_t * cumulative_frequencies
if the output callback is used in decoding, then the "cumulative_frequencies" and "max_symbol" must b...
Definition: arith_code.hh:105
unsigned int n_zooms
number of zooms
Definition: arith_code.hh:207
unsigned int number_input_bits()
number of bits inserted in the state
Definition: arith_code.cc:157
output_callback_t output_callback
callback when the encoder encodes a symbo
Definition: arith_code.hh:228
FILE * verbose_stream
Definition: arith_code.hh:74
unsigned int significant_bits
significant bits (used in the decoder)
Definition: arith_code.hh:204
F_t const cum_freq_uniform8[9]
cumulative tables of 8 equidistributed symbols
Definition: arith_code.hh:153
int resize_pull_one_bit()
returns 0 , 1 or -1 if no bit can be pulled at this moment ; resize S-interval and B-interval accordi...
Definition: arith_code.cc:190
const I_t One
Definition: arith_code.hh:140
F_t * frequencies
if the output callback is used in decoding, then the "cumulative_frequencies" and "max_symbol" must b...
Definition: arith_code.hh:116
void * callback_data
Definition: arith_code.hh:127
const I_t Qtr
representation of 1/4
Definition: arith_code.hh:144
I_t separ_low_high(int symb, const F_t *cum_freq)
divides Slow - Shigh in subintervals : returns the beginning of each interval; note that intervals ar...
Definition: arith_code.cc:277
I_t Bhigh
B-interval right extreme.
Definition: arith_code.hh:174
void frequencies2cumulative_frequencies()
if the output callback is used in decoding, then the "cumulative_frequencies" and "max_symbol" must b...
Definition: arith_code.cc:332
void freq2cum_freq(F_t cum_freq[], F_t freq[], int max_symb, int assert_non_zero=1)
computes the cumulative cum_freq given the frequencies of symbols
Definition: arith_code.cc:644
uint64_t long_I_t
Definition: arith_code.hh:30
const I_t MAX_FREQ
the sum of all frequencies of symbols cannot exceed this value
Definition: arith_code.hh:51
I_t Slow
S-interval left extreme.
Definition: arith_code.hh:165
uint32_t I_t
types for variables defining intervals
Definition: arith_code.hh:29
I_t Shigh
S-interval right extreme (included in the interval)
Definition: arith_code.hh:167
Definition: arith_code.hh:224
const I_t Half
Representation of 3/4.
Definition: arith_code.hh:148
Definition: arith_code.hh:250
std::pair< I_t, I_t > interval_t
struct to represent an interval
Definition: arith_code.hh:54
interval_t B_interval()
return the B_interval
Definition: arith_code.cc:186
void push_symbol(int symb, const F_t *cum_freq)
put symbol in S-interval by splitting it and choosing a subinterval, proportional to the frequencies ...
Definition: arith_code.cc:295
Base()
Definition: arith_code.cc:145
Definition: arith_code.cc:25
const I_t QtrMinus
representation of point preceding 1/4
Definition: arith_code.hh:146
void doublelow()
Definition: arith_code.cc:141
I_t F_t
types for variables defining frequencies
Definition: arith_code.hh:47
unsigned int n_out_symbs
number of symbols extracted from the state
Definition: arith_code.hh:207
unsigned int bitsToFollow
Definition: arith_code.hh:135
static const int n_symbols_flush
how many symbols in the special cumulative table used for flushing
Definition: arith_code.hh:156
int output_bit()
outputs one bit from the state, if available, else -1
Definition: arith_code.cc:222
void doublecen()
Definition: arith_code.cc:142
unsigned int n_in_bits
number of bits inserted in the state
Definition: arith_code.hh:207
unsigned int number_output_symbols()
number of symbols extracted from the state
Definition: arith_code.cc:166
unsigned int number_output_bits()
number of bits extracted from the state
Definition: arith_code.cc:163
void print_state()
print the internal state
Definition: arith_code.cc:169
std::function< int(void *)> input_callback_t
type for bit reader call
Definition: arith_code.hh:60
I_t interval_right(int symb, const F_t *cum_freq)
right extreme of a S-sub-interval ; note that symbols start from 0 here
Definition: arith_code.cc:286
const char * prefix
name of the class, for printing
Definition: arith_code.hh:70