codec: improve encoder state transitions

This commit improves the state transitions for the `codec::*` structs as
discussed in #7.
This commit is contained in:
lummax 2015-08-25 16:55:30 +02:00 committed by meh
parent ac5d8cfbe8
commit c867dd6259
4 changed files with 195 additions and 35 deletions

View File

@ -29,7 +29,16 @@ use ::{Codec, Error, Discard, Dictionary};
pub struct Decoder(pub Context);
impl Decoder {
pub fn open(mut self, codec: &Codec) -> Result<Opened, Error> {
pub fn open(mut self) -> Result<Opened, Error> {
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<Opened, Error> {
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<Opened, Error> {
pub fn open_as_with(mut self, codec: &Codec, options: Dictionary) -> Result<Opened, Error> {
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<Video, Error> {
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<Audio, Error> {
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<Subtitle, Error> {
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)

View File

@ -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
}
}

View File

@ -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<bool, Error> {
pub fn open(mut self) -> Result<Encoder, Error> {
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<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 Subtitle {
type Target = Encoder;
type Target = Super;
fn deref(&self) -> &<Self as Deref>::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<bool, Error> {
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) -> &<Self as Deref>::Target {
&self.0
}
}

View File

@ -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<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_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<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)
}
}
}
@ -226,7 +254,7 @@ impl Video {
}
impl Deref for Video {
type Target = Encoder;
type Target = Super;
fn deref(&self) -> &<Self as Deref>::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<bool, Error> {
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) -> &<Self as Deref>::Target {
&self.0
}
}