2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
17 #include <gstvideofilter.h>
22 #include <gst/video/video.h>
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(obj) \
33 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DICETV))
35 typedef struct _GstDiceTV GstDiceTV;
36 typedef struct _GstDiceTVClass GstDiceTVClass;
38 #define DEFAULT_CUBE_BITS 4
39 #define MAX_CUBE_BITS 5
40 #define MIN_CUBE_BITS 0
42 typedef enum _dice_dir
53 GstVideofilter videofilter;
64 struct _GstDiceTVClass
66 GstVideofilterClass parent_class;
69 GType gst_dicetv_get_type (void);
71 static void gst_dicetv_create_map (GstDiceTV * filter);
73 static GstElementDetails gst_dicetv_details = GST_ELEMENT_DETAILS ("DiceTV",
74 "Filter/Effect/Video",
75 "'Dices' the screen up into many small squares",
76 "Wim Taymans <wim.taymans@chello.be>");
78 static GstStaticPadTemplate gst_dicetv_src_template =
79 GST_STATIC_PAD_TEMPLATE ("src",
82 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
83 GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
86 static GstStaticPadTemplate gst_dicetv_sink_template =
87 GST_STATIC_PAD_TEMPLATE ("sink",
90 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
91 GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
94 static GstVideofilterClass *parent_class = NULL;
103 gst_dicetv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
106 GstDiceTV *filter = GST_DICETV (btrans);
107 GstStructure *structure;
108 gboolean ret = FALSE;
110 structure = gst_caps_get_structure (incaps, 0);
112 if (gst_structure_get_int (structure, "width", &filter->width) &&
113 gst_structure_get_int (structure, "height", &filter->height)) {
114 g_free (filter->dicemap);
116 (gchar *) g_malloc (filter->height * filter->width * sizeof (char));
117 gst_dicetv_create_map (filter);
125 gst_dicetv_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
129 GstStructure *structure;
130 gboolean ret = FALSE;
133 filter = GST_DICETV (btrans);
135 structure = gst_caps_get_structure (caps, 0);
137 if (gst_structure_get_int (structure, "width", &width) &&
138 gst_structure_get_int (structure, "height", &height)) {
139 *size = width * height * 32 / 8;
141 GST_DEBUG_OBJECT (filter, "our frame size is %d bytes (%dx%d)", *size,
151 static unsigned int fastrand_val;
153 return (fastrand_val = fastrand_val * 1103515245 + 12345);
157 gst_dicetv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
161 gint i, map_x, map_y, map_i, base, dx, dy, di;
162 gint video_width, g_cube_bits, g_cube_size;
163 GstFlowReturn ret = GST_FLOW_OK;
165 filter = GST_DICETV (trans);
166 src = (guint32 *) GST_BUFFER_DATA (in);
167 dest = (guint32 *) GST_BUFFER_DATA (out);
169 gst_buffer_stamp (out, in);
171 video_width = filter->width;
172 g_cube_bits = filter->g_cube_bits;
173 g_cube_size = filter->g_cube_size;
176 for (map_y = 0; map_y < filter->g_map_height; map_y++) {
177 for (map_x = 0; map_x < filter->g_map_width; map_x++) {
178 base = (map_y << g_cube_bits) * video_width + (map_x << g_cube_bits);
180 switch (filter->dicemap[map_i]) {
182 for (dy = 0; dy < g_cube_size; dy++) {
183 i = base + dy * video_width;
184 for (dx = 0; dx < g_cube_size; dx++) {
191 for (dy = 0; dy < g_cube_size; dy++) {
192 i = base + dy * video_width;
194 for (dx = 0; dx < g_cube_size; dx++) {
195 di = base + (dx * video_width) + (g_cube_size - dy - 1);
202 for (dy = 0; dy < g_cube_size; dy++) {
203 di = base + dy * video_width;
204 i = base + (g_cube_size - dy - 1) * video_width + g_cube_size;
205 for (dx = 0; dx < g_cube_size; dx++) {
213 for (dy = 0; dy < g_cube_size; dy++) {
214 i = base + (dy * video_width);
215 for (dx = 0; dx < g_cube_size; dx++) {
216 di = base + dy + (g_cube_size - dx - 1) * video_width;
223 g_assert_not_reached ();
234 gst_dicetv_create_map (GstDiceTV * filter)
238 filter->g_map_height = filter->height >> filter->g_cube_bits;
239 filter->g_map_width = filter->width >> filter->g_cube_bits;
240 filter->g_cube_size = 1 << filter->g_cube_bits;
244 for (y = 0; y < filter->g_map_height; y++) {
245 for (x = 0; x < filter->g_map_width; x++) {
246 // dicemap[i] = ((i + y) & 0x3); /* Up, Down, Left or Right */
247 filter->dicemap[i] = (fastrand () >> 24) & 0x03;
254 gst_dicetv_set_property (GObject * object, guint prop_id, const GValue * value,
259 g_return_if_fail (GST_IS_DICETV (object));
261 filter = GST_DICETV (object);
265 filter->g_cube_bits = g_value_get_int (value);
266 gst_dicetv_create_map (filter);
273 gst_dicetv_get_property (GObject * object, guint prop_id, GValue * value,
278 g_return_if_fail (GST_IS_DICETV (object));
280 filter = GST_DICETV (object);
284 g_value_set_int (value, filter->g_cube_bits);
287 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
293 gst_dicetv_base_init (gpointer g_class)
295 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
297 gst_element_class_set_details (element_class, &gst_dicetv_details);
299 gst_element_class_add_pad_template (element_class,
300 gst_static_pad_template_get (&gst_dicetv_sink_template));
301 gst_element_class_add_pad_template (element_class,
302 gst_static_pad_template_get (&gst_dicetv_src_template));
306 gst_dicetv_class_init (gpointer klass, gpointer class_data)
308 GObjectClass *gobject_class;
309 GstElementClass *element_class;
310 GstBaseTransformClass *trans_class;
312 gobject_class = (GObjectClass *) klass;
313 element_class = (GstElementClass *) klass;
314 trans_class = (GstBaseTransformClass *) klass;
316 parent_class = g_type_class_peek_parent (klass);
318 gobject_class->set_property = gst_dicetv_set_property;
319 gobject_class->get_property = gst_dicetv_get_property;
321 g_object_class_install_property (gobject_class, ARG_CUBE_BITS,
322 g_param_spec_int ("square_bits", "Square Bits", "The size of the Squares",
323 MIN_CUBE_BITS, MAX_CUBE_BITS, DEFAULT_CUBE_BITS, G_PARAM_READWRITE));
325 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_dicetv_set_caps);
326 trans_class->get_unit_size = GST_DEBUG_FUNCPTR (gst_dicetv_get_unit_size);
327 trans_class->transform = GST_DEBUG_FUNCPTR (gst_dicetv_transform);
331 gst_dicetv_init (GTypeInstance * instance, gpointer g_class)
333 GstDiceTV *filter = GST_DICETV (instance);
335 filter->dicemap = NULL;
336 filter->g_cube_bits = DEFAULT_CUBE_BITS;
337 filter->g_cube_size = 0;
338 filter->g_map_height = 0;
339 filter->g_map_width = 0;
343 gst_dicetv_get_type (void)
345 static GType dicetv_type = 0;
348 static const GTypeInfo dicetv_info = {
349 sizeof (GstDiceTVClass),
350 gst_dicetv_base_init,
352 (GClassInitFunc) gst_dicetv_class_init,
357 (GInstanceInitFunc) gst_dicetv_init,
361 g_type_register_static (GST_TYPE_VIDEOFILTER, "GstDiceTV", &dicetv_info,