From patchwork Thu Feb 29 05:33:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wu, Tong1" X-Patchwork-Id: 46640 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:a919:b0:19e:cdac:8cce with SMTP id cd25csp379595pzb; Wed, 28 Feb 2024 21:35:37 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXGIY+pxH1zjuFi0PMG5LsARIWPT5PrG8eNYRu0gl4Zq+NyLeEH4cQQaV0k6mxSgT5FxrtE/dZ7YODEhVFicEHXmZVXe7rUeVywMw== X-Google-Smtp-Source: AGHT+IH0s9mh9ehLViSzv1IpSZKLJBfdijQp1rdHYT1AoTwUNk/ELrK9ptd1bUeyNKu/q1tGDKXi X-Received: by 2002:aa7:dd0e:0:b0:566:3d19:9058 with SMTP id i14-20020aa7dd0e000000b005663d199058mr690996edv.33.1709184937108; Wed, 28 Feb 2024 21:35:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709184937; cv=none; d=google.com; s=arc-20160816; b=jwO3cZrizI34ziDSf+GPF/EMNVPEI8YoQBHzgVTLJPm9wqKWWXt+bnRELbnGuAZ4R8 HINzzmi3gjvGcw+myzFJmbDmr+CQHLD7ru8mOPUBgtaDadhSO2ZkLT9kLdlzwNwciDMF ls9TvFR7RIoVt0845Vln6jdUkcNBc77MoA+XHtjfD4KmmzVYItQjbM+Jz3OcT/CLvbVy TU0TjScTQo6A/t1aXxKJf/AujHgq3xB30QVIo+JlTkm/Vm8hl2DgrnyvkkytEsUH1xXm jqmgPBs+lwXtVHHlSR93ECTPUEnLNxU6uWJwfx3krgASj2asT4FKeXKF1mbtCjAbOvGE nY5w== 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=g64Unaz1BbZKbk6KF3fRILFgA/Njwck5iWAh3CzsS3s=; fh=CX/m9qTuMDwrotqtm4RkAOJT6yXlKL2vcfWDitFPXZs=; b=SvGcAjQuTFLBe239odo83qAT7i+gkcMw2TjGAbLk3dpStKIOENxr/2E64UNXP7za+p 9ECRGnKxk86tVbZD5h/Djvk6ATl6Qo43IPEIwYlMLYhf/9/vrcwxegzn3wuvpddxhJEH yQUnpfmbkhM0C34lkjV+R5pDLFkVuoihwn5vlFLjEOaWX9Xx5BKUTE51wEM74ESuCwSP sT61xrwViFnXmn79fBdQ3rbMN4VIheKF/IP6iFSlg77t7Ru4m2rPi8ahIApAGg+Yc6YT RGMXKJ3ANEXCn9dhUYEZyTSRMmMSKZlu7dA/EeWi/RfefbHtD514sAleeKOyIehkm5Yj 0JRg==; 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="WLWEyE//"; 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 c2-20020a50d642000000b0056633fa8a9fsi251457edj.380.2024.02.28.21.35.36; Wed, 28 Feb 2024 21:35:37 -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="WLWEyE//"; 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 CC9F368D025; Thu, 29 Feb 2024 07:34:59 +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.15]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 147AE68CFD1 for ; Thu, 29 Feb 2024 07:34:52 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709184899; x=1740720899; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Qhl+kLXsXwfizDE7kkHHRKCdCeCgn4LYUoBy/QKxnhk=; b=WLWEyE//f8+0SeynvRBIKSwqCm5o04M21Xh06SrRYyjUGHUlyh4cNe9s wtYwnvtv8v4zQGjyFZOKV1VSr1ehgQNZZKH1m1JtDscxXtswyZz0tWoJ9 6ukIFMnAbgXCiBN904aD5jyZZSMWGqB76A496wpWMB2Pj5rcDLyvgttNa VOzOiUwwNkzF5GQcU+mCj0KRNsR6JzPTuF8Be49YMbEYAnziq1zErfDnB NZAlyGc9/BaQ27YjvPwf7lQRkBx0E475LUooBk7xHvZ4Irhh4mtxrVtwk /2QlgpnBbN6URluaxNFhHo0EE3VlQZGXCGWZTToa0zmGkAy0tp4d2ZX60 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10998"; a="3797829" X-IronPort-AV: E=Sophos;i="6.06,192,1705392000"; d="scan'208";a="3797829" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Feb 2024 21:34:47 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,192,1705392000"; d="scan'208";a="30870253" Received: from desktop-qn7n0nf.sh.intel.com (HELO localhost.localdomain) ([10.239.160.66]) by fmviesa002.fm.intel.com with ESMTP; 28 Feb 2024 21:34:46 -0800 From: tong1.wu-at-intel.com@ffmpeg.org To: ffmpeg-devel@ffmpeg.org Date: Thu, 29 Feb 2024 13:33:21 +0800 Message-ID: <20240229053326.75-5-tong1.wu@intel.com> X-Mailer: git-send-email 2.41.0.windows.1 In-Reply-To: <20240229053326.75-1-tong1.wu@intel.com> References: <20240229053326.75-1-tong1.wu@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v6 5/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: MfKPFQEYQXL8 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 2db75e98c3..2aef29b561 100644 --- a/libavcodec/hw_base_encode.c +++ b/libavcodec/hw_base_encode.c @@ -692,6 +692,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 764385ab5a..383bc68280 100644 --- a/libavcodec/hw_base_encode.h +++ b/libavcodec/hw_base_encode.h @@ -237,6 +237,8 @@ int ff_hw_base_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt); 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 0f72fe5cb3..8503742430 100644 --- a/libavcodec/vaapi_encode.c +++ b/libavcodec/vaapi_encode.c @@ -2022,9 +2022,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) { @@ -2033,52 +2032,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) { @@ -2102,7 +2058,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; }