3 * Copyright (C) 2010 Luis de Bethencourt <luis@debethencourt.com>
5 * Dilate - dilated eye 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-dilate
50 * Dilate adjusts the colors of a video stream in realtime.
53 * <title>Example launch line</title>
55 * gst-launch -v videotestsrc ! dilate ! ffmpegcolorspace ! autovideosink
56 * ]| This pipeline shows the effect of dilate on a test stream
67 #include "gstplugin.h"
68 #include "gstdilate.h"
70 #include <gst/video/video.h>
71 #include <gst/controller/gstcontroller.h>
73 GST_DEBUG_CATEGORY_STATIC (gst_dilate_debug);
74 #define GST_CAT_DEFAULT gst_dilate_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_ERODE FALSE
99 static void transform (guint32 * src, guint32 * dest, gint video_area,
100 gint width, gint height, gboolean erode);
101 static inline guint32 get_luminance (guint32 in);
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 (GstDilate, gst_dilate, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
119 static void gst_dilate_set_property (GObject * object, guint prop_id,
120 const GValue * value, GParamSpec * pspec);
121 static void gst_dilate_get_property (GObject * object, guint prop_id,
122 GValue * value, GParamSpec * pspec);
124 static gboolean gst_dilate_set_caps (GstBaseTransform * btrans,
125 GstCaps * incaps, GstCaps * outcaps);
126 static GstFlowReturn gst_dilate_transform (GstBaseTransform * btrans,
127 GstBuffer * in_buf, GstBuffer * out_buf);
129 /* GObject vmethod implementations */
132 gst_dilate_base_init (gpointer gclass)
134 GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
136 gst_element_class_set_details_simple (element_class,
138 "Filter/Effect/Video",
139 "Dilate copies the brightest pixel around.",
140 "Luis de Bethencourt <luis@debethencourt.com>");
142 gst_element_class_add_static_pad_template (element_class, &src_factory);
143 gst_element_class_add_static_pad_template (element_class, &sink_factory);
146 /* Initialize the dilate's class. */
148 gst_dilate_class_init (GstDilateClass * klass)
150 GObjectClass *gobject_class = (GObjectClass *) klass;
151 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
153 gobject_class->set_property = gst_dilate_set_property;
154 gobject_class->get_property = gst_dilate_get_property;
156 g_object_class_install_property (gobject_class, PROP_ERODE,
157 g_param_spec_boolean ("erode", "Erode", "Erode parameter", FALSE,
158 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
160 g_object_class_install_property (gobject_class, PROP_SILENT,
161 g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
162 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_dilate_set_caps);
165 trans_class->transform = GST_DEBUG_FUNCPTR (gst_dilate_transform);
168 /* Initialize the element,
169 * instantiate pads and add them to element,
170 * set pad calback functions, and
171 * initialize instance structure.
174 gst_dilate_init (GstDilate * filter, GstDilateClass * gclass)
176 filter->erode = DEFAULT_ERODE;
177 filter->silent = FALSE;
181 gst_dilate_set_property (GObject * object, guint prop_id,
182 const GValue * value, GParamSpec * pspec)
184 GstDilate *filter = GST_DILATE (object);
188 filter->silent = g_value_get_boolean (value);
191 filter->erode = g_value_get_boolean (value);
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 gst_dilate_get_property (GObject * object, guint prop_id,
201 GValue * value, GParamSpec * pspec)
203 GstDilate *filter = GST_DILATE (object);
205 GST_OBJECT_LOCK (filter);
208 g_value_set_boolean (value, filter->silent);
211 g_value_set_boolean (value, filter->erode);
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 GST_OBJECT_UNLOCK (filter);
220 /* GstElement vmethod implementations */
222 /* Handle the link with other elements. */
224 gst_dilate_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
227 GstDilate *filter = GST_DILATE (btrans);
228 GstStructure *structure;
231 structure = gst_caps_get_structure (incaps, 0);
233 GST_OBJECT_LOCK (filter);
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_dilate_transform (GstBaseTransform * btrans,
246 GstBuffer * in_buf, GstBuffer * out_buf)
248 GstDilate *filter = GST_DILATE (btrans);
251 guint32 *src = (guint32 *) GST_BUFFER_DATA (in_buf);
252 guint32 *dest = (guint32 *) GST_BUFFER_DATA (out_buf);
253 GstClockTime timestamp;
256 video_size = filter->width * filter->height;
258 /* GstController: update the properties */
259 timestamp = GST_BUFFER_TIMESTAMP (in_buf);
261 gst_segment_to_stream_time (&btrans->segment, GST_FORMAT_TIME, timestamp);
263 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
264 GST_TIME_ARGS (timestamp));
266 if (GST_CLOCK_TIME_IS_VALID (stream_time))
267 gst_object_sync_values (G_OBJECT (filter), stream_time);
269 GST_OBJECT_LOCK (filter);
270 erode = filter->erode;
271 GST_OBJECT_UNLOCK (filter);
273 transform (src, dest, video_size, filter->width, filter->height, erode);
278 /* Entry point to initialize the plug-in.
279 * Register the element factories and other features. */
281 gst_dilate_plugin_init (GstPlugin * dilate)
283 /* debug category for fltering log messages */
284 GST_DEBUG_CATEGORY_INIT (gst_dilate_debug, "dilate", 0, "Template dilate");
286 return gst_element_register (dilate, "dilate", GST_RANK_NONE,
290 /*** Now the image processing work.... ***/
292 /* Return luminance of the color */
293 static inline guint32
294 get_luminance (guint32 in)
296 guint32 red, green, blue, luminance;
298 red = (in >> 16) & 0xff;
299 green = (in >> 8) & 0xff;
302 luminance = ((90 * red) + (115 * green) + (51 * blue));
307 /* Transform processes each frame. */
309 transform (guint32 * src, guint32 * dest, gint video_area, gint width,
310 gint height, gboolean erode)
312 guint32 out_luminance, down_luminance, right_luminance;
313 guint32 up_luminance, left_luminance;
315 guint32 *src_end = src + video_area;
323 while (src != src_end) {
324 guint32 *src_line_start = src;
325 guint32 *src_line_end = src + width;
326 while (src != src_line_end) {
334 if (left < src_line_start) {
339 if (down >= src_end) {
344 if (right >= src_line_end) {
349 out_luminance = get_luminance (*src);
351 down_luminance = get_luminance (*down);
352 if (down_luminance < out_luminance) {
354 out_luminance = down_luminance;
357 right_luminance = get_luminance (*right);
358 if (right_luminance < out_luminance) {
360 out_luminance = right_luminance;
363 up_luminance = get_luminance (*up);
364 if (up_luminance < out_luminance) {
366 out_luminance = up_luminance;
369 left_luminance = get_luminance (*left);
370 if (left_luminance < out_luminance) {
372 out_luminance = left_luminance;
382 while (src != src_end) {
383 guint32 *src_line_start = src;
384 guint32 *src_line_end = src + width;
385 while (src != src_line_end) {
393 if (left < src_line_start) {
398 if (down >= src_end) {
403 if (right >= src_line_end) {
408 out_luminance = get_luminance (*src);
410 down_luminance = get_luminance (*down);
411 if (down_luminance > out_luminance) {
413 out_luminance = down_luminance;
416 right_luminance = get_luminance (*right);
417 if (right_luminance > out_luminance) {
419 out_luminance = right_luminance;
422 up_luminance = get_luminance (*up);
423 if (up_luminance > out_luminance) {
425 out_luminance = up_luminance;
428 left_luminance = get_luminance (*left);
429 if (left_luminance > out_luminance) {
431 out_luminance = left_luminance;