From patchwork Wed Jun 29 06:32:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wu, Tong1" X-Patchwork-Id: 36518 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp121296pzh; Tue, 28 Jun 2022 23:34:30 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s2ipC2HDNL7FHZ+Y/8n98xpQUTs2dgNpmzhRnVDLIarGAHA8j+FrAB3f/7es+tRv2nl/E2 X-Received: by 2002:a05:6402:3708:b0:433:2d3b:ed5 with SMTP id ek8-20020a056402370800b004332d3b0ed5mr2186399edb.246.1656484470597; Tue, 28 Jun 2022 23:34:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656484470; cv=none; d=google.com; s=arc-20160816; b=Z0DrUVbLbN8J1lY/DlvnqUg3rKYEOgvt5CzLSNtOyZMXo6gyg5VxnUzJMu+0LhUoLJ inGhIMwZQm3mJjpKpWwThcufY7jXHN9O2Ft3DTQxpCUAtq89O2qds/tj2YhHU2GgQ5mI 1yeKc2foMx4LJW2jyAaZ+o7ByJLZsB/JnIZdUQqZdwaELe9JueMxmVBDMwQM/jw4Vr/K qwCndigwaOAEPqGr+BkhsSjWP4dHbcAuPfhDRHxnWiL35F9Q3oujQ1i+i/rVDU9OlzcM romrKtPE13WkiQuBp5hoeWQGLMssvhS8h3kNxxWjfI5L75vrQ/nxR7M//s2/jrljugPo Rzhg== 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=DtrJWgp+Rdbbx0lt6b6x0YWh+NhSdURFwvrvaIxuQR4=; b=CdzZvUxXIZTBEzjU76MRe1uVC+R0P4SkUW5/OHcyhP2Bln6s9GAhvXridu4mIGvea7 xidvEH7mpT1xh2CLEyYpD9LTaeN4NZSM+i2bs9aOaRN1JSD39jxDemgnonHlKLrSbbNB exGi/L18wH1TAQOVPsvrSEYCRz/xYc5K2vl/66svBxTdF0O+AEm+QOFSkmipAq5I4jb5 DF/sRftC9iTjtpz/blda8Pp7iPdlbBSRvBWvdLrSPv6XS3h/uA7WbBTLjnU6eIBiHxDt iSFxp7iCwAqaFg6RwOWZcWmrDJeGpAKbnYHXYqK/8AU/i7LedAsk6yAQlQjHuQTwIYp4 x9Lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b="QoZqc4w/"; 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 dr8-20020a170907720800b00715790f718dsi20262659ejc.318.2022.06.28.23.34.07; Tue, 28 Jun 2022 23:34:30 -0700 (PDT) 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="QoZqc4w/"; 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 C794668B6B0; Wed, 29 Jun 2022 09:34:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 82D3068AF1C for ; Wed, 29 Jun 2022 09:33:55 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656484441; x=1688020441; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=IYR8XUwKlj+wHdhMoBWYkmfNO1QamE/XcqsEP+Vc2eA=; b=QoZqc4w/cBjLxVWa1O3TSnz3jkmleTedryE5mF7kelJz+/UAOh4ALCeM LMLT2yxXaRtGv1O4c6BJetKZY2aVuEKqgjeZVq7/FA6yKVcgnghGoIr8I acw7S6Qd1COVf7ZgpOcLW5BTskpQ3FKG0f+KDLkb3+/WVxOlm9Wm4RTp7 WgBF50u1nZAK+U2Aw+BJ1XINiNrJKb1MciJQs0s+BG+oxLmsqdmQb3+bZ m84fcQj7BKQKMX8/15oTVjsQYpe1+HhfMLl+sY90YaGajVIKbXx7niTn9 9aCwCLBYHuC2DCd0d1wZAGfDuz0PkhHSSTNux8ffRiOhwtFypHniALuDB Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10392"; a="281973012" X-IronPort-AV: E=Sophos;i="5.92,230,1650956400"; d="scan'208";a="281973012" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jun 2022 23:33:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,230,1650956400"; d="scan'208";a="595108599" Received: from desktop-qn7n0nf.sh.intel.com (HELO localhost.localdomain) ([10.239.160.39]) by fmsmga007.fm.intel.com with ESMTP; 28 Jun 2022 23:33:52 -0700 From: Tong Wu To: ffmpeg-devel@ffmpeg.org Date: Wed, 29 Jun 2022 14:32:44 +0800 Message-Id: <20220629063246.183-1-tong1.wu@intel.com> X-Mailer: git-send-email 2.35.1.windows.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3] avutil/hwcontext: add a function to get the AVHWDeviceType 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: T3zH2i5CWYAb Add a function to get the corresponding AVHWDeviceType from a given hardware pixel format. Signed-off-by: Tong Wu --- libavutil/hwcontext.c | 12 ++++++++++++ libavutil/hwcontext.h | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c index ab9ad3703e..3521ed34f4 100644 --- a/libavutil/hwcontext.c +++ b/libavutil/hwcontext.c @@ -80,6 +80,18 @@ static const char *const hw_type_names[] = { [AV_HWDEVICE_TYPE_VULKAN] = "vulkan", }; +enum AVHWDeviceType av_hwdevice_get_type_by_pix_fmt(enum AVPixelFormat fmt) +{ + int i, j; + for (i = 0; hw_table[i]; i++) { + for (j = 0; hw_table[i]->pix_fmts[j] != AV_PIX_FMT_NONE; j++) { + if (hw_table[i]->pix_fmts[j] == fmt) + return hw_table[i]->type; + } + } + return AV_HWDEVICE_TYPE_NONE; +} + enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name) { int type; diff --git a/libavutil/hwcontext.h b/libavutil/hwcontext.h index c18b7e1e8b..97f94403e2 100644 --- a/libavutil/hwcontext.h +++ b/libavutil/hwcontext.h @@ -229,6 +229,15 @@ typedef struct AVHWFramesContext { int width, height; } AVHWFramesContext; +/** + * Get the device type by a given pixel format. + * + * @param fmt Pixel format from enum AVPixelFormat. + * @return The type from enum AVHWDeviceType, or AV_HWDEVICE_TYPE_NONE if + * not found. + */ +enum AVHWDeviceType av_hwdevice_get_type_by_pix_fmt(enum AVPixelFormat fmt); + /** * Look up an AVHWDeviceType by name. * From patchwork Wed Jun 29 06:32:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wu, Tong1" X-Patchwork-Id: 36519 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp121360pzh; Tue, 28 Jun 2022 23:34:40 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vjAw3L/KLtdosIjNOqnO2aIBLbI3qui3Nb3nEiQiyuYftoMYr4dicw10jSWS6S3WBDvtsM X-Received: by 2002:aa7:c881:0:b0:435:5dc4:5832 with SMTP id p1-20020aa7c881000000b004355dc45832mr2188160eds.265.1656484480405; Tue, 28 Jun 2022 23:34:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656484480; cv=none; d=google.com; s=arc-20160816; b=P/XyX0jc9xaII3uFPdZ4kaABNA07N25h3fRU2oQBY20K8Bl+4q+UZkZQLsl+l09LYd wPiJ7KHlAu9iJO/hPQSn1kHV3qYbdUfa6Lpu4/JL1TjgIq/ptDhJyA/6j3aWXu7fcQVk ZDL9Bg1x8mZFA1XlWT551KYxUVPdS+IyEplFdLKkmZmjXohcmLfuK1106ucSmunyR98D iWWwyLTe0fmRqZPpiytnW9eB9HG+0gaVil29crX3/Yp/M2pATPOrBYaO2khEKDbQfgj+ LRa+WqtpjfnJbeTT8lklxSwjMXzkLEeQ9WQGAG0N57bJlnGLni2Jva5LWFoUi5CABSIJ NN4g== 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=DBwlNNUy7qdpmeVczfv7Qw+GlXMrmfVq/Ad2xfV3DH0=; b=FOsBoJfMK3te+2upgkiENqda0+PaFQB4rWSxHqH4MFR5Rv5kgAJTpYCoPt8xtPYC9Q gijZ2lrcQ1gqhFuoRIbJGKz5GphpNdnocClc/7ujm7IE4Siwou1l1Gib8gGhDDmki84/ mHfnyTlOFw4Pm1/Z3TAqCzCByi1TYoPLfr8UD4CW4qyB0Nxd5rM7ThLdPlFJdU3HCN6y 4DZb4azaF9MtETZRAUn7TwNorPKqW3gQ1uWJttW/mU9dp7I2tjl14YkT1O7R8AgSW+W1 n6PcUltJvzuarZg6DkbJCT6lrO6QNA9bOVc67/pX5kDGo83skCkTgw83zeNqWqVw+JYZ cuYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=MpbrsVm9; 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 gs13-20020a1709072d0d00b007269f9395d7si10546401ejc.482.2022.06.28.23.34.18; Tue, 28 Jun 2022 23:34:40 -0700 (PDT) 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=MpbrsVm9; 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 EDFD568B6CC; Wed, 29 Jun 2022 09:34:04 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E4C6968B672 for ; Wed, 29 Jun 2022 09:33:56 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656484442; x=1688020442; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qeLfM2YUagz1A9dZF78vzkCMGvV4cYvTw126HNsX/Ps=; b=MpbrsVm9ba9zxCR/Y+pH+Yu3Mmfn8WBIOdnrB3JRnEC0MHRBNY95zakw ZLISWkKwTA1o5dpsFMNzwfRWfquTE0otzKc3na5WyeJlqXdYvBoS9psuy p8ojJImSeYvGcbsf1ymPpV+jypp8uGcPon7waHbAAWzCxccRfPKVXAicT jmbGAMKryvL0Q8d4sl7DCNY+bXlV3kye1s+5qTdg9C8a7BmHAfdI02gzf hy2H9P9EXtgWhbb0PNpY2T+hUBQ2N9aoU5mXsEJy6+kqmt7sGCc2+WCBG S9fNb62J3ANxB29jUPRoVBUP1bbbFDlgLXFzOp6JrUw+Ft9nIyZLudrB/ A==; X-IronPort-AV: E=McAfee;i="6400,9594,10392"; a="281973013" X-IronPort-AV: E=Sophos;i="5.92,230,1650956400"; d="scan'208";a="281973013" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jun 2022 23:33:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,230,1650956400"; d="scan'208";a="595108605" Received: from desktop-qn7n0nf.sh.intel.com (HELO localhost.localdomain) ([10.239.160.39]) by fmsmga007.fm.intel.com with ESMTP; 28 Jun 2022 23:33:53 -0700 From: Tong Wu To: ffmpeg-devel@ffmpeg.org Date: Wed, 29 Jun 2022 14:32:45 +0800 Message-Id: <20220629063246.183-2-tong1.wu@intel.com> X-Mailer: git-send-email 2.35.1.windows.2 In-Reply-To: <20220629063246.183-1-tong1.wu@intel.com> References: <20220629063246.183-1-tong1.wu@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/3] avfilter/vf_hwmap: get the AVHWDeviceType from outlink format 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: zn4dbTzbzH1K When a derive_device_type is not specified, the hwmap filter should be able to retrieve AVHWDeviceType from outlink->format and create corresponding hwdevice context. Signed-off-by: Tong Wu --- libavfilter/vf_hwmap.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/libavfilter/vf_hwmap.c b/libavfilter/vf_hwmap.c index 2e03dfc1fe..a0c2e134cf 100644 --- a/libavfilter/vf_hwmap.c +++ b/libavfilter/vf_hwmap.c @@ -72,26 +72,34 @@ static int hwmap_config_output(AVFilterLink *outlink) if (inlink->hw_frames_ctx) { hwfc = (AVHWFramesContext*)inlink->hw_frames_ctx->data; - if (ctx->derive_device_type) { - enum AVHWDeviceType type; + enum AVHWDeviceType type; + if (ctx->derive_device_type) { type = av_hwdevice_find_type_by_name(ctx->derive_device_type); if (type == AV_HWDEVICE_TYPE_NONE) { av_log(avctx, AV_LOG_ERROR, "Invalid device type.\n"); err = AVERROR(EINVAL); goto fail; } - - err = av_hwdevice_ctx_create_derived(&device, type, - hwfc->device_ref, 0); - if (err < 0) { - av_log(avctx, AV_LOG_ERROR, "Failed to created derived " - "device context: %d.\n", err); + } else { + type = av_hwdevice_get_type_by_pix_fmt(outlink->format); + if (type == AV_HWDEVICE_TYPE_NONE) { + av_log(avctx, AV_LOG_ERROR, "Could not get device type from " + "format %s.\n", av_get_pix_fmt_name(outlink->format)); + err = AVERROR(EINVAL); goto fail; } - device_is_derived = 1; } + err = av_hwdevice_ctx_create_derived(&device, type, + hwfc->device_ref, 0); + if (err < 0) { + av_log(avctx, AV_LOG_ERROR, "Failed to created derived " + "device context: %d.\n", err); + goto fail; + } + device_is_derived = 1; + desc = av_pix_fmt_desc_get(outlink->format); if (!desc) { err = AVERROR(EINVAL); From patchwork Wed Jun 29 06:32:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wu, Tong1" X-Patchwork-Id: 36520 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp121422pzh; Tue, 28 Jun 2022 23:34:51 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uzPEVWP6rk/tid4w/gI40TEo83ghuVD8dtPrXMf5tBab9Nob4zNDIvliQsYhwFWtf0MyD8 X-Received: by 2002:a17:907:3a11:b0:726:4573:1b6 with SMTP id fb17-20020a1709073a1100b00726457301b6mr1743669ejc.672.1656484491235; Tue, 28 Jun 2022 23:34:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656484491; cv=none; d=google.com; s=arc-20160816; b=RG9IseJG4yL75/hZQFqkdPN3kCdQvkt5Gv3QHpZm2EDvNw6u+wu6mmdSwzqWXV5woq VZCcngJiOnFdaaS9O6H3Sm6BsLBOBvsg3A7Pcdw6ih02UkacRRdkDldfSi+WEtPpWS/m ZNoH6csr8cci3mg5EedfYDXI/kx58i+QVYiaR3tSG6E3USEx8XFaXqdjG1iDBvML/Ecc 76beTlUzXaNwz8DCFxZ1LDNKCKup4KmPJMqARMmbWFvkC5Gw2n/cK2CBsPNTgG9UMNBF +7cnhWAL5ZM9CJTkbijywVcCA3lmHCkM6vqwKYnPIGe/Jrie7F8rFt9XZljMQ2u8MSit fXpA== 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=D5XxK0Xx9XpnBBotC6kX6Y0NpEbsq2Kj3/bWGztEscs=; b=C/vhJnw9Xpv06dcE8KAkW/FQg4O2zXt2DyArwOUHhWqxX/YNA1ErG1a9t101jc5txs 1UvCgfQ1kbWWZULRSJQN/JAYjN+9LDM9eV+ZFWID8zsYYu+hD5nF+sMMzIHM6+UYKSCu QjZXbCETIXG8MiEaUEhxZwxr/qZk9dZLbYDd2/YvyGtVGh+lrXHxRneBbrFoHw2FnlRx yB5qhbN17wfwuYR3JzmV8s4Z7jB0/rLmGyMMHqkFpHQSgaYDFGXRFQ7y3HYZ3gTODCHZ BHFIgvlnw9z5ZersDcNGvvmOgWIUww3kxHxv8YA7tcLdhVI0I7s+hd0jbC88luMkncQM d6tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=O+XEM7x7; 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 j17-20020a170906475100b00711db2a6646si16826259ejs.651.2022.06.28.23.34.28; Tue, 28 Jun 2022 23:34:51 -0700 (PDT) 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=O+XEM7x7; 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 F40C168B6D0; Wed, 29 Jun 2022 09:34:09 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 78DB168AF1C for ; Wed, 29 Jun 2022 09:34:02 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656484447; x=1688020447; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ifNYro8pdK3DM4EPpAnibbBqlSocyDLs5IfIGQwXivQ=; b=O+XEM7x7xhEdDEcv1f8MUYuvxQadXPDF0SW9O4fHhdU9MbRMBqMLf2pT gozywnPCGOYi0WgaMo01eibRAOHg4WVF01NhkhHUJYpLLkjd5/VHWSyvF /VpQ/kwFcodaolMSI0OVy6E567th+t2rtR/7WHCsAuw0P0MTHcFFm3MW0 /3WspQLQWwsk5gobIuLq9Q391b9kntaIyWFRF3qqzyExo31vIC4kzbbTa YpPgz+bepshi7lDfmuV6eKUDIJeL4cpzbLbXAhaDey06Do+r0hLx1Z7Tf oMal2TBAuuZLDLLRad0ip5pbkEgzdBEQXTIdDeNdk0nvfjbyCd8ljM2L6 g==; X-IronPort-AV: E=McAfee;i="6400,9594,10392"; a="281973017" X-IronPort-AV: E=Sophos;i="5.92,230,1650956400"; d="scan'208";a="281973017" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jun 2022 23:33:55 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,230,1650956400"; d="scan'208";a="595108616" Received: from desktop-qn7n0nf.sh.intel.com (HELO localhost.localdomain) ([10.239.160.39]) by fmsmga007.fm.intel.com with ESMTP; 28 Jun 2022 23:33:54 -0700 From: Tong Wu To: ffmpeg-devel@ffmpeg.org Date: Wed, 29 Jun 2022 14:32:46 +0800 Message-Id: <20220629063246.183-3-tong1.wu@intel.com> X-Mailer: git-send-email 2.35.1.windows.2 In-Reply-To: <20220629063246.183-1-tong1.wu@intel.com> References: <20220629063246.183-1-tong1.wu@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/3] avfilter/avfiltergraph: add an auto hwmap filter 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: x1qgmpacsnjL When two formats lists cannot be merged, a scale filter is auto-inserted. However, when it comes to hardware map, we have to manually add a hwmap filter to do the conversion. This patch introduces an auto hwmap filter to do the hwmap conversion automatically and modifies the negotiation structures, making it extensible for more auto filters. Signed-off-by: Tong Wu --- libavfilter/avfiltergraph.c | 183 ++++++++++++++++++++++++------------ libavfilter/formats.c | 26 ++++- libavfilter/formats.h | 9 +- 3 files changed, 150 insertions(+), 68 deletions(-) diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c index b7dbfc063b..0f12f2d802 100644 --- a/libavfilter/avfiltergraph.c +++ b/libavfilter/avfiltergraph.c @@ -393,6 +393,83 @@ static int formats_declared(AVFilterContext *f) return 1; } +static int insert_auto_filter(AVFilterContext **convert, AVFilterGraph *graph, + AVFilterLink *link, const AVFilterNegotiation *neg, + unsigned conv_step, int *converter_count, void *log_ctx) +{ + int ret; + const AVFilter *filter; + AVFilterContext *ctx; + AVFilterLink *inlink, *outlink; + char inst_name[30]; + const char *opts = neg->conversion_filters[conv_step].conversion_opts_offset == 0 ? NULL : + FF_FIELD_AT(char *, neg->conversion_filters[conv_step].conversion_opts_offset, *graph); + const char *name = neg->conversion_filters[conv_step].conversion_filter; + + if (graph->disable_auto_convert) { + av_log(log_ctx, AV_LOG_ERROR, + "The filters '%s' and '%s' do not have a common format " + "and automatic conversion is disabled.\n", + link->src->name, link->dst->name); + return AVERROR(EINVAL); + } + + if (!(filter = avfilter_get_by_name(name))) { + av_log(log_ctx, AV_LOG_ERROR, + "'%s' filter not present, cannot convert formats.\n", + name); + return AVERROR(EINVAL); + } + snprintf(inst_name, sizeof(inst_name), "auto_%s_%d", + name, (*converter_count)++); + ret = avfilter_graph_create_filter(&ctx, filter, inst_name, opts, NULL, graph); + if (ret < 0) + return ret; + + if ((ret = avfilter_insert_filter(link, ctx, 0, 0)) < 0) + return ret; + + if ((ret = filter_query_formats(ctx)) < 0) + return ret; + + inlink = ctx->inputs[0]; + outlink = ctx->outputs[0]; + av_assert0( inlink->incfg.formats->refcount > 0); + av_assert0( inlink->outcfg.formats->refcount > 0); + av_assert0(outlink->incfg.formats->refcount > 0); + av_assert0(outlink->outcfg.formats->refcount > 0); + if (outlink->type == AVMEDIA_TYPE_AUDIO) { + av_assert0( inlink-> incfg.samplerates->refcount > 0); + av_assert0( inlink->outcfg.samplerates->refcount > 0); + av_assert0(outlink-> incfg.samplerates->refcount > 0); + av_assert0(outlink->outcfg.samplerates->refcount > 0); + av_assert0( inlink-> incfg.channel_layouts->refcount > 0); + av_assert0( inlink->outcfg.channel_layouts->refcount > 0); + av_assert0(outlink-> incfg.channel_layouts->refcount > 0); + av_assert0(outlink->outcfg.channel_layouts->refcount > 0); + } + + *convert = ctx; + return 0; +} + +static int merge_auto_filter(AVFilterContext *convert, const AVFilterNegotiation *neg) +{ + int neg_step, ret; + AVFilterLink *inlink = convert->inputs[0]; + AVFilterLink *outlink = convert->outputs[0]; +#define MERGE(merger, link) \ + ((merger)->merge(FF_FIELD_AT(void *, (merger)->offset, (link)->incfg), \ + FF_FIELD_AT(void *, (merger)->offset, (link)->outcfg))) + for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) { + const AVFilterFormatsMerger *m = &neg->mergers[neg_step]; + if ((ret = MERGE(m, inlink)) <= 0 || + (ret = MERGE(m, outlink)) <= 0) + break; + } + return ret; +} + /** * Perform one round of query_formats() and merging formats lists on the * filter graph. @@ -433,7 +510,7 @@ static int query_formats(AVFilterGraph *graph, void *log_ctx) for (j = 0; j < filter->nb_inputs; j++) { AVFilterLink *link = filter->inputs[j]; const AVFilterNegotiation *neg; - unsigned neg_step; + unsigned neg_step, conv_step; int convert_needed = 0; if (!link) @@ -469,68 +546,50 @@ static int query_formats(AVFilterGraph *graph, void *log_ctx) } if (convert_needed) { - AVFilterContext *convert; - const AVFilter *filter; - AVFilterLink *inlink, *outlink; - char inst_name[30]; - const char *opts; - - if (graph->disable_auto_convert) { - av_log(log_ctx, AV_LOG_ERROR, - "The filters '%s' and '%s' do not have a common format " - "and automatic conversion is disabled.\n", - link->src->name, link->dst->name); - return AVERROR(EINVAL); - } - - /* couldn't merge format lists. auto-insert conversion filter */ - if (!(filter = avfilter_get_by_name(neg->conversion_filter))) { - av_log(log_ctx, AV_LOG_ERROR, - "'%s' filter not present, cannot convert formats.\n", - neg->conversion_filter); - return AVERROR(EINVAL); - } - snprintf(inst_name, sizeof(inst_name), "auto_%s_%d", - neg->conversion_filter, converter_count++); - opts = FF_FIELD_AT(char *, neg->conversion_opts_offset, *graph); - ret = avfilter_graph_create_filter(&convert, filter, inst_name, opts, NULL, graph); - if (ret < 0) - return ret; - if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0) - return ret; - - if ((ret = filter_query_formats(convert)) < 0) - return ret; + for (conv_step = 0; conv_step < neg->nb_conversion_filters; conv_step++) { + AVFilterContext *convert; + ret = insert_auto_filter(&convert, graph, link, neg, + conv_step, &converter_count, log_ctx); + if (ret < 0) { + av_log(log_ctx, AV_LOG_ERROR, "Failed to insert an auto filter.\n"); + return ret; + } - inlink = convert->inputs[0]; - outlink = convert->outputs[0]; - av_assert0( inlink->incfg.formats->refcount > 0); - av_assert0( inlink->outcfg.formats->refcount > 0); - av_assert0(outlink->incfg.formats->refcount > 0); - av_assert0(outlink->outcfg.formats->refcount > 0); - if (outlink->type == AVMEDIA_TYPE_AUDIO) { - av_assert0( inlink-> incfg.samplerates->refcount > 0); - av_assert0( inlink->outcfg.samplerates->refcount > 0); - av_assert0(outlink-> incfg.samplerates->refcount > 0); - av_assert0(outlink->outcfg.samplerates->refcount > 0); - av_assert0( inlink-> incfg.channel_layouts->refcount > 0); - av_assert0( inlink->outcfg.channel_layouts->refcount > 0); - av_assert0(outlink-> incfg.channel_layouts->refcount > 0); - av_assert0(outlink->outcfg.channel_layouts->refcount > 0); - } -#define MERGE(merger, link) \ - ((merger)->merge(FF_FIELD_AT(void *, (merger)->offset, (link)->incfg), \ - FF_FIELD_AT(void *, (merger)->offset, (link)->outcfg))) - for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) { - const AVFilterFormatsMerger *m = &neg->mergers[neg_step]; - if ((ret = MERGE(m, inlink)) <= 0 || - (ret = MERGE(m, outlink)) <= 0) { - if (ret < 0) - return ret; - av_log(log_ctx, AV_LOG_ERROR, - "Impossible to convert between the formats supported by the filter " - "'%s' and the filter '%s'\n", link->src->name, link->dst->name); - return AVERROR(ENOSYS); + ret = merge_auto_filter(convert, neg); + if (ret < 0) + return ret; + else if (ret > 0) + break; + else if (conv_step < neg->nb_conversion_filters - 1) { + AVFilterLink *inlink = convert->inputs[0]; + AVFilterLink *outlink = convert->outputs[0]; + av_log(log_ctx, AV_LOG_VERBOSE, + "Impossible to convert between the formats supported by the filter " + "'%s' and the filter '%s', try another conversion filter.\n", + link->src->name, link->dst->name); + unsigned dstpad_idx = outlink->dstpad - outlink->dst->input_pads; + converter_count--; + /* re-hookup the link */ + inlink->dst = outlink->dst; + inlink->dstpad = &outlink->dst->input_pads[dstpad_idx]; + outlink->dst->inputs[dstpad_idx] = inlink; + if (outlink->outcfg.formats) + ff_formats_changeref(&outlink->outcfg.formats, + &inlink->outcfg.formats); + if (outlink->outcfg.samplerates) + ff_formats_changeref(&outlink->outcfg.samplerates, + &inlink->outcfg.samplerates); + if (outlink->outcfg.channel_layouts) + ff_channel_layouts_changeref(&outlink->outcfg.channel_layouts, + &inlink->outcfg.channel_layouts); + /* remove the previous auto filter */ + convert->inputs[0] = NULL; + convert->outputs[0]->dst = NULL; + avfilter_free(convert); + } else { + av_log(log_ctx, AV_LOG_ERROR, + "Impossible to convert between the formats supported by the filter " + "'%s' and the filter '%s'\n", link->src->name, link->dst->name); } } } diff --git a/libavfilter/formats.c b/libavfilter/formats.c index e8c2888c0c..fee10fa0ee 100644 --- a/libavfilter/formats.c +++ b/libavfilter/formats.c @@ -326,18 +326,36 @@ static const AVFilterFormatsMerger mergers_audio[] = { }, }; +static const AVFilterFormatsFilter filters_video[] = { + { + .conversion_filter = "scale", + .conversion_opts_offset = offsetof(AVFilterGraph, scale_sws_opts), + }, + { + .conversion_filter = "hwmap", + .conversion_opts_offset = 0, + } +}; + +static const AVFilterFormatsFilter filters_audio[] = { + { + .conversion_filter = "aresample", + .conversion_opts_offset = offsetof(AVFilterGraph, aresample_swr_opts), + } +}; + static const AVFilterNegotiation negotiate_video = { .nb_mergers = FF_ARRAY_ELEMS(mergers_video), .mergers = mergers_video, - .conversion_filter = "scale", - .conversion_opts_offset = offsetof(AVFilterGraph, scale_sws_opts), + .nb_conversion_filters = FF_ARRAY_ELEMS(filters_video), + .conversion_filters = filters_video, }; static const AVFilterNegotiation negotiate_audio = { .nb_mergers = FF_ARRAY_ELEMS(mergers_audio), .mergers = mergers_audio, - .conversion_filter = "aresample", - .conversion_opts_offset = offsetof(AVFilterGraph, aresample_swr_opts), + .nb_conversion_filters = FF_ARRAY_ELEMS(filters_audio), + .conversion_filters = filters_audio, }; const AVFilterNegotiation *ff_filter_get_negotiation(AVFilterLink *link) diff --git a/libavfilter/formats.h b/libavfilter/formats.h index 22224dce2d..9943fb8aa3 100644 --- a/libavfilter/formats.h +++ b/libavfilter/formats.h @@ -330,6 +330,11 @@ typedef struct AVFilterFormatMerger { int (*can_merge)(const void *a, const void *b); } AVFilterFormatsMerger; +typedef struct AVFilterFormatFilter { + const char *conversion_filter; + unsigned conversion_opts_offset; +} AVFilterFormatsFilter; + /** * Callbacks and properties to describe the steps of a format negotiation. * @@ -418,8 +423,8 @@ typedef struct AVFilterFormatMerger { typedef struct AVFilterNegotiation { unsigned nb_mergers; const AVFilterFormatsMerger *mergers; - const char *conversion_filter; - unsigned conversion_opts_offset; + unsigned nb_conversion_filters; + const AVFilterFormatsFilter *conversion_filters; } AVFilterNegotiation; const AVFilterNegotiation *ff_filter_get_negotiation(AVFilterLink *link);