use std::collections::BTreeMap;
use std::num::NonZeroU64;
use std::path::{Path, PathBuf};
use serde::Serialize;
#[cfg(feature = "native")]
use crate::diagnostic::Result;
use crate::model::{
AudioDomain, ExactNumber, FrameCount, FrameRange, ImageFit, NodeId, ValueType, VideoDomain,
};
#[cfg(feature = "native")]
use crate::model::{AudioSpec, VideoSpec};
use crate::semantic::SourceOrigin;
#[cfg(feature = "native")]
use crate::source::SourceFile;
#[cfg(feature = "native")]
use super::policy::RenderPolicy;
use super::tools::ExternalToolIdentity;
#[cfg(feature = "native")]
use super::tools::{self, ToolIdentity};
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum PreparedExternalParameterValue {
Integer(i64),
Keyword(String),
File(PreparedAsset),
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum PreparedExternalArgument {
Text(String),
File(PreparedAsset),
}
#[derive(Clone, Debug)]
#[cfg(feature = "native")]
pub struct PreparedPlan {
format_version: u32,
engine_version: String,
semantic_hash: String,
render_policy: RenderPolicy,
video: VideoSpec,
audio: AudioSpec,
nodes: Vec<PreparedNode>,
result: NodeId,
named_values: BTreeMap<String, NodeId>,
output: PathBuf,
manifest: PathBuf,
ffmpeg: ToolIdentity,
ffprobe: ToolIdentity,
execution_namespace: String,
entrypoint_source: SourceFile,
source_paths: Vec<PathBuf>,
}
#[cfg(feature = "native")]
impl PreparedPlan {
#[expect(
clippy::too_many_arguments,
reason = "the sole plan constructor makes every identity-bearing and publication field explicit without a duplicate builder"
)]
pub(super) fn new(
format_version: u32,
engine_version: String,
semantic_hash: String,
render_policy: RenderPolicy,
video: VideoSpec,
audio: AudioSpec,
nodes: Vec<PreparedNode>,
result: NodeId,
named_values: BTreeMap<String, NodeId>,
output: PathBuf,
manifest: PathBuf,
ffmpeg: ToolIdentity,
ffprobe: ToolIdentity,
execution_namespace: String,
entrypoint_source: SourceFile,
source_paths: Vec<PathBuf>,
) -> Self {
Self {
format_version,
engine_version,
semantic_hash,
render_policy,
video,
audio,
nodes,
result,
named_values,
output,
manifest,
ffmpeg,
ffprobe,
execution_namespace,
entrypoint_source,
source_paths,
}
}
pub(crate) const fn format_version(&self) -> u32 {
self.format_version
}
pub(crate) fn engine_version(&self) -> &str {
&self.engine_version
}
pub fn prepared_json(&self) -> Result<String> {
crate::format::prepared_json::prepared_plan(self)
}
#[must_use]
pub fn semantic_hash(&self) -> &str {
&self.semantic_hash
}
#[must_use]
pub fn video(&self) -> &VideoSpec {
&self.video
}
#[must_use]
pub fn audio(&self) -> &AudioSpec {
&self.audio
}
#[must_use]
pub fn nodes(&self) -> &[PreparedNode] {
&self.nodes
}
#[must_use]
pub const fn result(&self) -> NodeId {
self.result
}
#[must_use]
pub fn named_values(&self) -> &BTreeMap<String, NodeId> {
&self.named_values
}
#[must_use]
pub fn output(&self) -> &Path {
&self.output
}
#[must_use]
pub fn manifest(&self) -> &Path {
&self.manifest
}
pub(crate) fn ffmpeg(&self) -> &ToolIdentity {
&self.ffmpeg
}
pub(crate) const fn render_policy(&self) -> RenderPolicy {
self.render_policy
}
pub(crate) fn ffprobe(&self) -> &ToolIdentity {
&self.ffprobe
}
pub(crate) fn execution_namespace(&self) -> &str {
&self.execution_namespace
}
pub(crate) fn verify_tool_identities(&self) -> Result<()> {
tools::verify_tool_identity(&self.ffmpeg, "FFmpeg")?;
tools::verify_tool_identity(&self.ffprobe, "FFprobe")
}
pub(crate) const fn entrypoint_source(&self) -> &SourceFile {
&self.entrypoint_source
}
pub(crate) fn source_paths(&self) -> &[PathBuf] {
&self.source_paths
}
}
#[derive(Clone, Debug)]
pub struct PreparedNode {
id: NodeId,
media: PreparedMedia,
origin: SourceOrigin,
fingerprint: String,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(tag = "value_type", rename_all = "snake_case")]
pub(crate) enum WorkingArtifactContract<'a> {
Video {
video: &'a VideoDomain,
audio: &'a AudioDomain,
},
Audio {
audio: &'a AudioDomain,
},
}
#[derive(Clone, Copy, Debug)]
#[cfg(feature = "native")]
pub(crate) enum PreparedResource<'a> {
Asset {
asset: &'a PreparedAsset,
role: &'static str,
},
ExternalExecutable(&'a ExternalToolIdentity),
}
#[cfg(feature = "native")]
impl<'a> PreparedResource<'a> {
pub(crate) fn path(self) -> &'a Path {
match self {
Self::Asset { asset, .. } => asset.source_path(),
Self::ExternalExecutable(executable) => executable.executable(),
}
}
pub(crate) const fn role(self) -> &'static str {
match self {
Self::Asset { role, .. } => role,
Self::ExternalExecutable(_) => "external executable",
}
}
}
#[derive(Clone, Debug)]
pub(super) enum PreparedMedia {
Video {
kind: PreparedVideoKind,
domain: VideoDomain,
working_audio: AudioDomain,
has_audio: bool,
},
Audio {
kind: PreparedAudioKind,
domain: AudioDomain,
},
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum PreparedNodeMedia<'a> {
Video {
kind: &'a PreparedVideoKind,
domain: &'a VideoDomain,
has_audio: bool,
},
Audio {
kind: &'a PreparedAudioKind,
domain: &'a AudioDomain,
},
}
impl PreparedNode {
pub(super) fn new(
id: NodeId,
media: PreparedMedia,
origin: SourceOrigin,
fingerprint: String,
) -> Self {
Self {
id,
media,
origin,
fingerprint,
}
}
pub(super) const fn prepared_media(&self) -> &PreparedMedia {
&self.media
}
pub(crate) const fn artifact_contract(&self) -> WorkingArtifactContract<'_> {
self.media.artifact_contract()
}
#[must_use]
pub const fn id(&self) -> NodeId {
self.id
}
#[must_use]
pub const fn media(&self) -> PreparedNodeMedia<'_> {
match &self.media {
PreparedMedia::Video {
kind,
domain,
has_audio,
..
} => PreparedNodeMedia::Video {
kind,
domain,
has_audio: *has_audio,
},
PreparedMedia::Audio { kind, domain } => PreparedNodeMedia::Audio { kind, domain },
}
}
#[must_use]
pub const fn video_kind(&self) -> Option<&PreparedVideoKind> {
match &self.media {
PreparedMedia::Video { kind, .. } => Some(kind),
PreparedMedia::Audio { .. } => None,
}
}
#[must_use]
pub const fn audio_kind(&self) -> Option<&PreparedAudioKind> {
match &self.media {
PreparedMedia::Video { .. } => None,
PreparedMedia::Audio { kind, .. } => Some(kind),
}
}
#[must_use]
pub const fn value_type(&self) -> ValueType {
match self.media {
PreparedMedia::Video { .. } => ValueType::Video,
PreparedMedia::Audio { .. } => ValueType::Audio,
}
}
#[must_use]
pub const fn video_domain(&self) -> Option<&VideoDomain> {
match &self.media {
PreparedMedia::Video { domain, .. } => Some(domain),
PreparedMedia::Audio { .. } => None,
}
}
#[must_use]
pub const fn audio_domain(&self) -> Option<&AudioDomain> {
match &self.media {
PreparedMedia::Video { .. } => None,
PreparedMedia::Audio { domain, .. } => Some(domain),
}
}
#[must_use]
pub const fn has_audio(&self) -> bool {
match self.media {
PreparedMedia::Video { has_audio, .. } => has_audio,
PreparedMedia::Audio { .. } => false,
}
}
#[must_use]
pub const fn origin(&self) -> &SourceOrigin {
&self.origin
}
#[must_use]
pub fn fingerprint(&self) -> &str {
&self.fingerprint
}
pub(crate) fn visit_inputs(&self, visitor: impl FnMut(NodeId)) {
match &self.media {
PreparedMedia::Video { kind, .. } => kind.visit_inputs(visitor),
PreparedMedia::Audio { kind, .. } => kind.visit_inputs(visitor),
}
}
#[cfg(feature = "native")]
pub(crate) fn try_visit_resources<E>(
&self,
mut visitor: impl FnMut(PreparedResource<'_>) -> std::result::Result<(), E>,
) -> std::result::Result<(), E> {
match &self.media {
PreparedMedia::Video {
kind: PreparedVideoKind::ImageVideo { asset, .. },
..
} => visitor(PreparedResource::Asset {
asset,
role: "image asset",
}),
PreparedMedia::Video {
kind: PreparedVideoKind::VideoSource { asset, .. },
..
} => visitor(PreparedResource::Asset {
asset,
role: "video asset",
}),
PreparedMedia::Audio {
kind: PreparedAudioKind::AudioSource { asset },
..
} => visitor(PreparedResource::Asset {
asset,
role: "audio asset",
}),
PreparedMedia::Video {
kind:
PreparedVideoKind::ExternalVideo {
executable,
arguments,
parameters,
..
},
..
} => {
visitor(PreparedResource::ExternalExecutable(executable))?;
for asset in arguments.iter().filter_map(|argument| match argument {
PreparedExternalArgument::File(asset) => Some(asset),
PreparedExternalArgument::Text(_) => None,
}) {
visitor(PreparedResource::Asset {
asset,
role: "external file argument",
})?;
}
for asset in parameters.values().filter_map(|value| match value {
PreparedExternalParameterValue::File(asset) => Some(asset),
PreparedExternalParameterValue::Integer(_)
| PreparedExternalParameterValue::Keyword(_) => None,
}) {
visitor(PreparedResource::Asset {
asset,
role: "external file parameter",
})?;
}
Ok(())
}
PreparedMedia::Video { .. } | PreparedMedia::Audio { .. } => Ok(()),
}
}
}
impl PreparedMedia {
pub(super) const fn artifact_contract(&self) -> WorkingArtifactContract<'_> {
match self {
Self::Video {
domain,
working_audio,
..
} => WorkingArtifactContract::Video {
video: domain,
audio: working_audio,
},
Self::Audio { domain, .. } => WorkingArtifactContract::Audio { audio: domain },
}
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum PreparedVideoKind {
ImageVideo {
asset: PreparedAsset,
frames: FrameCount,
fit: ImageFit,
},
VideoSource {
asset: PreparedAsset,
frames: FrameCount,
fit: ImageFit,
},
Slice {
input: NodeId,
range: FrameRange,
},
Repeat {
input: NodeId,
count: NonZeroU64,
frames: FrameCount,
},
ZoomIn {
input: NodeId,
by: ExactNumber,
},
FlashCut {
before: NodeId,
after: NodeId,
frames: FrameCount,
},
Crossfade {
before: NodeId,
after: NodeId,
frames: FrameCount,
},
Concat {
inputs: Vec<NodeId>,
},
SetAudio {
audio: NodeId,
video: NodeId,
},
AudioOnBlack {
audio: NodeId,
},
ExternalVideo {
executable: ExternalToolIdentity,
arguments: Vec<PreparedExternalArgument>,
inputs: BTreeMap<String, NodeId>,
parameters: BTreeMap<String, PreparedExternalParameterValue>,
preserve_input: String,
},
}
impl PreparedVideoKind {
pub(crate) fn visit_inputs(&self, mut visitor: impl FnMut(NodeId)) {
match self {
Self::ImageVideo { .. } | Self::VideoSource { .. } => {}
Self::Slice { input, .. }
| Self::Repeat { input, .. }
| Self::ZoomIn { input, .. }
| Self::AudioOnBlack { audio: input } => visitor(*input),
Self::FlashCut { before, after, .. } | Self::Crossfade { before, after, .. } => {
visitor(*before);
visitor(*after);
}
Self::Concat { inputs } => inputs.iter().copied().for_each(&mut visitor),
Self::SetAudio { audio, video } => {
visitor(*audio);
visitor(*video);
}
Self::ExternalVideo { inputs, .. } => {
inputs.values().copied().for_each(visitor);
}
}
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum PreparedAudioKind {
AudioSource {
asset: PreparedAsset,
},
AudioSlice {
input: NodeId,
range: crate::model::SampleRange,
},
AudioRepeat {
input: NodeId,
count: NonZeroU64,
},
AudioConcat {
inputs: Vec<NodeId>,
},
ExtractAudio {
video: NodeId,
},
}
impl PreparedAudioKind {
pub(crate) fn visit_inputs(&self, mut visitor: impl FnMut(NodeId)) {
match self {
Self::AudioSource { .. } => {}
Self::AudioSlice { input, .. } | Self::AudioRepeat { input, .. } => visitor(*input),
Self::AudioConcat { inputs } => inputs.iter().copied().for_each(visitor),
Self::ExtractAudio { video } => visitor(*video),
}
}
}
#[derive(Clone, Debug)]
pub struct PreparedAsset {
source_path: PathBuf,
content_hash: String,
}
impl PreparedAsset {
pub(super) fn new(source_path: PathBuf, content_hash: String) -> Self {
Self {
source_path,
content_hash,
}
}
#[must_use]
pub fn source_path(&self) -> &Path {
&self.source_path
}
#[must_use]
pub fn content_hash(&self) -> &str {
&self.content_hash
}
}