]> git.nihav.org Git - nihav.git/blobdiff - nihav-core/src/frame.rs
bink demuxer: save timebase in context
[nihav.git] / nihav-core / src / frame.rs
index 25c3856e7d9cd613b6dc2352e8f324d1d5aa66d1..e1529bfe213625f8ed92ceafe088c033b8b93783 100644 (file)
@@ -146,6 +146,7 @@ pub struct NAAudioBuffer<T> {
     info:   NAAudioInfo,
     data:   NABufferRef<Vec<T>>,
     offs:   Vec<usize>,
+    stride: usize,
     chmap:  NAChannelMap,
     len:    usize,
 }
@@ -155,8 +156,9 @@ impl<T: Clone> NAAudioBuffer<T> {
         if idx >= self.offs.len() { 0 }
         else { self.offs[idx] }
     }
+    pub fn get_stride(&self) -> usize { self.stride }
     pub fn get_info(&self) -> NAAudioInfo { self.info }
-    pub fn get_chmap(&self) -> NAChannelMap { self.chmap.clone() }
+    pub fn get_chmap(&self) -> &NAChannelMap { &self.chmap }
     pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
     pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
     pub fn copy_buffer(&mut self) -> Self {
@@ -164,7 +166,7 @@ impl<T: Clone> NAAudioBuffer<T> {
         data.clone_from(self.data.as_ref());
         let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
         offs.clone_from(&self.offs);
-        NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs, chmap: self.get_chmap(), len: self.len }
+        NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs, chmap: self.get_chmap().clone(), len: self.len, stride: self.stride }
     }
     pub fn get_length(&self) -> usize { self.len }
 }
@@ -172,7 +174,7 @@ impl<T: Clone> NAAudioBuffer<T> {
 impl NAAudioBuffer<u8> {
     pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef<Vec<u8>>, chmap: NAChannelMap) -> Self {
         let len = data.len();
-        NAAudioBuffer { info, data, chmap, offs: Vec::new(), len }
+        NAAudioBuffer { info, data, chmap, offs: Vec::new(), len, stride: 0 }
     }
 }
 
@@ -233,6 +235,46 @@ impl NABufferType {
             _ => None,
         }
     }
+    pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
+        match *self {
+            NABufferType::AudioU8(ref ab)     => Some(ab.get_info()),
+            NABufferType::AudioI16(ref ab)    => Some(ab.get_info()),
+            NABufferType::AudioI32(ref ab)    => Some(ab.get_info()),
+            NABufferType::AudioF32(ref ab)    => Some(ab.get_info()),
+            NABufferType::AudioPacked(ref ab) => Some(ab.get_info()),
+            _ => None,
+        }
+    }
+    pub fn get_chmap(&self) -> Option<&NAChannelMap> {
+        match *self {
+            NABufferType::AudioU8(ref ab)     => Some(ab.get_chmap()),
+            NABufferType::AudioI16(ref ab)    => Some(ab.get_chmap()),
+            NABufferType::AudioI32(ref ab)    => Some(ab.get_chmap()),
+            NABufferType::AudioF32(ref ab)    => Some(ab.get_chmap()),
+            NABufferType::AudioPacked(ref ab) => Some(ab.get_chmap()),
+            _ => None,
+        }
+    }
+    pub fn get_audio_length(&self) -> usize {
+        match *self {
+            NABufferType::AudioU8(ref ab)     => ab.get_length(),
+            NABufferType::AudioI16(ref ab)    => ab.get_length(),
+            NABufferType::AudioI32(ref ab)    => ab.get_length(),
+            NABufferType::AudioF32(ref ab)    => ab.get_length(),
+            NABufferType::AudioPacked(ref ab) => ab.get_length(),
+            _ => 0,
+        }
+    }
+    pub fn get_audio_stride(&self) -> usize {
+        match *self {
+            NABufferType::AudioU8(ref ab)     => ab.get_stride(),
+            NABufferType::AudioI16(ref ab)    => ab.get_stride(),
+            NABufferType::AudioI32(ref ab)    => ab.get_stride(),
+            NABufferType::AudioF32(ref ab)    => ab.get_stride(),
+            NABufferType::AudioPacked(ref ab) => ab.get_stride(),
+            _ => 0,
+        }
+    }
     pub fn get_abuf_u8(&self) -> Option<NAAudioBuffer<u8>> {
         match *self {
             NABufferType::AudioU8(ref ab) => Some(ab.clone()),
@@ -424,13 +466,14 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM
         let len = nsamples.checked_mul(ainfo.channels as usize);
         if len == None { return Err(AllocatorError::TooLargeDimensions); }
         let length = len.unwrap();
+        let stride = nsamples;
         for i in 0..ainfo.channels {
-            offs.push((i as usize) * nsamples);
+            offs.push((i as usize) * stride);
         }
         if ainfo.format.is_float() {
             if ainfo.format.get_bits() == 32 {
                 let data: Vec<f32> = vec![0.0; length];
-                let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples };
+                let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride };
                 Ok(NABufferType::AudioF32(buf))
             } else {
                 Err(AllocatorError::TooLargeDimensions)
@@ -438,11 +481,11 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM
         } else {
             if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() {
                 let data: Vec<u8> = vec![0; length];
-                let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples };
+                let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride };
                 Ok(NABufferType::AudioU8(buf))
             } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() {
                 let data: Vec<i16> = vec![0; length];
-                let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples };
+                let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride };
                 Ok(NABufferType::AudioI16(buf))
             } else {
                 Err(AllocatorError::TooLargeDimensions)
@@ -453,7 +496,7 @@ pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelM
         if len == None { return Err(AllocatorError::TooLargeDimensions); }
         let length = ainfo.format.get_audio_size(len.unwrap() as u64);
         let data: Vec<u8> = vec![0; length];
-        let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples };
+        let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride: 0 };
         Ok(NABufferType::AudioPacked(buf))
     }
 }