nvcodec: nvsldec: Refactor graphics api resource handling

* Move GL context object to GstNVDecoder object, and remove
  duplicated handling of each codec decoder element
* Don't create GL context too early. We can create it only if
  we need to negotiate with downstream gl element.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/2089>
This commit is contained in:
Seungha Yang 2021-03-17 14:30:09 +09:00 committed by GStreamer Marge Bot
parent be1f66a491
commit 791f1da7b8
6 changed files with 471 additions and 570 deletions

View file

@ -67,20 +67,39 @@ typedef struct _GstNvDecoderFrameInfo
gboolean available; gboolean available;
} GstNvDecoderFrameInfo; } GstNvDecoderFrameInfo;
typedef enum
{
GST_NV_DECODER_OUTPUT_TYPE_SYSTEM = 0,
GST_NV_DECODER_OUTPUT_TYPE_GL,
GST_NV_DECODER_OUTPUT_TYPE_CUDA,
/* FIXME: add support D3D11 memory */
} GstNvDecoderOutputType;
struct _GstNvDecoder struct _GstNvDecoder
{ {
GstObject parent; GstObject parent;
GstCudaContext *context; GstCudaContext *context;
CUstream cuda_stream;
CUvideodecoder decoder_handle; CUvideodecoder decoder_handle;
GstNvDecoderFrameInfo *frame_pool; GstNvDecoderFrameInfo *frame_pool;
guint pool_size; guint pool_size;
GstVideoInfo info; GstVideoInfo info;
GstVideoInfo coded_info;
gboolean configured;
/* For OpenGL interop. */
GstObject *gl_display;
GstObject *gl_context;
GstObject *other_gl_context;
GstNvDecoderOutputType output_type;
}; };
static void gst_nv_decoder_dispose (GObject * object); static void gst_nv_decoder_dispose (GObject * object);
static void gst_nv_decoder_finalize (GObject * object); static void gst_nv_decoder_reset (GstNvDecoder * self);
#define parent_class gst_nv_decoder_parent_class #define parent_class gst_nv_decoder_parent_class
G_DEFINE_TYPE (GstNvDecoder, gst_nv_decoder, GST_TYPE_OBJECT); G_DEFINE_TYPE (GstNvDecoder, gst_nv_decoder, GST_TYPE_OBJECT);
@ -91,7 +110,6 @@ gst_nv_decoder_class_init (GstNvDecoderClass * klass)
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = gst_nv_decoder_dispose; gobject_class->dispose = gst_nv_decoder_dispose;
gobject_class->finalize = gst_nv_decoder_finalize;
} }
static void static void
@ -104,21 +122,22 @@ gst_nv_decoder_dispose (GObject * object)
{ {
GstNvDecoder *self = GST_NV_DECODER (object); GstNvDecoder *self = GST_NV_DECODER (object);
gst_clear_object (&self->context); gst_nv_decoder_reset (self);
G_OBJECT_CLASS (parent_class)->dispose (object); if (self->context && self->cuda_stream) {
if (gst_cuda_context_push (self->context)) {
gst_cuda_result (CuStreamDestroy (self->cuda_stream));
gst_cuda_context_pop (NULL);
self->cuda_stream = NULL;
}
} }
static void gst_clear_object (&self->context);
gst_nv_decoder_finalize (GObject * object) gst_clear_object (&self->gl_display);
{ gst_clear_object (&self->gl_context);
GstNvDecoder *self = GST_NV_DECODER (object); gst_clear_object (&self->other_gl_context);
g_free (self->frame_pool); G_OBJECT_CLASS (parent_class)->dispose (object);
if (self->decoder_handle)
gst_cuda_result (CuvidDestroyDecoder (self->decoder_handle));
G_OBJECT_CLASS (parent_class)->finalize (object);
} }
static cudaVideoChromaFormat static cudaVideoChromaFormat
@ -206,27 +225,81 @@ gst_nv_decoder_prepare_frame_pool (GstNvDecoder * self, guint pool_size)
} }
GstNvDecoder * GstNvDecoder *
gst_nv_decoder_new (GstCudaContext * context, cudaVideoCodec codec, gst_nv_decoder_new (GstCudaContext * context)
GstVideoInfo * info, guint pool_size)
{ {
GstNvDecoder *decoder; GstNvDecoder *self;
CUVIDDECODECREATEINFO create_info = { 0, };
GstVideoFormat format;
g_return_val_if_fail (GST_IS_CUDA_CONTEXT (context), NULL); g_return_val_if_fail (GST_IS_CUDA_CONTEXT (context), NULL);
g_return_val_if_fail (codec < cudaVideoCodec_NumCodecs, NULL);
g_return_val_if_fail (info != NULL, NULL);
g_return_val_if_fail (pool_size > 0, NULL);
decoder = g_object_new (GST_TYPE_NV_DECODER, NULL); self = g_object_new (GST_TYPE_NV_DECODER, NULL);
decoder->context = gst_object_ref (context); self->context = gst_object_ref (context);
gst_object_ref_sink (decoder); gst_object_ref_sink (self);
if (gst_cuda_context_push (context)) {
CUresult cuda_ret;
cuda_ret = CuStreamCreate (&self->cuda_stream, CU_STREAM_DEFAULT);
if (!gst_cuda_result (cuda_ret)) {
GST_WARNING_OBJECT (self,
"Could not create CUDA stream, will use default stream");
self->cuda_stream = NULL;
}
gst_cuda_context_pop (NULL);
}
return self;
}
gboolean
gst_nv_decoder_is_configured (GstNvDecoder * decoder)
{
g_return_val_if_fail (GST_IS_NV_DECODER (decoder), FALSE);
return decoder->configured;
}
static void
gst_nv_decoder_reset (GstNvDecoder * self)
{
g_clear_pointer (&self->frame_pool, g_free);
if (self->decoder_handle) {
gst_cuda_context_push (self->context);
CuvidDestroyDecoder (self->decoder_handle);
gst_cuda_context_pop (NULL);
self->decoder_handle = NULL;
}
self->output_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM;
self->configured = FALSE;
}
gboolean
gst_nv_decoder_configure (GstNvDecoder * decoder, cudaVideoCodec codec,
GstVideoInfo * info, gint coded_width, gint coded_height, guint pool_size)
{
CUVIDDECODECREATEINFO create_info = { 0, };
GstVideoFormat format;
gboolean ret;
g_return_val_if_fail (GST_IS_NV_DECODER (decoder), FALSE);
g_return_val_if_fail (codec < cudaVideoCodec_NumCodecs, FALSE);
g_return_val_if_fail (info != NULL, FALSE);
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);
g_return_val_if_fail (pool_size > 0, FALSE);
gst_nv_decoder_reset (decoder);
decoder->info = *info;
gst_video_info_set_format (&decoder->coded_info, GST_VIDEO_INFO_FORMAT (info),
coded_width, coded_height);
format = GST_VIDEO_INFO_FORMAT (info); format = GST_VIDEO_INFO_FORMAT (info);
/* FIXME: check aligned resolution or actaul coded resolution */ /* FIXME: check aligned resolution or actual coded resolution */
create_info.ulWidth = GST_VIDEO_INFO_WIDTH (info);; create_info.ulWidth = GST_VIDEO_INFO_WIDTH (&decoder->coded_info);
create_info.ulHeight = GST_VIDEO_INFO_HEIGHT (info);; create_info.ulHeight = GST_VIDEO_INFO_HEIGHT (&decoder->coded_info);
create_info.ulNumDecodeSurfaces = pool_size; create_info.ulNumDecodeSurfaces = pool_size;
create_info.CodecType = codec; create_info.CodecType = codec;
create_info.ChromaFormat = chroma_format_from_video_format (format); create_info.ChromaFormat = chroma_format_from_video_format (format);
@ -241,44 +314,39 @@ gst_nv_decoder_new (GstCudaContext * context, cudaVideoCodec codec,
create_info.OutputFormat = output_format_from_video_format (format); create_info.OutputFormat = output_format_from_video_format (format);
create_info.DeinterlaceMode = cudaVideoDeinterlaceMode_Weave; create_info.DeinterlaceMode = cudaVideoDeinterlaceMode_Weave;
create_info.ulTargetWidth = GST_VIDEO_INFO_WIDTH (info);; create_info.ulTargetWidth = GST_VIDEO_INFO_WIDTH (&decoder->coded_info);
create_info.ulTargetHeight = GST_VIDEO_INFO_HEIGHT (info); create_info.ulTargetHeight = GST_VIDEO_INFO_HEIGHT (&decoder->coded_info);
/* we always copy decoded picture to output buffer */ /* we always copy decoded picture to output buffer */
create_info.ulNumOutputSurfaces = 1; create_info.ulNumOutputSurfaces = 1;
create_info.target_rect.left = 0; create_info.target_rect.left = 0;
create_info.target_rect.top = 0; create_info.target_rect.top = 0;
create_info.target_rect.right = GST_VIDEO_INFO_WIDTH (info); create_info.target_rect.right = GST_VIDEO_INFO_WIDTH (&decoder->coded_info);
create_info.target_rect.bottom = GST_VIDEO_INFO_HEIGHT (info); create_info.target_rect.bottom = GST_VIDEO_INFO_HEIGHT (&decoder->coded_info);
if (!gst_cuda_context_push (context)) { if (!gst_cuda_context_push (decoder->context)) {
GST_ERROR_OBJECT (decoder, "Failed to lock CUDA context"); GST_ERROR_OBJECT (decoder, "Failed to lock CUDA context");
goto error; return FALSE;
} }
if (!gst_cuda_result (CuvidCreateDecoder (&decoder->decoder_handle, ret = gst_cuda_result (CuvidCreateDecoder (&decoder->decoder_handle,
&create_info))) { &create_info));
gst_cuda_context_pop (NULL);
if (!ret) {
GST_ERROR_OBJECT (decoder, "Cannot create decoder instance"); GST_ERROR_OBJECT (decoder, "Cannot create decoder instance");
goto error; return FALSE;
}
if (!gst_cuda_context_pop (NULL)) {
GST_ERROR_OBJECT (decoder, "Failed to unlock CUDA context");
goto error;
} }
if (!gst_nv_decoder_prepare_frame_pool (decoder, pool_size)) { if (!gst_nv_decoder_prepare_frame_pool (decoder, pool_size)) {
GST_ERROR_OBJECT (decoder, "Cannot prepare internal surface buffer pool"); GST_ERROR_OBJECT (decoder, "Cannot prepare internal surface buffer pool");
goto error; gst_nv_decoder_reset (decoder);
return FALSE;
} }
decoder->info = *info; decoder->configured = TRUE;
return decoder; return TRUE;
error:
gst_clear_object (&decoder);
return NULL;
} }
GstNvDecoderFrame * GstNvDecoderFrame *
@ -593,7 +661,7 @@ gst_nv_decoder_copy_frame_to_gl_internal (GstGLContext * context,
* GST_VIDEO_INFO_COMP_PSTRIDE (info, i); * GST_VIDEO_INFO_COMP_PSTRIDE (info, i);
copy_params.srcDevice = frame->devptr + copy_params.srcDevice = frame->devptr +
(i * frame->pitch * GST_VIDEO_INFO_HEIGHT (info)); (i * frame->pitch * GST_VIDEO_INFO_HEIGHT (&self->coded_info));
copy_params.dstDevice = dst_ptr; copy_params.dstDevice = dst_ptr;
copy_params.Height = GST_VIDEO_INFO_COMP_HEIGHT (info, i); copy_params.Height = GST_VIDEO_INFO_COMP_HEIGHT (info, i);
@ -662,18 +730,18 @@ gst_nv_decoder_copy_frame_to_system (GstNvDecoder * decoder,
for (i = 0; i < GST_VIDEO_FRAME_N_PLANES (&video_frame); i++) { for (i = 0; i < GST_VIDEO_FRAME_N_PLANES (&video_frame); i++) {
copy_params.srcDevice = frame->devptr + copy_params.srcDevice = frame->devptr +
(i * frame->pitch * GST_VIDEO_INFO_HEIGHT (&decoder->info)); (i * frame->pitch * GST_VIDEO_INFO_HEIGHT (&decoder->coded_info));
copy_params.dstHost = GST_VIDEO_FRAME_PLANE_DATA (&video_frame, i); copy_params.dstHost = GST_VIDEO_FRAME_PLANE_DATA (&video_frame, i);
copy_params.dstPitch = GST_VIDEO_FRAME_PLANE_STRIDE (&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); copy_params.Height = GST_VIDEO_FRAME_COMP_HEIGHT (&video_frame, i);
if (!gst_cuda_result (CuMemcpy2DAsync (&copy_params, NULL))) { if (!gst_cuda_result (CuMemcpy2DAsync (&copy_params, decoder->cuda_stream))) {
GST_ERROR_OBJECT (decoder, "failed to copy %dth plane", i); GST_ERROR_OBJECT (decoder, "failed to copy %dth plane", i);
goto done; goto done;
} }
} }
gst_cuda_result (CuStreamSynchronize (NULL)); gst_cuda_result (CuStreamSynchronize (decoder->cuda_stream));
ret = TRUE; ret = TRUE;
@ -737,13 +805,13 @@ gst_nv_decoder_copy_frame_to_cuda (GstNvDecoder * decoder,
* GST_VIDEO_INFO_COMP_PSTRIDE (&decoder->info, 0); * GST_VIDEO_INFO_COMP_PSTRIDE (&decoder->info, 0);
copy_params.Height = GST_VIDEO_INFO_COMP_HEIGHT (&decoder->info, i); copy_params.Height = GST_VIDEO_INFO_COMP_HEIGHT (&decoder->info, i);
if (!gst_cuda_result (CuMemcpy2DAsync (&copy_params, NULL))) { if (!gst_cuda_result (CuMemcpy2DAsync (&copy_params, decoder->cuda_stream))) {
GST_ERROR_OBJECT (decoder, "failed to copy %dth plane", i); GST_ERROR_OBJECT (decoder, "failed to copy %dth plane", i);
goto done; goto done;
} }
} }
gst_cuda_result (CuStreamSynchronize (NULL)); gst_cuda_result (CuStreamSynchronize (decoder->cuda_stream));
ret = TRUE; ret = TRUE;
@ -756,55 +824,84 @@ done:
} }
gboolean gboolean
gst_nv_decoder_finish_frame (GstNvDecoder * decoder, gst_nv_decoder_finish_frame (GstNvDecoder * decoder, GstVideoDecoder * videodec,
GstNvDecoderOutputType output_type, GstObject * graphics_context, GstNvDecoderFrame * frame, GstBuffer ** buffer)
GstNvDecoderFrame * frame, GstBuffer * buffer)
{ {
GstBuffer *outbuf = NULL;
gboolean ret = FALSE; gboolean ret = FALSE;
g_return_val_if_fail (GST_IS_NV_DECODER (decoder), FALSE); g_return_val_if_fail (GST_IS_NV_DECODER (decoder), GST_FLOW_ERROR);
g_return_val_if_fail (frame != NULL, FALSE); g_return_val_if_fail (GST_IS_VIDEO_DECODER (videodec), GST_FLOW_ERROR);
g_return_val_if_fail (GST_IS_BUFFER (buffer), FALSE); g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
#ifdef HAVE_NVCODEC_GST_GL outbuf = gst_video_decoder_allocate_output_buffer (videodec);
if (output_type == GST_NV_DECODER_OUTPUT_TYPE_GL && !graphics_context) { if (!outbuf) {
if (!GST_IS_GL_CONTEXT (graphics_context)) { GST_ERROR_OBJECT (videodec, "Couldn't allocate output buffer");
GST_ERROR_OBJECT (decoder, "Invalid GL Context");
return FALSE; return FALSE;
} }
}
#endif
if (!gst_cuda_context_push (decoder->context)) { if (!gst_cuda_context_push (decoder->context)) {
GST_ERROR_OBJECT (decoder, "Failed to push CUDA context"); GST_ERROR_OBJECT (decoder, "Failed to push CUDA context");
return FALSE; goto error;
} }
if (!gst_nv_decoder_frame_map (frame)) { if (!gst_nv_decoder_frame_map (frame)) {
GST_ERROR_OBJECT (decoder, "Couldn't map frame"); GST_ERROR_OBJECT (decoder, "Couldn't map frame");
gst_cuda_context_pop (NULL); gst_cuda_context_pop (NULL);
return FALSE; goto error;
} }
gst_cuda_context_pop (NULL); gst_cuda_context_pop (NULL);
switch (decoder->output_type) {
case GST_NV_DECODER_OUTPUT_TYPE_SYSTEM:
ret = gst_nv_decoder_copy_frame_to_system (decoder, frame, outbuf);
break;
#ifdef HAVE_NVCODEC_GST_GL #ifdef HAVE_NVCODEC_GST_GL
if (output_type == GST_NV_DECODER_OUTPUT_TYPE_GL) { case GST_NV_DECODER_OUTPUT_TYPE_GL:
g_assert (decoder->gl_context != NULL);
ret = gst_nv_decoder_copy_frame_to_gl (decoder, ret = gst_nv_decoder_copy_frame_to_gl (decoder,
GST_GL_CONTEXT (graphics_context), frame, buffer); GST_GL_CONTEXT (decoder->gl_context), frame, outbuf);
} else break;
#endif #endif
if (output_type == GST_NV_DECODER_OUTPUT_TYPE_CUDA) { case GST_NV_DECODER_OUTPUT_TYPE_CUDA:
ret = gst_nv_decoder_copy_frame_to_cuda (decoder, frame, buffer); ret = gst_nv_decoder_copy_frame_to_cuda (decoder, frame, outbuf);
} else { break;
ret = gst_nv_decoder_copy_frame_to_system (decoder, frame, buffer); default:
g_assert_not_reached ();
goto error;
}
/* FIXME: This is the case where OpenGL context of downstream glbufferpool
* belongs to non-nvidia (or different device).
* There should be enhancement to ensure nvdec has compatible OpenGL context
*/
if (!ret && decoder->output_type == GST_NV_DECODER_OUTPUT_TYPE_GL) {
GST_WARNING_OBJECT (videodec,
"Couldn't copy frame to GL memory, fallback to system memory");
decoder->output_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM;
ret = gst_nv_decoder_copy_frame_to_system (decoder, frame, outbuf);
} }
gst_cuda_context_push (decoder->context); gst_cuda_context_push (decoder->context);
gst_nv_decoder_frame_unmap (frame); gst_nv_decoder_frame_unmap (frame);
gst_cuda_context_pop (NULL); gst_cuda_context_pop (NULL);
return ret; if (!ret) {
GST_WARNING_OBJECT (videodec, "Failed to copy frame");
goto error;
}
*buffer = outbuf;
return TRUE;
error:
gst_clear_buffer (&outbuf);
return FALSE;
} }
typedef enum typedef enum
@ -1230,84 +1327,37 @@ gst_cuda_video_codec_to_string (cudaVideoCodec codec)
} }
gboolean gboolean
gst_nv_decoder_ensure_element_data (GstElement * decoder, guint cuda_device_id, gst_nv_decoder_handle_set_context (GstNvDecoder * decoder,
GstCudaContext ** cuda_context, CUstream * cuda_stream, GstElement * videodec, GstContext * context)
GstObject ** gl_display, GstObject ** other_gl_context)
{ {
CUresult cuda_ret; g_return_val_if_fail (GST_IS_NV_DECODER (decoder), FALSE);
g_return_val_if_fail (GST_IS_ELEMENT (videodec), FALSE);
g_return_val_if_fail (GST_IS_ELEMENT (decoder), FALSE); #ifdef HAVE_NVCODEC_GST_GL
g_return_val_if_fail (cuda_context, FALSE); if (gst_gl_handle_set_context (videodec, context,
g_return_val_if_fail (cuda_stream, FALSE); (GstGLDisplay **) & decoder->gl_display,
g_return_val_if_fail (gl_display, FALSE); (GstGLContext **) & decoder->other_gl_context)) {
g_return_val_if_fail (other_gl_context, FALSE); return TRUE;
}
#endif
if (!gst_cuda_ensure_element_context (decoder, cuda_device_id, cuda_context)) {
GST_ERROR_OBJECT (decoder, "failed to create CUDA context");
return FALSE; return FALSE;
} }
if (gst_cuda_context_push (*cuda_context)) {
CUstream stream;
cuda_ret = CuStreamCreate (&stream, CU_STREAM_DEFAULT);
if (!gst_cuda_result (cuda_ret)) {
GST_WARNING_OBJECT (decoder,
"Could not create CUDA stream, will use default stream");
*cuda_stream = NULL;
} else {
*cuda_stream = stream;
}
gst_cuda_context_pop (NULL);
}
#if HAVE_NVCODEC_GST_GL
gst_gl_ensure_element_data (decoder,
(GstGLDisplay **) gl_display, (GstGLContext **) other_gl_context);
if (*gl_display)
gst_gl_display_filter_gl_api (GST_GL_DISPLAY (*gl_display),
SUPPORTED_GL_APIS);
#endif
return TRUE;
}
void
gst_nv_decoder_set_context (GstElement * decoder, GstContext * context,
guint cuda_device_id, GstCudaContext ** cuda_context,
GstObject ** gl_display, GstObject ** other_gl_context)
{
g_return_if_fail (GST_IS_ELEMENT (decoder));
g_return_if_fail (GST_IS_CONTEXT (context));
g_return_if_fail (cuda_context != NULL);
g_return_if_fail (gl_display != NULL);
g_return_if_fail (other_gl_context != NULL);
if (gst_cuda_handle_set_context (decoder, context, cuda_device_id,
cuda_context)) {
return;
}
#ifdef HAVE_NVCODEC_GST_GL
gst_gl_handle_set_context (decoder, context,
(GstGLDisplay **) gl_display, (GstGLContext **) other_gl_context);
#endif
}
gboolean gboolean
gst_nv_decoder_handle_context_query (GstElement * decoder, GstQuery * query, gst_nv_decoder_handle_context_query (GstNvDecoder * decoder,
GstCudaContext * cuda_context, GstObject * gl_display, GstVideoDecoder * videodec, GstQuery * query)
GstObject * gl_context, GstObject * other_gl_context)
{ {
g_return_val_if_fail (GST_IS_ELEMENT (decoder), FALSE); g_return_val_if_fail (GST_IS_NV_DECODER (decoder), FALSE);
g_return_val_if_fail (GST_IS_ELEMENT (videodec), FALSE);
if (gst_cuda_handle_context_query (decoder, query, cuda_context)) {
return TRUE;
}
#ifdef HAVE_NVCODEC_GST_GL #ifdef HAVE_NVCODEC_GST_GL
if (gst_gl_handle_context_query (GST_ELEMENT (decoder), query, if (gst_gl_handle_context_query (GST_ELEMENT (videodec), query,
(GstGLDisplay *) gl_display, (GstGLDisplay *) decoder->gl_display,
(GstGLContext *) gl_context, (GstGLContext *) other_gl_context)) { (GstGLContext *) decoder->gl_context,
if (gl_display) (GstGLContext *) decoder->other_gl_context)) {
gst_gl_display_filter_gl_api (GST_GL_DISPLAY (gl_display), if (decoder->gl_display)
gst_gl_display_filter_gl_api (GST_GL_DISPLAY (decoder->gl_display),
SUPPORTED_GL_APIS); SUPPORTED_GL_APIS);
return TRUE; return TRUE;
} }
@ -1339,50 +1389,52 @@ gst_nv_decoder_check_cuda_device_from_context (GstGLContext * context,
} }
static gboolean static gboolean
gst_nv_decoder_ensure_gl_context (GstElement * decoder, GstObject * gl_display, gst_nv_decoder_ensure_gl_context (GstNvDecoder * decoder, GstElement * videodec)
GstObject * other_gl_context, GstObject ** gl_context)
{ {
gboolean ret; gboolean ret;
GstGLDisplay *display; GstGLDisplay *display;
GstGLContext *context; GstGLContext *context;
if (!gl_display) { if (!gst_gl_ensure_element_data (videodec,
GST_DEBUG_OBJECT (decoder, "No available OpenGL display"); (GstGLDisplay **) & decoder->gl_display,
(GstGLContext **) & decoder->other_gl_context)) {
GST_DEBUG_OBJECT (videodec, "No available OpenGL display");
return FALSE; return FALSE;
} }
display = GST_GL_DISPLAY (gl_display); display = GST_GL_DISPLAY (decoder->gl_display);
if (!gst_gl_query_local_gl_context (decoder, GST_PAD_SRC, if (!gst_gl_query_local_gl_context (videodec, GST_PAD_SRC,
(GstGLContext **) gl_context)) { (GstGLContext **) & decoder->gl_context)) {
GST_INFO_OBJECT (decoder, "failed to query local OpenGL context"); GST_INFO_OBJECT (videodec, "failed to query local OpenGL context");
gst_clear_object (gl_context); gst_clear_object (&decoder->gl_context);
*gl_context = decoder->gl_context =
(GstObject *) gst_gl_display_get_gl_context_for_thread (display, NULL); (GstObject *) gst_gl_display_get_gl_context_for_thread (display, NULL);
if (*gl_context == NULL if (decoder->gl_context == NULL
|| !gst_gl_display_add_context (display, || !gst_gl_display_add_context (display,
GST_GL_CONTEXT (*gl_context))) { GST_GL_CONTEXT (decoder->gl_context))) {
gst_clear_object (gl_context); gst_clear_object (&decoder->gl_context);
if (!gst_gl_display_create_context (display, if (!gst_gl_display_create_context (display,
(GstGLContext *) other_gl_context, (GstGLContext *) decoder->other_gl_context,
(GstGLContext **) gl_context, NULL)) { (GstGLContext **) & decoder->gl_context, NULL)) {
GST_WARNING_OBJECT (decoder, "failed to create OpenGL context"); GST_WARNING_OBJECT (videodec, "failed to create OpenGL context");
return FALSE; return FALSE;
} }
if (!gst_gl_display_add_context (display, (GstGLContext *) * gl_context)) { if (!gst_gl_display_add_context (display,
GST_WARNING_OBJECT (decoder, (GstGLContext *) decoder->gl_context)) {
GST_WARNING_OBJECT (videodec,
"failed to add the OpenGL context to the display"); "failed to add the OpenGL context to the display");
return FALSE; return FALSE;
} }
} }
} }
context = GST_GL_CONTEXT (*gl_context); context = GST_GL_CONTEXT (decoder->gl_context);
if (!gst_gl_context_check_gl_version (context, SUPPORTED_GL_APIS, 3, 0)) { if (!gst_gl_context_check_gl_version (context, SUPPORTED_GL_APIS, 3, 0)) {
GST_WARNING_OBJECT (decoder, GST_WARNING_OBJECT (videodec,
"OpenGL context could not support PBO download"); "OpenGL context could not support PBO download");
return FALSE; return FALSE;
} }
@ -1392,7 +1444,7 @@ gst_nv_decoder_ensure_gl_context (GstElement * decoder, GstObject * gl_display,
&ret); &ret);
if (!ret) { if (!ret) {
GST_WARNING_OBJECT (decoder, GST_WARNING_OBJECT (videodec,
"Current OpenGL context is not CUDA-compatible"); "Current OpenGL context is not CUDA-compatible");
return FALSE; return FALSE;
} }
@ -1402,40 +1454,42 @@ gst_nv_decoder_ensure_gl_context (GstElement * decoder, GstObject * gl_display,
#endif #endif
gboolean gboolean
gst_nv_decoder_negotiate (GstVideoDecoder * decoder, gst_nv_decoder_negotiate (GstNvDecoder * decoder,
GstVideoCodecState * input_state, GstVideoFormat format, guint width, GstVideoDecoder * videodec, GstVideoCodecState * input_state,
guint height, GstObject * gl_display, GstObject * other_gl_context, GstVideoCodecState ** output_state)
GstObject ** gl_context, GstVideoCodecState ** output_state,
GstNvDecoderOutputType * output_type)
{ {
GstVideoCodecState *state; GstVideoCodecState *state;
GstVideoInfo *info;
g_return_val_if_fail (GST_IS_VIDEO_DECODER (decoder), FALSE); g_return_val_if_fail (GST_IS_NV_DECODER (decoder), FALSE);
g_return_val_if_fail (GST_IS_VIDEO_DECODER (videodec), FALSE);
g_return_val_if_fail (input_state != NULL, FALSE); g_return_val_if_fail (input_state != NULL, FALSE);
g_return_val_if_fail (format != GST_VIDEO_FORMAT_UNKNOWN, FALSE);
g_return_val_if_fail (width > 0, FALSE);
g_return_val_if_fail (height > 0, FALSE);
g_return_val_if_fail (output_state != NULL, FALSE); g_return_val_if_fail (output_state != NULL, FALSE);
g_return_val_if_fail (gl_context != NULL, FALSE);
g_return_val_if_fail (output_type != NULL, FALSE);
state = gst_video_decoder_set_output_state (decoder, if (!decoder->configured) {
format, width, height, input_state); GST_ERROR_OBJECT (videodec, "Should configure decoder first");
return FALSE;
}
info = &decoder->info;
state = gst_video_decoder_set_output_state (videodec,
GST_VIDEO_INFO_FORMAT (info),
GST_VIDEO_INFO_WIDTH (info), GST_VIDEO_INFO_HEIGHT (info), input_state);
state->caps = gst_video_info_to_caps (&state->info); state->caps = gst_video_info_to_caps (&state->info);
if (*output_state) if (*output_state)
gst_video_codec_state_unref (*output_state); gst_video_codec_state_unref (*output_state);
*output_state = state; *output_state = state;
*output_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM; decoder->output_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM;
{ {
GstCaps *caps; GstCaps *caps;
caps = gst_pad_get_allowed_caps (GST_VIDEO_DECODER_SRC_PAD (decoder)); caps = gst_pad_get_allowed_caps (GST_VIDEO_DECODER_SRC_PAD (videodec));
GST_DEBUG_OBJECT (decoder, "Allowed caps %" GST_PTR_FORMAT, caps); GST_DEBUG_OBJECT (videodec, "Allowed caps %" GST_PTR_FORMAT, caps);
if (!caps || gst_caps_is_any (caps)) { if (!caps || gst_caps_is_any (caps)) {
GST_DEBUG_OBJECT (decoder, GST_DEBUG_OBJECT (videodec,
"cannot determine output format, using system memory"); "cannot determine output format, using system memory");
} else { } else {
GstCapsFeatures *features; GstCapsFeatures *features;
@ -1448,47 +1502,45 @@ gst_nv_decoder_negotiate (GstVideoDecoder * decoder,
features = gst_caps_get_features (caps, i); features = gst_caps_get_features (caps, i);
if (features && gst_caps_features_contains (features, if (features && gst_caps_features_contains (features,
GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY)) { GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY)) {
GST_DEBUG_OBJECT (decoder, "found CUDA memory feature"); GST_DEBUG_OBJECT (videodec, "found CUDA memory feature");
have_cuda = TRUE; have_cuda = TRUE;
break; break;
} }
#ifdef HAVE_NVCODEC_GST_GL #ifdef HAVE_NVCODEC_GST_GL
if (gl_display && if (features && gst_caps_features_contains (features,
features && gst_caps_features_contains (features,
GST_CAPS_FEATURE_MEMORY_GL_MEMORY)) { GST_CAPS_FEATURE_MEMORY_GL_MEMORY)) {
GST_DEBUG_OBJECT (decoder, "found GL memory feature"); GST_DEBUG_OBJECT (videodec, "found GL memory feature");
have_gl = TRUE; have_gl = TRUE;
} }
#endif #endif
} }
if (have_cuda) if (have_cuda)
*output_type = GST_NV_DECODER_OUTPUT_TYPE_CUDA; decoder->output_type = GST_NV_DECODER_OUTPUT_TYPE_CUDA;
else if (have_gl) else if (have_gl)
*output_type = GST_NV_DECODER_OUTPUT_TYPE_GL; decoder->output_type = GST_NV_DECODER_OUTPUT_TYPE_GL;
} }
gst_clear_caps (&caps); gst_clear_caps (&caps);
} }
#ifdef HAVE_NVCODEC_GST_GL #ifdef HAVE_NVCODEC_GST_GL
if (*output_type == GST_NV_DECODER_OUTPUT_TYPE_GL && if (decoder->output_type == GST_NV_DECODER_OUTPUT_TYPE_GL &&
!gst_nv_decoder_ensure_gl_context (GST_ELEMENT (decoder), !gst_nv_decoder_ensure_gl_context (decoder, GST_ELEMENT (videodec))) {
gl_display, other_gl_context, gl_context)) { GST_WARNING_OBJECT (videodec,
GST_WARNING_OBJECT (decoder,
"OpenGL context is not CUDA-compatible, fallback to system memory"); "OpenGL context is not CUDA-compatible, fallback to system memory");
*output_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM; decoder->output_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM;
} }
#endif #endif
switch (*output_type) { switch (decoder->output_type) {
case GST_NV_DECODER_OUTPUT_TYPE_CUDA: case GST_NV_DECODER_OUTPUT_TYPE_CUDA:
GST_DEBUG_OBJECT (decoder, "using CUDA memory"); GST_DEBUG_OBJECT (videodec, "using CUDA memory");
gst_caps_set_features (state->caps, 0, gst_caps_set_features (state->caps, 0,
gst_caps_features_new (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY, NULL)); gst_caps_features_new (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY, NULL));
break; break;
#ifdef HAVE_NVCODEC_GST_GL #ifdef HAVE_NVCODEC_GST_GL
case GST_NV_DECODER_OUTPUT_TYPE_GL: case GST_NV_DECODER_OUTPUT_TYPE_GL:
GST_DEBUG_OBJECT (decoder, "using GL memory"); GST_DEBUG_OBJECT (videodec, "using GL memory");
gst_caps_set_features (state->caps, 0, gst_caps_set_features (state->caps, 0,
gst_caps_features_new (GST_CAPS_FEATURE_MEMORY_GL_MEMORY, NULL)); gst_caps_features_new (GST_CAPS_FEATURE_MEMORY_GL_MEMORY, NULL));
gst_caps_set_simple (state->caps, "texture-target", G_TYPE_STRING, gst_caps_set_simple (state->caps, "texture-target", G_TYPE_STRING,
@ -1496,7 +1548,7 @@ gst_nv_decoder_negotiate (GstVideoDecoder * decoder,
break; break;
#endif #endif
default: default:
GST_DEBUG_OBJECT (decoder, "using system memory"); GST_DEBUG_OBJECT (videodec, "using system memory");
break; break;
} }
@ -1547,17 +1599,24 @@ gst_nv_decoder_ensure_cuda_pool (GstNvDecoder * decoder, GstQuery * query)
#ifdef HAVE_NVCODEC_GST_GL #ifdef HAVE_NVCODEC_GST_GL
static gboolean static gboolean
gst_nv_decoder_ensure_gl_pool (GstNvDecoder * decoder, GstQuery * query, gst_nv_decoder_ensure_gl_pool (GstNvDecoder * decoder, GstQuery * query)
GstObject * gl_context)
{ {
GstCaps *outcaps; GstCaps *outcaps;
GstBufferPool *pool = NULL; GstBufferPool *pool = NULL;
guint n, size, min, max; guint n, size, min, max;
GstVideoInfo vinfo = { 0, }; GstVideoInfo vinfo = { 0, };
GstStructure *config; GstStructure *config;
GstGLContext *gl_context;
GST_DEBUG_OBJECT (decoder, "decide allocation"); GST_DEBUG_OBJECT (decoder, "decide allocation");
if (!decoder->gl_context) {
GST_ERROR_OBJECT (decoder, "GL context is not available");
return FALSE;
}
gl_context = GST_GL_CONTEXT (decoder->gl_context);
gst_query_parse_allocation (query, &outcaps, NULL); gst_query_parse_allocation (query, &outcaps, NULL);
n = gst_query_get_n_allocation_pools (query); n = gst_query_get_n_allocation_pools (query);
if (n > 0) if (n > 0)
@ -1593,25 +1652,29 @@ gst_nv_decoder_ensure_gl_pool (GstNvDecoder * decoder, GstQuery * query,
#endif #endif
gboolean gboolean
gst_nv_decoder_decide_allocation (GstNvDecoder * nvdec, gst_nv_decoder_decide_allocation (GstNvDecoder * decoder,
GstVideoDecoder * decoder, GstQuery * query, GstObject * gl_context, GstVideoDecoder * videodec, GstQuery * query)
GstNvDecoderOutputType output_type)
{ {
GST_DEBUG_OBJECT (decoder, "decide allocation"); gboolean ret = TRUE;
GST_DEBUG_OBJECT (videodec, "decide allocation");
switch (decoder->output_type) {
case GST_NV_DECODER_OUTPUT_TYPE_SYSTEM:
/* GstVideoDecoder will take care this case */ /* GstVideoDecoder will take care this case */
if (output_type == GST_NV_DECODER_OUTPUT_TYPE_SYSTEM) break;
return TRUE;
#ifdef HAVE_NVCODEC_GST_GL #ifdef HAVE_NVCODEC_GST_GL
if (output_type == GST_NV_DECODER_OUTPUT_TYPE_GL) { case GST_NV_DECODER_OUTPUT_TYPE_GL:
if (!gst_nv_decoder_ensure_gl_pool (nvdec, query, gl_context)) ret = gst_nv_decoder_ensure_gl_pool (decoder, query);
return FALSE; break;
} else
#endif #endif
if (!gst_nv_decoder_ensure_cuda_pool (nvdec, query)) { case GST_NV_DECODER_OUTPUT_TYPE_CUDA:
ret = gst_nv_decoder_ensure_cuda_pool (decoder, query);
break;
default:
g_assert_not_reached ();
return FALSE; return FALSE;
} }
return TRUE; return ret;
} }

View file

@ -46,17 +46,15 @@ typedef struct _GstNvDecoderFrame
gint ref_count; gint ref_count;
} GstNvDecoderFrame; } GstNvDecoderFrame;
typedef enum GstNvDecoder * gst_nv_decoder_new (GstCudaContext * context);
{
GST_NV_DECODER_OUTPUT_TYPE_SYSTEM = 0,
GST_NV_DECODER_OUTPUT_TYPE_GL,
GST_NV_DECODER_OUTPUT_TYPE_CUDA,
/* FIXME: add support D3D11 memory */
} GstNvDecoderOutputType;
GstNvDecoder * gst_nv_decoder_new (GstCudaContext * context, gboolean gst_nv_decoder_is_configured (GstNvDecoder * decoder);
gboolean gst_nv_decoder_configure (GstNvDecoder * decoder,
cudaVideoCodec codec, cudaVideoCodec codec,
GstVideoInfo * info, GstVideoInfo * info,
gint coded_width,
gint coded_height,
guint pool_size); guint pool_size);
GstNvDecoderFrame * gst_nv_decoder_new_frame (GstNvDecoder * decoder); GstNvDecoderFrame * gst_nv_decoder_new_frame (GstNvDecoder * decoder);
@ -69,10 +67,9 @@ gboolean gst_nv_decoder_decode_picture (GstNvDecoder * decoder,
CUVIDPICPARAMS * params); CUVIDPICPARAMS * params);
gboolean gst_nv_decoder_finish_frame (GstNvDecoder * decoder, gboolean gst_nv_decoder_finish_frame (GstNvDecoder * decoder,
GstNvDecoderOutputType output_type, GstVideoDecoder * videodec,
GstObject * graphics_context,
GstNvDecoderFrame *frame, GstNvDecoderFrame *frame,
GstBuffer *buffer); GstBuffer ** buffer);
/* utils for class registration */ /* utils for class registration */
gboolean gst_nv_decoder_check_device_caps (CUcontext cuda_ctx, gboolean gst_nv_decoder_check_device_caps (CUcontext cuda_ctx,
@ -83,43 +80,22 @@ gboolean gst_nv_decoder_check_device_caps (CUcontext cuda_ctx,
const gchar * gst_cuda_video_codec_to_string (cudaVideoCodec codec); const gchar * gst_cuda_video_codec_to_string (cudaVideoCodec codec);
/* helper methods */ /* helper methods */
gboolean gst_nv_decoder_ensure_element_data (GstElement * decoder, gboolean gst_nv_decoder_handle_set_context (GstNvDecoder * decoder,
guint cuda_device_id, GstElement * videodec,
GstCudaContext ** cuda_context, GstContext * context);
CUstream * cuda_stream,
GstObject ** gl_display,
GstObject ** other_gl_context);
void gst_nv_decoder_set_context (GstElement * decoder, gboolean gst_nv_decoder_handle_context_query (GstNvDecoder * decoder,
GstContext * context, GstVideoDecoder * videodec,
guint cuda_device_id, GstQuery * query);
GstCudaContext ** cuda_context,
GstObject ** gl_display,
GstObject ** other_gl_context);
gboolean gst_nv_decoder_handle_context_query (GstElement * decoder, gboolean gst_nv_decoder_negotiate (GstNvDecoder * decoder,
GstQuery * query, GstVideoDecoder * videodec,
GstCudaContext * cuda_context,
GstObject * gl_display,
GstObject * gl_context,
GstObject * other_gl_context);
gboolean gst_nv_decoder_negotiate (GstVideoDecoder * decoder,
GstVideoCodecState * input_state, GstVideoCodecState * input_state,
GstVideoFormat format, GstVideoCodecState ** output_state);
guint width,
guint height,
GstObject * gl_display,
GstObject * other_gl_context,
GstObject ** gl_context,
GstVideoCodecState ** output_state,
GstNvDecoderOutputType * output_type);
gboolean gst_nv_decoder_decide_allocation (GstNvDecoder * nvdec, gboolean gst_nv_decoder_decide_allocation (GstNvDecoder * decoder,
GstVideoDecoder * decoder, GstVideoDecoder * videodec,
GstQuery * query, GstQuery * query);
GstObject * gl_context,
GstNvDecoderOutputType output_type);
G_END_DECLS G_END_DECLS

View file

@ -93,7 +93,6 @@ struct _GstNvH264Dec
const GstH264PPS *last_pps; const GstH264PPS *last_pps;
GstCudaContext *context; GstCudaContext *context;
CUstream cuda_stream;
GstNvDecoder *decoder; GstNvDecoder *decoder;
CUVIDPICPARAMS params; CUVIDPICPARAMS params;
@ -113,14 +112,6 @@ struct _GstNvH264Dec
guint bitdepth; guint bitdepth;
guint chroma_format_idc; guint chroma_format_idc;
gint max_dpb_size; gint max_dpb_size;
GstVideoFormat out_format;
/* For OpenGL interop. */
GstObject *gl_display;
GstObject *gl_context;
GstObject *other_gl_context;
GstNvDecoderOutputType output_type;
}; };
struct _GstNvH264DecClass struct _GstNvH264DecClass
@ -232,9 +223,15 @@ gst_nv_h264_dec_set_context (GstElement * element, GstContext * context)
GST_DEBUG_OBJECT (self, "set context %s", GST_DEBUG_OBJECT (self, "set context %s",
gst_context_get_context_type (context)); gst_context_get_context_type (context));
gst_nv_decoder_set_context (element, context, klass->cuda_device_id, if (gst_cuda_handle_set_context (element, context, klass->cuda_device_id,
&self->context, &self->gl_display, &self->other_gl_context); &self->context)) {
goto done;
}
if (self->decoder)
gst_nv_decoder_handle_set_context (self->decoder, element, context);
done:
GST_ELEMENT_CLASS (parent_class)->set_context (element, context); GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
} }
@ -248,7 +245,6 @@ gst_d3d11_h264_dec_reset (GstNvH264Dec * self)
self->coded_height = 0; self->coded_height = 0;
self->bitdepth = 0; self->bitdepth = 0;
self->chroma_format_idc = 0; self->chroma_format_idc = 0;
self->out_format = GST_VIDEO_FORMAT_UNKNOWN;
self->max_dpb_size = 0; self->max_dpb_size = 0;
} }
@ -258,13 +254,20 @@ gst_nv_h264_dec_open (GstVideoDecoder * decoder)
GstNvH264Dec *self = GST_NV_H264_DEC (decoder); GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
GstNvH264DecClass *klass = GST_NV_H264_DEC_GET_CLASS (self); GstNvH264DecClass *klass = GST_NV_H264_DEC_GET_CLASS (self);
if (!gst_nv_decoder_ensure_element_data (GST_ELEMENT (self), if (!gst_cuda_ensure_element_context (GST_ELEMENT (self),
klass->cuda_device_id, &self->context, &self->cuda_stream, klass->cuda_device_id, &self->context)) {
&self->gl_display, &self->other_gl_context)) {
GST_ERROR_OBJECT (self, "Required element data is unavailable"); GST_ERROR_OBJECT (self, "Required element data is unavailable");
return FALSE; return FALSE;
} }
self->decoder = gst_nv_decoder_new (self->context);
if (!self->decoder) {
GST_ERROR_OBJECT (self, "Failed to create decoder object");
gst_clear_object (&self->context);
return FALSE;
}
gst_d3d11_h264_dec_reset (self); gst_d3d11_h264_dec_reset (self);
return TRUE; return TRUE;
@ -277,19 +280,7 @@ gst_nv_h264_dec_close (GstVideoDecoder * decoder)
g_clear_pointer (&self->output_state, gst_video_codec_state_unref); g_clear_pointer (&self->output_state, gst_video_codec_state_unref);
gst_clear_object (&self->decoder); gst_clear_object (&self->decoder);
if (self->context && self->cuda_stream) {
if (gst_cuda_context_push (self->context)) {
gst_cuda_result (CuStreamDestroy (self->cuda_stream));
gst_cuda_context_pop (NULL);
}
}
gst_clear_object (&self->gl_context);
gst_clear_object (&self->other_gl_context);
gst_clear_object (&self->gl_display);
gst_clear_object (&self->context); gst_clear_object (&self->context);
self->cuda_stream = NULL;
return TRUE; return TRUE;
} }
@ -302,9 +293,8 @@ gst_nv_h264_dec_negotiate (GstVideoDecoder * decoder)
GST_DEBUG_OBJECT (self, "negotiate"); GST_DEBUG_OBJECT (self, "negotiate");
gst_nv_decoder_negotiate (decoder, h264dec->input_state, self->out_format, gst_nv_decoder_negotiate (self->decoder, decoder, h264dec->input_state,
self->width, self->height, self->gl_display, self->other_gl_context, &self->output_state);
&self->gl_context, &self->output_state, &self->output_type);
/* TODO: add support D3D11 memory */ /* TODO: add support D3D11 memory */
@ -316,8 +306,10 @@ gst_nv_h264_dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
{ {
GstNvH264Dec *self = GST_NV_H264_DEC (decoder); GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
gst_nv_decoder_decide_allocation (self->decoder, decoder, query, if (!gst_nv_decoder_decide_allocation (self->decoder, decoder, query)) {
self->gl_context, self->output_type); GST_WARNING_OBJECT (self, "Failed to handle decide allocation");
return FALSE;
}
return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
(decoder, query); (decoder, query);
@ -330,9 +322,11 @@ gst_nv_h264_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
switch (GST_QUERY_TYPE (query)) { switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CONTEXT: case GST_QUERY_CONTEXT:
if (gst_nv_decoder_handle_context_query (GST_ELEMENT (self), query, if (gst_cuda_handle_context_query (GST_ELEMENT (decoder), query,
self->context, self->gl_display, self->gl_context, self->context)) {
self->other_gl_context)) { return TRUE;
} else if (self->decoder &&
gst_nv_decoder_handle_context_query (self->decoder, decoder, query)) {
return TRUE; return TRUE;
} }
break; break;
@ -390,44 +384,38 @@ gst_nv_h264_dec_new_sequence (GstH264Decoder * decoder, const GstH264SPS * sps,
modified = TRUE; modified = TRUE;
} }
if (modified || !self->decoder) { if (modified || !gst_nv_decoder_is_configured (self->decoder)) {
GstVideoInfo info; GstVideoInfo info;
GstVideoFormat out_format = GST_VIDEO_FORMAT_UNKNOWN;
self->out_format = GST_VIDEO_FORMAT_UNKNOWN;
if (self->bitdepth == 8) { if (self->bitdepth == 8) {
if (self->chroma_format_idc == 1) if (self->chroma_format_idc == 1)
self->out_format = GST_VIDEO_FORMAT_NV12; out_format = GST_VIDEO_FORMAT_NV12;
else { else {
GST_FIXME_OBJECT (self, "Could not support 8bits non-4:2:0 format"); GST_FIXME_OBJECT (self, "Could not support 8bits non-4:2:0 format");
} }
} else if (self->bitdepth == 10) { } else if (self->bitdepth == 10) {
if (self->chroma_format_idc == 1) if (self->chroma_format_idc == 1)
self->out_format = GST_VIDEO_FORMAT_P010_10LE; out_format = GST_VIDEO_FORMAT_P010_10LE;
else { else {
GST_FIXME_OBJECT (self, "Could not support 10bits non-4:2:0 format"); GST_FIXME_OBJECT (self, "Could not support 10bits non-4:2:0 format");
} }
} }
if (self->out_format == GST_VIDEO_FORMAT_UNKNOWN) { if (out_format == GST_VIDEO_FORMAT_UNKNOWN) {
GST_ERROR_OBJECT (self, "Could not support bitdepth/chroma format"); GST_ERROR_OBJECT (self, "Could not support bitdepth/chroma format");
return FALSE; return FALSE;
} }
gst_clear_object (&self->decoder); gst_video_info_set_format (&info, out_format, self->width, self->height);
gst_video_info_set_format (&info,
self->out_format, self->width, self->height);
self->max_dpb_size = max_dpb_size; self->max_dpb_size = max_dpb_size;
/* FIXME: add support cudaVideoCodec_H264_SVC and cudaVideoCodec_H264_MVC */ /* FIXME: add support cudaVideoCodec_H264_SVC and cudaVideoCodec_H264_MVC */
self->decoder = gst_nv_decoder_new (self->context, cudaVideoCodec_H264, if (!gst_nv_decoder_configure (self->decoder,
&info, cudaVideoCodec_H264, &info, self->coded_width, self->coded_height,
/* Additional 2 buffers for margin */ /* Additional 2 buffers for margin */
max_dpb_size + 2); max_dpb_size + 2)) {
GST_ERROR_OBJECT (self, "Failed to configure decoder");
if (!self->decoder) {
GST_ERROR_OBJECT (self, "Failed to create decoder");
return FALSE; return FALSE;
} }
@ -473,7 +461,6 @@ gst_nv_h264_dec_output_picture (GstH264Decoder * decoder,
GstNvH264Dec *self = GST_NV_H264_DEC (decoder); GstNvH264Dec *self = GST_NV_H264_DEC (decoder);
GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder); GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
GstNvDecoderFrame *decoder_frame; GstNvDecoderFrame *decoder_frame;
gboolean ret G_GNUC_UNUSED = FALSE;
GST_LOG_OBJECT (self, GST_LOG_OBJECT (self,
"Outputting picture %p (poc %d)", picture, picture->pic_order_cnt); "Outputting picture %p (poc %d)", picture, picture->pic_order_cnt);
@ -485,36 +472,12 @@ gst_nv_h264_dec_output_picture (GstH264Decoder * decoder,
goto error; goto error;
} }
frame->output_buffer = gst_video_decoder_allocate_output_buffer (vdec); if (!gst_nv_decoder_finish_frame (self->decoder, vdec, decoder_frame,
if (!frame->output_buffer) { &frame->output_buffer)) {
GST_ERROR_OBJECT (self, "Couldn't allocate output buffer"); GST_ERROR_OBJECT (self, "Failed to handle output picture");
goto error; goto error;
} }
if (self->output_type == GST_NV_DECODER_OUTPUT_TYPE_GL) {
ret = gst_nv_decoder_finish_frame (self->decoder,
GST_NV_DECODER_OUTPUT_TYPE_GL, self->gl_context,
decoder_frame, frame->output_buffer);
/* FIXME: This is the case where OpenGL context of downstream glbufferpool
* belongs to non-nvidia (or different device).
* There should be enhancement to ensure nvdec has compatible OpenGL context
*/
if (!ret) {
GST_WARNING_OBJECT (self,
"Couldn't copy frame to GL memory, fallback to system memory");
self->output_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM;
}
}
if (!ret) {
if (!gst_nv_decoder_finish_frame (self->decoder,
self->output_type, NULL, decoder_frame, frame->output_buffer)) {
GST_ERROR_OBJECT (self, "Failed to finish frame");
goto error;
}
}
gst_h264_picture_unref (picture); gst_h264_picture_unref (picture);
return gst_video_decoder_finish_frame (vdec, frame); return gst_video_decoder_finish_frame (vdec, frame);

View file

@ -93,7 +93,6 @@ struct _GstNvH265Dec
const GstH265PPS *last_pps; const GstH265PPS *last_pps;
GstCudaContext *context; GstCudaContext *context;
CUstream cuda_stream;
GstNvDecoder *decoder; GstNvDecoder *decoder;
CUVIDPICPARAMS params; CUVIDPICPARAMS params;
@ -112,14 +111,6 @@ struct _GstNvH265Dec
guint coded_width, coded_height; guint coded_width, coded_height;
guint bitdepth; guint bitdepth;
guint chroma_format_idc; guint chroma_format_idc;
GstVideoFormat out_format;
/* For OpenGL interop. */
GstObject *gl_display;
GstObject *gl_context;
GstObject *other_gl_context;
GstNvDecoderOutputType output_type;
}; };
struct _GstNvH265DecClass struct _GstNvH265DecClass
@ -226,9 +217,15 @@ gst_nv_h265_dec_set_context (GstElement * element, GstContext * context)
GST_DEBUG_OBJECT (self, "set context %s", GST_DEBUG_OBJECT (self, "set context %s",
gst_context_get_context_type (context)); gst_context_get_context_type (context));
gst_nv_decoder_set_context (element, context, klass->cuda_device_id, if (gst_cuda_handle_set_context (element, context, klass->cuda_device_id,
&self->context, &self->gl_display, &self->other_gl_context); &self->context)) {
goto done;
}
if (self->decoder)
gst_nv_decoder_handle_set_context (self->decoder, element, context);
done:
GST_ELEMENT_CLASS (parent_class)->set_context (element, context); GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
} }
@ -238,13 +235,20 @@ gst_nv_h265_dec_open (GstVideoDecoder * decoder)
GstNvH265Dec *self = GST_NV_H265_DEC (decoder); GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
GstNvH265DecClass *klass = GST_NV_H265_DEC_GET_CLASS (self); GstNvH265DecClass *klass = GST_NV_H265_DEC_GET_CLASS (self);
if (!gst_nv_decoder_ensure_element_data (GST_ELEMENT (self), if (!gst_cuda_ensure_element_context (GST_ELEMENT (self),
klass->cuda_device_id, &self->context, &self->cuda_stream, klass->cuda_device_id, &self->context)) {
&self->gl_display, &self->other_gl_context)) {
GST_ERROR_OBJECT (self, "Required element data is unavailable"); GST_ERROR_OBJECT (self, "Required element data is unavailable");
return FALSE; return FALSE;
} }
self->decoder = gst_nv_decoder_new (self->context);
if (!self->decoder) {
GST_ERROR_OBJECT (self, "Failed to create decoder object");
gst_clear_object (&self->context);
return FALSE;
}
return TRUE; return TRUE;
} }
@ -255,19 +259,7 @@ gst_nv_h265_dec_close (GstVideoDecoder * decoder)
g_clear_pointer (&self->output_state, gst_video_codec_state_unref); g_clear_pointer (&self->output_state, gst_video_codec_state_unref);
gst_clear_object (&self->decoder); gst_clear_object (&self->decoder);
if (self->context && self->cuda_stream) {
if (gst_cuda_context_push (self->context)) {
gst_cuda_result (CuStreamDestroy (self->cuda_stream));
gst_cuda_context_pop (NULL);
}
}
gst_clear_object (&self->gl_context);
gst_clear_object (&self->other_gl_context);
gst_clear_object (&self->gl_display);
gst_clear_object (&self->context); gst_clear_object (&self->context);
self->cuda_stream = NULL;
return TRUE; return TRUE;
} }
@ -280,9 +272,8 @@ gst_nv_h265_dec_negotiate (GstVideoDecoder * decoder)
GST_DEBUG_OBJECT (self, "negotiate"); GST_DEBUG_OBJECT (self, "negotiate");
gst_nv_decoder_negotiate (decoder, h265dec->input_state, self->out_format, gst_nv_decoder_negotiate (self->decoder, decoder, h265dec->input_state,
self->width, self->height, self->gl_display, self->other_gl_context, &self->output_state);
&self->gl_context, &self->output_state, &self->output_type);
/* TODO: add support D3D11 memory */ /* TODO: add support D3D11 memory */
@ -294,8 +285,10 @@ gst_nv_h265_dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
{ {
GstNvH265Dec *self = GST_NV_H265_DEC (decoder); GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
gst_nv_decoder_decide_allocation (self->decoder, decoder, query, if (!gst_nv_decoder_decide_allocation (self->decoder, decoder, query)) {
self->gl_context, self->output_type); GST_WARNING_OBJECT (self, "Failed to handle decide allocation");
return FALSE;
}
return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
(decoder, query); (decoder, query);
@ -308,9 +301,11 @@ gst_nv_h265_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
switch (GST_QUERY_TYPE (query)) { switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CONTEXT: case GST_QUERY_CONTEXT:
if (gst_nv_decoder_handle_context_query (GST_ELEMENT (self), query, if (gst_cuda_handle_context_query (GST_ELEMENT (decoder), query,
self->context, self->gl_display, self->gl_context, self->context)) {
self->other_gl_context)) { return TRUE;
} else if (self->decoder &&
gst_nv_decoder_handle_context_query (self->decoder, decoder, query)) {
return TRUE; return TRUE;
} }
break; break;
@ -362,42 +357,36 @@ gst_nv_h265_dec_new_sequence (GstH265Decoder * decoder, const GstH265SPS * sps,
modified = TRUE; modified = TRUE;
} }
if (modified || !self->decoder) { if (modified || !gst_nv_decoder_is_configured (self->decoder)) {
GstVideoInfo info; GstVideoInfo info;
GstVideoFormat out_format = GST_VIDEO_FORMAT_UNKNOWN;
self->out_format = GST_VIDEO_FORMAT_UNKNOWN;
if (self->bitdepth == 8) { if (self->bitdepth == 8) {
if (self->chroma_format_idc == 1) if (self->chroma_format_idc == 1)
self->out_format = GST_VIDEO_FORMAT_NV12; out_format = GST_VIDEO_FORMAT_NV12;
else { else {
GST_FIXME_OBJECT (self, "Could not support 8bits non-4:2:0 format"); GST_FIXME_OBJECT (self, "Could not support 8bits non-4:2:0 format");
} }
} else if (self->bitdepth == 10) { } else if (self->bitdepth == 10) {
if (self->chroma_format_idc == 1) if (self->chroma_format_idc == 1)
self->out_format = GST_VIDEO_FORMAT_P010_10LE; out_format = GST_VIDEO_FORMAT_P010_10LE;
else { else {
GST_FIXME_OBJECT (self, "Could not support 10bits non-4:2:0 format"); GST_FIXME_OBJECT (self, "Could not support 10bits non-4:2:0 format");
} }
} }
if (self->out_format == GST_VIDEO_FORMAT_UNKNOWN) { if (out_format == GST_VIDEO_FORMAT_UNKNOWN) {
GST_ERROR_OBJECT (self, "Could not support bitdepth/chroma format"); GST_ERROR_OBJECT (self, "Could not support bitdepth/chroma format");
return FALSE; return FALSE;
} }
gst_clear_object (&self->decoder); gst_video_info_set_format (&info, out_format, self->width, self->height);
gst_video_info_set_format (&info, if (!gst_nv_decoder_configure (self->decoder,
self->out_format, self->width, self->height); cudaVideoCodec_HEVC, &info, self->coded_width, self->coded_height,
self->decoder = gst_nv_decoder_new (self->context, cudaVideoCodec_HEVC,
&info,
/* Additional 2 buffers for margin */ /* Additional 2 buffers for margin */
max_dpb_size + 2); max_dpb_size + 2)) {
GST_ERROR_OBJECT (self, "Failed to configure decoder");
if (!self->decoder) {
GST_ERROR_OBJECT (self, "Failed to create decoder");
return FALSE; return FALSE;
} }
@ -442,7 +431,6 @@ gst_nv_h265_dec_output_picture (GstH265Decoder * decoder,
GstNvH265Dec *self = GST_NV_H265_DEC (decoder); GstNvH265Dec *self = GST_NV_H265_DEC (decoder);
GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder); GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
GstNvDecoderFrame *decoder_frame; GstNvDecoderFrame *decoder_frame;
gboolean ret G_GNUC_UNUSED = FALSE;
GST_LOG_OBJECT (self, GST_LOG_OBJECT (self,
"Outputting picture %p (poc %d)", picture, picture->pic_order_cnt); "Outputting picture %p (poc %d)", picture, picture->pic_order_cnt);
@ -454,32 +442,11 @@ gst_nv_h265_dec_output_picture (GstH265Decoder * decoder,
goto error; goto error;
} }
frame->output_buffer = if (!gst_nv_decoder_finish_frame (self->decoder, vdec, decoder_frame,
gst_video_decoder_allocate_output_buffer (GST_VIDEO_DECODER (self));; &frame->output_buffer)) {
GST_ERROR_OBJECT (self, "Failed to handle output picture");
if (self->output_type == GST_NV_DECODER_OUTPUT_TYPE_GL) {
ret = gst_nv_decoder_finish_frame (self->decoder,
GST_NV_DECODER_OUTPUT_TYPE_GL, self->gl_context,
decoder_frame, frame->output_buffer);
/* FIXME: This is the case where OpenGL context of downstream glbufferpool
* belongs to non-nvidia (or different device).
* There should be enhancement to ensure nvdec has compatible OpenGL context
*/
if (!ret) {
GST_WARNING_OBJECT (self,
"Couldn't copy frame to GL memory, fallback to system memory");
self->output_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM;
}
}
if (!ret) {
if (!gst_nv_decoder_finish_frame (self->decoder,
self->output_type, NULL, decoder_frame, frame->output_buffer)) {
GST_ERROR_OBJECT (self, "Failed to finish frame");
goto error; goto error;
} }
}
gst_h265_picture_unref (picture); gst_h265_picture_unref (picture);

View file

@ -40,18 +40,10 @@ struct _GstNvVp8Dec
GstVideoCodecState *output_state; GstVideoCodecState *output_state;
GstCudaContext *context; GstCudaContext *context;
CUstream cuda_stream;
GstNvDecoder *decoder; GstNvDecoder *decoder;
CUVIDPICPARAMS params; CUVIDPICPARAMS params;
guint width, height; guint width, height;
/* For OpenGL interop. */
GstObject *gl_display;
GstObject *gl_context;
GstObject *other_gl_context;
GstNvDecoderOutputType output_type;
}; };
struct _GstNvVp8DecClass struct _GstNvVp8DecClass
@ -134,9 +126,15 @@ gst_nv_vp8_dec_set_context (GstElement * element, GstContext * context)
GST_DEBUG_OBJECT (self, "set context %s", GST_DEBUG_OBJECT (self, "set context %s",
gst_context_get_context_type (context)); gst_context_get_context_type (context));
gst_nv_decoder_set_context (element, context, klass->cuda_device_id, if (gst_cuda_handle_set_context (element, context, klass->cuda_device_id,
&self->context, &self->gl_display, &self->other_gl_context); &self->context)) {
goto done;
}
if (self->decoder)
gst_nv_decoder_handle_set_context (self->decoder, element, context);
done:
GST_ELEMENT_CLASS (parent_class)->set_context (element, context); GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
} }
@ -146,13 +144,20 @@ gst_nv_vp8_dec_open (GstVideoDecoder * decoder)
GstNvVp8Dec *self = GST_NV_VP8_DEC (decoder); GstNvVp8Dec *self = GST_NV_VP8_DEC (decoder);
GstNvVp8DecClass *klass = GST_NV_VP8_DEC_GET_CLASS (self); GstNvVp8DecClass *klass = GST_NV_VP8_DEC_GET_CLASS (self);
if (!gst_nv_decoder_ensure_element_data (GST_ELEMENT (self), if (!gst_cuda_ensure_element_context (GST_ELEMENT (self),
klass->cuda_device_id, &self->context, &self->cuda_stream, klass->cuda_device_id, &self->context)) {
&self->gl_display, &self->other_gl_context)) {
GST_ERROR_OBJECT (self, "Required element data is unavailable"); GST_ERROR_OBJECT (self, "Required element data is unavailable");
return FALSE; return FALSE;
} }
self->decoder = gst_nv_decoder_new (self->context);
if (!self->decoder) {
GST_ERROR_OBJECT (self, "Failed to create decoder object");
gst_clear_object (&self->context);
return FALSE;
}
return TRUE; return TRUE;
} }
@ -163,19 +168,7 @@ gst_nv_vp8_dec_close (GstVideoDecoder * decoder)
g_clear_pointer (&self->output_state, gst_video_codec_state_unref); g_clear_pointer (&self->output_state, gst_video_codec_state_unref);
gst_clear_object (&self->decoder); gst_clear_object (&self->decoder);
if (self->context && self->cuda_stream) {
if (gst_cuda_context_push (self->context)) {
gst_cuda_result (CuStreamDestroy (self->cuda_stream));
gst_cuda_context_pop (NULL);
}
}
gst_clear_object (&self->gl_context);
gst_clear_object (&self->other_gl_context);
gst_clear_object (&self->gl_display);
gst_clear_object (&self->context); gst_clear_object (&self->context);
self->cuda_stream = NULL;
return TRUE; return TRUE;
} }
@ -188,9 +181,8 @@ gst_nv_vp8_dec_negotiate (GstVideoDecoder * decoder)
GST_DEBUG_OBJECT (self, "negotiate"); GST_DEBUG_OBJECT (self, "negotiate");
gst_nv_decoder_negotiate (decoder, vp8dec->input_state, GST_VIDEO_FORMAT_NV12, gst_nv_decoder_negotiate (self->decoder, decoder, vp8dec->input_state,
self->width, self->height, self->gl_display, self->other_gl_context, &self->output_state);
&self->gl_context, &self->output_state, &self->output_type);
/* TODO: add support D3D11 memory */ /* TODO: add support D3D11 memory */
@ -202,8 +194,10 @@ gst_nv_vp8_dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
{ {
GstNvVp8Dec *self = GST_NV_VP8_DEC (decoder); GstNvVp8Dec *self = GST_NV_VP8_DEC (decoder);
gst_nv_decoder_decide_allocation (self->decoder, decoder, query, if (!gst_nv_decoder_decide_allocation (self->decoder, decoder, query)) {
self->gl_context, self->output_type); GST_WARNING_OBJECT (self, "Failed to handle decide allocation");
return FALSE;
}
return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
(decoder, query); (decoder, query);
@ -216,9 +210,11 @@ gst_nv_vp8_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
switch (GST_QUERY_TYPE (query)) { switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CONTEXT: case GST_QUERY_CONTEXT:
if (gst_nv_decoder_handle_context_query (GST_ELEMENT (self), query, if (gst_cuda_handle_context_query (GST_ELEMENT (decoder), query,
self->context, self->gl_display, self->gl_context, self->context)) {
self->other_gl_context)) { return TRUE;
} else if (self->decoder &&
gst_nv_decoder_handle_context_query (self->decoder, decoder, query)) {
return TRUE; return TRUE;
} }
break; break;
@ -250,19 +246,16 @@ gst_nv_vp8_dec_new_sequence (GstVp8Decoder * decoder,
modified = TRUE; modified = TRUE;
} }
if (modified || !self->decoder) { if (modified || !gst_nv_decoder_is_configured (self->decoder)) {
GstVideoInfo info; GstVideoInfo info;
gst_clear_object (&self->decoder);
gst_video_info_set_format (&info, gst_video_info_set_format (&info,
GST_VIDEO_FORMAT_NV12, self->width, self->height); GST_VIDEO_FORMAT_NV12, self->width, self->height);
self->decoder = gst_nv_decoder_new (self->context, cudaVideoCodec_VP8, if (!gst_nv_decoder_configure (self->decoder,
&info, NUM_OUTPUT_VIEW); cudaVideoCodec_VP8, &info, self->width, self->height,
NUM_OUTPUT_VIEW)) {
if (!self->decoder) { GST_ERROR_OBJECT (self, "Failed to configure decoder");
GST_ERROR_OBJECT (self, "Failed to create decoder");
return FALSE; return FALSE;
} }
@ -408,7 +401,6 @@ gst_nv_vp8_dec_output_picture (GstVp8Decoder * decoder,
GstNvVp8Dec *self = GST_NV_VP8_DEC (decoder); GstNvVp8Dec *self = GST_NV_VP8_DEC (decoder);
GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder); GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
GstNvDecoderFrame *decoder_frame; GstNvDecoderFrame *decoder_frame;
gboolean ret G_GNUC_UNUSED = FALSE;
GST_LOG_OBJECT (self, "Outputting picture %p", picture); GST_LOG_OBJECT (self, "Outputting picture %p", picture);
@ -418,36 +410,12 @@ gst_nv_vp8_dec_output_picture (GstVp8Decoder * decoder,
goto error; goto error;
} }
frame->output_buffer = gst_video_decoder_allocate_output_buffer (vdec); if (!gst_nv_decoder_finish_frame (self->decoder, vdec, decoder_frame,
if (!frame->output_buffer) { &frame->output_buffer)) {
GST_ERROR_OBJECT (self, "Couldn't allocate output buffer"); GST_ERROR_OBJECT (self, "Failed to handle output picture");
goto error; goto error;
} }
if (self->output_type == GST_NV_DECODER_OUTPUT_TYPE_GL) {
ret = gst_nv_decoder_finish_frame (self->decoder,
GST_NV_DECODER_OUTPUT_TYPE_GL, self->gl_context,
decoder_frame, frame->output_buffer);
/* FIXME: This is the case where OpenGL context of downstream glbufferpool
* belongs to non-nvidia (or different device).
* There should be enhancement to ensure nvdec has compatible OpenGL context
*/
if (!ret) {
GST_WARNING_OBJECT (self,
"Couldn't copy frame to GL memory, fallback to system memory");
self->output_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM;
}
}
if (!ret) {
if (!gst_nv_decoder_finish_frame (self->decoder,
self->output_type, NULL, decoder_frame, frame->output_buffer)) {
GST_ERROR_OBJECT (self, "Failed to finish frame");
goto error;
}
}
gst_vp8_picture_unref (picture); gst_vp8_picture_unref (picture);
return gst_video_decoder_finish_frame (vdec, frame); return gst_video_decoder_finish_frame (vdec, frame);

View file

@ -40,21 +40,11 @@ struct _GstNvVp9Dec
GstVideoCodecState *output_state; GstVideoCodecState *output_state;
GstCudaContext *context; GstCudaContext *context;
CUstream cuda_stream;
GstNvDecoder *decoder; GstNvDecoder *decoder;
CUVIDPICPARAMS params; CUVIDPICPARAMS params;
guint width, height; guint width, height;
GstVP9Profile profile; GstVP9Profile profile;
GstVideoFormat out_format;
/* For OpenGL interop. */
GstObject *gl_display;
GstObject *gl_context;
GstObject *other_gl_context;
GstNvDecoderOutputType output_type;
}; };
struct _GstNvVp9DecClass struct _GstNvVp9DecClass
@ -141,9 +131,15 @@ gst_nv_vp9_dec_set_context (GstElement * element, GstContext * context)
GST_DEBUG_OBJECT (self, "set context %s", GST_DEBUG_OBJECT (self, "set context %s",
gst_context_get_context_type (context)); gst_context_get_context_type (context));
gst_nv_decoder_set_context (element, context, klass->cuda_device_id, if (gst_cuda_handle_set_context (element, context, klass->cuda_device_id,
&self->context, &self->gl_display, &self->other_gl_context); &self->context)) {
goto done;
}
if (self->decoder)
gst_nv_decoder_handle_set_context (self->decoder, element, context);
done:
GST_ELEMENT_CLASS (parent_class)->set_context (element, context); GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
} }
@ -153,13 +149,20 @@ gst_nv_vp9_dec_open (GstVideoDecoder * decoder)
GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder); GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
GstNvVp9DecClass *klass = GST_NV_VP9_DEC_GET_CLASS (self); GstNvVp9DecClass *klass = GST_NV_VP9_DEC_GET_CLASS (self);
if (!gst_nv_decoder_ensure_element_data (GST_ELEMENT (self), if (!gst_cuda_ensure_element_context (GST_ELEMENT (self),
klass->cuda_device_id, &self->context, &self->cuda_stream, klass->cuda_device_id, &self->context)) {
&self->gl_display, &self->other_gl_context)) {
GST_ERROR_OBJECT (self, "Required element data is unavailable"); GST_ERROR_OBJECT (self, "Required element data is unavailable");
return FALSE; return FALSE;
} }
self->decoder = gst_nv_decoder_new (self->context);
if (!self->decoder) {
GST_ERROR_OBJECT (self, "Failed to create decoder object");
gst_clear_object (&self->context);
return FALSE;
}
return TRUE; return TRUE;
} }
@ -170,19 +173,7 @@ gst_nv_vp9_dec_close (GstVideoDecoder * decoder)
g_clear_pointer (&self->output_state, gst_video_codec_state_unref); g_clear_pointer (&self->output_state, gst_video_codec_state_unref);
gst_clear_object (&self->decoder); gst_clear_object (&self->decoder);
if (self->context && self->cuda_stream) {
if (gst_cuda_context_push (self->context)) {
gst_cuda_result (CuStreamDestroy (self->cuda_stream));
gst_cuda_context_pop (NULL);
}
}
gst_clear_object (&self->gl_context);
gst_clear_object (&self->other_gl_context);
gst_clear_object (&self->gl_display);
gst_clear_object (&self->context); gst_clear_object (&self->context);
self->cuda_stream = NULL;
return TRUE; return TRUE;
} }
@ -195,9 +186,8 @@ gst_nv_vp9_dec_negotiate (GstVideoDecoder * decoder)
GST_DEBUG_OBJECT (self, "negotiate"); GST_DEBUG_OBJECT (self, "negotiate");
gst_nv_decoder_negotiate (decoder, vp9dec->input_state, self->out_format, gst_nv_decoder_negotiate (self->decoder, decoder, vp9dec->input_state,
self->width, self->height, self->gl_display, self->other_gl_context, &self->output_state);
&self->gl_context, &self->output_state, &self->output_type);
/* TODO: add support D3D11 memory */ /* TODO: add support D3D11 memory */
@ -209,8 +199,10 @@ gst_nv_vp9_dec_decide_allocation (GstVideoDecoder * decoder, GstQuery * query)
{ {
GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder); GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
gst_nv_decoder_decide_allocation (self->decoder, decoder, query, if (!gst_nv_decoder_decide_allocation (self->decoder, decoder, query)) {
self->gl_context, self->output_type); GST_WARNING_OBJECT (self, "Failed to handle decide allocation");
return FALSE;
}
return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation return GST_VIDEO_DECODER_CLASS (parent_class)->decide_allocation
(decoder, query); (decoder, query);
@ -223,9 +215,11 @@ gst_nv_vp9_dec_src_query (GstVideoDecoder * decoder, GstQuery * query)
switch (GST_QUERY_TYPE (query)) { switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CONTEXT: case GST_QUERY_CONTEXT:
if (gst_nv_decoder_handle_context_query (GST_ELEMENT (self), query, if (gst_cuda_handle_context_query (GST_ELEMENT (decoder), query,
self->context, self->gl_display, self->gl_context, self->context)) {
self->other_gl_context)) { return TRUE;
} else if (self->decoder &&
gst_nv_decoder_handle_context_query (self->decoder, decoder, query)) {
return TRUE; return TRUE;
} }
break; break;
@ -266,35 +260,30 @@ gst_nv_vp9_dec_new_sequence (GstVp9Decoder * decoder,
modified = TRUE; modified = TRUE;
} }
if (modified || !self->decoder) { if (modified || !gst_nv_decoder_is_configured (self->decoder)) {
GstVideoInfo info; GstVideoInfo info;
GstVideoFormat out_format = GST_VIDEO_FORMAT_UNKNOWN;
gst_clear_object (&self->decoder);
self->out_format = GST_VIDEO_FORMAT_UNKNOWN;
if (self->profile == GST_VP9_PROFILE_0) { if (self->profile == GST_VP9_PROFILE_0) {
self->out_format = GST_VIDEO_FORMAT_NV12; out_format = GST_VIDEO_FORMAT_NV12;
} else if (self->profile == GST_VP9_PROFILE_2) { } else if (self->profile == GST_VP9_PROFILE_2) {
if (parser->bit_depth == 10) if (parser->bit_depth == 10)
self->out_format = GST_VIDEO_FORMAT_P010_10LE; out_format = GST_VIDEO_FORMAT_P010_10LE;
else else
self->out_format = GST_VIDEO_FORMAT_P016_LE; out_format = GST_VIDEO_FORMAT_P016_LE;
} }
if (self->out_format == GST_VIDEO_FORMAT_UNKNOWN) { if (out_format == GST_VIDEO_FORMAT_UNKNOWN) {
GST_ERROR_OBJECT (self, "Could not support profile %d", self->profile); GST_ERROR_OBJECT (self, "Could not support profile %d", self->profile);
return FALSE; return FALSE;
} }
gst_video_info_set_format (&info, gst_video_info_set_format (&info, out_format, self->width, self->height);
self->out_format, self->width, self->height);
self->decoder = gst_nv_decoder_new (self->context, cudaVideoCodec_VP9, if (!gst_nv_decoder_configure (self->decoder,
&info, NUM_OUTPUT_VIEW); cudaVideoCodec_VP9, &info, self->width, self->height,
NUM_OUTPUT_VIEW)) {
if (!self->decoder) { GST_ERROR_OBJECT (self, "Failed to configure decoder");
GST_ERROR_OBJECT (self, "Failed to create decoder");
return FALSE; return FALSE;
} }
@ -519,7 +508,6 @@ gst_nv_vp9_dec_output_picture (GstVp9Decoder * decoder,
GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder); GstNvVp9Dec *self = GST_NV_VP9_DEC (decoder);
GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder); GstVideoDecoder *vdec = GST_VIDEO_DECODER (decoder);
GstNvDecoderFrame *decoder_frame; GstNvDecoderFrame *decoder_frame;
gboolean ret G_GNUC_UNUSED = FALSE;
GST_LOG_OBJECT (self, "Outputting picture %p", picture); GST_LOG_OBJECT (self, "Outputting picture %p", picture);
@ -529,36 +517,12 @@ gst_nv_vp9_dec_output_picture (GstVp9Decoder * decoder,
goto error; goto error;
} }
frame->output_buffer = gst_video_decoder_allocate_output_buffer (vdec); if (!gst_nv_decoder_finish_frame (self->decoder, vdec, decoder_frame,
if (!frame->output_buffer) { &frame->output_buffer)) {
GST_ERROR_OBJECT (self, "Couldn't allocate output buffer"); GST_ERROR_OBJECT (self, "Failed to handle output picture");
goto error; goto error;
} }
if (self->output_type == GST_NV_DECODER_OUTPUT_TYPE_GL) {
ret = gst_nv_decoder_finish_frame (self->decoder,
GST_NV_DECODER_OUTPUT_TYPE_GL, self->gl_context,
decoder_frame, frame->output_buffer);
/* FIXME: This is the case where OpenGL context of downstream glbufferpool
* belongs to non-nvidia (or different device).
* There should be enhancement to ensure nvdec has compatible OpenGL context
*/
if (!ret) {
GST_WARNING_OBJECT (self,
"Couldn't copy frame to GL memory, fallback to system memory");
self->output_type = GST_NV_DECODER_OUTPUT_TYPE_SYSTEM;
}
}
if (!ret) {
if (!gst_nv_decoder_finish_frame (self->decoder,
self->output_type, NULL, decoder_frame, frame->output_buffer)) {
GST_ERROR_OBJECT (self, "Failed to finish frame");
goto error;
}
}
gst_vp9_picture_unref (picture); gst_vp9_picture_unref (picture);
return gst_video_decoder_finish_frame (vdec, frame); return gst_video_decoder_finish_frame (vdec, frame);