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] lavfi/vf_libplacebo: support custom .cube LUTs
Date: Thu, 17 Feb 2022 19:03:19 +0100
Message-ID: <20220217180319.88427-1-ffmpeg@haasn.xyz> (raw)

From: Niklas Haas <git@haasn.dev>

Option boilerplate. Very soon we shall definitely need to add some sort
of generic forward-proof option parsing mechanism to libplacebo..
---
 libavfilter/vf_libplacebo.c | 78 +++++++++++++++++++++++++++++++++++++
 1 file changed, 78 insertions(+)

diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c
index 31ae28ac38..0ee2db80ed 100644
--- a/libavfilter/vf_libplacebo.c
+++ b/libavfilter/vf_libplacebo.c
@@ -26,6 +26,14 @@
 #include <libplacebo/utils/libav.h>
 #include <libplacebo/vulkan.h>
 
+struct custom_lut {
+    const struct pl_custom_lut *lut;
+    char *path;
+    void *bin;
+    int bin_len;
+    int type;
+};
+
 typedef struct LibplaceboContext {
     /* lavfi vulkan*/
     FFVulkanContext vkctx;
@@ -115,6 +123,11 @@ typedef struct LibplaceboContext {
     int shader_bin_len;
     const struct pl_hook *hooks[2];
     int num_hooks;
+
+    /* custom LUTs */
+    struct custom_lut lut;
+    struct custom_lut input_lut;
+    struct custom_lut output_lut;
 } LibplaceboContext;
 
 static inline enum pl_log_level get_log_level(void)
@@ -202,6 +215,37 @@ static int libplacebo_init(AVFilterContext *avctx)
     return 0;
 }
 
+static int load_custom_lut(AVFilterContext *avctx, struct custom_lut *lut)
+{
+    LibplaceboContext *s = avctx->priv;
+    uint8_t *buf = NULL;
+    size_t buf_len;
+    int err;
+
+    if (lut->bin_len) {
+        // Binary specified
+        buf = lut->bin;
+        buf_len = lut->bin_len;
+    } else if (lut->path) {
+        // File path specified
+        if ((err = av_file_map(lut->path, &buf, &buf_len, 0, s)))
+            return err;
+    } else {
+        // No LUT specified
+        return 0;
+    }
+
+    lut->lut = pl_lut_parse_cube(s->log, buf, buf_len);
+    if (!lut->lut) {
+        av_log(s, AV_LOG_ERROR, "Failed parsing custom .cube LUT!\n");
+        if (buf && buf != lut->bin)
+            av_file_unmap(buf, buf_len);
+        return AVERROR_EXTERNAL;
+    }
+
+    return 0;
+}
+
 static int init_vulkan(AVFilterContext *avctx)
 {
     int err = 0;
@@ -254,6 +298,10 @@ static int init_vulkan(AVFilterContext *avctx)
         RET(parse_shader(avctx, buf, buf_len));
     }
 
+    RET(load_custom_lut(avctx, &s->lut));
+    RET(load_custom_lut(avctx, &s->input_lut));
+    RET(load_custom_lut(avctx, &s->output_lut));
+
     /* fall through */
 fail:
     if (buf)
@@ -268,6 +316,9 @@ static void libplacebo_uninit(AVFilterContext *avctx)
 
     for (int i = 0; i < s->num_hooks; i++)
         pl_mpv_user_shader_destroy(&s->hooks[i]);
+    pl_lut_free(&s->lut.lut);
+    pl_lut_free(&s->input_lut.lut);
+    pl_lut_free(&s->output_lut.lut);
     pl_renderer_destroy(&s->renderer);
     pl_vulkan_destroy(&s->vulkan);
     pl_log_destroy(&s->log);
@@ -305,6 +356,11 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out, AVFrame *in)
         pl_rect2df_aspect_set(&target.crop, aspect, s->pad_crop_ratio);
     }
 
