tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.git] / gst / effectv / gstop.c
1 /* GStreamer
2  * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
3  *
4  * EffecTV - Realtime Digital Video Effector
5  * Copyright (C) 2001-2006 FUKUCHI Kentaro
6  *
7  * OpTV - Optical art meets real-time video effect.
8  * Copyright (C) 2004-2005 FUKUCHI Kentaro
9  *
10  * EffecTV is free software. This library is free software;
11  * you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  */
26
27 /**
28  * SECTION:element-optv
29  *
30  * Traditional black-white optical animation is now resurrected as a
31  * real-time video effect. Input images are binarized and combined with
32  * various optical pattern.
33  *
34  * <refsect2>
35  * <title>Example launch line</title>
36  * |[
37  * gst-launch -v videotestsrc ! optv ! ffmpegcolorspace ! autovideosink
38  * ]| This pipeline shows the effect of optv on a test stream.
39  * </refsect2>
40  */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include <math.h>
47 #include <string.h>
48
49 #include "gstop.h"
50 #include "gsteffectv.h"
51
52 #include <gst/video/video.h>
53 #include <gst/controller/gstcontroller.h>
54
55 enum
56 {
57   OP_SPIRAL1 = 0,
58   OP_SPIRAL2,
59   OP_PARABOLA,
60   OP_HSTRIPE
61 };
62
63 #define GST_TYPE_OPTV_MODE (gst_optv_mode_get_type())
64 static GType
65 gst_optv_mode_get_type (void)
66 {
67   static GType type = 0;
68
69   static const GEnumValue enumvalue[] = {
70     {OP_SPIRAL1, "Maelstrom", "maelstrom"},
71     {OP_SPIRAL2, "Radiation", "radiation"},
72     {OP_PARABOLA, "Horizontal Stripes",
73         "horizontal-stripes"},
74     {OP_HSTRIPE, "Vertical Stripes", "vertical-stripes"},
75     {0, NULL, NULL},
76   };
77
78   if (!type) {
79     type = g_enum_register_static ("GstOpTVMode", enumvalue);
80   }
81   return type;
82 }
83
84 #define DEFAULT_MODE OP_SPIRAL1
85 #define DEFAULT_SPEED 16
86 #define DEFAULT_THRESHOLD 60
87
88 enum
89 {
90   PROP_0,
91   PROP_MODE,
92   PROP_SPEED,
93   PROP_THRESHOLD
94 };
95
96 static guint32 palette[256];
97
98 GST_BOILERPLATE (GstOpTV, gst_optv, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
99
100 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
101 #define CAPS_STR GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_RGBx
102 #else
103 #define CAPS_STR GST_VIDEO_CAPS_xBGR ";" GST_VIDEO_CAPS_xRGB
104 #endif
105
106 static GstStaticPadTemplate gst_optv_src_template =
107 GST_STATIC_PAD_TEMPLATE ("src",
108     GST_PAD_SRC,
109     GST_PAD_ALWAYS,
110     GST_STATIC_CAPS (CAPS_STR)
111     );
112
113 static GstStaticPadTemplate gst_optv_sink_template =
114 GST_STATIC_PAD_TEMPLATE ("sink",
115     GST_PAD_SINK,
116     GST_PAD_ALWAYS,
117     GST_STATIC_CAPS (CAPS_STR)
118     );
119
120 static void
121 initPalette (void)
122 {
123   gint i;
124   guint8 v;
125
126   for (i = 0; i < 112; i++) {
127     palette[i] = 0;
128     palette[i + 128] = 0xffffff;
129   }
130   for (i = 0; i < 16; i++) {
131     v = 16 * (i + 1) - 1;
132     palette[i + 112] = (v << 16) | (v << 8) | v;
133     v = 255 - v;
134     palette[i + 240] = (v << 16) | (v << 8) | v;
135   }
136 }
137
138 static void
139 setOpmap (gint8 * opmap[4], gint width, gint height)
140 {
141   gint i, j, x, y;
142 #ifndef PS2
143   gdouble xx, yy, r, at, rr;
144 #else
145   gfloat xx, yy, r, at, rr;
146 #endif
147   gint sci;
148
149   sci = 640 / width;
150   i = 0;
151   for (y = 0; y < height; y++) {
152     yy = (gdouble) (y - height / 2) / width;
153     for (x = 0; x < width; x++) {
154       xx = (gdouble) x / width - 0.5;
155 #ifndef PS2
156       r = sqrt (xx * xx + yy * yy);
157       at = atan2 (xx, yy);
158 #else
159       r = sqrtf (xx * xx + yy * yy);
160       at = atan2f (xx, yy);
161 #endif
162
163       opmap[OP_SPIRAL1][i] = ((guint)
164           ((at / G_PI * 256) + (r * 4000))) & 255;
165
166       j = r * 300 / 32;
167       rr = r * 300 - j * 32;
168       j *= 64;
169       j += (rr > 28) ? (rr - 28) * 16 : 0;
170       opmap[OP_SPIRAL2][i] = ((guint)
171           ((at / G_PI * 4096) + (r * 1600) - j)) & 255;
172
173       opmap[OP_PARABOLA][i] =
174           ((guint) (yy / (xx * xx * 0.3 + 0.1) * 400)) & 255;
175       opmap[OP_HSTRIPE][i] = x * 8 * sci;
176       i++;
177     }
178   }
179 }
180
181 /* Taken from effectv/image.c */
182 /* Y value filters */
183 static void
184 image_y_over (guint32 * src, guint8 * diff, gint y_threshold, gint video_area)
185 {
186   gint i;
187   gint R, G, B, v;
188   guint8 *p = diff;
189
190   for (i = video_area; i > 0; i--) {
191     R = ((*src) & 0xff0000) >> (16 - 1);
192     G = ((*src) & 0xff00) >> (8 - 2);
193     B = (*src) & 0xff;
194     v = y_threshold * 7 - (R + G + B);
195     *p = (guint8) (v >> 24);
196     src++;
197     p++;
198   }
199 }
200
201 static GstFlowReturn
202 gst_optv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
203 {
204   GstOpTV *filter = GST_OPTV (trans);
205   guint32 *src, *dest;
206   GstFlowReturn ret = GST_FLOW_OK;
207   gint8 *p;
208   guint8 *diff;
209   gint x, y, width, height;
210   GstClockTime timestamp, stream_time;
211   guint8 phase;
212
213   timestamp = GST_BUFFER_TIMESTAMP (in);
214   stream_time =
215       gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
216
217   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
218       GST_TIME_ARGS (timestamp));
219
220   if (GST_CLOCK_TIME_IS_VALID (stream_time))
221     gst_object_sync_values (G_OBJECT (filter), stream_time);
222
223   src = (guint32 *) GST_BUFFER_DATA (in);
224   dest = (guint32 *) GST_BUFFER_DATA (out);
225
226   if (G_UNLIKELY (filter->opmap[0] == NULL))
227     return GST_FLOW_NOT_NEGOTIATED;
228
229   GST_OBJECT_LOCK (filter);
230   switch (filter->mode) {
231     default:
232     case 0:
233       p = filter->opmap[OP_SPIRAL1];
234       break;
235     case 1:
236       p = filter->opmap[OP_SPIRAL2];
237       break;
238     case 2:
239       p = filter->opmap[OP_PARABOLA];
240       break;
241     case 3:
242       p = filter->opmap[OP_HSTRIPE];
243       break;
244   }
245
246   filter->phase -= filter->speed;
247
248   diff = filter->diff;
249   image_y_over (src, diff, filter->threshold, filter->width * filter->height);
250   height = filter->height;
251   width = filter->width;
252   phase = filter->phase;
253
254   for (y = 0; y < height; y++) {
255     for (x = 0; x < width; x++) {
256       *dest++ = palette[(((guint8) (*p + phase)) ^ *diff++) & 255];
257       p++;
258     }
259   }
260   GST_OBJECT_UNLOCK (filter);
261
262   return ret;
263 }
264
265 static gboolean
266 gst_optv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
267     GstCaps * outcaps)
268 {
269   GstOpTV *filter = GST_OPTV (btrans);
270   GstStructure *structure;
271   gboolean ret = FALSE;
272
273   structure = gst_caps_get_structure (incaps, 0);
274
275   GST_OBJECT_LOCK (filter);
276   if (gst_structure_get_int (structure, "width", &filter->width) &&
277       gst_structure_get_int (structure, "height", &filter->height)) {
278     gint i;
279
280     for (i = 0; i < 4; i++) {
281       if (filter->opmap[i])
282         g_free (filter->opmap[i]);
283       filter->opmap[i] = g_new (gint8, filter->width * filter->height);
284     }
285     setOpmap (filter->opmap, filter->width, filter->height);
286
287     if (filter->diff)
288       g_free (filter->diff);
289     filter->diff = g_new (guint8, filter->width * filter->height);
290
291     ret = TRUE;
292   }
293   GST_OBJECT_UNLOCK (filter);
294
295   return ret;
296 }
297
298 static gboolean
299 gst_optv_start (GstBaseTransform * trans)
300 {
301   GstOpTV *filter = GST_OPTV (trans);
302
303   filter->phase = 0;
304
305   return TRUE;
306 }
307
308 static void
309 gst_optv_finalize (GObject * object)
310 {
311   GstOpTV *filter = GST_OPTV (object);
312
313   if (filter->opmap[0]) {
314     gint i;
315
316     for (i = 0; i < 4; i++) {
317       if (filter->opmap[i])
318         g_free (filter->opmap[i]);
319       filter->opmap[i] = NULL;
320     }
321   }
322
323   if (filter->diff)
324     g_free (filter->diff);
325   filter->diff = NULL;
326
327   G_OBJECT_CLASS (parent_class)->finalize (object);
328 }
329
330 static void
331 gst_optv_set_property (GObject * object, guint prop_id, const GValue * value,
332     GParamSpec * pspec)
333 {
334   GstOpTV *filter = GST_OPTV (object);
335
336   GST_OBJECT_LOCK (filter);
337   switch (prop_id) {
338     case PROP_MODE:
339       filter->mode = g_value_get_enum (value);
340       break;
341     case PROP_SPEED:
342       filter->speed = g_value_get_int (value);
343       break;
344     case PROP_THRESHOLD:
345       filter->threshold = g_value_get_uint (value);
346       break;
347     default:
348       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
349       break;
350   }
351   GST_OBJECT_UNLOCK (filter);
352 }
353
354 static void
355 gst_optv_get_property (GObject * object, guint prop_id, GValue * value,
356     GParamSpec * pspec)
357 {
358   GstOpTV *filter = GST_OPTV (object);
359
360   switch (prop_id) {
361     case PROP_MODE:
362       g_value_set_enum (value, filter->mode);
363       break;
364     case PROP_SPEED:
365       g_value_set_int (value, filter->speed);
366       break;
367     case PROP_THRESHOLD:
368       g_value_set_uint (value, filter->threshold);
369       break;
370     default:
371       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
372       break;
373   }
374 }
375
376 static void
377 gst_optv_base_init (gpointer g_class)
378 {
379   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
380
381   gst_element_class_set_details_simple (element_class, "OpTV effect",
382       "Filter/Effect/Video",
383       "Optical art meets real-time video effect",
384       "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
385       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
386
387   gst_element_class_add_static_pad_template (element_class,
388       &gst_optv_sink_template);
389   gst_element_class_add_static_pad_template (element_class,
390       &gst_optv_src_template);
391 }
392
393 static void
394 gst_optv_class_init (GstOpTVClass * klass)
395 {
396   GObjectClass *gobject_class = (GObjectClass *) klass;
397   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
398
399   gobject_class->set_property = gst_optv_set_property;
400   gobject_class->get_property = gst_optv_get_property;
401
402   gobject_class->finalize = gst_optv_finalize;
403
404   g_object_class_install_property (gobject_class, PROP_MODE,
405       g_param_spec_enum ("mode", "Mode",
406           "Mode", GST_TYPE_OPTV_MODE, DEFAULT_MODE,
407           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408
409   g_object_class_install_property (gobject_class, PROP_SPEED,
410       g_param_spec_int ("speed", "Speed",
411           "Effect speed", G_MININT, G_MAXINT, DEFAULT_SPEED,
412           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
413
414   g_object_class_install_property (gobject_class, PROP_THRESHOLD,
415       g_param_spec_uint ("threshold", "Threshold",
416           "Luma threshold", 0, G_MAXINT, DEFAULT_THRESHOLD,
417           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
418
419   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_optv_set_caps);
420   trans_class->transform = GST_DEBUG_FUNCPTR (gst_optv_transform);
421   trans_class->start = GST_DEBUG_FUNCPTR (gst_optv_start);
422
423   initPalette ();
424 }
425
426 static void
427 gst_optv_init (GstOpTV * filter, GstOpTVClass * klass)
428 {
429   filter->speed = DEFAULT_SPEED;
430   filter->mode = DEFAULT_MODE;
431   filter->threshold = DEFAULT_THRESHOLD;
432 }