fix clippy warnings
[nihav.git] / nihav-commonfmt / src / demuxers / mov.rs
index 7ac68bd72d7e8975414b66b72fb257aa791870a8..87eb2f17a9ff366b9082a683480f08aecf8c3bcf 100644 (file)
@@ -573,6 +573,7 @@ fn parse_audio_edata(br: &mut ByteReader, start_pos: u64, size: u64) -> DemuxerR
     }
 }
 
+#[allow(clippy::neg_cmp_op_on_partial_ord)]
 fn read_stsd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult<u64> {
     const KNOWN_STSD_SIZE: u64 = 24;
     validate!(size >= KNOWN_STSD_SIZE);
@@ -625,12 +626,12 @@ fn read_stsd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult
                 match depth & 0x1F {
                     2 => {
                         let mut pal = [0; 1024];
-                        (&mut pal[..4 * 4]).copy_from_slice(&MOV_DEFAULT_PAL_2BIT);
+                        pal[..4 * 4].copy_from_slice(&MOV_DEFAULT_PAL_2BIT);
                         track.pal = Some(Arc::new(pal));
                     },
                     4 => {
                         let mut pal = [0; 1024];
-                        (&mut pal[..16 * 4]).copy_from_slice(&MOV_DEFAULT_PAL_4BIT);
+                        pal[..16 * 4].copy_from_slice(&MOV_DEFAULT_PAL_4BIT);
                         track.pal = Some(Arc::new(pal));
                     },
                     8 => {
@@ -754,7 +755,9 @@ fn read_stsd(track: &mut Track, br: &mut ByteReader, size: u64) -> DemuxerResult
                     track.bsize = (sample_size / 8) as usize;
                 },
             };
-            track.tb_den = sample_rate;
+            if track.tb_den <= 1 {
+                track.tb_den = sample_rate;
+            }
             track.raw_audio = matches!(&fcc,
                     b"NONE" | b"raw " | b"twos" | b"sowt" |
                     b"in24" | b"in32" | b"fl32" | b"fl64" |
@@ -1274,7 +1277,7 @@ impl Track {
         for kf_time in self.keyframes.iter() {
             let pts = tsearch.map_time(*kf_time - 1, &self.time_to_sample);
             let time = NATimeInfo::ts_to_time(pts, 1000, self.tb_num, self.tb_den);
-            seek_index.add_entry(self.track_no as u32, SeekEntry { time, pts: u64::from(*kf_time - 1), pos: 0 });
+            seek_index.add_entry(self.track_no, SeekEntry { time, pts: u64::from(*kf_time - 1), pos: 0 });
         }
     }
     fn calculate_chunk_size(&self, nsamp: usize) -> usize {
@@ -1323,7 +1326,7 @@ impl Track {
                     Some(dts)
                 }
             } else {
-                None
+                Some(pts_val)
             };
         let mut pts = NATimeInfo::new(Some(pts_val), dts, None, self.tb_num, self.tb_den);
         if self.chunk_offsets.len() == self.chunk_sizes.len() { // simple one-to-one mapping
@@ -1407,7 +1410,8 @@ impl Track {
         }
     }
     #[allow(clippy::collapsible_if)]
-    fn seek(&mut self, pts: u64, tpoint: NATimePoint) -> DemuxerResult<()> {
+    #[allow(clippy::collapsible_else_if)]
+    fn seek(&mut self, pts: u64, tpoint: NATimePoint) -> DemuxerResult<u64> {
         self.cur_sample = pts as usize;
         self.samples_left = 0;
         self.cur_ts = None;
@@ -1480,6 +1484,25 @@ impl Track {
                 } else if self.chunk_offsets.len() == self.chunk_sizes.len() {
                     self.cur_chunk = self.cur_sample;
                 } else {
+                    if !self.time_to_sample.is_empty() {
+                        let mut remaining = exp_pts;
+                        let mut abs_csamp = 0;
+                        for &(count, scount) in self.time_to_sample.iter() {
+                            let count = u64::from(count);
+                            let scount = u64::from(scount);
+                            let nblk = remaining / scount;
+                            if nblk < count {
+                                abs_csamp += nblk;
+                                break;
+                            }
+                            remaining -= count * scount;
+                            abs_csamp += count;
+                        }
+                        self.cur_sample = abs_csamp as usize;
+                    } else {
+                        self.cur_sample = exp_pts as usize;
+                    }
+                    let tgt_sample = self.cur_sample;
                     let mut csamp = 0;
                     self.cur_chunk = 0;
                     let mut cmap = self.sample_map.iter();
@@ -1497,6 +1520,9 @@ impl Track {
                         csamp += cur_samps;
                         if csamp > self.cur_sample {
                             if self.cur_chunk >= self.chunk_offsets.len() {
+                                self.cur_sample = csamp - cur_samps;
+                                self.samples_left = 0;
+                                self.cur_sample = csamp;
                                 return Err(DemuxerError::SeekError);
                             }
                             self.last_offset = self.chunk_offsets[self.cur_chunk];
@@ -1508,6 +1534,15 @@ impl Track {
                     self.samples_left = cur_samps;
                     self.last_offset = self.chunk_offsets[self.cur_chunk];
                     self.cur_chunk += 1;
+
+                    // try to refine sample position
+                    if self.chunk_sizes.len() > self.chunk_offsets.len() {
+                        for i in self.cur_sample..tgt_sample {
+                            self.cur_sample   += 1;
+                            self.samples_left -= 1;
+                            self.last_offset  += u64::from(self.chunk_sizes[i]);
+                        }
+                    }
                 }
             } else {
                 self.cur_chunk = self.cur_sample;
@@ -1544,7 +1579,9 @@ impl Track {
             self.samples_left = csamp + cur_samps - self.cur_sample;
             self.cur_chunk += 1;
         }
-        Ok(())
+        let cur_pts = self.timesearch.map_time(self.cur_sample as u32, &self.time_to_sample);
+        let cur_time = NATimeInfo::ts_to_time(cur_pts, 1000, self.tb_num, self.tb_den);
+        Ok(cur_time)
     }
 }
 
@@ -1693,7 +1730,7 @@ impl<'a> DemuxCore<'a> for MOVDemuxer<'a> {
             if let NATimePoint::Milliseconds(_) = time {
                 let mut aonly = true;
                 for track in self.tracks.iter() {
-                    if track.stream_type != StreamType::Audio || !track.raw_audio {
+                    if track.stream_type != StreamType::Audio {
                         aonly = false;
                         break;
                     }
@@ -1710,14 +1747,36 @@ impl<'a> DemuxCore<'a> for MOVDemuxer<'a> {
         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;
+        let mut vpts = None;
+        let mut apts = None;
         for track in self.tracks.iter_mut() {
             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)?;
+            let actual_time = track.seek(cur_pts, time)?;
+            match track.stream_type {
+                StreamType::Video => vpts = Some(actual_time),
+                StreamType::Audio => apts = Some(actual_time),
+                _ => {},
+            };
+        }
+        /* For audio+video stream case when the post-seek actual times differ
+           by more than half a second try to seek audio to a closer position
+           to video.
+        */
+        if let (true, Some(vtime), Some(atime)) = (self.tracks.len() == 2, vpts, apts) {
+            if vtime.max(atime) - vtime.min(atime) > 500 && atime != 0 {
+                for track in self.tracks.iter_mut() {
+                    if track.stream_type == StreamType::Audio {
+                        let new_pts = NATimeInfo::time_to_ts(vtime, 1000, track.tb_num, track.tb_den);
+                        track.seek(new_pts, NATimePoint::Milliseconds(vtime))?;
+                    }
+                }
+            }
         }
+
         Ok(())
     }
     fn get_duration(&self) -> u64 {