Class: Omnizip::Algorithms::LZMA::BitModel

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(initial_prob = PROB_INIT) ⇒ BitModel

Initialize a new bit probability model

Parameters:

  • initial_prob (Integer) (defaults to: PROB_INIT)

    Initial probability value (default: PROB_INIT)



45
46
47
# File 'lib/omnizip/algorithms/lzma/bit_model.rb', line 45

def initialize(initial_prob = PROB_INIT)
  @probability = initial_prob
end

Instance Attribute Details

#probabilityObject

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

#dupBitModel

Create a copy of this bit model

Returns:

  • (BitModel)

    A new BitModel with the same probability



104
105
106
# File 'lib/omnizip/algorithms/lzma/bit_model.rb', line 104

def dup
  BitModel.new(@probability)
end

#prob_0Integer

Get the probability of encoding a 0 bit

Returns:

  • (Integer)

    Probability value (0..MAX_PROB)



90
91
92
# File 'lib/omnizip/algorithms/lzma/bit_model.rb', line 90

def prob_0
  @probability
end

#prob_1Integer

Get the probability of encoding a 1 bit

Returns:

  • (Integer)

    Probability value (0..MAX_PROB)



97
98
99
# File 'lib/omnizip/algorithms/lzma/bit_model.rb', line 97

def prob_1
  MAX_PROB - @probability
end

#resetvoid

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_rangeInteger

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.

Returns:

  • (Integer)

    Probability value (0..MAX_PROB)



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

Parameters:

  • bit (Integer)

    The actual bit value (0 or 1)



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

Deprecated.

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