--- libavutil/hwcontext_cuda.c.orig 2023-10-03 16:32:08.614514302 +0300 +++ libavutil/hwcontext_cuda.c 2023-10-03 17:09:03.570569066 +0300 @@ -288,9 +288,11 @@ CudaFunctions *cu = hwctx->internal->cuda_dl; if (hwctx->internal->is_allocated && hwctx->cuda_ctx) { +#ifdef CUDA_PRIMARY_CTX if (hwctx->internal->flags & AV_CUDA_USE_PRIMARY_CONTEXT) CHECK_CU(cu->cuDevicePrimaryCtxRelease(hwctx->internal->cuda_device)); else if (!(hwctx->internal->flags & AV_CUDA_USE_CURRENT_CONTEXT)) +#endif CHECK_CU(cu->cuCtxDestroy(hwctx->cuda_ctx)); hwctx->cuda_ctx = NULL; @@ -340,7 +342,7 @@ cu = hwctx->internal->cuda_dl; hwctx->internal->flags = flags; - +#ifdef CUDA_PRIMARY_CTX if (flags & AV_CUDA_USE_PRIMARY_CONTEXT) { ret = CHECK_CU(cu->cuDevicePrimaryCtxGetState(hwctx->internal->cuda_device, &dev_flags, &dev_active)); @@ -353,27 +355,33 @@ } else if (dev_flags != desired_flags) { ret = CHECK_CU(cu->cuDevicePrimaryCtxSetFlags(hwctx->internal->cuda_device, desired_flags)); + if (ret < 0) return ret; } + ret = CHECK_CU(cu->cuDevicePrimaryCtxRetain(&hwctx->cuda_ctx, hwctx->internal->cuda_device)); if (ret < 0) return ret; - } else if (flags & AV_CUDA_USE_CURRENT_CONTEXT) { + + } else if (flags & AV_CUDA_USE_CURRENT_CONTEXT) + + { ret = CHECK_CU(cu->cuCtxGetCurrent(&hwctx->cuda_ctx)); if (ret < 0) return ret; av_log(device_ctx, AV_LOG_INFO, "Using current CUDA context.\n"); } else { +#endif ret = CHECK_CU(cu->cuCtxCreate(&hwctx->cuda_ctx, desired_flags, hwctx->internal->cuda_device)); if (ret < 0) return ret; CHECK_CU(cu->cuCtxPopCurrent(&dummy)); - } +// } hwctx->internal->is_allocated = 1;