From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id A58334A23C for ; Mon, 25 Mar 2024 15:10:32 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EA22D68D44F; Mon, 25 Mar 2024 17:10:29 +0200 (EET) Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AB1A268D42F for ; Mon, 25 Mar 2024 17:10:23 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1711379423; bh=duBWBdWeopLai8pYr2mCF9CLJvbSqN+gD4HHGBOWPTs=; h=From:To:Cc:Subject:Date:From; b=LY7wHHqd2MmuagzNELTaR7m1wUfwotrai1aOp77R2mykyYEglAh84ml/OB6Zxt7eX ow5pq1UM6yB4GS1BdGOZAzvNcttbVtc609KVoOpTMvBxS3EYLbHP09TxRJUI6pKIlL loU24JllDzMsCIG3ktGiHj4ndF7P9UBDINX/lCnY= Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 7A37941128; Mon, 25 Mar 2024 16:10:23 +0100 (CET) From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Mon, 25 Mar 2024 16:10:20 +0100 Message-ID: <20240325151020.82220-1-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avfilter: properly reduce YUV colorspace format lists 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Niklas Haas Doing this with REDUCE_FORMATS() instead of swap_color_*() is not only shorter, but more importantly comes with the benefit of being done inside a loop, allowing us to correctly propagate complex graphs involving multiple conversion filters (e.g. -vf scale,zscale). The latter family of swapping functions is only used to settle the best *remaining* entry if no exact match was found, and as such was never the correct solution to YUV colorspaces, which only care about exact matches. --- libavfilter/avfiltergraph.c | 84 ++----------------------------------- 1 file changed, 4 insertions(+), 80 deletions(-) diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c index bb5399c55e8..12ff7d6ffb9 100644 --- a/libavfilter/avfiltergraph.c +++ b/libavfilter/avfiltergraph.c @@ -794,6 +794,10 @@ static int reduce_formats_on_filter(AVFilterContext *filter) nb_formats, ff_add_format); REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats, nb_formats, ff_add_format); + REDUCE_FORMATS(int, AVFilterFormats, color_spaces, formats, + nb_formats, ff_add_format); + REDUCE_FORMATS(int, AVFilterFormats, color_ranges, formats, + nb_formats, ff_add_format); /* reduce channel layouts */ for (i = 0; i < filter->nb_inputs; i++) { @@ -906,82 +910,6 @@ static void swap_samplerates(AVFilterGraph *graph) swap_samplerates_on_filter(graph->filters[i]); } -static void swap_color_spaces_on_filter(AVFilterContext *filter) -{ - AVFilterLink *link = NULL; - enum AVColorSpace csp; - int i; - - for (i = 0; i < filter->nb_inputs; i++) { - link = filter->inputs[i]; - if (link->type == AVMEDIA_TYPE_VIDEO && - link->outcfg.color_spaces->nb_formats == 1) - break; - } - if (i == filter->nb_inputs) - return; - - csp = link->outcfg.color_spaces->formats[0]; - - for (i = 0; i < filter->nb_outputs; i++) { - AVFilterLink *outlink = filter->outputs[i]; - if (outlink->type != AVMEDIA_TYPE_VIDEO) - continue; - /* there is no meaningful 'score' between different yuv matrices, - * so just prioritize an exact match if it exists */ - for (int j = 0; j < outlink->incfg.color_spaces->nb_formats; j++) { - if (csp == outlink->incfg.color_spaces->formats[j]) { - FFSWAP(int, outlink->incfg.color_spaces->formats[0], - outlink->incfg.color_spaces->formats[j]); - break; - } - } - } -} - -static void swap_color_spaces(AVFilterGraph *graph) -{ - for (int i = 0; i < graph->nb_filters; i++) - swap_color_spaces_on_filter(graph->filters[i]); -} - -static void swap_color_ranges_on_filter(AVFilterContext *filter) -{ - AVFilterLink *link = NULL; - enum AVColorRange range; - int i; - - for (i = 0; i < filter->nb_inputs; i++) { - link = filter->inputs[i]; - if (link->type == AVMEDIA_TYPE_VIDEO && - link->outcfg.color_ranges->nb_formats == 1) - break; - } - if (i == filter->nb_inputs) - return; - - range = link->outcfg.color_ranges->formats[0]; - - for (i = 0; i < filter->nb_outputs; i++) { - AVFilterLink *outlink = filter->outputs[i]; - if (outlink->type != AVMEDIA_TYPE_VIDEO) - continue; - for (int j = 0; j < outlink->incfg.color_ranges->nb_formats; j++) { - if (range == outlink->incfg.color_ranges->formats[j]) { - FFSWAP(int, outlink->incfg.color_ranges->formats[0], - outlink->incfg.color_ranges->formats[j]); - break; - } - } - } -} - -static void swap_color_ranges(AVFilterGraph *graph) -{ - for (int i = 0; i < graph->nb_filters; i++) - swap_color_ranges_on_filter(graph->filters[i]); -} - #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER) #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT) #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT) @@ -1258,10 +1186,6 @@ static int graph_config_formats(AVFilterGraph *graph, void *log_ctx) if ((ret = reduce_formats(graph)) < 0) return ret; - /* for video filters, ensure that the best colorspace metadata is selected */ - swap_color_spaces(graph); - swap_color_ranges(graph); - /* for audio filters, ensure the best format, sample rate and channel layout * is selected */ swap_sample_fmts(graph); -- 2.44.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".