taglist, plugins: fix compiler warnings with GLib >= 2.76
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / msdk / gstmsdkcontextutil.c
1 /* GStreamer Intel MSDK plugin
2  * Copyright (c) 2018, Intel Corporation
3  * Copyright (c) 2018, Igalia S.L.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
31  */
32
33 #include "gstmsdkcontextutil.h"
34 #ifndef _WIN32
35 #include <gst/va/gstvadisplay.h>
36 #include <gst/va/gstvautils.h>
37 #else
38 #include <gst/d3d11/gstd3d11device.h>
39 #endif
40
41 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
42
43 static void
44 _init_context_debug (void)
45 {
46 #ifndef GST_DISABLE_GST_DEBUG
47   static gsize _init = 0;
48
49   if (g_once_init_enter (&_init)) {
50     GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
51     g_once_init_leave (&_init, 1);
52   }
53 #endif
54 }
55
56 #ifdef _WIN32
57 static gboolean
58 _pad_query (const GValue * item, GValue * value, gpointer user_data)
59 {
60   GstPad *pad = g_value_get_object (item);
61   GstQuery *query = user_data;
62   gboolean res;
63
64   res = gst_pad_peer_query (pad, query);
65
66   if (res) {
67     g_value_set_boolean (value, TRUE);
68     return FALSE;
69   }
70
71   GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, pad, "pad peer query failed");
72   return TRUE;
73 }
74
75 static gboolean
76 _run_query (GstElement * element, GstQuery * query, GstPadDirection direction)
77 {
78   GstIterator *it;
79   GstIteratorFoldFunction func = _pad_query;
80   GValue res = G_VALUE_INIT;
81
82   g_value_init (&res, G_TYPE_BOOLEAN);
83   g_value_set_boolean (&res, FALSE);
84
85   if (direction == GST_PAD_SRC)
86     it = gst_element_iterate_src_pads (element);
87   else
88     it = gst_element_iterate_sink_pads (element);
89
90   while (gst_iterator_fold (it, func, &res, query) == GST_ITERATOR_RESYNC)
91     gst_iterator_resync (it);
92
93   gst_iterator_free (it);
94
95   return g_value_get_boolean (&res);
96 }
97
98 static void
99 _context_query (GstElement * element, const gchar * context_type)
100 {
101   GstQuery *query;
102   GstContext *ctxt = NULL;
103
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.
107    */
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);
119   } else {
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
124      *    application.
125      */
126     GstMessage *msg;
127
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),
131         context_type);
132     gst_element_post_message (element, msg);
133   }
134
135   /*
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().
139    */
140
141   gst_query_unref (query);
142 }
143 #endif
144
145 /* Find whether the other elements already have a msdk context. */
146 gboolean
147 gst_msdk_context_find (GstElement * element, GstMsdkContext ** context_ptr)
148 {
149   _init_context_debug ();
150
151   g_return_val_if_fail (element != NULL, FALSE);
152   g_return_val_if_fail (context_ptr != NULL, FALSE);
153
154   /* 1) Check if the element already has a context of the specific type. */
155   if (*context_ptr) {
156     GST_LOG_OBJECT (element, "already have a context %" GST_PTR_FORMAT,
157         *context_ptr);
158     return TRUE;
159   }
160
161   /* This may indirectly set *context_ptr, see function body */
162 #ifndef _WIN32
163   gst_va_context_query (element, GST_MSDK_CONTEXT_TYPE_NAME);
164 #else
165   _context_query (element, GST_MSDK_CONTEXT_TYPE_NAME);
166 #endif
167
168   if (*context_ptr) {
169     GST_LOG_OBJECT (element, "found a context %" GST_PTR_FORMAT, *context_ptr);
170     return TRUE;
171   }
172
173   return *context_ptr != NULL;
174 }
175
176 gboolean
177 gst_msdk_context_get_context (GstContext * context,
178     GstMsdkContext ** msdk_context)
179 {
180   const GstStructure *structure;
181   const gchar *type;
182
183   _init_context_debug ();
184
185   g_return_val_if_fail (GST_IS_CONTEXT (context), FALSE);
186
187   type = gst_context_get_context_type (context);
188
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);
193   }
194
195   return FALSE;
196 }
197
198 static void
199 gst_msdk_context_propagate (GstElement * element, GstMsdkContext * msdk_context)
200 {
201   GstContext *context;
202   GstStructure *structure;
203   GstMessage *msg;
204
205   context = gst_context_new (GST_MSDK_CONTEXT_TYPE_NAME, FALSE);
206
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);
210
211   GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
212       "posting `have-context' message with MSDK context %" GST_PTR_FORMAT,
213       msdk_context);
214
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");
218 }
219
220 /* When we can not find a suitable context from others, we ensure to create
221    a new context. */
222 gboolean
223 gst_msdk_ensure_new_context (GstElement * element, gboolean hardware,
224     GstMsdkContextJobType job, GstMsdkContext ** context_ptr)
225 {
226   GstMsdkContext *msdk_context;
227   gboolean propagate_display = FALSE;
228   gboolean ret = FALSE;
229
230   g_return_val_if_fail (element, FALSE);
231   g_return_val_if_fail (context_ptr, FALSE);
232
233   _init_context_debug ();
234
235   /* 1) Already have. */
236   if (g_atomic_pointer_get (context_ptr))
237     return TRUE;
238
239 #ifndef _WIN32
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);
244   if (msdk_context) {
245     gst_object_ref (msdk_context);
246     propagate_display = FALSE;
247     ret = TRUE;
248     goto done;
249   }
250 #else
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);
255   if (msdk_context) {
256     gst_object_ref (msdk_context);
257     propagate_display = FALSE;
258     ret = TRUE;
259     goto done;
260   }
261 #endif
262
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);
268   if (!msdk_context) {
269     GST_ERROR_OBJECT (element, "Context creation failed");
270     return FALSE;
271   }
272   propagate_display = TRUE;
273   ret = TRUE;
274
275   GST_INFO_OBJECT (element, "New MSDK Context %p", msdk_context);
276
277   gst_object_replace ((GstObject **) context_ptr, (GstObject *) msdk_context);
278
279 done:
280   if (propagate_display) {
281 #ifndef _WIN32
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);
286 #endif
287   }
288
289   gst_msdk_context_propagate (element, msdk_context);
290   gst_object_unref (msdk_context);
291
292   return ret;
293 }
294
295 #ifndef _WIN32
296 gboolean
297 gst_msdk_context_from_external_va_display (GstContext * context,
298     gboolean hardware, GstMsdkContextJobType job_type,
299     GstMsdkContext ** msdk_context)
300 {
301   GstObject *va_display = NULL;
302   const gchar *type;
303   const GstStructure *s;
304   GstMsdkContext *ctx = NULL;
305
306   _init_context_debug ();
307
308   type = gst_context_get_context_type (context);
309   if (g_strcmp0 (type, GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR))
310     return FALSE;
311
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. */
316       ctx =
317           gst_msdk_context_new_with_va_display (va_display, hardware, job_type);
318       if (ctx)
319         *msdk_context = ctx;
320     }
321
322     /* let's try other fields */
323     gst_clear_object (&va_display);
324   }
325
326   if (ctx)
327     return TRUE;
328
329   return FALSE;
330 }
331 #else
332 gboolean
333 gst_msdk_context_from_external_d3d11_device (GstContext * context,
334     gboolean hardware, GstMsdkContextJobType job_type,
335     GstMsdkContext ** msdk_context)
336 {
337   GstD3D11Device *d3d11_device = NULL;
338   const gchar *type;
339   const GstStructure *s;
340   GstMsdkContext *ctx = NULL;
341   guint vendor_id = 0;
342
343   _init_context_debug ();
344
345   type = gst_context_get_context_type (context);
346   if (g_strcmp0 (type, GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE))
347     return FALSE;
348
349   s = gst_context_get_structure (context);
350   if (gst_structure_get (s, "device", GST_TYPE_D3D11_DEVICE, &d3d11_device,
351           NULL)) {
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);
356       return FALSE;
357     }
358     ctx =
359         gst_msdk_context_new_with_d3d11_device (d3d11_device, hardware,
360         job_type);
361     if (ctx)
362       *msdk_context = ctx;
363
364     gst_clear_object (&d3d11_device);
365   }
366
367   if (ctx)
368     return TRUE;
369
370   return FALSE;
371 }
372 #endif
373
374 gboolean
375 gst_msdk_handle_context_query (GstElement * element, GstQuery * query,
376     GstMsdkContext * msdk_context)
377 {
378   const gchar *context_type;
379   GstContext *ctxt, *old_ctxt;
380   gboolean ret = FALSE;
381
382   _init_context_debug ();
383
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);
388
389   GST_CAT_LOG_OBJECT (GST_CAT_CONTEXT, element,
390       "handle context query %" GST_PTR_FORMAT, query);
391
392   if (!msdk_context)
393     return FALSE;
394
395   gst_query_parse_context_type (query, &context_type);
396
397   gst_query_parse_context (query, &old_ctxt);
398   if (old_ctxt)
399     ctxt = gst_context_copy (old_ctxt);
400   else
401     ctxt = gst_context_new (context_type, TRUE);
402
403 #ifndef _WIN32
404   if (g_strcmp0 (context_type, GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR) == 0) {
405     GstStructure *s;
406     GstObject *display = gst_msdk_context_get_va_display (msdk_context);
407
408     if (display) {
409       GST_CAT_LOG (GST_CAT_CONTEXT,
410           "setting GstVaDisplay (%" GST_PTR_FORMAT ") on context (%"
411           GST_PTR_FORMAT ")", display, ctxt);
412
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);
417       ret = TRUE;
418     }
419   } else
420 #else
421   if (g_strcmp0 (context_type, GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE) == 0) {
422     GstStructure *s;
423     GstD3D11Device *device = gst_msdk_context_get_d3d11_device (msdk_context);
424
425     if (device) {
426       GST_CAT_LOG (GST_CAT_CONTEXT,
427           "setting GstD3D11Device (%" GST_PTR_FORMAT ") on context (%"
428           GST_PTR_FORMAT ")", device, ctxt);
429
430       s = gst_context_writable_structure (ctxt);
431       gst_structure_set (s, "device", GST_TYPE_D3D11_DEVICE, device, NULL);
432       gst_object_unref (device);
433       ret = TRUE;
434     }
435   } else
436 #endif
437   if (g_strcmp0 (context_type, GST_MSDK_CONTEXT_TYPE_NAME) == 0) {
438     GstStructure *s;
439
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,
445         msdk_context, NULL);
446     ret = TRUE;
447   }
448
449   if (ret)
450     gst_query_set_context (query, ctxt);
451
452   gst_context_unref (ctxt);
453   return ret;
454 }