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 pub fn write(&mut self, val: u32, bits: u8) {
84 if self.mode.is_be() {
85 if self.bits + bits <= 32 {
86 self.bitbuf |= val << (32 - self.bits - bits);
90 let cbits = 32 - self.bits;
91 let bits2 = bits - cbits;
92 self.write(val >> bits2, cbits);
93 self.write(val & ((1 << bits2) - 1), bits2);
96 if self.bits + bits <= 32 {
97 self.bitbuf |= val << self.bits;
101 let cbits = 32 - self.bits;
102 let bits2 = bits - cbits;
103 self.write(val & ((1 << cbits) - 1), cbits);
104 self.write(val >> cbits, bits2);
108 /// Writes `bits` bits of signed `val` value to the output.
109 pub fn write_s(&mut self, val: i32, bits: u8) {
110 self.write((val as u32) & ((1 << bits) - 1), bits);
112 /// Tells the amount of bits written so far.
113 pub fn tell(&self) -> usize {
114 (self.dst.len() - self.start) * 8 + (self.bits as usize)
116 fn flush(&mut self) {
118 BitWriterMode::BE => {
119 while self.bits >= 8 {
120 self.dst.push((self.bitbuf >> 24) as u8);
125 BitWriterMode::LE => {
126 while self.bits >= 8 {
127 self.dst.push(self.bitbuf as u8);
132 BitWriterMode::LE16MSB => {
133 while self.bits >= 16 {
134 self.dst.push((self.bitbuf >> 16) as u8);
135 self.dst.push((self.bitbuf >> 24) as u8);
140 BitWriterMode::LE32MSB => {
142 self.dst.push( self.bitbuf as u8);
143 self.dst.push((self.bitbuf >> 8) as u8);
144 self.dst.push((self.bitbuf >> 16) as u8);
145 self.dst.push((self.bitbuf >> 24) as u8);
152 /// Finalises operations and returns the vector containing output data.
153 pub fn end(mut self) -> Vec<u8> {
157 BitWriterMode::BE => {
158 self.dst.push((self.bitbuf >> 24) as u8);
160 BitWriterMode::LE => {
161 self.dst.push(self.bitbuf as u8);
163 BitWriterMode::LE16MSB => {
164 self.dst.push((self.bitbuf >> 16) as u8);
165 self.dst.push((self.bitbuf >> 24) as u8);
167 BitWriterMode::LE32MSB => {
168 self.dst.push( self.bitbuf as u8);
169 self.dst.push((self.bitbuf >> 8) as u8);
170 self.dst.push((self.bitbuf >> 16) as u8);
171 self.dst.push((self.bitbuf >> 24) as u8);
185 let mut bw = BitWriter::new(Vec::new(), BitWriterMode::BE);
188 assert_eq!(&data, &[21, 128]);
190 let mut bw = BitWriter::new(Vec::new(), BitWriterMode::LE);
194 assert_eq!(&data, &[87, 0]);
196 let mut bw = BitWriter::new(Vec::new(), BitWriterMode::LE32MSB);
200 assert_eq!(&data, &[0, 128, 10, 21]);