diff --git a/src/codec/decoder/mod.rs b/src/codec/decoder/mod.rs index cff353b..4fc4f33 100644 --- a/src/codec/decoder/mod.rs +++ b/src/codec/decoder/mod.rs @@ -29,7 +29,16 @@ use ::{Codec, Error, Discard, Dictionary}; pub struct Decoder(pub Context); impl Decoder { - pub fn open(mut self, codec: &Codec) -> Result { + pub fn open(mut self) -> Result { + unsafe { + match avcodec_open2(self.as_mut_ptr(), ptr::null(), ptr::null_mut()) { + 0 => Ok(Opened(self)), + e => Err(Error::from(e)) + } + } + } + + pub fn open_as(mut self, codec: &Codec) -> Result { unsafe { if codec.is_decoder() { match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) { @@ -43,13 +52,13 @@ impl Decoder { } } - pub fn open_with(mut self, codec: &Codec, options: Dictionary) -> Result { + pub fn open_as_with(mut self, codec: &Codec, options: Dictionary) -> Result { unsafe { if codec.is_decoder() { - match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), &mut options.take()) { - 0 => Ok(Opened(self)), - e => Err(Error::from(e)) - } + match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), &mut options.take()) { + 0 => Ok(Opened(self)), + e => Err(Error::from(e)) + } } else { Err(Error::InvalidData) @@ -59,7 +68,7 @@ impl Decoder { pub fn video(self) -> Result { if let Some(ref codec) = find(self.id()) { - self.open(codec).and_then(|o| o.video()) + self.open_as(codec).and_then(|o| o.video()) } else { Err(Error::DecoderNotFound) @@ -68,7 +77,7 @@ impl Decoder { pub fn audio(self) -> Result { if let Some(ref codec) = find(self.id()) { - self.open(codec).and_then(|o| o.audio()) + self.open_as(codec).and_then(|o| o.audio()) } else { Err(Error::DecoderNotFound) @@ -77,7 +86,7 @@ impl Decoder { pub fn subtitle(self) -> Result { if let Some(ref codec) = find(self.id()) { - self.open(codec).and_then(|o| o.subtitle()) + self.open_as(codec).and_then(|o| o.subtitle()) } else { Err(Error::DecoderNotFound) diff --git a/src/codec/encoder/audio.rs b/src/codec/encoder/audio.rs index 09be933..ee6a09d 100644 --- a/src/codec/encoder/audio.rs +++ b/src/codec/encoder/audio.rs @@ -1,29 +1,56 @@ use std::ops::{Deref, DerefMut}; +use std::ptr; use libc::c_int; use ffi::*; -use super::Encoder; -use ::{Packet, Error}; +use super::Encoder as Super; +use ::{Packet, Error, Dictionary, Codec}; use ::frame; -pub struct Audio(pub Encoder); +pub struct Audio(pub Super); impl Audio { - pub fn encode(&mut self, frame: &frame::Audio, out: &mut Packet) -> Result { + pub fn open(mut self) -> Result { unsafe { - let mut got: c_int = 0; + match avcodec_open2(self.as_mut_ptr(), ptr::null(), ptr::null_mut()) { + 0 => Ok(Encoder(self)), + e => Err(Error::from(e)) + } + } + } - match avcodec_encode_audio2(self.as_mut_ptr(), out.as_mut_ptr(), frame.as_ptr(), &mut got) { - e if e < 0 => Err(Error::from(e)), - _ => Ok(got != 0) + pub fn open_as(mut self, codec: &Codec) -> Result { + unsafe { + if codec.is_encoder() { + match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) { + 0 => Ok(Encoder(self)), + e => Err(Error::from(e)) + } + } + else { + Err(Error::InvalidData) + } + } + } + + pub fn open_as_with(mut self, codec: &Codec, options: Dictionary) -> Result { + unsafe { + if codec.is_encoder() { + match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), &mut options.take()) { + 0 => Ok(Encoder(self)), + e => Err(Error::from(e)) + } + } + else { + Err(Error::InvalidData) } } } } impl Deref for Audio { - type Target = Encoder; + type Target = Super; fn deref(&self) -> &::Target { &self.0 @@ -35,3 +62,26 @@ impl DerefMut for Audio { &mut self.0 } } + +pub struct Encoder(pub Audio); + +impl Encoder { + pub fn encode(&mut self, frame: &frame::Audio, out: &mut Packet) -> Result { + unsafe { + let mut got: c_int = 0; + + match avcodec_encode_audio2(self.0.as_mut_ptr(), out.as_mut_ptr(), frame.as_ptr(), &mut got) { + e if e < 0 => Err(Error::from(e)), + _ => Ok(got != 0) + } + } + } +} + +impl Deref for Encoder { + type Target = Audio; + + fn deref(&self) -> &::Target { + &self.0 + } +} diff --git a/src/codec/encoder/subtitle.rs b/src/codec/encoder/subtitle.rs index 03f1df7..d96547b 100644 --- a/src/codec/encoder/subtitle.rs +++ b/src/codec/encoder/subtitle.rs @@ -1,26 +1,55 @@ use std::ops::{Deref, DerefMut}; +use std::ptr; use libc::c_int; use ffi::*; -use super::Encoder; -use ::Error; +use super::Encoder as Super; +use ::{Error, Dictionary, Codec}; -pub struct Subtitle(pub Encoder); +pub struct Subtitle(pub Super); impl Subtitle { - pub fn encode(&mut self, subtitle: &::Subtitle, out: &mut [u8]) -> Result { + pub fn open(mut self) -> Result { unsafe { - match avcodec_encode_subtitle(self.as_mut_ptr(), out.as_mut_ptr(), out.len() as c_int, subtitle.as_ptr()) { - e if e < 0 => Err(Error::from(e)), - _ => Ok(true) + match avcodec_open2(self.as_mut_ptr(), ptr::null(), ptr::null_mut()) { + 0 => Ok(Encoder(self)), + e => Err(Error::from(e)) + } + } + } + + pub fn open_as(mut self, codec: &Codec) -> Result { + unsafe { + if codec.is_encoder() { + match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) { + 0 => Ok(Encoder(self)), + e => Err(Error::from(e)) + } + } + else { + Err(Error::InvalidData) + } + } + } + + pub fn open_as_with(mut self, codec: &Codec, options: Dictionary) -> Result { + unsafe { + if codec.is_encoder() { + match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), &mut options.take()) { + 0 => Ok(Encoder(self)), + e => Err(Error::from(e)) + } + } + else { + Err(Error::InvalidData) } } } } impl Deref for Subtitle { - type Target = Encoder; + type Target = Super; fn deref(&self) -> &::Target { &self.0 @@ -32,3 +61,24 @@ impl DerefMut for Subtitle { &mut self.0 } } + +pub struct Encoder(pub Subtitle); + +impl Encoder { + pub fn encode(&mut self, subtitle: &::Subtitle, out: &mut [u8]) -> Result { + unsafe { + match avcodec_encode_subtitle(self.0.as_mut_ptr(), out.as_mut_ptr(), out.len() as c_int, subtitle.as_ptr()) { + e if e < 0 => Err(Error::from(e)), + _ => Ok(true) + } + } + } +} + +impl Deref for Encoder { + type Target = Subtitle; + + fn deref(&self) -> &::Target { + &self.0 + } +} diff --git a/src/codec/encoder/video.rs b/src/codec/encoder/video.rs index 35204fb..8eb7c8e 100644 --- a/src/codec/encoder/video.rs +++ b/src/codec/encoder/video.rs @@ -1,23 +1,51 @@ use std::ops::{Deref, DerefMut}; +use std::ptr; use libc::{c_int, c_float}; use ffi::*; -use super::{Encoder, MotionEstimation, Prediction, Comparison, Decision}; -use ::{Packet, Error, Rational}; +use super::Encoder as Super; +use super::{MotionEstimation, Prediction, Comparison, Decision}; +use ::{Packet, Error, Rational, Dictionary, Codec}; use ::frame; use ::format; -pub struct Video(pub Encoder); +pub struct Video(pub Super); impl Video { - pub fn encode(&mut self, frame: &frame::Video, out: &mut Packet) -> Result { + pub fn open(mut self) -> Result { unsafe { - let mut got: c_int = 0; + match avcodec_open2(self.as_mut_ptr(), ptr::null(), ptr::null_mut()) { + 0 => Ok(Encoder(self)), + e => Err(Error::from(e)) + } + } + } - match avcodec_encode_video2(self.as_mut_ptr(), out.as_mut_ptr(), frame.as_ptr(), &mut got) { - e if e < 0 => Err(Error::from(e)), - _ => Ok(got != 0) + pub fn open_as(mut self, codec: &Codec) -> Result { + unsafe { + if codec.is_encoder() { + match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) { + 0 => Ok(Encoder(self)), + e => Err(Error::from(e)) + } + } + else { + Err(Error::InvalidData) + } + } + } + + pub fn open_as_with(mut self, codec: &Codec, options: Dictionary) -> Result { + unsafe { + if codec.is_encoder() { + match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), &mut options.take()) { + 0 => Ok(Encoder(self)), + e => Err(Error::from(e)) + } + } + else { + Err(Error::InvalidData) } } } @@ -226,7 +254,7 @@ impl Video { } impl Deref for Video { - type Target = Encoder; + type Target = Super; fn deref(&self) -> &::Target { &self.0 @@ -238,3 +266,26 @@ impl DerefMut for Video { &mut self.0 } } + +pub struct Encoder(pub Video); + +impl Encoder { + pub fn encode(&mut self, frame: &frame::Video, out: &mut Packet) -> Result { + unsafe { + let mut got: c_int = 0; + + match avcodec_encode_video2(self.0.as_mut_ptr(), out.as_mut_ptr(), frame.as_ptr(), &mut got) { + e if e < 0 => Err(Error::from(e)), + _ => Ok(got != 0) + } + } + } +} + +impl Deref for Encoder { + type Target = Video; + + fn deref(&self) -> &::Target { + &self.0 + } +}