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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
use std::collections::HashMap;
use std::cmp::{max, min};
use super::bitreader::BitReader;
#[derive(Debug)]
pub enum CodebookError {
InvalidCodebook,
MemoryError,
InvalidCode,
}
#[derive(Debug, Copy, Clone)]
pub enum CodebookMode {
MSB,
LSB,
}
type CodebookResult<T> = Result<T, CodebookError>;
pub struct FullCodebookDesc<S> {
code: u32,
bits: u8,
sym: S,
}
pub struct ShortCodebookDesc {
code: u32,
bits: u8,
}
pub trait CodebookDescReader<S> {
fn bits(&mut self, idx: usize) -> u8;
fn code(&mut self, idx: usize) -> u32;
fn sym (&mut self, idx: usize) -> S;
fn len (&mut self) -> usize;
}
#[allow(dead_code)]
pub struct Codebook<S> {
table: Vec<u32>,
syms: Vec<S>,
lut_bits: u8,
}
pub trait CodebookReader<S> {
fn read_cb(&mut self, cb: &Codebook<S>) -> CodebookResult<S>;
}
const TABLE_FILL_VALUE: u32 = 0x7F;
const MAX_LUT_BITS: u8 = 10;
fn fill_lut_msb(table: &mut Vec<u32>, off: usize,
code: u32, bits: u8, lut_bits: u8, symidx: u32, esc: bool) {
if !esc {
let fill_len = lut_bits - bits;
let fill_size = 1 << fill_len;
let fill_code = code << (lut_bits - bits);
let lut_value = (symidx << 8) | (bits as u32);
for j in 0..fill_size {
let idx = (fill_code + j) as usize;
table[idx + off] = lut_value;
}
} else {
let idx = (code as usize) + off;
table[idx] = (symidx << 8) | 0x80 | (bits as u32);
}
}
fn fill_lut_lsb(table: &mut Vec<u32>, off: usize,
code: u32, bits: u8, lut_bits: u8, symidx: u32, esc: bool) {
if !esc {
let fill_len = lut_bits - bits;
let fill_size = 1 << fill_len;
let fill_code = code;
let step = lut_bits - fill_len;
for j in 0..fill_size {
let idx = (fill_code + (j << step)) as usize;
table[idx + off] = (symidx << 8) | (bits as u32);
}
} else {
let idx = (code as usize) + off;
table[idx] = (symidx << 8) | 0x80 | (bits as u32);
}
}
fn fill_lut(table: &mut Vec<u32>, mode: CodebookMode,
off: usize, code: u32, bits: u8, lut_bits: u8, symidx: u32, esc: bool) -> bool {
match mode {
CodebookMode::MSB => fill_lut_msb(table, off, code, bits, lut_bits, symidx, esc),
CodebookMode::LSB => fill_lut_lsb(table, off, code, bits, lut_bits, symidx, esc),
};
bits > lut_bits
}
fn resize_table(table: &mut Vec<u32>, bits: u8) -> CodebookResult<u32> {
let add_size = (1 << bits) as usize;
table.reserve(add_size);
let cur_off = table.len() as u32;
let new_size = table.len() + add_size;
if table.capacity() < new_size { return Err(CodebookError::MemoryError); }
table.resize(new_size, TABLE_FILL_VALUE);
Ok(cur_off)
}
fn extract_lut_part(code: u32, bits: u8, lut_bits: u8, mode: CodebookMode) -> u32 {
match mode {
CodebookMode::MSB => code >> (bits - lut_bits),
CodebookMode::LSB => code & ((1 << lut_bits) - 1),
}
}
fn extract_esc_part(code: u32, bits: u8, lut_bits: u8, mode: CodebookMode) -> u32 {
match mode {
CodebookMode::MSB => code & ((1 << (bits - lut_bits)) - 1),
CodebookMode::LSB => code >> lut_bits,
}
}
#[derive(Clone,Copy)]
struct Code {
code: u32,
bits: u8,
idx: usize,
}
struct CodeBucket {
maxlen: u8,
offset: usize,
codes: Vec<Code>,
}
impl CodeBucket {
fn new() -> Self {
CodeBucket { maxlen: 0, offset: 0, codes: Vec::new() }
}
fn add_code(&mut self, c: Code) {
if c.bits > self.maxlen { self.maxlen = c.bits; }
self.codes.push(c);
}
}
type EscapeCodes = HashMap<u32, CodeBucket>;
fn add_esc_code(cc: &mut EscapeCodes, key: u32, code: u32, bits: u8, idx: usize) {
if !cc.contains_key(&key) { cc.insert(key, CodeBucket::new()); }
let b = cc.get_mut(&key);
if let Some(bucket) = b {
bucket.add_code(Code {code: code, bits: bits, idx: idx });
} else { panic!("no bucket when expected!"); }
}
fn build_esc_lut(table: &mut Vec<u32>,
mode: CodebookMode,
bucket: &CodeBucket) -> CodebookResult<()> {
let mut escape_list: EscapeCodes = HashMap::new();
let maxlen = if bucket.maxlen > MAX_LUT_BITS { MAX_LUT_BITS } else { bucket.maxlen };
for code in &bucket.codes {
let bits = code.bits;
if code.bits <= MAX_LUT_BITS {
fill_lut(table, mode, bucket.offset, code.code, bits,
maxlen, code.idx as u32, false);
} else {
let ckey = extract_lut_part(code.code, bits, MAX_LUT_BITS, mode);
let cval = extract_esc_part(code.code, bits, MAX_LUT_BITS, mode);
add_esc_code(&mut escape_list, ckey, cval, bits - MAX_LUT_BITS, code.idx);
}
}
let cur_offset = bucket.offset;
for (ckey, sec_bucket) in &mut escape_list {
let key = *ckey as u32;
let maxlen = min(sec_bucket.maxlen, MAX_LUT_BITS);
let new_off = resize_table(table, maxlen)?;
fill_lut(table, mode, cur_offset, key, maxlen,
MAX_LUT_BITS, new_off, true);
sec_bucket.offset = new_off as usize;
}
for (_, sec_bucket) in &escape_list {
build_esc_lut(table, mode, sec_bucket)?;
}
Ok(())
}
impl<S: Copy> Codebook<S> {
pub fn new(cb: &mut CodebookDescReader<S>, mode: CodebookMode) -> CodebookResult<Self> {
let mut maxbits = 0;
let mut nnz = 0;
let mut escape_list: EscapeCodes = HashMap::new();
let mut symidx: usize = 0;
for i in 0..cb.len() {
let bits = cb.bits(i);
if bits > 0 {
nnz = nnz + 1;
if cb.code(i) >= (1 << bits) {
return Err(CodebookError::InvalidCodebook);
}
}
maxbits = max(bits, maxbits);
if bits > MAX_LUT_BITS {
let code = cb.code(i);
let ckey = extract_lut_part(code, bits, MAX_LUT_BITS, mode);
let cval = extract_esc_part(code, bits, MAX_LUT_BITS, mode);
add_esc_code(&mut escape_list, ckey, cval, bits - MAX_LUT_BITS, symidx);
}
if bits > 0 { symidx = symidx + 1; }
}
if maxbits == 0 { return Err(CodebookError::InvalidCodebook); }
if maxbits > MAX_LUT_BITS { maxbits = MAX_LUT_BITS; }
let tab_len = 1 << maxbits;
let mut table: Vec<u32> = Vec::with_capacity(tab_len);
let mut syms: Vec<S> = Vec::with_capacity(nnz);
if table.capacity() < tab_len { return Err(CodebookError::MemoryError); }
if syms.capacity() < nnz { return Err(CodebookError::MemoryError); }
table.resize(tab_len, TABLE_FILL_VALUE);
let mut symidx: u32 = 0;
for i in 0..cb.len() {
let bits = cb.bits(i);
let code = cb.code(i);
if bits == 0 { continue; }
if bits <= MAX_LUT_BITS {
fill_lut(&mut table, mode, 0, code, bits, maxbits, symidx, false);
} else {
let ckey = extract_lut_part(code, bits, MAX_LUT_BITS, mode) as usize;
if table[ckey] == TABLE_FILL_VALUE {
let key = ckey as u32;
if let Some(bucket) = escape_list.get_mut(&key) {
let maxlen = min(bucket.maxlen, MAX_LUT_BITS);
let new_off = resize_table(&mut table, maxlen)?;
fill_lut(&mut table, mode, 0, key, maxlen, MAX_LUT_BITS, new_off, true);
bucket.offset = new_off as usize;
}
}
}
symidx = symidx + 1;
}
for (_, bucket) in &escape_list {
build_esc_lut(&mut table, mode, &bucket)?;
}
for i in 0..cb.len() {
if cb.bits(i) > 0 {
syms.push(cb.sym(i));
}
}
Ok(Codebook { table: table, syms: syms, lut_bits: maxbits })
}
}
impl<'a, S: Copy> CodebookReader<S> for BitReader<'a> {
#[allow(unused_variables)]
fn read_cb(&mut self, cb: &Codebook<S>) -> CodebookResult<S> {
let mut esc = true;
let mut idx = 0;
let mut lut_bits = cb.lut_bits;
while esc {
let lut_idx = (self.peek(lut_bits) as usize) + (idx as usize);
if cb.table[lut_idx] == TABLE_FILL_VALUE { return Err(CodebookError::InvalidCode); }
let bits = cb.table[lut_idx] & 0x7F;
esc = (cb.table[lut_idx] & 0x80) != 0;
idx = (cb.table[lut_idx] >> 8) as usize;
if (bits as isize) > self.left() {
return Err(CodebookError::InvalidCode);
}
let skip_bits = if esc { lut_bits as u32 } else { bits };
if let Err(_) = self.skip(skip_bits as u32) {}
lut_bits = bits as u8;
}
Ok(cb.syms[idx])
}
}
pub struct FullCodebookDescReader<S> {
data: Vec<FullCodebookDesc<S>>,
}
impl<S> FullCodebookDescReader<S> {
pub fn new(data: Vec<FullCodebookDesc<S>>) -> Self {
FullCodebookDescReader { data: data }
}
}
impl<S: Copy> CodebookDescReader<S> for FullCodebookDescReader<S> {
fn bits(&mut self, idx: usize) -> u8 { self.data[idx].bits }
fn code(&mut self, idx: usize) -> u32 { self.data[idx].code }
fn sym (&mut self, idx: usize) -> S { self.data[idx].sym }
fn len(&mut self) -> usize { self.data.len() }
}
pub struct ShortCodebookDescReader {
data: Vec<ShortCodebookDesc>,
}
impl ShortCodebookDescReader {
pub fn new(data: Vec<ShortCodebookDesc<>>) -> Self {
ShortCodebookDescReader { data: data }
}
}
impl CodebookDescReader<u32> for ShortCodebookDescReader {
fn bits(&mut self, idx: usize) -> u8 { self.data[idx].bits }
fn code(&mut self, idx: usize) -> u32 { self.data[idx].code }
fn sym (&mut self, idx: usize) -> u32 { idx as u32 }
fn len(&mut self) -> usize { self.data.len() }
}
pub struct TableCodebookDescReader<CodeType:'static, IndexType:'static> {
bits: &'static [u8],
codes: &'static [CodeType],
idx_map: fn(usize) -> IndexType,
}
impl<'a, CodeType, IndexType> TableCodebookDescReader<CodeType, IndexType> {
pub fn new(codes: &'static [CodeType], bits: &'static [u8], idx_map: fn(usize) -> IndexType) -> Self {
Self { bits, codes, idx_map }
}
}
impl<CodeType: Copy+Into<u32>, IndexType> CodebookDescReader<IndexType> for TableCodebookDescReader<CodeType, IndexType>
{
fn bits(&mut self, idx: usize) -> u8 { self.bits[idx] }
fn code(&mut self, idx: usize) -> u32 { self.codes[idx].into() }
fn sym (&mut self, idx: usize) -> IndexType { (self.idx_map)(idx) }
fn len(&mut self) -> usize { self.bits.len() }
}
#[cfg(test)]
mod test {
use super::*;
use crate::io::bitreader::*;
#[test]
fn test_cb() {
const BITS: [u8; 2] = [0b01011011, 0b10111100];
let cb_desc: Vec<FullCodebookDesc<i8>> = vec!(
FullCodebookDesc { code: 0b0, bits: 1, sym: 16 },
FullCodebookDesc { code: 0b10, bits: 2, sym: -3 },
FullCodebookDesc { code: 0b110, bits: 3, sym: 42 },
FullCodebookDesc { code: 0b1110, bits: 4, sym: -42 }
);
let buf = &BITS;
let mut br = BitReader::new(buf, buf.len(), BitReaderMode::BE);
let mut cfr = FullCodebookDescReader::new(cb_desc);
let cb = Codebook::new(&mut cfr, CodebookMode::MSB).unwrap();
assert_eq!(br.read_cb(&cb).unwrap(), 16);
assert_eq!(br.read_cb(&cb).unwrap(), -3);
assert_eq!(br.read_cb(&cb).unwrap(), 42);
assert_eq!(br.read_cb(&cb).unwrap(), -42);
let ret = br.read_cb(&cb);
if let Err(e) = ret {
assert_eq!(e as i32, CodebookError::InvalidCode as i32);
} else {
assert_eq!(0, 1);
}
let scb_desc: Vec<ShortCodebookDesc> = vec!(
ShortCodebookDesc { code: 0b0, bits: 1 },
ShortCodebookDesc { code: 0, bits: 0 },
ShortCodebookDesc { code: 0b10, bits: 2 },
ShortCodebookDesc { code: 0, bits: 0 },
ShortCodebookDesc { code: 0, bits: 0 },
ShortCodebookDesc { code: 0b110, bits: 3 },
ShortCodebookDesc { code: 0, bits: 0 },
ShortCodebookDesc { code: 0b11100, bits: 5 },
ShortCodebookDesc { code: 0b11101, bits: 5 },
ShortCodebookDesc { code: 0b1111010, bits: 7 },
ShortCodebookDesc { code: 0b1111011, bits: 7 },
ShortCodebookDesc { code: 0b1111110, bits: 7 },
ShortCodebookDesc { code: 0b11111111, bits: 8 }
);
let mut br2 = BitReader::new(buf, buf.len(), BitReaderMode::BE);
let mut cfr = ShortCodebookDescReader::new(scb_desc);
let cb = Codebook::new(&mut cfr, CodebookMode::MSB).unwrap();
assert_eq!(br2.read_cb(&cb).unwrap(), 0);
assert_eq!(br2.read_cb(&cb).unwrap(), 2);
assert_eq!(br2.read_cb(&cb).unwrap(), 5);
assert_eq!(br2.read_cb(&cb).unwrap(), 8);
assert_eq!(reverse_bits(0b0000_0101_1011_1011_1101_1111_0111_1111, 32),
0b1111_1110_1111_1011_1101_1101_1010_0000);
const BITS_LE: [u8; 3] = [0b11101111, 0b01110010, 0b01];
let buf = &BITS_LE;
let scble_desc: Vec<ShortCodebookDesc> = vec!(
ShortCodebookDesc { code: 0b00, bits: 2 },
ShortCodebookDesc { code: 0, bits: 0 },
ShortCodebookDesc { code: 0b01, bits: 2 },
ShortCodebookDesc { code: 0, bits: 0 },
ShortCodebookDesc { code: 0, bits: 0 },
ShortCodebookDesc { code: 0b011, bits: 3 },
ShortCodebookDesc { code: 0, bits: 0 },
ShortCodebookDesc { code: 0b10111, bits: 5 },
ShortCodebookDesc { code: 0b00111, bits: 5 },
ShortCodebookDesc { code: 0b0101111, bits: 7 },
ShortCodebookDesc { code: 0b0111111, bits: 7 },
ShortCodebookDesc { code: 0b1011101111, bits: 10 }
);
let mut brl = BitReader::new(buf, buf.len(), BitReaderMode::LE);
let mut cfr = ShortCodebookDescReader::new(scble_desc);
let cb = Codebook::new(&mut cfr, CodebookMode::LSB).unwrap();
assert_eq!(brl.read_cb(&cb).unwrap(), 11);
assert_eq!(brl.read_cb(&cb).unwrap(), 0);
assert_eq!(brl.read_cb(&cb).unwrap(), 7);
assert_eq!(brl.read_cb(&cb).unwrap(), 0);
}
}
|