Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / effectv / gststreak.c
1 /* GStreamer
2  * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
3  *
4  * EffecTV - Realtime Digital Video Effector
5  * Copyright (C) 2001-2006 FUKUCHI Kentaro
6  *
7  * StreakTV - afterimage effector.
8  * Copyright (C) 2001-2002 FUKUCHI Kentaro
9  *
10  * This combines the StreakTV and BaltanTV effects, which are
11  * very similar. BaltanTV is used if the feedback property is set
12  * to TRUE, otherwise StreakTV is used.
13  *
14  * EffecTV is free software. This library is free software;
15  * you can redistribute it and/or
16  * modify it under the terms of the GNU Library General Public
17  * License as published by the Free Software Foundation; either
18  * version 2 of the License, or (at your option) any later version.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * Library General Public License for more details.
24  *
25  * You should have received a copy of the GNU Library General Public
26  * License along with this library; if not, write to the
27  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28  * Boston, MA 02111-1307, USA.
29  */
30
31 /**
32  * SECTION:element-streaktv
33  *
34  * StreakTV makes after images of moving objects.
35  *
36  * <refsect2>
37  * <title>Example launch line</title>
38  * |[
39  * gst-launch -v videotestsrc ! streaktv ! ffmpegcolorspace ! autovideosink
40  * ]| This pipeline shows the effect of streaktv on a test stream.
41  * </refsect2>
42  */
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <math.h>
49 #include <string.h>
50
51 #include "gststreak.h"
52 #include "gsteffectv.h"
53
54 #include <gst/video/video.h>
55
56 #define DEFAULT_FEEDBACK FALSE
57
58 enum
59 {
60   PROP_0,
61   PROP_FEEDBACK
62 };
63
64 GST_BOILERPLATE (GstStreakTV, gst_streaktv, GstVideoFilter,
65     GST_TYPE_VIDEO_FILTER);
66
67 static GstStaticPadTemplate gst_streaktv_src_template =
68     GST_STATIC_PAD_TEMPLATE ("src",
69     GST_PAD_SRC,
70     GST_PAD_ALWAYS,
71     GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx "; " GST_VIDEO_CAPS_RGBx ";"
72         GST_VIDEO_CAPS_xBGR "; " GST_VIDEO_CAPS_xRGB)
73     );
74
75 static GstStaticPadTemplate gst_streaktv_sink_template =
76     GST_STATIC_PAD_TEMPLATE ("sink",
77     GST_PAD_SINK,
78     GST_PAD_ALWAYS,
79     GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx "; " GST_VIDEO_CAPS_RGBx ";"
80         GST_VIDEO_CAPS_xBGR "; " GST_VIDEO_CAPS_xRGB)
81     );
82
83
84
85 static GstFlowReturn
86 gst_streaktv_transform (GstBaseTransform * trans, GstBuffer * in,
87     GstBuffer * out)
88 {
89   GstStreakTV *filter = GST_STREAKTV (trans);
90   guint32 *src, *dest;
91   GstFlowReturn ret = GST_FLOW_OK;
92   gint i, cf;
93   gint video_area = filter->width * filter->height;
94   guint32 **planetable = filter->planetable;
95   gint plane = filter->plane;
96   guint stride_mask, stride_shift, stride;
97
98   GST_OBJECT_LOCK (filter);
99   if (filter->feedback) {
100     stride_mask = 0xfcfcfcfc;
101     stride = 8;
102     stride_shift = 2;
103   } else {
104     stride_mask = 0xf8f8f8f8;
105     stride = 4;
106     stride_shift = 3;
107   }
108
109   src = (guint32 *) GST_BUFFER_DATA (in);
110   dest = (guint32 *) GST_BUFFER_DATA (out);
111
112   for (i = 0; i < video_area; i++) {
113     planetable[plane][i] = (src[i] & stride_mask) >> stride_shift;
114   }
115
116   cf = plane & (stride - 1);
117   if (filter->feedback) {
118     for (i = 0; i < video_area; i++) {
119       dest[i] = planetable[cf][i]
120           + planetable[cf + stride][i]
121           + planetable[cf + stride * 2][i]
122           + planetable[cf + stride * 3][i];
123       planetable[plane][i] = (dest[i] & stride_mask) >> stride_shift;
124     }
125   } else {
126     for (i = 0; i < video_area; i++) {
127       dest[i] = planetable[cf][i]
128           + planetable[cf + stride][i]
129           + planetable[cf + stride * 2][i]
130           + planetable[cf + stride * 3][i]
131           + planetable[cf + stride * 4][i]
132           + planetable[cf + stride * 5][i]
133           + planetable[cf + stride * 6][i]
134           + planetable[cf + stride * 7][i];
135     }
136   }
137
138   plane++;
139   filter->plane = plane & (PLANES - 1);
140   GST_OBJECT_UNLOCK (filter);
141
142   return ret;
143 }
144
145 static gboolean
146 gst_streaktv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
147     GstCaps * outcaps)
148 {
149   GstStreakTV *filter = GST_STREAKTV (btrans);
150   GstStructure *structure;
151   gboolean ret = FALSE;
152
153   structure = gst_caps_get_structure (incaps, 0);
154
155   GST_OBJECT_LOCK (filter);
156   if (gst_structure_get_int (structure, "width", &filter->width) &&
157       gst_structure_get_int (structure, "height", &filter->height)) {
158     gint i;
159
160     if (filter->planebuffer)
161       g_free (filter->planebuffer);
162
163     filter->planebuffer =
164         g_new0 (guint32, filter->width * filter->height * 4 * PLANES);
165     for (i = 0; i < PLANES; i++)
166       filter->planetable[i] =
167           &filter->planebuffer[filter->width * filter->height * i];
168
169     ret = TRUE;
170   }
171   GST_OBJECT_UNLOCK (filter);
172
173   return ret;
174 }
175
176 static gboolean
177 gst_streaktv_start (GstBaseTransform * trans)
178 {
179   GstStreakTV *filter = GST_STREAKTV (trans);
180
181   filter->plane = 0;
182
183   return TRUE;
184 }
185
186 static void
187 gst_streaktv_finalize (GObject * object)
188 {
189   GstStreakTV *filter = GST_STREAKTV (object);
190
191   if (filter->planebuffer) {
192     g_free (filter->planebuffer);
193     filter->planebuffer = NULL;
194   }
195
196   G_OBJECT_CLASS (parent_class)->finalize (object);
197 }
198
199 static void
200 gst_streaktv_set_property (GObject * object, guint prop_id,
201     const GValue * value, GParamSpec * pspec)
202 {
203   GstStreakTV *filter = GST_STREAKTV (object);
204
205   switch (prop_id) {
206     case PROP_FEEDBACK:
207       if (G_UNLIKELY (GST_STATE (filter) >= GST_STATE_PAUSED)) {
208         g_warning ("Changing the \"feedback\" property only allowed "
209             "in state < PLAYING");
210         return;
211       }
212
213       filter->feedback = g_value_get_boolean (value);
214       break;
215     default:
216       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217       break;
218   }
219 }
220
221 static void
222 gst_streaktv_get_property (GObject * object, guint prop_id, GValue * value,
223     GParamSpec * pspec)
224 {
225   GstStreakTV *filter = GST_STREAKTV (object);
226
227   switch (prop_id) {
228     case PROP_FEEDBACK:
229       g_value_set_boolean (value, filter->feedback);
230       break;
231     default:
232       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233       break;
234   }
235 }
236
237 static void
238 gst_streaktv_base_init (gpointer g_class)
239 {
240   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
241
242   gst_element_class_set_details_simple (element_class, "StreakTV effect",
243       "Filter/Effect/Video",
244       "StreakTV makes after images of moving objects",
245       "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
246       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
247
248   gst_element_class_add_static_pad_template (element_class,
249       &gst_streaktv_sink_template);
250   gst_element_class_add_static_pad_template (element_class,
251       &gst_streaktv_src_template);
252 }
253
254 static void
255 gst_streaktv_class_init (GstStreakTVClass * klass)
256 {
257   GObjectClass *gobject_class = (GObjectClass *) klass;
258   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
259
260   gobject_class->set_property = gst_streaktv_set_property;
261   gobject_class->get_property = gst_streaktv_get_property;
262
263   gobject_class->finalize = gst_streaktv_finalize;
264
265   g_object_class_install_property (gobject_class, PROP_FEEDBACK,
266       g_param_spec_boolean ("feedback", "Feedback",
267           "Feedback", DEFAULT_FEEDBACK,
268           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
269
270   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_streaktv_set_caps);
271   trans_class->transform = GST_DEBUG_FUNCPTR (gst_streaktv_transform);
272   trans_class->start = GST_DEBUG_FUNCPTR (gst_streaktv_start);
273 }
274
275 static void
276 gst_streaktv_init (GstStreakTV * filter, GstStreakTVClass * klass)
277 {
278   filter->feedback = DEFAULT_FEEDBACK;
279 }