vapoursynth4_sys/
helper.rs1#![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
18pub const VSH_STD_PLUGIN_ID: &CStr = c"com.vapoursynth.std";
20pub const VSH_RESIZE_PLUGIN_ID: &CStr = c"com.vapoursynth.resize";
22pub const VSH_TEXT_PLUGIN_ID: &CStr = c"com.vapoursynth.text";
24
25#[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#[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 #[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#[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#[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#[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#[inline]
103pub fn muldiv_rational(num: &mut i64, den: &mut i64, mul: i64, div: i64) {
104 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#[inline]
128pub fn reduce_rational(num: &mut i64, den: &mut i64) {
129 muldiv_rational(num, den, 1, 1);
130}
131
132#[inline]
134pub fn add_rational(num: &mut i64, den: &mut i64, mut addnum: i64, addden: i64) {
135 if *den == 0 {
137 return;
138 }
139
140 if *den == addden {
141 *num += addnum;
142 } else {
143 let temp = addden;
144 addnum *= *den;
145 *num *= temp;
147 *den *= temp;
148
149 *num += addnum;
150
151 reduce_rational(num, den);
152 }
153}
154
155#[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#[inline]
172#[must_use]
173pub const fn double_to_float_s(d: f64) -> f32 {
174 d as f32
175}
176
177#[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#[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}