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 ! videoconvert ! autovideosink
38 * ]| This pipeline shows the effect of optv on a test stream.
50 #include "gsteffectv.h"
52 #include <gst/video/video.h>
62 #define GST_TYPE_OPTV_MODE (gst_optv_mode_get_type())
64 gst_optv_mode_get_type (void)
66 static GType type = 0;
68 static const GEnumValue enumvalue[] = {
69 {OP_SPIRAL1, "Maelstrom", "maelstrom"},
70 {OP_SPIRAL2, "Radiation", "radiation"},
71 {OP_PARABOLA, "Horizontal Stripes",
72 "horizontal-stripes"},
73 {OP_HSTRIPE, "Vertical Stripes", "vertical-stripes"},
78 type = g_enum_register_static ("GstOpTVMode", enumvalue);
83 #define DEFAULT_MODE OP_SPIRAL1
84 #define DEFAULT_SPEED 16
85 #define DEFAULT_THRESHOLD 60
95 static guint32 palette[256];
97 #define gst_optv_parent_class parent_class
98 G_DEFINE_TYPE (GstOpTV, gst_optv, GST_TYPE_VIDEO_FILTER);
100 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
101 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ BGRx, RGBx }")
103 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ xBGR, xRGB }")
106 static GstStaticPadTemplate gst_optv_src_template =
107 GST_STATIC_PAD_TEMPLATE ("src",
110 GST_STATIC_CAPS (CAPS_STR)
113 static GstStaticPadTemplate gst_optv_sink_template =
114 GST_STATIC_PAD_TEMPLATE ("sink",
117 GST_STATIC_CAPS (CAPS_STR)
126 for (i = 0; i < 112; i++) {
128 palette[i + 128] = 0xffffff;
130 for (i = 0; i < 16; i++) {
131 v = 16 * (i + 1) - 1;
132 palette[i + 112] = (v << 16) | (v << 8) | v;
134 palette[i + 240] = (v << 16) | (v << 8) | v;
139 setOpmap (gint8 * opmap[4], gint width, gint height)
143 gdouble xx, yy, r, at, rr;
145 gfloat xx, yy, r, at, rr;
151 for (y = 0; y < height; y++) {
152 yy = (gdouble) (y - height / 2) / width;
153 for (x = 0; x < width; x++) {
154 xx = (gdouble) x / width - 0.5;
156 r = sqrt (xx * xx + yy * yy);
159 r = sqrtf (xx * xx + yy * yy);
160 at = atan2f (xx, yy);
163 opmap[OP_SPIRAL1][i] = ((guint)
164 ((at / G_PI * 256) + (r * 4000))) & 255;
167 rr = r * 300 - j * 32;
169 j += (rr > 28) ? (rr - 28) * 16 : 0;
170 opmap[OP_SPIRAL2][i] = ((guint)
171 ((at / G_PI * 4096) + (r * 1600) - j)) & 255;
173 opmap[OP_PARABOLA][i] =
174 ((guint) (yy / (xx * xx * 0.3 + 0.1) * 400)) & 255;
175 opmap[OP_HSTRIPE][i] = x * 8 * sci;
181 /* Taken from effectv/image.c */
182 /* Y value filters */
184 image_y_over (guint32 * src, guint8 * diff, gint y_threshold, gint video_area)
190 for (i = video_area; i > 0; i--) {
191 R = ((*src) & 0xff0000) >> (16 - 1);
192 G = ((*src) & 0xff00) >> (8 - 2);
194 v = y_threshold * 7 - (R + G + B);
195 *p = (guint8) (v >> 24);
202 gst_optv_transform_frame (GstVideoFilter * vfilter, GstVideoFrame * in_frame,
203 GstVideoFrame * out_frame)
205 GstOpTV *filter = GST_OPTV (vfilter);
209 gint x, y, width, height;
210 GstClockTime timestamp, stream_time;
213 timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
215 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (vfilter)->segment,
216 GST_FORMAT_TIME, timestamp);
218 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
219 GST_TIME_ARGS (timestamp));
221 if (GST_CLOCK_TIME_IS_VALID (stream_time))
222 gst_object_sync_values (GST_OBJECT (filter), stream_time);
224 if (G_UNLIKELY (filter->opmap[0] == NULL))
225 return GST_FLOW_NOT_NEGOTIATED;
227 src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
228 dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
230 width = GST_VIDEO_FRAME_WIDTH (in_frame);
231 height = GST_VIDEO_FRAME_HEIGHT (in_frame);
233 GST_OBJECT_LOCK (filter);
234 switch (filter->mode) {
237 p = filter->opmap[OP_SPIRAL1];
240 p = filter->opmap[OP_SPIRAL2];
243 p = filter->opmap[OP_PARABOLA];
246 p = filter->opmap[OP_HSTRIPE];
250 filter->phase -= filter->speed;
253 image_y_over (src, diff, filter->threshold, width * height);
254 phase = filter->phase;
256 for (y = 0; y < height; y++) {
257 for (x = 0; x < width; x++) {
258 *dest++ = palette[(((guint8) (*p + phase)) ^ *diff++) & 255];
262 GST_OBJECT_UNLOCK (filter);
268 gst_optv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
269 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
271 GstOpTV *filter = GST_OPTV (vfilter);
272 gint i, width, height;
274 width = GST_VIDEO_INFO_WIDTH (in_info);
275 height = GST_VIDEO_INFO_HEIGHT (in_info);
277 for (i = 0; i < 4; i++) {
278 if (filter->opmap[i])
279 g_free (filter->opmap[i]);
280 filter->opmap[i] = g_new (gint8, width * height);
282 setOpmap (filter->opmap, width, height);
285 g_free (filter->diff);
286 filter->diff = g_new (guint8, width * height);
292 gst_optv_start (GstBaseTransform * trans)
294 GstOpTV *filter = GST_OPTV (trans);
302 gst_optv_finalize (GObject * object)
304 GstOpTV *filter = GST_OPTV (object);
306 if (filter->opmap[0]) {
309 for (i = 0; i < 4; i++) {
310 if (filter->opmap[i])
311 g_free (filter->opmap[i]);
312 filter->opmap[i] = NULL;
317 g_free (filter->diff);
320 G_OBJECT_CLASS (parent_class)->finalize (object);
324 gst_optv_set_property (GObject * object, guint prop_id, const GValue * value,
327 GstOpTV *filter = GST_OPTV (object);
329 GST_OBJECT_LOCK (filter);
332 filter->mode = g_value_get_enum (value);
335 filter->speed = g_value_get_int (value);
338 filter->threshold = g_value_get_uint (value);
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344 GST_OBJECT_UNLOCK (filter);
348 gst_optv_get_property (GObject * object, guint prop_id, GValue * value,
351 GstOpTV *filter = GST_OPTV (object);
355 g_value_set_enum (value, filter->mode);
358 g_value_set_int (value, filter->speed);
361 g_value_set_uint (value, filter->threshold);
364 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
370 gst_optv_class_init (GstOpTVClass * klass)
372 GObjectClass *gobject_class = (GObjectClass *) klass;
373 GstElementClass *gstelement_class = (GstElementClass *) klass;
374 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
375 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
377 gobject_class->set_property = gst_optv_set_property;
378 gobject_class->get_property = gst_optv_get_property;
380 gobject_class->finalize = gst_optv_finalize;
382 g_object_class_install_property (gobject_class, PROP_MODE,
383 g_param_spec_enum ("mode", "Mode",
384 "Mode", GST_TYPE_OPTV_MODE, DEFAULT_MODE,
385 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
387 g_object_class_install_property (gobject_class, PROP_SPEED,
388 g_param_spec_int ("speed", "Speed",
389 "Effect speed", G_MININT, G_MAXINT, DEFAULT_SPEED,
390 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392 g_object_class_install_property (gobject_class, PROP_THRESHOLD,
393 g_param_spec_uint ("threshold", "Threshold",
394 "Luma threshold", 0, G_MAXINT, DEFAULT_THRESHOLD,
395 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
397 gst_element_class_set_details_simple (gstelement_class, "OpTV effect",
398 "Filter/Effect/Video",
399 "Optical art meets real-time video effect",
400 "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
401 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
403 gst_element_class_add_pad_template (gstelement_class,
404 gst_static_pad_template_get (&gst_optv_sink_template));
405 gst_element_class_add_pad_template (gstelement_class,
406 gst_static_pad_template_get (&gst_optv_src_template));
408 trans_class->start = GST_DEBUG_FUNCPTR (gst_optv_start);
410 vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_optv_set_info);
411 vfilter_class->transform_frame = GST_DEBUG_FUNCPTR (gst_optv_transform_frame);
417 gst_optv_init (GstOpTV * filter)
419 filter->speed = DEFAULT_SPEED;
420 filter->mode = DEFAULT_MODE;
421 filter->threshold = DEFAULT_THRESHOLD;