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];
135 #define gst_radioactv_parent_class parent_class
136 G_DEFINE_TYPE (GstRadioacTV, gst_radioactv, GST_TYPE_VIDEO_FILTER);
138 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
139 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("RGBx")
141 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("xBGR")
144 static GstStaticPadTemplate gst_radioactv_src_template =
145 GST_STATIC_PAD_TEMPLATE ("src",
148 GST_STATIC_CAPS (CAPS_STR)
151 static GstStaticPadTemplate gst_radioactv_sink_template =
152 GST_STATIC_PAD_TEMPLATE ("sink",
155 GST_STATIC_CAPS (CAPS_STR)
163 #define DELTA (255/(COLORS/2-1))
165 for (i = 0; i < COLORS / 2; i++) {
166 palettes[i] = i * DELTA;
167 palettes[COLORS + i] = (i * DELTA) << 8;
168 palettes[COLORS * 2 + i] = (i * DELTA) << 16;
170 for (i = 0; i < COLORS / 2; i++) {
171 palettes[+i + COLORS / 2] = 255 | (i * DELTA) << 16 | (i * DELTA) << 8;
172 palettes[COLORS + i + COLORS / 2] =
173 (255 << 8) | (i * DELTA) << 16 | i * DELTA;
174 palettes[COLORS * 2 + i + COLORS / 2] =
175 (255 << 16) | (i * DELTA) << 8 | i * DELTA;
177 for (i = 0; i < COLORS; i++) {
178 palettes[COLORS * 3 + i] = (255 * i / COLORS) * 0x10101;
180 for (i = 0; i < COLORS * PATTERN; i++) {
181 palettes[i] = palettes[i] & 0xfefeff;
186 #define VIDEO_HWIDTH (filter->buf_width/2)
187 #define VIDEO_HHEIGHT (filter->buf_height/2)
189 /* this table assumes that video_width is times of 32 */
191 setTable (GstRadioacTV * filter)
194 gint x, y, tx, ty, xx;
197 prevptr = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
198 for (xx = 0; xx < (filter->buf_width_blocks); xx++) {
200 for (x = 0; x < 32; x++) {
201 ptr = (gint) (0.5 + RATIO * (xx * 32 + x - VIDEO_HWIDTH) + VIDEO_HWIDTH);
207 filter->blurzoomx[xx] = bits;
210 ty = (gint) (0.5 + RATIO * (-VIDEO_HHEIGHT) + VIDEO_HHEIGHT);
211 tx = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
212 xx = (gint) (0.5 + RATIO * (filter->buf_width - 1 - VIDEO_HWIDTH) +
214 filter->blurzoomy[0] = ty * filter->buf_width + tx;
215 prevptr = ty * filter->buf_width + xx;
216 for (y = 1; y < filter->buf_height; y++) {
217 ty = (gint) (0.5 + RATIO * (y - VIDEO_HHEIGHT) + VIDEO_HHEIGHT);
218 filter->blurzoomy[y] = ty * filter->buf_width + tx - prevptr;
219 prevptr = ty * filter->buf_width + xx;
227 blur (GstRadioacTV * filter)
234 width = filter->buf_width;
235 p = filter->blurzoombuf + GST_VIDEO_INFO_WIDTH (&filter->info) + 1;
236 q = p + filter->buf_area;
238 for (y = filter->buf_height - 2; y > 0; y--) {
239 for (x = width - 2; x > 0; x--) {
240 v = (*(p - width) + *(p - 1) + *(p + 1) + *(p + width)) / 4 - 1;
253 zoom (GstRadioacTV * filter)
260 p = filter->blurzoombuf + filter->buf_area;
261 q = filter->blurzoombuf;
262 height = filter->buf_height;
263 blocks = filter->buf_width_blocks;
265 for (y = 0; y < height; y++) {
266 p += filter->blurzoomy[y];
267 for (b = 0; b < blocks; b++) {
268 dx = filter->blurzoomx[b];
269 for (x = 0; x < 32; x++) {
279 blurzoomcore (GstRadioacTV * filter)
285 /* Background image is refreshed every frame */
287 image_bgsubtract_update_y (guint32 * src, gint16 * background, guint8 * diff,
288 gint video_area, gint y_threshold)
300 for (i = 0; i < video_area; i++) {
301 R = ((*p) & 0xff0000) >> (16 - 1);
302 G = ((*p) & 0xff00) >> (8 - 2);
304 v = (R + G + B) - (gint) (*q);
305 *q = (gint16) (R + G + B);
306 *r = ((v + y_threshold) >> 24) | ((y_threshold - v) >> 24);
315 gst_radioactv_transform (GstBaseTransform * trans, GstBuffer * in,
318 GstRadioacTV *filter = GST_RADIOACTV (trans);
320 GstVideoFrame in_frame, out_frame;
321 GstClockTime timestamp, stream_time;
322 gint x, y, width, height;
327 timestamp = GST_BUFFER_TIMESTAMP (in);
329 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
331 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
332 GST_TIME_ARGS (timestamp));
334 if (GST_CLOCK_TIME_IS_VALID (stream_time))
335 gst_object_sync_values (GST_OBJECT (filter), stream_time);
337 if (!gst_video_frame_map (&in_frame, &filter->info, in, GST_MAP_READ))
340 if (!gst_video_frame_map (&out_frame, &filter->info, out, GST_MAP_WRITE))
343 src = GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
344 dest = GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
346 width = GST_VIDEO_FRAME_WIDTH (&in_frame);
347 height = GST_VIDEO_FRAME_HEIGHT (&in_frame);
349 GST_OBJECT_LOCK (filter);
350 palette = &palettes[COLORS * filter->color];
353 if (filter->mode == 3 && filter->trigger)
354 filter->snaptime = 0;
355 else if (filter->mode == 3 && !filter->trigger)
356 filter->snaptime = 1;
358 if (filter->mode != 2 || filter->snaptime <= 0) {
359 image_bgsubtract_update_y (src, filter->background, diff,
360 width * height, MAGIC_THRESHOLD * 7);
361 if (filter->mode == 0 || filter->snaptime <= 0) {
362 diff += filter->buf_margin_left;
363 p = filter->blurzoombuf;
364 for (y = 0; y < filter->buf_height; y++) {
365 for (x = 0; x < filter->buf_width; x++) {
366 p[x] |= diff[x] >> 3;
369 p += filter->buf_width;
371 if (filter->mode == 1 || filter->mode == 2) {
372 memcpy (filter->snapframe, src, width * height * 4);
376 blurzoomcore (filter);
378 if (filter->mode == 1 || filter->mode == 2) {
379 src = filter->snapframe;
381 p = filter->blurzoombuf;
382 for (y = 0; y < height; y++) {
383 for (x = 0; x < filter->buf_margin_left; x++) {
386 for (x = 0; x < filter->buf_width; x++) {
387 a = *src++ & 0xfefeff;
391 *dest++ = a | (b - (b >> 8));
393 for (x = 0; x < filter->buf_margin_right; x++) {
398 if (filter->mode == 1 || filter->mode == 2) {
400 if (filter->snaptime < 0) {
401 filter->snaptime = filter->interval;
404 GST_OBJECT_UNLOCK (filter);
406 gst_video_frame_unmap (&in_frame);
407 gst_video_frame_unmap (&out_frame);
414 GST_DEBUG_OBJECT (filter, "invalid input frame");
415 return GST_FLOW_ERROR;
419 GST_DEBUG_OBJECT (filter, "invalid output frame");
420 gst_video_frame_unmap (&in_frame);
421 return GST_FLOW_ERROR;
426 gst_radioactv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
429 GstRadioacTV *filter = GST_RADIOACTV (btrans);
433 if (!gst_video_info_from_caps (&info, incaps))
438 width = GST_VIDEO_INFO_WIDTH (&info);
439 height = GST_VIDEO_INFO_HEIGHT (&info);
441 filter->buf_width_blocks = width / 32;
442 if (filter->buf_width_blocks > 255)
445 filter->buf_width = filter->buf_width_blocks * 32;
446 filter->buf_height = height;
447 filter->buf_area = filter->buf_height * filter->buf_width;
448 filter->buf_margin_left = (width - filter->buf_width) / 2;
449 filter->buf_margin_right =
450 height - filter->buf_width - filter->buf_margin_left;
452 if (filter->blurzoombuf)
453 g_free (filter->blurzoombuf);
454 filter->blurzoombuf = g_new0 (guint8, filter->buf_area * 2);
456 if (filter->blurzoomx)
457 g_free (filter->blurzoomx);
458 filter->blurzoomx = g_new0 (gint, filter->buf_width);
460 if (filter->blurzoomy)
461 g_free (filter->blurzoomy);
462 filter->blurzoomy = g_new0 (gint, filter->buf_height);
464 if (filter->snapframe)
465 g_free (filter->snapframe);
466 filter->snapframe = g_new (guint32, width * height);
469 g_free (filter->diff);
470 filter->diff = g_new (guint8, width * height);
472 if (filter->background)
473 g_free (filter->background);
474 filter->background = g_new0 (gint16, width * height);
483 GST_DEBUG_OBJECT (filter, "invalid caps received");
488 GST_DEBUG_OBJECT (filter, "frame too wide");
494 gst_radioactv_start (GstBaseTransform * trans)
496 GstRadioacTV *filter = GST_RADIOACTV (trans);
498 filter->snaptime = 0;
504 gst_radioactv_finalize (GObject * object)
506 GstRadioacTV *filter = GST_RADIOACTV (object);
508 if (filter->snapframe)
509 g_free (filter->snapframe);
510 filter->snapframe = NULL;
512 if (filter->blurzoombuf)
513 g_free (filter->blurzoombuf);
514 filter->blurzoombuf = NULL;
517 g_free (filter->diff);
520 if (filter->background)
521 g_free (filter->background);
522 filter->background = NULL;
524 if (filter->blurzoomx)
525 g_free (filter->blurzoomx);
526 filter->blurzoomx = NULL;
528 if (filter->blurzoomy)
529 g_free (filter->blurzoomy);
530 filter->blurzoomy = NULL;
532 G_OBJECT_CLASS (parent_class)->finalize (object);
536 gst_radioactv_set_property (GObject * object, guint prop_id,
537 const GValue * value, GParamSpec * pspec)
539 GstRadioacTV *filter = GST_RADIOACTV (object);
541 GST_OBJECT_LOCK (filter);
544 filter->mode = g_value_get_enum (value);
545 if (filter->mode == 3)
546 filter->snaptime = 1;
549 filter->color = g_value_get_enum (value);
552 filter->interval = g_value_get_uint (value);
555 filter->trigger = g_value_get_boolean (value);
558 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
561 GST_OBJECT_UNLOCK (filter);
565 gst_radioactv_get_property (GObject * object, guint prop_id,
566 GValue * value, GParamSpec * pspec)
568 GstRadioacTV *filter = GST_RADIOACTV (object);
572 g_value_set_enum (value, filter->mode);
575 g_value_set_enum (value, filter->color);
578 g_value_set_uint (value, filter->interval);
581 g_value_set_boolean (value, filter->trigger);
584 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
590 gst_radioactv_class_init (GstRadioacTVClass * klass)
592 GObjectClass *gobject_class = (GObjectClass *) klass;
593 GstElementClass *gstelement_class = (GstElementClass *) klass;
594 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
596 gobject_class->set_property = gst_radioactv_set_property;
597 gobject_class->get_property = gst_radioactv_get_property;
599 gobject_class->finalize = gst_radioactv_finalize;
601 g_object_class_install_property (gobject_class, PROP_MODE,
602 g_param_spec_enum ("mode", "Mode",
603 "Mode", GST_TYPE_RADIOACTV_MODE, DEFAULT_MODE,
604 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
606 g_object_class_install_property (gobject_class, PROP_COLOR,
607 g_param_spec_enum ("color", "Color",
608 "Color", GST_TYPE_RADIOACTV_COLOR, DEFAULT_COLOR,
609 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
611 g_object_class_install_property (gobject_class, PROP_INTERVAL,
612 g_param_spec_uint ("interval", "Interval",
613 "Snapshot interval (in strobe mode)", 0, G_MAXINT, DEFAULT_INTERVAL,
614 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
616 g_object_class_install_property (gobject_class, PROP_TRIGGER,
617 g_param_spec_boolean ("trigger", "Trigger",
618 "Trigger (in trigger mode)", DEFAULT_TRIGGER,
619 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
621 gst_element_class_set_details_simple (gstelement_class, "RadioacTV effect",
622 "Filter/Effect/Video",
623 "motion-enlightment effect",
624 "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
625 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
627 gst_element_class_add_pad_template (gstelement_class,
628 gst_static_pad_template_get (&gst_radioactv_sink_template));
629 gst_element_class_add_pad_template (gstelement_class,
630 gst_static_pad_template_get (&gst_radioactv_src_template));
632 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_radioactv_set_caps);
633 trans_class->transform = GST_DEBUG_FUNCPTR (gst_radioactv_transform);
634 trans_class->start = GST_DEBUG_FUNCPTR (gst_radioactv_start);
640 gst_radioactv_init (GstRadioacTV * filter)
642 filter->mode = DEFAULT_MODE;
643 filter->color = DEFAULT_COLOR;
644 filter->interval = DEFAULT_INTERVAL;
645 filter->trigger = DEFAULT_TRIGGER;
647 gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (filter));
648 gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (filter));