Commit be74ba64 authored by Timo Rothenpieler's avatar Timo Rothenpieler

avcodec/nvenc: push cuda context before encoding a frame

Thanks to Miroslav Slugeň for figuring out what was going on here.
parent 965503d3
...@@ -1649,6 +1649,8 @@ int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, ...@@ -1649,6 +1649,8 @@ int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *frame, int *got_packet) const AVFrame *frame, int *got_packet)
{ {
NVENCSTATUS nv_status; NVENCSTATUS nv_status;
CUresult cu_res;
CUcontext dummy;
NvencSurface *tmpoutsurf, *inSurf; NvencSurface *tmpoutsurf, *inSurf;
int res; int res;
...@@ -1666,7 +1668,20 @@ int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, ...@@ -1666,7 +1668,20 @@ int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
return AVERROR_BUG; return AVERROR_BUG;
} }
cu_res = dl_fn->cuda_dl->cuCtxPushCurrent(ctx->cu_context);
if (cu_res != CUDA_SUCCESS) {
av_log(avctx, AV_LOG_ERROR, "cuCtxPushCurrent failed\n");
return AVERROR_EXTERNAL;
}
res = nvenc_upload_frame(avctx, frame, inSurf); res = nvenc_upload_frame(avctx, frame, inSurf);
cu_res = dl_fn->cuda_dl->cuCtxPopCurrent(&dummy);
if (cu_res != CUDA_SUCCESS) {
av_log(avctx, AV_LOG_ERROR, "cuCtxPopCurrent failed\n");
return AVERROR_EXTERNAL;
}
if (res) { if (res) {
inSurf->lockCount = 0; inSurf->lockCount = 0;
return res; return res;
...@@ -1702,7 +1717,20 @@ int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt, ...@@ -1702,7 +1717,20 @@ int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS; pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
} }
cu_res = dl_fn->cuda_dl->cuCtxPushCurrent(ctx->cu_context);
if (cu_res != CUDA_SUCCESS) {
av_log(avctx, AV_LOG_ERROR, "cuCtxPushCurrent failed\n");
return AVERROR_EXTERNAL;
}
nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params); nv_status = p_nvenc->nvEncEncodePicture(ctx->nvencoder, &pic_params);
cu_res = dl_fn->cuda_dl->cuCtxPopCurrent(&dummy);
if (cu_res != CUDA_SUCCESS) {
av_log(avctx, AV_LOG_ERROR, "cuCtxPopCurrent failed\n");
return AVERROR_EXTERNAL;
}
if (nv_status != NV_ENC_SUCCESS && if (nv_status != NV_ENC_SUCCESS &&
nv_status != NV_ENC_ERR_NEED_MORE_INPUT) nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
return nvenc_print_error(avctx, nv_status, "EncodePicture failed!"); return nvenc_print_error(avctx, nv_status, "EncodePicture failed!");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment