From bb4263b7be255a77659c8a3e0dc9ffcfc500a425 Mon Sep 17 00:00:00 2001 From: meh Date: Thu, 25 Jun 2015 22:23:33 +0200 Subject: [PATCH] util/rational: some more refactoring --- src/codec/decoder/mod.rs | 4 ++-- src/codec/decoder/video.rs | 2 +- src/codec/encoder/mod.rs | 4 ++-- src/codec/encoder/video.rs | 2 +- src/format/stream/mod.rs | 6 +++--- src/util/frame/video.rs | 2 +- src/util/rational.rs | 44 ++++++++++++++++++++++++-------------- 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/codec/decoder/mod.rs b/src/codec/decoder/mod.rs index 4916033..a3ed368 100644 --- a/src/codec/decoder/mod.rs +++ b/src/codec/decoder/mod.rs @@ -105,14 +105,14 @@ impl Decoder { None } else { - Some(Rational(value)) + Some(Rational::from(value)) } } } pub fn time_base(&self) -> Rational { unsafe { - Rational((*self.as_ptr()).time_base) + Rational::from((*self.as_ptr()).time_base) } } } diff --git a/src/codec/decoder/video.rs b/src/codec/decoder/video.rs index cc1af62..d2b3868 100644 --- a/src/codec/decoder/video.rs +++ b/src/codec/decoder/video.rs @@ -56,7 +56,7 @@ impl Video { pub fn aspect_ratio(&self) -> Rational { unsafe { - Rational((*self.as_ptr()).sample_aspect_ratio) + Rational::from((*self.as_ptr()).sample_aspect_ratio) } } diff --git a/src/codec/encoder/mod.rs b/src/codec/encoder/mod.rs index c30e6dc..91d4632 100644 --- a/src/codec/encoder/mod.rs +++ b/src/codec/encoder/mod.rs @@ -91,7 +91,7 @@ impl Encoder { pub fn set_frame_rate(&mut self, value: Option) { unsafe { if let Some(value) = value { - (*self.as_mut_ptr()).framerate = value.0; + (*self.as_mut_ptr()).framerate = value.into(); } else { (*self.as_mut_ptr()).framerate.num = 0; @@ -102,7 +102,7 @@ impl Encoder { pub fn set_time_base(&mut self, value: Rational) { unsafe { - (*self.as_mut_ptr()).time_base = value.0; + (*self.as_mut_ptr()).time_base = value.into(); } } } diff --git a/src/codec/encoder/video.rs b/src/codec/encoder/video.rs index 8e41a96..4d3c304 100644 --- a/src/codec/encoder/video.rs +++ b/src/codec/encoder/video.rs @@ -120,7 +120,7 @@ impl Video { pub fn set_aspect_ratio(&mut self, value: Rational) { unsafe { - (*self.as_mut_ptr()).sample_aspect_ratio = value.0; + (*self.as_mut_ptr()).sample_aspect_ratio = value.into(); } } diff --git a/src/format/stream/mod.rs b/src/format/stream/mod.rs index f7c757a..3d71994 100644 --- a/src/format/stream/mod.rs +++ b/src/format/stream/mod.rs @@ -45,7 +45,7 @@ impl<'a> Stream<'a> { pub fn time_base(&self) -> Rational { 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 { 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) { 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()); } } } diff --git a/src/util/frame/video.rs b/src/util/frame/video.rs index 40bef11..871e48d 100644 --- a/src/util/frame/video.rs +++ b/src/util/frame/video.rs @@ -165,7 +165,7 @@ impl Video { pub fn aspect_ratio(&self) -> Rational { unsafe { - Rational((*self.as_ptr()).sample_aspect_ratio) + Rational::from((*self.as_ptr()).sample_aspect_ratio) } } diff --git a/src/util/rational.rs b/src/util/rational.rs index db11f99..838e3ed 100644 --- a/src/util/rational.rs +++ b/src/util/rational.rs @@ -6,19 +6,19 @@ use libc::{c_int, int64_t}; use ffi::*; #[derive(Eq, PartialEq, Copy, Clone)] -pub struct Rational(pub AVRational); +pub struct Rational(pub i32, pub i32); impl Rational { 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 { - self.0.num as i32 + self.0 } pub fn denominator(&self) -> i32 { - self.0.den as i32 + self.1 } pub fn reduce(&self) -> Rational { @@ -38,21 +38,33 @@ impl Rational { max as int64_t); if exact == 1 { - Ok(Rational::new(dst_num, dst_den)) + Ok(Rational(dst_num, dst_den)) } else { - Err(Rational::new(dst_num, dst_den)) + Err(Rational(dst_num, dst_den)) } } } pub fn invert(&self) -> Rational { unsafe { - Rational(av_inv_q(self.0)) + Rational::from(av_inv_q((*self).into())) } } } +impl From for Rational { + fn from(value: AVRational) -> Rational { + Rational(value.num, value.den) + } +} + +impl Into for Rational { + fn into(self) -> AVRational { + AVRational { num: self.0, den: self.1 } + } +} + impl fmt::Display for Rational { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { f.write_str(&format!("{}/{}", self.numerator(), self.denominator())) @@ -68,7 +80,7 @@ impl fmt::Debug for Rational { impl From for Rational { fn from(value: f64) -> Rational { unsafe { - Rational(av_d2q(value, c_int::max_value())) + Rational::from(av_d2q(value, c_int::max_value())) } } } @@ -76,7 +88,7 @@ impl From for Rational { impl From for f64 { fn from(value: Rational) -> f64 { unsafe { - av_q2d(value.0) + av_q2d(value.into()) } } } @@ -84,7 +96,7 @@ impl From for f64 { impl From for u32 { fn from(value: Rational) -> u32 { unsafe { - av_q2intfloat(value.0) + av_q2intfloat(value.into()) } } } @@ -92,7 +104,7 @@ impl From for u32 { impl PartialOrd for Rational { fn partial_cmp(&self, other: &Self) -> Option { unsafe { - match av_cmp_q(self.0, other.0) { + match av_cmp_q((*self).into(), (*other).into()) { 0 => Some(Ordering::Equal), 1 => Some(Ordering::Greater), -1 => Some(Ordering::Less), @@ -108,7 +120,7 @@ impl Add for Rational { fn add(self, other: Rational) -> Rational { 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 { 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 { 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 { 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 { 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::Less, _ => Ordering::Equal,