codec: improve encoder state transitions
This commit improves the state transitions for the `codec::*` structs as discussed in #7.
This commit is contained in:
@ -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<bool, Error> {
|
||||
pub fn open(mut self) -> Result<Encoder, Error> {
|
||||
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<Encoder, Error> {
|
||||
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<Encoder, Error> {
|
||||
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) -> &<Self as Deref>::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<bool, Error> {
|
||||
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) -> &<Self as Deref>::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user