util/mathematics: add rounding and rescaling

This commit is contained in:
lummax 2015-10-25 13:31:51 +01:00 committed by meh
parent 2cf4da707a
commit 03740c31c0
7 changed files with 116 additions and 0 deletions

View File

@ -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;

View File

@ -0,0 +1,5 @@
pub mod rounding;
pub use self::rounding::Rounding;
pub mod rescale;
pub use self::rescale::Rescale;

View File

@ -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<S, D>(&self, source: S, destination: D) -> i64
where S: Into<Rational>,
D: Into<Rational>;
fn rescale_with<S, D>(&self, source: S, destination: D, rounding: Rounding) -> i64
where S: Into<Rational>,
D: Into<Rational>;
}
impl<T: Into<i64> + Clone> Rescale for T {
fn rescale<S, D>(&self, source: S, destination: D) -> i64
where S: Into<Rational>,
D: Into<Rational>
{
unsafe {
av_rescale_q(self.clone().into(), source.into().into(), destination.into().into()).into()
}
}
fn rescale_with<S, D>(&self, source: S, destination: D, rounding: Rounding) -> i64
where S: Into<Rational>,
D: Into<Rational>
{
unsafe {
av_rescale_q_rnd(self.clone().into(), source.into().into(), destination.into().into(), rounding.into()).into()
}
}
}

View File

@ -0,0 +1,41 @@
use ffi::*;
#[derive(Eq, PartialEq, Clone, Copy, Debug)]
pub enum Rounding {
Zero,
Infinity,
Down,
Up,
NearInfinity,
PassMinMax,
}
impl From<AVRounding> 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<AVRounding> 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,
}
}
}

View File

@ -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;

0
src/util/rounding.rs Normal file
View File

34
src/util/timebase.rs Normal file
View File

@ -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<S, D>(&self, source: S, destination: D) -> i64
where S: Into<Rational>,
D: Into<Rational>;
fn rescale_with<S, D>(&self, source: S, destination: D, rounding: Rounding) -> i64
where S: Into<Rational>,
D: Into<Rational>;
}
impl<T: Into<i64> + From<i64>> Rescaling for T {
fn rescale<S, D>(&self, source: S, destination: D) -> i64
where S: Into<Rational>,
D: Into<Rational>
{
unsafe {
av_rescale_q(self.into(), source.into().into(), destination.into().into()).into()
}
}
fn rescale_with<S, D>(&self, source: S, destination: D, rounding: Rounding) -> i64
where S: Into<Rational>,
D: Into<Rational>
{
unsafe {
av_rescale_q(self.into(), source.into().into(), destination.into().into()).into()
}
}
}