3 * unit test for ffmpegcolorspace
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 _RGBFormat
39 guint32 red_mask, green_mask, blue_mask, alpha_mask;
43 typedef struct _RGBConversion
52 rgb_format_to_caps (RGBFormat * fmt)
56 g_assert (fmt != NULL);
57 g_assert (fmt->endianness != 0);
59 caps = gst_caps_new_simple ("video/x-raw-rgb",
60 "bpp", G_TYPE_INT, fmt->bpp,
61 "depth", G_TYPE_INT, fmt->depth,
62 "red_mask", G_TYPE_INT, fmt->red_mask,
63 "green_mask", G_TYPE_INT, fmt->green_mask,
64 "blue_mask", G_TYPE_INT, fmt->blue_mask,
65 "width", G_TYPE_INT, 16, "height", G_TYPE_INT, 16,
66 "endianness", G_TYPE_INT, fmt->endianness,
67 "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
69 fail_unless (fmt->alpha_mask == 0 || fmt->bpp == 32);
71 if (fmt->alpha_mask != 0) {
72 gst_structure_set (gst_caps_get_structure (caps, 0),
73 "alpha_mask", G_TYPE_INT, fmt->alpha_mask, NULL);
80 create_rgb_conversions (void)
82 const RGBFormat rgb_formats[] = {
84 "RGBA", 32, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff, 0}, {
85 "ARGB", 32, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, 0}, {
86 "BGRA", 32, 32, 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff, 0}, {
87 "ABGR", 32, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, 0}, {
88 "RGBx", 32, 24, 0xff000000, 0x00ff0000, 0x0000ff00, 0x00000000, 0}, {
89 "xRGB", 32, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, 0}, {
90 "BGRx", 32, 24, 0x0000ff00, 0x00ff0000, 0xff000000, 0x00000000, 0}, {
91 "xBGR", 32, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, 0}, {
92 "RGB ", 24, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, 0}, {
93 "BGR ", 24, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, 0}, {
94 "RGB565", 16, 16, 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000, 0}, {
95 "xRGB1555", 16, 15, 0x00007c00, 0x000003e0, 0x0000001f, 0x0000000, 0}
99 guint from_endianness, to_endianness;
102 G_LITTLE_ENDIAN, G_LITTLE_ENDIAN}, {
103 G_BIG_ENDIAN, G_LITTLE_ENDIAN}, {
104 G_LITTLE_ENDIAN, G_BIG_ENDIAN}, {
105 G_BIG_ENDIAN, G_BIG_ENDIAN}
107 GList *conversions = NULL;
108 guint from_fmt, to_fmt;
110 for (from_fmt = 0; from_fmt < G_N_ELEMENTS (rgb_formats); ++from_fmt) {
111 for (to_fmt = 0; to_fmt < G_N_ELEMENTS (rgb_formats); ++to_fmt) {
114 for (i = 0; i < 4; ++i) {
115 RGBConversion *conversion;
117 conversion = g_new0 (RGBConversion, 1);
118 conversion->from_fmt = rgb_formats[from_fmt];
119 conversion->to_fmt = rgb_formats[to_fmt];
120 conversion->from_fmt.endianness = end_arr[i].from_endianness;
121 conversion->to_fmt.endianness = end_arr[i].to_endianness;
122 conversion->from_caps = rgb_format_to_caps (&conversion->from_fmt);
123 conversion->to_caps = rgb_format_to_caps (&conversion->to_fmt);
124 conversions = g_list_prepend (conversions, conversion);
129 return g_list_reverse (conversions);
133 rgb_conversion_free (RGBConversion * conv)
135 gst_caps_unref (conv->from_caps);
136 gst_caps_unref (conv->to_caps);
137 memset (conv, 0x99, sizeof (RGBConversion));
142 right_shift_colour (guint32 mask, guint32 pixel)
147 pixel = pixel & mask;
148 while ((mask & 0x01) == 0) {
157 fix_expected_colour (guint32 col_mask, guint8 col_expected)
160 gint last = g_bit_nth_msf (col_mask, -1);
161 gint first = g_bit_nth_lsf (col_mask, -1);
163 mask = 1 << (last - first + 1);
166 g_assert (col_expected == 0x00 || col_expected == 0xff);
168 /* this only works because we only check for all-bits-set or no-bits-set */
169 return col_expected & mask;
173 check_rgb_buf (const guint8 * pixels, guint32 r_mask, guint32 g_mask,
174 guint32 b_mask, guint32 a_mask, guint8 r_expected, guint8 g_expected,
175 guint8 b_expected, guint endianness, guint bpp, guint depth)
177 guint32 pixel, red, green, blue, alpha;
181 if (endianness == G_LITTLE_ENDIAN)
182 pixel = GST_READ_UINT32_LE (pixels);
184 pixel = GST_READ_UINT32_BE (pixels);
188 if (endianness == G_BIG_ENDIAN) {
189 pixel = (GST_READ_UINT8 (pixels) << 16) |
190 (GST_READ_UINT8 (pixels + 1) << 8) |
191 (GST_READ_UINT8 (pixels + 2) << 0);
193 pixel = (GST_READ_UINT8 (pixels + 2) << 16) |
194 (GST_READ_UINT8 (pixels + 1) << 8) |
195 (GST_READ_UINT8 (pixels + 0) << 0);
200 if (endianness == G_LITTLE_ENDIAN)
201 pixel = GST_READ_UINT16_LE (pixels);
203 pixel = GST_READ_UINT16_BE (pixels);
207 g_return_if_reached ();
210 red = right_shift_colour (r_mask, pixel);
211 green = right_shift_colour (g_mask, pixel);
212 blue = right_shift_colour (b_mask, pixel);
213 alpha = right_shift_colour (a_mask, pixel);
215 /* can't enable this by default, valgrind will complain about accessing
216 * uninitialised memory for the depth=24,bpp=32 formats ... */
217 /* GST_LOG ("pixels: 0x%02x 0x%02x 0x%02x 0x%02x => pixel = 0x%08x",
218 pixels[0], (guint) pixels[1], pixels[2], pixels[3], pixel); */
220 /* fix up the mask (for rgb15/16) */
222 r_expected = fix_expected_colour (r_mask, r_expected);
223 g_expected = fix_expected_colour (g_mask, g_expected);
224 b_expected = fix_expected_colour (b_mask, b_expected);
227 fail_unless (red == r_expected, "RED: expected 0x%02x, found 0x%02x "
228 "Bytes: 0x%02x 0x%02x 0x%02x 0x%02x Pixel: 0x%08x", r_expected, red,
229 pixels[0], pixels[1], pixels[2], pixels[3], pixel);
230 fail_unless (green == g_expected, "GREEN: expected 0x%02x, found 0x%02x "
231 "Bytes: 0x%02x 0x%02x 0x%02x 0x%02x Pixel: 0x%08x", g_expected, green,
232 pixels[0], pixels[1], pixels[2], pixels[3], pixel);
233 fail_unless (blue == b_expected, "BLUE: expected 0x%02x, found 0x%02x "
234 "Bytes: 0x%02x 0x%02x 0x%02x 0x%02x Pixel: 0x%08x", b_expected, blue,
235 pixels[0], pixels[1], pixels[2], pixels[3], pixel);
237 // FIXME: fix alpha check
238 // fail_unless (a_mask == 0 || alpha != 0); /* better than nothing */
242 got_buf_cb (GstElement * sink, GstBuffer * new_buf, GstPad * pad,
243 GstBuffer ** p_old_buf)
245 gst_buffer_replace (p_old_buf, new_buf);
248 /* Note: lots of this code here is also in the videotestsrc.c unit test */
249 GST_START_TEST (test_rgb_to_rgb)
253 const gchar *pattern_name;
258 } test_patterns[] = {
260 "white", 3, 0xff, 0xff, 0xff}, {
261 "red", 4, 0xff, 0x00, 0x00}, {
262 "green", 5, 0x00, 0xff, 0x00}, {
263 "blue", 6, 0x00, 0x00, 0xff}, {
264 "black", 2, 0x00, 0x00, 0x00}
266 GstElement *pipeline, *src, *filter1, *csp, *filter2, *sink;
267 const GstCaps *template_caps;
268 GstBuffer *buf = NULL;
270 GList *conversions, *l;
273 /* test check function */
274 fail_unless (right_shift_colour (0x00ff0000, 0x11223344) == 0x22);
276 pipeline = gst_pipeline_new ("pipeline");
277 src = gst_check_setup_element ("videotestsrc");
278 filter1 = gst_check_setup_element ("capsfilter");
279 csp = gst_check_setup_element ("ffmpegcolorspace");
280 filter2 = gst_element_factory_make ("capsfilter", "to_filter");
281 sink = gst_check_setup_element ("fakesink");
283 gst_bin_add_many (GST_BIN (pipeline), src, filter1, csp, filter2, sink, NULL);
285 fail_unless (gst_element_link (src, filter1));
286 fail_unless (gst_element_link (filter1, csp));
287 fail_unless (gst_element_link (csp, filter2));
288 fail_unless (gst_element_link (filter2, sink));
290 srcpad = gst_element_get_pad (src, "src");
291 template_caps = gst_pad_get_pad_template_caps (srcpad);
292 gst_object_unref (srcpad);
294 g_object_set (sink, "signal-handoffs", TRUE, NULL);
295 g_signal_connect (sink, "preroll-handoff", G_CALLBACK (got_buf_cb), &buf);
297 GST_LOG ("videotestsrc src template caps: %" GST_PTR_FORMAT, template_caps);
299 conversions = create_rgb_conversions ();
301 for (l = conversions; l != NULL; l = l->next) {
302 RGBConversion *conv = (RGBConversion *) l->data;
303 RGBFormat *from = &conv->from_fmt;
304 RGBFormat *to = &conv->to_fmt;
306 /* does videotestsrc support the from_caps? */
307 if (!gst_caps_is_subset (conv->from_caps, template_caps)) {
308 GST_DEBUG ("videotestsrc doesn't support from_caps %" GST_PTR_FORMAT,
313 /* caps are supported, let's run some tests then ... */
314 for (p = 0; p < G_N_ELEMENTS (test_patterns); ++p) {
315 GstStateChangeReturn state_ret;
317 gst_element_set_state (pipeline, GST_STATE_NULL);
319 g_object_set (src, "pattern", test_patterns[p].pattern_enum, NULL);
321 GST_INFO ("%5s %u/%u %08x %08x %08x %08x %u => "
322 "%5s %u/%u %08x %08x %08x %08x %u, pattern=%s",
323 from->nick, from->bpp, from->depth, from->red_mask,
324 from->green_mask, from->blue_mask, from->alpha_mask,
325 from->endianness, to->nick, to->bpp, to->depth, to->red_mask,
326 to->green_mask, to->blue_mask, to->alpha_mask, to->endianness,
327 test_patterns[p].pattern_name);
329 /* now get videotestsrc to produce a buffer with the given caps */
330 g_object_set (filter1, "caps", conv->from_caps, NULL);
332 /* ... and force ffmpegcolorspace to convert to our target caps */
333 g_object_set (filter2, "caps", conv->to_caps, NULL);
335 state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
336 if (state_ret == GST_STATE_CHANGE_FAILURE) {
340 msg = gst_bus_poll (GST_ELEMENT_BUS (pipeline), GST_MESSAGE_ERROR, 0);
341 fail_if (msg == NULL, "expected ERROR message on the bus");
342 fail_unless (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR);
343 gst_message_parse_error (msg, &err, NULL);
344 fail_unless (err != NULL);
345 if (msg->src == GST_OBJECT_CAST (src) &&
346 err->code == GST_STREAM_ERROR_FORMAT) {
347 GST_DEBUG ("ffmpegcolorspace does not support this conversion");
348 gst_message_unref (msg);
352 fail_unless (state_ret != GST_STATE_CHANGE_FAILURE,
353 "pipeline _set_state() to PAUSED failed: %s", err->message);
356 state_ret = gst_element_get_state (pipeline, NULL, NULL, -1);
357 fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS,
358 "pipeline failed going to PAUSED state");
360 state_ret = gst_element_set_state (pipeline, GST_STATE_NULL);
361 fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
363 fail_unless (buf != NULL);
365 /* check buffer caps */
370 fail_unless (GST_BUFFER_CAPS (buf) != NULL);
371 s = gst_caps_get_structure (GST_BUFFER_CAPS (buf), 0);
372 fail_unless (gst_structure_get_int (s, "bpp", &v));
373 fail_unless_equals_int (v, to->bpp);
374 fail_unless (gst_structure_get_int (s, "depth", &v));
375 fail_unless_equals_int (v, to->depth);
376 fail_unless (gst_structure_get_int (s, "red_mask", &v));
377 fail_unless_equals_int (v, to->red_mask);
378 fail_unless (gst_structure_get_int (s, "green_mask", &v));
379 fail_unless_equals_int (v, to->green_mask);
380 fail_unless (gst_structure_get_int (s, "blue_mask", &v));
381 fail_unless_equals_int (v, to->blue_mask);
382 /* there mustn't be an alpha_mask if there's no alpha component */
383 if (to->depth == 32) {
384 fail_unless (gst_structure_get_int (s, "alpha_mask", &v));
385 fail_unless_equals_int (v, to->alpha_mask);
387 fail_unless (gst_structure_get_value (s, "alpha_mask") == NULL);
391 /* now check the top-left pixel */
392 check_rgb_buf (GST_BUFFER_DATA (buf), to->red_mask,
393 to->green_mask, to->blue_mask, to->alpha_mask,
394 test_patterns[p].r_expected, test_patterns[p].g_expected,
395 test_patterns[p].b_expected, to->endianness, to->bpp, to->depth);
397 gst_buffer_unref (buf);
402 g_list_foreach (conversions, (GFunc) rgb_conversion_free, NULL);
403 g_list_free (conversions);
405 gst_element_set_state (pipeline, GST_STATE_NULL);
406 gst_object_unref (pipeline);
412 ffmpegcolorspace_suite (void)
414 Suite *s = suite_create ("ffmpegcolorspace");
415 TCase *tc_chain = tcase_create ("general");
417 suite_add_tcase (s, tc_chain);
420 if (RUNNING_ON_VALGRIND) {
421 /* otherwise valgrind errors out when liboil probes CPU extensions
422 * during which it causes SIGILLs etc. to be fired */
423 g_setenv ("OIL_CPU_FLAGS", "0", 0);
424 /* test_rgb_formats takes a bit longer, so increase timeout */
425 tcase_set_timeout (tc_chain, 5 * 60);
429 /* FIXME: add tests for YUV <=> YUV and YUV <=> RGB */
430 tcase_add_test (tc_chain, test_rgb_to_rgb);
435 GST_CHECK_MAIN (ffmpegcolorspace);