RangeCoder.cs 6.0 KB

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