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::marker::PhantomData;
use {ChannelLayout, format};
use super::codec::Codec;
use ffi::*;
pub struct Audio<'a> {
codec: &'a Codec<'a>,
pub struct Audio {
codec: Codec,
}
impl<'a> Audio<'a> {
pub unsafe fn new<'b>(codec: &'b Codec) -> Audio<'b> {
impl Audio {
pub unsafe fn new(codec: Codec) -> Audio {
Audio {
codec: codec,
}
}
}
impl<'a> Audio<'a> {
impl Audio {
pub fn rates(&self) -> Option<RateIter> {
unsafe {
if (*self.as_ptr()).supported_samplerates.is_null() {
@ -52,27 +51,25 @@ impl<'a> Audio<'a> {
}
}
impl<'a> Deref for Audio<'a> {
type Target = Codec<'a>;
impl Deref for Audio {
type Target = Codec;
fn deref(&self) -> &Self::Target {
self.codec
&self.codec
}
}
pub struct RateIter<'a> {
pub struct RateIter {
ptr: *const i32,
_marker: PhantomData<&'a ()>,
}
impl<'a> RateIter<'a> {
impl RateIter {
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;
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,
_marker: PhantomData<&'a ()>,
}
impl<'a> FormatIter<'a> {
impl FormatIter {
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;
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,
_marker: PhantomData<&'a ()>,
}
impl<'a> ChannelLayoutIter<'a> {
impl ChannelLayoutIter {
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;
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::str::from_utf8_unchecked;
@ -6,15 +5,17 @@ use ffi::*;
use super::{Id, Video, Audio, Capabilities, Profile};
use ::{Error, media};
pub struct Codec<'a> {
#[derive(PartialEq, Eq, Copy, Clone)]
pub struct Codec {
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 {
Codec { ptr: ptr, _marker: PhantomData }
Codec { ptr: ptr }
}
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 {
unsafe {
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 {
if self.medium() == media::Type::Video {
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 {
if self.medium() == media::Type::Audio {
Ok(Audio::new(self))
@ -109,20 +118,18 @@ impl<'a> Codec<'a> {
}
}
pub struct ProfileIter<'a> {
pub struct ProfileIter {
id: Id,
ptr: *const AVProfile,
_marker: PhantomData<&'a ()>,
}
impl<'a> ProfileIter<'a> {
impl ProfileIter {
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;
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 {
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 {
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 {
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 {
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 {Rational, format};
use super::codec::Codec;
use ffi::*;
pub struct Video<'a> {
codec: &'a Codec<'a>,
pub struct Video {
codec: Codec,
}
impl<'a> Video<'a> {
pub unsafe fn new<'b>(codec: &'b Codec) -> Video<'b> {
impl Video {
pub unsafe fn new(codec: Codec) -> Video {
Video {
codec: codec,
}
}
}
impl<'a> Video<'a> {
impl Video {
pub fn rates(&self) -> Option<RateIter> {
unsafe {
if (*self.codec.as_ptr()).supported_framerates.is_null() {
@ -41,27 +40,25 @@ impl<'a> Video<'a> {
}
}
impl<'a> Deref for Video<'a> {
type Target = Codec<'a>;
impl Deref for Video {
type Target = Codec;
fn deref(&self) -> &Self::Target {
self.codec
&self.codec
}
}
pub struct RateIter<'a> {
pub struct RateIter {
ptr: *const AVRational,
_marker: PhantomData<&'a ()>,
}
impl<'a> RateIter<'a> {
impl RateIter {
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;
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,
_marker: PhantomData<&'a ()>,
}
impl<'a> FormatIter<'a> {
impl FormatIter {
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;
fn next(&mut self) -> Option<<Self as Iterator>::Item> {