codec: use Option<_> in the getter API

This commit is contained in:
meh 2015-08-29 16:15:57 +02:00
parent b39d8df4ee
commit 8ed092c0c3
4 changed files with 147 additions and 69 deletions

View File

@ -17,16 +17,47 @@ fn main() {
if let Some(profiles) = codec.profiles() { if let Some(profiles) = codec.profiles() {
println!("\t profiles: {:?}", profiles.collect::<Vec<_>>()); println!("\t profiles: {:?}", profiles.collect::<Vec<_>>());
} }
else {
println!("\t profiles: none");
}
if let Ok(video) = codec.video() { if let Ok(video) = codec.video() {
println!("\t rates: {:?}", video.rates().collect::<Vec<_>>()); if let Some(rates) = video.rates() {
println!("\t formats: {:?}", video.formats().collect::<Vec<_>>()); println!("\t rates: {:?}", rates.collect::<Vec<_>>());
}
else {
println!("\t rates: any");
}
if let Some(formats) = video.formats() {
println!("\t formats: {:?}", formats.collect::<Vec<_>>());
}
else {
println!("\t formats: any");
}
} }
if let Ok(audio) = codec.audio() { if let Ok(audio) = codec.audio() {
println!("\t rates: {:?}", audio.rates().collect::<Vec<_>>()); if let Some(rates) = audio.rates() {
println!("\t formats: {:?}", audio.formats().collect::<Vec<_>>()); println!("\t rates: {:?}", rates.collect::<Vec<_>>());
println!("\t channel_layouts: {:?}", audio.channel_layouts().collect::<Vec<_>>()); }
else {
println!("\t rates: any");
}
if let Some(formats) = audio.formats() {
println!("\t formats: {:?}", formats.collect::<Vec<_>>());
}
else {
println!("\t formats: any");
}
if let Some(layouts) = audio.channel_layouts() {
println!("\t channel_layouts: {:?}", layouts.collect::<Vec<_>>());
}
else {
println!("\t channel_layouts: any");
}
} }
println!("\t max_lowres: {:?}", codec.max_lowres()); println!("\t max_lowres: {:?}", codec.max_lowres());
@ -46,14 +77,42 @@ fn main() {
} }
if let Ok(video) = codec.video() { if let Ok(video) = codec.video() {
println!("\t rates: {:?}", video.rates().collect::<Vec<_>>()); if let Some(rates) = video.rates() {
println!("\t formats: {:?}", video.formats().collect::<Vec<_>>()); println!("\t rates: {:?}", rates.collect::<Vec<_>>());
}
else {
println!("\t rates: any");
}
if let Some(formats) = video.formats() {
println!("\t formats: {:?}", formats.collect::<Vec<_>>());
}
else {
println!("\t formats: any");
}
} }
if let Ok(audio) = codec.audio() { if let Ok(audio) = codec.audio() {
println!("\t rates: {:?}", audio.rates().collect::<Vec<_>>()); if let Some(rates) = audio.rates() {
println!("\t formats: {:?}", audio.formats().collect::<Vec<_>>()); println!("\t rates: {:?}", rates.collect::<Vec<_>>());
println!("\t channel_layouts: {:?}", audio.channel_layouts().collect::<Vec<_>>()); }
else {
println!("\t rates: any");
}
if let Some(formats) = audio.formats() {
println!("\t formats: {:?}", formats.collect::<Vec<_>>());
}
else {
println!("\t formats: any");
}
if let Some(layouts) = audio.channel_layouts() {
println!("\t channel_layouts: {:?}", layouts.collect::<Vec<_>>());
}
else {
println!("\t channel_layouts: any");
}
} }
println!("\t max_lowres: {:?}", codec.max_lowres()); println!("\t max_lowres: {:?}", codec.max_lowres());

View File

