2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
5 * Copyright (C) 2001 FUKUCHI Kentarou
7 * Inspired by Adrian Likin's script for the GIMP.
8 * EffecTV is free software. This library is free software;
9 * you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * SECTION:element-shagadelictv
28 * Oh behave, ShagedelicTV makes images shagadelic!
31 * <title>Example launch line</title>
33 * gst-launch -v videotestsrc ! shagadelictv ! videoconvert ! autovideosink
34 * ]| This pipeline shows the effect of shagadelictv on a test stream.
45 #include "gstshagadelic.h"
46 #include "gsteffectv.h"
49 #define M_PI 3.14159265358979323846
52 #define gst_shagadelictv_parent_class parent_class
53 G_DEFINE_TYPE (GstShagadelicTV, gst_shagadelictv, GST_TYPE_VIDEO_FILTER);
55 static void gst_shagadelic_initialize (GstShagadelicTV * filter);
57 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
58 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("BGRx")
60 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("xRGB")
63 static GstStaticPadTemplate gst_shagadelictv_src_template =
64 GST_STATIC_PAD_TEMPLATE ("src",
67 GST_STATIC_CAPS (CAPS_STR)
70 static GstStaticPadTemplate gst_shagadelictv_sink_template =
71 GST_STATIC_PAD_TEMPLATE ("sink",
74 GST_STATIC_CAPS (CAPS_STR)
78 gst_shagadelictv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
81 GstShagadelicTV *filter = GST_SHAGADELICTV (btrans);
83 gint width, height, area;
85 if (!gst_video_info_from_caps (&info, incaps))
90 width = GST_VIDEO_INFO_WIDTH (&info);
91 height = GST_VIDEO_INFO_HEIGHT (&info);
93 area = width * height;
95 g_free (filter->ripple);
96 g_free (filter->spiral);
97 filter->ripple = (guint8 *) g_malloc (area * 4);
98 filter->spiral = (guint8 *) g_malloc (area);
100 gst_shagadelic_initialize (filter);
107 GST_DEBUG_OBJECT (filter, "invalid caps received");
113 gst_shagadelic_initialize (GstShagadelicTV * filter)
123 width = GST_VIDEO_INFO_WIDTH (&filter->info);
124 height = GST_VIDEO_INFO_HEIGHT (&filter->info);
127 for (y = 0; y < height * 2; y++) {
131 for (x = 0; x < width * 2; x++) {
134 filter->ripple[i++] = ((unsigned int) (sqrtf (xx * xx + yy) * 8)) & 255;
136 filter->ripple[i++] = ((unsigned int) (sqrt (xx * xx + yy) * 8)) & 255;
142 for (y = 0; y < height; y++) {
145 for (x = 0; x < width; x++) {
148 filter->spiral[i++] = ((unsigned int)
150 yy) / ((float) M_PI) * 256 * 9) + (sqrtf (xx * xx +
151 yy * yy) * 5))) & 255;
153 filter->spiral[i++] = ((unsigned int)
154 ((atan2 (xx, yy) / M_PI * 256 * 9) + (sqrt (xx * xx +
155 yy * yy) * 5))) & 255;
157 /* Here is another Swinger!
158 * ((atan2(xx, yy)/M_PI*256) + (sqrt(xx*xx+yy*yy)*10))&255;
162 filter->rx = fastrand () % width;
163 filter->ry = fastrand () % height;
164 filter->bx = fastrand () % width;
165 filter->by = fastrand () % height;
174 gst_shagadelictv_transform (GstBaseTransform * trans, GstBuffer * in,
177 GstShagadelicTV *filter = GST_SHAGADELICTV (trans);
182 GstVideoFrame in_frame, out_frame;
185 if (!gst_video_frame_map (&in_frame, &filter->info, in, GST_MAP_READ))
188 if (!gst_video_frame_map (&out_frame, &filter->info, out, GST_MAP_WRITE))
191 src = GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
192 dest = GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
194 width = GST_VIDEO_FRAME_WIDTH (&in_frame);
195 height = GST_VIDEO_FRAME_HEIGHT (&in_frame);
197 for (y = 0; y < height; y++) {
198 for (x = 0; x < width; x++) {
199 v = *src++ | 0x1010100;
200 v = (v - 0x707060) & 0x1010100;
204 * *dest++ = v & ((r<<16)|(g<<8)|b);
206 r = ((gint8) (filter->ripple[(filter->ry + y) * width * 2 + filter->rx +
207 x] + filter->phase * 2)) >> 7;
208 g = ((gint8) (filter->spiral[y * width + x] + filter->phase * 3)) >> 7;
209 b = ((gint8) (filter->ripple[(filter->by + y) * width * 2 + filter->bx +
210 x] - filter->phase)) >> 7;
211 *dest++ = v & ((r << 16) | (g << 8) | b);
216 if ((filter->rx + filter->rvx) < 0 || (filter->rx + filter->rvx) >= width)
217 filter->rvx = -filter->rvx;
218 if ((filter->ry + filter->rvy) < 0 || (filter->ry + filter->rvy) >= height)
219 filter->rvy = -filter->rvy;
220 if ((filter->bx + filter->bvx) < 0 || (filter->bx + filter->bvx) >= width)
221 filter->bvx = -filter->bvx;
222 if ((filter->by + filter->bvy) < 0 || (filter->by + filter->bvy) >= height)
223 filter->bvy = -filter->bvy;
224 filter->rx += filter->rvx;
225 filter->ry += filter->rvy;
226 filter->bx += filter->bvx;
227 filter->by += filter->bvy;
229 gst_video_frame_unmap (&in_frame);
230 gst_video_frame_unmap (&out_frame);
237 GST_DEBUG_OBJECT (filter, "invalid input frame");
238 return GST_FLOW_ERROR;
242 GST_DEBUG_OBJECT (filter, "invalid output frame");
243 gst_video_frame_unmap (&in_frame);
244 return GST_FLOW_ERROR;
249 gst_shagadelictv_finalize (GObject * object)
251 GstShagadelicTV *filter = GST_SHAGADELICTV (object);
254 g_free (filter->ripple);
255 filter->ripple = NULL;
258 g_free (filter->spiral);
259 filter->spiral = NULL;
261 G_OBJECT_CLASS (parent_class)->finalize (object);
265 gst_shagadelictv_class_init (GstShagadelicTVClass * klass)
267 GObjectClass *gobject_class = (GObjectClass *) klass;
268 GstElementClass *gstelement_class = (GstElementClass *) klass;
269 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
271 gobject_class->finalize = gst_shagadelictv_finalize;
273 gst_element_class_set_details_simple (gstelement_class, "ShagadelicTV",
274 "Filter/Effect/Video",
275 "Oh behave, ShagedelicTV makes images shagadelic!",
276 "Wim Taymans <wim.taymans@chello.be>");
278 gst_element_class_add_pad_template (gstelement_class,
279 gst_static_pad_template_get (&gst_shagadelictv_sink_template));
280 gst_element_class_add_pad_template (gstelement_class,
281 gst_static_pad_template_get (&gst_shagadelictv_src_template));
283 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_shagadelictv_set_caps);
284 trans_class->transform = GST_DEBUG_FUNCPTR (gst_shagadelictv_transform);
288 gst_shagadelictv_init (GstShagadelicTV * filter)
290 filter->ripple = NULL;
291 filter->spiral = NULL;
293 gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (filter));
294 gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (filter));