| 1 | use std::marker::PhantomData; |
| 2 | use std::slice; |
| 3 | |
| 4 | use ffi::*; |
| 5 | use libc::{c_double, c_int}; |
| 6 | |
| 7 | pub struct Vector<'a> { |
| 8 | ptr: *mut SwsVector, |
| 9 | |
| 10 | _own: bool, |
| 11 | _marker: PhantomData<&'a ()>, |
| 12 | } |
| 13 | |
| 14 | impl<'a> Vector<'a> { |
| 15 | pub unsafe fn wrap(ptr: *mut SwsVector) -> Self { |
| 16 | Vector { |
| 17 | ptr, |
| 18 | _own: false, |
| 19 | _marker: PhantomData, |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | pub unsafe fn as_ptr(&self) -> *const SwsVector { |
| 24 | self.ptr as *const _ |
| 25 | } |
| 26 | |
| 27 | pub unsafe fn as_mut_ptr(&mut self) -> *mut SwsVector { |
| 28 | self.ptr |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl<'a> Vector<'a> { |
| 33 | pub fn new(length: usize) -> Self { |
| 34 | unsafe { |
| 35 | Vector { |
| 36 | ptr: sws_allocVec(length as c_int), |
| 37 | _own: true, |
| 38 | _marker: PhantomData, |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub fn gaussian(variance: f64, quality: f64) -> Self { |
| 44 | unsafe { |
| 45 | Vector { |
| 46 | ptr: sws_getGaussianVec(variance as c_double, quality as c_double), |
| 47 | _own: true, |
| 48 | _marker: PhantomData, |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | #[cfg (not(feature = "ffmpeg_5_0" ))] |
| 54 | pub fn value(value: f64, length: usize) -> Self { |
| 55 | unsafe { |
| 56 | Vector { |
| 57 | ptr: sws_getConstVec(value as c_double, length as c_int), |
| 58 | _own: true, |
| 59 | _marker: PhantomData, |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | #[cfg (not(feature = "ffmpeg_5_0" ))] |
| 65 | pub fn identity() -> Self { |
| 66 | unsafe { |
| 67 | Vector { |
| 68 | ptr: sws_getIdentityVec(), |
| 69 | _own: true, |
| 70 | _marker: PhantomData, |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | pub fn scale(&mut self, scalar: f64) { |
| 76 | unsafe { |
| 77 | sws_scaleVec(self.as_mut_ptr(), scalar as c_double); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | pub fn normalize(&mut self, height: f64) { |
| 82 | unsafe { |
| 83 | sws_normalizeVec(self.as_mut_ptr(), height as c_double); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | #[cfg (not(feature = "ffmpeg_5_0" ))] |
| 88 | pub fn conv(&mut self, other: &Vector) { |
| 89 | unsafe { |
| 90 | sws_convVec(self.as_mut_ptr(), other.as_ptr() as *mut _); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | #[cfg (not(feature = "ffmpeg_5_0" ))] |
| 95 | pub fn add(&mut self, other: &Vector) { |
| 96 | unsafe { |
| 97 | sws_addVec(self.as_mut_ptr(), other.as_ptr() as *mut _); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | #[cfg (not(feature = "ffmpeg_5_0" ))] |
| 102 | pub fn sub(&mut self, other: &Vector) { |
| 103 | unsafe { |
| 104 | sws_subVec(self.as_mut_ptr(), other.as_ptr() as *mut _); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | #[cfg (not(feature = "ffmpeg_5_0" ))] |
| 109 | pub fn shift(&mut self, value: usize) { |
| 110 | unsafe { |
| 111 | sws_shiftVec(self.as_mut_ptr(), value as c_int); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | pub fn coefficients(&self) -> &[f64] { |
| 116 | unsafe { slice::from_raw_parts((*self.as_ptr()).coeff, (*self.as_ptr()).length as usize) } |
| 117 | } |
| 118 | |
| 119 | pub fn coefficients_mut(&self) -> &[f64] { |
| 120 | unsafe { |
| 121 | slice::from_raw_parts_mut((*self.as_ptr()).coeff, (*self.as_ptr()).length as usize) |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | #[cfg (not(feature = "ffmpeg_5_0" ))] |
| 127 | impl<'a> Clone for Vector<'a> { |
| 128 | fn clone(&self) -> Self { |
| 129 | unsafe { |
| 130 | Vector { |
| 131 | ptr: sws_cloneVec(self.as_ptr() as *mut _), |
| 132 | _own: true, |
| 133 | _marker: PhantomData, |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | impl<'a> Drop for Vector<'a> { |
| 140 | fn drop(&mut self) { |
| 141 | unsafe { |
| 142 | if self._own { |
| 143 | sws_freeVec(self.as_mut_ptr()); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |