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 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
101 #define CAPS_STR GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_RGBx
103 #define CAPS_STR GST_VIDEO_CAPS_xBGR ";" GST_VIDEO_CAPS_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 GstFlowReturn ret = GST_FLOW_OK;
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 (G_OBJECT (filter), stream_time);
223 src = (guint32 *) GST_BUFFER_DATA (in);
224 dest = (guint32 *) GST_BUFFER_DATA (out);
226 if (G_UNLIKELY (filter->opmap[0] == NULL))
227 return GST_FLOW_NOT_NEGOTIATED;
229 GST_OBJECT_LOCK (filter);
230 switch (filter->mode) {
233 p = filter->opmap[OP_SPIRAL1];
236 p = filter->opmap[OP_SPIRAL2];
239 p = filter->opmap[OP_PARABOLA];
242 p = filter->opmap[OP_HSTRIPE];
246 filter->phase -= filter->speed;
249 image_y_over (src, diff, filter->threshold, filter->width * filter->height);
250 height = filter->height;
251 width = filter->width;
252 phase = filter->phase;
254 for (y = 0; y < height; y++) {
255 for (x = 0; x < width; x++) {
256 *dest++ = palette[(((guint8) (*p + phase)) ^ *diff++) & 255];
260 GST_OBJECT_UNLOCK (filter);
266 gst_optv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
269 GstOpTV *filter = GST_OPTV (btrans);
270 GstStructure *structure;
271 gboolean ret = FALSE;
273 structure = gst_caps_get_structure (incaps, 0);
275 GST_OBJECT_LOCK (filter);
276 if (gst_structure_get_int (structure, "width", &filter->width) &&
277 gst_structure_get_int (structure, "height", &filter->height)) {
280 for (i = 0; i < 4; i++) {
281 if (filter->opmap[i])
282 g_free (filter->opmap[i]);
283 filter->opmap[i] = g_new (gint8, filter->width * filter->height);
285 setOpmap (filter->opmap, filter->width, filter->height);
288 g_free (filter->diff);
289 filter->diff = g_new (guint8, filter->width * filter->height);
293 GST_OBJECT_UNLOCK (filter);
299 gst_optv_start (GstBaseTransform * trans)
301 GstOpTV *filter = GST_OPTV (trans);
309 gst_optv_finalize (GObject * object)
311 GstOpTV *filter = GST_OPTV (object);
313 if (filter->opmap[0]) {
316 for (i = 0; i < 4; i++) {
317 if (filter->opmap[i])
318 g_free (filter->opmap[i]);
319 filter->opmap[i] = NULL;
324 g_free (filter->diff);
327 G_OBJECT_CLASS (parent_class)->finalize (object);
331 gst_optv_set_property (GObject * object, guint prop_id, const GValue * value,
334 GstOpTV *filter = GST_OPTV (object);
336 GST_OBJECT_LOCK (filter);
339 filter->mode = g_value_get_enum (value);
342 filter->speed = g_value_get_int (value);
345 filter->threshold = g_value_get_uint (value);
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
351 GST_OBJECT_UNLOCK (filter);
355 gst_optv_get_property (GObject * object, guint prop_id, GValue * value,
358 GstOpTV *filter = GST_OPTV (object);
362 g_value_set_enum (value, filter->mode);
365 g_value_set_int (value, filter->speed);
368 g_value_set_uint (value, filter->threshold);
371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
377 gst_optv_base_init (gpointer g_class)
379 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
381 gst_element_class_set_details_simple (element_class, "OpTV effect",
382 "Filter/Effect/Video",
383 "Optical art meets real-time video effect",
384 "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
385 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
387 gst_element_class_add_pad_template (element_class,
388 gst_static_pad_template_get (&gst_optv_sink_template));
389 gst_element_class_add_pad_template (element_class,
390 gst_static_pad_template_get (&gst_optv_src_template));
394 gst_optv_class_init (GstOpTVClass * klass)
396 GObjectClass *gobject_class = (GObjectClass *) klass;
397 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
399 gobject_class->set_property = gst_optv_set_property;
400 gobject_class->get_property = gst_optv_get_property;
402 gobject_class->finalize = gst_optv_finalize;
404 g_object_class_install_property (gobject_class, PROP_MODE,
405 g_param_spec_enum ("mode", "Mode",
406 "Mode", GST_TYPE_OPTV_MODE, DEFAULT_MODE,
407 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409 g_object_class_install_property (gobject_class, PROP_SPEED,
410 g_param_spec_int ("speed", "Speed",
411 "Effect speed", G_MININT, G_MAXINT, DEFAULT_SPEED,
412 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
414 g_object_class_install_property (gobject_class, PROP_THRESHOLD,
415 g_param_spec_uint ("threshold", "Threshold",
416 "Luma threshold", 0, G_MAXINT, DEFAULT_THRESHOLD,
417 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
419 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_optv_set_caps);
420 trans_class->transform = GST_DEBUG_FUNCPTR (gst_optv_transform);
421 trans_class->start = GST_DEBUG_FUNCPTR (gst_optv_start);
427 gst_optv_init (GstOpTV * filter, GstOpTVClass * klass)
429 filter->speed = DEFAULT_SPEED;
430 filter->mode = DEFAULT_MODE;
431 filter->threshold = DEFAULT_THRESHOLD;