1use crate::{
2 AudioProperties, AudioRead, Captions, ClipBlendMode, ClipComposition, ClipEffect, ClipId,
3 Easing, Matte, MediaKind, ShapeMask, TimeMap, TrackAudio, TrackId, VisualProperties,
4};
5use alpha2_media::SourceId;
6use alpha2_time::TimeRange;
7use serde::{Deserialize, Serialize};
8
9#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
11#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
12#[serde(deny_unknown_fields)]
13pub struct Timeline {
14 #[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_TRACKS }>")]
16 #[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_TRACKS)))]
17 pub tracks: Vec<Track>,
18}
19
20impl Timeline {
21 #[must_use]
24 pub fn extent(&self) -> Option<TimeRange> {
25 let mut ranges = self.tracks.iter().flat_map(|track| {
26 track.clips.iter().map(|clip| clip.timeline_range).chain(
27 track
28 .captions
29 .iter()
30 .flat_map(|captions| captions.cues.iter().map(|cue| cue.timeline_range)),
31 )
32 });
33 let first = ranges.next()?;
34 let (start, end) = ranges.fold((first.start(), first.end()), |(start, end), range| {
35 (start.min(range.start()), end.max(range.end()))
36 });
37 TimeRange::new(start, end).ok()
38 }
39
40 #[must_use]
43 pub fn matte_sources(&self) -> std::collections::HashSet<&ClipId> {
44 self.tracks
45 .iter()
46 .flat_map(|track| &track.clips)
47 .filter_map(|clip| clip.matte.as_ref().map(|matte| &matte.source))
48 .collect()
49 }
50}
51
52#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
54#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
55#[serde(deny_unknown_fields)]
56#[non_exhaustive]
57pub struct Track {
58 pub id: TrackId,
60 pub name: String,
62 pub kind: TrackKind,
64 #[serde(default)]
67 #[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_CLIPS_PER_TRACK }>")]
68 #[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_CLIPS_PER_TRACK)))]
69 pub clips: Vec<Clip>,
70 #[serde(default)]
72 #[serde(
73 deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_TRANSITIONS_PER_TRACK }>"
74 )]
75 #[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_TRANSITIONS_PER_TRACK)))]
76 pub transitions: Vec<ClipTransition>,
77 #[serde(default)]
79 pub audio: TrackAudio,
80 #[serde(default)]
82 pub captions: Option<Captions>,
83}
84
85impl Track {
86 #[must_use]
88 pub fn new(id: TrackId, name: impl Into<String>, kind: TrackKind) -> Self {
89 Self {
90 id,
91 name: name.into(),
92 kind,
93 clips: Vec::new(),
94 transitions: Vec::new(),
95 audio: TrackAudio::default(),
96 captions: (kind == TrackKind::Caption).then(Captions::default),
97 }
98 }
99}
100
101#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
103#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
104#[serde(rename_all = "snake_case")]
105#[non_exhaustive]
106pub enum TrackKind {
107 Video,
109 Audio,
111 Caption,
113}
114
115impl TrackKind {
116 pub(crate) const fn accepts(self, source: MediaKind) -> bool {
117 matches!(
118 (self, source),
119 (
120 Self::Video,
121 MediaKind::Video
122 | MediaKind::Image
123 | MediaKind::Generated
124 | MediaKind::Composition
125 | MediaKind::Adjustment
126 ) | (Self::Audio, MediaKind::Audio)
127 )
128 }
129}
130
131#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
133#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
134#[serde(deny_unknown_fields)]
135#[non_exhaustive]
136pub struct Clip {
137 pub id: ClipId,
139 pub source_id: SourceId,
141 pub timeline_range: TimeRange,
143 pub source_range: TimeRange,
145 #[serde(default)]
147 pub visual: VisualProperties,
148 #[serde(default)]
151 pub composition: Option<ClipComposition>,
152 #[serde(default)]
154 pub blend_mode: ClipBlendMode,
155 #[serde(default)]
158 #[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_CLIP_MASKS }>")]
159 #[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_CLIP_MASKS)))]
160 pub masks: Vec<ShapeMask>,
161 #[serde(default)]
163 pub matte: Option<Matte>,
164 #[serde(default)]
166 pub time_map: TimeMap,
167 #[serde(default)]
169 pub audio: AudioProperties,
170 #[serde(default, skip_serializing_if = "CacheIntent::is_auto")]
173 pub cache: CacheIntent,
174 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
177 pub motion_blur: bool,
178 #[serde(default, skip_serializing_if = "FrameInterpolation::is_hold")]
182 pub frame_interpolation: FrameInterpolation,
183}
184
185#[derive(Copy, Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
187#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
188#[serde(rename_all = "snake_case")]
189pub enum FrameInterpolation {
190 #[default]
192 Hold,
193 Mix,
198}
199
200impl FrameInterpolation {
201 #[must_use]
203 pub const fn is_hold(&self) -> bool {
204 matches!(self, Self::Hold)
205 }
206}
207
208#[derive(Copy, Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
210#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
211#[serde(rename_all = "snake_case")]
212pub enum CacheIntent {
213 #[default]
215 Auto,
216 Pin,
218 Bypass,
220}
221
222impl CacheIntent {
223 #[must_use]
225 pub const fn is_auto(&self) -> bool {
226 matches!(self, Self::Auto)
227 }
228}
229
230impl Clip {
231 #[must_use]
233 pub fn audio_read(&self) -> AudioRead {
234 self.audio
235 .read(&self.time_map, self.timeline_range, self.source_range)
236 }
237
238 #[must_use]
240 pub fn stretches_audio(&self) -> bool {
241 self.audio_read().stretches()
242 }
243
244 #[must_use]
246 pub fn new(
247 id: ClipId,
248 source_id: SourceId,
249 timeline_range: TimeRange,
250 source_range: TimeRange,
251 ) -> Self {
252 Self {
253 id,
254 source_id,
255 timeline_range,
256 source_range,
257 visual: VisualProperties::default(),
258 composition: None,
259 blend_mode: ClipBlendMode::default(),
260 masks: Vec::new(),
261 matte: None,
262 time_map: TimeMap::Forward,
263 audio: AudioProperties::default(),
264 cache: CacheIntent::Auto,
265 motion_blur: false,
266 frame_interpolation: FrameInterpolation::Hold,
267 }
268 }
269}
270
271#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
273#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
274#[serde(deny_unknown_fields)]
275pub struct ClipTransition {
276 pub from_clip: ClipId,
278 pub to_clip: ClipId,
280 pub timeline_range: TimeRange,
282 pub easing: Easing,
285 pub effect: ClipEffect,
287}