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