3 * Copyright (C) <2010-2015> 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., 51 Franklin St, Fifth Floor,
44 * Boston, MA 02110-1301, USA.
48 * SECTION:element-dilate
51 * Dilate adjusts the colors of a video stream in realtime.
53 * ## Example launch line
55 * gst-launch-1.0 -v videotestsrc ! dilate ! videoconvert ! autovideosink
56 * ]| This pipeline shows the effect of dilate on a test stream
67 #include "gstplugin.h"
68 #include "gstdilate.h"
70 GST_DEBUG_CATEGORY_STATIC (gst_dilate_debug);
71 #define GST_CAT_DEFAULT gst_dilate_debug
73 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
74 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ BGRx, RGBx }")
76 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ xBGR, xRGB }")
79 #define gst_dilate_parent_class parent_class
80 G_DEFINE_TYPE (GstDilate, gst_dilate, GST_TYPE_VIDEO_FILTER);
82 /* Filter signals and args. */
96 #define DEFAULT_ERODE FALSE
98 static void transform (guint32 * src, guint32 * dest, gint video_area,
99 gint width, gint height, gboolean erode);
100 static inline guint32 get_luminance (guint32 in);
102 /* The capabilities of the inputs and outputs. */
104 static GstStaticPadTemplate gst_dilate_sink_template =
105 GST_STATIC_PAD_TEMPLATE ("sink",
108 GST_STATIC_CAPS (CAPS_STR)
111 static GstStaticPadTemplate gst_dilate_src_template =
112 GST_STATIC_PAD_TEMPLATE ("src",
115 GST_STATIC_CAPS (CAPS_STR)
118 static void gst_dilate_set_property (GObject * object, guint prop_id,
119 const GValue * value, GParamSpec * pspec);
120 static void gst_dilate_get_property (GObject * object, guint prop_id,
121 GValue * value, GParamSpec * pspec);
122 static void gst_dilate_finalize (GObject * object);
124 static GstFlowReturn gst_dilate_transform_frame (GstVideoFilter * vfilter,
125 GstVideoFrame * in_frame, GstVideoFrame * out_frame);
127 /* GObject vmethod implementations */
129 /* Initialize the dilate's class. */
131 gst_dilate_class_init (GstDilateClass * klass)
133 GObjectClass *gobject_class = (GObjectClass *) klass;
134 GstElementClass *gstelement_class = (GstElementClass *) klass;
135 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
137 gst_element_class_set_static_metadata (gstelement_class,
139 "Filter/Effect/Video",
140 "Dilate copies the brightest pixel around.",
141 "Luis de Bethencourt <luis@debethencourt.com>");
143 gst_element_class_add_static_pad_template (gstelement_class,
144 &gst_dilate_sink_template);
145 gst_element_class_add_static_pad_template (gstelement_class,
146 &gst_dilate_src_template);
148 gobject_class->set_property = gst_dilate_set_property;
149 gobject_class->get_property = gst_dilate_get_property;
150 gobject_class->finalize = gst_dilate_finalize;
152 g_object_class_install_property (gobject_class, PROP_ERODE,
153 g_param_spec_boolean ("erode", "Erode", "Erode parameter", FALSE,
154 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
156 vfilter_class->transform_frame =
157 GST_DEBUG_FUNCPTR (gst_dilate_transform_frame);
160 /* Initialize the element,
161 * instantiate pads and add them to element,
162 * set pad callback functions, and
163 * initialize instance structure.
166 gst_dilate_init (GstDilate * filter)
168 filter->erode = DEFAULT_ERODE;
172 gst_dilate_set_property (GObject * object, guint prop_id,
173 const GValue * value, GParamSpec * pspec)
175 GstDilate *filter = GST_DILATE (object);
179 filter->erode = g_value_get_boolean (value);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 gst_dilate_get_property (GObject * object, guint prop_id,
189 GValue * value, GParamSpec * pspec)
191 GstDilate *filter = GST_DILATE (object);
193 GST_OBJECT_LOCK (filter);
196 g_value_set_boolean (value, filter->erode);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202 GST_OBJECT_UNLOCK (filter);
206 gst_dilate_finalize (GObject * object)
208 G_OBJECT_CLASS (parent_class)->finalize (object);
211 /* GstElement vmethod implementations */
213 /* Actual processing. */
215 gst_dilate_transform_frame (GstVideoFilter * vfilter,
216 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
218 GstDilate *filter = GST_DILATE (vfilter);
219 gint video_size, width, height;
222 GstClockTime timestamp;
225 src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
226 dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
228 width = GST_VIDEO_FRAME_WIDTH (in_frame);
229 height = GST_VIDEO_FRAME_HEIGHT (in_frame);
231 /* GstController: update the properties */
232 timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
234 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
235 GST_FORMAT_TIME, timestamp);
237 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
238 GST_TIME_ARGS (timestamp));
240 if (GST_CLOCK_TIME_IS_VALID (stream_time))
241 gst_object_sync_values (GST_OBJECT (filter), stream_time);
243 GST_OBJECT_LOCK (filter);
244 erode = filter->erode;
245 GST_OBJECT_UNLOCK (filter);
247 video_size = width * height;
248 transform (src, dest, video_size, width, height, erode);
253 /* Entry point to initialize the plug-in.
254 * Register the element factories and other features. */
256 gst_dilate_plugin_init (GstPlugin * dilate)
258 /* debug category for fltering log messages */
259 GST_DEBUG_CATEGORY_INIT (gst_dilate_debug, "dilate", 0, "Template dilate");
261 return gst_element_register (dilate, "dilate", GST_RANK_NONE,
265 /*** Now the image processing work.... ***/
267 /* Return luminance of the color */
268 static inline guint32
269 get_luminance (guint32 in)
271 guint32 red, green, blue, luminance;
273 red = (in >> 16) & 0xff;
274 green = (in >> 8) & 0xff;
277 luminance = ((90 * red) + (115 * green) + (51 * blue));
282 /* Transform processes each frame. */
284 transform (guint32 * src, guint32 * dest, gint video_area, gint width,
285 gint height, gboolean erode)
287 guint32 out_luminance, down_luminance, right_luminance;
288 guint32 up_luminance, left_luminance;
290 guint32 *src_end = src + video_area;
296 while (src != src_end) {
297 guint32 *src_line_start = src;
298 guint32 *src_line_end = src + width;
300 while (src != src_line_end) {
307 if (left < src_line_start) {
312 if (down >= src_end) {
317 if (right >= src_line_end) {
322 out_luminance = get_luminance (*src);
324 down_luminance = get_luminance (*down);
325 if ((erode && down_luminance < out_luminance) ||
326 (!erode && down_luminance > out_luminance)) {
328 out_luminance = down_luminance;
331 right_luminance = get_luminance (*right);
332 if ((erode && right_luminance < out_luminance) ||
333 (!erode && right_luminance > out_luminance)) {
335 out_luminance = right_luminance;
338 up_luminance = get_luminance (*up);
339 if ((erode && up_luminance < out_luminance) ||
340 (!erode && up_luminance > out_luminance)) {
342 out_luminance = up_luminance;
345 left_luminance = get_luminance (*left);
346 if ((erode && left_luminance < out_luminance) ||
347 (!erode && left_luminance > out_luminance)) {