Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] vf_colorspace: Add an option to clamp trc LUT output
       [not found] <347845>
@ 2025-08-19 14:17 ` Drew Dunne via ffmpeg-devel
  0 siblings, 0 replies; 5+ messages in thread
From: Drew Dunne via ffmpeg-devel @ 2025-08-19 14:17 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Drew Dunne

Add a new flag to the vf_colorspace filter which provides the user an
option to clamp the linear and delinear transfer characteristics LUT
values to the [0, 1] represented range. This helps constrain the
potential value range when converting between colorspaces.

Sorry about sending this multiple times, my comments outside my commit
message kept being removed from the patch for some reason. I was trying
to respond to the comment on confirming no additional time added.

I ran "time" with a command running just this filter on YUV frames and
confirmed that no significant time was adding with this patch. They both
averaged to the same time to the hundredth second.

Signed-off-by: Drew Dunne <asdunne@google.com>
---
 doc/filters.texi            |  3 +++
 libavfilter/vf_colorspace.c | 14 ++++++++++++--
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 908c98a3cf..cb09d73f62 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -10403,6 +10403,9 @@ von Kries whitepoint adaptation
 identity whitepoint adaptation (i.e. no whitepoint adaptation)
 @end table
 
+@item clamptrc
+Clamps the linear and delinear transfer characteristics LUT values to the [0, 1] represented range.
+
 @item iall
 Override all input properties at once. Same accepted values as @ref{all}.
 
diff --git a/libavfilter/vf_colorspace.c b/libavfilter/vf_colorspace.c
index e1f4725f63..15b4858b24 100644
--- a/libavfilter/vf_colorspace.c
+++ b/libavfilter/vf_colorspace.c
@@ -123,6 +123,7 @@ typedef struct ColorSpaceContext {
     int fast_mode;
     enum DitherMode dither;
     enum WhitepointAdaptation wp_adapt;
+    int clamp_trc;
 
     int16_t *rgb[3];
     ptrdiff_t rgb_stride;
@@ -215,7 +216,9 @@ static int fill_gamma_table(ColorSpaceContext *s)
         } else {
             d = out_alpha * pow(v, out_gamma) - (out_alpha - 1.0);
         }
-        s->delin_lut[n] = av_clip_int16(lrint(d * 28672.0));
+        long d_rounded = lrint(d * 28672.0);
+        s->delin_lut[n] = s->clamp_trc ? av_clip64(d_rounded, 0, 28672)
+                                       : av_clip_int16(d_rounded); 
 
         // linearize
         if (v <= -in_beta * in_delta) {
@@ -225,7 +228,9 @@ static int fill_gamma_table(ColorSpaceContext *s)
         } else {
             l = pow((v + in_alpha - 1.0) * in_ialpha, in_igamma);
         }
-        s->lin_lut[n] = av_clip_int16(lrint(l * 28672.0));
+        long l_rounded = lrint(l * 28672.0);
+        s->lin_lut[n] = s->clamp_trc ? av_clip64(l_rounded, 0, 28672)
+                                     : av_clip_int16(l_rounded); 
     }
 
     return 0;
@@ -1000,6 +1005,11 @@ static const AVOption colorspace_options[] = {
     ENUM("vonkries", WP_ADAPT_VON_KRIES, "wpadapt"),
     ENUM("identity", WP_ADAPT_IDENTITY, "wpadapt"),
 
+    { "clamptrc", 
+      "Clamps the linear and delinear LUT output values to the range [0, 1].",
+      OFFSET(clamp_trc), AV_OPT_TYPE_BOOL,  { .i64 = 0    },
+      0, 1, FLAGS },
+
     { "iall",       "Set all input color properties together",
       OFFSET(user_iall),   AV_OPT_TYPE_INT, { .i64 = CS_UNSPECIFIED },
       CS_UNSPECIFIED, CS_NB - 1, FLAGS, .unit = "all" },
-- 
2.51.0.rc1.167.g924127e9c0-goog

_______________________________________________
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

* [FFmpeg-devel] [PATCH] vf_colorspace: Add an option to clamp trc LUT output
       [not found] <347832>
@ 2025-08-19 14:12 ` Drew Dunne via ffmpeg-devel
  0 siblings, 0 replies; 5+ messages in thread
From: Drew Dunne via ffmpeg-devel @ 2025-08-19 14:12 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Drew Dunne

Add a new flag to the vf_colorspace filter which provides the user an
option to clamp the linear and delinear transfer characteristics LUT
values to the [0, 1] represented range. This helps constrain the
potential value range when converting between colorspaces.

Signed-off-by: Drew Dunne <asdunne@google.com>
---
 doc/filters.texi            |  3 +++
 libavfilter/vf_colorspace.c | 14 ++++++++++++--
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 908c98a3cf..cb09d73f62 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -10403,6 +10403,9 @@ von Kries whitepoint adaptation
 identity whitepoint adaptation (i.e. no whitepoint adaptation)
 @end table
 
