diff options
Diffstat (limited to 'matroska')
-rw-r--r-- | matroska/src/bin/mkvdump.rs | 3 | ||||
-rw-r--r-- | matroska/src/block.rs | 71 | ||||
-rw-r--r-- | matroska/src/lib.rs | 1 | ||||
-rw-r--r-- | matroska/src/matroska.rs | 4 |
4 files changed, 48 insertions, 31 deletions
diff --git a/matroska/src/bin/mkvdump.rs b/matroska/src/bin/mkvdump.rs index 27b4849..b58adcc 100644 --- a/matroska/src/bin/mkvdump.rs +++ b/matroska/src/bin/mkvdump.rs @@ -3,7 +3,7 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2024 metamuffin <metamuffin.org> */ -use jellymatroska::{block::Block, matroska::MatroskaTag, read::EbmlReader}; +use jellymatroska::{matroska::MatroskaTag, read::EbmlReader}; use std::{fs::File, io::BufReader}; fn main() { @@ -15,7 +15,6 @@ fn main() { let (position, tag) = tag.unwrap(); match tag { MatroskaTag::SimpleBlock(b) | MatroskaTag::Block(b) => { - let b = Block::parse(&b).unwrap(); println!("block kf={} ts_off={}", b.keyframe, b.timestamp_off) } _ => println!("{} {tag:?}", position.unwrap_or(0)), diff --git a/matroska/src/block.rs b/matroska/src/block.rs index 5ab398b..1ab8ceb 100644 --- a/matroska/src/block.rs +++ b/matroska/src/block.rs @@ -3,16 +3,19 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2024 metamuffin <metamuffin.org> */ +use crate::write::vint_length; use crate::{read::ReadExt, write::write_vint}; -use crate::Result; -use std::io::Cursor; +use crate::{ReadValue, Result, WriteValue}; +use std::io::{Cursor, Write}; +#[derive(Debug, PartialEq, Clone, Copy)] pub enum LacingType { Xiph, FixedSize, Ebml, } +#[derive(Debug, PartialEq, Clone)] pub struct Block { pub track: u64, pub keyframe: bool, @@ -23,8 +26,8 @@ pub struct Block { pub data: Vec<u8>, } -impl Block { - pub fn parse(buf: &[u8]) -> Result<Self> { +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]; @@ -51,28 +54,42 @@ impl Block { 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.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, - }, - ); - out.extend(self.data.iter()); - out +} + +impl WriteValue for Block { + fn write_to(&self, w: &mut impl Write) -> Result<()> { + 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.data)?; + Ok(()) + } + fn size(&self) -> usize { + let il = self.inner_len(); + vint_length(il as u64) + il + } +} + +impl Block { + fn inner_len(&self) -> usize { + vint_length(self.track) + + 2 // timestamp + + 1 // flags + + self.data.len() } } diff --git a/matroska/src/lib.rs b/matroska/src/lib.rs index 09635d4..7e4121b 100644 --- a/matroska/src/lib.rs +++ b/matroska/src/lib.rs @@ -22,5 +22,6 @@ pub enum Master { End, } +pub(crate) use block::Block; pub(crate) use error::Error; pub(crate) type Result<T> = core::result::Result<T, Error>; diff --git a/matroska/src/matroska.rs b/matroska/src/matroska.rs index 6bbc5f7..937fca7 100644 --- a/matroska/src/matroska.rs +++ b/matroska/src/matroska.rs @@ -76,7 +76,7 @@ define_ebml! { Cluster[0x1F43B675]: { BlockGroup[0xA0]: { - Block[0xA1]: Binary, + Block[0xA1]: Block, BlockAdditions[0x75A1]: { BlockMore[0xA6]: { BlockAddID[0xEE]: Uint, @@ -110,7 +110,7 @@ define_ebml! { SilentTracks[0x5854]: { SilentTrackNumber[0x58D7]: Uint, }, - SimpleBlock[0xA3]: Binary, + SimpleBlock[0xA3]: Block, Timestamp[0xE7]: Uint, }, |