1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  *
5  * EffecTV:
6  * Copyright (C) 2001-2002 FUKUCHI Kentarou
7  *
8  * QuarkTV - motion disolver.
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 option) any later version.
15  *
16  * This library is distributed in the hope 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 copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24  * Boston, MA 02110-1301, USA.
25  */
26 
27 /**
28  * SECTION:element-quarktv
29  *
30  * QuarkTV disolves moving objects. It picks up pixels from
31  * the last frames randomly.
32  *
33  * <refsect2>
34  * <title>Example launch line</title>
35  * |[
36  * gst-launch-1.0 -v videotestsrc ! quarktv ! videoconvert ! autovideosink
37  * ]| This pipeline shows the effect of quarktv on a test stream.
38  * </refsect2>
39  */
40 
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44 
45 #include <math.h>
46 #include <string.h>
47 
48 #include "gstquark.h"
49 #include "gsteffectv.h"
50 
51 /* number of frames of time-buffer. It should be as a configurable paramater */
52 /* This number also must be 2^n just for the speed. */
53 #define PLANES 16
54 
55 enum
56 {
57   PROP_0,
58   PROP_PLANES
59 };
60 
61 #define gst_quarktv_parent_class parent_class
62 G_DEFINE_TYPE (GstQuarkTV, gst_quarktv, GST_TYPE_VIDEO_FILTER);
63 
64 static void gst_quarktv_planetable_clear (GstQuarkTV * filter);
65 
66 static GstStaticPadTemplate gst_quarktv_src_template =
67 GST_STATIC_PAD_TEMPLATE ("src",
68     GST_PAD_SRC,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ xRGB, xBGR, BGRx, RGBx }"))
71     );
72 
73 static GstStaticPadTemplate gst_quarktv_sink_template =
74 GST_STATIC_PAD_TEMPLATE ("sink",
75     GST_PAD_SINK,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ xRGB, xBGR, BGRx, RGBx }"))
78     );
79 
80 static gboolean
gst_quarktv_set_info(GstVideoFilter * vfilter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)81 gst_quarktv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
82     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
83 {
84   GstQuarkTV *filter = GST_QUARKTV (vfilter);
85   gint width, height;
86 
87   width = GST_VIDEO_INFO_WIDTH (in_info);
88   height = GST_VIDEO_INFO_HEIGHT (in_info);
89 
90   gst_quarktv_planetable_clear (filter);
91   filter->area = width * height;
92 
93   return TRUE;
94 }
95 
96 static GstFlowReturn
gst_quarktv_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)97 gst_quarktv_transform_frame (GstVideoFilter * vfilter, GstVideoFrame * in_frame,
98     GstVideoFrame * out_frame)
99 {
100   GstQuarkTV *filter = GST_QUARKTV (vfilter);
101   gint area;
102   guint32 *src, *dest;
103   GstClockTime timestamp;
104   GstBuffer **planetable;
105   gint planes, current_plane;
106 
107   timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
108   timestamp =
109       gst_segment_to_stream_time (&GST_BASE_TRANSFORM (vfilter)->segment,
110       GST_FORMAT_TIME, timestamp);
111 
112   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
113       GST_TIME_ARGS (timestamp));
114 
115   if (GST_CLOCK_TIME_IS_VALID (timestamp))
116     gst_object_sync_values (GST_OBJECT (filter), timestamp);
117 
118   if (G_UNLIKELY (filter->planetable == NULL))
119     return GST_FLOW_FLUSHING;
120 
121   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
122   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
123 
124   GST_OBJECT_LOCK (filter);
125   area = filter->area;
126   planetable = filter->planetable;
127   planes = filter->planes;
128   current_plane = filter->current_plane;
129 
130   if (planetable[current_plane])
131     gst_buffer_unref (planetable[current_plane]);
132   planetable[current_plane] = gst_buffer_ref (in_frame->buffer);
133 
134   /* For each pixel */
135   while (--area) {
136     GstBuffer *rand;
137 
138     /* pick a random buffer */
139     rand = planetable[(current_plane + (fastrand () >> 24)) % planes];
140 
141     /* Copy the pixel from the random buffer to dest, FIXME, slow */
142     if (rand)
143       gst_buffer_extract (rand, area * 4, &dest[area], 4);
144     else
145       dest[area] = src[area];
146   }
147 
148   filter->current_plane--;
149   if (filter->current_plane < 0)
150     filter->current_plane = planes - 1;
151   GST_OBJECT_UNLOCK (filter);
152 
153   return GST_FLOW_OK;
154 }
155 
156 static void
gst_quarktv_planetable_clear(GstQuarkTV * filter)157 gst_quarktv_planetable_clear (GstQuarkTV * filter)
158 {
159   gint i;
160 
161   if (filter->planetable == NULL)
162     return;
163 
164   for (i = 0; i < filter->planes; i++) {
165     if (GST_IS_BUFFER (filter->planetable[i])) {
166       gst_buffer_unref (filter->planetable[i]);
167     }
168     filter->planetable[i] = NULL;
169   }
170 }
171 
172 static gboolean
gst_quarktv_start(GstBaseTransform * trans)173 gst_quarktv_start (GstBaseTransform * trans)
174 {
175   GstQuarkTV *filter = GST_QUARKTV (trans);
176 
177   if (filter->planetable) {
178     gst_quarktv_planetable_clear (filter);
179     g_free (filter->planetable);
180   }
181   filter->planetable =
182       (GstBuffer **) g_malloc0 (filter->planes * sizeof (GstBuffer *));
183 
184   return TRUE;
185 }
186 
187 static void
gst_quarktv_finalize(GObject * object)188 gst_quarktv_finalize (GObject * object)
189 {
190   GstQuarkTV *filter = GST_QUARKTV (object);
191 
192   if (filter->planetable) {
193     gst_quarktv_planetable_clear (filter);
194     g_free (filter->planetable);
195     filter->planetable = NULL;
196   }
197 
198   G_OBJECT_CLASS (parent_class)->finalize (object);
199 }
200 
201 static void
gst_quarktv_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)202 gst_quarktv_set_property (GObject * object, guint prop_id, const GValue * value,
203     GParamSpec * pspec)
204 {
205   GstQuarkTV *filter = GST_QUARKTV (object);
206 
207   GST_OBJECT_LOCK (filter);
208   switch (prop_id) {
209     case PROP_PLANES:
210     {
211       gint new_n_planes = g_value_get_int (value);
212       GstBuffer **new_planetable;
213       gint i;
214 
215       /* If the number of planes changed, copy across any existing planes */
216       if (new_n_planes != filter->planes) {
217         new_planetable =
218             (GstBuffer **) g_malloc0 (new_n_planes * sizeof (GstBuffer *));
219 
220         if (filter->planetable) {
221           for (i = 0; (i < new_n_planes) && (i < filter->planes); i++) {
222             new_planetable[i] = filter->planetable[i];
223           }
224           for (; i < filter->planes; i++) {
225             if (filter->planetable[i])
226               gst_buffer_unref (filter->planetable[i]);
227           }
228           g_free (filter->planetable);
229         }
230 
231         filter->planetable = new_planetable;
232         filter->planes = new_n_planes;
233         filter->current_plane = filter->planes - 1;
234       }
235       break;
236     }
237     default:
238       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239       break;
240   }
241   GST_OBJECT_UNLOCK (filter);
242 }
243 
244 static void
gst_quarktv_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)245 gst_quarktv_get_property (GObject * object, guint prop_id, GValue * value,
246     GParamSpec * pspec)
247 {
248   GstQuarkTV *filter = GST_QUARKTV (object);
249 
250   switch (prop_id) {
251     case PROP_PLANES:
252       g_value_set_int (value, filter->planes);
253       break;
254     default:
255       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256       break;
257   }
258 }
259 
260 static void
gst_quarktv_class_init(GstQuarkTVClass * klass)261 gst_quarktv_class_init (GstQuarkTVClass * klass)
262 {
263   GObjectClass *gobject_class = (GObjectClass *) klass;
264   GstElementClass *gstelement_class = (GstElementClass *) klass;
265   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
266   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
267 
268   gobject_class->set_property = gst_quarktv_set_property;
269   gobject_class->get_property = gst_quarktv_get_property;
270 
271   gobject_class->finalize = gst_quarktv_finalize;
272 
273   g_object_class_install_property (gobject_class, PROP_PLANES,
274       g_param_spec_int ("planes", "Planes",
275           "Number of planes", 1, 64, PLANES,
276           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
277 
278   gst_element_class_set_static_metadata (gstelement_class, "QuarkTV effect",
279       "Filter/Effect/Video",
280       "Motion dissolver", "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>");
281 
282   gst_element_class_add_static_pad_template (gstelement_class,
283       &gst_quarktv_sink_template);
284   gst_element_class_add_static_pad_template (gstelement_class,
285       &gst_quarktv_src_template);
286 
287   trans_class->start = GST_DEBUG_FUNCPTR (gst_quarktv_start);
288 
289   vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_quarktv_set_info);
290   vfilter_class->transform_frame =
291       GST_DEBUG_FUNCPTR (gst_quarktv_transform_frame);
292 }
293 
294 static void
gst_quarktv_init(GstQuarkTV * filter)295 gst_quarktv_init (GstQuarkTV * filter)
296 {
297   filter->planes = PLANES;
298   filter->current_plane = filter->planes - 1;
299 }
300