+@item clamptrc
+Clamps the linear and delinear transfer characteristics LUT values to the [0, 1] represented range.
+
 @item iall
 Override all input properties at once. Same accepted values as @ref{all}.
 
diff --git a/libavfilter/vf_colorspace.c b/libavfilter/vf_colorspace.c
index e1f4725f63..15b4858b24 100644
--- a/libavfilter/vf_colorspace.c
+++ b/libavfilter/vf_colorspace.c
@@ -123,6 +123,7 @@ typedef struct ColorSpaceContext {
     int fast_mode;
     enum DitherMode dither;
     enum WhitepointAdaptation wp_adapt;
+    int clamp_trc;
 
     int16_t *rgb[3];
     ptrdiff_t rgb_stride;
@@ -215,7 +216,9 @@ static int fill_gamma_table(ColorSpaceContext *s)
         } else {
             d = out_alpha * pow(v, out_gamma) - (out_alpha - 1.0);
         }
-        s->delin_lut[n] = av_clip_int16(lrint(d * 28672.0));
+        long d_rounded = lrint(d * 28672.0);
+        s->delin_lut[n] = s->clamp_trc ? av_clip64(d_rounded, 0, 28672)
+                                       : av_clip_int16(d_rounded); 
 
         // linearize
         if (v <= -in_beta * in_delta) {
@@ -225,7 +228,9 @@ static int fill_gamma_table(ColorSpaceContext *s)
         } else {
             l = pow((v + in_alpha - 1.0) * in_ialpha, in_igamma);
         }
-        s->lin_lut[n] = av_clip_int16(lrint(l * 28672.0));
+        long l_rounded = lrint(l * 28672.0);
+        s->lin_lut[n] = s->clamp_trc ? av_clip64(l_rounded, 0, 28672)
+                                     : av_clip_int16(l_rounded); 
     }
 
     return 0;
