use structure for timestamp information
authorKostya Shishkov <kostya.shishkov@gmail.com>
Mon, 22 May 2017 14:16:34 +0000 (16:16 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Mon, 22 May 2017 14:16:34 +0000 (16:16 +0200)
src/demuxers/avi.rs
src/demuxers/gdv.rs
src/demuxers/mod.rs
src/frame.rs

index 5a55ff7628bbb6a09499c933e09db69700e5ca8a..e5f688afd6a73a41c39864f362a6e7a4c6b124c4 100644 (file)
@@ -47,6 +47,8 @@ struct AVIDemuxer<'a> {
     size:           usize,
     movi_size:      usize,
     sstate:         StreamState,
+    tb_num:         u32,
+    tb_den:         u32,
 }
 
 #[derive(Debug,Clone,Copy,PartialEq)]
@@ -90,7 +92,9 @@ impl<'a> Demux<'a> for AVIDemuxer<'a> {
             let str = self.dmx.get_stream(stream_no as usize);
             if let None = str { return Err(InvalidData); }
             let stream = str.unwrap();
-            let pkt = self.src.read_packet(stream, Some(self.cur_frame[stream_no as usize]), None, None, false, size)?;
+            let (tb_num, tb_den) = stream.get_timebase();
+            let ts = NATimeInfo::new(Some(self.cur_frame[stream_no as usize]), None, None, tb_num, tb_den);
+            let pkt = self.src.read_packet(stream, ts, false, size)?;
             self.cur_frame[stream_no as usize] += 1;            
             self.movi_size -= size + 8;
 
@@ -115,6 +119,8 @@ impl<'a> AVIDemuxer<'a> {
             size: 0,
             movi_size: 0,
             sstate: StreamState::new(),
+            tb_num: 0,
+            tb_den: 0,
             dmx: Demuxer::new()
         }
     }
@@ -226,8 +232,8 @@ fn parse_strh(dmx: &mut AVIDemuxer, size: usize) -> DemuxerResult<usize> {
     dmx.src.read_skip(2)?; //priority
     dmx.src.read_skip(2)?; //language
     dmx.src.read_skip(4)?; //initial frames
-    let scale = dmx.src.read_u32le()?; //scale
-    let rate = dmx.src.read_u32le()?; //rate
+    dmx.tb_num = dmx.src.read_u32le()?; //scale
+    dmx.tb_den = dmx.src.read_u32le()?; //rate
     dmx.src.read_skip(4)?; //start
     dmx.src.read_skip(4)?; //length
     dmx.src.read_skip(4)?; //buf size
@@ -291,7 +297,7 @@ fn parse_strf_vids(dmx: &mut AVIDemuxer, size: usize) -> DemuxerResult<usize> {
                     Some(name) => name,
                 };
     let vinfo = NACodecInfo::new(cname, vci, edata);
-    let res = dmx.dmx.add_stream(NAStream::new(StreamType::Video, dmx.sstate.strm_no as u32, vinfo));
+    let res = dmx.dmx.add_stream(NAStream::new(StreamType::Video, dmx.sstate.strm_no as u32, vinfo, dmx.tb_num, dmx.tb_den));
     if let None = res { return Err(MemoryError); }
     dmx.sstate.reset();
     Ok(size)
@@ -315,7 +321,7 @@ fn parse_strf_auds(dmx: &mut AVIDemuxer, size: usize) -> DemuxerResult<usize> {
                     Some(name) => name,
                 };
     let ainfo = NACodecInfo::new(cname, NACodecTypeInfo::Audio(ahdr), edata);
-    let res = dmx.dmx.add_stream(NAStream::new(StreamType::Audio, dmx.sstate.strm_no as u32, ainfo));
+    let res = dmx.dmx.add_stream(NAStream::new(StreamType::Audio, dmx.sstate.strm_no as u32, ainfo, dmx.tb_num, dmx.tb_den));
     if let None = res { return Err(MemoryError); }
     dmx.sstate.reset();
     Ok(size)
@@ -324,7 +330,7 @@ fn parse_strf_auds(dmx: &mut AVIDemuxer, size: usize) -> DemuxerResult<usize> {
 fn parse_strf_xxxx(dmx: &mut AVIDemuxer, size: usize) -> DemuxerResult<usize> {
     let edata = dmx.read_extradata(size)?;
     let info = NACodecInfo::new("unknown", NACodecTypeInfo::None, edata);
-    let res = dmx.dmx.add_stream(NAStream::new(StreamType::Data, dmx.sstate.strm_no as u32, info));
+    let res = dmx.dmx.add_stream(NAStream::new(StreamType::Data, dmx.sstate.strm_no as u32, info, dmx.tb_num, dmx.tb_den));
     if let None = res { return Err(MemoryError); }
     dmx.sstate.reset();
     Ok(size)
index 9cd62ecac0fec4b42eb97d6bd1fbf796b3a164f4..9289cd41fcc8d888f8dbc051be20e41b7836872c 100644 (file)
@@ -44,13 +44,13 @@ impl<'a> Demux<'a> for GremlinVideoDemuxer<'a> {
             let vhdr = NAVideoInfo::new(width as usize, height as usize, false, PAL8_FORMAT);
             let vci = NACodecTypeInfo::Video(vhdr);
             let vinfo = NACodecInfo::new("gdv-video", vci, None);
-            self.v_id = self.dmx.add_stream(NAStream::new(StreamType::Video, 0, vinfo));
+            self.v_id = self.dmx.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, fps as u32));
         }
         if (aflags & 1) != 0 {
             let channels = if (aflags & 2) != 0 { 2 } else { 1 };
             let ahdr = NAAudioInfo::new(rate as u32, channels as u8, if (aflags & 4) != 0 { SND_S16_FORMAT } else { SND_U8_FORMAT }, 2);
             let ainfo = NACodecInfo::new("gdv-audio", NACodecTypeInfo::Audio(ahdr), None);
-            self.a_id = self.dmx.add_stream(NAStream::new(StreamType::Audio, 1, ainfo));
+            self.a_id = self.dmx.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, rate as u32));
 
             let packed = if (aflags & 8) != 0 { 1 } else { 0 };
             self.asize = (((rate / fps) * channels * (if (aflags & 4) != 0 { 2 } else { 1 })) >> packed) as usize;
