RangeCoder.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. namespace SevenZip.Compression.RangeCoder
  3. {
  4. class Encoder
  5. {
  6. public const uint kTopValue = (1 << 24);
  7. System.IO.Stream Stream;
  8. public UInt64 Low;
  9. public uint Range;
  10. uint _cacheSize;
  11. byte _cache;
  12. long StartPosition;
  13. public void SetStream(System.IO.Stream stream)
  14. {
  15. Stream = stream;
  16. }
  17. public void ReleaseStream()
  18. {
  19. Stream = null;
  20. }
  21. public void Init()
  22. {
  23. StartPosition = Stream.Position;
  24. Low = 0;
  25. Range = 0xFFFFFFFF;
  26. _cacheSize = 1;
  27. _cache = 0;
  28. }
  29. public void FlushData()
  30. {
  31. for (int i = 0; i < 5; i++)
  32. ShiftLow();
  33. }
  34. public void FlushStream()
  35. {
  36. Stream.Flush();
  37. }
  38. public void CloseStream()
  39. {
  40. Stream.Close();
  41. }
  42. public void Encode(uint start, uint size, uint total)
  43. {
  44. Low += start * (Range /= total);
  45. Range *= size;
  46. while (Range < kTopValue)
  47. {
  48. Range <<= 8;
  49. ShiftLow();
  50. }
  51. }
  52. public void ShiftLow()
  53. {
  54. if ((uint)Low < (uint)0xFF000000 || (uint)(Low >> 32) == 1)
  55. {
  56. byte temp = _cache;
  57. do
  58. {
  59. Stream.WriteByte((byte)(temp + (Low >> 32)));
  60. temp = 0xFF;
  61. }
  62. while (--_cacheSize != 0);
  63. _cache = (byte)(((uint)Low) >> 24);
  64. }
  65. _cacheSize++;
  66. Low = ((uint)Low) << 8;
  67. }
  68. public void EncodeDirectBits(uint v, int numTotalBits)
  69. {
  70. for (int i = numTotalBits - 1; i >= 0; i--)
  71. {
  72. Range >>= 1;
  73. if (((v >> i) & 1) == 1)
  74. Low += Range;
  75. if (Range < kTopValue)
  76. {
  77. Range <<= 8;
  78. ShiftLow();
  79. }
  80. }
  81. }
  82. public void EncodeBit(uint size0, int numTotalBits, uint symbol)
  83. {
  84. uint newBound = (Range >> numTotalBits) * size0;
  85. if (symbol == 0)
  86. Range = newBound;
  87. else
  88. {
  89. Low += newBound;
  90. Range -= newBound;
  91. }
  92. while (Range < kTopValue)
  93. {
  94. Range <<= 8;
  95. ShiftLow();
  96. }
  97. }
  98. public long GetProcessedSizeAdd()
  99. {
  100. return _cacheSize +
  101. Stream.Position - StartPosition + 4;
  102. // (long)Stream.GetProcessedSize();
  103. }
  104. }
  105. class Decoder
  106. {
  107. public const uint kTopValue = (1 << 24);
  108. public uint Range;
  109. public uint Code;
  110. // public Buffer.InBuffer Stream = new Buffer.InBuffer(1 << 16);
  111. public System.IO.Stream Stream;
  112. public void Init(System.IO.Stream stream)
  113. {
  114. // Stream.Init(stream);
  115. Stream = stream;
  116. Code = 0;
  117. Range = 0xFFFFFFFF;
  118. for (int i = 0; i < 5; i++)
  119. Code = (Code << 8) | (byte)Stream.ReadByte();
  120. }
  121. public void ReleaseStream()
  122. {
  123. // Stream.ReleaseStream();
  124. Stream = null;
  125. }
  126. public void CloseStream()
  127. {
  128. Stream.Close();
  129. }
  130. public void Normalize()
  131. {
  132. while (Range < kTopValue)
  133. {
  134. Code = (Code << 8) | (byte)Stream.ReadByte();
  135. Range <<= 8;
  136. }
  137. }
  138. public void Normalize2()
  139. {
  140. if (Range < kTopValue)
  141. {
  142. Code = (Code << 8) | (byte)Stream.ReadByte();
  143. Range <<= 8;
  144. }
  145. }
  146. public uint GetThreshold(uint total)
  147. {
  148. return Code / (Range /= total);
  149. }
  150. public void Decode(uint start, uint size, uint total)
  151. {
  152. Code -= start * Range;
  153. Range *= size;
  154. Normalize();
  155. }
  156. public uint DecodeDirectBits(int numTotalBits)
  157. {
  158. uint range = Range;
  159. uint code = Code;
  160. uint result = 0;
  161. for (int i = numTotalBits; i > 0; i--)
  162. {
  163. range >>= 1;
  164. /*
  165. result <<= 1;
  166. if (code >= range)
  167. {
  168. code -= range;
  169. result |= 1;
  170. }
  171. */
  172. uint t = (code - range) >> 31;
  173. code -= range & (t - 1);
  174. result = (result << 1) | (1 - t);
  175. if (range < kTopValue)
  176. {
  177. code = (code << 8) | (byte)Stream.ReadByte();
  178. range <<= 8;
  179. }
  180. }
  181. Range = range;
  182. Code = code;
  183. return result;
  184. }
  185. public uint DecodeBit(uint size0, int numTotalBits)
  186. {
  187. uint newBound = (Range >> numTotalBits) * size0;
  188. uint symbol;
  189. if (Code < newBound)
  190. {
  191. symbol = 0;
  192. Range = newBound;
  193. }
  194. else
  195. {
  196. symbol = 1;
  197. Code -= newBound;
  198. Range -= newBound;
  199. }
  200. Normalize();
  201. return symbol;
  202. }
  203. // ulong GetProcessedSize() {return Stream.GetProcessedSize(); }
  204. }
  205. }