Skip to content
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
3 changes: 2 additions & 1 deletion src/moov/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ mod test {
in_movie: true,
width: 1920.into(),
height: 1080.into(),
duration: Some(0),
..Default::default()
},
edts: Some(Edts {
Expand All @@ -182,7 +183,7 @@ mod test {
creation_time: 3773812694,
modification_time: 3773812694,
timescale: 90000,
duration: 0,
duration: Some(0),
language: "und".to_string(),
},
hdlr: Hdlr {
Expand Down
30 changes: 18 additions & 12 deletions src/moov/mvhd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Mvhd {
pub creation_time: u64,
pub modification_time: u64,
pub timescale: u32,
pub duration: u64,
pub duration: Option<u64>,

pub rate: FixedPoint<u16>,
pub volume: FixedPoint<u8>,
Expand All @@ -32,16 +32,19 @@ impl AtomExt for Mvhd {
u64::decode(buf)?,
u64::decode(buf)?,
u32::decode(buf)?,
u64::decode(buf)?,
match u64::decode(buf)? {
u64::MAX => None,
duration => Some(duration),
},
),
MvhdVersion::V0 => (
u32::decode(buf)? as u64,
u32::decode(buf)? as u64,
u32::decode(buf)?,
// All 1s means unknown; keep it all 1s in 64 bits.
// All 1s means unknown.
match u32::decode(buf)? {
u32::MAX => u64::MAX,
duration => duration as u64,
u32::MAX => None,
duration => Some(duration as u64),
},
),
};
Expand Down Expand Up @@ -74,7 +77,10 @@ impl AtomExt for Mvhd {
self.creation_time.encode(buf)?;
self.modification_time.encode(buf)?;
self.timescale.encode(buf)?;
self.duration.encode(buf)?;
match self.duration {
None => u64::MAX.encode(buf)?,
_ => self.duration.encode(buf)?,
};

self.rate.encode(buf)?;
self.volume.encode(buf)?;
Expand All @@ -98,7 +104,7 @@ impl Default for Mvhd {
creation_time: 0,
modification_time: 0,
timescale: 1000,
duration: 0,
duration: Some(0),
rate: Default::default(),
matrix: Default::default(),
volume: Default::default(),
Expand All @@ -117,7 +123,7 @@ mod tests {
creation_time: 100,
modification_time: 200,
timescale: 1000,
duration: 634634,
duration: Some(634634),
rate: 1.into(),
volume: 1.into(),
matrix: Matrix::default(),
Expand All @@ -138,7 +144,7 @@ mod tests {
creation_time: 100,
modification_time: 200,
timescale: 1000,
duration: 634634,
duration: Some(634634),
rate: 1.into(),
volume: 1.into(),
matrix: Matrix::default(),
Expand Down Expand Up @@ -179,19 +185,19 @@ mod tests {
decoded.encode(&mut buf).unwrap();
assert_eq!(buf[8], 1, "re-encoded as version 1");
assert_eq!(buf[32..40], [0xFF; 8], "duration is still unknown");
assert_eq!(decoded.duration, u64::MAX);
assert_eq!(decoded.duration, None);

// One below all 1s is still a finite duration.
let mut finite = raw.to_vec();
finite[27] = 0xFE;
let decoded = Mvhd::decode(&mut finite.as_slice()).unwrap();
assert_eq!(decoded.duration, u32::MAX as u64 - 1);
assert_eq!(decoded.duration, Some(u32::MAX as u64 - 1));
}

#[test]
fn test_mvhd64_duration_u32_max() {
let expected = Mvhd {
duration: u32::MAX as u64,
duration: Some(u32::MAX as u64),
..Default::default()
};
let mut buf = Vec::new();
Expand Down
27 changes: 17 additions & 10 deletions src/moov/trak/mdia/mdhd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub struct Mdhd {
pub creation_time: u64,
pub modification_time: u64,
pub timescale: u32,
pub duration: u64,
// None here means unknown - all 1s in the encoding
pub duration: Option<u64>,
pub language: String,
}

Expand All @@ -27,16 +28,19 @@ impl AtomExt for Mdhd {
u64::decode(buf)?,
u64::decode(buf)?,
u32::decode(buf)?,
u64::decode(buf)?,
match u64::decode(buf)? {
u64::MAX => None,
duration => Some(duration),
},
),
MdhdVersion::V0 => (
u32::decode(buf)? as u64,
u32::decode(buf)? as u64,
u32::decode(buf)?,
// All 1s means unknown; keep it all 1s in 64 bits.
match u32::decode(buf)? {
u32::MAX => u64::MAX,
duration => duration as u64,
u32::MAX => None,
duration => Some(duration as u64),
},
),
};
Expand All @@ -59,7 +63,10 @@ impl AtomExt for Mdhd {
self.creation_time.encode(buf)?;
self.modification_time.encode(buf)?;
self.timescale.encode(buf)?;
self.duration.encode(buf)?;
match self.duration {
None => u64::MAX.encode(buf)?,
_ => self.duration.encode(buf)?,
}

let language_code = language_code(&self.language);
(language_code).encode(buf)?;
Expand All @@ -79,7 +86,7 @@ mod tests {
creation_time: 100,
modification_time: 200,
timescale: 48000,
duration: 30439936,
duration: Some(30439936),
language: String::from("und"),
};
let mut buf = Vec::new();
Expand All @@ -96,7 +103,7 @@ mod tests {
creation_time: 100,
modification_time: 200,
timescale: 48000,
duration: 30439936,
duration: Some(30439936),
language: String::from("eng"),
};
let mut buf = Vec::new();
Expand Down Expand Up @@ -124,20 +131,20 @@ mod tests {
decoded.encode(&mut buf).unwrap();
assert_eq!(buf[8], 1, "re-encoded as version 1");
assert_eq!(buf[32..40], [0xFF; 8], "duration is still unknown");
assert_eq!(decoded.duration, u64::MAX);
assert_eq!(decoded.duration, None);

// One below all 1s is still a finite duration.
let mut finite = raw.to_vec();
finite[27] = 0xFE;
let decoded = Mdhd::decode(&mut finite.as_slice()).unwrap();
assert_eq!(decoded.duration, u32::MAX as u64 - 1);
assert_eq!(decoded.duration, Some(u32::MAX as u64 - 1));
}

#[test]
fn test_mdhd64_duration_u32_max() {
let expected = Mdhd {
timescale: 48000,
duration: u32::MAX as u64,
duration: Some(u32::MAX as u64),
language: String::from("und"),
..Default::default()
};
Expand Down
27 changes: 17 additions & 10 deletions src/moov/trak/tkhd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pub struct Tkhd {
pub creation_time: u64,
pub modification_time: u64,
pub track_id: u32,
pub duration: u64,
/// Duration can be indefinite (represented here by None).
pub duration: Option<u64>,
pub layer: u16,
pub alternate_group: u16,
pub enabled: bool,
Expand Down Expand Up @@ -45,7 +46,10 @@ impl AtomExt for Tkhd {
u64::decode(buf)?,
u32::decode(buf)?,
u32::decode(buf)?,
u64::decode(buf)?,
match u64::decode(buf)? {
u64::MAX => None,
duration => Some(duration),
},
),
TkhdVersion::V0 => (
u32::decode(buf)? as u64,
Expand All @@ -54,8 +58,8 @@ impl AtomExt for Tkhd {
u32::decode(buf)?,
// All 1s means unknown; keep it all 1s in 64 bits.
match u32::decode(buf)? {
u32::MAX => u64::MAX,
duration => duration as u64,
u32::MAX => None,
duration => Some(duration as u64),
},
),
};
Expand Down Expand Up @@ -94,7 +98,10 @@ impl AtomExt for Tkhd {
self.modification_time.encode(buf)?;
self.track_id.encode(buf)?;
0u32.encode(buf)?; // reserved
self.duration.encode(buf)?;
match self.duration {
None => u64::MAX.encode(buf)?,
_ => self.duration.encode(buf)?,
};

0u64.encode(buf)?; // reserved
self.layer.encode(buf)?;
Expand Down Expand Up @@ -199,7 +206,7 @@ mod tests {
creation_time: 100,
modification_time: 200,
track_id: 1,
duration: 634634,
duration: Some(634634),
layer: 0,
alternate_group: 0,
volume: 1.into(),
Expand All @@ -224,7 +231,7 @@ mod tests {
creation_time: 100,
modification_time: 200,
track_id: 1,
duration: 634634,
duration: Some(634634),
layer: 0,
alternate_group: 0,
volume: 1.into(),
Expand Down Expand Up @@ -270,20 +277,20 @@ mod tests {
decoded.encode(&mut buf).unwrap();
assert_eq!(buf[8], 1, "re-encoded as version 1");
assert_eq!(buf[36..44], [0xFF; 8], "duration is still unknown");
assert_eq!(decoded.duration, u64::MAX);
assert_eq!(decoded.duration, None);

// One below all 1s is still a finite duration.
let mut finite = raw.to_vec();
finite[31] = 0xFE;
let decoded = Tkhd::decode(&mut finite.as_slice()).unwrap();
assert_eq!(decoded.duration, u32::MAX as u64 - 1);
assert_eq!(decoded.duration, Some(u32::MAX as u64 - 1));
}

#[test]
fn test_tkhd64_duration_u32_max() {
let expected = Tkhd {
track_id: 1,
duration: u32::MAX as u64,
duration: Some(u32::MAX as u64),
enabled: true,
in_movie: true,
..Default::default()
Expand Down
6 changes: 3 additions & 3 deletions src/test/av1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn av1() {
creation_time: 0,
modification_time: 0,
timescale: 1000,
duration: 0,
duration: Some(0),
rate: 1.into(),
volume: 1.into(),
matrix: Matrix {
Expand Down Expand Up @@ -60,7 +60,7 @@ fn av1() {
creation_time: 0,
modification_time: 0,
track_id: 1,
duration: 0,
duration: Some(0),
layer: 0,
alternate_group: 0,
enabled: true,
Expand All @@ -86,7 +86,7 @@ fn av1() {
creation_time: 0,
modification_time: 0,
timescale: 25000,
duration: 0,
duration: Some(0),
language: "und".into()
},
hdlr: Hdlr {
Expand Down
6 changes: 3 additions & 3 deletions src/test/av1_mdta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn av1_mdta() {
creation_time: 0,
modification_time: 0,
timescale: 25000,
duration: 1000,
duration: Some(1000),
rate: 1.into(),
volume: 1.into(),
matrix: Matrix {
Expand All @@ -58,7 +58,7 @@ fn av1_mdta() {
creation_time: 0,
modification_time: 0,
track_id: 1,
duration: 1000,
duration: Some(1000),
layer: 0,
alternate_group: 0,
enabled: true,
Expand Down Expand Up @@ -93,7 +93,7 @@ fn av1_mdta() {
creation_time: 0,
modification_time: 0,
timescale: 25000,
duration: 1000,
duration: Some(1000),
language: "und".into()
},
hdlr: Hdlr {
Expand Down
5 changes: 5 additions & 0 deletions src/test/bbb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn bbb() {
rate: 1.into(),
volume: 1.into(),
next_track_id: 2,
duration: Some(0),
..Default::default()
},
mvex: Some(Mvex {
Expand All @@ -50,12 +51,14 @@ fn bbb() {
in_movie: true,
width: 1280.into(),
height: 720.into(),
duration: Some(0),
..Default::default()
},
mdia: Mdia {
mdhd: Mdhd {
timescale: 24000,
language: "und".into(),
duration: Some(0),
..Default::default()
},
hdlr: Hdlr {
Expand Down Expand Up @@ -133,12 +136,14 @@ fn bbb() {
enabled: true,
in_movie: true,
volume: 1.into(),
duration: Some(0),
..Default::default()
},
mdia: Mdia {
mdhd: Mdhd {
timescale: 44100,
language: "und".into(),
duration: Some(0),
..Default::default()
},
hdlr: Hdlr {
Expand Down
Loading
Loading