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:
Zhiming Wang 2020-08-08 00:28:51 +08:00
parent f88f1a981c
commit 363001febe
No known key found for this signature in database
GPG Key ID: 5B58F95EC95965D8
2 changed files with 60 additions and 4 deletions

View File

@ -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<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 {
unsafe { (*self.as_ptr()).bit_rate as usize }
}

View File

@ -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<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) {
unsafe {
(*self.as_mut_ptr()).bit_rate = value as i64;