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., 51 Franklin St, Fifth Floor,
28  * Boston, MA 02110-1301, 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-1.0 -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
gst_streaktv_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)81 gst_streaktv_transform_frame (GstVideoFilter * vfilter,
82     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
83 {
84   GstStreakTV *filter = GST_STREAKTV (vfilter);
85   guint32 *src, *dest;
86   gint i, cf;
87   gint video_area, width, height;
88   guint32 **planetable = filter->planetable;
89   gint plane = filter->plane;
90   guint stride_mask, stride_shift, stride;
91 
92   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
93   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
94 
95   width = GST_VIDEO_FRAME_WIDTH (in_frame);
96   height = GST_VIDEO_FRAME_HEIGHT (in_frame);
97 
98   video_area = width * height;
99 
100   GST_OBJECT_LOCK (filter);
101   if (filter->feedback) {
102     stride_mask = 0xfcfcfcfc;
103     stride = 8;
104     stride_shift = 2;
105   } else {
106     stride_mask = 0xf8f8f8f8;
107     stride = 4;
108     stride_shift = 3;
109   }
110 
111   for (i = 0; i < video_area; i++) {
112     planetable[plane][i] = (src[i] & stride_mask) >> stride_shift;
113   }
114 
115   cf = plane & (stride - 1);
116   if (filter->feedback) {
117     for (i = 0; i < video_area; i++) {
118       dest[i] = planetable[cf][i]
119           + planetable[cf + stride][i]
120           + planetable[cf + stride * 2][i]
121           + planetable[cf + stride * 3][i];
122       planetable[plane][i] = (dest[i] & stride_mask) >> stride_shift;
123     }
124   } else {
125     for (i = 0; i < video_area; i++) {
126       dest[i] = planetable[cf][i]
127           + planetable[cf + stride][i]
128           + planetable[cf + stride * 2][i]
129           + planetable[cf + stride * 3][i]
130           + planetable[cf + stride * 4][i]
131           + planetable[cf + stride * 5][i]
132           + planetable[cf + stride * 6][i]
133           + planetable[cf + stride * 7][i];
134     }
135   }
136 
137   plane++;
138   filter->plane = plane & (PLANES - 1);
139   GST_OBJECT_UNLOCK (filter);
140 
141   return GST_FLOW_OK;
142 }
143 
144 static gboolean
gst_streaktv_set_info(GstVideoFilter * vfilter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)145 gst_streaktv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
146     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
147 {
148   GstStreakTV *filter = GST_STREAKTV (vfilter);
149   gint i, width, height;
150 
151   width = GST_VIDEO_INFO_WIDTH (in_info);
152   height = GST_VIDEO_INFO_HEIGHT (in_info);
153 
154   g_free (filter->planebuffer);
155 
156   filter->planebuffer = g_new0 (guint32, width * height * 4 * PLANES);
157 
158   for (i = 0; i < PLANES; i++)
159     filter->planetable[i] = &filter->planebuffer[width * height * i];
160 
161   return TRUE;
162 }
163 
164 static gboolean
gst_streaktv_start(GstBaseTransform * trans)165 gst_streaktv_start (GstBaseTransform * trans)
166 {
167   GstStreakTV *filter = GST_STREAKTV (trans);
168 
169   filter->plane = 0;
170 
171   return TRUE;
172 }
173 
174 static void
gst_streaktv_finalize(GObject * object)175 gst_streaktv_finalize (GObject * object)
176 {
177   GstStreakTV *filter = GST_STREAKTV (object);
178 
179   if (filter->planebuffer) {
180     g_free (filter->planebuffer);
181     filter->planebuffer = NULL;
182   }
183 
184   G_OBJECT_CLASS (parent_class)->finalize (object);
185 }
186 
187 static void
gst_streaktv_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)188 gst_streaktv_set_property (GObject * object, guint prop_id,
189     const GValue * value, GParamSpec * pspec)
190 {
191   GstStreakTV *filter = GST_STREAKTV (object);
192 
193   switch (prop_id) {
194     case PROP_FEEDBACK:
195       if (G_UNLIKELY (GST_STATE (filter) >= GST_STATE_PAUSED)) {
196         g_warning ("Changing the \"feedback\" property only allowed "
197             "in state < PLAYING");
198         return;
199       }
200 
201       filter->feedback = g_value_get_boolean (value);
202       break;
203     default:
204       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205       break;
206   }
207 }
208 
209 static void
gst_streaktv_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)210 gst_streaktv_get_property (GObject * object, guint prop_id, GValue * value,
211     GParamSpec * pspec)
212 {
213   GstStreakTV *filter = GST_STREAKTV (object);
214 
215   switch (prop_id) {
216     case PROP_FEEDBACK:
217       g_value_set_boolean (value, filter->feedback);
218       break;
219     default:
220       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221       break;
222   }
223 }
224 
225 static void
gst_streaktv_class_init(GstStreakTVClass * klass)226 gst_streaktv_class_init (GstStreakTVClass * klass)
227 {
228   GObjectClass *gobject_class = (GObjectClass *) klass;
229   GstElementClass *gstelement_class = (GstElementClass *) klass;
230   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
231   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
232 
233   gobject_class->set_property = gst_streaktv_set_property;
234   gobject_class->get_property = gst_streaktv_get_property;
235 
236   gobject_class->finalize = gst_streaktv_finalize;
237 
238   g_object_class_install_property (gobject_class, PROP_FEEDBACK,
239       g_param_spec_boolean ("feedback", "Feedback",
240           "Feedback", DEFAULT_FEEDBACK,
241           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
242 
243   gst_element_class_set_static_metadata (gstelement_class, "StreakTV effect",
244       "Filter/Effect/Video",
245       "StreakTV makes after images of moving objects",
246       "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
247       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
248 
249   gst_element_class_add_static_pad_template (gstelement_class,
250       &gst_streaktv_sink_template);
251   gst_element_class_add_static_pad_template (gstelement_class,
252       &gst_streaktv_src_template);
253 
254   trans_class->start = GST_DEBUG_FUNCPTR (gst_streaktv_start);
255 
256   vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_streaktv_set_info);
257   vfilter_class->transform_frame =
258       GST_DEBUG_FUNCPTR (gst_streaktv_transform_frame);
259 }
260 
261 static void
gst_streaktv_init(GstStreakTV * filter)262 gst_streaktv_init (GstStreakTV * filter)
263 {
264   filter->feedback = DEFAULT_FEEDBACK;
265 }
266