aboutsummaryrefslogtreecommitdiff
path: root/matroska/src/block.rs
blob: c887a912325933f492352960959c0e0b77faf255 (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
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
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2023 metamuffin <metamuffin.org>
*/
use crate::{read::ReadExt, write::write_vint};
use anyhow::Result;
use std::io::Cursor;

pub enum LacingType {
    Xiph,
    FixedSize,
    Ebml,
}

pub struct Block {
    pub track: u64,
    pub keyframe: bool,
    pub discardable: bool,
    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 & 0b1000) == 0b1000;
        let lacing = match flags & 0b110 {
            0b000 => None,
            0b010 => Some(LacingType::Xiph),
            0b100 => Some(LacingType::FixedSize),
            0b110 => Some(LacingType::Ebml),
            _ => unreachable!(),
        };
        let keyframe = (flags & 0b10000000) == 0b10000000;
        let discardable = (flags & 0b1) == 0b1;

        Ok(Self {
            keyframe,
            discardable,
            track,
            data,
            invisible,
            lacing,
            timestamp_off,
        })
    }
    pub fn dump(&self) -> Vec<u8> {
        let mut out = vec![];
        write_vint(&mut out, self.track).unwrap();
        out.extend(self.timestamp_off.to_be_bytes().into_iter());
        out.push(
            match self.discardable {
                true => 0b1,
                false => 0b0,
            } | match self.invisible {
                true => 0b10000000,
                false => 0b00000000,
            } | match self.invisible {
                true => 0b1000,
                false => 0b0000,
            } | match self.lacing {
                Some(LacingType::Xiph) => 0b010,
                Some(LacingType::Ebml) => 0b100,
                Some(LacingType::FixedSize) => 0b110,
                None => 0b0000,
            },
        );
        out.extend(self.data.iter());
        out
    }
}