*: make internal pointer handling safer

This commit is contained in:
meh
2015-06-04 03:03:19 +02:00
parent b2c9dc3747
commit ff1b880be6
28 changed files with 683 additions and 466 deletions

View File

@ -28,13 +28,33 @@ pub struct Packet {
#[derive(PartialEq, Eq)]
pub struct Frame {
pub ptr: *mut AVFrame,
ptr: *mut AVFrame,
_own: bool,
}
impl Frame {
pub unsafe fn wrap(ptr: *mut AVFrame) -> Self {
Frame { ptr: ptr, _own: false }
}
pub unsafe fn empty() -> Self {
Frame { ptr: av_frame_alloc(), _own: true }
}
pub unsafe fn as_ptr(&self) -> *const AVFrame {
self.ptr as *const _
}
pub unsafe fn as_mut_ptr(&mut self) -> *mut AVFrame {
self.ptr
}
}
impl Frame {
pub fn is_key(&self) -> bool {
unsafe {
(*self.ptr).key_frame == 1
(*self.as_ptr()).key_frame == 1
}
}
@ -45,25 +65,25 @@ impl Frame {
pub fn packet(&self) -> Packet {
unsafe {
Packet {
duration: av_frame_get_pkt_duration(self.ptr) as i64,
position: av_frame_get_pkt_pos(self.ptr) as i64,
size: av_frame_get_pkt_size(self.ptr) as usize,
duration: av_frame_get_pkt_duration(self.as_ptr()) as i64,
position: av_frame_get_pkt_pos(self.as_ptr()) as i64,
size: av_frame_get_pkt_size(self.as_ptr()) as usize,
pts: (*self.ptr).pkt_pts,
dts: (*self.ptr).pkt_dts,
pts: (*self.as_ptr()).pkt_pts,
dts: (*self.as_ptr()).pkt_dts,
}
}
}
pub fn pts(&self) -> i64 {
unsafe {
(*self.ptr).pts as i64
(*self.as_ptr()).pts as i64
}
}
pub fn timestamp(&self) -> Option<i64> {
unsafe {
match av_frame_get_best_effort_timestamp(self.ptr) {
match av_frame_get_best_effort_timestamp(self.as_ptr()) {
AV_NOPTS_VALUE => None,
t => Some(t as i64)
}
@ -72,31 +92,31 @@ impl Frame {
pub fn quality(&self) -> usize {
unsafe {
(*self.ptr).quality as usize
(*self.as_ptr()).quality as usize
}
}
pub fn flags(&self) -> Flags {
unsafe {
Flags::from_bits_truncate((*self.ptr).flags)
Flags::from_bits_truncate((*self.as_ptr()).flags)
}
}
pub fn metadata(&self) -> Dictionary {
unsafe {
Dictionary::wrap(av_frame_get_metadata(self.ptr))
Dictionary::wrap(av_frame_get_metadata(self.as_ptr()))
}
}
pub fn set_metadata(&mut self, mut value: Dictionary) {
pub fn set_metadata(&mut self, value: Dictionary) {
unsafe {
av_frame_set_metadata(self.ptr, value.take());
av_frame_set_metadata(self.as_mut_ptr(), value.take());
}
}
pub fn side_data(&self, kind: side_data::Type) -> Option<SideData> {
unsafe {
let ptr = av_frame_get_side_data(self.ptr, kind.into());
let ptr = av_frame_get_side_data(self.as_ptr(), kind.into());
if ptr == ptr::null_mut() {
None
@ -109,7 +129,7 @@ impl Frame {
pub fn new_side_data(&mut self, kind: side_data::Type, size: usize) -> Option<SideData> {
unsafe {
let ptr = av_frame_new_side_data(self.ptr, kind.into(), size as c_int);
let ptr = av_frame_new_side_data(self.as_mut_ptr(), kind.into(), size as c_int);
if ptr == ptr::null_mut() {
None
@ -122,7 +142,7 @@ impl Frame {
pub fn remove_side_data(&mut self, kind: side_data::Type) {
unsafe {
av_frame_remove_side_data(self.ptr, kind.into());
av_frame_remove_side_data(self.as_mut_ptr(), kind.into());
}
}
}
@ -132,7 +152,7 @@ unsafe impl Send for Frame { }
impl Drop for Frame {
fn drop(&mut self) {
unsafe {
av_frame_free(&mut self.ptr);
av_frame_free(&mut self.as_mut_ptr());
}
}
}