aboutsummaryrefslogtreecommitdiff
path: root/matroska/src
diff options
context:
space:
mode:
Diffstat (limited to 'matroska/src')
-rw-r--r--matroska/src/bin/mkvdump.rs7
-rw-r--r--matroska/src/block.rs64
2 files changed, 35 insertions, 36 deletions
diff --git a/matroska/src/bin/mkvdump.rs b/matroska/src/bin/mkvdump.rs
index 923e48f..48420c6 100644
--- a/matroska/src/bin/mkvdump.rs
+++ b/matroska/src/bin/mkvdump.rs
@@ -15,7 +15,12 @@ fn main() {
let (position, tag) = tag.unwrap();
match tag {
MatroskaTag::SimpleBlock(b) | MatroskaTag::Block(b) => {
- println!("block kf={} ts_off={}", b.keyframe, b.timestamp_off)
+ println!(
+ "block t={} kf={} ts_off={}",
+ b.track,
+ b.flags.keyframe(),
+ b.timestamp_off
+ )
}
_ => println!("{} {tag:?}", position.unwrap_or(0)),
}
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(())
}