From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 697E841D28 for ; Thu, 17 Feb 2022 18:03:35 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4AB9268B34B; Thu, 17 Feb 2022 20:03:32 +0200 (EET) Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1339E68B1CC for ; Thu, 17 Feb 2022 20:03:26 +0200 (EET) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id 25C1342FBD; Thu, 17 Feb 2022 19:03:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1645121005; bh=UgrodhoZdDrHTLvKr/Rdw+a9ITKjpIJ9MWwB6w+pPvQ=; h=From:To:Cc:Subject:Date:From; b=FTk7AkVgKKuSwupVK6nxXkqxNzN6hiAKTOC+rVmmIa6MdtaoMvdYvEwbKokxWliVh IQG9oV+wTaWjlzJabOOwqqQAyjEbMRzpGLm9Rw2vvZT/R1LEptFqCqOwH+p7+s36XQ Sbj6Z6MRdGeqD/4dwNPbh7kH4eWbXBqg62y2hOvM= From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Thu, 17 Feb 2022 19:03:19 +0100 Message-Id: <20220217180319.88427-1-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] lavfi/vf_libplacebo: support custom .cube LUTs 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Niklas Haas 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 #include +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".