RangeCoderBitTree.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. namespace SevenZip.Compression.RangeCoder
  3. {
  4. struct BitTreeEncoder
  5. {
  6. BitEncoder[] Models;
  7. int NumBitLevels;
  8. public BitTreeEncoder(int numBitLevels)
  9. {
  10. NumBitLevels = numBitLevels;
  11. Models = new BitEncoder[1 << numBitLevels];
  12. }
  13. public void Init()
  14. {
  15. for (uint i = 1; i < (1 << NumBitLevels); i++)
  16. Models[i].Init();
  17. }
  18. public void Encode(Encoder rangeEncoder, UInt32 symbol)
  19. {
  20. UInt32 m = 1;
  21. for (int bitIndex = NumBitLevels; bitIndex > 0; )
  22. {
  23. bitIndex--;
  24. UInt32 bit = (symbol >> bitIndex) & 1;
  25. Models[m].Encode(rangeEncoder, bit);
  26. m = (m << 1) | bit;
  27. }
  28. }
  29. public void ReverseEncode(Encoder rangeEncoder, UInt32 symbol)
  30. {
  31. UInt32 m = 1;
  32. for (UInt32 i = 0; i < NumBitLevels; i++)
  33. {
  34. UInt32 bit = symbol & 1;
  35. Models[m].Encode(rangeEncoder, bit);
  36. m = (m << 1) | bit;
  37. symbol >>= 1;
  38. }
  39. }
  40. public UInt32 GetPrice(UInt32 symbol)
  41. {
  42. UInt32 price = 0;
  43. UInt32 m = 1;
  44. for (int bitIndex = NumBitLevels; bitIndex > 0; )
  45. {
  46. bitIndex--;
  47. UInt32 bit = (symbol >> bitIndex) & 1;
  48. price += Models[m].GetPrice(bit);
  49. m = (m << 1) + bit;
  50. }
  51. return price;
  52. }
  53. public UInt32 ReverseGetPrice(UInt32 symbol)
  54. {
  55. UInt32 price = 0;
  56. UInt32 m = 1;
  57. for (int i = NumBitLevels; i > 0; i--)
  58. {
  59. UInt32 bit = symbol & 1;
  60. symbol >>= 1;
  61. price += Models[m].GetPrice(bit);
  62. m = (m << 1) | bit;
  63. }
  64. return price;
  65. }
  66. public static UInt32 ReverseGetPrice(BitEncoder[] Models, UInt32 startIndex,
  67. int NumBitLevels, UInt32 symbol)
  68. {
  69. UInt32 price = 0;
  70. UInt32 m = 1;
  71. for (int i = NumBitLevels; i > 0; i--)
  72. {
  73. UInt32 bit = symbol & 1;
  74. symbol >>= 1;
  75. price += Models[startIndex + m].GetPrice(bit);
  76. m = (m << 1) | bit;
  77. }
  78. return price;
  79. }
  80. public static void ReverseEncode(BitEncoder[] Models, UInt32 startIndex,
  81. Encoder rangeEncoder, int NumBitLevels, UInt32 symbol)
  82. {
  83. UInt32 m = 1;
  84. for (int i = 0; i < NumBitLevels; i++)
  85. {
  86. UInt32 bit = symbol & 1;
  87. Models[startIndex + m].Encode(rangeEncoder, bit);
  88. m = (m << 1) | bit;
  89. symbol >>= 1;
  90. }
  91. }
  92. }
  93. struct BitTreeDecoder
  94. {
  95. BitDecoder[] Models;
  96. int NumBitLevels;
  97. public BitTreeDecoder(int numBitLevels)
  98. {
  99. NumBitLevels = numBitLevels;
  100. Models = new BitDecoder[1 << numBitLevels];
  101. }
  102. public void Init()
  103. {
  104. for (uint i = 1; i < (1 << NumBitLevels); i++)
  105. Models[i].Init();
  106. }
  107. public uint Decode(RangeCoder.Decoder rangeDecoder)
  108. {
  109. uint m = 1;
  110. for (int bitIndex = NumBitLevels; bitIndex > 0; bitIndex--)
  111. m = (m << 1) + Models[m].Decode(rangeDecoder);
  112. return m - ((uint)1 << NumBitLevels);
  113. }
  114. public uint ReverseDecode(RangeCoder.Decoder rangeDecoder)
  115. {
  116. uint m = 1;
  117. uint symbol = 0;
  118. for (int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
  119. {
  120. uint bit = Models[m].Decode(rangeDecoder);
  121. m <<= 1;
  122. m += bit;
  123. symbol |= (bit << bitIndex);
  124. }
  125. return symbol;
  126. }
  127. public static uint ReverseDecode(BitDecoder[] Models, UInt32 startIndex,
  128. RangeCoder.Decoder rangeDecoder, int NumBitLevels)
  129. {
  130. uint m = 1;
  131. uint symbol = 0;
  132. for (int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
  133. {
  134. uint bit = Models[startIndex + m].Decode(rangeDecoder);
  135. m <<= 1;
  136. m += bit;
  137. symbol |= (bit << bitIndex);
  138. }
  139. return symbol;
  140. }
  141. }
  142. }