mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
Merge pull request #1624 from storytold/codex/aaf-compat-schema
aaf: support compatible reference and locator conventions
This commit is contained in:
4 files changed
+113
-13
No files matched your search
+45
-2
@@ -57,13 +57,56 @@ pub struct WriteOptions<'a> {
|
||||
/// reported.
|
||||
pub fn write(doc: &Document, opts: &WriteOptions<'_>, report: &mut Report) -> Result<Vec<u8>> {
|
||||
let version = if opts.small_sectors { craft_cfb::Version::V3 } else { craft_cfb::Version::V4 };
|
||||
write::write(doc, version, &opts.identity, report)
|
||||
write::write(doc, version, &opts.identity, &ReferenceOptions::default(), report)
|
||||
}
|
||||
|
||||
/// Optional interpretation choices for applications retaining older interchange behavior.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct ReadOptions {
|
||||
/// Interpret a source clip's start using its target slot's edit rate.
|
||||
pub source_slot_rate: bool,
|
||||
/// Use the referenced mob name when the clip has no explicit name.
|
||||
pub infer_clip_name: bool,
|
||||
/// Decode percent escapes in relative network locators.
|
||||
pub decode_relative_locator: bool,
|
||||
/// Report selector and nested-scope flattening.
|
||||
pub report_flattening: bool,
|
||||
/// Accept truncated PCM WAVE essence summaries.
|
||||
pub permissive_pcm: bool,
|
||||
}
|
||||
impl Default for ReadOptions {
|
||||
fn default() -> Self {
|
||||
Self { source_slot_rate: true, infer_clip_name: true, decode_relative_locator: true, report_flattening: true, permissive_pcm: false }
|
||||
}
|
||||
}
|
||||
/// Read using explicitly selected interpretation choices.
|
||||
pub fn read_with_options(bytes: &[u8], options: &ReadOptions, report: &mut Report) -> Result<Document> {
|
||||
read::read(bytes, options, report)
|
||||
}
|
||||
|
||||
/// Optional compatibility choices for writing source references and locators.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct ReferenceOptions {
|
||||
/// Write source offsets in target-slot units.
|
||||
pub source_slot_rate: bool,
|
||||
/// Percent-encode relative locators.
|
||||
pub encode_relative_locator: bool,
|
||||
}
|
||||
impl Default for ReferenceOptions {
|
||||
fn default() -> Self {
|
||||
Self { source_slot_rate: true, encode_relative_locator: true }
|
||||
}
|
||||
}
|
||||
/// Write with explicitly selected reference and locator conventions.
|
||||
pub fn write_with_references(doc: &Document, opts: &WriteOptions<'_>, references: &ReferenceOptions, report: &mut Report) -> Result<Vec<u8>> {
|
||||
let version = if opts.small_sectors { craft_cfb::Version::V3 } else { craft_cfb::Version::V4 };
|
||||
write::write(doc, version, &opts.identity, references, report)
|
||||
}
|
||||
|
||||
/// Read an AAF file: its top-level compositions, the compositions nested in them and their
|
||||
/// sources. What cannot be represented is skipped and named in `report`.
|
||||
pub fn read(bytes: &[u8], report: &mut Report) -> Result<Document> {
|
||||
read::read(bytes, report)
|
||||
read::read(bytes, &ReadOptions::default(), report)
|
||||
}
|
||||
|
||||
/// Whether `bytes` look like an AAF file (a compound file whose root has the AAF root class or a
|
||||
|
||||
@@ -67,6 +67,7 @@ fn indirect_f64(d: &[u8]) -> Option<f64> {
|
||||
|
||||
struct R<'a> {
|
||||
report: &'a mut Report,
|
||||
options: super::ReadOptions,
|
||||
mobs: HashMap<MobId, &'a Obj>,
|
||||
essence: HashMap<MobId, &'a [u8]>,
|
||||
doc: Document,
|
||||
@@ -90,7 +91,7 @@ struct NestClip<'a> {
|
||||
/// Compositions nested deeper than this are read as gaps.
|
||||
const MAX_NESTING: usize = 16;
|
||||
|
||||
pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result<Document> {
|
||||
pub(crate) fn read(bytes: &[u8], options: &super::ReadOptions, report: &mut Report) -> Result<Document> {
|
||||
let root = store::read(bytes).map_err(|e| Error::Parse { format: "AAF", message: e })?;
|
||||
let header = root.strong(pid::ROOT_HEADER).ok_or_else(|| Error::Parse { format: "AAF", message: "no header object".into() })?;
|
||||
let content = header.strong(pid::CONTENT).ok_or_else(|| Error::Parse { format: "AAF", message: "no content storage".into() })?;
|
||||
@@ -106,7 +107,8 @@ pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result<Document> {
|
||||
essence.insert(id, d);
|
||||
}
|
||||
}
|
||||
let mut r = R { report, mobs, essence, doc: Document::default(), sources: HashMap::new(), nests: HashMap::new(), open: Vec::new() };
|
||||
let mut r =
|
||||
R { report, options: *options, mobs, essence, doc: Document::default(), sources: HashMap::new(), nests: HashMap::new(), open: Vec::new() };
|
||||
// Top-level compositions: those no other composition uses, among the ones tagged top-level
|
||||
// when any are. (Premiere Pro tags a nested sequence's composition top-level too; it is still
|
||||
// only a nested sequence.) Compositions that only use each other in a circle all count. The
|
||||
@@ -331,7 +333,9 @@ impl R<'_> {
|
||||
items
|
||||
}
|
||||
x if x == cls::SELECTOR => {
|
||||
self.report.info("a selector was flattened to its selected input");
|
||||
if self.options.report_flattening {
|
||||
self.report.info("a selector was flattened to its selected input");
|
||||
}
|
||||
match c.strong(pid::SELECTED) {
|
||||
Some(s) => {
|
||||
let mut items = self.segment(s, rate, kind, depth);
|
||||
@@ -342,7 +346,9 @@ impl R<'_> {
|
||||
}
|
||||
}
|
||||
x if x == cls::NESTED_SCOPE => {
|
||||
self.report.info("a nested scope was flattened to its final input");
|
||||
if self.options.report_flattening {
|
||||
self.report.info("a nested scope was flattened to its final input");
|
||||
}
|
||||
match c.objs(pid::NESTED_SLOTS).last() {
|
||||
Some(s) => {
|
||||
let mut items = self.segment(s, rate, kind, depth);
|
||||
@@ -388,7 +394,12 @@ impl R<'_> {
|
||||
let len = units_to_ticks(len_of(c), rate.0, rate.1);
|
||||
let tagged_name = tagged_values(c, pid::COMPONENT_USER_COMMENTS).into_iter().find(|(k, _)| k == "Clip Name").map(|(_, v)| v);
|
||||
let (Some(id), Some(slot)) = (c.mob_id(pid::SOURCE_ID), c.u32(pid::SOURCE_MOB_SLOT_ID)) else { return CItem::Filler(len) };
|
||||
let name = tagged_name.or_else(|| self.mobs.get(&id).and_then(|mob| mob.string(pid::MOB_NAME))).unwrap_or_default();
|
||||
let name = if self.options.infer_clip_name {
|
||||
tagged_name.or_else(|| self.mobs.get(&id).and_then(|mob| mob.string(pid::MOB_NAME)))
|
||||
} else {
|
||||
tagged_name
|
||||
}
|
||||
.unwrap_or_default();
|
||||
if id == [0; 32] {
|
||||
return CItem::Filler(len);
|
||||
}
|
||||
@@ -401,6 +412,7 @@ impl R<'_> {
|
||||
.and_then(|mob| mob.objs(pid::SLOTS).iter().find(|s| s.u32(pid::SLOT_ID) == Some(slot)))
|
||||
.and_then(|s| rate_of(s, pid::EDIT_RATE))
|
||||
.unwrap_or(rate);
|
||||
let source_rate = if self.options.source_slot_rate { source_rate } else { rate };
|
||||
let start = units_to_ticks(c.i64(pid::START_TIME).unwrap_or(0), source_rate.0, source_rate.1);
|
||||
match self.resolve(id, slot, start, kind, master_key, 0) {
|
||||
Some((source, t)) => CItem::Clip(CClip { len, source, start: t, gain: None, name }),
|
||||
@@ -506,6 +518,7 @@ impl R<'_> {
|
||||
.and_then(|m| m.objs(pid::SLOTS).iter().find(|s| s.u32(pid::SLOT_ID) == Some(next_slot)))
|
||||
.and_then(|s| rate_of(s, pid::EDIT_RATE))
|
||||
.unwrap_or(rate);
|
||||
let source_rate = if self.options.source_slot_rate { source_rate } else { rate };
|
||||
let inner = units_to_ticks(clip.i64(pid::START_TIME).unwrap_or(0), source_rate.0, source_rate.1);
|
||||
if next == [0; 32] {
|
||||
return None;
|
||||
@@ -545,7 +558,11 @@ impl R<'_> {
|
||||
DataKind::Sound => CKind::Sound,
|
||||
_ => kind,
|
||||
};
|
||||
let path = d.objs(pid::LOCATOR).iter().find_map(|l| l.string(pid::URL_STRING)).map(|u| locator_path(&u));
|
||||
let path = d
|
||||
.objs(pid::LOCATOR)
|
||||
.iter()
|
||||
.find_map(|l| l.string(pid::URL_STRING))
|
||||
.map(|u| if self.options.decode_relative_locator { locator_path(&u) } else { craft_timeline::path::file_url_to_path(&u) });
|
||||
let file_rate = rate_of(d, pid::SAMPLE_RATE).unwrap_or(rate);
|
||||
let length = d
|
||||
.i64(pid::FILE_LENGTH)
|
||||
@@ -604,7 +621,8 @@ impl R<'_> {
|
||||
if let Some(data) = self.essence.get(&id) {
|
||||
s.embedded = Some(data.to_vec());
|
||||
// embedded essence may be a whole WAVE / AIFF file
|
||||
if let Some((pcm, ch, sr, bits)) = crate::pcm::parse_wav(data) {
|
||||
let pcm = if self.options.permissive_pcm { craft_audio::essence_pcm::parse_wav(data) } else { crate::pcm::parse_wav(data) };
|
||||
if let Some((pcm, ch, sr, bits)) = pcm {
|
||||
s.embedded = Some(pcm);
|
||||
s.file_channels = ch as u32;
|
||||
s.channels = if s.channel.is_some() { 1 } else { ch as u32 };
|
||||
|
||||
@@ -557,3 +557,32 @@ fn relative_media_locator_round_trips_escaped_characters() {
|
||||
let got = read_clean(&bytes);
|
||||
assert!(got.sources.iter().any(|s| s.path.as_deref() == Some("Audio Files/100%20 #1.wav")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compatibility_options_keep_relative_paths_and_parent_rate() {
|
||||
let mut doc = crate::testdoc::sound_only(None);
|
||||
doc.compositions[0].sample_rate = 44_100;
|
||||
doc.sources[0].path = Some("Audio Files/100%20 #1.wav".into());
|
||||
let mut report = Report::default();
|
||||
let references = ReferenceOptions { source_slot_rate: false, encode_relative_locator: false };
|
||||
let bytes = write_with_references(&doc, &WriteOptions { identity: IDENTITY, small_sectors: false }, &references, &mut report).unwrap();
|
||||
let root = store::read(&bytes).unwrap();
|
||||
let mut all = Vec::new();
|
||||
walk(&root, &mut all);
|
||||
assert!(
|
||||
all.iter().filter(|o| o.class == cls::NETWORK_LOCATOR).any(|o| o.string(pid::URL_STRING).as_deref() == Some("Audio Files/100%20 #1.wav"))
|
||||
);
|
||||
let starts: Vec<i64> = all.iter().filter(|o| o.class == cls::SOURCE_CLIP).filter_map(|o| o.i64(pid::START_TIME)).collect();
|
||||
assert!(starts.contains(&17_640), "parent rate start: {starts:?}");
|
||||
let opts = ReadOptions {
|
||||
source_slot_rate: false,
|
||||
infer_clip_name: false,
|
||||
decode_relative_locator: false,
|
||||
report_flattening: false,
|
||||
permissive_pcm: true,
|
||||
};
|
||||
let back = read_with_options(&bytes, &opts, &mut report).unwrap();
|
||||
assert!(back.sources.iter().any(|s| s.path.as_deref() == Some("Audio Files/100%20 #1.wav")));
|
||||
let clip = back.compositions[0].tracks[0].items.iter().find_map(|i| if let CItem::Clip(c) = i { Some(c) } else { None }).unwrap();
|
||||
assert_eq!(clip.start, FrameRate::FPS_25.tick_of(10));
|
||||
}
|
||||
@@ -75,6 +75,7 @@ fn definition(class: Auid, id: Auid, name: &str) -> Obj {
|
||||
struct W<'a> {
|
||||
report: &'a mut Report,
|
||||
inexact: bool,
|
||||
references: super::ReferenceOptions,
|
||||
}
|
||||
|
||||
struct IdGen {
|
||||
@@ -350,9 +351,15 @@ fn add_tape_mob(first: &Source, tape_id: [u8; 32], timecode: Option<(i64, FrameR
|
||||
}
|
||||
|
||||
/// Serialise a composition document.
|
||||
pub(crate) fn write(doc: &Document, version: craft_cfb::Version, identity: &Identity<'_>, report: &mut Report) -> Result<Vec<u8>> {
|
||||
pub(crate) fn write(
|
||||
doc: &Document,
|
||||
version: craft_cfb::Version,
|
||||
identity: &Identity<'_>,
|
||||
references: &super::ReferenceOptions,
|
||||
report: &mut Report,
|
||||
) -> Result<Vec<u8>> {
|
||||
let comp = doc.compositions.first().ok_or(Error::Empty)?;
|
||||
let mut w = W { report, inexact: false };
|
||||
let mut w = W { report, inexact: false, references: *references };
|
||||
let mut ids = IdGen::for_document(doc, comp);
|
||||
let MediaOutput { mut mobs, essence, master_ref } = write_media(doc, &mut w, &mut ids);
|
||||
let mut used_ops: Vec<Auid> = Vec::new();
|
||||
@@ -360,7 +367,8 @@ pub(crate) fn write(doc: &Document, version: craft_cfb::Version, identity: &Iden
|
||||
|
||||
// ---- nested compositions: a clip refers to one as it does to a master mob, through the
|
||||
// composition's first track of the clip's kind (a broken-out channel: that channel's track)
|
||||
let mut refs = Refs { mobs: master_ref, rates: doc.sources.iter().enumerate().map(|(i, s)| (i, source_rate(s))).collect() };
|
||||
let rates = if references.source_slot_rate { doc.sources.iter().enumerate().map(|(i, s)| (i, source_rate(s))).collect() } else { HashMap::new() };
|
||||
let mut refs = Refs { mobs: master_ref, rates };
|
||||
let nested_ids: Vec<[u8; 32]> = doc.nested.iter().map(|_| ids.next()).collect();
|
||||
for (si, s) in doc.sources.iter().enumerate() {
|
||||
let Some((comp, id)) = s.nested.and_then(|n| Some((doc.nested.get(n)?, nested_ids.get(n)?))) else { continue };
|
||||
@@ -795,8 +803,10 @@ fn descriptor(w: &mut W, s: &Source) -> Obj {
|
||||
if let Some(p) = &s.path {
|
||||
let url = if craft_timeline::path::is_absolute(p) {
|
||||
craft_timeline::path::path_to_file_url(p, false)
|
||||
} else {
|
||||
} else if w.references.encode_relative_locator {
|
||||
craft_timeline::path::encode_uri_path(p)
|
||||
} else {
|
||||
p.clone()
|
||||
};
|
||||
d.set(pid::LOCATOR, Value::StrongVec(vec![Obj::new(cls::NETWORK_LOCATOR).data_prop(pid::URL_STRING, utf16z(&url))]));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user