gstdice.c 9.72 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1
2
3
4
5
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * dice.c: a 'dicing' effect
 *  copyright (c) 2001 Sam Mertens.  This code is subject to the provisions of
6
 *  the GNU Library Public License.
Wim Taymans's avatar
Wim Taymans committed
7
8
9
10
11
12
 *
 * I suppose this looks similar to PuzzleTV, but it's not. The screen is
 * divided into small squares, each of which is rotated either 0, 90, 180 or
 * 270 degrees.  The amount of rotation for each square is chosen at random.
 */

13
14
15
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Wim Taymans's avatar
Wim Taymans committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <string.h>
#include <gst/gst.h>
#include "gsteffectv.h"

#define GST_TYPE_DICETV \
  (gst_dicetv_get_type())
#define GST_DICETV(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_DICETV,GstDiceTV))
#define GST_DICETV_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstDiceTV))
#define GST_IS_DICETV(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_DICETV))
#define GST_IS_DICETV_CLASS(obj) \
  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_DICETV))

typedef struct _GstDiceTV GstDiceTV;
typedef struct _GstDiceTVClass GstDiceTVClass;

#define DEFAULT_CUBE_BITS   4
#define MAX_CUBE_BITS       5
#define MIN_CUBE_BITS       0

typedef enum _dice_dir 
{
  DICE_UP 	= 0,
  DICE_RIGHT 	= 1,
  DICE_DOWN 	= 2,
  DICE_LEFT 	= 3
} DiceDir;

struct _GstDiceTV
{
  GstElement element;

  GstPad *sinkpad, *srcpad;

  gint width, height;
  gchar* dicemap;

  gint g_cube_bits;
  gint g_cube_size;
  gint g_map_height;
  gint g_map_width;
};

struct _GstDiceTVClass
{
  GstElementClass parent_class;

Wim Taymans's avatar
Wim Taymans committed
65
  void (*reset) (GstElement *element);
Wim Taymans's avatar
Wim Taymans committed
66
67
};

68
/* elementfactory information */
69
static GstElementDetails gst_dicetv_details = GST_ELEMENT_DETAILS (
Wim Taymans's avatar
Wim Taymans committed
70
  "DiceTV",
71
  "Filter/Effect/Video",
Wim Taymans's avatar
Wim Taymans committed
72
  "'Dices' the screen up into many small squares",
73
74
  "Wim Taymans <wim.taymans@chello.be>"
);
Wim Taymans's avatar
Wim Taymans committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90


/* Filter signals and args */
enum
{
  /* FILL ME */
  RESET_SIGNAL,
  LAST_SIGNAL
};

enum
{
  ARG_0,
  ARG_CUBE_BITS,
};

91
static void     gst_dicetv_base_init		(gpointer g_class);   
Wim Taymans's avatar
Wim Taymans committed
92
93
94
95
96
97
98
99
100
101
102
static void 	gst_dicetv_class_init 		(GstDiceTVClass * klass);
static void 	gst_dicetv_init 		(GstDiceTV * filter);

static void 	gst_dicetv_reset_handler 	(GstElement *elem);
static void 	gst_dicetv_create_map 		(GstDiceTV *filter);

static void 	gst_dicetv_set_property 	(GObject * object, guint prop_id,
					  	 const GValue * value, GParamSpec * pspec);
static void 	gst_dicetv_get_property 	(GObject * object, guint prop_id,
					  	 GValue * value, GParamSpec * pspec);

103
static void 	gst_dicetv_chain 		(GstPad * pad, GstData *_data);
Wim Taymans's avatar
Wim Taymans committed
104
105
106
107
108
109
110
111
112
113

static GstElementClass *parent_class = NULL;
static guint gst_dicetv_signals[LAST_SIGNAL] = { 0 };

