Class: Omnizip::Algorithms::LZMA::BitModel
- Inherits:
-
Object
- Object
- Omnizip::Algorithms::LZMA::BitModel
- Defined in:
- lib/omnizip/algorithms/lzma/bit_model.rb
Overview
Adaptive probability model for range coding
This class manages probability states for individual bits in the range coder. It uses adaptive arithmetic coding where probabilities are updated based on actual bit values encountered during encoding or decoding.
Ported from XZ Utils range_encoder.c probability model implementation.
Constant Summary collapse
- PROB_INIT =
Initial probability (0.5)
1024- MOVE_BITS =
Probability update speed
5- MAX_PROB =
4096
1 << 11
- BIT_MODEL_TOTAL =
XZ Utils RC_BIT_MODEL_TOTAL = 2048
0x800
Instance Attribute Summary collapse
-
#probability ⇒ Object
Returns the value of attribute probability.
Instance Method Summary collapse
-
#dup ⇒ BitModel
Create a copy of this bit model.
-
#initialize(initial_prob = PROB_INIT) ⇒ BitModel
constructor
Initialize a new bit probability model.
-
#prob_0 ⇒ Integer
Get the probability of encoding a 0 bit.
-
#prob_1 ⇒ Integer
Get the probability of encoding a 1 bit.
-
#reset ⇒ void
Reset the probability model to initial state.
-
#to_range ⇒ Integer
For range coder: get probability scaled to 11 bits (XZ Utils compatibility).
-
#update(bit) ⇒ void
Update the probability model based on an actual bit value.
-
#update!(bit) ⇒ Object
deprecated
Deprecated.
Use #update instead (same functionality, XZ Utils compatible)
Constructor Details
Instance Attribute Details
#probability ⇒ Object
Returns the value of attribute probability.
40 41 42 |
# File 'lib/omnizip/algorithms/lzma/bit_model.rb', line 40 def probability @probability end |
Instance Method Details
#dup ⇒ BitModel
Create a copy of this bit model
104 105 106 |
# File 'lib/omnizip/algorithms/lzma/bit_model.rb', line 104 def dup BitModel.new(@probability) end |
#prob_0 ⇒ Integer
Get the probability of encoding a 0 bit
90 91 92 |
# File 'lib/omnizip/algorithms/lzma/bit_model.rb', line 90 def prob_0 @probability end |
#prob_1 ⇒ Integer
Get the probability of encoding a 1 bit
97 98 99 |
# File 'lib/omnizip/algorithms/lzma/bit_model.rb', line 97 def prob_1 MAX_PROB - @probability end |
#reset ⇒ void
This method returns an undefined value.
Reset the probability model to initial state
83 84 85 |
# File 'lib/omnizip/algorithms/lzma/bit_model.rb', line 83 def reset @probability = PROB_INIT end |
#to_range ⇒ Integer
For range coder: get probability scaled to 11 bits (XZ Utils compatibility)
This method returns the probability value in the format expected by the range coder for encoding/decoding operations.
114 115 116 |
# File 'lib/omnizip/algorithms/lzma/bit_model.rb', line 114 def to_range @probability end |
#update(bit) ⇒ void
This method returns an undefined value.
Update the probability model based on an actual bit value
This method implements the XZ Utils adaptive algorithm:
- If bit is 0: probability increases (shifts toward encoding 0)
- If bit is 1: probability decreases (shifts toward encoding 1)
The update uses a shift operation (MOVE_BITS) to control the adaptation rate. Smaller MOVE_BITS means faster adaptation.
XZ Utils formula (lzma/lzma_encoder.c:RC_BIT_*):
bit 0: prob += (RC_BIT_MODEL_TOTAL - prob) >> RC_MOVE_BITS
bit 1: prob -= prob >> RC_MOVE_BITS
where RC_BIT_MODEL_TOTAL = 2048, RC_MOVE_BITS = 5
65 66 67 68 69 70 71 72 73 |
# File 'lib/omnizip/algorithms/lzma/bit_model.rb', line 65 def update(bit) if bit.zero? # XZ Utils formula: prob += (RC_BIT_MODEL_TOTAL - prob) >> RC_MOVE_BITS @probability += ((BIT_MODEL_TOTAL - @probability) >> MOVE_BITS) else # XZ Utils formula: prob -= prob >> RC_MOVE_BITS @probability -= (@probability >> MOVE_BITS) end end |
#update!(bit) ⇒ Object
Use #update instead (same functionality, XZ Utils compatible)
76 77 78 |
# File 'lib/omnizip/algorithms/lzma/bit_model.rb', line 76 def update!(bit) update(bit) end |