From patchwork Wed Dec 22 00:07:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ridley Combs X-Patchwork-Id: 32817 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5921358iog; Tue, 21 Dec 2021 16:15:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJxJamCkFu3rQyby/Zm4pKt9qo10fjThC38P40/6KR5t1aT1AJcy7XsPhrU9KZ0dXsNpWQ3K X-Received: by 2002:a17:906:a0d:: with SMTP id w13mr580005ejf.642.1640132125985; Tue, 21 Dec 2021 16:15:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640132125; cv=none; d=google.com; s=arc-20160816; b=nMM67KMeQsIOj68rXaKY//LcSG9O9t7C4kyGEl31fbsGeGBoioLSy6Bx5nDuIj33Xc TlXMzeG/t4KDo9dtZgl2ctwF1+48DEMeYrbADkN8T3JfGcg1ayeI7J+x1Q0DEdc4MAaR viABBsCU/sVrMwbMdmDk1gjgvC8WgdcD1uEQyh22yuRMVwkPYSZKAXpDX0AC2tzm6IKm RfybgQ/QLF3FcV6ZYEqSkT1x9ETEME5ZVC52rstEdS/2d6jk/BK0WRYiWj+aH1XNpSDE zGAZOk4WHruOcYNYqu1w78n7oU4poH9OeLAhiK4ZIRzmo0huYhkh5RGmNEfAan2u7uxQ 2Y5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding: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=s49+9FJgIctPRrdqS2Z5uR48+jdzFWDjR4tUKE7EgYU=; b=KfnMspOFIeHxefREGNL2hHPgH3sPBV/TDRaG8djicfDV79KO5pfqw1EV3z8k8mco+U KOZrmYn5liaxd7VnhjAjd/m7HpYzMOFqmMVPhenTnTRBxQ9jEpbNoJt8t3ZduxFGtEmZ 1/HEZ6ME7/hn8xoph7pGzjnh4VDkW0o2DU14zr+aU/Aw4itDEWi749X4R6NffMDYzab+ c9eV5kvrsbeSy/XikFkujvk3iwQLNFQX1phwZqo8+2egXRGXFtETibl7bsWwnXnKUQmX EkRm0XnBN2oTtiUfdPs90WEZxqDTCINeA3JhGgrZqT3jimXxSCUJKRnDPSZ+KapS+pEe W3jQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@rcombs.me header.s=google header.b=SCkHa5dj; 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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=rcombs.me Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id gb9si189039ejc.256.2021.12.21.16.15.25; Tue, 21 Dec 2021 16:15:25 -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=@rcombs.me header.s=google header.b=SCkHa5dj; 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; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=rcombs.me Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 89AF268AF04; Wed, 22 Dec 2021 02:15:23 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk1-f182.google.com (mail-qk1-f182.google.com [209.85.222.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2271E68AE6F for ; Wed, 22 Dec 2021 02:15:17 +0200 (EET) Received: by mail-qk1-f182.google.com with SMTP id f138so695430qke.10 for ; Tue, 21 Dec 2021 16:15:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rcombs.me; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=Kz9saHUQ0iQh2XPQGyltBgzqHch+WMktFlSDzs1jj1g=; b=SCkHa5djERGhnl16OaWZz5FrAavyuajK1kWQHtsL8kI3qofcQ/393UvKAFsQlvw3jA z7wrRW/H+qTOvdtsd3bTxLKIXbN3SO773Q01fEZKWBRKb6gqcL+fcVDevD0U6MDlpJqk lFA8m1Bn3uhqn3JWBBnD4fXjUtI1xRr5O3IyJN0G2xE6umwYrEuAmNqJN4y5gM7EDyxF C1pYG4x372Ij88pBlVFnPazE75yFsQKfxdUNy+sKPASkUnbSauCT+c2gWrqeMeQV8TUv f4eiblUVD5hW47LHfMnsuVbWxyWwEQf+iZ0arMGKYE9awdvbxc2vDAlPHU4h4V4lWNcB KWWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Kz9saHUQ0iQh2XPQGyltBgzqHch+WMktFlSDzs1jj1g=; b=Pq8tuBdTDjm+ICU8/MlgwPuOW1TeESvtzPhF3/qosfdkDOmW66GS8V+XhC3UeLHVtl y9WJhgdJUL3Ve14QCoq2mYxgdFn6/ci0Vq2CzWNboXkmd71e773SYpjMxxdyD0VW7F/h yvqpsHLmvHue8IBcH86lt6gus4ocNl79Y9peX46iiNFcCrkqTaEfEEElWwCdoFCVh+M6 XmIPEWw+pBJ4sVIUPc1uhOYbmDjcs7sv4XoqvWOmzf3xdatg6Z1p8jsq/JZAhnJ+AnoM znbolKNMIrAigqwmNMaMHWXgU1U5E2spbV4hQk+FIUZpuxxJp/B3HHiMID2rNGL+bHae yrjw== X-Gm-Message-State: AOAM5328fXvZE+2eNTR6+7Zif6GAQlSkBJtAkvVRIQbnb8d6DQjNhgIJ oX2NPY+IeGAqedDIIdeXuMvyLEQfirvtbB0= X-Received: by 2002:a02:c80d:: with SMTP id p13mr296050jao.138.1640131657976; Tue, 21 Dec 2021 16:07:37 -0800 (PST) Received: from rcombs-mbp.localdomain ([2601:243:2000:5ac:d068:8cb9:95e2:de39]) by smtp.gmail.com with ESMTPSA id a1sm238240ilj.35.2021.12.21.16.07.37 for (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 21 Dec 2021 16:07:37 -0800 (PST) From: rcombs To: ffmpeg-devel@ffmpeg.org Date: Tue, 21 Dec 2021 18:07:25 -0600 Message-Id: <20211222000725.38266-7-rcombs@rcombs.me> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211222000725.38266-1-rcombs@rcombs.me> References: <20211222000725.38266-1-rcombs@rcombs.me> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 7/7] lavfi/metal: fix build with pre-10.11 deployment targets 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: LGO4KS6JCH9A - Ensure the yadif .metal compiles when targeting any Metal runtime version - Use some preprocessor awkwardness to ensure Core Video's Metal-specific functionality is exposed regardless of our deployment target (this works around what seems to be an SDK header bug, filed as FB9816002) - Ensure all direct references to Metal functions and classes are gated behind runtime version checks (this satisfies clang's deployment-target violation warnings provided by -Wunguarded-availability). --- libavfilter/metal/utils.h | 28 +++++++- libavfilter/metal/vf_yadif_videotoolbox.metal | 11 ++- libavfilter/vf_yadif_videotoolbox.m | 67 ++++++++++++++++--- 3 files changed, 94 insertions(+), 12 deletions(-) diff --git a/libavfilter/metal/utils.h b/libavfilter/metal/utils.h index bd0319f63c..7350d42a35 100644 --- a/libavfilter/metal/utils.h +++ b/libavfilter/metal/utils.h @@ -20,16 +20,40 @@ #define AVFILTER_METAL_UTILS_H #include + +#include + +// CoreVideo accidentally(?) preprocessor-gates Metal functionality +// on MAC_OS_X_VERSION_MIN_REQUIRED >= 101100 (FB9816002). +// There doesn't seem to be any particular reason for this, +// so here we temporarily redefine it to at least that value +// so CV will give us CVMetalTextureRef and the related functions. + +#if defined(MAC_OS_X_VERSION_MIN_REQUIRED) && (MAC_OS_X_VERSION_MIN_REQUIRED < 101100) +#define ORIG_MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_MIN_REQUIRED +#undef MAC_OS_X_VERSION_MIN_REQUIRED +#define MAC_OS_X_VERSION_MIN_REQUIRED 101100 +#endif + #include +#ifdef ORIG_MAC_OS_X_VERSION_MIN_REQUIRED +#undef MAC_OS_X_VERSION_MIN_REQUIRED +#define MAC_OS_X_VERSION_MIN_REQUIRED ORIG_MAC_OS_X_VERSION_MIN_REQUIRED +#undef ORIG_MAC_OS_X_VERSION_MIN_REQUIRED +#endif + void ff_metal_compute_encoder_dispatch(id device, id pipeline, id encoder, - NSUInteger width, NSUInteger height); + NSUInteger width, NSUInteger height) + API_AVAILABLE(macos(10.11), ios(8.0)); CVMetalTextureRef ff_metal_texture_from_pixbuf(void *avclass, CVMetalTextureCacheRef textureCache, CVPixelBufferRef pixbuf, int plane, - MTLPixelFormat format); + MTLPixelFormat format) + API_AVAILABLE(macos(10.11), ios(8.0)); + #endif /* AVFILTER_METAL_UTILS_H */ diff --git a/libavfilter/metal/vf_yadif_videotoolbox.metal b/libavfilter/metal/vf_yadif_videotoolbox.metal index 50783f2ffe..8a3d41a30f 100644 --- a/libavfilter/metal/vf_yadif_videotoolbox.metal +++ b/libavfilter/metal/vf_yadif_videotoolbox.metal @@ -26,6 +26,15 @@ using namespace metal; +/* + * Version compat shims + */ + +#if __METAL_VERSION__ < 210 +#define max3(x, y, z) max(x, max(y, z)) +#define min3(x, y, z) min(x, min(y, z)) +#endif + /* * Parameters */ @@ -44,7 +53,7 @@ struct deintParams { */ #define accesstype access::sample -const sampler s(coord::pixel); +constexpr sampler s(coord::pixel); template T tex2D(texture2d tex, uint x, uint y) diff --git a/libavfilter/vf_yadif_videotoolbox.m b/libavfilter/vf_yadif_videotoolbox.m index 65f155982e..455745817f 100644 --- a/libavfilter/vf_yadif_videotoolbox.m +++ b/libavfilter/vf_yadif_videotoolbox.m @@ -26,10 +26,12 @@ #include "libavutil/hwcontext.h" #include "libavutil/objc.h" +#include + extern char ff_vf_yadif_videotoolbox_metallib_data[]; extern unsigned int ff_vf_yadif_videotoolbox_metallib_len; -typedef struct YADIFVTContext { +typedef struct API_AVAILABLE(macos(10.11), ios(8.0)) YADIFVTContext { YADIFContext yadif; AVBufferRef *device_ref; @@ -44,7 +46,12 @@ typedef struct YADIFVTContext { id mtlParamsBuffer; CVMetalTextureCacheRef textureCache; -} YADIFVTContext; +} YADIFVTContext API_AVAILABLE(macos(10.11), ios(8.0)); + +// Using sizeof(YADIFVTContext) outside of an availability check will error +// if we're targeting an older OS version, so we need to calculate the size ourselves +// (we'll statically verify it's correct in yadif_videotoolbox_init behind a check) +#define YADIF_VT_CTX_SIZE (sizeof(YADIFContext) + sizeof(void*) * 10) struct mtlYadifParams { uint channels; @@ -62,7 +69,7 @@ static void call_kernel(AVFilterContext *ctx, id next, int channels, int parity, - int tff) + int tff) API_AVAILABLE(macos(10.11), ios(8.0)) { YADIFVTContext *s = ctx->priv; id buffer = s->mtlQueue.commandBuffer; @@ -93,7 +100,7 @@ static void call_kernel(AVFilterContext *ctx, } static void filter(AVFilterContext *ctx, AVFrame *dst, - int parity, int tff) + int parity, int tff) API_AVAILABLE(macos(10.11), ios(8.0)) { YADIFVTContext *s = ctx->priv; YADIFContext *y = &s->yadif; @@ -162,7 +169,7 @@ exit: return; } -static av_cold void yadif_videotoolbox_uninit(AVFilterContext *ctx) +static av_cold void do_uninit(AVFilterContext *ctx) API_AVAILABLE(macos(10.11), ios(8.0)) { YADIFVTContext *s = ctx->priv; YADIFContext *y = &s->yadif; @@ -188,7 +195,15 @@ static av_cold void yadif_videotoolbox_uninit(AVFilterContext *ctx) } } -static av_cold int yadif_videotoolbox_init(AVFilterContext *ctx) + +static av_cold void yadif_videotoolbox_uninit(AVFilterContext *ctx) +{ + if (@available(macOS 10.11, iOS 8.0, *)) { + do_uninit(ctx); + } +} + +static av_cold int do_init(AVFilterContext *ctx) API_AVAILABLE(macos(10.11), ios(8.0)) { YADIFVTContext *s = ctx->priv; NSError *err = nil; @@ -261,7 +276,19 @@ fail: return AVERROR_EXTERNAL; } -static int config_input(AVFilterLink *inlink) +static av_cold int yadif_videotoolbox_init(AVFilterContext *ctx) +{ + if (@available(macOS 10.11, iOS 8.0, *)) { + // Ensure we calculated YADIF_VT_CTX_SIZE correctly + static_assert(YADIF_VT_CTX_SIZE == sizeof(YADIFVTContext), "Incorrect YADIF_VT_CTX_SIZE value!"); + return do_init(ctx); + } else { + av_log(ctx, AV_LOG_ERROR, "Metal is not available on this OS version\n"); + return AVERROR(ENOSYS); + } +} + +static int do_config_input(AVFilterLink *inlink) API_AVAILABLE(macos(10.11), ios(8.0)) { AVFilterContext *ctx = inlink->dst; YADIFVTContext *s = ctx->priv; @@ -283,7 +310,18 @@ static int config_input(AVFilterLink *inlink) return 0; } -static int config_output(AVFilterLink *link) +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + if (@available(macOS 10.11, iOS 8.0, *)) { + return do_config_input(inlink); + } else { + av_log(ctx, AV_LOG_ERROR, "Metal is not available on this OS version\n"); + return AVERROR(ENOSYS); + } +} + +static int do_config_output(AVFilterLink *link) API_AVAILABLE(macos(10.11), ios(8.0)) { AVHWFramesContext *output_frames; AVFilterContext *ctx = link->src; @@ -347,6 +385,17 @@ exit: return ret; } +static int config_output(AVFilterLink *link) +{ + AVFilterContext *ctx = link->src; + if (@available(macOS 10.11, iOS 8.0, *)) { + return do_config_output(link); + } else { + av_log(ctx, AV_LOG_ERROR, "Metal is not available on this OS version\n"); + return AVERROR(ENOSYS); + } +} + #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit } @@ -394,7 +443,7 @@ static const AVFilterPad yadif_videotoolbox_outputs[] = { AVFilter ff_vf_yadif_videotoolbox = { .name = "yadif_videotoolbox", .description = NULL_IF_CONFIG_SMALL("YADIF for VideoToolbox frames using Metal compute"), - .priv_size = sizeof(YADIFVTContext), + .priv_size = YADIF_VT_CTX_SIZE, .priv_class = &yadif_videotoolbox_class, .init = yadif_videotoolbox_init, .uninit = yadif_videotoolbox_uninit,