forked from audiohacked/obs-studio-plugin-nvenc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobs-nvenc.c
135 lines (108 loc) · 4.26 KB
/
obs-nvenc.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
/******************************************************************************
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"
/* ------------------------------------------------------------------------- */
static const char *obs_nvenc_getname(void)
{
return "NVENC";
}
static void *obs_nvenc_create(obs_data_t *settings, obs_encoder_t *encoder)
{
struct obs_nvenc *obsnv = bzalloc(sizeof(struct obs_nvenc));
obsnv->encoder = encoder;
obsnv->settings = settings;
obs_nvenc_helper_create_instance(obsnv);
obs_nvenc_helper_open_session(obsnv);
obs_nvenc_helper_select_codec(obsnv);
obs_nvenc_helper_get_preset(obsnv);
obs_nvenc_helper_init_encoder(obsnv);
obs_nvenc_helper_create_buffer(obsnv);
return obsnv;
}
static void obs_nvenc_destroy(void *data)
{
struct obs_nvenc *obsnv = data;
memset(&obsnv->nvenc_picture, 0, sizeof(obsnv->nvenc_picture));
obsnv->nvenc_picture.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
obsnv->api->nvEncEncodePicture(obsnv->nvenc_device, &obsnv->nvenc_picture);
if (obsnv) {
os_end_high_performance(obsnv->performance_token);
clear_data(obsnv);
da_free(obsnv->packet_data);
bfree(obsnv);
}
}
static bool obs_nvenc_encode(void *data, struct encoder_frame *frame,
struct encoder_packet *packet, bool *received_packet)
{
struct obs_nvenc *obsnv = data;
NVENCSTATUS nvStatus = NV_ENC_ERR_GENERIC;
if (!frame || !packet || !received_packet)
return false;
// initialize input buffer lock data structure
NV_ENC_LOCK_INPUT_BUFFER input_buffer_lock;
memset(&input_buffer_lock, 0, sizeof(NV_ENC_LOCK_INPUT_BUFFER));
SET_VER(input_buffer_lock, NV_ENC_LOCK_INPUT_BUFFER);
//initialize output bitstream buffer lock data structure
NV_ENC_LOCK_BITSTREAM output_buffer_lock;
memset(&output_buffer_lock, 0, sizeof(NV_ENC_LOCK_BITSTREAM));
SET_VER(output_buffer_lock, NV_ENC_LOCK_BITSTREAM);
//lock input buffer
input_buffer_lock.inputBuffer = &obsnv->nvenc_buffer_input;
obsnv->api->nvEncLockInputBuffer(obsnv->nvenc_device, &input_buffer_lock);
//fill data
obs_nvenc_helper_fill_frame(obsnv, frame);
//unlock input buffer
obsnv->api->nvEncUnlockInputBuffer(obsnv->nvenc_device,
obsnv->nvenc_buffer_input.inputBuffer);
// maybe set Per-Frame Encode parameters
// submit buffer for encoding
obsnv->nvenc_picture.inputBuffer = &obsnv->nvenc_buffer_input;
nvStatus = obsnv->api->nvEncEncodePicture(obsnv->nvenc_device,
&obsnv->nvenc_picture);
if (nvStatus != NV_ENC_SUCCESS) {
warn("encode failed");
return false;
}
//lock output bitstream buffer
output_buffer_lock.bitstreamBufferPtr = &obsnv->nvenc_buffer_output;
obsnv->api->nvEncLockBitstream(obsnv->nvenc_device, &output_buffer_lock);
//grab data
obs_nvenc_helper_save_bitstream(obsnv, packet);
//unlock output bitstream buffer
obsnv->api->nvEncUnlockBitstream(obsnv->nvenc_device,
obsnv->nvenc_buffer_output.bitstreamBuffer);
return false;
}
struct obs_encoder_info obs_nvenc_encoder = {
.id = "obs_nvenc",
.type = OBS_ENCODER_VIDEO,
.codec = "h264",
.get_name = obs_nvenc_getname,
.create = obs_nvenc_create,
.destroy = obs_nvenc_destroy,
.encode = obs_nvenc_encode,
.update = obs_nvenc_update,
.get_properties = obs_nvenc_get_properties,
.get_defaults = obs_nvenc_get_defaults,
.get_extra_data = obs_nvenc_get_extra_data,
.get_sei_data = obs_nvenc_get_sei_data,
.get_video_info = obs_nvenc_get_video_info
};