From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id C0F5342365 for ; Fri, 17 Dec 2021 20:05:09 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DF2C668AF08; Fri, 17 Dec 2021 22:04:40 +0200 (EET) 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-Google-Smtp-Source: ABdhPJzSbwTFe+2ESLtXmJ+A9vNr86Xi3j3OqX4FEoRlIc8Q9alM3JRnCfowEZDTpDrRcMPBrWcJbw== 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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; +} -- 2.33.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".