* [FFmpeg-devel] [PATCH v1 1/1] avfilter/vf_tonemap: add herimte tone mapping
@ 2022-09-08 13:22 mirsfang
0 siblings, 0 replies; 3+ messages in thread
From: mirsfang @ 2022-09-08 13:22 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: mirs
From: mirs <fangpengcheng@bilibili.com>
Signed-off-by: mirs <fangpengcheng@bilibili.com>
add tonemapping hermite,this looks close real hdr display
format patch email
---
libavfilter/vf_tonemap.c | 65 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 65 insertions(+)
diff --git a/libavfilter/vf_tonemap.c b/libavfilter/vf_tonemap.c
index d1087e6bd9..3fb2bdfa2c 100644
--- a/libavfilter/vf_tonemap.c
+++ b/libavfilter/vf_tonemap.c
@@ -48,6 +48,7 @@ enum TonemapAlgorithm {
TONEMAP_REINHARD,
TONEMAP_HABLE,
TONEMAP_MOBIUS,
+ TONEMAP_HERMITE,
TONEMAP_MAX,
};
@@ -106,6 +107,66 @@ static float mobius(float in, float j, double peak)
return (b * b + 2.0f * b * j + j * j) / (b - a) * (in + a) / (in + b);
}
+static float hermite(float in) {
+ /*
+ * in theory, max mastering lumi 、max content lumi frome packet side data,
+ * display max lumi from Display system value,current value is R-REP-BT.2390 max value
+ */
+ float max_mastering_lumi = 1000.0f;
+ float max_content_lumi = 1000.0f;
+ float display_max_lumi = 400.0f; // Terminal domain 400NITS is the maximum lumen of a normal screen
+
+ float max_in_lumi = FFMAX(max_mastering_lumi,max_content_lumi);
+ float max_out_lumi = display_max_lumi;
+ float nits = in * (display_max_lumi / max_mastering_lumi);
+
+ // clamp
+ if (nits < 0) {
+ nits = 0.0;
+ } else if (nits > max_in_lumi) {
+ nits = max_in_lumi;
+ }
+
+ if (max_in_lumi <= max_out_lumi) {
+ nits *= max_out_lumi / max_in_lumi;
+ } else {
+ // three control points
+ const float x0 = 10.0f;
+ const float y0 = 17.0;
+ float x1 = max_out_lumi * 0.75;
+ float y1 = x1;
+ float x2 = x1 + (max_in_lumi - x1) / 2.0;
+ float y2 = y1 + (max_out_lumi - y1) * 0.75;
+ // horizontal distances between the last three control points
+ float h12 = x2 - x1;
+ float h23 = max_in_lumi - x2;
+ // tangents at the last three control points
+ float m1 = (y2 - y1) / h12;
+ float m3 = (max_out_lumi - y2) / h23;
+ float m2 = (m1 + m3) / 2.0;
+
+ if (nits < x0) {
+ // scale [0.0, x0] to [0.0, y0] linearly
+ float slope = y0 / x0;
+ nits *= slope;
+ } else if (nits < x1) {
+ // scale [x0, x1] to [y0, y1] linearly
+ float slope = (y1 - y0) / (x1 - x0);
+ nits = y0 + (nits - x0) * slope;
+ } else if (nits < x2) {
+ // scale [x1, x2] to [y1, y2] using Hermite interp
+ float t = (nits - x1) / h12;
+ nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +(y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
+ } else {
+ // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
+ float t = (nits - x2) / h23;
+ nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +(max_out_lumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
+ }
+ }
+
+ return nits;
+}
+
#define MIX(x,y,a) (x) * (1 - (a)) + (y) * (a)
static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in,
const AVPixFmtDescriptor *desc, int x, int y, double peak)
@@ -163,6 +224,9 @@ static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in,
case TONEMAP_MOBIUS:
sig = mobius(sig, s->param, peak);
break;
+ case TONEMAP_HERMITE:
+ sig = hermite(sig);
+ break;
}
/* apply the computed scale factor to the color,
@@ -291,6 +355,7 @@ static const AVOption tonemap_options[] = {
{ "reinhard", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_REINHARD}, 0, 0, FLAGS, "tonemap" },
{ "hable", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_HABLE}, 0, 0, FLAGS, "tonemap" },
{ "mobius", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_MOBIUS}, 0, 0, FLAGS, "tonemap" },
+ { "hermite", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_HERMITE}, 0, 0, FLAGS, "tonemap" },
{ "param", "tonemap parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl = NAN}, DBL_MIN, DBL_MAX, FLAGS },
{ "desat", "desaturation strength", OFFSET(desat), AV_OPT_TYPE_DOUBLE, {.dbl = 2}, 0, DBL_MAX, FLAGS },
{ "peak", "signal peak override", OFFSET(peak), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, DBL_MAX, FLAGS },
--
2.32.1 (Apple Git-133)
_______________________________________________
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] 3+ messages in thread
* [FFmpeg-devel] [PATCH v1 1/1] avfilter/vf_tonemap: add herimte tone mapping
@ 2022-09-08 12:41 mirsfang
2022-09-08 13:04 ` mirs fang
0 siblings, 1 reply; 3+ messages in thread
From: mirsfang @ 2022-09-08 12:41 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: mirs
From: mirs <fangpengcheng@bilibili.com>
Signed-off-by: mirs <fangpengcheng@bilibili.com>
add tonemapping hermite,this looks close real hdr display
---
libavfilter/vf_tonemap.c | 65 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 65 insertions(+)
diff --git a/libavfilter/vf_tonemap.c b/libavfilter/vf_tonemap.c
index d1087e6bd9..3fb2bdfa2c 100644
--- a/libavfilter/vf_tonemap.c
+++ b/libavfilter/vf_tonemap.c
@@ -48,6 +48,7 @@ enum TonemapAlgorithm {
TONEMAP_REINHARD,
TONEMAP_HABLE,
TONEMAP_MOBIUS,
+ TONEMAP_HERMITE,
TONEMAP_MAX,
};
@@ -106,6 +107,66 @@ static float mobius(float in, float j, double peak)
return (b * b + 2.0f * b * j + j * j) / (b - a) * (in + a) / (in + b);
}
+static float hermite(float in) {
+ /*
+ * in theory, max mastering lumi 、max content lumi frome packet side data,
+ * display max lumi from Display system value,current value is R-REP-BT.2390 max value
+ */
+ float max_mastering_lumi = 1000.0f;
+ float max_content_lumi = 1000.0f;
+ float display_max_lumi = 400.0f; // Terminal domain 400NITS is the maximum lumen of a normal screen
+
+ float max_in_lumi = FFMAX(max_mastering_lumi,max_content_lumi);
+ float max_out_lumi = display_max_lumi;
+ float nits = in * (display_max_lumi / max_mastering_lumi);
+
+ // clamp
+ if (nits < 0) {
+ nits = 0.0;
+ } else if (nits > max_in_lumi) {
+ nits = max_in_lumi;
+ }
+
+ if (max_in_lumi <= max_out_lumi) {
+ nits *= max_out_lumi / max_in_lumi;
+ } else {
+ // three control points
+ const float x0 = 10.0f;
+ const float y0 = 17.0;
+ float x1 = max_out_lumi * 0.75;
+ float y1 = x1;
+ float x2 = x1 + (max_in_lumi - x1) / 2.0;
+ float y2 = y1 + (max_out_lumi - y1) * 0.75;
+ // horizontal distances between the last three control points
+ float h12 = x2 - x1;
+ float h23 = max_in_lumi - x2;
+ // tangents at the last three control points
+ float m1 = (y2 - y1) / h12;
+ float m3 = (max_out_lumi - y2) / h23;
+ float m2 = (m1 + m3) / 2.0;
+
+ if (nits < x0) {
+ // scale [0.0, x0] to [0.0, y0] linearly
+ float slope = y0 / x0;
+ nits *= slope;
+ } else if (nits < x1) {
+ // scale [x0, x1] to [y0, y1] linearly
+ float slope = (y1 - y0) / (x1 - x0);
+ nits = y0 + (nits - x0) * slope;
+ } else if (nits < x2) {
+ // scale [x1, x2] to [y1, y2] using Hermite interp
+ float t = (nits - x1) / h12;
+ nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) * (1.0 - t) +(y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
+ } else {
+ // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite interp
+ float t = (nits - x2) / h23;
+ nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) * (1.0 - t) +(max_out_lumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
+ }
+ }
+
+ return nits;
+}
+
#define MIX(x,y,a) (x) * (1 - (a)) + (y) * (a)
static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in,
const AVPixFmtDescriptor *desc, int x, int y, double peak)
@@ -163,6 +224,9 @@ static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in,
case TONEMAP_MOBIUS:
sig = mobius(sig, s->param, peak);
break;
+ case TONEMAP_HERMITE:
+ sig = hermite(sig);
+ break;
}
/* apply the computed scale factor to the color,
@@ -291,6 +355,7 @@ static const AVOption tonemap_options[] = {
{ "reinhard", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_REINHARD}, 0, 0, FLAGS, "tonemap" },
{ "hable", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_HABLE}, 0, 0, FLAGS, "tonemap" },
{ "mobius", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_MOBIUS}, 0, 0, FLAGS, "tonemap" },
+ { "hermite", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_HERMITE}, 0, 0, FLAGS, "tonemap" },
{ "param", "tonemap parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl = NAN}, DBL_MIN, DBL_MAX, FLAGS },
{ "desat", "desaturation strength", OFFSET(desat), AV_OPT_TYPE_DOUBLE, {.dbl = 2}, 0, DBL_MAX, FLAGS },
{ "peak", "signal peak override", OFFSET(peak), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, DBL_MAX, FLAGS },
--
2.32.1 (Apple Git-133)
_______________________________________________
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] 3+ messages in thread
* Re: [FFmpeg-devel] [PATCH v1 1/1] avfilter/vf_tonemap: add herimte tone mapping
2022-09-08 12:41 mirsfang
@ 2022-09-08 13:04 ` mirs fang
0 siblings, 0 replies; 3+ messages in thread
From: mirs fang @ 2022-09-08 13:04 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: mirs
[-- Attachment #1.1: Type: text/plain, Size: 4976 bytes --]
hermite hava more light and more details for others
[image: 41662642193_.pic.jpg]
<mirsfang@gmail.com> 于2022年9月8日周四 20:42写道:
> From: mirs <fangpengcheng@bilibili.com>
>
> Signed-off-by: mirs <fangpengcheng@bilibili.com>
>
> add tonemapping hermite,this looks close real hdr display
>
> ---
> libavfilter/vf_tonemap.c | 65 ++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 65 insertions(+)
>
> diff --git a/libavfilter/vf_tonemap.c b/libavfilter/vf_tonemap.c
> index d1087e6bd9..3fb2bdfa2c 100644
> --- a/libavfilter/vf_tonemap.c
> +++ b/libavfilter/vf_tonemap.c
> @@ -48,6 +48,7 @@ enum TonemapAlgorithm {
> TONEMAP_REINHARD,
> TONEMAP_HABLE,
> TONEMAP_MOBIUS,
> + TONEMAP_HERMITE,
> TONEMAP_MAX,
> };
>
> @@ -106,6 +107,66 @@ static float mobius(float in, float j, double peak)
> return (b * b + 2.0f * b * j + j * j) / (b - a) * (in + a) / (in + b);
> }
>
> +static float hermite(float in) {
> + /*
> + * in theory, max mastering lumi 、max content lumi frome packet side
> data,
> + * display max lumi from Display system value,current value is
> R-REP-BT.2390 max value
> + */
> + float max_mastering_lumi = 1000.0f;
> + float max_content_lumi = 1000.0f;
> + float display_max_lumi = 400.0f; // Terminal domain 400NITS is the
> maximum lumen of a normal screen
> +
> + float max_in_lumi = FFMAX(max_mastering_lumi,max_content_lumi);
> + float max_out_lumi = display_max_lumi;
> + float nits = in * (display_max_lumi / max_mastering_lumi);
> +
> + // clamp
> + if (nits < 0) {
> + nits = 0.0;
> + } else if (nits > max_in_lumi) {
> + nits = max_in_lumi;
> + }
> +
> + if (max_in_lumi <= max_out_lumi) {
> + nits *= max_out_lumi / max_in_lumi;
> + } else {
> + // three control points
> + const float x0 = 10.0f;
> + const float y0 = 17.0;
> + float x1 = max_out_lumi * 0.75;
> + float y1 = x1;
> + float x2 = x1 + (max_in_lumi - x1) / 2.0;
> + float y2 = y1 + (max_out_lumi - y1) * 0.75;
> + // horizontal distances between the last three control points
> + float h12 = x2 - x1;
> + float h23 = max_in_lumi - x2;
> + // tangents at the last three control points
> + float m1 = (y2 - y1) / h12;
> + float m3 = (max_out_lumi - y2) / h23;
> + float m2 = (m1 + m3) / 2.0;
> +
> + if (nits < x0) {
> + // scale [0.0, x0] to [0.0, y0] linearly
> + float slope = y0 / x0;
> + nits *= slope;
> + } else if (nits < x1) {
> + // scale [x0, x1] to [y0, y1] linearly
> + float slope = (y1 - y0) / (x1 - x0);
> + nits = y0 + (nits - x0) * slope;
> + } else if (nits < x2) {
> + // scale [x1, x2] to [y1, y2] using Hermite interp
> + float t = (nits - x1) / h12;
> + nits = (y1 * (1.0 + 2.0 * t) + h12 * m1 * t) * (1.0 - t) *
> (1.0 - t) +(y2 * (3.0 - 2.0 * t) + h12 * m2 * (t - 1.0)) * t * t;
> + } else {
> + // scale [x2, maxInLumi] to [y2, maxOutLumi] using Hermite
> interp
> + float t = (nits - x2) / h23;
> + nits = (y2 * (1.0 + 2.0 * t) + h23 * m2 * t) * (1.0 - t) *
> (1.0 - t) +(max_out_lumi * (3.0 - 2.0 * t) + h23 * m3 * (t - 1.0)) * t * t;
> + }
> + }
> +
> + return nits;
> +}
> +
> #define MIX(x,y,a) (x) * (1 - (a)) + (y) * (a)
> static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in,
> const AVPixFmtDescriptor *desc, int x, int y, double
> peak)
> @@ -163,6 +224,9 @@ static void tonemap(TonemapContext *s, AVFrame *out,
> const AVFrame *in,
> case TONEMAP_MOBIUS:
> sig = mobius(sig, s->param, peak);
> break;
> + case TONEMAP_HERMITE:
> + sig = hermite(sig);
> + break;
> }
>
> /* apply the computed scale factor to the color,
> @@ -291,6 +355,7 @@ static const AVOption tonemap_options[] = {
> { "reinhard", 0, 0, AV_OPT_TYPE_CONST, {.i64 =
> TONEMAP_REINHARD}, 0, 0, FLAGS, "tonemap" },
> { "hable", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_HABLE},
> 0, 0, FLAGS, "tonemap" },
> { "mobius", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_MOBIUS},
> 0, 0, FLAGS, "tonemap" },
> + { "hermite", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_HERMITE},
> 0, 0, FLAGS, "tonemap" },
> { "param", "tonemap parameter", OFFSET(param),
> AV_OPT_TYPE_DOUBLE, {.dbl = NAN}, DBL_MIN, DBL_MAX, FLAGS },
> { "desat", "desaturation strength", OFFSET(desat),
> AV_OPT_TYPE_DOUBLE, {.dbl = 2}, 0, DBL_MAX, FLAGS },
> { "peak", "signal peak override", OFFSET(peak),
> AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, DBL_MAX, FLAGS },
> --
> 2.32.1 (Apple Git-133)
>
>
[-- Attachment #1.2: 41662642193_.pic.jpg --]
[-- Type: image/jpeg, Size: 420601 bytes --]
[-- Attachment #2: Type: text/plain, Size: 251 bytes --]
_______________________________________________
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] 3+ messages in thread
end of thread, other threads:[~2022-09-08 13:23 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-08 13:22 [FFmpeg-devel] [PATCH v1 1/1] avfilter/vf_tonemap: add herimte tone mapping mirsfang
-- strict thread matches above, loose matches on Subject: below --
2022-09-08 12:41 mirsfang
2022-09-08 13:04 ` mirs fang
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