From patchwork Fri Feb 2 10:16:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wu, Tong1" X-Patchwork-Id: 45979 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a28:b0:199:de12:6fa6 with SMTP id cj40csp841221pzb; Fri, 2 Feb 2024 02:08:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IFc6NX1k24wYt4DyDHvYYhg5dHJ8M58L86M3nd+Z+8M2JTO3M4gP9rGyp8ftbgI7yU6quBR X-Received: by 2002:a05:6402:6d6:b0:55f:8f59:8ef6 with SMTP id n22-20020a05640206d600b0055f8f598ef6mr5242138edy.5.1706868525415; Fri, 02 Feb 2024 02:08:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1706868525; cv=none; d=google.com; s=arc-20160816; b=IHuMJYOUa8x78c3TiEmLgZ1J5VpcumRYpK2WsX+FNkh/uAGE/WK+wBXzx0cepJl2aw nZ3eeDlPRk3bEEhgPsOBtJ14BbJ8649KrfxXW5Cf2Rw7i1FmbCfj+w18Hl7QaLJ1fcEr liPHpG2WuECsd734QU/buQmMersV/SquOtrdjH/3c8/Xm6y2rL2B2iYqI0w7NwoivWfW OzmUIpFTNR8TigzGLaIrkLBcRo6ykFl60rbb4jggbpYSC3ZYQfcal8XKPQXG11ieyyVg RVg87ZKwDQpnC3DAva6G6I/vmoQhrXngALwYKnyDQpdXRJswQDDLaY+U4uya8sjQ3wRp iWvw== 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=cPBJvPgKiwan50QylpF5bns5konz0VWiHKSu05y0pW4=; fh=UqgrRF6J2gPTGDCxH1x2Y/UAatPrjLB5xfp/e6ypcDg=; b=YDBTJ/4Vh1QtBUygCt0ns2D9BclrPJpnn6CtQIMTrSVuT+e+C6j72MNA/sIjW9d0nU D3hcnA55kvav7+JQebYMumvpI/uI7viPtuv+uuGUGraSG8107t8clknDGEppgqWXNdZG TTLaMWmwA5VEU6Yt2Bvf5W9DWaaXyBaFVRutJKdnMl9pQdxeYHr1jwCqVNIA1LppoTrz ez/p+Y2E/5qCIv6uD9aHnj2qspUqxlCnJeHtOaYQ5dKTPt5pqVpasK8CeNIXyo3XQ1se NmtdFCWsPZIXjiRvBFzc9aB2qnp7LzIAQt0IE+I1ORb2yFbcrgWOdyjbXT55+M4ra4Xc /N/w==; 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=oEcygBtD; 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 X-Forwarded-Encrypted: i=0; AJvYcCXIqzJBP0/fme4n1dx59eH88z2G3Bxfz0CtB4FFAKBWcsE9BwL9DAOs3BPOj3U+ICzCkx3XhXcWaMrcCJM+mrTvIc6MRFVh7eU5+WPWH7uc+XjQtosRg3/Cu1QmcsigfC2uIFeHiSwBFMfbdI1ZH6pHkgaFgpFmFtS9zynjxkv9V8blbv/4xEEYNVSn/4zROlo4sjo57ZCuZG4tvgnTYzNM0ZMtuBOezDkQkUtpOJRQStpTxYZj21tM/p9wVCpRjtlE53yXp1LxypHGv6JYCKYq06OvcA+CvrN7HCdUNt0SvUA3VXxWarWQTWovIQIaC2LLHgp2slPzAByTq+xVEiJfCGJ8XaObtLz5myuy4SD4DkRw58QHZ5DyW2rBSn5gTeYgTQx6o7uRK2u273ijY87iCXbw5OU+kNnlZqhSlv5UmpaCjK3tt7r3///Nl5GrUmg65OYgmt9/mk5JaxU3ZNpOeH1zBX45o7A8aNgb8Ca09Z7ahQ4r744sr6CPfgx68kzuI0QBZHGW7uC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HEQXDuLIOWh8xL1h Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id r21-20020a50aad5000000b0055eac41bb97si654964edc.69.2024.02.02.02.08.44; Fri, 02 Feb 2024 02:08: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=@intel.com header.s=Intel header.b=oEcygBtD; 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 0D63668D169; Fri, 2 Feb 2024 12:08:04 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.18]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8499E68D152 for ; Fri, 2 Feb 2024 12:07:55 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706868481; x=1738404481; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cKsLJR2y7UiJ9neIY7ZUKBs9919PzguRB1aG2krW/CM=; b=oEcygBtDC9Hz7iuBlY6p1Y758QVRL5q1o70/CC9Jj6503Yz1/dxUM9UI 0iBPTYNe3l5+ywAebhBO2OSM3VkbMa1nbE9hf+CrJJzAIJcnrn5f/476w Nbf3ejvQ3jvW+RX1UMK0Aq0WfwYdFdJh8fWgZsthfvqBP+CBbN9/7Va8N fTNwVWuC0u3JyzscPWVuTKqaQYopG8maEFyldFQfTA5o84aeJBpC9Pig2 8aIW+qonFdF21X+1M/LSDsVx30yj8XGX9QQ7tT1U0WKt69ZD3EQ5fLJ6z X3xWis7zkRQdddVlLFSTOMg3/kplpcsLDvxuvBdDLPOJi+hywGrDBR0wG g==; X-IronPort-AV: E=McAfee;i="6600,9927,10971"; a="33385" X-IronPort-AV: E=Sophos;i="6.05,237,1701158400"; d="scan'208";a="33385" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by fmvoesa112.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Feb 2024 02:07:53 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,237,1701158400"; d="scan'208";a="4795898" Received: from media-ci-tgl2.sh.intel.com (HELO localhost.localdomain) ([10.239.47.127]) by orviesa004.jf.intel.com with ESMTP; 02 Feb 2024 02:07:51 -0800 From: tong1.wu-at-intel.com@ffmpeg.org To: ffmpeg-devel@ffmpeg.org Date: Fri, 2 Feb 2024 18:16:11 +0800 Message-ID: <20240202101615.382-6-tong1.wu@intel.com> X-Mailer: git-send-email 2.43.0.windows.1 In-Reply-To: <20240202101615.382-1-tong1.wu@intel.com> References: <20240202101615.382-1-tong1.wu@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 6/9] avcodec/vaapi_encode: extract a get_recon_format function to base layer 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: Tong Wu Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: uyU2qPLnOx4M From: Tong Wu Get constraints and set recon frame format can be shared with other HW encoder such as D3D12. Extract this part as a new function to base layer. Signed-off-by: Tong Wu --- libavcodec/hw_base_encode.c | 58 +++++++++++++++++++++++++++++++++++++ libavcodec/hw_base_encode.h | 2 ++ libavcodec/vaapi_encode.c | 51 ++------------------------------ 3 files changed, 63 insertions(+), 48 deletions(-) diff --git a/libavcodec/hw_base_encode.c b/libavcodec/hw_base_encode.c index bb9fe70239..7497e0397e 100644 --- a/libavcodec/hw_base_encode.c +++ b/libavcodec/hw_base_encode.c @@ -836,6 +836,64 @@ int ff_hw_base_init_gop_structure(AVCodecContext *avctx, uint32_t ref_l0, uint32 return 0; } +int ff_hw_base_get_recon_format(AVCodecContext *avctx, const void *hwconfig, enum AVPixelFormat *fmt) +{ + HWBaseEncodeContext *ctx = avctx->priv_data; + AVHWFramesConstraints *constraints = NULL; + enum AVPixelFormat recon_format; + int err, i; + + constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref, + hwconfig); + if (!constraints) { + err = AVERROR(ENOMEM); + goto fail; + } + + // Probably we can use the input surface format as the surface format + // of the reconstructed frames. If not, we just pick the first (only?) + // format in the valid list and hope that it all works. + recon_format = AV_PIX_FMT_NONE; + if (constraints->valid_sw_formats) { + for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) { + if (ctx->input_frames->sw_format == + constraints->valid_sw_formats[i]) { + recon_format = ctx->input_frames->sw_format; + break; + } + } + if (recon_format == AV_PIX_FMT_NONE) { + // No match. Just use the first in the supported list and + // hope for the best. + recon_format = constraints->valid_sw_formats[0]; + } + } else { + // No idea what to use; copy input format. + recon_format = ctx->input_frames->sw_format; + } + av_log(avctx, AV_LOG_DEBUG, "Using %s as format of " + "reconstructed frames.\n", av_get_pix_fmt_name(recon_format)); + + if (ctx->surface_width < constraints->min_width || + ctx->surface_height < constraints->min_height || + ctx->surface_width > constraints->max_width || + ctx->surface_height > constraints->max_height) { + av_log(avctx, AV_LOG_ERROR, "Hardware does not support encoding at " + "size %dx%d (constraints: width %d-%d height %d-%d).\n", + ctx->surface_width, ctx->surface_height, + constraints->min_width, constraints->max_width, + constraints->min_height, constraints->max_height); + err = AVERROR(EINVAL); + goto fail; + } + + *fmt = recon_format; + err = 0; +fail: + av_hwframe_constraints_free(&constraints); + return err; +} + int ff_hw_base_encode_init(AVCodecContext *avctx) { HWBaseEncodeContext *ctx = avctx->priv_data; diff --git a/libavcodec/hw_base_encode.h b/libavcodec/hw_base_encode.h index d6d2fc03c5..3d026ee23e 100644 --- a/libavcodec/hw_base_encode.h +++ b/libavcodec/hw_base_encode.h @@ -279,6 +279,8 @@ int ff_hw_base_rc_mode_configure(AVCodecContext *avctx, const HWBaseEncodeRCMode int ff_hw_base_init_gop_structure(AVCodecContext *avctx, uint32_t ref_l0, uint32_t ref_l1, int flags, int prediction_pre_only); +int ff_hw_base_get_recon_format(AVCodecContext *avctx, const void *hwconfig, enum AVPixelFormat *fmt); + int ff_hw_base_encode_init(AVCodecContext *avctx); int ff_hw_base_encode_close(AVCodecContext *avctx); diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c index 0bce3ce105..84a81559e1 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -1898,9 +1898,8 @@ static av_cold int vaapi_encode_create_recon_frames(AVCodecContext *avctx) HWBaseEncodeContext *base_ctx = avctx->priv_data; VAAPIEncodeContext *ctx = avctx->priv_data; AVVAAPIHWConfig *hwconfig = NULL; - AVHWFramesConstraints *constraints = NULL; enum AVPixelFormat recon_format; - int err, i; + int err; hwconfig = av_hwdevice_hwconfig_alloc(base_ctx->device_ref); if (!hwconfig) { @@ -1909,52 +1908,9 @@ static av_cold int vaapi_encode_create_recon_frames(AVCodecContext *avctx) } hwconfig->config_id = ctx->va_config; - constraints = av_hwdevice_get_hwframe_constraints(base_ctx->device_ref, - hwconfig); - if (!constraints) { - err = AVERROR(ENOMEM); - goto fail; - } - - // Probably we can use the input surface format as the surface format - // of the reconstructed frames. If not, we just pick the first (only?) - // format in the valid list and hope that it all works. - recon_format = AV_PIX_FMT_NONE; - if (constraints->valid_sw_formats) { - for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) { - if (base_ctx->input_frames->sw_format == - constraints->valid_sw_formats[i]) { - recon_format = base_ctx->input_frames->sw_format; - break; - } - } - if (recon_format == AV_PIX_FMT_NONE) { - // No match. Just use the first in the supported list and - // hope for the best. - recon_format = constraints->valid_sw_formats[0]; - } - } else { - // No idea what to use; copy input format. - recon_format = base_ctx->input_frames->sw_format; - } - av_log(avctx, AV_LOG_DEBUG, "Using %s as format of " - "reconstructed frames.\n", av_get_pix_fmt_name(recon_format)); - - if (base_ctx->surface_width < constraints->min_width || - base_ctx->surface_height < constraints->min_height || - base_ctx->surface_width > constraints->max_width || - base_ctx->surface_height > constraints->max_height) { - av_log(avctx, AV_LOG_ERROR, "Hardware does not support encoding at " - "size %dx%d (constraints: width %d-%d height %d-%d).\n", - base_ctx->surface_width, base_ctx->surface_height, - constraints->min_width, constraints->max_width, - constraints->min_height, constraints->max_height); - err = AVERROR(EINVAL); + err = ff_hw_base_get_recon_format(avctx, (const void*)hwconfig, &recon_format); + if (err < 0) goto fail; - } - - av_freep(&hwconfig); - av_hwframe_constraints_free(&constraints); base_ctx->recon_frames_ref = av_hwframe_ctx_alloc(base_ctx->device_ref); if (!base_ctx->recon_frames_ref) { @@ -1978,7 +1934,6 @@ static av_cold int vaapi_encode_create_recon_frames(AVCodecContext *avctx) err = 0; fail: av_freep(&hwconfig); - av_hwframe_constraints_free(&constraints); return err; }