| 1 |
use std::fmt; |
| 2 |
use std::num::NonZeroU8; |
| 3 |
use std::ops::Range; |
| 4 |
|
| 5 |
use crate::{Bitstream, DecodeFailed}; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#[derive(Debug)] |
| 10 |
pub struct CanonicalTree { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
path_lengths: Vec<u8>, |
| 16 |
} |
| 17 |
|
| 18 |
pub struct Tree { |
| 19 |
path_lengths: Vec<u8>, |
| 20 |
largest_length: NonZeroU8, |
| 21 |
huffman_tree: Vec<u16>, |
| 22 |
} |
| 23 |
|
| 24 |
impl CanonicalTree { |
| 25 |
pub fn new(count: usize) -> Self { |
| 26 |
Self { |
| 27 |
|
| 28 |
|
| 29 |
path_lengths: vec![0; count], |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
pub fn create_instance_allow_empty(&self) -> Result<Option<Tree>, DecodeFailed> { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
let largest_length = |
| 47 |
match NonZeroU8::new(*self.path_lengths.iter().max().expect("empty path lengths")) { |
| 48 |
Some(x) => x, |
| 49 |
|
| 50 |
None => return Ok(None), |
| 51 |
}; |
| 52 |
let mut huffman_tree = vec![0; 1 << largest_length.get()]; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
let mut pos = 0; |
| 60 |
for bit in 1..=largest_length.get() { |
| 61 |
let amount = 1 << (largest_length.get() - bit); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
for code in 0..self.path_lengths.len() { |
| 66 |
|
| 67 |
|
| 68 |
if self.path_lengths[code] == bit { |
| 69 |
huffman_tree |
| 70 |
.get_mut(pos..pos + amount) |
| 71 |
.ok_or(DecodeFailed::InvalidPathLengths)? |
| 72 |
.iter_mut() |
| 73 |
.for_each(|x| *x = code as u16); |
| 74 |
|
| 75 |
pos += amount; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
if pos != huffman_tree.len() { |
| 82 |
Err(DecodeFailed::InvalidPathLengths)?; |
| 83 |
} |
| 84 |
|
| 85 |
Ok(Some(Tree { |
| 86 |
path_lengths: self.path_lengths.clone(), |
| 87 |
largest_length, |
| 88 |
huffman_tree, |
| 89 |
})) |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
pub fn create_instance(&self) -> Result<Tree, DecodeFailed> { |
| 99 |
self.create_instance_allow_empty()? |
| 100 |
.ok_or(DecodeFailed::EmptyTree) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
pub fn update_range_with_pretree( |
| 105 |
&mut self, |
| 106 |
bitstream: &mut Bitstream, |
| 107 |
range: Range<usize>, |
| 108 |
) -> Result<(), DecodeFailed> { |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
let pretree = { |
| 117 |
let mut path_lengths = Vec::with_capacity(20); |
| 118 |
for _ in 0..20 { |
| 119 |
path_lengths.push(bitstream.read_bits(4)? as u8) |
| 120 |
} |
| 121 |
|
| 122 |
Tree::from_path_lengths(path_lengths)? |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
let mut i = range.start; |
| 127 |
while i < range.end { |
| 128 |
|
| 129 |
let code = pretree.decode_element(bitstream)?; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
match code { |
| 136 |
0..=16 => { |
| 137 |
self.path_lengths[i] = (17 + self.path_lengths[i] - code as u8) % 17; |
| 138 |
i += 1; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
17 => { |
| 143 |
let zeros = bitstream.read_bits(4)?; |
| 144 |
self.path_lengths |
| 145 |
.get_mut(i..i + zeros as usize + 4) |
| 146 |
.ok_or(DecodeFailed::InvalidPretreeRle)? |
| 147 |
.iter_mut() |
| 148 |
.for_each(|x| *x = 0); |
| 149 |
i += zeros as usize + 4; |
| 150 |
} |
| 151 |
18 => { |
| 152 |
let zeros = bitstream.read_bits(5)?; |
| 153 |
self.path_lengths |
| 154 |
.get_mut(i..i + zeros as usize + 20) |
| 155 |
.ok_or(DecodeFailed::InvalidPretreeRle)? |
| 156 |
.iter_mut() |
| 157 |
.for_each(|x| *x = 0); |
| 158 |
i += zeros as usize + 20; |
| 159 |
} |
| 160 |
19 => { |
| 161 |
let same = bitstream.read_bits(1)?; |
| 162 |
|
| 163 |
|
| 164 |
let code = pretree.decode_element(bitstream)?; |
| 165 |
if code > 16 { |
| 166 |
return Err(DecodeFailed::InvalidPretreeElement(code))?; |
| 167 |
} |
| 168 |
|
| 169 |
let value = (17 + self.path_lengths[i] - code as u8) % 17; |
| 170 |
self.path_lengths |
| 171 |
.get_mut(i..i + same as usize + 4) |
| 172 |
.ok_or(DecodeFailed::InvalidPretreeRle)? |
| 173 |
.iter_mut() |
| 174 |
.for_each(|x| *x = value); |
| 175 |
i += same as usize + 4; |
| 176 |
} |
| 177 |
_ => return Err(DecodeFailed::InvalidPretreeElement(code)), |
| 178 |
}; |
| 179 |
} |
| 180 |
|
| 181 |
Ok(()) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
impl Tree { |
| 186 |
|
| 187 |
pub fn from_path_lengths(path_lengths: Vec<u8>) -> Result<Self, DecodeFailed> { |
| 188 |
CanonicalTree { path_lengths }.create_instance() |
| 189 |
} |
| 190 |
|
| 191 |
pub fn decode_element(&self, bitstream: &mut Bitstream) -> Result<u16, DecodeFailed> { |
| 192 |
… |
| 193 |
let code = self.huffman_tree[bitstream.peek_bits(self.largest_length.get()) as usize]; |
| 194 |
|
| 195 |
). |
| 196 |
bitstream.read_bits(self.path_lengths[code as usize])?; |
| 197 |
|
| 198 |
Ok(code) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
impl fmt::Debug for Tree { |
| 203 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 204 |
f.debug_struct("Tree") |
| 205 |
.field("path_lengths", &self.path_lengths.len()) |
| 206 |
.field("largest_length", &self.largest_length) |
| 207 |
.finish() |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
#[cfg(test)] |
| 212 |
mod tests { |
| 213 |
use super::*; |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn decode_simple_table() { |
| 217 |
|
| 218 |
let tree = Tree::from_path_lengths(vec![6, 5, 1, 3, 4, 6, 2, 0]).unwrap(); |
| 219 |
let value_count = vec![(2, 32), (6, 16), (3, 8), (4, 4), (1, 2), (0, 1), (5, 1)]; |
| 220 |
|
| 221 |
let mut i = 0; |
| 222 |
for (value, count) in value_count.into_iter() { |
| 223 |
(0..count).for_each(|_| { |
| 224 |
assert_eq!(tree.huffman_tree[i], value); |
| 225 |
i += 1; |
| 226 |
}) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
fn decode_complex_table() { |
| 232 |
|
| 233 |
let tree = Tree::from_path_lengths(vec![ |
| 234 |
1, 0, 0, 0, 0, 7, 3, 3, 4, 4, 5, 5, 5, 7, 8, 8, 0, 7, 0, 0, |
| 235 |
]) |
| 236 |
.unwrap(); |
| 237 |
let value_count = vec![ |
| 238 |
(0, 128), |
| 239 |
(6, 32), |
| 240 |
(7, 32), |
| 241 |
(8, 16), |
| 242 |
(9, 16), |
| 243 |
(10, 8), |
| 244 |
(11, 8), |
| 245 |
(12, 8), |
| 246 |
(5, 2), |
| 247 |
(13, 2), |
| 248 |
(17, 2), |
| 249 |
(14, 1), |
| 250 |
(15, 1), |
| 251 |
]; |
| 252 |
|
| 253 |
let mut i = 0; |
| 254 |
for (value, count) in value_count.into_iter() { |
| 255 |
(0..count).for_each(|_| { |
| 256 |
assert_eq!(tree.huffman_tree[i], value); |
| 257 |
i += 1; |
| 258 |
}) |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
#[test] |
| 263 |
fn decode_elements() { |
| 264 |
let tree = Tree::from_path_lengths(vec![6, 5, 1, 3, 4, 6, 2, 0]).unwrap(); |
| 265 |
|
| 266 |
let buffer = [0x5b, 0xda, 0x3f, 0xf8]; |
| 267 |
let mut bitstream = Bitstream::new(&buffer); |
| 268 |
bitstream.read_bits(11).unwrap(); |
| 269 |
assert_eq!(tree.decode_element(&mut bitstream), Ok(3)); |
| 270 |
assert_eq!(tree.decode_element(&mut bitstream), Ok(5)); |
| 271 |
assert_eq!(tree.decode_element(&mut bitstream), Ok(6)); |
| 272 |
assert_eq!(tree.decode_element(&mut bitstream), Ok(2)); |
| 273 |
} |
| 274 |
} |