@ -18,21 +18,36 @@ impl<'a> Audio<'a> {
} }
impl<'a> Audio<'a> { impl<'a> Audio<'a> {
pub fn rates(&self) -> RateIter { pub fn rates(&self) -> Option<RateIter> {
unsafe { unsafe {
RateIter::new((*self.codec.as_ptr()).supported_samplerates) if (*self.as_ptr()).supported_samplerates.is_null() {
None
}
else {
Some(RateIter::new((*self.codec.as_ptr()).supported_samplerates))
}
} }
} }
pub fn formats(&self) -> FormatIter { pub fn formats(&self) -> Option<FormatIter> {
unsafe { unsafe {
FormatIter::new((*self.codec.as_ptr()).sample_fmts) if (*self.codec.as_ptr()).sample_fmts.is_null() {
None
}
else {
Some(FormatIter::new((*self.codec.as_ptr()).sample_fmts))
}
} }
} }
pub fn channel_layouts(&self) -> ChannelLayoutIter { pub fn channel_layouts(&self) -> Option<ChannelLayoutIter> {
unsafe { unsafe {
ChannelLayoutIter::new((*self.codec.as_ptr()).channel_layouts) if (*self.codec.as_ptr()).channel_layouts.is_null() {
None
}
else {
Some(ChannelLayoutIter::new((*self.codec.as_ptr()).channel_layouts))
}
} }
} }
} }
@ -62,15 +77,14 @@ impl<'a> Iterator for RateIter<'a> {
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe { unsafe {
if !self.ptr.is_null() && (*self.ptr) != 0 { if *self.ptr == 0 {
let rate = *self.ptr; return None;
self.ptr = self.ptr.offset(1); }
Some(rate) let rate = *self.ptr;
} self.ptr = self.ptr.offset(1);
else {
None Some(rate)
}
} }
} }
} }
@ -92,15 +106,14 @@ impl<'a> Iterator for FormatIter<'a> {
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe { unsafe {
if !self.ptr.is_null() && (*self.ptr) != AVSampleFormat::AV_SAMPLE_FMT_NONE { if *self.ptr == AVSampleFormat::AV_SAMPLE_FMT_NONE {
let format = (*self.ptr).into(); return None;
self.ptr = self.ptr.offset(1); }
Some(format) let format = (*self.ptr).into();
} self.ptr = self.ptr.offset(1);
else {
None Some(format)
}
} }
} }
} }
@ -122,15 +135,14 @@ impl<'a> Iterator for ChannelLayoutIter<'a> {
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe { unsafe {
if !self.ptr.is_null() && (*self.ptr) != 0 { if *self.ptr == 0 {
let chl = ChannelLayout::from_bits_truncate(*self.ptr); return None;
self.ptr = self.ptr.offset(1); }
Some(chl) let layout = ChannelLayout::from_bits_truncate(*self.ptr);
} self.ptr = self.ptr.offset(1);
else {
None Some(layout)
}
} }
} }
} }

View File

@ -105,7 +105,7 @@ impl<'a> Codec<'a> {
pub fn profiles(&self) -> Option<ProfileIter> { pub fn profiles(&self) -> Option<ProfileIter> {
unsafe { unsafe {
if (*self.as_ptr()).profiles.is_null() { if (*self.as_ptr()).profiles.is_null() {
return None; None
} }
else { else {
Some(ProfileIter::new(self.id(), (*self.as_ptr()).profiles)) Some(ProfileIter::new(self.id(), (*self.as_ptr()).profiles))
@ -132,15 +132,14 @@ impl<'a> Iterator for ProfileIter<'a> {
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe { unsafe {
if (*self.ptr).profile != FF_PROFILE_UNKNOWN && !(*self.ptr).name.is_null() { if (*self.ptr).profile == FF_PROFILE_UNKNOWN {
let profile = Profile::from((self.id, (*self.ptr).profile)); return None;
self.ptr = self.ptr.offset(1); }
Some(profile) let profile = Profile::from((self.id, (*self.ptr).profile));
} self.ptr = self.ptr.offset(1);
else {
None Some(profile)
}
} }
} }
} }

View File

@ -18,15 +18,25 @@ impl<'a> Video<'a> {
} }
impl<'a> Video<'a> { impl<'a> Video<'a> {
pub fn rates(&self) -> RateIter { pub fn rates(&self) -> Option<RateIter> {
unsafe { unsafe {
RateIter::new((*self.codec.as_ptr()).supported_framerates) if (*self.codec.as_ptr()).supported_framerates.is_null() {
None
}
else {
Some(RateIter::new((*self.codec.as_ptr()).supported_framerates))
}
} }
} }
pub fn formats(&self) -> FormatIter { pub fn formats(&self) -> Option<FormatIter> {
unsafe { unsafe {
FormatIter::new((*self.codec.as_ptr()).pix_fmts) if (*self.codec.as_ptr()).pix_fmts.is_null() {
None
}
else {
Some(FormatIter::new((*self.codec.as_ptr()).pix_fmts))
}
} }
} }
} }
@ -56,15 +66,14 @@ impl<'a> Iterator for RateIter<'a> {
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe { unsafe {
if !self.ptr.is_null() && (*self.ptr) != (AVRational { num: 0, den: 0 }) { if (*self.ptr).num == 0 && (*self.ptr).den == 0 {
let rate = (*self.ptr).into(); return None;
self.ptr = self.ptr.offset(1); }
Some(rate) let rate = (*self.ptr).into();
} self.ptr = self.ptr.offset(1);
else {
None Some(rate)
}
} }
} }
} }
@ -86,15 +95,14 @@ impl<'a> Iterator for FormatIter<'a> {
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe { unsafe {
if !self.ptr.is_null() && (*self.ptr) != AVPixelFormat::AV_PIX_FMT_NONE { if *self.ptr == AVPixelFormat::AV_PIX_FMT_NONE {
let format = (*self.ptr).into(); return None;
self.ptr = self.ptr.offset(1); }
Some(format) let format = (*self.ptr).into();
} self.ptr = self.ptr.offset(1);
else {
None Some(format)
}
} }
} }
} }