From patchwork Fri Feb 10 12:35:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wm4 X-Patchwork-Id: 2480 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp473981vsb; Fri, 10 Feb 2017 04:36:45 -0800 (PST) X-Received: by 10.28.35.142 with SMTP id j136mr25224726wmj.11.1486730205140; Fri, 10 Feb 2017 04:36:45 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id r1si2004469wra.91.2017.02.10.04.36.44; Fri, 10 Feb 2017 04:36:45 -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=@googlemail.com; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 73453689DD8; Fri, 10 Feb 2017 14:35:59 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f194.google.com (mail-wr0-f194.google.com [209.85.128.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A96E0689DCB for ; Fri, 10 Feb 2017 14:35:58 +0200 (EET) Received: by mail-wr0-f194.google.com with SMTP id i10so15021159wrb.0 for ; Fri, 10 Feb 2017 04:36:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=TvYtHaf2g+80giVBwjhQP1VcMl4kJMGAV+j3KZ06+lY=; b=pcVivxh3LkDLtuqi6zpZPNS67HCyGSnYVhznMFHFECb+RiWtFpyOjR618trRLba57n hHcq80l8rgl4OwWiKQzWNRf5oaiguogt+DDQSd/OI3D2TvDD6e8L/7NPzCEXdXtxnPgJ pHNtHnu2ixQHPmiMyNsDXywDk4kZ6i61Lw7BnzcD+q03wVx752VJXHTptNgbCUuN+BQu QZR5N68sSKEkOcqX/+c1gD3k4QO32R0viM9ijWdRL1mlPt70Hi31gslxHjcD0dTJdpjY eHFTjx2UGv2R+F2UiiaOBXIRg9KvtawEuVcSe2ST0F7GwAkML9Ijn8PqH4jwL9lqp8ac qzBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=TvYtHaf2g+80giVBwjhQP1VcMl4kJMGAV+j3KZ06+lY=; b=WQGln8AaBteCkF6VhDThnsRq6yOcjkR70g3o4/Bj0ldvKFqpe5xi5uc42HS4z44RPx gR7A6mS6F870Qf9Zd8dUxk9vZgDK7UlmSzxzUvl5czgDtYvLTWI2b+8hP2kBpQnTFGIP LkAnMRsPHfUI8lMsymX1v27dCC1coOHkFJQ9xcGNfzryffPdJMGAFoLaJXWz9Nu3SYmE gn79atde0wsWEuDwJPepI1hUqncb3lrb+cE0QrvCdAuEURnTZ6/xyXwRjHSPJwXX6ECe pPlUPQdDCNOPb33ItPgErKj4h6YEB6/675N4Ld2ZHizhKLjSddpzPOApsM/F9HU9L8g+ zIcA== X-Gm-Message-State: AMke39kahc2ImJS5WTl1dJvioLtfJl4JKuGjDv28FTnzbK1wZMut5S89VOs73OvYSheUxw== X-Received: by 10.223.139.213 with SMTP id w21mr7776142wra.108.1486730161991; Fri, 10 Feb 2017 04:36:01 -0800 (PST) Received: from localhost.localdomain (p4FF029FA.dip0.t-ipconnect.de. [79.240.41.250]) by smtp.googlemail.com with ESMTPSA id s18sm1413656wmb.18.2017.02.10.04.35.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Feb 2017 04:36:01 -0800 (PST) From: wm4 To: ffmpeg-devel@ffmpeg.org Date: Fri, 10 Feb 2017 13:35:39 +0100 Message-Id: <20170210123541.32375-8-nfxjfg@googlemail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170210123541.32375-1-nfxjfg@googlemail.com> References: <20170210123541.32375-1-nfxjfg@googlemail.com> Subject: [FFmpeg-devel] [PATCH 7/9] ffmpeg_cuvid: adapt for recent filter graph initialization changes 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 Cc: Timo Rothenpieler MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Timo Rothenpieler --- ffmpeg.c | 13 ------ ffmpeg.h | 1 - ffmpeg_cuvid.c | 141 ++++++++++++--------------------------------------------- 3 files changed, 30 insertions(+), 125 deletions(-) diff --git a/ffmpeg.c b/ffmpeg.c index 97d0946b6b..eeb3d210b0 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -3491,19 +3491,6 @@ static int transcode_init(void) input_streams[j + ifile->ist_index]->start = av_gettime_relative(); } - /* hwaccel transcoding */ - for (i = 0; i < nb_output_streams; i++) { - ost = output_streams[i]; - - if (!ost->stream_copy) { - -#if CONFIG_CUVID - if (cuvid_transcode_init(ost)) - exit_program(1); -#endif - } - } - /* init input streams */ for (i = 0; i < nb_input_streams; i++) if ((ret = init_input_stream(i, error, sizeof(error))) < 0) { diff --git a/ffmpeg.h b/ffmpeg.h index 7a807e1ab9..6ede1dcd7b 100644 --- a/ffmpeg.h +++ b/ffmpeg.h @@ -646,6 +646,5 @@ int qsv_init(AVCodecContext *s); int vaapi_decode_init(AVCodecContext *avctx); int vaapi_device_init(const char *device); int cuvid_init(AVCodecContext *s); -int cuvid_transcode_init(OutputStream *ost); #endif /* FFMPEG_H */ diff --git a/ffmpeg_cuvid.c b/ffmpeg_cuvid.c index 46540795d4..3ff3b40f17 100644 --- a/ffmpeg_cuvid.c +++ b/ffmpeg_cuvid.c @@ -17,138 +17,57 @@ */ #include "libavutil/hwcontext.h" +#include "libavutil/pixdesc.h" #include "ffmpeg.h" -typedef struct CUVIDContext { - AVBufferRef *hw_frames_ctx; -} CUVIDContext; - static void cuvid_uninit(AVCodecContext *avctx) { - InputStream *ist = avctx->opaque; - CUVIDContext *ctx = ist->hwaccel_ctx; - - if (ctx) { - av_buffer_unref(&ctx->hw_frames_ctx); - av_freep(&ctx); - } - + InputStream *ist = avctx->opaque; av_buffer_unref(&ist->hw_frames_ctx); - - ist->hwaccel_ctx = 0; - ist->hwaccel_uninit = 0; } int cuvid_init(AVCodecContext *avctx) { - InputStream *ist = avctx->opaque; - CUVIDContext *ctx = ist->hwaccel_ctx; - - av_log(NULL, AV_LOG_TRACE, "Initializing cuvid hwaccel\n"); - - if (!ctx) { - av_log(NULL, AV_LOG_ERROR, "CUVID transcoding is not initialized. " - "-hwaccel cuvid should only be used for one-to-one CUVID transcoding " - "with no (software) filters.\n"); - return AVERROR(EINVAL); - } - - return 0; -} - -int cuvid_transcode_init(OutputStream *ost) -{ - InputStream *ist; - const enum AVPixelFormat *pix_fmt; - AVHWFramesContext *hwframe_ctx; - AVBufferRef *device_ref = NULL; - CUVIDContext *ctx = NULL; - int ret = 0; - - av_log(NULL, AV_LOG_TRACE, "Initializing cuvid transcoding\n"); + InputStream *ist = avctx->opaque; + AVHWFramesContext *frames_ctx; + int ret; - if (ost->source_index < 0) - return 0; + av_log(avctx, AV_LOG_VERBOSE, "Initializing cuvid hwaccel\n"); - ist = input_streams[ost->source_index]; - - /* check if the encoder supports CUVID */ - if (!ost->enc->pix_fmts) - goto cancel; - for (pix_fmt = ost->enc->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) - if (*pix_fmt == AV_PIX_FMT_CUDA) - break; - if (*pix_fmt == AV_PIX_FMT_NONE) - goto cancel; - - /* check if the decoder supports CUVID */ - if (ist->hwaccel_id != HWACCEL_CUVID || !ist->dec || !ist->dec->pix_fmts) - goto cancel; - for (pix_fmt = ist->dec->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) - if (*pix_fmt == AV_PIX_FMT_CUDA) - break; - if (*pix_fmt == AV_PIX_FMT_NONE) - goto cancel; - - av_log(NULL, AV_LOG_VERBOSE, "Setting up CUVID transcoding\n"); - - if (ist->hwaccel_ctx) { - ctx = ist->hwaccel_ctx; - } else { - ctx = av_mallocz(sizeof(*ctx)); - if (!ctx) { - ret = AVERROR(ENOMEM); - goto error; + if (!hw_device_ctx) { + ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_CUDA, + ist->hwaccel_device, NULL, 0); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Error creating a CUDA device\n"); + return ret; } } - if (!ctx->hw_frames_ctx) { - ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_CUDA, - ist->hwaccel_device, NULL, 0); - if (ret < 0) - goto error; + av_buffer_unref(&ist->hw_frames_ctx); + ist->hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx); + if (!ist->hw_frames_ctx) { + av_log(avctx, AV_LOG_ERROR, "Error creating a CUDA frames context\n"); + return AVERROR(ENOMEM); + } - ctx->hw_frames_ctx = av_hwframe_ctx_alloc(device_ref); - if (!ctx->hw_frames_ctx) { - av_log(NULL, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed\n"); - ret = AVERROR(ENOMEM); - goto error; - } - av_buffer_unref(&device_ref); + frames_ctx = (AVHWFramesContext*)ist->hw_frames_ctx->data; - ist->hw_frames_ctx = av_buffer_ref(ctx->hw_frames_ctx); - if (!ist->hw_frames_ctx) { - av_log(NULL, AV_LOG_ERROR, "av_buffer_ref failed\n"); - ret = AVERROR(ENOMEM); - goto error; - } + frames_ctx->format = AV_PIX_FMT_CUDA; + frames_ctx->sw_format = avctx->sw_pix_fmt; + frames_ctx->width = avctx->width; + frames_ctx->height = avctx->height; - ist->hwaccel_ctx = ctx; - ist->hwaccel_uninit = cuvid_uninit; + av_log(avctx, AV_LOG_DEBUG, "Initializing CUDA frames context: sw_format = %s, width = %d, height = %d\n", + av_get_pix_fmt_name(frames_ctx->sw_format), frames_ctx->width, frames_ctx->height); - /* This is a bit hacky, av_hwframe_ctx_init is called by the cuvid decoder - * once it has probed the necessary format information. But as filters/nvenc - * need to know the format/sw_format, set them here so they are happy. - * This is fine as long as CUVID doesn't add another supported pix_fmt. - */ - hwframe_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data; - hwframe_ctx->format = AV_PIX_FMT_CUDA; - hwframe_ctx->sw_format = ist->st->codecpar->format == AV_PIX_FMT_YUV420P10 ? AV_PIX_FMT_P010 : AV_PIX_FMT_NV12; + ret = av_hwframe_ctx_init(ist->hw_frames_ctx); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Error initializing a CUDA frame pool\n"); + return ret; } - return 0; - -error: - av_freep(&ctx); - av_buffer_unref(&device_ref); - return ret; - -cancel: - if (ist->hwaccel_id == HWACCEL_CUVID) { - av_log(NULL, AV_LOG_ERROR, "CUVID hwaccel requested, but impossible to achieve.\n"); - return AVERROR(EINVAL); - } + ist->hwaccel_uninit = cuvid_uninit; return 0; }