tests: videorate: remove obsolete color-matrix caps field
[platform/upstream/gstreamer.git] / tests / check / elements / videotestsrc.c
1 /* GStreamer
2  *
3  * unit test for videotestsrc
4  *
5  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
6  * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #ifdef HAVE_VALGRIND
29 # include <valgrind/valgrind.h>
30 #endif
31
32 #include <unistd.h>
33
34 #include <gst/check/gstcheck.h>
35
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;
40
41
42 #define CAPS_TEMPLATE_STRING            \
43     "video/x-raw, "                 \
44     "format = (string) UYVY, "          \
45     "width = (int) [ 1,  MAX ], "       \
46     "height = (int) [ 1,  MAX ], "      \
47     "framerate = (fraction) [ 0/1, MAX ]"
48
49 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
50     GST_PAD_SINK,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS (CAPS_TEMPLATE_STRING)
53     );
54
55 static GstElement *
56 setup_videotestsrc (void)
57 {
58   GstElement *videotestsrc;
59
60   GST_DEBUG ("setup_videotestsrc");
61   videotestsrc = gst_check_setup_element ("videotestsrc");
62   mysinkpad = gst_check_setup_sink_pad (videotestsrc, &sinktemplate);
63   gst_pad_set_active (mysinkpad, TRUE);
64
65   return videotestsrc;
66 }
67
68 static void
69 cleanup_videotestsrc (GstElement * videotestsrc)
70 {
71   GST_DEBUG ("cleanup_videotestsrc");
72
73   gst_check_drop_buffers ();
74
75   gst_pad_set_active (mysinkpad, FALSE);
76   gst_check_teardown_sink_pad (videotestsrc);
77   gst_check_teardown_element (videotestsrc);
78 }
79
80 GST_START_TEST (test_all_patterns)
81 {
82   GstElement *videotestsrc;
83   GObjectClass *oclass;
84   GParamSpec *property;
85   GEnumValue *values;
86   guint j = 0;
87
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;
93
94   while (values[j].value_name) {
95     GST_DEBUG_OBJECT (videotestsrc, "testing pattern %s", values[j].value_name);
96
97     g_object_set (videotestsrc, "pattern", j, NULL);
98
99     fail_unless (gst_element_set_state (videotestsrc,
100             GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
101         "could not set to playing");
102
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);
107     }
108     g_mutex_unlock (check_mutex);
109
110     gst_element_set_state (videotestsrc, GST_STATE_READY);
111
112     gst_check_drop_buffers ();
113     ++j;
114   }
115
116   /* cleanup */
117   cleanup_videotestsrc (videotestsrc);
118 }
119
120 GST_END_TEST;
121
122 static guint32
123 right_shift_colour (guint32 mask, guint32 pixel)
124 {
125   if (mask == 0)
126     return 0;
127
128   pixel = pixel & mask;
129   while ((mask & 0x01) == 0) {
130     mask = mask >> 1;
131     pixel = pixel >> 1;
132   }
133
134   return pixel;
135 }
136
137 static guint8
138 fix_expected_colour (guint32 col_mask, guint8 col_expected)
139 {
140   guint32 mask;
141   gint last = g_bit_nth_msf (col_mask, -1);
142   gint first = g_bit_nth_lsf (col_mask, -1);
143
144   mask = 1 << (last - first + 1);
145   mask -= 1;
146
147   g_assert (col_expected == 0x00 || col_expected == 0xff);
148
149   /* this only works because we only check for all-bits-set or no-bits-set */
150   return col_expected & mask;
151 }
152
153 static void
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 bpp, guint depth)
157 {
158   guint32 pixel, red, green, blue, alpha;
159
160   switch (bpp) {
161     case 32:
162       pixel = GST_READ_UINT32_BE (pixels);
163       break;
164     case 24:
165       pixel = (GST_READ_UINT8 (pixels) << 16) |
166           (GST_READ_UINT8 (pixels + 1) << 8) |
167           (GST_READ_UINT8 (pixels + 2) << 0);
168       break;
169     case 16:
170       if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
171         pixel = GST_READ_UINT16_LE (pixels);
172       else
173         pixel = GST_READ_UINT16_BE (pixels);
174       break;
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       r_expected, red);
198   fail_unless (green == g_expected, "GREEN: expected 0x%02x, found 0x%02x",
199       g_expected, green);
200   fail_unless (blue == b_expected, "BLUE: expected 0x%02x, found 0x%02x",
201       b_expected, blue);
202
203   fail_unless (a_mask == 0 || alpha != 0);      /* better than nothing */
204 }
205
206 static void
207 got_buf_cb (GstElement * sink, GstBuffer * new_buf, GstPad * pad,
208     GstSample ** p_old_sample)
209 {
210   GstCaps *caps;
211
212   caps = gst_pad_get_current_caps (pad);
213
214   if (*p_old_sample)
215     gst_sample_unref (*p_old_sample);
216   *p_old_sample = gst_sample_new (new_buf, caps, NULL, NULL);
217
218   gst_caps_unref (caps);
219 }
220
221 /* tests the positioning of pixels within the various RGB pixel layouts */
222 GST_START_TEST (test_rgb_formats)
223 {
224   const struct
225   {
226     const gchar *pattern_name;
227     gint pattern_enum;
228     guint8 r_expected;
229     guint8 g_expected;
230     guint8 b_expected;
231   } test_patterns[] = {
232     {
233     "white", 3, 0xff, 0xff, 0xff}, {
234     "red", 4, 0xff, 0x00, 0x00}, {
235     "green", 5, 0x00, 0xff, 0x00}, {
236     "blue", 6, 0x00, 0x00, 0xff}, {
237     "black", 2, 0x00, 0x00, 0x00}
238   };
239   const struct
240   {
241     const gchar *nick;
242     guint bpp, depth;
243     guint32 red_mask, green_mask, blue_mask, alpha_mask;
244   } rgb_formats[] = {
245     {
246     "RGBA", 32, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff}, {
247     "ARGB", 32, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000}, {
248     "BGRA", 32, 32, 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff}, {
249     "ABGR", 32, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000}, {
250     "RGBx", 32, 24, 0xff000000, 0x00ff0000, 0x0000ff00, 0x00000000}, {
251     "xRGB", 32, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}, {
252     "BGRx", 32, 24, 0x0000ff00, 0x00ff0000, 0xff000000, 0x00000000}, {
253     "xBGR", 32, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}, {
254     "RGB", 24, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}, {
255     "BGR", 24, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}, {
256     "RGB16", 16, 16, 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000}, {
257     "RGB15", 16, 15, 0x00007c00, 0x000003e0, 0x0000001f, 0x0000000}
258   };
259   GstElement *pipeline, *src, *filter, *sink;
260   GstCaps *template_caps;
261   GstSample *sample = NULL;
262   GstPad *srcpad;
263   gint p, i, e;
264
265   /* test check function */
266   fail_unless (right_shift_colour (0x00ff0000, 0x11223344) == 0x22);
267
268   pipeline = gst_pipeline_new ("pipeline");
269   src = gst_check_setup_element ("videotestsrc");
270   filter = gst_check_setup_element ("capsfilter");
271   sink = gst_check_setup_element ("fakesink");
272
273   gst_bin_add_many (GST_BIN (pipeline), src, filter, sink, NULL);
274
275   fail_unless (gst_element_link (src, filter));
276   fail_unless (gst_element_link (filter, sink));
277
278   srcpad = gst_element_get_static_pad (src, "src");
279   template_caps = gst_pad_get_pad_template_caps (srcpad);
280
281   g_object_set (sink, "signal-handoffs", TRUE, NULL);
282   g_signal_connect (sink, "preroll-handoff", G_CALLBACK (got_buf_cb), &sample);
283
284   GST_LOG ("videotestsrc src template caps: %" GST_PTR_FORMAT, template_caps);
285
286   for (i = 0; i < G_N_ELEMENTS (rgb_formats); ++i) {
287     for (e = 0; e < 2; ++e) {
288       GstCaps *caps;
289
290       caps = gst_caps_new_simple ("video/x-raw",
291           "format", G_TYPE_STRING, rgb_formats[i].nick,
292           "width", G_TYPE_INT, 16, "height", G_TYPE_INT, 16,
293           "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
294
295       if (gst_caps_is_subset (caps, template_caps)) {
296         /* caps are supported, let's run some tests then ... */
297         for (p = 0; p < G_N_ELEMENTS (test_patterns); ++p) {
298           GstStateChangeReturn state_ret;
299           GstMapInfo map;
300
301           g_object_set (src, "pattern", test_patterns[p].pattern_enum, NULL);
302
303           GST_INFO ("%5s %u/%u %08x %08x %08x %08x, pattern=%s",
304               rgb_formats[i].nick, rgb_formats[i].bpp, rgb_formats[i].depth,
305               rgb_formats[i].red_mask, rgb_formats[i].green_mask,
306               rgb_formats[i].blue_mask, rgb_formats[i].alpha_mask,
307               test_patterns[p].pattern_name);
308
309           /* now get videotestsrc to produce a buffer with the given caps */
310           g_object_set (filter, "caps", caps, NULL);
311
312           state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
313           fail_unless (state_ret != GST_STATE_CHANGE_FAILURE,
314               "pipeline _set_state() to PAUSED failed");
315           state_ret = gst_element_get_state (pipeline, NULL, NULL, -1);
316           fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS,
317               "pipeline failed going to PAUSED state");
318
319           state_ret = gst_element_set_state (pipeline, GST_STATE_NULL);
320           fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
321
322           fail_unless (sample != NULL);
323
324           /* check buffer caps */
325           {
326             GstBuffer *buf;
327             GstStructure *s;
328             GstCaps *caps;
329             const gchar *format;
330
331             buf = gst_sample_get_buffer (sample);
332             fail_unless (buf != NULL);
333             caps = gst_sample_get_caps (sample);
334             fail_unless (caps != NULL);
335
336             s = gst_caps_get_structure (caps, 0);
337             format = gst_structure_get_string (s, "format");
338             fail_unless (g_str_equal (format, rgb_formats[i].nick));
339
340             /* now check the first pixel */
341             gst_buffer_map (buf, &map, GST_MAP_READ);
342             check_rgb_buf (map.data, rgb_formats[i].red_mask,
343                 rgb_formats[i].green_mask, rgb_formats[i].blue_mask,
344                 rgb_formats[i].alpha_mask, test_patterns[p].r_expected,
345                 test_patterns[p].g_expected, test_patterns[p].b_expected,
346                 rgb_formats[i].bpp, rgb_formats[i].depth);
347             gst_buffer_unmap (buf, &map);
348
349             gst_sample_unref (sample);
350             sample = NULL;
351           }
352         }
353
354       } else {
355         GST_INFO ("videotestsrc doesn't support format %" GST_PTR_FORMAT, caps);
356       }
357
358       gst_caps_unref (caps);
359     }
360   }
361   gst_caps_unref (template_caps);
362   gst_object_unref (srcpad);
363
364   gst_object_unref (pipeline);
365 }
366
367 GST_END_TEST;
368
369
370 /* FIXME: add tests for YUV formats */
371
372 static Suite *
373 videotestsrc_suite (void)
374 {
375   Suite *s = suite_create ("videotestsrc");
376   TCase *tc_chain = tcase_create ("general");
377
378   suite_add_tcase (s, tc_chain);
379
380 #ifdef HAVE_VALGRIND
381   if (RUNNING_ON_VALGRIND) {
382     /* otherwise valgrind errors out when liboil probes CPU extensions
383      * during which it causes SIGILLs etc. to be fired */
384     g_setenv ("OIL_CPU_FLAGS", "0", 0);
385     /* test_rgb_formats takes a bit longer, so increase timeout */
386     tcase_set_timeout (tc_chain, 5 * 60);
387   }
388 #endif
389
390   tcase_add_test (tc_chain, test_all_patterns);
391   tcase_add_test (tc_chain, test_rgb_formats);
392
393   return s;
394 }
395
396 GST_CHECK_MAIN (videotestsrc);