Some cleanups ported the über cool warp plugin.
[platform/upstream/gstreamer.git] / gst / effectv / gstwarp.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * EffecTV is free software. We release this product under the terms of the
5  * GNU General Public License version 2. The license is included in the file
6  * COPYING.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
9  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
10  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
11  */
12 /*
13  * EffecTV - Realtime Digital Video Effector
14  * Copyright (C) 2001 FUKUCHI Kentarou
15  *
16  * From main.c of warp-1.1:
17  *
18  *      Simple DirectMedia Layer demo
19  *      Realtime picture 'gooing'
20  *      Released under GPL
21  *      by sam lantinga slouken@devolution.com
22  */
23
24 #include <string.h>
25 #include <math.h>
26 #include <gst/gst.h>
27 #include "gsteffectv.h"
28
29 #ifndef M_PI
30 #define M_PI    3.14159265358979323846
31 #endif
32
33 #define GST_TYPE_WARPTV \
34   (gst_warptv_get_type())
35 #define GST_WARPTV(obj) \
36   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_WARPTV,GstWarpTV))
37 #define GST_WARPTV_CLASS(klass) \
38   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstWarpTV))
39 #define GST_IS_WARPTV(obj) \
40   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_WARPTV))
41 #define GST_IS_WARPTV_CLASS(obj) \
42   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_WARPTV))
43
44 typedef struct _GstWarpTV GstWarpTV;
45 typedef struct _GstWarpTVClass GstWarpTVClass;
46
47 struct _GstWarpTV
48 {
49   GstElement element;
50
51   GstPad *sinkpad, *srcpad;
52
53   gint width, height;
54   gint *offstable;
55   gint32 *disttable;
56   gint32 ctable[1024];
57   gint32 sintable[1024+256];
58   gint tval;
59 };
60
61 struct _GstWarpTVClass
62 {
63   GstElementClass parent_class;
64 };
65
66 GstElementDetails gst_warptv_details = {
67   "WarpTV",
68   "Filter/Effect",
69   "WarpTV does realtime goo'ing of the video input",
70   VERSION,
71   "Sam Lantinga <slouken@devolution.com>",
72   "Wim Taymans <wim.taymans@chello.be>, "
73   "(C) 2001 FUKUCHI Kentarou",
74 };
75
76
77 /* Filter signals and args */
78 enum
79 {
80   /* FILL ME */
81   LAST_SIGNAL
82 };
83
84 enum
85 {
86   ARG_0,
87 };
88
89 static void     gst_warptv_class_init           (GstWarpTVClass * klass);
90 static void     gst_warptv_init                 (GstWarpTV * filter);
91
92 static void     gst_warptv_initialize           (GstWarpTV *filter);
93 static void     gst_warptv_dispose              (GObject *object);
94
95 static void     gst_warptv_set_property         (GObject * object, guint prop_id,
96                                                  const GValue * value, GParamSpec * pspec);
97 static void     gst_warptv_get_property         (GObject * object, guint prop_id,
98                                                  GValue * value, GParamSpec * pspec);
99
100 static void     gst_warptv_chain                (GstPad * pad, GstBuffer * buf);
101
102 static GstElementClass *parent_class = NULL;
103 /*static guint gst_warptv_signals[LAST_SIGNAL] = { 0 }; */
104
105 GType gst_warptv_get_type (void)
106 {
107   static GType warptv_type = 0;
108
109   if (!warptv_type) {
110     static const GTypeInfo warptv_info = {
111       sizeof (GstWarpTVClass), NULL,
112       NULL,
113       (GClassInitFunc) gst_warptv_class_init,
114       NULL,
115       NULL,
116       sizeof (GstWarpTV),
117       0,
118       (GInstanceInitFunc) gst_warptv_init,
119     };
120
121     warptv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstWarpTV", &warptv_info, 0);
122   }
123   return warptv_type;
124 }
125
126 static void
127 gst_warptv_class_init (GstWarpTVClass * klass)
128 {
129   GObjectClass *gobject_class;
130   GstElementClass *gstelement_class;
131
132   gobject_class = (GObjectClass *) klass;
133   gstelement_class = (GstElementClass *) klass;
134
135   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
136
137   gobject_class->set_property = gst_warptv_set_property;
138   gobject_class->get_property = gst_warptv_get_property;
139   gobject_class->dispose      = gst_warptv_dispose;
140 }
141
142 static GstPadConnectReturn
143 gst_warptv_sinkconnect (GstPad * pad, GstCaps * caps)
144 {
145   GstWarpTV *filter;
146
147   filter = GST_WARPTV (gst_pad_get_parent (pad));
148
149   if (!GST_CAPS_IS_FIXED (caps))
150     return GST_PAD_CONNECT_DELAYED;
151
152   gst_caps_get_int (caps, "width", &filter->width);
153   gst_caps_get_int (caps, "height", &filter->height);
154
155   gst_warptv_initialize (filter);
156
157   if (gst_pad_try_set_caps (filter->srcpad, caps)) {
158     return GST_PAD_CONNECT_OK;
159   }
160
161   return GST_PAD_CONNECT_REFUSED;
162 }
163
164 static void
165 gst_warptv_init (GstWarpTV * filter)
166 {
167   filter->sinkpad = gst_pad_new_from_template (gst_effectv_sink_factory (), "sink");
168   gst_pad_set_chain_function (filter->sinkpad, gst_warptv_chain);
169   gst_pad_set_connect_function (filter->sinkpad, gst_warptv_sinkconnect);
170   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
171
172   filter->srcpad = gst_pad_new_from_template (gst_effectv_src_factory (), "src");
173   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
174
175   filter->tval = 0;
176 }
177
178
179 static void 
180 initSinTable (GstWarpTV *filter) 
181 {
182   gint32        *tptr, *tsinptr;
183   double        i;
184
185   tsinptr = tptr = filter->sintable;
186
187   for (i = 0; i < 1024; i++)
188     *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
189
190   for (i = 0; i < 256; i++)
191     *tptr++ = *tsinptr++;
192 }
193
194 static void 
195 initOffsTable (GstWarpTV *filter) 
196 {
197   int y;
198         
199   for (y = 0; y < filter->height; y++) {
200     filter->offstable[y] = y * filter->width;
201   }
202 }
203       
204 static void 
205 initDistTable (GstWarpTV *filter) 
206 {
207   gint32 halfw, halfh, *distptr;
208 #ifdef PS2
209   float x,y,m;
210 #else
211   double x,y,m;
212 #endif
213
214   halfw = filter->width>> 1;
215   halfh = filter->height >> 1;
216
217   distptr = filter->disttable;
218
219   m = sqrt ((double)(halfw * halfw + halfh * halfh));
220
221   for (y = -halfh; y < halfh; y++)
222     for (x= -halfw; x < halfw; x++)
223 #ifdef PS2
224       *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
225 #else
226       *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
227 #endif
228 }
229
230 static void 
231 gst_warptv_initialize (GstWarpTV *filter) 
232 {
233   filter->offstable = (guint32 *) g_malloc (filter->height * sizeof (guint32));      
234   filter->disttable = g_malloc (filter->width * filter->height * sizeof (guint32));
235
236   initSinTable (filter);
237   initOffsTable (filter);
238   initDistTable (filter);
239 }
240
241 static void 
242 gst_warptv_dispose (GObject *object) 
243 {
244   GstWarpTV *filter = GST_WARPTV (object);
245
246   g_free (filter->disttable);
247   g_free (filter->offstable);
248 }
249
250 static void
251 gst_warptv_chain (GstPad * pad, GstBuffer * buf)
252 {
253   GstWarpTV *filter;
254   guint32 *src, *dest;
255   gint xw,yw,cw;
256   GstBuffer *outbuf;
257   gint32 c,i, x,y, dx,dy, maxx, maxy;
258   gint32 width, height, skip, *ctptr, *distptr;
259   gint32 *sintable, *ctable;
260
261   filter = GST_WARPTV (gst_pad_get_parent (pad));
262
263   src = (guint32 *) GST_BUFFER_DATA (buf);
264
265   outbuf = gst_buffer_new ();
266   GST_BUFFER_SIZE (outbuf) = (filter->width * filter->height * sizeof(guint32));
267   dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
268   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
269   
270   xw  = (gint) (sin ((filter->tval + 100) * M_PI / 128) * 30);
271   yw  = (gint) (sin ((filter->tval) * M_PI / 256) * -35);
272   cw  = (gint) (sin ((filter->tval - 70) * M_PI / 64) * 50);
273   xw += (gint) (sin ((filter->tval - 10) * M_PI / 512) * 40);
274   yw += (gint) (sin ((filter->tval + 30) * M_PI / 512) * 40);     
275
276   ctptr = filter->ctable;
277   distptr = filter->disttable;
278   width = filter->width;
279   height = filter->height;
280   sintable = filter->sintable;
281   ctable = filter->ctable;
282
283   skip = 0 ; /* video_width*sizeof(RGB32)/4 - video_width;; */
284   c = 0;
285
286   for (x = 0; x < 512; x++) {
287     i = (c >> 3) & 0x3FE;
288     *ctptr++ = ((sintable[i] * yw) >> 15);
289     *ctptr++ = ((sintable[i + 256] * xw) >> 15);
290     c += cw;
291   }
292   maxx = width - 2; maxy = height - 2;
293
294   for (y = 0; y < height - 1; y++) {
295     for (x = 0; x < width; x++) {
296       i = *distptr++; 
297       dx = ctable [i + 1] + x; 
298       dy = ctable [i] + y;       
299
300       if (dx < 0) dx = 0; 
301       else if (dx > maxx) dx = maxx; 
302    
303       if (dy < 0) dy = 0; 
304       else if (dy > maxy) dy = maxy; 
305       *dest++ = src[filter->offstable[dy] + dx]; 
306     }
307     dest += skip;
308   }
309
310   filter->tval = (filter->tval + 1) & 511;
311
312   gst_buffer_unref (buf);
313
314   gst_pad_push (filter->srcpad, outbuf);
315 }
316
317 static void
318 gst_warptv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
319 {
320   GstWarpTV *filter;
321
322   /* it's not null if we got it, but it might not be ours */
323   g_return_if_fail (GST_IS_WARPTV (object));
324
325   filter = GST_WARPTV (object);
326
327   switch (prop_id) {
328     default:
329       break;
330   }
331 }
332
333 static void
334 gst_warptv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
335 {
336   GstWarpTV *filter;
337
338   /* it's not null if we got it, but it might not be ours */
339   g_return_if_fail (GST_IS_WARPTV (object));
340
341   filter = GST_WARPTV (object);
342
343   switch (prop_id) {
344     default:
345       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346       break;
347   }
348 }