@@ -109,7 +109,9 @@ pktdta: Vec::new(),
     fn read_achunk(&mut self) -> DemuxerResult<NAPacket> {
         self.state = GDVState::AudioRead;
         let str = self.dmx.get_stream(self.a_id.unwrap()).unwrap();
-        self.src.read_packet(str, Some(self.cur_frame as u64), None, None, true, self.asize)
+        let (tb_num, tb_den) = str.get_timebase();
+        let ts = NATimeInfo::new(Some(self.cur_frame as u64), None, None, tb_num, tb_den);
+        self.src.read_packet(str, ts, true, self.asize)
     }
 
     fn read_vchunk(&mut self) -> DemuxerResult<NAPacket> {
@@ -122,7 +124,9 @@ pktdta: Vec::new(),
         let flags = (tmp & 0xFF) as usize;
         self.state = GDVState::NewFrame;
         self.cur_frame = self.cur_frame + 1;
-        src.read_packet(str, Some((self.cur_frame - 1) as u64), None, None, if (flags & 64) != 0 { true } else { false }, size)
+        let (tb_num, tb_den) = str.get_timebase();
+        let ts = NATimeInfo::new(Some((self.cur_frame - 1) as u64), None, None, tb_num, tb_den);
+        src.read_packet(str, ts, if (flags & 64) != 0 { true } else { false }, size)
     }
 }
 
