Expose packet methods

This commit is contained in:
Zhiming Wang
2021-03-21 20:19:41 +01:00
committed by meh
parent 4a855c053e
commit 89927694ae
4 changed files with 66 additions and 7 deletions
+1
View File
@@ -0,0 +1 @@
eval "$(lorri direnv)"
+34 -2
View File
@@ -1,10 +1,13 @@
use std::ops::{Deref, DerefMut};
use std::{
ops::{Deref, DerefMut},
ptr,
};
use super::{Audio, Decoder, Subtitle, Video};
use crate::{
codec::{Context, Profile},
ffi::*,
media, Error, Rational,
media, packet, Error, Frame, Rational,
};
pub struct Opened(pub Decoder);
@@ -37,6 +40,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 }
}
+30 -4
View File
@@ -1,9 +1,11 @@
use std::ops::{Deref, DerefMut};
use libc::c_int;
use std::{
ops::{Deref, DerefMut},
os::raw::c_int,
ptr,
};
use super::{audio, subtitle, video};
use crate::{codec::Context, media, Error, Rational};
use crate::{codec::Context, ffi::*, media, packet, Error, Frame, Rational};
pub struct Encoder(pub Context);
@@ -56,6 +58,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_strict_std_compliance(&mut self, value: i32) {
unsafe {
(*self.as_mut_ptr()).strict_std_compliance = value as c_int;
+1 -1
View File
@@ -71,7 +71,7 @@ impl Context {
}
#[inline]
fn nb_streams(&self) -> u32 {
pub fn nb_streams(&self) -> u32 {
unsafe { (*self.as_ptr()).nb_streams }
}