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 (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
204 GstOpTV *filter = GST_OPTV (trans);
206 GstVideoFrame in_frame, out_frame;
209 gint x, y, width, height;
210 GstClockTime timestamp, stream_time;
213 timestamp = GST_BUFFER_TIMESTAMP (in);
215 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
217 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
218 GST_TIME_ARGS (timestamp));
220 if (GST_CLOCK_TIME_IS_VALID (stream_time))
221 gst_object_sync_values (GST_OBJECT (filter), stream_time);
223 if (G_UNLIKELY (filter->opmap[0] == NULL))
224 return GST_FLOW_NOT_NEGOTIATED;
226 if (!gst_video_frame_map (&in_frame, &filter->info, in, GST_MAP_READ))
229 if (!gst_video_frame_map (&out_frame, &filter->info, out, GST_MAP_WRITE))
232 src = GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
233 dest = GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
235 width = GST_VIDEO_FRAME_WIDTH (&in_frame);
236 height = GST_VIDEO_FRAME_HEIGHT (&in_frame);
238 GST_OBJECT_LOCK (filter);
239 switch (filter->mode) {
242 p = filter->opmap[OP_SPIRAL1];
245 p = filter->opmap[OP_SPIRAL2];
248 p = filter->opmap[OP_PARABOLA];
251 p = filter->opmap[OP_HSTRIPE];
255 filter->phase -= filter->speed;
258 image_y_over (src, diff, filter->threshold, width * height);
259 phase = filter->phase;
261 for (y = 0; y < height; y++) {
262 for (x = 0; x < width; x++) {
263 *dest++ = palette[(((guint8) (*p + phase)) ^ *diff++) & 255];
267 GST_OBJECT_UNLOCK (filter);
269 gst_video_frame_unmap (&in_frame);
270 gst_video_frame_unmap (&out_frame);
277 GST_DEBUG_OBJECT (filter, "invalid input frame");
278 return GST_FLOW_ERROR;
282 GST_DEBUG_OBJECT (filter, "invalid output frame");
283 gst_video_frame_unmap (&in_frame);
284 return GST_FLOW_ERROR;
289 gst_optv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
292 GstOpTV *filter = GST_OPTV (btrans);
294 gint i, width, height;
296 if (!gst_video_info_from_caps (&info, incaps))
301 width = GST_VIDEO_INFO_WIDTH (&info);
302 height = GST_VIDEO_INFO_HEIGHT (&info);
304 for (i = 0; i < 4; i++) {
305 if (filter->opmap[i])
306 g_free (filter->opmap[i]);
307 filter->opmap[i] = g_new (gint8, width * height);
309 setOpmap (filter->opmap, width, height);
312 g_free (filter->diff);
313 filter->diff = g_new (guint8, width * height);
320 GST_DEBUG_OBJECT (filter, "invalid caps received");
326 gst_optv_start (GstBaseTransform * trans)
328 GstOpTV *filter = GST_OPTV (trans);
336 gst_optv_finalize (GObject * object)
338 GstOpTV *filter = GST_OPTV (object);
340 if (filter->opmap[0]) {
343 for (i = 0; i < 4; i++) {
344 if (filter->opmap[i])
345 g_free (filter->opmap[i]);
346 filter->opmap[i] = NULL;
351 g_free (filter->diff);
354 G_OBJECT_CLASS (parent_class)->finalize (object);
358 gst_optv_set_property (GObject * object, guint prop_id, const GValue * value,
361 GstOpTV *filter = GST_OPTV (object);
363 GST_OBJECT_LOCK (filter);
366 filter->mode = g_value_get_enum (value);
369 filter->speed = g_value_get_int (value);
372 filter->threshold = g_value_get_uint (value);
375 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
378 GST_OBJECT_UNLOCK (filter);
382 gst_optv_get_property (GObject * object, guint prop_id, GValue * value,
385 GstOpTV *filter = GST_OPTV (object);
389 g_value_set_enum (value, filter->mode);
392 g_value_set_int (value, filter->speed);
395 g_value_set_uint (value, filter->threshold);
398 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
404 gst_optv_class_init (GstOpTVClass * klass)
406 GObjectClass *gobject_class = (GObjectClass *) klass;
407 GstElementClass *gstelement_class = (GstElementClass *) klass;
408 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
410 gobject_class->set_property = gst_optv_set_property;
411 gobject_class->get_property = gst_optv_get_property;
413 gobject_class->finalize = gst_optv_finalize;
415 g_object_class_install_property (gobject_class, PROP_MODE,
416 g_param_spec_enum ("mode", "Mode",
417 "Mode", GST_TYPE_OPTV_MODE, DEFAULT_MODE,
418 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
420 g_object_class_install_property (gobject_class, PROP_SPEED,
421 g_param_spec_int ("speed", "Speed",
422 "Effect speed", G_MININT, G_MAXINT, DEFAULT_SPEED,
423 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
425 g_object_class_install_property (gobject_class, PROP_THRESHOLD,
426 g_param_spec_uint ("threshold", "Threshold",
427 "Luma threshold", 0, G_MAXINT, DEFAULT_THRESHOLD,
428 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 gst_element_class_set_details_simple (gstelement_class, "OpTV effect",
431 "Filter/Effect/Video",
432 "Optical art meets real-time video effect",
433 "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
434 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
436 gst_element_class_add_pad_template (gstelement_class,
437 gst_static_pad_template_get (&gst_optv_sink_template));
438 gst_element_class_add_pad_template (gstelement_class,
439 gst_static_pad_template_get (&gst_optv_src_template));
441 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_optv_set_caps);
442 trans_class->transform = GST_DEBUG_FUNCPTR (gst_optv_transform);
443 trans_class->start = GST_DEBUG_FUNCPTR (gst_optv_start);
449 gst_optv_init (GstOpTV * filter)
451 filter->speed = DEFAULT_SPEED;
452 filter->mode = DEFAULT_MODE;
453 filter->threshold = DEFAULT_THRESHOLD;