controller: port to new controller location and api
[platform/upstream/gstreamer.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 ! videoconvert ! 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 enum
59 {
60   RADIOAC_NORMAL = 0,
61   RADIOAC_STROBE,
62   RADIOAC_STROBE2,
63   RADIOAC_TRIGGER
64 };
65
66 enum
67 {
68   COLOR_RED = 0,
69   COLOR_GREEN,
70   COLOR_BLUE,
71   COLOR_WHITE
72 };
73
74 #define GST_TYPE_RADIOACTV_MODE (gst_radioactv_mode_get_type())
75 static GType
76 gst_radioactv_mode_get_type (void)
77 {
78   static GType type = 0;
79
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"},
85     {0, NULL, NULL},
86   };
87
88   if (!type) {
89     type = g_enum_register_static ("GstRadioacTVMode", enumvalue);
90   }
91   return type;
92 }
93
94 #define GST_TYPE_RADIOACTV_COLOR (gst_radioactv_color_get_type())
95 static GType
96 gst_radioactv_color_get_type (void)
97 {
98   static GType type = 0;
99
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"},
105     {0, NULL, NULL},
106   };
107
108   if (!type) {
109     type = g_enum_register_static ("GstRadioacTVColor", enumvalue);
110   }
111   return type;
112 }
113
114 #define DEFAULT_MODE RADIOAC_NORMAL
115 #define DEFAULT_COLOR COLOR_WHITE
116 #define DEFAULT_INTERVAL 3
117 #define DEFAULT_TRIGGER FALSE
118
119 enum
120 {
121   PROP_0,
122   PROP_MODE,
123   PROP_COLOR,
124   PROP_INTERVAL,
125   PROP_TRIGGER
126 };
127
128 #define COLORS 32
129 #define PATTERN 4
130 #define MAGIC_THRESHOLD 40
131 #define RATIO 0.95
132
133 static guint32 palettes[COLORS * PATTERN];
134
135 #define gst_radioactv_parent_class parent_class
136 G_DEFINE_TYPE (GstRadioacTV, gst_radioactv, GST_TYPE_VIDEO_FILTER);
137
138 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
139 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("RGBx")
140 #else
141 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("xBGR")
142 #endif
143
144 static GstStaticPadTemplate gst_radioactv_src_template =
145 GST_STATIC_PAD_TEMPLATE ("src",
146     GST_PAD_SRC,
147     GST_PAD_ALWAYS,
148     GST_STATIC_CAPS (CAPS_STR)
149     );
150
151 static GstStaticPadTemplate gst_radioactv_sink_template =
152 GST_STATIC_PAD_TEMPLATE ("sink",
153     GST_PAD_SINK,
154     GST_PAD_ALWAYS,
155     GST_STATIC_CAPS (CAPS_STR)
156     );
157
158 static void
159 makePalette (void)
160 {
161   gint i;
162
163 #define DELTA (255/(COLORS/2-1))
164
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;
169   }
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;
176   }
177   for (i = 0; i < COLORS; i++) {
178     palettes[COLORS * 3 + i] = (255 * i / COLORS) * 0x10101;
179   }
180   for (i = 0; i < COLORS * PATTERN; i++) {
181     palettes[i] = palettes[i] & 0xfefeff;
182   }
183 #undef DELTA
184 }
185
186 #define VIDEO_HWIDTH (filter->buf_width/2)
187 #define VIDEO_HHEIGHT (filter->buf_height/2)
188
189 /* this table assumes that video_width is times of 32 */
190 static void
191 setTable (GstRadioacTV * filter)
192 {
193   guint bits;
194   gint x, y, tx, ty, xx;
195   gint ptr, prevptr;
196
197   prevptr = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
198   for (xx = 0; xx < (filter->buf_width_blocks); xx++) {
199     bits = 0;
200     for (x = 0; x < 32; x++) {
201       ptr = (gint) (0.5 + RATIO * (xx * 32 + x - VIDEO_HWIDTH) + VIDEO_HWIDTH);
202       bits = bits >> 1;
203       if (ptr != prevptr)
204         bits |= 0x80000000;
205       prevptr = ptr;
206     }
207     filter->blurzoomx[xx] = bits;
208   }
209
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) +
213       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;
220   }
221 }
222
223 #undef VIDEO_HWIDTH
224 #undef VIDEO_HHEIGHT
225
226 static void
227 blur (GstRadioacTV * filter)
228 {
229   gint x, y;
230   gint width;
231   guint8 *p, *q;
232   guint8 v;
233
234   width = filter->buf_width;
235   p = filter->blurzoombuf + GST_VIDEO_INFO_WIDTH (&filter->info) + 1;
236   q = p + filter->buf_area;
237
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;
241       if (v == 255)
242         v = 0;
243       *q = v;
244       p++;
245       q++;
246     }
247     p += 2;
248     q += 2;
249   }
250 }
251
252 static void
253 zoom (GstRadioacTV * filter)
254 {
255   gint b, x, y;
256   guint8 *p, *q;
257   gint blocks, height;
258   gint dx;
259
260   p = filter->blurzoombuf + filter->buf_area;
261   q = filter->blurzoombuf;
262   height = filter->buf_height;
263   blocks = filter->buf_width_blocks;
264
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++) {
270         p += (dx & 1);
271         *q++ = *p;
272         dx = dx >> 1;
273       }
274     }
275   }
276 }
277
278 static void
279 blurzoomcore (GstRadioacTV * filter)
280 {
281   blur (filter);
282   zoom (filter);
283 }
284
285 /* Background image is refreshed every frame */
286 static void
287 image_bgsubtract_update_y (guint32 * src, gint16 * background, guint8 * diff,
288     gint video_area, gint y_threshold)
289 {
290   gint i;
291   gint R, G, B;
292   guint32 *p;
293   gint16 *q;
294   guint8 *r;
295   gint v;
296
297   p = src;
298   q = background;
299   r = diff;
300   for (i = 0; i < video_area; i++) {
301     R = ((*p) & 0xff0000) >> (16 - 1);
302     G = ((*p) & 0xff00) >> (8 - 2);
303     B = (*p) & 0xff;
304     v = (R + G + B) - (gint) (*q);
305     *q = (gint16) (R + G + B);
306     *r = ((v + y_threshold) >> 24) | ((y_threshold - v) >> 24);
307
308     p++;
309     q++;
310     r++;
311   }
312 }
313
314 static GstFlowReturn
315 gst_radioactv_transform (GstBaseTransform * trans, GstBuffer * in,
316     GstBuffer * out)
317 {
318   GstRadioacTV *filter = GST_RADIOACTV (trans);
319   guint32 *src, *dest;
320   GstVideoFrame in_frame, out_frame;
321   GstClockTime timestamp, stream_time;
322   gint x, y, width, height;
323   guint32 a, b;
324   guint8 *diff, *p;
325   guint32 *palette;
326
327   timestamp = GST_BUFFER_TIMESTAMP (in);
328   stream_time =
329       gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
330
331   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
332       GST_TIME_ARGS (timestamp));
333
334   if (GST_CLOCK_TIME_IS_VALID (stream_time))
335     gst_object_sync_values (GST_OBJECT (filter), stream_time);
336
337   if (!gst_video_frame_map (&in_frame, &filter->info, in, GST_MAP_READ))
338     goto invalid_in;
339
340   if (!gst_video_frame_map (&out_frame, &filter->info, out, GST_MAP_WRITE))
341     goto invalid_out;
342
343   src = GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
344   dest = GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
345
346   width = GST_VIDEO_FRAME_WIDTH (&in_frame);
347   height = GST_VIDEO_FRAME_HEIGHT (&in_frame);
348
349   GST_OBJECT_LOCK (filter);
350   palette = &palettes[COLORS * filter->color];
351   diff = filter->diff;
352
353   if (filter->mode == 3 && filter->trigger)
354     filter->snaptime = 0;
355   else if (filter->mode == 3 && !filter->trigger)
356     filter->snaptime = 1;
357
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;
367         }
368         diff += width;
369         p += filter->buf_width;
370       }
371       if (filter->mode == 1 || filter->mode == 2) {
372         memcpy (filter->snapframe, src, width * height * 4);
373       }
374     }
375   }
376   blurzoomcore (filter);
377
378   if (filter->mode == 1 || filter->mode == 2) {
379     src = filter->snapframe;
380   }
381   p = filter->blurzoombuf;
382   for (y = 0; y < height; y++) {
383     for (x = 0; x < filter->buf_margin_left; x++) {
384       *dest++ = *src++;
385     }
386     for (x = 0; x < filter->buf_width; x++) {
387       a = *src++ & 0xfefeff;
388       b = palette[*p++];
389       a += b;
390       b = a & 0x1010100;
391       *dest++ = a | (b - (b >> 8));
392     }
393     for (x = 0; x < filter->buf_margin_right; x++) {
394       *dest++ = *src++;
395     }
396   }
397
398   if (filter->mode == 1 || filter->mode == 2) {
399     filter->snaptime--;
400     if (filter->snaptime < 0) {
401       filter->snaptime = filter->interval;
402     }
403   }
404   GST_OBJECT_UNLOCK (filter);
405
406   gst_video_frame_unmap (&in_frame);
407   gst_video_frame_unmap (&out_frame);
408
409   return GST_FLOW_OK;
410
411   /* ERRORS */
412 invalid_in:
413   {
414     GST_DEBUG_OBJECT (filter, "invalid input frame");
415     return GST_FLOW_ERROR;
416   }
417 invalid_out:
418   {
419     GST_DEBUG_OBJECT (filter, "invalid output frame");
420     gst_video_frame_unmap (&in_frame);
421     return GST_FLOW_ERROR;
422   }
423 }
424
425 static gboolean
426 gst_radioactv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
427     GstCaps * outcaps)
428 {
429   GstRadioacTV *filter = GST_RADIOACTV (btrans);
430   GstVideoInfo info;
431   gint width, height;
432
433   if (!gst_video_info_from_caps (&info, incaps))
434     goto invalid_caps;
435
436   filter->info = info;
437
438   width = GST_VIDEO_INFO_WIDTH (&info);
439   height = GST_VIDEO_INFO_HEIGHT (&info);
440
441   filter->buf_width_blocks = width / 32;
442   if (filter->buf_width_blocks > 255)
443     goto too_wide;
444
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;
451
452   if (filter->blurzoombuf)
453     g_free (filter->blurzoombuf);
454   filter->blurzoombuf = g_new0 (guint8, filter->buf_area * 2);
455
456   if (filter->blurzoomx)
457     g_free (filter->blurzoomx);
458   filter->blurzoomx = g_new0 (gint, filter->buf_width);
459
460   if (filter->blurzoomy)
461     g_free (filter->blurzoomy);
462   filter->blurzoomy = g_new0 (gint, filter->buf_height);
463
464   if (filter->snapframe)
465     g_free (filter->snapframe);
466   filter->snapframe = g_new (guint32, width * height);
467
468   if (filter->diff)
469     g_free (filter->diff);
470   filter->diff = g_new (guint8, width * height);
471
472   if (filter->background)
473     g_free (filter->background);
474   filter->background = g_new0 (gint16, width * height);
475
476   setTable (filter);
477
478   return TRUE;
479
480   /* ERRORS */
481 invalid_caps:
482   {
483     GST_DEBUG_OBJECT (filter, "invalid caps received");
484     return FALSE;
485   }
486 too_wide:
487   {
488     GST_DEBUG_OBJECT (filter, "frame too wide");
489     return FALSE;
490   }
491 }
492
493 static gboolean
494 gst_radioactv_start (GstBaseTransform * trans)
495 {
496   GstRadioacTV *filter = GST_RADIOACTV (trans);
497
498   filter->snaptime = 0;
499
500   return TRUE;
501 }
502
503 static void
504 gst_radioactv_finalize (GObject * object)
505 {
506   GstRadioacTV *filter = GST_RADIOACTV (object);
507
508   if (filter->snapframe)
509     g_free (filter->snapframe);
510   filter->snapframe = NULL;
511
512   if (filter->blurzoombuf)
513     g_free (filter->blurzoombuf);
514   filter->blurzoombuf = NULL;
515
516   if (filter->diff)
517     g_free (filter->diff);
518   filter->diff = NULL;
519
520   if (filter->background)
521     g_free (filter->background);
522   filter->background = NULL;
523
524   if (filter->blurzoomx)
525     g_free (filter->blurzoomx);
526   filter->blurzoomx = NULL;
527
528   if (filter->blurzoomy)
529     g_free (filter->blurzoomy);
530   filter->blurzoomy = NULL;
531
532   G_OBJECT_CLASS (parent_class)->finalize (object);
533 }
534
535 static void
536 gst_radioactv_set_property (GObject * object, guint prop_id,
537     const GValue * value, GParamSpec * pspec)
538 {
539   GstRadioacTV *filter = GST_RADIOACTV (object);
540
541   GST_OBJECT_LOCK (filter);
542   switch (prop_id) {
543     case PROP_MODE:
544       filter->mode = g_value_get_enum (value);
545       if (filter->mode == 3)
546         filter->snaptime = 1;
547       break;
548     case PROP_COLOR:
549       filter->color = g_value_get_enum (value);
550       break;
551     case PROP_INTERVAL:
552       filter->interval = g_value_get_uint (value);
553       break;
554     case PROP_TRIGGER:
555       filter->trigger = g_value_get_boolean (value);
556       break;
557     default:
558       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
559       break;
560   }
561   GST_OBJECT_UNLOCK (filter);
562 }
563
564 static void
565 gst_radioactv_get_property (GObject * object, guint prop_id,
566     GValue * value, GParamSpec * pspec)
567 {
568   GstRadioacTV *filter = GST_RADIOACTV (object);
569
570   switch (prop_id) {
571     case PROP_MODE:
572       g_value_set_enum (value, filter->mode);
573       break;
574     case PROP_COLOR:
575       g_value_set_enum (value, filter->color);
576       break;
577     case PROP_INTERVAL:
578       g_value_set_uint (value, filter->interval);
579       break;
580     case PROP_TRIGGER:
581       g_value_set_boolean (value, filter->trigger);
582       break;
583     default:
584       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
585       break;
586   }
587 }
588
589 static void
590 gst_radioactv_class_init (GstRadioacTVClass * klass)
591 {
592   GObjectClass *gobject_class = (GObjectClass *) klass;
593   GstElementClass *gstelement_class = (GstElementClass *) klass;
594   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
595
596   gobject_class->set_property = gst_radioactv_set_property;
597   gobject_class->get_property = gst_radioactv_get_property;
598
599   gobject_class->finalize = gst_radioactv_finalize;
600
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));
605
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));
610
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));
615
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));
620
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>");
626
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));
631
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);
635
636   makePalette ();
637 }
638
639 static void
640 gst_radioactv_init (GstRadioacTV * filter)
641 {
642   filter->mode = DEFAULT_MODE;
643   filter->color = DEFAULT_COLOR;
644   filter->interval = DEFAULT_INTERVAL;
645   filter->trigger = DEFAULT_TRIGGER;
646
647   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (filter));
648   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (filter));
649 }