gst_plugins_base/tests/check/elements/videotestsrc.c
changeset 0 0e761a78d257
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     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) Y422, "          \
       
    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   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
       
    74   g_list_free (buffers);
       
    75   buffers = NULL;
       
    76 
       
    77   gst_pad_set_active (mysinkpad, FALSE);
       
    78   gst_check_teardown_sink_pad (videotestsrc);
       
    79   gst_check_teardown_element (videotestsrc);
       
    80 }
       
    81 
       
    82 GST_START_TEST (test_all_patterns)
       
    83 {
       
    84   GstElement *videotestsrc;
       
    85   GObjectClass *oclass;
       
    86   GParamSpec *property;
       
    87   GEnumValue *values;
       
    88   guint j = 0;
       
    89 
       
    90   videotestsrc = setup_videotestsrc ();
       
    91   oclass = G_OBJECT_GET_CLASS (videotestsrc);
       
    92   property = g_object_class_find_property (oclass, "pattern");
       
    93   fail_unless (G_IS_PARAM_SPEC_ENUM (property));
       
    94   values = G_ENUM_CLASS (g_type_class_ref (property->value_type))->values;
       
    95 
       
    96 
       
    97   while (values[j].value_name) {
       
    98     GST_DEBUG_OBJECT (videotestsrc, "testing pattern %s", values[j].value_name);
       
    99 
       
   100     fail_unless (gst_element_set_state (videotestsrc,
       
   101             GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
       
   102         "could not set to playing");
       
   103 
       
   104     g_mutex_lock (check_mutex);
       
   105     while (g_list_length (buffers) < 10) {
       
   106       GST_DEBUG_OBJECT (videotestsrc, "Waiting for more buffers");
       
   107       g_cond_wait (check_cond, check_mutex);
       
   108     }
       
   109     g_mutex_unlock (check_mutex);
       
   110 
       
   111 
       
   112     gst_element_set_state (videotestsrc, GST_STATE_READY);
       
   113 
       
   114     g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
       
   115     g_list_free (buffers);
       
   116     buffers = NULL;
       
   117     ++j;
       
   118   }
       
   119 
       
   120   /* cleanup */
       
   121   cleanup_videotestsrc (videotestsrc);
       
   122 }
       
   123 
       
   124 GST_END_TEST;
       
   125 
       
   126 static guint32
       
   127 right_shift_colour (guint32 mask, guint32 pixel)
       
   128 {
       
   129   if (mask == 0)
       
   130     return 0;
       
   131 
       
   132   pixel = pixel & mask;
       
   133   while ((mask & 0x01) == 0) {
       
   134     mask = mask >> 1;
       
   135     pixel = pixel >> 1;
       
   136   }
       
   137 
       
   138   return pixel;
       
   139 }
       
   140 
       
   141 static guint8
       
   142 fix_expected_colour (guint32 col_mask, guint8 col_expected)
       
   143 {
       
   144   guint32 mask;
       
   145   gint last = g_bit_nth_msf (col_mask, -1);
       
   146   gint first = g_bit_nth_lsf (col_mask, -1);
       
   147 
       
   148   mask = 1 << (last - first + 1);
       
   149   mask -= 1;
       
   150 
       
   151   g_assert (col_expected == 0x00 || col_expected == 0xff);
       
   152 
       
   153   /* this only works because we only check for all-bits-set or no-bits-set */
       
   154   return col_expected & mask;
       
   155 }
       
   156 
       
   157 static void
       
   158 check_rgb_buf (const guint8 * pixels, guint32 r_mask, guint32 g_mask,
       
   159     guint32 b_mask, guint32 a_mask, guint8 r_expected, guint8 g_expected,
       
   160     guint8 b_expected, guint endianness, guint bpp, guint depth)
       
   161 {
       
   162   guint32 pixel, red, green, blue, alpha;
       
   163 
       
   164   switch (bpp) {
       
   165     case 32:{
       
   166       if (endianness == G_LITTLE_ENDIAN)
       
   167         pixel = GST_READ_UINT32_LE (pixels);
       
   168       else
       
   169         pixel = GST_READ_UINT32_BE (pixels);
       
   170       break;
       
   171     }
       
   172     case 24:{
       
   173       if (endianness == G_BIG_ENDIAN) {
       
   174         pixel = (GST_READ_UINT8 (pixels) << 16) |
       
   175             (GST_READ_UINT8 (pixels + 1) << 8) |
       
   176             (GST_READ_UINT8 (pixels + 2) << 0);
       
   177       } else {
       
   178         pixel = (GST_READ_UINT8 (pixels + 2) << 16) |
       
   179             (GST_READ_UINT8 (pixels + 1) << 8) |
       
   180             (GST_READ_UINT8 (pixels + 0) << 0);
       
   181       }
       
   182       break;
       
   183     }
       
   184     case 16:{
       
   185       if (endianness == G_LITTLE_ENDIAN)
       
   186         pixel = GST_READ_UINT16_LE (pixels);
       
   187       else
       
   188         pixel = GST_READ_UINT16_BE (pixels);
       
   189       break;
       
   190     }
       
   191     default:
       
   192       g_return_if_reached ();
       
   193   }
       
   194 
       
   195   red = right_shift_colour (r_mask, pixel);
       
   196   green = right_shift_colour (g_mask, pixel);
       
   197   blue = right_shift_colour (b_mask, pixel);
       
   198   alpha = right_shift_colour (a_mask, pixel);
       
   199 
       
   200   /* can't enable this by default, valgrind will complain about accessing
       
   201    * uninitialised memory for the depth=24,bpp=32 formats ... */
       
   202   /* GST_LOG ("pixels: 0x%02x 0x%02x 0x%02x 0x%02x => pixel = 0x%08x",
       
   203      pixels[0], (guint) pixels[1], pixels[2], pixels[3], pixel); */
       
   204 
       
   205   /* fix up the mask (for rgb15/16) */
       
   206   if (bpp == 16) {
       
   207     r_expected = fix_expected_colour (r_mask, r_expected);
       
   208     g_expected = fix_expected_colour (g_mask, g_expected);
       
   209     b_expected = fix_expected_colour (b_mask, b_expected);
       
   210   }
       
   211 
       
   212   fail_unless (red == r_expected, "RED: expected 0x%02x, found 0x%02x",
       
   213       r_expected, red);
       
   214   fail_unless (green == g_expected, "GREEN: expected 0x%02x, found 0x%02x",
       
   215       g_expected, green);
       
   216   fail_unless (blue == b_expected, "BLUE: expected 0x%02x, found 0x%02x",
       
   217       b_expected, blue);
       
   218 
       
   219   fail_unless (a_mask == 0 || alpha != 0);      /* better than nothing */
       
   220 }
       
   221 
       
   222 static void
       
   223 got_buf_cb (GstElement * sink, GstBuffer * new_buf, GstPad * pad,
       
   224     GstBuffer ** p_old_buf)
       
   225 {
       
   226   gst_buffer_replace (p_old_buf, new_buf);
       
   227 }
       
   228 
       
   229 /* tests the positioning of pixels within the various RGB pixel layouts */
       
   230 GST_START_TEST (test_rgb_formats)
       
   231 {
       
   232   const struct
       
   233   {
       
   234     const gchar *pattern_name;
       
   235     gint pattern_enum;
       
   236     guint8 r_expected;
       
   237     guint8 g_expected;
       
   238     guint8 b_expected;
       
   239   } test_patterns[] = {
       
   240     {
       
   241     "white", 3, 0xff, 0xff, 0xff}, {
       
   242     "red", 4, 0xff, 0x00, 0x00}, {
       
   243     "green", 5, 0x00, 0xff, 0x00}, {
       
   244     "blue", 6, 0x00, 0x00, 0xff}, {
       
   245     "black", 2, 0x00, 0x00, 0x00}
       
   246   };
       
   247   const struct
       
   248   {
       
   249     const gchar *nick;
       
   250     guint bpp, depth;
       
   251     guint32 red_mask, green_mask, blue_mask, alpha_mask;
       
   252   } rgb_formats[] = {
       
   253     {
       
   254     "RGBA", 32, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff}, {
       
   255     "ARGB", 32, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000}, {
       
   256     "BGRA", 32, 32, 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff}, {
       
   257     "ABGR", 32, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000}, {
       
   258     "RGBx", 32, 24, 0xff000000, 0x00ff0000, 0x0000ff00, 0x00000000}, {
       
   259     "xRGB", 32, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}, {
       
   260     "BGRx", 32, 24, 0x0000ff00, 0x00ff0000, 0xff000000, 0x00000000}, {
       
   261     "xBGR", 32, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}, {
       
   262     "RGB ", 24, 24, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000}, {
       
   263     "BGR ", 24, 24, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000}, {
       
   264     "RGB565", 16, 16, 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000}, {
       
   265     "xRGB1555", 16, 15, 0x00007c00, 0x000003e0, 0x0000001f, 0x0000000}
       
   266   };
       
   267   GstElement *pipeline, *src, *filter, *sink;
       
   268   const GstCaps *template_caps;
       
   269   GstBuffer *buf = NULL;
       
   270   GstPad *srcpad;
       
   271   gint p, i, e;
       
   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   filter = gst_check_setup_element ("capsfilter");
       
   279   sink = gst_check_setup_element ("fakesink");
       
   280 
       
   281   gst_bin_add_many (GST_BIN (pipeline), src, filter, sink, NULL);
       
   282 
       
   283   fail_unless (gst_element_link (src, filter));
       
   284   fail_unless (gst_element_link (filter, sink));
       
   285 
       
   286   srcpad = gst_element_get_pad (src, "src");
       
   287   template_caps = gst_pad_get_pad_template_caps (srcpad);
       
   288   gst_object_unref (srcpad);
       
   289 
       
   290   g_object_set (sink, "signal-handoffs", TRUE, NULL);
       
   291   g_signal_connect (sink, "preroll-handoff", G_CALLBACK (got_buf_cb), &buf);
       
   292 
       
   293   GST_LOG ("videotestsrc src template caps: %" GST_PTR_FORMAT, template_caps);
       
   294 
       
   295   for (i = 0; i < G_N_ELEMENTS (rgb_formats); ++i) {
       
   296     for (e = 0; e < 2; ++e) {
       
   297       guint endianness;
       
   298       GstCaps *caps;
       
   299 
       
   300       if (e == 0) {
       
   301         endianness = G_BYTE_ORDER;
       
   302       } else {
       
   303         endianness =
       
   304             (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_LITTLE_ENDIAN : G_BIG_ENDIAN;
       
   305       }
       
   306 
       
   307       caps = gst_caps_new_simple ("video/x-raw-rgb",
       
   308           "bpp", G_TYPE_INT, rgb_formats[i].bpp,
       
   309           "depth", G_TYPE_INT, rgb_formats[i].depth,
       
   310           "red_mask", G_TYPE_INT, rgb_formats[i].red_mask,
       
   311           "green_mask", G_TYPE_INT, rgb_formats[i].green_mask,
       
   312           "blue_mask", G_TYPE_INT, rgb_formats[i].blue_mask,
       
   313           "width", G_TYPE_INT, 16, "height", G_TYPE_INT, 16,
       
   314           "endianness", G_TYPE_INT, endianness,
       
   315           "framerate", GST_TYPE_FRACTION, 1, 1, NULL);
       
   316 
       
   317       fail_unless (rgb_formats[i].alpha_mask == 0 || rgb_formats[i].bpp == 32);
       
   318 
       
   319       if (rgb_formats[i].alpha_mask != 0) {
       
   320         gst_structure_set (gst_caps_get_structure (caps, 0),
       
   321             "alpha_mask", G_TYPE_INT, rgb_formats[i].alpha_mask, NULL);
       
   322       }
       
   323 
       
   324       if (gst_caps_is_subset (caps, template_caps)) {
       
   325 
       
   326         /* caps are supported, let's run some tests then ... */
       
   327         for (p = 0; p < G_N_ELEMENTS (test_patterns); ++p) {
       
   328           GstStateChangeReturn state_ret;
       
   329 
       
   330           g_object_set (src, "pattern", test_patterns[p].pattern_enum, NULL);
       
   331 
       
   332           GST_INFO ("%5s %u/%u %08x %08x %08x %08x %u, pattern=%s",
       
   333               rgb_formats[i].nick, rgb_formats[i].bpp, rgb_formats[i].depth,
       
   334               rgb_formats[i].red_mask, rgb_formats[i].green_mask,
       
   335               rgb_formats[i].blue_mask, rgb_formats[i].alpha_mask, endianness,
       
   336               test_patterns[p].pattern_name);
       
   337 
       
   338           /* now get videotestsrc to produce a buffer with the given caps */
       
   339           g_object_set (filter, "caps", caps, NULL);
       
   340 
       
   341           state_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
       
   342           fail_unless (state_ret != GST_STATE_CHANGE_FAILURE,
       
   343               "pipeline _set_state() to PAUSED failed");
       
   344           state_ret = gst_element_get_state (pipeline, NULL, NULL, -1);
       
   345           fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS,
       
   346               "pipeline failed going to PAUSED state");
       
   347 
       
   348           state_ret = gst_element_set_state (pipeline, GST_STATE_NULL);
       
   349           fail_unless (state_ret == GST_STATE_CHANGE_SUCCESS);
       
   350 
       
   351           fail_unless (buf != NULL);
       
   352 
       
   353           /* check buffer caps */
       
   354           {
       
   355             GstStructure *s;
       
   356             gint v;
       
   357 
       
   358             fail_unless (GST_BUFFER_CAPS (buf) != NULL);
       
   359 
       
   360             s = gst_caps_get_structure (GST_BUFFER_CAPS (buf), 0);
       
   361             fail_unless (gst_structure_get_int (s, "bpp", &v));
       
   362             fail_unless_equals_int (v, rgb_formats[i].bpp);
       
   363             fail_unless (gst_structure_get_int (s, "depth", &v));
       
   364             fail_unless_equals_int (v, rgb_formats[i].depth);
       
   365             fail_unless (gst_structure_get_int (s, "red_mask", &v));
       
   366             fail_unless_equals_int (v, rgb_formats[i].red_mask);
       
   367             fail_unless (gst_structure_get_int (s, "green_mask", &v));
       
   368             fail_unless_equals_int (v, rgb_formats[i].green_mask);
       
   369             fail_unless (gst_structure_get_int (s, "blue_mask", &v));
       
   370             fail_unless_equals_int (v, rgb_formats[i].blue_mask);
       
   371             /* there mustn't be an alpha_mask if there's no alpha component */
       
   372             if (rgb_formats[i].depth == 32) {
       
   373               fail_unless (gst_structure_get_int (s, "alpha_mask", &v));
       
   374               fail_unless_equals_int (v, rgb_formats[i].alpha_mask);
       
   375             } else {
       
   376               fail_unless (gst_structure_get_value (s, "alpha_mask") == NULL);
       
   377             }
       
   378           }
       
   379 
       
   380 
       
   381           /* now check the first pixel */
       
   382           check_rgb_buf (GST_BUFFER_DATA (buf), rgb_formats[i].red_mask,
       
   383               rgb_formats[i].green_mask, rgb_formats[i].blue_mask,
       
   384               rgb_formats[i].alpha_mask, test_patterns[p].r_expected,
       
   385               test_patterns[p].g_expected, test_patterns[p].b_expected,
       
   386               endianness, rgb_formats[i].bpp, rgb_formats[i].depth);
       
   387 
       
   388           gst_buffer_unref (buf);
       
   389           buf = NULL;
       
   390         }
       
   391 
       
   392       } else {
       
   393         GST_INFO ("videotestsrc doesn't support format %" GST_PTR_FORMAT, caps);
       
   394       }
       
   395 
       
   396       gst_caps_unref (caps);
       
   397     }
       
   398   }
       
   399 
       
   400   gst_object_unref (pipeline);
       
   401 }
       
   402 
       
   403 GST_END_TEST;
       
   404 
       
   405 
       
   406 /* FIXME: add tests for YUV formats */
       
   407 
       
   408 static Suite *
       
   409 videotestsrc_suite (void)
       
   410 {
       
   411   Suite *s = suite_create ("videotestsrc");
       
   412   TCase *tc_chain = tcase_create ("general");
       
   413 
       
   414   suite_add_tcase (s, tc_chain);
       
   415 
       
   416 #ifdef HAVE_VALGRIND
       
   417   if (RUNNING_ON_VALGRIND) {
       
   418     /* otherwise valgrind errors out when liboil probes CPU extensions
       
   419      * during which it causes SIGILLs etc. to be fired */
       
   420     g_setenv ("OIL_CPU_FLAGS", "0", 0);
       
   421     /* test_rgb_formats takes a bit longer, so increase timeout */
       
   422     tcase_set_timeout (tc_chain, 5 * 60);
       
   423   }
       
   424 #endif
       
   425 
       
   426   tcase_add_test (tc_chain, test_all_patterns);
       
   427   tcase_add_test (tc_chain, test_rgb_formats);
       
   428 
       
   429   return s;
       
   430 }
       
   431 
       
   432 int
       
   433 main (int argc, char **argv)
       
   434 {
       
   435   int nf;
       
   436 
       
   437   Suite *s = videotestsrc_suite ();
       
   438   SRunner *sr = srunner_create (s);
       
   439 
       
   440   gst_check_init (&argc, &argv);
       
   441 
       
   442   srunner_run_all (sr, CK_NORMAL);
       
   443   nf = srunner_ntests_failed (sr);
       
   444   srunner_free (sr);
       
   445 
       
   446   return nf;
       
   447 }