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