From ff81b133352eaa453e9c6d0fae705b5c8efe1e4e Mon Sep 17 00:00:00 2001 From: Brad Hards Date: Sun, 15 Nov 2026 09:57:53 +1100 Subject: [PATCH] fix!: modify the duration in mvhd, tkhd, mdhd to indicate unknown This builds on work by @youdie006 to fix the inconsistency (see https://github.com/kixelated/mp4-atom/pull/266 ) in handling between V0 and V1 for these boxes. The API is easier to get right (or at least harder to get wrong) with explicit notation of "unknown", and it gives us a pathway to encoding to V0 when that would fit everything. --- src/moov/mod.rs | 3 ++- src/moov/mvhd.rs | 30 ++++++++++++++++++------------ src/moov/trak/mdia/mdhd.rs | 27 +++++++++++++++++---------- src/moov/trak/tkhd.rs | 27 +++++++++++++++++---------- src/test/av1.rs | 6 +++--- src/test/av1_mdta.rs | 6 +++--- src/test/bbb.rs | 5 +++++ src/test/esds.rs | 5 +++++ src/test/flac.rs | 6 +++--- src/test/h264.rs | 10 +++++----- src/test/hevc.rs | 6 +++--- src/test/libavif_anim.rs | 6 +++--- src/test/mebx.rs | 10 +++++----- src/test/uncompressed.rs | 6 +++--- src/test/vp9.rs | 6 +++--- 15 files changed, 95 insertions(+), 64 deletions(-) diff --git a/src/moov/mod.rs b/src/moov/mod.rs index 5e624a25..16beb8a2 100644 --- a/src/moov/mod.rs +++ b/src/moov/mod.rs @@ -166,6 +166,7 @@ mod test { in_movie: true, width: 1920.into(), height: 1080.into(), + duration: Some(0), ..Default::default() }, edts: Some(Edts { @@ -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 { diff --git a/src/moov/mvhd.rs b/src/moov/mvhd.rs index bb3dbdf7..18d53e7f 100644 --- a/src/moov/mvhd.rs +++ b/src/moov/mvhd.rs @@ -12,7 +12,7 @@ pub struct Mvhd { pub creation_time: u64, pub modification_time: u64, pub timescale: u32, - pub duration: u64, + pub duration: Option, pub rate: FixedPoint, pub volume: FixedPoint, @@ -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), }, ), }; @@ -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)?; @@ -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(), @@ -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(), @@ -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(), @@ -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(); diff --git a/src/moov/trak/mdia/mdhd.rs b/src/moov/trak/mdia/mdhd.rs index 2f85b756..a81b96e7 100644 --- a/src/moov/trak/mdia/mdhd.rs +++ b/src/moov/trak/mdia/mdhd.rs @@ -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, pub language: String, } @@ -27,7 +28,10 @@ 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, @@ -35,8 +39,8 @@ impl AtomExt for Mdhd { 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), }, ), }; @@ -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)?; @@ -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(); @@ -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(); @@ -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() }; diff --git a/src/moov/trak/tkhd.rs b/src/moov/trak/tkhd.rs index 0c231556..af640cea 100644 --- a/src/moov/trak/tkhd.rs +++ b/src/moov/trak/tkhd.rs @@ -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, pub layer: u16, pub alternate_group: u16, pub enabled: bool, @@ -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, @@ -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), }, ), }; @@ -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)?; @@ -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(), @@ -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(), @@ -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() diff --git a/src/test/av1.rs b/src/test/av1.rs index 8946f5d3..ba5f904f 100644 --- a/src/test/av1.rs +++ b/src/test/av1.rs @@ -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 { @@ -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, @@ -86,7 +86,7 @@ fn av1() { creation_time: 0, modification_time: 0, timescale: 25000, - duration: 0, + duration: Some(0), language: "und".into() }, hdlr: Hdlr { diff --git a/src/test/av1_mdta.rs b/src/test/av1_mdta.rs index 79a7b37e..76cf447b 100644 --- a/src/test/av1_mdta.rs +++ b/src/test/av1_mdta.rs @@ -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 { @@ -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, @@ -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 { diff --git a/src/test/bbb.rs b/src/test/bbb.rs index cb50f765..dfdb51a9 100644 --- a/src/test/bbb.rs +++ b/src/test/bbb.rs @@ -26,6 +26,7 @@ fn bbb() { rate: 1.into(), volume: 1.into(), next_track_id: 2, + duration: Some(0), ..Default::default() }, mvex: Some(Mvex { @@ -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 { @@ -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 { diff --git a/src/test/esds.rs b/src/test/esds.rs index 85f08228..5f0ab821 100644 --- a/src/test/esds.rs +++ b/src/test/esds.rs @@ -26,6 +26,7 @@ fn esds() { rate: 1.into(), volume: 1.into(), next_track_id: 2, + duration: Some(0), ..Default::default() }, mvex: Some(Mvex { @@ -50,12 +51,14 @@ fn esds() { in_movie: true, width: 1280.into(), height: 720.into(), + duration: Some(0), ..Default::default() }, mdia: Mdia { mdhd: Mdhd { timescale: 12288, language: "und".into(), + duration: Some(0), ..Default::default() }, hdlr: Hdlr { @@ -133,12 +136,14 @@ fn esds() { 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 { diff --git a/src/test/flac.rs b/src/test/flac.rs index e26f2f76..bc69a09a 100644 --- a/src/test/flac.rs +++ b/src/test/flac.rs @@ -25,7 +25,7 @@ fn flac() { creation_time: 3840517353, modification_time: 3840517353, timescale: 44100, - duration: 0, + duration: Some(0), rate: 1.into(), volume: 1.into(), matrix: Matrix { @@ -56,7 +56,7 @@ fn flac() { creation_time: 3840517353, modification_time: 3840517353, track_id: 1, - duration: 0, + duration: Some(0), layer: 0, alternate_group: 0, enabled: true, @@ -82,7 +82,7 @@ fn flac() { creation_time: 3840517353, modification_time: 3840517353, timescale: 44100, - duration: 0, + duration: Some(0), language: "und".into(), }, hdlr: Hdlr { diff --git a/src/test/h264.rs b/src/test/h264.rs index 469305e3..ed902599 100644 --- a/src/test/h264.rs +++ b/src/test/h264.rs @@ -24,7 +24,7 @@ fn avcc_ext() { creation_time: 0, modification_time: 0, timescale: 1000, - duration: 0, + duration: Some(0), rate: 1.into(), volume: 1.into(), matrix: Matrix { @@ -65,7 +65,7 @@ fn avcc_ext() { creation_time: 0, modification_time: 0, track_id: 1, - duration: 0, + duration: Some(0), layer: 0, alternate_group: 0, enabled: true, @@ -91,7 +91,7 @@ fn avcc_ext() { creation_time: 0, modification_time: 0, timescale: 15360, - duration: 0, + duration: Some(0), language: "und".into(), }, hdlr: Hdlr { @@ -188,7 +188,7 @@ fn avcc_ext() { creation_time: 0, modification_time: 0, track_id: 2, - duration: 0, + duration: Some(0), layer: 0, alternate_group: 1, enabled: true, @@ -214,7 +214,7 @@ fn avcc_ext() { creation_time: 0, modification_time: 0, timescale: 48000, - duration: 0, + duration: Some(0), language: "und".into(), }, hdlr: Hdlr { diff --git a/src/test/hevc.rs b/src/test/hevc.rs index b529819c..dafafb2b 100644 --- a/src/test/hevc.rs +++ b/src/test/hevc.rs @@ -24,7 +24,7 @@ fn hevc() { creation_time: 0, modification_time: 0, timescale: 1000, - duration: 0, + duration: Some(0), rate: 1.into(), volume: 1.into(), matrix: Matrix { @@ -55,7 +55,7 @@ fn hevc() { creation_time: 0, modification_time: 0, track_id: 1, - duration: 0, + duration: Some(0), layer: 0, alternate_group: 0, enabled: true, @@ -81,7 +81,7 @@ fn hevc() { creation_time: 0, modification_time: 0, timescale: 15360, - duration: 0, + duration: Some(0), language: "und".into() }, hdlr: Hdlr { diff --git a/src/test/libavif_anim.rs b/src/test/libavif_anim.rs index 85d1b4d4..7747acc6 100644 --- a/src/test/libavif_anim.rs +++ b/src/test/libavif_anim.rs @@ -133,7 +133,7 @@ fn av1_anim() { creation_time: 3852424385, modification_time: 3852424385, timescale: 25, - duration: 18446744073709551615, + duration: None, rate: 1.into(), volume: 1.into(), matrix: Matrix { @@ -156,7 +156,7 @@ fn av1_anim() { creation_time: 3852424385, modification_time: 3852424385, track_id: 1, - duration: 18446744073709551615, + duration: None, layer: 0, alternate_group: 0, enabled: true, @@ -191,7 +191,7 @@ fn av1_anim() { creation_time: 3852424385, modification_time: 3852424385, timescale: 25, - duration: 2, + duration: Some(2), language: "und".into() }, hdlr: Hdlr { diff --git a/src/test/mebx.rs b/src/test/mebx.rs index f9c48a39..14c9d743 100644 --- a/src/test/mebx.rs +++ b/src/test/mebx.rs @@ -55,7 +55,7 @@ fn mebx() { creation_time: 3732765738, modification_time: 3732765738, timescale: 600, - duration: 600, + duration: Some(600), rate: 1.into(), volume: 1.into(), matrix: Matrix { @@ -77,7 +77,7 @@ fn mebx() { creation_time: 3732765738, modification_time: 3732765738, track_id: 1, - duration: 600, + duration: Some(600), layer: 0, alternate_group: 0, enabled: true, @@ -103,7 +103,7 @@ fn mebx() { creation_time: 3732765738, modification_time: 3732765738, timescale: 30000, - duration: 30000, + duration: Some(30000), language: "und".into(), }, hdlr: Hdlr { @@ -232,7 +232,7 @@ fn mebx() { creation_time: 3732765738, modification_time: 3732765738, track_id: 2, - duration: 600, + duration: Some(600), layer: 0, alternate_group: 0, enabled: true, @@ -258,7 +258,7 @@ fn mebx() { creation_time: 3732765738, modification_time: 3732765738, timescale: 30000, - duration: 30000, + duration: Some(30000), language: "und".into(), }, hdlr: Hdlr { diff --git a/src/test/uncompressed.rs b/src/test/uncompressed.rs index ec4c5587..c5526e63 100644 --- a/src/test/uncompressed.rs +++ b/src/test/uncompressed.rs @@ -28,7 +28,7 @@ fn uncompressed() { creation_time: 3827291266, modification_time: 3827291266, timescale: 600, - duration: 1200, + duration: Some(1200), rate: 1.into(), volume: 1.into(), matrix: Matrix { @@ -49,7 +49,7 @@ fn uncompressed() { creation_time: 3827291266, modification_time: 3827291266, track_id: 1, - duration: 1200, + duration: Some(1200), layer: 0, alternate_group: 0, enabled: true, @@ -75,7 +75,7 @@ fn uncompressed() { creation_time: 3827291266, modification_time: 3827291266, timescale: 1, - duration: 2, + duration: Some(2), language: "und".to_string(), }, hdlr: Hdlr { diff --git a/src/test/vp9.rs b/src/test/vp9.rs index c3fa1d86..a25070e5 100644 --- a/src/test/vp9.rs +++ b/src/test/vp9.rs @@ -31,7 +31,7 @@ fn vp9() { creation_time: 3576083626, modification_time: 3576083626, timescale: 1000000, - duration: 0, + duration: Some(0), rate: 1.into(), volume: 1.into(), matrix: Matrix { @@ -80,7 +80,7 @@ fn vp9() { creation_time: 3576083626, modification_time: 3576083626, track_id: 1, - duration: 0, + duration: Some(0), layer: 0, alternate_group: 0, enabled: true, @@ -106,7 +106,7 @@ fn vp9() { creation_time: 3576083626, modification_time: 3576083626, timescale: 1000000, - duration: 0, + duration: Some(0), language: "und".to_string() }, hdlr: Hdlr {