@@ -1000,6 +1005,11 @@ static const AVOption colorspace_options[] = {
     ENUM("vonkries", WP_ADAPT_VON_KRIES, "wpadapt"),
     ENUM("identity", WP_ADAPT_IDENTITY, "wpadapt"),
 
+    { "clamptrc", 
+      "Clamps the linear and delinear LUT output values to the range [0, 1].",
+      OFFSET(clamp_trc), AV_OPT_TYPE_BOOL,  { .i64 = 0    },
+      0, 1, FLAGS },
+
     { "iall",       "Set all input color properties together",
       OFFSET(user_iall),   AV_OPT_TYPE_INT, { .i64 = CS_UNSPECIFIED },
       CS_UNSPECIFIED, CS_NB - 1, FLAGS, .unit = "all" },
-- 
2.51.0.rc1.167.g924127e9c0-goog

_______________________________________________
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

* [FFmpeg-devel] [PATCH] vf_colorspace: Add an option to clamp trc LUT output
       [not found] <347789>
@ 2025-08-18 15:55 ` Drew Dunne via ffmpeg-devel
  0 siblings, 0 replies; 5+ messages in thread
From: Drew Dunne via ffmpeg-devel @ 2025-08-18 15:55 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Drew Dunne

Add a new flag to the vf_colorspace filter which provides the user an
option to clamp the linear and delinear transfer characteristics LUT
values to the [0, 1] represented range. This helps constrain the
potential value range when converting between colorspaces.

Signed-off-by: Drew Dunne <asdunne@google.com>
---
 doc/filters.texi            |  3 +++
 libavfilter/vf_colorspace.c | 14 ++++++++++++--
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 908c98a3cf..cb09d73f62 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -10403,6 +10403,9 @@ von Kries whitepoint adaptation
 identity whitepoint adaptation (i.e. no whitepoint adaptation)
 @end table
 
+@item clamptrc
+Clamps the linear and delinear transfer characteristics LUT values to the [0, 1] represented range.
+
 @item iall
 Override all input properties at once. Same accepted values as @ref{all}.
 
diff --git a/libavfilter/vf_colorspace.c b/libavfilter/vf_colorspace.c
index e1f4725f63..15b4858b24 100644
--- a/libavfilter/vf_colorspace.c
+++ b/libavfilter/vf_colorspace.c
@@ -123,6 +123,7 @@ typedef struct ColorSpaceContext {
     int fast_mode;
     enum DitherMode dither;
     enum WhitepointAdaptation wp_adapt;
+    int clamp_trc;
 
     int16_t *rgb[3];
     ptrdiff_t rgb_stride;
@@ -215,7 +216,9 @@ static int fill_gamma_table(ColorSpaceContext *s)
         } else {
             d = out_alpha * pow(v, out_gamma) - (out_alpha - 1.0);
         }
-        s->delin_lut[n] = av_clip_int16(lrint(d * 28672.0));
+        long d_rounded = lrint(d * 28672.0);
+        s->delin_lut[n] = s->clamp_trc ? av_clip64(d_rounded, 0, 28672)
+                                       : av_clip_int16(d_rounded); 
 
         // linearize
         if (v <= -in_beta * in_delta) {
@@ -225,7 +228,9 @@ static int fill_gamma_table(ColorSpaceContext *s)
         } else {
             l = pow((v + in_alpha - 1.0) * in_ialpha, in_igamma);
         }
-        s->lin_lut[n] = av_clip_int16(lrint(l * 28672.0));
+        long l_rounded = lrint(l * 28672.0);
+        s->lin_lut[n] = s->clamp_trc ? av_clip64(l_rounded, 0, 28672)
+                                     : av_clip_int16(l_rounded); 
     }
 
     return 0;
@@ -1000,6 +1005,11 @@ static const AVOption colorspace_options[] = {
     ENUM("vonkries", WP_ADAPT_VON_KRIES, "wpadapt"),
     ENUM("identity", WP_ADAPT_IDENTITY, "wpadapt"),
 
+    { "clamptrc", 
+      "Clamps the linear and delinear LUT output values to the range [0, 1].",
+      OFFSET(clamp_trc), AV_OPT_TYPE_BOOL,  { .i64 = 0    },
+      0, 1, FLAGS },
+
     { "iall",       "Set all input color properties together",
       OFFSET(user_iall),   AV_OPT_TYPE_INT, { .i64 = CS_UNSPECIFIED },
       CS_UNSPECIFIED, CS_NB - 1, FLAGS, .unit = "all" },
-- 
2.51.0.rc1.163.g2494970778-goog

_______________________________________________
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] vf_colorspace: Add an option to clamp trc LUT output
  2025-08-14 15:23 Drew Dunne via ffmpeg-devel
@ 2025-08-14 17:25 ` Nicolas George
  0 siblings, 0 replies; 5+ messages in thread
From: Nicolas George @ 2025-08-14 17:25 UTC (permalink / raw)
  To: FFmpeg development discussions and patches; +Cc: Drew Dunne

Drew Dunne via ffmpeg-devel (HE12025-08-14):
> Add a new flag to the vf_colorspace filter which provides the user an
> option to clamp the linear and delinear transfer characteristics LUT
> values to the [0, 1] represented range. This helps constrain the
> potential value range when converting between colorspaces.

Hi. Thanks for the patch. This filter seems orphaned. I will go at it.

> Signed-off-by: Drew Dunne <asdunne@google.com>
> ---
>  libavfilter/vf_colorspace.c | 14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)

Missing update to doc/filters.texi.


> 
> diff --git a/libavfilter/vf_colorspace.c b/libavfilter/vf_colorspace.c
> index e1f4725f63..48aa267c07 100644
> --- a/libavfilter/vf_colorspace.c
> +++ b/libavfilter/vf_colorspace.c
> @@ -123,6 +123,7 @@ typedef struct ColorSpaceContext {
>      int fast_mode;
>      enum DitherMode dither;
>      enum WhitepointAdaptation wp_adapt;

> +    int clamplutoutput;

Other members in the structure have morereadablenames; please use
underscores.

>  
>      int16_t *rgb[3];
>      ptrdiff_t rgb_stride;
> @@ -215,7 +216,9 @@ static int fill_gamma_table(ColorSpaceContext *s)
>          } else {
>              d = out_alpha * pow(v, out_gamma) - (out_alpha - 1.0);
>          }

> -        s->delin_lut[n] = av_clip_int16(lrint(d * 28672.0));
> +        int d_int = av_clip_int16(lrint(d * 28672.0));
> +        s->delin_lut[n] =
> +            s->clamplutoutput ? FFMIN(FFMAX(0, d_int), 28672) : d_int; 

Please use av_clip() instead of chaining FFMIN() and FFMAX().

Also, avoid chaining av_clip_int16() and clipping to a smaller interval.

By the way, can you confirm it makes no significant difference in time?

>  
>          // linearize
>          if (v <= -in_beta * in_delta) {
> @@ -225,7 +228,9 @@ static int fill_gamma_table(ColorSpaceContext *s)
>          } else {
>              l = pow((v + in_alpha - 1.0) * in_ialpha, in_igamma);
>          }
> -        s->lin_lut[n] = av_clip_int16(lrint(l * 28672.0));
> +        int l_int = av_clip_int16(lrint(l * 28672.0));
> +        s->lin_lut[n] =
> +            s->clamplutoutput ? FFMIN(FFMAX(0, l_int), 28672) : l_int; 

