diff options
Diffstat (limited to 'src/capture/nvfbc.c')
-rw-r--r-- | src/capture/nvfbc.c | 116 |
1 files changed, 38 insertions, 78 deletions
diff --git a/src/capture/nvfbc.c b/src/capture/nvfbc.c index 9eabb18..97e0283 100644 --- a/src/capture/nvfbc.c +++ b/src/capture/nvfbc.c @@ -3,20 +3,18 @@ #include "../../include/cuda.h" #include "../../include/egl.h" #include "../../include/utils.h" +#include "../../include/color_conversion.h" + #include <dlfcn.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <math.h> + #include <X11/Xlib.h> -#include <libavutil/hwcontext.h> -#include <libavutil/hwcontext_cuda.h> -#include <libavutil/frame.h> -#include <libavutil/version.h> #include <libavcodec/avcodec.h> typedef struct { - gsr_capture_base base; gsr_capture_nvfbc_params params; void *library; @@ -43,13 +41,6 @@ typedef struct { double nvfbc_dead_start; } gsr_capture_nvfbc; -#if defined(_WIN64) || defined(__LP64__) -typedef unsigned long long CUdeviceptr_v2; -#else -typedef unsigned int CUdeviceptr_v2; -#endif -typedef CUdeviceptr_v2 CUdeviceptr; - static int max_int(int a, int b) { return a > b ? a : b; } @@ -301,14 +292,26 @@ static int gsr_capture_nvfbc_setup_session(gsr_capture_nvfbc *cap_nvfbc) { return 0; } +static void gsr_capture_nvfbc_stop(gsr_capture_nvfbc *cap_nvfbc) { + gsr_capture_nvfbc_destroy_session_and_handle(cap_nvfbc); + gsr_cuda_unload(&cap_nvfbc->cuda); + if(cap_nvfbc->library) { + dlclose(cap_nvfbc->library); + cap_nvfbc->library = NULL; + } + if(cap_nvfbc->params.display_to_capture) { + free((void*)cap_nvfbc->params.display_to_capture); + cap_nvfbc->params.display_to_capture = NULL; + } +} + static int gsr_capture_nvfbc_start(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame *frame) { gsr_capture_nvfbc *cap_nvfbc = cap->priv; - cap_nvfbc->base.video_codec_context = video_codec_context; - cap_nvfbc->base.egl = cap_nvfbc->params.egl; - - if(!gsr_cuda_load(&cap_nvfbc->cuda, cap_nvfbc->params.egl->x11.dpy, cap_nvfbc->params.overclock)) - return -1; + if(!cap_nvfbc->params.use_software_video_encoder) { + if(!gsr_cuda_load(&cap_nvfbc->cuda, cap_nvfbc->params.egl->x11.dpy, cap_nvfbc->params.overclock)) + return -1; + } if(!gsr_capture_nvfbc_load_library(cap)) { gsr_cuda_unload(&cap_nvfbc->cuda); @@ -323,15 +326,14 @@ static int gsr_capture_nvfbc_start(gsr_capture *cap, AVCodecContext *video_codec cap_nvfbc->capture_region = (cap_nvfbc->x > 0 || cap_nvfbc->y > 0 || cap_nvfbc->width > 0 || cap_nvfbc->height > 0); cap_nvfbc->supports_direct_cursor = false; - bool direct_capture = cap_nvfbc->params.direct_capture; int driver_major_version = 0; int driver_minor_version = 0; - if(direct_capture && get_driver_version(&driver_major_version, &driver_minor_version)) { + if(cap_nvfbc->params.direct_capture && get_driver_version(&driver_major_version, &driver_minor_version)) { fprintf(stderr, "Info: detected nvidia version: %d.%d\n", driver_major_version, driver_minor_version); // TODO: if(version_at_least(driver_major_version, driver_minor_version, 515, 57) && version_less_than(driver_major_version, driver_minor_version, 520, 56)) { - direct_capture = false; + cap_nvfbc->params.direct_capture = false; fprintf(stderr, "Warning: \"screen-direct\" has temporary been disabled as it causes stuttering with driver versions >= 515.57 and < 520.56. Please update your driver if possible. Capturing \"screen\" instead.\n"); } @@ -340,7 +342,7 @@ static int gsr_capture_nvfbc_start(gsr_capture *cap, AVCodecContext *video_codec /* if(direct_capture) { if(version_at_least(driver_major_version, driver_minor_version, 515, 57)) - supports_direct_cursor = true; + cap_nvfbc->supports_direct_cursor = true; else fprintf(stderr, "Info: capturing \"screen-direct\" but driver version appears to be less than 515.57. Disabling capture of cursor. Please update your driver if you want to capture your cursor or record \"screen\" instead.\n"); } @@ -356,42 +358,27 @@ static int gsr_capture_nvfbc_start(gsr_capture *cap, AVCodecContext *video_codec } if(cap_nvfbc->capture_region) { - video_codec_context->width = cap_nvfbc->width & ~1; - video_codec_context->height = cap_nvfbc->height & ~1; + video_codec_context->width = FFALIGN(cap_nvfbc->width, 2); + video_codec_context->height = FFALIGN(cap_nvfbc->height, 2); } else { - video_codec_context->width = cap_nvfbc->tracking_width & ~1; - video_codec_context->height = cap_nvfbc->tracking_height & ~1; + video_codec_context->width = FFALIGN(cap_nvfbc->tracking_width, 2); + video_codec_context->height = FFALIGN(cap_nvfbc->tracking_height, 2); } frame->width = video_codec_context->width; frame->height = video_codec_context->height; - if(!cuda_create_codec_context(cap_nvfbc->cuda.cu_ctx, video_codec_context, video_codec_context->width, video_codec_context->height, false, &cap_nvfbc->cuda_stream)) - goto error_cleanup; - - gsr_cuda_context cuda_context = { - .cuda = &cap_nvfbc->cuda, - .cuda_graphics_resources = cap_nvfbc->cuda_graphics_resources, - .mapped_arrays = cap_nvfbc->mapped_arrays - }; - - // TODO: Remove this, it creates shit we dont need - if(!gsr_capture_base_setup_cuda_textures(&cap_nvfbc->base, frame, &cuda_context, cap_nvfbc->params.color_range, GSR_SOURCE_COLOR_BGR, cap_nvfbc->params.hdr)) { - goto error_cleanup; - } /* Disable vsync */ set_vertical_sync_enabled(cap_nvfbc->params.egl, 0); return 0; error_cleanup: - gsr_capture_nvfbc_destroy_session_and_handle(cap_nvfbc); - gsr_capture_base_stop(&cap_nvfbc->base); - gsr_cuda_unload(&cap_nvfbc->cuda); + gsr_capture_nvfbc_stop(cap_nvfbc); return -1; } -static int gsr_capture_nvfbc_capture(gsr_capture *cap, AVFrame *frame) { +static int gsr_capture_nvfbc_capture(gsr_capture *cap, AVFrame *frame, gsr_color_conversion *color_conversion) { gsr_capture_nvfbc *cap_nvfbc = cap->priv; const double nvfbc_recreate_retry_time_seconds = 1.0; @@ -438,54 +425,25 @@ static int gsr_capture_nvfbc_capture(gsr_capture *cap, AVFrame *frame) { //cap_nvfbc->params.egl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f); cap_nvfbc->params.egl->glClear(0); - gsr_color_conversion_draw(&cap_nvfbc->base.color_conversion, cap_nvfbc->setup_params.dwTextures[grab_params.dwTextureIndex], + gsr_color_conversion_draw(color_conversion, cap_nvfbc->setup_params.dwTextures[grab_params.dwTextureIndex], (vec2i){0, 0}, (vec2i){frame->width, frame->height}, (vec2i){0, 0}, (vec2i){frame->width, frame->height}, 0.0f, false); cap_nvfbc->params.egl->glXSwapBuffers(cap_nvfbc->params.egl->x11.dpy, cap_nvfbc->params.egl->x11.window); - // TODO: HDR is broken - const int div[2] = {1, 2}; // divide UV texture size by 2 because chroma is half size - for(int i = 0; i < 2; ++i) { - CUDA_MEMCPY2D memcpy_struct; - memcpy_struct.srcXInBytes = 0; - memcpy_struct.srcY = 0; - memcpy_struct.srcMemoryType = CU_MEMORYTYPE_ARRAY; - - memcpy_struct.dstXInBytes = 0; - memcpy_struct.dstY = 0; - memcpy_struct.dstMemoryType = CU_MEMORYTYPE_DEVICE; - - memcpy_struct.srcArray = cap_nvfbc->mapped_arrays[i]; - memcpy_struct.srcPitch = frame->width / div[i]; - memcpy_struct.dstDevice = (CUdeviceptr)frame->data[i]; - memcpy_struct.dstPitch = frame->linesize[i]; - memcpy_struct.WidthInBytes = frame->width * (cap_nvfbc->params.hdr ? 2 : 1); - memcpy_struct.Height = frame->height / div[i]; - // TODO: Remove this copy if possible - cap_nvfbc->cuda.cuMemcpy2DAsync_v2(&memcpy_struct, cap_nvfbc->cuda_stream); - } - - // TODO: needed? - cap_nvfbc->cuda.cuStreamSynchronize(cap_nvfbc->cuda_stream); - return 0; } +static gsr_source_color gsr_capture_nvfbc_get_source_color(gsr_capture *cap) { + (void)cap; + return GSR_SOURCE_COLOR_BGR; +} + static void gsr_capture_nvfbc_destroy(gsr_capture *cap, AVCodecContext *video_codec_context) { (void)video_codec_context; gsr_capture_nvfbc *cap_nvfbc = cap->priv; - gsr_capture_nvfbc_destroy_session_and_handle(cap_nvfbc); - if(cap_nvfbc) { - gsr_capture_base_stop(&cap_nvfbc->base); - gsr_cuda_unload(&cap_nvfbc->cuda); - dlclose(cap_nvfbc->library); - free((void*)cap_nvfbc->params.display_to_capture); - cap_nvfbc->params.display_to_capture = NULL; - free(cap->priv); - cap->priv = NULL; - } + gsr_capture_nvfbc_stop(cap_nvfbc); free(cap); } @@ -527,6 +485,8 @@ gsr_capture* gsr_capture_nvfbc_create(const gsr_capture_nvfbc_params *params) { .should_stop = NULL, .capture = gsr_capture_nvfbc_capture, .capture_end = NULL, + .get_source_color = gsr_capture_nvfbc_get_source_color, + .uses_external_image = NULL, .destroy = gsr_capture_nvfbc_destroy, .priv = cap_nvfbc }; |