]> git.nihav.org Git - nihav.git/commitdiff
vmd: implement optional seeking to arbitrary position
authorKostya Shishkov <kostya.shishkov@gmail.com>
Thu, 2 Oct 2025 16:33:23 +0000 (18:33 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Thu, 2 Oct 2025 16:33:23 +0000 (18:33 +0200)
nihav-game/src/codecs/vmd.rs
nihav-game/src/demuxers/vmd.rs

index 02c76dda88c0d032a0dc0a95ad10b38017baab21..82da8da5b36f2d017620c7697de28e7d5f0a8458 100644 (file)
@@ -329,6 +329,9 @@ impl NADecoder for VMDVideoDecoder {
         Ok(frm.into_ref())
     }
     fn flush(&mut self) {
+        for el in self.framebuf.iter_mut() {
+            *el = 0;
+        }
     }
 }
 
index fd48a6611aae8550e6742f1a5f8d83324cd4ba46..41c14aa5c9da9a26297c177579f6411c750a5fd6 100644 (file)
@@ -28,6 +28,7 @@ struct VMDDemuxer<'a> {
     is_indeo:   bool,
     is_lhaud:   bool,
     frames:     Vec<FrameRec>,
+    force_seek: bool,
 }
 
 impl<'a> DemuxCore<'a> for VMDDemuxer<'a> {
@@ -178,17 +179,55 @@ impl<'a> DemuxCore<'a> for VMDDemuxer<'a> {
         Ok(pkt)
     }
 
-    fn seek(&mut self, _time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> {
-        Err(DemuxerError::NotPossible)
+    fn seek(&mut self, time: NATimePoint, _seek_index: &SeekIndex) -> DemuxerResult<()> {
+        if !self.force_seek {
+            Err(DemuxerError::NotPossible)
+        } else {
+            let seek_ts = match time {
+                    NATimePoint::Milliseconds(ms) => ms * 12 / 1000,
+                    NATimePoint::PTS(ts) => ts,
+                    _ => return Err(DemuxerError::SeekError),
+                };
+            let mut fno = 0;
+            for frame in self.frames.iter() {
+                if frame.chtype == CHTYPE_VIDEO && u64::from(frame.ts) >= seek_ts {
+                    break;
+                }
+                fno += 1;
+            }
+            if fno >= self.frames.len() {
+                return Err(DemuxerError::SeekError);
+            }
+            self.fno = fno;
+            Ok(())
+        }
     }
 
     fn get_duration(&self) -> u64 { 0 }
 }
 
+const DEMUXER_OPTS: &[NAOptionDefinition] = &[
+    NAOptionDefinition {
+        name: FORCE_SEEK_OPTION, description: FORCE_SEEK_OPTION_DESC,
+        opt_type: NAOptionDefinitionType::Bool },
+];
+
 impl<'a> NAOptionHandler for VMDDemuxer<'a> {
-    fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] }
-    fn set_options(&mut self, _options: &[NAOption]) { }
-    fn query_option_value(&self, _name: &str) -> Option<NAValue> { None }
+    fn get_supported_options(&self) -> &[NAOptionDefinition] { DEMUXER_OPTS }
+    fn set_options(&mut self, options: &[NAOption]) {
+        for option in options.iter() {
+            if let NAOption { name: FORCE_SEEK_OPTION, value: NAValue::Bool(ref bval) } = option {
+                self.force_seek = *bval;
+            }
+        }
+    }
+    fn query_option_value(&self, name: &str) -> Option<NAValue> {
+        if name == FORCE_SEEK_OPTION {
+            Some(NAValue::Bool(self.force_seek))
+        } else {
+            None
+        }
+    }
 }
 
 impl<'a> VMDDemuxer<'a> {
@@ -201,6 +240,7 @@ impl<'a> VMDDemuxer<'a> {
             is_indeo:   false,
             is_lhaud:   false,
             frames:     Vec::new(),
+            force_seek: false,
         }
     }
 }