effectv: Add basic documentation for the effectv elements
[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 ! 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/video/video.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 GST_BOILERPLATE (GstQuarkTV, gst_quarktv, GstVideoFilter,
64     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_BGRx "; " GST_VIDEO_CAPS_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_BGRx "; " GST_VIDEO_CAPS_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   GstStructure *structure;
88   gboolean ret = FALSE;
89
90   structure = gst_caps_get_structure (incaps, 0);
91
92   if (gst_structure_get_int (structure, "width", &filter->width) &&
93       gst_structure_get_int (structure, "height", &filter->height)) {
94     gst_quarktv_planetable_clear (filter);
95     filter->area = filter->width * filter->height;
96     ret = TRUE;
97   }
98
99   return ret;
100 }
101
102 static GstFlowReturn
103 gst_quarktv_transform (GstBaseTransform * trans, GstBuffer * in,
104     GstBuffer * out)
105 {
106   GstQuarkTV *filter = GST_QUARKTV (trans);
107   gint area;
108   guint32 *src, *dest;
109   GstFlowReturn ret = GST_FLOW_OK;
110
111   area = filter->area;
112   src = (guint32 *) GST_BUFFER_DATA (in);
113   dest = (guint32 *) GST_BUFFER_DATA (out);
114
115   if (G_UNLIKELY (filter->planetable == NULL))
116     return GST_FLOW_WRONG_STATE;
117
118   if (filter->planetable[filter->current_plane])
119     gst_buffer_unref (filter->planetable[filter->current_plane]);
120
121   filter->planetable[filter->current_plane] = gst_buffer_ref (in);
122
123   /* For each pixel */
124   while (--area) {
125     GstBuffer *rand;
126
127     /* pick a random buffer */
128     rand =
129         filter->planetable[(filter->current_plane +
130             (fastrand () >> 24)) % filter->planes];
131
132     /* Copy the pixel from the random buffer to dest */
133     dest[area] =
134         (rand ? ((guint32 *) GST_BUFFER_DATA (rand))[area] : src[area]);
135   }
136
137   filter->current_plane--;
138   if (filter->current_plane < 0)
139     filter->current_plane = filter->planes - 1;
140
141   return ret;
142 }
143
144 static void
145 gst_quarktv_planetable_clear (GstQuarkTV * filter)
146 {
147   gint i;
148
149   if (filter->planetable == NULL)
150     return;
151
152   for (i = 0; i < filter->planes; i++) {
153     if (GST_IS_BUFFER (filter->planetable[i])) {
154       gst_buffer_unref (filter->planetable[i]);
155     }
156     filter->planetable[i] = NULL;
157   }
158 }
159
160 static gboolean
161 gst_quarktv_start (GstBaseTransform * trans)
162 {
163   GstQuarkTV *filter = GST_QUARKTV (trans);
164
165   if (filter->planetable) {
166     gst_quarktv_planetable_clear (filter);
167     g_free (filter->planetable);
168   }
169   filter->planetable =
170       (GstBuffer **) g_malloc0 (filter->planes * sizeof (GstBuffer *));
171
172   return TRUE;
173 }
174
175 static void
176 gst_quarktv_finalize (GObject * object)
177 {
178   GstQuarkTV *filter = GST_QUARKTV (object);
179
180   if (filter->planetable) {
181     gst_quarktv_planetable_clear (filter);
182     g_free (filter->planetable);
183     filter->planetable = NULL;
184   }
185
186   G_OBJECT_CLASS (parent_class)->finalize (object);
187 }
188
189 static void
190 gst_quarktv_set_property (GObject * object, guint prop_id, const GValue * value,
191     GParamSpec * pspec)
192 {
193   GstQuarkTV *filter = GST_QUARKTV (object);
194
195   switch (prop_id) {
196     case PROP_PLANES:
197     {
198       gint new_n_planes = g_value_get_int (value);
199       GstBuffer **new_planetable;
200       gint i;
201
202       /* If the number of planes changed, copy across any existing planes */
203       if (new_n_planes != filter->planes) {
204         new_planetable =
205             (GstBuffer **) g_malloc0 (new_n_planes * sizeof (GstBuffer *));
206
207         if (filter->planetable) {
208           for (i = 0; (i < new_n_planes) && (i < filter->planes); i++) {
209             new_planetable[i] = filter->planetable[i];
210           }
211           for (; i < filter->planes; i++) {
212             if (filter->planetable[i])
213               gst_buffer_unref (filter->planetable[i]);
214           }
215           g_free (filter->planetable);
216         }
217
218         filter->planetable = new_planetable;
219         filter->planes = new_n_planes;
220         filter->current_plane = filter->planes - 1;
221       }
222       break;
223     }
224     default:
225       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226       break;
227   }
228 }
229
230 static void
231 gst_quarktv_get_property (GObject * object, guint prop_id, GValue * value,
232     GParamSpec * pspec)
233 {
234   GstQuarkTV *filter = GST_QUARKTV (object);
235
236   switch (prop_id) {
237     case PROP_PLANES:
238       g_value_set_int (value, filter->planes);
239       break;
240     default:
241       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242       break;
243   }
244 }
245
246 static void
247 gst_quarktv_base_init (gpointer g_class)
248 {
249   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
250
251   gst_element_class_set_details_simple (element_class, "QuarkTV effect",
252       "Filter/Effect/Video",
253       "Motion dissolver", "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>");
254
255   gst_element_class_add_pad_template (element_class,
256       gst_static_pad_template_get (&gst_quarktv_sink_template));
257   gst_element_class_add_pad_template (element_class,
258       gst_static_pad_template_get (&gst_quarktv_src_template));
259 }
260
261 static void
262 gst_quarktv_class_init (GstQuarkTVClass * klass)
263 {
264   GObjectClass *gobject_class = (GObjectClass *) klass;
265   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
266
267   gobject_class->set_property = gst_quarktv_set_property;
268   gobject_class->get_property = gst_quarktv_get_property;
269
270   gobject_class->finalize = gst_quarktv_finalize;
271
272   g_object_class_install_property (gobject_class, PROP_PLANES,
273       g_param_spec_int ("planes", "Planes",
274           "Number of planes", 0, 64, PLANES,
275           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
276
277   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_quarktv_set_caps);
278   trans_class->transform = GST_DEBUG_FUNCPTR (gst_quarktv_transform);
279   trans_class->start = GST_DEBUG_FUNCPTR (gst_quarktv_start);
280 }
281
282 static void
283 gst_quarktv_init (GstQuarkTV * filter, GstQuarkTVClass * klass)
284 {
285   filter->planes = PLANES;
286   filter->current_plane = filter->planes - 1;
287 }