From 635b31614cdbdb3bed4419ee32a5207dab80b7b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Tue, 19 Sep 2023 23:30:12 +0300 Subject: [PATCH] video: Fix ordering of video formats according to latest libgstvideo Part-of: --- gstreamer-video/src/video_format.rs | 89 +++++++++++----------- gstreamer-video/src/video_format_info.rs | 95 ++++++++++++------------ 2 files changed, 91 insertions(+), 93 deletions(-) diff --git a/gstreamer-video/src/video_format.rs b/gstreamer-video/src/video_format.rs index addc4c4c7..4e8518c81 100644 --- a/gstreamer-video/src/video_format.rs +++ b/gstreamer-video/src/video_format.rs @@ -39,13 +39,13 @@ pub static VIDEO_FORMATS_ALL: Lazy> = Lazy::new(|| { #[cfg(feature = "v1_16")] crate::VideoFormat::Y410, crate::VideoFormat::Gbra, - crate::VideoFormat::Abgr, + crate::VideoFormat::Ayuv, #[cfg(feature = "v1_16")] crate::VideoFormat::Vuya, - crate::VideoFormat::Bgra, - crate::VideoFormat::Ayuv, - crate::VideoFormat::Argb, crate::VideoFormat::Rgba, + crate::VideoFormat::Argb, + crate::VideoFormat::Bgra, + crate::VideoFormat::Abgr, crate::VideoFormat::A420, crate::VideoFormat::V216, crate::VideoFormat::Y44412le, @@ -66,46 +66,46 @@ pub static VIDEO_FORMATS_ALL: Lazy> = Lazy::new(|| { crate::VideoFormat::Nv1610le32, #[cfg(feature = "v1_16")] crate::VideoFormat::Y210, - crate::VideoFormat::V210, crate::VideoFormat::Uyvp, + crate::VideoFormat::V210, crate::VideoFormat::I42010le, crate::VideoFormat::I42010be, crate::VideoFormat::P01010le, - crate::VideoFormat::Nv1210le32, #[cfg(feature = "v1_16")] crate::VideoFormat::Nv1210le40, + crate::VideoFormat::Nv1210le32, crate::VideoFormat::P01010be, crate::VideoFormat::Y444, crate::VideoFormat::Gbr, crate::VideoFormat::Nv24, - crate::VideoFormat::Xbgr, - crate::VideoFormat::Bgrx, - crate::VideoFormat::Xrgb, - crate::VideoFormat::Rgbx, - crate::VideoFormat::Bgr, - crate::VideoFormat::Iyu2, crate::VideoFormat::V308, + crate::VideoFormat::Iyu2, + crate::VideoFormat::Rgbx, + crate::VideoFormat::Xrgb, + crate::VideoFormat::Bgrx, + crate::VideoFormat::Xbgr, crate::VideoFormat::Rgb, + crate::VideoFormat::Bgr, crate::VideoFormat::Y42b, - crate::VideoFormat::Nv61, crate::VideoFormat::Nv16, - crate::VideoFormat::Vyuy, - crate::VideoFormat::Uyvy, - crate::VideoFormat::Yvyu, + crate::VideoFormat::Nv61, crate::VideoFormat::Yuy2, + crate::VideoFormat::Yvyu, + crate::VideoFormat::Uyvy, + crate::VideoFormat::Vyuy, crate::VideoFormat::I420, crate::VideoFormat::Yv12, - crate::VideoFormat::Nv21, crate::VideoFormat::Nv12, + crate::VideoFormat::Nv21, crate::VideoFormat::Nv1264z32, crate::VideoFormat::Y41b, crate::VideoFormat::Iyu1, - crate::VideoFormat::Yvu9, crate::VideoFormat::Yuv9, - crate::VideoFormat::Rgb16, + crate::VideoFormat::Yvu9, crate::VideoFormat::Bgr16, - crate::VideoFormat::Rgb15, + crate::VideoFormat::Rgb16, crate::VideoFormat::Bgr15, + crate::VideoFormat::Rgb15, crate::VideoFormat::Rgb8p, crate::VideoFormat::Gray16Le, crate::VideoFormat::Gray16Be, @@ -129,17 +129,17 @@ pub static VIDEO_FORMATS_ALL: Lazy> = Lazy::new(|| { crate::VideoFormat::A42010be, crate::VideoFormat::A42010le, #[cfg(feature = "v1_16")] - crate::VideoFormat::Y410, - #[cfg(feature = "v1_16")] crate::VideoFormat::Bgr10a2Le, + #[cfg(feature = "v1_16")] + crate::VideoFormat::Y410, crate::VideoFormat::Gbra, - crate::VideoFormat::Abgr, + crate::VideoFormat::Ayuv, #[cfg(feature = "v1_16")] crate::VideoFormat::Vuya, - crate::VideoFormat::Bgra, - crate::VideoFormat::Ayuv, - crate::VideoFormat::Argb, crate::VideoFormat::Rgba, + crate::VideoFormat::Argb, + crate::VideoFormat::Bgra, + crate::VideoFormat::Abgr, crate::VideoFormat::A420, crate::VideoFormat::V216, crate::VideoFormat::Y44412be, @@ -160,46 +160,46 @@ pub static VIDEO_FORMATS_ALL: Lazy> = Lazy::new(|| { crate::VideoFormat::Nv1610le32, #[cfg(feature = "v1_16")] crate::VideoFormat::Y210, - crate::VideoFormat::V210, crate::VideoFormat::Uyvp, + crate::VideoFormat::V210, crate::VideoFormat::I42010be, crate::VideoFormat::I42010le, crate::VideoFormat::P01010be, - crate::VideoFormat::P01010le, - crate::VideoFormat::Nv1210le32, #[cfg(feature = "v1_16")] crate::VideoFormat::Nv1210le40, + crate::VideoFormat::Nv1210le32, + crate::VideoFormat::P01010le, crate::VideoFormat::Y444, crate::VideoFormat::Gbr, crate::VideoFormat::Nv24, - crate::VideoFormat::Xbgr, - crate::VideoFormat::Bgrx, - crate::VideoFormat::Xrgb, - crate::VideoFormat::Rgbx, - crate::VideoFormat::Bgr, - crate::VideoFormat::Iyu2, crate::VideoFormat::V308, + crate::VideoFormat::Iyu2, + crate::VideoFormat::Rgbx, + crate::VideoFormat::Xrgb, + crate::VideoFormat::Bgrx, + crate::VideoFormat::Xbgr, crate::VideoFormat::Rgb, + crate::VideoFormat::Bgr, crate::VideoFormat::Y42b, - crate::VideoFormat::Nv61, crate::VideoFormat::Nv16, - crate::VideoFormat::Vyuy, - crate::VideoFormat::Uyvy, - crate::VideoFormat::Yvyu, + crate::VideoFormat::Nv61, crate::VideoFormat::Yuy2, + crate::VideoFormat::Yvyu, + crate::VideoFormat::Uyvy, + crate::VideoFormat::Vyuy, crate::VideoFormat::I420, crate::VideoFormat::Yv12, - crate::VideoFormat::Nv21, crate::VideoFormat::Nv12, + crate::VideoFormat::Nv21, crate::VideoFormat::Nv1264z32, crate::VideoFormat::Y41b, crate::VideoFormat::Iyu1, - crate::VideoFormat::Yvu9, crate::VideoFormat::Yuv9, - crate::VideoFormat::Rgb16, + crate::VideoFormat::Yvu9, crate::VideoFormat::Bgr16, - crate::VideoFormat::Rgb15, + crate::VideoFormat::Rgb16, crate::VideoFormat::Bgr15, + crate::VideoFormat::Rgb15, crate::VideoFormat::Rgb8p, crate::VideoFormat::Gray16Be, crate::VideoFormat::Gray16Le, @@ -523,7 +523,6 @@ mod tests { assert_eq!(caps.to_string(), "video/x-raw, format=(string){ NV12, NV16 }, width=(int)[ 1, 2147483647 ], height=(int)[ 1, 2147483647 ], framerate=(fraction)[ 0/1, 2147483647/1 ]"); } - #[cfg(feature = "v1_18")] #[test] fn sort() { use itertools::Itertools; @@ -535,6 +534,8 @@ mod tests { > crate::VideoFormatInfo::from_format(crate::VideoFormat::Nv12) ); assert!(crate::VideoFormat::I420 > crate::VideoFormat::Yv12); + assert!(crate::VideoFormat::Nv12 > crate::VideoFormat::Nv21); + assert!(crate::VideoFormat::Xrgb > crate::VideoFormat::Rgb); let sorted: Vec = crate::VideoFormat::iter_raw().sorted().rev().collect(); diff --git a/gstreamer-video/src/video_format_info.rs b/gstreamer-video/src/video_format_info.rs index de00e6fc4..083b8bf8e 100644 --- a/gstreamer-video/src/video_format_info.rs +++ b/gstreamer-video/src/video_format_info.rs @@ -395,48 +395,21 @@ impl Ord for VideoFormatInfo { fn cmp(&self, other: &Self) -> Ordering { self.n_components() .cmp(&other.n_components()) - .then_with(|| self.depth().cmp(other.depth())) - .then_with(|| self.w_sub().cmp(other.w_sub()).reverse()) - .then_with(|| self.h_sub().cmp(other.h_sub()).reverse()) - .then_with(|| self.n_planes().cmp(&other.n_planes())) + .reverse() + .then_with(|| self.depth().cmp(other.depth()).reverse()) + .then_with(|| self.w_sub().cmp(other.w_sub())) + .then_with(|| self.h_sub().cmp(other.h_sub())) + .then_with(|| self.n_planes().cmp(&other.n_planes()).reverse()) .then_with(|| { - // Format using native endianness is considered as bigger + // Format using native endianness is considered smaller + let native_endianness = [crate::VideoFormat::Ayuv64, crate::VideoFormat::Argb64]; + let want_le = cfg!(target_endian = "little"); + match ( - self.flags().contains(crate::VideoFormatFlags::LE), - other.flags().contains(crate::VideoFormatFlags::LE), - ) { - (true, false) => { - // a LE, b BE - #[cfg(target_endian = "little")] - { - Ordering::Greater - } - #[cfg(target_endian = "big")] - { - Ordering::Less - } - } - (false, true) => { - // a BE, b LE - #[cfg(target_endian = "little")] - { - Ordering::Less - } - #[cfg(target_endian = "big")] - { - Ordering::Greater - } - } - _ => Ordering::Equal, - } - }) - .then_with(|| self.pixel_stride().cmp(other.pixel_stride())) - .then_with(|| self.poffset().cmp(other.poffset())) - .then_with(|| { - // Prefer non-complex formats - match ( - self.flags().contains(crate::VideoFormatFlags::COMPLEX), - other.flags().contains(crate::VideoFormatFlags::COMPLEX), + self.flags().contains(crate::VideoFormatFlags::LE) == want_le + || native_endianness.contains(&self.format()), + other.flags().contains(crate::VideoFormatFlags::LE) == want_le + || native_endianness.contains(&other.format()), ) { (true, false) => Ordering::Less, (false, true) => Ordering::Greater, @@ -444,32 +417,56 @@ impl Ord for VideoFormatInfo { } }) .then_with(|| { - // tiebreaker: YUV > RGB + // Prefer non-complex formats + match ( + self.flags().contains(crate::VideoFormatFlags::COMPLEX), + other.flags().contains(crate::VideoFormatFlags::COMPLEX), + ) { + (true, false) => Ordering::Greater, + (false, true) => Ordering::Less, + _ => Ordering::Equal, + } + }) + .then_with(|| { + // Prefer RGB over YUV if self.flags().contains(crate::VideoFormatFlags::RGB) && other.flags().contains(crate::VideoFormatFlags::YUV) { - Ordering::Less + Ordering::Greater } else if self.flags().contains(crate::VideoFormatFlags::YUV) && other.flags().contains(crate::VideoFormatFlags::RGB) { - Ordering::Greater + Ordering::Less } else { Ordering::Equal } }) .then_with(|| { - // Manual tiebreaker - match (self.format(), other.format()) { - // I420 is more commonly used in GStreamer - (crate::VideoFormat::I420, crate::VideoFormat::Yv12) => Ordering::Greater, - (crate::VideoFormat::Yv12, crate::VideoFormat::I420) => Ordering::Less, - _ => Ordering::Equal, + // Prefer xRGB and permutations over RGB and permutations + let xrgb = [ + crate::VideoFormat::Xrgb, + crate::VideoFormat::Xbgr, + crate::VideoFormat::Rgbx, + crate::VideoFormat::Bgrx, + ]; + let rgb = [crate::VideoFormat::Rgb, crate::VideoFormat::Bgr]; + + if xrgb.contains(&self.format()) && rgb.contains(&other.format()) { + Ordering::Less + } else if rgb.contains(&self.format()) && xrgb.contains(&other.format()) { + Ordering::Greater + } else { + Ordering::Equal } }) + .then_with(|| self.pixel_stride().cmp(other.pixel_stride())) + .then_with(|| self.poffset().cmp(other.poffset())) .then_with(|| { // tie, sort by name self.name().cmp(other.name()) }) + // and reverse the whole ordering so that "better quality" > "lower quality" + .reverse() } }