util/mathematics: add rounding and rescaling
This commit is contained in:
parent
2cf4da707a
commit
03740c31c0
@ -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;
|
||||
|
5
src/util/mathematics/mod.rs
Normal file
5
src/util/mathematics/mod.rs
Normal file
@ -0,0 +1,5 @@
|
||||
pub mod rounding;
|
||||
pub use self::rounding::Rounding;
|
||||
|
||||
pub mod rescale;
|
||||
pub use self::rescale::Rescale;
|
34
src/util/mathematics/rescale.rs
Normal file
34
src/util/mathematics/rescale.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
}
|
41
src/util/mathematics/rounding.rs
Normal file
41
src/util/mathematics/rounding.rs
Normal 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
0
src/util/rounding.rs
Normal file
34
src/util/timebase.rs
Normal file
34
src/util/timebase.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user