1 /* GStreamer gdkpixbufoverlay test app
2 * Copyright (C) 2014 Tim-Philipp Müller <tim centricular com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 #include <gst/video/video.h>
27 #include <gdk-pixbuf/gdk-pixbuf.h>
31 #define VIDEO_WIDTH 720
32 #define VIDEO_HEIGHT 480
35 /* GdkPixbuf RGBA C-Source image dump from gdk-pixbuf-csource --raw,
36 * gzipped and then base64 encoded */
37 const gchar gzipped_pixdata_base64[] =
38 "H4sICPX/Z1QAA2xvZ28ucGl4AO2dsZHrNhCG+ewK2II64ClyrhmnTtSBh4kLUOLQAUuwEhSgFtiA"
39 "A7agwA2wBT5AXJ5w4P5LgKLEO97ezDf2SCAIAftjFwuQ7/c///ojy/77+8eP7Jcs+/WfLMv+t/zW"
40 "dV32pSmK3HK6sXZbFOXV9PZfWipLbWksHdHQZxVpZP+Ee7u62/d7rt0fivIqimJnOX+w/zhauq68"
41 "aWjevfdUR1h3vXq/KMqzufueFN1J1FE+stf8KfCzobZ3q/ePojyT3v8gDSyB09GFND7g/N014rpl"
42 "41xF+Wz0/i817nwFjepP+Rb0PnDwOUOehePyQq1eurlrSkXZOkVx7OblbGK43upf+zcqyldhOT22"
43 "5GvV9ynKXIriQDpKye242Ldcve2KskX6deaRWVeebnpdu32Koryc4t/iaKksmm9Vvg3W3neWk+Vs"
44 "qS2tpSOu9NmZtLG4f7R15paS7jXct1q7XxTl2ZDd157dp1A/6q9I+1Wg+QH1g8pmcb7M0szUHseV"
45 "/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uC8ZXO00zuZ020G83rs8Y/xU8gmwcaRPVKa+uk/LLh"
72 "8y1NUEbKfc+aww3eP/xS73xIBez5XbKPzwuW3H4h6Ec3j6E9ba6PkR9MGgvBNmPbLeXa58TfKRqM"
73 "brtJPJuTom/vGm4dwJ2lQXmsWe/cM4l7mFtBOCcjPR+B9iMGW4LnGWLHa4bdzNqbj6wj2QYEXYX+"
74 "JPWMKlcOvpdEaIcUu0blUA1eX7Bnbx7os02/N4iLRSMY7QuacWwSc24YjhXQgxQ/sT4s0QbQs2kO"
75 "dl1o+hiYs09p/8x/7kPaqw/3GZGfkPbmka9C5wiQ30RzJdp3R2cTh3MKozk6ta1bgfYGUzU42sMX"
76 "xsP1q4ttuLxfci7Ps18/RwfPI6f2x4QOG087/m8arRlN2vN9yP+G5yiTcpETbUBxCpo7kGZzof3D"
77 "OwGG56ca7/NwDkdrwc1qUNiXT4Hrx9hn1WLOj0n7wDF7Vcka9Gw39lyL9Gz51P78ebAv1G9BfWiv"
78 "A2kQ+mKhzbP2aEz8s8DsmWGDfbxjc/+OENDfib4b3uNUejmZqGd8g/5EY3k1CWt2cz9nONR380XM"
79 "+HPvHlnivU/ce19aA96FA/TsP2984q6jcpeg/f75kTyoxwfFiSW6ZkJLHDHvt8np93Hv3xliCLT2"
80 "OIEx3OTeILMf2EztCYKYNcYG2fMQivKdYbQUpY1s/M6ZTcbpivJMMv5Zpdhn7mdpV1GUO1l/NjTU"
81 "Usx7Z8Iz3ZuM0xXlFWT8s4CsDkmzJZPD0ThUUWaSye8THZ6RqOj/uf2L1f79T0XZCpn8vJJ0Pkb9"
82 "n6IsCOVoBp/HvS+moe83+T4dRVEUZRmyn2F9swl9yAAA";
84 static GdkPixbuf *logo_pixbuf;
86 static GMainLoop *main_loop;
90 create_overlay_pixbuf (void)
92 GZlibDecompressor *decompress;
93 GConverterResult decomp_res;
94 guchar *gzipped_pixdata, *pixdata;
95 gsize gzipped_size, bytes_read, pixdata_size;
98 gzipped_pixdata = g_base64_decode (gzipped_pixdata_base64, &gzipped_size);
99 g_assert (gzipped_pixdata != NULL);
101 pixdata = g_malloc (64 * 1024);
103 decompress = g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP);
104 decomp_res = g_converter_convert (G_CONVERTER (decompress),
105 gzipped_pixdata, gzipped_size, pixdata, 64 * 1024,
106 G_CONVERTER_INPUT_AT_END, &bytes_read, &pixdata_size, NULL);
107 g_assert (decomp_res == G_CONVERTER_FINISHED);
108 g_assert (bytes_read == gzipped_size);
109 g_free (gzipped_pixdata);
110 g_object_unref (decompress);
112 /* 0: Pixbuf magic (0x47646b50) */
113 g_assert (GST_READ_UINT32_BE (pixdata) == 0x47646b50);
115 pixbuf = gdk_pixbuf_new_from_inline (pixdata_size, pixdata, TRUE, NULL);
116 g_assert (pixbuf != NULL);
124 bus_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
126 GMainLoop *loop = user_data;
128 switch (GST_MESSAGE_TYPE (msg)) {
129 case GST_MESSAGE_ERROR:{
133 gst_message_parse_error (msg, &err, &dbg);
134 gst_object_default_error (msg->src, err, dbg);
137 g_main_loop_quit (loop);
146 #define SPEED_SCALE_FACTOR (VIDEO_FPS * 4)
148 /* nicked from videotestsrc's ball pattern renderer */
150 calculate_position (gint * x, gint * y, guint logo_w, guint logo_h, guint n)
152 guint r_x = logo_w / 2;
153 guint r_y = logo_h / 2;
154 guint w = VIDEO_WIDTH + logo_w;
155 guint h = VIDEO_HEIGHT + logo_h;
157 *x = r_x + (0.5 + 0.5 * sin (2 * G_PI * n / SPEED_SCALE_FACTOR))
159 *y = r_y + (0.5 + 0.5 * sin (2 * G_PI * sqrt (2) * n / SPEED_SCALE_FACTOR))
166 static GstPadProbeReturn
167 buffer_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
169 GstElement *overlay = GST_PAD_PARENT (pad);
172 w = gdk_pixbuf_get_width (logo_pixbuf);
173 h = gdk_pixbuf_get_height (logo_pixbuf);
175 calculate_position (&x, &y, w, h, ++count);
177 GST_LOG ("%3d, %3d", x, y);
179 g_object_set (overlay, "offset-x", x, "offset-y", y, NULL);
181 return GST_PAD_PROBE_OK;
185 main (int argc, char **argv)
187 GOptionEntry options[] = {
192 GstElement *src, *q, *capsfilter, *overlay, *sink;
193 GstElement *pipeline;
195 GstCaps *filter_caps;
197 ctx = g_option_context_new ("");
198 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
199 g_option_context_add_group (ctx, gst_init_get_option_group ());
200 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
201 g_print ("Error initializing: %s\n", err->message);
204 g_option_context_free (ctx);
206 logo_pixbuf = create_overlay_pixbuf ();
208 main_loop = g_main_loop_new (NULL, FALSE);
210 pipeline = gst_pipeline_new ("pipeline");
212 src = gst_element_factory_make ("videotestsrc", NULL);
213 gst_util_set_object_arg (G_OBJECT (src), "pattern", "white");
215 overlay = gst_element_factory_make ("gdkpixbufoverlay", NULL);
217 /* set positioning-mode to absolute so we can set negative positions */
218 g_object_set (overlay, "pixbuf", logo_pixbuf, "positioning-mode", 1, NULL);
220 sink_pad = gst_element_get_static_pad (overlay, "sink");
221 gst_pad_add_probe (sink_pad, GST_PAD_PROBE_TYPE_BUFFER, buffer_cb, NULL,
223 gst_object_unref (sink_pad);
225 q = gst_element_factory_make ("queue", NULL);
227 capsfilter = gst_element_factory_make ("capsfilter", NULL);
228 filter_caps = gst_caps_from_string ("video/x-raw, format = "
229 GST_VIDEO_OVERLAY_COMPOSITION_BLEND_FORMATS);
230 gst_caps_set_simple (filter_caps,
231 "width", G_TYPE_INT, VIDEO_WIDTH,
232 "height", G_TYPE_INT, VIDEO_HEIGHT,
233 "framerate", GST_TYPE_FRACTION, VIDEO_FPS, 1, NULL);
234 g_object_set (capsfilter, "caps", filter_caps, NULL);
235 gst_caps_unref (filter_caps);
237 sink = gst_element_factory_make ("ximagesink", NULL);
239 gst_bin_add_many (GST_BIN (pipeline), src, q, overlay, capsfilter, sink,
242 gst_element_link_many (src, q, overlay, capsfilter, sink, NULL);
246 gst_element_set_state (pipeline, GST_STATE_PLAYING);
248 gst_bus_add_watch (GST_ELEMENT_BUS (pipeline), bus_cb, main_loop);
250 g_main_loop_run (main_loop);
252 gst_element_set_state (pipeline, GST_STATE_NULL);
253 gst_object_unref (pipeline);
254 g_object_unref (logo_pixbuf);