ffmpeg_the_third/util/option/
traits.rs

1//! NOTE: this will be much better once specialization comes
2
3use std::ffi::CString;
4use std::mem;
5
6use crate::ffi::*;
7use crate::util::format;
8use crate::{AsMutPtr, AsPtr, Error, Rational};
9use libc::c_int;
10
11#[cfg(not(feature = "ffmpeg_7_0"))]
12use crate::ChannelLayoutMask;
13
14macro_rules! check {
15    ($expr:expr) => {
16        match $expr {
17            0 => Ok(()),
18            e => Err(Error::from(e)),
19        }
20    };
21}
22
23pub trait Settable<T>: AsPtr<T> + AsMutPtr<T> {
24    fn set<V: 'static>(&mut self, name: &str, value: &V) -> Result<(), Error> {
25        unsafe {
26            let name = CString::new(name).unwrap();
27
28            check!(av_opt_set_bin(
29                self.as_mut_ptr() as *mut _,
30                name.as_ptr(),
31                value as *const _ as *const _,
32                mem::size_of::<V>() as c_int,
33                AV_OPT_SEARCH_CHILDREN
34            ))
35        }
36    }
37
38    fn set_str(&mut self, name: &str, value: &str) -> Result<(), Error> {
39        unsafe {
40            let name = CString::new(name).unwrap();
41            let value = CString::new(value).unwrap();
42
43            check!(av_opt_set(
44                self.as_mut_ptr() as *mut _,
45                name.as_ptr(),
46                value.as_ptr(),
47                AV_OPT_SEARCH_CHILDREN
48            ))
49        }
50    }
51
52    fn set_int(&mut self, name: &str, value: i64) -> Result<(), Error> {
53        unsafe {
54            let name = CString::new(name).unwrap();
55
56            check!(av_opt_set_int(
57                self.as_mut_ptr() as *mut _,
58                name.as_ptr(),
59                value,
60                AV_OPT_SEARCH_CHILDREN
61            ))
62        }
63    }
64
65    fn set_double(&mut self, name: &str, value: f64) -> Result<(), Error> {
66        unsafe {
67            let name = CString::new(name).unwrap();
68
69            check!(av_opt_set_double(
70                self.as_mut_ptr() as *mut _,
71                name.as_ptr(),
72                value,
73                AV_OPT_SEARCH_CHILDREN
74            ))
75        }
76    }
77
78    fn set_rational<V: Into<Rational>>(&mut self, name: &str, value: V) -> Result<(), Error> {
79        unsafe {
80            let name = CString::new(name).unwrap();
81
82            check!(av_opt_set_q(
83                self.as_mut_ptr() as *mut _,
84                name.as_ptr(),
85                value.into().into(),
86                AV_OPT_SEARCH_CHILDREN
87            ))
88        }
89    }
90
91    fn set_image_size(&mut self, name: &str, w: u32, h: u32) -> Result<(), Error> {
92        unsafe {
93            let name = CString::new(name).unwrap();
94
95            check!(av_opt_set_image_size(
96                self.as_mut_ptr() as *mut _,
97                name.as_ptr(),
98                w as c_int,
99                h as c_int,
100                AV_OPT_SEARCH_CHILDREN
101            ))
102        }
103    }
104
105    fn set_pixel_format(&mut self, name: &str, format: format::Pixel) -> Result<(), Error> {
106        unsafe {
107            let name = CString::new(name).unwrap();
108
109            check!(av_opt_set_pixel_fmt(
110                self.as_mut_ptr() as *mut _,
111                name.as_ptr(),
112                format.into(),
113                AV_OPT_SEARCH_CHILDREN
114            ))
115        }
116    }
117
118    fn set_sample_format(&mut self, name: &str, format: format::Sample) -> Result<(), Error> {
119        unsafe {
120            let name = CString::new(name).unwrap();
121
122            check!(av_opt_set_sample_fmt(
123                self.as_mut_ptr() as *mut _,
124                name.as_ptr(),
125                format.into(),
126                AV_OPT_SEARCH_CHILDREN
127            ))
128        }
129    }
130
131    #[cfg(not(feature = "ffmpeg_7_0"))]
132    fn set_channel_layout(&mut self, name: &str, layout: ChannelLayoutMask) -> Result<(), Error> {
133        unsafe {
134            let name = CString::new(name).unwrap();
135
136            check!(av_opt_set_channel_layout(
137                self.as_mut_ptr() as *mut _,
138                name.as_ptr(),
139                layout.bits() as i64,
140                AV_OPT_SEARCH_CHILDREN
141            ))
142        }
143    }
144}
145
146pub trait Gettable<T>: AsPtr<T> + AsMutPtr<T> {}
147
148pub trait Iterable<T>: AsPtr<T> + AsMutPtr<T> {}