Skip to main content

Module extract

Module extract 

Source
Expand description

The extractor contract. Code outside the engine is supplied frames or audio and returns primitives that a project records as data sets.

An extractor runs ahead of time over a range, never while a frame renders. The host drives an ExtractSession. It supplies each Need from its own decoders or renderer, hands each Delivery to the extractor, and passes the extractor’s Emits back for the session to validate. ExtractSession::finish returns sorted, validated data sets. Rust extractors implement Extractor and run through drive; other surfaces wrap the same step machine.

Source frames are delivered display-oriented, contain-fit to at most size on the long edge (delivered_size), as straight-alpha sRGB rgba8 or BT.709 luma8 (luma8_from_rgba8). The module uses plain data only; the render path never calls it.

§A minimal extractor

The mean brightness of each timeline frame, as a scalars data set:

use alpha2_engine::extract::{
    AudioInput, Descriptor, Emitter, ExtractRequest, ExtractSession, Extractor,
    ExtractorError, Need, PassInfo, RunContext, SuppliedAudio, SuppliedFrame, Supplier,
    VideoInput, delivered_size, drive,
};
use serde_json::json;

struct Brightness(Descriptor);

impl Extractor for Brightness {
    fn describe(&self) -> &Descriptor {
        &self.0
    }
    fn begin(&mut self, _context: &RunContext) -> Result<(), ExtractorError> {
        Ok(())
    }
    fn pass(&mut self, _pass: &PassInfo) -> Result<(), ExtractorError> {
        Ok(())
    }
    fn video(&mut self, frame: &VideoInput, out: &mut Emitter) -> Result<(), ExtractorError> {
        let sum: u64 = frame.bytes.iter().map(|&value| u64::from(value)).sum();
        let mean = sum as f32 / frame.bytes.len() as f32 / 255.0;
        out.sample("level", frame.time, &[Some(mean)]);
        Ok(())
    }
    fn audio(&mut self, _block: &AudioInput, _out: &mut Emitter) -> Result<(), ExtractorError> {
        Ok(())
    }
    fn end(&mut self, _out: &mut Emitter) -> Result<(), ExtractorError> {
        Ok(())
    }
}

/// Grey frames; a host supplies decoded or rendered ones.
struct Grey;

impl Supplier for Grey {
    fn frame(&mut self, need: &Need) -> Result<Option<SuppliedFrame>, String> {
        let Need::TimelineFrame { time, size, .. } = need else {
            return Err("timeline frames only".into());
        };
        let (width, height) = delivered_size((64, 36), *size);
        let rgba = vec![128; (width * height * 4) as usize];
        Ok(Some(SuppliedFrame { pts: *time, width, height, rgba }))
    }
    fn audio(&mut self, _need: &Need) -> Result<SuppliedAudio, String> {
        Err("no audio".into())
    }
}

let project = alpha2_model::Project::from_json(&json!({
    "schema_version": 1, "id": "p", "name": "P",
    "settings": { "width": 64, "height": 36, "frame_rate": { "num": 30, "den": 1 },
                  "sample_rate": 48_000 },
    "sources": [], "timeline": { "tracks": [] }
}).to_string())?;
let descriptor: Descriptor = serde_json::from_value(json!({
    "id": "com.example.brightness", "version": 1,
    "input": { "media": "video", "from": ["timeline"], "format": "luma8" },
    "outputs": [{ "name": "level", "kind": "scalars", "channels": ["y"] }]
}))?;
let request: ExtractRequest = serde_json::from_value(json!({
    "extractor": "com.example.brightness", "version": 1,
    "input": { "from": "timeline", "range": { "start": 0, "end": 12_000 }, "size": 32 },
    "outputs": { "level": "brightness" }
}))?;
let session = ExtractSession::new(&project, &descriptor, request)?;
let sets = drive(session, &mut Brightness(descriptor), &mut Grey)?;
assert_eq!(sets[0].id.as_str(), "brightness");

§Limits

Structs§

AudioInput
An audio block handed to the extractor.
Descriptor
What an extractor declares, with its identity, the input it reads and the outputs it emits.
Emitter
Collects what an extractor emits for one call; the session validates it afterwards.
ExtractRequest
One run of an extractor.
ExtractSession
One run of an extractor over a range, driven by the host.
ExtractorError
An extractor’s own failure, carried to the caller as is.
FramePlace
Where a supplier may write a frame’s pixels: memory the extractor offers.
OutputSpec
One output an extractor emits.
Pass
One pass of a run, from a start time in one direction.
PassInfo
A pass beginning, as an extractor is told.
PassProgress
Completed and total deliveries of one pass; the total is unknown for source frames.
PinnedResource
A file the manifest pins.
PluginAbout
What an interface shows about a plugin: plain text, never markup, without control characters.
PluginArtifact
One build of a plugin.
PluginManifest
A plugin’s manifest.
PluginResource
A file a plugin reads: pinned by the manifest, or supplied by the caller at load time.
RequestInput
What a run reads.
RunContext
What the extractor is told when the run begins.
SuppliedAudio
A supplied audio block: planar f32, channel after channel.
SuppliedFrame
A supplied video frame: tightly packed straight-alpha sRGB RGBA8 at its delivered size.
SuppliedSlot
A resource slot the caller fills at load time.
VideoInput
A frame handed to the extractor.

