Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / gaudieffects / gstdodge.c
1 /*
2  * GStreamer
3  * Copyright (C) 2010 Luis de Bethencourt <luis@debethencourt.com>
4  * 
5  * Dodge - saturation 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-dodge
49  *
50  * Dodge saturates the colors of a video stream in realtime.
51  *
52  * <refsect2>
53  * <title>Example launch line</title>
54  * |[
55  * gst-launch -v videotestsrc ! dodge ! ffmpegcolorspace ! autovideosink
56  * ]| This pipeline shows the effect of dodge 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 "gstdodge.h"
69
70 #include <gst/video/video.h>
71 #include <gst/controller/gstcontroller.h>
72
73 GST_DEBUG_CATEGORY_STATIC (gst_dodge_debug);
74 #define GST_CAT_DEFAULT gst_dodge_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_SILENT
92 };
93
94 /* Initializations */
95
96 static gint gate_int (gint value, gint min, gint max);
97 static void transform (guint32 * src, guint32 * dest, gint video_area);
98
99 /* The capabilities of the inputs and outputs. */
100
101 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
102     GST_PAD_SINK,
103     GST_PAD_ALWAYS,
104     GST_STATIC_CAPS (CAPS_STR)
105     );
106
107 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
108     GST_PAD_SRC,
109     GST_PAD_ALWAYS,
110     GST_STATIC_CAPS (CAPS_STR)
111     );
112
113 GST_BOILERPLATE (GstDodge, gst_dodge, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
114
115 static void gst_dodge_set_property (GObject * object, guint prop_id,
116     const GValue * value, GParamSpec * pspec);
117 static void gst_dodge_get_property (GObject * object, guint prop_id,
118     GValue * value, GParamSpec * pspec);
119
120 static gboolean gst_dodge_set_caps (GstBaseTransform * btrans,
121     GstCaps * incaps, GstCaps * outcaps);
122 static GstFlowReturn gst_dodge_transform (GstBaseTransform * btrans,
123     GstBuffer * in_buf, GstBuffer * out_buf);
124
125 /* GObject vmethod implementations */
126
127 static void
128 gst_dodge_base_init (gpointer gclass)
129 {
130   GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
131
132   gst_element_class_set_details_simple (element_class,
133       "Dodge",
134       "Filter/Effect/Video",
135       "Dodge saturates the colors in the video signal.",
136       "Luis de Bethencourt <luis@debethencourt.com>");
137
138   gst_element_class_add_static_pad_template (element_class, &src_factory);
139   gst_element_class_add_static_pad_template (element_class, &sink_factory);
140 }
141
142 /* Initialize the dodge's class. */
143 static void
144 gst_dodge_class_init (GstDodgeClass * klass)
145 {
146   GObjectClass *gobject_class = (GObjectClass *) klass;
147   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
148
149   gobject_class->set_property = gst_dodge_set_property;
150   gobject_class->get_property = gst_dodge_get_property;
151
152   g_object_class_install_property (gobject_class, PROP_SILENT,
153       g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?",
154           FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155
156   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_dodge_set_caps);
157   trans_class->transform = GST_DEBUG_FUNCPTR (gst_dodge_transform);
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_dodge_init (GstDodge * filter, GstDodgeClass * gclass)
167 {
168   filter->silent = FALSE;
169 }
170
171 static void
172 gst_dodge_set_property (GObject * object, guint prop_id,
173     const GValue * value, GParamSpec * pspec)
174 {
175   GstDodge *filter = GST_DODGE (object);
176
177   switch (prop_id) {
178     case PROP_SILENT:
179       filter->silent = 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_dodge_get_property (GObject * object, guint prop_id,
189     GValue * value, GParamSpec * pspec)
190 {
191   GstDodge *filter = GST_DODGE (object);
192
193   GST_OBJECT_LOCK (filter);
194   switch (prop_id) {
195     case PROP_SILENT:
196       g_value_set_boolean (value, filter->silent);
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 /* GstElement vmethod implementations */
206
207 /* Handle the link with other elements. */
208 static gboolean
209 gst_dodge_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
210     GstCaps * outcaps)
211 {
212   GstDodge *filter = GST_DODGE (btrans);
213   GstStructure *structure;
214   gboolean ret = TRUE;
215
216   structure = gst_caps_get_structure (incaps, 0);
217
218   GST_OBJECT_LOCK (filter);
219   if (gst_structure_get_int (structure, "width", &filter->width) &&
220       gst_structure_get_int (structure, "height", &filter->height)) {
221     ret = TRUE;
222   }
223   GST_OBJECT_UNLOCK (filter);
224
225   return ret;
226 }
227
228 /* Actual processing. */
229 static GstFlowReturn
230 gst_dodge_transform (GstBaseTransform * btrans,
231     GstBuffer * in_buf, GstBuffer * out_buf)
232 {
233   GstDodge *filter = GST_DODGE (btrans);
234   guint32 *src = (guint32 *) GST_BUFFER_DATA (in_buf);
235   guint32 *dest = (guint32 *) GST_BUFFER_DATA (out_buf);
236   gint video_size;
237
238   video_size = filter->width * filter->height;
239
240   transform (src, dest, video_size);
241
242   return GST_FLOW_OK;
243 }
244
245 /* Entry point to initialize the plug-in.
246  * Register the element factories and other features. */
247 gboolean
248 gst_dodge_plugin_init (GstPlugin * dodge)
249 {
250   /* debug category for fltering log messages */
251   GST_DEBUG_CATEGORY_INIT (gst_dodge_debug, "dodge", 0, "Template dodge");
252
253   return gst_element_register (dodge, "dodge", GST_RANK_NONE, GST_TYPE_DODGE);
254 }
255
256 /*** Now the image processing work.... ***/
257
258 /* Keep the values inbounds. */
259 static gint
260 gate_int (gint value, gint min, gint max)
261 {
262   if (value < min) {
263     return min;
264   } else if (value > max) {
265     return max;
266   } else {
267     return value;
268   }
269 }
270
271 /* Transform processes each frame. */
272 static void
273 transform (guint32 * src, guint32 * dest, gint video_area)
274 {
275   guint32 in, red, green, blue;
276   gint x;
277
278   for (x = 0; x < video_area; x++) {
279     in = *src++;
280
281     red = (in >> 16) & 0xff;
282     green = (in >> 8) & 0xff;
283     blue = (in) & 0xff;
284
285     red = (256 * red) / (256 - red);
286     green = (256 * green) / (256 - green);
287     blue = (256 * blue) / (256 - blue);
288
289     red = gate_int (red, 0, 255);
290     green = gate_int (green, 0, 255);
291     blue = gate_int (blue, 0, 255);
292
293     *dest++ = (red << 16) | (green << 8) | blue;
294   }
295 }