2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * EffecTV is free software. We release this product under the terms of the
5 * GNU General Public License version 2. The license is included in the file
8 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
9 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
10 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 * EffecTV - Realtime Digital Video Effector
14 * Copyright (C) 2001 FUKUCHI Kentarou
16 * From main.c of warp-1.1:
18 * Simple DirectMedia Layer demo
19 * Realtime picture 'gooing'
21 * by sam lantinga slouken@devolution.com
27 #include "gsteffectv.h"
30 #define M_PI 3.14159265358979323846
33 #define GST_TYPE_WARPTV \
34 (gst_warptv_get_type())
35 #define GST_WARPTV(obj) \
36 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_WARPTV,GstWarpTV))
37 #define GST_WARPTV_CLASS(klass) \
38 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstWarpTV))
39 #define GST_IS_WARPTV(obj) \
40 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_WARPTV))
41 #define GST_IS_WARPTV_CLASS(obj) \
42 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_WARPTV))
44 typedef struct _GstWarpTV GstWarpTV;
45 typedef struct _GstWarpTVClass GstWarpTVClass;
51 GstPad *sinkpad, *srcpad;
57 gint32 sintable[1024+256];
61 struct _GstWarpTVClass
63 GstElementClass parent_class;
66 GstElementDetails gst_warptv_details = {
69 "WarpTV does realtime goo'ing of the video input",
71 "Sam Lantinga <slouken@devolution.com>",
72 "Wim Taymans <wim.taymans@chello.be>, "
73 "(C) 2001 FUKUCHI Kentarou",
77 /* Filter signals and args */
89 static void gst_warptv_class_init (GstWarpTVClass * klass);
90 static void gst_warptv_init (GstWarpTV * filter);
92 static void gst_warptv_initialize (GstWarpTV *filter);
93 static void gst_warptv_dispose (GObject *object);
95 static void gst_warptv_set_property (GObject * object, guint prop_id,
96 const GValue * value, GParamSpec * pspec);
97 static void gst_warptv_get_property (GObject * object, guint prop_id,
98 GValue * value, GParamSpec * pspec);
100 static void gst_warptv_chain (GstPad * pad, GstBuffer * buf);
102 static GstElementClass *parent_class = NULL;
103 /*static guint gst_warptv_signals[LAST_SIGNAL] = { 0 }; */
105 GType gst_warptv_get_type (void)
107 static GType warptv_type = 0;
110 static const GTypeInfo warptv_info = {
111 sizeof (GstWarpTVClass), NULL,
113 (GClassInitFunc) gst_warptv_class_init,
118 (GInstanceInitFunc) gst_warptv_init,
121 warptv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstWarpTV", &warptv_info, 0);
127 gst_warptv_class_init (GstWarpTVClass * klass)
129 GObjectClass *gobject_class;
130 GstElementClass *gstelement_class;
132 gobject_class = (GObjectClass *) klass;
133 gstelement_class = (GstElementClass *) klass;
135 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
137 gobject_class->set_property = gst_warptv_set_property;
138 gobject_class->get_property = gst_warptv_get_property;
139 gobject_class->dispose = gst_warptv_dispose;
142 static GstPadConnectReturn
143 gst_warptv_sinkconnect (GstPad * pad, GstCaps * caps)
147 filter = GST_WARPTV (gst_pad_get_parent (pad));
149 if (!GST_CAPS_IS_FIXED (caps))
150 return GST_PAD_CONNECT_DELAYED;
152 gst_caps_get_int (caps, "width", &filter->width);
153 gst_caps_get_int (caps, "height", &filter->height);
155 gst_warptv_initialize (filter);
157 if (gst_pad_try_set_caps (filter->srcpad, caps)) {
158 return GST_PAD_CONNECT_OK;
161 return GST_PAD_CONNECT_REFUSED;
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);
180 initSinTable (GstWarpTV *filter)
182 gint32 *tptr, *tsinptr;
185 tsinptr = tptr = filter->sintable;
187 for (i = 0; i < 1024; i++)
188 *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
190 for (i = 0; i < 256; i++)
191 *tptr++ = *tsinptr++;
195 initOffsTable (GstWarpTV *filter)
199 for (y = 0; y < filter->height; y++) {
200 filter->offstable[y] = y * filter->width;
205 initDistTable (GstWarpTV *filter)
207 gint32 halfw, halfh, *distptr;
214 halfw = filter->width>> 1;
215 halfh = filter->height >> 1;
217 distptr = filter->disttable;
219 m = sqrt ((double)(halfw * halfw + halfh * halfh));
221 for (y = -halfh; y < halfh; y++)
222 for (x= -halfw; x < halfw; x++)
224 *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
226 *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
231 gst_warptv_initialize (GstWarpTV *filter)
233 filter->offstable = (guint32 *) g_malloc (filter->height * sizeof (guint32));
234 filter->disttable = g_malloc (filter->width * filter->height * sizeof (guint32));
236 initSinTable (filter);
237 initOffsTable (filter);
238 initDistTable (filter);
242 gst_warptv_dispose (GObject *object)
244 GstWarpTV *filter = GST_WARPTV (object);
246 g_free (filter->disttable);
247 g_free (filter->offstable);
251 gst_warptv_chain (GstPad * pad, GstBuffer * buf)
257 gint32 c,i, x,y, dx,dy, maxx, maxy;
258 gint32 width, height, skip, *ctptr, *distptr;
259 gint32 *sintable, *ctable;
261 filter = GST_WARPTV (gst_pad_get_parent (pad));
263 src = (guint32 *) GST_BUFFER_DATA (buf);
265 outbuf = gst_buffer_new ();
266 GST_BUFFER_SIZE (outbuf) = (filter->width * filter->height * sizeof(guint32));
267 dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
268 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
270 xw = (gint) (sin ((filter->tval + 100) * M_PI / 128) * 30);
271 yw = (gint) (sin ((filter->tval) * M_PI / 256) * -35);
272 cw = (gint) (sin ((filter->tval - 70) * M_PI / 64) * 50);
273 xw += (gint) (sin ((filter->tval - 10) * M_PI / 512) * 40);
274 yw += (gint) (sin ((filter->tval + 30) * M_PI / 512) * 40);
276 ctptr = filter->ctable;
277 distptr = filter->disttable;
278 width = filter->width;
279 height = filter->height;
280 sintable = filter->sintable;
281 ctable = filter->ctable;
283 skip = 0 ; /* video_width*sizeof(RGB32)/4 - video_width;; */
286 for (x = 0; x < 512; x++) {
287 i = (c >> 3) & 0x3FE;
288 *ctptr++ = ((sintable[i] * yw) >> 15);
289 *ctptr++ = ((sintable[i + 256] * xw) >> 15);
292 maxx = width - 2; maxy = height - 2;
294 for (y = 0; y < height - 1; y++) {
295 for (x = 0; x < width; x++) {
297 dx = ctable [i + 1] + x;
301 else if (dx > maxx) dx = maxx;
304 else if (dy > maxy) dy = maxy;
305 *dest++ = src[filter->offstable[dy] + dx];
310 filter->tval = (filter->tval + 1) & 511;
312 gst_buffer_unref (buf);
314 gst_pad_push (filter->srcpad, outbuf);
318 gst_warptv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
322 /* it's not null if we got it, but it might not be ours */
323 g_return_if_fail (GST_IS_WARPTV (object));
325 filter = GST_WARPTV (object);
334 gst_warptv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
338 /* it's not null if we got it, but it might not be ours */
339 g_return_if_fail (GST_IS_WARPTV (object));
341 filter = GST_WARPTV (object);
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);