Enums§

ArtifactKind
What kind of code an artifact is.
AudioChannels
Which channels delivered audio has.
AudioFormat
How delivered audio is stored.
Delivery
An input handed to the extractor.
Direction
The direction of a pass.
DriveError
Why a run did not complete.
Emit
One value an extractor emits.
FrameFrom
Where a source frame is asked for, relative to what the pass has seen.
InputFrom
Which clock an extractor reads on.
InputSpec
What an extractor reads.
ManifestError
Why a manifest was refused.
Need
What the host must supply next.
PluginKind
The contract a plugin implements.
Reference
What a manifest’s url names.
RequestError
Why a session could not start.
ResourceSource
Where a resource’s bytes come from.
SessionError
Why the session refused something.
SlotShape
What the caller names for a slot.
Step
What the session asks for or hands over next.
Suggest
How an output is usually used.
VideoFormat
How delivered video frames are packed.

Constants§

BLOCK_FRAMES_RANGE
The range of block_frames a descriptor may declare.
DEFAULT_BLOCK_FRAMES
Audio frames per delivered block when the descriptor gives none.
DEFAULT_MAX_SIZE
The longest edge of a delivered video frame when the descriptor gives none.
MAX_ABOUT_AUTHOR_CHARS
The longest about.author, in characters.
MAX_ABOUT_HOMEPAGE_CHARS
The longest about.homepage, in characters.
MAX_ABOUT_LICENSE_CHARS
The longest about.license, in characters.
MAX_ABOUT_NAME_CHARS
The longest about.name, in characters.
MAX_ABOUT_SUMMARY_CHARS
The longest about.summary, in characters.
MAX_EXTRACTOR_OUTPUTS
Most outputs one extractor declares.
MAX_EXTRACTOR_PASSES
Most passes one request asks for.
MAX_MANIFEST_BYTES
The longest manifest text accepted.
MAX_OUTPUT_DESCRIPTION_CHARS
The longest output description, in characters.
MAX_OUTPUT_LABEL_CHARS
The longest output label, in characters.
MAX_PLUGIN_ARTIFACTS
Most artifacts one manifest lists.
MAX_PLUGIN_ARTIFACT_BYTES
The longest artifact a manifest may pin, in bytes.
MAX_PLUGIN_RESOURCES
Most resources one manifest lists.
MAX_PLUGIN_RESOURCES_TOTAL_BYTES
The most bytes a manifest’s resources may pin together.
MAX_PLUGIN_RESOURCE_BYTES
The longest resource a manifest may pin, in bytes: what a wasm module’s memory can address.
MAX_PLUGIN_SLOTS
The most resource slots one manifest declares, inside MAX_PLUGIN_RESOURCES.
MAX_SLOT_ACCEPT
The most file name suffixes one slot’s accept lists.
MAX_SLOT_ACCEPT_BYTES
The longest file name suffix in a slot’s accept, in bytes.
MAX_SLOT_NAME_BYTES
The longest slot name, in bytes.
PLUGIN_MANIFEST_SCHEMA_ID
Where the plugin manifest schema is published.
WASM_TARGET
The target of a wasm artifact.

Traits§

Extractor
A function that is supplied frames or audio and returns primitives. Runs ahead of time over a range; what it emits is recorded as data sets.
Supplier
Supplies the inputs a session asks for: decoded and presented frames, or audio.

Functions§

client_context_json
The run context as clients are told it at begin.
delivered_size
The size a frame of displayed size (width, height) is delivered at for a request size: contain-fit so the long edge is at most size, never upscaled, each edge rounded half up and at least one pixel.
displayed_size
The displayed size of a decoded frame, its visible rectangle widened by the pixel aspect ratio (rounded half up) and turned upright.
drive
Runs extractor through session, supplying inputs from supplier, and returns the session’s data sets.
emit_from_json
An emit as clients write it, { output, time, values } for samples (null a lost channel), { output, start, end, text, speaker? } for words, and for intervals { output, start, end, label?, score? }.
is_slot_name
Whether name is a slot name: 1 to 64 of A-Z a-z 0-9 . _ -, not starting with ..
luma8_from_rgba8
BT.709 Y’ of straight-alpha sRGB-encoded RGBA8 pixels, rounded half up; alpha is ignored.
mono_downmix
The mixer’s mono fold of planes, whose speakers are speakers. Each plane is weighted by the mean of its stereo weights and summed in plane order. None for no planes or too many.
present_for_extraction
A decoded frame presented on the CPU renderer as an extraction delivery: display-oriented, contain-fit to at most size on the long edge, straight-alpha sRGB RGBA8. HDR is tone-mapped with the default tone map.
present_for_extraction_into
present_for_extraction, writing the pixels into the memory place gives when it gives some; the returned frame’s rgba is then empty.
reference
Reads a manifest’s url as a path under the manifest’s directory or an https: URL.
supply_frame
Answers a frame need of session from supplier, in memory extractor offers when it offers some: the supplier writes RGBA frames there, the session a frame’s luma; drive does this for every frame need.