Skip to main content

vapoursynth4_sys/
helper.rs

1/*
2 This Source Code Form is subject to the terms of the Mozilla Public
3 License, v. 2.0. If a copy of the MPL was not distributed with this
4 file, You can obtain one at http://mozilla.org/MPL/2.0/.
5*/
6
7//! VSHelper4.h
8
9#![allow(clippy::cast_possible_truncation)]
10
11use std::ffi::{CStr, c_int, c_void};
12
13use crate::{
14    VSAPI, VSAudioFormat, VSAudioInfo, VSColorFamily, VSCore, VSPresetVideoFormat, VSVideoFormat,
15    VSVideoInfo,
16};
17
18/// Identifier of the built-in standard filters plugin.
19pub const VSH_STD_PLUGIN_ID: &CStr = c"com.vapoursynth.std";
20/// Identifier of the built-in resize plugin.
21pub const VSH_RESIZE_PLUGIN_ID: &CStr = c"com.vapoursynth.resize";
22/// Identifier of the text plugin.
23pub const VSH_TEXT_PLUGIN_ID: &CStr = c"com.vapoursynth.text";
24
25/// Convenience function for checking if the format never changes between frames
26#[inline]
27#[must_use]
28pub const fn is_constant_video_format(vi: &VSVideoInfo) -> bool {
29    vi.height > 0
30        && vi.width > 0
31        && vi.format.color_family as i32 != VSColorFamily::Undefined as i32
32}
33
34/// Convenience function to check if two clips have the same format
35/// (unknown/changeable will be considered the same too)
36#[inline]
37#[must_use]
38pub const fn is_same_video_format(v1: &VSVideoFormat, v2: &VSVideoFormat) -> bool {
39    v1.color_family as i32 == v2.color_family as i32
40        && v1.sample_type as i32 == v2.sample_type as i32
41        && v1.bits_per_sample == v2.bits_per_sample
42        && v1.sub_sampling_w == v2.sub_sampling_w
43        && v1.sub_sampling_h == v2.sub_sampling_h
44}
45
46impl VSAPI {
47    /// Convenience function to check if a clip has the same format as a format id
48    ///
49    /// # Safety
50    ///
51    /// `core` must be valid
52    #[inline]
53    pub unsafe fn is_same_video_preset_format(
54        &self,
55        preset_format: VSPresetVideoFormat,
56        v: &VSVideoFormat,
57        core: *mut VSCore,
58    ) -> bool {
59        unsafe {
60            (self.queryVideoFormatID)(
61                v.color_family,
62                v.sample_type,
63                v.bits_per_sample,
64                v.sub_sampling_w,
65                v.sub_sampling_h,
66                core,
67            ) == preset_format as u32
68        }
69    }
70}
71
72/// Convenience function to check for if two clips have the same format
73/// (but not framerate) while also including width and height
74/// (unknown/changeable will be considered the same too)
75#[inline]
76#[must_use]
77pub const fn is_same_video_info(v1: &VSVideoInfo, v2: &VSVideoInfo) -> bool {
78    v1.height == v2.height && v1.width == v2.width && is_same_video_format(&v1.format, &v2.format)
79}
80
81/// Convenience function to check for if two clips have the same format while also including
82/// `sampleRate` (unknown/changeable will be considered the same too)
83#[inline]
84#[must_use]
85pub const fn is_same_audio_format(a1: &VSAudioFormat, a2: &VSAudioFormat) -> bool {
86    a1.bits_per_sample == a2.bits_per_sample
87        && a1.sample_type as i32 == a2.sample_type as i32
88        && a1.channel_layout == a2.channel_layout
89}
90
91/// Convenience function to check for if two clips have the same format while also including
92/// `sampleRate` (unknown/changeable will be considered the same too)
93#[inline]
94#[must_use]
95pub const fn is_same_audio_info(a1: &VSAudioInfo, a2: &VSAudioInfo) -> bool {
96    a1.sample_rate == a2.sample_rate && is_same_audio_format(&a1.format, &a2.format)
97}
98
99/// Multiplies and divides a rational number,
100/// such as a frame duration, in place and reduces the result
101// TODO: use `const` when available: https://github.com/rust-lang/rust/issues/57349
102#[inline]
103pub fn muldiv_rational(num: &mut i64, den: &mut i64, mul: i64, div: i64) {
104    // do nothing if the rational number is invalid
105    if *den == 0 {
106        return;
107    }
108
109    *num *= mul;
110    *den *= div;
111    let mut a = *num;
112    let mut b = *den;
113    while b != 0 {
114        let t = a;
115        a = b;
116        b = t % b;
117    }
118    if a < 0 {
119        a = -a;
120    }
121
122    *num /= a;
123    *den /= a;
124}
125
126/// Reduces a rational number
127#[inline]
128pub fn reduce_rational(num: &mut i64, den: &mut i64) {
129    muldiv_rational(num, den, 1, 1);
130}
131
132/// Add two rational numbers and reduces the result
133#[inline]
134pub fn add_rational(num: &mut i64, den: &mut i64, mut addnum: i64, addden: i64) {
135    // Do nothing if the rational number is invalid
136    if *den == 0 {
137        return;
138    }
139
140    if *den == addden {
141        *num += addnum;
142    } else {
143        let temp = addden;
144        addnum *= *den;
145        // addden *= *den;
146        *num *= temp;
147        *den *= temp;
148
149        *num += addnum;
150
151        reduce_rational(num, den);
152    }
153}
154
155/// Converts an int64 to int with saturation, useful to silence warnings when reading
156/// int properties among other things
157#[inline]
158#[must_use]
159pub const fn int64_to_int_s(i: i64) -> c_int {
160    if i > c_int::MAX as i64 {
161        c_int::MAX
162    } else if i < c_int::MIN as i64 {
163        c_int::MIN
164    } else {
165        i as c_int
166    }
167}
168
169/// Converts a double to float with saturation, useful to silence warnings when reading
170/// float properties among other things
171#[inline]
172#[must_use]
173pub const fn double_to_float_s(d: f64) -> f32 {
174    d as f32
175}
176
177/// Copies bytes from one plane to another. Basically, it is memcpy in a loop.
178///
179/// # Safety
180/// `srcp` and `dstp` must be valid and not overlapping
181#[inline]
182pub unsafe fn bitblt(
183    dstp: *mut c_void,
184    dst_stride: isize,
185    srcp: *const c_void,
186    src_stride: isize,
187    row_size: usize,
188    height: usize,
189) {
190    if height != 0 {
191        if src_stride == dst_stride && src_stride == row_size as isize {
192            unsafe { dstp.copy_from_nonoverlapping(srcp, row_size * height) };
193        } else {
194            let mut srcp8 = srcp.cast::<u8>();
195            let mut dstp8 = dstp.cast::<u8>();
196            let mut i = 0;
197            while i < height {
198                unsafe { dstp8.copy_from_nonoverlapping(srcp8, row_size) };
199                srcp8 = srcp8.wrapping_offset(src_stride);
200                dstp8 = dstp8.wrapping_offset(dst_stride);
201                i += 1;
202            }
203        }
204    }
205}
206
207// Check if the frame dimensions are valid for a given format
208// returns non-zero for valid width and height
209#[inline]
210#[must_use]
211pub const fn are_valid_dimensions(fi: &VSVideoFormat, width: c_int, height: c_int) -> bool {
212    width % (1 << fi.sub_sampling_w) == 0 && height % (1 << fi.sub_sampling_h) == 0
213}