Same as above.

>      }
>  
>      return 0;
> @@ -1000,6 +1005,11 @@ static const AVOption colorspace_options[] = {
>      ENUM("vonkries", WP_ADAPT_VON_KRIES, "wpadapt"),
>      ENUM("identity", WP_ADAPT_IDENTITY, "wpadapt"),
>  
> +    { "clamplutoutput", 
> +      "Clamps the linear and delinear LUT output values to the range [0, 1].",
> +      OFFSET(clamplutoutput), AV_OPT_TYPE_BOOL,  { .i64 = 0    },
> +      0, 1, FLAGS },
> +
>      { "iall",       "Set all input color properties together",
>        OFFSET(user_iall),   AV_OPT_TYPE_INT, { .i64 = CS_UNSPECIFIED },
>        CS_UNSPECIFIED, CS_NB - 1, FLAGS, .unit = "all" },

Regards,

-- 
  Nicolas George
_______________________________________________
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

* [FFmpeg-devel] [PATCH] vf_colorspace: Add an option to clamp trc LUT output
@ 2025-08-14 15:23 Drew Dunne via ffmpeg-devel
  2025-08-14 17:25 ` Nicolas George
  0 siblings, 1 reply; 5+ messages in thread
From: Drew Dunne via ffmpeg-devel @ 2025-08-14 15:23 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Drew Dunne

Add a new flag to the vf_colorspace filter which provides the user an
option to clamp the linear and delinear transfer characteristics LUT
values to the [0, 1] represented range. This helps constrain the
potential value range when converting between colorspaces.

Signed-off-by: Drew Dunne <asdunne@google.com>
---
 libavfilter/vf_colorspace.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/libavfilter/vf_colorspace.c b/libavfilter/vf_colorspace.c
index e1f4725f63..48aa267c07 100644
--- a/libavfilter/vf_colorspace.c
+++ b/libavfilter/vf_colorspace.c
@@ -123,6 +123,7 @@ typedef struct ColorSpaceContext {
     int fast_mode;
     enum DitherMode dither;
     enum WhitepointAdaptation wp_adapt;
+    int clamplutoutput;
 
     int16_t *rgb[3];
     ptrdiff_t rgb_stride;
@@ -215,7 +216,9 @@ static int fill_gamma_table(ColorSpaceContext *s)
         } else {
             d = out_alpha * pow(v, out_gamma) - (out_alpha - 1.0);
         }
-        s->delin_lut[n] = av_clip_int16(lrint(d * 28672.0));
+        int d_int = av_clip_int16(lrint(d * 28672.0));
+        s->delin_lut[n] =
+            s->clamplutoutput ? FFMIN(FFMAX(0, d_int), 28672) : d_int; 
 
         // linearize
         if (v <= -in_beta * in_delta) {
@@ -225,7 +228,9 @@ static int fill_gamma_table(ColorSpaceContext *s)
         } else {
             l = pow((v + in_alpha - 1.0) * in_ialpha, in_igamma);
         }
-        s->lin_lut[n] = av_clip_int16(lrint(l * 28672.0));
+        int l_int = av_clip_int16(lrint(l * 28672.0));
+        s->lin_lut[n] =
+            s->clamplutoutput ? FFMIN(FFMAX(0, l_int), 28672) : l_int; 
     }
 
     return 0;
@@ -1000,6 +1005,11 @@ static const AVOption colorspace_options[] = {
     ENUM("vonkries", WP_ADAPT_VON_KRIES, "wpadapt"),
     ENUM("identity", WP_ADAPT_IDENTITY, "wpadapt"),
 
+    { "clamplutoutput", 
+      "Clamps the linear and delinear LUT output values to the range [0, 1].",
+      OFFSET(clamplutoutput), AV_OPT_TYPE_BOOL,  { .i64 = 0    },
+      0, 1, FLAGS },
+
     { "iall",       "Set all input color properties together",
       OFFSET(user_iall),   AV_OPT_TYPE_INT, { .i64 = CS_UNSPECIFIED },
       CS_UNSPECIFIED, CS_NB - 1, FLAGS, .unit = "all" },
-- 
2.51.0.rc1.167.g924127e9c0-goog

_______________________________________________
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:[~2025-08-19 14:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <347845>
2025-08-19 14:17 ` [FFmpeg-devel] [PATCH] vf_colorspace: Add an option to clamp trc LUT output Drew Dunne via ffmpeg-devel
     [not found] <347832>
2025-08-19 14:12 ` Drew Dunne via ffmpeg-devel
     [not found] <347789>
2025-08-18 15:55 ` Drew Dunne via ffmpeg-devel
2025-08-14 15:23 Drew Dunne via ffmpeg-devel
2025-08-14 17:25 ` Nicolas George

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