forked from audiohacked/obs-studio-plugin-nvenc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obs-nvenc-helper.c
384 lines (329 loc) · 12.5 KB
/
obs-nvenc-helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/******************************************************************************
Copyright (C) 2015 by Sean Nelson <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <stdio.h>
#include <util/dstr.h>
#include <util/darray.h>
#include <util/platform.h>
#include <obs-module.h>
#include "obs-nvenc.h"
#include "nvEncodeAPI.h"
/* ------------------------------------------------------------------------- */
void clear_data(struct obs_nvenc *obsnv)
{
if (obsnv->nvenc_device) {
obsnv->api->nvEncDestroyInputBuffer(obsnv->nvenc_device,
&obsnv->nvenc_buffer_input);
obsnv->api->nvEncDestroyBitstreamBuffer(obsnv->nvenc_device,
&obsnv->nvenc_buffer_output);
obsnv->api->nvEncDestroyEncoder(obsnv->nvenc_device);
bfree(obsnv->nvenc_device);
bfree(obsnv->sei);
bfree(obsnv->extra_data);
bfree(obsnv->api);
obsnv->nvenc_device = NULL;
obsnv->sei = NULL;
obsnv->extra_data = NULL;
obsnv->api = NULL;
}
}
/* ------------------------------------------------------------------------- */
NVENCSTATUS obs_nvenc_helper_create_instance(void *data)
{
struct obs_nvenc *obsnv = data;
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
NVENCSTATUS (*nvEncodeAPICreateInstance)(NV_ENCODE_API_FUNCTION_LIST *);
// get function from DLL
nvEncodeAPICreateInstance = os_dlsym(obs_nvenc_lib,
"NvEncodeAPICreateInstance");
if (nvEncodeAPICreateInstance == NULL)
error("Unable to create API Instance");
// set up pointer for nVidia Encoder Function List
memset(&obsnv->api, 0, sizeof(NV_ENCODE_API_FUNCTION_LIST));
obsnv->api->version = NV_ENCODE_API_FUNCTION_LIST_VER;
// set up API Instance
nvStatus = nvEncodeAPICreateInstance(obsnv->api);
return nvStatus;
}
NVENCSTATUS obs_nvenc_helper_open_session(void* data)
{
struct obs_nvenc *obsnv = data;
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS session_params;
// set up session parameter data structure
memset(&session_params, 0, sizeof(session_params));
session_params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
// fill parameters with data
session_params.device = gs_get_context();
if (gs_get_device_type() == GS_DEVICE_DIRECT3D_11)
session_params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
else
session_params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
session_params.reserved = NULL;
session_params.apiVersion = NVENCAPI_VERSION;
// Open Session Encoder
obsnv->api->nvEncOpenEncodeSessionEx(&session_params, &obsnv->nvenc_device);
return nvStatus;
}
GUID obs_nvenc_helper_get_guid_codec(void)
{
//NvEncodeAPI 5.0 Codecs:
// NV_ENC_CODEC_HEVC_GUID
return NV_ENC_CODEC_H264_GUID;
}
GUID obs_nvenc_helper_get_guid_preset(OBS_NVENC_PRESET preset)
{
//NvEncodeAPI 5.0 Presets:
// NV_ENC_PRESET_DEFAULT_GUID
// NV_ENC_PRESET_HP_GUID
// NV_ENC_PRESET_HQ_GUID
// NV_ENC_PRESET_BD_GUID
// NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID
// NV_ENC_PRESET_LOW_LATENCY_HQ_GUID
// NV_ENC_PRESET_LOW_LATENCY_HP_GUID
// NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID
// NV_ENC_PRESET_LOSSLESS_HP_GUID
switch (preset) {
case OBS_NVENC_PRESET_DEFAULT:
return NV_ENC_PRESET_DEFAULT_GUID;
case OBS_NVENC_PRESET_HP:
return NV_ENC_PRESET_HP_GUID;
case OBS_NVENC_PRESET_HQ:
return NV_ENC_PRESET_HQ_GUID;
case OBS_NVENC_PRESET_BD:
return NV_ENC_PRESET_BD_GUID;
case OBS_NVENC_PRESET_LOW_LATENCY:
return NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID;
case OBS_NVENC_PRESET_LOW_LATENCY_HQ:
return NV_ENC_PRESET_LOW_LATENCY_HQ_GUID;
case OBS_NVENC_PRESET_LOW_LATENCY_HP:
return NV_ENC_PRESET_LOW_LATENCY_HP_GUID;
case OBS_NVENC_PRESET_LOSSLESS:
return NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID;
case OBS_NVENC_PRESET_LOSSLESS_HP:
return NV_ENC_PRESET_LOSSLESS_HP_GUID;
default:
return NV_ENC_PRESET_LOW_LATENCY_HQ_GUID;
}
}
GUID obs_nvenc_helper_get_guid_profile(OBS_NVENC_PROFILE profile)
{
//NvEncodeAPI 5.0 Profiles:
// NV_ENC_CODEC_PROFILE_AUTOSELECT_GUID
// NV_ENC_H264_PROFILE_BASELINE_GUID
// NV_ENC_H264_PROFILE_MAIN_GUID
// NV_ENC_H264_PROFILE_HIGH_GUID
// NV_ENC_H264_PROFILE_HIGH_444_GUID
// NV_ENC_H264_PROFILE_STEREO_GUID
// NV_ENC_H264_PROFILE_SVC_TEMPORAL_SCALABILTY
// NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID
// NV_ENC_HEVC_PROFILE_MAIN_GUID
switch (profile) {
case OBS_NVENC_PROFILE_AUTOSELECT:
return NV_ENC_CODEC_PROFILE_AUTOSELECT_GUID;
case OBS_NVENC_PROFILE_H264_BASELINE:
return NV_ENC_H264_PROFILE_BASELINE_GUID;
case OBS_NVENC_PROFILE_H264_MAIN:
return NV_ENC_H264_PROFILE_MAIN_GUID;
case OBS_NVENC_PROFILE_H264_HIGH:
return NV_ENC_H264_PROFILE_HIGH_GUID;
case OBS_NVENC_PROFILE_H264_HIGH_444:
return NV_ENC_H264_PROFILE_HIGH_444_GUID;
case OBS_NVENC_PROFILE_H264_STEREO:
return NV_ENC_H264_PROFILE_STEREO_GUID;
case OBS_NVENC_PROFILE_H264_SVC_TEMPORAL_SCALABILTY:
return NV_ENC_H264_PROFILE_SVC_TEMPORAL_SCALABILTY;
case OBS_NVENC_PROFILE_H264_CONSTRAINED_HIGH:
return NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID;
case OBS_NVENC_PROFILE_H265_MAIN:
return NV_ENC_HEVC_PROFILE_MAIN_GUID;
default:
return NV_ENC_H264_PROFILE_MAIN_GUID;
}
}
const char* obs_nvenc_profile_string(OBS_NVENC_PROFILE profile)
{
switch (profile) {
case OBS_NVENC_PROFILE_AUTOSELECT:
return "Auto Select Profile based on Codec";
case OBS_NVENC_PROFILE_H264_BASELINE:
return "H.264 Baseline";
case OBS_NVENC_PROFILE_H264_MAIN:
return "H.264 Main Profile";
case OBS_NVENC_PROFILE_H264_HIGH:
return "H.264 High Profile";;
case OBS_NVENC_PROFILE_H264_HIGH_444:
return "H.264 High Profile (444)";;
case OBS_NVENC_PROFILE_H264_STEREO:
return "H.264 Stereo";
case OBS_NVENC_PROFILE_H264_SVC_TEMPORAL_SCALABILTY:
return "H.264 SVC Temporal Scalabilty";
case OBS_NVENC_PROFILE_H264_CONSTRAINED_HIGH:
return "H.264 High Profile Constrained";
case OBS_NVENC_PROFILE_H265_MAIN:
return "H.265 Main Profile";
default:
return "Unknown Encoder Profile";
}
}
const char* obs_nvenc_preset_string(OBS_NVENC_PRESET preset)
{
switch (preset) {
case OBS_NVENC_PRESET_DEFAULT:
return "Default";
case OBS_NVENC_PRESET_HP:
return "High Performance";
case OBS_NVENC_PRESET_HQ:
return "High Quality";
case OBS_NVENC_PRESET_BD:
return "Blu-Ray";
case OBS_NVENC_PRESET_LOW_LATENCY:
return "Low Latency";
case OBS_NVENC_PRESET_LOW_LATENCY_HQ:
return "Low Latency High Quality";
case OBS_NVENC_PRESET_LOW_LATENCY_HP:
return "Low Latency High Performance";
case OBS_NVENC_PRESET_LOSSLESS:
return "Lossless";
case OBS_NVENC_PRESET_LOSSLESS_HP:
return "Lossless High Performance";
default:
return "Unknown Encoder Preset";
}
}
NVENCSTATUS obs_nvenc_helper_select_codec(void *data)
{
struct obs_nvenc *obsnv = data;
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
//uint32_t codec_count = 0;
//GUID *codec_array;
//obsnv->api->nvEncGetEncodeGUIDCount(obsnv->nvenc_device, &codec_count);
//memset(&codec_array, 0, codec_count*sizeof(GUID));
//obsnv->api->nvEncGetEncodeGUIDs(obsnv->nvenc_device, codec_array, codec_count, &codec_count);
obsnv->nvenc_codec = obs_nvenc_helper_get_guid_codec();
return nvStatus;
}
NVENCSTATUS obs_nvenc_helper_get_preset(void *data)
{
struct obs_nvenc *obsnv = data;
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
//uint32_t preset_count = 0;
//GUID *nvenc_preset_array;
//NV_ENC_PRESET_CONFIG nvenc_config_preset;
//obsnv->api->nvEncGetEncodePresetCount(obsnv->nvenc_device, obsnv->nvenc_codec, &preset_count);
//memset(&nvenc_preset_array, 0, preset_count*sizeof(GUID));
//obsnv->api->nvEncGetEncodePresetGUIDs(obsnv->nvenc_device, obsnv->nvenc_codec, nvenc_preset_array, preset_count, &preset_count);
obsnv->nvenc_preset = obs_nvenc_helper_get_guid_preset(0);
//nvStatus = obsnv->api->nvEncGetEncodePresetConfig(obsnv->nvenc_device, obsnv->nvenc_codec, obsnv->nvenc_preset, &nvenc_config_preset);
return nvStatus;
}
NVENCSTATUS obs_nvenc_helper_set_profile(void *data)
{
struct obs_nvenc *obsnv = data;
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
//uint32_t profile_count = 0;
//GUID *nvenc_profile_array;
//obsnv->api->nvEncGetEncodeProfileGUIDCount(obsnv->nvenc_device, obsnv->nvenc_codec, &profile_count);
//memset(&nvenc_profile_array, 0, profile_count*sizeof(GUID));
//obsnv->api->nvEncGetEncodeProfileGUIDs(obsnv->nvenc_device, obsnv->nvenc_codec, nvenc_profile_array, profile_count, &profile_count);
obsnv->nvenc_profile = obs_nvenc_helper_get_guid_profile(0);
return nvStatus;
}
NVENCSTATUS obs_nvenc_helper_get_input_formats(void *data)
{
struct obs_nvenc *obsnv = data;
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
uint32_t input_format_count = 0;
NV_ENC_BUFFER_FORMAT *nvenc_config_input_format;
obsnv->api->nvEncGetInputFormatCount(obsnv->nvenc_device,
obsnv->nvenc_codec, &input_format_count);
memset(&nvenc_config_input_format, 0,
input_format_count*sizeof(NV_ENC_BUFFER_FORMAT));
obsnv->api->nvEncGetInputFormats(obsnv->nvenc_device,
obsnv->nvenc_codec, nvenc_config_input_format,
input_format_count, &input_format_count);
return nvStatus;
}
NVENCSTATUS obs_nvenc_helper_init_encoder(void *data)
{
struct obs_nvenc *obsnv = data;
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
NV_ENC_INITIALIZE_PARAMS nvenc_config_init;
//obsnv->nvenc_config_encode->profileGUID = obsnv->nvenc_profile;
memset(&nvenc_config_init, 0, sizeof(NV_ENC_INITIALIZE_PARAMS));
SET_VER(nvenc_config_init, NV_ENC_INITIALIZE_PARAMS);
nvenc_config_init.encodeGUID = obs_nvenc_helper_get_guid_codec();
nvenc_config_init.encodeWidth =
(int)obs_encoder_get_width(obsnv->encoder);
nvenc_config_init.encodeHeight =
(int)obs_encoder_get_height(obsnv->encoder);
//nvenc_config_init.reportSliceOffsets =
//nvenc_config_init.enableEncodeAsync =
//nvenc_config_init.encodeConfig = obsnv->nvenc_config_encode;
//nvenc_config_init.encodeConfig->encodeCodecConfig =
// (NV_ENC_CODEC_CONFIG)obsnv->nvenc_config_h264;
nvStatus = obsnv->api->nvEncInitializeEncoder(obsnv->encoder,
&nvenc_config_init);
return nvStatus;
}
NVENCSTATUS obs_nvenc_helper_create_buffer(void *data)
{
struct obs_nvenc *obsnv = data;
NVENCSTATUS nvStatus = NV_ENC_SUCCESS;
// initialize input buffer
memset(&obsnv->nvenc_buffer_input, 0,
sizeof(NV_ENC_CREATE_INPUT_BUFFER));
SET_VER(obsnv->nvenc_buffer_input, NV_ENC_CREATE_INPUT_BUFFER);
obsnv->nvenc_buffer_input.bufferFmt = NV_ENC_BUFFER_FORMAT_NV12_PL;
obsnv->api->nvEncCreateInputBuffer(obsnv->nvenc_device,
&obsnv->nvenc_buffer_input);
// initialize output bitstream buffer
memset(&obsnv->nvenc_buffer_output, 0,
sizeof(NV_ENC_CREATE_BITSTREAM_BUFFER));
SET_VER(obsnv->nvenc_buffer_output, NV_ENC_CREATE_BITSTREAM_BUFFER);
obsnv->api->nvEncCreateBitstreamBuffer(obsnv->nvenc_device,
&obsnv->nvenc_buffer_output);
return nvStatus;
}
void obs_nvenc_helper_fill_frame(struct obs_nvenc *obsnv,
struct encoder_frame *frame)
{
video_t *video = obs_encoder_video(obsnv->encoder);
const struct video_output_info *vid_info = video_output_get_info(video);
obsnv->nvenc_picture.inputWidth =
(int)obs_encoder_get_width(obsnv->encoder);
obsnv->nvenc_picture.inputHeight =
(int)obs_encoder_get_height(obsnv->encoder);
obsnv->nvenc_picture.inputPitch = obsnv->nvenc_picture.inputWidth;
//obsnv->nvenc_picture.encodePicFlags = ;
//obsnv->nvenc_picture.frameIdx = ;
obsnv->nvenc_picture.inputTimeStamp = frame->pts;
//obsnv->nvenc_picture.inputDuration = ;
obsnv->nvenc_picture.inputBuffer = frame->data[0];
if (vid_info->format == VIDEO_FORMAT_I420)
obsnv->nvenc_picture.bufferFmt = NV_ENC_BUFFER_FORMAT_IYUV_PL;
else if (vid_info->format == VIDEO_FORMAT_NV12)
obsnv->nvenc_picture.bufferFmt = NV_ENC_BUFFER_FORMAT_NV12_PL;
else
obsnv->nvenc_picture.bufferFmt = NV_ENC_BUFFER_FORMAT_UNDEFINED;
}
void obs_nvenc_helper_save_bitstream(struct obs_nvenc *obsnv,
struct encoder_packet *packet)
{
packet->data = obsnv->nvenc_buffer_output.bitstreamBuffer;
packet->size = obsnv->nvenc_buffer_output.size;
packet->type = OBS_ENCODER_VIDEO;
//packet->pts = obsnv->nvenc_buffer_output.bitstreamBuffer
//packet->dts = obsnv->nvenc_buffer_output
//packet->keyframe = obsnv->nvenc_buffer_output
}