summaryrefslogtreecommitdiff
path: root/libavcodec
diff options
context:
space:
mode:
authorLuca Barbato <lu_zero@gentoo.org>2017-01-13 10:53:35 +0100
committerLuca Barbato <lu_zero@gentoo.org>2017-01-17 07:37:12 +0100
commitfb59f87ce72035b940c3f5045884098b9324e1b2 (patch)
treedc0e3b0f316f05ead00cdaaa8f81388980f835a9 /libavcodec
parent4795e4f61f993940c5384044caff56cc15078698 (diff)
nvenc: Explicitly push the cuda context on encoding
Make sure that NVENC does not misbehave if other cuda usages happen in the application.
Diffstat (limited to 'libavcodec')
-rw-r--r--libavcodec/nvenc.c7
-rw-r--r--libavcodec/nvenc.h2
2 files changed, 9 insertions, 0 deletions
diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c
index 4054e43eec..fe7b7f40f0 100644
--- a/libavcodec/nvenc.c
+++ b/libavcodec/nvenc.c
@@ -178,6 +178,7 @@ static av_cold int nvenc_load_libraries(AVCodecContext *avctx)
nvel->cu_device_compute_capability = cuDeviceComputeCapability;
nvel->cu_ctx_create = cuCtxCreate_v2;
nvel->cu_ctx_pop_current = cuCtxPopCurrent_v2;
+ nvel->cu_ctx_push_current = cuCtxPushCurrent_v2;
nvel->cu_ctx_destroy = cuCtxDestroy_v2;
#else
LOAD_LIBRARY(nvel->cuda, CUDA_LIBNAME);
@@ -190,6 +191,7 @@ static av_cold int nvenc_load_libraries(AVCodecContext *avctx)
"cuDeviceComputeCapability");
LOAD_SYMBOL(nvel->cu_ctx_create, nvel->cuda, "cuCtxCreate_v2");
LOAD_SYMBOL(nvel->cu_ctx_pop_current, nvel->cuda, "cuCtxPopCurrent_v2");
+ LOAD_SYMBOL(nvel->cu_ctx_push_current, nvel->cuda, "cuCtxPushCurrent_v2");
LOAD_SYMBOL(nvel->cu_ctx_destroy, nvel->cuda, "cuCtxDestroy_v2");
#endif
@@ -1522,9 +1524,11 @@ int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *frame, int *got_packet)
{
NVENCContext *ctx = avctx->priv_data;
+ NVENCLibraryContext *nvel = &ctx->nvel;
NV_ENCODE_API_FUNCTION_LIST *nv = &ctx->nvel.nvenc_funcs;
NV_ENC_PIC_PARAMS params = { 0 };
NVENCFrame *nvenc_frame = NULL;
+ CUcontext dummy;
int enc_ret, ret;
params.version = NV_ENC_PIC_PARAMS_VER;
@@ -1570,7 +1574,10 @@ int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
}
+ nvel->cu_ctx_push_current(ctx->cu_context);
enc_ret = nv->nvEncEncodePicture(ctx->nvenc_ctx, &params);
+ nvel->cu_ctx_pop_current(&dummy);
+
if (enc_ret != NV_ENC_SUCCESS &&
enc_ret != NV_ENC_ERR_NEED_MORE_INPUT)
return nvenc_print_error(avctx, enc_ret, "Error encoding the frame");
diff --git a/libavcodec/nvenc.h b/libavcodec/nvenc.h
index dfd03b5ebd..6df45480a9 100644
--- a/libavcodec/nvenc.h
+++ b/libavcodec/nvenc.h
@@ -66,6 +66,7 @@ typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
+typedef CUresult(CUDAAPI *PCUCTXPUSHCURRENT)(CUcontext ctx);
typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
typedef NVENCSTATUS (NVENCAPI *PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
@@ -84,6 +85,7 @@ typedef struct NVENCLibraryContext
PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
PCUCTXCREATE cu_ctx_create;
PCUCTXPOPCURRENT cu_ctx_pop_current;
+ PCUCTXPUSHCURRENT cu_ctx_push_current;
PCUCTXDESTROY cu_ctx_destroy;
NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;