From c1aea6d65af78c53d24dc820a08ecae8b020d165 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 16 Nov 2026 22:59:38 +0100 Subject: [PATCH] Bound OpenEXR pictures in memory and keep effects' depth fill The picture of an OpenEXR file (also the black one of a file without a main picture) is checked before anything is read: no larger than a still may decode to and than one read of the file may allocate, else refused with the reason instead of allocated. Import refuses such a picture as other stills. The fill of a picture channel in a part named like depth is the background depth again, as effects read it; the picture is 0 outside that part's data window when composed instead. A read decompresses on at most four threads of its own, none for a small file. --- crates/media/src/exr_channels.rs | 22 +++++++++++++++----- crates/media/src/exr_index.rs | 31 +++++++++++++++++++--------- crates/media/src/exr_planes.rs | 24 ++++++++++++++++++++- crates/media/src/exr_planes/tests.rs | 21 +++++++++++++++++++ crates/media/src/exr_read.rs | 11 ++++++++-- crates/media/src/probe.rs | 7 ++++++- crates/media/tests/it/exr_golden.rs | 10 ++++----- docs/footage.md | 4 ++-- 8 files changed, 104 insertions(+), 26 deletions(-) diff --git a/crates/media/src/exr_channels.rs b/crates/media/src/exr_channels.rs index cd2f5b7ce..422502c5f 100644 --- a/crates/media/src/exr_channels.rs +++ b/crates/media/src/exr_channels.rs @@ -93,6 +93,10 @@ pub fn picture_image(bytes: &[u8]) -> Option { let (w, h) = index.size(); let (w, h) = (u32::try_from(w).ok()?, u32::try_from(h).ok()?); let n = (w as usize).checked_mul(h as usize)?; + // (the caller converts it to a float RGBA picture: a still's bound) + if n as u64 > crate::probe::MAX_STILL_PIXELS { + return None; + } let Some(picture) = &index.picture else { let len = n.checked_mul(3)?; let mut data: Vec = Vec::new(); @@ -112,10 +116,15 @@ pub fn picture_image(bytes: &[u8]) -> Option { }; let mut data = Vec::new(); data.try_reserve_exact(n.checked_mul(if a.is_some() { 4 } else { 3 })?).ok()?; + // (0 outside the picture part's data window where its planes hold another fill) + let width = w as usize; + let shown = |i: usize| picture.inside.is_none_or(|[x0, y0, x1, y1]| (x0..x1).contains(&(i % width.max(1))) && (y0..y1).contains(&(i / width.max(1)))); for i in 0..n { - data.extend([r.get(i)?, g.get(i)?, b.get(i)?].map(|v| *v)); + let on = shown(i); + let pick = |v: &f32| if on { *v } else { 0.0 }; + data.extend([r.get(i)?, g.get(i)?, b.get(i)?].map(pick)); if let Some(a) = a { - data.push(*a.get(i)?); + data.push(pick(a.get(i)?)); } } match a { @@ -142,13 +151,16 @@ pub(crate) fn black_picture(w: u32, h: u32, op: AlphaOp) -> Image { /// The picture made of planes `rgb` and `a` (opaque without one) of `aux`, as /// [`crate::convert::dynamic_to_image`] makes it of the same values. `None` when a plane is /// missing or short. -pub(crate) fn planes_picture(aux: &AuxChannels, rgb: [&[f32]; 3], a: Option<&[f32]>, op: AlphaOp) -> Option { - let n = (aux.width as usize).checked_mul(aux.height as usize)?; +pub(crate) fn planes_picture(aux: &AuxChannels, rgb: [&[f32]; 3], a: Option<&[f32]>, inside: Option<[usize; 4]>, op: AlphaOp) -> Option { + let w = aux.width as usize; + let n = w.checked_mul(aux.height as usize)?; if rgb.iter().chain(&a).any(|p| p.len() < n) { return None; } let [r, g, b] = rgb; - let at = |p: &[f32], i: usize| p.get(i).copied().unwrap_or(0.0); + // (0 outside `inside`, the picture part's data window, where the planes hold another fill) + let shown = |i: usize| inside.is_none_or(|[x0, y0, x1, y1]| (x0..x1).contains(&(i % w.max(1))) && (y0..y1).contains(&(i / w.max(1)))); + let at = |p: &[f32], i: usize| if shown(i) { p.get(i).copied().unwrap_or(0.0) } else { 0.0 }; Some(float_to_image(aux.width, aux.height, op, |i| [at(r, i), at(g, i), at(b, i), a.map_or(1.0, |a| at(a, i))])) } diff --git a/crates/media/src/exr_index.rs b/crates/media/src/exr_index.rs index 9de4e2b42..cd9a7ef2d 100644 --- a/crates/media/src/exr_index.rs +++ b/crates/media/src/exr_index.rs @@ -47,6 +47,11 @@ pub(crate) struct IndexChannel { pub(crate) struct MainPicture { pub(crate) layout: ColorLayout, pub(crate) ids: Vec, + /// When a picture channel's plane isn't 0 outside the part's data window (a part whose name + /// reads as depth, `depth.R`: its planes keep the background depth for effects), the data + /// window in display pixels, `[x0, y0, x1, y1)`: the picture is 0 outside it, as the `image` + /// crate and After Effects show it. + pub(crate) inside: Option<[usize; 4]>, } impl MainPicture { @@ -207,14 +212,7 @@ pub(crate) fn build(src: &mut dyn ReadSeek, stamp: FileStamp) -> Result Vec<(String, Vec<(String, u32)>)> { /// ([`craft_exr::channels::main_picture`]: the unprefixed `R`, `G`, `B` (`A`) of the first part /// that has them, else the first unprefixed `Y` (`A`)), as channel ids. The parts' channels are /// matched as stored (a part's own `R`, not the `part.R` plane name). -fn main_picture(parts: &[PartInfo], channels: &[IndexChannel]) -> Option { +fn main_picture(parts: &[PartInfo], display: Window, channels: &[IndexChannel]) -> Option { let (part, layout, picked) = craft_exr::channels::main_picture(parts.iter().map(|p| p.channels.iter().map(|c| c.name.as_str())))?; let ids = picked.iter().map(|ci| channels.iter().position(|c| c.part == part && c.channel == *ci)).collect::>>()?; - Some(MainPicture { layout, ids }) + let filled = ids.iter().any(|id| channels.get(*id).is_some_and(|c| c.fill != 0.0)); + let inside = match filled { + true => Some(data_window_in_display(parts.get(part)?.data_window, display)), + false => None, + }; + Some(MainPicture { layout, ids, inside }) +} + +/// The part of `data` inside `display`, in display pixels from its top left: `[x0, y0, x1, y1)`. +fn data_window_in_display(data: Window, display: Window) -> [usize; 4] { + let clamp = |v: i64, size: u32| usize::try_from(v.clamp(0, i64::from(size))).unwrap_or(0); + let (x0, y0) = (i64::from(data.x) - i64::from(display.x), i64::from(data.y) - i64::from(display.y)); + let (x1, y1) = (x0.saturating_add(i64::from(data.width)), y0.saturating_add(i64::from(data.height))); + [clamp(x0, display.width), clamp(y0, display.height), clamp(x1, display.width), clamp(y1, display.height)] } /// At most this many files are indexed, and their indexes take at most [`INDEXED_BYTES`]; the diff --git a/crates/media/src/exr_planes.rs b/crates/media/src/exr_planes.rs index 40b111d9f..bf261491b 100644 --- a/crates/media/src/exr_planes.rs +++ b/crates/media/src/exr_planes.rs @@ -1153,6 +1153,9 @@ pub(crate) fn picture(path: &str, source: &ExrSource, op: AlphaOp, extra: Option Err(IndexError::Unsupported(why)) => return Picture::Done(Err(why.to_string())), Err(_) => return Picture::Legacy, }; + if let Err(why) = picture_fits(path, &index) { + return Picture::Done(Err(why)); + } let mut want = Want { all: false, ids: index.picture_ids().into_iter().collect() }; if let Some(need) = extra { match need_ids(&index, need) { @@ -1208,7 +1211,23 @@ fn picture_of(index: &ExrIndex, batch: Option<&Batch>, op: AlphaOp) -> Option Some(batch.plane(a)?), None => None, }; - planes_picture(&batch.aux, [batch.plane(r)?, batch.plane(g)?, batch.plane(b)?], a, op) + planes_picture(&batch.aux, [batch.plane(r)?, batch.plane(g)?, batch.plane(b)?], a, main.inside, op) +} + +/// The picture of the file `index` describes (float RGBA, 16 bytes a pixel, allocated whole) is +/// no larger than a still may decode to ([`crate::probe::MAX_STILL_PIXELS`]) nor than one read of +/// the file may allocate: else why not, to show. Checked before anything is read, also for the +/// black picture of a file without a main picture. +fn picture_fits(path: &str, index: &ExrIndex) -> Result<(), String> { + let (w, h) = index.size(); + let pixels = (w as u64).saturating_mul(h as u64); + let need = pixels.saturating_mul(16); + let most = (cap_of(path) as u64).min(crate::probe::MAX_STILL_PIXELS.saturating_mul(16)); + if need <= most { + return Ok(()); + } + let gb = |b: u64| b as f64 / f64::from(1u32 << 30); + Err(format!("its {w}×{h} OpenEXR picture needs {:.1} GB of memory, more than the {:.1} GB one file may use", gb(need), gb(most))) } /// The picture of the OpenEXR at `path` (as [`picture`]) of only the display rows `rows`: those @@ -1221,6 +1240,9 @@ pub(crate) fn picture_rows(path: &str, source: &ExrSource, op: AlphaOp, rows: (i Err(IndexError::Unsupported(why)) => return Picture::Done(Err(why.to_string())), Err(_) => return Picture::Legacy, }; + if let Err(why) = picture_fits(path, &index) { + return Picture::Done(Err(why)); + } // (in file order: the planes are looked up by id) let ids = index.picture_ids(); if ids.is_empty() { diff --git a/crates/media/src/exr_planes/tests.rs b/crates/media/src/exr_planes/tests.rs index f11985f7a..d4fd2d824 100644 --- a/crates/media/src/exr_planes/tests.rs +++ b/crates/media/src/exr_planes/tests.rs @@ -214,6 +214,27 @@ fn a_depth_part_first_leaves_the_picture_to_the_rgba_part() { assert_eq!(d.last().unwrap().stats.chunks, [0, SIZE.1], "only the RGBA part's chunks"); } +/// The black picture of a file without a main picture is allocated whole like any picture: one +/// larger than one read of the file may allocate is refused with the reason, not allocated (an +/// abort in the browser); within it, it is black at the file's size. +#[test] +fn a_black_picture_too_large_is_refused_not_allocated() { + let bytes = write(vec![part(None, &[("depth.Z", false)], (8, 8), Compression::ZIP1)], (8, 8)); + let path = "/no-picture-too-large.exr"; + let source = ExrSource::Bytes(bytes.as_slice().into()); + let op = AlphaOp::of(&Footage::default(), FloatCurve::Linear); + set_cap(path, 8 * 8 * 16 - 1); + match picture(path, &source, op, None) { + Picture::Done(Err(why)) => assert!(why.contains("8×8") && why.contains("GB"), "{why}"), + _ => panic!("refused"), + } + let fits = "/no-picture-fits.exr"; + set_cap(fits, 8 * 8 * 16); + let Picture::Done(Ok(img)) = picture(fits, &source, op, None) else { panic!("a black picture") }; + assert_eq!((img.width, img.height), (8, 8)); + assert!(img.data.iter().all(|p| *p == [0.0, 0.0, 0.0, 1.0])); +} + /// A luminance file (`Y` only, no `R`, `G`, `B`) is grey, as before. #[test] fn a_luminance_file_is_grey() { diff --git a/crates/media/src/exr_read.rs b/crates/media/src/exr_read.rs index 3fe4f544e..611e9e5fb 100644 --- a/crates/media/src/exr_read.rs +++ b/crates/media/src/exr_read.rs @@ -221,6 +221,11 @@ pub(crate) const MAX_DECODE_BYTES: usize = 8 << 30; #[cfg(not(target_pointer_width = "64"))] pub(crate) const MAX_DECODE_BYTES: usize = 512 << 20; +/// The most threads one read decompresses on (beside the caller's), and the size of a file +/// whose chunks are decompressed on the calling thread alone. +const READ_WORKERS: usize = 4; +const SMALL_FILE: u64 = 1 << 20; + /// The size of the planes of a read at `level` of a `w` × `h` display window. #[cfg(test)] pub(crate) fn level_size(size: (usize, usize), level: u32) -> (usize, usize) { @@ -265,8 +270,10 @@ fn read(src: &mut dyn ReadSeek, index: &ExrIndex, req: &ReadRequest) -> Result<( 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()); + // craft-exr decompresses on threads of its own, started for this read, beside the decode + // pool the caller runs on: a few at most, and none for a small file (its chunks are + // decompressed on the calling thread). + request.workers = Some(if index.file.file_len() < SMALL_FILE { 0 } else { rayon::current_num_threads().min(READ_WORKERS) }); 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), diff --git a/crates/media/src/probe.rs b/crates/media/src/probe.rs index 789043325..cf62eb910 100644 --- a/crates/media/src/probe.rs +++ b/crates/media/src/probe.rs @@ -296,7 +296,12 @@ fn probe_still_bytes(path: &str, bytes: &[u8], fmt: image::ImageFormat) -> Resul // shown black): its picture's size and alpha from the headers alone (#3271), no // pixels decoded. let size = (fmt == image::ImageFormat::OpenExr).then(|| crate::exr_channels::picture_size(bytes)).flatten(); - size.ok_or_else(|| MediaError::Decode(format!("{path}: {e}")))? + let (w, h, alpha) = size.ok_or_else(|| MediaError::Decode(format!("{path}: {e}")))?; + // (as every still: no larger than the renderer can hold as floats) + if u64::from(w) * u64::from(h) > MAX_STILL_PIXELS { + return Err(MediaError::Decode(format!("{path}: a {w}×{h} OpenEXR picture is larger than a still may be"))); + } + (w, h, alpha) } }; Ok(still_footage(path, w, h, fmt, has_alpha)) diff --git a/crates/media/tests/it/exr_golden.rs b/crates/media/tests/it/exr_golden.rs index e420de1a3..ff860c326 100644 --- a/crates/media/tests/it/exr_golden.rs +++ b/crates/media/tests/it/exr_golden.rs @@ -248,8 +248,9 @@ fn files() -> Vec<(&'static str, Vec, bool)> { true, ), ( - // The picture is the part "depth" (the first with R, G and B, as the image crate shows - // it): its channels are colour, 0 outside the data window, despite the part's name. + // The picture is the part "depth" (the first with R, G and B), 0 outside its data window + // as the image crate shows it, while its planes keep the background depth there for + // effects (the part's name reads as depth). "multipart_depth_named_rgb_zip16", write( vec![ @@ -338,8 +339,7 @@ fn hashes(name: &str, bytes: &[u8], aux_first: bool) -> Vec<(String, u64, bool)> /// Hashes taken on main before #474's shared decode (`EXR_GOLDEN_PRINT=1`). The pictures of /// the files of named layers only (`layered_*`, `multipart_blender5_rle`) were re-pinned when the -/// picture became After Effects' main picture (black for those files, their channels unchanged), -/// and `multipart_depth_named_rgb_zip16`'s channels when the picture's channels kept a fill of 0. +/// picture became After Effects' main picture (black for those files, their channels unchanged). const PINNED: &[(&str, &str, u64)] = &[ ("rgba_half_zip16", "probe", 0x0a32e074a28c1f66), ("rgba_half_zip16", "probed/linear", 0xa84600283dce98f6), @@ -487,7 +487,7 @@ const PINNED: &[(&str, &str, u64)] = &[ ("multipart_depth_named_rgb_zip16", "premul/linear", 0x869374e9b0384cea), ("multipart_depth_named_rgb_zip16", "ignore/linear", 0x3b86057d18a16bf0), ("multipart_depth_named_rgb_zip16", "preserve_rgb", 0x3b86057d18a16bf0), - ("multipart_depth_named_rgb_zip16", "aux", 0x6651502f552749d5), + ("multipart_depth_named_rgb_zip16", "aux", 0x0fa56884e83b4417), ]; #[test] diff --git a/docs/footage.md b/docs/footage.md index b0477af56..abab92161 100644 --- a/docs/footage.md +++ b/docs/footage.md @@ -118,8 +118,8 @@ false` goes back to Use Source File Timecode). Cineon Settings: `{"logConversion Float OpenEXR renders hold linear light; EffectCraft sRGB-encodes them as they are read, like After Effects with Interpret As Linear Light for 32 bpc, unless the item preserves RGB (above). A multi-layer file shows its main picture, as in After Effects: the channels without a layer -prefix, `R`, `G`, `B` (and `A`) of the first part that has all three, else the first part's `Y` -(and `A`) as grey. Renders that keep every pass in named layers (Blender's +prefix, `R`, `G`, `B` (and `A`) of the first part that has all three, else the first part that +has a `Y` (its `Y` and `A`) as grey. Renders that keep every pass in named layers (Blender's `ViewLayer.Combined`, its compositor's File Output `Image`, Nuke's `beauty.R`) have no main picture and show black at the file's size, as After Effects shows them; EXtractoR (and Cryptomatte and the 3D Channel effects) read every layer and channel by name.