make decoders Send-able
[nihav.git] / nihav-core / src / codecs / mod.rs
index f3fabf3a642dad3c249c7c08a9f9fe16ed652aff..9c59c40184029358038e1a87bdcc25910503ae28 100644 (file)
@@ -1,9 +1,7 @@
 use std::fmt;
 use std::ops::{Add, AddAssign, Sub, SubAssign};
 
-use crate::frame::*;
-use std::rc::Rc;
-use std::cell::RefCell;
+pub use crate::frame::*;
 use std::mem;
 use crate::io::byteio::ByteIOError;
 use crate::io::bitreader::BitReaderError;
@@ -45,13 +43,9 @@ impl From<AllocatorError> for DecoderError {
     fn from(_: AllocatorError) -> Self { DecoderError::AllocError }
 }
 
-macro_rules! validate {
-    ($a:expr) => { if !$a { println!("check failed at {}:{}", file!(), line!()); return Err(DecoderError::InvalidData); } };
-}
-
 #[allow(dead_code)]
 pub struct HAMShuffler {
-    lastframe: Option<NAVideoBuffer<u8>>,
+    lastframe: Option<NAVideoBufferRef<u8>>,
 }
 
 impl HAMShuffler {
@@ -60,21 +54,21 @@ impl HAMShuffler {
     #[allow(dead_code)]
     pub fn clear(&mut self) { self.lastframe = None; }
     #[allow(dead_code)]
-    pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
+    pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
         self.lastframe = Some(buf);
     }
     #[allow(dead_code)]
-    pub fn clone_ref(&mut self) -> Option<NAVideoBuffer<u8>> {
+    pub fn clone_ref(&mut self) -> Option<NAVideoBufferRef<u8>> {
         if let Some(ref mut frm) = self.lastframe {
             let newfrm = frm.copy_buffer();
-            *frm = newfrm.clone();
-            Some(newfrm)
+            *frm = newfrm.clone().into_ref();
+            Some(newfrm.into_ref())
         } else {
             None
         }
     }
     #[allow(dead_code)]
-    pub fn get_output_frame(&mut self) -> Option<NAVideoBuffer<u8>> {
+    pub fn get_output_frame(&mut self) -> Option<NAVideoBufferRef<u8>> {
         match self.lastframe {
             Some(ref frm) => Some(frm.clone()),
             None => None,
@@ -82,9 +76,13 @@ impl HAMShuffler {
     }
 }
 
+impl Default for HAMShuffler {
+    fn default() -> Self { Self { lastframe: None } }
+}
+
 #[allow(dead_code)]
 pub struct IPShuffler {
-    lastframe: Option<NAVideoBuffer<u8>>,
+    lastframe: Option<NAVideoBufferRef<u8>>,
 }
 
 impl IPShuffler {
@@ -93,11 +91,11 @@ impl IPShuffler {
     #[allow(dead_code)]
     pub fn clear(&mut self) { self.lastframe = None; }
     #[allow(dead_code)]
-    pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
+    pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
         self.lastframe = Some(buf);
     }
     #[allow(dead_code)]
-    pub fn get_ref(&mut self) -> Option<NAVideoBuffer<u8>> {
+    pub fn get_ref(&mut self) -> Option<NAVideoBufferRef<u8>> {
         if let Some(ref frm) = self.lastframe {
             Some(frm.clone())
         } else {
@@ -106,10 +104,14 @@ impl IPShuffler {
     }
 }
 
+impl Default for IPShuffler {
+    fn default() -> Self { Self { lastframe: None } }
+}
+
 #[allow(dead_code)]
 pub struct IPBShuffler {
-    lastframe: Option<NAVideoBuffer<u8>>,
-    nextframe: Option<NAVideoBuffer<u8>>,
+    lastframe: Option<NAVideoBufferRef<u8>>,
+    nextframe: Option<NAVideoBufferRef<u8>>,
 }
 
 impl IPBShuffler {
@@ -118,12 +120,12 @@ impl IPBShuffler {
     #[allow(dead_code)]
     pub fn clear(&mut self) { self.lastframe = None; self.nextframe = None; }
     #[allow(dead_code)]
-    pub fn add_frame(&mut self, buf: NAVideoBuffer<u8>) {
+    pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
         mem::swap(&mut self.lastframe, &mut self.nextframe);
         self.lastframe = Some(buf);
     }
     #[allow(dead_code)]
-    pub fn get_lastref(&mut self) -> Option<NAVideoBuffer<u8>> {
+    pub fn get_lastref(&mut self) -> Option<NAVideoBufferRef<u8>> {
         if let Some(ref frm) = self.lastframe {
             Some(frm.clone())
         } else {
@@ -131,7 +133,7 @@ impl IPBShuffler {
         }
     }
     #[allow(dead_code)]
-    pub fn get_nextref(&mut self) -> Option<NAVideoBuffer<u8>> {
+    pub fn get_nextref(&mut self) -> Option<NAVideoBufferRef<u8>> {
         if let Some(ref frm) = self.nextframe {
             Some(frm.clone())
         } else {
@@ -139,7 +141,7 @@ impl IPBShuffler {
         }
     }
     #[allow(dead_code)]
-    pub fn get_b_fwdref(&mut self) -> Option<NAVideoBuffer<u8>> {
+    pub fn get_b_fwdref(&mut self) -> Option<NAVideoBufferRef<u8>> {
         if let Some(ref frm) = self.nextframe {
             Some(frm.clone())
         } else {
@@ -147,7 +149,7 @@ impl IPBShuffler {
         }
     }
     #[allow(dead_code)]
-    pub fn get_b_bwdref(&mut self) -> Option<NAVideoBuffer<u8>> {
+    pub fn get_b_bwdref(&mut self) -> Option<NAVideoBufferRef<u8>> {
         if let Some(ref frm) = self.lastframe {
             Some(frm.clone())
         } else {
@@ -156,14 +158,20 @@ impl IPBShuffler {
     }
 }
 
-#[derive(Debug,Clone,Copy,PartialEq)]
+impl Default for IPBShuffler {
+    fn default() -> Self { Self { lastframe: None, nextframe: None } }
+}
+
+#[derive(Debug,Clone,Copy,Default,PartialEq)]
 pub struct MV {
     pub x: i16,
     pub y: i16,
 }
 
+#[allow(clippy::many_single_char_names)]
+#[allow(clippy::collapsible_if)]
 impl MV {
-    pub fn new(x: i16, y: i16) -> Self { MV{ x: x, y: y } }
+    pub fn new(x: i16, y: i16) -> Self { MV{ x, y } }
     pub fn pred(a: MV, b: MV, c: MV) -> Self {
         let x;
         if a.x < b.x {
@@ -193,7 +201,7 @@ impl MV {
                 y = b.y;
             }
         }
-        MV { x: x, y: y }
+        MV { x, y }
     }
 }
 
@@ -223,63 +231,46 @@ impl fmt::Display for MV {
     }
 }
 
+pub struct NADecoderSupport {
+    pub pool_u8:        NAVideoBufferPool<u8>,
+    pub pool_u16:       NAVideoBufferPool<u16>,
+    pub pool_u32:       NAVideoBufferPool<u32>,
+}
+
+impl NADecoderSupport {
+    pub fn new() -> Self {
+        Self {
+            pool_u8:        NAVideoBufferPool::new(0),
+            pool_u16:       NAVideoBufferPool::new(0),
+            pool_u32:       NAVideoBufferPool::new(0),
+        }
+    }
+}
+
+impl Default for NADecoderSupport {
+    fn default() -> Self { Self::new() }
+}
+
 
 pub trait NADecoder {
-    fn init(&mut self, info: Rc<NACodecInfo>) -> DecoderResult<()>;
-    fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef>;
+    fn init(&mut self, supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()>;
+    fn decode(&mut self, supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef>;
+    fn flush(&mut self);
 }
 
 #[derive(Clone,Copy)]
 pub struct DecoderInfo {
     pub name: &'static str,
-    pub get_decoder: fn () -> Box<NADecoder>,
+    pub get_decoder: fn () -> Box<dyn NADecoder + Send>,
 }
 
-#[cfg(any(feature="h263"))]
+#[cfg(any(feature="blockdsp"))]
 pub mod blockdsp;
 
-#[cfg(feature="decoder_clearvideo")]
-mod clearvideo;
-#[cfg(feature="decoder_gdvvid")]
-mod gremlinvideo;
 #[cfg(feature="h263")]
 pub mod h263;
 
-#[cfg(feature="decoder_aac")]
-mod aac;
-#[cfg(feature="decoder_atrac3")]
-mod atrac3;
-#[cfg(feature="decoder_pcm")]
-mod pcm;
-#[cfg(feature="decoder_sipro")]
-mod sipro;
-#[cfg(feature="decoder_ts102366")]
-mod ts102366;
-
-const DECODERS: &[DecoderInfo] = &[
-#[cfg(feature="decoder_clearvideo")]
-    DecoderInfo { name: "clearvideo", get_decoder: clearvideo::get_decoder },
-#[cfg(feature="decoder_clearvideo")]
-    DecoderInfo { name: "clearvideo_rm", get_decoder: clearvideo::get_decoder_rm },
-
-#[cfg(feature="decoder_pcm")]
-    DecoderInfo { name: "pcm", get_decoder: pcm::get_decoder },
-#[cfg(feature="decoder_sipro")]
-    DecoderInfo { name: "sipro", get_decoder: sipro::get_decoder },
-#[cfg(feature="decoder_ts102366")]
-    DecoderInfo { name: "ac3", get_decoder: ts102366::get_decoder },
-#[cfg(feature="decoder_atrac3")]
-    DecoderInfo { name: "atrac3", get_decoder: atrac3::get_decoder },
-#[cfg(feature="decoder_aac")]
-    DecoderInfo { name: "aac", get_decoder: aac::get_decoder },
-];
-
-pub fn core_register_all_codecs(rd: &mut RegisteredDecoders) {
-    for decoder in DECODERS.into_iter() {
-        rd.add_decoder(decoder.clone());
-    }
-}
-
+#[derive(Default)]
 pub struct RegisteredDecoders {
     decs:   Vec<DecoderInfo>,
 }
@@ -291,7 +282,7 @@ impl RegisteredDecoders {
     pub fn add_decoder(&mut self, dec: DecoderInfo) {
         self.decs.push(dec);
     }
-    pub fn find_decoder(&self, name: &str) -> Option<fn () -> Box<NADecoder>> {
+    pub fn find_decoder(&self, name: &str) -> Option<fn () -> Box<dyn NADecoder + Send>> {
         for &dec in self.decs.iter() {
             if dec.name == name {
                 return Some(dec.get_decoder);
@@ -299,4 +290,18 @@ impl RegisteredDecoders {
         }
         None
     }
+    pub fn iter(&self) -> std::slice::Iter<DecoderInfo> {
+        self.decs.iter()
+    }
 }
+
+pub const ZIGZAG: [usize; 64] = [
+     0,  1,  8, 16,  9,  2,  3, 10,
+    17, 24, 32, 25, 18, 11,  4,  5,
+    12, 19, 26, 33, 40, 48, 41, 34,
+    27, 20, 13,  6,  7, 14, 21, 28,
+    35, 42, 49, 56, 57, 50, 43, 36,
+    29, 22, 15, 23, 30, 37, 44, 51,
+    58, 59, 52, 45, 38, 31, 39, 46,
+    53, 60, 61, 54, 47, 55, 62, 63
+];