2 * Cradioacyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * EffecTV - Realtime Digital Video Effector
5 * Cradioacyright (C) 2001-2006 FUKUCHI Kentaro
7 * RadioacTV - motion-enlightment effect.
8 * Cradioacyright (C) 2001-2002 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 radioaction) any later version.
16 * This library is distributed in the hradioace 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 cradioacy 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-radioactv
30 * RadioacTV does *NOT* detect a radioactivity. It detects a difference
31 * from previous frame and blurs it.
33 * RadioacTV has 4 mode, normal, strobe1, strobe2 and trigger.
34 * In trigger mode, effect appears only when the trigger property is %TRUE.
36 * strobe1 and strobe2 mode drops some frames. strobe1 mode uses the difference between
37 * current frame and previous frame dropped, while strobe2 mode uses the difference from
38 * previous frame displayed. The effect of strobe2 is stronger than strobe1.
41 * <title>Example launch line</title>
43 * gst-launch -v videotestsrc ! radioactv ! ffmpegcolorspace ! autovideosink
44 * ]| This pipeline shows the effect of radioactv on a test stream.
55 #include "gstradioac.h"
56 #include "gsteffectv.h"
58 #include <gst/video/video.h>
59 #include <gst/controller/gstcontroller.h>
77 #define GST_TYPE_RADIOACTV_MODE (gst_radioactv_mode_get_type())
79 gst_radioactv_mode_get_type (void)
81 static GType type = 0;
83 static const GEnumValue enumvalue[] = {
84 {RADIOAC_NORMAL, "Normal", "normal"},
85 {RADIOAC_STROBE, "Strobe 1", "strobe1"},
86 {RADIOAC_STROBE2, "Strobe 2", "strobe2"},
87 {RADIOAC_TRIGGER, "Trigger", "trigger"},
92 type = g_enum_register_static ("GstRadioacTVMode", enumvalue);
97 #define GST_TYPE_RADIOACTV_COLOR (gst_radioactv_color_get_type())
99 gst_radioactv_color_get_type (void)
101 static GType type = 0;
103 static const GEnumValue enumvalue[] = {
104 {COLOR_RED, "Red", "red"},
105 {COLOR_GREEN, "Green", "green"},
106 {COLOR_BLUE, "Blue", "blue"},
107 {COLOR_WHITE, "White", "white"},
112 type = g_enum_register_static ("GstRadioacTVColor", enumvalue);
117 #define DEFAULT_MODE RADIOAC_NORMAL
118 #define DEFAULT_COLOR COLOR_WHITE
119 #define DEFAULT_INTERVAL 3
120 #define DEFAULT_TRIGGER FALSE
133 #define MAGIC_THRESHOLD 40
136 static guint32 palettes[COLORS * PATTERN];
137 static gint swap_tab[] = { 2, 1, 0, 3 };
139 GST_BOILERPLATE (GstRadioacTV, gst_radioactv, GstVideoFilter,
140 GST_TYPE_VIDEO_FILTER);
142 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
143 #define CAPS_STR GST_VIDEO_CAPS_RGBx "; " GST_VIDEO_CAPS_BGRx
145 #define CAPS_STR GST_VIDEO_CAPS_xBGR "; " GST_VIDEO_CAPS_xRGB
148 static GstStaticPadTemplate gst_radioactv_src_template =
149 GST_STATIC_PAD_TEMPLATE ("src",
152 GST_STATIC_CAPS (CAPS_STR)
155 static GstStaticPadTemplate gst_radioactv_sink_template =
156 GST_STATIC_PAD_TEMPLATE ("sink",
159 GST_STATIC_CAPS (CAPS_STR)
167 #define DELTA (255/(COLORS/2-1))
169 /* red, gree, blue */
170 for (i = 0; i < COLORS / 2; i++) {
171 palettes[i] = i * DELTA;
172 palettes[COLORS + i] = (i * DELTA) << 8;
173 palettes[COLORS * 2 + i] = (i * DELTA) << 16;
175 for (i = 0; i < COLORS / 2; i++) {
176 palettes[i + COLORS / 2] = 255 | (i * DELTA) << 16 | (i * DELTA) << 8;
177 palettes[COLORS + i + COLORS / 2] =
178 (255 << 8) | (i * DELTA) << 16 | i * DELTA;
179 palettes[COLORS * 2 + i + COLORS / 2] =
180 (255 << 16) | (i * DELTA) << 8 | i * DELTA;
183 for (i = 0; i < COLORS; i++) {
184 palettes[COLORS * 3 + i] = (255 * i / COLORS) * 0x10101;
186 for (i = 0; i < COLORS * PATTERN; i++) {
187 palettes[i] = palettes[i] & 0xfefeff;
192 #define VIDEO_HWIDTH (filter->buf_width/2)
193 #define VIDEO_HHEIGHT (filter->buf_height/2)
195 /* this table assumes that video_width is times of 32 */
197 setTable (GstRadioacTV * filter)
200 gint x, y, tx, ty, xx;
203 prevptr = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
204 for (xx = 0; xx < (filter->buf_width_blocks); xx++) {
206 for (x = 0; x < 32; x++) {
207 ptr = (gint) (0.5 + RATIO * (xx * 32 + x - VIDEO_HWIDTH) + VIDEO_HWIDTH);
213 filter->blurzoomx[xx] = bits;
216 ty = (gint) (0.5 + RATIO * (-VIDEO_HHEIGHT) + VIDEO_HHEIGHT);
217 tx = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
218 xx = (gint) (0.5 + RATIO * (filter->buf_width - 1 - VIDEO_HWIDTH) +
220 filter->blurzoomy[0] = ty * filter->buf_width + tx;
221 prevptr = ty * filter->buf_width + xx;
222 for (y = 1; y < filter->buf_height; y++) {
223 ty = (gint) (0.5 + RATIO * (y - VIDEO_HHEIGHT) + VIDEO_HHEIGHT);
224 filter->blurzoomy[y] = ty * filter->buf_width + tx - prevptr;
225 prevptr = ty * filter->buf_width + xx;
233 blur (GstRadioacTV * filter)
240 width = filter->buf_width;
241 p = filter->blurzoombuf + filter->width + 1;
242 q = p + filter->buf_area;
244 for (y = filter->buf_height - 2; y > 0; y--) {
245 for (x = width - 2; x > 0; x--) {
246 v = (*(p - width) + *(p - 1) + *(p + 1) + *(p + width)) / 4 - 1;
259 zoom (GstRadioacTV * filter)
266 p = filter->blurzoombuf + filter->buf_area;
267 q = filter->blurzoombuf;
268 height = filter->buf_height;
269 blocks = filter->buf_width_blocks;
271 for (y = 0; y < height; y++) {
272 p += filter->blurzoomy[y];
273 for (b = 0; b < blocks; b++) {
274 dx = filter->blurzoomx[b];
275 for (x = 0; x < 32; x++) {
285 blurzoomcore (GstRadioacTV * filter)
291 /* Background image is refreshed every frame */
293 image_bgsubtract_update_y (guint32 * src, gint16 * background, guint8 * diff,
294 gint video_area, gint y_threshold)
306 for (i = 0; i < video_area; i++) {
307 R = ((*p) & 0xff0000) >> (16 - 1);
308 G = ((*p) & 0xff00) >> (8 - 2);
310 v = (R + G + B) - (gint) (*q);
311 *q = (gint16) (R + G + B);
312 *r = ((v + y_threshold) >> 24) | ((y_threshold - v) >> 24);
321 gst_radioactv_transform (GstBaseTransform * trans, GstBuffer * in,
324 GstRadioacTV *filter = GST_RADIOACTV (trans);
326 GstFlowReturn ret = GST_FLOW_OK;
327 GstClockTime timestamp, stream_time;
333 timestamp = GST_BUFFER_TIMESTAMP (in);
335 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
337 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
338 GST_TIME_ARGS (timestamp));
340 if (GST_CLOCK_TIME_IS_VALID (stream_time))
341 gst_object_sync_values (G_OBJECT (filter), stream_time);
343 src = (guint32 *) GST_BUFFER_DATA (in);
344 dest = (guint32 *) GST_BUFFER_DATA (out);
346 GST_OBJECT_LOCK (filter);
347 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
348 if (filter->format == GST_VIDEO_FORMAT_RGBx) {
349 palette = &palettes[COLORS * filter->color];
351 palette = &palettes[COLORS * swap_tab[filter->color]];
354 if (filter->format == GST_VIDEO_FORMAT_xBGR) {
355 palette = &palettes[COLORS * filter->color];
357 palette = &palettes[COLORS * swap_tab[filter->color]];
362 if (filter->mode == 3 && filter->trigger)
363 filter->snaptime = 0;
364 else if (filter->mode == 3 && !filter->trigger)
365 filter->snaptime = 1;
367 if (filter->mode != 2 || filter->snaptime <= 0) {
368 image_bgsubtract_update_y (src, filter->background, diff,
369 filter->width * filter->height, MAGIC_THRESHOLD * 7);
370 if (filter->mode == 0 || filter->snaptime <= 0) {
371 diff += filter->buf_margin_left;
372 p = filter->blurzoombuf;
373 for (y = 0; y < filter->buf_height; y++) {
374 for (x = 0; x < filter->buf_width; x++) {
375 p[x] |= diff[x] >> 3;
377 diff += filter->width;
378 p += filter->buf_width;
380 if (filter->mode == 1 || filter->mode == 2) {
381 memcpy (filter->snapframe, src, filter->width * filter->height * 4);
385 blurzoomcore (filter);
387 if (filter->mode == 1 || filter->mode == 2) {
388 src = filter->snapframe;
390 p = filter->blurzoombuf;
391 for (y = 0; y < filter->height; y++) {
392 for (x = 0; x < filter->buf_margin_left; x++) {
395 for (x = 0; x < filter->buf_width; x++) {
396 a = *src++ & 0xfefeff;
400 *dest++ = a | (b - (b >> 8));
402 for (x = 0; x < filter->buf_margin_right; x++) {
407 if (filter->mode == 1 || filter->mode == 2) {
409 if (filter->snaptime < 0) {
410 filter->snaptime = filter->interval;
413 GST_OBJECT_UNLOCK (filter);
419 gst_radioactv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
422 GstRadioacTV *filter = GST_RADIOACTV (btrans);
423 gboolean ret = FALSE;
425 GST_OBJECT_LOCK (filter);
427 if (gst_video_format_parse_caps (incaps, &filter->format, &filter->width,
429 filter->buf_width_blocks = filter->width / 32;
430 if (filter->buf_width_blocks > 255)
433 filter->buf_width = filter->buf_width_blocks * 32;
434 filter->buf_height = filter->height;
435 filter->buf_area = filter->buf_height * filter->buf_width;
436 filter->buf_margin_left = (filter->width - filter->buf_width) / 2;
437 filter->buf_margin_right =
438 filter->height - filter->buf_width - filter->buf_margin_left;
440 if (filter->blurzoombuf)
441 g_free (filter->blurzoombuf);
442 filter->blurzoombuf = g_new0 (guint8, filter->buf_area * 2);
444 if (filter->blurzoomx)
445 g_free (filter->blurzoomx);
446 filter->blurzoomx = g_new0 (gint, filter->buf_width);
448 if (filter->blurzoomy)
449 g_free (filter->blurzoomy);
450 filter->blurzoomy = g_new0 (gint, filter->buf_height);
452 if (filter->snapframe)
453 g_free (filter->snapframe);
454 filter->snapframe = g_new (guint32, filter->width * filter->height);
457 g_free (filter->diff);
458 filter->diff = g_new (guint8, filter->width * filter->height);
460 if (filter->background)
461 g_free (filter->background);
462 filter->background = g_new0 (gint16, filter->width * filter->height);
469 GST_OBJECT_UNLOCK (filter);
475 gst_radioactv_start (GstBaseTransform * trans)
477 GstRadioacTV *filter = GST_RADIOACTV (trans);
479 filter->snaptime = 0;
485 gst_radioactv_finalize (GObject * object)
487 GstRadioacTV *filter = GST_RADIOACTV (object);
489 if (filter->snapframe)
490 g_free (filter->snapframe);
491 filter->snapframe = NULL;
493 if (filter->blurzoombuf)
494 g_free (filter->blurzoombuf);
495 filter->blurzoombuf = NULL;
498 g_free (filter->diff);
501 if (filter->background)
502 g_free (filter->background);
503 filter->background = NULL;
505 if (filter->blurzoomx)
506 g_free (filter->blurzoomx);
507 filter->blurzoomx = NULL;
509 if (filter->blurzoomy)
510 g_free (filter->blurzoomy);
511 filter->blurzoomy = NULL;
513 G_OBJECT_CLASS (parent_class)->finalize (object);
517 gst_radioactv_set_property (GObject * object, guint prop_id,
518 const GValue * value, GParamSpec * pspec)
520 GstRadioacTV *filter = GST_RADIOACTV (object);
522 GST_OBJECT_LOCK (filter);
525 filter->mode = g_value_get_enum (value);
526 if (filter->mode == 3)
527 filter->snaptime = 1;
530 filter->color = g_value_get_enum (value);
533 filter->interval = g_value_get_uint (value);
536 filter->trigger = g_value_get_boolean (value);
539 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
542 GST_OBJECT_UNLOCK (filter);
546 gst_radioactv_get_property (GObject * object, guint prop_id,
547 GValue * value, GParamSpec * pspec)
549 GstRadioacTV *filter = GST_RADIOACTV (object);
553 g_value_set_enum (value, filter->mode);
556 g_value_set_enum (value, filter->color);
559 g_value_set_uint (value, filter->interval);
562 g_value_set_boolean (value, filter->trigger);
565 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
571 gst_radioactv_base_init (gpointer g_class)
573 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
575 gst_element_class_set_details_simple (element_class, "RadioacTV effect",
576 "Filter/Effect/Video",
577 "motion-enlightment effect",
578 "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
579 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
581 gst_element_class_add_static_pad_template (element_class,
582 &gst_radioactv_sink_template);
583 gst_element_class_add_static_pad_template (element_class,
584 &gst_radioactv_src_template);
588 gst_radioactv_class_init (GstRadioacTVClass * klass)
590 GObjectClass *gobject_class = (GObjectClass *) klass;
591 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
593 gobject_class->set_property = gst_radioactv_set_property;
594 gobject_class->get_property = gst_radioactv_get_property;
596 gobject_class->finalize = gst_radioactv_finalize;
598 g_object_class_install_property (gobject_class, PROP_MODE,
599 g_param_spec_enum ("mode", "Mode",
600 "Mode", GST_TYPE_RADIOACTV_MODE, DEFAULT_MODE,
601 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
603 g_object_class_install_property (gobject_class, PROP_COLOR,
604 g_param_spec_enum ("color", "Color",
605 "Color", GST_TYPE_RADIOACTV_COLOR, DEFAULT_COLOR,
606 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
608 g_object_class_install_property (gobject_class, PROP_INTERVAL,
609 g_param_spec_uint ("interval", "Interval",
610 "Snapshot interval (in strobe mode)", 0, G_MAXINT, DEFAULT_INTERVAL,
611 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
613 g_object_class_install_property (gobject_class, PROP_TRIGGER,
614 g_param_spec_boolean ("trigger", "Trigger",
615 "Trigger (in trigger mode)", DEFAULT_TRIGGER,
616 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
618 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_radioactv_set_caps);
619 trans_class->transform = GST_DEBUG_FUNCPTR (gst_radioactv_transform);
620 trans_class->start = GST_DEBUG_FUNCPTR (gst_radioactv_start);
626 gst_radioactv_init (GstRadioacTV * filter, GstRadioacTVClass * klass)
628 filter->mode = DEFAULT_MODE;
629 filter->color = DEFAULT_COLOR;
630 filter->interval = DEFAULT_INTERVAL;
631 filter->trigger = DEFAULT_TRIGGER;