Media: movies opened in place keep their orientation (#796 × #827)

Opening movies in place (#796) skipped the whole-file bytes #827 read the
track matrix from, so the pool and the probe lost the turn for movies on
disk. stream::movie_header reads just the ftyp and moov boxes (bounded,
positional reads) and both in-place paths derive the orientation from it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Brandon Thomas
2026-10-12 03:37:34 -04:00
co-authored by Claude Opus 5.5
parent f41fdf0680
commit 42ef79a2fd
8 changed files with 101 additions and 46 deletions
+1 -1
View File
@@ -23,8 +23,8 @@ mod layered;
pub use layered::vector_doc;
mod pool;
mod probe;
mod webp_anim;
mod stream;
mod webp_anim;
pub use pool::{DEFAULT_BUDGET, MediaPool, PoolStats};
pub use probe::{DEFAULT_SEQUENCE_RATE, probe, probe_bytes, probe_model};
+7 -2
View File
@@ -589,12 +589,17 @@ impl Inner {
// Files on disk are read in place (the index, then samples on demand) rather than
// whole; in-memory files (`add_bytes`) and wasm32 builds open from bytes.
let in_memory = lock(&self.files).contains_key(&**path);
let in_place = if in_memory { None } else { crate::stream::open_source(std::path::Path::new(&**path)) };
let file = std::path::Path::new(&**path);
let in_place = if in_memory { None } else { crate::stream::open_source(file) };
let in_place = in_place.map(|src| {
let turn = crate::stream::movie_header(file).map_or(Orientation::NoTransforms, |h| crate::probe::video_turn(&h));
src.map(|src| (src, turn))
});
let opened = in_place.unwrap_or_else(|| {
self.read(path).and_then(|bytes| {
let name = std::path::Path::new(&**path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
let turn = crate::probe::video_turn(&bytes);
filmcraft_codecs::open_bytes(&name, bytes).map(|src| (src, turn)).map_err(MediaError::from)
filmcraft_codecs::open_bytes(&name, bytes).map(|src| (src, turn)).map_err(MediaError::from)
})
});
match opened {
+10 -3
View File
@@ -18,7 +18,10 @@ pub fn probe(path: impl AsRef<Path>) -> Result<Footage> {
let path = path.as_ref();
// A movie is read in place (its index), not loaded whole.
if let Some(src) = crate::stream::probe_source(path) {
return Ok(footage_from_info(&path.to_string_lossy(), src?.info()));
let mut f = footage_from_info(&path.to_string_lossy(), src?.info());
let turn = crate::stream::movie_header(path).map_or(Orientation::NoTransforms, |h| video_turn(&h));
turn_footage(&mut f, turn);
return Ok(f);
}
let bytes = std::fs::read(path).map_err(|e| MediaError::Io(format!("{}: {e}", path.display())))?;
if crate::MODEL_EXTENSIONS.contains(&ext_of(path).as_str()) {
@@ -94,14 +97,18 @@ pub fn probe_bytes(path: &str, bytes: Arc<[u8]>) -> Result<Footage> {
let turn = video_turn(&bytes);
let src = filmcraft_codecs::open_bytes(&name, bytes)?;
let mut f = footage_from_info(path, src.info());
// The size as displayed: a movie shot with the camera on its side trades width and height.
turn_footage(&mut f, turn);
Ok(f)
}
/// The size as displayed: a movie shot with the camera on its side trades width and height.
fn turn_footage(f: &mut Footage, turn: Orientation) {
if swaps_sides(turn) {
std::mem::swap(&mut f.width, &mut f.height);
if f.pixel_aspect > 0.0 {
f.pixel_aspect = 1.0 / f.pixel_aspect;
}
}
Ok(f)
}
pub(crate) fn footage_from_info(path: &str, info: &filmcraft_media::MediaInfo) -> Footage {
+51
View File
@@ -85,6 +85,57 @@ pub(crate) fn open_source(_path: &Path) -> Option<Result<SharedSource>> {
None
}
/// The leading `ftyp` and the `moov` box of an ISO-BMFF movie on disk (all a track's display
/// matrix needs), without reading the samples: top-level box headers are walked with positional
/// reads. `None` when the file isn't ISO-BMFF or its `moov` is missing or implausibly large.
#[cfg(any(unix, windows))]
pub(crate) fn movie_header(path: &Path) -> Option<Vec<u8>> {
use std::io::{Read, Seek, SeekFrom};
const MAX_MOOV: u64 = 64 << 20;
let mut f = std::fs::File::open(path).ok()?;
let len = f.metadata().ok()?.len();
let mut out = Vec::new();
let mut pos = 0u64;
for _ in 0..4096 {
if pos.checked_add(8)? > len {
return None;
}
f.seek(SeekFrom::Start(pos)).ok()?;
let mut h = [0u8; 16];
f.read_exact(&mut h[..8]).ok()?;
let kind = [h[4], h[5], h[6], h[7]];
let mut size = u64::from(u32::from_be_bytes([h[0], h[1], h[2], h[3]]));
if size == 1 {
f.read_exact(&mut h[8..16]).ok()?;
size = u64::from_be_bytes([h[8], h[9], h[10], h[11], h[12], h[13], h[14], h[15]]);
} else if size == 0 {
size = len - pos;
}
if size < 8 || pos.checked_add(size)? > len {
return None;
}
if kind == *b"ftyp" || kind == *b"moov" {
if size > MAX_MOOV {
return None;
}
f.seek(SeekFrom::Start(pos)).ok()?;
let mut b = vec![0u8; usize::try_from(size).ok()?];
f.read_exact(&mut b).ok()?;
out.extend_from_slice(&b);
if kind == *b"moov" {
return Some(out);
}
}
pos += size;
}
None
}
#[cfg(not(any(unix, windows)))]
pub(crate) fn movie_header(_path: &Path) -> Option<Vec<u8>> {
None
}
/// Probing in place: the opened container when `path` is a movie/audio container a reader opener
/// handles and nothing probed before containers (stills, layered and vector documents, 3D models)
/// claims it. `None`: probe the whole file's bytes as before.