2 * Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 #include <gst/video/video.h>
25 #ifdef GST_CUDA_HAS_D3D
26 #include <gst/d3d11/gstd3d11.h>
31 #include "nvEncodeAPI.h"
33 #include <gst/cuda/gstcudamemory.h>
37 #define GST_TYPE_NV_ENCODER (gst_nv_encoder_get_type())
38 #define GST_NV_ENCODER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_NV_ENCODER, GstNvEncoder))
39 #define GST_NV_ENCODER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_NV_ENCODER, GstNvEncoderClass))
40 #define GST_IS_NV_ENCODER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_NV_ENCODER))
41 #define GST_IS_NV_ENCODER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_NV_ENCODER))
42 #define GST_NV_ENCODER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_NV_ENCODER, GstNvEncoderClass))
43 #define GST_NV_ENCODER_CAST(obj) ((GstNvEncoder *)obj)
45 typedef struct _GstNvEncoder GstNvEncoder;
46 typedef struct _GstNvEncoderClass GstNvEncoderClass;
47 typedef struct _GstNvEncoderPrivate GstNvEncoderPrivate;
51 GST_NV_ENCODER_RECONFIGURE_NONE,
52 GST_NV_ENCODER_RECONFIGURE_BITRATE,
53 GST_NV_ENCODER_RECONFIGURE_FULL,
54 } GstNvEncoderReconfigure;
56 #define GST_TYPE_NV_ENCODER_PRESET (gst_nv_encoder_preset_get_type())
57 GType gst_nv_encoder_preset_get_type (void);
60 GST_NV_ENCODER_PRESET_DEFAULT,
61 GST_NV_ENCODER_PRESET_HP,
62 GST_NV_ENCODER_PRESET_HQ,
63 GST_NV_ENCODER_PRESET_LOW_LATENCY_DEFAULT,
64 GST_NV_ENCODER_PRESET_LOW_LATENCY_HQ,
65 GST_NV_ENCODER_PRESET_LOW_LATENCY_HP,
66 GST_NV_ENCODER_PRESET_LOSSLESS_DEFAULT,
67 GST_NV_ENCODER_PRESET_LOSSLESS_HP,
70 #define GST_TYPE_NV_ENCODER_RC_MODE (gst_nv_encoder_rc_mode_get_type())
71 GType gst_nv_encoder_rc_mode_get_type (void);
75 GST_NV_ENCODER_RC_MODE_CONSTQP,
76 GST_NV_ENCODER_RC_MODE_VBR,
77 GST_NV_ENCODER_RC_MODE_CBR,
78 GST_NV_ENCODER_RC_MODE_CBR_LOWDELAY_HQ,
79 GST_NV_ENCODER_RC_MODE_CBR_HQ,
80 GST_NV_ENCODER_RC_MODE_VBR_HQ,
86 gint ratecontrol_modes;
93 gint adaptive_transform;
96 gint hierarchical_pframes;
97 gint hierarchical_bframes;
100 gint separate_colour_plane;
105 gint dyn_bitrate_change;
106 gint dyn_force_constqp;
107 gint dyn_rcmode_change;
108 gint subframe_readback;
109 gint constrained_encoding;
111 gint custom_vbv_buf_size;
112 gint dynamic_slice_mode;
113 gint ref_pic_invalidation;
114 gint preproc_support;
115 gint async_encoding_support;
119 gint lossless_encode;
124 gint supports_10bit_encode;
125 gint num_max_ltr_frames;
126 gint weighted_prediction;
127 gint bframe_ref_mode;
128 gint emphasis_level_map;
131 gint multiple_ref_frames;
132 } GstNvEncoderDeviceCaps;
136 GST_NV_ENCODER_DEVICE_D3D11,
137 GST_NV_ENCODER_DEVICE_CUDA,
138 GST_NV_ENCODER_DEVICE_AUTO_SELECT,
139 } GstNvEncoderDeviceMode;
146 guint cuda_device_id;
149 GstNvEncoderDeviceMode device_mode;
150 GstNvEncoderDeviceCaps device_caps;
155 /* auto gpu select mode */
156 guint adapter_luid_size;
157 gint64 adapter_luid_list[8];
159 guint cuda_device_id_size;
160 guint cuda_device_id_list[8];
163 } GstNvEncoderClassData;
168 GstNvEncoder *encoder;
170 /* Holds ownership */
174 NV_ENC_REGISTER_RESOURCE register_resource;
175 NV_ENC_MAP_INPUT_RESOURCE mapped_resource;
177 /* Used when input resource cannot be registered */
178 NV_ENC_CREATE_INPUT_BUFFER input_buffer;
179 NV_ENC_LOCK_INPUT_BUFFER lk_input_buffer;
181 NV_ENC_OUTPUT_PTR output_ptr;
182 gpointer event_handle;
188 GstNvEncoderDeviceMode device_mode;
189 guint cuda_device_id;
192 } GstNvEncoderDeviceData;
196 GstVideoEncoder parent;
198 GstNvEncoderPrivate *priv;
201 struct _GstNvEncoderClass
203 GstVideoEncoderClass parent_class;
205 gboolean (*set_format) (GstNvEncoder * encoder,
206 GstVideoCodecState * state,
208 NV_ENC_INITIALIZE_PARAMS * init_params,
209 NV_ENC_CONFIG * config);
211 gboolean (*set_output_state) (GstNvEncoder * encoder,
212 GstVideoCodecState * state,
215 GstBuffer * (*create_output_buffer) (GstNvEncoder * encoder,
216 NV_ENC_LOCK_BITSTREAM * bitstream);
218 GstNvEncoderReconfigure (*check_reconfigure) (GstNvEncoder * encoder,
219 NV_ENC_CONFIG * config);
221 gboolean (*select_device) (GstNvEncoder * encoder,
222 const GstVideoInfo * info,
224 GstNvEncoderDeviceData * data);
226 guint (*calculate_min_buffers) (GstNvEncoder * encoder);
229 GType gst_nv_encoder_get_type (void);
231 guint gst_nv_encoder_get_task_size (GstNvEncoder * encoder);
233 const gchar * gst_nv_encoder_status_to_string (NVENCSTATUS status);
234 #define GST_NVENC_STATUS_FORMAT "s (%d)"
235 #define GST_NVENC_STATUS_ARGS(s) gst_nv_encoder_status_to_string (s), s
237 void gst_nv_encoder_preset_to_guid (GstNvEncoderPreset preset,
240 NV_ENC_PARAMS_RC_MODE gst_nv_encoder_rc_mode_to_native (GstNvEncoderRCMode rc_mode);
242 void gst_nv_encoder_set_device_mode (GstNvEncoder * encoder,
243 GstNvEncoderDeviceMode mode,
244 guint cuda_device_id,
245 gint64 adapter_luid);
247 GstNvEncoderClassData * gst_nv_encoder_class_data_new (void);
249 GstNvEncoderClassData * gst_nv_encoder_class_data_ref (GstNvEncoderClassData * cdata);
251 void gst_nv_encoder_class_data_unref (GstNvEncoderClassData * cdata);
253 void gst_nv_encoder_get_encoder_caps (gpointer session,
254 const GUID * encode_guid,
255 GstNvEncoderDeviceCaps * device_caps);
257 void gst_nv_encoder_merge_device_caps (const GstNvEncoderDeviceCaps * a,
258 const GstNvEncoderDeviceCaps * b,
259 GstNvEncoderDeviceCaps * merged);
262 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstNvEncoder, gst_object_unref)
268 inline bool is_equal_guid(const GUID & lhs, const GUID & rhs)
270 return memcmp(&lhs, &rhs, sizeof (GUID)) == 0;
273 inline bool operator==(const GUID & lhs, const GUID & rhs)
275 return is_equal_guid(lhs, rhs);
278 inline bool operator!=(const GUID & lhs, const GUID & rhs)
280 return !is_equal_guid(lhs, rhs);
282 #endif /* G_OS_WIN32 */
283 #endif /* __cplusplus */