From patchwork Tue Nov 23 10:30:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 31557 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:d206:0:0:0:0:0 with SMTP id q6csp8445893iob; Tue, 23 Nov 2021 02:31:39 -0800 (PST) X-Google-Smtp-Source: ABdhPJzGfR1kSV09TE+swtijysfgCzutvAWC/Tx+s2k7YIqK9QU2JB4PqWdDx1feGxAUH4RQqCH5 X-Received: by 2002:a05:6402:358c:: with SMTP id y12mr7951589edc.44.1637663499009; Tue, 23 Nov 2021 02:31:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637663499; cv=none; d=google.com; s=arc-20160816; b=sx7/TYfORnk5uS46Tl4+gwySMOC0P1D4BFFadD5qlNmzAtACoPBs0LMJmUsWHkxRqs q0HXMgfgpw8ZOU1B5ejUurTgccqq/CVH7d+isVmoXF+99sEcmmoOf5+HrmjJ2erJ51tQ 3sQzcDjhqFDHR0COQy/EnA1l8smDdg2tjltP4gG3Jdf/SV+8cGv0OGS9ocOBb5H/f1gH QNZJIX1LKw/8JoN7PkUMOJ+J6/ikD4h7Edd/ONbOdnRheKx5ibVMw8fGUt+zBZ+3Wvia qctCuIVU609VA1S3ZF7HMtIBpfTp+sDbe4NT7pbgSgyGd+Wi6rcHTKuWsIqy4uH/LX2F mWgA== 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=4xv2abIBgu+1ZlC/3sVy8eOWqkVWyD0ZBwLJNGqNZ5o=; b=vI7mlfhcCVftsa8F6DjcwxwPcYEijB5VvKS7EKK9Z6aNOgizDkMDQs+tLhSfLgM8EW U+UrHu+0LCS3DjQPPw4UR/BoNAVy7rK7T/lxg61l0GXdkkyjHZbtaQ/1zB4oNaUJD98a DDHFjGVR4VUogYJfQwv85rNUUfNHCkN5TQH4JfyBvRR9CgNhroLVD0lmlYreyUu1U8F8 OZnkt0YXJQtgstEAxaIin6qqMLzOcqw36I3LLDt2tlt8QX/yjpN3N0AT7CnJNGmFtdSp Xj/RDllewq0K3subLRaA62Qoe6nb4jkfSY+mGgHqRxecCNM49TkPXjVZPBLav34z/2cr x4gg== 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 ga32si33955582ejc.769.2021.11.23.02.31.38; Tue, 23 Nov 2021 02:31:38 -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 4A4D068AEA4; Tue, 23 Nov 2021 12:31:16 +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 E138068AE74 for ; Tue, 23 Nov 2021 12:31:07 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 92BF924017C for ; Tue, 23 Nov 2021 11:31:07 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id wpEWb5r4dw19 for ; Tue, 23 Nov 2021 11:31:07 +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 005F424017E for ; Tue, 23 Nov 2021 11:31:05 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id D459F3A0743; Tue, 23 Nov 2021 11:31:05 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 23 Nov 2021 11:30:01 +0100 Message-Id: <20211123103001.12888-4-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211123103001.12888-1-anton@khirnov.net> References: <20211123103001.12888-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/4] ffmpeg: only copy bits_per_sample from decoder when it remains valid 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: z13EqZsp3FDs I.e. when the only filters that are applied do not modify the frame data. --- fftools/ffmpeg.c | 14 +++++--------- fftools/ffmpeg.h | 3 +++ fftools/ffmpeg_filter.c | 26 ++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 55ad4e558e..ff4bae11b3 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3417,7 +3417,7 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) if (ost->bits_per_raw_sample) enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample; - else if (dec_ctx) + else if (dec_ctx && ost->filter->graph->is_meta) enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample, av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3); @@ -3443,7 +3443,10 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) av_buffersink_get_sample_aspect_ratio(ost->filter->filter); enc_ctx->pix_fmt = av_buffersink_get_format(ost->filter->filter); - if (dec_ctx) + + if (ost->bits_per_raw_sample) + enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample; + else if (dec_ctx && ost->filter->graph->is_meta) enc_ctx->bits_per_raw_sample = FFMIN(dec_ctx->bits_per_raw_sample, av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth); @@ -3459,13 +3462,6 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) ost->st->avg_frame_rate = ost->frame_rate; - if (!dec_ctx || - enc_ctx->width != dec_ctx->width || - enc_ctx->height != dec_ctx->height || - enc_ctx->pix_fmt != dec_ctx->pix_fmt) { - enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample; - } - // Field order: autodetection if (frame) { if (enc_ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) && diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 3d9bb30e72..51ab81b161 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -294,6 +294,9 @@ typedef struct FilterGraph { AVFilterGraph *graph; int reconfiguration; + // true when the filtergraph contains only meta filters + // that do not modify the frame data + int is_meta; InputFilter **inputs; int nb_inputs; diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index c70903295f..ac7a74ba02 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -980,6 +980,30 @@ static void cleanup_filtergraph(FilterGraph *fg) avfilter_graph_free(&fg->graph); } +static int filter_is_buffersrc(const AVFilterContext *f) +{ + return f->nb_inputs == 0 && + (!strcmp(f->filter->name, "buffersrc") || + !strcmp(f->filter->name, "abuffersrc")); +} + +static int graph_is_meta(AVFilterGraph *graph) +{ + for (unsigned i = 0; i < graph->nb_filters; i++) { + const AVFilterContext *f = graph->filters[i]; + + /* in addition to filters flagged as meta, also + * disregard sinks and buffersources (but not other sources, + * since they introduce data we are not aware of) + */ + if (!((f->filter->flags & AVFILTER_FLAG_META) || + f->nb_outputs == 0 || + filter_is_buffersrc(f))) + return 0; + } + return 1; +} + int configure_filtergraph(FilterGraph *fg) { AVFilterInOut *inputs, *outputs, *cur; @@ -1080,6 +1104,8 @@ int configure_filtergraph(FilterGraph *fg) if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0) goto fail; + fg->is_meta = graph_is_meta(fg->graph); + /* limit the lists of allowed formats to the ones selected, to * make sure they stay the same if the filtergraph is reconfigured later */ for (i = 0; i < fg->nb_outputs; i++) {