Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / gaudieffects / gstburn.c
1 /*
2  * GStreamer
3  * Copyright (C) 2010 Luis de Bethencourt <luis@debethencourt.com>
4  * 
5  * Burn - curve adjustment 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-burn
49  *
50  * Burn adjusts the colors of a video stream in realtime.
51  *
52  * <refsect2>
53  * <title>Example launch line</title>
54  * |[
55  * gst-launch -v videotestsrc ! burn ! ffmpegcolorspace ! autovideosink
56  * ]| This pipeline shows the effect of burn 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 "gstburn.h"
69
70 #include <gst/video/video.h>
71 #include <gst/controller/gstcontroller.h>
72
73 GST_DEBUG_CATEGORY_STATIC (gst_burn_debug);
74 #define GST_CAT_DEFAULT gst_burn_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_ADJUSTMENT,
92   PROP_SILENT
93 };
94
95 /* Initializations */
96
97 #define DEFAULT_ADJUSTMENT 175
98
99 static void transform (guint32 * src, guint32 * dest, gint video_area,
100     gint adjustment);
101
102 /* The capabilities of the inputs and outputs. */
103
104 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
105     GST_PAD_SINK,
106     GST_PAD_ALWAYS,
107     GST_STATIC_CAPS (CAPS_STR)
108     );
109
110 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
111     GST_PAD_SRC,
112     GST_PAD_ALWAYS,
113     GST_STATIC_CAPS (CAPS_STR)
114     );
115
116 GST_BOILERPLATE (GstBurn, gst_burn, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
117
118 static void gst_burn_set_property (GObject * object, guint prop_id,
119     const GValue * value, GParamSpec * pspec);
120 static void gst_burn_get_property (GObject * object, guint prop_id,
121     GValue * value, GParamSpec * pspec);
122
123 static gboolean gst_burn_set_caps (GstBaseTransform * btrans,
124     GstCaps * incaps, GstCaps * outcaps);
125 static GstFlowReturn gst_burn_transform (GstBaseTransform * btrans,
126     GstBuffer * in_buf, GstBuffer * out_buf);
127
128 /* GObject vmethod implementations */
129
130 static void
131 gst_burn_base_init (gpointer gclass)
132 {
133   GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);
134
135   gst_element_class_set_details_simple (element_class,
136       "Burn",
137       "Filter/Effect/Video",
138       "Burn adjusts the colors in the video signal.",
139       "Luis de Bethencourt <luis@debethencourt.com>");
140
141   gst_element_class_add_static_pad_template (element_class, &src_factory);
142   gst_element_class_add_static_pad_template (element_class, &sink_factory);
143 }
144
145 /* Initialize the burn's class. */
146 static void
147 gst_burn_class_init (GstBurnClass * klass)
148 {
149   GObjectClass *gobject_class = (GObjectClass *) klass;
150   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
151
152   gobject_class->set_property = gst_burn_set_property;
153   gobject_class->get_property = gst_burn_get_property;
154
155   g_object_class_install_property (gobject_class, PROP_ADJUSTMENT,
156       g_param_spec_uint ("adjustment", "Adjustment",
157           "Adjustment parameter", 0, 256, DEFAULT_ADJUSTMENT,
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_burn_set_caps);
165   trans_class->transform = GST_DEBUG_FUNCPTR (gst_burn_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_burn_init (GstBurn * filter, GstBurnClass * gclass)
175 {
176   filter->adjustment = DEFAULT_ADJUSTMENT;
177   filter->silent = FALSE;
178 }
179
180 static void
181 gst_burn_set_property (GObject * object, guint prop_id,
182     const GValue * value, GParamSpec * pspec)
183 {
184   GstBurn *filter = GST_BURN (object);
185
186   switch (prop_id) {
187     case PROP_SILENT:
188       filter->silent = g_value_get_boolean (value);
189       break;
190     case PROP_ADJUSTMENT:
191       filter->adjustment = g_value_get_uint (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_burn_get_property (GObject * object, guint prop_id,
201     GValue * value, GParamSpec * pspec)
202 {
203   GstBurn *filter = GST_BURN (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_ADJUSTMENT:
211       g_value_set_uint (value, filter->adjustment);
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_burn_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
225     GstCaps * outcaps)
226 {
227   GstBurn *filter = GST_BURN (btrans);
228   GstStructure *structure;
229   gboolean ret = FALSE;
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_burn_transform (GstBaseTransform * btrans,
246     GstBuffer * in_buf, GstBuffer * out_buf)
247 {
248   GstBurn *filter = GST_BURN (btrans);
249   gint video_size, adjustment;
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   video_size = filter->width * filter->height;
256
257   /* GstController: update the properties */
258   timestamp = GST_BUFFER_TIMESTAMP (in_buf);
259   stream_time =
260       gst_segment_to_stream_time (&btrans->segment, GST_FORMAT_TIME, timestamp);
261
262   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
263       GST_TIME_ARGS (timestamp));
264
265   if (GST_CLOCK_TIME_IS_VALID (stream_time))
266     gst_object_sync_values (G_OBJECT (filter), stream_time);
267
268   GST_OBJECT_LOCK (filter);
269   adjustment = filter->adjustment;
270   GST_OBJECT_UNLOCK (filter);
271
272   transform (src, dest, video_size, adjustment);
273
274   return GST_FLOW_OK;
275 }
276
277 /* Entry point to initialize the plug-in.
278  * Register the element factories and other features. */
279 gboolean
280 gst_burn_plugin_init (GstPlugin * burn)
281 {
282   /* debug category for fltering log messages */
283   GST_DEBUG_CATEGORY_INIT (gst_burn_debug, "burn", 0, "Template burn");
284
285   return gst_element_register (burn, "burn", GST_RANK_NONE, GST_TYPE_BURN);
286 }
287
288 /*** Now the image processing work.... ***/
289
290 /* Transform processes each frame. */
291 static void
292 transform (guint32 * src, guint32 * dest, gint video_area, gint adjustment)
293 {
294   guint32 in;
295   gint red, green, blue, c;
296   gint x;
297
298   for (x = 0; x < video_area; x++) {
299     in = *src++;
300
301     red = (in >> 16) & 0xff;
302     green = (in >> 8) & 0xff;
303     blue = (in) & 0xff;
304
305     c = (red + adjustment);
306     red = c ? (256 - (256 * (255 - red) / c)) : 0;
307     c = (green + adjustment);
308     green = c ? (256 - (256 * (255 - green) / c)) : 0;
309     c = (blue + adjustment);
310     blue = c ? (256 - (256 * (255 - blue) / c)) : 0;
311
312     red = CLAMP (red, 0, 255);
313     green = CLAMP (green, 0, 255);
314     blue = CLAMP (blue, 0, 255);
315
316     *dest++ = (red << 16) | (green << 8) | blue;
317   }
318 }