aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-core/src/io/bitwriter.rs
blob: 4f3c39c25d5e618dbc41c54a3a25f703044507e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! Bitstream writer functionality.
//!
//! Bitstream writer works on `Vec<u8>` and allows to write bits in different modes.
//!
//! # Examples
//!
//! Writing 17-bit value:
//! ```
//! use nihav_core::io::bitwriter::{BitWriter,BitWriterMode};
//!
//! # fn foo() -> Vec<u8> {
//! let mut bw = BitWriter::new(Vec::new(), BitWriterMode::BE);
//! bw.write(42, 17);
//! # bw.end()
//! # }
//! ```

/// Bitstream writing modes.
#[derive(Debug,Clone,Copy,PartialEq)]
pub enum BitWriterMode {
    /// The stream is big endian MSB first.
    BE,
    /// The stream is little endian LSB first.
    LE,
    /// The stream is packed into 16-bit little-endian words MSB first.
    LE16MSB,
    /// The stream is packed into 32-bit little-endian words MSB first.
    LE32MSB,
}

impl BitWriterMode {
    fn is_be(self) -> bool { self != BitWriterMode::LE }
}

/// Bitstream writer.
pub struct BitWriter {
    dst:    Vec<u8>,
    bitbuf: u32,
    bits:   u8,
    start:  usize,
    mode:   BitWriterMode,
}

impl BitWriter {
    /// Creates a new instance of `BitWriter` that will append data to the input vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use nihav_core::io::bitwriter::{BitWriter,BitWriterMode};
    ///
    /// let mut bw = BitWriter::new(Vec::with_capacity(100), BitWriterMode::BE);
    /// ```
    pub fn new(dst: Vec<u8>, mode: BitWriterMode) -> Self {
        let start = dst.len();
        Self {
            dst,
            mode,
            start,
            bitbuf: 0,
            bits:   0,
        }
    }
    /// Writes single zero bit to the output.
    pub fn write0(&mut self) { self.write_bit(false); }
    /// Writes single set bit to the output.
    pub fn write1(&mut self) { self.write_bit(true); }
    /// Writes single bit.
    pub fn write_bit(&mut self, bit: bool) {
        if self.mode.is_be() {
            self.bitbuf |= (bit as u32) << (31 - self.bits);
        } else {
            self.bitbuf |= (bit as u32) << self.bits;
        }
        self.bits += 1;
        self.flush();
    }
    /// Writes `bits` bits of `val` value to the output.
    #[allow(clippy::collapsible_if)]
    #[allow(clippy::collapsible_else_if)]
    pub fn write(&mut self, val: u32, bits: u8) {
        if bits == 0 {
            return;
        }
        if self.mode.is_be() {
            if self.bits + bits <= 32 {
                self.bitbuf |= val << (32 - self.bits - bits);
                self.bits += bits;
                self.flush();
            } else {
                let cbits = 32 - self.bits;
                let bits2 = bits - cbits;
                self.write(val >> bits2, cbits);
                self.write(val & ((1 << bits2) - 1), bits2);
            }
        } else {
            if self.bits + bits <= 32 {
                self.bitbuf |= val << self.bits;
                self.bits += bits;
                self.flush();
            } else {
                let cbits = 32 - self.bits;
                let bits2 = bits - cbits;
                self.write(val & ((1 << cbits) - 1), cbits);
                self.write(val >> cbits, bits2);
            }
        }
    }
    /// Writes `bits` bits of signed `val` value to the output.
    pub fn write_s(&mut self, val: i32, bits: u8) {
        self.write((val as u32) & ((1 << bits) - 1), bits);
    }
    /// Tells the amount of bits written so far.
    pub fn tell(&self) -> usize {
        (self.dst.len() - self.start) * 8 + (self.bits as usize)
    }
    fn flush(&mut self) {
        match self.mode {
            BitWriterMode::BE => {
                while self.bits >= 8 {
                    self.dst.push((self.bitbuf >> 24) as u8);
                    self.bitbuf <<= 8;
                    self.bits    -= 8;
                }
            },
            BitWriterMode::LE => {
                while self.bits >= 8 {
                    self.dst.push(self.bitbuf as u8);
                    self.bitbuf >>= 8;
                    self.bits    -= 8;
                }
            },
            BitWriterMode::LE16MSB => {
                while self.bits >= 16 {
                    self.dst.push((self.bitbuf >> 16) as u8);
                    self.dst.push((self.bitbuf >> 24) as u8);
                    self.bitbuf <<= 16;
                    self.bits    -= 16;
                }
            },
            BitWriterMode::LE32MSB => {
                if self.bits == 32 {
                    self.dst.push( self.bitbuf        as u8);
                    self.dst.push((self.bitbuf >>  8) as u8);
                    self.dst.push((self.bitbuf >> 16) as u8);
                    self.dst.push((self.bitbuf >> 24) as u8);
                    self.bitbuf = 0;
                    self.bits   = 0;
                }
            },
        };
    }
    /// Finalises operations and returns the vector containing output data.
    pub fn end(mut self) -> Vec<u8> {
        self.flush();
        if self.bits > 0 {
            match self.mode {
                BitWriterMode::BE => {
                    self.dst.push((self.bitbuf >> 24) as u8);
                },
                BitWriterMode::LE => {
                    self.dst.push(self.bitbuf as u8);
                },
                BitWriterMode::LE16MSB => {
                    self.dst.push((self.bitbuf >> 16) as u8);
                    self.dst.push((self.bitbuf >> 24) as u8);
                },
                BitWriterMode::LE32MSB => {
                    self.dst.push( self.bitbuf        as u8);
                    self.dst.push((self.bitbuf >>  8) as u8);
                    self.dst.push((self.bitbuf >> 16) as u8);
                    self.dst.push((self.bitbuf >> 24) as u8);
                },
            };
        }
        self.dst
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn bw_works() {
        let mut bw = BitWriter::new(Vec::new(), BitWriterMode::BE);
        bw.write(43, 9);
        let data = bw.end();
        assert_eq!(&data, &[21, 128]);

        let mut bw = BitWriter::new(Vec::new(), BitWriterMode::LE);
        bw.write1();
        bw.write(43, 9);
        let data = bw.end();
        assert_eq!(&data, &[87, 0]);

        let mut bw = BitWriter::new(Vec::new(), BitWriterMode::LE32MSB);
        bw.write(42, 9);
        bw.write(42, 9);
        let data = bw.end();
        assert_eq!(&data, &[0, 128, 10, 21]);
    }
}