The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.
Rust ffmpegfree-video-editormodel-context-protocolnon-linear-editoroffline-firstopen-source-video-editorself-hostedvideo-editingvideo-editing-automationvideo-editing-softwarevideo-editing-toolsvideo-editorvoice-cloningyoutube-automationyoutube-shorts
fix(media): HDV, 1440-wide AVCHD and DV footage is shown at its true width, not squeezed (#457)
Jareerb8337561dc2c
diff --git a/src/crates/concat-media/fixtures/wide-pixels-hdv.m2ts b/src/crates/concat-media/fixtures/wide-pixels-hdv.m2tsnew file mode 100644index 00000000..42bcefd1Binary files /dev/null and b/src/crates/concat-media/fixtures/wide-pixels-hdv.m2ts differdiff --git a/src/crates/concat-media/src/decode.rs b/src/crates/concat-media/src/decode.rsindex 6a707d5f..4bb61393 100644--- a/src/crates/concat-media/src/decode.rs+++ b/src/crates/concat-media/src/decode.rs@@ -693,6 +693,8 @@ impl Decoder {let time_base = stream.time_base();let start = ffi::start_of(&stream);let rotation = ffi::rotation(&stream);+ // Wide pixels are shown square: see `ffi::sample_aspect`.+ let aspect = ffi::sample_aspect(&stream);let coded = {let parameters = stream.parameters();(parameters.width(), parameters.height())@@ -825,7 +827,7 @@ impl Decoder {let (width, height) = match options.size {Some(size) => size,- None => ffi::displayed(coded.0, coded.1, rotation),+ None => ffi::displayed(coded.0, coded.1, aspect, rotation),};if width == 0 || height == 0 {return Err(Error::Probe {@@ -1528,6 +1530,34 @@ mod tests {);}+ /// A picture coded with wide pixels - HDV and AVCHD's 1440 across, DV's+ /// 720 - is probed and decoded at the width a player shows it, not+ /// squeezed into a pillarbox. The fixture codes 48 by 32 with pixels a+ /// third wider than tall, its left third white: shown 64 by 32, the+ /// white still a third of the width (#457).+ #[test]+ fn a_picture_of_wide_pixels_is_decoded_at_its_shown_width() {+ let path = concat!(env!("CARGO_MANIFEST_DIR"), "/fixtures/wide-pixels-hdv.m2ts");+ let video = crate::probe(path)+ .expect("probes")+ .video+ .expect("a picture");+ assert_eq!((video.width, video.height), (64, 32));+ let mut decoder = Decoder::open(path, &DecodeOptions::default()).expect("opens");+ assert_eq!((decoder.width(), decoder.height()), (64, 32));+ let frame = decoder.next_frame().expect("decodes").expect("a frame");+ assert_eq!((frame.width(), frame.height()), (64, 32));+ let row: Vec<u8> = frame.pixels()[..64 * 4]+ .chunks_exact(4)+ .map(|pixel| pixel[0])+ .collect();+ let white = row.iter().filter(|&&level| level > 128).count();+ assert!(+ (19..=24).contains(&white),+ "{white} of 64 columns are white, not a third"+ );+ }+/// A WebM alpha layer is a second plane the built-in decoders do not/// read; without it the picture arrives opaque and covers the lanes/// below. Both flavours go through libvpx and keep it. Seediff --git a/src/crates/concat-media/src/ffi.rs b/src/crates/concat-media/src/ffi.rsindex 1b22e876..5c22a84a 100644--- a/src/crates/concat-media/src/ffi.rs+++ b/src/crates/concat-media/src/ffi.rs@@ -228,9 +228,49 @@ pub(crate) fn rotation_filters(degrees: i64) -> Option<&'static str> {}}-/// The displayed size of a picture coded at `width` by `height` with the-/// given rotation: a quarter turn swaps the two.-pub(crate) fn displayed(width: u32, height: u32, degrees: i64) -> (u32, u32) {+/// The shape of one coded pixel, width over height, when it is not square.+///+/// HDV and the 1440-wide AVCHD modes code a 1920 by 1080 picture 1440+/// across with pixels a third wider than tall; DV codes 16:9 into 720 by+/// 480. A player stretches the picture back; a decoder hands it over+/// squeezed. The container's word wins over the codec's, as in+/// `av_guess_sample_aspect_ratio`. `None` for square pixels, for a stream+/// that says nothing, and for a shape no camera writes - past four to one+/// either way, which is a broken tag rather than a picture.+pub(crate) fn sample_aspect(stream: &ffmpeg::format::stream::Stream<'_>) -> Option<Rational> {+ let usable = |aspect: ffmpeg::sys::AVRational| {+ (aspect.num > 0 && aspect.den > 0)+ .then(|| Rational::new(i64::from(aspect.num), i64::from(aspect.den)))+ .filter(|aspect| (0.25..=4.0).contains(&aspect.as_f64()))+ };+ // SAFETY: the stream and its codec parameters are valid for the+ // stream's lifetime; both fields are plain rationals.+ let (container, codec) = unsafe {+ let raw = stream.as_ptr();+ (+ (*raw).sample_aspect_ratio,+ (*(*raw).codecpar).sample_aspect_ratio,+ )+ };+ usable(container)+ .or_else(|| usable(codec))+ .filter(|aspect| (aspect.as_f64() - 1.0).abs() > 0.001)+}++/// The displayed size of a picture coded at `width` by `height` with pixels+/// of the shape `aspect` (square for `None`) and the given rotation: the+/// width stretched to square pixels, kept even, as players do; then a+/// quarter turn swaps the two.+pub(crate) fn displayed(+ width: u32,+ height: u32,+ aspect: Option<Rational>,+ degrees: i64,+) -> (u32, u32) {+ let width = match aspect {+ Some(aspect) => ((f64::from(width) * aspect.as_f64() / 2.0).round() as u32 * 2).max(2),+ None => width,+ };if degrees.rem_euclid(180) == 90 {(height, width)} else {@@ -250,10 +290,28 @@ mod tests {#[test]fn a_quarter_turn_swaps_the_dimensions_and_a_half_turn_does_not() {- assert_eq!(displayed(1920, 1080, 90), (1080, 1920));- assert_eq!(displayed(1920, 1080, 270), (1080, 1920));- assert_eq!(displayed(1920, 1080, 180), (1920, 1080));- assert_eq!(displayed(1920, 1080, 0), (1920, 1080));+ assert_eq!(displayed(1920, 1080, None, 90), (1080, 1920));+ assert_eq!(displayed(1920, 1080, None, 270), (1080, 1920));+ assert_eq!(displayed(1920, 1080, None, 180), (1920, 1080));+ assert_eq!(displayed(1920, 1080, None, 0), (1920, 1080));+ }++ /// Wide pixels are stretched to square ones, as a player shows them:+ /// HDV's 1440 to 1920, widescreen DV's 720 to 854 (853.3, kept even),+ /// and the stretch comes before a quarter turn.+ #[test]+ fn a_picture_of_wide_pixels_is_shown_at_its_true_width() {+ let hdv = Some(Rational::new(4, 3));+ assert_eq!(displayed(1440, 1080, hdv, 0), (1920, 1080));+ assert_eq!(displayed(1440, 1080, hdv, 90), (1080, 1920));+ assert_eq!(+ displayed(720, 480, Some(Rational::new(32, 27)), 0),+ (854, 480)+ );+ assert_eq!(+ displayed(720, 480, Some(Rational::new(8, 9)), 0),+ (640, 480)+ );}#[test]diff --git a/src/crates/concat-media/src/probe.rs b/src/crates/concat-media/src/probe.rsindex 38544ee4..f8568bab 100644--- a/src/crates/concat-media/src/probe.rs+++ b/src/crates/concat-media/src/probe.rs@@ -20,8 +20,9 @@ pub struct VideoStream {pub index: u32,/// Codec short name, for example `h264`.pub codec: String,- /// Displayed width in pixels: the coded width, swapped with the height- /// when the stream carries a quarter-turn display rotation.+ /// Displayed width in pixels: the coded width stretched to square+ /// pixels where the stream's are not (HDV, DV), then swapped with the+ /// height when the stream carries a quarter-turn display rotation.pub width: u32,/// Displayed height in pixels; see [`VideoStream::width`].pub height: u32,@@ -171,7 +172,12 @@ fn video_stream(stream: &ffmpeg::format::stream::Stream<'_>, path: &Path) -> Resdetail: "video stream has no usable size".to_owned(),});}- let (width, height) = ffi::displayed(width, height, ffi::rotation(stream));+ let (width, height) = ffi::displayed(+ width,+ height,+ ffi::sample_aspect(stream),+ ffi::rotation(stream),+ );let frame_rate = pick_rate(rational(stream.avg_frame_rate()), rational(stream.rate())).ok_or_else(|| Error::Probe {