Port all elements that can be ported to videofilter, and fix up the caps.
[platform/upstream/gstreamer.git] / gst / effectv / gstwarp.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2003> David Schleef <ds@schleef.org>
4  *
5  * EffecTV - Realtime Digital Video Effector
6  * Copyright (C) 2001 FUKUCHI Kentarou
7  *
8  * This library is free software; 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
24 /*
25  * This file was (probably) generated from gstvideotemplate.c,
26  * gstvideotemplate.c,v 1.11 2004/01/07 08:56:45 ds Exp 
27  */
28
29 /* From main.c of warp-1.1:
30  *
31  *      Simple DirectMedia Layer demo
32  *      Realtime picture 'gooing'
33  *      by sam lantinga slouken@devolution.com
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <gst/gst.h>
41 #include <gstvideofilter.h>
42 #include <string.h>
43 #include <math.h>
44 #include "gsteffectv.h"
45
46 #ifndef M_PI
47 #define M_PI    3.14159265358979323846
48 #endif
49
50
51 #define GST_TYPE_WARPTV \
52   (gst_warptv_get_type())
53 #define GST_WARPTV(obj) \
54   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_WARPTV,GstWarpTV))
55 #define GST_WARPTV_CLASS(klass) \
56   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_WARPTV,GstWarpTVClass))
57 #define GST_IS_WARPTV(obj) \
58   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_WARPTV))
59 #define GST_IS_WARPTV_CLASS(obj) \
60   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_WARPTV))
61
62 typedef struct _GstWarpTV GstWarpTV;
63 typedef struct _GstWarpTVClass GstWarpTVClass;
64
65 struct _GstWarpTV {
66   GstVideofilter videofilter;
67
68   gint width, height;
69   gint *offstable;
70   gint32 *disttable;
71   gint32 ctable[1024];
72   gint32 sintable[1024+256];
73   gint tval;
74 };
75
76 struct _GstWarpTVClass {
77   GstVideofilterClass parent_class;
78 };
79
80
81 /* GstWarpTV signals and args */
82 enum {
83   /* FILL ME */
84   LAST_SIGNAL
85 };
86
87 enum {
88   ARG_0,
89   /* FILL ME */
90 };
91
92 static void     gst_warptv_base_init    (gpointer g_class);
93 static void     gst_warptv_class_init   (gpointer g_class, gpointer class_data);
94 static void     gst_warptv_init         (GTypeInstance *instance, gpointer g_class);
95
96 static void     gst_warptv_set_property         (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
97 static void     gst_warptv_get_property         (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
98
99 static void gst_warptv_setup(GstVideofilter *videofilter);
100 static void initSinTable (GstWarpTV *filter);
101 static void initOffsTable (GstWarpTV *filter);
102 static void initDistTable (GstWarpTV *filter);
103 static void gst_warptv_rgb32 (GstVideofilter *videofilter, void *d, void *s);
104
105 GType
106 gst_warptv_get_type (void)
107 {
108   static GType warptv_type = 0;
109
110   if (!warptv_type) {
111     static const GTypeInfo warptv_info = {
112       sizeof(GstWarpTVClass),
113       gst_warptv_base_init,
114       NULL,
115       gst_warptv_class_init,
116       NULL,
117       NULL,
118       sizeof(GstWarpTV),
119       0,
120       gst_warptv_init,
121     };
122     warptv_type = g_type_register_static(GST_TYPE_VIDEOFILTER,
123         "GstWarpTV", &warptv_info, 0);
124   }
125   return warptv_type;
126 }
127
128 static GstVideofilterFormat gst_warptv_formats[] = {
129   { "RGB ", 32, gst_warptv_rgb32, 24, G_BIG_ENDIAN, 0x00ff0000, 0x0000ff00, 0x000000ff },
130   { "RGB ", 32, gst_warptv_rgb32, 24, G_BIG_ENDIAN, 0xff000000, 0x00ff0000, 0x0000ff00 },
131   { "RGB ", 32, gst_warptv_rgb32, 24, G_BIG_ENDIAN, 0x000000ff, 0x0000ff00, 0x00ff0000 },
132   { "RGB ", 32, gst_warptv_rgb32, 24, G_BIG_ENDIAN, 0x0000ff00, 0x00ff0000, 0xff000000 },
133 };
134
135 static void
136 gst_warptv_base_init (gpointer g_class)
137 {
138   static GstElementDetails warptv_details = GST_ELEMENT_DETAILS (
139     "WarpTV",
140     "Filter/Effect/Video",
141     "WarpTV does realtime goo'ing of the video input",
142     "Sam Lantinga <slouken@devolution.com>"
143   );
144   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
145   GstVideofilterClass *videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
146   int i;
147   
148   gst_element_class_set_details (element_class, &warptv_details);
149
150   for(i=0;i<G_N_ELEMENTS(gst_warptv_formats);i++){
151     gst_videofilter_class_add_format(videofilter_class,
152         gst_warptv_formats + i);
153   }
154
155   gst_videofilter_class_add_pad_templates (GST_VIDEOFILTER_CLASS (g_class));
156 }
157
158 static void
159 gst_warptv_class_init (gpointer g_class, gpointer class_data)
160 {
161   GObjectClass *gobject_class;
162   GstVideofilterClass *videofilter_class;
163
164   gobject_class = G_OBJECT_CLASS (g_class);
165   videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
166
167 #if 0
168   g_object_class_install_property(gobject_class, ARG_METHOD,
169       g_param_spec_enum("method","method","method",
170       GST_TYPE_WARPTV_METHOD, GST_WARPTV_METHOD_1,
171       G_PARAM_READWRITE));
172 #endif
173
174   gobject_class->set_property = gst_warptv_set_property;
175   gobject_class->get_property = gst_warptv_get_property;
176
177   videofilter_class->setup = gst_warptv_setup;
178 }
179
180 static void
181 gst_warptv_init (GTypeInstance *instance, gpointer g_class)
182 {
183   GstWarpTV *warptv = GST_WARPTV (instance);
184   GstVideofilter *videofilter;
185
186   GST_DEBUG("gst_warptv_init");
187
188   videofilter = GST_VIDEOFILTER(warptv);
189
190   /* do stuff */
191 }
192
193 static void
194 gst_warptv_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
195 {
196   GstWarpTV *src;
197
198   /* it's not null if we got it, but it might not be ours */
199   g_return_if_fail(GST_IS_WARPTV(object));
200   src = GST_WARPTV(object);
201
202   GST_DEBUG("gst_warptv_set_property");
203   switch (prop_id) {
204 #if 0
205     case ARG_METHOD:
206       src->method = g_value_get_enum (value);
207       break;
208 #endif
209     default:
210       break;
211   }
212 }
213
214 static void
215 gst_warptv_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
216 {
217   GstWarpTV *src;
218
219   /* it's not null if we got it, but it might not be ours */
220   g_return_if_fail(GST_IS_WARPTV(object));
221   src = GST_WARPTV(object);
222
223   switch (prop_id) {
224 #if 0
225     case ARG_METHOD:
226       g_value_set_enum (value, src->method);
227       break;
228 #endif
229     default:
230       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231       break;
232   }
233 }
234
235
236 static void gst_warptv_setup(GstVideofilter *videofilter)
237 {
238   GstWarpTV *warptv;
239   int width = gst_videofilter_get_input_width(videofilter);
240   int height = gst_videofilter_get_input_height(videofilter);
241
242   g_return_if_fail(GST_IS_WARPTV(videofilter));
243   warptv = GST_WARPTV(videofilter);
244
245   /* if any setup needs to be done, do it here */
246
247   warptv->width = width;
248   warptv->height = height;
249 #if 0
250   /* FIXME this should be reset in PAUSE->READY, not here */
251   warptv->tval = 0;
252 #endif
253
254   g_free (warptv->disttable);
255   g_free (warptv->offstable);
256
257   warptv->offstable = (guint32 *) g_malloc (height * sizeof (guint32));      
258   warptv->disttable = g_malloc (width * height * sizeof (guint32));
259
260   initSinTable (warptv);
261   initOffsTable (warptv);
262   initDistTable (warptv);
263 }
264
265 static void 
266 initSinTable (GstWarpTV *filter) 
267 {
268   gint32        *tptr, *tsinptr;
269   double        i;
270
271   tsinptr = tptr = filter->sintable;
272
273   for (i = 0; i < 1024; i++)
274     *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
275
276   for (i = 0; i < 256; i++)
277     *tptr++ = *tsinptr++;
278 }
279
280 static void 
281 initOffsTable (GstWarpTV *filter) 
282 {
283   int y;
284         
285   for (y = 0; y < filter->height; y++) {
286     filter->offstable[y] = y * filter->width;
287   }
288 }
289       
290 static void 
291 initDistTable (GstWarpTV *filter) 
292 {
293   gint32 halfw, halfh, *distptr;
294 #ifdef PS2
295   float x,y,m;
296 #else
297   double x,y,m;
298 #endif
299
300   halfw = filter->width>> 1;
301   halfh = filter->height >> 1;
302
303   distptr = filter->disttable;
304
305   m = sqrt ((double)(halfw * halfw + halfh * halfh));
306
307   for (y = -halfh; y < halfh; y++)
308     for (x= -halfw; x < halfw; x++)
309 #ifdef PS2
310       *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
311 #else
312       *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
313 #endif
314 }
315
316 static void gst_warptv_rgb32 (GstVideofilter *videofilter,
317     void *d, void *s)
318 {
319   GstWarpTV *warptv;
320   int width = gst_videofilter_get_input_width(videofilter);
321   int height = gst_videofilter_get_input_height(videofilter);
322   guint32 *src = s;
323   guint32 *dest = d;
324   gint xw,yw,cw;
325   gint32 c,i, x,y, dx,dy, maxx, maxy;
326   gint32 skip, *ctptr, *distptr;
327   gint32 *sintable, *ctable;
328
329   g_return_if_fail(GST_IS_WARPTV(videofilter));
330   warptv = GST_WARPTV(videofilter);
331
332   xw  = (gint) (sin ((warptv->tval + 100) * M_PI / 128) * 30);
333   yw  = (gint) (sin ((warptv->tval) * M_PI / 256) * -35);
334   cw  = (gint) (sin ((warptv->tval - 70) * M_PI / 64) * 50);
335   xw += (gint) (sin ((warptv->tval - 10) * M_PI / 512) * 40);
336   yw += (gint) (sin ((warptv->tval + 30) * M_PI / 512) * 40);     
337
338   ctptr = warptv->ctable;
339   distptr = warptv->disttable;
340   sintable = warptv->sintable;
341   ctable = warptv->ctable;
342
343   skip = 0 ; /* video_width*sizeof(RGB32)/4 - video_width;; */
344   c = 0;
345
346   for (x = 0; x < 512; x++) {
347     i = (c >> 3) & 0x3FE;
348     *ctptr++ = ((sintable[i] * yw) >> 15);
349     *ctptr++ = ((sintable[i + 256] * xw) >> 15);
350     c += cw;
351   }
352   maxx = width - 2; maxy = height - 2;
353
354   for (y = 0; y < height - 1; y++) {
355     for (x = 0; x < width; x++) {
356       i = *distptr++; 
357       dx = ctable [i + 1] + x; 
358       dy = ctable [i] + y;       
359
360       if (dx < 0) dx = 0; 
361       else if (dx > maxx) dx = maxx; 
362    
363       if (dy < 0) dy = 0; 
364       else if (dy > maxy) dy = maxy; 
365       *dest++ = src[warptv->offstable[dy] + dx]; 
366     }
367     dest += skip;
368   }
369
370   warptv->tval = (warptv->tval + 1) & 511;
371 }
372