From patchwork Wed Oct 11 14:55:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 44222 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4b22:b0:15d:8365:d4b8 with SMTP id fp34csp965475pzb; Wed, 11 Oct 2023 08:09:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJixiBBtFeMigQbIvggi+1IP0DIAJW9rGDIkPAgYNTKZ0JPKWFnRMC51JE3GbfmEQ4tOya X-Received: by 2002:a17:907:8b8c:b0:9a2:28dc:4166 with SMTP id tb12-20020a1709078b8c00b009a228dc4166mr20480415ejc.75.1697036994926; Wed, 11 Oct 2023 08:09:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697036994; cv=none; d=google.com; s=arc-20160816; b=Xw44oBtpTAvZ5SnqMwvVkVAGHgr4/CNqjiF4fMtIuJVia+t1+7amUpLbQv7g0ZSinB GxmYKm/dppoQiAlOyjAe+HkGSsr+EZKPq2kbm9qxME7dpawBimhmp1/ISkOkn+E8P9hg PMHRRfOVU5gd4P6zVkRjywlM/ojj5vLJvsIIxE12IDovDneYndpvVm4sOvdG6aAiVp2r CQ90cEDaCc1cHIWviGOM+l+Phr+ulpTAWmE4tPTPy6nmFL6luY7xE/5jV622mFfvVb3n qNRVG8nnnPjMiDbqtXXPRRvDFyJkIRn4Q2Adr6/MPtdrI/i05eiGsbXq7hRD5NdRZO1D XR0w== 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=Jzh2uSZIxgTmDLSIrw30KTykfHtKfo2b0psEoSJdLiQ=; fh=xmAeKtysnShNOmkhiJmYkS30uw4Fu2hvBJ7qlIwukxQ=; b=cEGYN9qAvCcY211qmjDOwo25+k3g3bjYO7OYPnaqYbqKmFSn6D/hTubck+nFxiQAoC KuhIny5Uty94LCL9lcHa06coPJedXKy1GhMOkpGO17WWoYSpT2KYQgAyr2po00rgU1aW uzht969KKdRpjUqdHO2rumuWbstHYJl6+tOrbkz/gFiA230vaXFCWJ/n2UYt3Pqfk7Pc jL3m/BchOLuiQq6gkwW9cElkPNnH0Mc7+OCajIIxDJKeGUttYxc/TICc6MazWXtof376 jbLP/UD0ZsfrJTEazy0Ril9CVZeLs1Mhn9FCAt5x10+kgGtB6AJc0LQZ0UEcfMyIdhw5 2cmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=fw8XKJDf; 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 mf22-20020a170906cb9600b009adf102f63asi6327798ejb.427.2023.10.11.08.09.46; Wed, 11 Oct 2023 08:09:54 -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=fw8XKJDf; 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 96D7B68CC01; Wed, 11 Oct 2023 18:09:04 +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 8BC3768CBBB for ; Wed, 11 Oct 2023 18:08:54 +0300 (EEST) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 4DF654BC33; Wed, 11 Oct 2023 17:08:54 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1697036934; bh=FvYADoYR/xHO0ApY8ZG3SZl6b9K3yv3jRBL/Z+Lrsf0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fw8XKJDfK7cxd2qvYaNxweaD0dAZy1mfkBWxOj/t8dyPBh6VDA2FgQ8Hfrnbqp8La nECJOl7mgteS8gcDJaf95jxFxn5juvS1CZiNd9x7QW0xmEaA2L2g8gWPAhpffNnQZ9 9jN9UOtYEUkAomO3if4aUZbPElwWjtLs921rUc5U= From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Wed, 11 Oct 2023 16:55:38 +0200 Message-ID: <20231011150848.117897-5-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231011150848.117897-1-ffmpeg@haasn.xyz> References: <20231011150848.117897-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/7] fftools/ffmpeg_filter: auto-insert zscale 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: KO3hgpszywxD From: Niklas Haas Since swscale currently can't handle conversion between different colorimetry sets, supplement the missing bits and pieces using zscale. Subject to change in the future, if libswscale ever gets the capability to convert between colorspaces natively. --- fftools/ffmpeg_filter.c | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index c1bedfda00..c60b9d99f7 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -20,6 +20,8 @@ #include +#include "config_components.h" + #include "ffmpeg.h" #include "libavfilter/avfilter.h" @@ -171,6 +173,9 @@ typedef struct OutputFilterPriv { AVChannelLayout ch_layout; enum AVColorSpace csp; enum AVColorRange color_range; + enum AVChromaLocation chroma_loc; + 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 @@ -188,6 +193,9 @@ typedef struct OutputFilterPriv { const int *sample_rates; const enum AVColorSpace *csps; const enum AVColorRange *color_ranges; + const enum AVChromaLocation *chroma_locs; + const enum AVColorPrimaries *prims; + const enum AVColorTransferCharacteristic *trcs; AVRational enc_timebase; // offset for output timestamps, in AV_TIME_BASE_Q @@ -378,6 +386,17 @@ DEF_CHOOSE_FORMAT(out_color_matrix, enum AVColorSpace, csp, csps, DEF_CHOOSE_FORMAT(out_range, enum AVColorRange, color_range, color_ranges, AVCOL_RANGE_UNSPECIFIED, "%s", av_color_range_name); +#if CONFIG_ZSCALE_FILTER +DEF_CHOOSE_FORMAT(chromal, enum AVChromaLocation, chroma_loc, chroma_locs, + AVCHROMA_LOC_UNSPECIFIED, "%s", av_chroma_location_name); + +DEF_CHOOSE_FORMAT(primaries, enum AVColorPrimaries, prim, prims, + AVCOL_PRI_UNSPECIFIED, "%s", av_color_primaries_name); + +DEF_CHOOSE_FORMAT(transfer, enum AVColorTransferCharacteristic, trc, trcs, + AVCOL_TRC_UNSPECIFIED, "%s", av_color_transfer_name); +#endif + static void choose_channel_layouts(OutputFilterPriv *ofp, AVBPrint *bprint) { if (av_channel_layout_check(&ofp->ch_layout)) { @@ -724,6 +743,21 @@ int ofilter_bind_ost(OutputFilter *ofilter, OutputStream *ost) ofp->color_ranges = mjpeg_ranges; } } + if (ost->enc_ctx->chroma_sample_location) { + ofp->chroma_loc = ost->enc_ctx->chroma_sample_location; + } else { + ofp->chroma_locs = c->chroma_locs; + } + if (ost->enc_ctx->color_primaries) { + ofp->prim = ost->enc_ctx->color_primaries; + } else { + ofp->prims = c->primaries; + } + if (ost->enc_ctx->color_trc) { + ofp->trc = ost->enc_ctx->color_trc; + } else { + ofp->trcs = c->trcs; + } if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) { ofp->format = ost->enc_ctx->pix_fmt; } else { @@ -1280,6 +1314,34 @@ static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, last_filter = filter; pad_idx = 0; } + +#if CONFIG_ZSCALE_FILTER + av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED); + choose_chromal(ofp, &bprint); + choose_primaries(ofp, &bprint); + choose_transfer(ofp, &bprint); + if (bprint.len) { + /* Colorspace format conversion needed */ + AVFilterContext *filter; + + if (!av_bprint_is_complete(&bprint)) + return AVERROR(ENOMEM); + + snprintf(name, sizeof(name), "color_out_%d_%d", + ost->file_index, ost->index); + ret = avfilter_graph_create_filter(&filter, + avfilter_get_by_name("zscale"), + 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; + } +#endif } snprintf(name, sizeof(name), "trim_out_%d_%d",