effectv: fix docs
[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 /**
37  * SECTION:element-warptv
38  *
39  * WarpTV does realtime goo'ing of the video input.
40  *
41  * <refsect2>
42  * <title>Example launch line</title>
43  * |[
44  * gst-launch -v videotestsrc ! warptv ! videoconvert ! autovideosink
45  * ]| This pipeline shows the effect of warptv on a test stream.
46  * </refsect2>
47  */
48
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include <string.h>
54 #include <math.h>
55
56 #include "gstwarp.h"
57
58 #ifndef M_PI
59 #define M_PI    3.14159265358979323846
60 #endif
61
62 #define gst_warptv_parent_class parent_class
63 G_DEFINE_TYPE (GstWarpTV, gst_warptv, GST_TYPE_VIDEO_FILTER);
64
65 static void initSinTable ();
66 static void initOffsTable (GstWarpTV * filter, gint width, gint height);
67 static void initDistTable (GstWarpTV * filter, gint width, gint height);
68
69 static GstStaticPadTemplate gst_warptv_src_template =
70 GST_STATIC_PAD_TEMPLATE ("src",
71     GST_PAD_SRC,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR }"))
74     );
75
76 static GstStaticPadTemplate gst_warptv_sink_template =
77 GST_STATIC_PAD_TEMPLATE ("sink",
78     GST_PAD_SINK,
79     GST_PAD_ALWAYS,
80     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR }"))
81     );
82
83 static gboolean
84 gst_warptv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
85     GstCaps * outcaps)
86 {
87   GstWarpTV *filter = GST_WARPTV (btrans);
88   GstVideoInfo info;
89   gint width, height;
90
91   if (!gst_video_info_from_caps (&info, incaps))
92     goto invalid_caps;
93
94   filter->info = info;
95
96   width = GST_VIDEO_INFO_WIDTH (&info);
97   height = GST_VIDEO_INFO_HEIGHT (&info);
98
99   g_free (filter->disttable);
100   g_free (filter->offstable);
101   filter->offstable = g_malloc (height * sizeof (guint32));
102   filter->disttable = g_malloc (width * height * sizeof (guint32));
103   initOffsTable (filter, width, height);
104   initDistTable (filter, width, height);
105
106   return TRUE;
107
108   /* ERRORS */
109 invalid_caps:
110   {
111     GST_DEBUG_OBJECT (filter, "invalid caps received");
112     return FALSE;
113   }
114 }
115
116 static gint32 sintable[1024 + 256];
117
118 static void
119 initSinTable (void)
120 {
121   gint32 *tptr, *tsinptr;
122   gint i;
123
124   tsinptr = tptr = sintable;
125
126   for (i = 0; i < 1024; i++)
127     *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
128
129   for (i = 0; i < 256; i++)
130     *tptr++ = *tsinptr++;
131 }
132
133 static void
134 initOffsTable (GstWarpTV * filter, gint width, gint height)
135 {
136   gint y;
137
138   for (y = 0; y < height; y++) {
139     filter->offstable[y] = y * width;
140   }
141 }
142
143 static void
144 initDistTable (GstWarpTV * filter, gint width, gint height)
145 {
146   gint32 halfw, halfh, *distptr;
147   gint x, y;
148 #ifdef PS2
149   float m;
150 #else
151   float m;
152 #endif
153
154   halfw = width >> 1;
155   halfh = height >> 1;
156
157   distptr = filter->disttable;
158
159   m = sqrt ((double) (halfw * halfw + halfh * halfh));
160
161   for (y = -halfh; y < halfh; y++)
162     for (x = -halfw; x < halfw; x++)
163 #ifdef PS2
164       *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
165 #else
166       *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
167 #endif
168 }
169
170 static GstFlowReturn
171 gst_warptv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
172 {
173   GstWarpTV *warptv = GST_WARPTV (trans);
174   gint width, height;
175   gint xw, yw, cw;
176   gint32 c, i, x, y, dx, dy, maxx, maxy;
177   gint32 skip, *ctptr, *distptr;
178   gint32 *ctable;
179   guint32 *src, *dest;
180   GstVideoFrame in_frame, out_frame;
181
182   gst_video_frame_map (&in_frame, &warptv->info, in, GST_MAP_READ);
183   gst_video_frame_map (&out_frame, &warptv->info, out, GST_MAP_WRITE);
184
185   src = GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
186   dest = GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
187
188   width = GST_VIDEO_FRAME_WIDTH (&in_frame);
189   height = GST_VIDEO_FRAME_HEIGHT (&in_frame);
190
191   GST_OBJECT_LOCK (warptv);
192   xw = (gint) (sin ((warptv->tval + 100) * M_PI / 128) * 30);
193   yw = (gint) (sin ((warptv->tval) * M_PI / 256) * -35);
194   cw = (gint) (sin ((warptv->tval - 70) * M_PI / 64) * 50);
195   xw += (gint) (sin ((warptv->tval - 10) * M_PI / 512) * 40);
196   yw += (gint) (sin ((warptv->tval + 30) * M_PI / 512) * 40);
197
198   ctptr = warptv->ctable;
199   distptr = warptv->disttable;
200   ctable = warptv->ctable;
201
202   skip = 0;                     /* video_width*sizeof(RGB32)/4 - video_width;; */
203   c = 0;
204
205   for (x = 0; x < 512; x++) {
206     i = (c >> 3) & 0x3FE;
207     *ctptr++ = ((sintable[i] * yw) >> 15);
208     *ctptr++ = ((sintable[i + 256] * xw) >> 15);
209     c += cw;
210   }
211   maxx = width - 2;
212   maxy = height - 2;
213
214   for (y = 0; y < height - 1; y++) {
215     for (x = 0; x < width; x++) {
216       i = *distptr++;
217       dx = ctable[i + 1] + x;
218       dy = ctable[i] + y;
219
220       if (dx < 0)
221         dx = 0;
222       else if (dx > maxx)
223         dx = maxx;
224
225       if (dy < 0)
226         dy = 0;
227       else if (dy > maxy)
228         dy = maxy;
229       *dest++ = src[warptv->offstable[dy] + dx];
230     }
231     dest += skip;
232   }
233
234   warptv->tval = (warptv->tval + 1) & 511;
235   GST_OBJECT_UNLOCK (warptv);
236
237   gst_video_frame_unmap (&in_frame);
238   gst_video_frame_unmap (&out_frame);
239
240   return GST_FLOW_OK;
241 }
242
243 static gboolean
244 gst_warptv_start (GstBaseTransform * trans)
245 {
246   GstWarpTV *warptv = GST_WARPTV (trans);
247
248   warptv->tval = 0;
249
250   return TRUE;
251 }
252
253 static void
254 gst_warptv_finalize (GObject * object)
255 {
256   GstWarpTV *warptv = GST_WARPTV (object);
257
258   g_free (warptv->offstable);
259   warptv->offstable = NULL;
260   g_free (warptv->disttable);
261   warptv->disttable = NULL;
262
263   G_OBJECT_CLASS (parent_class)->finalize (object);
264 }
265
266 static void
267 gst_warptv_class_init (GstWarpTVClass * klass)
268 {
269   GObjectClass *gobject_class = (GObjectClass *) klass;
270   GstElementClass *gstelement_class = (GstElementClass *) klass;
271   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
272
273   gobject_class->finalize = gst_warptv_finalize;
274
275   gst_element_class_set_details_simple (gstelement_class, "WarpTV effect",
276       "Filter/Effect/Video",
277       "WarpTV does realtime goo'ing of the video input",
278       "Sam Lantinga <slouken@devolution.com>");
279
280   gst_element_class_add_pad_template (gstelement_class,
281       gst_static_pad_template_get (&gst_warptv_sink_template));
282   gst_element_class_add_pad_template (gstelement_class,
283       gst_static_pad_template_get (&gst_warptv_src_template));
284
285   trans_class->start = GST_DEBUG_FUNCPTR (gst_warptv_start);
286   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_warptv_set_caps);
287   trans_class->transform = GST_DEBUG_FUNCPTR (gst_warptv_transform);
288
289   initSinTable ();
290 }
291
292 static void
293 gst_warptv_init (GstWarpTV * warptv)
294 {
295   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (warptv));
296   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (warptv));
297 }