price.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file price.h
  4. /// \brief Probability price calculation
  5. //
  6. // Author: Igor Pavlov
  7. //
  8. // This file has been put into the public domain.
  9. // You can do whatever you want with this file.
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #ifndef LZMA_PRICE_H
  13. #define LZMA_PRICE_H
  14. #define RC_MOVE_REDUCING_BITS 4
  15. #define RC_BIT_PRICE_SHIFT_BITS 4
  16. #define RC_PRICE_TABLE_SIZE (RC_BIT_MODEL_TOTAL >> RC_MOVE_REDUCING_BITS)
  17. #define RC_INFINITY_PRICE (UINT32_C(1) << 30)
  18. /// Lookup table for the inline functions defined in this file.
  19. extern const uint8_t lzma_rc_prices[RC_PRICE_TABLE_SIZE];
  20. static inline uint32_t
  21. rc_bit_price(const probability prob, const uint32_t bit)
  22. {
  23. return lzma_rc_prices[(prob ^ ((UINT32_C(0) - bit)
  24. & (RC_BIT_MODEL_TOTAL - 1))) >> RC_MOVE_REDUCING_BITS];
  25. }
  26. static inline uint32_t
  27. rc_bit_0_price(const probability prob)
  28. {
  29. return lzma_rc_prices[prob >> RC_MOVE_REDUCING_BITS];
  30. }
  31. static inline uint32_t
  32. rc_bit_1_price(const probability prob)
  33. {
  34. return lzma_rc_prices[(prob ^ (RC_BIT_MODEL_TOTAL - 1))
  35. >> RC_MOVE_REDUCING_BITS];
  36. }
  37. static inline uint32_t
  38. rc_bittree_price(const probability *const probs,
  39. const uint32_t bit_levels, uint32_t symbol)
  40. {
  41. uint32_t price = 0;
  42. symbol += UINT32_C(1) << bit_levels;
  43. do {
  44. const uint32_t bit = symbol & 1;
  45. symbol >>= 1;
  46. price += rc_bit_price(probs[symbol], bit);
  47. } while (symbol != 1);
  48. return price;
  49. }
  50. static inline uint32_t
  51. rc_bittree_reverse_price(const probability *const probs,
  52. uint32_t bit_levels, uint32_t symbol)
  53. {
  54. uint32_t price = 0;
  55. uint32_t model_index = 1;
  56. do {
  57. const uint32_t bit = symbol & 1;
  58. symbol >>= 1;
  59. price += rc_bit_price(probs[model_index], bit);
  60. model_index = (model_index << 1) + bit;
  61. } while (--bit_levels != 0);
  62. return price;
  63. }
  64. static inline uint32_t
  65. rc_direct_price(const uint32_t bits)
  66. {
  67. return bits << RC_BIT_PRICE_SHIFT_BITS;
  68. }
  69. #endif