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., 51 Franklin St, Fifth Floor,
26 * Boston, MA 02110-1301, USA.
27 */
28
29 /**
30 * SECTION:element-dicetv
31 * @title: dicetv
32 *
33 * DiceTV 'dices' the screen up into many small squares, each defaulting
34 * to a size of 16 pixels by 16 pixels.. Each square is rotated randomly
35 * in one of four directions: up (no change), down (180 degrees, or
36 * upside down), right (90 degrees clockwise), or left (90 degrees
37 * counterclockwise). The direction of each square normally remains
38 * consistent between each frame.
39 *
40 * ## Example launch line
41 * |[
42 * gst-launch-1.0 -v videotestsrc ! dicetv ! videoconvert ! autovideosink
43 * ]| This pipeline shows the effect of dicetv on a test stream.
44 *
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 GST_ELEMENT_REGISTER_DEFINE (dicetv, "dicetv", GST_RANK_NONE, GST_TYPE_DICETV);
71
72 static void gst_dicetv_create_map (GstDiceTV * filter, GstVideoInfo * info);
73
74 static GstStaticPadTemplate gst_dicetv_src_template =
75 GST_STATIC_PAD_TEMPLATE ("src",
76 GST_PAD_SRC,
77 GST_PAD_ALWAYS,
78 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR }"))
79 );
80
81 static GstStaticPadTemplate gst_dicetv_sink_template =
82 GST_STATIC_PAD_TEMPLATE ("sink",
83 GST_PAD_SINK,
84 GST_PAD_ALWAYS,
85 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR }"))
86 );
87
88 enum
89 {
90 PROP_0,
91 PROP_CUBE_BITS
92 };
93
94 static gboolean
gst_dicetv_set_info(GstVideoFilter * vfilter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)95 gst_dicetv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
96 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
97 {
98 GstDiceTV *filter = GST_DICETV (vfilter);
99
100 g_free (filter->dicemap);
101 filter->dicemap =
102 (guint8 *) g_malloc (GST_VIDEO_INFO_WIDTH (in_info) *
103 GST_VIDEO_INFO_WIDTH (in_info));
104 gst_dicetv_create_map (filter, in_info);
105
106 return TRUE;
107 }
108
109 static GstFlowReturn
gst_dicetv_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)110 gst_dicetv_transform_frame (GstVideoFilter * vfilter, GstVideoFrame * in_frame,
111 GstVideoFrame * out_frame)
112 {
113 GstDiceTV *filter = GST_DICETV (vfilter);
114 guint32 *src, *dest;
115 gint i, map_x, map_y, map_i, base, dx, dy, di;
116 gint video_stride, g_cube_bits, g_cube_size;
117 gint g_map_height, g_map_width;
118 GstClockTime timestamp, stream_time;
119 const guint8 *dicemap;
120
121 timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
122 stream_time =
123 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (vfilter)->segment,
124 GST_FORMAT_TIME, timestamp);
125
126 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
127 GST_TIME_ARGS (timestamp));
128
129 if (GST_CLOCK_TIME_IS_VALID (stream_time))
130 gst_object_sync_values (GST_OBJECT (filter), stream_time);
131
132 src = (guint32 *) GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
133 dest = (guint32 *) GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
134 video_stride = GST_VIDEO_FRAME_PLANE_STRIDE (in_frame, 0);
135
136 GST_OBJECT_LOCK (filter);
137 g_cube_bits = filter->g_cube_bits;
138 g_cube_size = filter->g_cube_size;
139 g_map_height = filter->g_map_height;
140 g_map_width = filter->g_map_width;
141
142 dicemap = filter->dicemap;
143 video_stride /= 4;
144
145 map_i = 0;
146 for (map_y = 0; map_y < g_map_height; map_y++) {
147 for (map_x = 0; map_x < g_map_width; map_x++) {
148 base = (map_y << g_cube_bits) * video_stride + (map_x << g_cube_bits);
149
150 switch (dicemap[map_i]) {
151 case DICE_UP:
152 for (dy = 0; dy < g_cube_size; dy++) {
153 i = base + dy * video_stride;
154 for (dx = 0; dx < g_cube_size; dx++) {
155 dest[i] = src[i];
156 i++;
157 }
158 }
159 break;
160 case DICE_LEFT:
161 for (dy = 0; dy < g_cube_size; dy++) {
162 i = base + dy * video_stride;
163
164 for (dx = 0; dx < g_cube_size; dx++) {
165 di = base + (dx * video_stride) + (g_cube_size - dy - 1);
166 dest[di] = src[i];
167 i++;
168 }
169 }
170 break;
171 case DICE_DOWN:
172 for (dy = 0; dy < g_cube_size; dy++) {
173 di = base + dy * video_stride;
174 i = base + (g_cube_size - dy - 1) * video_stride + g_cube_size;
175 for (dx = 0; dx < g_cube_size; dx++) {
176 i--;
177 dest[di] = src[i];
178 di++;
179 }
180 }
181 break;
182 case DICE_RIGHT:
183 for (dy = 0; dy < g_cube_size; dy++) {
184 i = base + (dy * video_stride);
185 for (dx = 0; dx < g_cube_size; dx++) {
186 di = base + dy + (g_cube_size - dx - 1) * video_stride;
187 dest[di] = src[i];
188 i++;
189 }
190 }
191 break;
192 default:
193 g_assert_not_reached ();
194 break;
195 }
196 map_i++;
197 }
198 }
199 GST_OBJECT_UNLOCK (filter);
200
201 return GST_FLOW_OK;
202 }
203
204 static void
gst_dicetv_create_map(GstDiceTV * filter,GstVideoInfo * info)205 gst_dicetv_create_map (GstDiceTV * filter, GstVideoInfo * info)
206 {
207 gint x, y, i;
208 gint width, height;
209
210 width = GST_VIDEO_INFO_WIDTH (info);
211 height = GST_VIDEO_INFO_HEIGHT (info);
212
213 if (width <= 0 || height <= 0)
214 return;
215
216 filter->g_map_height = height >> filter->g_cube_bits;
217 filter->g_map_width = width >> filter->g_cube_bits;
218 filter->g_cube_size = 1 << filter->g_cube_bits;
219
220 i = 0;
221
222 for (y = 0; y < filter->g_map_height; y++) {
223 for (x = 0; x < filter->g_map_width; x++) {
224 // dicemap[i] = ((i + y) & 0x3); /* Up, Down, Left or Right */
225 filter->dicemap[i] = (fastrand () >> 24) & 0x03;
226 i++;
227 }
228 }
229 }
230
231 static void
gst_dicetv_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)232 gst_dicetv_set_property (GObject * object, guint prop_id, const GValue * value,
233 GParamSpec * pspec)
234 {
235 GstDiceTV *filter = GST_DICETV (object);
236
237 switch (prop_id) {
238 case PROP_CUBE_BITS:
239 GST_OBJECT_LOCK (filter);
240 filter->g_cube_bits = g_value_get_int (value);
241 gst_dicetv_create_map (filter, &GST_VIDEO_FILTER (filter)->in_info);
242 GST_OBJECT_UNLOCK (filter);
243 break;
244 default:
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246 break;
247 }
248 }
249
250 static void
gst_dicetv_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)251 gst_dicetv_get_property (GObject * object, guint prop_id, GValue * value,
252 GParamSpec * pspec)
253 {
254 GstDiceTV *filter = GST_DICETV (object);
255
256 switch (prop_id) {
257 case PROP_CUBE_BITS:
258 g_value_set_int (value, filter->g_cube_bits);
259 break;
260 default:
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262 break;
263 }
264 }
265
266 static void
gst_dicetv_finalize(GObject * object)267 gst_dicetv_finalize (GObject * object)
268 {
269 GstDiceTV *filter = GST_DICETV (object);
270
271 g_free (filter->dicemap);
272 filter->dicemap = NULL;
273
274 G_OBJECT_CLASS (parent_class)->finalize (object);
275 }
276
277 static void
gst_dicetv_class_init(GstDiceTVClass * klass)278 gst_dicetv_class_init (GstDiceTVClass * klass)
279 {
280 GObjectClass *gobject_class = (GObjectClass *) klass;
281 GstElementClass *gstelement_class = (GstElementClass *) klass;
282 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
283
284 gobject_class->set_property = gst_dicetv_set_property;
285 gobject_class->get_property = gst_dicetv_get_property;
286 gobject_class->finalize = gst_dicetv_finalize;
287
288 g_object_class_install_property (gobject_class, PROP_CUBE_BITS,
289 g_param_spec_int ("square-bits", "Square Bits", "The size of the Squares",
290 MIN_CUBE_BITS, MAX_CUBE_BITS, DEFAULT_CUBE_BITS,
291 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
292
293 gst_element_class_set_static_metadata (gstelement_class, "DiceTV effect",
294 "Filter/Effect/Video",
295 "'Dices' the screen up into many small squares",
296 "Wim Taymans <wim.taymans@gmail.be>");
297
298 gst_element_class_add_static_pad_template (gstelement_class,
299 &gst_dicetv_sink_template);
300 gst_element_class_add_static_pad_template (gstelement_class,
301 &gst_dicetv_src_template);
302
303 vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_dicetv_set_info);
304 vfilter_class->transform_frame =
305 GST_DEBUG_FUNCPTR (gst_dicetv_transform_frame);
306 }
307
308 static void
gst_dicetv_init(GstDiceTV * filter)309 gst_dicetv_init (GstDiceTV * filter)
310 {
311 filter->dicemap = NULL;
312 filter->g_cube_bits = DEFAULT_CUBE_BITS;
313 filter->g_cube_size = 0;
314 filter->g_map_height = 0;
315 filter->g_map_width = 0;
316 }
317