From 266da7b9db18727dc065a4e8971ecb0ac0b1e45f Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Wed, 13 Nov 2019 19:41:07 +0100 Subject: [PATCH] core/frame: add time to timestamp conversion utility function --- nihav-core/src/frame.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/nihav-core/src/frame.rs b/nihav-core/src/frame.rs index 091ba99..b1a81b7 100644 --- a/nihav-core/src/frame.rs +++ b/nihav-core/src/frame.rs @@ -663,6 +663,28 @@ impl NATimeInfo { pub fn set_pts(&mut self, pts: Option) { self.pts = pts; } pub fn set_dts(&mut self, dts: Option) { self.dts = dts; } pub fn set_duration(&mut self, dur: Option) { 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)] -- 2.39.5