+    image.lut = s->input_lut.lut;
+    image.lut_type = s->input_lut.type;
+    target.lut = s->output_lut.lut;
+    target.lut_type = s->output_lut.type;
+
     /* Update render params */
     params = (struct pl_render_params) {
         PL_RENDER_DEFAULTS
@@ -361,6 +417,8 @@ static int process_frames(AVFilterContext *avctx, AVFrame *out, AVFrame *in)
 
         .hooks = s->hooks,
         .num_hooks = s->num_hooks,
+        .lut = s->lut.lut,
+        .lut_type = s->lut.type,
 
         .skip_anti_aliasing = s->skip_aa,
         .polar_cutoff = s->polar_cutoff,
@@ -650,6 +708,26 @@ static const AVOption libplacebo_options[] = {
     { "custom_shader_path", "Path to custom user shader (mpv .hook format)", OFFSET(shader_path), AV_OPT_TYPE_STRING, .flags = STATIC },
     { "custom_shader_bin", "Custom user shader as binary (mpv .hook format)", OFFSET(shader_bin), AV_OPT_TYPE_BINARY, .flags = STATIC },
 
+    { "lut_path", "Path to custom color LUT (.cube format)", OFFSET(lut.path), AV_OPT_TYPE_STRING, .flags = STATIC },
+    { "lut_bin", "Custom color LUT as binary (.cube format)", OFFSET(lut.bin), AV_OPT_TYPE_BINARY, .flags = STATIC },
+    { "lut_type", "Color LUT interpretation", OFFSET(lut.type), AV_OPT_TYPE_INT, {.i64 = PL_LUT_UNKNOWN}, 0, PL_LUT_CONVERSION, DYNAMIC, "lut_type" },
+        { "unknown", "Unknown LUT type", 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_UNKNOWN}, 0, 0, STATIC, "lut_type" },
+        { "native", "Non-linear gamma RGB", 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_NATIVE}, 0, 0, STATIC, "lut_type" },
+        { "normalized", "Normalized linear RGB (1.0 = SDR peak)", 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_NORMALIZED}, 0, 0, STATIC, "lut_type" },
+        { "conversion", "LUT replaces tone mapping / gamut mapping", 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_CONVERSION}, 0, 0, STATIC, "lut_type" },
+
+    { "input_lut_path", "Path to custom input LUT (.cube format)", OFFSET(input_lut.path), AV_OPT_TYPE_STRING, .flags = STATIC },
+    { "input_lut_bin", "Custom input LUT as binary (.cube format)", OFFSET(input_lut.bin), AV_OPT_TYPE_BINARY, .flags = STATIC },
+    { "input_lut_type", "Input LUT interpretation", OFFSET(input_lut.type), AV_OPT_TYPE_INT, {.i64 = PL_LUT_UNKNOWN}, 0, PL_LUT_CONVERSION, DYNAMIC, "frame_lut_type" },
+
+    { "output_lut_path", "Path to custom output LUT (.cube format)", OFFSET(output_lut.path), AV_OPT_TYPE_STRING, .flags = STATIC },
+    { "output_lut_bin", "Custom output LUT as binary (.cube format)", OFFSET(output_lut.bin), AV_OPT_TYPE_BINARY, .flags = STATIC },
+    { "output_lut_type", "Output LUT interpretation", OFFSET(output_lut.type), AV_OPT_TYPE_INT, {.i64 = PL_LUT_UNKNOWN}, 0, PL_LUT_CONVERSION, DYNAMIC, "frame_lut_type" },
+        { "unknown", "Unknown LUT type", 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_UNKNOWN}, 0, 0, STATIC, "frame_lut_type" },
+        { "native", "Native image color space (e.g. YUV)", 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_NATIVE}, 0, 0, STATIC, "frame_lut_type" },
+        { "normalized", "Decoded non-linear RGB", 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_NORMALIZED}, 0, 0, STATIC, "frame_lut_type" },
+        { "conversion", "LUT replaces color system conversion", 0, AV_OPT_TYPE_CONST, {.i64 = PL_LUT_CONVERSION}, 0, 0, STATIC, "frame_lut_type" },
+
     /* Performance/quality tradeoff options */
     { "skip_aa", "Skip anti-aliasing", OFFSET(skip_aa), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 0, DYNAMIC },
     { "polar_cutoff", "Polar LUT cutoff", OFFSET(polar_cutoff), AV_OPT_TYPE_FLOAT, {.i64 = 0}, 0.0, 1.0, DYNAMIC },
-- 
2.35.1

_______________________________________________
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".

             reply	other threads:[~2022-02-17 18:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-17 18:03 Niklas Haas [this message]
2022-02-21 11:33 ` Anton Khirnov
2022-02-21 11:36   ` Niklas Haas
2022-02-21 11:38     ` Anton Khirnov
2022-02-21 13:48       ` 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=20220217180319.88427-1-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