From 03740c31c092cafde46aa2ee62d6272e1c3f9761 Mon Sep 17 00:00:00 2001 From: lummax Date: Sun, 25 Oct 2015 13:31:51 +0100 Subject: [PATCH] util/mathematics: add rounding and rescaling --- src/lib.rs | 1 + src/util/mathematics/mod.rs | 5 ++++ src/util/mathematics/rescale.rs | 34 ++++++++++++++++++++++++++ src/util/mathematics/rounding.rs | 41 ++++++++++++++++++++++++++++++++ src/util/mod.rs | 1 + src/util/rounding.rs | 0 src/util/timebase.rs | 34 ++++++++++++++++++++++++++ 7 files changed, 116 insertions(+) create mode 100644 src/util/mathematics/mod.rs create mode 100644 src/util/mathematics/rescale.rs create mode 100644 src/util/mathematics/rounding.rs create mode 100644 src/util/rounding.rs create mode 100644 src/util/timebase.rs diff --git a/src/lib.rs b/src/lib.rs index f2ab5d7..2233e8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,7 @@ pub use util::time; pub use util::frame::{self, Frame}; pub use util::channel_layout::{self, ChannelLayout}; pub use util::option; +pub use util::mathematics::{self, Rounding, Rescale, rescale}; #[cfg(feature = "format")] pub mod format; diff --git a/src/util/mathematics/mod.rs b/src/util/mathematics/mod.rs new file mode 100644 index 0000000..dfef2e6 --- /dev/null +++ b/src/util/mathematics/mod.rs @@ -0,0 +1,5 @@ +pub mod rounding; +pub use self::rounding::Rounding; + +pub mod rescale; +pub use self::rescale::Rescale; diff --git a/src/util/mathematics/rescale.rs b/src/util/mathematics/rescale.rs new file mode 100644 index 0000000..96dd107 --- /dev/null +++ b/src/util/mathematics/rescale.rs @@ -0,0 +1,34 @@ +use ffi::*; +use ::{Rational, Rounding}; + +pub const TIME_BASE: Rational = Rational(AV_TIME_BASE_Q.num, AV_TIME_BASE_Q.den); + +pub trait Rescale { + fn rescale(&self, source: S, destination: D) -> i64 + where S: Into, + D: Into; + + fn rescale_with(&self, source: S, destination: D, rounding: Rounding) -> i64 + where S: Into, + D: Into; +} + +impl + Clone> Rescale for T { + fn rescale(&self, source: S, destination: D) -> i64 + where S: Into, + D: Into + { + unsafe { + av_rescale_q(self.clone().into(), source.into().into(), destination.into().into()).into() + } + } + + fn rescale_with(&self, source: S, destination: D, rounding: Rounding) -> i64 + where S: Into, + D: Into + { + unsafe { + av_rescale_q_rnd(self.clone().into(), source.into().into(), destination.into().into(), rounding.into()).into() + } + } +} diff --git a/src/util/mathematics/rounding.rs b/src/util/mathematics/rounding.rs new file mode 100644 index 0000000..9e13a60 --- /dev/null +++ b/src/util/mathematics/rounding.rs @@ -0,0 +1,41 @@ +use ffi::*; + +#[derive(Eq, PartialEq, Clone, Copy, Debug)] +pub enum Rounding { + Zero, + Infinity, + Down, + Up, + NearInfinity, + PassMinMax, +} + +impl From for Rounding { + #[inline(always)] + fn from(value: AVRounding) -> Self { + match value { + AV_ROUND_ZERO => Rounding::Zero, + AV_ROUND_INF => Rounding::Infinity, + AV_ROUND_DOWN => Rounding::Down, + AV_ROUND_UP => Rounding::Up, + AV_ROUND_NEAR_INF => Rounding::NearInfinity, + AV_ROUND_PASS_MINMAX => Rounding::PassMinMax, + } + } +} + +impl Into for Rounding { + #[inline(always)] + fn into(self) -> AVRounding { + match self { + Rounding::Zero => AV_ROUND_ZERO, + Rounding::Infinity => AV_ROUND_INF, + Rounding::Down => AV_ROUND_DOWN, + Rounding::Up => AV_ROUND_UP, + Rounding::NearInfinity => AV_ROUND_NEAR_INF, + Rounding::PassMinMax => AV_ROUND_PASS_MINMAX, + } + } +} + + diff --git a/src/util/mod.rs b/src/util/mod.rs index a9d0beb..db3b92c 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -11,6 +11,7 @@ pub mod time; pub mod channel_layout; pub mod option; pub mod range; +pub mod mathematics; use std::ffi::CStr; use std::str::from_utf8_unchecked; diff --git a/src/util/rounding.rs b/src/util/rounding.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/util/timebase.rs b/src/util/timebase.rs new file mode 100644 index 0000000..a55fb8e --- /dev/null +++ b/src/util/timebase.rs @@ -0,0 +1,34 @@ +use ffi::*; +use ::{Rational, Rounding}; + +pub const DEFAULT: Rational = Rational(AV_TIME_BASE_Q.num, AV_TIME_BASE_Q.den); + +pub trait Rescaling { + fn rescale(&self, source: S, destination: D) -> i64 + where S: Into, + D: Into; + + fn rescale_with(&self, source: S, destination: D, rounding: Rounding) -> i64 + where S: Into, + D: Into; +} + +impl + From> Rescaling for T { + fn rescale(&self, source: S, destination: D) -> i64 + where S: Into, + D: Into + { + unsafe { + av_rescale_q(self.into(), source.into().into(), destination.into().into()).into() + } + } + + fn rescale_with(&self, source: S, destination: D, rounding: Rounding) -> i64 + where S: Into, + D: Into + { + unsafe { + av_rescale_q(self.into(), source.into().into(), destination.into().into()).into() + } + } +}