| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2017 Vittorio Giovara <vittorio.giovara@gmail.com> | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | /** | ||
| 22 | * @file | ||
| 23 | * tonemap algorithms | ||
| 24 | */ | ||
| 25 | |||
| 26 | #include <float.h> | ||
| 27 | #include <stdio.h> | ||
| 28 | |||
| 29 | #include "libavutil/csp.h" | ||
| 30 | #include "libavutil/imgutils.h" | ||
| 31 | #include "libavutil/internal.h" | ||
| 32 | #include "libavutil/intfloat.h" | ||
| 33 | #include "libavutil/intreadwrite.h" | ||
| 34 | #include "libavutil/opt.h" | ||
| 35 | #include "libavutil/pixdesc.h" | ||
| 36 | |||
| 37 | #include "avfilter.h" | ||
| 38 | #include "colorspace.h" | ||
| 39 | #include "filters.h" | ||
| 40 | #include "video.h" | ||
| 41 | |||
| 42 | enum TonemapAlgorithm { | ||
| 43 | TONEMAP_NONE, | ||
| 44 | TONEMAP_LINEAR, | ||
| 45 | TONEMAP_GAMMA, | ||
| 46 | TONEMAP_CLIP, | ||
| 47 | TONEMAP_REINHARD, | ||
| 48 | TONEMAP_HABLE, | ||
| 49 | TONEMAP_MOBIUS, | ||
| 50 | TONEMAP_MAX, | ||
| 51 | }; | ||
| 52 | |||
| 53 | typedef struct TonemapContext { | ||
| 54 | const AVClass *class; | ||
| 55 | |||
| 56 | enum TonemapAlgorithm tonemap; | ||
| 57 | double param; | ||
| 58 | double desat; | ||
| 59 | double peak; | ||
| 60 | |||
| 61 | const AVLumaCoefficients *coeffs; | ||
| 62 | } TonemapContext; | ||
| 63 | |||
| 64 | ✗ | static av_cold int init(AVFilterContext *ctx) | |
| 65 | { | ||
| 66 | ✗ | TonemapContext *s = ctx->priv; | |
| 67 | |||
| 68 | ✗ | switch(s->tonemap) { | |
| 69 | ✗ | case TONEMAP_GAMMA: | |
| 70 | ✗ | if (isnan(s->param)) | |
| 71 | ✗ | s->param = 1.8f; | |
| 72 | ✗ | break; | |
| 73 | ✗ | case TONEMAP_REINHARD: | |
| 74 | ✗ | if (!isnan(s->param)) | |
| 75 | ✗ | s->param = (1.0f - s->param) / s->param; | |
| 76 | ✗ | break; | |
| 77 | ✗ | case TONEMAP_MOBIUS: | |
| 78 | ✗ | if (isnan(s->param)) | |
| 79 | ✗ | s->param = 0.3f; | |
| 80 | ✗ | break; | |
| 81 | } | ||
| 82 | |||
| 83 | ✗ | if (isnan(s->param)) | |
| 84 | ✗ | s->param = 1.0f; | |
| 85 | |||
| 86 | ✗ | return 0; | |
| 87 | } | ||
| 88 | |||
| 89 | ✗ | static float hable(float in) | |
| 90 | { | ||
| 91 | ✗ | float a = 0.15f, b = 0.50f, c = 0.10f, d = 0.20f, e = 0.02f, f = 0.30f; | |
| 92 | ✗ | return (in * (in * a + b * c) + d * e) / (in * (in * a + b) + d * f) - e / f; | |
| 93 | } | ||
| 94 | |||
| 95 | ✗ | static float mobius(float in, float j, double peak) | |
| 96 | { | ||
| 97 | float a, b; | ||
| 98 | |||
| 99 | ✗ | if (in <= j) | |
| 100 | ✗ | return in; | |
| 101 | |||
| 102 | ✗ | a = -j * j * (peak - 1.0f) / (j * j - 2.0f * j + peak); | |
| 103 | ✗ | b = (j * j - 2.0f * j * peak + peak) / FFMAX(peak - 1.0f, 1e-6); | |
| 104 | |||
| 105 | ✗ | return (b * b + 2.0f * b * j + j * j) / (b - a) * (in + a) / (in + b); | |
| 106 | } | ||
| 107 | |||
| 108 | #define MIX(x,y,a) (x) * (1 - (a)) + (y) * (a) | ||
| 109 | ✗ | static void tonemap(TonemapContext *s, AVFrame *out, const AVFrame *in, | |
| 110 | const AVPixFmtDescriptor *desc, int x, int y, double peak) | ||
| 111 | { | ||
| 112 | ✗ | int map[3] = { desc->comp[0].plane, desc->comp[1].plane, desc->comp[2].plane }; | |
| 113 | ✗ | const float *r_in = (const float *)(in->data[map[0]] + x * desc->comp[map[0]].step + y * in->linesize[map[0]]); | |
| 114 | ✗ | const float *g_in = (const float *)(in->data[map[1]] + x * desc->comp[map[1]].step + y * in->linesize[map[1]]); | |
| 115 | ✗ | const float *b_in = (const float *)(in->data[map[2]] + x * desc->comp[map[2]].step + y * in->linesize[map[2]]); | |
| 116 | ✗ | float *r_out = (float *)(out->data[map[0]] + x * desc->comp[map[0]].step + y * out->linesize[map[0]]); | |
| 117 | ✗ | float *g_out = (float *)(out->data[map[1]] + x * desc->comp[map[1]].step + y * out->linesize[map[1]]); | |
| 118 | ✗ | float *b_out = (float *)(out->data[map[2]] + x * desc->comp[map[2]].step + y * out->linesize[map[2]]); | |
| 119 | float sig, sig_orig; | ||
| 120 | |||
| 121 | /* load values */ | ||
| 122 | ✗ | *r_out = *r_in; | |
| 123 | ✗ | *g_out = *g_in; | |
| 124 | ✗ | *b_out = *b_in; | |
| 125 | |||
| 126 | /* desaturate to prevent unnatural colors */ | ||
| 127 | ✗ | if (s->desat > 0) { | |
| 128 | ✗ | float luma = av_q2d(s->coeffs->cr) * *r_in + av_q2d(s->coeffs->cg) * *g_in + av_q2d(s->coeffs->cb) * *b_in; | |
| 129 | ✗ | float overbright = FFMAX(luma - s->desat, 1e-6) / FFMAX(luma, 1e-6); | |
| 130 | ✗ | *r_out = MIX(*r_in, luma, overbright); | |
| 131 | ✗ | *g_out = MIX(*g_in, luma, overbright); | |
| 132 | ✗ | *b_out = MIX(*b_in, luma, overbright); | |
| 133 | } | ||
| 134 | |||
| 135 | /* pick the brightest component, reducing the value range as necessary | ||
| 136 | * to keep the entire signal in range and preventing discoloration due to | ||
| 137 | * out-of-bounds clipping */ | ||
| 138 | ✗ | sig = FFMAX(FFMAX3(*r_out, *g_out, *b_out), 1e-6); | |
| 139 | ✗ | sig_orig = sig; | |
| 140 | |||
| 141 | ✗ | switch(s->tonemap) { | |
| 142 | ✗ | default: | |
| 143 | case TONEMAP_NONE: | ||
| 144 | // do nothing | ||
| 145 | ✗ | break; | |
| 146 | ✗ | case TONEMAP_LINEAR: | |
| 147 | ✗ | sig = sig * s->param / peak; | |
| 148 | ✗ | break; | |
| 149 | ✗ | case TONEMAP_GAMMA: | |
| 150 | ✗ | sig = sig > 0.05f ? pow(sig / peak, 1.0f / s->param) | |
| 151 | ✗ | : sig * pow(0.05f / peak, 1.0f / s->param) / 0.05f; | |
| 152 | ✗ | break; | |
| 153 | ✗ | case TONEMAP_CLIP: | |
| 154 | ✗ | sig = av_clipf(sig * s->param, 0, 1.0f); | |
| 155 | ✗ | break; | |
| 156 | ✗ | case TONEMAP_HABLE: | |
| 157 | ✗ | sig = hable(sig) / hable(peak); | |
| 158 | ✗ | break; | |
| 159 | ✗ | case TONEMAP_REINHARD: | |
| 160 | ✗ | sig = sig / (sig + s->param) * (peak + s->param) / peak; | |
| 161 | ✗ | break; | |
| 162 | ✗ | case TONEMAP_MOBIUS: | |
| 163 | ✗ | sig = mobius(sig, s->param, peak); | |
| 164 | ✗ | break; | |
| 165 | } | ||
| 166 | |||
| 167 | /* apply the computed scale factor to the color, | ||
| 168 | * linearly to prevent discoloration */ | ||
| 169 | ✗ | *r_out *= sig / sig_orig; | |
| 170 | ✗ | *g_out *= sig / sig_orig; | |
| 171 | ✗ | *b_out *= sig / sig_orig; | |
| 172 | ✗ | } | |
| 173 | |||
| 174 | typedef struct ThreadData { | ||
| 175 | AVFrame *in, *out; | ||
| 176 | const AVPixFmtDescriptor *desc; | ||
| 177 | double peak; | ||
| 178 | } ThreadData; | ||
| 179 | |||
| 180 | ✗ | static int tonemap_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) | |
| 181 | { | ||
| 182 | ✗ | TonemapContext *s = ctx->priv; | |
| 183 | ✗ | ThreadData *td = arg; | |
| 184 | ✗ | AVFrame *in = td->in; | |
| 185 | ✗ | AVFrame *out = td->out; | |
| 186 | ✗ | const AVPixFmtDescriptor *desc = td->desc; | |
| 187 | ✗ | const int slice_start = (in->height * jobnr) / nb_jobs; | |
| 188 | ✗ | const int slice_end = (in->height * (jobnr+1)) / nb_jobs; | |
| 189 | ✗ | double peak = td->peak; | |
| 190 | |||
| 191 | ✗ | for (int y = slice_start; y < slice_end; y++) | |
| 192 | ✗ | for (int x = 0; x < out->width; x++) | |
| 193 | ✗ | tonemap(s, out, in, desc, x, y, peak); | |
| 194 | |||
| 195 | ✗ | return 0; | |
| 196 | } | ||
| 197 | |||
| 198 | ✗ | static int filter_frame(AVFilterLink *link, AVFrame *in) | |
| 199 | { | ||
| 200 | ✗ | AVFilterContext *ctx = link->dst; | |
| 201 | ✗ | TonemapContext *s = ctx->priv; | |
| 202 | ✗ | AVFilterLink *outlink = ctx->outputs[0]; | |
| 203 | ThreadData td; | ||
| 204 | AVFrame *out; | ||
| 205 | ✗ | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format); | |
| 206 | ✗ | const AVPixFmtDescriptor *odesc = av_pix_fmt_desc_get(outlink->format); | |
| 207 | int ret, x, y; | ||
| 208 | ✗ | double peak = s->peak; | |
| 209 | |||
| 210 | ✗ | if (!desc || !odesc) { | |
| 211 | ✗ | av_frame_free(&in); | |
| 212 | ✗ | return AVERROR_BUG; | |
| 213 | } | ||
| 214 | |||
| 215 | ✗ | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
| 216 | ✗ | if (!out) { | |
| 217 | ✗ | av_frame_free(&in); | |
| 218 | ✗ | return AVERROR(ENOMEM); | |
| 219 | } | ||
| 220 | |||
| 221 | ✗ | ret = av_frame_copy_props(out, in); | |
| 222 | ✗ | if (ret < 0) { | |
| 223 | ✗ | av_frame_free(&in); | |
| 224 | ✗ | av_frame_free(&out); | |
| 225 | ✗ | return ret; | |
| 226 | } | ||
| 227 | |||
| 228 | /* input and output transfer will be linear */ | ||
| 229 | ✗ | if (in->color_trc == AVCOL_TRC_UNSPECIFIED) { | |
| 230 | ✗ | av_log(ctx, AV_LOG_WARNING, "Untagged transfer, assuming linear light\n"); | |
| 231 | ✗ | out->color_trc = AVCOL_TRC_LINEAR; | |
| 232 | ✗ | } else if (in->color_trc != AVCOL_TRC_LINEAR) | |
| 233 | ✗ | av_log(ctx, AV_LOG_WARNING, "Tonemapping works on linear light only\n"); | |
| 234 | |||
| 235 | /* read peak from side data if not passed in */ | ||
| 236 | ✗ | if (!peak) { | |
| 237 | ✗ | peak = ff_determine_signal_peak(in); | |
| 238 | ✗ | av_log(ctx, AV_LOG_DEBUG, "Computed signal peak: %f\n", peak); | |
| 239 | } | ||
| 240 | |||
| 241 | /* load original color space even if pixel format is RGB to compute overbrights */ | ||
| 242 | ✗ | s->coeffs = av_csp_luma_coeffs_from_avcsp(in->colorspace); | |
| 243 | ✗ | if (s->desat > 0 && (in->colorspace == AVCOL_SPC_UNSPECIFIED || !s->coeffs)) { | |
| 244 | ✗ | if (in->colorspace == AVCOL_SPC_UNSPECIFIED) | |
| 245 | ✗ | av_log(ctx, AV_LOG_WARNING, "Missing color space information, "); | |
| 246 | ✗ | else if (!s->coeffs) | |
| 247 | ✗ | av_log(ctx, AV_LOG_WARNING, "Unsupported color space '%s', ", | |
| 248 | ✗ | av_color_space_name(in->colorspace)); | |
| 249 | ✗ | av_log(ctx, AV_LOG_WARNING, "desaturation is disabled\n"); | |
| 250 | ✗ | s->desat = 0; | |
| 251 | } | ||
| 252 | |||
| 253 | /* do the tone map */ | ||
| 254 | ✗ | td.out = out; | |
| 255 | ✗ | td.in = in; | |
| 256 | ✗ | td.desc = desc; | |
| 257 | ✗ | td.peak = peak; | |
| 258 | ✗ | ff_filter_execute(ctx, tonemap_slice, &td, NULL, | |
| 259 | ✗ | FFMIN(in->height, ff_filter_get_nb_threads(ctx))); | |
| 260 | |||
| 261 | /* copy/generate alpha if needed */ | ||
| 262 | ✗ | if (desc->flags & AV_PIX_FMT_FLAG_ALPHA && odesc->flags & AV_PIX_FMT_FLAG_ALPHA) { | |
| 263 | ✗ | av_image_copy_plane(out->data[3], out->linesize[3], | |
| 264 | ✗ | in->data[3], in->linesize[3], | |
| 265 | ✗ | out->linesize[3], outlink->h); | |
| 266 | ✗ | } else if (odesc->flags & AV_PIX_FMT_FLAG_ALPHA) { | |
| 267 | ✗ | for (y = 0; y < out->height; y++) { | |
| 268 | ✗ | for (x = 0; x < out->width; x++) { | |
| 269 | ✗ | AV_WN32(out->data[3] + x * odesc->comp[3].step + y * out->linesize[3], | |
| 270 | av_float2int(1.0f)); | ||
| 271 | } | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | ✗ | av_frame_free(&in); | |
| 276 | |||
| 277 | ✗ | ff_update_hdr_metadata(out, peak); | |
| 278 | |||
| 279 | ✗ | return ff_filter_frame(outlink, out); | |
| 280 | } | ||
| 281 | |||
| 282 | #define OFFSET(x) offsetof(TonemapContext, x) | ||
| 283 | #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | ||
| 284 | static const AVOption tonemap_options[] = { | ||
| 285 | { "tonemap", "tonemap algorithm selection", OFFSET(tonemap), AV_OPT_TYPE_INT, {.i64 = TONEMAP_NONE}, TONEMAP_NONE, TONEMAP_MAX - 1, FLAGS, .unit = "tonemap" }, | ||
| 286 | { "none", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_NONE}, 0, 0, FLAGS, .unit = "tonemap" }, | ||
| 287 | { "linear", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_LINEAR}, 0, 0, FLAGS, .unit = "tonemap" }, | ||
| 288 | { "gamma", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_GAMMA}, 0, 0, FLAGS, .unit = "tonemap" }, | ||
| 289 | { "clip", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_CLIP}, 0, 0, FLAGS, .unit = "tonemap" }, | ||
| 290 | { "reinhard", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_REINHARD}, 0, 0, FLAGS, .unit = "tonemap" }, | ||
| 291 | { "hable", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_HABLE}, 0, 0, FLAGS, .unit = "tonemap" }, | ||
| 292 | { "mobius", 0, 0, AV_OPT_TYPE_CONST, {.i64 = TONEMAP_MOBIUS}, 0, 0, FLAGS, .unit = "tonemap" }, | ||
| 293 | { "param", "tonemap parameter", OFFSET(param), AV_OPT_TYPE_DOUBLE, {.dbl = NAN}, DBL_MIN, DBL_MAX, FLAGS }, | ||
| 294 | { "desat", "desaturation strength", OFFSET(desat), AV_OPT_TYPE_DOUBLE, {.dbl = 2}, 0, DBL_MAX, FLAGS }, | ||
| 295 | { "peak", "signal peak override", OFFSET(peak), AV_OPT_TYPE_DOUBLE, {.dbl = 0}, 0, DBL_MAX, FLAGS }, | ||
| 296 | { NULL } | ||
| 297 | }; | ||
| 298 | |||
| 299 | AVFILTER_DEFINE_CLASS(tonemap); | ||
| 300 | |||
| 301 | static const AVFilterPad tonemap_inputs[] = { | ||
| 302 | { | ||
| 303 | .name = "default", | ||
| 304 | .type = AVMEDIA_TYPE_VIDEO, | ||
| 305 | .filter_frame = filter_frame, | ||
| 306 | }, | ||
| 307 | }; | ||
| 308 | |||
| 309 | const FFFilter ff_vf_tonemap = { | ||
| 310 | .p.name = "tonemap", | ||
| 311 | .p.description = NULL_IF_CONFIG_SMALL("Conversion to/from different dynamic ranges."), | ||
| 312 | .p.priv_class = &tonemap_class, | ||
| 313 | .p.flags = AVFILTER_FLAG_SLICE_THREADS, | ||
| 314 | .init = init, | ||
| 315 | .priv_size = sizeof(TonemapContext), | ||
| 316 | FILTER_INPUTS(tonemap_inputs), | ||
| 317 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
| 318 | FILTER_PIXFMTS(AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32), | ||
| 319 | }; | ||
| 320 |