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 ! videoconvert ! autovideosink
44 * ]| This pipeline shows the effect of radioactv on a test stream.
55 #include "gstradioac.h"
56 #include "gsteffectv.h"
74 #define GST_TYPE_RADIOACTV_MODE (gst_radioactv_mode_get_type())
76 gst_radioactv_mode_get_type (void)
78 static GType type = 0;
80 static const GEnumValue enumvalue[] = {
81 {RADIOAC_NORMAL, "Normal", "normal"},
82 {RADIOAC_STROBE, "Strobe 1", "strobe1"},
83 {RADIOAC_STROBE2, "Strobe 2", "strobe2"},
84 {RADIOAC_TRIGGER, "Trigger", "trigger"},
89 type = g_enum_register_static ("GstRadioacTVMode", enumvalue);
94 #define GST_TYPE_RADIOACTV_COLOR (gst_radioactv_color_get_type())
96 gst_radioactv_color_get_type (void)
98 static GType type = 0;
100 static const GEnumValue enumvalue[] = {
101 {COLOR_RED, "Red", "red"},
102 {COLOR_GREEN, "Green", "green"},
103 {COLOR_BLUE, "Blue", "blue"},
104 {COLOR_WHITE, "White", "white"},
109 type = g_enum_register_static ("GstRadioacTVColor", enumvalue);
114 #define DEFAULT_MODE RADIOAC_NORMAL
115 #define DEFAULT_COLOR COLOR_WHITE
116 #define DEFAULT_INTERVAL 3
117 #define DEFAULT_TRIGGER FALSE
130 #define MAGIC_THRESHOLD 40
133 static guint32 palettes[COLORS * PATTERN];
134 static gint swap_tab[] = { 2, 1, 0, 3 };
136 #define gst_radioactv_parent_class parent_class
137 G_DEFINE_TYPE (GstRadioacTV, gst_radioactv, GST_TYPE_VIDEO_FILTER);
139 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
140 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ RGBx, BGRx }")
142 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ xBGR, xRGB }")
145 static GstStaticPadTemplate gst_radioactv_src_template =
146 GST_STATIC_PAD_TEMPLATE ("src",
149 GST_STATIC_CAPS (CAPS_STR)
152 static GstStaticPadTemplate gst_radioactv_sink_template =
153 GST_STATIC_PAD_TEMPLATE ("sink",
156 GST_STATIC_CAPS (CAPS_STR)
164 #define DELTA (255/(COLORS/2-1))
166 /* red, gree, blue */
167 for (i = 0; i < COLORS / 2; i++) {
168 palettes[i] = i * DELTA;
169 palettes[COLORS + i] = (i * DELTA) << 8;
170 palettes[COLORS * 2 + i] = (i * DELTA) << 16;
172 for (i = 0; i < COLORS / 2; i++) {
173 palettes[i + COLORS / 2] = 255 | (i * DELTA) << 16 | (i * DELTA) << 8;
174 palettes[COLORS + i + COLORS / 2] =
175 (255 << 8) | (i * DELTA) << 16 | i * DELTA;
176 palettes[COLORS * 2 + i + COLORS / 2] =
177 (255 << 16) | (i * DELTA) << 8 | i * DELTA;
180 for (i = 0; i < COLORS; i++) {
181 palettes[COLORS * 3 + i] = (255 * i / COLORS) * 0x10101;
183 for (i = 0; i < COLORS * PATTERN; i++) {
184 palettes[i] = palettes[i] & 0xfefeff;
189 #define VIDEO_HWIDTH (filter->buf_width/2)
190 #define VIDEO_HHEIGHT (filter->buf_height/2)
192 /* this table assumes that video_width is times of 32 */
194 setTable (GstRadioacTV * filter)
197 gint x, y, tx, ty, xx;
200 prevptr = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
201 for (xx = 0; xx < (filter->buf_width_blocks); xx++) {
203 for (x = 0; x < 32; x++) {
204 ptr = (gint) (0.5 + RATIO * (xx * 32 + x - VIDEO_HWIDTH) + VIDEO_HWIDTH);
210 filter->blurzoomx[xx] = bits;
213 ty = (gint) (0.5 + RATIO * (-VIDEO_HHEIGHT) + VIDEO_HHEIGHT);
214 tx = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
215 xx = (gint) (0.5 + RATIO * (filter->buf_width - 1 - VIDEO_HWIDTH) +
217 filter->blurzoomy[0] = ty * filter->buf_width + tx;
218 prevptr = ty * filter->buf_width + xx;
219 for (y = 1; y < filter->buf_height; y++) {
220 ty = (gint) (0.5 + RATIO * (y - VIDEO_HHEIGHT) + VIDEO_HHEIGHT);
221 filter->blurzoomy[y] = ty * filter->buf_width + tx - prevptr;
222 prevptr = ty * filter->buf_width + xx;
230 blur (GstRadioacTV * filter)
237 width = filter->buf_width;
238 p = filter->blurzoombuf + GST_VIDEO_INFO_WIDTH (&filter->info) + 1;
239 q = p + filter->buf_area;
241 for (y = filter->buf_height - 2; y > 0; y--) {
242 for (x = width - 2; x > 0; x--) {
243 v = (*(p - width) + *(p - 1) + *(p + 1) + *(p + width)) / 4 - 1;
256 zoom (GstRadioacTV * filter)
263 p = filter->blurzoombuf + filter->buf_area;
264 q = filter->blurzoombuf;
265 height = filter->buf_height;
266 blocks = filter->buf_width_blocks;
268 for (y = 0; y < height; y++) {
269 p += filter->blurzoomy[y];
270 for (b = 0; b < blocks; b++) {
271 dx = filter->blurzoomx[b];
272 for (x = 0; x < 32; x++) {
282 blurzoomcore (GstRadioacTV * filter)
288 /* Background image is refreshed every frame */
290 image_bgsubtract_update_y (guint32 * src, gint16 * background, guint8 * diff,
291 gint video_area, gint y_threshold)
303 for (i = 0; i < video_area; i++) {
304 R = ((*p) & 0xff0000) >> (16 - 1);
305 G = ((*p) & 0xff00) >> (8 - 2);
307 v = (R + G + B) - (gint) (*q);
308 *q = (gint16) (R + G + B);
309 *r = ((v + y_threshold) >> 24) | ((y_threshold - v) >> 24);
318 gst_radioactv_transform (GstBaseTransform * trans, GstBuffer * in,
321 GstRadioacTV *filter = GST_RADIOACTV (trans);
323 GstVideoFrame in_frame, out_frame;
324 GstClockTime timestamp, stream_time;
325 gint x, y, width, height;
330 timestamp = GST_BUFFER_TIMESTAMP (in);
332 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
334 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
335 GST_TIME_ARGS (timestamp));
337 if (GST_CLOCK_TIME_IS_VALID (stream_time))
338 gst_object_sync_values (GST_OBJECT (filter), stream_time);
340 if (!gst_video_frame_map (&in_frame, &filter->info, in, GST_MAP_READ))
343 if (!gst_video_frame_map (&out_frame, &filter->info, out, GST_MAP_WRITE))
346 src = GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
347 dest = GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
349 width = GST_VIDEO_FRAME_WIDTH (&in_frame);
350 height = GST_VIDEO_FRAME_HEIGHT (&in_frame);
352 GST_OBJECT_LOCK (filter);
353 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
354 if (GST_VIDEO_FRAME_FORMAT (&in_frame) == GST_VIDEO_FORMAT_RGBx) {
355 palette = &palettes[COLORS * filter->color];
357 palette = &palettes[COLORS * swap_tab[filter->color]];
360 if (GST_VIDEO_FRAME_FORMAT (&in_frame) == GST_VIDEO_FORMAT_xBGR) {
361 palette = &palettes[COLORS * filter->color];
363 palette = &palettes[COLORS * swap_tab[filter->color]];
368 if (filter->mode == 3 && filter->trigger)
369 filter->snaptime = 0;
370 else if (filter->mode == 3 && !filter->trigger)
371 filter->snaptime = 1;
373 if (filter->mode != 2 || filter->snaptime <= 0) {
374 image_bgsubtract_update_y (src, filter->background, diff,
375 width * height, MAGIC_THRESHOLD * 7);
376 if (filter->mode == 0 || filter->snaptime <= 0) {
377 diff += filter->buf_margin_left;
378 p = filter->blurzoombuf;
379 for (y = 0; y < filter->buf_height; y++) {
380 for (x = 0; x < filter->buf_width; x++) {
381 p[x] |= diff[x] >> 3;
384 p += filter->buf_width;
386 if (filter->mode == 1 || filter->mode == 2) {
387 memcpy (filter->snapframe, src, width * height * 4);
391 blurzoomcore (filter);
393 if (filter->mode == 1 || filter->mode == 2) {
394 src = filter->snapframe;
396 p = filter->blurzoombuf;
397 for (y = 0; y < height; y++) {
398 for (x = 0; x < filter->buf_margin_left; x++) {
401 for (x = 0; x < filter->buf_width; x++) {
402 a = *src++ & 0xfefeff;
406 *dest++ = a | (b - (b >> 8));
408 for (x = 0; x < filter->buf_margin_right; x++) {
413 if (filter->mode == 1 || filter->mode == 2) {
415 if (filter->snaptime < 0) {
416 filter->snaptime = filter->interval;
419 GST_OBJECT_UNLOCK (filter);
421 gst_video_frame_unmap (&in_frame);
422 gst_video_frame_unmap (&out_frame);
429 GST_DEBUG_OBJECT (filter, "invalid input frame");
430 return GST_FLOW_ERROR;
434 GST_DEBUG_OBJECT (filter, "invalid output frame");
435 gst_video_frame_unmap (&in_frame);
436 return GST_FLOW_ERROR;
441 gst_radioactv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
444 GstRadioacTV *filter = GST_RADIOACTV (btrans);
448 if (!gst_video_info_from_caps (&info, incaps))
453 width = GST_VIDEO_INFO_WIDTH (&info);
454 height = GST_VIDEO_INFO_HEIGHT (&info);
456 filter->buf_width_blocks = width / 32;
457 if (filter->buf_width_blocks > 255)
460 filter->buf_width = filter->buf_width_blocks * 32;
461 filter->buf_height = height;
462 filter->buf_area = filter->buf_height * filter->buf_width;
463 filter->buf_margin_left = (width - filter->buf_width) / 2;
464 filter->buf_margin_right =
465 height - filter->buf_width - filter->buf_margin_left;
467 if (filter->blurzoombuf)
468 g_free (filter->blurzoombuf);
469 filter->blurzoombuf = g_new0 (guint8, filter->buf_area * 2);
471 if (filter->blurzoomx)
472 g_free (filter->blurzoomx);
473 filter->blurzoomx = g_new0 (gint, filter->buf_width);
475 if (filter->blurzoomy)
476 g_free (filter->blurzoomy);
477 filter->blurzoomy = g_new0 (gint, filter->buf_height);
479 if (filter->snapframe)
480 g_free (filter->snapframe);
481 filter->snapframe = g_new (guint32, width * height);
484 g_free (filter->diff);
485 filter->diff = g_new (guint8, width * height);
487 if (filter->background)
488 g_free (filter->background);
489 filter->background = g_new0 (gint16, width * height);
498 GST_DEBUG_OBJECT (filter, "invalid caps received");
503 GST_DEBUG_OBJECT (filter, "frame too wide");
509 gst_radioactv_start (GstBaseTransform * trans)
511 GstRadioacTV *filter = GST_RADIOACTV (trans);
513 filter->snaptime = 0;
519 gst_radioactv_finalize (GObject * object)
521 GstRadioacTV *filter = GST_RADIOACTV (object);
523 if (filter->snapframe)
524 g_free (filter->snapframe);
525 filter->snapframe = NULL;
527 if (filter->blurzoombuf)
528 g_free (filter->blurzoombuf);
529 filter->blurzoombuf = NULL;
532 g_free (filter->diff);
535 if (filter->background)
536 g_free (filter->background);
537 filter->background = NULL;
539 if (filter->blurzoomx)
540 g_free (filter->blurzoomx);
541 filter->blurzoomx = NULL;
543 if (filter->blurzoomy)
544 g_free (filter->blurzoomy);
545 filter->blurzoomy = NULL;
547 G_OBJECT_CLASS (parent_class)->finalize (object);
551 gst_radioactv_set_property (GObject * object, guint prop_id,
552 const GValue * value, GParamSpec * pspec)
554 GstRadioacTV *filter = GST_RADIOACTV (object);
556 GST_OBJECT_LOCK (filter);
559 filter->mode = g_value_get_enum (value);
560 if (filter->mode == 3)
561 filter->snaptime = 1;
564 filter->color = g_value_get_enum (value);
567 filter->interval = g_value_get_uint (value);
570 filter->trigger = g_value_get_boolean (value);
573 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
576 GST_OBJECT_UNLOCK (filter);
580 gst_radioactv_get_property (GObject * object, guint prop_id,
581 GValue * value, GParamSpec * pspec)
583 GstRadioacTV *filter = GST_RADIOACTV (object);
587 g_value_set_enum (value, filter->mode);
590 g_value_set_enum (value, filter->color);
593 g_value_set_uint (value, filter->interval);
596 g_value_set_boolean (value, filter->trigger);
599 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
605 gst_radioactv_class_init (GstRadioacTVClass * klass)
607 GObjectClass *gobject_class = (GObjectClass *) klass;
608 GstElementClass *gstelement_class = (GstElementClass *) klass;
609 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
611 gobject_class->set_property = gst_radioactv_set_property;
612 gobject_class->get_property = gst_radioactv_get_property;
614 gobject_class->finalize = gst_radioactv_finalize;
616 g_object_class_install_property (gobject_class, PROP_MODE,
617 g_param_spec_enum ("mode", "Mode",
618 "Mode", GST_TYPE_RADIOACTV_MODE, DEFAULT_MODE,
619 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
621 g_object_class_install_property (gobject_class, PROP_COLOR,
622 g_param_spec_enum ("color", "Color",
623 "Color", GST_TYPE_RADIOACTV_COLOR, DEFAULT_COLOR,
624 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
626 g_object_class_install_property (gobject_class, PROP_INTERVAL,
627 g_param_spec_uint ("interval", "Interval",
628 "Snapshot interval (in strobe mode)", 0, G_MAXINT, DEFAULT_INTERVAL,
629 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
631 g_object_class_install_property (gobject_class, PROP_TRIGGER,
632 g_param_spec_boolean ("trigger", "Trigger",
633 "Trigger (in trigger mode)", DEFAULT_TRIGGER,
634 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
636 gst_element_class_set_details_simple (gstelement_class, "RadioacTV effect",
637 "Filter/Effect/Video",
638 "motion-enlightment effect",
639 "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
640 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
642 gst_element_class_add_pad_template (gstelement_class,
643 gst_static_pad_template_get (&gst_radioactv_sink_template));
644 gst_element_class_add_pad_template (gstelement_class,
645 gst_static_pad_template_get (&gst_radioactv_src_template));
647 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_radioactv_set_caps);
648 trans_class->transform = GST_DEBUG_FUNCPTR (gst_radioactv_transform);
649 trans_class->start = GST_DEBUG_FUNCPTR (gst_radioactv_start);
655 gst_radioactv_init (GstRadioacTV * filter)
657 filter->mode = DEFAULT_MODE;
658 filter->color = DEFAULT_COLOR;
659 filter->interval = DEFAULT_INTERVAL;
660 filter->trigger = DEFAULT_TRIGGER;