documentation: fixed a heap o' typos
[platform/upstream/gstreamer.git] / gst / gaudieffects / gstchromium.c
1 /*
2  * GStreamer
3  * Copyright (C) <2010> Luis de Bethencourt <luis@debethencourt.com>
4  *
5  * Chromium - burning chrome 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-chromium
49  * @title: chromium
50  *
51  * Chromium breaks the colors of a video stream in realtime.
52  *
53  * ## Example launch line
54  * |[
55  * gst-launch-1.0 -v videotestsrc ! chromium ! videoconvert ! autovideosink
56  * ]| This pipeline shows the effect of chromium on a test stream
57  *
58  */
59
60 #ifdef HAVE_CONFIG_H
61 #  include <config.h>
62 #endif
63
64 #include <math.h>
65 #include <gst/gst.h>
66
67 #include "gstplugin.h"
68 #include "gstchromium.h"
69
70 #define gst_chromium_parent_class parent_class
71 G_DEFINE_TYPE (GstChromium, gst_chromium, GST_TYPE_VIDEO_FILTER);
72
73 GST_DEBUG_CATEGORY_STATIC (gst_chromium_debug);
74 #define GST_CAT_DEFAULT gst_chromium_debug
75
76 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
77 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  BGRx, RGBx }")
78 #else
79 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{  xBGR, xRGB }")
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_EDGE_A,
92   PROP_EDGE_B,
93 };
94
95 /* Initializations */
96
97 #define DEFAULT_EDGE_A 200
98 #define DEFAULT_EDGE_B 1
99
100 const float pi = 3.141582f;
101
102 gint cosTablePi = 512;
103 gint cosTableTwoPi = (2 * 512);
104 gint cosTableOne = 512;
105 gint cosTableMask = 1023;
106
107 gint cosTable[2 * 512];
108
109 void setup_cos_table (void);
110 static gint cos_from_table (int angle);
111 static inline int abs_int (int val);
112 static void transform (guint32 * src, guint32 * dest, gint video_area,
113     gint edge_a, gint edge_b);
114
115 /* The capabilities of the inputs and outputs. */
116
117 static GstStaticPadTemplate gst_chromium_sink_template =
118 GST_STATIC_PAD_TEMPLATE ("sink",
119     GST_PAD_SINK,
120     GST_PAD_ALWAYS,
121     GST_STATIC_CAPS (CAPS_STR)
122     );
123
124 static GstStaticPadTemplate gst_chromium_src_template =
125 GST_STATIC_PAD_TEMPLATE ("src",
126     GST_PAD_SRC,
127     GST_PAD_ALWAYS,
128     GST_STATIC_CAPS (CAPS_STR)
129     );
130
131 static void gst_chromium_set_property (GObject * object, guint prop_id,
132     const GValue * value, GParamSpec * pspec);
133 static void gst_chromium_get_property (GObject * object, guint prop_id,
134     GValue * value, GParamSpec * pspec);
135 static void gst_chromium_finalize (GObject * object);
136
137 static GstFlowReturn gst_chromium_transform_frame (GstVideoFilter * vfilter,
138     GstVideoFrame * in_frame, GstVideoFrame * out_frame);
139
140 /* GObject vmethod implementations */
141
142 /* Initialize the chromium's class. */
143 static void
144 gst_chromium_class_init (GstChromiumClass * klass)
145 {
146   GObjectClass *gobject_class = (GObjectClass *) klass;
147   GstElementClass *gstelement_class = (GstElementClass *) klass;
148   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
149
150   gst_element_class_set_static_metadata (gstelement_class, "Chromium",
151       "Filter/Effect/Video",
152       "Chromium breaks the colors of the video signal.",
153       "Luis de Bethencourt <luis@debethencourt.com>");
154
155   gst_element_class_add_static_pad_template (gstelement_class,
156       &gst_chromium_sink_template);
157   gst_element_class_add_static_pad_template (gstelement_class,
158       &gst_chromium_src_template);
159
160   gobject_class->set_property = gst_chromium_set_property;
161   gobject_class->get_property = gst_chromium_get_property;
162   gobject_class->finalize = gst_chromium_finalize;
163
164   g_object_class_install_property (gobject_class, PROP_EDGE_A,
165       g_param_spec_uint ("edge-a", "Edge A",
166           "First edge parameter", 0, 256, DEFAULT_EDGE_A,
167           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
168
169   g_object_class_install_property (gobject_class, PROP_EDGE_B,
170       g_param_spec_uint ("edge-b", "Edge B",
171           "Second edge parameter", 0, 256, DEFAULT_EDGE_B,
172           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
173
174   vfilter_class->transform_frame =
175       GST_DEBUG_FUNCPTR (gst_chromium_transform_frame);
176 }
177
178 /* Initialize the element,
179  * instantiate pads and add them to element,
180  * set pad callback functions, and
181  * initialize instance structure.
182  */
183 static void
184 gst_chromium_init (GstChromium * filter)
185 {
186   filter->edge_a = DEFAULT_EDGE_A;
187   filter->edge_b = DEFAULT_EDGE_B;
188
189   setup_cos_table ();
190 }
191
192 static void
193 gst_chromium_set_property (GObject * object, guint prop_id,
194     const GValue * value, GParamSpec * pspec)
195 {
196   GstChromium *filter = GST_CHROMIUM (object);
197
198   switch (prop_id) {
199     case PROP_EDGE_A:
200       filter->edge_a = g_value_get_uint (value);
201       break;
202     case PROP_EDGE_B:
203       filter->edge_b = g_value_get_uint (value);
204       break;
205     default:
206       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207       break;
208   }
209 }
210
211 static void
212 gst_chromium_get_property (GObject * object, guint prop_id,
213     GValue * value, GParamSpec * pspec)
214 {
215   GstChromium *filter = GST_CHROMIUM (object);
216
217   GST_OBJECT_LOCK (filter);
218   switch (prop_id) {
219     case PROP_EDGE_A:
220       g_value_set_uint (value, filter->edge_a);
221       break;
222     case PROP_EDGE_B:
223       g_value_set_uint (value, filter->edge_b);
224       break;
225     default:
226       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227       break;
228   }
229   GST_OBJECT_UNLOCK (filter);
230 }
231
232 static void
233 gst_chromium_finalize (GObject * object)
234 {
235   G_OBJECT_CLASS (parent_class)->finalize (object);
236 }
237
238 /* GstElement vmethod implementations */
239
240 /* Actual processing. */
241 static GstFlowReturn
242 gst_chromium_transform_frame (GstVideoFilter * vfilter,
243     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
244 {
245   GstChromium *filter = GST_CHROMIUM (vfilter);
246   gint video_size, edge_a, edge_b;
247   guint32 *src, *dest;
248   GstClockTime timestamp;
249   gint64 stream_time;
250
251   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
252   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
253
254   /* GstController: update the properties */
255   timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
256   stream_time =
257       gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
258       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 (GST_OBJECT (filter), stream_time);
265
266   GST_OBJECT_LOCK (filter);
267   edge_a = filter->edge_a;
268   edge_b = filter->edge_b;
269   GST_OBJECT_UNLOCK (filter);
270
271   video_size = GST_VIDEO_FRAME_WIDTH (in_frame) *
272       GST_VIDEO_FRAME_HEIGHT (in_frame);
273   transform (src, dest, video_size, edge_a, edge_b);
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_chromium_plugin_init (GstPlugin * chromium)
282 {
283   /* debug category for fltering log messages */
284   GST_DEBUG_CATEGORY_INIT (gst_chromium_debug, "chromium", 0,
285       "Template chromium");
286
287   return gst_element_register (chromium, "chromium", GST_RANK_NONE,
288       GST_TYPE_CHROMIUM);
289 }
290
291 /*** Now the image processing work.... ***/
292 /* Set up the cosine table. */
293 void
294 setup_cos_table (void)
295 {
296   int angle;
297
298   for (angle = 0; angle < cosTableTwoPi; ++angle) {
299     float angleInRadians = ((float) (angle) / cosTablePi) * pi;
300     cosTable[angle] = (int) (cos (angleInRadians) * cosTableOne);
301   }
302 }
303
304 /* Keep the values absolute. */
305 static inline int
306 abs_int (int val)
307 {
308   if (val > 0) {
309     return val;
310   } else {
311     return -val;
312   }
313 }
314
315 /* Cosine from Table. */
316 static gint
317 cos_from_table (int angle)
318 {
319   angle &= cosTableMask;
320   return cosTable[angle];
321 }
322
323 /* Transform processes each frame. */
324 static void
325 transform (guint32 * src, guint32 * dest, gint video_area,
326     gint edge_a, gint edge_b)
327 {
328   guint32 in;
329   gint x, red, green, blue;
330
331   for (x = 0; x < video_area; x++) {
332     in = *src++;
333
334     red = (in >> 16) & 0xff;
335     green = (in >> 8) & 0xff;
336     blue = (in) & 0xff;
337
338     red = abs_int (cos_from_table ((red + edge_a) + ((red * edge_b) / 2)));
339     green = abs_int (cos_from_table (
340             (green + edge_a) + ((green * edge_b) / 2)));
341     blue = abs_int (cos_from_table ((blue + edge_a) + ((blue * edge_b) / 2)));
342
343     red = CLAMP (red, 0, 255);
344     green = CLAMP (green, 0, 255);
345     blue = CLAMP (blue, 0, 255);
346
347     *dest++ = (red << 16) | (green << 8) | blue;
348   }
349 }