mirror of
https://github.com/alfg/mp4-rust.git
synced 2024-12-22 12:06:33 +00:00
Example/mp4sample supports fragmented mp4 (#46)
* feat: mvex box中的mehd box改为可选,支持fmp4的解析 * feat: example/mp4sample supports fragmented mp4 * feat: utilize function sample_count(), extract duplicated codes as a new function * feat: field data_offset in TrunBox is optional * feat: remove an ".expect()" * feat: remove an ".expect()" * chore: re-style code
This commit is contained in:
parent
4d2b5acf9e
commit
eedccec776
2 changed files with 133 additions and 22 deletions
|
@ -9,19 +9,52 @@ pub struct TrunBox {
|
||||||
pub version: u8,
|
pub version: u8,
|
||||||
pub flags: u32,
|
pub flags: u32,
|
||||||
pub sample_count: u32,
|
pub sample_count: u32,
|
||||||
pub data_offset: i32,
|
pub data_offset: Option<i32>,
|
||||||
|
pub first_sample_flags: Option<u32>,
|
||||||
|
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
pub sample_durations: Vec<u32>,
|
||||||
#[serde(skip_serializing)]
|
#[serde(skip_serializing)]
|
||||||
pub sample_sizes: Vec<u32>,
|
pub sample_sizes: Vec<u32>,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
pub sample_flags: Vec<u32>,
|
||||||
|
#[serde(skip_serializing)]
|
||||||
|
pub sample_cts: Vec<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TrunBox {
|
impl TrunBox {
|
||||||
|
pub const FLAG_DATA_OFFSET: u32 = 0x01;
|
||||||
|
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 {
|
pub fn get_type(&self) -> BoxType {
|
||||||
BoxType::TrunBox
|
BoxType::TrunBox
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_size(&self) -> u64 {
|
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 + 4;
|
||||||
|
if TrunBox::FLAG_DATA_OFFSET & self.flags > 0 {
|
||||||
|
sum += 4;
|
||||||
|
}
|
||||||
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,12 +85,43 @@ impl<R: Read + Seek> ReadBox<&mut R> for TrunBox {
|
||||||
let (version, flags) = read_box_header_ext(reader)?;
|
let (version, flags) = read_box_header_ext(reader)?;
|
||||||
|
|
||||||
let sample_count = reader.read_u32::<BigEndian>()?;
|
let sample_count = reader.read_u32::<BigEndian>()?;
|
||||||
let data_offset = reader.read_i32::<BigEndian>()?;
|
|
||||||
|
|
||||||
|
let data_offset = if TrunBox::FLAG_DATA_OFFSET & flags > 0 {
|
||||||
|
Some(reader.read_i32::<BigEndian>()?)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
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_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 {
|
for _ in 0..sample_count {
|
||||||
let sample_size = reader.read_u32::<BigEndian>()?;
|
if TrunBox::FLAG_SAMPLE_DURATION & flags > 0 {
|
||||||
sample_sizes.push(sample_size);
|
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)?;
|
skip_bytes_to(reader, start + size)?;
|
||||||
|
@ -67,7 +131,11 @@ impl<R: Read + Seek> ReadBox<&mut R> for TrunBox {
|
||||||
flags,
|
flags,
|
||||||
sample_count,
|
sample_count,
|
||||||
data_offset,
|
data_offset,
|
||||||
|
first_sample_flags,
|
||||||
|
sample_durations,
|
||||||
sample_sizes,
|
sample_sizes,
|
||||||
|
sample_flags,
|
||||||
|
sample_cts,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,10 +148,26 @@ impl<W: Write> WriteBox<&mut W> for TrunBox {
|
||||||
write_box_header_ext(writer, self.version, self.flags)?;
|
write_box_header_ext(writer, self.version, self.flags)?;
|
||||||
|
|
||||||
writer.write_u32::<BigEndian>(self.sample_count)?;
|
writer.write_u32::<BigEndian>(self.sample_count)?;
|
||||||
writer.write_i32::<BigEndian>(self.data_offset)?;
|
if let Some(v) = self.data_offset{
|
||||||
|
writer.write_i32::<BigEndian>(v)?;
|
||||||
|
}
|
||||||
|
if let Some(v) = self.first_sample_flags {
|
||||||
|
writer.write_u32::<BigEndian>(v)?;
|
||||||
|
}
|
||||||
assert_eq!(self.sample_count, self.sample_sizes.len() as u32);
|
assert_eq!(self.sample_count, self.sample_sizes.len() as u32);
|
||||||
for sample_number in self.sample_sizes.iter() {
|
for i in 0..self.sample_count as usize {
|
||||||
writer.write_u32::<BigEndian>(*sample_number)?;
|
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)
|
Ok(size)
|
||||||
|
@ -101,9 +185,13 @@ mod tests {
|
||||||
let src_box = TrunBox {
|
let src_box = TrunBox {
|
||||||
version: 0,
|
version: 0,
|
||||||
flags: 0,
|
flags: 0,
|
||||||
data_offset: 0,
|
data_offset: None,
|
||||||
sample_count: 0,
|
sample_count: 0,
|
||||||
sample_sizes: vec![],
|
sample_sizes: vec![],
|
||||||
|
sample_flags: vec![],
|
||||||
|
first_sample_flags: None,
|
||||||
|
sample_durations: vec![],
|
||||||
|
sample_cts: vec![],
|
||||||
};
|
};
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
src_box.write_box(&mut buf).unwrap();
|
src_box.write_box(&mut buf).unwrap();
|
||||||
|
@ -122,10 +210,14 @@ mod tests {
|
||||||
fn test_trun_many_sizes() {
|
fn test_trun_many_sizes() {
|
||||||
let src_box = TrunBox {
|
let src_box = TrunBox {
|
||||||
version: 0,
|
version: 0,
|
||||||
flags: 0,
|
flags: TrunBox::FLAG_SAMPLE_DURATION | TrunBox::FLAG_SAMPLE_SIZE | TrunBox::FLAG_SAMPLE_FLAGS | TrunBox::FLAG_SAMPLE_CTS,
|
||||||
data_offset: 0,
|
data_offset: None,
|
||||||
sample_count: 9,
|
sample_count: 9,
|
||||||
sample_sizes: vec![1165, 11, 11, 8545, 10126, 10866, 9643, 9351, 7730],
|
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();
|
let mut buf = Vec::new();
|
||||||
src_box.write_box(&mut buf).unwrap();
|
src_box.write_box(&mut buf).unwrap();
|
||||||
|
|
41
src/track.rs
41
src/track.rs
|
@ -361,25 +361,39 @@ impl Mp4Track {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// return `(traf_idx, sample_idx_in_trun)`
|
||||||
|
fn find_traf_idx_and_sample_idx(&self, sample_id: u32) -> Option<(usize, usize)>{
|
||||||
|
let global_idx = sample_id - 1;
|
||||||
|
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 > (global_idx - offset) {
|
||||||
|
return Some((traf_idx, (global_idx - offset) as _));
|
||||||
|
}
|
||||||
|
offset += sample_count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
fn sample_size(&self, sample_id: u32) -> Result<u32> {
|
fn sample_size(&self, sample_id: u32) -> Result<u32> {
|
||||||
if self.trafs.len() > 0 {
|
if self.trafs.len() > 0 {
|
||||||
let sample_sizes_count = self.sample_count() / self.trafs.len() as u32;
|
if let Some((traf_idx, sample_idx)) = self.find_traf_idx_and_sample_idx(sample_id) {
|
||||||
let traf_idx = (sample_id - 1) / sample_sizes_count;
|
if let Some(size) = self.trafs[traf_idx].trun.as_ref().unwrap().sample_sizes.get(sample_idx) {
|
||||||
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)
|
Ok(*size)
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::EntryInTrunNotFound(
|
Err(Error::EntryInTrunNotFound(
|
||||||
self.track_id(),
|
self.track_id(),
|
||||||
BoxType::TrunBox,
|
BoxType::TrunBox,
|
||||||
sample_id,
|
sample_id,
|
||||||
));
|
))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::BoxInTrafNotFound(
|
Err(Error::BoxInTrafNotFound(
|
||||||
self.track_id(),
|
self.track_id(),
|
||||||
BoxType::TrafBox,
|
BoxType::TrafBox,
|
||||||
));
|
))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let stsz = &self.trak.mdia.minf.stbl.stsz;
|
let stsz = &self.trak.mdia.minf.stbl.stsz;
|
||||||
|
@ -413,9 +427,14 @@ impl Mp4Track {
|
||||||
|
|
||||||
fn sample_offset(&self, sample_id: u32) -> Result<u64> {
|
fn sample_offset(&self, sample_id: u32) -> Result<u64> {
|
||||||
if self.trafs.len() > 0 {
|
if self.trafs.len() > 0 {
|
||||||
let sample_sizes_count = self.sample_count() / self.trafs.len() as u32;
|
if let Some((traf_idx, _sample_idx)) = self.find_traf_idx_and_sample_idx(sample_id) {
|
||||||
let traf_idx = (sample_id - 1) / sample_sizes_count;
|
Ok(self.trafs[traf_idx].tfhd.base_data_offset as u64)
|
||||||
Ok(self.trafs[(sample_id - (sample_sizes_count * traf_idx)) as usize].tfhd.base_data_offset as u64)
|
} else {
|
||||||
|
Err(Error::BoxInTrafNotFound(
|
||||||
|
self.track_id(),
|
||||||
|
BoxType::TrafBox,
|
||||||
|
))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let stsc_index = self.stsc_index(sample_id);
|
let stsc_index = self.stsc_index(sample_id);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue