diff mbox series

[FFmpeg-devel] lavfi/vf_libplacebo: support custom .cube LUTs

Message ID 20220217180319.88427-1-ffmpeg@haasn.xyz
State New
Headers show
Series [FFmpeg-devel] lavfi/vf_libplacebo: support custom .cube LUTs | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished
yinshiyou/configure_loongarch64 warning Failed to run configure
andriy/make_aarch64_jetson success Make finished
andriy/make_fate_aarch64_jetson success Make fate finished

Commit Message

Niklas Haas Feb. 17, 2022, 6:03 p.m. UTC
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(+)

Comments

Anton Khirnov Feb. 21, 2022, 11:33 a.m. UTC | #1
Quoting Niklas Haas (2022-02-17 19:03:19)
> 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)))

Non-overridable IO in random places is evil.
Niklas Haas Feb. 21, 2022, 11:36 a.m. UTC | #2
On Mon, 21 Feb 2022 12:33:19 +0100 Anton Khirnov <anton@khirnov.net> wrote:
> Non-overridable IO in random places is evil.

Can you recommend a better way of accomplishing this?
Anton Khirnov Feb. 21, 2022, 11:38 a.m. UTC | #3
Quoting Niklas Haas (2022-02-21 12:36:23)
> On Mon, 21 Feb 2022 12:33:19 +0100 Anton Khirnov <anton@khirnov.net> wrote:
> > Non-overridable IO in random places is evil.
> 
> Can you recommend a better way of accomplishing this?

How large are these? Can they be set directly as binary AVOptions?
We could invent syntax for ffmpeg.c to provide the contents of a file as
an option value.
Niklas Haas Feb. 21, 2022, 1:48 p.m. UTC | #4
On Mon, 21 Feb 2022 12:38:54 +0100 Anton Khirnov <anton@khirnov.net> wrote:
> Quoting Niklas Haas (2022-02-21 12:36:23)
> > On Mon, 21 Feb 2022 12:33:19 +0100 Anton Khirnov <anton@khirnov.net> wrote:
> > > Non-overridable IO in random places is evil.
> > 
> > Can you recommend a better way of accomplishing this?
> 
> How large are these? Can they be set directly as binary AVOptions?
> We could invent syntax for ffmpeg.c to provide the contents of a file as
> an option value.

Yes, I think that's a good idea in general. That would also apply
retroactively to the custom_shader_path / custom_shader_bin options.

Typically, these LUTs are around 1MB in size, but I also have LUTs as
large as 100 MB.

Syntax-wise, my proposal would be `option=@filename.cube`, matching e.g.
`curl -F src=@filename`.
diff mbox series

Patch

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 },