Welcome to mirror list, hosted at ThFree Co, Russian Federation.

imp.rs « hsvfilter « src « hsv « video - gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 391de8a92184443c612c37e805d85e9cf71c69aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// 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.
//
// SPDX-License-Identifier: MIT/Apache-2.0

use gst::glib;
use gst::gst_info;
use gst::prelude::*;
use gst::subclass::prelude::*;
use gst_base::subclass::prelude::*;
use gst_video::subclass::prelude::*;

use std::i32;
use std::sync::Mutex;

use once_cell::sync::Lazy;

use super::super::hsvutils;

// Default values of properties
const DEFAULT_HUE_SHIFT: f32 = 0.0;
const DEFAULT_SATURATION_MUL: f32 = 1.0;
const DEFAULT_SATURATION_OFF: f32 = 0.0;
const DEFAULT_VALUE_MUL: f32 = 1.0;
const DEFAULT_VALUE_OFF: f32 = 0.0;

// Property value storage
#[derive(Debug, Clone, Copy)]
struct Settings {
    hue_shift: f32,
    saturation_mul: f32,
    saturation_off: f32,
    value_mul: f32,
    value_off: f32,
}

impl Default for Settings {
    fn default() -> Self {
        Settings {
            hue_shift: DEFAULT_HUE_SHIFT,
            saturation_mul: DEFAULT_SATURATION_MUL,
            saturation_off: DEFAULT_SATURATION_OFF,
            value_mul: DEFAULT_VALUE_MUL,
            value_off: DEFAULT_VALUE_OFF,
        }
    }
}

// Struct containing all the element data
#[derive(Default)]
pub struct HsvFilter {
    settings: Mutex<Settings>,
}

static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
    gst::DebugCategory::new(
        "hsvfilter",
        gst::DebugColorFlags::empty(),
        Some("Rust HSV transformation filter"),
    )
});

#[glib::object_subclass]
impl ObjectSubclass for HsvFilter {
    const NAME: &'static str = "HsvFilter";
    type Type = super::HsvFilter;
    type ParentType = gst_video::VideoFilter;
}

impl HsvFilter {
    #[inline]
    fn hsv_filter<CF, FF>(
        &self,
        frame: &mut gst_video::video_frame::VideoFrameRef<&mut gst::buffer::BufferRef>,
        to_hsv: CF,
        apply_filter: FF,
    ) where
        CF: Fn(&[u8]) -> [f32; 3],
        FF: Fn(&[f32; 3], &mut [u8]),
    {
        let settings = *self.settings.lock().unwrap();

        let width = frame.width() as usize;
        let stride = frame.plane_stride()[0] as usize;
        let nb_channels = frame.format_info().pixel_stride()[0] as usize;
        let data = frame.plane_data_mut(0).unwrap();

        assert_eq!(data.len() % nb_channels, 0);

        let line_bytes = width * nb_channels;

        for line in data.chunks_exact_mut(stride) {
            for p in line[..line_bytes].chunks_exact_mut(nb_channels) {
                assert_eq!(p.len(), nb_channels);

                let mut hsv = to_hsv(p);

                hsv[0] = (hsv[0] + settings.hue_shift) % 360.0;
                if hsv[0] < 0.0 {
                    hsv[0] += 360.0;
                }
                hsv[1] = hsvutils::Clamp::clamp(
                    settings.saturation_mul * hsv[1] + settings.saturation_off,
                    0.0,
                    1.0,
                );
                hsv[2] = hsvutils::Clamp::clamp(
                    settings.value_mul * hsv[2] + settings.value_off,
                    0.0,
                    1.0,
                );

                apply_filter(&hsv, p);
            }
        }
    }
}

