Remove unused variables in _class_init
[platform/upstream/gstreamer.git] / gst / effectv / gstshagadelic.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * EffecTV:
5  * Copyright (C) 2001 FUKUCHI Kentarou
6  *
7  * Inspired by Adrian Likin's script for the GIMP.
8  * EffecTV is free software.  This library is free software;
9  * you can redistribute it and/or
10  *  modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <gst/video/gstvideofilter.h>
30
31 #include <math.h>
32 #include <string.h>
33
34 #include <gst/video/video.h>
35
36 #ifndef M_PI
37 #define M_PI  3.14159265358979323846
38 #endif
39
40 #define GST_TYPE_SHAGADELICTV \
41   (gst_shagadelictv_get_type())
42 #define GST_SHAGADELICTV(obj) \
43   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_SHAGADELICTV,GstShagadelicTV))
44 #define GST_SHAGADELICTV_CLASS(klass) \
45   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_SHAGADELICTV,GstShagadelicTVClass))
46 #define GST_IS_SHAGADELICTV(obj) \
47   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_SHAGADELICTV))
48 #define GST_IS_SHAGADELICTV_CLASS(klass) \
49   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_SHAGADELICTV))
50
51 typedef struct _GstShagadelicTV GstShagadelicTV;
52 typedef struct _GstShagadelicTVClass GstShagadelicTVClass;
53
54 struct _GstShagadelicTV
55 {
56   GstVideoFilter videofilter;
57
58   gint width, height;
59   gint stat;
60   gchar *ripple;
61   gchar *spiral;
62   guchar phase;
63   gint rx, ry;
64   gint bx, by;
65   gint rvx, rvy;
66   gint bvx, bvy;
67 };
68
69 struct _GstShagadelicTVClass
70 {
71   GstVideoFilterClass parent_class;
72 };
73
74 GType gst_shagadelictv_get_type (void);
75
76 static void gst_shagadelic_initialize (GstShagadelicTV * filter);
77
78 static const GstElementDetails shagadelictv_details =
79 GST_ELEMENT_DETAILS ("ShagadelicTV",
80     "Filter/Effect/Video",
81     "Oh behave, ShagedelicTV makes images shagadelic!",
82     "Wim Taymans <wim.taymans@chello.be>");
83
84 static GstStaticPadTemplate gst_shagadelictv_src_template =
85 GST_STATIC_PAD_TEMPLATE ("src",
86     GST_PAD_SRC,
87     GST_PAD_ALWAYS,
88     GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
89     );
90
91 static GstStaticPadTemplate gst_shagadelictv_sink_template =
92 GST_STATIC_PAD_TEMPLATE ("sink",
93     GST_PAD_SINK,
94     GST_PAD_ALWAYS,
95     GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
96     );
97
98 static GstVideoFilterClass *parent_class = NULL;
99
100 static gboolean
101 gst_shagadelictv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
102     GstCaps * outcaps)
103 {
104   GstShagadelicTV *filter = GST_SHAGADELICTV (btrans);
105   GstStructure *structure;
106   gboolean ret = FALSE;
107
108   structure = gst_caps_get_structure (incaps, 0);
109
110   if (gst_structure_get_int (structure, "width", &filter->width) &&
111       gst_structure_get_int (structure, "height", &filter->height)) {
112     gint area = filter->width * filter->height;
113
114     g_free (filter->ripple);
115     g_free (filter->spiral);
116
117     filter->ripple = (gchar *) g_malloc (area * 4);
118     filter->spiral = (gchar *) g_malloc (area);
119
120     gst_shagadelic_initialize (filter);
121     ret = TRUE;
122   }
123
124   return ret;
125 }
126
127 static gboolean
128 gst_shagadelictv_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
129     guint * size)
130 {
131   GstShagadelicTV *filter;
132   GstStructure *structure;
133   gboolean ret = FALSE;
134   gint width, height;
135
136   filter = GST_SHAGADELICTV (btrans);
137
138   structure = gst_caps_get_structure (caps, 0);
139
140   if (gst_structure_get_int (structure, "width", &width) &&
141       gst_structure_get_int (structure, "height", &height)) {
142     *size = width * height * 32 / 8;
143     ret = TRUE;
144     GST_DEBUG_OBJECT (filter, "our frame size is %d bytes (%dx%d)", *size,
145         width, height);
146   }
147
148   return ret;
149 }
150
151 static unsigned int
152 fastrand (void)
153 {
154   static unsigned int fastrand_val;
155
156   return (fastrand_val = fastrand_val * 1103515245 + 12345);
157 }
158
159 static void
160 gst_shagadelic_initialize (GstShagadelicTV * filter)
161 {
162   int i, x, y;
163
164 #ifdef PS2
165   float xx, yy;
166 #else
167   double xx, yy;
168 #endif
169
170   i = 0;
171   for (y = 0; y < filter->height * 2; y++) {
172     yy = y - filter->height;
173     yy *= yy;
174
175     for (x = 0; x < filter->width * 2; x++) {
176       xx = x - filter->width;
177 #ifdef PS2
178       filter->ripple[i++] = ((unsigned int) (sqrtf (xx * xx + yy) * 8)) & 255;
179 #else
180       filter->ripple[i++] = ((unsigned int) (sqrt (xx * xx + yy) * 8)) & 255;
181 #endif
182     }
183   }
184
185   i = 0;
186   for (y = 0; y < filter->height; y++) {
187     yy = y - filter->height / 2;
188
189     for (x = 0; x < filter->width; x++) {
190       xx = x - filter->width / 2;
191 #ifdef PS2
192       filter->spiral[i++] = ((unsigned int)
193           ((atan2f (xx,
194                       yy) / ((float) M_PI) * 256 * 9) + (sqrtf (xx * xx +
195                       yy * yy) * 5))) & 255;
196 #else
197       filter->spiral[i++] = ((unsigned int)
198           ((atan2 (xx, yy) / M_PI * 256 * 9) + (sqrt (xx * xx +
199                       yy * yy) * 5))) & 255;
200 #endif
201 /* Here is another Swinger!
202  * ((atan2(xx, yy)/M_PI*256) + (sqrt(xx*xx+yy*yy)*10))&255;
203  */
204     }
205   }
206   filter->rx = fastrand () % filter->width;
207   filter->ry = fastrand () % filter->height;
208   filter->bx = fastrand () % filter->width;
209   filter->by = fastrand () % filter->height;
210   filter->rvx = -2;
211   filter->rvy = -2;
212   filter->bvx = 2;
213   filter->bvy = 2;
214   filter->phase = 0;
215 }
216
217 static GstFlowReturn
218 gst_shagadelictv_transform (GstBaseTransform * trans, GstBuffer * in,
219     GstBuffer * out)
220 {
221   GstShagadelicTV *filter;
222   guint32 *src, *dest;
223   gint x, y;
224   guint32 v;
225   guchar r, g, b;
226   gint width, height;
227   GstFlowReturn ret = GST_FLOW_OK;
228
229   filter = GST_SHAGADELICTV (trans);
230
231   gst_buffer_copy_metadata (out, in, GST_BUFFER_COPY_TIMESTAMPS);
232
233   src = (guint32 *) GST_BUFFER_DATA (in);
234   dest = (guint32 *) GST_BUFFER_DATA (out);
235
236   width = filter->width;
237   height = filter->height;
238
239   for (y = 0; y < height; y++) {
240     for (x = 0; x < width; x++) {
241       v = *src++ | 0x1010100;
242       v = (v - 0x707060) & 0x1010100;
243       v -= v >> 8;
244 /* Try another Babe! 
245  * v = *src++;
246  * *dest++ = v & ((r<<16)|(g<<8)|b);
247  */
248       r = (gchar) (filter->ripple[(filter->ry + y) * width * 2 + filter->rx +
249               x] + filter->phase * 2) >> 7;
250       g = (gchar) (filter->spiral[y * width + x] + filter->phase * 3) >> 7;
251       b = (gchar) (filter->ripple[(filter->by + y) * width * 2 + filter->bx +
252               x] - filter->phase) >> 7;
253       *dest++ = v & ((r << 16) | (g << 8) | b);
254     }
255   }
256
257   filter->phase -= 8;
258   if ((filter->rx + filter->rvx) < 0 || (filter->rx + filter->rvx) >= width)
259     filter->rvx = -filter->rvx;
260   if ((filter->ry + filter->rvy) < 0 || (filter->ry + filter->rvy) >= height)
261     filter->rvy = -filter->rvy;
262   if ((filter->bx + filter->bvx) < 0 || (filter->bx + filter->bvx) >= width)
263     filter->bvx = -filter->bvx;
264   if ((filter->by + filter->bvy) < 0 || (filter->by + filter->bvy) >= height)
265     filter->bvy = -filter->bvy;
266   filter->rx += filter->rvx;
267   filter->ry += filter->rvy;
268   filter->bx += filter->bvx;
269   filter->by += filter->bvy;
270
271   return ret;
272 }
273
274 static void
275 gst_shagadelictv_base_init (gpointer g_class)
276 {
277   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
278
279   gst_element_class_set_details (element_class, &shagadelictv_details);
280
281   gst_element_class_add_pad_template (element_class,
282       gst_static_pad_template_get (&gst_shagadelictv_sink_template));
283   gst_element_class_add_pad_template (element_class,
284       gst_static_pad_template_get (&gst_shagadelictv_src_template));
285 }
286
287 static void
288 gst_shagadelictv_class_init (gpointer klass, gpointer class_data)
289 {
290   GstBaseTransformClass *trans_class;
291
292   trans_class = (GstBaseTransformClass *) klass;
293
294   parent_class = g_type_class_peek_parent (klass);
295
296   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_shagadelictv_set_caps);
297   trans_class->get_unit_size =
298       GST_DEBUG_FUNCPTR (gst_shagadelictv_get_unit_size);
299   trans_class->transform = GST_DEBUG_FUNCPTR (gst_shagadelictv_transform);
300 }
301
302 static void
303 gst_shagadelictv_init (GTypeInstance * instance, gpointer g_class)
304 {
305   GstShagadelicTV *filter = GST_SHAGADELICTV (instance);
306
307   filter->ripple = NULL;
308   filter->spiral = NULL;
309 }
310
311 GType
312 gst_shagadelictv_get_type (void)
313 {
314   static GType shagadelictv_type = 0;
315
316   if (!shagadelictv_type) {
317     static const GTypeInfo shagadelictv_info = {
318       sizeof (GstShagadelicTVClass),
319       gst_shagadelictv_base_init,
320       NULL,
321       (GClassInitFunc) gst_shagadelictv_class_init,
322       NULL,
323       NULL,
324       sizeof (GstShagadelicTV),
325       0,
326       (GInstanceInitFunc) gst_shagadelictv_init,
327     };
328
329     shagadelictv_type =
330         g_type_register_static (GST_TYPE_VIDEO_FILTER, "GstShagadelicTV",
331         &shagadelictv_info, 0);
332   }
333   return shagadelictv_type;
334 }