51b9839b82053a0fb067b5588d4ee81964832002
[platform/upstream/gstreamer.git] / tests / check / elements / ffmpegcolorspace.c
1 /* GStreamer
2  *
3  * unit test for ffmpegcolorspace
4  *
5  * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #ifdef HAVE_VALGRIND
28 # include <valgrind/valgrind.h>
29 #endif
30
31 #include <unistd.h>
32
33 #include <gst/check/gstcheck.h>
34
35 typedef struct _RGBFormat
36 {
37   const gchar *nick;
38   guint bpp, depth;
39   guint32 red_mask, green_mask, blue_mask, alpha_mask;
40   guint endianness;
41 } RGBFormat;
42
43 typedef struct _RGBConversion
44 {
45   RGBFormat from_fmt;
46   RGBFormat to_fmt;
47   GstCaps *from_caps;
48   GstCaps *to_caps;
49 } RGBConversion;
50
51 static GstCaps *
52 rgb_format_to_caps (RGBFormat * fmt)
53 {
54   GstCaps *caps;
55
56   g_assert (fmt != NULL);
57   g_assert (fmt->endianness != 0);
58
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);
68
69   fail_unless (fmt->alpha_mask == 0 || fmt->bpp == 32);
70
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);
74   }
75
76   return caps;
77 }
78
79 static GList *
80 create_rgb_conversions (void)
81 {
82   const RGBFormat rgb_formats[] = {
83     {
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}
96   };
97   const struct
98   {
99     guint from_endianness, to_endianness;
100   } end_arr[4] = {
101     {
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}
106   };
107   GList *conversions = NULL;
108   guint from_fmt, to_fmt;
109
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) {
112       guint i;
113
114       for (i = 0; i < 4; ++i) {
115         RGBConversion *conversion;
116
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);
125       }
126     }
127   }
128
129   return g_list_reverse (conversions);
130 }
131
132 static void
133 rgb_conversion_free (RGBConversion * conv)
134 {
135   gst_caps_unref (conv->from_caps);
136   gst_caps_unref (conv->to_caps);
137   memset (conv, 0x99, sizeof (RGBConversion));
138   g_free (conv);
139 }
140
141 static guint32
142 right_shift_colour (guint32 mask, guint32 pixel)
143 {
144   if (mask == 0)
145     return 0;
146
147   pixel = pixel & mask;
148   while ((mask & 0x01) == 0) {
149     mask = mask >> 1;
150     pixel = pixel >> 1;
151   }
152
153   return pixel;
154 }
155
156 static guint8
157 fix_expected_colour (guint32 col_mask, guint8 col_expected)
158 {
159   guint32 mask;
160   gint last = g_bit_nth_msf (col_mask, -1);
161   gint first = g_bit_nth_lsf (col_mask, -1);
162
163   mask = 1 << (last - first + 1);
164   mask -= 1;
165
166   g_assert (col_expected == 0x00 || col_expected == 0xff);
167
168   /* this only works because we only check for all-bits-set or no-bits-set */
169   return col_expected & mask;
170 }
171
172 static void
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)
176 {
177   guint32 pixel, red, green, blue, alpha;
178
179   switch (bpp) {
180     case 32:{
181       if (endianness == G_LITTLE_ENDIAN)
182         pixel = GST_READ_UINT32_LE (pixels);
183       else
184         pixel = GST_READ_UINT32_BE (pixels);
185       break;
186     }
187     case 24:{
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);
192       } else {
193         pixel = (GST_READ_UINT8 (pixels + 2) << 16) |
194             (GST_READ_UINT8 (pixels + 1) << 8) |
195             (GST_READ_UINT8 (pixels + 0) << 0);
196       }
197       break;
198     }
199     case 16:{
200       if (endianness == G_LITTLE_ENDIAN)
201         pixel = GST_READ_UINT16_LE (pixels);
202       else
203         pixel = GST_READ_UINT16_BE (pixels);
204       break;
205     }
206     default:
207       g_return_if_reached ();
208   }
209
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);
214
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); */
219
220   /* fix up the mask (for rgb15/16) */
221   if (bpp == 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);
225   }
226
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);
236
237 //  FIXME: fix alpha check
238 //  fail_unless (a_mask == 0 || alpha != 0);      /* better than nothing */
239 }
240
241 static void
242 got_buf_cb (GstElement * sink, GstBuffer * new_buf, GstPad * pad,
243     GstBuffer ** p_old_buf)
244 {
245   gst_buffer_replace (p_old_buf, new_buf);
246 }
247
248 /* Note: lots of this code here is also in the videotestsrc.c unit test */
249 GST_START_TEST (test_rgb_to_rgb)
250 {
251   const struct
252   {
253     const gchar *pattern_name;
254     gint pattern_enum;
255     guint8 r_expected;
256     guint8 g_expected;
257     guint8 b_expected;
258   } test_patterns[] = {
259     {
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}
265   };
266   GstElement *pipeline, *src, *filter1, *csp, *filter2, *sink;
267   const GstCaps *template_caps;
268   GstBuffer *buf = NULL;
269   GstPad *srcpad;
270   GList *conversions, *l;
271   gint p;
272
273   /* test check function */
274   fail_unless (right_shift_colour (0x00ff0000, 0x11223344) == 0x22);
275
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");
282
283   gst_bin_add_many (GST_BIN (pipeline), src, filter1, csp, filter2, sink, NULL);
284
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));
289
290   srcpad = gst_element_get_pad (src, "src");
291   template_caps = gst_pad_get_pad_template_caps (srcpad);
292   gst_object_unref (srcpad);
293
294   g_object_set (sink, "signal-handoffs", TRUE, NULL);
295   g_signal_connect (sink, "preroll-handoff", G_CALLBACK (got_buf_cb), &buf);
296
297   GST_LOG ("videotestsrc src template caps: %" GST_PTR_FORMAT, template_caps);
298
299   conversions = create_rgb_conversions ();
300
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;
305
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,
309           conv->from_caps);
310       continue;
311     }
312
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;
316
317       gst_element_set_state (pipeline, GST_STATE_NULL);
318
319       g_object_set (src, "pattern", test_patterns[p].pattern_enum, NULL);
320
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);
328
329       /* now get videotestsrc to produce a buffer with the given caps */
330       g_object_set (filter1, "caps", conv->from_caps, NULL);
331
332       /* ... and force ffmpegcolorspace to convert to our target caps */
333       g_object_set (filter2, "caps", conv->to_caps, NULL);
334
335       state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
336       if (state_ret == GST_STATE_CHANGE_FAILURE) {
337         GstMessage *msg;
338         GError *err = NULL;
339
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);
349           g_error_free (err);
350           continue;
351         }
352         fail_unless (state_ret != GST_STATE_CHANGE_FAILURE,
353             "pipeline _set_state() to PAUSED failed: %s", err->message);
354       }
355
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");
359
360       state_ret = gst_element_set_state (pipeline, GST_STATE_NULL);
361       fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
362
363       fail_unless (buf != NULL);
364
365       /* check buffer caps */
366       {
367         GstStructure *s;
368         gint v;
369
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);
386         } else {
387           fail_unless (gst_structure_get_value (s, "alpha_mask") == NULL);
388         }
389       }
390
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);
396
397       gst_buffer_unref (buf);
398       buf = NULL;
399     }
400   }
401
402   g_list_foreach (conversions, (GFunc) rgb_conversion_free, NULL);
403   g_list_free (conversions);
404
405   gst_element_set_state (pipeline, GST_STATE_NULL);
406   gst_object_unref (pipeline);
407 }
408
409 GST_END_TEST;
410
411 static Suite *
412 ffmpegcolorspace_suite (void)
413 {
414   Suite *s = suite_create ("ffmpegcolorspace");
415   TCase *tc_chain = tcase_create ("general");
416
417   suite_add_tcase (s, tc_chain);
418
419 #ifdef HAVE_VALGRIND
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);
426   }
427 #endif
428
429   /* FIXME: add tests for YUV <=> YUV and YUV <=> RGB */
430   tcase_add_test (tc_chain, test_rgb_to_rgb);
431
432   return s;
433 }
434
435 GST_CHECK_MAIN (ffmpegcolorspace);