]> git.nihav.org Git - nihav.git/commitdiff
add IVF demuxer for VP8 test samples
authorKostya Shishkov <kostya.shishkov@gmail.com>
Sat, 9 Oct 2021 09:34:40 +0000 (11:34 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Thu, 14 Oct 2021 14:38:26 +0000 (16:38 +0200)
nihav-allstuff/src/lib.rs
nihav-duck/Cargo.toml
nihav-duck/src/demuxers/ivf.rs [new file with mode: 0644]
nihav-duck/src/demuxers/mod.rs [new file with mode: 0644]
nihav-duck/src/lib.rs
nihav-registry/src/detect.rs

index 36d5d50d6469a0e3a696b76ee2195401b74ded8c..195d67d3caf422175eb48a82aa4fb7233983af5f 100644 (file)
@@ -23,6 +23,7 @@ use nihav_commonfmt::generic_register_all_encoders;
 use nihav_commonfmt::generic_register_all_muxers;
 
 use nihav_duck::duck_register_all_decoders;
+use nihav_duck::duck_register_all_demuxers;
 use nihav_duck::duck_register_all_encoders;
 
 use nihav_game::game_register_all_decoders;
@@ -66,6 +67,7 @@ pub fn nihav_register_all_decoders(rd: &mut RegisteredDecoders) {
 
 /// Registers all known demuxers.
 pub fn nihav_register_all_demuxers(rd: &mut RegisteredDemuxers) {
+    duck_register_all_demuxers(rd);
     generic_register_all_demuxers(rd);
     game_register_all_demuxers(rd);
     llaudio_register_all_demuxers(rd);
index 69aa80072f29aeacd531b40ecd4b90913dbea9b0..7de0d500b63dd013e3771ae49f6c842320d5ac0c 100644 (file)
@@ -15,7 +15,7 @@ features = ["fft", "dsp_window", "blockdsp"]
 nihav_commonfmt = { path = "../nihav-commonfmt", default-features=false, features = ["all_demuxers", "all_muxers"] }
 
 [features]
-default = ["all_decoders", "all_encoders"]
+default = ["all_decoders", "all_encoders", "all_demuxers"]
 
 all_decoders = ["all_video_decoders", "all_audio_decoders"]
 all_video_decoders = ["decoder_truemotion1", "decoder_truemotionrt", "decoder_truemotion2", "decoder_truemotion2x", "decoder_vp3", "decoder_vp4", "decoder_vp5", "decoder_vp6", "decoder_vp7"]
@@ -39,4 +39,9 @@ all_encoders = ["all_video_encoders"]
 all_video_encoders = ["encoder_vp6"]
 encoders = []
 
-encoder_vp6 = ["encoders"]
\ No newline at end of file
+encoder_vp6 = ["encoders"]
+
+all_demuxers = ["demuxer_ivf"]
+demuxers = []
+
+demuxer_ivf = ["demuxers"]
\ No newline at end of file
diff --git a/nihav-duck/src/demuxers/ivf.rs b/nihav-duck/src/demuxers/ivf.rs
new file mode 100644 (file)
index 0000000..7d44460
--- /dev/null
@@ -0,0 +1,118 @@
+use nihav_core::demuxers::*;
+
+struct IVFDemuxer<'a> {
+    src:            &'a mut ByteReader<'a>,
+    nframes:        u32,
+    frameno:        u32,
+}
+
+impl<'a> IVFDemuxer<'a> {
+    fn new(src: &'a mut ByteReader<'a>) -> Self {
+        IVFDemuxer {
+            src,
+            nframes:    0,
+            frameno:    0,
+        }
+    }
+}
+
+impl<'a> DemuxCore<'a> for IVFDemuxer<'a> {
+    fn open(&mut self, strmgr: &mut StreamManager, _seek_index: &mut SeekIndex) -> DemuxerResult<()> {
+        let tag                         = self.src.read_tag()?;
+        validate!(&tag == b"DKIF");
+        let ver                         = self.src.read_u16le()?;
+        validate!(ver == 0);
+        let hdr_len                     = self.src.read_u16le()? as usize;
+        validate!(hdr_len >= 32);
+        let fcc                         = self.src.read_tag()?;
+        let codec_name = match &fcc {
+                b"VP80" => "vp8",
+                _       => "unknown",
+            };
+        let width                       = self.src.read_u16le()? as usize;
+        let height                      = self.src.read_u16le()? as usize;
+        validate!(width > 0 && height > 0);
+        let tb_den                      = self.src.read_u32le()?;
+        let tb_num                      = self.src.read_u32le()?;
+        self.nframes                    = self.src.read_u32le()?;
+        self.frameno = 0;
+
+        self.src.seek(SeekFrom::Start(hdr_len as u64))?;
+
+        let vci = NACodecTypeInfo::Video(NAVideoInfo::new(width, height, false, YUV420_FORMAT));
+        let vinfo = NACodecInfo::new(codec_name, vci, None);
+        if strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, tb_num, tb_den, u64::from(self.nframes))).is_none() {
+            return Err(DemuxerError::MemoryError);
+        }
+
+        Ok(())
+    }
+
+    fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
+        while self.frameno < self.nframes {
+            let fsize                   = self.src.read_u32le()? as usize;
+            let tstamp                  = self.src.read_u64le()?;
+            self.frameno += 1;
+
+            if fsize == 0 {
+                continue;
+            }
+
+            if let Some(stream) = strmgr.get_stream(0) {
+                let (tb_num, tb_den) = stream.get_timebase();
+                let ts = NATimeInfo::new(Some(tstamp), None, None, tb_num, tb_den);
+                return self.src.read_packet(stream, ts, false, fsize);
+            } else {
+                return Err(DemuxerError::InvalidData);
+            }
+        }
+        Err(DemuxerError::EOF)
+    }
+
+    fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> {
+        Err(DemuxerError::NotPossible)
+    }
+    fn get_duration(&self) -> u64 { 0 }
+}
+
+impl<'a> NAOptionHandler for IVFDemuxer<'a> {
+    fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
+    fn set_options(&mut self, _options: &[NAOption]) { }
+    fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
+}
+
+pub struct IVFDemuxerCreator { }
+
+impl DemuxerCreator for IVFDemuxerCreator {
+    fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
+        Box::new(IVFDemuxer::new(br))
+    }
+    fn get_name(&self) -> &'static str { "ivf" }
+}
+
+#[cfg(test)]
+mod test {
+    use super::*;
+    use std::fs::File;
+
+    #[test]
+    fn test_ivf_demux() {
+        let mut file = File::open("assets/Duck/VP8/vp80-00-comprehensive-001.ivf").unwrap();
+        let mut fr = FileReader::new_read(&mut file);
+        let mut br = ByteReader::new(&mut fr);
+        let mut dmx = IVFDemuxer::new(&mut br);
+        let mut sm = StreamManager::new();
+        let mut si = SeekIndex::new();
+        dmx.open(&mut sm, &mut si).unwrap();
+
+        loop {
+            let pktres = dmx.get_frame(&mut sm);
+            if let Err(e) = pktres {
+                if e == DemuxerError::EOF { break; }
+                panic!("error");
+            }
+            let pkt = pktres.unwrap();
+            println!("Got {}", pkt);
+        }
+    }
+}
diff --git a/nihav-duck/src/demuxers/mod.rs b/nihav-duck/src/demuxers/mod.rs
new file mode 100644 (file)
index 0000000..3ac19a2
--- /dev/null
@@ -0,0 +1,22 @@
+use nihav_core::demuxers::*;
+
+
+#[allow(unused_macros)]
+macro_rules! validate {
+    ($a:expr) => { if !$a { println!("check failed at {}:{}", file!(), line!()); return Err(DemuxerError::InvalidData); } };
+}
+
+#[cfg(feature="demuxer_ivf")]
+mod ivf;
+
+const DEMUXERS: &[&dyn DemuxerCreator] = &[
+#[cfg(feature="demuxer_ivf")]
+    &ivf::IVFDemuxerCreator {},
+];
+
+/// Registers all available demuxers provided by this crate.
+pub fn duck_register_all_demuxers(rd: &mut RegisteredDemuxers) {
+    for demuxer in DEMUXERS.iter() {
+        rd.add_demuxer(*demuxer);
+    }
+}
index eb8560e96067b5f0bf5ad6920c1516dba8bace66..0885514a246bf1bd576a1e5eb14156858a36467f 100644 (file)
@@ -9,8 +9,11 @@ extern crate nihav_codec_support;
 #[allow(clippy::verbose_bit_mask)]
 mod codecs;
 
+mod demuxers;
+
 pub use crate::codecs::duck_register_all_decoders;
 pub use crate::codecs::duck_register_all_encoders;
+pub use crate::demuxers::duck_register_all_demuxers;
 
 #[cfg(test)]
 extern crate nihav_commonfmt;
index c51936d3a3c4b22526f4d0317c25802fe3c76834..11fd2ed00be1d6140cf42587071551d9840284be 100644 (file)
@@ -235,6 +235,12 @@ const DETECTORS: &[DetectConditions] = &[
         extensions: ".y4m",
         conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"YUV4MPEG2 ") }],
     },
+    DetectConditions {
+        demux_name: "ivf",
+        extensions: ".ivf",
+        conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"DKIF\x00\x00")},
+                      CheckItem{offs: 6, cond: &CC::Ge(Arg::U16LE(32))}],
+    },
     DetectConditions {
         demux_name: "fcmp",
         extensions: ".cmp",