range_encoding/opus/
decode.rs

1use CumulativeDistributionFrequency;
2
3use opus::imported_decode;
4
5use std;
6
7pub struct Reader<R>
8where
9    R: std::io::Read,
10{
11    state: imported_decode::ec_dec<R>,
12}
13
14impl<R> Reader<R>
15where
16    R: std::io::Read,
17{
18    /*
19        pub fn from_boxed_slice(mut source: Box<[u8]>) -> Self {
20            let state = unsafe {
21                let mut state : imported_decode::ec_dec = std::mem::uninitialized();
22                imported_decode::ec_dec_init(&mut state, source.as_mut_ptr(), source.len() as u32);
23                state
24            };
25            Reader {
26                source,
27                state
28            }
29        }
30    */
31    pub fn new(input: R) -> Result<Self, std::io::Error> {
32        let mut state = imported_decode::ec_dec {
33            inp: input,
34            // The rest will be initialized by `ec_dec_init`.
35            end_window: 0,
36            nend_bits: 0,
37            nbits_total: 0,
38            rng: 0,
39            rem: 0,
40            val: 0,
41            ext: 0,
42        };
43        unsafe {
44            imported_decode::ec_dec_init(&mut state)?;
45        }
46        Ok(Reader { state })
47    }
48
49    /// Decode the next symbol in line.
50    pub fn symbol(
51        &mut self,
52        icdf: &CumulativeDistributionFrequency,
53    ) -> Result<u32, std::io::Error> {
54        let index = unsafe {
55            let frequency = imported_decode::ec_decode(&mut self.state, icdf.width());
56            let indexed = icdf.find(frequency).ok_or_else(|| {
57                std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid probability")
58            })?;
59            imported_decode::ec_dec_update(
60                &mut self.state,
61                indexed.segment.low,
62                indexed.segment.next,
63                icdf.width(),
64            )?;
65            indexed.index
66        };
67        Ok(index as u32)
68    }
69
70    /*
71    // FIXME: I actually don't understand `bits()` well enough
72    // to publish it.    /// Encode a sequence of raw bits, without any frequency information.
73        pub fn bits(&mut self, size: usize) -> Result<u16, std::io::Error> {
74            let result = unsafe {
75                let result = imported_decode::ec_dec_bits(&mut self.state,
76                    size as u32);
77                self.check_status()?;
78                result as u16
79            };
80            Ok(result)
81        }
82    */
83
84    pub fn done(self) {
85        // FIXME: Nothing to do?
86    }
87}