RangeCoderBit.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* This file is part of SevenZipSharp.
  2. SevenZipSharp is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU Lesser General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. SevenZipSharp is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with SevenZipSharp. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. using System;
  14. namespace SevenZip.Sdk.Compression.RangeCoder
  15. {
  16. internal struct BitEncoder
  17. {
  18. public const uint kBitModelTotal = (1 << kNumBitModelTotalBits);
  19. public const int kNumBitModelTotalBits = 11;
  20. public const int kNumBitPriceShiftBits = 6;
  21. private const int kNumMoveBits = 5;
  22. private const int kNumMoveReducingBits = 2;
  23. private static readonly UInt32[] ProbPrices = new UInt32[kBitModelTotal >> kNumMoveReducingBits];
  24. private uint Prob;
  25. static BitEncoder()
  26. {
  27. const int kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits);
  28. for (int i = kNumBits - 1; i >= 0; i--)
  29. {
  30. UInt32 start = (UInt32) 1 << (kNumBits - i - 1);
  31. UInt32 end = (UInt32) 1 << (kNumBits - i);
  32. for (UInt32 j = start; j < end; j++)
  33. ProbPrices[j] = ((UInt32) i << kNumBitPriceShiftBits) +
  34. (((end - j) << kNumBitPriceShiftBits) >> (kNumBits - i - 1));
  35. }
  36. }
  37. public void Init()
  38. {
  39. Prob = kBitModelTotal >> 1;
  40. }
  41. /*public void UpdateModel(uint symbol)
  42. {
  43. if (symbol == 0)
  44. Prob += (kBitModelTotal - Prob) >> kNumMoveBits;
  45. else
  46. Prob -= (Prob) >> kNumMoveBits;
  47. }*/
  48. public void Encode(Encoder encoder, uint symbol)
  49. {
  50. // encoder.EncodeBit(Prob, kNumBitModelTotalBits, symbol);
  51. // UpdateModel(symbol);
  52. uint newBound = (encoder.Range >> kNumBitModelTotalBits)*Prob;
  53. if (symbol == 0)
  54. {
  55. encoder.Range = newBound;
  56. Prob += (kBitModelTotal - Prob) >> kNumMoveBits;
  57. }
  58. else
  59. {
  60. encoder.Low += newBound;
  61. encoder.Range -= newBound;
  62. Prob -= (Prob) >> kNumMoveBits;
  63. }
  64. if (encoder.Range < Encoder.kTopValue)
  65. {
  66. encoder.Range <<= 8;
  67. encoder.ShiftLow();
  68. }
  69. }
  70. public uint GetPrice(uint symbol)
  71. {
  72. return ProbPrices[(((Prob - symbol) ^ ((-(int) symbol))) & (kBitModelTotal - 1)) >> kNumMoveReducingBits];
  73. }
  74. public uint GetPrice0()
  75. {
  76. return ProbPrices[Prob >> kNumMoveReducingBits];
  77. }
  78. public uint GetPrice1()
  79. {
  80. return ProbPrices[(kBitModelTotal - Prob) >> kNumMoveReducingBits];
  81. }
  82. }
  83. internal struct BitDecoder
  84. {
  85. public const uint kBitModelTotal = (1 << kNumBitModelTotalBits);
  86. public const int kNumBitModelTotalBits = 11;
  87. private const int kNumMoveBits = 5;
  88. private uint Prob;
  89. /*public void UpdateModel(int numMoveBits, uint symbol)
  90. {
  91. if (symbol == 0)
  92. Prob += (kBitModelTotal - Prob) >> numMoveBits;
  93. else
  94. Prob -= (Prob) >> numMoveBits;
  95. }*/
  96. public void Init()
  97. {
  98. Prob = kBitModelTotal >> 1;
  99. }
  100. public uint Decode(Decoder rangeDecoder)
  101. {
  102. uint newBound = (rangeDecoder.Range >> kNumBitModelTotalBits)*Prob;
  103. if (rangeDecoder.Code < newBound)
  104. {
  105. rangeDecoder.Range = newBound;
  106. Prob += (kBitModelTotal - Prob) >> kNumMoveBits;
  107. if (rangeDecoder.Range < Decoder.kTopValue)
  108. {
  109. rangeDecoder.Code = (rangeDecoder.Code << 8) | (byte) rangeDecoder.Stream.ReadByte();
  110. rangeDecoder.Range <<= 8;
  111. }
  112. return 0;
  113. }
  114. else
  115. {
  116. rangeDecoder.Range -= newBound;
  117. rangeDecoder.Code -= newBound;
  118. Prob -= (Prob) >> kNumMoveBits;
  119. if (rangeDecoder.Range < Decoder.kTopValue)
  120. {
  121. rangeDecoder.Code = (rangeDecoder.Code << 8) | (byte) rangeDecoder.Stream.ReadByte();
  122. rangeDecoder.Range <<= 8;
  123. }
  124. return 1;
  125. }
  126. }
  127. }
  128. }