MEPP2 Project
arithmetic_codec.inl
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 // - - Inclusion - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46 
47 #include <stdlib.h>
48 
49 
50 // - - Constants - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
51 //
52 // ELO-note: constants transformed into functions to make this file header-only
53 //
54 
55 inline
56 unsigned AC__MinLength(void)
57 {
58  return 0x01000000U; // threshold for renormalization
59 }
60 
61 inline
62 unsigned AC__MaxLength(void)
63 {
64  return 0xFFFFFFFFU; // maximum AC interval length
65 }
66 
67 // Maximum values for binary models
68 
69 inline
70 unsigned BM__LengthShift(void)
71 {
72  return 13; // length bits discarded before mult.
73 }
74 
75 inline
76 unsigned BM__MaxCount(void)
77 {
78  return 1 << BM__LengthShift(); // for adaptive models
79 }
80 
81 // Maximum values for general models
82 
83 inline
84 unsigned DM__LengthShift(void)
85 {
86  return 15; // length bits discarded before mult.
87 }
88 
89 inline
90 unsigned DM__MaxCount(void)
91 {
92  return 1 << DM__LengthShift(); // for adaptive models
93 }
94 
95 
96 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97 // - - Static functions - - - - - - - - - - - - - - - - - - - - - - - - - - -
98 //
99 // ELO-note: made inline instead of static to make this file header-only
100 //
101 
102 inline void
103 AC_Error(const char *msg)
104 {
105  fprintf(stderr, "\n\n -> Arithmetic coding error: ");
106  fputs(msg, stderr);
107  fputs("\n Execution terminated!\n", stderr);
108  exit(1);
109 }
110 
111 
112 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
113 // - - Coding implementations - - - - - - - - - - - - - - - - - - - - - - - -
114 
115 inline void
117 {
118  unsigned char *p; // carry propagation on compressed data buffer
119  for(p = ac_pointer - 1; *p == 0xFFU; p--)
120  *p = 0;
121  ++*p;
122 }
123 
124 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
125 
126 inline void
128 {
129  do
130  { // output and discard top byte
131  *ac_pointer++ = (unsigned char)(base >> 24);
132  base <<= 8;
133  } while((length <<= 8) < AC__MinLength()); // length multiplied by 256
134 }
135 
136 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
137 
138 inline void
140 {
141  do
142  { // read least-significant byte
143  value = (value << 8) | unsigned(*++ac_pointer);
144  } while((length <<= 8) < AC__MinLength()); // length multiplied by 256
145 }
146 
147 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
148 
149 inline
150 void
152 {
153 #ifdef _DEBUG
154  if(mode != 1)
155  AC_Error("encoder not initialized");
156 #endif
157 
158  length >>= 1; // halve interval
159  if(bit)
160  {
161  unsigned init_base = base;
162  base += length; // move base
163  if(init_base > base)
164  propagate_carry(); // overflow = carry
165  }
166 
167  if(length < AC__MinLength())
168  renorm_enc_interval(); // renormalization
169 }
170 
171 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
172 
173 inline
174 unsigned
176 {
177 #ifdef _DEBUG
178  if(mode != 2)
179  AC_Error("decoder not initialized");
180 #endif
181 
182  length >>= 1; // halve interval
183  unsigned bit = (value >= length); // decode bit
184  if(bit)
185  value -= length; // move base
186 
187  if(length < AC__MinLength())
188  renorm_dec_interval(); // renormalization
189 
190  return bit; // return data bit value
191 }
192 
193 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
194 
195 inline
196 void
197 Arithmetic_Codec::put_bits(unsigned data, unsigned bits)
198 {
199 #ifdef _DEBUG
200  if(mode != 1)
201  AC_Error("encoder not initialized");
202  if((bits < 1) || (bits > 20))
203  AC_Error("invalid number of bits");
204  if(data >= (1U << bits))
205  AC_Error("invalid data");
206 #endif
207 
208  unsigned init_base = base;
209  base += data * (length >>= bits); // new interval base and length
210 
211  if(init_base > base)
212  propagate_carry(); // overflow = carry
213  if(length < AC__MinLength())
214  renorm_enc_interval(); // renormalization
215 }
216 
217 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
218 
219 inline
220 unsigned
222 {
223 #ifdef _DEBUG
224  if(mode != 2)
225  AC_Error("decoder not initialized");
226  if((bits < 1) || (bits > 20))
227  AC_Error("invalid number of bits");
228 #endif
229 
230  unsigned s = value / (length >>= bits); // decode symbol, change length
231 
232  value -= length * s; // update interval
233  if(length < AC__MinLength())
234  renorm_dec_interval(); // renormalization
235 
236  return s;
237 }
238 
239 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
240 
241 inline
242 void
244 {
245 #ifdef _DEBUG
246  if(mode != 1)
247  AC_Error("encoder not initialized");
248 #endif
249 
250  unsigned x = M.bit_0_prob * (length >> BM__LengthShift()); // product l x p0
251  // update interval
252  if(bit == 0)
253  length = x;
254  else
255  {
256  unsigned init_base = base;
257  base += x;
258  length -= x;
259  if(init_base > base)
260  propagate_carry(); // overflow = carry
261  }
262 
263  if(length < AC__MinLength())
264  renorm_enc_interval(); // renormalization
265 }
266 
267 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
268 
269 inline
270 unsigned
272 {
273 #ifdef _DEBUG
274  if(mode != 2)
275  AC_Error("decoder not initialized");
276 #endif
277 
278  unsigned x = M.bit_0_prob * (length >> BM__LengthShift()); // product l x p0
279  unsigned bit = (value >= x); // decision
280  // update & shift interval
281  if(bit == 0)
282  length = x;
283  else
284  {
285  value -= x; // shifted interval base = 0
286  length -= x;
287  }
288 
289  if(length < AC__MinLength())
290  renorm_dec_interval(); // renormalization
291 
292  return bit; // return data bit value
293 }
294 
295 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
296 
297 inline
298 void
300 {
301 #ifdef _DEBUG
302  if(mode != 1)
303  AC_Error("encoder not initialized");
304 #endif
305 
306  unsigned x = M.bit_0_prob * (length >> BM__LengthShift()); // product l x p0
307  // update interval
308  if(bit == 0)
309  {
310  length = x;
311  ++M.bit_0_count;
312  }
313  else
314  {
315  unsigned init_base = base;
316  base += x;
317  length -= x;
318  if(init_base > base)
319  propagate_carry(); // overflow = carry
320  }
321 
322  if(length < AC__MinLength())
323  renorm_enc_interval(); // renormalization
324 
325  if(--M.bits_until_update == 0)
326  M.update(); // periodic model update
327 }
328 
329 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
330 
331 inline
332 unsigned
334 {
335 #ifdef _DEBUG
336  if(mode != 2)
337  AC_Error("decoder not initialized");
338 #endif
339 
340  unsigned x = M.bit_0_prob * (length >> BM__LengthShift()); // product l x p0
341  unsigned bit = (value >= x); // decision
342  // update interval
343  if(bit == 0)
344  {
345  length = x;
346  ++M.bit_0_count;
347  }
348  else
349  {
350  value -= x;
351  length -= x;
352  }
353 
354  if(length < AC__MinLength())
355  renorm_dec_interval(); // renormalization
356 
357  if(--M.bits_until_update == 0)
358  M.update(); // periodic model update
359 
360  return bit; // return data bit value
361 }
362 
363 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
364 
365 inline
366 void
368 {
369 #ifdef _DEBUG
370  if(mode != 1)
371  AC_Error("encoder not initialized");
372  if(data >= M.data_symbols)
373  AC_Error("invalid data symbol");
374 #endif
375 
376  unsigned x, init_base = base;
377  // compute products
378  if(data == M.last_symbol)
379  {
380  x = M.distribution[data] * (length >> DM__LengthShift());
381  base += x; // update interval
382  length -= x; // no product needed
383  }
384  else
385  {
386  x = M.distribution[data] * (length >>= DM__LengthShift());
387  base += x; // update interval
388  length = M.distribution[data + 1] * length - x;
389  }
390 
391  if(init_base > base)
392  propagate_carry(); // overflow = carry
393 
394  if(length < AC__MinLength())
395  renorm_enc_interval(); // renormalization
396 }
397 
398 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
399 
400 inline
401 unsigned
403 {
404 #ifdef _DEBUG
405  if(mode != 2)
406  AC_Error("decoder not initialized");
407 #endif
408 
409  unsigned n, s, x, y = length;
410 
411  if(M.decoder_table)
412  { // use table look-up for faster decoding
413 
414  unsigned dv = value / (length >>= DM__LengthShift());
415  unsigned t = dv >> M.table_shift;
416 
417  s = M.decoder_table[t]; // initial decision based on table look-up
418  n = M.decoder_table[t + 1] + 1;
419 
420  while(n > s + 1)
421  { // finish with bisection search
422  unsigned m = (s + n) >> 1;
423  if(M.distribution[m] > dv)
424  n = m;
425  else
426  s = m;
427  }
428  // compute products
429  x = M.distribution[s] * length;
430  if(s != M.last_symbol)
431  y = M.distribution[s + 1] * length;
432  }
433 
434  else
435  { // decode using only multiplications
436 
437  x = s = 0;
438  length >>= DM__LengthShift();
439  unsigned m = (n = M.data_symbols) >> 1;
440  // decode via bisection search
441  do
442  {
443  unsigned z = length * M.distribution[m];
444  if(z > value)
445  {
446  n = m;
447  y = z; // value is smaller
448  }
449  else
450  {
451  s = m;
452  x = z; // value is larger or equal
453  }
454  } while((m = (s + n) >> 1) != s);
455  }
456 
457  value -= x; // update interval
458  length = y - x;
459 
460  if(length < AC__MinLength())
461  renorm_dec_interval(); // renormalization
462 
463  return s;
464 }
465 
466 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
467 
468 inline
469 void
471 {
472 #ifdef _DEBUG
473  if(mode != 1)
474  AC_Error("encoder not initialized");
475  if(data >= M.data_symbols)
476  AC_Error("invalid data symbol");
477 #endif
478 
479  unsigned x, init_base = base;
480  // compute products
481  if(data == M.last_symbol)
482  {
483  x = M.distribution[data] * (length >> DM__LengthShift());
484  base += x; // update interval
485  length -= x; // no product needed
486  }
487  else
488  {
489  x = M.distribution[data] * (length >>= DM__LengthShift());
490  base += x; // update interval
491  length = M.distribution[data + 1] * length - x;
492  }
493 
494  if(init_base > base)
495  propagate_carry(); // overflow = carry
496 
497  if(length < AC__MinLength())
498  renorm_enc_interval(); // renormalization
499 
500  ++M.symbol_count[data];
501  if(--M.symbols_until_update == 0)
502  M.update(true); // periodic model update
503 }
504 
505 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
506 
507 inline
508 unsigned
510 {
511 #ifdef _DEBUG
512  if(mode != 2)
513  AC_Error("decoder not initialized");
514 #endif
515 
516  unsigned n, s, x, y = length;
517 
518  if(M.decoder_table)
519  { // use table look-up for faster decoding
520 
521  unsigned dv = value / (length >>= DM__LengthShift());
522  unsigned t = dv >> M.table_shift;
523 
524  s = M.decoder_table[t]; // initial decision based on table look-up
525  n = M.decoder_table[t + 1] + 1;
526 
527  while(n > s + 1)
528  { // finish with bisection search
529  unsigned m = (s + n) >> 1;
530  if(M.distribution[m] > dv)
531  n = m;
532  else
533  s = m;
534  }
535  // compute products
536  x = M.distribution[s] * length;
537  if(s != M.last_symbol)
538  y = M.distribution[s + 1] * length;
539  }
540 
541  else
542  { // decode using only multiplications
543 
544  x = s = 0;
545  length >>= DM__LengthShift();
546  unsigned m = (n = M.data_symbols) >> 1;
547  // decode via bisection search
548  do
549  {
550  unsigned z = length * M.distribution[m];
551  if(z > value)
552  {
553  n = m;
554  y = z; // value is smaller
555  }
556  else
557  {
558  s = m;
559  x = z; // value is larger or equal
560  }
561  } while((m = (s + n) >> 1) != s);
562  }
563 
564  value -= x; // update interval
565  length = y - x;
566 
567  if(length < AC__MinLength())
568  renorm_dec_interval(); // renormalization
569 
570  ++M.symbol_count[s];
571  if(--M.symbols_until_update == 0)
572  M.update(false); // periodic model update
573 
574  return s;
575 }
576 
577 
578 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
579 // - - Other Arithmetic_Codec implementations - - - - - - - - - - - - - - - -
580 
581 inline
583 {
584  mode = buffer_size = 0;
585  new_buffer = code_buffer = 0;
586 }
587 
588 inline
589 Arithmetic_Codec::Arithmetic_Codec(unsigned max_code_bytes,
590  unsigned char *user_buffer)
591 {
592  mode = buffer_size = 0;
593  new_buffer = code_buffer = 0;
594  set_buffer(max_code_bytes, user_buffer);
595 }
596 
597 inline
599 
600 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
601 
602 inline
603 void
604 Arithmetic_Codec::set_buffer(unsigned max_code_bytes,
605  unsigned char *user_buffer)
606 {
607  // test for reasonable sizes
608  if((max_code_bytes < 16) || (max_code_bytes > 0x1000000U))
609  AC_Error("invalid codec buffer size");
610  if(mode != 0)
611  AC_Error("cannot set buffer while encoding or decoding");
612 
613  if(user_buffer != 0)
614  { // user provides memory buffer
615  buffer_size = max_code_bytes;
616  code_buffer = user_buffer; // set buffer for compressed data
617  delete[] new_buffer; // free anything previously assigned
618  new_buffer = 0;
619  return;
620  }
621 
622  if(max_code_bytes <= buffer_size)
623  return; // enough available
624 
625  buffer_size = max_code_bytes; // assign new memory
626  delete[] new_buffer; // free anything previously assigned
627  if((new_buffer = new unsigned char[buffer_size + 16]) == 0) // 16 extra bytes
628  AC_Error("cannot assign memory for compressed data buffer");
629  code_buffer = new_buffer; // set buffer for compressed data
630 }
631 
632 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
633 
634 inline
635 void
637 {
638  if(mode != 0)
639  AC_Error("cannot start encoder");
640  if(buffer_size == 0)
641  AC_Error("no code buffer set");
642 
643  mode = 1;
644  base = 0; // initialize encoder variables: interval and pointer
645  length = AC__MaxLength();
646  ac_pointer = code_buffer; // pointer to next data byte
647 }
648 
649 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
650 
651 inline
652 void
654 {
655  if(mode != 0)
656  AC_Error("cannot start decoder");
657  if(buffer_size == 0)
658  AC_Error("no code buffer set");
659 
660  // initialize decoder: interval, pointer, initial code value
661  mode = 2;
662  length = AC__MaxLength();
663  ac_pointer = code_buffer + 3;
664  value = (unsigned(code_buffer[0]) << 24) | (unsigned(code_buffer[1]) << 16) |
665  (unsigned(code_buffer[2]) << 8) | unsigned(code_buffer[3]);
666 }
667 
668 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
669 
670 inline
671 void
673 {
674  unsigned shift = 0, code_bytes = 0;
675  int file_byte;
676  // read variable-length header with number of code bytes
677  do
678  {
679  if((file_byte = getc(code_file)) == EOF)
680  AC_Error("cannot read code from file");
681  code_bytes |= unsigned(file_byte & 0x7F) << shift;
682  shift += 7;
683  } while(file_byte & 0x80);
684  // read compressed data
685  if(code_bytes > buffer_size)
686  AC_Error("code buffer overflow");
687  if(fread(code_buffer, 1, code_bytes, code_file) != code_bytes)
688  AC_Error("cannot read code from file");
689 
690  start_decoder(); // initialize decoder
691 }
692 
693 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
694 
695 inline
696 unsigned
698 {
699  if(mode != 1)
700  AC_Error("invalid to stop encoder");
701  mode = 0;
702 
703  unsigned init_base = base; // done encoding: set final data bytes
704 
705  if(length > 2 * AC__MinLength())
706  {
707  base += AC__MinLength(); // base offset
708  length = AC__MinLength() >> 1; // set new length for 1 more byte
709  }
710  else
711  {
712  base += AC__MinLength() >> 1; // base offset
713  length = AC__MinLength() >> 9; // set new length for 2 more bytes
714  }
715 
716  if(init_base > base)
717  propagate_carry(); // overflow = carry
718 
719  renorm_enc_interval(); // renormalization = output last bytes
720 
721  unsigned code_bytes = unsigned(ac_pointer - code_buffer);
722  if(code_bytes > buffer_size)
723  AC_Error("code buffer overflow");
724 
725  return code_bytes; // number of bytes used
726 }
727 
728 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
729 
730 inline
731 unsigned
733 {
734  unsigned header_bytes = 0, code_bytes = stop_encoder(), nb = code_bytes;
735 
736  // write variable-length header with number of code bytes
737  do
738  {
739  int file_byte = int(nb & 0x7FU);
740  if((nb >>= 7) > 0)
741  file_byte |= 0x80;
742  if(putc(file_byte, code_file) == EOF)
743  AC_Error("cannot write compressed data to file");
744  header_bytes++;
745  } while(nb);
746  // write compressed data
747  if(fwrite(code_buffer, 1, code_bytes, code_file) != code_bytes)
748  AC_Error("cannot write compressed data to file");
749 
750  return code_bytes + header_bytes; // bytes used
751 }
752 
753 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
754 
755 inline
756 void
758 {
759  if(mode != 2)
760  AC_Error("invalid to stop decoder");
761  mode = 0;
762 }
763 
764 
765 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
766 // - Static bit model implementation - - - - - - - - - - - - - - - - - - - - -
767 
768 inline
770 {
771  bit_0_prob = 1U << (BM__LengthShift() - 1); // p0 = 0.5
772 }
773 
774 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
775 
776 inline
777 void
779 {
780  if((p0 < 0.0001) || (p0 > 0.9999))
781  AC_Error("invalid bit probability");
782  bit_0_prob = unsigned(p0 * (1 << BM__LengthShift()));
783 }
784 
785 
786 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
787 // - Adaptive bit model implementation - - - - - - - - - - - - - - - - - - - -
788 
789 inline
791 
792 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
793 
794 inline
795 void
797 {
798  // initialization to equiprobable model
799  bit_0_count = 1;
800  bit_count = 2;
801  bit_0_prob = 1U << (BM__LengthShift() - 1);
802  update_cycle = bits_until_update = 4; // start with frequent updates
803 }
804 
805 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
806 
807 inline
808 void
810 {
811  // halve counts when a threshold is reached
812 
813  if((bit_count += update_cycle) > BM__MaxCount())
814  {
815  bit_count = (bit_count + 1) >> 1;
816  bit_0_count = (bit_0_count + 1) >> 1;
817  if(bit_0_count == bit_count)
818  ++bit_count;
819  }
820  // compute scaled bit 0 probability
821  unsigned scale = 0x80000000U / bit_count;
822  bit_0_prob = (bit_0_count * scale) >> (31 - BM__LengthShift());
823 
824  // set frequency of model updates
825  update_cycle = (5 * update_cycle) >> 2;
826  if(update_cycle > 64)
827  update_cycle = 64;
829 }
830 
831 
832 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
833 // - - Static data model implementation - - - - - - - - - - - - - - - - - - -
834 
835 inline
837 {
838  data_symbols = 0;
839  distribution = 0;
840 }
841 
842 inline
844 
845 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
846 
847 inline
848 void
849 Static_Data_Model::set_distribution(unsigned number_of_symbols,
850  const double probability[])
851 {
852  if((number_of_symbols < 2) || (number_of_symbols > (1 << 11)))
853  AC_Error("invalid number of data symbols");
854 
855  if(data_symbols != number_of_symbols)
856  { // assign memory for data model
857  data_symbols = number_of_symbols;
859  delete[] distribution;
860  // define size of table for fast decoding
861  if(data_symbols > 16)
862  {
863  unsigned table_bits = 3;
864  while(data_symbols > (1U << (table_bits + 2)))
865  ++table_bits;
866  table_size = (1 << table_bits) + 4;
867  table_shift = DM__LengthShift() - table_bits;
868  distribution = new unsigned[data_symbols + table_size + 6];
870  }
871  else
872  { // small alphabet: no table needed
873  decoder_table = 0;
874  table_size = table_shift = 0;
875  distribution = new unsigned[data_symbols];
876  }
877  if(distribution == 0)
878  AC_Error("cannot assign model memory");
879  }
880  // compute cumulative distribution, decoder table
881  unsigned s = 0;
882  double sum = 0.0, p = 1.0 / double(data_symbols);
883 
884  for(unsigned k = 0; k < data_symbols; k++)
885  {
886  if(probability)
887  p = probability[k];
888  if((p < 0.0001) || (p > 0.9999))
889  AC_Error("invalid symbol probability");
890  distribution[k] = unsigned(sum * (1 << DM__LengthShift()));
891  sum += p;
892  if(table_size == 0)
893  continue;
894  unsigned w = distribution[k] >> table_shift;
895  while(s < w)
896  decoder_table[++s] = k - 1;
897  }
898 
899  if(table_size != 0)
900  {
901  decoder_table[0] = 0;
902  while(s <= table_size)
903  decoder_table[++s] = data_symbols - 1;
904  }
905 
906  if((sum < 0.9999) || (sum > 1.0001))
907  AC_Error("invalid probabilities");
908 }
909 
910 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
911 // - - Adaptive data model implementation - - - - - - - - - - - - - - - - - -
912 
913 inline
915 {
916  data_symbols = 0;
917  distribution = 0;
918 }
919 
920 inline
921 Adaptive_Data_Model::Adaptive_Data_Model(unsigned number_of_symbols)
922 {
923  data_symbols = 0;
924  distribution = 0;
925  set_alphabet(number_of_symbols);
926 }
927 
928 inline
930 
931 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
932 
933 inline
934 void
935 Adaptive_Data_Model::set_alphabet(unsigned number_of_symbols)
936 {
937  if((number_of_symbols < 2) || (number_of_symbols > (1 << 13)))
938  AC_Error("invalid number of data symbols");
939 
940  if(data_symbols != number_of_symbols)
941  { // assign memory for data model
942  data_symbols = number_of_symbols;
944  delete[] distribution;
945  // define size of table for fast decoding
946  if(data_symbols > 16)
947  {
948  unsigned table_bits = 3;
949  while(data_symbols > (1U << (table_bits + 2)))
950  ++table_bits;
951  table_size = (1 << table_bits) + 4;
952  table_shift = DM__LengthShift() - table_bits;
953  distribution = new unsigned[2 * data_symbols + table_size + 6];
955  }
956  else
957  { // small alphabet: no table needed
958  decoder_table = 0;
959  table_size = table_shift = 0;
960  distribution = new unsigned[2 * data_symbols];
961  }
963  if(distribution == 0)
964  AC_Error("cannot assign model memory");
965  }
966 
967  reset(); // initialize model
968 }
969 
970 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
971 
972 inline
973 void
974 Adaptive_Data_Model::update(bool from_encoder)
975 {
976  // halve counts when a threshold is reached
977 
979  {
980  total_count = 0;
981  for(unsigned n = 0; n < data_symbols; n++)
982  total_count += (symbol_count[n] = (symbol_count[n] + 1) >> 1);
983  }
984  // compute cumulative distribution, decoder table
985  unsigned k, sum = 0, s = 0;
986  unsigned scale = 0x80000000U / total_count;
987 
988  if(from_encoder || (table_size == 0))
989  for(k = 0; k < data_symbols; k++)
990  {
991  distribution[k] = (scale * sum) >> (31 - DM__LengthShift());
992  sum += symbol_count[k];
993  }
994  else
995  {
996  for(k = 0; k < data_symbols; k++)
997  {
998  distribution[k] = (scale * sum) >> (31 - DM__LengthShift());
999  sum += symbol_count[k];
1000  unsigned w = distribution[k] >> table_shift;
1001  while(s < w)
1002  decoder_table[++s] = k - 1;
1003  }
1004  decoder_table[0] = 0;
1005  while(s <= table_size)
1006  decoder_table[++s] = data_symbols - 1;
1007  }
1008  // set frequency of model updates
1009  update_cycle = (5 * update_cycle) >> 2;
1010  unsigned max_cycle = (data_symbols + 6) << 3;
1011  if(update_cycle > max_cycle)
1012  update_cycle = max_cycle;
1014 }
1015 
1016 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1017 
1018 inline
1019 void
1021 {
1022  if(data_symbols == 0)
1023  return;
1024 
1025  // restore probability estimates to uniform distribution
1026  total_count = 0;
1028  for(unsigned k = 0; k < data_symbols; k++)
1029  symbol_count[k] = 1;
1030  update(false);
1032 }
1033 
1034 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
BM__MaxCount
unsigned BM__MaxCount(void)
Definition: arithmetic_codec.inl:76
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
DM__LengthShift
unsigned DM__LengthShift(void)
Definition: arithmetic_codec.inl:84
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
Adaptive_Data_Model::distribution
unsigned * distribution
Definition: arithmetic_codec.hpp:133
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
AC_Error
void AC_Error(const char *msg)
Definition: arithmetic_codec.inl:103
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
AC__MinLength
unsigned AC__MinLength(void)
Definition: arithmetic_codec.inl:56
Adaptive_Data_Model::last_symbol
unsigned last_symbol
Definition: arithmetic_codec.hpp:135
DM__MaxCount
unsigned DM__MaxCount(void)
Definition: arithmetic_codec.inl:90
Arithmetic_Codec::set_buffer
void set_buffer(unsigned max_code_bytes, unsigned char *user_buffer=0)
Definition: arithmetic_codec.inl:604
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::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
AC__MaxLength
unsigned AC__MaxLength(void)
Definition: arithmetic_codec.inl:62
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
BM__LengthShift
unsigned BM__LengthShift(void)
Definition: arithmetic_codec.inl:70
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_Bit_Model::reset
void reset(void)
Definition: arithmetic_codec.inl:796
Adaptive_Data_Model::total_count
unsigned total_count
Definition: arithmetic_codec.hpp:134