RangeCoderBit.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. namespace SevenZip.Compression.RangeCoder
  3. {
  4. struct BitEncoder
  5. {
  6. public const int kNumBitModelTotalBits = 11;
  7. public const uint kBitModelTotal = (1 << kNumBitModelTotalBits);
  8. const int kNumMoveBits = 5;
  9. const int kNumMoveReducingBits = 2;
  10. public const int kNumBitPriceShiftBits = 6;
  11. uint Prob;
  12. public void Init() { Prob = kBitModelTotal >> 1; }
  13. public void UpdateModel(uint symbol)
  14. {
  15. if (symbol == 0)
  16. Prob += (kBitModelTotal - Prob) >> kNumMoveBits;
  17. else
  18. Prob -= (Prob) >> kNumMoveBits;
  19. }
  20. public void Encode(Encoder encoder, uint symbol)
  21. {
  22. // encoder.EncodeBit(Prob, kNumBitModelTotalBits, symbol);
  23. // UpdateModel(symbol);
  24. uint newBound = (encoder.Range >> kNumBitModelTotalBits) * Prob;
  25. if (symbol == 0)
  26. {
  27. encoder.Range = newBound;
  28. Prob += (kBitModelTotal - Prob) >> kNumMoveBits;
  29. }
  30. else
  31. {
  32. encoder.Low += newBound;
  33. encoder.Range -= newBound;
  34. Prob -= (Prob) >> kNumMoveBits;
  35. }
  36. if (encoder.Range < Encoder.kTopValue)
  37. {
  38. encoder.Range <<= 8;
  39. encoder.ShiftLow();
  40. }
  41. }
  42. private static UInt32[] ProbPrices = new UInt32[kBitModelTotal >> kNumMoveReducingBits];
  43. static BitEncoder()
  44. {
  45. const int kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits);
  46. for (int i = kNumBits - 1; i >= 0; i--)
  47. {
  48. UInt32 start = (UInt32)1 << (kNumBits - i - 1);
  49. UInt32 end = (UInt32)1 << (kNumBits - i);
  50. for (UInt32 j = start; j < end; j++)
  51. ProbPrices[j] = ((UInt32)i << kNumBitPriceShiftBits) +
  52. (((end - j) << kNumBitPriceShiftBits) >> (kNumBits - i - 1));
  53. }
  54. }
  55. public uint GetPrice(uint symbol)
  56. {
  57. return ProbPrices[(((Prob - symbol) ^ ((-(int)symbol))) & (kBitModelTotal - 1)) >> kNumMoveReducingBits];
  58. }
  59. public uint GetPrice0() { return ProbPrices[Prob >> kNumMoveReducingBits]; }
  60. public uint GetPrice1() { return ProbPrices[(kBitModelTotal - Prob) >> kNumMoveReducingBits]; }
  61. }
  62. struct BitDecoder
  63. {
  64. public const int kNumBitModelTotalBits = 11;
  65. public const uint kBitModelTotal = (1 << kNumBitModelTotalBits);
  66. const int kNumMoveBits = 5;
  67. uint Prob;
  68. public void UpdateModel(int numMoveBits, uint symbol)
  69. {
  70. if (symbol == 0)
  71. Prob += (kBitModelTotal - Prob) >> numMoveBits;
  72. else
  73. Prob -= (Prob) >> numMoveBits;
  74. }
  75. public void Init() { Prob = kBitModelTotal >> 1; }
  76. public uint Decode(RangeCoder.Decoder rangeDecoder)
  77. {
  78. uint newBound = (uint)(rangeDecoder.Range >> kNumBitModelTotalBits) * (uint)Prob;
  79. if (rangeDecoder.Code < newBound)
  80. {
  81. rangeDecoder.Range = newBound;
  82. Prob += (kBitModelTotal - Prob) >> kNumMoveBits;
  83. if (rangeDecoder.Range < Decoder.kTopValue)
  84. {
  85. rangeDecoder.Code = (rangeDecoder.Code << 8) | (byte)rangeDecoder.Stream.ReadByte();
  86. rangeDecoder.Range <<= 8;
  87. }
  88. return 0;
  89. }
  90. else
  91. {
  92. rangeDecoder.Range -= newBound;
  93. rangeDecoder.Code -= newBound;
  94. Prob -= (Prob) >> kNumMoveBits;
  95. if (rangeDecoder.Range < Decoder.kTopValue)
  96. {
  97. rangeDecoder.Code = (rangeDecoder.Code << 8) | (byte)rangeDecoder.Stream.ReadByte();
  98. rangeDecoder.Range <<= 8;
  99. }
  100. return 1;
  101. }
  102. }
  103. }
  104. }