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
use crate::{calls, types::*, Internal, Result};
use std::os::unix::io::RawFd;

impl Internal<&mut Format> {
    pub fn get(&mut self, fd: RawFd) -> Result<()> {
        unsafe_call!(calls::g_fmt(fd, *self.as_mut() as *mut _)).map(|_| ())
    }
}

impl Internal<&mut Format> {
    pub fn set(&self, fd: RawFd) -> Result<()> {
        unsafe_call!(calls::s_fmt(fd, *self.as_ref() as *const _ as *mut _)).map(|_| ())
    }

    pub fn try_(&self, fd: RawFd) -> Result<()> {
        unsafe_call!(calls::try_fmt(fd, *self.as_ref() as *const _ as *mut _)).map(|_| ())
    }
}

impl From<BufferType> for Format {
    fn from(type_: BufferType) -> Self {
        Self {
            type_,
            fmt: FormatUnion { raw_data: [0; 200] },
        }
    }
}

unsafe impl Send for Format {}

impl Format {
    /// Create from value
    pub fn new<T: IsFormatData>(type_: BufferType, data: T) -> Option<Self> {
        if T::TYPES.contains(&type_) {
            let mut this = Self::from(type_);
            *this.try_mut().unwrap() = data;
            Some(this)
        } else {
            None
        }
    }

    /// Get reference to value
    pub fn try_ref<T: IsFormatData>(&self) -> Option<&T> {
        if T::TYPES.contains(&self.type_()) {
            Some(unsafe { &*(&self.fmt as *const _ as *const T) })
        } else {
            None
        }
    }

    /// Get mutable reference to value
    pub fn try_mut<T: IsFormatData>(&mut self) -> Option<&mut T> {
        if T::TYPES.contains(&self.type_()) {
            Some(unsafe { &mut *(&mut self.fmt as *mut _ as *mut T) })
        } else {
            None
        }
    }
}

/// Format data types
pub trait IsFormatData {
    /// Buffer types which corresponds to format type
    const TYPES: &'static [BufferType];
}

macro_rules! format_impl {
    ($($type:ty: $($buf_type:ident)*,)*) => {
        $(
            impl IsFormatData for $type {
                const TYPES: &'static [BufferType] = &[ $(BufferType::$buf_type,)* ];
            }
        )*

        impl core::fmt::Display for Format {
            fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                self.type_.fmt(f)?;
                ": ".fmt(f)?;
                match self.type_ {
                    $(
                        $(BufferType::$buf_type)|* => self.try_ref::<$type>()
                            .ok_or_else(Default::default)?.fmt(f),
                    )*
                }
            }
        }
    };
}

format_impl! {
    PixFormat: VideoCapture VideoOutput,
    VbiFormat: VbiCapture VbiOutput,
    SlicedVbiFormat: SlicedVbiCapture SlicedVbiOutput,
    Window: VideoOverlay VideoOutputOverlay,
    PixFormatMplane: VideoCaptureMplane VideoOutputMplane,
    SdrFormat: SdrCapture SdrOutput,
    MetaFormat: MetaCapture MetaOutput,
}

impl core::fmt::Display for PixFormat {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        self.width.fmt(f)?;
        'x'.fmt(f)?;
        self.height.fmt(f)?;
        ' '.fmt(f)?;
        self.pixel_format.fmt(f)?;
        ' '.fmt(f)?;
        self.field.fmt(f)?;
        " #".fmt(f)?;
        self.bytes_per_line.fmt(f)?;
        '/'.fmt(f)?;
        self.size_image.fmt(f)?;
        ' '.fmt(f)?;
        self.color_space.fmt(f)?;
        ' '.fmt(f)?;
        // TODO: union
        self.quantization.fmt(f)?;
        ' '.fmt(f)?;
        self.xfer_func.fmt(f)
    }
}

impl PixFormatMplane {
    /// Plane formats
    pub fn plane_fmt(&self) -> &[PlanePixFormat] {
        &self.plane_fmt[..self.num_planes as usize]
    }

    /// Plane formats
    pub fn plane_fmt_mut(&mut self) -> &mut [PlanePixFormat] {
        &mut self.plane_fmt[..self.num_planes as usize]
    }
}

impl core::fmt::Display for PixFormatMplane {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        self.width().fmt(f)?;
        'x'.fmt(f)?;
        self.height().fmt(f)?;
        ' '.fmt(f)?;
        self.pixel_format().fmt(f)?;
        ' '.fmt(f)?;
        self.field().fmt(f)?;
        for plane in self.plane_fmt() {
            " #".fmt(f)?;
            plane.bytes_per_line().fmt(f)?;
            '/'.fmt(f)?;
            plane.size_image().fmt(f)?;
        }
        ' '.fmt(f)?;
        self.color_space().fmt(f)?;
        ' '.fmt(f)?;
        // TODO: union
        self.quantization().fmt(f)?;
        ' '.fmt(f)?;
        self.xfer_func().fmt(f)
    }
}

impl core::fmt::Display for SdrFormat {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        self.pixel_format().fmt(f)?;
        " #".fmt(f)?;
        self.buffer_size().fmt(f)
    }
}

impl core::fmt::Display for MetaFormat {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        self.data_format().fmt(f)?;
        " #".fmt(f)?;
        self.buffer_size().fmt(f)
    }
}

impl core::fmt::Display for Window {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        // TODO:
        self.w.fmt(f)?;
        ' '.fmt(f)?;
        self.field.fmt(f)?;
        " !".fmt(f)?;
        self.chromakey.fmt(f)
    }
}

impl core::fmt::Display for VbiFormat {
    fn fmt(&self, _f: &mut core::fmt::Formatter) -> core::fmt::Result {
        todo!()
    }
}

impl core::fmt::Display for SlicedVbiFormat {
    fn fmt(&self, _f: &mut core::fmt::Formatter) -> core::fmt::Result {
        todo!()
    }
}