From patchwork Thu Feb 29 05:34:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xiang, Haihao" X-Patchwork-Id: 46645 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:a919:b0:19e:cdac:8cce with SMTP id cd25csp379773pzb; Wed, 28 Feb 2024 21:36:20 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCWgesYSorm4J/3SzZVrjqd6r60Qx4Chm48nnVQB45Syt9j8muMEhDoqIG+XlCe6DXOMEajKyKC5dG1o7LA8UazDYNRFoPuxkvpAqw== X-Google-Smtp-Source: AGHT+IHfyEOVPxkAI5orTZ0JDh/2K5DUpeqqEPXME+4zVyv/cG0tIV8HnIVpUBgzErhkrlOPT6kv X-Received: by 2002:a17:906:418:b0:a3f:daf8:bd3b with SMTP id d24-20020a170906041800b00a3fdaf8bd3bmr581118eja.8.1709184979883; Wed, 28 Feb 2024 21:36:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709184979; cv=none; d=google.com; s=arc-20160816; b=K78iq9x1GHew70tm54puSubkpJ09QysJGB+mhFKK+BPyVEcHktGWEerXSValMawVrU aL6SfZOG9+iAlNQ0/BFv1xpu/AH9jghoKMUy8DoCcat7qydw2HLWsjpY/QL59LxdFnLW EGl9yj1llWUpAtcEMdfxtkhMtq55SIbmD2/3rogyc39sm0VDnOKOMZGos1DV6r0t0WJ9 Tf/4ytVN7yXZqBFei7UxtGAyt2pojT5EoU3XTnlmJkt5a4KTlKSyEYgz/lYJKDM4sq04 kRei4a8amsJ4mETLZu+P00l8XA7sWDX+bIQl9AOGOo7YfVSxArt9LB16uc7+OH6osLBi 5fNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=KnUqoioB4CvRbCyLhC9QdsrgHzwpOp1p0fzAZ60GSoI=; fh=1kpmTRI1sW42eNCnNMby7VLIISQBX6KJuVYxOEG0t2A=; b=iiD5CUBUzvhsKuhxVpbuveEHCy1Qqyatfvkjp2HDdLaPUfzLSIgjZQclEe+8nNz8BT QHwv0ETpH+2ON00B0MTV/UxR7mW31wmuVGIRoxpwD1b6NdXyI++YVubBnSnG00NLylH1 g9ejE773KSUE/q9OLCcoECSsl6tj0KGMzBD3dheQv7Gc+8HXVJjNdswH3iPfX+rf8h/C ++mDebl0eWO2x+2p3YN7OsnOH3cW0CK7nLR+cGRHh0frct3UjY9LDaJPR4Y0EQaKViA/ +O6vJx1T0YMKDqUf/zmfunNH8alYifPajptTADE6eOB5v7rgnUd+QdaRQYLVeNlGcFD6 x/0g==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=ihrEfkSw; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id by21-20020a170906a2d500b00a3fc1f16170si255683ejb.626.2024.02.28.21.36.19; Wed, 28 Feb 2024 21:36:19 -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=@intel.com header.s=Intel header.b=ihrEfkSw; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0B28868D02A; Thu, 29 Feb 2024 07:35:13 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E0D7E68CDE9 for ; Thu, 29 Feb 2024 07:35:10 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709184911; x=1740720911; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=DMu5ODhfRXM9iHKWsJPyXYGt/iNEs9/zYvuyxEV/so4=; b=ihrEfkSwuuNSU8E+fgamtgFkNjdQmtbCTUkoj69HEfo7FLgIfyGKutou apZhEsXPEkVrjPtGLhGj/tO3acPm8WN0ZKU7tOGAdBBBxaD0vL3U046Mo 2dOx0AAorv9ZUPHGKvxIIoArG2+Tjx5y1/402utOONLK5ivbmjg5HhmlT obaUYR5fAY/Sv/xvIp1vMoPfBW0LwnJLr/CV2OQJBkiLFLR2T2RvxFVzU rYIGDkbAGkx/KcVj8E4j/23NAoya7/FTWTwNC0VtZ22Wd+PpZbbkFr4nQ H1hUb0TlP/AlBj30nz2ZbnyX0z25u8ZKhhDCENj1+yz613B0w9eRLnI+9 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10998"; a="21090389" X-IronPort-AV: E=Sophos;i="6.06,192,1705392000"; d="scan'208";a="21090389" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2024 21:35:08 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,192,1705392000"; d="scan'208";a="7569500" Received: from xhh-dg264.sh.intel.com ([10.238.2.76]) by fmviesa007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2024 21:35:07 -0800 From: "Xiang, Haihao" To: ffmpeg-devel@ffmpeg.org Date: Thu, 29 Feb 2024 13:34:33 +0800 Message-Id: <20240229053434.2476820-1-haihao.xiang@intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] lavc/qsvenc: always allocate the array to store mfxExtBuffer points X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Haihao Xiang Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: nclJSOl5jLEt From: Haihao Xiang This allows us to append mfxExtBuffer per user's settings Signed-off-by: Haihao Xiang --- libavcodec/qsvenc.c | 44 +++++++++++++++++++++++++------------------- libavcodec/qsvenc.h | 1 + 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index c63b72e384..27e0e7ee71 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -1608,6 +1608,7 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) int iopattern = 0; int opaque_alloc = 0; int ret; + void *tmp; q->param.AsyncDepth = q->async_depth; @@ -1668,35 +1669,40 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) if (ret < 0) return ret; + tmp = av_realloc_array(q->extparam, q->nb_extparam_internal, sizeof(*q->extparam)); + if (!tmp) + return AVERROR(ENOMEM); + + q->extparam = tmp; + q->nb_extparam = q->nb_extparam_internal; + memcpy(q->extparam, q->extparam_internal, q->nb_extparam * sizeof(*q->extparam)); + if (avctx->hwaccel_context) { AVQSVContext *qsv = avctx->hwaccel_context; int i, j; - q->extparam = av_calloc(qsv->nb_ext_buffers + q->nb_extparam_internal, - sizeof(*q->extparam)); - if (!q->extparam) - return AVERROR(ENOMEM); - - q->param.ExtParam = q->extparam; - for (i = 0; i < qsv->nb_ext_buffers; i++) - q->param.ExtParam[i] = qsv->ext_buffers[i]; - q->param.NumExtParam = qsv->nb_ext_buffers; - - for (i = 0; i < q->nb_extparam_internal; i++) { - for (j = 0; j < qsv->nb_ext_buffers; j++) { - if (qsv->ext_buffers[j]->BufferId == q->extparam_internal[i]->BufferId) + for (i = 0; i < qsv->nb_ext_buffers; i++) { + for (j = 0; j < q->nb_extparam_internal; j++) { + if (qsv->ext_buffers[i]->BufferId == q->extparam_internal[j]->BufferId) { + q->extparam[j] = qsv->ext_buffers[i]; break; + } } - if (j < qsv->nb_ext_buffers) - continue; - q->param.ExtParam[q->param.NumExtParam++] = q->extparam_internal[i]; + if (j == q->nb_extparam_internal) { + tmp = av_realloc_array(q->extparam, q->nb_extparam + 1, sizeof(*q->extparam)); + if (!tmp) + return AVERROR(ENOMEM); + + q->extparam = tmp; + q->extparam[q->nb_extparam++] = qsv->ext_buffers[i]; + } } - } else { - q->param.ExtParam = q->extparam_internal; - q->param.NumExtParam = q->nb_extparam_internal; } + q->param.ExtParam = q->extparam; + q->param.NumExtParam = q->nb_extparam; + ret = MFXVideoENCODE_Query(q->session, &q->param, &q->param); if (ret == MFX_WRN_PARTIAL_ACCELERATION) { av_log(avctx, AV_LOG_WARNING, "Encoder will work with partial HW acceleration\n"); diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h index c71bf2ed50..f2f0687bfe 100644 --- a/libavcodec/qsvenc.h +++ b/libavcodec/qsvenc.h @@ -196,6 +196,7 @@ typedef struct QSVEncContext { int nb_extparam_internal; mfxExtBuffer **extparam; + int nb_extparam; AVFifo *async_fifo; From patchwork Thu Feb 29 05:34:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xiang, Haihao" X-Patchwork-Id: 46646 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:a919:b0:19e:cdac:8cce with SMTP id cd25csp379818pzb; Wed, 28 Feb 2024 21:36:27 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUu3uQq5t+KnJI15/jJbxc1Y+6zDyWSpWm5K/uTJxlVUepaEUgzUaCQshEZjqSqL1xcsIJxraUe7YWtZ/jYHPJqwiP1mBLMKKHUXg== X-Google-Smtp-Source: AGHT+IHy00/DUKYraZMCmgfiZJ74aaWNUKLW4eJ40dgl5C6g+Vk51PlyxucmvG/jIKItIlslpu5J X-Received: by 2002:a05:6402:2156:b0:566:93d9:a184 with SMTP id bq22-20020a056402215600b0056693d9a184mr637138edb.7.1709184987371; Wed, 28 Feb 2024 21:36:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709184987; cv=none; d=google.com; s=arc-20160816; b=pa7XjmNRFCjrZS6urzyRic9T3q4bAnJS94n30/ut7u3YUd3VXFJdw8A4yRLLipmckA +wvsX29Qqn3FfBl4RMxEqnc1qTGdmM04vvCYD3UjfFW5S59C8ZZ8e7hvbxVU/L6Fi5bK MwBsOH4zpM2I7PKVi66uQ3fpCaJ0lU51SE4t9vntzVvwybzH+wl4teNF5mlQ9vrFI4IX FpsftVdw3mc2vogUln92e2fhnL8+eVq3TK1bK1IQAwwrTWIc6VOK8HBy1ALkAs4Dkiqv eyBOmkGZDnKk8owkxcBmYS+scPwSRvEX424Oed0SCvkjNNggcNzNnid4u14YLhdDU95V LzZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=uVsXD8ZTgaIheQgzzlKyXDk5WBQr8VWX20iTnc8swF8=; fh=xMUt5ZqLERfZ4EmaFs0SqJX6wF5PBu8N2o+CnKkV9PY=; b=GrynrNaiUfHXPwtxi3tlRi5478qMC92SrcOC7+vxorNDuHWOrxFEvxtt8eL2+pKNk6 ntU1k8hffV97oJ1Whd8uwYymOq/pEq8g5UT468dEUP7ip1S3XfliPOwzdGRWhHTfEGrE 9Kgw+ejUwuuSy91z9A5Tp3KKsXlrAUvZW+9/cEIo7bdUHi9C8+ts+1zSKIyubjPstUIf YBFUtOl4X4LWPlouQ1Xa+No2XTeTxZgfL8ip1IMkCCPdMKYxOsbkwUdb1UWbsiP/UkG+ zX4BuDzYzqDjlvo2B7CBQ9Al6KgSGhnHIvvX1KuwDwM8kXj1X4qmOnqQELtVISgi76Up QQOg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=kiTgo+cU; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id d19-20020a05640208d300b005665d7634fesi264323edz.355.2024.02.28.21.36.27; Wed, 28 Feb 2024 21:36:27 -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=@intel.com header.s=Intel header.b=kiTgo+cU; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 14A6868D07E; Thu, 29 Feb 2024 07:35:15 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 01D9768CDE9 for ; Thu, 29 Feb 2024 07:35:11 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709184912; x=1740720912; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9vVk2KchXvBNZJ/W/EE5ScNO/G8q4dPUV6wlz6/PRpM=; b=kiTgo+cU56j6etwH1nI+tLmEMYMp4E8OfophxBxAuSuBq6FNcAwkrEpt GIuXjs64zvgEohLG6GQ48ns9t/x/hQjiWAkOhdMVZI0vwOYs9b3OrD0el yfGYWkxCDhSnzGmfL/615nfwqd4NI6MV9GBPUs8TLfwudDT/QyYR/hJfN MajStHWkI/5YqLGWBTjzy+rKSDvwtkGlDFpDz15x1TSK3P71+0WeKJuei jWCyipIOUWyIslWRYio07hPgI8pQPY1C7LDixKplI4TU00q3SYyZ+T7ia M9EHl9xyZyHxTkiYT9i9Jl1cq6C5So5pAZVtVAgNUTSmy4tJsghOkYwc5 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10998"; a="21090391" X-IronPort-AV: E=Sophos;i="6.06,192,1705392000"; d="scan'208";a="21090391" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2024 21:35:09 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,192,1705392000"; d="scan'208";a="7569506" Received: from xhh-dg264.sh.intel.com ([10.238.2.76]) by fmviesa007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2024 21:35:08 -0800 From: "Xiang, Haihao" To: ffmpeg-devel@ffmpeg.org Date: Thu, 29 Feb 2024 13:34:34 +0800 Message-Id: <20240229053434.2476820-2-haihao.xiang@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240229053434.2476820-1-haihao.xiang@intel.com> References: <20240229053434.2476820-1-haihao.xiang@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] lavc/qsvenc: add support for oneVPL string API X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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: Haihao Xiang , "Mandava, Mounika" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: HyzwWttIgMbT From: "Mandava, Mounika" A new option -qsv_params is added, where is a :-separated list of key=value parameters. Example: $ ffmpeg -y -f lavfi -i testsrc -vf "format=nv12" -c:v h264_qsv -qsv_params "TargetUsage=1:GopPicSize=30:GopRefDist=2:TargetKbps=5000" -f null - Signed-off-by: Mounika Mandava Signed-off-by: Haihao Xiang --- Changelog | 1 + configure | 2 ++ doc/encoders.texi | 14 ++++++++++ libavcodec/qsvenc.c | 62 +++++++++++++++++++++++++++++++++++++++++++++ libavcodec/qsvenc.h | 8 +++++- 5 files changed, 86 insertions(+), 1 deletion(-) diff --git a/Changelog b/Changelog index 610ee61dd6..b137d089d8 100644 --- a/Changelog +++ b/Changelog @@ -27,6 +27,7 @@ version : - a C11-compliant compiler is now required; note that this requirement will be bumped to C17 in the near future, so consider updating your build environment if it lacks C17 support +- qsv_params option added for QSV encoders version 6.1: - libaribcaption decoder diff --git a/configure b/configure index bb5e630bad..046c481f63 100755 --- a/configure +++ b/configure @@ -2431,6 +2431,7 @@ TYPES_LIST=" struct_sockaddr_storage struct_stat_st_mtim_tv_nsec struct_v4l2_frmivalenum_discrete + struct_mfxConfigInterface " HAVE_LIST=" @@ -6828,6 +6829,7 @@ elif enabled libvpl; then check_pkg_config libmfx "vpl >= 2.6" "mfxvideo.h mfxdispatcher.h" MFXLoad || \ die "ERROR: libvpl >= 2.6 not found" add_cflags -DMFX_DEPRECATED_OFF + check_type "vpl/mfxdefs.h vpl/mfxvideo.h" "struct mfxConfigInterface" fi if enabled libmfx; then diff --git a/doc/encoders.texi b/doc/encoders.texi index 9f477d7c53..cbd3b538cf 100644 --- a/doc/encoders.texi +++ b/doc/encoders.texi @@ -3485,6 +3485,20 @@ Change these value to reset qsv codec's bitrate control configuration. @item @var{pic_timing_sei} Supported in h264_qsv and hevc_qsv. Change this value to reset qsv codec's pic_timing_sei configuration. + +@item @var{qsv_params} +Set QSV encoder parameters as a colon-separated list of key-value pairs. + +The @option{qsv_params} should be formatted as @code{key1=value1:key2=value2:...}. + +These parameters are passed directly to the underlying Intel Quick Sync Video (QSV) encoder using the MFXSetParameter function. + +Example: +@example +ffmpeg -i input.mp4 -c:v h264_qsv -qsv_params "CodingOption1=1:CodingOption2=2" output.mp4 +@end example + +This option allows fine-grained control over various encoder-specific settings provided by the QSV encoder. @end table @subsection H264 options diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c index 27e0e7ee71..0189c219d2 100644 --- a/libavcodec/qsvenc.c +++ b/libavcodec/qsvenc.c @@ -31,6 +31,7 @@ #include "libavutil/hwcontext_qsv.h" #include "libavutil/mem.h" #include "libavutil/log.h" +#include "libavutil/dict.h" #include "libavutil/time.h" #include "libavutil/imgutils.h" @@ -1609,6 +1610,11 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) int opaque_alloc = 0; int ret; void *tmp; +#if HAVE_STRUCT_MFXCONFIGINTERFACE + mfxExtBuffer ext_buf; + mfxConfigInterface *iface = NULL; + const AVDictionaryEntry *param = NULL; +#endif q->param.AsyncDepth = q->async_depth; @@ -1703,6 +1709,58 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q) q->param.ExtParam = q->extparam; q->param.NumExtParam = q->nb_extparam; +#if HAVE_STRUCT_MFXCONFIGINTERFACE + ret = MFXVideoCORE_GetHandle(q->session, MFX_HANDLE_CONFIG_INTERFACE, (mfxHDL *)(&iface)); + if (ret < 0) + return ff_qsv_print_error(avctx, ret, + "Error getting mfx config interface handle"); + + while ((param = av_dict_get(q->qsv_params, "", param, AV_DICT_IGNORE_SUFFIX))) { + const char *param_key = param->key; + const char *param_value = param->value; + mfxExtBuffer *new_ext_buf; + void *tmp; + + av_log(avctx, AV_LOG_VERBOSE, "Parameter key: %s, value: %s\n", param_key, param_value); + + // Set encoding parameters using MFXSetParameter + for (int i = 0; i < 2; i++) { + ret = iface->SetParameter(iface, (mfxU8*)param_key, (mfxU8*)param_value, MFX_STRUCTURE_TYPE_VIDEO_PARAM, &q->param, &ext_buf); + if (ret == MFX_ERR_NONE) { + break; + } else if (i == 0 && ret == MFX_ERR_MORE_EXTBUFFER) { + tmp = av_realloc_array(q->extparam_str, q->nb_extparam_str + 1, sizeof(*q->extparam_str)); + if (!tmp) + return AVERROR(ENOMEM); + q->extparam_str = tmp; + + tmp = av_realloc_array(q->extparam, q->nb_extparam + 1, sizeof(*q->extparam)); + if (!tmp) + return AVERROR(ENOMEM); + q->extparam = tmp; + + new_ext_buf = (mfxExtBuffer*)av_mallocz(ext_buf.BufferSz); + if (!new_ext_buf) + return AVERROR(ENOMEM); + + new_ext_buf->BufferId = ext_buf.BufferId; + new_ext_buf->BufferSz = ext_buf.BufferSz; + q->extparam_str[q->nb_extparam_str++] = new_ext_buf; + q->extparam[q->nb_extparam++] = new_ext_buf; + q->param.ExtParam = q->extparam; + q->param.NumExtParam = q->nb_extparam; + } else { + av_log(avctx, AV_LOG_ERROR, "Failed to set parameter: %s\n", param_key); + return AVERROR_UNKNOWN; + } + } + } +#else + if (q->qsv_params) { + av_log(avctx, AV_LOG_WARNING, "MFX string API is not supported, ignore qsv_params option\n"); + } +#endif + ret = MFXVideoENCODE_Query(q->session, &q->param, &q->param); if (ret == MFX_WRN_PARTIAL_ACCELERATION) { av_log(avctx, AV_LOG_WARNING, "Encoder will work with partial HW acceleration\n"); @@ -2662,6 +2720,10 @@ int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q) av_buffer_unref(&q->opaque_alloc_buf); #endif + for (int i = 0; i < q->nb_extparam_str; i++) + av_free(q->extparam_str[i]); + + av_freep(&q->extparam_str); av_freep(&q->extparam); return 0; diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h index f2f0687bfe..e3eb083746 100644 --- a/libavcodec/qsvenc.h +++ b/libavcodec/qsvenc.h @@ -64,7 +64,8 @@ { "slower", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_2 }, INT_MIN, INT_MAX, VE, .unit = "preset" }, \ { "veryslow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MFX_TARGETUSAGE_BEST_QUALITY }, INT_MIN, INT_MAX, VE, .unit = "preset" }, \ { "forced_idr", "Forcing I frames as IDR frames", OFFSET(qsv.forced_idr), AV_OPT_TYPE_BOOL,{ .i64 = 0 }, 0, 1, VE }, \ -{ "low_power", "enable low power mode(experimental: many limitations by mfx version, BRC modes, etc.)", OFFSET(qsv.low_power), AV_OPT_TYPE_BOOL, { .i64 = -1}, -1, 1, VE}, +{ "low_power", "enable low power mode(experimental: many limitations by mfx version, BRC modes, etc.)", OFFSET(qsv.low_power), AV_OPT_TYPE_BOOL, { .i64 = -1}, -1, 1, VE},\ +{ "qsv_params", "Set QSV encoder parameters as key1=value1:key2=value2:...", OFFSET(qsv.qsv_params), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE }, #if QSV_HAVE_HE #define QSV_HE_OPTIONS \ @@ -195,6 +196,9 @@ typedef struct QSVEncContext { mfxExtBuffer *extparam_internal[5 + (QSV_HAVE_MF * 2) + (QSV_HAVE_EXT_AV1_PARAM * 2) + QSV_HAVE_HE]; int nb_extparam_internal; + mfxExtBuffer **extparam_str; + int nb_extparam_str; + mfxExtBuffer **extparam; int nb_extparam; @@ -315,6 +319,8 @@ typedef struct QSVEncContext { int skip_frame; // This is used for Hyper Encode int dual_gfx; + + AVDictionary *qsv_params; } QSVEncContext; int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q);