#[derive(Clone, Debug, Default)]
pub struct BitWriter {
bytes: Vec<u8>,
cur: u8,
nbits: u8,
}
impl BitWriter {
pub fn new() -> Self {
Self::default()
}
pub fn with_capacity(bytes: usize) -> Self {
Self {
bytes: Vec::with_capacity(bytes),
cur: 0,
nbits: 0,
}
}
#[inline]
pub fn write_bit(&mut self, bit: u32) {
self.cur |= ((bit & 1) as u8) << (7 - self.nbits);
self.nbits += 1;
if self.nbits == 8 {
self.bytes.push(self.cur);
self.cur = 0;
self.nbits = 0;
}
}
#[inline]
pub fn write_bits(&mut self, value: u32, n: u32) {
debug_assert!(n <= 32);
for i in (0..n).rev() {
self.write_bit((value >> i) & 1);
}
}
pub fn write_bits64(&mut self, value: u64, n: u32) {
debug_assert!(n <= 64);
for i in (0..n).rev() {
self.write_bit(((value >> i) & 1) as u32);
}
}
#[inline]
pub fn flag(&mut self, b: bool) {
self.write_bit(b as u32);
}
#[inline]
pub fn write_byte(&mut self, b: u8) {
if self.nbits == 0 {
self.bytes.push(b);
} else {
self.write_bits(b as u32, 8);
}
}
pub fn write_ue(&mut self, value: u32) {
let v1 = value as u64 + 1;
let len = 64 - v1.leading_zeros(); for _ in 0..(len - 1) {
self.write_bit(0);
}
self.write_bits64(v1, len);
}
pub fn write_se(&mut self, value: i32) {
let code = if value <= 0 {
(-(value as i64) as u64) * 2
} else {
(value as u64) * 2 - 1
};
self.write_ue(code as u32);
}
pub fn bit_len(&self) -> usize {
self.bytes.len() * 8 + self.nbits as usize
}
pub fn is_byte_aligned(&self) -> bool {
self.nbits == 0
}
pub fn align_zero(&mut self) {
while self.nbits != 0 {
self.write_bit(0);
}
}
pub fn finish(mut self) -> Vec<u8> {
self.align_zero();
self.bytes
}
pub fn as_bytes(&self) -> &[u8] {
debug_assert!(
self.is_byte_aligned(),
"as_bytes on a non-byte-aligned writer"
);
&self.bytes
}
}
pub fn rbsp_trailing_bits(w: &mut BitWriter) {
w.write_bit(1);
w.align_zero();
}
pub fn emulation_prevention(rbsp: &[u8]) -> Vec<u8> {
let mut out = Vec::with_capacity(rbsp.len() + rbsp.len() / 128 + 4);
let mut zeros = 0u32;
for &b in rbsp {
if zeros >= 2 && b <= 0x03 {
out.push(0x03);
zeros = 0;
}
out.push(b);
if b == 0 {
zeros += 1;
} else {
zeros = 0;
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn bits_of(w: &BitWriter) -> String {
let mut s = String::new();
let full = w.bytes.len();
for &byte in &w.bytes {
for i in (0..8).rev() {
s.push(if (byte >> i) & 1 == 1 { '1' } else { '0' });
}
}
let _ = full;
for i in (0..w.nbits).rev() {
s.push(if (w.cur >> (7 - (w.nbits - 1 - i))) & 1 == 1 {
'1'
} else {
'0'
});
}
s
}
#[test]
fn ue_known_vectors() {
let cases: &[(u32, &str)] = &[
(0, "1"),
(1, "010"),
(2, "011"),
(3, "00100"),
(4, "00101"),
(5, "00110"),
(6, "00111"),
(7, "0001000"),
(8, "0001001"),
];
for &(v, expect) in cases {
let mut w = BitWriter::new();
w.write_ue(v);
assert_eq!(bits_of(&w), expect, "ue({v})");
}
}
#[test]
fn se_known_vectors() {
let cases: &[(i32, &str)] = &[
(0, "1"),
(1, "010"),
(-1, "011"),
(2, "00100"),
(-2, "00101"),
(3, "00110"),
(-3, "00111"),
];
for &(v, expect) in cases {
let mut w = BitWriter::new();
w.write_se(v);
assert_eq!(bits_of(&w), expect, "se({v})");
}
}
#[test]
fn write_bits_msb_first() {
let mut w = BitWriter::new();
w.write_bits(0b101, 3);
w.write_bits(0b1, 1);
w.write_bits(0b0000, 4);
assert_eq!(w.finish(), vec![0b1011_0000]);
}
#[test]
fn rbsp_trailing_aligns_with_stop_bit() {
let mut w = BitWriter::new();
w.write_bits(0b101, 3); rbsp_trailing_bits(&mut w);
assert_eq!(w.finish(), vec![0b1011_0000]);
}
#[test]
fn emulation_prevention_inserts_03() {
assert_eq!(
emulation_prevention(&[0x00, 0x00, 0x00]),
vec![0x00, 0x00, 0x03, 0x00]
);
assert_eq!(
emulation_prevention(&[0x00, 0x00, 0x01]),
vec![0x00, 0x00, 0x03, 0x01]
);
assert_eq!(
emulation_prevention(&[0x00, 0x00, 0x02]),
vec![0x00, 0x00, 0x03, 0x02]
);
assert_eq!(
emulation_prevention(&[0x00, 0x00, 0x03]),
vec![0x00, 0x00, 0x03, 0x03]
);
assert_eq!(
emulation_prevention(&[0x00, 0x00, 0x04]),
vec![0x00, 0x00, 0x04]
);
assert_eq!(
emulation_prevention(&[0x12, 0x34, 0x56]),
vec![0x12, 0x34, 0x56]
);
assert_eq!(
emulation_prevention(&[0x00, 0x00, 0x00, 0x00]),
vec![0x00, 0x00, 0x03, 0x00, 0x00]
);
}
}