Skip to content

Commit 17e3d53

Browse files
committed
Renamed field ArithmeticCoderBase.STATE_SIZE to numStateBits/num_state_bits (including mentions in comments), in all languages.
1 parent 1dfa7a1 commit 17e3d53

File tree

6 files changed

+46
-46
lines changed

6 files changed

+46
-46
lines changed

cpp/ArithmeticCoder.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ using std::uint64_t;
1717
ArithmeticCoderBase::ArithmeticCoderBase(int stateSize) {
1818
if (stateSize < 1 || stateSize > 63)
1919
throw std::domain_error("State size out of range");
20-
STATE_SIZE = stateSize;
21-
MAX_RANGE = static_cast<decltype(MAX_RANGE)>(1) << STATE_SIZE;
20+
numStateBits = stateSize;
21+
MAX_RANGE = static_cast<decltype(MAX_RANGE)>(1) << numStateBits;
2222
TOP_MASK = MAX_RANGE >> 1;
2323
SECOND_MASK = TOP_MASK >> 1;
2424
MIN_RANGE = (MAX_RANGE >> 2) + 2;
@@ -75,7 +75,7 @@ ArithmeticDecoder::ArithmeticDecoder(int stateSize, BitInputStream &in) :
7575
ArithmeticCoderBase(stateSize),
7676
input(in),
7777
code(0) {
78-
for (int i = 0; i < STATE_SIZE; i++)
78+
for (int i = 0; i < numStateBits; i++)
7979
code = code << 1 | readCodeBit();
8080
}
8181

@@ -151,7 +151,7 @@ void ArithmeticEncoder::finish() {
151151

152152

153153
void ArithmeticEncoder::shift() {
154-
int bit = static_cast<int>(low >> (STATE_SIZE - 1));
154+
int bit = static_cast<int>(low >> (numStateBits - 1));
155155
output.write(bit);
156156

157157
// Write out the saved underflow bits

cpp/ArithmeticCoder.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ class ArithmeticCoderBase {
2929
// - But for state sizes greater than the midpoint, because intermediate computations are limited
3030
// to the long integer type's 63-bit unsigned precision, larger state sizes will decrease the
3131
// maximum frequency total, which might constrain the user-supplied probability model.
32-
// - Therefore STATE_SIZE=32 is recommended as the most versatile setting
32+
// - Therefore numStateBits=32 is recommended as the most versatile setting
3333
// because it maximizes MAX_TOTAL (which ends up being slightly over 2^30).
34-
// - Note that STATE_SIZE=63 is legal but useless because it implies MAX_TOTAL=1,
34+
// - Note that numStateBits=63 is legal but useless because it implies MAX_TOTAL=1,
3535
// which means a frequency table can only support one symbol with non-zero frequency.
36-
protected: int STATE_SIZE;
36+
protected: int numStateBits;
3737

38-
// Maximum range (high+1-low) during coding (trivial), which is 2^STATE_SIZE = 1000...000.
38+
// Maximum range (high+1-low) during coding (trivial), which is 2^numStateBits = 1000...000.
3939
protected: std::uint64_t MAX_RANGE;
4040

41-
// The top bit at width STATE_SIZE, which is 0100...000.
41+
// The top bit at width numStateBits, which is 0100...000.
4242
protected: std::uint64_t TOP_MASK;
4343

44-
// The second highest bit at width STATE_SIZE, which is 0010...000. This is zero when STATE_SIZE=1.
44+
// The second highest bit at width numStateBits, which is 0010...000. This is zero when numStateBits=1.
4545
protected: std::uint64_t SECOND_MASK;
4646

4747
// Minimum range (high+1-low) during coding (non-trivial), which is 0010...010.
@@ -50,7 +50,7 @@ class ArithmeticCoderBase {
5050
// Maximum allowed total from a frequency table at all times during coding.
5151
protected: std::uint64_t MAX_TOTAL;
5252

53-
// Bit mask of STATE_SIZE ones, which is 0111...111.
53+
// Bit mask of numStateBits ones, which is 0111...111.
5454
protected: std::uint64_t MASK;
5555

5656

@@ -77,14 +77,14 @@ class ArithmeticCoderBase {
7777
// Updates the code range (low and high) of this arithmetic coder as a result
7878
// of processing the given symbol with the given frequency table.
7979
// Invariants that are true before and after encoding/decoding each symbol:
80-
// * 0 <= low <= code <= high < 2^STATE_SIZE. ('code' exists only in the decoder.)
81-
// Therefore these variables are unsigned integers of STATE_SIZE bits.
82-
// * (low < 1/2 * 2^STATE_SIZE) && (high >= 1/2 * 2^STATE_SIZE).
80+
// * 0 <= low <= code <= high < 2^numStateBits. ('code' exists only in the decoder.)
81+
// Therefore these variables are unsigned integers of numStateBits bits.
82+
// * (low < 1/2 * 2^numStateBits) && (high >= 1/2 * 2^numStateBits).
8383
// In other words, they are in different halves of the full range.
84-
// * (low < 1/4 * 2^STATE_SIZE) || (high >= 3/4 * 2^STATE_SIZE).
84+
// * (low < 1/4 * 2^numStateBits) || (high >= 3/4 * 2^numStateBits).
8585
// In other words, they are not both in the middle two quarters.
8686
// * Let range = high - low + 1, then MAX_RANGE/4 < MIN_RANGE <= range
87-
// <= MAX_RANGE = 2^STATE_SIZE. These invariants for 'range' essentially dictate the maximum
87+
// <= MAX_RANGE = 2^numStateBits. These invariants for 'range' essentially dictate the maximum
8888
// total that the incoming frequency table can have, such that intermediate calculations don't overflow.
8989
protected: virtual void update(const FrequencyTable &freqs, std::uint32_t symbol);
9090

java/src/ArithmeticCoderBase.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ public abstract class ArithmeticCoderBase {
2828
* <li>But for state sizes greater than the midpoint, because intermediate computations are limited
2929
* to the long integer type's 63-bit unsigned precision, larger state sizes will decrease the
3030
* maximum frequency total, which might constrain the user-supplied probability model.</li>
31-
* <li>Therefore STATE_SIZE=32 is recommended as the most versatile setting
31+
* <li>Therefore numStateBits=32 is recommended as the most versatile setting
3232
* because it maximizes MAX_TOTAL (which ends up being slightly over 2^30).</li>
33-
* <li>Note that STATE_SIZE=62 is legal but useless because it implies MAX_TOTAL=1,
33+
* <li>Note that numStateBits=62 is legal but useless because it implies MAX_TOTAL=1,
3434
* which means a frequency table can only support one symbol with non-zero frequency.</li>
3535
* </ul>
3636
*/
37-
protected final int STATE_SIZE;
37+
protected final int numStateBits;
3838

39-
/** Maximum range (high+1-low) during coding (trivial), which is 2^STATE_SIZE = 1000...000. */
39+
/** Maximum range (high+1-low) during coding (trivial), which is 2^numStateBits = 1000...000. */
4040
protected final long MAX_RANGE;
4141

42-
/** The top bit at width STATE_SIZE, which is 0100...000. */
42+
/** The top bit at width numStateBits, which is 0100...000. */
4343
protected final long TOP_MASK;
4444

45-
/** The second highest bit at width STATE_SIZE, which is 0010...000. This is zero when STATE_SIZE=1. */
45+
/** The second highest bit at width numStateBits, which is 0010...000. This is zero when numStateBits=1. */
4646
protected final long SECOND_MASK;
4747

4848
/** Minimum range (high+1-low) during coding (non-trivial), which is 0010...010. */
@@ -51,7 +51,7 @@ public abstract class ArithmeticCoderBase {
5151
/** Maximum allowed total from a frequency table at all times during coding. */
5252
protected final long MAX_TOTAL;
5353

54-
/** Bit mask of STATE_SIZE ones, which is 0111...111. */
54+
/** Bit mask of numStateBits ones, which is 0111...111. */
5555
protected final long MASK;
5656

5757

@@ -80,8 +80,8 @@ public abstract class ArithmeticCoderBase {
8080
public ArithmeticCoderBase(int stateSize) {
8181
if (stateSize < 1 || stateSize > 62)
8282
throw new IllegalArgumentException("State size out of range");
83-
STATE_SIZE = stateSize;
84-
MAX_RANGE = 1L << STATE_SIZE;
83+
numStateBits = stateSize;
84+
MAX_RANGE = 1L << numStateBits;
8585
TOP_MASK = MAX_RANGE >>> 1;
8686
SECOND_MASK = TOP_MASK >>> 1;
8787
MIN_RANGE = (MAX_RANGE >>> 2) + 2;
@@ -101,14 +101,14 @@ public ArithmeticCoderBase(int stateSize) {
101101
* of processing the specified symbol with the specified frequency table.
102102
* <p>Invariants that are true before and after encoding/decoding each symbol:</p>
103103
* <ul>
104-
* <li>0 &le; low &le; code &le; high &lt; 2<sup>STATE_SIZE</sup>. ('code' exists only in the decoder.)
105-
* Therefore these variables are unsigned integers of STATE_SIZE bits.</li>
106-
* <li>(low &lt; 1/2 * 2<sup>STATE_SIZE</sup>) && (high &ge; 1/2 * 2<sup>STATE_SIZE</sup>).
104+
* <li>0 &le; low &le; code &le; high &lt; 2<sup>numStateBits</sup>. ('code' exists only in the decoder.)
105+
* Therefore these variables are unsigned integers of numStateBits bits.</li>
106+
* <li>(low &lt; 1/2 * 2<sup>numStateBits</sup>) && (high &ge; 1/2 * 2<sup>numStateBits</sup>).
107107
* In other words, they are in different halves of the full range.</li>
108-
* <li>(low &lt; 1/4 * 2<sup>STATE_SIZE</sup>) || (high &ge; 3/4 * 2<sup>STATE_SIZE</sup>).
108+
* <li>(low &lt; 1/4 * 2<sup>numStateBits</sup>) || (high &ge; 3/4 * 2<sup>numStateBits</sup>).
109109
* In other words, they are not both in the middle two quarters.</li>
110110
* <li>Let range = high &minus; low + 1, then MAX_RANGE/4 &lt; MIN_RANGE &le; range
111-
* &le; MAX_RANGE = 2<sup>STATE_SIZE</sup>. These invariants for 'range' essentially dictate the maximum
111+
* &le; MAX_RANGE = 2<sup>numStateBits</sup>. These invariants for 'range' essentially dictate the maximum
112112
* total that the incoming frequency table can have, such that intermediate calculations don't overflow.</li>
113113
* </ul>
114114
* @param freqs the frequency table to use

java/src/ArithmeticDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public ArithmeticDecoder(int stateSize, BitInputStream in) throws IOException {
4141
super(stateSize);
4242
input = Objects.requireNonNull(in);
4343
code = 0;
44-
for (int i = 0; i < STATE_SIZE; i++)
44+
for (int i = 0; i < numStateBits; i++)
4545
code = code << 1 | readCodeBit();
4646
}
4747

java/src/ArithmeticEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void finish() throws IOException {
8888

8989

9090
protected void shift() throws IOException {
91-
int bit = (int)(low >>> (STATE_SIZE - 1));
91+
int bit = (int)(low >>> (numStateBits - 1));
9292
output.write(bit);
9393

9494
// Write out the saved underflow bits

python/arithmeticcoding.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ def __init__(self, statesize):
3030
# and compression gains beyond ~30 bits essentially zero in real-world applications.
3131
# - Python has native bigint arithmetic, so there is no upper limit to the state size.
3232
# For Java and C++ where using native machine-sized integers makes the most sense,
33-
# they have a recommended value of STATE_SIZE=32 as the most versatile setting.
34-
self.STATE_SIZE = statesize
35-
# Maximum range (high+1-low) during coding (trivial), which is 2^STATE_SIZE = 1000...000.
36-
self.MAX_RANGE = 1 << self.STATE_SIZE
37-
# The top bit at width STATE_SIZE, which is 0100...000.
33+
# they have a recommended value of num_state_bits=32 as the most versatile setting.
34+
self.num_state_bits = statesize
35+
# Maximum range (high+1-low) during coding (trivial), which is 2^num_state_bits = 1000...000.
36+
self.MAX_RANGE = 1 << self.num_state_bits
37+
# The top bit at width num_state_bits, which is 0100...000.
3838
self.TOP_MASK = self.MAX_RANGE >> 1
39-
# The second highest bit at width STATE_SIZE, which is 0010...000. This is zero when STATE_SIZE=1.
39+
# The second highest bit at width num_state_bits, which is 0010...000. This is zero when num_state_bits=1.
4040
self.SECOND_MASK = self.TOP_MASK >> 1
4141
# Minimum range (high+1-low) during coding (non-trivial), which is 0010...010.
4242
self.MIN_RANGE = (self.MAX_RANGE >> 2) + 2
4343
# Maximum allowed total from a frequency table at all times during coding. This differs from Java
4444
# and C++ because Python's native bigint avoids constraining the size of intermediate computations.
4545
self.MAX_TOTAL = self.MIN_RANGE
46-
# Bit mask of STATE_SIZE ones, which is 0111...111.
46+
# Bit mask of num_state_bits ones, which is 0111...111.
4747
self.MASK = self.MAX_RANGE - 1
4848

4949
# -- State fields --
@@ -56,14 +56,14 @@ def __init__(self, statesize):
5656
# Updates the code range (low and high) of this arithmetic coder as a result
5757
# of processing the given symbol with the given frequency table.
5858
# Invariants that are true before and after encoding/decoding each symbol:
59-
# - 0 <= low <= code <= high < 2^STATE_SIZE. ('code' exists only in the decoder.)
60-
# Therefore these variables are unsigned integers of STATE_SIZE bits.
61-
# - (low < 1/2 * 2^STATE_SIZE) && (high >= 1/2 * 2^STATE_SIZE).
59+
# - 0 <= low <= code <= high < 2^num_state_bits. ('code' exists only in the decoder.)
60+
# Therefore these variables are unsigned integers of num_state_bits bits.
61+
# - (low < 1/2 * 2^num_state_bits) && (high >= 1/2 * 2^num_state_bits).
6262
# In other words, they are in different halves of the full range.
63-
# - (low < 1/4 * 2^STATE_SIZE) || (high >= 3/4 * 2^STATE_SIZE).
63+
# - (low < 1/4 * 2^num_state_bits) || (high >= 3/4 * 2^num_state_bits).
6464
# In other words, they are not both in the middle two quarters.
6565
# - Let range = high - low + 1, then MAX_RANGE/4 < MIN_RANGE <= range
66-
# <= MAX_RANGE = 2^STATE_SIZE. These invariants for 'range' essentially
66+
# <= MAX_RANGE = 2^num_state_bits. These invariants for 'range' essentially
6767
# dictate the maximum total that the incoming frequency table can have.
6868
def update(self, freqs, symbol):
6969
# State check
@@ -142,7 +142,7 @@ def finish(self):
142142

143143

144144
def shift(self):
145-
bit = self.low >> (self.STATE_SIZE - 1)
145+
bit = self.low >> (self.num_state_bits - 1)
146146
self.output.write(bit)
147147

148148
# Write out the saved underflow bits
@@ -167,7 +167,7 @@ def __init__(self, statesize, bitin):
167167
self.input = bitin
168168
# The current raw code bits being buffered, which is always in the range [low, high].
169169
self.code = 0
170-
for _ in range(self.STATE_SIZE):
170+
for _ in range(self.num_state_bits):
171171
self.code = self.code << 1 | self.read_code_bit()
172172

173173

0 commit comments

Comments
 (0)