RangeCoderBit.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Compress/RangeCoder/RangeCoderBit.cpp
  2. #include "StdAfx.h"
  3. #include "RangeCoderBit.h"
  4. namespace NCompress {
  5. namespace NRangeCoder {
  6. UInt32 CPriceTables::ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
  7. static CPriceTables g_PriceTables;
  8. CPriceTables::CPriceTables() { Init(); }
  9. void CPriceTables::Init()
  10. {
  11. const int kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits);
  12. for(int i = kNumBits - 1; i >= 0; i--)
  13. {
  14. UInt32 start = 1 << (kNumBits - i - 1);
  15. UInt32 end = 1 << (kNumBits - i);
  16. for (UInt32 j = start; j < end; j++)
  17. ProbPrices[j] = (i << kNumBitPriceShiftBits) +
  18. (((end - j) << kNumBitPriceShiftBits) >> (kNumBits - i - 1));
  19. }
  20. /*
  21. // simplest: bad solution
  22. for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++)
  23. ProbPrices[i] = kBitPrice;
  24. */
  25. /*
  26. const double kDummyMultMid = (1.0 / kBitPrice) / 2;
  27. const double kDummyMultMid = 0;
  28. // float solution
  29. double ln2 = log(double(2));
  30. double lnAll = log(double(kBitModelTotal >> kNumMoveReducingBits));
  31. for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++)
  32. ProbPrices[i] = UInt32((fabs(lnAll - log(double(i))) / ln2 + kDummyMultMid) * kBitPrice);
  33. */
  34. /*
  35. // experimental, slow, solution:
  36. for(UInt32 i = 1; i < (kBitModelTotal >> kNumMoveReducingBits) - 1; i++)
  37. {
  38. const int kCyclesBits = 5;
  39. const UInt32 kCycles = (1 << kCyclesBits);
  40. UInt32 range = UInt32(-1);
  41. UInt32 bitCount = 0;
  42. for (UInt32 j = 0; j < kCycles; j++)
  43. {
  44. range >>= (kNumBitModelTotalBits - kNumMoveReducingBits);
  45. range *= i;
  46. while(range < (1 << 31))
  47. {
  48. range <<= 1;
  49. bitCount++;
  50. }
  51. }
  52. bitCount <<= kNumBitPriceShiftBits;
  53. range -= (1 << 31);
  54. for (int k = kNumBitPriceShiftBits - 1; k >= 0; k--)
  55. {
  56. range <<= 1;
  57. if (range > (1 << 31))
  58. {
  59. bitCount += (1 << k);
  60. range -= (1 << 31);
  61. }
  62. }
  63. ProbPrices[i] = (bitCount
  64. // + (1 << (kCyclesBits - 1))
  65. ) >> kCyclesBits;
  66. }
  67. */
  68. }
  69. }}