Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / effectv / gstquark.c
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., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, 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 -v videotestsrc ! quarktv ! ffmpegcolorspace ! 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 #include <gst/controller/gstcontroller.h>
52 #include <gst/video/video.h>
53
54 /* number of frames of time-buffer. It should be as a configurable paramater */
55 /* This number also must be 2^n just for the speed. */
56 #define PLANES 16
57
58 enum
59 {
60   PROP_0,
61   PROP_PLANES
62 };
63
64 GST_BOILERPLATE (GstQuarkTV, gst_quarktv, GstVideoFilter,
65     GST_TYPE_VIDEO_FILTER);
66
67 static void gst_quarktv_planetable_clear (GstQuarkTV * filter);
68
69 static GstStaticPadTemplate gst_quarktv_src_template =
70     GST_STATIC_PAD_TEMPLATE ("src",
71     GST_PAD_SRC,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR ";"
74         GST_VIDEO_CAPS_BGRx "; " GST_VIDEO_CAPS_RGBx)
75     );
76
77 static GstStaticPadTemplate gst_quarktv_sink_template =
78     GST_STATIC_PAD_TEMPLATE ("sink",
79     GST_PAD_SINK,
80     GST_PAD_ALWAYS,
81     GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_xBGR ";"
82         GST_VIDEO_CAPS_BGRx "; " GST_VIDEO_CAPS_RGBx)
83     );
84
85 static gboolean
86 gst_quarktv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
87     GstCaps * outcaps)
88 {
89   GstQuarkTV *filter = GST_QUARKTV (btrans);
90   GstStructure *structure;
91   gboolean ret = FALSE;
92
93   structure = gst_caps_get_structure (incaps, 0);
94
95   GST_OBJECT_LOCK (filter);
96   if (gst_structure_get_int (structure, "width", &filter->width) &&
97       gst_structure_get_int (structure, "height", &filter->height)) {
98     gst_quarktv_planetable_clear (filter);
99     filter->area = filter->width * filter->height;
100     ret = TRUE;
101   }
102   GST_OBJECT_UNLOCK (filter);
103
104   return ret;
105 }
106
107 static GstFlowReturn
108 gst_quarktv_transform (GstBaseTransform * trans, GstBuffer * in,
109     GstBuffer * out)
110 {
111   GstQuarkTV *filter = GST_QUARKTV (trans);
112   gint area;
113   guint32 *src, *dest;
114   GstFlowReturn ret = GST_FLOW_OK;
115   GstClockTime timestamp;
116   GstBuffer **planetable;
117   gint planes, current_plane;
118
119   timestamp = GST_BUFFER_TIMESTAMP (in);
120   timestamp =
121       gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
122
123   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
124       GST_TIME_ARGS (timestamp));
125
126   if (GST_CLOCK_TIME_IS_VALID (timestamp))
127     gst_object_sync_values (G_OBJECT (filter), timestamp);
128
129   if (G_UNLIKELY (filter->planetable == NULL))
130     return GST_FLOW_WRONG_STATE;
131
132   GST_OBJECT_LOCK (filter);
133   area = filter->area;
134   src = (guint32 *) GST_BUFFER_DATA (in);
135   dest = (guint32 *) GST_BUFFER_DATA (out);
136   planetable = filter->planetable;
137   planes = filter->planes;
138   current_plane = filter->current_plane;
139
140   if (planetable[current_plane])
141     gst_buffer_unref (planetable[current_plane]);
142   planetable[current_plane] = gst_buffer_ref (in);
143
144   /* For each pixel */
145   while (--area) {
146     GstBuffer *rand;
147
148     /* pick a random buffer */
149     rand = planetable[(current_plane + (fastrand () >> 24)) % planes];
150
151     /* Copy the pixel from the random buffer to dest */
152     dest[area] =
153         (rand ? ((guint32 *) GST_BUFFER_DATA (rand))[area] : src[area]);
154   }
155
156   filter->current_plane--;
157   if (filter->current_plane < 0)
158     filter->current_plane = planes - 1;
159   GST_OBJECT_UNLOCK (filter);
160
161   return ret;
162 }
163
164 static void
165 gst_quarktv_planetable_clear (GstQuarkTV * filter)
166 {
167   gint i;
168
169   if (filter->planetable == NULL)
170     return;
171
172   for (i = 0; i < filter->planes; i++) {
173     if (GST_IS_BUFFER (filter->planetable[i])) {
174       gst_buffer_unref (filter->planetable[i]);
175     }
176     filter->planetable[i] = NULL;
177   }
178 }
179
180 static gboolean
181 gst_quarktv_start (GstBaseTransform * trans)
182 {
183   GstQuarkTV *filter = GST_QUARKTV (trans);
184
185   if (filter->planetable) {
186     gst_quarktv_planetable_clear (filter);
187     g_free (filter->planetable);
188   }
189   filter->planetable =
190       (GstBuffer **) g_malloc0 (filter->planes * sizeof (GstBuffer *));
191
192   return TRUE;
193 }
194
195 static void
196 gst_quarktv_finalize (GObject * object)
197 {
198   GstQuarkTV *filter = GST_QUARKTV (object);
199
200   if (filter->planetable) {
201     gst_quarktv_planetable_clear (filter);
202     g_free (filter->planetable);
203     filter->planetable = NULL;
204   }
205
206   G_OBJECT_CLASS (parent_class)->finalize (object);
207 }
208
209 static void
210 gst_quarktv_set_property (GObject * object, guint prop_id, const GValue * value,
211     GParamSpec * pspec)
212 {
213   GstQuarkTV *filter = GST_QUARKTV (object);
214
215   GST_OBJECT_LOCK (filter);
216   switch (prop_id) {
217     case PROP_PLANES:
218     {
219       gint new_n_planes = g_value_get_int (value);
220       GstBuffer **new_planetable;
221       gint i;
222
223       /* If the number of planes changed, copy across any existing planes */
224       if (new_n_planes != filter->planes) {
225         new_planetable =
226             (GstBuffer **) g_malloc0 (new_n_planes * sizeof (GstBuffer *));
227
228         if (filter->planetable) {
229           for (i = 0; (i < new_n_planes) && (i < filter->planes); i++) {
230             new_planetable[i] = filter->planetable[i];
231           }
232           for (; i < filter->planes; i++) {
233             if (filter->planetable[i])
234               gst_buffer_unref (filter->planetable[i]);
235           }
236           g_free (filter->planetable);
237         }
238
239         filter->planetable = new_planetable;
240         filter->planes = new_n_planes;
241         filter->current_plane = filter->planes - 1;
242       }
243       break;
244     }
245     default:
246       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247       break;
248   }
249   GST_OBJECT_UNLOCK (filter);
250 }
251
252 static void
253 gst_quarktv_get_property (GObject * object, guint prop_id, GValue * value,
254     GParamSpec * pspec)
255 {
256   GstQuarkTV *filter = GST_QUARKTV (object);
257
258   switch (prop_id) {
259     case PROP_PLANES:
260       g_value_set_int (value, filter->planes);
261       break;
262     default:
263       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264       break;
265   }
266 }
267
268 static void
269 gst_quarktv_base_init (gpointer g_class)
270 {
271   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
272
273   gst_element_class_set_details_simple (element_class, "QuarkTV effect",
274       "Filter/Effect/Video",
275       "Motion dissolver", "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>");
276
277   gst_element_class_add_static_pad_template (element_class,
278       &gst_quarktv_sink_template);
279   gst_element_class_add_static_pad_template (element_class,
280       &gst_quarktv_src_template);
281 }
282
283 static void
284 gst_quarktv_class_init (GstQuarkTVClass * klass)
285 {
286   GObjectClass *gobject_class = (GObjectClass *) klass;
287   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
288
289   gobject_class->set_property = gst_quarktv_set_property;
290   gobject_class->get_property = gst_quarktv_get_property;
291
292   gobject_class->finalize = gst_quarktv_finalize;
293
294   g_object_class_install_property (gobject_class, PROP_PLANES,
295       g_param_spec_int ("planes", "Planes",
296           "Number of planes", 0, 64, PLANES,
297           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
298
299   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_quarktv_set_caps);
300   trans_class->transform = GST_DEBUG_FUNCPTR (gst_quarktv_transform);
301   trans_class->start = GST_DEBUG_FUNCPTR (gst_quarktv_start);
302 }
303
304 static void
305 gst_quarktv_init (GstQuarkTV * filter, GstQuarkTVClass * klass)
306 {
307   filter->planes = PLANES;
308   filter->current_plane = filter->planes - 1;
309 }