3 * unit test for videotestsrc
5 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
6 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
29 # include <valgrind/valgrind.h>
34 #include <gst/check/gstcheck.h>
36 /* For ease of programming we use globals to keep refs for our floating
37 * src and sink pads we create; otherwise we always have to do get_pad,
38 * get_peer, and then remove references in every test function */
39 static GstPad *mysinkpad;
42 #define CAPS_TEMPLATE_STRING \
44 "format = (fourcc) UYVY, " \
45 "width = (int) [ 1, MAX ], " \
46 "height = (int) [ 1, MAX ], " \
47 "framerate = (fraction) [ 0/1, MAX ]"
49 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
52 GST_STATIC_CAPS (CAPS_TEMPLATE_STRING)
56 setup_videotestsrc (void)
58 GstElement *videotestsrc;
60 GST_DEBUG ("setup_videotestsrc");
61 videotestsrc = gst_check_setup_element ("videotestsrc");
62 mysinkpad = gst_check_setup_sink_pad (videotestsrc, &sinktemplate, NULL);
63 gst_pad_set_active (mysinkpad, TRUE);
69 cleanup_videotestsrc (GstElement * videotestsrc)
71 GST_DEBUG ("cleanup_videotestsrc");
73 gst_check_drop_buffers ();
75 gst_pad_set_active (mysinkpad, FALSE);
76 gst_check_teardown_sink_pad (videotestsrc);
77 gst_check_teardown_element (videotestsrc);
80 GST_START_TEST (test_all_patterns)
82 GstElement *videotestsrc;
88 videotestsrc = setup_videotestsrc ();
89 oclass = G_OBJECT_GET_CLASS (videotestsrc);
90 property = g_object_class_find_property (oclass, "pattern");
91 fail_unless (G_IS_PARAM_SPEC_ENUM (property));
92 values = G_ENUM_CLASS (g_type_class_ref (property->value_type))->values;
94 while (values[j].value_name) {
95 GST_DEBUG_OBJECT (videotestsrc, "testing pattern %s", values[j].value_name);
97 g_object_set (videotestsrc, "pattern", j, NULL);
99 fail_unless (gst_element_set_state (videotestsrc,
100 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
101 "could not set to playing");
103 g_mutex_lock (check_mutex);
104 while (g_list_length (buffers) < 10) {
105 GST_DEBUG_OBJECT (videotestsrc, "Waiting for more buffers");
106 g_cond_wait (check_cond, check_mutex);
108 g_mutex_unlock (check_mutex);
110 gst_element_set_state (videotestsrc, GST_STATE_READY);
112 gst_check_drop_buffers ();
117 cleanup_videotestsrc (videotestsrc);
123 right_shift_colour (guint32 mask, guint32 pixel)
128 pixel = pixel & mask;
129 while ((mask & 0x01) == 0) {
138 fix_expected_colour (guint32 col_mask, guint8 col_expected)
141 gint last = g_bit_nth_msf (col_mask, -1);
142 gint first = g_bit_nth_lsf (col_mask, -1);
144 mask = 1 << (last - first + 1);
147 g_assert (col_expected == 0x00 || col_expected == 0xff);
149 /* this only works because we only check for all-bits-set or no-bits-set */
150 return col_expected & mask;
154 check_rgb_buf (const guint8 * pixels, guint32 r_mask, guint32 g_mask,
155 guint32 b_mask, guint32 a_mask, guint8 r_expected, guint8 g_expected,
156 guint8 b_expected, guint endianness, guint bpp, guint depth)
158 guint32 pixel, red, green, blue, alpha;
162 if (endianness == G_LITTLE_ENDIAN)
163 pixel = GST_READ_UINT32_LE (pixels);
165 pixel = GST_READ_UINT32_BE (pixels);
169 if (endianness == G_BIG_ENDIAN) {
170 pixel = (GST_READ_UINT8 (pixels) << 16) |
171 (GST_READ_UINT8 (pixels + 1) << 8) |
172 (GST_READ_UINT8 (pixels + 2) << 0);
174 pixel = (GST_READ_UINT8 (pixels + 2) << 16) |
175 (GST_READ_UINT8 (pixels + 1) << 8) |
176 (GST_READ_UINT8 (pixels + 0) << 0);
181 if (endianness == G_LITTLE_ENDIAN)
182 pixel = GST_READ_UINT16_LE (pixels);
184 pixel = GST_READ_UINT16_BE (pixels);
188 g_return_if_reached ();
191 red = right_shift_colour (r_mask, pixel);
192 green = right_shift_colour (g_mask, pixel);
193 blue = right_shift_colour (b_mask, pixel);
194 alpha = right_shift_colour (a_mask, pixel);
196 /* can't enable this by default, valgrind will complain about accessing
197 * uninitialised memory for the depth=24,bpp=32 formats ... */
198 /* GST_LOG ("pixels: 0x%02x 0x%02x 0x%02x 0x%02x => pixel = 0x%08x",
199 pixels[0], (guint) pixels[1], pixels[2], pixels[3], pixel); */
201 /* fix up the mask (for rgb15/16) */
203 r_expected = fix_expected_colour (r_mask, r_expected);
204 g_expected = fix_expected_colour (g_mask, g_expected);
205 b_expected = fix_expected_colour (b_mask, b_expected);
208 fail_unless (red == r_expected, "RED: expected 0x%02x, found 0x%02x",
210 fail_unless (green == g_expected, "GREEN: expected 0x%02x, found 0x%02x",
212 fail_unless (blue == b_expected, "BLUE: expected 0x%02x, found 0x%02x",
215 fail_unless (a_mask == 0 || alpha != 0); /* better than nothing */
219 got_buf_cb (GstElement * sink, GstBuffer * new_buf, GstPad * pad,
220 GstBuffer ** p_old_buf)
222 gst_buffer_replace (p_old_buf, new_buf);
225 /* tests the positioning of pixels within the various RGB pixel layouts */
226 GST_START_TEST (test_rgb_formats)
230 const gchar *pattern_name;
235 } test_patterns[] = {
237 "white", 3, 0xff, 0xff, 0xff}, {
238 "red", 4, 0xff, 0x00, 0x00}, {
239 "green", 5, 0x00, 0xff, 0x00}, {
240 "blue", 6, 0x00, 0x00, 0xff}, {
241 "black", 2, 0x00, 0x00, 0x00}
247 guint32 red_mask, green_mask, blue_mask, alpha_mask;
250 "RGBA", 32, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff}, {
251 "ARGB", 32, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000}, {
252 "BGRA", 32, 32, 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff}, {
253 "ABGR", 32, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000}, {
254 "RGBx", 32, 24, 0xff000000, 0x00ff0000, 0x0000ff00, 0x00000000}, {
255 "xRGB", 32, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}, {
256 "BGRx", 32, 24, 0x0000ff00, 0x00ff0000, 0xff000000, 0x00000000}, {
257 "xBGR", 32, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}, {
258 "RGB ", 24, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}, {
259 "BGR ", 24, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}, {
260 "RGB565", 16, 16, 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000}, {
261 "xRGB1555", 16, 15, 0x00007c00, 0x000003e0, 0x0000001f, 0x0000000}
263 GstElement *pipeline, *src, *filter, *sink;
264 const GstCaps *template_caps;
265 GstBuffer *buf = NULL;
269 /* test check function */
270 fail_unless (right_shift_colour (0x00ff0000, 0x11223344) == 0x22);
272 pipeline = gst_pipeline_new ("pipeline");
273 src = gst_check_setup_element ("videotestsrc");
274 filter = gst_check_setup_element ("capsfilter");
275 sink = gst_check_setup_element ("fakesink");
277 gst_bin_add_many (GST_BIN (pipeline), src, filter, sink, NULL);
279 fail_unless (gst_element_link (src, filter));
280 fail_unless (gst_element_link (filter, sink));
282 srcpad = gst_element_get_static_pad (src, "src");
283 template_caps = gst_pad_get_pad_template_caps (srcpad);
284 gst_object_unref (srcpad);
286 g_object_set (sink, "signal-handoffs", TRUE, NULL);
287 g_signal_connect (sink, "preroll-handoff", G_CALLBACK (got_buf_cb), &buf);
289 GST_LOG ("videotestsrc src template caps: %" GST_PTR_FORMAT, template_caps);
291 for (i = 0; i < G_N_ELEMENTS (rgb_formats); ++i) {
292 for (e = 0; e < 2; ++e) {
297 endianness = G_BYTE_ORDER;
300 (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_LITTLE_ENDIAN : G_BIG_ENDIAN;
303 caps = gst_caps_new_simple ("video/x-raw-rgb",
304 "bpp", G_TYPE_INT, rgb_formats[i].bpp,
305 "depth", G_TYPE_INT, rgb_formats[i].depth,
306 "red_mask", G_TYPE_INT, rgb_formats[i].red_mask,
307 "green_mask", G_TYPE_INT, rgb_formats[i].green_mask,
308 "blue_mask", G_TYPE_INT, rgb_formats[i].blue_mask,
309 "width", G_TYPE_INT, 16, "height", G_TYPE_INT, 16,
310 "endianness", G_TYPE_INT, endianness,
311 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
313 fail_unless (rgb_formats[i].alpha_mask == 0 || rgb_formats[i].bpp == 32);
315 if (rgb_formats[i].alpha_mask != 0) {
316 gst_structure_set (gst_caps_get_structure (caps, 0),
317 "alpha_mask", G_TYPE_INT, rgb_formats[i].alpha_mask, NULL);
320 if (gst_caps_is_subset (caps, template_caps)) {
322 /* caps are supported, let's run some tests then ... */
323 for (p = 0; p < G_N_ELEMENTS (test_patterns); ++p) {
324 GstStateChangeReturn state_ret;
326 g_object_set (src, "pattern", test_patterns[p].pattern_enum, NULL);
328 GST_INFO ("%5s %u/%u %08x %08x %08x %08x %u, pattern=%s",
329 rgb_formats[i].nick, rgb_formats[i].bpp, rgb_formats[i].depth,
330 rgb_formats[i].red_mask, rgb_formats[i].green_mask,
331 rgb_formats[i].blue_mask, rgb_formats[i].alpha_mask, endianness,
332 test_patterns[p].pattern_name);
334 /* now get videotestsrc to produce a buffer with the given caps */
335 g_object_set (filter, "caps", caps, NULL);
337 state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
338 fail_unless (state_ret != GST_STATE_CHANGE_FAILURE,
339 "pipeline _set_state() to PAUSED failed");
340 state_ret = gst_element_get_state (pipeline, NULL, NULL, -1);
341 fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS,
342 "pipeline failed going to PAUSED state");
344 state_ret = gst_element_set_state (pipeline, GST_STATE_NULL);
345 fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
347 fail_unless (buf != NULL);
349 /* check buffer caps */
354 fail_unless (GST_BUFFER_CAPS (buf) != NULL);
356 s = gst_caps_get_structure (GST_BUFFER_CAPS (buf), 0);
357 fail_unless (gst_structure_get_int (s, "bpp", &v));
358 fail_unless_equals_int (v, rgb_formats[i].bpp);
359 fail_unless (gst_structure_get_int (s, "depth", &v));
360 fail_unless_equals_int (v, rgb_formats[i].depth);
361 fail_unless (gst_structure_get_int (s, "red_mask", &v));
362 fail_unless_equals_int (v, rgb_formats[i].red_mask);
363 fail_unless (gst_structure_get_int (s, "green_mask", &v));
364 fail_unless_equals_int (v, rgb_formats[i].green_mask);
365 fail_unless (gst_structure_get_int (s, "blue_mask", &v));
366 fail_unless_equals_int (v, rgb_formats[i].blue_mask);
367 /* there mustn't be an alpha_mask if there's no alpha component */
368 if (rgb_formats[i].depth == 32) {
369 fail_unless (gst_structure_get_int (s, "alpha_mask", &v));
370 fail_unless_equals_int (v, rgb_formats[i].alpha_mask);
372 fail_unless (gst_structure_get_value (s, "alpha_mask") == NULL);
377 /* now check the first pixel */
378 check_rgb_buf (GST_BUFFER_DATA (buf), rgb_formats[i].red_mask,
379 rgb_formats[i].green_mask, rgb_formats[i].blue_mask,
380 rgb_formats[i].alpha_mask, test_patterns[p].r_expected,
381 test_patterns[p].g_expected, test_patterns[p].b_expected,
382 endianness, rgb_formats[i].bpp, rgb_formats[i].depth);
384 gst_buffer_unref (buf);
389 GST_INFO ("videotestsrc doesn't support format %" GST_PTR_FORMAT, caps);
392 gst_caps_unref (caps);
396 gst_object_unref (pipeline);
402 /* FIXME: add tests for YUV formats */
405 videotestsrc_suite (void)
407 Suite *s = suite_create ("videotestsrc");
408 TCase *tc_chain = tcase_create ("general");
410 suite_add_tcase (s, tc_chain);
413 if (RUNNING_ON_VALGRIND) {
414 /* otherwise valgrind errors out when liboil probes CPU extensions
415 * during which it causes SIGILLs etc. to be fired */
416 g_setenv ("OIL_CPU_FLAGS", "0", 0);
417 /* test_rgb_formats takes a bit longer, so increase timeout */
418 tcase_set_timeout (tc_chain, 5 * 60);
422 tcase_add_test (tc_chain, test_all_patterns);
423 tcase_add_test (tc_chain, test_rgb_formats);
428 GST_CHECK_MAIN (videotestsrc);