{"id":523,"date":"2018-01-13T23:23:48","date_gmt":"2018-01-13T22:23:48","guid":{"rendered":"https:\/\/coaxion.net\/blog\/?p=523"},"modified":"2018-01-22T16:01:18","modified_gmt":"2018-01-22T15:01:18","slug":"how-to-write-gstreamer-elements-in-rust-part-1-a-video-filter-for-converting-rgb-to-grayscale","status":"publish","type":"post","link":"https:\/\/coaxion.net\/blog\/2018\/01\/how-to-write-gstreamer-elements-in-rust-part-1-a-video-filter-for-converting-rgb-to-grayscale\/","title":{"rendered":"How to write GStreamer Elements in Rust Part 1: A Video Filter for converting RGB to grayscale"},"content":{"rendered":"<p>This is part one of a series of blog posts that I&#8217;ll write in the next weeks, as previously announced in <a href=\"https:\/\/coaxion.net\/blog\/2017\/12\/gstreamer-rust-bindings-release-0-10-0-gst-plugin-release-0-1-0\/\" rel=\"noopener\" target=\"_blank\">the GStreamer Rust bindings 0.10.0 release blog post<\/a>. Since the last series of blog posts about writing GStreamer plugins in Rust (<a href=\"https:\/\/coaxion.net\/blog\/2016\/05\/writing-gstreamer-plugins-and-elements-in-rust\/\" rel=\"noopener\" target=\"_blank\">[1]<\/a> <a href=\"https:\/\/coaxion.net\/blog\/2016\/09\/writing-gstreamer-elements-in-rust-part-2-dont-panic-we-have-better-assertions-now-and-other-updates\/\" rel=\"noopener\" target=\"_blank\">[2]<\/a> <a href=\"https:\/\/coaxion.net\/blog\/2016\/11\/writing-gstreamer-elements-in-rust-part-3-parsing-data-from-untrusted-sources-like-its-2016\/\" rel=\"noopener\" target=\"_blank\">[3]<\/a> <a href=\"https:\/\/coaxion.net\/blog\/2017\/03\/writing-gstreamer-elements-in-rust-part-4-logging-cows-and-plugins\/\" rel=\"noopener\" target=\"_blank\">[4]<\/a>) a lot has changed, and the content of those blog posts has only historical value now, as the journey of experimentation to what exists now.<\/p>\n<p>In this first part we&#8217;re going to write a plugin that contains a video filter element. The video filter can convert from RGB to grayscale, either output as 8-bit per pixel grayscale or 32-bit per pixel RGB. In addition there&#8217;s a property to invert all grayscale values, or to shift them by up to 255 values. In the end this will allow you to watch <a href=\"https:\/\/peach.blender.org\/\" rel=\"noopener\" target=\"_blank\">Big Bucky Bunny<\/a>, or anything else really that can somehow go into a GStreamer pipeline, in grayscale. Or encode the output to a new video file, send it over the network via <a href=\"https:\/\/gstconf.ubicast.tv\/videos\/gstreamer-webrtc\/\" rel=\"noopener\" target=\"_blank\">WebRTC<\/a> or something else, or basically do anything you want with it.<\/p>\n<figure id=\"attachment_540\" aria-describedby=\"caption-attachment-540\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/coaxion.net\/blog\/wp-content\/uploads\/2018\/01\/bbb-500x281.jpg\" alt=\"\" width=\"500\" height=\"281\" class=\"size-medium wp-image-540\" srcset=\"https:\/\/coaxion.net\/blog\/wp-content\/uploads\/2018\/01\/bbb-500x281.jpg 500w, https:\/\/coaxion.net\/blog\/wp-content\/uploads\/2018\/01\/bbb-150x84.jpg 150w, https:\/\/coaxion.net\/blog\/wp-content\/uploads\/2018\/01\/bbb.jpg 640w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><figcaption id=\"caption-attachment-540\" class=\"wp-caption-text\">Big Bucky Bunny \u2013 Grayscale<\/figcaption><\/figure>\n<p>This will show the basics of how to write a GStreamer plugin and element in Rust: the basic setup for registering a type and implementing it in Rust, and how to use the various GStreamer API and APIs from the Rust standard library to do the processing.<\/p>\n<p>The final code for this plugin can be found <a href=\"https:\/\/github.com\/sdroege\/gst-plugin-rs\/tree\/0.1\/gst-plugin-tutorial\" rel=\"noopener\" target=\"_blank\">here<\/a>,  and it is based on the 0.1 version of the <a href=\"https:\/\/crates.io\/crates\/gst-plugin\" rel=\"noopener\" target=\"_blank\">gst-plugin<\/a> crate and the 0.10 version of the <a href=\"https:\/\/crates.io\/crates\/gstreamer\" rel=\"noopener\" target=\"_blank\">gstreamer<\/a> crate. At least Rust 1.20 is required for all this. I&#8217;m also assuming that you have GStreamer (at least version 1.8) installed for your platform, see e.g. the GStreamer bindings <a href=\"https:\/\/github.com\/sdroege\/gstreamer-rs#installation\" rel=\"noopener\" target=\"_blank\">installation instructions<\/a>.<\/p>\n<h3 id=\"toc\">Table of Contents<\/h3>\n<ol>\n<li><a href=\"#project-structure\">Project Structure<\/a><\/li>\n<li><a href=\"#plugin-initialization\">Plugin Initialization<\/a><\/li>\n<li><a href=\"#type-registration\">Type Registration<\/a>\n<li><a href=\"#type-initialization\">Type Class &#038; Instance Initialization<\/a><\/li>\n<li><a href=\"#caps-pad-templates\">Caps &#038; Pad Templates<\/a><\/li>\n<li><a href=\"#caps-handling-1\">Caps Handling Part 1<\/a><\/li>\n<li><a href=\"#caps-handling-2\">Caps Handling Part 2<\/a><\/li>\n<li><a href=\"#conversion\">Conversion of BGRx Video Frames to Grayscale<\/a><\/li>\n<li><a href=\"#testing\">Testing the new element<\/a><\/li>\n<li><a href=\"#properties\">Properties<\/a><\/li>\n<li><a href=\"#what-next\">What next?<\/a><\/li>\n<\/ol>\n<h3 id=\"project-structure\">Project Structure<\/h3>\n<p>We&#8217;ll create a new <em>cargo<\/em> project with <em>cargo init &#8211;lib &#8211;name gst-plugin-tutorial<\/em>. This will create a basically empty <em>Cargo.toml<\/em> and a corresponding <em>src\/lib.rs<\/em>. We will use this structure: <em>lib.rs<\/em> will contain all the plugin related code, separate modules will contain any GStreamer plugins that are added.<\/p>\n<p>The empty <em>Cargo.toml<\/em> has to be updated to list all the dependencies that we need, and to define that the crate should result in a <em>cdylib<\/em>, i.e. a C library that does not contain any Rust-specific metadata. The final <em>Cargo.toml<\/em> looks as follows<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ini\" data-enlighter-title=\"Cargo.toml\">[package]\r\nname = &quot;gst-plugin-tutorial&quot;\r\nversion = &quot;0.1.0&quot;\r\nauthors = [&quot;Sebastian Dr\u00f6ge &lt;sebastian@centricular.com&gt;&quot;]\r\nrepository = &quot;https:\/\/github.com\/sdroege\/gst-plugin-rs&quot;\r\nlicense = &quot;MIT\/Apache-2.0&quot;\r\n\r\n[dependencies]\r\nglib = &quot;0.4&quot;\r\ngstreamer = &quot;0.10&quot;\r\ngstreamer-base = &quot;0.10&quot;\r\ngstreamer-video = &quot;0.10&quot;\r\ngst-plugin = &quot;0.1&quot;\r\n\r\n[lib]\r\nname = &quot;gstrstutorial&quot;\r\ncrate-type = [&quot;cdylib&quot;]\r\npath = &quot;src\/lib.rs&quot;<\/pre>\n<p>We&#8217;re depending on the <em>gst-plugin<\/em> crate, which provides all the basic infrastructure for implementing GStreamer plugins and elements. In addition we depend on the <em>gstreamer<\/em>, <em>gstreamer-base<\/em> and <em>gstreamer-video<\/em> crates for various GStreamer API that we&#8217;re going to use later, and the <em>glib<\/em> crate to be able to use some GLib API that we&#8217;ll need. GStreamer is building upon GLib, and this leaks through in various places.<\/p>\n<p>With the basic project structure being set-up, we should be able to compile the project with <em>cargo build<\/em> now, which will download and build all dependencies and then creates a file called <em>target\/debug\/libgstrstutorial.so<\/em> (or .dll on Windows, .dylib on macOS). This is going to be our GStreamer plugin.<\/p>\n<p>To allow GStreamer to find our new plugin and make it available in every GStreamer-based application, we could install it into the system- or user-wide GStreamer plugin path or simply point the <em>GST_PLUGIN_PATH<\/em> environment variable to the directory containing it:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sh\" data-enlighter-title=\"\">export GST_PLUGIN_PATH=&lt;code&gt;{{EJS0}}&lt;\/code&gt;\/target\/debug<\/pre>\n<p>If you now run the <em>gst-inspect-1.0<\/em> tool on the <em>libgstrstutorial.so<\/em>, it will not yet print all information it can extract from the plugin but for now just complains that this is not a valid GStreamer plugin. Which is true, we didn&#8217;t write any code for it yet.<\/p>\n<h3 id=\"plugin-initialization\">Plugin Initialization<\/h3>\n<p>Let&#8217;s start editing <em>src\/lib.rs<\/em> to make this an actual GStreamer plugin. First of all, we need to add various <em>extern crate<\/em> directives to be able to use our dependencies and also mark some of them <em>#[macro_use]<\/em> because we&#8217;re going to use macros defined in some of them. This looks like the following<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/lib.rs\">extern crate glib;\r\n#[macro_use]\r\nextern crate gstreamer as gst;\r\nextern crate gstreamer_base as gst_base;\r\nextern crate gstreamer_video as gst_video;\r\n#[macro_use]\r\nextern crate gst_plugin;<\/pre>\n<p>Next we make use of the <em>plugin_define!<\/em> macro from the <em>gst-plugin<\/em> crate to set-up the static metadata of the plugin (and make the shared library recognizeable by GStreamer to be a valid plugin), and to define the name of our entry point function (<em>plugin_init<\/em>) where we will register all the elements that this plugin provides.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/lib.rs\">plugin_define!(\r\n    b&quot;rstutorial\\0&quot;,\r\n    b&quot;Rust Tutorial Plugin\\0&quot;,\r\n    plugin_init,\r\n    b&quot;1.0\\0&quot;,\r\n    b&quot;MIT\/X11\\0&quot;,\r\n    b&quot;rstutorial\\0&quot;,\r\n    b&quot;rstutorial\\0&quot;,\r\n    b&quot;https:\/\/github.com\/sdroege\/gst-plugin-rs\\0&quot;,\r\n    b&quot;2017-12-30\\0&quot;\r\n);<\/pre>\n<p>This is unfortunately not very beautiful yet due to a) GStreamer requiring this information to be statically available in the shared library, not returned by a function (starting with GStreamer 1.14 it can be a function), and b) Rust not allowing raw strings (<em>b&#8221;blabla<\/em>) to be concatenated with a macro like the <a href=\"https:\/\/doc.rust-lang.org\/std\/macro.concat.html\" rel=\"noopener\" target=\"_blank\"><em>std::concat<\/em><\/a> macro (so that the <em>b<\/em> and <em>\\0<\/em> parts could be hidden away). Expect this to become better in the future.<\/p>\n<p>The static plugin metadata that we provide here is<\/p>\n<ol>\n<li>name of the plugin<\/li>\n<li>short description for the plugin<\/li>\n<li>name of the plugin entry point function<\/li>\n<li>version number of the plugin<\/li>\n<li>license of the plugin (only a fixed set of licenses is allowed here, <a href=\"https:\/\/gstreamer.freedesktop.org\/data\/doc\/gstreamer\/head\/gstreamer\/html\/GstPlugin.html#GstPluginDesc\" rel=\"noopener\" target=\"_blank\">see<\/a>)<\/li>\n<li>source package name<\/li>\n<li>binary package name (only really makes sense for e.g. Linux distributions)<\/li>\n<li>origin of the plugin<\/li>\n<li>release date of this version<\/li>\n<\/ol>\n<p>In addition we&#8217;re defining an empty plugin entry point function that just returns <em>true<\/em><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/lib.rs\">fn plugin_init(plugin: &amp;gst::Plugin) -&gt; bool {\r\n    true\r\n}<\/pre>\n<p>With all that given, <em>gst-inspect-1.0<\/em> should print exactly this information when running on the <em>libgstrstutorial.so<\/em> file (or .dll on Windows, or .dylib on macOS)<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sh\" data-enlighter-title=\"\">gst-inspect-1.0 target\/debug\/libgstrstutorial.so<\/pre>\n<h3 id=\"type-registration\">Type Registration<\/h3>\n<p>As a next step, we&#8217;re going to add another module <em>rgb2gray<\/em> to our project, and call a function called <em>register<\/em> from our <em>plugin_init<\/em> function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/lib.rs\">mod rgb2gray;\r\n\r\nfn plugin_init(plugin: &amp;gst::Plugin) -&gt; bool {\r\n    rgb2gray::register(plugin);\r\n    true\r\n}<\/pre>\n<p>With that our <em>src\/lib.rs<\/em> is complete, and all following code is only in <em>src\/rgb2gray.rs<\/em>. At the top of the new file we first need to add various <em>use<\/em>-directives to import various types and functions we&#8217;re going to use into the current module&#8217;s scope<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/rgb2gray.rs\">use glib;\r\nuse gst;\r\nuse gst::prelude::*;\r\nuse gst_video;\r\n\r\nuse gst_plugin::properties::*;\r\nuse gst_plugin::object::*;\r\nuse gst_plugin::element::*;\r\nuse gst_plugin::base_transform::*;\r\n\r\nuse std::i32;\r\nuse std::sync::Mutex;<\/pre>\n<p>GStreamer is based on the GLib object system (<a href=\"https:\/\/developer.gnome.org\/gobject\/stable\/\" rel=\"noopener\" target=\"_blank\">GObject<\/a>). C (just like Rust) does not have built-in support for object orientated programming, inheritance, virtual methods and related concepts, and GObject makes these features available in C as a library. Without language support this is a quite verbose endeavour in C, and the <em>gst-plugin<\/em> crate tries to expose all this in a (as much as possible) Rust-style API while hiding all the details that do not really matter.<\/p>\n<p>So, as a next step we need to register a new type for our RGB to Grayscale converter GStreamer element with the GObject type system, and then register that type with GStreamer to be able to create new instances of it. We do this with the following code<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/rgb2gray.rs\">struct Rgb2GrayStatic;\r\n\r\nimpl ImplTypeStatic&lt;BaseTransform&gt; for Rgb2GrayStatic {\r\n    fn get_name(&amp;self) -&gt; &amp;str {\r\n        &quot;Rgb2Gray&quot;\r\n    }\r\n\r\n    fn new(&amp;self, element: &amp;BaseTransform) -&gt; Box&lt;BaseTransformImpl&lt;BaseTransform&gt;&gt; {\r\n        Rgb2Gray::new(element)\r\n    }\r\n\r\n    fn class_init(&amp;self, klass: &amp;mut BaseTransformClass) {\r\n        Rgb2Gray::class_init(klass);\r\n    }\r\n}\r\n\r\npub fn register(plugin: &amp;gst::Plugin) {\r\n    let type_ = register_type(Rgb2GrayStatic);\r\n    gst::Element::register(plugin, &quot;rsrgb2gray&quot;, 0, type_);\r\n}<\/pre>\n<p>This defines a zero-sized struct <em>Rgb2GrayStatic<\/em> that is used to implement the <em>ImplTypeStatic&lt;BaseTransform&gt;<\/em> trait on it for providing static information about the type to the type system. In our case this is a zero-sized struct, but in other cases this struct might contain actual data (for example if the same element code is used for multiple elements, e.g. when wrapping a generic codec API that provides support for multiple decoders and then wanting to register one element per decoder). By implementing <em>ImplTypeStatic&lt;BaseTransform&gt;<\/em> we also declare that our element is going to be based on the GStreamer <em>BaseTransform<\/em> base class, which provides a relatively simple API for 1:1 transformation elements like ours is going to be.<\/p>\n<p><em>ImplTypeStatic<\/em> provides functions that return a name for the type, and functions for initializing\/returning a new instance of our element (<em>new<\/em>) and for initializing the class metadata (<em>class_init<\/em>, more on that later). We simply let those functions proxy to associated functions on the <em>Rgb2Gray<\/em> struct that we&#8217;re going to define at a later time.<\/p>\n<p>In addition, we also define a <em>register<\/em> function (the one that is already called from our <em>plugin_init<\/em> function) and in there first register the <em>Rgb2GrayStatic<\/em> type metadata with the GObject type system to retrieve a type ID, and then register this type ID to GStreamer to be able to create new instances of it with the name &#8220;rsrgb2gray&#8221; (e.g. when using <a href=\"https:\/\/sdroege.github.io\/rustdoc\/gstreamer\/gstreamer\/struct.ElementFactory.html#method.make\" rel=\"noopener\" target=\"_blank\"><em>gst::ElementFactory::make<\/em><\/a>).<\/p>\n<h3 id=\"type-initialization\">Type Class &#038; Instance Initialization<\/h3>\n<p>As a next step we declare the <em>Rgb2Gray<\/em> struct and implement the <em>new<\/em> and <em>class_init<\/em> functions on it. In the first version, this struct is almost empty but we will later use it to store all state of our element.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/rgb2gray.rs\">struct Rgb2Gray {\r\n    cat: gst::DebugCategory,\r\n}\r\n\r\nimpl Rgb2Gray {\r\n    fn new(_transform: &amp;BaseTransform) -&gt; Box&lt;BaseTransformImpl&lt;BaseTransform&gt;&gt; {\r\n        Box::new(Self {\r\n            cat: gst::DebugCategory::new(\r\n                &quot;rsrgb2gray&quot;,\r\n                gst::DebugColorFlags::empty(),\r\n                &quot;Rust RGB-GRAY converter&quot;,\r\n            ),\r\n        })\r\n    }\r\n\r\n    fn class_init(klass: &amp;mut BaseTransformClass) {\r\n        klass.set_metadata(\r\n            &quot;RGB-GRAY Converter&quot;,\r\n            &quot;Filter\/Effect\/Converter\/Video&quot;,\r\n            &quot;Converts RGB to GRAY or grayscale RGB&quot;,\r\n            &quot;Sebastian Dr\u00f6ge &lt;sebastian@centricular.com&gt;&quot;,\r\n        );\r\n\r\n        klass.configure(BaseTransformMode::NeverInPlace, false, false);\r\n    }\r\n}<\/pre>\n<p>In the <em>new<\/em> function we return a boxed (i.e. heap-allocated) version of our struct, containing a newly created GStreamer debug category of name &#8220;rsrgb2gray&#8221;. We&#8217;re going to use this debug category later for making use of GStreamer&#8217;s debug logging system for logging the state and changes of our element.<\/p>\n<p>In the <em>class_init<\/em> function we, again, set up some metadata for our new element. In this case these are a description, a classification of our element, a longer description and the author. The metadata can later be retrieved and made use of via the <a href=\"https:\/\/sdroege.github.io\/rustdoc\/gstreamer\/gstreamer\/struct.Registry.html\" rel=\"noopener\" target=\"_blank\">Registry<\/a> and <a href=\"https:\/\/sdroege.github.io\/rustdoc\/gstreamer\/gstreamer\/struct.PluginFeature.html\" rel=\"noopener\" target=\"_blank\">PluginFeature<\/a>\/<a href=\"https:\/\/sdroege.github.io\/rustdoc\/gstreamer\/gstreamer\/struct.ElementFactory.html\" rel=\"noopener\" target=\"_blank\">ElementFactory<\/a> API. We also configure the <em>BaseTransform<\/em> class and define that we will never operate in-place (producing our output in the input buffer), and that we don&#8217;t want to work in passthrough mode if the input\/output formats are the same.<\/p>\n<p>Additionally we need to implement various traits on the <em>Rgb2Gray<\/em> struct, which will later be used to override virtual methods of the various parent classes of our element. For now we can keep the trait implementations empty. There is one trait implementation required per parent class.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/rgb2gray.rs\">impl ObjectImpl&lt;BaseTransform&gt; for Rgb2Gray {}\r\nimpl ElementImpl&lt;BaseTransform&gt; for Rgb2Gray {}\r\nimpl BaseTransformImpl&lt;BaseTransform&gt; for Rgb2Gray {}<\/pre>\n<p>With all this defined, <em>gst-inspect-1.0<\/em> should be able to show some more information about our element already but will still complain that it&#8217;s not complete yet.<\/p>\n<h3 id=\"caps-pad-templates\">Caps &#038; Pad Templates<\/h3>\n<p>Data flow of GStreamer elements is happening via pads, which are the input(s) and output(s) (or sinks and sources) of an element. Via the pads, buffers containing actual media data, events or queries are transferred. An element can have any number of sink and source pads, but our new element will only have one of each.<\/p>\n<p>To be able to declare what kinds of pads an element can create (they are not necessarily all static but could be created at runtime by the element or the application), it is necessary to install so-called pad templates during the class initialization. These pad templates contain the name (or rather &#8220;name template&#8221;, it could be something like <em>src_%u<\/em> for e.g. pad templates that declare multiple possible pads), the direction of the pad (sink or source), the availability of the pad (is it <strong>always<\/strong> there, <strong>sometimes<\/strong> added\/removed by the element or to be <strong>requested<\/strong> by the application) and all the possible media types (called caps) that the pad can consume (sink pads) or produce (src pads).<\/p>\n<p>In our case we only have <strong>always<\/strong> pads, one sink pad called &#8220;sink&#8221;, on which we can only accept RGB (BGRx to be exact) data with any width\/height\/framerate and one source pad called &#8220;src&#8221;, on which we will produce either RGB (BGRx) data or GRAY8 (8-bit grayscale) data. We do this by adding the following code to the <em>class_init<\/em> function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/rgb2gray.rs\">        let caps = gst::Caps::new_simple(\r\n            &quot;video\/x-raw&quot;,\r\n            &amp;[\r\n                (\r\n                    &quot;format&quot;,\r\n                    &amp;gst::List::new(&amp;[\r\n                        &amp;gst_video::VideoFormat::Bgrx.to_string(),\r\n                        &amp;gst_video::VideoFormat::Gray8.to_string(),\r\n                    ]),\r\n                ),\r\n                (&quot;width&quot;, &amp;gst::IntRange::&lt;i32&gt;::new(0, i32::MAX)),\r\n                (&quot;height&quot;, &amp;gst::IntRange::&lt;i32&gt;::new(0, i32::MAX)),\r\n                (\r\n                    &quot;framerate&quot;,\r\n                    &amp;gst::FractionRange::new(\r\n                        gst::Fraction::new(0, 1),\r\n                        gst::Fraction::new(i32::MAX, 1),\r\n                    ),\r\n                ),\r\n            ],\r\n        );\r\n\r\n        let src_pad_template = gst::PadTemplate::new(\r\n            &quot;src&quot;,\r\n            gst::PadDirection::Src,\r\n            gst::PadPresence::Always,\r\n            &amp;caps,\r\n        );\r\n        klass.add_pad_template(src_pad_template);\r\n\r\n        let caps = gst::Caps::new_simple(\r\n            &quot;video\/x-raw&quot;,\r\n            &amp;[\r\n                (&quot;format&quot;, &amp;gst_video::VideoFormat::Bgrx.to_string()),\r\n                (&quot;width&quot;, &amp;gst::IntRange::&lt;i32&gt;::new(0, i32::MAX)),\r\n                (&quot;height&quot;, &amp;gst::IntRange::&lt;i32&gt;::new(0, i32::MAX)),\r\n                (\r\n                    &quot;framerate&quot;,\r\n                    &amp;gst::FractionRange::new(\r\n                        gst::Fraction::new(0, 1),\r\n                        gst::Fraction::new(i32::MAX, 1),\r\n                    ),\r\n                ),\r\n            ],\r\n        );\r\n\r\n        let sink_pad_template = gst::PadTemplate::new(\r\n            &quot;sink&quot;,\r\n            gst::PadDirection::Sink,\r\n            gst::PadPresence::Always,\r\n            &amp;caps,\r\n        );\r\n        klass.add_pad_template(sink_pad_template);<\/pre>\n<p>The names &#8220;src&#8221; and &#8220;sink&#8221; are pre-defined by the <em>BaseTransform<\/em> class and this base-class will also create the actual pads with those names from the templates for us whenever a new element instance is created. Otherwise we would have to do that in our <em>new<\/em> function but here this is not needed.<\/p>\n<p>If you now run <em>gst-inspect-1.0<\/em> on the <em>rsrgb2gray<\/em> element, these pad templates with their caps should also show up.<\/p>\n<h3 id=\"caps-handling-1\">Caps Handling Part 1<\/h3>\n<p>As a next step we will add caps handling to our new element. This involves overriding 4 virtual methods from the <em>BaseTransformImpl<\/em> trait, and actually storing the configured input and output caps inside our element struct. Let&#8217;s start with the latter<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/rgb2gray.rs\">struct State {\r\n    in_info: gst_video::VideoInfo,\r\n    out_info: gst_video::VideoInfo,\r\n}\r\n\r\nstruct Rgb2Gray {\r\n    cat: gst::DebugCategory,\r\n    state: Mutex&lt;Option&lt;State&gt;&gt;,\r\n}\r\n\r\nimpl Rgb2Gray {\r\n    fn new(_transform: &amp;BaseTransform) -&gt; Box&lt;BaseTransformImpl&lt;BaseTransform&gt;&gt; {\r\n        Box::new(Self {\r\n            cat: gst::DebugCategory::new(\r\n                &quot;rsrgb2gray&quot;,\r\n                gst::DebugColorFlags::empty(),\r\n                &quot;Rust RGB-GRAY converter&quot;,\r\n            ),\r\n            state: Mutex::new(None),\r\n        })\r\n    }\r\n}<\/pre>\n<p>We define a new struct <em>State<\/em> that contains the input and output caps, stored in a <a href=\"https:\/\/sdroege.github.io\/rustdoc\/gstreamer\/gstreamer_video\/struct.VideoInfo.html\" rel=\"noopener\" target=\"_blank\"><em>VideoInfo<\/em><\/a>. <em>VideoInfo<\/em> is a struct that contains various fields like width\/height, framerate and the video format and allows to conveniently with the properties of (raw) video formats. We have to store it inside a <a href=\"https:\/\/doc.rust-lang.org\/std\/sync\/struct.Mutex.html\" rel=\"noopener\" target=\"_blank\"><em>Mutex<\/em><\/a> in our <em>Rgb2Gray<\/em> struct as this can (in theory) be accessed from multiple threads at the same time.<\/p>\n<p>Whenever input\/output caps are configured on our element, the <em>set_caps<\/em> virtual method of <em>BaseTransform<\/em> is called with both caps (i.e. in the very beginning before the data flow and whenever it changes), and all following video frames that pass through our element should be according to those caps. Once the element is shut down, the <em>stop<\/em> virtual method is called and it would make sense to release the <em>State<\/em> as it only contains stream-specific information. We&#8217;re doing this by adding the following to the <em>BaseTransformImpl<\/em> trait implementation<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/rgb2gray.rs\">impl BaseTransformImpl&lt;BaseTransform&gt; for Rgb2Gray {\r\n    fn set_caps(&amp;self, element: &amp;BaseTransform, incaps: &amp;gst::Caps, outcaps: &amp;gst::Caps) -&gt; bool {\r\n        let in_info = match gst_video::VideoInfo::from_caps(incaps) {\r\n            None =&gt; return false,\r\n            Some(info) =&gt; info,\r\n        };\r\n        let out_info = match gst_video::VideoInfo::from_caps(outcaps) {\r\n            None =&gt; return false,\r\n            Some(info) =&gt; info,\r\n        };\r\n\r\n        gst_debug!(\r\n            self.cat,\r\n            obj: element,\r\n            &quot;Configured for caps {} to {}&quot;,\r\n            incaps,\r\n            outcaps\r\n        );\r\n\r\n        *self.state.lock().unwrap() = Some(State {\r\n            in_info: in_info,\r\n            out_info: out_info,\r\n        });\r\n\r\n        true\r\n    }\r\n\r\n    fn stop(&amp;self, element: &amp;BaseTransform) -&gt; bool {\r\n        \/\/ Drop state\r\n        let _ = self.state.lock().unwrap().take();\r\n\r\n        gst_info!(self.cat, obj: element, &quot;Stopped&quot;);\r\n\r\n        true\r\n    }\r\n}<\/pre>\n<p>This code should be relatively self-explanatory. In <em>set_caps<\/em> we&#8217;re parsing the two caps into a <em>VideoInfo<\/em> and then store this in our <em>State<\/em>, in <em>stop<\/em> we drop the <em>State<\/em> and replace it with <em>None<\/em>. In addition we make use of our debug category here and use the <em>gst_info!<\/em> and <em>gst_debug!<\/em> macros to output the current caps configuration to the GStreamer debug logging system. This information can later be useful for debugging any problems once the element is running.<\/p>\n<p>Next we have to provide information to the <em>BaseTransform<\/em> base class about the size in bytes of a video frame with specific caps. This is needed so that the base class can allocate an appropriately sized output buffer for us, that we can then fill later. This is done with the <em>get_unit_size<\/em> virtual method, which is required to return the size of one processing unit in specific caps. In our case, one processing unit is one video frame. In the case of raw audio it would be the size of one sample multiplied by the number of channels.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/rgb2gray.rs\">impl BaseTransformImpl&lt;BaseTransform&gt; for Rgb2Gray {\r\n    fn get_unit_size(&amp;self, _element: &amp;BaseTransform, caps: &amp;gst::Caps) -&gt; Option&lt;usize&gt; {\r\n        gst_video::VideoInfo::from_caps(caps).map(|info| info.size())\r\n    }\r\n}<\/pre>\n<p>We simply make use of the <em>VideoInfo<\/em> API here again, which conveniently gives us the size of one video frame already.<\/p>\n<p>Instead of <em>get_unit_size<\/em> it would also be possible to implement the <em>transform_size<\/em> virtual method, which is getting passed one size and the corresponding caps, another caps and is supposed to return the size converted to the second caps. Depending on how your element works, one or the other can be easier to implement.<\/p>\n<h3 id=\"caps-handling-2\">Caps Handling Part 2<\/h3>\n<p>We&#8217;re not done yet with caps handling though. As a very last step it is required that we implement a function that is converting caps into the corresponding caps in the other direction. For example, if we receive BGRx caps with some width\/height on the sinkpad, we are supposed to convert this into new caps with the same width\/height but BGRx or GRAY8. That is, we can convert BGRx to BGRx or GRAY8. Similarly, if the element downstream of ours can accept GRAY8 with a specific width\/height from our source pad, we have to convert this to BGRx with that very same width\/height.<\/p>\n<p>This has to be implemented in the <em>transform_caps<\/em> virtual method, and looks as following<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/rgb2gray.rs\">impl BaseTransformImpl&lt;BaseTransform&gt; for Rgb2Gray {\r\n    fn transform_caps(\r\n        &amp;self,\r\n        element: &amp;BaseTransform,\r\n        direction: gst::PadDirection,\r\n        caps: gst::Caps,\r\n        filter: Option&lt;&amp;gst::Caps&gt;,\r\n    ) -&gt; gst::Caps {\r\n        let other_caps = if direction == gst::PadDirection::Src {\r\n            let mut caps = caps.clone();\r\n\r\n            for s in caps.make_mut().iter_mut() {\r\n                s.set(&quot;format&quot;, &amp;gst_video::VideoFormat::Bgrx.to_string());\r\n            }\r\n\r\n            caps\r\n        } else {\r\n            let mut gray_caps = gst::Caps::new_empty();\r\n\r\n            {\r\n                let gray_caps = gray_caps.get_mut().unwrap();\r\n\r\n                for s in caps.iter() {\r\n                    let mut s_gray = s.to_owned();\r\n                    s_gray.set(&quot;format&quot;, &amp;gst_video::VideoFormat::Gray8.to_string());\r\n                    gray_caps.append_structure(s_gray);\r\n                }\r\n                gray_caps.append(caps.clone());\r\n            }\r\n\r\n            gray_caps\r\n        };\r\n\r\n        gst_debug!(\r\n            self.cat,\r\n            obj: element,\r\n            &quot;Transformed caps from {} to {} in direction {:?}&quot;,\r\n            caps,\r\n            other_caps,\r\n            direction\r\n        );\r\n\r\n        if let Some(filter) = filter {\r\n            filter.intersect_with_mode(&amp;other_caps, gst::CapsIntersectMode::First)\r\n        } else {\r\n            other_caps\r\n        }\r\n    }\r\n}<\/pre>\n<p>This caps conversion happens in 3 steps. First we check if we got caps for the source pad. In that case, the caps on the other pad (the sink pad) are going to be exactly the same caps but no matter if the caps contained BGRx or GRAY8 they must become BGRx as that&#8217;s the only format that our sink pad can accept. We do this by creating a clone of the input caps, then making sure that those caps are actually writable (i.e. we&#8217;re having the only reference to them, or a copy is going to be created) and then iterate over all the structures inside the caps and then set the &#8220;format&#8221; field to BGRx. After this, all structures in the new caps will be with the format field set to BGRx.<\/p>\n<p>Similarly, if we get caps for the sink pad and are supposed to convert it to caps for the source pad, we create new caps and in there append a copy of each structure of the input caps (which are BGRx) with the format field set to GRAY8. In the end we append the original caps, giving us first all caps as GRAY8 and then the same caps as BGRx. With this ordering we signal to GStreamer that we would prefer to output GRAY8 over BGRx.<\/p>\n<p>In the end the caps we created for the other pad are filtered against optional filter caps to reduce the potential size of the caps. This is done by intersecting the caps with that filter, while keeping the order (and thus preferences) of the filter caps (<em>gst::CapsIntersectMode::First<\/em>).<\/p>\n<h3 id=\"conversion\">Conversion of BGRx Video Frames to Grayscale<\/h3>\n<p>Now that all the caps handling is implemented, we can finally get to the implementation of the actual video frame conversion. For this we start with defining a helper function <em>bgrx_to_gray<\/em> that converts one BGRx pixel to a grayscale value. The BGRx pixel is passed as a <em>&#038;[u8]<\/em> slice with 4 elements and the function returns another <em>u8<\/em> for the grayscale value.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/rgb2gray.rs\">impl Rgb2Gray {\r\n    #[inline]\r\n    fn bgrx_to_gray(in_p: &amp;[u8]) -&gt; u8 {\r\n        \/\/ See https:\/\/en.wikipedia.org\/wiki\/YUV#SDTV_with_BT.601\r\n        const R_Y: u32 = 19595; \/\/ 0.299 * 65536\r\n        const G_Y: u32 = 38470; \/\/ 0.587 * 65536\r\n        const B_Y: u32 = 7471; \/\/ 0.114 * 65536\r\n\r\n        assert_eq!(in_p.len(), 4);\r\n\r\n        let b = u32::from(in_p[0]);\r\n        let g = u32::from(in_p[1]);\r\n        let r = u32::from(in_p[2]);\r\n\r\n        let gray = ((r * R_Y) + (g * G_Y) + (b * B_Y)) \/ 65536;\r\n        (gray as u8)\r\n    }\r\n}<\/pre>\n<p>This function works by extracting the blue, green and red components from each pixel (remember: we work on BGRx, so the first value will be blue, the second green, the third red and the fourth unused), extending it from 8 to 32 bits for a wider value-range and then converts it to the Y component of the YUV colorspace (basically what your grandparents&#8217; black &#038; white TV would&#8217;ve displayed). The coefficients come from the Wikipedia page about YUV and are normalized to unsigned 16 bit integers so we can keep some accuracy, don&#8217;t have to work with floating point arithmetic and stay inside the range of 32 bit integers for all our calculations. As you can see, the green component is weighted more than the others, which comes from our eyes being more sensitive to green than to other colors.<\/p>\n<p><strong>Note:<\/strong> This is only doing the actual conversion from linear RGB to grayscale (and in <a href=\"https:\/\/en.wikipedia.org\/wiki\/YUV#SDTV_with_BT.601\" rel=\"noopener\" target=\"_blank\">BT.601<\/a> colorspace). To do this conversion correctly you need to know your colorspaces and use the correct coefficients for conversion, and also do <a href=\"https:\/\/en.wikipedia.org\/wiki\/Gamma_correction\" rel=\"noopener\" target=\"_blank\">gamma correction<\/a>. See <a href=\"https:\/\/web.archive.org\/web\/20161024090830\/http:\/\/www.4p8.com\/eric.brasseur\/gamma.html\" rel=\"noopener\" target=\"_blank\">this<\/a> about why it is important.<\/p>\n<p>Afterwards we have to actually call this function on every pixel. For this the <em>transform<\/em> virtual method is implemented, which gets a input and output buffer passed and we&#8217;re supposed to read the input buffer and fill the output buffer. The implementation looks as follows, and is going to be our biggest function for this element<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"default\" data-enlighter-title=\"\">impl BaseTransformImpl&lt;BaseTransform&gt; for Rgb2Gray {\r\n    fn transform(\r\n        &amp;self,\r\n        element: &amp;BaseTransform,\r\n        inbuf: &amp;gst::Buffer,\r\n        outbuf: &amp;mut gst::BufferRef,\r\n    ) -&gt; gst::FlowReturn {\r\n        let mut state_guard = self.state.lock().unwrap();\r\n        let state = match *state_guard {\r\n            None =&gt; {\r\n                gst_element_error!(element, gst::CoreError::Negotiation, [&quot;Have no state yet&quot;]);\r\n                return gst::FlowReturn::NotNegotiated;\r\n            }\r\n            Some(ref mut state) =&gt; state,\r\n        };\r\n\r\n        let in_frame = match gst_video::VideoFrameRef::from_buffer_ref_readable(\r\n            inbuf.as_ref(),\r\n            &amp;state.in_info,\r\n        ) {\r\n            None =&gt; {\r\n                gst_element_error!(\r\n                    element,\r\n                    gst::CoreError::Failed,\r\n                    [&quot;Failed to map input buffer readable&quot;]\r\n                );\r\n                return gst::FlowReturn::Error;\r\n            }\r\n            Some(in_frame) =&gt; in_frame,\r\n        };\r\n\r\n        let mut out_frame =\r\n            match gst_video::VideoFrameRef::from_buffer_ref_writable(outbuf, &amp;state.out_info) {\r\n                None =&gt; {\r\n                    gst_element_error!(\r\n                        element,\r\n                        gst::CoreError::Failed,\r\n                        [&quot;Failed to map output buffer writable&quot;]\r\n                    );\r\n                    return gst::FlowReturn::Error;\r\n                }\r\n                Some(out_frame) =&gt; out_frame,\r\n            };\r\n\r\n        let width = in_frame.width() as usize;\r\n        let in_stride = in_frame.plane_stride()[0] as usize;\r\n        let in_data = in_frame.plane_data(0).unwrap();\r\n        let out_stride = out_frame.plane_stride()[0] as usize;\r\n        let out_format = out_frame.format();\r\n        let out_data = out_frame.plane_data_mut(0).unwrap();\r\n\r\n        if out_format == gst_video::VideoFormat::Bgrx {\r\n            assert_eq!(in_data.len() % 4, 0);\r\n            assert_eq!(out_data.len() % 4, 0);\r\n            assert_eq!(out_data.len() \/ out_stride, in_data.len() \/ in_stride);\r\n\r\n            let in_line_bytes = width * 4;\r\n            let out_line_bytes = width * 4;\r\n\r\n            assert!(in_line_bytes &lt;= in_stride);\r\n            assert!(out_line_bytes &lt;= out_stride);\r\n\r\n            for (in_line, out_line) in in_data\r\n                .chunks(in_stride)\r\n                .zip(out_data.chunks_mut(out_stride))\r\n            {\r\n                for (in_p, out_p) in in_line[..in_line_bytes]\r\n                    .chunks(4)\r\n                    .zip(out_line[..out_line_bytes].chunks_mut(4))\r\n                {\r\n                    assert_eq!(out_p.len(), 4);\r\n\r\n                    let gray = Rgb2Gray::bgrx_to_gray(in_p);\r\n                    out_p[0] = gray;\r\n                    out_p[1] = gray;\r\n                    out_p[2] = gray;\r\n                }\r\n            }\r\n        } else if out_format == gst_video::VideoFormat::Gray8 {\r\n            assert_eq!(in_data.len() % 4, 0);\r\n            assert_eq!(out_data.len() \/ out_stride, in_data.len() \/ in_stride);\r\n\r\n            let in_line_bytes = width * 4;\r\n            let out_line_bytes = width;\r\n\r\n            assert!(in_line_bytes &lt;= in_stride);\r\n            assert!(out_line_bytes &lt;= out_stride);\r\n\r\n            for (in_line, out_line) in in_data\r\n                .chunks(in_stride)\r\n                .zip(out_data.chunks_mut(out_stride))\r\n            {\r\n                for (in_p, out_p) in in_line[..in_line_bytes]\r\n                    .chunks(4)\r\n                    .zip(out_line[..out_line_bytes].iter_mut())\r\n                {\r\n                    let gray = Rgb2Gray::bgrx_to_gray(in_p);\r\n                    *out_p = gray;\r\n                }\r\n            }\r\n        } else {\r\n            unimplemented!();\r\n        }\r\n\r\n        gst::FlowReturn::Ok\r\n    }\r\n}<\/pre>\n<p>What happens here is that we first of all lock our state (the input\/output <em>VideoInfo<\/em>) and error out if we don&#8217;t have any yet (which can&#8217;t really happen unless other elements have a bug, but better safe than sorry). After that we map the input buffer readable and the output buffer writable with the <a href=\"https:\/\/sdroege.github.io\/rustdoc\/gstreamer\/gstreamer_video\/video_frame\/struct.VideoFrameRef.html\" rel=\"noopener\" target=\"_blank\"><em>VideoFrameRef<\/em><\/a> API. By mapping the buffers we get access to the underlying bytes of them, and the mapping operation could for example make GPU memory available or just do nothing and give us access to a normally allocated memory area. We have access to the bytes of the buffer until the <em>VideoFrameRef<\/em> goes out of scope.<\/p>\n<p>Instead of <em>VideoFrameRef<\/em> we could&#8217;ve also used the <a href=\"https:\/\/sdroege.github.io\/rustdoc\/gstreamer\/gstreamer\/buffer\/struct.BufferRef.html#method.map_readable\" rel=\"noopener\" target=\"_blank\"><em>gst::Buffer::map_readable()<\/em><\/a> and <a href=\"https:\/\/sdroege.github.io\/rustdoc\/gstreamer\/gstreamer\/buffer\/struct.BufferRef.html#method.map_writable\" rel=\"noopener\" target=\"_blank\"><em>gst::Buffer::map_writable()<\/em><\/a> API, but different to those the <em>VideoFrameRef<\/em> API also extracts various metadata from the raw video buffers and makes them available. For example we can directly access the different planes as slices without having to calculate the offsets ourselves, or we get directly access to the width and height of the video frame.<\/p>\n<p>After mapping the buffers, we store various information we&#8217;re going to need later in local variables to save some typing later. This is the width (same for input and output as we never changed the width in <em>transform_caps<\/em>), the input and out (row-) stride (the number of bytes per row\/line, which possibly includes some padding at the end of each line for alignment reasons), the output format (which can be BGRx or GRAY8 because of how we implemented <em>transform_caps<\/em>) and the pointers to the first plane of the input and output (which in this case also is the only plane, BGRx and GRAY8 both have only a single plane containing all the RGB\/gray components).<\/p>\n<p>Then based on whether the output is BGRx or GRAY8, we iterate over all pixels. The code is basically the same in both cases, so I&#8217;m only going to explain the case where BGRx is output.<\/p>\n<p>We start by iterating over each line of the input and output, and do so by using the <a href=\"https:\/\/doc.rust-lang.org\/std\/primitive.slice.html#method.chunks\" rel=\"noopener\" target=\"_blank\"><em>chunks<\/em><\/a> iterator to give us chunks of as many bytes as the (row-) stride of the video frame is, do the same for the other frame and then zip both iterators together. This means that on each iteration we get exactly one line as a slice from each of the frames and can then start accessing the actual pixels in each line.<\/p>\n<p>To access the individual pixels in each line, we again use the <em>chunks<\/em> iterator the same way, but this time to always give us chunks of 4 bytes from each line. As BGRx uses 4 bytes for each pixel, this gives us exactly one pixel. Instead of iterating over the whole line, we only take the actual sub-slice that contains the pixels, not the whole line with stride number of bytes containing potential padding at the end. Now for each of these pixels we call our previously defined <em>bgrx_to_gray<\/em> function and then fill the B, G and R components of the output buffer with that value to get grayscale output. And that&#8217;s all.<\/p>\n<p>Using Rust high-level abstractions like the <em>chunks<\/em> iterators and bounds-checking slice accesses might seem like it&#8217;s going to cause quite some performance penalty, but if you look at the generated assembly most of the bounds checks are completely optimized away and the resulting assembly code is close to what one would&#8217;ve written manually (especially when using the newly-added <a href=\"https:\/\/github.com\/rust-lang\/rust\/pull\/47126\" rel=\"noopener\" target=\"_blank\"><em>exact_chunks<\/em><\/a> iterators). Here you&#8217;re getting safe and high-level looking code with low-level performance!<\/p>\n<p>You might&#8217;ve also noticed the various assertions in the processing function. These are there to give further hints to the compiler about properties of the code, and thus potentially being able to optimize the code better and moving e.g. bounds checks out of the inner loop and just having the assertion outside the loop check for the same. In Rust adding assertions can often improve performance by allowing further optimizations to be applied, but in the end always check the resulting assembly to see if what you did made any difference.<\/p>\n<h3 id=\"testing\">Testing the new element<\/h3>\n<p>Now we implemented almost all functionality of our new element and could run it on actual video data. This can be done now with the <em>gst-launch-1.0<\/em> tool, or any application using GStreamer and allowing us to insert our new element somewhere in the video part of the pipeline. With <em>gst-launch-1.0<\/em> you could run for example the following pipelines<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sh\" data-enlighter-title=\"\"># Run on a test pattern\r\ngst-launch-1.0 videotestsrc ! rsrgb2gray ! videoconvert ! autovideosink\r\n\r\n# Run on some video file, also playing the audio\r\ngst-launch-1.0 playbin uri=file:\/\/\/path\/to\/some\/file video-filter=rsrgb2gray<\/pre>\n<p>Note that you will likely want to compile with <em>cargo build &#8211;release<\/em> and add the <em>target\/release<\/em> directory to <em>GST_PLUGIN_PATH<\/em> instead. The debug build might be too slow, and generally the release builds are multiple orders of magnitude (!) faster.<\/p>\n<h3 id=\"properties\">Properties<\/h3>\n<p>The only feature missing now are the properties I mentioned in the opening paragraph: one boolean property to invert the grayscale value and one integer property to shift the value by up to 255. Implementing this on top of the previous code is not a lot of work. Let&#8217;s start with defining a struct for holding the property values and defining the property metadata.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/rgb2gray.rs\">const DEFAULT_INVERT: bool = false;\r\nconst DEFAULT_SHIFT: u32 = 0;\r\n\r\n#[derive(Debug, Clone, Copy)]\r\nstruct Settings {\r\n    invert: bool,\r\n    shift: u32,\r\n}\r\n\r\nimpl Default for Settings {\r\n    fn default() -&gt; Self {\r\n        Settings {\r\n            invert: DEFAULT_INVERT,\r\n            shift: DEFAULT_SHIFT,\r\n        }\r\n    }\r\n}\r\n\r\nstatic PROPERTIES: [Property; 2] = [\r\n    Property::Boolean(\r\n        &quot;invert&quot;,\r\n        &quot;Invert&quot;,\r\n        &quot;Invert grayscale output&quot;,\r\n        DEFAULT_INVERT,\r\n        PropertyMutability::ReadWrite,\r\n    ),\r\n    Property::UInt(\r\n        &quot;shift&quot;,\r\n        &quot;Shift&quot;,\r\n        &quot;Shift grayscale output (wrapping around)&quot;,\r\n        (0, 255),\r\n        DEFAULT_SHIFT,\r\n        PropertyMutability::ReadWrite,\r\n    ),\r\n];\r\n\r\nstruct Rgb2Gray {\r\n    cat: gst::DebugCategory,\r\n    settings: Mutex&lt;Settings&gt;,\r\n    state: Mutex&lt;Option&lt;State&gt;&gt;,\r\n}\r\n\r\nimpl Rgb2Gray {\r\n    fn new(_transform: &amp;BaseTransform) -&gt; Box&lt;BaseTransformImpl&lt;BaseTransform&gt;&gt; {\r\n        Box::new(Self {\r\n            cat: gst::DebugCategory::new(\r\n                &quot;rsrgb2gray&quot;,\r\n                gst::DebugColorFlags::empty(),\r\n                &quot;Rust RGB-GRAY converter&quot;,\r\n            ),\r\n            settings: Mutex::new(Default::default()),\r\n            state: Mutex::new(None),\r\n        })\r\n    }\r\n}<\/pre>\n<p>This should all be rather straightforward: we define a <em>Settings<\/em> struct that stores the two values, implement the <a href=\"https:\/\/doc.rust-lang.org\/nightly\/std\/default\/trait.Default.html\" rel=\"noopener\" target=\"_blank\"><em>Default<\/em><\/a> trait for it, then define a two-element array with property metadata (names, description, ranges, default value, writability), and then store the default value of our <em>Settings<\/em> struct inside another <em>Mutex<\/em> inside the element struct.<\/p>\n<p>In the next step we have to make use of these: we need to tell the GObject type system about the properties, and we need to implement functions that are called whenever a property value is set or get.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"src\/rgb2gray.rs\">impl Rgb2Gray {\r\n    fn class_init(klass: &amp;mut BaseTransformClass) {\r\n        [...]\r\n        klass.install_properties(&amp;PROPERTIES);\r\n        [...]\r\n    }\r\n}\r\n\r\nimpl ObjectImpl&lt;BaseTransform&gt; for Rgb2Gray {\r\n    fn set_property(&amp;self, obj: &amp;glib::Object, id: u32, value: &amp;glib::Value) {\r\n        let prop = &amp;PROPERTIES[id as usize];\r\n        let element = obj.clone().downcast::&lt;BaseTransform&gt;().unwrap();\r\n\r\n        match *prop {\r\n            Property::Boolean(&quot;invert&quot;, ..) =&gt; {\r\n                let mut settings = self.settings.lock().unwrap();\r\n                let invert = value.get().unwrap();\r\n                gst_info!(\r\n                    self.cat,\r\n                    obj: &amp;element,\r\n                    &quot;Changing invert from {} to {}&quot;,\r\n                    settings.invert,\r\n                    invert\r\n                );\r\n                settings.invert = invert;\r\n            }\r\n            Property::UInt(&quot;shift&quot;, ..) =&gt; {\r\n                let mut settings = self.settings.lock().unwrap();\r\n                let shift = value.get().unwrap();\r\n                gst_info!(\r\n                    self.cat,\r\n                    obj: &amp;element,\r\n                    &quot;Changing shift from {} to {}&quot;,\r\n                    settings.shift,\r\n                    shift\r\n                );\r\n                settings.shift = shift;\r\n            }\r\n            _ =&gt; unimplemented!(),\r\n        }\r\n    }\r\n\r\n    fn get_property(&amp;self, _obj: &amp;glib::Object, id: u32) -&gt; Result&lt;glib::Value, ()&gt; {\r\n        let prop = &amp;PROPERTIES[id as usize];\r\n\r\n        match *prop {\r\n            Property::Boolean(&quot;invert&quot;, ..) =&gt; {\r\n                let settings = self.settings.lock().unwrap();\r\n                Ok(settings.invert.to_value())\r\n            }\r\n            Property::UInt(&quot;shift&quot;, ..) =&gt; {\r\n                let settings = self.settings.lock().unwrap();\r\n                Ok(settings.shift.to_value())\r\n            }\r\n            _ =&gt; unimplemented!(),\r\n        }\r\n    }\r\n}<\/pre>\n<p>Property values can be changed from any thread at any time, that&#8217;s why the <em>Mutex<\/em> is needed here to protect our struct. And we&#8217;re using a new mutex to be able to have it locked only for the shorted possible amount of time: we don&#8217;t want to keep it locked for the whole time of the <em>transform<\/em> function, otherwise applications trying to set\/get values would block for up to one frame.<\/p>\n<p>In the property setter\/getter functions we are working with a <em>glib::Value<\/em>. This is a dynamically typed value type that can contain values of any type, together with the type information of the contained value. Here we&#8217;re using it to handle an unsigned integer (u32) and a boolean for our two properties. To know which property is currently set\/get, we get an identifier passed which is the index into our <em>PROPERTIES<\/em> array. We then simply match on the name of that to decide which property was meant<\/p>\n<p>With this implemented, we can already compile everything, see the properties and their metadata in <em>gst-inspect-1.0<\/em> and can also set them on gst-launch-1.0 like this<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sh\" data-enlighter-title=\"\"># Set invert to true and shift to 128\r\ngst-launch-1.0 videotestsrc ! rsrgb2gray invert=true shift=128 ! videoconvert ! autovideosink<\/pre>\n<p>If we set <em>GST_DEBUG=rsrgb2gray:6<\/em> in the environment before running that, we can also see the corresponding debug output when the values are changing. The only thing missing now is to actually make use of the property values for the processing. For this we add the following changes to <em>bgrx_to_gray<\/em> and the <em>transform<\/em> function<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"rust\" data-enlighter-title=\"\">impl Rgb2Gray {\r\n    #[inline]\r\n    fn bgrx_to_gray(in_p: &amp;[u8], shift: u8, invert: bool) -&gt; u8 {\r\n        [...]\r\n\r\n        let gray = ((r * R_Y) + (g * G_Y) + (b * B_Y)) \/ 65536;\r\n        let gray = (gray as u8).wrapping_add(shift);\r\n\r\n        if invert {\r\n            255 - gray\r\n        } else {\r\n            gray\r\n        }\r\n    }\r\n}\r\n\r\nimpl BaseTransformImpl&lt;BaseTransform&gt; for Rgb2Gray {\r\n    fn transform(\r\n        &amp;self,\r\n        element: &amp;BaseTransform,\r\n        inbuf: &amp;gst::Buffer,\r\n        outbuf: &amp;mut gst::BufferRef,\r\n    ) -&gt; gst::FlowReturn {\r\n        let settings = *self.settings.lock().unwrap();\r\n        [...]\r\n                    let gray = Rgb2Gray::bgrx_to_gray(in_p, settings.shift as u8, settings.invert);\r\n        [...]\r\n    }\r\n}<\/pre>\n<p>And that&#8217;s all. If you run the element in <em>gst-launch-1.0<\/em> and change the values of the properties you should also see the corresponding changes in the video output.<\/p>\n<p>Note that we always take a copy of the <em>Settings<\/em> struct at the beginning of the <em>transform<\/em> function. This ensures that we take the mutex only the shorted possible amount of time and then have a local snapshot of the settings for each frame.<\/p>\n<p>Also keep in mind that the usage of the property values in the <em>bgrx_to_gray<\/em> function is far from optimal. It means the addition of another condition to the calculation of each pixel, thus potentially slowing it down a lot. Ideally this condition would be moved outside the inner loops and the <em>bgrx_to_gray<\/em> function would made generic over that. See for example <a href=\"https:\/\/bluejekyll.github.io\/blog\/rust\/2018\/01\/10\/branchless-rust.html\" rel=\"noopener\" target=\"_blank\">this blog post<\/a> about &#8220;branchless Rust&#8221; for ideas how to do that, the actual implementation is left as an exercise for the reader.<\/p>\n<h3 id=\"what-next\">What next?<\/h3>\n<p>I hope the code walkthrough above was useful to understand how to implement GStreamer plugins and elements in Rust. If you have any questions, feel free to ask them here in the comments.<\/p>\n<p>The same approach also works for audio filters or anything that can be handled in some way with the API of the <em>BaseTransform<\/em> base class. You can find another filter, an audio echo filter, using the same approach <a href=\"https:\/\/github.com\/sdroege\/gst-plugin-rs\/blob\/0.1\/gst-plugin-audiofx\/src\/audioecho.rs\" rel=\"noopener\" target=\"_blank\">here<\/a>.<\/p>\n<p>In the next blog post in this series I&#8217;ll show how to use another base class to implement another kind of element, but for the time being you can also check <a href=\"https:\/\/github.com\/sdroege\/gst-plugin-rs\/tree\/0.1\" rel=\"noopener\" target=\"_blank\">the GIT repository<\/a> for various other element implementations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is part one of a series of blog posts that I&#8217;ll write in the next weeks, as previously announced in the GStreamer Rust bindings 0.10.0 release blog post. Since the last series of blog posts about writing GStreamer plugins in Rust ([1] [2] [3] [4]) a lot has changed, and the content of those &hellip; <a href=\"https:\/\/coaxion.net\/blog\/2018\/01\/how-to-write-gstreamer-elements-in-rust-part-1-a-video-filter-for-converting-rgb-to-grayscale\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to write GStreamer Elements in Rust Part 1: A Video Filter for converting RGB to grayscale<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,6,5,53],"tags":[],"class_list":["post-523","post","type-post","status-publish","format-standard","hentry","category-free-software","category-gnome","category-gstreamer","category-rust"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/coaxion.net\/blog\/wp-json\/wp\/v2\/posts\/523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coaxion.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coaxion.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coaxion.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coaxion.net\/blog\/wp-json\/wp\/v2\/comments?post=523"}],"version-history":[{"count":23,"href":"https:\/\/coaxion.net\/blog\/wp-json\/wp\/v2\/posts\/523\/revisions"}],"predecessor-version":[{"id":564,"href":"https:\/\/coaxion.net\/blog\/wp-json\/wp\/v2\/posts\/523\/revisions\/564"}],"wp:attachment":[{"href":"https:\/\/coaxion.net\/blog\/wp-json\/wp\/v2\/media?parent=523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coaxion.net\/blog\/wp-json\/wp\/v2\/categories?post=523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coaxion.net\/blog\/wp-json\/wp\/v2\/tags?post=523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}