impl ObjectImpl for HsvFilter {
    fn properties() -> &'static [glib::ParamSpec] {
        static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
            vec![
                glib::ParamSpecFloat::new(
                    "hue-shift",
                    "Hue shift",
                    "Hue shifting in degrees",
                    f32::MIN,
                    f32::MAX,
                    DEFAULT_HUE_SHIFT,
                    glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_PLAYING,
                ),
                glib::ParamSpecFloat::new(
                    "saturation-mul",
                    "Saturation multiplier",
                    "Saturation multiplier to apply to the saturation value (before offset)",
                    f32::MIN,
                    f32::MAX,
                    DEFAULT_SATURATION_MUL,
                    glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_PLAYING,
                ),
                glib::ParamSpecFloat::new(
                    "saturation-off",
                    "Saturation offset",
                    "Saturation offset to add to the saturation value (after multiplier)",
                    f32::MIN,
                    f32::MAX,
                    DEFAULT_SATURATION_OFF,
                    glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_PLAYING,
                ),
                glib::ParamSpecFloat::new(
                    "value-mul",
                    "Value multiplier",
                    "Value multiplier to apply to the value (before offset)",
                    f32::MIN,
                    f32::MAX,
                    DEFAULT_VALUE_MUL,
                    glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_PLAYING,
                ),
                glib::ParamSpecFloat::new(
                    "value-off",
                    "Value offset",
                    "Value offset to add to the value (after multiplier)",
                    f32::MIN,
                    f32::MAX,
                    DEFAULT_VALUE_OFF,
                    glib::ParamFlags::READWRITE | gst::PARAM_FLAG_MUTABLE_PLAYING,
                ),
            ]
        });

        PROPERTIES.as_ref()
    }

    fn set_property(
        &self,
        obj: &Self::Type,
        _id: usize,
        value: &glib::Value,
        pspec: &glib::ParamSpec,
    ) {
        match pspec.name() {
            "hue-shift" => {
                let mut settings = self.settings.lock().unwrap();
                let hue_shift = value.get().expect("type checked upstream");
                gst_info!(
                    CAT,
                    obj: obj,
                    "Changing hue-shift from {} to {}",
                    settings.hue_shift,
                    hue_shift
                );
                settings.hue_shift = hue_shift;
            }
            "saturation-mul" => {
                let mut settings = self.settings.lock().unwrap();
                let saturation_mul = value.get().expect("type checked upstream");
                gst_info!(
                    CAT,
                    obj: obj,
                    "Changing saturation-mul from {} to {}",
                    settings.saturation_mul,
                    saturation_mul
                );
                settings.saturation_mul = saturation_mul;
            }
            "saturation-off" => {
                let mut settings = self.settings.lock().unwrap();
                let saturation_off = value.get().expect("type checked upstream");
                gst_info!(
                    CAT,
                    obj: obj,
                    "Changing saturation-off from {} to {}",
                    settings.saturation_off,
                    saturation_off
                );
                settings.saturation_off = saturation_off;
            }
            "value-mul" => {
                let mut settings = self.settings.lock().unwrap();
                let value_mul = value.get().expect("type checked upstream");
                gst_info!(
                    CAT,
                    obj: obj,
                    "Changing value-mul from {} to {}",
                    settings.value_mul,
                    value_mul
                );
                settings.value_mul = value_mul;
            }
            "value-off" => {
                let mut settings = self.settings.lock().unwrap();
                let value_off = value.get().expect("type checked upstream");
                gst_info!(
                    CAT,
                    obj: obj,
                    "Changing value-off from {} to {}",
                    settings.value_off,
                    value_off
                );
                settings.value_off = value_off;
            }
            _ => unimplemented!(),
        }
    }

    // Called whenever a value of a property is read. It can be called
    // at any time from any thread.
    fn property(&self, _obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
        match pspec.name() {
            "hue-shift" => {
                let settings = self.settings.lock().unwrap();
                settings.hue_shift.to_value()
            }
            "saturation-mul" => {
                let settings = self.settings.lock().unwrap();
                settings.saturation_mul.to_value()
            }
            "saturation-off" => {
                let settings = self.settings.lock().unwrap();
                settings.saturation_off.to_value()
            }
            "value-mul" => {
                let settings = self.settings.lock().unwrap();
                settings.value_mul.to_value()
            }
            "value-off" => {
                let settings = self.settings.lock().unwrap();
                settings.value_off.to_value()
            }
            _ => unimplemented!(),
        }
    }
}

impl GstObjectImpl for HsvFilter {}

