+
+ if (hwctx->internal) {
+ if (hwctx->internal->is_allocated && hwctx->cuda_ctx) {
+ hwctx->internal->cuda_dl->cuCtxDestroy(hwctx->cuda_ctx);
+ hwctx->cuda_ctx = NULL;
+ }
+ cuda_free_functions(&hwctx->internal->cuda_dl);
+ }
+
+ av_freep(&hwctx->internal);
+}
+
+static int cuda_device_init(AVHWDeviceContext *ctx)
+{
+ AVCUDADeviceContext *hwctx = ctx->hwctx;
+ int ret;
+
+ if (!hwctx->internal) {
+ hwctx->internal = av_mallocz(sizeof(*hwctx->internal));
+ if (!hwctx->internal)
+ return AVERROR(ENOMEM);
+ }
+
+ if (!hwctx->internal->cuda_dl) {
+ ret = cuda_load_functions(&hwctx->internal->cuda_dl);
+ if (ret < 0) {
+ av_log(ctx, AV_LOG_ERROR, "Could not dynamically load CUDA\n");
+ goto error;
+ }
+ }
+
+ return 0;
+
+error:
+ cuda_device_uninit(ctx);
+ return ret;