Merge upstream/main (#3191 merged as 84382487) into footage-cache-3271

The conflicting files are the #3191 branch's own on main; this branch's changes on top of them
are kept.
This commit is contained in:
vangel76 committed 2026-11-08 03:34:34 +01:00
commit 1e47afcf9a
14 files changed
+801 -38

No files matched your search

+57 -8
View File
@@ -12,6 +12,11 @@
//! project-level `LIST EfdG` (one `EfDf` per effect type: a `tdmn` with the match name, then a copy
//! of one instance's `sspc`), see [`Project::effect_params`]. That copy also carries the values of
//! the instance it was copied from, so values are only ever read from the instance's own `tdgp`.
//!
//! Some files have no `EfdG` at all (seen in 17 projects, all from inside After Effects-made
//! Motion Graphics templates). In those, one instance per effect type kept its full `parT` (which
//! one is not known) and it is the only place the definitions are written, so the other instances
//! of that type borrow them from it (#3170).
use std::collections::{HashMap, HashSet};
@@ -138,7 +143,11 @@ pub enum DefinitionSource {
/// The project-level `EfdG/EfDf` copy for the effect's match name (the instance's own `parT`
/// is empty, 383).
Project,
/// Neither has any: the effect has no readable parameter definitions.
/// The own `parT` of another instance of the same match name in the file: the instance's
/// own `parT` is empty and the file has no project-level copy of the type (a project without
/// `EfdG`, #3170).
OtherInstance,
/// None of these has any: the effect has no readable parameter definitions.
Missing,
}
@@ -155,7 +164,8 @@ pub struct EffectParam<'f, 'a> {
pub name: String,
/// The popup items (`pdnm`, split at `|`; for a button its label, e.g. `Swap Colors`).
pub items: Option<Vec<String>>,
/// True when the definition is the project-level copy (see [`DefinitionSource::Project`]).
/// True when the definition is borrowed: the project-level copy ([`DefinitionSource::Project`])
/// or another instance's ([`DefinitionSource::OtherInstance`]).
pub borrowed_definition: bool,
pard: &'f [u8],
stored: Option<Property<'f, 'a>>,
@@ -214,6 +224,22 @@ impl<'f, 'a> Effect<'f, 'a> {
self.chunk.child_list(b"parT").is_some_and(has_pard)
}
/// The match names of the value records in the instance's own `tdgp` (properties only, not
/// groups), in file order, each once.
pub fn value_record_names(&self) -> Vec<String> {
let mut seen: HashSet<String> = HashSet::new();
self.chunk
.child_list(b"tdgp")
.and_then(PropGroup::from_chunk)
.map(|g| g.entries())
.unwrap_or_default()
.into_iter()
.filter(|e| matches!(e.node, PropNode::Property(_)))
.map(|e| e.match_name)
.filter(|n| seen.insert(n.clone()))
.collect()
}
/// The parameters in the order of the instance's own `parT`. Empty for an instance whose
/// `parT` is empty; [`Project::effect_params`] falls back to the project's definitions.
pub fn params(&self) -> Vec<EffectParam<'f, 'a>> {
@@ -299,8 +325,25 @@ impl<'f, 'a> Project<'f, 'a> {
/// [`Self::effect_definitions`] indexed by match name, for looking up many effects: build it
/// once per file, not once per effect (a file can hold many definitions and many instances).
/// For a type without a project-level copy it also keeps the own `parT` of the first instance
/// in the file (items, layers and effects in file order) that has one.
pub fn effect_definition_index(&self) -> EffectDefinitions<'f, 'a> {
EffectDefinitions { by_name: self.effect_definitions().into_iter().collect() }
let by_name: HashMap<String, &'f Chunk<'a>> = self.effect_definitions().into_iter().collect();
let mut instances: HashMap<String, &'f Chunk<'a>> = HashMap::new();
for item in self.all_items() {
let Some(comp) = item.comp() else { continue };
for layer in comp.layers() {
for fx in layer.effects() {
if by_name.contains_key(&fx.match_name) || instances.contains_key(&fx.match_name) {
continue;
}
if let Some(part) = fx.chunk.child_list(b"parT").filter(|p| has_pard(p)) {
instances.insert(fx.match_name, part);
}
}
}
}
EffectDefinitions { by_name, instances }
}
/// Where the parameter definitions of `effect` come from. Builds the index each call: use
@@ -316,10 +359,13 @@ impl<'f, 'a> Project<'f, 'a> {
}
}
/// The project-level effect definitions by match name ([`Project::effect_definition_index`]).
/// The borrowable effect definitions by match name ([`Project::effect_definition_index`]).
#[derive(Debug, Clone, Default)]
pub struct EffectDefinitions<'f, 'a> {
/// The project-level copies (`EfdG`).
by_name: HashMap<String, &'f Chunk<'a>>,
/// For a type without a project-level copy: the own `parT` of the first instance that has one.
instances: HashMap<String, &'f Chunk<'a>>,
}
impl<'f, 'a> EffectDefinitions<'f, 'a> {
@@ -329,20 +375,23 @@ impl<'f, 'a> EffectDefinitions<'f, 'a> {
DefinitionSource::Instance
} else if self.by_name.contains_key(effect.match_name()) {
DefinitionSource::Project
} else if self.instances.contains_key(effect.match_name()) {
DefinitionSource::OtherInstance
} else {
DefinitionSource::Missing
}
}
/// The parameters of `effect`: from its own `parT`, or, when that is empty, from the
/// project-level definition of its match name ([`EffectParam::borrowed_definition`]). Values
/// always come from the instance's own records (absent = the `pard` default); the project
/// copy's values belong to another instance and are never read.
/// project-level definition of its match name, else from another instance of it that has
/// its own ([`EffectParam::borrowed_definition`]). Values always come from the instance's own
/// records (absent = the `pard` default); the values next to borrowed definitions belong to
/// another instance and are never read.
pub fn params(&self, effect: &Effect<'f, 'a>) -> Vec<EffectParam<'f, 'a>> {
if effect.has_own_definitions() {
return effect.params();
}
match self.by_name.get(effect.match_name()) {
match self.by_name.get(effect.match_name()).or_else(|| self.instances.get(effect.match_name())) {
Some(part) => effect.params_from(part, true),
None => Vec::new(),
}
+83 -6
View File
@@ -2,6 +2,7 @@
//! of a layer's source.
use crate::container::bytes::{be_f32, be_f64, be_u32, cstr, le_u32, text};
use crate::container::chunk::Chunk;
use crate::container::header::Rational;
use crate::container::typed::{Item, ItemKind, Layer, Project};
@@ -159,19 +160,25 @@ fn json_string_at(json: &[u8], mut i: usize) -> Option<String> {
}
}
/// The numbering of an image sequence item (353, 603).
/// The numbering of an image sequence item (353, 603), or its file list when it was imported with
/// Force alphabetical order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SequenceInfo {
/// The file name before the frame number (`seq_`, `shot_`): the first `Utf8` after the alias.
/// An empty prefix is not stored (`0001.png`): the one `Utf8` is then the suffix.
pub prefix: String,
/// The file name after the frame number (`.png`, `.exr`): the second `Utf8` after the alias.
pub suffix: String,
/// The first and last frame number (`sspc` `u32` at 172 and 176: 1 and 3 in 353, 101 and 103
/// in 603).
/// in 603; both `0xFFFFFFFF` for a sequence with a file list).
pub start: u32,
pub end: u32,
/// The digits of the frame number (`sspc` `u32` at 180: 3 for `seq_001`, 4 for `shot_0101`).
pub digits: u32,
/// The file names in order for a sequence imported with Force alphabetical order (`LIST StVc`
/// after the alias: `StVS` with the count as a little-endian `u32`, then one `Utf8` per file);
/// empty for a numbered sequence. When set, it replaces the numbering.
pub files: Vec<String>,
}
impl SequenceInfo {
@@ -180,6 +187,62 @@ impl SequenceInfo {
let width = usize::try_from(self.digits.min(16)).unwrap_or(0);
format!("{}{n:0width$}{}", self.prefix, self.suffix)
}
/// The number of files: the listed files, or the frame numbers from start to end (0 when the
/// end lies before the start).
pub fn frame_count(&self) -> u32 {
if self.files.is_empty() {
self.end.checked_sub(self.start).map_or(0, |d| d.saturating_add(1))
} else {
u32::try_from(self.files.len()).unwrap_or(u32::MAX)
}
}
/// The name of the `i`-th file (from 0): from the file list, or numbered from `start`; `None`
/// past the last file.
pub fn nth_file_name(&self, i: u32) -> Option<String> {
if self.files.is_empty() {
let n = self.start.checked_add(i).filter(|n| *n <= self.end)?;
Some(self.file_name(n))
} else {
self.files.get(usize::try_from(i).ok()?).cloned()
}
}
}
/// The file names of a `LIST StVc` (Force alphabetical order): the `Utf8` children in order, at most
/// as many as the count in `StVS` (little-endian `u32`) says. Empty names count toward that number
/// and are then skipped. Without a readable count (no `StVS`, or one shorter than 4 bytes) the list
/// is unreadable (empty). A name that is not a plain file name (holding `/`, `\`, `:` or a control
/// character such as NUL, or being `.` or `..`) makes the whole list unreadable (empty): the names
/// are joined to the folder.
fn sequence_file_list(list: &Chunk<'_>) -> Vec<String> {
// After Effects writes the count first (the one sample); a list without a readable count is
// not read rather than guessed.
let Some(count) = list.child(b"StVS").and_then(|c| c.bytes()).and_then(|b| le_u32(b, 0)).and_then(|n| usize::try_from(n).ok()) else {
return Vec::new();
};
let names: Vec<String> =
list.children().iter().filter(|c| c.is(b"Utf8")).filter_map(|c| c.bytes()).take(count).map(text).filter(|n| !n.is_empty()).collect();
if names.iter().any(|n| n.contains(|c: char| matches!(c, '/' | '\\' | ':') || c.is_control()) || n == "." || n == "..") {
return Vec::new();
}
names
}
/// The prefix and suffix of a sequence whose naming holds one `Utf8` (`part`): After Effects leaves
/// out an empty prefix (`0001.png` stores only `.png`). The stored first file name (`opti`) decides:
/// a name starting with `part` has it as the prefix, one ending with it as the suffix. Without that
/// evidence, a `part` starting with `.` is the suffix (an extension); anything else is not read.
fn single_name_part(part: &str, first_file: Option<&str>) -> Option<(String, String)> {
if let Some(first) = first_file.filter(|_| !part.is_empty()) {
match (first.starts_with(part), first.ends_with(part)) {
(true, false) => return Some((part.to_string(), String::new())),
(false, true) => return Some((String::new(), part.to_string())),
_ => {}
}
}
part.starts_with('.').then(|| (String::new(), part.to_string()))
}
impl<'f, 'a> Item<'f, 'a> {
@@ -194,10 +257,24 @@ impl<'f, 'a> Item<'f, 'a> {
let sspc = pin.child(b"sspc")?.bytes()?;
let kids = pin.children();
let after = kids.iter().position(|c| c.is_list(b"Als2"))?;
let mut names = kids.iter().skip(after + 1).take_while(|c| !c.is(b"opti")).filter(|c| c.is(b"Utf8")).filter_map(|c| c.bytes()).map(text);
let prefix = names.next()?;
let suffix = names.next()?;
Some(SequenceInfo { prefix, suffix, start: be_u32(sspc, 172)?, end: be_u32(sspc, 176)?, digits: be_u32(sspc, 180)? })
let naming = || kids.iter().skip(after.saturating_add(1)).take_while(|c| !c.is(b"opti"));
let (start, end, digits) = (be_u32(sspc, 172)?, be_u32(sspc, 176)?, be_u32(sspc, 180)?);
// Force alphabetical order: the file list replaces the numbering.
if let Some(list) = naming().find(|c| c.is_list(b"StVc")) {
let files = sequence_file_list(list);
if files.is_empty() {
return None;
}
return Some(SequenceInfo { prefix: String::new(), suffix: String::new(), start, end, digits, files });
}
let names: Vec<String> = naming().filter(|c| c.is(b"Utf8")).filter_map(|c| c.bytes()).map(text).take(2).collect();
let (prefix, suffix) = match names.as_slice() {
[prefix, suffix] => (prefix.clone(), suffix.clone()),
// An empty prefix is not stored (`0001.png` has only `.png`).
[one] => single_name_part(one, f.name.as_deref())?,
_ => return None,
};
Some(SequenceInfo { prefix, suffix, start, end, digits, files: Vec::new() })
}
/// The rate a clip is conformed to (Interpret Footage ▸ Conform to frame rate; `sspc` `u32` at
+27 -9
View File
@@ -206,6 +206,8 @@ fn new_comp(h: &CompHeader, renderer: Option<AepRenderer>, map: &TimeMap, name:
comp
}
/// Adds one footage item to the project under the given parent.
/// Unreadable footage is recorded as a loss, solids and null placeholders are handled separately, and file and sequence footage is linked to its files.
fn import_footage(project: &mut Project, cx: &mut Ctx<'_>, item: &AepItem<'_, '_>, name: String, label: Label, parent: ItemId, null_only: &HashSet<u32>) {
let Some(f) = item.footage() else {
cx.col.lose(LossCategory::DamagedData, format!("footage '{name}': data unreadable, not imported"));
@@ -240,31 +242,42 @@ fn import_footage(project: &mut Project, cx: &mut Ctx<'_>, item: &AepItem<'_, '_
}
let mut path = f.path.clone().unwrap_or_default();
// An image sequence's alias is its folder; its files are named by the stored numbering
// (353, 603). The first file stands for the sequence, as in EffectCraft.
// (353, 603), or listed one by one when imported with Force alphabetical order. The first
// file stands for the sequence, as in EffectCraft.
let mut sequence = Vec::new();
if let Some(seq) = item.sequence_info().filter(|s| !path.is_empty() && s.end >= s.start) {
let frames = (seq.end - seq.start).saturating_add(1);
if let Some(seq) = item.sequence_info().filter(|s| !path.is_empty() && s.frame_count() > 0) {
let frames = seq.frame_count();
let sep = if path.contains('\\') { '\\' } else { '/' };
let folder = path.trim_end_matches(['\\', '/']).to_string();
// One name is the folder, a separator and the file name; the number is as wide as the stored
// digits or as the largest number, whichever is longer (an upper bound for every frame).
let name_bytes = (folder.len() + 1 + seq.prefix.len() + seq.suffix.len() + usize::try_from(seq.digits.min(16)).unwrap_or(16).max(10)) as u64;
let bytes = name_bytes.saturating_mul(u64::from(frames));
// One name is the folder, a separator and the file name. A numbered name is as wide as the
// stored digits or as the largest number, whichever is longer (an upper bound for every
// frame); listed names are counted as they are.
let len = |n: usize| u64::try_from(n).unwrap_or(u64::MAX);
let file_bytes = if seq.files.is_empty() {
len(seq.prefix.len().saturating_add(seq.suffix.len()).saturating_add(usize::try_from(seq.digits.min(16)).unwrap_or(16).max(10)))
.saturating_mul(u64::from(frames))
} else {
seq.files.iter().fold(0u64, |sum, n| sum.saturating_add(len(n.len())))
};
let bytes = len(folder.len().saturating_add(1)).saturating_mul(u64::from(frames)).saturating_add(file_bytes);
if frames > MAX_SEQUENCE_FRAMES || frames > cx.sequence_budget || bytes > cx.sequence_bytes {
// Not expanded: the item keeps its first file (a still standing for the sequence).
cx.col.lose(
LossCategory::DamagedData,
format!("footage '{name}': a sequence of {frames} frames is longer than the import expands; only its first file is linked"),
);
path = format!("{folder}{sep}{}", seq.file_name(seq.start));
path = format!("{folder}{sep}{}", seq.nth_file_name(0).unwrap_or_default());
} else {
cx.sequence_budget -= frames;
cx.sequence_bytes -= bytes;
sequence = (seq.start..=seq.end).map(|n| format!("{folder}{sep}{}", seq.file_name(n))).collect();
sequence = (0..frames).filter_map(|i| seq.nth_file_name(i)).map(|n| format!("{folder}{sep}{n}")).collect();
}
if let Some(first) = sequence.first() {
path = first.clone();
}
} else if f.is_sequence && !path.is_empty() {
// The alias is a folder, but the file names could not be read: the item links the folder.
cx.col.lose(LossCategory::DamagedData, format!("footage '{name}': image sequence naming not read, linked to its folder"));
}
let present = !path.is_empty() && (cx.options.exists)(&path);
if !present {
@@ -318,6 +331,11 @@ fn import_footage(project: &mut Project, cx: &mut Ctx<'_>, item: &AepItem<'_, '_
/// sequence the name with the frame range (`shot_[0101-0103].exr`).
fn default_footage_name(item: &AepItem<'_, '_>, f: &crate::Footage) -> Option<String> {
if let Some(s) = item.sequence_info() {
// A listed sequence (Force alphabetical order) has no numbering: its first file names it.
// Assumption: what After Effects shows for such an item without a stored name was not sampled.
if let Some(first) = s.files.first() {
return Some(first.clone());
}
let w = usize::try_from(s.digits.min(16)).unwrap_or(0);
return Some(format!("{}[{:0w$}-{:0w$}]{}", s.prefix, s.start, s.end, s.suffix));
}
+16
View File
@@ -99,6 +99,22 @@ pub(crate) fn import_effects<'f, 'a>(
None => col.lose(LossCategory::DamagedData, format!("{}: '{}': the fx switch of '{shown}' is unreadable, imported switched on", cx.comp, cx.layer)),
}
let params = defs.params(&fx);
// Borrowed definitions (another instance's or the project copy) may not name every value
// record this instance has: such a record has nothing to import into, so it is reported.
if params.first().is_some_and(|p| p.borrowed_definition) {
let defined: std::collections::HashSet<&str> = params.iter().map(|p| p.match_name.as_str()).collect();
for name in fx.value_record_names().iter().filter(|n| !defined.contains(n.as_str())) {
col.lose(
LossCategory::Effects,
format!(
"{}: '{}' effect '{shown}' ({}): the value record {name} has no parameter definition, not imported",
cx.comp,
cx.layer,
fx.match_name()
),
);
}
}
if map.extra == Extra::Cryptomatte {
import_cryptomatte(&fx, &params, cx, &shown, &mut group, col);
} else {
+4 -2
View File
@@ -237,9 +237,11 @@ fn exercise_layer(project: &Project<'_, '_>, comp_size: (u32, u32), layer: &Laye
if let Some(root) = layer.properties() {
exercise_group(&root, 0);
}
// One index for the layer's effects: building it walks every layer of the file.
let defs = project.effect_definition_index();
for e in layer.effects() {
let _ = (e.match_name().len(), e.name(), e.declared_param_count(), e.has_own_definitions(), project.definition_source(&e));
for p in e.params().into_iter().chain(project.effect_params(&e)) {
let _ = (e.match_name().len(), e.name(), e.declared_param_count(), e.has_own_definitions(), defs.source(&e));
for p in e.params().into_iter().chain(defs.params(&e)) {
let _ = (p.index, p.kind, p.name.len(), p.items.as_ref().map(Vec::len), p.pard().len(), p.value(), p.stored_value(), p.pard_default());
let _ = (p.is_default(), p.popup_item_count(), p.popup_selected_index(), p.popup_selected_item(), p.valid_max(), p.is_animated());
let _ = (p.valid_min(), p.slider_range());
+7 -5
View File
@@ -626,9 +626,10 @@ fn bogus_effect_structure_is_reported_not_imported_blindly() {
"{r:?}"
);
// No project-level definitions: a Ramp with an empty parT cannot be read and is reported; one
// with its own parT still arrives. (In the single-case file one of the two had its own; in
// the combined file an earlier case holds the definitions, so both take the project copy.)
// No project-level definitions: a Ramp with an empty parT borrows the definitions of a Ramp
// in the file that has its own parT (#3170). In the single-case file one of the two has its
// own, so both arrive; in the case cut from the combined file an earlier case (not kept)
// held them, so neither has readable definitions and both are reported.
let own = {
let bytes = skip!(crate::common::try_fixture("383-fx-ramp-on-100x100-start-10-20.aep"));
let file = parse(&bytes).unwrap();
@@ -646,8 +647,9 @@ fn bogus_effect_structure_is_reported_not_imported_blindly() {
}
}));
let r = report_of(&bytes);
assert_eq!(r.imported.effects, own, "{r:?}");
assert_eq!(r.count(effectcraft_aep::import::LossCategory::Effects), 2 - own, "{r:?}");
let arrive = if own > 0 { 2 } else { 0 };
assert_eq!(r.imported.effects, arrive, "{r:?}");
assert_eq!(r.count(effectcraft_aep::import::LossCategory::Effects), 2 - arrive, "{r:?}");
assert!(try_all(&bytes)); // 368, the first Gradient Ramp of its file, has its own parT: it still arrives.
let bytes = skip!(mutate("368-fx-ramp-default.aep", &mut |c| {
if let Body::List { list_type, .. } = &mut c.body
+398
View File
@@ -77,6 +77,167 @@ fn a_sequence_with_long_names_is_not_expanded_past_the_byte_budget() {
assert_eq!(longest, 0, "the sequence was not expanded");
}
/// The project with the sequence's naming (the chunks between `Als2` and `opti` in its `Pin `)
/// replaced by `naming`, and the `sspc` numbering (start, end, digits at 172, 176, 180) set to
/// `numbering` when given.
fn with_sequence_naming(naming: &[Chunk<'static>], numbering: Option<[u32; 3]>) -> Vec<u8> {
let mut file: AepFile<'_> = parse(SYNTHETIC).unwrap();
let mut patched = 0;
visit(&mut file.chunks, &mut |c| {
if !c.is_list(b"Pin ") {
return;
}
let Body::List { children, .. } = &mut c.body else { return };
let Some(als) = children.iter().position(|k| k.is_list(b"Als2")) else { return };
let Some(opti) = children.iter().position(|k| k.is(b"opti")) else { return };
assert!(als < opti);
let tail = children.split_off(opti);
children.truncate(als + 1);
children.extend(naming.iter().map(shorten));
children.extend(tail);
if let Some(numbering) = numbering {
let sspc = children.iter_mut().find(|k| k.is(b"sspc")).unwrap();
for (at, v) in [172, 176, 180].into_iter().zip(numbering) {
set_data(sspc, at, &v.to_be_bytes());
}
}
patched += 1;
});
assert_eq!(patched, 1, "the synthetic project has one sequence");
write(&file).unwrap()
}
/// `bytes` with the first file name the sequence stores in its png `opti` (byte 58, NUL-terminated)
/// set to `name`.
fn with_first_file(bytes: &[u8], name: &str) -> Vec<u8> {
let mut file: AepFile<'_> = parse(bytes).unwrap();
let mut patched = 0;
visit(&mut file.chunks, &mut |c| {
if c.is(b"opti") && c.bytes().is_some_and(|d| d.get(..4) == Some(&b"png!"[..])) {
let mut z = name.as_bytes().to_vec();
z.push(0);
set_data(c, 58, &z);
patched += 1;
}
});
assert_eq!(patched, 1, "the synthetic project has one png item");
write(&file).unwrap()
}
/// The imported sequence item's footage.
fn the_footage(p: &Project) -> effectcraft_project::Footage {
p.items.values().find_map(|i| if let ItemKind::Footage(f) = &i.kind { Some(f.clone()) } else { None }).expect("the sequence")
}
/// The file names (without the folder) of a footage's sequence.
fn file_names(f: &effectcraft_project::Footage) -> Vec<String> {
f.sequence.iter().map(|s| s.rsplit(['\\', '/']).next().unwrap_or_default().to_string()).collect()
}
/// A copy of `c` that borrows for any lifetime (a chunk is covariant in its lifetime).
fn shorten<'a>(c: &Chunk<'static>) -> Chunk<'a> {
c.clone()
}
/// A `Utf8` string chunk holding `s`.
fn utf8(s: &str) -> Chunk<'static> {
Chunk::data(*b"Utf8", s.as_bytes().to_vec())
}
/// A sequence whose naming has only a suffix (no filename prefix) imports as a sequence of its files.
#[test]
fn a_sequence_without_a_filename_prefix_imports_its_files() {
// `0001.png` .. `0003.png`: After Effects omits the empty prefix, so only `.png` follows the
// alias. Before, `.png` was read as the prefix, the suffix was missing and the item became a
// video linked to its folder.
let (p, r) = run(&with_sequence_naming(&[utf8(".png")], None), true);
r.unwrap();
let f = the_footage(&p);
assert_eq!(f.kind, FootageKind::Sequence);
assert_eq!(file_names(&f), ["0001.png", "0002.png", "0003.png"]);
assert!(f.path.ends_with("0001.png"), "{}", f.path);
}
/// A single `Utf8` naming part is read as the suffix or the prefix depending on how the stored first file name matches it.
#[test]
fn a_single_naming_part_is_placed_by_the_stored_first_file_name() {
// `0001_v2.png` ..: the one `Utf8` is the suffix, as the first file name (`opti`) ends with it.
let bytes = with_first_file(&with_sequence_naming(&[utf8("_v2.png")], None), "0001_v2.png");
let (p, r) = run(&bytes, true);
r.unwrap();
let f = the_footage(&p);
assert_eq!(f.kind, FootageKind::Sequence);
assert_eq!(file_names(&f), ["0001_v2.png", "0002_v2.png", "0003_v2.png"]);
// `shot_0001` ..: a name without an extension; the one `Utf8` is the prefix.
let bytes = with_first_file(&with_sequence_naming(&[utf8("shot_")], None), "shot_0001");
let (p, r) = run(&bytes, true);
r.unwrap();
assert_eq!(file_names(&the_footage(&p)), ["shot_0001", "shot_0002", "shot_0003"]);
// Without that evidence (the stored first file is `shot_0001.png`), a part that is no extension
// is not guessed: the item is reported.
let bytes = with_sequence_naming(&[utf8("_v2.png")], None);
reported_and_refused(&bytes, LossCategory::DamagedData, "image sequence naming not read, linked to its folder");
}
/// A file list containing a name with a separator, drive colon, dot directory or control character is refused and reported.
#[test]
fn a_file_list_with_a_name_that_is_not_a_plain_file_name_is_reported() {
// The listed names are joined to the folder: a separator, a drive colon or a dot directory
// would point outside it, and a NUL or another control character is no file name, so such a
// list is not read.
for hostile in ["..\\..\\x.png", "../x.png", "C:x.png", "..", ".", "x\0.png", "x\n.png"] {
let list = Chunk::list(*b"StVc", vec![Chunk::data(*b"StVS", 2u32.to_le_bytes().to_vec()), utf8("a.png"), utf8(hostile)]);
let bytes = with_sequence_naming(&[list], Some([u32::MAX, u32::MAX, 0]));
reported_and_refused(&bytes, LossCategory::DamagedData, "image sequence naming not read, linked to its folder");
}
}
/// A file list with a missing or too short count chunk is refused and reported.
#[test]
fn a_file_list_without_a_readable_count_is_reported() {
// No `StVS`, or one too short for its `u32`: the list is not read rather than taken whole.
for count in [None, Some(vec![2u8, 0])] {
let mut kids: Vec<Chunk<'static>> = count.map(|c| Chunk::data(*b"StVS", c)).into_iter().collect();
kids.extend([utf8("a.png"), utf8("b.png")]);
let bytes = with_sequence_naming(&[Chunk::list(*b"StVc", kids)], Some([u32::MAX, u32::MAX, 0]));
reported_and_refused(&bytes, LossCategory::DamagedData, "image sequence naming not read, linked to its folder");
}
}
/// A force-alphabetical sequence imports its stored file list in the stored order.
#[test]
fn a_sequence_imported_with_force_alphabetical_order_imports_its_file_list() {
// Force alphabetical order: `LIST StVc` with `StVS` (the count, little-endian) and one `Utf8`
// per file instead of the prefix and suffix; the numbering in `sspc` is not set. Before, the
// item became a video linked to its folder.
let list = Chunk::list(*b"StVc", vec![Chunk::data(*b"StVS", 3u32.to_le_bytes().to_vec()), utf8("b_take.png"), utf8("a_take.png"), utf8("c_take.png")]);
let (p, r) = run(&with_sequence_naming(&[list], Some([u32::MAX, u32::MAX, 0])), true);
let r = r.unwrap();
let f = the_footage(&p);
assert_eq!(f.kind, FootageKind::Sequence);
// The stored order is kept.
assert_eq!(file_names(&f), ["b_take.png", "a_take.png", "c_take.png"]);
assert!(f.path.ends_with("b_take.png"), "{}", f.path);
assert!(losses(&r, LossCategory::DamagedData).is_empty(), "{:?}", r.report.losses);
// The count bounds the list.
let list = Chunk::list(*b"StVc", vec![Chunk::data(*b"StVS", 2u32.to_le_bytes().to_vec()), utf8("x.png"), utf8("y.png"), utf8("z.png")]);
let (p, r) = run(&with_sequence_naming(&[list], Some([u32::MAX, u32::MAX, 0])), true);
r.unwrap();
assert_eq!(file_names(&the_footage(&p)), ["x.png", "y.png"]);
}
/// A sequence with no naming or an empty file list is reported and linked to its folder.
#[test]
fn a_sequence_whose_naming_cannot_be_read_is_reported() {
// No naming at all after the alias. Before, the item silently linked its folder.
let bytes = with_sequence_naming(&[], None);
reported_and_refused(&bytes, LossCategory::DamagedData, "image sequence naming not read, linked to its folder");
// An empty file list is no naming either.
let empty = Chunk::list(*b"StVc", vec![Chunk::data(*b"StVS", 0u32.to_le_bytes().to_vec())]);
let bytes = with_sequence_naming(&[empty], Some([u32::MAX, u32::MAX, 0]));
reported_and_refused(&bytes, LossCategory::DamagedData, "image sequence naming not read, linked to its folder");
}
#[test]
fn a_solid_with_a_nan_channel_is_repaired_and_reported() {
let nan = f32::NAN.to_be_bytes();
@@ -392,6 +553,186 @@ fn default_state_effect_records_import_like_written_ones() {
assert_eq!(clip, vec![("clipToOutputBlack".to_string(), effectcraft_keyframe::Value::Enum(1))]);
}
/// Sets the `cdat` of the value record `param` in the value group `tdgp` (the first `f64`), and
/// its state to `tdsb` when given. True when the group has that record.
fn patch_record(tdgp: &mut Chunk<'_>, param: &str, tdsb: Option<u32>, value: f64) -> bool {
let kids = tdgp.children_mut();
let Some(at) = kids.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.split(|x| *x == 0).next() == Some(param.as_bytes()))) else {
return false;
};
let Some(record) = kids.get_mut(at + 1).filter(|k| k.is_list(b"tdbs")) else { return false };
for k in record.children_mut() {
if k.is(b"tdsb")
&& let Some(state) = tdsb
{
set_data(k, 0, &state.to_be_bytes());
} else if k.is(b"cdat") {
set_data(k, 0, &value.to_be_bytes());
}
}
true
}
/// The project without its project-level effect definitions (`EfdG`; some files have none, seen
/// in the projects inside After Effects-made Motion Graphics templates) and with a second Glow
/// just above the first on the layer: a copy of it whose `parT` is empty, as such files write
/// every instance of a type but one. `edit_copy` is applied to every chunk of the copy; with
/// `empty_original` the original's `parT` is emptied too.
fn second_glow_without_definitions(edit_copy: &mut dyn FnMut(&mut Chunk<'_>), empty_original: bool) -> Vec<u8> {
fn empty_part(fx: &mut Chunk<'_>) {
for k in fx.children_mut() {
if let Body::List { list_type, children, .. } = &mut k.body
&& list_type == b"parT"
{
children.clear();
}
}
}
let mut file: AepFile<'_> = parse(SYNTHETIC).unwrap();
let mut added = 0;
visit(&mut file.chunks, &mut |c| {
if let Body::List { list_type, children, .. } = &mut c.body {
if list_type == b"EfdG" {
*list_type = *b"XfdG";
return;
}
if list_type != b"tdgp" {
return;
}
let Some(at) = children.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.split(|x| *x == 0).next() == Some(b"ADBE Glo2"))) else {
return;
};
let (Some(name), Some(mut fx)) = (children.get(at).cloned(), children.get(at + 1).filter(|k| k.is_list(b"sspc")).cloned()) else { return };
empty_part(&mut fx);
edit_copy(&mut fx);
visit(fx.children_mut(), edit_copy);
if empty_original && let Some(original) = children.get_mut(at + 1) {
empty_part(original);
}
children.insert(at, fx);
children.insert(at, name);
added += 1;
}
});
assert_eq!(added, 1, "one Glow on the layer");
write(&file).unwrap()
}
/// The imported Glows in stack order, each as its property values by property id.
fn glow_values(p: &Project) -> Vec<Vec<(String, effectcraft_keyframe::Value)>> {
let glow = effectcraft_project::GroupKind::Effect { effect: "ec.stylize.glow".to_string() };
let mut glows = Vec::new();
for item in p.items.values() {
if let ItemKind::Comp(c) = &item.kind {
for layer in &c.layers {
for node in layer.effects().map(|fx| fx.children.as_slice()).unwrap_or_default() {
if let effectcraft_project::Node::Group(g) = node
&& g.kind == glow
{
let mut values = Vec::new();
g.walk("", &mut |_, prop| values.push((prop.match_id.clone(), prop.value.clone())));
glows.push(values);
}
}
}
}
}
glows
}
/// The value of property `id` in `values`.
fn value_of<'v>(values: Option<&'v Vec<(String, effectcraft_keyframe::Value)>>, id: &str) -> Option<&'v effectcraft_keyframe::Value> {
values?.iter().find(|(i, _)| i == id).map(|(_, v)| v)
}
/// Every instance of an effect type but one has an empty `parT`; without a project-level copy the
/// others borrow the definitions of that one (#3170: 129 Color Controls of a template were
/// reported "without readable parameter definitions" and dropped).
#[test]
fn an_instance_without_definitions_imports_with_those_of_another_instance() {
// Both Glows import; before, the copy was reported and not imported.
let (p, r) = run(&second_glow_without_definitions(&mut |_| {}, false), true);
let r = r.unwrap();
let found = losses(&r, LossCategory::Effects);
assert!(!found.iter().any(|d| d.contains("without readable parameter definitions") || d.contains("no parameter definition")), "{found:?}");
assert_eq!(r.report.imported.effects, 3, "Levels and both Glows: {:?}", r.report.losses);
let glows = glow_values(&p);
assert_eq!(glows.len(), 2);
assert_eq!(glows.first(), glows.get(1), "the copy has the same records, so the same values");
// The values come from each instance's own records: the copy's A & B Midpoint (`-0011`) set
// to 0.75 arrives as 75 % in the copy only; the Glow it borrows from keeps its own.
let mut patched = 0;
let bytes = second_glow_without_definitions(
&mut |c| {
if c.is_list(b"tdgp") && patch_record(c, "ADBE Glo2-0011", None, 0.75) {
patched += 1;
}
},
false,
);
assert_eq!(patched, 1, "the copy's record");
let (p, r) = run(&bytes, true);
r.unwrap();
let glows = glow_values(&p);
let seventy_five = effectcraft_keyframe::Value::Scalar(75.0);
assert_eq!(value_of(glows.first(), "abMidpoint"), Some(&seventy_five));
let own = value_of(glows.get(1), "abMidpoint");
assert!(own.is_some() && own != Some(&seventy_five), "{own:?}");
}
/// A value record that the borrowed definitions do not name is reported, not dropped silently.
#[test]
fn a_value_record_the_borrowed_definitions_do_not_name_is_reported() {
let bytes = second_glow_without_definitions(
&mut |c| {
if c.is(b"tdmn") && c.bytes().is_some_and(|b| b.split(|x| *x == 0).next() == Some(b"ADBE Glo2-0011")) {
set_data(c, 0, b"ADBE Glo2-0099\0");
}
},
false,
);
let (_, r) = run(&bytes, true);
let r = r.unwrap();
let found = losses(&r, LossCategory::Effects);
assert!(found.iter().any(|d| d.contains("ADBE Glo2-0099") && d.contains("no parameter definition")), "{found:?}");
assert_eq!(r.report.imported.effects, 3, "{:?}", r.report.losses);
}
/// When no instance of the type keeps its definitions, every instance is reported.
#[test]
fn instances_without_any_definitions_are_reported() {
let (p, r) = run(&second_glow_without_definitions(&mut |_| {}, true), true);
let r = r.unwrap();
let found = losses(&r, LossCategory::Effects);
assert_eq!(found.iter().filter(|d| d.contains("ADBE Glo2") && d.contains("without readable parameter definitions, not imported")).count(), 2, "{found:?}");
assert_eq!(r.report.imported.effects, 1, "Levels only: {:?}", r.report.losses);
assert!(glow_values(&p).is_empty());
}
/// The reader's side: the copy's definitions come from the other instance.
#[test]
fn an_instance_without_definitions_reads_those_of_another_instance() {
let bytes = second_glow_without_definitions(&mut |_| {}, false);
let file = parse(&bytes).unwrap();
let project = effectcraft_aep::Project::new(&file).unwrap();
let defs = project.effect_definition_index();
let mut sources = Vec::new();
for item in project.all_items() {
let Some(comp) = item.comp() else { continue };
for layer in comp.layers() {
for fx in layer.effects().iter().filter(|fx| fx.match_name() == "ADBE Glo2") {
sources.push(defs.source(fx));
let params = defs.params(fx);
assert_eq!(params.len(), 16, "Glow's parameters");
assert_eq!(params.iter().all(|p| p.borrowed_definition), defs.source(fx) == effectcraft_aep::DefinitionSource::OtherInstance);
}
}
}
// The copy is above the instance it borrows from (earlier in the stack).
assert_eq!(sources, [effectcraft_aep::DefinitionSource::OtherInstance, effectcraft_aep::DefinitionSource::Instance]);
}
/// A record in a state other than 1 or 3 is not read and is reported.
#[test]
fn an_effect_record_in_another_state_is_reported_as_damaged() {
@@ -403,3 +744,60 @@ fn an_effect_record_in_another_state_is_reported_as_damaged() {
assert!(damaged.iter().any(|l| l.contains("unreadable value")), "tdsb {tdsb}: {:?}", r.report.losses);
}
}
/// The project with a value record for Glow's Color A (`ADBE Glo2-0012`, which the project does
/// not store) in state `tdsb` holding the ARGB doubles `argb` (0..255): a copy of the A & B
/// Midpoint record (`-0011`) made a 4-component colour without its expression.
fn with_color_a(tdsb: u32, argb: [f64; 4]) -> Vec<u8> {
let mut file: AepFile<'_> = parse(SYNTHETIC).unwrap();
let mut added = 0;
visit(&mut file.chunks, &mut |c| {
let Body::List { list_type, children, .. } = &mut c.body else { return };
if list_type != b"tdgp" {
return;
}
let Some(at) = children.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.split(|x| *x == 0).next() == Some(b"ADBE Glo2-0011"))) else {
return;
};
let (Some(mut name), Some(mut record)) = (children.get(at).cloned(), children.get(at + 1).filter(|k| k.is_list(b"tdbs")).cloned()) else { return };
set_data(&mut name, 0, b"ADBE Glo2-0012\0");
if let Body::List { children: kids, .. } = &mut record.body {
kids.retain(|k| !k.is(b"Utf8"));
for k in kids.iter_mut() {
if k.is(b"tdsb") {
set_data(k, 0, &tdsb.to_be_bytes());
} else if k.is(b"tdb4") {
// 4 components; no expression (bytes 76, 77 and 120).
set_data(k, 3, &[4]);
for byte in [76, 77, 120] {
set_data(k, byte, &[0]);
}
} else if k.is(b"cdat") {
*k = Chunk::data(*b"cdat", argb.iter().flat_map(|v| v.to_be_bytes()).collect::<Vec<u8>>());
}
}
}
children.insert(at + 2, record);
children.insert(at + 2, name);
added += 1;
});
assert_eq!(added, 2, "the instance and the EfdG copy");
write(&file).unwrap()
}
/// A colour recorded in the default state (`tdsb` 3) reads like a written one (#3170's "parameter
/// Color ... holds an unreadable value" on 0.21.1, which read only written records).
#[test]
fn a_default_state_colour_record_holds_the_value() {
let argb = [255.0, 51.0, 153.0, 255.0];
let (written, r1) = run(&with_color_a(1, argb), true);
let (default_state, r3) = run(&with_color_a(3, argb), true);
let (r1, r3) = (r1.unwrap(), r3.unwrap());
assert!(losses(&r3, LossCategory::DamagedData).is_empty(), "{:?}", r3.report.losses);
assert_eq!(r3.report.losses, r1.report.losses);
let colour = |p: &Project| value_of(glow_values(p).first(), "colorA").cloned();
let (unchanged, _) = run(SYNTHETIC, true);
assert!(colour(&written).is_some());
assert_eq!(colour(&default_state), colour(&written));
assert_ne!(colour(&default_state), colour(&unchanged), "the recorded colour, not the default");
}
+1
View File
@@ -44,6 +44,7 @@ impl EffectcraftApp {
starting_since: now,
first_cached: None,
shown_at: Default::default(),
stalls: 0,
};
if !self.playback.caching {
if plan.video {
+52 -4
View File
@@ -27,6 +27,26 @@ fn startup_frames(ahead: i64, window: i64, budget: usize, frame_bytes: usize) ->
ahead.min(window).min(fit).max(1)
}
/// How many cached frames ahead a held sound waits for before it starts again while the silent
/// preview keeps real time: the prefetch's `ahead`, doubled for each of the preview's `stalls`
/// that stopped the sound, never more than `cap` ([`held_audio_cap`]) nor less than one. Without
/// the doubling, a preview that renders just below real time started its sound as soon as
/// `ahead` frames were cached, ran out of them, stopped it again, and so on (#2473, #3260).
fn held_audio_frames(ahead: i64, cap: i64, stalls: u32) -> i64 {
ahead.saturating_mul(1i64 << stalls.min(32)).min(cap).max(1)
}
/// The most cached frames ahead a held sound waits for, with a RAM preview of `fit` frames.
/// `prefetch_playback` requests offsets below `fit - 1` (what `advance_playback` gives it), and
/// while the silent preview moves on, the frames it requested last are still rendering, so a
/// run of `fit` (or even `fit - 1`) frames ahead is never all cached until the end of the
/// preview cuts it short. Half of what is requested can be, and never fewer than the prefetch's
/// `ahead` (the wait before any stall) when that many are requested.
fn held_audio_cap(ahead: i64, fit: i64) -> i64 {
let reach = (fit - 1).max(1);
ahead.max(reach / 2).min(reach)
}
impl EffectcraftApp {
/// RAM preview: advance at the preview's frame rate when frames are cached; otherwise hold
/// and keep rendering ahead (the green cache bar fills, then playback runs in real time).
@@ -248,15 +268,17 @@ impl EffectcraftApp {
}
/// Held sound starts once the frames ahead are cached again after a stall (see
/// `Playback::audio_held`): the prefetch window when the silent preview keeps real time, else
/// the rest of the preview.
/// `Playback::audio_held`): the prefetch window when the silent preview keeps real time
/// (twice as many frames after each stall, see [`held_audio_frames`]), else the rest of the
/// preview.
fn start_held_audio(&mut self, pass: &PlaybackPass<'_>, fit: i64) {
let PlaybackPass { now, plan, series, cur, ahead, frame_rate: fr, .. } = *pass;
if !self.playback.audio_held {
return;
}
let Some(p) = plan else { return };
if (self.playback.keeps_up(now) && self.cached_ahead(&p, &series, cur, ahead.min(fit))) || self.cached_ahead(&p, &series, cur, fit) {
let need = held_audio_frames(ahead, held_audio_cap(ahead, fit), self.playback.stalls);
if (self.playback.keeps_up(now) && self.cached_ahead(&p, &series, cur, need)) || self.cached_ahead(&p, &series, cur, fit) {
self.playback.audio_held = false;
self.playback.start_wall = now;
self.playback.start_frame = cur;
@@ -294,6 +316,7 @@ impl EffectcraftApp {
// frame shows as it renders until the frames ahead are cached again.
self.audio = None;
self.playback.audio_held = true;
self.playback.stalls = self.playback.stalls.saturating_add(1);
self.playback.waiting = true;
// The rate measured before the stall no longer says it keeps up.
self.playback.shown_at.clear();
@@ -464,7 +487,32 @@ impl EffectcraftApp {
#[cfg(test)]
mod startup_tests {
use super::startup_frames;
use super::{held_audio_cap, held_audio_frames, startup_frames};
/// After each stall the held sound waits for twice as many frames ahead, up to its cap
/// (#2473, #3260).
#[test]
fn held_sound_waits_longer_after_each_stall() {
assert_eq!(held_audio_frames(8, 1000, 0), 8, "before any stall: the prefetch's frames ahead");
assert_eq!(held_audio_frames(8, 1000, 1), 16);
assert_eq!(held_audio_frames(8, 1000, 3), 64);
assert_eq!(held_audio_frames(8, 100, 4), 100, "never more than the cap");
assert_eq!(held_audio_frames(8, 100, u32::MAX), 100, "no overflow however many stalls");
assert_eq!(held_audio_frames(8, 5, 0), 5, "no more than the cap");
assert_eq!(held_audio_frames(0, 0, 2), 1, "at least the current frame");
}
/// The cap is what the prefetch can keep cached ahead of a moving preview: never `fit`, which
/// includes a frame it never requests, nor `fit - 1`, whose last frames are still rendering.
#[test]
fn a_held_sound_waits_only_for_frames_that_can_be_cached() {
assert_eq!(held_audio_cap(8, 1000), 499, "half of the 999 frames requested ahead");
assert_eq!(held_audio_cap(8, 10), 8, "never below the wait before any stall");
assert_eq!(held_audio_cap(8, 6), 5, "nor above the frames requested");
assert_eq!(held_audio_cap(8, 1), 1, "a one-frame RAM preview: the current frame");
assert_eq!(held_audio_cap(8, 0), 1);
assert_eq!(held_audio_frames(8, held_audio_cap(8, 10), 5), 8, "the backoff stops short of fit");
}
/// The frames a preview with sound waits for are always requested and fit together (#947).
#[test]
+5
View File
@@ -215,6 +215,11 @@ pub struct Playback {
pub first_cached: Option<f64>,
/// When the last frames were shown (seconds, the last second's worth): the achieved rate.
pub shown_at: std::collections::VecDeque<f64>,
/// How many stalls have stopped this preview's sound. After each one the held sound waits
/// for twice as many cached frames ahead before it starts again (up to what fits in the RAM
/// preview), so a preview that renders just below real time settles instead of starting and
/// stopping its sound every few frames, which plays as distortion (#2473, #3260).
pub stalls: u32,
}
impl Playback {
+137
View File
@@ -74,6 +74,16 @@ impl GatedFrames {
}
}
/// Cache every frame the app has asked for since the last call, and nothing else: a renderer
/// that keeps up with the real prefetch requests.
fn release_requested(&self, h: &mut Harness<'_, EffectcraftApp>) {
while h.state().frames.dispatch_remote() != 0 {}
for (_, width, height, done) in std::mem::take(&mut *self.pending.lock().unwrap()) {
let rgba = vec![0; (width as usize).saturating_mul(height as usize).saturating_mul(4)];
done(Ok(RemoteFrame { width, height, rgba, ms: 1.0 }));
}
}
/// Drop everything queued without caching it (the warm-up passes request viewer frames).
fn discard(&self, h: &mut Harness<'_, EffectcraftApp>) {
while h.state().frames.dispatch_remote() != 0 {}
@@ -269,6 +279,133 @@ fn preview_with_audio_sounds_without_caching_the_work_area() {
assert!((1..900).contains(&cached), "it started with {cached} of the 900 frames cached, not the whole work area");
}
/// Pretend the silent preview showed its last ten frames in real time, up to now: the next step
/// then finds it keeping up (`Playback::keeps_up`), however long steps take on the test host.
fn kept_up(h: &mut Harness<'_, EffectcraftApp>) {
let now = h.state().now();
h.state_mut().playback.shown_at = (0..10i32).map(|i| now - f64::from(9 - i) / 30.0).collect();
}
/// Frames `from..from + n`.
fn run(from: i64, n: i64) -> Vec<i64> {
(from..from + n).collect()
}
/// A preview that renders just below real time (#2473, #3260): once a stall stopped the sound,
/// `ahead` cached frames were enough to start it again while the silent preview kept up, so it
/// ran out of them, stopped and started again every few frames, which played as distortion.
/// After each stall the sound waits for twice as many cached frames ahead.
#[test]
fn each_stall_doubles_the_frames_ahead_the_sound_waits_for() {
let fake = Fake::default();
let (mut app, cid) = setup_long(true, &fake, 30.0);
let gate = GatedFrames::default();
let renderer: Arc<dyn RemoteFrames> = Arc::new(gate.clone());
app.frames.set_remote(Some(renderer));
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 30.0).build_eframe(|_| app);
h.state_mut().use_manual_clock();
h.run_steps(2);
gate.discard(&mut h);
let ahead = (h.state().frames.parallelism() * 2).max(4) as i64;
let now = h.state().now();
h.state_mut().play(now);
gate.render(&mut h, cid, &run(0, ahead));
step(&mut h);
assert!(h.state().audio.is_some(), "the sound started");
assert_eq!(h.state().playback.stalls, 0);
// A stall stops the sound.
gate.discard(&mut h);
h.state().frames.clear();
h.step();
assert!(h.state().audio.is_none() && h.state().playback.audio_held, "a stall stops the sound");
assert_eq!(h.state().playback.stalls, 1);
// One and a half times `ahead` cached while it keeps up: that used to start the sound again.
let at = frame(&h, cid);
gate.render(&mut h, cid, &run(at, ahead + ahead / 2));
kept_up(&mut h);
step(&mut h);
assert!(h.state().audio.is_none(), "after one stall, {} frames ahead are not enough", ahead + ahead / 2);
// Twice `ahead` are.
let at = frame(&h, cid);
gate.render(&mut h, cid, &run(at, 3 * ahead));
kept_up(&mut h);
step(&mut h);
assert!(h.state().audio.is_some(), "after one stall, {} frames ahead start the sound", 3 * ahead);
// A second stall: now three times `ahead` are not enough, and five times are.
gate.discard(&mut h);
h.state().frames.clear();
h.step();
assert!(h.state().audio.is_none() && h.state().playback.audio_held);
assert_eq!(h.state().playback.stalls, 2);
let at = frame(&h, cid);
gate.render(&mut h, cid, &run(at, 3 * ahead));
kept_up(&mut h);
step(&mut h);
assert!(h.state().audio.is_none(), "after two stalls, {} frames ahead are not enough", 3 * ahead);
let at = frame(&h, cid);
gate.render(&mut h, cid, &run(at, 5 * ahead));
kept_up(&mut h);
step(&mut h);
assert!(h.state().audio.is_some(), "after two stalls, {} frames ahead start the sound", 5 * ahead);
// A new preview starts counting again.
h.state_mut().stop();
let now = h.state().now();
h.state_mut().play(now);
assert_eq!(h.state().playback.stalls, 0);
}
/// Once the backoff reaches its cap, the held sound waits only for frames the prefetch really
/// requests and can keep cached while the silent preview moves on. It used to wait for `fit`
/// frames, one of which was never asked for, so a stall kept the preview silent almost to its
/// end even after rendering recovered.
#[test]
fn a_held_sound_at_the_ram_limit_starts_on_the_frames_prefetch_requests() {
let fake = Fake::default();
let (mut app, cid) = setup_long(true, &fake, 30.0);
let gate = GatedFrames::default();
let renderer: Arc<dyn RemoteFrames> = Arc::new(gate.clone());
app.frames.set_remote(Some(renderer));
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 30.0).build_eframe(|_| app);
h.state_mut().use_manual_clock();
h.run_steps(2);
gate.discard(&mut h);
let ahead = (h.state().frames.parallelism() * 2).max(4) as i64;
// A RAM preview of `ahead + 2` frames (a 64x36 frame is 9216 bytes; 90 % of the budget counts).
let fit = ahead + 2;
h.state().frames.set_budget(((fit as f64 + 0.5) * 9216.0 / 0.9) as usize);
h.state().frames.clear();
let now = h.state().now();
h.state_mut().play(now);
for _ in 0..90 {
gate.release_requested(&mut h);
step(&mut h);
if h.state().audio.is_some() {
break;
}
}
assert!(h.state().audio.is_some(), "the sound started");
// A stall, and enough stalls before it that the backoff is past the RAM preview's limit.
gate.discard(&mut h);
h.state().frames.clear();
h.step();
assert!(h.state().audio.is_none() && h.state().playback.audio_held, "a stall stops the sound");
let stalled = frame(&h, cid);
h.state_mut().playback.stalls = 8;
// Rendering keeps up again, caching only what playback itself requests.
let started = (0..4 * fit).position(|_| {
gate.release_requested(&mut h);
kept_up(&mut h);
step(&mut h);
h.state().audio.is_some()
});
assert!(started.is_some(), "the sound starts again on the frames the prefetch requests (RAM preview of {fit})");
assert!(frame(&h, cid) < stalled + 4 * fit, "near where it stalled ({stalled}), not at the end of the preview");
}
/// Pull `frames` stereo frames from the output, waiting for the feeder thread to mix them.
fn pull(feed: &AudioFeed, frames: usize) -> Vec<f32> {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);