1 //! Bitstream writer functionality.
3 //! Bitstream writer works on `Vec<u8>` and allows to write bits in different modes.
7 //! Writing 17-bit value:
9 //! use nihav_core::io::bitwriter::{BitWriter,BitWriterMode};
11 //! # fn foo() -> Vec<u8> {
12 //! let mut bw = BitWriter::new(Vec::new(), BitWriterMode::BE);
18 /// Bitstream writing modes.
19 #[derive(Debug,Clone,Copy,PartialEq)]
20 pub enum BitWriterMode {
21 /// The stream is big endian MSB first.
23 /// The stream is little endian LSB first.
25 /// The stream is packed into 16-bit little-endian words MSB first.
27 /// The stream is packed into 32-bit little-endian words MSB first.
32 fn is_be(self) -> bool { self != BitWriterMode::LE }
36 pub struct BitWriter {
45 /// Creates a new instance of `BitWriter` that will append data to the input vector.
50 /// use nihav_core::io::bitwriter::{BitWriter,BitWriterMode};
52 /// let mut bw = BitWriter::new(Vec::with_capacity(100), BitWriterMode::BE);
54 pub fn new(dst: Vec<u8>, mode: BitWriterMode) -> Self {
55 let start = dst.len();
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);
73 self.bitbuf |= (bit as u32) << self.bits;
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) {
85 if self.mode.is_be() {
86 if self.bits + bits <= 32 {
87 self.bitbuf |= val << (32 - self.bits - bits);
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);
97 if self.bits + bits <= 32 {
98 self.bitbuf |= val << self.bits;
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);
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);
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)
117 fn flush(&mut self) {
119 BitWriterMode::BE => {
120 while self.bits >= 8 {
121 self.dst.push((self.bitbuf >> 24) as u8);
126 BitWriterMode::LE => {
127 while self.bits >= 8 {
128 self.dst.push(self.bitbuf as u8);
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);
141 BitWriterMode::LE32MSB => {
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);
153 /// Finalises operations and returns the vector containing output data.
154 pub fn end(mut self) -> Vec<u8> {
158 BitWriterMode::BE => {
159 self.dst.push((self.bitbuf >> 24) as u8);
161 BitWriterMode::LE => {
162 self.dst.push(self.bitbuf as u8);
164 BitWriterMode::LE16MSB => {
165 self.dst.push((self.bitbuf >> 16) as u8);
166 self.dst.push((self.bitbuf >> 24) as u8);
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);
186 let mut bw = BitWriter::new(Vec::new(), BitWriterMode::BE);
189 assert_eq!(&data, &[21, 128]);
191 let mut bw = BitWriter::new(Vec::new(), BitWriterMode::LE);
195 assert_eq!(&data, &[87, 0]);
197 let mut bw = BitWriter::new(Vec::new(), BitWriterMode::LE32MSB);
201 assert_eq!(&data, &[0, 128, 10, 21]);