Skip to content

Commit ee6ebac

Browse files
authored
Correct arithmetic coder interval normalisation (#106)
1 parent 6ef8a5b commit ee6ebac

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ where
1717
{
1818
pub fn new(precision: u32) -> Self {
1919
let low = B::ZERO;
20-
let high = B::ONE << precision;
20+
let high = (B::ONE << precision) - B::ONE;
2121

2222
Self {
2323
precision,

src/decoder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,13 @@ where
209209
fn normalise(&mut self) -> io::Result<()> {
210210
while self.state.high < self.state.half() || self.state.low >= self.state.half() {
211211
if self.state.high < self.state.half() {
212-
self.state.high <<= 1;
212+
self.state.high = (self.state.high << 1) + B::ONE;
213213
self.state.low <<= 1;
214214
self.x <<= 1;
215215
} else {
216216
// self.low >= self.half()
217217
self.state.low = (self.state.low - self.state.half()) << 1;
218-
self.state.high = (self.state.high - self.state.half()) << 1;
218+
self.state.high = ((self.state.high - self.state.half()) << 1) + B::ONE;
219219
self.x = (self.x - self.state.half()) << 1;
220220
}
221221

@@ -228,7 +228,7 @@ where
228228
&& self.state.high < (self.state.three_quarter())
229229
{
230230
self.state.low = (self.state.low - self.state.quarter()) << 1;
231-
self.state.high = (self.state.high - self.state.quarter()) << 1;
231+
self.state.high = ((self.state.high - self.state.quarter()) << 1) + B::ONE;
232232
self.x = (self.x - self.state.quarter()) << 1;
233233

234234
if self.input.next_bit()? == Some(true) {

src/encoder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,12 @@ where
201201
while self.state.high < self.state.half() || self.state.low >= self.state.half() {
202202
if self.state.high < self.state.half() {
203203
self.emit(false)?;
204-
self.state.high <<= 1;
204+
self.state.high = (self.state.high << 1) + B::ONE;
205205
self.state.low <<= 1;
206206
} else {
207207
self.emit(true)?;
208208
self.state.low = (self.state.low - self.state.half()) << 1;
209-
self.state.high = (self.state.high - self.state.half()) << 1;
209+
self.state.high = ((self.state.high - self.state.half()) << 1) + B::ONE;
210210
}
211211
}
212212

@@ -215,7 +215,7 @@ where
215215
{
216216
self.pending += 1;
217217
self.state.low = (self.state.low - self.state.quarter()) << 1;
218-
self.state.high = (self.state.high - self.state.quarter()) << 1;
218+
self.state.high = ((self.state.high - self.state.quarter()) << 1) + B::ONE;
219219
}
220220

221221
Ok(())

0 commit comments

Comments
 (0)