a21ab3d5a8489068e5ce263a061d2584c44f2b4e
[platform/upstream/gstreamer.git] / gst / gaudieffects / gstdilate.c
1 /*
2  * GStreamer
3  * Copyright (C) <2010-2015> Luis de Bethencourt <luis@debethencourt.com>
4  *
5  * Dilate - dilated eye video effect.
6  * Based on Pete Warden's FreeFrame plugin with the same name.
7  *
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:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
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.
25  *
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
29  * mentioned above:
30  *
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.
35  *
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.
40  *
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.
45  */
46
47 /**
48  * SECTION:element-dilate
49  * @title: dilate
50  *
51  * Dilate adjusts the colors of a video stream in realtime.
52  *
53  * ## Example launch line
54  * |[
55  * gst-launch-1.0 -v videotestsrc ! dilate ! videoconvert ! autovideosink
56  * ]| This pipeline shows the effect of dilate on a test stream
57  *
58  */
59
60 #ifdef HAVE_CONFIG_H
61 #  include <config.h>
62 #endif
63
64 #include <gst/gst.h>
65 #include <math.h>
66
67 #include "gstplugin.h"
68 #include "gstdilate.h"
69
70 GST_DEBUG_CATEGORY_STATIC (gst_dilate_debug);
71 #define GST_CAT_DEFAULT gst_dilate_debug
72
73 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
74 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  BGRx, RGBx }")
75 #else
76 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  xBGR, xRGB }")
77 #endif
78
79 #define gst_dilate_parent_class parent_class
80 G_DEFINE_TYPE (GstDilate, gst_dilate, GST_TYPE_VIDEO_FILTER);
81
82 /* Filter signals and args. */
83 enum
84 {
85   LAST_SIGNAL
86 };
87
88 enum
89 {
90   PROP_0,
91   PROP_ERODE,
92 };
93
94 /* Initializations */
95
96 #define DEFAULT_ERODE FALSE
97
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);
101
102 /* The capabilities of the inputs and outputs. */
103
104 static GstStaticPadTemplate gst_dilate_sink_template =
105 GST_STATIC_PAD_TEMPLATE ("sink",
106     GST_PAD_SINK,
107     GST_PAD_ALWAYS,
108     GST_STATIC_CAPS (CAPS_STR)
109     );
110
111 static GstStaticPadTemplate gst_dilate_src_template =
112 GST_STATIC_PAD_TEMPLATE ("src",
113     GST_PAD_SRC,
114     GST_PAD_ALWAYS,
115     GST_STATIC_CAPS (CAPS_STR)
116     );
117
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);
123
124 static GstFlowReturn gst_dilate_transform_frame (GstVideoFilter * vfilter,
125     GstVideoFrame * in_frame, GstVideoFrame * out_frame);
126
127 /* GObject vmethod implementations */
128
129 /* Initialize the dilate's class. */
130 static void
131 gst_dilate_class_init (GstDilateClass * klass)
132 {
133   GObjectClass *gobject_class = (GObjectClass *) klass;
134   GstElementClass *gstelement_class = (GstElementClass *) klass;
135   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
136
137   gst_element_class_set_static_metadata (gstelement_class,
138       "Dilate",
139       "Filter/Effect/Video",
140       "Dilate copies the brightest pixel around.",
141       "Luis de Bethencourt <luis@debethencourt.com>");
142
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);
147
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;
151
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));
155
156   vfilter_class->transform_frame =
157       GST_DEBUG_FUNCPTR (gst_dilate_transform_frame);
158 }
159
160 /* Initialize the element,
161  * instantiate pads and add them to element,
162  * set pad calback functions, and
163  * initialize instance structure.
164  */
165 static void
166 gst_dilate_init (GstDilate * filter)
167 {
168   filter->erode = DEFAULT_ERODE;
169 }
170
171 static void
172 gst_dilate_set_property (GObject * object, guint prop_id,
173     const GValue * value, GParamSpec * pspec)
174 {
175   GstDilate *filter = GST_DILATE (object);
176
177   switch (prop_id) {
178     case PROP_ERODE:
179       filter->erode = g_value_get_boolean (value);
180       break;
181     default:
182       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183       break;
184   }
185 }
186
187 static void
188 gst_dilate_get_property (GObject * object, guint prop_id,
189     GValue * value, GParamSpec * pspec)
190 {
191   GstDilate *filter = GST_DILATE (object);
192
193   GST_OBJECT_LOCK (filter);
194   switch (prop_id) {
195     case PROP_ERODE:
196       g_value_set_boolean (value, filter->erode);
197       break;
198     default:
199       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200       break;
201   }
202   GST_OBJECT_UNLOCK (filter);
203 }
204
205 static void
206 gst_dilate_finalize (GObject * object)
207 {
208   G_OBJECT_CLASS (parent_class)->finalize (object);
209 }
210
211 /* GstElement vmethod implementations */
212
213 /* Actual processing. */
214 static GstFlowReturn
215 gst_dilate_transform_frame (GstVideoFilter * vfilter,
216     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
217 {
218   GstDilate *filter = GST_DILATE (vfilter);
219   gint video_size, width, height;
220   gboolean erode;
221   guint32 *src, *dest;
222   GstClockTime timestamp;
223   gint64 stream_time;
224
225   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
226   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
227
228   width = GST_VIDEO_FRAME_WIDTH (in_frame);
229   height = GST_VIDEO_FRAME_HEIGHT (in_frame);
230
231   /* GstController: update the properties */
232   timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
233   stream_time =
234       gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
235       GST_FORMAT_TIME, timestamp);
236
237   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
238       GST_TIME_ARGS (timestamp));
239
240   if (GST_CLOCK_TIME_IS_VALID (stream_time))
241     gst_object_sync_values (GST_OBJECT (filter), stream_time);
242
243   GST_OBJECT_LOCK (filter);
244   erode = filter->erode;
245   GST_OBJECT_UNLOCK (filter);
246
247   video_size = width * height;
248   transform (src, dest, video_size, width, height, erode);
249
250   return GST_FLOW_OK;
251 }
252
253 /* Entry point to initialize the plug-in.
254  * Register the element factories and other features. */
255 gboolean
256 gst_dilate_plugin_init (GstPlugin * dilate)
257 {
258   /* debug category for fltering log messages */
259   GST_DEBUG_CATEGORY_INIT (gst_dilate_debug, "dilate", 0, "Template dilate");
260
261   return gst_element_register (dilate, "dilate", GST_RANK_NONE,
262       GST_TYPE_DILATE);
263 }
264
265 /*** Now the image processing work.... ***/
266
267 /* Return luminance of the color */
268 static inline guint32
269 get_luminance (guint32 in)
270 {
271   guint32 red, green, blue, luminance;
272
273   red = (in >> 16) & 0xff;
274   green = (in >> 8) & 0xff;
275   blue = (in) & 0xff;
276
277   luminance = ((90 * red) + (115 * green) + (51 * blue));
278
279   return luminance;
280 }
281
282 /* Transform processes each frame. */
283 static void
284 transform (guint32 * src, guint32 * dest, gint video_area, gint width,
285     gint height, gboolean erode)
286 {
287   guint32 out_luminance, down_luminance, right_luminance;
288   guint32 up_luminance, left_luminance;
289
290   guint32 *src_end = src + video_area;
291   guint32 *up;
292   guint32 *left;
293   guint32 *down;
294   guint32 *right;
295
296   while (src != src_end) {
297     guint32 *src_line_start = src;
298     guint32 *src_line_end = src + width;
299
300     while (src != src_line_end) {
301       up = src - width;
302       if (up < src) {
303         up = src;
304       }
305
306       left = src - 1;
307       if (left < src_line_start) {
308         left = src;
309       }
310
311       down = src + width;
312       if (down >= src_end) {
313         down = src;
314       }
315
316       right = src + 1;
317       if (right >= src_line_end) {
318         right = src;
319       }
320
321       *dest = *src;
322       out_luminance = get_luminance (*src);
323
324       down_luminance = get_luminance (*down);
325       if ((erode && down_luminance < out_luminance) ||
326           (!erode && down_luminance > out_luminance)) {
327         *dest = *down;
328         out_luminance = down_luminance;
329       }
330
331       right_luminance = get_luminance (*right);
332       if ((erode && right_luminance < out_luminance) ||
333           (!erode && right_luminance > out_luminance)) {
334         *dest = *right;
335         out_luminance = right_luminance;
336       }
337
338       up_luminance = get_luminance (*up);
339       if ((erode && up_luminance < out_luminance) ||
340           (!erode && up_luminance > out_luminance)) {
341         *dest = *up;
342         out_luminance = up_luminance;
343       }
344
345       left_luminance = get_luminance (*left);
346       if ((erode && left_luminance < out_luminance) ||
347           (!erode && left_luminance > out_luminance)) {
348         *dest = *left;
349       }
350
351       src += 1;
352       dest += 1;
353     }
354   }
355 }