controller: port to new controller location and api
[platform/upstream/gstreamer.git] / gst / effectv / gstdice.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  * dice.c: a 'dicing' effect
6  *  copyright (c) 2001 Sam Mertens.  This code is subject to the provisions of
7  *  the GNU Library Public License.
8  *
9  * I suppose this looks similar to PuzzleTV, but it's not. The screen is
10  * divided into small squares, each of which is rotated either 0, 90, 180 or
11  * 270 degrees.  The amount of rotation for each square is chosen at random.
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Library General Public
15  * License as published by the Free Software Foundation; either
16  * version 2 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Library General Public License for more details.
22  *
23  * You should have received a copy of the GNU Library General Public
24  * License along with this library; if not, write to the
25  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26  * Boston, MA 02111-1307, USA.
27  */
28
29 /**
30  * SECTION:element-dicetv
31  *
32  * DiceTV 'dices' the screen up into many small squares, each defaulting
33  * to a size of 16 pixels by 16 pixels.. Each square is rotated randomly
34  * in one of four directions: up (no change), down (180 degrees, or
35  * upside down), right (90 degrees clockwise), or left (90 degrees
36  * counterclockwise). The direction of each square normally remains
37  * consistent between each frame.
38  *
39  * <refsect2>
40  * <title>Example launch line</title>
41  * |[
42  * gst-launch -v videotestsrc ! dicetv ! videoconvert ! autovideosink
43  * ]| This pipeline shows the effect of dicetv on a test stream.
44  * </refsect2>
45  */
46
47 #ifdef HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50
51 #include <string.h>
52
53 #include "gstdice.h"
54 #include "gsteffectv.h"
55
56 #define DEFAULT_CUBE_BITS   4
57 #define MAX_CUBE_BITS       5
58 #define MIN_CUBE_BITS       0
59
60 typedef enum _dice_dir
61 {
62   DICE_UP = 0,
63   DICE_RIGHT = 1,
64   DICE_DOWN = 2,
65   DICE_LEFT = 3
66 } DiceDir;
67
68 #define gst_dicetv_parent_class parent_class
69 G_DEFINE_TYPE (GstDiceTV, gst_dicetv, GST_TYPE_VIDEO_FILTER);
70
71 static void gst_dicetv_create_map (GstDiceTV * filter);
72
73 static GstStaticPadTemplate gst_dicetv_src_template =
74 GST_STATIC_PAD_TEMPLATE ("src",
75     GST_PAD_SRC,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR }"))
78     );
79
80 static GstStaticPadTemplate gst_dicetv_sink_template =
81 GST_STATIC_PAD_TEMPLATE ("sink",
82     GST_PAD_SINK,
83     GST_PAD_ALWAYS,
84     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR }"))
85     );
86
87 enum
88 {
89   PROP_0,
90   PROP_CUBE_BITS
91 };
92
93 static gboolean
94 gst_dicetv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
95     GstCaps * outcaps)
96 {
97   GstDiceTV *filter = GST_DICETV (btrans);
98   GstVideoInfo info;
99
100   if (!gst_video_info_from_caps (&info, incaps))
101     goto invalid_caps;
102
103   g_free (filter->dicemap);
104   filter->dicemap =
105       (guint8 *) g_malloc (GST_VIDEO_INFO_WIDTH (&info) *
106       GST_VIDEO_INFO_WIDTH (&info));
107   gst_dicetv_create_map (filter);
108
109   filter->info = info;
110
111   return TRUE;
112
113   /* ERRORS */
114 invalid_caps:
115   {
116     GST_DEBUG_OBJECT (filter, "invalid caps");
117     return FALSE;
118   }
119 }
120
121 static GstFlowReturn
122 gst_dicetv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
123 {
124   GstDiceTV *filter = GST_DICETV (trans);
125   GstVideoFrame in_frame, out_frame;
126   guint32 *src, *dest;
127   gint i, map_x, map_y, map_i, base, dx, dy, di;
128   gint video_stride, g_cube_bits, g_cube_size;
129   gint g_map_height, g_map_width;
130   GstClockTime timestamp, stream_time;
131   const guint8 *dicemap;
132
133   timestamp = GST_BUFFER_TIMESTAMP (in);
134   stream_time =
135       gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
136
137   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
138       GST_TIME_ARGS (timestamp));
139
140   if (GST_CLOCK_TIME_IS_VALID (stream_time))
141     gst_object_sync_values (GST_OBJECT (filter), stream_time);
142
143   gst_video_frame_map (&in_frame, &filter->info, in, GST_MAP_READ);
144   gst_video_frame_map (&out_frame, &filter->info, out, GST_MAP_WRITE);
145
146   src = (guint32 *) GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
147   dest = (guint32 *) GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
148   video_stride = GST_VIDEO_FRAME_PLANE_STRIDE (&in_frame, 0);
149
150   GST_OBJECT_LOCK (filter);
151   g_cube_bits = filter->g_cube_bits;
152   g_cube_size = filter->g_cube_size;
153   g_map_height = filter->g_map_height;
154   g_map_width = filter->g_map_width;
155
156   dicemap = filter->dicemap;
157
158   map_i = 0;
159   for (map_y = 0; map_y < g_map_height; map_y++) {
160     for (map_x = 0; map_x < g_map_width; map_x++) {
161       base = (map_y << g_cube_bits) * video_stride + (map_x << g_cube_bits);
162
163       switch (dicemap[map_i]) {
164         case DICE_UP:
165           for (dy = 0; dy < g_cube_size; dy++) {
166             i = base + dy * video_stride;
167             for (dx = 0; dx < g_cube_size; dx++) {
168               dest[i] = src[i];
169               i++;
170             }
171           }
172           break;
173         case DICE_LEFT:
174           for (dy = 0; dy < g_cube_size; dy++) {
175             i = base + dy * video_stride;
176
177             for (dx = 0; dx < g_cube_size; dx++) {
178               di = base + (dx * video_stride) + (g_cube_size - dy - 1);
179               dest[di] = src[i];
180               i++;
181             }
182           }
183           break;
184         case DICE_DOWN:
185           for (dy = 0; dy < g_cube_size; dy++) {
186             di = base + dy * video_stride;
187             i = base + (g_cube_size - dy - 1) * video_stride + g_cube_size;
188             for (dx = 0; dx < g_cube_size; dx++) {
189               i--;
190               dest[di] = src[i];
191               di++;
192             }
193           }
194           break;
195         case DICE_RIGHT:
196           for (dy = 0; dy < g_cube_size; dy++) {
197             i = base + (dy * video_stride);
198             for (dx = 0; dx < g_cube_size; dx++) {
199               di = base + dy + (g_cube_size - dx - 1) * video_stride;
200               dest[di] = src[i];
201               i++;
202             }
203           }
204           break;
205         default:
206           g_assert_not_reached ();
207           break;
208       }
209       map_i++;
210     }
211   }
212   GST_OBJECT_UNLOCK (filter);
213
214   gst_video_frame_unmap (&in_frame);
215   gst_video_frame_unmap (&out_frame);
216
217   return GST_FLOW_OK;
218 }
219
220 static void
221 gst_dicetv_create_map (GstDiceTV * filter)
222 {
223   gint x, y, i;
224   gint width, height;
225
226   width = GST_VIDEO_INFO_WIDTH (&filter->info);
227   height = GST_VIDEO_INFO_HEIGHT (&filter->info);
228
229   if (width <= 0 || height <= 0)
230     return;
231
232   filter->g_map_height = height >> filter->g_cube_bits;
233   filter->g_map_width = width >> filter->g_cube_bits;
234   filter->g_cube_size = 1 << filter->g_cube_bits;
235
236   i = 0;
237
238   for (y = 0; y < filter->g_map_height; y++) {
239     for (x = 0; x < filter->g_map_width; x++) {
240       // dicemap[i] = ((i + y) & 0x3); /* Up, Down, Left or Right */
241       filter->dicemap[i] = (fastrand () >> 24) & 0x03;
242       i++;
243     }
244   }
245 }
246
247 static void
248 gst_dicetv_set_property (GObject * object, guint prop_id, const GValue * value,
249     GParamSpec * pspec)
250 {
251   GstDiceTV *filter = GST_DICETV (object);
252
253   switch (prop_id) {
254     case PROP_CUBE_BITS:
255       GST_OBJECT_LOCK (filter);
256       filter->g_cube_bits = g_value_get_int (value);
257       gst_dicetv_create_map (filter);
258       GST_OBJECT_UNLOCK (filter);
259       break;
260     default:
261       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262       break;
263   }
264 }
265
266 static void
267 gst_dicetv_get_property (GObject * object, guint prop_id, GValue * value,
268     GParamSpec * pspec)
269 {
270   GstDiceTV *filter = GST_DICETV (object);
271
272   switch (prop_id) {
273     case PROP_CUBE_BITS:
274       g_value_set_int (value, filter->g_cube_bits);
275       break;
276     default:
277       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278       break;
279   }
280 }
281
282 static void
283 gst_dicetv_finalize (GObject * object)
284 {
285   GstDiceTV *filter = GST_DICETV (object);
286
287   g_free (filter->dicemap);
288   filter->dicemap = NULL;
289
290   G_OBJECT_CLASS (parent_class)->finalize (object);
291 }
292
293 static void
294 gst_dicetv_class_init (GstDiceTVClass * klass)
295 {
296   GObjectClass *gobject_class = (GObjectClass *) klass;
297   GstElementClass *gstelement_class = (GstElementClass *) klass;
298   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
299
300   gobject_class->set_property = gst_dicetv_set_property;
301   gobject_class->get_property = gst_dicetv_get_property;
302   gobject_class->finalize = gst_dicetv_finalize;
303
304   g_object_class_install_property (gobject_class, PROP_CUBE_BITS,
305       g_param_spec_int ("square-bits", "Square Bits", "The size of the Squares",
306           MIN_CUBE_BITS, MAX_CUBE_BITS, DEFAULT_CUBE_BITS,
307           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
308
309   gst_element_class_set_details_simple (gstelement_class, "DiceTV effect",
310       "Filter/Effect/Video",
311       "'Dices' the screen up into many small squares",
312       "Wim Taymans <wim.taymans@gmail.be>");
313
314   gst_element_class_add_pad_template (gstelement_class,
315       gst_static_pad_template_get (&gst_dicetv_sink_template));
316   gst_element_class_add_pad_template (gstelement_class,
317       gst_static_pad_template_get (&gst_dicetv_src_template));
318
319   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_dicetv_set_caps);
320   trans_class->transform = GST_DEBUG_FUNCPTR (gst_dicetv_transform);
321 }
322
323 static void
324 gst_dicetv_init (GstDiceTV * filter)
325 {
326   filter->dicemap = NULL;
327   filter->g_cube_bits = DEFAULT_CUBE_BITS;
328   filter->g_cube_size = 0;
329   filter->g_map_height = 0;
330   filter->g_map_width = 0;
331 }