nvencoder: Fix min buffers parameter of allocation query in auto GPU mode
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / nvcodec / gstnvencoder.h
1 /* GStreamer
2  * Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #pragma once
21
22 #include <gst/gst.h>
23 #include <gst/video/video.h>
24
25 #ifdef GST_CUDA_HAS_D3D
26 #include <gst/d3d11/gstd3d11.h>
27 #endif
28
29 #include <string.h>
30
31 #include "nvEncodeAPI.h"
32 #include "gstnvenc.h"
33 #include <gst/cuda/gstcudamemory.h>
34
35 G_BEGIN_DECLS
36
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)
44
45 typedef struct _GstNvEncoder GstNvEncoder;
46 typedef struct _GstNvEncoderClass GstNvEncoderClass;
47 typedef struct _GstNvEncoderPrivate GstNvEncoderPrivate;
48
49 typedef enum
50 {
51   GST_NV_ENCODER_RECONFIGURE_NONE,
52   GST_NV_ENCODER_RECONFIGURE_BITRATE,
53   GST_NV_ENCODER_RECONFIGURE_FULL,
54 } GstNvEncoderReconfigure;
55
56 #define GST_TYPE_NV_ENCODER_PRESET (gst_nv_encoder_preset_get_type())
57 GType gst_nv_encoder_preset_get_type (void);
58 typedef enum
59 {
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,
68 } GstNvEncoderPreset;
69
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);
72
73 typedef enum
74 {
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,
81 } GstNvEncoderRCMode;
82
83 typedef struct
84 {
85   gint max_bframes;
86   gint ratecontrol_modes;
87   gint field_encoding;
88   gint monochrome;
89   gint fmo;
90   gint qpelmv;
91   gint bdirect_mode;
92   gint cabac;
93   gint adaptive_transform;
94   gint stereo_mvc;
95   gint temoral_layers;
96   gint hierarchical_pframes;
97   gint hierarchical_bframes;
98   gint level_max;
99   gint level_min;
100   gint separate_colour_plane;
101   gint width_max;
102   gint height_max;
103   gint temporal_svc;
104   gint dyn_res_change;
105   gint dyn_bitrate_change;
106   gint dyn_force_constqp;
107   gint dyn_rcmode_change;
108   gint subframe_readback;
109   gint constrained_encoding;
110   gint intra_refresh;
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;
116   gint mb_num_max;
117   gint mb_per_sec_max;
118   gint yuv444_encode;
119   gint lossless_encode;
120   gint sao;
121   gint meonly_mode;
122   gint lookahead;
123   gint temporal_aq;
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;
129   gint width_min;
130   gint height_min;
131   gint multiple_ref_frames;
132 } GstNvEncoderDeviceCaps;
133
134 typedef enum
135 {
136   GST_NV_ENCODER_DEVICE_D3D11,
137   GST_NV_ENCODER_DEVICE_CUDA,
138   GST_NV_ENCODER_DEVICE_AUTO_SELECT,
139 } GstNvEncoderDeviceMode;
140
141 typedef struct
142 {
143   GstCaps *sink_caps;
144   GstCaps *src_caps;
145
146   guint cuda_device_id;
147   gint64 adapter_luid;
148
149   GstNvEncoderDeviceMode device_mode;
150   GstNvEncoderDeviceCaps device_caps;
151
152   GList *formats;
153   GList *profiles;
154
155   /* auto gpu select mode */
156   guint adapter_luid_size;
157   gint64 adapter_luid_list[8];
158
159   guint cuda_device_id_size;
160   guint cuda_device_id_list[8];
161
162   gint ref_count;
163 } GstNvEncoderClassData;
164
165 typedef struct
166 {
167   /* without ref */
168   GstNvEncoder *encoder;
169
170   /* Holds ownership */
171   GstBuffer *buffer;
172   GstMapInfo map_info;
173
174   NV_ENC_REGISTER_RESOURCE register_resource;
175   NV_ENC_MAP_INPUT_RESOURCE mapped_resource;
176
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;
180
181   NV_ENC_OUTPUT_PTR output_ptr;
182   gpointer event_handle;
183   gboolean is_eos;
184 } GstNvEncoderTask;
185
186 typedef struct
187 {
188   GstNvEncoderDeviceMode device_mode;
189   guint cuda_device_id;
190   gint64 adapter_luid;
191   GstObject *device;
192 } GstNvEncoderDeviceData;
193
194 struct _GstNvEncoder
195 {
196   GstVideoEncoder parent;
197
198   GstNvEncoderPrivate *priv;
199 };
200
201 struct _GstNvEncoderClass
202 {
203   GstVideoEncoderClass parent_class;
204
205   gboolean    (*set_format)           (GstNvEncoder * encoder,
206                                        GstVideoCodecState * state,
207                                        gpointer session,
208                                        NV_ENC_INITIALIZE_PARAMS * init_params,
209                                        NV_ENC_CONFIG * config);
210
211   gboolean    (*set_output_state)     (GstNvEncoder * encoder,
212                                        GstVideoCodecState * state,
213                                        gpointer session);
214
215   GstBuffer * (*create_output_buffer) (GstNvEncoder * encoder,
216                                        NV_ENC_LOCK_BITSTREAM * bitstream);
217
218   GstNvEncoderReconfigure (*check_reconfigure)  (GstNvEncoder * encoder,
219                                                  NV_ENC_CONFIG * config);
220
221   gboolean    (*select_device)        (GstNvEncoder * encoder,
222                                        const GstVideoInfo * info,
223                                        GstBuffer * buffer,
224                                        GstNvEncoderDeviceData * data);
225
226   guint       (*calculate_min_buffers) (GstNvEncoder * encoder);
227 };
228
229 GType gst_nv_encoder_get_type (void);
230
231 guint gst_nv_encoder_get_task_size (GstNvEncoder * encoder);
232
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
236
237 void gst_nv_encoder_preset_to_guid (GstNvEncoderPreset preset,
238                                     GUID * guid);
239
240 NV_ENC_PARAMS_RC_MODE gst_nv_encoder_rc_mode_to_native (GstNvEncoderRCMode rc_mode);
241
242 void gst_nv_encoder_set_device_mode (GstNvEncoder * encoder,
243                                      GstNvEncoderDeviceMode mode,
244                                      guint cuda_device_id,
245                                      gint64 adapter_luid);
246
247 GstNvEncoderClassData * gst_nv_encoder_class_data_new (void);
248
249 GstNvEncoderClassData * gst_nv_encoder_class_data_ref (GstNvEncoderClassData * cdata);
250
251 void gst_nv_encoder_class_data_unref (GstNvEncoderClassData * cdata);
252
253 void gst_nv_encoder_get_encoder_caps (gpointer session,
254                                       const GUID * encode_guid,
255                                       GstNvEncoderDeviceCaps * device_caps);
256
257 void gst_nv_encoder_merge_device_caps (const GstNvEncoderDeviceCaps * a,
258                                        const GstNvEncoderDeviceCaps * b,
259                                        GstNvEncoderDeviceCaps * merged);
260
261
262 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstNvEncoder, gst_object_unref)
263
264 G_END_DECLS
265
266 #ifdef __cplusplus
267 #ifndef G_OS_WIN32
268 inline bool is_equal_guid(const GUID & lhs, const GUID & rhs)
269 {
270   return memcmp(&lhs, &rhs, sizeof (GUID)) == 0;
271 }
272
273 inline bool operator==(const GUID & lhs, const GUID & rhs)
274 {
275   return is_equal_guid(lhs, rhs);
276 }
277
278 inline bool operator!=(const GUID & lhs, const GUID & rhs)
279 {
280   return !is_equal_guid(lhs, rhs);
281 }
282 #endif /* G_OS_WIN32 */
283 #endif /* __cplusplus */