aboutsummaryrefslogtreecommitdiff
path: root/matroska/src/block.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-04-19 14:40:34 +0200
committermetamuffin <metamuffin@disroot.org>2025-04-19 14:40:34 +0200
commite4d60fc1a59f1c747c81871118512ef543e48e05 (patch)
treeedae7c6308af119b06a4ee7c7b61820717cc24ce /matroska/src/block.rs
parent121722729caaacbbd430b0a58c302389575acd05 (diff)
downloadjellything-e4d60fc1a59f1c747c81871118512ef543e48e05.tar
jellything-e4d60fc1a59f1c747c81871118512ef543e48e05.tar.bz2
jellything-e4d60fc1a59f1c747c81871118512ef543e48e05.tar.zst
lazy block flags parsing
Diffstat (limited to 'matroska/src/block.rs')
-rw-r--r--matroska/src/block.rs64
1 files changed, 29 insertions, 35 deletions
diff --git a/matroska/src/block.rs b/matroska/src/block.rs
index 51bea84..54d9de5 100644
--- a/matroska/src/block.rs
+++ b/matroska/src/block.rs
@@ -12,6 +12,7 @@ use std::io::{Cursor, Write};
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum LacingType {
+ None,
Xiph,
FixedSize,
Ebml,
@@ -20,39 +21,46 @@ pub enum LacingType {
#[derive(Debug, PartialEq, Clone)]
pub struct Block {
pub track: u64,
- pub keyframe: bool,
- pub discardable: bool,
+ pub flags: Flags,
pub timestamp_off: i16,
- pub invisible: bool,
- pub lacing: Option<LacingType>,
pub data: Vec<u8>,
}
+#[derive(Debug, PartialEq, Clone)]
+pub struct Flags(u8);
+
+impl Flags {
+ pub fn keyframe(&self) -> bool {
+ self.0 & 0b10000000 != 0
+ }
+ pub fn lacing(&self) -> LacingType {
+ match self.0 & 0b00000110 {
+ 0b000 => LacingType::None,
+ 0b010 => LacingType::Xiph,
+ 0b100 => LacingType::FixedSize,
+ 0b110 => LacingType::Ebml,
+ _ => unreachable!(),
+ }
+ }
+ pub fn discardable(&self) -> bool {
+ self.0 & 0b00000001 != 0
+ }
+ pub fn invisible(&self) -> bool {
+ self.0 & 0b00001000 != 0
+ }
+}
+
impl ReadValue for Block {
fn from_buf(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 flags = 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,
+ flags,
timestamp_off,
})
}
@@ -63,21 +71,7 @@ impl WriteValue for Block {
write_vint(w, self.inner_len() as u64)?;
write_vint(w, self.track)?;
w.write_all(&self.timestamp_off.to_be_bytes())?;
- w.write_all(&[match self.discardable {
- true => 0b1,
- false => 0b0,
- } | match self.keyframe {
- 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,
- }])?;
+ w.write_all(&[self.flags.0])?;
w.write_all(&self.data)?;
Ok(())
}