mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 20:25:02 +00:00
Merge remote-tracking branch 'origin/main' into w1777
This commit is contained in:
+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))]));
|
||||
}
|
||||
|
||||
@@ -29,7 +29,11 @@ mod roundtrip_tests;
|
||||
#[cfg(test)]
|
||||
mod testdoc;
|
||||
|
||||
/// Diagnostic types used by readers and writers.
|
||||
pub use craft_timeline::report;
|
||||
pub use craft_timeline::report::Report;
|
||||
/// Exact time types used by the composition model.
|
||||
pub use craft_timeline::time;
|
||||
|
||||
/// Error from a reader or writer. Unsupported *features* never error; they go in the [`Report`].
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
|
||||
+340
-10
@@ -12,6 +12,10 @@ type Uid = [u8; 12];
|
||||
|
||||
const MAX_REFERENCE_VISITS: usize = 65_536;
|
||||
|
||||
/// A source-clip search stopped at its work limit.
|
||||
#[derive(Clone, Copy)]
|
||||
struct SearchLimit;
|
||||
|
||||
struct R<'a, 'b> {
|
||||
c: &'b Container<'a>,
|
||||
report: &'b mut Report,
|
||||
@@ -21,15 +25,49 @@ struct R<'a, 'b> {
|
||||
media: HashMap<Uid, Vec<u8>>,
|
||||
doc: Document,
|
||||
sources: HashMap<(Uid, u32, String), usize>,
|
||||
/// Component visits left for [`R::segment`] over the whole document.
|
||||
budget: usize,
|
||||
/// Outcome of the source-clip search under each segment: deterministic, so it is made once.
|
||||
clip_search: HashMap<u32, std::result::Result<Option<u32>, SearchLimit>>,
|
||||
/// Visits and queued references left for source-clip searches over the whole document:
|
||||
/// distinct roots over one shared graph would otherwise each search it in full.
|
||||
search_budget: usize,
|
||||
}
|
||||
|
||||
/// Components one document may expand into timeline items; a small file can reference the same
|
||||
/// sequence from many places and would otherwise multiply without bound.
|
||||
const MAX_COMPONENT_VISITS: usize = 1_000_000;
|
||||
|
||||
/// Reported when the shared component budget runs out.
|
||||
const BUDGET_WARNING: &str = "OMF composition is too large to import completely (the remaining components are dropped)";
|
||||
/// Most visited components, and most queued references, one source-clip search may spend.
|
||||
const MAX_CLIP_SEARCH_VISITS: usize = 100_000;
|
||||
const MAX_CLIP_SEARCH_PENDING: usize = 1_000_000;
|
||||
/// Work all source-clip searches in one document may spend together.
|
||||
const MAX_DOCUMENT_SEARCH_WORK: usize = 2_000_000;
|
||||
|
||||
pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result<Document> {
|
||||
read_with_budget(bytes, report, MAX_COMPONENT_VISITS)
|
||||
}
|
||||
|
||||
fn read_with_budget(bytes: &[u8], report: &mut Report, budget: usize) -> Result<Document> {
|
||||
let perr = |m: String| Error::Parse { format: "OMF", message: m };
|
||||
let c = Container::open(bytes).map_err(perr)?;
|
||||
let class_of = |o: u32| c.get(o, "OMFI:OOBJ:ObjClass").and_then(|v| v.get(..4).map(|b| [b[0], b[1], b[2], b[3]]));
|
||||
let head = c.objects.keys().copied().find(|&o| class_of(o) == Some(*b"HEAD")).ok_or_else(|| perr("no OMF header object".into()))?;
|
||||
let be = c.get(head, "OMFI:HEAD:ByteOrder").is_none_or(|v| v.first() != Some(&0x49));
|
||||
let mut r = R { c: &c, report, be, mobs: HashMap::new(), media: HashMap::new(), doc: Document::default(), sources: HashMap::new() };
|
||||
let mut r = R {
|
||||
c: &c,
|
||||
report,
|
||||
be,
|
||||
mobs: HashMap::new(),
|
||||
media: HashMap::new(),
|
||||
doc: Document::default(),
|
||||
sources: HashMap::new(),
|
||||
budget,
|
||||
clip_search: HashMap::new(),
|
||||
search_budget: MAX_DOCUMENT_SEARCH_WORK,
|
||||
};
|
||||
let mob_list = r.refs(head, "OMFI:HEAD:Mobs");
|
||||
let mob_list = if mob_list.is_empty() {
|
||||
c.objects.keys().copied().filter(|&o| matches!(class_of(o).as_ref(), Some(b"CMOB" | b"MMOB" | b"SMOB"))).collect()
|
||||
@@ -66,8 +104,14 @@ pub(crate) fn read(bytes: &[u8], report: &mut Report) -> Result<Document> {
|
||||
cmobs.iter().copied().filter(|m| r.uid(*m, "OMFI:MOBJ:MobID").is_none_or(|id| !referenced.contains(&id))).collect()
|
||||
};
|
||||
for m in top {
|
||||
// every composition spends from the budget too: a list of up to 65,535 repeated entries must not yield that many
|
||||
if r.budget == 0 && !r.doc.compositions.is_empty() {
|
||||
r.report.warn(BUDGET_WARNING);
|
||||
break;
|
||||
}
|
||||
let comp = r.composition(m);
|
||||
r.doc.compositions.push(comp);
|
||||
r.budget = r.budget.saturating_sub(1);
|
||||
}
|
||||
if r.doc.compositions.is_empty() {
|
||||
return Err(Error::Empty);
|
||||
@@ -166,6 +210,13 @@ impl R<'_, '_> {
|
||||
let slots = self.refs(m, "OMFI:MOBJ:Slots");
|
||||
let mut na = 0;
|
||||
for s in slots {
|
||||
if self.budget == 0 {
|
||||
self.report.warn(BUDGET_WARNING);
|
||||
break;
|
||||
}
|
||||
// Every slot spends a unit, skipped ones too: a list of up to 65,535 repeated slots
|
||||
// must not cost that much work in each of 65,535 repeated compositions.
|
||||
self.budget -= 1;
|
||||
let Some(seg) = self.r#ref(s, "OMFI:MSLT:Segment") else { continue };
|
||||
let rate = self.rational(s, "OMFI:MSLT:EditRate").filter(|r| r.0 > 0 && r.1 > 0).unwrap_or((25, 1));
|
||||
let kind = self.data_kind(seg);
|
||||
@@ -213,13 +264,32 @@ impl R<'_, '_> {
|
||||
|
||||
fn segment(&mut self, seg: u32, rate: (i64, i64), depth: usize) -> Vec<CItem> {
|
||||
let len = units_to_ticks(self.len(seg), rate.0, rate.1);
|
||||
// Depth alone does not bound a graph that references the same component many times at
|
||||
// every level, so each visit also spends from a per-document budget.
|
||||
// A visit stopped by depth spends from it too.
|
||||
if self.budget == 0 {
|
||||
self.report.warn(BUDGET_WARNING);
|
||||
return vec![CItem::Filler(len)];
|
||||
}
|
||||
self.budget -= 1;
|
||||
if depth > 32 {
|
||||
return vec![CItem::Filler(len)];
|
||||
}
|
||||
match &self.class(seg) {
|
||||
b"SEQU" => {
|
||||
let mut out = Vec::new();
|
||||
let mut expanded = 0i64;
|
||||
for c in self.refs(seg, "OMFI:SEQU:Components") {
|
||||
if self.budget == 0 {
|
||||
// the rest of the sequence keeps its duration as one gap, not a gap per reference
|
||||
self.report.warn(BUDGET_WARNING);
|
||||
let rest = self.len(seg).saturating_sub(expanded);
|
||||
if rest > 0 {
|
||||
out.push(CItem::Filler(units_to_ticks(rest, rate.0, rate.1)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
expanded = expanded.saturating_add(self.len(c));
|
||||
out.extend(self.segment(c, rate, depth + 1));
|
||||
}
|
||||
out
|
||||
@@ -306,12 +376,66 @@ impl R<'_, '_> {
|
||||
.find(|&s| self.r#ref(s, "OMFI:MSLT:TrackDesc").and_then(|t| self.u32(t, "OMFI:TRKD:TrackID")) == Some(track))
|
||||
}
|
||||
|
||||
fn first_clip(&self, seg: u32) -> Option<u32> {
|
||||
match &self.class(seg) {
|
||||
b"SCLP" => Some(seg),
|
||||
b"SEQU" => self.refs(seg, "OMFI:SEQU:Components").into_iter().find_map(|c| self.first_clip(c)),
|
||||
_ => None,
|
||||
/// First source clip under `seg`, depth-first; a sequence referencing itself (directly or
|
||||
/// through a cycle) ends the search rather than the stack. `Err` when the search spent its
|
||||
/// budget of visited components or queued references before finishing (the answer is unknown).
|
||||
fn first_clip(&mut self, seg: u32) -> std::result::Result<Option<u32>, SearchLimit> {
|
||||
if let Some(found) = self.clip_search.get(&seg) {
|
||||
return *found;
|
||||
}
|
||||
let mut spent = 0;
|
||||
let found = if self.search_budget == 0 {
|
||||
Err(SearchLimit)
|
||||
} else {
|
||||
let visits = MAX_CLIP_SEARCH_VISITS.min(self.search_budget);
|
||||
let pending = MAX_CLIP_SEARCH_PENDING.min(self.search_budget);
|
||||
self.first_clip_within(seg, visits, pending, &mut spent)
|
||||
};
|
||||
self.search_budget = self.search_budget.saturating_sub(spent);
|
||||
self.clip_search.insert(seg, found);
|
||||
found
|
||||
}
|
||||
|
||||
/// `spent` receives the visits plus queued references the search used.
|
||||
fn first_clip_within(&self, seg: u32, max_visits: usize, max_pending: usize, spent: &mut usize) -> std::result::Result<Option<u32>, SearchLimit> {
|
||||
let mut visited = HashSet::new();
|
||||
let mut todo = vec![seg];
|
||||
let mut queued = 1usize;
|
||||
let found = Self::search_from(self, &mut visited, &mut todo, &mut queued, max_visits, max_pending);
|
||||
*spent = visited.len().saturating_add(queued);
|
||||
found
|
||||
}
|
||||
|
||||
fn search_from(
|
||||
&self,
|
||||
visited: &mut HashSet<u32>,
|
||||
todo: &mut Vec<u32>,
|
||||
queued: &mut usize,
|
||||
max_visits: usize,
|
||||
max_pending: usize,
|
||||
) -> std::result::Result<Option<u32>, SearchLimit> {
|
||||
while let Some(seg) = todo.pop() {
|
||||
if !visited.insert(seg) {
|
||||
continue;
|
||||
}
|
||||
if visited.len() > max_visits {
|
||||
return Err(SearchLimit);
|
||||
}
|
||||
match &self.class(seg) {
|
||||
b"SCLP" => return Ok(Some(seg)),
|
||||
b"SEQU" => {
|
||||
let comps = self.refs(seg, "OMFI:SEQU:Components");
|
||||
// sequences may share one component list, so queued references grow with expansions, not visits
|
||||
*queued = queued.saturating_add(comps.len());
|
||||
if *queued > max_pending {
|
||||
return Err(SearchLimit);
|
||||
}
|
||||
todo.extend(comps.into_iter().rev());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn resolve(&mut self, id: Uid, track: u32, start: Tick, master: Option<String>, depth: usize) -> Option<(usize, Tick)> {
|
||||
@@ -328,7 +452,13 @@ impl R<'_, '_> {
|
||||
match &self.class(mob) {
|
||||
b"MMOB" => {
|
||||
let key = master.unwrap_or_else(|| id.iter().map(|b| format!("{b:02x}")).collect());
|
||||
let clip = self.first_clip(seg)?;
|
||||
let clip = match self.first_clip(seg) {
|
||||
Ok(c) => c?,
|
||||
Err(SearchLimit) => {
|
||||
self.report.warn("a master mob is too large to search for its source clip (imported as a gap)");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let inner = units_to_ticks(self.i32(clip, "OMFI:SCLP:StartTime").unwrap_or(0), rate.0, rate.1);
|
||||
let next = self.uid(clip, "OMFI:SCLP:SourceID")?;
|
||||
let next_track = self.u32(clip, "OMFI:SCLP:SourceTrackID")?;
|
||||
@@ -353,7 +483,9 @@ impl R<'_, '_> {
|
||||
let file_rate = sr.filter(|r| r.0 > 0 && r.1 > 0).unwrap_or(rate);
|
||||
let length = units_to_ticks(self.i32(desc, "OMFI:MDFL:Length").unwrap_or(0), file_rate.0, file_rate.1);
|
||||
let summary = self.c.get(desc, "OMFI:WAVD:Summary").or_else(|| self.c.get(desc, "OMFI:AIFD:Summary")).unwrap_or_default();
|
||||
let fmt = crate::pcm::parse_wav(&summary).or_else(|| crate::pcm::parse_aiff(&summary)).map(|(_, ch, sr, bits)| (ch as u32, sr, bits));
|
||||
let fmt = craft_audio::essence_pcm::parse_wav(&summary)
|
||||
.or_else(|| craft_audio::essence_pcm::parse_aiff(&summary))
|
||||
.map(|(_, ch, sr, bits)| (ch as u32, sr, bits));
|
||||
let mut path = None;
|
||||
for l in self.refs(desc, "OMFI:MDES:Locator") {
|
||||
let p = match &self.class(l) {
|
||||
@@ -373,7 +505,8 @@ impl R<'_, '_> {
|
||||
let (file_channels, sample_rate, bits) = fmt.unwrap_or((1, file_rate.0 as u32, 16));
|
||||
let channel = (file_channels > 1 && sound_slots > 1).then(|| track.saturating_sub(1).min(file_channels - 1));
|
||||
// timecode of the physical source
|
||||
let tc = self.first_clip(seg).and_then(|clip| {
|
||||
let first = self.first_clip(seg).ok().flatten();
|
||||
let tc = first.and_then(|clip| {
|
||||
let tid = self.uid(clip, "OMFI:SCLP:SourceID")?;
|
||||
let tape = *self.mobs.get(&tid)?;
|
||||
let (start_tc, fps, trate) = self.refs(tape, "OMFI:MOBJ:Slots").into_iter().find_map(|s| {
|
||||
@@ -415,7 +548,7 @@ impl R<'_, '_> {
|
||||
nested: None,
|
||||
};
|
||||
if let Some(d) = self.media.get(&id) {
|
||||
if let Some((pcm, ch, sr, b)) = crate::pcm::parse_wav(d).or_else(|| crate::pcm::parse_aiff(d)) {
|
||||
if let Some((pcm, ch, sr, b)) = craft_audio::essence_pcm::parse_wav(d).or_else(|| craft_audio::essence_pcm::parse_aiff(d)) {
|
||||
s.embedded = Some(pcm);
|
||||
s.file_channels = ch as u32;
|
||||
s.channels = if s.channel.is_some() { 1 } else { ch as u32 };
|
||||
@@ -437,3 +570,200 @@ impl R<'_, '_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::super::bento::Writer;
|
||||
use super::*;
|
||||
|
||||
fn obj(w: &mut Writer, class: &[u8; 4]) -> u32 {
|
||||
let id = w.new_object();
|
||||
w.set(id, "OMFI:OOBJ:ObjClass", "omfi:ClassID", class);
|
||||
id
|
||||
}
|
||||
fn list(w: &mut Writer, o: u32, p: &str, targets: &[u32]) {
|
||||
let mut v = (targets.len() as u16).to_be_bytes().to_vec();
|
||||
for t in targets {
|
||||
v.extend_from_slice(&t.to_be_bytes());
|
||||
}
|
||||
w.set(o, p, "omfi:ObjRefArray", &v);
|
||||
}
|
||||
|
||||
fn search(max_visits: usize, max_pending: usize, build: impl FnOnce(&mut Writer) -> u32) -> std::result::Result<Option<u32>, SearchLimit> {
|
||||
let mut w = Writer::new();
|
||||
let root = build(&mut w);
|
||||
let bytes = w.finish();
|
||||
let c = Container::open(&bytes).unwrap();
|
||||
let mut report = Report::default();
|
||||
let r = R {
|
||||
c: &c,
|
||||
report: &mut report,
|
||||
be: true,
|
||||
mobs: HashMap::new(),
|
||||
media: HashMap::new(),
|
||||
doc: Document::default(),
|
||||
sources: HashMap::new(),
|
||||
budget: 0,
|
||||
clip_search: HashMap::new(),
|
||||
search_budget: MAX_DOCUMENT_SEARCH_WORK,
|
||||
};
|
||||
r.first_clip_within(root, max_visits, max_pending, &mut 0)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clip_search_reports_its_limits() {
|
||||
// found within the limits
|
||||
assert!(matches!(
|
||||
search(10, 10, |w| {
|
||||
let (s, c) = (obj(w, b"SEQU"), obj(w, b"SCLP"));
|
||||
list(w, s, "OMFI:SEQU:Components", &[c]);
|
||||
s
|
||||
}),
|
||||
Ok(Some(_))
|
||||
));
|
||||
// too many distinct components before any clip
|
||||
assert!(
|
||||
search(5, 1000, |w| {
|
||||
let (s, f) = (obj(w, b"SEQU"), (0..20).map(|_| obj(w, b"FILL")).collect::<Vec<_>>());
|
||||
list(w, s, "OMFI:SEQU:Components", &f);
|
||||
s
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
// few visits, but sequences that each queue a long list of references
|
||||
assert!(
|
||||
search(1000, 500, |w| {
|
||||
let f = obj(w, b"FILL");
|
||||
let inner: Vec<u32> = (0..20).map(|_| obj(w, b"SEQU")).collect();
|
||||
for &i in &inner {
|
||||
list(w, i, "OMFI:SEQU:Components", &[f; 100]);
|
||||
}
|
||||
let s = obj(w, b"SEQU");
|
||||
list(w, s, "OMFI:SEQU:Components", &inner);
|
||||
s
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
// a search that ends without a clip is not a limit
|
||||
assert!(matches!(search(10, 10, |w| obj(w, b"FILL")), Ok(None)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skipped_slots_spend_the_budget() {
|
||||
let mut w = Writer::new();
|
||||
let head = obj(&mut w, b"HEAD");
|
||||
let cm = obj(&mut w, b"CMOB");
|
||||
let slot = obj(&mut w, b"MSLT");
|
||||
list(&mut w, cm, "OMFI:MOBJ:Slots", &[slot; 200]);
|
||||
list(&mut w, head, "OMFI:HEAD:Mobs", &[cm]);
|
||||
list(&mut w, head, "OMFI:HEAD:PrimaryMobs", &[cm]);
|
||||
let bytes = w.finish();
|
||||
let mut report = Report::default();
|
||||
read_with_budget(&bytes, &mut report, 50).unwrap();
|
||||
assert!(report.mentions(BUDGET_WARNING), "slots with no segment cost nothing before");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_source_clip_search_is_made_once_per_segment() {
|
||||
let mut w = Writer::new();
|
||||
let (s, c) = (obj(&mut w, b"SEQU"), obj(&mut w, b"SCLP"));
|
||||
list(&mut w, s, "OMFI:SEQU:Components", &[c]);
|
||||
let bytes = w.finish();
|
||||
let c = Container::open(&bytes).unwrap();
|
||||
let mut report = Report::default();
|
||||
let mut r = R {
|
||||
c: &c,
|
||||
report: &mut report,
|
||||
be: true,
|
||||
mobs: HashMap::new(),
|
||||
media: HashMap::new(),
|
||||
doc: Document::default(),
|
||||
sources: HashMap::new(),
|
||||
budget: 0,
|
||||
clip_search: HashMap::new(),
|
||||
search_budget: MAX_DOCUMENT_SEARCH_WORK,
|
||||
};
|
||||
let first = r.first_clip(s).ok();
|
||||
assert!(matches!(first, Some(Some(_))));
|
||||
assert_eq!(r.clip_search.len(), 1);
|
||||
assert_eq!(r.first_clip(s).ok(), first);
|
||||
assert_eq!(r.clip_search.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_slots_and_compositions_spend_the_budget() {
|
||||
let mut w = Writer::new();
|
||||
let head = obj(&mut w, b"HEAD");
|
||||
let cm = obj(&mut w, b"CMOB");
|
||||
let dd = obj(&mut w, b"DDEF");
|
||||
w.set(dd, "OMFI:DDEF:DataKindID", "omfi:UniqueName", b"omfi:data:Sound\0");
|
||||
let fill = obj(&mut w, b"FILL");
|
||||
w.set(fill, "OMFI:CPNT:DataKind", "omfi:ObjRef", &dd.to_be_bytes());
|
||||
let slot = obj(&mut w, b"MSLT");
|
||||
w.set(slot, "OMFI:MSLT:Segment", "omfi:ObjRef", &fill.to_be_bytes());
|
||||
list(&mut w, cm, "OMFI:MOBJ:Slots", &[slot; 200]);
|
||||
list(&mut w, head, "OMFI:HEAD:Mobs", &[cm]);
|
||||
list(&mut w, head, "OMFI:HEAD:PrimaryMobs", &[cm; 200]);
|
||||
let bytes = w.finish();
|
||||
let mut report = Report::default();
|
||||
let doc = read_with_budget(&bytes, &mut report, 50).unwrap();
|
||||
let tracks: usize = doc.compositions.iter().map(|c| c.tracks.len()).sum();
|
||||
assert!(tracks <= 50, "{tracks} tracks from a budget of 50");
|
||||
assert!(doc.compositions.len() <= 2, "{} compositions", doc.compositions.len());
|
||||
assert!(report.has_warnings());
|
||||
}
|
||||
|
||||
fn with_reader<T>(bytes: &[u8], budget: usize, search_budget: usize, f: impl FnOnce(&mut R) -> T) -> (T, Report) {
|
||||
let c = Container::open(bytes).unwrap();
|
||||
let mut report = Report::default();
|
||||
let mut r = R {
|
||||
c: &c,
|
||||
report: &mut report,
|
||||
be: true,
|
||||
mobs: HashMap::new(),
|
||||
media: HashMap::new(),
|
||||
doc: Document::default(),
|
||||
sources: HashMap::new(),
|
||||
budget,
|
||||
clip_search: HashMap::new(),
|
||||
search_budget,
|
||||
};
|
||||
let out = f(&mut r);
|
||||
(out, report)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn depth_limited_components_spend_the_budget() {
|
||||
// a chain 33 sequences deep whose last one repeats a depth-limited sequence many times
|
||||
let mut w = Writer::new();
|
||||
let deep = obj(&mut w, b"SEQU");
|
||||
let chain: Vec<u32> = (0..33).map(|_| obj(&mut w, b"SEQU")).collect();
|
||||
for pair in chain.windows(2) {
|
||||
list(&mut w, pair[0], "OMFI:SEQU:Components", &[pair[1]]);
|
||||
}
|
||||
list(&mut w, chain[32], "OMFI:SEQU:Components", &[deep; 1000]);
|
||||
let bytes = w.finish();
|
||||
let (items, report) = with_reader(&bytes, 100, MAX_DOCUMENT_SEARCH_WORK, |r| r.segment(chain[0], (48_000, 1), 0));
|
||||
assert!(items.len() <= 100, "{} items from a budget of 100", items.len());
|
||||
assert!(report.mentions(BUDGET_WARNING));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn source_clip_searches_share_a_document_budget() {
|
||||
// distinct roots over one shared clip-free graph
|
||||
let mut w = Writer::new();
|
||||
let fills: Vec<u32> = (0..50).map(|_| obj(&mut w, b"FILL")).collect();
|
||||
let shared = obj(&mut w, b"SEQU");
|
||||
list(&mut w, shared, "OMFI:SEQU:Components", &fills);
|
||||
let roots: Vec<u32> = (0..30).map(|_| obj(&mut w, b"SEQU")).collect();
|
||||
for &root in &roots {
|
||||
list(&mut w, root, "OMFI:SEQU:Components", &[shared]);
|
||||
}
|
||||
let bytes = w.finish();
|
||||
let (results, _) = with_reader(&bytes, 0, 500, |r| roots.iter().map(|&root| r.first_clip(root)).collect::<Vec<_>>());
|
||||
assert!(matches!(results[0], Ok(None)), "the first search fits");
|
||||
assert!(results.iter().any(|r| r.is_err()), "later searches run out of the shared budget");
|
||||
let (results, _) = with_reader(&bytes, 0, MAX_DOCUMENT_SEARCH_WORK, |r| roots.iter().map(|&root| r.first_clip(root)).collect::<Vec<_>>());
|
||||
assert!(results.iter().all(|r| matches!(r, Ok(None))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,3 @@
|
||||
pub(crate) fn parse_wav(b: &[u8]) -> Option<(Vec<u8>, u16, u32, u16)> {
|
||||
craft_audio::wav_pcm(b).map(|p| (p.data, p.channels, p.sample_rate, p.bits))
|
||||
}
|
||||
|
||||
/// The same for an AIFF / AIFF-C (`NONE`, `twos`, `sowt`) file.
|
||||
pub(crate) fn parse_aiff(b: &[u8]) -> Option<(Vec<u8>, u16, u32, u16)> {
|
||||
craft_audio::aiff_pcm(b).map(|p| (p.data, p.channels, p.sample_rate, p.bits))
|
||||
}
|
||||
|
||||
@@ -63,6 +63,7 @@ pub fn parse_aiff(b: &[u8]) -> Option<(Vec<u8>, u16, u32, u16)> {
|
||||
if b.len() < 12 || &b[..4] != b"FORM" || !(&b[8..12] == b"AIFF" || &b[8..12] == b"AIFC") {
|
||||
return None;
|
||||
}
|
||||
let aifc = &b[8..12] == b"AIFC";
|
||||
let mut at = 12;
|
||||
let mut comm = None;
|
||||
while at + 8 <= b.len() {
|
||||
@@ -73,7 +74,17 @@ pub fn parse_aiff(b: &[u8]) -> Option<(Vec<u8>, u16, u32, u16)> {
|
||||
let ch = u16::from_be_bytes([body[0], body[1]]);
|
||||
let bits = u16::from_be_bytes([body[6], body[7]]);
|
||||
let sr = from_extended(body[8..18].try_into().ok()?).round() as u32;
|
||||
let little = body.len() >= 22 && &body[18..22] == b"sowt";
|
||||
// AIFF has no compression code (trailing COMM bytes mean nothing); AIFF-C must name one
|
||||
// of the integer PCM codes, or the samples are not integer PCM at all
|
||||
let little = if aifc {
|
||||
match body.get(18..22)? {
|
||||
b"NONE" | b"twos" => false,
|
||||
b"sowt" => true,
|
||||
_ => return None,
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
comm = Some((ch, sr, bits, little));
|
||||
} else if id == b"SSND" && body.len() >= 8 {
|
||||
let (ch, sr, bits, little) = comm?;
|
||||
@@ -144,4 +155,35 @@ mod tests {
|
||||
let _ = parse_aiff(&a[..cut.min(a.len())]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aifc_needs_an_integer_pcm_compression_code() {
|
||||
let file = |kind: &[u8; 4], code: &[u8]| {
|
||||
let mut comm = vec![0, 1, 0, 0, 0, 1, 0, 16];
|
||||
comm.extend_from_slice(&[0x40, 0x0E, 0xBB, 0x80, 0, 0, 0, 0, 0, 0]);
|
||||
comm.extend_from_slice(code);
|
||||
let mut body = kind.to_vec();
|
||||
body.extend_from_slice(b"COMM");
|
||||
body.extend_from_slice(&(comm.len() as u32).to_be_bytes());
|
||||
body.extend_from_slice(&comm);
|
||||
if comm.len() & 1 == 1 {
|
||||
body.push(0);
|
||||
}
|
||||
body.extend_from_slice(b"SSND");
|
||||
body.extend_from_slice(&10u32.to_be_bytes());
|
||||
body.extend_from_slice(&[0; 8]);
|
||||
body.extend_from_slice(&[0x12, 0x34]);
|
||||
let mut v = b"FORM".to_vec();
|
||||
v.extend_from_slice(&(body.len() as u32).to_be_bytes());
|
||||
v.extend_from_slice(&body);
|
||||
v
|
||||
};
|
||||
assert_eq!(parse_aiff(&file(b"AIFC", b"NONE")).map(|p| p.0), Some(vec![0x34, 0x12]));
|
||||
assert_eq!(parse_aiff(&file(b"AIFC", b"twos")).map(|p| p.0), Some(vec![0x34, 0x12]));
|
||||
assert_eq!(parse_aiff(&file(b"AIFC", b"sowt")).map(|p| p.0), Some(vec![0x12, 0x34]));
|
||||
assert!(parse_aiff(&file(b"AIFC", b"fl32")).is_none(), "float samples are not integer PCM");
|
||||
assert!(parse_aiff(&file(b"AIFC", b"")).is_none(), "AIFF-C without a compression code");
|
||||
// plain AIFF ignores trailing COMM bytes
|
||||
assert_eq!(parse_aiff(&file(b"AIFF", b"sowt")).map(|p| p.0), Some(vec![0x34, 0x12]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,6 +198,15 @@ fn data_end(b: &[u8]) -> (DataEnd, u64) {
|
||||
data_end_mode(b, false)
|
||||
}
|
||||
|
||||
/// Whether JPEG `b` holds too little scan data for a `w` × `h` frame to be worth decoding: no scan
|
||||
/// data at all, or under one byte for every [`MAX_PIXELS_PER_BYTE`] pixels (counted on the scan
|
||||
/// data the marker walk crosses, so APP padding doesn't count). `zune-jpeg` allocates the whole
|
||||
/// output before it reads a scan, so a few bytes could otherwise ask for a frame of any size.
|
||||
pub(crate) fn scan_too_short(b: &[u8], w: u32, h: u32) -> bool {
|
||||
let (end, scan_bytes) = data_end(b);
|
||||
end == DataEnd::Empty || u64::from(w) * u64::from(h) / MAX_PIXELS_PER_BYTE > scan_bytes
|
||||
}
|
||||
|
||||
/// Whether the marker walk actually reaches the first EOI, with malformed segment lengths refused.
|
||||
pub(crate) fn reaches_eoi(b: &[u8]) -> bool {
|
||||
data_end_mode(b, true).0 == DataEnd::Complete
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
//! A JPEG's headers and four-component samples read the way `zune-jpeg` and libjpeg-style readers
|
||||
//! give them, for apps that keep a CMYK JPEG's inks: the component count and ICC profile from the
|
||||
//! headers alone, and the ink amounts of a CMYK or YCCK JPEG.
|
||||
//!
|
||||
//! [`crate::decode`] reads the same files differently, on purpose: it keeps the samples of a
|
||||
//! four-component JPEG without Adobe's marker as they are, rounds its YCCK conversion, and hands
|
||||
//! arithmetic-coded, lossless and 12-bit files to its whole-coefficient decoder. The readers here
|
||||
//! follow the older convention instead, so a document that kept a CMYK JPEG's inks that way reads
|
||||
//! the same: every four-component file is taken to hold Adobe's inverted samples, YCCK is turned
|
||||
//! into CMYK as libjpeg does (truncating), and only the 8-bit Huffman-coded frames `zune-jpeg`
|
||||
//! decodes are read.
|
||||
|
||||
use zune_core::bytestream::ZCursor;
|
||||
use zune_core::colorspace::ColorSpace;
|
||||
use zune_core::options::DecoderOptions;
|
||||
use zune_jpeg::JpegDecoder;
|
||||
|
||||
use crate::Format;
|
||||
use crate::error::CodecError;
|
||||
use crate::image::{ChannelLayout, Image, SampleType};
|
||||
use crate::options::Limits;
|
||||
|
||||
/// A decoder of `bytes` whose headers have been read, with the default options (16384 pixels a
|
||||
/// side at most); `None` when they aren't a JPEG or its headers can't be read.
|
||||
fn headers(bytes: &[u8]) -> Option<JpegDecoder<ZCursor<&[u8]>>> {
|
||||
if !bytes.starts_with(&[0xFF, 0xD8]) {
|
||||
return None;
|
||||
}
|
||||
let mut d = JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default());
|
||||
d.decode_headers().ok()?;
|
||||
Some(d)
|
||||
}
|
||||
|
||||
/// The number of colour components the frame header of JPEG `bytes` declares, whatever it is;
|
||||
/// `None` when they aren't a JPEG whose headers can be read.
|
||||
pub fn jpeg_components(bytes: &[u8]) -> Option<usize> {
|
||||
Some(usize::from(headers(bytes)?.info()?.components))
|
||||
}
|
||||
|
||||
/// The width and height the frame header of JPEG `bytes` declares, for any colour type; `None`
|
||||
/// when they aren't a JPEG whose headers can be read (which includes sides past 16384 pixels).
|
||||
pub fn jpeg_dimensions(bytes: &[u8]) -> Option<(u32, u32)> {
|
||||
let (w, h) = headers(bytes)?.dimensions()?;
|
||||
Some((u32::try_from(w).ok()?, u32::try_from(h).ok()?))
|
||||
}
|
||||
|
||||
/// The ICC profile of JPEG `bytes`, put together from all its numbered `ICC_PROFILE` APP2
|
||||
/// segments; `None` without one, when it is empty, and when the headers can't be read.
|
||||
pub fn jpeg_icc_profile(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
headers(bytes)?.icc_profile().filter(|icc| !icc.is_empty())
|
||||
}
|
||||
|
||||
/// The ink amounts of a four-component (CMYK or YCCK) JPEG: C, M, Y and K bytes a pixel, 0 for
|
||||
/// no ink, as a [`ChannelLayout::Cmyk`] image. The samples are taken to be Adobe's inverted ones
|
||||
/// whether or not the file has Adobe's marker; YCCK is first turned into CMYK as libjpeg does
|
||||
/// (the complement of the YCbCr colour's RGB, each value truncated).
|
||||
///
|
||||
/// Errors: bytes that aren't a JPEG with readable headers ([`CodecError::InvalidImage`]); one of
|
||||
/// other than four components, or a frame `zune-jpeg` doesn't decode, such as an arithmetic-coded
|
||||
/// or 12-bit one ([`CodecError::Unsupported`]); a size past `limits`; scan data too short for the
|
||||
/// frame (under a byte for every 2048 pixels, refused before anything is allocated); and data that
|
||||
/// doesn't decode,
|
||||
/// including frames `zune-jpeg` panics on, which are caught.
|
||||
pub fn decode_jpeg_inks(bytes: &[u8], limits: &Limits) -> Result<Image, CodecError> {
|
||||
let bad = |m: &str| CodecError::InvalidImage(format!("JPEG inks: {m}"));
|
||||
let mut d = headers(bytes).ok_or_else(|| bad("not a JPEG with readable headers"))?;
|
||||
let space = d.input_colorspace().ok_or_else(|| bad("no colour space"))?;
|
||||
let components = d.info().map_or(0, |i| i.components);
|
||||
if components != 4 || !matches!(space, ColorSpace::CMYK | ColorSpace::YCCK) {
|
||||
return Err(CodecError::unsupported(Format::Jpeg, format!("inks of a {components}-component {space:?} image")));
|
||||
}
|
||||
let (w, h) = d.dimensions().ok_or_else(|| bad("no size"))?;
|
||||
let (w, h) = (u32::try_from(w).map_err(|_| bad("width"))?, u32::try_from(h).map_err(|_| bad("height"))?);
|
||||
limits.check(w, h, ChannelLayout::Cmyk, SampleType::U8)?;
|
||||
if crate::codecs::jpeg::scan_too_short(bytes, w, h) {
|
||||
return Err(bad("the scan data is too short for the frame it declares"));
|
||||
}
|
||||
d.set_options(DecoderOptions::default().jpeg_set_out_colorspace(space));
|
||||
// zune-jpeg 0.5 asserts on some subsampled four-component frames whose first component isn't
|
||||
// the largest (a scratch row too short for its upsampler): that is an error here, not a panic.
|
||||
let decoded = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| d.decode()));
|
||||
let mut data = decoded
|
||||
.map_err(|_| CodecError::unsupported(Format::Jpeg, "inks: zune-jpeg failed on this file"))?
|
||||
.map_err(|e| CodecError::unsupported(Format::Jpeg, format!("inks: {e}")))?;
|
||||
for p in data.as_chunks_mut::<4>().0 {
|
||||
if space == ColorSpace::YCCK {
|
||||
let [y, cb, cr] = [p[0], p[1], p[2]].map(f32::from);
|
||||
// libjpeg's YCCK → CMYK: the complement of the YCbCr colour's RGB.
|
||||
p[0] = (434.456 - y - 1.402 * cr).clamp(0.0, 255.0) as u8;
|
||||
p[1] = (119.541 - y + 0.344 * cb + 0.714 * cr).clamp(0.0, 255.0) as u8;
|
||||
p[2] = (481.816 - y - 1.772 * cb).clamp(0.0, 255.0) as u8;
|
||||
}
|
||||
for v in p.iter_mut() {
|
||||
*v = 255 - *v;
|
||||
}
|
||||
}
|
||||
Image::from_u8(w, h, ChannelLayout::Cmyk, data)
|
||||
}
|
||||
@@ -26,6 +26,7 @@ mod fidelity;
|
||||
mod format;
|
||||
mod image;
|
||||
mod inspection;
|
||||
pub mod jpeg_inks;
|
||||
pub mod jpeg_sef;
|
||||
mod lab;
|
||||
mod options;
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
//! `jpeg_inks`: four-component JPEGs read as Adobe-inverted inks, as `zune-jpeg`-based readers
|
||||
//! kept them, beside `decode`'s own reading.
|
||||
|
||||
use craft_image_codecs::jpeg_inks::{decode_jpeg_inks, jpeg_components, jpeg_dimensions, jpeg_icc_profile};
|
||||
use craft_image_codecs::*;
|
||||
|
||||
/// A `w` × `h` four-component JPEG of `samples` at quality 100, written by `jpeg-encoder` as
|
||||
/// `ty`, with ICC profile `icc` when given.
|
||||
fn four(w: u16, h: u16, samples: &[u8], ty: jpeg_encoder::ColorType, icc: Option<&[u8]>) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
let mut enc = jpeg_encoder::Encoder::new(&mut out, 100);
|
||||
enc.set_sampling_factor(jpeg_encoder::SamplingFactor::R_4_4_4);
|
||||
if let Some(icc) = icc {
|
||||
enc.add_icc_profile(icc).unwrap();
|
||||
}
|
||||
enc.encode(samples, w, h, ty).unwrap();
|
||||
out
|
||||
}
|
||||
|
||||
/// Flat 8×8 blocks of ink, so the coding keeps each block's value.
|
||||
fn blocks(w: usize, h: usize) -> Vec<u8> {
|
||||
(0..w * h)
|
||||
.flat_map(|i| {
|
||||
let b = ((i % w) / 8 + (i / w) / 8 * 7) as u8;
|
||||
[b.wrapping_mul(37), b.wrapping_mul(91), 255 - b.wrapping_mul(13), b.wrapping_mul(53)]
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The segments of `bytes` up to the scan without Adobe's APP14 marker.
|
||||
fn without_adobe(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut out = bytes[..2].to_vec();
|
||||
let mut at = 2;
|
||||
loop {
|
||||
let marker = bytes[at + 1];
|
||||
if marker == 0xDA {
|
||||
out.extend_from_slice(&bytes[at..]);
|
||||
return out;
|
||||
}
|
||||
let len = usize::from(u16::from_be_bytes([bytes[at + 2], bytes[at + 3]]));
|
||||
if !(marker == 0xEE && &bytes[at + 4..at + 9] == b"Adobe") {
|
||||
out.extend_from_slice(&bytes[at..at + 2 + len]);
|
||||
}
|
||||
at += 2 + len;
|
||||
}
|
||||
}
|
||||
|
||||
/// Adobe CMYK reads to the inks it was written with, as `decode` reads it; without the marker
|
||||
/// the samples are still taken as inverted (where `decode` keeps them as they are), and YCCK
|
||||
/// turns into the same inks closely, truncating where `decode` rounds.
|
||||
#[test]
|
||||
fn four_component_jpegs_read_as_inverted_inks() {
|
||||
let (w, h) = (32u16, 16u16);
|
||||
let inks = blocks(usize::from(w), usize::from(h));
|
||||
let cmyk = four(w, h, &inks, jpeg_encoder::ColorType::Cmyk, None);
|
||||
let read = decode_jpeg_inks(&cmyk, &Limits::default()).unwrap();
|
||||
assert_eq!((read.dimensions(), read.layout()), ((32, 16), ChannelLayout::Cmyk));
|
||||
assert!(read.data().iter().zip(&inks).all(|(a, b)| a.abs_diff(*b) <= 2), "Adobe CMYK keeps its inks");
|
||||
assert_eq!(read.data(), decode(&cmyk).unwrap().data(), "as decode reads Adobe CMYK");
|
||||
// No marker: inverted all the same, where decode keeps the stored samples.
|
||||
let bare = without_adobe(&cmyk);
|
||||
assert_ne!(bare, cmyk);
|
||||
let unmarked = decode_jpeg_inks(&bare, &Limits::default()).unwrap();
|
||||
assert_eq!(unmarked.data(), read.data());
|
||||
let kept = decode(&bare).unwrap();
|
||||
assert!(kept.data().iter().zip(read.data()).all(|(k, r)| *k == 255 - *r), "decode keeps them as stored");
|
||||
// YCCK: libjpeg's conversion, truncated.
|
||||
let ycck = four(w, h, &inks, jpeg_encoder::ColorType::CmykAsYcck, None);
|
||||
let read = decode_jpeg_inks(&ycck, &Limits::default()).unwrap();
|
||||
assert!(read.data().iter().zip(&inks).all(|(a, b)| a.abs_diff(*b) <= 3), "YCCK keeps its inks closely");
|
||||
let rounded = decode(&ycck).unwrap();
|
||||
let differ = read.data().iter().zip(rounded.data()).filter(|(a, b)| a != b).count();
|
||||
assert!(read.data().iter().zip(rounded.data()).all(|(a, b)| a.abs_diff(*b) <= 1), "within a step of decode's rounding");
|
||||
assert!(differ > 0, "truncation differs from rounding somewhere");
|
||||
}
|
||||
|
||||
/// The headers alone give the component count of any JPEG and its ICC profile, put together from
|
||||
/// its numbered segments.
|
||||
#[test]
|
||||
fn headers_give_components_and_icc() {
|
||||
let icc: Vec<u8> = (0..200_000u32).map(|i| (i * 7 % 251) as u8).collect();
|
||||
let cmyk = four(8, 8, &[9; 256], jpeg_encoder::ColorType::Cmyk, Some(&icc));
|
||||
assert_eq!((jpeg_components(&cmyk), jpeg_dimensions(&cmyk)), (Some(4), Some((8, 8))));
|
||||
assert_eq!(jpeg_icc_profile(&cmyk).as_deref(), Some(icc.as_slice()), "three APP2 segments");
|
||||
let gray = encode(&Image::from_u8(8, 8, ChannelLayout::Gray, vec![50; 64]).unwrap(), Format::Jpeg, &EncodeOptions::default()).unwrap();
|
||||
assert_eq!((jpeg_components(&gray), jpeg_icc_profile(&gray)), (Some(1), None));
|
||||
for not in [&b""[..], b"\xFF\xD8", b"\x89PNG\r\n\x1a\n"] {
|
||||
assert_eq!((jpeg_components(not), jpeg_icc_profile(not), jpeg_dimensions(not)), (None, None, None));
|
||||
}
|
||||
}
|
||||
|
||||
/// A frame header past 16384 pixels a side, `zune-jpeg`'s default, isn't read.
|
||||
#[test]
|
||||
fn sides_past_zunes_default_are_not_read() {
|
||||
let mut gray = encode(&Image::from_u8(8, 8, ChannelLayout::Gray, vec![50; 64]).unwrap(), Format::Jpeg, &EncodeOptions::default()).unwrap();
|
||||
let sof = gray.windows(2).position(|w| w == [0xFF, 0xC0]).unwrap();
|
||||
gray[sof + 5..sof + 9].copy_from_slice(&[0x40, 0x00, 0x40, 0x00]);
|
||||
assert_eq!(jpeg_dimensions(&gray), Some((16384, 16384)));
|
||||
gray[sof + 7..sof + 9].copy_from_slice(&[0x40, 0x01]);
|
||||
assert_eq!((jpeg_dimensions(&gray), jpeg_components(&gray)), (None, None));
|
||||
}
|
||||
|
||||
/// What isn't a four-component image `zune-jpeg` decodes is refused: other component counts, an
|
||||
/// arithmetic-coded or 12-bit frame (`decode` hands those to craft-jpeg), a size past the limits,
|
||||
/// and data that isn't a JPEG.
|
||||
#[test]
|
||||
fn other_jpegs_are_refused() {
|
||||
let gray = encode(&Image::from_u8(8, 8, ChannelLayout::Gray, vec![50; 64]).unwrap(), Format::Jpeg, &EncodeOptions::default()).unwrap();
|
||||
assert!(matches!(decode_jpeg_inks(&gray, &Limits::default()), Err(CodecError::Unsupported { .. })));
|
||||
let cmyk = four(16, 8, &[40; 16 * 8 * 4], jpeg_encoder::ColorType::Cmyk, None);
|
||||
let sof = cmyk.windows(2).position(|w| w == [0xFF, 0xC0]).unwrap();
|
||||
for (marker, precision) in [(0xC9u8, 8u8), (0xC1, 12)] {
|
||||
let mut other = cmyk.clone();
|
||||
other[sof + 1] = marker;
|
||||
other[sof + 4] = precision;
|
||||
assert!(decode_jpeg_inks(&other, &Limits::default()).is_err(), "SOF {marker:#x}, {precision} bits");
|
||||
}
|
||||
// A tiny file declaring a large frame is refused before zune-jpeg allocates its output.
|
||||
let mut huge = cmyk.clone();
|
||||
huge[sof + 5..sof + 9].copy_from_slice(&[0x20, 0x00, 0x20, 0x00]);
|
||||
assert!(matches!(decode_jpeg_inks(&huge, &Limits::default()), Err(CodecError::InvalidImage(m)) if m.contains("too short")));
|
||||
let tight = Limits { max_pixels: 100, ..Limits::default() };
|
||||
assert!(matches!(decode_jpeg_inks(&cmyk, &tight), Err(CodecError::LimitExceeded(_))));
|
||||
assert!(matches!(decode_jpeg_inks(b"not a jpeg", &Limits::default()), Err(CodecError::InvalidImage(_))));
|
||||
assert!(decode_jpeg_inks(&cmyk[..12], &Limits::default()).is_err(), "headers cut short");
|
||||
}
|
||||
|
||||
/// The ink reader four-component JPEGs were read with before (vectorcraft's `jpeg_inks`, on
|
||||
/// `zune-jpeg` as this crate uses it), kept as the reference the readers here match.
|
||||
fn reference_inks(bytes: &[u8]) -> Option<(usize, usize, Vec<u8>)> {
|
||||
use zune_core::{bytestream::ZCursor, colorspace::ColorSpace, options::DecoderOptions};
|
||||
let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default());
|
||||
d.decode_headers().ok()?;
|
||||
let space = d.input_colorspace()?;
|
||||
if d.info()?.components != 4 || !matches!(space, ColorSpace::CMYK | ColorSpace::YCCK) {
|
||||
return None;
|
||||
}
|
||||
let (w, h) = d.dimensions()?;
|
||||
d.set_options(DecoderOptions::default().jpeg_set_out_colorspace(space));
|
||||
let mut data = d.decode().ok()?;
|
||||
for p in data.as_chunks_mut::<4>().0 {
|
||||
if space == ColorSpace::YCCK {
|
||||
let [y, cb, cr] = [p[0], p[1], p[2]].map(f32::from);
|
||||
p[0] = (434.456 - y - 1.402 * cr).clamp(0.0, 255.0) as u8;
|
||||
p[1] = (119.541 - y + 0.344 * cb + 0.714 * cr).clamp(0.0, 255.0) as u8;
|
||||
p[2] = (481.816 - y - 1.772 * cb).clamp(0.0, 255.0) as u8;
|
||||
}
|
||||
for v in p.iter_mut() {
|
||||
*v = 255 - *v;
|
||||
}
|
||||
}
|
||||
Some((w, h, data))
|
||||
}
|
||||
|
||||
/// Over CMYK and YCCK JPEGs of many sizes, samplings, qualities and pictures, with and without
|
||||
/// Adobe's marker, baseline and progressive, the inks and component counts read the same as the
|
||||
/// reference reader.
|
||||
#[test]
|
||||
fn matches_the_reference_reader() {
|
||||
use jpeg_encoder::{ColorType, Encoder, SamplingFactor};
|
||||
let (mut seen, mut refused) = (0, 0);
|
||||
for (i, (w, h)) in [(1u16, 1u16), (7, 3), (8, 8), (17, 9), (33, 31), (64, 5), (100, 77)].into_iter().enumerate() {
|
||||
let (wu, n) = (usize::from(w), usize::from(w) * usize::from(h));
|
||||
let noise: Vec<u8> = (0..n * 4).map(|k| ((k as u64).wrapping_mul(2_654_435_761).wrapping_add(i as u64 * 97) >> 7) as u8).collect();
|
||||
let ramp: Vec<u8> = (0..n * 4).map(|k| (k / 4 % wu * 255 / wu.max(2)) as u8).collect();
|
||||
for samples in [&noise, &ramp] {
|
||||
for ty in [ColorType::Cmyk, ColorType::CmykAsYcck] {
|
||||
for (q, sampling, progressive) in
|
||||
[(100, SamplingFactor::R_4_4_4, false), (75, SamplingFactor::R_4_2_0, false), (40, SamplingFactor::R_4_2_2, true)]
|
||||
{
|
||||
let mut out = Vec::new();
|
||||
let mut enc = Encoder::new(&mut out, q);
|
||||
enc.set_sampling_factor(sampling);
|
||||
enc.set_progressive(progressive);
|
||||
enc.encode(samples, w, h, ty).unwrap();
|
||||
for file in [without_adobe(&out), out] {
|
||||
let read = decode_jpeg_inks(&file, &Limits::default());
|
||||
assert_eq!(jpeg_components(&file), Some(4));
|
||||
let (dw, dh) = jpeg_dimensions(&file).unwrap();
|
||||
assert_eq!((usize::from(w), usize::from(h)), (dw as usize, dh as usize));
|
||||
let Some((rw, rh, inks)) = reference_inks(&file) else {
|
||||
eprintln!("refused: {w}x{h} {ty:?} q{q} progressive {progressive}");
|
||||
assert!(read.is_err(), "refused by both: {w}x{h} {ty:?} q{q}");
|
||||
refused += 1;
|
||||
continue;
|
||||
};
|
||||
let read = read.unwrap();
|
||||
assert_eq!(read.dimensions(), (rw as u32, rh as u32));
|
||||
assert_eq!(read.data(), inks.as_slice(), "{w}x{h} {ty:?} q{q}");
|
||||
seen += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
eprintln!("read {seen}, refused {refused}");
|
||||
assert_eq!(seen + refused, 7 * 2 * 2 * 3 * 2);
|
||||
assert!(seen > refused);
|
||||
}
|
||||
|
||||
/// zune-jpeg 0.5 panics on some subsampled CMYK frames whose first component isn't the largest
|
||||
/// (here `jpeg-encoder`'s 2×2 K with C, M and Y at 1×1, progressive): an error, not a panic.
|
||||
#[test]
|
||||
fn frames_zune_panics_on_are_errors() {
|
||||
for (w, h) in [(1u16, 1u16), (3, 3)] {
|
||||
let mut out = Vec::new();
|
||||
let mut enc = jpeg_encoder::Encoder::new(&mut out, 70);
|
||||
enc.set_sampling_factor(jpeg_encoder::SamplingFactor::F_2_2);
|
||||
enc.set_progressive(true);
|
||||
enc.encode(&vec![9; usize::from(w * h) * 4], w, h, jpeg_encoder::ColorType::Cmyk).unwrap();
|
||||
assert!(std::panic::catch_unwind(|| reference_inks(&out)).is_err(), "the reference reader panics on {w}x{h}");
|
||||
assert!(matches!(decode_jpeg_inks(&out, &Limits::default()), Err(CodecError::Unsupported { .. })), "{w}x{h}");
|
||||
assert_eq!((jpeg_components(&out), jpeg_dimensions(&out)), (Some(4), Some((u32::from(w), u32::from(h)))));
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ mod exr_stream;
|
||||
mod fidelity;
|
||||
mod gif_frames;
|
||||
mod heif;
|
||||
mod jpeg_inks;
|
||||
mod jpegls_oracle;
|
||||
mod limits;
|
||||
mod malformed;
|
||||
|
||||
@@ -15,7 +15,10 @@ mod smf;
|
||||
pub mod sync;
|
||||
|
||||
pub use sequence::{CtrlEvent, CtrlKind, Note, Sequence};
|
||||
pub use smf::{LenientRead, MAX_EVENTS, MAX_TRACKS, MAX_WARNINGS, Smf, SmfTrack, read_smf, read_smf_lenient, write_smf};
|
||||
pub use smf::{
|
||||
LenientRead, MAX_EVENTS, MAX_TRACKS, MAX_WARNINGS, Smf, SmfTrack, TimedEvent, encode_meta_event, read_smf, read_smf_lenient, write_smf,
|
||||
write_timed_smf,
|
||||
};
|
||||
|
||||
/// Ticks per quarter note used throughout the model.
|
||||
pub const PPQ: u32 = 960;
|
||||
|
||||
+98
-7
@@ -598,10 +598,68 @@ fn push_vlq(out: &mut Vec<u8>, v: u64) -> Result<(), MidiError> {
|
||||
}
|
||||
|
||||
/// An event ready to be serialised: `(tick, order, bytes)`. `order` breaks ties at one tick.
|
||||
struct WEvent {
|
||||
tick: u64,
|
||||
order: u8,
|
||||
bytes: Vec<u8>,
|
||||
/// An already encoded SMF event at an absolute source tick.
|
||||
///
|
||||
/// `bytes` must contain one complete channel, meta, or SysEx event, including its
|
||||
/// status and any length, but no delta time. The caller owns payload validity.
|
||||
/// Do not provide running-status fragments or End-of-Track (added by the writer).
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct TimedEvent {
|
||||
pub tick: u64,
|
||||
pub order: u8,
|
||||
pub bytes: Vec<u8>,
|
||||
}
|
||||
|
||||
type WEvent = TimedEvent;
|
||||
|
||||
/// Encode an SMF meta event, without its delta time.
|
||||
pub fn encode_meta_event(kind: u8, data: &[u8]) -> Result<Vec<u8>, MidiError> {
|
||||
meta(kind, data)
|
||||
}
|
||||
|
||||
/// Serialize source-timed events without normalizing ticks or trimming notes.
|
||||
///
|
||||
/// Tracks and same-tick events retain their input order (with `order` as the tie
|
||||
/// breaker). Format must be 0 (exactly one track) or 1. Division is PPQ 1..=32767.
|
||||
/// Channel running-status compression is disabled. `clamp_deltas` is an explicit
|
||||
/// compatibility option: gaps above the SMF 28-bit maximum are saturated rather
|
||||
/// than rejected. It can shorten represented time and should normally be false.
|
||||
///
|
||||
/// `max_bytes` bounds the conservative serialized size (four bytes per delta),
|
||||
/// not caller-owned events or sorting memory. Encoded payloads are trusted; this
|
||||
/// is a framing primitive, not validation of arbitrary untrusted event bytes.
|
||||
pub fn write_timed_smf(format: u16, division: u16, tracks: Vec<Vec<TimedEvent>>, clamp_deltas: bool, max_bytes: usize) -> Result<Vec<u8>, MidiError> {
|
||||
if format > 1 || (format == 0 && tracks.len() != 1) || division == 0 || division > 0x7fff {
|
||||
return Err(MidiError::Write("invalid format, track count, or PPQ division".into()));
|
||||
}
|
||||
let count = u16::try_from(tracks.len()).map_err(|_| MidiError::Write("more than 65535 tracks".into()))?;
|
||||
let mut bound = 14usize;
|
||||
for track in &tracks {
|
||||
bound = bound.checked_add(12).ok_or_else(|| MidiError::Write("output size overflow".into()))?;
|
||||
let mut body = 4usize;
|
||||
for event in track {
|
||||
let size = event.bytes.len().checked_add(4).ok_or_else(|| MidiError::Write("event size overflow".into()))?;
|
||||
body = body.checked_add(size).ok_or_else(|| MidiError::Write("track size overflow".into()))?;
|
||||
bound = bound.checked_add(size).ok_or_else(|| MidiError::Write("output size overflow".into()))?;
|
||||
}
|
||||
if body > u32::MAX as usize {
|
||||
return Err(MidiError::Write("track larger than 4 GiB".into()));
|
||||
}
|
||||
}
|
||||
if bound > max_bytes {
|
||||
return Err(MidiError::Write("serialized output exceeds byte budget".into()));
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
out.try_reserve(bound).map_err(|_| MidiError::Write("output allocation failed".into()))?;
|
||||
out.extend_from_slice(b"MThd");
|
||||
out.extend_from_slice(&6u32.to_be_bytes());
|
||||
out.extend_from_slice(&format.to_be_bytes());
|
||||
out.extend_from_slice(&count.to_be_bytes());
|
||||
out.extend_from_slice(&division.to_be_bytes());
|
||||
for track in tracks {
|
||||
write_track_policy(&mut out, track, clamp_deltas, false)?;
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn clamp_tick(t: i64) -> u64 {
|
||||
@@ -615,17 +673,22 @@ fn meta(kind: u8, data: &[u8]) -> Result<Vec<u8>, MidiError> {
|
||||
Ok(b)
|
||||
}
|
||||
|
||||
fn write_track(out: &mut Vec<u8>, mut events: Vec<WEvent>) -> Result<(), MidiError> {
|
||||
fn write_track(out: &mut Vec<u8>, events: Vec<WEvent>) -> Result<(), MidiError> {
|
||||
write_track_policy(out, events, false, true)
|
||||
}
|
||||
|
||||
fn write_track_policy(out: &mut Vec<u8>, mut events: Vec<WEvent>, clamp_deltas: bool, running_status: bool) -> Result<(), MidiError> {
|
||||
events.sort_by_key(|e| (e.tick, e.order));
|
||||
let mut body = Vec::new();
|
||||
let mut last_tick = 0u64;
|
||||
let mut running: Option<u8> = None;
|
||||
for e in &events {
|
||||
push_vlq(&mut body, e.tick - last_tick)?;
|
||||
let delta = e.tick - last_tick;
|
||||
push_vlq(&mut body, if clamp_deltas { delta.min(MAX_VLQ) } else { delta })?;
|
||||
last_tick = e.tick;
|
||||
let status = e.bytes.first().copied().unwrap_or(0);
|
||||
if (0x80..=0xEF).contains(&status) {
|
||||
if running == Some(status) {
|
||||
if running_status && running == Some(status) {
|
||||
body.extend_from_slice(e.bytes.get(1..).unwrap_or(&[]));
|
||||
} else {
|
||||
body.extend_from_slice(&e.bytes);
|
||||
@@ -847,4 +910,32 @@ mod tests {
|
||||
// of two notes starting together only the longer survives.
|
||||
assert_eq!(got, vec![(0, 100), (100, 200), (1000, 100), (2000, 900)]);
|
||||
}
|
||||
#[test]
|
||||
fn timed_writer_preserves_source_ticks_payloads_and_stable_order() {
|
||||
let events = vec![
|
||||
TimedEvent { tick: 96, order: 3, bytes: vec![0x90, 60, 100] },
|
||||
TimedEvent { tick: 96, order: 1, bytes: vec![0x80, 60, 0] },
|
||||
TimedEvent { tick: 96, order: 3, bytes: vec![0x90, 60, 110] },
|
||||
];
|
||||
let out = write_timed_smf(0, 96, vec![events], false, 53).unwrap();
|
||||
assert_eq!(&out[..14], b"MThd\0\0\0\x06\0\0\0\x01\0\x60");
|
||||
assert_eq!(&out[22..], &[96, 0x80, 60, 0, 0, 0x90, 60, 100, 0, 0x90, 60, 110, 0, 0xff, 0x2f, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timed_writer_checks_budget_format_division_and_delta_policy() {
|
||||
let event = TimedEvent { tick: MAX_VLQ + 1, order: 0, bytes: vec![0xc0, 1] };
|
||||
assert!(write_timed_smf(0, 480, vec![vec![event.clone()]], false, 32).is_err());
|
||||
let out = write_timed_smf(0, 480, vec![vec![event.clone()]], true, 32).unwrap();
|
||||
assert_eq!(&out[22..26], &[0xff, 0xff, 0xff, 0x7f]);
|
||||
assert!(write_timed_smf(0, 480, vec![vec![event]], true, 31).is_err());
|
||||
assert!(write_timed_smf(0, 96, vec![], false, 32).is_err());
|
||||
assert!(write_timed_smf(2, 96, vec![], false, 32).is_err());
|
||||
for division in [0, 0x8000, u16::MAX] {
|
||||
assert!(write_timed_smf(1, division, vec![], false, 32).is_err());
|
||||
}
|
||||
assert_eq!(write_timed_smf(1, 96, vec![], false, 14).unwrap().len(), 14);
|
||||
assert!(write_timed_smf(1, 96, vec![], false, 13).is_err());
|
||||
assert_eq!(encode_meta_event(4, b"Piano").unwrap(), b"\xff\x04\x05Piano");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user