|
| 1 | +//=============================================================================== |
| 2 | +// |
| 3 | +// FILE: arithmeticbitmodel.cs |
| 4 | +// |
| 5 | +// CONTENTS: |
| 6 | +// |
| 7 | +// C# port of a modular C++ wrapper for an adapted version of Amir Said's FastAC Code. |
| 8 | +// see: http://www.cipr.rpi.edu/~said/FastAC.html |
| 9 | +// |
| 10 | +// PROGRAMMERS: |
| 11 | +// |
| 12 | +// martin.isenburg@rapidlasso.com - http://rapidlasso.com |
| 13 | +// |
| 14 | +// COPYRIGHT: |
| 15 | +// |
| 16 | +// (c) 2005-2017, martin isenburg, rapidlasso - fast tools to catch reality |
| 17 | +// (c) of the C# port 2014-2017 by Shinta <shintadono@googlemail.com> |
| 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: omitted for easier Copy&Paste (pls see the original) |
| 27 | +// |
| 28 | +//=============================================================================== |
| 29 | + |
| 30 | +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 31 | +// - |
| 32 | +// Fast arithmetic coding implementation - |
| 33 | +// -> 32-bit variables, 32-bit product, periodic updates, table decoding - |
| 34 | +// - |
| 35 | +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 36 | +// - |
| 37 | +// Version 1.00 - April 25, 2004 - |
| 38 | +// - |
| 39 | +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 40 | +// - |
| 41 | +// WARNING - |
| 42 | +// ========= - |
| 43 | +// - |
| 44 | +// The only purpose of this program is to demonstrate the basic principles - |
| 45 | +// of arithmetic coding. It is provided as is, without any express or - |
| 46 | +// implied warranty, without even the warranty of fitness for any particular - |
| 47 | +// purpose, or that the implementations are correct. - |
| 48 | +// - |
| 49 | +// Permission to copy and redistribute this code is hereby granted, provided - |
| 50 | +// that this warning and copyright notices are not removed or altered. - |
| 51 | +// - |
| 52 | +// Copyright (c) 2004 by Amir Said (said@ieee.org) & - |
| 53 | +// William A. Pearlman (pearlw@ecse.rpi.edu) - |
| 54 | +// - |
| 55 | +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 56 | +// - |
| 57 | +// A description of the arithmetic coding method used here is available in - |
| 58 | +// - |
| 59 | +// Lossless Compression Handbook, ed. K. Sayood - |
| 60 | +// Chapter 5: Arithmetic Coding (A. Said), pp. 101-152, Academic Press, 2003 - |
| 61 | +// - |
| 62 | +// A. Said, Introduction to Arithetic Coding Theory and Practice - |
| 63 | +// HP Labs report HPL-2004-76 - http://www.hpl.hp.com/techreports/ - |
| 64 | +// - |
| 65 | +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 66 | + |
| 67 | +namespace LASzip.Net |
| 68 | +{ |
| 69 | + class ArithmeticBitModel |
| 70 | + { |
| 71 | + public ArithmeticBitModel() |
| 72 | + { |
| 73 | + init(); |
| 74 | + } |
| 75 | + |
| 76 | + public int init() |
| 77 | + { |
| 78 | + // initialization to equiprobable model |
| 79 | + bit_0_count = 1; |
| 80 | + bit_count = 2; |
| 81 | + bit_0_prob = 1u << (BM.LengthShift - 1); |
| 82 | + |
| 83 | + // start with frequent updates |
| 84 | + update_cycle = bits_until_update = 4; |
| 85 | + |
| 86 | + return 0; |
| 87 | + } |
| 88 | + |
| 89 | + internal void update() |
| 90 | + { |
| 91 | + // halve counts when a threshold is reached |
| 92 | + if ((bit_count += update_cycle) > BM.MaxCount) |
| 93 | + { |
| 94 | + bit_count = (bit_count + 1) >> 1; |
| 95 | + bit_0_count = (bit_0_count + 1) >> 1; |
| 96 | + if (bit_0_count == bit_count) ++bit_count; |
| 97 | + } |
| 98 | + |
| 99 | + // compute scaled bit 0 probability |
| 100 | + uint scale = 0x80000000u / bit_count; |
| 101 | + bit_0_prob = (bit_0_count * scale) >> (31 - BM.LengthShift); |
| 102 | + |
| 103 | + // set frequency of model updates |
| 104 | + update_cycle = (5 * update_cycle) >> 2; |
| 105 | + if (update_cycle > 64) update_cycle = 64; |
| 106 | + bits_until_update = update_cycle; |
| 107 | + } |
| 108 | + |
| 109 | + internal uint update_cycle, bits_until_update; |
| 110 | + internal uint bit_0_prob, bit_0_count, bit_count; |
| 111 | + } |
| 112 | +} |
0 commit comments