Fixed license statements in EffectTv plugins so they now state LGPL.
[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  *      Released under GPL
28  *      by sam lantinga slouken@devolution.com
29  */
30
31 #include <string.h>
32 #include <math.h>
33 #include <gst/gst.h>
34 #include "gsteffectv.h"
35
36 #ifndef M_PI
37 #define M_PI    3.14159265358979323846
38 #endif
39
40 #define GST_TYPE_WARPTV \
41   (gst_warptv_get_type())
42 #define GST_WARPTV(obj) \
43   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_WARPTV,GstWarpTV))
44 #define GST_WARPTV_CLASS(klass) \
45   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstWarpTV))
46 #define GST_IS_WARPTV(obj) \
47   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_WARPTV))
48 #define GST_IS_WARPTV_CLASS(obj) \
49   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_WARPTV))
50
51 typedef struct _GstWarpTV GstWarpTV;
52 typedef struct _GstWarpTVClass GstWarpTVClass;
53
54 struct _GstWarpTV
55 {
56   GstElement element;
57
58   GstPad *sinkpad, *srcpad;
59
60   gint width, height;
61   gint *offstable;
62   gint32 *disttable;
63   gint32 ctable[1024];
64   gint32 sintable[1024+256];
65   gint tval;
66 };
67
68 struct _GstWarpTVClass
69 {
70   GstElementClass parent_class;
71 };
72
73 GstElementDetails gst_warptv_details = {
74   "WarpTV",
75   "Filter/Video/Effect",
76   "WarpTV does realtime goo'ing of the video input",
77   VERSION,
78   "Sam Lantinga <slouken@devolution.com>",
79   "Wim Taymans <wim.taymans@chello.be>, "
80   "(C) 2001 FUKUCHI Kentarou",
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_class_init           (GstWarpTVClass * klass);
97 static void     gst_warptv_init                 (GstWarpTV * filter);
98
99 static void     gst_warptv_initialize           (GstWarpTV *filter);
100
101 static void     gst_warptv_set_property         (GObject * object, guint prop_id,
102                                                  const GValue * value, GParamSpec * pspec);
103 static void     gst_warptv_get_property         (GObject * object, guint prop_id,
104                                                  GValue * value, GParamSpec * pspec);
105
106 static void     gst_warptv_chain                (GstPad * pad, GstBuffer * buf);
107
108 static GstElementClass *parent_class = NULL;
109 /*static guint gst_warptv_signals[LAST_SIGNAL] = { 0 }; */
110
111 GType gst_warptv_get_type (void)
112 {
113   static GType warptv_type = 0;
114
115   if (!warptv_type) {
116     static const GTypeInfo warptv_info = {
117       sizeof (GstWarpTVClass), NULL,
118       NULL,
119       (GClassInitFunc) gst_warptv_class_init,
120       NULL,
121       NULL,
122       sizeof (GstWarpTV),
123       0,
124       (GInstanceInitFunc) gst_warptv_init,
125     };
126
127     warptv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstWarpTV", &warptv_info, 0);
128   }
129   return warptv_type;
130 }
131
132 static void
133 gst_warptv_class_init (GstWarpTVClass * klass)
134 {
135   GObjectClass *gobject_class;
136   GstElementClass *gstelement_class;
137
138   gobject_class = (GObjectClass *) klass;
139   gstelement_class = (GstElementClass *) klass;
140
141   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
142
143   gobject_class->set_property = gst_warptv_set_property;
144   gobject_class->get_property = gst_warptv_get_property;
145 }
146
147 static GstPadConnectReturn
148 gst_warptv_sinkconnect (GstPad * pad, GstCaps * caps)
149 {
150   GstWarpTV *filter;
151
152   filter = GST_WARPTV (gst_pad_get_parent (pad));
153
154   if (!GST_CAPS_IS_FIXED (caps))
155     return GST_PAD_CONNECT_DELAYED;
156
157   gst_caps_get_int (caps, "width", &filter->width);
158   gst_caps_get_int (caps, "height", &filter->height);
159
160   gst_warptv_initialize (filter);
161
162   if (gst_pad_try_set_caps (filter->srcpad, caps)) {
163     return GST_PAD_CONNECT_OK;
164   }
165
166   return GST_PAD_CONNECT_REFUSED;
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_connect_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, GstBuffer * buf)
253 {
254   GstWarpTV *filter;
255   guint32 *src, *dest;
256   gint xw,yw,cw;
257   GstBuffer *outbuf;
258   gint32 c,i, x,y, dx,dy, maxx, maxy;
259   gint32 width, height, skip, *ctptr, *distptr;
260   gint32 *sintable, *ctable;
261
262   filter = GST_WARPTV (gst_pad_get_parent (pad));
263
264   src = (guint32 *) GST_BUFFER_DATA (buf);
265
266   outbuf = gst_buffer_new ();
267   GST_BUFFER_SIZE (outbuf) = (filter->width * filter->height * sizeof(guint32));
268   dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
269   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
270   
271   xw  = (gint) (sin ((filter->tval + 100) * M_PI / 128) * 30);
272   yw  = (gint) (sin ((filter->tval) * M_PI / 256) * -35);
273   cw  = (gint) (sin ((filter->tval - 70) * M_PI / 64) * 50);
274   xw += (gint) (sin ((filter->tval - 10) * M_PI / 512) * 40);
275   yw += (gint) (sin ((filter->tval + 30) * M_PI / 512) * 40);     
276
277   ctptr = filter->ctable;
278   distptr = filter->disttable;
279   width = filter->width;
280   height = filter->height;
281   sintable = filter->sintable;
282   ctable = filter->ctable;
283
284   skip = 0 ; /* video_width*sizeof(RGB32)/4 - video_width;; */
285   c = 0;
286
287   for (x = 0; x < 512; x++) {
288     i = (c >> 3) & 0x3FE;
289     *ctptr++ = ((sintable[i] * yw) >> 15);
290     *ctptr++ = ((sintable[i + 256] * xw) >> 15);
291     c += cw;
292   }
293   maxx = width - 2; maxy = height - 2;
294
295   for (y = 0; y < height - 1; y++) {
296     for (x = 0; x < width; x++) {
297       i = *distptr++; 
298       dx = ctable [i + 1] + x; 
299       dy = ctable [i] + y;       
300
301       if (dx < 0) dx = 0; 
302       else if (dx > maxx) dx = maxx; 
303    
304       if (dy < 0) dy = 0; 
305       else if (dy > maxy) dy = maxy; 
306       *dest++ = src[filter->offstable[dy] + dx]; 
307     }
308     dest += skip;
309   }
310
311   filter->tval = (filter->tval + 1) & 511;
312
313   gst_buffer_unref (buf);
314
315   gst_pad_push (filter->srcpad, outbuf);
316 }
317
318 static void
319 gst_warptv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
320 {
321   GstWarpTV *filter;
322
323   /* it's not null if we got it, but it might not be ours */
324   g_return_if_fail (GST_IS_WARPTV (object));
325
326   filter = GST_WARPTV (object);
327
328   switch (prop_id) {
329     default:
330       break;
331   }
332 }
333
334 static void
335 gst_warptv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
336 {
337   GstWarpTV *filter;
338
339   /* it's not null if we got it, but it might not be ours */
340   g_return_if_fail (GST_IS_WARPTV (object));
341
342   filter = GST_WARPTV (object);
343
344   switch (prop_id) {
345     default:
346       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
347       break;
348   }
349 }