]> git.nihav.org Git - nihav.git/commitdiff
core/frame: add time to timestamp conversion utility function
authorKostya Shishkov <kostya.shishkov@gmail.com>
Wed, 13 Nov 2019 18:41:07 +0000 (19:41 +0100)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Wed, 13 Nov 2019 18:41:07 +0000 (19:41 +0100)
nihav-core/src/frame.rs

index 091ba99a688aac8e077ba3b4f1304146c9d713ed..b1a81b72874b40bcf2a73656fd6daf10ea8a855d 100644 (file)
@@ -663,6 +663,28 @@ impl NATimeInfo {
     pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
     pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
     pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
+
+    pub fn time_to_ts(time: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 {
+        let tb_num = tb_num as u64;
+        let tb_den = tb_den as u64;
+        let tmp = time.checked_mul(tb_num);
+        if let Some(tmp) = tmp {
+            tmp / base / tb_den
+        } else {
+            let tmp = time.checked_mul(tb_num);
+            if let Some(tmp) = tmp {
+                tmp / base / tb_den
+            } else {
+                let coarse = time / base;
+                let tmp = coarse.checked_mul(tb_num);
+                if let Some(tmp) = tmp {
+                    tmp / tb_den
+                } else {
+                    (coarse / tb_den) * tb_num
+                }
+            }
+        }
+    }
 }
 
 #[allow(dead_code)]