codec: expose modern send/receive packet/frame APIs
avcodec_encode_video2()/avcodec_decode_video2/avcodec_encode_audio2()/avcodec_decode_audio4() have been deprecated since FFmpeg 3.1, with modern avcodec_send_packet()/avcodec_receive_frame()/avcodec_send_frame()/avcodec_receive_packet() as replacements. This commit adds send_packet(), send_eof() and receive_frame() to decoder::Opened; send_frame(), send_eof() and receive_packet() to encoder::Encoder to expose these modern APIs.
This commit is contained in:
parent
f88f1a981c
commit
363001febe
@ -1,10 +1,10 @@
|
|||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
use super::{Audio, Decoder, Subtitle, Video};
|
use super::{Audio, Decoder, Subtitle, Video};
|
||||||
use codec::{Context, Profile};
|
use codec::{Context, Profile};
|
||||||
use ffi::*;
|
use ffi::*;
|
||||||
use media;
|
use {media, packet, Error, Frame, Rational};
|
||||||
use {Error, Rational};
|
|
||||||
|
|
||||||
pub struct Opened(pub Decoder);
|
pub struct Opened(pub Decoder);
|
||||||
|
|
||||||
@ -33,6 +33,35 @@ impl Opened {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn send_packet<P: packet::Ref>(&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 {
|
pub fn bit_rate(&self) -> usize {
|
||||||
unsafe { (*self.as_ptr()).bit_rate as usize }
|
unsafe { (*self.as_ptr()).bit_rate as usize }
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
|
use ffi::*;
|
||||||
|
use libc::c_int;
|
||||||
|
|
||||||
use super::{audio, subtitle, video};
|
use super::{audio, subtitle, video};
|
||||||
use codec::Context;
|
use codec::Context;
|
||||||
use libc::c_int;
|
use {media, packet, Error, Frame, Rational};
|
||||||
use {media, Error, Rational};
|
|
||||||
|
|
||||||
pub struct Encoder(pub Context);
|
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<P: packet::Mut>(&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) {
|
pub fn set_bit_rate(&mut self, value: usize) {
|
||||||
unsafe {
|
unsafe {
|
||||||
(*self.as_mut_ptr()).bit_rate = value as i64;
|
(*self.as_mut_ptr()).bit_rate = value as i64;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user