codec: add various Codec related stuff
This commit is contained in:
parent
9c8ca6601f
commit
66901497ab
61
src/codec/codec.rs
Normal file
61
src/codec/codec.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use std::marker::PhantomData;
|
||||
use std::ffi::CStr;
|
||||
use std::str::from_utf8_unchecked;
|
||||
|
||||
use ffi::*;
|
||||
use super::{Id, Context};
|
||||
use ::media;
|
||||
use ::Error;
|
||||
use ::codec::context::Opened;
|
||||
|
||||
pub struct Codec<'a> {
|
||||
pub ptr: *mut AVCodec,
|
||||
|
||||
_marker: PhantomData<&'a i32>,
|
||||
}
|
||||
|
||||
impl<'a> Codec<'a> {
|
||||
pub fn wrap(ptr: *mut AVCodec) -> Self {
|
||||
Codec { ptr: ptr, _marker: PhantomData }
|
||||
}
|
||||
|
||||
pub fn open(&self) -> Result<Opened<'a>, Error> {
|
||||
Context::new().open(self)
|
||||
}
|
||||
|
||||
pub fn is_encoder(&self) -> bool {
|
||||
unsafe {
|
||||
av_codec_is_encoder(self.ptr) != 0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_decoder(&self) -> bool {
|
||||
unsafe {
|
||||
av_codec_is_decoder(self.ptr) != 0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&'a self) -> &'a str {
|
||||
unsafe {
|
||||
from_utf8_unchecked(CStr::from_ptr((*self.ptr).name).to_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn description(&'a self) -> &'a str {
|
||||
unsafe {
|
||||
from_utf8_unchecked(CStr::from_ptr((*self.ptr).long_name).to_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn kind(&self) -> media::Type {
|
||||
unsafe {
|
||||
media::Type::from((*self.ptr).kind)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn id(&self) -> Id {
|
||||
unsafe {
|
||||
Id::from((*self.ptr).id)
|
||||
}
|
||||
}
|
||||
}
|
198
src/codec/context.rs
Normal file
198
src/codec/context.rs
Normal file
@ -0,0 +1,198 @@
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Deref;
|
||||
use std::ptr;
|
||||
use libc::c_int;
|
||||
|
||||
use ffi::*;
|
||||
use ::media;
|
||||
use ::{Error, Codec, Dictionary, Packet, Subtitle};
|
||||
use super::{Id, Encode, Decode};
|
||||
use ::frame;
|
||||
|
||||
pub struct Context<'a> {
|
||||
pub ptr: *mut AVCodecContext,
|
||||
|
||||
own: bool,
|
||||
_marker: PhantomData<&'a i32>,
|
||||
}
|
||||
|
||||
impl<'a> Context<'a> {
|
||||
pub fn new() -> Self {
|
||||
unsafe {
|
||||
Context { ptr: avcodec_alloc_context3(ptr::null()), own: true, _marker: PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wrap(ptr: *mut AVCodecContext) -> Self {
|
||||
Context { ptr: ptr, own: false, _marker: PhantomData }
|
||||
}
|
||||
|
||||
pub fn open(self, codec: &Codec) -> Result<Opened<'a>, Error> {
|
||||
unsafe {
|
||||
match avcodec_open2(self.ptr, codec.ptr, ptr::null_mut()) {
|
||||
0 => Ok(Opened(self)),
|
||||
e => Err(Error::new(e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_with(self, codec: &Codec, mut options: Dictionary) -> Result<Opened<'a>, Error> {
|
||||
unsafe {
|
||||
match avcodec_open2(self.ptr, codec.ptr, &mut options.ptr) {
|
||||
0 => Ok(Opened(self)),
|
||||
e => Err(Error::new(e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn kind(&self) -> media::Type {
|
||||
unsafe {
|
||||
media::Type::from((*self.ptr).codec_type)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn id(&self) -> Id {
|
||||
unsafe {
|
||||
Id::from((*self.ptr).codec_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for Context<'a> {
|
||||
fn drop(&mut self) {
|
||||
if self.own {
|
||||
unsafe {
|
||||
avcodec_free_context(&mut self.ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Clone for Context<'a> {
|
||||
fn clone(&self) -> Self {
|
||||
let mut ctx = Context::new();
|
||||
ctx.clone_from(self);
|
||||
|
||||
ctx
|
||||
}
|
||||
|
||||
fn clone_from(&mut self, source: &Self) {
|
||||
unsafe {
|
||||
avcodec_copy_context(self.ptr, source.ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Decode for Context<'a> {
|
||||
fn video(&self, packet: &Packet, out: &mut frame::Video) -> Result<bool, Error> {
|
||||
unsafe {
|
||||
let mut got: c_int = 0;
|
||||
|
||||
match avcodec_decode_video2(self.ptr, out.ptr, &mut got, &packet.val) {
|
||||
e if e < 0 => Err(Error::new(e)),
|
||||
_ => Ok(got != 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn audio(&self, packet: &Packet, out: &mut frame::Audio) -> Result<bool, Error> {
|
||||
unsafe {
|
||||
let mut got: c_int = 0;
|
||||
|
||||
match avcodec_decode_audio4(self.ptr, out.ptr, &mut got, &packet.val) {
|
||||
e if e < 0 => Err(Error::new(e)),
|
||||
_ => Ok(got != 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn subtitle(&self, packet: &Packet, out: &mut Subtitle) -> Result<bool, Error> {
|
||||
unsafe {
|
||||
let mut got: c_int = 0;
|
||||
|
||||
match avcodec_decode_subtitle2(self.ptr, &mut out.val, &mut got, &packet.val) {
|
||||
e if e < 0 => Err(Error::new(e)),
|
||||
_ => Ok(got != 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Encode for Context<'a> {
|
||||
fn video(&self, frame: &frame::Video, out: &mut Packet) -> Result<bool, Error> {
|
||||
unsafe {
|
||||
let mut got: c_int = 0;
|
||||
|
||||
match avcodec_encode_video2(self.ptr, &mut out.val, frame.ptr, &mut got) {
|
||||
e if e < 0 => Err(Error::new(e)),
|
||||
_ => Ok(got != 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn audio(&self, frame: &frame::Audio, out: &mut Packet) -> Result<bool, Error> {
|
||||
unsafe {
|
||||
let mut got: c_int = 0;
|
||||
|
||||
match avcodec_encode_audio2(self.ptr, &mut out.val, frame.ptr, &mut got) {
|
||||
e if e < 0 => Err(Error::new(e)),
|
||||
_ => Ok(got != 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn subtitle(&self, subtitle: &Subtitle, out: &mut [u8]) -> Result<bool, Error> {
|
||||
unsafe {
|
||||
match avcodec_encode_subtitle(self.ptr, out.as_mut_ptr(), out.len() as c_int, &subtitle.val) {
|
||||
e if e < 0 => Err(Error::new(e)),
|
||||
_ => Ok(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Opened<'a>(Context<'a>);
|
||||
|
||||
impl<'a> Decode for Opened<'a> {
|
||||
fn video(&self, packet: &Packet, out: &mut frame::Video) -> Result<bool, Error> {
|
||||
Decode::video(&self.0, packet, out)
|
||||
}
|
||||
|
||||
fn audio(&self, packet: &Packet, out: &mut frame::Audio) -> Result<bool, Error> {
|
||||
Decode::audio(&self.0, packet, out)
|
||||
}
|
||||
|
||||
fn subtitle(&self, packet: &Packet, out: &mut Subtitle) -> Result<bool, Error> {
|
||||
Decode::subtitle(&self.0, packet, out)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Encode for Opened<'a> {
|
||||
fn video(&self, frame: &frame::Video, out: &mut Packet) -> Result<bool, Error> {
|
||||
Encode::video(&self.0, frame, out)
|
||||
}
|
||||
|
||||
fn audio(&self, frame: &frame::Audio, out: &mut Packet) -> Result<bool, Error> {
|
||||
Encode::audio(&self.0, frame, out)
|
||||
}
|
||||
|
||||
fn subtitle(&self, subtitle: &Subtitle, out: &mut [u8]) -> Result<bool, Error> {
|
||||
Encode::subtitle(&self.0, subtitle, out)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for Opened<'a> {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
avcodec_close(self.0.ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Deref for Opened<'a> {
|
||||
type Target = Context<'a>;
|
||||
|
||||
fn deref(&self) -> &Context<'a> {
|
||||
&self.0
|
||||
}
|
||||
}
|
39
src/codec/decoder.rs
Normal file
39
src/codec/decoder.rs
Normal file
@ -0,0 +1,39 @@
|
||||
use std::ffi::CString;
|
||||
use std::ptr;
|
||||
|
||||
use ffi::*;
|
||||
use ::codec::Id;
|
||||
use ::{Codec, Packet, Subtitle, Error};
|
||||
use ::frame;
|
||||
|
||||
pub trait Decode {
|
||||
fn video(&self, packet: &Packet, out: &mut frame::Video) -> Result<bool, Error>;
|
||||
fn audio(&self, packet: &Packet, out: &mut frame::Audio) -> Result<bool, Error>;
|
||||
fn subtitle(&self, packet: &Packet, out: &mut Subtitle) -> Result<bool, Error>;
|
||||
}
|
||||
|
||||
pub fn find(id: Id) -> Option<Codec<'static>> {
|
||||
unsafe {
|
||||
let ptr = avcodec_find_decoder(id.into());
|
||||
|
||||
if ptr == ptr::null_mut() {
|
||||
None
|
||||
}
|
||||
else {
|
||||
Some(Codec::wrap(ptr))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_by_name(name: &str) -> Option<Codec<'static>> {
|
||||
unsafe {
|
||||
let ptr = avcodec_find_decoder_by_name(CString::new(name).unwrap().as_ptr());
|
||||
|
||||
if ptr == ptr::null_mut() {
|
||||
None
|
||||
}
|
||||
else {
|
||||
Some(Codec::wrap(ptr))
|
||||
}
|
||||
}
|
||||
}
|
39
src/codec/encoder.rs
Normal file
39
src/codec/encoder.rs
Normal file
@ -0,0 +1,39 @@
|
||||
use std::ffi::CString;
|
||||
use std::ptr;
|
||||
|
||||
use ffi::*;
|
||||
use ::codec::Id;
|
||||
use ::{Codec, Packet, Subtitle, Error};
|
||||
use ::frame;
|
||||
|
||||
pub trait Encode {
|
||||
fn video(&self, frame: &frame::Video, out: &mut Packet) -> Result<bool, Error>;
|
||||
fn audio(&self, frame: &frame::Audio, out: &mut Packet) -> Result<bool, Error>;
|
||||
fn subtitle(&self, subtitle: &Subtitle, out: &mut [u8]) -> Result<bool, Error>;
|
||||
}
|
||||
|
||||
pub fn find(id: Id) -> Option<Codec<'static>> {
|
||||
unsafe {
|
||||
let ptr = avcodec_find_encoder(id.into());
|
||||
|
||||
if ptr == ptr::null_mut() {
|
||||
None
|
||||
}
|
||||
else {
|
||||
Some(Codec::wrap(ptr))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_by_name(name: &str) -> Option<Codec<'static>> {
|
||||
unsafe {
|
||||
let ptr = avcodec_find_encoder_by_name(CString::new(name).unwrap().as_ptr());
|
||||
|
||||
if ptr == ptr::null_mut() {
|
||||
None
|
||||
}
|
||||
else {
|
||||
Some(Codec::wrap(ptr))
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,17 @@ pub mod subtitle;
|
||||
|
||||
pub mod discard;
|
||||
|
||||
pub mod context;
|
||||
pub use self::context::Context;
|
||||
|
||||
pub mod codec;
|
||||
|
||||
pub mod encoder;
|
||||
pub use self::encoder::Encode;
|
||||
|
||||
pub mod decoder;
|
||||
pub use self::decoder::Decode;
|
||||
|
||||
use std::ffi::CStr;
|
||||
use std::str::from_utf8_unchecked;
|
||||
|
||||
|
@ -18,3 +18,6 @@ pub mod codec;
|
||||
pub use codec::packet::{self, Packet};
|
||||
pub use codec::subtitle::Subtitle;
|
||||
pub use codec::discard::Discard;
|
||||
pub use codec::codec::Codec;
|
||||
pub use codec::encoder::{self, Encode};
|
||||
pub use codec::decoder::{self, Decode};
|
||||
|
Loading…
x
Reference in New Issue
Block a user