]> git.nihav.org Git - nihav-player.git/commitdiff
sndplay: support various input kinds
authorKostya Shishkov <kostya.shishkov@gmail.com>
Wed, 17 Nov 2021 14:00:32 +0000 (15:00 +0100)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Wed, 17 Nov 2021 17:17:30 +0000 (18:17 +0100)
sndplay/Cargo.toml
sndplay/src/allreg.rs
sndplay/src/demux.rs [new file with mode: 0644]
sndplay/src/main.rs

index 1661282ab7b149dfaeac15fff42ecbfdb967bdcb..3a718c7f625b6391a2ca26049fddfa3b8c34e90a 100644 (file)
@@ -9,10 +9,12 @@ nihav_core = { path="../../nihav-core" }
 nihav_registry = { path="../../nihav-registry" }
 nihav_commonfmt = { path="../../nihav-commonfmt", default-features=false, features = ["all_demuxers", "all_audio_decoders"] }
 nihav_duck = { path = "../../nihav-duck", default-features=false, features = ["all_audio_decoders"] }
+nihav_flash = { path = "../../nihav-flash", default-features=false, features = ["all_demuxers", "all_audio_decoders"] }
 nihav_game = { path = "../../nihav-game", default-features=false, features = ["all_demuxers", "all_audio_decoders"] }
 nihav_indeo = { path = "../../nihav-indeo", default-features=false, features = ["all_audio_decoders"] }
 #nihav_itu = { path = "../../nihav-itu" }
 nihav_llaudio = { path = "../../nihav-llaudio" }
+nihav_mpeg = { path = "../../nihav-mpeg", default-features=false, features = ["all_audio_decoders"] }
 nihav_ms = { path = "../../nihav-ms", default-features=false, features = ["all_audio_decoders"] }
 nihav_qt = { path = "../../nihav-qt", default-features=false, features = ["all_audio_decoders"] }
 nihav_rad = { path = "../../nihav-rad", default-features=false, features = ["all_demuxers", "all_audio_decoders"] }
index d0c6b394192103bcee48fca37d6ce3a4c2c14116..a2a4df64810ba90c529f3c72f086dd6b9725b31b 100644 (file)
@@ -1,11 +1,14 @@
-use nihav_core::codecs::RegisteredDecoders;
-use nihav_core::demuxers::RegisteredDemuxers;
+use nihav_core::codecs::{RegisteredDecoders, RegisteredPacketisers};
+use nihav_core::demuxers::{RegisteredDemuxers, RegisteredRawDemuxers};
 
 use nihav_commonfmt::generic_register_all_decoders;
 use nihav_commonfmt::generic_register_all_demuxers;
 
 use nihav_duck::duck_register_all_decoders;
 
+use nihav_flash::flash_register_all_decoders;
+use nihav_flash::flash_register_all_demuxers;
+
 use nihav_game::game_register_all_decoders;
 use nihav_game::game_register_all_demuxers;
 
@@ -14,6 +17,9 @@ use nihav_indeo::indeo_register_all_decoders;
 use nihav_llaudio::llaudio_register_all_decoders;
 use nihav_llaudio::llaudio_register_all_demuxers;
 
+use nihav_mpeg::mpeg_register_all_decoders;
+use nihav_mpeg::mpeg_register_all_packetisers;
+
 use nihav_ms::ms_register_all_decoders;
 
 use nihav_qt::qt_register_all_decoders;
@@ -27,12 +33,14 @@ use nihav_realmedia::realmedia_register_all_demuxers;
 use nihav_vivo::vivo_register_all_decoders;
 use nihav_vivo::vivo_register_all_demuxers;
 
