Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Niklas Haas <ffmpeg@haasn.xyz>
To: ffmpeg-devel@ffmpeg.org
Cc: Niklas Haas <git@haasn.dev>
Subject: [FFmpeg-devel] [PATCH 4/7] fftools/ffmpeg_filter: auto-insert zscale filter
Date: Wed, 11 Oct 2023 16:55:38 +0200
Message-ID: <20231011150848.117897-5-ffmpeg@haasn.xyz> (raw)
In-Reply-To: <20231011150848.117897-1-ffmpeg@haasn.xyz>

From: Niklas Haas <git@haasn.dev>

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 <stdint.h>
 
+#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",
-- 
2.42.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".

  parent reply	other threads:[~2023-10-11 15:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-11 14:55 [FFmpeg-devel] [RFC] [PATCH 0/7] Towards YUVJ removal, v2 Niklas Haas
2023-10-11 14:55 ` [FFmpeg-devel] [PATCH 1/7] avcodec: add extended AVCodec color metadata Niklas Haas
2023-10-11 14:55 ` [FFmpeg-devel] [PATCH 2/7] lavc/options: properly init AVCodecContext.colorspace Niklas Haas
2023-10-11 15:44   ` Andreas Rheinhardt
2023-10-11 14:55 ` [FFmpeg-devel] [PATCH 3/7] fftools/ffmpeg_filter: auto-insert csp conversion filter Niklas Haas
2023-10-11 14:55 ` Niklas Haas [this message]
2023-10-11 16:55   ` [FFmpeg-devel] [PATCH 4/7] fftools/ffmpeg_filter: auto-insert zscale filter Michael Niedermayer
2023-10-11 17:34     ` Niklas Haas
2023-10-11 14:55 ` [FFmpeg-devel] [PATCH 5/7] avcodec/encode: enforce AVCodec capabilities at encode time Niklas Haas
2023-10-11 14:55 ` [FFmpeg-devel] [PATCH 6/7] lavc: set color_ranges for YUVJ-only codecs Niklas Haas
2023-10-11 14:55 ` [FFmpeg-devel] [PATCH 7/7] lavfi/lavu/lavc: remove YUVJ pixel formats Niklas Haas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231011150848.117897-5-ffmpeg@haasn.xyz \
    --to=ffmpeg@haasn.xyz \
    --cc=ffmpeg-devel@ffmpeg.org \
    --cc=git@haasn.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git