vapoursynth4_sys/vsscript.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// VSScript4.h
8//! `VSScript` provides a convenient wrapper for VapourSynth’s scripting interface(s),
9//! allowing the evaluation of `VapourSynth` scripts and retrieval of output clips.
10//!
11//! For reasons unknown, the `VSScript` library is called `VSScript` in Windows and
12//! `vapoursynth-script` everywhere else.
13//!
14//! At this time, `VapourSynth` scripts can be written only in Python (version 3).
15//!
16//! Here are a few users of the `VSScript` library:
17//!
18//! * [vspipe](https://github.com/vapoursynth/vapoursynth/blob/master/src/vspipe/vspipe.cpp)
19//! * [vsvfw](https://github.com/vapoursynth/vapoursynth/blob/master/src/vfw/vsvfw.cpp)
20//! * [an example program][1]
21//! * the video player [mpv]
22//!
23//! [1]: https://github.com/vapoursynth/vapoursynth/blob/master/sdk/vsscript_example.c
24//! [mpv]: https://github.com/mpv-player/mpv/blob/master/video/filter/vf_vapoursynth.c
25//!
26//! # Note
27//!
28//! If `libvapoursynth-script` is loaded with `dlopen()`, the `RTLD_GLOBAL` flag must be used.
29//! If not, Python won’t be able to import binary modules. This is due to Python’s design.
30
31#![cfg(feature = "vsscript")]
32
33use std::ffi::{c_char, c_int};
34
35use super::{VSAPI, VSCore, VSMap, VSNode, opaque_struct, vs_make_version};
36
37pub const VSSCRIPT_API_MAJOR: u16 = 4;
38pub const VSSCRIPT_API_MINOR: u16 = if cfg!(feature = "vsscript-43") {
39 3
40} else if cfg!(feature = "vsscript-42") {
41 2
42} else {
43 1
44};
45pub const VSSCRIPT_API_VERSION: i32 = vs_make_version(VSSCRIPT_API_MAJOR, VSSCRIPT_API_MINOR);
46
47opaque_struct!(
48 /// A script environment. All evaluation and communication with evaluated scripts happens
49 /// through a [`VSScript`] object.
50 VSScript
51);
52
53/// This struct is the way to access VSScript’s public API.
54#[allow(non_snake_case)]
55#[repr(C)]
56pub struct VSSCRIPTAPI {
57 /// Returns the api version provided by vsscript.
58 pub getApiVersion: unsafe extern "system-unwind" fn() -> c_int,
59
60 /// Retrieves the [`VSAPI`] struct. Exists mostly as a convenience so
61 /// the vapoursynth module doesn’t have to be explicitly loaded.
62 ///
63 /// This could return `NULL` if the `VapourSynth` library doesn’t
64 /// provide the requested version.
65 pub getVSAPI: unsafe extern "system-unwind" fn(version: c_int) -> *const VSAPI,
66
67 /// Creates an empty script environment that can be used to evaluate scripts.
68 /// Passing a pre-created core can be useful to have custom core creation flags,
69 /// log callbacks or plugins pre-loaded. Passing `NULL` will automatically create
70 /// a new core with default settings.
71 ///
72 /// Takes over ownership of the core regardless of success or failure.
73 /// Returns `NULL` on error.
74 pub createScript: unsafe extern "system-unwind" fn(core: *mut VSCore) -> *mut VSScript,
75
76 /// Retrieves the `VapourSynth` core that was created in the script environment.
77 /// If a `VapourSynth` core has not been created yet, it will be created now,
78 /// with the default options (see the [Python Reference][1]).
79 ///
80 /// [1]: http://www.vapoursynth.com/doc/pythonreference.html
81 ///
82 /// [`VSScript`] retains ownership of the returned core object.
83 ///
84 /// Returns `NULL` on error.
85 ///
86 /// Note: The core is valid as long as the environment exists
87 pub getCore: unsafe extern "system-unwind" fn(handle: *mut VSScript) -> *mut VSCore,
88
89 /// Evaluates a script contained in a C string. Can be called multiple times on
90 /// the same script environment to successively add more processing.
91 ///
92 /// # Arguments
93 ///
94 /// * `handle` - Pointer to a script environment.
95 ///
96 /// * `buffer` - The entire script to evaluate, as a C string.
97 ///
98 /// * `scriptFilename` - A name for the script, which will be displayed in error messages.
99 /// If this is `NULL`, the name "\<string\>" will be used.
100 ///
101 /// The special `__file__` variable will be set to `scriptFilename`'s absolute path
102 /// if this is not `NULL`.
103 ///
104 /// Returns non-zero in case of errors. The error message can be retrieved with
105 /// [`getError()`](Self::getError). If the script calls `sys.exit(code)`
106 /// the exit code can be retrieved with [`getExitCode()`](Self::getExitCode).
107 /// The working directory behavior can be changed by calling
108 /// [`evalSetWorkingDir()`](Self::evalSetWorkingDir) before this function.
109 ///
110 /// Note: calling any function other than [`getError()`](Self::getError) and
111 /// [`freeScript()`](Self::freeScript) on a [`VSScript`] object in the error state
112 /// will result in undefined behavior.
113 pub evaluateBuffer: unsafe extern "system-unwind" fn(
114 handle: *mut VSScript,
115 buffer: *const c_char,
116 scriptFilename: *const c_char,
117 ) -> c_int,
118
119 /// Evaluates a script contained in a file. This is a convenience function
120 /// which reads the script from a file for you. It will only read the first 16 MiB
121 /// which should be enough for everyone.
122 ///
123 /// Behaves the same as [`evaluateBuffer()`](Self::evaluateBuffer).
124 pub evaluateFile: unsafe extern "system-unwind" fn(
125 handle: *mut VSScript,
126 scriptFilename: *const c_char,
127 ) -> c_int,
128
129 /// Returns the error message from a script environment, or `NULL`, if there is no error.
130 ///
131 /// It is okay to pass `NULL`.
132 ///
133 /// `VSScript` retains ownership of the pointer and it is only guaranteed
134 /// to be valid until the next vsscript operation on the handle.
135 pub getError: unsafe extern "system-unwind" fn(handle: *mut VSScript) -> *const c_char,
136
137 /// Returns the exit code if the script calls `sys.exit(code)`, or 0,
138 /// if the script fails for other reasons or calls `sys.exit(0)`.
139 ///
140 /// It is okay to pass `NULL`.
141 pub getExitCode: unsafe extern "system-unwind" fn(handle: *mut VSScript) -> c_int,
142
143 /// Retrieves a variable from the script environment.
144 ///
145 /// If a `VapourSynth` core has not been created yet in the script environment,
146 /// one will be created now, with the default options (see the [Python Reference][1]).
147 ///
148 /// [1]: http://www.vapoursynth.com/doc/pythonreference.html
149 ///
150 /// # Arguments
151 ///
152 /// * `name` - Name of the variable to retrieve.
153 ///
154 /// * `dst` - Map where the variable’s value will be placed, with the key name.
155 ///
156 /// Returns non-zero on error.
157 pub getVariable: unsafe extern "system-unwind" fn(
158 handle: *mut VSScript,
159 name: *const c_char,
160 dst: *mut VSMap,
161 ) -> c_int,
162
163 /// Sets variables in the script environment.
164 ///
165 /// The variables are now available to the script.
166 ///
167 /// If a `VapourSynth` core has not been created yet in the script environment,
168 /// one will be created now, with the default options (see the [Python Reference][1]).
169 ///
170 /// [1]: http://www.vapoursynth.com/doc/pythonreference.html
171 ///
172 /// # Arguments
173 ///
174 /// * `vars` - Map containing the variables to set.
175 ///
176 /// Returns non-zero on error.
177 pub setVariables:
178 unsafe extern "system-unwind" fn(handle: *mut VSScript, vars: *const VSMap) -> c_int,
179
180 /// Retrieves a node from the script environment. A node in the script must have been
181 /// marked for output with the requested `index`.
182 ///
183 /// The returned node has its reference count incremented by one.
184 ///
185 /// Returns `NULL` if there is no node at the requested index.
186 pub getOutputNode:
187 unsafe extern "system-unwind" fn(handle: *mut VSScript, index: c_int) -> *mut VSNode,
188 /// Retrieves an alpha node from the script environment. A node with associated alpha
189 /// in the script must have been marked for output with the requested `index`.
190 ///
191 /// The returned node has its reference count incremented by one.
192 ///
193 /// Returns `NULL` if there is no alpha node at the requested index.
194 pub getOutputAlphaNode:
195 unsafe extern "system-unwind" fn(handle: *mut VSScript, index: c_int) -> *mut VSNode,
196 /// Retrieves the alternative output mode settings from the script.
197 /// This value has no fixed meaning but in vspipe and vsvfw it indicates
198 /// that alternate output formats should be used when multiple ones are available.
199 /// It is up to the client application to define the exact meaning
200 /// or simply disregard it completely.
201 ///
202 /// Returns 0 if there is no alt output mode set.
203 pub getAltOutputMode:
204 unsafe extern "system-unwind" fn(handle: *mut VSScript, index: c_int) -> c_int,
205
206 /// Frees a script environment. `handle` is no longer usable.
207 ///
208 /// * Cancels any clips set for output in the script environment.
209 /// * Clears any variables set in the script environment.
210 /// * Clears the error message from the script environment, if there is one.
211 /// * Frees the `VapourSynth` core used in the script environment, if there is one.
212 /// * Since this function frees the `VapourSynth` core, it must be called only after
213 /// all frame requests are finished and all objects obtained from the script
214 /// have been freed (frames, nodes, etc).
215 ///
216 /// It is safe to pass `NULL`.
217 pub freeScript: unsafe extern "system-unwind" fn(handle: *mut VSScript),
218
219 /// Set whether or not the working directory is temporarily changed to the same location
220 /// as the script file when [`evaluateFile()`](Self::evaluateFile) is called. Off by default.
221 pub evalSetWorkingDir: unsafe extern "system-unwind" fn(handle: *mut VSScript, setCWD: c_int),
222
223 /// Write a list of set output index values to dst but at most size values.
224 /// Always returns the total number of available output index values.
225 #[cfg(feature = "vsscript-42")]
226 pub getAvailableOutputNodes: unsafe extern "system-unwind" fn(
227 handle: *mut VSScript,
228 size: c_int,
229 dst: *mut c_int,
230 ) -> c_int,
231}
232
233// Since R74 no `VapourSynth` distribution ships an import library, so Windows
234// binds the DLL directly by name instead of going through one.
235#[cfg(feature = "link-vsscript")]
236#[cfg_attr(windows, link(name = "vsscript", kind = "raw-dylib"))]
237#[cfg_attr(not(windows), link(name = "vapoursynth-script"))]
238unsafe extern "system-unwind" {
239 /// Returns a struct containing function pointer for the api.
240 /// Will return `NULL` is the specified version isn’t supported.
241 ///
242 /// It is recommended to always pass [`VSSCRIPT_API_VERSION`].
243 pub fn getVSScriptAPI(version: c_int) -> *const VSSCRIPTAPI;
244
245 /// Returns the error message from the last [`getVSScriptAPI()`] failure,
246 /// or an empty string on success.
247 #[cfg(feature = "vsscript-43")]
248 pub fn getVSScriptAPILastError() -> *const c_char;
249}