documentation: fixed a heap o' typos
[platform/upstream/gstreamer.git] / sys / nvcodec / gstcudautils.c
1 /* GStreamer
2  * Copyright (C) <2018-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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "gstcudautils.h"
25 #include "gstcudacontext.h"
26
27 #ifdef HAVE_NVCODEC_GST_GL
28 #include <gst/gl/gl.h>
29 #include <gst/gl/gstglfuncs.h>
30 #endif
31
32 GST_DEBUG_CATEGORY_STATIC (gst_cuda_utils_debug);
33 #define GST_CAT_DEFAULT gst_cuda_utils_debug
34 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
35
36 static void
37 _init_debug (void)
38 {
39   static volatile gsize once_init = 0;
40
41   if (g_once_init_enter (&once_init)) {
42
43     GST_DEBUG_CATEGORY_INIT (gst_cuda_utils_debug, "cudautils", 0,
44         "CUDA utils");
45     GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
46     g_once_init_leave (&once_init, 1);
47   }
48 }
49
50 static gboolean
51 pad_query (const GValue * item, GValue * value, gpointer user_data)
52 {
53   GstPad *pad = g_value_get_object (item);
54   GstQuery *query = user_data;
55   gboolean res;
56
57   res = gst_pad_peer_query (pad, query);
58
59   if (res) {
60     g_value_set_boolean (value, TRUE);
61     return FALSE;
62   }
63
64   GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, pad, "pad peer query failed");
65   return TRUE;
66 }
67
68 static gboolean
69 run_query (GstElement * element, GstQuery * query, GstPadDirection direction)
70 {
71   GstIterator *it;
72   GstIteratorFoldFunction func = pad_query;
73   GValue res = { 0 };
74
75   g_value_init (&res, G_TYPE_BOOLEAN);
76   g_value_set_boolean (&res, FALSE);
77
78   /* Ask neighbor */
79   if (direction == GST_PAD_SRC)
80     it = gst_element_iterate_src_pads (element);
81   else
82     it = gst_element_iterate_sink_pads (element);
83
84   while (gst_iterator_fold (it, func, &res, query) == GST_ITERATOR_RESYNC)
85     gst_iterator_resync (it);
86
87   gst_iterator_free (it);
88
89   return g_value_get_boolean (&res);
90 }
91
92 static void
93 find_cuda_context (GstElement * element, GstCudaContext ** cuda_ctx)
94 {
95   GstQuery *query;
96   GstContext *ctxt;
97
98   /*  1) Query downstream with GST_QUERY_CONTEXT for the context and
99    *      check if upstream already has a context of the specific type
100    *  2) Query upstream as above.
101    */
102   query = gst_query_new_context (GST_CUDA_CONTEXT_TYPE);
103   if (run_query (element, query, GST_PAD_SRC)) {
104     gst_query_parse_context (query, &ctxt);
105     GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
106         "found context (%p) in downstream query", ctxt);
107     gst_element_set_context (element, ctxt);
108   }
109
110   /* although we found cuda context above, the element does not want
111    * to use the context. Then try to find from the other direction */
112   if (*cuda_ctx == NULL && run_query (element, query, GST_PAD_SINK)) {
113     gst_query_parse_context (query, &ctxt);
114     GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
115         "found context (%p) in upstream query", ctxt);
116     gst_element_set_context (element, ctxt);
117   }
118
119   if (*cuda_ctx == NULL) {
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. 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         GST_CUDA_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 cuda_ctx or call gst_cuda_handle_set_context().
139    */
140
141   gst_query_unref (query);
142 }
143
144 static void
145 context_set_cuda_context (GstContext * context, GstCudaContext * cuda_ctx)
146 {
147   GstStructure *s;
148   gint device_id;
149
150   g_return_if_fail (context != NULL);
151
152   g_object_get (G_OBJECT (cuda_ctx), "cuda-device-id", &device_id, NULL);
153
154   GST_CAT_LOG (GST_CAT_CONTEXT,
155       "setting GstCudaContext(%" GST_PTR_FORMAT
156       ") with cuda-device-id %d on context(%" GST_PTR_FORMAT ")",
157       cuda_ctx, device_id, context);
158
159   s = gst_context_writable_structure (context);
160   gst_structure_set (s, GST_CUDA_CONTEXT_TYPE, GST_TYPE_CUDA_CONTEXT,
161       cuda_ctx, "cuda-device-id", G_TYPE_INT, device_id, NULL);
162 }
163
164 /**
165  * gst_cuda_ensure_element_context:
166  * @element: the #GstElement running the query
167  * @device_id: preferred device-id, pass device_id >=0 when
168  *             the device_id explicitly required. Otherwise, set -1.
169  * @cuda_ctx: (inout): the resulting #GstCudaContext
170  *
171  * Perform the steps necessary for retrieving a #GstCudaContext from the
172  * surrounding elements or from the application using the #GstContext mechanism.
173  *
174  * If the content of @cuda_ctx is not %NULL, then no #GstContext query is
175  * necessary for #GstCudaContext.
176  *
177  * Returns: whether a #GstCudaContext exists in @cuda_ctx
178  */
179 gboolean
180 gst_cuda_ensure_element_context (GstElement * element, gint device_id,
181     GstCudaContext ** cuda_ctx)
182 {
183   g_return_val_if_fail (element != NULL, FALSE);
184   g_return_val_if_fail (cuda_ctx != NULL, FALSE);
185
186   _init_debug ();
187
188   if (*cuda_ctx)
189     return TRUE;
190
191   find_cuda_context (element, cuda_ctx);
192   if (*cuda_ctx)
193     return TRUE;
194
195   /* No available CUDA context in pipeline, create new one here */
196   *cuda_ctx = gst_cuda_context_new (device_id);
197
198   if (*cuda_ctx == NULL) {
199     GST_CAT_ERROR_OBJECT (GST_CAT_CONTEXT, element,
200         "Failed to create CUDA context with device-id %d", device_id);
201     return FALSE;
202   } else {
203     GstContext *context;
204     GstMessage *msg;
205
206     /* Propagate new CUDA context */
207
208     context = gst_context_new (GST_CUDA_CONTEXT_TYPE, TRUE);
209     context_set_cuda_context (context, *cuda_ctx);
210
211     gst_element_set_context (element, context);
212
213     GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
214         "posting have context (%p) message with CUDA context (%p)",
215         context, *cuda_ctx);
216     msg = gst_message_new_have_context (GST_OBJECT_CAST (element), context);
217     gst_element_post_message (GST_ELEMENT_CAST (element), msg);
218   }
219
220   return TRUE;
221 }
222
223 /**
224  * gst_cuda_handle_set_context:
225  * @element: a #GstElement
226  * @context: a #GstContext
227  * @device_id: preferred device-id, pass device_id >=0 when
228  *             the device_id explicitly required. Otherwise, set -1.
229  * @cuda_ctx: (inout) (transfer full): location of a #GstCudaContext
230  *
231  * Helper function for implementing #GstElementClass.set_context() in
232  * CUDA capable elements.
233  *
234  * Retrieves the #GstCudaContext in @context and places the result in @cuda_ctx.
235  *
236  * Returns: whether the @cuda_ctx could be set successfully
237  */
238 gboolean
239 gst_cuda_handle_set_context (GstElement * element,
240     GstContext * context, gint device_id, GstCudaContext ** cuda_ctx)
241 {
242   const gchar *context_type;
243
244   g_return_val_if_fail (element != NULL, FALSE);
245   g_return_val_if_fail (cuda_ctx != NULL, FALSE);
246
247   _init_debug ();
248
249   if (!context)
250     return FALSE;
251
252   context_type = gst_context_get_context_type (context);
253   if (g_strcmp0 (context_type, GST_CUDA_CONTEXT_TYPE) == 0) {
254     const GstStructure *str;
255     GstCudaContext *other_ctx = NULL;
256     gint other_device_id = 0;
257
258     /* If we had context already, will not replace it */
259     if (*cuda_ctx)
260       return TRUE;
261
262     str = gst_context_get_structure (context);
263     if (gst_structure_get (str, GST_CUDA_CONTEXT_TYPE, GST_TYPE_CUDA_CONTEXT,
264             &other_ctx, NULL)) {
265       g_object_get (other_ctx, "cuda-device-id", &other_device_id, NULL);
266
267       if (device_id == -1 || other_device_id == device_id) {
268         GST_CAT_DEBUG_OBJECT (GST_CAT_CONTEXT, element, "Found CUDA context");
269         *cuda_ctx = other_ctx;
270
271         return TRUE;
272       }
273
274       gst_object_unref (other_ctx);
275     }
276   }
277
278   return FALSE;
279 }
280
281 /**
282  * gst_cuda_handle_context_query:
283  * @element: a #GstElement
284  * @query: a #GstQuery of type %GST_QUERY_CONTEXT
285  * @cuda_ctx: (transfer none) (nullable): a #GstCudaContext
286  *
287  * Returns: Whether the @query was successfully responded to from the passed
288  *          @context.
289  */
290 gboolean
291 gst_cuda_handle_context_query (GstElement * element,
292     GstQuery * query, GstCudaContext * cuda_ctx)
293 {
294   const gchar *context_type;
295   GstContext *context, *old_context;
296
297   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
298   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
299   g_return_val_if_fail (cuda_ctx == NULL
300       || GST_IS_CUDA_CONTEXT (cuda_ctx), FALSE);
301
302   _init_debug ();
303
304   GST_CAT_LOG_OBJECT (GST_CAT_CONTEXT, element,
305       "handle context query %" GST_PTR_FORMAT, query);
306   gst_query_parse_context_type (query, &context_type);
307
308   if (cuda_ctx && g_strcmp0 (context_type, GST_CUDA_CONTEXT_TYPE) == 0) {
309     gst_query_parse_context (query, &old_context);
310
311     if (old_context)
312       context = gst_context_copy (old_context);
313     else
314       context = gst_context_new (GST_CUDA_CONTEXT_TYPE, TRUE);
315
316     context_set_cuda_context (context, cuda_ctx);
317     gst_query_set_context (query, context);
318     gst_context_unref (context);
319     GST_CAT_DEBUG_OBJECT (GST_CAT_CONTEXT, element,
320         "successfully set %" GST_PTR_FORMAT " on %" GST_PTR_FORMAT, cuda_ctx,
321         query);
322
323     return TRUE;
324   }
325
326   return FALSE;
327 }
328
329 /**
330  * gst_context_new_cuda_context:
331  * @cuda_ctx: (transfer none) a #GstCudaContext
332  *
333  * Returns: (transfer full) (nullable): a new #GstContext embedding the @cuda_ctx
334  * or %NULL
335  */
336 GstContext *
337 gst_context_new_cuda_context (GstCudaContext * cuda_ctx)
338 {
339   GstContext *context;
340
341   g_return_val_if_fail (GST_IS_CUDA_CONTEXT (cuda_ctx), NULL);
342
343   context = gst_context_new (GST_CUDA_CONTEXT_TYPE, TRUE);
344   context_set_cuda_context (context, cuda_ctx);
345
346   return context;
347 }
348
349 static const gchar *gst_cuda_quark_strings[] =
350     { "GstCudaQuarkGraphicsResource" };
351
352 static GQuark gst_cuda_quark_table[GST_CUDA_QUARK_MAX];
353
354 static void
355 init_cuda_quark_once (void)
356 {
357   static volatile gsize once_init = 0;
358
359   if (g_once_init_enter (&once_init)) {
360     gint i;
361
362     for (i = 0; i < GST_CUDA_QUARK_MAX; i++) {
363       gst_cuda_quark_table[i] =
364           g_quark_from_static_string (gst_cuda_quark_strings[i]);
365
366       g_once_init_leave (&once_init, 1);
367     }
368   }
369 }
370
371 /**
372  * gst_cuda_quark_from_id: (skip)
373  * @id: a #GstCudaQuarkId
374  *
375  * Returns: the GQuark for given @id or 0 if @id is unknown value
376  */
377 GQuark
378 gst_cuda_quark_from_id (GstCudaQuarkId id)
379 {
380   g_return_val_if_fail (id < GST_CUDA_QUARK_MAX, 0);
381
382   init_cuda_quark_once ();
383   _init_debug ();
384
385   return gst_cuda_quark_table[id];
386 }
387
388 /**
389  * gst_cuda_graphics_resource_new: (skip)
390  * @context: (transfer none): a #GstCudaContext
391  * @graphics_context: (transfer none) (nullable): a graphics API specific context object
392  * @type: a #GstCudaGraphicsResourceType of resource registration
393  *
394  * Create new #GstCudaGraphicsResource with given @context and @type
395  *
396  * Returns: a new #GstCudaGraphicsResource.
397  * Free with gst_cuda_graphics_resource_free
398  */
399 GstCudaGraphicsResource *
400 gst_cuda_graphics_resource_new (GstCudaContext *
401     context, GstObject * graphics_context, GstCudaGraphicsResourceType type)
402 {
403   GstCudaGraphicsResource *resource;
404
405   g_return_val_if_fail (GST_IS_CUDA_CONTEXT (context), NULL);
406
407   _init_debug ();
408
409   resource = g_new0 (GstCudaGraphicsResource, 1);
410   resource->cuda_context = gst_object_ref (context);
411   if (graphics_context)
412     resource->graphics_context = gst_object_ref (graphics_context);
413
414   return resource;
415 }
416
417 /**
418  * gst_cuda_graphics_resource_register_gl_buffer: (skip)
419  * @resource a #GstCudaGraphicsResource
420  * @buffer: a GL buffer object
421  * @flags: a #CUgraphicsRegisterFlags
422  *
423  * Register the @buffer for access by CUDA.
424  * Must be called from the gl context thread with current cuda context was
425  * pushed on the current thread
426  *
427  * Returns: whether @buffer was registered or not
428  */
429 gboolean
430 gst_cuda_graphics_resource_register_gl_buffer (GstCudaGraphicsResource *
431     resource, guint buffer, CUgraphicsRegisterFlags flags)
432 {
433   CUresult cuda_ret;
434
435   g_return_val_if_fail (resource != NULL, FALSE);
436   g_return_val_if_fail (resource->registered == FALSE, FALSE);
437
438   _init_debug ();
439
440   cuda_ret = CuGraphicsGLRegisterBuffer (&resource->resource, buffer, flags);
441
442   if (!gst_cuda_result (cuda_ret))
443     return FALSE;
444
445   resource->registered = TRUE;
446   resource->type = GST_CUDA_GRAPHICS_RESOURCE_GL_BUFFER;
447   resource->flags = flags;
448
449   return TRUE;
450 }
451
452 /**
453  * gst_cuda_graphics_resource_unregister: (skip)
454  * @resource: a #GstCudaGraphicsResource
455  *
456  * Unregister previously registered resource.
457  * For GL resource, this method must be called from gl context thread.
458  * Also, current cuda context should be pushed on the current thread
459  * before calling this method.
460  */
461 void
462 gst_cuda_graphics_resource_unregister (GstCudaGraphicsResource * resource)
463 {
464   g_return_if_fail (resource != NULL);
465
466   _init_debug ();
467
468   if (!resource->registered)
469     return;
470
471   gst_cuda_result (CuGraphicsUnregisterResource (resource->resource));
472   resource->resource = NULL;
473   resource->registered = FALSE;
474
475   return;
476 }
477
478 /**
479  * gst_cuda_graphics_resource_map: (skip)
480  * @resource: a #GstCudaGraphicsResource
481  * @stream: a #CUstream
482  * @flags: a #CUgraphicsMapResourceFlags
483  *
484  * Map previously registered resource with map flags
485  *
486  * Returns: the #CUgraphicsResource if successful or %NULL when failed
487  */
488 CUgraphicsResource
489 gst_cuda_graphics_resource_map (GstCudaGraphicsResource * resource,
490     CUstream stream, CUgraphicsMapResourceFlags flags)
491 {
492   CUresult cuda_ret;
493
494   g_return_val_if_fail (resource != NULL, NULL);
495   g_return_val_if_fail (resource->registered != FALSE, NULL);
496
497   _init_debug ();
498
499   cuda_ret = CuGraphicsResourceSetMapFlags (resource->resource, flags);
500   if (!gst_cuda_result (cuda_ret))
501     return NULL;
502
503   cuda_ret = CuGraphicsMapResources (1, &resource->resource, stream);
504   if (!gst_cuda_result (cuda_ret))
505     return NULL;
506
507   resource->mapped = TRUE;
508
509   return resource->resource;
510 }
511
512 /**
513  * gst_cuda_graphics_resource_unmap: (skip)
514  * @resource: a #GstCudaGraphicsResource
515  * @stream: a #CUstream
516  *
517  * Unmap previously mapped resource
518  */
519 void
520 gst_cuda_graphics_resource_unmap (GstCudaGraphicsResource * resource,
521     CUstream stream)
522 {
523   g_return_if_fail (resource != NULL);
524   g_return_if_fail (resource->registered != FALSE);
525
526   _init_debug ();
527
528   if (!resource->mapped)
529     return;
530
531   gst_cuda_result (CuGraphicsUnmapResources (1, &resource->resource, stream));
532
533   resource->mapped = FALSE;
534 }
535
536 #ifdef HAVE_NVCODEC_GST_GL
537 static void
538 unregister_resource_from_gl_thread (GstGLContext * gl_context,
539     GstCudaGraphicsResource * resource)
540 {
541   GstCudaContext *cuda_context = resource->cuda_context;
542
543   if (!gst_cuda_context_push (cuda_context)) {
544     GST_WARNING_OBJECT (cuda_context, "failed to push CUDA context");
545     return;
546   }
547
548   gst_cuda_graphics_resource_unregister (resource);
549
550   if (!gst_cuda_context_pop (NULL)) {
551     GST_WARNING_OBJECT (cuda_context, "failed to pop CUDA context");
552   }
553 }
554 #endif
555
556 /**
557  * gst_cuda_graphics_resource_free: (skip)
558  * @resource: a #GstCudaGraphicsResource
559  *
560  * Free @resource
561  */
562 void
563 gst_cuda_graphics_resource_free (GstCudaGraphicsResource * resource)
564 {
565   g_return_if_fail (resource != NULL);
566
567   if (resource->registered) {
568 #ifdef HAVE_NVCODEC_GST_GL
569     if (resource->type == GST_CUDA_GRAPHICS_RESOURCE_GL_BUFFER) {
570       gst_gl_context_thread_add ((GstGLContext *) resource->graphics_context,
571           (GstGLContextThreadFunc) unregister_resource_from_gl_thread,
572           resource);
573     } else
574 #endif
575     {
576       /* FIXME: currently opengl only */
577       g_assert_not_reached ();
578     }
579   }
580
581   gst_object_unref (resource->cuda_context);
582   if (resource->graphics_context)
583     gst_object_unref (resource->graphics_context);
584   g_free (resource);
585 }