Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

#323: AAF files open in readers that load the meta-dictionary

txiki739ccaa7212c1e1
diff --git a/crates/interchange/src/aaf/ids.rs b/crates/interchange/src/aaf/ids.rs
index c010f5a75..262d1d0b8 100644
--- a/crates/interchange/src/aaf/ids.rs
+++ b/crates/interchange/src/aaf/ids.rs
@@ -82,6 +82,10 @@ pub(crate) mod pid {
// Root
pub const ROOT_META_DICTIONARY: u16 = 0x0001;
pub const ROOT_HEADER: u16 = 0x0002;
+ // MetaDictionary and MetaDefinition
+ pub const META_CLASS_DEFINITIONS: u16 = 0x0003;
+ pub const META_TYPE_DEFINITIONS: u16 = 0x0004;
+ pub const META_IDENTIFICATION: u16 = 0x0005;
// Header
pub const BYTE_ORDER: u16 = 0x3B01;
pub const LAST_MODIFIED: u16 = 0x3B02;
diff --git a/crates/interchange/src/aaf/store.rs b/crates/interchange/src/aaf/store.rs
index c89b143d8..69b17e888 100644
--- a/crates/interchange/src/aaf/store.rs
+++ b/crates/interchange/src/aaf/store.rs
@@ -172,6 +172,8 @@ fn prop_name(pid: u16) -> &'static str {
match pid {
0x0001 => "MetaDictionary",
0x0002 => "Header",
+ 0x0003 => "ClassDefinitions",
+ 0x0004 => "TypeDefinitions",
0x3B03 => "Content",
0x3B04 => "Dictionary",
0x3B06 => "IdentificationList",
@@ -200,6 +202,16 @@ fn prop_name(pid: u16) -> &'static str {
}
}
+/// The key size of a strong reference set keyed by `key_pid`, for a set with no entry to take it
+/// from: AUIDs (definitions) are 16 bytes, MobIDs 32.
+fn set_key_size(key_pid: u16) -> usize {
+ match key_pid {
+ 0x0005 | 0x1B01 => 16,
+ 0x4401 | 0x2701 => 32,
+ _ => 0,
+ }
+}
+
fn storage_name(pid: u16) -> String {
let suffix = format!("-{pid:x}");
let name: String = prop_name(pid).chars().take(22 - suffix.len()).collect();
@@ -269,7 +281,7 @@ impl W {
index.extend_from_slice(&(items.len() as u32).to_le_bytes()); // first free key
index.extend_from_slice(&u32::MAX.to_le_bytes()); // last free key
let set_key = if let Value::StrongSet(_, k) = v { Some(*k) } else { None };
- let key_size = set_key.map(|k| items.first().and_then(|i| i.data(k)).map_or(0, <[u8]>::len)).unwrap_or(0);
+ let key_size = set_key.map(|k| items.first().and_then(|i| i.data(k)).map_or_else(|| set_key_size(k), <[u8]>::len)).unwrap_or(0);
if let Some(k) = set_key {
index.extend_from_slice(&k.to_le_bytes());
index.push(key_size as u8);
diff --git a/crates/interchange/src/aaf/tests.rs b/crates/interchange/src/aaf/tests.rs
index 4cfbf62ee..1cfe1c2eb 100644
--- a/crates/interchange/src/aaf/tests.rs
+++ b/crates/interchange/src/aaf/tests.rs
@@ -94,6 +94,32 @@ fn keys(dict: &Obj, p: u16) -> HashSet<Vec<u8>> {
dict.objs(p).iter().filter_map(|d| d.data(pid::IDENTIFICATION).map(<[u8]>::to_vec)).collect()
}
+/// #323: readers that load the meta-dictionary (pyaaf2) need its ClassDefinitions and
+/// TypeDefinitions sets. They are empty: everything written uses the baseline classes and types
+/// those readers already know.
+#[test]
+fn the_meta_dictionary_has_its_class_and_type_definition_sets() {
+ let (p, s) = project();
+ for small in [false, true] {
+ let (bytes, _) = export(&p, s, &AafOptions { small_sectors: small, ..Default::default() }).unwrap();
+ let cf = filmcraft_cfb::CompoundFile::open(&bytes).unwrap();
+ assert_eq!(cf.entries[cf.find("MetaDictionary-1").unwrap()].clsid, ids::META_DICTIONARY);
+ for name in ["ClassDefinitions-3", "TypeDefinitions-4"] {
+ // u32 count, u32 first free key, u32 last free key, u16 key pid, u8 key size
+ let index = cf.read_path(&format!("MetaDictionary-1/{name} index")).unwrap_or_else(|e| panic!("{name}: {e}"));
+ assert_eq!(index.len(), 15, "{name}: an empty set");
+ assert_eq!(index[0..4], 0u32.to_le_bytes(), "{name}: no entries");
+ assert_eq!(u16::from_le_bytes([index[12], index[13]]), pid::META_IDENTIFICATION, "{name}: keyed by the definition's AUID");
+ assert_eq!(index[14], 16, "{name}: 16-byte keys");
+ }
+ let root = store::read(&bytes).unwrap();
+ let meta = root.strong(pid::ROOT_META_DICTIONARY).unwrap();
+ for set in [pid::META_CLASS_DEFINITIONS, pid::META_TYPE_DEFINITIONS] {
+ assert!(matches!(meta.get(set), Some(Value::StrongSet(items, k)) if items.is_empty() && *k == pid::META_IDENTIFICATION), "{set:#06x}");
+ }
+ }
+}
+
#[test]
fn written_files_have_the_required_structure() {
let (p, s) = project();
diff --git a/crates/interchange/src/aaf/write.rs b/crates/interchange/src/aaf/write.rs
index f0876cfeb..ea0ea8e8f 100644
--- a/crates/interchange/src/aaf/write.rs
+++ b/crates/interchange/src/aaf/write.rs
@@ -349,9 +349,13 @@ pub(crate) fn write(doc: &Document, version: filmcraft_cfb::Version, report: &mu
.with(pid::CONTENT, Value::Strong(Box::new(content)))
.with(pid::DICTIONARY, Value::Strong(Box::new(dict)))
.with(pid::IDENTIFICATION_LIST, Value::StrongVec(vec![ident]));
- let root = Obj::new(ROOT)
- .with(pid::ROOT_META_DICTIONARY, Value::Strong(Box::new(Obj::new(META_DICTIONARY))))
- .with(pid::ROOT_HEADER, Value::Strong(Box::new(header)));
+ // The meta-dictionary holds only extensions to the baseline object model, and everything written
+ // here is baseline, so its class and type definition sets are empty; readers that load them
+ // (pyaaf2) still need the sets to be there (#323).
+ let meta = Obj::new(META_DICTIONARY)
+ .with(pid::META_CLASS_DEFINITIONS, Value::StrongSet(Vec::new(), pid::META_IDENTIFICATION))
+ .with(pid::META_TYPE_DEFINITIONS, Value::StrongSet(Vec::new(), pid::META_IDENTIFICATION));
+ let root = Obj::new(ROOT).with(pid::ROOT_META_DICTIONARY, Value::Strong(Box::new(meta))).with(pid::ROOT_HEADER, Value::Strong(Box::new(header)));
store::write(&root, version).map_err(Error::Other)
}