aboutsummaryrefslogtreecommitdiff
path: root/matroska/src/block.rs
blob: b0d6f6b878f68f968907382b456c012cf81bc74a (plain)
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
use crate::read::ReadExt;
use anyhow::Result;
use std::io::Cursor;

pub enum LacingType {
    FixedSize,
    Ebml,
    Xiph,
}

pub struct Block {
    pub track: u64,
    pub timestamp_off: i16,
    pub invisible: bool,
    pub lacing: Option<LacingType>,
    pub data: Vec<u8>,
}

impl Block {
    pub fn parse(buf: &[u8]) -> Result<Self> {
        let (track, c) = Cursor::new(buf).read_vint_len()?;
        let timestamp_off = i16::from_be_bytes(buf[c..c + 2].try_into().unwrap());
        let flags = buf[c + 2];
        let data = Vec::from(&buf[c + 3..]);

        let invisible = (flags & 0b10000) == 0b10000;
        let lacing = match flags & 0b1100 {
            0b0000 => None,
            0b0100 => Some(LacingType::Xiph),
            0b1000 => Some(LacingType::Ebml),
            0b1100 => Some(LacingType::FixedSize),
            _ => unreachable!(),
        };

        Ok(Self {
            track,
            data,
            invisible,
            lacing,
            timestamp_off,
        })
    }
}