mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-20 01:14:53 +01:00
Footage cache (#3271): OpenEXR index, chunk reader and plane store
exr_index: headers and offset tables only, kept per file; probe and the channel lists use it. exr_read: only the chunks a request needs, by offset table in file order, decompressed on the decode threads, only the requested channels converted; rows and levels in the API; cancellable. exr_planes: one store of decoded planes per file and level for the picture and the effects, single flight with merged channel sets and follow-up decodes of what is missing; replaces the pool's OpenEXR decode and the separate channel cache. Pixels as before.
This commit is contained in:
1 parent
089f8e374d
commit
804067b9da
8 files changed
+1998
-948
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,371 @@
|
||||
//! [`ExrIndex`]: what an OpenEXR file holds, read from its headers and offset tables only
|
||||
//! (#3271): the parts, every channel by its plane name, where each chunk lies and which rows it
|
||||
//! holds, the Cryptomatte manifests and the channels its picture is made of.
|
||||
//!
|
||||
//! An index is a few kilobytes and is kept per file ([`get`]): the channel popups,
|
||||
//! `layer.channels`, the Cryptomatte layer lists, import and the planning of a read use it
|
||||
//! and never decode pixels. The reader ([`crate::exr_read`]) fetches the chunks it lists.
|
||||
//!
|
||||
//! Everything is validated before anything is sized by it: the display window
|
||||
//! ([`crate::exr_channels::MAX_DISPLAY_PIXELS`]), the chunk count (eight bytes of offset table
|
||||
//! each, so never more than the file can hold), and every offset (inside the chunk area; a chunk
|
||||
//! with a bad offset is never read).
|
||||
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::io::{Read, Seek, SeekFrom};
|
||||
use std::sync::{Arc, Mutex, PoisonError};
|
||||
|
||||
use effectcraft_raster::AuxChannels;
|
||||
use effectcraft_raster::channels3d::BACKGROUND_DEPTH;
|
||||
use effectcraft_render::reads::FileStamp;
|
||||
use exr::meta::MetaData;
|
||||
use exr::meta::attribute::{AttributeValue, IntegerBounds, Text};
|
||||
|
||||
use crate::exr_channels::{MAX_DISPLAY_PIXELS, Rgb, channel_name, is_depth, parse_manifest, part_name, picture_names};
|
||||
use crate::exr_read::{ExrSource, ReadSeek};
|
||||
|
||||
/// One channel of the file, in file order (parts in order, each part's channels as its header
|
||||
/// lists them): the order of [`AuxChannels::channels`] when every channel is read.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct IndexChannel {
|
||||
/// The plane name (`part.channel`, see [`channel_name`]).
|
||||
pub(crate) name: String,
|
||||
/// Its part and its position in the part's channel list.
|
||||
pub(crate) part: usize,
|
||||
pub(crate) channel: usize,
|
||||
/// The value outside the part's data window (the background depth for depth channels).
|
||||
pub(crate) fill: f32,
|
||||
}
|
||||
|
||||
/// One chunk of a part, in offset-table order.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(crate) struct IndexChunk {
|
||||
/// Where it starts in the file, and how many bytes to read for it (0: its offset is not
|
||||
/// valid, it is never read).
|
||||
pub(crate) offset: u64,
|
||||
pub(crate) len: u64,
|
||||
/// Its first row in the part's data window and how many rows it holds.
|
||||
pub(crate) y: i64,
|
||||
pub(crate) rows: usize,
|
||||
/// Of the full-resolution level (tiled files also store reduced levels).
|
||||
pub(crate) largest: bool,
|
||||
}
|
||||
|
||||
/// One part of the file.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct IndexPart {
|
||||
/// The part's data window position in the display window, and the data window's size.
|
||||
pub(crate) offset: (i64, i64),
|
||||
pub(crate) layer: (usize, usize),
|
||||
pub(crate) chunks: Vec<IndexChunk>,
|
||||
}
|
||||
|
||||
/// What an OpenEXR file holds ([`get`]).
|
||||
pub(crate) struct ExrIndex {
|
||||
pub(crate) meta: MetaData,
|
||||
/// The file's identity when indexed: a read of another identity indexes it again.
|
||||
pub(crate) stamp: FileStamp,
|
||||
/// The first part's display window: every plane covers it.
|
||||
pub(crate) display: IntegerBounds,
|
||||
pub(crate) parts: Vec<IndexPart>,
|
||||
pub(crate) channels: Vec<IndexChannel>,
|
||||
/// How the `image` crate's decoder would show the file (indices into `channels`).
|
||||
pub(crate) rgb: Rgb,
|
||||
/// A file the decoder refuses ([`Rgb::None`]): the channels its picture is made of
|
||||
/// ([`picture_names`]: red, green, blue and alpha by name) and every channel answering to
|
||||
/// one of those names (lookups ignore case).
|
||||
pub(crate) picture: Option<(Vec<usize>, [String; 3], Option<String>)>,
|
||||
/// The names only (planes empty), manifests and aliases: for the channel lists and for
|
||||
/// resolving what effects declare. Never handed to an effect.
|
||||
pub(crate) names: Arc<AuxChannels>,
|
||||
}
|
||||
|
||||
impl ExrIndex {
|
||||
/// The display window's size.
|
||||
pub(crate) fn size(&self) -> (usize, usize) {
|
||||
(self.display.size.0, self.display.size.1)
|
||||
}
|
||||
|
||||
/// The channels the picture is made of: the image crate's choice ([`Rgb::Planes`]) or the
|
||||
/// layered rule's. `None`: the planes can't make it (decode with the `image` crate).
|
||||
pub(crate) fn picture_ids(&self) -> Option<Vec<usize>> {
|
||||
match self.rgb {
|
||||
Rgb::Planes([r, g, b], a) => Some([Some(r), Some(g), Some(b), a].into_iter().flatten().collect()),
|
||||
Rgb::None => self.picture.as_ref().map(|(ids, _, _)| ids.clone()),
|
||||
Rgb::Unsupported => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The channels named `names` (exact plane names, each once, in file order).
|
||||
pub(crate) fn ids_named<'a>(&self, names: impl IntoIterator<Item = &'a str>) -> Vec<usize> {
|
||||
let names: std::collections::HashSet<&str> = names.into_iter().collect();
|
||||
self.channels.iter().enumerate().filter(|(_, c)| names.contains(c.name.as_str())).map(|(i, _)| i).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// Why a file has no index.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) enum IndexError {
|
||||
/// Not an OpenEXR file this reader takes (decoders may still try the whole file).
|
||||
Unreadable,
|
||||
/// An OpenEXR file whose compression can't be decoded (the reason, to show).
|
||||
Unsupported(&'static str),
|
||||
/// The file can't be opened or read.
|
||||
Io,
|
||||
}
|
||||
|
||||
/// Bytes counted as they are read (the offset tables start where the headers end).
|
||||
struct Counted<'r> {
|
||||
inner: &'r mut dyn ReadSeek,
|
||||
read: u64,
|
||||
}
|
||||
|
||||
impl Read for Counted<'_> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
let n = self.inner.read(buf)?;
|
||||
self.read = self.read.saturating_add(n as u64);
|
||||
Ok(n)
|
||||
}
|
||||
}
|
||||
|
||||
/// The headers of the file in `src` as the high-level reader takes them (headers read strictly,
|
||||
/// then validated leniently: the files that opened before open now).
|
||||
fn headers(src: &mut dyn ReadSeek) -> Result<(MetaData, u64), IndexError> {
|
||||
let (strict, read) = {
|
||||
let mut counted = Counted { inner: &mut *src, read: 0 };
|
||||
let strict = MetaData::read_from_buffered(&mut counted, true).and_then(|meta| MetaData::validate(&meta.headers, false).map(|_| meta));
|
||||
(strict, counted.read)
|
||||
};
|
||||
match strict {
|
||||
Ok(meta) => Ok((meta, read)),
|
||||
Err(_) => {
|
||||
// (an HTJ2K file fails here: say why)
|
||||
src.seek(SeekFrom::Start(0)).map_err(|_| IndexError::Io)?;
|
||||
match MetaData::read_from_buffered(&mut *src, false).ok().as_ref().and_then(unsupported) {
|
||||
Some(why) => Err(IndexError::Unsupported(why)),
|
||||
None => Err(IndexError::Unreadable),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// [`crate::exr_channels::unsupported_compression`] of parsed headers.
|
||||
pub(crate) fn unsupported(meta: &MetaData) -> Option<&'static str> {
|
||||
use exr::compression::Compression;
|
||||
meta.headers
|
||||
.iter()
|
||||
.any(|h| matches!(h.compression, Compression::HTJ2K32 | Compression::HTJ2K256))
|
||||
.then_some("OpenEXR HTJ2K compression isn't supported yet: re-save the file with ZIP, PIZ or DWAA compression")
|
||||
}
|
||||
|
||||
/// Index the OpenEXR file in `src` (`len` bytes, identity `stamp`), reading its headers and
|
||||
/// offset tables only.
|
||||
pub(crate) fn build(src: &mut dyn ReadSeek, len: u64, stamp: FileStamp) -> Result<ExrIndex, IndexError> {
|
||||
src.seek(SeekFrom::Start(0)).map_err(|_| IndexError::Io)?;
|
||||
let (meta, header_end) = headers(&mut *src)?;
|
||||
if let Some(why) = unsupported(&meta) {
|
||||
return Err(IndexError::Unsupported(why));
|
||||
}
|
||||
let first = meta.headers.first().ok_or(IndexError::Unreadable)?;
|
||||
// Deep data isn't read (the whole file was refused before).
|
||||
if meta.headers.iter().any(|h| h.deep) {
|
||||
return Err(IndexError::Unreadable);
|
||||
}
|
||||
let display = first.shared_attributes.display_window;
|
||||
// Each plane covers the display window: a 60000² window over a 1×1 data window was a
|
||||
// multi-gigabyte allocation per channel (#2141).
|
||||
display.size.0.checked_mul(display.size.1).filter(|n| *n > 0 && *n <= MAX_DISPLAY_PIXELS).ok_or(IndexError::Unreadable)?;
|
||||
// Eight bytes of offset table per chunk: a header can't claim more chunks than that.
|
||||
let chunks: usize = meta.headers.iter().map(|h| h.chunk_count).fold(0, usize::saturating_add);
|
||||
if (chunks as u64).saturating_mul(8) > len.saturating_sub(header_end) {
|
||||
return Err(IndexError::Unreadable);
|
||||
}
|
||||
let tables = {
|
||||
let mut counted = Counted { inner: &mut *src, read: 0 };
|
||||
let tables = MetaData::read_offset_tables(&mut exr::io::PeekRead::new(&mut counted), &meta.headers).map_err(|_| IndexError::Unreadable)?;
|
||||
(tables, counted.read)
|
||||
};
|
||||
let (tables, table_bytes) = tables;
|
||||
let chunks_start = header_end.saturating_add(table_bytes);
|
||||
// Where each chunk ends: at the next chunk's start, or at the end of the file.
|
||||
let mut starts: Vec<u64> = tables.iter().flatten().copied().filter(|o| *o >= chunks_start && *o < len).collect();
|
||||
starts.sort_unstable();
|
||||
starts.dedup();
|
||||
let end_of = |offset: u64| match starts.binary_search(&offset) {
|
||||
Ok(i) => starts.get(i + 1).copied().unwrap_or(len),
|
||||
Err(_) => offset,
|
||||
};
|
||||
let mut parts = Vec::with_capacity(meta.headers.len());
|
||||
for (pi, h) in meta.headers.iter().enumerate() {
|
||||
let table = tables.get(pi).ok_or(IndexError::Unreadable)?;
|
||||
// A chunk is at most its block's bytes plus its own small header (part number, position
|
||||
// and sizes): never read more for one.
|
||||
let most = (h.max_block_byte_size() as u64).saturating_add(64);
|
||||
let mut list = Vec::with_capacity(h.chunk_count);
|
||||
for (i, tile) in h.blocks_increasing_y_order().enumerate() {
|
||||
let bounds = h.get_absolute_block_pixel_coordinates(tile.location).map_err(|_| IndexError::Unreadable)?;
|
||||
let offset = table.get(i).copied().unwrap_or(0);
|
||||
let valid = offset >= chunks_start && offset < len;
|
||||
let bytes = if valid { end_of(offset).saturating_sub(offset).min(most) } else { 0 };
|
||||
list.push(IndexChunk {
|
||||
offset,
|
||||
len: bytes,
|
||||
y: i64::from(bounds.position.1),
|
||||
rows: bounds.size.1,
|
||||
largest: tile.location.is_largest_resolution_level(),
|
||||
});
|
||||
}
|
||||
let pos = h.own_attributes.layer_position;
|
||||
parts.push(IndexPart {
|
||||
offset: (i64::from(pos.0) - i64::from(display.position.0), i64::from(pos.1) - i64::from(display.position.1)),
|
||||
layer: (h.layer_size.0, h.layer_size.1),
|
||||
chunks: list,
|
||||
});
|
||||
}
|
||||
let mut channels = vec![];
|
||||
let mut aliases = vec![];
|
||||
for (pi, h) in meta.headers.iter().enumerate() {
|
||||
let prefix = part_name(h);
|
||||
for (ci, ch) in h.channels.list.iter().enumerate() {
|
||||
let stored = ch.name.to_string();
|
||||
let name = channel_name(prefix.as_deref(), &stored);
|
||||
// A stored `layer.channel` name inside a part named otherwise is also found as stored
|
||||
// (`Crypto_Material00.R` in part `Crypto_Material`); bare names (`R`) are not.
|
||||
if prefix.is_some() && name != stored && stored.contains('.') {
|
||||
aliases.push((stored, name.clone()));
|
||||
}
|
||||
let fill = if is_depth(&name) { BACKGROUND_DEPTH } else { 0.0 };
|
||||
channels.push(IndexChannel { name, part: pi, channel: ci, fill });
|
||||
}
|
||||
}
|
||||
let manifests = manifests(&meta);
|
||||
let rgb = rgb_of(&meta, &channels);
|
||||
let mut names = AuxChannels::new(display.size.0 as u32, display.size.1 as u32, 1.0);
|
||||
names.channels = channels.iter().map(|c| (c.name.clone(), vec![])).collect();
|
||||
names.manifests = manifests;
|
||||
names.aliases = aliases;
|
||||
let picture = picture_names(&names).map(|(rgb, a)| {
|
||||
// Planes are looked up by name ignoring case, so every channel that could answer is read.
|
||||
let ids = channels
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, c)| rgb.iter().chain(&a).any(|k| k.eq_ignore_ascii_case(&c.name)))
|
||||
.map(|(i, _)| i)
|
||||
.collect();
|
||||
(ids, rgb, a)
|
||||
});
|
||||
Ok(ExrIndex { meta, stamp, display, parts, channels, rgb, picture, names: Arc::new(names) })
|
||||
}
|
||||
|
||||
/// The Cryptomatte manifests of the headers (`cryptomatte/<key>/name` and `…/manifest`), sorted.
|
||||
fn manifests(meta: &MetaData) -> Vec<(String, Vec<(String, u32)>)> {
|
||||
let mut crypto: HashMap<String, (Option<String>, Option<String>)> = HashMap::new();
|
||||
let mut collect = |other: &HashMap<Text, AttributeValue>| {
|
||||
for (k, v) in other {
|
||||
let key = k.to_string();
|
||||
let Some(rest) = key.strip_prefix("cryptomatte/") else { continue };
|
||||
let Some((id, field)) = rest.split_once('/') else { continue };
|
||||
let AttributeValue::Text(t) = v else { continue };
|
||||
let e = crypto.entry(id.to_string()).or_default();
|
||||
match field {
|
||||
"name" => e.0 = Some(t.to_string()),
|
||||
"manifest" => e.1 = Some(t.to_string()),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
};
|
||||
// (the image's attributes are the first part's shared ones, as the decoder reports them)
|
||||
if let Some(first) = meta.headers.first() {
|
||||
collect(&first.shared_attributes.other);
|
||||
}
|
||||
for h in meta.headers.iter() {
|
||||
collect(&h.own_attributes.other);
|
||||
}
|
||||
let mut out: Vec<(String, Vec<(String, u32)>)> =
|
||||
crypto.into_values().filter_map(|(name, manifest)| Some((name?, parse_manifest(&manifest?)))).collect();
|
||||
out.sort();
|
||||
out
|
||||
}
|
||||
|
||||
/// How the `image` crate's decoder would show the file: the first part with exact `R`, `G` and
|
||||
/// `B` channels (alpha `A` when it has one), unless its planes can't stand in for it (a
|
||||
/// depth-named channel, filled with the background depth outside the data window, or a part
|
||||
/// with its own display window).
|
||||
fn rgb_of(meta: &MetaData, channels: &[IndexChannel]) -> Rgb {
|
||||
let Some(display) = meta.headers.first().map(|h| h.shared_attributes.display_window) else { return Rgb::None };
|
||||
for (pi, h) in meta.headers.iter().enumerate() {
|
||||
let find = |c: &str| h.channels.find_index_of_channel(&Text::from(c));
|
||||
let (Some(r), Some(g), Some(b)) = (find("R"), find("G"), find("B")) else { continue };
|
||||
let id = |ci: usize| channels.iter().position(|c| c.part == pi && c.channel == ci);
|
||||
let picked = [Some(r), Some(g), Some(b), find("A")];
|
||||
let ids: Vec<Option<usize>> = picked.iter().map(|c| c.and_then(id)).collect();
|
||||
let (Some(Some(r)), Some(Some(g)), Some(Some(b))) = (ids.first(), ids.get(1), ids.get(2)) else { return Rgb::Unsupported };
|
||||
let a = ids.get(3).copied().flatten();
|
||||
let depth = [Some(*r), Some(*g), Some(*b), a].iter().flatten().any(|i| channels.get(*i).is_some_and(|c| is_depth(&c.name)));
|
||||
if depth || h.shared_attributes.display_window != display {
|
||||
return Rgb::Unsupported;
|
||||
}
|
||||
return Rgb::Planes([*r, *g, *b], a);
|
||||
}
|
||||
Rgb::None
|
||||
}
|
||||
|
||||
/// At most this many files are indexed (a few kilobytes each); the oldest goes first.
|
||||
const INDEXED_FILES: usize = 4096;
|
||||
|
||||
/// Indexed files by path.
|
||||
struct Indexes {
|
||||
map: HashMap<String, Arc<ExrIndex>>,
|
||||
order: VecDeque<String>,
|
||||
}
|
||||
|
||||
fn indexes() -> &'static Mutex<Indexes> {
|
||||
static INDEXES: std::sync::OnceLock<Mutex<Indexes>> = std::sync::OnceLock::new();
|
||||
INDEXES.get_or_init(|| Mutex::new(Indexes { map: HashMap::new(), order: VecDeque::new() }))
|
||||
}
|
||||
|
||||
fn lock() -> std::sync::MutexGuard<'static, Indexes> {
|
||||
indexes().lock().unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
|
||||
/// Keep `index` as the index of `path`.
|
||||
pub(crate) fn keep(path: &str, index: Arc<ExrIndex>) {
|
||||
let mut ix = lock();
|
||||
if ix.map.insert(path.to_string(), index).is_none() {
|
||||
ix.order.push_back(path.to_string());
|
||||
}
|
||||
while ix.map.len() > INDEXED_FILES {
|
||||
let Some(old) = ix.order.pop_front() else { break };
|
||||
ix.map.remove(&old);
|
||||
}
|
||||
}
|
||||
|
||||
/// The index of the file at `path` (read from `source` the first time; kept until
|
||||
/// [`forget`]). Header-only users take it as it is; a read checks the file's identity and
|
||||
/// indexes it again when it changed.
|
||||
pub(crate) fn get(path: &str, source: &ExrSource) -> Result<Arc<ExrIndex>, IndexError> {
|
||||
if let Some(ix) = lock().map.get(path).cloned() {
|
||||
return Ok(ix);
|
||||
}
|
||||
let mut opened = source.open().map_err(|e| {
|
||||
log::debug!("media: {path}: {e}");
|
||||
IndexError::Io
|
||||
})?;
|
||||
let ix = Arc::new(build(&mut *opened.reader, opened.len, opened.stamp)?);
|
||||
keep(path, ix.clone());
|
||||
Ok(ix)
|
||||
}
|
||||
|
||||
/// The index of `path` if it is kept.
|
||||
#[cfg_attr(not(test), allow(dead_code))]
|
||||
pub(crate) fn cached(path: &str) -> Option<Arc<ExrIndex>> {
|
||||
lock().map.get(path).cloned()
|
||||
}
|
||||
|
||||
/// Drop the index of `path` (the file changed on disk).
|
||||
pub(crate) fn forget(path: &str) {
|
||||
let mut ix = lock();
|
||||
if ix.map.remove(path).is_some() {
|
||||
ix.order.retain(|p| p != path);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,701 @@
|
||||
//! The plane store (#3271): decoded OpenEXR channel planes in RAM, one entry per (file, level),
|
||||
//! 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.
|
||||
//!
|
||||
//! - **Single flight.** One decode per entry at a time; requests for it wait instead of
|
||||
//! decoding again. A request arriving before the decode has started reading adds its
|
||||
//! channels to it; one arriving later waits, and if that decode doesn't cover it, the next
|
||||
//! decode reads only the channels still missing.
|
||||
//! - **Covered requests are served from the store**: an entry holds one set of planes (the
|
||||
//! union of what was decoded, [`Batch`]), and any request for a subset gets it as it is.
|
||||
//! - **Never on the wrong thread.** The decode runs on the footage decode threads
|
||||
//! ([`crate::pool::decode_threads`]); a request on another thread hands it over and blocks
|
||||
//! plainly. A thread running a decode never waits for another (rayon lets a thread waiting in
|
||||
//! a parallel loop run other jobs, which could wait on that very decode): it decodes a copy.
|
||||
//! - **Nothing made before a Reload is kept or served** ([`forget`]): a decode that started
|
||||
//! before the file was forgotten keeps nothing, and the requests waiting for it ask again.
|
||||
//! - Bounded by bytes ([`CACHE_BUDGET`]), least recently used out first; entries in use (planes
|
||||
//! an effect holds, a decode in progress, the last pictures' planes kept for their effects,
|
||||
//! [`HELD_FILES`]) are never dropped, and the newest stays even when it alone is over.
|
||||
|
||||
use std::collections::{BTreeSet, VecDeque};
|
||||
use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};
|
||||
|
||||
use effectcraft_raster::channels3d::AuxNeed;
|
||||
use effectcraft_raster::{AuxChannels, Image};
|
||||
use effectcraft_render::reads::ReadFile;
|
||||
|
||||
use crate::convert::AlphaOp;
|
||||
use crate::exr_channels::{Rgb, planes_picture};
|
||||
use crate::exr_index::{self, ExrIndex, IndexError};
|
||||
use crate::exr_channels::CACHE_BUDGET;
|
||||
use crate::exr_read::{ExrSource, ReadError, ReadRequest, read_planes};
|
||||
|
||||
/// At most this many entries are kept (unreadable files hold no bytes).
|
||||
const CACHE_FILES: usize = 64;
|
||||
|
||||
/// At most this many pictures' planes are kept for their effects (the renderer asks for a
|
||||
/// frame's picture, then its channels): about as many frames as are rendered at once.
|
||||
const HELD_FILES: usize = 4;
|
||||
|
||||
/// How long a request waits for another's decode before it decodes a copy (a safety net for a
|
||||
/// decode that hangs).
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
const WAIT_LIMIT: std::time::Duration = std::time::Duration::from_secs(10);
|
||||
|
||||
/// How often a request asks again after a decode it waited for was called off or forgotten.
|
||||
const RETRIES: u32 = 3;
|
||||
|
||||
/// Decoded planes of a file: the union of the channels read so far.
|
||||
pub(crate) struct Batch {
|
||||
/// The index they were read with (channel ids are its).
|
||||
pub(crate) index: Arc<ExrIndex>,
|
||||
/// The channels held, in file order (parallel to `aux.channels`).
|
||||
pub(crate) ids: Vec<usize>,
|
||||
/// Every channel of the file.
|
||||
pub(crate) all: bool,
|
||||
pub(crate) aux: Arc<AuxChannels>,
|
||||
/// The file as read.
|
||||
pub(crate) file: ReadFile,
|
||||
}
|
||||
|
||||
impl Batch {
|
||||
/// Bytes held (the planes; names and manifests are small).
|
||||
fn bytes(&self) -> usize {
|
||||
self.aux.channels.iter().map(|(n, p)| n.len() + std::mem::size_of_val(p.as_slice())).sum()
|
||||
}
|
||||
|
||||
/// The plane of channel `id`.
|
||||
pub(crate) fn plane(&self, id: usize) -> Option<&[f32]> {
|
||||
let at = self.ids.binary_search(&id).ok()?;
|
||||
self.aux.channels.get(at).map(|(_, p)| p.as_slice())
|
||||
}
|
||||
|
||||
/// It holds what `want` asks of the file `index` describes.
|
||||
fn covers(&self, index: &Arc<ExrIndex>, want: &Want) -> bool {
|
||||
Arc::ptr_eq(&self.index, index) && (self.all || (!want.all && want.ids.iter().all(|id| self.ids.binary_search(id).is_ok())))
|
||||
}
|
||||
}
|
||||
|
||||
/// The channels a request asks for.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub(crate) struct Want {
|
||||
/// Every channel.
|
||||
pub(crate) all: bool,
|
||||
pub(crate) ids: BTreeSet<usize>,
|
||||
}
|
||||
|
||||
impl Want {
|
||||
fn add(&mut self, other: &Want) {
|
||||
self.all |= other.all;
|
||||
self.ids.extend(other.ids.iter().copied());
|
||||
}
|
||||
}
|
||||
|
||||
/// Who asks.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub(crate) enum Use {
|
||||
/// The picture of a footage item whose effects read its channels next: kept for them.
|
||||
Picture,
|
||||
/// Anything else (the channels themselves, a picture alone).
|
||||
Other,
|
||||
}
|
||||
|
||||
/// Why a request got no planes.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) enum Refused {
|
||||
/// Not readable (remembered until the file is forgotten).
|
||||
Unreadable,
|
||||
/// More planes than one file may allocate (the reason; remembered).
|
||||
TooLarge(String),
|
||||
/// The request was called off.
|
||||
Cancelled,
|
||||
/// The file changed since it was indexed: index it again and ask again.
|
||||
Changed,
|
||||
/// The file was forgotten while it decoded: ask again.
|
||||
Forgotten,
|
||||
}
|
||||
|
||||
/// A decode in progress: what it reads (until it starts reading) and its outcome.
|
||||
struct Flight {
|
||||
state: Mutex<FlightState>,
|
||||
ready: Condvar,
|
||||
}
|
||||
|
||||
struct FlightState {
|
||||
index: Arc<ExrIndex>,
|
||||
want: Want,
|
||||
/// Reading started: requests no longer add to `want`.
|
||||
sealed: bool,
|
||||
outcome: Option<Result<Arc<Batch>, Refused>>,
|
||||
}
|
||||
|
||||
impl Flight {
|
||||
fn new(index: Arc<ExrIndex>, want: Want) -> Arc<Flight> {
|
||||
Arc::new(Flight { state: Mutex::new(FlightState { index, want, sealed: false, outcome: None }), ready: Condvar::new() })
|
||||
}
|
||||
|
||||
fn state(&self) -> MutexGuard<'_, FlightState> {
|
||||
self.state.lock().unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
|
||||
fn end(&self, outcome: Result<Arc<Batch>, Refused>) {
|
||||
self.state().outcome = Some(outcome);
|
||||
self.ready.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
struct Entry {
|
||||
path: String,
|
||||
level: u32,
|
||||
batch: Option<Arc<Batch>>,
|
||||
flight: Option<Arc<Flight>>,
|
||||
failed: Option<Refused>,
|
||||
bytes: usize,
|
||||
/// When it was last asked for or finished decoding (a counter).
|
||||
used: u64,
|
||||
}
|
||||
|
||||
impl Entry {
|
||||
/// Dropping it would free nothing or lose work in progress: a decode runs, or planes are held.
|
||||
fn in_use(&self) -> bool {
|
||||
self.flight.is_some() || self.batch.as_ref().is_some_and(|b| Arc::strong_count(b) > 1 || Arc::strong_count(&b.aux) > 1)
|
||||
}
|
||||
}
|
||||
|
||||
struct State {
|
||||
entries: Vec<Entry>,
|
||||
clock: u64,
|
||||
/// Pictures' planes kept for their effects, oldest first (at most [`HELD_FILES`]).
|
||||
held: VecDeque<(String, Arc<Batch>)>,
|
||||
}
|
||||
|
||||
/// The request decoding an entry: it stores the outcome and ends the flight, also when the decode
|
||||
/// panics (then with an error), so no request waits for planes that never come and the entry
|
||||
/// doesn't stay "decoding".
|
||||
struct Owning<'a> {
|
||||
store: &'a PlaneStore,
|
||||
path: &'a str,
|
||||
level: u32,
|
||||
flight: &'a Arc<Flight>,
|
||||
ended: bool,
|
||||
}
|
||||
|
||||
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);
|
||||
self.flight.end(outcome.clone());
|
||||
outcome
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Owning<'_> {
|
||||
fn drop(&mut self) {
|
||||
if !self.ended {
|
||||
let _ = self.end(Err(Refused::Unreadable));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Decoded planes by (file, level), bounded by bytes.
|
||||
pub(crate) struct PlaneStore {
|
||||
state: Mutex<State>,
|
||||
budget: usize,
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
/// Store decodes running on this thread: such a thread doesn't wait for another decode.
|
||||
static DECODING: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// Marks a store decode on this thread ([`DECODING`]) until dropped (also when it panics).
|
||||
struct Decoding;
|
||||
|
||||
impl Decoding {
|
||||
fn new() -> Decoding {
|
||||
DECODING.with(|n| n.set(n.get().saturating_add(1)));
|
||||
Decoding
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Decoding {
|
||||
fn drop(&mut self) {
|
||||
DECODING.with(|n| n.set(n.get().saturating_sub(1)));
|
||||
}
|
||||
}
|
||||
|
||||
/// What waiting for another request's decode ended with.
|
||||
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
|
||||
enum Waited {
|
||||
Got(Result<Arc<Batch>, Refused>),
|
||||
/// This request was called off meanwhile.
|
||||
Cancelled,
|
||||
/// This thread may not wait, or waited too long: decode a copy.
|
||||
No,
|
||||
}
|
||||
|
||||
/// One decode the store ran (tests and measurements): the channels read and what it cost.
|
||||
#[cfg(test)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct DecodeRecord {
|
||||
pub(crate) channels: Vec<String>,
|
||||
pub(crate) stats: crate::exr_read::ReadStats,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
static DECODES: Mutex<Vec<(String, DecodeRecord)>> = Mutex::new(Vec::new());
|
||||
|
||||
/// The decodes of `path` so far (tests; paths are unique per test).
|
||||
#[cfg(test)]
|
||||
pub(crate) fn decodes_of(path: &str) -> Vec<DecodeRecord> {
|
||||
DECODES.lock().unwrap_or_else(PoisonError::into_inner).iter().filter(|(p, _)| p == path).map(|(_, d)| d.clone()).collect()
|
||||
}
|
||||
|
||||
/// Runs before a decode reads its chunks, with the file's path (tests).
|
||||
#[cfg(test)]
|
||||
type Hook = Arc<dyn Fn(&str) + Send + Sync>;
|
||||
#[cfg(test)]
|
||||
static BEFORE_READ: Mutex<Option<Hook>> = Mutex::new(None);
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn set_before_read(hook: Option<Hook>) {
|
||||
*BEFORE_READ.lock().unwrap_or_else(PoisonError::into_inner) = hook;
|
||||
}
|
||||
|
||||
impl PlaneStore {
|
||||
pub(crate) const fn new(budget: usize) -> PlaneStore {
|
||||
PlaneStore { state: Mutex::new(State { entries: Vec::new(), clock: 0, held: VecDeque::new() }), budget }
|
||||
}
|
||||
|
||||
fn lock(&self) -> MutexGuard<'_, State> {
|
||||
self.state.lock().unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
|
||||
/// The planes of `want` of the file at `path` (`index`, read from `source`): from the store,
|
||||
/// from a decode in progress, or decoded now (only what the store doesn't hold yet). The
|
||||
/// file it was read from is noted ([`crate::reads`]).
|
||||
pub(crate) fn get(&self, path: &str, source: &ExrSource, index: &Arc<ExrIndex>, want: &Want, by: Use) -> Result<Arc<Batch>, Refused> {
|
||||
let level = 0;
|
||||
let mut retries = 0;
|
||||
loop {
|
||||
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) {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
st.entries.push(Entry { path: path.to_string(), level, batch: None, flight: None, failed: None, bytes: 0, used: clock });
|
||||
st.entries.len() - 1
|
||||
}
|
||||
};
|
||||
let Some(e) = st.entries.get_mut(at) else { return Err(Refused::Unreadable) };
|
||||
e.used = clock;
|
||||
if let Some(f) = &e.failed {
|
||||
return Err(f.clone());
|
||||
}
|
||||
if let Some(b) = e.batch.clone().filter(|b| b.covers(index, want)) {
|
||||
Self::hold(&mut st, path, &b, by);
|
||||
drop(st);
|
||||
crate::reads::note(b.file.clone());
|
||||
return Ok(b);
|
||||
}
|
||||
if let Some(flight) = e.flight.clone() {
|
||||
{
|
||||
let mut fs = flight.state();
|
||||
if !fs.sealed && Arc::ptr_eq(&fs.index, index) {
|
||||
fs.want.add(want);
|
||||
}
|
||||
}
|
||||
drop(st);
|
||||
match self.wait(&flight) {
|
||||
Waited::Got(Ok(b)) if b.covers(index, want) => {
|
||||
crate::reads::note(b.file.clone());
|
||||
return Ok(b);
|
||||
}
|
||||
// Not all it asks for: ask again (the next decode reads what is missing).
|
||||
Waited::Got(Ok(_)) => {}
|
||||
Waited::Got(Err(Refused::Cancelled | Refused::Forgotten)) if retries < RETRIES => retries += 1,
|
||||
Waited::Got(Err(e)) => return Err(e),
|
||||
Waited::Cancelled => return Err(Refused::Cancelled),
|
||||
// A copy, kept nowhere.
|
||||
Waited::No => return run(path, source, &Flight::new(index.clone(), want.clone()), None),
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// This request decodes: what it asks for, minus what the store holds.
|
||||
let base = e.batch.clone().filter(|b| Arc::ptr_eq(&b.index, index));
|
||||
let flight = Flight::new(index.clone(), want.clone());
|
||||
e.flight = Some(flight.clone());
|
||||
drop(st);
|
||||
#[cfg(test)]
|
||||
crate::exr_channels::count_decode(true);
|
||||
let mut owning = Owning { store: self, path, level, flight: &flight, ended: false };
|
||||
let outcome = run(path, source, &flight, base);
|
||||
let outcome = owning.end(outcome);
|
||||
match outcome {
|
||||
Ok(b) if b.covers(index, want) => {
|
||||
let mut st = self.lock();
|
||||
Self::hold(&mut st, path, &b, by);
|
||||
drop(st);
|
||||
crate::reads::note(b.file.clone());
|
||||
return Ok(b);
|
||||
}
|
||||
Ok(_) | Err(Refused::Forgotten) if retries < RETRIES => retries += 1,
|
||||
Ok(_) | Err(Refused::Forgotten) => return Err(Refused::Cancelled),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep `b` for the effects of the picture asking (`Use::Picture`), or stop keeping the
|
||||
/// file's planes once its channels are asked for.
|
||||
fn hold(st: &mut State, path: &str, b: &Arc<Batch>, by: Use) {
|
||||
match by {
|
||||
Use::Picture => {
|
||||
if !st.held.iter().any(|(p, _)| p == path) {
|
||||
st.held.push_back((path.to_string(), b.clone()));
|
||||
if st.held.len() > HELD_FILES {
|
||||
st.held.pop_front();
|
||||
}
|
||||
}
|
||||
}
|
||||
Use::Other => st.held.retain(|(p, _)| p != path),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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> {
|
||||
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 {
|
||||
return match outcome {
|
||||
Err(Refused::Cancelled) => Err(Refused::Cancelled),
|
||||
_ => Err(Refused::Forgotten),
|
||||
};
|
||||
};
|
||||
e.flight = None;
|
||||
e.used = clock;
|
||||
match &outcome {
|
||||
Ok(b) => {
|
||||
e.bytes = b.bytes();
|
||||
e.batch = Some(b.clone());
|
||||
}
|
||||
Err(Refused::Unreadable) => e.failed = Some(Refused::Unreadable),
|
||||
Err(Refused::TooLarge(why)) => e.failed = Some(Refused::TooLarge(why.clone())),
|
||||
// (the file changed: what is held is of the old one)
|
||||
Err(Refused::Changed) => {
|
||||
e.batch = None;
|
||||
e.bytes = 0;
|
||||
}
|
||||
Err(Refused::Cancelled | Refused::Forgotten) => {}
|
||||
}
|
||||
Self::evict(&mut st, self.budget, path, level);
|
||||
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) {
|
||||
loop {
|
||||
if st.entries.iter().map(|e| e.bytes).fold(0usize, usize::saturating_add) <= budget && st.entries.len() <= CACHE_FILES {
|
||||
break;
|
||||
}
|
||||
let held = &st.held;
|
||||
let oldest = st
|
||||
.entries
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, e)| !(e.path == path && e.level == level) && !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);
|
||||
}
|
||||
}
|
||||
|
||||
/// Wait for the outcome of another request's decode (`flight`). A thread running a store
|
||||
/// decode doesn't wait (see the module notes); the time limit is a safety net.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn wait(&self, flight: &Flight) -> Waited {
|
||||
if DECODING.with(std::cell::Cell::get) > 0 {
|
||||
return Waited::No;
|
||||
}
|
||||
let until = std::time::Instant::now() + WAIT_LIMIT;
|
||||
let mut fs = flight.state();
|
||||
loop {
|
||||
if let Some(o) = fs.outcome.as_ref() {
|
||||
return Waited::Got(o.clone());
|
||||
}
|
||||
if filmcraft_media::cancel::cancelled() {
|
||||
return Waited::Cancelled;
|
||||
}
|
||||
let left = until.saturating_duration_since(std::time::Instant::now());
|
||||
if left.is_zero() {
|
||||
log::warn!("media: OpenEXR planes took over {} s to decode on another thread; decoding a copy", WAIT_LIMIT.as_secs());
|
||||
return Waited::No;
|
||||
}
|
||||
fs = match flight.ready.wait_timeout(fs, left.min(std::time::Duration::from_millis(20))) {
|
||||
Ok((g, _)) => g,
|
||||
Err(p) => p.into_inner().0,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// (A wasm32 build has one thread: a decode in flight is further down this thread's stack.)
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn wait(&self, _flight: &Flight) -> Waited {
|
||||
Waited::No
|
||||
}
|
||||
|
||||
/// Drop the planes of `path` (the file changed on disk); a decode of it in progress keeps
|
||||
/// nothing.
|
||||
pub(crate) fn forget(&self, path: &str) {
|
||||
let mut st = self.lock();
|
||||
st.entries.retain(|e| e.path != path);
|
||||
st.held.retain(|(p, _)| p != path);
|
||||
}
|
||||
|
||||
/// Stop keeping the last pictures' planes for their effects.
|
||||
pub(crate) fn release_held(&self) {
|
||||
self.lock().held.clear();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn cached(&self) -> Vec<String> {
|
||||
self.lock().entries.iter().filter(|e| e.batch.is_some() || e.failed.is_some()).map(|e| e.path.clone()).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn is_held(&self, path: &str) -> bool {
|
||||
self.lock().held.iter().any(|(p, _)| p == path)
|
||||
}
|
||||
}
|
||||
|
||||
/// Run `flight`'s decode on the footage decode threads (here when this is one of them, or
|
||||
/// there are none) and block until it is done.
|
||||
fn run(path: &str, source: &ExrSource, flight: &Arc<Flight>, base: Option<Arc<Batch>>) -> Result<Arc<Batch>, Refused> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
if let Some(threads) = crate::pool::decode_threads()
|
||||
&& threads.current_thread_index().is_none()
|
||||
{
|
||||
let (tx, rx) = std::sync::mpsc::sync_channel(1);
|
||||
let (path, source, flight) = (path.to_string(), source.clone(), flight.clone());
|
||||
// The request's cancel flag and decoding hints go with it.
|
||||
let hints = filmcraft_media::cancel::Context::current();
|
||||
threads.spawn(move || {
|
||||
let _ = tx.send(hints.run(|| decode(&path, &source, &flight, base)));
|
||||
});
|
||||
// A plain block: this thread runs nothing else meanwhile.
|
||||
return rx.recv().unwrap_or(Err(Refused::Unreadable));
|
||||
}
|
||||
decode(path, source, flight, base)
|
||||
}
|
||||
|
||||
/// Decode what `flight` asks for and `base` doesn't hold, and make the union of both.
|
||||
fn decode(path: &str, source: &ExrSource, flight: &Arc<Flight>, base: Option<Arc<Batch>>) -> Result<Arc<Batch>, Refused> {
|
||||
let _decoding = Decoding::new();
|
||||
let mut opened = source.open().map_err(|e| {
|
||||
log::warn!("media: {path}: {e}");
|
||||
Refused::Unreadable
|
||||
})?;
|
||||
let index = flight.state().index.clone();
|
||||
if opened.stamp != index.stamp {
|
||||
// The file changed since it was indexed.
|
||||
let fresh = exr_index::build(&mut *opened.reader, opened.len, opened.stamp).map_err(|_| Refused::Unreadable)?;
|
||||
exr_index::keep(path, Arc::new(fresh));
|
||||
return Err(Refused::Changed);
|
||||
}
|
||||
#[cfg(test)]
|
||||
{
|
||||
let hook = BEFORE_READ.lock().unwrap_or_else(PoisonError::into_inner).clone();
|
||||
if let Some(h) = hook {
|
||||
h(path);
|
||||
}
|
||||
}
|
||||
// From here on the request is fixed.
|
||||
let want = {
|
||||
let mut fs = flight.state();
|
||||
fs.sealed = true;
|
||||
fs.want.clone()
|
||||
};
|
||||
let wanted: Vec<usize> = if want.all { (0..index.channels.len()).collect() } else { want.ids.iter().copied().filter(|i| *i < index.channels.len()).collect() };
|
||||
let base = base.filter(|b| Arc::ptr_eq(&b.index, &index));
|
||||
let missing: Vec<usize> = wanted.iter().copied().filter(|id| !base.as_ref().is_some_and(|b| b.ids.binary_search(id).is_ok())).collect();
|
||||
let (planes, stats) = read_planes(&mut *opened.reader, &index, &ReadRequest::full(missing.clone()));
|
||||
#[cfg(test)]
|
||||
DECODES.lock().unwrap_or_else(PoisonError::into_inner).push((
|
||||
path.to_string(),
|
||||
DecodeRecord { channels: missing.iter().filter_map(|i| index.channels.get(*i)).map(|c| c.name.clone()).collect(), stats: stats.clone() },
|
||||
));
|
||||
#[cfg(not(test))]
|
||||
let _ = &stats;
|
||||
let planes = planes.map_err(|e| match e {
|
||||
ReadError::Cancelled => Refused::Cancelled,
|
||||
ReadError::TooLarge(why) => {
|
||||
log::warn!("media: {path}: {why}");
|
||||
Refused::TooLarge(why)
|
||||
}
|
||||
ReadError::Unreadable(why) => {
|
||||
log::warn!("media: {path}: {why}");
|
||||
Refused::Unreadable
|
||||
}
|
||||
})?;
|
||||
// The union, in file order: the planes held and the ones just read.
|
||||
let mut new: Vec<(usize, Vec<f32>)> = missing.into_iter().zip(planes).collect();
|
||||
if let Some(b) = &base {
|
||||
for (at, id) in b.ids.iter().enumerate() {
|
||||
if let Some((_, p)) = b.aux.channels.get(at) {
|
||||
new.push((*id, p.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
new.sort_by_key(|(id, _)| *id);
|
||||
new.dedup_by_key(|(id, _)| *id);
|
||||
let all = new.len() == index.channels.len();
|
||||
let (w, h) = index.size();
|
||||
let mut aux = AuxChannels::new(w as u32, h as u32, 1.0);
|
||||
aux.manifests = index.names.manifests.clone();
|
||||
aux.aliases = index.names.aliases.clone();
|
||||
if !all {
|
||||
aux.listed = index.channels.iter().map(|c| c.name.clone()).collect();
|
||||
}
|
||||
let ids: Vec<usize> = new.iter().map(|(id, _)| *id).collect();
|
||||
aux.channels = new.into_iter().filter_map(|(id, p)| Some((index.channels.get(id)?.name.clone(), p))).collect();
|
||||
Ok(Arc::new(Batch { index, ids, all, aux: Arc::new(aux), file: ReadFile::new(path, opened.stamp) }))
|
||||
}
|
||||
|
||||
fn store() -> &'static PlaneStore {
|
||||
static STORE: PlaneStore = PlaneStore::new(CACHE_BUDGET);
|
||||
&STORE
|
||||
}
|
||||
|
||||
/// Is `path` an OpenEXR file by name (the store reads those; other files, and files named so
|
||||
/// that aren't OpenEXR, go the `image` crate's way).
|
||||
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>, IndexError> {
|
||||
exr_index::get(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)?;
|
||||
Some(index.ids_named(names.iter().map(String::as_str)).into_iter().collect())
|
||||
}
|
||||
|
||||
/// The outcome of [`picture`].
|
||||
pub(crate) enum Picture {
|
||||
Done(Result<Image, String>),
|
||||
Cancelled,
|
||||
/// Not for the planes (an `image` crate picture, or not an OpenEXR this reader takes):
|
||||
/// decode the whole file with the `image` crate.
|
||||
Legacy,
|
||||
}
|
||||
|
||||
/// The picture of the OpenEXR at `path` made of its planes: the channels the `image` crate's
|
||||
/// decoder would show, or a multi-layer file's colour layer (see
|
||||
/// [`crate::exr_channels::layered_image`]). With `extra` (an effect reads the footage's
|
||||
/// channels) those channels are read in the same pass and kept for the effects.
|
||||
pub(crate) fn picture(path: &str, source: &ExrSource, op: AlphaOp, extra: Option<&AuxNeed>) -> Picture {
|
||||
for _ in 0..2 {
|
||||
let index = match index_of(path, source) {
|
||||
Ok(ix) => ix,
|
||||
Err(IndexError::Unsupported(why)) => return Picture::Done(Err(why.to_string())),
|
||||
Err(_) => return Picture::Legacy,
|
||||
};
|
||||
let Some(ids) = index.picture_ids() else { return Picture::Legacy };
|
||||
let mut want = Want { all: false, ids: ids.into_iter().collect() };
|
||||
if let Some(need) = extra {
|
||||
match need_ids(&index, need) {
|
||||
Some(more) => want.ids.extend(more),
|
||||
None => want.all = true,
|
||||
}
|
||||
}
|
||||
let by = if extra.is_some() { Use::Picture } else { Use::Other };
|
||||
let batch = match store().get(path, source, &index, &want, by) {
|
||||
Ok(b) => b,
|
||||
Err(Refused::Changed) => continue,
|
||||
Err(Refused::Cancelled | Refused::Forgotten) => return Picture::Cancelled,
|
||||
Err(Refused::TooLarge(why)) => return Picture::Done(Err(why)),
|
||||
Err(Refused::Unreadable) => return Picture::Legacy,
|
||||
};
|
||||
let aux = &batch.aux;
|
||||
let img = match index.rgb {
|
||||
Rgb::Planes([r, g, b], a) => {
|
||||
let a = match a {
|
||||
Some(a) => batch.plane(a).map(Some),
|
||||
None => Some(None),
|
||||
};
|
||||
match (batch.plane(r), batch.plane(g), batch.plane(b), a) {
|
||||
(Some(r), Some(g), Some(b), Some(a)) => planes_picture(aux, [r, g, b], a, op),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
Rgb::None => index.picture.as_ref().and_then(|(_, rgb, a)| {
|
||||
let a = match a {
|
||||
Some(n) => Some(aux.get(n)?),
|
||||
None => None,
|
||||
};
|
||||
planes_picture(aux, [aux.get(&rgb[0])?, aux.get(&rgb[1])?, aux.get(&rgb[2])?], a, op)
|
||||
}),
|
||||
Rgb::Unsupported => None,
|
||||
};
|
||||
return match img {
|
||||
Some(img) => Picture::Done(Ok(img)),
|
||||
None => Picture::Legacy,
|
||||
};
|
||||
}
|
||||
Picture::Legacy
|
||||
}
|
||||
|
||||
/// The channels `need` reads of the OpenEXR at `path` (all for [`AuxNeed::All`]), from the
|
||||
/// store. `None` when it isn't a readable OpenEXR (or the request was called off).
|
||||
pub(crate) fn aux(path: &str, source: &ExrSource, need: &AuxNeed) -> Option<Arc<AuxChannels>> {
|
||||
for _ in 0..2 {
|
||||
let index = index_of(path, source).ok()?;
|
||||
let want = match need_ids(&index, need) {
|
||||
Some(ids) => Want { all: false, ids },
|
||||
None => Want { all: true, ids: BTreeSet::new() },
|
||||
};
|
||||
match store().get(path, source, &index, &want, Use::Other) {
|
||||
Ok(b) => return Some(b.aux.clone()),
|
||||
Err(Refused::Changed) => continue,
|
||||
Err(_) => return None,
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// The channel names (planes empty), manifests and aliases of the OpenEXR at `path`, from its
|
||||
/// headers alone. Never handed to an effect.
|
||||
pub(crate) fn names(path: &str, source: &ExrSource) -> Option<Arc<AuxChannels>> {
|
||||
index_of(path, source).ok().map(|ix| ix.names.clone())
|
||||
}
|
||||
|
||||
/// Drop what is kept of `path`: its index and planes.
|
||||
pub(crate) fn forget(path: &str) {
|
||||
exr_index::forget(path);
|
||||
store().forget(path);
|
||||
}
|
||||
|
||||
/// Stop keeping the last pictures' planes for their effects (which footage effects read changed).
|
||||
pub(crate) fn release_held() {
|
||||
store().release_held();
|
||||
}
|
||||
|
||||
/// The store all footage shares (tests).
|
||||
#[cfg(test)]
|
||||
pub(crate) fn shared() -> &'static PlaneStore {
|
||||
store()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,439 @@
|
||||
//! The OpenEXR index, reader and plane store (#3271) on synthetic files written here.
|
||||
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use effectcraft_project::{Footage, ItemId};
|
||||
use effectcraft_raster::channels3d::{AuxLookup, AuxNeed};
|
||||
use effectcraft_render::reads::FileStamp;
|
||||
use effectcraft_render::{FloatCurve, FootageSource};
|
||||
use effectcraft_time::Tick;
|
||||
use exr::prelude::{AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, WritableImage, f16};
|
||||
|
||||
use super::*;
|
||||
use crate::exr_read::ExrSource;
|
||||
|
||||
/// A deterministic sample for pixel `i` of channel `c` (an `A` channel gets 0..=1).
|
||||
fn value(i: usize, c: usize, alpha: bool) -> f32 {
|
||||
if alpha { ((i * 3 + c) % 11) as f32 / 10.0 } else { ((i * 7 + c * 29) % 97) as f32 / 40.0 - 0.3 }
|
||||
}
|
||||
|
||||
/// A part named `name` (`None`: unnamed) of `channels` ((name, half)) over `size`, compressed
|
||||
/// with `compression` in scan lines.
|
||||
fn part(name: Option<&str>, channels: &[(&str, bool)], size: (usize, usize), compression: Compression) -> Layer<AnyChannels<FlatSamples>> {
|
||||
let n = size.0 * size.1;
|
||||
let list: Vec<AnyChannel<FlatSamples>> = channels
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(c, (ch, half))| {
|
||||
let alpha = ch.rsplit('.').next().is_some_and(|l| l == "A");
|
||||
let samples = if *half {
|
||||
FlatSamples::F16((0..n).map(|i| f16::from_f32(value(i, c, alpha))).collect())
|
||||
} else {
|
||||
FlatSamples::F32((0..n).map(|i| value(i, c, alpha)).collect())
|
||||
};
|
||||
AnyChannel::new(*ch, samples)
|
||||
})
|
||||
.collect();
|
||||
let mut attrs = name.map_or_else(LayerAttributes::default, LayerAttributes::named);
|
||||
if name == Some("Crypto_Material") {
|
||||
attrs.other.insert(exr::prelude::Text::from("cryptomatte/abc1234/name"), exr::prelude::AttributeValue::Text(exr::prelude::Text::from("Crypto_Material")));
|
||||
attrs.other.insert(
|
||||
exr::prelude::Text::from("cryptomatte/abc1234/manifest"),
|
||||
exr::prelude::AttributeValue::Text(exr::prelude::Text::from(r#"{"bunny":"13851a76"}"#)),
|
||||
);
|
||||
}
|
||||
let encoding = Encoding { compression, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing };
|
||||
Layer::new(size, attrs, encoding, AnyChannels::sort(list.into()))
|
||||
}
|
||||
|
||||
/// The bytes of a file of `parts` over a display window of `size`.
|
||||
fn write(parts: Vec<Layer<AnyChannels<FlatSamples>>>, size: (usize, usize)) -> Vec<u8> {
|
||||
let image = exr::prelude::Image::from_layers(ImageAttributes::new(IntegerBounds::from_dimensions(size)), parts);
|
||||
let mut bytes = std::io::Cursor::new(Vec::new());
|
||||
image.write().non_parallel().to_buffered(&mut bytes).unwrap();
|
||||
bytes.into_inner()
|
||||
}
|
||||
|
||||
const SIZE: (usize, usize) = (64, 64);
|
||||
const RGBA: [(&str, bool); 4] = [("R", true), ("G", true), ("B", true), ("A", true)];
|
||||
const CRYPTO: [(&str, bool); 4] = [("Crypto_Material00.R", false), ("Crypto_Material00.G", false), ("Crypto_Material00.B", false), ("Crypto_Material00.A", false)];
|
||||
|
||||
/// A multi-part file as renderers write one part per pass: the beauty, a Cryptomatte layer whose
|
||||
/// channels keep their full names, and depth; one scan line per chunk.
|
||||
fn multipart() -> Vec<u8> {
|
||||
write(
|
||||
vec![
|
||||
part(Some("beauty"), &RGBA, SIZE, Compression::ZIP1),
|
||||
part(Some("Crypto_Material"), &CRYPTO, SIZE, Compression::ZIP1),
|
||||
part(Some("depth"), &[("Z", false)], SIZE, Compression::ZIP1),
|
||||
],
|
||||
SIZE,
|
||||
)
|
||||
}
|
||||
|
||||
/// A single-part ZIPS file with a picture and many passes: every chunk holds every channel.
|
||||
fn single_part_zips() -> Vec<u8> {
|
||||
let mut list: Vec<(String, bool)> = RGBA.iter().map(|(n, h)| (n.to_string(), *h)).collect();
|
||||
for pass in ["diffuse", "specular", "normal", "emit"] {
|
||||
list.extend(["R", "G", "B"].map(|c| (format!("{pass}.{c}"), true)));
|
||||
}
|
||||
list.extend(["R", "G", "B", "A"].map(|c| (format!("CryptoObject00.{c}"), false)));
|
||||
let names: Vec<(&str, bool)> = list.iter().map(|(n, h)| (n.as_str(), *h)).collect();
|
||||
write(vec![part(None, &names, SIZE, Compression::ZIP1)], SIZE)
|
||||
}
|
||||
|
||||
/// `bytes` written to a temporary file named after `name` (removed by [`Temp`]).
|
||||
struct Temp(std::path::PathBuf);
|
||||
|
||||
impl Temp {
|
||||
fn new(name: &str, bytes: &[u8]) -> Temp {
|
||||
let path = std::env::temp_dir().join(format!("ec-3271-{}-{name}.exr", std::process::id()));
|
||||
std::fs::write(&path, bytes).unwrap();
|
||||
Temp(path)
|
||||
}
|
||||
fn path(&self) -> String {
|
||||
self.0.to_string_lossy().to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Temp {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_file(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn bits(img: &Image) -> Vec<u32> {
|
||||
img.data.iter().flat_map(|p| p.map(f32::to_bits)).collect()
|
||||
}
|
||||
|
||||
/// The picture the `image` crate's decoder shows of `bytes`, converted as the pool converts it.
|
||||
fn image_crates(bytes: &[u8], f: &Footage) -> Image {
|
||||
let img = crate::probe::load_oriented(bytes, Some(image::ImageFormat::OpenExr)).unwrap();
|
||||
crate::convert::dynamic_to_image(&img, AlphaOp::of(f, FloatCurve::Srgb))
|
||||
}
|
||||
|
||||
/// The chunks a decode fetched in all.
|
||||
fn chunks(r: &DecodeRecord) -> usize {
|
||||
r.stats.chunks.iter().sum()
|
||||
}
|
||||
|
||||
/// #3271: the picture of a multi-part file reads the beauty part's chunks and nothing else,
|
||||
/// converts only its channels, and is the `image` crate's picture bit for bit.
|
||||
#[test]
|
||||
fn a_picture_reads_only_its_part() {
|
||||
let bytes = multipart();
|
||||
let file = Temp::new("picture-part", &bytes);
|
||||
let path = file.path();
|
||||
let f = crate::probe(&file.0).unwrap();
|
||||
let pool = crate::MediaPool::new();
|
||||
let img = pool.frame_at(&f, Tick::ZERO).unwrap();
|
||||
assert_eq!(bits(&img), bits(&image_crates(&bytes, &f)), "the image crate's picture");
|
||||
let d = decodes_of(&path);
|
||||
assert_eq!(d.len(), 1, "{d:?}");
|
||||
assert_eq!(d[0].channels, ["beauty.A", "beauty.B", "beauty.G", "beauty.R"]);
|
||||
assert_eq!(d[0].stats.chunks, [SIZE.1, 0, 0], "only the beauty part's chunks");
|
||||
assert!(d[0].stats.bytes * 2 < bytes.len() as u64, "{} of {} bytes", d[0].stats.bytes, bytes.len());
|
||||
}
|
||||
|
||||
/// #3271: EXtractoR reading 3 channels of a multi-part file reads only their part and converts
|
||||
/// only those channels; the stored names find them (`Crypto_Material00.R` in part
|
||||
/// `Crypto_Material`), and the manifest and layer resolve as the file names them.
|
||||
#[test]
|
||||
fn three_aux_channels_read_only_their_part() {
|
||||
let bytes = multipart();
|
||||
let file = Temp::new("aux-part", &bytes);
|
||||
let path = file.path();
|
||||
let f = crate::probe(&file.0).unwrap();
|
||||
let pool = crate::MediaPool::new();
|
||||
let need = AuxNeed::Lookups(["Crypto_Material00.R", "Crypto_Material00.G", "Crypto_Material00.B"].map(|n| AuxLookup::Name(n.into())).to_vec());
|
||||
let aux = pool.aux_for(ItemId(1), &f, Tick::ZERO, &need).unwrap();
|
||||
let d = decodes_of(&path);
|
||||
assert_eq!(d.len(), 1);
|
||||
assert_eq!(d[0].channels.len(), 3, "{:?}", d[0].channels);
|
||||
assert_eq!(d[0].stats.chunks, [0, SIZE.1, 0], "only the Cryptomatte part's chunks");
|
||||
let r = aux.get("Crypto_Material00.R").unwrap();
|
||||
assert_eq!(r.len(), SIZE.0 * SIZE.1);
|
||||
assert_eq!(r.get(5).copied(), Some(value(5, 0, false)));
|
||||
assert!(aux.get("Crypto_Material00.A").is_none(), "not read");
|
||||
assert!(aux.listed.iter().any(|n| n == "depth.Z"), "every name listed");
|
||||
}
|
||||
|
||||
/// #3271: lookups by the name a multi-part file stores a channel under work on all channels too
|
||||
/// (EXtractoR's `Crypto_Material00.R`, Cryptomatte's Layer Name `Crypto_Material`), while the
|
||||
/// channel list keeps showing `Crypto_Material.Crypto_Material00.R`.
|
||||
#[test]
|
||||
fn multipart_channels_are_found_by_their_stored_names() {
|
||||
let bytes = multipart();
|
||||
let path = "/3271-stored-names.exr";
|
||||
let pool = crate::MediaPool::new();
|
||||
pool.add_bytes(path, bytes.as_slice().into());
|
||||
let f = crate::probe_bytes(path, bytes.as_slice().into()).unwrap();
|
||||
let aux = pool.aux(ItemId(1), &f, Tick::ZERO).unwrap();
|
||||
assert!(aux.names().contains(&"Crypto_Material.Crypto_Material00.R"), "{:?}", aux.names());
|
||||
assert!(aux.get("Crypto_Material00.R").is_some());
|
||||
let layer = effectcraft_raster::channels3d::crypto_layer_named(&aux, "Crypto_Material", 0).unwrap();
|
||||
assert_eq!(layer, "Crypto_Material.Crypto_Material");
|
||||
assert_eq!(aux.crypto_rank_planes(&layer).len(), 2);
|
||||
assert_eq!(aux.manifest(&layer).map(<[_]>::len), Some(1));
|
||||
// The names alone, from the headers.
|
||||
let names = pool.aux_names(ItemId(1), &f, Tick::ZERO).unwrap();
|
||||
assert_eq!(names.names(), aux.names());
|
||||
assert!(names.channels.iter().all(|(_, p)| p.is_empty()), "no pixels decoded");
|
||||
}
|
||||
|
||||
/// #3271: footage whose effects read 3 channels decodes its picture with them in one pass: one
|
||||
/// read of the file's chunks, one decode, for the picture and the effect.
|
||||
#[test]
|
||||
fn picture_and_aux_share_one_read_and_one_decode() {
|
||||
let bytes = single_part_zips();
|
||||
let file = Temp::new("one-decode", &bytes);
|
||||
let path = file.path();
|
||||
let f = crate::probe(&file.0).unwrap();
|
||||
let pool = crate::MediaPool::new();
|
||||
let need = AuxNeed::Lookups(vec![AuxLookup::Name("normal.R".into()), AuxLookup::Crypto { name: String::new(), index: 0 }]);
|
||||
pool.set_aux_footage(std::slice::from_ref(&f.path));
|
||||
pool.set_aux_needs(&[(f.path.clone(), need.clone())]);
|
||||
let img = pool.frame_at(&f, Tick::ZERO).unwrap();
|
||||
let aux = pool.aux_for(ItemId(1), &f, Tick::ZERO, &need).unwrap();
|
||||
let d = decodes_of(&path);
|
||||
assert_eq!(d.len(), 1, "one decode: {d:?}");
|
||||
assert_eq!(chunks(&d[0]), SIZE.1, "each chunk read once");
|
||||
assert_eq!(d[0].channels.len(), 4 + 1 + 4, "the picture's, normal.R and the rank planes: {:?}", d[0].channels);
|
||||
assert!(aux.get("normal.R").is_some() && aux.crypto_rank_planes("CryptoObject").len() == 2);
|
||||
assert_eq!(bits(&img), bits(&image_crates(&bytes, &f)));
|
||||
}
|
||||
|
||||
/// The index and an in-memory source of `bytes`.
|
||||
fn indexed(bytes: &[u8]) -> (Arc<ExrIndex>, std::io::Cursor<Vec<u8>>) {
|
||||
let mut src = std::io::Cursor::new(bytes.to_vec());
|
||||
let ix = crate::exr_index::build(&mut src, bytes.len() as u64, FileStamp::Virtual).unwrap();
|
||||
(Arc::new(ix), src)
|
||||
}
|
||||
|
||||
/// #3271: a row range decompresses only the chunks holding those rows: with one scan line per
|
||||
/// chunk (ZIPS) exactly those lines, with 16 (ZIP) the chunks around them; a reduced level
|
||||
/// skips every other line on ZIPS. The rows read are the full read's, the rest is fill.
|
||||
#[test]
|
||||
fn rows_and_levels_skip_lines() {
|
||||
for (compression, lines) in [(Compression::ZIP1, 10), (Compression::ZIP16, 32)] {
|
||||
let bytes = write(vec![part(None, &RGBA, (32, 64), compression)], (32, 64));
|
||||
let (ix, mut src) = indexed(&bytes);
|
||||
let all = ReadRequest::full(vec![0, 1, 2, 3]);
|
||||
let (full, full_stats) = crate::exr_read::read_planes(&mut src, &ix, &all);
|
||||
let full = full.unwrap();
|
||||
assert_eq!(full_stats.lines, 64);
|
||||
let band = ReadRequest { rows: Some(10..20), ..all.clone() };
|
||||
let (got, stats) = crate::exr_read::read_planes(&mut src, &ix, &band);
|
||||
let got = got.unwrap();
|
||||
assert_eq!(stats.lines, lines, "{compression:?}");
|
||||
for (g, f) in got.iter().zip(&full) {
|
||||
for y in 0..64 {
|
||||
let row = |p: &Vec<f32>| p[y * 32..(y + 1) * 32].to_vec();
|
||||
if (10..20).contains(&y) {
|
||||
assert_eq!(row(g), row(f), "row {y}");
|
||||
} else {
|
||||
assert!(row(g).iter().all(|v| *v == 0.0), "row {y} not read");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Half resolution on ZIPS: half the lines, every other row and column of the full read.
|
||||
let bytes = write(vec![part(None, &RGBA, (32, 64), Compression::ZIP1)], (32, 64));
|
||||
let (ix, mut src) = indexed(&bytes);
|
||||
let (full, _) = crate::exr_read::read_planes(&mut src, &ix, &ReadRequest::full(vec![3]));
|
||||
let (half, stats) = crate::exr_read::read_planes(&mut src, &ix, &ReadRequest { level: 1, ..ReadRequest::full(vec![3]) });
|
||||
let (full, half) = (full.unwrap(), half.unwrap());
|
||||
assert_eq!(stats.lines, 32);
|
||||
assert_eq!(crate::exr_read::level_size((32, 64), 1), (16, 32));
|
||||
for y in 0..32 {
|
||||
for x in 0..16 {
|
||||
assert_eq!(half[0][y * 16 + x].to_bits(), full[0][y * 2 * 32 + x * 2].to_bits(), "({x}, {y})");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// #474: requests for a file at the same time decode it once; the others wait for that decode
|
||||
/// and share its planes.
|
||||
#[test]
|
||||
fn concurrent_requests_decode_a_file_once() {
|
||||
let bytes: Arc<[u8]> = single_part_zips().into();
|
||||
let path = "/3271-concurrent.exr";
|
||||
let source = ExrSource::Bytes(bytes.clone());
|
||||
let store = PlaneStore::new(CACHE_BUDGET);
|
||||
let index = crate::exr_index::get(path, &source).unwrap();
|
||||
let barrier = std::sync::Barrier::new(8);
|
||||
let got: Vec<Arc<Batch>> = std::thread::scope(|s| {
|
||||
let jobs: Vec<_> = (0..8)
|
||||
.map(|_| {
|
||||
s.spawn(|| {
|
||||
barrier.wait();
|
||||
store.get(path, &source, &index, &Want { all: true, ids: Default::default() }, Use::Other).unwrap()
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
jobs.into_iter().map(|j| j.join().unwrap()).collect()
|
||||
});
|
||||
assert!(got.iter().all(|b| Arc::ptr_eq(b, &got[0])), "every caller has the same planes");
|
||||
assert_eq!(decodes_of(path).len(), 1, "one decode");
|
||||
}
|
||||
|
||||
/// #3271: a request for channels a decode in progress doesn't read waits for it, then decodes
|
||||
/// only what is still missing; the planes it gets hold both.
|
||||
#[test]
|
||||
fn a_request_not_covered_decodes_only_what_is_missing() {
|
||||
let bytes: Arc<[u8]> = single_part_zips().into();
|
||||
let path = "/3271-follow-up.exr";
|
||||
let source = ExrSource::Bytes(bytes);
|
||||
let store = PlaneStore::new(CACHE_BUDGET);
|
||||
let index = crate::exr_index::get(path, &source).unwrap();
|
||||
let picture: BTreeSet<usize> = index.picture_ids().unwrap().into_iter().collect();
|
||||
let normal: BTreeSet<usize> = index.ids_named(["normal.R", "normal.G"]).into_iter().collect();
|
||||
store.get(path, &source, &index, &Want { all: false, ids: picture.clone() }, Use::Other).unwrap();
|
||||
let b = store.get(path, &source, &index, &Want { all: false, ids: normal.clone() }, Use::Other).unwrap();
|
||||
let d = decodes_of(path);
|
||||
assert_eq!(d.len(), 2);
|
||||
assert_eq!(d[1].channels, ["normal.G", "normal.R"], "only the missing channels");
|
||||
assert!(picture.iter().chain(&normal).all(|id| b.plane(*id).is_some()), "both");
|
||||
// Covered now: no decode.
|
||||
store.get(path, &source, &index, &Want { all: false, ids: picture }, Use::Other).unwrap();
|
||||
assert_eq!(decodes_of(path).len(), 2);
|
||||
}
|
||||
|
||||
/// #474: the store is bounded by bytes, least recently used out first; the newest file stays
|
||||
/// even when it alone is over the budget, and unreadable files are remembered without bytes
|
||||
/// (not read again) until forgotten.
|
||||
#[test]
|
||||
fn the_store_evicts_least_recently_used_by_bytes() {
|
||||
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(2 * one);
|
||||
let all = Want { all: true, ids: Default::default() };
|
||||
let get = |p: &str| {
|
||||
let source = ExrSource::Bytes(bytes.clone());
|
||||
let index = crate::exr_index::get(p, &source).unwrap();
|
||||
store.get(p, &source, &index, &all, Use::Other).map(|_| ())
|
||||
};
|
||||
get("/3271-evict-a.exr").unwrap();
|
||||
get("/3271-evict-b.exr").unwrap();
|
||||
get("/3271-evict-a.exr").unwrap(); // a is now more recent than b
|
||||
get("/3271-evict-c.exr").unwrap();
|
||||
assert_eq!(store.cached(), ["/3271-evict-a.exr", "/3271-evict-c.exr"], "b was least recently used");
|
||||
let small = PlaneStore::new(one / 2);
|
||||
for p in ["/3271-evict-big.exr", "/3271-evict-big2.exr"] {
|
||||
let source = ExrSource::Bytes(bytes.clone());
|
||||
let index = crate::exr_index::get(p, &source).unwrap();
|
||||
assert!(small.get(p, &source, &index, &all, Use::Other).is_ok());
|
||||
assert_eq!(small.cached(), [p], "the newest stays");
|
||||
}
|
||||
// An unreadable file (its chunk table points past the end): remembered, not read again.
|
||||
let mut broken = bytes.to_vec();
|
||||
broken.truncate(broken.len() - 64);
|
||||
let source = ExrSource::Bytes(broken.into());
|
||||
let path = "/3271-evict-broken.exr";
|
||||
let index = crate::exr_index::get(path, &source).unwrap();
|
||||
assert!(store.get(path, &source, &index, &all, Use::Other).is_err());
|
||||
assert!(store.get(path, &source, &index, &all, Use::Other).is_err());
|
||||
assert_eq!(decodes_of(path).len(), 1, "not read again");
|
||||
store.forget(path);
|
||||
assert!(!store.cached().contains(&path.to_string()));
|
||||
}
|
||||
|
||||
/// #474 / #3191: a file forgotten (Reload Footage) while it decodes keeps nothing of that decode,
|
||||
/// and the request asks again: no planes made before the reload are kept or served.
|
||||
#[test]
|
||||
fn a_file_forgotten_while_it_decodes_is_not_kept() {
|
||||
let bytes: Arc<[u8]> = write(vec![part(None, &RGBA, (8, 8), Compression::ZIP1)], (8, 8)).into();
|
||||
let path = "/3271-forgotten.exr";
|
||||
let source = ExrSource::Bytes(bytes);
|
||||
let store = Arc::new(PlaneStore::new(CACHE_BUDGET));
|
||||
let index = crate::exr_index::get(path, &source).unwrap();
|
||||
let forgot = Arc::new(AtomicU64::new(0));
|
||||
{
|
||||
let (store, forgot) = (store.clone(), forgot.clone());
|
||||
set_before_read(Some(Arc::new(move |p: &str| {
|
||||
if p == "/3271-forgotten.exr" && forgot.fetch_add(1, Ordering::SeqCst) == 0 {
|
||||
store.forget(p);
|
||||
}
|
||||
})));
|
||||
}
|
||||
let got = store.get(path, &source, &index, &Want { all: true, ids: Default::default() }, Use::Other);
|
||||
set_before_read(None);
|
||||
assert!(got.is_ok());
|
||||
assert_eq!(decodes_of(path).len(), 2, "decoded again after the reload");
|
||||
assert_eq!(store.cached(), [path], "only the second decode is kept");
|
||||
}
|
||||
|
||||
/// #474: the planes of a picture whose effects read the footage's channels are kept for them
|
||||
/// (the renderer asks for the picture, then the channels) until the channels are asked for.
|
||||
#[test]
|
||||
fn a_pictures_planes_are_held_for_its_effects() {
|
||||
let bytes: Arc<[u8]> = single_part_zips().into();
|
||||
let path = "/3271-held.exr";
|
||||
let source = ExrSource::Bytes(bytes);
|
||||
let store = PlaneStore::new(1);
|
||||
let index = crate::exr_index::get(path, &source).unwrap();
|
||||
let all = Want { all: true, ids: Default::default() };
|
||||
drop(store.get(path, &source, &index, &all, Use::Picture).unwrap());
|
||||
assert!(store.is_held(path));
|
||||
drop(store.get(path, &source, &index, &all, Use::Other).unwrap());
|
||||
assert!(!store.is_held(path), "the channels were asked for");
|
||||
assert_eq!(decodes_of(path).len(), 1);
|
||||
}
|
||||
|
||||
/// #474: footage the project says effects read the channels of decodes once from its first frame
|
||||
/// (the picture with the channels). Learned only when `aux()` first asks, the picture was read
|
||||
/// alone before, and the channels then read only what it didn't.
|
||||
#[test]
|
||||
fn given_aux_footage_decodes_once_from_the_first_frame() {
|
||||
use crate::exr_channels::take_decodes;
|
||||
let bytes: Arc<[u8]> = single_part_zips().into();
|
||||
let (given, learned) = ("/3271-given.exr", "/3271-learned.exr");
|
||||
let pool = crate::MediaPool::new();
|
||||
let mut f = std::collections::HashMap::new();
|
||||
for p in [given, learned] {
|
||||
pool.add_bytes(p, bytes.clone());
|
||||
f.insert(p, crate::probe_bytes(p, bytes.clone()).unwrap());
|
||||
}
|
||||
pool.set_aux_footage(&[given.to_string()]);
|
||||
take_decodes();
|
||||
pool.frame_at(&f[given], Tick::ZERO).unwrap();
|
||||
assert!(pool.aux(ItemId(1), &f[given], Tick::ZERO).unwrap().get("diffuse.R").is_some());
|
||||
assert_eq!(take_decodes(), [0, 1], "the picture and the channels share one decode");
|
||||
pool.frame_at(&f[learned], Tick::ZERO).unwrap();
|
||||
pool.aux(ItemId(2), &f[learned], Tick::ZERO).unwrap();
|
||||
assert_eq!(take_decodes(), [0, 2]);
|
||||
let d = decodes_of(learned);
|
||||
assert_eq!(d[0].channels.len(), 4, "the picture alone");
|
||||
assert_eq!(d[1].channels.len() + 4, crate::exr_index::cached(learned).unwrap().channels.len(), "the rest");
|
||||
}
|
||||
|
||||
/// #474: the picture made of the planes of a file with exact R, G, B channels is the `image`
|
||||
/// crate's, bit for bit (first such part, alpha or opaque, data window placed with 0 around it),
|
||||
/// in every alpha interpretation and float curve.
|
||||
#[test]
|
||||
fn picture_from_planes_is_the_image_crates() {
|
||||
use effectcraft_project::AlphaMode;
|
||||
let display = (5usize, 4usize);
|
||||
let files = [
|
||||
write(vec![part(None, &[("R", true), ("G", true), ("B", true), ("A", false), ("Z", false)], display, Compression::ZIP16)], display),
|
||||
write(vec![part(None, &[("R", false), ("G", false), ("B", false), ("Image.R", true)], display, Compression::RLE)], display),
|
||||
write(
|
||||
vec![part(Some("diffuse"), &[("R", true), ("G", true), ("B", true)], display, Compression::ZIP1), part(Some("beauty"), &RGBA, display, Compression::PIZ)],
|
||||
display,
|
||||
),
|
||||
];
|
||||
for (k, bytes) in files.iter().enumerate() {
|
||||
let path = format!("/3271-planes-{k}.exr");
|
||||
let source = ExrSource::Bytes(bytes.as_slice().into());
|
||||
assert!(matches!(crate::exr_index::get(&path, &source).unwrap().rgb, Rgb::Planes(..)), "file {k}");
|
||||
let img = crate::probe::load_oriented(bytes, Some(image::ImageFormat::OpenExr)).unwrap();
|
||||
for alpha in [AlphaMode::Straight, AlphaMode::Premultiplied, AlphaMode::Ignore] {
|
||||
for curve in [FloatCurve::Srgb, FloatCurve::Linear, FloatCurve::Gamma24] {
|
||||
let op = AlphaOp::of(&Footage { alpha, premul_color: [0.2, 0.4, 0.6], ..Default::default() }, curve);
|
||||
let want = crate::convert::dynamic_to_image(&img, op);
|
||||
let Picture::Done(Ok(got)) = picture(&path, &source, op, None) else { panic!("file {k}: a picture") };
|
||||
assert_eq!(bits(&got), bits(&want), "file {k} {alpha:?} {curve:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,331 @@
|
||||
//! The OpenEXR reader (#3271): only the chunks a request needs, fetched by the offset table in
|
||||
//! file order (part by part as the file stores them), decompressed with the `exr` crate's block
|
||||
//! API on the thread pool the caller runs on (the footage decode threads), and only the
|
||||
//! requested channels converted, straight into their display-window planes.
|
||||
//!
|
||||
//! - **No whole-file read.** A request reads the headers it is given ([`ExrIndex`]) and then
|
||||
//! exactly the bytes of the chunks it needs; a multi-part file's other parts are never read.
|
||||
//! - **Rows and levels.** A request names display rows and a level (every 2^level-th row and
|
||||
//! column). With one scan line per chunk (NONE, RLE, ZIPS) a chunk is a line and the lines not
|
||||
//! needed are not read or decompressed; with larger chunks (ZIP, PIZ, DWA, tiles) whole chunks
|
||||
//! are skipped and the rows within a chunk are only skipped when converting.
|
||||
//! - **Cancellable** between batches of chunks ([`filmcraft_media::cancel::cancelled`]).
|
||||
//! - **Bit-identical** to the high-level reader: the same decompression, and each line placed
|
||||
//! as `exr_channels`' planes reader placed it (clipped to the display window, the fill value
|
||||
//! outside the data window, `u32` samples as `f32`).
|
||||
|
||||
use std::io::{Read, Seek, SeekFrom};
|
||||
use std::ops::Range;
|
||||
use std::sync::Arc;
|
||||
|
||||
use effectcraft_render::reads::FileStamp;
|
||||
use exr::block::UncompressedBlock;
|
||||
use exr::block::chunk::Chunk;
|
||||
use exr::meta::attribute::SampleType;
|
||||
use rayon::prelude::*;
|
||||
|
||||
use crate::exr_index::ExrIndex;
|
||||
|
||||
/// A source to read an OpenEXR file from: in-memory bytes or a file on disk.
|
||||
pub(crate) trait ReadSeek: Read + Seek + Send {}
|
||||
impl<T: Read + Seek + Send> ReadSeek for T {}
|
||||
|
||||
/// Where an OpenEXR file's bytes come from.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) enum ExrSource {
|
||||
/// Registered in memory (`MediaPool::add_bytes`, web builds, tests).
|
||||
Bytes(Arc<[u8]>),
|
||||
/// A file, opened for each read and read in place.
|
||||
File(std::path::PathBuf),
|
||||
}
|
||||
|
||||
/// An opened [`ExrSource`].
|
||||
pub(crate) struct Opened {
|
||||
pub(crate) reader: Box<dyn ReadSeek>,
|
||||
pub(crate) len: u64,
|
||||
pub(crate) stamp: FileStamp,
|
||||
}
|
||||
|
||||
impl ExrSource {
|
||||
/// Open it: a file only when it is a regular file (a project can name `/dev/zero` or a FIFO,
|
||||
/// #2134), with its identity from the open file.
|
||||
pub(crate) fn open(&self) -> std::io::Result<Opened> {
|
||||
match self {
|
||||
ExrSource::Bytes(b) => Ok(Opened { len: b.len() as u64, reader: Box::new(std::io::Cursor::new(b.clone())), stamp: FileStamp::Virtual }),
|
||||
ExrSource::File(path) => {
|
||||
#[cfg(test)]
|
||||
crate::count_open(path);
|
||||
let file = std::fs::File::open(path)?;
|
||||
let meta = file.metadata()?;
|
||||
if !meta.is_file() {
|
||||
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{} is not a file", path.display())));
|
||||
}
|
||||
Ok(Opened { len: meta.len(), stamp: FileStamp::of_metadata(&meta), reader: Box::new(file) })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What to read: channels (indices into [`ExrIndex::channels`]), display rows, and a level.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct ReadRequest {
|
||||
pub(crate) channels: Vec<usize>,
|
||||
/// Rows of the display window (full resolution); `None`: all.
|
||||
pub(crate) rows: Option<Range<i64>>,
|
||||
/// Every 2^level-th row and column of the display window (0: full resolution).
|
||||
pub(crate) level: u32,
|
||||
}
|
||||
|
||||
impl ReadRequest {
|
||||
/// Every row of `channels` at full resolution.
|
||||
pub(crate) fn full(channels: Vec<usize>) -> ReadRequest {
|
||||
ReadRequest { channels, rows: None, level: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
/// What a read did, for measurements and tests.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub(crate) struct ReadStats {
|
||||
/// Chunks fetched, per part.
|
||||
pub(crate) chunks: Vec<usize>,
|
||||
/// Bytes of chunks read from the source.
|
||||
pub(crate) bytes: u64,
|
||||
/// Scan lines decompressed (rows of the chunks fetched).
|
||||
pub(crate) lines: usize,
|
||||
}
|
||||
|
||||
/// Why a read gave no planes.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) enum ReadError {
|
||||
/// A chunk can't be read or decompressed.
|
||||
Unreadable(String),
|
||||
/// The planes need more memory than one read may allocate (the reason, to show).
|
||||
TooLarge(String),
|
||||
/// The request was called off.
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
/// The most bytes of planes one read allocates (every requested channel is a display-window
|
||||
/// plane of f32): a header can ask for far more than the file holds.
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
pub(crate) const MAX_DECODE_BYTES: usize = 8 << 30;
|
||||
#[cfg(not(target_pointer_width = "64"))]
|
||||
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).
|
||||
const BATCH_CHUNKS: usize = 256;
|
||||
const BATCH_BYTES: u64 = 64 << 20;
|
||||
|
||||
/// Samples of one line placed in a plane.
|
||||
struct Placed {
|
||||
/// The requested channel's position in the request.
|
||||
slot: usize,
|
||||
/// The first sample's index in the plane.
|
||||
start: usize,
|
||||
values: Vec<f32>,
|
||||
}
|
||||
|
||||
/// The size of the planes of a read at `level` of a `w` × `h` display window.
|
||||
pub(crate) fn level_size((w, h): (usize, usize), level: u32) -> (usize, usize) {
|
||||
let stride = 1usize << level.min(16);
|
||||
(w.div_ceil(stride), h.div_ceil(stride))
|
||||
}
|
||||
|
||||
/// Read `req` from `src` (the file `index` describes): one plane per requested channel, in
|
||||
/// request order, covering the display window at the request's level (the channel's fill value
|
||||
/// where the file has no samples or the request no rows).
|
||||
pub(crate) fn read_planes(src: &mut dyn ReadSeek, index: &ExrIndex, req: &ReadRequest) -> (Result<Vec<Vec<f32>>, ReadError>, ReadStats) {
|
||||
let mut stats = ReadStats { chunks: vec![0; index.parts.len()], ..Default::default() };
|
||||
let planes = read_into(src, index, req, &mut stats);
|
||||
(planes, stats)
|
||||
}
|
||||
|
||||
fn read_into(src: &mut dyn ReadSeek, index: &ExrIndex, req: &ReadRequest, stats: &mut ReadStats) -> Result<Vec<Vec<f32>>, ReadError> {
|
||||
let level = req.level.min(16);
|
||||
let stride = 1i64 << level;
|
||||
let (w, h) = index.size();
|
||||
let (ow, oh) = level_size((w, h), level);
|
||||
let n = ow.checked_mul(oh).ok_or_else(|| ReadError::Unreadable("display window".into()))?;
|
||||
let need = n.saturating_mul(req.channels.len()).saturating_mul(std::mem::size_of::<f32>());
|
||||
if need > MAX_DECODE_BYTES {
|
||||
let gb = |b: usize| b as f64 / f64::from(1u32 << 30);
|
||||
return Err(ReadError::TooLarge(format!(
|
||||
"its {}×{} OpenEXR channels need {:.1} GB of memory, more than the {:.1} GB one file may use",
|
||||
w,
|
||||
h,
|
||||
gb(need),
|
||||
gb(MAX_DECODE_BYTES)
|
||||
)));
|
||||
}
|
||||
// Per part: the plane slot of each of its channels (`None`: not requested).
|
||||
let mut slots: Vec<Vec<Option<usize>>> = index.meta.headers.iter().map(|h| vec![None; h.channels.list.len()]).collect();
|
||||
let mut planes = Vec::with_capacity(req.channels.len());
|
||||
for (slot, id) in req.channels.iter().enumerate() {
|
||||
let c = index.channels.get(*id).ok_or_else(|| ReadError::Unreadable("no such channel".into()))?;
|
||||
if let Some(s) = slots.get_mut(c.part).and_then(|p| p.get_mut(c.channel)) {
|
||||
*s = Some(slot);
|
||||
}
|
||||
planes.push(vec![c.fill; n]);
|
||||
}
|
||||
let rows = req.rows.clone().unwrap_or(0..h as i64);
|
||||
let (r0, r1) = (rows.start.max(0), rows.end.min(h as i64));
|
||||
// A display row is read: in the rows, on the level's grid.
|
||||
let wanted = move |ty: i64| ty >= r0 && ty < r1 && ty.rem_euclid(stride) == 0;
|
||||
// A chunk holding display rows `a..b` has a row read.
|
||||
let any_wanted = move |a: i64, b: i64| {
|
||||
let first = a.max(r0);
|
||||
let first = first + (stride - first.rem_euclid(stride)) % stride;
|
||||
first < b.min(r1)
|
||||
};
|
||||
// The chunks to read, in file order.
|
||||
let mut jobs: Vec<(usize, u64, u64)> = vec![];
|
||||
for (pi, part) in index.parts.iter().enumerate() {
|
||||
if !slots.get(pi).is_some_and(|s| s.iter().any(Option::is_some)) {
|
||||
continue;
|
||||
}
|
||||
for c in &part.chunks {
|
||||
let ty0 = c.y.saturating_add(part.offset.1);
|
||||
if !c.largest || !any_wanted(ty0, ty0.saturating_add(c.rows as i64)) {
|
||||
continue;
|
||||
}
|
||||
if c.len == 0 {
|
||||
return Err(ReadError::Unreadable("a chunk offset outside the file".into()));
|
||||
}
|
||||
jobs.push((pi, c.offset, c.len));
|
||||
}
|
||||
}
|
||||
jobs.sort_by_key(|(_, offset, _)| *offset);
|
||||
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() {
|
||||
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()));
|
||||
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::Unreadable(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::Unreadable(e.to_string()))?;
|
||||
(&mut *src).take(*len).read_to_end(&mut buf).map_err(|e| ReadError::Unreadable(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();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(planes)
|
||||
}
|
||||
|
||||
/// The planes' geometry for [`chunk_lines`].
|
||||
struct Geometry<'w> {
|
||||
/// The display window's size, and the planes' (at the level).
|
||||
size: (usize, usize),
|
||||
out: (usize, usize),
|
||||
stride: i64,
|
||||
wanted: &'w (dyn Fn(i64) -> bool + Sync),
|
||||
}
|
||||
|
||||
/// Decompress the chunk `buf` of part `pi` and convert the lines of the requested channels
|
||||
/// (`slots`: each channel's plane slot): the samples to place, and the scan lines decompressed.
|
||||
/// A chunk that names another part than its offset-table slot's is not placed.
|
||||
fn chunk_lines(index: &ExrIndex, pi: usize, buf: &[u8], slots: &[Option<usize>], g: &Geometry) -> Result<(Vec<Placed>, usize), String> {
|
||||
let meta = &index.meta;
|
||||
let mut read = buf;
|
||||
let chunk = Chunk::read(&mut read, meta).map_err(|e| e.to_string())?;
|
||||
if chunk.layer_index != pi {
|
||||
return Ok((vec![], 0));
|
||||
}
|
||||
let block = UncompressedBlock::decompress_chunk(chunk, meta, false).map_err(|e| e.to_string())?;
|
||||
let (Some(header), Some(part)) = (meta.headers.get(pi), index.parts.get(pi)) else { return Ok((vec![], 0)) };
|
||||
let lines = block.index.pixel_size.1;
|
||||
let ((lw, lh), (w, h)) = (part.layer, g.size);
|
||||
let (ow, _) = g.out;
|
||||
let mut out = vec![];
|
||||
let (mut f16s, mut f32s, mut u32s) = (vec![], vec![], vec![]);
|
||||
for line in block.lines(&header.channels) {
|
||||
let Some(Some(slot)) = slots.get(line.location.channel).copied() else { continue };
|
||||
let Some(kind) = header.channels.list.get(line.location.channel).map(|c| c.sample_type) else { continue };
|
||||
let (x0, y, count) = (line.location.position.0, line.location.position.1, line.location.sample_count);
|
||||
// The samples of data-window row `y`, columns `x0..x0 + count`, at display row `ty`
|
||||
// from display column `tx0`; clipped to the display window.
|
||||
if y >= lh || x0.checked_add(count).is_none_or(|end| end > lw) {
|
||||
continue;
|
||||
}
|
||||
let Some(ty) = usize::try_from(y as i64 + part.offset.1).ok().filter(|ty| *ty < h) else { continue };
|
||||
if !(g.wanted)(ty as i64) {
|
||||
continue;
|
||||
}
|
||||
let tx0 = x0 as i64 + part.offset.0;
|
||||
let skip = usize::try_from(-tx0).unwrap_or(0).min(count);
|
||||
let tx = usize::try_from(tx0.max(0)).unwrap_or(usize::MAX);
|
||||
let take = (count - skip).min(w.saturating_sub(tx));
|
||||
if take == 0 {
|
||||
continue;
|
||||
}
|
||||
let samples: Vec<f32> = match kind {
|
||||
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()
|
||||
}
|
||||
SampleType::F32 => {
|
||||
f32s.resize(count, 0.0);
|
||||
line.read_samples_into_slice(&mut f32s).map_err(|e| e.to_string())?;
|
||||
f32s.iter().skip(skip).take(take).copied().collect()
|
||||
}
|
||||
SampleType::U32 => {
|
||||
u32s.resize(count, 0);
|
||||
line.read_samples_into_slice(&mut u32s).map_err(|e| e.to_string())?;
|
||||
u32s.iter().skip(skip).take(take).map(|s| *s as f32).collect()
|
||||
}
|
||||
};
|
||||
if g.stride == 1 {
|
||||
let Some(start) = ty.checked_mul(w).and_then(|row| row.checked_add(tx)) else { continue };
|
||||
out.push(Placed { slot, start, values: samples });
|
||||
continue;
|
||||
}
|
||||
// A reduced level: every stride-th column of the row, on the level's grid.
|
||||
let stride = g.stride as usize;
|
||||
let first = tx.div_ceil(stride) * stride;
|
||||
let values: Vec<f32> = samples.iter().enumerate().filter(|(j, _)| (tx + j) % stride == 0).map(|(_, v)| *v).collect();
|
||||
if values.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let Some(start) = (ty / stride).checked_mul(ow).and_then(|row| row.checked_add(first / stride)) else { continue };
|
||||
out.push(Placed { slot, start, values });
|
||||
}
|
||||
Ok((out, lines))
|
||||
}
|
||||
@@ -20,6 +20,9 @@ mod convert;
|
||||
pub mod downmix;
|
||||
mod dpx;
|
||||
pub mod exr_channels;
|
||||
mod exr_index;
|
||||
mod exr_planes;
|
||||
mod exr_read;
|
||||
mod gif_anim;
|
||||
mod layered;
|
||||
pub use layered::vector_doc;
|
||||
|
||||
+73
-23
@@ -7,6 +7,7 @@ use std::sync::{Arc, Condvar, Mutex, OnceLock};
|
||||
|
||||
use effectcraft_project::{AlphaMode, Footage, FootageKind, ItemId};
|
||||
use effectcraft_raster::{Image, Px};
|
||||
use effectcraft_raster::channels3d::AuxNeed;
|
||||
use effectcraft_render::reads::{FileStamp, ReadFile, ReadFiles, ReadLog};
|
||||
use effectcraft_render::{FloatCurve, FootageSource};
|
||||
use effectcraft_time::{FrameRate, TICKS_PER_SECOND, Tick};
|
||||
@@ -316,7 +317,7 @@ impl Drop for Decoding {
|
||||
/// frame job that waits on it. The decode's own parallel work runs on these threads, which
|
||||
/// never wait for a picture.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn decode_threads() -> Option<&'static rayon::ThreadPool> {
|
||||
pub(crate) fn decode_threads() -> Option<&'static rayon::ThreadPool> {
|
||||
static THREADS: OnceLock<Option<rayon::ThreadPool>> = OnceLock::new();
|
||||
THREADS
|
||||
.get_or_init(|| {
|
||||
@@ -436,12 +437,21 @@ struct AuxFootage {
|
||||
/// Read through [`FootageSource::aux`] but not given (a reader the project doesn't name, or
|
||||
/// a source that never gives a set). Cleared with each new set.
|
||||
learned: HashSet<String>,
|
||||
/// What the effects reading each footage's channels read of them
|
||||
/// ([`FootageSource::set_aux_needs`], #3271); a footage read without one reads them all.
|
||||
needs: HashMap<String, AuxNeed>,
|
||||
}
|
||||
|
||||
impl AuxFootage {
|
||||
fn contains(&self, path: &str) -> bool {
|
||||
self.given.contains(path) || self.learned.contains(path)
|
||||
}
|
||||
|
||||
/// The channels to read with the picture of footage `path`: what its effects read, all of
|
||||
/// them when they didn't say, none (`None`) when no effect reads them.
|
||||
fn plan(&self, path: &str) -> Option<AuxNeed> {
|
||||
self.contains(path).then(|| self.needs.get(path).cloned().unwrap_or_default())
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes footage for the renderer: stills, image sequences and movies (FilmCraft codecs), plus
|
||||
@@ -621,7 +631,7 @@ impl MediaPool {
|
||||
c.generation = c.generation.wrapping_add(1);
|
||||
c.last.remove(path);
|
||||
drop(c);
|
||||
crate::exr_channels::forget(path);
|
||||
crate::exr_planes::forget(path);
|
||||
}
|
||||
|
||||
/// Provide the contents of `path` from memory (used instead of the file system; web builds).
|
||||
@@ -871,6 +881,14 @@ impl Inner {
|
||||
bytes
|
||||
}
|
||||
|
||||
/// Where the OpenEXR file at `path` is read from: registered bytes, else the file.
|
||||
fn exr_source(&self, path: &str) -> crate::exr_read::ExrSource {
|
||||
match lock(&self.files).get(path) {
|
||||
Some(b) => crate::exr_read::ExrSource::Bytes(b.clone()),
|
||||
None => crate::exr_read::ExrSource::File(std::path::PathBuf::from(path)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The bytes of the file at `path` and its identity as read ([`FileStamp::Missing`] when it
|
||||
/// can't be read), not noted.
|
||||
fn read_file(&self, path: &str) -> (Result<Arc<[u8]>>, ReadFile) {
|
||||
@@ -1281,8 +1299,21 @@ impl Inner {
|
||||
if &**path == PLACEHOLDER {
|
||||
return Ok(colour_bars(footage.width, footage.height));
|
||||
}
|
||||
// OpenEXR: the picture's channels from the plane store, read chunk by chunk (#3271), with
|
||||
// the channels effects read when they read this footage's (#474).
|
||||
if crate::exr_planes::is_exr(path) {
|
||||
use crate::exr_planes::Picture;
|
||||
let extra = lock(&self.aux_footage).plan(&footage.path);
|
||||
match crate::exr_planes::picture(path, &self.exr_source(path), op, extra.as_ref()) {
|
||||
Picture::Done(Ok(img)) => return Ok(img),
|
||||
Picture::Done(Err(why)) => return Err(MediaError::Decode(format!("{path}: {why}"))),
|
||||
Picture::Cancelled => return Err(MediaError::Cancelled),
|
||||
// (the `image` crate decodes the whole file)
|
||||
Picture::Legacy => {}
|
||||
}
|
||||
}
|
||||
let (bytes, file) = self.read_file(path);
|
||||
reads::note(file.clone());
|
||||
reads::note(file);
|
||||
let bytes = bytes?;
|
||||
if let Some(img) = crate::dpx::decode(path, &bytes)? {
|
||||
return Ok(dynamic_to_image(&img, op));
|
||||
@@ -1297,21 +1328,15 @@ impl Inner {
|
||||
{
|
||||
return Err(MediaError::Decode(format!("{path}: {why}")));
|
||||
}
|
||||
// An effect reads this footage's channels: the picture is made of the same decode (#474).
|
||||
let shared = lock(&self.aux_footage).contains(&footage.path);
|
||||
if shared
|
||||
&& path.to_ascii_lowercase().ends_with(".exr")
|
||||
&& let Some(img) = crate::exr_channels::cached_picture(path, bytes.clone(), &file, op)
|
||||
{
|
||||
return Ok(img);
|
||||
}
|
||||
#[cfg(test)]
|
||||
crate::exr_channels::count_decode(false);
|
||||
match crate::probe::load_oriented(&bytes, crate::still_format(path, &bytes)) {
|
||||
Ok(img) => Ok(dynamic_to_image(&img, op)),
|
||||
// A multi-layer OpenEXR file without an unnamed RGB layer: its colour layer.
|
||||
Err(e) => crate::exr_channels::layered_picture(path, bytes, &file, op, shared)
|
||||
.map_err(|why| MediaError::Decode(format!("{path}: {}", why.unwrap_or_else(|| e.to_string())))),
|
||||
Err(e) => match crate::exr_channels::layered_image(&bytes) {
|
||||
Some(img) => Ok(dynamic_to_image(&img, op)),
|
||||
None => Err(MediaError::Decode(format!("{path}: {e}"))),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1350,6 +1375,20 @@ impl Inner {
|
||||
}
|
||||
}
|
||||
|
||||
impl MediaPool {
|
||||
/// The OpenEXR file holding the channels of `footage` at `t` (a still or a sequence frame).
|
||||
fn aux_path(&self, footage: &Footage, t: Tick) -> Option<Arc<str>> {
|
||||
if footage.missing || !footage.has_video {
|
||||
return None;
|
||||
}
|
||||
let loc = Inner::locate(footage, t, FloatCurve::Srgb);
|
||||
if loc.media_t.is_some() || !crate::exr_planes::is_exr(&loc.key.path) {
|
||||
return None;
|
||||
}
|
||||
Some(loc.key.path)
|
||||
}
|
||||
}
|
||||
|
||||
impl FootageSource for MediaPool {
|
||||
fn set_conform_folder(&self, folder: Option<std::path::PathBuf>) {
|
||||
MediaPool::set_conform_folder(self, folder);
|
||||
@@ -1414,21 +1453,32 @@ impl FootageSource for MediaPool {
|
||||
let given: HashSet<String> = paths.iter().cloned().collect();
|
||||
let mut wanted = lock(&self.inner.aux_footage);
|
||||
if given != wanted.given || !wanted.learned.is_empty() {
|
||||
crate::exr_channels::release_held();
|
||||
crate::exr_planes::release_held();
|
||||
}
|
||||
wanted.given = given;
|
||||
wanted.learned.clear();
|
||||
}
|
||||
|
||||
fn aux(&self, _item: ItemId, footage: &Footage, t: Tick) -> Option<Arc<effectcraft_raster::AuxChannels>> {
|
||||
if footage.missing || !footage.has_video {
|
||||
return None;
|
||||
fn set_aux_needs(&self, needs: &[(String, AuxNeed)]) {
|
||||
let mut wanted = lock(&self.inner.aux_footage);
|
||||
let needs: HashMap<String, AuxNeed> = needs.iter().cloned().collect();
|
||||
if needs != wanted.needs {
|
||||
crate::exr_planes::release_held();
|
||||
}
|
||||
let loc = Inner::locate(footage, t, FloatCurve::Srgb);
|
||||
if loc.media_t.is_some() || !loc.key.path.to_ascii_lowercase().ends_with(".exr") {
|
||||
return None;
|
||||
}
|
||||
let path = loc.key.path.clone();
|
||||
wanted.needs = needs;
|
||||
}
|
||||
|
||||
fn aux(&self, item: ItemId, footage: &Footage, t: Tick) -> Option<Arc<effectcraft_raster::AuxChannels>> {
|
||||
self.aux_for(item, footage, t, &AuxNeed::All)
|
||||
}
|
||||
|
||||
fn aux_names(&self, _item: ItemId, footage: &Footage, t: Tick) -> Option<Arc<effectcraft_raster::AuxChannels>> {
|
||||
let path = self.aux_path(footage, t)?;
|
||||
crate::exr_planes::names(&path, &self.inner.exr_source(&path))
|
||||
}
|
||||
|
||||
fn aux_for(&self, _item: ItemId, footage: &Footage, t: Tick, need: &AuxNeed) -> Option<Arc<effectcraft_raster::AuxChannels>> {
|
||||
let path = self.aux_path(footage, t)?;
|
||||
{
|
||||
let mut wanted = lock(&self.inner.aux_footage);
|
||||
if !wanted.contains(&footage.path) {
|
||||
@@ -1439,7 +1489,7 @@ impl FootageSource for MediaPool {
|
||||
wanted.learned.insert(footage.path.clone());
|
||||
}
|
||||
}
|
||||
crate::exr_channels::cached(&path, || self.inner.read(&path).ok())
|
||||
crate::exr_planes::aux(&path, &self.inner.exr_source(&path), need)
|
||||
}
|
||||
|
||||
fn audio(&self, _item: ItemId, footage: &Footage, t: Tick, frames: usize, rate: u32) -> Option<Vec<f32>> {
|
||||
|
||||
@@ -266,10 +266,10 @@ fn probe_still_bytes(path: &str, bytes: &[u8], fmt: image::ImageFormat) -> Resul
|
||||
(w, h, dec.color_type().has_alpha())
|
||||
}
|
||||
Err(e) => {
|
||||
// A multi-layer OpenEXR file without an unnamed RGB layer.
|
||||
let img = (fmt == image::ImageFormat::OpenExr).then(|| crate::exr_channels::layered_image(bytes)).flatten();
|
||||
let img = img.ok_or_else(|| MediaError::Decode(format!("{path}: {e}")))?;
|
||||
(img.width(), img.height(), img.color().has_alpha())
|
||||
// A multi-layer OpenEXR file without an unnamed RGB layer: its picture's size and
|
||||
// alpha from the headers alone (#3271), no pixels decoded.
|
||||
let size = (fmt == image::ImageFormat::OpenExr).then(|| crate::exr_channels::layered_size(bytes)).flatten();
|
||||
size.ok_or_else(|| MediaError::Decode(format!("{path}: {e}")))?
|
||||
}
|
||||
};
|
||||
Ok(still_footage(path, w, h, fmt, has_alpha))
|
||||
|
||||
Reference in new issue
Block a user