mov: fix track duration rescaling
[nihav.git] / nihav-commonfmt / src / demuxers / mov.rs
index 847d494877711700b13769dfc6c07bf6a8b89ba6..ac124faec443190d44b9b8296822c750588ab49d 100644 (file)
@@ -343,11 +343,29 @@ fn read_mdia(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult
 }
 
 const MDIA_CHUNK_HANDLERS: &[TrackChunkHandler] = &[
-    TrackChunkHandler { ctype: mktag!(b"mdhd"), parse: skip_chunk },
+    TrackChunkHandler { ctype: mktag!(b"mdhd"), parse: read_mdhd },
     TrackChunkHandler { ctype: mktag!(b"hdlr"), parse: read_hdlr },
     TrackChunkHandler { ctype: mktag!(b"minf"), parse: read_minf },
 ];
 
+fn read_mdhd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
+    const KNOWN_MDHD_SIZE: u64 = 24;
+    validate!(size >= KNOWN_MDHD_SIZE);
+    let version             = br.read_byte()?;
+    validate!(version == 0);
+    let flags               = br.read_u24be()?;
+    validate!(flags == 0);
+    let _ctime              = br.read_u32be()?;
+    let _mtime              = br.read_u32be()?;
+    track.tb_den            = br.read_u32be()?;
+    validate!(track.tb_den != 0);
+    track.duration          = br.read_u32be()?;
+    let _language           = br.read_u16be()?;
+    let _quality            = br.read_u16be()?;
+
+    Ok(KNOWN_MDHD_SIZE)
+}
+
 fn read_hdlr(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
     const KNOWN_HDLR_SIZE: u64 = 24;
     validate!(size >= KNOWN_HDLR_SIZE);
@@ -683,11 +701,12 @@ fn read_stts(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult
         if let Some(ref mut stream) = track.stream {
             let tb_den = stream.tb_den;
             let (tb_num, tb_den) = reduce_timebase(tb_num * stream.tb_num, tb_den);
-            stream.duration /= u64::from(stream.tb_den / tb_den);
+            stream.duration /= u64::from(track.tb_div);
             stream.tb_num = tb_num;
             stream.tb_den = tb_den;
             track.tb_num = tb_num;
             track.tb_den = tb_den;
+            track.duration /= track.tb_div;
         }
     } else {
         track.time_to_sample.truncate(0);