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