GType gst_dicetv_get_type (void)
{
  static GType dicetv_type = 0;

  if (!dicetv_type) {
    static const GTypeInfo dicetv_info = {
114
115
      sizeof (GstDiceTVClass), 
      gst_dicetv_base_init,
Wim Taymans's avatar
Wim Taymans committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
      NULL,
      (GClassInitFunc) gst_dicetv_class_init,
      NULL,
      NULL,
      sizeof (GstDiceTV),
      0,
      (GInstanceInitFunc) gst_dicetv_init,
    };

    dicetv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstDiceTV", &dicetv_info, 0);
  }
  return dicetv_type;
}

130
131
132
133
134
135
136
137
138
139
140
static void
gst_dicetv_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_add_pad_template (element_class, gst_effectv_src_factory ());
  gst_element_class_add_pad_template (element_class, gst_effectv_sink_factory ());
 
  gst_element_class_set_details (element_class, &gst_dicetv_details);
}

Wim Taymans's avatar
Wim Taymans committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
static void
gst_dicetv_class_init (GstDiceTVClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;

  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);

  gst_dicetv_signals[RESET_SIGNAL] =
    g_signal_new ("reset",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GstDiceTVClass, reset),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

  klass->reset = gst_dicetv_reset_handler;
	
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CUBE_BITS,
    g_param_spec_int ("square_bits","Square Bits","The size of the Squares",
                      MIN_CUBE_BITS, MAX_CUBE_BITS, DEFAULT_CUBE_BITS, G_PARAM_READWRITE));

  gobject_class->set_property = gst_dicetv_set_property;
  gobject_class->get_property = gst_dicetv_get_property;
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
171
static GstPadLinkReturn
Wim Taymans's avatar
Wim Taymans committed
172
173
174
175
176
177
178
gst_dicetv_sinkconnect (GstPad * pad, GstCaps * caps)
{
  GstDiceTV *filter;

  filter = GST_DICETV (gst_pad_get_parent (pad));

  if (!GST_CAPS_IS_FIXED (caps))
179
    return GST_PAD_LINK_DELAYED;
Wim Taymans's avatar
Wim Taymans committed
180
181
182
183
184
185
186
187

  gst_caps_get_int (caps, "width", &filter->width);
  gst_caps_get_int (caps, "height", &filter->height);

  g_free (filter->dicemap);
  filter->dicemap = (gchar *) g_malloc (filter->height * filter->width * sizeof(char));
  gst_dicetv_create_map (filter);

188
  return gst_pad_try_set_caps (filter->srcpad, gst_caps_ref (caps));
Wim Taymans's avatar
Wim Taymans committed
189
190
191
192
193
194
195
}

static void
gst_dicetv_init (GstDiceTV * filter)
{
  filter->sinkpad = gst_pad_new_from_template (gst_effectv_sink_factory (), "sink");
  gst_pad_set_chain_function (filter->sinkpad, gst_dicetv_chain);
196
  gst_pad_set_link_function (filter->sinkpad, gst_dicetv_sinkconnect);
Wim Taymans's avatar
Wim Taymans committed
197
198
199
200
201
202
203
204
205
206
207
208
209
  gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);

  filter->srcpad = gst_pad_new_from_template (gst_effectv_src_factory (), "src");
  gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);

  filter->dicemap = NULL;
  filter->g_cube_bits = DEFAULT_CUBE_BITS;
  filter->g_cube_size = 0;
  filter->g_map_height = 0;
  filter->g_map_width = 0;
}

static void
Wim Taymans's avatar
Wim Taymans committed
210
gst_dicetv_reset_handler (GstElement *element)
Wim Taymans's avatar
Wim Taymans committed
211
{
Wim Taymans's avatar
Wim Taymans committed
212
  GstDiceTV *filter = GST_DICETV (element);
Wim Taymans's avatar
Wim Taymans committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313

  gst_dicetv_create_map (filter);
}


static unsigned int
fastrand (void)
{   
  static unsigned int fastrand_val;

  return (fastrand_val = fastrand_val * 1103515245 + 12345);
}

