1 /* GStreamer video overlay composition blending test app
2 * Copyright (C) 2014 Tim-Philipp Müller <tim centricular com>
3
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.
8 *
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.
13 *
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.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gio/gio.h>
25 #include <gst/gst.h>
26 #include <gst/video/video.h>
27
28 #include <math.h>
29
30 #define VIDEO_WIDTH 720
31 #define VIDEO_HEIGHT 480
32 #define VIDEO_FPS 50
33
34 /* GdkPixbuf RGBA C-Source image dump from gdk-pixbuf-csource --raw,
35 * gzipped and then base64 encoded */
36 const gchar gzipped_pixdata_base64[] =
37 "H4sICPX/Z1QAA2xvZ28ucGl4AO2dsZHrNhCG+ewK2II64ClyrhmnTtSBh4kLUOLQAUuwEhSgFtiA"
38 "A7agwA2wBT5AXJ5w4P5LgKLEO97ezDf2SCAIAftjFwuQ7/c///ojy/77+8eP7Jcs+/WfLMv+t/zW"
39 "dV32pSmK3HK6sXZbFOXV9PZfWipLbWksHdHQZxVpZP+Ee7u62/d7rt0fivIqimJnOX+w/zhauq68"
40 "aWjevfdUR1h3vXq/KMqzufueFN1J1FE+stf8KfCzobZ3q/ePojyT3v8gDSyB09GFND7g/N014rpl"
41 "41xF+Wz0/i817nwFjepP+Rb0PnDwOUOehePyQq1eurlrSkXZOkVx7OblbGK43upf+zcqyldhOT22"
42 "5GvV9ynKXIriQDpKye242Ldcve2KskX6deaRWVeebnpdu32Koryc4t/iaKksmm9Vvg3W3neWk+V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u4Wfy5P0CHKoXGaEMci1TYj7sfqJbI+uBfwSNuFelPPXkp+"
70 "k823BGXQWuGh3AmYRzf9zpKMOfvyYB8uEiuC8ZXO00zuZ020G83rs8Y/xU8gmwcaRPVKa+uk/LLh"
71 "8y1NUEbKfc+aww3eP/xS73xIBez5XbKPzwuW3H4h6Ec3j6E9ba6PkR9MGgvBNmPbLeXa58TfKRqM"
72 "brtJPJuTom/vGm4dwJ2lQXmsWe/cM4l7mFtBOCcjPR+B9iMGW4LnGWLHa4bdzNqbj6wj2QYEXYX+"
73 "JPWMKlcOvpdEaIcUu0blUA1eX7Bnbx7os02/N4iLRSMY7QuacWwSc24YjhXQgxQ/sT4s0QbQs2kO"
74 "dl1o+hiYs09p/8x/7kPaqw/3GZGfkPbmka9C5wiQ30RzJdp3R2cTh3MKozk6ta1bgfYGUzU42sMX"
75 "xsP1q4ttuLxfci7Ps18/RwfPI6f2x4QOG087/m8arRlN2vN9yP+G5yiTcpETbUBxCpo7kGZzof3D"
76 "OwGG56ca7/NwDkdrwc1qUNiXT4Hrx9hn1WLOj0n7wDF7Vcka9Gw39lyL9Gz51P78ebAv1G9BfWiv"
77 "A2kQ+mKhzbP2aEz8s8DsmWGDfbxjc/+OENDfib4b3uNUejmZqGd8g/5EY3k1CWt2cz9nONR380XM"
78 "+HPvHlnivU/ce19aA96FA/TsP2984q6jcpeg/f75kTyoxwfFiSW6ZkJLHDHvt8np93Hv3xliCLT2"
79 "OIEx3OTeILMf2EztCYKYNcYG2fMQivKdYbQUpY1s/M6ZTcbpivJMMv5Zpdhn7mdpV1GUO1l/NjTU"
80 "Usx7Z8Iz3ZuM0xXlFWT8s4CsDkmzJZPD0ThUUWaSye8THZ6RqOj/uf2L1f79T0XZCpn8vJJ0Pkb9"
81 "n6IsCOVoBp/HvS+moe83+T4dRVEUZRmyn2F9swl9yAAA";
82
83 static GstBuffer *logo_buf;
84
85 static GMainLoop *main_loop;
86 static gint count;
87
88 static GstBuffer *
create_overlay_buffer(void)89 create_overlay_buffer (void)
90 {
91 GZlibDecompressor *decompress;
92 GConverterResult decomp_res;
93 guchar *gzipped_pixdata, *pixdata;
94 gsize gzipped_size, bytes_read, pixdata_size;
95 GstBuffer *logo_pixels;
96 guint w, h, stride;
97
98 gzipped_pixdata = g_base64_decode (gzipped_pixdata_base64, &gzipped_size);
99 g_assert (gzipped_pixdata != NULL);
100
101 pixdata = g_malloc (64 * 1024);
102
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);
111
112 /* 0: Pixbuf magic (0x47646b50) */
113 g_assert (GST_READ_UINT32_BE (pixdata) == 0x47646b50);
114
115 /* 4: length incl. header */
116 /* 8: pixdata_type */
117 /* 12: rowstride (900) */
118 stride = GST_READ_UINT32_BE (pixdata + 12);
119 /* 16: width (225) */
120 w = GST_READ_UINT32_BE (pixdata + 16);
121 /* 20: height (57) */
122 h = GST_READ_UINT32_BE (pixdata + 20);
123 /* 24: pixel_data */
124 GST_LOG ("%dx%d @ %d", w, h, stride);
125 /* we assume that the last line also has padding at the end */
126 g_assert (pixdata_size - 24 >= h * stride);
127
128 logo_pixels = gst_buffer_new_and_alloc (h * stride);
129 gst_buffer_fill (logo_pixels, 0, pixdata + 24, h * stride);
130 gst_buffer_add_video_meta (logo_pixels, GST_VIDEO_FRAME_FLAG_NONE,
131 GST_VIDEO_OVERLAY_COMPOSITION_FORMAT_RGB, w, h);
132
133 g_free (pixdata);
134
135 return logo_pixels;
136 }
137
138 static gboolean
bus_cb(GstBus * bus,GstMessage * msg,gpointer user_data)139 bus_cb (GstBus * bus, GstMessage * msg, gpointer user_data)
140 {
141 GMainLoop *loop = user_data;
142
143 switch (GST_MESSAGE_TYPE (msg)) {
144 case GST_MESSAGE_ERROR:{
145 GError *err = NULL;
146 gchar *dbg;
147
148 gst_message_parse_error (msg, &err, &dbg);
149 gst_object_default_error (msg->src, err, dbg);
150 g_error_free (err);
151 g_free (dbg);
152 g_main_loop_quit (loop);
153 break;
154 }
155 default:
156 break;
157 }
158 return TRUE;
159 }
160
161 #define SPEED_SCALE_FACTOR (VIDEO_FPS * 4)
162
163 /* nicked from videotestsrc's ball pattern renderer */
164 static void
calculate_position(gint * x,gint * y,guint logo_w,guint logo_h,guint n)165 calculate_position (gint * x, gint * y, guint logo_w, guint logo_h, guint n)
166 {
167 guint r_x = logo_w / 2;
168 guint r_y = logo_h / 2;
169 guint w = VIDEO_WIDTH + logo_w;
170 guint h = VIDEO_HEIGHT + logo_h;
171
172 *x = r_x + (0.5 + 0.5 * sin (2 * G_PI * n / SPEED_SCALE_FACTOR))
173 * (w - 2 * r_x);
174 *y = r_y + (0.5 + 0.5 * sin (2 * G_PI * sqrt (2) * n / SPEED_SCALE_FACTOR))
175 * (h - 2 * r_y);
176
177 *x -= logo_w;
178 *y -= logo_h;
179 }
180
181 static GstPadProbeReturn
buffer_cb(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)182 buffer_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
183 {
184 GstVideoOverlayRectangle *rect;
185 GstVideoOverlayComposition *comp;
186 GstVideoFrame frame;
187 GstVideoMeta *vmeta;
188 GstVideoInfo vinfo;
189 GstCaps *caps;
190 gint x, y;
191
192 caps = gst_pad_get_current_caps (pad);
193 gst_video_info_from_caps (&vinfo, caps);
194 gst_caps_unref (caps);
195
196 info->data = gst_buffer_make_writable (info->data);
197
198 vmeta = gst_buffer_get_video_meta (logo_buf);
199
200 calculate_position (&x, &y, vmeta->width, vmeta->height, ++count);
201
202 GST_LOG ("%3d, %3d", x, y);
203
204 rect = gst_video_overlay_rectangle_new_raw (logo_buf, x, y,
205 vmeta->width, vmeta->height, GST_VIDEO_OVERLAY_FORMAT_FLAG_NONE);
206 comp = gst_video_overlay_composition_new (rect);
207 gst_video_overlay_rectangle_unref (rect);
208
209 gst_video_frame_map (&frame, &vinfo, info->data, GST_MAP_READWRITE);
210
211 if (!gst_video_overlay_composition_blend (comp, &frame))
212 g_warning ("Error blending overlay at position (%d,%d)", x, y);
213
214 gst_video_frame_unmap (&frame);
215
216 gst_video_overlay_composition_unref (comp);
217
218 return GST_PAD_PROBE_OK;
219 }
220
221 int
main(int argc,char ** argv)222 main (int argc, char **argv)
223 {
224 GOptionEntry options[] = {
225 {NULL}
226 };
227 GOptionContext *ctx;
228 GError *err = NULL;
229 GstElement *src, *q, *capsfilter, *sink;
230 GstElement *pipeline;
231 GstPad *src_pad;
232 GstCaps *filter_caps;
233
234 ctx = g_option_context_new ("");
235 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
236 g_option_context_add_group (ctx, gst_init_get_option_group ());
237 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
238 g_print ("Error initializing: %s\n", err->message);
239 return 1;
240 }
241 g_option_context_free (ctx);
242
243 logo_buf = create_overlay_buffer ();
244
245 main_loop = g_main_loop_new (NULL, FALSE);
246
247 pipeline = gst_pipeline_new ("pipeline");
248
249 src = gst_element_factory_make ("videotestsrc", NULL);
250 gst_util_set_object_arg (G_OBJECT (src), "pattern", "white");
251
252 src_pad = gst_element_get_static_pad (src, "src");
253 gst_pad_add_probe (src_pad, GST_PAD_PROBE_TYPE_BUFFER, buffer_cb,
254 main_loop, NULL);
255 gst_object_unref (src_pad);
256
257 q = gst_element_factory_make ("queue", NULL);
258
259 capsfilter = gst_element_factory_make ("capsfilter", NULL);
260 filter_caps = gst_caps_from_string ("video/x-raw, format = "
261 GST_VIDEO_OVERLAY_COMPOSITION_BLEND_FORMATS);
262 gst_caps_set_simple (filter_caps,
263 "width", G_TYPE_INT, VIDEO_WIDTH,
264 "height", G_TYPE_INT, VIDEO_HEIGHT,
265 "framerate", GST_TYPE_FRACTION, VIDEO_FPS, 1, NULL);
266 g_object_set (capsfilter, "caps", filter_caps, NULL);
267 gst_caps_unref (filter_caps);
268
269 sink = gst_element_factory_make ("ximagesink", NULL);
270
271 gst_bin_add_many (GST_BIN (pipeline), src, q, capsfilter, sink, NULL);
272
273 gst_element_link_many (src, q, capsfilter, sink, NULL);
274
275 count = 0;
276
277 gst_element_set_state (pipeline, GST_STATE_PLAYING);
278
279 gst_bus_add_watch (GST_ELEMENT_BUS (pipeline), bus_cb, main_loop);
280
281 g_main_loop_run (main_loop);
282
283 gst_element_set_state (pipeline, GST_STATE_NULL);
284 gst_object_unref (pipeline);
285 gst_buffer_unref (logo_buf);
286
287 return 0;
288 }
289