diff --git a/crates/engine/src/decode.rs b/crates/engine/src/decode.rs index 887afeb..f0760a8 100644 --- a/crates/engine/src/decode.rs +++ b/crates/engine/src/decode.rs @@ -17,6 +17,7 @@ use crate::hw::HwSession; use crate::project::Speed; use crate::scale::Composer; use crate::tonemap::{self, ToneMapper}; +use crate::transform::TransformParams; /// One decoded picture, ready to hand to a renderer. pub struct Frame { @@ -176,6 +177,7 @@ struct SpanCmd { start: u32, end: u32, color: ColorParams, + transform: TransformParams, canvas: Composer, tone: tonemap::Preset, /// The clip's own playback speed ([`crate::project::Clip::speed`]), for @@ -308,6 +310,7 @@ impl Worker { start: u32, end: u32, color: ColorParams, + transform: TransformParams, canvas: Composer, tone: tonemap::Preset, speed: Speed, @@ -331,6 +334,7 @@ impl Worker { start, end, color, + transform, canvas, tone, speed, @@ -433,6 +437,9 @@ impl DecodeSession { start_frame, end_frame, ColorParams::default(), + // No clip, no transform either: the file-level API hands back the + // pictures the file holds, untouched. + TransformParams::default(), // No project, no canvas: the file-level API hands back the // pictures the file holds, at the size it holds them. Composer::passthrough(), @@ -512,6 +519,7 @@ impl DecodeSession { start_frame: u32, len: u32, color: ColorParams, + transform: TransformParams, canvas: Composer, ) -> crate::Result { let still = Still::open(path)?; @@ -538,6 +546,7 @@ impl DecodeSession { // the project is watching. let first = Render::new( color, + transform, ColorDescription::default(), canvas, tonemap::Preset::default(), @@ -610,6 +619,7 @@ impl DecodeSession { start_frame: u32, end_frame: u32, color: ColorParams, + transform: TransformParams, canvas: Composer, tone: tonemap::Preset, ) -> crate::Result<(VideoMeta, FrameStream)> { @@ -650,6 +660,7 @@ impl DecodeSession { start_frame, end_frame, color, + transform, canvas, tone, // A file-level open has no clip and no speed to skip frames @@ -677,6 +688,7 @@ impl DecodeSession { start_frame: u32, end_frame: u32, color: ColorParams, + transform: TransformParams, canvas: Composer, tone: tonemap::Preset, speed: Speed, @@ -687,6 +699,7 @@ impl DecodeSession { start_frame, end_frame, color, + transform, canvas, tone, speed, @@ -710,6 +723,7 @@ fn span_worker( start_frame: u32, end_frame: u32, color: ColorParams, + transform: TransformParams, canvas: Composer, tone: tonemap::Preset, speed: Speed, @@ -740,6 +754,7 @@ fn span_worker( start: start_frame, end: end_frame, color, + transform, canvas, tone, speed, @@ -919,6 +934,7 @@ fn run_span( start, end, color, + transform, canvas, tone, speed, @@ -947,7 +963,7 @@ fn run_span( // The stream's own colour and peak brightness: properties of the stream, so // neither can change while one range decodes -- but the grade, the canvas // and the rendition are the *span's*, so this is built per span. - let mut render = Render::new(color, opened.meta.color, canvas, tone, opened.peak); + let mut render = Render::new(color, transform, opened.meta.color, canvas, tone, opened.peak); if let Some(reused) = opened.position_hw(path, start) { // Cancelled during the init (or the seek): leave without decoding. if abort.hit() { @@ -1007,6 +1023,9 @@ fn run_span( /// project resolution, byte for byte and allocation for allocation. struct Render { color: ColorParams, + /// This clip's placement, on top of whatever the canvas's own fit policy + /// already does ([`crate::scale::Composer::place_transformed`]). + transform: TransformParams, /// What the source's samples mean -- the stream's own matrix and range, /// which is what the conversion below is done in rather than the BT.601 it /// used to assume of every file. @@ -1021,6 +1040,10 @@ struct Render { /// across the worker's whole range. Empty unless the clip needs one: it is /// tone-mapped in place, graded in place, or both. graded: (Vec, Vec, Vec), + /// Where [`Composer::place_transformed`]'s owned picture lands, so a + /// transformed frame's planes live as long as `self` -- matching what + /// [`Composer::place`] already hands back by borrowing `self.canvas`. + placed: (Vec, Vec, Vec), } /// What [`Render::frame`] converts a tone-mapped picture in: the tone map's @@ -1038,6 +1061,7 @@ impl Render { /// reference rendition reads (see [`tonemap::Preset`]). fn new( color: ColorParams, + transform: TransformParams, desc: ColorDescription, canvas: Composer, preset: tonemap::Preset, @@ -1045,6 +1069,7 @@ impl Render { ) -> Self { Self { color, + transform, desc, canvas, // corner-cut: the ceiling is that the map reads limited-range codes @@ -1058,6 +1083,7 @@ impl Render { Transfer::Hlg => Some(ToneMapper::new(tonemap::Transfer::Hlg, preset, peak)), }, graded: (Vec::new(), Vec::new(), Vec::new()), + placed: (Vec::new(), Vec::new(), Vec::new()), } } @@ -1071,6 +1097,12 @@ impl Render { height: u32, ) -> Frame { let passthrough = self.canvas.is_passthrough(width, height); + // A transform still has a picture to place even where the canvas + // itself would hand this size back untouched -- moving, scaling, + // rotating or cropping a passthrough-sized clip is still a placement, + // and must not be silently dropped by the fused conversion below. + let transform_active = !self.transform.is_identity(); + let skip_placement = passthrough && !transform_active; // Everything that has to happen to the samples before the canvas sees // them, on one copy: the tone map (HDR streams only) and, unless the // conversion below can fuse it, the grade. An SDR stream that is either @@ -1080,7 +1112,8 @@ impl Render { // picture a viewer is shown, so its brightness and saturation mean what // they say in the SDR the tone map just produced, not in the 10-stop // HDR the file was in. - let (y, u, v, desc) = if self.tone.is_some() || (!passthrough && !self.color.is_identity()) + let (y, u, v, desc) = if self.tone.is_some() + || (!skip_placement && !self.color.is_identity()) { let (gy, gu, gv) = &mut self.graded; gy.clear(); @@ -1104,7 +1137,7 @@ impl Render { } else { (y, u, v, self.desc) }; - if passthrough { + if skip_placement { return Frame { index, width, @@ -1120,7 +1153,15 @@ impl Render { ), }; } - let (y, u, v, width, height) = self.canvas.place(y, u, v, width, height); + let (y, u, v, width, height) = if transform_active { + let (py, pu, pv, w, h) = self + .canvas + .place_transformed(y, u, v, width, height, &self.transform); + self.placed = (py, pu, pv); + (&self.placed.0[..], &self.placed.1[..], &self.placed.2[..], w, h) + } else { + self.canvas.place(y, u, v, width, height) + }; Frame { index, width, @@ -1384,6 +1425,7 @@ mod tests { 0, u32::MAX, ColorParams::default(), + TransformParams::default(), Composer::passthrough(), tonemap::Preset::default(), Speed::NORMAL, @@ -1405,6 +1447,7 @@ mod tests { 0, u32::MAX, ColorParams::default(), + TransformParams::default(), Composer::passthrough(), tonemap::Preset::default(), ) @@ -1453,6 +1496,7 @@ mod tests { 0, u32::MAX, ColorParams::default(), + TransformParams::default(), Composer::passthrough(), tonemap::Preset::default(), Speed::NORMAL, @@ -1536,6 +1580,7 @@ mod tests { 0, u32::MAX, ColorParams::default(), + TransformParams::default(), Composer::passthrough(), tonemap::Preset::default(), ) diff --git a/crates/engine/src/export.rs b/crates/engine/src/export.rs index 32a9a6c..2438167 100644 --- a/crates/engine/src/export.rs +++ b/crates/engine/src/export.rs @@ -82,6 +82,7 @@ use crate::project::{Lane, LaneKind, Project, Rate}; use crate::scale::{blend_i420, Composer}; use crate::subtitle::{Cue, SubtitleTrack}; use crate::tonemap::{self, ToneMapper}; +use crate::transform::TransformParams; /// Progress is reported in permille: an atomic integer the render loop can read /// without a lock, fine enough for any progress bar. @@ -1083,8 +1084,10 @@ struct Dissolve<'a> { mapper: Option<&'a ToneMapper>, remap: Option<(Matrix, Matrix)>, grade: Option, + transform: TransformParams, canvas: Composer, graded: (Vec, Vec, Vec), + transformed: (Vec, Vec, Vec), } /// The track's cues where the *exported* timeline puts them, which is the only @@ -1643,6 +1646,14 @@ impl CopyPlan { { return None; } + // A moved, scaled, rotated or cropped clip is a different picture + // than the source's own packets, exactly as a grade is. + if project + .composite_transform_at(span.start) + .is_some_and(|params| !params.is_identity()) + { + return None; + } let path = &entries.get(source)?.path; if sources.get(source)?.is_none() { if !crate::demux::is_matroska(path) { @@ -2537,6 +2548,9 @@ fn run( && project .composite_color_at(span.start) .is_none_or(|params| params.is_identity()) + && project + .composite_transform_at(span.start) + .is_none_or(|params| params.is_identity()) && dissolve.is_none(); let want = untouched.then(|| encoder.dma_want(meta)).flatten(); // Opened at the file's own frame, which is the only place the @@ -2566,7 +2580,12 @@ fn run( // is touched. Scratch outside the frame loop: it is refilled per frame // and its allocation survives the whole span. let grade = project.composite_color_at(span.start).copied(); + let transform = project + .composite_transform_at(span.start) + .copied() + .unwrap_or_default(); let mut graded = (Vec::new(), Vec::new(), Vec::new()); + let mut transformed = (Vec::new(), Vec::new(), Vec::new()); // ...and the canvas it is placed on, which is where a source of another // resolution becomes a picture at the project's. The same `Composer` // playback composes with, given the same policy, so an export is what @@ -2595,12 +2614,17 @@ fn run( mapper: b_mapper, remap: b_remap, grade: project.composite_color_at(b_start).copied(), + transform: project + .composite_transform_at(b_start) + .copied() + .unwrap_or_default(), canvas: Composer::new( meta.width, meta.height, project.composite_fit_at(b_start), ), graded: (Vec::new(), Vec::new(), Vec::new()), + transformed: (Vec::new(), Vec::new(), Vec::new()), }) }) .transpose()?; @@ -2688,9 +2712,11 @@ fn run( width, height, grade, + transform, remap, mapper, &mut graded, + &mut transformed, &mut canvas, )), }; @@ -2718,8 +2744,9 @@ fn run( } Some(Frame::Pixels(by, bu, bv, bw, bh)) => { let (by, bu, bv, _, _) = place_picture( - by, bu, bv, bw, bh, d.grade, d.remap, d.mapper, - &mut d.graded, &mut d.canvas, + by, bu, bv, bw, bh, d.grade, d.transform, d.remap, + d.mapper, &mut d.graded, &mut d.transformed, + &mut d.canvas, ); let idx_in_window = done_here + r - d.tail_start; let t = dissolve_weight(idx_in_window, d.window); @@ -2854,9 +2881,11 @@ fn place_picture<'a>( width: u32, height: u32, grade: Option, + transform: TransformParams, remap: Option<(Matrix, Matrix)>, mapper: Option<&ToneMapper>, graded: &'a mut (Vec, Vec, Vec), + transformed: &'a mut (Vec, Vec, Vec), canvas: &'a mut Composer, ) -> (&'a [u8], &'a [u8], &'a [u8], u32, u32) { // The planes are borrowed from the decoder (and `Black::picture` hands the @@ -2891,7 +2920,13 @@ fn place_picture<'a>( }; // Grade first, place second: the grade is the clip's own pixels and the // bars around them are not the clip (see `scale::Composer`). - canvas.place(y, u, v, width, height) + if transform.is_identity() { + canvas.place(y, u, v, width, height) + } else { + let (py, pu, pv, w, h) = canvas.place_transformed(y, u, v, width, height, &transform); + *transformed = (py, pu, pv); + (&transformed.0[..], &transformed.1[..], &transformed.2[..], w, h) + } } /// Which matrix pair a span's samples are rewritten by on the way into a file diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index ddea7e1..14b36af 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -28,6 +28,7 @@ use crate::demux::{Codec, Demuxer, VideoMeta}; use crate::eq::EqParams; use crate::project::{Clip, Edge, Lane, LaneKind, Project, Rate, Source, Span, Speed, SubClip}; use crate::scale::{Composer, FitPolicy}; +use crate::transform::TransformParams; /// How long the feeder waits out a full ring. The ring holds a second, so this /// only has to be short next to that; it costs one wakeup per 10 ms of audio. @@ -510,6 +511,7 @@ impl PlaybackSession { 0, u32::MAX, ColorParams::default(), + TransformParams::default(), Composer::passthrough(), // The default rendition: a file just opened is a project nobody has // picked one for, exactly as it is a project nobody has graded. @@ -738,6 +740,7 @@ impl PlaybackSession { 0, span.map_or(1, |s| s.len), ColorParams::default(), + TransformParams::default(), Composer::passthrough(), )?; Ok(Self { @@ -870,6 +873,7 @@ impl PlaybackSession { 0, 1, ColorParams::default(), + TransformParams::default(), Composer::passthrough(), )?; (meta, stream) @@ -879,6 +883,7 @@ impl PlaybackSession { 0, u32::MAX, ColorParams::default(), + TransformParams::default(), Composer::passthrough(), // The saved rendition, from the document rather than from the // project (which is built below): this placeholder is what the @@ -1603,6 +1608,10 @@ impl PlaybackSession { .composite_color_at(start) .copied() .unwrap_or_default(), + self.project + .composite_transform_at(start) + .copied() + .unwrap_or_default(), Composer::new( self.meta.width, self.meta.height, @@ -1646,6 +1655,11 @@ impl PlaybackSession { .composite_color_at(start) .copied() .unwrap_or_default(); + let transform = self + .project + .composite_transform_at(start) + .copied() + .unwrap_or_default(); // ...and the canvas it is placed on: the project's resolution // and this clip's own fit policy, constant across the span for // the reason the grade is. Built where it is handed over rather @@ -1684,6 +1698,7 @@ impl PlaybackSession { start_frame, end_frame, color, + transform, canvas(), tone, decode_speed, @@ -1698,6 +1713,7 @@ impl PlaybackSession { start_frame, end_frame, color, + transform, canvas(), tone, decode_speed, @@ -3578,6 +3594,7 @@ fn matches_timeline( 0, 0, ColorParams::default(), + TransformParams::default(), Composer::passthrough(), // A zero-length range decodes nothing, so no table is built and the // rendition cannot matter here.