Merge CAPS branch
[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/Effect/Video",
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,
140       gst_static_pad_template_get (&gst_effectv_src_template));
141   gst_element_class_add_pad_template (element_class,
142       gst_static_pad_template_get (&gst_effectv_sink_template));
143  
144   gst_element_class_set_details (element_class, &gst_warptv_details);
145 }
146
147 static void
148 gst_warptv_class_init (GstWarpTVClass * klass)
149 {
150   GObjectClass *gobject_class;
151   GstElementClass *gstelement_class;
152
153   gobject_class = (GObjectClass *) klass;
154   gstelement_class = (GstElementClass *) klass;
155
156   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
157
158   gobject_class->set_property = gst_warptv_set_property;
159   gobject_class->get_property = gst_warptv_get_property;
160 }
161
162 static GstPadLinkReturn
163 gst_warptv_sinkconnect (GstPad * pad, const GstCaps * caps)
164 {
165   GstWarpTV *filter;
166   GstStructure *structure;
167
168   filter = GST_WARPTV (gst_pad_get_parent (pad));
169   structure = gst_caps_get_structure (caps, 0);
170
171   gst_structure_get_int  (structure, "width", &filter->width);
172   gst_structure_get_int  (structure, "height", &filter->height);
173
174   gst_warptv_initialize (filter);
175
176   return gst_pad_try_set_caps (filter->srcpad, caps);
177 }
178
179 static void
180 gst_warptv_init (GstWarpTV * filter)
181 {
182   filter->sinkpad = gst_pad_new_from_template (
183       gst_static_pad_template_get (&gst_effectv_sink_template), "sink");
184   gst_pad_set_chain_function (filter->sinkpad, gst_warptv_chain);
185   gst_pad_set_link_function (filter->sinkpad, gst_warptv_sinkconnect);
186   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
187
188   filter->srcpad = gst_pad_new_from_template (
189       gst_static_pad_template_get (&gst_effectv_src_template), "src");
190   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
191
192   filter->tval = 0;
193   filter->disttable = NULL;
194   filter->offstable = NULL;
195 }
196
197
198 static void 
199 initSinTable (GstWarpTV *filter) 
200 {
201   gint32        *tptr, *tsinptr;
202   double        i;
203
204   tsinptr = tptr = filter->sintable;
205
206   for (i = 0; i < 1024; i++)
207     *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
208
209   for (i = 0; i < 256; i++)
210     *tptr++ = *tsinptr++;
211 }
212
213 static void 
214 initOffsTable (GstWarpTV *filter) 
215 {
216   int y;
217         
218   for (y = 0; y < filter->height; y++) {
219     filter->offstable[y] = y * filter->width;
220   }
221 }
222       
223 static void 
224 initDistTable (GstWarpTV *filter) 
225 {
226   gint32 halfw, halfh, *distptr;
227 #ifdef PS2
228   float x,y,m;
229 #else
230   double x,y,m;
231 #endif
232
233   halfw = filter->width>> 1;
234   halfh = filter->height >> 1;
235
236   distptr = filter->disttable;
237
238   m = sqrt ((double)(halfw * halfw + halfh * halfh));
239
240   for (y = -halfh; y < halfh; y++)
241     for (x= -halfw; x < halfw; x++)
242 #ifdef PS2
243       *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
244 #else
245       *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
246 #endif
247 }
248
249 static void 
250 gst_warptv_initialize (GstWarpTV *filter) 
251 {
252   g_free (filter->disttable);
253   g_free (filter->offstable);
254
255   filter->offstable = (guint32 *) g_malloc (filter->height * sizeof (guint32));      
256   filter->disttable = g_malloc (filter->width * filter->height * sizeof (guint32));
257
258   initSinTable (filter);
259   initOffsTable (filter);
260   initDistTable (filter);
261 }
262
263 static void
264 gst_warptv_chain (GstPad * pad, GstData *_data)
265 {
266   GstBuffer *buf = GST_BUFFER (_data);
267   GstWarpTV *filter;
268   guint32 *src, *dest;
269   gint xw,yw,cw;
270   GstBuffer *outbuf;
271   gint32 c,i, x,y, dx,dy, maxx, maxy;
272   gint32 width, height, skip, *ctptr, *distptr;
273   gint32 *sintable, *ctable;
274
275   filter = GST_WARPTV (gst_pad_get_parent (pad));
276
277   src = (guint32 *) GST_BUFFER_DATA (buf);
278
279   outbuf = gst_buffer_new ();
280   GST_BUFFER_SIZE (outbuf) = (filter->width * filter->height * sizeof(guint32));
281   dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
282   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
283   
284   xw  = (gint) (sin ((filter->tval + 100) * M_PI / 128) * 30);
285   yw  = (gint) (sin ((filter->tval) * M_PI / 256) * -35);
286   cw  = (gint) (sin ((filter->tval - 70) * M_PI / 64) * 50);
287   xw += (gint) (sin ((filter->tval - 10) * M_PI / 512) * 40);
288   yw += (gint) (sin ((filter->tval + 30) * M_PI / 512) * 40);     
289
290   ctptr = filter->ctable;
291   distptr = filter->disttable;
292   width = filter->width;
293   height = filter->height;
294   sintable = filter->sintable;
295   ctable = filter->ctable;
296
297   skip = 0 ; /* video_width*sizeof(RGB32)/4 - video_width;; */
298   c = 0;
299
300   for (x = 0; x < 512; x++) {
301     i = (c >> 3) & 0x3FE;
302     *ctptr++ = ((sintable[i] * yw) >> 15);
303     *ctptr++ = ((sintable[i + 256] * xw) >> 15);
304     c += cw;
305   }
306   maxx = width - 2; maxy = height - 2;
307
308   for (y = 0; y < height - 1; y++) {
309     for (x = 0; x < width; x++) {
310       i = *distptr++; 
311       dx = ctable [i + 1] + x; 
312       dy = ctable [i] + y;       
313
314       if (dx < 0) dx = 0; 
315       else if (dx > maxx) dx = maxx; 
316    
317       if (dy < 0) dy = 0; 
318       else if (dy > maxy) dy = maxy; 
319       *dest++ = src[filter->offstable[dy] + dx]; 
320     }
321     dest += skip;
322   }
323
324   filter->tval = (filter->tval + 1) & 511;
325
326   gst_buffer_unref (buf);
327
328   gst_pad_push (filter->srcpad, GST_DATA (outbuf));
329 }
330
331 static void
332 gst_warptv_set_property (GObject * object, guint prop_id, const 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       break;
344   }
345 }
346
347 static void
348 gst_warptv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
349 {
350   GstWarpTV *filter;
351
352   /* it's not null if we got it, but it might not be ours */
353   g_return_if_fail (GST_IS_WARPTV (object));
354
355   filter = GST_WARPTV (object);
356
357   switch (prop_id) {
358     default:
359       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
360       break;
361   }
362 }