Import: MP4/MOV movies respect their rotation
A movie shot with a phone held upright stores sideways frames and says so in its video track header; it imported lying on its side. Probing now reports the turned size, and decoded frames (full and reduced size) are turned to match. Only the rotation is used, snapped to a quarter turn; mirroring, scaling and offsets in the header are ignored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
683ec372d9
commit
c03b0a7aa7
@@ -104,6 +104,32 @@ pub(crate) fn frame_to_image_in(f: &VideoFrame, op: AlphaOp, mut buf: Vec<Px>) -
|
||||
img
|
||||
}
|
||||
|
||||
/// A movie frame turned the way its file says it is shown ([`crate::probe::video_turn`]:
|
||||
/// clockwise quarter turns; anything else leaves it as decoded).
|
||||
pub(crate) fn turned(img: Image, turn: image::metadata::Orientation) -> Image {
|
||||
use image::metadata::Orientation;
|
||||
let (w, h) = (img.width as usize, img.height as usize);
|
||||
if img.data.len() != w * h || w == 0 || h == 0 {
|
||||
return img;
|
||||
}
|
||||
// The source pixel shown at (x, y) of the turned picture.
|
||||
let (tw, th, from): (usize, usize, fn(usize, usize, usize, usize) -> usize) = match turn {
|
||||
Orientation::Rotate90 => (h, w, |x, y, w, h| (h - 1 - x) * w + y),
|
||||
Orientation::Rotate180 => (w, h, |x, y, w, h| (h - 1 - y) * w + (w - 1 - x)),
|
||||
Orientation::Rotate270 => (h, w, |x, y, w, _| x * w + (w - 1 - y)),
|
||||
_ => return img,
|
||||
};
|
||||
let mut data = vec![[0.0; 4]; tw * th];
|
||||
data.par_chunks_mut(tw).enumerate().for_each(|(y, row)| {
|
||||
for (x, px) in row.iter_mut().enumerate() {
|
||||
if let Some(s) = img.data.get(from(x, y, w, h)) {
|
||||
*px = *s;
|
||||
}
|
||||
}
|
||||
});
|
||||
Image { width: tw as u32, height: th as u32, data }
|
||||
}
|
||||
|
||||
/// A FilmCraft frame for a render that shows it at `w` × `h`, at most half its size each way:
|
||||
/// what `resample(&frame_to_image(f, op), w, h)` makes, converting a quarter of the pixels.
|
||||
/// Opaque 4:2:0 Y'CbCr is box-filtered to half size before conversion (each 2×2 block of luma,
|
||||
|
||||
Reference in New Issue
Block a user