@@ -36,7 +36,7 @@ impl<T: Default> BitResult<T> {
36
36
}
37
37
38
38
#[ cfg_attr( test, derive( Debug ) ) ]
39
- pub ( crate ) struct BoolReader {
39
+ pub ( crate ) struct ArithmeticDecoder {
40
40
chunks : Box < [ [ u8 ; 4 ] ] > ,
41
41
state : State ,
42
42
final_bytes : [ u8 ; 3 ] ,
@@ -53,21 +53,21 @@ struct State {
53
53
}
54
54
55
55
#[ cfg_attr( test, derive( Debug ) ) ]
56
- struct FastReader < ' a > {
56
+ struct FastDecoder < ' a > {
57
57
chunks : & ' a [ [ u8 ; 4 ] ] ,
58
58
uncommitted_state : State ,
59
59
save_state : & ' a mut State ,
60
60
}
61
61
62
- impl BoolReader {
63
- pub ( crate ) fn new ( ) -> BoolReader {
62
+ impl ArithmeticDecoder {
63
+ pub ( crate ) fn new ( ) -> ArithmeticDecoder {
64
64
let state = State {
65
65
chunk_index : 0 ,
66
66
value : 0 ,
67
67
range : 255 ,
68
68
bit_count : -8 ,
69
69
} ;
70
- BoolReader {
70
+ ArithmeticDecoder {
71
71
chunks : Box :: new ( [ ] ) ,
72
72
state,
73
73
final_bytes : [ 0 ; 3 ] ,
@@ -117,7 +117,7 @@ impl BoolReader {
117
117
/// discarded anyway.
118
118
///
119
119
/// 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 `.
121
121
#[ inline( always) ]
122
122
pub ( crate ) fn start_accumulated_result ( & mut self ) -> BitResultAccumulator {
123
123
BitResultAccumulator
@@ -216,7 +216,7 @@ impl BoolReader {
216
216
self . cold_read_with_tree ( tree, usize:: from ( first_node. index ) )
217
217
}
218
218
219
- // As a similar (but different) speedup to BitResult, the FastReader reads
219
+ // As a similar (but different) speedup to BitResult, the FastDecoder reads
220
220
// bits under an assumption and validates it at the end.
221
221
//
222
222
// The idea here is that for normal-sized webp images, the vast majority
@@ -228,8 +228,8 @@ impl BoolReader {
228
228
// work for those last few bytes -- in fact we even keep retrying the fast
229
229
// method to save an if-statement --, but more than make up for that by
230
230
// 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 {
233
233
chunks : & self . chunks ,
234
234
uncommitted_state : self . state ,
235
235
save_state : & mut self . state ,
@@ -377,7 +377,7 @@ impl BoolReader {
377
377
}
378
378
}
379
379
380
- impl FastReader < ' _ > {
380
+ impl FastDecoder < ' _ > {
381
381
fn commit_if_valid < T > ( self , value_if_not_past_eof : T ) -> Option < T > {
382
382
// If `chunk_index > self.chunks.len()`, it means we used zeroes
383
383
// instead of an actual chunk and `value_if_not_past_eof` is nonsense.
@@ -564,50 +564,50 @@ mod tests {
564
564
use super :: * ;
565
565
566
566
#[ 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 ( ) ;
569
569
let data = b"hel" ;
570
570
let size = data. len ( ) ;
571
571
let mut buf = vec ! [ [ 0u8 ; 4 ] ; 1 ] ;
572
572
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 ( ) ;
583
583
}
584
584
585
585
#[ 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 ( ) ;
588
588
let data = b"hello world" ;
589
589
let size = data. len ( ) ;
590
590
let mut buf = vec ! [ [ 0u8 ; 4 ] ; ( size + 3 ) / 4 ] ;
591
591
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 ( ) ;
603
603
}
604
604
605
605
#[ 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, ( ) ) ;
611
611
assert ! ( result. is_err( ) ) ;
612
612
}
613
613
}
0 commit comments