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 ! ffmpegcolorspace ! autovideosink
34 * ]| This pipeline shows the effect of shagadelictv on a test stream.
45 #include "gstshagadelic.h"
46 #include "gsteffectv.h"
48 #include <gst/video/video.h>
51 #define M_PI 3.14159265358979323846
54 GST_BOILERPLATE (GstShagadelicTV, gst_shagadelictv, GstVideoFilter,
55 GST_TYPE_VIDEO_FILTER);
57 static void gst_shagadelic_initialize (GstShagadelicTV * filter);
59 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
60 #define CAPS_STR GST_VIDEO_CAPS_BGRx
62 #define CAPS_STR GST_VIDEO_CAPS_xRGB
65 static GstStaticPadTemplate gst_shagadelictv_src_template =
66 GST_STATIC_PAD_TEMPLATE ("src",
69 GST_STATIC_CAPS (CAPS_STR)
72 static GstStaticPadTemplate gst_shagadelictv_sink_template =
73 GST_STATIC_PAD_TEMPLATE ("sink",
76 GST_STATIC_CAPS (CAPS_STR)
80 gst_shagadelictv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
83 GstShagadelicTV *filter = GST_SHAGADELICTV (btrans);
84 GstStructure *structure;
87 structure = gst_caps_get_structure (incaps, 0);
89 GST_OBJECT_LOCK (filter);
90 if (gst_structure_get_int (structure, "width", &filter->width) &&
91 gst_structure_get_int (structure, "height", &filter->height)) {
92 gint area = filter->width * filter->height;
94 g_free (filter->ripple);
95 g_free (filter->spiral);
97 filter->ripple = (guint8 *) g_malloc (area * 4);
98 filter->spiral = (guint8 *) g_malloc (area);
100 gst_shagadelic_initialize (filter);
103 GST_OBJECT_UNLOCK (filter);
109 gst_shagadelic_initialize (GstShagadelicTV * filter)
120 for (y = 0; y < filter->height * 2; y++) {
121 yy = y - filter->height;
124 for (x = 0; x < filter->width * 2; x++) {
125 xx = x - filter->width;
127 filter->ripple[i++] = ((unsigned int) (sqrtf (xx * xx + yy) * 8)) & 255;
129 filter->ripple[i++] = ((unsigned int) (sqrt (xx * xx + yy) * 8)) & 255;
135 for (y = 0; y < filter->height; y++) {
136 yy = y - filter->height / 2;
138 for (x = 0; x < filter->width; x++) {
139 xx = x - filter->width / 2;
141 filter->spiral[i++] = ((unsigned int)
143 yy) / ((float) M_PI) * 256 * 9) + (sqrtf (xx * xx +
144 yy * yy) * 5))) & 255;
146 filter->spiral[i++] = ((unsigned int)
147 ((atan2 (xx, yy) / M_PI * 256 * 9) + (sqrt (xx * xx +
148 yy * yy) * 5))) & 255;
150 /* Here is another Swinger!
151 * ((atan2(xx, yy)/M_PI*256) + (sqrt(xx*xx+yy*yy)*10))&255;
155 filter->rx = fastrand () % filter->width;
156 filter->ry = fastrand () % filter->height;
157 filter->bx = fastrand () % filter->width;
158 filter->by = fastrand () % filter->height;
167 gst_shagadelictv_transform (GstBaseTransform * trans, GstBuffer * in,
170 GstShagadelicTV *filter = GST_SHAGADELICTV (trans);
176 GstFlowReturn ret = GST_FLOW_OK;
178 src = (guint32 *) GST_BUFFER_DATA (in);
179 dest = (guint32 *) GST_BUFFER_DATA (out);
181 GST_OBJECT_LOCK (filter);
182 width = filter->width;
183 height = filter->height;
185 for (y = 0; y < height; y++) {
186 for (x = 0; x < width; x++) {
187 v = *src++ | 0x1010100;
188 v = (v - 0x707060) & 0x1010100;
192 * *dest++ = v & ((r<<16)|(g<<8)|b);
194 r = ((gint8) (filter->ripple[(filter->ry + y) * width * 2 + filter->rx +
195 x] + filter->phase * 2)) >> 7;
196 g = ((gint8) (filter->spiral[y * width + x] + filter->phase * 3)) >> 7;
197 b = ((gint8) (filter->ripple[(filter->by + y) * width * 2 + filter->bx +
198 x] - filter->phase)) >> 7;
199 *dest++ = v & ((r << 16) | (g << 8) | b);
204 if ((filter->rx + filter->rvx) < 0 || (filter->rx + filter->rvx) >= width)
205 filter->rvx = -filter->rvx;
206 if ((filter->ry + filter->rvy) < 0 || (filter->ry + filter->rvy) >= height)
207 filter->rvy = -filter->rvy;
208 if ((filter->bx + filter->bvx) < 0 || (filter->bx + filter->bvx) >= width)
209 filter->bvx = -filter->bvx;
210 if ((filter->by + filter->bvy) < 0 || (filter->by + filter->bvy) >= height)
211 filter->bvy = -filter->bvy;
212 filter->rx += filter->rvx;
213 filter->ry += filter->rvy;
214 filter->bx += filter->bvx;
215 filter->by += filter->bvy;
216 GST_OBJECT_UNLOCK (filter);
222 gst_shagadelictv_finalize (GObject * object)
224 GstShagadelicTV *filter = GST_SHAGADELICTV (object);
227 g_free (filter->ripple);
228 filter->ripple = NULL;
231 g_free (filter->spiral);
232 filter->spiral = NULL;
234 G_OBJECT_CLASS (parent_class)->finalize (object);
238 gst_shagadelictv_base_init (gpointer g_class)
240 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
242 gst_element_class_set_details_simple (element_class, "ShagadelicTV",
243 "Filter/Effect/Video",
244 "Oh behave, ShagedelicTV makes images shagadelic!",
245 "Wim Taymans <wim.taymans@chello.be>");
247 gst_element_class_add_pad_template (element_class,
248 gst_static_pad_template_get (&gst_shagadelictv_sink_template));
249 gst_element_class_add_pad_template (element_class,
250 gst_static_pad_template_get (&gst_shagadelictv_src_template));
254 gst_shagadelictv_class_init (GstShagadelicTVClass * klass)
256 GObjectClass *gobject_class = (GObjectClass *) klass;
257 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
259 gobject_class->finalize = gst_shagadelictv_finalize;
261 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_shagadelictv_set_caps);
262 trans_class->transform = GST_DEBUG_FUNCPTR (gst_shagadelictv_transform);
266 gst_shagadelictv_init (GstShagadelicTV * filter, GstShagadelicTVClass * klass)
268 filter->ripple = NULL;
269 filter->spiral = NULL;
271 gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (filter));
272 gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (filter));