diff --git a/src/codec/decoder/opened.rs b/src/codec/decoder/opened.rs index d1cf5d6..719aeda 100644 --- a/src/codec/decoder/opened.rs +++ b/src/codec/decoder/opened.rs @@ -1,10 +1,10 @@ use std::ops::{Deref, DerefMut}; +use std::ptr; use super::{Audio, Decoder, Subtitle, Video}; use codec::{Context, Profile}; use ffi::*; -use media; -use {Error, Rational}; +use {media, packet, Error, Frame, Rational}; pub struct Opened(pub Decoder); @@ -33,6 +33,35 @@ impl Opened { } } + pub fn send_packet(&mut self, packet: &P) -> Result<(), Error> { + unsafe { + match avcodec_send_packet(self.as_mut_ptr(), packet.as_ptr()) { + e if e < 0 => Err(Error::from(e)), + _ => Ok(()), + } + } + } + + /// Sends a NULL packet to the decoder to signal end of stream and enter + /// draining mode. + pub fn send_eof(&mut self) -> Result<(), Error> { + unsafe { + match avcodec_send_packet(self.as_mut_ptr(), ptr::null()) { + e if e < 0 => Err(Error::from(e)), + _ => Ok(()), + } + } + } + + pub fn receive_frame(&mut self, frame: &mut Frame) -> Result<(), Error> { + unsafe { + match avcodec_receive_frame(self.as_mut_ptr(), frame.as_mut_ptr()) { + e if e < 0 => Err(Error::from(e)), + _ => Ok(()), + } + } + } + pub fn bit_rate(&self) -> usize { unsafe { (*self.as_ptr()).bit_rate as usize } } diff --git a/src/codec/encoder/encoder.rs b/src/codec/encoder/encoder.rs index 5d1fa8d..3dd507b 100644 --- a/src/codec/encoder/encoder.rs +++ b/src/codec/encoder/encoder.rs @@ -1,9 +1,12 @@ use std::ops::{Deref, DerefMut}; +use std::ptr; + +use ffi::*; +use libc::c_int; use super::{audio, subtitle, video}; use codec::Context; -use libc::c_int; -use {media, Error, Rational}; +use {media, packet, Error, Frame, Rational}; pub struct Encoder(pub Context); @@ -56,6 +59,30 @@ impl Encoder { } } + pub fn send_frame(&mut self, frame: &Frame) -> Result<(), Error> { + unsafe { + match avcodec_send_frame(self.as_mut_ptr(), frame.as_ptr()) { + e if e < 0 => Err(Error::from(e)), + _ => Ok(()), + } + } + } + + /// Sends a NULL packet to the encoder to signal end of stream and enter + /// draining mode. + pub fn send_eof(&mut self) -> Result<(), Error> { + unsafe { self.send_frame(&Frame::wrap(ptr::null_mut())) } + } + + pub fn receive_packet(&mut self, packet: &mut P) -> Result<(), Error> { + unsafe { + match avcodec_receive_packet(self.as_mut_ptr(), packet.as_mut_ptr()) { + e if e < 0 => Err(Error::from(e)), + _ => Ok(()), + } + } + } + pub fn set_bit_rate(&mut self, value: usize) { unsafe { (*self.as_mut_ptr()).bit_rate = value as i64;