index ac7ae3827386fb9498851a140ec491a7f43ea3fb..aaf92e4cf36d421b8eeb3befabb7e2aa20a884cd 100644 (file)
@@ -29,18 +29,18 @@ pub trait Demux<'a> {
 }
 
 pub trait NAPacketReader {
-    fn read_packet(&mut self, str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>;
+    fn read_packet(&mut self, str: Rc<NAStream>, ts: NATimeInfo, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>;
     fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>;
 }
 
 impl<'a> NAPacketReader for ByteReader<'a> {
-    fn read_packet(&mut self, str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, size: usize) -> DemuxerResult<NAPacket> {
+    fn read_packet(&mut self, str: Rc<NAStream>, ts: NATimeInfo, kf: bool, size: usize) -> DemuxerResult<NAPacket> {
         let mut buf: Vec<u8> = Vec::with_capacity(size);
         if buf.capacity() < size { return Err(DemuxerError::MemoryError); }
         buf.resize(size, 0);
         let res = self.read_buf(buf.as_mut_slice());
         if let Err(_) = res { return Err(DemuxerError::IOError); }
-        let pkt = NAPacket::new(str, pts, dts, dur, kf, buf);
+        let pkt = NAPacket::new(str, ts, kf, buf);
         Ok(pkt)
     }
     fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> {
index d935a0c17aec3e3c418846ee86ffc2b5651f136c..19c1733c9c716b7101f199d9301e62931f68c5e5 100644 (file)
@@ -90,24 +90,7 @@ impl fmt::Display for NACodecTypeInfo {
 
 pub type BufferRef = Rc<RefCell<Vec<u8>>>;
 
-#[allow(dead_code)]
-#[derive(Clone)]
-pub struct NABuffer {
-    id:   u64,
-    data: BufferRef,
-}
-
-impl Drop for NABuffer {
-    fn drop(&mut self) { }
-}
-
-impl NABuffer {
-    pub fn get_data(&self) -> Ref<Vec<u8>> { self.data.borrow() }
-    pub fn get_data_mut(&mut self) -> RefMut<Vec<u8>> { self.data.borrow_mut() }
-}
-
 pub type NABufferRefT<T> = Rc<RefCell<Vec<T>>>;
-pub type NABufferRef = Rc<RefCell<NABuffer>>;
 
 #[derive(Clone)]
 pub struct NAVideoBuffer<T> {
@@ -406,12 +389,31 @@ impl fmt::Display for FrameType {
     }
 }
 
-#[allow(dead_code)]
-#[derive(Clone)]
-pub struct NAFrame {
+#[derive(Debug,Clone,Copy)]
+pub struct NATimeInfo {
     pts:            Option<u64>,
     dts:            Option<u64>,
     duration:       Option<u64>,
+    tb_num:         u32,
+    tb_den:         u32,
+}
+
+impl NATimeInfo {
+    pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self {
+        NATimeInfo { pts: pts, dts: dts, duration: duration, tb_num: tb_num, tb_den: tb_den }
+    }
+    pub fn get_pts(&self) -> Option<u64> { self.pts }
+    pub fn get_dts(&self) -> Option<u64> { self.dts }
+    pub fn get_duration(&self) -> Option<u64> { self.duration }
+    pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
+    pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
+    pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
+}
+
+#[allow(dead_code)]
+#[derive(Clone)]
+pub struct NAFrame {
+    ts:             NATimeInfo,
     buffer:         NABufferType,
     info:           Rc<NACodecInfo>,
     ftype:          FrameType,
@@ -431,26 +433,25 @@ fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
 }
 
 impl NAFrame {
-    pub fn new(pts:            Option<u64>,
-               dts:            Option<u64>,
-               duration:       Option<u64>,
+    pub fn new(ts:             NATimeInfo,
                ftype:          FrameType,
                keyframe:       bool,
                info:           Rc<NACodecInfo>,
                options:        HashMap<String, NAValue>,
                buffer:         NABufferType) -> Self {
-        NAFrame { pts: pts, dts: dts, duration: duration, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options }
+        NAFrame { ts: ts, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options }
     }
-    pub fn get_pts(&self) -> Option<u64> { self.pts }
-    pub fn get_dts(&self) -> Option<u64> { self.dts }
-    pub fn get_duration(&self) -> Option<u64> { self.duration }
     pub fn get_frame_type(&self) -> FrameType { self.ftype }
     pub fn is_keyframe(&self) -> bool { self.key }
-    pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
-    pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
-    pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
     pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; }
     pub fn set_keyframe(&mut self, key: bool) { self.key = key; }
+    pub fn get_time_information(&self) -> NATimeInfo { self.ts }
+    pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
+    pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
+    pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
+    pub fn set_pts(&mut self, pts: Option<u64>) { self.ts.set_pts(pts); }
+    pub fn set_dts(&mut self, dts: Option<u64>) { self.ts.set_dts(dts); }
+    pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); }
 
     pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
 }
@@ -458,9 +459,9 @@ impl NAFrame {
 impl fmt::Display for NAFrame {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         let mut foo = format!("frame type {}", self.ftype);
-        if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); }
-        if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); }
-        if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); }
+        if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
+        if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
+        if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
         if self.key { foo = format!("{} kf", foo); }
         write!(f, "[{}]", foo)
     }
