controller: port to new controller location and api
[platform/upstream/gstreamer.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 ! 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
81 gst_quarktv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
82     GstCaps * outcaps)
83 {
84   GstQuarkTV *filter = GST_QUARKTV (btrans);
85   GstVideoInfo info;
86   gint width, height;
87
88   if (!gst_video_info_from_caps (&info, incaps))
89     goto invalid_caps;
90
91   filter->info = info;
92
93   width = GST_VIDEO_INFO_WIDTH (&info);
94   height = GST_VIDEO_INFO_HEIGHT (&info);
95
96   gst_quarktv_planetable_clear (filter);
97   filter->area = width * height;
98
99   return TRUE;
100
101   /* ERRORS */
102 invalid_caps:
103   {
104     GST_DEBUG_OBJECT (filter, "invalid caps received");
105     return FALSE;
106   }
107 }
108
109 static GstFlowReturn
110 gst_quarktv_transform (GstBaseTransform * trans, GstBuffer * in,
111     GstBuffer * out)
112 {
113   GstQuarkTV *filter = GST_QUARKTV (trans);
114   gint area;
115   guint32 *src, *dest;
116   GstClockTime timestamp;
117   GstBuffer **planetable;
118   gint planes, current_plane;
119   GstVideoFrame in_frame, out_frame;
120
121   timestamp = GST_BUFFER_TIMESTAMP (in);
122   timestamp =
123       gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
124
125   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
126       GST_TIME_ARGS (timestamp));
127
128   if (GST_CLOCK_TIME_IS_VALID (timestamp))
129     gst_object_sync_values (GST_OBJECT (filter), timestamp);
130
131   if (G_UNLIKELY (filter->planetable == NULL))
132     return GST_FLOW_WRONG_STATE;
133
134   if (!gst_video_frame_map (&in_frame, &filter->info, in, GST_MAP_READ))
135     goto invalid_in;
136
137   if (!gst_video_frame_map (&out_frame, &filter->info, out, GST_MAP_WRITE))
138     goto invalid_out;
139
140   src = GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
141   dest = GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
142
143   GST_OBJECT_LOCK (filter);
144   area = filter->area;
145   planetable = filter->planetable;
146   planes = filter->planes;
147   current_plane = filter->current_plane;
148
149   if (planetable[current_plane])
150     gst_buffer_unref (planetable[current_plane]);
151   planetable[current_plane] = gst_buffer_ref (in);
152
153   /* For each pixel */
154   while (--area) {
155     GstBuffer *rand;
156
157     /* pick a random buffer */
158     rand = planetable[(current_plane + (fastrand () >> 24)) % planes];
159
160     /* Copy the pixel from the random buffer to dest, FIXME, slow */
161     if (rand)
162       gst_buffer_extract (rand, area * 4, &dest[area], 4);
163     else
164       dest[area] = src[area];
165   }
166
167   filter->current_plane--;
168   if (filter->current_plane < 0)
169     filter->current_plane = planes - 1;
170   GST_OBJECT_UNLOCK (filter);
171
172   gst_video_frame_unmap (&in_frame);
173   gst_video_frame_unmap (&out_frame);
174
175   return GST_FLOW_OK;
176
177   /* ERRORS */
178 invalid_in:
179   {
180     GST_DEBUG_OBJECT (filter, "invalid input frame");
181     return GST_FLOW_ERROR;
182   }
183 invalid_out:
184   {
185     GST_DEBUG_OBJECT (filter, "invalid output frame");
186     gst_video_frame_unmap (&in_frame);
187     return GST_FLOW_ERROR;
188   }
189 }
190
191 static void
192 gst_quarktv_planetable_clear (GstQuarkTV * filter)
193 {
194   gint i;
195
196   if (filter->planetable == NULL)
197     return;
198
199   for (i = 0; i < filter->planes; i++) {
200     if (GST_IS_BUFFER (filter->planetable[i])) {
201       gst_buffer_unref (filter->planetable[i]);
202     }
203     filter->planetable[i] = NULL;
204   }
205 }
206
207 static gboolean
208 gst_quarktv_start (GstBaseTransform * trans)
209 {
210   GstQuarkTV *filter = GST_QUARKTV (trans);
211
212   if (filter->planetable) {
213     gst_quarktv_planetable_clear (filter);
214     g_free (filter->planetable);
215   }
216   filter->planetable =
217       (GstBuffer **) g_malloc0 (filter->planes * sizeof (GstBuffer *));
218
219   return TRUE;
220 }
221
222 static void
223 gst_quarktv_finalize (GObject * object)
224 {
225   GstQuarkTV *filter = GST_QUARKTV (object);
226
227   if (filter->planetable) {
228     gst_quarktv_planetable_clear (filter);
229     g_free (filter->planetable);
230     filter->planetable = NULL;
231   }
232
233   G_OBJECT_CLASS (parent_class)->finalize (object);
234 }
235
236 static void
237 gst_quarktv_set_property (GObject * object, guint prop_id, const GValue * value,
238     GParamSpec * pspec)
239 {
240   GstQuarkTV *filter = GST_QUARKTV (object);
241
242   GST_OBJECT_LOCK (filter);
243   switch (prop_id) {
244     case PROP_PLANES:
245     {
246       gint new_n_planes = g_value_get_int (value);
247       GstBuffer **new_planetable;
248       gint i;
249
250       /* If the number of planes changed, copy across any existing planes */
251       if (new_n_planes != filter->planes) {
252         new_planetable =
253             (GstBuffer **) g_malloc0 (new_n_planes * sizeof (GstBuffer *));
254
255         if (filter->planetable) {
256           for (i = 0; (i < new_n_planes) && (i < filter->planes); i++) {
257             new_planetable[i] = filter->planetable[i];
258           }
259           for (; i < filter->planes; i++) {
260             if (filter->planetable[i])
261               gst_buffer_unref (filter->planetable[i]);
262           }
263           g_free (filter->planetable);
264         }
265
266         filter->planetable = new_planetable;
267         filter->planes = new_n_planes;
268         filter->current_plane = filter->planes - 1;
269       }
270       break;
271     }
272     default:
273       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274       break;
275   }
276   GST_OBJECT_UNLOCK (filter);
277 }
278
279 static void
280 gst_quarktv_get_property (GObject * object, guint prop_id, GValue * value,
281     GParamSpec * pspec)
282 {
283   GstQuarkTV *filter = GST_QUARKTV (object);
284
285   switch (prop_id) {
286     case PROP_PLANES:
287       g_value_set_int (value, filter->planes);
288       break;
289     default:
290       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291       break;
292   }
293 }
294
295 static void
296 gst_quarktv_class_init (GstQuarkTVClass * klass)
297 {
298   GObjectClass *gobject_class = (GObjectClass *) klass;
299   GstElementClass *gstelement_class = (GstElementClass *) klass;
300   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
301
302   gobject_class->set_property = gst_quarktv_set_property;
303   gobject_class->get_property = gst_quarktv_get_property;
304
305   gobject_class->finalize = gst_quarktv_finalize;
306
307   g_object_class_install_property (gobject_class, PROP_PLANES,
308       g_param_spec_int ("planes", "Planes",
309           "Number of planes", 0, 64, PLANES,
310           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
311
312   gst_element_class_set_details_simple (gstelement_class, "QuarkTV effect",
313       "Filter/Effect/Video",
314       "Motion dissolver", "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>");
315
316   gst_element_class_add_pad_template (gstelement_class,
317       gst_static_pad_template_get (&gst_quarktv_sink_template));
318   gst_element_class_add_pad_template (gstelement_class,
319       gst_static_pad_template_get (&gst_quarktv_src_template));
320
321   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_quarktv_set_caps);
322   trans_class->transform = GST_DEBUG_FUNCPTR (gst_quarktv_transform);
323   trans_class->start = GST_DEBUG_FUNCPTR (gst_quarktv_start);
324 }
325
326 static void
327 gst_quarktv_init (GstQuarkTV * filter)
328 {
329   filter->planes = PLANES;
330   filter->current_plane = filter->planes - 1;
331
332   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (filter));
333   gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (filter));
334 }