You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.3 KiB
Rust

use crate::av::Rational;
use ffmpeg_sys_next::AVPacketSideDataType::AV_PKT_DATA_NEW_EXTRADATA;
use ffmpeg_sys_next::{
av_packet_alloc, av_packet_get_side_data, av_packet_rescale_ts, av_packet_unref, AVPacket,
};
pub struct Packet(pub *mut AVPacket);
impl Packet {
pub fn alloc() -> Packet {
let pkt = unsafe { av_packet_alloc() };
if pkt.is_null() {
panic!("Failed to allocate packet");
}
Packet(pkt)
}
#[inline]
pub fn stream(&self) -> i32 {
return unsafe { (*self.0).stream_index };
}
#[inline]
pub fn set_stream(&self, stream_index: i32) {
unsafe { (*self.0).stream_index = stream_index };
}
#[inline]
pub fn pts(&self) -> i64 {
unsafe { (*self.0).pts }
}
#[inline]
pub fn set_pts(&self, pts: i64) {
unsafe { (*self.0).pts = pts }
}
#[inline]
pub fn dts(&self) -> i64 {
unsafe { (*self.0).dts }
}
#[inline]
pub fn set_dts(&self, dts: i64) {
unsafe { (*self.0).dts = dts }
}
#[inline]
pub fn rescale<R1: Rational, R2: Rational>(&self, from: R1, to: R2) {
unsafe {
av_packet_rescale_ts(self.0, from.to_av(), to.to_av());
}
}
#[inline]
pub fn duration(&self) -> i64 {
unsafe { (*self.0).duration }
}
#[inline]
pub fn set_duration(&self, duration: i64) {
unsafe { (*self.0).duration = duration }
}
pub fn extra_data(&self) -> Vec<u8> {
unsafe {
let mut size = 0;
let data = av_packet_get_side_data(self.as_ptr(), AV_PKT_DATA_NEW_EXTRADATA, &mut size);
let size = size as usize;
let mut extra_data = vec![0u8; size];
data.copy_to(extra_data.as_mut_ptr(), size);
extra_data
}
}
pub fn data(&self) -> Vec<u8> {
unsafe {
let size = (*self.0).size as usize;
let mut data = vec![0u8; size];
(*self.0).data.copy_to(data.as_mut_ptr(), size);
data
}
}
#[inline]
pub fn as_ptr(&self) -> *const AVPacket {
return self.0;
}
#[inline]
pub fn as_mut_ptr(&self) -> *mut AVPacket {
return self.0;
}
}
impl Drop for Packet {
fn drop(&mut self) {
unsafe {
av_packet_unref(self.0);
}
}
}