Skip to main content

renderbox_sdk/ops/
mod.rs

1pub mod video;
2pub mod audio;
3pub mod ai;
4pub mod combine;
5pub mod source;
6pub mod timeline;
7
8use std::collections::BTreeMap;
9
10use renderbox_dsl::{OpNode, ParamValue};
11
12use crate::expr::{Expr, NumberOrExpr};
13use crate::sorts::{Audio, Video};
14use crate::graph::Stream;
15
16pub(crate) trait IntoParam {
17    fn into_param(self) -> ParamValue;
18}
19
20impl IntoParam for f64 {
21    fn into_param(self) -> ParamValue {
22        ParamValue::Number(self)
23    }
24}
25
26impl IntoParam for u32 {
27    fn into_param(self) -> ParamValue {
28        ParamValue::Number(self as f64)
29    }
30}
31
32impl IntoParam for i32 {
33    fn into_param(self) -> ParamValue {
34        ParamValue::Number(self as f64)
35    }
36}
37
38impl IntoParam for bool {
39    fn into_param(self) -> ParamValue {
40        ParamValue::Bool(self)
41    }
42}
43
44impl IntoParam for &str {
45    fn into_param(self) -> ParamValue {
46        ParamValue::String(self.to_string())
47    }
48}
49
50impl IntoParam for String {
51    fn into_param(self) -> ParamValue {
52        ParamValue::String(self)
53    }
54}
55
56impl IntoParam for Expr {
57    fn into_param(self) -> ParamValue {
58        ParamValue::String(self.compile())
59    }
60}
61
62impl IntoParam for NumberOrExpr {
63    fn into_param(self) -> ParamValue {
64        match self {
65            NumberOrExpr::Number(n) => ParamValue::Number(n),
66            NumberOrExpr::Expr(e) => ParamValue::String(e.compile()),
67        }
68    }
69}
70
71macro_rules! params {
72    ($($key:expr => $val:expr),* $(,)?) => {{
73        #[allow(unused_mut)]
74        let mut m = ::std::collections::BTreeMap::new();
75        $(m.insert($key.into(), $crate::ops::IntoParam::into_param($val));)*
76        m
77    }};
78}
79
80pub(crate) use params;
81
82pub(crate) fn add_video_filter(
83    input: Stream<Video>,
84    name: &str,
85    params: BTreeMap<String, ParamValue>,
86) -> Stream<Video> {
87    let node_id = input.graph.borrow_mut().add_node(OpNode::Filter {
88        name: name.into(),
89        params,
90        input: input.node_id,
91    });
92    Stream::new(node_id, input.graph)
93}
94
95pub(crate) fn add_audio_filter(
96    input: Stream<Audio>,
97    name: &str,
98    params: BTreeMap<String, ParamValue>,
99) -> Stream<Audio> {
100    let node_id = input.graph.borrow_mut().add_node(OpNode::AudioFilter {
101        name: name.into(),
102        params,
103        input: input.node_id,
104    });
105    Stream::new(node_id, input.graph)
106}
107
108pub(crate) struct ExprNum(pub f64);
109
110impl IntoParam for ExprNum {
111    fn into_param(self) -> ParamValue {
112        let n = self.0;
113        if n == n.floor() && n.abs() < 1e15 {
114            ParamValue::String(format!("{:.1}", n))
115        } else {
116            ParamValue::String(format!("{}", n))
117        }
118    }
119}
120
121pub(crate) trait OptParam {
122    fn insert_into(self, key: &str, map: &mut BTreeMap<String, ParamValue>);
123}
124
125impl<T: IntoParam> OptParam for Option<T> {
126    fn insert_into(self, key: &str, map: &mut BTreeMap<String, ParamValue>) {
127        if let Some(v) = self {
128            map.insert(key.into(), v.into_param());
129        }
130    }
131}