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 and uses only the fields `size` covers.
62#[repr(C)]
63#[derive(Clone, Copy, Debug)]
64pub struct HostV1 {
65 /// [`ABI_MAJOR`].
66 pub abi: u32,
67 /// Bytes of the table as the host that filled it knows it.
68 pub size: u32,
69 /// The host's own state for this instance.
70 pub context: *mut c_void,
71 /// Takes a batch of [`records`]; returns [`status::OK`], or [`status::FAILED`] once the
72 /// session has refused an emit and the plugin should stop.
73 pub emit: unsafe extern "C" fn(context: *mut c_void, records: *const u8, len: usize) -> i32,
74 /// The resource `name` (UTF-8) and its length, or null when the manifest declares none by that
75 /// name. The bytes stay valid until the instance is destroyed.
76 pub resource: unsafe extern "C" fn(
77 context: *mut c_void,
78 name: *const u8,
79 name_len: usize,
80 len: *mut usize,
81 ) -> *const u8,
82 /// Records why the call about to return [`status::FAILED`] failed (UTF-8).
83 pub fail: unsafe extern "C" fn(context: *mut c_void, message: *const u8, len: usize),
84 /// Writes a diagnostic (UTF-8) at a [`log_level`].
85 pub log: unsafe extern "C" fn(context: *mut c_void, level: u32, message: *const u8, len: usize),
86}
87
88/// The static table a native plugin's [`NATIVE_ENTRY`] returns, which outlives every instance.
89///
90/// Calls other than `describe` and `create` take the instance `create` returned. Pointers passed to
91/// the plugin are borrowed for the call alone. Every entry is required. `/1` grows only by fields
92/// appended here; a host reads `abi` and `size` first and refuses a table shorter than its own.
93#[repr(C)]
94#[derive(Clone, Copy, Debug)]
95pub struct PluginV1 {
96 /// [`ABI_MAJOR`].
97 pub abi: u32,
98 /// Bytes of the table as the plugin that filled it knows it.
99 pub size: u32,
100 /// The descriptor as UTF-8 JSON and its length; static, valid while the library is loaded.
101 pub describe: unsafe extern "C" fn(len: *mut usize) -> *const u8,
102 /// A new instance that calls `host`'s functions; null when it cannot be made. `host` stays
103 /// valid until `destroy`.
104 pub create: unsafe extern "C" fn(host: *const HostV1) -> *mut c_void,
105 /// Frees an instance; no call takes it afterwards.
106 pub destroy: unsafe extern "C" fn(instance: *mut c_void),
107 /// Starts the run with its context, UTF-8 JSON.
108 pub begin: unsafe extern "C" fn(instance: *mut c_void, context: *const u8, len: usize) -> i32,
109 /// Starts pass `pass` in a [`direction`].
110 pub pass: unsafe extern "C" fn(instance: *mut c_void, pass: u32, direction: u32) -> i32,
111 /// One frame: `len` bytes of rows top first, tightly packed in a [`video_format`].
112 pub video: unsafe extern "C" fn(
113 instance: *mut c_void,
114 time: i64,
115 width: u32,
116 height: u32,
117 format: u32,
118 data: *const u8,
119 len: usize,
120 ) -> i32,
121 /// One block of `count` samples, planar, channel after channel.
122 pub audio: unsafe extern "C" fn(
123 instance: *mut c_void,
124 start: i64,
125 start_sample: i64,
126 frames: u32,
127 rate: u32,
128 channels: u32,
129 samples: *const f32,
130 count: usize,
131 ) -> i32,
132 /// Ends the run; whole-range work emits here.
133 pub end: unsafe extern "C" fn(instance: *mut c_void) -> i32,
134}
135
136/// Names a wasm plugin exports and imports. Pointers and lengths are `i32` offsets into the
137/// module's exported `memory`.
138pub mod wasm {
139 /// The module the host functions are imported from.
140 pub const IMPORT_MODULE: &str = "alpha2";
141 /// `() -> i32`: [`super::ABI_MAJOR`].
142 pub const ABI: &str = "alpha2_abi";
143 /// `(size: i32, align: i32) -> i32`: memory the host writes call arguments into.
144 pub const ALLOC: &str = "alpha2_alloc";
145 /// `(ptr: i32, size: i32, align: i32)`: frees what `alpha2_alloc` returned.
146 pub const FREE: &str = "alpha2_free";
147 /// `() -> i64`: the descriptor JSON's offset in the low 32 bits and its length in the high 32.
148 pub const DESCRIBE: &str = "alpha2_describe";
149 /// `(ptr: i32, len: i32) -> i32`.
150 pub const BEGIN: &str = "alpha2_begin";
151 /// `(pass: i32, direction: i32) -> i32`.
152 pub const PASS: &str = "alpha2_pass";
153 /// `(time: i64, width: i32, height: i32, format: i32, ptr: i32, len: i32) -> i32`.
154 pub const VIDEO: &str = "alpha2_video";
155 /// `(start: i64, start_sample: i64, frames: i32, rate: i32, channels: i32, ptr: i32,
156 /// count: i32) -> i32`; `count` samples of 4 bytes each.
157 pub const AUDIO: &str = "alpha2_audio";
158 /// `() -> i32`.
159 pub const END: &str = "alpha2_end";
160 /// The exported memory.
161 pub const MEMORY: &str = "memory";
162 /// Import `(ptr: i32, len: i32) -> i32`, as [`super::HostV1::emit`].
163 pub const EMIT: &str = "emit";
164 /// Import `(name: i32, name_len: i32) -> i64`: the resource's length, or -1.
165 pub const RESOURCE_SIZE: &str = "resource_size";
166 /// Import `(name: i32, name_len: i32, dst: i32, dst_len: i32) -> i32`: copies the resource;
167 /// 0 when it was copied whole.
168 pub const RESOURCE_READ: &str = "resource_read";
169 /// Import `(ptr: i32, len: i32)`, as [`super::HostV1::fail`].
170 pub const FAIL: &str = "fail";
171 /// Import `(level: i32, ptr: i32, len: i32)`, as [`super::HostV1::log`].
172 pub const LOG: &str = "log";
173}
174
175#[cfg(test)]
176mod tests;