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 * @title: quarktv
30 *
31 * QuarkTV disolves moving objects. It picks up pixels from
32 * the last frames randomly.
33 *
34 * ## Example launch line
35 * |[
36 * gst-launch-1.0 -v videotestsrc ! quarktv ! videoconvert ! autovideosink
37 * ]| This pipeline shows the effect of quarktv on a test stream.
38 *
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 parameter */
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 GST_ELEMENT_REGISTER_DEFINE (quarktv, "quarktv", GST_RANK_NONE,
64 GST_TYPE_QUARKTV);
65
66 static void gst_quarktv_planetable_clear (GstQuarkTV * filter);
67
68 static GstStaticPadTemplate gst_quarktv_src_template =
69 GST_STATIC_PAD_TEMPLATE ("src",
70 GST_PAD_SRC,
71 GST_PAD_ALWAYS,
72 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ xRGB, xBGR, BGRx, RGBx }"))
73 );
74
75 static GstStaticPadTemplate gst_quarktv_sink_template =
76 GST_STATIC_PAD_TEMPLATE ("sink",
77 GST_PAD_SINK,
78 GST_PAD_ALWAYS,
79 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ xRGB, xBGR, BGRx, RGBx }"))
80 );
81
82 static gboolean
gst_quarktv_set_info(GstVideoFilter * vfilter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)83 gst_quarktv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
84 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
85 {
86 GstQuarkTV *filter = GST_QUARKTV (vfilter);
87 gint width, height;
88
89 width = GST_VIDEO_INFO_WIDTH (in_info);
90 height = GST_VIDEO_INFO_HEIGHT (in_info);
91
92 gst_quarktv_planetable_clear (filter);
93 filter->area = width * height;
94
95 return TRUE;
96 }
97
98 static GstFlowReturn
gst_quarktv_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)99 gst_quarktv_transform_frame (GstVideoFilter * vfilter, GstVideoFrame * in_frame,
100 GstVideoFrame * out_frame)
101 {
102 GstQuarkTV *filter = GST_QUARKTV (vfilter);
103 gint area;
104 guint32 *src, *dest;
105 GstClockTime timestamp;
106 GstBuffer **planetable;
107 gint planes, current_plane;
108
109 timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
110 timestamp =
111 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (vfilter)->segment,
112 GST_FORMAT_TIME, timestamp);
113
114 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
115 GST_TIME_ARGS (timestamp));
116
117 if (GST_CLOCK_TIME_IS_VALID (timestamp))
118 gst_object_sync_values (GST_OBJECT (filter), timestamp);
119
120 if (G_UNLIKELY (filter->planetable == NULL))
121 return GST_FLOW_FLUSHING;
122
123 src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
124 dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
125
126 GST_OBJECT_LOCK (filter);
127 area = filter->area;
128 planetable = filter->planetable;
129 planes = filter->planes;
130 current_plane = filter->current_plane;
131
132 if (planetable[current_plane])
133 gst_buffer_unref (planetable[current_plane]);
134 planetable[current_plane] = gst_buffer_ref (in_frame->buffer);
135
136 /* For each pixel */
137 while (--area) {
138 GstBuffer *rand;
139
140 /* pick a random buffer */
141 rand = planetable[(current_plane + (fastrand () >> 24)) % planes];
142
143 /* Copy the pixel from the random buffer to dest, FIXME, slow */
144 if (rand)
145 gst_buffer_extract (rand, area * 4, &dest[area], 4);
146 else
147 dest[area] = src[area];
148 }
149
150 filter->current_plane--;
151 if (filter->current_plane < 0)
152 filter->current_plane = planes - 1;
153 GST_OBJECT_UNLOCK (filter);
154
155 return GST_FLOW_OK;
156 }
157
158 static void
gst_quarktv_planetable_clear(GstQuarkTV * filter)159 gst_quarktv_planetable_clear (GstQuarkTV * filter)
160 {
161 gint i;
162
163 if (filter->planetable == NULL)
164 return;
165
166 for (i = 0; i < filter->planes; i++) {
167 if (GST_IS_BUFFER (filter->planetable[i])) {
168 gst_buffer_unref (filter->planetable[i]);
169 }
170 filter->planetable[i] = NULL;
171 }
172 }
173
174 static gboolean
gst_quarktv_start(GstBaseTransform * trans)175 gst_quarktv_start (GstBaseTransform * trans)
176 {
177 GstQuarkTV *filter = GST_QUARKTV (trans);
178
179 if (filter->planetable) {
180 gst_quarktv_planetable_clear (filter);
181 g_free (filter->planetable);
182 }
183 filter->planetable =
184 (GstBuffer **) g_malloc0 (filter->planes * sizeof (GstBuffer *));
185
186 return TRUE;
187 }
188
189 static void
gst_quarktv_finalize(GObject * object)190 gst_quarktv_finalize (GObject * object)
191 {
192 GstQuarkTV *filter = GST_QUARKTV (object);
193
194 if (filter->planetable) {
195 gst_quarktv_planetable_clear (filter);
196 g_free (filter->planetable);
197 filter->planetable = NULL;
198 }
199
200 G_OBJECT_CLASS (parent_class)->finalize (object);
201 }
202
203 static void
gst_quarktv_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)204 gst_quarktv_set_property (GObject * object, guint prop_id, const GValue * value,
205 GParamSpec * pspec)
206 {
207 GstQuarkTV *filter = GST_QUARKTV (object);
208
209 GST_OBJECT_LOCK (filter);
210 switch (prop_id) {
211 case PROP_PLANES:
212 {
213 gint new_n_planes = g_value_get_int (value);
214 GstBuffer **new_planetable;
215 gint i;
216
217 /* If the number of planes changed, copy across any existing planes */
218 if (new_n_planes != filter->planes) {
219 new_planetable =
220 (GstBuffer **) g_malloc0 (new_n_planes * sizeof (GstBuffer *));
221
222 if (filter->planetable) {
223 for (i = 0; (i < new_n_planes) && (i < filter->planes); i++) {
224 new_planetable[i] = filter->planetable[i];
225 }
226 for (; i < filter->planes; i++) {
227 if (filter->planetable[i])
228 gst_buffer_unref (filter->planetable[i]);
229 }
230 g_free (filter->planetable);
231 }
232
233 filter->planetable = new_planetable;
234 filter->planes = new_n_planes;
235 filter->current_plane = filter->planes - 1;
236 }
237 break;
238 }
239 default:
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241 break;
242 }
243 GST_OBJECT_UNLOCK (filter);
244 }
245
246 static void
gst_quarktv_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)247 gst_quarktv_get_property (GObject * object, guint prop_id, GValue * value,
248 GParamSpec * pspec)
249 {
250 GstQuarkTV *filter = GST_QUARKTV (object);
251
252 switch (prop_id) {
253 case PROP_PLANES:
254 g_value_set_int (value, filter->planes);
255 break;
256 default:
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258 break;
259 }
260 }
261
262 static void
gst_quarktv_class_init(GstQuarkTVClass * klass)263 gst_quarktv_class_init (GstQuarkTVClass * klass)
264 {
265 GObjectClass *gobject_class = (GObjectClass *) klass;
266 GstElementClass *gstelement_class = (GstElementClass *) klass;
267 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
268 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
269
270 gobject_class->set_property = gst_quarktv_set_property;
271 gobject_class->get_property = gst_quarktv_get_property;
272
273 gobject_class->finalize = gst_quarktv_finalize;
274
275 g_object_class_install_property (gobject_class, PROP_PLANES,
276 g_param_spec_int ("planes", "Planes",
277 "Number of planes", 1, 64, PLANES,
278 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
279
280 gst_element_class_set_static_metadata (gstelement_class, "QuarkTV effect",
281 "Filter/Effect/Video",
282 "Motion dissolver", "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>");
283
284 gst_element_class_add_static_pad_template (gstelement_class,
285 &gst_quarktv_sink_template);
286 gst_element_class_add_static_pad_template (gstelement_class,
287 &gst_quarktv_src_template);
288
289 trans_class->start = GST_DEBUG_FUNCPTR (gst_quarktv_start);
290
291 vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_quarktv_set_info);
292 vfilter_class->transform_frame =
293 GST_DEBUG_FUNCPTR (gst_quarktv_transform_frame);
294 }
295
296 static void
gst_quarktv_init(GstQuarkTV * filter)297 gst_quarktv_init (GstQuarkTV * filter)
298 {
299 filter->planes = PLANES;
300 filter->current_plane = filter->planes - 1;
301 }
302