1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{ops, ptr};

use crate::VideoFrameExt;
use glib::{prelude::*, translate::*};

#[derive(Debug)]
#[doc(alias = "GstVideoConverter")]
pub struct VideoConverter(ptr::NonNull<ffi::GstVideoConverter>);

impl Drop for VideoConverter {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            ffi::gst_video_converter_free(self.0.as_ptr());
        }
    }
}

unsafe impl Send for VideoConverter {}
unsafe impl Sync for VideoConverter {}

impl VideoConverter {
    #[doc(alias = "gst_video_converter_new")]
    pub fn new(
        in_info: &crate::VideoInfo,
        out_info: &crate::VideoInfo,
        config: Option<VideoConverterConfig>,
    ) -> Result<Self, glib::BoolError> {
        skip_assert_initialized!();
        if in_info.fps() != out_info.fps() {
            return Err(glib::bool_error!("Can't do framerate conversion"));
        }

        if in_info.interlace_mode() != out_info.interlace_mode() {
            return Err(glib::bool_error!("Can't do interlacing conversion"));
        }

        unsafe {
            let ptr = ffi::gst_video_converter_new(
                in_info.to_glib_none().0 as *mut _,
                out_info.to_glib_none().0 as *mut _,
                config
                    .map(|s| s.0.into_glib_ptr())
                    .unwrap_or(ptr::null_mut()),
            );
            if ptr.is_null() {
                Err(glib::bool_error!("Failed to create video converter"))
            } else {
                Ok(Self(ptr::NonNull::new_unchecked(ptr)))
            }
        }
    }

    #[doc(alias = "get_config")]
    #[doc(alias = "gst_video_converter_get_config")]
    pub fn config(&self) -> VideoConverterConfig {
        unsafe {
            VideoConverterConfig(
                gst::StructureRef::from_glib_borrow(ffi::gst_video_converter_get_config(
                    self.0.as_ptr(),
                ))
                .to_owned(),
            )
        }
    }

    #[doc(alias = "gst_video_converter_set_config")]
    pub fn set_config(&mut self, config: VideoConverterConfig) {
        unsafe {
            ffi::gst_video_converter_set_config(self.0.as_ptr(), config.0.into_glib_ptr());
        }
    }

    #[cfg(feature = "v1_22")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
    #[doc(alias = "get_in_info")]
    #[doc(alias = "gst_video_converter_get_in_info")]
    pub fn in_info(&self) -> crate::VideoInfo {
        unsafe { from_glib_none(ffi::gst_video_converter_get_in_info(self.0.as_ptr())) }
    }

    #[cfg(feature = "v1_22")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_22")))]
    #[doc(alias = "get_out_info")]
    #[doc(alias = "gst_video_converter_get_out_info")]
    pub fn out_info(&self) -> crate::VideoInfo {
        unsafe { from_glib_none(ffi::gst_video_converter_get_out_info(self.0.as_ptr())) }
    }

    #[doc(alias = "gst_video_converter_frame")]
    pub fn frame<T>(
        &self,
        src: &crate::VideoFrame<T>,
        dest: &mut crate::VideoFrame<crate::video_frame::Writable>,
    ) {
        unsafe {
            ffi::gst_video_converter_frame(self.0.as_ptr(), src.as_ptr(), dest.as_mut_ptr());
        }
    }

