mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-19 16:45:28 +00:00
Footage cache (#3271): faster OpenEXR reads and disk plane hits
- The reader reads the next batch of chunks while the one before decompresses, reads chunks that follow each other in the file in one request, starts with a small batch so decoding begins early, places the planes in parallel, and converts half samples a slice at a time. - A plane entry stores a half channel as half floats when every value round-trips bit for bit (NaN planes and float data stay f32), with a plain byte-plane shuffle; one entry per channel, read in parallel on a hit. Old multi-plane entries (format 5) read as misses. - The planes of one decode go to the disk writer as one job that shares the plane store's set instead of a copy per channel; the queue counts their bytes once the store lets go of them. - The plane store loses its unused level, and one eviction loop serves the budget and the memory accountant; dropping for bytes keeps remembered failures.
This commit is contained in:
1 parent
6f498a09eb
commit
dfe9aedfe4
7 files changed
+473
-228
No files matched your search
Generated
+2
@@ -1762,6 +1762,7 @@ dependencies = [
|
||||
"filmcraft-mpegts",
|
||||
"filmcraft-time",
|
||||
"gif",
|
||||
"half",
|
||||
"image",
|
||||
"log",
|
||||
"rayon",
|
||||
@@ -1874,6 +1875,7 @@ dependencies = [
|
||||
"effectcraft-raster",
|
||||
"effectcraft-text",
|
||||
"effectcraft-time",
|
||||
"half",
|
||||
"kurbo",
|
||||
"log",
|
||||
"lz4_flex",
|
||||
|
||||
@@ -30,6 +30,7 @@ filmcraft-frame = { workspace = true }
|
||||
filmcraft-color = { workspace = true }
|
||||
filmcraft-time = { workspace = true }
|
||||
exr = { workspace = true }
|
||||
half = { workspace = true }
|
||||
gif = { workspace = true }
|
||||
image = { workspace = true, features = ["gif", "webp", "tiff", "bmp", "exr", "tga", "hdr"] }
|
||||
rayon = { workspace = true }
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! The plane store (#3271): decoded OpenEXR channel planes in RAM, one entry per (file, level),
|
||||
//! The plane store (#3271): decoded OpenEXR channel planes in RAM, one entry per file,
|
||||
//! shared by every consumer of a frame: the picture of a footage layer and the channels its
|
||||
//! effects read (EXtractoR, Cryptomatte, the 3D Channel effects). It replaces the pool's own
|
||||
//! OpenEXR decode and the separate channel cache of #474.
|
||||
@@ -22,6 +22,8 @@
|
||||
use std::collections::{BTreeSet, VecDeque};
|
||||
use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};
|
||||
|
||||
use rayon::prelude::*;
|
||||
|
||||
use effectcraft_raster::channels3d::AuxNeed;
|
||||
use effectcraft_raster::{AuxChannels, Image};
|
||||
use effectcraft_render::reads::ReadFile;
|
||||
@@ -255,7 +257,6 @@ impl Flight {
|
||||
|
||||
struct Entry {
|
||||
path: String,
|
||||
level: u32,
|
||||
batch: Option<Arc<Batch>>,
|
||||
flight: Option<Arc<Flight>>,
|
||||
/// Requests that failed for good (unreadable, too large) and why: a request asking for at
|
||||
@@ -286,7 +287,6 @@ struct State {
|
||||
struct Owning<'a> {
|
||||
store: &'a PlaneStore,
|
||||
path: &'a str,
|
||||
level: u32,
|
||||
flight: &'a Arc<Flight>,
|
||||
ended: bool,
|
||||
}
|
||||
@@ -294,7 +294,7 @@ struct Owning<'a> {
|
||||
impl Owning<'_> {
|
||||
fn end(&mut self, outcome: Result<Arc<Batch>, Refused>) -> Result<Arc<Batch>, Refused> {
|
||||
self.ended = true;
|
||||
let outcome = self.store.finish(self.path, self.level, self.flight, outcome);
|
||||
let outcome = self.store.finish(self.path, self.flight, outcome);
|
||||
self.flight.end(outcome.clone());
|
||||
effectcraft_render::memory::Accountant::global().note_growth();
|
||||
outcome
|
||||
@@ -309,7 +309,7 @@ impl Drop for Owning<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Decoded planes by (file, level), bounded by bytes.
|
||||
/// Decoded planes by file, bounded by bytes.
|
||||
pub(crate) struct PlaneStore {
|
||||
state: Mutex<State>,
|
||||
budget: usize,
|
||||
@@ -470,14 +470,13 @@ impl PlaneStore {
|
||||
/// One round of [`PlaneStore::get`].
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn step(&self, path: &str, source: &ExrSource, index: &Arc<ExrIndex>, want: &Want, by: Use, opened: &mut Option<Opened>, since: u64) -> Step {
|
||||
let level = 0;
|
||||
let mut st = self.lock();
|
||||
st.clock += 1;
|
||||
let clock = st.clock;
|
||||
let at = match st.entries.iter().position(|e| e.path == path && e.level == level) {
|
||||
let at = match st.entries.iter().position(|e| e.path == path) {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
st.entries.push(Entry { path: path.to_string(), level, batch: None, flight: None, failed: vec![], bytes: 0, used: clock });
|
||||
st.entries.push(Entry { path: path.to_string(), batch: None, flight: None, failed: vec![], bytes: 0, used: clock });
|
||||
st.entries.len() - 1
|
||||
}
|
||||
};
|
||||
@@ -527,7 +526,7 @@ impl PlaneStore {
|
||||
drop(st);
|
||||
#[cfg(test)]
|
||||
crate::exr_channels::count_decode(true);
|
||||
let mut owning = Owning { store: self, path, level, flight: &flight, ended: false };
|
||||
let mut owning = Owning { store: self, path, flight: &flight, ended: false };
|
||||
let outcome = run(path, source, &flight, base, opened.take());
|
||||
match owning.end(outcome) {
|
||||
Ok(b) if b.covers(index, want) => {
|
||||
@@ -570,11 +569,11 @@ impl PlaneStore {
|
||||
|
||||
/// Store the outcome of `flight`'s decode in its entry, unless the file was forgotten
|
||||
/// meanwhile (then nothing made of it is kept or handed out); drop what doesn't fit.
|
||||
fn finish(&self, path: &str, level: u32, flight: &Arc<Flight>, outcome: Result<Arc<Batch>, Refused>) -> Result<Arc<Batch>, Refused> {
|
||||
fn finish(&self, path: &str, flight: &Arc<Flight>, outcome: Result<Arc<Batch>, Refused>) -> Result<Arc<Batch>, Refused> {
|
||||
let mut st = self.lock();
|
||||
st.clock += 1;
|
||||
let clock = st.clock;
|
||||
let Some(e) = st.entries.iter_mut().find(|e| e.path == path && e.level == level && e.flight.as_ref().is_some_and(|f| Arc::ptr_eq(f, flight))) else {
|
||||
let Some(e) = st.entries.iter_mut().find(|e| e.path == path && e.flight.as_ref().is_some_and(|f| Arc::ptr_eq(f, flight))) else {
|
||||
return match outcome {
|
||||
Err(e @ (Refused::Cancelled | Refused::Stopped)) => Err(e),
|
||||
_ => Err(Refused::Forgotten),
|
||||
@@ -601,23 +600,26 @@ impl PlaneStore {
|
||||
}
|
||||
Err(Refused::Cancelled | Refused::Forgotten | Refused::Stopped) => {}
|
||||
}
|
||||
Self::evict(&mut st, self.budget, path, level);
|
||||
Self::drop_oldest(&mut st, self.budget, CACHE_FILES, Some(path));
|
||||
outcome
|
||||
}
|
||||
|
||||
/// Drop least recently used entries not in use until the rest fit the budget, keeping the
|
||||
/// newest (`path` at `level`, just decoded) and the pictures' planes held for their effects.
|
||||
fn evict(st: &mut State, budget: usize, path: &str, level: u32) {
|
||||
/// Drop least recently used entries until the rest take at most `bytes` in at most `files`
|
||||
/// entries, never one in use, the pictures' planes held for their effects, or `newest` (just
|
||||
/// decoded); entries that hold no bytes (a failure remembered) only when there are too many.
|
||||
fn drop_oldest(st: &mut State, bytes: usize, files: usize, newest: Option<&str>) {
|
||||
loop {
|
||||
if st.entries.iter().map(|e| e.bytes).fold(0usize, usize::saturating_add) <= budget && st.entries.len() <= CACHE_FILES {
|
||||
if st.entries.iter().map(|e| e.bytes).fold(0usize, usize::saturating_add) <= bytes && st.entries.len() <= files {
|
||||
break;
|
||||
}
|
||||
// (an entry holding no bytes only goes for the count)
|
||||
let over_count = st.entries.len() > files;
|
||||
let held = &st.held;
|
||||
let oldest = st
|
||||
.entries
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, e)| !(e.path == path && e.level == level) && !e.in_use())
|
||||
.filter(|(_, e)| Some(e.path.as_str()) != newest && !e.in_use() && (e.bytes > 0 || over_count))
|
||||
.filter(|(_, e)| !e.batch.as_ref().is_some_and(|b| held.iter().any(|(_, h)| Arc::ptr_eq(h, b))))
|
||||
.min_by_key(|(_, e)| e.used)
|
||||
.map(|(i, _)| i);
|
||||
@@ -671,23 +673,7 @@ impl PlaneStore {
|
||||
/// Drop least recently used entries not in use until at most `bytes` are held (the memory
|
||||
/// accountant, #3271).
|
||||
fn trim_to(&self, bytes: usize) {
|
||||
let mut st = self.lock();
|
||||
loop {
|
||||
if st.entries.iter().map(|e| e.bytes).fold(0usize, usize::saturating_add) <= bytes {
|
||||
break;
|
||||
}
|
||||
let held = &st.held;
|
||||
let oldest = st
|
||||
.entries
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, e)| e.bytes > 0 && !e.in_use())
|
||||
.filter(|(_, e)| !e.batch.as_ref().is_some_and(|b| held.iter().any(|(_, h)| Arc::ptr_eq(h, b))))
|
||||
.min_by_key(|(_, e)| e.used)
|
||||
.map(|(i, _)| i);
|
||||
let Some(i) = oldest else { break };
|
||||
st.entries.remove(i);
|
||||
}
|
||||
Self::drop_oldest(&mut self.lock(), bytes, usize::MAX, None);
|
||||
}
|
||||
|
||||
/// Stop keeping the last pictures' planes for their effects.
|
||||
@@ -838,15 +824,18 @@ fn decode(path: &str, source: &ExrSource, flight: &Arc<Flight>, base: Option<Arc
|
||||
let unique = index.channels.iter().map(|c| c.name.as_str()).collect::<std::collections::HashSet<_>>().len() == index.channels.len();
|
||||
let disk = disk_cache_for(path).filter(|_| !missing.is_empty() && unique);
|
||||
let since = flight.since;
|
||||
let key = |name: &str| effectcraft_render::disk_cache::plane_key(path, &opened.stamp, &[name], 0, None);
|
||||
let mut kept: Vec<Option<Vec<f32>>> = names
|
||||
.iter()
|
||||
.map(|n| {
|
||||
let (planes, _) = disk.as_ref()?.get_planes(key(n), None, (w as u32, h as u32, 1))?;
|
||||
let (name, plane) = planes.planes.into_iter().next()?;
|
||||
(name == *n && plane.len() == w.saturating_mul(h)).then_some(plane)
|
||||
})
|
||||
.collect();
|
||||
let key = |name: &str| effectcraft_render::disk_cache::plane_key(path, &opened.stamp, name);
|
||||
// (read and decompressed side by side on this pool's threads)
|
||||
let mut kept: Vec<Option<Vec<f32>>> = match &disk {
|
||||
Some(d) => names
|
||||
.par_iter()
|
||||
.map(|n| {
|
||||
let (name, plane) = d.get_plane(key(n), w as u32, h as u32)?;
|
||||
(name == *n && plane.len() == w.saturating_mul(h)).then_some(plane)
|
||||
})
|
||||
.collect(),
|
||||
None => vec![None; names.len()],
|
||||
};
|
||||
let to_read: Vec<usize> = missing.iter().zip(&kept).filter(|(_, k)| k.is_none()).map(|(id, _)| *id).collect();
|
||||
let from_disk = to_read.is_empty();
|
||||
let (read, stats) = if to_read.is_empty() {
|
||||
@@ -875,20 +864,6 @@ fn decode(path: &str, source: &ExrSource, flight: &Arc<Flight>, base: Option<Arc
|
||||
Refused::Unreadable
|
||||
}
|
||||
})?;
|
||||
// Kept on disk for the next time, each channel read from the file in an entry of its own
|
||||
// (encoded on the disk cache's writer thread; a file from memory, which can't be checked
|
||||
// later, isn't).
|
||||
if let Some(d) = &disk {
|
||||
let mut files = effectcraft_render::reads::ReadFiles::default();
|
||||
files.add(ReadFile::new(path, opened.stamp));
|
||||
if let Some(entry) = effectcraft_render::disk_cache::EntryFiles::of(&files, None) {
|
||||
for (id, plane) in to_read.iter().zip(&read) {
|
||||
if let Some(c) = index.channels.get(*id) {
|
||||
d.put_planes(key(&c.name), w as u32, h as u32, vec![(c.name.clone(), plane.clone())], &entry, since);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut read = read.into_iter();
|
||||
let planes: Vec<Vec<f32>> = kept.iter_mut().map(|k| k.take().or_else(|| read.next()).unwrap_or_default()).collect();
|
||||
// The union, in file order: the planes held and the ones just read.
|
||||
@@ -913,6 +888,18 @@ fn decode(path: &str, source: &ExrSource, flight: &Arc<Flight>, base: Option<Arc
|
||||
aux.channels = new.into_iter().filter_map(|(id, p)| Some((index.channels.get(id)?.name.clone(), p))).collect();
|
||||
let file = ReadFile::new(path, opened.stamp);
|
||||
let batch = Arc::new(Batch { index, ids, all, aux: Arc::new(aux), file });
|
||||
// Kept on disk for the next time, each channel read from the file in an entry of its own,
|
||||
// in one job the disk cache's writer thread encodes from the planes the batch holds (a file
|
||||
// from memory, which can't be checked later, isn't).
|
||||
if let Some(d) = &disk {
|
||||
let mut files = effectcraft_render::reads::ReadFiles::default();
|
||||
files.add(batch.file.clone());
|
||||
if let Some(entry) = effectcraft_render::disk_cache::EntryFiles::of(&files, None) {
|
||||
let planes: Vec<(u128, usize)> =
|
||||
to_read.iter().filter_map(|id| Some((key(&batch.index.channels.get(*id)?.name), batch.ids.binary_search(id).ok()?))).collect();
|
||||
d.put_planes(&batch.aux, &planes, &entry, since);
|
||||
}
|
||||
}
|
||||
Ok(batch)
|
||||
}
|
||||
|
||||
@@ -988,11 +975,6 @@ pub(crate) fn is_exr(path: &str) -> bool {
|
||||
path.to_ascii_lowercase().ends_with(".exr")
|
||||
}
|
||||
|
||||
/// The index of `path`, and the store's verdict on an unreadable one.
|
||||
fn index_of(path: &str, source: &ExrSource) -> Result<(Arc<ExrIndex>, Option<Opened>), IndexError> {
|
||||
exr_index::get_opened(path, source)
|
||||
}
|
||||
|
||||
/// The ids `need` reads of the file `index` describes (`None`: all).
|
||||
fn need_ids(index: &ExrIndex, need: &AuxNeed) -> Option<BTreeSet<usize>> {
|
||||
let names = need.resolve(&index.names)?;
|
||||
@@ -1017,7 +999,7 @@ pub(crate) fn picture(path: &str, source: &ExrSource, op: AlphaOp, extra: Option
|
||||
for _ in 0..3 {
|
||||
// (before the file is opened: see `Flight::since`)
|
||||
let since = drop_generation();
|
||||
let (index, opened) = match index_of(path, source) {
|
||||
let (index, opened) = match exr_index::get_opened(path, source) {
|
||||
Ok(ix) => ix,
|
||||
Err(IndexError::Unsupported(why)) => return Picture::Done(Err(why.to_string())),
|
||||
Err(_) => return Picture::Legacy,
|
||||
@@ -1085,7 +1067,7 @@ fn picture_of(index: &ExrIndex, batch: &Batch, op: AlphaOp) -> Option<Image> {
|
||||
/// converted, the others stay empty (#3271: a region of interest). Read for this request alone,
|
||||
/// not kept in the store or on disk (the pool keeps the picture under a key naming the rows).
|
||||
pub(crate) fn picture_rows(path: &str, source: &ExrSource, op: AlphaOp, rows: (i64, i64)) -> Picture {
|
||||
let (index, opened) = match index_of(path, source) {
|
||||
let (index, opened) = match exr_index::get_opened(path, source) {
|
||||
Ok(ix) => ix,
|
||||
Err(IndexError::Unsupported(why)) => return Picture::Done(Err(why.to_string())),
|
||||
Err(_) => return Picture::Legacy,
|
||||
@@ -1133,7 +1115,7 @@ pub(crate) fn picture_rows(path: &str, source: &ExrSource, op: AlphaOp, rows: (i
|
||||
pub(crate) fn aux(path: &str, source: &ExrSource, need: &AuxNeed) -> Option<Arc<AuxChannels>> {
|
||||
for _ in 0..2 {
|
||||
let since = drop_generation();
|
||||
let (index, opened) = index_of(path, source).ok()?;
|
||||
let (index, opened) = exr_index::get_opened(path, source).ok()?;
|
||||
let want = match need_ids(&index, need) {
|
||||
Some(ids) => Want { all: false, ids },
|
||||
None => Want { all: true, ids: BTreeSet::new() },
|
||||
|
||||
@@ -1038,3 +1038,107 @@ fn a_region_of_interest_reads_only_its_rows() {
|
||||
assert!(decodes_of(path).get(before..).unwrap_or_default().iter().all(|d| d.stats.lines == SIZE.1), "collapsed: the whole frame");
|
||||
}
|
||||
}
|
||||
|
||||
/// A source that calls the request off after `cancel_after` reads, or fails every read after
|
||||
/// `fail_after` (tests of a read in several batches).
|
||||
struct Flaky {
|
||||
inner: std::io::Cursor<Vec<u8>>,
|
||||
reads: usize,
|
||||
cancel_after: Option<(usize, Arc<std::sync::atomic::AtomicBool>)>,
|
||||
fail_after: Option<usize>,
|
||||
}
|
||||
|
||||
impl std::io::Read for Flaky {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
self.reads += 1;
|
||||
if let Some((n, flag)) = &self.cancel_after
|
||||
&& self.reads > *n
|
||||
{
|
||||
flag.store(true, Ordering::SeqCst);
|
||||
}
|
||||
if self.fail_after.is_some_and(|n| self.reads > n) {
|
||||
return Err(std::io::Error::other("the share went away"));
|
||||
}
|
||||
self.inner.read(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::io::Seek for Flaky {
|
||||
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
|
||||
self.inner.seek(pos)
|
||||
}
|
||||
}
|
||||
|
||||
/// #3271: a file of more chunks than one batch is read in several (the next batch is read while
|
||||
/// the one before decompresses, adjacent chunks in one read): the planes are the file's values,
|
||||
/// also for a row range across batch boundaries; called off between batches it stops; a read
|
||||
/// failing in a later batch is an I/O error.
|
||||
#[test]
|
||||
fn a_read_in_several_batches_is_whole_and_stops_between_them() {
|
||||
let (w, h) = (8usize, 700usize);
|
||||
let channels = [("A", true), ("B", false), ("G", true), ("R", true)];
|
||||
let bytes = write(vec![part(None, &channels, (w, h), Compression::ZIP1)], (w, h));
|
||||
let (ix, mut src) = indexed(&bytes);
|
||||
let expect = |c: usize, i: usize| {
|
||||
let (name, half) = channels[c];
|
||||
let v = value(i, c, name == "A");
|
||||
if half { f16::from_f32(v).to_f32() } else { v }
|
||||
};
|
||||
let all = ReadRequest::full(vec![0, 1, 2, 3]);
|
||||
let (planes, stats) = crate::exr_read::read_planes(&mut src, &ix, &all);
|
||||
let planes = planes.unwrap();
|
||||
assert_eq!(stats.lines, h, "every line, in three batches");
|
||||
for (c, p) in planes.iter().enumerate() {
|
||||
assert!(p.iter().enumerate().all(|(i, v)| v.to_bits() == expect(c, i).to_bits()), "channel {c}");
|
||||
}
|
||||
let band = ReadRequest { rows: Some(250..520), ..all.clone() };
|
||||
let (got, stats) = crate::exr_read::read_planes(&mut src, &ix, &band);
|
||||
let got = got.unwrap();
|
||||
assert_eq!(stats.lines, 270);
|
||||
for (c, p) in got.iter().enumerate() {
|
||||
for (i, v) in p.iter().enumerate() {
|
||||
let want = if (250..520).contains(&(i / w)) { expect(c, i) } else { 0.0 };
|
||||
assert_eq!(v.to_bits(), want.to_bits(), "channel {c}, sample {i}");
|
||||
}
|
||||
}
|
||||
// Called off while the first batch is read: it stops before the next.
|
||||
let flag = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
let mut flaky = Flaky { inner: std::io::Cursor::new(bytes.clone()), reads: 0, cancel_after: Some((1, flag.clone())), fail_after: None };
|
||||
let (got, stats) = filmcraft_media::cancel::with_cancel(&flag, || crate::exr_read::read_planes(&mut flaky, &ix, &all));
|
||||
assert_eq!(got.err(), Some(ReadError::Cancelled));
|
||||
assert!(stats.lines < h, "stopped early: {} lines", stats.lines);
|
||||
// Chunks that follow each other in the file are read in one go, not one read per chunk.
|
||||
let mut counted = Flaky { inner: std::io::Cursor::new(bytes.clone()), reads: 0, cancel_after: None, fail_after: None };
|
||||
assert!(crate::exr_read::read_planes(&mut counted, &ix, &all).0.is_ok());
|
||||
assert!(counted.reads <= 12, "{} reads for {h} chunks", counted.reads);
|
||||
// A read failing in the second batch.
|
||||
let mut flaky = Flaky { inner: std::io::Cursor::new(bytes), reads: 0, cancel_after: None, fail_after: Some(1) };
|
||||
let (got, _) = crate::exr_read::read_planes(&mut flaky, &ix, &all);
|
||||
assert!(matches!(got, Err(ReadError::Io(_))), "{got:?}");
|
||||
}
|
||||
|
||||
/// #3271: dropping entries for bytes (a decode over the budget, the memory accountant) drops
|
||||
/// only entries that hold bytes: an unreadable file stays remembered (and isn't read again)
|
||||
/// until there are too many entries.
|
||||
#[test]
|
||||
fn trimming_for_bytes_keeps_remembered_failures() {
|
||||
let bytes: Arc<[u8]> = write(vec![part(None, &RGBA, (32, 32), Compression::ZIP1)], (32, 32)).into();
|
||||
let one = 4 * (32 * 32 * 4 + 1);
|
||||
let store = PlaneStore::new(one);
|
||||
let all = Want { all: true, ids: Default::default() };
|
||||
let mut broken = bytes.to_vec();
|
||||
broken.truncate(broken.len() - 64);
|
||||
let broken = ExrSource::Bytes(broken.into());
|
||||
let path = "/3271-trim-broken.exr";
|
||||
let index = crate::exr_index::get(path, &broken).unwrap();
|
||||
assert!(store.get(path, &broken, &index, &all, Use::Other).is_err());
|
||||
for p in ["/3271-trim-a.exr", "/3271-trim-b.exr"] {
|
||||
let source = ExrSource::Bytes(bytes.clone());
|
||||
let index = crate::exr_index::get(p, &source).unwrap();
|
||||
assert!(store.get(p, &source, &index, &all, Use::Other).is_ok());
|
||||
}
|
||||
store.trim_to(0);
|
||||
assert_eq!(store.cached(), [path], "the planes went, the failure stays");
|
||||
assert!(store.get(path, &broken, &index, &all, Use::Other).is_err());
|
||||
assert_eq!(decodes_of(path).len(), 1, "not read again");
|
||||
}
|
||||
+105
-37
@@ -145,9 +145,18 @@ pub(crate) const MAX_DECODE_BYTES: usize = 8 << 30;
|
||||
pub(crate) const MAX_DECODE_BYTES: usize = 512 << 20;
|
||||
|
||||
/// At most this many chunks, or bytes of them, are read before they are decompressed (and the
|
||||
/// request is checked for being called off).
|
||||
/// request is checked for being called off). The first batch takes at most [`FIRST_BATCH_BYTES`]
|
||||
/// and each one after twice the one before, so decoding starts early.
|
||||
const BATCH_CHUNKS: usize = 256;
|
||||
const BATCH_BYTES: u64 = 64 << 20;
|
||||
const FIRST_BATCH_BYTES: u64 = 1 << 20;
|
||||
|
||||
/// The raw chunks of a batch: their bytes one after another, and each chunk's part and bytes.
|
||||
#[derive(Default)]
|
||||
struct Raw {
|
||||
data: Vec<u8>,
|
||||
chunks: Vec<(usize, Range<usize>)>,
|
||||
}
|
||||
|
||||
/// Samples of one line placed in a plane.
|
||||
struct Placed {
|
||||
@@ -232,56 +241,110 @@ fn read_into(src: &mut dyn ReadSeek, index: &ExrIndex, req: &ReadRequest, stats:
|
||||
let geometry = Geometry { size: (w, h), out: (ow, oh), stride, wanted: &wanted };
|
||||
let mut pos: Option<u64> = None;
|
||||
let mut rest = jobs.as_slice();
|
||||
while !rest.is_empty() {
|
||||
let mut raw = Raw::default();
|
||||
let mut most = FIRST_BATCH_BYTES;
|
||||
loop {
|
||||
if filmcraft_media::cancel::cancelled() {
|
||||
return Err(ReadError::Cancelled);
|
||||
}
|
||||
// A batch: up to BATCH_CHUNKS chunks or BATCH_BYTES bytes (at least one chunk).
|
||||
let mut take = 0;
|
||||
let mut bytes = 0u64;
|
||||
while let Some((_, _, len)) = rest.get(take) {
|
||||
if take > 0 && (take >= BATCH_CHUNKS || bytes.saturating_add(*len) > BATCH_BYTES) {
|
||||
break;
|
||||
}
|
||||
bytes = bytes.saturating_add(*len);
|
||||
take += 1;
|
||||
}
|
||||
let (batch, more) = rest.split_at(take.min(rest.len()));
|
||||
// The next batch is read on this thread while the one read before decompresses and
|
||||
// converts on this pool's other threads (a network read overlaps the decoding).
|
||||
let (batch, more) = rest.split_at(batch_len(rest, most));
|
||||
rest = more;
|
||||
// Raw bytes on this thread, in file order (one seek per gap).
|
||||
let mut raw = Vec::with_capacity(batch.len());
|
||||
for (pi, offset, len) in batch {
|
||||
if pos != Some(*offset) {
|
||||
src.seek(SeekFrom::Start(*offset)).map_err(|e| ReadError::Io(e.to_string()))?;
|
||||
}
|
||||
let size = usize::try_from(*len).map_err(|_| ReadError::Unreadable("chunk size".into()))?;
|
||||
let mut buf = Vec::new();
|
||||
buf.try_reserve_exact(size).map_err(|e| ReadError::Io(e.to_string()))?;
|
||||
(&mut *src).take(*len).read_to_end(&mut buf).map_err(|e| ReadError::Io(e.to_string()))?;
|
||||
pos = Some(offset.saturating_add(buf.len() as u64));
|
||||
stats.bytes = stats.bytes.saturating_add(buf.len() as u64);
|
||||
if let Some(c) = stats.chunks.get_mut(*pi) {
|
||||
*c += 1;
|
||||
}
|
||||
raw.push((*pi, buf));
|
||||
}
|
||||
// Decompressed and converted on this pool's threads, placed here.
|
||||
let done: Vec<Result<(Vec<Placed>, usize), String>> =
|
||||
raw.into_par_iter().map(|(pi, buf)| chunk_lines(index, pi, &buf, slots.get(pi).map_or(&[][..], Vec::as_slice), &geometry)).collect();
|
||||
most = most.saturating_mul(2).min(BATCH_BYTES);
|
||||
let decode = std::mem::take(&mut raw);
|
||||
let (next, done) = rayon::join(
|
||||
|| read_batch(&mut *src, batch, &mut pos, stats),
|
||||
|| -> Vec<Result<(Vec<Placed>, usize), String>> {
|
||||
let data = &decode.data;
|
||||
decode
|
||||
.chunks
|
||||
.par_iter()
|
||||
.map(|(pi, r)| chunk_lines(index, *pi, data.get(r.clone()).unwrap_or_default(), slots.get(*pi).map_or(&[][..], Vec::as_slice), &geometry))
|
||||
.collect()
|
||||
},
|
||||
);
|
||||
// Placed plane by plane, the planes in parallel.
|
||||
let mut by_slot: Vec<Vec<Placed>> = (0..planes.len()).map(|_| vec![]).collect();
|
||||
for d in done {
|
||||
let (placed, lines) = d.map_err(ReadError::Unreadable)?;
|
||||
stats.lines = stats.lines.saturating_add(lines);
|
||||
for p in placed {
|
||||
let end = p.start.saturating_add(p.values.len());
|
||||
if let Some(dst) = planes.get_mut(p.slot).and_then(|pl| pl.get_mut(p.start..end)) {
|
||||
dst.copy_from_slice(&p.values);
|
||||
if let Some(s) = by_slot.get_mut(p.slot) {
|
||||
s.push(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
planes.par_iter_mut().zip(by_slot).for_each(|(plane, placed)| {
|
||||
for p in placed {
|
||||
let end = p.start.saturating_add(p.values.len());
|
||||
if let Some(dst) = plane.get_mut(p.start..end) {
|
||||
dst.copy_from_slice(&p.values);
|
||||
}
|
||||
}
|
||||
});
|
||||
raw = next?;
|
||||
if raw.chunks.is_empty() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(planes)
|
||||
}
|
||||
|
||||
/// How many of `jobs` (chunks in file order) the next batch takes: up to [`BATCH_CHUNKS`] chunks
|
||||
/// or `most` bytes, at least one.
|
||||
fn batch_len(jobs: &[(usize, u64, u64)], most: u64) -> usize {
|
||||
let mut take = 0;
|
||||
let mut bytes = 0u64;
|
||||
while let Some((_, _, len)) = jobs.get(take) {
|
||||
if take > 0 && (take >= BATCH_CHUNKS || bytes.saturating_add(*len) > most) {
|
||||
break;
|
||||
}
|
||||
bytes = bytes.saturating_add(*len);
|
||||
take += 1;
|
||||
}
|
||||
take
|
||||
}
|
||||
|
||||
/// The raw bytes of the chunks `batch` (part, offset, length; in file order): chunks that follow
|
||||
/// each other in the file are read in one go (one request to a network share, not one per
|
||||
/// chunk), with one seek per gap (`pos`: where the source stands). A chunk the file ends in
|
||||
/// keeps the bytes there are.
|
||||
fn read_batch(src: &mut dyn ReadSeek, batch: &[(usize, u64, u64)], pos: &mut Option<u64>, stats: &mut ReadStats) -> Result<Raw, ReadError> {
|
||||
let total = batch.iter().map(|(_, _, len)| *len).fold(0u64, u64::saturating_add);
|
||||
let mut raw = Raw::default();
|
||||
raw.data.try_reserve_exact(usize::try_from(total).map_err(|_| ReadError::Unreadable("chunk size".into()))?).map_err(|e| ReadError::Io(e.to_string()))?;
|
||||
let mut i = 0;
|
||||
while let Some((_, start, _)) = batch.get(i) {
|
||||
// A run of chunks, each starting where the one before ends.
|
||||
let (start, mut end, mut j) = (*start, *start, i);
|
||||
while let Some((_, offset, len)) = batch.get(j)
|
||||
&& *offset == end
|
||||
{
|
||||
end = offset.saturating_add(*len);
|
||||
j += 1;
|
||||
}
|
||||
if *pos != Some(start) {
|
||||
src.seek(SeekFrom::Start(start)).map_err(|e| ReadError::Io(e.to_string()))?;
|
||||
}
|
||||
let at = raw.data.len();
|
||||
(&mut *src).take(end - start).read_to_end(&mut raw.data).map_err(|e| ReadError::Io(e.to_string()))?;
|
||||
let got = raw.data.len() - at;
|
||||
*pos = Some(start.saturating_add(got as u64));
|
||||
for (pi, offset, len) in batch.get(i..j).unwrap_or_default() {
|
||||
let a = usize::try_from(offset - start).map_or(raw.data.len(), |o| at.saturating_add(o)).min(raw.data.len());
|
||||
let b = usize::try_from(*len).map_or(raw.data.len(), |l| a.saturating_add(l)).min(raw.data.len());
|
||||
raw.chunks.push((*pi, a..b));
|
||||
if let Some(c) = stats.chunks.get_mut(*pi) {
|
||||
*c += 1;
|
||||
}
|
||||
}
|
||||
stats.bytes = stats.bytes.saturating_add(got as u64);
|
||||
i = j.max(i + 1);
|
||||
}
|
||||
Ok(raw)
|
||||
}
|
||||
|
||||
/// The planes' geometry for [`chunk_lines`].
|
||||
struct Geometry<'w> {
|
||||
/// The display window's size, and the planes' (at the level).
|
||||
@@ -332,7 +395,12 @@ fn chunk_lines(index: &ExrIndex, pi: usize, buf: &[u8], slots: &[Option<usize>],
|
||||
SampleType::F16 => {
|
||||
f16s.resize(count, exr::prelude::f16::ZERO);
|
||||
line.read_samples_into_slice(&mut f16s).map_err(|e| e.to_string())?;
|
||||
f16s.iter().skip(skip).take(take).map(|s| s.to_f32()).collect()
|
||||
// (the slice conversion runs the same per-value conversion, in vector steps)
|
||||
let mut out = vec![0.0; take];
|
||||
if let Some(src) = f16s.get(skip..skip + take) {
|
||||
half::slice::HalfFloatSliceExt::convert_to_f32_slice(src, &mut out);
|
||||
}
|
||||
out
|
||||
}
|
||||
SampleType::F32 => {
|
||||
f32s.resize(count, 0.0);
|
||||
|
||||
@@ -16,6 +16,7 @@ effectcraft-color = { workspace = true }
|
||||
effectcraft-keyframe = { workspace = true }
|
||||
effectcraft-project = { workspace = true }
|
||||
effectcraft-raster = { workspace = true }
|
||||
half = { workspace = true }
|
||||
effectcraft-path = { workspace = true }
|
||||
effectcraft-text = { workspace = true }
|
||||
effectcraft-effects = { workspace = true }
|
||||
|
||||
+213
-126
@@ -40,7 +40,7 @@ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||
use std::sync::{Arc, Condvar, Mutex};
|
||||
|
||||
use effectcraft_effects::Buf;
|
||||
use effectcraft_raster::Image;
|
||||
use effectcraft_raster::{AuxChannels, Image};
|
||||
|
||||
use crate::reads::ReadFiles;
|
||||
|
||||
@@ -121,43 +121,22 @@ const FORMAT_F16_FILES: u8 = 3;
|
||||
/// Layer entries: their source files ([`EntryFiles`]), then the buffer. The format before it
|
||||
/// (0, without files) reads as a miss and is deleted.
|
||||
const FORMAT_LAYER_FILES: u8 = 4;
|
||||
/// Plane entries: their source file ([`EntryFiles`]), then named `f32` planes ([`DiskPlanes`]).
|
||||
const FORMAT_PLANES_FILES: u8 = 5;
|
||||
/// Plane entries: their source file ([`EntryFiles`]), then one named plane ([`encode_plane`]).
|
||||
/// (5, several `f32` planes per entry, reads as a miss and is deleted.)
|
||||
const FORMAT_PLANES_FILES: u8 = 6;
|
||||
|
||||
/// Decoded footage channels as a plane entry holds them (#3271): named `f32` planes of
|
||||
/// `width` × `height`, bit-exact (Cryptomatte IDs are float bit patterns).
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct DiskPlanes {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub planes: Vec<(String, Vec<f32>)>,
|
||||
}
|
||||
|
||||
/// The most planes and pixels a plane entry holds (a damaged entry can't ask for more).
|
||||
const MAX_ENTRY_PLANES: usize = 4096;
|
||||
/// The most pixels a plane entry holds (a damaged entry can't ask for more).
|
||||
const MAX_ENTRY_PIXELS: usize = 1 << 28;
|
||||
|
||||
/// The disk key of decoded footage channels: the source file's path and identity as read, the
|
||||
/// channel names (in the order stored), the level and the rows read (`None`: all).
|
||||
pub fn plane_key(path: &str, stamp: &crate::reads::FileStamp, channels: &[&str], level: u32, rows: Option<(i64, i64)>) -> u128 {
|
||||
/// The disk key of a decoded footage channel: the source file's path and identity as read, and
|
||||
/// the channel's name.
|
||||
pub fn plane_key(path: &str, stamp: &crate::reads::FileStamp, channel: &str) -> u128 {
|
||||
let mut h = Hash128::default();
|
||||
h.write(b"planes-v1");
|
||||
h.write(b"plane-v2");
|
||||
h.write(path.as_bytes());
|
||||
h.write(format!("{stamp:?}").as_bytes());
|
||||
h.write_u64(channels.len() as u64);
|
||||
for c in channels {
|
||||
h.write_u64(c.len() as u64);
|
||||
h.write(c.as_bytes());
|
||||
}
|
||||
h.write_u64(u64::from(level));
|
||||
match rows {
|
||||
Some((a, b)) => {
|
||||
h.write_u64(1);
|
||||
h.write_u64(a as u64);
|
||||
h.write_u64(b as u64);
|
||||
}
|
||||
None => h.write_u64(0),
|
||||
}
|
||||
h.write_u64(channel.len() as u64);
|
||||
h.write(channel.as_bytes());
|
||||
h.finish()
|
||||
}
|
||||
|
||||
@@ -326,8 +305,9 @@ enum Payload {
|
||||
/// An 8-bit or half-float frame (width, height, pixels) and its source files.
|
||||
Frame8(u32, u32, Vec<u8>, EntryFiles),
|
||||
FrameF16(u32, u32, Vec<u16>, EntryFiles),
|
||||
/// Decoded footage planes (width, height, named planes), and their source file.
|
||||
Planes(u32, u32, Vec<(String, Vec<f32>)>, EntryFiles),
|
||||
/// Decoded footage channels: planes of a set the plane store holds (no copy), each written
|
||||
/// as an entry of its own (key, plane index), and their source file.
|
||||
Planes(Arc<AuxChannels>, Vec<(u128, usize)>, EntryFiles),
|
||||
/// Bytes to compress with LZ4 (a segmentation).
|
||||
Lz4(Vec<u8>),
|
||||
}
|
||||
@@ -345,18 +325,29 @@ pub(crate) fn encoded_here() -> u64 {
|
||||
}
|
||||
|
||||
impl Payload {
|
||||
/// The entry's payload bytes: its source files, then its pixels, compressed.
|
||||
fn encode(self) -> Vec<u8> {
|
||||
/// Encode it (its source files, then its pixels, compressed) and write it to `cache` as
|
||||
/// `key` (planes: each under its own key); an entry written meanwhile by another render
|
||||
/// isn't encoded again.
|
||||
fn write(self, cache: &DiskCache, kind: Kind, key: u128, format: u8) {
|
||||
#[cfg(test)]
|
||||
if !matches!(self, Payload::Lz4(_)) {
|
||||
ENCODED_HERE.with(|n| n.set(n.get() + 1));
|
||||
}
|
||||
let one = |key: u128, bytes: &dyn Fn() -> Vec<u8>| {
|
||||
if !cache.contains(kind, key) {
|
||||
cache.write_format(kind, key, format, &bytes());
|
||||
}
|
||||
};
|
||||
match self {
|
||||
Payload::Layer(buf, files) => with_files(&files, encode_layer(&buf)),
|
||||
Payload::Frame8(w, h, rgba, files) => with_files(&files, encode_frame(w, h, &rgba)),
|
||||
Payload::FrameF16(w, h, rgba, files) => with_files(&files, encode_frame_f16(w, h, &rgba)),
|
||||
Payload::Planes(w, h, planes, files) => with_files(&files, encode_planes(w, h, &planes)),
|
||||
Payload::Lz4(raw) => lz4_flex::compress_prepend_size(&raw),
|
||||
Payload::Layer(buf, files) => one(key, &|| with_files(&files, encode_layer(&buf))),
|
||||
Payload::Frame8(w, h, rgba, files) => one(key, &|| with_files(&files, encode_frame(w, h, &rgba))),
|
||||
Payload::FrameF16(w, h, rgba, files) => one(key, &|| with_files(&files, encode_frame_f16(w, h, &rgba))),
|
||||
Payload::Planes(aux, planes, files) => {
|
||||
for (key, at) in planes {
|
||||
one(key, &|| with_files(&files, encode_plane(&aux, at)));
|
||||
}
|
||||
}
|
||||
Payload::Lz4(raw) => one(key, &|| lz4_flex::compress_prepend_size(&raw)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -367,15 +358,19 @@ impl Payload {
|
||||
Payload::Layer(buf, _) => buf.img.data.len().saturating_mul(16),
|
||||
Payload::Frame8(_, _, p, _) => p.len(),
|
||||
Payload::FrameF16(_, _, p, _) => p.len().saturating_mul(2),
|
||||
Payload::Planes(_, _, planes, _) => planes.iter().map(|(_, p)| p.len().saturating_mul(4)).fold(0, usize::saturating_add),
|
||||
Payload::Planes(aux, planes, _) => {
|
||||
planes.iter().filter_map(|(_, at)| aux.channels.get(*at)).map(|(_, p)| p.len().saturating_mul(4)).fold(0, usize::saturating_add)
|
||||
}
|
||||
Payload::Lz4(raw) => raw.len(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The bytes only this payload holds: a layer buffer the RAM cache shares counts there.
|
||||
/// The bytes only this payload holds: a layer buffer the RAM cache shares counts there, and
|
||||
/// so do planes the plane store still holds.
|
||||
fn unique(&self) -> usize {
|
||||
match self {
|
||||
Payload::Layer(buf, _) if Arc::strong_count(buf) > 1 => 0,
|
||||
Payload::Planes(aux, _, _) if Arc::strong_count(aux) > 1 => 0,
|
||||
_ => self.held(),
|
||||
}
|
||||
}
|
||||
@@ -920,7 +915,7 @@ impl DiskCache {
|
||||
if !matches!(payload, Payload::Lz4(_)) {
|
||||
self.count_encode();
|
||||
}
|
||||
self.write_format(kind, key, format, &payload.encode());
|
||||
payload.write(self, kind, key, format);
|
||||
true
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -1261,27 +1256,30 @@ impl DiskCache {
|
||||
}
|
||||
}
|
||||
|
||||
/// Decoded footage channels ([`plane_key`]) whose source file is unchanged, with that file,
|
||||
/// as [`DiskCache::get_checked`] (#3271). `virtual_id` names files served from memory.
|
||||
/// `expect`: the planes' width, height and count; an entry of another shape is a miss
|
||||
/// (nothing larger is decompressed).
|
||||
pub fn get_planes(&self, key: u128, virtual_id: Option<VirtualFileId<'_>>, expect: (u32, u32, usize)) -> Option<(DiskPlanes, ReadFiles)> {
|
||||
self.get_checked(Kind::Plane, key, FORMAT_PLANES_FILES, virtual_id, |p| decode_planes(p, expect))
|
||||
/// A decoded footage channel ([`plane_key`]) whose source file is unchanged, as
|
||||
/// [`DiskCache::get_checked`] (#3271): its name and its `width` x `height` plane. An entry
|
||||
/// of another size is a miss (nothing larger is decompressed).
|
||||
pub fn get_plane(&self, key: u128, width: u32, height: u32) -> Option<(String, Vec<f32>)> {
|
||||
self.get_checked(Kind::Plane, key, FORMAT_PLANES_FILES, None, |p| decode_plane(p, (width, height))).map(|(plane, _)| plane)
|
||||
}
|
||||
|
||||
/// Queue decoded footage channels for writing: named `width` × `height` planes (in the order
|
||||
/// [`plane_key`] names them), with the source file they were read from; encoded on the writer
|
||||
/// thread. Refused when a plane is of another size, there is no room, or the source file was
|
||||
/// dropped after `since`.
|
||||
pub fn put_planes(&self, key: u128, width: u32, height: u32, planes: Vec<(String, Vec<f32>)>, files: &EntryFiles, since: u64) -> bool {
|
||||
if self.contains(Kind::Plane, key) {
|
||||
return true;
|
||||
}
|
||||
let n = (width as usize).checked_mul(height as usize);
|
||||
if planes.is_empty() || planes.iter().any(|(_, p)| Some(p.len()) != n) || !self.accepts_writes() {
|
||||
/// Queue planes of `aux` (decoded footage channels: each [`plane_key`] and plane index)
|
||||
/// for writing, with the source file they were read from: one job for all of them, which
|
||||
/// the writer thread encodes from the set itself (no copy). Planes stored already or of
|
||||
/// another size are left out; refused when there is no room or the source file was dropped
|
||||
/// after `since`.
|
||||
pub fn put_planes(&self, aux: &Arc<AuxChannels>, planes: &[(u128, usize)], files: &EntryFiles, since: u64) -> bool {
|
||||
let n = (aux.width as usize).checked_mul(aux.height as usize);
|
||||
let planes: Vec<(u128, usize)> = planes
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|(key, at)| !self.contains(Kind::Plane, *key) && aux.channels.get(*at).is_some_and(|(_, p)| Some(p.len()) == n))
|
||||
.collect();
|
||||
let Some((first, _)) = planes.first().copied() else { return true };
|
||||
if !self.accepts_writes() {
|
||||
return false;
|
||||
}
|
||||
self.enqueue_format(Kind::Plane, key, FORMAT_PLANES_FILES, Payload::Planes(width, height, planes, files.clone()), Some((files, since)))
|
||||
self.enqueue_format(Kind::Plane, first, FORMAT_PLANES_FILES, Payload::Planes(aux.clone(), planes, files.clone()), Some((files, since)))
|
||||
}
|
||||
|
||||
/// A segmentation (chain key, scale) written by [`DiskCache::put_seg`].
|
||||
@@ -1383,10 +1381,7 @@ fn writer(dc: std::sync::Weak<DiskCache>, q: Arc<(Mutex<Queue>, Condvar)>) {
|
||||
let Some(cache) = dc.upgrade() else { return };
|
||||
let written = match job {
|
||||
Job::Write(kind, key, format, payload) => {
|
||||
// (written meanwhile by another render: not encoded again)
|
||||
if !cache.contains(kind, key) {
|
||||
cache.write_format(kind, key, format, &payload.encode());
|
||||
}
|
||||
payload.write(&cache, kind, key, format);
|
||||
(kind == Kind::Frame).then_some(key)
|
||||
}
|
||||
Job::Drop(paths) => {
|
||||
@@ -1609,82 +1604,86 @@ fn decode_frame_f16(p: &[u8]) -> Option<FrameF16> {
|
||||
Some(FrameF16 { width: w, height: h, rgba })
|
||||
}
|
||||
|
||||
/// A plane entry's pixels: width, height, the plane count, each plane's name (length and UTF-8
|
||||
/// bytes) and type (2: `f32`), then every plane's bytes shuffled into byte planes (plane by plane,
|
||||
/// the first bytes of its floats, then the second…), LZ4-compressed. Planes of `aux` named
|
||||
/// `names` that it doesn't hold, or holds at another size, are written as zeros.
|
||||
fn encode_planes(w: u32, h: u32, planes: &[(String, Vec<f32>)]) -> Vec<u8> {
|
||||
let n = (w as usize).saturating_mul(h as usize);
|
||||
/// A plane entry's pixels (plane `at` of `aux`): width, height, the name (length and UTF-8
|
||||
/// bytes) and the sample type, then the samples' bytes shuffled into byte planes (the first
|
||||
/// bytes of all samples, then the second...), LZ4-compressed. Type 1: half floats, when every
|
||||
/// value is one exactly (a half channel: half the bytes); type 2: `f32` (bit-exact: Cryptomatte
|
||||
/// IDs are float bit patterns).
|
||||
fn encode_plane(aux: &AuxChannels, at: usize) -> Vec<u8> {
|
||||
let Some((name, plane)) = aux.channels.get(at) else { return vec![] };
|
||||
let mut out = Vec::with_capacity(64);
|
||||
out.extend_from_slice(&w.to_le_bytes());
|
||||
out.extend_from_slice(&h.to_le_bytes());
|
||||
out.extend_from_slice(&(planes.len() as u32).to_le_bytes());
|
||||
for (name, _) in planes {
|
||||
let bytes = name.as_bytes();
|
||||
let len = bytes.len().min(u16::MAX as usize);
|
||||
out.extend_from_slice(&(len as u16).to_le_bytes());
|
||||
out.extend_from_slice(bytes.get(..len).unwrap_or_default());
|
||||
out.extend_from_slice(&aux.width.to_le_bytes());
|
||||
out.extend_from_slice(&aux.height.to_le_bytes());
|
||||
let name = name.as_bytes();
|
||||
let len = name.len().min(u16::MAX as usize);
|
||||
out.extend_from_slice(&(len as u16).to_le_bytes());
|
||||
out.extend_from_slice(name.get(..len).unwrap_or_default());
|
||||
let mut halves = vec![half::f16::ZERO; plane.len()];
|
||||
half::slice::HalfFloatSliceExt::convert_from_f32_slice(halves.as_mut_slice(), plane);
|
||||
// (NaNs stay `f32`: their payloads must come back as they were)
|
||||
let exact = halves.iter().zip(plane).all(|(h, v)| !v.is_nan() && h.to_f32().to_bits() == v.to_bits());
|
||||
let shuffled = if exact {
|
||||
out.push(1);
|
||||
shuffle(&halves.iter().map(|h| u32::from(h.to_bits())).collect::<Vec<_>>(), 2)
|
||||
} else {
|
||||
out.push(2);
|
||||
}
|
||||
let Some(total) = n.checked_mul(4).and_then(|b| b.checked_mul(planes.len())) else { return out };
|
||||
let mut shuffled = vec![0u8; total];
|
||||
for (k, (_, plane)) in planes.iter().enumerate() {
|
||||
let Some(base) = k.checked_mul(n).and_then(|b| b.checked_mul(4)) else { continue };
|
||||
for (i, v) in plane.iter().take(n).enumerate() {
|
||||
for (byte, value) in v.to_bits().to_le_bytes().iter().enumerate() {
|
||||
if let Some(o) = shuffled.get_mut(base + byte * n + i) {
|
||||
*o = *value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
shuffle(&plane.iter().map(|v| v.to_bits()).collect::<Vec<_>>(), 4)
|
||||
};
|
||||
out.extend_from_slice(&lz4_flex::compress_prepend_size(&shuffled));
|
||||
out
|
||||
}
|
||||
|
||||
/// The planes of a plane entry's pixels ([`encode_planes`]); `None` when damaged.
|
||||
fn decode_planes(p: &[u8], expect: (u32, u32, usize)) -> Option<DiskPlanes> {
|
||||
/// The low `width` bytes of each of `words` as byte planes: the first bytes of all, then the
|
||||
/// second... (similar bytes side by side compress better).
|
||||
fn shuffle(words: &[u32], width: usize) -> Vec<u8> {
|
||||
let mut out = vec![0u8; words.len().saturating_mul(width)];
|
||||
for (b, dst) in out.chunks_exact_mut(words.len().max(1)).enumerate() {
|
||||
for (o, w) in dst.iter_mut().zip(words) {
|
||||
*o = (w >> (8 * b)) as u8;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The name and plane of a plane entry's pixels ([`encode_plane`]) of `expect` (width, height);
|
||||
/// `None` when damaged or of another size.
|
||||
fn decode_plane(p: &[u8], expect: (u32, u32)) -> Option<(String, Vec<f32>)> {
|
||||
let u32_at = |at: usize| Some(u32::from_le_bytes(p.get(at..at + 4)?.try_into().ok()?));
|
||||
let (w, h, count) = (u32_at(0)?, u32_at(4)?, u32_at(8)? as usize);
|
||||
if (w, h, count) != expect {
|
||||
let (w, h) = (u32_at(0)?, u32_at(4)?);
|
||||
if (w, h) != expect {
|
||||
return None;
|
||||
}
|
||||
let n = (w as usize).checked_mul(h as usize).filter(|n| *n <= MAX_ENTRY_PIXELS)?;
|
||||
if count > MAX_ENTRY_PLANES {
|
||||
return None;
|
||||
}
|
||||
let mut at = 12;
|
||||
let mut names = Vec::with_capacity(count);
|
||||
for _ in 0..count {
|
||||
let len = u16::from_le_bytes(p.get(at..at + 2)?.try_into().ok()?) as usize;
|
||||
let name = std::str::from_utf8(p.get(at + 2..at + 2 + len)?).ok()?.to_string();
|
||||
if *p.get(at + 2 + len)? != 2 {
|
||||
return None;
|
||||
}
|
||||
names.push(name);
|
||||
at += 3 + len;
|
||||
}
|
||||
let body = p.get(at..)?;
|
||||
// (the size the compressed block says must be the planes': nothing larger is allocated)
|
||||
let want = n.checked_mul(4)?.checked_mul(count)?;
|
||||
let len = u16::from_le_bytes(p.get(8..10)?.try_into().ok()?) as usize;
|
||||
let name = std::str::from_utf8(p.get(10..10 + len)?).ok()?.to_string();
|
||||
let width = match *p.get(10 + len)? {
|
||||
1 => 2,
|
||||
2 => 4,
|
||||
_ => return None,
|
||||
};
|
||||
let body = p.get(11 + len..)?;
|
||||
// (the size the compressed block says must be the plane's: nothing larger is allocated)
|
||||
let want = n.checked_mul(width)?;
|
||||
if u32::from_le_bytes(body.get(..4)?.try_into().ok()?) as usize != want {
|
||||
return None;
|
||||
}
|
||||
let shuffled = lz4_flex::decompress_size_prepended(body).ok()?;
|
||||
if shuffled.len() != want {
|
||||
let s = lz4_flex::decompress_size_prepended(body).ok()?;
|
||||
if s.len() != want {
|
||||
return None;
|
||||
}
|
||||
let mut planes = Vec::with_capacity(count);
|
||||
for (k, name) in names.into_iter().enumerate() {
|
||||
let base = k * n * 4;
|
||||
let mut plane = Vec::with_capacity(n);
|
||||
for i in 0..n {
|
||||
let b = [*shuffled.get(base + i)?, *shuffled.get(base + n + i)?, *shuffled.get(base + 2 * n + i)?, *shuffled.get(base + 3 * n + i)?];
|
||||
plane.push(f32::from_bits(u32::from_le_bytes(b)));
|
||||
}
|
||||
planes.push((name, plane));
|
||||
}
|
||||
Some(DiskPlanes { width: w, height: h, planes })
|
||||
let plane = if width == 2 {
|
||||
let (lo, hi) = s.split_at_checked(n)?;
|
||||
let halves: Vec<half::f16> = lo.iter().zip(hi).map(|(l, h)| half::f16::from_bits(u16::from_le_bytes([*l, *h]))).collect();
|
||||
let mut plane = vec![0.0; n];
|
||||
half::slice::HalfFloatSliceExt::convert_to_f32_slice(halves.as_slice(), &mut plane);
|
||||
plane
|
||||
} else {
|
||||
let (b0, rest) = s.split_at_checked(n)?;
|
||||
let (b1, rest) = rest.split_at_checked(n)?;
|
||||
let (b2, b3) = rest.split_at_checked(n)?;
|
||||
b0.iter().zip(b1).zip(b2).zip(b3).map(|(((a, b), c), d)| f32::from_bits(u32::from_le_bytes([*a, *b, *c, *d]))).collect()
|
||||
};
|
||||
Some((name, plane))
|
||||
}
|
||||
|
||||
fn encode_layer(b: &Buf) -> Vec<u8> {
|
||||
@@ -2025,6 +2024,94 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A source file for plane entries (it must exist: hits check it is unchanged).
|
||||
fn plane_source(dir: &Path) -> (EntryFiles, crate::reads::FileStamp) {
|
||||
std::fs::create_dir_all(dir).unwrap();
|
||||
let src = dir.join("plate.exr");
|
||||
std::fs::write(&src, b"exr").unwrap();
|
||||
let stamp = crate::reads::FileStamp::of_metadata(&std::fs::metadata(&src).unwrap());
|
||||
let mut files = ReadFiles::default();
|
||||
files.add(crate::reads::ReadFile::new(src.to_string_lossy().to_string(), stamp));
|
||||
(EntryFiles::of(&files, None).unwrap(), stamp)
|
||||
}
|
||||
|
||||
/// #3271: a plane entry gives back every value bit for bit. A plane of half values is stored
|
||||
/// as half floats (half the bytes), any other (Cryptomatte IDs, NaN payloads, values a half
|
||||
/// can't hold) as `f32`; -0.0 and denormals come back as they were. An entry of another size,
|
||||
/// a damaged one and one of the earlier format read as misses.
|
||||
#[test]
|
||||
fn plane_entries_round_trip_bit_for_bit() {
|
||||
let dir = tmpdir("planes");
|
||||
let dc = DiskCache::open(&dir, 1 << 30).unwrap();
|
||||
let (files, _) = plane_source(&dir.join("src"));
|
||||
let (w, h) = (16u32, 8u32);
|
||||
let n = (w * h) as usize;
|
||||
let halves: Vec<f32> = (0..n).map(|i| half::f16::from_bits((i * 517) as u16 & 0x7bff).to_f32()).collect();
|
||||
let mut mixed: Vec<f32> = (0..n).map(|i| f32::from_bits(0x3f80_0001 + i as u32 * 7919)).collect();
|
||||
mixed[0] = f32::from_bits(0x7fc0_1234);
|
||||
mixed[1] = -0.0;
|
||||
mixed[2] = f32::from_bits(1);
|
||||
let mut nan_half = halves.clone();
|
||||
nan_half[3] = f32::from_bits(0x7fc0_0000 | 0x2000);
|
||||
let mut aux = AuxChannels::new(w, h, 1.0);
|
||||
aux.channels = vec![("A.R".into(), halves.clone()), ("A.id".into(), mixed.clone()), ("A.nan".into(), nan_half.clone())];
|
||||
let aux = Arc::new(aux);
|
||||
for (at, want) in [(0, 1u8), (1, 2), (2, 2)] {
|
||||
let p = encode_plane(&aux, at);
|
||||
let len = p[8] as usize;
|
||||
assert_eq!(p[10 + len], want, "plane {at}: sample type");
|
||||
}
|
||||
assert!(dc.put_planes(&aux, &[(1, 0), (2, 1), (3, 2)], &files, 0));
|
||||
dc.flush();
|
||||
for (key, name, want) in [(1, "A.R", &halves), (2, "A.id", &mixed), (3, "A.nan", &nan_half)] {
|
||||
let (got_name, got) = dc.get_plane(key, w, h).unwrap();
|
||||
assert_eq!(got_name, name);
|
||||
assert_eq!(got.iter().map(|v| v.to_bits()).collect::<Vec<_>>(), want.iter().map(|v| v.to_bits()).collect::<Vec<_>>(), "{name}");
|
||||
}
|
||||
assert!(dc.get_plane(1, w, h + 1).is_none(), "another size: a miss");
|
||||
let entry = encode_plane(&aux, 1);
|
||||
assert!(decode_plane(entry.get(..entry.len() - 9).unwrap(), (w, h)).is_none(), "short");
|
||||
let mut damaged = entry.clone();
|
||||
let len = damaged[8] as usize;
|
||||
damaged[10 + len] = 3;
|
||||
assert!(decode_plane(&damaged, (w, h)).is_none(), "unknown sample type");
|
||||
// An entry of the earlier format (5) is a miss, and deleted.
|
||||
dc.write_format(Kind::Plane, 9, 5, &with_files(&files, encode_plane(&aux, 0)));
|
||||
assert!(dc.contains(Kind::Plane, 9));
|
||||
assert!(dc.get_plane(9, w, h).is_none());
|
||||
assert!(!dc.contains(Kind::Plane, 9), "deleted");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// #3271: the planes of one decode are queued as one job (a file of 69 channels lost some to
|
||||
/// the queue's job limit), each written as an entry of its own; while the plane store holds
|
||||
/// them they count there, once it let go of them they count in the queue.
|
||||
#[test]
|
||||
fn a_decode_queues_its_planes_as_one_job() {
|
||||
let dir = tmpdir("planes-job");
|
||||
let dc = DiskCache::open(&dir, 1 << 30).unwrap();
|
||||
let (files, _) = plane_source(&dir.join("src"));
|
||||
let mut aux = AuxChannels::new(4, 2, 1.0);
|
||||
aux.channels = (0..69).map(|i| (format!("c{i:02}"), vec![i as f32; 8])).collect();
|
||||
let aux = Arc::new(aux);
|
||||
let planes: Vec<(u128, usize)> = (0..69).map(|i| (100 + i as u128, i)).collect();
|
||||
let job = Payload::Planes(aux.clone(), planes.clone(), files.clone());
|
||||
assert_eq!(job.held(), 69 * 8 * 4);
|
||||
assert_eq!(job.unique(), 0, "the store holds them");
|
||||
let kept = Arc::downgrade(&aux);
|
||||
drop(aux);
|
||||
assert_eq!(job.unique(), 69 * 8 * 4, "the queue alone holds them");
|
||||
let aux = kept.upgrade().unwrap();
|
||||
drop(job);
|
||||
assert!(dc.put_planes(&aux, &planes, &files, 0));
|
||||
dc.flush();
|
||||
assert_eq!(dc.keys(Kind::Plane).len(), 69);
|
||||
assert_eq!(dc.stats().dropped, 0);
|
||||
assert_eq!(crate::memory::MemoryHolder::bytes(&*dc), 0, "written: the queue holds nothing");
|
||||
assert_eq!(dc.get_plane(168, 4, 2).map(|(n, p)| (n, p[0])), Some(("c68".to_string(), 68.0)));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hit_miss_and_exact_round_trip() {
|
||||
let dir = tmpdir("hit");
|
||||
|
||||
Reference in new issue
Block a user