From patchwork Sun Oct 20 20:05:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 52424 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:612c:143:b0:48e:c0f8:d0de with SMTP id h3csp2271678vqi; Sun, 20 Oct 2024 14:59:03 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUhnAYPb+1EbMzKmRfwzUYZiQZwb1/aUnkIHOUV+DU96zqmqFnvrIm357maFCXUstO04oPETEWlA1ttcEWV+fzA@gmail.com X-Google-Smtp-Source: AGHT+IFpSLMFAt8X18v1yrhYL8DJ00IHeKT1OX6+8DnwZjAIQKPqOM13m/WI+c6UOhqdTkwtOlCN X-Received: by 2002:a05:6512:1244:b0:539:e9f8:d45d with SMTP id 2adb3069b0e04-53a154f9980mr4445538e87.52.1729461542930; Sun, 20 Oct 2024 14:59:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1729461542; cv=none; d=google.com; s=arc-20240605; b=VEX5dgJKy+Y3aIvj5xvjeDfU+gwVu3BJ8bKAYlAp6yJE1mp1H11V1rarnp2LBqQUX0 2imoo3DWdK6ew7OL8w+/NvbCHCsJH0GW79hJpGZpMDdZ9X5sQEBUJqVkQnFhhLgrTFh0 cxI+iLJUQf0B0AJD8hIeZB0vbLdzfLN8Q/nLVOo/vkoDNfrPHVgKVnukvN6rpw/CVZJ0 sF4EUHt8zYtqONhV1dhmXaWeOGtMVUDX5NOZQ9hvqiYepmFoh733ORAw99matII3PEHJ JTr8mJ/QAEVmOtiObjzjHGQcuQ5Dp9VhpHm/eoFcT5N8RZOGwfA3DwnmZPxcnD+SjfDc tdYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=JBZU5n4kIHs2H0+ul3v5kgAqqxzUQR+SqNWWEkGClgg=; fh=xmAeKtysnShNOmkhiJmYkS30uw4Fu2hvBJ7qlIwukxQ=; b=dWzcf3/t+Ep61w0xN876i9DWsfQroJdLdOiL00wBIqPJkg4t6nWoLUv3LH1kM8HJCa zUwptUMeOALOHd4TWpiZgr+z63JnrMYyuWzclR8Blj6kM9PuJIU21vtIqJ3QLSgxFzHK EysSoOM1U1bBmVS2w/dZejkylcEn0JyVBA3VW5pAgvsK43SvbNGqlPJnznHdRlNg5rkw WBEMNdMWUVA9PZCbatxdBr1Dngio4ry/1lfpzDga2QyURXk5Sd5xXZIuerwOwXx+3lWu QHrqX1UnvjTBy6QYBoSP5kyk7igjsvUi9QS5J7PsZwNnTBU4B8pzOPOqQGEFNwkxB5yQ CZzw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b="g/74ZCKb"; 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 2adb3069b0e04-53a2245a59esi727402e87.647.2024.10.20.14.59.02; Sun, 20 Oct 2024 14:59:02 -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=@haasn.xyz header.s=mail header.b="g/74ZCKb"; 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 6DE9A68DD37; Sun, 20 Oct 2024 23:09:16 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A6D3068DBA5 for ; Sun, 20 Oct 2024 23:08:59 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1729454935; bh=tzHM6pEftd5cckNgFZW48yrJQd6fehj7fHfs1Jar+nM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g/74ZCKbbY7oYRzBwIrunQ3d5MxbnM/51M99IaPJbFu6piwo1bYvtFKJVUf2XWTNi TvACHsXBwWeGzx753S+5/plGlOH9AyVNQZMvsg+pgAFwQNrqzO5oMIEMz3FCzSCjwS rH82iZf2VV1LLuNqZFaeEiBdwxD5npIcgqYoE3yo= Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id A91AB4BE81; Sun, 20 Oct 2024 22:08:55 +0200 (CEST) From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Sun, 20 Oct 2024 22:05:15 +0200 Message-ID: <20241020200851.1414766-7-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241020200851.1414766-1-ffmpeg@haasn.xyz> References: <20241020200851.1414766-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 06/18] swscale: add new frame testing API 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: Niklas Haas Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: RCaQYKngF2we From: Niklas Haas Replacing the old sws_isSupported* API with a more consistent family of functions that follows the same signature and naming convention, including a placeholder for testing the color space parameters that we don't currently implement conversions for. These functions also perform some extra basic sanity checking. Sponsored-by: Sovereign Tech Fund Signed-off-by: Niklas Haas --- libswscale/swscale.h | 55 +++++++++++++++++++++++++++++++++++ libswscale/utils.c | 68 ++++++++++++++++++++++++++++++++++++++++++++ libswscale/utils.h | 2 ++ 3 files changed, 125 insertions(+) diff --git a/libswscale/swscale.h b/libswscale/swscale.h index f9fd340240..ba944cdc6f 100644 --- a/libswscale/swscale.h +++ b/libswscale/swscale.h @@ -85,6 +85,61 @@ SwsContext *sws_alloc_context(void); */ void sws_free_context(SwsContext **ctx); +/*************************** + * Supported frame formats * + ***************************/ + +/** + * Test if a given pixel format is supported. + * + * @param output If 0, test if compatible with the source/input frame; + * otherwise, with the destination/output frame. + * @param format The format to check. + * + * @return A positive integer if supported, 0 otherwise. + */ +int sws_test_format(enum AVPixelFormat format, int output); + +/** + * Test if a given color space is supported. + * + * @param output If 0, test if compatible with the source/input frame; + * otherwise, with the destination/output frame. + * @param colorspace The colorspace to check. + * + * @return A positive integer if supported, 0 otherwise. + */ +int sws_test_colorspace(enum AVColorSpace colorspace, int output); + +/** + * Test if a given set of color primaries is supported. + * + * @param output If 0, test if compatible with the source/input frame; + * otherwise, with the destination/output frame. + * @param primaries The color primaries to check. + * + * @return A positive integer if supported, 0 otherwise. + */ +int sws_test_primaries(enum AVColorPrimaries primaries, int output); + +/** + * Test if a given color transfer function is supported. + * + * @param output If 0, test if compatible with the source/input frame; + * otherwise, with the destination/output frame. + * @param trc The color transfer function to check. + * + * @return A positive integer if supported, 0 otherwise. + */ +int sws_test_transfer(enum AVColorTransferCharacteristic trc, int output); + +/** + * Helper function to run all sws_test_* against a frame, as well as testing + * the basic frame properties for sanity. Ignores irrelevant properties - for + * example, AVColorSpace is not checked for RGB frames. + */ +int sws_test_frame(const AVFrame *frame, int output); + /* values for the flags, the stuff on the command line is different */ #define SWS_FAST_BILINEAR 1 #define SWS_BILINEAR 2 diff --git a/libswscale/utils.c b/libswscale/utils.c index af913eadab..5339bbe099 100644 --- a/libswscale/utils.c +++ b/libswscale/utils.c @@ -2717,3 +2717,71 @@ SwsFormat ff_fmt_from_frame(const AVFrame *frame, int field) return fmt; } + +int sws_test_format(enum AVPixelFormat format, int output) +{ + return output ? sws_isSupportedOutput(format) : sws_isSupportedInput(format); +} + +int sws_test_colorspace(enum AVColorSpace csp, int output) +{ + switch (csp) { + case AVCOL_SPC_UNSPECIFIED: + case AVCOL_SPC_RGB: + case AVCOL_SPC_BT709: + case AVCOL_SPC_BT470BG: + case AVCOL_SPC_SMPTE170M: + case AVCOL_SPC_FCC: + case AVCOL_SPC_SMPTE240M: + case AVCOL_SPC_BT2020_NCL: + return 1; + default: + return 0; + } +} + +int sws_test_primaries(enum AVColorPrimaries prim, int output) +{ + return prim > AVCOL_PRI_RESERVED0 && prim < AVCOL_PRI_NB && + prim != AVCOL_PRI_RESERVED; +} + +int sws_test_transfer(enum AVColorTransferCharacteristic trc, int output) +{ + return trc > AVCOL_TRC_RESERVED0 && trc < AVCOL_TRC_NB && + trc != AVCOL_TRC_RESERVED; +} + +static int test_range(enum AVColorRange range) +{ + return range >= 0 && range < AVCOL_RANGE_NB; +} + +static int test_loc(enum AVChromaLocation loc) +{ + return loc >= 0 && loc < AVCHROMA_LOC_NB; +} + +int ff_test_fmt(const SwsFormat *fmt, int output) +{ + return fmt->width > 0 && fmt->height > 0 && + sws_test_format (fmt->format, output) && + sws_test_colorspace(fmt->csp, output) && + sws_test_primaries (fmt->prim, output) && + sws_test_transfer (fmt->trc, output) && + test_range (fmt->range) && + test_loc (fmt->loc); +} + +int sws_test_frame(const AVFrame *frame, int output) +{ + for (int field = 0; field < 2; field++) { + const SwsFormat fmt = ff_fmt_from_frame(frame, field); + if (!ff_test_fmt(&fmt, output)) + return 0; + if (!fmt.interlaced) + break; + } + + return 1; +} diff --git a/libswscale/utils.h b/libswscale/utils.h index a210b646b0..4d204ef6cc 100644 --- a/libswscale/utils.h +++ b/libswscale/utils.h @@ -72,4 +72,6 @@ static inline int ff_fmt_align(enum AVPixelFormat fmt) } } +int ff_test_fmt(const SwsFormat *fmt, int output); + #endif /* SWSCALE_UTILS_H */