codec: make lifetimes saner

This commit is contained in:
meh 2015-09-04 16:28:45 +02:00
parent a2979c828d
commit fbf5cd94eb
5 changed files with 62 additions and 67 deletions

View File

@ -1,23 +1,22 @@
use std::ops::Deref; use std::ops::Deref;
use std::marker::PhantomData;
use {ChannelLayout, format}; use {ChannelLayout, format};
use super::codec::Codec; use super::codec::Codec;
use ffi::*; use ffi::*;
pub struct Audio<'a> { pub struct Audio {
codec: &'a Codec<'a>, codec: Codec,
} }
impl<'a> Audio<'a> { impl Audio {
pub unsafe fn new<'b>(codec: &'b Codec) -> Audio<'b> { pub unsafe fn new(codec: Codec) -> Audio {
Audio { Audio {
codec: codec, codec: codec,
} }
} }
} }
impl<'a> Audio<'a> { impl Audio {
pub fn rates(&self) -> Option<RateIter> { pub fn rates(&self) -> Option<RateIter> {
unsafe { unsafe {
if (*self.as_ptr()).supported_samplerates.is_null() { if (*self.as_ptr()).supported_samplerates.is_null() {
@ -52,27 +51,25 @@ impl<'a> Audio<'a> {
} }
} }
impl<'a> Deref for Audio<'a> { impl Deref for Audio {
type Target = Codec<'a>; type Target = Codec;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
self.codec &self.codec
} }
} }
pub struct RateIter<'a> { pub struct RateIter {
ptr: *const i32, ptr: *const i32,
_marker: PhantomData<&'a ()>,
} }
impl<'a> RateIter<'a> { impl RateIter {
pub fn new(ptr: *const i32) -> Self { pub fn new(ptr: *const i32) -> Self {
RateIter { ptr: ptr, _marker: PhantomData } RateIter { ptr: ptr }
} }
} }
impl<'a> Iterator for RateIter<'a> { impl Iterator for RateIter {
type Item = i32; type Item = i32;
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {
@ -89,19 +86,17 @@ impl<'a> Iterator for RateIter<'a> {
} }
} }
pub struct FormatIter<'a> { pub struct FormatIter {
ptr: *const AVSampleFormat, ptr: *const AVSampleFormat,
_marker: PhantomData<&'a ()>,
} }
impl<'a> FormatIter<'a> { impl FormatIter {
pub fn new(ptr: *const AVSampleFormat) -> Self { pub fn new(ptr: *const AVSampleFormat) -> Self {
FormatIter { ptr: ptr, _marker: PhantomData } FormatIter { ptr: ptr }
} }
} }
impl<'a> Iterator for FormatIter<'a> { impl Iterator for FormatIter {
type Item = format::Sample; type Item = format::Sample;
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {
@ -118,19 +113,17 @@ impl<'a> Iterator for FormatIter<'a> {
} }
} }
pub struct ChannelLayoutIter<'a> { pub struct ChannelLayoutIter {
ptr: *const u64, ptr: *const u64,
_marker: PhantomData<&'a ()>,
} }
impl<'a> ChannelLayoutIter<'a> { impl ChannelLayoutIter {
pub fn new(ptr: *const u64) -> Self { pub fn new(ptr: *const u64) -> Self {
ChannelLayoutIter { ptr: ptr, _marker: PhantomData } ChannelLayoutIter { ptr: ptr }
} }
} }
impl<'a> Iterator for ChannelLayoutIter<'a> { impl Iterator for ChannelLayoutIter {
type Item = ChannelLayout; type Item = ChannelLayout;
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {

View File

@ -1,4 +1,3 @@
use std::marker::PhantomData;
use std::ffi::CStr; use std::ffi::CStr;
use std::str::from_utf8_unchecked; use std::str::from_utf8_unchecked;
@ -6,15 +5,17 @@ use ffi::*;
use super::{Id, Video, Audio, Capabilities, Profile}; use super::{Id, Video, Audio, Capabilities, Profile};
use ::{Error, media}; use ::{Error, media};
pub struct Codec<'a> { #[derive(PartialEq, Eq, Copy, Clone)]
pub struct Codec {
ptr: *mut AVCodec, ptr: *mut AVCodec,
_marker: PhantomData<&'a ()>,
} }
impl<'a> Codec<'a> { unsafe impl Send for Codec { }
unsafe impl Sync for Codec { }
impl Codec {
pub unsafe fn wrap(ptr: *mut AVCodec) -> Self { pub unsafe fn wrap(ptr: *mut AVCodec) -> Self {
Codec { ptr: ptr, _marker: PhantomData } Codec { ptr: ptr }
} }
pub unsafe fn as_ptr(&self) -> *const AVCodec { pub unsafe fn as_ptr(&self) -> *const AVCodec {
@ -26,7 +27,7 @@ impl<'a> Codec<'a> {
} }
} }
impl<'a> Codec<'a> { impl Codec {
pub fn is_encoder(&self) -> bool { pub fn is_encoder(&self) -> bool {
unsafe { unsafe {
av_codec_is_encoder(self.as_ptr()) != 0 av_codec_is_encoder(self.as_ptr()) != 0
@ -63,7 +64,11 @@ impl<'a> Codec<'a> {
} }
} }
pub fn video(&self) -> Result<Video, Error> { pub fn is_video(&self) -> bool {
self.medium() == media::Type::Video
}
pub fn video(self) -> Result<Video, Error> {
unsafe { unsafe {
if self.medium() == media::Type::Video { if self.medium() == media::Type::Video {
Ok(Video::new(self)) Ok(Video::new(self))
@ -74,7 +79,11 @@ impl<'a> Codec<'a> {
} }
} }
pub fn audio(&self) -> Result<Audio, Error> { pub fn is_audio(&self) -> bool {
self.medium() == media::Type::Audio
}
pub fn audio(self) -> Result<Audio, Error> {
unsafe { unsafe {
if self.medium() == media::Type::Audio { if self.medium() == media::Type::Audio {
Ok(Audio::new(self)) Ok(Audio::new(self))
@ -109,20 +118,18 @@ impl<'a> Codec<'a> {
} }
} }
pub struct ProfileIter<'a> { pub struct ProfileIter {
id: Id, id: Id,
ptr: *const AVProfile, ptr: *const AVProfile,
_marker: PhantomData<&'a ()>,
} }
impl<'a> ProfileIter<'a> { impl ProfileIter {
pub fn new(id: Id, ptr: *const AVProfile) -> Self { pub fn new(id: Id, ptr: *const AVProfile) -> Self {
ProfileIter { id: id, ptr: ptr, _marker: PhantomData } ProfileIter { id: id, ptr: ptr }
} }
} }
impl<'a> Iterator for ProfileIter<'a> { impl Iterator for ProfileIter {
type Item = Profile; type Item = Profile;
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {

View File

@ -138,7 +138,7 @@ impl DerefMut for Decoder {
} }
} }
pub fn find(id: Id) -> Option<Codec<'static>> { pub fn find(id: Id) -> Option<Codec> {
unsafe { unsafe {
let ptr = avcodec_find_decoder(id.into()); let ptr = avcodec_find_decoder(id.into());
@ -151,7 +151,7 @@ pub fn find(id: Id) -> Option<Codec<'static>> {
} }
} }
pub fn find_by_name(name: &str) -> Option<Codec<'static>> { pub fn find_by_name(name: &str) -> Option<Codec> {
unsafe { unsafe {
let ptr = avcodec_find_decoder_by_name(CString::new(name).unwrap().as_ptr()); let ptr = avcodec_find_decoder_by_name(CString::new(name).unwrap().as_ptr());

View File

@ -114,7 +114,7 @@ impl DerefMut for Encoder {
} }
} }
pub fn find(id: Id) -> Option<Codec<'static>> { pub fn find(id: Id) -> Option<Codec> {
unsafe { unsafe {
let ptr = avcodec_find_encoder(id.into()); let ptr = avcodec_find_encoder(id.into());
@ -127,7 +127,7 @@ pub fn find(id: Id) -> Option<Codec<'static>> {
} }
} }
pub fn find_by_name(name: &str) -> Option<Codec<'static>> { pub fn find_by_name(name: &str) -> Option<Codec> {
unsafe { unsafe {
let ptr = avcodec_find_encoder_by_name(CString::new(name).unwrap().as_ptr()); let ptr = avcodec_find_encoder_by_name(CString::new(name).unwrap().as_ptr());

View File

@ -1,23 +1,22 @@
use std::marker::PhantomData;
use std::ops::Deref; use std::ops::Deref;
use {Rational, format}; use {Rational, format};
use super::codec::Codec; use super::codec::Codec;
use ffi::*; use ffi::*;
pub struct Video<'a> { pub struct Video {
codec: &'a Codec<'a>, codec: Codec,
} }
impl<'a> Video<'a> { impl Video {
pub unsafe fn new<'b>(codec: &'b Codec) -> Video<'b> { pub unsafe fn new(codec: Codec) -> Video {
Video { Video {
codec: codec, codec: codec,
} }
} }
} }
impl<'a> Video<'a> { impl Video {
pub fn rates(&self) -> Option<RateIter> { pub fn rates(&self) -> Option<RateIter> {
unsafe { unsafe {
if (*self.codec.as_ptr()).supported_framerates.is_null() { if (*self.codec.as_ptr()).supported_framerates.is_null() {
@ -41,27 +40,25 @@ impl<'a> Video<'a> {
} }
} }
impl<'a> Deref for Video<'a> { impl Deref for Video {
type Target = Codec<'a>; type Target = Codec;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
self.codec &self.codec
} }
} }
pub struct RateIter<'a> { pub struct RateIter {
ptr: *const AVRational, ptr: *const AVRational,
_marker: PhantomData<&'a ()>,
} }
impl<'a> RateIter<'a> { impl RateIter {
pub fn new(ptr: *const AVRational) -> Self { pub fn new(ptr: *const AVRational) -> Self {
RateIter { ptr: ptr, _marker: PhantomData } RateIter { ptr: ptr }
} }
} }
impl<'a> Iterator for RateIter<'a> { impl Iterator for RateIter {
type Item = Rational; type Item = Rational;
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {
@ -78,19 +75,17 @@ impl<'a> Iterator for RateIter<'a> {
} }
} }
pub struct FormatIter<'a> { pub struct FormatIter {
ptr: *const AVPixelFormat, ptr: *const AVPixelFormat,
_marker: PhantomData<&'a ()>,
} }
impl<'a> FormatIter<'a> { impl FormatIter {
pub fn new(ptr: *const AVPixelFormat) -> Self { pub fn new(ptr: *const AVPixelFormat) -> Self {
FormatIter { ptr: ptr, _marker: PhantomData } FormatIter { ptr: ptr }
} }
} }
impl<'a> Iterator for FormatIter<'a> { impl Iterator for FormatIter {
type Item = format::Pixel; type Item = format::Pixel;
fn next(&mut self) -> Option<<Self as Iterator>::Item> { fn next(&mut self) -> Option<<Self as Iterator>::Item> {