tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.git] / gst / effectv / gstradioac.c
1 /* GStreamer
2  * Cradioacyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
3  *
4  * EffecTV - Realtime Digital Video Effector
5  * Cradioacyright (C) 2001-2006 FUKUCHI Kentaro
6  *
7  * RadioacTV - motion-enlightment effect.
8  * Cradioacyright (C) 2001-2002 FUKUCHI Kentaro
9  *
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.
15  *
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.
20  *
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.
25  */
26
27 /**
28  * SECTION:element-radioactv
29  *
30  * RadioacTV does *NOT* detect a radioactivity. It detects a difference
31  * from previous frame and blurs it.
32  * 
33  * RadioacTV has 4 mode, normal, strobe1, strobe2 and trigger.
34  * In trigger mode, effect appears only when the trigger property is %TRUE.
35  *
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.
39  *
40  * <refsect2>
41  * <title>Example launch line</title>
42  * |[
43  * gst-launch -v videotestsrc ! radioactv ! ffmpegcolorspace ! autovideosink
44  * ]| This pipeline shows the effect of radioactv on a test stream.
45  * </refsect2>
46  */
47
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52 #include <math.h>
53 #include <string.h>
54
55 #include "gstradioac.h"
56 #include "gsteffectv.h"
57
58 #include <gst/video/video.h>
59 #include <gst/controller/gstcontroller.h>
60
61 enum
62 {
63   RADIOAC_NORMAL = 0,
64   RADIOAC_STROBE,
65   RADIOAC_STROBE2,
66   RADIOAC_TRIGGER
67 };
68
69 enum
70 {
71   COLOR_RED = 0,
72   COLOR_GREEN,
73   COLOR_BLUE,
74   COLOR_WHITE
75 };
76
77 #define GST_TYPE_RADIOACTV_MODE (gst_radioactv_mode_get_type())
78 static GType
79 gst_radioactv_mode_get_type (void)
80 {
81   static GType type = 0;
82
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"},
88     {0, NULL, NULL},
89   };
90
91   if (!type) {
92     type = g_enum_register_static ("GstRadioacTVMode", enumvalue);
93   }
94   return type;
95 }
96
97 #define GST_TYPE_RADIOACTV_COLOR (gst_radioactv_color_get_type())
98 static GType
99 gst_radioactv_color_get_type (void)
100 {
101   static GType type = 0;
102
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"},
108     {0, NULL, NULL},
109   };
110
111   if (!type) {
112     type = g_enum_register_static ("GstRadioacTVColor", enumvalue);
113   }
114   return type;
115 }
116
117 #define DEFAULT_MODE RADIOAC_NORMAL
118 #define DEFAULT_COLOR COLOR_WHITE
119 #define DEFAULT_INTERVAL 3
120 #define DEFAULT_TRIGGER FALSE
121
122 enum
123 {
124   PROP_0,
125   PROP_MODE,
126   PROP_COLOR,
127   PROP_INTERVAL,
128   PROP_TRIGGER
129 };
130
131 #define COLORS 32
132 #define PATTERN 4
133 #define MAGIC_THRESHOLD 40
134 #define RATIO 0.95
135
136 static guint32 palettes[COLORS * PATTERN];
137 static gint swap_tab[] = { 2, 1, 0, 3 };
138
139 GST_BOILERPLATE (GstRadioacTV, gst_radioactv, GstVideoFilter,
140     GST_TYPE_VIDEO_FILTER);
141
142 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
143 #define CAPS_STR GST_VIDEO_CAPS_RGBx "; " GST_VIDEO_CAPS_BGRx
144 #else
145 #define CAPS_STR GST_VIDEO_CAPS_xBGR "; " GST_VIDEO_CAPS_xRGB
146 #endif
147
148 static GstStaticPadTemplate gst_radioactv_src_template =
149 GST_STATIC_PAD_TEMPLATE ("src",
150     GST_PAD_SRC,
151     GST_PAD_ALWAYS,
152     GST_STATIC_CAPS (CAPS_STR)
153     );
154
155 static GstStaticPadTemplate gst_radioactv_sink_template =
156 GST_STATIC_PAD_TEMPLATE ("sink",
157     GST_PAD_SINK,
158     GST_PAD_ALWAYS,
159     GST_STATIC_CAPS (CAPS_STR)
160     );
161
162 static void
163 makePalette (void)
164 {
165   gint i;
166
167 #define DELTA (255/(COLORS/2-1))
168
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;
174   }
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;
181   }
182   /* white */
183   for (i = 0; i < COLORS; i++) {
184     palettes[COLORS * 3 + i] = (255 * i / COLORS) * 0x10101;
185   }
186   for (i = 0; i < COLORS * PATTERN; i++) {
187     palettes[i] = palettes[i] & 0xfefeff;
188   }
189 #undef DELTA
190 }
191
192 #define VIDEO_HWIDTH (filter->buf_width/2)
193 #define VIDEO_HHEIGHT (filter->buf_height/2)
194
195 /* this table assumes that video_width is times of 32 */
196 static void
197 setTable (GstRadioacTV * filter)
198 {
199   guint bits;
200   gint x, y, tx, ty, xx;
201   gint ptr, prevptr;
202
203   prevptr = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
204   for (xx = 0; xx < (filter->buf_width_blocks); xx++) {
205     bits = 0;
206     for (x = 0; x < 32; x++) {
207       ptr = (gint) (0.5 + RATIO * (xx * 32 + x - VIDEO_HWIDTH) + VIDEO_HWIDTH);
208       bits = bits >> 1;
209       if (ptr != prevptr)
210         bits |= 0x80000000;
211       prevptr = ptr;
212     }
213     filter->blurzoomx[xx] = bits;
214   }
215
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) +
219       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;
226   }
227 }
228
229 #undef VIDEO_HWIDTH
230 #undef VIDEO_HHEIGHT
231
232 static void
233 blur (GstRadioacTV * filter)
234 {
235   gint x, y;
236   gint width;
237   guint8 *p, *q;
238   guint8 v;
239
240   width = filter->buf_width;
241   p = filter->blurzoombuf + filter->width + 1;
242   q = p + filter->buf_area;
243
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;
247       if (v == 255)
248         v = 0;
249       *q = v;
250       p++;
251       q++;
252     }
253     p += 2;
254     q += 2;
255   }
256 }
257
258 static void
259 zoom (GstRadioacTV * filter)
260 {
261   gint b, x, y;
262   guint8 *p, *q;
263   gint blocks, height;
264   gint dx;
265
266   p = filter->blurzoombuf + filter->buf_area;
267   q = filter->blurzoombuf;
268   height = filter->buf_height;
269   blocks = filter->buf_width_blocks;
270
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++) {
276         p += (dx & 1);
277         *q++ = *p;
278         dx = dx >> 1;
279       }
280     }
281   }
282 }
283
284 static void
285 blurzoomcore (GstRadioacTV * filter)
286 {
287   blur (filter);
288   zoom (filter);
289 }
290
291 /* Background image is refreshed every frame */
292 static void
293 image_bgsubtract_update_y (guint32 * src, gint16 * background, guint8 * diff,
294     gint video_area, gint y_threshold)
295 {
296   gint i;
297   gint R, G, B;
298   guint32 *p;
299   gint16 *q;
300   guint8 *r;
301   gint v;
302
303   p = src;
304   q = background;
305   r = diff;
306   for (i = 0; i < video_area; i++) {
307     R = ((*p) & 0xff0000) >> (16 - 1);
308     G = ((*p) & 0xff00) >> (8 - 2);
309     B = (*p) & 0xff;
310     v = (R + G + B) - (gint) (*q);
311     *q = (gint16) (R + G + B);
312     *r = ((v + y_threshold) >> 24) | ((y_threshold - v) >> 24);
313
314     p++;
315     q++;
316     r++;
317   }
318 }
319
320 static GstFlowReturn
321 gst_radioactv_transform (GstBaseTransform * trans, GstBuffer * in,
322     GstBuffer * out)
323 {
324   GstRadioacTV *filter = GST_RADIOACTV (trans);
325   guint32 *src, *dest;
326   GstFlowReturn ret = GST_FLOW_OK;
327   GstClockTime timestamp, stream_time;
328   gint x, y;
329   guint32 a, b;
330   guint8 *diff, *p;
331   guint32 *palette;
332
333   timestamp = GST_BUFFER_TIMESTAMP (in);
334   stream_time =
335       gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
336
337   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
338       GST_TIME_ARGS (timestamp));
339
340   if (GST_CLOCK_TIME_IS_VALID (stream_time))
341     gst_object_sync_values (G_OBJECT (filter), stream_time);
342
343   src = (guint32 *) GST_BUFFER_DATA (in);
344   dest = (guint32 *) GST_BUFFER_DATA (out);
345
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];
350   } else {
351     palette = &palettes[COLORS * swap_tab[filter->color]];
352   }
353 #else
354   if (filter->format == GST_VIDEO_FORMAT_xBGR) {
355     palette = &palettes[COLORS * filter->color];
356   } else {
357     palette = &palettes[COLORS * swap_tab[filter->color]];
358   }
359 #endif
360   diff = filter->diff;
361
362   if (filter->mode == 3 && filter->trigger)
363     filter->snaptime = 0;
364   else if (filter->mode == 3 && !filter->trigger)
365     filter->snaptime = 1;
366
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;
376         }
377         diff += filter->width;
378         p += filter->buf_width;
379       }
380       if (filter->mode == 1 || filter->mode == 2) {
381         memcpy (filter->snapframe, src, filter->width * filter->height * 4);
382       }
383     }
384   }
385   blurzoomcore (filter);
386
387   if (filter->mode == 1 || filter->mode == 2) {
388     src = filter->snapframe;
389   }
390   p = filter->blurzoombuf;
391   for (y = 0; y < filter->height; y++) {
392     for (x = 0; x < filter->buf_margin_left; x++) {
393       *dest++ = *src++;
394     }
395     for (x = 0; x < filter->buf_width; x++) {
396       a = *src++ & 0xfefeff;
397       b = palette[*p++];
398       a += b;
399       b = a & 0x1010100;
400       *dest++ = a | (b - (b >> 8));
401     }
402     for (x = 0; x < filter->buf_margin_right; x++) {
403       *dest++ = *src++;
404     }
405   }
406
407   if (filter->mode == 1 || filter->mode == 2) {
408     filter->snaptime--;
409     if (filter->snaptime < 0) {
410       filter->snaptime = filter->interval;
411     }
412   }
413   GST_OBJECT_UNLOCK (filter);
414
415   return ret;
416 }
417
418 static gboolean
419 gst_radioactv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
420     GstCaps * outcaps)
421 {
422   GstRadioacTV *filter = GST_RADIOACTV (btrans);
423   gboolean ret = FALSE;
424
425   GST_OBJECT_LOCK (filter);
426
427   if (gst_video_format_parse_caps (incaps, &filter->format, &filter->width,
428           &filter->height)) {
429     filter->buf_width_blocks = filter->width / 32;
430     if (filter->buf_width_blocks > 255)
431       goto out;
432
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;
439
440     if (filter->blurzoombuf)
441       g_free (filter->blurzoombuf);
442     filter->blurzoombuf = g_new0 (guint8, filter->buf_area * 2);
443
444     if (filter->blurzoomx)
445       g_free (filter->blurzoomx);
446     filter->blurzoomx = g_new0 (gint, filter->buf_width);
447
448     if (filter->blurzoomy)
449       g_free (filter->blurzoomy);
450     filter->blurzoomy = g_new0 (gint, filter->buf_height);
451
452     if (filter->snapframe)
453       g_free (filter->snapframe);
454     filter->snapframe = g_new (guint32, filter->width * filter->height);
455
456     if (filter->diff)
457       g_free (filter->diff);
458     filter->diff = g_new (guint8, filter->width * filter->height);
459
460     if (filter->background)
461       g_free (filter->background);
462     filter->background = g_new0 (gint16, filter->width * filter->height);
463
464     setTable (filter);
465
466     ret = TRUE;
467   }
468 out:
469   GST_OBJECT_UNLOCK (filter);
470
471   return ret;
472 }
473
474 static gboolean
475 gst_radioactv_start (GstBaseTransform * trans)
476 {
477   GstRadioacTV *filter = GST_RADIOACTV (trans);
478
479   filter->snaptime = 0;
480
481   return TRUE;
482 }
483
484 static void
485 gst_radioactv_finalize (GObject * object)
486 {
487   GstRadioacTV *filter = GST_RADIOACTV (object);
488
489   if (filter->snapframe)
490     g_free (filter->snapframe);
491   filter->snapframe = NULL;
492
493   if (filter->blurzoombuf)
494     g_free (filter->blurzoombuf);
495   filter->blurzoombuf = NULL;
496
497   if (filter->diff)
498     g_free (filter->diff);
499   filter->diff = NULL;
500
501   if (filter->background)
502     g_free (filter->background);
503   filter->background = NULL;
504
505   if (filter->blurzoomx)
506     g_free (filter->blurzoomx);
507   filter->blurzoomx = NULL;
508
509   if (filter->blurzoomy)
510     g_free (filter->blurzoomy);
511   filter->blurzoomy = NULL;
512
513   G_OBJECT_CLASS (parent_class)->finalize (object);
514 }
515
516 static void
517 gst_radioactv_set_property (GObject * object, guint prop_id,
518     const GValue * value, GParamSpec * pspec)
519 {
520   GstRadioacTV *filter = GST_RADIOACTV (object);
521
522   GST_OBJECT_LOCK (filter);
523   switch (prop_id) {
524     case PROP_MODE:
525       filter->mode = g_value_get_enum (value);
526       if (filter->mode == 3)
527         filter->snaptime = 1;
528       break;
529     case PROP_COLOR:
530       filter->color = g_value_get_enum (value);
531       break;
532     case PROP_INTERVAL:
533       filter->interval = g_value_get_uint (value);
534       break;
535     case PROP_TRIGGER:
536       filter->trigger = g_value_get_boolean (value);
537       break;
538     default:
539       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
540       break;
541   }
542   GST_OBJECT_UNLOCK (filter);
543 }
544
545 static void
546 gst_radioactv_get_property (GObject * object, guint prop_id,
547     GValue * value, GParamSpec * pspec)
548 {
549   GstRadioacTV *filter = GST_RADIOACTV (object);
550
551   switch (prop_id) {
552     case PROP_MODE:
553       g_value_set_enum (value, filter->mode);
554       break;
555     case PROP_COLOR:
556       g_value_set_enum (value, filter->color);
557       break;
558     case PROP_INTERVAL:
559       g_value_set_uint (value, filter->interval);
560       break;
561     case PROP_TRIGGER:
562       g_value_set_boolean (value, filter->trigger);
563       break;
564     default:
565       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
566       break;
567   }
568 }
569
570 static void
571 gst_radioactv_base_init (gpointer g_class)
572 {
573   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
574
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>");
580
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);
585 }
586
587 static void
588 gst_radioactv_class_init (GstRadioacTVClass * klass)
589 {
590   GObjectClass *gobject_class = (GObjectClass *) klass;
591   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
592
593   gobject_class->set_property = gst_radioactv_set_property;
594   gobject_class->get_property = gst_radioactv_get_property;
595
596   gobject_class->finalize = gst_radioactv_finalize;
597
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));
602
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));
607
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));
612
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));
617
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);
621
622   makePalette ();
623 }
624
625 static void
626 gst_radioactv_init (GstRadioacTV * filter, GstRadioacTVClass * klass)
627 {
628   filter->mode = DEFAULT_MODE;
629   filter->color = DEFAULT_COLOR;
630   filter->interval = DEFAULT_INTERVAL;
631   filter->trigger = DEFAULT_TRIGGER;
632 }