ffmpeg_the_third/software/scaling/
vector.rs1use std::marker::PhantomData;
2use std::slice;
3
4use crate::ffi::*;
5use libc::{c_double, c_int};
6
7pub struct Vector<'a> {
8 ptr: *mut SwsVector,
9
10 _own: bool,
11 _marker: PhantomData<&'a ()>,
12}
13
14impl<'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
32impl<'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 pub fn scale(&mut self, scalar: f64) {
54 unsafe {
55 sws_scaleVec(self.as_mut_ptr(), scalar as c_double);
56 }
57 }
58
59 pub fn normalize(&mut self, height: f64) {
60 unsafe {
61 sws_normalizeVec(self.as_mut_ptr(), height as c_double);
62 }
63 }
64
65 pub fn coefficients(&self) -> &[f64] {
66 unsafe { slice::from_raw_parts((*self.as_ptr()).coeff, (*self.as_ptr()).length as usize) }
67 }
68
69 pub fn coefficients_mut(&self) -> &[f64] {
70 unsafe {
71 slice::from_raw_parts_mut((*self.as_ptr()).coeff, (*self.as_ptr()).length as usize)
72 }
73 }
74}
75
76impl<'a> Drop for Vector<'a> {
77 fn drop(&mut self) {
78 unsafe {
79 if self._own {
80 sws_freeVec(self.as_mut_ptr());
81 }
82 }
83 }
84}