635ca32d8c0d7b7b520b6b8da07482ebd1bf41c0
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / 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-yuv, "                 \
44     "format = (fourcc) 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, NULL);
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 endianness, guint bpp, guint depth)
157 {
158   guint32 pixel, red, green, blue, alpha;
159
160   switch (bpp) {
161     case 32:{
162       if (endianness == G_LITTLE_ENDIAN)
163         pixel = GST_READ_UINT32_LE (pixels);
164       else
165         pixel = GST_READ_UINT32_BE (pixels);
166       break;
167     }
168     case 24:{
169       if (endianness == G_BIG_ENDIAN) {
170         pixel = (GST_READ_UINT8 (pixels) << 16) |
171             (GST_READ_UINT8 (pixels + 1) << 8) |
172             (GST_READ_UINT8 (pixels + 2) << 0);
173       } else {
174         pixel = (GST_READ_UINT8 (pixels + 2) << 16) |
175             (GST_READ_UINT8 (pixels + 1) << 8) |
176             (GST_READ_UINT8 (pixels + 0) << 0);
177       }
178       break;
179     }
180     case 16:{
181       if (endianness == G_LITTLE_ENDIAN)
182         pixel = GST_READ_UINT16_LE (pixels);
183       else
184         pixel = GST_READ_UINT16_BE (pixels);
185       break;
186     }
187     default:
188       g_return_if_reached ();
189   }
190
191   red = right_shift_colour (r_mask, pixel);
192   green = right_shift_colour (g_mask, pixel);
193   blue = right_shift_colour (b_mask, pixel);
194   alpha = right_shift_colour (a_mask, pixel);
195
196   /* can't enable this by default, valgrind will complain about accessing
197    * uninitialised memory for the depth=24,bpp=32 formats ... */
198   /* GST_LOG ("pixels: 0x%02x 0x%02x 0x%02x 0x%02x => pixel = 0x%08x",
199      pixels[0], (guint) pixels[1], pixels[2], pixels[3], pixel); */
200
201   /* fix up the mask (for rgb15/16) */
202   if (bpp == 16) {
203     r_expected = fix_expected_colour (r_mask, r_expected);
204     g_expected = fix_expected_colour (g_mask, g_expected);
205     b_expected = fix_expected_colour (b_mask, b_expected);
206   }
207
208   fail_unless (red == r_expected, "RED: expected 0x%02x, found 0x%02x",
209       r_expected, red);
210   fail_unless (green == g_expected, "GREEN: expected 0x%02x, found 0x%02x",
211       g_expected, green);
212   fail_unless (blue == b_expected, "BLUE: expected 0x%02x, found 0x%02x",
213       b_expected, blue);
214
215   fail_unless (a_mask == 0 || alpha != 0);      /* better than nothing */
216 }
217
218 static void
219 got_buf_cb (GstElement * sink, GstBuffer * new_buf, GstPad * pad,
220     GstBuffer ** p_old_buf)
221 {
222   gst_buffer_replace (p_old_buf, new_buf);
223 }
224
225 /* tests the positioning of pixels within the various RGB pixel layouts */
226 GST_START_TEST (test_rgb_formats)
227 {
228   const struct
229   {
230     const gchar *pattern_name;
231     gint pattern_enum;
232     guint8 r_expected;
233     guint8 g_expected;
234     guint8 b_expected;
235   } test_patterns[] = {
236     {
237     "white", 3, 0xff, 0xff, 0xff}, {
238     "red", 4, 0xff, 0x00, 0x00}, {
239     "green", 5, 0x00, 0xff, 0x00}, {
240     "blue", 6, 0x00, 0x00, 0xff}, {
241     "black", 2, 0x00, 0x00, 0x00}
242   };
243   const struct
244   {
245     const gchar *nick;
246     guint bpp, depth;
247     guint32 red_mask, green_mask, blue_mask, alpha_mask;
248   } rgb_formats[] = {
249     {
250     "RGBA", 32, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff}, {
251     "ARGB", 32, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000}, {
252     "BGRA", 32, 32, 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff}, {
253     "ABGR", 32, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000}, {
254     "RGBx", 32, 24, 0xff000000, 0x00ff0000, 0x0000ff00, 0x00000000}, {
255     "xRGB", 32, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}, {
256     "BGRx", 32, 24, 0x0000ff00, 0x00ff0000, 0xff000000, 0x00000000}, {
257     "xBGR", 32, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}, {
258     "RGB ", 24, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}, {
259     "BGR ", 24, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}, {
260     "RGB565", 16, 16, 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000}, {
261     "xRGB1555", 16, 15, 0x00007c00, 0x000003e0, 0x0000001f, 0x0000000}
262   };
263   GstElement *pipeline, *src, *filter, *sink;
264   const GstCaps *template_caps;
265   GstBuffer *buf = NULL;
266   GstPad *srcpad;
267   gint p, i, e;
268
269   /* test check function */
270   fail_unless (right_shift_colour (0x00ff0000, 0x11223344) == 0x22);
271
272   pipeline = gst_pipeline_new ("pipeline");
273   src = gst_check_setup_element ("videotestsrc");
274   filter = gst_check_setup_element ("capsfilter");
275   sink = gst_check_setup_element ("fakesink");
276
277   gst_bin_add_many (GST_BIN (pipeline), src, filter, sink, NULL);
278
279   fail_unless (gst_element_link (src, filter));
280   fail_unless (gst_element_link (filter, sink));
281
282   srcpad = gst_element_get_static_pad (src, "src");
283   template_caps = gst_pad_get_pad_template_caps (srcpad);
284   gst_object_unref (srcpad);
285
286   g_object_set (sink, "signal-handoffs", TRUE, NULL);
287   g_signal_connect (sink, "preroll-handoff", G_CALLBACK (got_buf_cb), &buf);
288
289   GST_LOG ("videotestsrc src template caps: %" GST_PTR_FORMAT, template_caps);
290
291   for (i = 0; i < G_N_ELEMENTS (rgb_formats); ++i) {
292     for (e = 0; e < 2; ++e) {
293       guint endianness;
294       GstCaps *caps;
295
296       if (e == 0) {
297         endianness = G_BYTE_ORDER;
298       } else {
299         endianness =
300             (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_LITTLE_ENDIAN : G_BIG_ENDIAN;
301       }
302
303       caps = gst_caps_new_simple ("video/x-raw-rgb",
304           "bpp", G_TYPE_INT, rgb_formats[i].bpp,
305           "depth", G_TYPE_INT, rgb_formats[i].depth,
306           "red_mask", G_TYPE_INT, rgb_formats[i].red_mask,
307           "green_mask", G_TYPE_INT, rgb_formats[i].green_mask,
308           "blue_mask", G_TYPE_INT, rgb_formats[i].blue_mask,
309           "width", G_TYPE_INT, 16, "height", G_TYPE_INT, 16,
310           "endianness", G_TYPE_INT, endianness,
311           "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
312
313       fail_unless (rgb_formats[i].alpha_mask == 0 || rgb_formats[i].bpp == 32);
314
315       if (rgb_formats[i].alpha_mask != 0) {
316         gst_structure_set (gst_caps_get_structure (caps, 0),
317             "alpha_mask", G_TYPE_INT, rgb_formats[i].alpha_mask, NULL);
318       }
319
320       if (gst_caps_is_subset (caps, template_caps)) {
321
322         /* caps are supported, let's run some tests then ... */
323         for (p = 0; p < G_N_ELEMENTS (test_patterns); ++p) {
324           GstStateChangeReturn state_ret;
325
326           g_object_set (src, "pattern", test_patterns[p].pattern_enum, NULL);
327
328           GST_INFO ("%5s %u/%u %08x %08x %08x %08x %u, pattern=%s",
329               rgb_formats[i].nick, rgb_formats[i].bpp, rgb_formats[i].depth,
330               rgb_formats[i].red_mask, rgb_formats[i].green_mask,
331               rgb_formats[i].blue_mask, rgb_formats[i].alpha_mask, endianness,
332               test_patterns[p].pattern_name);
333
334           /* now get videotestsrc to produce a buffer with the given caps */
335           g_object_set (filter, "caps", caps, NULL);
336
337           state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
338           fail_unless (state_ret != GST_STATE_CHANGE_FAILURE,
339               "pipeline _set_state() to PAUSED failed");
340           state_ret = gst_element_get_state (pipeline, NULL, NULL, -1);
341           fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS,
342               "pipeline failed going to PAUSED state");
343
344           state_ret = gst_element_set_state (pipeline, GST_STATE_NULL);
345           fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
346
347           fail_unless (buf != NULL);
348
349           /* check buffer caps */
350           {
351             GstStructure *s;
352             gint v;
353
354             fail_unless (GST_BUFFER_CAPS (buf) != NULL);
355
356             s = gst_caps_get_structure (GST_BUFFER_CAPS (buf), 0);
357             fail_unless (gst_structure_get_int (s, "bpp", &v));
358             fail_unless_equals_int (v, rgb_formats[i].bpp);
359             fail_unless (gst_structure_get_int (s, "depth", &v));
360             fail_unless_equals_int (v, rgb_formats[i].depth);
361             fail_unless (gst_structure_get_int (s, "red_mask", &v));
362             fail_unless_equals_int (v, rgb_formats[i].red_mask);
363             fail_unless (gst_structure_get_int (s, "green_mask", &v));
364             fail_unless_equals_int (v, rgb_formats[i].green_mask);
365             fail_unless (gst_structure_get_int (s, "blue_mask", &v));
366             fail_unless_equals_int (v, rgb_formats[i].blue_mask);
367             /* there mustn't be an alpha_mask if there's no alpha component */
368             if (rgb_formats[i].depth == 32) {
369               fail_unless (gst_structure_get_int (s, "alpha_mask", &v));
370               fail_unless_equals_int (v, rgb_formats[i].alpha_mask);
371             } else {
372               fail_unless (gst_structure_get_value (s, "alpha_mask") == NULL);
373             }
374           }
375
376
377           /* now check the first pixel */
378           check_rgb_buf (GST_BUFFER_DATA (buf), rgb_formats[i].red_mask,
379               rgb_formats[i].green_mask, rgb_formats[i].blue_mask,
380               rgb_formats[i].alpha_mask, test_patterns[p].r_expected,
381               test_patterns[p].g_expected, test_patterns[p].b_expected,
382               endianness, rgb_formats[i].bpp, rgb_formats[i].depth);
383
384           gst_buffer_unref (buf);
385           buf = NULL;
386         }
387
388       } else {
389         GST_INFO ("videotestsrc doesn't support format %" GST_PTR_FORMAT, caps);
390       }
391
392       gst_caps_unref (caps);
393     }
394   }
395
396   gst_object_unref (pipeline);
397 }
398
399 GST_END_TEST;
400
401
402 /* FIXME: add tests for YUV formats */
403
404 static Suite *
405 videotestsrc_suite (void)
406 {
407   Suite *s = suite_create ("videotestsrc");
408   TCase *tc_chain = tcase_create ("general");
409
410   suite_add_tcase (s, tc_chain);
411
412 #ifdef HAVE_VALGRIND
413   if (RUNNING_ON_VALGRIND) {
414     /* otherwise valgrind errors out when liboil probes CPU extensions
415      * during which it causes SIGILLs etc. to be fired */
416     g_setenv ("OIL_CPU_FLAGS", "0", 0);
417     /* test_rgb_formats takes a bit longer, so increase timeout */
418     tcase_set_timeout (tc_chain, 5 * 60);
419   }
420 #endif
421
422   tcase_add_test (tc_chain, test_all_patterns);
423   tcase_add_test (tc_chain, test_rgb_formats);
424
425   return s;
426 }
427
428 GST_CHECK_MAIN (videotestsrc);