Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / gaudieffects / gstdilate.c
1 /*
2  * GStreamer
3  * Copyright (C) 2010 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., 59 Temple Place - Suite 330,
44  * Boston, MA 02111-1307, USA.
45  */
46
47 /**
48  * SECTION:element-dilate
49  *
50  * Dilate adjusts the colors of a video stream in realtime.
51  *
52  * <refsect2>
53  * <title>Example launch line</title>
54  * |[
55  * gst-launch -v videotestsrc ! dilate ! ffmpegcolorspace ! autovideosink
56  * ]| This pipeline shows the effect of dilate on a test stream
57  * </refsect2>
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 #include <gst/video/video.h>
71 #include <gst/controller/gstcontroller.h>
72
73 GST_DEBUG_CATEGORY_STATIC (gst_dilate_debug);
74 #define GST_CAT_DEFAULT gst_dilate_debug
75
76 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
77 #define CAPS_STR GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_RGBx
78 #else
79 #define CAPS_STR GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR
80 #endif
81
82 /* Filter signals and args. */
83 enum
84 {
85   LAST_SIGNAL
86 };
87
88 enum
89 {
90   PROP_0,
91   PROP_ERODE,
92   PROP_SILENT
93 };
94
95 /* Initializations */
96
97 #define DEFAULT_ERODE FALSE
98
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);
102
103 /* The capabilities of the inputs and outputs. */
104
105 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
106     GST_PAD_SINK,
107     GST_PAD_ALWAYS,
108     GST_STATIC_CAPS (CAPS_STR)
109     );
110
111 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
112     GST_PAD_SRC,
113     GST_PAD_ALWAYS,
114     GST_STATIC_CAPS (CAPS_STR)
115     );
116
117 GST_BOILERPLATE (GstDilate, gst_dilate, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
118
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);
123
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);
128
129 /* GObject vmethod implementations */
130
131 static void
132 gst_dilate_base_init (gpointer gclass)
133 {
134   GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
135
136   gst_element_class_set_details_simple (element_class,
137       "Dilate",
138       "Filter/Effect/Video",
139       "Dilate copies the brightest pixel around.",
140       "Luis de Bethencourt <luis@debethencourt.com>");
141
142   gst_element_class_add_static_pad_template (element_class, &src_factory);
143   gst_element_class_add_static_pad_template (element_class, &sink_factory);
144 }
145
146 /* Initialize the dilate's class. */
147 static void
148 gst_dilate_class_init (GstDilateClass * klass)
149 {
150   GObjectClass *gobject_class = (GObjectClass *) klass;
151   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
152
153   gobject_class->set_property = gst_dilate_set_property;
154   gobject_class->get_property = gst_dilate_get_property;
155
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));
159
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));
163
164   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_dilate_set_caps);
165   trans_class->transform = GST_DEBUG_FUNCPTR (gst_dilate_transform);
166 }
167
168 /* Initialize the element,
169  * instantiate pads and add them to element,
170  * set pad calback functions, and
171  * initialize instance structure.
172  */
173 static void
174 gst_dilate_init (GstDilate * filter, GstDilateClass * gclass)
175 {
176   filter->erode = DEFAULT_ERODE;
177   filter->silent = FALSE;
178 }
179
180 static void
181 gst_dilate_set_property (GObject * object, guint prop_id,
182     const GValue * value, GParamSpec * pspec)
183 {
184   GstDilate *filter = GST_DILATE (object);
185
186   switch (prop_id) {
187     case PROP_SILENT:
188       filter->silent = g_value_get_boolean (value);
189       break;
190     case PROP_ERODE:
191       filter->erode = g_value_get_boolean (value);
192       break;
193     default:
194       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195       break;
196   }
197 }
198
199 static void
200 gst_dilate_get_property (GObject * object, guint prop_id,
201     GValue * value, GParamSpec * pspec)
202 {
203   GstDilate *filter = GST_DILATE (object);
204
205   GST_OBJECT_LOCK (filter);
206   switch (prop_id) {
207     case PROP_SILENT:
208       g_value_set_boolean (value, filter->silent);
209       break;
210     case PROP_ERODE:
211       g_value_set_boolean (value, filter->erode);
212       break;
213     default:
214       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215       break;
216   }
217   GST_OBJECT_UNLOCK (filter);
218 }
219
220 /* GstElement vmethod implementations */
221
222 /* Handle the link with other elements. */
223 static gboolean
224 gst_dilate_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
225     GstCaps * outcaps)
226 {
227   GstDilate *filter = GST_DILATE (btrans);
228   GstStructure *structure;
229   gboolean ret = TRUE;
230
231   structure = gst_caps_get_structure (incaps, 0);
232
233   GST_OBJECT_LOCK (filter);
234   if (gst_structure_get_int (structure, "width", &filter->width) &&
235       gst_structure_get_int (structure, "height", &filter->height)) {
236     ret = TRUE;
237   }
238   GST_OBJECT_UNLOCK (filter);
239
240   return ret;
241 }
242
243 /* Actual processing. */
244 static GstFlowReturn
245 gst_dilate_transform (GstBaseTransform * btrans,
246     GstBuffer * in_buf, GstBuffer * out_buf)
247 {
248   GstDilate *filter = GST_DILATE (btrans);
249   gint video_size;
250   gboolean erode;
251   guint32 *src = (guint32 *) GST_BUFFER_DATA (in_buf);
252   guint32 *dest = (guint32 *) GST_BUFFER_DATA (out_buf);
253   GstClockTime timestamp;
254   gint64 stream_time;
255
256   video_size = filter->width * filter->height;
257
258   /* GstController: update the properties */
259   timestamp = GST_BUFFER_TIMESTAMP (in_buf);
260   stream_time =
261       gst_segment_to_stream_time (&btrans->segment, GST_FORMAT_TIME, timestamp);
262
263   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
264       GST_TIME_ARGS (timestamp));
265
266   if (GST_CLOCK_TIME_IS_VALID (stream_time))
267     gst_object_sync_values (G_OBJECT (filter), stream_time);
268
269   GST_OBJECT_LOCK (filter);
270   erode = filter->erode;
271   GST_OBJECT_UNLOCK (filter);
272
273   transform (src, dest, video_size, filter->width, filter->height, erode);
274
275   return GST_FLOW_OK;
276 }
277
278 /* Entry point to initialize the plug-in.
279  * Register the element factories and other features. */
280 gboolean
281 gst_dilate_plugin_init (GstPlugin * dilate)
282 {
283   /* debug category for fltering log messages */
284   GST_DEBUG_CATEGORY_INIT (gst_dilate_debug, "dilate", 0, "Template dilate");
285
286   return gst_element_register (dilate, "dilate", GST_RANK_NONE,
287       GST_TYPE_DILATE);
288 }
289
290 /*** Now the image processing work.... ***/
291
292 /* Return luminance of the color */
293 static inline guint32
294 get_luminance (guint32 in)
295 {
296   guint32 red, green, blue, luminance;
297
298   red = (in >> 16) & 0xff;
299   green = (in >> 8) & 0xff;
300   blue = (in) & 0xff;
301
302   luminance = ((90 * red) + (115 * green) + (51 * blue));
303
304   return luminance;
305 }
306
307 /* Transform processes each frame. */
308 static void
309 transform (guint32 * src, guint32 * dest, gint video_area, gint width,
310     gint height, gboolean erode)
311 {
312   guint32 out_luminance, down_luminance, right_luminance;
313   guint32 up_luminance, left_luminance;
314
315   guint32 *src_end = src + video_area;
316   guint32 *up;
317   guint32 *left;
318   guint32 *down;
319   guint32 *right;
320
321   if (erode) {
322
323     while (src != src_end) {
324       guint32 *src_line_start = src;
325       guint32 *src_line_end = src + width;
326       while (src != src_line_end) {
327
328         up = src - width;
329         if (up < src) {
330           up = src;
331         }
332
333         left = src - 1;
334         if (left < src_line_start) {
335           left = src;
336         }
337
338         down = src + width;
339         if (down >= src_end) {
340           down = src;
341         }
342
343         right = src + 1;
344         if (right >= src_line_end) {
345           right = src;
346         }
347
348         *dest = *src;
349         out_luminance = get_luminance (*src);
350
351         down_luminance = get_luminance (*down);
352         if (down_luminance < out_luminance) {
353           *dest = *down;
354           out_luminance = down_luminance;
355         }
356
357         right_luminance = get_luminance (*right);
358         if (right_luminance < out_luminance) {
359           *dest = *right;
360           out_luminance = right_luminance;
361         }
362
363         up_luminance = get_luminance (*up);
364         if (up_luminance < out_luminance) {
365           *dest = *up;
366           out_luminance = up_luminance;
367         }
368
369         left_luminance = get_luminance (*left);
370         if (left_luminance < out_luminance) {
371           *dest = *left;
372           out_luminance = left_luminance;
373         }
374
375         src += 1;
376         dest += 1;
377       }
378     }
379
380   } else {
381
382     while (src != src_end) {
383       guint32 *src_line_start = src;
384       guint32 *src_line_end = src + width;
385       while (src != src_line_end) {
386
387         up = src - width;
388         if (up < src) {
389           up = src;
390         }
391
392         left = src - 1;
393         if (left < src_line_start) {
394           left = src;
395         }
396
397         down = src + width;
398         if (down >= src_end) {
399           down = src;
400         }
401
402         right = src + 1;
403         if (right >= src_line_end) {
404           right = src;
405         }
406
407         *dest = *src;
408         out_luminance = get_luminance (*src);
409
410         down_luminance = get_luminance (*down);
411         if (down_luminance > out_luminance) {
412           *dest = *down;
413           out_luminance = down_luminance;
414         }
415
416         right_luminance = get_luminance (*right);
417         if (right_luminance > out_luminance) {
418           *dest = *right;
419           out_luminance = right_luminance;
420         }
421
422         up_luminance = get_luminance (*up);
423         if (up_luminance > out_luminance) {
424           *dest = *up;
425           out_luminance = up_luminance;
426         }
427
428         left_luminance = get_luminance (*left);
429         if (left_luminance > out_luminance) {
430           *dest = *left;
431           out_luminance = left_luminance;
432         }
433
434         src += 1;
435         dest += 1;
436       }
437     }
438   }
439 }