Skip to content

Commit

Permalink
avcodec/nvenc: push cuda context before encoding a frame
Browse files Browse the repository at this point in the history
Thanks to Miroslav Slugeň for figuring out what was going on here.
  • Loading branch information
BtbN committed Feb 13, 2017
1 parent c4636f3 commit ba7563b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions libavcodec/nvenc.c
Expand Up @@ -1649,6 +1649,8 @@ int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *frame, int *got_packet)
{
NVENCSTATUS nv_status;
CUresult cu_res;
CUcontext dummy;
NvencSurface *tmpoutsurf, *inSurf;
int res;

Expand All @@ -1666,7 +1668,20 @@ int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
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);

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) {
inSurf->lockCount = 0;
return res;
Expand Down Expand Up @@ -1702,7 +1717,20 @@ int ff_nvenc_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
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);

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 &&
nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
return nvenc_print_error(avctx, nv_status, "EncodePicture failed!");
Expand Down

0 comments on commit ba7563b

Please sign in to comment.