impl ElementImpl for HsvFilter {
    fn metadata() -> Option<&'static gst::subclass::ElementMetadata> {
        static ELEMENT_METADATA: Lazy<gst::subclass::ElementMetadata> = Lazy::new(|| {
            gst::subclass::ElementMetadata::new(
                "HSV filter",
                "Filter/Effect/Converter/Video",
                "Works within the HSV colorspace to apply tranformations to incoming frames",
                "Julien Bardagi <julien.bardagi@gmail.com>",
            )
        });

        Some(&*ELEMENT_METADATA)
    }

    fn pad_templates() -> &'static [gst::PadTemplate] {
        static PAD_TEMPLATES: Lazy<Vec<gst::PadTemplate>> = Lazy::new(|| {
            // src pad capabilities
            let caps = gst::Caps::builder("video/x-raw")
                .field(
                    "format",
                    gst::List::new([
                        gst_video::VideoFormat::Rgbx.to_str(),
                        gst_video::VideoFormat::Xrgb.to_str(),
                        gst_video::VideoFormat::Bgrx.to_str(),
                        gst_video::VideoFormat::Xbgr.to_str(),
                        gst_video::VideoFormat::Rgba.to_str(),
                        gst_video::VideoFormat::Argb.to_str(),
                        gst_video::VideoFormat::Bgra.to_str(),
                        gst_video::VideoFormat::Abgr.to_str(),
                        gst_video::VideoFormat::Rgb.to_str(),
                        gst_video::VideoFormat::Bgr.to_str(),
                    ]),
                )
                .field("width", gst::IntRange::new(0, i32::MAX))
                .field("height", gst::IntRange::new(0, i32::MAX))
                .field(
                    "framerate",
                    gst::FractionRange::new(
                        gst::Fraction::new(0, 1),
                        gst::Fraction::new(i32::MAX, 1),
                    ),
                )
                .build();

            let src_pad_template = gst::PadTemplate::new(
                "src",
                gst::PadDirection::Src,
                gst::PadPresence::Always,
                &caps,
            )
            .unwrap();

            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()
    }
}

impl BaseTransformImpl for HsvFilter {
    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;
}

impl VideoFilterImpl for HsvFilter {
    fn transform_frame_ip(
        &self,
        _element: &Self::Type,
        frame: &mut gst_video::VideoFrameRef<&mut gst::BufferRef>,
    ) -> Result<gst::FlowSuccess, gst::FlowError> {
        match frame.format() {
            gst_video::VideoFormat::Rgbx
            | gst_video::VideoFormat::Rgba
            | gst_video::VideoFormat::Rgb => {
                self.hsv_filter(
                    frame,
                    |p| hsvutils::from_rgb(p[..3].try_into().expect("slice with incorrect length")),
                    |hsv, p| {
                        p[..3].copy_from_slice(&hsvutils::to_rgb(hsv));
                    },
                );
            }
            gst_video::VideoFormat::Xrgb | gst_video::VideoFormat::Argb => {
                self.hsv_filter(
                    frame,
                    |p| {
                        hsvutils::from_rgb(p[1..4].try_into().expect("slice with incorrect length"))
                    },
                    |hsv, p| {
                        p[1..4].copy_from_slice(&hsvutils::to_rgb(hsv));
                    },
                );
            }
            gst_video::VideoFormat::Bgrx
            | gst_video::VideoFormat::Bgra
            | gst_video::VideoFormat::Bgr => {
                self.hsv_filter(
                    frame,
                    |p| hsvutils::from_bgr(p[..3].try_into().expect("slice with incorrect length")),
                    |hsv, p| {
                        p[..3].copy_from_slice(&hsvutils::to_bgr(hsv));
                    },
                );
            }
            gst_video::VideoFormat::Xbgr | gst_video::VideoFormat::Abgr => {
                self.hsv_filter(
                    frame,
                    |p| {
                        hsvutils::from_bgr(p[1..4].try_into().expect("slice with incorrect length"))
                    },
                    |hsv, p| {
                        p[1..4].copy_from_slice(&hsvutils::to_bgr(hsv));
                    },
                );
            }
            _ => unreachable!(),
        }

        Ok(gst::FlowSuccess::Ok)
    }
}