1 | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | |
3 | use std::{ops, ptr}; |
4 | |
5 | use glib::translate::*; |
6 | |
7 | #[derive (Debug)] |
8 | #[doc (alias = "GstVideoConverter" )] |
9 | pub struct VideoConverter(ptr::NonNull<ffi::GstVideoConverter>); |
10 | |
11 | impl Drop for VideoConverter { |
12 | #[inline ] |
13 | fn drop(&mut self) { |
14 | unsafe { |
15 | ffi::gst_video_converter_free(self.0.as_ptr()); |
16 | } |
17 | } |
18 | } |
19 | |
20 | unsafe impl Send for VideoConverter {} |
21 | unsafe impl Sync for VideoConverter {} |
22 | |
23 | impl VideoConverter { |
24 | #[doc (alias = "gst_video_converter_new" )] |
25 | pub fn new( |
26 | in_info: &crate::VideoInfo, |
27 | out_info: &crate::VideoInfo, |
28 | config: Option<VideoConverterConfig>, |
29 | ) -> Result<Self, glib::BoolError> { |
30 | skip_assert_initialized!(); |
31 | if in_info.fps() != out_info.fps() { |
32 | return Err(glib::bool_error!("Can't do framerate conversion" )); |
33 | } |
34 | |
35 | if in_info.interlace_mode() != out_info.interlace_mode() { |
36 | return Err(glib::bool_error!("Can't do interlacing conversion" )); |
37 | } |
38 | |
39 | unsafe { |
40 | let ptr = ffi::gst_video_converter_new( |
41 | in_info.to_glib_none().0 as *mut _, |
42 | out_info.to_glib_none().0 as *mut _, |
43 | config |
44 | .map(|s| s.0.into_glib_ptr()) |
45 | .unwrap_or(ptr::null_mut()), |
46 | ); |
47 | if ptr.is_null() { |
48 | Err(glib::bool_error!("Failed to create video converter" )) |
49 | } else { |
50 | Ok(Self(ptr::NonNull::new_unchecked(ptr))) |
51 | } |
52 | } |
53 | } |
54 | |
55 | #[doc (alias = "get_config" )] |
56 | #[doc (alias = "gst_video_converter_get_config" )] |
57 | pub fn config(&self) -> VideoConverterConfig { |
58 | unsafe { |
59 | VideoConverterConfig( |
60 | gst::StructureRef::from_glib_borrow(ffi::gst_video_converter_get_config( |
61 | self.0.as_ptr(), |
62 | )) |
63 | .to_owned(), |
64 | ) |
65 | } |
66 | } |
67 | |
68 | #[doc (alias = "gst_video_converter_set_config" )] |
69 | pub fn set_config(&mut self, config: VideoConverterConfig) { |
70 | unsafe { |
71 | ffi::gst_video_converter_set_config(self.0.as_ptr(), config.0.into_glib_ptr()); |
72 | } |
73 | } |
74 | |
75 | #[cfg (feature = "v1_22" )] |
76 | #[cfg_attr (docsrs, doc(cfg(feature = "v1_22" )))] |
77 | #[doc (alias = "get_in_info" )] |
78 | #[doc (alias = "gst_video_converter_get_in_info" )] |
79 | pub fn in_info(&self) -> crate::VideoInfo { |
80 | unsafe { from_glib_none(ffi::gst_video_converter_get_in_info(self.0.as_ptr())) } |
81 | } |
82 | |
83 | #[cfg (feature = "v1_22" )] |
84 | #[cfg_attr (docsrs, doc(cfg(feature = "v1_22" )))] |
85 | #[doc (alias = "get_out_info" )] |
86 | #[doc (alias = "gst_video_converter_get_out_info" )] |
87 | pub fn out_info(&self) -> crate::VideoInfo { |
88 | unsafe { from_glib_none(ffi::gst_video_converter_get_out_info(self.0.as_ptr())) } |
89 | } |
90 | |
91 | #[doc (alias = "gst_video_converter_frame" )] |
92 | pub fn frame<T>( |
93 | &self, |
94 | src: &crate::VideoFrame<T>, |
95 | dest: &mut crate::VideoFrame<crate::video_frame::Writable>, |
96 | ) { |
97 | unsafe { |
98 | ffi::gst_video_converter_frame(self.0.as_ptr(), src.as_ptr(), dest.as_mut_ptr()); |
99 | } |
100 | } |
101 | |
102 | pub fn frame_ref<T>( |
103 | &self, |
104 | src: &crate::VideoFrameRef<T>, |
105 | dest: &mut crate::VideoFrameRef<&mut gst::BufferRef>, |
106 | ) { |
107 | unsafe { |
108 | ffi::gst_video_converter_frame(self.0.as_ptr(), src.as_ptr(), dest.as_mut_ptr()); |
109 | } |
110 | } |
111 | } |
112 | |
113 | #[derive (Debug, Clone, PartialEq, Eq)] |
114 | pub struct VideoConverterConfig(gst::Structure); |
115 | |
116 | impl ops::Deref for VideoConverterConfig { |
117 | type Target = gst::StructureRef; |
118 | |
119 | #[inline ] |
120 | fn deref(&self) -> &gst::StructureRef { |
121 | self.0.deref() |
122 | } |
123 | } |
124 | |
125 | impl ops::DerefMut for VideoConverterConfig { |
126 | #[inline ] |
127 | fn deref_mut(&mut self) -> &mut gst::StructureRef { |
128 | self.0.deref_mut() |
129 | } |
130 | } |
131 | |
132 | impl AsRef<gst::StructureRef> for VideoConverterConfig { |
133 | #[inline ] |
134 | fn as_ref(&self) -> &gst::StructureRef { |
135 | self.0.as_ref() |
136 | } |
137 | } |
138 | |
139 | impl AsMut<gst::StructureRef> for VideoConverterConfig { |
140 | #[inline ] |
141 | fn as_mut(&mut self) -> &mut gst::StructureRef { |
142 | self.0.as_mut() |
143 | } |
144 | } |
145 | |
146 | impl Default for VideoConverterConfig { |
147 | fn default() -> Self { |
148 | Self::new() |
149 | } |
150 | } |
151 | |
152 | impl TryFrom<gst::Structure> for VideoConverterConfig { |
153 | type Error = glib::BoolError; |
154 | |
155 | fn try_from(v: gst::Structure) -> Result<Self, Self::Error> { |
156 | skip_assert_initialized!(); |
157 | if v.name() == "GstVideoConverter" { |
158 | Ok(Self(v)) |
159 | } else { |
160 | Err(glib::bool_error!("Structure is no VideoConverterConfig" )) |
161 | } |
162 | } |
163 | } |
164 | |
165 | impl<'a> TryFrom<&'a gst::StructureRef> for VideoConverterConfig { |
166 | type Error = glib::BoolError; |
167 | |
168 | fn try_from(v: &'a gst::StructureRef) -> Result<Self, Self::Error> { |
169 | skip_assert_initialized!(); |
170 | Self::try_from(v.to_owned()) |
171 | } |
172 | } |
173 | |
174 | impl From<VideoConverterConfig> for gst::Structure { |
175 | fn from(v: VideoConverterConfig) -> Self { |
176 | skip_assert_initialized!(); |
177 | v.0 |
178 | } |
179 | } |
180 | |
181 | impl VideoConverterConfig { |
182 | pub fn new() -> Self { |
183 | Self(gst::Structure::new_empty("GstVideoConverter" )) |
184 | } |
185 | |
186 | pub fn set_resampler_method(&mut self, v: crate::VideoResamplerMethod) { |
187 | self.0 |
188 | .set(glib::gstr!("GstVideoConverter.resampler-method" ), v); |
189 | } |
190 | |
191 | #[doc (alias = "get_resampler_method" )] |
192 | pub fn resampler_method(&self) -> crate::VideoResamplerMethod { |
193 | self.0 |
194 | .get_optional(glib::gstr!("GstVideoConverter.resampler-method" )) |
195 | .expect("Wrong type" ) |
196 | .unwrap_or(crate::VideoResamplerMethod::Cubic) |
197 | } |
198 | |
199 | pub fn set_chroma_resampler_method(&mut self, v: crate::VideoResamplerMethod) { |
200 | self.0 |
201 | .set(glib::gstr!("GstVideoConverter.chroma-resampler-method" ), v); |
202 | } |
203 | |
204 | #[doc (alias = "get_chroma_resampler_method" )] |
205 | pub fn chroma_resampler_method(&self) -> crate::VideoResamplerMethod { |
206 | self.0 |
207 | .get_optional(glib::gstr!("GstVideoConverter.chroma-resampler-method" )) |
208 | .expect("Wrong type" ) |
209 | .unwrap_or(crate::VideoResamplerMethod::Linear) |
210 | } |
211 | |
212 | pub fn set_resampler_taps(&mut self, v: u32) { |
213 | self.0 |
214 | .set(glib::gstr!("GstVideoConverter.resampler-taps" ), v); |
215 | } |
216 | |
217 | #[doc (alias = "get_resampler_taps" )] |
218 | pub fn resampler_taps(&self) -> u32 { |
219 | self.0 |
220 | .get_optional(glib::gstr!("GstVideoConverter.resampler-taps" )) |
221 | .expect("Wrong type" ) |
222 | .unwrap_or(0) |
223 | } |
224 | |
225 | pub fn set_dither_method(&mut self, v: crate::VideoDitherMethod) { |
226 | self.0 |
227 | .set(glib::gstr!("GstVideoConverter.dither-method" ), v); |
228 | } |
229 | |
230 | #[doc (alias = "get_dither_method" )] |
231 | pub fn dither_method(&self) -> crate::VideoDitherMethod { |
232 | self.0 |
233 | .get_optional(glib::gstr!("GstVideoConverter.dither-method" )) |
234 | .expect("Wrong type" ) |
235 | .unwrap_or(crate::VideoDitherMethod::Bayer) |
236 | } |
237 | |
238 | pub fn set_dither_quantization(&mut self, v: u32) { |
239 | self.0 |
240 | .set(glib::gstr!("GstVideoConverter.dither-quantization" ), v); |
241 | } |
242 | |
243 | #[doc (alias = "get_dither_quantization" )] |
244 | pub fn dither_quantization(&self) -> u32 { |
245 | self.0 |
246 | .get_optional(glib::gstr!("GstVideoConverter.dither-quantization" )) |
247 | .expect("Wrong type" ) |
248 | .unwrap_or(1) |
249 | } |
250 | |
251 | pub fn set_src_x(&mut self, v: i32) { |
252 | self.0.set(glib::gstr!("GstVideoConverter.src-x" ), v); |
253 | } |
254 | |
255 | #[doc (alias = "get_src_x" )] |
256 | pub fn src_x(&self) -> i32 { |
257 | self.0 |
258 | .get_optional(glib::gstr!("GstVideoConverter.src-x" )) |
259 | .expect("Wrong type" ) |
260 | .unwrap_or(0) |
261 | } |
262 | |
263 | pub fn set_src_y(&mut self, v: i32) { |
264 | self.0.set(glib::gstr!("GstVideoConverter.src-y" ), v); |
265 | } |
266 | |
267 | #[doc (alias = "get_src_y" )] |
268 | pub fn src_y(&self) -> i32 { |
269 | self.0 |
270 | .get_optional(glib::gstr!("GstVideoConverter.src-y" )) |
271 | .expect("Wrong type" ) |
272 | .unwrap_or(0) |
273 | } |
274 | |
275 | pub fn set_src_width(&mut self, v: Option<i32>) { |
276 | if let Some(v) = v { |
277 | self.0.set(glib::gstr!("GstVideoConverter.src-width" ), v); |
278 | } else { |
279 | self.0 |
280 | .remove_field(glib::gstr!("GstVideoConverter.src-width" )); |
281 | } |
282 | } |
283 | |
284 | #[doc (alias = "get_src_width" )] |
285 | pub fn src_width(&self) -> Option<i32> { |
286 | self.0 |
287 | .get_optional(glib::gstr!("GstVideoConverter.src-width" )) |
288 | .expect("Wrong type" ) |
289 | } |
290 | |
291 | pub fn set_src_height(&mut self, v: Option<i32>) { |
292 | if let Some(v) = v { |
293 | self.0.set(glib::gstr!("GstVideoConverter.src-height" ), v); |
294 | } else { |
295 | self.0 |
296 | .remove_field(glib::gstr!("GstVideoConverter.src-height" )); |
297 | } |
298 | } |
299 | |
300 | #[doc (alias = "get_src_height" )] |
301 | pub fn src_height(&self) -> Option<i32> { |
302 | self.0 |
303 | .get_optional(glib::gstr!("GstVideoConverter.src-height" )) |
304 | .expect("Wrong type" ) |
305 | } |
306 | |
307 | pub fn set_dest_x(&mut self, v: i32) { |
308 | self.0.set(glib::gstr!("GstVideoConverter.dest-x" ), v); |
309 | } |
310 | |
311 | #[doc (alias = "get_dest_x" )] |
312 | pub fn dest_x(&self) -> i32 { |
313 | self.0 |
314 | .get_optional(glib::gstr!("GstVideoConverter.dest-x" )) |
315 | .expect("Wrong type" ) |
316 | .unwrap_or(0) |
317 | } |
318 | |
319 | pub fn set_dest_y(&mut self, v: i32) { |
320 | self.0.set(glib::gstr!("GstVideoConverter.dest-y" ), v); |
321 | } |
322 | |
323 | #[doc (alias = "get_dest_y" )] |
324 | pub fn dest_y(&self) -> i32 { |
325 | self.0 |
326 | .get_optional(glib::gstr!("GstVideoConverter.dest-y" )) |
327 | .expect("Wrong type" ) |
328 | .unwrap_or(0) |
329 | } |
330 | |
331 | pub fn set_dest_width(&mut self, v: Option<i32>) { |
332 | if let Some(v) = v { |
333 | self.0.set(glib::gstr!("GstVideoConverter.dest-width" ), v); |
334 | } else { |
335 | self.0 |
336 | .remove_field(glib::gstr!("GstVideoConverter.dest-width" )); |
337 | } |
338 | } |
339 | |
340 | #[doc (alias = "get_dest_width" )] |
341 | pub fn dest_width(&self) -> Option<i32> { |
342 | self.0 |
343 | .get_optional(glib::gstr!("GstVideoConverter.dest-width" )) |
344 | .expect("Wrong type" ) |
345 | } |
346 | |
347 | pub fn set_dest_height(&mut self, v: Option<i32>) { |
348 | if let Some(v) = v { |
349 | self.0.set(glib::gstr!("GstVideoConverter.dest-height" ), v); |
350 | } else { |
351 | self.0 |
352 | .remove_field(glib::gstr!("GstVideoConverter.dest-height" )); |
353 | } |
354 | } |
355 | |
356 | #[doc (alias = "get_dest_height" )] |
357 | pub fn dest_height(&self) -> Option<i32> { |
358 | self.0 |
359 | .get_optional(glib::gstr!("GstVideoConverter.dest-height" )) |
360 | .expect("Wrong type" ) |
361 | } |
362 | |
363 | pub fn set_fill_border(&mut self, v: bool) { |
364 | self.0.set(glib::gstr!("GstVideoConverter.fill-border" ), v); |
365 | } |
366 | |
367 | #[doc (alias = "get_fill_border" )] |
368 | pub fn fills_border(&self) -> bool { |
369 | self.0 |
370 | .get_optional(glib::gstr!("GstVideoConverter.fill-border" )) |
371 | .expect("Wrong type" ) |
372 | .unwrap_or(true) |
373 | } |
374 | |
375 | pub fn set_alpha_value(&mut self, v: f64) { |
376 | self.0.set(glib::gstr!("GstVideoConverter.alpha-value" ), v); |
377 | } |
378 | |
379 | #[doc (alias = "get_alpha_value" )] |
380 | pub fn alpha_value(&self) -> f64 { |
381 | self.0 |
382 | .get_optional(glib::gstr!("GstVideoConverter.alpha-value" )) |
383 | .expect("Wrong type" ) |
384 | .unwrap_or(1.0) |
385 | } |
386 | |
387 | pub fn set_alpha_mode(&mut self, v: crate::VideoAlphaMode) { |
388 | self.0.set(glib::gstr!("GstVideoConverter.alpha-mode" ), v); |
389 | } |
390 | |
391 | #[doc (alias = "get_alpha_mode" )] |
392 | pub fn alpha_mode(&self) -> crate::VideoAlphaMode { |
393 | self.0 |
394 | .get_optional(glib::gstr!("GstVideoConverter.alpha-mode" )) |
395 | .expect("Wrong type" ) |
396 | .unwrap_or(crate::VideoAlphaMode::Copy) |
397 | } |
398 | |
399 | pub fn set_border_argb(&mut self, v: u32) { |
400 | self.0.set(glib::gstr!("GstVideoConverter.border-argb" ), v); |
401 | } |
402 | |
403 | #[doc (alias = "get_border_argb" )] |
404 | pub fn border_argb(&self) -> u32 { |
405 | self.0 |
406 | .get_optional(glib::gstr!("GstVideoConverter.border-argb" )) |
407 | .expect("Wrong type" ) |
408 | .unwrap_or(0xff_00_00_00) |
409 | } |
410 | |
411 | pub fn set_chroma_mode(&mut self, v: crate::VideoChromaMode) { |
412 | self.0.set(glib::gstr!("GstVideoConverter.chroma-mode" ), v); |
413 | } |
414 | |
415 | #[doc (alias = "get_chroma_mode" )] |
416 | pub fn chroma_mode(&self) -> crate::VideoChromaMode { |
417 | self.0 |
418 | .get_optional(glib::gstr!("GstVideoConverter.chroma-mode" )) |
419 | .expect("Wrong type" ) |
420 | .unwrap_or(crate::VideoChromaMode::Full) |
421 | } |
422 | |
423 | pub fn set_matrix_mode(&mut self, v: crate::VideoMatrixMode) { |
424 | self.0.set(glib::gstr!("GstVideoConverter.matrix-mode" ), v); |
425 | } |
426 | |
427 | #[doc (alias = "get_matrix_mode" )] |
428 | pub fn matrix_mode(&self) -> crate::VideoMatrixMode { |
429 | self.0 |
430 | .get_optional(glib::gstr!("GstVideoConverter.matrix-mode" )) |
431 | .expect("Wrong type" ) |
432 | .unwrap_or(crate::VideoMatrixMode::Full) |
433 | } |
434 | |
435 | pub fn set_gamma_mode(&mut self, v: crate::VideoGammaMode) { |
436 | self.0.set(glib::gstr!("GstVideoConverter.gamma-mode" ), v); |
437 | } |
438 | |
439 | #[doc (alias = "get_gamma_mode" )] |
440 | pub fn gamma_mode(&self) -> crate::VideoGammaMode { |
441 | self.0 |
442 | .get_optional(glib::gstr!("GstVideoConverter.gamma-mode" )) |
443 | .expect("Wrong type" ) |
444 | .unwrap_or(crate::VideoGammaMode::None) |
445 | } |
446 | |
447 | pub fn set_primaries_mode(&mut self, v: crate::VideoPrimariesMode) { |
448 | self.0 |
449 | .set(glib::gstr!("GstVideoConverter.primaries-mode" ), v); |
450 | } |
451 | |
452 | #[doc (alias = "get_primaries_mode" )] |
453 | pub fn primaries_mode(&self) -> crate::VideoPrimariesMode { |
454 | self.0 |
455 | .get_optional(glib::gstr!("GstVideoConverter.primaries-mode" )) |
456 | .expect("Wrong type" ) |
457 | .unwrap_or(crate::VideoPrimariesMode::None) |
458 | } |
459 | |
460 | pub fn set_threads(&mut self, v: u32) { |
461 | self.0.set(glib::gstr!("GstVideoConverter.threads" ), v); |
462 | } |
463 | |
464 | #[doc (alias = "get_threads" )] |
465 | pub fn threads(&self) -> u32 { |
466 | self.0 |
467 | .get_optional(glib::gstr!("GstVideoConverter.threads" )) |
468 | .expect("Wrong type" ) |
469 | .unwrap_or(1) |
470 | } |
471 | } |
472 | |