From aa7e65d284a733dee69a47ec9a3f612884ba4a17 Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Tue, 19 Feb 2019 13:47:52 +0100 Subject: [PATCH] VMD fixes --- nihav-game/src/codecs/vmd.rs | 22 +++++++++++++++++----- nihav-game/src/demuxers/vmd.rs | 11 ++++++++--- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/nihav-game/src/codecs/vmd.rs b/nihav-game/src/codecs/vmd.rs index c6d98aa..bd8e772 100644 --- a/nihav-game/src/codecs/vmd.rs +++ b/nihav-game/src/codecs/vmd.rs @@ -330,7 +330,7 @@ impl VMDAudioDecoder { } let mut ch = 0; let flip_ch = if channels == 2 { 1 } else { 0 }; - for _ in 1..self.blk_align { + for _ in channels..self.blk_align*channels { let b = br.read_byte()? as usize; if (b & 0x80) != 0 { pred[ch] -= SOL_AUD_STEPS16[b & 0x7F] as i32; @@ -358,12 +358,13 @@ impl NADecoder for VMDAudioDecoder { fmt = SND_U8_FORMAT; self.is16bit = false; self.blk_size = ainfo.get_block_len(); + self.blk_align = ainfo.get_block_len() / (ainfo.get_channels() as usize); } else { fmt = SND_S16P_FORMAT; self.is16bit = true; self.blk_size = (ainfo.get_block_len() + 1) * (ainfo.get_channels() as usize); + self.blk_align = ainfo.get_block_len(); }; - self.blk_align = ainfo.get_block_len() / (ainfo.get_channels() as usize); self.ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), ainfo.get_channels(), fmt, ainfo.get_block_len()); self.chmap = NAChannelMap::from_str(if ainfo.get_channels() == 1 { "C" } else { "L,R" }).unwrap(); Ok(()) @@ -410,12 +411,21 @@ impl NADecoder for VMDAudioDecoder { for i in 0..self.blk_align * channels { dst[doff + i] = 0; } - } else { - for i in 0..self.blk_align * channels { + } else if channels == 1 { + for i in 0..self.blk_size { dst[doff + i] = br.read_byte()?; } + } else { + for i in 0..self.blk_size { + let val = br.read_byte()?; + if val < 128 { + dst[doff + i] = 127 - val; + } else { + dst[doff + i] = val; + } + } } - doff += self.blk_align; + doff += self.blk_align * channels; mask >>= 1; } } @@ -461,6 +471,8 @@ mod test { // let file = "assets/1491.VMD"; let file = "assets/128.vmd"; +// let file = "assets/1000.VMD"; +// let file = "assets/235.VMD"; test_decode_audio("vmd", file, None, "vmd", &dmx_reg, &dec_reg); } } diff --git a/nihav-game/src/demuxers/vmd.rs b/nihav-game/src/demuxers/vmd.rs index 74fe3a7..a41967e 100644 --- a/nihav-game/src/demuxers/vmd.rs +++ b/nihav-game/src/demuxers/vmd.rs @@ -54,11 +54,11 @@ impl<'a> DemuxCore<'a> for VMDDemuxer<'a> { self.vid_id = strmgr.add_stream(NAStream::new(StreamType::Video, 0, vinfo, 1, 12)).unwrap(); let srate = read_u16le(&header[804..])? as u32; + let block_size; if srate > 0 { let bsize = read_u16le(&header[806..])? as usize; - let channels = if (header[811] & 0x80) != 0 { 2 } else { 1 }; + let channels = if (header[811] & 0x8F) != 0 { 2 } else { 1 }; let is16bit; - let block_size; if (bsize & 0x8000) != 0 { is16bit = true; block_size = 0x10000 - bsize; @@ -70,6 +70,8 @@ impl<'a> DemuxCore<'a> for VMDDemuxer<'a> { let ahdr = NAAudioInfo::new(srate, channels, if is16bit { SND_S16P_FORMAT } else { SND_U8_FORMAT }, block_size); let ainfo = NACodecInfo::new("vmd-audio", NACodecTypeInfo::Audio(ahdr), None); self.aud_id = strmgr.add_stream(NAStream::new(StreamType::Audio, 1, ainfo, 1, srate)).unwrap(); + } else { + block_size = 0; } let adelay = read_u16le(&header[808..])? as u32; @@ -88,9 +90,12 @@ impl<'a> DemuxCore<'a> for VMDDemuxer<'a> { for _ in 0..fpb { let chtype = src.read_byte()?; src.read_skip(1)?; - let size = src.read_u32le()?; + let mut size = src.read_u32le()?; let mut hdr: [u8; FRAME_HDR_SIZE] = [0; FRAME_HDR_SIZE]; src.read_buf(&mut hdr)?; + if (i == 0) && (chtype == CHTYPE_AUDIO) && (size > 4) && ((size as usize) < block_size/2) { + size += 0x10000; + } if (chtype == CHTYPE_VIDEO || chtype == CHTYPE_AUDIO) && (size > 0) { let ts = if (i == 0) || (chtype != CHTYPE_AUDIO) { i as u32 -- 2.39.5