From patchwork Wed Dec 13 13:12:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 45103 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1225:b0:181:818d:5e7f with SMTP id v37csp4263853pzf; Wed, 13 Dec 2023 05:16:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IHNLimatyXUjhpMqX1sm8hbdy71zn0PUGwyyHmKrM9h/H/eRrQid/GKMkGy1vgfVyPWJG5l X-Received: by 2002:a17:907:c312:b0:a01:ae7b:d19b with SMTP id tl18-20020a170907c31200b00a01ae7bd19bmr9388469ejc.7.1702473408004; Wed, 13 Dec 2023 05:16:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702473407; cv=none; d=google.com; s=arc-20160816; b=ApW8GegTnKrqAnzmzJ8NzuiKoVMNWIdzFqUc744fMe5eoHDkTjDHiQnAmkDKQWSu1m /1xo5ONPiPSfzvah4ic5wZDOdYgHW2bH/yvbNoGpyFZwODPPaDgzNJUc3HaC3+8tvZPB MavNe7M0833MG8oYG2A2ANirTwUIJMQ0bVcXrpf//txdEr3hpRZRnRWqLi214DsKTnaw Of06v3sPybkPbHB/fVx82y9DfZOi4RRIdlYrNZxVkoTQ32YkxLEOkjc9lYfsy1B/SLQG OSs0v64XaORksxfoIoQGQmTFstP+W2loBgx4d86t3u4ihvTD3H/PY2NimJNLLCjHLOC7 /0dQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=uRxwznLNbSEs6izUhIUzwBVJDdcxyboyueBFe8Q34PM=; fh=xmAeKtysnShNOmkhiJmYkS30uw4Fu2hvBJ7qlIwukxQ=; b=OeWgUZCt8o2w/0MLnLcm8RAGBC15R2K7tsSmiHLR4aCYx0knKl4yKvCPcDezIrgqIk rtb91xkgLIwM9vbJwVxwe+3N0gjxit5TOR79h7MEZiBwFpBitIRtg4U5wRvomkb5QUeh b9T/HZq4VtdvRj7FDsDb5OpRSdalP1DwdQttMhfL+UaZ1sXY7FpHqF81WhQH5B9H5EMg TVwITY3ipJ96jojMaCRimP+HcIKcRsIc4rNtFxTTZCdaFT55eQP0BFF23DPXcW6VAlkC BtOBo01MqqMoz/t5v5sHMxxnMfQI3aqgTxiD/3aB5tIevD1Bps2X9Uym7k6qubr06MVC Yy7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=Emo3+cmk; 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 m7-20020a170906234700b00a1f673b5eb5si4943966eja.784.2023.12.13.05.16.47; Wed, 13 Dec 2023 05:16:47 -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=@haasn.xyz header.s=mail header.b=Emo3+cmk; 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 D01EF68D1DD; Wed, 13 Dec 2023 15:15:52 +0200 (EET) 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 51B7468D15F for ; Wed, 13 Dec 2023 15:15:45 +0200 (EET) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id C84034BD4A; Wed, 13 Dec 2023 14:15:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1702473342; bh=ngZwxPipoD6o9iWOx9WY5GaYJvmLQ3OjQGlZ5Gv5onE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Emo3+cmkXWNC6ErkdSUMV0dCz86u+SU/DwHw54cm0RbCgxsiPYf2cwbZvqpdYoorL 65Rsq6ccrKFZYybceYJqWPslUIZKUHiEuKuCJeanQTh+SaHXpMFIM+EweWJiQV6sfN GThIB1wkwu/RGHJZpEr84oWch3wpstTLojRS7TP0= From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Wed, 13 Dec 2023 14:12:10 +0100 Message-ID: <20231213131536.10242-14-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231213131536.10242-1-ffmpeg@haasn.xyz> References: <20231213131536.10242-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 13/15] avfilter/vf_format: re-use AVFilterFormats for pix_fmt parsing 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: 4bi/61jLs5et From: Niklas Haas Rewrite the format parsing code to make it more easily generalizable. In particular, `invert_formats` does not depend on the type of format list passed to it, which allows me to re-use this helper in an upcoming commit. Slightly shortens the code, at the sole cost of doing several malloc (ff_add_format) instead of a single malloc. --- libavfilter/vf_format.c | 103 +++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 60 deletions(-) diff --git a/libavfilter/vf_format.c b/libavfilter/vf_format.c index 1189bd61c2..59832b7768 100644 --- a/libavfilter/vf_format.c +++ b/libavfilter/vf_format.c @@ -41,25 +41,48 @@ typedef struct FormatContext { const AVClass *class; char *pix_fmts; - /** - * pix_fmts parsed into AVPixelFormats and terminated with - * AV_PIX_FMT_NONE - */ - enum AVPixelFormat *formats; + AVFilterFormats *formats; ///< parsed from `pix_fmts` } FormatContext; static av_cold void uninit(AVFilterContext *ctx) { FormatContext *s = ctx->priv; - av_freep(&s->formats); + ff_formats_unref(&s->formats); +} + +static av_cold int invert_formats(AVFilterFormats **fmts, + AVFilterFormats *allfmts) +{ + if (!allfmts) + return AVERROR(ENOMEM); + if (!*fmts) { + /* empty fmt list means no restriction, regardless of filter type */ + ff_formats_unref(&allfmts); + return 0; + } + + for (int i = 0; i < allfmts->nb_formats; i++) { + for (int j = 0; j < (*fmts)->nb_formats; j++) { + if (allfmts->formats[i] == (*fmts)->formats[j]) { + /* format is forbidden, remove it from allfmts list */ + memmove(&allfmts->formats[i], &allfmts->formats[i+1], + (allfmts->nb_formats - (i+1)) * sizeof(*allfmts->formats)); + allfmts->nb_formats--; + i--; /* repeat loop with same idx */ + break; + } + } + } + + ff_formats_unref(fmts); + *fmts = allfmts; + return 0; } static av_cold int init(AVFilterContext *ctx) { FormatContext *s = ctx->priv; - char *cur, *sep; - int nb_formats = 1; - int i; + enum AVPixelFormat pix_fmt; int ret; if (!s->pix_fmts) { @@ -67,64 +90,24 @@ static av_cold int init(AVFilterContext *ctx) return AVERROR(EINVAL); } - /* count the formats */ - cur = s->pix_fmts; - while ((cur = strchr(cur, '|'))) { - nb_formats++; - if (*cur) - cur++; - } - - s->formats = av_malloc_array(nb_formats + 1, sizeof(*s->formats)); - if (!s->formats) - return AVERROR(ENOMEM); - - /* parse the list of formats */ - cur = s->pix_fmts; - for (i = 0; i < nb_formats; i++) { + for (char *sep, *cur = s->pix_fmts; cur; cur = sep) { sep = strchr(cur, '|'); - if (sep) + if (sep && *sep) *sep++ = 0; - - if ((ret = ff_parse_pixel_format(&s->formats[i], cur, ctx)) < 0) + if ((ret = ff_parse_pixel_format(&pix_fmt, cur, ctx)) < 0 || + (ret = ff_add_format(&s->formats, pix_fmt)) < 0) return ret; - - cur = sep; } - s->formats[nb_formats] = AV_PIX_FMT_NONE; if (!strcmp(ctx->filter->name, "noformat")) { - const AVPixFmtDescriptor *desc = NULL; - enum AVPixelFormat *formats_allowed; - int nb_formats_lavu = 0, nb_formats_allowed = 0; - - /* count the formats known to lavu */ - while ((desc = av_pix_fmt_desc_next(desc))) - nb_formats_lavu++; - - formats_allowed = av_malloc_array(nb_formats_lavu + 1, sizeof(*formats_allowed)); - if (!formats_allowed) - return AVERROR(ENOMEM); - - /* for each format known to lavu, check if it's in the list of - * forbidden formats */ - while ((desc = av_pix_fmt_desc_next(desc))) { - enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(desc); - - for (i = 0; i < nb_formats; i++) { - if (s->formats[i] == pix_fmt) - break; - } - if (i < nb_formats) - continue; - - formats_allowed[nb_formats_allowed++] = pix_fmt; - } - formats_allowed[nb_formats_allowed] = AV_PIX_FMT_NONE; - av_freep(&s->formats); - s->formats = formats_allowed; + if ((ret = invert_formats(&s->formats, ff_all_formats(AVMEDIA_TYPE_VIDEO))) < 0) + return ret; } + /* hold on to a ref for the lifetime of the filter */ + if ((ret = ff_formats_ref(s->formats, &s->formats)) < 0) + return ret; + return 0; } @@ -132,7 +115,7 @@ static int query_formats(AVFilterContext *ctx) { FormatContext *s = ctx->priv; - return ff_set_common_formats_from_list(ctx, s->formats); + return ff_set_common_formats(ctx, s->formats); }