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
//! Video clip formats.

use std::fmt::Debug;
use std::ops::Deref;
use std::ptr;
use vapoursynth_sys as ffi;

use crate::format::Format;
use crate::node;

/// Represents video resolution.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Resolution {
    /// Width of the clip, greater than 0.
    pub width: usize,

    /// Height of the clip, greater than 0.
    pub height: usize,
}

/// Represents video framerate.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Framerate {
    /// FPS numerator, greater than 0.
    pub numerator: u64,

    /// FPS denominator, greater than 0.
    pub denominator: u64,
}

/// Represents a property that can be either constant or variable, like the resolution or the
/// framerate.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum Property<T: Debug + Clone + Copy + Eq + PartialEq> {
    /// This property is variable.
    Variable,

    /// This property is constant.
    Constant(T),
}

/// Contains information about a video clip.
#[derive(Debug, Copy, Clone)]
pub struct VideoInfo<'core> {
    /// Format of the clip.
    pub format: Property<Format<'core>>,

    /// Framerate of the clip.
    pub framerate: Property<Framerate>,

    /// Resolution of the clip.
    pub resolution: Property<Resolution>,

    /// Length of the clip, greater than 0.
    #[cfg(feature = "gte-vapoursynth-api-32")]
    pub num_frames: usize,

    /// Length of the clip.
    #[cfg(not(feature = "gte-vapoursynth-api-32"))]
    pub num_frames: Property<usize>,

    /// The flags of this clip.
    pub flags: node::Flags,
}

impl<'core> VideoInfo<'core> {
    /// Creates a `VideoInfo` from a raw pointer.
    ///
    /// # Safety
    /// The caller must ensure `ptr` and the lifetime is valid.
    pub(crate) unsafe fn from_ptr(ptr: *const ffi::VSVideoInfo) -> Self {
        let info = &*ptr;

        debug_assert!(info.fpsNum >= 0);
        debug_assert!(info.fpsDen >= 0);
        debug_assert!(info.width >= 0);
        debug_assert!(info.height >= 0);
        debug_assert!(info.numFrames >= 0);

        let format = if info.format.is_null() {
            Property::Variable
        } else {
            Property::Constant(Format::from_ptr(info.format))
        };

        let framerate = if info.fpsNum == 0 {
            debug_assert!(info.fpsDen == 0);
            Property::Variable
        } else {
            debug_assert!(info.fpsDen != 0);
            Property::Constant(Framerate {
                numerator: info.fpsNum as _,
                denominator: info.fpsDen as _,
            })
        };

        let resolution = if info.width == 0 {
            debug_assert!(info.height == 0);
            Property::Variable
        } else {
            debug_assert!(info.height != 0);
            Property::Constant(Resolution {
                width: info.width as _,
                height: info.height as _,
            })
        };

        #[cfg(feature = "gte-vapoursynth-api-32")]
        let num_frames = {
            debug_assert!(info.numFrames != 0);
            info.numFrames as _
        };

        #[cfg(not(feature = "gte-vapoursynth-api-32"))]
        let num_frames = {
            if info.numFrames == 0 {
                Property::Variable
            } else {
                Property::Constant(info.numFrames as _)
            }
        };

        Self {
            format,
            framerate,
            resolution,
            num_frames,
            flags: ffi::VSNodeFlags(info.flags).into(),
        }
    }

    /// Converts the Rust struct into a C struct.
    pub(crate) fn ffi_type(self) -> ffi::VSVideoInfo {
        let format = match self.format {
            Property::Variable => ptr::null(),
            Property::Constant(x) => x.deref(),
        };

        let (fps_num, fps_den) = match self.framerate {
            Property::Variable => (0, 0),
            Property::Constant(Framerate {
                numerator,
                denominator,
            }) => (numerator as i64, denominator as i64),
        };

        let (width, height) = match self.resolution {
            Property::Variable => (0, 0),
            Property::Constant(Resolution { width, height }) => (width as i32, height as i32),
        };

        #[cfg(feature = "gte-vapoursynth-api-32")]
        let num_frames = self.num_frames as i32;

        #[cfg(not(feature = "gte-vapoursynth-api-32"))]
        let num_frames = match self.num_frames {
            Property::Variable => 0,
            Property::Constant(x) => x as i32,
        };

        let flags = self.flags.bits();

        ffi::VSVideoInfo {
            format,
            fpsNum: fps_num,
            fpsDen: fps_den,
            width,
            height,
            numFrames: num_frames,
            flags,
        }
    }
}

impl<T> From<T> for Property<T>
where
    T: Debug + Clone + Copy + Eq + PartialEq,
{
    #[inline]
    fn from(x: T) -> Self {
        Property::Constant(x)
    }
}