From patchwork Sun May 21 12:24:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Niklas Haas X-Patchwork-Id: 41755 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:ba91:b0:105:feb:71f2 with SMTP id fb17csp1072712pzb; Sun, 21 May 2023 05:24:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6Nm2CFQ+zr/LwYzVPZ66HNO69KwvJPIAulONi2QEx/RfSBYKb8nWUCkspl1vwsin3mCCV8 X-Received: by 2002:a17:907:9614:b0:96f:65db:d6f0 with SMTP id gb20-20020a170907961400b0096f65dbd6f0mr8014703ejc.17.1684671892661; Sun, 21 May 2023 05:24:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684671892; cv=none; d=google.com; s=arc-20160816; b=s2p3Oc3zqETwYB6AMnuqD3PUDZXd4jMQqHNkkb5Yx9EdEnmTbHKp5WwxZWlFtTckt9 oTQ0D7EPsmbc2wHJ2t5HW3ywy+1J6J5tOfj1130oP5AqVIkZXtdKc0EO0OPJtiNaCw79 P/x4SKvvYdGUuOeX8mfL5BMwmGfzxY8KHsaYRczOQ/P22Zusm3lXkDM5x4YEezlnES7m JuzoFlFwm8NsDU7y67qIPUX20xWB/3+i+gPY+KYLSzRfoiRwdfRHrOG7ii9SHIEtDUYs ngknP6AFOHjhX4K3nEmztmhdrzGYhtC8J1YdG7gluzgi3M5HogriweAvazk4/7F9GFGW iyxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=GcR9dPlvuMWHqDXWhMFMD6H8J/HS69IpUDg8pMCgMvk=; b=FpwBL54APGA2VybaOfgYYKhcS+6ENbdwvwgscBeJEhOmmZ/hmERFrwcZFhUbkmzKzF KqSzb7ViOOc3TFWs/fzLi0h0f3kiZKDt60kpAgscnuxAusS4XEfj74ygR4QiGYJJxlrM MIBpYAv9ak45oIrvXgBfvI671TPJZcostELklpmBtVEtBK4SqPazKJ46mbaMRfoo9FW+ 354JtgeT9ZpVn0soIs9kb/oF654WjAbX1ft62pg9qabs3ckPMxkEJaHkzYTJrvLmMKGP ZqUVjq4ja58aoNh3pOxoxYm9evDb4xqL/TNv873MmyW4np7qgk5M5AUqu0gKO9SAPw05 F0LQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=I+qk5u+N; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z11-20020a1709067e4b00b0096a24c1ec90si2480506ejr.756.2023.05.21.05.24.52; Sun, 21 May 2023 05:24:52 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@haasn.xyz header.s=mail header.b=I+qk5u+N; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5922568C052; Sun, 21 May 2023 15:24:47 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 257CB68BBF7 for ; Sun, 21 May 2023 15:24:41 +0300 (EEST) Received: from haasn.dev (unknown [10.30.0.2]) by haasn.dev (Postfix) with ESMTP id BE31742DED; Sun, 21 May 2023 14:24:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=haasn.xyz; s=mail; t=1684671880; bh=ZDqnKQTqTW+Ltvkf+jF6nuTatZV06kV6x7vXZhS7T2s=; h=From:To:Cc:Subject:Date:From; b=I+qk5u+N6ZhXrXGcu401bmMAeDx7thzpQG34PRrS8Y6sXTqDpmIDUi+HOtVfG0Jnh pXBuQEMfeMCBvtSGvPajTOw6gvRBE2aKrAALcqxOy666Hg+gUHEBqXnuSk+BYJLr8U knixG2+yuDOwLeZmgolXqXmlFgz6OPKhAQ9cYka8= From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Sun, 21 May 2023 14:24:35 +0200 Message-Id: <20230521122438.78375-1-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/4] lavfi/vf_libplacebo: switch to new gamut mapping API 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: PCrBuhm9GDzo From: Niklas Haas Upstream deprecated the old ad-hoc, enum/intent-based gamut mapping API and added a new API based on colorimetrically accurate gamut mapping functions. The relevant change for us is the addition of several new modes, as well as deprecation of the old options. Update the documentation accordingly. --- doc/filters.texi | 47 ++++++++++---------- libavfilter/vf_libplacebo.c | 86 +++++++++++++++++++++++++++++-------- 2 files changed, 92 insertions(+), 41 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index ddc8529bbe9..97023d5f2e8 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -16353,37 +16353,36 @@ gamut-mapping when dealing with mismatches between wide-gamut or HDR content. In general, libplacebo relies on accurate source tagging and mastering display gamut information to produce the best results. @table @option -@item intent -Rendering intent to use when adapting between different primary color gamuts -(after tone-mapping). -@table @samp -@item perceptual -Perceptual gamut mapping. Currently equivalent to relative colorimetric. -@item relative -Relative colorimetric. This is the default. -@item absolute -Absolute colorimetric. -@item saturation -Saturation mapping. Forcibly stretches the source gamut to the target gamut. -@end table - @item gamut_mode How to handle out-of-gamut colors that can occur as a result of colorimetric gamut mapping. @table @samp @item clip -Do nothing, simply clip out-of-range colors to the RGB volume. This is the -default. -@item warn -Highlight out-of-gamut pixels (by coloring them pink). -@item darken -Linearly reduces content brightness to preserves saturated details, followed by -clipping the remaining out-of-gamut colors. As the name implies, this makes -everything darker, but provides a good balance between preserving details and -colors. +Do nothing, simply clip out-of-range colors to the RGB volume. Low quality but +extremely fast. +@item perceptual +Perceptually soft-clip colors to the gamut volume. This is the default. +@item relative +Relative colorimetric hard-clip. Similar to @code{perceptual} but without +the soft knee. +@item saturation +Saturation mapping, maps primaries directly to primaries in RGB space. +Not recommended except for artificial computer graphics for which a bright, +saturated display is desired. +@item absolute +Absolute colorimetric hard-clip. Performs no adjustment of the white point. @item desaturate Hard-desaturates out-of-gamut colors towards white, while preserving the -luminance. Has a tendency to shift colors. +luminance. Has a tendency to distort the visual appearance of bright objects. +@item darken +Linearly reduces content brightness to preserves saturated details, followed by +clipping the remaining out-of-gamut colors. +@item warn +Highlight out-of-gamut pixels (by inverting/marking them). +@item linear +Linearly reduces chromaticity of the entire image to make it fit within the +target color volume. Be careful when using this on BT.2020 sources without +proper mastering metadata, as doing so will lead to excessive desaturation. @end table @item tonemapping diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c index f26d0126beb..09bb3dfac86 100644 --- a/libavfilter/vf_libplacebo.c +++ b/libavfilter/vf_libplacebo.c @@ -56,6 +56,19 @@ enum { TONE_MAP_COUNT, }; +enum { + GAMUT_MAP_CLIP, + GAMUT_MAP_PERCEPTUAL, + GAMUT_MAP_RELATIVE, + GAMUT_MAP_SATURATION, + GAMUT_MAP_ABSOLUTE, + GAMUT_MAP_DESATURATE, + GAMUT_MAP_DARKEN, + GAMUT_MAP_HIGHLIGHT, + GAMUT_MAP_LINEAR, + GAMUT_MAP_COUNT, +}; + static const char *const var_names[] = { "in_w", "iw", ///< width of the input video frame "in_h", "ih", ///< height of the input video frame @@ -186,7 +199,6 @@ typedef struct LibplaceboContext { /* pl_color_map_params */ struct pl_color_map_params color_map_params; - int intent; int gamut_mode; int tonemapping; float tonemapping_param; @@ -202,6 +214,7 @@ typedef struct LibplaceboContext { int gamut_warning; int gamut_clipping; int force_icc_lut; + int intent; #endif /* pl_dither_params */ @@ -272,6 +285,34 @@ static const struct pl_tone_map_function *pl_get_tonemapping_func(int tm) { } } +static void set_gamut_mode(struct pl_color_map_params *p, int gamut_mode) +{ + switch (gamut_mode) { +#if PL_API_VER >= 269 + case GAMUT_MAP_CLIP: p->gamut_mapping = &pl_gamut_map_clip; return; + case GAMUT_MAP_PERCEPTUAL: p->gamut_mapping = &pl_gamut_map_perceptual; return; + case GAMUT_MAP_RELATIVE: p->gamut_mapping = &pl_gamut_map_relative; return; + case GAMUT_MAP_SATURATION: p->gamut_mapping = &pl_gamut_map_saturation; return; + case GAMUT_MAP_ABSOLUTE: p->gamut_mapping = &pl_gamut_map_absolute; return; + case GAMUT_MAP_DESATURATE: p->gamut_mapping = &pl_gamut_map_desaturate; return; + case GAMUT_MAP_DARKEN: p->gamut_mapping = &pl_gamut_map_darken; return; + case GAMUT_MAP_HIGHLIGHT: p->gamut_mapping = &pl_gamut_map_highlight; return; + case GAMUT_MAP_LINEAR: p->gamut_mapping = &pl_gamut_map_linear; return; +#else + case GAMUT_MAP_RELATIVE: p->intent = PL_INTENT_RELATIVE_COLORIMETRIC; return; + case GAMUT_MAP_SATURATION: p->intent = PL_INTENT_SATURATION; return; + case GAMUT_MAP_ABSOLUTE: p->intent = PL_INTENT_ABSOLUTE_COLORIMETRIC; return; + case GAMUT_MAP_DESATURATE: p->gamut_mode = PL_GAMUT_DESATURATE; return; + case GAMUT_MAP_DARKEN: p->gamut_mode = PL_GAMUT_DARKEN; return; + case GAMUT_MAP_HIGHLIGHT: p->gamut_mode = PL_GAMUT_WARN; return; + /* Use defaults for all other cases */ + default: return; +#endif + } + + av_assert0(0); +}; + static int parse_shader(AVFilterContext *avctx, const void *shader, size_t len) { LibplaceboContext *s = avctx->priv; @@ -317,7 +358,7 @@ static int update_settings(AVFilterContext *ctx) int err = 0; LibplaceboContext *s = ctx->priv; enum pl_tone_map_mode tonemapping_mode = s->tonemapping_mode; - enum pl_gamut_mode gamut_mode = s->gamut_mode; + int gamut_mode = s->gamut_mode; uint8_t color_rgba[4]; RET(av_parse_color(color_rgba, s->fillcolor, -1, s)); @@ -336,10 +377,16 @@ static int update_settings(AVFilterContext *ctx) } } + switch (s->intent) { + case PL_INTENT_SATURATION: gamut_mode = GAMUT_MAP_SATURATION; break; + case PL_INTENT_RELATIVE_COLORIMETRIC: gamut_mode = GAMUT_MAP_RELATIVE; break; + case PL_INTENT_ABSOLUTE_COLORIMETRIC: gamut_mode = GAMUT_MAP_ABSOLUTE; break; + } + if (s->gamut_warning) - gamut_mode = PL_GAMUT_WARN; + gamut_mode = GAMUT_MAP_HIGHLIGHT; if (s->gamut_clipping) - gamut_mode = PL_GAMUT_DESATURATE; + gamut_mode = GAMUT_MAP_DESATURATE; #endif s->deband_params = *pl_deband_params( @@ -366,8 +413,6 @@ static int update_settings(AVFilterContext *ctx) ); s->color_map_params = *pl_color_map_params( - .intent = s->intent, - .gamut_mode = gamut_mode, .tone_mapping_function = pl_get_tonemapping_func(s->tonemapping), .tone_mapping_param = s->tonemapping_param, .tone_mapping_mode = tonemapping_mode, @@ -376,6 +421,8 @@ static int update_settings(AVFilterContext *ctx) .lut_size = s->tonemapping_lut_size, ); + set_gamut_mode(&s->color_map_params, gamut_mode); + s->dither_params = *pl_dither_params( .method = s->dithering, .lut_size = s->dither_lut_size, @@ -1143,16 +1190,16 @@ static const AVOption libplacebo_options[] = { { "scene_threshold_high", "Scene change high threshold", OFFSET(scene_high), AV_OPT_TYPE_FLOAT, {.dbl = 10.0}, -1.0, 100.0, DYNAMIC }, { "overshoot", "Tone-mapping overshoot margin", OFFSET(overshoot), AV_OPT_TYPE_FLOAT, {.dbl = 0.05}, 0.0, 1.0, DYNAMIC }, - { "intent", "Rendering intent", OFFSET(intent), AV_OPT_TYPE_INT, {.i64 = PL_INTENT_RELATIVE_COLORIMETRIC}, 0, 3, DYNAMIC, "intent" }, - { "perceptual", "Perceptual", 0, AV_OPT_TYPE_CONST, {.i64 = PL_INTENT_PERCEPTUAL}, 0, 0, STATIC, "intent" }, - { "relative", "Relative colorimetric", 0, AV_OPT_TYPE_CONST, {.i64 = PL_INTENT_RELATIVE_COLORIMETRIC}, 0, 0, STATIC, "intent" }, - { "absolute", "Absolute colorimetric", 0, AV_OPT_TYPE_CONST, {.i64 = PL_INTENT_ABSOLUTE_COLORIMETRIC}, 0, 0, STATIC, "intent" }, - { "saturation", "Saturation mapping", 0, AV_OPT_TYPE_CONST, {.i64 = PL_INTENT_SATURATION}, 0, 0, STATIC, "intent" }, - { "gamut_mode", "Gamut-mapping mode", OFFSET(gamut_mode), AV_OPT_TYPE_INT, {.i64 = PL_GAMUT_CLIP}, 0, PL_GAMUT_MODE_COUNT - 1, DYNAMIC, "gamut_mode" }, - { "clip", "Hard-clip gamut boundary", 0, AV_OPT_TYPE_CONST, {.i64 = PL_GAMUT_CLIP}, 0, 0, STATIC, "gamut_mode" }, - { "warn", "Highlight out-of-gamut colors", 0, AV_OPT_TYPE_CONST, {.i64 = PL_GAMUT_WARN}, 0, 0, STATIC, "gamut_mode" }, - { "darken", "Darken image to fit gamut", 0, AV_OPT_TYPE_CONST, {.i64 = PL_GAMUT_DARKEN}, 0, 0, STATIC, "gamut_mode" }, - { "desaturate", "Colorimetrically desaturate colors", 0, AV_OPT_TYPE_CONST, {.i64 = PL_GAMUT_DESATURATE}, 0, 0, STATIC, "gamut_mode" }, + { "gamut_mode", "Gamut-mapping mode", OFFSET(gamut_mode), AV_OPT_TYPE_INT, {.i64 = GAMUT_MAP_PERCEPTUAL}, 0, GAMUT_MAP_COUNT - 1, DYNAMIC, "gamut_mode" }, + { "clip", "Hard-clip (RGB per-channel)", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_CLIP}, 0, 0, STATIC, "gamut_mode" }, + { "perceptual", "Colorimetric soft clipping", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_PERCEPTUAL}, 0, 0, STATIC, "gamut_mode" }, + { "relative", "Relative colorimetric clipping", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_RELATIVE}, 0, 0, STATIC, "gamut_mode" }, + { "saturation", "Saturation mapping (RGB -> RGB)", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_SATURATION}, 0, 0, STATIC, "gamut_mode" }, + { "absolute", "Absolute colorimetric clipping", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_ABSOLUTE}, 0, 0, STATIC, "gamut_mode" }, + { "desaturate", "Colorimetrically desaturate colors towards white", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_DESATURATE}, 0, 0, STATIC, "gamut_mode" }, + { "darken", "Colorimetric clip with bias towards darkening image to fit gamut", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_DARKEN}, 0, 0, STATIC, "gamut_mode" }, + { "warn", "Highlight out-of-gamut colors", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_HIGHLIGHT}, 0, 0, STATIC, "gamut_mode" }, + { "linear", "Linearly reduce chromaticity to fit gamut", 0, AV_OPT_TYPE_CONST, {.i64 = GAMUT_MAP_LINEAR}, 0, 0, STATIC, "gamut_mode" }, { "tonemapping", "Tone-mapping algorithm", OFFSET(tonemapping), AV_OPT_TYPE_INT, {.i64 = TONE_MAP_AUTO}, 0, TONE_MAP_COUNT - 1, DYNAMIC, "tonemap" }, { "auto", "Automatic selection", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_AUTO}, 0, 0, STATIC, "tonemap" }, { "clip", "No tone mapping (clip", 0, AV_OPT_TYPE_CONST, {.i64 = TONE_MAP_CLIP}, 0, 0, STATIC, "tonemap" }, @@ -1184,7 +1231,12 @@ static const AVOption libplacebo_options[] = { { "desaturation_strength", "Desaturation strength", OFFSET(desat_str), AV_OPT_TYPE_FLOAT, {.dbl = -1.0}, -1.0, 1.0, DYNAMIC | AV_OPT_FLAG_DEPRECATED }, { "desaturation_exponent", "Desaturation exponent", OFFSET(desat_exp), AV_OPT_TYPE_FLOAT, {.dbl = -1.0}, -1.0, 10.0, DYNAMIC | AV_OPT_FLAG_DEPRECATED }, { "gamut_warning", "Highlight out-of-gamut colors", OFFSET(gamut_warning), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DYNAMIC | AV_OPT_FLAG_DEPRECATED }, - { "gamut_clipping", "Enable colorimetric gamut clipping", OFFSET(gamut_clipping), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DYNAMIC | AV_OPT_FLAG_DEPRECATED }, + { "gamut_clipping", "Enable desaturating colorimetric gamut clipping", OFFSET(gamut_clipping), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DYNAMIC | AV_OPT_FLAG_DEPRECATED }, + { "intent", "Rendering intent", OFFSET(intent), AV_OPT_TYPE_INT, {.i64 = PL_INTENT_PERCEPTUAL}, 0, 3, DYNAMIC | AV_OPT_FLAG_DEPRECATED, "intent" }, + { "perceptual", "Perceptual", 0, AV_OPT_TYPE_CONST, {.i64 = PL_INTENT_PERCEPTUAL}, 0, 0, STATIC, "intent" }, + { "relative", "Relative colorimetric", 0, AV_OPT_TYPE_CONST, {.i64 = PL_INTENT_RELATIVE_COLORIMETRIC}, 0, 0, STATIC, "intent" }, + { "absolute", "Absolute colorimetric", 0, AV_OPT_TYPE_CONST, {.i64 = PL_INTENT_ABSOLUTE_COLORIMETRIC}, 0, 0, STATIC, "intent" }, + { "saturation", "Saturation mapping", 0, AV_OPT_TYPE_CONST, {.i64 = PL_INTENT_SATURATION}, 0, 0, STATIC, "intent" }, #endif { "dithering", "Dither method to use", OFFSET(dithering), AV_OPT_TYPE_INT, {.i64 = PL_DITHER_BLUE_NOISE}, -1, PL_DITHER_METHOD_COUNT - 1, DYNAMIC, "dither" },