plugins part of license field patch
[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 #include <string.h>
31 #include <math.h>
32 #include <gst/gst.h>
33 #include "gsteffectv.h"
34
35 #ifndef M_PI
36 #define M_PI    3.14159265358979323846
37 #endif
38
39 #define GST_TYPE_WARPTV \
40   (gst_warptv_get_type())
41 #define GST_WARPTV(obj) \
42   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_WARPTV,GstWarpTV))
43 #define GST_WARPTV_CLASS(klass) \
44   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstWarpTV))
45 #define GST_IS_WARPTV(obj) \
46   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_WARPTV))
47 #define GST_IS_WARPTV_CLASS(obj) \
48   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_WARPTV))
49
50 typedef struct _GstWarpTV GstWarpTV;
51 typedef struct _GstWarpTVClass GstWarpTVClass;
52
53 struct _GstWarpTV
54 {
55   GstElement element;
56
57   GstPad *sinkpad, *srcpad;
58
59   gint width, height;
60   gint *offstable;
61   gint32 *disttable;
62   gint32 ctable[1024];
63   gint32 sintable[1024+256];
64   gint tval;
65 };
66
67 struct _GstWarpTVClass
68 {
69   GstElementClass parent_class;
70 };
71
72 /* elementfactory information */
73 GstElementDetails gst_warptv_details = {
74   "WarpTV",
75   "Filter/Video/Effect",
76   "LGPL",
77   "WarpTV does realtime goo'ing of the video input",
78   VERSION,
79   "Sam Lantinga <slouken@devolution.com>",
80   "Wim Taymans <wim.taymans@chello.be>, "
81   "(C) 2001 FUKUCHI Kentarou",
82 };
83
84
85 /* Filter signals and args */
86 enum
87 {
88   /* FILL ME */
89   LAST_SIGNAL
90 };
91
92 enum
93 {
94   ARG_0,
95 };
96
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, GstBuffer * buf);
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), NULL,
119       NULL,
120       (GClassInitFunc) gst_warptv_class_init,
121       NULL,
122       NULL,
123       sizeof (GstWarpTV),
124       0,
125       (GInstanceInitFunc) gst_warptv_init,
126     };
127
128     warptv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstWarpTV", &warptv_info, 0);
129   }
130   return warptv_type;
131 }
132
133 static void
134 gst_warptv_class_init (GstWarpTVClass * klass)
135 {
136   GObjectClass *gobject_class;
137   GstElementClass *gstelement_class;
138
139   gobject_class = (GObjectClass *) klass;
140   gstelement_class = (GstElementClass *) klass;
141
142   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
143
144   gobject_class->set_property = gst_warptv_set_property;
145   gobject_class->get_property = gst_warptv_get_property;
146 }
147
148 static GstPadConnectReturn
149 gst_warptv_sinkconnect (GstPad * pad, GstCaps * caps)
150 {
151   GstWarpTV *filter;
152
153   filter = GST_WARPTV (gst_pad_get_parent (pad));
154
155   if (!GST_CAPS_IS_FIXED (caps))
156     return GST_PAD_CONNECT_DELAYED;
157
158   gst_caps_get_int (caps, "width", &filter->width);
159   gst_caps_get_int (caps, "height", &filter->height);
160
161   gst_warptv_initialize (filter);
162
163   return gst_pad_try_set_caps (filter->srcpad, caps);
164 }
165
166 static void
167 gst_warptv_init (GstWarpTV * filter)
168 {
169   filter->sinkpad = gst_pad_new_from_template (gst_effectv_sink_factory (), "sink");
170   gst_pad_set_chain_function (filter->sinkpad, gst_warptv_chain);
171   gst_pad_set_connect_function (filter->sinkpad, gst_warptv_sinkconnect);
172   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
173
174   filter->srcpad = gst_pad_new_from_template (gst_effectv_src_factory (), "src");
175   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
176
177   filter->tval = 0;
178   filter->disttable = NULL;
179   filter->offstable = NULL;
180 }
181
182
183 static void 
184 initSinTable (GstWarpTV *filter) 
185 {
186   gint32        *tptr, *tsinptr;
187   double        i;
188
189   tsinptr = tptr = filter->sintable;
190
191   for (i = 0; i < 1024; i++)
192     *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
193
194   for (i = 0; i < 256; i++)
195     *tptr++ = *tsinptr++;
196 }
197
198 static void 
199 initOffsTable (GstWarpTV *filter) 
200 {
201   int y;
202         
203   for (y = 0; y < filter->height; y++) {
204     filter->offstable[y] = y * filter->width;
205   }
206 }
207       
208 static void 
209 initDistTable (GstWarpTV *filter) 
210 {
211   gint32 halfw, halfh, *distptr;
212 #ifdef PS2
213   float x,y,m;
214 #else
215   double x,y,m;
216 #endif
217
218   halfw = filter->width>> 1;
219   halfh = filter->height >> 1;
220
221   distptr = filter->disttable;
222
223   m = sqrt ((double)(halfw * halfw + halfh * halfh));
224
225   for (y = -halfh; y < halfh; y++)
226     for (x= -halfw; x < halfw; x++)
227 #ifdef PS2
228       *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
229 #else
230       *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
231 #endif
232 }
233
234 static void 
235 gst_warptv_initialize (GstWarpTV *filter) 
236 {
237   g_free (filter->disttable);
238   g_free (filter->offstable);
239
240   filter->offstable = (guint32 *) g_malloc (filter->height * sizeof (guint32));      
241   filter->disttable = g_malloc (filter->width * filter->height * sizeof (guint32));
242
243   initSinTable (filter);
244   initOffsTable (filter);
245   initDistTable (filter);
246 }
247
248 static void
249 gst_warptv_chain (GstPad * pad, GstBuffer * buf)
250 {
251   GstWarpTV *filter;
252   guint32 *src, *dest;
253   gint xw,yw,cw;
254   GstBuffer *outbuf;
255   gint32 c,i, x,y, dx,dy, maxx, maxy;
256   gint32 width, height, skip, *ctptr, *distptr;
257   gint32 *sintable, *ctable;
258
259   filter = GST_WARPTV (gst_pad_get_parent (pad));
260
261   src = (guint32 *) GST_BUFFER_DATA (buf);
262
263   outbuf = gst_buffer_new ();
264   GST_BUFFER_SIZE (outbuf) = (filter->width * filter->height * sizeof(guint32));
265   dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
266   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
267   
268   xw  = (gint) (sin ((filter->tval + 100) * M_PI / 128) * 30);
269   yw  = (gint) (sin ((filter->tval) * M_PI / 256) * -35);
270   cw  = (gint) (sin ((filter->tval - 70) * M_PI / 64) * 50);
271   xw += (gint) (sin ((filter->tval - 10) * M_PI / 512) * 40);
272   yw += (gint) (sin ((filter->tval + 30) * M_PI / 512) * 40);     
273
274   ctptr = filter->ctable;
275   distptr = filter->disttable;
276   width = filter->width;
277   height = filter->height;
278   sintable = filter->sintable;
279   ctable = filter->ctable;
280
281   skip = 0 ; /* video_width*sizeof(RGB32)/4 - video_width;; */
282   c = 0;
283
284   for (x = 0; x < 512; x++) {
285     i = (c >> 3) & 0x3FE;
286     *ctptr++ = ((sintable[i] * yw) >> 15);
287     *ctptr++ = ((sintable[i + 256] * xw) >> 15);
288     c += cw;
289   }
290   maxx = width - 2; maxy = height - 2;
291
292   for (y = 0; y < height - 1; y++) {
293     for (x = 0; x < width; x++) {
294       i = *distptr++; 
295       dx = ctable [i + 1] + x; 
296       dy = ctable [i] + y;       
297
298       if (dx < 0) dx = 0; 
299       else if (dx > maxx) dx = maxx; 
300    
301       if (dy < 0) dy = 0; 
302       else if (dy > maxy) dy = maxy; 
303       *dest++ = src[filter->offstable[dy] + dx]; 
304     }
305     dest += skip;
306   }
307
308   filter->tval = (filter->tval + 1) & 511;
309
310   gst_buffer_unref (buf);
311
312   gst_pad_push (filter->srcpad, outbuf);
313 }
314
315 static void
316 gst_warptv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
317 {
318   GstWarpTV *filter;
319
320   /* it's not null if we got it, but it might not be ours */
321   g_return_if_fail (GST_IS_WARPTV (object));
322
323   filter = GST_WARPTV (object);
324
325   switch (prop_id) {
326     default:
327       break;
328   }
329 }
330
331 static void
332 gst_warptv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
333 {
334   GstWarpTV *filter;
335
336   /* it's not null if we got it, but it might not be ours */
337   g_return_if_fail (GST_IS_WARPTV (object));
338
339   filter = GST_WARPTV (object);
340
341   switch (prop_id) {
342     default:
343       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344       break;
345   }
346 }