- if (!hw_device_ctx) {
- hw_device_ctx = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_CUDA);
- if (!hw_device_ctx) {
- av_log(NULL, AV_LOG_ERROR, "av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_CUDA) failed\n");
- ret = AVERROR(ENOMEM);
- goto error;
- }
-
- err = cuInit(0);
- if (err != CUDA_SUCCESS) {
- av_log(NULL, AV_LOG_ERROR, "Could not initialize the CUDA driver API\n");
- ret = AVERROR_UNKNOWN;
- goto error;
- }
-
- err = cuDeviceGet(&device, 0); ///TODO: Make device index configurable
- if (err != CUDA_SUCCESS) {
- av_log(NULL, AV_LOG_ERROR, "Could not get the device number %d\n", 0);
- ret = AVERROR_UNKNOWN;
- goto error;
- }
-
- err = cuCtxCreate(&cuda_ctx, CU_CTX_SCHED_BLOCKING_SYNC, device);
- if (err != CUDA_SUCCESS) {
- av_log(NULL, AV_LOG_ERROR, "Error creating a CUDA context\n");
- ret = AVERROR_UNKNOWN;
- goto error;
- }
-
- device_ctx = (AVHWDeviceContext*)hw_device_ctx->data;
- device_ctx->free = cuvid_ctx_free;
-
- device_hwctx = device_ctx->hwctx;
- device_hwctx->cuda_ctx = cuda_ctx;
-
- err = cuCtxPopCurrent(&dummy);
- if (err != CUDA_SUCCESS) {
- av_log(NULL, AV_LOG_ERROR, "cuCtxPopCurrent failed\n");
- ret = AVERROR_UNKNOWN;
- goto error;
- }
-
- ret = av_hwdevice_ctx_init(hw_device_ctx);
- if (ret < 0) {
- av_log(NULL, AV_LOG_ERROR, "av_hwdevice_ctx_init failed\n");
+ if (!ctx->hw_frames_ctx) {
+ ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_CUDA,
+ ist->hwaccel_device, NULL, 0);
+ if (ret < 0)