tests: videorate: remove obsolete color-matrix caps field
[platform/upstream/gstreamer.git] / tests / check / elements / ffmpegcolorspace.c
1 /* GStreamer
2  *
3  * unit test for videoconvert
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 _RGBConversion
36 {
37   const gchar *from_fmt;
38   const gchar *to_fmt;
39   GstCaps *from_caps;
40   GstCaps *to_caps;
41 } RGBConversion;
42
43 static GstCaps *
44 rgb_format_to_caps (const gchar * fmt)
45 {
46   GstCaps *caps;
47
48   g_assert (fmt != NULL);
49
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);
54
55   return caps;
56 }
57
58 static GList *
59 create_rgb_conversions (void)
60 {
61   const gchar *rgb_formats[] = {
62     "RGBA",
63     "ARGB",
64     "BGRA",
65     "ABGR",
66     "RGBx",
67     "xRGB",
68     "BGRx",
69     "xBGR",
70     "RGB",
71     "BGR",
72     "RGB15",
73     "BGR15",
74     "RGB16",
75     "BGR16"
76   };
77   GList *conversions = NULL;
78   guint from_fmt, to_fmt;
79
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) {
82       guint i;
83
84       for (i = 0; i < 4; ++i) {
85         RGBConversion *conversion;
86
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);
93       }
94     }
95   }
96
97   return g_list_reverse (conversions);
98 }
99
100 static void
101 rgb_conversion_free (RGBConversion * conv)
102 {
103   gst_caps_unref (conv->from_caps);
104   gst_caps_unref (conv->to_caps);
105   memset (conv, 0x99, sizeof (RGBConversion));
106   g_free (conv);
107 }
108
109 static guint32
110 right_shift_colour (guint32 mask, guint32 pixel)
111 {
112   if (mask == 0)
113     return 0;
114
115   pixel = pixel & mask;
116   while ((mask & 0x01) == 0) {
117     mask = mask >> 1;
118     pixel = pixel >> 1;
119   }
120
121   return pixel;
122 }
123
124 #if 0
125 static guint8
126 fix_expected_colour (guint32 col_mask, guint8 col_expected)
127 {
128   guint32 mask;
129   gint last = g_bit_nth_msf (col_mask, -1);
130   gint first = g_bit_nth_lsf (col_mask, -1);
131
132   mask = 1 << (last - first + 1);
133   mask -= 1;
134
135   g_assert (col_expected == 0x00 || col_expected == 0xff);
136
137   /* this only works because we only check for all-bits-set or no-bits-set */
138   return col_expected & mask;
139 }
140
141 static void
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)
145 {
146   guint32 pixel, red, green, blue;
147
148   switch (bpp) {
149     case 32:{
150       if (endianness == G_LITTLE_ENDIAN)
151         pixel = GST_READ_UINT32_LE (pixels);
152       else
153         pixel = GST_READ_UINT32_BE (pixels);
154       break;
155     }
156     case 24:{
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);
161       } else {
162         pixel = (GST_READ_UINT8 (pixels + 2) << 16) |
163             (GST_READ_UINT8 (pixels + 1) << 8) |
164             (GST_READ_UINT8 (pixels + 0) << 0);
165       }
166       break;
167     }
168     case 16:{
169       if (endianness == G_LITTLE_ENDIAN)
170         pixel = GST_READ_UINT16_LE (pixels);
171       else
172         pixel = GST_READ_UINT16_BE (pixels);
173       break;
174     }
175     default:
176       g_return_if_reached ();
177   }
178
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); */
183
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); */
188
189   /* fix up the mask (for rgb15/16) */
190   if (bpp == 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);
194   }
195
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);
205
206 //  FIXME: fix alpha check
207 //  fail_unless (a_mask == 0 || alpha != 0);      /* better than nothing */
208 }
209 #endif
210
211 static void
212 got_buf_cb (GstElement * sink, GstBuffer * new_buf, GstPad * pad,
213     GstBuffer ** p_old_buf)
214 {
215   gst_buffer_replace (p_old_buf, new_buf);
216 }
217
218 /* Note: lots of this code here is also in the videotestsrc.c unit test */
219 GST_START_TEST (test_rgb_to_rgb)
220 {
221   const struct
222   {
223     const gchar *pattern_name;
224     gint pattern_enum;
225     guint8 r_expected;
226     guint8 g_expected;
227     guint8 b_expected;
228   } test_patterns[] = {
229     {
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}
235   };
236   GstElement *pipeline, *src, *filter1, *csp, *filter2, *sink;
237   GstCaps *template_caps;
238   GstBuffer *buf = NULL;
239   GstPad *srcpad, *csppad;
240   GList *conversions, *l;
241   gint p;
242
243   /* test check function */
244   fail_unless (right_shift_colour (0x00ff0000, 0x11223344) == 0x22);
245
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");
252
253   gst_bin_add_many (GST_BIN (pipeline), src, filter1, csp, filter2, sink, NULL);
254
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));
259
260   srcpad = gst_element_get_static_pad (src, "src");
261   template_caps = gst_pad_get_pad_template_caps (srcpad);
262   gst_object_unref (srcpad);
263
264   csppad = gst_element_get_static_pad (csp, "src");
265
266   g_object_set (sink, "signal-handoffs", TRUE, NULL);
267   g_signal_connect (sink, "preroll-handoff", G_CALLBACK (got_buf_cb), &buf);
268
269   GST_LOG ("videotestsrc src template caps: %" GST_PTR_FORMAT, template_caps);
270
271   conversions = create_rgb_conversions ();
272
273   for (l = conversions; l != NULL; l = l->next) {
274     RGBConversion *conv = (RGBConversion *) l->data;
275
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,
279           conv->from_caps);
280       continue;
281     }
282
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;
288 #if 0
289       guint8 *data;
290       gsize size;
291 #endif
292
293       /* trick compiler into thinking from is used, might throw warning
294        * otherwise if the debugging system is disabled */
295       fail_unless (from != NULL);
296
297       gst_element_set_state (pipeline, GST_STATE_NULL);
298
299       g_object_set (src, "pattern", test_patterns[p].pattern_enum, NULL);
300
301       GST_INFO ("%5s => %5s, pattern=%s",
302           from, to, test_patterns[p].pattern_name);
303
304       /* now get videotestsrc to produce a buffer with the given caps */
305       g_object_set (filter1, "caps", conv->from_caps, NULL);
306
307       /* ... and force videoconvert to convert to our target caps */
308       g_object_set (filter2, "caps", conv->to_caps, NULL);
309
310       state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
311       if (state_ret == GST_STATE_CHANGE_FAILURE) {
312         GstMessage *msg;
313         GError *err = NULL;
314
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);
324           g_error_free (err);
325           continue;
326         }
327         fail_unless (state_ret != GST_STATE_CHANGE_FAILURE,
328             "pipeline _set_state() to PAUSED failed: %s", err->message);
329       }
330
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");
334
335       state_ret = gst_element_set_state (pipeline, GST_STATE_NULL);
336       fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
337
338       fail_unless (buf != NULL);
339
340       /* check buffer caps */
341       {
342         GstCaps *caps;
343         GstStructure *s;
344         const gchar *fmt;
345
346         g_object_get (csppad, "caps", &caps, NULL);
347
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));
353       }
354
355 #if 0
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);
363 #endif
364
365       gst_buffer_unref (buf);
366       buf = NULL;
367     }
368   }
369
370   gst_caps_unref (template_caps);
371
372   g_list_foreach (conversions, (GFunc) rgb_conversion_free, NULL);
373   g_list_free (conversions);
374
375   gst_element_set_state (pipeline, GST_STATE_NULL);
376   gst_object_unref (pipeline);
377 }
378
379 GST_END_TEST;
380
381 static Suite *
382 videoconvert_suite (void)
383 {
384   Suite *s = suite_create ("videoconvert");
385   TCase *tc_chain = tcase_create ("general");
386
387   suite_add_tcase (s, tc_chain);
388
389 #ifdef HAVE_VALGRIND
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);
396   }
397 #endif
398
399   /* FIXME: add tests for YUV <=> YUV and YUV <=> RGB */
400   tcase_add_test (tc_chain, test_rgb_to_rgb);
401
402   return s;
403 }
404
405 GST_CHECK_MAIN (videoconvert);