transmux 0.30.0

Any-to-any media container muxing hub: demux TS, fMP4/CMAF, MPEG-PS, WebM, FLV, or RTMP into one neutral IR and mux to CMAF/fMP4, progressive MP4, TS, DASH, low-latency DASH, HLS, low-latency HLS, Smooth Streaming, or RTMP. CENC/CBCS encrypt+decrypt, SSAI splice, RTP/RTCP, and an fMP4/CMAF conformance validator; parses codec config headers only, samples stay opaque. no_std + alloc, except the XML manifest paths (DASH/Smooth/PlayReady), which need the std feature.
Documentation
//! MSB-first bit writer for the in-crate tests that hand-build H.264/HEVC
//! RBSP bytes and SEI payloads: fixed-width `u`, Exp-Golomb `ue`/`se`
//! (ITU-T H.264 §9.1), and RBSP trailing bits (§7.3.2.11).

use alloc::vec::Vec;

#[derive(Default)]
pub(crate) struct BitWriter {
    out: Vec<u8>,
    cur: u8,
    n: u8,
}

impl BitWriter {
    pub(crate) fn new() -> Self {
        Self::default()
    }

    pub(crate) fn bit(&mut self, b: bool) -> &mut Self {
        self.cur = (self.cur << 1) | u8::from(b);
        self.n += 1;
        if self.n == 8 {
            self.out.push(self.cur);
            self.cur = 0;
            self.n = 0;
        }
        self
    }

    /// `bits` bits of `v`, most significant first.
    ///
    /// # Panics
    /// If `v` does not fit in `bits` bits.
    pub(crate) fn u(&mut self, bits: u32, v: u64) -> &mut Self {
        assert!(bits == 64 || v >> bits == 0, "{v} wider than {bits} bits");
        for i in (0..bits).rev() {
            self.bit((v >> i) & 1 == 1);
        }
        self
    }

    pub(crate) fn ue(&mut self, v: u64) -> &mut Self {
        let code = v + 1;
        let len = 64 - code.leading_zeros();
        self.u(len - 1, 0);
        self.u(len, code)
    }

    pub(crate) fn se(&mut self, v: i64) -> &mut Self {
        self.ue(if v > 0 {
            (2 * v - 1).cast_unsigned()
        } else {
            (-2 * v).cast_unsigned()
        })
    }

    pub(crate) fn align_zero(&mut self) -> &mut Self {
        while self.n != 0 {
            self.bit(false);
        }
        self
    }

    /// The bytes so far, the last partial byte zero-padded.
    pub(crate) fn finish(mut self) -> Vec<u8> {
        self.align_zero();
        self.out
    }

    /// `rbsp_trailing_bits()`: a one bit, then zeros to the byte boundary.
    pub(crate) fn trailing(mut self) -> Vec<u8> {
        self.bit(true);
        self.finish()
    }
}