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 <gst/video/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(klass) \
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 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>");
79 static GstStaticPadTemplate gst_dicetv_src_template =
80 GST_STATIC_PAD_TEMPLATE ("src",
83 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
84 GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
87 static GstStaticPadTemplate gst_dicetv_sink_template =
88 GST_STATIC_PAD_TEMPLATE ("sink",
91 GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
92 GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
95 static GstVideoFilterClass *parent_class = NULL;
104 gst_dicetv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
107 GstDiceTV *filter = GST_DICETV (btrans);
108 GstStructure *structure;
109 gboolean ret = FALSE;
111 structure = gst_caps_get_structure (incaps, 0);
113 if (gst_structure_get_int (structure, "width", &filter->width) &&
114 gst_structure_get_int (structure, "height", &filter->height)) {
115 g_free (filter->dicemap);
117 (gchar *) g_malloc (filter->height * filter->width * sizeof (char));
118 gst_dicetv_create_map (filter);
126 gst_dicetv_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
130 GstStructure *structure;
131 gboolean ret = FALSE;
134 filter = GST_DICETV (btrans);
136 structure = gst_caps_get_structure (caps, 0);
138 if (gst_structure_get_int (structure, "width", &width) &&
139 gst_structure_get_int (structure, "height", &height)) {
140 *size = width * height * 32 / 8;
142 GST_DEBUG_OBJECT (filter, "our frame size is %d bytes (%dx%d)", *size,
152 static unsigned int fastrand_val;
154 return (fastrand_val = fastrand_val * 1103515245 + 12345);
158 gst_dicetv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
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;
166 filter = GST_DICETV (trans);
167 src = (guint32 *) GST_BUFFER_DATA (in);
168 dest = (guint32 *) GST_BUFFER_DATA (out);
170 gst_buffer_copy_metadata (out, in, GST_BUFFER_COPY_TIMESTAMPS);
172 video_width = filter->width;
173 g_cube_bits = filter->g_cube_bits;
174 g_cube_size = filter->g_cube_size;
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);
181 switch (filter->dicemap[map_i]) {
183 for (dy = 0; dy < g_cube_size; dy++) {
184 i = base + dy * video_width;
185 for (dx = 0; dx < g_cube_size; dx++) {
192 for (dy = 0; dy < g_cube_size; dy++) {
193 i = base + dy * video_width;
195 for (dx = 0; dx < g_cube_size; dx++) {
196 di = base + (dx * video_width) + (g_cube_size - dy - 1);
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++) {
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;
224 g_assert_not_reached ();
235 gst_dicetv_create_map (GstDiceTV * filter)
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;
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;
255 gst_dicetv_set_property (GObject * object, guint prop_id, const GValue * value,
260 g_return_if_fail (GST_IS_DICETV (object));
262 filter = GST_DICETV (object);
266 filter->g_cube_bits = g_value_get_int (value);
267 gst_dicetv_create_map (filter);
274 gst_dicetv_get_property (GObject * object, guint prop_id, GValue * value,
279 g_return_if_fail (GST_IS_DICETV (object));
281 filter = GST_DICETV (object);
285 g_value_set_int (value, filter->g_cube_bits);
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
294 gst_dicetv_base_init (gpointer g_class)
296 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
298 gst_element_class_set_details (element_class, &gst_dicetv_details);
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));
307 gst_dicetv_class_init (gpointer klass, gpointer class_data)
309 GObjectClass *gobject_class;
310 GstBaseTransformClass *trans_class;
312 gobject_class = (GObjectClass *) klass;
313 trans_class = (GstBaseTransformClass *) klass;
315 parent_class = g_type_class_peek_parent (klass);
317 gobject_class->set_property = gst_dicetv_set_property;
318 gobject_class->get_property = gst_dicetv_get_property;
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));
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);
330 gst_dicetv_init (GTypeInstance * instance, gpointer g_class)
332 GstDiceTV *filter = GST_DICETV (instance);
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;
342 gst_dicetv_get_type (void)
344 static GType dicetv_type = 0;
347 static const GTypeInfo dicetv_info = {
348 sizeof (GstDiceTVClass),
349 gst_dicetv_base_init,
351 (GClassInitFunc) gst_dicetv_class_init,
356 (GInstanceInitFunc) gst_dicetv_init,
360 g_type_register_static (GST_TYPE_VIDEO_FILTER, "GstDiceTV",