* [FFmpeg-devel] [PATCH] lavfi/vf_libplacebo: support custom .cube LUTs
@ 2022-02-17 18:03 Niklas Haas
2022-02-21 11:33 ` Anton Khirnov
0 siblings, 1 reply; 5+ messages in thread
From: Niklas Haas @ 2022-02-17 18:03 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Niklas Haas
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".
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [FFmpeg-devel] [PATCH] lavfi/vf_libplacebo: support custom .cube LUTs
2022-02-17 18:03 [FFmpeg-devel] [PATCH] lavfi/vf_libplacebo: support custom .cube LUTs Niklas Haas
@ 2022-02-21 11:33 ` Anton Khirnov
2022-02-21 11:36 ` Niklas Haas
0 siblings, 1 reply; 5+ messages in thread
From: Anton Khirnov @ 2022-02-21 11:33 UTC (permalink / raw)
To: FFmpeg development discussions and patches; +Cc: Niklas Haas
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.
--
Anton Khirnov
_______________________________________________
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".
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [FFmpeg-devel] [PATCH] lavfi/vf_libplacebo: support custom .cube LUTs
2022-02-21 11:33 ` Anton Khirnov
@ 2022-02-21 11:36 ` Niklas Haas
2022-02-21 11:38 ` Anton Khirnov
0 siblings, 1 reply; 5+ messages in thread
From: Niklas Haas @ 2022-02-21 11:36 UTC (permalink / raw)
To: FFmpeg development discussions and patches
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?
_______________________________________________
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".
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [FFmpeg-devel] [PATCH] lavfi/vf_libplacebo: support custom .cube LUTs
2022-02-21 11:36 ` Niklas Haas
@ 2022-02-21 11:38 ` Anton Khirnov
2022-02-21 13:48 ` Niklas Haas
0 siblings, 1 reply; 5+ messages in thread
From: Anton Khirnov @ 2022-02-21 11:38 UTC (permalink / raw)
To: FFmpeg development discussions and patches
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.
--
Anton Khirnov
_______________________________________________
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".
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [FFmpeg-devel] [PATCH] lavfi/vf_libplacebo: support custom .cube LUTs
2022-02-21 11:38 ` Anton Khirnov
@ 2022-02-21 13:48 ` Niklas Haas
0 siblings, 0 replies; 5+ messages in thread
From: Niklas Haas @ 2022-02-21 13:48 UTC (permalink / raw)
To: FFmpeg development discussions and patches
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`.
_______________________________________________
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".
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2022-02-21 13:48 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-17 18:03 [FFmpeg-devel] [PATCH] lavfi/vf_libplacebo: support custom .cube LUTs Niklas Haas
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
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