1 /*
2 ===============================================================================
3 
4   FILE:  arithmeticmodel.hpp
5 
6   CONTENTS:
7 
8     A modular C++ wrapper for an adapted version of Amir Said's FastAC Code.
9     see: http://www.cipr.rpi.edu/~said/FastAC.html
10 
11   PROGRAMMERS:
12 
13     martin.isenburg@rapidlasso.com  -  http://rapidlasso.com
14 
15   COPYRIGHT:
16 
17     (c) 2007-2019, martin isenburg, rapidlasso - fast tools to catch reality
18 
19     This is free software; you can redistribute and/or modify it under the
20     terms of the GNU Lesser General Licence as published by the Free Software
21     Foundation. See the COPYING file for more information.
22 
23     This software is distributed WITHOUT ANY WARRANTY and without even the
24     implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
25 
26   CHANGE HISTORY:
27 
28     11 April 2019 -- 1024 AC_BUFFER_SIZE to 4096 for propagate_carry() overflow
29     10 January 2011 -- licensing change for LGPL release and liblas integration
30     8 December 2010 -- unified framework for all entropy coders
31     30 October 2009 -- refactoring Amir Said's FastAC code
32 
33 ===============================================================================
34 */
35 #ifndef ARITHMETIC_MODEL_HPP
36 #define ARITHMETIC_MODEL_HPP
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 
41 #include "mydefs.hpp"
42 
43 /* this header byte needs to change in case incompatible change happen */
44 #define AC_HEADER_BYTE 2
45 #define AC_BUFFER_SIZE 4096
46 
47 const U32 AC__MinLength = 0x01000000U;   // threshold for renormalization
48 const U32 AC__MaxLength = 0xFFFFFFFFU;      // maximum AC interval length
49 
50                                            // Maximum values for binary models
51 const U32 BM__LengthShift = 13;     // length bits discarded before mult.
52 const U32 BM__MaxCount    = 1 << BM__LengthShift;  // for adaptive models
53 
54                                           // Maximum values for general models
55 const U32 DM__LengthShift = 15;     // length bits discarded before mult.
56 const U32 DM__MaxCount    = 1 << DM__LengthShift;  // for adaptive models
57 
58 class ArithmeticModel
59 {
60 public:
61   ArithmeticModel(U32 symbols, BOOL compress);
62   ~ArithmeticModel();
63 
64   I32 init(U32* table=0);
65 
66 private:
67   void update();
68   U32 * distribution, * symbol_count, * decoder_table;
69   U32 total_count, update_cycle, symbols_until_update;
70   U32 symbols, last_symbol, table_size, table_shift;
71   BOOL compress;
72   friend class ArithmeticEncoder;
73   friend class ArithmeticDecoder;
74 };
75 
76 class ArithmeticBitModel
77 {
78 public:
79   ArithmeticBitModel();
80   ~ArithmeticBitModel();
81 
82   void init();
83 
84 private:
85   void update();
86   U32 update_cycle, bits_until_update;
87   U32 bit_0_prob, bit_0_count, bit_count;
88   friend class ArithmeticEncoder;
89   friend class ArithmeticDecoder;
90 };
91 
92 #endif
93