aboutsummaryrefslogtreecommitdiff
path: root/remuxer/src/import/seek_index.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2023-08-03 20:37:11 +0200
committermetamuffin <metamuffin@disroot.org>2023-08-03 20:37:11 +0200
commit5b71ccaf2bbe34f1d39d4f38f2b5c2090a9761b1 (patch)
treeee66fbaec90cb0e2d3a9eb3d8e7a450fd45846b3 /remuxer/src/import/seek_index.rs
parentc71a10717392cc321b97e3f2173645e78c263488 (diff)
downloadjellything-5b71ccaf2bbe34f1d39d4f38f2b5c2090a9761b1.tar
jellything-5b71ccaf2bbe34f1d39d4f38f2b5c2090a9761b1.tar.bz2
jellything-5b71ccaf2bbe34f1d39d4f38f2b5c2090a9761b1.tar.zst
refactor import script
Diffstat (limited to 'remuxer/src/import/seek_index.rs')
-rw-r--r--remuxer/src/import/seek_index.rs116
1 files changed, 116 insertions, 0 deletions
diff --git a/remuxer/src/import/seek_index.rs b/remuxer/src/import/seek_index.rs
new file mode 100644
index 0000000..363a12f
--- /dev/null
+++ b/remuxer/src/import/seek_index.rs
@@ -0,0 +1,116 @@
+use anyhow::Result;
+use jellycommon::{BlockIndex, SeekIndex};
+use jellymatroska::{
+ block::Block,
+ read::EbmlReader,
+ unflatten::{IterWithPos, Unflat, Unflatten},
+ MatroskaTag,
+};
+use log::{debug, info, trace, warn};
+use std::collections::BTreeMap;
+
+pub fn import_seek_index(input: &mut EbmlReader) -> Result<BTreeMap<u64, SeekIndex>> {
+ let mut seek_index = BTreeMap::new();
+ while let Some(item) = input.next() {
+ let item = match item {
+ Ok(item) => item,
+ Err(e) => {
+ warn!("{e}");
+ break;
+ }
+ };
+ match item {
+ MatroskaTag::Segment(_) => {
+ info!("segment start");
+ let mut children = Unflatten::new_with_end(input, item);
+ import_seek_index_segment(&mut children, &mut seek_index)?;
+ info!("segment end");
+ }
+ _ => debug!("(r) tag ignored: {item:?}"),
+ }
+ }
+ Ok(seek_index)
+}
+
+fn import_seek_index_segment(
+ segment: &mut Unflatten,
+ seek_index: &mut BTreeMap<u64, SeekIndex>,
+) -> Result<()> {
+ while let Some(Ok(Unflat { children, item, .. })) = segment.n() {
+ match item {
+ MatroskaTag::SeekHead(_) => {}
+ MatroskaTag::Info(_) => {}
+ MatroskaTag::Tags(_) => {}
+ MatroskaTag::Cues(_) => {}
+ MatroskaTag::Chapters(_) => {}
+ MatroskaTag::Tracks(_) => {}
+ MatroskaTag::Cluster(_) => {
+ let mut children = children.unwrap();
+ let mut pts = 0;
+ let mut position = children.position();
+
+ loop {
+ if let Some(Ok(Unflat { children, item, .. })) = children.n() {
+ match item {
+ MatroskaTag::Timestamp(ts) => pts = ts,
+ MatroskaTag::BlockGroup(_) => {
+ debug!("group");
+ let mut children = children.unwrap();
+ // let position = children.position(); //? TODO where should this point to? cluster or block? // probably block
+ while let Some(Ok(Unflat {
+ children: _,
+ item,
+ position,
+ })) = children.n()
+ {
+ match item {
+ MatroskaTag::Block(ref buf) => {
+ let block = Block::parse(buf)?;
+ debug!(
+ "block: track={} tso={}",
+ block.track, block.timestamp_off
+ );
+ seek_index
+ .entry(block.track)
+ .or_insert(SeekIndex { blocks: vec![] })
+ .blocks
+ .push(BlockIndex {
+ pts: pts + block.timestamp_off as u64,
+ source_off: position,
+ size: block.data.len(),
+ });
+ }
+ _ => trace!("{item:?}"),
+ }
+ }
+ }
+ MatroskaTag::SimpleBlock(buf) => {
+ let block = Block::parse(&buf)?;
+ debug!(
+ "simple block: track={} tso={}",
+ block.track, block.timestamp_off
+ );
+ debug!("{pts} {}", block.timestamp_off);
+ seek_index
+ .entry(block.track)
+ .or_insert(SeekIndex { blocks: vec![] })
+ .blocks
+ .push(BlockIndex {
+ pts: (pts as i64 + block.timestamp_off as i64) as u64,
+ source_off: position,
+ size: block.data.len(),
+ });
+ }
+ _ => debug!("(rsc) tag ignored: {item:?}"),
+ }
+ } else {
+ break;
+ }
+ position = children.position();
+ }
+ }
+ _ => debug!("(rs) tag ignored: {item:?}"),
+ };
+ }
+ Ok(())
+}