]> git.nihav.org Git - nihav.git/blobdiff - src/frame.rs
remove panics in working tests
[nihav.git] / src / frame.rs
index c086339bf3dd0299f7696cadd3a09798f0711010..628efb6eb75aff1606a5ea7097a85181b7831148 100644 (file)
@@ -1,3 +1,4 @@
+use std::cmp::max;
 use std::collections::HashMap;
 use std::fmt;
 use std::rc::Rc;
@@ -46,6 +47,8 @@ impl NAVideoInfo {
     pub fn get_height(&self) -> usize { self.height as usize }
     pub fn is_flipped(&self) -> bool { self.flipped }
     pub fn get_format(&self) -> NAPixelFormaton { self.format }
+    pub fn set_width(&mut self, w: usize)  { self.width  = w; }
+    pub fn set_height(&mut self, h: usize) { self.height = h; }
 }
 
 impl fmt::Display for NAVideoInfo {
@@ -61,6 +64,33 @@ pub enum NACodecTypeInfo {
     Video(NAVideoInfo),
 }
 
+impl NACodecTypeInfo {
+    pub fn get_video_info(&self) -> Option<NAVideoInfo> {
+        match *self {
+            NACodecTypeInfo::Video(vinfo) => Some(vinfo),
+            _ => None,
+        }
+    }
+    pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
+        match *self {
+            NACodecTypeInfo::Audio(ainfo) => Some(ainfo),
+            _ => None,
+        }
+    }
+    pub fn is_video(&self) -> bool {
+        match *self {
+            NACodecTypeInfo::Video(_) => true,
+            _ => false,
+        }
+    }
+    pub fn is_audio(&self) -> bool {
+        match *self {
+            NACodecTypeInfo::Audio(_) => true,
+            _ => false,
+        }
+    }
+}
+
 impl fmt::Display for NACodecTypeInfo {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         let ret = match *self {
@@ -72,25 +102,292 @@ impl fmt::Display for NACodecTypeInfo {
     }
 }
 
-pub type BufferRef = Rc<RefCell<Vec<u8>>>;
+pub type NABufferRefT<T> = Rc<RefCell<Vec<T>>>;
 
-#[allow(dead_code)]
 #[derive(Clone)]
-pub struct NABuffer {
-    id:   u64,
-    data: BufferRef,
+pub struct NAVideoBuffer<T> {
+    info:    NAVideoInfo,
+    data:    NABufferRefT<T>,
+    offs:    Vec<usize>,
+    strides: Vec<usize>,
+}
+
+impl<T: Clone> NAVideoBuffer<T> {
+    pub fn get_offset(&self, idx: usize) -> usize {
+        if idx >= self.offs.len() { 0 }
+        else { self.offs[idx] }
+    }
+    pub fn get_info(&self) -> NAVideoInfo { self.info }
+    pub fn get_data(&self) -> Ref<Vec<T>> { self.data.borrow() }
+    pub fn get_data_mut(&mut self) -> RefMut<Vec<T>> { self.data.borrow_mut() }
+    pub fn copy_buffer(&mut self) -> Self {
+        let mut data: Vec<T> = Vec::with_capacity(self.data.borrow().len());
+        data.clone_from(self.data.borrow().as_ref());
+        let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
+        offs.clone_from(&self.offs);
+        let mut strides: Vec<usize> = Vec::with_capacity(self.strides.len());
+        strides.clone_from(&self.strides);
+        NAVideoBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, strides: strides }
+    }
+    pub fn get_stride(&self, idx: usize) -> usize {
+        if idx >= self.strides.len() { return 0; }
+        self.strides[idx]
+    }
+    pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
+        get_plane_size(&self.info, idx)
+    }
+}
+
+#[derive(Clone)]
+pub struct NAAudioBuffer<T> {
+    info:   NAAudioInfo,
+    data:   NABufferRefT<T>,
+    offs:   Vec<usize>,
+    chmap:  NAChannelMap,
+    len:    usize,
+}
+
+impl<T: Clone> NAAudioBuffer<T> {
+    pub fn get_offset(&self, idx: usize) -> usize {
+        if idx >= self.offs.len() { 0 }
+        else { self.offs[idx] }
+    }
+    pub fn get_info(&self) -> NAAudioInfo { self.info }
+    pub fn get_chmap(&self) -> NAChannelMap { self.chmap.clone() }
+    pub fn get_data(&self) -> Ref<Vec<T>> { self.data.borrow() }
+    pub fn get_data_mut(&mut self) -> RefMut<Vec<T>> { self.data.borrow_mut() }
+    pub fn copy_buffer(&mut self) -> Self {
+        let mut data: Vec<T> = Vec::with_capacity(self.data.borrow().len());
+        data.clone_from(self.data.borrow().as_ref());
+        let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
+        offs.clone_from(&self.offs);
+        NAAudioBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: offs, chmap: self.get_chmap(), len: self.len }
+    }
+    pub fn get_length(&self) -> usize { self.len }
+}
+
+impl NAAudioBuffer<u8> {
+    pub fn new_from_buf(info: NAAudioInfo, data: NABufferRefT<u8>, chmap: NAChannelMap) -> Self {
+        let len = data.borrow().len();
+        NAAudioBuffer { info: info, data: data, chmap: chmap, offs: Vec::new(), len: len }
+    }
+}
+
+#[derive(Clone)]
+pub enum NABufferType {
+    Video      (NAVideoBuffer<u8>),
+    Video16    (NAVideoBuffer<u16>),
+    VideoPacked(NAVideoBuffer<u8>),
+    AudioU8    (NAAudioBuffer<u8>),
+    AudioI16   (NAAudioBuffer<i16>),
+    AudioI32   (NAAudioBuffer<i32>),
+    AudioF32   (NAAudioBuffer<f32>),
+    AudioPacked(NAAudioBuffer<u8>),
+    Data       (NABufferRefT<u8>),
+    None,
+}
+
+impl NABufferType {
+    pub fn get_offset(&self, idx: usize) -> usize {
+        match *self {
+            NABufferType::Video(ref vb)       => vb.get_offset(idx),
+            NABufferType::Video16(ref vb)     => vb.get_offset(idx),
+            NABufferType::VideoPacked(ref vb) => vb.get_offset(idx),
+            NABufferType::AudioU8(ref ab)     => ab.get_offset(idx),
+            NABufferType::AudioI16(ref ab)    => ab.get_offset(idx),
+            NABufferType::AudioF32(ref ab)    => ab.get_offset(idx),
+            NABufferType::AudioPacked(ref ab) => ab.get_offset(idx),
+            _ => 0,
+        }
+    }
+    pub fn get_vbuf(&mut self) -> Option<NAVideoBuffer<u8>> {
+        match *self {
+            NABufferType::Video(ref vb)       => Some(vb.clone()),
+            NABufferType::VideoPacked(ref vb) => Some(vb.clone()),
+            _ => None,
+        }
+    }
+    pub fn get_vbuf16(&mut self) -> Option<NAVideoBuffer<u16>> {
+        match *self {
+            NABufferType::Video16(ref vb)     => Some(vb.clone()),
+            _ => None,
+        }
+    }
+    pub fn get_abuf_u8(&mut self) -> Option<NAAudioBuffer<u8>> {
+        match *self {
+            NABufferType::AudioU8(ref ab) => Some(ab.clone()),
+            NABufferType::AudioPacked(ref ab) => Some(ab.clone()),
+            _ => None,
+        }
+    }
+    pub fn get_abuf_i16(&mut self) -> Option<NAAudioBuffer<i16>> {
+        match *self {
+            NABufferType::AudioI16(ref ab) => Some(ab.clone()),
+            _ => None,
+        }
+    }
+    pub fn get_abuf_i32(&mut self) -> Option<NAAudioBuffer<i32>> {
+        match *self {
+            NABufferType::AudioI32(ref ab) => Some(ab.clone()),
+            _ => None,
+        }
+    }
+    pub fn get_abuf_f32(&mut self) -> Option<NAAudioBuffer<f32>> {
+        match *self {
+            NABufferType::AudioF32(ref ab) => Some(ab.clone()),
+            _ => None,
+        }
+    }
+}
+
+#[derive(Debug,Clone,Copy,PartialEq)]
+pub enum AllocatorError {
+    TooLargeDimensions,
+    FormatError,
+}
+
+pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType, AllocatorError> {
+    let fmt = &vinfo.format;
+    let mut new_size: usize = 0;
+    let mut offs:    Vec<usize> = Vec::new();
+    let mut strides: Vec<usize> = Vec::new();
+
+    for i in 0..fmt.get_num_comp() {
+        if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); }
+    }
+
+    let align_mod = ((1 << align) as usize) - 1;
+    let width  = ((vinfo.width  as usize) + align_mod) & !align_mod;
+    let height = ((vinfo.height as usize) + align_mod) & !align_mod;
+    let mut max_depth = 0;
+    let mut all_packed = true;
+    for i in 0..fmt.get_num_comp() {
+        let ochr = fmt.get_chromaton(i);
+        if let None = ochr { continue; }
+        let chr = ochr.unwrap();
+        if !chr.is_packed() {
+            all_packed = false;
+            break;
+        }
+        max_depth = max(max_depth, chr.get_depth());
+    }
+
+//todo semi-packed like NV12
+    if fmt.is_paletted() {
+//todo various-sized palettes?
+        let stride = vinfo.get_format().get_chromaton(0).unwrap().get_linesize(width);
+        let pic_sz = stride.checked_mul(height);
+        if pic_sz == None { return Err(AllocatorError::TooLargeDimensions); }
+        let pal_size = 256 * (fmt.get_elem_size() as usize);
+        let new_size = pic_sz.unwrap().checked_add(pal_size);
+        if new_size == None { return Err(AllocatorError::TooLargeDimensions); }
+        offs.push(0);
+        offs.push(stride * height);
+        strides.push(stride);
+        let mut data: Vec<u8> = Vec::with_capacity(new_size.unwrap());
+        data.resize(new_size.unwrap(), 0);
+        let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
+        Ok(NABufferType::Video(buf))
+    } else if !all_packed {
+        for i in 0..fmt.get_num_comp() {
+            let ochr = fmt.get_chromaton(i);
+            if let None = ochr { continue; }
+            let chr = ochr.unwrap();
+            if !vinfo.is_flipped() {
+                offs.push(new_size as usize);
+            }
+            let stride = chr.get_linesize(width);
+            let cur_h = chr.get_height(height);
+            let cur_sz = stride.checked_mul(cur_h);
+            if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); }
+            let new_sz = new_size.checked_add(cur_sz.unwrap());
+            if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
+            new_size = new_sz.unwrap();
+            if vinfo.is_flipped() {
+                offs.push(new_size as usize);
+            }
+            strides.push(stride);
+        }
+        if max_depth <= 8 {
+            let mut data: Vec<u8> = Vec::with_capacity(new_size);
+            data.resize(new_size, 0);
+            let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
+            Ok(NABufferType::Video(buf))
+        } else {
+            let mut data: Vec<u16> = Vec::with_capacity(new_size);
+            data.resize(new_size, 0);
+            let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
+            Ok(NABufferType::Video16(buf))
+        }
+    } else {
+        let elem_sz = fmt.get_elem_size();
+        let line_sz = width.checked_mul(elem_sz as usize);
+        if line_sz == None { return Err(AllocatorError::TooLargeDimensions); }
+        let new_sz = line_sz.unwrap().checked_mul(height);
+        if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
+        new_size = new_sz.unwrap();
+        let mut data: Vec<u8> = Vec::with_capacity(new_size);
+        data.resize(new_size, 0);
+        strides.push(line_sz.unwrap());
+        let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
+        Ok(NABufferType::VideoPacked(buf))
+    }
 }
 
