From 24536fef732423fe404689308fa571042e354a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Tue, 18 Mar 2025 15:36:00 +0200 Subject: [PATCH] colordetect: Change to videofilter base class And also work in passthrough mode to not require all input buffers to be writable unnecessarily. Part-of: --- docs/plugins/gst_plugins_cache.json | 1 + video/videofx/src/colordetect/imp.rs | 39 +++++++++++++--------------- video/videofx/src/colordetect/mod.rs | 2 +- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/docs/plugins/gst_plugins_cache.json b/docs/plugins/gst_plugins_cache.json index 745f4243d..77ca80978 100644 --- a/docs/plugins/gst_plugins_cache.json +++ b/docs/plugins/gst_plugins_cache.json @@ -12799,6 +12799,7 @@ "description": "Detects the dominant color of a video", "hierarchy": [ "GstColorDetect", + "GstVideoFilter", "GstBaseTransform", "GstElement", "GstObject", diff --git a/video/videofx/src/colordetect/imp.rs b/video/videofx/src/colordetect/imp.rs index 75cdea679..7219d8845 100644 --- a/video/videofx/src/colordetect/imp.rs +++ b/video/videofx/src/colordetect/imp.rs @@ -44,7 +44,6 @@ impl Default for Settings { struct State { color_format: ColorFormat, - out_info: gst_video::VideoInfo, current_color: Option, } @@ -57,7 +56,7 @@ pub struct ColorDetect { impl ColorDetect { fn detect_color( &self, - buf: &mut gst::BufferRef, + frame: &gst_video::VideoFrameRef<&gst::BufferRef>, ) -> Result)>, gst::FlowError> { let mut state_guard = self.state.borrow_mut(); let state = state_guard.as_mut().ok_or_else(|| { @@ -66,8 +65,6 @@ impl ColorDetect { })?; let settings = *self.settings.lock().unwrap(); - let frame = - gst_video::VideoFrameRef::from_buffer_ref_readable(buf, &state.out_info).unwrap(); let palette = get_palette( frame.plane_data(0).unwrap(), state.color_format, @@ -119,7 +116,7 @@ impl ColorDetect { impl ObjectSubclass for ColorDetect { const NAME: &'static str = "GstColorDetect"; type Type = super::ColorDetect; - type ParentType = gst_base::BaseTransform; + type ParentType = gst_video::VideoFilter; } impl ObjectImpl for ColorDetect { @@ -249,26 +246,24 @@ impl ElementImpl for ColorDetect { impl BaseTransformImpl for ColorDetect { const MODE: gst_base::subclass::BaseTransformMode = gst_base::subclass::BaseTransformMode::AlwaysInPlace; - const PASSTHROUGH_ON_SAME_CAPS: bool = false; - const TRANSFORM_IP_ON_PASSTHROUGH: bool = false; + const PASSTHROUGH_ON_SAME_CAPS: bool = true; + const TRANSFORM_IP_ON_PASSTHROUGH: bool = true; fn stop(&self) -> Result<(), gst::ErrorMessage> { *self.state.borrow_mut() = None; gst::info!(CAT, imp = self, "Stopped"); Ok(()) } +} - fn set_caps(&self, incaps: &gst::Caps, outcaps: &gst::Caps) -> Result<(), gst::LoggableError> { - let in_info = match gst_video::VideoInfo::from_caps(incaps) { - Err(_) => return Err(gst::loggable_error!(CAT, "Failed to parse input caps")), - Ok(info) => info, - }; - - let out_info = match gst_video::VideoInfo::from_caps(outcaps) { - Err(_) => return Err(gst::loggable_error!(CAT, "Failed to parse output caps")), - Ok(info) => info, - }; - +impl VideoFilterImpl for ColorDetect { + fn set_info( + &self, + incaps: &gst::Caps, + in_info: &gst_video::VideoInfo, + outcaps: &gst::Caps, + _out_info: &gst_video::VideoInfo, + ) -> Result<(), gst::LoggableError> { gst::debug!( CAT, imp = self, @@ -292,15 +287,17 @@ impl BaseTransformImpl for ColorDetect { }; *self.state.borrow_mut() = Some(State { color_format, - out_info, current_color: previous_color, }); Ok(()) } - fn transform_ip(&self, buf: &mut gst::BufferRef) -> Result { - if let Some((dominant_color_name, palette)) = self.detect_color(buf)? { + fn transform_frame_ip_passthrough( + &self, + frame: &gst_video::VideoFrameRef<&gst::BufferRef>, + ) -> Result { + if let Some((dominant_color_name, palette)) = self.detect_color(frame)? { self.color_changed(&dominant_color_name, palette); } diff --git a/video/videofx/src/colordetect/mod.rs b/video/videofx/src/colordetect/mod.rs index 689fa3572..2d716ca9c 100644 --- a/video/videofx/src/colordetect/mod.rs +++ b/video/videofx/src/colordetect/mod.rs @@ -13,7 +13,7 @@ use gst::prelude::*; pub mod imp; glib::wrapper! { - pub struct ColorDetect(ObjectSubclass) @extends gst_base::BaseTransform, gst::Element, gst::Object; + pub struct ColorDetect(ObjectSubclass) @extends gst_video::VideoFilter, gst_base::BaseTransform, gst::Element, gst::Object; } pub fn register(plugin: &gst::Plugin) -> Result<(), glib::BoolError> {