1 | use super::super::blocks::sequence_section::Sequence; |
2 | use super::decodebuffer::Decodebuffer; |
3 | use crate::decoding::dictionary::Dictionary; |
4 | use crate::fse::FSETable; |
5 | use crate::huff0::HuffmanTable; |
6 | use alloc::vec::Vec; |
7 | |
8 | pub struct DecoderScratch { |
9 | pub huf: HuffmanScratch, |
10 | pub fse: FSEScratch, |
11 | pub buffer: Decodebuffer, |
12 | pub offset_hist: [u32; 3], |
13 | |
14 | pub literals_buffer: Vec<u8>, |
15 | pub sequences: Vec<Sequence>, |
16 | pub block_content_buffer: Vec<u8>, |
17 | } |
18 | |
19 | impl DecoderScratch { |
20 | pub fn new(window_size: usize) -> DecoderScratch { |
21 | DecoderScratch { |
22 | huf: HuffmanScratch { |
23 | table: HuffmanTable::new(), |
24 | }, |
25 | fse: FSEScratch { |
26 | offsets: FSETable::new(), |
27 | of_rle: None, |
28 | literal_lengths: FSETable::new(), |
29 | ll_rle: None, |
30 | match_lengths: FSETable::new(), |
31 | ml_rle: None, |
32 | }, |
33 | buffer: Decodebuffer::new(window_size), |
34 | offset_hist: [1, 4, 8], |
35 | |
36 | block_content_buffer: Vec::new(), |
37 | literals_buffer: Vec::new(), |
38 | sequences: Vec::new(), |
39 | } |
40 | } |
41 | |
42 | pub fn reset(&mut self, window_size: usize) { |
43 | self.offset_hist = [1, 4, 8]; |
44 | self.literals_buffer.clear(); |
45 | self.sequences.clear(); |
46 | self.block_content_buffer.clear(); |
47 | |
48 | self.buffer.reset(window_size); |
49 | |
50 | self.fse.literal_lengths.reset(); |
51 | self.fse.match_lengths.reset(); |
52 | self.fse.offsets.reset(); |
53 | self.fse.ll_rle = None; |
54 | self.fse.ml_rle = None; |
55 | self.fse.of_rle = None; |
56 | |
57 | self.huf.table.reset(); |
58 | } |
59 | |
60 | pub fn init_from_dict(&mut self, dict: &Dictionary) { |
61 | self.fse.reinit_from(&dict.fse); |
62 | self.huf.table.reinit_from(&dict.huf.table); |
63 | self.offset_hist = dict.offset_hist; |
64 | self.buffer.dict_content.clear(); |
65 | self.buffer |
66 | .dict_content |
67 | .extend_from_slice(&dict.dict_content); |
68 | } |
69 | } |
70 | |
71 | pub struct HuffmanScratch { |
72 | pub table: HuffmanTable, |
73 | } |
74 | |
75 | impl HuffmanScratch { |
76 | pub fn new() -> HuffmanScratch { |
77 | HuffmanScratch { |
78 | table: HuffmanTable::new(), |
79 | } |
80 | } |
81 | } |
82 | |
83 | impl Default for HuffmanScratch { |
84 | fn default() -> Self { |
85 | Self::new() |
86 | } |
87 | } |
88 | |
89 | pub struct FSEScratch { |
90 | pub offsets: FSETable, |
91 | pub of_rle: Option<u8>, |
92 | pub literal_lengths: FSETable, |
93 | pub ll_rle: Option<u8>, |
94 | pub match_lengths: FSETable, |
95 | pub ml_rle: Option<u8>, |
96 | } |
97 | |
98 | impl FSEScratch { |
99 | pub fn new() -> FSEScratch { |
100 | FSEScratch { |
101 | offsets: FSETable::new(), |
102 | of_rle: None, |
103 | literal_lengths: FSETable::new(), |
104 | ll_rle: None, |
105 | match_lengths: FSETable::new(), |
106 | ml_rle: None, |
107 | } |
108 | } |
109 | |
110 | pub fn reinit_from(&mut self, other: &Self) { |
111 | self.offsets.reinit_from(&other.offsets); |
112 | self.literal_lengths.reinit_from(&other.literal_lengths); |
113 | self.match_lengths.reinit_from(&other.match_lengths); |
114 | self.of_rle = other.of_rle; |
115 | self.ll_rle = other.ll_rle; |
116 | self.ml_rle = other.ml_rle; |
117 | } |
118 | } |
119 | |
120 | impl Default for FSEScratch { |
121 | fn default() -> Self { |
122 | Self::new() |
123 | } |
124 | } |
125 | |