]> git.nihav.org Git - nihav.git/commitdiff
add Arxel Tribe video support
authorKostya Shishkov <kostya.shishkov@gmail.com>
Sat, 12 Nov 2022 11:44:39 +0000 (12:44 +0100)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Sat, 12 Nov 2022 11:44:39 +0000 (12:44 +0100)
nihav-game/Cargo.toml
nihav-game/src/codecs/arxel_vid.rs [new file with mode: 0644]
nihav-game/src/codecs/mod.rs
nihav-game/src/demuxers/cnm.rs [new file with mode: 0644]
nihav-game/src/demuxers/mod.rs
nihav-registry/src/detect.rs
nihav-registry/src/register.rs

index 4e6ca3134de7530bfdfa4be44175ecc34b63717e..24ccc06232c2e50c61d89fbe454298c3d00ee936 100644 (file)
@@ -18,9 +18,10 @@ nihav_commonfmt = { path = "../nihav-commonfmt", default-features=false, feature
 [features]
 default = ["all_decoders", "all_demuxers"]
 demuxers = []
-all_demuxers = ["demuxer_bmv", "demuxer_bmv3", "demuxer_fcmp", "demuxer_fst", "demuxer_gdv", "demuxer_hl_fmv", "demuxer_imax", "demuxer_q", "demuxer_siff", "demuxer_smush", "demuxer_vmd", "demuxer_vx"]
+all_demuxers = ["demuxer_bmv", "demuxer_bmv3", "demuxer_cnm", "demuxer_fcmp", "demuxer_fst", "demuxer_gdv", "demuxer_hl_fmv", "demuxer_imax", "demuxer_q", "demuxer_siff", "demuxer_smush", "demuxer_vmd", "demuxer_vx"]
 demuxer_bmv = ["demuxers"]
 demuxer_bmv3 = ["demuxers"]
+demuxer_cnm = ["demuxers"]
 demuxer_fcmp = ["demuxers"]
 demuxer_fst = ["demuxers"]
 demuxer_gdv = ["demuxers"]
@@ -35,7 +36,8 @@ demuxer_vx = ["demuxers"]
 all_decoders = ["all_video_decoders", "all_audio_decoders"]
 decoders = []
 
-all_video_decoders = ["decoder_beam_fcp", "decoder_beam_vbv", "decoder_bmv", "decoder_bmv3", "decoder_fstvid", "decoder_gdvvid", "decoder_hl_fmv", "decoder_imax", "decoder_ipma", "decoder_midivid", "decoder_midivid3", "decoder_q", "decoder_smush_video", "decoder_vmd", "decoder_vx"]
+all_video_decoders = ["decoder_arxel_vid", "decoder_beam_fcp", "decoder_beam_vbv", "decoder_bmv", "decoder_bmv3", "decoder_fstvid", "decoder_gdvvid", "decoder_hl_fmv", "decoder_imax", "decoder_ipma", "decoder_midivid", "decoder_midivid3", "decoder_q", "decoder_smush_video", "decoder_vmd", "decoder_vx"]
+decoder_arxel_vid = ["decoders"]
 decoder_beam_fcp = ["decoders"]
 decoder_beam_vbv = ["decoders"]
 decoder_bmv = ["decoders"]
diff --git a/nihav-game/src/codecs/arxel_vid.rs b/nihav-game/src/codecs/arxel_vid.rs
new file mode 100644 (file)
index 0000000..b2d6815
--- /dev/null
@@ -0,0 +1,182 @@
+use nihav_core::codecs::*;
+use nihav_core::io::byteio::*;
+use nihav_core::io::bitreader::*;
+
+const HEADER_SIZE: usize = 0x2F;
+
+const MV_2BIT: [(i8, i8); 4] = [(-1, 0), (-1, -1), (1, -1), (0, -2)];
+const MV_4BIT: [(i8, i8); 16] = [
+    (-2, -3), ( 2, -3), (-1, -4), ( 1, -4),
+    (-1, -2), ( 1, -2), ( 0, -3), ( 0, -4),
+    (-2,  0), (-2, -1), ( 1, -1), (-2, -2),
+    ( 2, -2), (-1, -3), ( 1, -3), ( 0, -5)
+];
+
+const BPP: usize = 4;
+
+struct ArxelVideoDecoder {
+    info:       NACodecInfoRef,
+    tiles:      [u8; 65536],
+}
+
+impl ArxelVideoDecoder {
+    fn new() -> Self {
+        Self {
+            info:   NACodecInfoRef::default(),
+            tiles:  [0; 65536],
+        }
+    }
+}
+
+const RGBA_FORMAT: NAPixelFormaton = NAPixelFormaton {
+        model: ColorModel::RGB(RGBSubmodel::RGB), components: 4,
+        comp_info: [
+            Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 2, next_elem: 4 }),
+            Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 1, next_elem: 4 }),
+            Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 0, next_elem: 4 }),
+            Some(NAPixelChromaton{ h_ss: 0, v_ss: 0, packed: true, depth: 8, shift: 0, comp_offs: 3, next_elem: 4 }),
+            None ],
+        elem_size: 4, be: false, alpha: true, palette: false };
+
+impl NADecoder for ArxelVideoDecoder {
+    fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> {
+        if let NACodecTypeInfo::Video(vinfo) = info.get_properties() {
+            let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(vinfo.get_width(), vinfo.get_height(), true, RGBA_FORMAT));
+            self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref();
+
+            Ok(())
+        } else {
+            Err(DecoderError::InvalidData)
+        }
+    }
+    fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
+        let src = pkt.get_buffer();
+        validate!(src.len() > HEADER_SIZE);
+
+        let mut mr = MemoryReader::new_read(&src);
+        let mut br = ByteReader::new(&mut mr);
+
+        let size                            = br.read_u32le()? as usize;
+        validate!(src.len() >= size + HEADER_SIZE);
+        let part2_off                       = br.read_u32le()? as u64;
+        validate!(part2_off > 0 && part2_off < (size as u64));
+        let num_tiles                       = br.read_u16le()? as usize;
+        validate!(num_tiles > 0 && num_tiles < 4096);
+        let tile_size                       = br.read_u16le()? as usize;
+        let width                           = br.read_u32le()? as usize;
+        let height                          = br.read_u32le()? as usize;
+
+        let vinfo = self.info.get_properties().get_video_info().unwrap();
+        validate!(width == vinfo.get_width());
+        validate!(height == vinfo.get_height());
+
+                                              br.seek(SeekFrom::Start(part2_off + (HEADER_SIZE as u64)))?;
+        match tile_size {
+            2 => {
+                return Err(DecoderError::NotImplemented);
+            },
+            4 => {
+                                              br.read_buf(&mut self.tiles[..16])?;
+                let off = br.tell() as usize;
+                let mut bir = BitReader::new(&src[off..], BitReaderMode::BE);
+                for tile in 1..num_tiles {
+                    for i in 0..16 {
+                        self.tiles[tile * 16 + i] = self.tiles[tile * 16 + i - 16];
+                    }
+                    let bits                = bir.read(3)? as u8 + 1;
+                    validate!(bits < 8);
+                    for el in self.tiles[tile * 16..][..16].iter_mut() {
+                        let mut delta       = bir.read(bits)? as i16;
+                        if delta != 0 && bir.read_bool()? {
+                            delta = -delta;
+                        }
+                        *el = (i16::from(*el) + delta) as u8;
+                    }
+                }
+            },
+            _ => {
+                validate!(tile_size > 0);
+                                              br.read_buf(&mut self.tiles[..tile_size])?;
+            },
+        };
+
+        let bufinfo = alloc_video_buffer(vinfo, 0)?;
+        let bufo = bufinfo.get_vbuf();
+        let mut buf = bufo.unwrap();
+        let stride = buf.get_stride(0);
+        let data = buf.get_data_mut().unwrap();
+        let dst = data.as_mut_slice();
+
+        let mut br = BitReader::new(&src[HEADER_SIZE..], BitReaderMode::BE);
+        let tile_w = 4;
+        let tsize = tile_w * BPP;
+        let idx_bits = if num_tiles < 0x400 { 10 } else if num_tiles < 0x800 { 11 } else { 12 };
+        for y in (0..height).step_by(2) {
+            for x in (0..width).step_by(tile_w) {
+                let dst_pos = x * BPP + y * stride;
+                if !br.read_bool()? {
+                    let idx             = br.read(idx_bits)? as usize;
+                    validate!(idx < num_tiles);
+                    dst[dst_pos..][..tsize].copy_from_slice(&self.tiles[idx * tsize..][..tsize]);
+                } else {
+                    let (mv_x, mv_y) = if br.read_bool()? {
+                            (0, -1)
+                        } else if br.read_bool()? {
+                            MV_2BIT[br.read(2)? as usize]
+                        } else {
+                            MV_4BIT[br.read(4)? as usize]
+                        };
+
+                    let isrc = (dst_pos as isize) + isize::from(mv_x) * (tsize as isize) + isize::from(mv_y) * ((stride * 2) as isize);
+                    validate!(isrc >= 0);
+                    let src_pos = isrc as usize;
+                    validate!(src_pos + tsize <= dst.len());
+                    let (src, dst) = dst.split_at_mut(dst_pos);
+                    dst[..tsize].copy_from_slice(&src[src_pos..][..tsize]);
+                }
+            }
+            // double lines
+            let lines = &mut dst[y * stride..];
+            let (src, dst) = lines.split_at_mut(stride);
+            dst[..stride].copy_from_slice(src);
+        }
+
+        let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo);
+        frm.set_keyframe(true);
+        frm.set_frame_type(FrameType::I);
+        Ok(frm.into_ref())
+    }
+    fn flush(&mut self) {
+    }
+}
+
+impl NAOptionHandler for ArxelVideoDecoder {
+    fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
+    fn set_options(&mut self, _options: &[NAOption]) { }
+    fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
+}
+
+
+pub fn get_decoder() -> Box<dyn NADecoder + Send> {
+    Box::new(ArxelVideoDecoder::new())
+}
+
+#[cfg(test)]
+mod test {
+    use nihav_core::codecs::RegisteredDecoders;
+    use nihav_core::demuxers::RegisteredDemuxers;
+    use nihav_codec_support::test::dec_video::*;
+    use crate::game_register_all_decoders;
+    use crate::game_register_all_demuxers;
+    // sample from the Ring game
+    #[test]
+    fn test_arxel_video() {
+        let mut dmx_reg = RegisteredDemuxers::new();
+        game_register_all_demuxers(&mut dmx_reg);
+        let mut dec_reg = RegisteredDecoders::new();
+        game_register_all_decoders(&mut dec_reg);
+
+        test_decoding("arxel-cnm", "arxel-video", "assets/Game/logo.cnm", Some(10), &dmx_reg, &dec_reg,
+                      ExpectedTestResult::MD5([0xcf12f83a, 0xfdce1ed2, 0x2d183394, 0xa265f164]));
+    }
+}
index 4d6710e09553f0ede5d58bf48b807296100000c1..c31c6a3134a12b79f12bb7a947e333e26e117182 100644 (file)
@@ -5,6 +5,8 @@ macro_rules! validate {
     ($a:expr) => { if !$a { println!("check failed at {}:{}", file!(), line!()); return Err(DecoderError::InvalidData); } };
 }
 
