Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / gaudieffects / gstexclusion.c
1 /*
2  * GStreamer
3  * Copyright (C) 2010 Luis de Bethencourt <luis@debethencourt.com>
4  * 
5  * Exclusion - color exclusion 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-exclusion
49  *
50  * Exclusion saturates the colors of a video stream in realtime.
51  *
52  * <refsect2>
53  * <title>Example launch line</title>
54  * |[
55  * gst-launch -v videotestsrc ! exclusion ! ffmpegcolorspace ! autovideosink
56  * ]| This pipeline shows the effect of exclusion 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 "gstexclusion.h"
69
70 #include <gst/video/video.h>
71 #include <gst/controller/gstcontroller.h>
72
73 GST_DEBUG_CATEGORY_STATIC (gst_exclusion_debug);
74 #define GST_CAT_DEFAULT gst_exclusion_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 = 0,
91   PROP_FACTOR,
92   PROP_SILENT
93 };
94
95 /* Initializations */
96
97 #define DEFAULT_FACTOR 175
98
99 static gint gate_int (gint value, gint min, gint max);
100 static void transform (guint32 * src, guint32 * dest, gint video_area,
101     gint factor);
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 (GstExclusion, gst_exclusion, GstVideoFilter,
118     GST_TYPE_VIDEO_FILTER);
119
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);
124
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);
129
130 /* GObject vmethod implementations */
131
132 static void
133 gst_exclusion_base_init (gpointer gclass)
134 {
135   GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
136
137   gst_element_class_set_details_simple (element_class,
138       "Exclusion",
139       "Filter/Effect/Video",
140       "Exclusion exclodes the colors in the video signal.",
141       "Luis de Bethencourt <luis@debethencourt.com>");
142
143   gst_element_class_add_static_pad_template (element_class, &src_factory);
144   gst_element_class_add_static_pad_template (element_class, &sink_factory);
145 }
146
147 /* Initialize the exclusion's class. */
148 static void
149 gst_exclusion_class_init (GstExclusionClass * klass)
150 {
151   GObjectClass *gobject_class = (GObjectClass *) klass;
152   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
153
154   gobject_class->set_property = gst_exclusion_set_property;
155   gobject_class->get_property = gst_exclusion_get_property;
156
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));
161
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));
165
166   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_exclusion_set_caps);
167   trans_class->transform = GST_DEBUG_FUNCPTR (gst_exclusion_transform);
168 }
169
170 /* Initialize the element,
171  * instantiate pads and add them to element,
172  * set pad calback functions, and
173  * initialize instance structure.
174  */
175 static void
176 gst_exclusion_init (GstExclusion * filter, GstExclusionClass * gclass)
177 {
178   filter->factor = DEFAULT_FACTOR;
179   filter->silent = FALSE;
180 }
181
182 static void
183 gst_exclusion_set_property (GObject * object, guint prop_id,
184     const GValue * value, GParamSpec * pspec)
185 {
186   GstExclusion *filter = GST_EXCLUSION (object);
187
188   switch (prop_id) {
189     case PROP_SILENT:
190       filter->silent = g_value_get_boolean (value);
191       break;
192     case PROP_FACTOR:
193       filter->factor = g_value_get_uint (value);
194       break;
195     default:
196       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
197       break;
198   }
199 }
200
201 static void
202 gst_exclusion_get_property (GObject * object, guint prop_id,
203     GValue * value, GParamSpec * pspec)
204 {
205   GstExclusion *filter = GST_EXCLUSION (object);
206
207   GST_OBJECT_LOCK (filter);
208   switch (prop_id) {
209     case PROP_SILENT:
210       g_value_set_boolean (value, filter->silent);
211       break;
212     case PROP_FACTOR:
213       g_value_set_uint (value, filter->factor);
214       break;
215     default:
216       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217       break;
218   }
219   GST_OBJECT_UNLOCK (filter);
220 }
221
222 /* GstElement vmethod implementations */
223 /* Handle the link with other elements. */
224 static gboolean
225 gst_exclusion_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
226     GstCaps * outcaps)
227 {
228   GstExclusion *filter = GST_EXCLUSION (btrans);
229   GstStructure *structure;
230   gboolean ret = FALSE;
231
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)) {
236     ret = TRUE;
237   }
238   GST_OBJECT_UNLOCK (filter);
239
240   return ret;
241 }
242
243 /* Actual processing. */
244 static GstFlowReturn
245 gst_exclusion_transform (GstBaseTransform * btrans,
246     GstBuffer * in_buf, GstBuffer * out_buf)
247 {
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;
253   gint64 stream_time;
254
255   /* GstController: update the properties */
256   timestamp = GST_BUFFER_TIMESTAMP (in_buf);
257   stream_time =
258       gst_segment_to_stream_time (&btrans->segment, GST_FORMAT_TIME, timestamp);
259
260   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
261       GST_TIME_ARGS (timestamp));
262
263   if (GST_CLOCK_TIME_IS_VALID (stream_time))
264     gst_object_sync_values (G_OBJECT (filter), stream_time);
265
266   GST_OBJECT_LOCK (filter);
267   factor = filter->factor;
268   GST_OBJECT_UNLOCK (filter);
269
270   video_size = filter->width * filter->height;
271   transform (src, dest, video_size, factor);
272
273   return GST_FLOW_OK;
274 }
275
276 /* Entry point to initialize the plug-in.
277  * Register the element factories and other features. */
278 gboolean
279 gst_exclusion_plugin_init (GstPlugin * exclusion)
280 {
281   /* debug category for fltering log messages */
282   GST_DEBUG_CATEGORY_INIT (gst_exclusion_debug, "exclusion",
283       0, "Template exclusion");
284
285   return gst_element_register (exclusion, "exclusion", GST_RANK_NONE,
286       GST_TYPE_EXCLUSION);
287 }
288
289 /*** Now the image processing work.... ***/
290 /* Keep the values inbounds. */
291 static gint
292 gate_int (gint value, gint min, gint max)
293 {
294   if (value < min) {
295     return min;
296   } else if (value > max) {
297     return max;
298   } else {
299     return value;
300   }
301 }
302
303 /* Transform processes each frame. */
304 static void
305 transform (guint32 * src, guint32 * dest, gint video_area, gint factor)
306 {
307   guint32 in, red, green, blue;
308   gint x;
309
310   for (x = 0; x < video_area; x++) {
311     in = *src++;
312
313     red = (in >> 16) & 0xff;
314     green = (in >> 8) & 0xff;
315     blue = (in) & 0xff;
316
317     red = factor -
318         (((factor - red) * (factor - red) / factor) + ((green * red) / factor));
319     green = factor -
320         (((factor - green) * (factor - green) / factor) +
321         ((green * green) / factor));
322     blue = factor -
323         (((factor - blue) * (factor - blue) / factor) +
324         ((blue * blue) / factor));
325
326     red = gate_int (red, 0, 255);
327     green = gate_int (green, 0, 255);
328     blue = gate_int (blue, 0, 255);
329
330     *dest++ = (red << 16) | (green << 8) | blue;
331   }
332 }