Remove unused variables in _class_init
[platform/upstream/gstreamer.git] / gst / effectv / gstdice.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * dice.c: a 'dicing' effect
5  *  copyright (c) 2001 Sam Mertens.  This code is subject to the provisions of
6  *  the GNU Library Public License.
7  *
8  * I suppose this looks similar to PuzzleTV, but it's not. The screen is
9  * divided into small squares, each of which is rotated either 0, 90, 180 or
10  * 270 degrees.  The amount of rotation for each square is chosen at random.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16
17 #include <gst/video/gstvideofilter.h>
18
19 #include <string.h>
20 #include <gst/gst.h>
21
22 #include <gst/video/video.h>
23
24 #define GST_TYPE_DICETV \
25   (gst_dicetv_get_type())
26 #define GST_DICETV(obj) \
27   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DICETV,GstDiceTV))
28 #define GST_DICETV_CLASS(klass) \
29   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_DICETV,GstDiceTVClass))
30 #define GST_IS_DICETV(obj) \
31   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DICETV))
32 #define GST_IS_DICETV_CLASS(klass) \
33   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DICETV))
34
35 typedef struct _GstDiceTV GstDiceTV;
36 typedef struct _GstDiceTVClass GstDiceTVClass;
37
38 #define DEFAULT_CUBE_BITS   4
39 #define MAX_CUBE_BITS       5
40 #define MIN_CUBE_BITS       0
41
42 typedef enum _dice_dir
43 {
44   DICE_UP = 0,
45   DICE_RIGHT = 1,
46   DICE_DOWN = 2,
47   DICE_LEFT = 3
48 }
49 DiceDir;
50
51 struct _GstDiceTV
52 {
53   GstVideoFilter videofilter;
54
55   gint width, height;
56   gchar *dicemap;
57
58   gint g_cube_bits;
59   gint g_cube_size;
60   gint g_map_height;
61   gint g_map_width;
62 };
63
64 struct _GstDiceTVClass
65 {
66   GstVideoFilterClass parent_class;
67 };
68
69 GType gst_dicetv_get_type (void);
70
71 static void gst_dicetv_create_map (GstDiceTV * filter);
72
73 static const GstElementDetails gst_dicetv_details =
74 GST_ELEMENT_DETAILS ("DiceTV effect",
75     "Filter/Effect/Video",
76     "'Dices' the screen up into many small squares",
77     "Wim Taymans <wim.taymans@chello.be>");
78
79 static GstStaticPadTemplate gst_dicetv_src_template =
80     GST_STATIC_PAD_TEMPLATE ("src",
81     GST_PAD_SRC,
82     GST_PAD_ALWAYS,
83     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
84         GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
85     );
86
87 static GstStaticPadTemplate gst_dicetv_sink_template =
88     GST_STATIC_PAD_TEMPLATE ("sink",
89     GST_PAD_SINK,
90     GST_PAD_ALWAYS,
91     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
92         GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
93     );
94
95 static GstVideoFilterClass *parent_class = NULL;
96
97 enum
98 {
99   ARG_0,
100   ARG_CUBE_BITS
101 };
102
103 static gboolean
104 gst_dicetv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
105     GstCaps * outcaps)
106 {
107   GstDiceTV *filter = GST_DICETV (btrans);
108   GstStructure *structure;
109   gboolean ret = FALSE;
110
111   structure = gst_caps_get_structure (incaps, 0);
112
113   if (gst_structure_get_int (structure, "width", &filter->width) &&
114       gst_structure_get_int (structure, "height", &filter->height)) {
115     g_free (filter->dicemap);
116     filter->dicemap =
117         (gchar *) g_malloc (filter->height * filter->width * sizeof (char));
118     gst_dicetv_create_map (filter);
119     ret = TRUE;
120   }
121
122   return ret;
123 }
124
125 static gboolean
126 gst_dicetv_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
127     guint * size)
128 {
129   GstDiceTV *filter;
130   GstStructure *structure;
131   gboolean ret = FALSE;
132   gint width, height;
133
134   filter = GST_DICETV (btrans);
135
136   structure = gst_caps_get_structure (caps, 0);
137
138   if (gst_structure_get_int (structure, "width", &width) &&
139       gst_structure_get_int (structure, "height", &height)) {
140     *size = width * height * 32 / 8;
141     ret = TRUE;
142     GST_DEBUG_OBJECT (filter, "our frame size is %d bytes (%dx%d)", *size,
143         width, height);
144   }
145
146   return ret;
147 }
148
149 static unsigned int
150 fastrand (void)
151 {
152   static unsigned int fastrand_val;
153
154   return (fastrand_val = fastrand_val * 1103515245 + 12345);
155 }
156
157 static GstFlowReturn
158 gst_dicetv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
159 {
160   GstDiceTV *filter;
161   guint32 *src, *dest;
162   gint i, map_x, map_y, map_i, base, dx, dy, di;
163   gint video_width, g_cube_bits, g_cube_size;
164   GstFlowReturn ret = GST_FLOW_OK;
165
166   filter = GST_DICETV (trans);
167   src = (guint32 *) GST_BUFFER_DATA (in);
168   dest = (guint32 *) GST_BUFFER_DATA (out);
169
170   gst_buffer_copy_metadata (out, in, GST_BUFFER_COPY_TIMESTAMPS);
171
172   video_width = filter->width;
173   g_cube_bits = filter->g_cube_bits;
174   g_cube_size = filter->g_cube_size;
175
176   map_i = 0;
177   for (map_y = 0; map_y < filter->g_map_height; map_y++) {
178     for (map_x = 0; map_x < filter->g_map_width; map_x++) {
179       base = (map_y << g_cube_bits) * video_width + (map_x << g_cube_bits);
180
181       switch (filter->dicemap[map_i]) {
182         case DICE_UP:
183           for (dy = 0; dy < g_cube_size; dy++) {
184             i = base + dy * video_width;
185             for (dx = 0; dx < g_cube_size; dx++) {
186               dest[i] = src[i];
187               i++;
188             }
189           }
190           break;
191         case DICE_LEFT:
192           for (dy = 0; dy < g_cube_size; dy++) {
193             i = base + dy * video_width;
194
195             for (dx = 0; dx < g_cube_size; dx++) {
196               di = base + (dx * video_width) + (g_cube_size - dy - 1);
197               dest[di] = src[i];
198               i++;
199             }
200           }
201           break;
202         case DICE_DOWN:
203           for (dy = 0; dy < g_cube_size; dy++) {
204             di = base + dy * video_width;
205             i = base + (g_cube_size - dy - 1) * video_width + g_cube_size;
206             for (dx = 0; dx < g_cube_size; dx++) {
207               i--;
208               dest[di] = src[i];
209               di++;
210             }
211           }
212           break;
213         case DICE_RIGHT:
214           for (dy = 0; dy < g_cube_size; dy++) {
215             i = base + (dy * video_width);
216             for (dx = 0; dx < g_cube_size; dx++) {
217               di = base + dy + (g_cube_size - dx - 1) * video_width;
218               dest[di] = src[i];
219               i++;
220             }
221           }
222           break;
223         default:
224           g_assert_not_reached ();
225           break;
226       }
227       map_i++;
228     }
229   }
230
231   return ret;
232 }
233
234 static void
235 gst_dicetv_create_map (GstDiceTV * filter)
236 {
237   gint x, y, i;
238
239   filter->g_map_height = filter->height >> filter->g_cube_bits;
240   filter->g_map_width = filter->width >> filter->g_cube_bits;
241   filter->g_cube_size = 1 << filter->g_cube_bits;
242
243   i = 0;
244
245   for (y = 0; y < filter->g_map_height; y++) {
246     for (x = 0; x < filter->g_map_width; x++) {
247       // dicemap[i] = ((i + y) & 0x3); /* Up, Down, Left or Right */
248       filter->dicemap[i] = (fastrand () >> 24) & 0x03;
249       i++;
250     }
251   }
252 }
253
254 static void
255 gst_dicetv_set_property (GObject * object, guint prop_id, const GValue * value,
256     GParamSpec * pspec)
257 {
258   GstDiceTV *filter;
259
260   g_return_if_fail (GST_IS_DICETV (object));
261
262   filter = GST_DICETV (object);
263
264   switch (prop_id) {
265     case ARG_CUBE_BITS:
266       filter->g_cube_bits = g_value_get_int (value);
267       gst_dicetv_create_map (filter);
268     default:
269       break;
270   }
271 }
272
273 static void
274 gst_dicetv_get_property (GObject * object, guint prop_id, GValue * value,
275     GParamSpec * pspec)
276 {
277   GstDiceTV *filter;
278
279   g_return_if_fail (GST_IS_DICETV (object));
280
281   filter = GST_DICETV (object);
282
283   switch (prop_id) {
284     case ARG_CUBE_BITS:
285       g_value_set_int (value, filter->g_cube_bits);
286       break;
287     default:
288       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289       break;
290   }
291 }
292
293 static void
294 gst_dicetv_base_init (gpointer g_class)
295 {
296   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
297
298   gst_element_class_set_details (element_class, &gst_dicetv_details);
299
300   gst_element_class_add_pad_template (element_class,
301       gst_static_pad_template_get (&gst_dicetv_sink_template));
302   gst_element_class_add_pad_template (element_class,
303       gst_static_pad_template_get (&gst_dicetv_src_template));
304 }
305
306 static void
307 gst_dicetv_class_init (gpointer klass, gpointer class_data)
308 {
309   GObjectClass *gobject_class;
310   GstBaseTransformClass *trans_class;
311
312   gobject_class = (GObjectClass *) klass;
313   trans_class = (GstBaseTransformClass *) klass;
314
315   parent_class = g_type_class_peek_parent (klass);
316
317   gobject_class->set_property = gst_dicetv_set_property;
318   gobject_class->get_property = gst_dicetv_get_property;
319
320   g_object_class_install_property (gobject_class, ARG_CUBE_BITS,
321       g_param_spec_int ("square_bits", "Square Bits", "The size of the Squares",
322           MIN_CUBE_BITS, MAX_CUBE_BITS, DEFAULT_CUBE_BITS, G_PARAM_READWRITE));
323
324   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_dicetv_set_caps);
325   trans_class->get_unit_size = GST_DEBUG_FUNCPTR (gst_dicetv_get_unit_size);
326   trans_class->transform = GST_DEBUG_FUNCPTR (gst_dicetv_transform);
327 }
328
329 static void
330 gst_dicetv_init (GTypeInstance * instance, gpointer g_class)
331 {
332   GstDiceTV *filter = GST_DICETV (instance);
333
334   filter->dicemap = NULL;
335   filter->g_cube_bits = DEFAULT_CUBE_BITS;
336   filter->g_cube_size = 0;
337   filter->g_map_height = 0;
338   filter->g_map_width = 0;
339 }
340
341 GType
342 gst_dicetv_get_type (void)
343 {
344   static GType dicetv_type = 0;
345
346   if (!dicetv_type) {
347     static const GTypeInfo dicetv_info = {
348       sizeof (GstDiceTVClass),
349       gst_dicetv_base_init,
350       NULL,
351       (GClassInitFunc) gst_dicetv_class_init,
352       NULL,
353       NULL,
354       sizeof (GstDiceTV),
355       0,
356       (GInstanceInitFunc) gst_dicetv_init,
357     };
358
359     dicetv_type =
360         g_type_register_static (GST_TYPE_VIDEO_FILTER, "GstDiceTV",
361         &dicetv_info, 0);
362   }
363   return dicetv_type;
364 }