static void 
gst_dicetv_draw (GstDiceTV *filter, guint32 *src, guint32 *dest)
{
  gint i;
  gint map_x, map_y, map_i;
  gint base;
  gint dx, dy, di;
  gint video_width = filter->width;
  gint g_cube_bits = filter->g_cube_bits;
  gint g_cube_size = filter->g_cube_size;
    
  map_i = 0;
  for (map_y = 0; map_y < filter->g_map_height; map_y++) {
    for (map_x = 0; map_x < filter->g_map_width; map_x++) {
       base = (map_y << g_cube_bits) * video_width + (map_x << g_cube_bits);

       switch (filter->dicemap[map_i]) {
         case DICE_UP:
           for (dy = 0; dy < g_cube_size; dy++) {
             i = base + dy * video_width;
             for (dx = 0; dx < g_cube_size; dx++) {
               dest[i] = src[i];
               i++;
             }
           }
           break;
         case DICE_LEFT:
           for (dy = 0; dy < g_cube_size; dy++) {
             i = base + dy * video_width;

             for (dx = 0; dx < g_cube_size; dx++) {
               di = base + (dx * video_width) + (g_cube_size - dy - 1);
               dest[di] = src[i];
               i++;
             }
           }
           break;
         case DICE_DOWN:
           for (dy = 0; dy < g_cube_size; dy++) {
             di = base + dy * video_width;
             i = base + (g_cube_size - dy - 1) * video_width + g_cube_size;
             for (dx = 0; dx < g_cube_size; dx++) {
               i--;
               dest[di] = src[i];
               di++;
             }
           }
           break;
         case DICE_RIGHT:
           for (dy = 0; dy < g_cube_size; dy++) {
             i = base + (dy * video_width);
             for (dx = 0; dx < g_cube_size; dx++) {
               di = base + dy + (g_cube_size - dx - 1) * video_width;
               dest[di] = src[i];
               i++;
             }
           }
           break;
         default:
	   g_assert_not_reached ();
           break;
      }
      map_i++;
    }
  }
}

static void 
gst_dicetv_create_map (GstDiceTV *filter)
{
  gint x, y, i;
    
  filter->g_map_height = filter->height >> filter->g_cube_bits;
  filter->g_map_width = filter->width >> filter->g_cube_bits;
  filter->g_cube_size = 1 << filter->g_cube_bits;

  i = 0;

  for (y = 0; y < filter->g_map_height; y++) {
    for(x = 0; x < filter->g_map_width; x++) {
      // dicemap[i] = ((i + y) & 0x3); /* Up, Down, Left or Right */
      filter->dicemap[i] = (fastrand() >> 24) & 0x03;
      i++;
    }
  }
}

static void
314
gst_dicetv_chain (GstPad * pad, GstData *_data)
Wim Taymans's avatar
Wim Taymans committed
315
{
316
  GstBuffer *buf = GST_BUFFER (_data);
Wim Taymans's avatar
Wim Taymans committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  GstDiceTV *filter;
  guint32 *src, *dest;
  GstBuffer *outbuf;

  filter = GST_DICETV (gst_pad_get_parent (pad));

  src = (guint32 *) GST_BUFFER_DATA (buf);

  outbuf = gst_buffer_new ();
  GST_BUFFER_SIZE (outbuf) = (filter->width * filter->height * sizeof(guint32));
  dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
  GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);

  gst_dicetv_draw (filter, src, dest);
  
  gst_buffer_unref (buf);

334
  gst_pad_push (filter->srcpad, GST_DATA (outbuf));
Wim Taymans's avatar
Wim Taymans committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
}

static void
gst_dicetv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
{
  GstDiceTV *filter;

  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (GST_IS_DICETV (object));

  filter = GST_DICETV (object);

  switch (prop_id) {
    case ARG_CUBE_BITS:
      filter->g_cube_bits = g_value_get_int (value);
      gst_dicetv_create_map (filter);
    default:
      break;
  }
}

static void
gst_dicetv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
{
  GstDiceTV *filter;

  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (GST_IS_DICETV (object));

  filter = GST_DICETV (object);

  switch (prop_id) {
    case ARG_CUBE_BITS:
      g_value_set_int (value, filter->g_cube_bits);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}