This updates all plugins to the new API for gst_pad_try_set_caps
[platform/upstream/gstreamer.git] / gst / effectv / gstwarp.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * EffecTV - Realtime Digital Video Effector
5  * Copyright (C) 2001 FUKUCHI Kentarou
6  * 
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.
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  * From main.c of warp-1.1:
24  *
25  *      Simple DirectMedia Layer demo
26  *      Realtime picture 'gooing'
27  *      by sam lantinga slouken@devolution.com
28  */
29
30 #include <string.h>
31 #include <math.h>
32 #include <gst/gst.h>
33 #include "gsteffectv.h"
34
35 #ifndef M_PI
36 #define M_PI    3.14159265358979323846
37 #endif
38
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))
49
50 typedef struct _GstWarpTV GstWarpTV;
51 typedef struct _GstWarpTVClass GstWarpTVClass;
52
53 struct _GstWarpTV
54 {
55   GstElement element;
56
57   GstPad *sinkpad, *srcpad;
58
59   gint width, height;
60   gint *offstable;
61   gint32 *disttable;
62   gint32 ctable[1024];
63   gint32 sintable[1024+256];
64   gint tval;
65 };
66
67 struct _GstWarpTVClass
68 {
69   GstElementClass parent_class;
70 };
71
72 GstElementDetails gst_warptv_details = {
73   "WarpTV",
74   "Filter/Video/Effect",
75   "WarpTV does realtime goo'ing of the video input",
76   VERSION,
77   "Sam Lantinga <slouken@devolution.com>",
78   "Wim Taymans <wim.taymans@chello.be>, "
79   "(C) 2001 FUKUCHI Kentarou",
80 };
81
82
83 /* Filter signals and args */
84 enum
85 {
86   /* FILL ME */
87   LAST_SIGNAL
88 };
89
90 enum
91 {
92   ARG_0,
93 };
94
95 static void     gst_warptv_class_init           (GstWarpTVClass * klass);
96 static void     gst_warptv_init                 (GstWarpTV * filter);
97
98 static void     gst_warptv_initialize           (GstWarpTV *filter);
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_chain                (GstPad * pad, GstBuffer * buf);
106
107 static GstElementClass *parent_class = NULL;
108 /*static guint gst_warptv_signals[LAST_SIGNAL] = { 0 }; */
109
110 GType gst_warptv_get_type (void)
111 {
112   static GType warptv_type = 0;
113
114   if (!warptv_type) {
115     static const GTypeInfo warptv_info = {
116       sizeof (GstWarpTVClass), NULL,
117       NULL,
118       (GClassInitFunc) gst_warptv_class_init,
119       NULL,
120       NULL,
121       sizeof (GstWarpTV),
122       0,
123       (GInstanceInitFunc) gst_warptv_init,
124     };
125
126     warptv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstWarpTV", &warptv_info, 0);
127   }
128   return warptv_type;
129 }
130
131 static void
132 gst_warptv_class_init (GstWarpTVClass * klass)
133 {
134   GObjectClass *gobject_class;
135   GstElementClass *gstelement_class;
136
137   gobject_class = (GObjectClass *) klass;
138   gstelement_class = (GstElementClass *) klass;
139
140   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
141
142   gobject_class->set_property = gst_warptv_set_property;
143   gobject_class->get_property = gst_warptv_get_property;
144 }
145
146 static GstPadConnectReturn
147 gst_warptv_sinkconnect (GstPad * pad, GstCaps * caps)
148 {
149   GstWarpTV *filter;
150
151   filter = GST_WARPTV (gst_pad_get_parent (pad));
152
153   if (!GST_CAPS_IS_FIXED (caps))
154     return GST_PAD_CONNECT_DELAYED;
155
156   gst_caps_get_int (caps, "width", &filter->width);
157   gst_caps_get_int (caps, "height", &filter->height);
158
159   gst_warptv_initialize (filter);
160
161   return gst_pad_try_set_caps (filter->srcpad, caps);
162 }
163
164 static void
165 gst_warptv_init (GstWarpTV * filter)
166 {
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);
171
172   filter->srcpad = gst_pad_new_from_template (gst_effectv_src_factory (), "src");
173   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
174
175   filter->tval = 0;
176   filter->disttable = NULL;
177   filter->offstable = NULL;
178 }
179
180
181 static void 
182 initSinTable (GstWarpTV *filter) 
183 {
184   gint32        *tptr, *tsinptr;
185   double        i;
186
187   tsinptr = tptr = filter->sintable;
188
189   for (i = 0; i < 1024; i++)
190     *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
191
192   for (i = 0; i < 256; i++)
193     *tptr++ = *tsinptr++;
194 }
195
196 static void 
197 initOffsTable (GstWarpTV *filter) 
198 {
199   int y;
200         
201   for (y = 0; y < filter->height; y++) {
202     filter->offstable[y] = y * filter->width;
203   }
204 }
205       
206 static void 
207 initDistTable (GstWarpTV *filter) 
208 {
209   gint32 halfw, halfh, *distptr;
210 #ifdef PS2
211   float x,y,m;
212 #else
213   double x,y,m;
214 #endif
215
216   halfw = filter->width>> 1;
217   halfh = filter->height >> 1;
218
219   distptr = filter->disttable;
220
221   m = sqrt ((double)(halfw * halfw + halfh * halfh));
222
223   for (y = -halfh; y < halfh; y++)
224     for (x= -halfw; x < halfw; x++)
225 #ifdef PS2
226       *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
227 #else
228       *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
229 #endif
230 }
231
232 static void 
233 gst_warptv_initialize (GstWarpTV *filter) 
234 {
235   g_free (filter->disttable);
236   g_free (filter->offstable);
237
238   filter->offstable = (guint32 *) g_malloc (filter->height * sizeof (guint32));      
239   filter->disttable = g_malloc (filter->width * filter->height * sizeof (guint32));
240
241   initSinTable (filter);
242   initOffsTable (filter);
243   initDistTable (filter);
244 }
245
246 static void
247 gst_warptv_chain (GstPad * pad, GstBuffer * buf)
248 {
249   GstWarpTV *filter;
250   guint32 *src, *dest;
251   gint xw,yw,cw;
252   GstBuffer *outbuf;
253   gint32 c,i, x,y, dx,dy, maxx, maxy;
254   gint32 width, height, skip, *ctptr, *distptr;
255   gint32 *sintable, *ctable;
256
257   filter = GST_WARPTV (gst_pad_get_parent (pad));
258
259   src = (guint32 *) GST_BUFFER_DATA (buf);
260
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);
265   
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);     
271
272   ctptr = filter->ctable;
273   distptr = filter->disttable;
274   width = filter->width;
275   height = filter->height;
276   sintable = filter->sintable;
277   ctable = filter->ctable;
278
279   skip = 0 ; /* video_width*sizeof(RGB32)/4 - video_width;; */
280   c = 0;
281
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);
286     c += cw;
287   }
288   maxx = width - 2; maxy = height - 2;
289
290   for (y = 0; y < height - 1; y++) {
291     for (x = 0; x < width; x++) {
292       i = *distptr++; 
293       dx = ctable [i + 1] + x; 
294       dy = ctable [i] + y;       
295
296       if (dx < 0) dx = 0; 
297       else if (dx > maxx) dx = maxx; 
298    
299       if (dy < 0) dy = 0; 
300       else if (dy > maxy) dy = maxy; 
301       *dest++ = src[filter->offstable[dy] + dx]; 
302     }
303     dest += skip;
304   }
305
306   filter->tval = (filter->tval + 1) & 511;
307
308   gst_buffer_unref (buf);
309
310   gst_pad_push (filter->srcpad, outbuf);
311 }
312
313 static void
314 gst_warptv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
315 {
316   GstWarpTV *filter;
317
318   /* it's not null if we got it, but it might not be ours */
319   g_return_if_fail (GST_IS_WARPTV (object));
320
321   filter = GST_WARPTV (object);
322
323   switch (prop_id) {
324     default:
325       break;
326   }
327 }
328
329 static void
330 gst_warptv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
331 {
332   GstWarpTV *filter;
333
334   /* it's not null if we got it, but it might not be ours */
335   g_return_if_fail (GST_IS_WARPTV (object));
336
337   filter = GST_WARPTV (object);
338
339   switch (prop_id) {
340     default:
341       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342       break;
343   }
344 }