playa_ffmpeg/software/scaling/
vector.rs

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