effectv: Fix compilation with gcc 3
[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 ! 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 / M_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 / M_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;
210   GstClockTime timestamp, stream_time;
211
212   timestamp = GST_BUFFER_TIMESTAMP (in);
213   stream_time =
214       gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
215
216   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
217       GST_TIME_ARGS (timestamp));
218
219   if (GST_CLOCK_TIME_IS_VALID (stream_time))
220     gst_object_sync_values (G_OBJECT (filter), stream_time);
221
222   src = (guint32 *) GST_BUFFER_DATA (in);
223   dest = (guint32 *) GST_BUFFER_DATA (out);
224
225   if (G_UNLIKELY (filter->opmap[0] == NULL))
226     return GST_FLOW_NOT_NEGOTIATED;
227
228   switch (filter->mode) {
229     default:
230     case 0:
231       p = filter->opmap[OP_SPIRAL1];
232       break;
233     case 1:
234       p = filter->opmap[OP_SPIRAL2];
235       break;
236     case 2:
237       p = filter->opmap[OP_PARABOLA];
238       break;
239     case 3:
240       p = filter->opmap[OP_HSTRIPE];
241       break;
242   }
243
244   filter->phase -= filter->speed;
245
246   diff = filter->diff;
247   image_y_over (src, diff, filter->threshold, filter->width * filter->height);
248
249   for (y = 0; y < filter->height; y++) {
250     for (x = 0; x < filter->width; x++) {
251       *dest++ = palette[(((guint8) (*p + filter->phase)) ^ *diff++) & 255];
252       p++;
253     }
254   }
255
256   return ret;
257 }
258
259 static gboolean
260 gst_optv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
261     GstCaps * outcaps)
262 {
263   GstOpTV *filter = GST_OPTV (btrans);
264   GstStructure *structure;
265   gboolean ret = FALSE;
266
267   structure = gst_caps_get_structure (incaps, 0);
268
269   if (gst_structure_get_int (structure, "width", &filter->width) &&
270       gst_structure_get_int (structure, "height", &filter->height)) {
271     gint i;
272
273     for (i = 0; i < 4; i++) {
274       if (filter->opmap[i])
275         g_free (filter->opmap[i]);
276       filter->opmap[i] = g_new (gint8, filter->width * filter->height);
277     }
278     setOpmap (filter->opmap, filter->width, filter->height);
279
280     if (filter->diff)
281       g_free (filter->diff);
282     filter->diff = g_new (guint8, filter->width * filter->height);
283
284     ret = TRUE;
285   }
286
287   return ret;
288 }
289
290 static gboolean
291 gst_optv_start (GstBaseTransform * trans)
292 {
293   GstOpTV *filter = GST_OPTV (trans);
294
295   filter->phase = 0;
296
297   return TRUE;
298 }
299
300 static void
301 gst_optv_finalize (GObject * object)
302 {
303   GstOpTV *filter = GST_OPTV (object);
304
305   if (filter->opmap[0]) {
306     gint i;
307
308     for (i = 0; i < 4; i++) {
309       if (filter->opmap[i])
310         g_free (filter->opmap[i]);
311       filter->opmap[i] = NULL;
312     }
313   }
314
315   if (filter->diff)
316     g_free (filter->diff);
317   filter->diff = NULL;
318
319   G_OBJECT_CLASS (parent_class)->finalize (object);
320 }
321
322 static void
323 gst_optv_set_property (GObject * object, guint prop_id, const GValue * value,
324     GParamSpec * pspec)
325 {
326   GstOpTV *filter = GST_OPTV (object);
327
328   switch (prop_id) {
329     case PROP_MODE:
330       filter->mode = g_value_get_enum (value);
331       break;
332     case PROP_SPEED:
333       filter->speed = g_value_get_int (value);
334       break;
335     case PROP_THRESHOLD:
336       filter->threshold = g_value_get_uint (value);
337       break;
338     default:
339       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
340       break;
341   }
342 }
343
344 static void
345 gst_optv_get_property (GObject * object, guint prop_id, GValue * value,
346     GParamSpec * pspec)
347 {
348   GstOpTV *filter = GST_OPTV (object);
349
350   switch (prop_id) {
351     case PROP_MODE:
352       g_value_set_enum (value, filter->mode);
353       break;
354     case PROP_SPEED:
355       g_value_set_int (value, filter->speed);
356       break;
357     case PROP_THRESHOLD:
358       g_value_set_uint (value, filter->threshold);
359       break;
360     default:
361       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
362       break;
363   }
364 }
365
366 static void
367 gst_optv_base_init (gpointer g_class)
368 {
369   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
370
371   gst_element_class_set_details_simple (element_class, "OpTV effect",
372       "Filter/Effect/Video",
373       "Optical art meets real-time video effect",
374       "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
375       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
376
377   gst_element_class_add_pad_template (element_class,
378       gst_static_pad_template_get (&gst_optv_sink_template));
379   gst_element_class_add_pad_template (element_class,
380       gst_static_pad_template_get (&gst_optv_src_template));
381 }
382
383 static void
384 gst_optv_class_init (GstOpTVClass * klass)
385 {
386   GObjectClass *gobject_class = (GObjectClass *) klass;
387   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
388
389   gobject_class->set_property = gst_optv_set_property;
390   gobject_class->get_property = gst_optv_get_property;
391
392   gobject_class->finalize = gst_optv_finalize;
393
394   g_object_class_install_property (gobject_class, PROP_MODE,
395       g_param_spec_enum ("mode", "Mode",
396           "Mode", GST_TYPE_OPTV_MODE, DEFAULT_MODE,
397           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398
399   g_object_class_install_property (gobject_class, PROP_SPEED,
400       g_param_spec_int ("speed", "Speed",
401           "Effect speed", G_MININT, G_MAXINT, DEFAULT_SPEED,
402           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
403
404   g_object_class_install_property (gobject_class, PROP_THRESHOLD,
405       g_param_spec_uint ("threshold", "Threshold",
406           "Luma threshold", 0, G_MAXINT, DEFAULT_THRESHOLD,
407           GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408
409   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_optv_set_caps);
410   trans_class->transform = GST_DEBUG_FUNCPTR (gst_optv_transform);
411   trans_class->start = GST_DEBUG_FUNCPTR (gst_optv_start);
412
413   initPalette ();
414 }
415
416 static void
417 gst_optv_init (GstOpTV * filter, GstOpTVClass * klass)
418 {
419   filter->speed = DEFAULT_SPEED;
420   filter->mode = DEFAULT_MODE;
421   filter->threshold = DEFAULT_THRESHOLD;
422 }