use std::collections::HashMap;
use alpha2_engine::ExportFrameNeeds;
use alpha2_eval::FrameNeed;
use alpha2_media::SourceId;
use alpha2_model::{ClipId, NodeId};
use alpha2_time::{MediaTime, TimeRange};
#[derive(Debug, Eq, PartialEq)]
pub struct BackwardRun {
pub times: Vec<MediaTime>,
pub until: u64,
}
#[must_use]
pub fn backward_run(
frames: &[ExportFrameNeeds],
start: u64,
cursor: &DecodeCursorKey,
) -> BackwardRun {
let mut times = Vec::<MediaTime>::new();
let mut until = start;
for frame in frames.iter().skip_while(|frame| frame.index < start) {
let Some(time) = cursor_time(frame, cursor) else {
break;
};
match times.last() {
Some(last) if time > *last => break,
Some(last) if time == *last => {}
_ => times.push(time),
}
until = frame.index;
}
BackwardRun { times, until }
}
#[must_use]
pub fn cursor_time(frame: &ExportFrameNeeds, cursor: &DecodeCursorKey) -> Option<MediaTime> {
frame
.needs
.iter()
.find(|need| decode_cursor_key(need) == *cursor)
.map(|need| need.media_time)
}
pub const MAX_SEQUENTIAL_DECODE_GAP: MediaTime =
MediaTime::from_ticks(2 * alpha2_time::TICKS_PER_SECOND);
#[must_use]
pub fn decodes_on(previous: MediaTime, requested: MediaTime) -> bool {
requested >= previous
&& requested.ticks().saturating_sub(previous.ticks()) <= MAX_SEQUENTIAL_DECODE_GAP.ticks()
}
pub type DecodeCursorKey = (SourceId, ClipId, Option<NodeId>);
#[must_use]
pub fn decode_cursor_key(need: &FrameNeed) -> DecodeCursorKey {
(
need.source_id.clone(),
need.clip_id.clone(),
need.reader.clone(),
)
}
#[must_use]
pub fn visual_decode_reads(
project: &alpha2_model::Project,
export_range: TimeRange,
) -> (usize, CursorReads) {
let decoded = project
.sources
.iter()
.filter(|source| source.kind == alpha2_model::MediaKind::Video && source.provider.is_none())
.map(|source| &source.id)
.collect::<std::collections::HashSet<_>>();
let Ok(index) = alpha2_eval::ActiveIndex::build(project) else {
return (1, CursorReads::default());
};
let counted = |source: &SourceId| decoded.contains(source);
(
index.peak_source_reads(export_range, counted).max(1),
CursorReads::new(index.source_reads(export_range, counted)),
)
}
#[derive(Debug, Default)]
pub struct CursorReads {
spans: HashMap<DecodeCursorKey, Vec<TimeRange>>,
}
impl CursorReads {
#[must_use]
pub fn new(reads: Vec<alpha2_eval::SourceRead>) -> Self {
let mut spans = HashMap::<DecodeCursorKey, Vec<TimeRange>>::new();
for read in reads {
spans
.entry((read.source_id, read.clip_id, read.reader))
.or_default()
.push(read.span);
}
for cursor_spans in spans.values_mut() {
cursor_spans.sort_unstable_by_key(|span| span.start());
}
Self { spans }
}
#[must_use]
pub fn knows(&self, cursor: &DecodeCursorKey) -> bool {
self.spans.contains_key(cursor)
}
#[must_use]
pub fn next_read(&self, cursor: &DecodeCursorKey, time: MediaTime) -> Option<MediaTime> {
self.spans
.get(cursor)?
.iter()
.find(|span| span.end() > time)
.map(|span| span.start().max(time))
}
#[must_use]
pub fn finished(&self, cursor: &DecodeCursorKey, time: MediaTime) -> bool {
self.knows(cursor) && self.next_read(cursor, time).is_none()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn range(start: i64, end: i64) -> TimeRange {
TimeRange::new(MediaTime::from_ticks(start), MediaTime::from_ticks(end)).expect("range")
}
#[test]
fn decode_parallelism_counts_what_compositions_read() {
use serde_json::json;
let span = |start, end| json!({"start": start, "end": end});
let video = |id: &str| json!({"id": id, "kind": "video", "locator": format!("{id}.mp4")});
let media = |id: &str| json!({"id": id, "op": {"type": "media", "source_id": id}});
let quad = json!({"id": "quad", "nodes": [
media("a"), media("b"), media("c"), media("d"),
{"id": "mix", "op": {"type": "merge", "layers": [
{"input": "a"}, {"input": "b"}, {"input": "c"}, {"input": "d"}
]}}
], "output": "mix"});
let project = |clip: serde_json::Value| {
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": [video("a"), video("b"), video("c"), video("d"),
{"id": "split", "kind": "composition", "composition": "quad"}],
"compositions": [quad],
"timeline": {"tracks": [{"id": "t", "name": "V", "kind": "video", "clips": [clip]}]}
})
.to_string(),
)
.expect("valid")
};
let composed = project(json!({
"id": "c", "source_id": "a", "composition": {"id": "quad"},
"timeline_range": span(0, 120_000), "source_range": span(0, 120_000)
}));
assert_eq!(visual_decode_reads(&composed, range(0, 120_000)).0, 4);
let source = project(json!({
"id": "c", "source_id": "split",
"timeline_range": span(0, 120_000), "source_range": span(0, 120_000)
}));
assert_eq!(visual_decode_reads(&source, range(0, 120_000)).0, 4);
}
}