2017-05-03 00:21:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Ericsson AB. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2021-12-08 11:48:08 +00:00
|
|
|
#include "gstcuvidloader.h"
|
2017-05-03 00:21:43 +00:00
|
|
|
#include "gstnvdec.h"
|
2019-08-06 04:44:20 +00:00
|
|
|
|
2019-07-31 03:11:05 +00:00
|
|
|
#include <string.h>
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-11-28 09:54:31 +00:00
|
|
|
GST_DEBUG_CATEGORY_EXTERN (gst_nvdec_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_nvdec_debug
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2020-10-02 13:22:34 +00:00
|
|
|
#define DEFAULT_MAX_DISPLAY_DELAY -1
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_MAX_DISPLAY_DELAY,
|
2022-06-26 12:09:50 +00:00
|
|
|
PROP_CUDA_DEVICE_ID,
|
2020-10-02 13:22:34 +00:00
|
|
|
};
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2019-08-17 05:03:57 +00:00
|
|
|
#define SUPPORTED_GL_APIS (GST_GL_API_OPENGL | GST_GL_API_OPENGL3 | GST_GL_API_GLES2)
|
|
|
|
|
2019-07-19 13:46:01 +00:00
|
|
|
static gboolean
|
|
|
|
gst_nvdec_copy_device_to_gl (GstNvDec * nvdec,
|
|
|
|
CUVIDPARSERDISPINFO * dispinfo, GstBuffer * output_buffer);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static gboolean
|
2019-08-30 04:55:25 +00:00
|
|
|
gst_nvdec_copy_device_to_memory (GstNvDec * nvdec,
|
2019-07-19 13:46:01 +00:00
|
|
|
CUVIDPARSERDISPINFO * dispinfo, GstBuffer * output_buffer);
|
2019-07-18 15:52:59 +00:00
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2019-08-16 10:32:39 +00:00
|
|
|
typedef struct _GstNvDecRegisterResourceData
|
|
|
|
{
|
|
|
|
GstMemory *mem;
|
2019-08-18 04:27:38 +00:00
|
|
|
GstCudaGraphicsResource *resource;
|
2019-08-16 10:32:39 +00:00
|
|
|
GstNvDec *nvdec;
|
|
|
|
gboolean ret;
|
|
|
|
} GstNvDecRegisterResourceData;
|
|
|
|
|
2017-05-03 00:21:43 +00:00
|
|
|
static void
|
2019-08-16 10:32:39 +00:00
|
|
|
register_cuda_resource (GstGLContext * context,
|
|
|
|
GstNvDecRegisterResourceData * data)
|
2017-05-03 00:21:43 +00:00
|
|
|
{
|
2019-08-16 10:32:39 +00:00
|
|
|
GstMemory *mem = data->mem;
|
2019-08-18 04:27:38 +00:00
|
|
|
GstCudaGraphicsResource *resource = data->resource;
|
2019-08-16 10:32:39 +00:00
|
|
|
GstNvDec *nvdec = data->nvdec;
|
2017-05-03 00:21:43 +00:00
|
|
|
GstMapInfo map_info = GST_MAP_INFO_INIT;
|
2019-08-17 05:46:00 +00:00
|
|
|
GstGLBuffer *gl_buf_obj;
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-16 10:32:39 +00:00
|
|
|
data->ret = FALSE;
|
|
|
|
|
|
|
|
if (!gst_cuda_context_push (nvdec->cuda_ctx)) {
|
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to push CUDA context");
|
|
|
|
return;
|
|
|
|
}
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2017-06-28 01:47:37 +00:00
|
|
|
if (gst_memory_map (mem, &map_info, GST_MAP_READ | GST_MAP_GL)) {
|
2019-08-17 05:46:00 +00:00
|
|
|
GstGLMemoryPBO *gl_mem = (GstGLMemoryPBO *) data->mem;
|
|
|
|
gl_buf_obj = gl_mem->pbo;
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-17 05:46:00 +00:00
|
|
|
GST_LOG_OBJECT (nvdec,
|
2019-09-10 23:13:17 +00:00
|
|
|
"register glbuffer %d to CUDA resource", gl_buf_obj->id);
|
2019-08-17 05:46:00 +00:00
|
|
|
|
2019-08-18 04:27:38 +00:00
|
|
|
/* register resource without read/write only flags, since
|
|
|
|
* downstream CUDA elements (e.g., nvenc) might want to access
|
|
|
|
* this resource later. Instead, use map flags during map/unmap */
|
|
|
|
if (gst_cuda_graphics_resource_register_gl_buffer (resource,
|
|
|
|
gl_buf_obj->id, CU_GRAPHICS_REGISTER_FLAGS_NONE)) {
|
2019-08-16 10:32:39 +00:00
|
|
|
data->ret = TRUE;
|
2019-08-17 05:46:00 +00:00
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to register memory");
|
2019-08-16 10:32:39 +00:00
|
|
|
}
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2017-06-28 01:47:37 +00:00
|
|
|
gst_memory_unmap (mem, &map_info);
|
2019-08-16 10:32:39 +00:00
|
|
|
} else {
|
2019-07-21 12:23:30 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to map memory");
|
2019-08-16 10:32:39 +00:00
|
|
|
}
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
if (!gst_cuda_context_pop (NULL))
|
2019-07-21 12:23:30 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to unlock CUDA context");
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 04:27:38 +00:00
|
|
|
static GstCudaGraphicsResource *
|
2019-07-21 12:23:30 +00:00
|
|
|
ensure_cuda_graphics_resource (GstMemory * mem, GstNvDec * nvdec)
|
2017-05-03 00:21:43 +00:00
|
|
|
{
|
2019-08-18 04:27:38 +00:00
|
|
|
GQuark quark;
|
|
|
|
GstCudaGraphicsResource *cgr_info;
|
2019-08-16 10:32:39 +00:00
|
|
|
GstNvDecRegisterResourceData data;
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-17 05:46:00 +00:00
|
|
|
if (!gst_is_gl_memory_pbo (mem)) {
|
|
|
|
GST_WARNING_OBJECT (nvdec, "memory is not GL PBO memory, %s",
|
|
|
|
mem->allocator->mem_type);
|
2017-05-03 00:21:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-18 04:27:38 +00:00
|
|
|
quark = gst_cuda_quark_from_id (GST_CUDA_QUARK_GRAPHICS_RESOURCE);
|
2017-06-28 01:47:37 +00:00
|
|
|
|
|
|
|
cgr_info = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem), quark);
|
|
|
|
if (!cgr_info) {
|
2019-08-18 04:27:38 +00:00
|
|
|
cgr_info = gst_cuda_graphics_resource_new (nvdec->cuda_ctx,
|
|
|
|
GST_OBJECT (GST_GL_BASE_MEMORY_CAST (mem)->context),
|
|
|
|
GST_CUDA_GRAPHICS_RESOURCE_GL_BUFFER);
|
2019-08-16 10:32:39 +00:00
|
|
|
data.mem = mem;
|
2019-08-18 04:27:38 +00:00
|
|
|
data.resource = cgr_info;
|
2019-08-16 10:32:39 +00:00
|
|
|
data.nvdec = nvdec;
|
2019-08-18 04:27:38 +00:00
|
|
|
gst_gl_context_thread_add ((GstGLContext *) cgr_info->graphics_context,
|
2019-08-16 10:32:39 +00:00
|
|
|
(GstGLContextThreadFunc) register_cuda_resource, &data);
|
|
|
|
if (!data.ret) {
|
|
|
|
GST_WARNING_OBJECT (nvdec, "could not register resource");
|
2019-08-18 04:27:38 +00:00
|
|
|
gst_cuda_graphics_resource_free (cgr_info);
|
2019-08-16 10:32:39 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-28 01:47:37 +00:00
|
|
|
gst_mini_object_set_qdata (GST_MINI_OBJECT (mem), quark, cgr_info,
|
2019-08-18 04:27:38 +00:00
|
|
|
(GDestroyNotify) gst_cuda_graphics_resource_free);
|
2017-06-28 01:47:37 +00:00
|
|
|
}
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-18 04:27:38 +00:00
|
|
|
return cgr_info;
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
2023-02-17 15:36:29 +00:00
|
|
|
#endif /* HAVE_CUDA_GST_GL */
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-07-19 13:46:01 +00:00
|
|
|
static gboolean gst_nvdec_open (GstVideoDecoder * decoder);
|
2017-05-03 00:21:43 +00:00
|
|
|
static gboolean gst_nvdec_start (GstVideoDecoder * decoder);
|
|
|
|
static gboolean gst_nvdec_stop (GstVideoDecoder * decoder);
|
2019-08-06 04:44:20 +00:00
|
|
|
static gboolean gst_nvdec_close (GstVideoDecoder * decoder);
|
2017-05-03 00:21:43 +00:00
|
|
|
static gboolean gst_nvdec_set_format (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecState * state);
|
|
|
|
static GstFlowReturn gst_nvdec_handle_frame (GstVideoDecoder * decoder,
|
|
|
|
GstVideoCodecFrame * frame);
|
|
|
|
static gboolean gst_nvdec_decide_allocation (GstVideoDecoder * decoder,
|
|
|
|
GstQuery * query);
|
|
|
|
static void gst_nvdec_set_context (GstElement * element, GstContext * context);
|
|
|
|
static gboolean gst_nvdec_src_query (GstVideoDecoder * decoder,
|
|
|
|
GstQuery * query);
|
2017-11-17 06:09:22 +00:00
|
|
|
static gboolean gst_nvdec_flush (GstVideoDecoder * decoder);
|
|
|
|
static GstFlowReturn gst_nvdec_drain (GstVideoDecoder * decoder);
|
2018-12-20 03:37:43 +00:00
|
|
|
static GstFlowReturn gst_nvdec_finish (GstVideoDecoder * decoder);
|
2019-08-09 11:19:38 +00:00
|
|
|
static gboolean gst_nvdec_negotiate (GstVideoDecoder * decoder);
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2019-08-29 11:20:14 +00:00
|
|
|
static gboolean gst_nvdec_ensure_gl_context (GstNvDec * nvdec);
|
|
|
|
#endif
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-09 11:19:38 +00:00
|
|
|
#define gst_nvdec_parent_class parent_class
|
2019-07-11 12:53:46 +00:00
|
|
|
G_DEFINE_ABSTRACT_TYPE (GstNvDec, gst_nvdec, GST_TYPE_VIDEO_DECODER);
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2020-10-02 13:22:34 +00:00
|
|
|
static void
|
|
|
|
gst_nv_dec_set_property (GObject * object, guint prop_id, const GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_MAX_DISPLAY_DELAY:
|
|
|
|
nvdec->max_display_delay = g_value_get_int (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_nv_dec_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (object);
|
2022-06-26 12:09:50 +00:00
|
|
|
GstNvDecClass *klass = GST_NVDEC_GET_CLASS (nvdec);
|
2020-10-02 13:22:34 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_MAX_DISPLAY_DELAY:
|
|
|
|
g_value_set_int (value, nvdec->max_display_delay);
|
|
|
|
break;
|
2022-06-26 12:09:50 +00:00
|
|
|
case PROP_CUDA_DEVICE_ID:
|
|
|
|
g_value_set_uint (value, klass->cuda_device_id);
|
|
|
|
break;
|
2020-10-02 13:22:34 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 00:21:43 +00:00
|
|
|
static void
|
|
|
|
gst_nvdec_class_init (GstNvDecClass * klass)
|
|
|
|
{
|
2020-10-02 13:22:34 +00:00
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
2017-05-03 00:21:43 +00:00
|
|
|
GstVideoDecoderClass *video_decoder_class = GST_VIDEO_DECODER_CLASS (klass);
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
2020-10-02 13:22:34 +00:00
|
|
|
gobject_class->set_property = gst_nv_dec_set_property;
|
|
|
|
gobject_class->get_property = gst_nv_dec_get_property;
|
|
|
|
|
2019-07-19 13:46:01 +00:00
|
|
|
video_decoder_class->open = GST_DEBUG_FUNCPTR (gst_nvdec_open);
|
2017-05-03 00:21:43 +00:00
|
|
|
video_decoder_class->start = GST_DEBUG_FUNCPTR (gst_nvdec_start);
|
|
|
|
video_decoder_class->stop = GST_DEBUG_FUNCPTR (gst_nvdec_stop);
|
2019-08-06 04:44:20 +00:00
|
|
|
video_decoder_class->close = GST_DEBUG_FUNCPTR (gst_nvdec_close);
|
2017-05-03 00:21:43 +00:00
|
|
|
video_decoder_class->set_format = GST_DEBUG_FUNCPTR (gst_nvdec_set_format);
|
|
|
|
video_decoder_class->handle_frame =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_nvdec_handle_frame);
|
|
|
|
video_decoder_class->decide_allocation =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_nvdec_decide_allocation);
|
|
|
|
video_decoder_class->src_query = GST_DEBUG_FUNCPTR (gst_nvdec_src_query);
|
2017-11-17 06:09:22 +00:00
|
|
|
video_decoder_class->drain = GST_DEBUG_FUNCPTR (gst_nvdec_drain);
|
|
|
|
video_decoder_class->flush = GST_DEBUG_FUNCPTR (gst_nvdec_flush);
|
2018-12-20 03:37:43 +00:00
|
|
|
video_decoder_class->finish = GST_DEBUG_FUNCPTR (gst_nvdec_finish);
|
2019-08-09 11:19:38 +00:00
|
|
|
video_decoder_class->negotiate = GST_DEBUG_FUNCPTR (gst_nvdec_negotiate);
|
2017-05-03 00:21:43 +00:00
|
|
|
|
|
|
|
element_class->set_context = GST_DEBUG_FUNCPTR (gst_nvdec_set_context);
|
2020-06-23 00:51:48 +00:00
|
|
|
gst_type_mark_as_plugin_api (GST_TYPE_NVDEC, 0);
|
2020-10-02 13:22:34 +00:00
|
|
|
|
2021-11-04 19:06:22 +00:00
|
|
|
/**
|
|
|
|
* GstNvDec:max-display-delay:
|
|
|
|
*
|
|
|
|
* Since: 1.20
|
|
|
|
*/
|
2020-10-02 13:22:34 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_MAX_DISPLAY_DELAY,
|
|
|
|
g_param_spec_int ("max-display-delay", "Max Display Delay",
|
|
|
|
"Improves pipelining of decode with display, 0 means no delay "
|
|
|
|
"(auto = -1)",
|
|
|
|
-1, G_MAXINT, DEFAULT_MAX_DISPLAY_DELAY,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2022-06-26 12:09:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GstNvDec:cuda-device-id:
|
|
|
|
*
|
|
|
|
* Assigned CUDA device id
|
|
|
|
*
|
|
|
|
* Since: 1.22
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (gobject_class, PROP_CUDA_DEVICE_ID,
|
|
|
|
g_param_spec_uint ("cuda-device-id", "CUDA device id",
|
|
|
|
"Assigned CUDA device id", 0, G_MAXINT, 0,
|
|
|
|
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_nvdec_init (GstNvDec * nvdec)
|
|
|
|
{
|
2020-10-02 13:22:34 +00:00
|
|
|
nvdec->max_display_delay = DEFAULT_MAX_DISPLAY_DELAY;
|
2017-05-03 00:21:43 +00:00
|
|
|
gst_video_decoder_set_packetized (GST_VIDEO_DECODER (nvdec), TRUE);
|
|
|
|
gst_video_decoder_set_needs_format (GST_VIDEO_DECODER (nvdec), TRUE);
|
|
|
|
}
|
|
|
|
|
2019-08-09 09:47:14 +00:00
|
|
|
static cudaVideoSurfaceFormat
|
|
|
|
get_cuda_surface_format_from_gst (GstVideoFormat format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case GST_VIDEO_FORMAT_NV12:
|
|
|
|
return cudaVideoSurfaceFormat_NV12;
|
|
|
|
case GST_VIDEO_FORMAT_P010_10LE:
|
|
|
|
case GST_VIDEO_FORMAT_P010_10BE:
|
|
|
|
case GST_VIDEO_FORMAT_P016_LE:
|
|
|
|
case GST_VIDEO_FORMAT_P016_BE:
|
|
|
|
return cudaVideoSurfaceFormat_P016;
|
|
|
|
case GST_VIDEO_FORMAT_Y444:
|
|
|
|
return cudaVideoSurfaceFormat_YUV444;
|
|
|
|
case GST_VIDEO_FORMAT_Y444_16LE:
|
|
|
|
case GST_VIDEO_FORMAT_Y444_16BE:
|
|
|
|
return cudaVideoSurfaceFormat_YUV444_16Bit;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cudaVideoSurfaceFormat_NV12;
|
|
|
|
}
|
|
|
|
|
2020-04-09 07:12:58 +00:00
|
|
|
static guint
|
|
|
|
calculate_num_decode_surface (cudaVideoCodec codec, guint width, guint height)
|
|
|
|
{
|
|
|
|
switch (codec) {
|
|
|
|
case cudaVideoCodec_VP9:
|
|
|
|
return 12;
|
|
|
|
case cudaVideoCodec_H264:
|
|
|
|
case cudaVideoCodec_H264_SVC:
|
|
|
|
case cudaVideoCodec_H264_MVC:
|
|
|
|
return 20;
|
|
|
|
case cudaVideoCodec_HEVC:{
|
|
|
|
gint max_dpb_size;
|
|
|
|
gint MaxLumaPS;
|
|
|
|
const gint MaxDpbPicBuf = 6;
|
|
|
|
gint PicSizeInSamplesY;
|
|
|
|
|
|
|
|
/* A.4.1 */
|
|
|
|
MaxLumaPS = 35651584;
|
|
|
|
PicSizeInSamplesY = width * height;
|
|
|
|
if (PicSizeInSamplesY <= (MaxLumaPS >> 2))
|
|
|
|
max_dpb_size = MaxDpbPicBuf * 4;
|
|
|
|
else if (PicSizeInSamplesY <= (MaxLumaPS >> 1))
|
|
|
|
max_dpb_size = MaxDpbPicBuf * 2;
|
|
|
|
else if (PicSizeInSamplesY <= ((3 * MaxLumaPS) >> 2))
|
|
|
|
max_dpb_size = (MaxDpbPicBuf * 4) / 3;
|
|
|
|
else
|
|
|
|
max_dpb_size = MaxDpbPicBuf;
|
|
|
|
|
|
|
|
max_dpb_size = MIN (max_dpb_size, 16);
|
|
|
|
|
|
|
|
return max_dpb_size + 4;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
2020-10-02 13:22:34 +00:00
|
|
|
static guint
|
|
|
|
gst_nvdec_get_max_display_delay (GstNvDec * nvdec)
|
|
|
|
{
|
|
|
|
return nvdec->max_display_delay >= 0 ? nvdec->max_display_delay :
|
|
|
|
(nvdec->is_live ? 0 : 4);
|
|
|
|
}
|
|
|
|
|
2020-10-02 14:02:38 +00:00
|
|
|
static gint64
|
|
|
|
gst_nvdec_get_latency (GstNvDec * nvdec)
|
|
|
|
{
|
2020-11-20 15:01:03 +00:00
|
|
|
gint fps_n, fps_d;
|
|
|
|
|
2020-10-02 14:02:38 +00:00
|
|
|
if (!nvdec->input_state)
|
|
|
|
return 0;
|
2020-11-20 15:01:03 +00:00
|
|
|
fps_n = GST_VIDEO_INFO_FPS_N (&nvdec->input_state->info);
|
|
|
|
fps_d = GST_VIDEO_INFO_FPS_D (&nvdec->input_state->info);
|
|
|
|
|
|
|
|
/* We assume 25 fps if the input framerate is invalid */
|
|
|
|
if (fps_n < 1 || fps_d < 1) {
|
|
|
|
fps_n = 25;
|
|
|
|
fps_d = 1;
|
|
|
|
}
|
2020-10-02 14:02:38 +00:00
|
|
|
|
|
|
|
return gst_util_uint64_scale_int ((nvdec->num_decode_surface +
|
2020-11-20 15:01:03 +00:00
|
|
|
gst_nvdec_get_max_display_delay (nvdec)) * GST_SECOND, fps_d, fps_n);
|
2020-10-02 14:02:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 07:12:58 +00:00
|
|
|
/* 0: fail, 1: succeeded, > 1: override dpb size of parser
|
|
|
|
* (set by CUVIDPARSERPARAMS::ulMaxNumDecodeSurfaces while creating parser) */
|
|
|
|
static gint CUDAAPI
|
2017-05-03 00:21:43 +00:00
|
|
|
parser_sequence_callback (GstNvDec * nvdec, CUVIDEOFORMAT * format)
|
|
|
|
{
|
2019-08-09 09:47:14 +00:00
|
|
|
guint width, height;
|
2017-05-03 00:21:43 +00:00
|
|
|
CUVIDDECODECREATEINFO create_info = { 0, };
|
2019-08-09 09:47:14 +00:00
|
|
|
GstVideoFormat out_format;
|
|
|
|
GstVideoInfo *in_info = &nvdec->input_state->info;
|
|
|
|
GstVideoInfo *out_info = &nvdec->out_info;
|
|
|
|
GstVideoInfo prev_out_info = *out_info;
|
2019-08-06 04:44:20 +00:00
|
|
|
GstCudaContext *ctx = nvdec->cuda_ctx;
|
2019-08-09 09:47:14 +00:00
|
|
|
GstStructure *in_s = NULL;
|
|
|
|
gboolean updata = FALSE;
|
2020-04-09 07:12:58 +00:00
|
|
|
guint major_api_ver = 0;
|
2020-10-02 14:02:38 +00:00
|
|
|
guint64 curr_latency, old_latency;
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2020-10-02 14:02:38 +00:00
|
|
|
old_latency = gst_nvdec_get_latency (nvdec);
|
2017-05-03 00:21:43 +00:00
|
|
|
width = format->display_area.right - format->display_area.left;
|
|
|
|
height = format->display_area.bottom - format->display_area.top;
|
2019-08-09 09:47:14 +00:00
|
|
|
|
|
|
|
switch (format->chroma_format) {
|
|
|
|
case cudaVideoChromaFormat_444:
|
|
|
|
if (format->bit_depth_luma_minus8 == 0) {
|
|
|
|
out_format = GST_VIDEO_FORMAT_Y444;
|
|
|
|
} else if (format->bit_depth_luma_minus8 == 2 ||
|
|
|
|
format->bit_depth_luma_minus8 == 4) {
|
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
|
|
|
out_format = GST_VIDEO_FORMAT_Y444_16LE;
|
|
|
|
#else
|
|
|
|
out_format = GST_VIDEO_FORMAT_Y444_16BE;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
GST_ERROR_OBJECT (nvdec, "Unknown 4:4:4 format bitdepth %d",
|
|
|
|
format->bit_depth_luma_minus8 + 8);
|
|
|
|
|
|
|
|
nvdec->last_ret = GST_FLOW_NOT_NEGOTIATED;
|
2020-04-09 07:12:58 +00:00
|
|
|
return 0;
|
2019-08-09 09:47:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case cudaVideoChromaFormat_420:
|
|
|
|
if (format->bit_depth_luma_minus8 == 0) {
|
|
|
|
out_format = GST_VIDEO_FORMAT_NV12;
|
|
|
|
} else if (format->bit_depth_luma_minus8 == 2) {
|
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
|
|
|
out_format = GST_VIDEO_FORMAT_P010_10LE;
|
|
|
|
#else
|
|
|
|
out_format = GST_VIDEO_FORMAT_P010_10BE;
|
|
|
|
#endif
|
|
|
|
} else if (format->bit_depth_luma_minus8 == 4) {
|
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
|
|
|
out_format = GST_VIDEO_FORMAT_P016_LE;
|
|
|
|
#else
|
|
|
|
out_format = GST_VIDEO_FORMAT_P016_BE;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
GST_ERROR_OBJECT (nvdec, "Unknown 4:2:0 format bitdepth %d",
|
|
|
|
format->bit_depth_luma_minus8 + 8);
|
|
|
|
|
|
|
|
nvdec->last_ret = GST_FLOW_NOT_NEGOTIATED;
|
2020-04-09 07:12:58 +00:00
|
|
|
return 0;
|
2019-08-09 09:47:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GST_ERROR_OBJECT (nvdec, "unhandled chroma format %d, bitdepth %d",
|
|
|
|
format->chroma_format, format->bit_depth_luma_minus8 + 8);
|
|
|
|
|
|
|
|
nvdec->last_ret = GST_FLOW_NOT_NEGOTIATED;
|
2020-04-09 07:12:58 +00:00
|
|
|
return 0;
|
2019-08-09 09:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (nvdec,
|
|
|
|
"out format: %s", gst_video_format_to_string (out_format));
|
|
|
|
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_DEBUG_OBJECT (nvdec, "width: %u, height: %u", width, height);
|
|
|
|
|
2019-08-09 09:47:14 +00:00
|
|
|
gst_video_info_set_format (out_info, out_format, width, height);
|
|
|
|
GST_VIDEO_INFO_FPS_N (out_info) = GST_VIDEO_INFO_FPS_N (in_info);
|
|
|
|
GST_VIDEO_INFO_FPS_D (out_info) = GST_VIDEO_INFO_FPS_D (in_info);
|
|
|
|
|
|
|
|
if (GST_VIDEO_INFO_FPS_N (out_info) < 1 ||
|
|
|
|
GST_VIDEO_INFO_FPS_D (out_info) < 1) {
|
|
|
|
GST_VIDEO_INFO_FPS_N (out_info) = format->frame_rate.numerator;
|
|
|
|
GST_VIDEO_INFO_FPS_D (out_info) = MAX (1, format->frame_rate.denominator);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (nvdec,
|
|
|
|
"Reading colorimetry information full-range %d matrix %d transfer %d primaries %d",
|
|
|
|
format->video_signal_description.video_full_range_flag,
|
|
|
|
format->video_signal_description.matrix_coefficients,
|
|
|
|
format->video_signal_description.transfer_characteristics,
|
|
|
|
format->video_signal_description.color_primaries);
|
|
|
|
|
|
|
|
if (nvdec->input_state->caps)
|
|
|
|
in_s = gst_caps_get_structure (nvdec->input_state->caps, 0);
|
|
|
|
|
|
|
|
/* Set colorimetry when upstream did not provide it */
|
|
|
|
if (in_s && !gst_structure_has_field (in_s, "colorimetry")) {
|
|
|
|
GstVideoColorimetry colorimetry = { 0, };
|
|
|
|
|
|
|
|
if (format->video_signal_description.video_full_range_flag)
|
|
|
|
colorimetry.range = GST_VIDEO_COLOR_RANGE_0_255;
|
|
|
|
else
|
|
|
|
colorimetry.range = GST_VIDEO_COLOR_RANGE_16_235;
|
|
|
|
|
|
|
|
colorimetry.primaries =
|
|
|
|
gst_video_color_primaries_from_iso
|
|
|
|
(format->video_signal_description.color_primaries);
|
|
|
|
|
|
|
|
colorimetry.transfer =
|
2020-09-07 09:14:47 +00:00
|
|
|
gst_video_transfer_function_from_iso
|
2019-08-09 09:47:14 +00:00
|
|
|
(format->video_signal_description.transfer_characteristics);
|
|
|
|
|
|
|
|
colorimetry.matrix =
|
|
|
|
gst_video_color_matrix_from_iso
|
|
|
|
(format->video_signal_description.matrix_coefficients);
|
|
|
|
|
|
|
|
/* Use a colorimetry having at least one valid colorimetry entry,
|
|
|
|
* because we don't know whether the returned
|
|
|
|
* colorimetry (by nvdec) was actually parsed information or not.
|
|
|
|
* Otherwise let GstVideoInfo handle it with default colorimetry */
|
|
|
|
if (colorimetry.primaries != GST_VIDEO_COLOR_PRIMARIES_UNKNOWN ||
|
|
|
|
colorimetry.transfer != GST_VIDEO_TRANSFER_UNKNOWN ||
|
|
|
|
colorimetry.matrix != GST_VIDEO_COLOR_MATRIX_UNKNOWN) {
|
|
|
|
GST_DEBUG_OBJECT (nvdec,
|
|
|
|
"Found valid colorimetry, update output colorimetry");
|
|
|
|
out_info->colorimetry = colorimetry;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
out_info->colorimetry = in_info->colorimetry;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format->progressive_sequence) {
|
|
|
|
out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
|
|
|
|
|
|
|
|
/* nvdec doesn't seem to deal with interlacing with hevc so rely
|
|
|
|
* on upstream's value */
|
|
|
|
if (format->codec == cudaVideoCodec_HEVC) {
|
|
|
|
out_info->interlace_mode = in_info->interlace_mode;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
out_info->interlace_mode = GST_VIDEO_INTERLACE_MODE_MIXED;
|
|
|
|
}
|
|
|
|
|
2020-04-09 07:12:58 +00:00
|
|
|
if (gst_cuvid_get_api_version (&major_api_ver, NULL) && major_api_ver >= 9) {
|
|
|
|
/* min_num_decode_surfaces was introduced in nvcodec sdk 9.0 header */
|
2020-10-02 14:02:38 +00:00
|
|
|
nvdec->num_decode_surface = format->min_num_decode_surfaces;
|
2020-04-09 07:12:58 +00:00
|
|
|
|
2020-10-02 14:02:38 +00:00
|
|
|
GST_DEBUG_OBJECT (nvdec,
|
|
|
|
"Num decode surface: %d", nvdec->num_decode_surface);
|
2020-04-09 07:12:58 +00:00
|
|
|
} else {
|
2020-10-02 14:02:38 +00:00
|
|
|
nvdec->num_decode_surface =
|
2020-04-09 07:12:58 +00:00
|
|
|
calculate_num_decode_surface (format->codec, width, height);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (nvdec,
|
2020-10-02 14:02:38 +00:00
|
|
|
"Calculated num decode surface: %d", nvdec->num_decode_surface);
|
2020-04-09 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 14:02:38 +00:00
|
|
|
/* Update the latency if it has changed */
|
|
|
|
curr_latency = gst_nvdec_get_latency (nvdec);
|
|
|
|
if (old_latency != curr_latency)
|
|
|
|
gst_video_decoder_set_latency (GST_VIDEO_DECODER (nvdec), curr_latency,
|
|
|
|
curr_latency);
|
|
|
|
|
2019-08-09 09:47:14 +00:00
|
|
|
if (!nvdec->decoder || !gst_video_info_is_equal (out_info, &prev_out_info)) {
|
|
|
|
updata = TRUE;
|
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
if (!gst_cuda_context_push (ctx)) {
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to lock CUDA context");
|
2019-07-18 15:52:59 +00:00
|
|
|
goto error;
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nvdec->decoder) {
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "destroying decoder");
|
2019-08-06 04:50:28 +00:00
|
|
|
if (!gst_cuda_result (CuvidDestroyDecoder (nvdec->decoder))) {
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to destroy decoder");
|
2019-07-18 15:52:59 +00:00
|
|
|
goto error;
|
2017-05-03 00:21:43 +00:00
|
|
|
} else
|
|
|
|
nvdec->decoder = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "creating decoder");
|
2022-09-13 15:58:37 +00:00
|
|
|
create_info.ulWidth = format->coded_width;
|
|
|
|
create_info.ulHeight = format->coded_height;
|
2020-10-02 14:02:38 +00:00
|
|
|
create_info.ulNumDecodeSurfaces = nvdec->num_decode_surface;
|
2017-05-03 00:21:43 +00:00
|
|
|
create_info.CodecType = format->codec;
|
|
|
|
create_info.ChromaFormat = format->chroma_format;
|
|
|
|
create_info.ulCreationFlags = cudaVideoCreate_Default;
|
|
|
|
create_info.display_area.left = format->display_area.left;
|
|
|
|
create_info.display_area.top = format->display_area.top;
|
|
|
|
create_info.display_area.right = format->display_area.right;
|
|
|
|
create_info.display_area.bottom = format->display_area.bottom;
|
2019-08-09 09:47:14 +00:00
|
|
|
create_info.OutputFormat = get_cuda_surface_format_from_gst (out_format);
|
2019-07-24 01:00:56 +00:00
|
|
|
create_info.bitDepthMinus8 = format->bit_depth_luma_minus8;
|
2017-05-03 00:21:43 +00:00
|
|
|
create_info.DeinterlaceMode = cudaVideoDeinterlaceMode_Weave;
|
|
|
|
create_info.ulTargetWidth = width;
|
|
|
|
create_info.ulTargetHeight = height;
|
|
|
|
create_info.ulNumOutputSurfaces = 1;
|
|
|
|
create_info.target_rect.left = 0;
|
|
|
|
create_info.target_rect.top = 0;
|
|
|
|
create_info.target_rect.right = width;
|
|
|
|
create_info.target_rect.bottom = height;
|
|
|
|
|
|
|
|
if (nvdec->decoder
|
2019-08-06 04:50:28 +00:00
|
|
|
|| !gst_cuda_result (CuvidCreateDecoder (&nvdec->decoder,
|
|
|
|
&create_info))) {
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to create decoder");
|
2019-07-18 15:52:59 +00:00
|
|
|
goto error;
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
if (!gst_cuda_context_pop (NULL)) {
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to unlock CUDA context");
|
2019-07-18 15:52:59 +00:00
|
|
|
goto error;
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 09:47:14 +00:00
|
|
|
if (!gst_pad_has_current_caps (GST_VIDEO_DECODER_SRC_PAD (nvdec)) || updata) {
|
2019-08-09 11:19:38 +00:00
|
|
|
if (!gst_video_decoder_negotiate (GST_VIDEO_DECODER (nvdec))) {
|
|
|
|
nvdec->last_ret = GST_FLOW_NOT_NEGOTIATED;
|
2020-04-09 07:12:58 +00:00
|
|
|
return 0;
|
2019-08-09 11:19:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 14:02:38 +00:00
|
|
|
return nvdec->num_decode_surface;
|
2019-08-09 11:19:38 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
nvdec->last_ret = GST_FLOW_ERROR;
|
2020-04-09 07:12:58 +00:00
|
|
|
return 0;
|
2019-08-09 11:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_nvdec_negotiate (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (decoder);
|
|
|
|
GstVideoCodecState *state;
|
|
|
|
GstVideoInfo *vinfo;
|
|
|
|
GstVideoInfo *out_info = &nvdec->out_info;
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "negotiate");
|
2019-07-18 15:52:59 +00:00
|
|
|
|
2019-08-09 11:19:38 +00:00
|
|
|
state = gst_video_decoder_set_output_state (GST_VIDEO_DECODER (nvdec),
|
|
|
|
GST_VIDEO_INFO_FORMAT (out_info), GST_VIDEO_INFO_WIDTH (out_info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (out_info), nvdec->input_state);
|
|
|
|
vinfo = &state->info;
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-09 11:19:38 +00:00
|
|
|
/* update output info with CUvidparser provided one */
|
|
|
|
vinfo->interlace_mode = out_info->interlace_mode;
|
|
|
|
vinfo->fps_n = out_info->fps_n;
|
|
|
|
vinfo->fps_d = out_info->fps_d;
|
2019-07-18 15:52:59 +00:00
|
|
|
|
2019-08-09 11:19:38 +00:00
|
|
|
state->caps = gst_video_info_to_caps (&state->info);
|
|
|
|
nvdec->mem_type = GST_NVDEC_MEM_TYPE_SYSTEM;
|
2019-07-19 13:46:01 +00:00
|
|
|
|
2019-08-09 11:19:38 +00:00
|
|
|
{
|
|
|
|
GstCaps *caps;
|
|
|
|
caps = gst_pad_get_allowed_caps (GST_VIDEO_DECODER_SRC_PAD (nvdec));
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "Allowed caps %" GST_PTR_FORMAT, caps);
|
|
|
|
|
|
|
|
if (!caps || gst_caps_is_any (caps)) {
|
|
|
|
GST_DEBUG_OBJECT (nvdec,
|
|
|
|
"cannot determine output format, use system memory");
|
2019-08-30 04:55:25 +00:00
|
|
|
} else {
|
2019-08-09 11:19:38 +00:00
|
|
|
GstCapsFeatures *features;
|
|
|
|
guint size = gst_caps_get_size (caps);
|
|
|
|
guint i;
|
2019-08-30 04:55:25 +00:00
|
|
|
gboolean have_cuda = FALSE;
|
|
|
|
gboolean have_gl = FALSE;
|
2019-08-09 11:19:38 +00:00
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
features = gst_caps_get_features (caps, i);
|
|
|
|
if (features && gst_caps_features_contains (features,
|
2019-08-30 04:55:25 +00:00
|
|
|
GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY)) {
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "found CUDA memory feature");
|
|
|
|
have_cuda = TRUE;
|
2019-08-09 11:19:38 +00:00
|
|
|
break;
|
2019-07-19 13:46:01 +00:00
|
|
|
}
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2019-08-30 04:55:25 +00:00
|
|
|
if (nvdec->gl_display &&
|
|
|
|
features && gst_caps_features_contains (features,
|
|
|
|
GST_CAPS_FEATURE_MEMORY_GL_MEMORY)) {
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "found GL memory feature");
|
|
|
|
have_gl = TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
2019-07-19 13:46:01 +00:00
|
|
|
}
|
2019-08-30 04:55:25 +00:00
|
|
|
|
|
|
|
if (have_cuda)
|
|
|
|
nvdec->mem_type = GST_NVDEC_MEM_TYPE_CUDA;
|
|
|
|
else if (have_gl)
|
|
|
|
nvdec->mem_type = GST_NVDEC_MEM_TYPE_GL;
|
2019-07-19 13:46:01 +00:00
|
|
|
}
|
2019-08-09 11:19:38 +00:00
|
|
|
gst_clear_caps (&caps);
|
|
|
|
}
|
2019-07-18 15:52:59 +00:00
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2019-08-29 11:20:14 +00:00
|
|
|
if (nvdec->mem_type == GST_NVDEC_MEM_TYPE_GL &&
|
|
|
|
!gst_nvdec_ensure_gl_context (nvdec)) {
|
|
|
|
GST_WARNING_OBJECT (nvdec,
|
2020-03-15 10:20:47 +00:00
|
|
|
"OpenGL context is not CUDA-compatible, fallback to system memory");
|
2019-08-29 11:20:14 +00:00
|
|
|
nvdec->mem_type = GST_NVDEC_MEM_TYPE_SYSTEM;
|
|
|
|
}
|
2020-09-24 15:25:33 +00:00
|
|
|
#endif
|
2019-08-29 11:20:14 +00:00
|
|
|
|
2019-08-30 04:55:25 +00:00
|
|
|
switch (nvdec->mem_type) {
|
|
|
|
case GST_NVDEC_MEM_TYPE_CUDA:
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "use cuda memory");
|
|
|
|
gst_caps_set_features (state->caps, 0,
|
2024-09-03 09:16:19 +00:00
|
|
|
gst_caps_features_new_static_str (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY,
|
|
|
|
NULL));
|
2019-08-30 04:55:25 +00:00
|
|
|
break;
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2019-08-30 04:55:25 +00:00
|
|
|
case GST_NVDEC_MEM_TYPE_GL:
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "use gl memory");
|
|
|
|
gst_caps_set_features (state->caps, 0,
|
2024-09-03 09:16:19 +00:00
|
|
|
gst_caps_features_new_static_str (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
|
|
|
|
NULL));
|
|
|
|
gst_caps_set_simple (state->caps, "texture-target", G_TYPE_STRING, "2D",
|
|
|
|
NULL);
|
2019-08-30 04:55:25 +00:00
|
|
|
break;
|
2019-07-19 13:46:01 +00:00
|
|
|
#endif
|
2019-08-30 04:55:25 +00:00
|
|
|
default:
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "use system memory");
|
|
|
|
break;
|
|
|
|
}
|
2019-07-18 15:52:59 +00:00
|
|
|
|
2019-08-09 11:19:38 +00:00
|
|
|
if (nvdec->output_state)
|
|
|
|
gst_video_codec_state_unref (nvdec->output_state);
|
2019-07-24 01:00:56 +00:00
|
|
|
|
2019-08-09 11:19:38 +00:00
|
|
|
nvdec->output_state = state;
|
2019-07-18 15:52:59 +00:00
|
|
|
|
2019-08-09 11:19:38 +00:00
|
|
|
ret = GST_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
|
2019-07-18 15:52:59 +00:00
|
|
|
|
2019-08-09 11:19:38 +00:00
|
|
|
if (!ret) {
|
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to negotiate with downstream");
|
|
|
|
nvdec->last_ret = GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
}
|
2019-07-18 15:52:59 +00:00
|
|
|
|
2019-08-09 11:19:38 +00:00
|
|
|
return ret;
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 09:52:26 +00:00
|
|
|
static gboolean CUDAAPI
|
2017-05-03 00:21:43 +00:00
|
|
|
parser_decode_callback (GstNvDec * nvdec, CUVIDPICPARAMS * params)
|
|
|
|
{
|
2019-07-18 15:52:59 +00:00
|
|
|
GList *iter, *pending_frames;
|
2019-08-06 04:44:20 +00:00
|
|
|
GstCudaContext *ctx = nvdec->cuda_ctx;
|
2017-05-03 00:21:43 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (nvdec, "picture index: %u", params->CurrPicIdx);
|
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
if (!gst_cuda_context_push (ctx)) {
|
2019-07-18 15:52:59 +00:00
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to lock CUDA context");
|
|
|
|
goto error;
|
|
|
|
}
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-06 04:50:28 +00:00
|
|
|
if (!gst_cuda_result (CuvidDecodePicture (nvdec->decoder, params))) {
|
2019-07-18 15:52:59 +00:00
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to decode picture");
|
|
|
|
goto error;
|
|
|
|
}
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
if (!gst_cuda_context_pop (NULL)) {
|
2019-07-18 15:52:59 +00:00
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to unlock CUDA context");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
pending_frames = gst_video_decoder_get_frames (GST_VIDEO_DECODER (nvdec));
|
2019-07-18 16:07:38 +00:00
|
|
|
|
|
|
|
/* NOTE: this decode callback could be invoked multiple times for
|
|
|
|
* one cuvidParseVideoData() call. Most likely it can be related to "decode only"
|
|
|
|
* frame of VPX codec but no document available.
|
|
|
|
* In that case, the last decoded frame seems to be displayed */
|
|
|
|
|
2019-07-18 15:52:59 +00:00
|
|
|
for (iter = pending_frames; iter; iter = g_list_next (iter)) {
|
|
|
|
guint id;
|
|
|
|
GstVideoCodecFrame *frame = (GstVideoCodecFrame *) iter->data;
|
2019-07-18 16:07:38 +00:00
|
|
|
gboolean set_data = FALSE;
|
2019-07-18 15:52:59 +00:00
|
|
|
|
|
|
|
id = GPOINTER_TO_UINT (gst_video_codec_frame_get_user_data (frame));
|
2019-07-18 16:07:38 +00:00
|
|
|
if (G_UNLIKELY (nvdec->state == GST_NVDEC_STATE_DECODE)) {
|
|
|
|
if (id) {
|
|
|
|
GST_LOG_OBJECT (nvdec, "reset the last user data");
|
|
|
|
set_data = TRUE;
|
|
|
|
}
|
|
|
|
} else if (!id) {
|
|
|
|
set_data = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (set_data) {
|
2019-07-18 15:52:59 +00:00
|
|
|
gst_video_codec_frame_set_user_data (frame,
|
|
|
|
GUINT_TO_POINTER (params->CurrPicIdx + 1), NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-07-18 16:07:38 +00:00
|
|
|
nvdec->state = GST_NVDEC_STATE_DECODE;
|
|
|
|
|
2019-07-18 15:52:59 +00:00
|
|
|
g_list_free_full (pending_frames,
|
|
|
|
(GDestroyNotify) gst_video_codec_frame_unref);
|
2017-05-03 00:21:43 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2019-07-18 15:52:59 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
nvdec->last_ret = GST_FLOW_ERROR;
|
|
|
|
return FALSE;
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 09:52:26 +00:00
|
|
|
static gboolean CUDAAPI
|
2017-05-03 00:21:43 +00:00
|
|
|
parser_display_callback (GstNvDec * nvdec, CUVIDPARSERDISPINFO * dispinfo)
|
|
|
|
{
|
2019-07-18 15:52:59 +00:00
|
|
|
GList *iter, *pending_frames;
|
|
|
|
GstVideoCodecFrame *frame = NULL;
|
|
|
|
GstBuffer *output_buffer = NULL;
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2020-03-15 10:20:47 +00:00
|
|
|
gboolean copy_ret = FALSE;
|
2017-05-03 00:21:43 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (nvdec, "picture index: %u", dispinfo->picture_index);
|
|
|
|
|
2019-07-18 15:52:59 +00:00
|
|
|
pending_frames = gst_video_decoder_get_frames (GST_VIDEO_DECODER (nvdec));
|
|
|
|
for (iter = pending_frames; iter; iter = g_list_next (iter)) {
|
|
|
|
guint id;
|
|
|
|
GstVideoCodecFrame *tmp = (GstVideoCodecFrame *) iter->data;
|
|
|
|
|
|
|
|
id = GPOINTER_TO_UINT (gst_video_codec_frame_get_user_data (tmp));
|
|
|
|
if (id == dispinfo->picture_index + 1) {
|
|
|
|
frame = gst_video_codec_frame_ref (tmp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_list_free_full (pending_frames,
|
|
|
|
(GDestroyNotify) gst_video_codec_frame_unref);
|
|
|
|
|
|
|
|
if (G_UNLIKELY (frame == NULL)) {
|
|
|
|
GST_WARNING_OBJECT (nvdec, "no frame for picture index %u",
|
|
|
|
dispinfo->picture_index);
|
|
|
|
|
|
|
|
output_buffer =
|
|
|
|
gst_video_decoder_allocate_output_buffer (GST_VIDEO_DECODER (nvdec));
|
|
|
|
|
|
|
|
if (!output_buffer) {
|
|
|
|
GST_ERROR_OBJECT (nvdec, "Couldn't allocate output buffer");
|
|
|
|
nvdec->last_ret = GST_FLOW_ERROR;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_BUFFER_PTS (output_buffer) = dispinfo->timestamp;
|
|
|
|
GST_BUFFER_DTS (output_buffer) = GST_CLOCK_TIME_NONE;
|
|
|
|
/* assume buffer duration from framerate */
|
2023-12-05 10:52:22 +00:00
|
|
|
if (nvdec->out_info.fps_n > 0 && nvdec->out_info.fps_d > 0) {
|
|
|
|
GST_BUFFER_DURATION (output_buffer) =
|
|
|
|
gst_util_uint64_scale (GST_SECOND,
|
|
|
|
GST_VIDEO_INFO_FPS_D (&nvdec->out_info),
|
|
|
|
GST_VIDEO_INFO_FPS_N (&nvdec->out_info));
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_DURATION (output_buffer) = GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
2019-07-18 15:52:59 +00:00
|
|
|
} else {
|
|
|
|
ret = gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (nvdec),
|
|
|
|
frame);
|
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to allocate output frame");
|
|
|
|
nvdec->last_ret = ret;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
output_buffer = frame->output_buffer;
|
|
|
|
|
|
|
|
if (dispinfo->timestamp != frame->pts) {
|
|
|
|
GST_INFO_OBJECT (nvdec,
|
|
|
|
"timestamp mismatch, diff: %" GST_STIME_FORMAT,
|
|
|
|
GST_STIME_ARGS (GST_CLOCK_DIFF (dispinfo->timestamp, frame->pts)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2019-07-19 13:46:01 +00:00
|
|
|
if (nvdec->mem_type == GST_NVDEC_MEM_TYPE_GL) {
|
|
|
|
copy_ret = gst_nvdec_copy_device_to_gl (nvdec, dispinfo, output_buffer);
|
2020-03-15 10:20:47 +00:00
|
|
|
|
|
|
|
/* FIXME: This is the case where OpenGL context of downstream glbufferpool
|
|
|
|
* belongs to non-nvidia (or different device).
|
|
|
|
* There should be enhancement to ensure nvdec has compatible OpenGL context
|
|
|
|
*/
|
|
|
|
if (!copy_ret) {
|
|
|
|
GST_WARNING_OBJECT (nvdec,
|
|
|
|
"Couldn't copy frame to GL memory, fallback to system memory");
|
|
|
|
nvdec->mem_type = GST_NVDEC_MEM_TYPE_SYSTEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!copy_ret)
|
2019-07-19 13:46:01 +00:00
|
|
|
#endif
|
|
|
|
{
|
2019-08-30 04:55:25 +00:00
|
|
|
copy_ret = gst_nvdec_copy_device_to_memory (nvdec, dispinfo, output_buffer);
|
2019-07-18 15:52:59 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 13:46:01 +00:00
|
|
|
if (!copy_ret) {
|
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to copy decoded picture to output buffer");
|
|
|
|
nvdec->last_ret = GST_FLOW_ERROR;
|
|
|
|
|
|
|
|
if (frame)
|
|
|
|
gst_video_decoder_drop_frame (GST_VIDEO_DECODER (nvdec), frame);
|
|
|
|
else
|
|
|
|
gst_buffer_unref (output_buffer);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2019-07-18 15:52:59 +00:00
|
|
|
|
|
|
|
if (!dispinfo->progressive_frame) {
|
|
|
|
GST_BUFFER_FLAG_SET (output_buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
|
|
|
|
|
|
|
|
if (dispinfo->top_field_first) {
|
|
|
|
GST_BUFFER_FLAG_SET (output_buffer, GST_VIDEO_BUFFER_FLAG_TFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dispinfo->repeat_first_field == -1) {
|
|
|
|
GST_BUFFER_FLAG_SET (output_buffer, GST_VIDEO_BUFFER_FLAG_ONEFIELD);
|
|
|
|
} else {
|
|
|
|
GST_BUFFER_FLAG_SET (output_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame) {
|
|
|
|
ret = gst_video_decoder_finish_frame (GST_VIDEO_DECODER (nvdec), frame);
|
|
|
|
} else {
|
|
|
|
ret = gst_pad_push (GST_VIDEO_DECODER_SRC_PAD (nvdec), output_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "failed to finish frame %s",
|
|
|
|
gst_flow_get_name (ret));
|
|
|
|
nvdec->last_ret = ret;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2017-05-03 00:21:43 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2019-07-19 13:46:01 +00:00
|
|
|
gst_nvdec_open (GstVideoDecoder * decoder)
|
2017-05-03 00:21:43 +00:00
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (decoder);
|
2019-07-21 12:23:30 +00:00
|
|
|
GstNvDecClass *klass = GST_NVDEC_GET_CLASS (nvdec);
|
2017-05-03 00:21:43 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "creating CUDA context");
|
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
if (!gst_cuda_ensure_element_context (GST_ELEMENT_CAST (decoder),
|
|
|
|
klass->cuda_device_id, &nvdec->cuda_ctx)) {
|
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to create CUDA context");
|
2017-05-03 00:21:43 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2019-08-18 13:07:38 +00:00
|
|
|
|
2022-12-21 14:59:19 +00:00
|
|
|
nvdec->stream = gst_cuda_stream_new (nvdec->cuda_ctx);
|
|
|
|
if (!nvdec->stream) {
|
|
|
|
GST_WARNING_OBJECT (nvdec,
|
|
|
|
"Could not create CUDA stream, will use default stream");
|
2019-08-18 13:07:38 +00:00
|
|
|
}
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2019-07-19 13:46:01 +00:00
|
|
|
gst_gl_ensure_element_data (GST_ELEMENT (nvdec),
|
|
|
|
&nvdec->gl_display, &nvdec->other_gl_context);
|
2019-08-17 05:03:57 +00:00
|
|
|
if (nvdec->gl_display)
|
|
|
|
gst_gl_display_filter_gl_api (GST_GL_DISPLAY (nvdec->gl_display),
|
|
|
|
SUPPORTED_GL_APIS);
|
2019-07-19 13:46:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-07-19 13:46:01 +00:00
|
|
|
static gboolean
|
|
|
|
gst_nvdec_start (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (decoder);
|
2022-03-09 17:28:11 +00:00
|
|
|
GstNvDecClass *klass = GST_NVDEC_GET_CLASS (nvdec);
|
2019-07-19 13:46:01 +00:00
|
|
|
|
|
|
|
nvdec->state = GST_NVDEC_STATE_INIT;
|
2019-07-18 15:52:59 +00:00
|
|
|
nvdec->last_ret = GST_FLOW_OK;
|
2019-08-09 09:47:14 +00:00
|
|
|
gst_video_info_init (&nvdec->out_info);
|
2019-07-18 15:52:59 +00:00
|
|
|
|
2022-03-09 17:28:11 +00:00
|
|
|
if (klass->codec_type == cudaVideoCodec_H264)
|
|
|
|
nvdec->h264_parser = gst_h264_nal_parser_new ();
|
|
|
|
else if (klass->codec_type == cudaVideoCodec_HEVC)
|
|
|
|
nvdec->h265_parser = gst_h265_parser_new ();
|
|
|
|
|
2017-05-03 00:21:43 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
maybe_destroy_decoder_and_parser (GstNvDec * nvdec)
|
|
|
|
{
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
if (!gst_cuda_context_push (nvdec->cuda_ctx)) {
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to lock CUDA context");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nvdec->decoder) {
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "destroying decoder");
|
2019-08-06 04:50:28 +00:00
|
|
|
ret = gst_cuda_result (CuvidDestroyDecoder (nvdec->decoder));
|
2019-08-18 13:14:37 +00:00
|
|
|
nvdec->decoder = NULL;
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-18 13:14:37 +00:00
|
|
|
if (!ret)
|
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to destroy decoder");
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nvdec->parser) {
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "destroying parser");
|
2019-08-06 04:50:28 +00:00
|
|
|
if (!gst_cuda_result (CuvidDestroyVideoParser (nvdec->parser))) {
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to destroy parser");
|
2019-08-18 13:14:37 +00:00
|
|
|
ret = FALSE;
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
nvdec->parser = NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-18 13:14:37 +00:00
|
|
|
if (!gst_cuda_context_pop (NULL)) {
|
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to pop CUDA context");
|
|
|
|
}
|
|
|
|
|
2017-05-03 00:21:43 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-09 17:28:11 +00:00
|
|
|
static void
|
|
|
|
gst_nvdec_clear_codec_data (GstNvDec * self)
|
|
|
|
{
|
|
|
|
GstNvDecClass *klass = GST_NVDEC_GET_CLASS (self);
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
if (klass->codec_type == cudaVideoCodec_HEVC) {
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (self->vps_nals); i++) {
|
|
|
|
gst_clear_buffer (&self->vps_nals[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (klass->codec_type == cudaVideoCodec_HEVC ||
|
|
|
|
klass->codec_type == cudaVideoCodec_H264) {
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (self->sps_nals); i++) {
|
|
|
|
gst_clear_buffer (&self->sps_nals[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (self->pps_nals); i++) {
|
|
|
|
gst_clear_buffer (&self->pps_nals[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_clear_buffer (&self->codec_data);
|
|
|
|
|
|
|
|
self->need_codec_data = TRUE;
|
|
|
|
}
|
|
|
|
|
2017-05-03 00:21:43 +00:00
|
|
|
static gboolean
|
|
|
|
gst_nvdec_stop (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (decoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "stop");
|
|
|
|
|
|
|
|
if (!maybe_destroy_decoder_and_parser (nvdec))
|
|
|
|
return FALSE;
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2022-03-09 17:28:11 +00:00
|
|
|
gst_clear_object (&nvdec->gl_context);
|
|
|
|
gst_clear_object (&nvdec->other_gl_context);
|
|
|
|
gst_clear_object (&nvdec->gl_display);
|
2019-07-19 13:46:01 +00:00
|
|
|
#endif
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2022-03-09 17:28:11 +00:00
|
|
|
g_clear_pointer (&nvdec->input_state, gst_video_codec_state_unref);
|
|
|
|
g_clear_pointer (&nvdec->output_state, gst_video_codec_state_unref);
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2022-03-09 17:28:11 +00:00
|
|
|
g_clear_pointer (&nvdec->h264_parser, gst_h264_nal_parser_free);
|
|
|
|
g_clear_pointer (&nvdec->h265_parser, gst_h265_parser_free);
|
2019-07-24 01:00:56 +00:00
|
|
|
|
2022-03-09 17:28:11 +00:00
|
|
|
gst_nvdec_clear_codec_data (nvdec);
|
2019-08-28 14:46:44 +00:00
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2019-07-21 12:23:30 +00:00
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
static gboolean
|
|
|
|
gst_nvdec_close (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (decoder);
|
|
|
|
|
2022-12-21 14:59:19 +00:00
|
|
|
gst_clear_cuda_stream (&nvdec->stream);
|
2019-08-06 04:44:20 +00:00
|
|
|
gst_clear_object (&nvdec->cuda_ctx);
|
2019-07-21 12:23:30 +00:00
|
|
|
|
2017-05-03 00:21:43 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_nvdec_set_format (GstVideoDecoder * decoder, GstVideoCodecState * state)
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (decoder);
|
2019-07-11 12:53:46 +00:00
|
|
|
GstNvDecClass *klass = GST_NVDEC_GET_CLASS (decoder);
|
2017-05-03 00:21:43 +00:00
|
|
|
CUVIDPARSERPARAMS parser_params = { 0, };
|
2020-10-02 13:22:34 +00:00
|
|
|
GstQuery *query;
|
2019-08-18 13:14:37 +00:00
|
|
|
gboolean ret = TRUE;
|
2017-05-03 00:21:43 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "set format");
|
|
|
|
|
|
|
|
if (nvdec->input_state)
|
|
|
|
gst_video_codec_state_unref (nvdec->input_state);
|
|
|
|
|
|
|
|
nvdec->input_state = gst_video_codec_state_ref (state);
|
|
|
|
|
|
|
|
if (!maybe_destroy_decoder_and_parser (nvdec))
|
|
|
|
return FALSE;
|
|
|
|
|
2020-10-02 13:22:34 +00:00
|
|
|
/* Check if pipeline is live */
|
|
|
|
nvdec->is_live = FALSE;
|
|
|
|
query = gst_query_new_latency ();
|
|
|
|
if (gst_pad_peer_query (GST_VIDEO_DECODER_SINK_PAD (decoder), query))
|
|
|
|
gst_query_parse_latency (query, &nvdec->is_live, NULL, NULL);
|
|
|
|
gst_query_unref (query);
|
|
|
|
|
2019-07-11 12:53:46 +00:00
|
|
|
parser_params.CodecType = klass->codec_type;
|
2020-04-09 07:12:58 +00:00
|
|
|
/* ulMaxNumDecodeSurfaces will be updated by the return value of
|
|
|
|
* SequenceCallback */
|
|
|
|
parser_params.ulMaxNumDecodeSurfaces = 1;
|
2017-05-03 00:21:43 +00:00
|
|
|
parser_params.ulErrorThreshold = 100;
|
2020-10-02 13:22:34 +00:00
|
|
|
parser_params.ulMaxDisplayDelay = gst_nvdec_get_max_display_delay (nvdec);
|
2017-05-03 00:21:43 +00:00
|
|
|
parser_params.ulClockRate = GST_SECOND;
|
|
|
|
parser_params.pUserData = nvdec;
|
|
|
|
parser_params.pfnSequenceCallback =
|
|
|
|
(PFNVIDSEQUENCECALLBACK) parser_sequence_callback;
|
|
|
|
parser_params.pfnDecodePicture =
|
|
|
|
(PFNVIDDECODECALLBACK) parser_decode_callback;
|
|
|
|
parser_params.pfnDisplayPicture =
|
|
|
|
(PFNVIDDISPLAYCALLBACK) parser_display_callback;
|
|
|
|
|
2019-08-18 13:14:37 +00:00
|
|
|
gst_cuda_context_push (nvdec->cuda_ctx);
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_DEBUG_OBJECT (nvdec, "creating parser");
|
2019-08-06 04:50:28 +00:00
|
|
|
if (!gst_cuda_result (CuvidCreateVideoParser (&nvdec->parser,
|
|
|
|
&parser_params))) {
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to create parser");
|
2019-08-18 13:14:37 +00:00
|
|
|
ret = FALSE;
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 13:14:37 +00:00
|
|
|
gst_cuda_context_pop (NULL);
|
|
|
|
|
2019-08-28 14:46:44 +00:00
|
|
|
/* store codec data */
|
2022-03-09 17:28:11 +00:00
|
|
|
gst_nvdec_clear_codec_data (nvdec);
|
|
|
|
|
2019-08-28 14:46:44 +00:00
|
|
|
if (ret && nvdec->input_state->caps) {
|
|
|
|
GstStructure *str;
|
|
|
|
|
|
|
|
str = gst_caps_get_structure (nvdec->input_state->caps, 0);
|
2022-03-09 17:28:11 +00:00
|
|
|
|
|
|
|
if (klass->codec_type == cudaVideoCodec_MPEG4) {
|
|
|
|
const GValue *codec_data_value;
|
|
|
|
codec_data_value = gst_structure_get_value (str, "codec_data");
|
|
|
|
if (codec_data_value && GST_VALUE_HOLDS_BUFFER (codec_data_value)) {
|
|
|
|
GstBuffer *codec_data = gst_value_get_buffer (codec_data_value);
|
|
|
|
gst_buffer_replace (&nvdec->codec_data, codec_data);
|
|
|
|
}
|
2019-08-28 14:46:44 +00:00
|
|
|
}
|
2020-01-22 15:26:02 +00:00
|
|
|
|
2021-08-20 17:20:11 +00:00
|
|
|
/* For all CODEC we get complete picture ... */
|
2020-01-22 15:26:02 +00:00
|
|
|
nvdec->recv_complete_picture = TRUE;
|
|
|
|
|
|
|
|
/* Except for JPEG, for which it depends on the caps */
|
|
|
|
if (klass->codec_type == cudaVideoCodec_JPEG) {
|
|
|
|
gboolean parsed;
|
|
|
|
if (gst_structure_get_boolean (str, "parsed", &parsed))
|
|
|
|
nvdec->recv_complete_picture = parsed;
|
|
|
|
else
|
|
|
|
nvdec->recv_complete_picture = FALSE;
|
|
|
|
}
|
2019-08-28 14:46:44 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 13:14:37 +00:00
|
|
|
return ret;
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2019-07-19 13:46:01 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec;
|
|
|
|
CUVIDPARSERDISPINFO *dispinfo;
|
|
|
|
gboolean ret;
|
2019-08-18 04:27:38 +00:00
|
|
|
GstBuffer *output_buffer;
|
2019-07-19 13:46:01 +00:00
|
|
|
} GstNvDecCopyToGLData;
|
|
|
|
|
2017-05-03 00:21:43 +00:00
|
|
|
static void
|
2019-07-19 13:46:01 +00:00
|
|
|
copy_video_frame_to_gl_textures (GstGLContext * context,
|
|
|
|
GstNvDecCopyToGLData * data)
|
2017-05-03 00:21:43 +00:00
|
|
|
{
|
2019-07-19 13:46:01 +00:00
|
|
|
GstNvDec *nvdec = data->nvdec;
|
|
|
|
CUVIDPARSERDISPINFO *dispinfo = data->dispinfo;
|
2019-08-18 04:27:38 +00:00
|
|
|
GstCudaGraphicsResource **resources;
|
|
|
|
guint num_resources;
|
2017-05-03 00:21:43 +00:00
|
|
|
CUVIDPROCPARAMS proc_params = { 0, };
|
nvdec,nvenc: Port to dynamic library loading
... and put them into new nvcodec plugin.
* nvcodec plugin
Now each nvenc and nvdec element is moved to be a part of nvcodec plugin
for better interoperability.
Additionally, cuda runtime API header dependencies
(i.e., cuda_runtime_api.h and cuda_gl_interop.h) are removed.
Note that cuda runtime APIs have prefix "cuda". Since 1.16 release with
Windows support, only "cuda.h" and "cudaGL.h" dependent symbols have
been used except for some defined types. However, those types could be
replaced with other types which were defined by "cuda.h".
* dynamic library loading
CUDA library will be opened with g_module_open() instead of build-time linking.
On Windows, nvcuda.dll is installed to system path by CUDA Toolkit
installer, and on *nix, user should ensure that libcuda.so.1 can be
loadable (i.e., via LD_LIBRARY_PATH or default dlopen path)
Therefore, NVIDIA_VIDEO_CODEC_SDK_PATH env build time dependency for Windows
is removed.
2019-05-17 13:27:50 +00:00
|
|
|
guintptr dptr;
|
2017-05-03 00:21:43 +00:00
|
|
|
guint pitch, i;
|
|
|
|
CUDA_MEMCPY2D mcpy2d = { 0, };
|
2019-07-24 01:00:56 +00:00
|
|
|
GstVideoInfo *info = &nvdec->output_state->info;
|
2022-12-21 14:59:19 +00:00
|
|
|
CUstream stream = gst_cuda_stream_get_handle (nvdec->stream);
|
2017-05-03 00:21:43 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (nvdec, "picture index: %u", dispinfo->picture_index);
|
|
|
|
|
|
|
|
proc_params.progressive_frame = dispinfo->progressive_frame;
|
|
|
|
proc_params.top_field_first = dispinfo->top_field_first;
|
|
|
|
proc_params.unpaired_field = dispinfo->repeat_first_field == -1;
|
2022-12-21 14:59:19 +00:00
|
|
|
proc_params.output_stream = stream;
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-07-19 13:46:01 +00:00
|
|
|
data->ret = TRUE;
|
|
|
|
|
2019-08-18 04:27:38 +00:00
|
|
|
num_resources = gst_buffer_n_memory (data->output_buffer);
|
|
|
|
resources = g_newa (GstCudaGraphicsResource *, num_resources);
|
|
|
|
|
|
|
|
for (i = 0; i < num_resources; i++) {
|
|
|
|
GstMemory *mem;
|
|
|
|
|
|
|
|
mem = gst_buffer_peek_memory (data->output_buffer, i);
|
|
|
|
resources[i] = ensure_cuda_graphics_resource (mem, nvdec);
|
|
|
|
if (!resources[i]) {
|
|
|
|
GST_WARNING_OBJECT (nvdec, "could not register %dth memory", i);
|
|
|
|
data->ret = FALSE;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Need PBO -> texture */
|
|
|
|
GST_MINI_OBJECT_FLAG_SET (mem, GST_GL_BASE_MEMORY_TRANSFER_NEED_UPLOAD);
|
|
|
|
}
|
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
if (!gst_cuda_context_push (nvdec->cuda_ctx)) {
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to lock CUDA context");
|
2019-07-19 13:46:01 +00:00
|
|
|
data->ret = FALSE;
|
2017-05-03 00:21:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-06 04:50:28 +00:00
|
|
|
if (!gst_cuda_result (CuvidMapVideoFrame (nvdec->decoder,
|
|
|
|
dispinfo->picture_index, &dptr, &pitch, &proc_params))) {
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to map CUDA video frame");
|
2019-07-19 13:46:01 +00:00
|
|
|
data->ret = FALSE;
|
2017-05-03 00:21:43 +00:00
|
|
|
goto unlock_cuda_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
mcpy2d.srcMemoryType = CU_MEMORYTYPE_DEVICE;
|
|
|
|
mcpy2d.srcPitch = pitch;
|
2019-08-17 05:46:00 +00:00
|
|
|
mcpy2d.dstMemoryType = CU_MEMORYTYPE_DEVICE;
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2017-06-28 01:47:37 +00:00
|
|
|
for (i = 0; i < num_resources; i++) {
|
2019-08-17 05:46:00 +00:00
|
|
|
CUdeviceptr cuda_ptr;
|
|
|
|
gsize size;
|
2019-08-18 04:27:38 +00:00
|
|
|
CUgraphicsResource cuda_resource =
|
2022-12-21 14:59:19 +00:00
|
|
|
gst_cuda_graphics_resource_map (resources[i], stream,
|
2019-08-18 04:27:38 +00:00
|
|
|
CU_GRAPHICS_MAP_RESOURCE_FLAGS_WRITE_DISCARD);
|
|
|
|
|
|
|
|
if (!cuda_resource) {
|
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to map CUDA resources");
|
|
|
|
data->ret = FALSE;
|
|
|
|
goto unmap_video_frame;
|
|
|
|
}
|
2019-08-17 05:46:00 +00:00
|
|
|
|
|
|
|
if (!gst_cuda_result (CuGraphicsResourceGetMappedPointer (&cuda_ptr, &size,
|
2019-08-18 04:27:38 +00:00
|
|
|
cuda_resource))) {
|
2019-08-17 05:46:00 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to map CUDA resource");
|
2019-07-19 13:46:01 +00:00
|
|
|
data->ret = FALSE;
|
2017-05-03 00:21:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-17 05:46:00 +00:00
|
|
|
mcpy2d.dstPitch = GST_VIDEO_INFO_PLANE_STRIDE (info, i);
|
|
|
|
mcpy2d.WidthInBytes = GST_VIDEO_INFO_COMP_WIDTH (info, i)
|
|
|
|
* GST_VIDEO_INFO_COMP_PSTRIDE (info, i);
|
|
|
|
|
2019-08-09 09:47:14 +00:00
|
|
|
mcpy2d.srcDevice = dptr + (i * pitch * GST_VIDEO_INFO_HEIGHT (info));
|
2019-08-17 05:46:00 +00:00
|
|
|
mcpy2d.dstDevice = cuda_ptr;
|
2019-07-19 13:46:01 +00:00
|
|
|
mcpy2d.Height = GST_VIDEO_INFO_COMP_HEIGHT (info, i);
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2022-12-21 14:59:19 +00:00
|
|
|
if (!gst_cuda_result (CuMemcpy2DAsync (&mcpy2d, stream))) {
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "memcpy to mapped array failed");
|
2019-07-19 13:46:01 +00:00
|
|
|
data->ret = FALSE;
|
|
|
|
}
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 14:59:19 +00:00
|
|
|
gst_cuda_result (CuStreamSynchronize (stream));
|
2019-08-18 13:07:38 +00:00
|
|
|
|
2017-05-03 00:21:43 +00:00
|
|
|
unmap_video_frame:
|
2019-08-18 04:27:38 +00:00
|
|
|
for (i = 0; i < num_resources; i++) {
|
2022-12-21 14:59:19 +00:00
|
|
|
gst_cuda_graphics_resource_unmap (resources[i], stream);
|
2019-08-18 04:27:38 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 04:50:28 +00:00
|
|
|
if (!gst_cuda_result (CuvidUnmapVideoFrame (nvdec->decoder, dptr)))
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to unmap CUDA video frame");
|
|
|
|
|
|
|
|
unlock_cuda_context:
|
2019-08-06 04:44:20 +00:00
|
|
|
if (!gst_cuda_context_pop (NULL))
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to unlock CUDA context");
|
|
|
|
}
|
|
|
|
|
2019-07-19 13:46:01 +00:00
|
|
|
static gboolean
|
|
|
|
gst_nvdec_copy_device_to_gl (GstNvDec * nvdec,
|
|
|
|
CUVIDPARSERDISPINFO * dispinfo, GstBuffer * output_buffer)
|
|
|
|
{
|
|
|
|
GstNvDecCopyToGLData data = { 0, };
|
|
|
|
|
|
|
|
data.nvdec = nvdec;
|
|
|
|
data.dispinfo = dispinfo;
|
2019-08-18 04:27:38 +00:00
|
|
|
data.output_buffer = output_buffer;
|
2019-07-19 13:46:01 +00:00
|
|
|
|
|
|
|
gst_gl_context_thread_add (nvdec->gl_context,
|
|
|
|
(GstGLContextThreadFunc) copy_video_frame_to_gl_textures, &data);
|
|
|
|
|
|
|
|
return data.ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static gboolean
|
2019-08-30 04:55:25 +00:00
|
|
|
gst_nvdec_copy_device_to_memory (GstNvDec * nvdec,
|
2019-07-19 13:46:01 +00:00
|
|
|
CUVIDPARSERDISPINFO * dispinfo, GstBuffer * output_buffer)
|
|
|
|
{
|
|
|
|
CUVIDPROCPARAMS params = { 0, };
|
|
|
|
CUDA_MEMCPY2D copy_params = { 0, };
|
|
|
|
guintptr dptr;
|
|
|
|
guint pitch;
|
|
|
|
GstVideoFrame video_frame;
|
|
|
|
GstVideoInfo *info = &nvdec->output_state->info;
|
|
|
|
gint i;
|
2019-08-30 04:55:25 +00:00
|
|
|
GstMemory *mem;
|
2022-03-02 18:25:47 +00:00
|
|
|
gboolean use_device_copy = FALSE;
|
2022-12-20 15:13:56 +00:00
|
|
|
gboolean need_sync = TRUE;
|
2022-03-02 18:25:47 +00:00
|
|
|
GstMapFlags map_flags = GST_MAP_WRITE;
|
2022-12-21 14:59:19 +00:00
|
|
|
CUstream stream = gst_cuda_stream_get_handle (nvdec->stream);
|
2019-07-19 13:46:01 +00:00
|
|
|
|
2019-08-30 04:55:25 +00:00
|
|
|
if (nvdec->mem_type == GST_NVDEC_MEM_TYPE_CUDA &&
|
|
|
|
(mem = gst_buffer_peek_memory (output_buffer, 0)) &&
|
|
|
|
gst_is_cuda_memory (mem)) {
|
2022-12-20 15:13:56 +00:00
|
|
|
GstCudaStream *mem_stream;
|
|
|
|
|
2022-03-02 18:25:47 +00:00
|
|
|
map_flags |= GST_MAP_CUDA;
|
|
|
|
use_device_copy = TRUE;
|
2022-12-20 15:13:56 +00:00
|
|
|
|
|
|
|
mem_stream = gst_cuda_memory_get_stream (GST_CUDA_MEMORY_CAST (mem));
|
|
|
|
if (mem_stream) {
|
|
|
|
stream = gst_cuda_stream_get_handle (mem_stream);
|
|
|
|
need_sync = FALSE;
|
|
|
|
}
|
2019-08-30 04:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:25:47 +00:00
|
|
|
if (!gst_video_frame_map (&video_frame, info, output_buffer, map_flags)) {
|
|
|
|
GST_ERROR_OBJECT (nvdec, "frame map failure");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_cuda_context_push (nvdec->cuda_ctx)) {
|
|
|
|
gst_video_frame_unmap (&video_frame);
|
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to lock CUDA context");
|
|
|
|
return FALSE;
|
2019-07-19 13:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params.progressive_frame = dispinfo->progressive_frame;
|
|
|
|
params.second_field = dispinfo->repeat_first_field + 1;
|
|
|
|
params.top_field_first = dispinfo->top_field_first;
|
|
|
|
params.unpaired_field = dispinfo->repeat_first_field < 0;
|
2022-12-21 14:59:19 +00:00
|
|
|
params.output_stream = stream;
|
2019-07-19 13:46:01 +00:00
|
|
|
|
2019-08-06 04:50:28 +00:00
|
|
|
if (!gst_cuda_result (CuvidMapVideoFrame (nvdec->decoder,
|
2019-07-19 13:46:01 +00:00
|
|
|
dispinfo->picture_index, &dptr, &pitch, ¶ms))) {
|
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to map video frame");
|
2019-08-06 04:44:20 +00:00
|
|
|
gst_cuda_context_pop (NULL);
|
2019-07-19 13:46:01 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_params.srcMemoryType = CU_MEMORYTYPE_DEVICE;
|
|
|
|
copy_params.srcPitch = pitch;
|
2019-08-30 04:55:25 +00:00
|
|
|
copy_params.dstMemoryType =
|
2022-03-02 18:25:47 +00:00
|
|
|
use_device_copy ? CU_MEMORYTYPE_DEVICE : CU_MEMORYTYPE_HOST;
|
2019-07-19 13:46:01 +00:00
|
|
|
|
2019-08-30 04:55:25 +00:00
|
|
|
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
|
2019-08-09 09:47:14 +00:00
|
|
|
copy_params.srcDevice = dptr + (i * pitch * GST_VIDEO_INFO_HEIGHT (info));
|
2022-03-02 18:25:47 +00:00
|
|
|
if (use_device_copy) {
|
|
|
|
copy_params.dstDevice =
|
|
|
|
(CUdeviceptr) GST_VIDEO_FRAME_PLANE_DATA (&video_frame, i);
|
2019-08-30 04:55:25 +00:00
|
|
|
} else {
|
|
|
|
copy_params.dstHost = GST_VIDEO_FRAME_PLANE_DATA (&video_frame, i);
|
|
|
|
}
|
2022-03-02 18:25:47 +00:00
|
|
|
copy_params.dstPitch = GST_VIDEO_FRAME_PLANE_STRIDE (&video_frame, i);
|
2019-08-30 04:55:25 +00:00
|
|
|
copy_params.WidthInBytes = GST_VIDEO_INFO_COMP_WIDTH (info, i)
|
|
|
|
* GST_VIDEO_INFO_COMP_PSTRIDE (info, i);
|
|
|
|
copy_params.Height = GST_VIDEO_INFO_COMP_HEIGHT (info, i);
|
2019-07-19 13:46:01 +00:00
|
|
|
|
2022-12-21 14:59:19 +00:00
|
|
|
if (!gst_cuda_result (CuMemcpy2DAsync (©_params, stream))) {
|
2019-07-19 13:46:01 +00:00
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to copy %dth plane", i);
|
|
|
|
CuvidUnmapVideoFrame (nvdec->decoder, dptr);
|
2022-03-02 18:25:47 +00:00
|
|
|
gst_video_frame_unmap (&video_frame);
|
2019-08-06 04:44:20 +00:00
|
|
|
gst_cuda_context_pop (NULL);
|
2019-07-19 13:46:01 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 15:13:56 +00:00
|
|
|
if (need_sync)
|
|
|
|
gst_cuda_result (CuStreamSynchronize (stream));
|
2019-07-19 13:46:01 +00:00
|
|
|
|
2022-03-02 18:25:47 +00:00
|
|
|
gst_video_frame_unmap (&video_frame);
|
2019-07-19 13:46:01 +00:00
|
|
|
|
2019-08-06 04:50:28 +00:00
|
|
|
if (!gst_cuda_result (CuvidUnmapVideoFrame (nvdec->decoder, dptr)))
|
2019-07-19 13:46:01 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to unmap video frame");
|
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
if (!gst_cuda_context_pop (NULL))
|
2019-07-19 13:46:01 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "failed to unlock CUDA context");
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-03-09 17:28:11 +00:00
|
|
|
static void
|
|
|
|
gst_nvdec_store_h264_nal (GstNvDec * self, guint id,
|
|
|
|
GstH264NalUnitType nal_type, GstH264NalUnit * nalu)
|
|
|
|
{
|
|
|
|
GstBuffer *buf, **store;
|
|
|
|
guint size = nalu->size, store_size;
|
|
|
|
static const guint8 start_code[] = { 0, 0, 1 };
|
|
|
|
|
|
|
|
if (nal_type == GST_H264_NAL_SPS || nal_type == GST_H264_NAL_SUBSET_SPS) {
|
|
|
|
store_size = GST_H264_MAX_SPS_COUNT;
|
|
|
|
store = self->sps_nals;
|
|
|
|
GST_DEBUG_OBJECT (self, "storing sps %u", id);
|
|
|
|
} else if (nal_type == GST_H264_NAL_PPS) {
|
|
|
|
store_size = GST_H264_MAX_PPS_COUNT;
|
|
|
|
store = self->pps_nals;
|
|
|
|
GST_DEBUG_OBJECT (self, "storing pps %u", id);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id >= store_size) {
|
|
|
|
GST_DEBUG_OBJECT (self, "unable to store nal, id out-of-range %d", id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = gst_buffer_new_allocate (NULL, size + sizeof (start_code), NULL);
|
|
|
|
gst_buffer_fill (buf, 0, start_code, sizeof (start_code));
|
|
|
|
gst_buffer_fill (buf, sizeof (start_code), nalu->data + nalu->offset, size);
|
|
|
|
|
|
|
|
if (store[id])
|
|
|
|
gst_buffer_unref (store[id]);
|
|
|
|
|
|
|
|
store[id] = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstBuffer *
|
|
|
|
gst_nvdec_handle_h264_buffer (GstNvDec * self, GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstH264NalParser *parser = self->h264_parser;
|
|
|
|
GstH264NalUnit nalu;
|
|
|
|
GstH264ParserResult pres;
|
|
|
|
GstMapInfo map;
|
|
|
|
gboolean have_sps = FALSE;
|
|
|
|
gboolean have_pps = FALSE;
|
|
|
|
guint i;
|
|
|
|
GstBuffer *new_buf;
|
|
|
|
|
|
|
|
if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
|
|
|
|
GST_WARNING_OBJECT (self, "Failed to map input buffer");
|
|
|
|
return gst_buffer_ref (buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset (&nalu, 0, sizeof (GstH264NalUnit));
|
|
|
|
|
|
|
|
do {
|
|
|
|
pres = gst_h264_parser_identify_nalu (parser,
|
|
|
|
map.data, nalu.offset + nalu.size, map.size, &nalu);
|
|
|
|
|
|
|
|
if (pres == GST_H264_PARSER_NO_NAL_END)
|
|
|
|
pres = GST_H264_PARSER_OK;
|
|
|
|
|
|
|
|
switch (nalu.type) {
|
|
|
|
case GST_H264_NAL_SPS:
|
|
|
|
case GST_H264_NAL_SUBSET_SPS:{
|
|
|
|
GstH264SPS sps;
|
|
|
|
|
|
|
|
if (nalu.type == GST_H264_NAL_SPS) {
|
|
|
|
pres = gst_h264_parser_parse_sps (parser, &nalu, &sps);
|
|
|
|
} else {
|
|
|
|
pres = gst_h264_parser_parse_subset_sps (parser, &nalu, &sps);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pres != GST_H264_PARSER_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
have_sps = TRUE;
|
|
|
|
gst_nvdec_store_h264_nal (self, sps.id, nalu.type, &nalu);
|
|
|
|
gst_h264_sps_clear (&sps);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_H264_NAL_PPS:{
|
|
|
|
GstH264PPS pps;
|
|
|
|
|
|
|
|
pres = gst_h264_parser_parse_pps (parser, &nalu, &pps);
|
|
|
|
if (pres != GST_H264_PARSER_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
have_pps = TRUE;
|
|
|
|
gst_nvdec_store_h264_nal (self, pps.id, nalu.type, &nalu);
|
|
|
|
gst_h264_pps_clear (&pps);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (pres == GST_H264_PARSER_OK);
|
|
|
|
|
|
|
|
gst_buffer_unmap (buffer, &map);
|
|
|
|
|
|
|
|
if (!self->need_codec_data || (have_sps && have_pps)) {
|
|
|
|
self->need_codec_data = FALSE;
|
|
|
|
return gst_buffer_ref (buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
new_buf = gst_buffer_new ();
|
|
|
|
if (!have_sps) {
|
|
|
|
for (i = 0; i < GST_H264_MAX_SPS_COUNT; i++) {
|
|
|
|
if (!self->sps_nals[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
have_sps = TRUE;
|
|
|
|
new_buf = gst_buffer_append (new_buf, gst_buffer_ref (self->sps_nals[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!have_pps) {
|
|
|
|
for (i = 0; i < GST_H264_MAX_PPS_COUNT; i++) {
|
|
|
|
if (!self->pps_nals[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
have_pps = TRUE;
|
|
|
|
new_buf = gst_buffer_append (new_buf, gst_buffer_ref (self->pps_nals[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new_buf = gst_buffer_append (new_buf, gst_buffer_ref (buffer));
|
|
|
|
|
|
|
|
if (have_sps && have_pps)
|
|
|
|
self->need_codec_data = FALSE;
|
|
|
|
|
|
|
|
return new_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_nvdec_store_h265_nal (GstNvDec * self, guint id,
|
|
|
|
GstH265NalUnitType nal_type, GstH265NalUnit * nalu)
|
|
|
|
{
|
|
|
|
GstBuffer *buf, **store;
|
|
|
|
guint size = nalu->size, store_size;
|
|
|
|
static const guint8 start_code[] = { 0, 0, 1 };
|
|
|
|
|
|
|
|
if (nal_type == GST_H265_NAL_VPS) {
|
|
|
|
store_size = GST_H265_MAX_VPS_COUNT;
|
|
|
|
store = self->vps_nals;
|
|
|
|
GST_DEBUG_OBJECT (self, "storing vps %u", id);
|
|
|
|
} else if (nal_type == GST_H265_NAL_SPS) {
|
|
|
|
store_size = GST_H265_MAX_SPS_COUNT;
|
|
|
|
store = self->sps_nals;
|
|
|
|
GST_DEBUG_OBJECT (self, "storing sps %u", id);
|
|
|
|
} else if (nal_type == GST_H265_NAL_PPS) {
|
|
|
|
store_size = GST_H265_MAX_PPS_COUNT;
|
|
|
|
store = self->pps_nals;
|
|
|
|
GST_DEBUG_OBJECT (self, "storing pps %u", id);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id >= store_size) {
|
|
|
|
GST_DEBUG_OBJECT (self, "unable to store nal, id out-of-range %d", id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = gst_buffer_new_allocate (NULL, size + sizeof (start_code), NULL);
|
|
|
|
gst_buffer_fill (buf, 0, start_code, sizeof (start_code));
|
|
|
|
gst_buffer_fill (buf, sizeof (start_code), nalu->data + nalu->offset, size);
|
|
|
|
|
|
|
|
if (store[id])
|
|
|
|
gst_buffer_unref (store[id]);
|
|
|
|
|
|
|
|
store[id] = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstBuffer *
|
|
|
|
gst_nvdec_handle_h265_buffer (GstNvDec * self, GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstH265Parser *parser = self->h265_parser;
|
|
|
|
GstH265NalUnit nalu;
|
|
|
|
GstH265ParserResult pres;
|
|
|
|
GstMapInfo map;
|
|
|
|
gboolean have_vps = FALSE;
|
|
|
|
gboolean have_sps = FALSE;
|
|
|
|
gboolean have_pps = FALSE;
|
|
|
|
GstBuffer *new_buf;
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
if (!gst_buffer_map (buffer, &map, GST_MAP_READ)) {
|
|
|
|
GST_WARNING_OBJECT (self, "Failed to map input buffer");
|
|
|
|
return gst_buffer_ref (buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset (&nalu, 0, sizeof (GstH265NalUnit));
|
|
|
|
|
|
|
|
do {
|
|
|
|
pres = gst_h265_parser_identify_nalu (parser,
|
|
|
|
map.data, nalu.offset + nalu.size, map.size, &nalu);
|
|
|
|
|
|
|
|
if (pres == GST_H265_PARSER_NO_NAL_END)
|
|
|
|
pres = GST_H265_PARSER_OK;
|
|
|
|
|
|
|
|
switch (nalu.type) {
|
|
|
|
case GST_H265_NAL_VPS:{
|
|
|
|
GstH265VPS vps;
|
|
|
|
|
|
|
|
pres = gst_h265_parser_parse_vps (parser, &nalu, &vps);
|
|
|
|
if (pres != GST_H265_PARSER_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
have_vps = TRUE;
|
|
|
|
gst_nvdec_store_h265_nal (self, vps.id, nalu.type, &nalu);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_H265_NAL_SPS:{
|
|
|
|
GstH265SPS sps;
|
|
|
|
|
|
|
|
pres = gst_h265_parser_parse_sps (parser, &nalu, &sps, FALSE);
|
|
|
|
if (pres != GST_H265_PARSER_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
have_sps = TRUE;
|
|
|
|
gst_nvdec_store_h265_nal (self, sps.id, nalu.type, &nalu);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_H265_NAL_PPS:{
|
|
|
|
GstH265PPS pps;
|
|
|
|
|
|
|
|
pres = gst_h265_parser_parse_pps (parser, &nalu, &pps);
|
|
|
|
if (pres != GST_H265_PARSER_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
have_pps = TRUE;
|
|
|
|
gst_nvdec_store_h265_nal (self, pps.id, nalu.type, &nalu);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (pres == GST_H265_PARSER_OK);
|
|
|
|
|
|
|
|
gst_buffer_unmap (buffer, &map);
|
|
|
|
|
|
|
|
if (!self->need_codec_data || (have_sps && have_pps)) {
|
|
|
|
self->need_codec_data = FALSE;
|
|
|
|
return gst_buffer_ref (buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
new_buf = gst_buffer_new ();
|
|
|
|
if (!have_vps) {
|
|
|
|
for (i = 0; i < GST_H265_MAX_VPS_COUNT; i++) {
|
|
|
|
if (!self->vps_nals[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
new_buf = gst_buffer_append (new_buf, gst_buffer_ref (self->vps_nals[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!have_sps) {
|
|
|
|
for (i = 0; i < GST_H265_MAX_SPS_COUNT; i++) {
|
|
|
|
if (!self->sps_nals[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
have_sps = TRUE;
|
|
|
|
new_buf = gst_buffer_append (new_buf, gst_buffer_ref (self->sps_nals[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!have_pps) {
|
|
|
|
for (i = 0; i < GST_H265_MAX_PPS_COUNT; i++) {
|
|
|
|
if (!self->pps_nals[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
have_pps = TRUE;
|
|
|
|
new_buf = gst_buffer_append (new_buf, gst_buffer_ref (self->pps_nals[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_sps && have_pps)
|
|
|
|
self->need_codec_data = FALSE;
|
|
|
|
|
|
|
|
return gst_buffer_append (new_buf, gst_buffer_ref (buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstBuffer *
|
|
|
|
gst_nvdec_process_input (GstNvDec * self, GstBuffer * inbuf)
|
|
|
|
{
|
|
|
|
GstNvDecClass *klass = GST_NVDEC_GET_CLASS (self);
|
|
|
|
gboolean parse_nal = FALSE;
|
|
|
|
|
|
|
|
if (!GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_DELTA_UNIT) ||
|
|
|
|
self->need_codec_data) {
|
|
|
|
parse_nal = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (klass->codec_type == cudaVideoCodec_MPEG4 &&
|
|
|
|
self->codec_data && GST_BUFFER_IS_DISCONT (inbuf)) {
|
|
|
|
return gst_buffer_append (gst_buffer_ref (self->codec_data),
|
|
|
|
gst_buffer_ref (inbuf));
|
|
|
|
} else if (klass->codec_type == cudaVideoCodec_H264 && parse_nal) {
|
|
|
|
return gst_nvdec_handle_h264_buffer (self, inbuf);
|
|
|
|
} else if (klass->codec_type == cudaVideoCodec_HEVC && parse_nal) {
|
|
|
|
return gst_nvdec_handle_h265_buffer (self, inbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return gst_buffer_ref (inbuf);
|
|
|
|
}
|
|
|
|
|
2017-05-03 00:21:43 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_nvdec_handle_frame (GstVideoDecoder * decoder, GstVideoCodecFrame * frame)
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (decoder);
|
|
|
|
GstMapInfo map_info = GST_MAP_INFO_INIT;
|
|
|
|
CUVIDSOURCEDATAPACKET packet = { 0, };
|
2019-08-28 14:46:44 +00:00
|
|
|
GstBuffer *in_buffer;
|
2017-05-03 00:21:43 +00:00
|
|
|
|
|
|
|
GST_LOG_OBJECT (nvdec, "handle frame");
|
|
|
|
|
2019-07-18 15:52:59 +00:00
|
|
|
/* initialize with zero to keep track of frames */
|
2017-05-03 00:21:43 +00:00
|
|
|
gst_video_codec_frame_set_user_data (frame, GUINT_TO_POINTER (0), NULL);
|
|
|
|
|
2022-03-09 17:28:11 +00:00
|
|
|
in_buffer = gst_nvdec_process_input (nvdec, frame->input_buffer);
|
2019-08-28 14:46:44 +00:00
|
|
|
|
|
|
|
if (!gst_buffer_map (in_buffer, &map_info, GST_MAP_READ)) {
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to map input buffer");
|
2019-08-28 14:46:44 +00:00
|
|
|
gst_buffer_unref (in_buffer);
|
2017-05-03 00:21:43 +00:00
|
|
|
gst_video_codec_frame_unref (frame);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet.payload_size = (gulong) map_info.size;
|
|
|
|
packet.payload = map_info.data;
|
|
|
|
packet.timestamp = frame->pts;
|
2019-08-28 14:46:44 +00:00
|
|
|
packet.flags |= CUVID_PKT_TIMESTAMP;
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2020-01-22 15:26:02 +00:00
|
|
|
if (nvdec->recv_complete_picture)
|
|
|
|
packet.flags |= CUVID_PKT_ENDOFPICTURE;
|
|
|
|
|
2019-07-18 16:07:38 +00:00
|
|
|
nvdec->state = GST_NVDEC_STATE_PARSE;
|
2019-08-29 08:19:27 +00:00
|
|
|
nvdec->last_ret = GST_FLOW_OK;
|
2019-07-18 16:07:38 +00:00
|
|
|
|
2019-08-06 04:50:28 +00:00
|
|
|
if (!gst_cuda_result (CuvidParseVideoData (nvdec->parser, &packet)))
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "parser failed");
|
|
|
|
|
2019-08-28 14:46:44 +00:00
|
|
|
gst_buffer_unmap (in_buffer, &map_info);
|
|
|
|
gst_buffer_unref (in_buffer);
|
2017-05-03 00:21:43 +00:00
|
|
|
gst_video_codec_frame_unref (frame);
|
|
|
|
|
2019-07-18 15:52:59 +00:00
|
|
|
return nvdec->last_ret;
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 06:09:22 +00:00
|
|
|
static gboolean
|
|
|
|
gst_nvdec_flush (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (decoder);
|
|
|
|
CUVIDSOURCEDATAPACKET packet = { 0, };
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "flush");
|
|
|
|
|
|
|
|
packet.payload_size = 0;
|
|
|
|
packet.payload = NULL;
|
|
|
|
packet.flags = CUVID_PKT_ENDOFSTREAM;
|
|
|
|
|
2019-07-18 16:07:38 +00:00
|
|
|
nvdec->state = GST_NVDEC_STATE_PARSE;
|
2019-08-29 08:19:27 +00:00
|
|
|
nvdec->last_ret = GST_FLOW_OK;
|
2019-07-18 16:07:38 +00:00
|
|
|
|
2019-08-06 04:50:28 +00:00
|
|
|
if (nvdec->parser
|
|
|
|
&& !gst_cuda_result (CuvidParseVideoData (nvdec->parser, &packet)))
|
2017-11-17 06:09:22 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "parser failed");
|
|
|
|
|
2022-03-09 17:28:11 +00:00
|
|
|
nvdec->need_codec_data = TRUE;
|
|
|
|
|
2017-11-17 06:09:22 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_nvdec_drain (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (decoder);
|
|
|
|
CUVIDSOURCEDATAPACKET packet = { 0, };
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "draining decoder");
|
|
|
|
|
|
|
|
packet.payload_size = 0;
|
|
|
|
packet.payload = NULL;
|
|
|
|
packet.flags = CUVID_PKT_ENDOFSTREAM;
|
|
|
|
|
2019-08-18 06:45:37 +00:00
|
|
|
nvdec->state = GST_NVDEC_STATE_PARSE;
|
2019-08-29 08:19:27 +00:00
|
|
|
nvdec->last_ret = GST_FLOW_OK;
|
2019-08-18 06:45:37 +00:00
|
|
|
|
2019-08-06 04:50:28 +00:00
|
|
|
if (nvdec->parser
|
|
|
|
&& !gst_cuda_result (CuvidParseVideoData (nvdec->parser, &packet)))
|
2017-11-17 06:09:22 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "parser failed");
|
|
|
|
|
2022-03-09 17:28:11 +00:00
|
|
|
nvdec->need_codec_data = TRUE;
|
|
|
|
|
2019-07-18 15:52:59 +00:00
|
|
|
return nvdec->last_ret;
|
2017-11-17 06:09:22 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 03:37:43 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_nvdec_finish (GstVideoDecoder * decoder)
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (decoder, "finish");
|
|
|
|
|
|
|
|
return gst_nvdec_drain (decoder);
|
|
|
|
}
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2020-03-15 10:20:47 +00:00
|
|
|
static void
|
|
|
|
gst_nvdec_check_cuda_device_from_context (GstGLContext * context,
|
|
|
|
gboolean * ret)
|
|
|
|
{
|
|
|
|
guint device_count = 0;
|
|
|
|
CUdevice device_list[1] = { 0, };
|
|
|
|
CUresult cuda_ret;
|
|
|
|
|
|
|
|
*ret = FALSE;
|
|
|
|
|
|
|
|
cuda_ret = CuGLGetDevices (&device_count,
|
|
|
|
device_list, 1, CU_GL_DEVICE_LIST_ALL);
|
|
|
|
|
|
|
|
if (!gst_cuda_result (cuda_ret) || device_count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*ret = TRUE;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-03 00:21:43 +00:00
|
|
|
static gboolean
|
2019-08-29 11:20:14 +00:00
|
|
|
gst_nvdec_ensure_gl_context (GstNvDec * nvdec)
|
2017-05-03 00:21:43 +00:00
|
|
|
{
|
2020-03-15 10:20:47 +00:00
|
|
|
gboolean ret;
|
|
|
|
|
2019-08-29 11:20:14 +00:00
|
|
|
if (!nvdec->gl_display) {
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "No available OpenGL display");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-29 11:20:14 +00:00
|
|
|
if (!gst_gl_query_local_gl_context (GST_ELEMENT (nvdec), GST_PAD_SRC,
|
2017-05-03 00:21:43 +00:00
|
|
|
&nvdec->gl_context)) {
|
|
|
|
GST_INFO_OBJECT (nvdec, "failed to query local OpenGL context");
|
|
|
|
if (nvdec->gl_context)
|
|
|
|
gst_object_unref (nvdec->gl_context);
|
|
|
|
nvdec->gl_context =
|
|
|
|
gst_gl_display_get_gl_context_for_thread (nvdec->gl_display, NULL);
|
|
|
|
if (!nvdec->gl_context
|
|
|
|
|| !gst_gl_display_add_context (nvdec->gl_display, nvdec->gl_context)) {
|
|
|
|
if (nvdec->gl_context)
|
|
|
|
gst_object_unref (nvdec->gl_context);
|
|
|
|
if (!gst_gl_display_create_context (nvdec->gl_display,
|
|
|
|
nvdec->other_gl_context, &nvdec->gl_context, NULL)) {
|
|
|
|
GST_ERROR_OBJECT (nvdec, "failed to create OpenGL context");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!gst_gl_display_add_context (nvdec->gl_display, nvdec->gl_context)) {
|
|
|
|
GST_ERROR_OBJECT (nvdec,
|
|
|
|
"failed to add the OpenGL context to the display");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 05:03:57 +00:00
|
|
|
if (!gst_gl_context_check_gl_version (nvdec->gl_context,
|
|
|
|
SUPPORTED_GL_APIS, 3, 0)) {
|
2019-08-29 11:20:14 +00:00
|
|
|
GST_WARNING_OBJECT (nvdec, "OpenGL context could not support PBO download");
|
2019-08-17 05:03:57 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-03-15 10:20:47 +00:00
|
|
|
gst_gl_context_thread_add (nvdec->gl_context,
|
|
|
|
(GstGLContextThreadFunc) gst_nvdec_check_cuda_device_from_context, &ret);
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
GST_WARNING_OBJECT (nvdec, "Current OpenGL context is not CUDA-compatible");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-08-29 11:20:14 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2019-08-30 04:55:25 +00:00
|
|
|
gst_nvdec_ensure_gl_pool (GstNvDec * nvdec, GstQuery * query)
|
2019-08-29 11:20:14 +00:00
|
|
|
{
|
|
|
|
GstCaps *outcaps;
|
|
|
|
GstBufferPool *pool = NULL;
|
|
|
|
guint n, size, min, max;
|
|
|
|
GstVideoInfo vinfo = { 0, };
|
|
|
|
GstStructure *config;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "decide allocation");
|
|
|
|
|
2017-05-03 00:21:43 +00:00
|
|
|
gst_query_parse_allocation (query, &outcaps, NULL);
|
|
|
|
n = gst_query_get_n_allocation_pools (query);
|
2019-08-13 01:07:38 +00:00
|
|
|
if (n > 0)
|
2017-05-03 00:21:43 +00:00
|
|
|
gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
|
2019-08-13 01:07:38 +00:00
|
|
|
|
|
|
|
if (pool && !GST_IS_GL_BUFFER_POOL (pool)) {
|
|
|
|
gst_object_unref (pool);
|
|
|
|
pool = NULL;
|
2017-05-03 00:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pool) {
|
2019-08-30 04:55:25 +00:00
|
|
|
GST_DEBUG_OBJECT (nvdec, "no downstream pool, create our pool");
|
2017-05-03 00:21:43 +00:00
|
|
|
pool = gst_gl_buffer_pool_new (nvdec->gl_context);
|
|
|
|
|
|
|
|
if (outcaps)
|
|
|
|
gst_video_info_from_caps (&vinfo, outcaps);
|
|
|
|
size = (guint) vinfo.size;
|
|
|
|
min = max = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
config = gst_buffer_pool_get_config (pool);
|
|
|
|
gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
|
|
|
|
gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
|
|
|
|
gst_buffer_pool_set_config (pool, config);
|
|
|
|
if (n > 0)
|
|
|
|
gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
|
|
|
|
else
|
|
|
|
gst_query_add_allocation_pool (query, pool, size, min, max);
|
|
|
|
gst_object_unref (pool);
|
2019-08-30 04:55:25 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2019-07-19 13:46:01 +00:00
|
|
|
#endif
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-30 04:55:25 +00:00
|
|
|
static gboolean
|
|
|
|
gst_nvdec_ensure_cuda_pool (GstNvDec * nvdec, GstQuery * query)
|
|
|
|
{
|
|
|
|
GstCaps *outcaps;
|
|
|
|
GstBufferPool *pool = NULL;
|
|
|
|
guint n, size, min, max;
|
|
|
|
GstVideoInfo vinfo = { 0, };
|
|
|
|
GstStructure *config;
|
|
|
|
|
|
|
|
gst_query_parse_allocation (query, &outcaps, NULL);
|
|
|
|
n = gst_query_get_n_allocation_pools (query);
|
|
|
|
if (n > 0) {
|
|
|
|
gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
|
2022-03-02 18:25:47 +00:00
|
|
|
if (pool) {
|
|
|
|
if (!GST_IS_CUDA_BUFFER_POOL (pool)) {
|
|
|
|
gst_clear_object (&pool);
|
|
|
|
} else {
|
|
|
|
GstCudaBufferPool *cpool = GST_CUDA_BUFFER_POOL (pool);
|
|
|
|
|
|
|
|
if (cpool->context != nvdec->cuda_ctx)
|
|
|
|
gst_clear_object (&pool);
|
|
|
|
}
|
2019-08-30 04:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "no downstream pool, create our pool");
|
|
|
|
pool = gst_cuda_buffer_pool_new (nvdec->cuda_ctx);
|
|
|
|
|
|
|
|
if (outcaps)
|
|
|
|
gst_video_info_from_caps (&vinfo, outcaps);
|
|
|
|
size = (guint) vinfo.size;
|
|
|
|
min = max = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
config = gst_buffer_pool_get_config (pool);
|
|
|
|
gst_buffer_pool_config_set_params (config, outcaps, size, min, max);
|
|
|
|
gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
|
|
|
|
gst_buffer_pool_set_config (pool, config);
|
2022-03-02 18:25:47 +00:00
|
|
|
|
|
|
|
/* Get updated size by cuda buffer pool */
|
|
|
|
config = gst_buffer_pool_get_config (pool);
|
|
|
|
gst_buffer_pool_config_get_params (config, NULL, &size, NULL, NULL);
|
|
|
|
gst_structure_free (config);
|
|
|
|
|
2019-08-30 04:55:25 +00:00
|
|
|
if (n > 0)
|
|
|
|
gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
|
|
|
|
else
|
|
|
|
gst_query_add_allocation_pool (query, pool, size, min, max);
|
|
|
|
gst_object_unref (pool);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_nvdec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (decoder);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (nvdec, "decide allocation");
|
|
|
|
|
|
|
|
if (nvdec->mem_type == GST_NVDEC_MEM_TYPE_SYSTEM)
|
|
|
|
goto done;
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2019-08-30 04:55:25 +00:00
|
|
|
if (nvdec->mem_type == GST_NVDEC_MEM_TYPE_GL) {
|
|
|
|
if (!gst_nvdec_ensure_gl_pool (nvdec, query))
|
|
|
|
return FALSE;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (!gst_nvdec_ensure_cuda_pool (nvdec, query)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2017-05-03 00:21:43 +00:00
|
|
|
return GST_VIDEO_DECODER_CLASS (gst_nvdec_parent_class)->decide_allocation
|
|
|
|
(decoder, query);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_nvdec_src_query (GstVideoDecoder * decoder, GstQuery * query)
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (decoder);
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_CONTEXT:
|
2019-08-06 04:44:20 +00:00
|
|
|
if (gst_cuda_handle_context_query (GST_ELEMENT (decoder),
|
|
|
|
query, nvdec->cuda_ctx)) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2017-05-03 00:21:43 +00:00
|
|
|
if (gst_gl_handle_context_query (GST_ELEMENT (decoder), query,
|
2019-08-17 05:03:57 +00:00
|
|
|
nvdec->gl_display, nvdec->gl_context, nvdec->other_gl_context)) {
|
|
|
|
if (nvdec->gl_display)
|
|
|
|
gst_gl_display_filter_gl_api (GST_GL_DISPLAY (nvdec->gl_display),
|
|
|
|
SUPPORTED_GL_APIS);
|
2017-05-03 00:21:43 +00:00
|
|
|
return TRUE;
|
2019-08-17 05:03:57 +00:00
|
|
|
}
|
2019-08-17 04:58:33 +00:00
|
|
|
#endif
|
2017-05-03 00:21:43 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GST_VIDEO_DECODER_CLASS (gst_nvdec_parent_class)->src_query (decoder,
|
|
|
|
query);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_nvdec_set_context (GstElement * element, GstContext * context)
|
|
|
|
{
|
|
|
|
GstNvDec *nvdec = GST_NVDEC (element);
|
2019-08-06 04:44:20 +00:00
|
|
|
GstNvDecClass *klass = GST_NVDEC_GET_CLASS (nvdec);
|
2019-07-19 13:46:01 +00:00
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
GST_DEBUG_OBJECT (nvdec, "set context %s",
|
|
|
|
gst_context_get_context_type (context));
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
if (gst_cuda_handle_set_context (element,
|
|
|
|
context, klass->cuda_device_id, &nvdec->cuda_ctx)) {
|
|
|
|
goto done;
|
|
|
|
}
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2017-05-03 00:21:43 +00:00
|
|
|
gst_gl_handle_set_context (element, context, &nvdec->gl_display,
|
|
|
|
&nvdec->other_gl_context);
|
2019-07-19 13:46:01 +00:00
|
|
|
#endif
|
2017-05-03 00:21:43 +00:00
|
|
|
|
2019-08-06 04:44:20 +00:00
|
|
|
done:
|
2017-05-03 00:21:43 +00:00
|
|
|
GST_ELEMENT_CLASS (gst_nvdec_parent_class)->set_context (element, context);
|
|
|
|
}
|
2019-07-11 12:53:46 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstCaps *sink_caps;
|
|
|
|
GstCaps *src_caps;
|
|
|
|
cudaVideoCodec codec_type;
|
|
|
|
gchar *codec;
|
|
|
|
guint cuda_device_id;
|
|
|
|
gboolean is_default;
|
|
|
|
} GstNvDecClassData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_nvdec_subclass_init (gpointer g_class, gpointer data)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
GstNvDecClass *nvdec_class = GST_NVDEC_CLASS (g_class);
|
|
|
|
GstNvDecClassData *cdata = data;
|
|
|
|
gchar *long_name;
|
|
|
|
|
|
|
|
if (cdata->is_default) {
|
|
|
|
long_name = g_strdup_printf ("NVDEC %s Video Decoder", cdata->codec);
|
|
|
|
} else {
|
2019-09-11 06:16:45 +00:00
|
|
|
long_name = g_strdup_printf ("NVDEC %s Video Decoder with device %d",
|
2019-07-11 12:53:46 +00:00
|
|
|
cdata->codec, cdata->cuda_device_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_element_class_set_metadata (element_class, long_name,
|
|
|
|
"Codec/Decoder/Video/Hardware", "NVDEC video decoder",
|
|
|
|
"Ericsson AB, http://www.ericsson.com, "
|
|
|
|
"Seungha Yang <seungha.yang@navercorp.com>");
|
|
|
|
g_free (long_name);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
|
|
|
|
cdata->sink_caps));
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
|
|
|
|
cdata->src_caps));
|
|
|
|
|
|
|
|
nvdec_class->codec_type = cdata->codec_type;
|
|
|
|
nvdec_class->cuda_device_id = cdata->cuda_device_id;
|
|
|
|
|
|
|
|
gst_caps_unref (cdata->sink_caps);
|
|
|
|
gst_caps_unref (cdata->src_caps);
|
|
|
|
g_free (cdata->codec);
|
|
|
|
g_free (cdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_nvdec_subclass_register (GstPlugin * plugin, GType type,
|
|
|
|
cudaVideoCodec codec_type, const gchar * codec, guint device_id, guint rank,
|
|
|
|
GstCaps * sink_caps, GstCaps * src_caps)
|
|
|
|
{
|
|
|
|
GTypeQuery type_query;
|
|
|
|
GTypeInfo type_info = { 0, };
|
|
|
|
GType subtype;
|
|
|
|
gchar *type_name;
|
|
|
|
GstNvDecClassData *cdata;
|
2019-07-23 01:24:10 +00:00
|
|
|
gboolean is_default = TRUE;
|
2022-06-26 12:09:50 +00:00
|
|
|
gint index = 0;
|
2019-07-11 12:53:46 +00:00
|
|
|
|
|
|
|
cdata = g_new0 (GstNvDecClassData, 1);
|
|
|
|
cdata->sink_caps = gst_caps_ref (sink_caps);
|
|
|
|
cdata->src_caps = gst_caps_ref (src_caps);
|
|
|
|
cdata->codec_type = codec_type;
|
|
|
|
cdata->codec = g_strdup (codec);
|
|
|
|
cdata->cuda_device_id = device_id;
|
|
|
|
|
|
|
|
g_type_query (type, &type_query);
|
|
|
|
memset (&type_info, 0, sizeof (type_info));
|
|
|
|
type_info.class_size = type_query.class_size;
|
|
|
|
type_info.instance_size = type_query.instance_size;
|
|
|
|
type_info.class_init = gst_nvdec_subclass_init;
|
|
|
|
type_info.class_data = cdata;
|
|
|
|
|
|
|
|
type_name = g_strdup_printf ("nv%sdec", codec);
|
2022-06-26 12:09:50 +00:00
|
|
|
while (g_type_from_name (type_name)) {
|
|
|
|
index++;
|
2019-07-11 12:53:46 +00:00
|
|
|
g_free (type_name);
|
2022-06-26 12:09:50 +00:00
|
|
|
type_name = g_strdup_printf ("nv%sdevice%ddec", codec, index);
|
2019-07-23 01:24:10 +00:00
|
|
|
is_default = FALSE;
|
2019-07-11 12:53:46 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 01:24:10 +00:00
|
|
|
cdata->is_default = is_default;
|
2019-07-11 12:53:46 +00:00
|
|
|
subtype = g_type_register_static (type, type_name, &type_info, 0);
|
|
|
|
|
|
|
|
/* make lower rank than default device */
|
2019-07-23 01:24:10 +00:00
|
|
|
if (rank > 0 && !is_default)
|
|
|
|
rank--;
|
|
|
|
|
|
|
|
if (!gst_element_register (plugin, type_name, rank, subtype))
|
2019-07-11 12:53:46 +00:00
|
|
|
GST_WARNING ("Failed to register plugin '%s'", type_name);
|
|
|
|
|
|
|
|
g_free (type_name);
|
|
|
|
}
|
|
|
|
|
2019-07-25 07:45:21 +00:00
|
|
|
void
|
2019-11-28 09:54:31 +00:00
|
|
|
gst_nvdec_plugin_init (GstPlugin * plugin, guint device_index,
|
2020-04-16 14:27:31 +00:00
|
|
|
cudaVideoCodec codec, const gchar * codec_name, GstCaps * sink_template,
|
|
|
|
GstCaps * src_template)
|
2019-07-11 12:53:46 +00:00
|
|
|
{
|
2020-04-16 14:27:31 +00:00
|
|
|
gst_nvdec_subclass_register (plugin, GST_TYPE_NVDEC, codec,
|
|
|
|
codec_name, device_index, GST_RANK_PRIMARY, sink_template, src_template);
|
2019-07-11 12:53:46 +00:00
|
|
|
}
|