[FFmpeg-cvslog] Add tonemap filter

Vittorio Giovara git at videolan.org
Tue Aug 15 16:52:49 EEST 2017


ffmpeg | branch: master | Vittorio Giovara <vittorio.giovara at gmail.com> | Mon Jul 10 10:34:30 2017 -0400| [62dfa2ba14d9adbf88e8748a7e1c5ce68f0f8fa8] | committer: Vittorio Giovara

Add tonemap filter

Based off mpv automatic tonemapping capabilities.

Signed-off-by: Vittorio Giovara <vittorio.giovara at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=62dfa2ba14d9adbf88e8748a7e1c5ce68f0f8fa8
---

 doc/filters.texi         | 108 +++++++++++++++
 libavfilter/Makefile     |   1 +
 libavfilter/allfilters.c |   1 +
 libavfilter/version.h    |   2 +-
 libavfilter/vf_tonemap.c | 354 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 465 insertions(+), 1 deletion(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 8b0c6d920b..502ebd5e45 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -14442,6 +14442,113 @@ Vertical low-pass filtering can only be enabled for @option{mode}
 
 @end table
 
+ at section tonemap
+Tone map colors from different dynamic ranges.
+
+This filter expects data in single precision floating point, as it needs to
+operate on (and can output) out-of-range values. Another filter, such as
+ at ref{zscale}, is needed to convert the resulting frame to a usable format.
+
+The tonemapping algorithms implemented only work on linear light, so input
+data should be linearized beforehand (and possibly correctly tagged).
+
+ at example
+ffmpeg -i INPUT -vf zscale=transfer=linear,tonemap=clip,zscale=transfer=bt709,format=yuv420p OUTPUT
+ at end example
+
+ at subsection Options
+The filter accepts the following options.
+
+ at table @option
+ at item tonemap
+Set the tone map algorithm to use.
+
+Possible values are:
+ at table @var
+ at item none
+Do not apply any tone map, only desaturate overbright pixels.
+
+ at item clip
+Hard-clip any out-of-range values. Use it for perfect color accuracy for
+in-range values, while distorting out-of-range values.
+
+ at item linear
+Stretch the entire reference gamut to a linear multiple of the display.
+
+ at item gamma
+Fit a logarithmic transfer between the tone curves.
+
+ at item reinhard
+Preserve overall image brightness with a simple curve, using nonlinear
+contrast, which results in flattening details and degrading color accuracy.
+
+ at item hable
+Peserve both dark and bright details better than @var{reinhard}, at the cost
+of slightly darkening everything. Use it when detail preservation is more
+important than color and brightness accuracy.
+
+ at item mobius
+Smoothly map out-of-range values, while retaining contrast and colors for
+in-range material as much as possible. Use it when color accuracy is more
+important than detail preservation.
+ at end table
+
+Default is none.
+
+ at item param
+Tune the tone mapping algorithm.
+
+This affects the following algorithms:
+ at table @var
+ at item none
+Ignored.
+
+ at item linear
+Specifies the scale factor to use while stretching.
+Default to 1.0.
+
+ at item gamma
+Specifies the exponent of the function.
+Default to 1.8.
+
+ at item clip
+Specify an extra linear coefficient to multiply into the signal before clipping.
+Default to 1.0.
+
+ at item reinhard
+Specify the local contrast coefficient at the display peak.
+Default to 0.5, which means that in-gamut values will be about half as bright
+as when clipping.
+
+ at item hable
+Ignored.
+
+ at item mobius
+Specify the transition point from linear to mobius transform. Every value
+below this point is guaranteed to be mapped 1:1. The higher the value, the
+more accurate the result will be, at the cost of losing bright details.
+Default to 0.3, which due to the steep initial slope still preserves in-range
+colors fairly accurately.
+ at end table
+
+ at item desat
+Apply desaturation for highlights that exceed this level of brightness. The
+higher the parameter, the more color information will be preserved. This
+setting helps prevent unnaturally blown-out colors for super-highlights, by
+(smoothly) turning into white instead. This makes images feel more natural,
+at the cost of reducing information about out-of-range colors.
+
+The default of 2.0 is somewhat conservative and will mostly just apply to
+skies or directly sunlit surfaces. A setting of 0.0 disables this option.
+
+This option works only if the input frame has a supported color tag.
+
+ at item peak
+Override signal/nominal/reference peak with this value. Useful when the
+embedded peak information in display metadata is not reliable or when tone
+mapping from a lower range to a higher range.
+ at end table
+
 @section transpose
 
 Transpose rows with columns in the input video and optionally flip it.
@@ -15627,6 +15734,7 @@ zoompan=z='min(max(zoom,pzoom)+0.0015,1.5)':d=1:x='iw/2-(iw/zoom/2)':y='ih/2-(ih
 @end example
 @end itemize
 
+ at anchor{zscale}
 @section zscale
 Scale (resize) the input video, using the z.lib library:
 https://github.com/sekrit-twc/zimg.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 06b915fc91..545b871cd7 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -314,6 +314,7 @@ OBJS-$(CONFIG_THUMBNAIL_FILTER)              += vf_thumbnail.o
 OBJS-$(CONFIG_TILE_FILTER)                   += vf_tile.o
 OBJS-$(CONFIG_TINTERLACE_FILTER)             += vf_tinterlace.o
 OBJS-$(CONFIG_TLUT2_FILTER)                  += vf_lut2.o framesync2.o
+OBJS-$(CONFIG_TONEMAP_FILTER)                += vf_tonemap.o
 OBJS-$(CONFIG_TRANSPOSE_FILTER)              += vf_transpose.o
 OBJS-$(CONFIG_TRIM_FILTER)                   += trim.o
 OBJS-$(CONFIG_UNPREMULTIPLY_FILTER)          += vf_premultiply.o framesync2.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index e58c0d5c82..f1dacaffa8 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -325,6 +325,7 @@ static void register_all(void)
     REGISTER_FILTER(TILE,           tile,           vf);
     REGISTER_FILTER(TINTERLACE,     tinterlace,     vf);
     REGISTER_FILTER(TLUT2,          tlut2,          vf);
+    REGISTER_FILTER(TONEMAP,        tonemap,        vf);
     REGISTER_FILTER(TRANSPOSE,      transpose,      vf);
     REGISTER_FILTER(TRIM,           trim,           vf);
     REGISTER_FILTER(UNPREMULTIPLY,  unpremultiply,  vf);
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 1d37fd8e4b..5efb648c6d 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -30,7 +30,7 @@
 #include "libavutil/version.h"
 
 #define LIBAVFILTER_VERSION_MAJOR   6
-#define LIBAVFILTER_VERSION_MINOR  98
+#define LIBAVFILTER_VERSION_MINOR  99
 #define LIBAVFILTER_VERSION_MICRO 100
 
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
diff --git a/libavfilter/vf_tonemap.c b/libavfilter/vf_tonemap.c
new file mode 100644
index 0000000000..10308bdb16
--- /dev/null
+++ b/libavfilter/vf_tonemap.c
@@ -0,0 +1,354 @@
+/*
+ * Copyright (c) 2017 Vittorio Giovara <vittorio.giovara at gmail.com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * tonemap algorithms
+ */
+
+#include <float.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "libavutil/imgutils.h"
+#include "libavutil/internal.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/mastering_display_metadata.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+#define REFERENCE_WHITE 100.0f
+
+enum TonemapAlgorithm {
+    TONEMAP_NONE,
+    TONEMAP_LINEAR,
+    TONEMAP_GAMMA,
+    TONEMAP_CLIP,
+    TONEMAP_REINHARD,
+    TONEMAP_HABLE,
+    TONEMAP_MOBIUS,
+    TONEMAP_MAX,
+};
+
+typedef struct LumaCoefficients {
+    double cr, cg, cb;
+} LumaCoefficients;
+
+
+static const struct LumaCoefficients luma_coefficients[AVCOL_SPC_NB] = {
+    [AVCOL_SPC_FCC]        = { 0.30,   0.59,   0.11   },
+    [AVCOL_SPC_BT470BG]    = { 0.299,  0.587,  0.114  },
+    [AVCOL_SPC_SMPTE170M]  = { 0.299,  0.587,  0.114  },
+    [AVCOL_SPC_BT709]      = { 0.2126, 0.7152, 0.0722 },
+    [AVCOL_SPC_SMPTE240M]  = { 0.212,  0.701,  0.087  },
+    [AVCOL_SPC_BT2020_NCL] = { 0.2627, 0.6780, 0.0593 },
+    [AVCOL_SPC_BT2020_CL]  = { 0.2627, 0.6780, 0.0593 },
+};
+
+typedef struct TonemapContext {
+    const AVClass *class;
+
+    enum TonemapAlgorithm tonemap;
+    double param;
+    double desat;
+    double peak;
+
+    const LumaCoefficients *coeffs;
+} TonemapContext;
+
+static const enum AVPixelFormat pix_fmts[] = {
+    AV_PIX_FMT_GBRPF32,
+    AV_PIX_FMT_GBRAPF32,
+    AV_PIX_FMT_NONE,
+};
+
+static int query_formats(AVFilterContext *ctx)
+{
+    return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    TonemapContext *s = ctx->priv;
+
+    switch(s->tonemap) {
+    case TONEMAP_GAMMA:
+        if (isnan(s->param))
+            s->param = 1.8f;
+        break;
+    case TONEMAP_REINHARD:
+        if (!isnan(s->param))
+            s->param = (1.0f - s->param) / s->param;
+        break;
+    case TONEMAP_MOBIUS:
+        if (isnan(s->param))
+            s->param = 0.3f;
+        break;
+    }
+
+    if (isnan(s->param))
+        s->param = 1.0f;
+
+    return 0;
+}
+
+static double determine_signal_peak(AVFrame *in)
+{
+    AVFrameSideData *sd = av_frame_get_side_data(in, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
+    double peak = 0;
+
+    if (sd) {
+        AVContentLightMetadata *clm = (AVContentLightMetadata *)sd->data;
+        peak = clm->MaxCLL / REFERENCE_WHITE;
+    }
+
+    sd = av_frame_get_side_data(in, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
+    if (!peak && sd) {
+        AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)sd->data;
+        if (metadata->has_luminance)
+            peak = av_q2d(metadata->max_luminance) / REFERENCE_WHITE;
+    }
+
+    /* smpte2084 needs the side data above to work correctly
+     * if missing, assume that the original transfer was arib-std-b67 */
+    if (!peak)
+        peak = 12;
+
+    return peak;
+}
+
+static float hable(float in)
+{
+    float a = 0.15f, b = 0.50f, c = 0.10f, d = 0.20f, e = 0.02f, f = 0.30f;
+    return (in * (in * a + b * c) + d * e) / (in * (in * a + b) + d * f) - e / f;
+}
+
+static float mobius(float in, float j, double peak)
+{
+    float a, b;
+
+    if (in <= j)
+        return in;
+
+    a = -j * j * (peak - 1.0f) / (j * j - 2.0f * j + peak);
+    b = (j * j - 2.0f * j * peak + peak) / FFMAX(peak - 1.0f, 1e-6);
+
+    return (b * b + 2.0f * b * j + j * j) / (b - a) * (in + a) / (in + b);
+}
+
+#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)
+{
+    const float *r_in = (const float *)(in->data[0] + x * desc->comp[0].step + y * in->linesize[0]);
+    const float *b_in = (const float *)(in->data[1] + x * desc->comp[1].step + y * in->linesize[1]);
+    const float *g_in = (const float *)(in->data[2] + x * desc->comp[2].step + y * in->linesize[2]);
+    float *r_out = (float *)(out->data[0] + x * desc->comp[0].step + y * out->linesize[0]);
+    float *b_out = (float *)(out->data[1] + x * desc->comp[1].step + y * out->linesize[1]);
+    float *g_out = (float *)(out->data[2] + x * desc->comp[2].step + y * out->linesize[2]);
+    float sig, sig_orig;
+
+    /* load values */
+    *r_out = *r_in;
+    *b_out = *b_in;
+    *g_out = *g_in;
+
+    /* desaturate to prevent unnatural colors */
+    if (s->desat > 0) {
+        float luma = s->coeffs->cr * *r_in + s->coeffs->cg * *g_in + s->coeffs->cb * *b_in;
+        float overbright = FFMAX(luma - s->desat, 1e-6) / FFMAX(luma, 1e-6);
+        *r_out = MIX(*r_in, luma, overbright);
+        *g_out = MIX(*g_in, luma, overbright);
+        *b_out = MIX(*b_in, luma, overbright);
+    }
+
+    /* pick the brightest component, reducing the value range as necessary
+     * to keep the entire signal in range and preventing discoloration due to
+     * out-of-bounds clipping */
+    sig = FFMAX(FFMAX3(*r_out, *g_out, *b_out), 1e-6);
+    sig_orig = sig;
+
+    switch(s->tonemap) {
+    default:
+    case TONEMAP_NONE:
+        // do nothing
+        break;
+    case TONEMAP_LINEAR:
+        sig = sig * s->param / peak;
+        break;
+    case TONEMAP_GAMMA:
+        sig = sig > 0.05f ? pow(sig / peak, 1.0f / s->param)
+                          : sig * pow(0.05f / peak, 1.0f / s->param) / 0.05f;
+        break;
+    case TONEMAP_CLIP:
+        sig = av_clipf(sig * s->param, 0, 1.0f);
+        break;
+    case TONEMAP_HABLE:
+        sig = hable(sig) / hable(peak);
+        break;
+    case TONEMAP_REINHARD:
+        sig = sig / (sig + s->param) * (peak + s->param) / peak;
+        break;
+    case TONEMAP_MOBIUS:
+        sig = mobius(sig, s->param, peak);
+        break;
+    }
+
+    /* apply the computed scale factor to the color,
+     * linearly to prevent discoloration */
+    *r_out *= sig / sig_orig;
+    *g_out *= sig / sig_orig;
+    *b_out *= sig / sig_orig;
+}
+
+static int filter_frame(AVFilterLink *link, AVFrame *in)
+{
+    TonemapContext *s = link->dst->priv;
+    AVFilterLink *outlink = link->dst->outputs[0];
+    AVFrame *out;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
+    const AVPixFmtDescriptor *odesc = av_pix_fmt_desc_get(outlink->format);
+    int ret, x, y;
+    double peak = s->peak;
+
+    if (!desc || !odesc) {
+        av_frame_free(&in);
+        return AVERROR_BUG;
+    }
+
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+    if (!out) {
+        av_frame_free(&in);
+        return AVERROR(ENOMEM);
+    }
+
+    ret = av_frame_copy_props(out, in);
+    if (ret < 0) {
+        av_frame_free(&in);
+        av_frame_free(&out);
+        return ret;
+    }
+
+    /* input and output transfer will be linear */
+    if (in->color_trc == AVCOL_TRC_UNSPECIFIED) {
+        av_log(s, AV_LOG_WARNING, "Untagged transfer, assuming linear light\n");
+        out->color_trc = AVCOL_TRC_LINEAR;
+    } else if (in->color_trc != AVCOL_TRC_LINEAR)
+        av_log(s, AV_LOG_WARNING, "Tonemapping works on linear light only\n");
+
+    /* read peak from side data if not passed in */
+    if (!peak) {
+        peak = determine_signal_peak(in);
+        av_log(s, AV_LOG_DEBUG, "Computed signal peak: %f\n", peak);
+    }
+
+    /* load original color space even if pixel format is RGB to compute overbrights */
+    s->coeffs = &luma_coefficients[in->colorspace];
+    if (s->desat > 0 && (in->colorspace == AVCOL_SPC_UNSPECIFIED || !s->coeffs)) {
+        if (in->colorspace == AVCOL_SPC_UNSPECIFIED)
+            av_log(s, AV_LOG_WARNING, "Missing color space information, ");
+        else if (!s->coeffs)
+            av_log(s, AV_LOG_WARNING, "Unsupported color space '%s', ",
+                   av_color_space_name(in->colorspace));
+        av_log(s, AV_LOG_WARNING, "desaturation is disabled\n");
+        s->desat = 0;
+    }
+
+    /* do the tone map */
+    for (y = 0; y < out->height; y++)
+        for (x = 0; x < out->width; x++)
+            tonemap(s, out, in, desc, x, y, peak);
+
+    /* copy/generate alpha if needed */
+    if (desc->flags & AV_PIX_FMT_FLAG_ALPHA && odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
+        av_image_copy_plane(out->data[3], out->linesize[3],
+                            in->data[3], in->linesize[3],
+                            out->linesize[3], outlink->h);
+    } else if (odesc->flags & AV_PIX_FMT_FLAG_ALPHA) {
+        for (y = 0; y < out->height; y++) {
+            for (x = 0; x < out->width; x++) {
+                AV_WN32(out->data[3] + x * odesc->comp[3].step + y * out->linesize[3],
+                        av_float2int(1.0f));
+            }
+        }
+    }
+
+    av_frame_free(&in);
+
+    return ff_filter_frame(outlink, out);
+}
+
+#define OFFSET(x) offsetof(TonemapContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
+static const AVOption tonemap_options[] = {
+    { "tonemap",      "tonemap algorithm selection", OFFSET(tonemap), AV_OPT_TYPE_INT, {.i64 = TONEMAP_NONE}, TONEMAP_NONE, TONEMAP_MAX - 1, FLAGS, "tonemap" },
+    {     "none",     0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_NONE},              0, 0, FLAGS, "tonemap" },
+    {     "linear",   0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_LINEAR},            0, 0, FLAGS, "tonemap" },
+    {     "gamma",    0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_GAMMA},             0, 0, FLAGS, "tonemap" },
+    {     "clip",     0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_CLIP},              0, 0, FLAGS, "tonemap" },
+    {     "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" },
+    { "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 },
+    { NULL }
+};
+
+static const AVClass tonemap_class = {
+    .class_name       = "tonemap",
+    .item_name        = av_default_item_name,
+    .option           = tonemap_options,
+    .version          = LIBAVUTIL_VERSION_INT,
+    .category         = AV_CLASS_CATEGORY_FILTER,
+};
+
+static const AVFilterPad tonemap_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad tonemap_outputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_tonemap = {
+    .name            = "tonemap",
+    .description     = NULL_IF_CONFIG_SMALL("Conversion to/from different dynamic ranges."),
+    .init            = init,
+    .query_formats   = query_formats,
+    .priv_size       = sizeof(TonemapContext),
+    .priv_class      = &tonemap_class,
+    .inputs          = tonemap_inputs,
+    .outputs         = tonemap_outputs,
+};



More information about the ffmpeg-cvslog mailing list