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