Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / tests / check / elements / nvdec.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 <string.h>
23
24 /* 64x64 h264 byte-stream format keyframe with sps/pps
25  * encoded using x264enc with videotestsrc */
26 static guint8 h264_frame[] = {
27   0x00, 0x00, 0x00, 0x01, 0x09, 0x10, 0x00, 0x00, 0x00, 0x01, 0x67, 0x64,
28   0x00, 0x14, 0xac, 0xd9, 0x44, 0x26, 0xc0, 0x5a, 0x83, 0x00, 0x83, 0x52,
29   0x80, 0x00, 0x00, 0x03, 0x00, 0x80, 0x00, 0x00, 0x1e, 0x47, 0x8a, 0x14,
30   0xcb, 0x00, 0x00, 0x00, 0x01, 0x68, 0xeb, 0xec, 0xb2, 0x2c, 0x00, 0x00,
31   0x00, 0x01, 0x06, 0x05, 0xff, 0xff, 0xf5, 0xdc, 0x45, 0xe9, 0xbd, 0xe6,
32   0xd9, 0x48, 0xb7, 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef, 0x78,
33   0x32, 0x36, 0x34, 0x20, 0x2d, 0x20, 0x63, 0x6f, 0x72, 0x65, 0x20, 0x31,
34   0x35, 0x37, 0x20, 0x72, 0x32, 0x39, 0x34, 0x35, 0x2b, 0x34, 0x35, 0x20,
35   0x32, 0x37, 0x35, 0x36, 0x35, 0x64, 0x31, 0x20, 0x2d, 0x20, 0x48, 0x2e,
36   0x32, 0x36, 0x34, 0x2f, 0x4d, 0x50, 0x45, 0x47, 0x2d, 0x34, 0x20, 0x41,
37   0x56, 0x43, 0x20, 0x63, 0x6f, 0x64, 0x65, 0x63, 0x20, 0x2d, 0x20, 0x43,
38   0x6f, 0x70, 0x79, 0x6c, 0x65, 0x66, 0x74, 0x20, 0x32, 0x30, 0x30, 0x33,
39   0x2d, 0x32, 0x30, 0x31, 0x38, 0x20, 0x2d, 0x20, 0x68, 0x74, 0x74, 0x70,
40   0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x76, 0x69, 0x64, 0x65, 0x6f,
41   0x6c, 0x61, 0x6e, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x78, 0x32, 0x36, 0x34,
42   0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x20, 0x2d, 0x20, 0x6f, 0x70, 0x74, 0x69,
43   0x6f, 0x6e, 0x73, 0x3a, 0x20, 0x63, 0x61, 0x62, 0x61, 0x63, 0x3d, 0x31,
44   0x20, 0x72, 0x65, 0x66, 0x3d, 0x33, 0x20, 0x64, 0x65, 0x62, 0x6c, 0x6f,
45   0x63, 0x6b, 0x3d, 0x31, 0x3a, 0x30, 0x3a, 0x30, 0x20, 0x61, 0x6e, 0x61,
46   0x6c, 0x79, 0x73, 0x65, 0x3d, 0x30, 0x78, 0x33, 0x3a, 0x30, 0x78, 0x31,
47   0x31, 0x33, 0x20, 0x6d, 0x65, 0x3d, 0x68, 0x65, 0x78, 0x20, 0x73, 0x75,
48   0x62, 0x6d, 0x65, 0x3d, 0x37, 0x20, 0x70, 0x73, 0x79, 0x3d, 0x31, 0x20,
49   0x70, 0x73, 0x79, 0x5f, 0x72, 0x64, 0x3d, 0x31, 0x2e, 0x30, 0x30, 0x3a,
50   0x30, 0x2e, 0x30, 0x30, 0x20, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x5f, 0x72,
51   0x65, 0x66, 0x3d, 0x31, 0x20, 0x6d, 0x65, 0x5f, 0x72, 0x61, 0x6e, 0x67,
52   0x65, 0x3d, 0x31, 0x36, 0x20, 0x63, 0x68, 0x72, 0x6f, 0x6d, 0x61, 0x5f,
53   0x6d, 0x65, 0x3d, 0x31, 0x20, 0x74, 0x72, 0x65, 0x6c, 0x6c, 0x69, 0x73,
54   0x3d, 0x31, 0x20, 0x38, 0x78, 0x38, 0x64, 0x63, 0x74, 0x3d, 0x31, 0x20,
55   0x63, 0x71, 0x6d, 0x3d, 0x30, 0x20, 0x64, 0x65, 0x61, 0x64, 0x7a, 0x6f,
56   0x6e, 0x65, 0x3d, 0x32, 0x31, 0x2c, 0x31, 0x31, 0x20, 0x66, 0x61, 0x73,
57   0x74, 0x5f, 0x70, 0x73, 0x6b, 0x69, 0x70, 0x3d, 0x31, 0x20, 0x63, 0x68,
58   0x72, 0x6f, 0x6d, 0x61, 0x5f, 0x71, 0x70, 0x5f, 0x6f, 0x66, 0x66, 0x73,
59   0x65, 0x74, 0x3d, 0x2d, 0x32, 0x20, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64,
60   0x73, 0x3d, 0x32, 0x20, 0x6c, 0x6f, 0x6f, 0x6b, 0x61, 0x68, 0x65, 0x61,
61   0x64, 0x5f, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x31, 0x20,
62   0x73, 0x6c, 0x69, 0x63, 0x65, 0x64, 0x5f, 0x74, 0x68, 0x72, 0x65, 0x61,
63   0x64, 0x73, 0x3d, 0x30, 0x20, 0x6e, 0x72, 0x3d, 0x30, 0x20, 0x64, 0x65,
64   0x63, 0x69, 0x6d, 0x61, 0x74, 0x65, 0x3d, 0x31, 0x20, 0x69, 0x6e, 0x74,
65   0x65, 0x72, 0x6c, 0x61, 0x63, 0x65, 0x64, 0x3d, 0x30, 0x20, 0x62, 0x6c,
66   0x75, 0x72, 0x61, 0x79, 0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x3d,
67   0x30, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x72, 0x61, 0x69, 0x6e, 0x65,
68   0x64, 0x5f, 0x69, 0x6e, 0x74, 0x72, 0x61, 0x3d, 0x30, 0x20, 0x62, 0x66,
69   0x72, 0x61, 0x6d, 0x65, 0x73, 0x3d, 0x33, 0x20, 0x62, 0x5f, 0x70, 0x79,
70   0x72, 0x61, 0x6d, 0x69, 0x64, 0x3d, 0x32, 0x20, 0x62, 0x5f, 0x61, 0x64,
71   0x61, 0x70, 0x74, 0x3d, 0x31, 0x20, 0x62, 0x5f, 0x62, 0x69, 0x61, 0x73,
72   0x3d, 0x30, 0x20, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x3d, 0x31, 0x20,
73   0x77, 0x65, 0x69, 0x67, 0x68, 0x74, 0x62, 0x3d, 0x31, 0x20, 0x6f, 0x70,
74   0x65, 0x6e, 0x5f, 0x67, 0x6f, 0x70, 0x3d, 0x30, 0x20, 0x77, 0x65, 0x69,
75   0x67, 0x68, 0x74, 0x70, 0x3d, 0x32, 0x20, 0x6b, 0x65, 0x79, 0x69, 0x6e,
76   0x74, 0x3d, 0x33, 0x30, 0x30, 0x20, 0x6b, 0x65, 0x79, 0x69, 0x6e, 0x74,
77   0x5f, 0x6d, 0x69, 0x6e, 0x3d, 0x33, 0x30, 0x20, 0x73, 0x63, 0x65, 0x6e,
78   0x65, 0x63, 0x75, 0x74, 0x3d, 0x34, 0x30, 0x20, 0x69, 0x6e, 0x74, 0x72,
79   0x61, 0x5f, 0x72, 0x65, 0x66, 0x72, 0x65, 0x73, 0x68, 0x3d, 0x30, 0x20,
80   0x72, 0x63, 0x5f, 0x6c, 0x6f, 0x6f, 0x6b, 0x61, 0x68, 0x65, 0x61, 0x64,
81   0x3d, 0x34, 0x30, 0x20, 0x72, 0x63, 0x3d, 0x63, 0x62, 0x72, 0x20, 0x6d,
82   0x62, 0x74, 0x72, 0x65, 0x65, 0x3d, 0x31, 0x20, 0x62, 0x69, 0x74, 0x72,
83   0x61, 0x74, 0x65, 0x3d, 0x32, 0x30, 0x34, 0x38, 0x20, 0x72, 0x61, 0x74,
84   0x65, 0x74, 0x6f, 0x6c, 0x3d, 0x31, 0x2e, 0x30, 0x20, 0x71, 0x63, 0x6f,
85   0x6d, 0x70, 0x3d, 0x30, 0x2e, 0x36, 0x30, 0x20, 0x71, 0x70, 0x6d, 0x69,
86   0x6e, 0x3d, 0x30, 0x20, 0x71, 0x70, 0x6d, 0x61, 0x78, 0x3d, 0x36, 0x39,
87   0x20, 0x71, 0x70, 0x73, 0x74, 0x65, 0x70, 0x3d, 0x34, 0x20, 0x76, 0x62,
88   0x76, 0x5f, 0x6d, 0x61, 0x78, 0x72, 0x61, 0x74, 0x65, 0x3d, 0x32, 0x30,
89   0x34, 0x38, 0x20, 0x76, 0x62, 0x76, 0x5f, 0x62, 0x75, 0x66, 0x73, 0x69,
90   0x7a, 0x65, 0x3d, 0x31, 0x32, 0x32, 0x38, 0x20, 0x6e, 0x61, 0x6c, 0x5f,
91   0x68, 0x72, 0x64, 0x3d, 0x6e, 0x6f, 0x6e, 0x65, 0x20, 0x66, 0x69, 0x6c,
92   0x6c, 0x65, 0x72, 0x3d, 0x30, 0x20, 0x69, 0x70, 0x5f, 0x72, 0x61, 0x74,
93   0x69, 0x6f, 0x3d, 0x31, 0x2e, 0x34, 0x30, 0x20, 0x61, 0x71, 0x3d, 0x31,
94   0x3a, 0x31, 0x2e, 0x30, 0x30, 0x00, 0x80, 0x00, 0x00, 0x00, 0x01, 0x65,
95   0x88, 0x84, 0x00, 0x1a, 0xff, 0xfe, 0xf7, 0xd4, 0xb7, 0xcc, 0xb2, 0xee,
96   0x07, 0x22, 0xb6, 0x0b, 0xa8, 0xf7, 0xa2, 0x9c, 0x8c, 0x81, 0xee, 0x0c,
97   0x19, 0x51, 0xa2, 0x7c, 0x81, 0xbd, 0x7b, 0x46, 0xcd
98 };
99
100 static guint h264_frame_len = 849;
101
102 #define MAX_PUSH_BUFFER 64
103
104 GST_START_TEST (test_downstream_reconfigure)
105 {
106   GstHarness *h;
107   GstElement *capsfilter;
108   GstCaps *caps;
109   GstBuffer *in_buf, *out_buf = NULL;
110   GstMapInfo map;
111   GstFlowReturn ret;
112   gint i = 0;
113
114   h = gst_harness_new_parse ("h264parse ! nvh264dec ! capsfilter");
115   fail_unless (h != NULL);
116
117   capsfilter = gst_harness_find_element (h, "capsfilter");
118
119   gst_harness_play (h);
120
121   gst_harness_set_src_caps_str (h, "video/x-h264,width=64,height=64,"
122       "stream-format=byte-stream,alignment=au");
123
124   /* enforce system memory */
125   caps = gst_caps_from_string ("video/x-raw");
126   g_object_set (capsfilter, "caps", caps, NULL);
127   gst_caps_unref (caps);
128
129   in_buf = gst_buffer_new_and_alloc (h264_frame_len);
130   gst_buffer_map (in_buf, &map, GST_MAP_WRITE);
131   memcpy (map.data, h264_frame, h264_frame_len);
132   gst_buffer_unmap (in_buf, &map);
133
134   GST_BUFFER_DURATION (in_buf) = GST_SECOND;
135
136   /* Push buffers until get decoder output */
137   do {
138     fail_if (i > MAX_PUSH_BUFFER);
139
140     GST_BUFFER_PTS (in_buf) = GST_BUFFER_DTS (in_buf) = i * GST_SECOND;
141
142     ret = gst_harness_push (h, gst_buffer_ref (in_buf));
143     fail_unless (ret == GST_FLOW_OK, "GstFlowReturn was %s",
144         gst_flow_get_name (ret));
145
146     out_buf = gst_harness_try_pull (h);
147     i++;
148   } while (out_buf == NULL);
149   gst_buffer_unref (out_buf);
150
151   /* Use opengl memory */
152   caps = gst_caps_from_string ("video/x-raw(memory:GLMemory)");
153   g_object_set (capsfilter, "caps", caps, NULL);
154   gst_caps_unref (caps);
155
156   do {
157     fail_if (i > 2 * MAX_PUSH_BUFFER);
158
159     GST_BUFFER_PTS (in_buf) = GST_BUFFER_DTS (in_buf) = i * GST_SECOND;
160
161     ret = gst_harness_push (h, gst_buffer_ref (in_buf));
162     fail_unless (ret == GST_FLOW_OK, "GstFlowReturn was %s",
163         gst_flow_get_name (ret));
164
165     out_buf = gst_harness_try_pull (h);
166     i++;
167   } while (out_buf == NULL);
168   gst_buffer_unref (in_buf);
169   gst_buffer_unref (out_buf);
170
171   gst_object_unref (capsfilter);
172   gst_harness_teardown (h);
173 }
174
175 GST_END_TEST;
176
177 static gboolean
178 check_nvcodec_available (void)
179 {
180   gboolean ret = TRUE;
181   GstElement *nvdec;
182
183   nvdec = gst_element_factory_make ("nvh264dec", NULL);
184   if (!nvdec) {
185     GST_WARNING ("nvh264dec is not available, possibly driver load failure");
186     return FALSE;
187   }
188
189   /* GST_STATE_READY is meaning that driver could be loaded */
190   if (gst_element_set_state (nvdec,
191           GST_STATE_PAUSED) != GST_STATE_CHANGE_SUCCESS) {
192     GST_WARNING ("cannot open device");
193     ret = FALSE;
194   }
195
196   gst_element_set_state (nvdec, GST_STATE_NULL);
197   gst_object_unref (nvdec);
198
199   return ret;
200 }
201
202 static Suite *
203 nvdec_suite (void)
204 {
205   Suite *s;
206   TCase *tc_chain;
207
208   /* HACK: cuda device init/deinit with fork seems to problematic */
209   g_setenv ("CK_FORK", "no", TRUE);
210
211   s = suite_create ("nvdec");
212   tc_chain = tcase_create ("general");
213
214   suite_add_tcase (s, tc_chain);
215
216   if (!check_nvcodec_available ()) {
217     GST_DEBUG ("Skip nvdec test since cannot open device");
218     goto end;
219   }
220
221   tcase_add_test (tc_chain, test_downstream_reconfigure);
222
223 end:
224   return s;
225 }
226
227 GST_CHECK_MAIN (nvdec);