util/rational: some more refactoring

This commit is contained in:
meh 2015-06-25 22:23:33 +02:00
parent 23b414b01e
commit bb4263b7be
7 changed files with 38 additions and 26 deletions

View File

@ -105,14 +105,14 @@ impl Decoder {
None None
} }
else { else {
Some(Rational(value)) Some(Rational::from(value))
} }
} }
} }
pub fn time_base(&self) -> Rational { pub fn time_base(&self) -> Rational {
unsafe { unsafe {
Rational((*self.as_ptr()).time_base) Rational::from((*self.as_ptr()).time_base)
} }
} }
} }

View File

@ -56,7 +56,7 @@ impl Video {
pub fn aspect_ratio(&self) -> Rational { pub fn aspect_ratio(&self) -> Rational {
unsafe { unsafe {
Rational((*self.as_ptr()).sample_aspect_ratio) Rational::from((*self.as_ptr()).sample_aspect_ratio)
} }
} }

View File

@ -91,7 +91,7 @@ impl Encoder {
pub fn set_frame_rate(&mut self, value: Option<Rational>) { pub fn set_frame_rate(&mut self, value: Option<Rational>) {
unsafe { unsafe {
if let Some(value) = value { if let Some(value) = value {
(*self.as_mut_ptr()).framerate = value.0; (*self.as_mut_ptr()).framerate = value.into();
} }
else { else {
(*self.as_mut_ptr()).framerate.num = 0; (*self.as_mut_ptr()).framerate.num = 0;
@ -102,7 +102,7 @@ impl Encoder {
pub fn set_time_base(&mut self, value: Rational) { pub fn set_time_base(&mut self, value: Rational) {
unsafe { unsafe {
(*self.as_mut_ptr()).time_base = value.0; (*self.as_mut_ptr()).time_base = value.into();
} }
} }
} }

View File

@ -120,7 +120,7 @@ impl Video {
pub fn set_aspect_ratio(&mut self, value: Rational) { pub fn set_aspect_ratio(&mut self, value: Rational) {
unsafe { unsafe {
(*self.as_mut_ptr()).sample_aspect_ratio = value.0; (*self.as_mut_ptr()).sample_aspect_ratio = value.into();
} }
} }

View File

@ -45,7 +45,7 @@ impl<'a> Stream<'a> {
pub fn time_base(&self) -> Rational { pub fn time_base(&self) -> Rational {
unsafe { unsafe {
Rational((*self.as_ptr()).time_base) Rational::from((*self.as_ptr()).time_base)
} }
} }
@ -87,13 +87,13 @@ impl<'a> Stream<'a> {
pub fn frame_rate(&self) -> Rational { pub fn frame_rate(&self) -> Rational {
unsafe { unsafe {
Rational(av_stream_get_r_frame_rate(self.as_ptr())) Rational::from(av_stream_get_r_frame_rate(self.as_ptr()))
} }
} }
pub fn set_frame_rate(&mut self, value: Rational) { pub fn set_frame_rate(&mut self, value: Rational) {
unsafe { unsafe {
av_stream_set_r_frame_rate(self.as_mut_ptr(), value.0); av_stream_set_r_frame_rate(self.as_mut_ptr(), value.into());
} }
} }
} }

View File

@ -165,7 +165,7 @@ impl Video {
pub fn aspect_ratio(&self) -> Rational { pub fn aspect_ratio(&self) -> Rational {
unsafe { unsafe {
Rational((*self.as_ptr()).sample_aspect_ratio) Rational::from((*self.as_ptr()).sample_aspect_ratio)
} }
} }

View File

@ -6,19 +6,19 @@ use libc::{c_int, int64_t};
use ffi::*; use ffi::*;
#[derive(Eq, PartialEq, Copy, Clone)] #[derive(Eq, PartialEq, Copy, Clone)]
pub struct Rational(pub AVRational); pub struct Rational(pub i32, pub i32);
impl Rational { impl Rational {
pub fn new(numerator: i32, denominator: i32) -> Self { pub fn new(numerator: i32, denominator: i32) -> Self {
Rational(AVRational { num: numerator as c_int, den: denominator as c_int }) Rational(numerator, denominator)
} }
pub fn numerator(&self) -> i32 { pub fn numerator(&self) -> i32 {
self.0.num as i32 self.0
} }
pub fn denominator(&self) -> i32 { pub fn denominator(&self) -> i32 {
self.0.den as i32 self.1
} }
pub fn reduce(&self) -> Rational { pub fn reduce(&self) -> Rational {
@ -38,21 +38,33 @@ impl Rational {
max as int64_t); max as int64_t);
if exact == 1 { if exact == 1 {
Ok(Rational::new(dst_num, dst_den)) Ok(Rational(dst_num, dst_den))
} }
else { else {
Err(Rational::new(dst_num, dst_den)) Err(Rational(dst_num, dst_den))
} }
} }
} }
pub fn invert(&self) -> Rational { pub fn invert(&self) -> Rational {
unsafe { unsafe {
Rational(av_inv_q(self.0)) Rational::from(av_inv_q((*self).into()))
} }
} }
} }
impl From<AVRational> for Rational {
fn from(value: AVRational) -> Rational {
Rational(value.num, value.den)
}
}
impl Into<AVRational> for Rational {
fn into(self) -> AVRational {
AVRational { num: self.0, den: self.1 }
}
}
impl fmt::Display for Rational { impl fmt::Display for Rational {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str(&format!("{}/{}", self.numerator(), self.denominator())) f.write_str(&format!("{}/{}", self.numerator(), self.denominator()))
@ -68,7 +80,7 @@ impl fmt::Debug for Rational {
impl From<f64> for Rational { impl From<f64> for Rational {
fn from(value: f64) -> Rational { fn from(value: f64) -> Rational {
unsafe { unsafe {
Rational(av_d2q(value, c_int::max_value())) Rational::from(av_d2q(value, c_int::max_value()))
} }
} }
} }
@ -76,7 +88,7 @@ impl From<f64> for Rational {
impl From<Rational> for f64 { impl From<Rational> for f64 {
fn from(value: Rational) -> f64 { fn from(value: Rational) -> f64 {
unsafe { unsafe {
av_q2d(value.0) av_q2d(value.into())
} }
} }
} }
@ -84,7 +96,7 @@ impl From<Rational> for f64 {
impl From<Rational> for u32 { impl From<Rational> for u32 {
fn from(value: Rational) -> u32 { fn from(value: Rational) -> u32 {
unsafe { unsafe {
av_q2intfloat(value.0) av_q2intfloat(value.into())
} }
} }
} }
@ -92,7 +104,7 @@ impl From<Rational> for u32 {
impl PartialOrd for Rational { impl PartialOrd for Rational {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
unsafe { unsafe {
match av_cmp_q(self.0, other.0) { match av_cmp_q((*self).into(), (*other).into()) {
0 => Some(Ordering::Equal), 0 => Some(Ordering::Equal),
1 => Some(Ordering::Greater), 1 => Some(Ordering::Greater),
-1 => Some(Ordering::Less), -1 => Some(Ordering::Less),
@ -108,7 +120,7 @@ impl Add for Rational {
fn add(self, other: Rational) -> Rational { fn add(self, other: Rational) -> Rational {
unsafe { unsafe {
Rational(av_add_q(self.0, other.0)) Rational::from(av_add_q(self.into(), other.into()))
} }
} }
} }
@ -118,7 +130,7 @@ impl Sub for Rational {
fn sub(self, other: Rational) -> Rational { fn sub(self, other: Rational) -> Rational {
unsafe { unsafe {
Rational(av_sub_q(self.0, other.0)) Rational::from(av_sub_q(self.into(), other.into()))
} }
} }
} }
@ -128,7 +140,7 @@ impl Mul for Rational {
fn mul(self, other: Rational) -> Rational { fn mul(self, other: Rational) -> Rational {
unsafe { unsafe {
Rational(av_mul_q(self.0, other.0)) Rational::from(av_mul_q(self.into(), other.into()))
} }
} }
} }
@ -138,14 +150,14 @@ impl Div for Rational {
fn div(self, other: Rational) -> Rational { fn div(self, other: Rational) -> Rational {
unsafe { unsafe {
Rational(av_div_q(self.0, other.0)) Rational::from(av_div_q(self.into(), other.into()))
} }
} }
} }
pub fn nearer(q: Rational, q1: Rational, q2: Rational) -> Ordering { pub fn nearer(q: Rational, q1: Rational, q2: Rational) -> Ordering {
unsafe { unsafe {
match av_nearer_q(q.0, q1.0, q2.0) { match av_nearer_q(q.into(), q1.into(), q2.into()) {
1 => Ordering::Greater, 1 => Ordering::Greater,
-1 => Ordering::Less, -1 => Ordering::Less,
_ => Ordering::Equal, _ => Ordering::Equal,