-impl Drop for NABuffer {
-    fn drop(&mut self) { }
+pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> {
+    let mut offs: Vec<usize> = Vec::new();
+    if ainfo.format.is_planar() || (ainfo.channels == 1 && (ainfo.format.get_bits() % 8) == 0) {
+        let len = nsamples.checked_mul(ainfo.channels as usize);
+        if len == None { return Err(AllocatorError::TooLargeDimensions); }
+        let length = len.unwrap();
+        for i in 0..ainfo.channels {
+            offs.push((i as usize) * nsamples);
+        }
+        if ainfo.format.is_float() {
+            if ainfo.format.get_bits() == 32 {
+                let mut data: Vec<f32> = Vec::with_capacity(length);
+                data.resize(length, 0.0);
+                let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
+                Ok(NABufferType::AudioF32(buf))
+            } else {
+                Err(AllocatorError::TooLargeDimensions)
+            }
+        } else {
+            if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() {
+                let mut data: Vec<u8> = Vec::with_capacity(length);
+                data.resize(length, 0);
+                let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
+                Ok(NABufferType::AudioU8(buf))
+            } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() {
+                let mut data: Vec<i16> = Vec::with_capacity(length);
+                data.resize(length, 0);
+                let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
+                Ok(NABufferType::AudioI16(buf))
+            } else {
+                Err(AllocatorError::TooLargeDimensions)
+            }
+        }
+    } else {
+        let len = nsamples.checked_mul(ainfo.channels as usize);
+        if len == None { return Err(AllocatorError::TooLargeDimensions); }
+        let length = ainfo.format.get_audio_size(len.unwrap() as u64);
+        let mut data: Vec<u8> = Vec::with_capacity(length);
+        data.resize(length, 0);
+        let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
+        Ok(NABufferType::AudioPacked(buf))        
+    }
 }
 
-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 fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> {
+    let mut data: Vec<u8> = Vec::with_capacity(size);
+    data.resize(size, 0);
+    let buf: NABufferRefT<u8> = Rc::new(RefCell::new(data));
+    Ok(NABufferType::Data(buf))
 }
 
-pub type NABufferRef = Rc<RefCell<NABuffer>>;
+pub fn copy_buffer(buf: NABufferType) -> NABufferType {
+    buf.clone()
+}
 
 #[allow(dead_code)]
 #[derive(Clone)]
@@ -125,6 +422,12 @@ impl NACodecInfo {
         if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
         false
     }
+    pub fn new_dummy() -> Rc<Self> {
+        Rc::new(DUMMY_CODEC_INFO)
+    }
+    pub fn replace_info(&self, p: NACodecTypeInfo) -> Rc<Self> {
+        Rc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() })
+    }
 }
 
 impl fmt::Display for NACodecInfo {
@@ -142,56 +445,6 @@ pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
                                 properties: NACodecTypeInfo::None,
                                 extradata: None };
 