-pub fn register_all_decoders(rd: &mut RegisteredDecoders) {
+fn register_all_decoders(rd: &mut RegisteredDecoders) {
     generic_register_all_decoders(rd);
     duck_register_all_decoders(rd);
+    flash_register_all_decoders(rd);
     game_register_all_decoders(rd);
     indeo_register_all_decoders(rd);
     llaudio_register_all_decoders(rd);
+    mpeg_register_all_decoders(rd);
     ms_register_all_decoders(rd);
     qt_register_all_decoders(rd);
     rad_register_all_decoders(rd);
@@ -40,8 +48,13 @@ pub fn register_all_decoders(rd: &mut RegisteredDecoders) {
     vivo_register_all_decoders(rd);
 }
 
-pub fn register_all_demuxers(rd: &mut RegisteredDemuxers) {
+fn register_all_packetisers(rp: &mut RegisteredPacketisers) {
+    mpeg_register_all_packetisers(rp);
+}
+
+fn register_all_demuxers(rd: &mut RegisteredDemuxers) {
     generic_register_all_demuxers(rd);
+    flash_register_all_demuxers(rd);
     game_register_all_demuxers(rd);
     llaudio_register_all_demuxers(rd);
     rad_register_all_demuxers(rd);
@@ -49,3 +62,29 @@ pub fn register_all_demuxers(rd: &mut RegisteredDemuxers) {
     vivo_register_all_demuxers(rd);
 }
 
+fn register_all_raw_demuxers(_rd: &mut RegisteredRawDemuxers) {
+    //llaudio_register_all_raw_demuxers(rd);
+}
+
+
+pub struct FullRegister {
+    pub dmx_reg:    RegisteredDemuxers,
+    pub rdmx_reg:   RegisteredRawDemuxers,
+    pub pkt_reg:    RegisteredPacketisers,
+    pub dec_reg:    RegisteredDecoders,
+}
+
+impl FullRegister {
+    pub fn new() -> Self {
+        let mut dmx_reg = RegisteredDemuxers::new();
+        register_all_demuxers(&mut dmx_reg);
+        let mut rdmx_reg = RegisteredRawDemuxers::new();
+        register_all_raw_demuxers(&mut rdmx_reg);
+        let mut dec_reg = RegisteredDecoders::new();
+        register_all_decoders(&mut dec_reg);
+        let mut pkt_reg = RegisteredPacketisers::new();
+        register_all_packetisers(&mut pkt_reg);
+        Self { dmx_reg, rdmx_reg, pkt_reg, dec_reg }
+    }
+}
+
diff --git a/sndplay/src/demux.rs b/sndplay/src/demux.rs
new file mode 100644 (file)
index 0000000..6fd3a7e
--- /dev/null
@@ -0,0 +1,244 @@
+use std::io::SeekFrom;
+use nihav_core::codecs::*;
+use nihav_core::demuxers::*;
+use nihav_registry::detect;
+use nihav_core::io::byteio::ByteReader;
+use super::allreg::FullRegister;
+
+pub enum DemuxerObject<'a> {
+    None,
+    Normal(Demuxer<'a>),
+    Raw(RawDemuxer<'a>, Vec<Option<Box<dyn NAPacketiser + Send>>>, bool),
+    RawStream(Box<dyn NAPacketiser + Send>, NAStreamRef, &'a mut ByteReader<'a>),
+}
+
+impl<'a> DemuxerObject<'a> {
+    pub fn create(br: &'a mut ByteReader<'a>, reg: &FullRegister, name: &str, is_raw: bool) -> DemuxerObject<'a> {
+        if !is_raw {
+            let res = detect::detect_format(name, br);
+            let (dmx_name, _) = res.unwrap_or(("", detect::DetectionScore::No));
+            if dmx_name != "" {
+                println!("trying demuxer {} on {}", dmx_name, name);
+                if let Some(dmx_fact) = reg.dmx_reg.find_demuxer(dmx_name) {
+                    br.seek(SeekFrom::Start(0)).unwrap();
+                    let dmx = create_demuxer(dmx_fact, br).unwrap();
+                    return DemuxerObject::Normal(dmx);
+                }
+            }
+            if dmx_name != "" {
+                println!("trying raw demuxer {} on {}", dmx_name, name);
+                if let Some(rdmx_fact) = reg.rdmx_reg.find_demuxer(dmx_name) {
+                    br.seek(SeekFrom::Start(0)).unwrap();
+                    let dmx = create_raw_demuxer(rdmx_fact, br).unwrap();
+                    let mut pkts = Vec::new();
+                    for stream in dmx.get_streams() {
+                        if let Some(pcreate) = reg.pkt_reg.find_packetiser(stream.get_info().get_name()) {
+                            let packetiser = (pcreate)();
+                            pkts.push(Some(packetiser));
+                        } else {
+                            pkts.push(None);
+                        }
+                    }
+                    return DemuxerObject::Raw(dmx, pkts, false);
+                }
+            }
+            for rdmx in reg.rdmx_reg.iter() {
+                if rdmx.check_format(br) {
+                    println!("detected {} as {}", name, rdmx.get_name());
+                    br.seek(SeekFrom::Start(0)).unwrap();
+                    let dmx = create_raw_demuxer(*rdmx, br).unwrap();
+                    let mut pkts = Vec::new();
+                    for stream in dmx.get_streams() {
+                        if let Some(pcreate) = reg.pkt_reg.find_packetiser(stream.get_info().get_name()) {
+                            let packetiser = (pcreate)();
+                            pkts.push(Some(packetiser));
+                        } else {
+                            pkts.push(None);
+                        }
+                    }
+                    return DemuxerObject::Raw(dmx, pkts, false);
+                }
+            }
+        }
+        br.seek(SeekFrom::Start(0)).unwrap();
+        let mut buf = vec![0; 1048576];
+        let size = br.peek_buf(&mut buf).unwrap();
+        let mut pname = "";
+
+        for pinfo in reg.pkt_reg.iter() {
+            let mut packetiser = (pinfo.get_packetiser)();
+            packetiser.add_data(&buf[..size]);
+            if packetiser.parse_stream(0).is_ok() {
+                pname = pinfo.name;
+                break;
+            }
+        }
+        if pname != "" {
+            println!("found raw stream of type {} for {}", pname, name);
+            let pcreate = reg.pkt_reg.find_packetiser(pname).unwrap();
+            let mut packetiser = (pcreate)();
+            packetiser.add_data(&buf[..size]);
+            let stream = packetiser.parse_stream(0).unwrap();
+            packetiser.reset();
+            DemuxerObject::RawStream(packetiser, stream, br)
+        } else {
+            DemuxerObject::None
+        }
+    }
+    pub fn is_none(&self) -> bool {
+        match *self {
+            DemuxerObject::None => true,
+            _ => false,
+        }
+    }
+    pub fn get_duration(&self) -> u64 {
+        match *self {
+            DemuxerObject::Normal(ref dmx) => dmx.get_duration(),
+            DemuxerObject::Raw(ref dmx, _, _) => dmx.get_duration(),
+            _ => 0,
+        }
+    }
+    pub fn get_num_streams(&self) -> usize {
+        match *self {
+            DemuxerObject::None => 0,
+            DemuxerObject::Normal(ref dmx) => dmx.get_num_streams(),
+            DemuxerObject::Raw(ref dmx, _, _) => dmx.get_num_streams(),
+            DemuxerObject::RawStream(_, _, _) => 1,
+        }
+    }
+    pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> {
+        match *self {
+            DemuxerObject::Normal(ref dmx) => dmx.get_stream(idx),
+            DemuxerObject::Raw(ref dmx, _, _) => dmx.get_stream(idx),
+            DemuxerObject::RawStream(_, ref stream, _) if idx == 0 => Some(stream.clone()),
+            _ => None,
+        }
+    }
+    pub fn get_frame(&mut self) -> DemuxerResult<NAPacket> {
+        match *self {
+            DemuxerObject::Normal(ref mut dmx) => dmx.get_frame(),
+            DemuxerObject::Raw(ref mut dmx, ref mut packetisers, ref mut eof) => {
+                loop {
+                    let mut has_some = false;
+                    for (stream, p) in dmx.get_streams().zip(packetisers.iter_mut()) {
+                        if let Some(ref mut pkts) = p {
+                            match pkts.get_packet(stream.clone()) {
+                                Ok(Some(pkt)) => return Ok(pkt),
+                                Ok(None) | Err(DecoderError::ShortData) => {
+                                    if *eof {
+                                        *p = None;
+                                    }
+                                },
+                                Err(err) => {
+                                    println!("packetisation error {:?}", err);
+                                    return Err(DemuxerError::InvalidData);
+                                }
+                            };
+                            has_some |= p.is_some();
+                        }
+                    }
+                    if !has_some {
+                        return Err(DemuxerError::EOF);
+                    }
+                    if let Ok(data) = dmx.get_data() {
+                        let id = data.get_stream().get_id();
+                        for (i, stream) in dmx.get_streams().enumerate() {
+                            if stream.get_id() == id {
+                                if let Some(ref mut pkts) = packetisers[i] {
+                                    pkts.add_data(&data.get_buffer());
+                                }
+                                break;
+                            }
+                        }
+                    } else {
+                        *eof = true;
+                    }
+                }
+            },
+            DemuxerObject::RawStream(ref mut pkt, ref stream, ref mut br) => {
+                let mut buf = [0; 1048576];
+                loop {
+                    match pkt.get_packet(stream.clone()) {
+                        Ok(Some(packet)) => return Ok(packet),
+                        Ok(None) => {},
+                        Err(DecoderError::ShortData) => {},
+                        _ => return Err(DemuxerError::InvalidData),
+                    };
+                    match br.read_buf_some(&mut buf) {
+                        Ok(size) => {
+                            pkt.add_data(&buf[..size]);
+                        },
+                        Err(_) => {
+                            match pkt.get_packet(stream.clone()) {
+                                Ok(Some(packet)) => return Ok(packet),
+                                Ok(None) | Err(DecoderError::ShortData) => return Err(DemuxerError::EOF),
+                                _ => return Err(DemuxerError::InvalidData),
+                            };
+                        },
+                    };
+                }
+            },
+            _ => unreachable!(),
+        }
+    }
+    pub fn seek(&mut self, seek_time: NATimePoint) -> DemuxerResult<()> {
+        match *self {
+            DemuxerObject::Normal(ref mut dmx) => dmx.seek(seek_time),
+            DemuxerObject::Raw(ref mut dmx, _, _) => dmx.seek(seek_time),
+            _ => Err(DemuxerError::NotImplemented),
+        }
+    }
+}
+
+pub fn detect_tags(br: &mut ByteReader) -> (bool, u64, Option<u64>) {
+    let mut is_raw = false;
+    let mut start = 0;
+    let mut end = None;
+
+    // check for ID3v{2-4}
+    let mut buf = [0; 5];
+    br.peek_buf(&mut buf).unwrap();
+    if &buf[0..3] == b"ID3" && buf[3] > 0 && buf[3] < 5 && buf[4] == 0 { //ID3 tag found, must be a raw stream
+        br.read_skip(6).unwrap();
+        let mut size = 0;
+        for _ in 0..4 {
+            let b = br.read_byte().unwrap();
+            if (b & 0x80) != 0 {
+                println!("Invalid ID3 size");
+                break;
+            }
+            size = (size << 7) | u64::from(b);
+        }
+        start = size + 10;
+        is_raw = true;
+    }
+    // check for ID3v1
+    br.seek(SeekFrom::End(-128)).unwrap();
+    let off = br.tell();
+    br.peek_buf(&mut buf[..3]).unwrap();
+    if &buf[0..3] == b"TAG" {
+        end = Some(off);
+    }
+    // check for APETAG
+    let mut buf = [0; 8];
+    if let Some(off) = end {
+        br.seek(SeekFrom::Start(off - 32)).unwrap();
+    } else {
+        br.seek(SeekFrom::End(-32)).unwrap();
+    }
+    let off = br.tell();
+    br.read_buf(&mut buf).unwrap();
+    if &buf == b"APETAGEX" {
+        let ver     = br.read_u32le().unwrap();
+        let size    = u64::from(br.read_u32le().unwrap());
+        let _items  = br.read_u32le().unwrap();
+        let flags   = br.read_u32le().unwrap();
+        if ver == 1000 || (flags & 0x80000000) == 0 {
+            end = Some(off - size + 32);
+        } else {
+            end = Some(off - size);
+        }
+    }
+
+    (is_raw, start, end)
+}
index 618c2271b14c9950d8d6429a93ce0d542baf0ec6..b58577f0611f6a7cd47e17f590d9aec0b71b6de7 100644 (file)
@@ -5,7 +5,7 @@ extern crate nihav_registry;
 
 use std::fs::File;
 use std::io::prelude::*;
-use std::io::{BufReader, SeekFrom};
+use std::io::BufReader;
 use std::sync::mpsc;
 use std::time::Duration;
 use std::thread;
@@ -15,16 +15,16 @@ use nihav_core::frame::*;
 use nihav_core::codecs::*;
 use nihav_core::demuxers::*;
 use nihav_core::soundcvt::*;
-use nihav_registry::detect;
 
 mod allreg;
 mod command;
 use command::*;
+mod demux;
+use demux::*;
 
 struct Player {
     ended:      bool,
-    dmx_reg:    RegisteredDemuxers,
-    dec_reg:    RegisteredDecoders,
+    full_reg:   allreg::FullRegister,
     paused:     bool,
     mute:       bool,
     volume:     u8,
@@ -92,7 +92,7 @@ impl Drop for AudioDevice {
 }
 
 struct Decoder<'a> {
-    demuxer:    Demuxer<'a>,
+    demuxer:    DemuxerObject<'a>,
     decoder:    Box<dyn NADecoder>,
     dsupp:      Box<NADecoderSupport>,
     buf:        &'a mut Vec<i16>,
@@ -215,10 +215,7 @@ fn format_time(ms: u64) -> String {
 
 impl Player {
     fn new() -> Self {
-        let mut dmx_reg = RegisteredDemuxers::new();
-        allreg::register_all_demuxers(&mut dmx_reg);
-        let mut dec_reg = RegisteredDecoders::new();
-        allreg::register_all_decoders(&mut dec_reg);
+        let full_reg = allreg::FullRegister::new();
 
         unsafe {
             if sdl2_sys::SDL_Init(sdl2_sys::SDL_INIT_AUDIO) != 0 {
@@ -229,7 +226,7 @@ impl Player {
         Self {
             ended:  false,
             paused: false,
-            dmx_reg, dec_reg,
+            full_reg,
             volume: 100,
             mute:   false,
             debug:  false,
@@ -242,35 +239,27 @@ impl Player {
             println!("error opening {}", name);
             return;
         }
-        let mut file = ret.unwrap();
+        let file = ret.unwrap();
+        let file = BufReader::new(file);
 
-        let mut fr = FileReader::new_read(&mut file);
+        let mut fr = FileReader::new_read(file);
         let mut br = ByteReader::new(&mut fr);
-        let res = detect::detect_format(name, &mut br);
-        if res.is_none() {
-            println!("cannot detect format for {}", name);
-            return;
-        }
-        let (dmx_name, _) = res.unwrap();
-        drop(br);
-        drop(fr);
-        let dmx_fact = self.dmx_reg.find_demuxer(dmx_name);
-        if dmx_fact.is_none() {
-            println!("no demuxer for format {}", dmx_name);
-            return;
-        }
-        let dmx_fact = dmx_fact.unwrap();
+        let (is_raw, start, end) = detect_tags(&mut br);
 
-        file.seek(SeekFrom::Start(0)).unwrap();
-        let mut file = BufReader::new(file);
-        let mut fr = FileReader::new_read(&mut file);
-        let mut br = ByteReader::new(&mut fr);
-        let res = create_demuxer(dmx_fact, &mut br);
-        if res.is_err() {
-            println!("cannot create demuxer");
+        let mut nfr: Box<dyn ByteIO>;
+        if start != 0 || end.is_some() {
+            //println!(" limiting range to {:X}-{:X}", start, end.unwrap_or(0));
+            let file = fr.finish();
+            nfr = Box::new(BoundedFileReader::new_read(file, start, end).unwrap());
+        } else {
+            nfr = Box::new(fr);
+        }
+        let mut br = ByteReader::new(nfr.as_mut());
+        let dmx = DemuxerObject::create(&mut br, &self.full_reg, name, is_raw);
+        if dmx.is_none() {
+            println!("No demuxer found!");
             return;
         }
-        let dmx = res.unwrap();
 
         let mut ainfo = None;
         let mut dec: Option<(Box<NADecoderSupport>, Box<dyn NADecoder>)> = None;
@@ -280,7 +269,7 @@ impl Player {
             let s = dmx.get_stream(i).unwrap();
             let info = s.get_info();
             if info.is_audio() {
-                let decfunc = self.dec_reg.find_decoder(info.get_name());
+                let decfunc = self.full_reg.dec_reg.find_decoder(info.get_name());
                 if decfunc.is_none() {
                     println!("no decoder for {}", info.get_name());
                     continue;