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
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{fmt, marker::PhantomData, mem, ops, ptr, str};

use glib::translate::*;
use gst::prelude::*;

use crate::{VideoFormat, VideoInfo};

#[doc(alias = "gst_video_dma_drm_fourcc_from_format")]
pub fn dma_drm_fourcc_from_format(v: VideoFormat) -> Result<u32, glib::BoolError> {
    skip_assert_initialized!();
    unsafe {
        let res = ffi::gst_video_dma_drm_fourcc_from_format(v.into_glib());
        if res == 0 {
            Err(glib::bool_error!("Unsupported video format"))
        } else {
            Ok(res)
        }
    }
}

#[doc(alias = "gst_video_dma_drm_fourcc_to_format")]
pub fn dma_drm_fourcc_to_format(v: u32) -> Result<VideoFormat, glib::BoolError> {
    skip_assert_initialized!();
    unsafe {
        let res = ffi::gst_video_dma_drm_fourcc_to_format(v);
        if res == ffi::GST_VIDEO_FORMAT_UNKNOWN {
            Err(glib::bool_error!("Unsupported fourcc"))
        } else {
            Ok(from_glib(res))
        }
    }
}

#[doc(alias = "gst_video_dma_drm_fourcc_to_string")]
pub fn dma_drm_fourcc_to_string(fourcc: u32, modifier: u64) -> glib::GString {
    skip_assert_initialized!();
    unsafe {
        glib::GString::from_glib_full(ffi::gst_video_dma_drm_fourcc_to_string(fourcc, modifier))
    }
}

#[doc(alias = "gst_video_dma_drm_fourcc_from_string")]
pub fn dma_drm_fourcc_from_str(v: &str) -> Result<(u32, u64), glib::BoolError> {
    skip_assert_initialized!();
    unsafe {
        let mut modifier = mem::MaybeUninit::uninit();
        let res =
            ffi::gst_video_dma_drm_fourcc_from_string(v.to_glib_none().0, modifier.as_mut_ptr());
        if res == 0 {
            Err(glib::bool_error!("Can't parse fourcc string"))
        } else {
            Ok((res, modifier.assume_init()))
        }
    }
}

/// Information describing a DMABuf image properties. It wraps [`VideoInfo`][crate::VideoInfo] and
/// adds DRM information such as drm-fourcc and drm-modifier, required for
/// negotiation and mapping.
#[doc(alias = "GstVideoInfoDmaDrm")]
#[derive(Clone)]
#[repr(transparent)]
pub struct VideoInfoDmaDrm(pub(crate) ffi::GstVideoInfoDmaDrm);

impl ops::Deref for VideoInfoDmaDrm {
    type Target = VideoInfo;

    fn deref(&self) -> &Self::Target {
        unsafe { &*(&self.0.vinfo as *const ffi::GstVideoInfo as *const VideoInfo) }
    }
}

impl fmt::Debug for VideoInfoDmaDrm {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("VideoInfoDmaDrm")
            .field("info", &**self)
            .field("drm_fourcc", &self.0.drm_fourcc)
            .field("drm_modifier", &self.0.drm_modifier)
            .finish()
    }
}

impl VideoInfoDmaDrm {
    /// Allocate a new [`VideoInfoDmaDrm`][crate::VideoInfoDmaDrm] that is also initialized with
    /// [`init()`][Self::init()].
    ///
    /// # Returns
    ///
    /// a new [`VideoInfoDmaDrm`][crate::VideoInfoDmaDrm].
    /// Free it with `gst_video_info_dma_drm_free()`.
    pub fn new(info: VideoInfo, fourcc: u32, modifier: u64) -> VideoInfoDmaDrm {
        assert_initialized_main_thread!();

        VideoInfoDmaDrm(ffi::GstVideoInfoDmaDrm {
            vinfo: info.0,
            drm_fourcc: fourcc,
            drm_modifier: modifier,
            _gst_reserved: [0; 20],
        })
    }

    #[inline]
    pub fn is_valid(&self) -> bool {
        !self.0.vinfo.finfo.is_null()
            && self.0.vinfo.width > 0
            && self.0.vinfo.height > 0
            && self.0.vinfo.size > 0
    }

