]> git.nihav.org Git - nihav.git/commitdiff
VMD fixes
authorKostya Shishkov <kostya.shishkov@gmail.com>
Tue, 19 Feb 2019 12:47:52 +0000 (13:47 +0100)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Tue, 19 Feb 2019 12:47:52 +0000 (13:47 +0100)
nihav-game/src/codecs/vmd.rs
nihav-game/src/demuxers/vmd.rs

index c6d98aa2d52dcfc1b26967883372e971426f8005..bd8e772694b98ddce92589f79f31009c9c14ab3f 100644 (file)
@@ -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);
     }
 }
index 74fe3a799b3243a9d9c707e69717875a78c79d8c..a41967e2c9aa642768b2b37d3100926c60d1dc8e 100644 (file)
@@ -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