1 /* GStreamer Intel MSDK plugin
2 * Copyright (c) 2018, Intel Corporation
3 * Copyright (c) 2018, Igalia S.L.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGDECE
29 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "gstmsdkcontextutil.h"
35 #include <gst/va/gstvadisplay.h>
36 #include <gst/va/gstvautils.h>
38 #include <gst/d3d11/gstd3d11device.h>
41 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
44 _init_context_debug (void)
46 #ifndef GST_DISABLE_GST_DEBUG
47 static gsize _init = 0;
49 if (g_once_init_enter (&_init)) {
50 GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
51 g_once_init_leave (&_init, 1);
58 _pad_query (const GValue * item, GValue * value, gpointer user_data)
60 GstPad *pad = g_value_get_object (item);
61 GstQuery *query = user_data;
64 res = gst_pad_peer_query (pad, query);
67 g_value_set_boolean (value, TRUE);
71 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, pad, "pad peer query failed");
76 _run_query (GstElement * element, GstQuery * query, GstPadDirection direction)
79 GstIteratorFoldFunction func = _pad_query;
80 GValue res = G_VALUE_INIT;
82 g_value_init (&res, G_TYPE_BOOLEAN);
83 g_value_set_boolean (&res, FALSE);
85 if (direction == GST_PAD_SRC)
86 it = gst_element_iterate_src_pads (element);
88 it = gst_element_iterate_sink_pads (element);
90 while (gst_iterator_fold (it, func, &res, query) == GST_ITERATOR_RESYNC)
91 gst_iterator_resync (it);
93 gst_iterator_free (it);
95 return g_value_get_boolean (&res);
99 _context_query (GstElement * element, const gchar * context_type)
102 GstContext *ctxt = NULL;
104 /* 2a) Query downstream with GST_QUERY_CONTEXT for the context and
105 * check if downstream already has a context of the specific type
106 * 2b) Query upstream as above.
108 query = gst_query_new_context (context_type);
109 if (_run_query (element, query, GST_PAD_SRC)) {
110 gst_query_parse_context (query, &ctxt);
111 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
112 "found context (%p) in downstream query", ctxt);
113 gst_element_set_context (element, ctxt);
114 } else if (_run_query (element, query, GST_PAD_SINK)) {
115 gst_query_parse_context (query, &ctxt);
116 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
117 "found context (%p) in upstream query", ctxt);
118 gst_element_set_context (element, ctxt);
120 /* 3) Post a GST_MESSAGE_NEED_CONTEXT message on the bus with
121 * the required context type and afterwards check if a
122 * usable context was set now as in 1). The message could
123 * be handled by the parent bins of the element and the
128 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
129 "posting need context message");
130 msg = gst_message_new_need_context (GST_OBJECT_CAST (element),
132 gst_element_post_message (element, msg);
136 * Whomever responds to the need-context message performs a
137 * GstElement::set_context() with the required context in which the element
138 * is required to update the display_ptr or call gst_va_handle_set_context().
141 gst_query_unref (query);
145 /* Find whether the other elements already have a msdk context. */
147 gst_msdk_context_find (GstElement * element, GstMsdkContext ** context_ptr)
149 _init_context_debug ();
151 g_return_val_if_fail (element != NULL, FALSE);
152 g_return_val_if_fail (context_ptr != NULL, FALSE);
154 /* 1) Check if the element already has a context of the specific type. */
156 GST_LOG_OBJECT (element, "already have a context %" GST_PTR_FORMAT,
161 /* This may indirectly set *context_ptr, see function body */
163 gst_va_context_query (element, GST_MSDK_CONTEXT_TYPE_NAME);
165 _context_query (element, GST_MSDK_CONTEXT_TYPE_NAME);
169 GST_LOG_OBJECT (element, "found a context %" GST_PTR_FORMAT, *context_ptr);
173 return *context_ptr != NULL;
177 gst_msdk_context_get_context (GstContext * context,
178 GstMsdkContext ** msdk_context)
180 const GstStructure *structure;
183 _init_context_debug ();
185 g_return_val_if_fail (GST_IS_CONTEXT (context), FALSE);
187 type = gst_context_get_context_type (context);
189 if (!g_strcmp0 (type, GST_MSDK_CONTEXT_TYPE_NAME)) {
190 structure = gst_context_get_structure (context);
191 return gst_structure_get (structure, GST_MSDK_CONTEXT_TYPE_NAME,
192 GST_TYPE_MSDK_CONTEXT, msdk_context, NULL);
199 gst_msdk_context_propagate (GstElement * element, GstMsdkContext * msdk_context)
202 GstStructure *structure;
205 context = gst_context_new (GST_MSDK_CONTEXT_TYPE_NAME, FALSE);
207 structure = gst_context_writable_structure (context);
208 gst_structure_set (structure, GST_MSDK_CONTEXT_TYPE_NAME,
209 GST_TYPE_MSDK_CONTEXT, msdk_context, NULL);
211 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
212 "posting `have-context' message with MSDK context %" GST_PTR_FORMAT,
215 msg = gst_message_new_have_context (GST_OBJECT_CAST (element), context);
216 if (!gst_element_post_message (element, msg))
217 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element, "No bus attached");
220 /* When we can not find a suitable context from others, we ensure to create
223 gst_msdk_ensure_new_context (GstElement * element, gboolean hardware,
224 GstMsdkContextJobType job, GstMsdkContext ** context_ptr)
226 GstMsdkContext *msdk_context;
227 gboolean propagate_display = FALSE;
228 gboolean ret = FALSE;
230 g_return_val_if_fail (element, FALSE);
231 g_return_val_if_fail (context_ptr, FALSE);
233 _init_context_debug ();
235 /* 1) Already have. */
236 if (g_atomic_pointer_get (context_ptr))
240 /* 2) Query the neighbour the VA display. If already a valid VA display,
241 using it by gst_msdk_context_from_external_va_display() in set_context(). */
242 gst_va_context_query (element, GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR);
243 msdk_context = g_atomic_pointer_get (context_ptr);
245 gst_object_ref (msdk_context);
246 propagate_display = FALSE;
251 /* 2) Query the neighbour the D3D11 device. If already a valid D3D11 device,
252 using it by gst_msdk_context_from_external_d3d11_device() in set_context(). */
253 _context_query (element, GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE);
254 msdk_context = g_atomic_pointer_get (context_ptr);
256 gst_object_ref (msdk_context);
257 propagate_display = FALSE;
263 /* 3) Create a MSDK context from scratch. Currently we use environment variable
264 to enable user to choose GPU device in multi-GPU environment. This variable
265 is only valid when there's no context returned by upstream or downstream.
266 Otherwise it will use the device that created by upstream or downstream. */
267 msdk_context = gst_msdk_context_new (hardware, job);
269 GST_ERROR_OBJECT (element, "Context creation failed");
272 propagate_display = TRUE;
275 GST_INFO_OBJECT (element, "New MSDK Context %p", msdk_context);
277 gst_object_replace ((GstObject **) context_ptr, (GstObject *) msdk_context);
280 if (propagate_display) {
282 GstVaDisplay *display =
283 (GstVaDisplay *) gst_msdk_context_get_va_display (msdk_context);
284 gst_va_element_propagate_display_context (element, display);
285 gst_clear_object (&display);
289 gst_msdk_context_propagate (element, msdk_context);
290 gst_object_unref (msdk_context);
297 gst_msdk_context_from_external_va_display (GstContext * context,
298 gboolean hardware, GstMsdkContextJobType job_type,
299 GstMsdkContext ** msdk_context)
301 GstObject *va_display = NULL;
303 const GstStructure *s;
304 GstMsdkContext *ctx = NULL;
306 _init_context_debug ();
308 type = gst_context_get_context_type (context);
309 if (g_strcmp0 (type, GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR))
312 s = gst_context_get_structure (context);
313 if (gst_structure_get (s, "gst-display", GST_TYPE_OBJECT, &va_display, NULL)) {
314 if (GST_IS_VA_DISPLAY (va_display)) {
315 /* TODO: Need to check whether the display is the DEV we want. */
317 gst_msdk_context_new_with_va_display (va_display, hardware, job_type);
322 /* let's try other fields */
323 gst_clear_object (&va_display);
333 gst_msdk_context_from_external_d3d11_device (GstContext * context,
334 gboolean hardware, GstMsdkContextJobType job_type,
335 GstMsdkContext ** msdk_context)
337 GstD3D11Device *d3d11_device = NULL;
339 const GstStructure *s;
340 GstMsdkContext *ctx = NULL;
343 _init_context_debug ();
345 type = gst_context_get_context_type (context);
346 if (g_strcmp0 (type, GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE))
349 s = gst_context_get_structure (context);
350 if (gst_structure_get (s, "device", GST_TYPE_D3D11_DEVICE, &d3d11_device,
352 g_object_get (d3d11_device, "vendor-id", &vendor_id, NULL);
353 if (vendor_id != 0x8086) {
354 GST_ERROR ("Not an Intel device");
355 gst_clear_object (&d3d11_device);
359 gst_msdk_context_new_with_d3d11_device (d3d11_device, hardware,
364 gst_clear_object (&d3d11_device);
375 gst_msdk_handle_context_query (GstElement * element, GstQuery * query,
376 GstMsdkContext * msdk_context)
378 const gchar *context_type;
379 GstContext *ctxt, *old_ctxt;
380 gboolean ret = FALSE;
382 _init_context_debug ();
384 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
385 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
386 g_return_val_if_fail (!msdk_context
387 || GST_IS_MSDK_CONTEXT (msdk_context), FALSE);
389 GST_CAT_LOG_OBJECT (GST_CAT_CONTEXT, element,
390 "handle context query %" GST_PTR_FORMAT, query);
395 gst_query_parse_context_type (query, &context_type);
397 gst_query_parse_context (query, &old_ctxt);
399 ctxt = gst_context_copy (old_ctxt);
401 ctxt = gst_context_new (context_type, TRUE);
404 if (g_strcmp0 (context_type, GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR) == 0) {
406 GstObject *display = gst_msdk_context_get_va_display (msdk_context);
409 GST_CAT_LOG (GST_CAT_CONTEXT,
410 "setting GstVaDisplay (%" GST_PTR_FORMAT ") on context (%"
411 GST_PTR_FORMAT ")", display, ctxt);
413 s = gst_context_writable_structure (ctxt);
414 gst_structure_set (s, "gst-display", GST_TYPE_OBJECT, display, NULL);
415 /* Structure hold one ref */
416 gst_object_unref (display);
421 if (g_strcmp0 (context_type, GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE) == 0) {
423 GstD3D11Device *device = gst_msdk_context_get_d3d11_device (msdk_context);
426 GST_CAT_LOG (GST_CAT_CONTEXT,
427 "setting GstD3D11Device (%" GST_PTR_FORMAT ") on context (%"
428 GST_PTR_FORMAT ")", device, ctxt);
430 s = gst_context_writable_structure (ctxt);
431 gst_structure_set (s, "device", GST_TYPE_D3D11_DEVICE, device, NULL);
432 gst_object_unref (device);
437 if (g_strcmp0 (context_type, GST_MSDK_CONTEXT_TYPE_NAME) == 0) {
440 s = gst_context_writable_structure (ctxt);
441 GST_CAT_LOG (GST_CAT_CONTEXT,
442 "setting GstMsdkContext (%" GST_PTR_FORMAT ") on context (%"
443 GST_PTR_FORMAT ")", msdk_context, ctxt);
444 gst_structure_set (s, GST_MSDK_CONTEXT_TYPE_NAME, GST_TYPE_MSDK_CONTEXT,
450 gst_query_set_context (query, ctxt);
452 gst_context_unref (ctxt);