aboutsummaryrefslogtreecommitdiff
path: root/matroska/src
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-01-25 07:42:27 +0100
committermetamuffin <metamuffin@disroot.org>2023-01-25 07:42:27 +0100
commit814896238c9b3928709f27606816ab6de60abdf3 (patch)
tree8134ed5213cf41f907f2af68ad9c8df245a937bd /matroska/src
parent4529d07cc3f2f86a9dbb0d4802875a81d5c4c495 (diff)
downloadjellything-814896238c9b3928709f27606816ab6de60abdf3.tar
jellything-814896238c9b3928709f27606816ab6de60abdf3.tar.bz2
jellything-814896238c9b3928709f27606816ab6de60abdf3.tar.zst
generate seek index
Diffstat (limited to 'matroska/src')
-rw-r--r--matroska/src/bin/experiment.rs2
-rw-r--r--matroska/src/bin/mkvdump.rs2
-rw-r--r--matroska/src/read.rs8
-rw-r--r--matroska/src/unflatten.rs16
-rw-r--r--matroska/src/write.rs28
5 files changed, 49 insertions, 7 deletions
diff --git a/matroska/src/bin/experiment.rs b/matroska/src/bin/experiment.rs
index 9bc667c..7787f4f 100644
--- a/matroska/src/bin/experiment.rs
+++ b/matroska/src/bin/experiment.rs
@@ -3,7 +3,7 @@
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2023 metamuffin <metamuffin.org>
*/
-use jellymatroska::{matroska::MatroskaTag, read::EbmlReader, write::EbmlWriter};
+use jellymatroska::{matroska::MatroskaTag, read::EbmlReader, write::EbmlWriter, unflatten::IterWithPos};
use std::{
fs::File,
io::{stdout, BufReader, BufWriter},
diff --git a/matroska/src/bin/mkvdump.rs b/matroska/src/bin/mkvdump.rs
index 6f2e063..10c697a 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) 2023 metamuffin <metamuffin.org>
*/
-use jellymatroska::{matroska::MatroskaTag, read::EbmlReader};
+use jellymatroska::{matroska::MatroskaTag, read::EbmlReader, unflatten::IterWithPos};
use std::{fs::File, io::BufReader};
fn main() -> anyhow::Result<()> {
diff --git a/matroska/src/read.rs b/matroska/src/read.rs
index 1211351..c99f304 100644
--- a/matroska/src/read.rs
+++ b/matroska/src/read.rs
@@ -3,7 +3,7 @@
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2023 metamuffin <metamuffin.org>
*/
-use crate::{matroska::MatroskaTag, size::EbmlSize, Master};
+use crate::{matroska::MatroskaTag, size::EbmlSize, unflatten::IterWithPos, Master};
use anyhow::{anyhow, bail, Result};
use log::{debug, warn};
use std::{
@@ -159,9 +159,13 @@ impl EbmlReader {
}
}
-impl Iterator for EbmlReader {
+impl IterWithPos for EbmlReader {
type Item = Result<MatroskaTag>;
+ fn position(&self) -> usize {
+ self.position
+ }
+
fn next(&mut self) -> Option<Self::Item> {
if let Some(t) = self.queue.pop_front() {
// match t {
diff --git a/matroska/src/unflatten.rs b/matroska/src/unflatten.rs
index 5e0ba31..663eebc 100644
--- a/matroska/src/unflatten.rs
+++ b/matroska/src/unflatten.rs
@@ -6,19 +6,25 @@
use crate::{matroska::MatroskaTag, Master};
use anyhow::Result;
+pub trait IterWithPos {
+ type Item;
+ fn next(&mut self) -> Option<Self::Item>;
+ fn position(&self) -> usize;
+}
+
pub struct Unflat<'a> {
pub item: MatroskaTag,
pub children: Option<Unflatten<'a>>,
}
pub struct Unflatten<'a> {
- inner: &'a mut dyn Iterator<Item = Result<MatroskaTag>>,
+ inner: &'a mut dyn IterWithPos<Item = Result<MatroskaTag>>,
stop: bool,
end: Option<MatroskaTag>,
}
impl<'a> Unflatten<'a> {
- pub fn new(inner: &'a mut dyn Iterator<Item = Result<MatroskaTag>>) -> Self {
+ pub fn new(inner: &'a mut dyn IterWithPos<Item = Result<MatroskaTag>>) -> Self {
Self {
inner,
stop: false,
@@ -26,7 +32,7 @@ impl<'a> Unflatten<'a> {
}
}
pub fn new_with_end(
- inner: &'a mut dyn Iterator<Item = Result<MatroskaTag>>,
+ inner: &'a mut dyn IterWithPos<Item = Result<MatroskaTag>>,
start: MatroskaTag,
) -> Self {
Self {
@@ -36,6 +42,10 @@ impl<'a> Unflatten<'a> {
}
}
+ pub fn position(&self) -> usize {
+ self.inner.position()
+ }
+
pub fn next(&mut self) -> Option<Result<Unflat>> {
if self.stop {
return None;
diff --git a/matroska/src/write.rs b/matroska/src/write.rs
index 70ee06c..ee1c44c 100644
--- a/matroska/src/write.rs
+++ b/matroska/src/write.rs
@@ -26,6 +26,31 @@ impl EbmlWriter {
Ok(())
}
+ pub fn write_padding(&mut self, position: usize) -> Result<()> {
+ let mut size = position - self.position;
+ match size {
+ 0 => return Ok(()),
+ 1 => bail!("this is sadly not possible"),
+ _ => (),
+ }
+ size -= 1; // subtract tag size
+ size -= 4; // subtract vint size
+
+ // match size {
+ // _ if size < (1 << 7) => size -= 1,
+ // _ if size < (1 << 14) => size -= 2,
+ // _ if size < (1 << 21) => size -= 3,
+ // _ if size < (1 << 28) => size -= 4,
+ // _ if size < (1 << 35) => size -= 5,
+ // _ => bail!("padding to large"),
+ // }
+
+ self.write(&[0xec])?;
+ self.write_vint_len(size.try_into().unwrap(), 4)?;
+ self.write(&vec![0; size])?;
+ Ok(())
+ }
+
pub fn write_tag(&mut self, tag: &MatroskaTag) -> Result<()> {
let mut buf = vec![];
tag.write_full(&mut buf)?;
@@ -44,6 +69,9 @@ impl EbmlWriter {
}
len += 1;
}
+ self.write_vint_len(i, len)
+ }
+ pub fn write_vint_len(&mut self, i: u64, len: usize) -> Result<()> {
let mut bytes = i.to_be_bytes();
let trunc = &mut bytes[(8 - len)..];
trunc[0] |= 1 << (8 - len);