From patchwork Sun Nov 12 09:30:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hendrik Leppkes X-Patchwork-Id: 6008 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.161.94 with SMTP id m30csp914387jah; Sun, 12 Nov 2017 01:38:25 -0800 (PST) X-Google-Smtp-Source: AGs4zMbt6TlGx5JwVoRlE5LIXemlYDMnE8ConPZfF3Njt2yEPatS8b6Iu2oBKnGmmmhgxaveHykp X-Received: by 10.28.63.77 with SMTP id m74mr4190958wma.117.1510479505020; Sun, 12 Nov 2017 01:38:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510479504; cv=none; d=google.com; s=arc-20160816; b=jIH4Jzi8AnhhqbM0oZ2PK5zfJB8E+iQncp7gQVDVYjp15bC0UWJ+uqNeWUROMFa4sV gMFDuG3Yax85MXOjJz7z+nK+BjCej/+/tbf6c6/62qpJN1q3lQ4rPJPVK9Jcx0xEzMQu whYo2MK5iUea1IXzZW67qatK9RCCyz5jsN2cAVv4mFZA1wKYbEhcX9FFNY/dUlZYGTtO XW1oNIhBs+aTEmCuC+ClgfIbL71INk4WwJDKXwiKbOiQ9zK02sJdTMrf+5cS8ORQD+Wl 3haPOP+Wx3q+oxRo+fW2ERHEuzpvsIERrPdNPsxm6lByvRmwigKEraMNxvU5swcHnw4F ObuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=vBPBbe9EjH2MscXZveaq2ELvz0S+vFZjeoo9s+dFLF0=; b=JlFPijCan5flZhreAhItvREvi55TNzzpGbTt0MUReM5CjeUTGtzbXQxtgSVSjDZdy5 R4101+AwvHZ2zQfTG2nGPZgjkNTTB6j3e2GHC80cVyNjwEeEI3F+zsbDutXL7tNRiqEp YID3jmUUTpm9dk/LLQwluqz6MbPTuabyO19V0z6vBf5Ru0yKl61642K29dXAGJrMkAye OnA/V47mkIVXBUopO/Oy/xgkO2fdrx9r083UeON32fHyIis47+9dOU/NO88ZlAeSmsvy srk/I6MmCuMGxbdMYEEfvyfTdjWYO1aWuEmMimSbDmy3HIXJCKCjSUotEsoJDaegbkOF smnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=T/HSCLql; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id c38si6555089wrc.538.2017.11.12.01.38.24; Sun, 12 Nov 2017 01:38:24 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=T/HSCLql; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8CBDC68A35E; Sun, 12 Nov 2017 11:38:09 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f68.google.com (mail-wm0-f68.google.com [74.125.82.68]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3476068A065 for ; Sun, 12 Nov 2017 11:38:03 +0200 (EET) Received: by mail-wm0-f68.google.com with SMTP id n74so10543658wmi.1 for ; Sun, 12 Nov 2017 01:38:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=5VTvk6Lx4XWuK0nGPjVdMQBEwxSFCQj1BXQ4OjTRGuQ=; b=T/HSCLqlOPWOT6RpwoGgOYXEcLGCXmXYMKqclkDLbcqRglpDZrbXcNiOvETto72EQG Gfddf2GONL8kqFOdbciE136dvi4BzMeEyZ3L1F7jccGeaPq/iypvcuYztQ0CFcVVycvQ Az9ll0zksD1xk2Iu39R40PzfDn+piY1vD1poh3BmNVZLt1f30DeNBTVVN6A+WgpJyu3Y JC+X3UvKGPdx8ofPkBlT9cWGlEglNY2tCmbOEdd93f/LxveAP5X2Zrac6zVd1SZoJqe2 vY2SgR9tAD17AYW2ofVKq9uCBld7FslmSalJNbnwjssYrtR7Fi+IJRugVncW0X8i6zVs P+BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=5VTvk6Lx4XWuK0nGPjVdMQBEwxSFCQj1BXQ4OjTRGuQ=; b=PH0VB6cX/KblgSnZz0SG1AHYNIBTgkRElYE5LQB5yBr6Sm7x8ze26nzAPRLFGEhdMY ujsuqAei+hLNbYNoozG10qx39VN0cUvLFFmdvpEbUvJbzJ80i8INWrlT2j7Vy0BQwhV3 11iq+INwmNAmRv/yzdyHATAyJAxeKcp0Gpn41xmk4t4CZX+leiwU30qa46jOqUBtRBXG YTrhjAZT41Wrc4jEuGpKWzXqCfFhseqTtguSszel1gRPAkMYKmlQtwPp1An+FEZOB2vv M+WJhvCCxQPYCkz7qeG93fQI3c9A0c7j30UgPaMknV99GaLjsSVzSUcdjJPISRldCtpg 8CxA== X-Gm-Message-State: AJaThX70TTEFixYhjB4YfxCrMtSzIXa41kMu/82//DgdM5eeCwQe+hrC 8PJJrUg37FbxqO9kLH3TD3SJo1SE X-Received: by 10.28.74.193 with SMTP id n62mr4293307wmi.61.1510479023511; Sun, 12 Nov 2017 01:30:23 -0800 (PST) Received: from localhost (p4FC4F36E.dip0.t-ipconnect.de. [79.196.243.110]) by smtp.gmail.com with ESMTPSA id m187sm10860460wmb.17.2017.11.12.01.30.22 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 12 Nov 2017 01:30:22 -0800 (PST) From: Hendrik Leppkes To: ffmpeg-devel@ffmpeg.org Date: Sun, 12 Nov 2017 10:30:20 +0100 Message-Id: <20171112093021.23484-1-h.leppkes@gmail.com> X-Mailer: git-send-email 2.13.2.windows.1 Subject: [FFmpeg-devel] [PATCH 1/2] nvenc: factor context push/pop into functions X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This reduces code repetition, and will allow adding further push/pop refinement for D3D11 devices in future commits. --- libavcodec/nvenc.c | 171 ++++++++++++++++++++++++----------------------------- 1 file changed, 76 insertions(+), 95 deletions(-) diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c index e1d3316de3..c685d973c1 100644 --- a/libavcodec/nvenc.c +++ b/libavcodec/nvenc.c @@ -166,6 +166,37 @@ static av_cold int nvenc_load_libraries(AVCodecContext *avctx) return 0; } +static int nvenc_push_context(AVCodecContext *avctx) +{ + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; + CUresult cu_res; + + 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; + } + + return 0; +} + +static int nvenc_pop_context(AVCodecContext *avctx) +{ + NvencContext *ctx = avctx->priv_data; + NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; + CUresult cu_res; + CUcontext dummy; + + 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; + } + + return 0; +} + static av_cold int nvenc_open_session(AVCodecContext *avctx) { NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 }; @@ -335,7 +366,6 @@ static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx) int major, minor, ret; CUresult cu_res; CUdevice cu_device; - CUcontext dummy; int loglevel = AV_LOG_VERBOSE; if (ctx->device == LIST_DEVICES) @@ -378,11 +408,8 @@ static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx) ctx->cu_context = ctx->cu_context_internal; - cu_res = dl_fn->cuda_dl->cuCtxPopCurrent(&dummy); - if (cu_res != CUDA_SUCCESS) { - av_log(avctx, AV_LOG_FATAL, "Failed popping CUDA context: 0x%x\n", (int)cu_res); + if ((ret = nvenc_pop_context(avctx)) < 0) goto fail2; - } if ((ret = nvenc_open_session(avctx)) < 0) goto fail2; @@ -398,20 +425,14 @@ static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx) return 0; fail3: - 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; - } + if ((ret = nvenc_push_context(avctx)) < 0) + return ret; p_nvenc->nvEncDestroyEncoder(ctx->nvencoder); ctx->nvencoder = NULL; - 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 ((ret = nvenc_pop_context(avctx)) < 0) + return ret; fail2: dl_fn->cuda_dl->cuCtxDestroy(ctx->cu_context_internal); @@ -1031,8 +1052,6 @@ static av_cold int nvenc_setup_encoder(AVCodecContext *avctx) NV_ENC_PRESET_CONFIG preset_config = { 0 }; NVENCSTATUS nv_status = NV_ENC_SUCCESS; AVCPBProperties *cpb_props; - CUresult cu_res; - CUcontext dummy; int res = 0; int dw, dh; @@ -1123,19 +1142,15 @@ static av_cold int nvenc_setup_encoder(AVCodecContext *avctx) if (res) return res; - 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_push_context(avctx); + if (res < 0) + return res; nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_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; - } + res = nvenc_pop_context(avctx); + if (res < 0) + return res; if (nv_status != NV_ENC_SUCCESS) { return nvenc_print_error(avctx, nv_status, "InitializeEncoder failed"); @@ -1239,9 +1254,6 @@ static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx) static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx) { NvencContext *ctx = avctx->priv_data; - NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; - CUresult cu_res; - CUcontext dummy; int i, res; ctx->surfaces = av_mallocz_array(ctx->nb_surfaces, sizeof(*ctx->surfaces)); @@ -1263,29 +1275,21 @@ static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx) if (!ctx->output_surface_ready_queue) return AVERROR(ENOMEM); - 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_push_context(avctx); + if (res < 0) + return res; for (i = 0; i < ctx->nb_surfaces; i++) { if ((res = nvenc_alloc_surface(avctx, i)) < 0) { - 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; - } + nvenc_pop_context(avctx); return res; } } - 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; - } + res = nvenc_pop_context(avctx); + if (res < 0) + return res; return 0; } @@ -1328,20 +1332,16 @@ av_cold int ff_nvenc_encode_close(AVCodecContext *avctx) NvencContext *ctx = avctx->priv_data; NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs; - CUresult cu_res; - CUcontext dummy; - int i; + int i, res; /* the encoder has to be flushed before it can be closed */ if (ctx->nvencoder) { NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER, .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; - } + res = nvenc_push_context(avctx); + if (res < 0) + return res; p_nvenc->nvEncEncodePicture(ctx->nvencoder, ¶ms); } @@ -1378,11 +1378,9 @@ av_cold int ff_nvenc_encode_close(AVCodecContext *avctx) if (ctx->nvencoder) { p_nvenc->nvEncDestroyEncoder(ctx->nvencoder); - 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; - } + res = nvenc_pop_context(avctx); + if (res < 0) + return res; } ctx->nvencoder = NULL; @@ -1810,10 +1808,8 @@ static int output_ready(AVCodecContext *avctx, int flush) int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) { NVENCSTATUS nv_status; - CUresult cu_res; - CUcontext dummy; NvencSurface *tmp_out_surf, *in_surf; - int res; + int res, res2; NvencContext *ctx = avctx->priv_data; NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; @@ -1833,19 +1829,15 @@ int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) if (!in_surf) return AVERROR(EAGAIN); - 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_push_context(avctx); + if (res < 0) + return res; res = nvenc_upload_frame(avctx, frame, in_surf); - 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; - } + res2 = nvenc_pop_context(avctx); + if (res2 < 0) + return res2; if (res) return res; @@ -1881,19 +1873,15 @@ int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) ctx->encoder_flushing = 1; } - 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_push_context(avctx); + if (res < 0) + return res; 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; - } + res = nvenc_pop_context(avctx); + if (res < 0) + return res; if (nv_status != NV_ENC_SUCCESS && nv_status != NV_ENC_ERR_NEED_MORE_INPUT) @@ -1922,13 +1910,10 @@ int ff_nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt) { - CUresult cu_res; - CUcontext dummy; NvencSurface *tmp_out_surf; - int res; + int res, res2; NvencContext *ctx = avctx->priv_data; - NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs; if (!ctx->cu_context || !ctx->nvencoder) return AVERROR(EINVAL); @@ -1936,19 +1921,15 @@ int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt) if (output_ready(avctx, ctx->encoder_flushing)) { av_fifo_generic_read(ctx->output_surface_ready_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL); - 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_push_context(avctx); + if (res < 0) + return res; res = process_output_surface(avctx, pkt, tmp_out_surf); - 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; - } + res2 = nvenc_pop_context(avctx); + if (res2 < 0) + return res2; if (res) return res;