use raw stream duration
[nihav-encoder.git] / src / demux.rs
index dbc1447a0eddb4e76ca83e005a2f90fca7618ccf..859faf5acad11c99716f1826c2e845103c91bc35 100644 (file)
@@ -41,13 +41,14 @@ pub struct RawStreamCtx<'a> {
     sm:         StreamManager,
     packetiser: Box<dyn NAPacketiser + Send>,
     br:         &'a mut ByteReader<'a>,
+    pts:        u64,
 }
 
 impl<'a> RawStreamCtx<'a> {
     fn new(stream: NAStreamRef, packetiser: Box<dyn NAPacketiser + Send>, br: &'a mut ByteReader<'a>) -> Self {
         let mut sm = StreamManager::new();
         sm.add_stream_ref(stream.clone());
-        Self { stream, sm, packetiser, br }
+        Self { stream, sm, packetiser, br, pts: 0 }
     }
 }
 
@@ -146,13 +147,16 @@ impl<'a> DemuxerObject<'a> {
             _ => false,
         }
     }
-    /*pub fn get_duration(&self) -> u64 {
+    pub fn get_duration(&self) -> u64 {
         match *self {
             DemuxerObject::Normal(ref dmx) => dmx.get_duration(),
             DemuxerObject::Raw(ref dmx, _, _) => dmx.get_duration(),
+            DemuxerObject::RawStream(ref ctx) => {
+                NATimeInfo::ts_to_time(ctx.stream.duration, 1000, ctx.stream.tb_num, ctx.stream.tb_den)
+            },
             _ => 0,
         }
-    }*/
+    }
     pub fn get_num_streams(&self) -> usize {
         match *self {
             DemuxerObject::None => 0,
@@ -230,7 +234,13 @@ impl<'a> DemuxerObject<'a> {
                 let mut buf = [0; 65536];
                 loop {
                     match ctx.packetiser.get_packet(ctx.stream.clone()) {
-                        Ok(Some(packet)) => return Ok(packet),
+                        Ok(Some(mut packet)) => {
+                            if packet.get_pts().is_none() && packet.get_duration().is_some() {
+                                packet.ts.pts = Some(ctx.pts);
+                            }
+                            ctx.pts += packet.get_duration().unwrap_or(0);
+                            return Ok(packet);
+                        },
                         Ok(None) => {},
                         Err(DecoderError::ShortData) => {},
                         _ => return Err(DemuxerError::InvalidData),
@@ -241,7 +251,13 @@ impl<'a> DemuxerObject<'a> {
                         },
                         Err(_) => {
                             match ctx.packetiser.get_packet(ctx.stream.clone()) {
-                                Ok(Some(packet)) => return Ok(packet),
+                                Ok(Some(mut packet)) => {
+                                    if packet.get_pts().is_none() && packet.get_duration().is_some() {
+                                        packet.ts.pts = Some(ctx.pts);
+                                    }
+                                    ctx.pts += packet.get_duration().unwrap_or(0);
+                                    return Ok(packet);
+                                },
                                 Ok(None) | Err(DecoderError::ShortData) => return Err(DemuxerError::EOF),
                                 _ => return Err(DemuxerError::InvalidData),
                             };