From patchwork Fri Dec 17 20:04:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aman Karmani X-Patchwork-Id: 32694 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp1914289iog; Fri, 17 Dec 2021 12:05:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJy8vjLGAIPIKktLPTn0Wi170j+BTgymFPycNQuspq57cKALzv7jacTn+7hhhG+qkgjqcQOc X-Received: by 2002:a17:906:58d5:: with SMTP id e21mr3950616ejs.540.1639771512330; Fri, 17 Dec 2021 12:05:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639771512; cv=none; d=google.com; s=arc-20160816; b=Az3PYuIH1IcoK5XJf6TMhFawR9ZEwQA5nHt82ZPMv/0eSuQWKdskYAPe5Q5vmOAzhJ 0ieXSyVjhb5Tf8d4aL5yFyvjgq4L62PRSqNkfCqrP1rpRkLOyeOJQPgV+qfmC3fWt81X FM64lM6ogjKPGD2MW01bNuw5PDA9IaHiP8C5KaAkgURYiiL3aAVp8Pbx1HWNJXfNXxci 6eWbbcy/OYvRgTZOJR5nRcE6Jm4fj8ukOjeSePgwoL+RtdktgDu+j7EclxAlGixriWS4 F1UpOrVI9x5aLKRCf5Vjk9IGMxqJ3kiaEuTRLBhJA03cz2k4p0UdDxHvynSqJ8LHUPBn dZ+Q== 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=8gfnr3SjzwJUsW5xkCdlp4vnMkSiEekyLzqFaOIa554=; b=YMN1ate6sp8HmLfOyRL+hZrfrxDqQzd2Q9vI7ZPYkn1E0Y4wQ0K+6qCLzPV48PIcUW R4cvTf2Zq+hARrXxvdfM7HV6o+ixYfm+vscanMT/nZJWsPJm1uJUza0+fT1W+Suh1N1s k7AD+SBZp0nfJz4+FzfgPeyvjXS0fKX32e8BOdYgslNxL8OnloQXmVPv1FmumLfGQRjl qgsgyt+PYbO4dqVW57y8WbqMKXO89Ru13LHzJZpg3FFOQss2X3KCKk0OrPlZonDtjaz2 /tx4sd+SqY2TZKHuu0hdl5XdMJb60SGcFWFDBTd8AIalZReTEB/8WuLsReUsiTQkRREs JzBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Nn+MMee+; 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 hr32si6444934ejc.956.2021.12.17.12.05.12; Fri, 17 Dec 2021 12:05:12 -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=@gmail.com header.s=20210112 header.b=Nn+MMee+; 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 1E45168ACAE; Fri, 17 Dec 2021 22:04:41 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1125468A8F1 for ; Fri, 17 Dec 2021 22:04:31 +0200 (EET) Received: by mail-pl1-f182.google.com with SMTP id u17so2768940plg.9 for ; Fri, 17 Dec 2021 12:04:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xiWxaPzyrnydAyOwz+8aknzAfeTSTLX6PY3H1TOJNe4=; b=Nn+MMee+BBG2+CXV6xU5oFoC1IZTAfqPBQ1VpPpZiEJ/I3BEUodQPKsxIkxnrQtwdo 8VqTW4bHPYW7NPfXcUCPRaQUPpIWvJOOexqf7CdccaI45BQpbA+2epfUjyWQ7SPFBrp1 8CB9Ir2/IN6MHzOkTPFFjmbqhjRFITmZ3AcHzPOt4eY6Ds9W7pVcCGL1K59GKARHgaAR 4bTdIhynO9gBf2sJHluX3yorupU9uwkb31Ynk9LxdIRhYexWZ5LvtGKv2O0RNk+6T7Tw nG7jRWlYkF2ep9WsZDWAzgeYhoq+6gdAj605vmNmYxTKj8Su7pHXKvEqow5gcO1wM1tP LihA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=xiWxaPzyrnydAyOwz+8aknzAfeTSTLX6PY3H1TOJNe4=; b=B67ZmEWBqtgA3liErvmulQhejdtCBBP3piKhVH8MnBC9uzwwSlTIZ2dc8lUZzbpB7h i3VhOJKs5qeCXLSkS5SC7+o2lv1DkRxNn0R/ehD6EPGttCqFSeQBSKKI87K+mkBu6XpY igWdNt5A9MGyAP7jV8dl1ugpiQB19bo9LuWRH3lXb49na3+Hk60fJjTagDHI7q/if45o rUIRBrdo15cqnM9pEX3rd9mQudWoNSUA2134OH82a7KRCWAISrr28umRs4WTG9B9KK3G Ss7CUHnOmk8mQUgxHKq+mYv8ceHmgxurO9BmL5qW7bm9IywkzsADAG2J1bU5eV+lvMrR tw3A== X-Gm-Message-State: AOAM531TseI2Pw43SfOSq6u2nWjdRboSx8kfzR2OB6yXDdha7ffPs0Pq P9Ji1AocV6QYVSVN94qdcg7j3iejwhO51g== X-Received: by 2002:a17:902:8e87:b0:148:a2e8:2c5f with SMTP id bg7-20020a1709028e8700b00148a2e82c5fmr4873543plb.174.1639771468955; Fri, 17 Dec 2021 12:04:28 -0800 (PST) Received: from tmm1-imac.lan ([2600:8802:5501:308f:68db:dc19:cd7b:1dc7]) by smtp.gmail.com with ESMTPSA id k2sm8767301pgh.11.2021.12.17.12.04.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Dec 2021 12:04:28 -0800 (PST) From: Aman Karmani To: ffmpeg-devel@ffmpeg.org Date: Fri, 17 Dec 2021 12:04:17 -0800 Message-Id: <20211217200418.68942-4-ffmpeg@tmm1.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211217200418.68942-1-ffmpeg@tmm1.net> References: <20211217200418.68942-1-ffmpeg@tmm1.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 4/5] avfilter: add metal utilities 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: aman@tmm1.net Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: fPXNseau8o6a From: Aman Karmani Reviewed-by: Ridley Combs Signed-off-by: Aman Karmani --- libavfilter/metal/utils.h | 35 +++++++++++++++++++ libavfilter/metal/utils.m | 73 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 libavfilter/metal/utils.h create mode 100644 libavfilter/metal/utils.m diff --git a/libavfilter/metal/utils.h b/libavfilter/metal/utils.h new file mode 100644 index 0000000000..bd0319f63c --- /dev/null +++ b/libavfilter/metal/utils.h @@ -0,0 +1,35 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVFILTER_METAL_UTILS_H +#define AVFILTER_METAL_UTILS_H + +#include +#include + +void ff_metal_compute_encoder_dispatch(id device, + id pipeline, + id encoder, + NSUInteger width, NSUInteger height); + +CVMetalTextureRef ff_metal_texture_from_pixbuf(void *avclass, + CVMetalTextureCacheRef textureCache, + CVPixelBufferRef pixbuf, + int plane, + MTLPixelFormat format); +#endif /* AVFILTER_METAL_UTILS_H */ diff --git a/libavfilter/metal/utils.m b/libavfilter/metal/utils.m new file mode 100644 index 0000000000..759ebedfba --- /dev/null +++ b/libavfilter/metal/utils.m @@ -0,0 +1,73 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/log.h" +#include + +void ff_metal_compute_encoder_dispatch(id device, + id pipeline, + id encoder, + NSUInteger width, NSUInteger height) +{ + [encoder setComputePipelineState:pipeline]; + NSUInteger w = pipeline.threadExecutionWidth; + NSUInteger h = pipeline.maxTotalThreadsPerThreadgroup / w; + MTLSize threadsPerThreadgroup = MTLSizeMake(w, h, 1); + BOOL fallback = YES; + if (@available(macOS 10.15, iOS 11, tvOS 14.5, *)) { + if ([device supportsFamily:MTLGPUFamilyCommon3]) { + MTLSize threadsPerGrid = MTLSizeMake(width, height, 1); + [encoder dispatchThreads:threadsPerGrid threadsPerThreadgroup:threadsPerThreadgroup]; + fallback = NO; + } + } + if (fallback) { + MTLSize threadgroups = MTLSizeMake((width + w - 1) / w, + (height + h - 1) / h, + 1); + [encoder dispatchThreadgroups:threadgroups threadsPerThreadgroup:threadsPerThreadgroup]; + } +} + +CVMetalTextureRef ff_metal_texture_from_pixbuf(void *ctx, + CVMetalTextureCacheRef textureCache, + CVPixelBufferRef pixbuf, + int plane, + MTLPixelFormat format) +{ + CVMetalTextureRef tex = NULL; + CVReturn ret; + + ret = CVMetalTextureCacheCreateTextureFromImage( + NULL, + textureCache, + pixbuf, + NULL, + format, + CVPixelBufferGetWidthOfPlane(pixbuf, plane), + CVPixelBufferGetHeightOfPlane(pixbuf, plane), + plane, + &tex + ); + if (ret != kCVReturnSuccess) { + av_log(ctx, AV_LOG_ERROR, "Failed to create CVMetalTexture from image: %d\n", ret); + return NULL; + } + + return tex; +}