upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.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 ! ffmpegcolorspace ! 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 #include <gst/video/video.h>
59
60 #ifndef M_PI
61 #define M_PI    3.14159265358979323846
62 #endif
63
64 GST_BOILERPLATE (GstWarpTV, gst_warptv, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
65
66 static void initSinTable ();
67 static void initOffsTable (GstWarpTV * filter);
68 static void initDistTable (GstWarpTV * filter);
69
70 static GstStaticPadTemplate gst_warptv_src_template =
71     GST_STATIC_PAD_TEMPLATE ("src",
72     GST_PAD_SRC,
73     GST_PAD_ALWAYS,
74     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
75         GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
76     );
77
78 static GstStaticPadTemplate gst_warptv_sink_template =
79     GST_STATIC_PAD_TEMPLATE ("sink",
80     GST_PAD_SINK,
81     GST_PAD_ALWAYS,
82     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
83         GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
84     );
85
86 static gboolean
87 gst_warptv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
88     GstCaps * outcaps)
89 {
90   GstWarpTV *filter = GST_WARPTV (btrans);
91   GstStructure *structure;
92   gboolean ret = FALSE;
93
94   structure = gst_caps_get_structure (incaps, 0);
95
96   GST_OBJECT_LOCK (filter);
97   if (gst_structure_get_int (structure, "width", &filter->width) &&
98       gst_structure_get_int (structure, "height", &filter->height)) {
99     g_free (filter->disttable);
100     g_free (filter->offstable);
101
102     filter->offstable = g_malloc (filter->height * sizeof (guint32));
103     filter->disttable =
104         g_malloc (filter->width * filter->height * sizeof (guint32));
105
106     initOffsTable (filter);
107     initDistTable (filter);
108     ret = TRUE;
109   }
110   GST_OBJECT_UNLOCK (filter);
111
112   return ret;
113 }
114
115 static gint32 sintable[1024 + 256];
116
117 static void
118 initSinTable (void)
119 {
120   gint32 *tptr, *tsinptr;
121   gint i;
122
123   tsinptr = tptr = sintable;
124
125   for (i = 0; i < 1024; i++)
126     *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
127
128   for (i = 0; i < 256; i++)
129     *tptr++ = *tsinptr++;
130 }
131
132 static void
133 initOffsTable (GstWarpTV * filter)
134 {
135   gint y;
136
137   for (y = 0; y < filter->height; y++) {
138     filter->offstable[y] = y * filter->width;
139   }
140 }
141
142 static void
143 initDistTable (GstWarpTV * filter)
144 {
145   gint32 halfw, halfh, *distptr;
146   gint x, y;
147 #ifdef PS2
148   float m;
149 #else
150   float m;
151 #endif
152
153   halfw = filter->width >> 1;
154   halfh = filter->height >> 1;
155
156   distptr = filter->disttable;
157
158   m = sqrt ((double) (halfw * halfw + halfh * halfh));
159
160   for (y = -halfh; y < halfh; y++)
161     for (x = -halfw; x < halfw; x++)
162 #ifdef PS2
163       *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
164 #else
165       *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
166 #endif
167 }
168
169 static GstFlowReturn
170 gst_warptv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
171 {
172   GstWarpTV *warptv = GST_WARPTV (trans);
173   gint width, height;
174   guint32 *src = (guint32 *) GST_BUFFER_DATA (in);
175   guint32 *dest = (guint32 *) GST_BUFFER_DATA (out);
176   gint xw, yw, cw;
177   gint32 c, i, x, y, dx, dy, maxx, maxy;
178   gint32 skip, *ctptr, *distptr;
179   gint32 *ctable;
180   GstFlowReturn ret = GST_FLOW_OK;
181
182   GST_OBJECT_LOCK (warptv);
183   width = warptv->width;
184   height = warptv->height;
185
186   xw = (gint) (sin ((warptv->tval + 100) * M_PI / 128) * 30);
187   yw = (gint) (sin ((warptv->tval) * M_PI / 256) * -35);
188   cw = (gint) (sin ((warptv->tval - 70) * M_PI / 64) * 50);
189   xw += (gint) (sin ((warptv->tval - 10) * M_PI / 512) * 40);
190   yw += (gint) (sin ((warptv->tval + 30) * M_PI / 512) * 40);
191
192   ctptr = warptv->ctable;
193   distptr = warptv->disttable;
194   ctable = warptv->ctable;
195
196   skip = 0;                     /* video_width*sizeof(RGB32)/4 - video_width;; */
197   c = 0;
198
199   for (x = 0; x < 512; x++) {
200     i = (c >> 3) & 0x3FE;
201     *ctptr++ = ((sintable[i] * yw) >> 15);
202     *ctptr++ = ((sintable[i + 256] * xw) >> 15);
203     c += cw;
204   }
205   maxx = width - 2;
206   maxy = height - 2;
207
208   for (y = 0; y < height - 1; y++) {
209     for (x = 0; x < width; x++) {
210       i = *distptr++;
211       dx = ctable[i + 1] + x;
212       dy = ctable[i] + y;
213
214       if (dx < 0)
215         dx = 0;
216       else if (dx > maxx)
217         dx = maxx;
218
219       if (dy < 0)
220         dy = 0;
221       else if (dy > maxy)
222         dy = maxy;
223       *dest++ = src[warptv->offstable[dy] + dx];
224     }
225     dest += skip;
226   }
227
228   warptv->tval = (warptv->tval + 1) & 511;
229   GST_OBJECT_UNLOCK (warptv);
230
231   return ret;
232 }
233
234 static gboolean
235 gst_warptv_start (GstBaseTransform * trans)
236 {
237   GstWarpTV *warptv = GST_WARPTV (trans);
238
239   warptv->tval = 0;
240
241   return TRUE;
242 }
243
244 static void
245 gst_warptv_finalize (GObject * object)
246 {
247   GstWarpTV *warptv = GST_WARPTV (object);
248
249   g_free (warptv->offstable);
250   warptv->offstable = NULL;
251   g_free (warptv->disttable);
252   warptv->disttable = NULL;
253
254   G_OBJECT_CLASS (parent_class)->finalize (object);
255 }
256
257 static void
258 gst_warptv_base_init (gpointer g_class)
259 {
260   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
261
262   gst_element_class_set_details_simple (element_class, "WarpTV effect",
263       "Filter/Effect/Video",
264       "WarpTV does realtime goo'ing of the video input",
265       "Sam Lantinga <slouken@devolution.com>");
266
267   gst_element_class_add_pad_template (element_class,
268       gst_static_pad_template_get (&gst_warptv_sink_template));
269   gst_element_class_add_pad_template (element_class,
270       gst_static_pad_template_get (&gst_warptv_src_template));
271 }
272
273 static void
274 gst_warptv_class_init (GstWarpTVClass * klass)
275 {
276   GObjectClass *gobject_class = (GObjectClass *) klass;
277   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
278
279   gobject_class->finalize = gst_warptv_finalize;
280
281   trans_class->start = GST_DEBUG_FUNCPTR (gst_warptv_start);
282   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_warptv_set_caps);
283   trans_class->transform = GST_DEBUG_FUNCPTR (gst_warptv_transform);
284
285   initSinTable ();
286 }
287
288 static void
289 gst_warptv_init (GstWarpTV * warptv, GstWarpTVClass * klass)
290 {
291   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (warptv));
292   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (warptv));
293 }