codec: improve encoder state transitions
This commit improves the state transitions for the `codec::*` structs as discussed in #7.
This commit is contained in:
parent
ac5d8cfbe8
commit
c867dd6259
@ -29,7 +29,16 @@ use ::{Codec, Error, Discard, Dictionary};
|
|||||||
pub struct Decoder(pub Context);
|
pub struct Decoder(pub Context);
|
||||||
|
|
||||||
impl Decoder {
|
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 {
|
unsafe {
|
||||||
if codec.is_decoder() {
|
if codec.is_decoder() {
|
||||||
match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) {
|
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 {
|
unsafe {
|
||||||
if codec.is_decoder() {
|
if codec.is_decoder() {
|
||||||
match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), &mut options.take()) {
|
match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), &mut options.take()) {
|
||||||
0 => Ok(Opened(self)),
|
0 => Ok(Opened(self)),
|
||||||
e => Err(Error::from(e))
|
e => Err(Error::from(e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Err(Error::InvalidData)
|
Err(Error::InvalidData)
|
||||||
@ -59,7 +68,7 @@ impl Decoder {
|
|||||||
|
|
||||||
pub fn video(self) -> Result<Video, Error> {
|
pub fn video(self) -> Result<Video, Error> {
|
||||||
if let Some(ref codec) = find(self.id()) {
|
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 {
|
else {
|
||||||
Err(Error::DecoderNotFound)
|
Err(Error::DecoderNotFound)
|
||||||
@ -68,7 +77,7 @@ impl Decoder {
|
|||||||
|
|
||||||
pub fn audio(self) -> Result<Audio, Error> {
|
pub fn audio(self) -> Result<Audio, Error> {
|
||||||
if let Some(ref codec) = find(self.id()) {
|
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 {
|
else {
|
||||||
Err(Error::DecoderNotFound)
|
Err(Error::DecoderNotFound)
|
||||||
@ -77,7 +86,7 @@ impl Decoder {
|
|||||||
|
|
||||||
pub fn subtitle(self) -> Result<Subtitle, Error> {
|
pub fn subtitle(self) -> Result<Subtitle, Error> {
|
||||||
if let Some(ref codec) = find(self.id()) {
|
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 {
|
else {
|
||||||
Err(Error::DecoderNotFound)
|
Err(Error::DecoderNotFound)
|
||||||
|
@ -1,29 +1,56 @@
|
|||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
use libc::c_int;
|
use libc::c_int;
|
||||||
use ffi::*;
|
use ffi::*;
|
||||||
|
|
||||||
use super::Encoder;
|
use super::Encoder as Super;
|
||||||
use ::{Packet, Error};
|
use ::{Packet, Error, Dictionary, Codec};
|
||||||
use ::frame;
|
use ::frame;
|
||||||
|
|
||||||
pub struct Audio(pub Encoder);
|
pub struct Audio(pub Super);
|
||||||
|
|
||||||
impl Audio {
|
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 {
|
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) {
|
pub fn open_as(mut self, codec: &Codec) -> Result<Encoder, Error> {
|
||||||
e if e < 0 => Err(Error::from(e)),
|
unsafe {
|
||||||
_ => Ok(got != 0)
|
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 {
|
impl Deref for Audio {
|
||||||
type Target = Encoder;
|
type Target = Super;
|
||||||
|
|
||||||
fn deref(&self) -> &<Self as Deref>::Target {
|
fn deref(&self) -> &<Self as Deref>::Target {
|
||||||
&self.0
|
&self.0
|
||||||
@ -35,3 +62,26 @@ impl DerefMut for Audio {
|
|||||||
&mut self.0
|
&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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,26 +1,55 @@
|
|||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
use libc::c_int;
|
use libc::c_int;
|
||||||
use ffi::*;
|
use ffi::*;
|
||||||
|
|
||||||
use super::Encoder;
|
use super::Encoder as Super;
|
||||||
use ::Error;
|
use ::{Error, Dictionary, Codec};
|
||||||
|
|
||||||
pub struct Subtitle(pub Encoder);
|
pub struct Subtitle(pub Super);
|
||||||
|
|
||||||
impl Subtitle {
|
impl Subtitle {
|
||||||
pub fn encode(&mut self, subtitle: &::Subtitle, out: &mut [u8]) -> Result<bool, Error> {
|
pub fn open(mut self) -> Result<Encoder, Error> {
|
||||||
unsafe {
|
unsafe {
|
||||||
match avcodec_encode_subtitle(self.as_mut_ptr(), out.as_mut_ptr(), out.len() as c_int, subtitle.as_ptr()) {
|
match avcodec_open2(self.as_mut_ptr(), ptr::null(), ptr::null_mut()) {
|
||||||
e if e < 0 => Err(Error::from(e)),
|
0 => Ok(Encoder(self)),
|
||||||
_ => Ok(true)
|
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 {
|
impl Deref for Subtitle {
|
||||||
type Target = Encoder;
|
type Target = Super;
|
||||||
|
|
||||||
fn deref(&self) -> &<Self as Deref>::Target {
|
fn deref(&self) -> &<Self as Deref>::Target {
|
||||||
&self.0
|
&self.0
|
||||||
@ -32,3 +61,24 @@ impl DerefMut for Subtitle {
|
|||||||
&mut self.0
|
&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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,23 +1,51 @@
|
|||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
use libc::{c_int, c_float};
|
use libc::{c_int, c_float};
|
||||||
use ffi::*;
|
use ffi::*;
|
||||||
|
|
||||||
use super::{Encoder, MotionEstimation, Prediction, Comparison, Decision};
|
use super::Encoder as Super;
|
||||||
use ::{Packet, Error, Rational};
|
use super::{MotionEstimation, Prediction, Comparison, Decision};
|
||||||
|
use ::{Packet, Error, Rational, Dictionary, Codec};
|
||||||
use ::frame;
|
use ::frame;
|
||||||
use ::format;
|
use ::format;
|
||||||
|
|
||||||
pub struct Video(pub Encoder);
|
pub struct Video(pub Super);
|
||||||
|
|
||||||
impl Video {
|
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 {
|
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) {
|
pub fn open_as(mut self, codec: &Codec) -> Result<Encoder, Error> {
|
||||||
e if e < 0 => Err(Error::from(e)),
|
unsafe {
|
||||||
_ => Ok(got != 0)
|
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 {
|
impl Deref for Video {
|
||||||
type Target = Encoder;
|
type Target = Super;
|
||||||
|
|
||||||
fn deref(&self) -> &<Self as Deref>::Target {
|
fn deref(&self) -> &<Self as Deref>::Target {
|
||||||
&self.0
|
&self.0
|
||||||
@ -238,3 +266,26 @@ impl DerefMut for Video {
|
|||||||
&mut self.0
|
&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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user