2020-04-16 14:27:31 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2017 Ericsson AB. All rights reserved.
|
|
|
|
* Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2020-04-16 14:27:31 +00:00
|
|
|
#include <gst/gl/gl.h>
|
|
|
|
#include <gst/gl/gstglfuncs.h>
|
|
|
|
#endif
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <gst/d3d11/gstd3d11.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gst/cuda/gstcuda.h>
|
|
|
|
#include <gst/cuda/gstcuda-private.h>
|
2020-04-16 14:27:31 +00:00
|
|
|
#include "gstnvdecoder.h"
|
2023-09-27 15:03:08 +00:00
|
|
|
#include "gstcudaconverter.h"
|
2020-04-16 14:27:31 +00:00
|
|
|
#include <string.h>
|
2023-09-21 13:18:56 +00:00
|
|
|
#include <string>
|
|
|
|
#include <set>
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-02-07 16:42:55 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
GST_DEBUG_CATEGORY_EXTERN (gst_nv_decoder_debug);
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:27:31 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_nv_decoder_debug
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2023-02-07 16:42:55 +00:00
|
|
|
#define SUPPORTED_GL_APIS (GstGLAPI) (GST_GL_API_OPENGL | GST_GL_API_OPENGL3)
|
2020-06-18 19:50:26 +00:00
|
|
|
#endif
|
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
2023-09-27 15:03:08 +00:00
|
|
|
GST_NV_DECODER_OUTPUT_TYPE_UNKNOWN = 0,
|
|
|
|
GST_NV_DECODER_OUTPUT_TYPE_SYSTEM = (1 << 0),
|
|
|
|
GST_NV_DECODER_OUTPUT_TYPE_GL = (1 << 1),
|
|
|
|
GST_NV_DECODER_OUTPUT_TYPE_CUDA = (1 << 2),
|
|
|
|
GST_NV_DECODER_OUTPUT_TYPE_D3D11 = (1 << 3),
|
2021-03-17 05:30:09 +00:00
|
|
|
} GstNvDecoderOutputType;
|
|
|
|
|
2020-04-16 14:27:31 +00:00
|
|
|
struct _GstNvDecoder
|
|
|
|
{
|
|
|
|
GstObject parent;
|
2023-02-07 17:25:35 +00:00
|
|
|
|
2023-09-27 14:02:43 +00:00
|
|
|
guint device_id;
|
2023-09-27 15:03:08 +00:00
|
|
|
gint64 adapter_luid;
|
2023-09-27 14:02:43 +00:00
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
GstNvDecObject *object;
|
2020-04-16 14:27:31 +00:00
|
|
|
GstCudaContext *context;
|
2022-12-21 14:59:19 +00:00
|
|
|
GstCudaStream *stream;
|
2020-04-16 14:27:31 +00:00
|
|
|
|
|
|
|
GstVideoInfo info;
|
2023-09-28 12:32:12 +00:00
|
|
|
GstVideoInfo aligned_info;
|
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
CUVIDDECODECREATEINFO create_info;
|
2021-03-17 05:30:09 +00:00
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
gboolean alloc_aux_frame;
|
2021-03-17 05:30:09 +00:00
|
|
|
gboolean configured;
|
2023-02-07 17:25:35 +00:00
|
|
|
guint downstream_min_buffers;
|
|
|
|
guint num_output_surfaces;
|
2023-09-16 14:26:27 +00:00
|
|
|
gboolean wait_on_pool_full;
|
2023-02-07 17:25:35 +00:00
|
|
|
|
|
|
|
GMutex lock;
|
2021-03-17 05:30:09 +00:00
|
|
|
|
|
|
|
/* For OpenGL interop. */
|
|
|
|
GstObject *gl_display;
|
|
|
|
GstObject *gl_context;
|
|
|
|
GstObject *other_gl_context;
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
/* D3D11 interop */
|
|
|
|
GstObject *d3d11_device;
|
|
|
|
GstCudaConverter *converter;
|
|
|
|
GstBuffer *export_buf;
|
|
|
|
GstBuffer *convert_buf;
|
|
|
|
GstVideoInfo output_info;
|
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
GstNvDecoderOutputType output_type;
|
2020-04-16 14:27:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void gst_nv_decoder_dispose (GObject * object);
|
2023-02-07 17:25:35 +00:00
|
|
|
static void gst_nv_decoder_finalize (GObject * object);
|
2020-04-16 14:27:31 +00:00
|
|
|
|
|
|
|
#define parent_class gst_nv_decoder_parent_class
|
|
|
|
G_DEFINE_TYPE (GstNvDecoder, gst_nv_decoder, GST_TYPE_OBJECT);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_nv_decoder_class_init (GstNvDecoderClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
gobject_class->dispose = gst_nv_decoder_dispose;
|
2023-02-07 17:25:35 +00:00
|
|
|
gobject_class->finalize = gst_nv_decoder_finalize;
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_nv_decoder_init (GstNvDecoder * self)
|
|
|
|
{
|
2023-02-07 17:25:35 +00:00
|
|
|
g_mutex_init (&self->lock);
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_nv_decoder_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstNvDecoder *self = GST_NV_DECODER (object);
|
|
|
|
|
2023-09-27 14:02:43 +00:00
|
|
|
gst_nv_decoder_close (self);
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
static void
|
|
|
|
gst_nv_decoder_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstNvDecoder *self = GST_NV_DECODER (object);
|
|
|
|
|
|
|
|
g_mutex_clear (&self->lock);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:27:31 +00:00
|
|
|
static cudaVideoChromaFormat
|
|
|
|
chroma_format_from_video_format (GstVideoFormat format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case GST_VIDEO_FORMAT_NV12:
|
|
|
|
case GST_VIDEO_FORMAT_P010_10LE:
|
2023-09-21 13:18:56 +00:00
|
|
|
case GST_VIDEO_FORMAT_P012_LE:
|
2020-04-16 14:27:31 +00:00
|
|
|
return cudaVideoChromaFormat_420;
|
|
|
|
case GST_VIDEO_FORMAT_Y444:
|
|
|
|
case GST_VIDEO_FORMAT_Y444_16LE:
|
2023-09-21 13:18:56 +00:00
|
|
|
case GST_VIDEO_FORMAT_GBR:
|
|
|
|
case GST_VIDEO_FORMAT_GBR_16LE:
|
2020-04-16 14:27:31 +00:00
|
|
|
return cudaVideoChromaFormat_444;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cudaVideoChromaFormat_420;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cudaVideoSurfaceFormat
|
|
|
|
output_format_from_video_format (GstVideoFormat format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case GST_VIDEO_FORMAT_NV12:
|
|
|
|
return cudaVideoSurfaceFormat_NV12;
|
|
|
|
case GST_VIDEO_FORMAT_P010_10LE:
|
2023-09-21 13:18:56 +00:00
|
|
|
case GST_VIDEO_FORMAT_P012_LE:
|
2020-04-16 14:27:31 +00:00
|
|
|
return cudaVideoSurfaceFormat_P016;
|
|
|
|
case GST_VIDEO_FORMAT_Y444:
|
2023-09-21 13:18:56 +00:00
|
|
|
case GST_VIDEO_FORMAT_GBR:
|
2020-04-16 14:27:31 +00:00
|
|
|
return cudaVideoSurfaceFormat_YUV444;
|
|
|
|
case GST_VIDEO_FORMAT_Y444_16LE:
|
2023-09-21 13:18:56 +00:00
|
|
|
case GST_VIDEO_FORMAT_GBR_16LE:
|
2020-04-16 14:27:31 +00:00
|
|
|
return cudaVideoSurfaceFormat_YUV444_16Bit;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cudaVideoSurfaceFormat_NV12;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstNvDecoder *
|
2023-09-27 15:03:08 +00:00
|
|
|
gst_nv_decoder_new (guint device_id, gint64 adapter_luid)
|
2021-03-17 05:30:09 +00:00
|
|
|
{
|
|
|
|
GstNvDecoder *self;
|
|
|
|
|
2023-02-07 16:42:55 +00:00
|
|
|
self = (GstNvDecoder *) g_object_new (GST_TYPE_NV_DECODER, nullptr);
|
2023-09-27 14:02:43 +00:00
|
|
|
self->device_id = device_id;
|
2023-09-27 15:03:08 +00:00
|
|
|
self->adapter_luid = adapter_luid;
|
2021-03-17 05:30:09 +00:00
|
|
|
gst_object_ref_sink (self);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2023-09-27 14:02:43 +00:00
|
|
|
gst_nv_decoder_open (GstNvDecoder * decoder, GstElement * element)
|
2021-03-17 05:30:09 +00:00
|
|
|
{
|
2023-09-27 14:02:43 +00:00
|
|
|
if (!gst_cuda_ensure_element_context (element,
|
|
|
|
decoder->device_id, &decoder->context)) {
|
|
|
|
GST_ERROR_OBJECT (element, "Couldn't create CUDA context");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2021-03-17 05:30:09 +00:00
|
|
|
|
2023-09-27 14:02:43 +00:00
|
|
|
gst_clear_cuda_stream (&decoder->stream);
|
|
|
|
decoder->stream = gst_cuda_stream_new (decoder->context);
|
|
|
|
|
|
|
|
return TRUE;
|
2021-03-17 05:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-02-07 17:25:35 +00:00
|
|
|
gst_nv_decoder_reset_unlocked (GstNvDecoder * self)
|
2021-03-17 05:30:09 +00:00
|
|
|
{
|
2023-02-07 17:25:35 +00:00
|
|
|
if (self->object)
|
|
|
|
gst_nv_dec_object_set_flushing (self->object, TRUE);
|
2021-03-17 05:30:09 +00:00
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
gst_clear_object (&self->object);
|
2021-03-17 05:30:09 +00:00
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
self->output_type = GST_NV_DECODER_OUTPUT_TYPE_UNKNOWN;
|
2021-03-17 05:30:09 +00:00
|
|
|
self->configured = FALSE;
|
2023-02-07 17:25:35 +00:00
|
|
|
self->downstream_min_buffers = 0;
|
|
|
|
self->num_output_surfaces = 0;
|
2021-03-17 05:30:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-27 14:02:43 +00:00
|
|
|
gboolean
|
|
|
|
gst_nv_decoder_close (GstNvDecoder * decoder)
|
|
|
|
{
|
|
|
|
gst_nv_decoder_reset_unlocked (decoder);
|
|
|
|
|
|
|
|
gst_clear_cuda_stream (&decoder->stream);
|
|
|
|
gst_clear_object (&decoder->context);
|
|
|
|
gst_clear_object (&decoder->gl_display);
|
|
|
|
gst_clear_object (&decoder->gl_context);
|
|
|
|
gst_clear_object (&decoder->other_gl_context);
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
gst_clear_object (&decoder->d3d11_device);
|
|
|
|
gst_clear_object (&decoder->converter);
|
|
|
|
gst_clear_buffer (&decoder->convert_buf);
|
|
|
|
gst_clear_buffer (&decoder->export_buf);
|
|
|
|
|
2023-09-27 14:02:43 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_nv_decoder_is_configured (GstNvDecoder * decoder)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_NV_DECODER (decoder), FALSE);
|
|
|
|
|
|
|
|
return decoder->configured;
|
|
|
|
}
|
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
gboolean
|
|
|
|
gst_nv_decoder_configure (GstNvDecoder * decoder, cudaVideoCodec codec,
|
2022-01-31 15:12:06 +00:00
|
|
|
GstVideoInfo * info, gint coded_width, gint coded_height,
|
2023-02-07 17:25:35 +00:00
|
|
|
guint coded_bitdepth, guint pool_size, gboolean alloc_aux_frame,
|
2023-02-13 12:59:05 +00:00
|
|
|
guint num_output_surfaces, guint init_max_width, guint init_max_height)
|
2020-04-16 14:27:31 +00:00
|
|
|
{
|
|
|
|
CUVIDDECODECREATEINFO create_info = { 0, };
|
2023-02-13 12:59:05 +00:00
|
|
|
GstVideoFormat format, prev_format = GST_VIDEO_FORMAT_UNKNOWN;
|
2022-05-02 13:59:59 +00:00
|
|
|
guint alloc_size;
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
g_return_val_if_fail (GST_IS_NV_DECODER (decoder), FALSE);
|
|
|
|
g_return_val_if_fail (codec < cudaVideoCodec_NumCodecs, FALSE);
|
2023-02-07 16:42:55 +00:00
|
|
|
g_return_val_if_fail (info != nullptr, FALSE);
|
2021-03-17 05:30:09 +00:00
|
|
|
g_return_val_if_fail (coded_width >= GST_VIDEO_INFO_WIDTH (info), FALSE);
|
|
|
|
g_return_val_if_fail (coded_height >= GST_VIDEO_INFO_HEIGHT (info), FALSE);
|
2022-01-31 15:12:06 +00:00
|
|
|
g_return_val_if_fail (coded_bitdepth >= 8, FALSE);
|
2021-03-17 05:30:09 +00:00
|
|
|
g_return_val_if_fail (pool_size > 0, FALSE);
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
gst_clear_buffer (&decoder->export_buf);
|
|
|
|
gst_clear_buffer (&decoder->convert_buf);
|
|
|
|
gst_clear_object (&decoder->converter);
|
|
|
|
|
2020-04-16 14:27:31 +00:00
|
|
|
format = GST_VIDEO_INFO_FORMAT (info);
|
2023-02-13 12:59:05 +00:00
|
|
|
if (decoder->info.finfo)
|
|
|
|
prev_format = GST_VIDEO_INFO_FORMAT (&decoder->info);
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-06-12 18:05:48 +00:00
|
|
|
/* h264 may require additional 1 frame because of its bumping process */
|
|
|
|
if (codec == cudaVideoCodec_H264)
|
|
|
|
pool_size += 1;
|
2022-05-02 13:46:06 +00:00
|
|
|
|
2022-05-02 13:59:59 +00:00
|
|
|
/* Need pool size * 2 for decode-only (used for reference) frame
|
|
|
|
* and output frame, AV1 film grain case for example */
|
2023-02-07 17:25:35 +00:00
|
|
|
decoder->alloc_aux_frame = alloc_aux_frame;
|
2022-05-02 13:59:59 +00:00
|
|
|
if (alloc_aux_frame) {
|
|
|
|
alloc_size = pool_size * 2;
|
|
|
|
} else {
|
|
|
|
alloc_size = pool_size;
|
|
|
|
}
|
|
|
|
|
2023-02-13 12:59:05 +00:00
|
|
|
decoder->info = *info;
|
2023-09-28 12:32:12 +00:00
|
|
|
gst_video_info_set_format (&decoder->aligned_info, format,
|
|
|
|
GST_ROUND_UP_2 (info->width), GST_ROUND_UP_2 (info->height));
|
2023-02-13 12:59:05 +00:00
|
|
|
|
|
|
|
g_mutex_lock (&decoder->lock);
|
|
|
|
if (decoder->object) {
|
|
|
|
GST_DEBUG_OBJECT (decoder,
|
|
|
|
"Configured max resolution %ux%u %s (bit-depth %u), "
|
|
|
|
"new resolution %ux%u %s (bit-depth %u)",
|
|
|
|
(guint) decoder->create_info.ulMaxWidth,
|
|
|
|
(guint) decoder->create_info.ulMaxHeight,
|
|
|
|
gst_video_format_to_string (prev_format),
|
|
|
|
(guint) decoder->create_info.bitDepthMinus8 + 8,
|
|
|
|
(guint) coded_width, (guint) coded_height,
|
|
|
|
gst_video_format_to_string (format), coded_bitdepth);
|
|
|
|
|
|
|
|
if (format == prev_format &&
|
|
|
|
(guint) coded_width <= decoder->create_info.ulMaxWidth &&
|
|
|
|
(guint) coded_height <= decoder->create_info.ulMaxHeight &&
|
|
|
|
coded_bitdepth == (guint) decoder->create_info.bitDepthMinus8 + 8) {
|
|
|
|
CUVIDRECONFIGUREDECODERINFO reconfig_info = { 0, };
|
|
|
|
|
2023-09-28 12:32:12 +00:00
|
|
|
reconfig_info.ulWidth = coded_width;
|
|
|
|
reconfig_info.ulHeight = coded_height;
|
|
|
|
reconfig_info.ulTargetWidth = decoder->aligned_info.width;
|
|
|
|
reconfig_info.ulTargetHeight = decoder->aligned_info.height;
|
2023-02-13 12:59:05 +00:00
|
|
|
reconfig_info.ulNumDecodeSurfaces = alloc_size;
|
|
|
|
reconfig_info.display_area.right = GST_VIDEO_INFO_WIDTH (info);
|
|
|
|
reconfig_info.display_area.bottom = GST_VIDEO_INFO_HEIGHT (info);
|
|
|
|
reconfig_info.target_rect.right = GST_VIDEO_INFO_WIDTH (info);
|
|
|
|
reconfig_info.target_rect.bottom = GST_VIDEO_INFO_HEIGHT (info);
|
|
|
|
|
|
|
|
if (gst_nv_dec_object_reconfigure (decoder->object,
|
|
|
|
&reconfig_info, info, alloc_aux_frame)) {
|
|
|
|
GST_DEBUG_OBJECT (decoder, "Reconfigured");
|
|
|
|
decoder->configured = TRUE;
|
|
|
|
g_mutex_unlock (&decoder->lock);
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (decoder,
|
|
|
|
"Couldn't reconfigure decoder, creating new decoder instance");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (decoder, "Need new decoder instance");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_nv_decoder_reset_unlocked (decoder);
|
|
|
|
g_mutex_unlock (&decoder->lock);
|
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
decoder->num_output_surfaces = num_output_surfaces;
|
2022-05-02 13:59:59 +00:00
|
|
|
|
2023-09-28 12:32:12 +00:00
|
|
|
create_info.ulWidth = coded_width;
|
|
|
|
create_info.ulHeight = coded_height;
|
2022-05-02 13:59:59 +00:00
|
|
|
create_info.ulNumDecodeSurfaces = alloc_size;
|
2020-04-16 14:27:31 +00:00
|
|
|
create_info.CodecType = codec;
|
|
|
|
create_info.ChromaFormat = chroma_format_from_video_format (format);
|
|
|
|
create_info.ulCreationFlags = cudaVideoCreate_Default;
|
2022-01-31 15:12:06 +00:00
|
|
|
create_info.bitDepthMinus8 = coded_bitdepth - 8;
|
2020-04-16 14:27:31 +00:00
|
|
|
create_info.ulIntraDecodeOnly = 0;
|
|
|
|
|
|
|
|
create_info.display_area.left = 0;
|
|
|
|
create_info.display_area.top = 0;
|
|
|
|
create_info.display_area.right = GST_VIDEO_INFO_WIDTH (info);
|
|
|
|
create_info.display_area.bottom = GST_VIDEO_INFO_HEIGHT (info);
|
|
|
|
create_info.OutputFormat = output_format_from_video_format (format);
|
|
|
|
create_info.DeinterlaceMode = cudaVideoDeinterlaceMode_Weave;
|
|
|
|
|
2023-09-28 12:32:12 +00:00
|
|
|
create_info.ulTargetWidth = decoder->aligned_info.width;
|
|
|
|
create_info.ulTargetHeight = decoder->aligned_info.height;
|
2023-02-07 17:25:35 +00:00
|
|
|
/* Will be updated on negotiate() */
|
2020-04-16 14:27:31 +00:00
|
|
|
create_info.ulNumOutputSurfaces = 1;
|
|
|
|
|
|
|
|
create_info.target_rect.left = 0;
|
|
|
|
create_info.target_rect.top = 0;
|
2022-01-31 15:18:44 +00:00
|
|
|
create_info.target_rect.right = GST_VIDEO_INFO_WIDTH (info);
|
|
|
|
create_info.target_rect.bottom = GST_VIDEO_INFO_HEIGHT (info);
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-02-13 12:59:05 +00:00
|
|
|
create_info.ulMaxWidth = MAX (create_info.ulWidth, init_max_width);
|
|
|
|
create_info.ulMaxHeight = MAX (create_info.ulHeight, init_max_height);
|
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
decoder->create_info = create_info;
|
2021-03-17 05:30:09 +00:00
|
|
|
decoder->configured = TRUE;
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
return TRUE;
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
GstFlowReturn
|
2023-09-05 16:00:42 +00:00
|
|
|
gst_nv_decoder_new_picture (GstNvDecoder * decoder, GstCodecPicture * picture)
|
2020-04-16 14:27:31 +00:00
|
|
|
{
|
2023-09-05 16:00:42 +00:00
|
|
|
GstNvDecSurface *surface;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
g_return_val_if_fail (GST_IS_NV_DECODER (decoder), GST_FLOW_ERROR);
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
if (!decoder->object) {
|
|
|
|
if (decoder->output_type == GST_NV_DECODER_OUTPUT_TYPE_CUDA) {
|
|
|
|
if (decoder->num_output_surfaces == 0 ||
|
|
|
|
decoder->num_output_surfaces < decoder->downstream_min_buffers) {
|
|
|
|
/* Auto mode or user specified num-output-surfaces value is too small */
|
|
|
|
decoder->create_info.ulNumOutputSurfaces =
|
|
|
|
decoder->downstream_min_buffers + 2;
|
2020-10-30 12:20:57 +00:00
|
|
|
} else {
|
2023-02-07 17:25:35 +00:00
|
|
|
/* Otherwise use user provided value */
|
|
|
|
decoder->create_info.ulNumOutputSurfaces = decoder->num_output_surfaces;
|
2020-10-30 12:20:57 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
GST_INFO_OBJECT (decoder, "Updating ulNumOutputSurfaces to %u, "
|
|
|
|
"user requested %u, min-downstream %u",
|
|
|
|
(guint) decoder->create_info.ulNumOutputSurfaces,
|
|
|
|
decoder->num_output_surfaces, decoder->downstream_min_buffers);
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
g_mutex_lock (&decoder->lock);
|
|
|
|
decoder->object = gst_nv_dec_object_new (decoder->context,
|
|
|
|
&decoder->create_info, &decoder->info, decoder->alloc_aux_frame);
|
|
|
|
g_mutex_unlock (&decoder->lock);
|
|
|
|
if (!decoder->object) {
|
|
|
|
GST_ERROR_OBJECT (decoder, "Couldn't create decoder object");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
2023-02-07 17:25:35 +00:00
|
|
|
|
2023-09-05 16:00:42 +00:00
|
|
|
ret = gst_nv_dec_object_acquire_surface (decoder->object, &surface);
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
gst_codec_picture_set_user_data (picture,
|
|
|
|
surface, (GDestroyNotify) gst_nv_dec_surface_unref);
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2023-02-07 17:25:35 +00:00
|
|
|
gst_nv_decoder_decode (GstNvDecoder * decoder, CUVIDPICPARAMS * params)
|
2020-04-16 14:27:31 +00:00
|
|
|
{
|
2023-02-07 17:25:35 +00:00
|
|
|
g_return_val_if_fail (GST_IS_NV_DECODER (decoder), FALSE);
|
|
|
|
g_return_val_if_fail (decoder->object != nullptr, FALSE);
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
return gst_nv_dec_object_decode (decoder->object, params);
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2020-04-16 14:27:31 +00:00
|
|
|
static gboolean
|
|
|
|
gst_nv_decoder_register_cuda_resource (GstNvDecoder * self, GstMemory * mem,
|
|
|
|
GstCudaGraphicsResource * resource)
|
|
|
|
{
|
|
|
|
GstMapInfo info;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
if (!gst_cuda_context_push (self->context)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to push CUDA context");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-02-07 16:42:55 +00:00
|
|
|
if (gst_memory_map (mem, &info, (GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
|
2020-04-16 14:27:31 +00:00
|
|
|
GstGLMemoryPBO *gl_mem = (GstGLMemoryPBO *) mem;
|
|
|
|
GstGLBuffer *gl_buffer = gl_mem->pbo;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (self,
|
|
|
|
"Register glbuffer %d to CUDA resource", gl_buffer->id);
|
|
|
|
|
|
|
|
/* 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_buffer->id, CU_GRAPHICS_REGISTER_FLAGS_NONE)) {
|
|
|
|
ret = TRUE;
|
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (self, "Failed to register memory");
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_memory_unmap (mem, &info);
|
|
|
|
} else {
|
|
|
|
GST_WARNING_OBJECT (self, "Failed to map memory");
|
|
|
|
}
|
|
|
|
|
2023-02-07 16:42:55 +00:00
|
|
|
if (!gst_cuda_context_pop (nullptr))
|
2020-04-16 14:27:31 +00:00
|
|
|
GST_WARNING_OBJECT (self, "Failed to pop CUDA context");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstCudaGraphicsResource *
|
|
|
|
gst_nv_decoder_ensure_cuda_graphics_resource (GstNvDecoder * self,
|
|
|
|
GstMemory * mem)
|
|
|
|
{
|
|
|
|
GQuark quark;
|
|
|
|
GstCudaGraphicsResource *resource;
|
|
|
|
|
|
|
|
if (!gst_is_gl_memory_pbo (mem)) {
|
|
|
|
GST_WARNING_OBJECT (self, "memory is not GL PBO memory, %s",
|
|
|
|
mem->allocator->mem_type);
|
2023-02-07 16:42:55 +00:00
|
|
|
return nullptr;
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
quark = gst_cuda_quark_from_id (GST_CUDA_QUARK_GRAPHICS_RESOURCE);
|
|
|
|
|
|
|
|
resource = (GstCudaGraphicsResource *)
|
|
|
|
gst_mini_object_get_qdata (GST_MINI_OBJECT (mem), quark);
|
|
|
|
|
|
|
|
if (!resource) {
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
resource = gst_cuda_graphics_resource_new (self->context,
|
|
|
|
GST_OBJECT (GST_GL_BASE_MEMORY_CAST (mem)->context),
|
|
|
|
GST_CUDA_GRAPHICS_RESOURCE_GL_BUFFER);
|
|
|
|
|
|
|
|
ret = gst_nv_decoder_register_cuda_resource (self, mem, resource);
|
|
|
|
if (!ret) {
|
|
|
|
GST_WARNING_OBJECT (self, "Couldn't register resource");
|
|
|
|
gst_cuda_graphics_resource_free (resource);
|
|
|
|
|
2023-02-07 16:42:55 +00:00
|
|
|
return nullptr;
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_mini_object_set_qdata (GST_MINI_OBJECT (mem), quark, resource,
|
|
|
|
(GDestroyNotify) gst_cuda_graphics_resource_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstNvDecoder *self;
|
|
|
|
gboolean ret;
|
2023-02-07 17:25:35 +00:00
|
|
|
GstNvDecSurface *surface;
|
2020-04-16 14:27:31 +00:00
|
|
|
GstBuffer *output_buffer;
|
|
|
|
} GstNvDecoderCopyToGLData;
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_nv_decoder_copy_frame_to_gl_internal (GstGLContext * context,
|
|
|
|
GstNvDecoderCopyToGLData * data)
|
|
|
|
{
|
|
|
|
GstNvDecoder *self = data->self;
|
2023-02-07 17:25:35 +00:00
|
|
|
GstNvDecSurface *surface = data->surface;
|
2020-04-16 14:27:31 +00:00
|
|
|
GstCudaGraphicsResource **resources;
|
|
|
|
guint num_resources;
|
|
|
|
guint i;
|
|
|
|
CUDA_MEMCPY2D copy_params = { 0, };
|
|
|
|
GstVideoInfo *info = &self->info;
|
2022-12-21 14:59:19 +00:00
|
|
|
CUstream stream = gst_cuda_stream_get_handle (self->stream);
|
2020-04-16 14:27:31 +00:00
|
|
|
|
|
|
|
data->ret = TRUE;
|
|
|
|
|
|
|
|
num_resources = gst_buffer_n_memory (data->output_buffer);
|
|
|
|
resources = g_newa (GstCudaGraphicsResource *, num_resources);
|
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
if (!gst_cuda_context_push (self->context)) {
|
|
|
|
GST_WARNING_OBJECT (self, "Failed to push CUDA context");
|
|
|
|
data->ret = FALSE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:27:31 +00:00
|
|
|
for (i = 0; i < num_resources; i++) {
|
|
|
|
GstMemory *mem;
|
|
|
|
|
|
|
|
mem = gst_buffer_peek_memory (data->output_buffer, i);
|
|
|
|
resources[i] = gst_nv_decoder_ensure_cuda_graphics_resource (self, mem);
|
|
|
|
if (!resources[i]) {
|
|
|
|
GST_WARNING_OBJECT (self, "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);
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_params.srcMemoryType = CU_MEMORYTYPE_DEVICE;
|
2023-02-07 17:25:35 +00:00
|
|
|
copy_params.srcPitch = surface->pitch;
|
2020-04-16 14:27:31 +00:00
|
|
|
copy_params.dstMemoryType = CU_MEMORYTYPE_DEVICE;
|
|
|
|
|
|
|
|
for (i = 0; i < num_resources; i++) {
|
|
|
|
CUdeviceptr dst_ptr;
|
|
|
|
gsize size;
|
|
|
|
CUgraphicsResource cuda_resource =
|
2023-02-07 16:42:55 +00:00
|
|
|
gst_cuda_graphics_resource_map (resources[i], nullptr,
|
2020-04-16 14:27:31 +00:00
|
|
|
CU_GRAPHICS_MAP_RESOURCE_FLAGS_WRITE_DISCARD);
|
|
|
|
|
|
|
|
if (!cuda_resource) {
|
|
|
|
GST_WARNING_OBJECT (self, "failed to map CUDA resources");
|
|
|
|
data->ret = FALSE;
|
|
|
|
goto unmap_video_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_cuda_result (CuGraphicsResourceGetMappedPointer (&dst_ptr, &size,
|
|
|
|
cuda_resource))) {
|
|
|
|
GST_WARNING_OBJECT (self, "failed to map CUDA resource");
|
|
|
|
data->ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_params.dstPitch = GST_VIDEO_INFO_PLANE_STRIDE (info, i);
|
|
|
|
copy_params.WidthInBytes = GST_VIDEO_INFO_COMP_WIDTH (info, i)
|
|
|
|
* GST_VIDEO_INFO_COMP_PSTRIDE (info, i);
|
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
copy_params.srcDevice = surface->devptr +
|
2023-09-28 12:32:12 +00:00
|
|
|
(i * surface->pitch * GST_VIDEO_INFO_HEIGHT (&self->aligned_info));
|
2020-04-16 14:27:31 +00:00
|
|
|
copy_params.dstDevice = dst_ptr;
|
|
|
|
copy_params.Height = GST_VIDEO_INFO_COMP_HEIGHT (info, i);
|
|
|
|
|
2022-12-21 14:59:19 +00:00
|
|
|
if (!gst_cuda_result (CuMemcpy2DAsync (©_params, stream))) {
|
2020-04-16 14:27:31 +00:00
|
|
|
GST_WARNING_OBJECT (self, "memcpy to mapped array failed");
|
|
|
|
data->ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 14:59:19 +00:00
|
|
|
gst_cuda_result (CuStreamSynchronize (stream));
|
2020-04-16 14:27:31 +00:00
|
|
|
|
|
|
|
unmap_video_frame:
|
|
|
|
for (i = 0; i < num_resources; i++) {
|
2023-02-07 16:42:55 +00:00
|
|
|
gst_cuda_graphics_resource_unmap (resources[i], nullptr);
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 16:42:55 +00:00
|
|
|
if (!gst_cuda_context_pop (nullptr))
|
2020-04-16 14:27:31 +00:00
|
|
|
GST_WARNING_OBJECT (self, "Failed to pop CUDA context");
|
|
|
|
}
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
static GstFlowReturn
|
2020-04-16 14:27:31 +00:00
|
|
|
gst_nv_decoder_copy_frame_to_gl (GstNvDecoder * decoder,
|
2023-02-07 17:25:35 +00:00
|
|
|
GstGLContext * context, GstNvDecSurface * surface, GstBuffer * buffer)
|
2020-04-16 14:27:31 +00:00
|
|
|
{
|
|
|
|
GstNvDecoderCopyToGLData data;
|
|
|
|
|
|
|
|
data.self = decoder;
|
2023-02-07 17:25:35 +00:00
|
|
|
data.surface = surface;
|
2020-04-16 14:27:31 +00:00
|
|
|
data.output_buffer = buffer;
|
|
|
|
|
|
|
|
gst_gl_context_thread_add (context,
|
|
|
|
(GstGLContextThreadFunc) gst_nv_decoder_copy_frame_to_gl_internal, &data);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (decoder, "Copy frame to GL ret %d", data.ret);
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
if (!data.ret)
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_nv_decoder_copy_frame_to_d3d11 (GstNvDecoder * self,
|
|
|
|
GstNvDecSurface * surface, GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstFlowReturn ret;
|
|
|
|
GstMemory *mem;
|
|
|
|
GstCudaMemory *cmem;
|
|
|
|
GstVideoFrame src_frame, dst_frame;
|
|
|
|
gboolean convert_ret;
|
|
|
|
|
|
|
|
if (!self->converter || !self->convert_buf) {
|
|
|
|
GST_ERROR_OBJECT (self, "D3D11 output is not configured");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gst_nv_dec_object_export_surface (self->object,
|
|
|
|
surface, self->stream, &mem);
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
GST_WARNING_OBJECT (self, "Couldn't export surface");
|
|
|
|
gst_nv_dec_object_unmap_surface (self->object, surface);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmem = GST_CUDA_MEMORY_CAST (mem);
|
|
|
|
|
|
|
|
/* TODO: convert without buffer wrapping */
|
|
|
|
if (!self->export_buf) {
|
|
|
|
self->export_buf = gst_buffer_new ();
|
|
|
|
gst_buffer_add_video_meta_full (self->export_buf, GST_VIDEO_FRAME_FLAG_NONE,
|
|
|
|
GST_VIDEO_INFO_FORMAT (&self->info),
|
|
|
|
GST_VIDEO_INFO_WIDTH (&self->info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&self->info),
|
|
|
|
GST_VIDEO_INFO_N_PLANES (&self->info),
|
|
|
|
cmem->info.offset, cmem->info.stride);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_append_memory (self->export_buf, mem);
|
|
|
|
if (!gst_video_frame_map (&src_frame, &self->info, self->export_buf,
|
|
|
|
(GstMapFlags) (GST_MAP_READ | GST_MAP_CUDA))) {
|
|
|
|
GST_ERROR_OBJECT (self, "Couldn't map exported buffer");
|
|
|
|
gst_buffer_remove_all_memory (self->export_buf);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_video_frame_map (&dst_frame, &self->output_info, self->convert_buf,
|
|
|
|
(GstMapFlags) (GST_MAP_WRITE | GST_MAP_CUDA))) {
|
|
|
|
GST_ERROR_OBJECT (self, "Couldn't map converter output buffer");
|
|
|
|
gst_buffer_remove_all_memory (self->export_buf);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
convert_ret = gst_cuda_converter_convert_frame (self->converter, &src_frame,
|
|
|
|
&dst_frame, gst_cuda_stream_get_handle (self->stream), nullptr);
|
|
|
|
gst_video_frame_unmap (&dst_frame);
|
|
|
|
gst_video_frame_unmap (&src_frame);
|
|
|
|
gst_buffer_remove_all_memory (self->export_buf);
|
|
|
|
|
|
|
|
if (!convert_ret) {
|
|
|
|
GST_ERROR_OBJECT (self, "Couldn't convert frame");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_cuda_buffer_copy (buffer, GST_CUDA_BUFFER_COPY_D3D11,
|
|
|
|
&self->output_info, self->convert_buf, GST_CUDA_BUFFER_COPY_CUDA,
|
|
|
|
&self->output_info, self->context, self->stream)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Couldn't copy buffer to d3d11 output");
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2020-04-16 14:27:31 +00:00
|
|
|
gst_nv_decoder_copy_frame_to_system (GstNvDecoder * decoder,
|
2023-02-07 17:25:35 +00:00
|
|
|
GstNvDecSurface * surface, GstBuffer * buffer)
|
2020-04-16 14:27:31 +00:00
|
|
|
{
|
|
|
|
GstVideoFrame video_frame;
|
|
|
|
CUDA_MEMCPY2D copy_params = { 0, };
|
2023-09-27 15:03:08 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_ERROR;
|
2022-12-21 14:59:19 +00:00
|
|
|
CUstream stream = gst_cuda_stream_get_handle (decoder->stream);
|
2020-04-16 14:27:31 +00:00
|
|
|
|
|
|
|
if (!gst_video_frame_map (&video_frame, &decoder->info, buffer,
|
|
|
|
GST_MAP_WRITE)) {
|
|
|
|
GST_ERROR_OBJECT (decoder, "Couldn't map video frame");
|
2023-09-27 15:03:08 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
copy_params.srcMemoryType = CU_MEMORYTYPE_DEVICE;
|
2023-02-07 17:25:35 +00:00
|
|
|
copy_params.srcPitch = surface->pitch;
|
2020-04-16 14:27:31 +00:00
|
|
|
copy_params.dstMemoryType = CU_MEMORYTYPE_HOST;
|
|
|
|
copy_params.WidthInBytes = GST_VIDEO_INFO_COMP_WIDTH (&decoder->info, 0)
|
|
|
|
* GST_VIDEO_INFO_COMP_PSTRIDE (&decoder->info, 0);
|
|
|
|
|
2023-02-07 16:42:55 +00:00
|
|
|
for (guint i = 0; i < GST_VIDEO_FRAME_N_PLANES (&video_frame); i++) {
|
2023-02-07 17:25:35 +00:00
|
|
|
copy_params.srcDevice = surface->devptr +
|
2023-09-28 12:32:12 +00:00
|
|
|
(i * surface->pitch * GST_VIDEO_INFO_HEIGHT (&decoder->aligned_info));
|
2020-04-16 14:27:31 +00:00
|
|
|
copy_params.dstHost = GST_VIDEO_FRAME_PLANE_DATA (&video_frame, i);
|
|
|
|
copy_params.dstPitch = GST_VIDEO_FRAME_PLANE_STRIDE (&video_frame, i);
|
|
|
|
copy_params.Height = GST_VIDEO_FRAME_COMP_HEIGHT (&video_frame, i);
|
|
|
|
|
2022-12-21 14:59:19 +00:00
|
|
|
if (!gst_cuda_result (CuMemcpy2DAsync (©_params, stream))) {
|
2020-04-16 14:27:31 +00:00
|
|
|
GST_ERROR_OBJECT (decoder, "failed to copy %dth plane", i);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 14:59:19 +00:00
|
|
|
gst_cuda_result (CuStreamSynchronize (stream));
|
2023-09-27 15:03:08 +00:00
|
|
|
ret = GST_FLOW_OK;
|
2020-04-16 14:27:31 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
gst_video_frame_unmap (&video_frame);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
static GstFlowReturn
|
2020-10-24 17:27:52 +00:00
|
|
|
gst_nv_decoder_copy_frame_to_cuda (GstNvDecoder * decoder,
|
2023-02-07 17:25:35 +00:00
|
|
|
GstNvDecSurface * surface, GstBuffer * buffer, GstCudaStream * stream)
|
2020-10-24 17:27:52 +00:00
|
|
|
{
|
|
|
|
CUDA_MEMCPY2D copy_params = { 0, };
|
|
|
|
GstMemory *mem;
|
2022-03-02 18:25:47 +00:00
|
|
|
GstVideoFrame video_frame;
|
2022-12-20 15:13:56 +00:00
|
|
|
CUstream stream_handle = gst_cuda_stream_get_handle (stream);
|
2023-09-27 15:03:08 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_ERROR;
|
2020-10-24 17:27:52 +00:00
|
|
|
|
|
|
|
mem = gst_buffer_peek_memory (buffer, 0);
|
|
|
|
if (!gst_is_cuda_memory (mem)) {
|
|
|
|
GST_WARNING_OBJECT (decoder, "Not a CUDA memory");
|
2023-09-27 15:03:08 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2020-10-24 17:27:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:25:47 +00:00
|
|
|
if (!gst_video_frame_map (&video_frame,
|
2023-02-07 16:42:55 +00:00
|
|
|
&decoder->info, buffer,
|
|
|
|
(GstMapFlags) (GST_MAP_WRITE | GST_MAP_CUDA))) {
|
2022-03-02 18:25:47 +00:00
|
|
|
GST_ERROR_OBJECT (decoder, "frame map failure");
|
2023-09-27 15:03:08 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2020-10-24 17:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
copy_params.srcMemoryType = CU_MEMORYTYPE_DEVICE;
|
2023-02-07 17:25:35 +00:00
|
|
|
copy_params.srcPitch = surface->pitch;
|
2020-10-24 17:27:52 +00:00
|
|
|
copy_params.dstMemoryType = CU_MEMORYTYPE_DEVICE;
|
|
|
|
|
2023-02-07 16:42:55 +00:00
|
|
|
for (guint i = 0; i < GST_VIDEO_INFO_N_PLANES (&decoder->info); i++) {
|
2023-02-07 17:25:35 +00:00
|
|
|
copy_params.srcDevice = surface->devptr +
|
2023-09-28 12:32:12 +00:00
|
|
|
(i * surface->pitch * GST_VIDEO_INFO_HEIGHT (&decoder->aligned_info));
|
2022-03-02 18:25:47 +00:00
|
|
|
copy_params.dstDevice =
|
|
|
|
(CUdeviceptr) GST_VIDEO_FRAME_PLANE_DATA (&video_frame, i);
|
|
|
|
copy_params.dstPitch = GST_VIDEO_FRAME_PLANE_STRIDE (&video_frame, i);
|
2020-10-24 17:27:52 +00:00
|
|
|
copy_params.WidthInBytes = GST_VIDEO_INFO_COMP_WIDTH (&decoder->info, 0)
|
|
|
|
* GST_VIDEO_INFO_COMP_PSTRIDE (&decoder->info, 0);
|
|
|
|
copy_params.Height = GST_VIDEO_INFO_COMP_HEIGHT (&decoder->info, i);
|
|
|
|
|
2022-12-20 15:13:56 +00:00
|
|
|
if (!gst_cuda_result (CuMemcpy2DAsync (©_params, stream_handle))) {
|
2020-10-24 17:27:52 +00:00
|
|
|
GST_ERROR_OBJECT (decoder, "failed to copy %dth plane", i);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 15:13:56 +00:00
|
|
|
/* Don't sync if we are using downstream memory's stream */
|
2023-02-07 17:25:35 +00:00
|
|
|
if (!stream)
|
|
|
|
gst_cuda_result (CuStreamSynchronize (nullptr));
|
|
|
|
else
|
|
|
|
GST_MINI_OBJECT_FLAG_SET (mem, GST_CUDA_MEMORY_TRANSFER_NEED_SYNC);
|
2020-10-24 17:27:52 +00:00
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
ret = GST_FLOW_OK;
|
2020-10-24 17:27:52 +00:00
|
|
|
|
|
|
|
done:
|
2022-03-02 18:25:47 +00:00
|
|
|
gst_video_frame_unmap (&video_frame);
|
2020-10-24 17:27:52 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
GstFlowReturn
|
2023-09-05 16:00:42 +00:00
|
|
|
gst_nv_decoder_output_picture (GstNvDecoder * decoder,
|
|
|
|
GstVideoDecoder * videodec, GstVideoCodecFrame * frame,
|
|
|
|
GstCodecPicture * picture, guint buffer_flags)
|
2020-04-16 14:27:31 +00:00
|
|
|
{
|
2023-09-05 16:00:42 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
GstNvDecSurface *surface;
|
2022-12-20 15:13:56 +00:00
|
|
|
GstCudaStream *stream;
|
2023-09-16 14:26:27 +00:00
|
|
|
gboolean can_export = FALSE;
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-09-05 16:00:42 +00:00
|
|
|
if (picture->discont_state) {
|
2023-09-27 15:03:08 +00:00
|
|
|
GST_DEBUG_OBJECT (videodec, "Negotiate again on input state change");
|
2023-09-05 16:00:42 +00:00
|
|
|
if (!gst_nv_decoder_negotiate (decoder, videodec, picture->discont_state)) {
|
2022-11-05 12:06:39 +00:00
|
|
|
GST_ERROR_OBJECT (videodec, "Couldn't re-negotiate with updated state");
|
2023-09-05 16:00:42 +00:00
|
|
|
ret = GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
goto error;
|
2022-11-05 12:06:39 +00:00
|
|
|
}
|
2023-09-27 15:03:08 +00:00
|
|
|
} else if (gst_pad_check_reconfigure (videodec->srcpad)) {
|
|
|
|
GST_DEBUG_OBJECT (videodec, "Negotiate again on reconfigure");
|
|
|
|
if (!gst_video_decoder_negotiate (videodec)) {
|
|
|
|
GST_ERROR_OBJECT (videodec,
|
|
|
|
"Couldn't re-negotiate on downstram reconfigure");
|
|
|
|
ret = GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
goto error;
|
|
|
|
}
|
2022-11-05 12:06:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 16:00:42 +00:00
|
|
|
surface = (GstNvDecSurface *) gst_codec_picture_get_user_data (picture);
|
|
|
|
if (!surface) {
|
|
|
|
GST_ERROR_OBJECT (decoder, "No decoder frame in picture %p", picture);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2023-02-07 17:25:35 +00:00
|
|
|
if (!gst_cuda_context_push (decoder->context)) {
|
|
|
|
GST_ERROR_OBJECT (decoder, "Couldn't push context");
|
2023-09-05 16:00:42 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto error;
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 15:13:56 +00:00
|
|
|
stream = decoder->stream;
|
2023-09-05 16:00:42 +00:00
|
|
|
ret = gst_nv_dec_object_map_surface (decoder->object, surface, stream);
|
|
|
|
if (ret != GST_FLOW_OK) {
|
2023-02-07 17:25:35 +00:00
|
|
|
gst_cuda_context_pop (nullptr);
|
2023-09-05 16:00:42 +00:00
|
|
|
goto error;
|
2022-12-20 15:13:56 +00:00
|
|
|
}
|
|
|
|
|
2023-09-16 14:26:27 +00:00
|
|
|
if (videodec->input_segment.rate > 0 &&
|
|
|
|
decoder->output_type == GST_NV_DECODER_OUTPUT_TYPE_CUDA &&
|
2023-02-07 17:25:35 +00:00
|
|
|
(guint) decoder->create_info.ulNumOutputSurfaces >=
|
|
|
|
decoder->downstream_min_buffers) {
|
2023-09-16 14:26:27 +00:00
|
|
|
if (decoder->wait_on_pool_full) {
|
|
|
|
can_export = TRUE;
|
|
|
|
} else {
|
|
|
|
guint num_free_surfaces =
|
|
|
|
gst_nv_dec_object_get_num_free_surfaces (decoder->object);
|
|
|
|
|
|
|
|
/* If downstream didn't propose pool but we have free surfaces */
|
|
|
|
if (num_free_surfaces > 0)
|
|
|
|
can_export = TRUE;
|
|
|
|
else
|
|
|
|
GST_LOG_OBJECT (decoder, "No more free output surface, need copy");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (can_export) {
|
2023-02-07 17:25:35 +00:00
|
|
|
GstMemory *mem;
|
|
|
|
GstCudaMemory *cmem;
|
|
|
|
GstBuffer *buf;
|
|
|
|
GstVideoInfo *info = &decoder->info;
|
|
|
|
|
2023-09-16 14:26:27 +00:00
|
|
|
GST_LOG_OBJECT (decoder, "Exporting output surface without copy");
|
|
|
|
|
2023-09-05 16:00:42 +00:00
|
|
|
ret = gst_nv_dec_object_export_surface (decoder->object,
|
2023-02-07 17:25:35 +00:00
|
|
|
surface, stream, &mem);
|
2023-09-05 16:00:42 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
2023-02-07 17:25:35 +00:00
|
|
|
GST_WARNING_OBJECT (decoder, "Couldn't export surface");
|
|
|
|
gst_nv_dec_object_unmap_surface (decoder->object, surface);
|
|
|
|
gst_cuda_context_pop (nullptr);
|
2023-09-05 16:00:42 +00:00
|
|
|
|
|
|
|
goto error;
|
2023-02-07 17:25:35 +00:00
|
|
|
}
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-02-07 16:42:55 +00:00
|
|
|
gst_cuda_context_pop (nullptr);
|
2023-02-07 17:25:35 +00:00
|
|
|
|
|
|
|
GST_MINI_OBJECT_FLAG_SET (mem, GST_CUDA_MEMORY_TRANSFER_NEED_DOWNLOAD);
|
|
|
|
|
|
|
|
if (stream)
|
|
|
|
GST_MINI_OBJECT_FLAG_SET (mem, GST_CUDA_MEMORY_TRANSFER_NEED_SYNC);
|
|
|
|
|
|
|
|
buf = gst_buffer_new ();
|
|
|
|
cmem = GST_CUDA_MEMORY_CAST (mem);
|
|
|
|
gst_buffer_append_memory (buf, mem);
|
|
|
|
gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE,
|
|
|
|
GST_VIDEO_INFO_FORMAT (info), GST_VIDEO_INFO_WIDTH (info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (info), GST_VIDEO_INFO_N_PLANES (info),
|
|
|
|
cmem->info.offset, cmem->info.stride);
|
|
|
|
|
2023-09-05 16:00:42 +00:00
|
|
|
frame->output_buffer = buf;
|
|
|
|
} else {
|
2023-09-27 15:03:08 +00:00
|
|
|
gboolean need_unmap = TRUE;
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-09-05 16:00:42 +00:00
|
|
|
frame->output_buffer = gst_video_decoder_allocate_output_buffer (videodec);
|
|
|
|
if (!frame->output_buffer) {
|
|
|
|
GST_ERROR_OBJECT (videodec, "Couldn't allocate output buffer");
|
|
|
|
gst_nv_dec_object_unmap_surface (decoder->object, surface);
|
|
|
|
gst_cuda_context_pop (nullptr);
|
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto error;
|
|
|
|
}
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-09-05 16:00:42 +00:00
|
|
|
switch (decoder->output_type) {
|
2023-09-27 15:03:08 +00:00
|
|
|
case GST_NV_DECODER_OUTPUT_TYPE_UNKNOWN:
|
2023-09-05 16:00:42 +00:00
|
|
|
case GST_NV_DECODER_OUTPUT_TYPE_SYSTEM:
|
2023-09-27 15:03:08 +00:00
|
|
|
ret = gst_nv_decoder_copy_frame_to_system (decoder,
|
2023-09-05 16:00:42 +00:00
|
|
|
surface, frame->output_buffer);
|
|
|
|
break;
|
2023-09-27 15:03:08 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
case GST_NV_DECODER_OUTPUT_TYPE_D3D11:
|
|
|
|
ret = gst_nv_decoder_copy_frame_to_d3d11 (decoder, surface,
|
|
|
|
frame->output_buffer);
|
|
|
|
need_unmap = FALSE;
|
|
|
|
break;
|
|
|
|
#endif
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2023-09-05 16:00:42 +00:00
|
|
|
case GST_NV_DECODER_OUTPUT_TYPE_GL:
|
|
|
|
g_assert (decoder->gl_context != nullptr);
|
2021-03-17 05:30:09 +00:00
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
ret = gst_nv_decoder_copy_frame_to_gl (decoder,
|
2023-09-05 16:00:42 +00:00
|
|
|
GST_GL_CONTEXT (decoder->gl_context), surface,
|
|
|
|
frame->output_buffer);
|
|
|
|
break;
|
2020-04-16 14:27:31 +00:00
|
|
|
#endif
|
2023-09-05 16:00:42 +00:00
|
|
|
case GST_NV_DECODER_OUTPUT_TYPE_CUDA:
|
2023-09-27 15:03:08 +00:00
|
|
|
ret = gst_nv_decoder_copy_frame_to_cuda (decoder,
|
2023-09-05 16:00:42 +00:00
|
|
|
surface, frame->output_buffer, stream);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
gst_nv_dec_object_unmap_surface (decoder->object, surface);
|
|
|
|
gst_cuda_context_pop (nullptr);
|
|
|
|
ret = GST_FLOW_ERROR;
|
|
|
|
goto error;
|
|
|
|
}
|
2021-03-17 05:30:09 +00:00
|
|
|
|
2023-09-05 16:00:42 +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
|
|
|
|
*/
|
2023-09-27 15:03:08 +00:00
|
|
|
if (ret != GST_FLOW_OK &&
|
|
|
|
decoder->output_type == GST_NV_DECODER_OUTPUT_TYPE_GL) {
|
2023-09-05 16:00:42 +00:00
|
|
|
GST_WARNING_OBJECT (videodec,
|
|
|
|
"Couldn't copy frame to GL memory, fallback to system memory");
|
|
|
|
decoder->output_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM;
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
ret = gst_nv_decoder_copy_frame_to_system (decoder, surface,
|
2023-09-05 16:00:42 +00:00
|
|
|
frame->output_buffer);
|
|
|
|
}
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
if (need_unmap)
|
|
|
|
gst_nv_dec_object_unmap_surface (decoder->object, surface);
|
2023-09-05 16:00:42 +00:00
|
|
|
gst_cuda_context_pop (nullptr);
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
if (ret != GST_FLOW_OK) {
|
2023-09-05 16:00:42 +00:00
|
|
|
GST_WARNING_OBJECT (videodec, "Failed to copy frame");
|
|
|
|
goto error;
|
|
|
|
}
|
2021-03-17 05:30:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 16:00:42 +00:00
|
|
|
GST_BUFFER_FLAG_SET (frame->output_buffer, buffer_flags);
|
|
|
|
gst_codec_picture_unref (picture);
|
2021-03-17 05:30:09 +00:00
|
|
|
|
2023-09-05 16:00:42 +00:00
|
|
|
return gst_video_decoder_finish_frame (videodec, frame);
|
2021-03-17 05:30:09 +00:00
|
|
|
|
|
|
|
error:
|
2023-09-05 16:00:42 +00:00
|
|
|
gst_codec_picture_unref (picture);
|
|
|
|
gst_video_decoder_release_frame (videodec, frame);
|
|
|
|
|
|
|
|
return ret;
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_NONE = (1 << 0),
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_420_8BITS = (1 << 1),
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_420_10BITS = (1 << 2),
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_420_12BITS = (1 << 3),
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_444_8BITS = (1 << 4),
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_444_10BITS = (1 << 5),
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_444_12BITS = (1 << 6),
|
|
|
|
} GstNvDecoderFormatFlags;
|
|
|
|
|
|
|
|
static gboolean
|
2020-11-12 12:46:59 +00:00
|
|
|
gst_nv_decoder_get_supported_codec_profiles (GValue * profiles,
|
2020-04-16 14:27:31 +00:00
|
|
|
cudaVideoCodec codec, GstNvDecoderFormatFlags flags)
|
|
|
|
{
|
|
|
|
GValue val = G_VALUE_INIT;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
g_value_init (&val, G_TYPE_STRING);
|
|
|
|
|
|
|
|
switch (codec) {
|
|
|
|
case cudaVideoCodec_H264:
|
|
|
|
if ((flags & GST_NV_DECODER_FORMAT_FLAG_420_8BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_420_8BITS) {
|
|
|
|
g_value_set_static_string (&val, "constrained-baseline");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
|
|
|
|
g_value_set_static_string (&val, "baseline");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
|
|
|
|
g_value_set_static_string (&val, "main");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
|
|
|
|
g_value_set_static_string (&val, "high");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
2020-09-22 19:42:37 +00:00
|
|
|
|
|
|
|
g_value_set_static_string (&val, "constrained-high");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
|
|
|
|
g_value_set_static_string (&val, "progressive-high");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* NVDEC supports only 4:2:0 8bits h264 decoding.
|
|
|
|
* following conditions are for the future enhancement */
|
|
|
|
if ((flags & GST_NV_DECODER_FORMAT_FLAG_420_10BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_420_10BITS) {
|
|
|
|
g_value_set_static_string (&val, "high-10");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
2020-09-22 19:42:37 +00:00
|
|
|
|
|
|
|
g_value_set_static_string (&val, "progressive-high-10");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
2020-04-16 14:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & GST_NV_DECODER_FORMAT_FLAG_420_12BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_420_12BITS ||
|
|
|
|
(flags & GST_NV_DECODER_FORMAT_FLAG_444_8BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_444_8BITS ||
|
|
|
|
(flags & GST_NV_DECODER_FORMAT_FLAG_444_10BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_444_10BITS ||
|
|
|
|
(flags & GST_NV_DECODER_FORMAT_FLAG_444_12BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_444_12BITS) {
|
|
|
|
g_value_set_static_string (&val, "high-4:4:4");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
break;
|
|
|
|
case cudaVideoCodec_HEVC:
|
|
|
|
if ((flags & GST_NV_DECODER_FORMAT_FLAG_420_8BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_420_8BITS) {
|
|
|
|
g_value_set_static_string (&val, "main");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & GST_NV_DECODER_FORMAT_FLAG_420_10BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_420_10BITS) {
|
|
|
|
g_value_set_static_string (&val, "main-10");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & GST_NV_DECODER_FORMAT_FLAG_420_12BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_420_12BITS) {
|
|
|
|
g_value_set_static_string (&val, "main-12");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & GST_NV_DECODER_FORMAT_FLAG_444_8BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_444_8BITS) {
|
|
|
|
g_value_set_static_string (&val, "main-444");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & GST_NV_DECODER_FORMAT_FLAG_444_10BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_444_10BITS) {
|
|
|
|
g_value_set_static_string (&val, "main-444-10");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & GST_NV_DECODER_FORMAT_FLAG_444_12BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_444_12BITS) {
|
|
|
|
g_value_set_static_string (&val, "main-444-12");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
}
|
|
|
|
|
2020-10-30 14:22:01 +00:00
|
|
|
ret = TRUE;
|
|
|
|
break;
|
|
|
|
case cudaVideoCodec_VP9:
|
|
|
|
if ((flags & GST_NV_DECODER_FORMAT_FLAG_420_8BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_420_8BITS) {
|
|
|
|
g_value_set_static_string (&val, "0");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & GST_NV_DECODER_FORMAT_FLAG_420_10BITS) ==
|
|
|
|
GST_NV_DECODER_FORMAT_FLAG_420_10BITS) {
|
|
|
|
g_value_set_static_string (&val, "2");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:27:31 +00:00
|
|
|
ret = TRUE;
|
|
|
|
break;
|
2022-05-02 13:59:59 +00:00
|
|
|
case cudaVideoCodec_AV1:
|
|
|
|
g_value_set_static_string (&val, "main");
|
|
|
|
gst_value_list_append_value (profiles, &val);
|
|
|
|
ret = TRUE;
|
2020-04-16 14:27:31 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_value_unset (&val);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
cudaVideoCodec codec;
|
|
|
|
const gchar *codec_name;
|
|
|
|
const gchar *sink_caps_string;
|
|
|
|
} GstNvdecoderCodecMap;
|
|
|
|
|
|
|
|
const GstNvdecoderCodecMap codec_map_list[] = {
|
|
|
|
{cudaVideoCodec_MPEG1, "mpegvideo",
|
|
|
|
"video/mpeg, mpegversion = (int) 1, systemstream = (boolean) false"},
|
|
|
|
{cudaVideoCodec_MPEG2, "mpeg2video",
|
|
|
|
"video/mpeg, mpegversion = (int) 2, systemstream = (boolean) false"},
|
|
|
|
{cudaVideoCodec_MPEG4, "mpeg4video",
|
|
|
|
"video/mpeg, mpegversion = (int) 4, systemstream = (boolean) false"},
|
|
|
|
#if 0
|
|
|
|
/* FIXME: need verification */
|
|
|
|
{cudaVideoCodec_VC1, "vc1"},
|
|
|
|
#endif
|
|
|
|
/* NOTE: common supported h264 profiles for all GPU architecture
|
|
|
|
* 4:2:0, baseline, main, and high profiles
|
|
|
|
*/
|
|
|
|
{cudaVideoCodec_H264, "h264",
|
|
|
|
"video/x-h264, stream-format = (string) byte-stream"
|
|
|
|
", alignment = (string) au"
|
2020-09-22 19:42:37 +00:00
|
|
|
", profile = (string) { constrained-baseline, baseline, main, high, constrained-high, progressive-high }"},
|
2020-04-16 14:27:31 +00:00
|
|
|
{cudaVideoCodec_JPEG, "jpeg", "image/jpeg"},
|
|
|
|
#if 0
|
|
|
|
/* FIXME: need verification */
|
|
|
|
{cudaVideoCodec_H264_SVC, "h264svc"},
|
|
|
|
{cudaVideoCodec_H264_MVC, "h264mvc"},
|
|
|
|
#endif
|
|
|
|
{cudaVideoCodec_HEVC, "h265",
|
|
|
|
"video/x-h265, stream-format = (string) byte-stream"
|
|
|
|
", alignment = (string) au, profile = (string) { main }"},
|
|
|
|
{cudaVideoCodec_VP8, "vp8", "video/x-vp8"},
|
2022-05-02 13:59:59 +00:00
|
|
|
{cudaVideoCodec_VP9, "vp9", "video/x-vp9"},
|
|
|
|
{cudaVideoCodec_AV1, "av1", "video/x-av1, alignment = (string) frame"}
|
2020-04-16 14:27:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_nv_decoder_check_device_caps (CUcontext cuda_ctx, cudaVideoCodec codec,
|
|
|
|
GstCaps ** sink_template, GstCaps ** src_template)
|
|
|
|
{
|
|
|
|
CUresult cuda_ret;
|
2023-02-07 16:42:55 +00:00
|
|
|
guint max_width = 0, min_width = G_MAXINT;
|
|
|
|
guint max_height = 0, min_height = G_MAXINT;
|
|
|
|
GstCaps *sink_templ = nullptr;
|
|
|
|
GstCaps *src_templ = nullptr;
|
2020-04-16 14:27:31 +00:00
|
|
|
/* FIXME: support 12bits format */
|
|
|
|
guint bitdepth_minus8[3] = { 0, 2, 4 };
|
2023-02-07 16:42:55 +00:00
|
|
|
GstNvDecoderFormatFlags format_flags = (GstNvDecoderFormatFlags) 0;
|
|
|
|
guint c_idx, b_idx;
|
2020-04-16 14:27:31 +00:00
|
|
|
cudaVideoChromaFormat chroma_list[] = {
|
|
|
|
#if 0
|
|
|
|
/* FIXME: support monochrome */
|
|
|
|
cudaVideoChromaFormat_Monochrome,
|
|
|
|
/* FIXME: Can our OpenGL support NV16 and its 10/12bits variant?? */
|
|
|
|
cudaVideoChromaFormat_422,
|
|
|
|
#endif
|
|
|
|
cudaVideoChromaFormat_420,
|
|
|
|
cudaVideoChromaFormat_444,
|
|
|
|
};
|
|
|
|
GValue profile_list = G_VALUE_INIT;
|
2023-02-07 16:42:55 +00:00
|
|
|
const GstNvdecoderCodecMap *codec_map = nullptr;
|
2020-04-16 14:27:31 +00:00
|
|
|
guint i;
|
|
|
|
gboolean ret = FALSE;
|
2023-09-21 13:18:56 +00:00
|
|
|
std::set < std::string > formats;
|
2023-09-27 15:03:08 +00:00
|
|
|
std::set < std::string > planar_formats;
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
gboolean is_stateless = FALSE;
|
|
|
|
|
|
|
|
switch (codec) {
|
|
|
|
case cudaVideoCodec_H264:
|
|
|
|
case cudaVideoCodec_HEVC:
|
|
|
|
case cudaVideoCodec_VP8:
|
|
|
|
case cudaVideoCodec_VP9:
|
|
|
|
case cudaVideoCodec_AV1:
|
|
|
|
is_stateless = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2020-04-16 14:27:31 +00:00
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (codec_map_list); i++) {
|
|
|
|
if (codec_map_list[i].codec == codec) {
|
|
|
|
codec_map = &codec_map_list[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!codec_map) {
|
|
|
|
GST_INFO ("No codec map corresponding to codec %d", codec);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_cuvid_can_get_decoder_caps ()) {
|
|
|
|
GST_INFO ("Too old nvidia driver to query decoder capability");
|
|
|
|
|
|
|
|
src_templ = gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("NV12"));
|
|
|
|
|
|
|
|
{
|
2020-09-24 14:33:58 +00:00
|
|
|
GstCaps *cuda_caps = gst_caps_copy (src_templ);
|
|
|
|
gst_caps_set_features_simple (cuda_caps,
|
|
|
|
gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY));
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
if (is_stateless) {
|
|
|
|
GstCaps *d3d11_caps =
|
|
|
|
gst_caps_from_string (GST_VIDEO_CAPS_MAKE ("I420"));
|
|
|
|
gst_caps_set_features_simple (d3d11_caps,
|
|
|
|
gst_caps_features_from_string
|
|
|
|
(GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY));
|
|
|
|
gst_caps_append (src_templ, d3d11_caps);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2020-09-24 14:33:58 +00:00
|
|
|
{
|
|
|
|
GstCaps *gl_caps = gst_caps_copy (src_templ);
|
|
|
|
gst_caps_set_features_simple (gl_caps,
|
|
|
|
gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));
|
|
|
|
gst_caps_append (src_templ, gl_caps);
|
|
|
|
}
|
2020-04-16 14:27:31 +00:00
|
|
|
#endif
|
|
|
|
|
2020-09-24 14:33:58 +00:00
|
|
|
gst_caps_append (src_templ, cuda_caps);
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:27:31 +00:00
|
|
|
sink_templ = gst_caps_from_string (codec_map->sink_caps_string);
|
|
|
|
|
|
|
|
*src_template = src_templ;
|
|
|
|
*sink_template = sink_templ;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_value_init (&profile_list, GST_TYPE_LIST);
|
|
|
|
|
|
|
|
if (CuCtxPushCurrent (cuda_ctx) != CUDA_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
for (c_idx = 0; c_idx < G_N_ELEMENTS (chroma_list); c_idx++) {
|
|
|
|
for (b_idx = 0; b_idx < G_N_ELEMENTS (bitdepth_minus8); b_idx++) {
|
2023-02-07 16:42:55 +00:00
|
|
|
CUVIDDECODECAPS decoder_caps;
|
|
|
|
GstNvDecoderFormatFlags cur_flag = (GstNvDecoderFormatFlags) 0;
|
|
|
|
|
|
|
|
memset (&decoder_caps, 0, sizeof (CUVIDDECODECAPS));
|
2020-04-16 14:27:31 +00:00
|
|
|
|
|
|
|
decoder_caps.eCodecType = codec;
|
|
|
|
decoder_caps.eChromaFormat = chroma_list[c_idx];
|
|
|
|
decoder_caps.nBitDepthMinus8 = bitdepth_minus8[b_idx];
|
|
|
|
|
|
|
|
cuda_ret = CuvidGetDecoderCaps (&decoder_caps);
|
|
|
|
if (cuda_ret != CUDA_SUCCESS) {
|
|
|
|
GST_INFO ("could not query %s decoder capability, ret %d",
|
|
|
|
codec_map->codec_name, cuda_ret);
|
|
|
|
continue;
|
|
|
|
} else if (!decoder_caps.bIsSupported) {
|
|
|
|
GST_LOG ("%s bit-depth %d with chroma format %d is not supported",
|
|
|
|
codec_map->codec_name, bitdepth_minus8[b_idx] + 8, c_idx);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min_width > decoder_caps.nMinWidth)
|
|
|
|
min_width = decoder_caps.nMinWidth;
|
|
|
|
if (min_height > decoder_caps.nMinHeight)
|
|
|
|
min_height = decoder_caps.nMinHeight;
|
|
|
|
if (max_width < decoder_caps.nMaxWidth)
|
|
|
|
max_width = decoder_caps.nMaxWidth;
|
|
|
|
if (max_height < decoder_caps.nMaxHeight)
|
|
|
|
max_height = decoder_caps.nMaxHeight;
|
|
|
|
|
|
|
|
if (chroma_list[c_idx] == cudaVideoChromaFormat_420)
|
|
|
|
cur_flag = GST_NV_DECODER_FORMAT_FLAG_420_8BITS;
|
|
|
|
else
|
|
|
|
cur_flag = GST_NV_DECODER_FORMAT_FLAG_444_8BITS;
|
|
|
|
|
2023-02-07 16:42:55 +00:00
|
|
|
format_flags = (GstNvDecoderFormatFlags) (format_flags |
|
|
|
|
(cur_flag << (bitdepth_minus8[b_idx] / 2)));
|
2020-04-16 14:27:31 +00:00
|
|
|
|
|
|
|
GST_INFO ("%s bit-depth %d with chroma format %d [%d - %d] x [%d - %d]",
|
|
|
|
codec_map->codec_name, bitdepth_minus8[b_idx] + 8, c_idx, min_width,
|
|
|
|
max_width, min_height, max_height);
|
|
|
|
|
|
|
|
switch (chroma_list[c_idx]) {
|
|
|
|
case cudaVideoChromaFormat_420:
|
|
|
|
if (bitdepth_minus8[b_idx] == 0) {
|
2023-09-21 13:18:56 +00:00
|
|
|
formats.insert ("NV12");
|
2023-09-27 15:03:08 +00:00
|
|
|
planar_formats.insert ("I420");
|
2020-04-16 14:27:31 +00:00
|
|
|
} else if (bitdepth_minus8[b_idx] == 2) {
|
2023-09-21 13:18:56 +00:00
|
|
|
formats.insert ("P010_10LE");
|
2023-09-27 15:03:08 +00:00
|
|
|
planar_formats.insert ("I420_10LE");
|
2020-04-16 14:27:31 +00:00
|
|
|
} else if (bitdepth_minus8[b_idx] == 4) {
|
2023-09-21 13:18:56 +00:00
|
|
|
formats.insert ("P012_LE");
|
2023-09-27 15:03:08 +00:00
|
|
|
planar_formats.insert ("I420_12LE");
|
2020-04-16 14:27:31 +00:00
|
|
|
} else {
|
|
|
|
GST_WARNING ("unhandled bitdepth %d", bitdepth_minus8[b_idx] + 8);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case cudaVideoChromaFormat_444:
|
|
|
|
if (cudaVideoCodec_JPEG == codec) {
|
|
|
|
/* NVDEC jpeg decoder can decode 4:4:4 format
|
|
|
|
* but it produces 4:2:0 frame */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bitdepth_minus8[b_idx] == 0) {
|
2023-09-21 13:18:56 +00:00
|
|
|
formats.insert ("Y444");
|
2023-09-27 15:03:08 +00:00
|
|
|
planar_formats.insert ("Y444");
|
|
|
|
if (codec == cudaVideoCodec_HEVC) {
|
2023-09-21 13:18:56 +00:00
|
|
|
formats.insert ("GBR");
|
2023-09-27 15:03:08 +00:00
|
|
|
planar_formats.insert ("GBR");
|
|
|
|
}
|
2020-04-16 14:27:31 +00:00
|
|
|
} else if (bitdepth_minus8[b_idx] == 2 || bitdepth_minus8[b_idx] == 4) {
|
2023-09-21 13:18:56 +00:00
|
|
|
formats.insert ("Y444_16LE");
|
2023-09-27 15:03:08 +00:00
|
|
|
planar_formats.insert ("Y444_16LE");
|
|
|
|
if (codec == cudaVideoCodec_HEVC) {
|
2023-09-21 13:18:56 +00:00
|
|
|
formats.insert ("GBR_16LE");
|
2023-09-27 15:03:08 +00:00
|
|
|
planar_formats.insert ("GBR_16LE");
|
|
|
|
}
|
2020-04-16 14:27:31 +00:00
|
|
|
} else {
|
|
|
|
GST_WARNING ("unhandled bitdepth %d", bitdepth_minus8[b_idx] + 8);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 13:18:56 +00:00
|
|
|
if (formats.empty ()) {
|
2020-04-16 14:27:31 +00:00
|
|
|
GST_INFO ("device can not support %s", codec_map->codec_name);
|
|
|
|
goto done;
|
|
|
|
}
|
2023-09-21 13:18:56 +00:00
|
|
|
#define APPEND_STRING(dst,set,str) G_STMT_START { \
|
|
|
|
if (set.find(str) != set.end()) { \
|
|
|
|
if (!first) \
|
|
|
|
dst += ", "; \
|
|
|
|
dst += str; \
|
|
|
|
first = false; \
|
|
|
|
} \
|
|
|
|
} G_STMT_END
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-09-21 13:18:56 +00:00
|
|
|
{
|
|
|
|
std::string format_str;
|
|
|
|
if (formats.size () == 1) {
|
|
|
|
format_str += *(formats.begin ());
|
|
|
|
} else {
|
|
|
|
bool first = true;
|
|
|
|
format_str += "{ ";
|
|
|
|
APPEND_STRING (format_str, formats, "NV12");
|
|
|
|
APPEND_STRING (format_str, formats, "P010_10LE");
|
|
|
|
APPEND_STRING (format_str, formats, "P012_LE");
|
|
|
|
APPEND_STRING (format_str, formats, "Y444");
|
|
|
|
APPEND_STRING (format_str, formats, "Y444_16LE");
|
|
|
|
APPEND_STRING (format_str, formats, "GBR");
|
|
|
|
APPEND_STRING (format_str, formats, "GBR_16LE");
|
|
|
|
format_str += " }";
|
|
|
|
}
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-09-21 13:18:56 +00:00
|
|
|
std::string src_caps_string =
|
|
|
|
"video/x-raw, format = (string) " + format_str;
|
|
|
|
GstCaps *raw_caps = gst_caps_from_string (src_caps_string.c_str ());
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-09-21 13:18:56 +00:00
|
|
|
src_templ = gst_caps_copy (raw_caps);
|
|
|
|
gst_caps_set_features_simple (src_templ,
|
2020-09-24 14:33:58 +00:00
|
|
|
gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY));
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
if (is_stateless) {
|
|
|
|
format_str.clear ();
|
|
|
|
|
|
|
|
if (planar_formats.size () == 1) {
|
|
|
|
format_str += *(planar_formats.begin ());
|
|
|
|
} else {
|
|
|
|
bool first = true;
|
|
|
|
format_str += "{ ";
|
|
|
|
APPEND_STRING (format_str, planar_formats, "I420");
|
|
|
|
APPEND_STRING (format_str, planar_formats, "I420_10LE");
|
|
|
|
APPEND_STRING (format_str, planar_formats, "I420_12LE");
|
|
|
|
APPEND_STRING (format_str, planar_formats, "Y444");
|
|
|
|
APPEND_STRING (format_str, planar_formats, "Y444_16LE");
|
|
|
|
APPEND_STRING (format_str, planar_formats, "GBR");
|
|
|
|
APPEND_STRING (format_str, planar_formats, "GBR_16LE");
|
|
|
|
format_str += " }";
|
|
|
|
}
|
|
|
|
|
|
|
|
src_caps_string = "video/x-raw, format = (string) " + format_str;
|
|
|
|
GstCaps *d3d11_caps = gst_caps_from_string (src_caps_string.c_str ());
|
|
|
|
gst_caps_set_features_simple (d3d11_caps,
|
|
|
|
gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY));
|
|
|
|
gst_caps_append (src_templ, d3d11_caps);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-09-24 14:33:58 +00:00
|
|
|
/* OpenGL specific */
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2023-09-21 13:18:56 +00:00
|
|
|
format_str.clear ();
|
|
|
|
|
|
|
|
if (formats.size () == 1) {
|
|
|
|
format_str += *(formats.begin ());
|
|
|
|
} else {
|
|
|
|
bool first = true;
|
|
|
|
format_str += "{ ";
|
|
|
|
APPEND_STRING (format_str, formats, "NV12");
|
|
|
|
APPEND_STRING (format_str, formats, "P010_10LE");
|
|
|
|
APPEND_STRING (format_str, formats, "P012_LE");
|
|
|
|
APPEND_STRING (format_str, formats, "Y444");
|
|
|
|
APPEND_STRING (format_str, formats, "GBR");
|
|
|
|
format_str += " }";
|
2020-09-24 14:33:58 +00:00
|
|
|
}
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2023-09-21 13:18:56 +00:00
|
|
|
src_caps_string = "video/x-raw, format = (string) " + format_str;
|
|
|
|
GstCaps *gl_caps = gst_caps_from_string (src_caps_string.c_str ());
|
|
|
|
gst_caps_set_features_simple (gl_caps,
|
|
|
|
gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));
|
|
|
|
gst_caps_append (src_templ, gl_caps);
|
|
|
|
#endif
|
|
|
|
gst_caps_append (src_templ, raw_caps);
|
2020-09-24 14:33:58 +00:00
|
|
|
}
|
2023-09-21 13:18:56 +00:00
|
|
|
#undef APPEND_STRING
|
|
|
|
|
|
|
|
gst_caps_set_simple (src_templ,
|
|
|
|
"width", GST_TYPE_INT_RANGE, min_width, max_width,
|
|
|
|
"height", GST_TYPE_INT_RANGE, min_height, max_height, nullptr);
|
2020-09-24 14:33:58 +00:00
|
|
|
|
2020-04-16 14:27:31 +00:00
|
|
|
sink_templ = gst_caps_from_string (codec_map->sink_caps_string);
|
|
|
|
gst_caps_set_simple (sink_templ,
|
|
|
|
"width", GST_TYPE_INT_RANGE, min_width, max_width,
|
2023-02-07 16:42:55 +00:00
|
|
|
"height", GST_TYPE_INT_RANGE, min_height, max_height, nullptr);
|
2020-04-16 14:27:31 +00:00
|
|
|
|
2020-11-12 12:46:59 +00:00
|
|
|
if (gst_nv_decoder_get_supported_codec_profiles (&profile_list, codec,
|
2020-04-16 14:27:31 +00:00
|
|
|
format_flags)) {
|
|
|
|
gst_caps_set_value (sink_templ, "profile", &profile_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG ("sink template caps %" GST_PTR_FORMAT, sink_templ);
|
|
|
|
GST_DEBUG ("src template caps %" GST_PTR_FORMAT, src_templ);
|
|
|
|
|
2023-02-07 16:42:55 +00:00
|
|
|
CuCtxPopCurrent (nullptr);
|
2020-04-16 14:27:31 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
g_value_unset (&profile_list);
|
|
|
|
|
|
|
|
if (!sink_templ || !src_templ) {
|
|
|
|
gst_clear_caps (&sink_templ);
|
|
|
|
gst_clear_caps (&src_templ);
|
|
|
|
|
|
|
|
ret = FALSE;
|
|
|
|
} else {
|
|
|
|
/* class data will be leaked if the element never gets instantiated */
|
|
|
|
GST_MINI_OBJECT_FLAG_SET (src_templ, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
|
|
|
|
GST_MINI_OBJECT_FLAG_SET (sink_templ, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
|
|
|
|
|
|
|
|
*src_template = src_templ;
|
|
|
|
*sink_template = sink_templ;
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gchar *
|
|
|
|
gst_cuda_video_codec_to_string (cudaVideoCodec codec)
|
|
|
|
{
|
2023-02-07 16:42:55 +00:00
|
|
|
for (guint i = 0; i < G_N_ELEMENTS (codec_map_list); i++) {
|
2020-04-16 14:27:31 +00:00
|
|
|
if (codec_map_list[i].codec == codec)
|
|
|
|
return codec_map_list[i].codec_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown";
|
|
|
|
}
|
2020-06-18 19:50:26 +00:00
|
|
|
|
2023-09-27 14:02:43 +00:00
|
|
|
void
|
2021-03-17 05:30:09 +00:00
|
|
|
gst_nv_decoder_handle_set_context (GstNvDecoder * decoder,
|
2023-09-27 14:02:43 +00:00
|
|
|
GstElement * element, GstContext * context)
|
2020-06-18 19:50:26 +00:00
|
|
|
{
|
2023-09-27 14:02:43 +00:00
|
|
|
if (gst_cuda_handle_set_context (element, context, decoder->device_id,
|
|
|
|
&decoder->context)) {
|
|
|
|
return;
|
2020-06-18 19:50:26 +00:00
|
|
|
}
|
2023-09-27 15:03:08 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
if (gst_d3d11_handle_set_context_for_adapter_luid (element, context,
|
|
|
|
decoder->adapter_luid, (GstD3D11Device **) & decoder->d3d11_device)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-09-27 14:02:43 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
|
|
|
gst_gl_handle_set_context (element, context,
|
|
|
|
(GstGLDisplay **) & decoder->gl_display,
|
|
|
|
(GstGLContext **) & decoder->other_gl_context);
|
2020-06-18 19:50:26 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2023-09-27 14:02:43 +00:00
|
|
|
gst_nv_decoder_handle_query (GstNvDecoder * decoder, GstElement * element,
|
|
|
|
GstQuery * query)
|
2020-06-18 19:50:26 +00:00
|
|
|
{
|
2023-09-27 14:02:43 +00:00
|
|
|
if (GST_QUERY_TYPE (query) != GST_QUERY_CONTEXT)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (gst_cuda_handle_context_query (element, query, decoder->context))
|
|
|
|
return TRUE;
|
2020-06-18 19:50:26 +00:00
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
if (gst_d3d11_handle_context_query (element,
|
|
|
|
query, (GstD3D11Device *) decoder->d3d11_device)) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2023-09-27 14:02:43 +00:00
|
|
|
if (gst_gl_handle_context_query (element, query,
|
2021-03-17 05:30:09 +00:00
|
|
|
(GstGLDisplay *) decoder->gl_display,
|
|
|
|
(GstGLContext *) decoder->gl_context,
|
|
|
|
(GstGLContext *) decoder->other_gl_context)) {
|
|
|
|
if (decoder->gl_display)
|
|
|
|
gst_gl_display_filter_gl_api (GST_GL_DISPLAY (decoder->gl_display),
|
2020-06-18 19:50:26 +00:00
|
|
|
SUPPORTED_GL_APIS);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2020-06-18 19:50:26 +00:00
|
|
|
static void
|
|
|
|
gst_nv_decoder_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-03-17 05:30:09 +00:00
|
|
|
gst_nv_decoder_ensure_gl_context (GstNvDecoder * decoder, GstElement * videodec)
|
2020-06-18 19:50:26 +00:00
|
|
|
{
|
|
|
|
gboolean ret;
|
|
|
|
GstGLDisplay *display;
|
|
|
|
GstGLContext *context;
|
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
if (!gst_gl_ensure_element_data (videodec,
|
|
|
|
(GstGLDisplay **) & decoder->gl_display,
|
|
|
|
(GstGLContext **) & decoder->other_gl_context)) {
|
|
|
|
GST_DEBUG_OBJECT (videodec, "No available OpenGL display");
|
2020-06-18 19:50:26 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
display = GST_GL_DISPLAY (decoder->gl_display);
|
2020-06-18 19:50:26 +00:00
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
if (!gst_gl_query_local_gl_context (videodec, GST_PAD_SRC,
|
|
|
|
(GstGLContext **) & decoder->gl_context)) {
|
|
|
|
GST_INFO_OBJECT (videodec, "failed to query local OpenGL context");
|
2020-06-18 19:50:26 +00:00
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
gst_clear_object (&decoder->gl_context);
|
|
|
|
decoder->gl_context =
|
2023-02-07 16:42:55 +00:00
|
|
|
(GstObject *) gst_gl_display_get_gl_context_for_thread (display,
|
|
|
|
nullptr);
|
|
|
|
if (decoder->gl_context == nullptr
|
2020-06-18 19:50:26 +00:00
|
|
|
|| !gst_gl_display_add_context (display,
|
2021-03-17 05:30:09 +00:00
|
|
|
GST_GL_CONTEXT (decoder->gl_context))) {
|
|
|
|
gst_clear_object (&decoder->gl_context);
|
2020-06-18 19:50:26 +00:00
|
|
|
if (!gst_gl_display_create_context (display,
|
2021-03-17 05:30:09 +00:00
|
|
|
(GstGLContext *) decoder->other_gl_context,
|
2023-02-07 16:42:55 +00:00
|
|
|
(GstGLContext **) & decoder->gl_context, nullptr)) {
|
2021-03-17 05:30:09 +00:00
|
|
|
GST_WARNING_OBJECT (videodec, "failed to create OpenGL context");
|
2020-06-18 19:50:26 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
if (!gst_gl_display_add_context (display,
|
|
|
|
(GstGLContext *) decoder->gl_context)) {
|
|
|
|
GST_WARNING_OBJECT (videodec,
|
2020-06-18 19:50:26 +00:00
|
|
|
"failed to add the OpenGL context to the display");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
context = GST_GL_CONTEXT (decoder->gl_context);
|
2020-06-18 19:50:26 +00:00
|
|
|
|
|
|
|
if (!gst_gl_context_check_gl_version (context, SUPPORTED_GL_APIS, 3, 0)) {
|
2021-03-17 05:30:09 +00:00
|
|
|
GST_WARNING_OBJECT (videodec,
|
2020-06-18 19:50:26 +00:00
|
|
|
"OpenGL context could not support PBO download");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_gl_context_thread_add (context,
|
|
|
|
(GstGLContextThreadFunc) gst_nv_decoder_check_cuda_device_from_context,
|
|
|
|
&ret);
|
|
|
|
|
|
|
|
if (!ret) {
|
2021-03-17 05:30:09 +00:00
|
|
|
GST_WARNING_OBJECT (videodec,
|
2020-06-18 19:50:26 +00:00
|
|
|
"Current OpenGL context is not CUDA-compatible");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
static gboolean
|
|
|
|
gst_nv_decoder_ensure_d3d11_output (GstNvDecoder * self, GstElement * element)
|
|
|
|
{
|
|
|
|
GstVideoFormat out_format = GST_VIDEO_FORMAT_UNKNOWN;
|
|
|
|
GstVideoFormat decoder_format;
|
|
|
|
|
|
|
|
if (!gst_d3d11_ensure_element_data_for_adapter_luid (GST_ELEMENT (element),
|
|
|
|
self->adapter_luid, (GstD3D11Device **) & self->d3d11_device)) {
|
|
|
|
GST_WARNING_OBJECT (element, "D3D11 device is not available");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder_format = GST_VIDEO_INFO_FORMAT (&self->info);
|
|
|
|
switch (decoder_format) {
|
|
|
|
case GST_VIDEO_FORMAT_NV12:
|
|
|
|
out_format = GST_VIDEO_FORMAT_I420;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_P010_10LE:
|
|
|
|
out_format = GST_VIDEO_FORMAT_I420_10LE;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_P012_LE:
|
|
|
|
out_format = GST_VIDEO_FORMAT_I420_12LE;
|
|
|
|
break;
|
|
|
|
case GST_VIDEO_FORMAT_Y444:
|
|
|
|
case GST_VIDEO_FORMAT_Y444_16LE:
|
|
|
|
case GST_VIDEO_FORMAT_GBR:
|
|
|
|
case GST_VIDEO_FORMAT_GBR_16LE:
|
|
|
|
out_format = GST_VIDEO_INFO_FORMAT (&self->info);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
GST_WARNING_OBJECT (element,
|
|
|
|
"Unexpected format %s", gst_video_format_to_string (decoder_format));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_video_info_set_interlaced_format (&self->output_info, out_format,
|
|
|
|
GST_VIDEO_INFO_INTERLACE_MODE (&self->info), self->info.width,
|
|
|
|
self->info.height);
|
|
|
|
|
|
|
|
if (!self->converter) {
|
|
|
|
self->converter = gst_cuda_converter_new (&self->info, &self->output_info,
|
|
|
|
self->context, nullptr);
|
|
|
|
|
|
|
|
if (!self->converter) {
|
|
|
|
GST_WARNING_OBJECT (element, "Couldn't create converter");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!self->convert_buf) {
|
|
|
|
GstMemory *mem = gst_cuda_allocator_alloc (nullptr, self->context,
|
|
|
|
self->stream, &self->output_info);
|
|
|
|
GstCudaMemory *cmem;
|
|
|
|
|
|
|
|
if (!mem) {
|
|
|
|
GST_WARNING_OBJECT (element, "Couldn't allocate memory for conversion");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmem = GST_CUDA_MEMORY_CAST (mem);
|
|
|
|
|
|
|
|
self->convert_buf = gst_buffer_new ();
|
|
|
|
gst_buffer_append_memory (self->convert_buf, mem);
|
|
|
|
gst_buffer_add_video_meta_full (self->convert_buf,
|
|
|
|
GST_VIDEO_FRAME_FLAG_NONE,
|
|
|
|
GST_VIDEO_INFO_FORMAT (&self->output_info),
|
|
|
|
GST_VIDEO_INFO_WIDTH (&self->output_info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&self->output_info),
|
|
|
|
GST_VIDEO_INFO_N_PLANES (&self->output_info),
|
|
|
|
cmem->info.offset, cmem->info.stride);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-18 19:50:26 +00:00
|
|
|
gboolean
|
2021-03-17 05:30:09 +00:00
|
|
|
gst_nv_decoder_negotiate (GstNvDecoder * decoder,
|
2022-11-05 12:00:43 +00:00
|
|
|
GstVideoDecoder * videodec, GstVideoCodecState * input_state)
|
2020-06-18 19:50:26 +00:00
|
|
|
{
|
|
|
|
GstVideoCodecState *state;
|
2023-09-27 15:03:08 +00:00
|
|
|
guint prev_output_type, available_types, selected_type;
|
2020-06-18 19:50:26 +00:00
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
g_return_val_if_fail (GST_IS_NV_DECODER (decoder), FALSE);
|
|
|
|
g_return_val_if_fail (GST_IS_VIDEO_DECODER (videodec), FALSE);
|
2023-02-07 16:42:55 +00:00
|
|
|
g_return_val_if_fail (input_state != nullptr, FALSE);
|
2020-06-18 19:50:26 +00:00
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
if (!decoder->configured) {
|
|
|
|
GST_ERROR_OBJECT (videodec, "Should configure decoder first");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
decoder->output_info = decoder->info;
|
|
|
|
prev_output_type = decoder->output_type;
|
|
|
|
available_types = selected_type = 0;
|
2020-06-18 19:50:26 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
GstCaps *caps;
|
2021-03-17 05:30:09 +00:00
|
|
|
caps = gst_pad_get_allowed_caps (GST_VIDEO_DECODER_SRC_PAD (videodec));
|
|
|
|
GST_DEBUG_OBJECT (videodec, "Allowed caps %" GST_PTR_FORMAT, caps);
|
2020-06-18 19:50:26 +00:00
|
|
|
|
|
|
|
if (!caps || gst_caps_is_any (caps)) {
|
2021-03-17 05:30:09 +00:00
|
|
|
GST_DEBUG_OBJECT (videodec,
|
2020-06-18 19:50:26 +00:00
|
|
|
"cannot determine output format, using system memory");
|
2020-10-24 17:27:52 +00:00
|
|
|
} else {
|
2020-06-18 19:50:26 +00:00
|
|
|
GstCapsFeatures *features;
|
|
|
|
guint size = gst_caps_get_size (caps);
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
features = gst_caps_get_features (caps, i);
|
|
|
|
if (features && gst_caps_features_contains (features,
|
2020-10-24 17:27:52 +00:00
|
|
|
GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY)) {
|
2021-03-17 05:30:09 +00:00
|
|
|
GST_DEBUG_OBJECT (videodec, "found CUDA memory feature");
|
2023-09-27 15:03:08 +00:00
|
|
|
available_types |= GST_NV_DECODER_OUTPUT_TYPE_CUDA;
|
2020-06-18 19:50:26 +00:00
|
|
|
}
|
2023-09-27 15:03:08 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
if (features && gst_caps_features_contains (features,
|
|
|
|
GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY)) {
|
|
|
|
GST_DEBUG_OBJECT (videodec, "found D3D11 memory feature");
|
|
|
|
available_types |= GST_NV_DECODER_OUTPUT_TYPE_D3D11;
|
|
|
|
}
|
|
|
|
#endif
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2023-09-21 13:18:56 +00:00
|
|
|
/* TODO: gl does not support Y444_16 and GBR_16 */
|
2023-09-27 15:03:08 +00:00
|
|
|
if (GST_VIDEO_INFO_FORMAT (&decoder->info) !=
|
|
|
|
GST_VIDEO_FORMAT_Y444_16LE &&
|
|
|
|
GST_VIDEO_INFO_FORMAT (&decoder->info) !=
|
|
|
|
GST_VIDEO_FORMAT_GBR_16LE &&
|
2023-09-21 13:18:56 +00:00
|
|
|
features && gst_caps_features_contains (features,
|
2020-10-24 17:27:52 +00:00
|
|
|
GST_CAPS_FEATURE_MEMORY_GL_MEMORY)) {
|
2021-03-17 05:30:09 +00:00
|
|
|
GST_DEBUG_OBJECT (videodec, "found GL memory feature");
|
2023-09-27 15:03:08 +00:00
|
|
|
available_types |= GST_NV_DECODER_OUTPUT_TYPE_GL;
|
2020-10-24 17:27:52 +00:00
|
|
|
}
|
|
|
|
#endif
|
2020-06-18 19:50:26 +00:00
|
|
|
}
|
2020-10-24 17:27:52 +00:00
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
if (prev_output_type != GST_NV_DECODER_OUTPUT_TYPE_UNKNOWN &&
|
|
|
|
(prev_output_type & available_types) == prev_output_type) {
|
|
|
|
selected_type = prev_output_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selected_type == GST_NV_DECODER_OUTPUT_TYPE_UNKNOWN) {
|
|
|
|
if ((available_types & GST_NV_DECODER_OUTPUT_TYPE_CUDA) != 0)
|
|
|
|
selected_type = GST_NV_DECODER_OUTPUT_TYPE_CUDA;
|
|
|
|
else if ((available_types & GST_NV_DECODER_OUTPUT_TYPE_D3D11) != 0)
|
|
|
|
selected_type = GST_NV_DECODER_OUTPUT_TYPE_D3D11;
|
|
|
|
else if ((available_types & GST_NV_DECODER_OUTPUT_TYPE_GL) != 0)
|
|
|
|
selected_type = GST_NV_DECODER_OUTPUT_TYPE_GL;
|
|
|
|
else
|
|
|
|
selected_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder->output_type = (GstNvDecoderOutputType) selected_type;
|
|
|
|
GST_DEBUG_OBJECT (videodec, "Selected type %d", selected_type);
|
2020-06-18 19:50:26 +00:00
|
|
|
}
|
|
|
|
gst_clear_caps (&caps);
|
|
|
|
}
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
if (decoder->output_type == GST_NV_DECODER_OUTPUT_TYPE_D3D11 &&
|
|
|
|
!gst_nv_decoder_ensure_d3d11_output (decoder, GST_ELEMENT (videodec))) {
|
|
|
|
GST_WARNING_OBJECT (videodec, "D3D11 setup failed");
|
|
|
|
decoder->output_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM;
|
|
|
|
decoder->output_info = decoder->info;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2021-03-17 05:30:09 +00:00
|
|
|
if (decoder->output_type == GST_NV_DECODER_OUTPUT_TYPE_GL &&
|
|
|
|
!gst_nv_decoder_ensure_gl_context (decoder, GST_ELEMENT (videodec))) {
|
|
|
|
GST_WARNING_OBJECT (videodec,
|
2020-06-18 19:50:26 +00:00
|
|
|
"OpenGL context is not CUDA-compatible, fallback to system memory");
|
2021-03-17 05:30:09 +00:00
|
|
|
decoder->output_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM;
|
2020-06-18 19:50:26 +00:00
|
|
|
}
|
2020-10-24 17:27:52 +00:00
|
|
|
#endif
|
2020-06-18 19:50:26 +00:00
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
state = gst_video_decoder_set_interlaced_output_state (videodec,
|
|
|
|
GST_VIDEO_INFO_FORMAT (&decoder->output_info),
|
|
|
|
GST_VIDEO_INFO_INTERLACE_MODE (&decoder->output_info),
|
|
|
|
GST_VIDEO_INFO_WIDTH (&decoder->output_info),
|
|
|
|
GST_VIDEO_INFO_HEIGHT (&decoder->output_info), input_state);
|
|
|
|
state->caps = gst_video_info_to_caps (&state->info);
|
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
switch (decoder->output_type) {
|
2020-11-12 12:46:59 +00:00
|
|
|
case GST_NV_DECODER_OUTPUT_TYPE_CUDA:
|
2021-03-17 05:30:09 +00:00
|
|
|
GST_DEBUG_OBJECT (videodec, "using CUDA memory");
|
2020-10-24 17:27:52 +00:00
|
|
|
gst_caps_set_features (state->caps, 0,
|
2023-09-27 15:03:08 +00:00
|
|
|
gst_caps_features_new_single (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY));
|
2020-10-24 17:27:52 +00:00
|
|
|
break;
|
2023-09-27 15:03:08 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
case GST_NV_DECODER_OUTPUT_TYPE_D3D11:
|
|
|
|
gst_caps_set_features (state->caps, 0,
|
|
|
|
gst_caps_features_new_single (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY));
|
|
|
|
break;
|
|
|
|
#endif
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2020-11-12 12:46:59 +00:00
|
|
|
case GST_NV_DECODER_OUTPUT_TYPE_GL:
|
2021-03-17 05:30:09 +00:00
|
|
|
GST_DEBUG_OBJECT (videodec, "using GL memory");
|
2020-10-24 17:27:52 +00:00
|
|
|
gst_caps_set_features (state->caps, 0,
|
2023-09-27 15:03:08 +00:00
|
|
|
gst_caps_features_new_single (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));
|
2020-10-24 17:27:52 +00:00
|
|
|
gst_caps_set_simple (state->caps, "texture-target", G_TYPE_STRING,
|
2023-02-07 16:42:55 +00:00
|
|
|
"2D", nullptr);
|
2020-10-24 17:27:52 +00:00
|
|
|
break;
|
2020-06-18 19:50:26 +00:00
|
|
|
#endif
|
2020-10-24 17:27:52 +00:00
|
|
|
default:
|
2021-03-17 05:30:09 +00:00
|
|
|
GST_DEBUG_OBJECT (videodec, "using system memory");
|
2020-10-24 17:27:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-06-18 19:50:26 +00:00
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
/* decoder baseclass will hold other reference to output state */
|
|
|
|
gst_video_codec_state_unref (state);
|
|
|
|
|
2020-06-18 19:50:26 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:27:52 +00:00
|
|
|
static gboolean
|
|
|
|
gst_nv_decoder_ensure_cuda_pool (GstNvDecoder * decoder, GstQuery * query)
|
2020-06-18 19:50:26 +00:00
|
|
|
{
|
|
|
|
GstCaps *outcaps;
|
2023-02-07 16:42:55 +00:00
|
|
|
GstBufferPool *pool = nullptr;
|
2023-02-07 17:25:35 +00:00
|
|
|
guint n, size, min = 0, max = 0;
|
2020-06-18 19:50:26 +00:00
|
|
|
GstVideoInfo vinfo = { 0, };
|
|
|
|
GstStructure *config;
|
2023-02-07 17:25:35 +00:00
|
|
|
GstCudaStream *stream;
|
2020-06-18 19:50:26 +00:00
|
|
|
|
2023-02-07 16:42:55 +00:00
|
|
|
gst_query_parse_allocation (query, &outcaps, nullptr);
|
2020-10-24 17:27:52 +00:00
|
|
|
n = gst_query_get_n_allocation_pools (query);
|
|
|
|
if (n > 0) {
|
|
|
|
gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
|
|
|
|
if (pool && !GST_IS_CUDA_BUFFER_POOL (pool)) {
|
|
|
|
gst_object_unref (pool);
|
2023-02-07 16:42:55 +00:00
|
|
|
pool = nullptr;
|
2020-10-24 17:27:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-18 19:50:26 +00:00
|
|
|
|
2020-10-24 17:27:52 +00:00
|
|
|
if (!pool) {
|
|
|
|
GST_DEBUG_OBJECT (decoder, "no downstream pool, create our pool");
|
|
|
|
pool = gst_cuda_buffer_pool_new (decoder->context);
|
|
|
|
|
|
|
|
if (outcaps)
|
|
|
|
gst_video_info_from_caps (&vinfo, outcaps);
|
|
|
|
size = (guint) vinfo.size;
|
2023-09-16 14:26:27 +00:00
|
|
|
decoder->wait_on_pool_full = FALSE;
|
|
|
|
} else {
|
|
|
|
decoder->wait_on_pool_full = TRUE;
|
2020-10-24 17:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config = gst_buffer_pool_get_config (pool);
|
2023-02-07 17:25:35 +00:00
|
|
|
stream = gst_buffer_pool_config_get_cuda_stream (config);
|
|
|
|
if (stream) {
|
|
|
|
GST_DEBUG_OBJECT (decoder, "Downstream CUDA stream is available");
|
|
|
|
gst_clear_cuda_stream (&decoder->stream);
|
|
|
|
decoder->stream = stream;
|
|
|
|
} else if (decoder->stream) {
|
|
|
|
GST_DEBUG_OBJECT (decoder,
|
|
|
|
"Downstream CUDA stream is not available, use ours");
|
|
|
|
gst_buffer_pool_config_set_cuda_stream (config, decoder->stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder->downstream_min_buffers = min;
|
|
|
|
GST_DEBUG_OBJECT (decoder, "Downstream min buffers %d", min);
|
|
|
|
|
|
|
|
/* Since we don't use downstream buffer pool, pre-allocation is unnecessary */
|
|
|
|
min = 0;
|
|
|
|
|
2020-10-24 17:27:52 +00:00
|
|
|
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);
|
2023-02-07 16:42:55 +00:00
|
|
|
gst_buffer_pool_config_get_params (config, nullptr, &size, nullptr, nullptr);
|
2022-03-02 18:25:47 +00:00
|
|
|
gst_structure_free (config);
|
|
|
|
|
2020-10-24 17:27:52 +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;
|
|
|
|
}
|
|
|
|
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2020-10-24 17:27:52 +00:00
|
|
|
static gboolean
|
2021-03-17 05:30:09 +00:00
|
|
|
gst_nv_decoder_ensure_gl_pool (GstNvDecoder * decoder, GstQuery * query)
|
2020-10-24 17:27:52 +00:00
|
|
|
{
|
|
|
|
GstCaps *outcaps;
|
2023-02-07 16:42:55 +00:00
|
|
|
GstBufferPool *pool = nullptr;
|
2020-10-24 17:27:52 +00:00
|
|
|
guint n, size, min, max;
|
|
|
|
GstVideoInfo vinfo = { 0, };
|
|
|
|
GstStructure *config;
|
2021-03-17 05:30:09 +00:00
|
|
|
GstGLContext *gl_context;
|
2020-10-24 17:27:52 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (decoder, "decide allocation");
|
2020-06-18 19:50:26 +00:00
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
if (!decoder->gl_context) {
|
|
|
|
GST_ERROR_OBJECT (decoder, "GL context is not available");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl_context = GST_GL_CONTEXT (decoder->gl_context);
|
|
|
|
|
2023-02-07 16:42:55 +00:00
|
|
|
gst_query_parse_allocation (query, &outcaps, nullptr);
|
2020-06-18 19:50:26 +00:00
|
|
|
n = gst_query_get_n_allocation_pools (query);
|
|
|
|
if (n > 0)
|
|
|
|
gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
|
|
|
|
|
|
|
|
if (pool && !GST_IS_GL_BUFFER_POOL (pool)) {
|
|
|
|
gst_object_unref (pool);
|
2023-02-07 16:42:55 +00:00
|
|
|
pool = nullptr;
|
2020-06-18 19:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pool) {
|
2020-10-24 17:27:52 +00:00
|
|
|
GST_DEBUG_OBJECT (decoder, "no downstream pool, create our pool");
|
2020-06-18 19:50:26 +00:00
|
|
|
pool = gst_gl_buffer_pool_new (GST_GL_CONTEXT (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);
|
2020-10-24 17:27:52 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2020-06-18 19:50:26 +00:00
|
|
|
#endif
|
|
|
|
|
2023-09-27 15:03:08 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
static gboolean
|
|
|
|
gst_nv_decoder_ensure_d3d11_pool (GstNvDecoder * self, GstElement * element,
|
|
|
|
GstQuery * query)
|
|
|
|
{
|
|
|
|
GstCaps *outcaps;
|
|
|
|
GstBufferPool *pool = NULL;
|
|
|
|
guint n, size, min = 0, max = 0;
|
|
|
|
GstVideoInfo info;
|
|
|
|
GstStructure *config;
|
|
|
|
GstD3D11AllocationParams *d3d11_params;
|
|
|
|
GstD3D11Device *device;
|
|
|
|
|
|
|
|
gst_query_parse_allocation (query, &outcaps, nullptr);
|
|
|
|
|
|
|
|
if (!outcaps) {
|
|
|
|
GST_DEBUG_OBJECT (element, "No output caps");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_d3d11_ensure_element_data_for_adapter_luid (element,
|
|
|
|
self->adapter_luid, (GstD3D11Device **) & self->d3d11_device)) {
|
|
|
|
GST_ERROR_OBJECT (element, "Couldn't create d3d11 device");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
device = GST_D3D11_DEVICE (self->d3d11_device);
|
|
|
|
|
|
|
|
gst_video_info_from_caps (&info, outcaps);
|
|
|
|
n = gst_query_get_n_allocation_pools (query);
|
|
|
|
if (n > 0)
|
|
|
|
gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
|
|
|
|
|
|
|
|
/* create our own pool */
|
|
|
|
if (pool) {
|
|
|
|
if (!GST_IS_D3D11_BUFFER_POOL (pool)) {
|
|
|
|
GST_DEBUG_OBJECT (self,
|
|
|
|
"Downstream pool is not d3d11, will create new one");
|
|
|
|
gst_clear_object (&pool);
|
|
|
|
} else {
|
|
|
|
GstD3D11BufferPool *dpool = GST_D3D11_BUFFER_POOL (pool);
|
|
|
|
if (dpool->device != device) {
|
|
|
|
GST_DEBUG_OBJECT (element, "Different device, will create new one");
|
|
|
|
gst_clear_object (&pool);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
pool = gst_d3d11_buffer_pool_new (device);
|
|
|
|
|
|
|
|
size = info.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
d3d11_params = gst_buffer_pool_config_get_d3d11_allocation_params (config);
|
|
|
|
if (!d3d11_params) {
|
|
|
|
d3d11_params = gst_d3d11_allocation_params_new (device, &info,
|
|
|
|
GST_D3D11_ALLOCATION_FLAG_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_pool_config_set_d3d11_allocation_params (config, d3d11_params);
|
|
|
|
gst_d3d11_allocation_params_free (d3d11_params);
|
|
|
|
|
|
|
|
gst_buffer_pool_set_config (pool, config);
|
|
|
|
/* d3d11 buffer pool will update buffer size based on allocated texture,
|
|
|
|
* get size from config again */
|
|
|
|
config = gst_buffer_pool_get_config (pool);
|
|
|
|
gst_buffer_pool_config_get_params (config, nullptr, &size, nullptr, nullptr);
|
|
|
|
gst_structure_free (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);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-10-24 17:27:52 +00:00
|
|
|
gboolean
|
2021-03-17 05:30:09 +00:00
|
|
|
gst_nv_decoder_decide_allocation (GstNvDecoder * decoder,
|
|
|
|
GstVideoDecoder * videodec, GstQuery * query)
|
2020-10-24 17:27:52 +00:00
|
|
|
{
|
2021-03-17 05:30:09 +00:00
|
|
|
gboolean ret = TRUE;
|
2020-10-24 17:27:52 +00:00
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
GST_DEBUG_OBJECT (videodec, "decide allocation");
|
2020-10-24 17:27:52 +00:00
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
switch (decoder->output_type) {
|
2023-09-27 15:03:08 +00:00
|
|
|
case GST_NV_DECODER_OUTPUT_TYPE_UNKNOWN:
|
2021-03-17 05:30:09 +00:00
|
|
|
case GST_NV_DECODER_OUTPUT_TYPE_SYSTEM:
|
|
|
|
/* GstVideoDecoder will take care this case */
|
|
|
|
break;
|
2023-09-27 15:03:08 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
case GST_NV_DECODER_OUTPUT_TYPE_D3D11:
|
|
|
|
ret = gst_nv_decoder_ensure_d3d11_pool (decoder, GST_ELEMENT (videodec),
|
|
|
|
query);
|
|
|
|
break;
|
|
|
|
#endif
|
2023-02-17 15:36:29 +00:00
|
|
|
#ifdef HAVE_CUDA_GST_GL
|
2021-03-17 05:30:09 +00:00
|
|
|
case GST_NV_DECODER_OUTPUT_TYPE_GL:
|
|
|
|
ret = gst_nv_decoder_ensure_gl_pool (decoder, query);
|
|
|
|
break;
|
2020-10-24 17:27:52 +00:00
|
|
|
#endif
|
2021-03-17 05:30:09 +00:00
|
|
|
case GST_NV_DECODER_OUTPUT_TYPE_CUDA:
|
|
|
|
ret = gst_nv_decoder_ensure_cuda_pool (decoder, query);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return FALSE;
|
2020-10-24 17:27:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 05:30:09 +00:00
|
|
|
return ret;
|
2020-06-18 19:50:26 +00:00
|
|
|
}
|
2023-02-07 17:25:35 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
gst_nv_decoder_set_flushing (GstNvDecoder * decoder, gboolean flushing)
|
|
|
|
{
|
|
|
|
g_mutex_lock (&decoder->lock);
|
|
|
|
if (decoder->object)
|
|
|
|
gst_nv_dec_object_set_flushing (decoder->object, flushing);
|
|
|
|
g_mutex_unlock (&decoder->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gst_nv_decoder_reset (GstNvDecoder * decoder)
|
|
|
|
{
|
|
|
|
g_mutex_lock (&decoder->lock);
|
|
|
|
gst_nv_decoder_reset_unlocked (decoder);
|
|
|
|
g_mutex_unlock (&decoder->lock);
|
|
|
|
}
|
2023-02-13 12:59:05 +00:00
|
|
|
|
|
|
|
guint
|
|
|
|
gst_nv_decoder_get_max_output_size (guint coded_size, guint user_requested,
|
|
|
|
guint device_max)
|
|
|
|
{
|
|
|
|
if (user_requested <= coded_size)
|
|
|
|
return coded_size;
|
|
|
|
|
|
|
|
user_requested = GST_ROUND_UP_16 (user_requested);
|
|
|
|
|
|
|
|
return MIN (user_requested, device_max);
|
|
|
|
}
|