Merge branch 'master' into 0.11
[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 /**
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 ! videoconvert ! 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 #ifndef M_PI
49 #define M_PI  3.14159265358979323846
50 #endif
51
52 #define gst_shagadelictv_parent_class parent_class
53 G_DEFINE_TYPE (GstShagadelicTV, gst_shagadelictv, GST_TYPE_VIDEO_FILTER);
54
55 static void gst_shagadelic_initialize (GstShagadelicTV * filter);
56
57 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
58 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("BGRx")
59 #else
60 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("xRGB")
61 #endif
62
63 static GstStaticPadTemplate gst_shagadelictv_src_template =
64 GST_STATIC_PAD_TEMPLATE ("src",
65     GST_PAD_SRC,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS (CAPS_STR)
68     );
69
70 static GstStaticPadTemplate gst_shagadelictv_sink_template =
71 GST_STATIC_PAD_TEMPLATE ("sink",
72     GST_PAD_SINK,
73     GST_PAD_ALWAYS,
74     GST_STATIC_CAPS (CAPS_STR)
75     );
76
77 static gboolean
78 gst_shagadelictv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
79     GstCaps * outcaps)
80 {
81   GstShagadelicTV *filter = GST_SHAGADELICTV (btrans);
82   GstVideoInfo info;
83   gint width, height, area;
84
85   if (!gst_video_info_from_caps (&info, incaps))
86     goto invalid_caps;
87
88   filter->info = info;
89
90   width = GST_VIDEO_INFO_WIDTH (&info);
91   height = GST_VIDEO_INFO_HEIGHT (&info);
92
93   area = width * height;
94
95   g_free (filter->ripple);
96   g_free (filter->spiral);
97   filter->ripple = (guint8 *) g_malloc (area * 4);
98   filter->spiral = (guint8 *) g_malloc (area);
99
100   gst_shagadelic_initialize (filter);
101
102   return TRUE;
103
104   /* ERRORS */
105 invalid_caps:
106   {
107     GST_DEBUG_OBJECT (filter, "invalid caps received");
108     return FALSE;
109   }
110 }
111
112 static void
113 gst_shagadelic_initialize (GstShagadelicTV * filter)
114 {
115   int i, x, y;
116 #ifdef PS2
117   float xx, yy;
118 #else
119   double xx, yy;
120 #endif
121   gint width, height;
122
123   width = GST_VIDEO_INFO_WIDTH (&filter->info);
124   height = GST_VIDEO_INFO_HEIGHT (&filter->info);
125
126   i = 0;
127   for (y = 0; y < height * 2; y++) {
128     yy = y - height;
129     yy *= yy;
130
131     for (x = 0; x < width * 2; x++) {
132       xx = x - width;
133 #ifdef PS2
134       filter->ripple[i++] = ((unsigned int) (sqrtf (xx * xx + yy) * 8)) & 255;
135 #else
136       filter->ripple[i++] = ((unsigned int) (sqrt (xx * xx + yy) * 8)) & 255;
137 #endif
138     }
139   }
140
141   i = 0;
142   for (y = 0; y < height; y++) {
143     yy = y - height / 2;
144
145     for (x = 0; x < width; x++) {
146       xx = x - width / 2;
147 #ifdef PS2
148       filter->spiral[i++] = ((unsigned int)
149           ((atan2f (xx,
150                       yy) / ((float) M_PI) * 256 * 9) + (sqrtf (xx * xx +
151                       yy * yy) * 5))) & 255;
152 #else
153       filter->spiral[i++] = ((unsigned int)
154           ((atan2 (xx, yy) / M_PI * 256 * 9) + (sqrt (xx * xx +
155                       yy * yy) * 5))) & 255;
156 #endif
157 /* Here is another Swinger!
158  * ((atan2(xx, yy)/M_PI*256) + (sqrt(xx*xx+yy*yy)*10))&255;
159  */
160     }
161   }
162   filter->rx = fastrand () % width;
163   filter->ry = fastrand () % height;
164   filter->bx = fastrand () % width;
165   filter->by = fastrand () % height;
166   filter->rvx = -2;
167   filter->rvy = -2;
168   filter->bvx = 2;
169   filter->bvy = 2;
170   filter->phase = 0;
171 }
172
173 static GstFlowReturn
174 gst_shagadelictv_transform (GstBaseTransform * trans, GstBuffer * in,
175     GstBuffer * out)
176 {
177   GstShagadelicTV *filter = GST_SHAGADELICTV (trans);
178   guint32 *src, *dest;
179   gint x, y;
180   guint32 v;
181   guint8 r, g, b;
182   GstVideoFrame in_frame, out_frame;
183   gint width, height;
184
185   if (!gst_video_frame_map (&in_frame, &filter->info, in, GST_MAP_READ))
186     goto invalid_in;
187
188   if (!gst_video_frame_map (&out_frame, &filter->info, out, GST_MAP_WRITE))
189     goto invalid_out;
190
191   src = GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
192   dest = GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
193
194   width = GST_VIDEO_FRAME_WIDTH (&in_frame);
195   height = GST_VIDEO_FRAME_HEIGHT (&in_frame);
196
197   for (y = 0; y < height; y++) {
198     for (x = 0; x < width; x++) {
199       v = *src++ | 0x1010100;
200       v = (v - 0x707060) & 0x1010100;
201       v -= v >> 8;
202 /* Try another Babe! 
203  * v = *src++;
204  * *dest++ = v & ((r<<16)|(g<<8)|b);
205  */
206       r = ((gint8) (filter->ripple[(filter->ry + y) * width * 2 + filter->rx +
207                   x] + filter->phase * 2)) >> 7;
208       g = ((gint8) (filter->spiral[y * width + x] + filter->phase * 3)) >> 7;
209       b = ((gint8) (filter->ripple[(filter->by + y) * width * 2 + filter->bx +
210                   x] - filter->phase)) >> 7;
211       *dest++ = v & ((r << 16) | (g << 8) | b);
212     }
213   }
214
215   filter->phase -= 8;
216   if ((filter->rx + filter->rvx) < 0 || (filter->rx + filter->rvx) >= width)
217     filter->rvx = -filter->rvx;
218   if ((filter->ry + filter->rvy) < 0 || (filter->ry + filter->rvy) >= height)
219     filter->rvy = -filter->rvy;
220   if ((filter->bx + filter->bvx) < 0 || (filter->bx + filter->bvx) >= width)
221     filter->bvx = -filter->bvx;
222   if ((filter->by + filter->bvy) < 0 || (filter->by + filter->bvy) >= height)
223     filter->bvy = -filter->bvy;
224   filter->rx += filter->rvx;
225   filter->ry += filter->rvy;
226   filter->bx += filter->bvx;
227   filter->by += filter->bvy;
228
229   gst_video_frame_unmap (&in_frame);
230   gst_video_frame_unmap (&out_frame);
231
232   return GST_FLOW_OK;
233
234   /* ERRORS */
235 invalid_in:
236   {
237     GST_DEBUG_OBJECT (filter, "invalid input frame");
238     return GST_FLOW_ERROR;
239   }
240 invalid_out:
241   {
242     GST_DEBUG_OBJECT (filter, "invalid output frame");
243     gst_video_frame_unmap (&in_frame);
244     return GST_FLOW_ERROR;
245   }
246 }
247
248 static void
249 gst_shagadelictv_finalize (GObject * object)
250 {
251   GstShagadelicTV *filter = GST_SHAGADELICTV (object);
252
253   if (filter->ripple)
254     g_free (filter->ripple);
255   filter->ripple = NULL;
256
257   if (filter->spiral)
258     g_free (filter->spiral);
259   filter->spiral = NULL;
260
261   G_OBJECT_CLASS (parent_class)->finalize (object);
262 }
263
264 static void
265 gst_shagadelictv_class_init (GstShagadelicTVClass * klass)
266 {
267   GObjectClass *gobject_class = (GObjectClass *) klass;
268   GstElementClass *gstelement_class = (GstElementClass *) klass;
269   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
270
271   gobject_class->finalize = gst_shagadelictv_finalize;
272
273   gst_element_class_set_details_simple (gstelement_class, "ShagadelicTV",
274       "Filter/Effect/Video",
275       "Oh behave, ShagedelicTV makes images shagadelic!",
276       "Wim Taymans <wim.taymans@chello.be>");
277
278   gst_element_class_add_pad_template (gstelement_class,
279       gst_static_pad_template_get (&gst_shagadelictv_sink_template));
280   gst_element_class_add_pad_template (gstelement_class,
281       gst_static_pad_template_get (&gst_shagadelictv_src_template));
282
283   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_shagadelictv_set_caps);
284   trans_class->transform = GST_DEBUG_FUNCPTR (gst_shagadelictv_transform);
285 }
286
287 static void
288 gst_shagadelictv_init (GstShagadelicTV * filter)
289 {
290   filter->ripple = NULL;
291   filter->spiral = NULL;
292
293   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (filter));
294   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (filter));
295 }