effectv: Remove get_unit_size implementations
[platform/upstream/gst-plugins-good.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 <math.h>
30 #include <string.h>
31
32 #include <gst/gst.h>
33
34 #include <gst/video/video.h>
35 #include <gst/video/gstvideofilter.h>
36
37 #ifndef M_PI
38 #define M_PI  3.14159265358979323846
39 #endif
40
41 #define GST_TYPE_SHAGADELICTV \
42   (gst_shagadelictv_get_type())
43 #define GST_SHAGADELICTV(obj) \
44   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_SHAGADELICTV,GstShagadelicTV))
45 #define GST_SHAGADELICTV_CLASS(klass) \
46   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_SHAGADELICTV,GstShagadelicTVClass))
47 #define GST_IS_SHAGADELICTV(obj) \
48   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_SHAGADELICTV))
49 #define GST_IS_SHAGADELICTV_CLASS(klass) \
50   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_SHAGADELICTV))
51
52 typedef struct _GstShagadelicTV GstShagadelicTV;
53 typedef struct _GstShagadelicTVClass GstShagadelicTVClass;
54
55 struct _GstShagadelicTV
56 {
57   GstVideoFilter videofilter;
58
59   gint width, height;
60   gint stat;
61   guint8 *ripple;
62   guint8 *spiral;
63   guint8 phase;
64   gint rx, ry;
65   gint bx, by;
66   gint rvx, rvy;
67   gint bvx, bvy;
68 };
69
70 struct _GstShagadelicTVClass
71 {
72   GstVideoFilterClass parent_class;
73 };
74
75 GST_BOILERPLATE (GstShagadelicTV, gst_shagadelictv, GstVideoFilter,
76     GST_TYPE_VIDEO_FILTER);
77
78 static void gst_shagadelic_initialize (GstShagadelicTV * filter);
79
80 static GstStaticPadTemplate gst_shagadelictv_src_template =
81 GST_STATIC_PAD_TEMPLATE ("src",
82     GST_PAD_SRC,
83     GST_PAD_ALWAYS,
84     GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
85     );
86
87 static GstStaticPadTemplate gst_shagadelictv_sink_template =
88 GST_STATIC_PAD_TEMPLATE ("sink",
89     GST_PAD_SINK,
90     GST_PAD_ALWAYS,
91     GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
92     );
93
94 static gboolean
95 gst_shagadelictv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
96     GstCaps * outcaps)
97 {
98   GstShagadelicTV *filter = GST_SHAGADELICTV (btrans);
99   GstStructure *structure;
100   gboolean ret = FALSE;
101
102   structure = gst_caps_get_structure (incaps, 0);
103
104   if (gst_structure_get_int (structure, "width", &filter->width) &&
105       gst_structure_get_int (structure, "height", &filter->height)) {
106     gint area = filter->width * filter->height;
107
108     g_free (filter->ripple);
109     g_free (filter->spiral);
110
111     filter->ripple = (guint8 *) g_malloc (area * 4);
112     filter->spiral = (guint8 *) g_malloc (area);
113
114     gst_shagadelic_initialize (filter);
115     ret = TRUE;
116   }
117
118   return ret;
119 }
120
121 static inline guint
122 fastrand (void)
123 {
124   static guint fastrand_val;
125
126   return (fastrand_val = fastrand_val * 1103515245 + 12345);
127 }
128
129 static void
130 gst_shagadelic_initialize (GstShagadelicTV * filter)
131 {
132   int i, x, y;
133
134 #ifdef PS2
135   float xx, yy;
136 #else
137   double xx, yy;
138 #endif
139
140   i = 0;
141   for (y = 0; y < filter->height * 2; y++) {
142     yy = y - filter->height;
143     yy *= yy;
144
145     for (x = 0; x < filter->width * 2; x++) {
146       xx = x - filter->width;
147 #ifdef PS2
148       filter->ripple[i++] = ((unsigned int) (sqrtf (xx * xx + yy) * 8)) & 255;
149 #else
150       filter->ripple[i++] = ((unsigned int) (sqrt (xx * xx + yy) * 8)) & 255;
151 #endif
152     }
153   }
154
155   i = 0;
156   for (y = 0; y < filter->height; y++) {
157     yy = y - filter->height / 2;
158
159     for (x = 0; x < filter->width; x++) {
160       xx = x - filter->width / 2;
161 #ifdef PS2
162       filter->spiral[i++] = ((unsigned int)
163           ((atan2f (xx,
164                       yy) / ((float) M_PI) * 256 * 9) + (sqrtf (xx * xx +
165                       yy * yy) * 5))) & 255;
166 #else
167       filter->spiral[i++] = ((unsigned int)
168           ((atan2 (xx, yy) / M_PI * 256 * 9) + (sqrt (xx * xx +
169                       yy * yy) * 5))) & 255;
170 #endif
171 /* Here is another Swinger!
172  * ((atan2(xx, yy)/M_PI*256) + (sqrt(xx*xx+yy*yy)*10))&255;
173  */
174     }
175   }
176   filter->rx = fastrand () % filter->width;
177   filter->ry = fastrand () % filter->height;
178   filter->bx = fastrand () % filter->width;
179   filter->by = fastrand () % filter->height;
180   filter->rvx = -2;
181   filter->rvy = -2;
182   filter->bvx = 2;
183   filter->bvy = 2;
184   filter->phase = 0;
185 }
186
187 static GstFlowReturn
188 gst_shagadelictv_transform (GstBaseTransform * trans, GstBuffer * in,
189     GstBuffer * out)
190 {
191   GstShagadelicTV *filter = GST_SHAGADELICTV (trans);
192   guint32 *src, *dest;
193   gint x, y;
194   guint32 v;
195   guint8 r, g, b;
196   gint width, height;
197   GstFlowReturn ret = GST_FLOW_OK;
198
199   src = (guint32 *) GST_BUFFER_DATA (in);
200   dest = (guint32 *) GST_BUFFER_DATA (out);
201
202   width = filter->width;
203   height = filter->height;
204
205   for (y = 0; y < height; y++) {
206     for (x = 0; x < width; x++) {
207       v = *src++ | 0x1010100;
208       v = (v - 0x707060) & 0x1010100;
209       v -= v >> 8;
210 /* Try another Babe! 
211  * v = *src++;
212  * *dest++ = v & ((r<<16)|(g<<8)|b);
213  */
214       r = ((gint8) (filter->ripple[(filter->ry + y) * width * 2 + filter->rx +
215                   x] + filter->phase * 2)) >> 7;
216       g = ((gint8) (filter->spiral[y * width + x] + filter->phase * 3)) >> 7;
217       b = ((gint8) (filter->ripple[(filter->by + y) * width * 2 + filter->bx +
218                   x] - filter->phase)) >> 7;
219       *dest++ = v & ((r << 16) | (g << 8) | b);
220     }
221   }
222
223   filter->phase -= 8;
224   if ((filter->rx + filter->rvx) < 0 || (filter->rx + filter->rvx) >= width)
225     filter->rvx = -filter->rvx;
226   if ((filter->ry + filter->rvy) < 0 || (filter->ry + filter->rvy) >= height)
227     filter->rvy = -filter->rvy;
228   if ((filter->bx + filter->bvx) < 0 || (filter->bx + filter->bvx) >= width)
229     filter->bvx = -filter->bvx;
230   if ((filter->by + filter->bvy) < 0 || (filter->by + filter->bvy) >= height)
231     filter->bvy = -filter->bvy;
232   filter->rx += filter->rvx;
233   filter->ry += filter->rvy;
234   filter->bx += filter->bvx;
235   filter->by += filter->bvy;
236
237   return ret;
238 }
239
240 static void
241 gst_shagadelictv_finalize (GObject * object)
242 {
243   GstShagadelicTV *filter = GST_SHAGADELICTV (object);
244
245   if (filter->ripple)
246     g_free (filter->ripple);
247   filter->ripple = NULL;
248
249   if (filter->spiral)
250     g_free (filter->spiral);
251   filter->spiral = NULL;
252
253   G_OBJECT_CLASS (parent_class)->finalize (object);
254 }
255
256 static void
257 gst_shagadelictv_base_init (gpointer g_class)
258 {
259   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
260
261   gst_element_class_set_details_simple (element_class, "ShagadelicTV",
262       "Filter/Effect/Video",
263       "Oh behave, ShagedelicTV makes images shagadelic!",
264       "Wim Taymans <wim.taymans@chello.be>");
265
266   gst_element_class_add_pad_template (element_class,
267       gst_static_pad_template_get (&gst_shagadelictv_sink_template));
268   gst_element_class_add_pad_template (element_class,
269       gst_static_pad_template_get (&gst_shagadelictv_src_template));
270 }
271
272 static void
273 gst_shagadelictv_class_init (GstShagadelicTVClass * klass)
274 {
275   GObjectClass *gobject_class = (GObjectClass *) klass;
276   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
277
278   gobject_class->finalize = gst_shagadelictv_finalize;
279
280   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_shagadelictv_set_caps);
281   trans_class->transform = GST_DEBUG_FUNCPTR (gst_shagadelictv_transform);
282 }
283
284 static void
285 gst_shagadelictv_init (GstShagadelicTV * filter, GstShagadelicTVClass * klass)
286 {
287   filter->ripple = NULL;
288   filter->spiral = NULL;
289 }