    /// Parse `caps` to generate a [`VideoInfoDmaDrm`][crate::VideoInfoDmaDrm]. Please note that the
    /// `caps` should be a dma drm caps. The `gst_video_is_dma_drm_caps()` can
    /// be used to verify it before calling this function.
    /// ## `caps`
    /// a [`gst::Caps`][crate::gst::Caps]
    ///
    /// # Returns
    ///
    /// A [`VideoInfoDmaDrm`][crate::VideoInfoDmaDrm],
    ///  or [`None`] if `caps` couldn't be parsed.
    #[doc(alias = "gst_video_info_dma_drm_from_caps")]
    pub fn from_caps(caps: &gst::CapsRef) -> Result<Self, glib::error::BoolError> {
        skip_assert_initialized!();

        unsafe {
            let mut info = mem::MaybeUninit::uninit();
            if from_glib(ffi::gst_video_info_dma_drm_from_caps(
                info.as_mut_ptr(),
                caps.as_ptr(),
            )) {
                Ok(Self(info.assume_init()))
            } else {
                Err(glib::bool_error!(
                    "Failed to create VideoInfoDmaDrm from caps"
                ))
            }
        }
    }

    /// Convert the values of `self` into a [`gst::Caps`][crate::gst::Caps]. Please note that the
    /// `caps` returned will be a dma drm caps which sets format field to DMA_DRM,
    /// and contains a new drm-format field. The value of drm-format field is
    /// composed of a drm fourcc and a modifier, such as NV12:0x0100000000000002.
    ///
    /// # Returns
    ///
    /// a new [`gst::Caps`][crate::gst::Caps] containing the
    /// info in `self`.
    #[doc(alias = "gst_video_info_dma_drm_to_caps")]
    pub fn to_caps(&self) -> Result<gst::Caps, glib::error::BoolError> {
        unsafe {
            let result = from_glib_full(ffi::gst_video_info_dma_drm_to_caps(mut_override(&self.0)));
            match result {
                Some(c) => Ok(c),
                None => Err(glib::bool_error!(
                    "Failed to create caps from VideoInfoDmaDrm"
                )),
            }
        }
    }

    /// Fills `drm_info` if `info`'s format has a valid drm format and `modifier` is also
    /// valid
    /// ## `info`
    /// a [`VideoInfo`][crate::VideoInfo]
    /// ## `modifier`
    /// the associated modifier value.
    ///
    /// # Returns
    ///
    /// [`true`] if `drm_info` is filled correctly.
    ///
    /// ## `drm_info`
    /// [`VideoInfoDmaDrm`][crate::VideoInfoDmaDrm]
    #[doc(alias = "gst_video_info_dma_drm_from_video_info")]
    pub fn from_video_info(
        video_info: &crate::VideoInfo,
        modifier: u64,
    ) -> Result<Self, glib::error::BoolError> {
        skip_assert_initialized!();

        unsafe {
            let mut info = mem::MaybeUninit::uninit();
            if from_glib(ffi::gst_video_info_dma_drm_from_video_info(
                info.as_mut_ptr(),
                video_info.to_glib_none().0,
                modifier,
            )) {
                Ok(Self(info.assume_init()))
            } else {
                Err(glib::bool_error!(
                    "Failed to create VideoInfoDmaDrm from VideoInfo"
                ))
            }
        }
    }

    /// Convert the [`VideoInfoDmaDrm`][crate::VideoInfoDmaDrm] into a traditional [`VideoInfo`][crate::VideoInfo] with
    /// recognized video format. For DMA kind memory, the non linear DMA format
    /// should be recognized as [`VideoFormat::DmaDrm`][crate::VideoFormat::DmaDrm]. This helper function
    /// sets `info`'s video format into the default value according to `self`'s
    /// drm_fourcc field.
    ///
    /// # Returns
    ///
    /// [`true`] if `info` is converted correctly.
    ///
    /// ## `info`
    /// [`VideoInfo`][crate::VideoInfo]
    #[doc(alias = "gst_video_info_dma_drm_to_video_info")]
    pub fn to_video_info(&self) -> Result<crate::VideoInfo, glib::error::BoolError> {
        unsafe {
            let mut video_info = mem::MaybeUninit::uninit();
            if from_glib(ffi::gst_video_info_dma_drm_to_video_info(
                mut_override(&self.0),
                video_info.as_mut_ptr(),
            )) {
                Ok(crate::VideoInfo(video_info.assume_init()))
            } else {
                Err(glib::bool_error!(
                    "Failed to create VideoInfo from VideoInfoDmaDrm"
                ))
            }
        }
    }

    #[inline]
    pub fn fourcc(&self) -> u32 {
        self.0.drm_fourcc
    }

    #[inline]
    pub fn modifier(&self) -> u64 {
        self.0.drm_modifier
    }
}

