tests: Add CUDA filter unit tests
[platform/upstream/gstreamer.git] / tests / check / elements / cudafilter.c
1 /* GStreamer
2  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <gst/check/gstcheck.h>
21 #include <gst/check/gstharness.h>
22 #include <gst/video/video.h>
23
24
25 static void
26 test_buffer_meta_common (const gchar * in_caps, const gchar * out_caps,
27     const gchar * pipeline)
28 {
29   GstHarness *h;
30   GstElement *capsfilter;
31   GstCaps *caps, *srccaps;
32   GstBuffer *in_buf, *out_buf = NULL;
33   GstFlowReturn ret;
34   GstVideoInfo info;
35   GstVideoTimeCodeMeta *meta = NULL;
36
37   h = gst_harness_new_parse (pipeline);
38   fail_unless (h != NULL);
39
40   capsfilter = gst_harness_find_element (h, "capsfilter");
41
42   gst_harness_play (h);
43
44   srccaps = gst_caps_from_string (in_caps);
45   fail_unless (srccaps != NULL);
46   fail_unless (gst_video_info_from_caps (&info, srccaps));
47
48   gst_harness_set_src_caps (h, srccaps);
49
50   /* enforce cuda memory */
51   caps = gst_caps_from_string (out_caps);
52   g_object_set (capsfilter, "caps", caps, NULL);
53   gst_caps_unref (caps);
54   gst_object_unref (capsfilter);
55
56   in_buf = gst_buffer_new_and_alloc (GST_VIDEO_INFO_SIZE (&info));
57   gst_buffer_memset (in_buf, 0, 0, GST_VIDEO_INFO_SIZE (&info));
58
59   GST_BUFFER_DURATION (in_buf) = GST_SECOND;
60   GST_BUFFER_PTS (in_buf) = 0;
61   GST_BUFFER_DTS (in_buf) = GST_CLOCK_TIME_NONE;
62
63   gst_buffer_add_video_time_code_meta_full (in_buf, 30, 1,
64       NULL, GST_VIDEO_TIME_CODE_FLAGS_NONE, 0, 0, 1, 1, 0);
65
66   ret = gst_harness_push (h, gst_buffer_ref (in_buf));
67   fail_unless (ret == GST_FLOW_OK, "GstFlowReturn was %s",
68       gst_flow_get_name (ret));
69
70   out_buf = gst_harness_try_pull (h);
71   fail_unless (out_buf != NULL, "No output buffer");
72
73   meta = gst_buffer_get_video_time_code_meta (out_buf);
74   fail_unless (meta != NULL, "output buffer has no meta");
75   fail_unless_equals_int (meta->tc.config.fps_n, 30);
76   fail_unless_equals_int (meta->tc.config.fps_d, 1);
77   fail_unless_equals_int (meta->tc.seconds, 1);
78
79   gst_buffer_unref (in_buf);
80   gst_buffer_unref (out_buf);
81
82   gst_harness_teardown (h);
83 }
84
85 GST_START_TEST (test_buffer_meta)
86 {
87   /* test whether buffer meta would be preserved or not */
88
89   test_buffer_meta_common
90       ("video/x-raw,format=(string)NV12,width=340,height=240",
91       "video/x-raw(memory:CUDAMemory)", "cudaupload ! capsfilter");
92   test_buffer_meta_common
93       ("video/x-raw,format=(string)NV12,width=340,height=240", "video/x-raw",
94       "cudaupload ! cudadownload ! capsfilter");
95   test_buffer_meta_common
96       ("video/x-raw,format=(string)NV12,width=340,height=240",
97       "video/x-raw,format=(string)I420,width=340,height=240",
98       "cudaupload ! cudaconvert ! cudadownload ! capsfilter");
99   test_buffer_meta_common
100       ("video/x-raw,format=(string)NV12,width=340,height=240",
101       "video/x-raw,format=(string)NV12,width=640,height=480",
102       "cudaupload ! cudaconvert ! cudascale ! cudaconvert ! cudadownload ! capsfilter");
103 }
104
105 GST_END_TEST;
106
107 static gboolean
108 check_cuda_available (void)
109 {
110   GstElement *elem;
111
112   elem = gst_element_factory_make ("cudaupload", NULL);
113   if (!elem) {
114     GST_WARNING ("cudaupload is not available, possibly driver load failure");
115     return FALSE;
116   }
117   gst_object_unref (elem);
118
119   elem = gst_element_factory_make ("cudadownload", NULL);
120   if (!elem) {
121     GST_WARNING ("cudadownload is not available, possibly driver load failure");
122     return FALSE;
123   }
124   gst_object_unref (elem);
125
126   elem = gst_element_factory_make ("cudaconvert", NULL);
127   if (!elem) {
128     GST_WARNING ("cudaconvert is not available, possibly driver load failure");
129     return FALSE;
130   }
131   gst_object_unref (elem);
132
133   elem = gst_element_factory_make ("cudadownload", NULL);
134   if (!elem) {
135     GST_WARNING ("cudascale is not available, possibly driver load failure");
136     return FALSE;
137   }
138   gst_object_unref (elem);
139
140   return TRUE;
141 }
142
143 static Suite *
144 cudafilter_suite (void)
145 {
146   Suite *s;
147   TCase *tc_chain;
148
149   /* HACK: cuda device init/deinit with fork seems to problematic */
150   g_setenv ("CK_FORK", "no", TRUE);
151
152   s = suite_create ("cudafilter");
153   tc_chain = tcase_create ("general");
154
155   suite_add_tcase (s, tc_chain);
156
157   if (!check_cuda_available ()) {
158     GST_DEBUG ("Skip cuda filter test since cannot open device");
159     goto end;
160   }
161
162   tcase_add_test (tc_chain, test_buffer_meta);
163
164 end:
165   return s;
166 }
167
168 GST_CHECK_MAIN (cudafilter);