first batch : remove ',' at end of enums as they could confuse older gcc, foreign...
[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 {
67   GstVideofilter videofilter;
68
69   gint width, height;
70   gint *offstable;
71   gint32 *disttable;
72   gint32 ctable[1024];
73   gint32 sintable[1024 + 256];
74   gint tval;
75 };
76
77 struct _GstWarpTVClass
78 {
79   GstVideofilterClass parent_class;
80 };
81
82
83 /* GstWarpTV signals and args */
84 enum
85 {
86   /* FILL ME */
87   LAST_SIGNAL
88 };
89
90 enum
91 {
92   ARG_0
93       /* FILL ME */
94 };
95
96 static void gst_warptv_base_init (gpointer g_class);
97 static void gst_warptv_class_init (gpointer g_class, gpointer class_data);
98 static void gst_warptv_init (GTypeInstance * instance, gpointer g_class);
99
100 static void gst_warptv_set_property (GObject * object, guint prop_id,
101     const GValue * value, GParamSpec * pspec);
102 static void gst_warptv_get_property (GObject * object, guint prop_id,
103     GValue * value, GParamSpec * pspec);
104
105 static void gst_warptv_setup (GstVideofilter * videofilter);
106 static void initSinTable (GstWarpTV * filter);
107 static void initOffsTable (GstWarpTV * filter);
108 static void initDistTable (GstWarpTV * filter);
109 static void gst_warptv_rgb32 (GstVideofilter * videofilter, void *d, void *s);
110
111 GType
112 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       gst_warptv_class_init,
122       NULL,
123       NULL,
124       sizeof (GstWarpTV),
125       0,
126       gst_warptv_init,
127     };
128
129     warptv_type = g_type_register_static (GST_TYPE_VIDEOFILTER,
130         "GstWarpTV", &warptv_info, 0);
131   }
132   return warptv_type;
133 }
134
135 static GstVideofilterFormat gst_warptv_formats[] = {
136   {"RGB ", 32, gst_warptv_rgb32, 24, G_BIG_ENDIAN, 0x00ff0000, 0x0000ff00,
137       0x000000ff},
138   {"RGB ", 32, gst_warptv_rgb32, 24, G_BIG_ENDIAN, 0xff000000, 0x00ff0000,
139       0x0000ff00},
140   {"RGB ", 32, gst_warptv_rgb32, 24, G_BIG_ENDIAN, 0x000000ff, 0x0000ff00,
141       0x00ff0000},
142   {"RGB ", 32, gst_warptv_rgb32, 24, G_BIG_ENDIAN, 0x0000ff00, 0x00ff0000,
143       0xff000000},
144 };
145
146 static void
147 gst_warptv_base_init (gpointer g_class)
148 {
149   static GstElementDetails warptv_details = GST_ELEMENT_DETAILS ("WarpTV",
150       "Filter/Effect/Video",
151       "WarpTV does realtime goo'ing of the video input",
152       "Sam Lantinga <slouken@devolution.com>");
153   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
154   GstVideofilterClass *videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
155   int i;
156
157   gst_element_class_set_details (element_class, &warptv_details);
158
159   for (i = 0; i < G_N_ELEMENTS (gst_warptv_formats); i++) {
160     gst_videofilter_class_add_format (videofilter_class,
161         gst_warptv_formats + i);
162   }
163
164   gst_videofilter_class_add_pad_templates (GST_VIDEOFILTER_CLASS (g_class));
165 }
166
167 static void
168 gst_warptv_class_init (gpointer g_class, gpointer class_data)
169 {
170   GObjectClass *gobject_class;
171   GstVideofilterClass *videofilter_class;
172
173   gobject_class = G_OBJECT_CLASS (g_class);
174   videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
175
176 #if 0
177   g_object_class_install_property (gobject_class, ARG_METHOD,
178       g_param_spec_enum ("method", "method", "method",
179           GST_TYPE_WARPTV_METHOD, GST_WARPTV_METHOD_1, G_PARAM_READWRITE));
180 #endif
181
182   gobject_class->set_property = gst_warptv_set_property;
183   gobject_class->get_property = gst_warptv_get_property;
184
185   videofilter_class->setup = gst_warptv_setup;
186 }
187
188 static void
189 gst_warptv_init (GTypeInstance * instance, gpointer g_class)
190 {
191   GstWarpTV *warptv = GST_WARPTV (instance);
192   GstVideofilter *videofilter;
193
194   GST_DEBUG ("gst_warptv_init");
195
196   videofilter = GST_VIDEOFILTER (warptv);
197
198   /* do stuff */
199 }
200
201 static void
202 gst_warptv_set_property (GObject * object, guint prop_id, const GValue * value,
203     GParamSpec * pspec)
204 {
205   GstWarpTV *src;
206
207   /* it's not null if we got it, but it might not be ours */
208   g_return_if_fail (GST_IS_WARPTV (object));
209   src = GST_WARPTV (object);
210
211   GST_DEBUG ("gst_warptv_set_property");
212   switch (prop_id) {
213 #if 0
214     case ARG_METHOD:
215       src->method = g_value_get_enum (value);
216       break;
217 #endif
218     default:
219       break;
220   }
221 }
222
223 static void
224 gst_warptv_get_property (GObject * object, guint prop_id, GValue * value,
225     GParamSpec * pspec)
226 {
227   GstWarpTV *src;
228
229   /* it's not null if we got it, but it might not be ours */
230   g_return_if_fail (GST_IS_WARPTV (object));
231   src = GST_WARPTV (object);
232
233   switch (prop_id) {
234 #if 0
235     case ARG_METHOD:
236       g_value_set_enum (value, src->method);
237       break;
238 #endif
239     default:
240       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241       break;
242   }
243 }
244
245
246 static void
247 gst_warptv_setup (GstVideofilter * videofilter)
248 {
249   GstWarpTV *warptv;
250   int width = gst_videofilter_get_input_width (videofilter);
251   int height = gst_videofilter_get_input_height (videofilter);
252
253   g_return_if_fail (GST_IS_WARPTV (videofilter));
254   warptv = GST_WARPTV (videofilter);
255
256   /* if any setup needs to be done, do it here */
257
258   warptv->width = width;
259   warptv->height = height;
260 #if 0
261   /* FIXME this should be reset in PAUSE->READY, not here */
262   warptv->tval = 0;
263 #endif
264
265   g_free (warptv->disttable);
266   g_free (warptv->offstable);
267
268   warptv->offstable = (guint32 *) g_malloc (height * sizeof (guint32));
269   warptv->disttable = g_malloc (width * height * sizeof (guint32));
270
271   initSinTable (warptv);
272   initOffsTable (warptv);
273   initDistTable (warptv);
274 }
275
276 static void
277 initSinTable (GstWarpTV * filter)
278 {
279   gint32 *tptr, *tsinptr;
280   double i;
281
282   tsinptr = tptr = filter->sintable;
283
284   for (i = 0; i < 1024; i++)
285     *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
286
287   for (i = 0; i < 256; i++)
288     *tptr++ = *tsinptr++;
289 }
290
291 static void
292 initOffsTable (GstWarpTV * filter)
293 {
294   int y;
295
296   for (y = 0; y < filter->height; y++) {
297     filter->offstable[y] = y * filter->width;
298   }
299 }
300
301 static void
302 initDistTable (GstWarpTV * filter)
303 {
304   gint32 halfw, halfh, *distptr;
305
306 #ifdef PS2
307   float x, y, m;
308 #else
309   double x, y, m;
310 #endif
311
312   halfw = filter->width >> 1;
313   halfh = filter->height >> 1;
314
315   distptr = filter->disttable;
316
317   m = sqrt ((double) (halfw * halfw + halfh * halfh));
318
319   for (y = -halfh; y < halfh; y++)
320     for (x = -halfw; x < halfw; x++)
321 #ifdef PS2
322       *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
323 #else
324       *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
325 #endif
326 }
327
328 static void
329 gst_warptv_rgb32 (GstVideofilter * videofilter, void *d, void *s)
330 {
331   GstWarpTV *warptv;
332   int width = gst_videofilter_get_input_width (videofilter);
333   int height = gst_videofilter_get_input_height (videofilter);
334   guint32 *src = s;
335   guint32 *dest = d;
336   gint xw, yw, cw;
337   gint32 c, i, x, y, dx, dy, maxx, maxy;
338   gint32 skip, *ctptr, *distptr;
339   gint32 *sintable, *ctable;
340
341   g_return_if_fail (GST_IS_WARPTV (videofilter));
342   warptv = GST_WARPTV (videofilter);
343
344   xw = (gint) (sin ((warptv->tval + 100) * M_PI / 128) * 30);
345   yw = (gint) (sin ((warptv->tval) * M_PI / 256) * -35);
346   cw = (gint) (sin ((warptv->tval - 70) * M_PI / 64) * 50);
347   xw += (gint) (sin ((warptv->tval - 10) * M_PI / 512) * 40);
348   yw += (gint) (sin ((warptv->tval + 30) * M_PI / 512) * 40);
349
350   ctptr = warptv->ctable;
351   distptr = warptv->disttable;
352   sintable = warptv->sintable;
353   ctable = warptv->ctable;
354
355   skip = 0;                     /* video_width*sizeof(RGB32)/4 - video_width;; */
356   c = 0;
357
358   for (x = 0; x < 512; x++) {
359     i = (c >> 3) & 0x3FE;
360     *ctptr++ = ((sintable[i] * yw) >> 15);
361     *ctptr++ = ((sintable[i + 256] * xw) >> 15);
362     c += cw;
363   }
364   maxx = width - 2;
365   maxy = height - 2;
366
367   for (y = 0; y < height - 1; y++) {
368     for (x = 0; x < width; x++) {
369       i = *distptr++;
370       dx = ctable[i + 1] + x;
371       dy = ctable[i] + y;
372
373       if (dx < 0)
374         dx = 0;
375       else if (dx > maxx)
376         dx = maxx;
377
378       if (dy < 0)
379         dy = 0;
380       else if (dy > maxy)
381         dy = maxy;
382       *dest++ = src[warptv->offstable[dy] + dx];
383     }
384     dest += skip;
385   }
386
387   warptv->tval = (warptv->tval + 1) & 511;
388 }