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

imp.rs « colordetect « src « videofx « video - github.com/sdroege/gst-plugin-rs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f3112c86751036fe95abd3e02959f31bf1b07f0f (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
// Copyright (C) 2022, Igalia S.L
//      Author: Philippe Normand <philn@igalia.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at
// <https://mozilla.org/MPL/2.0/>.
//
// SPDX-License-Identifier: MPL-2.0

use atomic_refcell::AtomicRefCell;
use color_name;
use color_thief::{get_palette, Color, ColorFormat};
use gst::{glib, subclass::prelude::*};
use gst_base::prelude::*;
use gst_video::{subclass::prelude::*, VideoFormat};
use once_cell::sync::Lazy;
use std::sync::Mutex;

const DEFAULT_QUALITY: u32 = 10;
const DEFAULT_MAX_COLORS: u32 = 2;

static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
    gst::DebugCategory::new(
        "colordetect",
        gst::DebugColorFlags::empty(),
        Some("Dominant color detection"),
    )
});

#[derive(Debug, Clone, Copy)]
struct Settings {
    quality: u32,
    max_colors: u32,
}

impl Default for Settings {
    fn default() -> Self {
        Settings {
            quality: DEFAULT_QUALITY,
            max_colors: DEFAULT_MAX_COLORS,
        }
    }
}

struct State {
    color_format: ColorFormat,
    out_info: gst_video::VideoInfo,
    current_color: Option<String>,
}

#[derive(Default)]
pub struct ColorDetect {
    settings: Mutex<Settings>,
    state: AtomicRefCell<Option<State>>,
}

impl ColorDetect {
    fn detect_color(
        &self,
        buf: &mut gst::BufferRef,
    ) -> Result<Option<(String, Vec<Color>)>, gst::FlowError> {
        let mut state_guard = self.state.borrow_mut();
        let state = state_guard.as_mut().ok_or_else(|| {
            gst::element_imp_error!(self, gst::CoreError::Negotiation, ["Have no state yet"]);
            gst::FlowError::NotNegotiated
        })?;

        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,
            settings.quality as u8,
            settings.max_colors as u8,
        )
        .map_err(|_| gst::FlowError::Error)?;

        let dominant_color = palette[0];
        let dominant_color_name =
            color_name::Color::similar([dominant_color.r, dominant_color.g, dominant_color.b])
                .to_lowercase();
        if state
            .current_color
            .as_ref()
            .map_or(true, |current_color| current_color != &dominant_color_name)
        {
            let name = dominant_color_name.clone();
            state.current_color = Some(dominant_color_name);
            return Ok(Some((name, palette)));
        }
        Ok(None)
    }

    fn color_changed(&self, dominant_color_name: &str, palette: Vec<Color>) {
        gst::debug!(
            CAT,
            imp: self,
            "Dominant color changed to {}",
            dominant_color_name
        );
        let palette_colors = gst::List::new(
            palette
                .iter()
                .map(|c| ((c.r as u32) << 16) | ((c.g as u32) << 8) | (c.b as u32)),
        );

        self.obj()
            .post_message(
                gst::message::Element::builder(
                    gst::structure::Structure::builder("colordetect")
                        .field("dominant-color", dominant_color_name)
                        .field("palette", palette_colors)
                        .build(),
                )
                .build(),
            )
            .expect("Element without bus. Should not happen!");
    }
}

#[glib::object_subclass]
impl ObjectSubclass for ColorDetect {
    const NAME: &'static str = "GstColorDetect";
    type Type = super::ColorDetect;
    type ParentType = gst_base::BaseTransform;
}

