mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-11-26 19:51:11 +00:00
nvcodec: Add new Direct3D11/CUDA mode encoder implementation
Adding new encoder elements nvd3d11{h264,h265}enc for Direct3D11 input support and re-written nvcuda{h264,h265}enc elements. Newly writeen elements have some differences compared with old nv{h264,h265}enc including non-backward compatible changes. * RGBA is not a supported input format any more: New elements will support only YUV formats to avoid implicit conversion done by hardware. Ideally it should be done by upstream element in order to have more control on it. Moreover, RGBA support can cause redundant RGBA -> YUV conversion if multiple encoders are used for the same RGBA input * Subsampled planar format support is dropped: I420 and YV12 format are not supported formats for Direct3D11. Although it's supported in CUDA mode, it's not a hardware friendly memory layout and it will waste GPU memory since UV planes will have large padding due to the memory layout requirement of NVENC. * GL support is dropped: Similar to the RGBA case, GL support in encoder would be suboptimal if GL input is used by multiple encoders, because each encoder will copy GL memory into CUDA memory. Upstream cudaupload element can be used for GL <-> CUDA interop instead. * No more pre-allocation of encoder input surfaces. New implementation will use input CUDA memory without copy (zero-copy) or will copy into a NVENC's input buffer struct in case of system memory input. Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1997>
This commit is contained in:
parent
ea2f686487
commit
60735deded
12 changed files with 6367 additions and 36 deletions
|
@ -261,6 +261,20 @@ NvEncEncodePicture (void *encoder, NV_ENC_PIC_PARAMS * pic_params)
|
|||
return nvenc_api.nvEncEncodePicture (encoder, pic_params);
|
||||
}
|
||||
|
||||
NVENCSTATUS NVENCAPI
|
||||
NvEncRegisterAsyncEvent (void *encoder, NV_ENC_EVENT_PARAMS * event_params)
|
||||
{
|
||||
g_assert (nvenc_api.nvEncRegisterAsyncEvent != NULL);
|
||||
return nvenc_api.nvEncRegisterAsyncEvent (encoder, event_params);
|
||||
}
|
||||
|
||||
NVENCSTATUS NVENCAPI
|
||||
NvEncUnregisterAsyncEvent (void *encoder, NV_ENC_EVENT_PARAMS * event_params)
|
||||
{
|
||||
g_assert (nvenc_api.nvEncUnregisterAsyncEvent != NULL);
|
||||
return nvenc_api.nvEncUnregisterAsyncEvent (encoder, event_params);
|
||||
}
|
||||
|
||||
gboolean
|
||||
gst_nvenc_cmp_guid (GUID g1, GUID g2)
|
||||
{
|
||||
|
|
|
@ -26,6 +26,8 @@
|
|||
#include "gstcudaloader.h"
|
||||
#include "nvEncodeAPI.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
gboolean gst_nvenc_cmp_guid (GUID g1, GUID g2);
|
||||
|
||||
NV_ENC_BUFFER_FORMAT gst_nvenc_get_nv_buffer_format (GstVideoFormat fmt);
|
||||
|
@ -89,4 +91,6 @@ guint32 gst_nvenc_get_open_encode_session_ex_params_version (voi
|
|||
gboolean gst_nvenc_load_library (guint * api_major_ver,
|
||||
guint * api_minor_ver);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __GST_NVENC_H_INCLUDED__ */
|
||||
|
|
2000
subprojects/gst-plugins-bad/sys/nvcodec/gstnvencoder.cpp
Normal file
2000
subprojects/gst-plugins-bad/sys/nvcodec/gstnvencoder.cpp
Normal file
File diff suppressed because it is too large
Load diff
173
subprojects/gst-plugins-bad/sys/nvcodec/gstnvencoder.h
Normal file
173
subprojects/gst-plugins-bad/sys/nvcodec/gstnvencoder.h
Normal file
|
@ -0,0 +1,173 @@
|
|||
/* GStreamer
|
||||
* Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gst/gst.h>
|
||||
#include <gst/video/video.h>
|
||||
|
||||
#ifdef HAVE_NVCODEC_GST_D3D11
|
||||
#include <gst/d3d11/gstd3d11.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "nvEncodeAPI.h"
|
||||
#include "gstnvenc.h"
|
||||
#include "gstcudamemory.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define GST_TYPE_NV_ENCODER (gst_nv_encoder_get_type())
|
||||
#define GST_NV_ENCODER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_NV_ENCODER, GstNvEncoder))
|
||||
#define GST_NV_ENCODER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_NV_ENCODER, GstNvEncoderClass))
|
||||
#define GST_IS_NV_ENCODER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_NV_ENCODER))
|
||||
#define GST_IS_NV_ENCODER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_NV_ENCODER))
|
||||
#define GST_NV_ENCODER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_NV_ENCODER, GstNvEncoderClass))
|
||||
#define GST_NV_ENCODER_CAST(obj) ((GstNvEncoder *)obj)
|
||||
|
||||
typedef struct _GstNvEncoder GstNvEncoder;
|
||||
typedef struct _GstNvEncoderClass GstNvEncoderClass;
|
||||
typedef struct _GstNvEncoderPrivate GstNvEncoderPrivate;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GST_NV_ENCODER_RECONFIGURE_NONE,
|
||||
GST_NV_ENCODER_RECONFIGURE_BITRATE,
|
||||
GST_NV_ENCODER_RECONFIGURE_FULL,
|
||||
} GstNvEncoderReconfigure;
|
||||
|
||||
#define GST_TYPE_NV_ENCODER_PRESET (gst_nv_encoder_preset_get_type())
|
||||
GType gst_nv_encoder_preset_get_type (void);
|
||||
typedef enum
|
||||
{
|
||||
GST_NV_ENCODER_PRESET_DEFAULT,
|
||||
GST_NV_ENCODER_PRESET_HP,
|
||||
GST_NV_ENCODER_PRESET_HQ,
|
||||
GST_NV_ENCODER_PRESET_LOW_LATENCY_DEFAULT,
|
||||
GST_NV_ENCODER_PRESET_LOW_LATENCY_HQ,
|
||||
GST_NV_ENCODER_PRESET_LOW_LATENCY_HP,
|
||||
GST_NV_ENCODER_PRESET_LOSSLESS_DEFAULT,
|
||||
GST_NV_ENCODER_PRESET_LOSSLESS_HP,
|
||||
} GstNvEncoderPreset;
|
||||
|
||||
#define GST_TYPE_NV_ENCODER_RC_MODE (gst_nv_encoder_rc_mode_get_type())
|
||||
GType gst_nv_encoder_rc_mode_get_type (void);
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GST_NV_ENCODER_RC_MODE_DEFAULT,
|
||||
GST_NV_ENCODER_RC_MODE_CONSTQP,
|
||||
GST_NV_ENCODER_RC_MODE_VBR,
|
||||
GST_NV_ENCODER_RC_MODE_CBR,
|
||||
GST_NV_ENCODER_RC_MODE_CBR_LOWDELAY_HQ,
|
||||
GST_NV_ENCODER_RC_MODE_CBR_HQ,
|
||||
GST_NV_ENCODER_RC_MODE_VBR_HQ,
|
||||
} GstNvEncoderRCMode;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
/* without ref */
|
||||
GstNvEncoder *encoder;
|
||||
|
||||
/* Holds ownership */
|
||||
GstBuffer *buffer;
|
||||
GstMapInfo map_info;
|
||||
|
||||
NV_ENC_REGISTER_RESOURCE register_resource;
|
||||
NV_ENC_MAP_INPUT_RESOURCE mapped_resource;
|
||||
|
||||
/* Used when input resource cannot be registered */
|
||||
NV_ENC_CREATE_INPUT_BUFFER input_buffer;
|
||||
NV_ENC_LOCK_INPUT_BUFFER lk_input_buffer;
|
||||
|
||||
NV_ENC_OUTPUT_PTR output_ptr;
|
||||
gpointer event_handle;
|
||||
gboolean is_eos;
|
||||
} GstNvEncoderTask;
|
||||
|
||||
struct _GstNvEncoder
|
||||
{
|
||||
GstVideoEncoder parent;
|
||||
|
||||
GstNvEncoderPrivate *priv;
|
||||
};
|
||||
|
||||
struct _GstNvEncoderClass
|
||||
{
|
||||
GstVideoEncoderClass parent_class;
|
||||
|
||||
gboolean (*set_format) (GstNvEncoder * encoder,
|
||||
GstVideoCodecState * state,
|
||||
gpointer session,
|
||||
NV_ENC_INITIALIZE_PARAMS * init_params,
|
||||
NV_ENC_CONFIG * config);
|
||||
|
||||
gboolean (*set_output_state) (GstNvEncoder * encoder,
|
||||
GstVideoCodecState * state,
|
||||
gpointer session);
|
||||
|
||||
GstBuffer * (*create_output_buffer) (GstNvEncoder * encoder,
|
||||
NV_ENC_LOCK_BITSTREAM * bitstream);
|
||||
|
||||
GstNvEncoderReconfigure (*check_reconfigure) (GstNvEncoder * encoder,
|
||||
NV_ENC_CONFIG * config);
|
||||
};
|
||||
|
||||
GType gst_nv_encoder_get_type (void);
|
||||
|
||||
guint gst_nv_encoder_get_task_size (GstNvEncoder * encoder);
|
||||
|
||||
const gchar * gst_nv_encoder_status_to_string (NVENCSTATUS status);
|
||||
#define GST_NVENC_STATUS_FORMAT "s (%d)"
|
||||
#define GST_NVENC_STATUS_ARGS(s) gst_nv_encoder_status_to_string (s), s
|
||||
|
||||
void gst_nv_encoder_preset_to_guid (GstNvEncoderPreset preset,
|
||||
GUID * guid);
|
||||
|
||||
NV_ENC_PARAMS_RC_MODE gst_nv_encoder_rc_mode_to_native (GstNvEncoderRCMode rc_mode);
|
||||
|
||||
void gst_nv_encoder_set_cuda_device_id (GstNvEncoder * encoder,
|
||||
guint device_id);
|
||||
|
||||
void gst_nv_encoder_set_dxgi_adapter_luid (GstNvEncoder * encoder,
|
||||
gint64 adapter_luid);
|
||||
|
||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstNvEncoder, gst_object_unref)
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#ifdef __cplusplus
|
||||
#ifndef G_OS_WIN32
|
||||
inline bool is_equal_guid(const GUID & lhs, const GUID & rhs)
|
||||
{
|
||||
return !!memcmp(&lhs, &rhs, sizeof (GUID));
|
||||
}
|
||||
|
||||
inline bool operator==(const GUID & lhs, const GUID & rhs)
|
||||
{
|
||||
return is_equal_guid(lhs, rhs);
|
||||
}
|
||||
|
||||
inline bool operator!=(const GUID & lhs, const GUID & rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
#endif /* G_OS_WIN32 */
|
||||
#endif /* __cplusplus */
|
1928
subprojects/gst-plugins-bad/sys/nvcodec/gstnvh264encoder.cpp
Normal file
1928
subprojects/gst-plugins-bad/sys/nvcodec/gstnvh264encoder.cpp
Normal file
File diff suppressed because it is too large
Load diff
37
subprojects/gst-plugins-bad/sys/nvcodec/gstnvh264encoder.h
Normal file
37
subprojects/gst-plugins-bad/sys/nvcodec/gstnvh264encoder.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/* GStreamer
|
||||
* Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "gstnvencoder.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
void gst_nv_h264_encoder_register_cuda (GstPlugin * plugin,
|
||||
GstCudaContext * context,
|
||||
guint rank);
|
||||
|
||||
#ifdef HAVE_NVCODEC_GST_D3D11
|
||||
void gst_nv_h264_encoder_register_d3d11 (GstPlugin * plugin,
|
||||
GstD3D11Device * device,
|
||||
guint rank);
|
||||
#endif
|
||||
|
||||
|
||||
G_END_DECLS
|
1939
subprojects/gst-plugins-bad/sys/nvcodec/gstnvh265encoder.cpp
Normal file
1939
subprojects/gst-plugins-bad/sys/nvcodec/gstnvh265encoder.cpp
Normal file
File diff suppressed because it is too large
Load diff
36
subprojects/gst-plugins-bad/sys/nvcodec/gstnvh265encoder.h
Normal file
36
subprojects/gst-plugins-bad/sys/nvcodec/gstnvh265encoder.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
/* GStreamer
|
||||
* Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "gstnvencoder.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
void gst_nv_h265_encoder_register_cuda (GstPlugin * plugin,
|
||||
GstCudaContext * context,
|
||||
guint rank);
|
||||
|
||||
#ifdef HAVE_NVCODEC_GST_D3D11
|
||||
void gst_nv_h265_encoder_register_d3d11 (GstPlugin * plugin,
|
||||
GstD3D11Device * device,
|
||||
guint rank);
|
||||
#endif
|
||||
|
||||
G_END_DECLS
|
|
@ -25,6 +25,9 @@ nvcodec_sources = [
|
|||
'gstcudascale.c',
|
||||
'gstnvvp8dec.c',
|
||||
'gstnvvp9dec.c',
|
||||
'gstnvencoder.cpp',
|
||||
'gstnvh264encoder.cpp',
|
||||
'gstnvh265encoder.cpp',
|
||||
]
|
||||
|
||||
nvmm_sources = [
|
||||
|
@ -36,14 +39,14 @@ if get_option('nvcodec').disabled()
|
|||
endif
|
||||
|
||||
plugin_incdirs = [configinc, include_directories('./stub')]
|
||||
extra_c_args = ['-DGST_USE_UNSTABLE_API']
|
||||
extra_args = ['-DGST_USE_UNSTABLE_API']
|
||||
|
||||
if gstgl_dep.found()
|
||||
extra_c_args += ['-DHAVE_NVCODEC_GST_GL=1']
|
||||
extra_args += ['-DHAVE_NVCODEC_GST_GL=1']
|
||||
endif
|
||||
|
||||
if gstd3d11_dep.found()
|
||||
extra_c_args += ['-DHAVE_NVCODEC_GST_D3D11=1', '-DCOBJMACROS']
|
||||
extra_args += ['-DHAVE_NVCODEC_GST_D3D11=1', '-DCOBJMACROS']
|
||||
endif
|
||||
|
||||
if host_system == 'linux'
|
||||
|
@ -57,14 +60,29 @@ if host_system == 'linux'
|
|||
endif
|
||||
|
||||
if have_nvmm
|
||||
extra_c_args += ['-DHAVE_NVCODEC_NVMM']
|
||||
extra_args += ['-DHAVE_NVCODEC_NVMM']
|
||||
nvcodec_sources += nvmm_sources
|
||||
endif
|
||||
endif
|
||||
|
||||
override_opt = []
|
||||
if host_system == 'windows'
|
||||
# MinGW 32bits compiler seems to be complaining about redundant-decls
|
||||
# when ComPtr is in use. Let's just disable the warning
|
||||
if cc.get_id() != 'msvc'
|
||||
extra_args += cc.get_supported_arguments([
|
||||
'-Wno-redundant-decls',
|
||||
])
|
||||
endif
|
||||
else
|
||||
override_opt += ['cpp_std=c++11']
|
||||
endif
|
||||
|
||||
gstnvcodec = library('gstnvcodec',
|
||||
nvcodec_sources,
|
||||
c_args : gst_plugins_bad_args + extra_c_args,
|
||||
c_args : gst_plugins_bad_args + extra_args,
|
||||
cpp_args : gst_plugins_bad_args + extra_args,
|
||||
override_options: override_opt,
|
||||
include_directories : plugin_incdirs,
|
||||
dependencies : [gstbase_dep, gstvideo_dep, gstpbutils_dep, gstgl_dep, gstglproto_dep, gmodule_dep, gstcodecs_dep, gstd3d11_dep],
|
||||
install : true,
|
||||
|
|
|
@ -43,6 +43,12 @@
|
|||
#include "gstcudanvmm.h"
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_NVCODEC_GST_D3D11
|
||||
#include <gst/d3d11/gstd3d11.h>
|
||||
#endif
|
||||
#include "gstnvh264encoder.h"
|
||||
#include "gstnvh265encoder.h"
|
||||
|
||||
GST_DEBUG_CATEGORY (gst_nvcodec_debug);
|
||||
GST_DEBUG_CATEGORY (gst_nvdec_debug);
|
||||
GST_DEBUG_CATEGORY (gst_nvenc_debug);
|
||||
|
@ -59,7 +65,7 @@ plugin_init (GstPlugin * plugin)
|
|||
{
|
||||
CUresult cuda_ret;
|
||||
gint dev_count = 0;
|
||||
gint i;
|
||||
guint i;
|
||||
gboolean nvdec_available = TRUE;
|
||||
gboolean nvenc_available = TRUE;
|
||||
/* hardcoded minimum supported version */
|
||||
|
@ -139,24 +145,15 @@ plugin_init (GstPlugin * plugin)
|
|||
}
|
||||
|
||||
for (i = 0; i < dev_count; i++) {
|
||||
CUdevice cuda_device;
|
||||
GstCudaContext *context = gst_cuda_context_new (i);
|
||||
CUcontext cuda_ctx;
|
||||
|
||||
cuda_ret = CuDeviceGet (&cuda_device, i);
|
||||
if (cuda_ret != CUDA_SUCCESS) {
|
||||
GST_WARNING ("Failed to get device handle %d, ret: 0x%x", i,
|
||||
(gint) cuda_ret);
|
||||
if (!context) {
|
||||
GST_WARNING ("Failed to create context for deevice %d", i);
|
||||
continue;
|
||||
}
|
||||
|
||||
cuda_ret = CuCtxCreate (&cuda_ctx, 0, cuda_device);
|
||||
if (cuda_ret != CUDA_SUCCESS) {
|
||||
GST_WARNING ("Failed to create cuda context, ret: 0x%x", (gint) cuda_ret);
|
||||
continue;
|
||||
}
|
||||
|
||||
CuCtxPopCurrent (NULL);
|
||||
|
||||
cuda_ctx = gst_cuda_context_get_handle (context);
|
||||
if (nvdec_available) {
|
||||
gint j;
|
||||
|
||||
|
@ -237,10 +234,32 @@ plugin_init (GstPlugin * plugin)
|
|||
}
|
||||
}
|
||||
|
||||
if (nvenc_available)
|
||||
gst_nvenc_plugin_init (plugin, i, cuda_ctx);
|
||||
if (nvenc_available) {
|
||||
#ifdef HAVE_NVCODEC_GST_D3D11
|
||||
if (g_win32_check_windows_version (6, 0, 0, G_WIN32_OS_ANY)) {
|
||||
gint64 adapter_luid;
|
||||
GstD3D11Device *d3d11_device;
|
||||
|
||||
CuCtxDestroy (cuda_ctx);
|
||||
g_object_get (context, "dxgi-adapter-luid", &adapter_luid, NULL);
|
||||
d3d11_device = gst_d3d11_device_new_for_adapter_luid (adapter_luid,
|
||||
D3D11_CREATE_DEVICE_BGRA_SUPPORT);
|
||||
if (!d3d11_device) {
|
||||
GST_WARNING ("Failed to d3d11 create device");
|
||||
} else {
|
||||
gst_nv_h264_encoder_register_d3d11 (plugin,
|
||||
d3d11_device, GST_RANK_NONE);
|
||||
gst_nv_h265_encoder_register_d3d11 (plugin,
|
||||
d3d11_device, GST_RANK_NONE);
|
||||
gst_object_unref (d3d11_device);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
gst_nv_h264_encoder_register_cuda (plugin, context, GST_RANK_NONE);
|
||||
gst_nv_h265_encoder_register_cuda (plugin, context, GST_RANK_NONE);
|
||||
gst_nvenc_plugin_init (plugin, i, cuda_ctx);
|
||||
}
|
||||
|
||||
gst_object_unref (context);
|
||||
}
|
||||
|
||||
gst_cuda_memory_copy_register (plugin, GST_RANK_NONE);
|
||||
|
|
|
@ -34,16 +34,20 @@
|
|||
#include <termios.h>
|
||||
#endif
|
||||
|
||||
#ifdef G_OS_WIN32
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#endif
|
||||
|
||||
#include <gst/gst.h>
|
||||
|
||||
/* This is all not thread-safe, but doesn't have to be really */
|
||||
|
||||
#ifdef G_OS_UNIX
|
||||
|
||||
static struct termios term_settings;
|
||||
static gboolean term_settings_saved = FALSE;
|
||||
static GstNvCodecPlayKbFunc kb_callback;
|
||||
static gpointer kb_callback_data;
|
||||
|
||||
#ifdef G_OS_UNIX
|
||||
static struct termios term_settings;
|
||||
static gboolean term_settings_saved = FALSE;
|
||||
static gulong io_watch_id;
|
||||
|
||||
static gboolean
|
||||
|
@ -129,7 +133,158 @@ gst_nvcodec_kb_set_key_handler (GstNvCodecPlayKbFunc kb_func,
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
#else /* !G_OS_UNIX */
|
||||
#elif defined(G_OS_WIN32)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
GThread *thread;
|
||||
HANDLE event_handle;
|
||||
HANDLE console_handle;
|
||||
gboolean closing;
|
||||
GMutex lock;
|
||||
} Win32KeyHandler;
|
||||
|
||||
static Win32KeyHandler *win32_handler = NULL;
|
||||
|
||||
static gboolean
|
||||
gst_nvcodec_kb_source_cb (Win32KeyHandler * handler)
|
||||
{
|
||||
HANDLE h_input = handler->console_handle;
|
||||
INPUT_RECORD buffer;
|
||||
DWORD n;
|
||||
|
||||
if (PeekConsoleInput (h_input, &buffer, 1, &n) && n == 1) {
|
||||
ReadConsoleInput (h_input, &buffer, 1, &n);
|
||||
|
||||
if (buffer.EventType == KEY_EVENT && buffer.Event.KeyEvent.bKeyDown) {
|
||||
gchar key_val[2] = { 0 };
|
||||
|
||||
switch (buffer.Event.KeyEvent.wVirtualKeyCode) {
|
||||
case VK_RIGHT:
|
||||
kb_callback (GST_NVCODEC_KB_ARROW_RIGHT, kb_callback_data);
|
||||
break;
|
||||
case VK_LEFT:
|
||||
kb_callback (GST_NVCODEC_KB_ARROW_LEFT, kb_callback_data);
|
||||
break;
|
||||
case VK_UP:
|
||||
kb_callback (GST_NVCODEC_KB_ARROW_UP, kb_callback_data);
|
||||
break;
|
||||
case VK_DOWN:
|
||||
kb_callback (GST_NVCODEC_KB_ARROW_DOWN, kb_callback_data);
|
||||
break;
|
||||
default:
|
||||
key_val[0] = buffer.Event.KeyEvent.uChar.AsciiChar;
|
||||
kb_callback (key_val, kb_callback_data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return G_SOURCE_REMOVE;
|
||||
}
|
||||
|
||||
static gpointer
|
||||
gst_nvcodec_kb_win32_thread (gpointer user_data)
|
||||
{
|
||||
Win32KeyHandler *handler = (Win32KeyHandler *) user_data;
|
||||
HANDLE handles[2];
|
||||
|
||||
handles[0] = handler->event_handle;
|
||||
handles[1] = handler->console_handle;
|
||||
|
||||
if (!kb_callback)
|
||||
return NULL;
|
||||
|
||||
while (TRUE) {
|
||||
DWORD ret = WaitForMultipleObjects (2, handles, FALSE, INFINITE);
|
||||
|
||||
if (ret == WAIT_FAILED) {
|
||||
GST_WARNING ("WaitForMultipleObject Failed");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
g_mutex_lock (&handler->lock);
|
||||
if (handler->closing) {
|
||||
g_mutex_unlock (&handler->lock);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
g_mutex_unlock (&handler->lock);
|
||||
|
||||
g_idle_add ((GSourceFunc) gst_nvcodec_kb_source_cb, handler);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gboolean
|
||||
gst_nvcodec_kb_set_key_handler (GstNvCodecPlayKbFunc kb_func,
|
||||
gpointer user_data)
|
||||
{
|
||||
gint fd = _fileno (stdin);
|
||||
|
||||
if (!_isatty (fd)) {
|
||||
GST_INFO ("stdin is not connected to a terminal");
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (win32_handler) {
|
||||
g_mutex_lock (&win32_handler->lock);
|
||||
win32_handler->closing = TRUE;
|
||||
g_mutex_unlock (&win32_handler->lock);
|
||||
|
||||
SetEvent (win32_handler->event_handle);
|
||||
g_thread_join (win32_handler->thread);
|
||||
CloseHandle (win32_handler->event_handle);
|
||||
|
||||
g_mutex_clear (&win32_handler->lock);
|
||||
g_free (win32_handler);
|
||||
win32_handler = NULL;
|
||||
}
|
||||
|
||||
if (kb_func) {
|
||||
SECURITY_ATTRIBUTES sec_attrs;
|
||||
|
||||
sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
|
||||
sec_attrs.lpSecurityDescriptor = NULL;
|
||||
sec_attrs.bInheritHandle = FALSE;
|
||||
|
||||
win32_handler = g_new0 (Win32KeyHandler, 1);
|
||||
|
||||
/* create cancellable event handle */
|
||||
win32_handler->event_handle = CreateEvent (&sec_attrs, TRUE, FALSE, NULL);
|
||||
|
||||
if (!win32_handler->event_handle) {
|
||||
GST_WARNING ("Couldn't create event handle");
|
||||
g_free (win32_handler);
|
||||
win32_handler = NULL;
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
win32_handler->console_handle = GetStdHandle (STD_INPUT_HANDLE);
|
||||
if (!win32_handler->console_handle) {
|
||||
GST_WARNING ("Couldn't get console handle");
|
||||
CloseHandle (win32_handler->event_handle);
|
||||
g_free (win32_handler);
|
||||
win32_handler = NULL;
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
g_mutex_init (&win32_handler->lock);
|
||||
win32_handler->thread =
|
||||
g_thread_new ("gst-play-kb", gst_nvcodec_kb_win32_thread,
|
||||
win32_handler);
|
||||
}
|
||||
|
||||
kb_callback = kb_func;
|
||||
kb_callback_data = user_data;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
gboolean
|
||||
gst_nvcodec_kb_set_key_handler (GstNvCodecPlayKbFunc key_func,
|
||||
|
|
|
@ -231,14 +231,15 @@ bus_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
|
|||
}
|
||||
|
||||
static gboolean
|
||||
check_nvcodec_available (void)
|
||||
check_nvcodec_available (const gchar * encoder_name)
|
||||
{
|
||||
gboolean ret = TRUE;
|
||||
GstElement *elem;
|
||||
|
||||
elem = gst_element_factory_make ("nvh264enc", NULL);
|
||||
elem = gst_element_factory_make (encoder_name, NULL);
|
||||
if (!elem) {
|
||||
GST_WARNING ("nvh264enc is not available, possibly driver load failure");
|
||||
GST_WARNING ("%s is not available, possibly driver load failure",
|
||||
encoder_name);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
@ -332,13 +333,16 @@ main (gint argc, gchar ** argv)
|
|||
GstCaps *caps;
|
||||
TestCallbackData data = { 0, };
|
||||
GstPad *pad;
|
||||
|
||||
gchar *encoder_name = NULL;
|
||||
/* *INDENT-OFF* */
|
||||
GOptionEntry options[] = {
|
||||
{"use-gl", 0, 0, G_OPTION_ARG_NONE, &use_gl,
|
||||
"Use OpenGL memory as input to the nvenc", NULL}
|
||||
,
|
||||
"Use OpenGL memory as input to the nvenc", NULL},
|
||||
{"encoder", 0, 0, G_OPTION_ARG_STRING, &encoder_name,
|
||||
"NVENC encoder element to test, default: nvh264enc"},
|
||||
{NULL}
|
||||
};
|
||||
/* *INDENT-ON* */
|
||||
|
||||
option_ctx = g_option_context_new ("nvcodec dynamic reconfigure example");
|
||||
g_option_context_add_main_entries (option_ctx, options, NULL);
|
||||
|
@ -352,7 +356,10 @@ main (gint argc, gchar ** argv)
|
|||
g_option_context_free (option_ctx);
|
||||
gst_init (NULL, NULL);
|
||||
|
||||
if (!check_nvcodec_available ()) {
|
||||
if (!encoder_name)
|
||||
encoder_name = g_strdup ("nvh264enc");
|
||||
|
||||
if (!check_nvcodec_available (encoder_name)) {
|
||||
g_printerr ("Cannot load nvcodec plugin");
|
||||
exit (1);
|
||||
}
|
||||
|
@ -404,7 +411,7 @@ main (gint argc, gchar ** argv)
|
|||
|
||||
capsfilter = gst_element_factory_make ("capsfilter", NULL);
|
||||
queue = gst_element_factory_make ("queue", NULL);
|
||||
enc = gst_element_factory_make ("nvh264enc", NULL);
|
||||
enc = gst_element_factory_make (encoder_name, NULL);
|
||||
parse = gst_element_factory_make ("h264parse", NULL);
|
||||
|
||||
/* vbr with target bitrate */
|
||||
|
@ -471,6 +478,7 @@ terminate:
|
|||
|
||||
gst_object_unref (pipeline);
|
||||
g_main_loop_unref (loop);
|
||||
g_free (encoder_name);
|
||||
|
||||
return exitcode;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue