2 * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * EffecTV - Realtime Digital Video Effector
5 * Copyright (C) 2001-2006 FUKUCHI Kentaro
7 * OpTV - Optical art meets real-time video effect.
8 * Copyright (C) 2004-2005 FUKUCHI Kentaro
10 * EffecTV is free software. This library is free software;
11 * you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
28 * SECTION:element-optv
30 * Traditional black-white optical animation is now resurrected as a
31 * real-time video effect. Input images are binarized and combined with
32 * various optical pattern.
35 * <title>Example launch line</title>
37 * gst-launch -v videotestsrc ! optv ! ffmpegcolorspace ! autovideosink
38 * ]| This pipeline shows the effect of optv on a test stream.
50 #include "gsteffectv.h"
52 #include <gst/video/video.h>
53 #include <gst/controller/gstcontroller.h>
63 #define GST_TYPE_OPTV_MODE (gst_optv_mode_get_type())
65 gst_optv_mode_get_type (void)
67 static GType type = 0;
69 static const GEnumValue enumvalue[] = {
70 {OP_SPIRAL1, "Maelstrom", "maelstrom"},
71 {OP_SPIRAL2, "Radiation", "radiation"},
72 {OP_PARABOLA, "Horizontal Stripes",
73 "horizontal-stripes"},
74 {OP_HSTRIPE, "Vertical Stripes", "vertical-stripes"},
79 type = g_enum_register_static ("GstOpTVMode", enumvalue);
84 #define DEFAULT_MODE OP_SPIRAL1
85 #define DEFAULT_SPEED 16
86 #define DEFAULT_THRESHOLD 60
96 static guint32 palette[256];
98 GST_BOILERPLATE (GstOpTV, gst_optv, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
100 static GstStaticPadTemplate gst_optv_src_template =
101 GST_STATIC_PAD_TEMPLATE ("src",
104 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx "; " GST_VIDEO_CAPS_RGBx)
107 static GstStaticPadTemplate gst_optv_sink_template =
108 GST_STATIC_PAD_TEMPLATE ("sink",
111 GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx "; " GST_VIDEO_CAPS_RGBx)
120 for (i = 0; i < 112; i++) {
122 palette[i + 128] = 0xffffff;
124 for (i = 0; i < 16; i++) {
125 v = 16 * (i + 1) - 1;
126 palette[i + 112] = (v << 16) | (v << 8) | v;
128 palette[i + 240] = (v << 16) | (v << 8) | v;
133 setOpmap (gint8 * opmap[4], gint width, gint height)
137 gdouble xx, yy, r, at, rr;
139 gfloat xx, yy, r, at, rr;
145 for (y = 0; y < height; y++) {
146 yy = (gdouble) (y - height / 2) / width;
147 for (x = 0; x < width; x++) {
148 xx = (gdouble) x / width - 0.5;
150 r = sqrt (xx * xx + yy * yy);
153 r = sqrtf (xx * xx + yy * yy);
154 at = atan2f (xx, yy);
157 opmap[OP_SPIRAL1][i] = ((guint)
158 ((at / M_PI * 256) + (r * 4000))) & 255;
161 rr = r * 300 - j * 32;
163 j += (rr > 28) ? (rr - 28) * 16 : 0;
164 opmap[OP_SPIRAL2][i] = ((guint)
165 ((at / M_PI * 4096) + (r * 1600) - j)) & 255;
167 opmap[OP_PARABOLA][i] =
168 ((guint) (yy / (xx * xx * 0.3 + 0.1) * 400)) & 255;
169 opmap[OP_HSTRIPE][i] = x * 8 * sci;
175 /* Taken from effectv/image.c */
176 /* Y value filters */
178 image_y_over (guint32 * src, guint8 * diff, gint y_threshold, gint video_area)
184 for (i = video_area; i > 0; i--) {
185 R = ((*src) & 0xff0000) >> (16 - 1);
186 G = ((*src) & 0xff00) >> (8 - 2);
188 v = y_threshold * 7 - (R + G + B);
189 *p = (guint8) (v >> 24);
196 gst_optv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
198 GstOpTV *filter = GST_OPTV (trans);
200 GstFlowReturn ret = GST_FLOW_OK;
204 GstClockTime timestamp, stream_time;
206 timestamp = GST_BUFFER_TIMESTAMP (in);
208 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
210 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
211 GST_TIME_ARGS (timestamp));
213 if (GST_CLOCK_TIME_IS_VALID (stream_time))
214 gst_object_sync_values (G_OBJECT (filter), stream_time);
216 src = (guint32 *) GST_BUFFER_DATA (in);
217 dest = (guint32 *) GST_BUFFER_DATA (out);
219 if (G_UNLIKELY (filter->opmap[0] == NULL))
220 return GST_FLOW_NOT_NEGOTIATED;
222 switch (filter->mode) {
225 p = filter->opmap[OP_SPIRAL1];
228 p = filter->opmap[OP_SPIRAL2];
231 p = filter->opmap[OP_PARABOLA];
234 p = filter->opmap[OP_HSTRIPE];
238 filter->phase -= filter->speed;
241 image_y_over (src, diff, filter->threshold, filter->width * filter->height);
243 for (y = 0; y < filter->height; y++) {
244 for (x = 0; x < filter->width; x++) {
245 *dest++ = palette[(((guint8) (*p + filter->phase)) ^ *diff++) & 255];
254 gst_optv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
257 GstOpTV *filter = GST_OPTV (btrans);
258 GstStructure *structure;
259 gboolean ret = FALSE;
261 structure = gst_caps_get_structure (incaps, 0);
263 if (gst_structure_get_int (structure, "width", &filter->width) &&
264 gst_structure_get_int (structure, "height", &filter->height)) {
267 for (i = 0; i < 4; i++) {
268 if (filter->opmap[i])
269 g_free (filter->opmap[i]);
270 filter->opmap[i] = g_new (gint8, filter->width * filter->height);
272 setOpmap (filter->opmap, filter->width, filter->height);
275 g_free (filter->diff);
276 filter->diff = g_new (guint8, filter->width * filter->height);
285 gst_optv_start (GstBaseTransform * trans)
287 GstOpTV *filter = GST_OPTV (trans);
295 gst_optv_finalize (GObject * object)
297 GstOpTV *filter = GST_OPTV (object);
299 if (filter->opmap[0]) {
302 for (i = 0; i < 4; i++) {
303 if (filter->opmap[i])
304 g_free (filter->opmap[i]);
305 filter->opmap[i] = NULL;
310 g_free (filter->diff);
313 G_OBJECT_CLASS (parent_class)->finalize (object);
317 gst_optv_set_property (GObject * object, guint prop_id, const GValue * value,
320 GstOpTV *filter = GST_OPTV (object);
324 filter->mode = g_value_get_enum (value);
327 filter->speed = g_value_get_int (value);
330 filter->threshold = g_value_get_int (value);
333 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
339 gst_optv_get_property (GObject * object, guint prop_id, GValue * value,
342 GstOpTV *filter = GST_OPTV (object);
346 g_value_set_enum (value, filter->mode);
349 g_value_set_int (value, filter->speed);
352 g_value_set_int (value, filter->threshold);
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361 gst_optv_base_init (gpointer g_class)
363 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
365 gst_element_class_set_details_simple (element_class, "OpTV effect",
366 "Filter/Effect/Video",
367 "Optical art meets real-time video effect",
368 "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
369 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
371 gst_element_class_add_pad_template (element_class,
372 gst_static_pad_template_get (&gst_optv_sink_template));
373 gst_element_class_add_pad_template (element_class,
374 gst_static_pad_template_get (&gst_optv_src_template));
378 gst_optv_class_init (GstOpTVClass * klass)
380 GObjectClass *gobject_class = (GObjectClass *) klass;
381 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
383 gobject_class->set_property = gst_optv_set_property;
384 gobject_class->get_property = gst_optv_get_property;
386 gobject_class->finalize = gst_optv_finalize;
388 g_object_class_install_property (gobject_class, PROP_MODE,
389 g_param_spec_enum ("mode", "Mode",
390 "Mode", GST_TYPE_OPTV_MODE, DEFAULT_MODE,
391 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
393 g_object_class_install_property (gobject_class, PROP_SPEED,
394 g_param_spec_int ("speed", "Speed",
395 "Effect speed", G_MININT, G_MAXINT, DEFAULT_SPEED,
396 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 g_object_class_install_property (gobject_class, PROP_THRESHOLD,
399 g_param_spec_int ("threshold", "Threshold",
400 "Luma threshold", G_MININT, G_MAXINT, DEFAULT_THRESHOLD,
401 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
403 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_optv_set_caps);
404 trans_class->transform = GST_DEBUG_FUNCPTR (gst_optv_transform);
405 trans_class->start = GST_DEBUG_FUNCPTR (gst_optv_start);
411 gst_optv_init (GstOpTV * filter, GstOpTVClass * klass)
413 filter->speed = DEFAULT_SPEED;
414 filter->mode = DEFAULT_MODE;
415 filter->threshold = DEFAULT_THRESHOLD;