fix clippy warnings
[nihav.git] / nihav-core / src / io / bitwriter.rs
1 //! Bitstream writer functionality.
2 //!
3 //! Bitstream writer works on `Vec<u8>` and allows to write bits in different modes.
4 //!
5 //! # Examples
6 //!
7 //! Writing 17-bit value:
8 //! ```
9 //! use nihav_core::io::bitwriter::{BitWriter,BitWriterMode};
10 //!
11 //! # fn foo() -> Vec<u8> {
12 //! let mut bw = BitWriter::new(Vec::new(), BitWriterMode::BE);
13 //! bw.write(42, 17);
14 //! # bw.end()
15 //! # }
16 //! ```
17
18 /// Bitstream writing modes.
19 #[derive(Debug,Clone,Copy,PartialEq)]
20 pub enum BitWriterMode {
21 /// The stream is big endian MSB first.
22 BE,
23 /// The stream is little endian LSB first.
24 LE,
25 /// The stream is packed into 16-bit little-endian words MSB first.
26 LE16MSB,
27 /// The stream is packed into 32-bit little-endian words MSB first.
28 LE32MSB,
29 }
30
31 impl BitWriterMode {
32 fn is_be(self) -> bool { self != BitWriterMode::LE }
33 }
34
35 /// Bitstream writer.
36 pub struct BitWriter {
37 dst: Vec<u8>,
38 bitbuf: u32,
39 bits: u8,
40 start: usize,
41 mode: BitWriterMode,
42 }
43
44 impl BitWriter {
45 /// Creates a new instance of `BitWriter` that will append data to the input vector.
46 ///
47 /// # Examples
48 ///
49 /// ```
50 /// use nihav_core::io::bitwriter::{BitWriter,BitWriterMode};
51 ///
52 /// let mut bw = BitWriter::new(Vec::with_capacity(100), BitWriterMode::BE);
53 /// ```
54 pub fn new(dst: Vec<u8>, mode: BitWriterMode) -> Self {
55 let start = dst.len();
56 Self {
57 dst,
58 mode,
59 start,
60 bitbuf: 0,
61 bits: 0,
62 }
63 }
64 /// Writes single zero bit to the output.
65 pub fn write0(&mut self) { self.write_bit(false); }
66 /// Writes single set bit to the output.
67 pub fn write1(&mut self) { self.write_bit(true); }
68 /// Writes single bit.
69 pub fn write_bit(&mut self, bit: bool) {
70 if self.mode.is_be() {
71 self.bitbuf |= (bit as u32) << (31 - self.bits);
72 } else {
73 self.bitbuf |= (bit as u32) << self.bits;
74 }
75 self.bits += 1;
76 self.flush();
77 }
78 /// Writes `bits` bits of `val` value to the output.
79 #[allow(clippy::collapsible_if)]
80 #[allow(clippy::collapsible_else_if)]
81 pub fn write(&mut self, val: u32, bits: u8) {
82 if bits == 0 {
83 return;
84 }
85 if self.mode.is_be() {
86 if self.bits + bits <= 32 {
87 self.bitbuf |= val << (32 - self.bits - bits);
88 self.bits += bits;
89 self.flush();
90 } else {
91 let cbits = 32 - self.bits;
92 let bits2 = bits - cbits;
93 self.write(val >> bits2, cbits);
94 self.write(val & ((1 << bits2) - 1), bits2);
95 }
96 } else {
97 if self.bits + bits <= 32 {
98 self.bitbuf |= val << self.bits;
99 self.bits += bits;
100 self.flush();
101 } else {
102 let cbits = 32 - self.bits;
103 let bits2 = bits - cbits;
104 self.write(val & ((1 << cbits) - 1), cbits);
105 self.write(val >> cbits, bits2);
106 }
107 }
108 }
109 /// Writes `bits` bits of signed `val` value to the output.
110 pub fn write_s(&mut self, val: i32, bits: u8) {
111 self.write((val as u32) & ((1 << bits) - 1), bits);
112 }
113 /// Tells the amount of bits written so far.
114 pub fn tell(&self) -> usize {
115 (self.dst.len() - self.start) * 8 + (self.bits as usize)
116 }
117 fn flush(&mut self) {
118 match self.mode {
119 BitWriterMode::BE => {
120 while self.bits >= 8 {
121 self.dst.push((self.bitbuf >> 24) as u8);
122 self.bitbuf <<= 8;
123 self.bits -= 8;
124 }
125 },
126 BitWriterMode::LE => {
127 while self.bits >= 8 {
128 self.dst.push(self.bitbuf as u8);
129 self.bitbuf >>= 8;
130 self.bits -= 8;
131 }
132 },
133 BitWriterMode::LE16MSB => {
134 while self.bits >= 16 {
135 self.dst.push((self.bitbuf >> 16) as u8);
136 self.dst.push((self.bitbuf >> 24) as u8);
137 self.bitbuf <<= 16;
138 self.bits -= 16;
139 }
140 },
141 BitWriterMode::LE32MSB => {
142 if self.bits == 32 {
143 self.dst.push( self.bitbuf as u8);
144 self.dst.push((self.bitbuf >> 8) as u8);
145 self.dst.push((self.bitbuf >> 16) as u8);
146 self.dst.push((self.bitbuf >> 24) as u8);
147 self.bitbuf = 0;
148 self.bits = 0;
149 }
150 },
151 };
152 }
153 /// Finalises operations and returns the vector containing output data.
154 pub fn end(mut self) -> Vec<u8> {
155 self.flush();
156 if self.bits > 0 {
157 match self.mode {
158 BitWriterMode::BE => {
159 self.dst.push((self.bitbuf >> 24) as u8);
160 },
161 BitWriterMode::LE => {
162 self.dst.push(self.bitbuf as u8);
163 },
164 BitWriterMode::LE16MSB => {
165 self.dst.push((self.bitbuf >> 16) as u8);
166 self.dst.push((self.bitbuf >> 24) as u8);
167 },
168 BitWriterMode::LE32MSB => {
169 self.dst.push( self.bitbuf as u8);
170 self.dst.push((self.bitbuf >> 8) as u8);
171 self.dst.push((self.bitbuf >> 16) as u8);
172 self.dst.push((self.bitbuf >> 24) as u8);
173 },
174 };
175 }
176 self.dst
177 }
178 }
179
180 #[cfg(test)]
181 mod test {
182 use super::*;
183
184 #[test]
185 fn bw_works() {
186 let mut bw = BitWriter::new(Vec::new(), BitWriterMode::BE);
187 bw.write(43, 9);
188 let data = bw.end();
189 assert_eq!(&data, &[21, 128]);
190
191 let mut bw = BitWriter::new(Vec::new(), BitWriterMode::LE);
192 bw.write1();
193 bw.write(43, 9);
194 let data = bw.end();
195 assert_eq!(&data, &[87, 0]);
196
197 let mut bw = BitWriter::new(Vec::new(), BitWriterMode::LE32MSB);
198 bw.write(42, 9);
199 bw.write(42, 9);
200 let data = bw.end();
201 assert_eq!(&data, &[0, 128, 10, 21]);
202 }
203 }