forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathnvenc-cuda.c
More file actions
330 lines (275 loc) · 9.08 KB
/
nvenc-cuda.c
File metadata and controls
330 lines (275 loc) · 9.08 KB
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
#include "nvenc-internal.h"
#include "nvenc-helpers.h"
/*
* NVENC implementation using CUDA context and arrays
*/
/* ------------------------------------------------------------------------- */
/* CUDA Context management */
bool cuda_ctx_init(struct nvenc_data *enc, obs_data_t *settings, const bool texture)
{
#ifdef _WIN32
if (texture)
return true;
#endif
int count;
CUdevice device;
int gpu = (int)obs_data_get_int(settings, "device");
#ifndef _WIN32
/* CUDA can do fairly efficient cross-GPU OpenGL mappings, allow it as
* a hidden option for experimentation. */
bool force_cuda_tex = obs_data_get_bool(settings, "force_cuda_tex");
#endif
if (gpu == -1)
gpu = 0;
CU_FAILED(cu->cuInit(0))
CU_FAILED(cu->cuDeviceGetCount(&count))
if (!count) {
NV_FAIL("No CUDA devices found");
return false;
}
#ifdef _WIN32
CU_FAILED(cu->cuDeviceGet(&device, gpu))
#else
if (!texture || force_cuda_tex) {
CU_FAILED(cu->cuDeviceGet(&device, gpu))
} else {
unsigned int ctx_count = 0;
CUdevice devices[2];
obs_enter_graphics();
CUresult res = cu->cuGLGetDevices(&ctx_count, devices, 2, CU_GL_DEVICE_LIST_ALL);
obs_leave_graphics();
if (res != CUDA_SUCCESS || !ctx_count) {
/* Probably running on iGPU, should just fall back to
* non-texture encoder. */
if (res == CUDA_ERROR_INVALID_GRAPHICS_CONTEXT) {
info("Not running on NVIDIA GPU, falling back "
"to non-texture encoder");
} else {
const char *name, *desc;
if (cuda_get_error_desc(res, &name, &desc)) {
error("Failed to get a CUDA device for "
"the current OpenGL context: "
"%s: %s",
name, desc);
} else {
error("Failed to get a CUDA device for "
"the current OpenGL context: %d",
res);
}
}
return false;
}
/* Documentation indicates this should only ever happen with
* SLI, i.e. never for OBS. */
if (ctx_count > 1) {
warn("Got more than one CUDA devices for OpenGL context,"
" this is untested.");
}
device = devices[0];
debug("Loading up CUDA on device %u", device);
}
#endif
CU_FAILED(cu->cuCtxCreate(&enc->cu_ctx, 0, device))
CU_FAILED(cu->cuCtxPopCurrent(NULL))
return true;
}
void cuda_ctx_free(struct nvenc_data *enc)
{
if (enc->cu_ctx) {
cu->cuCtxPopCurrent(NULL);
cu->cuCtxDestroy(enc->cu_ctx);
}
}
/* ------------------------------------------------------------------------- */
/* CUDA Surface management */
static bool cuda_surface_init(struct nvenc_data *enc, struct nv_cuda_surface *nvsurf)
{
const bool p010 = obs_encoder_video_tex_active(enc->encoder, VIDEO_FORMAT_P010);
CUDA_ARRAY3D_DESCRIPTOR desc;
desc.Width = enc->cx;
desc.Height = enc->cy;
desc.Depth = 0;
desc.Flags = CUDA_ARRAY3D_SURFACE_LDST;
desc.NumChannels = 1;
if (!enc->non_texture) {
desc.Format = p010 ? CU_AD_FORMAT_UNSIGNED_INT16 : CU_AD_FORMAT_UNSIGNED_INT8;
desc.Height = enc->cy + enc->cy / 2;
} else {
switch (enc->surface_format) {
case NV_ENC_BUFFER_FORMAT_NV12:
desc.Format = CU_AD_FORMAT_UNSIGNED_INT8;
// Additional half-height plane for UV data
desc.Height += enc->cy / 2;
break;
case NV_ENC_BUFFER_FORMAT_YUV420_10BIT:
desc.Format = CU_AD_FORMAT_UNSIGNED_INT16; // 2 bytes per element
desc.Height += enc->cy / 2;
break;
case NV_ENC_BUFFER_FORMAT_YUV444:
desc.Format = CU_AD_FORMAT_UNSIGNED_INT8;
desc.Height *= 3; // 3 full-size planes
break;
default:
error("Unknown input format: %d", enc->surface_format);
return false;
}
}
CU_FAILED(cu->cuArray3DCreate(&nvsurf->tex, &desc))
NV_ENC_REGISTER_RESOURCE res = {0};
res.version = NV_ENC_REGISTER_RESOURCE_VER;
res.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDAARRAY;
res.resourceToRegister = (void *)nvsurf->tex;
res.width = enc->cx;
res.height = enc->cy;
res.pitch = (uint32_t)(desc.Width * desc.NumChannels);
if (!enc->non_texture) {
res.bufferFormat = p010 ? NV_ENC_BUFFER_FORMAT_YUV420_10BIT : NV_ENC_BUFFER_FORMAT_NV12;
} else {
res.bufferFormat = enc->surface_format;
}
if (NV_FAILED(nv.nvEncRegisterResource(enc->session, &res))) {
return false;
}
nvsurf->res = res.registeredResource;
nvsurf->mapped_res = NULL;
return true;
}
bool cuda_init_surfaces(struct nvenc_data *enc)
{
switch (enc->in_format) {
case VIDEO_FORMAT_P010:
enc->surface_format = NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
break;
case VIDEO_FORMAT_I444:
enc->surface_format = NV_ENC_BUFFER_FORMAT_YUV444;
break;
default:
enc->surface_format = NV_ENC_BUFFER_FORMAT_NV12;
}
da_reserve(enc->surfaces, enc->buf_count);
CU_FAILED(cu->cuCtxPushCurrent(enc->cu_ctx))
for (uint32_t i = 0; i < enc->buf_count; i++) {
struct nv_cuda_surface buf;
if (!cuda_surface_init(enc, &buf)) {
return false;
}
da_push_back(enc->surfaces, &buf);
}
CU_FAILED(cu->cuCtxPopCurrent(NULL))
return true;
}
static void cuda_surface_free(struct nvenc_data *enc, struct nv_cuda_surface *nvsurf)
{
if (nvsurf->res) {
if (enc->session && nvsurf->mapped_res) {
nv.nvEncUnmapInputResource(enc->session, nvsurf->mapped_res);
nvsurf->mapped_res = NULL;
}
if (enc->session)
nv.nvEncUnregisterResource(enc->session, nvsurf->res);
nvsurf->res = NULL;
cu->cuArrayDestroy(nvsurf->tex);
nvsurf->tex = NULL;
}
}
void cuda_free_surfaces(struct nvenc_data *enc)
{
if (!enc->cu_ctx)
return;
cu->cuCtxPushCurrent(enc->cu_ctx);
for (size_t i = 0; i < enc->surfaces.num; i++) {
cuda_surface_free(enc, &enc->surfaces.array[i]);
}
cu->cuCtxPopCurrent(NULL);
}
/* ------------------------------------------------------------------------- */
/* Actual encoding stuff */
static inline bool copy_frame(struct nvenc_data *enc, struct encoder_frame *frame, struct nv_cuda_surface *surf)
{
bool success = true;
size_t height = enc->cy;
size_t width = enc->cx;
CUDA_MEMCPY2D m = {0};
m.srcMemoryType = CU_MEMORYTYPE_HOST;
m.dstMemoryType = CU_MEMORYTYPE_ARRAY;
m.dstArray = surf->tex;
m.WidthInBytes = width;
m.Height = height;
CU_FAILED(cu->cuCtxPushCurrent(enc->cu_ctx))
if (enc->surface_format == NV_ENC_BUFFER_FORMAT_NV12) {
/* Page-locks the host memory so that it can be DMAd directly
* rather than CUDA doing an internal copy to page-locked
* memory before actually DMA-ing to the GPU. */
CU_CHECK(cu->cuMemHostRegister(frame->data[0], frame->linesize[0] * height, 0))
CU_CHECK(cu->cuMemHostRegister(frame->data[1], frame->linesize[1] * height / 2, 0))
m.srcPitch = frame->linesize[0];
m.srcHost = frame->data[0];
CU_FAILED(cu->cuMemcpy2D(&m))
m.srcPitch = frame->linesize[1];
m.srcHost = frame->data[1];
m.dstY += height;
m.Height /= 2;
CU_FAILED(cu->cuMemcpy2D(&m))
} else if (enc->surface_format == NV_ENC_BUFFER_FORMAT_YUV420_10BIT) {
CU_CHECK(cu->cuMemHostRegister(frame->data[0], frame->linesize[0] * height, 0))
CU_CHECK(cu->cuMemHostRegister(frame->data[1], frame->linesize[1] * height / 2, 0))
// P010 lines are double the size (16 bit per pixel)
m.WidthInBytes *= 2;
m.srcPitch = frame->linesize[0];
m.srcHost = frame->data[0];
CU_FAILED(cu->cuMemcpy2D(&m))
m.srcPitch = frame->linesize[1];
m.srcHost = frame->data[1];
m.dstY += height;
m.Height /= 2;
CU_FAILED(cu->cuMemcpy2D(&m))
} else { // I444
CU_CHECK(cu->cuMemHostRegister(frame->data[0], frame->linesize[0] * height, 0))
CU_CHECK(cu->cuMemHostRegister(frame->data[1], frame->linesize[1] * height, 0))
CU_CHECK(cu->cuMemHostRegister(frame->data[2], frame->linesize[2] * height, 0))
m.srcPitch = frame->linesize[0];
m.srcHost = frame->data[0];
CU_FAILED(cu->cuMemcpy2D(&m))
m.srcPitch = frame->linesize[1];
m.srcHost = frame->data[1];
m.dstY += height;
CU_FAILED(cu->cuMemcpy2D(&m))
m.srcPitch = frame->linesize[2];
m.srcHost = frame->data[2];
m.dstY += height;
CU_FAILED(cu->cuMemcpy2D(&m))
}
unmap:
if (frame->data[0])
cu->cuMemHostUnregister(frame->data[0]);
if (frame->data[1])
cu->cuMemHostUnregister(frame->data[1]);
if (frame->data[2])
cu->cuMemHostUnregister(frame->data[2]);
CU_FAILED(cu->cuCtxPopCurrent(NULL))
return success;
}
bool cuda_encode(void *data, struct encoder_frame *frame, struct encoder_packet *packet, bool *received_packet)
{
struct nvenc_data *enc = data;
struct nv_cuda_surface *surf;
struct nv_bitstream *bs;
bs = &enc->bitstreams.array[enc->next_bitstream];
surf = &enc->surfaces.array[enc->next_bitstream];
deque_push_back(&enc->dts_list, &frame->pts, sizeof(frame->pts));
/* ------------------------------------ */
/* copy to CUDA surface */
if (!copy_frame(enc, frame, surf))
return false;
/* ------------------------------------ */
/* map output tex so nvenc can use it */
NV_ENC_MAP_INPUT_RESOURCE map = {NV_ENC_MAP_INPUT_RESOURCE_VER};
map.registeredResource = surf->res;
map.mappedBufferFmt = enc->surface_format;
if (NV_FAILED(nv.nvEncMapInputResource(enc->session, &map)))
return false;
surf->mapped_res = map.mappedResource;
/* ------------------------------------ */
/* do actual encode call */
return nvenc_encode_base(enc, bs, surf->mapped_res, frame->pts, packet, received_packet);
}