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
use std::mem;

use obs_sys::{
    obs_source_frame, video_output_get_format, video_output_get_frame_rate,
    video_output_get_height, video_output_get_width, video_t,
};

#[derive(Debug)]
pub enum VideoFormat {
    Unknown,
    None,
    I420,
    NV12,
    YVYU,
    YUY2,
    UYVY,
    RGBA,
    BGRA,
    BGRX,
    Y800,
    I444,
    BGR3,
    I422,
    I40A,
    I42A,
    YUVA,
    AYUV,
}

impl PartialEq for VideoFormat {
    fn eq(&self, other: &Self) -> bool {
        if matches!(self, VideoFormat::Unknown) || matches!(other, VideoFormat::Unknown) {
            false
        } else {
            mem::discriminant(self) == mem::discriminant(other)
        }
    }
}

impl From<u32> for VideoFormat {
    fn from(raw: u32) -> Self {
        match raw {
            0 => VideoFormat::None,
            1 => VideoFormat::I420,
            2 => VideoFormat::NV12,
            3 => VideoFormat::YVYU,
            4 => VideoFormat::YUY2,
            5 => VideoFormat::UYVY,
            6 => VideoFormat::RGBA,
            7 => VideoFormat::BGRA,
            8 => VideoFormat::BGRX,
            9 => VideoFormat::Y800,
            10 => VideoFormat::I444,
            11 => VideoFormat::BGR3,
            12 => VideoFormat::I422,
            13 => VideoFormat::I40A,
            14 => VideoFormat::I42A,
            15 => VideoFormat::YUVA,
            16 => VideoFormat::AYUV,
            _ => VideoFormat::Unknown,
        }
    }
}

pub struct VideoDataContext {
    pointer: *mut obs_source_frame,
}

impl VideoDataContext {
    pub(crate) unsafe fn from_raw(pointer: *mut obs_source_frame) -> Self {
        Self { pointer }
    }

    pub fn get_format(&self) -> VideoFormat {
        let raw = unsafe { (*self.pointer).format };

        VideoFormat::from(raw as u32)
    }

    pub fn get_width(&self) -> u32 {
        unsafe { (*self.pointer).width }
    }

    pub fn get_height(&self) -> u32 {
        unsafe { (*self.pointer).height }
    }

    pub fn get_data_buffer(&self, idx: usize) -> *mut u8 {
        unsafe { (*self.pointer).data[idx] }
    }

    pub fn get_linesize(&self, idx: usize) -> u32 {
        unsafe { (*self.pointer).linesize[idx] }
    }
}

#[allow(unused)]
pub struct VideoRef {
    pointer: *mut video_t,
}

#[allow(unused)]
impl VideoRef {
    pub(crate) unsafe fn from_raw(pointer: *mut video_t) -> Self {
        Self { pointer }
    }

    pub(crate) fn get_width(&self) -> u32 {
        unsafe { video_output_get_width(self.pointer) }
    }

    pub(crate) fn get_height(&self) -> u32 {
        unsafe { video_output_get_height(self.pointer) }
    }

    pub(crate) fn get_frame_rate(&self) -> f64 {
        unsafe { video_output_get_frame_rate(self.pointer) }
    }

    pub(crate) fn get_format(&self) -> VideoFormat {
        let raw = unsafe { video_output_get_format(self.pointer) };

        VideoFormat::from(raw as u32)
    }
}