Skip to content

Commit 344ec6f

Browse files
SLiV9kornelski
authored andcommitted
Rename BoolReader to ArithmeticDecoder
1 parent de0fa12 commit 344ec6f

File tree

3 files changed

+61
-62
lines changed

3 files changed

+61
-62
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub use self::decoder::{DecodingError, LoopCount, WebPDecoder};
1313
pub use self::encoder::{ColorType, EncoderParams, EncodingError, WebPEncoder};
1414

1515
mod alpha_blending;
16-
mod bool_reader;
1716
mod decoder;
1817
mod encoder;
1918
mod extended;
@@ -22,5 +21,6 @@ mod loop_filter;
2221
mod lossless;
2322
mod lossless_transform;
2423
mod transform;
24+
mod vp8_arithmetic_decoder;
2525

2626
pub mod vp8;

src/vp8.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ use std::io::Read;
1717

1818
use crate::decoder::DecodingError;
1919

20-
use super::bool_reader::BoolReader;
21-
use super::loop_filter;
22-
use super::transform;
20+
use super::vp8_arithmetic_decoder::ArithmeticDecoder;
21+
use super::{loop_filter, transform};
2322

2423
const MAX_SEGMENTS: usize = 4;
2524
const NUM_DCT_TOKENS: usize = 12;
@@ -1021,7 +1020,7 @@ struct Segment {
10211020
/// Only decodes keyframes
10221021
pub struct Vp8Decoder<R> {
10231022
r: R,
1024-
b: BoolReader,
1023+
b: ArithmeticDecoder,
10251024

10261025
mbwidth: u16,
10271026
mbheight: u16,
@@ -1036,7 +1035,7 @@ pub struct Vp8Decoder<R> {
10361035
ref_delta: [i32; 4],
10371036
mode_delta: [i32; 4],
10381037

1039-
partitions: [BoolReader; 8],
1038+
partitions: [ArithmeticDecoder; 8],
10401039
num_partitions: u8,
10411040

10421041
segment_tree_nodes: [TreeNode; 3],
@@ -1065,7 +1064,7 @@ impl<R: Read> Vp8Decoder<R> {
10651064

10661065
Self {
10671066
r,
1068-
b: BoolReader::new(),
1067+
b: ArithmeticDecoder::new(),
10691068

10701069
mbwidth: 0,
10711070
mbheight: 0,
@@ -1080,14 +1079,14 @@ impl<R: Read> Vp8Decoder<R> {
10801079
mode_delta: [0; 4],
10811080

10821081
partitions: [
1083-
BoolReader::new(),
1084-
BoolReader::new(),
1085-
BoolReader::new(),
1086-
BoolReader::new(),
1087-
BoolReader::new(),
1088-
BoolReader::new(),
1089-
BoolReader::new(),
1090-
BoolReader::new(),
1082+
ArithmeticDecoder::new(),
1083+
ArithmeticDecoder::new(),
1084+
ArithmeticDecoder::new(),
1085+
ArithmeticDecoder::new(),
1086+
ArithmeticDecoder::new(),
1087+
ArithmeticDecoder::new(),
1088+
ArithmeticDecoder::new(),
1089+
ArithmeticDecoder::new(),
10911090
],
10921091

10931092
num_partitions: 1,
@@ -1631,9 +1630,9 @@ impl<R: Read> Vp8Decoder<R> {
16311630

16321631
let first = if plane == 0 { 1usize } else { 0usize };
16331632
let probs = &self.token_probs[plane];
1634-
let reader = &mut self.partitions[p];
1633+
let decoder = &mut self.partitions[p];
16351634

1636-
let mut res = reader.start_accumulated_result();
1635+
let mut res = decoder.start_accumulated_result();
16371636

16381637
let mut complexity = complexity;
16391638
let mut has_coefficients = false;
@@ -1643,7 +1642,7 @@ impl<R: Read> Vp8Decoder<R> {
16431642
let band = COEFF_BANDS[i] as usize;
16441643
let tree = &probs[band][complexity];
16451644

1646-
let token = reader
1645+
let token = decoder
16471646
.read_with_tree_with_first_node(tree, tree[skip as usize])
16481647
.or_accumulate(&mut res);
16491648

@@ -1668,8 +1667,8 @@ impl<R: Read> Vp8Decoder<R> {
16681667
if t == 0 {
16691668
break;
16701669
}
1671-
let b = reader.read_bool(t).or_accumulate(&mut res);
1672-
extra = extra + extra + b as i16;
1670+
let b = decoder.read_bool(t).or_accumulate(&mut res);
1671+
extra = extra + extra + i16::from(b);
16731672
}
16741673

16751674
i16::from(DCT_CAT_BASE[(category - DCT_CAT1) as usize]) + extra
@@ -1688,7 +1687,7 @@ impl<R: Read> Vp8Decoder<R> {
16881687
2
16891688
};
16901689

1691-
if reader.read_flag().or_accumulate(&mut res) {
1690+
if decoder.read_flag().or_accumulate(&mut res) {
16921691
abs_value = -abs_value;
16931692
}
16941693

@@ -1698,7 +1697,7 @@ impl<R: Read> Vp8Decoder<R> {
16981697
has_coefficients = true;
16991698
}
17001699

1701-
reader.check(res, has_coefficients)
1700+
decoder.check(res, has_coefficients)
17021701
}
17031702

17041703
fn read_residual_data(

src/bool_reader.rs renamed to src/vp8_arithmetic_decoder.rs

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<T: Default> BitResult<T> {
3636
}
3737

3838
#[cfg_attr(test, derive(Debug))]
39-
pub(crate) struct BoolReader {
39+
pub(crate) struct ArithmeticDecoder {
4040
chunks: Box<[[u8; 4]]>,
4141
state: State,
4242
final_bytes: [u8; 3],
@@ -53,21 +53,21 @@ struct State {
5353
}
5454

5555
#[cfg_attr(test, derive(Debug))]
56-
struct FastReader<'a> {
56+
struct FastDecoder<'a> {
5757
chunks: &'a [[u8; 4]],
5858
uncommitted_state: State,
5959
save_state: &'a mut State,
6060
}
6161

62-
impl BoolReader {
63-
pub(crate) fn new() -> BoolReader {
62+
impl ArithmeticDecoder {
63+
pub(crate) fn new() -> ArithmeticDecoder {
6464
let state = State {
6565
chunk_index: 0,
6666
value: 0,
6767
range: 255,
6868
bit_count: -8,
6969
};
70-
BoolReader {
70+
ArithmeticDecoder {
7171
chunks: Box::new([]),
7272
state,
7373
final_bytes: [0; 3],
@@ -117,7 +117,7 @@ impl BoolReader {
117117
/// discarded anyway.
118118
///
119119
/// Each call to `start_accumulated_result` must be followed by a call to
120-
/// `check` on the *same* `BoolReader`.
120+
/// `check` on the *same* `ArithmeticDecoder`.
121121
#[inline(always)]
122122
pub(crate) fn start_accumulated_result(&mut self) -> BitResultAccumulator {
123123
BitResultAccumulator
@@ -216,7 +216,7 @@ impl BoolReader {
216216
self.cold_read_with_tree(tree, usize::from(first_node.index))
217217
}
218218

219-
// As a similar (but different) speedup to BitResult, the FastReader reads
219+
// As a similar (but different) speedup to BitResult, the FastDecoder reads
220220
// bits under an assumption and validates it at the end.
221221
//
222222
// The idea here is that for normal-sized webp images, the vast majority
@@ -228,8 +228,8 @@ impl BoolReader {
228228
// work for those last few bytes -- in fact we even keep retrying the fast
229229
// method to save an if-statement --, but more than make up for that by
230230
// speeding up reading from the other thousands or millions of bytes.
231-
fn fast(&mut self) -> FastReader<'_> {
232-
FastReader {
231+
fn fast(&mut self) -> FastDecoder<'_> {
232+
FastDecoder {
233233
chunks: &self.chunks,
234234
uncommitted_state: self.state,
235235
save_state: &mut self.state,
@@ -377,7 +377,7 @@ impl BoolReader {
377377
}
378378
}
379379

380-
impl FastReader<'_> {
380+
impl FastDecoder<'_> {
381381
fn commit_if_valid<T>(self, value_if_not_past_eof: T) -> Option<T> {
382382
// If `chunk_index > self.chunks.len()`, it means we used zeroes
383383
// instead of an actual chunk and `value_if_not_past_eof` is nonsense.
@@ -564,50 +564,50 @@ mod tests {
564564
use super::*;
565565

566566
#[test]
567-
fn test_bool_reader_hello_short() {
568-
let mut reader = BoolReader::new();
567+
fn test_arithmetic_decoder_hello_short() {
568+
let mut decoder = ArithmeticDecoder::new();
569569
let data = b"hel";
570570
let size = data.len();
571571
let mut buf = vec![[0u8; 4]; 1];
572572
buf.as_mut_slice().as_flattened_mut()[..size].copy_from_slice(&data[..]);
573-
reader.init(buf, size).unwrap();
574-
let mut res = reader.start_accumulated_result();
575-
assert_eq!(false, reader.read_flag().or_accumulate(&mut res));
576-
assert_eq!(true, reader.read_bool(10).or_accumulate(&mut res));
577-
assert_eq!(false, reader.read_bool(250).or_accumulate(&mut res));
578-
assert_eq!(1, reader.read_literal(1).or_accumulate(&mut res));
579-
assert_eq!(5, reader.read_literal(3).or_accumulate(&mut res));
580-
assert_eq!(64, reader.read_literal(8).or_accumulate(&mut res));
581-
assert_eq!(185, reader.read_literal(8).or_accumulate(&mut res));
582-
reader.check(res, ()).unwrap();
573+
decoder.init(buf, size).unwrap();
574+
let mut res = decoder.start_accumulated_result();
575+
assert_eq!(false, decoder.read_flag().or_accumulate(&mut res));
576+
assert_eq!(true, decoder.read_bool(10).or_accumulate(&mut res));
577+
assert_eq!(false, decoder.read_bool(250).or_accumulate(&mut res));
578+
assert_eq!(1, decoder.read_literal(1).or_accumulate(&mut res));
579+
assert_eq!(5, decoder.read_literal(3).or_accumulate(&mut res));
580+
assert_eq!(64, decoder.read_literal(8).or_accumulate(&mut res));
581+
assert_eq!(185, decoder.read_literal(8).or_accumulate(&mut res));
582+
decoder.check(res, ()).unwrap();
583583
}
584584

585585
#[test]
586-
fn test_bool_reader_hello_long() {
587-
let mut reader = BoolReader::new();
586+
fn test_arithmetic_decoder_hello_long() {
587+
let mut decoder = ArithmeticDecoder::new();
588588
let data = b"hello world";
589589
let size = data.len();
590590
let mut buf = vec![[0u8; 4]; (size + 3) / 4];
591591
buf.as_mut_slice().as_flattened_mut()[..size].copy_from_slice(&data[..]);
592-
reader.init(buf, size).unwrap();
593-
let mut res = reader.start_accumulated_result();
594-
assert_eq!(false, reader.read_flag().or_accumulate(&mut res));
595-
assert_eq!(true, reader.read_bool(10).or_accumulate(&mut res));
596-
assert_eq!(false, reader.read_bool(250).or_accumulate(&mut res));
597-
assert_eq!(1, reader.read_literal(1).or_accumulate(&mut res));
598-
assert_eq!(5, reader.read_literal(3).or_accumulate(&mut res));
599-
assert_eq!(64, reader.read_literal(8).or_accumulate(&mut res));
600-
assert_eq!(185, reader.read_literal(8).or_accumulate(&mut res));
601-
assert_eq!(31, reader.read_literal(8).or_accumulate(&mut res));
602-
reader.check(res, ()).unwrap();
592+
decoder.init(buf, size).unwrap();
593+
let mut res = decoder.start_accumulated_result();
594+
assert_eq!(false, decoder.read_flag().or_accumulate(&mut res));
595+
assert_eq!(true, decoder.read_bool(10).or_accumulate(&mut res));
596+
assert_eq!(false, decoder.read_bool(250).or_accumulate(&mut res));
597+
assert_eq!(1, decoder.read_literal(1).or_accumulate(&mut res));
598+
assert_eq!(5, decoder.read_literal(3).or_accumulate(&mut res));
599+
assert_eq!(64, decoder.read_literal(8).or_accumulate(&mut res));
600+
assert_eq!(185, decoder.read_literal(8).or_accumulate(&mut res));
601+
assert_eq!(31, decoder.read_literal(8).or_accumulate(&mut res));
602+
decoder.check(res, ()).unwrap();
603603
}
604604

605605
#[test]
606-
fn test_bool_reader_uninit() {
607-
let mut reader = BoolReader::new();
608-
let mut res = reader.start_accumulated_result();
609-
let _ = reader.read_flag().or_accumulate(&mut res);
610-
let result = reader.check(res, ());
606+
fn test_arithmetic_decoder_uninit() {
607+
let mut decoder = ArithmeticDecoder::new();
608+
let mut res = decoder.start_accumulated_result();
609+
let _ = decoder.read_flag().or_accumulate(&mut res);
610+
let result = decoder.check(res, ());
611611
assert!(result.is_err());
612612
}
613613
}

0 commit comments

Comments
 (0)