From: Kostya Shishkov Date: Sat, 23 Jan 2021 14:27:38 +0000 (+0100) Subject: FutureVision game formats support X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=8d91d85f878bac4d415d313cebe450865a520f35;p=nihav.git FutureVision game formats support --- diff --git a/nihav-game/Cargo.toml b/nihav-game/Cargo.toml index c13f9f3..6aab363 100644 --- a/nihav-game/Cargo.toml +++ b/nihav-game/Cargo.toml @@ -18,9 +18,11 @@ nihav_commonfmt = { path = "../nihav-commonfmt" } [features] default = ["all_decoders", "all_demuxers"] demuxers = [] -all_demuxers = ["demuxer_bmv", "demuxer_bmv3", "demuxer_gdv", "demuxer_vmd", "demuxer_vx"] +all_demuxers = ["demuxer_bmv", "demuxer_bmv3", "demuxer_fcmp", "demuxer_fst", "demuxer_gdv", "demuxer_vmd", "demuxer_vx"] demuxer_bmv = ["demuxers"] demuxer_bmv3 = ["demuxers"] +demuxer_fcmp = ["demuxers"] +demuxer_fst = ["demuxers"] demuxer_gdv = ["demuxers"] demuxer_vmd = ["demuxers"] demuxer_vx = ["demuxers"] @@ -28,14 +30,16 @@ demuxer_vx = ["demuxers"] all_decoders = ["all_video_decoders", "all_audio_decoders"] decoders = [] -all_video_decoders = ["decoder_bmv", "decoder_bmv3", "decoder_gdvvid", "decoder_midivid", "decoder_midivid3", "decoder_vmd", "decoder_vx"] +all_video_decoders = ["decoder_bmv", "decoder_bmv3", "decoder_fstvid", "decoder_gdvvid", "decoder_midivid", "decoder_midivid3", "decoder_vmd", "decoder_vx"] decoder_bmv = ["decoders"] decoder_bmv3 = ["decoders"] +decoder_fstvid = ["decoders"] decoder_gdvvid = ["decoders"] decoder_midivid = ["decoders"] decoder_midivid3 = ["decoders"] decoder_vmd = ["decoders"] decoder_vx = ["decoders"] -all_audio_decoders = ["decoder_lhst500f22"] +all_audio_decoders = ["decoder_fstaud", "decoder_lhst500f22"] +decoder_fstaud = ["decoders"] decoder_lhst500f22 = ["decoders"] diff --git a/nihav-game/src/codecs/futurevision.rs b/nihav-game/src/codecs/futurevision.rs new file mode 100644 index 0000000..c59e9bb --- /dev/null +++ b/nihav-game/src/codecs/futurevision.rs @@ -0,0 +1,250 @@ +use nihav_core::frame::*; +use nihav_core::formats; +use nihav_core::formats::NAChannelMap; +use nihav_core::codecs::*; +use nihav_core::io::byteio::*; +use nihav_codec_support::codecs::imaadpcm::IMAState; + +struct FutureVisionVideoDecoder { + info: NACodecInfoRef, + pal: [u8; 768], + frame: Vec, + w: usize, + h: usize, +} + +struct Bits8<'a> { + src: &'a [u8], + pos: usize, + buf: u8, + bit: u8, +} + +impl<'a> Bits8<'a> { + fn new(src: &'a [u8]) -> Self { Bits8 { src, pos: 0, buf: 0, bit: 0 } } + fn read_bit(&mut self) -> ByteIOResult { + if self.bit == 0 { + if self.pos < self.src.len() { + self.buf = self.src[self.pos]; + self.pos += 1; + self.bit = 8; + } else { + return Err(ByteIOError::ReadError); + } + } + let bit = (self.buf & 0x80) != 0; + self.buf <<= 1; + self.bit -= 1; + Ok(bit) + } +} + +impl FutureVisionVideoDecoder { + fn new() -> Self { + FutureVisionVideoDecoder { + info: NACodecInfoRef::default(), + pal: [0; 768], + frame: Vec::new(), + w: 0, + h: 0, + } + } + + fn output_frame(&mut self, bufinfo: &mut NABufferType, w: usize, h: usize) { + let bufo = bufinfo.get_vbuf(); + let mut buf = bufo.unwrap(); + let paloff = buf.get_offset(1); + let stride = buf.get_stride(0); + let data = buf.get_data_mut().unwrap(); + let dst = data.as_mut_slice(); + + dst[paloff..][..768].copy_from_slice(&self.pal); + for (dline, sline) in dst.chunks_mut(stride).zip(self.frame.chunks(w)).take(h) { + dline[..w].copy_from_slice(sline); + } + } +} + +impl NADecoder for FutureVisionVideoDecoder { + fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { + if let NACodecTypeInfo::Video(vinfo) = info.get_properties() { + let w = vinfo.get_width(); + let h = vinfo.get_height(); + validate!((w & 1) == 0 && (h & 1) == 0); + let fmt = PAL8_FORMAT; + let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, false, fmt)); + self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref(); + self.w = w; + self.h = h; + + self.frame.resize(w * h, 0); + self.pal = [0; 768]; + Ok(()) + } else { + Err(DecoderError::InvalidData) + } + } + fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult { + let src = pkt.get_buffer(); + validate!(src.len() >= 4); + + let bitsize = read_u16le(&src)? as usize; + let bsize = (bitsize + 8) >> 3; + validate!(bsize + 2 <= src.len()); + + let mut flags = Bits8::new(&src[2..][..bsize]); + let mut mr = MemoryReader::new_read(&src[2 + bsize..]); + let mut br = ByteReader::new(&mut mr); + + if (bsize + 2 != src.len()) && flags.read_bit()? { + for dst in self.pal.iter_mut() { + let b = br.read_byte()?; + *dst = (b << 2) | (b >> 4); + } + } + + let mut is_intra = true; + let stride = self.w; + // for some reason last row should not be decoded + for row4 in self.frame.chunks_mut(stride * 4).take(self.h / 4 - 1) { + for x in (0..self.w).step_by(4) { + if flags.read_bit()? { + if flags.read_bit()? { + let c0 = br.read_byte()?; + let c1 = br.read_byte()?; + let mut mask = br.read_u16le()?; + for dst in row4[x..].chunks_mut(stride) { + for pix in dst.iter_mut().take(4) { + *pix = if (mask & 0x8000) != 0 { c1 } else { c0 }; + mask <<= 1; + } + } + } else { + for dst in row4[x..].chunks_mut(stride) { + br.read_buf(&mut dst[..4])?; + } + } + } else { + is_intra = false; + } + } + } + + let mut bufinfo = alloc_video_buffer(self.info.get_properties().get_video_info().unwrap(), 0)?; + + self.output_frame(&mut bufinfo, self.w, self.h); + + let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); + frm.set_keyframe(is_intra); + frm.set_frame_type(if is_intra { FrameType::I } else { FrameType::P }); + Ok(frm.into_ref()) + } + fn flush(&mut self) { + } +} + +impl NAOptionHandler for FutureVisionVideoDecoder { + fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } + fn set_options(&mut self, _options: &[NAOption]) { } + fn query_option_value(&self, _name: &str) -> Option { None } +} + +pub fn get_decoder_video() -> Box { + Box::new(FutureVisionVideoDecoder::new()) +} + +struct FutureVisionAudioDecoder { + ainfo: NAAudioInfo, + chmap: NAChannelMap, + state: IMAState, + count: usize, +} + +impl FutureVisionAudioDecoder { + fn new() -> Self { + FutureVisionAudioDecoder { + ainfo: NAAudioInfo::new(0, 1, formats::SND_S16_FORMAT, 0), + chmap: NAChannelMap::from_ms_mapping(0x4), //single channel + state: IMAState::new(), + count: 0, + } + } +} + +impl NADecoder for FutureVisionAudioDecoder { + fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { + if let NACodecTypeInfo::Audio(ainfo) = info.get_properties() { + self.ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), 1, formats::SND_S16P_FORMAT, 1); + Ok(()) + } else { + Err(DecoderError::InvalidData) + } + } + fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult { + let info = pkt.get_stream().get_info(); + if let NACodecTypeInfo::Audio(_) = info.get_properties() { + let pktbuf = pkt.get_buffer(); + let samples = pktbuf.len() * 2; + let abuf = alloc_audio_buffer(self.ainfo, samples, self.chmap.clone())?; + let mut adata = abuf.get_abuf_i16().unwrap(); + let buf = adata.get_data_mut().unwrap(); + for (dst, &val) in buf.chunks_exact_mut(2).zip(pktbuf.iter()) { + dst[0] = self.state.expand_sample(val & 0xF); + dst[1] = self.state.expand_sample(val >> 4); + if self.count < 50 { + dst[0] = 0; + dst[1] = 0; + } + self.count += 2; + } + let mut frm = NAFrame::new_from_pkt(pkt, info, abuf); + frm.set_duration(Some(samples as u64)); + frm.set_keyframe(false); + Ok(frm.into_ref()) + } else { + Err(DecoderError::InvalidData) + } + } + fn flush(&mut self) { + } +} + +impl NAOptionHandler for FutureVisionAudioDecoder { + fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } + fn set_options(&mut self, _options: &[NAOption]) { } + fn query_option_value(&self, _name: &str) -> Option { None } +} + +pub fn get_decoder_audio() -> Box { + Box::new(FutureVisionAudioDecoder::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; + + #[test] + fn test_fst_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("fst", "fst-video", "assets/Game/alarm.fst", None, &dmx_reg, &dec_reg, + ExpectedTestResult::MD5([0x4028440a, 0xcb8aed5b, 0x2a9f1ead, 0x269169f5])); + } + #[test] + fn test_fst_audio() { + 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("fcmp", "fst-audio", "assets/Game/anxiety.cmp", None, &dmx_reg, &dec_reg, + ExpectedTestResult::MD5([0xa45b65b3, 0xe0654352, 0xf553e90b, 0x5dce0023])); + } +} diff --git a/nihav-game/src/codecs/mod.rs b/nihav-game/src/codecs/mod.rs index d54abb6..d98233b 100644 --- a/nihav-game/src/codecs/mod.rs +++ b/nihav-game/src/codecs/mod.rs @@ -8,6 +8,8 @@ macro_rules! validate { pub mod bmv; #[cfg(feature="decoder_bmv3")] pub mod bmv3; +#[cfg(any(feature="decoder_fstvid",feature="decoder_fstaud"))] +pub mod futurevision; #[cfg(feature="decoder_gdvvid")] pub mod gremlinvideo; #[cfg(feature="decoder_lhst500f22")] @@ -36,6 +38,10 @@ const GAME_CODECS: &[DecoderInfo] = &[ DecoderInfo { name: "bmv3-audio", get_decoder: bmv3::get_decoder_audio }, #[cfg(feature="decoder_bmv3")] DecoderInfo { name: "bmv3-video", get_decoder: bmv3::get_decoder_video }, +#[cfg(feature="decoder_fstaud")] + DecoderInfo { name: "fst-audio", get_decoder: futurevision::get_decoder_audio }, +#[cfg(feature="decoder_fstvid")] + DecoderInfo { name: "fst-video", get_decoder: futurevision::get_decoder_video }, #[cfg(feature="decoder_vmd")] DecoderInfo { name: "vmd-audio", get_decoder: vmd::get_decoder_audio }, #[cfg(feature="decoder_vmd")] diff --git a/nihav-game/src/demuxers/fst.rs b/nihav-game/src/demuxers/fst.rs new file mode 100644 index 0000000..b6cc066 --- /dev/null +++ b/nihav-game/src/demuxers/fst.rs @@ -0,0 +1,230 @@ +use nihav_core::frame::*; +use nihav_core::demuxers::*; + +#[allow(dead_code)] +struct FutureVisionVideoDemuxer<'a> { + src: &'a mut ByteReader<'a>, + cur_frame: usize, + apos: u64, + vsize: Vec, + asize: Vec, + a_id: Option, + v_id: Option, + vframe: bool, +} + +impl<'a> DemuxCore<'a> for FutureVisionVideoDemuxer<'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"2TSF"); + let width = src.read_u32le()? as usize; + let height = src.read_u32le()? as usize; + validate!(width != 0 && height != 0); + let _flags = src.read_u32le()?; + let nframes = src.read_u32le()? as usize; + let fps = src.read_u32le()?; + let arate = src.read_u32le()?; + let abits = src.read_u32le()?; + + let vhdr = NAVideoInfo::new(width, height, false, PAL8_FORMAT); + let vci = NACodecTypeInfo::Video(vhdr); + let vinfo = NACodecInfo::new("fst-video", vci, None); + self.v_id = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, fps, nframes as u64)); + if arate != 0 { + validate!(abits == 8 || abits == 16); + let ahdr = NAAudioInfo::new(arate, 1, if abits == 16 { SND_S16_FORMAT } else { SND_U8_FORMAT }, 2); + let ainfo = NACodecInfo::new("pcm", NACodecTypeInfo::Audio(ahdr), None); + self.a_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, arate, 2)); + } + self.vsize = Vec::with_capacity(nframes); + self.asize = Vec::with_capacity(nframes); + for _ in 0..nframes { + let vsize = src.read_u32le()? as usize; + let asize = src.read_u16le()? as usize; + self.vsize.push(vsize); + self.asize.push(asize); + } + self.vframe = true; + self.cur_frame = 0; + self.apos = 0; + Ok(()) + } + + #[allow(unused_variables)] + fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult { + if self.cur_frame >= self.vsize.len() { return Err(DemuxerError::EOF); } + let (id, size, pts) = if self.vframe { + self.vframe = self.a_id.is_none(); + + (self.v_id.unwrap_or(0), self.vsize[self.cur_frame], self.cur_frame as u64) + } else { + self.vframe = true; + let apos = self.apos; + self.apos += (self.asize[self.cur_frame] as u64) * 2; + + (self.a_id.unwrap_or(0), self.asize[self.cur_frame], apos) + }; + + if self.vframe { + self.cur_frame += 1; + } + + let str = strmgr.get_stream(id).unwrap(); + let (tb_num, tb_den) = str.get_timebase(); + let ts = NATimeInfo::new(Some(pts), None, None, tb_num, tb_den); + self.src.read_packet(str, ts, true, size) + } + + fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> { + Err(DemuxerError::NotPossible) + } + fn get_duration(&self) -> u64 { 0 } +} +impl<'a> NAOptionHandler for FutureVisionVideoDemuxer<'a> { + fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } + fn set_options(&mut self, _options: &[NAOption]) { } + fn query_option_value(&self, _name: &str) -> Option { None } +} +impl<'a> FutureVisionVideoDemuxer<'a> { + fn new(io: &'a mut ByteReader<'a>) -> Self { + FutureVisionVideoDemuxer { + src: io, + cur_frame: 0, + apos: 0, + vsize: Vec::new(), + asize: Vec::new(), + a_id: None, + v_id: None, + vframe: false, + } + } +} + +pub struct FSTDemuxerCreator { } + +impl DemuxerCreator for FSTDemuxerCreator { + fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box + 'a> { + Box::new(FutureVisionVideoDemuxer::new(br)) + } + fn get_name(&self) -> &'static str { "fst" } +} + +#[allow(dead_code)] +struct FutureVisionAudioDemuxer<'a> { + src: &'a mut ByteReader<'a>, + a_id: usize, + end: u64, + arate: u32, +} + +impl<'a> DemuxCore<'a> for FutureVisionAudioDemuxer<'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"FCMP"); + let size = u64::from(src.read_u32le()?); + let arate = src.read_u32le()?; + validate!(arate != 0); + let abits = src.read_u16le()?; + validate!(abits == 8 || abits == 16); + + let ahdr = NAAudioInfo::new(arate, 1, if abits == 16 { SND_S16_FORMAT } else { SND_U8_FORMAT }, 2); + let ainfo = NACodecInfo::new("fst-audio", NACodecTypeInfo::Audio(ahdr), None); + self.a_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 0, ainfo, 1, arate, 2)).unwrap(); + self.end = self.src.tell() + size; + self.arate = arate; + Ok(()) + } + + #[allow(unused_variables)] + fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult { + if self.src.tell() >= self.end { return Err(DemuxerError::EOF); } + let size = (self.end - self.src.tell()).min(0x2000) as usize; + let pts = (self.src.tell() - 14) * 2; + + let str = strmgr.get_stream(self.a_id).unwrap(); + let (tb_num, tb_den) = str.get_timebase(); + let ts = NATimeInfo::new(Some(pts), None, None, tb_num, tb_den); + self.src.read_packet(str, ts, true, size) + } + + fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> { + Err(DemuxerError::NotPossible) + } + fn get_duration(&self) -> u64 { (self.end - 14) * 2000 / u64::from(self.arate) } +} +impl<'a> NAOptionHandler for FutureVisionAudioDemuxer<'a> { + fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } + fn set_options(&mut self, _options: &[NAOption]) { } + fn query_option_value(&self, _name: &str) -> Option { None } +} +impl<'a> FutureVisionAudioDemuxer<'a> { + fn new(io: &'a mut ByteReader<'a>) -> Self { + FutureVisionAudioDemuxer { + src: io, + a_id: 0, + end: 0, + arate: 0, + } + } +} + +pub struct FCMPDemuxerCreator { } + +impl DemuxerCreator for FCMPDemuxerCreator { + fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box + 'a> { + Box::new(FutureVisionAudioDemuxer::new(br)) + } + fn get_name(&self) -> &'static str { "fcmp" } +} + +#[cfg(test)] +mod test { + use super::*; + use std::fs::File; + + #[test] + fn test_fst_demux() { + let mut file = File::open("assets/Game/c007.fst").unwrap(); + let mut fr = FileReader::new_read(&mut file); + let mut br = ByteReader::new(&mut fr); + let mut dmx = FutureVisionVideoDemuxer::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_fcmp_demux() { + let mut file = File::open("assets/Game/anxiety.cmp").unwrap(); + let mut fr = FileReader::new_read(&mut file); + let mut br = ByteReader::new(&mut fr); + let mut dmx = FutureVisionAudioDemuxer::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); + } + } +} diff --git a/nihav-game/src/demuxers/mod.rs b/nihav-game/src/demuxers/mod.rs index efc83ac..fcd3dcd 100644 --- a/nihav-game/src/demuxers/mod.rs +++ b/nihav-game/src/demuxers/mod.rs @@ -7,6 +7,8 @@ macro_rules! validate { #[cfg(any(feature="demuxer_bmv",feature="demuxer_bmv3"))] mod bmv; +#[cfg(any(feature="demuxer_fst",feature="demuxer_fcmp"))] +mod fst; #[cfg(feature="demuxer_gdv")] mod gdv; #[cfg(feature="demuxer_vmd")] @@ -19,6 +21,10 @@ const GAME_DEMUXERS: &[&dyn DemuxerCreator] = &[ &bmv::BMVDemuxerCreator {}, #[cfg(feature="demuxer_bmv3")] &bmv::BMV3DemuxerCreator {}, +#[cfg(feature="demuxer_fcmp")] + &fst::FCMPDemuxerCreator {}, +#[cfg(feature="demuxer_fst")] + &fst::FSTDemuxerCreator {}, #[cfg(feature="demuxer_gdv")] &gdv::GDVDemuxerCreator {}, #[cfg(feature="demuxer_vmd")] diff --git a/nihav-registry/src/detect.rs b/nihav-registry/src/detect.rs index e916dc6..a34670e 100644 --- a/nihav-registry/src/detect.rs +++ b/nihav-registry/src/detect.rs @@ -220,6 +220,16 @@ const DETECTORS: &[DetectConditions] = &[ &CC::Str(b"moov")), &CC::Str(b"ftyp")) }], }, + DetectConditions { + demux_name: "fcmp", + extensions: ".cmp", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"FCMP")}], + }, + DetectConditions { + demux_name: "fst", + extensions: ".fst", + conditions: &[CheckItem{offs: 0, cond: &CC::Str(b"2TSF")}], + }, DetectConditions { demux_name: "gdv", extensions: ".gdv", diff --git a/nihav-registry/src/register.rs b/nihav-registry/src/register.rs index d4e2b02..fac4e04 100644 --- a/nihav-registry/src/register.rs +++ b/nihav-registry/src/register.rs @@ -227,6 +227,8 @@ static CODEC_REGISTER: &'static [CodecDescription] = &[ desc!(audio; "bmv-audio", "BMV audio"), desc!(video; "bmv3-video", "DW Noir BMV video"), desc!(audio; "bmv3-audio", "DW Noir BMV audio"), + desc!(video; "fst-video", "FutureVision video"), + desc!(audio; "fst-audio", "FutureVision audio"), desc!(video; "midivid", "MidiVid"), desc!(video; "midivid3", "MidiVid 3"), desc!(video-ll; "midivid-ll", "MidiVid Lossless"),