]> git.nihav.org Git - nihav.git/blobdiff - nihav-game/src/codecs/vmd.rs
game: fix or silence clippy warnings
[nihav.git] / nihav-game / src / codecs / vmd.rs
index 904fe1fc998a0a027c617a6b8a4fdaf3ae7bab1f..110548a7917642d84266a666a35a129c6a9201cd 100644 (file)
@@ -335,6 +335,12 @@ impl NADecoder for VMDVideoDecoder {
     }
 }
 
+impl NAOptionHandler for VMDVideoDecoder {
+    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_video() -> Box<dyn NADecoder + Send> {
     Box::new(VMDVideoDecoder::new())
@@ -435,9 +441,6 @@ impl VMDAudioDecoder {
             pred + i32::from(SOL_AUD_STEPS16[(val & 0x7F) as usize])
         }
     }
-    fn cvt_u8(val: u8) -> u8 {
-        val ^ 0x80
-    }
 }
 
 impl NADecoder for VMDAudioDecoder {
@@ -448,7 +451,7 @@ impl NADecoder for VMDAudioDecoder {
             let edata = info.get_extradata();
             let flags = if let Some(ref buf) = edata {
                     validate!(buf.len() >= 2);
-                    (buf[0] as u16) | ((buf[1] as u16) << 8)
+                    u16::from(buf[0]) | (u16::from(buf[1]) << 8)
                 } else {
                     0
                 };
@@ -465,24 +468,27 @@ impl NADecoder for VMDAudioDecoder {
                     self.is_odd = (channels == 2) && ((self.blk_size & 1) != 0);
                 }
             } else {
-                fmt = SND_S16P_FORMAT;
                 self.blk_align = ainfo.get_block_len();
                 if (flags & 0x10) == 0 {
+                    fmt = SND_S16P_FORMAT;
                     self.blk_size = (ainfo.get_block_len() + 1) * channels;
                     self.mode = VMDAudioMode::DPCM;
                 } else {
-                    self.blk_size = ainfo.get_block_len() / 2 + 3;
+                    fmt = SND_S16_FORMAT;
+                    self.blk_size = (ainfo.get_block_len() * channels + 1) / 2 + 3 * channels;
                     self.mode = VMDAudioMode::ADPCM;
                 }
             };
             self.ainfo = NAAudioInfo::new(ainfo.get_sample_rate(), ainfo.get_channels(), fmt, ainfo.get_block_len());
-            self.info = info.replace_info(NACodecTypeInfo::Audio(self.ainfo.clone()));
+            self.info = info.replace_info(NACodecTypeInfo::Audio(self.ainfo));
             self.chmap = NAChannelMap::from_str(if channels == 1 { "C" } else { "L,R" }).unwrap();
             Ok(())
         } else {
             Err(DecoderError::InvalidData)
         }
     }
+    #[allow(clippy::identity_op)]
+    #[allow(clippy::cyclomatic_complexity)]
     fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> {
         let info = pkt.get_stream().get_info();
         if let NACodecTypeInfo::Audio(_) = info.get_properties() {
@@ -532,10 +538,7 @@ impl NADecoder for VMDAudioDecoder {
                                 dst[doff + i]       = br.read_byte()?;
                             }
                         } else {
-                            for i in 0..self.blk_size {
-                                let val             = Self::cvt_u8(br.read_byte()?);
-                                dst[doff + i] = val;
-                            }
+                            unreachable!();
                         }
                         doff += self.blk_align * channels;
                         mask >>= 1;
@@ -603,7 +606,10 @@ impl NADecoder for VMDAudioDecoder {
                         let mut ima = IMAState::new();
                         for _ in 0..nblocks {
                             if (mask & 1) != 0 {
-                                doff += (self.blk_size - 3) * 2;
+                                for i in 0..self.blk_align {
+                                    dst[doff + i] = 0;
+                                }
+                                doff += self.blk_align;
                                 mask >>= 1;
                                 continue;
                             }
@@ -611,17 +617,48 @@ impl NADecoder for VMDAudioDecoder {
                             let step                        = br.read_byte()?;
                             validate!((step as usize) < IMA_STEP_TABLE.len());
                             ima.reset(pred, step);
-                            for _ in 3..self.blk_size {
+                            let mut b = 0;
+                            for i in 0..self.blk_align {
+                                if (i & 1) == 0 {
+                                    b                       = br.read_byte()?;
+                                    dst[doff] = ima.expand_sample(b >> 4);
+                                } else {
+                                    dst[doff] = ima.expand_sample(b & 0xF);
+                                }
+                                doff += 1;
+                            }
+                            mask >>= 1;
+                        }
+                    } else {
+                        let mut mask = mask;
+                        let mut ima1 = IMAState::new();
+                        let mut ima2 = IMAState::new();
+                        for _ in 0..nblocks {
+                            if (mask & 1) != 0 {
+                                for i in 0..self.blk_align * 2 {
+                                    dst[doff + i] = 0;
+                                }
+                                doff += self.blk_align * 2;
+                                mask >>= 1;
+                                continue;
+                            }
+                            let pred1                       = br.read_u16le()? as i16;
+                            let pred2                       = br.read_u16le()? as i16;
+                            let step1                       = br.read_byte()?;
+                            let step2                       = br.read_byte()?;
+                            validate!((step1 as usize) < IMA_STEP_TABLE.len());
+                            validate!((step2 as usize) < IMA_STEP_TABLE.len());
+                            ima1.reset(pred1, step1);
+                            ima2.reset(pred2, step2);
+                            for _ in 0..self.blk_align {
                                 let b                       = br.read_byte()?;
-                                dst[doff] = ima.expand_sample(b >> 4);
+                                dst[doff] = ima1.expand_sample(b >> 4);
                                 doff += 1;
-                                dst[doff] = ima.expand_sample(b & 0xF);
+                                dst[doff] = ima2.expand_sample(b & 0xF);
                                 doff += 1;
                             }
                             mask >>= 1;
                         }
-                    } else {
-                        return Err(DecoderError::InvalidData);
                     }
                 },
             };
@@ -638,6 +675,12 @@ impl NADecoder for VMDAudioDecoder {
     }
 }
 
+impl NAOptionHandler for VMDAudioDecoder {
+    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_audio() -> Box<dyn NADecoder + Send> {
     Box::new(VMDAudioDecoder::new())
 }