From patchwork Sun Jul 3 12:57:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wu, Tong1" X-Patchwork-Id: 36630 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp2206464pzh; Sun, 3 Jul 2022 05:58:33 -0700 (PDT) X-Google-Smtp-Source: AGRyM1v6Cnjd0df/kotj+uapeVungJQValqWIqQegb+sOsc9IIAQJJXnJjBtVSjldxKgx6qGIG1M X-Received: by 2002:a05:6402:452:b0:434:a373:f9f8 with SMTP id p18-20020a056402045200b00434a373f9f8mr31961220edw.290.1656853113093; Sun, 03 Jul 2022 05:58:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656853113; cv=none; d=google.com; s=arc-20160816; b=NYQuWn4a+JhNYBOTT86IPPGMjTe/Q8jnbJ1E9GSJHBrz1WzfpNoofPsvH95OvWrFeu f3N+8D4ebOoIGO2MmSbvPNTj/MCoqIy1NdMiJJ8iLorGSGKeP0LdT6lxAk5tzZFbjjk4 c+/bP2cUfheg6IyUaQajoh/a3muaD0E/+uv22+7C+YWTZATi4qpDAtn1oxYUcyCozjIJ Xxvdwy2sUAiJ38Ky0kKWX0L6CU6IfiNolqu0hNrBZmpqyRNeA4HS61wk8TtkV/Zqv+S9 TtlazePChA1ibFUcIGOiEQ9EgMWLDAcd+vcIwoa+dLHAkkStvN9ClZ+Y9GsVImsvsWSd 1QMA== 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=M+H82gMRMl2HLP30tiiRnMHBU5MBf+LmRnlL8zVKIFU=; b=mbipC07lrw1iaYjjgR+RdFfM0xRl5R+OhAG8sFVdaaNBv8pQu+R1g6skTNBH0fLApj gTtX5IwZAjpLkMS2WgSZZ4nqmsH44Kimx3Yu+cuuL0ujG0cbRscnlvmng4CuLUG3/SSG Pd2V85B3uefJ0HDTkcOShQQV8AUD1XswAoXr4/RtEdul4FXnhssubAWYAgll8ySDMS4F o29pSKKhVr4Hh6pKs7Adj50BSu8kxMStwNzA44xNPE0pp/hwimZwdJGnmdbQzNEG8oAx vPNBTKazQNDdhjdERQic8uydtCDgSkYa+qSCQXXRXjvZzx2Jaa8QJK6QI4nVmh79crvx v1ZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=X6KNkdMj; 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 gx13-20020a1709068a4d00b00718d0b634e0si14843607ejc.660.2022.07.03.05.58.32; Sun, 03 Jul 2022 05:58:33 -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=X6KNkdMj; 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 608A468B8C5; Sun, 3 Jul 2022 15:58:28 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 212DF68B971 for ; Sun, 3 Jul 2022 15:58:20 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656853106; x=1688389106; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=X67rUWLColLJTpR48nmyqHn2fB4QpDP+iB34C77FqVs=; b=X6KNkdMjLAdGt5PBzxYXgy6kDaH4w4v3LYRzkrSxdJe7i1VjbcWwNasH sfMwnh7ASmqoOQwq3ZHmJ6CysywcS1RwaVCC9qllokeYcr3vrtElEd5qM FpPvxme0w3gZjkfSTN+O/aK0v81oX3+iA9TlkC4eJ0fQCiZXUrLMz2Oxh KyHZ5DivnTkuyQhIxQoDnsXvjukHmb4MmzCKwIGX+vtSELedcS7Pk7sWE oST3rxF69Yz20chIGuw+hF2t+HG0vWMijBEZLwJ8Bhjna4lEFp7pmtpf0 NYJVDYEO7xAU2Os3dsMGR0cqzPMVP2EOnI8ZKAnAhFx0aaTSJ+buaGj3L Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10396"; a="280500544" X-IronPort-AV: E=Sophos;i="5.92,241,1650956400"; d="scan'208";a="280500544" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jul 2022 05:58:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,241,1650956400"; d="scan'208";a="542206167" Received: from desktop-qn7n0nf.sh.intel.com (HELO localhost.localdomain) ([10.239.160.39]) by orsmga003.jf.intel.com with ESMTP; 03 Jul 2022 05:58:17 -0700 From: Tong Wu To: ffmpeg-devel@ffmpeg.org Date: Sun, 3 Jul 2022 20:57:12 +0800 Message-Id: <20220703125714.1230-1-tong1.wu@intel.com> X-Mailer: git-send-email 2.35.1.windows.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 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: vBeJM3wbdMUt Add a function to get the corresponding AVHWDeviceType from a given hardware pixel format. Signed-off-by: Tong Wu --- libavutil/hwcontext.c | 11 +++++++++++ libavutil/hwcontext.h | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c index ab9ad3703e..4d14cb2cb4 100644 --- a/libavutil/hwcontext.c +++ b/libavutil/hwcontext.c @@ -80,6 +80,17 @@ static const char *const hw_type_names[] = { [AV_HWDEVICE_TYPE_VULKAN] = "vulkan", }; +enum AVHWDeviceType av_hwdevice_get_type_by_pix_fmt(enum AVPixelFormat fmt) +{ + for (int i = 0; hw_table[i]; i++) { + for (int 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..49f3a799ed 100644 --- a/libavutil/hwcontext.h +++ b/libavutil/hwcontext.h @@ -229,6 +229,18 @@ typedef struct AVHWFramesContext { int width, height; } AVHWFramesContext; +/** + * Get the device type by a given pixel format. + * + * This function only returns a preferred device type which supports the given + * pixel format. There is no guarantee that the device type is unique. + * + * @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 Sun Jul 3 12:57:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wu, Tong1" X-Patchwork-Id: 36631 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp2206514pzh; Sun, 3 Jul 2022 05:58:42 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sRx4eN4gG+u4Z0oVZl7JDApLdxDGffrGXRgeHgKB1UhncPeOmNPaN9VSaJgvhyjr8EMuYd X-Received: by 2002:a17:907:3e86:b0:6f5:917:10cc with SMTP id hs6-20020a1709073e8600b006f5091710ccmr24005227ejc.53.1656853121940; Sun, 03 Jul 2022 05:58:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656853121; cv=none; d=google.com; s=arc-20160816; b=SPRmGzuy1s1OSz20in9j6Yv3M483n34XBwCjS012HS9U5BLuGKGvgdrQyG9w/Ht/DQ fEfsvU+eLch4Kqe5/r9auGS0G9kHTNYBee3ZrSr7FoAI9Ns2mnjrGy7Xer+oNecBeXG7 3ebcNgHh6C9pCojx4jHKR+iX49FI//ca/rat9CdSIXuQOSqVSbvY7CXj2zlFVM9T3/65 7yoEdEkdgoz6xWhFrTyJHaQMuQ8Rzn8EKlOXGFsZC5/S1f0gUSiXH+Fenno2GuJb89he LZAjizfbv7klibuVfQnL7jL3571juSNqs957ulS526wWIW0fiWKlCJ8gK0wSQnJZv0T1 UEiA== 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=njHaXZf5l/1L6XsThpyr0BRHVMD0iicBBJG2r5JBKJY=; b=NxuxhTONQ3Ib/AM3lKAvgI+rr7AxXhyz5FybuqSZr2F3FYP01ezWt66V5tvMlTC3a8 zNvAI7Z0qUzzIiQ4iCRVfuiZBxZlFAXCk8f3sHzKjsz3qCTs+KahlonkeeHmokvu/g+m wKcj70XHt8f47dRAHvKGojJF9e2FnCHTCFb7t87yQFnKTVYBGwOEaebj0F8R3ayKabNl ZCVZ9eY34YF0tMBR9R+qbFMm7l24pWW66uHWc9Tz9beUbRbx8ATyjI/1TsFoeQptQBDa x7iRG5WqQloSisIAXCndJTEqDPCVGXovakMfWogHoZwUDE3+wSe1MimnFlx01Hr5Myct lDyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=QleO37xF; 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 qw7-20020a1709066a0700b00709521d4b92si10409105ejc.397.2022.07.03.05.58.41; Sun, 03 Jul 2022 05:58:41 -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=QleO37xF; 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 4B3CA68B9C7; Sun, 3 Jul 2022 15:58:30 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C9C1268B8F2 for ; Sun, 3 Jul 2022 15:58:22 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656853108; x=1688389108; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fPZ87X4OL+EVQ1rCHSoguqWAsxkiSM5XnwOhtEYcza0=; b=QleO37xFNyPy+MC5gEaMbw2nZaGrupWv+xQET1FFGC9oMBbthxnzw8JK jgzTcSH02hm6BeE7Rn88vOpHll+nSbG2k4zd8Xa8u2K9afUbHjc7xoqWU 0BeNgDFgYWfHI2NLWgqxeO1M8z4m9TRp2lbUEYG4snRGRD/knSFPTe0H/ 3L8htEFGQH9VyXI1XHUa2zuwbXQ07+QKlxmW9rDRyD21aQupuVXS7nvUU Y3QJmxT72MS876q3tAqqdHAt5T9kZesl34J4wYnlc3A/zmYq2fs3RJopD u9bUPuZR0VZJ6yxrhBLEAv8D9C2S2eV9Jeri9jxasGp5URe8vx/QUxpZJ g==; X-IronPort-AV: E=McAfee;i="6400,9594,10396"; a="280500547" X-IronPort-AV: E=Sophos;i="5.92,241,1650956400"; d="scan'208";a="280500547" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jul 2022 05:58:19 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,241,1650956400"; d="scan'208";a="542206170" Received: from desktop-qn7n0nf.sh.intel.com (HELO localhost.localdomain) ([10.239.160.39]) by orsmga003.jf.intel.com with ESMTP; 03 Jul 2022 05:58:18 -0700 From: Tong Wu To: ffmpeg-devel@ffmpeg.org Date: Sun, 3 Jul 2022 20:57:13 +0800 Message-Id: <20220703125714.1230-2-tong1.wu@intel.com> X-Mailer: git-send-email 2.35.1.windows.2 In-Reply-To: <20220703125714.1230-1-tong1.wu@intel.com> References: <20220703125714.1230-1-tong1.wu@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 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: lPKR6cOZD3c1 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 | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/libavfilter/vf_hwmap.c b/libavfilter/vf_hwmap.c index 2e03dfc1fe..328395e352 100644 --- a/libavfilter/vf_hwmap.c +++ b/libavfilter/vf_hwmap.c @@ -70,18 +70,27 @@ static int hwmap_config_output(AVFilterLink *outlink) device_is_derived = 0; if (inlink->hw_frames_ctx) { + enum AVHWDeviceType type; hwfc = (AVHWFramesContext*)inlink->hw_frames_ctx->data; if (ctx->derive_device_type) { - enum AVHWDeviceType 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; } + } 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; + } + } + if (!device || ctx->derive_device_type) { err = av_hwdevice_ctx_create_derived(&device, type, hwfc->device_ref, 0); if (err < 0) { From patchwork Sun Jul 3 12:57:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wu, Tong1" X-Patchwork-Id: 36632 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8b27:b0:88:1bbf:7fd2 with SMTP id l39csp2206574pzh; Sun, 3 Jul 2022 05:58:51 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vEcA8QptoO7rZd+m7rLwwgkVwNbgGUy3sdts/xIf3U5IuOtlmFbEaKnmv9EP4CBwBgTcsp X-Received: by 2002:a05:6402:2788:b0:435:d40e:c660 with SMTP id b8-20020a056402278800b00435d40ec660mr31575054ede.424.1656853130776; Sun, 03 Jul 2022 05:58:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656853130; cv=none; d=google.com; s=arc-20160816; b=E74oyTOMCQZlqhTkaIROXkmWnD2L7doW7onWlUabDtHNtqgU4+LlRKYjr7gUl/Vusx dmsIAgGFh/sKgc1UzQYblHQzEzZdbRYlhtT7fM+5DjmCg3y/BrKSkHnR1OauoKGscOUx 4gMcWYz7oQl2lSnOefuH/NLrkIETwTuHJzOAsWd7FvriauZbqk4MzkGqoiyHNJl/aJuQ fNyaX2262S8rnjITOSOVru78orDwz5QmnmgkLu3DqFdIeajfSN2Rn3gEGR682lJC1ixg hbLwscCn1fAWYE0FdxUEAPzccxhb06f0nfL8/xDYjK4mGR9OuO/bnG/lDAbb43Z+S0A1 NJIA== 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=AB8597lkubAYqeRso1Y8VyAf6u7rJ4ULcTG79YB78d2LjFZiScNR0imiDU8iAPka3b M5wufTvq5cxypzAvWd+MAfylxV9gq9b+aaP5rltQRTO3VOvtSMn/29FGul6z7p+E/K6h UwOXj4dqBPdd44XQXXuQm9cvspick8vg+Qd4vKyX8nuDaceBTvhxmTh/zc/ro3KM21hX ij0t6D0kXt1+YDKGHZXkMU2DOhiI1BWo4pnYKpdDUFktKnbkgtX8tHw8kBMWXyExzfkF L1rFV2Cq7NgZgxiPPMWEHEOsfzFux0JZnjolnBlWiPglT+lM5AspUFqnZpYayplEJfzj ootg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@intel.com header.s=Intel header.b=fR3P4FCU; 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 md27-20020a170906ae9b00b0071bbadaaf42si3682417ejb.425.2022.07.03.05.58.50; Sun, 03 Jul 2022 05:58:50 -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=fR3P4FCU; 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 49DDF68B9D1; Sun, 3 Jul 2022 15:58:35 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 08E9968B985 for ; Sun, 3 Jul 2022 15:58:26 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656853113; x=1688389113; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ifNYro8pdK3DM4EPpAnibbBqlSocyDLs5IfIGQwXivQ=; b=fR3P4FCUA74+ziP1LKh2GyCIJj4psjSLN2Wxb02o+LeNeJKdrm8EMog8 nr/NIyk5xqhRrTh7a0eyeQqKKgFn6/sv/4DnL6l0vuCuVEdpLXz5+VfJi bkkq0qUfxeL/3QVZ0z4SycyF4q19Ta+krq+uvRm1i7xFsGgBFfqWuvn8O AyIG5dyQwTZtsLcjvKQS3lBH4IpklTL6Zb0QC6s5nrCc5wF4Oma4PCI/1 S+CKS2s2yh5s7pskNJ6L7Aaq8t0bLCsS83zUzztsa/0bgfNZIRyCKeVcl H3I7/M83juhFtnz2f6u/SEgnLPlzPM3EJe3AWfxkXlLd7A4umN7Dtx/Ei g==; X-IronPort-AV: E=McAfee;i="6400,9594,10396"; a="280500552" X-IronPort-AV: E=Sophos;i="5.92,241,1650956400"; d="scan'208";a="280500552" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jul 2022 05:58:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,241,1650956400"; d="scan'208";a="542206177" Received: from desktop-qn7n0nf.sh.intel.com (HELO localhost.localdomain) ([10.239.160.39]) by orsmga003.jf.intel.com with ESMTP; 03 Jul 2022 05:58:19 -0700 From: Tong Wu To: ffmpeg-devel@ffmpeg.org Date: Sun, 3 Jul 2022 20:57:14 +0800 Message-Id: <20220703125714.1230-3-tong1.wu@intel.com> X-Mailer: git-send-email 2.35.1.windows.2 In-Reply-To: <20220703125714.1230-1-tong1.wu@intel.com> References: <20220703125714.1230-1-tong1.wu@intel.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 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: h7/fOMuOEkaB 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);