*: 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

@ -11,22 +11,28 @@ use super::Frame;
#[derive(PartialEq, Eq)]
pub struct Audio(Frame);
impl Audio {
pub unsafe fn wrap(ptr: *mut AVFrame) -> Self {
Audio(Frame::wrap(ptr))
}
}
impl Audio {
pub fn empty() -> Self {
unsafe {
Audio(Frame { ptr: av_frame_alloc() })
Audio(Frame::empty())
}
}
pub fn new(format: format::Sample, samples: usize, layout: ChannelLayout) -> Self {
pub fn new(format: format::Sample, length: usize, layout: ChannelLayout) -> Self {
unsafe {
let mut frame = Audio::empty();
frame.set_format(format);
frame.set_sample_number(samples);
frame.set_length(length);
frame.set_channel_layout(layout);
av_frame_get_buffer(frame.ptr, 1);
av_frame_get_buffer(frame.as_mut_ptr(), 1);
frame
}
@ -34,75 +40,79 @@ impl Audio {
pub fn format(&self) -> format::Sample {
unsafe {
if (*self.ptr).format == -1 {
if (*self.as_ptr()).format == -1 {
format::Sample::None
}
else {
format::Sample::from(mem::transmute::<_, AVSampleFormat>(((*self.ptr).format)))
format::Sample::from(mem::transmute::<_, AVSampleFormat>(((*self.as_ptr()).format)))
}
}
}
pub fn set_format(&mut self, value: format::Sample) {
unsafe {
(*self.ptr).format = mem::transmute::<AVSampleFormat, c_int>(value.into());
(*self.as_mut_ptr()).format = mem::transmute::<AVSampleFormat, c_int>(value.into());
}
}
pub fn channel_layout(&self) -> ChannelLayout {
unsafe {
ChannelLayout::from_bits_truncate(av_frame_get_channel_layout(self.ptr) as c_ulonglong)
ChannelLayout::from_bits_truncate(av_frame_get_channel_layout(self.as_ptr()) as c_ulonglong)
}
}
pub fn set_channel_layout(&mut self, value: ChannelLayout) {
unsafe {
av_frame_set_channel_layout(self.ptr, value.bits() as int64_t);
av_frame_set_channel_layout(self.as_mut_ptr(), value.bits() as int64_t);
}
}
pub fn channels(&self) -> u16 {
unsafe {
av_frame_get_channels(self.ptr) as u16
av_frame_get_channels(self.as_ptr()) as u16
}
}
pub fn set_channels(&mut self, value: u16) {
unsafe {
av_frame_set_channels(self.ptr, value as c_int);
av_frame_set_channels(self.as_mut_ptr(), value as c_int);
}
}
pub fn rate(&self) -> u32 {
unsafe {
av_frame_get_sample_rate(self.ptr) as u32
av_frame_get_sample_rate(self.as_ptr()) as u32
}
}
pub fn set_rate(&mut self, value: u32) {
unsafe {
av_frame_set_sample_rate(self.ptr, value as c_int);
av_frame_set_sample_rate(self.as_mut_ptr(), value as c_int);
}
}
pub fn sample_number(&self) -> usize {
pub fn length(&self) -> usize {
unsafe {
(*self.ptr).nb_samples as usize
(*self.as_ptr()).nb_samples as usize
}
}
pub fn set_sample_number(&mut self, value: usize) {
pub fn set_length(&mut self, value: usize) {
unsafe {
(*self.ptr).nb_samples = value as c_int;
(*self.as_mut_ptr()).nb_samples = value as c_int;
}
}
pub fn samples(&self) -> Samples {
Samples::wrap(self.ptr as *mut AVPicture, self.format(), self.rate(), self.sample_number(), self.channels(), self.channel_layout())
unsafe {
Samples::wrap(self.as_ptr() as *mut AVPicture, self.format(), self.rate(), self.length(), self.channels(), self.channel_layout())
}
}
pub fn samples_mut(&mut self) -> Samples {
Samples::wrap(self.ptr as *mut AVPicture, self.format(), self.rate(), self.sample_number(), self.channels(), self.channel_layout())
unsafe {
Samples::wrap(self.as_ptr() as *mut AVPicture, self.format(), self.rate(), self.length(), self.channels(), self.channel_layout())
}
}
}
@ -124,7 +134,7 @@ impl DerefMut for Audio {
impl Clone for Audio {
fn clone(&self) -> Self {
let mut cloned = Audio::new(self.format(), self.sample_number(), self.channel_layout());
let mut cloned = Audio::new(self.format(), self.length(), self.channel_layout());
cloned.clone_from(self);
cloned
@ -132,8 +142,8 @@ impl Clone for Audio {
fn clone_from(&mut self, source: &Self) {
unsafe {
av_frame_copy(self.ptr, source.ptr);
av_frame_copy_props(self.ptr, source.ptr);
av_frame_copy(self.as_mut_ptr(), source.as_ptr());
av_frame_copy_props(self.as_mut_ptr(), source.as_ptr());
}
}
}

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());
}
}
}

View File

@ -67,31 +67,41 @@ impl Into<AVFrameSideDataType> for Type {
}
pub struct SideData<'a> {
pub ptr: *mut AVFrameSideData,
ptr: *mut AVFrameSideData,
_marker: PhantomData<&'a Frame>,
}
impl<'a> SideData<'a> {
pub fn wrap(ptr: *mut AVFrameSideData) -> Self {
pub unsafe fn wrap(ptr: *mut AVFrameSideData) -> Self {
SideData { ptr: ptr, _marker: PhantomData }
}
pub unsafe fn as_ptr(&self) -> *const AVFrameSideData {
self.ptr as *const _
}
pub unsafe fn as_mut_ptr(&mut self) -> *mut AVFrameSideData {
self.ptr
}
}
impl<'a> SideData<'a> {
pub fn kind(&self) -> Type {
unsafe {
Type::from((*self.ptr).kind)
Type::from((*self.as_ptr()).kind)
}
}
pub fn data(&self) -> &[u8] {
unsafe {
slice::from_raw_parts((*self.ptr).data, (*self.ptr).size as usize)
slice::from_raw_parts((*self.as_ptr()).data, (*self.as_ptr()).size as usize)
}
}
pub fn metadata(&self) -> Dictionary {
unsafe {
Dictionary::wrap((*self.ptr).metadata)
Dictionary::wrap((*self.as_ptr()).metadata)
}
}
}

View File

@ -3,7 +3,7 @@ use std::mem;
use std::ops::{Deref, DerefMut};
use ffi::*;
use ::{Rational, Picture};
use ::Rational;
use ::util::format;
use ::util::chroma;
use ::picture;
@ -13,10 +13,16 @@ use super::Frame;
#[derive(PartialEq, Eq)]
pub struct Video(Frame);
impl Video {
pub unsafe fn wrap(ptr: *mut AVFrame) -> Self {
Video(Frame::wrap(ptr))
}
}
impl Video {
pub fn empty() -> Self {
unsafe {
Video(Frame { ptr: av_frame_alloc() })
Video(Frame::empty())
}
}
@ -28,160 +34,152 @@ impl Video {
frame.set_width(width);
frame.set_height(height);
av_frame_get_buffer(frame.ptr, 1);
av_frame_get_buffer(frame.as_mut_ptr(), 1);
frame
}
}
pub fn picture(&self) -> Picture {
Picture::wrap(self.ptr as *mut AVPicture, self.format(), self.width(), self.height())
}
pub fn picture_mut(&mut self) -> Picture {
Picture::wrap(self.ptr as *mut AVPicture, self.format(), self.width(), self.height())
}
pub fn format(&self) -> format::Pixel {
unsafe {
if (*self.ptr).format == -1 {
if (*self.as_ptr()).format == -1 {
format::Pixel::None
}
else {
format::Pixel::from(mem::transmute::<_, AVPixelFormat>(((*self.ptr).format)))
format::Pixel::from(mem::transmute::<_, AVPixelFormat>(((*self.as_ptr()).format)))
}
}
}
pub fn set_format(&mut self, value: format::Pixel) {
unsafe {
(*self.ptr).format = mem::transmute::<AVPixelFormat, c_int>(value.into());
(*self.as_mut_ptr()).format = mem::transmute::<AVPixelFormat, c_int>(value.into());
}
}
pub fn kind(&self) -> picture::Type {
unsafe {
picture::Type::from((*self.ptr).pict_type)
picture::Type::from((*self.as_ptr()).pict_type)
}
}
pub fn is_interlaced(&self) -> bool {
unsafe {
(*self.ptr).interlaced_frame != 0
(*self.as_ptr()).interlaced_frame != 0
}
}
pub fn is_top_first(&self) -> bool {
unsafe {
(*self.ptr).top_field_first != 0
(*self.as_ptr()).top_field_first != 0
}
}
pub fn has_palette_changed(&self) -> bool {
unsafe {
(*self.ptr).palette_has_changed != 0
(*self.as_ptr()).palette_has_changed != 0
}
}
pub fn width(&self) -> u32 {
unsafe {
(*self.ptr).width as u32
(*self.as_ptr()).width as u32
}
}
pub fn set_width(&mut self, value: u32) {
unsafe {
(*self.ptr).width = value as c_int;
(*self.as_mut_ptr()).width = value as c_int;
}
}
pub fn height(&self) -> u32 {
unsafe {
(*self.ptr).height as u32
(*self.as_ptr()).height as u32
}
}
pub fn set_height(&mut self, value: u32) {
unsafe {
(*self.ptr).height = value as c_int;
(*self.as_mut_ptr()).height = value as c_int;
}
}
pub fn color_space(&self) -> color::Space {
unsafe {
color::Space::from(av_frame_get_colorspace(self.ptr))
color::Space::from(av_frame_get_colorspace(self.as_ptr()))
}
}
pub fn set_color_space(&mut self, value: color::Space) {
unsafe {
av_frame_set_colorspace(self.ptr, value.into());
av_frame_set_colorspace(self.as_mut_ptr(), value.into());
}
}
pub fn color_range(&self) -> color::Range {
unsafe {
color::Range::from(av_frame_get_color_range(self.ptr))
color::Range::from(av_frame_get_color_range(self.as_ptr()))
}
}
pub fn set_color_range(&mut self, value: color::Range) {
unsafe {
av_frame_set_color_range(self.ptr, value.into());
av_frame_set_color_range(self.as_mut_ptr(), value.into());
}
}
pub fn color_primaries(&self) -> color::Primaries {
unsafe {
color::Primaries::from((*self.ptr).color_primaries)
color::Primaries::from((*self.as_ptr()).color_primaries)
}
}
pub fn set_color_primaries(&mut self, value: color::Primaries) {
unsafe {
(*self.ptr).color_primaries = value.into();
(*self.as_mut_ptr()).color_primaries = value.into();
}
}
pub fn color_transfer_characteristic(&self) -> color::TransferCharacteristic {
unsafe {
color::TransferCharacteristic::from((*self.ptr).color_trc)
color::TransferCharacteristic::from((*self.as_ptr()).color_trc)
}
}
pub fn set_color_transfer_characteristic(&mut self, value: color::TransferCharacteristic) {
unsafe {
(*self.ptr).color_trc = value.into();
(*self.as_mut_ptr()).color_trc = value.into();
}
}
pub fn chroma_location(&self) -> chroma::Location {
unsafe {
chroma::Location::from((*self.ptr).chroma_location)
chroma::Location::from((*self.as_ptr()).chroma_location)
}
}
pub fn aspect_ratio(&self) -> Rational {
unsafe {
Rational((*self.ptr).sample_aspect_ratio)
Rational((*self.as_ptr()).sample_aspect_ratio)
}
}
pub fn coded_number(&self) -> usize {
unsafe {
(*self.ptr).coded_picture_number as usize
(*self.as_ptr()).coded_picture_number as usize
}
}
pub fn display_number(&self) -> usize {
unsafe {
(*self.ptr).display_picture_number as usize
(*self.as_ptr()).display_picture_number as usize
}
}
pub fn repeat(&self) -> f64 {
unsafe {
(*self.ptr).repeat_pict as f64
(*self.as_ptr()).repeat_pict as f64
}
}
}
@ -212,8 +210,8 @@ impl Clone for Video {
fn clone_from(&mut self, source: &Self) {
unsafe {
av_frame_copy(self.ptr, source.ptr);
av_frame_copy_props(self.ptr, source.ptr);
av_frame_copy(self.as_mut_ptr(), source.as_ptr());
av_frame_copy_props(self.as_mut_ptr(), source.as_ptr());
}
}
}