Updated for the new plugin loading code
[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 static GstElementDetails gst_warptv_details = GST_ELEMENT_DETAILS (
77   "WarpTV",
78   "Filter/Video/Effect",
79   "WarpTV does realtime goo'ing of the video input",
80   "Sam Lantinga <slouken@devolution.com>"
81 );
82
83
84 /* Filter signals and args */
85 enum
86 {
87   /* FILL ME */
88   LAST_SIGNAL
89 };
90
91 enum
92 {
93   ARG_0,
94 };
95
96 static void     gst_warptv_base_init            (gpointer g_class);
97 static void     gst_warptv_class_init           (GstWarpTVClass * klass);
98 static void     gst_warptv_init                 (GstWarpTV * filter);
99
100 static void     gst_warptv_initialize           (GstWarpTV *filter);
101
102 static void     gst_warptv_set_property         (GObject * object, guint prop_id,
103                                                  const GValue * value, GParamSpec * pspec);
104 static void     gst_warptv_get_property         (GObject * object, guint prop_id,
105                                                  GValue * value, GParamSpec * pspec);
106
107 static void     gst_warptv_chain                (GstPad * pad, GstData *_data);
108
109 static GstElementClass *parent_class = NULL;
110 /*static guint gst_warptv_signals[LAST_SIGNAL] = { 0 }; */
111
112 GType gst_warptv_get_type (void)
113 {
114   static GType warptv_type = 0;
115
116   if (!warptv_type) {
117     static const GTypeInfo warptv_info = {
118       sizeof (GstWarpTVClass), 
119       gst_warptv_base_init,
120       NULL,
121       (GClassInitFunc) gst_warptv_class_init,
122       NULL,
123       NULL,
124       sizeof (GstWarpTV),
125       0,
126       (GInstanceInitFunc) gst_warptv_init,
127     };
128
129     warptv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstWarpTV", &warptv_info, 0);
130   }
131   return warptv_type;
132 }
133
134 static void
135 gst_warptv_base_init (gpointer g_class)
136 {
137   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
138
139   gst_element_class_add_pad_template (element_class, gst_effectv_src_factory ());
140   gst_element_class_add_pad_template (element_class, gst_effectv_sink_factory ());
141  
142   gst_element_class_set_details (element_class, &gst_warptv_details);
143 }
144
145 static void
146 gst_warptv_class_init (GstWarpTVClass * klass)
147 {
148   GObjectClass *gobject_class;
149   GstElementClass *gstelement_class;
150
151   gobject_class = (GObjectClass *) klass;
152   gstelement_class = (GstElementClass *) klass;
153
154   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
155
156   gobject_class->set_property = gst_warptv_set_property;
157   gobject_class->get_property = gst_warptv_get_property;
158 }
159
160 static GstPadLinkReturn
161 gst_warptv_sinkconnect (GstPad * pad, GstCaps * caps)
162 {
163   GstWarpTV *filter;
164
165   filter = GST_WARPTV (gst_pad_get_parent (pad));
166
167   if (!GST_CAPS_IS_FIXED (caps))
168     return GST_PAD_LINK_DELAYED;
169
170   gst_caps_get_int (caps, "width", &filter->width);
171   gst_caps_get_int (caps, "height", &filter->height);
172
173   gst_warptv_initialize (filter);
174
175   return gst_pad_try_set_caps (filter->srcpad, gst_caps_ref (caps));
176 }
177
178 static void
179 gst_warptv_init (GstWarpTV * filter)
180 {
181   filter->sinkpad = gst_pad_new_from_template (gst_effectv_sink_factory (), "sink");
182   gst_pad_set_chain_function (filter->sinkpad, gst_warptv_chain);
183   gst_pad_set_link_function (filter->sinkpad, gst_warptv_sinkconnect);
184   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
185
186   filter->srcpad = gst_pad_new_from_template (gst_effectv_src_factory (), "src");
187   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
188
189   filter->tval = 0;
190   filter->disttable = NULL;
191   filter->offstable = NULL;
192 }
193
194
195 static void 
196 initSinTable (GstWarpTV *filter) 
197 {
198   gint32        *tptr, *tsinptr;
199   double        i;
200
201   tsinptr = tptr = filter->sintable;
202
203   for (i = 0; i < 1024; i++)
204     *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
205
206   for (i = 0; i < 256; i++)
207     *tptr++ = *tsinptr++;
208 }
209
210 static void 
211 initOffsTable (GstWarpTV *filter) 
212 {
213   int y;
214         
215   for (y = 0; y < filter->height; y++) {
216     filter->offstable[y] = y * filter->width;
217   }
218 }
219       
220 static void 
221 initDistTable (GstWarpTV *filter) 
222 {
223   gint32 halfw, halfh, *distptr;
224 #ifdef PS2
225   float x,y,m;
226 #else
227   double x,y,m;
228 #endif
229
230   halfw = filter->width>> 1;
231   halfh = filter->height >> 1;
232
233   distptr = filter->disttable;
234
235   m = sqrt ((double)(halfw * halfw + halfh * halfh));
236
237   for (y = -halfh; y < halfh; y++)
238     for (x= -halfw; x < halfw; x++)
239 #ifdef PS2
240       *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
241 #else
242       *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
243 #endif
244 }
245
246 static void 
247 gst_warptv_initialize (GstWarpTV *filter) 
248 {
249   g_free (filter->disttable);
250   g_free (filter->offstable);
251
252   filter->offstable = (guint32 *) g_malloc (filter->height * sizeof (guint32));      
253   filter->disttable = g_malloc (filter->width * filter->height * sizeof (guint32));
254
255   initSinTable (filter);
256   initOffsTable (filter);
257   initDistTable (filter);
258 }
259
260 static void
261 gst_warptv_chain (GstPad * pad, GstData *_data)
262 {
263   GstBuffer *buf = GST_BUFFER (_data);
264   GstWarpTV *filter;
265   guint32 *src, *dest;
266   gint xw,yw,cw;
267   GstBuffer *outbuf;
268   gint32 c,i, x,y, dx,dy, maxx, maxy;
269   gint32 width, height, skip, *ctptr, *distptr;
270   gint32 *sintable, *ctable;
271
272   filter = GST_WARPTV (gst_pad_get_parent (pad));
273
274   src = (guint32 *) GST_BUFFER_DATA (buf);
275
276   outbuf = gst_buffer_new ();
277   GST_BUFFER_SIZE (outbuf) = (filter->width * filter->height * sizeof(guint32));
278   dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
279   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
280   
281   xw  = (gint) (sin ((filter->tval + 100) * M_PI / 128) * 30);
282   yw  = (gint) (sin ((filter->tval) * M_PI / 256) * -35);
283   cw  = (gint) (sin ((filter->tval - 70) * M_PI / 64) * 50);
284   xw += (gint) (sin ((filter->tval - 10) * M_PI / 512) * 40);
285   yw += (gint) (sin ((filter->tval + 30) * M_PI / 512) * 40);     
286
287   ctptr = filter->ctable;
288   distptr = filter->disttable;
289   width = filter->width;
290   height = filter->height;
291   sintable = filter->sintable;
292   ctable = filter->ctable;
293
294   skip = 0 ; /* video_width*sizeof(RGB32)/4 - video_width;; */
295   c = 0;
296
297   for (x = 0; x < 512; x++) {
298     i = (c >> 3) & 0x3FE;
299     *ctptr++ = ((sintable[i] * yw) >> 15);
300     *ctptr++ = ((sintable[i + 256] * xw) >> 15);
301     c += cw;
302   }
303   maxx = width - 2; maxy = height - 2;
304
305   for (y = 0; y < height - 1; y++) {
306     for (x = 0; x < width; x++) {
307       i = *distptr++; 
308       dx = ctable [i + 1] + x; 
309       dy = ctable [i] + y;       
310
311       if (dx < 0) dx = 0; 
312       else if (dx > maxx) dx = maxx; 
313    
314       if (dy < 0) dy = 0; 
315       else if (dy > maxy) dy = maxy; 
316       *dest++ = src[filter->offstable[dy] + dx]; 
317     }
318     dest += skip;
319   }
320
321   filter->tval = (filter->tval + 1) & 511;
322
323   gst_buffer_unref (buf);
324
325   gst_pad_push (filter->srcpad, GST_DATA (outbuf));
326 }
327
328 static void
329 gst_warptv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
330 {
331   GstWarpTV *filter;
332
333   /* it's not null if we got it, but it might not be ours */
334   g_return_if_fail (GST_IS_WARPTV (object));
335
336   filter = GST_WARPTV (object);
337
338   switch (prop_id) {
339     default:
340       break;
341   }
342 }
343
344 static void
345 gst_warptv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
346 {
347   GstWarpTV *filter;
348
349   /* it's not null if we got it, but it might not be ours */
350   g_return_if_fail (GST_IS_WARPTV (object));
351
352   filter = GST_WARPTV (object);
353
354   switch (prop_id) {
355     default:
356       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
357       break;
358   }
359 }