2020-12-22 11:44:59 +00:00
|
|
|
// Copyright (C) 2020 Julien Bardagi <julien.bardagi@gmail.com>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2022-01-15 19:18:47 +00:00
|
|
|
//
|
2022-03-14 08:22:53 +00:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
2020-12-22 11:44:59 +00:00
|
|
|
|
2021-06-03 18:20:54 +00:00
|
|
|
use gst::glib;
|
2020-12-22 11:44:59 +00:00
|
|
|
use gst::prelude::*;
|
|
|
|
use gst::subclass::prelude::*;
|
|
|
|
|
|
|
|
use gst_base::subclass::prelude::*;
|
2021-10-16 12:24:11 +00:00
|
|
|
use gst_video::subclass::prelude::*;
|
2020-12-22 11:44:59 +00:00
|
|
|
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
|
|
|
use super::super::hsvutils;
|
|
|
|
|
|
|
|
// Default values of properties
|
|
|
|
const DEFAULT_HUE_REF: f32 = 0.0;
|
|
|
|
const DEFAULT_HUE_VAR: f32 = 10.0;
|
|
|
|
const DEFAULT_SATURATION_REF: f32 = 0.0;
|
|
|
|
const DEFAULT_SATURATION_VAR: f32 = 0.15;
|
|
|
|
const DEFAULT_VALUE_REF: f32 = 0.0;
|
|
|
|
const DEFAULT_VALUE_VAR: f32 = 0.3;
|
|
|
|
|
|
|
|
// Property value storage
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
struct Settings {
|
|
|
|
hue_ref: f32,
|
|
|
|
hue_var: f32,
|
|
|
|
saturation_ref: f32,
|
|
|
|
saturation_var: f32,
|
|
|
|
value_ref: f32,
|
|
|
|
value_var: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Settings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Settings {
|
|
|
|
hue_ref: DEFAULT_HUE_REF,
|
|
|
|
hue_var: DEFAULT_HUE_VAR,
|
|
|
|
saturation_ref: DEFAULT_SATURATION_REF,
|
|
|
|
saturation_var: DEFAULT_SATURATION_VAR,
|
|
|
|
value_ref: DEFAULT_VALUE_REF,
|
|
|
|
value_var: DEFAULT_VALUE_VAR,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Struct containing all the element data
|
2021-03-07 16:22:24 +00:00
|
|
|
#[derive(Default)]
|
2020-12-22 11:44:59 +00:00
|
|
|
pub struct HsvDetector {
|
|
|
|
settings: Mutex<Settings>,
|
|
|
|
}
|
|
|
|
|
|
|
|
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
|
|
|
|
gst::DebugCategory::new(
|
|
|
|
"hsvdetector",
|
|
|
|
gst::DebugColorFlags::empty(),
|
|
|
|
Some("Rust HSV-based detection filter"),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2021-03-07 16:22:24 +00:00
|
|
|
#[glib::object_subclass]
|
2020-12-22 11:44:59 +00:00
|
|
|
impl ObjectSubclass for HsvDetector {
|
|
|
|
const NAME: &'static str = "HsvDetector";
|
|
|
|
type Type = super::HsvDetector;
|
2021-10-16 12:24:11 +00:00
|
|
|
type ParentType = gst_video::VideoFilter;
|
2021-01-21 18:21:29 +00:00
|
|
|
}
|
2020-12-22 11:44:59 +00:00
|
|
|
|
2022-07-22 11:07:49 +00:00
|
|
|
fn video_input_formats() -> impl IntoIterator<Item = gst_video::VideoFormat> {
|
|
|
|
[
|
2020-12-23 18:58:41 +00:00
|
|
|
gst_video::VideoFormat::Rgbx,
|
|
|
|
gst_video::VideoFormat::Xrgb,
|
|
|
|
gst_video::VideoFormat::Bgrx,
|
|
|
|
gst_video::VideoFormat::Xbgr,
|
|
|
|
gst_video::VideoFormat::Rgb,
|
|
|
|
gst_video::VideoFormat::Bgr,
|
2022-07-22 11:07:49 +00:00
|
|
|
]
|
2020-12-23 18:58:41 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 11:07:49 +00:00
|
|
|
fn video_output_formats() -> impl IntoIterator<Item = gst_video::VideoFormat> {
|
|
|
|
[
|
2020-12-23 18:58:41 +00:00
|
|
|
gst_video::VideoFormat::Rgba,
|
|
|
|
gst_video::VideoFormat::Argb,
|
|
|
|
gst_video::VideoFormat::Bgra,
|
|
|
|
gst_video::VideoFormat::Abgr,
|
2022-07-22 11:07:49 +00:00
|
|
|
]
|
2020-12-23 18:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HsvDetector {
|
|
|
|
#[inline]
|
|
|
|
fn hsv_detect<CF, DF>(
|
|
|
|
&self,
|
|
|
|
in_frame: &gst_video::video_frame::VideoFrameRef<&gst::buffer::BufferRef>,
|
|
|
|
out_frame: &mut gst_video::video_frame::VideoFrameRef<&mut gst::buffer::BufferRef>,
|
|
|
|
to_hsv: CF,
|
|
|
|
apply_alpha: DF,
|
|
|
|
) where
|
|
|
|
CF: Fn(&[u8]) -> [f32; 3],
|
|
|
|
DF: Fn(&[u8], &mut [u8], u8),
|
|
|
|
{
|
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
|
|
|
|
// Keep the various metadata we need for working with the video frames in
|
|
|
|
// local variables. This saves some typing below.
|
|
|
|
let width = in_frame.width() as usize;
|
|
|
|
let in_stride = in_frame.plane_stride()[0] as usize;
|
|
|
|
let in_data = in_frame.plane_data(0).unwrap();
|
|
|
|
let out_stride = out_frame.plane_stride()[0] as usize;
|
|
|
|
let out_data = out_frame.plane_data_mut(0).unwrap();
|
|
|
|
let nb_input_channels = in_frame.format_info().pixel_stride()[0] as usize;
|
|
|
|
|
|
|
|
assert_eq!(out_data.len() / out_stride, in_data.len() / in_stride);
|
|
|
|
assert_eq!(in_data.len() % nb_input_channels, 0);
|
|
|
|
|
|
|
|
let in_line_bytes = width * nb_input_channels;
|
|
|
|
let out_line_bytes = width * 4;
|
|
|
|
|
|
|
|
assert!(in_line_bytes <= in_stride);
|
|
|
|
assert!(out_line_bytes <= out_stride);
|
|
|
|
|
|
|
|
for (in_line, out_line) in in_data
|
|
|
|
.chunks_exact(in_stride)
|
|
|
|
.zip(out_data.chunks_exact_mut(out_stride))
|
|
|
|
{
|
|
|
|
for (in_p, out_p) in in_line[..in_line_bytes]
|
|
|
|
.chunks_exact(nb_input_channels)
|
|
|
|
.zip(out_line[..out_line_bytes].chunks_exact_mut(4))
|
|
|
|
{
|
|
|
|
let hsv = to_hsv(in_p);
|
|
|
|
|
|
|
|
// We handle hue being circular here
|
|
|
|
let ref_hue_offset = 180.0 - settings.hue_ref;
|
|
|
|
let mut shifted_hue = hsv[0] + ref_hue_offset;
|
|
|
|
|
|
|
|
if shifted_hue < 0.0 {
|
|
|
|
shifted_hue += 360.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
shifted_hue %= 360.0;
|
|
|
|
|
|
|
|
if (shifted_hue - 180.0).abs() <= settings.hue_var
|
|
|
|
&& (hsv[1] - settings.saturation_ref).abs() <= settings.saturation_var
|
|
|
|
&& (hsv[2] - settings.value_ref).abs() <= settings.value_var
|
|
|
|
{
|
|
|
|
apply_alpha(in_p, out_p, 255);
|
|
|
|
} else {
|
|
|
|
apply_alpha(in_p, out_p, 0);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 18:21:29 +00:00
|
|
|
impl ObjectImpl for HsvDetector {
|
|
|
|
fn properties() -> &'static [glib::ParamSpec] {
|
|
|
|
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
|
|
|
|
vec![
|
2021-11-20 10:25:14 +00:00
|
|
|
glib::ParamSpecFloat::new(
|
2021-01-21 18:21:29 +00:00
|
|
|
"hue-ref",
|
|
|
|
"Hue reference",
|
|
|
|
"Hue reference in degrees",
|
|
|
|
f32::MIN,
|
|
|
|
f32::MAX,
|
|
|
|
DEFAULT_HUE_REF,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_PLAYING,
|
2020-12-22 11:44:59 +00:00
|
|
|
),
|
2021-11-20 10:25:14 +00:00
|
|
|
glib::ParamSpecFloat::new(
|
2021-01-21 18:21:29 +00:00
|
|
|
"hue-var",
|
|
|
|
"Hue variation",
|
|
|
|
"Allowed hue variation from the reference hue angle, in degrees",
|
|
|
|
0.0,
|
|
|
|
180.0,
|
|
|
|
DEFAULT_HUE_VAR,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_PLAYING,
|
2020-12-22 11:44:59 +00:00
|
|
|
),
|
2021-11-20 10:25:14 +00:00
|
|
|
glib::ParamSpecFloat::new(
|
2021-01-21 18:21:29 +00:00
|
|
|
"saturation-ref",
|
|
|
|
"Saturation reference",
|
|
|
|
"Reference saturation value",
|
|
|
|
0.0,
|
|
|
|
1.0,
|
|
|
|
DEFAULT_SATURATION_REF,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_PLAYING,
|
2020-12-22 11:44:59 +00:00
|
|
|
),
|
2021-11-20 10:25:14 +00:00
|
|
|
glib::ParamSpecFloat::new(
|
2021-01-21 18:21:29 +00:00
|
|
|
"saturation-var",
|
|
|
|
"Saturation variation",
|
|
|
|
"Allowed saturation variation from the reference value",
|
|
|
|
0.0,
|
|
|
|
1.0,
|
|
|
|
DEFAULT_SATURATION_VAR,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_PLAYING,
|
2020-12-22 11:44:59 +00:00
|
|
|
),
|
2021-11-20 10:25:14 +00:00
|
|
|
glib::ParamSpecFloat::new(
|
2021-01-21 18:21:29 +00:00
|
|
|
"value-ref",
|
|
|
|
"Value reference",
|
|
|
|
"Reference value value",
|
|
|
|
0.0,
|
|
|
|
1.0,
|
|
|
|
DEFAULT_VALUE_REF,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_PLAYING,
|
2021-01-21 18:21:29 +00:00
|
|
|
),
|
2021-11-20 10:25:14 +00:00
|
|
|
glib::ParamSpecFloat::new(
|
2021-01-21 18:21:29 +00:00
|
|
|
"value-var",
|
|
|
|
"Value variation",
|
|
|
|
"Allowed value variation from the reference value",
|
|
|
|
0.0,
|
|
|
|
1.0,
|
|
|
|
DEFAULT_VALUE_VAR,
|
2021-01-31 12:44:45 +00:00
|
|
|
glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_PLAYING,
|
2021-01-21 18:21:29 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
});
|
2020-12-22 11:44:59 +00:00
|
|
|
|
2021-01-21 18:21:29 +00:00
|
|
|
PROPERTIES.as_ref()
|
2020-12-22 11:44:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 18:21:29 +00:00
|
|
|
fn set_property(
|
|
|
|
&self,
|
|
|
|
obj: &Self::Type,
|
|
|
|
_id: usize,
|
|
|
|
value: &glib::Value,
|
|
|
|
pspec: &glib::ParamSpec,
|
|
|
|
) {
|
2021-04-12 12:49:54 +00:00
|
|
|
match pspec.name() {
|
2021-01-21 18:21:29 +00:00
|
|
|
"hue-ref" => {
|
2020-12-22 11:44:59 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
let hue_ref = value.get().expect("type checked upstream");
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::info!(
|
2020-12-22 11:44:59 +00:00
|
|
|
CAT,
|
|
|
|
obj: obj,
|
|
|
|
"Changing hue-ref from {} to {}",
|
|
|
|
settings.hue_ref,
|
|
|
|
hue_ref
|
|
|
|
);
|
|
|
|
settings.hue_ref = hue_ref;
|
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"hue-var" => {
|
2020-12-22 11:44:59 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
let hue_var = value.get().expect("type checked upstream");
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::info!(
|
2020-12-22 11:44:59 +00:00
|
|
|
CAT,
|
|
|
|
obj: obj,
|
|
|
|
"Changing hue-var from {} to {}",
|
|
|
|
settings.hue_var,
|
|
|
|
hue_var
|
|
|
|
);
|
|
|
|
settings.hue_var = hue_var;
|
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"saturation-ref" => {
|
2020-12-22 11:44:59 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
let saturation_ref = value.get().expect("type checked upstream");
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::info!(
|
2020-12-22 11:44:59 +00:00
|
|
|
CAT,
|
|
|
|
obj: obj,
|
|
|
|
"Changing saturation-ref from {} to {}",
|
|
|
|
settings.saturation_ref,
|
|
|
|
saturation_ref
|
|
|
|
);
|
|
|
|
settings.saturation_ref = saturation_ref;
|
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"saturation-var" => {
|
2020-12-22 11:44:59 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
let saturation_var = value.get().expect("type checked upstream");
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::info!(
|
2020-12-22 11:44:59 +00:00
|
|
|
CAT,
|
|
|
|
obj: obj,
|
|
|
|
"Changing saturation-var from {} to {}",
|
|
|
|
settings.saturation_var,
|
|
|
|
saturation_var
|
|
|
|
);
|
|
|
|
settings.saturation_var = saturation_var;
|
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"value-ref" => {
|
2020-12-22 11:44:59 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
let value_ref = value.get().expect("type checked upstream");
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::info!(
|
2020-12-22 11:44:59 +00:00
|
|
|
CAT,
|
|
|
|
obj: obj,
|
|
|
|
"Changing value-ref from {} to {}",
|
|
|
|
settings.value_ref,
|
|
|
|
value_ref
|
|
|
|
);
|
|
|
|
settings.value_ref = value_ref;
|
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"value-var" => {
|
2020-12-22 11:44:59 +00:00
|
|
|
let mut settings = self.settings.lock().unwrap();
|
2021-04-25 12:41:22 +00:00
|
|
|
let value_var = value.get().expect("type checked upstream");
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::info!(
|
2020-12-22 11:44:59 +00:00
|
|
|
CAT,
|
|
|
|
obj: obj,
|
|
|
|
"Changing value-var from {} to {}",
|
|
|
|
settings.value_var,
|
|
|
|
value_var
|
|
|
|
);
|
|
|
|
settings.value_var = value_var;
|
|
|
|
}
|
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called whenever a value of a property is read. It can be called
|
|
|
|
// at any time from any thread.
|
2021-04-20 12:57:40 +00:00
|
|
|
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
|
2021-04-12 12:49:54 +00:00
|
|
|
match pspec.name() {
|
2021-01-21 18:21:29 +00:00
|
|
|
"hue-ref" => {
|
2020-12-22 11:44:59 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
settings.hue_ref.to_value()
|
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"hue-var" => {
|
2020-12-22 11:44:59 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
settings.hue_var.to_value()
|
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"saturation-ref" => {
|
2020-12-22 11:44:59 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
settings.saturation_ref.to_value()
|
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"saturation-var" => {
|
2020-12-22 11:44:59 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
settings.saturation_var.to_value()
|
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"value-ref" => {
|
2020-12-22 11:44:59 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
settings.value_ref.to_value()
|
|
|
|
}
|
2021-01-21 18:21:29 +00:00
|
|
|
"value-var" => {
|
2020-12-22 11:44:59 +00:00
|
|
|
let settings = self.settings.lock().unwrap();
|
|
|
|
settings.value_var.to_value()
|
|
|
|
}
|
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 08:57:31 +00:00
|
|
|
impl GstObjectImpl for HsvDetector {}
|
|
|
|
|
2021-01-21 18:21:29 +00:00
|
|
|
impl ElementImpl for HsvDetector {
|
|
|
|
fn metadata() -> Option<&'static gst::subclass::ElementMetadata> {
|
|
|
|
static ELEMENT_METADATA: Lazy<gst::subclass::ElementMetadata> = Lazy::new(|| {
|
|
|
|
gst::subclass::ElementMetadata::new(
|
|
|
|
"HSV detector",
|
|
|
|
"Filter/Effect/Converter/Video",
|
|
|
|
"Works within the HSV colorspace to mark positive pixels",
|
|
|
|
"Julien Bardagi <julien.bardagi@gmail.com>",
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(&*ELEMENT_METADATA)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pad_templates() -> &'static [gst::PadTemplate] {
|
|
|
|
static PAD_TEMPLATES: Lazy<Vec<gst::PadTemplate>> = Lazy::new(|| {
|
2022-07-22 11:07:49 +00:00
|
|
|
let caps = gst_video::VideoCapsBuilder::new()
|
|
|
|
.format_list(video_output_formats())
|
2021-11-06 07:34:10 +00:00
|
|
|
.build();
|
2021-01-21 18:21:29 +00:00
|
|
|
|
|
|
|
let src_pad_template = gst::PadTemplate::new(
|
|
|
|
"src",
|
|
|
|
gst::PadDirection::Src,
|
|
|
|
gst::PadPresence::Always,
|
|
|
|
&caps,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// sink pad capabilities
|
2022-07-22 11:07:49 +00:00
|
|
|
let caps = gst_video::VideoCapsBuilder::new()
|
|
|
|
.format_list(video_input_formats())
|
2021-11-06 07:34:10 +00:00
|
|
|
.build();
|
2021-01-21 18:21:29 +00:00
|
|
|
|
|
|
|
let sink_pad_template = gst::PadTemplate::new(
|
|
|
|
"sink",
|
|
|
|
gst::PadDirection::Sink,
|
|
|
|
gst::PadPresence::Always,
|
|
|
|
&caps,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
vec![src_pad_template, sink_pad_template]
|
|
|
|
});
|
|
|
|
|
|
|
|
PAD_TEMPLATES.as_ref()
|
|
|
|
}
|
|
|
|
}
|
2020-12-22 11:44:59 +00:00
|
|
|
|
|
|
|
impl BaseTransformImpl for HsvDetector {
|
2021-01-21 18:21:29 +00:00
|
|
|
const MODE: gst_base::subclass::BaseTransformMode =
|
|
|
|
gst_base::subclass::BaseTransformMode::NeverInPlace;
|
|
|
|
const PASSTHROUGH_ON_SAME_CAPS: bool = false;
|
|
|
|
const TRANSFORM_IP_ON_PASSTHROUGH: bool = false;
|
|
|
|
|
2020-12-22 11:44:59 +00:00
|
|
|
fn transform_caps(
|
|
|
|
&self,
|
|
|
|
element: &Self::Type,
|
|
|
|
direction: gst::PadDirection,
|
|
|
|
caps: &gst::Caps,
|
|
|
|
filter: Option<&gst::Caps>,
|
|
|
|
) -> Option<gst::Caps> {
|
2021-06-04 22:16:05 +00:00
|
|
|
let mut other_caps = caps.clone();
|
|
|
|
if direction == gst::PadDirection::Src {
|
|
|
|
for s in other_caps.make_mut().iter_mut() {
|
2022-07-22 11:07:49 +00:00
|
|
|
s.set("format", gst::List::new(video_input_formats()));
|
2020-12-22 11:44:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-06-04 22:16:05 +00:00
|
|
|
for s in other_caps.make_mut().iter_mut() {
|
2022-07-22 11:07:49 +00:00
|
|
|
s.set("format", gst::List::new(video_output_formats()));
|
2020-12-22 11:44:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2020-12-22 11:44:59 +00:00
|
|
|
CAT,
|
|
|
|
obj: element,
|
|
|
|
"Transformed caps from {} to {} in direction {:?}",
|
|
|
|
caps,
|
|
|
|
other_caps,
|
|
|
|
direction
|
|
|
|
);
|
|
|
|
|
|
|
|
// In the end we need to filter the caps through an optional filter caps to get rid of any
|
|
|
|
// unwanted caps.
|
|
|
|
if let Some(filter) = filter {
|
|
|
|
Some(filter.intersect_with_mode(&other_caps, gst::CapsIntersectMode::First))
|
|
|
|
} else {
|
|
|
|
Some(other_caps)
|
|
|
|
}
|
|
|
|
}
|
2021-10-16 12:24:11 +00:00
|
|
|
}
|
2020-12-22 11:44:59 +00:00
|
|
|
|
2021-10-16 12:24:11 +00:00
|
|
|
impl VideoFilterImpl for HsvDetector {
|
|
|
|
fn transform_frame(
|
2020-12-22 11:44:59 +00:00
|
|
|
&self,
|
2021-10-16 12:24:11 +00:00
|
|
|
_element: &Self::Type,
|
|
|
|
in_frame: &gst_video::VideoFrameRef<&gst::BufferRef>,
|
|
|
|
out_frame: &mut gst_video::VideoFrameRef<&mut gst::BufferRef>,
|
2020-12-22 11:44:59 +00:00
|
|
|
) -> Result<gst::FlowSuccess, gst::FlowError> {
|
2021-10-16 12:24:11 +00:00
|
|
|
match in_frame.format() {
|
2020-12-23 18:58:41 +00:00
|
|
|
gst_video::VideoFormat::Rgbx | gst_video::VideoFormat::Rgb => {
|
2021-10-16 12:24:11 +00:00
|
|
|
match out_frame.format() {
|
2020-12-23 18:58:41 +00:00
|
|
|
gst_video::VideoFormat::Rgba => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_rgb(
|
|
|
|
in_p[..3].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[..3].copy_from_slice(&in_p[..3]);
|
|
|
|
out_p[3] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
gst_video::VideoFormat::Argb => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_rgb(
|
|
|
|
in_p[..3].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[1..4].copy_from_slice(&in_p[..3]);
|
|
|
|
out_p[0] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
gst_video::VideoFormat::Bgra => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_rgb(
|
|
|
|
in_p[..3].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[0] = in_p[2];
|
|
|
|
out_p[1] = in_p[1];
|
|
|
|
out_p[2] = in_p[0];
|
|
|
|
out_p[3] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
gst_video::VideoFormat::Abgr => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_rgb(
|
|
|
|
in_p[..3].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[1] = in_p[2];
|
|
|
|
out_p[2] = in_p[1];
|
|
|
|
out_p[3] = in_p[0];
|
|
|
|
out_p[0] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
2020-12-22 11:44:59 +00:00
|
|
|
}
|
2020-12-23 18:58:41 +00:00
|
|
|
}
|
|
|
|
gst_video::VideoFormat::Xrgb => {
|
2021-10-16 12:24:11 +00:00
|
|
|
match out_frame.format() {
|
2020-12-23 18:58:41 +00:00
|
|
|
gst_video::VideoFormat::Rgba => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_rgb(
|
|
|
|
in_p[1..4].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[..3].copy_from_slice(&in_p[1..4]);
|
|
|
|
out_p[3] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
gst_video::VideoFormat::Argb => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_rgb(
|
|
|
|
in_p[1..4].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[1..4].copy_from_slice(&in_p[1..4]);
|
|
|
|
out_p[0] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
gst_video::VideoFormat::Bgra => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_rgb(
|
|
|
|
in_p[1..4].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[0] = in_p[3];
|
|
|
|
out_p[1] = in_p[2];
|
|
|
|
out_p[2] = in_p[1];
|
|
|
|
out_p[3] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
gst_video::VideoFormat::Abgr => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_rgb(
|
|
|
|
in_p[1..4].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[1] = in_p[3];
|
|
|
|
out_p[2] = in_p[2];
|
|
|
|
out_p[3] = in_p[1];
|
|
|
|
out_p[0] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
2020-12-22 11:44:59 +00:00
|
|
|
};
|
|
|
|
}
|
2020-12-23 18:58:41 +00:00
|
|
|
gst_video::VideoFormat::Bgrx | gst_video::VideoFormat::Bgr => {
|
2021-10-16 12:24:11 +00:00
|
|
|
match out_frame.format() {
|
2020-12-23 18:58:41 +00:00
|
|
|
gst_video::VideoFormat::Rgba => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_bgr(
|
|
|
|
in_p[..3].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[0] = in_p[2];
|
|
|
|
out_p[1] = in_p[1];
|
|
|
|
out_p[2] = in_p[0];
|
|
|
|
out_p[3] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
gst_video::VideoFormat::Argb => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_bgr(
|
|
|
|
in_p[..3].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[1] = in_p[2];
|
|
|
|
out_p[2] = in_p[1];
|
|
|
|
out_p[3] = in_p[0];
|
|
|
|
out_p[0] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
gst_video::VideoFormat::Bgra => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_bgr(
|
|
|
|
in_p[..3].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[..3].copy_from_slice(&in_p[..3]);
|
|
|
|
out_p[3] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
gst_video::VideoFormat::Abgr => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_bgr(
|
|
|
|
in_p[..3].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[1..4].copy_from_slice(&in_p[..3]);
|
|
|
|
out_p[0] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2021-10-16 12:24:11 +00:00
|
|
|
gst_video::VideoFormat::Xbgr => match out_frame.format() {
|
2020-12-23 18:58:41 +00:00
|
|
|
gst_video::VideoFormat::Rgba => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_bgr(
|
|
|
|
in_p[1..4].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[0] = in_p[3];
|
|
|
|
out_p[1] = in_p[2];
|
|
|
|
out_p[2] = in_p[1];
|
|
|
|
out_p[3] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
gst_video::VideoFormat::Argb => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_bgr(
|
|
|
|
in_p[1..4].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[1] = in_p[3];
|
|
|
|
out_p[2] = in_p[2];
|
|
|
|
out_p[3] = in_p[1];
|
|
|
|
out_p[0] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
gst_video::VideoFormat::Bgra => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_bgr(
|
|
|
|
in_p[1..4].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[..3].copy_from_slice(&in_p[1..4]);
|
|
|
|
out_p[3] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
gst_video::VideoFormat::Abgr => {
|
|
|
|
self.hsv_detect(
|
2021-10-16 12:24:11 +00:00
|
|
|
in_frame,
|
|
|
|
out_frame,
|
2020-12-23 18:58:41 +00:00
|
|
|
|in_p| {
|
|
|
|
hsvutils::from_bgr(
|
|
|
|
in_p[1..4].try_into().expect("slice with incorrect length"),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|in_p, out_p, val| {
|
|
|
|
out_p[1..4].copy_from_slice(&in_p[1..4]);
|
|
|
|
out_p[0] = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2020-12-22 11:44:59 +00:00
|
|
|
|
|
|
|
Ok(gst::FlowSuccess::Ok)
|
|
|
|
}
|
|
|
|
}
|