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

@ -2,17 +2,10 @@ use std::ptr;
use libc::{c_int};
use ffi::*;
use ::{Error, Picture};
use ::{Error, frame};
use ::util::format;
use super::Flags;
pub struct Context {
pub ptr: *mut SwsContext,
input: Definition,
output: Definition,
}
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub struct Definition {
pub format: format::Pixel,
@ -20,15 +13,33 @@ pub struct Definition {
pub height: u32,
}
pub struct Context {
ptr: *mut SwsContext,
input: Definition,
output: Definition,
}
impl Context {
pub unsafe fn as_ptr(&self) -> *const SwsContext {
self.ptr as *const _
}
pub unsafe fn as_mut_ptr(&mut self) -> *mut SwsContext {
self.ptr
}
}
impl Context {
pub fn get(src_format: format::Pixel, src_w: u32, src_h: u32,
dst_format: format::Pixel, dst_w: u32, dst_h: u32,
flags: Flags) -> Result<Self, Error> {
unsafe {
let ptr = sws_getContext(src_w as c_int, src_h as c_int, src_format.into(),
dst_w as c_int, dst_h as c_int, dst_format.into(),
flags.bits(),
ptr::null_mut(), ptr::null_mut(), ptr::null_mut());
let ptr = sws_getContext(
src_w as c_int, src_h as c_int, src_format.into(),
dst_w as c_int, dst_h as c_int, dst_format.into(),
flags.bits(),
ptr::null_mut(), ptr::null_mut(), ptr::null_mut());
if ptr != ptr::null_mut() {
Ok(Context {
@ -70,7 +81,7 @@ impl Context {
};
unsafe {
self.ptr = sws_getCachedContext(self.ptr,
self.ptr = sws_getCachedContext(self.as_mut_ptr(),
src_w as c_int, src_h as c_int, src_format.into(),
dst_w as c_int, dst_h as c_int, dst_format.into(),
flags.bits(), ptr::null_mut(), ptr::null_mut(), ptr::null());
@ -85,7 +96,7 @@ impl Context {
&self.output
}
pub fn run(&self, input: &Picture, output: &mut Picture) -> Result<(), Error> {
pub fn run(&mut self, input: &frame::Video, output: &mut frame::Video) -> Result<(), Error> {
if input.format() != self.input.format || input.width() != self.input.width || input.height() != self.input.height {
return Err(Error::InputChanged);
}
@ -95,10 +106,10 @@ impl Context {
}
unsafe {
sws_scale(self.ptr,
(*input.ptr).data.as_ptr() as *const *const _, (*input.ptr).linesize.as_ptr() as *const _,
sws_scale(self.as_mut_ptr(),
(*input.as_ptr()).data.as_ptr() as *const *const _, (*input.as_ptr()).linesize.as_ptr() as *const _,
0, self.output.height as c_int,
(*output.ptr).data.as_ptr() as *mut *mut _, (*output.ptr).linesize.as_ptr() as *mut _);
(*output.as_mut_ptr()).data.as_ptr() as *mut *mut _, (*output.as_mut_ptr()).linesize.as_ptr() as *mut _);
}
Ok(())
@ -108,7 +119,7 @@ impl Context {
impl Drop for Context {
fn drop(&mut self) {
unsafe {
sws_freeContext(self.ptr);
sws_freeContext(self.as_mut_ptr());
}
}
}

View File

@ -2,7 +2,17 @@ use ffi::*;
use super::Vector;
pub struct Filter {
pub ptr: *mut SwsFilter,
ptr: *mut SwsFilter,
}
impl Filter {
pub unsafe fn as_ptr(&self) -> *const SwsFilter {
self.ptr as *const _
}
pub unsafe fn as_mut_ptr(&mut self) -> *mut SwsFilter {
self.ptr
}
}
impl Filter {
@ -24,49 +34,49 @@ impl Filter {
pub fn luma_horizontal(&self) -> Vector {
unsafe {
Vector::wrap((*self.ptr).lumH)
Vector::wrap((*self.as_ptr()).lumH)
}
}
pub fn luma_horizontal_mut(&mut self) -> Vector {
unsafe {
Vector::wrap((*self.ptr).lumH)
Vector::wrap((*self.as_mut_ptr()).lumH)
}
}
pub fn luma_vertical(&self) -> Vector {
unsafe {
Vector::wrap((*self.ptr).lumV)
Vector::wrap((*self.as_ptr()).lumV)
}
}
pub fn luma_vertical_mut(&mut self) -> Vector {
unsafe {
Vector::wrap((*self.ptr).lumV)
Vector::wrap((*self.as_mut_ptr()).lumV)
}
}
pub fn chroma_horizontal(&self) -> Vector {
unsafe {
Vector::wrap((*self.ptr).lumV)
Vector::wrap((*self.as_ptr()).lumV)
}
}
pub fn chroma_horizontal_mut(&mut self) -> Vector {
unsafe {
Vector::wrap((*self.ptr).lumV)
Vector::wrap((*self.as_mut_ptr()).lumV)
}
}
pub fn chroma_vertical(&self) -> Vector {
unsafe {
Vector::wrap((*self.ptr).lumV)
Vector::wrap((*self.as_ptr()).lumV)
}
}
pub fn chroma_vertical_mut(&mut self) -> Vector {
unsafe {
Vector::wrap((*self.ptr).lumV)
Vector::wrap((*self.as_mut_ptr()).lumV)
}
}
}
@ -74,7 +84,7 @@ impl Filter {
impl Drop for Filter {
fn drop(&mut self) {
unsafe {
sws_freeFilter(self.ptr);
sws_freeFilter(self.as_mut_ptr());
}
}
}

View File

@ -5,17 +5,27 @@ use libc::{c_int, c_double};
use ffi::*;
pub struct Vector<'a> {
pub ptr: *mut SwsVector,
ptr: *mut SwsVector,
_own: bool,
_marker: PhantomData<&'a ()>,
}
impl<'a> Vector<'a> {
pub fn wrap(ptr: *mut SwsVector) -> Self {
pub unsafe fn wrap(ptr: *mut SwsVector) -> Self {
Vector { ptr: ptr, _own: false, _marker: PhantomData }
}
pub unsafe fn as_ptr(&self) -> *const SwsVector {
self.ptr as *const _
}
pub unsafe fn as_mut_ptr(&mut self) -> *mut SwsVector {
self.ptr
}
}
impl<'a> Vector<'a> {
pub fn new(length: usize) -> Self {
unsafe {
Vector { ptr: sws_allocVec(length as c_int), _own: true, _marker: PhantomData }
@ -42,49 +52,49 @@ impl<'a> Vector<'a> {
pub fn scale(&mut self, scalar: f64) {
unsafe {
sws_scaleVec(self.ptr, scalar as c_double);
sws_scaleVec(self.as_mut_ptr(), scalar as c_double);
}
}
pub fn normalize(&mut self, height: f64) {
unsafe {
sws_normalizeVec(self.ptr, height as c_double);
sws_normalizeVec(self.as_mut_ptr(), height as c_double);
}
}
pub fn conv(&mut self, other: &Vector) {
unsafe {
sws_convVec(self.ptr, other.ptr);
sws_convVec(self.as_mut_ptr(), other.as_ptr());
}
}
pub fn add(&mut self, other: &Vector) {
unsafe {
sws_addVec(self.ptr, other.ptr);
sws_addVec(self.as_mut_ptr(), other.as_ptr());
}
}
pub fn sub(&mut self, other: &Vector) {
unsafe {
sws_subVec(self.ptr, other.ptr);
sws_subVec(self.as_mut_ptr(), other.as_ptr());
}
}
pub fn shift(&mut self, value: usize) {
unsafe {
sws_shiftVec(self.ptr, value as c_int);
sws_shiftVec(self.as_mut_ptr(), value as c_int);
}
}
pub fn coefficients(&self) -> &[f64] {
unsafe {
slice::from_raw_parts((*self.ptr).coeff, (*self.ptr).length as usize)
slice::from_raw_parts((*self.as_ptr()).coeff, (*self.as_ptr()).length as usize)
}
}
pub fn coefficients_mut(&self) -> &[f64] {
unsafe {
slice::from_raw_parts_mut((*self.ptr).coeff, (*self.ptr).length as usize)
slice::from_raw_parts_mut((*self.as_ptr()).coeff, (*self.as_ptr()).length as usize)
}
}
}
@ -92,7 +102,7 @@ impl<'a> Vector<'a> {
impl<'a> Clone for Vector<'a> {
fn clone(&self) -> Self {
unsafe {
Vector { ptr: sws_cloneVec(self.ptr), _own: true, _marker: PhantomData }
Vector { ptr: sws_cloneVec(self.as_ptr()), _own: true, _marker: PhantomData }
}
}
}
@ -101,7 +111,7 @@ impl<'a> Drop for Vector<'a> {
fn drop(&mut self) {
unsafe {
if self._own {
sws_freeVec(self.ptr);
sws_freeVec(self.as_mut_ptr());
}
}
}