controller: port to new controller location and api
[platform/upstream/gstreamer.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 ! videoconvert ! 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 #define DEFAULT_FEEDBACK FALSE
55
56 enum
57 {
58   PROP_0,
59   PROP_FEEDBACK
60 };
61
62 #define gst_streaktv_parent_class parent_class
63 G_DEFINE_TYPE (GstStreakTV, gst_streaktv, GST_TYPE_VIDEO_FILTER);
64
65 static GstStaticPadTemplate gst_streaktv_src_template =
66 GST_STATIC_PAD_TEMPLATE ("src",
67     GST_PAD_SRC,
68     GST_PAD_ALWAYS,
69     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ BGRx, RGBx, xBGR, xRGB }"))
70     );
71
72 static GstStaticPadTemplate gst_streaktv_sink_template =
73 GST_STATIC_PAD_TEMPLATE ("sink",
74     GST_PAD_SINK,
75     GST_PAD_ALWAYS,
76     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ BGRx, RGBx, xBGR, xRGB }"))
77     );
78
79
80 static GstFlowReturn
81 gst_streaktv_transform (GstBaseTransform * trans, GstBuffer * in,
82     GstBuffer * out)
83 {
84   GstStreakTV *filter = GST_STREAKTV (trans);
85   guint32 *src, *dest;
86   GstVideoFrame in_frame, out_frame;
87   gint i, cf;
88   gint video_area, width, height;
89   guint32 **planetable = filter->planetable;
90   gint plane = filter->plane;
91   guint stride_mask, stride_shift, stride;
92
93   if (!gst_video_frame_map (&in_frame, &filter->info, in, GST_MAP_READ))
94     goto invalid_in;
95
96   if (!gst_video_frame_map (&out_frame, &filter->info, out, GST_MAP_WRITE))
97     goto invalid_out;
98
99   src = GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
100   dest = GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
101
102   width = GST_VIDEO_FRAME_WIDTH (&in_frame);
103   height = GST_VIDEO_FRAME_HEIGHT (&in_frame);
104
105   video_area = width * height;
106
107   GST_OBJECT_LOCK (filter);
108   if (filter->feedback) {
109     stride_mask = 0xfcfcfcfc;
110     stride = 8;
111     stride_shift = 2;
112   } else {
113     stride_mask = 0xf8f8f8f8;
114     stride = 4;
115     stride_shift = 3;
116   }
117
118   for (i = 0; i < video_area; i++) {
119     planetable[plane][i] = (src[i] & stride_mask) >> stride_shift;
120   }
121
122   cf = plane & (stride - 1);
123   if (filter->feedback) {
124     for (i = 0; i < video_area; i++) {
125       dest[i] = planetable[cf][i]
126           + planetable[cf + stride][i]
127           + planetable[cf + stride * 2][i]
128           + planetable[cf + stride * 3][i];
129       planetable[plane][i] = (dest[i] & stride_mask) >> stride_shift;
130     }
131   } else {
132     for (i = 0; i < video_area; i++) {
133       dest[i] = planetable[cf][i]
134           + planetable[cf + stride][i]
135           + planetable[cf + stride * 2][i]
136           + planetable[cf + stride * 3][i]
137           + planetable[cf + stride * 4][i]
138           + planetable[cf + stride * 5][i]
139           + planetable[cf + stride * 6][i]
140           + planetable[cf + stride * 7][i];
141     }
142   }
143
144   plane++;
145   filter->plane = plane & (PLANES - 1);
146   GST_OBJECT_UNLOCK (filter);
147
148   gst_video_frame_unmap (&in_frame);
149   gst_video_frame_unmap (&out_frame);
150
151   return GST_FLOW_OK;
152
153   /* ERRORS */
154 invalid_in:
155   {
156     GST_DEBUG_OBJECT (filter, "invalid input frame");
157     return GST_FLOW_ERROR;
158   }
159 invalid_out:
160   {
161     GST_DEBUG_OBJECT (filter, "invalid output frame");
162     gst_video_frame_unmap (&in_frame);
163     return GST_FLOW_ERROR;
164   }
165 }
166
167 static gboolean
168 gst_streaktv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
169     GstCaps * outcaps)
170 {
171   GstStreakTV *filter = GST_STREAKTV (btrans);
172   GstVideoInfo info;
173   gint i, width, height;
174
175   if (!gst_video_info_from_caps (&info, incaps))
176     goto invalid_caps;
177
178   filter->info = info;
179
180   width = GST_VIDEO_INFO_WIDTH (&info);
181   height = GST_VIDEO_INFO_HEIGHT (&info);
182
183   if (filter->planebuffer)
184     g_free (filter->planebuffer);
185
186   filter->planebuffer = g_new0 (guint32, width * height * 4 * PLANES);
187
188   for (i = 0; i < PLANES; i++)
189     filter->planetable[i] = &filter->planebuffer[width * height * i];
190
191   return TRUE;
192
193   /* ERRORS */
194 invalid_caps:
195   {
196     GST_DEBUG_OBJECT (filter, "invalid caps received");
197     return FALSE;
198   }
199 }
200
201 static gboolean
202 gst_streaktv_start (GstBaseTransform * trans)
203 {
204   GstStreakTV *filter = GST_STREAKTV (trans);
205
206   filter->plane = 0;
207
208   return TRUE;
209 }
210
211 static void
212 gst_streaktv_finalize (GObject * object)
213 {
214   GstStreakTV *filter = GST_STREAKTV (object);
215
216   if (filter->planebuffer) {
217     g_free (filter->planebuffer);
218     filter->planebuffer = NULL;
219   }
220
221   G_OBJECT_CLASS (parent_class)->finalize (object);
222 }
223
224 static void
225 gst_streaktv_set_property (GObject * object, guint prop_id,
226     const GValue * value, GParamSpec * pspec)
227 {
228   GstStreakTV *filter = GST_STREAKTV (object);
229
230   switch (prop_id) {
231     case PROP_FEEDBACK:
232       if (G_UNLIKELY (GST_STATE (filter) >= GST_STATE_PAUSED)) {
233         g_warning ("Changing the \"feedback\" property only allowed "
234             "in state < PLAYING");
235         return;
236       }
237
238       filter->feedback = g_value_get_boolean (value);
239       break;
240     default:
241       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242       break;
243   }
244 }
245
246 static void
247 gst_streaktv_get_property (GObject * object, guint prop_id, GValue * value,
248     GParamSpec * pspec)
249 {
250   GstStreakTV *filter = GST_STREAKTV (object);
251
252   switch (prop_id) {
253     case PROP_FEEDBACK:
254       g_value_set_boolean (value, filter->feedback);
255       break;
256     default:
257       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258       break;
259   }
260 }
261
262 static void
263 gst_streaktv_class_init (GstStreakTVClass * klass)
264 {
265   GObjectClass *gobject_class = (GObjectClass *) klass;
266   GstElementClass *gstelement_class = (GstElementClass *) klass;
267   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
268
269   gobject_class->set_property = gst_streaktv_set_property;
270   gobject_class->get_property = gst_streaktv_get_property;
271
272   gobject_class->finalize = gst_streaktv_finalize;
273
274   g_object_class_install_property (gobject_class, PROP_FEEDBACK,
275       g_param_spec_boolean ("feedback", "Feedback",
276           "Feedback", DEFAULT_FEEDBACK,
277           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
278
279   gst_element_class_set_details_simple (gstelement_class, "StreakTV effect",
280       "Filter/Effect/Video",
281       "StreakTV makes after images of moving objects",
282       "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
283       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
284
285   gst_element_class_add_pad_template (gstelement_class,
286       gst_static_pad_template_get (&gst_streaktv_sink_template));
287   gst_element_class_add_pad_template (gstelement_class,
288       gst_static_pad_template_get (&gst_streaktv_src_template));
289
290   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_streaktv_set_caps);
291   trans_class->transform = GST_DEBUG_FUNCPTR (gst_streaktv_transform);
292   trans_class->start = GST_DEBUG_FUNCPTR (gst_streaktv_start);
293 }
294
295 static void
296 gst_streaktv_init (GstStreakTV * filter)
297 {
298   filter->feedback = DEFAULT_FEEDBACK;
299
300   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (filter));
301   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (filter));
302 }