    pub fn frame_ref<T>(
        &self,
        src: &crate::VideoFrameRef<T>,
        dest: &mut crate::VideoFrameRef<&mut gst::BufferRef>,
    ) {
        unsafe {
            ffi::gst_video_converter_frame(self.0.as_ptr(), src.as_ptr(), dest.as_mut_ptr());
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VideoConverterConfig(gst::Structure);

impl ops::Deref for VideoConverterConfig {
    type Target = gst::StructureRef;

    #[inline]
    fn deref(&self) -> &gst::StructureRef {
        self.0.deref()
    }
}

impl ops::DerefMut for VideoConverterConfig {
    #[inline]
    fn deref_mut(&mut self) -> &mut gst::StructureRef {
        self.0.deref_mut()
    }
}

impl AsRef<gst::StructureRef> for VideoConverterConfig {
    #[inline]
    fn as_ref(&self) -> &gst::StructureRef {
        self.0.as_ref()
    }
}

impl AsMut<gst::StructureRef> for VideoConverterConfig {
    #[inline]
    fn as_mut(&mut self) -> &mut gst::StructureRef {
        self.0.as_mut()
    }
}

impl Default for VideoConverterConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl TryFrom<gst::Structure> for VideoConverterConfig {
    type Error = glib::BoolError;

    fn try_from(v: gst::Structure) -> Result<Self, Self::Error> {
        skip_assert_initialized!();
        if v.name() == "GstVideoConverter" {
            Ok(Self(v))
        } else {
            Err(glib::bool_error!("Structure is no VideoConverterConfig"))
        }
    }
}

impl<'a> TryFrom<&'a gst::StructureRef> for VideoConverterConfig {
    type Error = glib::BoolError;

    fn try_from(v: &'a gst::StructureRef) -> Result<Self, Self::Error> {
        skip_assert_initialized!();
        Self::try_from(v.to_owned())
    }
}

impl From<VideoConverterConfig> for gst::Structure {
    fn from(v: VideoConverterConfig) -> Self {
        skip_assert_initialized!();
        v.0
    }
}

impl glib::value::ToValue for VideoConverterConfig {
    fn to_value(&self) -> glib::Value {
        self.0.to_value()
    }

    fn value_type(&self) -> glib::Type {
        self.0.value_type()
    }
}

impl glib::value::ToValueOptional for VideoConverterConfig {
    fn to_value_optional(s: Option<&Self>) -> glib::Value {
        skip_assert_initialized!();
        s.map(|s| &s.0).to_value()
    }
}

impl From<VideoConverterConfig> for glib::Value {
    fn from(s: VideoConverterConfig) -> glib::Value {
        skip_assert_initialized!();
        s.0.into()
    }
}

impl VideoConverterConfig {
    pub fn new() -> Self {
        Self(gst::Structure::new_empty("GstVideoConverter"))
    }

    pub fn set_resampler_method(&mut self, v: crate::VideoResamplerMethod) {
        self.0
            .set(glib::gstr!("GstVideoConverter.resampler-method"), v);
    }

    #[doc(alias = "get_resampler_method")]
    pub fn resampler_method(&self) -> crate::VideoResamplerMethod {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.resampler-method"))
            .expect("Wrong type")
            .unwrap_or(crate::VideoResamplerMethod::Cubic)
    }

    pub fn set_chroma_resampler_method(&mut self, v: crate::VideoResamplerMethod) {
        self.0
            .set(glib::gstr!("GstVideoConverter.chroma-resampler-method"), v);
    }

    #[doc(alias = "get_chroma_resampler_method")]
    pub fn chroma_resampler_method(&self) -> crate::VideoResamplerMethod {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.chroma-resampler-method"))
            .expect("Wrong type")
            .unwrap_or(crate::VideoResamplerMethod::Linear)
    }

    pub fn set_resampler_taps(&mut self, v: u32) {
        self.0
            .set(glib::gstr!("GstVideoConverter.resampler-taps"), v);
    }

    #[doc(alias = "get_resampler_taps")]
    pub fn resampler_taps(&self) -> u32 {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.resampler-taps"))
            .expect("Wrong type")
            .unwrap_or(0)
    }

    pub fn set_dither_method(&mut self, v: crate::VideoDitherMethod) {
        self.0
            .set(glib::gstr!("GstVideoConverter.dither-method"), v);
    }

    #[doc(alias = "get_dither_method")]
    pub fn dither_method(&self) -> crate::VideoDitherMethod {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.dither-method"))
            .expect("Wrong type")
            .unwrap_or(crate::VideoDitherMethod::Bayer)
    }

    pub fn set_dither_quantization(&mut self, v: u32) {
        self.0
            .set(glib::gstr!("GstVideoConverter.dither-quantization"), v);
    }

    #[doc(alias = "get_dither_quantization")]
    pub fn dither_quantization(&self) -> u32 {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.dither-quantization"))
            .expect("Wrong type")
            .unwrap_or(1)
    }

    pub fn set_src_x(&mut self, v: i32) {
        self.0.set(glib::gstr!("GstVideoConverter.src-x"), v);
    }

    #[doc(alias = "get_src_x")]
    pub fn src_x(&self) -> i32 {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.src-x"))
            .expect("Wrong type")
            .unwrap_or(0)
    }

    pub fn set_src_y(&mut self, v: i32) {
        self.0.set(glib::gstr!("GstVideoConverter.src-y"), v);
    }

    #[doc(alias = "get_src_y")]
    pub fn src_y(&self) -> i32 {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.src-y"))
            .expect("Wrong type")
            .unwrap_or(0)
    }

    pub fn set_src_width(&mut self, v: Option<i32>) {
        if let Some(v) = v {
            self.0.set(glib::gstr!("GstVideoConverter.src-width"), v);
        } else {
            self.0
                .remove_field(glib::gstr!("GstVideoConverter.src-width"));
        }
    }

    #[doc(alias = "get_src_width")]
    pub fn src_width(&self) -> Option<i32> {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.src-width"))
            .expect("Wrong type")
    }

    pub fn set_src_height(&mut self, v: Option<i32>) {
        if let Some(v) = v {
            self.0.set(glib::gstr!("GstVideoConverter.src-height"), v);
        } else {
            self.0
                .remove_field(glib::gstr!("GstVideoConverter.src-height"));
        }
    }

    #[doc(alias = "get_src_height")]
    pub fn src_height(&self) -> Option<i32> {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.src-height"))
            .expect("Wrong type")
    }

    pub fn set_dest_x(&mut self, v: i32) {
        self.0.set(glib::gstr!("GstVideoConverter.dest-x"), v);
    }

    #[doc(alias = "get_dest_x")]
    pub fn dest_x(&self) -> i32 {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.dest-x"))
            .expect("Wrong type")
            .unwrap_or(0)
    }

    pub fn set_dest_y(&mut self, v: i32) {
        self.0.set(glib::gstr!("GstVideoConverter.dest-y"), v);
    }

    #[doc(alias = "get_dest_y")]
    pub fn dest_y(&self) -> i32 {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.dest-y"))
            .expect("Wrong type")
            .unwrap_or(0)
    }

    pub fn set_dest_width(&mut self, v: Option<i32>) {
        if let Some(v) = v {
            self.0.set(glib::gstr!("GstVideoConverter.dest-width"), v);
        } else {
            self.0
                .remove_field(glib::gstr!("GstVideoConverter.dest-width"));
        }
    }

    #[doc(alias = "get_dest_width")]
    pub fn dest_width(&self) -> Option<i32> {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.dest-width"))
            .expect("Wrong type")
    }

    pub fn set_dest_height(&mut self, v: Option<i32>) {
        if let Some(v) = v {
            self.0.set(glib::gstr!("GstVideoConverter.dest-height"), v);
        } else {
            self.0
                .remove_field(glib::gstr!("GstVideoConverter.dest-height"));
        }
    }

    #[doc(alias = "get_dest_height")]
    pub fn dest_height(&self) -> Option<i32> {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.dest-height"))
            .expect("Wrong type")
    }

    pub fn set_fill_border(&mut self, v: bool) {
        self.0.set(glib::gstr!("GstVideoConverter.fill-border"), v);
    }

    #[doc(alias = "get_fill_border")]
    pub fn fills_border(&self) -> bool {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.fill-border"))
            .expect("Wrong type")
            .unwrap_or(true)
    }

    pub fn set_alpha_value(&mut self, v: f64) {
        self.0.set(glib::gstr!("GstVideoConverter.alpha-value"), v);
    }

    #[doc(alias = "get_alpha_value")]
    pub fn alpha_value(&self) -> f64 {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.alpha-value"))
            .expect("Wrong type")
            .unwrap_or(1.0)
    }

    pub fn set_alpha_mode(&mut self, v: crate::VideoAlphaMode) {
        self.0.set(glib::gstr!("GstVideoConverter.alpha-mode"), v);
    }

    #[doc(alias = "get_alpha_mode")]
    pub fn alpha_mode(&self) -> crate::VideoAlphaMode {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.alpha-mode"))
            .expect("Wrong type")
            .unwrap_or(crate::VideoAlphaMode::Copy)
    }

    pub fn set_border_argb(&mut self, v: u32) {
        self.0.set(glib::gstr!("GstVideoConverter.border-argb"), v);
    }

    #[doc(alias = "get_border_argb")]
    pub fn border_argb(&self) -> u32 {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.border-argb"))
            .expect("Wrong type")
            .unwrap_or(0xff_00_00_00)
    }

    pub fn set_chroma_mode(&mut self, v: crate::VideoChromaMode) {
        self.0.set(glib::gstr!("GstVideoConverter.chroma-mode"), v);
    }

    #[doc(alias = "get_chroma_mode")]
    pub fn chroma_mode(&self) -> crate::VideoChromaMode {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.chroma-mode"))
            .expect("Wrong type")
            .unwrap_or(crate::VideoChromaMode::Full)
    }

    pub fn set_matrix_mode(&mut self, v: crate::VideoMatrixMode) {
        self.0.set(glib::gstr!("GstVideoConverter.matrix-mode"), v);
    }

    #[doc(alias = "get_matrix_mode")]
    pub fn matrix_mode(&self) -> crate::VideoMatrixMode {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.matrix-mode"))
            .expect("Wrong type")
            .unwrap_or(crate::VideoMatrixMode::Full)
    }

    pub fn set_gamma_mode(&mut self, v: crate::VideoGammaMode) {
        self.0.set(glib::gstr!("GstVideoConverter.gamma-mode"), v);
    }

    #[doc(alias = "get_gamma_mode")]
    pub fn gamma_mode(&self) -> crate::VideoGammaMode {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.gamma-mode"))
            .expect("Wrong type")
            .unwrap_or(crate::VideoGammaMode::None)
    }

    pub fn set_primaries_mode(&mut self, v: crate::VideoPrimariesMode) {
        self.0
            .set(glib::gstr!("GstVideoConverter.primaries-mode"), v);
    }

    #[doc(alias = "get_primaries_mode")]
    pub fn primaries_mode(&self) -> crate::VideoPrimariesMode {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.primaries-mode"))
            .expect("Wrong type")
            .unwrap_or(crate::VideoPrimariesMode::None)
    }

    pub fn set_threads(&mut self, v: u32) {
        self.0.set(glib::gstr!("GstVideoConverter.threads"), v);
    }

    #[doc(alias = "get_threads")]
    pub fn threads(&self) -> u32 {
        self.0
            .get_optional(glib::gstr!("GstVideoConverter.threads"))
            .expect("Wrong type")
            .unwrap_or(1)
    }
}