mirror of
https://github.com/alfg/mp4-rust.git
synced 2024-06-11 01:19:21 +00:00
feat: example/mp4sample supports fragmented mp4
This commit is contained in:
parent
a66517bc1a
commit
2d329d52ae
|
@ -10,18 +10,47 @@ pub struct TrunBox {
|
|||
pub flags: u32,
|
||||
pub sample_count: u32,
|
||||
pub data_offset: i32,
|
||||
pub first_sample_flags: Option<u32>,
|
||||
|
||||
#[serde(skip_serializing)]
|
||||
pub sample_durations: Vec<u32>,
|
||||
#[serde(skip_serializing)]
|
||||
pub sample_sizes: Vec<u32>,
|
||||
#[serde(skip_serializing)]
|
||||
pub sample_flags: Vec<u32>,
|
||||
#[serde(skip_serializing)]
|
||||
pub sample_cts: Vec<u32>,
|
||||
}
|
||||
|
||||
impl TrunBox {
|
||||
pub const FLAG_FIRST_SAMPLE_FLAGS: u32 = 0x04;
|
||||
pub const FLAG_SAMPLE_DURATION: u32 = 0x100;
|
||||
pub const FLAG_SAMPLE_SIZE: u32 = 0x200;
|
||||
pub const FLAG_SAMPLE_FLAGS: u32 = 0x400;
|
||||
pub const FLAG_SAMPLE_CTS: u32 = 0x800;
|
||||
|
||||
pub fn get_type(&self) -> BoxType {
|
||||
BoxType::TrunBox
|
||||
}
|
||||
|
||||
pub fn get_size(&self) -> u64 {
|
||||
HEADER_SIZE + HEADER_EXT_SIZE + 8 + (4 * self.sample_sizes.len() as u64)
|
||||
let mut sum = HEADER_SIZE + HEADER_EXT_SIZE + 8;
|
||||
if TrunBox::FLAG_FIRST_SAMPLE_FLAGS & self.flags > 0 {
|
||||
sum += 4;
|
||||
}
|
||||
if TrunBox::FLAG_SAMPLE_DURATION & self.flags > 0 {
|
||||
sum += 4 * self.sample_count as u64;
|
||||
}
|
||||
if TrunBox::FLAG_SAMPLE_SIZE & self.flags > 0 {
|
||||
sum += 4 * self.sample_count as u64;
|
||||
}
|
||||
if TrunBox::FLAG_SAMPLE_FLAGS & self.flags > 0 {
|
||||
sum += 4 * self.sample_count as u64;
|
||||
}
|
||||
if TrunBox::FLAG_SAMPLE_CTS & self.flags > 0 {
|
||||
sum += 4 * self.sample_count as u64;
|
||||
}
|
||||
sum
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,10 +83,36 @@ impl<R: Read + Seek> ReadBox<&mut R> for TrunBox {
|
|||
let sample_count = reader.read_u32::<BigEndian>()?;
|
||||
let data_offset = reader.read_i32::<BigEndian>()?;
|
||||
|
||||
let first_sample_flags = if TrunBox::FLAG_FIRST_SAMPLE_FLAGS & flags > 0 {
|
||||
Some(reader.read_u32::<BigEndian>()?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut sample_durations = Vec::with_capacity(sample_count as usize);
|
||||
let mut sample_sizes = Vec::with_capacity(sample_count as usize);
|
||||
let mut sample_flags = Vec::with_capacity(sample_count as usize);
|
||||
let mut sample_cts = Vec::with_capacity(sample_count as usize);
|
||||
for _ in 0..sample_count {
|
||||
let sample_size = reader.read_u32::<BigEndian>()?;
|
||||
sample_sizes.push(sample_size);
|
||||
if TrunBox::FLAG_SAMPLE_DURATION & flags > 0 {
|
||||
let duration = reader.read_u32::<BigEndian>()?;
|
||||
sample_durations.push(duration);
|
||||
}
|
||||
|
||||
if TrunBox::FLAG_SAMPLE_SIZE & flags > 0 {
|
||||
let sample_size = reader.read_u32::<BigEndian>()?;
|
||||
sample_sizes.push(sample_size);
|
||||
}
|
||||
|
||||
if TrunBox::FLAG_SAMPLE_FLAGS & flags > 0 {
|
||||
let sample_flag = reader.read_u32::<BigEndian>()?;
|
||||
sample_flags.push(sample_flag);
|
||||
}
|
||||
|
||||
if TrunBox::FLAG_SAMPLE_CTS & flags > 0 {
|
||||
let cts = reader.read_u32::<BigEndian>()?;
|
||||
sample_cts.push(cts);
|
||||
}
|
||||
}
|
||||
|
||||
skip_bytes_to(reader, start + size)?;
|
||||
|
@ -67,7 +122,11 @@ impl<R: Read + Seek> ReadBox<&mut R> for TrunBox {
|
|||
flags,
|
||||
sample_count,
|
||||
data_offset,
|
||||
first_sample_flags,
|
||||
sample_durations,
|
||||
sample_sizes,
|
||||
sample_flags,
|
||||
sample_cts,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -81,9 +140,23 @@ impl<W: Write> WriteBox<&mut W> for TrunBox {
|
|||
|
||||
writer.write_u32::<BigEndian>(self.sample_count)?;
|
||||
writer.write_i32::<BigEndian>(self.data_offset)?;
|
||||
if let Some(v) = self.first_sample_flags {
|
||||
writer.write_u32::<BigEndian>(v)?;
|
||||
}
|
||||
assert_eq!(self.sample_count, self.sample_sizes.len() as u32);
|
||||
for sample_number in self.sample_sizes.iter() {
|
||||
writer.write_u32::<BigEndian>(*sample_number)?;
|
||||
for i in 0..self.sample_count as usize {
|
||||
if TrunBox::FLAG_SAMPLE_DURATION & self.flags > 0 {
|
||||
writer.write_u32::<BigEndian>(self.sample_durations[i])?;
|
||||
}
|
||||
if TrunBox::FLAG_SAMPLE_SIZE & self.flags > 0 {
|
||||
writer.write_u32::<BigEndian>(self.sample_sizes[i])?;
|
||||
}
|
||||
if TrunBox::FLAG_SAMPLE_FLAGS & self.flags > 0 {
|
||||
writer.write_u32::<BigEndian>(self.sample_flags[i])?;
|
||||
}
|
||||
if TrunBox::FLAG_SAMPLE_CTS & self.flags > 0 {
|
||||
writer.write_u32::<BigEndian>(self.sample_cts[i])?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(size)
|
||||
|
@ -104,6 +177,10 @@ mod tests {
|
|||
data_offset: 0,
|
||||
sample_count: 0,
|
||||
sample_sizes: vec![],
|
||||
sample_flags: vec![],
|
||||
first_sample_flags: None,
|
||||
sample_durations: vec![],
|
||||
sample_cts: vec![],
|
||||
};
|
||||
let mut buf = Vec::new();
|
||||
src_box.write_box(&mut buf).unwrap();
|
||||
|
@ -122,10 +199,14 @@ mod tests {
|
|||
fn test_trun_many_sizes() {
|
||||
let src_box = TrunBox {
|
||||
version: 0,
|
||||
flags: 0,
|
||||
flags: TrunBox::FLAG_SAMPLE_DURATION | TrunBox::FLAG_SAMPLE_SIZE | TrunBox::FLAG_SAMPLE_FLAGS | TrunBox::FLAG_SAMPLE_CTS,
|
||||
data_offset: 0,
|
||||
sample_count: 9,
|
||||
sample_sizes: vec![1165, 11, 11, 8545, 10126, 10866, 9643, 9351, 7730],
|
||||
sample_flags: vec![1165, 11, 11, 8545, 10126, 10866, 9643, 9351, 7730],
|
||||
first_sample_flags: None,
|
||||
sample_durations: vec![1165, 11, 11, 8545, 10126, 10866, 9643, 9351, 7730],
|
||||
sample_cts: vec![1165, 11, 11, 8545, 10126, 10866, 9643, 9351, 7730],
|
||||
};
|
||||
let mut buf = Vec::new();
|
||||
src_box.write_box(&mut buf).unwrap();
|
||||
|
|
53
src/track.rs
53
src/track.rs
|
@ -363,24 +363,28 @@ impl Mp4Track {
|
|||
|
||||
fn sample_size(&self, sample_id: u32) -> Result<u32> {
|
||||
if self.trafs.len() > 0 {
|
||||
let sample_sizes_count = self.sample_count() / self.trafs.len() as u32;
|
||||
let traf_idx = (sample_id - 1) / sample_sizes_count;
|
||||
if let Some(trun) = &self.trafs[traf_idx as usize].trun {
|
||||
if let Some(size) = trun.sample_sizes.get((sample_id - (sample_sizes_count * traf_idx)) as usize - 1) {
|
||||
Ok(*size)
|
||||
} else {
|
||||
return Err(Error::EntryInTrunNotFound(
|
||||
self.track_id(),
|
||||
BoxType::TrunBox,
|
||||
sample_id,
|
||||
));
|
||||
let mut offset = 0;
|
||||
for traf_idx in 0..self.trafs.len() {
|
||||
if let Some(trun) = &self.trafs[traf_idx].trun {
|
||||
let sample_count = trun.sample_count;
|
||||
if sample_count > (sample_id - 1 - offset) {
|
||||
if let Some(size) = trun.sample_sizes.get((sample_id - offset) as usize - 1) {
|
||||
return Ok(*size);
|
||||
} else {
|
||||
return Err(Error::EntryInTrunNotFound(
|
||||
self.track_id(),
|
||||
BoxType::TrunBox,
|
||||
sample_id,
|
||||
));
|
||||
}
|
||||
}
|
||||
offset += sample_count;
|
||||
}
|
||||
} else {
|
||||
return Err(Error::BoxInTrafNotFound(
|
||||
self.track_id(),
|
||||
BoxType::TrafBox,
|
||||
));
|
||||
}
|
||||
return Err(Error::BoxInTrafNotFound(
|
||||
self.track_id(),
|
||||
BoxType::TrafBox,
|
||||
));
|
||||
} else {
|
||||
let stsz = &self.trak.mdia.minf.stbl.stsz;
|
||||
if stsz.sample_size > 0 {
|
||||
|
@ -413,9 +417,20 @@ impl Mp4Track {
|
|||
|
||||
fn sample_offset(&self, sample_id: u32) -> Result<u64> {
|
||||
if self.trafs.len() > 0 {
|
||||
let sample_sizes_count = self.sample_count() / self.trafs.len() as u32;
|
||||
let traf_idx = (sample_id - 1) / sample_sizes_count;
|
||||
Ok(self.trafs[(sample_id - (sample_sizes_count * traf_idx)) as usize].tfhd.base_data_offset as u64)
|
||||
let mut offset = 0;
|
||||
for traf_idx in 0..self.trafs.len() {
|
||||
if let Some(trun) = &self.trafs[traf_idx].trun {
|
||||
let sample_count = trun.sample_count;
|
||||
if sample_count > (sample_id - 1 - offset) {
|
||||
return Ok(self.trafs[traf_idx].tfhd.base_data_offset as u64);
|
||||
}
|
||||
offset += sample_count;
|
||||
}
|
||||
}
|
||||
return Err(Error::BoxInTrafNotFound(
|
||||
self.track_id(),
|
||||
BoxType::TrafBox,
|
||||
));
|
||||
} else {
|
||||
let stsc_index = self.stsc_index(sample_id);
|
||||
|
||||
|
|
Loading…
Reference in a new issue