1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use ::CumulativeDistributionFrequency;
use opus::imported_decode;
use std;
pub struct Reader<R> where R: std::io::Read {
state: imported_decode::ec_dec<R>,
}
impl<R> Reader<R> where R: std::io::Read {
pub fn new(input: R) -> Result<Self, std::io::Error> {
let mut state = imported_decode::ec_dec {
inp: input,
end_window: 0,
nend_bits: 0,
nbits_total: 0,
rng: 0,
rem: 0,
val: 0,
ext: 0,
};
unsafe { imported_decode::ec_dec_init(&mut state)?; }
Ok(Reader {
state
})
}
pub fn symbol(&mut self, icdf: &CumulativeDistributionFrequency) -> Result<u32, std::io::Error> {
let index = unsafe {
let frequency = imported_decode::ec_decode(&mut self.state, icdf.width());
let indexed= icdf.find(frequency)
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid probability"))?;
imported_decode::ec_dec_update(&mut self.state, indexed.segment.low, indexed.segment.next, icdf.width())?;
indexed.index
};
Ok(index as u32)
}
pub fn done(self) {
}
}