impl ObjectImpl for ColorDetect {
    fn properties() -> &'static [glib::ParamSpec] {
        static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
            vec![
                glib::ParamSpecUInt::builder("quality")
                    .nick("Quality of an output colors")
                    .blurb("A step in pixels to improve performance")
                    .maximum(10)
                    .default_value(DEFAULT_QUALITY)
                    .mutable_playing()
                    .build(),
                glib::ParamSpecUInt::builder("max-colors")
                    .nick("Number of colors in the output palette")
                    .blurb("Actual colors count can be lower depending on the image")
                    .minimum(2)
                    .maximum(255)
                    .default_value(DEFAULT_MAX_COLORS)
                    .mutable_playing()
                    .build(),
            ]
        });

        PROPERTIES.as_ref()
    }

    fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
        match pspec.name() {
            "quality" => {
                let mut settings = self.settings.lock().unwrap();
                let quality = value.get().expect("type checked upstream");
                if settings.quality != quality {
                    gst::info!(
                        CAT,
                        imp: self,
                        "Changing quality from {} to {}",
                        settings.quality,
                        quality
                    );
                    settings.quality = quality;
                }
            }
            "max-colors" => {
                let mut settings = self.settings.lock().unwrap();
                let max_colors = value.get().expect("type checked upstream");
                if settings.max_colors != max_colors {
                    gst::info!(
                        CAT,
                        imp: self,
                        "Changing max_colors from {} to {}",
                        settings.max_colors,
                        max_colors
                    );
                    settings.max_colors = max_colors;
                }
            }
            _ => unimplemented!(),
        }
    }

    fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
        match pspec.name() {
            "quality" => {
                let settings = self.settings.lock().unwrap();
                settings.quality.to_value()
            }
            "max-colors" => {
                let settings = self.settings.lock().unwrap();
                settings.max_colors.to_value()
            }
            _ => unimplemented!(),
        }
    }
}

impl GstObjectImpl for ColorDetect {}

impl ElementImpl for ColorDetect {
    fn metadata() -> Option<&'static gst::subclass::ElementMetadata> {
        static ELEMENT_METADATA: Lazy<gst::subclass::ElementMetadata> = Lazy::new(|| {
            gst::subclass::ElementMetadata::new(
                "Dominant color detection",
                "Filter/Video",
                "Detects the dominant color of a video",
                "Philippe Normand <philn@igalia.com>",
            )
        });

        Some(&*ELEMENT_METADATA)
    }

    fn pad_templates() -> &'static [gst::PadTemplate] {
        static PAD_TEMPLATES: Lazy<Vec<gst::PadTemplate>> = Lazy::new(|| {
            let caps = gst_video::VideoCapsBuilder::new()
                .format_list([
                    VideoFormat::Rgb,
                    VideoFormat::Rgba,
                    VideoFormat::Argb,
                    VideoFormat::Bgr,
                    VideoFormat::Bgra,
                ])
                .build();
            let sink_pad_template = gst::PadTemplate::new(
                "sink",
                gst::PadDirection::Sink,
                gst::PadPresence::Always,
                &caps,
            )
            .unwrap();

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

            vec![sink_pad_template, src_pad_template]
        });

        PAD_TEMPLATES.as_ref()
    }
}

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;

    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,
        };

        gst::debug!(
            CAT,
            imp: self,
            "Configured for caps {} to {}",
            incaps,
            outcaps
        );

        let color_format = match in_info.format() {
            VideoFormat::Rgb => ColorFormat::Rgb,
            VideoFormat::Rgba => ColorFormat::Rgba,
            VideoFormat::Argb => ColorFormat::Argb,
            VideoFormat::Bgr => ColorFormat::Bgr,
            VideoFormat::Bgra => ColorFormat::Bgra,
            _ => unimplemented!(),
        };

        let previous_color = match self.state.borrow().as_ref() {
            Some(state) => state.current_color.clone(),
            None => None,
        };
        *self.state.borrow_mut() = Some(State {
            color_format,
            out_info,
            current_color: previous_color,
        });

        Ok(())
    }

    fn transform_ip(&self, buf: &mut gst::BufferRef) -> Result<gst::FlowSuccess, gst::FlowError> {
        if let Some((dominant_color_name, palette)) = self.detect_color(buf)? {
            self.color_changed(&dominant_color_name, palette);
        }

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