proper support for linesizes
authorKostya Shishkov <kostya.shishkov@gmail.com>
Sun, 4 Jun 2017 09:28:08 +0000 (11:28 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Sun, 4 Jun 2017 09:28:08 +0000 (11:28 +0200)
src/codecs/mod.rs
src/formats.rs
src/frame.rs

index dd72aeb657f719d2172c4890b41876657d3daa98..625485e1ed4ed8382c2957b2bc2f4add44863390 100644 (file)
@@ -158,7 +158,7 @@ fn write_pgmyuv(pfx: &str, strno: usize, num: u64, frmref: NAFrameRef) {
     let dta = buf.get_data();
     let ls = buf.get_stride(0);
     let mut idx = 0;
-    let mut idx2 = ls;
+    let mut idx2 = w;
     let mut pad: Vec<u8> = Vec::with_capacity((w - w2 * 2) / 2);
     pad.resize((w - w2 * 2) / 2, 0xFF);
     for _ in 0..h {
index aa053b2e75f55df7eb5cf6b7e0b64b3121a89a35..180e9800433bfc297bd2e2015c3e6d223f17a52a 100644 (file)
@@ -330,7 +330,11 @@ impl NAPixelChromaton {
     }
     pub fn get_linesize(&self, width: usize) -> usize {
         let d = self.depth as usize;
-        (self.get_width(width) * d + d - 1) >> 3
+        if self.packed {
+            (self.get_width(width) * d + d - 1) >> 3
+        } else {
+            self.get_width(width)
+        }
     }
     pub fn get_data_size(&self, width: usize, height: usize) -> usize {
         let nh = (height + ((1 << self.v_ss) - 1)) >> self.v_ss;
index 02bdf3348d84cd09a3e212e9844dd1d23bf4cc09..e739559a2e3f69adc0736d67ee2ecdf085398047 100644 (file)
@@ -92,9 +92,10 @@ 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> {
@@ -110,11 +111,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)
@@ -229,7 +232,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); }
@@ -241,7 +245,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;
@@ -252,26 +258,30 @@ pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType,
 //todo semi-packed like NV12
     if fmt.is_paletted() {
 //todo various-sized palettes?
-        let pic_sz = width.checked_mul(height);
+        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(width * height);
+        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 };
+        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); }
@@ -279,16 +289,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 {
@@ -300,7 +311,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))
     }
 }