From patchwork Fri Oct 13 14:24:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 44243 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4b15:b0:15d:8365:d4b8 with SMTP id fp21csp1039620pzb; Fri, 13 Oct 2023 07:28:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGDrUWBPd/k6Kpax9lOGEKVS2WzYD8LE2vTu++ER7LuumF/vH/UNCleqslMJOMnkXTbA3YP X-Received: by 2002:a17:906:fd89:b0:9b2:b808:6a1c with SMTP id xa9-20020a170906fd8900b009b2b8086a1cmr234028ejb.35.1697207296349; Fri, 13 Oct 2023 07:28:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697207296; cv=none; d=google.com; s=arc-20160816; b=r0mV9lUnxhTqEqxtbns11kwL0prSmDADDdp0YvCr6WaqMNC0h+gTmp/xb4d0haZyWU LPejp+onqU6xYq2qyV4es+TbmNvzGKmoQcFL4KzIr1rDgMz2n3ai8lmv9vb59ZNZWt3L bJOwJNgt+9BqWmoW0SkPR01H00HwGxXMklbg2PuvuTrdhywrfbBNjPuv1sHcfJzfSvGK QZR9TYRBfG6F9Ll4ybrEV3RNTPmvMu6zBJwDxzb3GZ2rvzBS5q9baVNzybAFrIq4wqnI prCDh8skwOhrFapoOpPkjP7AUZ03iRmEV9lgJIZCe1UBg7rR3p4OPph1/+SUtghFdqpD U3lg== 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=GtX3NqH3yRK4TE9OA+vdMk9NeaRt/GtswBumWU4cGL4=; fh=xmAeKtysnShNOmkhiJmYkS30uw4Fu2hvBJ7qlIwukxQ=; b=ERVdsMTQfIqsnKdw1VW5U4KGw0ug4n01l9QgUkNBr9306s96Kbnx7mHVPm9ibD32CB 9bth858IdAF3Zc5nBtGqthBxMLXHReflR5zt8sfRIkwq/Fbe+xhJE4aUgVxuViCvP1WP asRS6j4FoFPE8ljO7mXgWaInejqiyUsKKNUWZ1drGShnZdiVMe+KHpTLmv8Q/owkdToq HjzcMKSXiqyMda72zgTorIe/s0ZLetDh3Bt4ycFx6ZadMjbGxBpUkGN2OVgYfSdhWSe+ D7zk17iC2P9y/pxPxnWi7jbFS+cvedbSuErMwFhhq1VSN37foMi7On5HN6w4chsjOYQJ PlqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=ebIjafCc; 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 y12-20020a17090614cc00b009adc84fc9e0si8430402ejc.289.2023.10.13.07.28.15; Fri, 13 Oct 2023 07:28:16 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=ebIjafCc; 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 61E7368C9ED; Fri, 13 Oct 2023 17:27:23 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 58BC868C9AB for ; Fri, 13 Oct 2023 17:27:15 +0300 (EEST) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 79A074BC23; Fri, 13 Oct 2023 16:27:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1697207231; bh=U0Bxuv9GGbsw8gAa1qYHaHoq5V0FqtEHt9Q/meWS12A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ebIjafCc0exZTHbxROuJXs8EKsll4juw0husoZE5WQtsrbUBmp+Ed8HFo5fnGkOOD qPJ/2BWjfTyLAvmH8EapOVELiBQcs4SMbkFoOHPQnYXxq/qpvQ3+1nsa3nH3SpwA7e imeD1y20gjCNaW5ulPefPlxsB1tfixusG4tkrTCg= From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Fri, 13 Oct 2023 16:24:43 +0200 Message-ID: <20231013142706.23971-6-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231013142706.23971-1-ffmpeg@haasn.xyz> References: <20231013142706.23971-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 05/13] fftools/ffmpeg_filter: auto-insert colorspace filter 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: V0n7/nFaJgku From: Niklas Haas To convert between color space/transfer/primaries, if needed by the codec properties. Libswscale can't handle this at the moment, but fortunately there exists vf_colorspace which we can use unconditionally. --- fftools/ffmpeg_filter.c | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index acdfa72043..41758ac2b5 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -170,6 +170,9 @@ typedef struct OutputFilterPriv { int sample_rate; AVChannelLayout ch_layout; enum AVColorRange range; + enum AVColorSpace csp; + enum AVColorPrimaries prim; + enum AVColorTransferCharacteristic trc; // time base in which the output is sent to our downstream // does not need to match the filtersink's timebase @@ -186,6 +189,9 @@ typedef struct OutputFilterPriv { const AVChannelLayout *ch_layouts; const int *sample_rates; const enum AVColorRange *ranges; + const enum AVColorSpace *csps; + const enum AVColorPrimaries *prims; + const enum AVColorTransferCharacteristic *trcs; AVRational enc_timebase; // offset for output timestamps, in AV_TIME_BASE_Q @@ -373,6 +379,15 @@ DEF_CHOOSE_FORMAT(sample_rates, int, sample_rate, sample_rates, 0, DEF_CHOOSE_FORMAT(out_range, enum AVColorRange, range, ranges, AVCOL_RANGE_UNSPECIFIED, "%s", av_color_range_name); +DEF_CHOOSE_FORMAT(space, enum AVColorSpace, csp, csps, + AVCOL_SPC_UNSPECIFIED, "%s", av_color_space_name); + +DEF_CHOOSE_FORMAT(primaries, enum AVColorPrimaries, prim, prims, + AVCOL_PRI_UNSPECIFIED, "%s", av_color_primaries_name); + +DEF_CHOOSE_FORMAT(trc, enum AVColorTransferCharacteristic, trc, trcs, + AVCOL_TRC_UNSPECIFIED, "%s", av_color_transfer_name); + static void choose_channel_layouts(OutputFilterPriv *ofp, AVBPrint *bprint) { if (av_channel_layout_check(&ofp->ch_layout)) { @@ -599,6 +614,9 @@ static OutputFilter *ofilter_alloc(FilterGraph *fg) ofilter = &ofp->ofilter; ofilter->graph = fg; ofp->format = -1; + ofp->csp = AVCOL_SPC_UNSPECIFIED; + ofp->prim = AVCOL_PRI_UNSPECIFIED; + ofp->trc = AVCOL_TRC_UNSPECIFIED; ofilter->last_pts = AV_NOPTS_VALUE; return ofilter; @@ -686,6 +704,9 @@ int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost) FilterGraphPriv *fgp = fgp_from_fg(fg); const AVCodec *c = ost->enc_ctx->codec; enum AVColorRange color_range; + enum AVColorPrimaries color_primaries; + enum AVColorTransferCharacteristic color_trc; + enum AVColorSpace color_space; av_assert0(!ofilter->ost); @@ -700,6 +721,9 @@ int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost) ofp->width = ost->enc_ctx->width; ofp->height = ost->enc_ctx->height; color_range = ost_opt_int(ost, "color_range", ost->enc_ctx->color_range); + color_primaries = ost_opt_int(ost, "color_primaries", ost->enc_ctx->color_primaries); + color_trc = ost_opt_int(ost, "color_trc", ost->enc_ctx->color_trc); + color_space = ost_opt_int(ost, "colorspace", ost->enc_ctx->colorspace); if (color_range != AVCOL_RANGE_UNSPECIFIED) { ofp->range = color_range; } else { @@ -719,6 +743,21 @@ int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost) ofp->ranges = mjpeg_ranges; } } + if (color_primaries != AVCOL_PRI_UNSPECIFIED) { + ofp->prim = color_primaries; + } else { + ofp->prims = c->primaries; + } + if (color_trc != AVCOL_TRC_UNSPECIFIED) { + ofp->trc = color_trc; + } else { + ofp->trcs = c->trcs; + } + if (color_space != AVCOL_SPC_UNSPECIFIED) { + ofp->csp = color_space; + } else { + ofp->csps = c->csps; + } if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) { ofp->format = ost->enc_ctx->pix_fmt; } else { @@ -1231,6 +1270,32 @@ static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, pad_idx = 0; } + if (!fgp->disable_conversions) { + av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED); + choose_space(ofp, &bprint); + choose_primaries(ofp, &bprint); + choose_trc(ofp, &bprint); + if (!av_bprint_is_complete(&bprint)) + return AVERROR(ENOMEM); + if (bprint.len) { + AVFilterContext *filter; + + snprintf(name, sizeof(name), "csp_out_%d_%d", + ost->file_index, ost->index); + ret = avfilter_graph_create_filter(&filter, + avfilter_get_by_name("colorspace"), + name, bprint.str, NULL, fg->graph); + av_bprint_finalize(&bprint, NULL); + if (ret < 0) + return ret; + if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0) + return ret; + + last_filter = filter; + pad_idx = 0; + } + } + av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED); ret = choose_pix_fmts(ofilter, &bprint, &pix_fmts); if (ret < 0)