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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#![allow(dead_code,
mutable_transmutes,
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused_mut)]
use std;
use std::io::Read;
pub type size_t = std::os::raw::c_ulong;
pub type int32_t = std::os::raw::c_int;
pub type uint32_t = std::os::raw::c_uint;
pub type __uint16_t = std::os::raw::c_ushort;
pub type __uint32_t = std::os::raw::c_uint;
pub type __int64_t = std::os::raw::c_longlong;
pub type __uint64_t = std::os::raw::c_ulonglong;
pub type __darwin_off_t = __int64_t;
pub type opus_int32 = int32_t;
pub type opus_uint32 = uint32_t;
pub type ec_window = opus_uint32;
pub struct ec_dec<R>
where
R: std::io::Read,
{
pub inp: R,
pub end_window: ec_window,
pub nend_bits: std::os::raw::c_int,
pub nbits_total: std::os::raw::c_int,
pub rng: opus_uint32,
pub val: opus_uint32,
pub ext: opus_uint32,
pub rem: std::os::raw::c_int,
}
unsafe extern "C" fn celt_udiv(mut n: opus_uint32, mut d: opus_uint32) -> opus_uint32 {
return n.wrapping_div(d);
}
unsafe extern "C" fn celt_sudiv(mut n: opus_int32, mut d: opus_int32) -> opus_int32 {
return n / d;
}
pub unsafe extern "C" fn ec_dec_init<R: Read>(
mut _this: *mut ec_dec<R>,
) -> Result<(), std::io::Error> {
(*_this).end_window = 0i32 as ec_window;
(*_this).nend_bits = 0i32;
(*_this).nbits_total = 32i32 + 1i32 - (32i32 - ((32i32 - 2i32) % 8i32 + 1i32)) / 8i32 * 8i32;
(*_this).rng = 1u32 << (32i32 - 2i32) % 8i32 + 1i32;
(*_this).rem = ec_read_byte(_this)? as i32;
(*_this).val = (*_this)
.rng
.wrapping_sub(1i32 as std::os::raw::c_uint)
.wrapping_sub(
((*_this).rem >> 8i32 - ((32i32 - 2i32) % 8i32 + 1i32)) as std::os::raw::c_uint,
);
ec_dec_normalize(_this)?;
Ok(())
}
unsafe extern "C" fn ec_dec_normalize<R: Read>(
mut _this: *mut ec_dec<R>,
) -> Result<(), std::io::Error> {
while (*_this).rng <= 1u32 << 32i32 - 1i32 >> 8i32 {
(*_this).nbits_total += 8i32;
(*_this).rng <<= 8i32;
let mut sym = (*_this).rem;
(*_this).rem = ec_read_byte(_this)? as i32;
sym = (sym << 8i32 | (*_this).rem) >> 8i32 - ((32i32 - 2i32) % 8i32 + 1i32);
(*_this).val = ((*_this).val << 8i32).wrapping_add(
(1u32 << 8i32).wrapping_sub(1i32 as std::os::raw::c_uint)
& !sym as std::os::raw::c_uint,
) & (1u32 << 32i32 - 1i32).wrapping_sub(1i32 as std::os::raw::c_uint)
}
Ok(())
}
unsafe extern "C" fn ec_read_byte<R: Read>(
mut _this: *mut ec_dec<R>,
) -> Result<u8, std::io::Error> {
let mut buf = [0];
if let Err(err) = (*_this).inp.read_exact(&mut buf) {
if let std::io::ErrorKind::UnexpectedEof = err.kind() {
Ok(0)
} else {
Err(err)
}
} else {
Ok(buf[0])
}
}
pub unsafe extern "C" fn ec_decode<R: Read>(
mut _this: *mut ec_dec<R>,
mut _ft: std::os::raw::c_uint,
) -> std::os::raw::c_uint {
(*_this).ext = celt_udiv((*_this).rng, _ft);
let mut s = (*_this).val.wrapping_div((*_this).ext);
return _ft.wrapping_sub(s.wrapping_add(1i32 as std::os::raw::c_uint).wrapping_add(
_ft.wrapping_sub(s.wrapping_add(1i32 as std::os::raw::c_uint))
& -((_ft < s.wrapping_add(1i32 as std::os::raw::c_uint)) as std::os::raw::c_int)
as std::os::raw::c_uint,
));
}
pub unsafe extern "C" fn ec_decode_bin<R: Read>(
mut _this: *mut ec_dec<R>,
mut _bits: std::os::raw::c_uint,
) -> std::os::raw::c_uint {
(*_this).ext = (*_this).rng >> _bits;
let mut s = (*_this).val.wrapping_div((*_this).ext);
return (1u32 << _bits).wrapping_sub(s.wrapping_add(1u32).wrapping_add(
(1u32 << _bits).wrapping_sub(s.wrapping_add(1u32))
& -((1u32 << _bits < s.wrapping_add(1u32)) as std::os::raw::c_int)
as std::os::raw::c_uint,
));
}
pub unsafe extern "C" fn ec_dec_update<R: Read>(
mut _this: *mut ec_dec<R>,
mut _fl: std::os::raw::c_uint,
mut _fh: std::os::raw::c_uint,
mut _ft: std::os::raw::c_uint,
) -> Result<(), std::io::Error> {
let mut s = (*_this).ext.wrapping_mul(_ft.wrapping_sub(_fh));
(*_this).val =
((*_this).val as std::os::raw::c_uint).wrapping_sub(s) as opus_uint32 as opus_uint32;
(*_this).rng = if _fl > 0i32 as std::os::raw::c_uint {
(*_this).ext.wrapping_mul(_fh.wrapping_sub(_fl))
} else {
(*_this).rng.wrapping_sub(s)
};
ec_dec_normalize(_this)?;
Ok(())
}
pub unsafe extern "C" fn ec_dec_bit_logp<R: Read>(
mut _this: *mut ec_dec<R>,
mut _logp: std::os::raw::c_uint,
) -> Result<i32, std::io::Error> {
let mut r = (*_this).rng;
let mut d = (*_this).val;
let mut s = r >> _logp;
let mut ret = (d < s) as std::os::raw::c_int;
if 0 == ret {
(*_this).val = d.wrapping_sub(s)
}
(*_this).rng = if 0 != ret { s } else { r.wrapping_sub(s) };
ec_dec_normalize(_this)?;
Ok(ret)
}
pub unsafe extern "C" fn ec_dec_icdf<R: Read>(
mut _this: *mut ec_dec<R>,
mut _icdf: *const std::os::raw::c_uchar,
mut _ftb: std::os::raw::c_uint,
) -> Result<i32, std::io::Error> {
let mut t;
let mut s = (*_this).rng;
let mut d = (*_this).val;
let mut r = s >> _ftb;
let mut ret = -1i32;
loop {
t = s;
ret += 1;
s = r.wrapping_mul(*_icdf.offset(ret as isize) as std::os::raw::c_uint);
if !(d < s) {
break;
}
}
(*_this).val = d.wrapping_sub(s);
(*_this).rng = t.wrapping_sub(s);
ec_dec_normalize(_this)?;
Ok(ret)
}