1
0
Fork 0
mirror of https://github.com/alfg/mp4-rust.git synced 2024-06-11 09:29:21 +00:00

feat: field data_offset in TrunBox is optional

This commit is contained in:
ninthakeey 2021-01-28 22:53:28 +08:00
parent eb61c9de37
commit 1d66e50d7f
2 changed files with 22 additions and 11 deletions

View file

@ -9,7 +9,7 @@ pub struct TrunBox {
pub version: u8,
pub flags: u32,
pub sample_count: u32,
pub data_offset: i32,
pub data_offset: Option<i32>,
pub first_sample_flags: Option<u32>,
#[serde(skip_serializing)]
@ -23,6 +23,7 @@ pub struct 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;
@ -34,7 +35,10 @@ impl TrunBox {
}
pub fn get_size(&self) -> u64 {
let mut sum = HEADER_SIZE + HEADER_EXT_SIZE + 8;
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;
}
@ -81,7 +85,12 @@ impl<R: Read + Seek> ReadBox<&mut R> for TrunBox {
let (version, flags) = read_box_header_ext(reader)?;
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>()?)
@ -139,7 +148,9 @@ impl<W: Write> WriteBox<&mut W> for TrunBox {
write_box_header_ext(writer, self.version, self.flags)?;
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)?;
}
@ -174,7 +185,7 @@ mod tests {
let src_box = TrunBox {
version: 0,
flags: 0,
data_offset: 0,
data_offset: None,
sample_count: 0,
sample_sizes: vec![],
sample_flags: vec![],
@ -200,7 +211,7 @@ mod tests {
let src_box = TrunBox {
version: 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_sizes: vec![1165, 11, 11, 8545, 10126, 10866, 9643, 9351, 7730],
sample_flags: vec![1165, 11, 11, 8545, 10126, 10866, 9643, 9351, 7730],

View file

@ -379,9 +379,8 @@ impl Mp4Track {
fn sample_size(&self, sample_id: u32) -> Result<u32> {
if self.trafs.len() > 0 {
println!("trafs.len={}", self.trafs.len());
return if let Some((traf_idx, offset)) = self.find_traf_idx_and_sample_idx(sample_id) {
if let Some(size) = self.trafs[traf_idx].trun.as_ref().unwrap().sample_sizes.get(offset) {
return if let Some((traf_idx, sample_idx)) = self.find_traf_idx_and_sample_idx(sample_id) {
if let Some(size) = self.trafs[traf_idx].trun.as_ref().unwrap().sample_sizes.get(sample_idx) {
Ok(*size)
} else {
Err(Error::EntryInTrunNotFound(
@ -428,7 +427,7 @@ impl Mp4Track {
fn sample_offset(&self, sample_id: u32) -> Result<u64> {
if self.trafs.len() > 0 {
return if let Some((traf_idx, _offset)) = self.find_traf_idx_and_sample_idx(sample_id) {
return if let Some((traf_idx, _sample_idx)) = self.find_traf_idx_and_sample_idx(sample_id) {
Ok(self.trafs[traf_idx].tfhd.base_data_offset as u64)
} else {
Err(Error::BoxInTrafNotFound(
@ -530,7 +529,8 @@ impl Mp4Track {
let mut buffer = vec![0x0u8; sample_size as usize];
reader.seek(SeekFrom::Start(sample_offset))?;
reader.read_exact(&mut buffer)?;
reader.read_exact(&mut buffer)
.expect(&format!("sample_offset={}, sample_size={}", sample_offset, sample_size));
let (start_time, duration) = self.sample_time(sample_id).unwrap(); // XXX
let rendering_offset = self.sample_rendering_offset(sample_id);