]> git.nihav.org Git - nihav.git/blobdiff - src/frame.rs
allow setting w/h for NAVideoInfo
[nihav.git] / src / frame.rs
index 19c1733c9c716b7101f199d9301e62931f68c5e5..6f22dafc1cdcdc4277704789ad3fbed197f00249 100644 (file)
@@ -47,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 {
@@ -75,6 +77,18 @@ impl NACodecTypeInfo {
             _ => 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 {
@@ -88,15 +102,14 @@ impl fmt::Display for NACodecTypeInfo {
     }
 }
 
-pub type BufferRef = Rc<RefCell<Vec<u8>>>;
-
 pub type NABufferRefT<T> = Rc<RefCell<Vec<T>>>;
 
 #[derive(Clone)]
 pub struct NAVideoBuffer<T> {
-    info:   NAVideoInfo,
-    data:   NABufferRefT<T>,
-    offs:   Vec<usize>,
+    info:    NAVideoInfo,
+    data:    NABufferRefT<T>,
+    offs:    Vec<usize>,
+    strides: Vec<usize>,
 }
 
 impl<T: Clone> NAVideoBuffer<T> {
@@ -112,11 +125,13 @@ impl<T: Clone> NAVideoBuffer<T> {
         data.clone_from(self.data.borrow().as_ref());
         let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
         offs.clone_from(&self.offs);
-        NAVideoBuffer { info: self.info, data: Rc::new(RefCell::new(data)), offs: 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.info.get_format().get_num_comp() { return 0; }
-        self.info.get_format().get_chromaton(idx).unwrap().get_linesize(self.info.get_width())
+        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)
@@ -129,6 +144,7 @@ pub struct NAAudioBuffer<T> {
     data:   NABufferRefT<T>,
     offs:   Vec<usize>,
     chmap:  NAChannelMap,
+    len:    usize,
 }
 
 impl<T: Clone> NAAudioBuffer<T> {
@@ -145,7 +161,15 @@ impl<T: Clone> NAAudioBuffer<T> {
         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() }
+        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 }
     }
 }
 
@@ -156,6 +180,7 @@ pub enum NABufferType {
     VideoPacked(NAVideoBuffer<u8>),
     AudioU8    (NAAudioBuffer<u8>),
     AudioI16   (NAAudioBuffer<i16>),
+    AudioI32   (NAAudioBuffer<i32>),
     AudioF32   (NAAudioBuffer<f32>),
     AudioPacked(NAAudioBuffer<u8>),
     Data       (NABufferRefT<u8>),
@@ -178,6 +203,38 @@ impl NABufferType {
     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,
         }
     }
@@ -192,7 +249,8 @@ pub enum AllocatorError {
 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 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); }
@@ -204,7 +262,9 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType,
     let mut max_depth = 0;
     let mut all_packed = true;
     for i in 0..fmt.get_num_comp() {
-        let chr = fmt.get_chromaton(i).unwrap();
+        let ochr = fmt.get_chromaton(i);
+        if let None = ochr { continue; }
+        let chr = ochr.unwrap();
         if !chr.is_packed() {
             all_packed = false;
             break;
@@ -213,15 +273,32 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType,
     }
 
 //todo semi-packed like NV12
-    if !all_packed {
+    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 chr = fmt.get_chromaton(i).unwrap();
+            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 cur_w = chr.get_width(width);
+            let stride = chr.get_linesize(width);
             let cur_h = chr.get_height(height);
-            let cur_sz = cur_w.checked_mul(cur_h);
+            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); }
@@ -229,16 +306,17 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType,
             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 };
+            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 };
+            let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: Rc::new(RefCell::new(data)), info: vinfo, offs: offs, strides: strides };
             Ok(NABufferType::Video16(buf))
         }
     } else {
@@ -250,7 +328,8 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType,
         new_size = new_sz.unwrap();
         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.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))
     }
 }
@@ -268,7 +347,7 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM
             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 };
+                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)
@@ -277,12 +356,12 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM
             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 };
+                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 };
+                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)
@@ -294,7 +373,7 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM
         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 };
+        let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: Rc::new(RefCell::new(data)), info: ainfo, offs: offs, chmap: chmap, len: nsamples };
         Ok(NABufferType::AudioPacked(buf))        
     }
 }
@@ -343,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 {
@@ -375,6 +460,7 @@ pub enum FrameType {
     I,
     P,
     B,
+    Skip,
     Other,
 }
 
@@ -384,6 +470,7 @@ 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"),
         }
     }
@@ -441,6 +528,7 @@ impl NAFrame {
                buffer:         NABufferType) -> Self {
         NAFrame { ts: ts, buffer: buffer, info: info, ftype: ftype, key: keyframe, options: options }
     }
+    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_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; }