codec: improve decoder state transitions
This commit improves the state transitions for the `codec::*` structs as discussed in #7.
This commit is contained in:
@ -3,9 +3,8 @@ use std::ffi::CStr;
|
|||||||
use std::str::from_utf8_unchecked;
|
use std::str::from_utf8_unchecked;
|
||||||
|
|
||||||
use ffi::*;
|
use ffi::*;
|
||||||
use super::{Id, Context, Video, Audio, Capabilities, Profile};
|
use super::{Id, Video, Audio, Capabilities, Profile};
|
||||||
use ::{Error, media};
|
use ::{Error, media};
|
||||||
use ::codec::context::Opened;
|
|
||||||
|
|
||||||
pub struct Codec<'a> {
|
pub struct Codec<'a> {
|
||||||
ptr: *mut AVCodec,
|
ptr: *mut AVCodec,
|
||||||
@ -28,10 +27,6 @@ impl<'a> Codec<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Codec<'a> {
|
impl<'a> Codec<'a> {
|
||||||
pub fn open(&self) -> Result<Opened, Error> {
|
|
||||||
Context::new().open(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_encoder(&self) -> bool {
|
pub fn is_encoder(&self) -> bool {
|
||||||
unsafe {
|
unsafe {
|
||||||
av_codec_is_encoder(self.as_ptr()) != 0
|
av_codec_is_encoder(self.as_ptr()) != 0
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
use std::ops::{Deref, DerefMut};
|
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
|
||||||
use libc::c_int;
|
use libc::c_int;
|
||||||
use ffi::*;
|
use ffi::*;
|
||||||
use ::media;
|
use ::media;
|
||||||
use ::{Error, Codec, Dictionary};
|
use ::Codec;
|
||||||
use super::{Id, Debug, Compliance, threading};
|
use super::{Id, Debug, Compliance, threading};
|
||||||
use super::decoder::Decoder;
|
use super::decoder::Decoder;
|
||||||
use super::encoder::Encoder;
|
use super::encoder::Encoder;
|
||||||
@ -38,40 +37,12 @@ impl Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn open(mut self, codec: &Codec) -> Result<Opened, Error> {
|
pub fn decoder(self) -> Decoder {
|
||||||
unsafe {
|
Decoder(self)
|
||||||
match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) {
|
|
||||||
0 => Ok(Opened(self)),
|
|
||||||
e => Err(Error::from(e))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn open_with(mut self, codec: &Codec, options: Dictionary) -> Result<Opened, Error> {
|
pub fn encoder(self) -> Encoder {
|
||||||
unsafe {
|
Encoder(self)
|
||||||
match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), &mut options.take()) {
|
|
||||||
0 => Ok(Opened(self)),
|
|
||||||
e => Err(Error::from(e))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn decoder(&self) -> Result<Decoder, Error> {
|
|
||||||
if let Some(ref codec) = super::decoder::find(self.id()) {
|
|
||||||
self.clone().open(codec).and_then(|c| c.decoder())
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Err(Error::DecoderNotFound)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn encoder(&self) -> Result<Encoder, Error> {
|
|
||||||
if let Some(ref codec) = super::encoder::find(self.id()) {
|
|
||||||
self.clone().open(codec).and_then(|c| c.encoder())
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Err(Error::EncoderNotFound)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn codec(&self) -> Option<Codec> {
|
pub fn codec(&self) -> Option<Codec> {
|
||||||
@ -164,59 +135,3 @@ impl Clone for Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Opened(pub Context);
|
|
||||||
|
|
||||||
impl Opened {
|
|
||||||
pub fn decoder(self) -> Result<Decoder, Error> {
|
|
||||||
let mut valid = false;
|
|
||||||
|
|
||||||
if let Some(codec) = self.codec() {
|
|
||||||
valid = codec.is_decoder();
|
|
||||||
}
|
|
||||||
|
|
||||||
if valid {
|
|
||||||
Ok(Decoder(self))
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Err(Error::InvalidData)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn encoder(self) -> Result<Encoder, Error> {
|
|
||||||
let mut valid = false;
|
|
||||||
|
|
||||||
if let Some(codec) = self.codec() {
|
|
||||||
valid = codec.is_encoder();
|
|
||||||
}
|
|
||||||
|
|
||||||
if valid {
|
|
||||||
Ok(Encoder(self))
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Err(Error::InvalidData)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for Opened {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
unsafe {
|
|
||||||
avcodec_close(self.as_mut_ptr());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for Opened {
|
|
||||||
type Target = Context;
|
|
||||||
|
|
||||||
fn deref(&self) -> &<Self as Deref>::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DerefMut for Opened {
|
|
||||||
fn deref_mut(&mut self) -> &mut<Self as Deref>::Target {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -3,12 +3,12 @@ use std::ops::{Deref, DerefMut};
|
|||||||
use libc::c_int;
|
use libc::c_int;
|
||||||
use ffi::*;
|
use ffi::*;
|
||||||
|
|
||||||
use super::Decoder;
|
use super::Opened;
|
||||||
use ::{Packet, Error, AudioService, ChannelLayout};
|
use ::{Packet, Error, AudioService, ChannelLayout};
|
||||||
use ::frame;
|
use ::frame;
|
||||||
use ::util::format;
|
use ::util::format;
|
||||||
|
|
||||||
pub struct Audio(pub Decoder);
|
pub struct Audio(pub Opened);
|
||||||
|
|
||||||
impl Audio {
|
impl Audio {
|
||||||
pub fn decode(&mut self, packet: &Packet, out: &mut frame::Audio) -> Result<bool, Error> {
|
pub fn decode(&mut self, packet: &Packet, out: &mut frame::Audio) -> Result<bool, Error> {
|
||||||
@ -99,7 +99,7 @@ impl Audio {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for Audio {
|
impl Deref for Audio {
|
||||||
type Target = Decoder;
|
type Target = Opened;
|
||||||
|
|
||||||
fn deref(&self) -> &<Self as Deref>::Target {
|
fn deref(&self) -> &<Self as Deref>::Target {
|
||||||
&self.0
|
&self.0
|
||||||
|
@ -15,110 +15,78 @@ pub use self::conceal::Conceal;
|
|||||||
pub mod check;
|
pub mod check;
|
||||||
pub use self::check::Check;
|
pub use self::check::Check;
|
||||||
|
|
||||||
|
pub mod opened;
|
||||||
|
pub use self::opened::Opened;
|
||||||
|
|
||||||
|
use std::ptr;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::slice::from_raw_parts;
|
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
use ffi::*;
|
use ffi::*;
|
||||||
use super::{Id, Profile};
|
use super::{Id, Context};
|
||||||
use super::context::Opened;
|
use ::{Codec, Error, Dictionary};
|
||||||
use ::{Codec, Error, Discard, Rational};
|
|
||||||
use ::media;
|
|
||||||
|
|
||||||
pub struct Decoder(pub Opened);
|
pub struct Decoder(pub Context);
|
||||||
|
|
||||||
impl Decoder {
|
impl Decoder {
|
||||||
pub fn video(self) -> Result<Video, Error> {
|
pub fn open(mut self, codec: &Codec) -> Result<Opened, Error> {
|
||||||
if self.medium() == media::Type::Video {
|
unsafe {
|
||||||
Ok(Video(self))
|
if codec.is_decoder() {
|
||||||
|
match avcodec_open2(self.as_mut_ptr(), codec.as_ptr(), ptr::null_mut()) {
|
||||||
|
0 => Ok(Opened(self)),
|
||||||
|
e => Err(Error::from(e))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Err(Error::InvalidData)
|
Err(Error::InvalidData)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn open_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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Err(Error::InvalidData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn video(self) -> Result<Video, Error> {
|
||||||
|
if let Some(ref codec) = find(self.id()) {
|
||||||
|
self.open(codec).and_then(|o| o.video())
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Err(Error::DecoderNotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn audio(self) -> Result<Audio, Error> {
|
pub fn audio(self) -> Result<Audio, Error> {
|
||||||
if self.medium() == media::Type::Audio {
|
if let Some(ref codec) = find(self.id()) {
|
||||||
Ok(Audio(self))
|
self.open(codec).and_then(|o| o.audio())
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Err(Error::InvalidData)
|
Err(Error::DecoderNotFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subtitle(self) -> Result<Subtitle, Error> {
|
pub fn subtitle(self) -> Result<Subtitle, Error> {
|
||||||
if self.medium() == media::Type::Subtitle {
|
if let Some(ref codec) = find(self.id()) {
|
||||||
Ok(Subtitle(self))
|
self.open(codec).and_then(|o| o.subtitle())
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Err(Error::InvalidData)
|
Err(Error::DecoderNotFound)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn conceal(&mut self, value: Conceal) {
|
|
||||||
unsafe {
|
|
||||||
(*self.as_mut_ptr()).error_concealment = value.bits();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn check(&mut self, value: Check) {
|
|
||||||
unsafe {
|
|
||||||
(*self.as_mut_ptr()).err_recognition = value.bits();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn profile(&self) -> Profile {
|
|
||||||
unsafe {
|
|
||||||
Profile::from((self.id(), (*self.as_ptr()).profile))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn skip_loop_filter(&mut self, value: Discard) {
|
|
||||||
unsafe {
|
|
||||||
(*self.as_mut_ptr()).skip_loop_filter = value.into();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn skip_idct(&mut self, value: Discard) {
|
|
||||||
unsafe {
|
|
||||||
(*self.as_mut_ptr()).skip_idct = value.into();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn skip_frame(&mut self, value: Discard) {
|
|
||||||
unsafe {
|
|
||||||
(*self.as_mut_ptr()).skip_frame = value.into();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn subtitle_header(&self) -> &[u8] {
|
|
||||||
unsafe {
|
|
||||||
from_raw_parts((*self.as_ptr()).subtitle_header, (*self.as_ptr()).subtitle_header_size as usize)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn frame_rate(&self) -> Option<Rational> {
|
|
||||||
unsafe {
|
|
||||||
let value = (*self.as_ptr()).framerate;
|
|
||||||
|
|
||||||
if value == (AVRational { num: 0, den: 1 }) {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Some(Rational::from(value))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn time_base(&self) -> Rational {
|
|
||||||
unsafe {
|
|
||||||
Rational::from((*self.as_ptr()).time_base)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for Decoder {
|
impl Deref for Decoder {
|
||||||
type Target = Opened;
|
type Target = Context;
|
||||||
|
|
||||||
fn deref(&self) -> &<Self as Deref>::Target {
|
fn deref(&self) -> &<Self as Deref>::Target {
|
||||||
&self.0
|
&self.0
|
||||||
|
122
src/codec/decoder/opened.rs
Normal file
122
src/codec/decoder/opened.rs
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use std::slice::from_raw_parts;
|
||||||
|
|
||||||
|
use ffi::*;
|
||||||
|
use super::{Conceal, Check, Video, Audio, Subtitle, Decoder};
|
||||||
|
use ::codec::Profile;
|
||||||
|
use ::{Error, Discard, Rational};
|
||||||
|
use ::media;
|
||||||
|
|
||||||
|
pub struct Opened(pub Decoder);
|
||||||
|
|
||||||
|
impl Opened {
|
||||||
|
pub fn video(self) -> Result<Video, Error> {
|
||||||
|
if self.medium() == media::Type::Video {
|
||||||
|
Ok(Video(self))
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Err(Error::InvalidData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn audio(self) -> Result<Audio, Error> {
|
||||||
|
if self.medium() == media::Type::Audio {
|
||||||
|
Ok(Audio(self))
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Err(Error::InvalidData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn subtitle(self) -> Result<Subtitle, Error> {
|
||||||
|
if self.medium() == media::Type::Subtitle {
|
||||||
|
Ok(Subtitle(self))
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Err(Error::InvalidData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn conceal(&mut self, value: Conceal) {
|
||||||
|
unsafe {
|
||||||
|
(*self.as_mut_ptr()).error_concealment = value.bits();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn check(&mut self, value: Check) {
|
||||||
|
unsafe {
|
||||||
|
(*self.as_mut_ptr()).err_recognition = value.bits();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn profile(&self) -> Profile {
|
||||||
|
unsafe {
|
||||||
|
Profile::from((self.id(), (*self.as_ptr()).profile))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn skip_loop_filter(&mut self, value: Discard) {
|
||||||
|
unsafe {
|
||||||
|
(*self.as_mut_ptr()).skip_loop_filter = value.into();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn skip_idct(&mut self, value: Discard) {
|
||||||
|
unsafe {
|
||||||
|
(*self.as_mut_ptr()).skip_idct = value.into();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn skip_frame(&mut self, value: Discard) {
|
||||||
|
unsafe {
|
||||||
|
(*self.as_mut_ptr()).skip_frame = value.into();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn subtitle_header(&self) -> &[u8] {
|
||||||
|
unsafe {
|
||||||
|
from_raw_parts((*self.as_ptr()).subtitle_header, (*self.as_ptr()).subtitle_header_size as usize)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn frame_rate(&self) -> Option<Rational> {
|
||||||
|
unsafe {
|
||||||
|
let value = (*self.as_ptr()).framerate;
|
||||||
|
|
||||||
|
if value == (AVRational { num: 0, den: 1 }) {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Some(Rational::from(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn time_base(&self) -> Rational {
|
||||||
|
unsafe {
|
||||||
|
Rational::from((*self.as_ptr()).time_base)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Opened {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
unsafe {
|
||||||
|
avcodec_close(self.as_mut_ptr());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for Opened {
|
||||||
|
type Target = Decoder;
|
||||||
|
|
||||||
|
fn deref(&self) -> &<Self as Deref>::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for Opened {
|
||||||
|
fn deref_mut(&mut self) -> &mut<Self as Deref>::Target {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
@ -3,10 +3,10 @@ use std::ops::{Deref, DerefMut};
|
|||||||
use libc::c_int;
|
use libc::c_int;
|
||||||
use ffi::*;
|
use ffi::*;
|
||||||
|
|
||||||
use super::Decoder;
|
use super::Opened;
|
||||||
use ::{Packet, Error};
|
use ::{Packet, Error};
|
||||||
|
|
||||||
pub struct Subtitle(pub Decoder);
|
pub struct Subtitle(pub Opened);
|
||||||
|
|
||||||
impl Subtitle {
|
impl Subtitle {
|
||||||
pub fn decode(&mut self, packet: &Packet, out: &mut ::Subtitle) -> Result<bool, Error> {
|
pub fn decode(&mut self, packet: &Packet, out: &mut ::Subtitle) -> Result<bool, Error> {
|
||||||
@ -22,7 +22,7 @@ impl Subtitle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for Subtitle {
|
impl Deref for Subtitle {
|
||||||
type Target = Decoder;
|
type Target = Opened;
|
||||||
|
|
||||||
fn deref(&self) -> &<Self as Deref>::Target {
|
fn deref(&self) -> &<Self as Deref>::Target {
|
||||||
&self.0
|
&self.0
|
||||||
|
@ -3,14 +3,14 @@ use std::ops::{Deref, DerefMut};
|
|||||||
use libc::c_int;
|
use libc::c_int;
|
||||||
use ffi::*;
|
use ffi::*;
|
||||||
|
|
||||||
use super::{Decoder, slice};
|
use super::{Opened, slice};
|
||||||
use ::{Packet, Error, Rational, FieldOrder};
|
use ::{Packet, Error, Rational, FieldOrder};
|
||||||
use ::frame;
|
use ::frame;
|
||||||
use ::util::format;
|
use ::util::format;
|
||||||
use ::util::chroma;
|
use ::util::chroma;
|
||||||
use ::color;
|
use ::color;
|
||||||
|
|
||||||
pub struct Video(pub Decoder);
|
pub struct Video(pub Opened);
|
||||||
|
|
||||||
impl Video {
|
impl Video {
|
||||||
pub fn decode(&mut self, packet: &Packet, out: &mut frame::Video) -> Result<bool, Error> {
|
pub fn decode(&mut self, packet: &Packet, out: &mut frame::Video) -> Result<bool, Error> {
|
||||||
@ -137,7 +137,7 @@ impl Video {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for Video {
|
impl Deref for Video {
|
||||||
type Target = Decoder;
|
type Target = Opened;
|
||||||
|
|
||||||
fn deref(&self) -> &<Self as Deref>::Target {
|
fn deref(&self) -> &<Self as Deref>::Target {
|
||||||
&self.0
|
&self.0
|
||||||
|
@ -24,12 +24,11 @@ use std::ops::{Deref, DerefMut};
|
|||||||
|
|
||||||
use libc::c_int;
|
use libc::c_int;
|
||||||
use ffi::*;
|
use ffi::*;
|
||||||
use super::Id;
|
use super::{Id, Context};
|
||||||
use super::context::Opened;
|
|
||||||
use ::{Codec, Error, Rational};
|
use ::{Codec, Error, Rational};
|
||||||
use ::media;
|
use ::media;
|
||||||
|
|
||||||
pub struct Encoder(pub Opened);
|
pub struct Encoder(pub Context);
|
||||||
|
|
||||||
impl Encoder {
|
impl Encoder {
|
||||||
pub fn video(self) -> Result<Video, Error> {
|
pub fn video(self) -> Result<Video, Error> {
|
||||||
@ -108,7 +107,7 @@ impl Encoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for Encoder {
|
impl Deref for Encoder {
|
||||||
type Target = Opened;
|
type Target = Context;
|
||||||
|
|
||||||
fn deref(&self) -> &<Self as Deref>::Target {
|
fn deref(&self) -> &<Self as Deref>::Target {
|
||||||
&self.0
|
&self.0
|
||||||
|
Reference in New Issue
Block a user