From patchwork Fri Jan 20 19:31:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 40083 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3ca3:b0:b9:1511:ac2c with SMTP id b35csp1432444pzj; Fri, 20 Jan 2023 11:33:07 -0800 (PST) X-Google-Smtp-Source: AMrXdXtBuCZavxlP0otMS0508tyzdytm7u9506YPX1YZ2EbEKK6pQdFzm2chtYeqQVc7kXy1/92b X-Received: by 2002:a17:906:cd1f:b0:84d:4a2b:73b9 with SMTP id oz31-20020a170906cd1f00b0084d4a2b73b9mr17425151ejb.59.1674243187503; Fri, 20 Jan 2023 11:33:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674243187; cv=none; d=google.com; s=arc-20160816; b=bWqWUrhnl/Gwn/STe0DYI2S06bpvEJq28WNQm7mh2vQ3q2hoHbpgTcFrcKmXiCyPbw aUjvP9rNiTz219VMLe1mxIsGxbXo+H1qnF+xBVKV9SctRKUTE2sERkgjVf4WczkANXx+ bdgiQFziztu6Y6vf5WkBW0x1H2KNpjjt0SXOmzPRzIiGSArYRAnxjYy1QZw6PeeNWoaR z2su5iorAwAIjJPG5/sgNjGgSi8GFYtU6ultxKM/O/1SuWcCCqrS+9VrxC1305Lufg3+ slVMguWfKtgZKCvaJhUd1/kFrUbG7iIydZvGdiKwMqs5tmHDfsRSvBBYQ9VjjKfa1knn SXXQ== 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:message-id:date:to:from :delivered-to; bh=hXx17WtzWTwc7jyO3KNR7Wkx6//uBcrsc4dbY1YpYWQ=; b=GJoSkt8S/CamB4jIb9lm9Dq0VqhP594lZakE/M3JYa+mhVob/10zDiv9Qqke73OfYF qfYmr34Ztk1hZjX3qx2RnzZ4XOWAFDBjMsqOwC3EZWrtRZlNzUJbuRu5+3s/BYNDaOcz S6UFPYAbVaolajANVXrFV56wgd5YC4FZRVa9dbHw2cjM2bvheQFlE3oOiS+YRHHYC0vC zFW2C9zombuZYixJmt5zxCLRCwiKhfNbMPX3yRvv4PAjpK8jLpcWQVpxaoIVtdEimifD Px2vsyaL2rWsfUdgrydRfPxuzCCbOMK4qASEWEYaXeL/iUR92MrCcREDcCXv5nhKCnFU uWvQ== ARC-Authentication-Results: i=1; mx.google.com; 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 t3-20020a170906948300b0087787ae09f5si3809898ejx.412.2023.01.20.11.32.51; Fri, 20 Jan 2023 11:33:07 -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; 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 DDE2568BD40; Fri, 20 Jan 2023 21:32:09 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0ECC968BCDC for ; Fri, 20 Jan 2023 21:32:00 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id C57322404F5 for ; Fri, 20 Jan 2023 20:31:59 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id tljhF59iUipt for ; Fri, 20 Jan 2023 20:31:59 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id AD4F6240178 for ; Fri, 20 Jan 2023 20:31:56 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id A404F3A0101 for ; Fri, 20 Jan 2023 20:31:48 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 20 Jan 2023 20:31:28 +0100 Message-Id: <20230120193132.21597-1-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/5] lavfi/avfilter: export process_options() 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: NHEMfvN2TKU9 Also, replace an AVFilterContext argument with a logging context+private class, as those are the only things needed in this function. Will be useful in future commits. --- libavfilter/avfilter.c | 20 ++++++++++---------- libavfilter/internal.h | 13 +++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c index c2ecdffa6f..86b275dc4f 100644 --- a/libavfilter/avfilter.c +++ b/libavfilter/avfilter.c @@ -797,8 +797,8 @@ int ff_filter_get_nb_threads(AVFilterContext *ctx) return ctx->graph->nb_threads; } -static int process_options(AVFilterContext *ctx, AVDictionary **options, - const char *args) +int ff_filter_opt_parse(void *logctx, const AVClass *priv_class, + AVDictionary **options, const char *args) { const AVOption *o = NULL; int ret; @@ -812,8 +812,8 @@ static int process_options(AVFilterContext *ctx, AVDictionary **options, while (*args) { const char *shorthand = NULL; - if (ctx->filter->priv_class) - o = av_opt_next(ctx->priv, o); + if (priv_class) + o = av_opt_next(&priv_class, o); if (o) { if (o->type == AV_OPT_TYPE_CONST || o->offset == offset) continue; @@ -826,9 +826,9 @@ static int process_options(AVFilterContext *ctx, AVDictionary **options, &parsed_key, &value); if (ret < 0) { if (ret == AVERROR(EINVAL)) - av_log(ctx, AV_LOG_ERROR, "No option name near '%s'\n", args); + av_log(logctx, AV_LOG_ERROR, "No option name near '%s'\n", args); else - av_log(ctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args, + av_log(logctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args, av_err2str(ret)); return ret; } @@ -838,13 +838,13 @@ static int process_options(AVFilterContext *ctx, AVDictionary **options, key = parsed_key; /* discard all remaining shorthand */ - if (ctx->filter->priv_class) - while ((o = av_opt_next(ctx->priv, o))); + if (priv_class) + while ((o = av_opt_next(&priv_class, o))); } else { key = shorthand; } - av_log(ctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value); + av_log(logctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value); av_dict_set(options, key, value, AV_DICT_MULTIKEY); @@ -908,7 +908,7 @@ int avfilter_init_str(AVFilterContext *filter, const char *args) int ret = 0; if (args && *args) { - ret = process_options(filter, &options, args); + ret = ff_filter_opt_parse(filter, filter->filter->priv_class, &options, args); if (ret < 0) goto fail; } diff --git a/libavfilter/internal.h b/libavfilter/internal.h index aaf2c6c584..2ec41917f7 100644 --- a/libavfilter/internal.h +++ b/libavfilter/internal.h @@ -405,4 +405,17 @@ int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link, int default_pool_size); +/** + * Parse filter options into a dictionary. + * + * @param logctx context for logging + * @param priv_class a filter's private class for shorthand options or NULL + * @param options dictionary to store parsed options in + * @param args options string to parse + * + * @return a non-negative number on success, a negative error code on failure + */ +int ff_filter_opt_parse(void *logctx, const AVClass *priv_class, + AVDictionary **options, const char *args); + #endif /* AVFILTER_INTERNAL_H */ From patchwork Fri Jan 20 19:31:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 40079 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3ca3:b0:b9:1511:ac2c with SMTP id b35csp1431990pzj; Fri, 20 Jan 2023 11:32:21 -0800 (PST) X-Google-Smtp-Source: AMrXdXsRdVD5CSCtf5JbRl6acwK0DIZQ95q5Nari0fzgBqyjkwDM4P62b1GEKXqRPnMh1cOKUzao X-Received: by 2002:a17:906:d8a8:b0:84b:b481:6188 with SMTP id qc8-20020a170906d8a800b0084bb4816188mr12789279ejb.64.1674243141488; Fri, 20 Jan 2023 11:32:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674243141; cv=none; d=google.com; s=arc-20160816; b=HXQZizllb94fPArHC/r+t5ta0Ad6hZq6A/WyDyT381sncJA7zL68R4Nsg15lcLa8Ip I7WEreazP2AWp3eXf/mbvUG4wn2FwMYMSIB4j/bo6QbEESiiFvOZkBUdyEvl3LbABwXZ /STaIq3xOl6ABy01d9m/WFbM9Prb3fwZhSoD22LLs8jFYlEyrECBOxPV52X24FLLdTYl rrnrDlTkHy1ZcTF7tntwa0hBaO9+oy+aPd4FYLN6D5CFysMwW+r+MtH5SVWRB5JvPekH uhml6HjDpJ2o74SLrQMpRVhGQUN6HbKgRvosIqNP1KA9VrqCZcCvXig4gBApDOSbgAzR 6x1g== 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:delivered-to; bh=WlzJ4Gyu1g5ehedbTHOBoIE/lKUJ5S4vrNPPGGtC7xc=; b=E2XTVskk55KUpqRDZEa3c9veGS9SB3Gq8UoD33v7H1HkoUjWmIhabXZFAO9xKEf1L3 X5vA41YwoS7TlkG5hRVyHpOogmo1k2G6V3OS5JXWgzK3DwK/+bUIWxXYhq9zF9g1CVkk FRDKybgFhINsO5BGHj9VY85BCfwdDoRuUjz2n7FkVQJgfeotvoTHvLsOpGWdZYjNXmAT k2DbCU3NdRO/9qTME2Pv94uIv/+O0a9psbTg0Fs182hFxO39zRWy2VnHu/jGgd80d6BP D2qAEds36GIRoZYfMnTVNUZsdDXhy25DbW/C4aMHxRMcdZITVZbz7+Hdhq3o7GhAWaTj yskw== ARC-Authentication-Results: i=1; mx.google.com; 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 qk43-20020a1709077fab00b0086f53c24d9csi23415588ejc.625.2023.01.20.11.32.10; Fri, 20 Jan 2023 11:32:21 -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; 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 1F5A268BD18; Fri, 20 Jan 2023 21:32:06 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 88A5168BCEE for ; Fri, 20 Jan 2023 21:31:59 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 2CF8F2404F8 for ; Fri, 20 Jan 2023 20:31:59 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id Yrp8XCeFxX9i for ; Fri, 20 Jan 2023 20:31:58 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id B279E240591 for ; Fri, 20 Jan 2023 20:31:56 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id B09363A0344 for ; Fri, 20 Jan 2023 20:31:48 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 20 Jan 2023 20:31:29 +0100 Message-Id: <20230120193132.21597-2-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230120193132.21597-1-anton@khirnov.net> References: <20230120193132.21597-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/5] lavfi/avfilter: track whether a filter has been initialized 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: AsQO3MmFxYmu Refuse to link uninitialized filters or initialize a filter twice. --- libavfilter/avfilter.c | 12 ++++++++++++ libavfilter/internal.h | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c index 86b275dc4f..619b96f5ac 100644 --- a/libavfilter/avfilter.c +++ b/libavfilter/avfilter.c @@ -158,6 +158,11 @@ int avfilter_link(AVFilterContext *src, unsigned srcpad, src->outputs[srcpad] || dst->inputs[dstpad]) return AVERROR(EINVAL); + if (!src->internal->initialized || !dst->internal->initialized) { + av_log(src, AV_LOG_ERROR, "Filters must be initialized before linking.\n"); + return AVERROR(EINVAL); + } + if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) { av_log(src, AV_LOG_ERROR, "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n", @@ -872,6 +877,11 @@ int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options) { int ret = 0; + if (ctx->internal->initialized) { + av_log(ctx, AV_LOG_ERROR, "Filter already initialized\n"); + return AVERROR(EINVAL); + } + ret = av_opt_set_dict2(ctx, options, AV_OPT_SEARCH_CHILDREN); if (ret < 0) { av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n"); @@ -898,6 +908,8 @@ int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options) return ret; } + ctx->internal->initialized = 1; + return 0; } diff --git a/libavfilter/internal.h b/libavfilter/internal.h index 2ec41917f7..8b232a8d8f 100644 --- a/libavfilter/internal.h +++ b/libavfilter/internal.h @@ -137,6 +137,10 @@ struct AVFilterGraphInternal { struct AVFilterInternal { avfilter_execute_func *execute; + + // 1 when avfilter_init_*() was successfully called on this filter + // 0 otherwise + int initialized; }; static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, From patchwork Fri Jan 20 19:31:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 40081 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3ca3:b0:b9:1511:ac2c with SMTP id b35csp1432253pzj; Fri, 20 Jan 2023 11:32:50 -0800 (PST) X-Google-Smtp-Source: AMrXdXvclMrXZaNI8mnUg7JFIvBqjv2VcdVDnQWi3cMDggwdHuinTvy4HNWj3E4x/HRw84Sv17BQ X-Received: by 2002:a05:6402:17d9:b0:498:ebb7:d8b9 with SMTP id s25-20020a05640217d900b00498ebb7d8b9mr16639297edy.5.1674243170074; Fri, 20 Jan 2023 11:32:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674243170; cv=none; d=google.com; s=arc-20160816; b=sdRPHq4gvGtykXxgE+6b3svIKJoB+ercrWssxXBjIfPEQLPdYNHivBwkpynsFjSq/w ikyuflhCOAd/IXtgFgT0bZ+wpt0OhR7pTtP9oBERV9DwWAh+MYpvy9dFGqFXzFm8fqPk 7oofxZsEGDMIh5dSVF1nMo9TuvP4kLbTMpNMjpw25sm9X/8aE4ktHerJ/0GDIR9JUGVL A57vmL/qjhFE9H2KxSWsW+LUvi3KGjVCh0t8rt2186U+48jaZiJNxCiVShQkuAOmRz9f PVzN/oKdzzV9U1QMOo0iTncl8mJPZkOu6OwtA7FlFb54sxc1gApe/1hVKM3yKh+Aj+HA GY+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: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:delivered-to; bh=hVvV1/Z/R5AxJ30kkdj8A0TD90Mk89MTbYNdeSctknM=; b=Pvmc20Ukl9S0UL3qyBhhO3CUDpruOOwJm/fvVlKRllOPz3LOxDVEshlzKEi8tyBeW4 2xQobDX2efeWYDYUfprAa9JMN8Meagl+SJdjsEK4FqRcfP8v/I9UbPpFnAy3nzKwylSH arbzuwtBEWoDenWfEtIa+cwGvqS0+ZQDjMZKRLO7FUwFPQYrFeksQPi1GYkEIYIwsCXn 8Yao0ZOmij2yQb1TFuEQLbIXTD35NMktdg2SJdrC0IWzaXAazTiMtBkAZW1tL77aGle3 yrG6Pm6sj5G4AzH1UEBMxYphO1MGxFoQ0Os7yXRaaoprgPWMHj4H2OBcelnwZaGc1oBw q6Ew== ARC-Authentication-Results: i=1; mx.google.com; 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 f15-20020aa7d84f000000b0047f85add083si38563742eds.623.2023.01.20.11.32.31; Fri, 20 Jan 2023 11:32:50 -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; 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 0C85868BCC5; Fri, 20 Jan 2023 21:32:08 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8B1A268BCF0 for ; Fri, 20 Jan 2023 21:31:59 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 966F12404EC for ; Fri, 20 Jan 2023 20:31:58 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id sWFvRDQHDlgo for ; Fri, 20 Jan 2023 20:31:56 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id B1A542404F8 for ; Fri, 20 Jan 2023 20:31:56 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id BC0923A034A for ; Fri, 20 Jan 2023 20:31:48 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 20 Jan 2023 20:31:30 +0100 Message-Id: <20230120193132.21597-3-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230120193132.21597-1-anton@khirnov.net> References: <20230120193132.21597-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/5] lavfi: add a new filtergraph parsing 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: sJ3NzfffoRf+ Callers currently have two ways of adding filters to a graph - they can either - create, initialize, and link them manually - use one of the avfilter_graph_parse*() functions, which take a (typically end-user-written) string, split it into individual filter definitions+options, then create filters, apply options, initialize filters, and finally link them - all based on information from this string. A major problem with the second approach is that it performs many actions as a single atomic unit, leaving the caller no space to intervene in between. Such intervention would be useful e.g. to - modify filter options; - supply hardware device contexts; both of which typically must be done before the filter is initialized. Callers who need such intervention are then forced to invent their own filtergraph parsing, which is clearly suboptimal. This commit aims to address this problem by adding a new modular filtergraph parsing API. It adds a new avfilter_graph_segment_parse() function to parse a string filtergraph description into an intermediate tree-like representation (AVFilterGraphSegment and its children). This intermediate form may then be applied step by step using further new avfilter_graph_segment*() functions, with user intervention possible between each step. --- doc/APIchanges | 15 + libavfilter/avfilter.h | 313 +++++++++++++++++ libavfilter/graphparser.c | 689 +++++++++++++++++++++++++++++++++++++- 3 files changed, 1010 insertions(+), 7 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index a11acadecd..f8e936ba9a 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -14,6 +14,21 @@ libavutil: 2021-04-27 API changes, most recent first: +2023-xx-xx - xxxxxxxxxx - lavfi 8.x.100 - avfilter.h + Add filtergraph segment parsing API. + New structs: + - AVFilterGraphSegment + - AVFilterChain + - AVFilterParams + - AVFilterPadParams + New functions: + - avfilter_graph_segment_parse() + - avfilter_graph_segment_create_filters() + - avfilter_graph_segment_apply_opts() + - avfilter_graph_segment_init() + - avfilter_graph_segment_link() + - avfilter_graph_segment_apply() + 2023-01-13 - xxxxxxxxxx - lavu 57.44.100 - ambient_viewing_environment.h frame.h Adds a new structure for holding H.274 Ambient Viewing Environment metadata, AVAmbientViewingEnvironment. diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h index c2ec7a4b5f..037a8bc490 100644 --- a/libavfilter/avfilter.h +++ b/libavfilter/avfilter.h @@ -1118,6 +1118,319 @@ int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs); +/** + * Parameters of a filter's input or output pad. + * + * Created as a child of AVFilterParams by avfilter_graph_segment_parse(). + * Freed in avfilter_graph_segment_free(). + */ +typedef struct AVFilterPadParams { + /** + * An av_malloc()'ed string containing the pad label. + * + * May be av_free()'d and set to NULL by the caller, in which case this pad + * will be treated as unlabeled for linking. + * May also be replaced by another av_malloc()'ed string. + */ + char *label; +} AVFilterPadParams; + +/** + * Parameters describing a filter to be created in a filtergraph. + * + * Created as a child of AVFilterGraphSegment by avfilter_graph_segment_parse(). + * Freed in avfilter_graph_segment_free(). + */ +typedef struct AVFilterParams { + /** + * The filter context. + * + * Created by avfilter_graph_segment_create_filters() based on + * AVFilterParams.filter_name and instance_name. + * + * Callers may also create the filter context manually, then they should + * av_free() filter_name and set it to NULL. Such AVFilterParams instances + * are then skipped by avfilter_graph_segment_create_filters(). + */ + AVFilterContext *filter; + + /** + * Name of the AVFilter to be used. + * + * An av_malloc()'ed string, set by avfilter_graph_segment_parse(). Will be + * passed to avfilter_get_by_name() by + * avfilter_graph_segment_create_filters(). + * + * Callers may av_free() this string and replace it with another one or + * NULL. If the caller creates the filter instance manually, this string + * MUST be set to NULL. + * + * When both AVFilterParams.filter an AVFilterParams.filter_name are NULL, + * this AVFilterParams instance is skipped by avfilter_graph_segment_*() + * functions. + */ + char *filter_name; + /** + * Name to be used for this filter instance. + * + * An av_malloc()'ed string, may be set by avfilter_graph_segment_parse() or + * left NULL. The caller may av_free() this string and replace with another + * one or NULL. + * + * Will be used by avfilter_graph_segment_create_filters() - passed as the + * third argument to avfilter_graph_alloc_filter(), then freed and set to + * NULL. + */ + char *instance_name; + + /** + * Options to be apllied to the filter. + * + * Filled by avfilter_graph_segment_parse(). Afterwards may be freely + * modified by the caller. + * + * Will be applied to the filter by avfilter_graph_segment_apply_opts() + * with an equivalent of av_opt_set_dict2(filter, &opts, * AV_OPT_SEARCH_CHILDREN), + * i.e. any unapplied options will be left in this dictionary. + */ + AVDictionary *opts; + + AVFilterPadParams **inputs; + unsigned nb_inputs; + + AVFilterPadParams **outputs; + unsigned nb_outputs; +} AVFilterParams; + +/** + * A filterchain is a list of filter specifications. + * + * Created as a child of AVFilterGraphSegment by avfilter_graph_segment_parse(). + * Freed in avfilter_graph_segment_free(). + */ +typedef struct AVFilterChain { + AVFilterParams **filters; + size_t nb_filters; +} AVFilterChain; + +/** + * A parsed representation of a filtergraph segment. + * + * A filtergraph segment is conceptually a list of filterchains, with some + * supplementary information (e.g. format conversion flags). + * + * Created by avfilter_graph_segment_parse(). Must be freed with + * avfilter_graph_segment_free(). + */ +typedef struct AVFilterGraphSegment { + /** + * The filtergraph this segment is associated with. + * Set by avfilter_graph_segment_parse(). + */ + AVFilterGraph *graph; + + /** + * A list of filter chain contained in this segment.. + * Set in avfilter_graph_segment_parse(). + */ + AVFilterChain **chains; + size_t nb_chains; + + /** + * A string containing a colon-separated list of key=value options applied + * to all scale filters in this segment. + * + * May be set by avfilter_graph_segment_parse(). + * The caller may free this string with av_free() and replace it with a + * different av_malloc()'ed string. + */ + char *scale_sws_opts; +} AVFilterGraphSegment; + +/** + * Parse a textual filtergraph description into an intermediate form. + * + * This intermediate representation is intended to be modified by the caller as + * described in the documentation of AVFilterGraphSegment and its children, and + * then applied to the graph either manually or with other + * avfilter_graph_segment_*() functions. See the documentation for + * avfilter_graph_segment_apply() for the canonical way to apply + * AVFilterGraphSegment. + * + * @param graph Filter graph the parsed segment is associated with. Will only be + * used for logging and similar auxiliary purposes. The graph will + * not be actually modified by this function - the parsing results + * are instead stored in seg for further processing. + * @param graph_str a string describing the filtergraph segment + * @param flags reserved for future use, caller must set to 0 for now + * @param seg A pointer to the newly-created AVFilterGraphSegment is written + * here on success. The graph segment is owned by the caller and must + * be freed with avfilter_graph_segment_free() before graph itself is + * freed. + * + * @return a non-negative number on success, a negative error code on failure + */ +int avfilter_graph_segment_parse(AVFilterGraph *graph, const char *graph_str, + int flags, AVFilterGraphSegment **seg); + +/** + * Create filters specified in a graph segment. + * + * Walk through the creation-pending AVFilterParams in the segment and create + * new filter instances for them. + * Creation-pending params are those where AVFilterParams.filter_name is + * non-NULL (and hence AVFilterParams.filter is NULL). All other AVFilterParams + * instances are ignored. + * + * For any filter created by this function, the corresponding + * AVFilterParams.filter is set to the newly-created filter context, + * AVFilterParams.filter_name and AVFilterParams.instance_name are freed and set + * to NULL. + * + * @param seg the filtergraph segment to process + * @param flags reserved for future use, caller must set to 0 for now + * + * @return + * - a non-negative number if all creation-pending filters were successfully + * created + * - AVERROR_FILTER_NOT_FOUND if some filter's name did not correspond to a + * known filter + * - another negative error code on other failures + * + * @note Calling this function multiple times is safe, as it is idempotent. + */ +int avfilter_graph_segment_create_filters(AVFilterGraphSegment *seg, int flags); + +/** + * Apply parsed options to filter instances in a graph segment. + * + * Walk through all filter instances in the graph segment that have option + * dictionaries associated with them and apply those options with + * av_opt_set_dict2(..., AV_OPT_SEARCH_CHILDREN). AVFilterParams.opts is + * replaced by the dictionary output by av_opt_set_dict2(), which should be + * empty (NULL) if all options were successfully applied. + * + * If any options could not be found, this function will continue processing all + * other filters and finally return AVERROR_OPTION_NOT_FOUND (unless another + * error happens). The calling program may then deal with unapplied options as + * it wishes. + * + * Any creation-pending filters (see avfilter_graph_segment_create_filters()) + * present in the segment will cause this function to fail. AVFilterParams with + * no associated filter context are simply skipped. + * + * @param seg the filtergraph segment to process + * @param flags reserved for future use, caller must set to 0 for now + * + * @return + * - a non-negative number if all options were successfully applied + * - AVERROR_OPTION_NOT_FOUND if some options were not found in a filter + * - another negative error code on other failures + * + * @note Calling this function multiple times is safe, as it is idempotent. + */ +int avfilter_graph_segment_apply_opts(AVFilterGraphSegment *seg, int flags); + +/** + * Initialize all filter instances in a graph segment. + * + * Walk through all filter instances in the graph segment and call + * avfilter_init_dict(..., NULL) on those that have not been initialized yet. + * + * Any creation-pending filters (see avfilter_graph_segment_create_filters()) + * present in the segment will cause this function to fail. AVFilterParams with + * no associated filter context or whose filter context is already initialized, + * are simply skipped. + * + * @param seg the filtergraph segment to process + * @param flags reserved for future use, caller must set to 0 for now + * + * @return + * - a non-negative number if all filter instances were successfully initialized + * - a negative error code on other failures + * + * @note Calling this function multiple times is safe, as it is idempotent. + */ +int avfilter_graph_segment_init(AVFilterGraphSegment *seg, int flags); + +/** + * Link filters in a graph segment. + * + * Walk through all filter instances in the graph segment and try to link all + * unlinked input and output pads. Any creation-pending filters (see + * avfilter_graph_segment_create_filters()) present in the segment will cause + * this function to fail. Disabled filters and already linked pads are skipped. + * + * Every filter output pad that has a corresponding AVFilterPadParams with a + * non-NULL label is + * - linked to the input with the matching label, if one exists; + * - exported in the outputs linked list otherwise, with the label preserved. + * Unlabeled outputs are + * - linked to the first unlinked unlabeled input in the next non-disabled + * filter in the chain, if one exists + * - exported in the ouputs linked list otherwise, with NULL label + * + * Similarly, unlinked input pads are exported in the inputs linked list. + * + * @param seg the filtergraph segment to process + * @param flags reserved for future use, caller must set to 0 for now + * @param[out] inputs a linked list of all free (unlinked) inputs of the + * filters in this graph segment will be returned here. It + * is to be freed by the caller using avfilter_inout_free(). + * @param[out] outputs a linked list of all free (unlinked) outputs of the + * filters in this graph segment will be returned here. It + * is to be freed by the caller using avfilter_inout_free(). + * + * @return + * - a non-negative number on success + * - a negative error code on failure + * + * @note Calling this function multiple times is safe, as it is idempotent. + */ +int avfilter_graph_segment_link(AVFilterGraphSegment *seg, int flags, + AVFilterInOut **inputs, + AVFilterInOut **outputs); + +/** + * Apply all filter/link descriptions from a graph segment to the associated filtergraph. + * + * This functions is currently equivalent to calling the following in sequence: + * - avfilter_graph_segment_create_filters(); + * - avfilter_graph_segment_apply_opts(); + * - avfilter_graph_segment_init(); + * - avfilter_graph_segment_link(); + * failing if any of them fails. This list may be extended in the future. + * + * Since the above functions are idempotent, the caller may call some of the + * manually, then do some custom processing on the filtergraph, then call this + * function to do the rest. + * + * @param seg the filtergraph segment to process + * @param flags reserved for future use, caller must set to 0 for now + * @param[out] inputs passed to avfilter_graph_segment_link() + * @param[out] outputs passed to avfilter_graph_segment_link() + * @return + * - a non-negative number on success + * - a negative error code on failure + * + * @note Calling this function multiple times is safe, as it is idempotent. + */ +int avfilter_graph_segment_apply(AVFilterGraphSegment *seg, int flags, + AVFilterInOut **inputs, + AVFilterInOut **outputs); + +/** + * Free the provided AVFilterGraphSegment and everything associated with it. + * + * @param seg double pointer to the AVFilterGraphSegment to be freed. NULL will + * be written to this pointer on exit from this function. + * + * @note + * The filter contexts (AVFilterParams.filter) are owned by AVFilterGraph rather + * than AVFilterGraphSegment, so they are not freed. + */ +void avfilter_graph_segment_free(AVFilterGraphSegment **seg); + /** * Send a command to one or more filter instances. * diff --git a/libavfilter/graphparser.c b/libavfilter/graphparser.c index 0759c39014..ac6171e0c4 100644 --- a/libavfilter/graphparser.c +++ b/libavfilter/graphparser.c @@ -24,10 +24,12 @@ #include #include "libavutil/avstring.h" +#include "libavutil/dict.h" #include "libavutil/mem.h" #include "libavutil/opt.h" #include "avfilter.h" +#include "internal.h" #define WHITESPACES " \n\t\r" @@ -386,7 +388,7 @@ static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs, return pad; } -static int parse_sws_flags(const char **buf, AVFilterGraph *graph) +static int parse_sws_flags(const char **buf, char **dst, void *log_ctx) { char *p = strchr(*buf, ';'); @@ -394,16 +396,16 @@ static int parse_sws_flags(const char **buf, AVFilterGraph *graph) return 0; if (!p) { - av_log(graph, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n"); + av_log(log_ctx, AV_LOG_ERROR, "sws_flags not terminated with ';'.\n"); return AVERROR(EINVAL); } *buf += 4; // keep the 'flags=' part - av_freep(&graph->scale_sws_opts); - if (!(graph->scale_sws_opts = av_mallocz(p - *buf + 1))) + av_freep(dst); + if (!(*dst = av_mallocz(p - *buf + 1))) return AVERROR(ENOMEM); - av_strlcpy(graph->scale_sws_opts, *buf, p - *buf + 1); + av_strlcpy(*dst, *buf, p - *buf + 1); *buf = p + 1; return 0; @@ -420,7 +422,7 @@ int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters, filters += strspn(filters, WHITESPACES); - if ((ret = parse_sws_flags(&filters, graph)) < 0) + if ((ret = parse_sws_flags(&filters, &graph->scale_sws_opts, graph)) < 0) goto end; do { @@ -551,7 +553,7 @@ int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters, AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL; AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL; - if ((ret = parse_sws_flags(&filters, graph)) < 0) + if ((ret = parse_sws_flags(&filters, &graph->scale_sws_opts, graph)) < 0) goto end; do { @@ -623,3 +625,676 @@ end: } return ret; } + +static void pad_params_free(AVFilterPadParams **pfpp) +{ + AVFilterPadParams *fpp = *pfpp; + + if (!fpp) + return; + + av_freep(&fpp->label); + + av_freep(pfpp); +} + +static void filter_params_free(AVFilterParams **pp) +{ + AVFilterParams *p = *pp; + + if (!p) + return; + + for (unsigned i = 0; i < p->nb_inputs; i++) + pad_params_free(&p->inputs[i]); + av_freep(&p->inputs); + + for (unsigned i = 0; i < p->nb_outputs; i++) + pad_params_free(&p->outputs[i]); + av_freep(&p->outputs); + + av_dict_free(&p->opts); + + av_freep(&p->filter_name); + av_freep(&p->instance_name); + + av_freep(pp); +} + +static void chain_free(AVFilterChain **pch) +{ + AVFilterChain *ch = *pch; + + if (!ch) + return; + + for (size_t i = 0; i < ch->nb_filters; i++) + filter_params_free(&ch->filters[i]); + av_freep(&ch->filters); + + av_freep(pch); +} + +void avfilter_graph_segment_free(AVFilterGraphSegment **pseg) +{ + AVFilterGraphSegment *seg = *pseg; + + if (!seg) + return; + + for (size_t i = 0; i < seg->nb_chains; i++) + chain_free(&seg->chains[i]); + av_freep(&seg->chains); + + av_freep(&seg->scale_sws_opts); + + av_freep(pseg); +} + +static int linklabels_parse(void *logctx, const char **linklabels, + AVFilterPadParams ***res, unsigned *nb_res) +{ + AVFilterPadParams **pp = NULL; + unsigned nb = 0; + int ret; + + while (**linklabels == '[') { + char *label; + AVFilterPadParams *par, **tmp; + + label = parse_link_name(linklabels, logctx); + if (!label) { + ret = AVERROR(EINVAL); + goto fail; + } + + par = av_mallocz(sizeof(*par)); + if (!par) { + av_freep(&label); + ret = AVERROR(ENOMEM); + goto fail; + } + + par->label = label; + + tmp = av_realloc_array(pp, nb + 1, sizeof(*pp)); + if (!tmp) { + pad_params_free(&par); + ret = AVERROR(ENOMEM); + goto fail; + } + + pp = tmp; + pp[nb++] = par; + + *linklabels += strspn(*linklabels, WHITESPACES); + } + + *res = pp; + *nb_res = nb; + + return 0; +fail: + for (unsigned i = 0; i < nb; i++) + pad_params_free(&pp[i]); + av_freep(&pp); + return ret; +} + +static int filter_parse(void *logctx, const char **filter, + AVFilterParams **pp) +{ + AVFilterParams *p; + char *inst_name; + int ret; + + p = av_mallocz(sizeof(*p)); + if (!p) + return AVERROR(ENOMEM); + + ret = linklabels_parse(logctx, filter, &p->inputs, &p->nb_inputs); + if (ret < 0) + goto fail; + + p->filter_name = av_get_token(filter, "=,;["); + if (!p->filter_name) { + ret = AVERROR(ENOMEM); + goto fail; + } + + inst_name = strchr(p->filter_name, '@'); + if (inst_name) { + *inst_name++ = 0; + p->instance_name = av_strdup(inst_name); + if (!p->instance_name) { + ret = AVERROR(ENOMEM); + goto fail; + } + } + + if (**filter == '=') { + const AVFilter *f = avfilter_get_by_name(p->filter_name); + char *opts; + + (*filter)++; + + opts = av_get_token(filter, "[],;"); + if (!opts) { + ret = AVERROR(ENOMEM); + goto fail; + } + + ret = ff_filter_opt_parse(logctx, f ? f->priv_class : NULL, + &p->opts, opts); + av_freep(&opts); + if (ret < 0) + goto fail; + } + + ret = linklabels_parse(logctx, filter, &p->outputs, &p->nb_outputs); + if (ret < 0) + goto fail; + + *filter += strspn(*filter, WHITESPACES); + + *pp = p; + return 0; +fail: + av_log(logctx, AV_LOG_ERROR, + "Error parsing a filter description around: %s\n", *filter); + filter_params_free(&p); + return ret; +} + +static int chain_parse(void *logctx, const char **pchain, + AVFilterChain **pch) +{ + const char *chain = *pchain; + AVFilterChain *ch; + int ret; + + *pch = NULL; + + ch = av_mallocz(sizeof(*ch)); + if (!ch) + return AVERROR(ENOMEM); + + while (*chain) { + AVFilterParams *p, **tmp; + char chr; + + ret = filter_parse(logctx, &chain, &p); + if (ret < 0) + goto fail; + + tmp = av_realloc_array(ch->filters, ch->nb_filters + 1, sizeof(*ch->filters)); + if (!tmp) { + filter_params_free(&p); + ret = AVERROR(ENOMEM); + goto fail; + } + + ch->filters = tmp; + ch->filters[ch->nb_filters++] = p; + + // a filter ends with one of: , ; end-of-string + chr = *chain; + if (chr && chr != ',' && chr != ';') { + av_log(logctx, AV_LOG_ERROR, + "Trailing garbage after a filter: %s\n", chain); + ret = AVERROR(EINVAL); + goto fail; + } + + if (chr) { + chain++; + chain += strspn(chain, WHITESPACES); + + if (chr == ';') + break; + } + } + + *pchain = chain; + *pch = ch; + + return 0; +fail: + av_log(logctx, AV_LOG_ERROR, + "Error parsing filterchain '%s' around: %s\n", *pchain, chain); + chain_free(&ch); + return ret; +} + +int avfilter_graph_segment_parse(AVFilterGraph *graph, const char *graph_str, + int flags, AVFilterGraphSegment **pseg) +{ + AVFilterGraphSegment *seg; + int ret; + + *pseg = NULL; + + if (flags) + return AVERROR(ENOSYS); + + seg = av_mallocz(sizeof(*seg)); + if (!seg) + return AVERROR(ENOMEM); + + seg->graph = graph; + + graph_str += strspn(graph_str, WHITESPACES); + + ret = parse_sws_flags(&graph_str, &seg->scale_sws_opts, &graph); + if (ret < 0) + goto fail; + + graph_str += strspn(graph_str, WHITESPACES); + + while (*graph_str) { + AVFilterChain *ch, **tmp; + + ret = chain_parse(graph, &graph_str, &ch); + if (ret < 0) + goto fail; + + tmp = av_realloc_array(seg->chains, seg->nb_chains + 1, sizeof(*seg->chains)); + if (!tmp) { + chain_free(&ch); + ret = AVERROR(ENOMEM); + goto fail; + } + + seg->chains = tmp; + seg->chains[seg->nb_chains++] = ch; + + graph_str += strspn(graph_str, WHITESPACES); + } + + if (!seg->nb_chains) { + av_log(graph, AV_LOG_ERROR, "No filters specified in the graph description\n"); + ret = AVERROR(EINVAL); + goto fail; + } + + *pseg = seg; + + return 0; +fail: + avfilter_graph_segment_free(&seg); + return ret; +} + +int avfilter_graph_segment_create_filters(AVFilterGraphSegment *seg, int flags) +{ + size_t idx = 0; + + if (flags) + return AVERROR(ENOSYS); + + if (seg->scale_sws_opts) { + av_freep(&seg->graph->scale_sws_opts); + seg->graph->scale_sws_opts = av_strdup(seg->scale_sws_opts); + if (!seg->graph->scale_sws_opts) + return AVERROR(ENOMEM); + } + + for (size_t i = 0; i < seg->nb_chains; i++) { + AVFilterChain *ch = seg->chains[i]; + + for (size_t j = 0; j < ch->nb_filters; j++) { + AVFilterParams *p = ch->filters[j]; + const AVFilter *f = avfilter_get_by_name(p->filter_name); + char inst_name[30], *name = p->instance_name ? p->instance_name : + inst_name; + + // skip already processed filters + if (p->filter || !p->filter_name) + continue; + + if (!f) { + av_log(seg->graph, AV_LOG_ERROR, + "No such filter: '%s'\n", p->filter_name); + return AVERROR_FILTER_NOT_FOUND; + } + + if (!p->instance_name) + snprintf(inst_name, sizeof(inst_name), "Parsed_%s_%zu", f->name, idx); + + p->filter = avfilter_graph_alloc_filter(seg->graph, f, name); + if (!p->filter) + return AVERROR(ENOMEM); + + if (!strcmp(f->name, "scale") && seg->graph->scale_sws_opts) { + int ret = av_set_options_string(p->filter, seg->graph->scale_sws_opts, + "=", ":"); + if (ret < 0) { + avfilter_free(p->filter); + p->filter = NULL; + return ret; + } + } + + av_freep(&p->filter_name); + av_freep(&p->instance_name); + + idx++; + } + } + + return 0; +} + +static int fail_creation_pending(AVFilterGraphSegment *seg, const char *fn, + const char *func) +{ + av_log(seg->graph, AV_LOG_ERROR, + "A creation-pending filter '%s' present in the segment. All filters " + "must be created or disabled before calling %s().\n", fn, func); + return AVERROR(EINVAL); +} + +int avfilter_graph_segment_apply_opts(AVFilterGraphSegment *seg, int flags) +{ + int ret, leftover_opts = 0; + + if (flags) + return AVERROR(ENOSYS); + + for (size_t i = 0; i < seg->nb_chains; i++) { + AVFilterChain *ch = seg->chains[i]; + + for (size_t j = 0; j < ch->nb_filters; j++) { + AVFilterParams *p = ch->filters[j]; + + if (p->filter_name) + return fail_creation_pending(seg, p->filter_name, __func__); + if (!p->filter || !p->opts) + continue; + + ret = av_opt_set_dict2(p->filter, &p->opts, AV_OPT_SEARCH_CHILDREN); + if (ret < 0) + return ret; + + if (av_dict_count(p->opts)) + leftover_opts = 1; + } + } + + return leftover_opts ? AVERROR_OPTION_NOT_FOUND : 0; +} + +int avfilter_graph_segment_init(AVFilterGraphSegment *seg, int flags) +{ + if (flags) + return AVERROR(ENOSYS); + + for (size_t i = 0; i < seg->nb_chains; i++) { + AVFilterChain *ch = seg->chains[i]; + + for (size_t j = 0; j < ch->nb_filters; j++) { + AVFilterParams *p = ch->filters[j]; + int ret; + + if (p->filter_name) + return fail_creation_pending(seg, p->filter_name, __func__); + if (!p->filter || p->filter->internal->initialized) + continue; + + ret = avfilter_init_dict(p->filter, NULL); + if (ret < 0) + return ret; + } + } + + return 0; +} + +static unsigned +find_linklabel(AVFilterGraphSegment *seg, const char *label, + int output, size_t idx_chain, size_t idx_filter, + AVFilterParams **pp) +{ + for (; idx_chain < seg->nb_chains; idx_chain++) { + AVFilterChain *ch = seg->chains[idx_chain]; + + for (; idx_filter < ch->nb_filters; idx_filter++) { + AVFilterParams *p = ch->filters[idx_filter]; + AVFilterPadParams **io = output ? p->outputs : p->inputs; + unsigned nb_io = output ? p->nb_outputs : p->nb_inputs; + AVFilterLink **l; + unsigned nb_l; + + if (!p->filter) + continue; + + l = output ? p->filter->outputs : p->filter->inputs; + nb_l = output ? p->filter->nb_outputs : p->filter->nb_inputs; + + for (unsigned i = 0; i < FFMIN(nb_io, nb_l); i++) + if (!l[i] && io[i]->label && !strcmp(io[i]->label, label)) { + *pp = p; + return i; + } + } + + idx_filter = 0; + } + + *pp = NULL; + return 0; +} + +static int inout_add(AVFilterInOut **inouts, AVFilterContext *f, unsigned pad_idx, + const char *label) +{ + AVFilterInOut *io = av_mallocz(sizeof(*io)); + + if (!io) + return AVERROR(ENOMEM); + + io->filter_ctx = f; + io->pad_idx = pad_idx; + + if (label) { + io->name = av_strdup(label); + if (!io->name) { + avfilter_inout_free(&io); + return AVERROR(ENOMEM); + } + } + + append_inout(inouts, &io); + + return 0; +} + +static int link_inputs(AVFilterGraphSegment *seg, size_t idx_chain, + size_t idx_filter, AVFilterInOut **inputs) +{ + AVFilterChain *ch = seg->chains[idx_chain]; + AVFilterParams *p = ch->filters[idx_filter]; + AVFilterContext *f = p->filter; + + int ret; + + if (f->nb_inputs < p->nb_inputs) { + av_log(seg->graph, AV_LOG_ERROR, + "More input link labels specified for filter '%s' than " + "it has inputs: %u > %d\n", f->filter->name, + p->nb_inputs, f->nb_inputs); + return AVERROR(EINVAL); + } + + for (unsigned in = 0; in < f->nb_inputs; in++) { + const char *label = (in < p->nb_inputs) ? p->inputs[in]->label : NULL; + + // skip already linked inputs + if (f->inputs[in]) + continue; + + if (label) { + AVFilterParams *po = NULL; + unsigned idx = find_linklabel(seg, label, 1, idx_chain, idx_filter, &po); + + if (po) { + ret = avfilter_link(po->filter, idx, f, in); + if (ret < 0) + return ret; + + continue; + } + } + + ret = inout_add(inputs, f, in, label); + if (ret < 0) + return ret; + } + + return 0; +} + +static int link_outputs(AVFilterGraphSegment *seg, size_t idx_chain, + size_t idx_filter, AVFilterInOut **outputs) +{ + AVFilterChain *ch = seg->chains[idx_chain]; + AVFilterParams *p = ch->filters[idx_filter]; + AVFilterContext *f = p->filter; + + int ret; + + if (f->nb_outputs < p->nb_outputs) { + av_log(seg->graph, AV_LOG_ERROR, + "More output link labels specified for filter '%s' than " + "it has outputs: %u > %d\n", f->filter->name, + p->nb_outputs, f->nb_outputs); + return AVERROR(EINVAL); + } + for (unsigned out = 0; out < f->nb_outputs; out++) { + char *label = (out < p->nb_outputs) ? p->outputs[out]->label : NULL; + + // skip already linked outputs + if (f->outputs[out]) + continue; + + if (label) { + AVFilterParams *po = NULL; + unsigned idx = find_linklabel(seg, label, 0, idx_chain, idx_filter, &po); + + if (po) { + ret = avfilter_link(f, out, po->filter, idx); + if (ret < 0) + return ret; + + continue; + } + } + + // if this output is unlabeled, try linking it to an unlabeled + // input in the next non-disabled filter in the chain + for (size_t i = idx_filter + 1; i < ch->nb_filters && !label; i++) { + AVFilterParams *p_next = ch->filters[i]; + + if (!p_next->filter) + continue; + + for (unsigned in = 0; in < p_next->filter->nb_inputs; in++) { + if (!p_next->filter->inputs[in] && + (in >= p_next->nb_inputs || !p_next->inputs[in]->label)) { + ret = avfilter_link(f, out, p_next->filter, in); + if (ret < 0) + return ret; + + goto cont; + } + } + break; + } + + ret = inout_add(outputs, f, out, label); + if (ret < 0) + return ret; + +cont:; + } + + return 0; +} + +int avfilter_graph_segment_link(AVFilterGraphSegment *seg, int flags, + AVFilterInOut **inputs, + AVFilterInOut **outputs) +{ + int ret; + + *inputs = NULL; + *outputs = NULL; + + if (flags) + return AVERROR(ENOSYS); + + for (size_t idx_chain = 0; idx_chain < seg->nb_chains; idx_chain++) { + AVFilterChain *ch = seg->chains[idx_chain]; + + for (size_t idx_filter = 0; idx_filter < ch->nb_filters; idx_filter++) { + AVFilterParams *p = ch->filters[idx_filter]; + + if (p->filter_name) { + ret = fail_creation_pending(seg, p->filter_name, __func__); + goto fail; + } + + if (!p->filter) + continue; + + ret = link_inputs(seg, idx_chain, idx_filter, inputs); + if (ret < 0) + goto fail; + + ret = link_outputs(seg, idx_chain, idx_filter, outputs); + if (ret < 0) + goto fail; + } + } + return 0; +fail: + avfilter_inout_free(inputs); + avfilter_inout_free(outputs); + return ret; +} + +int avfilter_graph_segment_apply(AVFilterGraphSegment *seg, int flags, + AVFilterInOut **inputs, + AVFilterInOut **outputs) +{ + int ret; + + if (flags) + return AVERROR(ENOSYS); + + ret = avfilter_graph_segment_create_filters(seg, 0); + if (ret < 0) { + av_log(seg->graph, AV_LOG_ERROR, "Error creating filters\n"); + return ret; + } + + ret = avfilter_graph_segment_apply_opts(seg, 0); + if (ret < 0) { + av_log(seg->graph, AV_LOG_ERROR, "Error applying filter options\n"); + return ret; + } + + ret = avfilter_graph_segment_init(seg, 0); + if (ret < 0) { + av_log(seg->graph, AV_LOG_ERROR, "Error initializing filters\n"); + return ret; + } + + ret = avfilter_graph_segment_link(seg, 0, inputs, outputs); + if (ret < 0) { + av_log(seg->graph, AV_LOG_ERROR, "Error linking filters\n"); + return ret; + } + + return 0; +} From patchwork Fri Jan 20 19:31:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 40080 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3ca3:b0:b9:1511:ac2c with SMTP id b35csp1431995pzj; Fri, 20 Jan 2023 11:32:22 -0800 (PST) X-Google-Smtp-Source: AMrXdXugmeoYj65RA0pflyySVaeBofMj+OxLjwfcGeL0cZihO2YBpUMk79HDdIXoPJYd4POdT/pM X-Received: by 2002:a05:6402:1f05:b0:49e:16fc:b525 with SMTP id b5-20020a0564021f0500b0049e16fcb525mr16771107edb.41.1674243141847; Fri, 20 Jan 2023 11:32:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674243141; cv=none; d=google.com; s=arc-20160816; b=OfIEGpwtqq/y50IEbnYRczRkeC5fZFu1WPoinvtrAdEZ0O17i7rC1EZFniiVMHL2+2 XJqbWkox+/O/wmVzhDbSo9k04DmMK/pYybHxT41cMO75hKz8vaCqLXll6XiQEZlh9eqN F74oRKohdhLc9Vt917iWC9cpX0a+0+cmOy0wuAE8vN1D80bmUifTWhql8eXBIMsEuu8b J/yPejPmXXmP2XAQfmbt4TJNha5VLw/hA7SoEZ9ExySfZTFpdM7XhX+zfad0NTnLT/5M k2np1MJSHo8hO8yfCr0DFp5IkYfW5iC0jes3FFENcsowG9J1g+0WhP++247pV0gH4pVd xaLA== 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:delivered-to; bh=Vle5DmgN1zmotbkAW/6Ai4I0YHmyS6b4jWoFlQLDFls=; b=mELC5N0gyDAGE5qr2DDiXToMCxq5yRL3cmSiIXwBBM5ONJK12C8gBFeIrNZdTY7H4q Fq5daTDMThmWd2lw7VYWkDK9X9TqDCaEaHopmC6BKjRuaU2bDb1MnjxXw+SFU9/B5dnw buSE7upcojVVpImcR6JZlwUm9f8F2DLfzOAbwum9U6ubLFZrRnpax3w2Nm4r4s2nLkSr xkuC2y6jLpKO4Rh2mk2KMyrj2LmI5VpuBmwQsd7ph3LF0fbppIN7iy2zmzU7L9CM2J/c 9XPB/CZCAri7eckT/WM2Ix8dmyezhJJn155gj7xbnKswRMSdQHpgr+rSuteiR1O3rPIl 59lg== ARC-Authentication-Results: i=1; mx.google.com; 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 xb13-20020a170907070d00b0084d1310711dsi46331871ejb.315.2023.01.20.11.32.21; Fri, 20 Jan 2023 11:32:21 -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; 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 25F3068BD23; Fri, 20 Jan 2023 21:32:07 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 85F4A68BCDC for ; Fri, 20 Jan 2023 21:31:59 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 11E852405B5 for ; Fri, 20 Jan 2023 20:31:58 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id i4d7E69WJJTd for ; Fri, 20 Jan 2023 20:31:56 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id AF2482404EC for ; Fri, 20 Jan 2023 20:31:56 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id CA9FB3A034C for ; Fri, 20 Jan 2023 20:31:48 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 20 Jan 2023 20:31:31 +0100 Message-Id: <20230120193132.21597-4-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230120193132.21597-1-anton@khirnov.net> References: <20230120193132.21597-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/5] lavfi/graphparser: reimplement avfilter_graph_parse* using new 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: MrWU1Kr8Q8LD --- libavfilter/graphparser.c | 537 +++++++++----------------------------- 1 file changed, 126 insertions(+), 411 deletions(-) diff --git a/libavfilter/graphparser.c b/libavfilter/graphparser.c index ac6171e0c4..5db8b95a0c 100644 --- a/libavfilter/graphparser.c +++ b/libavfilter/graphparser.c @@ -33,26 +33,6 @@ #define WHITESPACES " \n\t\r" -/** - * Link two filters together. - * - * @see avfilter_link() - */ -static int link_filter(AVFilterContext *src, int srcpad, - AVFilterContext *dst, int dstpad, - void *log_ctx) -{ - int ret; - if ((ret = avfilter_link(src, srcpad, dst, dstpad))) { - av_log(log_ctx, AV_LOG_ERROR, - "Cannot create the link %s:%d -> %s:%d\n", - src->filter->name, srcpad, dst->filter->name, dstpad); - return ret; - } - - return 0; -} - /** * Parse the name of a link, which has the format "[linkname]". * @@ -87,119 +67,6 @@ static char *parse_link_name(const char **buf, void *log_ctx) return name; } -/** - * Create an instance of a filter, initialize and insert it in the - * filtergraph in *ctx. - * - * @param filt_ctx put here a filter context in case of successful creation and configuration, NULL otherwise. - * @param ctx the filtergraph context - * @param index an index which is supposed to be unique for each filter instance added to the filtergraph - * @param name the name of the filter to create, can be filter name or filter_name\@id as instance name - * @param args the arguments provided to the filter during its initialization - * @param log_ctx the log context to use - * @return >= 0 in case of success, a negative AVERROR code otherwise - */ -static int create_filter(AVFilterContext **filt_ctx, AVFilterGraph *ctx, int index, - const char *name, const char *args, void *log_ctx) -{ - const AVFilter *filt; - char name2[30]; - const char *inst_name = NULL, *filt_name = NULL; - int ret, k; - - av_strlcpy(name2, name, sizeof(name2)); - - for (k = 0; name2[k]; k++) { - if (name2[k] == '@' && name[k+1]) { - name2[k] = 0; - inst_name = name; - filt_name = name2; - break; - } - } - - if (!inst_name) { - snprintf(name2, sizeof(name2), "Parsed_%s_%d", name, index); - inst_name = name2; - filt_name = name; - } - - filt = avfilter_get_by_name(filt_name); - - if (!filt) { - av_log(log_ctx, AV_LOG_ERROR, - "No such filter: '%s'\n", filt_name); - return AVERROR(EINVAL); - } - - *filt_ctx = avfilter_graph_alloc_filter(ctx, filt, inst_name); - if (!*filt_ctx) { - av_log(log_ctx, AV_LOG_ERROR, - "Error creating filter '%s'\n", filt_name); - return AVERROR(ENOMEM); - } - - if (!strcmp(filt_name, "scale") && ctx->scale_sws_opts) { - ret = av_set_options_string(*filt_ctx, ctx->scale_sws_opts, "=", ":"); - if (ret < 0) - return ret; - } - - ret = avfilter_init_str(*filt_ctx, args); - if (ret < 0) { - av_log(log_ctx, AV_LOG_ERROR, - "Error initializing filter '%s'", filt_name); - if (args) - av_log(log_ctx, AV_LOG_ERROR, " with args '%s'", args); - av_log(log_ctx, AV_LOG_ERROR, "\n"); - avfilter_free(*filt_ctx); - *filt_ctx = NULL; - } - - return ret; -} - -/** - * Parse a string of the form FILTER_NAME[=PARAMS], and create a - * corresponding filter instance which is added to graph with - * create_filter(). - * - * @param filt_ctx Pointer that is set to the created and configured filter - * context on success, set to NULL on failure. - * @param filt_ctx put here a pointer to the created filter context on - * success, NULL otherwise - * @param buf pointer to the buffer to parse, *buf will be updated to - * point to the char next after the parsed string - * @param index an index which is assigned to the created filter - * instance, and which is supposed to be unique for each filter - * instance added to the filtergraph - * @return >= 0 in case of success, a negative AVERROR code otherwise - */ -static int parse_filter(AVFilterContext **filt_ctx, const char **buf, AVFilterGraph *graph, - int index, void *log_ctx) -{ - char *opts = NULL; - char *name = av_get_token(buf, "=,;["); - int ret; - - if (!name) - return AVERROR(ENOMEM); - - if (**buf == '=') { - (*buf)++; - opts = av_get_token(buf, "[],;"); - if (!opts) { - av_free(name); - return AVERROR(ENOMEM); - } - } - - ret = create_filter(filt_ctx, graph, index, name, opts, log_ctx); - av_free(name); - av_free(opts); - return ret; -} - AVFilterInOut *avfilter_inout_alloc(void) { return av_mallocz(sizeof(AVFilterInOut)); @@ -232,12 +99,6 @@ static AVFilterInOut *extract_inout(const char *label, AVFilterInOut **links) return ret; } -static void insert_inout(AVFilterInOut **inouts, AVFilterInOut *element) -{ - element->next = *inouts; - *inouts = element; -} - static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element) { while (*inouts && (*inouts)->next) @@ -250,144 +111,6 @@ static void append_inout(AVFilterInOut **inouts, AVFilterInOut **element) *element = NULL; } -static int link_filter_inouts(AVFilterContext *filt_ctx, - AVFilterInOut **curr_inputs, - AVFilterInOut **open_inputs, void *log_ctx) -{ - int pad, ret; - - for (pad = 0; pad < filt_ctx->nb_inputs; pad++) { - AVFilterInOut *p = *curr_inputs; - - if (p) { - *curr_inputs = (*curr_inputs)->next; - p->next = NULL; - } else if (!(p = av_mallocz(sizeof(*p)))) - return AVERROR(ENOMEM); - - if (p->filter_ctx) { - ret = link_filter(p->filter_ctx, p->pad_idx, filt_ctx, pad, log_ctx); - av_freep(&p->name); - av_freep(&p); - if (ret < 0) - return ret; - } else { - p->filter_ctx = filt_ctx; - p->pad_idx = pad; - append_inout(open_inputs, &p); - } - } - - if (*curr_inputs) { - av_log(log_ctx, AV_LOG_ERROR, - "Too many inputs specified for the \"%s\" filter.\n", - filt_ctx->filter->name); - return AVERROR(EINVAL); - } - - pad = filt_ctx->nb_outputs; - while (pad--) { - AVFilterInOut *currlinkn = av_mallocz(sizeof(AVFilterInOut)); - if (!currlinkn) - return AVERROR(ENOMEM); - currlinkn->filter_ctx = filt_ctx; - currlinkn->pad_idx = pad; - insert_inout(curr_inputs, currlinkn); - } - - return 0; -} - -static int parse_inputs(const char **buf, AVFilterInOut **curr_inputs, - AVFilterInOut **open_outputs, void *log_ctx) -{ - AVFilterInOut *parsed_inputs = NULL; - int pad = 0; - - while (**buf == '[') { - char *name = parse_link_name(buf, log_ctx); - AVFilterInOut *match; - - if (!name) { - avfilter_inout_free(&parsed_inputs); - return AVERROR(EINVAL); - } - - /* First check if the label is not in the open_outputs list */ - match = extract_inout(name, open_outputs); - - if (match) { - av_free(name); - } else { - /* Not in the list, so add it as an input */ - if (!(match = av_mallocz(sizeof(AVFilterInOut)))) { - avfilter_inout_free(&parsed_inputs); - av_free(name); - return AVERROR(ENOMEM); - } - match->name = name; - match->pad_idx = pad; - } - - append_inout(&parsed_inputs, &match); - - *buf += strspn(*buf, WHITESPACES); - pad++; - } - - append_inout(&parsed_inputs, curr_inputs); - *curr_inputs = parsed_inputs; - - return pad; -} - -static int parse_outputs(const char **buf, AVFilterInOut **curr_inputs, - AVFilterInOut **open_inputs, - AVFilterInOut **open_outputs, void *log_ctx) -{ - int ret, pad = 0; - - while (**buf == '[') { - char *name = parse_link_name(buf, log_ctx); - AVFilterInOut *match; - - AVFilterInOut *input = *curr_inputs; - - if (!name) - return AVERROR(EINVAL); - - if (!input) { - av_log(log_ctx, AV_LOG_ERROR, - "No output pad can be associated to link label '%s'.\n", name); - av_free(name); - return AVERROR(EINVAL); - } - *curr_inputs = (*curr_inputs)->next; - - /* First check if the label is not in the open_inputs list */ - match = extract_inout(name, open_inputs); - - if (match) { - ret = link_filter(input->filter_ctx, input->pad_idx, - match->filter_ctx, match->pad_idx, log_ctx); - av_freep(&match->name); - av_freep(&name); - av_freep(&match); - av_freep(&input); - if (ret < 0) - return ret; - } else { - /* Not in the list, so add the first input as an open_output */ - input->name = name; - insert_inout(open_outputs, input); - } - *buf += strspn(*buf, WHITESPACES); - pad++; - } - - return pad; -} - static int parse_sws_flags(const char **buf, char **dst, void *log_ctx) { char *p = strchr(*buf, ';'); @@ -415,66 +138,24 @@ int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs) { - int index = 0, ret = 0; - char chr = 0; - - AVFilterInOut *curr_inputs = NULL, *open_inputs = NULL, *open_outputs = NULL; - - filters += strspn(filters, WHITESPACES); - - if ((ret = parse_sws_flags(&filters, &graph->scale_sws_opts, graph)) < 0) - goto end; - - do { - AVFilterContext *filter; - filters += strspn(filters, WHITESPACES); - - if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, graph)) < 0) - goto end; - if ((ret = parse_filter(&filter, &filters, graph, index, graph)) < 0) - goto end; - - - if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, graph)) < 0) - goto end; - - if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs, - graph)) < 0) - goto end; - - filters += strspn(filters, WHITESPACES); - chr = *filters++; + AVFilterGraphSegment *seg; + int ret; - if (chr == ';' && curr_inputs) - append_inout(&open_outputs, &curr_inputs); - index++; - } while (chr == ',' || chr == ';'); + ret = avfilter_graph_segment_parse(graph, filters, 0, &seg); + if (ret < 0) + return ret; - if (chr) { - av_log(graph, AV_LOG_ERROR, - "Unable to parse graph description substring: \"%s\"\n", - filters - 1); - ret = AVERROR(EINVAL); + ret = avfilter_graph_segment_apply(seg, 0, inputs, outputs); + avfilter_graph_segment_free(&seg); + if (ret < 0) goto end; - } - - append_inout(&open_outputs, &curr_inputs); - - *inputs = open_inputs; - *outputs = open_outputs; return 0; end: while (graph->nb_filters) avfilter_free(graph->filters[0]); av_freep(&graph->filters); - avfilter_inout_free(&open_inputs); - avfilter_inout_free(&open_outputs); - avfilter_inout_free(&curr_inputs); - - *inputs = NULL; - *outputs = NULL; return ret; } @@ -542,90 +223,6 @@ int avfilter_graph_parse(AVFilterGraph *graph, const char *filters, return ret; } -int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters, - AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr, - void *log_ctx) -{ - int index = 0, ret = 0; - char chr = 0; - - AVFilterInOut *curr_inputs = NULL; - AVFilterInOut *open_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL; - AVFilterInOut *open_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL; - - if ((ret = parse_sws_flags(&filters, &graph->scale_sws_opts, graph)) < 0) - goto end; - - do { - AVFilterContext *filter; - const char *filterchain = filters; - filters += strspn(filters, WHITESPACES); - - if ((ret = parse_inputs(&filters, &curr_inputs, &open_outputs, log_ctx)) < 0) - goto end; - - if ((ret = parse_filter(&filter, &filters, graph, index, log_ctx)) < 0) - goto end; - - if (filter->nb_inputs == 1 && !curr_inputs && !index) { - /* First input pad, assume it is "[in]" if not specified */ - const char *tmp = "[in]"; - if ((ret = parse_inputs(&tmp, &curr_inputs, &open_outputs, log_ctx)) < 0) - goto end; - } - - if ((ret = link_filter_inouts(filter, &curr_inputs, &open_inputs, log_ctx)) < 0) - goto end; - - if ((ret = parse_outputs(&filters, &curr_inputs, &open_inputs, &open_outputs, - log_ctx)) < 0) - goto end; - - filters += strspn(filters, WHITESPACES); - chr = *filters++; - - if (chr == ';' && curr_inputs) { - av_log(log_ctx, AV_LOG_ERROR, - "Invalid filterchain containing an unlabelled output pad: \"%s\"\n", - filterchain); - ret = AVERROR(EINVAL); - goto end; - } - index++; - } while (chr == ',' || chr == ';'); - - if (chr) { - av_log(log_ctx, AV_LOG_ERROR, - "Unable to parse graph description substring: \"%s\"\n", - filters - 1); - ret = AVERROR(EINVAL); - goto end; - } - - if (curr_inputs) { - /* Last output pad, assume it is "[out]" if not specified */ - const char *tmp = "[out]"; - if ((ret = parse_outputs(&tmp, &curr_inputs, &open_inputs, &open_outputs, - log_ctx)) < 0) - goto end; - } - -end: - /* clear open_in/outputs only if not passed as parameters */ - if (open_inputs_ptr) *open_inputs_ptr = open_inputs; - else avfilter_inout_free(&open_inputs); - if (open_outputs_ptr) *open_outputs_ptr = open_outputs; - else avfilter_inout_free(&open_outputs); - avfilter_inout_free(&curr_inputs); - - if (ret < 0) { - while (graph->nb_filters) - avfilter_free(graph->filters[0]); - av_freep(&graph->filters); - } - return ret; -} - static void pad_params_free(AVFilterPadParams **pfpp) { AVFilterPadParams *fpp = *pfpp; @@ -1298,3 +895,121 @@ int avfilter_graph_segment_apply(AVFilterGraphSegment *seg, int flags, return 0; } + +int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters, + AVFilterInOut **open_inputs_ptr, AVFilterInOut **open_outputs_ptr, + void *log_ctx) +{ + AVFilterInOut *user_inputs = open_inputs_ptr ? *open_inputs_ptr : NULL; + AVFilterInOut *user_outputs = open_outputs_ptr ? *open_outputs_ptr : NULL; + + AVFilterInOut *inputs = NULL, *outputs = NULL; + AVFilterGraphSegment *seg = NULL; + AVFilterChain *ch; + AVFilterParams *p; + int ret; + + ret = avfilter_graph_segment_parse(graph, filters, 0, &seg); + if (ret < 0) + goto end; + + ret = avfilter_graph_segment_create_filters(seg, 0); + if (ret < 0) + goto end; + + ret = avfilter_graph_segment_apply_opts(seg, 0); + if (ret < 0) + goto end; + + ret = avfilter_graph_segment_init(seg, 0); + if (ret < 0) + goto end; + + /* First input pad, assume it is "[in]" if not specified */ + p = seg->chains[0]->filters[0]; + if (p->filter->nb_inputs == 1 && !p->inputs) { + const char *tmp = "[in]"; + + ret = linklabels_parse(graph, &tmp, &p->inputs, &p->nb_inputs); + if (ret < 0) + goto end; + } + + /* Last output pad, assume it is "[out]" if not specified */ + ch = seg->chains[seg->nb_chains - 1]; + p = ch->filters[ch->nb_filters - 1]; + if (p->filter->nb_outputs == 1 && !p->outputs) { + const char *tmp = "[out]"; + + ret = linklabels_parse(graph, &tmp, &p->outputs, &p->nb_outputs); + if (ret < 0) + goto end; + } + + ret = avfilter_graph_segment_apply(seg, 0, &inputs, &outputs); + avfilter_graph_segment_free(&seg); + if (ret < 0) + goto end; + + // process user-supplied inputs/outputs + while (inputs) { + AVFilterInOut *cur, *match = NULL; + + cur = inputs; + inputs = cur->next; + cur->next = NULL; + + if (cur->name) + match = extract_inout(cur->name, &user_outputs); + + if (match) { + ret = avfilter_link(match->filter_ctx, match->pad_idx, + cur->filter_ctx, cur->pad_idx); + avfilter_inout_free(&match); + avfilter_inout_free(&cur); + if (ret < 0) + goto end; + } else + append_inout(&user_inputs, &cur); + } + while (outputs) { + AVFilterInOut *cur, *match = NULL; + + cur = outputs; + outputs = cur->next; + cur->next = NULL; + + if (cur->name) + match = extract_inout(cur->name, &user_inputs); + + if (match) { + ret = avfilter_link(cur->filter_ctx, cur->pad_idx, + match->filter_ctx, match->pad_idx); + avfilter_inout_free(&match); + avfilter_inout_free(&cur); + if (ret < 0) + goto end; + } else + append_inout(&user_outputs, &cur); + } + +end: + avfilter_graph_segment_free(&seg); + + if (ret < 0) { + while (graph->nb_filters) + avfilter_free(graph->filters[0]); + av_freep(&graph->filters); + } + + /* clear open_in/outputs only if not passed as parameters */ + if (open_inputs_ptr) *open_inputs_ptr = user_inputs; + else avfilter_inout_free(&user_inputs); + if (open_outputs_ptr) *open_outputs_ptr = user_outputs; + else avfilter_inout_free(&user_outputs); + + avfilter_inout_free(&inputs); + avfilter_inout_free(&outputs); + + return ret; +} From patchwork Fri Jan 20 19:31:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 40082 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3ca3:b0:b9:1511:ac2c with SMTP id b35csp1432341pzj; Fri, 20 Jan 2023 11:32:58 -0800 (PST) X-Google-Smtp-Source: AMrXdXv69RIbE+acJgCKCjBSxIPWRgiQHXu/Vl8N60IBkm10fo7C+62ZkWxr5uAH5imxh/og29ly X-Received: by 2002:a05:6402:548f:b0:49c:1fe4:9efc with SMTP id fg15-20020a056402548f00b0049c1fe49efcmr17272349edb.40.1674243177920; Fri, 20 Jan 2023 11:32:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674243177; cv=none; d=google.com; s=arc-20160816; b=UE0R2aJ+HnAEDlNsbx+BOd7LA9ZMY+LpueNGNvCh0lf2sMq0qLMWc6YETZ2pRFOnbv VZCc5ivgGAOSCNB8em/UhtlHFJBVHjQGDUZE4LUwcmEDvSzWzGAj8t3SS8mlTSm5Vli6 YLX4G8/JE4iHGhNIBeObx9hcTzngSD+EyKTU7q6LExHtaOZFm2YT2+Ru04TZgx62gDBe QylrvCEnkHe7Y+epX6A0wfSos6FcOrR+p3lu7q1AA8u60p9cOEGerrAOft6MzzQZS23N tEGKG8rvIYOC17iJG7i1djOcswtE1ao9bLNL7aeRv0AGkCGgLj0wd+OIqczCMzgkafCc qdIg== 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:delivered-to; bh=37QTbnpEwLa4epZAkl9YUQ0EnNTQyfwIEL4HmTW9ibY=; b=QTOsdCVMT34Gfq812NljDIldGb0dgg0bBLf056vfED4F7L/r0FyLXZPmRyYpuo/5YO KYwq/M7D+l7Xth/0lVw1Sf3agJOizBXwfckJuC59cCw/ciAAhVQQzx9m9maYCBrXPG/m YyHwTcHMSNpRPOAbJO3OLsVEEQcGmCwcWYVWtYVrh+aCvLn73h/P+ziRpWxRbRxh3Fv9 OllDgh2h1Cw//YjRcuyI9rGVbk6flL21uhd3VElyhXO3k0207T/D19PH+nvLy2J0uQMD U7mYFwH8VK23UhbZOcrd7PANsYk52pcojiB3bRyY7uvZ34jxJiOlkPeH8ily1wNphYLH 1oIw== ARC-Authentication-Results: i=1; mx.google.com; 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 l19-20020a056402125300b0049deaf9d2absi19912672edw.126.2023.01.20.11.32.41; Fri, 20 Jan 2023 11:32:57 -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; 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 E08A568BD35; Fri, 20 Jan 2023 21:32:08 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B117E68BCDC for ; Fri, 20 Jan 2023 21:31:59 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 758AB240591 for ; Fri, 20 Jan 2023 20:31:59 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id QDbixY-Vx1Kp for ; Fri, 20 Jan 2023 20:31:58 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id B0CC42404F5 for ; Fri, 20 Jan 2023 20:31:56 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id D5ED83A0354 for ; Fri, 20 Jan 2023 20:31:48 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 20 Jan 2023 20:31:32 +0100 Message-Id: <20230120193132.21597-5-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230120193132.21597-1-anton@khirnov.net> References: <20230120193132.21597-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/5] fftools/ffmpeg: add special syntax for loading filter options from files 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: XFDcbvRZE2yv Many filters accept user-provided data that is cumbersome to provide as text strings - e.g. binary files or very long text. For that reason such filters typically provide a option whose value is the path from which the filter loads the actual data. However, filters doing their own IO internally is a layering violation that the callers may not expect, and is thus best avoided. With the recently introduced graph segment parsing API, loading option values from files can now be handled by the caller. This commit makes use of the new API in ffmpeg CLI. Any option name in the filtergraph syntax can now be prefixed with a slash '/'. This will cause ffmpeg to interpret the value as the path to load the actual value from. --- Changelog | 2 + doc/filters.texi | 11 +++ fftools/ffmpeg_filter.c | 154 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 165 insertions(+), 2 deletions(-) diff --git a/Changelog b/Changelog index 5c01e8365e..2d1ea9540e 100644 --- a/Changelog +++ b/Changelog @@ -29,6 +29,8 @@ version : - corr video filter - adrc audio filter - afdelaysrc audio filter +- filtergraph syntax in ffmpeg CLI now supports passing file contents + as option values, by prefixing option name with '/' version 5.1: diff --git a/doc/filters.texi b/doc/filters.texi index be70a2396b..7ff9948239 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -171,6 +171,17 @@ within the quoted text; otherwise the argument string is considered terminated when the next special character (belonging to the set @samp{[]=;,}) is encountered. +A special syntax implemented in the @command{ffmpeg} CLI tool allows loading +option values from files. This is done be prepending a slash '/' to the option +name, then the supplied value is interpreted as a path from which the actual +value is loaded. E.g. +@example +ffmpeg -i -vf drawtext=/text=/tmp/some_text +@end example +will load the text to be drawn from @file{/tmp/some_text}. API users wishing to +implement a similar feature should use the @code{avfilter_graph_segment_*()} +functions together with custom IO code. + The name and arguments of the filter are optionally preceded and followed by a list of link labels. A link label allows one to name a link and associate it to a filter output diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index 7eb656dbe5..3f79133f75 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -314,6 +314,156 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in) ist->filters[ist->nb_filters - 1] = ifilter; } +static int read_binary(const char *path, uint8_t **data, int *len) +{ + AVIOContext *io = NULL; + int64_t fsize; + int ret; + + *data = NULL; + *len = 0; + + ret = avio_open2(&io, path, AVIO_FLAG_READ, &int_cb, NULL); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s': %s\n", + path, av_err2str(ret)); + return ret; + } + + fsize = avio_size(io); + if (fsize < 0 || fsize > INT_MAX) { + av_log(NULL, AV_LOG_ERROR, "Cannot obtain size of file %s\n", path); + ret = AVERROR(EIO); + goto fail; + } + + *data = av_malloc(fsize); + if (!*data) { + ret = AVERROR(ENOMEM); + goto fail; + } + + ret = avio_read(io, *data, fsize); + if (ret != fsize) { + av_log(NULL, AV_LOG_ERROR, "Error reading file %s\n", path); + ret = ret < 0 ? ret : AVERROR(EIO); + goto fail; + } + + *len = fsize; + + return 0; +fail: + avio_close(io); + av_freep(data); + *len = 0; + return ret; +} + +static int filter_opt_apply(AVFilterContext *f, const char *key, const char *val) +{ + const AVOption *o; + int ret; + + ret = av_opt_set(f, key, val, AV_OPT_SEARCH_CHILDREN); + if (ret >= 0) + return 0; + + if (ret == AVERROR_OPTION_NOT_FOUND && key[0] == '/') + o = av_opt_find(f, key + 1, NULL, 0, AV_OPT_SEARCH_CHILDREN); + if (!o) + goto err_apply; + + // key is a valid option name prefixed with '/' + // interpret value as a path from which to load the actual option value + key++; + + if (o->type == AV_OPT_TYPE_BINARY) { + uint8_t *data; + int len; + + ret = read_binary(val, &data, &len); + if (ret < 0) + goto err_load; + + ret = av_opt_set_bin(f, key, data, len, AV_OPT_SEARCH_CHILDREN); + av_freep(&data); + } else { + char *data = file_read(val); + if (!val) { + ret = AVERROR(EIO); + goto err_load; + } + + ret = av_opt_set(f, key, data, AV_OPT_SEARCH_CHILDREN); + av_freep(&data); + } + if (ret < 0) + goto err_apply; + + return 0; + +err_apply: + av_log(NULL, AV_LOG_ERROR, + "Error applying option '%s' to filter '%s': %s\n", + key, f->filter->name, av_err2str(ret)); + return ret; +err_load: + av_log(NULL, AV_LOG_ERROR, + "Error loading value for option '%s' from file '%s'\n", + key, val); + return ret; +} + +static int graph_opts_apply(AVFilterGraphSegment *seg) +{ + for (size_t i = 0; i < seg->nb_chains; i++) { + AVFilterChain *ch = seg->chains[i]; + + for (size_t j = 0; j < ch->nb_filters; j++) { + AVFilterParams *p = ch->filters[j]; + const AVDictionaryEntry *e = NULL; + + av_assert0(p->filter); + + while ((e = av_dict_iterate(p->opts, e))) { + int ret = filter_opt_apply(p->filter, e->key, e->value); + if (ret < 0) + return ret; + } + + av_dict_free(&p->opts); + } + } + + return 0; +} + +static int graph_parse(AVFilterGraph *graph, const char *desc, + AVFilterInOut **inputs, AVFilterInOut **outputs) +{ + AVFilterGraphSegment *seg; + int ret; + + ret = avfilter_graph_segment_parse(graph, desc, 0, &seg); + if (ret < 0) + return ret; + + ret = avfilter_graph_segment_create_filters(seg, 0); + if (ret < 0) + goto fail; + + ret = graph_opts_apply(seg); + if (ret < 0) + goto fail; + + ret = avfilter_graph_segment_apply(seg, 0, inputs, outputs); + +fail: + avfilter_graph_segment_free(&seg); + return ret; +} + int init_complex_filtergraph(FilterGraph *fg) { AVFilterInOut *inputs, *outputs, *cur; @@ -327,7 +477,7 @@ int init_complex_filtergraph(FilterGraph *fg) return AVERROR(ENOMEM); graph->nb_threads = 1; - ret = avfilter_graph_parse2(graph, fg->graph_desc, &inputs, &outputs); + ret = graph_parse(graph, fg->graph_desc, &inputs, &outputs); if (ret < 0) goto fail; @@ -1004,7 +1154,7 @@ int configure_filtergraph(FilterGraph *fg) fg->graph->nb_threads = filter_complex_nbthreads; } - if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0) + if ((ret = graph_parse(fg->graph, graph_desc, &inputs, &outputs)) < 0) goto fail; ret = hw_device_setup_for_filter(fg);