Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple trex boxes. #103

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/mp4dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ fn get_boxes(file: File) -> Result<Vec<Box>> {
if let Some(mehd) = &mvex.mehd {
boxes.push(build_box(mehd));
}
boxes.push(build_box(&mvex.trex));
for trex in mvex.trexs.iter() {
boxes.push(build_box(trex));
}
}

// trak.
Expand Down
6 changes: 6 additions & 0 deletions src/mp4box/moov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ impl MoovBox {
if let Some(udta) = &self.udta {
size += udta.box_size();
}
if let Some(mvex) = &self.mvex {
size += mvex.box_size();
}
size
}
}
Expand Down Expand Up @@ -134,6 +137,9 @@ impl<W: Write> WriteBox<&mut W> for MoovBox {
for trak in self.traks.iter() {
trak.write_box(writer)?;
}
if let Some(mvex) = &self.mvex {
mvex.write_box(writer)?;
}
if let Some(meta) = &self.meta {
meta.write_box(writer)?;
}
Expand Down
32 changes: 21 additions & 11 deletions src/mp4box/mvex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@ use crate::mp4box::{mehd::MehdBox, trex::TrexBox};
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct MvexBox {
pub mehd: Option<MehdBox>,
pub trex: TrexBox,

#[serde(rename = "trex")]
pub trexs: Vec<TrexBox>,
}

impl MvexBox {
pub fn get_type(&self) -> BoxType {
BoxType::MdiaBox
BoxType::MvexBox
}

pub fn get_size(&self) -> u64 {
HEADER_SIZE + self.mehd.as_ref().map(|x| x.box_size()).unwrap_or(0) + self.trex.box_size()
let mut size = HEADER_SIZE;

size += self.mehd.as_ref().map_or(0, |x| x.box_size());

for trex in self.trexs.iter() {
size += trex.box_size();
}

size
}
}

Expand Down Expand Up @@ -44,7 +54,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for MvexBox {
let start = box_start(reader)?;

let mut mehd = None;
let mut trex = None;
let mut trexs = Vec::new();

let mut current = reader.stream_position()?;
let end = start + size;
Expand All @@ -63,7 +73,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for MvexBox {
mehd = Some(MehdBox::read_box(reader, s)?);
}
BoxType::TrexBox => {
trex = Some(TrexBox::read_box(reader, s)?);
trexs.push(TrexBox::read_box(reader, s)?);
}
_ => {
// XXX warn!()
Expand All @@ -74,16 +84,13 @@ impl<R: Read + Seek> ReadBox<&mut R> for MvexBox {
current = reader.stream_position()?;
}

if trex.is_none() {
if trexs.is_empty() {
return Err(Error::BoxNotFound(BoxType::TrexBox));
}

skip_bytes_to(reader, start + size)?;

Ok(MvexBox {
mehd,
trex: trex.unwrap(),
})
Ok(MvexBox { mehd, trexs })
}
}

Expand All @@ -95,7 +102,10 @@ impl<W: Write> WriteBox<&mut W> for MvexBox {
if let Some(mehd) = &self.mehd {
mehd.write_box(writer)?;
}
self.trex.write_box(writer)?;

for trex in self.trexs.iter() {
trex.write_box(writer)?;
}

Ok(size)
}
Expand Down
20 changes: 14 additions & 6 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,24 @@ impl<R: Read + Seek> Mp4Reader<R> {

// Update tracks if any fragmented (moof) boxes are found.
if !moofs.is_empty() {
let mut default_sample_duration = 0;
if let Some(ref moov) = moov {
if let Some(ref mvex) = &moov.mvex {
default_sample_duration = mvex.trex.default_sample_duration
}
}
// Grab the mvex box if it exists.
let mvex = moov.as_ref().and_then(|moov| moov.mvex.as_ref());

for moof in moofs.iter() {
for traf in moof.trafs.iter() {
let track_id = traf.tfhd.track_id;

// Get the default sample duration for the indicated track.
// This is buried in the optional mvex box, in a trex box per track.
let default_sample_duration = mvex
.and_then(|mvex| {
mvex.trexs
.iter()
.find(|trex| trex.track_id == track_id)
.map(|trex| trex.default_sample_duration)
})
.unwrap_or(0);

if let Some(track) = tracks.get_mut(&track_id) {
track.default_sample_duration = default_sample_duration;
track.trafs.push(traf.clone())
Expand Down
9 changes: 2 additions & 7 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,20 +657,15 @@ pub fn creation_time(creation_time: u64) -> u64 {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Default)]
pub enum DataType {
#[default]
Binary = 0x000000,
Text = 0x000001,
Image = 0x00000D,
TempoCpil = 0x000015,
}

impl std::default::Default for DataType {
fn default() -> Self {
DataType::Binary
}
}

impl TryFrom<u32> for DataType {
type Error = Error;
fn try_from(value: u32) -> Result<DataType> {
Expand Down