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 = {
68 "Filter/Video/Effect",
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);
94 static void gst_warptv_set_property (GObject * object, guint prop_id,
95 const GValue * value, GParamSpec * pspec);
96 static void gst_warptv_get_property (GObject * object, guint prop_id,
97 GValue * value, GParamSpec * pspec);
99 static void gst_warptv_chain (GstPad * pad, GstBuffer * buf);
101 static GstElementClass *parent_class = NULL;
102 /*static guint gst_warptv_signals[LAST_SIGNAL] = { 0 }; */
104 GType gst_warptv_get_type (void)
106 static GType warptv_type = 0;
109 static const GTypeInfo warptv_info = {
110 sizeof (GstWarpTVClass), NULL,
112 (GClassInitFunc) gst_warptv_class_init,
117 (GInstanceInitFunc) gst_warptv_init,
120 warptv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstWarpTV", &warptv_info, 0);
126 gst_warptv_class_init (GstWarpTVClass * klass)
128 GObjectClass *gobject_class;
129 GstElementClass *gstelement_class;
131 gobject_class = (GObjectClass *) klass;
132 gstelement_class = (GstElementClass *) klass;
134 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
136 gobject_class->set_property = gst_warptv_set_property;
137 gobject_class->get_property = gst_warptv_get_property;
140 static GstPadConnectReturn
141 gst_warptv_sinkconnect (GstPad * pad, GstCaps * caps)
145 filter = GST_WARPTV (gst_pad_get_parent (pad));
147 if (!GST_CAPS_IS_FIXED (caps))
148 return GST_PAD_CONNECT_DELAYED;
150 gst_caps_get_int (caps, "width", &filter->width);
151 gst_caps_get_int (caps, "height", &filter->height);
153 gst_warptv_initialize (filter);
155 if (gst_pad_try_set_caps (filter->srcpad, caps)) {
156 return GST_PAD_CONNECT_OK;
159 return GST_PAD_CONNECT_REFUSED;
163 gst_warptv_init (GstWarpTV * filter)
165 filter->sinkpad = gst_pad_new_from_template (gst_effectv_sink_factory (), "sink");
166 gst_pad_set_chain_function (filter->sinkpad, gst_warptv_chain);
167 gst_pad_set_connect_function (filter->sinkpad, gst_warptv_sinkconnect);
168 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
170 filter->srcpad = gst_pad_new_from_template (gst_effectv_src_factory (), "src");
171 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
174 filter->disttable = NULL;
175 filter->offstable = NULL;
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 g_free (filter->disttable);
234 g_free (filter->offstable);
236 filter->offstable = (guint32 *) g_malloc (filter->height * sizeof (guint32));
237 filter->disttable = g_malloc (filter->width * filter->height * sizeof (guint32));
239 initSinTable (filter);
240 initOffsTable (filter);
241 initDistTable (filter);
245 gst_warptv_chain (GstPad * pad, GstBuffer * buf)
251 gint32 c,i, x,y, dx,dy, maxx, maxy;
252 gint32 width, height, skip, *ctptr, *distptr;
253 gint32 *sintable, *ctable;
255 filter = GST_WARPTV (gst_pad_get_parent (pad));
257 src = (guint32 *) GST_BUFFER_DATA (buf);
259 outbuf = gst_buffer_new ();
260 GST_BUFFER_SIZE (outbuf) = (filter->width * filter->height * sizeof(guint32));
261 dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
262 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
264 xw = (gint) (sin ((filter->tval + 100) * M_PI / 128) * 30);
265 yw = (gint) (sin ((filter->tval) * M_PI / 256) * -35);
266 cw = (gint) (sin ((filter->tval - 70) * M_PI / 64) * 50);
267 xw += (gint) (sin ((filter->tval - 10) * M_PI / 512) * 40);
268 yw += (gint) (sin ((filter->tval + 30) * M_PI / 512) * 40);
270 ctptr = filter->ctable;
271 distptr = filter->disttable;
272 width = filter->width;
273 height = filter->height;
274 sintable = filter->sintable;
275 ctable = filter->ctable;
277 skip = 0 ; /* video_width*sizeof(RGB32)/4 - video_width;; */
280 for (x = 0; x < 512; x++) {
281 i = (c >> 3) & 0x3FE;
282 *ctptr++ = ((sintable[i] * yw) >> 15);
283 *ctptr++ = ((sintable[i + 256] * xw) >> 15);
286 maxx = width - 2; maxy = height - 2;
288 for (y = 0; y < height - 1; y++) {
289 for (x = 0; x < width; x++) {
291 dx = ctable [i + 1] + x;
295 else if (dx > maxx) dx = maxx;
298 else if (dy > maxy) dy = maxy;
299 *dest++ = src[filter->offstable[dy] + dx];
304 filter->tval = (filter->tval + 1) & 511;
306 gst_buffer_unref (buf);
308 gst_pad_push (filter->srcpad, outbuf);
312 gst_warptv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
316 /* it's not null if we got it, but it might not be ours */
317 g_return_if_fail (GST_IS_WARPTV (object));
319 filter = GST_WARPTV (object);
328 gst_warptv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
332 /* it's not null if we got it, but it might not be ours */
333 g_return_if_fail (GST_IS_WARPTV (object));
335 filter = GST_WARPTV (object);
339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);