-fn alloc_video_buf(vinfo: NAVideoInfo, data: &mut Vec<u8>, offs: &mut Vec<usize>) {
-//todo use overflow detection mul
-    let width = vinfo.width as usize;
-    let height = vinfo.height as usize;
-    let fmt = &vinfo.format;
-    let mut new_size = 0;
-    for i in 0..fmt.get_num_comp() {
-        let chr = fmt.get_chromaton(i).unwrap();
-        if !vinfo.is_flipped() {
-            offs.push(new_size as usize);
-        }
-        new_size += chr.get_data_size(width, height);
-        if vinfo.is_flipped() {
-            offs.push(new_size as usize);
-        }
-    }
-    data.resize(new_size, 0);
-}
-
-fn alloc_audio_buf(ainfo: NAAudioInfo, data: &mut Vec<u8>, offs: &mut Vec<usize>) {
-//todo better alloc
-    let length = ((ainfo.sample_rate as usize) * (ainfo.format.get_bits() as usize)) >> 3;
-    let new_size: usize = length * (ainfo.channels as usize);
-    data.resize(new_size, 0);
-    for i in 0..ainfo.channels {
-        if ainfo.format.is_planar() {
-            offs.push((i as usize) * length);
-        } else {
-            offs.push(((i * ainfo.format.get_bits()) >> 3) as usize);
-        }
-    }
-}
-
-pub fn alloc_buf(info: &NACodecInfo) -> (NABufferRef, Vec<usize>) {
-    let mut data: Vec<u8> = Vec::new();
-    let mut offs: Vec<usize> = Vec::new();
-    match info.properties {
-        NACodecTypeInfo::Audio(ainfo) => alloc_audio_buf(ainfo, &mut data, &mut offs),
-        NACodecTypeInfo::Video(vinfo) => alloc_video_buf(vinfo, &mut data, &mut offs),
-        _ => (),
-    }
-    (Rc::new(RefCell::new(NABuffer { id: 0, data: Rc::new(RefCell::new(data)) })), offs)
-}
-
-pub fn copy_buf(buf: &NABuffer) -> NABufferRef {
-    let mut data: Vec<u8> = Vec::new();
-    data.clone_from(buf.get_data().as_ref());
-    Rc::new(RefCell::new(NABuffer { id: 0, data: Rc::new(RefCell::new(data)) }))
-}
-
 #[derive(Debug,Clone)]
 pub enum NAValue {
     None,
@@ -207,6 +460,7 @@ pub enum FrameType {
     I,
     P,
     B,
+    Skip,
     Other,
 }
 
@@ -216,25 +470,46 @@ impl fmt::Display for FrameType {
             FrameType::I => write!(f, "I"),
             FrameType::P => write!(f, "P"),
             FrameType::B => write!(f, "B"),
+            FrameType::Skip => write!(f, "skip"),
             FrameType::Other => write!(f, "x"),
         }
     }
 }
 
