2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * EffecTV - Realtime Digital Video Effector
5 * Copyright (C) 2001 FUKUCHI Kentarou
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.
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.
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.
23 * From main.c of warp-1.1:
25 * Simple DirectMedia Layer demo
26 * Realtime picture 'gooing'
27 * by sam lantinga slouken@devolution.com
33 #include "gsteffectv.h"
36 #define M_PI 3.14159265358979323846
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))
50 typedef struct _GstWarpTV GstWarpTV;
51 typedef struct _GstWarpTVClass GstWarpTVClass;
57 GstPad *sinkpad, *srcpad;
63 gint32 sintable[1024+256];
67 struct _GstWarpTVClass
69 GstElementClass parent_class;
72 GstElementDetails gst_warptv_details = {
74 "Filter/Video/Effect",
75 "WarpTV does realtime goo'ing of the video input",
77 "Sam Lantinga <slouken@devolution.com>",
78 "Wim Taymans <wim.taymans@chello.be>, "
79 "(C) 2001 FUKUCHI Kentarou",
83 /* Filter signals and args */
95 static void gst_warptv_class_init (GstWarpTVClass * klass);
96 static void gst_warptv_init (GstWarpTV * filter);
98 static void gst_warptv_initialize (GstWarpTV *filter);
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);
105 static void gst_warptv_chain (GstPad * pad, GstBuffer * buf);
107 static GstElementClass *parent_class = NULL;
108 /*static guint gst_warptv_signals[LAST_SIGNAL] = { 0 }; */
110 GType gst_warptv_get_type (void)
112 static GType warptv_type = 0;
115 static const GTypeInfo warptv_info = {
116 sizeof (GstWarpTVClass), NULL,
118 (GClassInitFunc) gst_warptv_class_init,
123 (GInstanceInitFunc) gst_warptv_init,
126 warptv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstWarpTV", &warptv_info, 0);
132 gst_warptv_class_init (GstWarpTVClass * klass)
134 GObjectClass *gobject_class;
135 GstElementClass *gstelement_class;
137 gobject_class = (GObjectClass *) klass;
138 gstelement_class = (GstElementClass *) klass;
140 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
142 gobject_class->set_property = gst_warptv_set_property;
143 gobject_class->get_property = gst_warptv_get_property;
146 static GstPadConnectReturn
147 gst_warptv_sinkconnect (GstPad * pad, GstCaps * caps)
151 filter = GST_WARPTV (gst_pad_get_parent (pad));
153 if (!GST_CAPS_IS_FIXED (caps))
154 return GST_PAD_CONNECT_DELAYED;
156 gst_caps_get_int (caps, "width", &filter->width);
157 gst_caps_get_int (caps, "height", &filter->height);
159 gst_warptv_initialize (filter);
161 return gst_pad_try_set_caps (filter->srcpad, caps);
165 gst_warptv_init (GstWarpTV * filter)
167 filter->sinkpad = gst_pad_new_from_template (gst_effectv_sink_factory (), "sink");
168 gst_pad_set_chain_function (filter->sinkpad, gst_warptv_chain);
169 gst_pad_set_connect_function (filter->sinkpad, gst_warptv_sinkconnect);
170 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
172 filter->srcpad = gst_pad_new_from_template (gst_effectv_src_factory (), "src");
173 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
176 filter->disttable = NULL;
177 filter->offstable = NULL;
182 initSinTable (GstWarpTV *filter)
184 gint32 *tptr, *tsinptr;
187 tsinptr = tptr = filter->sintable;
189 for (i = 0; i < 1024; i++)
190 *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
192 for (i = 0; i < 256; i++)
193 *tptr++ = *tsinptr++;
197 initOffsTable (GstWarpTV *filter)
201 for (y = 0; y < filter->height; y++) {
202 filter->offstable[y] = y * filter->width;
207 initDistTable (GstWarpTV *filter)
209 gint32 halfw, halfh, *distptr;
216 halfw = filter->width>> 1;
217 halfh = filter->height >> 1;
219 distptr = filter->disttable;
221 m = sqrt ((double)(halfw * halfw + halfh * halfh));
223 for (y = -halfh; y < halfh; y++)
224 for (x= -halfw; x < halfw; x++)
226 *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
228 *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
233 gst_warptv_initialize (GstWarpTV *filter)
235 g_free (filter->disttable);
236 g_free (filter->offstable);
238 filter->offstable = (guint32 *) g_malloc (filter->height * sizeof (guint32));
239 filter->disttable = g_malloc (filter->width * filter->height * sizeof (guint32));
241 initSinTable (filter);
242 initOffsTable (filter);
243 initDistTable (filter);
247 gst_warptv_chain (GstPad * pad, GstBuffer * buf)
253 gint32 c,i, x,y, dx,dy, maxx, maxy;
254 gint32 width, height, skip, *ctptr, *distptr;
255 gint32 *sintable, *ctable;
257 filter = GST_WARPTV (gst_pad_get_parent (pad));
259 src = (guint32 *) GST_BUFFER_DATA (buf);
261 outbuf = gst_buffer_new ();
262 GST_BUFFER_SIZE (outbuf) = (filter->width * filter->height * sizeof(guint32));
263 dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
264 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
266 xw = (gint) (sin ((filter->tval + 100) * M_PI / 128) * 30);
267 yw = (gint) (sin ((filter->tval) * M_PI / 256) * -35);
268 cw = (gint) (sin ((filter->tval - 70) * M_PI / 64) * 50);
269 xw += (gint) (sin ((filter->tval - 10) * M_PI / 512) * 40);
270 yw += (gint) (sin ((filter->tval + 30) * M_PI / 512) * 40);
272 ctptr = filter->ctable;
273 distptr = filter->disttable;
274 width = filter->width;
275 height = filter->height;
276 sintable = filter->sintable;
277 ctable = filter->ctable;
279 skip = 0 ; /* video_width*sizeof(RGB32)/4 - video_width;; */
282 for (x = 0; x < 512; x++) {
283 i = (c >> 3) & 0x3FE;
284 *ctptr++ = ((sintable[i] * yw) >> 15);
285 *ctptr++ = ((sintable[i + 256] * xw) >> 15);
288 maxx = width - 2; maxy = height - 2;
290 for (y = 0; y < height - 1; y++) {
291 for (x = 0; x < width; x++) {
293 dx = ctable [i + 1] + x;
297 else if (dx > maxx) dx = maxx;
300 else if (dy > maxy) dy = maxy;
301 *dest++ = src[filter->offstable[dy] + dx];
306 filter->tval = (filter->tval + 1) & 511;
308 gst_buffer_unref (buf);
310 gst_pad_push (filter->srcpad, outbuf);
314 gst_warptv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
318 /* it's not null if we got it, but it might not be ours */
319 g_return_if_fail (GST_IS_WARPTV (object));
321 filter = GST_WARPTV (object);
330 gst_warptv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
334 /* it's not null if we got it, but it might not be ours */
335 g_return_if_fail (GST_IS_WARPTV (object));
337 filter = GST_WARPTV (object);
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);