]> git.nihav.org Git - nihav.git/commitdiff
mov: improve audio seeking (hopefully)
authorKostya Shishkov <kostya.shishkov@gmail.com>
Tue, 15 Dec 2020 17:56:29 +0000 (18:56 +0100)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Tue, 15 Dec 2020 17:56:29 +0000 (18:56 +0100)
nihav-commonfmt/src/demuxers/mov.rs

index da5059185777e1dab73d03b245649e3218f5ef73..8e448faae2416783665f29bedb48f4ce6c882a4c 100644 (file)
@@ -1240,8 +1240,37 @@ impl Track {
                         self.samples_left = csamp - self.cur_sample;
                         self.cur_chunk += 1;
                     }
-                } else {
+                } else if self.chunk_offsets.len() == self.chunk_sizes.len() {
                     self.cur_chunk = self.cur_sample;
+                } else {
+                    let mut csamp = 0;
+                    self.cur_chunk = 0;
+                    let mut cmap = self.sample_map.iter();
+                    let mut cur_samps = 0;
+                    let (mut next_idx, mut next_samples) = cmap.next().unwrap();
+                    loop {
+                        if self.cur_chunk + 1 == next_idx as usize {
+                            self.samples_left = cur_samps;
+                            cur_samps = next_samples as usize;
+                            if let Some((new_idx, new_samples)) = cmap.next() {
+                                next_idx = *new_idx;
+                                next_samples = *new_samples;
+                            }
+                        }
+                        csamp += cur_samps;
+                        if csamp > self.cur_sample {
+                            if self.cur_chunk >= self.chunk_offsets.len() {
+                                return Err(DemuxerError::SeekError);
+                            }
+                            self.last_offset = self.chunk_offsets[self.cur_chunk];
+                            break;
+                        }
+                        self.cur_chunk += 1;
+                    }
+                    self.cur_sample = csamp - cur_samps;
+                    self.samples_left = cur_samps;
+                    self.last_offset = self.chunk_offsets[self.cur_chunk];
+                    self.cur_chunk += 1;
                 }
             } else {
                 self.cur_chunk = self.cur_sample;
@@ -1325,11 +1354,33 @@ impl<'a> DemuxCore<'a> for MOVDemuxer<'a> {
     fn seek(&mut self, time: NATimePoint, seek_index: &SeekIndex) -> DemuxerResult<()> {
         let ret = seek_index.find_pos(time);
         if ret.is_none() {
+            if let NATimePoint::Milliseconds(_) = time {
+                let mut aonly = true;
+                for track in self.tracks.iter() {
+                    if track.stream_type != StreamType::Audio || !track.raw_audio {
+                        aonly = false;
+                        break;
+                    }
+                }
+                if aonly {
+                    for track in self.tracks.iter_mut() {
+                        track.seek(0, time)?;
+                    }
+                    return Ok(());
+                }
+            }
             return Err(DemuxerError::SeekError);
         }
         let seek_info = ret.unwrap();
+        let tbn = self.tracks[seek_info.str_id as usize].tb_num;
+        let tbd = self.tracks[seek_info.str_id as usize].tb_den;
         for track in self.tracks.iter_mut() {
-            track.seek(seek_info.pts, time)?;
+            let cur_pts = if track.track_id == seek_info.str_id {
+                    seek_info.pts
+                } else {
+                    seek_info.pts * u64::from(tbn) * u64::from(track.tb_den) / (u64::from(tbd) * u64::from(track.tb_num))
+                };
+            track.seek(cur_pts, time)?;
         }
         Ok(())
     }