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 better handling of sample sizes for fragmented media #137

Open
wants to merge 2 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
5 changes: 4 additions & 1 deletion src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ 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;
let mut default_sample_size = 0;
if let Some(ref moov) = moov {
if let Some(ref mvex) = &moov.mvex {
default_sample_duration = mvex.trex.default_sample_duration
default_sample_duration = mvex.trex.default_sample_duration;
default_sample_size = mvex.trex.default_sample_size;
}
}

Expand All @@ -109,6 +111,7 @@ impl<R: Read + Seek> Mp4Reader<R> {
let track_id = traf.tfhd.track_id;
if let Some(track) = tracks.get_mut(&track_id) {
track.default_sample_duration = default_sample_duration;
track.default_sample_size = default_sample_size;
track.moof_offsets.push(moof_offset);
track.trafs.push(traf.clone())
} else {
Expand Down
26 changes: 13 additions & 13 deletions src/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub struct Mp4Track {

// Fragmented Tracks Defaults.
pub default_sample_duration: u32,
pub default_sample_size: u32,
}

impl Mp4Track {
Expand All @@ -107,6 +108,7 @@ impl Mp4Track {
trafs: Vec::new(),
moof_offsets: Vec::new(),
default_sample_duration: 0,
default_sample_size: 0,
}
}

Expand Down Expand Up @@ -261,7 +263,7 @@ impl Mp4Track {

pub fn sequence_parameter_set(&self) -> Result<&[u8]> {
if let Some(ref avc1) = self.trak.mdia.minf.stbl.stsd.avc1 {
match avc1.avcc.sequence_parameter_sets.get(0) {
match avc1.avcc.sequence_parameter_sets.first() {
Some(nal) => Ok(nal.bytes.as_ref()),
None => Err(Error::EntryInStblNotFound(
self.track_id(),
Expand All @@ -276,7 +278,7 @@ impl Mp4Track {

pub fn picture_parameter_set(&self) -> Result<&[u8]> {
if let Some(ref avc1) = self.trak.mdia.minf.stbl.stsd.avc1 {
match avc1.avcc.picture_parameter_sets.get(0) {
match avc1.avcc.picture_parameter_sets.first() {
Some(nal) => Ok(nal.bytes.as_ref()),
None => Err(Error::EntryInStblNotFound(
self.track_id(),
Expand Down Expand Up @@ -389,21 +391,19 @@ impl Mp4Track {
fn sample_size(&self, sample_id: u32) -> Result<u32> {
if !self.trafs.is_empty() {
if let Some((traf_idx, sample_idx)) = self.find_traf_idx_and_sample_idx(sample_id) {
if let Some(size) = self.trafs[traf_idx]
let mut default_sample_size = self.default_sample_size;
let traf = &self.trafs[traf_idx];
if let Some(size) = traf.tfhd.default_sample_size {
default_sample_size = size;
}
if let Some(size) = traf
.trun
.as_ref()
.unwrap()
.sample_sizes
.get(sample_idx)
.and_then(|trun| trun.sample_sizes.get(sample_idx))
{
Ok(*size)
} else {
Err(Error::EntryInTrunNotFound(
self.track_id(),
BoxType::TrunBox,
sample_id,
))
default_sample_size = *size;
}
Ok(default_sample_size)
} else {
Err(Error::BoxInTrafNotFound(self.track_id(), BoxType::TrafBox))
}
Expand Down
Loading