Re: [FFmpeg-devel] [PATCH v2] avfilter/vf_libplacebo: update for new tone mapping API

2022-03-31 Thread Niklas Haas
Applied as e301a24fa191ad19574289b765ff1946b23c03f3

On Fri, 25 Mar 2022 16:11:19 +0100 Niklas Haas  wrote:
> From: Niklas Haas 
> 
> Upstream gained a new tone-mapping API, which we never switched to. We
> don't need a version bump for this because it was included as part of
> the v4.192 release we currently already depend on.
> 
> Some of the old options can be moderately approximated with the new API,
> but specifically "desaturation_base" and "max_boost" cannot. Remove
> these entirely, rather than deprecating them. They have actually been
> non-functional for a while as a result of the upstream deprecation.
> 
> Signed-off-by: Niklas Haas 
> ---
> Changes in v2:
> - Avoid use of strings in favor of replicating the enum values
> - Fix two wrong enum option value ranges
> - Simplify the option setting code again slightly
> ---
>  libavfilter/vf_libplacebo.c | 112 
>  1 file changed, 89 insertions(+), 23 deletions(-)
> 
> diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c
> index 31ae28ac38..8ce6462c66 100644
> --- a/libavfilter/vf_libplacebo.c
> +++ b/libavfilter/vf_libplacebo.c
> @@ -26,6 +26,33 @@
>  #include 
>  #include 
>  
> +enum {
> +TONE_MAP_AUTO,
> +TONE_MAP_CLIP,
> +TONE_MAP_BT2390,
> +TONE_MAP_BT2446A,
> +TONE_MAP_SPLINE,
> +TONE_MAP_REINHARD,
> +TONE_MAP_MOBIUS,
> +TONE_MAP_HABLE,
> +TONE_MAP_GAMMA,
> +TONE_MAP_LINEAR,
> +TONE_MAP_COUNT,
> +};
> +
> +static const struct pl_tone_map_function * const 
> tonemapping_funcs[TONE_MAP_COUNT] = {
> +[TONE_MAP_AUTO] = _tone_map_auto,
> +[TONE_MAP_CLIP] = _tone_map_clip,
> +[TONE_MAP_BT2390]   = _tone_map_bt2390,
> +[TONE_MAP_BT2446A]  = _tone_map_bt2446a,
> +[TONE_MAP_SPLINE]   = _tone_map_spline,
> +[TONE_MAP_REINHARD] = _tone_map_reinhard,
> +[TONE_MAP_MOBIUS]   = _tone_map_mobius,
> +[TONE_MAP_HABLE]= _tone_map_hable,
> +[TONE_MAP_GAMMA]= _tone_map_gamma,
> +[TONE_MAP_LINEAR]   = _tone_map_linear,
> +};
> +
>  typedef struct LibplaceboContext {
>  /* lavfi vulkan*/
>  FFVulkanContext vkctx;
> @@ -91,12 +118,16 @@ typedef struct LibplaceboContext {
>  
>  /* pl_color_map_params */
>  int intent;
> +int gamut_mode;
>  int tonemapping;
>  float tonemapping_param;
> +int tonemapping_mode;
> +int inverse_tonemapping;
> +float crosstalk;
> +int tonemapping_lut_size;
> +/* for backwards compatibility */
>  float desat_str;
>  float desat_exp;
> -float desat_base;
> -float max_boost;
>  int gamut_warning;
>  int gamut_clipping;
>  
> @@ -281,6 +312,8 @@ static int process_frames(AVFilterContext *avctx, AVFrame 
> *out, AVFrame *in)
>  int err = 0, ok;
>  LibplaceboContext *s = avctx->priv;
>  struct pl_render_params params;
> +enum pl_tone_map_mode tonemapping_mode = s->tonemapping_mode;
> +enum pl_gamut_mode gamut_mode = s->gamut_mode;
>  struct pl_frame image, target;
>  ok = pl_map_avframe_ex(s->gpu, , pl_avframe_params(
>  .frame= in,
> @@ -305,6 +338,24 @@ static int process_frames(AVFilterContext *avctx, 
> AVFrame *out, AVFrame *in)
>  pl_rect2df_aspect_set(, aspect, s->pad_crop_ratio);
>  }
>  
> +/* backwards compatibility with older API */
> +if (!tonemapping_mode && (s->desat_str >= 0.0f || s->desat_exp >= 0.0f)) 
> {
> +float str = s->desat_str < 0.0f ? 0.9f : s->desat_str;
> +float exp = s->desat_exp < 0.0f ? 0.2f : s->desat_exp;
> +if (str >= 0.9f && exp <= 0.1f) {
> +tonemapping_mode = PL_TONE_MAP_RGB;
> +} else if (str > 0.1f) {
> +tonemapping_mode = PL_TONE_MAP_HYBRID;
> +} else {
> +tonemapping_mode = PL_TONE_MAP_LUMA;
> +}
> +}
> +
> +if (s->gamut_warning)
> +gamut_mode = PL_GAMUT_WARN;
> +if (s->gamut_clipping)
> +gamut_mode = PL_GAMUT_DESATURATE;
> +
>  /* Update render params */
>  params = (struct pl_render_params) {
>  PL_RENDER_DEFAULTS
> @@ -338,14 +389,13 @@ static int process_frames(AVFilterContext *avctx, 
> AVFrame *out, AVFrame *in)
>  
>  .color_map_params = pl_color_map_params(
>  .intent = s->intent,
> -.tone_mapping_algo = s->tonemapping,
> +.gamut_mode = gamut_mode,
> +.tone_mapping_function = tonemapping_funcs[s->tonemapping],
>  .tone_mapping_param = s->tonemapping_param,
> -.desaturation_strength = s->desat_str,
> -.desaturation_exponent = s->desat_exp,
> -.desaturation_base = s->desat_base,
> -.max_boost = s->max_boost,
> -.gamut_warning = s->gamut_warning,
> -.gamut_clipping = s->gamut_clipping,
> +.tone_mapping_mode = tonemapping_mode,
> +.inverse_tone_mapping = s->inverse_tonemapping,
> +.tone_mapping_crosstalk = s->crosstalk,
> + 

[FFmpeg-devel] [PATCH v2] avfilter/vf_libplacebo: update for new tone mapping API

2022-03-25 Thread Niklas Haas
From: Niklas Haas 

Upstream gained a new tone-mapping API, which we never switched to. We
don't need a version bump for this because it was included as part of
the v4.192 release we currently already depend on.

Some of the old options can be moderately approximated with the new API,
but specifically "desaturation_base" and "max_boost" cannot. Remove
these entirely, rather than deprecating them. They have actually been
non-functional for a while as a result of the upstream deprecation.

Signed-off-by: Niklas Haas 
---
Changes in v2:
- Avoid use of strings in favor of replicating the enum values
- Fix two wrong enum option value ranges
- Simplify the option setting code again slightly
---
 libavfilter/vf_libplacebo.c | 112 
 1 file changed, 89 insertions(+), 23 deletions(-)

diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c
index 31ae28ac38..8ce6462c66 100644
--- a/libavfilter/vf_libplacebo.c
+++ b/libavfilter/vf_libplacebo.c
@@ -26,6 +26,33 @@
 #include 
 #include 
 
+enum {
+TONE_MAP_AUTO,
+TONE_MAP_CLIP,
+TONE_MAP_BT2390,
+TONE_MAP_BT2446A,
+TONE_MAP_SPLINE,
+TONE_MAP_REINHARD,
+TONE_MAP_MOBIUS,
+TONE_MAP_HABLE,
+TONE_MAP_GAMMA,
+TONE_MAP_LINEAR,
+TONE_MAP_COUNT,
+};
+
+static const struct pl_tone_map_function * const 
tonemapping_funcs[TONE_MAP_COUNT] = {
+[TONE_MAP_AUTO] = _tone_map_auto,
+[TONE_MAP_CLIP] = _tone_map_clip,
+[TONE_MAP_BT2390]   = _tone_map_bt2390,
+[TONE_MAP_BT2446A]  = _tone_map_bt2446a,
+[TONE_MAP_SPLINE]   = _tone_map_spline,
+[TONE_MAP_REINHARD] = _tone_map_reinhard,
+[TONE_MAP_MOBIUS]   = _tone_map_mobius,
+[TONE_MAP_HABLE]= _tone_map_hable,
+[TONE_MAP_GAMMA]= _tone_map_gamma,
+[TONE_MAP_LINEAR]   = _tone_map_linear,
+};
+
 typedef struct LibplaceboContext {
 /* lavfi vulkan*/
 FFVulkanContext vkctx;
@@ -91,12 +118,16 @@ typedef struct LibplaceboContext {
 
 /* pl_color_map_params */
 int intent;
+int gamut_mode;
 int tonemapping;
 float tonemapping_param;
+int tonemapping_mode;
+int inverse_tonemapping;
+float crosstalk;
+int tonemapping_lut_size;
+/* for backwards compatibility */
 float desat_str;
 float desat_exp;
-float desat_base;
-float max_boost;
 int gamut_warning;
 int gamut_clipping;
 
@@ -281,6 +312,8 @@ static int process_frames(AVFilterContext *avctx, AVFrame 
*out, AVFrame *in)
 int err = 0, ok;
 LibplaceboContext *s = avctx->priv;
 struct pl_render_params params;
+enum pl_tone_map_mode tonemapping_mode = s->tonemapping_mode;
+enum pl_gamut_mode gamut_mode = s->gamut_mode;
 struct pl_frame image, target;
 ok = pl_map_avframe_ex(s->gpu, , pl_avframe_params(
 .frame= in,
@@ -305,6 +338,24 @@ static int process_frames(AVFilterContext *avctx, AVFrame 
*out, AVFrame *in)
 pl_rect2df_aspect_set(, aspect, s->pad_crop_ratio);
 }
 
+/* backwards compatibility with older API */
+if (!tonemapping_mode && (s->desat_str >= 0.0f || s->desat_exp >= 0.0f)) {
+float str = s->desat_str < 0.0f ? 0.9f : s->desat_str;
+float exp = s->desat_exp < 0.0f ? 0.2f : s->desat_exp;
+if (str >= 0.9f && exp <= 0.1f) {
+tonemapping_mode = PL_TONE_MAP_RGB;
+} else if (str > 0.1f) {
+tonemapping_mode = PL_TONE_MAP_HYBRID;
+} else {
+tonemapping_mode = PL_TONE_MAP_LUMA;
+}
+}
+
+if (s->gamut_warning)
+gamut_mode = PL_GAMUT_WARN;
+if (s->gamut_clipping)
+gamut_mode = PL_GAMUT_DESATURATE;
+
 /* Update render params */
 params = (struct pl_render_params) {
 PL_RENDER_DEFAULTS
@@ -338,14 +389,13 @@ static int process_frames(AVFilterContext *avctx, AVFrame 
*out, AVFrame *in)
 
 .color_map_params = pl_color_map_params(
 .intent = s->intent,
-.tone_mapping_algo = s->tonemapping,
+.gamut_mode = gamut_mode,
+.tone_mapping_function = tonemapping_funcs[s->tonemapping],
 .tone_mapping_param = s->tonemapping_param,
-.desaturation_strength = s->desat_str,
-.desaturation_exponent = s->desat_exp,
-.desaturation_base = s->desat_base,
-.max_boost = s->max_boost,
-.gamut_warning = s->gamut_warning,
-.gamut_clipping = s->gamut_clipping,
+.tone_mapping_mode = tonemapping_mode,
+.inverse_tone_mapping = s->inverse_tonemapping,
+.tone_mapping_crosstalk = s->crosstalk,
+.lut_size = s->tonemapping_lut_size,
 ),
 
 .dither_params = s->dithering < 0 ? NULL : pl_dither_params(
@@ -616,21 +666,37 @@ static const AVOption libplacebo_options[] = {
 { "relative", "Relative colorimetric", 0, AV_OPT_TYPE_CONST, {.i64 = 
PL_INTENT_RELATIVE_COLORIMETRIC}, 0, 0, STATIC, "intent" },
 { "absolute", "Absolute