2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
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.
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.
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.
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.
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.
30 * SECTION:element-dicetv
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.
40 * <title>Example launch line</title>
42 * gst-launch -v videotestsrc ! dicetv ! videoconvert ! autovideosink
43 * ]| This pipeline shows the effect of dicetv on a test stream.
54 #include "gsteffectv.h"
56 #define DEFAULT_CUBE_BITS 4
57 #define MAX_CUBE_BITS 5
58 #define MIN_CUBE_BITS 0
60 typedef enum _dice_dir
68 #define gst_dicetv_parent_class parent_class
69 G_DEFINE_TYPE (GstDiceTV, gst_dicetv, GST_TYPE_VIDEO_FILTER);
71 static void gst_dicetv_create_map (GstDiceTV * filter, GstVideoInfo * info);
73 static GstStaticPadTemplate gst_dicetv_src_template =
74 GST_STATIC_PAD_TEMPLATE ("src",
77 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR }"))
80 static GstStaticPadTemplate gst_dicetv_sink_template =
81 GST_STATIC_PAD_TEMPLATE ("sink",
84 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR }"))
94 gst_dicetv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
95 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
97 GstDiceTV *filter = GST_DICETV (vfilter);
99 g_free (filter->dicemap);
101 (guint8 *) g_malloc (GST_VIDEO_INFO_WIDTH (in_info) *
102 GST_VIDEO_INFO_WIDTH (in_info));
103 gst_dicetv_create_map (filter, in_info);
109 gst_dicetv_transform_frame (GstVideoFilter * vfilter, GstVideoFrame * in_frame,
110 GstVideoFrame * out_frame)
112 GstDiceTV *filter = GST_DICETV (vfilter);
114 gint i, map_x, map_y, map_i, base, dx, dy, di;
115 gint video_stride, g_cube_bits, g_cube_size;
116 gint g_map_height, g_map_width;
117 GstClockTime timestamp, stream_time;
118 const guint8 *dicemap;
120 timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
122 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (vfilter)->segment,
123 GST_FORMAT_TIME, timestamp);
125 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
126 GST_TIME_ARGS (timestamp));
128 if (GST_CLOCK_TIME_IS_VALID (stream_time))
129 gst_object_sync_values (GST_OBJECT (filter), stream_time);
131 src = (guint32 *) GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
132 dest = (guint32 *) GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
133 video_stride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
135 GST_OBJECT_LOCK (filter);
136 g_cube_bits = filter->g_cube_bits;
137 g_cube_size = filter->g_cube_size;
138 g_map_height = filter->g_map_height;
139 g_map_width = filter->g_map_width;
141 dicemap = filter->dicemap;
144 for (map_y = 0; map_y < g_map_height; map_y++) {
145 for (map_x = 0; map_x < g_map_width; map_x++) {
146 base = (map_y << g_cube_bits) * video_stride + (map_x << g_cube_bits);
148 switch (dicemap[map_i]) {
150 for (dy = 0; dy < g_cube_size; dy++) {
151 i = base + dy * video_stride;
152 for (dx = 0; dx < g_cube_size; dx++) {
159 for (dy = 0; dy < g_cube_size; dy++) {
160 i = base + dy * video_stride;
162 for (dx = 0; dx < g_cube_size; dx++) {
163 di = base + (dx * video_stride) + (g_cube_size - dy - 1);
170 for (dy = 0; dy < g_cube_size; dy++) {
171 di = base + dy * video_stride;
172 i = base + (g_cube_size - dy - 1) * video_stride + g_cube_size;
173 for (dx = 0; dx < g_cube_size; dx++) {
181 for (dy = 0; dy < g_cube_size; dy++) {
182 i = base + (dy * video_stride);
183 for (dx = 0; dx < g_cube_size; dx++) {
184 di = base + dy + (g_cube_size - dx - 1) * video_stride;
191 g_assert_not_reached ();
197 GST_OBJECT_UNLOCK (filter);
203 gst_dicetv_create_map (GstDiceTV * filter, GstVideoInfo * info)
208 width = GST_VIDEO_INFO_WIDTH (info);
209 height = GST_VIDEO_INFO_HEIGHT (info);
211 if (width <= 0 || height <= 0)
214 filter->g_map_height = height >> filter->g_cube_bits;
215 filter->g_map_width = width >> filter->g_cube_bits;
216 filter->g_cube_size = 1 << filter->g_cube_bits;
220 for (y = 0; y < filter->g_map_height; y++) {
221 for (x = 0; x < filter->g_map_width; x++) {
222 // dicemap[i] = ((i + y) & 0x3); /* Up, Down, Left or Right */
223 filter->dicemap[i] = (fastrand () >> 24) & 0x03;
230 gst_dicetv_set_property (GObject * object, guint prop_id, const GValue * value,
233 GstDiceTV *filter = GST_DICETV (object);
237 GST_OBJECT_LOCK (filter);
238 filter->g_cube_bits = g_value_get_int (value);
239 gst_dicetv_create_map (filter, &GST_VIDEO_FILTER (filter)->in_info);
240 GST_OBJECT_UNLOCK (filter);
243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249 gst_dicetv_get_property (GObject * object, guint prop_id, GValue * value,
252 GstDiceTV *filter = GST_DICETV (object);
256 g_value_set_int (value, filter->g_cube_bits);
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
265 gst_dicetv_finalize (GObject * object)
267 GstDiceTV *filter = GST_DICETV (object);
269 g_free (filter->dicemap);
270 filter->dicemap = NULL;
272 G_OBJECT_CLASS (parent_class)->finalize (object);
276 gst_dicetv_class_init (GstDiceTVClass * klass)
278 GObjectClass *gobject_class = (GObjectClass *) klass;
279 GstElementClass *gstelement_class = (GstElementClass *) klass;
280 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
282 gobject_class->set_property = gst_dicetv_set_property;
283 gobject_class->get_property = gst_dicetv_get_property;
284 gobject_class->finalize = gst_dicetv_finalize;
286 g_object_class_install_property (gobject_class, PROP_CUBE_BITS,
287 g_param_spec_int ("square-bits", "Square Bits", "The size of the Squares",
288 MIN_CUBE_BITS, MAX_CUBE_BITS, DEFAULT_CUBE_BITS,
289 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
291 gst_element_class_set_details_simple (gstelement_class, "DiceTV effect",
292 "Filter/Effect/Video",
293 "'Dices' the screen up into many small squares",
294 "Wim Taymans <wim.taymans@gmail.be>");
296 gst_element_class_add_pad_template (gstelement_class,
297 gst_static_pad_template_get (&gst_dicetv_sink_template));
298 gst_element_class_add_pad_template (gstelement_class,
299 gst_static_pad_template_get (&gst_dicetv_src_template));
301 vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_dicetv_set_info);
302 vfilter_class->transform_frame =
303 GST_DEBUG_FUNCPTR (gst_dicetv_transform_frame);
307 gst_dicetv_init (GstDiceTV * filter)
309 filter->dicemap = NULL;
310 filter->g_cube_bits = DEFAULT_CUBE_BITS;
311 filter->g_cube_size = 0;
312 filter->g_map_height = 0;
313 filter->g_map_width = 0;