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