impl PartialEq for VideoInfoDmaDrm {
    #[doc(alias = "gst_video_info_is_equal")]
    fn eq(&self, other: &Self) -> bool {
        unsafe {
            from_glib(ffi::gst_video_info_is_equal(&self.0.vinfo, &other.0.vinfo))
                && self.0.drm_fourcc == other.0.drm_fourcc
                && self.0.drm_modifier == other.0.drm_modifier
        }
    }
}

impl Eq for VideoInfoDmaDrm {}

unsafe impl Send for VideoInfoDmaDrm {}
unsafe impl Sync for VideoInfoDmaDrm {}

impl glib::types::StaticType for VideoInfoDmaDrm {
    #[inline]
    fn static_type() -> glib::types::Type {
        unsafe { glib::translate::from_glib(ffi::gst_video_info_dma_drm_get_type()) }
    }
}

impl glib::value::ValueType for VideoInfoDmaDrm {
    type Type = Self;
}

#[doc(hidden)]
unsafe impl<'a> glib::value::FromValue<'a> for VideoInfoDmaDrm {
    type Checker = glib::value::GenericValueTypeOrNoneChecker<Self>;

    unsafe fn from_value(value: &'a glib::Value) -> Self {
        skip_assert_initialized!();
        from_glib_none(glib::gobject_ffi::g_value_get_boxed(value.to_glib_none().0)
            as *mut ffi::GstVideoInfoDmaDrm)
    }
}

#[doc(hidden)]
impl glib::value::ToValue for VideoInfoDmaDrm {
    fn to_value(&self) -> glib::Value {
        let mut value = glib::Value::for_value_type::<Self>();
        unsafe {
            glib::gobject_ffi::g_value_set_boxed(
                value.to_glib_none_mut().0,
                self.to_glib_none().0 as *mut _,
            )
        }
        value
    }

    fn value_type(&self) -> glib::Type {
        Self::static_type()
    }
}

#[doc(hidden)]
impl glib::value::ToValueOptional for VideoInfoDmaDrm {
    fn to_value_optional(s: Option<&Self>) -> glib::Value {
        skip_assert_initialized!();
        let mut value = glib::Value::for_value_type::<Self>();
        unsafe {
            glib::gobject_ffi::g_value_set_boxed(
                value.to_glib_none_mut().0,
                s.to_glib_none().0 as *mut _,
            )
        }
        value
    }
}

#[doc(hidden)]
impl From<VideoInfoDmaDrm> for glib::Value {
    fn from(v: VideoInfoDmaDrm) -> glib::Value {
        skip_assert_initialized!();
        glib::value::ToValue::to_value(&v)
    }
}

#[doc(hidden)]
impl glib::translate::Uninitialized for VideoInfoDmaDrm {
    #[inline]
    unsafe fn uninitialized() -> Self {
        mem::zeroed()
    }
}

#[doc(hidden)]
impl glib::translate::GlibPtrDefault for VideoInfoDmaDrm {
    type GlibType = *mut ffi::GstVideoInfoDmaDrm;
}

#[doc(hidden)]
impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstVideoInfoDmaDrm> for VideoInfoDmaDrm {
    type Storage = PhantomData<&'a Self>;

    #[inline]
    fn to_glib_none(&'a self) -> glib::translate::Stash<'a, *const ffi::GstVideoInfoDmaDrm, Self> {
        glib::translate::Stash(&self.0, PhantomData)
    }

    fn to_glib_full(&self) -> *const ffi::GstVideoInfoDmaDrm {
        unimplemented!()
    }
}

#[doc(hidden)]
impl glib::translate::FromGlibPtrNone<*const ffi::GstVideoInfoDmaDrm> for VideoInfoDmaDrm {
    #[inline]
    unsafe fn from_glib_none(ptr: *const ffi::GstVideoInfoDmaDrm) -> Self {
        Self(ptr::read(ptr))
    }
}

#[doc(hidden)]
impl glib::translate::FromGlibPtrNone<*mut ffi::GstVideoInfoDmaDrm> for VideoInfoDmaDrm {
    #[inline]
    unsafe fn from_glib_none(ptr: *mut ffi::GstVideoInfoDmaDrm) -> Self {
        Self(ptr::read(ptr))
    }
}

#[doc(hidden)]
impl glib::translate::FromGlibPtrFull<*mut ffi::GstVideoInfoDmaDrm> for VideoInfoDmaDrm {
    #[inline]
    unsafe fn from_glib_full(ptr: *mut ffi::GstVideoInfoDmaDrm) -> Self {
        let info = from_glib_none(ptr);
        glib::ffi::g_free(ptr as *mut _);
        info
    }
}