+#[cfg(feature="decoder_arxel_vid")]
+pub mod arxel_vid;
 #[cfg(any(feature="decoder_beam_vbv",feature="decoder_beam_fcp"))]
 pub mod beam;
 #[cfg(feature="decoder_bmv")]
@@ -43,6 +45,8 @@ const GAME_CODECS: &[DecoderInfo] = &[
     DecoderInfo { name: "gdv-audio", get_decoder: gremlinvideo::get_decoder_audio },
 #[cfg(feature="decoder_gdvvid")]
     DecoderInfo { name: "gdv-video", get_decoder: gremlinvideo::get_decoder_video },
+#[cfg(feature="decoder_arxel_vid")]
+    DecoderInfo { name: "arxel-video", get_decoder: arxel_vid::get_decoder },
 #[cfg(feature="decoder_beam_fcp")]
     DecoderInfo { name: "beam-fcp", get_decoder: beam::get_decoder_fcp },
 #[cfg(feature="decoder_beam_vbv")]
diff --git a/nihav-game/src/demuxers/cnm.rs b/nihav-game/src/demuxers/cnm.rs
new file mode 100644 (file)
index 0000000..1c25ec4
--- /dev/null
@@ -0,0 +1,217 @@
+use nihav_core::frame::*;
+use nihav_core::demuxers::*;
+
+struct ArxelCinemaDemuxer<'a> {
+    src:        &'a mut ByteReader<'a>,
+    cur_frame:  usize,
+    astreams:   usize,
+    offsets:    Vec<u32>,
+    vpts:       u64,
+    tb_num:     u32,
+    tb_den:     u32,
+}
+
+impl<'a> DemuxCore<'a> for ArxelCinemaDemuxer<'a> {
+    #[allow(unused_variables)]
+    fn open(&mut self, strmgr: &mut StreamManager, _seek_index: &mut SeekIndex) -> DemuxerResult<()> {
+        let src = &mut self.src;
+
+        let magic                       = src.read_tag()?;
+        validate!(&magic == b"CNM ");
+        let magic                       = src.read_tag()?;
+        validate!(&magic == b"UNR\x00");
+        let _duration                   = src.read_u32le()?;
+        let tb_den                      = src.read_u32le()?;
+                                          src.read_byte()?;
+        let width                       = src.read_u32le()? as usize;
+        let height                      = src.read_u32le()? as usize;
+        validate!(width != 0 && height != 0);
+                                          src.read_u16le()?;
+        self.astreams                   = src.read_byte()? as usize;
+        validate!(self.astreams < 4);
+        if self.astreams > 1 {
+            return Err(DemuxerError::NotImplemented);
+        }
+        let nframes                     = src.read_u32le()? as usize;
+                                          src.read_u32le()?; //nframes again?
+        let tab_size                    = src.read_u32le()? as usize;
+        validate!(tab_size == nframes * 8);
+                                          src.read_skip(0x98)?;
+
+        let vhdr = NAVideoInfo::new(width, height, true, RGB24_FORMAT);
+        let vci = NACodecTypeInfo::Video(vhdr);
+        let vinfo = NACodecInfo::new("arxel-video", vci, None);
+        if strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 100, tb_den, nframes as u64)).is_none() {
+            return Err(DemuxerError::MemoryError);
+        }
+        if let Some(stream) = strmgr.get_stream_by_id(0) {
+            let (tb_num, tb_den) = stream.get_timebase();
+            self.tb_num = tb_num;
+            self.tb_den = tb_den;
+        }
+
+        for trk in 0..(self.astreams as u32) {
+            let channels                = src.read_byte()? + 1;
+            let abits                   = src.read_byte()?;
+            let arate                   = src.read_u32le()?;
+                                          src.read_skip(10)?;
+            validate!(channels > 0);
+            validate!(abits >= 8);
+            validate!(arate > 0);
+
+            let ahdr = NAAudioInfo::new(arate, channels, if abits > 8 { SND_S16_FORMAT } else { SND_U8_FORMAT }, 2);
+            let ainfo = NACodecInfo::new("pcm", NACodecTypeInfo::Audio(ahdr), None);
+            if strmgr.add_stream(NAStream::new(StreamType::Audio, 1 + trk, ainfo, 1, arate, 0)).is_none() {
+                return Err(DemuxerError::MemoryError);
+            }
+        }
+
+        let tab_size = tab_size / 4;
+        self.offsets = Vec::with_capacity(tab_size);
+        for _ in 0..tab_size {
+            let offset                  = src.read_u32le()?;
+            self.offsets.push(offset);
+        }
+        self.cur_frame = 0;
+        self.vpts = 0;
+        Ok(())
+    }
+
+    #[allow(unused_variables)]
+    fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket> {
+        loop {
+            if self.cur_frame >= self.offsets.len() { return Err(DemuxerError::EOF); }
+            let pos = u64::from(self.offsets[self.cur_frame]);
+            let stream_id = (self.cur_frame % (self.astreams.max(1) + 1)) as u32;
+            self.cur_frame += 1;
+            if pos == 0 {
+                continue;
+            }
+
+                                          self.src.seek(SeekFrom::Start(pos))?;
+            let ftype                   = self.src.read_byte()?;
+            match ftype {
+                0x41 | 0x42 | 0x5A => {
+                    let size            = self.src.read_u32le()? as usize;
+                    if size == 0 {
+                        continue;
+                    }
+                    if let Some(stream) = strmgr.get_stream_by_id(stream_id) {
+                        let ts = stream.make_ts(None, None, None);
+                        return self.src.read_packet(stream, ts, true, size);
+                    } else {
+                                          self.src.read_skip(size)?;
+                    }
+                },
+                0x53 => {
+                    let size            = self.src.peek_u32le()? as usize;
+                    if let Some(stream) = strmgr.get_stream_by_id(0) {
+                        let ts = stream.make_ts(Some(self.vpts), None, None);
+                        self.vpts += 1;
+                        return self.src.read_packet(stream, ts, true, size + 0x2F);
+                    } else {
+                        return Err(DemuxerError::MemoryError);
+                    }
+                },
+                _ => continue,
+            };
+        }
+    }
+
+    fn seek(&mut self, time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> {
+        match time {
+            NATimePoint::PTS(pts) => self.seek_to_frame(pts),
+            NATimePoint::Milliseconds(ms) => {
+                if self.tb_num == 0 || self.tb_den == 0 {
+                    return Err(DemuxerError::NotPossible);
+                }
+                let pts = NATimeInfo::time_to_ts(ms, 1000, self.tb_num, self.tb_den);
+                self.seek_to_frame(pts)
+            },
+            _ => Err(DemuxerError::NotPossible)
+        }
+    }
+    fn get_duration(&self) -> u64 { 0 }
+}
+impl<'a> NAOptionHandler for ArxelCinemaDemuxer<'a> {
+    fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
+    fn set_options(&mut self, _options: &[NAOption]) { }
+    fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
+}
+impl<'a> ArxelCinemaDemuxer<'a> {
+    fn new(io: &'a mut ByteReader<'a>) -> Self {
+        ArxelCinemaDemuxer {
+            src:        io,
+            cur_frame:  0,
+            astreams:   0,
+            offsets:    Vec::new(),
+            vpts:       0,
+            tb_num:     0,
+            tb_den:     0,
+        }
+    }
+    fn seek_to_frame(&mut self, pts: u64) -> DemuxerResult<()> {
+        let nframe = (pts as usize) * (1 + self.astreams.max(1));
+        if nframe < self.offsets.len() {
+            self.cur_frame = nframe;
+            Ok(())
+        } else {
+            Err(DemuxerError::SeekError)
+        }
+    }
+}
+
+pub struct CNMDemuxerCreator { }
+
+impl DemuxerCreator for CNMDemuxerCreator {
+    fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a> {
+        Box::new(ArxelCinemaDemuxer::new(br))
+    }
+    fn get_name(&self) -> &'static str { "arxel-cnm" }
+}
+
+#[cfg(test)]
+mod test {
+    use super::*;
+    use std::fs::File;
+
+    // samples from the Ring game
+    #[test]
+    fn test_cnm_demux() {
+        let mut file = File::open("assets/Game/logo.cnm").unwrap();
+        let mut fr = FileReader::new_read(&mut file);
+        let mut br = ByteReader::new(&mut fr);
+        let mut dmx = ArxelCinemaDemuxer::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 as i32) == (DemuxerError::EOF as i32) { break; }
+                panic!("error");
+            }
+            let pkt = pktres.unwrap();
+            println!("Got {}", pkt);
+        }
+    }
+    #[test]
+    fn test_cnm_noaud_demux() {
+        let mut file = File::open("assets/Game/tr_as_ro_d.cnm").unwrap();
+        let mut fr = FileReader::new_read(&mut file);
+        let mut br = ByteReader::new(&mut fr);
+        let mut dmx = ArxelCinemaDemuxer::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 as i32) == (DemuxerError::EOF as i32) { break; }
+                panic!("error");
+            }
+            let pkt = pktres.unwrap();
+            println!("Got {}", pkt);
+        }
+    }
+}
index b3320785df644664431a7a8b2b3ce38e01f9fc3c..f1449652c2a38432a1c4088dc0a4ece6fbc5efaa 100644 (file)
@@ -7,6 +7,8 @@ macro_rules! validate {
 
 #[cfg(any(feature="demuxer_bmv",feature="demuxer_bmv3"))]
 mod bmv;
+#[cfg(feature="demuxer_cnm")]
+mod cnm;
 #[cfg(any(feature="demuxer_fst",feature="demuxer_fcmp"))]
 mod fst;
 #[cfg(feature="demuxer_gdv")]
@@ -31,6 +33,8 @@ const GAME_DEMUXERS: &[&dyn DemuxerCreator] = &[
     &bmv::BMVDemuxerCreator {},
 #[cfg(feature="demuxer_bmv3")]
     &bmv::BMV3DemuxerCreator {},
+#[cfg(feature="demuxer_cnm")]
+    &cnm::CNMDemuxerCreator {},
 #[cfg(feature="demuxer_fcmp")]
     &fst::FCMPDemuxerCreator {},
 #[cfg(feature="demuxer_fst")]
index dd095d436697cc6398466cfc21b0d8be448abe74..bfe1d44a301656edf49b7d977996d581c2107517 100644 (file)
@@ -253,6 +253,11 @@ const DETECTORS: &[DetectConditions] = &[
         conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"DKIF\x00\x00")},
                       CheckItem{offs: 6, cond: &CC::Ge(Arg::U16LE(32))}],
     },
+    DetectConditions {
+        demux_name: "arxel-cnm",
+        extensions: ".cnm",
+        conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"CNM UNR\x00")}],
+    },
     DetectConditions {
         demux_name: "fcmp",
         extensions: ".cmp",
index 9b0c392bfdd310d6fbd55ee97d14b7c00c852b8b..c8d3b60ba26435b338e7a3955d3fc306b17d6b6c 100644 (file)
@@ -243,6 +243,7 @@ static CODEC_REGISTER: &[CodecDescription] = &[
 
     desc!(video;    "gdv-video",     "Gremlin Digital Video - video"),
     desc!(audio;    "gdv-audio",     "Gremlin Digital Video - audio"),
+    desc!(video-im; "arxel-video",   "Arxel Tribe Video"),
     desc!(video;    "beam-fcp",      "Beam Software Animation"),
     desc!(video;    "beam-video",    "Beam Software Video"),
     desc!(video;    "bmv-video",     "BMV video"),