upload tizen1.0 source
[framework/multimedia/gst-plugins-good0.10.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 ! ffmpegcolorspace ! 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 #include <gst/video/video.h>
57 #include <gst/controller/gstcontroller.h>
58
59 #define DEFAULT_CUBE_BITS   4
60 #define MAX_CUBE_BITS       5
61 #define MIN_CUBE_BITS       0
62
63 typedef enum _dice_dir
64 {
65   DICE_UP = 0,
66   DICE_RIGHT = 1,
67   DICE_DOWN = 2,
68   DICE_LEFT = 3
69 } DiceDir;
70
71 GST_BOILERPLATE (GstDiceTV, gst_dicetv, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
72
73 static void gst_dicetv_create_map (GstDiceTV * filter);
74
75 static GstStaticPadTemplate gst_dicetv_src_template =
76     GST_STATIC_PAD_TEMPLATE ("src",
77     GST_PAD_SRC,
78     GST_PAD_ALWAYS,
79     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
80         GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
81     );
82
83 static GstStaticPadTemplate gst_dicetv_sink_template =
84     GST_STATIC_PAD_TEMPLATE ("sink",
85     GST_PAD_SINK,
86     GST_PAD_ALWAYS,
87     GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_xRGB ";"
88         GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_xBGR)
89     );
90
91 enum
92 {
93   PROP_0,
94   PROP_CUBE_BITS
95 };
96
97 static gboolean
98 gst_dicetv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
99     GstCaps * outcaps)
100 {
101   GstDiceTV *filter = GST_DICETV (btrans);
102   GstStructure *structure;
103   gboolean ret = FALSE;
104
105   structure = gst_caps_get_structure (incaps, 0);
106
107   GST_OBJECT_LOCK (filter);
108   if (gst_structure_get_int (structure, "width", &filter->width) &&
109       gst_structure_get_int (structure, "height", &filter->height)) {
110     g_free (filter->dicemap);
111     filter->dicemap = (guint8 *) g_malloc (filter->height * filter->width);
112     gst_dicetv_create_map (filter);
113     ret = TRUE;
114   }
115   GST_OBJECT_UNLOCK (filter);
116
117   return ret;
118 }
119
120 static GstFlowReturn
121 gst_dicetv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
122 {
123   GstDiceTV *filter = GST_DICETV (trans);
124   guint32 *src, *dest;
125   gint i, map_x, map_y, map_i, base, dx, dy, di;
126   gint video_width, g_cube_bits, g_cube_size;
127   gint g_map_height, g_map_width;
128   GstFlowReturn ret = GST_FLOW_OK;
129   GstClockTime timestamp, stream_time;
130   const guint8 *dicemap;
131
132   src = (guint32 *) GST_BUFFER_DATA (in);
133   dest = (guint32 *) GST_BUFFER_DATA (out);
134
135   timestamp = GST_BUFFER_TIMESTAMP (in);
136   stream_time =
137       gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
138
139   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
140       GST_TIME_ARGS (timestamp));
141
142   if (GST_CLOCK_TIME_IS_VALID (stream_time))
143     gst_object_sync_values (G_OBJECT (filter), stream_time);
144
145   GST_OBJECT_LOCK (filter);
146   video_width = filter->width;
147   g_cube_bits = filter->g_cube_bits;
148   g_cube_size = filter->g_cube_size;
149   g_map_height = filter->g_map_height;
150   g_map_width = filter->g_map_width;
151
152   dicemap = filter->dicemap;
153
154   map_i = 0;
155   for (map_y = 0; map_y < g_map_height; map_y++) {
156     for (map_x = 0; map_x < g_map_width; map_x++) {
157       base = (map_y << g_cube_bits) * video_width + (map_x << g_cube_bits);
158
159       switch (dicemap[map_i]) {
160         case DICE_UP:
161           for (dy = 0; dy < g_cube_size; dy++) {
162             i = base + dy * video_width;
163             for (dx = 0; dx < g_cube_size; dx++) {
164               dest[i] = src[i];
165               i++;
166             }
167           }
168           break;
169         case DICE_LEFT:
170           for (dy = 0; dy < g_cube_size; dy++) {
171             i = base + dy * video_width;
172
173             for (dx = 0; dx < g_cube_size; dx++) {
174               di = base + (dx * video_width) + (g_cube_size - dy - 1);
175               dest[di] = src[i];
176               i++;
177             }
178           }
179           break;
180         case DICE_DOWN:
181           for (dy = 0; dy < g_cube_size; dy++) {
182             di = base + dy * video_width;
183             i = base + (g_cube_size - dy - 1) * video_width + g_cube_size;
184             for (dx = 0; dx < g_cube_size; dx++) {
185               i--;
186               dest[di] = src[i];
187               di++;
188             }
189           }
190           break;
191         case DICE_RIGHT:
192           for (dy = 0; dy < g_cube_size; dy++) {
193             i = base + (dy * video_width);
194             for (dx = 0; dx < g_cube_size; dx++) {
195               di = base + dy + (g_cube_size - dx - 1) * video_width;
196               dest[di] = src[i];
197               i++;
198             }
199           }
200           break;
201         default:
202           g_assert_not_reached ();
203           break;
204       }
205       map_i++;
206     }
207   }
208   GST_OBJECT_UNLOCK (filter);
209
210   return ret;
211 }
212
213 static void
214 gst_dicetv_create_map (GstDiceTV * filter)
215 {
216   gint x, y, i;
217
218   if (filter->height <= 0 || filter->width <= 0)
219     return;
220
221   filter->g_map_height = filter->height >> filter->g_cube_bits;
222   filter->g_map_width = filter->width >> filter->g_cube_bits;
223   filter->g_cube_size = 1 << filter->g_cube_bits;
224
225   i = 0;
226
227   for (y = 0; y < filter->g_map_height; y++) {
228     for (x = 0; x < filter->g_map_width; x++) {
229       // dicemap[i] = ((i + y) & 0x3); /* Up, Down, Left or Right */
230       filter->dicemap[i] = (fastrand () >> 24) & 0x03;
231       i++;
232     }
233   }
234 }
235
236 static void
237 gst_dicetv_set_property (GObject * object, guint prop_id, const GValue * value,
238     GParamSpec * pspec)
239 {
240   GstDiceTV *filter = GST_DICETV (object);
241
242   switch (prop_id) {
243     case PROP_CUBE_BITS:
244       GST_OBJECT_LOCK (filter);
245       filter->g_cube_bits = g_value_get_int (value);
246       gst_dicetv_create_map (filter);
247       GST_OBJECT_UNLOCK (filter);
248       break;
249     default:
250       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251       break;
252   }
253 }
254
255 static void
256 gst_dicetv_get_property (GObject * object, guint prop_id, GValue * value,
257     GParamSpec * pspec)
258 {
259   GstDiceTV *filter = GST_DICETV (object);
260
261   switch (prop_id) {
262     case PROP_CUBE_BITS:
263       g_value_set_int (value, filter->g_cube_bits);
264       break;
265     default:
266       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
267       break;
268   }
269 }
270
271 static void
272 gst_dicetv_finalize (GObject * object)
273 {
274   GstDiceTV *filter = GST_DICETV (object);
275
276   g_free (filter->dicemap);
277   filter->dicemap = NULL;
278
279   G_OBJECT_CLASS (parent_class)->finalize (object);
280 }
281
282 static void
283 gst_dicetv_base_init (gpointer g_class)
284 {
285   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
286
287   gst_element_class_set_details_simple (element_class, "DiceTV effect",
288       "Filter/Effect/Video",
289       "'Dices' the screen up into many small squares",
290       "Wim Taymans <wim.taymans@chello.be>");
291
292   gst_element_class_add_pad_template (element_class,
293       gst_static_pad_template_get (&gst_dicetv_sink_template));
294   gst_element_class_add_pad_template (element_class,
295       gst_static_pad_template_get (&gst_dicetv_src_template));
296 }
297
298 static void
299 gst_dicetv_class_init (GstDiceTVClass * klass)
300 {
301   GObjectClass *gobject_class = (GObjectClass *) klass;
302   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
303
304   gobject_class->set_property = gst_dicetv_set_property;
305   gobject_class->get_property = gst_dicetv_get_property;
306   gobject_class->finalize = gst_dicetv_finalize;
307
308   g_object_class_install_property (gobject_class, PROP_CUBE_BITS,
309       g_param_spec_int ("square-bits", "Square Bits", "The size of the Squares",
310           MIN_CUBE_BITS, MAX_CUBE_BITS, DEFAULT_CUBE_BITS,
311           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
312
313   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_dicetv_set_caps);
314   trans_class->transform = GST_DEBUG_FUNCPTR (gst_dicetv_transform);
315 }
316
317 static void
318 gst_dicetv_init (GstDiceTV * filter, GstDiceTVClass * klass)
319 {
320   filter->dicemap = NULL;
321   filter->g_cube_bits = DEFAULT_CUBE_BITS;
322   filter->g_cube_size = 0;
323   filter->g_map_height = 0;
324   filter->g_map_width = 0;
325 }