@@ -501,45 +502,68 @@ pub struct NAStream {
     id:             u32,
     num:            usize,
     info:           Rc<NACodecInfo>,
+    tb_num:         u32,
+    tb_den:         u32,
+}
+
+pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) {
+    if tb_num == 0 { return (tb_num, tb_den); }
+    if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); }
+
+    let mut a = tb_num;
+    let mut b = tb_den;
+
+    while a != b {
+        if a > b { a -= b; }
+        else if b > a { b -= a; }
+    }
+
+    (tb_num / a, tb_den / a)
 }
 
 impl NAStream {
-    pub fn new(mt: StreamType, id: u32, info: NACodecInfo) -> Self {
-        NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info) }
+    pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self {
+        let (n, d) = reduce_timebase(tb_num, tb_den);
+        NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info), tb_num: n, tb_den: d }
     }
     pub fn get_id(&self) -> u32 { self.id }
     pub fn get_num(&self) -> usize { self.num }
     pub fn set_num(&mut self, num: usize) { self.num = num; }
     pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
+    pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) }
+    pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) {
+        let (n, d) = reduce_timebase(tb_num, tb_den);
+        self.tb_num = n;
+        self.tb_den = d;
+    }
 }
 
 impl fmt::Display for NAStream {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "({}#{} - {})", self.media_type, self.id, self.info.get_properties())
+        write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties())
     }
 }
 
 #[allow(dead_code)]
 pub struct NAPacket {
     stream:         Rc<NAStream>,
-    pts:            Option<u64>,
-    dts:            Option<u64>,
-    duration:       Option<u64>,
+    ts:             NATimeInfo,
     buffer:         Rc<Vec<u8>>,
     keyframe:       bool,
 //    options:        HashMap<String, NAValue<'a>>,
 }
 
 impl NAPacket {
-    pub fn new(str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, vec: Vec<u8>) -> Self {
+    pub fn new(str: Rc<NAStream>, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
 //        let mut vec: Vec<u8> = Vec::new();
 //        vec.resize(size, 0);
-        NAPacket { stream: str, pts: pts, dts: dts, duration: dur, keyframe: kf, buffer: Rc::new(vec) }
+        NAPacket { stream: str, ts: ts, keyframe: kf, buffer: Rc::new(vec) }
     }
     pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() }
-    pub fn get_pts(&self) -> Option<u64> { self.pts }
-    pub fn get_dts(&self) -> Option<u64> { self.dts }
-    pub fn get_duration(&self) -> Option<u64> { self.duration }
+    pub fn get_time_information(&self) -> NATimeInfo { self.ts }
+    pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
+    pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
+    pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
     pub fn is_keyframe(&self) -> bool { self.keyframe }
     pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() }
 }
@@ -551,9 +575,9 @@ impl Drop for NAPacket {
 impl fmt::Display for NAPacket {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
-        if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); }
-        if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); }
-        if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); }
+        if let Some(pts) = self.ts.pts { foo = format!("{} pts {}", foo, pts); }
+        if let Some(dts) = self.ts.dts { foo = format!("{} dts {}", foo, dts); }
+        if let Some(dur) = self.ts.duration { foo = format!("{} duration {}", foo, dur); }
         if self.keyframe { foo = format!("{} kf", foo); }
         foo = foo + "]";
         write!(f, "{}", foo)
@@ -567,12 +591,10 @@ pub trait FrameFromPacket {
 
 impl FrameFromPacket for NAFrame {
     fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame {
-        NAFrame::new(pkt.pts, pkt.dts, pkt.duration, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf)
+        NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf)
     }
     fn fill_timestamps(&mut self, pkt: &NAPacket) {
-        self.set_pts(pkt.pts);
-        self.set_dts(pkt.dts);
-        self.set_duration(pkt.duration);
+        self.ts = pkt.get_time_information();
     }
 }