-#[allow(dead_code)]
-#[derive(Clone)]
-pub struct NAFrame {
+#[derive(Debug,Clone,Copy)]
+pub struct NATimeInfo {
     pts:            Option<u64>,
     dts:            Option<u64>,
     duration:       Option<u64>,
-    buffer:         NABufferRef,
+    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,
     key:            bool,
-    offsets:        Vec<usize>,
     options:        HashMap<String, NAValue>,
 }
 
+pub type NAFrameRef = Rc<RefCell<NAFrame>>;
+
 fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
     let chromaton = info.get_format().get_chromaton(idx);
     if let None = chromaton { return (0, 0); }
@@ -245,53 +520,40 @@ 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>) -> Self {
-        let (buf, offs) = alloc_buf(&info);
-        NAFrame { pts: pts, dts: dts, duration: duration, buffer: buf, offsets: offs, info: info, ftype: ftype, key: keyframe, options: options }
-    }
-    pub fn from_copy(src: &NAFrame) -> Self {
-        let buf = copy_buf(&src.get_buffer());
-        let mut offs: Vec<usize> = Vec::new();
-        offs.clone_from(&src.offsets);
-        NAFrame { pts: None, dts: None, duration: None, buffer: buf, offsets: offs, info: src.info.clone(), ftype: src.ftype, key: src.key, options: src.options.clone() }
+               options:        HashMap<String, NAValue>,
+               buffer:         NABufferType) -> Self {
+        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_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
     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_offset(&self, idx: usize) -> usize { self.offsets[idx] }
-    pub fn get_buffer(&self) -> Ref<NABuffer> { self.buffer.borrow() }
-    pub fn get_buffer_mut(&mut self) -> RefMut<NABuffer> { self.buffer.borrow_mut() }
-    pub fn get_stride(&self, idx: usize) -> usize {
-        if let NACodecTypeInfo::Video(vinfo) = self.info.get_properties() {
-            if idx >= vinfo.get_format().get_num_comp() { return 0; }
-            vinfo.get_format().get_chromaton(idx).unwrap().get_linesize(vinfo.get_width())
-        } else {
-            0
-        }
-    }
-    pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
-        match self.info.get_properties() {
-            NACodecTypeInfo::Video(ref vinfo) => get_plane_size(vinfo, idx),
-            _ => (0, 0),
-        }
-    }
+    pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
 }
 
-pub type NAFrameRef = Rc<RefCell<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.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)
+    }
+}
 
 /// Possible stream types.
 #[derive(Debug,Clone,Copy)]
@@ -328,45 +590,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() }
 }
@@ -378,9 +663,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)
@@ -388,18 +673,16 @@ impl fmt::Display for NAPacket {
 }
 
 pub trait FrameFromPacket {
-    fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>) -> NAFrame;
+    fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame;
     fn fill_timestamps(&mut self, pkt: &NAPacket);
 }
 
 impl FrameFromPacket for NAFrame {
-    fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>) -> NAFrame {
-        NAFrame::new(pkt.pts, pkt.dts, pkt.duration, FrameType::Other, pkt.keyframe, info, HashMap::new())
+    fn new_from_pkt(pkt: &NAPacket, info: Rc<NACodecInfo>, buf: NABufferType) -> NAFrame {
+        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();
     }
 }