o3dgcArithmeticCodec.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. // - - Inclusion - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  65. #include <stdlib.h>
  66. #include "o3dgcArithmeticCodec.h"
  67. namespace o3dgc
  68. {
  69. // - - Constants - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  70. const unsigned AC__MinLength = 0x01000000U; // threshold for renormalization
  71. const unsigned AC__MaxLength = 0xFFFFFFFFU; // maximum AC interval length
  72. // Maximum values for binary models
  73. const unsigned BM__LengthShift = 13; // length bits discarded before mult.
  74. const unsigned BM__MaxCount = 1 << BM__LengthShift; // for adaptive models
  75. // Maximum values for general models
  76. const unsigned DM__LengthShift = 15; // length bits discarded before mult.
  77. const unsigned DM__MaxCount = 1 << DM__LengthShift; // for adaptive models
  78. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  79. // - - Static functions - - - - - - - - - - - - - - - - - - - - - - - - - - -
  80. static void AC_Error(const char * msg)
  81. {
  82. fprintf(stderr, "\n\n -> Arithmetic coding error: ");
  83. fputs(msg, stderr);
  84. fputs("\n Execution terminated!\n", stderr);
  85. getchar();
  86. exit(1);
  87. }
  88. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  89. // - - Coding implementations - - - - - - - - - - - - - - - - - - - - - - - -
  90. inline void Arithmetic_Codec::propagate_carry(void)
  91. {
  92. unsigned char * p; // carry propagation on compressed data buffer
  93. for (p = ac_pointer - 1; *p == 0xFFU; p--) *p = 0;
  94. ++*p;
  95. }
  96. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  97. inline void Arithmetic_Codec::renorm_enc_interval(void)
  98. {
  99. do { // output and discard top byte
  100. *ac_pointer++ = (unsigned char)(base >> 24);
  101. base <<= 8;
  102. } while ((length <<= 8) < AC__MinLength); // length multiplied by 256
  103. }
  104. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  105. inline void Arithmetic_Codec::renorm_dec_interval(void)
  106. {
  107. do { // read least-significant byte
  108. value = (value << 8) | unsigned(*++ac_pointer);
  109. } while ((length <<= 8) < AC__MinLength); // length multiplied by 256
  110. }
  111. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  112. void Arithmetic_Codec::put_bit(unsigned bit)
  113. {
  114. #ifdef _DEBUG
  115. if (mode != 1) AC_Error("encoder not initialized");
  116. #endif
  117. length >>= 1; // halve interval
  118. if (bit) {
  119. unsigned init_base = base;
  120. base += length; // move base
  121. if (init_base > base) propagate_carry(); // overflow = carry
  122. }
  123. if (length < AC__MinLength) renorm_enc_interval(); // renormalization
  124. }
  125. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  126. unsigned Arithmetic_Codec::get_bit(void)
  127. {
  128. #ifdef _DEBUG
  129. if (mode != 2) AC_Error("decoder not initialized");
  130. #endif
  131. length >>= 1; // halve interval
  132. unsigned bit = (value >= length); // decode bit
  133. if (bit) value -= length; // move base
  134. if (length < AC__MinLength) renorm_dec_interval(); // renormalization
  135. return bit; // return data bit value
  136. }
  137. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  138. void Arithmetic_Codec::put_bits(unsigned data, unsigned bits)
  139. {
  140. #ifdef _DEBUG
  141. if (mode != 1) AC_Error("encoder not initialized");
  142. if ((bits < 1) || (bits > 20)) AC_Error("invalid number of bits");
  143. if (data >= (1U << bits)) AC_Error("invalid data");
  144. #endif
  145. unsigned init_base = base;
  146. base += data * (length >>= bits); // new interval base and length
  147. if (init_base > base) propagate_carry(); // overflow = carry
  148. if (length < AC__MinLength) renorm_enc_interval(); // renormalization
  149. }
  150. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  151. unsigned Arithmetic_Codec::get_bits(unsigned bits)
  152. {
  153. #ifdef _DEBUG
  154. if (mode != 2) AC_Error("decoder not initialized");
  155. if ((bits < 1) || (bits > 20)) AC_Error("invalid number of bits");
  156. #endif
  157. unsigned s = value / (length >>= bits); // decode symbol, change length
  158. value -= length * s; // update interval
  159. if (length < AC__MinLength) renorm_dec_interval(); // renormalization
  160. return s;
  161. }
  162. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  163. void Arithmetic_Codec::encode(unsigned bit,
  164. Static_Bit_Model & M)
  165. {
  166. #ifdef _DEBUG
  167. if (mode != 1) AC_Error("encoder not initialized");
  168. #endif
  169. unsigned x = M.bit_0_prob * (length >> BM__LengthShift); // product l x p0
  170. // update interval
  171. if (bit == 0)
  172. length = x;
  173. else {
  174. unsigned init_base = base;
  175. base += x;
  176. length -= x;
  177. if (init_base > base) propagate_carry(); // overflow = carry
  178. }
  179. if (length < AC__MinLength) renorm_enc_interval(); // renormalization
  180. }
  181. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  182. unsigned Arithmetic_Codec::decode(Static_Bit_Model & M)
  183. {
  184. #ifdef _DEBUG
  185. if (mode != 2) AC_Error("decoder not initialized");
  186. #endif
  187. unsigned x = M.bit_0_prob * (length >> BM__LengthShift); // product l x p0
  188. unsigned bit = (value >= x); // decision
  189. // update & shift interval
  190. if (bit == 0)
  191. length = x;
  192. else {
  193. value -= x; // shifted interval base = 0
  194. length -= x;
  195. }
  196. if (length < AC__MinLength) renorm_dec_interval(); // renormalization
  197. return bit; // return data bit value
  198. }
  199. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  200. void Arithmetic_Codec::encode(unsigned bit,
  201. Adaptive_Bit_Model & M)
  202. {
  203. #ifdef _DEBUG
  204. if (mode != 1) AC_Error("encoder not initialized");
  205. #endif
  206. unsigned x = M.bit_0_prob * (length >> BM__LengthShift); // product l x p0
  207. // update interval
  208. if (bit == 0) {
  209. length = x;
  210. ++M.bit_0_count;
  211. }
  212. else {
  213. unsigned init_base = base;
  214. base += x;
  215. length -= x;
  216. if (init_base > base) propagate_carry(); // overflow = carry
  217. }
  218. if (length < AC__MinLength) renorm_enc_interval(); // renormalization
  219. if (--M.bits_until_update == 0) M.update(); // periodic model update
  220. }
  221. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  222. unsigned Arithmetic_Codec::decode(Adaptive_Bit_Model & M)
  223. {
  224. #ifdef _DEBUG
  225. if (mode != 2) AC_Error("decoder not initialized");
  226. #endif
  227. unsigned x = M.bit_0_prob * (length >> BM__LengthShift); // product l x p0
  228. unsigned bit = (value >= x); // decision
  229. // update interval
  230. if (bit == 0) {
  231. length = x;
  232. ++M.bit_0_count;
  233. }
  234. else {
  235. value -= x;
  236. length -= x;
  237. }
  238. if (length < AC__MinLength) renorm_dec_interval(); // renormalization
  239. if (--M.bits_until_update == 0) M.update(); // periodic model update
  240. return bit; // return data bit value
  241. }
  242. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  243. void Arithmetic_Codec::encode(unsigned data,
  244. Static_Data_Model & M)
  245. {
  246. #ifdef _DEBUG
  247. if (mode != 1) AC_Error("encoder not initialized");
  248. if (data >= M.data_symbols) AC_Error("invalid data symbol");
  249. #endif
  250. unsigned x, init_base = base;
  251. // compute products
  252. if (data == M.last_symbol) {
  253. x = M.distribution[data] * (length >> DM__LengthShift);
  254. base += x; // update interval
  255. length -= x; // no product needed
  256. }
  257. else {
  258. x = M.distribution[data] * (length >>= DM__LengthShift);
  259. base += x; // update interval
  260. length = M.distribution[data+1] * length - x;
  261. }
  262. if (init_base > base) propagate_carry(); // overflow = carry
  263. if (length < AC__MinLength) renorm_enc_interval(); // renormalization
  264. }
  265. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  266. unsigned Arithmetic_Codec::decode(Static_Data_Model & M)
  267. {
  268. #ifdef _DEBUG
  269. if (mode != 2) AC_Error("decoder not initialized");
  270. #endif
  271. unsigned n, s, x, y = length;
  272. if (M.decoder_table) { // use table look-up for faster decoding
  273. unsigned dv = value / (length >>= DM__LengthShift);
  274. unsigned t = dv >> M.table_shift;
  275. s = M.decoder_table[t]; // initial decision based on table look-up
  276. n = M.decoder_table[t+1] + 1;
  277. while (n > s + 1) { // finish with bisection search
  278. unsigned m = (s + n) >> 1;
  279. if (M.distribution[m] > dv) n = m; else s = m;
  280. }
  281. // compute products
  282. x = M.distribution[s] * length;
  283. if (s != M.last_symbol) y = M.distribution[s+1] * length;
  284. }
  285. else { // decode using only multiplications
  286. x = s = 0;
  287. length >>= DM__LengthShift;
  288. unsigned m = (n = M.data_symbols) >> 1;
  289. // decode via bisection search
  290. do {
  291. unsigned z = length * M.distribution[m];
  292. if (z > value) {
  293. n = m;
  294. y = z; // value is smaller
  295. }
  296. else {
  297. s = m;
  298. x = z; // value is larger or equal
  299. }
  300. } while ((m = (s + n) >> 1) != s);
  301. }
  302. value -= x; // update interval
  303. length = y - x;
  304. if (length < AC__MinLength) renorm_dec_interval(); // renormalization
  305. return s;
  306. }
  307. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  308. void Arithmetic_Codec::encode(unsigned data,
  309. Adaptive_Data_Model & M)
  310. {
  311. #ifdef _DEBUG
  312. if (mode != 1) AC_Error("encoder not initialized");
  313. if (data >= M.data_symbols)
  314. {
  315. AC_Error("invalid data symbol");
  316. }
  317. #endif
  318. unsigned x, init_base = base;
  319. // compute products
  320. if (data == M.last_symbol) {
  321. x = M.distribution[data] * (length >> DM__LengthShift);
  322. base += x; // update interval
  323. length -= x; // no product needed
  324. }
  325. else {
  326. x = M.distribution[data] * (length >>= DM__LengthShift);
  327. base += x; // update interval
  328. length = M.distribution[data+1] * length - x;
  329. }
  330. if (init_base > base) propagate_carry(); // overflow = carry
  331. if (length < AC__MinLength) renorm_enc_interval(); // renormalization
  332. ++M.symbol_count[data];
  333. if (--M.symbols_until_update == 0) M.update(true); // periodic model update
  334. }
  335. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  336. unsigned Arithmetic_Codec::decode(Adaptive_Data_Model & M)
  337. {
  338. #ifdef _DEBUG
  339. if (mode != 2) AC_Error("decoder not initialized");
  340. #endif
  341. unsigned n, s, x, y = length;
  342. if (M.decoder_table) { // use table look-up for faster decoding
  343. unsigned dv = value / (length >>= DM__LengthShift);
  344. unsigned t = dv >> M.table_shift;
  345. s = M.decoder_table[t]; // initial decision based on table look-up
  346. n = M.decoder_table[t+1] + 1;
  347. while (n > s + 1) { // finish with bisection search
  348. unsigned m = (s + n) >> 1;
  349. if (M.distribution[m] > dv) n = m; else s = m;
  350. }
  351. // compute products
  352. x = M.distribution[s] * length;
  353. if (s != M.last_symbol) {
  354. y = M.distribution[s+1] * length;
  355. }
  356. }
  357. else { // decode using only multiplications
  358. x = s = 0;
  359. length >>= DM__LengthShift;
  360. unsigned m = (n = M.data_symbols) >> 1;
  361. // decode via bisection search
  362. do {
  363. unsigned z = length * M.distribution[m];
  364. if (z > value) {
  365. n = m;
  366. y = z; // value is smaller
  367. }
  368. else {
  369. s = m;
  370. x = z; // value is larger or equal
  371. }
  372. } while ((m = (s + n) >> 1) != s);
  373. }
  374. value -= x; // update interval
  375. length = y - x;
  376. if (length < AC__MinLength) renorm_dec_interval(); // renormalization
  377. ++M.symbol_count[s];
  378. if (--M.symbols_until_update == 0) M.update(false); // periodic model update
  379. return s;
  380. }
  381. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  382. // - - Other Arithmetic_Codec implementations - - - - - - - - - - - - - - - -
  383. Arithmetic_Codec::Arithmetic_Codec(void)
  384. {
  385. mode = buffer_size = 0;
  386. new_buffer = code_buffer = 0;
  387. }
  388. Arithmetic_Codec::Arithmetic_Codec(unsigned max_code_bytes,
  389. unsigned char * user_buffer)
  390. {
  391. mode = buffer_size = 0;
  392. new_buffer = code_buffer = 0;
  393. set_buffer(max_code_bytes, user_buffer);
  394. }
  395. Arithmetic_Codec::~Arithmetic_Codec(void)
  396. {
  397. delete [] new_buffer;
  398. }
  399. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  400. void Arithmetic_Codec::set_buffer(unsigned max_code_bytes,
  401. unsigned char * user_buffer)
  402. {
  403. // test for reasonable sizes
  404. if (!max_code_bytes)// || (max_code_bytes > 0x10000000U)) // updated by K. Mammou
  405. {
  406. AC_Error("invalid codec buffer size");
  407. }
  408. if (mode != 0) AC_Error("cannot set buffer while encoding or decoding");
  409. if (user_buffer != 0) { // user provides memory buffer
  410. buffer_size = max_code_bytes;
  411. code_buffer = user_buffer; // set buffer for compressed data
  412. delete [] new_buffer; // free anything previously assigned
  413. new_buffer = 0;
  414. return;
  415. }
  416. if (max_code_bytes <= buffer_size) return; // enough available
  417. buffer_size = max_code_bytes; // assign new memory
  418. delete [] new_buffer; // free anything previously assigned
  419. if ((new_buffer = new unsigned char[buffer_size+16]) == 0) // 16 extra bytes
  420. AC_Error("cannot assign memory for compressed data buffer");
  421. code_buffer = new_buffer; // set buffer for compressed data
  422. }
  423. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  424. void Arithmetic_Codec::start_encoder(void)
  425. {
  426. if (mode != 0) AC_Error("cannot start encoder");
  427. if (buffer_size == 0) AC_Error("no code buffer set");
  428. mode = 1;
  429. base = 0; // initialize encoder variables: interval and pointer
  430. length = AC__MaxLength;
  431. ac_pointer = code_buffer; // pointer to next data byte
  432. }
  433. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  434. void Arithmetic_Codec::start_decoder(void)
  435. {
  436. if (mode != 0) AC_Error("cannot start decoder");
  437. if (buffer_size == 0) AC_Error("no code buffer set");
  438. // initialize decoder: interval, pointer, initial code value
  439. mode = 2;
  440. length = AC__MaxLength;
  441. ac_pointer = code_buffer + 3;
  442. value = (unsigned(code_buffer[0]) << 24)|(unsigned(code_buffer[1]) << 16) |
  443. (unsigned(code_buffer[2]) << 8)| unsigned(code_buffer[3]);
  444. }
  445. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  446. void Arithmetic_Codec::read_from_file(FILE * code_file)
  447. {
  448. unsigned shift = 0, code_bytes = 0;
  449. int file_byte;
  450. // read variable-length header with number of code bytes
  451. do {
  452. if ((file_byte = getc(code_file)) == EOF)
  453. AC_Error("cannot read code from file");
  454. code_bytes |= unsigned(file_byte & 0x7F) << shift;
  455. shift += 7;
  456. } while (file_byte & 0x80);
  457. // read compressed data
  458. if (code_bytes > buffer_size) AC_Error("code buffer overflow");
  459. if (fread(code_buffer, 1, code_bytes, code_file) != code_bytes)
  460. AC_Error("cannot read code from file");
  461. start_decoder(); // initialize decoder
  462. }
  463. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  464. unsigned Arithmetic_Codec::stop_encoder(void)
  465. {
  466. if (mode != 1) AC_Error("invalid to stop encoder");
  467. mode = 0;
  468. unsigned init_base = base; // done encoding: set final data bytes
  469. if (length > 2 * AC__MinLength) {
  470. base += AC__MinLength; // base offset
  471. length = AC__MinLength >> 1; // set new length for 1 more byte
  472. }
  473. else {
  474. base += AC__MinLength >> 1; // base offset
  475. length = AC__MinLength >> 9; // set new length for 2 more bytes
  476. }
  477. if (init_base > base) propagate_carry(); // overflow = carry
  478. renorm_enc_interval(); // renormalization = output last bytes
  479. unsigned code_bytes = unsigned(ac_pointer - code_buffer);
  480. if (code_bytes > buffer_size) AC_Error("code buffer overflow");
  481. return code_bytes; // number of bytes used
  482. }
  483. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  484. unsigned Arithmetic_Codec::write_to_file(FILE * code_file)
  485. {
  486. unsigned header_bytes = 0, code_bytes = stop_encoder(), nb = code_bytes;
  487. // write variable-length header with number of code bytes
  488. do {
  489. int file_byte = int(nb & 0x7FU);
  490. if ((nb >>= 7) > 0) file_byte |= 0x80;
  491. if (putc(file_byte, code_file) == EOF)
  492. AC_Error("cannot write compressed data to file");
  493. header_bytes++;
  494. } while (nb);
  495. // write compressed data
  496. if (fwrite(code_buffer, 1, code_bytes, code_file) != code_bytes)
  497. AC_Error("cannot write compressed data to file");
  498. return code_bytes + header_bytes; // bytes used
  499. }
  500. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  501. void Arithmetic_Codec::stop_decoder(void)
  502. {
  503. if (mode != 2) AC_Error("invalid to stop decoder");
  504. mode = 0;
  505. }
  506. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  507. // - Static bit model implementation - - - - - - - - - - - - - - - - - - - - -
  508. Static_Bit_Model::Static_Bit_Model(void)
  509. {
  510. bit_0_prob = 1U << (BM__LengthShift - 1); // p0 = 0.5
  511. }
  512. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  513. void Static_Bit_Model::set_probability_0(double p0)
  514. {
  515. if ((p0 < 0.0001)||(p0 > 0.9999)) AC_Error("invalid bit probability");
  516. bit_0_prob = unsigned(p0 * (1 << BM__LengthShift));
  517. }
  518. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  519. // - Adaptive bit model implementation - - - - - - - - - - - - - - - - - - - -
  520. Adaptive_Bit_Model::Adaptive_Bit_Model(void)
  521. {
  522. reset();
  523. }
  524. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  525. void Adaptive_Bit_Model::reset(void)
  526. {
  527. // initialization to equiprobable model
  528. bit_0_count = 1;
  529. bit_count = 2;
  530. bit_0_prob = 1U << (BM__LengthShift - 1);
  531. update_cycle = bits_until_update = 4; // start with frequent updates
  532. }
  533. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  534. void Adaptive_Bit_Model::update(void)
  535. {
  536. // halve counts when a threshold is reached
  537. if ((bit_count += update_cycle) > BM__MaxCount) {
  538. bit_count = (bit_count + 1) >> 1;
  539. bit_0_count = (bit_0_count + 1) >> 1;
  540. if (bit_0_count == bit_count) ++bit_count;
  541. }
  542. // compute scaled bit 0 probability
  543. unsigned scale = 0x80000000U / bit_count;
  544. bit_0_prob = (bit_0_count * scale) >> (31 - BM__LengthShift);
  545. // set frequency of model updates
  546. update_cycle = (5 * update_cycle) >> 2;
  547. if (update_cycle > 64) update_cycle = 64;
  548. bits_until_update = update_cycle;
  549. }
  550. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  551. // - - Static data model implementation - - - - - - - - - - - - - - - - - - -
  552. Static_Data_Model::Static_Data_Model(void)
  553. {
  554. data_symbols = 0;
  555. distribution = 0;
  556. }
  557. Static_Data_Model::~Static_Data_Model(void)
  558. {
  559. delete [] distribution;
  560. }
  561. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  562. void Static_Data_Model::set_distribution(unsigned number_of_symbols,
  563. const double probability[])
  564. {
  565. if ((number_of_symbols < 2) || (number_of_symbols > (1 << 11)))
  566. AC_Error("invalid number of data symbols");
  567. if (data_symbols != number_of_symbols) { // assign memory for data model
  568. data_symbols = number_of_symbols;
  569. last_symbol = data_symbols - 1;
  570. delete [] distribution;
  571. // define size of table for fast decoding
  572. if (data_symbols > 16) {
  573. unsigned table_bits = 3;
  574. while (data_symbols > (1U << (table_bits + 2))) ++table_bits;
  575. table_size = 1 << table_bits;
  576. table_shift = DM__LengthShift - table_bits;
  577. distribution = new unsigned[data_symbols+table_size+2];
  578. decoder_table = distribution + data_symbols;
  579. }
  580. else { // small alphabet: no table needed
  581. decoder_table = 0;
  582. table_size = table_shift = 0;
  583. distribution = new unsigned[data_symbols];
  584. }
  585. if (distribution == 0) AC_Error("cannot assign model memory");
  586. }
  587. // compute cumulative distribution, decoder table
  588. unsigned s = 0;
  589. double sum = 0.0, p = 1.0 / double(data_symbols);
  590. for (unsigned k = 0; k < data_symbols; k++) {
  591. if (probability) p = probability[k];
  592. if ((p < 0.0001) || (p > 0.9999)) AC_Error("invalid symbol probability");
  593. distribution[k] = unsigned(sum * (1 << DM__LengthShift));
  594. sum += p;
  595. if (table_size == 0) continue;
  596. unsigned w = distribution[k] >> table_shift;
  597. while (s < w) decoder_table[++s] = k - 1;
  598. }
  599. if (table_size != 0) {
  600. decoder_table[0] = 0;
  601. while (s <= table_size) decoder_table[++s] = data_symbols - 1;
  602. }
  603. if ((sum < 0.9999) || (sum > 1.0001)) AC_Error("invalid probabilities");
  604. }
  605. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  606. // - - Adaptive data model implementation - - - - - - - - - - - - - - - - - -
  607. Adaptive_Data_Model::Adaptive_Data_Model(void)
  608. {
  609. data_symbols = 0;
  610. distribution = 0;
  611. }
  612. Adaptive_Data_Model::Adaptive_Data_Model(unsigned number_of_symbols)
  613. {
  614. data_symbols = 0;
  615. distribution = 0;
  616. set_alphabet(number_of_symbols);
  617. }
  618. Adaptive_Data_Model::~Adaptive_Data_Model(void)
  619. {
  620. delete [] distribution;
  621. }
  622. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  623. void Adaptive_Data_Model::set_alphabet(unsigned number_of_symbols)
  624. {
  625. if ((number_of_symbols < 2) || (number_of_symbols > (1 << 11)))
  626. AC_Error("invalid number of data symbols");
  627. if (data_symbols != number_of_symbols) { // assign memory for data model
  628. data_symbols = number_of_symbols;
  629. last_symbol = data_symbols - 1;
  630. delete [] distribution;
  631. // define size of table for fast decoding
  632. if (data_symbols > 16) {
  633. unsigned table_bits = 3;
  634. while (data_symbols > (1U << (table_bits + 2))) ++table_bits;
  635. table_size = 1 << table_bits;
  636. table_shift = DM__LengthShift - table_bits;
  637. distribution = new unsigned[2*data_symbols+table_size+2];
  638. decoder_table = distribution + 2 * data_symbols;
  639. }
  640. else { // small alphabet: no table needed
  641. decoder_table = 0;
  642. table_size = table_shift = 0;
  643. distribution = new unsigned[2*data_symbols];
  644. }
  645. symbol_count = distribution + data_symbols;
  646. if (distribution == 0) AC_Error("cannot assign model memory");
  647. }
  648. reset(); // initialize model
  649. }
  650. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  651. void Adaptive_Data_Model::update(bool from_encoder)
  652. {
  653. // halve counts when a threshold is reached
  654. if ((total_count += update_cycle) > DM__MaxCount) {
  655. total_count = 0;
  656. for (unsigned n = 0; n < data_symbols; n++)
  657. total_count += (symbol_count[n] = (symbol_count[n] + 1) >> 1);
  658. }
  659. assert(total_count > 0);
  660. // compute cumulative distribution, decoder table
  661. unsigned k, sum = 0, s = 0;
  662. unsigned scale = 0x80000000U / total_count;
  663. if (from_encoder || (table_size == 0))
  664. for (k = 0; k < data_symbols; k++) {
  665. distribution[k] = (scale * sum) >> (31 - DM__LengthShift);
  666. sum += symbol_count[k];
  667. }
  668. else {
  669. assert(decoder_table);
  670. for (k = 0; k < data_symbols; k++) {
  671. distribution[k] = (scale * sum) >> (31 - DM__LengthShift);
  672. sum += symbol_count[k];
  673. unsigned w = distribution[k] >> table_shift;
  674. while (s < w) decoder_table[++s] = k - 1;
  675. }
  676. decoder_table[0] = 0;
  677. while (s <= table_size) decoder_table[++s] = data_symbols - 1;
  678. }
  679. // set frequency of model updates
  680. update_cycle = (5 * update_cycle) >> 2;
  681. unsigned max_cycle = (data_symbols + 6) << 3;
  682. if (update_cycle > max_cycle) update_cycle = max_cycle;
  683. symbols_until_update = update_cycle;
  684. }
  685. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  686. void Adaptive_Data_Model::reset(void)
  687. {
  688. if (data_symbols == 0) return;
  689. // restore probability estimates to uniform distribution
  690. total_count = 0;
  691. update_cycle = data_symbols;
  692. for (unsigned k = 0; k < data_symbols; k++) symbol_count[k] = 1;
  693. update(false);
  694. symbols_until_update = update_cycle = (data_symbols + 6) >> 1;
  695. }
  696. }
  697. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */