Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.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 ! videoconvert ! 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
54 enum
55 {
56   OP_SPIRAL1 = 0,
57   OP_SPIRAL2,
58   OP_PARABOLA,
59   OP_HSTRIPE
60 };
61
62 #define GST_TYPE_OPTV_MODE (gst_optv_mode_get_type())
63 static GType
64 gst_optv_mode_get_type (void)
65 {
66   static GType type = 0;
67
68   static const GEnumValue enumvalue[] = {
69     {OP_SPIRAL1, "Maelstrom", "maelstrom"},
70     {OP_SPIRAL2, "Radiation", "radiation"},
71     {OP_PARABOLA, "Horizontal Stripes",
72         "horizontal-stripes"},
73     {OP_HSTRIPE, "Vertical Stripes", "vertical-stripes"},
74     {0, NULL, NULL},
75   };
76
77   if (!type) {
78     type = g_enum_register_static ("GstOpTVMode", enumvalue);
79   }
80   return type;
81 }
82
83 #define DEFAULT_MODE OP_SPIRAL1
84 #define DEFAULT_SPEED 16
85 #define DEFAULT_THRESHOLD 60
86
87 enum
88 {
89   PROP_0,
90   PROP_MODE,
91   PROP_SPEED,
92   PROP_THRESHOLD
93 };
94
95 static guint32 palette[256];
96
97 #define gst_optv_parent_class parent_class
98 G_DEFINE_TYPE (GstOpTV, gst_optv, GST_TYPE_VIDEO_FILTER);
99
100 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
101 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ BGRx, RGBx }")
102 #else
103 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ xBGR, 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_frame (GstVideoFilter * vfilter, GstVideoFrame * in_frame,
203     GstVideoFrame * out_frame)
204 {
205   GstOpTV *filter = GST_OPTV (vfilter);
206   guint32 *src, *dest;
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_frame->buffer);
214   stream_time =
215       gst_segment_to_stream_time (&GST_BASE_TRANSFORM (vfilter)->segment,
216       GST_FORMAT_TIME, timestamp);
217
218   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
219       GST_TIME_ARGS (timestamp));
220
221   if (GST_CLOCK_TIME_IS_VALID (stream_time))
222     gst_object_sync_values (GST_OBJECT (filter), stream_time);
223
224   if (G_UNLIKELY (filter->opmap[0] == NULL))
225     return GST_FLOW_NOT_NEGOTIATED;
226
227   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
228   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
229
230   width = GST_VIDEO_FRAME_WIDTH (in_frame);
231   height = GST_VIDEO_FRAME_HEIGHT (in_frame);
232
233   GST_OBJECT_LOCK (filter);
234   switch (filter->mode) {
235     default:
236     case 0:
237       p = filter->opmap[OP_SPIRAL1];
238       break;
239     case 1:
240       p = filter->opmap[OP_SPIRAL2];
241       break;
242     case 2:
243       p = filter->opmap[OP_PARABOLA];
244       break;
245     case 3:
246       p = filter->opmap[OP_HSTRIPE];
247       break;
248   }
249
250   filter->phase -= filter->speed;
251
252   diff = filter->diff;
253   image_y_over (src, diff, filter->threshold, width * height);
254   phase = filter->phase;
255
256   for (y = 0; y < height; y++) {
257     for (x = 0; x < width; x++) {
258       *dest++ = palette[(((guint8) (*p + phase)) ^ *diff++) & 255];
259       p++;
260     }
261   }
262   GST_OBJECT_UNLOCK (filter);
263
264   return GST_FLOW_OK;
265 }
266
267 static gboolean
268 gst_optv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
269     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
270 {
271   GstOpTV *filter = GST_OPTV (vfilter);
272   gint i, width, height;
273
274   width = GST_VIDEO_INFO_WIDTH (in_info);
275   height = GST_VIDEO_INFO_HEIGHT (in_info);
276
277   for (i = 0; i < 4; i++) {
278     if (filter->opmap[i])
279       g_free (filter->opmap[i]);
280     filter->opmap[i] = g_new (gint8, width * height);
281   }
282   setOpmap (filter->opmap, width, height);
283
284   if (filter->diff)
285     g_free (filter->diff);
286   filter->diff = g_new (guint8, width * height);
287
288   return TRUE;
289 }
290
291 static gboolean
292 gst_optv_start (GstBaseTransform * trans)
293 {
294   GstOpTV *filter = GST_OPTV (trans);
295
296   filter->phase = 0;
297
298   return TRUE;
299 }
300
301 static void
302 gst_optv_finalize (GObject * object)
303 {
304   GstOpTV *filter = GST_OPTV (object);
305
306   if (filter->opmap[0]) {
307     gint i;
308
309     for (i = 0; i < 4; i++) {
310       if (filter->opmap[i])
311         g_free (filter->opmap[i]);
312       filter->opmap[i] = NULL;
313     }
314   }
315
316   if (filter->diff)
317     g_free (filter->diff);
318   filter->diff = NULL;
319
320   G_OBJECT_CLASS (parent_class)->finalize (object);
321 }
322
323 static void
324 gst_optv_set_property (GObject * object, guint prop_id, const GValue * value,
325     GParamSpec * pspec)
326 {
327   GstOpTV *filter = GST_OPTV (object);
328
329   GST_OBJECT_LOCK (filter);
330   switch (prop_id) {
331     case PROP_MODE:
332       filter->mode = g_value_get_enum (value);
333       break;
334     case PROP_SPEED:
335       filter->speed = g_value_get_int (value);
336       break;
337     case PROP_THRESHOLD:
338       filter->threshold = g_value_get_uint (value);
339       break;
340     default:
341       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342       break;
343   }
344   GST_OBJECT_UNLOCK (filter);
345 }
346
347 static void
348 gst_optv_get_property (GObject * object, guint prop_id, GValue * value,
349     GParamSpec * pspec)
350 {
351   GstOpTV *filter = GST_OPTV (object);
352
353   switch (prop_id) {
354     case PROP_MODE:
355       g_value_set_enum (value, filter->mode);
356       break;
357     case PROP_SPEED:
358       g_value_set_int (value, filter->speed);
359       break;
360     case PROP_THRESHOLD:
361       g_value_set_uint (value, filter->threshold);
362       break;
363     default:
364       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
365       break;
366   }
367 }
368
369 static void
370 gst_optv_class_init (GstOpTVClass * klass)
371 {
372   GObjectClass *gobject_class = (GObjectClass *) klass;
373   GstElementClass *gstelement_class = (GstElementClass *) klass;
374   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
375   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
376
377   gobject_class->set_property = gst_optv_set_property;
378   gobject_class->get_property = gst_optv_get_property;
379
380   gobject_class->finalize = gst_optv_finalize;
381
382   g_object_class_install_property (gobject_class, PROP_MODE,
383       g_param_spec_enum ("mode", "Mode",
384           "Mode", GST_TYPE_OPTV_MODE, DEFAULT_MODE,
385           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386
387   g_object_class_install_property (gobject_class, PROP_SPEED,
388       g_param_spec_int ("speed", "Speed",
389           "Effect speed", G_MININT, G_MAXINT, DEFAULT_SPEED,
390           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
391
392   g_object_class_install_property (gobject_class, PROP_THRESHOLD,
393       g_param_spec_uint ("threshold", "Threshold",
394           "Luma threshold", 0, G_MAXINT, DEFAULT_THRESHOLD,
395           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
396
397   gst_element_class_set_details_simple (gstelement_class, "OpTV effect",
398       "Filter/Effect/Video",
399       "Optical art meets real-time video effect",
400       "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
401       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
402
403   gst_element_class_add_pad_template (gstelement_class,
404       gst_static_pad_template_get (&gst_optv_sink_template));
405   gst_element_class_add_pad_template (gstelement_class,
406       gst_static_pad_template_get (&gst_optv_src_template));
407
408   trans_class->start = GST_DEBUG_FUNCPTR (gst_optv_start);
409
410   vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_optv_set_info);
411   vfilter_class->transform_frame = GST_DEBUG_FUNCPTR (gst_optv_transform_frame);
412
413   initPalette ();
414 }
415
416 static void
417 gst_optv_init (GstOpTV * filter)
418 {
419   filter->speed = DEFAULT_SPEED;
420   filter->mode = DEFAULT_MODE;
421   filter->threshold = DEFAULT_THRESHOLD;
422 }