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