alpha2_plugin_abi/lib.rs
1//! The `alpha2-plugin/1` ABI, covering what a plugin exports, what a host gives it, and the binary
2//! records a plugin emits values in. Native plugins exchange the `#[repr(C)]` tables below; wasm
3//! plugins use the export and import names in [`wasm`]. Both send emits as [`records`].
4
5#![no_std]
6
7extern crate alloc;
8
9pub mod records;
10
11use core::ffi::c_void;
12
13/// The ABI a manifest's `abi` names.
14pub const ABI: &str = "alpha2-plugin/1";
15
16/// The major version in [`ABI`], which the native tables and `alpha2_abi` carry.
17pub const ABI_MAJOR: u32 = 1;
18
19/// A plugin call returns one of these.
20pub mod status {
21 /// The call succeeded.
22 pub const OK: i32 = 0;
23 /// The extractor failed; the message it passed to `fail` says why.
24 pub const FAILED: i32 = 1;
25 /// A panic was caught at the boundary.
26 pub const TRAPPED: i32 = 2;
27}
28
29/// `format` of a video call.
30pub mod video_format {
31 /// Four bytes per pixel, straight alpha, display-encoded sRGB.
32 pub const RGBA8: u32 = 0;
33 /// One byte per pixel: BT.709 luma of the display-encoded picture.
34 pub const LUMA8: u32 = 1;
35}
36
37/// `direction` of a pass call.
38pub mod direction {
39 /// Times increase through the pass.
40 pub const FORWARD: u32 = 0;
41 /// Times decrease through the pass.
42 pub const BACKWARD: u32 = 1;
43}
44
45/// `level` of a log call.
46pub mod log_level {
47 /// Detail for debugging a plugin.
48 pub const DEBUG: u32 = 0;
49 /// Something the user may want to know.
50 pub const INFO: u32 = 1;
51 /// Something went wrong without failing the run.
52 pub const WARN: u32 = 2;
53}
54
55/// The symbol a native plugin exports, NUL-terminated: `extern "C" fn() -> *const PluginV1`.
56pub const NATIVE_ENTRY: &[u8] = b"alpha2_plugin_v1\0";
57
58/// What a host gives a native plugin instance. Every function takes `context` first.
59///
60/// Pointers passed to these functions are borrowed for the call alone. `/1` grows only by fields
61/// appended here; a plugin reads `abi` and `size` first, refuses a table shorter than
62/// [`HOST_V1_BASE_BYTES`] and uses only the fields `size` covers.
63#[repr(C)]
64#[derive(Clone, Copy, Debug)]
65pub struct HostV1 {
66 /// [`ABI_MAJOR`].
67 pub abi: u32,
68 /// Bytes of the table as the host that filled it knows it.
69 pub size: u32,
70 /// The host's own state for this instance.
71 pub context: *mut c_void,
72 /// Takes a batch of [`records`]; returns [`status::OK`], or [`status::FAILED`] once the
73 /// session has refused an emit and the plugin should stop.
74 pub emit: unsafe extern "C" fn(context: *mut c_void, records: *const u8, len: usize) -> i32,
75 /// The resource `name` (UTF-8) and its length, or null when the manifest declares none by that
76 /// name. The bytes stay valid until the instance is destroyed.
77 pub resource: unsafe extern "C" fn(
78 context: *mut c_void,
79 name: *const u8,
80 name_len: usize,
81 len: *mut usize,
82 ) -> *const u8,
83 /// Records why the call about to return [`status::FAILED`] failed (UTF-8).
84 pub fail: unsafe extern "C" fn(context: *mut c_void, message: *const u8, len: usize),
85 /// Writes a diagnostic (UTF-8) at a [`log_level`].
86 pub log: unsafe extern "C" fn(context: *mut c_void, level: u32, message: *const u8, len: usize),
87 /// The absolute path (UTF-8) of the supplied file, filled directory slot or directory under
88 /// one named `name`, and its length; null for a pinned resource, an empty or undeclared name,
89 /// or a path that is not UTF-8. Valid until the instance is destroyed. Appended within `/1`.
90 pub resource_path: unsafe extern "C" fn(
91 context: *mut c_void,
92 name: *const u8,
93 name_len: usize,
94 len: *mut usize,
95 ) -> *const u8,
96}
97
98/// Bytes of [`HostV1`] as the first `/1` hosts filled it, before any appended field; every host
99/// table is at least this long.
100pub const HOST_V1_BASE_BYTES: usize = core::mem::offset_of!(HostV1, resource_path);
101
102/// The static table a native plugin's [`NATIVE_ENTRY`] returns, which outlives every instance.
103///
104/// Calls other than `describe` and `create` take the instance `create` returned. Pointers passed to
105/// the plugin are borrowed for the call alone. Every entry is required. `/1` grows only by fields
106/// appended here; a host reads `abi` and `size` first and refuses a table shorter than its own.
107#[repr(C)]
108#[derive(Clone, Copy, Debug)]
109pub struct PluginV1 {
110 /// [`ABI_MAJOR`].
111 pub abi: u32,
112 /// Bytes of the table as the plugin that filled it knows it.
113 pub size: u32,
114 /// The descriptor as UTF-8 JSON and its length; static, valid while the library is loaded.
115 pub describe: unsafe extern "C" fn(len: *mut usize) -> *const u8,
116 /// A new instance that calls `host`'s functions; null when it cannot be made. `host` stays
117 /// valid until `destroy`.
118 pub create: unsafe extern "C" fn(host: *const HostV1) -> *mut c_void,
119 /// Frees an instance; no call takes it afterwards.
120 pub destroy: unsafe extern "C" fn(instance: *mut c_void),
121 /// Starts the run with its context, UTF-8 JSON.
122 pub begin: unsafe extern "C" fn(instance: *mut c_void, context: *const u8, len: usize) -> i32,
123 /// Starts pass `pass` in a [`direction`].
124 pub pass: unsafe extern "C" fn(instance: *mut c_void, pass: u32, direction: u32) -> i32,
125 /// One frame: `len` bytes of rows top first, tightly packed in a [`video_format`].
126 pub video: unsafe extern "C" fn(
127 instance: *mut c_void,
128 time: i64,
129 width: u32,
130 height: u32,
131 format: u32,
132 data: *const u8,
133 len: usize,
134 ) -> i32,
135 /// One block of `count` samples, planar, channel after channel.
136 pub audio: unsafe extern "C" fn(
137 instance: *mut c_void,
138 start: i64,
139 start_sample: i64,
140 frames: u32,
141 rate: u32,
142 channels: u32,
143 samples: *const f32,
144 count: usize,
145 ) -> i32,
146 /// Ends the run; whole-range work emits here.
147 pub end: unsafe extern "C" fn(instance: *mut c_void) -> i32,
148}
149
150/// Names a wasm plugin exports and imports. Pointers and lengths are `i32` offsets into the
151/// module's exported `memory`.
152pub mod wasm {
153 /// The module the host functions are imported from.
154 pub const IMPORT_MODULE: &str = "alpha2";
155 /// `() -> i32`: [`super::ABI_MAJOR`].
156 pub const ABI: &str = "alpha2_abi";
157 /// `(size: i32, align: i32) -> i32`: memory the host writes call arguments into.
158 pub const ALLOC: &str = "alpha2_alloc";
159 /// `(ptr: i32, size: i32, align: i32)`: frees what `alpha2_alloc` returned.
160 pub const FREE: &str = "alpha2_free";
161 /// `() -> i64`: the descriptor JSON's offset in the low 32 bits and its length in the high 32.
162 pub const DESCRIBE: &str = "alpha2_describe";
163 /// `(ptr: i32, len: i32) -> i32`.
164 pub const BEGIN: &str = "alpha2_begin";
165 /// `(pass: i32, direction: i32) -> i32`.
166 pub const PASS: &str = "alpha2_pass";
167 /// `(time: i64, width: i32, height: i32, format: i32, ptr: i32, len: i32) -> i32`.
168 pub const VIDEO: &str = "alpha2_video";
169 /// `(start: i64, start_sample: i64, frames: i32, rate: i32, channels: i32, ptr: i32,
170 /// count: i32) -> i32`; `count` samples of 4 bytes each.
171 pub const AUDIO: &str = "alpha2_audio";
172 /// `() -> i32`.
173 pub const END: &str = "alpha2_end";
174 /// The exported memory.
175 pub const MEMORY: &str = "memory";
176 /// Import `(ptr: i32, len: i32) -> i32`, as [`super::HostV1::emit`].
177 pub const EMIT: &str = "emit";
178 /// Import `(name: i32, name_len: i32) -> i64`: the resource's length, or -1.
179 pub const RESOURCE_SIZE: &str = "resource_size";
180 /// Import `(name: i32, name_len: i32, dst: i32, dst_len: i32) -> i32`: copies the resource;
181 /// 0 when it was copied whole.
182 pub const RESOURCE_READ: &str = "resource_read";
183 /// Import `(ptr: i32, len: i32)`, as [`super::HostV1::fail`].
184 pub const FAIL: &str = "fail";
185 /// Import `(level: i32, ptr: i32, len: i32)`, as [`super::HostV1::log`].
186 pub const LOG: &str = "log";
187}
188
189#[cfg(test)]
190mod tests;