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];
138 GST_BOILERPLATE (GstRadioacTV, gst_radioactv, GstVideoFilter,
139 GST_TYPE_VIDEO_FILTER);
141 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
142 #define CAPS_STR GST_VIDEO_CAPS_RGBx
144 #define CAPS_STR GST_VIDEO_CAPS_xBGR
147 static GstStaticPadTemplate gst_radioactv_src_template =
148 GST_STATIC_PAD_TEMPLATE ("src",
151 GST_STATIC_CAPS (CAPS_STR)
154 static GstStaticPadTemplate gst_radioactv_sink_template =
155 GST_STATIC_PAD_TEMPLATE ("sink",
158 GST_STATIC_CAPS (CAPS_STR)
166 #define DELTA (255/(COLORS/2-1))
168 for (i = 0; i < COLORS / 2; i++) {
169 palettes[i] = i * DELTA;
170 palettes[COLORS + i] = (i * DELTA) << 8;
171 palettes[COLORS * 2 + i] = (i * DELTA) << 16;
173 for (i = 0; i < COLORS / 2; i++) {
174 palettes[+i + COLORS / 2] = 255 | (i * DELTA) << 16 | (i * DELTA) << 8;
175 palettes[COLORS + i + COLORS / 2] =
176 (255 << 8) | (i * DELTA) << 16 | i * DELTA;
177 palettes[COLORS * 2 + i + COLORS / 2] =
178 (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 + filter->width + 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 GstFlowReturn ret = GST_FLOW_OK;
324 GstClockTime timestamp, stream_time;
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 (G_OBJECT (filter), stream_time);
340 src = (guint32 *) GST_BUFFER_DATA (in);
341 dest = (guint32 *) GST_BUFFER_DATA (out);
343 GST_OBJECT_LOCK (filter);
344 palette = &palettes[COLORS * filter->color];
347 if (filter->mode == 3 && filter->trigger)
348 filter->snaptime = 0;
349 else if (filter->mode == 3 && !filter->trigger)
350 filter->snaptime = 1;
352 if (filter->mode != 2 || filter->snaptime <= 0) {
353 image_bgsubtract_update_y (src, filter->background, diff,
354 filter->width * filter->height, MAGIC_THRESHOLD * 7);
355 if (filter->mode == 0 || filter->snaptime <= 0) {
356 diff += filter->buf_margin_left;
357 p = filter->blurzoombuf;
358 for (y = 0; y < filter->buf_height; y++) {
359 for (x = 0; x < filter->buf_width; x++) {
360 p[x] |= diff[x] >> 3;
362 diff += filter->width;
363 p += filter->buf_width;
365 if (filter->mode == 1 || filter->mode == 2) {
366 memcpy (filter->snapframe, src, filter->width * filter->height * 4);
370 blurzoomcore (filter);
372 if (filter->mode == 1 || filter->mode == 2) {
373 src = filter->snapframe;
375 p = filter->blurzoombuf;
376 for (y = 0; y < filter->height; y++) {
377 for (x = 0; x < filter->buf_margin_left; x++) {
380 for (x = 0; x < filter->buf_width; x++) {
381 a = *src++ & 0xfefeff;
385 *dest++ = a | (b - (b >> 8));
387 for (x = 0; x < filter->buf_margin_right; x++) {
392 if (filter->mode == 1 || filter->mode == 2) {
394 if (filter->snaptime < 0) {
395 filter->snaptime = filter->interval;
398 GST_OBJECT_UNLOCK (filter);
404 gst_radioactv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
407 GstRadioacTV *filter = GST_RADIOACTV (btrans);
408 GstStructure *structure;
409 gboolean ret = FALSE;
411 structure = gst_caps_get_structure (incaps, 0);
413 GST_OBJECT_LOCK (filter);
414 if (gst_structure_get_int (structure, "width", &filter->width) &&
415 gst_structure_get_int (structure, "height", &filter->height)) {
416 filter->buf_width_blocks = filter->width / 32;
417 if (filter->buf_width_blocks > 255)
420 filter->buf_width = filter->buf_width_blocks * 32;
421 filter->buf_height = filter->height;
422 filter->buf_area = filter->buf_height * filter->buf_width;
423 filter->buf_margin_left = (filter->width - filter->buf_width) / 2;
424 filter->buf_margin_right =
425 filter->height - filter->buf_width - filter->buf_margin_left;
427 if (filter->blurzoombuf)
428 g_free (filter->blurzoombuf);
429 filter->blurzoombuf = g_new0 (guint8, filter->buf_area * 2);
431 if (filter->blurzoomx)
432 g_free (filter->blurzoomx);
433 filter->blurzoomx = g_new0 (gint, filter->buf_width);
435 if (filter->blurzoomy)
436 g_free (filter->blurzoomy);
437 filter->blurzoomy = g_new0 (gint, filter->buf_height);
439 if (filter->snapframe)
440 g_free (filter->snapframe);
441 filter->snapframe = g_new (guint32, filter->width * filter->height);
444 g_free (filter->diff);
445 filter->diff = g_new (guint8, filter->width * filter->height);
447 if (filter->background)
448 g_free (filter->background);
449 filter->background = g_new0 (gint16, filter->width * filter->height);
456 GST_OBJECT_UNLOCK (filter);
462 gst_radioactv_start (GstBaseTransform * trans)
464 GstRadioacTV *filter = GST_RADIOACTV (trans);
466 filter->snaptime = 0;
472 gst_radioactv_finalize (GObject * object)
474 GstRadioacTV *filter = GST_RADIOACTV (object);
476 if (filter->snapframe)
477 g_free (filter->snapframe);
478 filter->snapframe = NULL;
480 if (filter->blurzoombuf)
481 g_free (filter->blurzoombuf);
482 filter->blurzoombuf = NULL;
485 g_free (filter->diff);
488 if (filter->background)
489 g_free (filter->background);
490 filter->background = NULL;
492 if (filter->blurzoomx)
493 g_free (filter->blurzoomx);
494 filter->blurzoomx = NULL;
496 if (filter->blurzoomy)
497 g_free (filter->blurzoomy);
498 filter->blurzoomy = NULL;
500 G_OBJECT_CLASS (parent_class)->finalize (object);
504 gst_radioactv_set_property (GObject * object, guint prop_id,
505 const GValue * value, GParamSpec * pspec)
507 GstRadioacTV *filter = GST_RADIOACTV (object);
509 GST_OBJECT_LOCK (filter);
512 filter->mode = g_value_get_enum (value);
513 if (filter->mode == 3)
514 filter->snaptime = 1;
517 filter->color = g_value_get_enum (value);
520 filter->interval = g_value_get_uint (value);
523 filter->trigger = g_value_get_boolean (value);
526 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
529 GST_OBJECT_UNLOCK (filter);
533 gst_radioactv_get_property (GObject * object, guint prop_id,
534 GValue * value, GParamSpec * pspec)
536 GstRadioacTV *filter = GST_RADIOACTV (object);
540 g_value_set_enum (value, filter->mode);
543 g_value_set_enum (value, filter->color);
546 g_value_set_uint (value, filter->interval);
549 g_value_set_boolean (value, filter->trigger);
552 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
558 gst_radioactv_base_init (gpointer g_class)
560 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
562 gst_element_class_set_details_simple (element_class, "RadioacTV effect",
563 "Filter/Effect/Video",
564 "motion-enlightment effect",
565 "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
566 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
568 gst_element_class_add_pad_template (element_class,
569 gst_static_pad_template_get (&gst_radioactv_sink_template));
570 gst_element_class_add_pad_template (element_class,
571 gst_static_pad_template_get (&gst_radioactv_src_template));
575 gst_radioactv_class_init (GstRadioacTVClass * klass)
577 GObjectClass *gobject_class = (GObjectClass *) klass;
578 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
580 gobject_class->set_property = gst_radioactv_set_property;
581 gobject_class->get_property = gst_radioactv_get_property;
583 gobject_class->finalize = gst_radioactv_finalize;
585 g_object_class_install_property (gobject_class, PROP_MODE,
586 g_param_spec_enum ("mode", "Mode",
587 "Mode", GST_TYPE_RADIOACTV_MODE, DEFAULT_MODE,
588 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
590 g_object_class_install_property (gobject_class, PROP_COLOR,
591 g_param_spec_enum ("color", "Color",
592 "Color", GST_TYPE_RADIOACTV_COLOR, DEFAULT_COLOR,
593 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
595 g_object_class_install_property (gobject_class, PROP_INTERVAL,
596 g_param_spec_uint ("interval", "Interval",
597 "Snapshot interval (in strobe mode)", 0, G_MAXINT, DEFAULT_INTERVAL,
598 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
600 g_object_class_install_property (gobject_class, PROP_TRIGGER,
601 g_param_spec_boolean ("trigger", "Trigger",
602 "Trigger (in trigger mode)", DEFAULT_TRIGGER,
603 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
605 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_radioactv_set_caps);
606 trans_class->transform = GST_DEBUG_FUNCPTR (gst_radioactv_transform);
607 trans_class->start = GST_DEBUG_FUNCPTR (gst_radioactv_start);
613 gst_radioactv_init (GstRadioacTV * filter, GstRadioacTVClass * klass)
615 filter->mode = DEFAULT_MODE;
616 filter->color = DEFAULT_COLOR;
617 filter->interval = DEFAULT_INTERVAL;
618 filter->trigger = DEFAULT_TRIGGER;
620 gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (filter));
621 gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (filter));