3 * unit test for videoconvert
5 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
28 # include <valgrind/valgrind.h>
33 #include <gst/check/gstcheck.h>
35 typedef struct _RGBConversion
37 const gchar *from_fmt;
44 rgb_format_to_caps (const gchar * fmt)
48 g_assert (fmt != NULL);
50 caps = gst_caps_new_simple ("video/x-raw",
51 "format", G_TYPE_STRING, fmt,
52 "width", G_TYPE_INT, 16, "height", G_TYPE_INT, 16,
53 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
59 create_rgb_conversions (void)
61 const gchar *rgb_formats[] = {
77 GList *conversions = NULL;
78 guint from_fmt, to_fmt;
80 for (from_fmt = 0; from_fmt < G_N_ELEMENTS (rgb_formats); ++from_fmt) {
81 for (to_fmt = 0; to_fmt < G_N_ELEMENTS (rgb_formats); ++to_fmt) {
84 for (i = 0; i < 4; ++i) {
85 RGBConversion *conversion;
87 conversion = g_new0 (RGBConversion, 1);
88 conversion->from_fmt = rgb_formats[from_fmt];
89 conversion->to_fmt = rgb_formats[to_fmt];
90 conversion->from_caps = rgb_format_to_caps (conversion->from_fmt);
91 conversion->to_caps = rgb_format_to_caps (conversion->to_fmt);
92 conversions = g_list_prepend (conversions, conversion);
97 return g_list_reverse (conversions);
101 rgb_conversion_free (RGBConversion * conv)
103 gst_caps_unref (conv->from_caps);
104 gst_caps_unref (conv->to_caps);
105 memset (conv, 0x99, sizeof (RGBConversion));
110 right_shift_colour (guint32 mask, guint32 pixel)
115 pixel = pixel & mask;
116 while ((mask & 0x01) == 0) {
126 fix_expected_colour (guint32 col_mask, guint8 col_expected)
129 gint last = g_bit_nth_msf (col_mask, -1);
130 gint first = g_bit_nth_lsf (col_mask, -1);
132 mask = 1 << (last - first + 1);
135 g_assert (col_expected == 0x00 || col_expected == 0xff);
137 /* this only works because we only check for all-bits-set or no-bits-set */
138 return col_expected & mask;
142 check_rgb_buf (const guint8 * pixels, guint32 r_mask, guint32 g_mask,
143 guint32 b_mask, guint32 a_mask, guint8 r_expected, guint8 g_expected,
144 guint8 b_expected, guint endianness, guint bpp, guint depth)
146 guint32 pixel, red, green, blue;
150 if (endianness == G_LITTLE_ENDIAN)
151 pixel = GST_READ_UINT32_LE (pixels);
153 pixel = GST_READ_UINT32_BE (pixels);
157 if (endianness == G_BIG_ENDIAN) {
158 pixel = (GST_READ_UINT8 (pixels) << 16) |
159 (GST_READ_UINT8 (pixels + 1) << 8) |
160 (GST_READ_UINT8 (pixels + 2) << 0);
162 pixel = (GST_READ_UINT8 (pixels + 2) << 16) |
163 (GST_READ_UINT8 (pixels + 1) << 8) |
164 (GST_READ_UINT8 (pixels + 0) << 0);
169 if (endianness == G_LITTLE_ENDIAN)
170 pixel = GST_READ_UINT16_LE (pixels);
172 pixel = GST_READ_UINT16_BE (pixels);
176 g_return_if_reached ();
179 red = right_shift_colour (r_mask, pixel);
180 green = right_shift_colour (g_mask, pixel);
181 blue = right_shift_colour (b_mask, pixel);
182 /* alpha = right_shift_colour (a_mask, pixel); */
184 /* can't enable this by default, valgrind will complain about accessing
185 * uninitialised memory for the depth=24,bpp=32 formats ... */
186 /* GST_LOG ("pixels: 0x%02x 0x%02x 0x%02x 0x%02x => pixel = 0x%08x",
187 pixels[0], (guint) pixels[1], pixels[2], pixels[3], pixel); */
189 /* fix up the mask (for rgb15/16) */
191 r_expected = fix_expected_colour (r_mask, r_expected);
192 g_expected = fix_expected_colour (g_mask, g_expected);
193 b_expected = fix_expected_colour (b_mask, b_expected);
196 fail_unless (red == r_expected, "RED: expected 0x%02x, found 0x%02x "
197 "Bytes: 0x%02x 0x%02x 0x%02x 0x%02x Pixel: 0x%08x", r_expected, red,
198 pixels[0], pixels[1], pixels[2], pixels[3], pixel);
199 fail_unless (green == g_expected, "GREEN: expected 0x%02x, found 0x%02x "
200 "Bytes: 0x%02x 0x%02x 0x%02x 0x%02x Pixel: 0x%08x", g_expected, green,
201 pixels[0], pixels[1], pixels[2], pixels[3], pixel);
202 fail_unless (blue == b_expected, "BLUE: expected 0x%02x, found 0x%02x "
203 "Bytes: 0x%02x 0x%02x 0x%02x 0x%02x Pixel: 0x%08x", b_expected, blue,
204 pixels[0], pixels[1], pixels[2], pixels[3], pixel);
206 // FIXME: fix alpha check
207 // fail_unless (a_mask == 0 || alpha != 0); /* better than nothing */
212 got_buf_cb (GstElement * sink, GstBuffer * new_buf, GstPad * pad,
213 GstBuffer ** p_old_buf)
215 gst_buffer_replace (p_old_buf, new_buf);
218 /* Note: lots of this code here is also in the videotestsrc.c unit test */
219 GST_START_TEST (test_rgb_to_rgb)
223 const gchar *pattern_name;
228 } test_patterns[] = {
230 "white", 3, 0xff, 0xff, 0xff}, {
231 "red", 4, 0xff, 0x00, 0x00}, {
232 "green", 5, 0x00, 0xff, 0x00}, {
233 "blue", 6, 0x00, 0x00, 0xff}, {
234 "black", 2, 0x00, 0x00, 0x00}
236 GstElement *pipeline, *src, *filter1, *csp, *filter2, *sink;
237 GstCaps *template_caps;
238 GstBuffer *buf = NULL;
239 GstPad *srcpad, *csppad;
240 GList *conversions, *l;
243 /* test check function */
244 fail_unless (right_shift_colour (0x00ff0000, 0x11223344) == 0x22);
246 pipeline = gst_pipeline_new ("pipeline");
247 src = gst_check_setup_element ("videotestsrc");
248 filter1 = gst_check_setup_element ("capsfilter");
249 csp = gst_check_setup_element ("videoconvert");
250 filter2 = gst_element_factory_make ("capsfilter", "to_filter");
251 sink = gst_check_setup_element ("fakesink");
253 gst_bin_add_many (GST_BIN (pipeline), src, filter1, csp, filter2, sink, NULL);
255 fail_unless (gst_element_link (src, filter1));
256 fail_unless (gst_element_link (filter1, csp));
257 fail_unless (gst_element_link (csp, filter2));
258 fail_unless (gst_element_link (filter2, sink));
260 srcpad = gst_element_get_static_pad (src, "src");
261 template_caps = gst_pad_get_pad_template_caps (srcpad);
262 gst_object_unref (srcpad);
264 csppad = gst_element_get_static_pad (csp, "src");
266 g_object_set (sink, "signal-handoffs", TRUE, NULL);
267 g_signal_connect (sink, "preroll-handoff", G_CALLBACK (got_buf_cb), &buf);
269 GST_LOG ("videotestsrc src template caps: %" GST_PTR_FORMAT, template_caps);
271 conversions = create_rgb_conversions ();
273 for (l = conversions; l != NULL; l = l->next) {
274 RGBConversion *conv = (RGBConversion *) l->data;
276 /* does videotestsrc support the from_caps? */
277 if (!gst_caps_is_subset (conv->from_caps, template_caps)) {
278 GST_DEBUG ("videotestsrc doesn't support from_caps %" GST_PTR_FORMAT,
283 /* caps are supported, let's run some tests then ... */
284 for (p = 0; p < G_N_ELEMENTS (test_patterns); ++p) {
285 GstStateChangeReturn state_ret;
286 const gchar *from = conv->from_fmt;
287 const gchar *to = conv->to_fmt;
293 /* trick compiler into thinking from is used, might throw warning
294 * otherwise if the debugging system is disabled */
295 fail_unless (from != NULL);
297 gst_element_set_state (pipeline, GST_STATE_NULL);
299 g_object_set (src, "pattern", test_patterns[p].pattern_enum, NULL);
301 GST_INFO ("%5s => %5s, pattern=%s",
302 from, to, test_patterns[p].pattern_name);
304 /* now get videotestsrc to produce a buffer with the given caps */
305 g_object_set (filter1, "caps", conv->from_caps, NULL);
307 /* ... and force videoconvert to convert to our target caps */
308 g_object_set (filter2, "caps", conv->to_caps, NULL);
310 state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
311 if (state_ret == GST_STATE_CHANGE_FAILURE) {
315 msg = gst_bus_poll (GST_ELEMENT_BUS (pipeline), GST_MESSAGE_ERROR, 0);
316 fail_if (msg == NULL, "expected ERROR message on the bus");
317 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
318 gst_message_parse_error (msg, &err, NULL);
319 fail_unless (err != NULL);
320 if (msg->src == GST_OBJECT_CAST (src) &&
321 err->code == GST_STREAM_ERROR_FORMAT) {
322 GST_DEBUG ("videoconvert does not support this conversion");
323 gst_message_unref (msg);
327 fail_unless (state_ret != GST_STATE_CHANGE_FAILURE,
328 "pipeline _set_state() to PAUSED failed: %s", err->message);
331 state_ret = gst_element_get_state (pipeline, NULL, NULL, -1);
332 fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS,
333 "pipeline failed going to PAUSED state");
335 state_ret = gst_element_set_state (pipeline, GST_STATE_NULL);
336 fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
338 fail_unless (buf != NULL);
340 /* check buffer caps */
346 g_object_get (csppad, "caps", &caps, NULL);
348 fail_unless (caps != NULL);
349 s = gst_caps_get_structure (caps, 0);
350 fmt = gst_structure_get_string (s, "format");
351 fail_unless (fmt != NULL);
352 fail_unless (!strcmp (fmt, to));
356 /* now check the top-left pixel */
357 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
358 check_rgb_buf (data, to->red_mask,
359 to->green_mask, to->blue_mask, to->alpha_mask,
360 test_patterns[p].r_expected, test_patterns[p].g_expected,
361 test_patterns[p].b_expected, to->endianness, to->bpp, to->depth);
362 gst_buffer_unmap (buf, data, size);
365 gst_buffer_unref (buf);
370 gst_caps_unref (template_caps);
372 g_list_foreach (conversions, (GFunc) rgb_conversion_free, NULL);
373 g_list_free (conversions);
375 gst_element_set_state (pipeline, GST_STATE_NULL);
376 gst_object_unref (pipeline);
382 videoconvert_suite (void)
384 Suite *s = suite_create ("videoconvert");
385 TCase *tc_chain = tcase_create ("general");
387 suite_add_tcase (s, tc_chain);
390 if (RUNNING_ON_VALGRIND) {
391 /* otherwise valgrind errors out when liboil probes CPU extensions
392 * during which it causes SIGILLs etc. to be fired */
393 g_setenv ("OIL_CPU_FLAGS", "0", 0);
394 /* test_rgb_formats takes a bit longer, so increase timeout */
395 tcase_set_timeout (tc_chain, 10 * 60);
399 /* FIXME: add tests for YUV <=> YUV and YUV <=> RGB */
400 tcase_add_test (tc_chain, test_rgb_to_rgb);
405 GST_CHECK_MAIN (videoconvert);