3 * Copyright (C) 2010 Luis de Bethencourt <luis@debethencourt.com>
5 * Exclusion - color exclusion video effect.
6 * Based on Pete Warden's FreeFrame plugin with the same name.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * Alternatively, the contents of this file may be used under the
27 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
28 * which case the following provisions apply instead of the ones
31 * This library is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU Library General Public
33 * License as published by the Free Software Foundation; either
34 * version 2 of the License, or (at your option) any later version.
36 * This library is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 * Library General Public License for more details.
41 * You should have received a copy of the GNU Library General Public
42 * License along with this library; if not, write to the
43 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
44 * Boston, MA 02111-1307, USA.
48 * SECTION:element-exclusion
50 * Exclusion saturates the colors of a video stream in realtime.
53 * <title>Example launch line</title>
55 * gst-launch -v videotestsrc ! exclusion ! ffmpegcolorspace ! autovideosink
56 * ]| This pipeline shows the effect of exclusion on a test stream
67 #include "gstplugin.h"
68 #include "gstexclusion.h"
70 #include <gst/video/video.h>
71 #include <gst/controller/gstcontroller.h>
73 GST_DEBUG_CATEGORY_STATIC (gst_exclusion_debug);
74 #define GST_CAT_DEFAULT gst_exclusion_debug
76 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
77 #define CAPS_STR GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_RGBx
79 #define CAPS_STR GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR
82 /* Filter signals and args. */
97 #define DEFAULT_FACTOR 175
99 static gint gate_int (gint value, gint min, gint max);
100 static void transform (guint32 * src, guint32 * dest, gint video_area,
103 /* The capabilities of the inputs and outputs. */
105 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
108 GST_STATIC_CAPS (CAPS_STR)
111 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
114 GST_STATIC_CAPS (CAPS_STR)
117 GST_BOILERPLATE (GstExclusion, gst_exclusion, GstVideoFilter,
118 GST_TYPE_VIDEO_FILTER);
120 static void gst_exclusion_set_property (GObject * object, guint prop_id,
121 const GValue * value, GParamSpec * pspec);
122 static void gst_exclusion_get_property (GObject * object, guint prop_id,
123 GValue * value, GParamSpec * pspec);
125 static gboolean gst_exclusion_set_caps (GstBaseTransform * btrans,
126 GstCaps * incaps, GstCaps * outcaps);
127 static GstFlowReturn gst_exclusion_transform (GstBaseTransform * btrans,
128 GstBuffer * in_buf, GstBuffer * out_buf);
130 /* GObject vmethod implementations */
133 gst_exclusion_base_init (gpointer gclass)
135 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
137 gst_element_class_set_details_simple (element_class,
139 "Filter/Effect/Video",
140 "Exclusion exclodes the colors in the video signal.",
141 "Luis de Bethencourt <luis@debethencourt.com>");
143 gst_element_class_add_static_pad_template (element_class, &src_factory);
144 gst_element_class_add_static_pad_template (element_class, &sink_factory);
147 /* Initialize the exclusion's class. */
149 gst_exclusion_class_init (GstExclusionClass * klass)
151 GObjectClass *gobject_class = (GObjectClass *) klass;
152 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
154 gobject_class->set_property = gst_exclusion_set_property;
155 gobject_class->get_property = gst_exclusion_get_property;
157 g_object_class_install_property (gobject_class, PROP_FACTOR,
158 g_param_spec_uint ("factor", "Factor",
159 "Exclusion factor parameter", 0, 175, DEFAULT_FACTOR,
160 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
162 g_object_class_install_property (gobject_class, PROP_SILENT,
163 g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
164 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
166 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_exclusion_set_caps);
167 trans_class->transform = GST_DEBUG_FUNCPTR (gst_exclusion_transform);
170 /* Initialize the element,
171 * instantiate pads and add them to element,
172 * set pad calback functions, and
173 * initialize instance structure.
176 gst_exclusion_init (GstExclusion * filter, GstExclusionClass * gclass)
178 filter->factor = DEFAULT_FACTOR;
179 filter->silent = FALSE;
183 gst_exclusion_set_property (GObject * object, guint prop_id,
184 const GValue * value, GParamSpec * pspec)
186 GstExclusion *filter = GST_EXCLUSION (object);
190 filter->silent = g_value_get_boolean (value);
193 filter->factor = g_value_get_uint (value);
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202 gst_exclusion_get_property (GObject * object, guint prop_id,
203 GValue * value, GParamSpec * pspec)
205 GstExclusion *filter = GST_EXCLUSION (object);
207 GST_OBJECT_LOCK (filter);
210 g_value_set_boolean (value, filter->silent);
213 g_value_set_uint (value, filter->factor);
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 GST_OBJECT_UNLOCK (filter);
222 /* GstElement vmethod implementations */
223 /* Handle the link with other elements. */
225 gst_exclusion_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
228 GstExclusion *filter = GST_EXCLUSION (btrans);
229 GstStructure *structure;
230 gboolean ret = FALSE;
232 GST_OBJECT_LOCK (filter);
233 structure = gst_caps_get_structure (incaps, 0);
234 if (gst_structure_get_int (structure, "width", &filter->width) &&
235 gst_structure_get_int (structure, "height", &filter->height)) {
238 GST_OBJECT_UNLOCK (filter);
243 /* Actual processing. */
245 gst_exclusion_transform (GstBaseTransform * btrans,
246 GstBuffer * in_buf, GstBuffer * out_buf)
248 GstExclusion *filter = GST_EXCLUSION (btrans);
249 gint video_size, factor;
250 guint32 *src = (guint32 *) GST_BUFFER_DATA (in_buf);
251 guint32 *dest = (guint32 *) GST_BUFFER_DATA (out_buf);
252 GstClockTime timestamp;
255 /* GstController: update the properties */
256 timestamp = GST_BUFFER_TIMESTAMP (in_buf);
258 gst_segment_to_stream_time (&btrans->segment, GST_FORMAT_TIME, timestamp);
260 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
261 GST_TIME_ARGS (timestamp));
263 if (GST_CLOCK_TIME_IS_VALID (stream_time))
264 gst_object_sync_values (G_OBJECT (filter), stream_time);
266 GST_OBJECT_LOCK (filter);
267 factor = filter->factor;
268 GST_OBJECT_UNLOCK (filter);
270 video_size = filter->width * filter->height;
271 transform (src, dest, video_size, factor);
276 /* Entry point to initialize the plug-in.
277 * Register the element factories and other features. */
279 gst_exclusion_plugin_init (GstPlugin * exclusion)
281 /* debug category for fltering log messages */
282 GST_DEBUG_CATEGORY_INIT (gst_exclusion_debug, "exclusion",
283 0, "Template exclusion");
285 return gst_element_register (exclusion, "exclusion", GST_RANK_NONE,
289 /*** Now the image processing work.... ***/
290 /* Keep the values inbounds. */
292 gate_int (gint value, gint min, gint max)
296 } else if (value > max) {
303 /* Transform processes each frame. */
305 transform (guint32 * src, guint32 * dest, gint video_area, gint factor)
307 guint32 in, red, green, blue;
310 for (x = 0; x < video_area; x++) {
313 red = (in >> 16) & 0xff;
314 green = (in >> 8) & 0xff;
318 (((factor - red) * (factor - red) / factor) + ((green * red) / factor));
320 (((factor - green) * (factor - green) / factor) +
321 ((green * green) / factor));
323 (((factor - blue) * (factor - blue) / factor) +
324 ((blue * blue) / factor));
326 red = gate_int (red, 0, 255);
327 green = gate_int (green, 0, 255);
328 blue = gate_int (blue, 0, 255);
330 *dest++ = (red << 16) | (green << 8) | blue;