mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-19 16:45:28 +00:00
Read OpenEXR planes through craft-exr's seekable reader
The plane store's index and reader now use craft_exr::seek: Index::build reads and checks the headers and offset tables, read_planes fetches only the chunks of the requested channels, rows and level. The app keeps what is its own: the file identity, plane names and aliases, Cryptomatte manifests, the picture's channels, the memory cap of one read and its message, cancellation and the error mapping. The own chunk reading, batching and header parsing are gone, and the exr crate is only a dev-dependency of effectcraft-media now (the tests write files with it).
This commit is contained in:
1 parent
9ff031c5f1
commit
58038903bd
6 files changed
+159
-536
No files matched your search
@@ -31,7 +31,6 @@ craft-media = { workspace = true, features = ["mpegts"] }
|
||||
filmcraft-frame = { workspace = true }
|
||||
filmcraft-color = { workspace = true }
|
||||
filmcraft-time = { workspace = true }
|
||||
exr = { workspace = true }
|
||||
half = { workspace = true }
|
||||
image = { workspace = true, features = ["gif", "webp", "tiff", "bmp", "exr", "tga", "hdr"] }
|
||||
rayon = { workspace = true }
|
||||
@@ -40,6 +39,8 @@ thiserror = { workspace = true }
|
||||
log = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
# Writes the OpenEXR files of the tests (reading is craft-exr's).
|
||||
exr = { workspace = true }
|
||||
# Writes the animated GIFs of the tests.
|
||||
gif = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//! Auxiliary channels from multi-layer OpenEXR files (depth, object / material IDs, normals,
|
||||
//! Cryptomatte, …) for the 3D Channel effects, read with the pure-Rust `exr` crate.
|
||||
//! Cryptomatte, …) for the 3D Channel effects, read with `craft-exr`.
|
||||
//!
|
||||
//! Every channel of every layer becomes a plane named `layer.channel` (or just `channel` for
|
||||
//! the unnamed layer), placed in the display window (pixels outside a layer's data window are
|
||||
@@ -12,10 +12,10 @@
|
||||
//! which the picture of an EXR footage item shares with EXtractoR / Cryptomatte (#474, #3271).
|
||||
//! This module holds the naming and picture rules they share, and whole-file reads of bytes.
|
||||
|
||||
use craft_exr::decode::PartInfo;
|
||||
use effectcraft_raster::channels3d::split_channel;
|
||||
use effectcraft_raster::{AuxChannels, Image};
|
||||
use effectcraft_render::reads::FileStamp;
|
||||
use exr::meta::header::Header;
|
||||
|
||||
use crate::convert::{AlphaOp, float_to_image};
|
||||
use crate::exr_index::ExrIndex;
|
||||
@@ -30,16 +30,16 @@ pub(crate) use craft_exr::channels::{is_depth, part_channel_name as channel_name
|
||||
pub use craft_exr::cryptomatte::parse_manifest;
|
||||
|
||||
/// The part name channels are prefixed with (`None`: unnamed).
|
||||
pub(crate) fn part_name(header: &Header) -> Option<String> {
|
||||
header.own_attributes.layer_name.as_ref().map(|t| t.to_string()).filter(|s| !s.is_empty())
|
||||
pub(crate) fn part_name(part: &PartInfo) -> Option<String> {
|
||||
part.name.clone().filter(|s| !s.is_empty())
|
||||
}
|
||||
|
||||
/// Why an OpenEXR file's pixels can't be decoded when its headers name a compression this build
|
||||
/// can't decompress (HTJ2K, OpenEXR 3.4's High-Throughput JPEG 2000): the decoders only said
|
||||
/// "no non-deep rgb channels" or showed a transparent frame (#482). `None` for other files.
|
||||
pub fn unsupported_compression(bytes: &[u8]) -> Option<&'static str> {
|
||||
let meta = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(bytes), false).ok()?;
|
||||
crate::exr_index::unsupported(&meta)
|
||||
let parts = craft_exr::decode::parts(bytes, &craft_exr::decode::Limits::none()).ok()?;
|
||||
parts.iter().any(|p| p.compression.starts_with("HTJ2K")).then_some(crate::exr_index::HTJ2K)
|
||||
}
|
||||
|
||||
/// How the `image` crate's OpenEXR decoder would show a file: the first part with exact `R`, `G`
|
||||
@@ -75,7 +75,7 @@ pub const WEB_CACHE_CAP: usize = 256 << 20;
|
||||
/// not a readable EXR or the planes need too much memory.
|
||||
fn read_bytes(bytes: &[u8], ids: Option<Vec<usize>>) -> Option<(ExrIndex, Vec<usize>, Vec<Vec<f32>>)> {
|
||||
let mut src = std::io::Cursor::new(bytes);
|
||||
let index = crate::exr_index::build(&mut src, bytes.len() as u64, FileStamp::Virtual).ok()?;
|
||||
let index = crate::exr_index::build(&mut src, FileStamp::Virtual).ok()?;
|
||||
let ids = ids.unwrap_or_else(|| (0..index.channels.len()).collect());
|
||||
let (planes, _) = read_planes(&mut src, &index, &ReadRequest::full(ids.clone()));
|
||||
Some((index, ids, planes.ok()?))
|
||||
@@ -159,7 +159,7 @@ pub(crate) fn picture_names(aux: &AuxChannels) -> Option<([String; 3], Option<St
|
||||
/// Only the picture's channels are decoded (in a multi-part file the other parts aren't read).
|
||||
pub fn layered_image(bytes: &[u8]) -> Option<image::DynamicImage> {
|
||||
let mut src = std::io::Cursor::new(bytes);
|
||||
let index = crate::exr_index::build(&mut src, bytes.len() as u64, FileStamp::Virtual).ok()?;
|
||||
let index = crate::exr_index::build(&mut src, FileStamp::Virtual).ok()?;
|
||||
let (ids, rgb, a) = index.picture.clone()?;
|
||||
let (planes, _) = read_planes(&mut src, &index, &ReadRequest::full(ids.clone()));
|
||||
let aux = aux_of(&index, &ids, planes.ok()?);
|
||||
@@ -186,7 +186,7 @@ pub fn layered_image(bytes: &[u8]) -> Option<image::DynamicImage> {
|
||||
/// (import). `None` when it is not a readable EXR.
|
||||
pub(crate) fn layered_size(bytes: &[u8]) -> Option<(u32, u32, bool)> {
|
||||
let mut src = std::io::Cursor::new(bytes);
|
||||
let index = crate::exr_index::build(&mut src, bytes.len() as u64, FileStamp::Virtual).ok()?;
|
||||
let index = crate::exr_index::build(&mut src, FileStamp::Virtual).ok()?;
|
||||
let (_, _, a) = index.picture.as_ref()?;
|
||||
let (w, h) = index.size();
|
||||
Some((w as u32, h as u32, a.is_some()))
|
||||
@@ -341,7 +341,7 @@ mod tests {
|
||||
let layer = Layer::new((1, 1), LayerAttributes::default(), Encoding::FAST_LOSSLESS, AnyChannels::sort(list.into()));
|
||||
let bytes = file(vec![layer], IntegerBounds::new((0, 0), (16384, 16384)));
|
||||
let mut src = std::io::Cursor::new(bytes.as_slice());
|
||||
let index = crate::exr_index::build(&mut src, bytes.len() as u64, FileStamp::Virtual).unwrap();
|
||||
let index = crate::exr_index::build(&mut src, FileStamp::Virtual).unwrap();
|
||||
let all = ReadRequest::full((0..index.channels.len()).collect());
|
||||
match read_planes(&mut src, &index, &all).0 {
|
||||
Err(crate::exr_read::ReadError::TooLarge(why)) => assert!(why.contains("16384×16384") && why.contains("GB"), "{why}"),
|
||||
|
||||
+79
-228
@@ -1,25 +1,28 @@
|
||||
//! [`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.
|
||||
//! (#3271): the parts, every channel by its plane name, 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.
|
||||
//! The headers and offset tables are read and checked by `craft-exr`
|
||||
//! ([`craft_exr::seek::Index`]), which also knows where each chunk lies; this index adds the
|
||||
//! app's view of the file: the plane names and aliases, the manifests, the picture's channels and
|
||||
//! the file's identity. 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).
|
||||
//! ([`crate::exr_channels::MAX_DISPLAY_PIXELS`]), the data windows (four times that), the blocks,
|
||||
//! 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, SeekFrom};
|
||||
use std::io::SeekFrom;
|
||||
use std::sync::{Arc, Mutex, PoisonError};
|
||||
|
||||
use craft_exr::decode::{Limits, PartInfo, Window};
|
||||
use craft_exr::seek;
|
||||
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, Opened, ReadSeek};
|
||||
@@ -37,37 +40,14 @@ pub(crate) struct IndexChannel {
|
||||
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 headers and chunk tables, as `craft-exr` reads them.
|
||||
pub(crate) file: seek::Index,
|
||||
/// 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) display: Window,
|
||||
pub(crate) channels: Vec<IndexChannel>,
|
||||
/// How the `image` crate's decoder would show the file (indices into `channels`).
|
||||
pub(crate) rgb: Rgb,
|
||||
@@ -83,7 +63,7 @@ pub(crate) struct ExrIndex {
|
||||
impl ExrIndex {
|
||||
/// The display window's size.
|
||||
pub(crate) fn size(&self) -> (usize, usize) {
|
||||
(self.display.size.0, self.display.size.1)
|
||||
(self.display.width as usize, self.display.height as usize)
|
||||
}
|
||||
|
||||
/// The channels the picture is made of: the image crate's choice ([`Rgb::Planes`]) or the
|
||||
@@ -108,6 +88,14 @@ impl ExrIndex {
|
||||
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()
|
||||
}
|
||||
|
||||
/// About the bytes it holds: `craft-exr`'s index (its chunks and header facts), the channels
|
||||
/// and the manifests.
|
||||
pub(crate) fn bytes(&self) -> usize {
|
||||
let names: usize = self.channels.iter().map(|c| c.name.len() * 2 + 64).sum();
|
||||
let manifests: usize = self.names.manifests.iter().map(|(l, m)| l.len() + m.iter().map(|(n, _)| n.len() + 32).sum::<usize>()).sum();
|
||||
self.file.bytes().saturating_add(names).saturating_add(manifests.saturating_mul(2))
|
||||
}
|
||||
}
|
||||
|
||||
/// Why a file has no index.
|
||||
@@ -121,173 +109,59 @@ pub(crate) enum IndexError {
|
||||
Io,
|
||||
}
|
||||
|
||||
/// Bytes counted as they are read (the offset tables start where the headers end).
|
||||
struct Counted<'r> {
|
||||
inner: &'r mut dyn Read,
|
||||
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), checked before anything is
|
||||
/// sized by them, and the offset tables: the headers, the tables and where the chunks start.
|
||||
/// Read through one buffer (attribute names are read a byte at a time); the bytes taken from it
|
||||
/// are counted, so the chunk area's start is exact.
|
||||
fn headers_and_tables(src: &mut dyn ReadSeek, len: u64) -> Result<(MetaData, exr::meta::OffsetTables, u64), IndexError> {
|
||||
let strict = {
|
||||
let mut buffered = std::io::BufReader::with_capacity(64 << 10, &mut *src);
|
||||
let mut counted = Counted { inner: &mut buffered, read: 0 };
|
||||
match MetaData::read_from_buffered(&mut counted, true).and_then(|meta| MetaData::validate(&meta.headers, false).map(|_| meta)) {
|
||||
Ok(meta) => {
|
||||
let header_end = counted.read;
|
||||
check(&meta, len, header_end)?;
|
||||
let tables = MetaData::read_offset_tables(&mut exr::io::PeekRead::new(&mut counted), &meta.headers).map_err(|_| IndexError::Unreadable)?;
|
||||
Some((meta, tables, counted.read))
|
||||
}
|
||||
Err(_) => None,
|
||||
}
|
||||
};
|
||||
match strict {
|
||||
Some(parsed) => Ok(parsed),
|
||||
None => {
|
||||
// (an HTJ2K file fails here: say why)
|
||||
src.seek(SeekFrom::Start(0)).map_err(|_| IndexError::Io)?;
|
||||
match MetaData::read_from_buffered(std::io::BufReader::new(&mut *src), false).ok().as_ref().and_then(unsupported) {
|
||||
Some(why) => Err(IndexError::Unsupported(why)),
|
||||
None => Err(IndexError::Unreadable),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The most bytes one uncompressed block of a part may take: the decompressor allocates that
|
||||
/// much for a block before it looks at the data, so a hostile header (a data window a billion
|
||||
/// pixels wide over one tiny chunk) would ask for gigabytes.
|
||||
pub(crate) const MAX_BLOCK_BYTES: usize = 256 << 20;
|
||||
/// Why an HTJ2K file (OpenEXR 3.4's High-Throughput JPEG 2000) isn't read.
|
||||
pub(crate) const HTJ2K: &str = "OpenEXR HTJ2K compression isn't supported yet: re-save the file with ZIP, PIZ or DWAA compression";
|
||||
|
||||
/// The most pixels of a part's data window (the display window's limit, four times over: data
|
||||
/// windows may reach past it).
|
||||
const MAX_DATA_PIXELS: usize = 4 * MAX_DISPLAY_PIXELS;
|
||||
|
||||
/// The bytes of one uncompressed block of `h` (its pixel bytes times a block's pixels, checked):
|
||||
/// `None` past [`MAX_BLOCK_BYTES`] or when it doesn't fit in a `usize`.
|
||||
pub(crate) fn block_bytes(h: &exr::meta::header::Header) -> Option<usize> {
|
||||
let (w, rows) = match h.blocks {
|
||||
exr::meta::BlockDescription::Tiles(t) => (t.tile_size.0, t.tile_size.1),
|
||||
exr::meta::BlockDescription::ScanLines => (h.layer_size.0, h.compression.scan_lines_per_block().min(h.layer_size.1.max(1))),
|
||||
};
|
||||
let per_pixel = h.channels.list.iter().map(|c| c.sample_type.bytes_per_sample()).try_fold(0usize, usize::checked_add)?;
|
||||
per_pixel.checked_mul(w)?.checked_mul(rows).filter(|b| *b <= MAX_BLOCK_BYTES)
|
||||
/// What `craft-exr` may index: data windows of at most [`MAX_DATA_PIXELS`], of any width or
|
||||
/// height (the display window is checked against [`MAX_DISPLAY_PIXELS`] in [`build`]).
|
||||
/// `craft-exr` bounds every block itself (256 MiB uncompressed).
|
||||
fn limits() -> Limits {
|
||||
let mut limits = Limits::none();
|
||||
limits.max_pixels = MAX_DATA_PIXELS as u64;
|
||||
limits
|
||||
}
|
||||
|
||||
/// Every part of `meta` is safe to decode: its data window and its blocks within bounds.
|
||||
pub(crate) fn parts_in_bounds(meta: &MetaData) -> bool {
|
||||
meta.headers.iter().all(|h| h.layer_size.0.checked_mul(h.layer_size.1).is_some_and(|n| n <= MAX_DATA_PIXELS) && block_bytes(h).is_some())
|
||||
/// The headers and offset tables of the OpenEXR file in `src`, read and checked by `craft-exr`.
|
||||
fn index_file(src: &mut dyn ReadSeek) -> Result<seek::Index, seek::Error> {
|
||||
src.seek(SeekFrom::Start(0)).map_err(|e| seek::Error::Io(e.to_string()))?;
|
||||
// (an index is shared by every request for the file: building one is never called off)
|
||||
seek::Index::build(src, &limits(), &|| false)
|
||||
}
|
||||
|
||||
/// The headers of the OpenEXR `bytes` (read leniently) are safe to hand to a decoder
|
||||
/// ([`parts_in_bounds`]); `false` for anything else.
|
||||
/// The headers of the OpenEXR `bytes` are safe to hand to a decoder: no part's data window or
|
||||
/// block is larger than this reader takes. Damaged headers are left to the decoder to report.
|
||||
pub(crate) fn safe_to_decode(bytes: &[u8]) -> bool {
|
||||
MetaData::read_from_buffered(std::io::Cursor::new(bytes), false).is_ok_and(|meta| parts_in_bounds(&meta))
|
||||
!matches!(index_file(&mut std::io::Cursor::new(bytes)), Err(seek::Error::LimitExceeded(_)))
|
||||
}
|
||||
|
||||
/// Refuse headers this reader doesn't take or that ask for more than the file can hold.
|
||||
fn check(meta: &MetaData, len: u64, header_end: u64) -> Result<(), IndexError> {
|
||||
if !parts_in_bounds(meta) {
|
||||
return Err(IndexError::Unreadable);
|
||||
}
|
||||
if let Some(why) = unsupported(meta) {
|
||||
return Err(IndexError::Unsupported(why));
|
||||
}
|
||||
let first = meta.headers.first().ok_or(IndexError::Unreadable)?;
|
||||
/// Index the OpenEXR file in `src` (identity `stamp`), reading its headers and offset tables
|
||||
/// only.
|
||||
pub(crate) fn build(src: &mut dyn ReadSeek, stamp: FileStamp) -> Result<ExrIndex, IndexError> {
|
||||
let file = index_file(src).map_err(|e| match e {
|
||||
seek::Error::Unsupported(why) if why.contains("HTJ2K") => IndexError::Unsupported(HTJ2K),
|
||||
seek::Error::Io(_) => IndexError::Io,
|
||||
_ => IndexError::Unreadable,
|
||||
})?;
|
||||
let parts = file.parts();
|
||||
// Deep data isn't read (the whole file was refused before).
|
||||
if meta.headers.iter().any(|h| h.deep) {
|
||||
if parts.iter().any(|p| p.deep) {
|
||||
return Err(IndexError::Unreadable);
|
||||
}
|
||||
let display = first.shared_attributes.display_window;
|
||||
let display = file.display();
|
||||
// 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);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// [`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 (mut meta, tables, chunks_start) = headers_and_tables(&mut *src, len)?;
|
||||
// The manifests are parsed, and the other attributes (and previews) aren't kept: an index
|
||||
// stays small however much a renderer writes into the headers.
|
||||
let manifests = manifests(&meta);
|
||||
for h in meta.headers.iter_mut() {
|
||||
h.shared_attributes.other.clear();
|
||||
h.own_attributes.other.clear();
|
||||
h.own_attributes.preview = None;
|
||||
}
|
||||
let first = meta.headers.first().ok_or(IndexError::Unreadable)?;
|
||||
let display = first.shared_attributes.display_window;
|
||||
// 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 = (block_bytes(h).ok_or(IndexError::Unreadable)? 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,
|
||||
});
|
||||
}
|
||||
(display.width as usize).checked_mul(display.height as usize).filter(|n| *n > 0 && *n <= MAX_DISPLAY_PIXELS).ok_or(IndexError::Unreadable)?;
|
||||
let manifests = manifests(parts);
|
||||
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();
|
||||
for (pi, p) in parts.iter().enumerate() {
|
||||
let prefix = part_name(p);
|
||||
for (ci, ch) in p.channels.iter().enumerate() {
|
||||
let stored = ch.name.clone();
|
||||
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.
|
||||
@@ -298,8 +172,8 @@ pub(crate) fn build(src: &mut dyn ReadSeek, len: u64, stamp: FileStamp) -> Resul
|
||||
channels.push(IndexChannel { name, part: pi, channel: ci, fill });
|
||||
}
|
||||
}
|
||||
let rgb = rgb_of(&meta, &channels);
|
||||
let mut names = AuxChannels::new(display.size.0 as u32, display.size.1 as u32, 1.0);
|
||||
let rgb = rgb_of(parts, display, &channels);
|
||||
let mut names = AuxChannels::new(display.width, display.height, 1.0);
|
||||
names.channels = channels.iter().map(|c| (c.name.clone(), vec![])).collect();
|
||||
names.manifests = manifests;
|
||||
names.aliases = aliases;
|
||||
@@ -308,32 +182,22 @@ pub(crate) fn build(src: &mut dyn ReadSeek, len: u64, stamp: FileStamp) -> Resul
|
||||
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) })
|
||||
Ok(ExrIndex { file, stamp, display, 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)>)> {
|
||||
/// The Cryptomatte manifests of the headers (`cryptomatte/<key>/name` and `…/manifest`, in any
|
||||
/// part), sorted.
|
||||
fn manifests(parts: &[PartInfo]) -> 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()),
|
||||
_ => {}
|
||||
}
|
||||
for (key, value) in parts.iter().flat_map(|p| &p.text_attributes) {
|
||||
let Some(rest) = key.strip_prefix("cryptomatte/") else { continue };
|
||||
let Some((id, field)) = rest.split_once('/') else { continue };
|
||||
let e = crypto.entry(id.to_string()).or_default();
|
||||
match field {
|
||||
"name" => e.0 = Some(value.clone()),
|
||||
"manifest" => e.1 = Some(value.clone()),
|
||||
_ => {}
|
||||
}
|
||||
};
|
||||
// (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();
|
||||
@@ -344,10 +208,9 @@ fn manifests(meta: &MetaData) -> Vec<(String, Vec<(String, u32)>)> {
|
||||
/// `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));
|
||||
fn rgb_of(parts: &[PartInfo], display: Window, channels: &[IndexChannel]) -> Rgb {
|
||||
for (pi, p) in parts.iter().enumerate() {
|
||||
let find = |c: &str| p.channels.iter().position(|ch| ch.name == 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")];
|
||||
@@ -355,7 +218,7 @@ fn rgb_of(meta: &MetaData, channels: &[IndexChannel]) -> Rgb {
|
||||
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 {
|
||||
if depth || p.display_window != display {
|
||||
return Rgb::Unsupported;
|
||||
}
|
||||
return Rgb::Planes([*r, *g, *b], a);
|
||||
@@ -368,18 +231,6 @@ fn rgb_of(meta: &MetaData, channels: &[IndexChannel]) -> Rgb {
|
||||
const INDEXED_FILES: usize = 4096;
|
||||
const INDEXED_BYTES: usize = 64 << 20;
|
||||
|
||||
impl ExrIndex {
|
||||
/// About the bytes it holds: its chunks, channels and manifests (the headers' other
|
||||
/// attributes are dropped when it is built).
|
||||
pub(crate) fn bytes(&self) -> usize {
|
||||
let chunks: usize = self.parts.iter().map(|p| p.chunks.len()).sum();
|
||||
let names: usize = self.channels.iter().map(|c| c.name.len() * 2 + 64).sum();
|
||||
let manifests: usize = self.names.manifests.iter().map(|(l, m)| l.len() + m.iter().map(|(n, _)| n.len() + 32).sum::<usize>()).sum();
|
||||
let headers = self.meta.headers.len().saturating_mul(1024);
|
||||
(chunks * std::mem::size_of::<IndexChunk>()).saturating_add(names).saturating_add(manifests.saturating_mul(2)).saturating_add(headers)
|
||||
}
|
||||
}
|
||||
|
||||
/// Indexed files by path.
|
||||
struct Indexes {
|
||||
map: HashMap<String, Arc<ExrIndex>>,
|
||||
@@ -448,7 +299,7 @@ pub(crate) fn get_opened(path: &str, source: &ExrSource) -> Result<(Arc<ExrIndex
|
||||
})?;
|
||||
// (the identity just read: the next checks within the stamp's lifetime use it)
|
||||
source.opened_as(opened.stamp);
|
||||
let ix = Arc::new(build(&mut *opened.reader, opened.len, opened.stamp)?);
|
||||
let ix = Arc::new(build(&mut *opened.reader, opened.stamp)?);
|
||||
keep(path, ix.clone());
|
||||
Ok((ix, Some(opened)))
|
||||
}
|
||||
|
||||
@@ -912,7 +912,7 @@ fn decode(path: &str, source: &ExrSource, flight: &Arc<Flight>, base: Option<Arc
|
||||
if opened.stamp != index.stamp {
|
||||
// The file changed since it was indexed (the next index check sees its new identity).
|
||||
source.opened_as(opened.stamp);
|
||||
let fresh = exr_index::build(&mut *opened.reader, opened.len, opened.stamp).map_err(|_| Refused::Unreadable)?;
|
||||
let fresh = exr_index::build(&mut *opened.reader, opened.stamp).map_err(|_| Refused::Unreadable)?;
|
||||
exr_index::keep(path, Arc::new(fresh));
|
||||
return Err(Refused::Changed);
|
||||
}
|
||||
|
||||
@@ -58,6 +58,19 @@ fn write(parts: Vec<Layer<AnyChannels<FlatSamples>>>, size: (usize, usize)) -> V
|
||||
bytes.into_inner()
|
||||
}
|
||||
|
||||
/// Each part's chunks of the OpenEXR `bytes`, in offset-table order: where each starts and how
|
||||
/// many bytes it takes (up to the next chunk, or the end of the file). Read with the `exr` crate:
|
||||
/// `craft-exr`'s index keeps them to itself.
|
||||
fn chunk_table(bytes: &[u8]) -> Vec<Vec<(u64, u64)>> {
|
||||
let mut src = bytes;
|
||||
let meta = exr::meta::MetaData::read_from_buffered(&mut src, false).unwrap();
|
||||
let tables = exr::meta::MetaData::read_offset_tables(&mut exr::io::PeekRead::new(&mut src), &meta.headers).unwrap();
|
||||
let mut starts: Vec<u64> = tables.iter().flatten().copied().collect();
|
||||
starts.sort_unstable();
|
||||
let end = |at: u64| starts.iter().copied().find(|s| *s > at).unwrap_or(bytes.len() as u64);
|
||||
tables.iter().map(|t| t.iter().map(|at| (*at, end(*at) - at)).collect()).collect()
|
||||
}
|
||||
|
||||
const SIZE: (usize, usize) = (64, 64);
|
||||
const RGBA: [(&str, bool); 4] = [("R", true), ("G", true), ("B", true), ("A", true)];
|
||||
const CRYPTO: [(&str, bool); 4] =
|
||||
@@ -134,8 +147,7 @@ fn a_picture_reads_only_its_part() {
|
||||
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");
|
||||
let index = crate::exr_index::cached(&path).unwrap();
|
||||
let beauty: u64 = index.parts[0].chunks.iter().map(|c| c.len).sum();
|
||||
let beauty: u64 = chunk_table(&bytes)[0].iter().map(|(_, len)| len).sum();
|
||||
assert_eq!(d[0].stats.bytes, beauty, "the beauty part's bytes and nothing else");
|
||||
}
|
||||
|
||||
@@ -210,7 +222,7 @@ fn picture_and_aux_share_one_read_and_one_decode() {
|
||||
/// 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();
|
||||
let ix = crate::exr_index::build(&mut src, FileStamp::Virtual).unwrap();
|
||||
(Arc::new(ix), src)
|
||||
}
|
||||
|
||||
@@ -832,7 +844,7 @@ fn a_hostile_data_window_is_refused_not_allocated() {
|
||||
let bytes = with_data_window_x_max(small, (1 << 30) - 3);
|
||||
let path = format!("/3271-hostile-{compression:?}.exr");
|
||||
assert!(!crate::exr_index::safe_to_decode(&bytes));
|
||||
assert!(crate::exr_index::build(&mut std::io::Cursor::new(bytes.as_slice()), bytes.len() as u64, FileStamp::Virtual).is_err());
|
||||
assert!(crate::exr_index::build(&mut std::io::Cursor::new(bytes.as_slice()), FileStamp::Virtual).is_err());
|
||||
assert!(crate::exr_channels::read_exr_channels(&bytes).is_none());
|
||||
assert!(crate::exr_channels::layered_image(&bytes).is_none());
|
||||
let b: Arc<[u8]> = bytes.into();
|
||||
@@ -852,13 +864,7 @@ fn a_hostile_data_window_is_refused_not_allocated() {
|
||||
fn damaged_files_never_panic() {
|
||||
let files = [single_part_zips(), multipart(), write(vec![part(None, &RGBA, (16, 40), Compression::PIZ)], (16, 40))];
|
||||
for good in &files {
|
||||
let start = crate::exr_index::build(&mut std::io::Cursor::new(good.as_slice()), good.len() as u64, FileStamp::Virtual)
|
||||
.unwrap()
|
||||
.parts
|
||||
.iter()
|
||||
.flat_map(|p| p.chunks.iter().map(|c| c.offset))
|
||||
.min()
|
||||
.unwrap() as usize;
|
||||
let start = chunk_table(good).iter().flatten().map(|(at, _)| *at).min().unwrap() as usize;
|
||||
// From a little before the chunks (the offset tables) to the end, in steps.
|
||||
let from = start.saturating_sub(64);
|
||||
for (i, at) in (from..good.len()).step_by(((good.len() - from) / 150).max(1)).enumerate() {
|
||||
@@ -867,7 +873,7 @@ fn damaged_files_never_panic() {
|
||||
let _ = crate::exr_channels::read_exr_channels(&bad);
|
||||
let _ = crate::exr_channels::layered_image(&bad);
|
||||
let mut src = std::io::Cursor::new(bad.as_slice());
|
||||
if let Ok(ix) = crate::exr_index::build(&mut src, bad.len() as u64, FileStamp::Virtual) {
|
||||
if let Ok(ix) = crate::exr_index::build(&mut src, FileStamp::Virtual) {
|
||||
let req = ReadRequest { rows: Some(3..9), level: 1, ..ReadRequest::full((0..ix.channels.len()).collect()) };
|
||||
let _ = crate::exr_read::read_planes(&mut src, &ix, &req);
|
||||
}
|
||||
@@ -1229,19 +1235,20 @@ fn a_read_in_several_batches_is_whole_and_stops_between_them() {
|
||||
// Called off while the first batch is read: it stops before the next.
|
||||
let flag = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
let mut flaky = Flaky { inner: std::io::Cursor::new(bytes.clone()), reads: 0, cancel_after: Some((1, flag.clone())), fail_from: None };
|
||||
let (got, stats) = filmcraft_media::cancel::with_cancel(&flag, || crate::exr_read::read_planes(&mut flaky, &ix, &all));
|
||||
let (got, _) = filmcraft_media::cancel::with_cancel(&flag, || crate::exr_read::read_planes(&mut flaky, &ix, &all));
|
||||
assert_eq!(got.err(), Some(ReadError::Cancelled));
|
||||
assert!(stats.lines < h, "stopped early: {} lines", stats.lines);
|
||||
// Chunks that follow each other in the file are read in one go, not one read per chunk.
|
||||
let mut counted = Flaky { inner: std::io::Cursor::new(bytes.clone()), reads: 0, cancel_after: None, fail_from: None };
|
||||
assert!(crate::exr_read::read_planes(&mut counted, &ix, &all).0.is_ok());
|
||||
assert!(counted.reads <= 12, "{} reads for {h} chunks", counted.reads);
|
||||
// A read failing in a later batch (at chunk 300): an I/O error after the first was decoded.
|
||||
let at = ix.parts[0].chunks[300].offset;
|
||||
// (a failed read has no stats: the reads it made tell how far it went)
|
||||
assert!(flaky.reads < counted.reads, "stopped early: {} of {} reads", flaky.reads, counted.reads);
|
||||
// A read failing in a later batch (at chunk 300): an I/O error after the first was read.
|
||||
let at = chunk_table(&bytes)[0][300].0;
|
||||
let mut flaky = Flaky { inner: std::io::Cursor::new(bytes), reads: 0, cancel_after: None, fail_from: Some(at) };
|
||||
let (got, stats) = crate::exr_read::read_planes(&mut flaky, &ix, &all);
|
||||
let (got, _) = crate::exr_read::read_planes(&mut flaky, &ix, &all);
|
||||
assert!(matches!(got, Err(ReadError::Io(_))), "{got:?}");
|
||||
assert!(stats.lines > 0, "the batches before it were decoded");
|
||||
assert!(flaky.reads > 1, "the batches before it were read");
|
||||
}
|
||||
|
||||
/// #3271: dropping entries for bytes (a decode over the budget, the memory accountant) drops
|
||||
|
||||
+42
-278
@@ -1,7 +1,7 @@
|
||||
//! 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.
|
||||
//! The OpenEXR reader (#3271): where a file's bytes come from ([`ExrSource`], its identity and
|
||||
//! how it is opened) and the app's side of a read ([`ReadRequest`] of the index's channels, the
|
||||
//! memory one read may take, cancellation and the errors shown). The reading itself is
|
||||
//! `craft-exr`'s ([`craft_exr::seek`]):
|
||||
//!
|
||||
//! - **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.
|
||||
@@ -9,20 +9,16 @@
|
||||
//! 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`).
|
||||
//! - **Cancellable** while chunks are read ([`filmcraft_media::cancel::cancelled`]).
|
||||
//! - **Bit-identical** to the high-level reader: the same decompression, each line clipped to
|
||||
//! the display window, the fill value outside the data window, `u32` samples as `f32`.
|
||||
|
||||
use std::io::{Read, Seek, SeekFrom};
|
||||
use std::io::{Read, Seek};
|
||||
use std::ops::Range;
|
||||
use std::sync::Arc;
|
||||
|
||||
use craft_exr::seek;
|
||||
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;
|
||||
|
||||
@@ -45,7 +41,6 @@ pub(crate) enum ExrSource {
|
||||
/// An opened [`ExrSource`].
|
||||
pub(crate) struct Opened {
|
||||
pub(crate) reader: Box<dyn ReadSeek>,
|
||||
pub(crate) len: u64,
|
||||
pub(crate) stamp: FileStamp,
|
||||
}
|
||||
|
||||
@@ -144,7 +139,7 @@ impl ExrSource {
|
||||
/// #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::Bytes(b) => Ok(Opened { reader: Box::new(std::io::Cursor::new(b.clone())), stamp: FileStamp::Virtual }),
|
||||
ExrSource::File(path) => {
|
||||
#[cfg(test)]
|
||||
crate::count_open(path);
|
||||
@@ -153,7 +148,7 @@ impl ExrSource {
|
||||
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) })
|
||||
Ok(Opened { stamp: FileStamp::of_metadata(&meta), reader: Box::new(file) })
|
||||
}
|
||||
ExrSource::Localized { original, copy, stamp } => {
|
||||
let local = || -> std::io::Result<Opened> {
|
||||
@@ -166,7 +161,7 @@ impl ExrSource {
|
||||
if !meta.is_file() || !complete {
|
||||
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, format!("{}: not a complete copy", copy.display())));
|
||||
}
|
||||
Ok(Opened { len: meta.len(), stamp: *stamp, reader: Box::new(file) })
|
||||
Ok(Opened { stamp: *stamp, reader: Box::new(file) })
|
||||
};
|
||||
// (a copy evicted or damaged meanwhile: the share)
|
||||
local().or_else(|_| ExrSource::File(original.clone()).open())
|
||||
@@ -226,49 +221,26 @@ 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). The first batch takes at most [`FIRST_BATCH_BYTES`]
|
||||
/// and each one after twice the one before, so decoding starts early.
|
||||
const BATCH_CHUNKS: usize = 256;
|
||||
const BATCH_BYTES: u64 = 64 << 20;
|
||||
const FIRST_BATCH_BYTES: u64 = 1 << 20;
|
||||
|
||||
/// The raw chunks of a batch: their bytes one after another, and each chunk's part and bytes.
|
||||
#[derive(Default)]
|
||||
struct Raw {
|
||||
data: Vec<u8>,
|
||||
chunks: Vec<(usize, Range<usize>)>,
|
||||
}
|
||||
|
||||
/// Samples of one line placed in a plane.
|
||||
struct Placed {
|
||||
/// 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))
|
||||
#[cfg(test)]
|
||||
pub(crate) fn level_size(size: (usize, usize), level: u32) -> (usize, usize) {
|
||||
seek::level_size(size, level)
|
||||
}
|
||||
|
||||
/// 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).
|
||||
/// where the file has no samples or the request no rows). The stats are a failed read's
|
||||
/// defaults: `craft-exr` reports none for it.
|
||||
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)
|
||||
match read(src, index, req) {
|
||||
Ok((planes, stats)) => (Ok(planes), stats),
|
||||
Err(e) => (Err(e), ReadStats { chunks: vec![0; index.file.parts().len()], ..Default::default() }),
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
fn read(src: &mut dyn ReadSeek, index: &ExrIndex, req: &ReadRequest) -> Result<(Vec<Vec<f32>>, ReadStats), ReadError> {
|
||||
let (w, h) = index.size();
|
||||
let (ow, oh) = level_size((w, h), level);
|
||||
let (ow, oh) = seek::level_size((w, h), req.level.min(16));
|
||||
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>());
|
||||
let most = req.max_bytes.min(MAX_DECODE_BYTES);
|
||||
@@ -282,233 +254,25 @@ fn read_into(src: &mut dyn ReadSeek, index: &ExrIndex, req: &ReadRequest, stats:
|
||||
gb(most)
|
||||
)));
|
||||
}
|
||||
// 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 mut channels = Vec::with_capacity(req.channels.len());
|
||||
let mut fill = Vec::with_capacity(req.channels.len());
|
||||
for id in &req.channels {
|
||||
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]);
|
||||
channels.push((c.part, c.channel));
|
||||
fill.push(c.fill);
|
||||
}
|
||||
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();
|
||||
let mut raw = Raw::default();
|
||||
let mut most = FIRST_BATCH_BYTES;
|
||||
loop {
|
||||
if filmcraft_media::cancel::cancelled() {
|
||||
return Err(ReadError::Cancelled);
|
||||
}
|
||||
// The next batch is read on this thread while the one read before decompresses and
|
||||
// converts on this pool's other threads (a network read overlaps the decoding).
|
||||
let (batch, more) = rest.split_at(batch_len(rest, most));
|
||||
rest = more;
|
||||
most = most.saturating_mul(2).min(BATCH_BYTES);
|
||||
let decode = std::mem::take(&mut raw);
|
||||
let (next, done) = rayon::join(
|
||||
|| read_batch(&mut *src, batch, &mut pos, stats),
|
||||
|| -> Vec<Result<(Vec<Placed>, usize), String>> {
|
||||
let data = &decode.data;
|
||||
decode
|
||||
.chunks
|
||||
.par_iter()
|
||||
.map(|(pi, r)| chunk_lines(index, *pi, data.get(r.clone()).unwrap_or_default(), slots.get(*pi).map_or(&[][..], Vec::as_slice), &geometry))
|
||||
.collect()
|
||||
},
|
||||
);
|
||||
// Placed plane by plane, the planes in parallel.
|
||||
let mut by_slot: Vec<Vec<Placed>> = (0..planes.len()).map(|_| vec![]).collect();
|
||||
for d in done {
|
||||
let (placed, lines) = d.map_err(ReadError::Unreadable)?;
|
||||
stats.lines = stats.lines.saturating_add(lines);
|
||||
for p in placed {
|
||||
if let Some(s) = by_slot.get_mut(p.slot) {
|
||||
s.push(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
planes.par_iter_mut().zip(by_slot).for_each(|(plane, placed)| {
|
||||
for p in placed {
|
||||
let end = p.start.saturating_add(p.values.len());
|
||||
if let Some(dst) = plane.get_mut(p.start..end) {
|
||||
dst.copy_from_slice(&p.values);
|
||||
}
|
||||
}
|
||||
});
|
||||
raw = next?;
|
||||
if raw.chunks.is_empty() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(planes)
|
||||
}
|
||||
|
||||
/// How many of `jobs` (chunks in file order) the next batch takes: up to [`BATCH_CHUNKS`] chunks
|
||||
/// or `most` bytes, at least one.
|
||||
fn batch_len(jobs: &[(usize, u64, u64)], most: u64) -> usize {
|
||||
let mut take = 0;
|
||||
let mut bytes = 0u64;
|
||||
while let Some((_, _, len)) = jobs.get(take) {
|
||||
if take > 0 && (take >= BATCH_CHUNKS || bytes.saturating_add(*len) > most) {
|
||||
break;
|
||||
}
|
||||
bytes = bytes.saturating_add(*len);
|
||||
take += 1;
|
||||
}
|
||||
take
|
||||
}
|
||||
|
||||
/// The raw bytes of the chunks `batch` (part, offset, length; in file order): chunks that follow
|
||||
/// each other in the file are read in one go (one request to a network share, not one per
|
||||
/// chunk), with one seek per gap (`pos`: where the source stands). A chunk the file ends in
|
||||
/// keeps the bytes there are.
|
||||
fn read_batch(src: &mut dyn ReadSeek, batch: &[(usize, u64, u64)], pos: &mut Option<u64>, stats: &mut ReadStats) -> Result<Raw, ReadError> {
|
||||
let total = batch.iter().map(|(_, _, len)| *len).fold(0u64, u64::saturating_add);
|
||||
let mut raw = Raw::default();
|
||||
raw.data.try_reserve_exact(usize::try_from(total).map_err(|_| ReadError::Unreadable("chunk size".into()))?).map_err(|e| ReadError::Io(e.to_string()))?;
|
||||
let mut i = 0;
|
||||
while let Some((_, start, _)) = batch.get(i) {
|
||||
// A run of chunks, each starting where the one before ends.
|
||||
let (start, mut end, mut j) = (*start, *start, i);
|
||||
while let Some((_, offset, len)) = batch.get(j)
|
||||
&& *offset == end
|
||||
{
|
||||
end = offset.saturating_add(*len);
|
||||
j += 1;
|
||||
}
|
||||
if *pos != Some(start) {
|
||||
src.seek(SeekFrom::Start(start)).map_err(|e| ReadError::Io(e.to_string()))?;
|
||||
}
|
||||
let at = raw.data.len();
|
||||
(&mut *src).take(end.saturating_sub(start)).read_to_end(&mut raw.data).map_err(|e| ReadError::Io(e.to_string()))?;
|
||||
let got = raw.data.len().saturating_sub(at);
|
||||
*pos = Some(start.saturating_add(got as u64));
|
||||
for (pi, offset, len) in batch.get(i..j).unwrap_or_default() {
|
||||
let a = usize::try_from(offset.saturating_sub(start)).map_or(raw.data.len(), |o| at.saturating_add(o)).min(raw.data.len());
|
||||
let b = usize::try_from(*len).map_or(raw.data.len(), |l| a.saturating_add(l)).min(raw.data.len());
|
||||
raw.chunks.push((*pi, a..b));
|
||||
if let Some(c) = stats.chunks.get_mut(*pi) {
|
||||
*c += 1;
|
||||
}
|
||||
}
|
||||
stats.bytes = stats.bytes.saturating_add(got as u64);
|
||||
i = j.max(i + 1);
|
||||
}
|
||||
Ok(raw)
|
||||
}
|
||||
|
||||
/// The planes' geometry for [`chunk_lines`].
|
||||
struct Geometry<'w> {
|
||||
/// The display window's size, and the planes' (at the level).
|
||||
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())?;
|
||||
// (the slice conversion runs the same per-value conversion, in vector steps)
|
||||
let mut out = vec![0.0; take];
|
||||
if let Some(src) = f16s.get(skip..skip + take) {
|
||||
half::slice::HalfFloatSliceExt::convert_to_f32_slice(src, &mut out);
|
||||
}
|
||||
out
|
||||
}
|
||||
SampleType::F32 => {
|
||||
f32s.resize(count, 0.0);
|
||||
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))
|
||||
let mut request = seek::Request::new(&channels, most);
|
||||
request.fill = &fill;
|
||||
request.rows = req.rows.clone();
|
||||
request.level = req.level;
|
||||
// (as many decoding threads as the pool the caller runs on: the footage decode threads)
|
||||
request.workers = Some(rayon::current_num_threads());
|
||||
let planes = seek::read_planes(src, &index.file, &request, &filmcraft_media::cancel::cancelled).map_err(|e| match e {
|
||||
seek::Error::Cancelled => ReadError::Cancelled,
|
||||
seek::Error::Io(why) => ReadError::Io(why),
|
||||
seek::Error::LimitExceeded(why) => ReadError::TooLarge(format!("its OpenEXR channels need more memory than one file may use ({why})")),
|
||||
seek::Error::Malformed(why) | seek::Error::Unsupported(why) => ReadError::Unreadable(why),
|
||||
})?;
|
||||
let stats = ReadStats { chunks: planes.stats.chunks, bytes: planes.stats.bytes, lines: planes.stats.lines };
|
||||
Ok((planes.planes, stats))
|
||||
}
|
||||
Reference in new issue
Block a user