o3dgcArithmeticCodec.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. Copyright (c) 2004 Amir Said ([email protected]) & William A. Pearlman ([email protected])
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. - Redistributions of source code must retain the above copyright notice, this list
  7. of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright notice, this list of
  9. conditions and the following disclaimer in the documentation and/or other materials
  10. provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  12. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  13. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  14. THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  15. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  16. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  17. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  18. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  19. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  20. OF SUCH DAMAGE.
  21. */
  22. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  23. // -
  24. // **************************** -
  25. // ARITHMETIC CODING EXAMPLES -
  26. // **************************** -
  27. // -
  28. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  29. // -
  30. // Fast arithmetic coding implementation -
  31. // -> 32-bit variables, 32-bit product, periodic updates, table decoding -
  32. // -
  33. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  34. // -
  35. // Version 1.00 - April 25, 2004 -
  36. // -
  37. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  38. // -
  39. // WARNING -
  40. // ========= -
  41. // -
  42. // The only purpose of this program is to demonstrate the basic principles -
  43. // of arithmetic coding. It is provided as is, without any express or -
  44. // implied warranty, without even the warranty of fitness for any particular -
  45. // purpose, or that the implementations are correct. -
  46. // -
  47. // Permission to copy and redistribute this code is hereby granted, provided -
  48. // that this warning and copyright notices are not removed or altered. -
  49. // -
  50. // Copyright (c) 2004 by Amir Said ([email protected]) & -
  51. // William A. Pearlman ([email protected]) -
  52. // -
  53. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  54. // -
  55. // A description of the arithmetic coding method used here is available in -
  56. // -
  57. // Lossless Compression Handbook, ed. K. Sayood -
  58. // Chapter 5: Arithmetic Coding (A. Said), pp. 101-152, Academic Press, 2003 -
  59. // -
  60. // A. Said, Introduction to Arithetic Coding Theory and Practice -
  61. // HP Labs report HPL-2004-76 - http://www.hpl.hp.com/techreports/ -
  62. // -
  63. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  64. // - - Definitions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  65. #ifndef O3DGC_ARITHMETIC_CODEC
  66. #define O3DGC_ARITHMETIC_CODEC
  67. #include <stdio.h>
  68. #include "o3dgcCommon.h"
  69. namespace o3dgc
  70. {
  71. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  72. // - - Class definitions - - - - - - - - - - - - - - - - - - - - - - - - - - -
  73. class Static_Bit_Model // static model for binary data
  74. {
  75. public:
  76. Static_Bit_Model(void);
  77. void set_probability_0(double); // set probability of symbol '0'
  78. private: // . . . . . . . . . . . . . . . . . . . . . .
  79. unsigned bit_0_prob;
  80. friend class Arithmetic_Codec;
  81. };
  82. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  83. class Static_Data_Model // static model for general data
  84. {
  85. public:
  86. Static_Data_Model(void);
  87. ~Static_Data_Model(void);
  88. unsigned model_symbols(void) { return data_symbols; }
  89. void set_distribution(unsigned number_of_symbols,
  90. const double probability[] = 0); // 0 means uniform
  91. private: // . . . . . . . . . . . . . . . . . . . . . .
  92. unsigned * distribution, * decoder_table;
  93. unsigned data_symbols, last_symbol, table_size, table_shift;
  94. friend class Arithmetic_Codec;
  95. };
  96. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  97. class Adaptive_Bit_Model // adaptive model for binary data
  98. {
  99. public:
  100. Adaptive_Bit_Model(void);
  101. void reset(void); // reset to equiprobable model
  102. private: // . . . . . . . . . . . . . . . . . . . . . .
  103. void update(void);
  104. unsigned update_cycle, bits_until_update;
  105. unsigned bit_0_prob, bit_0_count, bit_count;
  106. friend class Arithmetic_Codec;
  107. };
  108. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  109. class Adaptive_Data_Model // adaptive model for binary data
  110. {
  111. public:
  112. Adaptive_Data_Model(void);
  113. Adaptive_Data_Model(unsigned number_of_symbols);
  114. ~Adaptive_Data_Model(void);
  115. unsigned model_symbols(void) { return data_symbols; }
  116. void reset(void); // reset to equiprobable model
  117. void set_alphabet(unsigned number_of_symbols);
  118. private: // . . . . . . . . . . . . . . . . . . . . . .
  119. void update(bool);
  120. unsigned * distribution, * symbol_count, * decoder_table;
  121. unsigned total_count, update_cycle, symbols_until_update;
  122. unsigned data_symbols, last_symbol, table_size, table_shift;
  123. friend class Arithmetic_Codec;
  124. };
  125. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  126. // - - Encoder and decoder class - - - - - - - - - - - - - - - - - - - - - - -
  127. // Class with both the arithmetic encoder and decoder. All compressed data is
  128. // saved to a memory buffer
  129. class Arithmetic_Codec
  130. {
  131. public:
  132. Arithmetic_Codec(void);
  133. ~Arithmetic_Codec(void);
  134. Arithmetic_Codec(unsigned max_code_bytes,
  135. unsigned char * user_buffer = 0); // 0 = assign new
  136. unsigned char * buffer(void) { return code_buffer; }
  137. void set_buffer(unsigned max_code_bytes,
  138. unsigned char * user_buffer = 0); // 0 = assign new
  139. void start_encoder(void);
  140. void start_decoder(void);
  141. void read_from_file(FILE * code_file); // read code data, start decoder
  142. unsigned stop_encoder(void); // returns number of bytes used
  143. unsigned write_to_file(FILE * code_file); // stop encoder, write code data
  144. void stop_decoder(void);
  145. void put_bit(unsigned bit);
  146. unsigned get_bit(void);
  147. void put_bits(unsigned data, unsigned number_of_bits);
  148. unsigned get_bits(unsigned number_of_bits);
  149. void encode(unsigned bit,
  150. Static_Bit_Model &);
  151. unsigned decode(Static_Bit_Model &);
  152. void encode(unsigned data,
  153. Static_Data_Model &);
  154. unsigned decode(Static_Data_Model &);
  155. void encode(unsigned bit,
  156. Adaptive_Bit_Model &);
  157. unsigned decode(Adaptive_Bit_Model &);
  158. void encode(unsigned data,
  159. Adaptive_Data_Model &);
  160. unsigned decode(Adaptive_Data_Model &);
  161. // This section was added by K. Mammou
  162. void ExpGolombEncode(unsigned int symbol,
  163. int k,
  164. Static_Bit_Model & bModel0,
  165. Adaptive_Bit_Model & bModel1)
  166. {
  167. while(1)
  168. {
  169. if (symbol >= (unsigned int)(1<<k))
  170. {
  171. encode(1, bModel1);
  172. symbol = symbol - (1<<k);
  173. k++;
  174. }
  175. else
  176. {
  177. encode(0, bModel1); // now terminated zero of unary part
  178. while (k--) // next binary part
  179. {
  180. encode((signed short)((symbol>>k)&1), bModel0);
  181. }
  182. break;
  183. }
  184. }
  185. }
  186. unsigned ExpGolombDecode(int k,
  187. Static_Bit_Model & bModel0,
  188. Adaptive_Bit_Model & bModel1)
  189. {
  190. unsigned int l;
  191. int symbol = 0;
  192. int binary_symbol = 0;
  193. do
  194. {
  195. l=decode(bModel1);
  196. if (l==1)
  197. {
  198. symbol += (1<<k);
  199. k++;
  200. }
  201. }
  202. while (l!=0);
  203. while (k--) //next binary part
  204. if (decode(bModel0)==1)
  205. {
  206. binary_symbol |= (1<<k);
  207. }
  208. return (unsigned int) (symbol+binary_symbol);
  209. }
  210. //----------------------------------------------------------
  211. private: // . . . . . . . . . . . . . . . . . . . . . .
  212. void propagate_carry(void);
  213. void renorm_enc_interval(void);
  214. void renorm_dec_interval(void);
  215. unsigned char * code_buffer, * new_buffer, * ac_pointer;
  216. unsigned base, value, length; // arithmetic coding state
  217. unsigned buffer_size, mode; // mode: 0 = undef, 1 = encoder, 2 = decoder
  218. };
  219. inline long DecodeIntACEGC(Arithmetic_Codec & acd,
  220. Adaptive_Data_Model & mModelValues,
  221. Static_Bit_Model & bModel0,
  222. Adaptive_Bit_Model & bModel1,
  223. const unsigned long exp_k,
  224. const unsigned long M)
  225. {
  226. unsigned long uiValue = acd.decode(mModelValues);
  227. if (uiValue == M)
  228. {
  229. uiValue += acd.ExpGolombDecode(exp_k, bModel0, bModel1);
  230. }
  231. return UIntToInt(uiValue);
  232. }
  233. inline unsigned long DecodeUIntACEGC(Arithmetic_Codec & acd,
  234. Adaptive_Data_Model & mModelValues,
  235. Static_Bit_Model & bModel0,
  236. Adaptive_Bit_Model & bModel1,
  237. const unsigned long exp_k,
  238. const unsigned long M)
  239. {
  240. unsigned long uiValue = acd.decode(mModelValues);
  241. if (uiValue == M)
  242. {
  243. uiValue += acd.ExpGolombDecode(exp_k, bModel0, bModel1);
  244. }
  245. return uiValue;
  246. }
  247. inline void EncodeIntACEGC(long predResidual,
  248. Arithmetic_Codec & ace,
  249. Adaptive_Data_Model & mModelValues,
  250. Static_Bit_Model & bModel0,
  251. Adaptive_Bit_Model & bModel1,
  252. const unsigned long M)
  253. {
  254. unsigned long uiValue = IntToUInt(predResidual);
  255. if (uiValue < M)
  256. {
  257. ace.encode(uiValue, mModelValues);
  258. }
  259. else
  260. {
  261. ace.encode(M, mModelValues);
  262. ace.ExpGolombEncode(uiValue-M, 0, bModel0, bModel1);
  263. }
  264. }
  265. inline void EncodeUIntACEGC(long predResidual,
  266. Arithmetic_Codec & ace,
  267. Adaptive_Data_Model & mModelValues,
  268. Static_Bit_Model & bModel0,
  269. Adaptive_Bit_Model & bModel1,
  270. const unsigned long M)
  271. {
  272. unsigned long uiValue = (unsigned long) predResidual;
  273. if (uiValue < M)
  274. {
  275. ace.encode(uiValue, mModelValues);
  276. }
  277. else
  278. {
  279. ace.encode(M, mModelValues);
  280. ace.ExpGolombEncode(uiValue-M, 0, bModel0, bModel1);
  281. }
  282. }
  283. }
  284. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  285. #endif