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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, 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-1.0 -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 #include <gst/video/gstvideometa.h>
58 #include <gst/video/gstvideopool.h>
59 
60 #ifndef M_PI
61 #define M_PI    3.14159265358979323846
62 #endif
63 
64 #define gst_warptv_parent_class parent_class
65 G_DEFINE_TYPE (GstWarpTV, gst_warptv, GST_TYPE_VIDEO_FILTER);
66 
67 static void initSinTable ();
68 static void initDistTable (GstWarpTV * filter, gint width, gint height);
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_MAKE ("{ RGBx, xRGB, BGRx, xBGR }"))
75     );
76 
77 static GstStaticPadTemplate gst_warptv_sink_template =
78 GST_STATIC_PAD_TEMPLATE ("sink",
79     GST_PAD_SINK,
80     GST_PAD_ALWAYS,
81     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR }"))
82     );
83 
84 static gboolean
gst_warptv_set_info(GstVideoFilter * vfilter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)85 gst_warptv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
86     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
87 {
88   GstWarpTV *filter = GST_WARPTV (vfilter);
89   gint width, height;
90 
91   width = GST_VIDEO_INFO_WIDTH (in_info);
92   height = GST_VIDEO_INFO_HEIGHT (in_info);
93 
94   g_free (filter->disttable);
95   filter->disttable = g_malloc (width * height * sizeof (guint32));
96   initDistTable (filter, width, height);
97 
98   return TRUE;
99 }
100 
101 static gint32 sintable[1024 + 256];
102 
103 static void
initSinTable(void)104 initSinTable (void)
105 {
106   gint32 *tptr, *tsinptr;
107   gint i;
108 
109   tsinptr = tptr = sintable;
110 
111   for (i = 0; i < 1024; i++)
112     *tptr++ = (int) (sin (i * M_PI / 512) * 32767);
113 
114   for (i = 0; i < 256; i++)
115     *tptr++ = *tsinptr++;
116 }
117 
118 static void
initDistTable(GstWarpTV * filter,gint width,gint height)119 initDistTable (GstWarpTV * filter, gint width, gint height)
120 {
121   gint32 halfw, halfh, *distptr;
122   gint x, y;
123   float m;
124 
125   halfw = width >> 1;
126   halfh = height >> 1;
127 
128   distptr = filter->disttable;
129 
130   m = sqrt ((double) (halfw * halfw + halfh * halfh));
131 
132   for (y = -halfh; y < halfh; y++)
133     for (x = -halfw; x < halfw; x++)
134 #ifdef PS2
135       *distptr++ = ((int) ((sqrtf (x * x + y * y) * 511.9999) / m)) << 1;
136 #else
137       *distptr++ = ((int) ((sqrt (x * x + y * y) * 511.9999) / m)) << 1;
138 #endif
139 }
140 
141 static GstFlowReturn
gst_warptv_transform_frame(GstVideoFilter * filter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)142 gst_warptv_transform_frame (GstVideoFilter * filter, GstVideoFrame * in_frame,
143     GstVideoFrame * out_frame)
144 {
145   GstWarpTV *warptv = GST_WARPTV (filter);
146   gint width, height;
147   gint xw, yw, cw;
148   gint32 c, i, x, y, dx, dy, maxx, maxy;
149   gint32 *ctptr, *distptr;
150   gint32 *ctable;
151   guint32 *src, *dest;
152   gint sstride, dstride;
153 
154   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
155   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
156 
157   sstride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
158   dstride = GST_VIDEO_FRAME_PLANE_STRIDE (out_frame, 0);
159 
160   width = GST_VIDEO_FRAME_WIDTH (in_frame);
161   height = GST_VIDEO_FRAME_HEIGHT (in_frame);
162 
163   GST_OBJECT_LOCK (warptv);
164   xw = (gint) (sin ((warptv->tval + 100) * M_PI / 128) * 30);
165   yw = (gint) (sin ((warptv->tval) * M_PI / 256) * -35);
166   cw = (gint) (sin ((warptv->tval - 70) * M_PI / 64) * 50);
167   xw += (gint) (sin ((warptv->tval - 10) * M_PI / 512) * 40);
168   yw += (gint) (sin ((warptv->tval + 30) * M_PI / 512) * 40);
169 
170   ctptr = warptv->ctable;
171   distptr = warptv->disttable;
172   ctable = warptv->ctable;
173 
174   c = 0;
175 
176   for (x = 0; x < 512; x++) {
177     i = (c >> 3) & 0x3FE;
178     *ctptr++ = ((sintable[i] * yw) >> 15);
179     *ctptr++ = ((sintable[i + 256] * xw) >> 15);
180     c += cw;
181   }
182   maxx = width - 2;
183   maxy = height - 2;
184 
185   for (y = 0; y < height - 1; y++) {
186     for (x = 0; x < width; x++) {
187       i = *distptr++;
188       dx = ctable[i + 1] + x;
189       dy = ctable[i] + y;
190 
191       if (dx < 0)
192         dx = 0;
193       else if (dx > maxx)
194         dx = maxx;
195 
196       if (dy < 0)
197         dy = 0;
198       else if (dy > maxy)
199         dy = maxy;
200 
201       dest[x] = src[dy * sstride / 4 + dx];
202     }
203     dest += dstride / 4;
204   }
205 
206   warptv->tval = (warptv->tval + 1) & 511;
207   GST_OBJECT_UNLOCK (warptv);
208 
209   return GST_FLOW_OK;
210 }
211 
212 static gboolean
gst_warptv_start(GstBaseTransform * trans)213 gst_warptv_start (GstBaseTransform * trans)
214 {
215   GstWarpTV *warptv = GST_WARPTV (trans);
216 
217   warptv->tval = 0;
218 
219   return TRUE;
220 }
221 
222 static void
gst_warptv_finalize(GObject * object)223 gst_warptv_finalize (GObject * object)
224 {
225   GstWarpTV *warptv = GST_WARPTV (object);
226 
227   g_free (warptv->disttable);
228   warptv->disttable = NULL;
229 
230   G_OBJECT_CLASS (parent_class)->finalize (object);
231 }
232 
233 static void
gst_warptv_class_init(GstWarpTVClass * klass)234 gst_warptv_class_init (GstWarpTVClass * klass)
235 {
236   GObjectClass *gobject_class = (GObjectClass *) klass;
237   GstElementClass *gstelement_class = (GstElementClass *) klass;
238   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
239   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
240 
241   gobject_class->finalize = gst_warptv_finalize;
242 
243   gst_element_class_set_static_metadata (gstelement_class, "WarpTV effect",
244       "Filter/Effect/Video",
245       "WarpTV does realtime goo'ing of the video input",
246       "Sam Lantinga <slouken@devolution.com>");
247 
248   gst_element_class_add_static_pad_template (gstelement_class,
249       &gst_warptv_sink_template);
250   gst_element_class_add_static_pad_template (gstelement_class,
251       &gst_warptv_src_template);
252 
253   trans_class->start = GST_DEBUG_FUNCPTR (gst_warptv_start);
254 
255   vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_warptv_set_info);
256   vfilter_class->transform_frame =
257       GST_DEBUG_FUNCPTR (gst_warptv_transform_frame);
258 
259   initSinTable ();
260 }
261 
262 static void
gst_warptv_init(GstWarpTV * warptv)263 gst_warptv_init (GstWarpTV * warptv)
264 {
265   /* nothing to do */
266 }
267